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