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