2 * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
3 * Copyright (c) 2010, Aleksandr Rybalko <ray@ddteam.net>
6 * Developed by Semihalf.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of MARVELL nor the names of contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * BHND attachment driver for the USB Enhanced Host Controller.
38 * Ported from ZRouter with insignificant adaptations for FreeBSD11.
43 #include <sys/stdint.h>
44 #include <sys/stddef.h>
45 #include <sys/param.h>
46 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/linker_set.h>
53 #include <sys/module.h>
55 #include <sys/mutex.h>
56 #include <sys/condvar.h>
57 #include <sys/sysctl.h>
59 #include <sys/unistd.h>
60 #include <sys/callout.h>
61 #include <sys/malloc.h>
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usb_core.h>
68 #include <dev/usb/usb_busdma.h>
69 #include <dev/usb/usb_process.h>
70 #include <dev/usb/usb_util.h>
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #include <dev/usb/controller/ehci.h>
75 #include <dev/usb/controller/ehcireg.h>
77 #include <dev/bhnd/bhnd.h>
79 #define EHCI_HC_DEVSTR "Broadcom EHCI"
81 #define USB_BRIDGE_INTR_CAUSE 0x210
82 #define USB_BRIDGE_INTR_MASK 0x214
84 static device_attach_t bhnd_ehci_attach;
85 static device_detach_t bhnd_ehci_detach;
87 static int bhnd_ehci_probe(device_t self);
88 static void bhnd_ehci_post_reset(struct ehci_softc *ehci_softc);
91 bhnd_ehci_probe(device_t self)
94 device_set_desc(self, EHCI_HC_DEVSTR);
96 return (BUS_PROBE_DEFAULT);
100 bhnd_ehci_post_reset(struct ehci_softc *ehci_softc)
104 /* Force HOST mode */
105 usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM);
106 usbmode &= ~EHCI_UM_CM;
107 usbmode |= EHCI_UM_CM_HOST;
108 EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode);
112 bhnd_ehci_attach(device_t self)
118 sc = device_get_softc(self);
119 /* initialise some bus fields */
120 sc->sc_bus.parent = self;
121 sc->sc_bus.devices = sc->sc_devices;
122 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
123 sc->sc_bus.usbrev = USB_REV_2_0;
124 sc->sc_bus.dma_bits = 32;
126 /* get all DMA memory */
127 if ((err = usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
128 &ehci_iterate_hw_softc)) != 0) {
129 BHND_ERROR_DEV(self, "can't allocate DMA memory: %d", err);
134 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
136 if (!sc->sc_io_res) {
137 BHND_ERROR_DEV(self, "Could not map memory");
140 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
141 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
142 sc->sc_io_size = rman_get_size(sc->sc_io_res);
145 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
146 RF_SHAREABLE | RF_ACTIVE);
148 if (sc->sc_irq_res == NULL) {
149 BHND_ERROR_DEV(self, "Could not allocate error irq");
150 bhnd_ehci_detach(self);
154 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
155 if (!sc->sc_bus.bdev) {
156 BHND_ERROR_DEV(self, "Could not add USB device");
159 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
160 device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
162 sprintf(sc->sc_vendor, "Broadcom");
164 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
165 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
167 BHND_ERROR_DEV(self, "Could not setup irq, %d", err);
168 sc->sc_intr_hdl = NULL;
172 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
173 sc->sc_vendor_post_reset = bhnd_ehci_post_reset;
177 err = device_probe_and_attach(sc->sc_bus.bdev);
180 BHND_ERROR_DEV(self, "USB init failed err=%d", err);
186 bhnd_ehci_detach(self);
191 bhnd_ehci_detach(device_t self)
196 sc = device_get_softc(self);
198 /* during module unload there are lots of children leftover */
199 device_delete_children(self);
202 * disable interrupts that might have been switched on in ehci_init
206 EWRITE4(sc, EHCI_USBINTR, 0);
207 EWRITE4(sc, USB_BRIDGE_INTR_MASK, 0);
210 if (sc->sc_irq_res && sc->sc_intr_hdl) {
212 * only call ehci_detach() after ehci_init()
216 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
219 /* XXX or should we panic? */
220 BHND_ERROR_DEV(self, "Could not tear down irq, %d", err);
222 sc->sc_intr_hdl = NULL;
224 if (sc->sc_irq_res) {
225 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
226 sc->sc_irq_res = NULL;
229 bus_release_resource(self, SYS_RES_MEMORY, 0, sc->sc_io_res);
230 sc->sc_io_res = NULL;
232 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
237 static device_method_t ehci_methods[] = {
238 /* Device interface */
239 DEVMETHOD(device_probe, bhnd_ehci_probe),
240 DEVMETHOD(device_attach, bhnd_ehci_attach),
241 DEVMETHOD(device_detach, bhnd_ehci_detach),
242 DEVMETHOD(device_suspend, bus_generic_suspend),
243 DEVMETHOD(device_resume, bus_generic_resume),
244 DEVMETHOD(device_shutdown, bus_generic_shutdown),
247 DEVMETHOD(bus_print_child, bus_generic_print_child),
252 static driver_t ehci_driver = {
255 sizeof(ehci_softc_t),
258 static devclass_t ehci_devclass;
260 DRIVER_MODULE(ehci, bhnd_usb, ehci_driver, ehci_devclass, 0, 0);
261 MODULE_DEPEND(ehci, usb, 1, 1, 1);