1 /*- 2 * Copyright (c) 1998 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Lennart Augustsson (augustss@carlstedt.se) at 7 * Carlstedt Research & Technology. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 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 * 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. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 /* 35 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller. 36 * 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 41 */ 42 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 46 */ 47 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> 55 #include <sys/bus.h> 56 #include <sys/module.h> 57 #include <sys/lock.h> 58 #include <sys/mutex.h> 59 #include <sys/condvar.h> 60 #include <sys/sysctl.h> 61 #include <sys/sx.h> 62 #include <sys/unistd.h> 63 #include <sys/callout.h> 64 #include <sys/malloc.h> 65 #include <sys/priv.h> 66 67 #include <dev/usb/usb.h> 68 #include <dev/usb/usbdi.h> 69 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> 74 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> 80 81 #define PCI_EHCI_VENDORID_ACERLABS 0x10b9 82 #define PCI_EHCI_VENDORID_AMD 0x1022 83 #define PCI_EHCI_VENDORID_APPLE 0x106b 84 #define PCI_EHCI_VENDORID_ATI 0x1002 85 #define PCI_EHCI_VENDORID_CMDTECH 0x1095 86 #define PCI_EHCI_VENDORID_INTEL 0x8086 87 #define PCI_EHCI_VENDORID_NEC 0x1033 88 #define PCI_EHCI_VENDORID_OPTI 0x1045 89 #define PCI_EHCI_VENDORID_PHILIPS 0x1131 90 #define PCI_EHCI_VENDORID_SIS 0x1039 91 #define PCI_EHCI_VENDORID_NVIDIA 0x12D2 92 #define PCI_EHCI_VENDORID_NVIDIA2 0x10DE 93 #define PCI_EHCI_VENDORID_VIA 0x1106 94 95 static void ehci_pci_takecontroller(device_t self); 96 97 static device_probe_t ehci_pci_probe; 98 static device_attach_t ehci_pci_attach; 99 static device_detach_t ehci_pci_detach; 100 static device_suspend_t ehci_pci_suspend; 101 static device_resume_t ehci_pci_resume; 102 static device_shutdown_t ehci_pci_shutdown; 103 104 static int 105 ehci_pci_suspend(device_t self) 106 { 107 ehci_softc_t *sc = device_get_softc(self); 108 int err; 109 110 err = bus_generic_suspend(self); 111 if (err) 112 return (err); 113 ehci_suspend(sc); 114 return (0); 115 } 116 117 static int 118 ehci_pci_resume(device_t self) 119 { 120 ehci_softc_t *sc = device_get_softc(self); 121 122 ehci_pci_takecontroller(self); 123 ehci_resume(sc); 124 125 bus_generic_resume(self); 126 127 return (0); 128 } 129 130 static int 131 ehci_pci_shutdown(device_t self) 132 { 133 ehci_softc_t *sc = device_get_softc(self); 134 int err; 135 136 err = bus_generic_shutdown(self); 137 if (err) 138 return (err); 139 ehci_shutdown(sc); 140 141 return (0); 142 } 143 144 static const char * 145 ehci_pci_match(device_t self) 146 { 147 uint32_t device_id = pci_get_devid(self); 148 149 switch (device_id) { 150 case 0x268c8086: 151 return ("Intel 63XXESB USB 2.0 controller"); 152 153 case 0x523910b9: 154 return "ALi M5239 USB 2.0 controller"; 155 156 case 0x10227463: 157 return "AMD 8111 USB 2.0 controller"; 158 159 case 0x20951022: 160 return ("AMD CS5536 (Geode) USB 2.0 controller"); 161 162 case 0x43451002: 163 return "ATI SB200 USB 2.0 controller"; 164 case 0x43731002: 165 return "ATI SB400 USB 2.0 controller"; 166 167 case 0x25ad8086: 168 return "Intel 6300ESB USB 2.0 controller"; 169 case 0x24cd8086: 170 return "Intel 82801DB/L/M (ICH4) USB 2.0 controller"; 171 case 0x24dd8086: 172 return "Intel 82801EB/R (ICH5) USB 2.0 controller"; 173 case 0x265c8086: 174 return "Intel 82801FB (ICH6) USB 2.0 controller"; 175 case 0x27cc8086: 176 return "Intel 82801GB/R (ICH7) USB 2.0 controller"; 177 178 case 0x28368086: 179 return "Intel 82801H (ICH8) USB 2.0 controller USB2-A"; 180 case 0x283a8086: 181 return "Intel 82801H (ICH8) USB 2.0 controller USB2-B"; 182 case 0x293a8086: 183 return "Intel 82801I (ICH9) USB 2.0 controller"; 184 case 0x293c8086: 185 return "Intel 82801I (ICH9) USB 2.0 controller"; 186 case 0x3a3a8086: 187 return "Intel 82801JI (ICH10) USB 2.0 controller USB-A"; 188 case 0x3a3c8086: 189 return "Intel 82801JI (ICH10) USB 2.0 controller USB-B"; 190 case 0x3b348086: 191 return ("Intel PCH USB 2.0 controller USB-A"); 192 case 0x3b3c8086: 193 return ("Intel PCH USB 2.0 controller USB-B"); 194 195 case 0x00e01033: 196 return ("NEC uPD 720100 USB 2.0 controller"); 197 198 case 0x006810de: 199 return "NVIDIA nForce2 USB 2.0 controller"; 200 case 0x008810de: 201 return "NVIDIA nForce2 Ultra 400 USB 2.0 controller"; 202 case 0x00d810de: 203 return "NVIDIA nForce3 USB 2.0 controller"; 204 case 0x00e810de: 205 return "NVIDIA nForce3 250 USB 2.0 controller"; 206 case 0x005b10de: 207 return "NVIDIA nForce4 USB 2.0 controller"; 208 case 0x036d10de: 209 return "NVIDIA nForce MCP55 USB 2.0 controller"; 210 case 0x03f210de: 211 return "NVIDIA nForce MCP61 USB 2.0 controller"; 212 case 0x0aa610de: 213 return "NVIDIA nForce MCP79 USB 2.0 controller"; 214 case 0x0aa910de: 215 return "NVIDIA nForce MCP79 USB 2.0 controller"; 216 case 0x0aaa10de: 217 return "NVIDIA nForce MCP79 USB 2.0 controller"; 218 219 case 0x15621131: 220 return "Philips ISP156x USB 2.0 controller"; 221 222 case 0x31041106: 223 return ("VIA VT6202 USB 2.0 controller"); 224 225 default: 226 break; 227 } 228 229 if ((pci_get_class(self) == PCIC_SERIALBUS) 230 && (pci_get_subclass(self) == PCIS_SERIALBUS_USB) 231 && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) { 232 return ("EHCI (generic) USB 2.0 controller"); 233 } 234 return (NULL); /* dunno */ 235 } 236 237 static int 238 ehci_pci_probe(device_t self) 239 { 240 const char *desc = ehci_pci_match(self); 241 242 if (desc) { 243 device_set_desc(self, desc); 244 return (0); 245 } else { 246 return (ENXIO); 247 } 248 } 249 250 static void 251 ehci_pci_ati_quirk(device_t self, uint8_t is_sb700) 252 { 253 device_t smbdev; 254 uint32_t val; 255 256 if (is_sb700) { 257 /* Lookup SMBUS PCI device */ 258 smbdev = pci_find_device(PCI_EHCI_VENDORID_ATI, 0x4385); 259 if (smbdev == NULL) 260 return; 261 val = pci_get_revid(smbdev); 262 if (val != 0x3a && val != 0x3b) 263 return; 264 } 265 266 /* 267 * Note: this bit is described as reserved in SB700 268 * Register Reference Guide. 269 */ 270 val = pci_read_config(self, 0x53, 1); 271 if (!(val & 0x8)) { 272 val |= 0x8; 273 pci_write_config(self, 0x53, val, 1); 274 device_printf(self, "AMD SB600/700 quirk applied\n"); 275 } 276 } 277 278 static void 279 ehci_pci_via_quirk(device_t self) 280 { 281 uint32_t val; 282 283 if ((pci_get_device(self) == 0x3104) && 284 ((pci_get_revid(self) & 0xf0) == 0x60)) { 285 /* Correct schedule sleep time to 10us */ 286 val = pci_read_config(self, 0x4b, 1); 287 if (val & 0x20) 288 return; 289 pci_write_config(self, 0x4b, val, 1); 290 device_printf(self, "VIA-quirk applied\n"); 291 } 292 } 293 294 static int 295 ehci_pci_attach(device_t self) 296 { 297 ehci_softc_t *sc = device_get_softc(self); 298 int err; 299 int rid; 300 301 /* initialise some bus fields */ 302 sc->sc_bus.parent = self; 303 sc->sc_bus.devices = sc->sc_devices; 304 sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 305 306 /* get all DMA memory */ 307 if (usb_bus_mem_alloc_all(&sc->sc_bus, 308 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) { 309 return (ENOMEM); 310 } 311 312 pci_enable_busmaster(self); 313 314 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) { 315 case PCI_USB_REV_PRE_1_0: 316 case PCI_USB_REV_1_0: 317 case PCI_USB_REV_1_1: 318 /* 319 * NOTE: some EHCI USB controllers have the wrong USB 320 * revision number. It appears those controllers are 321 * fully compliant so we just ignore this value in 322 * some common cases. 323 */ 324 device_printf(self, "pre-2.0 USB revision (ignored)\n"); 325 /* fallthrough */ 326 case PCI_USB_REV_2_0: 327 break; 328 default: 329 /* Quirk for Parallels Desktop 4.0 */ 330 device_printf(self, "USB revision is unknown. Assuming v2.0.\n"); 331 break; 332 } 333 334 rid = PCI_CBMEM; 335 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 336 RF_ACTIVE); 337 if (!sc->sc_io_res) { 338 device_printf(self, "Could not map memory\n"); 339 goto error; 340 } 341 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 342 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 343 sc->sc_io_size = rman_get_size(sc->sc_io_res); 344 345 rid = 0; 346 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 347 RF_SHAREABLE | RF_ACTIVE); 348 if (sc->sc_irq_res == NULL) { 349 device_printf(self, "Could not allocate irq\n"); 350 goto error; 351 } 352 sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 353 if (!sc->sc_bus.bdev) { 354 device_printf(self, "Could not add USB device\n"); 355 goto error; 356 } 357 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 358 359 /* 360 * ehci_pci_match will never return NULL if ehci_pci_probe 361 * succeeded 362 */ 363 device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self)); 364 switch (pci_get_vendor(self)) { 365 case PCI_EHCI_VENDORID_ACERLABS: 366 sprintf(sc->sc_vendor, "AcerLabs"); 367 break; 368 case PCI_EHCI_VENDORID_AMD: 369 sprintf(sc->sc_vendor, "AMD"); 370 break; 371 case PCI_EHCI_VENDORID_APPLE: 372 sprintf(sc->sc_vendor, "Apple"); 373 break; 374 case PCI_EHCI_VENDORID_ATI: 375 sprintf(sc->sc_vendor, "ATI"); 376 break; 377 case PCI_EHCI_VENDORID_CMDTECH: 378 sprintf(sc->sc_vendor, "CMDTECH"); 379 break; 380 case PCI_EHCI_VENDORID_INTEL: 381 sprintf(sc->sc_vendor, "Intel"); 382 break; 383 case PCI_EHCI_VENDORID_NEC: 384 sprintf(sc->sc_vendor, "NEC"); 385 break; 386 case PCI_EHCI_VENDORID_OPTI: 387 sprintf(sc->sc_vendor, "OPTi"); 388 break; 389 case PCI_EHCI_VENDORID_PHILIPS: 390 sprintf(sc->sc_vendor, "Philips"); 391 break; 392 case PCI_EHCI_VENDORID_SIS: 393 sprintf(sc->sc_vendor, "SiS"); 394 break; 395 case PCI_EHCI_VENDORID_NVIDIA: 396 case PCI_EHCI_VENDORID_NVIDIA2: 397 sprintf(sc->sc_vendor, "nVidia"); 398 break; 399 case PCI_EHCI_VENDORID_VIA: 400 sprintf(sc->sc_vendor, "VIA"); 401 break; 402 default: 403 if (bootverbose) 404 device_printf(self, "(New EHCI DeviceId=0x%08x)\n", 405 pci_get_devid(self)); 406 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self)); 407 } 408 409 #if (__FreeBSD_version >= 700031) 410 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 411 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); 412 #else 413 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 414 (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); 415 #endif 416 if (err) { 417 device_printf(self, "Could not setup irq, %d\n", err); 418 sc->sc_intr_hdl = NULL; 419 goto error; 420 } 421 ehci_pci_takecontroller(self); 422 423 /* Undocumented quirks taken from Linux */ 424 425 switch (pci_get_vendor(self)) { 426 case PCI_EHCI_VENDORID_ATI: 427 /* SB600 and SB700 EHCI quirk */ 428 switch (pci_get_device(self)) { 429 case 0x4386: 430 ehci_pci_ati_quirk(self, 0); 431 break; 432 case 0x4396: 433 ehci_pci_ati_quirk(self, 1); 434 break; 435 default: 436 break; 437 } 438 break; 439 440 case PCI_EHCI_VENDORID_VIA: 441 ehci_pci_via_quirk(self); 442 break; 443 444 default: 445 break; 446 } 447 448 /* Dropped interrupts workaround */ 449 switch (pci_get_vendor(self)) { 450 case PCI_EHCI_VENDORID_ATI: 451 case PCI_EHCI_VENDORID_VIA: 452 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG; 453 if (bootverbose) 454 device_printf(self, 455 "Dropped interrupts workaround enabled\n"); 456 break; 457 default: 458 break; 459 } 460 461 /* Doorbell feature workaround */ 462 switch (pci_get_vendor(self)) { 463 case PCI_EHCI_VENDORID_NVIDIA: 464 case PCI_EHCI_VENDORID_NVIDIA2: 465 sc->sc_flags |= EHCI_SCFLG_IAADBUG; 466 if (bootverbose) 467 device_printf(self, 468 "Doorbell workaround enabled\n"); 469 break; 470 default: 471 break; 472 } 473 474 err = ehci_init(sc); 475 if (!err) { 476 err = device_probe_and_attach(sc->sc_bus.bdev); 477 } 478 if (err) { 479 device_printf(self, "USB init failed err=%d\n", err); 480 goto error; 481 } 482 return (0); 483 484 error: 485 ehci_pci_detach(self); 486 return (ENXIO); 487 } 488 489 static int 490 ehci_pci_detach(device_t self) 491 { 492 ehci_softc_t *sc = device_get_softc(self); 493 device_t bdev; 494 495 if (sc->sc_bus.bdev) { 496 bdev = sc->sc_bus.bdev; 497 device_detach(bdev); 498 device_delete_child(self, bdev); 499 } 500 /* during module unload there are lots of children leftover */ 501 device_delete_all_children(self); 502 503 pci_disable_busmaster(self); 504 505 if (sc->sc_irq_res && sc->sc_intr_hdl) { 506 /* 507 * only call ehci_detach() after ehci_init() 508 */ 509 ehci_detach(sc); 510 511 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 512 513 if (err) 514 /* XXX or should we panic? */ 515 device_printf(self, "Could not tear down irq, %d\n", 516 err); 517 sc->sc_intr_hdl = NULL; 518 } 519 if (sc->sc_irq_res) { 520 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 521 sc->sc_irq_res = NULL; 522 } 523 if (sc->sc_io_res) { 524 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, 525 sc->sc_io_res); 526 sc->sc_io_res = NULL; 527 } 528 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); 529 530 return (0); 531 } 532 533 static void 534 ehci_pci_takecontroller(device_t self) 535 { 536 ehci_softc_t *sc = device_get_softc(self); 537 uint32_t cparams; 538 uint32_t eec; 539 uint16_t to; 540 uint8_t eecp; 541 uint8_t bios_sem; 542 543 cparams = EREAD4(sc, EHCI_HCCPARAMS); 544 545 /* Synchronise with the BIOS if it owns the controller. */ 546 for (eecp = EHCI_HCC_EECP(cparams); eecp != 0; 547 eecp = EHCI_EECP_NEXT(eec)) { 548 eec = pci_read_config(self, eecp, 4); 549 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) { 550 continue; 551 } 552 bios_sem = pci_read_config(self, eecp + 553 EHCI_LEGSUP_BIOS_SEM, 1); 554 if (bios_sem == 0) { 555 continue; 556 } 557 device_printf(sc->sc_bus.bdev, "waiting for BIOS " 558 "to give up control\n"); 559 pci_write_config(self, eecp + 560 EHCI_LEGSUP_OS_SEM, 1, 1); 561 to = 500; 562 while (1) { 563 bios_sem = pci_read_config(self, eecp + 564 EHCI_LEGSUP_BIOS_SEM, 1); 565 if (bios_sem == 0) 566 break; 567 568 if (--to == 0) { 569 device_printf(sc->sc_bus.bdev, 570 "timed out waiting for BIOS\n"); 571 break; 572 } 573 usb_pause_mtx(NULL, hz / 100); /* wait 10ms */ 574 } 575 } 576 } 577 578 static driver_t ehci_driver = 579 { 580 .name = "ehci", 581 .methods = (device_method_t[]){ 582 /* device interface */ 583 DEVMETHOD(device_probe, ehci_pci_probe), 584 DEVMETHOD(device_attach, ehci_pci_attach), 585 DEVMETHOD(device_detach, ehci_pci_detach), 586 DEVMETHOD(device_suspend, ehci_pci_suspend), 587 DEVMETHOD(device_resume, ehci_pci_resume), 588 DEVMETHOD(device_shutdown, ehci_pci_shutdown), 589 /* bus interface */ 590 DEVMETHOD(bus_print_child, bus_generic_print_child), 591 592 {0, 0} 593 }, 594 .size = sizeof(struct ehci_softc), 595 }; 596 597 static devclass_t ehci_devclass; 598 599 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0); 600 MODULE_DEPEND(ehci, usb, 1, 1, 1); 601