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 /* 42 * USB Open Host Controller driver. 43 * 44 * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf 45 */ 46 47 /* The low level controller code for OHCI has been split into 48 * PCI probes and OHCI specific code. This was done to facilitate the 49 * sharing of code between *BSD's 50 */ 51 52 #include <dev/usb/usb.h> 53 #include <dev/usb/usb_mfunc.h> 54 #include <dev/usb/usb_error.h> 55 #include <dev/usb/usb_defs.h> 56 57 #include <dev/usb/usb_core.h> 58 #include <dev/usb/usb_busdma.h> 59 #include <dev/usb/usb_process.h> 60 #include <dev/usb/usb_sw_transfer.h> 61 #include <dev/usb/usb_util.h> 62 63 #include <dev/usb/usb_controller.h> 64 #include <dev/usb/usb_bus.h> 65 #include <dev/usb/usb_pci.h> 66 #include <dev/usb/controller/ohci.h> 67 68 #define PCI_OHCI_VENDORID_ACERLABS 0x10b9 69 #define PCI_OHCI_VENDORID_AMD 0x1022 70 #define PCI_OHCI_VENDORID_APPLE 0x106b 71 #define PCI_OHCI_VENDORID_ATI 0x1002 72 #define PCI_OHCI_VENDORID_CMDTECH 0x1095 73 #define PCI_OHCI_VENDORID_NEC 0x1033 74 #define PCI_OHCI_VENDORID_NVIDIA 0x12D2 75 #define PCI_OHCI_VENDORID_NVIDIA2 0x10DE 76 #define PCI_OHCI_VENDORID_OPTI 0x1045 77 #define PCI_OHCI_VENDORID_SIS 0x1039 78 #define PCI_OHCI_VENDORID_SUN 0x108e 79 80 #define PCI_OHCI_BASE_REG 0x10 81 82 static device_probe_t ohci_pci_probe; 83 static device_attach_t ohci_pci_attach; 84 static device_detach_t ohci_pci_detach; 85 static device_suspend_t ohci_pci_suspend; 86 static device_resume_t ohci_pci_resume; 87 88 static int 89 ohci_pci_suspend(device_t self) 90 { 91 ohci_softc_t *sc = device_get_softc(self); 92 int err; 93 94 err = bus_generic_suspend(self); 95 if (err) { 96 return (err); 97 } 98 ohci_suspend(sc); 99 return (0); 100 } 101 102 static int 103 ohci_pci_resume(device_t self) 104 { 105 ohci_softc_t *sc = device_get_softc(self); 106 uint32_t reg, int_line; 107 108 if (pci_get_powerstate(self) != PCI_POWERSTATE_D0) { 109 device_printf(self, "chip is in D%d mode " 110 "-- setting to D0\n", pci_get_powerstate(self)); 111 reg = pci_read_config(self, PCI_CBMEM, 4); 112 int_line = pci_read_config(self, PCIR_INTLINE, 4); 113 pci_set_powerstate(self, PCI_POWERSTATE_D0); 114 pci_write_config(self, PCI_CBMEM, reg, 4); 115 pci_write_config(self, PCIR_INTLINE, int_line, 4); 116 } 117 ohci_resume(sc); 118 119 bus_generic_resume(self); 120 return (0); 121 } 122 123 static const char * 124 ohci_pci_match(device_t self) 125 { 126 uint32_t device_id = pci_get_devid(self); 127 128 switch (device_id) { 129 case 0x523710b9: 130 return ("AcerLabs M5237 (Aladdin-V) USB controller"); 131 132 case 0x740c1022: 133 return ("AMD-756 USB Controller"); 134 135 case 0x74141022: 136 return ("AMD-766 USB Controller"); 137 138 case 0x43741002: 139 return "ATI SB400 USB Controller"; 140 case 0x43751002: 141 return "ATI SB400 USB Controller"; 142 143 case 0x06701095: 144 return ("CMD Tech 670 (USB0670) USB controller"); 145 146 case 0x06731095: 147 return ("CMD Tech 673 (USB0673) USB controller"); 148 149 case 0xc8611045: 150 return ("OPTi 82C861 (FireLink) USB controller"); 151 152 case 0x00351033: 153 return ("NEC uPD 9210 USB controller"); 154 155 case 0x00d710de: 156 return ("nVidia nForce3 USB Controller"); 157 158 case 0x70011039: 159 return ("SiS 5571 USB controller"); 160 161 case 0x1103108e: 162 return "Sun PCIO-2 USB controller"; 163 164 case 0x0019106b: 165 return ("Apple KeyLargo USB controller"); 166 167 default: 168 break; 169 } 170 if ((pci_get_class(self) == PCIC_SERIALBUS) && 171 (pci_get_subclass(self) == PCIS_SERIALBUS_USB) && 172 (pci_get_progif(self) == PCI_INTERFACE_OHCI)) { 173 return ("OHCI (generic) USB controller"); 174 } 175 return (NULL); 176 } 177 178 static int 179 ohci_pci_probe(device_t self) 180 { 181 const char *desc = ohci_pci_match(self); 182 183 if (desc) { 184 device_set_desc(self, desc); 185 return (0); 186 } else { 187 return (ENXIO); 188 } 189 } 190 191 static int 192 ohci_pci_attach(device_t self) 193 { 194 ohci_softc_t *sc = device_get_softc(self); 195 int rid; 196 int err; 197 198 /* initialise some bus fields */ 199 sc->sc_bus.parent = self; 200 sc->sc_bus.devices = sc->sc_devices; 201 sc->sc_bus.devices_max = OHCI_MAX_DEVICES; 202 203 /* get all DMA memory */ 204 if (usb2_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self), 205 &ohci_iterate_hw_softc)) { 206 return (ENOMEM); 207 } 208 sc->sc_dev = self; 209 210 pci_enable_busmaster(self); 211 212 /* 213 * Some Sun PCIO-2 USB controllers have their intpin register 214 * bogusly set to 0, although it should be 4. Correct that. 215 */ 216 if (pci_get_devid(self) == 0x1103108e && pci_get_intpin(self) == 0) 217 pci_set_intpin(self, 4); 218 219 rid = PCI_CBMEM; 220 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 221 RF_ACTIVE); 222 if (!sc->sc_io_res) { 223 device_printf(self, "Could not map memory\n"); 224 goto error; 225 } 226 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 227 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 228 sc->sc_io_size = rman_get_size(sc->sc_io_res); 229 230 rid = 0; 231 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 232 RF_SHAREABLE | RF_ACTIVE); 233 if (sc->sc_irq_res == NULL) { 234 device_printf(self, "Could not allocate irq\n"); 235 goto error; 236 } 237 sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 238 if (!sc->sc_bus.bdev) { 239 device_printf(self, "Could not add USB device\n"); 240 goto error; 241 } 242 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 243 244 /* 245 * ohci_pci_match will never return NULL if ohci_pci_probe 246 * succeeded 247 */ 248 device_set_desc(sc->sc_bus.bdev, ohci_pci_match(self)); 249 switch (pci_get_vendor(self)) { 250 case PCI_OHCI_VENDORID_ACERLABS: 251 sprintf(sc->sc_vendor, "AcerLabs"); 252 break; 253 case PCI_OHCI_VENDORID_AMD: 254 sprintf(sc->sc_vendor, "AMD"); 255 break; 256 case PCI_OHCI_VENDORID_APPLE: 257 sprintf(sc->sc_vendor, "Apple"); 258 break; 259 case PCI_OHCI_VENDORID_ATI: 260 sprintf(sc->sc_vendor, "ATI"); 261 break; 262 case PCI_OHCI_VENDORID_CMDTECH: 263 sprintf(sc->sc_vendor, "CMDTECH"); 264 break; 265 case PCI_OHCI_VENDORID_NEC: 266 sprintf(sc->sc_vendor, "NEC"); 267 break; 268 case PCI_OHCI_VENDORID_NVIDIA: 269 case PCI_OHCI_VENDORID_NVIDIA2: 270 sprintf(sc->sc_vendor, "nVidia"); 271 break; 272 case PCI_OHCI_VENDORID_OPTI: 273 sprintf(sc->sc_vendor, "OPTi"); 274 break; 275 case PCI_OHCI_VENDORID_SIS: 276 sprintf(sc->sc_vendor, "SiS"); 277 break; 278 case PCI_OHCI_VENDORID_SUN: 279 sprintf(sc->sc_vendor, "SUN"); 280 break; 281 default: 282 if (bootverbose) { 283 device_printf(self, "(New OHCI DeviceId=0x%08x)\n", 284 pci_get_devid(self)); 285 } 286 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self)); 287 } 288 289 /* sc->sc_bus.usbrev; set by ohci_init() */ 290 291 #if (__FreeBSD_version >= 700031) 292 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 293 NULL, (void *)(void *)ohci_interrupt, sc, &sc->sc_intr_hdl); 294 #else 295 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 296 (void *)(void *)ohci_interrupt, sc, &sc->sc_intr_hdl); 297 #endif 298 if (err) { 299 device_printf(self, "Could not setup irq, %d\n", err); 300 sc->sc_intr_hdl = NULL; 301 goto error; 302 } 303 err = ohci_init(sc); 304 if (!err) { 305 err = device_probe_and_attach(sc->sc_bus.bdev); 306 } 307 if (err) { 308 device_printf(self, "USB init failed\n"); 309 goto error; 310 } 311 return (0); 312 313 error: 314 ohci_pci_detach(self); 315 return (ENXIO); 316 } 317 318 static int 319 ohci_pci_detach(device_t self) 320 { 321 ohci_softc_t *sc = device_get_softc(self); 322 device_t bdev; 323 324 if (sc->sc_bus.bdev) { 325 bdev = sc->sc_bus.bdev; 326 device_detach(bdev); 327 device_delete_child(self, bdev); 328 } 329 /* during module unload there are lots of children leftover */ 330 device_delete_all_children(self); 331 332 pci_disable_busmaster(self); 333 334 if (sc->sc_irq_res && sc->sc_intr_hdl) { 335 /* 336 * only call ohci_detach() after ohci_init() 337 */ 338 ohci_detach(sc); 339 340 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 341 342 if (err) { 343 /* XXX or should we panic? */ 344 device_printf(self, "Could not tear down irq, %d\n", 345 err); 346 } 347 sc->sc_intr_hdl = NULL; 348 } 349 if (sc->sc_irq_res) { 350 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 351 sc->sc_irq_res = NULL; 352 } 353 if (sc->sc_io_res) { 354 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, 355 sc->sc_io_res); 356 sc->sc_io_res = NULL; 357 } 358 usb2_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc); 359 360 return (0); 361 } 362 363 static driver_t ohci_driver = 364 { 365 .name = "ohci", 366 .methods = (device_method_t[]){ 367 /* device interface */ 368 DEVMETHOD(device_probe, ohci_pci_probe), 369 DEVMETHOD(device_attach, ohci_pci_attach), 370 DEVMETHOD(device_detach, ohci_pci_detach), 371 DEVMETHOD(device_suspend, ohci_pci_suspend), 372 DEVMETHOD(device_resume, ohci_pci_resume), 373 DEVMETHOD(device_shutdown, bus_generic_shutdown), 374 375 /* bus interface */ 376 DEVMETHOD(bus_print_child, bus_generic_print_child), 377 378 {0, 0} 379 }, 380 .size = sizeof(struct ohci_softc), 381 }; 382 383 static devclass_t ohci_devclass; 384 385 DRIVER_MODULE(ohci, pci, ohci_driver, ohci_devclass, 0, 0); 386 DRIVER_MODULE(ohci, cardbus, ohci_driver, ohci_devclass, 0, 0); 387 MODULE_DEPEND(ohci, usb, 1, 1, 1); 388