uhci_pci.c revision 187186
1204431Sraj/*- 2204431Sraj * Copyright (c) 1998 The NetBSD Foundation, Inc. 3204431Sraj * All rights reserved. 4204431Sraj * 5204431Sraj * This code is derived from software contributed to The NetBSD Foundation 6204431Sraj * by Lennart Augustsson (augustss@carlstedt.se) at 7204431Sraj * Carlstedt Research & Technology. 8204431Sraj * 9204431Sraj * Redistribution and use in source and binary forms, with or without 10204431Sraj * modification, are permitted provided that the following conditions 11204431Sraj * are met: 12204431Sraj * 1. Redistributions of source code must retain the above copyright 13204431Sraj * notice, this list of conditions and the following disclaimer. 14204431Sraj * 2. Redistributions in binary form must reproduce the above copyright 15204431Sraj * notice, this list of conditions and the following disclaimer in the 16204431Sraj * documentation and/or other materials provided with the distribution. 17204431Sraj * 3. All advertising materials mentioning features or use of this software 18204431Sraj * must display the following acknowledgement: 19204431Sraj * This product includes software developed by the NetBSD 20204431Sraj * Foundation, Inc. and its contributors. 21204431Sraj * 4. Neither the name of The NetBSD Foundation nor the names of its 22204431Sraj * contributors may be used to endorse or promote products derived 23204431Sraj * 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: head/sys/dev/usb2/controller/uhci2_pci.c 187186 2009-01-13 19:14:51Z thompsa $"); 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/usb2/include/usb2_mfunc.h> 52#include <dev/usb2/include/usb2_defs.h> 53#include <dev/usb2/include/usb2_standard.h> 54 55#include <dev/usb2/core/usb2_core.h> 56#include <dev/usb2/core/usb2_busdma.h> 57#include <dev/usb2/core/usb2_process.h> 58#include <dev/usb2/core/usb2_sw_transfer.h> 59#include <dev/usb2/core/usb2_util.h> 60#include <dev/usb2/core/usb2_debug.h> 61 62#include <dev/usb2/controller/usb2_controller.h> 63#include <dev/usb2/controller/usb2_bus.h> 64#include <dev/usb2/controller/usb2_pci.h> 65#include <dev/usb2/controller/uhci2.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 74static device_probe_t uhci_pci_probe; 75static device_attach_t uhci_pci_attach; 76static device_detach_t uhci_pci_detach; 77static device_suspend_t uhci_pci_suspend; 78static device_resume_t uhci_pci_resume; 79 80static int 81uhci_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 94static int 95uhci_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 107static const char * 108uhci_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 229static int 230uhci_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 242static int 243uhci_pci_attach(device_t self) 244{ 245 uhci_softc_t *sc = device_get_softc(self); 246 int rid; 247 int err; 248 249 if (sc == NULL) { 250 device_printf(self, "Could not allocate sc\n"); 251 return (ENXIO); 252 } 253 /* initialise some bus fields */ 254 sc->sc_bus.parent = self; 255 sc->sc_bus.devices = sc->sc_devices; 256 sc->sc_bus.devices_max = UHCI_MAX_DEVICES; 257 258 /* get all DMA memory */ 259 if (usb2_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self), 260 &uhci_iterate_hw_softc)) { 261 return ENOMEM; 262 } 263 sc->sc_dev = self; 264 265 pci_enable_busmaster(self); 266 267 rid = PCI_UHCI_BASE_REG; 268 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid, 269 RF_ACTIVE); 270 if (!sc->sc_io_res) { 271 device_printf(self, "Could not map ports\n"); 272 goto error; 273 } 274 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 275 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 276 sc->sc_io_size = rman_get_size(sc->sc_io_res); 277 278 /* disable interrupts */ 279 bus_space_write_2(sc->sc_io_tag, sc->sc_io_hdl, UHCI_INTR, 0); 280 281 rid = 0; 282 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 283 RF_SHAREABLE | RF_ACTIVE); 284 if (sc->sc_irq_res == NULL) { 285 device_printf(self, "Could not allocate irq\n"); 286 goto error; 287 } 288 sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 289 if (!sc->sc_bus.bdev) { 290 device_printf(self, "Could not add USB device\n"); 291 goto error; 292 } 293 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 294 295 /* 296 * uhci_pci_match must never return NULL if uhci_pci_probe 297 * succeeded 298 */ 299 device_set_desc(sc->sc_bus.bdev, uhci_pci_match(self)); 300 switch (pci_get_vendor(self)) { 301 case PCI_UHCI_VENDORID_INTEL: 302 sprintf(sc->sc_vendor, "Intel"); 303 break; 304 case PCI_UHCI_VENDORID_VIA: 305 sprintf(sc->sc_vendor, "VIA"); 306 break; 307 default: 308 if (bootverbose) { 309 device_printf(self, "(New UHCI DeviceId=0x%08x)\n", 310 pci_get_devid(self)); 311 } 312 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self)); 313 } 314 315 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) { 316 case PCI_USB_REV_PRE_1_0: 317 sc->sc_bus.usbrev = USB_REV_PRE_1_0; 318 break; 319 case PCI_USB_REV_1_0: 320 sc->sc_bus.usbrev = USB_REV_1_0; 321 break; 322 default: 323 /* Quirk for Parallels Desktop 4.0 */ 324 device_printf(self, "USB revision is unknown. Assuming v1.1.\n"); 325 sc->sc_bus.usbrev = USB_REV_1_1; 326 break; 327 } 328 329#if (__FreeBSD_version >= 700031) 330 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 331 NULL, (void *)(void *)uhci_interrupt, sc, &sc->sc_intr_hdl); 332#else 333 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 334 (void *)(void *)uhci_interrupt, sc, &sc->sc_intr_hdl); 335#endif 336 337 if (err) { 338 device_printf(self, "Could not setup irq, %d\n", err); 339 sc->sc_intr_hdl = NULL; 340 goto error; 341 } 342 /* 343 * Set the PIRQD enable bit and switch off all the others. We don't 344 * want legacy support to interfere with us XXX Does this also mean 345 * that the BIOS won't touch the keyboard anymore if it is connected 346 * to the ports of the root hub? 347 */ 348#if USB_DEBUG 349 if (pci_read_config(self, PCI_LEGSUP, 2) != PCI_LEGSUP_USBPIRQDEN) { 350 device_printf(self, "LegSup = 0x%04x\n", 351 pci_read_config(self, PCI_LEGSUP, 2)); 352 } 353#endif 354 pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2); 355 356 err = uhci_init(sc); 357 if (!err) { 358 err = device_probe_and_attach(sc->sc_bus.bdev); 359 } 360 if (err) { 361 device_printf(self, "USB init failed\n"); 362 goto error; 363 } 364 return (0); 365 366error: 367 uhci_pci_detach(self); 368 return (ENXIO); 369} 370 371int 372uhci_pci_detach(device_t self) 373{ 374 uhci_softc_t *sc = device_get_softc(self); 375 device_t bdev; 376 377 if (sc->sc_bus.bdev) { 378 bdev = sc->sc_bus.bdev; 379 device_detach(bdev); 380 device_delete_child(self, bdev); 381 } 382 /* during module unload there are lots of children leftover */ 383 device_delete_all_children(self); 384 385 /* 386 * disable interrupts that might have been switched on in 387 * uhci_init. 388 */ 389 if (sc->sc_io_res) { 390 USB_BUS_LOCK(&sc->sc_bus); 391 392 /* stop the controller */ 393 uhci_reset(sc); 394 395 USB_BUS_UNLOCK(&sc->sc_bus); 396 } 397 pci_disable_busmaster(self); 398 399 if (sc->sc_irq_res && sc->sc_intr_hdl) { 400 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 401 402 if (err) { 403 /* XXX or should we panic? */ 404 device_printf(self, "Could not tear down irq, %d\n", 405 err); 406 } 407 sc->sc_intr_hdl = NULL; 408 } 409 if (sc->sc_irq_res) { 410 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 411 sc->sc_irq_res = NULL; 412 } 413 if (sc->sc_io_res) { 414 bus_release_resource(self, SYS_RES_IOPORT, PCI_UHCI_BASE_REG, 415 sc->sc_io_res); 416 sc->sc_io_res = NULL; 417 } 418 usb2_bus_mem_free_all(&sc->sc_bus, &uhci_iterate_hw_softc); 419 420 return (0); 421} 422 423static driver_t uhci_driver = 424{ 425 .name = "uhci", 426 .methods = (device_method_t[]){ 427 /* device interface */ 428 DEVMETHOD(device_probe, uhci_pci_probe), 429 DEVMETHOD(device_attach, uhci_pci_attach), 430 DEVMETHOD(device_detach, uhci_pci_detach), 431 432 DEVMETHOD(device_suspend, uhci_pci_suspend), 433 DEVMETHOD(device_resume, uhci_pci_resume), 434 DEVMETHOD(device_shutdown, bus_generic_shutdown), 435 436 /* Bus interface */ 437 DEVMETHOD(bus_print_child, bus_generic_print_child), 438 {0, 0} 439 }, 440 .size = sizeof(struct uhci_softc), 441}; 442 443static devclass_t uhci_devclass; 444 445DRIVER_MODULE(uhci, pci, uhci_driver, uhci_devclass, 0, 0); 446DRIVER_MODULE(uhci, cardbus, uhci_driver, uhci_devclass, 0, 0); 447MODULE_DEPEND(uhci, usb2_controller, 1, 1, 1); 448MODULE_DEPEND(uhci, usb2_core, 1, 1, 1); 449