2 * Copyright (c) 2012 Ganbold Tsagaankhuu <ganbold@freebsd.org>
3 * Copyright (c) 2016 The FreeBSD Foundation
6 * This software was developed by Andrew Turner under
7 * sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Generic EHCI driver based on the Allwinner A10 EHCI driver
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 #include <sys/condvar.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <machine/bus.h>
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
53 #include <dev/usb/usb_core.h>
54 #include <dev/usb/usb_busdma.h>
55 #include <dev/usb/usb_process.h>
56 #include <dev/usb/usb_util.h>
58 #include <dev/usb/usb_controller.h>
59 #include <dev/usb/usb_bus.h>
60 #include <dev/usb/controller/ehci.h>
61 #include <dev/usb/controller/ehcireg.h>
63 #include <contrib/dev/acpica/include/acpi.h>
64 #include <contrib/dev/acpica/include/accommon.h>
65 #include <dev/acpica/acpivar.h>
67 static device_attach_t generic_ehci_attach;
68 static device_detach_t generic_ehci_detach;
71 generic_ehci_probe(device_t self)
75 if ((h = acpi_get_handle(self)) == NULL ||
76 !acpi_MatchHid(h, "PNP0D20"))
79 device_set_desc(self, "Generic EHCI Controller");
80 return (BUS_PROBE_DEFAULT);
84 generic_ehci_attach(device_t self)
86 ehci_softc_t *sc = device_get_softc(self);
90 /* initialise some bus fields */
91 sc->sc_bus.parent = self;
92 sc->sc_bus.devices = sc->sc_devices;
93 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
94 sc->sc_bus.dma_bits = 32;
96 /* get all DMA memory */
97 if (usb_bus_mem_alloc_all(&sc->sc_bus,
98 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
102 sc->sc_bus.usbrev = USB_REV_2_0;
105 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
107 if (!sc->sc_io_res) {
108 device_printf(self, "Could not map memory\n");
112 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
113 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
114 sc->sc_io_size = rman_get_size(sc->sc_io_res);
117 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
118 RF_SHAREABLE | RF_ACTIVE);
119 if (sc->sc_irq_res == NULL) {
120 device_printf(self, "Could not allocate irq\n");
123 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
124 if (!sc->sc_bus.bdev) {
125 device_printf(self, "Could not add USB device\n");
128 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
130 strlcpy(sc->sc_vendor, "Generic", sizeof(sc->sc_vendor));
132 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
133 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
135 device_printf(self, "Could not setup irq, %d\n", err);
136 sc->sc_intr_hdl = NULL;
140 sc->sc_flags |= EHCI_SCFLG_DONTRESET;
144 err = device_probe_and_attach(sc->sc_bus.bdev);
151 generic_ehci_detach(self);
156 generic_ehci_detach(device_t self)
158 ehci_softc_t *sc = device_get_softc(self);
161 /* during module unload there are lots of children leftover */
162 device_delete_children(self);
164 if (sc->sc_irq_res && sc->sc_intr_hdl) {
166 * only call ehci_detach() after ehci_init()
170 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
173 /* XXX or should we panic? */
174 device_printf(self, "Could not tear down irq, %d\n",
176 sc->sc_intr_hdl = NULL;
179 if (sc->sc_irq_res) {
180 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
181 sc->sc_irq_res = NULL;
184 bus_release_resource(self, SYS_RES_MEMORY, 0,
186 sc->sc_io_res = NULL;
188 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
193 static device_method_t ehci_methods[] = {
194 /* Device interface */
195 DEVMETHOD(device_probe, generic_ehci_probe),
196 DEVMETHOD(device_attach, generic_ehci_attach),
197 DEVMETHOD(device_detach, generic_ehci_detach),
198 DEVMETHOD(device_suspend, bus_generic_suspend),
199 DEVMETHOD(device_resume, bus_generic_resume),
200 DEVMETHOD(device_shutdown, bus_generic_shutdown),
205 static driver_t ehci_driver = {
207 .methods = ehci_methods,
208 .size = sizeof(ehci_softc_t),
211 static devclass_t ehci_devclass;
213 DRIVER_MODULE(ehci, acpi, ehci_driver, ehci_devclass, 0, 0);
214 MODULE_DEPEND(ehci, usb, 1, 1, 1);