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>
87 #include <dev/usb/controller/ehcireg.h>
89 #define PCI_EHCI_VENDORID_ACERLABS 0x10b9
90 #define PCI_EHCI_VENDORID_AMD 0x1022
91 #define PCI_EHCI_VENDORID_APPLE 0x106b
92 #define PCI_EHCI_VENDORID_ATI 0x1002
93 #define PCI_EHCI_VENDORID_CMDTECH 0x1095
94 #define PCI_EHCI_VENDORID_INTEL 0x8086
95 #define PCI_EHCI_VENDORID_NEC 0x1033
96 #define PCI_EHCI_VENDORID_OPTI 0x1045
97 #define PCI_EHCI_VENDORID_PHILIPS 0x1131
98 #define PCI_EHCI_VENDORID_SIS 0x1039
99 #define PCI_EHCI_VENDORID_NVIDIA 0x12D2
100 #define PCI_EHCI_VENDORID_NVIDIA2 0x10DE
101 #define PCI_EHCI_VENDORID_VIA 0x1106
103 #define PCI_EHCI_BASE_REG 0x10
105 static void ehci_pci_takecontroller(device_t self);
107 static device_probe_t ehci_pci_probe;
108 static device_attach_t ehci_pci_attach;
109 static device_detach_t ehci_pci_detach;
110 static device_suspend_t ehci_pci_suspend;
111 static device_resume_t ehci_pci_resume;
112 static device_shutdown_t ehci_pci_shutdown;
115 ehci_pci_suspend(device_t self)
117 ehci_softc_t *sc = device_get_softc(self);
120 err = bus_generic_suspend(self);
128 ehci_pci_resume(device_t self)
130 ehci_softc_t *sc = device_get_softc(self);
132 ehci_pci_takecontroller(self);
135 bus_generic_resume(self);
141 ehci_pci_shutdown(device_t self)
143 ehci_softc_t *sc = device_get_softc(self);
146 err = bus_generic_shutdown(self);
155 ehci_pci_match(device_t self)
157 uint32_t device_id = pci_get_devid(self);
161 return ("Intel 63XXESB USB 2.0 controller");
164 return "ALi M5239 USB 2.0 controller";
167 return "AMD 8111 USB 2.0 controller";
170 return ("AMD CS5536 (Geode) USB 2.0 controller");
173 return "ATI SB200 USB 2.0 controller";
175 return "ATI SB400 USB 2.0 controller";
178 return "Intel 6300ESB USB 2.0 controller";
180 return "Intel 82801DB/L/M (ICH4) USB 2.0 controller";
182 return "Intel 82801EB/R (ICH5) USB 2.0 controller";
184 return "Intel 82801FB (ICH6) USB 2.0 controller";
186 return "Intel 82801GB/R (ICH7) USB 2.0 controller";
189 return "Intel 82801H (ICH8) USB 2.0 controller USB2-A";
191 return "Intel 82801H (ICH8) USB 2.0 controller USB2-B";
193 return "Intel 82801I (ICH9) USB 2.0 controller";
195 return "Intel 82801I (ICH9) USB 2.0 controller";
197 return "Intel 82801JI (ICH10) USB 2.0 controller USB-A";
199 return "Intel 82801JI (ICH10) USB 2.0 controller USB-B";
201 return ("Intel PCH USB 2.0 controller USB-A");
203 return ("Intel PCH USB 2.0 controller USB-B");
206 return ("NEC uPD 720100 USB 2.0 controller");
209 return "NVIDIA nForce2 USB 2.0 controller";
211 return "NVIDIA nForce2 Ultra 400 USB 2.0 controller";
213 return "NVIDIA nForce3 USB 2.0 controller";
215 return "NVIDIA nForce3 250 USB 2.0 controller";
217 return "NVIDIA nForce4 USB 2.0 controller";
219 return "NVIDIA nForce MCP55 USB 2.0 controller";
221 return "NVIDIA nForce MCP61 USB 2.0 controller";
223 return "NVIDIA nForce MCP79 USB 2.0 controller";
225 return "NVIDIA nForce MCP79 USB 2.0 controller";
227 return "NVIDIA nForce MCP79 USB 2.0 controller";
230 return "Philips ISP156x USB 2.0 controller";
233 return ("VIA VT6202 USB 2.0 controller");
239 if ((pci_get_class(self) == PCIC_SERIALBUS)
240 && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
241 && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) {
242 return ("EHCI (generic) USB 2.0 controller");
244 return (NULL); /* dunno */
248 ehci_pci_probe(device_t self)
250 const char *desc = ehci_pci_match(self);
253 device_set_desc(self, desc);
261 ehci_pci_ati_quirk(device_t self, uint8_t is_sb700)
267 /* Lookup SMBUS PCI device */
268 smbdev = pci_find_device(PCI_EHCI_VENDORID_ATI, 0x4385);
271 val = pci_get_revid(smbdev);
272 if (val != 0x3a && val != 0x3b)
277 * Note: this bit is described as reserved in SB700
278 * Register Reference Guide.
280 val = pci_read_config(self, 0x53, 1);
283 pci_write_config(self, 0x53, val, 1);
284 device_printf(self, "AMD SB600/700 quirk applied\n");
289 ehci_pci_via_quirk(device_t self)
293 if ((pci_get_device(self) == 0x3104) &&
294 ((pci_get_revid(self) & 0xf0) == 0x60)) {
295 /* Correct schedule sleep time to 10us */
296 val = pci_read_config(self, 0x4b, 1);
299 pci_write_config(self, 0x4b, val, 1);
300 device_printf(self, "VIA-quirk applied\n");
305 ehci_pci_attach(device_t self)
307 ehci_softc_t *sc = device_get_softc(self);
311 /* initialise some bus fields */
312 sc->sc_bus.parent = self;
313 sc->sc_bus.devices = sc->sc_devices;
314 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
316 /* get all DMA memory */
317 if (usb_bus_mem_alloc_all(&sc->sc_bus,
318 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
322 pci_enable_busmaster(self);
324 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) {
325 case PCI_USB_REV_PRE_1_0:
326 case PCI_USB_REV_1_0:
327 case PCI_USB_REV_1_1:
329 * NOTE: some EHCI USB controllers have the wrong USB
330 * revision number. It appears those controllers are
331 * fully compliant so we just ignore this value in
334 device_printf(self, "pre-2.0 USB revision (ignored)\n");
336 case PCI_USB_REV_2_0:
339 /* Quirk for Parallels Desktop 4.0 */
340 device_printf(self, "USB revision is unknown. Assuming v2.0.\n");
345 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
347 if (!sc->sc_io_res) {
348 device_printf(self, "Could not map memory\n");
351 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
352 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
353 sc->sc_io_size = rman_get_size(sc->sc_io_res);
356 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
357 RF_SHAREABLE | RF_ACTIVE);
358 if (sc->sc_irq_res == NULL) {
359 device_printf(self, "Could not allocate irq\n");
362 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
363 if (!sc->sc_bus.bdev) {
364 device_printf(self, "Could not add USB device\n");
367 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
370 * ehci_pci_match will never return NULL if ehci_pci_probe
373 device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self));
374 switch (pci_get_vendor(self)) {
375 case PCI_EHCI_VENDORID_ACERLABS:
376 sprintf(sc->sc_vendor, "AcerLabs");
378 case PCI_EHCI_VENDORID_AMD:
379 sprintf(sc->sc_vendor, "AMD");
381 case PCI_EHCI_VENDORID_APPLE:
382 sprintf(sc->sc_vendor, "Apple");
384 case PCI_EHCI_VENDORID_ATI:
385 sprintf(sc->sc_vendor, "ATI");
387 case PCI_EHCI_VENDORID_CMDTECH:
388 sprintf(sc->sc_vendor, "CMDTECH");
390 case PCI_EHCI_VENDORID_INTEL:
391 sprintf(sc->sc_vendor, "Intel");
393 case PCI_EHCI_VENDORID_NEC:
394 sprintf(sc->sc_vendor, "NEC");
396 case PCI_EHCI_VENDORID_OPTI:
397 sprintf(sc->sc_vendor, "OPTi");
399 case PCI_EHCI_VENDORID_PHILIPS:
400 sprintf(sc->sc_vendor, "Philips");
402 case PCI_EHCI_VENDORID_SIS:
403 sprintf(sc->sc_vendor, "SiS");
405 case PCI_EHCI_VENDORID_NVIDIA:
406 case PCI_EHCI_VENDORID_NVIDIA2:
407 sprintf(sc->sc_vendor, "nVidia");
409 case PCI_EHCI_VENDORID_VIA:
410 sprintf(sc->sc_vendor, "VIA");
414 device_printf(self, "(New EHCI DeviceId=0x%08x)\n",
415 pci_get_devid(self));
416 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
419 #if (__FreeBSD_version >= 700031)
420 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
421 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
423 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
424 (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
427 device_printf(self, "Could not setup irq, %d\n", err);
428 sc->sc_intr_hdl = NULL;
431 ehci_pci_takecontroller(self);
433 /* Undocumented quirks taken from Linux */
435 switch (pci_get_vendor(self)) {
436 case PCI_EHCI_VENDORID_ATI:
437 /* SB600 and SB700 EHCI quirk */
438 switch (pci_get_device(self)) {
440 ehci_pci_ati_quirk(self, 0);
443 ehci_pci_ati_quirk(self, 1);
450 case PCI_EHCI_VENDORID_VIA:
451 ehci_pci_via_quirk(self);
458 /* Dropped interrupts workaround */
459 switch (pci_get_vendor(self)) {
460 case PCI_EHCI_VENDORID_ATI:
461 case PCI_EHCI_VENDORID_VIA:
462 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG;
465 "Dropped interrupts workaround enabled\n");
471 /* Doorbell feature workaround */
472 switch (pci_get_vendor(self)) {
473 case PCI_EHCI_VENDORID_NVIDIA:
474 case PCI_EHCI_VENDORID_NVIDIA2:
475 sc->sc_flags |= EHCI_SCFLG_IAADBUG;
478 "Doorbell workaround enabled\n");
486 err = device_probe_and_attach(sc->sc_bus.bdev);
489 device_printf(self, "USB init failed err=%d\n", err);
495 ehci_pci_detach(self);
500 ehci_pci_detach(device_t self)
502 ehci_softc_t *sc = device_get_softc(self);
505 if (sc->sc_bus.bdev) {
506 bdev = sc->sc_bus.bdev;
508 device_delete_child(self, bdev);
510 /* during module unload there are lots of children leftover */
511 device_delete_all_children(self);
513 pci_disable_busmaster(self);
516 * disable interrupts that might have been switched on in ehci_init
519 EWRITE4(sc, EHCI_USBINTR, 0);
521 if (sc->sc_irq_res && sc->sc_intr_hdl) {
523 * only call ehci_detach() after ehci_init()
527 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
530 /* XXX or should we panic? */
531 device_printf(self, "Could not tear down irq, %d\n",
533 sc->sc_intr_hdl = NULL;
535 if (sc->sc_irq_res) {
536 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
537 sc->sc_irq_res = NULL;
540 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
542 sc->sc_io_res = NULL;
544 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
550 ehci_pci_takecontroller(device_t self)
552 ehci_softc_t *sc = device_get_softc(self);
559 cparams = EREAD4(sc, EHCI_HCCPARAMS);
561 /* Synchronise with the BIOS if it owns the controller. */
562 for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
563 eecp = EHCI_EECP_NEXT(eec)) {
564 eec = pci_read_config(self, eecp, 4);
565 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
568 bios_sem = pci_read_config(self, eecp +
569 EHCI_LEGSUP_BIOS_SEM, 1);
573 device_printf(sc->sc_bus.bdev, "waiting for BIOS "
574 "to give up control\n");
575 pci_write_config(self, eecp +
576 EHCI_LEGSUP_OS_SEM, 1, 1);
579 bios_sem = pci_read_config(self, eecp +
580 EHCI_LEGSUP_BIOS_SEM, 1);
585 device_printf(sc->sc_bus.bdev,
586 "timed out waiting for BIOS\n");
589 usb_pause_mtx(NULL, hz / 100); /* wait 10ms */
594 static driver_t ehci_driver =
597 .methods = (device_method_t[]){
598 /* device interface */
599 DEVMETHOD(device_probe, ehci_pci_probe),
600 DEVMETHOD(device_attach, ehci_pci_attach),
601 DEVMETHOD(device_detach, ehci_pci_detach),
602 DEVMETHOD(device_suspend, ehci_pci_suspend),
603 DEVMETHOD(device_resume, ehci_pci_resume),
604 DEVMETHOD(device_shutdown, ehci_pci_shutdown),
606 DEVMETHOD(bus_print_child, bus_generic_print_child),
610 .size = sizeof(struct ehci_softc),
613 static devclass_t ehci_devclass;
615 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0);
616 MODULE_DEPEND(ehci, usb, 1, 1, 1);