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 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 /* Universal Host Controller Interface 35 * 36 * UHCI spec: http://www.intel.com/ 37 */ 38 39 /* The low level controller code for UHCI has been split into 40 * PCI probes and UHCI specific code. This was done to facilitate the 41 * sharing of code between *BSD's 42 */ 43 44 #include <sys/stdint.h> 45 #include <sys/stddef.h> 46 #include <sys/param.h> 47 #include <sys/queue.h> 48 #include <sys/types.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/bus.h> 52 #include <sys/module.h> 53 #include <sys/lock.h> 54 #include <sys/mutex.h> 55 #include <sys/condvar.h> 56 #include <sys/sysctl.h> 57 #include <sys/sx.h> 58 #include <sys/unistd.h> 59 #include <sys/callout.h> 60 #include <sys/malloc.h> 61 #include <sys/priv.h> 62 63 #include <dev/usb/usb.h> 64 #include <dev/usb/usbdi.h> 65 66 #include <dev/usb/usb_core.h> 67 #include <dev/usb/usb_busdma.h> 68 #include <dev/usb/usb_process.h> 69 #include <dev/usb/usb_util.h> 70 #include <dev/usb/usb_debug.h> 71 72 #include <dev/usb/usb_controller.h> 73 #include <dev/usb/usb_bus.h> 74 #include <dev/usb/usb_pci.h> 75 #include <dev/usb/controller/uhci.h> 76 #include <dev/usb/controller/uhcireg.h> 77 78 #define PCI_UHCI_VENDORID_INTEL 0x8086 79 #define PCI_UHCI_VENDORID_VIA 0x1106 80 81 /* PIIX4E has no separate stepping */ 82 83 static device_probe_t uhci_pci_probe; 84 static device_attach_t uhci_pci_attach; 85 static device_detach_t uhci_pci_detach; 86 static device_suspend_t uhci_pci_suspend; 87 static device_resume_t uhci_pci_resume; 88 89 static int 90 uhci_pci_suspend(device_t self) 91 { 92 uhci_softc_t *sc = device_get_softc(self); 93 int err; 94 95 err = bus_generic_suspend(self); 96 if (err) { 97 return (err); 98 } 99 uhci_suspend(sc); 100 return (0); 101 } 102 103 static int 104 uhci_pci_resume(device_t self) 105 { 106 uhci_softc_t *sc = device_get_softc(self); 107 108 pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2); 109 110 uhci_resume(sc); 111 112 bus_generic_resume(self); 113 return (0); 114 } 115 116 static const char * 117 uhci_pci_match(device_t self) 118 { 119 uint32_t device_id = pci_get_devid(self); 120 121 switch (device_id) { 122 case 0x26888086: 123 return ("Intel 631XESB/632XESB/3100 USB controller USB-1"); 124 125 case 0x26898086: 126 return ("Intel 631XESB/632XESB/3100 USB controller USB-2"); 127 128 case 0x268a8086: 129 return ("Intel 631XESB/632XESB/3100 USB controller USB-3"); 130 131 case 0x268b8086: 132 return ("Intel 631XESB/632XESB/3100 USB controller USB-4"); 133 134 case 0x70208086: 135 return ("Intel 82371SB (PIIX3) USB controller"); 136 137 case 0x71128086: 138 return ("Intel 82371AB/EB (PIIX4) USB controller"); 139 140 case 0x24128086: 141 return ("Intel 82801AA (ICH) USB controller"); 142 143 case 0x24228086: 144 return ("Intel 82801AB (ICH0) USB controller"); 145 146 case 0x24428086: 147 return ("Intel 82801BA/BAM (ICH2) USB controller USB-A"); 148 149 case 0x24448086: 150 return ("Intel 82801BA/BAM (ICH2) USB controller USB-B"); 151 152 case 0x24828086: 153 return ("Intel 82801CA/CAM (ICH3) USB controller USB-A"); 154 155 case 0x24848086: 156 return ("Intel 82801CA/CAM (ICH3) USB controller USB-B"); 157 158 case 0x24878086: 159 return ("Intel 82801CA/CAM (ICH3) USB controller USB-C"); 160 161 case 0x24c28086: 162 return ("Intel 82801DB (ICH4) USB controller USB-A"); 163 164 case 0x24c48086: 165 return ("Intel 82801DB (ICH4) USB controller USB-B"); 166 167 case 0x24c78086: 168 return ("Intel 82801DB (ICH4) USB controller USB-C"); 169 170 case 0x24d28086: 171 return ("Intel 82801EB (ICH5) USB controller USB-A"); 172 173 case 0x24d48086: 174 return ("Intel 82801EB (ICH5) USB controller USB-B"); 175 176 case 0x24d78086: 177 return ("Intel 82801EB (ICH5) USB controller USB-C"); 178 179 case 0x24de8086: 180 return ("Intel 82801EB (ICH5) USB controller USB-D"); 181 182 case 0x26588086: 183 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-A"); 184 185 case 0x26598086: 186 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-B"); 187 188 case 0x265a8086: 189 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-C"); 190 191 case 0x265b8086: 192 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-D"); 193 194 case 0x27c88086: 195 return ("Intel 82801G (ICH7) USB controller USB-A"); 196 case 0x27c98086: 197 return ("Intel 82801G (ICH7) USB controller USB-B"); 198 case 0x27ca8086: 199 return ("Intel 82801G (ICH7) USB controller USB-C"); 200 case 0x27cb8086: 201 return ("Intel 82801G (ICH7) USB controller USB-D"); 202 203 case 0x28308086: 204 return ("Intel 82801H (ICH8) USB controller USB-A"); 205 case 0x28318086: 206 return ("Intel 82801H (ICH8) USB controller USB-B"); 207 case 0x28328086: 208 return ("Intel 82801H (ICH8) USB controller USB-C"); 209 case 0x28348086: 210 return ("Intel 82801H (ICH8) USB controller USB-D"); 211 case 0x28358086: 212 return ("Intel 82801H (ICH8) USB controller USB-E"); 213 case 0x29348086: 214 return ("Intel 82801I (ICH9) USB controller"); 215 case 0x29358086: 216 return ("Intel 82801I (ICH9) USB controller"); 217 case 0x29368086: 218 return ("Intel 82801I (ICH9) USB controller"); 219 case 0x29378086: 220 return ("Intel 82801I (ICH9) USB controller"); 221 case 0x29388086: 222 return ("Intel 82801I (ICH9) USB controller"); 223 case 0x29398086: 224 return ("Intel 82801I (ICH9) USB controller"); 225 case 0x3a348086: 226 return ("Intel 82801JI (ICH10) USB controller USB-A"); 227 case 0x3a358086: 228 return ("Intel 82801JI (ICH10) USB controller USB-B"); 229 case 0x3a368086: 230 return ("Intel 82801JI (ICH10) USB controller USB-C"); 231 case 0x3a378086: 232 return ("Intel 82801JI (ICH10) USB controller USB-D"); 233 case 0x3a388086: 234 return ("Intel 82801JI (ICH10) USB controller USB-E"); 235 case 0x3a398086: 236 return ("Intel 82801JI (ICH10) USB controller USB-F"); 237 238 case 0x719a8086: 239 return ("Intel 82443MX USB controller"); 240 241 case 0x76028086: 242 return ("Intel 82372FB/82468GX USB controller"); 243 244 case 0x30381106: 245 return ("VIA 83C572 USB controller"); 246 247 default: 248 break; 249 } 250 251 if ((pci_get_class(self) == PCIC_SERIALBUS) && 252 (pci_get_subclass(self) == PCIS_SERIALBUS_USB) && 253 (pci_get_progif(self) == PCI_INTERFACE_UHCI)) { 254 return ("UHCI (generic) USB controller"); 255 } 256 return (NULL); 257 } 258 259 static int 260 uhci_pci_probe(device_t self) 261 { 262 const char *desc = uhci_pci_match(self); 263 264 if (desc) { 265 device_set_desc(self, desc); 266 return (0); 267 } else { 268 return (ENXIO); 269 } 270 } 271 272 static int 273 uhci_pci_attach(device_t self) 274 { 275 uhci_softc_t *sc = device_get_softc(self); 276 int rid; 277 int err; 278 279 /* initialise some bus fields */ 280 sc->sc_bus.parent = self; 281 sc->sc_bus.devices = sc->sc_devices; 282 sc->sc_bus.devices_max = UHCI_MAX_DEVICES; 283 284 /* get all DMA memory */ 285 if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self), 286 &uhci_iterate_hw_softc)) { 287 return ENOMEM; 288 } 289 sc->sc_dev = self; 290 291 pci_enable_busmaster(self); 292 293 rid = PCI_UHCI_BASE_REG; 294 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid, 295 RF_ACTIVE); 296 if (!sc->sc_io_res) { 297 device_printf(self, "Could not map ports\n"); 298 goto error; 299 } 300 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 301 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 302 sc->sc_io_size = rman_get_size(sc->sc_io_res); 303 304 /* disable interrupts */ 305 bus_space_write_2(sc->sc_io_tag, sc->sc_io_hdl, UHCI_INTR, 0); 306 307 rid = 0; 308 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 309 RF_SHAREABLE | RF_ACTIVE); 310 if (sc->sc_irq_res == NULL) { 311 device_printf(self, "Could not allocate irq\n"); 312 goto error; 313 } 314 sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 315 if (!sc->sc_bus.bdev) { 316 device_printf(self, "Could not add USB device\n"); 317 goto error; 318 } 319 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 320 321 /* 322 * uhci_pci_match must never return NULL if uhci_pci_probe 323 * succeeded 324 */ 325 device_set_desc(sc->sc_bus.bdev, uhci_pci_match(self)); 326 switch (pci_get_vendor(self)) { 327 case PCI_UHCI_VENDORID_INTEL: 328 sprintf(sc->sc_vendor, "Intel"); 329 break; 330 case PCI_UHCI_VENDORID_VIA: 331 sprintf(sc->sc_vendor, "VIA"); 332 break; 333 default: 334 if (bootverbose) { 335 device_printf(self, "(New UHCI DeviceId=0x%08x)\n", 336 pci_get_devid(self)); 337 } 338 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self)); 339 } 340 341 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) { 342 case PCI_USB_REV_PRE_1_0: 343 sc->sc_bus.usbrev = USB_REV_PRE_1_0; 344 break; 345 case PCI_USB_REV_1_0: 346 sc->sc_bus.usbrev = USB_REV_1_0; 347 break; 348 default: 349 /* Quirk for Parallels Desktop 4.0 */ 350 device_printf(self, "USB revision is unknown. Assuming v1.1.\n"); 351 sc->sc_bus.usbrev = USB_REV_1_1; 352 break; 353 } 354 355 #if (__FreeBSD_version >= 700031) 356 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 357 NULL, (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl); 358 #else 359 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 360 (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl); 361 #endif 362 363 if (err) { 364 device_printf(self, "Could not setup irq, %d\n", err); 365 sc->sc_intr_hdl = NULL; 366 goto error; 367 } 368 /* 369 * Set the PIRQD enable bit and switch off all the others. We don't 370 * want legacy support to interfere with us XXX Does this also mean 371 * that the BIOS won't touch the keyboard anymore if it is connected 372 * to the ports of the root hub? 373 */ 374 #ifdef USB_DEBUG 375 if (pci_read_config(self, PCI_LEGSUP, 2) != PCI_LEGSUP_USBPIRQDEN) { 376 device_printf(self, "LegSup = 0x%04x\n", 377 pci_read_config(self, PCI_LEGSUP, 2)); 378 } 379 #endif 380 pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2); 381 382 err = uhci_init(sc); 383 if (!err) { 384 err = device_probe_and_attach(sc->sc_bus.bdev); 385 } 386 if (err) { 387 device_printf(self, "USB init failed\n"); 388 goto error; 389 } 390 return (0); 391 392 error: 393 uhci_pci_detach(self); 394 return (ENXIO); 395 } 396 397 int 398 uhci_pci_detach(device_t self) 399 { 400 uhci_softc_t *sc = device_get_softc(self); 401 device_t bdev; 402 403 if (sc->sc_bus.bdev) { 404 bdev = sc->sc_bus.bdev; 405 device_detach(bdev); 406 device_delete_child(self, bdev); 407 } 408 /* during module unload there are lots of children leftover */ 409 device_delete_all_children(self); 410 411 /* 412 * disable interrupts that might have been switched on in 413 * uhci_init. 414 */ 415 if (sc->sc_io_res) { 416 USB_BUS_LOCK(&sc->sc_bus); 417 418 /* stop the controller */ 419 uhci_reset(sc); 420 421 USB_BUS_UNLOCK(&sc->sc_bus); 422 } 423 pci_disable_busmaster(self); 424 425 if (sc->sc_irq_res && sc->sc_intr_hdl) { 426 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 427 428 if (err) { 429 /* XXX or should we panic? */ 430 device_printf(self, "Could not tear down irq, %d\n", 431 err); 432 } 433 sc->sc_intr_hdl = NULL; 434 } 435 if (sc->sc_irq_res) { 436 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 437 sc->sc_irq_res = NULL; 438 } 439 if (sc->sc_io_res) { 440 bus_release_resource(self, SYS_RES_IOPORT, PCI_UHCI_BASE_REG, 441 sc->sc_io_res); 442 sc->sc_io_res = NULL; 443 } 444 usb_bus_mem_free_all(&sc->sc_bus, &uhci_iterate_hw_softc); 445 446 return (0); 447 } 448 449 static driver_t uhci_driver = 450 { 451 .name = "uhci", 452 .methods = (device_method_t[]){ 453 /* device interface */ 454 DEVMETHOD(device_probe, uhci_pci_probe), 455 DEVMETHOD(device_attach, uhci_pci_attach), 456 DEVMETHOD(device_detach, uhci_pci_detach), 457 458 DEVMETHOD(device_suspend, uhci_pci_suspend), 459 DEVMETHOD(device_resume, uhci_pci_resume), 460 DEVMETHOD(device_shutdown, bus_generic_shutdown), 461 462 /* Bus interface */ 463 DEVMETHOD(bus_print_child, bus_generic_print_child), 464 {0, 0} 465 }, 466 .size = sizeof(struct uhci_softc), 467 }; 468 469 static devclass_t uhci_devclass; 470 471 DRIVER_MODULE(uhci, pci, uhci_driver, uhci_devclass, 0, 0); 472 MODULE_DEPEND(uhci, usb, 1, 1, 1); 473