ehci_pci.c revision 185950
150476Speter/*- 215903Swosch * Copyright (c) 1998 The NetBSD Foundation, Inc. 315903Swosch * All rights reserved. 415903Swosch * 515903Swosch * This code is derived from software contributed to The NetBSD Foundation 615903Swosch * by Lennart Augustsson (augustss@carlstedt.se) at 715903Swosch * Carlstedt Research & Technology. 8105327Sru * 9105327Sru * Redistribution and use in source and binary forms, with or without 10105327Sru * modification, are permitted provided that the following conditions 11105327Sru * are met: 1215903Swosch * 1. Redistributions of source code must retain the above copyright 1315903Swosch * notice, this list of conditions and the following disclaimer. 14105327Sru * 2. Redistributions in binary form must reproduce the above copyright 15105327Sru * notice, this list of conditions and the following disclaimer in the 16105327Sru * documentation and/or other materials provided with the distribution. 17105327Sru * 3. All advertising materials mentioning features or use of this software 1815903Swosch * must display the following acknowledgement: 1915903Swosch * This product includes software developed by the NetBSD 2015903Swosch * Foundation, Inc. and its contributors. 2115903Swosch * 4. Neither the name of The NetBSD Foundation nor the names of its 2215903Swosch * contributors may be used to endorse or promote products derived 2315903Swosch * from this software without specific prior written permission. 24124490Sru * 25124490Sru * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2615903Swosch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2715903Swosch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2815903Swosch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2915903Swosch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3015903Swosch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3115903Swosch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3215903Swosch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3315903Swosch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3415903Swosch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3515903Swosch * POSSIBILITY OF SUCH DAMAGE. 3615903Swosch */ 37105327Sru 38105327Sru#include <sys/cdefs.h> 39105327Sru__FBSDID("$FreeBSD: head/sys/dev/usb2/controller/ehci2_pci.c 185950 2008-12-11 23:17:48Z thompsa $"); 40105327Sru 411845Swollman/* 4295255Sru * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller. 4395255Sru * 4495255Sru * The EHCI 1.0 spec can be found at 4515903Swosch * http://developer.intel.com/technology/usb/download/ehci-r10.pdf 46105327Sru * and the USB 2.0 spec at 47105327Sru * http://www.usb.org/developers/docs/usb_20.zip 48105327Sru */ 49105327Sru 50105327Sru/* The low level controller code for EHCI has been split into 5194768Sru * PCI probes and EHCI specific code. This was done to facilitate the 5296132Sbde * sharing of code between *BSD's 5394768Sru */ 5494768Sru 5594768Sru#include <dev/usb2/include/usb2_mfunc.h> 5614986Swosch#include <dev/usb2/include/usb2_defs.h> 5714986Swosch#include <dev/usb2/include/usb2_standard.h> 58105327Sru 59105327Sru#include <dev/usb2/core/usb2_core.h> 60105327Sru#include <dev/usb2/core/usb2_busdma.h> 61105327Sru#include <dev/usb2/core/usb2_process.h> 62105327Sru#include <dev/usb2/core/usb2_config_td.h> 63105327Sru#include <dev/usb2/core/usb2_sw_transfer.h> 64105327Sru#include <dev/usb2/core/usb2_util.h> 65105327Sru 66105327Sru#include <dev/usb2/controller/usb2_controller.h> 67105327Sru#include <dev/usb2/controller/usb2_bus.h> 68105327Sru#include <dev/usb2/controller/usb2_pci.h> 69105327Sru#include <dev/usb2/controller/ehci2.h> 70105327Sru 71105327Sru#define PCI_EHCI_VENDORID_ACERLABS 0x10b9 7235951Sbde#define PCI_EHCI_VENDORID_AMD 0x1022 7335951Sbde#define PCI_EHCI_VENDORID_APPLE 0x106b 7435951Sbde#define PCI_EHCI_VENDORID_ATI 0x1002 75124637Sru#define PCI_EHCI_VENDORID_CMDTECH 0x1095 76124637Sru#define PCI_EHCI_VENDORID_INTEL 0x8086 77124637Sru#define PCI_EHCI_VENDORID_NEC 0x1033 78124637Sru#define PCI_EHCI_VENDORID_OPTI 0x1045 79124637Sru#define PCI_EHCI_VENDORID_PHILIPS 0x1131 80124637Sru#define PCI_EHCI_VENDORID_SIS 0x1039 8135951Sbde#define PCI_EHCI_VENDORID_NVIDIA 0x12D2 82124435Sru#define PCI_EHCI_VENDORID_NVIDIA2 0x10DE 8335951Sbde#define PCI_EHCI_VENDORID_VIA 0x1106 8435951Sbde 85124637Sru#define PCI_EHCI_BASE_REG 0x10 86124637Sru 87124637Srustatic void ehci_pci_takecontroller(device_t self); 8835951Sbde 89124435Srustatic device_probe_t ehci_pci_probe; 9035951Sbdestatic device_attach_t ehci_pci_attach; 9135951Sbdestatic device_detach_t ehci_pci_detach; 9235951Sbdestatic device_suspend_t ehci_pci_suspend; 9335951Sbdestatic device_resume_t ehci_pci_resume; 94124435Srustatic device_shutdown_t ehci_pci_shutdown; 9535951Sbde 96124435Srustatic int 9735951Sbdeehci_pci_suspend(device_t self) 9835951Sbde{ 9935951Sbde ehci_softc_t *sc = device_get_softc(self); 10035951Sbde int err; 10135951Sbde 102124435Sru err = bus_generic_suspend(self); 10335951Sbde if (err) 104124435Sru return (err); 10535951Sbde ehci_suspend(sc); 10635951Sbde return (0); 10735951Sbde} 108124435Sru 109124435Srustatic int 11035951Sbdeehci_pci_resume(device_t self) 11135951Sbde{ 11235951Sbde ehci_softc_t *sc = device_get_softc(self); 11335951Sbde 11435951Sbde ehci_pci_takecontroller(self); 115124637Sru ehci_resume(sc); 116124637Sru 117124637Sru bus_generic_resume(self); 11835951Sbde 11935951Sbde return (0); 12035951Sbde} 12135951Sbde 1221845Swollmanstatic int 12330113Sjkhehci_pci_shutdown(device_t self) 12495306Sru{ 12514986Swosch ehci_softc_t *sc = device_get_softc(self); 12634181Sbde int err; 12734181Sbde 128124490Sru err = bus_generic_shutdown(self); 129124490Sru if (err) 13014986Swosch return (err); 131124490Sru ehci_shutdown(sc); 13234181Sbde 13334181Sbde return (0); 134117171Sru} 13514986Swosch 136124490Srustatic const char * 137124490Sruehci_pci_match(device_t self) 13814986Swosch{ 13934181Sbde uint32_t device_id = pci_get_devid(self); 14036673Sdt 14114986Swosch switch (device_id) { 142124490Sru case 0x268c8086: 14336054Sbde return ("Intel 63XXESB USB 2.0 controller"); 14436054Sbde 14536054Sbde case 0x523910b9: 14636054Sbde return "ALi M5239 USB 2.0 controller"; 14736054Sbde 14824750Sbde case 0x10227463: 14994922Sru return "AMD 8111 USB 2.0 controller"; 15094841Sru 15124750Sbde case 0x20951022: 15214986Swosch return ("AMD CS5536 (Geode) USB 2.0 controller"); 15330113Sjkh 1541845Swollman case 0x43451002: 15595306Sru return "ATI SB200 USB 2.0 controller"; 1561845Swollman case 0x43731002: 1571845Swollman return "ATI SB400 USB 2.0 controller"; 1581845Swollman 15930113Sjkh case 0x25ad8086: 16030113Sjkh return "Intel 6300ESB USB 2.0 controller"; 16130113Sjkh case 0x24cd8086: 1621845Swollman return "Intel 82801DB/L/M (ICH4) USB 2.0 controller"; 1631845Swollman case 0x24dd8086: 1641845Swollman return "Intel 82801EB/R (ICH5) USB 2.0 controller"; 1651845Swollman case 0x265c8086: 1661845Swollman return "Intel 82801FB (ICH6) USB 2.0 controller"; 1671845Swollman case 0x27cc8086: 16816663Sjkh return "Intel 82801GB/R (ICH7) USB 2.0 controller"; 16995306Sru 17028806Sbde case 0x28368086: 171105327Sru return "Intel 82801H (ICH8) USB 2.0 controller USB2-A"; 172105327Sru case 0x283a8086: 173105327Sru return "Intel 82801H (ICH8) USB 2.0 controller USB2-B"; 174105327Sru case 0x293a8086: 17524861Sjkh return "Intel 82801I (ICH9) USB 2.0 controller"; 176105327Sru case 0x293c8086: 17716663Sjkh return "Intel 82801I (ICH9) USB 2.0 controller"; 17816663Sjkh 17924861Sjkh case 0x00e01033: 180105327Sru return ("NEC uPD 720100 USB 2.0 controller"); 18199344Sru 18299344Sru case 0x006810de: 183117195Sbde return "NVIDIA nForce2 USB 2.0 controller"; 184117195Sbde case 0x008810de: 18599344Sru return "NVIDIA nForce2 Ultra 400 USB 2.0 controller"; 186117195Sbde case 0x00d810de: 187117195Sbde return "NVIDIA nForce3 USB 2.0 controller"; 188117195Sbde case 0x00e810de: 189117195Sbde return "NVIDIA nForce3 250 USB 2.0 controller"; 19099344Sru case 0x005b10de: 191117195Sbde return "NVIDIA nForce4 USB 2.0 controller"; 192 193 case 0x15621131: 194 return "Philips ISP156x USB 2.0 controller"; 195 196 case 0x31041106: 197 return ("VIA VT6202 USB 2.0 controller"); 198 199 default: 200 break; 201 } 202 203 if ((pci_get_class(self) == PCIC_SERIALBUS) 204 && (pci_get_subclass(self) == PCIS_SERIALBUS_USB) 205 && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) { 206 return ("EHCI (generic) USB 2.0 controller"); 207 } 208 return (NULL); /* dunno */ 209} 210 211static int 212ehci_pci_probe(device_t self) 213{ 214 const char *desc = ehci_pci_match(self); 215 216 if (desc) { 217 device_set_desc(self, desc); 218 return (0); 219 } else { 220 return (ENXIO); 221 } 222} 223 224static int 225ehci_pci_attach(device_t self) 226{ 227 ehci_softc_t *sc = device_get_softc(self); 228 int err; 229 int rid; 230 231 if (sc == NULL) { 232 device_printf(self, "Could not allocate sc\n"); 233 return (ENXIO); 234 } 235 /* get all DMA memory */ 236 237 if (usb2_bus_mem_alloc_all(&sc->sc_bus, 238 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) { 239 return ENOMEM; 240 } 241 sc->sc_dev = self; 242 243 pci_enable_busmaster(self); 244 245 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) { 246 case PCI_USB_REV_PRE_1_0: 247 case PCI_USB_REV_1_0: 248 case PCI_USB_REV_1_1: 249 /* 250 * NOTE: some EHCI USB controllers have the wrong USB 251 * revision number. It appears those controllers are 252 * fully compliant so we just ignore this value in 253 * some common cases. 254 */ 255 device_printf(self, "pre-2.0 USB revision (ignored)\n"); 256 /* fallthrough */ 257 case PCI_USB_REV_2_0: 258 sc->sc_bus.usbrev = USB_REV_2_0; 259 break; 260 default: 261 sc->sc_bus.usbrev = USB_REV_UNKNOWN; 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 err = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_bus.bus_mtx, 341 NULL, 0, 4); 342 if (err) { 343 device_printf(self, "could not setup config thread!\n"); 344 goto error; 345 } 346#if (__FreeBSD_version >= 700031) 347 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 348 NULL, (void *)(void *)ehci_interrupt, sc, &sc->sc_intr_hdl); 349#else 350 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 351 (void *)(void *)ehci_interrupt, sc, &sc->sc_intr_hdl); 352#endif 353 if (err) { 354 device_printf(self, "Could not setup irq, %d\n", err); 355 sc->sc_intr_hdl = NULL; 356 goto error; 357 } 358 ehci_pci_takecontroller(self); 359 err = ehci_init(sc); 360 if (!err) { 361 err = device_probe_and_attach(sc->sc_bus.bdev); 362 } 363 if (err) { 364 device_printf(self, "USB init failed err=%d\n", err); 365 goto error; 366 } 367 return (0); 368 369error: 370 ehci_pci_detach(self); 371 return (ENXIO); 372} 373 374static int 375ehci_pci_detach(device_t self) 376{ 377 ehci_softc_t *sc = device_get_softc(self); 378 device_t bdev; 379 380 usb2_config_td_drain(&sc->sc_config_td); 381 382 if (sc->sc_bus.bdev) { 383 bdev = sc->sc_bus.bdev; 384 device_detach(bdev); 385 device_delete_child(self, bdev); 386 } 387 /* during module unload there are lots of children leftover */ 388 device_delete_all_children(self); 389 390 pci_disable_busmaster(self); 391 392 /* 393 * disable interrupts that might have been switched on in ehci_init 394 */ 395 if (sc->sc_io_res) { 396 EWRITE4(sc, EHCI_USBINTR, 0); 397 } 398 if (sc->sc_irq_res && sc->sc_intr_hdl) { 399 /* 400 * only call ehci_detach() after ehci_init() 401 */ 402 ehci_detach(sc); 403 404 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 405 406 if (err) 407 /* XXX or should we panic? */ 408 device_printf(self, "Could not tear down irq, %d\n", 409 err); 410 sc->sc_intr_hdl = NULL; 411 } 412 if (sc->sc_irq_res) { 413 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 414 sc->sc_irq_res = NULL; 415 } 416 if (sc->sc_io_res) { 417 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, 418 sc->sc_io_res); 419 sc->sc_io_res = NULL; 420 } 421 usb2_config_td_unsetup(&sc->sc_config_td); 422 423 usb2_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); 424 425 return (0); 426} 427 428static void 429ehci_pci_takecontroller(device_t self) 430{ 431 ehci_softc_t *sc = device_get_softc(self); 432 uint32_t cparams; 433 uint32_t eec; 434 uint16_t to; 435 uint8_t eecp; 436 uint8_t bios_sem; 437 438 cparams = EREAD4(sc, EHCI_HCCPARAMS); 439 440 /* Synchronise with the BIOS if it owns the controller. */ 441 for (eecp = EHCI_HCC_EECP(cparams); eecp != 0; 442 eecp = EHCI_EECP_NEXT(eec)) { 443 eec = pci_read_config(self, eecp, 4); 444 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) { 445 continue; 446 } 447 bios_sem = pci_read_config(self, eecp + 448 EHCI_LEGSUP_BIOS_SEM, 1); 449 if (bios_sem == 0) { 450 continue; 451 } 452 device_printf(sc->sc_bus.bdev, "waiting for BIOS " 453 "to give up control\n"); 454 pci_write_config(self, eecp + 455 EHCI_LEGSUP_OS_SEM, 1, 1); 456 to = 500; 457 while (1) { 458 bios_sem = pci_read_config(self, eecp + 459 EHCI_LEGSUP_BIOS_SEM, 1); 460 if (bios_sem == 0) 461 break; 462 463 if (--to == 0) { 464 device_printf(sc->sc_bus.bdev, 465 "timed out waiting for BIOS\n"); 466 break; 467 } 468 usb2_pause_mtx(NULL, 10); /* wait 10ms */ 469 } 470 } 471} 472 473static driver_t ehci_driver = 474{ 475 .name = "ehci", 476 .methods = (device_method_t[]){ 477 /* device interface */ 478 DEVMETHOD(device_probe, ehci_pci_probe), 479 DEVMETHOD(device_attach, ehci_pci_attach), 480 DEVMETHOD(device_detach, ehci_pci_detach), 481 DEVMETHOD(device_suspend, ehci_pci_suspend), 482 DEVMETHOD(device_resume, ehci_pci_resume), 483 DEVMETHOD(device_shutdown, ehci_pci_shutdown), 484 /* bus interface */ 485 DEVMETHOD(bus_print_child, bus_generic_print_child), 486 487 {0, 0} 488 }, 489 .size = sizeof(struct ehci_softc), 490}; 491 492static devclass_t ehci_devclass; 493 494DRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0); 495DRIVER_MODULE(ehci, cardbus, ehci_driver, ehci_devclass, 0, 0); 496MODULE_DEPEND(ehci, usb2_controller, 1, 1, 1); 497MODULE_DEPEND(ehci, usb2_core, 1, 1, 1); 498