ehci_pci.c revision 203693
1184610Salfred/*- 2184610Salfred * Copyright (c) 1998 The NetBSD Foundation, Inc. 3184610Salfred * All rights reserved. 4184610Salfred * 5184610Salfred * This code is derived from software contributed to The NetBSD Foundation 6184610Salfred * by Lennart Augustsson (augustss@carlstedt.se) at 7184610Salfred * Carlstedt Research & Technology. 8184610Salfred * 9184610Salfred * Redistribution and use in source and binary forms, with or without 10184610Salfred * modification, are permitted provided that the following conditions 11184610Salfred * are met: 12184610Salfred * 1. Redistributions of source code must retain the above copyright 13184610Salfred * notice, this list of conditions and the following disclaimer. 14184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 15184610Salfred * notice, this list of conditions and the following disclaimer in the 16184610Salfred * documentation and/or other materials provided with the distribution. 17184610Salfred * 3. All advertising materials mentioning features or use of this software 18184610Salfred * must display the following acknowledgement: 19184610Salfred * This product includes software developed by the NetBSD 20184610Salfred * Foundation, Inc. and its contributors. 21184610Salfred * 4. Neither the name of The NetBSD Foundation nor the names of its 22184610Salfred * contributors may be used to endorse or promote products derived 23184610Salfred * from this software without specific prior written permission. 24184610Salfred * 25184610Salfred * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26184610Salfred * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27184610Salfred * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28184610Salfred * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29184610Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30184610Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31184610Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32184610Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33184610Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34184610Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35184610Salfred * POSSIBILITY OF SUCH DAMAGE. 36184610Salfred */ 37184610Salfred 38184610Salfred#include <sys/cdefs.h> 39184610Salfred__FBSDID("$FreeBSD: head/sys/dev/usb/controller/ehci_pci.c 203693 2010-02-09 00:38:40Z thompsa $"); 40184610Salfred 41184610Salfred/* 42184610Salfred * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller. 43184610Salfred * 44184610Salfred * The EHCI 1.0 spec can be found at 45184610Salfred * http://developer.intel.com/technology/usb/download/ehci-r10.pdf 46184610Salfred * and the USB 2.0 spec at 47184610Salfred * http://www.usb.org/developers/docs/usb_20.zip 48184610Salfred */ 49184610Salfred 50184610Salfred/* The low level controller code for EHCI has been split into 51184610Salfred * PCI probes and EHCI specific code. This was done to facilitate the 52184610Salfred * sharing of code between *BSD's 53184610Salfred */ 54184610Salfred 55194677Sthompsa#include <sys/stdint.h> 56194677Sthompsa#include <sys/stddef.h> 57194677Sthompsa#include <sys/param.h> 58194677Sthompsa#include <sys/queue.h> 59194677Sthompsa#include <sys/types.h> 60194677Sthompsa#include <sys/systm.h> 61194677Sthompsa#include <sys/kernel.h> 62194677Sthompsa#include <sys/bus.h> 63194677Sthompsa#include <sys/linker_set.h> 64194677Sthompsa#include <sys/module.h> 65194677Sthompsa#include <sys/lock.h> 66194677Sthompsa#include <sys/mutex.h> 67194677Sthompsa#include <sys/condvar.h> 68194677Sthompsa#include <sys/sysctl.h> 69194677Sthompsa#include <sys/sx.h> 70194677Sthompsa#include <sys/unistd.h> 71194677Sthompsa#include <sys/callout.h> 72194677Sthompsa#include <sys/malloc.h> 73194677Sthompsa#include <sys/priv.h> 74194677Sthompsa 75188942Sthompsa#include <dev/usb/usb.h> 76194677Sthompsa#include <dev/usb/usbdi.h> 77184610Salfred 78188942Sthompsa#include <dev/usb/usb_core.h> 79188942Sthompsa#include <dev/usb/usb_busdma.h> 80188942Sthompsa#include <dev/usb/usb_process.h> 81188942Sthompsa#include <dev/usb/usb_util.h> 82184610Salfred 83188942Sthompsa#include <dev/usb/usb_controller.h> 84188942Sthompsa#include <dev/usb/usb_bus.h> 85188942Sthompsa#include <dev/usb/usb_pci.h> 86188942Sthompsa#include <dev/usb/controller/ehci.h> 87198151Sthompsa#include <dev/usb/controller/ehcireg.h> 88184610Salfred 89184610Salfred#define PCI_EHCI_VENDORID_ACERLABS 0x10b9 90184610Salfred#define PCI_EHCI_VENDORID_AMD 0x1022 91184610Salfred#define PCI_EHCI_VENDORID_APPLE 0x106b 92184610Salfred#define PCI_EHCI_VENDORID_ATI 0x1002 93184610Salfred#define PCI_EHCI_VENDORID_CMDTECH 0x1095 94184610Salfred#define PCI_EHCI_VENDORID_INTEL 0x8086 95184610Salfred#define PCI_EHCI_VENDORID_NEC 0x1033 96184610Salfred#define PCI_EHCI_VENDORID_OPTI 0x1045 97184610Salfred#define PCI_EHCI_VENDORID_PHILIPS 0x1131 98184610Salfred#define PCI_EHCI_VENDORID_SIS 0x1039 99184610Salfred#define PCI_EHCI_VENDORID_NVIDIA 0x12D2 100184610Salfred#define PCI_EHCI_VENDORID_NVIDIA2 0x10DE 101184610Salfred#define PCI_EHCI_VENDORID_VIA 0x1106 102184610Salfred 103184610Salfred#define PCI_EHCI_BASE_REG 0x10 104184610Salfred 105198501Sthompsastatic void ehci_pci_takecontroller(device_t self); 106198501Sthompsa 107184610Salfredstatic device_probe_t ehci_pci_probe; 108184610Salfredstatic device_attach_t ehci_pci_attach; 109184610Salfredstatic device_detach_t ehci_pci_detach; 110184610Salfredstatic device_suspend_t ehci_pci_suspend; 111184610Salfredstatic device_resume_t ehci_pci_resume; 112184610Salfredstatic device_shutdown_t ehci_pci_shutdown; 113184610Salfred 114184610Salfredstatic int 115184610Salfredehci_pci_suspend(device_t self) 116184610Salfred{ 117184610Salfred ehci_softc_t *sc = device_get_softc(self); 118184610Salfred int err; 119184610Salfred 120184610Salfred err = bus_generic_suspend(self); 121184610Salfred if (err) 122184610Salfred return (err); 123184610Salfred ehci_suspend(sc); 124184610Salfred return (0); 125184610Salfred} 126184610Salfred 127184610Salfredstatic int 128184610Salfredehci_pci_resume(device_t self) 129184610Salfred{ 130184610Salfred ehci_softc_t *sc = device_get_softc(self); 131184610Salfred 132198501Sthompsa ehci_pci_takecontroller(self); 133184610Salfred ehci_resume(sc); 134184610Salfred 135184610Salfred bus_generic_resume(self); 136184610Salfred 137184610Salfred return (0); 138184610Salfred} 139184610Salfred 140184610Salfredstatic int 141184610Salfredehci_pci_shutdown(device_t self) 142184610Salfred{ 143184610Salfred ehci_softc_t *sc = device_get_softc(self); 144184610Salfred int err; 145184610Salfred 146184610Salfred err = bus_generic_shutdown(self); 147184610Salfred if (err) 148184610Salfred return (err); 149184610Salfred ehci_shutdown(sc); 150184610Salfred 151184610Salfred return (0); 152184610Salfred} 153184610Salfred 154184610Salfredstatic const char * 155184610Salfredehci_pci_match(device_t self) 156184610Salfred{ 157184610Salfred uint32_t device_id = pci_get_devid(self); 158184610Salfred 159184610Salfred switch (device_id) { 160184610Salfred case 0x268c8086: 161184610Salfred return ("Intel 63XXESB USB 2.0 controller"); 162184610Salfred 163184610Salfred case 0x523910b9: 164184610Salfred return "ALi M5239 USB 2.0 controller"; 165184610Salfred 166184610Salfred case 0x10227463: 167184610Salfred return "AMD 8111 USB 2.0 controller"; 168184610Salfred 169184610Salfred case 0x20951022: 170184610Salfred return ("AMD CS5536 (Geode) USB 2.0 controller"); 171184610Salfred 172184610Salfred case 0x43451002: 173184610Salfred return "ATI SB200 USB 2.0 controller"; 174184610Salfred case 0x43731002: 175184610Salfred return "ATI SB400 USB 2.0 controller"; 176184610Salfred 177184610Salfred case 0x25ad8086: 178184610Salfred return "Intel 6300ESB USB 2.0 controller"; 179184610Salfred case 0x24cd8086: 180184610Salfred return "Intel 82801DB/L/M (ICH4) USB 2.0 controller"; 181184610Salfred case 0x24dd8086: 182184610Salfred return "Intel 82801EB/R (ICH5) USB 2.0 controller"; 183184610Salfred case 0x265c8086: 184184610Salfred return "Intel 82801FB (ICH6) USB 2.0 controller"; 185184610Salfred case 0x27cc8086: 186184610Salfred return "Intel 82801GB/R (ICH7) USB 2.0 controller"; 187184610Salfred 188184610Salfred case 0x28368086: 189184610Salfred return "Intel 82801H (ICH8) USB 2.0 controller USB2-A"; 190184610Salfred case 0x283a8086: 191184610Salfred return "Intel 82801H (ICH8) USB 2.0 controller USB2-B"; 192184610Salfred case 0x293a8086: 193184610Salfred return "Intel 82801I (ICH9) USB 2.0 controller"; 194184610Salfred case 0x293c8086: 195184610Salfred return "Intel 82801I (ICH9) USB 2.0 controller"; 196200087Sthompsa case 0x3a3a8086: 197200305Sthompsa return "Intel 82801JI (ICH10) USB 2.0 controller USB-A"; 198200087Sthompsa case 0x3a3c8086: 199200305Sthompsa return "Intel 82801JI (ICH10) USB 2.0 controller USB-B"; 200200822Sthompsa case 0x3b348086: 201200822Sthompsa return ("Intel PCH USB 2.0 controller USB-A"); 202200822Sthompsa case 0x3b3c8086: 203200822Sthompsa return ("Intel PCH USB 2.0 controller USB-B"); 204184610Salfred 205184610Salfred case 0x00e01033: 206184610Salfred return ("NEC uPD 720100 USB 2.0 controller"); 207184610Salfred 208184610Salfred case 0x006810de: 209184610Salfred return "NVIDIA nForce2 USB 2.0 controller"; 210184610Salfred case 0x008810de: 211184610Salfred return "NVIDIA nForce2 Ultra 400 USB 2.0 controller"; 212184610Salfred case 0x00d810de: 213184610Salfred return "NVIDIA nForce3 USB 2.0 controller"; 214184610Salfred case 0x00e810de: 215184610Salfred return "NVIDIA nForce3 250 USB 2.0 controller"; 216184610Salfred case 0x005b10de: 217184610Salfred return "NVIDIA nForce4 USB 2.0 controller"; 218195958Salfred case 0x03f210de: 219195958Salfred return "NVIDIA nForce MCP61 USB 2.0 controller"; 220200822Sthompsa case 0x0aa610de: 221200822Sthompsa return "NVIDIA nForce MCP79 USB 2.0 controller"; 222200822Sthompsa case 0x0aa910de: 223200822Sthompsa return "NVIDIA nForce MCP79 USB 2.0 controller"; 224200822Sthompsa case 0x0aaa10de: 225200822Sthompsa return "NVIDIA nForce MCP79 USB 2.0 controller"; 226184610Salfred 227184610Salfred case 0x15621131: 228184610Salfred return "Philips ISP156x USB 2.0 controller"; 229184610Salfred 230184610Salfred case 0x31041106: 231184610Salfred return ("VIA VT6202 USB 2.0 controller"); 232184610Salfred 233184610Salfred default: 234184610Salfred break; 235184610Salfred } 236184610Salfred 237184610Salfred if ((pci_get_class(self) == PCIC_SERIALBUS) 238184610Salfred && (pci_get_subclass(self) == PCIS_SERIALBUS_USB) 239184610Salfred && (pci_get_progif(self) == PCI_INTERFACE_EHCI)) { 240184610Salfred return ("EHCI (generic) USB 2.0 controller"); 241184610Salfred } 242184610Salfred return (NULL); /* dunno */ 243184610Salfred} 244184610Salfred 245184610Salfredstatic int 246184610Salfredehci_pci_probe(device_t self) 247184610Salfred{ 248184610Salfred const char *desc = ehci_pci_match(self); 249184610Salfred 250184610Salfred if (desc) { 251184610Salfred device_set_desc(self, desc); 252184610Salfred return (0); 253184610Salfred } else { 254184610Salfred return (ENXIO); 255184610Salfred } 256184610Salfred} 257184610Salfred 258197554Sthompsastatic void 259197554Sthompsaehci_pci_ati_quirk(device_t self, uint8_t is_sb700) 260197554Sthompsa{ 261197554Sthompsa device_t smbdev; 262197554Sthompsa uint32_t val; 263197554Sthompsa 264197554Sthompsa if (is_sb700) { 265197554Sthompsa /* Lookup SMBUS PCI device */ 266197554Sthompsa smbdev = pci_find_device(PCI_EHCI_VENDORID_ATI, 0x4385); 267197554Sthompsa if (smbdev == NULL) 268197554Sthompsa return; 269197554Sthompsa val = pci_get_revid(smbdev); 270197554Sthompsa if (val != 0x3a && val != 0x3b) 271197554Sthompsa return; 272197554Sthompsa } 273197554Sthompsa 274197554Sthompsa /* 275197554Sthompsa * Note: this bit is described as reserved in SB700 276197554Sthompsa * Register Reference Guide. 277197554Sthompsa */ 278197554Sthompsa val = pci_read_config(self, 0x53, 1); 279197554Sthompsa if (!(val & 0x8)) { 280197554Sthompsa val |= 0x8; 281197554Sthompsa pci_write_config(self, 0x53, val, 1); 282197554Sthompsa device_printf(self, "AMD SB600/700 quirk applied\n"); 283197554Sthompsa } 284197554Sthompsa} 285197554Sthompsa 286197554Sthompsastatic void 287197554Sthompsaehci_pci_via_quirk(device_t self) 288197554Sthompsa{ 289197554Sthompsa uint32_t val; 290197554Sthompsa 291197554Sthompsa if ((pci_get_device(self) == 0x3104) && 292197554Sthompsa ((pci_get_revid(self) & 0xf0) == 0x60)) { 293197554Sthompsa /* Correct schedule sleep time to 10us */ 294197554Sthompsa val = pci_read_config(self, 0x4b, 1); 295197554Sthompsa if (val & 0x20) 296197554Sthompsa return; 297197554Sthompsa pci_write_config(self, 0x4b, val, 1); 298197554Sthompsa device_printf(self, "VIA-quirk applied\n"); 299197554Sthompsa } 300197554Sthompsa} 301197554Sthompsa 302184610Salfredstatic int 303184610Salfredehci_pci_attach(device_t self) 304184610Salfred{ 305184610Salfred ehci_softc_t *sc = device_get_softc(self); 306184610Salfred int err; 307184610Salfred int rid; 308184610Salfred 309187170Sthompsa /* initialise some bus fields */ 310187170Sthompsa sc->sc_bus.parent = self; 311187170Sthompsa sc->sc_bus.devices = sc->sc_devices; 312187170Sthompsa sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 313187170Sthompsa 314184610Salfred /* get all DMA memory */ 315194228Sthompsa if (usb_bus_mem_alloc_all(&sc->sc_bus, 316184610Salfred USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) { 317187170Sthompsa return (ENOMEM); 318184610Salfred } 319184610Salfred 320184610Salfred pci_enable_busmaster(self); 321184610Salfred 322184610Salfred switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) { 323184610Salfred case PCI_USB_REV_PRE_1_0: 324184610Salfred case PCI_USB_REV_1_0: 325184610Salfred case PCI_USB_REV_1_1: 326184610Salfred /* 327184610Salfred * NOTE: some EHCI USB controllers have the wrong USB 328184610Salfred * revision number. It appears those controllers are 329184610Salfred * fully compliant so we just ignore this value in 330184610Salfred * some common cases. 331184610Salfred */ 332184610Salfred device_printf(self, "pre-2.0 USB revision (ignored)\n"); 333184610Salfred /* fallthrough */ 334184610Salfred case PCI_USB_REV_2_0: 335184610Salfred break; 336184610Salfred default: 337187186Sthompsa /* Quirk for Parallels Desktop 4.0 */ 338187186Sthompsa device_printf(self, "USB revision is unknown. Assuming v2.0.\n"); 339199057Sthompsa break; 340184610Salfred } 341184610Salfred 342184610Salfred rid = PCI_CBMEM; 343184610Salfred sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 344184610Salfred RF_ACTIVE); 345184610Salfred if (!sc->sc_io_res) { 346184610Salfred device_printf(self, "Could not map memory\n"); 347184610Salfred goto error; 348184610Salfred } 349184610Salfred sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 350184610Salfred sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 351184610Salfred sc->sc_io_size = rman_get_size(sc->sc_io_res); 352184610Salfred 353184610Salfred rid = 0; 354184610Salfred sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 355184610Salfred RF_SHAREABLE | RF_ACTIVE); 356184610Salfred if (sc->sc_irq_res == NULL) { 357184610Salfred device_printf(self, "Could not allocate irq\n"); 358184610Salfred goto error; 359184610Salfred } 360184610Salfred sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 361184610Salfred if (!sc->sc_bus.bdev) { 362184610Salfred device_printf(self, "Could not add USB device\n"); 363184610Salfred goto error; 364184610Salfred } 365184610Salfred device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 366184610Salfred 367184610Salfred /* 368184610Salfred * ehci_pci_match will never return NULL if ehci_pci_probe 369184610Salfred * succeeded 370184610Salfred */ 371184610Salfred device_set_desc(sc->sc_bus.bdev, ehci_pci_match(self)); 372184610Salfred switch (pci_get_vendor(self)) { 373184610Salfred case PCI_EHCI_VENDORID_ACERLABS: 374184610Salfred sprintf(sc->sc_vendor, "AcerLabs"); 375184610Salfred break; 376184610Salfred case PCI_EHCI_VENDORID_AMD: 377184610Salfred sprintf(sc->sc_vendor, "AMD"); 378184610Salfred break; 379184610Salfred case PCI_EHCI_VENDORID_APPLE: 380184610Salfred sprintf(sc->sc_vendor, "Apple"); 381184610Salfred break; 382184610Salfred case PCI_EHCI_VENDORID_ATI: 383184610Salfred sprintf(sc->sc_vendor, "ATI"); 384184610Salfred break; 385184610Salfred case PCI_EHCI_VENDORID_CMDTECH: 386184610Salfred sprintf(sc->sc_vendor, "CMDTECH"); 387184610Salfred break; 388184610Salfred case PCI_EHCI_VENDORID_INTEL: 389184610Salfred sprintf(sc->sc_vendor, "Intel"); 390184610Salfred break; 391184610Salfred case PCI_EHCI_VENDORID_NEC: 392184610Salfred sprintf(sc->sc_vendor, "NEC"); 393184610Salfred break; 394184610Salfred case PCI_EHCI_VENDORID_OPTI: 395184610Salfred sprintf(sc->sc_vendor, "OPTi"); 396184610Salfred break; 397184610Salfred case PCI_EHCI_VENDORID_PHILIPS: 398184610Salfred sprintf(sc->sc_vendor, "Philips"); 399184610Salfred break; 400184610Salfred case PCI_EHCI_VENDORID_SIS: 401184610Salfred sprintf(sc->sc_vendor, "SiS"); 402184610Salfred break; 403184610Salfred case PCI_EHCI_VENDORID_NVIDIA: 404184610Salfred case PCI_EHCI_VENDORID_NVIDIA2: 405184610Salfred sprintf(sc->sc_vendor, "nVidia"); 406184610Salfred break; 407184610Salfred case PCI_EHCI_VENDORID_VIA: 408184610Salfred sprintf(sc->sc_vendor, "VIA"); 409184610Salfred break; 410184610Salfred default: 411184610Salfred if (bootverbose) 412184610Salfred device_printf(self, "(New EHCI DeviceId=0x%08x)\n", 413184610Salfred pci_get_devid(self)); 414184610Salfred sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self)); 415184610Salfred } 416184610Salfred 417184610Salfred#if (__FreeBSD_version >= 700031) 418184610Salfred err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 419190183Sthompsa NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); 420184610Salfred#else 421184610Salfred err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 422190183Sthompsa (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); 423184610Salfred#endif 424184610Salfred if (err) { 425184610Salfred device_printf(self, "Could not setup irq, %d\n", err); 426184610Salfred sc->sc_intr_hdl = NULL; 427184610Salfred goto error; 428184610Salfred } 429198501Sthompsa ehci_pci_takecontroller(self); 430197554Sthompsa 431197554Sthompsa /* Undocumented quirks taken from Linux */ 432197554Sthompsa 433197554Sthompsa switch (pci_get_vendor(self)) { 434197554Sthompsa case PCI_EHCI_VENDORID_ATI: 435197554Sthompsa /* SB600 and SB700 EHCI quirk */ 436197554Sthompsa switch (pci_get_device(self)) { 437197554Sthompsa case 0x4386: 438197554Sthompsa ehci_pci_ati_quirk(self, 0); 439197554Sthompsa break; 440197554Sthompsa case 0x4396: 441197554Sthompsa ehci_pci_ati_quirk(self, 1); 442197554Sthompsa break; 443197554Sthompsa default: 444197554Sthompsa break; 445197554Sthompsa } 446197554Sthompsa break; 447197554Sthompsa 448197554Sthompsa case PCI_EHCI_VENDORID_VIA: 449197554Sthompsa ehci_pci_via_quirk(self); 450197554Sthompsa break; 451197554Sthompsa 452197554Sthompsa default: 453197554Sthompsa break; 454197554Sthompsa } 455197554Sthompsa 456199058Sthompsa /* Dropped interrupts workaround */ 457199058Sthompsa switch (pci_get_vendor(self)) { 458199058Sthompsa case PCI_EHCI_VENDORID_ATI: 459199058Sthompsa case PCI_EHCI_VENDORID_VIA: 460199058Sthompsa sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG; 461199058Sthompsa if (bootverbose) 462199058Sthompsa device_printf(self, 463199058Sthompsa "Dropped interrupts workaround enabled\n"); 464199058Sthompsa break; 465199058Sthompsa default: 466199058Sthompsa break; 467199058Sthompsa } 468199058Sthompsa 469203693Sthompsa /* Doorbell feature workaround */ 470203693Sthompsa switch (pci_get_vendor(self)) { 471203693Sthompsa case PCI_EHCI_VENDORID_NVIDIA: 472203693Sthompsa case PCI_EHCI_VENDORID_NVIDIA2: 473203693Sthompsa sc->sc_flags |= EHCI_SCFLG_IAADBUG; 474203693Sthompsa if (bootverbose) 475203693Sthompsa device_printf(self, 476203693Sthompsa "Doorbell workaround enabled\n"); 477203693Sthompsa break; 478203693Sthompsa default: 479203693Sthompsa break; 480203693Sthompsa } 481203693Sthompsa 482184610Salfred err = ehci_init(sc); 483184610Salfred if (!err) { 484184610Salfred err = device_probe_and_attach(sc->sc_bus.bdev); 485184610Salfred } 486184610Salfred if (err) { 487184610Salfred device_printf(self, "USB init failed err=%d\n", err); 488184610Salfred goto error; 489184610Salfred } 490184610Salfred return (0); 491184610Salfred 492184610Salfrederror: 493184610Salfred ehci_pci_detach(self); 494184610Salfred return (ENXIO); 495184610Salfred} 496184610Salfred 497184610Salfredstatic int 498184610Salfredehci_pci_detach(device_t self) 499184610Salfred{ 500184610Salfred ehci_softc_t *sc = device_get_softc(self); 501184610Salfred device_t bdev; 502184610Salfred 503184610Salfred if (sc->sc_bus.bdev) { 504184610Salfred bdev = sc->sc_bus.bdev; 505184610Salfred device_detach(bdev); 506184610Salfred device_delete_child(self, bdev); 507184610Salfred } 508184610Salfred /* during module unload there are lots of children leftover */ 509184610Salfred device_delete_all_children(self); 510184610Salfred 511184610Salfred pci_disable_busmaster(self); 512184610Salfred 513184610Salfred /* 514184610Salfred * disable interrupts that might have been switched on in ehci_init 515184610Salfred */ 516184610Salfred if (sc->sc_io_res) { 517184610Salfred EWRITE4(sc, EHCI_USBINTR, 0); 518184610Salfred } 519184610Salfred if (sc->sc_irq_res && sc->sc_intr_hdl) { 520184610Salfred /* 521184610Salfred * only call ehci_detach() after ehci_init() 522184610Salfred */ 523184610Salfred ehci_detach(sc); 524184610Salfred 525184610Salfred int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 526184610Salfred 527184610Salfred if (err) 528184610Salfred /* XXX or should we panic? */ 529184610Salfred device_printf(self, "Could not tear down irq, %d\n", 530184610Salfred err); 531184610Salfred sc->sc_intr_hdl = NULL; 532184610Salfred } 533184610Salfred if (sc->sc_irq_res) { 534184610Salfred bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 535184610Salfred sc->sc_irq_res = NULL; 536184610Salfred } 537184610Salfred if (sc->sc_io_res) { 538184610Salfred bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, 539184610Salfred sc->sc_io_res); 540184610Salfred sc->sc_io_res = NULL; 541184610Salfred } 542194228Sthompsa usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); 543184610Salfred 544184610Salfred return (0); 545184610Salfred} 546184610Salfred 547198501Sthompsastatic void 548198501Sthompsaehci_pci_takecontroller(device_t self) 549198501Sthompsa{ 550198501Sthompsa ehci_softc_t *sc = device_get_softc(self); 551198501Sthompsa uint32_t cparams; 552198501Sthompsa uint32_t eec; 553198501Sthompsa uint16_t to; 554198501Sthompsa uint8_t eecp; 555198501Sthompsa uint8_t bios_sem; 556198501Sthompsa 557198501Sthompsa cparams = EREAD4(sc, EHCI_HCCPARAMS); 558198501Sthompsa 559198501Sthompsa /* Synchronise with the BIOS if it owns the controller. */ 560198501Sthompsa for (eecp = EHCI_HCC_EECP(cparams); eecp != 0; 561198501Sthompsa eecp = EHCI_EECP_NEXT(eec)) { 562198501Sthompsa eec = pci_read_config(self, eecp, 4); 563198501Sthompsa if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) { 564198501Sthompsa continue; 565198501Sthompsa } 566198501Sthompsa bios_sem = pci_read_config(self, eecp + 567198501Sthompsa EHCI_LEGSUP_BIOS_SEM, 1); 568198501Sthompsa if (bios_sem == 0) { 569198501Sthompsa continue; 570198501Sthompsa } 571198501Sthompsa device_printf(sc->sc_bus.bdev, "waiting for BIOS " 572198501Sthompsa "to give up control\n"); 573198501Sthompsa pci_write_config(self, eecp + 574198501Sthompsa EHCI_LEGSUP_OS_SEM, 1, 1); 575198501Sthompsa to = 500; 576198501Sthompsa while (1) { 577198501Sthompsa bios_sem = pci_read_config(self, eecp + 578198501Sthompsa EHCI_LEGSUP_BIOS_SEM, 1); 579198501Sthompsa if (bios_sem == 0) 580198501Sthompsa break; 581198501Sthompsa 582198501Sthompsa if (--to == 0) { 583198501Sthompsa device_printf(sc->sc_bus.bdev, 584198501Sthompsa "timed out waiting for BIOS\n"); 585198501Sthompsa break; 586198501Sthompsa } 587198501Sthompsa usb_pause_mtx(NULL, hz / 100); /* wait 10ms */ 588198501Sthompsa } 589198501Sthompsa } 590198501Sthompsa} 591198501Sthompsa 592184610Salfredstatic driver_t ehci_driver = 593184610Salfred{ 594184610Salfred .name = "ehci", 595184610Salfred .methods = (device_method_t[]){ 596184610Salfred /* device interface */ 597184610Salfred DEVMETHOD(device_probe, ehci_pci_probe), 598184610Salfred DEVMETHOD(device_attach, ehci_pci_attach), 599184610Salfred DEVMETHOD(device_detach, ehci_pci_detach), 600184610Salfred DEVMETHOD(device_suspend, ehci_pci_suspend), 601184610Salfred DEVMETHOD(device_resume, ehci_pci_resume), 602184610Salfred DEVMETHOD(device_shutdown, ehci_pci_shutdown), 603184610Salfred /* bus interface */ 604184610Salfred DEVMETHOD(bus_print_child, bus_generic_print_child), 605184610Salfred 606184610Salfred {0, 0} 607184610Salfred }, 608184610Salfred .size = sizeof(struct ehci_softc), 609184610Salfred}; 610184610Salfred 611184610Salfredstatic devclass_t ehci_devclass; 612184610Salfred 613184610SalfredDRIVER_MODULE(ehci, pci, ehci_driver, ehci_devclass, 0, 0); 614188942SthompsaMODULE_DEPEND(ehci, usb, 1, 1, 1); 615