2 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Lennart Augustsson (augustss@carlstedt.se) at
7 * Carlstedt Research & Technology.
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.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
44 * The EHCI 1.0 spec can be found at
45 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
46 * and the USB 2.0 spec at
47 * http://www.usb.org/developers/docs/usb_20.zip
50 /* The low level controller code for EHCI has been split into
51 * PCI probes and EHCI specific code. This was done to facilitate the
52 * sharing of code between *BSD's
55 #include <sys/stdint.h>
56 #include <sys/stddef.h>
57 #include <sys/param.h>
58 #include <sys/queue.h>
59 #include <sys/types.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
63 #include <sys/linker_set.h>
64 #include <sys/module.h>
66 #include <sys/mutex.h>
67 #include <sys/condvar.h>
68 #include <sys/sysctl.h>
70 #include <sys/unistd.h>
71 #include <sys/callout.h>
72 #include <sys/malloc.h>
75 #include <dev/usb/usb.h>
76 #include <dev/usb/usbdi.h>
78 #include <dev/usb/usb_core.h>
79 #include <dev/usb/usb_busdma.h>
80 #include <dev/usb/usb_process.h>
81 #include <dev/usb/usb_util.h>
83 #include <dev/usb/usb_controller.h>
84 #include <dev/usb/usb_bus.h>
85 #include <dev/usb/usb_pci.h>
86 #include <dev/usb/controller/ehci.h>
88 #define PCI_EHCI_VENDORID_ACERLABS 0x10b9
89 #define PCI_EHCI_VENDORID_AMD 0x1022
90 #define PCI_EHCI_VENDORID_APPLE 0x106b
91 #define PCI_EHCI_VENDORID_ATI 0x1002
92 #define PCI_EHCI_VENDORID_CMDTECH 0x1095
93 #define PCI_EHCI_VENDORID_INTEL 0x8086
94 #define PCI_EHCI_VENDORID_NEC 0x1033
95 #define PCI_EHCI_VENDORID_OPTI 0x1045
96 #define PCI_EHCI_VENDORID_PHILIPS 0x1131
97 #define PCI_EHCI_VENDORID_SIS 0x1039
98 #define PCI_EHCI_VENDORID_NVIDIA 0x12D2
99 #define PCI_EHCI_VENDORID_NVIDIA2 0x10DE
100 #define PCI_EHCI_VENDORID_VIA 0x1106
102 #define PCI_EHCI_BASE_REG 0x10
104 static void ehci_pci_takecontroller(device_t self);
106 static device_probe_t ehci_pci_probe;
107 static device_attach_t ehci_pci_attach;
108 static device_detach_t ehci_pci_detach;
109 static device_suspend_t ehci_pci_suspend;
110 static device_resume_t ehci_pci_resume;
111 static device_shutdown_t ehci_pci_shutdown;
114 ehci_pci_suspend(device_t self)
116 ehci_softc_t *sc = device_get_softc(self);
119 err = bus_generic_suspend(self);
127 ehci_pci_resume(device_t self)
129 ehci_softc_t *sc = device_get_softc(self);
131 ehci_pci_takecontroller(self);
134 bus_generic_resume(self);
140 ehci_pci_shutdown(device_t self)
142 ehci_softc_t *sc = device_get_softc(self);
145 err = bus_generic_shutdown(self);
154 ehci_pci_match(device_t self)
156 uint32_t device_id = pci_get_devid(self);
160 return ("Intel 63XXESB USB 2.0 controller");
163 return "ALi M5239 USB 2.0 controller";
166 return "AMD 8111 USB 2.0 controller";
169 return ("AMD CS5536 (Geode) USB 2.0 controller");
172 return "ATI SB200 USB 2.0 controller";
174 return "ATI SB400 USB 2.0 controller";
177 return "Intel 6300ESB USB 2.0 controller";
179 return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
181 return "Intel 82801EB/R (ICH5) USB 2.0 controller";
183 return "Intel 82801FB (ICH6) USB 2.0 controller";
185 return "Intel 82801GB/R (ICH7) USB 2.0 controller";
188 return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
190 return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
192 return "Intel 82801I (ICH9) USB 2.0 controller";
194 return "Intel 82801I (ICH9) USB 2.0 controller";
197 return ("NEC uPD 720100 USB 2.0 controller");
200 return "NVIDIA nForce2 USB 2.0 controller";
202 return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
204 return "NVIDIA nForce3 USB 2.0 controller";
206 return "NVIDIA nForce3 250 USB 2.0 controller";
208 return "NVIDIA nForce4 USB 2.0 controller";
210 return "NVIDIA nForce MCP61 USB 2.0 controller";
213 return "Philips ISP156x USB 2.0 controller";
216 return ("VIA VT6202 USB 2.0 controller");
222 if ((pci_get_class(self) == PCIC_SERIALBUS)
223 && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
224 && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
225 return ("EHCI (generic) USB 2.0 controller");
227 return (NULL); /* dunno */
231 ehci_pci_probe(device_t self)
233 const char *desc = ehci_pci_match(self);
236 device_set_desc(self, desc);
244 ehci_pci_attach(device_t self)
246 ehci_softc_t *sc = device_get_softc(self);
250 /* initialise some bus fields */
251 sc->sc_bus.parent = self;
252 sc->sc_bus.devices = sc->sc_devices;
253 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
255 /* get all DMA memory */
256 if (usb_bus_mem_alloc_all(&sc->sc_bus,
257 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
261 pci_enable_busmaster(self);
263 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
264 case PCI_USB_REV_PRE_1_0:
265 case PCI_USB_REV_1_0:
266 case PCI_USB_REV_1_1:
268 * NOTE: some EHCI USB controllers have the wrong USB
269 * revision number. It appears those controllers are
270 * fully compliant so we just ignore this value in
273 device_printf(self, "pre-2.0 USB revision (ignored)\n");
275 case PCI_USB_REV_2_0:
276 sc->sc_bus.usbrev = USB_REV_2_0;
279 /* Quirk for Parallels Desktop 4.0 */
280 device_printf(self, "USB revision is unknown. Assuming v2.0.\n");
281 sc->sc_bus.usbrev = USB_REV_2_0;
286 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
288 if (!sc->sc_io_res) {
289 device_printf(self, "Could not map memory\n");
292 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
293 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
294 sc->sc_io_size = rman_get_size(sc->sc_io_res);
297 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
298 RF_SHAREABLE | RF_ACTIVE);
299 if (sc->sc_irq_res == NULL) {
300 device_printf(self, "Could not allocate irq\n");
303 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
304 if (!sc->sc_bus.bdev) {
305 device_printf(self, "Could not add USB device\n");
308 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
311 * ehci_pci_match will never return NULL if ehci_pci_probe
314 device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
315 switch (pci_get_vendor(self)) {
316 case PCI_EHCI_VENDORID_ACERLABS:
317 sprintf(sc->sc_vendor, "AcerLabs");
319 case PCI_EHCI_VENDORID_AMD:
320 sprintf(sc->sc_vendor, "AMD");
322 case PCI_EHCI_VENDORID_APPLE:
323 sprintf(sc->sc_vendor, "Apple");
325 case PCI_EHCI_VENDORID_ATI:
326 sprintf(sc->sc_vendor, "ATI");
328 case PCI_EHCI_VENDORID_CMDTECH:
329 sprintf(sc->sc_vendor, "CMDTECH");
331 case PCI_EHCI_VENDORID_INTEL:
332 sprintf(sc->sc_vendor, "Intel");
334 case PCI_EHCI_VENDORID_NEC:
335 sprintf(sc->sc_vendor, "NEC");
337 case PCI_EHCI_VENDORID_OPTI:
338 sprintf(sc->sc_vendor, "OPTi");
340 case PCI_EHCI_VENDORID_PHILIPS:
341 sprintf(sc->sc_vendor, "Philips");
343 case PCI_EHCI_VENDORID_SIS:
344 sprintf(sc->sc_vendor, "SiS");
346 case PCI_EHCI_VENDORID_NVIDIA:
347 case PCI_EHCI_VENDORID_NVIDIA2:
348 sprintf(sc->sc_vendor, "nVidia");
350 case PCI_EHCI_VENDORID_VIA:
351 sprintf(sc->sc_vendor, "VIA");
355 device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
356 pci_get_devid(self));
357 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
360 #if (__FreeBSD_version >= 700031)
361 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
362 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
364 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
365 (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
368 device_printf(self, "Could not setup irq, %d\n", err);
369 sc->sc_intr_hdl = NULL;
372 ehci_pci_takecontroller(self);
375 err = device_probe_and_attach(sc->sc_bus.bdev);
378 device_printf(self, "USB init failed err=%d\n", err);
384 ehci_pci_detach(self);
389 ehci_pci_detach(device_t self)
391 ehci_softc_t *sc = device_get_softc(self);
394 if (sc->sc_bus.bdev) {
395 bdev = sc->sc_bus.bdev;
397 device_delete_child(self, bdev);
399 /* during module unload there are lots of children leftover */
400 device_delete_all_children(self);
402 pci_disable_busmaster(self);
405 * disable interrupts that might have been switched on in ehci_init
408 EWRITE4(sc, EHCI_USBINTR, 0);
410 if (sc->sc_irq_res && sc->sc_intr_hdl) {
412 * only call ehci_detach() after ehci_init()
416 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
419 /* XXX or should we panic? */
420 device_printf(self, "Could not tear down irq, %d\n",
422 sc->sc_intr_hdl = NULL;
424 if (sc->sc_irq_res) {
425 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
426 sc->sc_irq_res = NULL;
429 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
431 sc->sc_io_res = NULL;
433 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
439 ehci_pci_takecontroller(device_t self)
441 ehci_softc_t *sc = device_get_softc(self);
448 cparams = EREAD4(sc, EHCI_HCCPARAMS);
450 /* Synchronise with the BIOS if it owns the controller. */
451 for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
452 eecp = EHCI_EECP_NEXT(eec)) {
453 eec = pci_read_config(self, eecp, 4);
454 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
457 bios_sem = pci_read_config(self, eecp +
458 EHCI_LEGSUP_BIOS_SEM, 1);
462 device_printf(sc->sc_bus.bdev, "waiting for BIOS "
463 "to give up control\n");
464 pci_write_config(self, eecp +
465 EHCI_LEGSUP_OS_SEM, 1, 1);
468 bios_sem = pci_read_config(self, eecp +
469 EHCI_LEGSUP_BIOS_SEM, 1);
474 device_printf(sc->sc_bus.bdev,
475 "timed out waiting for BIOS\n");
478 usb_pause_mtx(NULL, hz / 100); /* wait 10ms */
483 static driver_t ehci_driver =
486 .methods = (device_method_t[]){
487 /* device interface */
488 DEVMETHOD(device_probe, ehci_pci_probe),
489 DEVMETHOD(device_attach, ehci_pci_attach),
490 DEVMETHOD(device_detach, ehci_pci_detach),
491 DEVMETHOD(device_suspend, ehci_pci_suspend),
492 DEVMETHOD(device_resume, ehci_pci_resume),
493 DEVMETHOD(device_shutdown, ehci_pci_shutdown),
495 DEVMETHOD(bus_print_child, bus_generic_print_child),
499 .size = sizeof(struct ehci_softc),
502 static devclass_t ehci_devclass;
504 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
505 MODULE_DEPEND(ehci, usb, 1, 1, 1);