1 /* 2 * Copyright (c) 2003 Hidetoshi Shimokawa 3 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the acknowledgement as bellow: 16 * 17 * This product includes software developed by K. Kobayashi and H. SHimokawa 18 * 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 * 34 * $FreeBSD: src/sys/dev/firewire/fwohci_pci.c,v 1.38 2004/01/23 17:37:09 simokawa Exp $ 35 * $DragonFly: src/sys/bus/firewire/fwohci_pci.c,v 1.21 2005/11/17 21:29:45 dillon Exp $ 36 */ 37 38 #define BOUNCE_BUFFER_TEST 0 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/module.h> 44 #include <sys/conf.h> 45 #include <sys/bus.h> 46 #include <sys/queue.h> 47 #include <machine/bus.h> 48 #include <sys/rman.h> 49 #include <sys/malloc.h> 50 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102 51 #include <sys/lock.h> 52 #include <sys/mutex.h> 53 #endif 54 #include <machine/resource.h> 55 #include <sys/thread2.h> 56 57 #if defined(__DragonFly__) || __FreeBSD_version < 500000 58 #include <machine/clock.h> /* for DELAY() */ 59 #endif 60 61 #ifdef __DragonFly__ 62 #include <bus/pci/pcivar.h> 63 #include <bus/pci/pcireg.h> 64 65 #include "firewire.h" 66 #include "firewirereg.h" 67 68 #include "fwdma.h" 69 #include "fwohcireg.h" 70 #include "fwohcivar.h" 71 #else 72 #if __FreeBSD_version < 500000 73 #include <pci/pcivar.h> 74 #include <pci/pcireg.h> 75 #else 76 #include <dev/pci/pcivar.h> 77 #include <dev/pci/pcireg.h> 78 #endif 79 80 #include <dev/firewire/firewire.h> 81 #include <dev/firewire/firewirereg.h> 82 83 #include <dev/firewire/fwdma.h> 84 #include <dev/firewire/fwohcireg.h> 85 #include <dev/firewire/fwohcivar.h> 86 #endif 87 88 static int fwohci_pci_attach(device_t self); 89 static int fwohci_pci_detach(device_t self); 90 91 /* 92 * The probe routine. 93 */ 94 static int 95 fwohci_pci_probe( device_t dev ) 96 { 97 #if 1 98 u_int32_t id; 99 100 id = pci_get_devid(dev); 101 if (id == (FW_VENDORID_NATSEMI | FW_DEVICE_CS4210)) { 102 device_set_desc(dev, "National Semiconductor CS4210"); 103 return 0; 104 } 105 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) { 106 device_set_desc(dev, "NEC uPD72861"); 107 return 0; 108 } 109 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) { 110 device_set_desc(dev, "NEC uPD72871/2"); 111 return 0; 112 } 113 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72870)) { 114 device_set_desc(dev, "NEC uPD72870"); 115 return 0; 116 } 117 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72874)) { 118 device_set_desc(dev, "NEC uPD72874"); 119 return 0; 120 } 121 if (id == (FW_VENDORID_SIS | FW_DEVICE_7007)) { 122 /* It has no real identifier, using device id. */ 123 device_set_desc(dev, "SiS 7007"); 124 return 0; 125 } 126 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) { 127 device_set_desc(dev, "Texas Instruments TSB12LV22"); 128 return 0; 129 } 130 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) { 131 device_set_desc(dev, "Texas Instruments TSB12LV23"); 132 return 0; 133 } 134 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) { 135 device_set_desc(dev, "Texas Instruments TSB12LV26"); 136 return 0; 137 } 138 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) { 139 device_set_desc(dev, "Texas Instruments TSB43AA22"); 140 return 0; 141 } 142 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) { 143 device_set_desc(dev, "Texas Instruments TSB43AB22/A"); 144 return 0; 145 } 146 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB21)) { 147 device_set_desc(dev, "Texas Instruments TSB43AB21/A/AI/A-EP"); 148 return 0; 149 } 150 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) { 151 device_set_desc(dev, "Texas Instruments TSB43AB23"); 152 return 0; 153 } 154 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) { 155 device_set_desc(dev, "Texas Instruments TSB82AA2"); 156 return 0; 157 } 158 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) { 159 device_set_desc(dev, "Texas Instruments PCI4450"); 160 return 0; 161 } 162 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) { 163 device_set_desc(dev, "Texas Instruments PCI4410A"); 164 return 0; 165 } 166 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) { 167 device_set_desc(dev, "Texas Instruments PCI4451"); 168 return 0; 169 } 170 if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD1947)) { 171 device_set_desc(dev, "Sony i.LINK (CXD1947)"); 172 return 0; 173 } 174 if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD3222)) { 175 device_set_desc(dev, "Sony i.LINK (CXD3222)"); 176 return 0; 177 } 178 if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) { 179 device_set_desc(dev, "VIA Fire II (VT6306)"); 180 return 0; 181 } 182 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) { 183 device_set_desc(dev, "Ricoh R5C551"); 184 return 0; 185 } 186 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) { 187 device_set_desc(dev, "Ricoh R5C552"); 188 return 0; 189 } 190 if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) { 191 device_set_desc(dev, "Apple Pangea"); 192 return 0; 193 } 194 if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) { 195 device_set_desc(dev, "Apple UniNorth"); 196 return 0; 197 } 198 if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) { 199 device_set_desc(dev, "Lucent FW322/323"); 200 return 0; 201 } 202 if (id == (FW_VENDORID_INTEL | FW_DEVICE_82372FB)) { 203 device_set_desc(dev, "Intel 82372FB"); 204 return 0; 205 } 206 if (id == (FW_VENDORID_ADAPTEC | FW_DEVICE_AIC5800)) { 207 device_set_desc(dev, "Adaptec AHA-894x/AIC-5800"); 208 return 0; 209 } 210 #endif 211 if (pci_get_class(dev) == PCIC_SERIALBUS 212 && pci_get_subclass(dev) == PCIS_SERIALBUS_FW 213 && pci_get_progif(dev) == PCI_INTERFACE_OHCI) { 214 device_printf(dev, "vendor=%x, dev=%x\n", pci_get_vendor(dev), 215 pci_get_device(dev)); 216 device_set_desc(dev, "1394 Open Host Controller Interface"); 217 return 0; 218 } 219 220 return ENXIO; 221 } 222 223 static int 224 fwohci_pci_init(device_t self) 225 { 226 int olatency, latency, ocache_line, cache_line; 227 u_int16_t cmd; 228 229 /* 230 * Clear PCIM_CMD_MWRICEN as per FreeBSD/1.20, but note that the 231 * problem may have been related to SERR and PERR being 232 * unconditionally enabled in that rev. 233 * 234 * Do not change the SERRESPEN or PERRESPEN bits. Use the BIOS 235 * values (probably off). This crashes some machines in fwohci_init(). 236 * 237 * The theory here is that the device may not be properly initializing 238 * its on-chip memory, leaving some of it in a parity errored state, 239 * and enabling parity check may result in the device blowing up. 240 * It's also possible that some hardware is just plain broken. OpenBSD 241 * does not turn on SERRESPEN or PERRESPEN so we won't either. 242 */ 243 cmd = pci_read_config(self, PCIR_COMMAND, 2); 244 cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN; 245 cmd &= ~PCIM_CMD_MWRICEN; 246 pci_write_config(self, PCIR_COMMAND, cmd, 2); 247 248 latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1); 249 #define DEF_LATENCY 0x20 250 if (olatency < DEF_LATENCY) { 251 latency = DEF_LATENCY; 252 pci_write_config(self, PCIR_LATTIMER, latency, 1); 253 } 254 255 cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1); 256 #define DEF_CACHE_LINE 8 257 if (ocache_line < DEF_CACHE_LINE) { 258 cache_line = DEF_CACHE_LINE; 259 pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1); 260 } 261 262 if (firewire_debug) { 263 device_printf(self, "latency timer %d -> %d.\n", 264 olatency, latency); 265 device_printf(self, "cache size %d -> %d.\n", 266 ocache_line, cache_line); 267 } 268 269 return 0; 270 } 271 272 static int 273 fwohci_pci_attach(device_t self) 274 { 275 fwohci_softc_t *sc = device_get_softc(self); 276 int err; 277 int rid; 278 #if defined(__DragonFly__) || __FreeBSD_version < 500000 279 int intr; 280 /* For the moment, put in a message stating what is wrong */ 281 intr = pci_read_config(self, PCIR_INTLINE, 1); 282 if (intr == 0 || intr == 255) { 283 device_printf(self, "Invalid irq %d\n", intr); 284 #ifdef __i386__ 285 device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n"); 286 #endif 287 } 288 #endif 289 290 if (bootverbose) 291 firewire_debug = bootverbose; 292 293 fwohci_pci_init(self); 294 295 rid = PCI_CBMEM; 296 sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid, 297 0, ~0, 1, RF_ACTIVE); 298 if (!sc->bsr) { 299 device_printf(self, "Could not map memory\n"); 300 return ENXIO; 301 } 302 303 sc->bst = rman_get_bustag(sc->bsr); 304 sc->bsh = rman_get_bushandle(sc->bsr); 305 306 rid = 0; 307 sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1, 308 RF_SHAREABLE | RF_ACTIVE); 309 if (sc->irq_res == NULL) { 310 device_printf(self, "Could not allocate irq\n"); 311 fwohci_pci_detach(self); 312 return ENXIO; 313 } 314 315 316 err = bus_setup_intr(self, sc->irq_res, 317 #if FWOHCI_TASKQUEUE 318 INTR_MPSAFE, 319 #else 320 0, 321 #endif 322 (driver_intr_t *) fwohci_intr, sc, &sc->ih, NULL); 323 if (err) { 324 device_printf(self, "Could not setup irq, %d\n", err); 325 fwohci_pci_detach(self); 326 return ENXIO; 327 } 328 329 err = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, 330 /*boundary*/0, 331 #if BOUNCE_BUFFER_TEST 332 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT, 333 #else 334 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 335 #endif 336 /*highaddr*/BUS_SPACE_MAXADDR, 337 /*filter*/NULL, /*filterarg*/NULL, 338 /*maxsize*/0x100000, 339 /*nsegments*/0x20, 340 /*maxsegsz*/0x8000, 341 /*flags*/BUS_DMA_ALLOCNOW, 342 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102 343 /*lockfunc*/busdma_lock_mutex, 344 /*lockarg*/&Giant, 345 #endif 346 &sc->fc.dmat); 347 if (err != 0) { 348 printf("fwohci_pci_attach: Could not allocate DMA tag " 349 "- error %d\n", err); 350 return (ENOMEM); 351 } 352 353 err = fwohci_init(sc, self); 354 355 if (err) { 356 device_printf(self, "fwohci_init failed with err=%d\n", err); 357 fwohci_pci_detach(self); 358 return EIO; 359 } 360 361 /* probe and attach a child device(firewire) */ 362 bus_generic_probe(self); 363 bus_generic_attach(self); 364 365 return 0; 366 } 367 368 static int 369 fwohci_pci_detach(device_t self) 370 { 371 fwohci_softc_t *sc = device_get_softc(self); 372 373 crit_enter(); 374 if (sc->bsr) 375 fwohci_stop(sc, self); 376 377 bus_generic_detach(self); 378 if (sc->fc.bdev) { 379 device_delete_child(self, sc->fc.bdev); 380 sc->fc.bdev = NULL; 381 } 382 383 /* disable interrupts that might have been switched on */ 384 if (sc->bst && sc->bsh) 385 bus_space_write_4(sc->bst, sc->bsh, 386 FWOHCI_INTMASKCLR, OHCI_INT_EN); 387 388 if (sc->irq_res) { 389 int err = bus_teardown_intr(self, sc->irq_res, sc->ih); 390 if (err) 391 /* XXX or should we panic? */ 392 device_printf(self, "Could not tear down irq, %d\n", 393 err); 394 sc->ih = NULL; 395 } 396 397 if (sc->irq_res) { 398 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res); 399 sc->irq_res = NULL; 400 } 401 402 if (sc->bsr) { 403 bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr); 404 sc->bsr = NULL; 405 sc->bst = 0; 406 sc->bsh = 0; 407 } 408 409 fwohci_detach(sc, self); 410 crit_exit(); 411 412 return 0; 413 } 414 415 static int 416 fwohci_pci_suspend(device_t dev) 417 { 418 fwohci_softc_t *sc = device_get_softc(dev); 419 int err; 420 421 device_printf(dev, "fwohci_pci_suspend\n"); 422 err = bus_generic_suspend(dev); 423 if (err) 424 return err; 425 fwohci_stop(sc, dev); 426 return 0; 427 } 428 429 static int 430 fwohci_pci_resume(device_t dev) 431 { 432 fwohci_softc_t *sc = device_get_softc(dev); 433 434 #ifndef BURN_BRIDGES 435 device_printf(dev, "fwohci_pci_resume: power_state = 0x%08x\n", 436 pci_get_powerstate(dev)); 437 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 438 #endif 439 fwohci_pci_init(dev); 440 fwohci_resume(sc, dev); 441 return 0; 442 } 443 444 static int 445 fwohci_pci_shutdown(device_t dev) 446 { 447 fwohci_softc_t *sc = device_get_softc(dev); 448 449 bus_generic_shutdown(dev); 450 fwohci_stop(sc, dev); 451 return 0; 452 } 453 454 static device_t 455 fwohci_pci_add_child(device_t bus, device_t parent, int order, const char *name, int unit) 456 { 457 struct fwohci_softc *sc; 458 device_t child; 459 int err = 0; 460 461 sc = (struct fwohci_softc *)device_get_softc(bus); 462 child = device_add_child(parent, name, unit); 463 if (child == NULL) 464 return (child); 465 466 sc->fc.bdev = child; 467 device_set_ivars(child, (void *)&sc->fc); 468 469 err = device_probe_and_attach(child); 470 if (err) { 471 device_printf(parent, "probe_and_attach failed with err=%d\n", 472 err); 473 fwohci_pci_detach(parent); 474 device_delete_child(parent, child); 475 return NULL; 476 } 477 478 /* XXX 479 * Clear the bus reset event flag to start transactions even when 480 * interrupt is disabled during the boot process. 481 */ 482 DELAY(250); /* 2 cycles */ 483 crit_enter(); 484 fwohci_poll((void *)sc, 0, -1); 485 crit_exit(); 486 487 return (child); 488 } 489 490 static device_method_t fwohci_methods[] = { 491 /* Device interface */ 492 DEVMETHOD(device_probe, fwohci_pci_probe), 493 DEVMETHOD(device_attach, fwohci_pci_attach), 494 DEVMETHOD(device_detach, fwohci_pci_detach), 495 DEVMETHOD(device_suspend, fwohci_pci_suspend), 496 DEVMETHOD(device_resume, fwohci_pci_resume), 497 DEVMETHOD(device_shutdown, fwohci_pci_shutdown), 498 499 /* Bus interface */ 500 DEVMETHOD(bus_add_child, fwohci_pci_add_child), 501 DEVMETHOD(bus_print_child, bus_generic_print_child), 502 503 { 0, 0 } 504 }; 505 506 static driver_t fwohci_driver = { 507 "fwohci", 508 fwohci_methods, 509 sizeof(fwohci_softc_t), 510 }; 511 512 static devclass_t fwohci_devclass; 513 514 #ifdef FWOHCI_MODULE 515 MODULE_DEPEND(fwohci, firewire, 1, 1, 1); 516 #endif 517 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0); 518 DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0); 519