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