zusb.c revision 1.1 1 1.1 chris /* $NetBSD: zusb.c,v 1.1 2008/03/31 23:32:43 chris Exp $ */
2 1.1 chris
3 1.1 chris /*
4 1.1 chris * Copyright (c) 2008 Christopher Gilbert
5 1.1 chris * All rights reserved.
6 1.1 chris *
7 1.1 chris * 1. Redistributions of source code must retain the above copyright
8 1.1 chris * notice, this list of conditions and the following disclaimer.
9 1.1 chris * 2. Redistributions in binary form must reproduce the above copyright
10 1.1 chris * notice, this list of conditions and the following disclaimer in the
11 1.1 chris * documentation and/or other materials provided with the distribution.
12 1.1 chris * 3. The name of the company nor the name of the author may be used to
13 1.1 chris * endorse or promote products derived from this software without specific
14 1.1 chris * prior written permission.
15 1.1 chris *
16 1.1 chris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 1.1 chris * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 1.1 chris * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 chris * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20 1.1 chris * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.1 chris * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.1 chris * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 chris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 chris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 chris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 chris * SUCH DAMAGE.
27 1.1 chris */
28 1.1 chris
29 1.1 chris #include <sys/cdefs.h>
30 1.1 chris __KERNEL_RCSID(0, "$NetBSD: zusb.c,v 1.1 2008/03/31 23:32:43 chris Exp $");
31 1.1 chris
32 1.1 chris #include <sys/param.h>
33 1.1 chris #include <sys/device.h>
34 1.1 chris #include <sys/kernel.h>
35 1.1 chris
36 1.1 chris #include <arm/xscale/pxa2x0reg.h>
37 1.1 chris #include <arm/xscale/pxa2x0var.h>
38 1.1 chris #include <arm/xscale/pxa2x0_gpio.h>
39 1.1 chris
40 1.1 chris #include <machine/intr.h>
41 1.1 chris #include <machine/bus.h>
42 1.1 chris
43 1.1 chris #include <zaurus/zaurus/zaurus_reg.h>
44 1.1 chris #include <zaurus/zaurus/zaurus_var.h>
45 1.1 chris
46 1.1 chris struct zusb_softc {
47 1.1 chris struct device sc_dev;
48 1.1 chris void *sc_client_ih;
49 1.1 chris void *sc_host_ih;
50 1.1 chris bus_space_tag_t sc_iot;
51 1.1 chris bus_space_handle_t sc_ioh;
52 1.1 chris };
53 1.1 chris
54 1.1 chris static int zusb_match(struct device *, struct cfdata *, void *);
55 1.1 chris static void zusb_attach(struct device *, struct device *, void *);
56 1.1 chris
57 1.1 chris CFATTACH_DECL(zusb, sizeof(struct zusb_softc),
58 1.1 chris zusb_match, zusb_attach, NULL, NULL);
59 1.1 chris
60 1.1 chris static int zusb_client_intr(void *);
61 1.1 chris static int zusb_host_intr(void *);
62 1.1 chris static void zusb_test_and_enabled_host_port(struct zusb_softc *);
63 1.1 chris
64 1.1 chris static int
65 1.1 chris zusb_match(struct device *parent, struct cfdata *cf, void *aux)
66 1.1 chris {
67 1.1 chris
68 1.1 chris if (ZAURUS_ISC3000)
69 1.1 chris return 1;
70 1.1 chris return 0;
71 1.1 chris }
72 1.1 chris
73 1.1 chris static void
74 1.1 chris zusb_attach(struct device *parent, struct device *self, void *aux)
75 1.1 chris {
76 1.1 chris struct zusb_softc *sc = (struct zusb_softc *)self;
77 1.1 chris struct pxaip_attach_args *pxa = aux;
78 1.1 chris
79 1.1 chris sc->sc_iot = pxa->pxa_iot;
80 1.1 chris
81 1.1 chris /* Map I/O space */
82 1.1 chris if (bus_space_map(sc->sc_iot, PXA2X0_USBDC_BASE, PXA270_USBDC_SIZE, 0,
83 1.1 chris &sc->sc_ioh)) {
84 1.1 chris aprint_error(": couldn't map memory space\n");
85 1.1 chris return;
86 1.1 chris }
87 1.1 chris
88 1.1 chris pxa2x0_gpio_set_function(C3000_USB_DEVICE_PIN, GPIO_IN);
89 1.1 chris sc->sc_client_ih = pxa2x0_gpio_intr_establish(C3000_USB_DEVICE_PIN,
90 1.1 chris IST_EDGE_BOTH, IPL_BIO, zusb_client_intr, sc);
91 1.1 chris
92 1.1 chris pxa2x0_gpio_set_function(C3000_USB_HOST_PIN, GPIO_IN);
93 1.1 chris sc->sc_host_ih = pxa2x0_gpio_intr_establish(C3000_USB_HOST_PIN,
94 1.1 chris IST_EDGE_BOTH, IPL_BIO, zusb_host_intr, sc);
95 1.1 chris
96 1.1 chris /* configure port 2 for input */
97 1.1 chris bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR,
98 1.1 chris USBDC_UP2OCR_HXS | USBDC_UP2OCR_HXOE |
99 1.1 chris USBDC_UP2OCR_DPPDE | USBDC_UP2OCR_DMPDE);
100 1.1 chris
101 1.1 chris zusb_test_and_enabled_host_port(sc);
102 1.1 chris
103 1.1 chris printf(": USB Mode detection\n");
104 1.1 chris }
105 1.1 chris
106 1.1 chris static int
107 1.1 chris zusb_client_intr(void *v)
108 1.1 chris {
109 1.1 chris struct zusb_softc *sc = v;
110 1.1 chris printf("USB client cable changed\n");
111 1.1 chris zusb_test_and_enabled_host_port(sc);
112 1.1 chris
113 1.1 chris return 1;
114 1.1 chris }
115 1.1 chris
116 1.1 chris static int
117 1.1 chris zusb_host_intr(void *v)
118 1.1 chris {
119 1.1 chris struct zusb_softc *sc = v;
120 1.1 chris
121 1.1 chris printf("USB host cable changed\n");
122 1.1 chris zusb_test_and_enabled_host_port(sc);
123 1.1 chris
124 1.1 chris return 1;
125 1.1 chris }
126 1.1 chris
127 1.1 chris static void
128 1.1 chris zusb_test_and_enabled_host_port(struct zusb_softc *sc)
129 1.1 chris {
130 1.1 chris int host_cable = pxa2x0_gpio_get_bit(C3000_USB_HOST_PIN);
131 1.1 chris int client_cable = pxa2x0_gpio_get_bit(C3000_USB_DEVICE_PIN);
132 1.1 chris
133 1.1 chris printf("USB cable: host %d, client %d\n", host_cable, client_cable);
134 1.1 chris if (!host_cable) {
135 1.1 chris pxa2x0_gpio_set_function(C3000_USB_HOST_POWER_PIN, GPIO_OUT | GPIO_SET);
136 1.1 chris printf("USB host power enabled\n");
137 1.1 chris } else {
138 1.1 chris pxa2x0_gpio_set_function(C3000_USB_HOST_POWER_PIN, GPIO_OUT | GPIO_CLR);
139 1.1 chris printf("USB host power disabled\n");
140 1.1 chris }
141 1.1 chris }
142 1.1 chris
143