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 * 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. 24 * 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. 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 /* 42 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller. 43 * 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 48 */ 49 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 53 */ 54 55 #include <dev/usb/usb_mfunc.h> 56 #include <dev/usb/usb_defs.h> 57 #include <dev/usb/usb.h> 58 59 #include <dev/usb/usb_core.h> 60 #include <dev/usb/usb_busdma.h> 61 #include <dev/usb/usb_process.h> 62 #include <dev/usb/usb_sw_transfer.h> 63 #include <dev/usb/usb_util.h> 64 65 #include <dev/usb/usb_controller.h> 66 #include <dev/usb/usb_bus.h> 67 #include <dev/usb/usb_pci.h> 68 #include <dev/usb/controller/ehci.h> 69 70 #define PCI_EHCI_VENDORID_ACERLABS 0x10b9 71 #define PCI_EHCI_VENDORID_AMD 0x1022 72 #define PCI_EHCI_VENDORID_APPLE 0x106b 73 #define PCI_EHCI_VENDORID_ATI 0x1002 74 #define PCI_EHCI_VENDORID_CMDTECH 0x1095 75 #define PCI_EHCI_VENDORID_INTEL 0x8086 76 #define PCI_EHCI_VENDORID_NEC 0x1033 77 #define PCI_EHCI_VENDORID_OPTI 0x1045 78 #define PCI_EHCI_VENDORID_PHILIPS 0x1131 79 #define PCI_EHCI_VENDORID_SIS 0x1039 80 #define PCI_EHCI_VENDORID_NVIDIA 0x12D2 81 #define PCI_EHCI_VENDORID_NVIDIA2 0x10DE 82 #define PCI_EHCI_VENDORID_VIA 0x1106 83 84 #define PCI_EHCI_BASE_REG 0x10 85 86 static void ehci_pci_takecontroller(device_t self); 87 88 static device_probe_t ehci_pci_probe; 89 static device_attach_t ehci_pci_attach; 90 static device_detach_t ehci_pci_detach; 91 static device_suspend_t ehci_pci_suspend; 92 static device_resume_t ehci_pci_resume; 93 static device_shutdown_t ehci_pci_shutdown; 94 95 static int 96 ehci_pci_suspend(device_t self) 97 { 98 ehci_softc_t *sc = device_get_softc(self); 99 int err; 100 101 err = bus_generic_suspend(self); 102 if (err) 103 return (err); 104 ehci_suspend(sc); 105 return (0); 106 } 107 108 static int 109 ehci_pci_resume(device_t self) 110 { 111 ehci_softc_t *sc = device_get_softc(self); 112 113 ehci_pci_takecontroller(self); 114 ehci_resume(sc); 115 116 bus_generic_resume(self); 117 118 return (0); 119 } 120 121 static int 122 ehci_pci_shutdown(device_t self) 123 { 124 ehci_softc_t *sc = device_get_softc(self); 125 int err; 126 127 err = bus_generic_shutdown(self); 128 if (err) 129 return (err); 130 ehci_shutdown(sc); 131 132 return (0); 133 } 134 135 static const char * 136 ehci_pci_match(device_t self) 137 { 138 uint32_t device_id = pci_get_devid(self); 139 140 switch (device_id) { 141 case 0x268c8086: 142 return ("Intel 63XXESB USB 2.0 controller"); 143 144 case 0x523910b9: 145 return "ALi M5239 USB 2.0 controller"; 146 147 case 0x10227463: 148 return "AMD 8111 USB 2.0 controller"; 149 150 case 0x20951022: 151 return ("AMD CS5536 (Geode) USB 2.0 controller"); 152 153 case 0x43451002: 154 return "ATI SB200 USB 2.0 controller"; 155 case 0x43731002: 156 return "ATI SB400 USB 2.0 controller"; 157 158 case 0x25ad8086: 159 return "Intel 6300ESB USB 2.0 controller"; 160 case 0x24cd8086: 161 return "Intel 82801DB/L/M (ICH4) USB 2.0 controller"; 162 case 0x24dd8086: 163 return "Intel 82801EB/R (ICH5) USB 2.0 controller"; 164 case 0x265c8086: 165 return "Intel 82801FB (ICH6) USB 2.0 controller"; 166 case 0x27cc8086: 167 return "Intel 82801GB/R (ICH7) USB 2.0 controller"; 168 169 case 0x28368086: 170 return "Intel 82801H (ICH8) USB 2.0 controller USB2-A"; 171 case 0x283a8086: 172 return "Intel 82801H (ICH8) USB 2.0 controller USB2-B"; 173 case 0x293a8086: 174 return "Intel 82801I (ICH9) USB 2.0 controller"; 175 case 0x293c8086: 176 return "Intel 82801I (ICH9) USB 2.0 controller"; 177 178 case 0x00e01033: 179 return ("NEC uPD 720100 USB 2.0 controller"); 180 181 case 0x006810de: 182 return "NVIDIA nForce2 USB 2.0 controller"; 183 case 0x008810de: 184 return "NVIDIA nForce2 Ultra 400 USB 2.0 controller"; 185 case 0x00d810de: 186 return "NVIDIA nForce3 USB 2.0 controller"; 187 case 0x00e810de: 188 return "NVIDIA nForce3 250 USB 2.0 controller"; 189 case 0x005b10de: 190 return "NVIDIA nForce4 USB 2.0 controller"; 191 192 case 0x15621131: 193 return "Philips ISP156x USB 2.0 controller"; 194 195 case 0x31041106: 196 return ("VIA VT6202 USB 2.0 controller"); 197 198 default: 199 break; 200 } 201 202 if ((pci_get_class(self) == PCIC_SERIALBUS) 203 && (pci_get_subclass(self) == PCIS_SERIALBUS_USB) 204 && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) { 205 return ("EHCI (generic) USB 2.0 controller"); 206 } 207 return (NULL); /* dunno */ 208 } 209 210 static int 211 ehci_pci_probe(device_t self) 212 { 213 const char *desc = ehci_pci_match(self); 214 215 if (desc) { 216 device_set_desc(self, desc); 217 return (0); 218 } else { 219 return (ENXIO); 220 } 221 } 222 223 static int 224 ehci_pci_attach(device_t self) 225 { 226 ehci_softc_t *sc = device_get_softc(self); 227 int err; 228 int rid; 229 230 /* initialise some bus fields */ 231 sc->sc_bus.parent = self; 232 sc->sc_bus.devices = sc->sc_devices; 233 sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 234 235 /* get all DMA memory */ 236 if (usb2_bus_mem_alloc_all(&sc->sc_bus, 237 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) { 238 return (ENOMEM); 239 } 240 241 pci_enable_busmaster(self); 242 243 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) { 244 case PCI_USB_REV_PRE_1_0: 245 case PCI_USB_REV_1_0: 246 case PCI_USB_REV_1_1: 247 /* 248 * NOTE: some EHCI USB controllers have the wrong USB 249 * revision number. It appears those controllers are 250 * fully compliant so we just ignore this value in 251 * some common cases. 252 */ 253 device_printf(self, "pre-2.0 USB revision (ignored)\n"); 254 /* fallthrough */ 255 case PCI_USB_REV_2_0: 256 sc->sc_bus.usbrev = USB_REV_2_0; 257 break; 258 default: 259 /* Quirk for Parallels Desktop 4.0 */ 260 device_printf(self, "USB revision is unknown. Assuming v2.0.\n"); 261 sc->sc_bus.usbrev = USB_REV_2_0; 262 break; 263 } 264 265 rid = PCI_CBMEM; 266 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 267 RF_ACTIVE); 268 if (!sc->sc_io_res) { 269 device_printf(self, "Could not map memory\n"); 270 goto error; 271 } 272 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 273 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 274 sc->sc_io_size = rman_get_size(sc->sc_io_res); 275 276 rid = 0; 277 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 278 RF_SHAREABLE | RF_ACTIVE); 279 if (sc->sc_irq_res == NULL) { 280 device_printf(self, "Could not allocate irq\n"); 281 goto error; 282 } 283 sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 284 if (!sc->sc_bus.bdev) { 285 device_printf(self, "Could not add USB device\n"); 286 goto error; 287 } 288 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 289 290 /* 291 * ehci_pci_match will never return NULL if ehci_pci_probe 292 * succeeded 293 */ 294 device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self)); 295 switch (pci_get_vendor(self)) { 296 case PCI_EHCI_VENDORID_ACERLABS: 297 sprintf(sc->sc_vendor, "AcerLabs"); 298 break; 299 case PCI_EHCI_VENDORID_AMD: 300 sprintf(sc->sc_vendor, "AMD"); 301 break; 302 case PCI_EHCI_VENDORID_APPLE: 303 sprintf(sc->sc_vendor, "Apple"); 304 break; 305 case PCI_EHCI_VENDORID_ATI: 306 sprintf(sc->sc_vendor, "ATI"); 307 break; 308 case PCI_EHCI_VENDORID_CMDTECH: 309 sprintf(sc->sc_vendor, "CMDTECH"); 310 break; 311 case PCI_EHCI_VENDORID_INTEL: 312 sprintf(sc->sc_vendor, "Intel"); 313 break; 314 case PCI_EHCI_VENDORID_NEC: 315 sprintf(sc->sc_vendor, "NEC"); 316 break; 317 case PCI_EHCI_VENDORID_OPTI: 318 sprintf(sc->sc_vendor, "OPTi"); 319 break; 320 case PCI_EHCI_VENDORID_PHILIPS: 321 sprintf(sc->sc_vendor, "Philips"); 322 break; 323 case PCI_EHCI_VENDORID_SIS: 324 sprintf(sc->sc_vendor, "SiS"); 325 break; 326 case PCI_EHCI_VENDORID_NVIDIA: 327 case PCI_EHCI_VENDORID_NVIDIA2: 328 sprintf(sc->sc_vendor, "nVidia"); 329 break; 330 case PCI_EHCI_VENDORID_VIA: 331 sprintf(sc->sc_vendor, "VIA"); 332 break; 333 default: 334 if (bootverbose) 335 device_printf(self, "(New EHCI DeviceId=0x%08x)\n", 336 pci_get_devid(self)); 337 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self)); 338 } 339 340 #if (__FreeBSD_version >= 700031) 341 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 342 NULL, (void *)(void *)ehci_interrupt, sc, &sc->sc_intr_hdl); 343 #else 344 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 345 (void *)(void *)ehci_interrupt, sc, &sc->sc_intr_hdl); 346 #endif 347 if (err) { 348 device_printf(self, "Could not setup irq, %d\n", err); 349 sc->sc_intr_hdl = NULL; 350 goto error; 351 } 352 ehci_pci_takecontroller(self); 353 err = ehci_init(sc); 354 if (!err) { 355 err = device_probe_and_attach(sc->sc_bus.bdev); 356 } 357 if (err) { 358 device_printf(self, "USB init failed err=%d\n", err); 359 goto error; 360 } 361 return (0); 362 363 error: 364 ehci_pci_detach(self); 365 return (ENXIO); 366 } 367 368 static int 369 ehci_pci_detach(device_t self) 370 { 371 ehci_softc_t *sc = device_get_softc(self); 372 device_t bdev; 373 374 if (sc->sc_bus.bdev) { 375 bdev = sc->sc_bus.bdev; 376 device_detach(bdev); 377 device_delete_child(self, bdev); 378 } 379 /* during module unload there are lots of children leftover */ 380 device_delete_all_children(self); 381 382 pci_disable_busmaster(self); 383 384 /* 385 * disable interrupts that might have been switched on in ehci_init 386 */ 387 if (sc->sc_io_res) { 388 EWRITE4(sc, EHCI_USBINTR, 0); 389 } 390 if (sc->sc_irq_res && sc->sc_intr_hdl) { 391 /* 392 * only call ehci_detach() after ehci_init() 393 */ 394 ehci_detach(sc); 395 396 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 397 398 if (err) 399 /* XXX or should we panic? */ 400 device_printf(self, "Could not tear down irq, %d\n", 401 err); 402 sc->sc_intr_hdl = NULL; 403 } 404 if (sc->sc_irq_res) { 405 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 406 sc->sc_irq_res = NULL; 407 } 408 if (sc->sc_io_res) { 409 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, 410 sc->sc_io_res); 411 sc->sc_io_res = NULL; 412 } 413 usb2_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); 414 415 return (0); 416 } 417 418 static void 419 ehci_pci_takecontroller(device_t self) 420 { 421 ehci_softc_t *sc = device_get_softc(self); 422 uint32_t cparams; 423 uint32_t eec; 424 uint16_t to; 425 uint8_t eecp; 426 uint8_t bios_sem; 427 428 cparams = EREAD4(sc, EHCI_HCCPARAMS); 429 430 /* Synchronise with the BIOS if it owns the controller. */ 431 for (eecp = EHCI_HCC_EECP(cparams); eecp != 0; 432 eecp = EHCI_EECP_NEXT(eec)) { 433 eec = pci_read_config(self, eecp, 4); 434 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) { 435 continue; 436 } 437 bios_sem = pci_read_config(self, eecp + 438 EHCI_LEGSUP_BIOS_SEM, 1); 439 if (bios_sem == 0) { 440 continue; 441 } 442 device_printf(sc->sc_bus.bdev, "waiting for BIOS " 443 "to give up control\n"); 444 pci_write_config(self, eecp + 445 EHCI_LEGSUP_OS_SEM, 1, 1); 446 to = 500; 447 while (1) { 448 bios_sem = pci_read_config(self, eecp + 449 EHCI_LEGSUP_BIOS_SEM, 1); 450 if (bios_sem == 0) 451 break; 452 453 if (--to == 0) { 454 device_printf(sc->sc_bus.bdev, 455 "timed out waiting for BIOS\n"); 456 break; 457 } 458 usb2_pause_mtx(NULL, hz / 100); /* wait 10ms */ 459 } 460 } 461 } 462 463 static driver_t ehci_driver = 464 { 465 .name = "ehci", 466 .methods = (device_method_t[]){ 467 /* device interface */ 468 DEVMETHOD(device_probe, ehci_pci_probe), 469 DEVMETHOD(device_attach, ehci_pci_attach), 470 DEVMETHOD(device_detach, ehci_pci_detach), 471 DEVMETHOD(device_suspend, ehci_pci_suspend), 472 DEVMETHOD(device_resume, ehci_pci_resume), 473 DEVMETHOD(device_shutdown, ehci_pci_shutdown), 474 /* bus interface */ 475 DEVMETHOD(bus_print_child, bus_generic_print_child), 476 477 {0, 0} 478 }, 479 .size = sizeof(struct ehci_softc), 480 }; 481 482 static devclass_t ehci_devclass; 483 484 DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0); 485 DRIVER_MODULE(ehci, cardbus, ehci_driver, ehci_devclass, 0, 0); 486 MODULE_DEPEND(ehci, usb, 1, 1, 1); 487