]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/controller/ehci_mbus.c
MFC r264294:
[FreeBSD/stable/8.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/module.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/condvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/sx.h>
55 #include <sys/unistd.h>
56 #include <sys/callout.h>
57 #include <sys/malloc.h>
58 #include <sys/priv.h>
59
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62
63 #include <dev/usb/usb_core.h>
64 #include <dev/usb/usb_busdma.h>
65 #include <dev/usb/usb_process.h>
66 #include <dev/usb/usb_util.h>
67
68 #include <dev/usb/usb_controller.h>
69 #include <dev/usb/usb_bus.h>
70 #include <dev/usb/controller/ehci.h>
71 #include <dev/usb/controller/ehcireg.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
82 static int err_intr(void *arg);
83
84 static struct resource *irq_err;
85 static void *ih_err;
86
87 /* EHCI HC regs start at this offset within USB range */
88 #define MV_USB_HOST_OFST        0x0100
89
90 #define USB_BRIDGE_INTR_CAUSE  0x210
91 #define USB_BRIDGE_INTR_MASK   0x214
92
93 #define MV_USB_ADDR_DECODE_ERR (1 << 0)
94 #define MV_USB_HOST_UNDERFLOW  (1 << 1)
95 #define MV_USB_HOST_OVERFLOW   (1 << 2)
96 #define MV_USB_DEVICE_UNDERFLOW (1 << 3)
97
98 static int
99 ehci_mbus_probe(device_t self)
100 {
101
102         device_set_desc(self, EHCI_HC_DEVSTR);
103
104         return (BUS_PROBE_DEFAULT);
105 }
106
107 static int
108 ehci_mbus_attach(device_t self)
109 {
110         ehci_softc_t *sc = device_get_softc(self);
111         bus_space_handle_t bsh;
112         int err;
113         int rid;
114
115         /* initialise some bus fields */
116         sc->sc_bus.parent = self;
117         sc->sc_bus.devices = sc->sc_devices;
118         sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
119
120         /* get all DMA memory */
121         if (usb_bus_mem_alloc_all(&sc->sc_bus,
122             USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
123                 return (ENOMEM);
124         }
125
126         rid = 0;
127         sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
128         if (!sc->sc_io_res) {
129                 device_printf(self, "Could not map memory\n");
130                 goto error;
131         }
132         sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
133         bsh = rman_get_bushandle(sc->sc_io_res);
134         sc->sc_io_size = rman_get_size(sc->sc_io_res) - MV_USB_HOST_OFST;
135
136         /*
137          * Marvell EHCI host controller registers start at certain offset within
138          * the whole USB registers range, so create a subregion for the host
139          * mode configuration purposes.
140          */
141         if (bus_space_subregion(sc->sc_io_tag, bsh, MV_USB_HOST_OFST,
142             sc->sc_io_size, &sc->sc_io_hdl) != 0)
143                 panic("%s: unable to subregion USB host registers",
144                     device_get_name(self));
145
146         rid = 0;
147         irq_err = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
148             RF_SHAREABLE | RF_ACTIVE);
149         if (irq_err == NULL) {
150                 device_printf(self, "Could not allocate error irq\n");
151                 ehci_mbus_detach(self);
152                 return (ENXIO);
153         }
154
155         /*
156          * Notice: Marvell EHCI controller has TWO interrupt lines, so make sure to
157          * use the correct rid for the main one (controller interrupt) --
158          * refer to obio_devices[] for the right resource number to use here.
159          */
160         rid = 1;
161         sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
162             RF_SHAREABLE | RF_ACTIVE);
163         if (sc->sc_irq_res == NULL) {
164                 device_printf(self, "Could not allocate irq\n");
165                 goto error;
166         }
167
168         sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
169         if (!sc->sc_bus.bdev) {
170                 device_printf(self, "Could not add USB device\n");
171                 goto error;
172         }
173         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
174         device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
175
176         sprintf(sc->sc_vendor, "Marvell");
177
178         err = bus_setup_intr(self, irq_err, INTR_FAST | INTR_TYPE_BIO,
179             err_intr, NULL, sc, &ih_err);
180         if (err) {
181                 device_printf(self, "Could not setup error irq, %d\n", err);
182                 ih_err = NULL;
183                 goto error;
184         }
185
186         EWRITE4(sc, USB_BRIDGE_INTR_MASK, MV_USB_ADDR_DECODE_ERR |
187             MV_USB_HOST_UNDERFLOW | MV_USB_HOST_OVERFLOW |
188             MV_USB_DEVICE_UNDERFLOW);
189
190         err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
191             NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
192         if (err) {
193                 device_printf(self, "Could not setup irq, %d\n", err);
194                 sc->sc_intr_hdl = NULL;
195                 goto error;
196         }
197
198         /*
199          * Workaround for Marvell integrated EHCI controller: reset of
200          * the EHCI core clears the USBMODE register, which sets the core in
201          * an undefined state (neither host nor agent), so it needs to be set
202          * again for proper operation.
203          *
204          * Refer to errata document MV-S500832-00D.pdf (p. 5.24 GL USB-2) for
205          * details.
206          */
207         sc->sc_flags |= EHCI_SCFLG_SETMODE;
208         if (bootverbose)
209                 device_printf(self, "5.24 GL USB-2 workaround enabled\n");
210
211         /* XXX all MV chips need it? */
212         sc->sc_flags |= EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_NORESTERM;
213
214         err = ehci_init(sc);
215         if (!err) {
216                 err = device_probe_and_attach(sc->sc_bus.bdev);
217         }
218         if (err) {
219                 device_printf(self, "USB init failed err=%d\n", err);
220                 goto error;
221         }
222         return (0);
223
224 error:
225         ehci_mbus_detach(self);
226         return (ENXIO);
227 }
228
229 static int
230 ehci_mbus_detach(device_t self)
231 {
232         ehci_softc_t *sc = device_get_softc(self);
233         device_t bdev;
234         int err;
235
236         if (sc->sc_bus.bdev) {
237                 bdev = sc->sc_bus.bdev;
238                 device_detach(bdev);
239                 device_delete_child(self, bdev);
240         }
241         /* during module unload there are lots of children leftover */
242         device_delete_all_children(self);
243
244         /*
245          * disable interrupts that might have been switched on in ehci_init
246          */
247         if (sc->sc_io_res) {
248                 EWRITE4(sc, EHCI_USBINTR, 0);
249                 EWRITE4(sc, USB_BRIDGE_INTR_MASK, 0);
250         }
251         if (sc->sc_irq_res && sc->sc_intr_hdl) {
252                 /*
253                  * only call ehci_detach() after ehci_init()
254                  */
255                 ehci_detach(sc);
256
257                 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
258
259                 if (err)
260                         /* XXX or should we panic? */
261                         device_printf(self, "Could not tear down irq, %d\n",
262                             err);
263                 sc->sc_intr_hdl = NULL;
264         }
265         if (irq_err && ih_err) {
266                 err = bus_teardown_intr(self, irq_err, ih_err);
267
268                 if (err)
269                         device_printf(self, "Could not tear down irq, %d\n",
270                             err);
271                 ih_err = NULL;
272         }
273         if (irq_err) {
274                 bus_release_resource(self, SYS_RES_IRQ, 0, irq_err);
275                 irq_err = NULL;
276         }
277         if (sc->sc_irq_res) {
278                 bus_release_resource(self, SYS_RES_IRQ, 1, sc->sc_irq_res);
279                 sc->sc_irq_res = NULL;
280         }
281         if (sc->sc_io_res) {
282                 bus_release_resource(self, SYS_RES_MEMORY, 0,
283                     sc->sc_io_res);
284                 sc->sc_io_res = NULL;
285         }
286         usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
287
288         return (0);
289 }
290
291 static int
292 err_intr(void *arg)
293 {
294         ehci_softc_t *sc = arg;
295         unsigned int cause;
296
297         cause = EREAD4(sc, USB_BRIDGE_INTR_CAUSE);
298         if (cause) {
299                 printf("IRQ ERR: cause: 0x%08x\n", cause);
300                 if (cause & MV_USB_ADDR_DECODE_ERR)
301                         printf("IRQ ERR: Address decoding error\n");
302                 if (cause & MV_USB_HOST_UNDERFLOW)
303                         printf("IRQ ERR: USB Host Underflow\n");
304                 if (cause & MV_USB_HOST_OVERFLOW)
305                         printf("IRQ ERR: USB Host Overflow\n");
306                 if (cause & MV_USB_DEVICE_UNDERFLOW)
307                         printf("IRQ ERR: USB Device Underflow\n");
308                 if (cause & ~(MV_USB_ADDR_DECODE_ERR | MV_USB_HOST_UNDERFLOW |
309                     MV_USB_HOST_OVERFLOW | MV_USB_DEVICE_UNDERFLOW))
310                         printf("IRQ ERR: Unknown error\n");
311
312                 EWRITE4(sc, USB_BRIDGE_INTR_CAUSE, 0);
313         }
314         return (FILTER_HANDLED);
315 }
316
317 static device_method_t ehci_methods[] = {
318         /* Device interface */
319         DEVMETHOD(device_probe, ehci_mbus_probe),
320         DEVMETHOD(device_attach, ehci_mbus_attach),
321         DEVMETHOD(device_detach, ehci_mbus_detach),
322         DEVMETHOD(device_suspend, bus_generic_suspend),
323         DEVMETHOD(device_resume, bus_generic_resume),
324         DEVMETHOD(device_shutdown, bus_generic_shutdown),
325
326         DEVMETHOD_END
327 };
328
329 static driver_t ehci_driver = {
330         .name = "ehci",
331         .methods = ehci_methods,
332         .size = sizeof(ehci_softc_t),
333 };
334
335 static devclass_t ehci_devclass;
336
337 DRIVER_MODULE(ehci, mbus, ehci_driver, ehci_devclass, 0, 0);
338 MODULE_DEPEND(ehci, usb, 1, 1, 1);