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.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
37 * The EHCI 1.0 spec can be found at
38 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
39 * and the USB 2.0 spec at
40 * http://www.usb.org/developers/docs/usb_20.zip
43 /* The low level controller code for EHCI has been split into
44 * PCI probes and EHCI specific code. This was done to facilitate the
45 * sharing of code between *BSD's
48 #include <sys/stdint.h>
49 #include <sys/stddef.h>
50 #include <sys/param.h>
51 #include <sys/queue.h>
52 #include <sys/types.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
56 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/sysctl.h>
62 #include <sys/unistd.h>
63 #include <sys/callout.h>
64 #include <sys/malloc.h>
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usb_core.h>
71 #include <dev/usb/usb_busdma.h>
72 #include <dev/usb/usb_process.h>
73 #include <dev/usb/usb_util.h>
75 #include <dev/usb/usb_controller.h>
76 #include <dev/usb/usb_bus.h>
77 #include <dev/usb/usb_pci.h>
78 #include <dev/usb/controller/ehci.h>
79 #include <dev/usb/controller/ehcireg.h>
82 #define PCI_EHCI_VENDORID_ACERLABS 0x10b9
83 #define PCI_EHCI_VENDORID_AMD 0x1022
84 #define PCI_EHCI_VENDORID_APPLE 0x106b
85 #define PCI_EHCI_VENDORID_ATI 0x1002
86 #define PCI_EHCI_VENDORID_CMDTECH 0x1095
87 #define PCI_EHCI_VENDORID_INTEL 0x8086
88 #define PCI_EHCI_VENDORID_NEC 0x1033
89 #define PCI_EHCI_VENDORID_OPTI 0x1045
90 #define PCI_EHCI_VENDORID_PHILIPS 0x1131
91 #define PCI_EHCI_VENDORID_SIS 0x1039
92 #define PCI_EHCI_VENDORID_NVIDIA 0x12D2
93 #define PCI_EHCI_VENDORID_NVIDIA2 0x10DE
94 #define PCI_EHCI_VENDORID_VIA 0x1106
96 static device_probe_t ehci_pci_probe;
97 static device_attach_t ehci_pci_attach;
98 static device_detach_t ehci_pci_detach;
99 static usb_take_controller_t ehci_pci_take_controller;
102 ehci_pci_match(device_t self)
104 uint32_t device_id = pci_get_devid(self);
108 return ("Intel 63XXESB USB 2.0 controller");
111 return "ALi M5239 USB 2.0 controller";
114 return "AMD 8111 USB 2.0 controller";
117 return ("AMD CS5536 (Geode) USB 2.0 controller");
120 return "ATI SB200 USB 2.0 controller";
122 return "ATI SB400 USB 2.0 controller";
125 return "Intel 6300ESB USB 2.0 controller";
127 return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
129 return "Intel 82801EB/R (ICH5) USB 2.0 controller";
131 return "Intel 82801FB (ICH6) USB 2.0 controller";
133 return "Intel 82801GB/R (ICH7) USB 2.0 controller";
136 return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
138 return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
140 return "Intel 82801I (ICH9) USB 2.0 controller";
142 return "Intel 82801I (ICH9) USB 2.0 controller";
144 return "Intel 82801JI (ICH10) USB 2.0 controller USB-A";
146 return "Intel 82801JI (ICH10) USB 2.0 controller USB-B";
148 return ("Intel PCH USB 2.0 controller USB-A");
150 return ("Intel PCH USB 2.0 controller USB-B");
153 return ("NEC uPD 720100 USB 2.0 controller");
156 return "NVIDIA nForce2 USB 2.0 controller";
158 return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
160 return "NVIDIA nForce3 USB 2.0 controller";
162 return "NVIDIA nForce3 250 USB 2.0 controller";
164 return "NVIDIA nForce4 USB 2.0 controller";
166 return "NVIDIA nForce MCP55 USB 2.0 controller";
168 return "NVIDIA nForce MCP61 USB 2.0 controller";
170 return "NVIDIA nForce MCP79 USB 2.0 controller";
172 return "NVIDIA nForce MCP79 USB 2.0 controller";
174 return "NVIDIA nForce MCP79 USB 2.0 controller";
177 return "Philips ISP156x USB 2.0 controller";
180 return ("VIA VT6202 USB 2.0 controller");
186 if ((pci_get_class(self) == PCIC_SERIALBUS)
187 && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
188 && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
189 return ("EHCI (generic) USB 2.0 controller");
191 return (NULL); /* dunno */
195 ehci_pci_probe(device_t self)
197 const char *desc = ehci_pci_match(self);
200 device_set_desc(self, desc);
208 ehci_pci_ati_quirk(device_t self, uint8_t is_sb700)
214 /* Lookup SMBUS PCI device */
215 smbdev = pci_find_device(PCI_EHCI_VENDORID_ATI, 0x4385);
218 val = pci_get_revid(smbdev);
219 if (val != 0x3a && val != 0x3b)
224 * Note: this bit is described as reserved in SB700
225 * Register Reference Guide.
227 val = pci_read_config(self, 0x53, 1);
230 pci_write_config(self, 0x53, val, 1);
231 device_printf(self, "AMD SB600/700 quirk applied\n");
236 ehci_pci_via_quirk(device_t self)
240 if ((pci_get_device(self) == 0x3104) &&
241 ((pci_get_revid(self) & 0xf0) == 0x60)) {
242 /* Correct schedule sleep time to 10us */
243 val = pci_read_config(self, 0x4b, 1);
246 pci_write_config(self, 0x4b, val, 1);
247 device_printf(self, "VIA-quirk applied\n");
252 ehci_pci_attach(device_t self)
254 ehci_softc_t *sc = device_get_softc(self);
258 /* initialise some bus fields */
259 sc->sc_bus.parent = self;
260 sc->sc_bus.devices = sc->sc_devices;
261 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
263 /* get all DMA memory */
264 if (usb_bus_mem_alloc_all(&sc->sc_bus,
265 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
269 pci_enable_busmaster(self);
271 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
272 case PCI_USB_REV_PRE_1_0:
273 case PCI_USB_REV_1_0:
274 case PCI_USB_REV_1_1:
276 * NOTE: some EHCI USB controllers have the wrong USB
277 * revision number. It appears those controllers are
278 * fully compliant so we just ignore this value in
281 device_printf(self, "pre-2.0 USB revision (ignored)\n");
283 case PCI_USB_REV_2_0:
286 /* Quirk for Parallels Desktop 4.0 */
287 device_printf(self, "USB revision is unknown. Assuming v2.0.\n");
292 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
294 if (!sc->sc_io_res) {
295 device_printf(self, "Could not map memory\n");
298 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
299 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
300 sc->sc_io_size = rman_get_size(sc->sc_io_res);
303 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
304 RF_SHAREABLE | RF_ACTIVE);
305 if (sc->sc_irq_res == NULL) {
306 device_printf(self, "Could not allocate irq\n");
309 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
310 if (!sc->sc_bus.bdev) {
311 device_printf(self, "Could not add USB device\n");
314 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
317 * ehci_pci_match will never return NULL if ehci_pci_probe
320 device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
321 switch (pci_get_vendor(self)) {
322 case PCI_EHCI_VENDORID_ACERLABS:
323 sprintf(sc->sc_vendor, "AcerLabs");
325 case PCI_EHCI_VENDORID_AMD:
326 sprintf(sc->sc_vendor, "AMD");
328 case PCI_EHCI_VENDORID_APPLE:
329 sprintf(sc->sc_vendor, "Apple");
331 case PCI_EHCI_VENDORID_ATI:
332 sprintf(sc->sc_vendor, "ATI");
334 case PCI_EHCI_VENDORID_CMDTECH:
335 sprintf(sc->sc_vendor, "CMDTECH");
337 case PCI_EHCI_VENDORID_INTEL:
338 sprintf(sc->sc_vendor, "Intel");
340 case PCI_EHCI_VENDORID_NEC:
341 sprintf(sc->sc_vendor, "NEC");
343 case PCI_EHCI_VENDORID_OPTI:
344 sprintf(sc->sc_vendor, "OPTi");
346 case PCI_EHCI_VENDORID_PHILIPS:
347 sprintf(sc->sc_vendor, "Philips");
349 case PCI_EHCI_VENDORID_SIS:
350 sprintf(sc->sc_vendor, "SiS");
352 case PCI_EHCI_VENDORID_NVIDIA:
353 case PCI_EHCI_VENDORID_NVIDIA2:
354 sprintf(sc->sc_vendor, "nVidia");
356 case PCI_EHCI_VENDORID_VIA:
357 sprintf(sc->sc_vendor, "VIA");
361 device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
362 pci_get_devid(self));
363 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
366 #if (__FreeBSD_version >= 700031)
367 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
368 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
370 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
371 (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
374 device_printf(self, "Could not setup irq, %d\n", err);
375 sc->sc_intr_hdl = NULL;
378 ehci_pci_take_controller(self);
380 /* Undocumented quirks taken from Linux */
382 switch (pci_get_vendor(self)) {
383 case PCI_EHCI_VENDORID_ATI:
384 /* SB600 and SB700 EHCI quirk */
385 switch (pci_get_device(self)) {
387 ehci_pci_ati_quirk(self, 0);
390 ehci_pci_ati_quirk(self, 1);
397 case PCI_EHCI_VENDORID_VIA:
398 ehci_pci_via_quirk(self);
405 /* Dropped interrupts workaround */
406 switch (pci_get_vendor(self)) {
407 case PCI_EHCI_VENDORID_ATI:
408 case PCI_EHCI_VENDORID_VIA:
409 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
412 "Dropped interrupts workaround enabled\n");
418 /* Doorbell feature workaround */
419 switch (pci_get_vendor(self)) {
420 case PCI_EHCI_VENDORID_NVIDIA:
421 case PCI_EHCI_VENDORID_NVIDIA2:
422 sc->sc_flags |= EHCI_SCFLG_IAADBUG;
425 "Doorbell workaround enabled\n");
433 err = device_probe_and_attach(sc->sc_bus.bdev);
436 device_printf(self, "USB init failed err=%d\n", err);
442 ehci_pci_detach(self);
447 ehci_pci_detach(device_t self)
449 ehci_softc_t *sc = device_get_softc(self);
452 if (sc->sc_bus.bdev) {
453 bdev = sc->sc_bus.bdev;
455 device_delete_child(self, bdev);
457 /* during module unload there are lots of children leftover */
458 device_delete_children(self);
460 pci_disable_busmaster(self);
462 if (sc->sc_irq_res && sc->sc_intr_hdl) {
464 * only call ehci_detach() after ehci_init()
468 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
471 /* XXX or should we panic? */
472 device_printf(self, "Could not tear down irq, %d\n",
474 sc->sc_intr_hdl = NULL;
476 if (sc->sc_irq_res) {
477 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
478 sc->sc_irq_res = NULL;
481 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
483 sc->sc_io_res = NULL;
485 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
491 ehci_pci_take_controller(device_t self)
493 ehci_softc_t *sc = device_get_softc(self);
500 cparams = EREAD4(sc, EHCI_HCCPARAMS);
502 /* Synchronise with the BIOS if it owns the controller. */
503 for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
504 eecp = EHCI_EECP_NEXT(eec)) {
505 eec = pci_read_config(self, eecp, 4);
506 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
509 bios_sem = pci_read_config(self, eecp +
510 EHCI_LEGSUP_BIOS_SEM, 1);
514 device_printf(sc->sc_bus.bdev, "waiting for BIOS "
515 "to give up control\n");
516 pci_write_config(self, eecp +
517 EHCI_LEGSUP_OS_SEM, 1, 1);
520 bios_sem = pci_read_config(self, eecp +
521 EHCI_LEGSUP_BIOS_SEM, 1);
526 device_printf(sc->sc_bus.bdev,
527 "timed out waiting for BIOS\n");
530 usb_pause_mtx(NULL, hz / 100); /* wait 10ms */
536 static device_method_t ehci_pci_methods[] = {
537 /* Device interface */
538 DEVMETHOD(device_probe, ehci_pci_probe),
539 DEVMETHOD(device_attach, ehci_pci_attach),
540 DEVMETHOD(device_detach, ehci_pci_detach),
541 DEVMETHOD(device_suspend, bus_generic_suspend),
542 DEVMETHOD(device_resume, bus_generic_resume),
543 DEVMETHOD(device_shutdown, bus_generic_shutdown),
544 DEVMETHOD(usb_take_controller, ehci_pci_take_controller),
549 static driver_t ehci_driver = {
551 .methods = ehci_pci_methods,
552 .size = sizeof(struct ehci_softc),
555 static devclass_t ehci_devclass;
557 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
558 MODULE_DEPEND(ehci, usb, 1, 1, 1);