1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-NetBSD 3 * 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (augustss@carlstedt.se) at 9 * Carlstedt Research & Technology. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 /* 37 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller. 38 * 39 * The EHCI 1.0 spec can be found at 40 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf 41 * and the USB 2.0 spec at 42 * http://www.usb.org/developers/docs/usb_20.zip 43 */ 44 45 /* The low level controller code for EHCI has been split into 46 * PCI probes and EHCI specific code. This was done to facilitate the 47 * sharing of code between *BSD's 48 */ 49 50 #include <sys/stdint.h> 51 #include <sys/stddef.h> 52 #include <sys/param.h> 53 #include <sys/queue.h> 54 #include <sys/types.h> 55 #include <sys/systm.h> 56 #include <sys/kernel.h> 57 #include <sys/bus.h> 58 #include <sys/module.h> 59 #include <sys/lock.h> 60 #include <sys/mutex.h> 61 #include <sys/condvar.h> 62 #include <sys/sysctl.h> 63 #include <sys/sx.h> 64 #include <sys/unistd.h> 65 #include <sys/callout.h> 66 #include <sys/malloc.h> 67 #include <sys/priv.h> 68 69 #include <dev/usb/usb.h> 70 #include <dev/usb/usbdi.h> 71 72 #include <dev/usb/usb_core.h> 73 #include <dev/usb/usb_busdma.h> 74 #include <dev/usb/usb_process.h> 75 #include <dev/usb/usb_util.h> 76 77 #include <dev/usb/usb_controller.h> 78 #include <dev/usb/usb_bus.h> 79 #include <dev/usb/usb_pci.h> 80 #include <dev/usb/controller/ehci.h> 81 #include <dev/usb/controller/ehcireg.h> 82 #include "usb_if.h" 83 84 #define PCI_EHCI_VENDORID_ACERLABS 0x10b9 85 #define PCI_EHCI_VENDORID_AMD 0x1022 86 #define PCI_EHCI_VENDORID_APPLE 0x106b 87 #define PCI_EHCI_VENDORID_ATI 0x1002 88 #define PCI_EHCI_VENDORID_CMDTECH 0x1095 89 #define PCI_EHCI_VENDORID_HYGON 0x1d94 90 #define PCI_EHCI_VENDORID_INTEL 0x8086 91 #define PCI_EHCI_VENDORID_NEC 0x1033 92 #define PCI_EHCI_VENDORID_OPTI 0x1045 93 #define PCI_EHCI_VENDORID_PHILIPS 0x1131 94 #define PCI_EHCI_VENDORID_SIS 0x1039 95 #define PCI_EHCI_VENDORID_NVIDIA 0x12D2 96 #define PCI_EHCI_VENDORID_NVIDIA2 0x10DE 97 #define PCI_EHCI_VENDORID_VIA 0x1106 98 #define PCI_EHCI_VENDORID_VMWARE 0x15ad 99 100 static device_probe_t ehci_pci_probe; 101 static device_attach_t ehci_pci_attach; 102 static device_detach_t ehci_pci_detach; 103 static usb_take_controller_t ehci_pci_take_controller; 104 105 static const char * 106 ehci_pci_match(device_t self) 107 { 108 uint32_t device_id = pci_get_devid(self); 109 110 switch (device_id) { 111 case 0x523910b9: 112 return "ALi M5239 USB 2.0 controller"; 113 114 case 0x10227463: 115 return "AMD 8111 USB 2.0 controller"; 116 117 case 0x20951022: 118 return ("AMD CS5536 (Geode) USB 2.0 controller"); 119 case 0x78081022: 120 return ("AMD FCH USB 2.0 controller"); 121 case 0x79081022: 122 return ("AMD FCH USB 2.0 controller"); 123 124 case 0x43451002: 125 return "ATI SB200 USB 2.0 controller"; 126 case 0x43731002: 127 return "ATI SB400 USB 2.0 controller"; 128 case 0x43961002: 129 return ("AMD SB7x0/SB8x0/SB9x0 USB 2.0 controller"); 130 131 case 0x0f348086: 132 return ("Intel BayTrail USB 2.0 controller"); 133 case 0x1c268086: 134 return ("Intel Cougar Point USB 2.0 controller"); 135 case 0x1c2d8086: 136 return ("Intel Cougar Point USB 2.0 controller"); 137 case 0x1d268086: 138 return ("Intel Patsburg USB 2.0 controller"); 139 case 0x1d2d8086: 140 return ("Intel Patsburg USB 2.0 controller"); 141 case 0x1e268086: 142 return ("Intel Panther Point USB 2.0 controller"); 143 case 0x1e2d8086: 144 return ("Intel Panther Point USB 2.0 controller"); 145 case 0x1f2c8086: 146 return ("Intel Avoton USB 2.0 controller"); 147 case 0x25ad8086: 148 return "Intel 6300ESB USB 2.0 controller"; 149 case 0x24cd8086: 150 return "Intel 82801DB/L/M (ICH4) USB 2.0 controller"; 151 case 0x24dd8086: 152 return "Intel 82801EB/R (ICH5) USB 2.0 controller"; 153 case 0x265c8086: 154 return "Intel 82801FB (ICH6) USB 2.0 controller"; 155 case 0x268c8086: 156 return ("Intel 63XXESB USB 2.0 controller"); 157 case 0x27cc8086: 158 return "Intel 82801GB/R (ICH7) USB 2.0 controller"; 159 case 0x28368086: 160 return "Intel 82801H (ICH8) USB 2.0 controller USB2-A"; 161 case 0x283a8086: 162 return "Intel 82801H (ICH8) USB 2.0 controller USB2-B"; 163 case 0x293a8086: 164 return "Intel 82801I (ICH9) USB 2.0 controller"; 165 case 0x293c8086: 166 return "Intel 82801I (ICH9) USB 2.0 controller"; 167 case 0x3a3a8086: 168 return "Intel 82801JI (ICH10) USB 2.0 controller USB-A"; 169 case 0x3a3c8086: 170 return "Intel 82801JI (ICH10) USB 2.0 controller USB-B"; 171 case 0x3b348086: 172 return ("Intel PCH USB 2.0 controller USB-A"); 173 case 0x3b3c8086: 174 return ("Intel PCH USB 2.0 controller USB-B"); 175 case 0x8c268086: 176 return ("Intel Lynx Point USB 2.0 controller USB-A"); 177 case 0x8c2d8086: 178 return ("Intel Lynx Point USB 2.0 controller USB-B"); 179 case 0x8ca68086: 180 return ("Intel Wildcat Point USB 2.0 controller USB-A"); 181 case 0x8cad8086: 182 return ("Intel Wildcat Point USB 2.0 controller USB-B"); 183 case 0x8d268086: 184 return ("Intel Wellsburg USB 2.0 controller"); 185 case 0x8d2d8086: 186 return ("Intel Wellsburg USB 2.0 controller"); 187 case 0x9c268086: 188 return ("Intel Lynx Point-LP USB 2.0 controller"); 189 case 0x9ca68086: 190 return ("Intel Wildcat Point-LP USB 2.0 controller"); 191 192 case 0x00e01033: 193 return ("NEC uPD 72010x USB 2.0 controller"); 194 195 case 0x006810de: 196 return "NVIDIA nForce2 USB 2.0 controller"; 197 case 0x008810de: 198 return "NVIDIA nForce2 Ultra 400 USB 2.0 controller"; 199 case 0x00d810de: 200 return "NVIDIA nForce3 USB 2.0 controller"; 201 case 0x00e810de: 202 return "NVIDIA nForce3 250 USB 2.0 controller"; 203 case 0x005b10de: 204 return "NVIDIA nForce CK804 USB 2.0 controller"; 205 case 0x036d10de: 206 return "NVIDIA nForce MCP55 USB 2.0 controller"; 207 case 0x03f210de: 208 return "NVIDIA nForce MCP61 USB 2.0 controller"; 209 case 0x0aa610de: 210 return "NVIDIA nForce MCP79 USB 2.0 controller"; 211 case 0x0aa910de: 212 return "NVIDIA nForce MCP79 USB 2.0 controller"; 213 case 0x0aaa10de: 214 return "NVIDIA nForce MCP79 USB 2.0 controller"; 215 216 case 0x15621131: 217 return "Philips ISP156x USB 2.0 controller"; 218 219 case 0x70021039: 220 return "SiS 968 USB 2.0 controller"; 221 222 case 0x31041106: 223 return ("VIA VT6202 USB 2.0 controller"); 224 225 case 0x077015ad: 226 return ("VMware USB 2.0 controller"); 227 default: 228 break; 229 } 230 231 if ((pci_get_class(self) == PCIC_SERIALBUS) 232 && (pci_get_subclass(self) == PCIS_SERIALBUS_USB) 233 && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) { 234 return ("EHCI (generic) USB 2.0 controller"); 235 } 236 return (NULL); /* dunno */ 237 } 238 239 static int 240 ehci_pci_probe(device_t self) 241 { 242 const char *desc = ehci_pci_match(self); 243 244 if (desc) { 245 device_set_desc(self, desc); 246 return (BUS_PROBE_DEFAULT); 247 } else { 248 return (ENXIO); 249 } 250 } 251 252 static void 253 ehci_pci_ati_quirk(device_t self, uint8_t is_sb700) 254 { 255 device_t smbdev; 256 uint32_t val; 257 258 if (is_sb700) { 259 /* Lookup SMBUS PCI device */ 260 smbdev = pci_find_device(PCI_EHCI_VENDORID_ATI, 0x4385); 261 if (smbdev == NULL) 262 return; 263 val = pci_get_revid(smbdev); 264 if (val != 0x3a && val != 0x3b) 265 return; 266 } 267 268 /* 269 * Note: this bit is described as reserved in SB700 270 * Register Reference Guide. 271 */ 272 val = pci_read_config(self, 0x53, 1); 273 if (!(val & 0x8)) { 274 val |= 0x8; 275 pci_write_config(self, 0x53, val, 1); 276 device_printf(self, "AMD SB600/700 quirk applied\n"); 277 } 278 } 279 280 static void 281 ehci_pci_via_quirk(device_t self) 282 { 283 uint32_t val; 284 285 if ((pci_get_device(self) == 0x3104) && 286 ((pci_get_revid(self) & 0xf0) == 0x60)) { 287 /* Correct schedule sleep time to 10us */ 288 val = pci_read_config(self, 0x4b, 1); 289 if (val & 0x20) 290 return; 291 val |= 0x20; 292 pci_write_config(self, 0x4b, val, 1); 293 device_printf(self, "VIA-quirk applied\n"); 294 } 295 } 296 297 static int 298 ehci_pci_attach(device_t self) 299 { 300 ehci_softc_t *sc = device_get_softc(self); 301 int err; 302 int rid; 303 304 /* initialise some bus fields */ 305 sc->sc_bus.parent = self; 306 sc->sc_bus.devices = sc->sc_devices; 307 sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 308 sc->sc_bus.dma_bits = 32; 309 310 /* get all DMA memory */ 311 if (usb_bus_mem_alloc_all(&sc->sc_bus, 312 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) { 313 return (ENOMEM); 314 } 315 316 pci_enable_busmaster(self); 317 318 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) { 319 case PCI_USB_REV_PRE_1_0: 320 case PCI_USB_REV_1_0: 321 case PCI_USB_REV_1_1: 322 /* 323 * NOTE: some EHCI USB controllers have the wrong USB 324 * revision number. It appears those controllers are 325 * fully compliant so we just ignore this value in 326 * some common cases. 327 */ 328 device_printf(self, "pre-2.0 USB revision (ignored)\n"); 329 /* fallthrough */ 330 case PCI_USB_REV_2_0: 331 break; 332 default: 333 /* Quirk for Parallels Desktop 4.0 */ 334 device_printf(self, "USB revision is unknown. Assuming v2.0.\n"); 335 break; 336 } 337 338 rid = PCI_CBMEM; 339 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 340 RF_ACTIVE); 341 if (!sc->sc_io_res) { 342 device_printf(self, "Could not map memory\n"); 343 goto error; 344 } 345 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 346 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 347 sc->sc_io_size = rman_get_size(sc->sc_io_res); 348 349 rid = 0; 350 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 351 RF_SHAREABLE | RF_ACTIVE); 352 if (sc->sc_irq_res == NULL) { 353 device_printf(self, "Could not allocate irq\n"); 354 goto error; 355 } 356 sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 357 if (!sc->sc_bus.bdev) { 358 device_printf(self, "Could not add USB device\n"); 359 goto error; 360 } 361 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 362 363 /* 364 * ehci_pci_match will never return NULL if ehci_pci_probe 365 * succeeded 366 */ 367 device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self)); 368 switch (pci_get_vendor(self)) { 369 case PCI_EHCI_VENDORID_ACERLABS: 370 sprintf(sc->sc_vendor, "AcerLabs"); 371 break; 372 case PCI_EHCI_VENDORID_AMD: 373 sprintf(sc->sc_vendor, "AMD"); 374 break; 375 case PCI_EHCI_VENDORID_APPLE: 376 sprintf(sc->sc_vendor, "Apple"); 377 break; 378 case PCI_EHCI_VENDORID_ATI: 379 sprintf(sc->sc_vendor, "ATI"); 380 break; 381 case PCI_EHCI_VENDORID_CMDTECH: 382 sprintf(sc->sc_vendor, "CMDTECH"); 383 break; 384 case PCI_EHCI_VENDORID_HYGON: 385 sprintf(sc->sc_vendor, "Hygon"); 386 break; 387 case PCI_EHCI_VENDORID_INTEL: 388 sprintf(sc->sc_vendor, "Intel"); 389 break; 390 case PCI_EHCI_VENDORID_NEC: 391 sprintf(sc->sc_vendor, "NEC"); 392 break; 393 case PCI_EHCI_VENDORID_OPTI: 394 sprintf(sc->sc_vendor, "OPTi"); 395 break; 396 case PCI_EHCI_VENDORID_PHILIPS: 397 sprintf(sc->sc_vendor, "Philips"); 398 break; 399 case PCI_EHCI_VENDORID_SIS: 400 sprintf(sc->sc_vendor, "SiS"); 401 break; 402 case PCI_EHCI_VENDORID_NVIDIA: 403 case PCI_EHCI_VENDORID_NVIDIA2: 404 sprintf(sc->sc_vendor, "nVidia"); 405 break; 406 case PCI_EHCI_VENDORID_VIA: 407 sprintf(sc->sc_vendor, "VIA"); 408 break; 409 case PCI_EHCI_VENDORID_VMWARE: 410 sprintf(sc->sc_vendor, "VMware"); 411 break; 412 default: 413 if (bootverbose) 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)); 417 } 418 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); 422 #else 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); 425 #endif 426 if (err) { 427 device_printf(self, "Could not setup irq, %d\n", err); 428 sc->sc_intr_hdl = NULL; 429 goto error; 430 } 431 ehci_pci_take_controller(self); 432 433 /* Undocumented quirks taken from Linux */ 434 435 switch (pci_get_vendor(self)) { 436 case PCI_EHCI_VENDORID_ATI: 437 /* SB600 and SB700 EHCI quirk */ 438 switch (pci_get_device(self)) { 439 case 0x4386: 440 ehci_pci_ati_quirk(self, 0); 441 break; 442 case 0x4396: 443 ehci_pci_ati_quirk(self, 1); 444 break; 445 default: 446 break; 447 } 448 break; 449 450 case PCI_EHCI_VENDORID_VIA: 451 ehci_pci_via_quirk(self); 452 break; 453 454 default: 455 break; 456 } 457 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; 463 if (bootverbose) 464 device_printf(self, 465 "Dropped interrupts workaround enabled\n"); 466 break; 467 default: 468 break; 469 } 470 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; 476 if (bootverbose) 477 device_printf(self, 478 "Doorbell workaround enabled\n"); 479 break; 480 default: 481 break; 482 } 483 484 err = ehci_init(sc); 485 if (!err) { 486 err = device_probe_and_attach(sc->sc_bus.bdev); 487 } 488 if (err) { 489 device_printf(self, "USB init failed err=%d\n", err); 490 goto error; 491 } 492 return (0); 493 494 error: 495 ehci_pci_detach(self); 496 return (ENXIO); 497 } 498 499 static int 500 ehci_pci_detach(device_t self) 501 { 502 ehci_softc_t *sc = device_get_softc(self); 503 504 /* during module unload there are lots of children leftover */ 505 device_delete_children(self); 506 507 pci_disable_busmaster(self); 508 509 if (sc->sc_irq_res && sc->sc_intr_hdl) { 510 /* 511 * only call ehci_detach() after ehci_init() 512 */ 513 ehci_detach(sc); 514 515 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 516 517 if (err) 518 /* XXX or should we panic? */ 519 device_printf(self, "Could not tear down irq, %d\n", 520 err); 521 sc->sc_intr_hdl = NULL; 522 } 523 if (sc->sc_irq_res) { 524 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 525 sc->sc_irq_res = NULL; 526 } 527 if (sc->sc_io_res) { 528 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, 529 sc->sc_io_res); 530 sc->sc_io_res = NULL; 531 } 532 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); 533 534 return (0); 535 } 536 537 static int 538 ehci_pci_take_controller(device_t self) 539 { 540 ehci_softc_t *sc = device_get_softc(self); 541 uint32_t cparams; 542 uint32_t eec; 543 uint16_t to; 544 uint8_t eecp; 545 uint8_t bios_sem; 546 547 cparams = EREAD4(sc, EHCI_HCCPARAMS); 548 549 /* Synchronise with the BIOS if it owns the controller. */ 550 for (eecp = EHCI_HCC_EECP(cparams); eecp != 0; 551 eecp = EHCI_EECP_NEXT(eec)) { 552 eec = pci_read_config(self, eecp, 4); 553 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) { 554 continue; 555 } 556 bios_sem = pci_read_config(self, eecp + 557 EHCI_LEGSUP_BIOS_SEM, 1); 558 if (bios_sem == 0) { 559 continue; 560 } 561 device_printf(sc->sc_bus.bdev, "waiting for BIOS " 562 "to give up control\n"); 563 pci_write_config(self, eecp + 564 EHCI_LEGSUP_OS_SEM, 1, 1); 565 to = 500; 566 while (1) { 567 bios_sem = pci_read_config(self, eecp + 568 EHCI_LEGSUP_BIOS_SEM, 1); 569 if (bios_sem == 0) 570 break; 571 572 if (--to == 0) { 573 device_printf(sc->sc_bus.bdev, 574 "timed out waiting for BIOS\n"); 575 break; 576 } 577 usb_pause_mtx(NULL, hz / 100); /* wait 10ms */ 578 } 579 } 580 return (0); 581 } 582 583 static device_method_t ehci_pci_methods[] = { 584 /* Device interface */ 585 DEVMETHOD(device_probe, ehci_pci_probe), 586 DEVMETHOD(device_attach, ehci_pci_attach), 587 DEVMETHOD(device_detach, ehci_pci_detach), 588 DEVMETHOD(device_suspend, bus_generic_suspend), 589 DEVMETHOD(device_resume, bus_generic_resume), 590 DEVMETHOD(device_shutdown, bus_generic_shutdown), 591 DEVMETHOD(usb_take_controller, ehci_pci_take_controller), 592 593 DEVMETHOD_END 594 }; 595 596 static driver_t ehci_driver = { 597 .name = "ehci", 598 .methods = ehci_pci_methods, 599 .size = sizeof(struct ehci_softc), 600 }; 601 602 DRIVER_MODULE(ehci, pci, ehci_driver, 0, 0); 603 MODULE_DEPEND(ehci, usb, 1, 1, 1); 604