pxa2x0_ohci.c revision 1.1.6.2 1 1.1.6.2 ad /* $NetBSD: pxa2x0_ohci.c,v 1.1.6.2 2007/01/12 01:00:42 ad Exp $ */
2 1.1.6.2 ad /* $OpenBSD: pxa2x0_ohci.c,v 1.19 2005/04/08 02:32:54 dlg Exp $ */
3 1.1.6.2 ad
4 1.1.6.2 ad /*
5 1.1.6.2 ad * Copyright (c) 2005 David Gwynne <dlg (at) openbsd.org>
6 1.1.6.2 ad *
7 1.1.6.2 ad * Permission to use, copy, modify, and distribute this software for any
8 1.1.6.2 ad * purpose with or without fee is hereby granted, provided that the above
9 1.1.6.2 ad * copyright notice and this permission notice appear in all copies.
10 1.1.6.2 ad *
11 1.1.6.2 ad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1.6.2 ad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1.6.2 ad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1.6.2 ad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1.6.2 ad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1.6.2 ad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1.6.2 ad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1.6.2 ad */
19 1.1.6.2 ad
20 1.1.6.2 ad #include <sys/param.h>
21 1.1.6.2 ad #include <sys/systm.h>
22 1.1.6.2 ad #include <sys/device.h>
23 1.1.6.2 ad #include <sys/kernel.h>
24 1.1.6.2 ad
25 1.1.6.2 ad #include <machine/intr.h>
26 1.1.6.2 ad #include <machine/bus.h>
27 1.1.6.2 ad
28 1.1.6.2 ad #include <dev/usb/usb.h>
29 1.1.6.2 ad #include <dev/usb/usbdi.h>
30 1.1.6.2 ad #include <dev/usb/usbdivar.h>
31 1.1.6.2 ad #include <dev/usb/usb_mem.h>
32 1.1.6.2 ad
33 1.1.6.2 ad #include <dev/usb/ohcireg.h>
34 1.1.6.2 ad #include <dev/usb/ohcivar.h>
35 1.1.6.2 ad
36 1.1.6.2 ad #include <arm/xscale/pxa2x0cpu.h>
37 1.1.6.2 ad #include <arm/xscale/pxa2x0reg.h>
38 1.1.6.2 ad #include <arm/xscale/pxa2x0var.h>
39 1.1.6.2 ad #include <arm/xscale/pxa2x0_gpio.h>
40 1.1.6.2 ad
41 1.1.6.2 ad struct pxaohci_softc {
42 1.1.6.2 ad ohci_softc_t sc;
43 1.1.6.2 ad
44 1.1.6.2 ad void *sc_ih;
45 1.1.6.2 ad };
46 1.1.6.2 ad
47 1.1.6.2 ad static int pxaohci_match(struct device *, struct cfdata *, void *);
48 1.1.6.2 ad static void pxaohci_attach(struct device *, struct device *, void *);
49 1.1.6.2 ad static int pxaohci_detach(struct device *, int);
50 1.1.6.2 ad
51 1.1.6.2 ad CFATTACH_DECL(pxaohci, sizeof(struct pxaohci_softc),
52 1.1.6.2 ad pxaohci_match, pxaohci_attach, pxaohci_detach, ohci_activate);
53 1.1.6.2 ad
54 1.1.6.2 ad static void pxaohci_power(int, void *);
55 1.1.6.2 ad static void pxaohci_enable(struct pxaohci_softc *);
56 1.1.6.2 ad static void pxaohci_disable(struct pxaohci_softc *);
57 1.1.6.2 ad
58 1.1.6.2 ad #define HREAD4(sc,r) bus_space_read_4((sc)->sc.iot, (sc)->sc.ioh, (r))
59 1.1.6.2 ad #define HWRITE4(sc,r,v) bus_space_write_4((sc)->sc.iot, (sc)->sc.ioh, (r), (v))
60 1.1.6.2 ad
61 1.1.6.2 ad static int
62 1.1.6.2 ad pxaohci_match(struct device *parent, struct cfdata *cf, void *aux)
63 1.1.6.2 ad {
64 1.1.6.2 ad
65 1.1.6.2 ad if (CPU_IS_PXA270)
66 1.1.6.2 ad return 1;
67 1.1.6.2 ad return 0;
68 1.1.6.2 ad }
69 1.1.6.2 ad
70 1.1.6.2 ad static void
71 1.1.6.2 ad pxaohci_attach(struct device *parent, struct device *self, void *aux)
72 1.1.6.2 ad {
73 1.1.6.2 ad struct pxaohci_softc *sc = (struct pxaohci_softc *)self;
74 1.1.6.2 ad struct pxaip_attach_args *pxa = aux;
75 1.1.6.2 ad usbd_status r;
76 1.1.6.2 ad
77 1.1.6.2 ad {
78 1.1.6.2 ad extern int ohcidebug;
79 1.1.6.2 ad ohcidebug = 16;
80 1.1.6.2 ad }
81 1.1.6.2 ad
82 1.1.6.2 ad sc->sc.iot = pxa->pxa_iot;
83 1.1.6.2 ad sc->sc.sc_bus.dmatag = pxa->pxa_dmat;
84 1.1.6.2 ad sc->sc.sc_size = 0;
85 1.1.6.2 ad sc->sc_ih = NULL;
86 1.1.6.2 ad
87 1.1.6.2 ad /* Map I/O space */
88 1.1.6.2 ad if (bus_space_map(sc->sc.iot, PXA2X0_USBHC_BASE, PXA2X0_USBHC_SIZE, 0,
89 1.1.6.2 ad &sc->sc.ioh)) {
90 1.1.6.2 ad aprint_error(": couldn't map memory space\n");
91 1.1.6.2 ad return;
92 1.1.6.2 ad }
93 1.1.6.2 ad sc->sc.sc_size = PXA2X0_USBHC_SIZE;
94 1.1.6.2 ad
95 1.1.6.2 ad /* XXX copied from ohci_pci.c. needed? */
96 1.1.6.2 ad bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size,
97 1.1.6.2 ad BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
98 1.1.6.2 ad
99 1.1.6.2 ad /* start the usb clock */
100 1.1.6.2 ad pxa2x0_clkman_config(CKEN_USBHC, 1);
101 1.1.6.2 ad pxaohci_enable(sc);
102 1.1.6.2 ad
103 1.1.6.2 ad /* Disable interrupts, so we don't get any spurious ones. */
104 1.1.6.2 ad bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
105 1.1.6.2 ad OHCI_MIE);
106 1.1.6.2 ad
107 1.1.6.2 ad sc->sc_ih = pxa2x0_intr_establish(PXA2X0_INT_USBH1, IPL_USB,
108 1.1.6.2 ad ohci_intr, &sc->sc);
109 1.1.6.2 ad if (sc->sc_ih == NULL) {
110 1.1.6.2 ad aprint_error(": unable to establish interrupt\n");
111 1.1.6.2 ad goto free_map;
112 1.1.6.2 ad }
113 1.1.6.2 ad
114 1.1.6.2 ad strlcpy(sc->sc.sc_vendor, "PXA27x", sizeof(sc->sc.sc_vendor));
115 1.1.6.2 ad r = ohci_init(&sc->sc);
116 1.1.6.2 ad if (r != USBD_NORMAL_COMPLETION) {
117 1.1.6.2 ad aprint_error("%s: init failed, error=%d\n",
118 1.1.6.2 ad sc->sc.sc_bus.bdev.dv_xname, r);
119 1.1.6.2 ad goto free_intr;
120 1.1.6.2 ad }
121 1.1.6.2 ad
122 1.1.6.2 ad sc->sc.sc_powerhook = powerhook_establish(sc->sc.sc_bus.bdev.dv_xname,
123 1.1.6.2 ad pxaohci_power, sc);
124 1.1.6.2 ad if (sc->sc.sc_powerhook == NULL) {
125 1.1.6.2 ad aprint_error("%s: cannot establish powerhook\n",
126 1.1.6.2 ad sc->sc.sc_bus.bdev.dv_xname);
127 1.1.6.2 ad }
128 1.1.6.2 ad
129 1.1.6.2 ad sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus, usbctlprint);
130 1.1.6.2 ad
131 1.1.6.2 ad return;
132 1.1.6.2 ad
133 1.1.6.2 ad free_intr:
134 1.1.6.2 ad pxa2x0_intr_disestablish(sc->sc_ih);
135 1.1.6.2 ad sc->sc_ih = NULL;
136 1.1.6.2 ad free_map:
137 1.1.6.2 ad pxaohci_disable(sc);
138 1.1.6.2 ad pxa2x0_clkman_config(CKEN_USBHC, 0);
139 1.1.6.2 ad bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
140 1.1.6.2 ad sc->sc.sc_size = 0;
141 1.1.6.2 ad }
142 1.1.6.2 ad
143 1.1.6.2 ad static int
144 1.1.6.2 ad pxaohci_detach(struct device *self, int flags)
145 1.1.6.2 ad {
146 1.1.6.2 ad struct pxaohci_softc *sc = (struct pxaohci_softc *)self;
147 1.1.6.2 ad int error;
148 1.1.6.2 ad
149 1.1.6.2 ad error = ohci_detach(&sc->sc, flags);
150 1.1.6.2 ad if (error)
151 1.1.6.2 ad return error;
152 1.1.6.2 ad
153 1.1.6.2 ad if (sc->sc.sc_powerhook) {
154 1.1.6.2 ad powerhook_disestablish(sc->sc.sc_powerhook);
155 1.1.6.2 ad sc->sc.sc_powerhook = NULL;
156 1.1.6.2 ad }
157 1.1.6.2 ad
158 1.1.6.2 ad if (sc->sc_ih) {
159 1.1.6.2 ad pxa2x0_intr_disestablish(sc->sc_ih);
160 1.1.6.2 ad sc->sc_ih = NULL;
161 1.1.6.2 ad }
162 1.1.6.2 ad
163 1.1.6.2 ad pxaohci_disable(sc);
164 1.1.6.2 ad
165 1.1.6.2 ad /* stop clock */
166 1.1.6.2 ad pxa2x0_clkman_config(CKEN_USBHC, 0);
167 1.1.6.2 ad
168 1.1.6.2 ad if (sc->sc.sc_size) {
169 1.1.6.2 ad bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
170 1.1.6.2 ad sc->sc.sc_size = 0;
171 1.1.6.2 ad }
172 1.1.6.2 ad
173 1.1.6.2 ad return 0;
174 1.1.6.2 ad }
175 1.1.6.2 ad
176 1.1.6.2 ad static void
177 1.1.6.2 ad pxaohci_power(int why, void *arg)
178 1.1.6.2 ad {
179 1.1.6.2 ad struct pxaohci_softc *sc = (struct pxaohci_softc *)arg;
180 1.1.6.2 ad int s;
181 1.1.6.2 ad
182 1.1.6.2 ad s = splhardusb();
183 1.1.6.2 ad sc->sc.sc_bus.use_polling++;
184 1.1.6.2 ad switch (why) {
185 1.1.6.2 ad case PWR_STANDBY:
186 1.1.6.2 ad case PWR_SUSPEND:
187 1.1.6.2 ad #if 0
188 1.1.6.2 ad ohci_power(why, &sc->sc);
189 1.1.6.2 ad #endif
190 1.1.6.2 ad pxa2x0_clkman_config(CKEN_USBHC, 0);
191 1.1.6.2 ad break;
192 1.1.6.2 ad
193 1.1.6.2 ad case PWR_RESUME:
194 1.1.6.2 ad pxa2x0_clkman_config(CKEN_USBHC, 1);
195 1.1.6.2 ad pxaohci_enable(sc);
196 1.1.6.2 ad #if 0
197 1.1.6.2 ad ohci_power(why, &sc->sc);
198 1.1.6.2 ad #endif
199 1.1.6.2 ad break;
200 1.1.6.2 ad }
201 1.1.6.2 ad sc->sc.sc_bus.use_polling--;
202 1.1.6.2 ad splx(s);
203 1.1.6.2 ad }
204 1.1.6.2 ad
205 1.1.6.2 ad static void
206 1.1.6.2 ad pxaohci_enable(struct pxaohci_softc *sc)
207 1.1.6.2 ad {
208 1.1.6.2 ad uint32_t hr;
209 1.1.6.2 ad
210 1.1.6.2 ad /* Full host reset */
211 1.1.6.2 ad hr = HREAD4(sc, USBHC_HR);
212 1.1.6.2 ad HWRITE4(sc, USBHC_HR, (hr & USBHC_HR_MASK) | USBHC_HR_FHR);
213 1.1.6.2 ad
214 1.1.6.2 ad DELAY(USBHC_RST_WAIT);
215 1.1.6.2 ad
216 1.1.6.2 ad hr = HREAD4(sc, USBHC_HR);
217 1.1.6.2 ad HWRITE4(sc, USBHC_HR, (hr & USBHC_HR_MASK) & ~(USBHC_HR_FHR));
218 1.1.6.2 ad
219 1.1.6.2 ad /* Force system bus interface reset */
220 1.1.6.2 ad hr = HREAD4(sc, USBHC_HR);
221 1.1.6.2 ad HWRITE4(sc, USBHC_HR, (hr & USBHC_HR_MASK) | USBHC_HR_FSBIR);
222 1.1.6.2 ad
223 1.1.6.2 ad while (HREAD4(sc, USBHC_HR) & USBHC_HR_FSBIR)
224 1.1.6.2 ad DELAY(3);
225 1.1.6.2 ad
226 1.1.6.2 ad /* Enable the ports (physically only one, only enable that one?) */
227 1.1.6.2 ad hr = HREAD4(sc, USBHC_HR);
228 1.1.6.2 ad HWRITE4(sc, USBHC_HR, (hr & USBHC_HR_MASK) & ~(USBHC_HR_SSE));
229 1.1.6.2 ad hr = HREAD4(sc, USBHC_HR);
230 1.1.6.2 ad HWRITE4(sc, USBHC_HR, (hr & USBHC_HR_MASK) & ~(USBHC_HR_SSEP2));
231 1.1.6.2 ad }
232 1.1.6.2 ad
233 1.1.6.2 ad static void
234 1.1.6.2 ad pxaohci_disable(struct pxaohci_softc *sc)
235 1.1.6.2 ad {
236 1.1.6.2 ad uint32_t hr;
237 1.1.6.2 ad
238 1.1.6.2 ad /* Full host reset */
239 1.1.6.2 ad hr = HREAD4(sc, USBHC_HR);
240 1.1.6.2 ad HWRITE4(sc, USBHC_HR, (hr & USBHC_HR_MASK) | USBHC_HR_FHR);
241 1.1.6.2 ad
242 1.1.6.2 ad DELAY(USBHC_RST_WAIT);
243 1.1.6.2 ad
244 1.1.6.2 ad hr = HREAD4(sc, USBHC_HR);
245 1.1.6.2 ad HWRITE4(sc, USBHC_HR, (hr & USBHC_HR_MASK) & ~(USBHC_HR_FHR));
246 1.1.6.2 ad }
247