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