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