]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/dev/usb/controller/ehci_mbus.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / dev / usb / controller / ehci_mbus.c
1 /*-
2  * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
3  * All rights reserved.
4  *
5  * Developed by Semihalf.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of MARVELL nor the names of contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 /*
33  * MBus attachment driver for the USB Enhanced Host Controller.
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include "opt_bus.h"
40
41 #include <sys/stdint.h>
42 #include <sys/stddef.h>
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/bus.h>
49 #include <sys/linker_set.h>
50 #include <sys/module.h>
51 #include <sys/lock.h>
52 #include <sys/mutex.h>
53 #include <sys/condvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/sx.h>
56 #include <sys/unistd.h>
57 #include <sys/callout.h>
58 #include <sys/malloc.h>
59 #include <sys/priv.h>
60
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
63
64 #include <dev/usb/usb_core.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_process.h>
67 #include <dev/usb/usb_util.h>
68
69 #include <dev/usb/usb_controller.h>
70 #include <dev/usb/usb_bus.h>
71 #include <dev/usb/controller/ehci.h>
72
73 #include <arm/mv/mvreg.h>
74 #include <arm/mv/mvvar.h>
75
76 #define EHCI_VENDORID_MRVL      0x1286
77 #define EHCI_HC_DEVSTR          "Marvell Integrated USB 2.0 controller"
78
79 static device_attach_t ehci_mbus_attach;
80 static device_detach_t ehci_mbus_detach;
81 static device_shutdown_t ehci_mbus_shutdown;
82 static device_suspend_t ehci_mbus_suspend;
83 static device_resume_t ehci_mbus_resume;
84
85 static int err_intr(void *arg);
86
87 static struct resource *irq_err;
88 static void *ih_err;
89
90 /* EHCI HC regs start at this offset within USB range */
91 #define MV_USB_HOST_OFST        0x0100
92
93 #define USB_BRIDGE_INTR_CAUSE  0x210
94 #define USB_BRIDGE_INTR_MASK   0x214
95
96 #define MV_USB_ADDR_DECODE_ERR (1 << 0)
97 #define MV_USB_HOST_UNDERFLOW  (1 << 1)
98 #define MV_USB_HOST_OVERFLOW   (1 << 2)
99 #define MV_USB_DEVICE_UNDERFLOW (1 << 3)
100
101 static int
102 ehci_mbus_suspend(device_t self)
103 {
104         ehci_softc_t *sc = device_get_softc(self);
105         int err;
106
107         err = bus_generic_suspend(self);
108         if (err)
109                 return (err);
110         ehci_suspend(sc);
111         return (0);
112 }
113
114 static int
115 ehci_mbus_resume(device_t self)
116 {
117         ehci_softc_t *sc = device_get_softc(self);
118
119         ehci_resume(sc);
120
121         bus_generic_resume(self);
122
123         return (0);
124 }
125
126 static int
127 ehci_mbus_shutdown(device_t self)
128 {
129         ehci_softc_t *sc = device_get_softc(self);
130         int err;
131
132         err = bus_generic_shutdown(self);
133         if (err)
134                 return (err);
135         ehci_shutdown(sc);
136
137         return (0);
138 }
139
140 static int
141 ehci_mbus_probe(device_t self)
142 {
143
144         device_set_desc(self, EHCI_HC_DEVSTR);
145
146         return (BUS_PROBE_DEFAULT);
147 }
148
149 static int
150 ehci_mbus_attach(device_t self)
151 {
152         ehci_softc_t *sc = device_get_softc(self);
153         bus_space_handle_t bsh;
154         int err;
155         int rid;
156
157         /* initialise some bus fields */
158         sc->sc_bus.parent = self;
159         sc->sc_bus.devices = sc->sc_devices;
160         sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
161
162         /* get all DMA memory */
163         if (usb_bus_mem_alloc_all(&sc->sc_bus,
164             USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
165                 return (ENOMEM);
166         }
167
168         sc->sc_bus.usbrev = USB_REV_2_0;
169
170         rid = 0;
171         sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
172         if (!sc->sc_io_res) {
173                 device_printf(self, "Could not map memory\n");
174                 goto error;
175         }
176         sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
177         bsh = rman_get_bushandle(sc->sc_io_res);
178         sc->sc_io_size = rman_get_size(sc->sc_io_res) - MV_USB_HOST_OFST;
179
180         /*
181          * Marvell EHCI host controller registers start at certain offset within
182          * the whole USB registers range, so create a subregion for the host
183          * mode configuration purposes.
184          */
185         if (bus_space_subregion(sc->sc_io_tag, bsh, MV_USB_HOST_OFST,
186             sc->sc_io_size, &sc->sc_io_hdl) != 0)
187                 panic("%s: unable to subregion USB host registers",
188                     device_get_name(self));
189
190         rid = 0;
191         irq_err = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
192             RF_SHAREABLE | RF_ACTIVE);
193         if (irq_err == NULL) {
194                 device_printf(self, "Could not allocate error irq\n");
195                 ehci_mbus_detach(self);
196                 return (ENXIO);
197         }
198
199         /*
200          * Notice: Marvell EHCI controller has TWO interrupt lines, so make sure to
201          * use the correct rid for the main one (controller interrupt) --
202          * refer to obio_devices[] for the right resource number to use here.
203          */
204         rid = 1;
205         sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
206             RF_SHAREABLE | RF_ACTIVE);
207         if (sc->sc_irq_res == NULL) {
208                 device_printf(self, "Could not allocate irq\n");
209                 goto error;
210         }
211
212         sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
213         if (!sc->sc_bus.bdev) {
214                 device_printf(self, "Could not add USB device\n");
215                 goto error;
216         }
217         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
218         device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
219
220         sprintf(sc->sc_vendor, "Marvell");
221
222         err = bus_setup_intr(self, irq_err, INTR_FAST | INTR_TYPE_BIO,
223             err_intr, NULL, sc, &ih_err);
224         if (err) {
225                 device_printf(self, "Could not setup error irq, %d\n", err);
226                 ih_err = NULL;
227                 goto error;
228         }
229
230         EWRITE4(sc, USB_BRIDGE_INTR_MASK, MV_USB_ADDR_DECODE_ERR |
231             MV_USB_HOST_UNDERFLOW | MV_USB_HOST_OVERFLOW |
232             MV_USB_DEVICE_UNDERFLOW);
233
234         err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
235             NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
236         if (err) {
237                 device_printf(self, "Could not setup irq, %d\n", err);
238                 sc->sc_intr_hdl = NULL;
239                 goto error;
240         }
241
242         /*
243          * Workaround for Marvell integrated EHCI controller: reset of
244          * the EHCI core clears the USBMODE register, which sets the core in
245          * an undefined state (neither host nor agent), so it needs to be set
246          * again for proper operation.
247          *
248          * Refer to errata document MV-S500832-00D.pdf (p. 5.24 GL USB-2) for
249          * details.
250          */
251         sc->sc_flags |= EHCI_SCFLG_SETMODE;
252         if (bootverbose)
253                 device_printf(self, "5.24 GL USB-2 workaround enabled\n");
254
255         /* XXX all MV chips need it? */
256         sc->sc_flags |= EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_NORESTERM;
257
258         err = ehci_init(sc);
259         if (!err) {
260                 err = device_probe_and_attach(sc->sc_bus.bdev);
261         }
262         if (err) {
263                 device_printf(self, "USB init failed err=%d\n", err);
264                 goto error;
265         }
266         return (0);
267
268 error:
269         ehci_mbus_detach(self);
270         return (ENXIO);
271 }
272
273 static int
274 ehci_mbus_detach(device_t self)
275 {
276         ehci_softc_t *sc = device_get_softc(self);
277         device_t bdev;
278         int err;
279
280         if (sc->sc_bus.bdev) {
281                 bdev = sc->sc_bus.bdev;
282                 device_detach(bdev);
283                 device_delete_child(self, bdev);
284         }
285         /* during module unload there are lots of children leftover */
286         device_delete_all_children(self);
287
288         /*
289          * disable interrupts that might have been switched on in ehci_init
290          */
291         if (sc->sc_io_res) {
292                 EWRITE4(sc, EHCI_USBINTR, 0);
293                 EWRITE4(sc, USB_BRIDGE_INTR_MASK, 0);
294         }
295         if (sc->sc_irq_res && sc->sc_intr_hdl) {
296                 /*
297                  * only call ehci_detach() after ehci_init()
298                  */
299                 ehci_detach(sc);
300
301                 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
302
303                 if (err)
304                         /* XXX or should we panic? */
305                         device_printf(self, "Could not tear down irq, %d\n",
306                             err);
307                 sc->sc_intr_hdl = NULL;
308         }
309         if (irq_err && ih_err) {
310                 err = bus_teardown_intr(self, irq_err, ih_err);
311
312                 if (err)
313                         device_printf(self, "Could not tear down irq, %d\n",
314                             err);
315                 ih_err = NULL;
316         }
317         if (irq_err) {
318                 bus_release_resource(self, SYS_RES_IRQ, 0, irq_err);
319                 irq_err = NULL;
320         }
321         if (sc->sc_irq_res) {
322                 bus_release_resource(self, SYS_RES_IRQ, 1, sc->sc_irq_res);
323                 sc->sc_irq_res = NULL;
324         }
325         if (sc->sc_io_res) {
326                 bus_release_resource(self, SYS_RES_MEMORY, 0,
327                     sc->sc_io_res);
328                 sc->sc_io_res = NULL;
329         }
330         usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
331
332         return (0);
333 }
334
335 static int
336 err_intr(void *arg)
337 {
338         ehci_softc_t *sc = arg;
339         unsigned int cause;
340
341         cause = EREAD4(sc, USB_BRIDGE_INTR_CAUSE);
342         if (cause) {
343                 printf("IRQ ERR: cause: 0x%08x\n", cause);
344                 if (cause & MV_USB_ADDR_DECODE_ERR)
345                         printf("IRQ ERR: Address decoding error\n");
346                 if (cause & MV_USB_HOST_UNDERFLOW)
347                         printf("IRQ ERR: USB Host Underflow\n");
348                 if (cause & MV_USB_HOST_OVERFLOW)
349                         printf("IRQ ERR: USB Host Overflow\n");
350                 if (cause & MV_USB_DEVICE_UNDERFLOW)
351                         printf("IRQ ERR: USB Device Underflow\n");
352                 if (cause & ~(MV_USB_ADDR_DECODE_ERR | MV_USB_HOST_UNDERFLOW |
353                     MV_USB_HOST_OVERFLOW | MV_USB_DEVICE_UNDERFLOW))
354                         printf("IRQ ERR: Unknown error\n");
355
356                 EWRITE4(sc, USB_BRIDGE_INTR_CAUSE, 0);
357         }
358         return (FILTER_HANDLED);
359 }
360
361 static device_method_t ehci_methods[] = {
362         /* Device interface */
363         DEVMETHOD(device_probe, ehci_mbus_probe),
364         DEVMETHOD(device_attach, ehci_mbus_attach),
365         DEVMETHOD(device_detach, ehci_mbus_detach),
366         DEVMETHOD(device_suspend, ehci_mbus_suspend),
367         DEVMETHOD(device_resume, ehci_mbus_resume),
368         DEVMETHOD(device_shutdown, ehci_mbus_shutdown),
369
370         /* Bus interface */
371         DEVMETHOD(bus_print_child, bus_generic_print_child),
372
373         {0, 0}
374 };
375
376 static driver_t ehci_driver = {
377         "ehci",
378         ehci_methods,
379         sizeof(ehci_softc_t),
380 };
381
382 static devclass_t ehci_devclass;
383
384 DRIVER_MODULE(ehci, mbus, ehci_driver, ehci_devclass, 0, 0);
385 MODULE_DEPEND(ehci, usb, 1, 1, 1);