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$ 35 */ 36 37 #define BOUNCE_BUFFER_TEST 0 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/module.h> 43 #include <sys/bus.h> 44 #include <sys/queue.h> 45 #include <machine/bus.h> 46 #include <sys/rman.h> 47 #include <sys/malloc.h> 48 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102 49 #include <sys/lock.h> 50 #include <sys/mutex.h> 51 #endif 52 #include <machine/resource.h> 53 54 #if defined(__DragonFly__) || __FreeBSD_version < 500000 55 #include <machine/clock.h> /* for DELAY() */ 56 #endif 57 58 #ifdef __DragonFly__ 59 #include <bus/pci/pcivar.h> 60 #include <bus/pci/pcireg.h> 61 62 #include "firewire.h" 63 #include "firewirereg.h" 64 65 #include "fwdma.h" 66 #include "fwohcireg.h" 67 #include "fwohcivar.h" 68 #else 69 #if __FreeBSD_version < 500000 70 #include <pci/pcivar.h> 71 #include <pci/pcireg.h> 72 #else 73 #include <dev/pci/pcivar.h> 74 #include <dev/pci/pcireg.h> 75 #endif 76 77 #include <dev/firewire/firewire.h> 78 #include <dev/firewire/firewirereg.h> 79 80 #include <dev/firewire/fwdma.h> 81 #include <dev/firewire/fwohcireg.h> 82 #include <dev/firewire/fwohcivar.h> 83 #endif 84 85 static int fwohci_pci_attach(device_t self); 86 static int fwohci_pci_detach(device_t self); 87 88 /* 89 * The probe routine. 90 */ 91 static int 92 fwohci_pci_probe( device_t dev ) 93 { 94 #if 1 95 uint32_t id; 96 97 id = pci_get_devid(dev); 98 if (id == (FW_VENDORID_NATSEMI | FW_DEVICE_CS4210)) { 99 device_set_desc(dev, "National Semiconductor CS4210"); 100 return 0; 101 } 102 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) { 103 device_set_desc(dev, "NEC uPD72861"); 104 return 0; 105 } 106 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) { 107 device_set_desc(dev, "NEC uPD72871/2"); 108 return 0; 109 } 110 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72870)) { 111 device_set_desc(dev, "NEC uPD72870"); 112 return 0; 113 } 114 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72873)) { 115 device_set_desc(dev, "NEC uPD72873"); 116 return 0; 117 } 118 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72874)) { 119 device_set_desc(dev, "NEC uPD72874"); 120 return 0; 121 } 122 if (id == (FW_VENDORID_SIS | FW_DEVICE_7007)) { 123 /* It has no real identifier, using device id. */ 124 device_set_desc(dev, "SiS 7007"); 125 return 0; 126 } 127 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) { 128 device_set_desc(dev, "Texas Instruments TSB12LV22"); 129 return 0; 130 } 131 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) { 132 device_set_desc(dev, "Texas Instruments TSB12LV23"); 133 return 0; 134 } 135 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) { 136 device_set_desc(dev, "Texas Instruments TSB12LV26"); 137 return 0; 138 } 139 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) { 140 device_set_desc(dev, "Texas Instruments TSB43AA22"); 141 return 0; 142 } 143 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) { 144 device_set_desc(dev, "Texas Instruments TSB43AB22/A"); 145 return 0; 146 } 147 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB21)) { 148 device_set_desc(dev, "Texas Instruments TSB43AB21/A/AI/A-EP"); 149 return 0; 150 } 151 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) { 152 device_set_desc(dev, "Texas Instruments TSB43AB23"); 153 return 0; 154 } 155 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) { 156 device_set_desc(dev, "Texas Instruments TSB82AA2"); 157 return 0; 158 } 159 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) { 160 device_set_desc(dev, "Texas Instruments PCI4450"); 161 return 0; 162 } 163 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) { 164 device_set_desc(dev, "Texas Instruments PCI4410A"); 165 return 0; 166 } 167 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) { 168 device_set_desc(dev, "Texas Instruments PCI4451"); 169 return 0; 170 } 171 if (id == (FW_VENDORID_SONY | FW_DEVICE_CX3022)) { 172 device_set_desc(dev, "Sony CX3022"); 173 return 0; 174 } 175 if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) { 176 device_set_desc(dev, "VIA VT6306"); 177 return 0; 178 } 179 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) { 180 device_set_desc(dev, "Ricoh R5C551"); 181 return 0; 182 } 183 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) { 184 device_set_desc(dev, "Ricoh R5C552"); 185 return 0; 186 } 187 if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) { 188 device_set_desc(dev, "Apple Pangea"); 189 return 0; 190 } 191 if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) { 192 device_set_desc(dev, "Apple UniNorth"); 193 return 0; 194 } 195 if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) { 196 device_set_desc(dev, "Lucent FW322/323"); 197 return 0; 198 } 199 if (id == (FW_VENDORID_INTEL | FW_DEVICE_82372FB)) { 200 device_set_desc(dev, "Intel 82372FB"); 201 return 0; 202 } 203 if (id == (FW_VENDORID_ADAPTEC | FW_DEVICE_AIC5800)) { 204 device_set_desc(dev, "Adaptec AHA-894x/AIC-5800"); 205 return 0; 206 } 207 #endif 208 if (pci_get_class(dev) == PCIC_SERIALBUS 209 && pci_get_subclass(dev) == PCIS_SERIALBUS_FW 210 && pci_get_progif(dev) == PCI_INTERFACE_OHCI) { 211 if (bootverbose) 212 device_printf(dev, "vendor=%x, dev=%x\n", 213 pci_get_vendor(dev), pci_get_device(dev)); 214 device_set_desc(dev, "1394 Open Host Controller Interface"); 215 return 0; 216 } 217 218 return ENXIO; 219 } 220 221 #if defined(__DragonFly__) || __FreeBSD_version < 500000 222 static void 223 fwohci_dummy_intr(void *arg) 224 { 225 /* XXX do nothing */ 226 } 227 #endif 228 229 static int 230 fwohci_pci_init(device_t self) 231 { 232 int olatency, latency, ocache_line, cache_line; 233 uint16_t cmd; 234 235 cmd = pci_read_config(self, PCIR_COMMAND, 2); 236 cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN | 237 PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN; 238 #if 1 239 cmd &= ~PCIM_CMD_MWRICEN; 240 #endif 241 pci_write_config(self, PCIR_COMMAND, cmd, 2); 242 243 latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1); 244 #define DEF_LATENCY 0x20 245 if (olatency < DEF_LATENCY) { 246 latency = DEF_LATENCY; 247 pci_write_config(self, PCIR_LATTIMER, latency, 1); 248 } 249 250 cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1); 251 #define DEF_CACHE_LINE 8 252 if (ocache_line < DEF_CACHE_LINE) { 253 cache_line = DEF_CACHE_LINE; 254 pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1); 255 } 256 257 if (firewire_debug) { 258 device_printf(self, "latency timer %d -> %d.\n", 259 olatency, latency); 260 device_printf(self, "cache size %d -> %d.\n", 261 ocache_line, cache_line); 262 } 263 264 return 0; 265 } 266 267 static int 268 fwohci_pci_attach(device_t self) 269 { 270 fwohci_softc_t *sc = device_get_softc(self); 271 int err; 272 int rid; 273 #if defined(__DragonFly__) || __FreeBSD_version < 500000 274 int intr; 275 /* For the moment, put in a message stating what is wrong */ 276 intr = pci_read_config(self, PCIR_INTLINE, 1); 277 if (intr == 0 || intr == 255) { 278 device_printf(self, "Invalid irq %d\n", intr); 279 #ifdef __i386__ 280 device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n"); 281 #endif 282 } 283 #endif 284 285 #if 0 286 if (bootverbose) 287 firewire_debug = bootverbose; 288 #endif 289 290 fwohci_pci_init(self); 291 292 rid = PCI_CBMEM; 293 #if __FreeBSD_version >= 502109 294 sc->bsr = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE); 295 #else 296 sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid, 297 0, ~0, 1, RF_ACTIVE); 298 #endif 299 if (!sc->bsr) { 300 device_printf(self, "Could not map memory\n"); 301 return ENXIO; 302 } 303 304 sc->bst = rman_get_bustag(sc->bsr); 305 sc->bsh = rman_get_bushandle(sc->bsr); 306 307 rid = 0; 308 #if __FreeBSD_version >= 502109 309 sc->irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 310 RF_SHAREABLE | RF_ACTIVE); 311 #else 312 sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1, 313 RF_SHAREABLE | RF_ACTIVE); 314 #endif 315 if (sc->irq_res == NULL) { 316 device_printf(self, "Could not allocate irq\n"); 317 fwohci_pci_detach(self); 318 return ENXIO; 319 } 320 321 322 err = bus_setup_intr(self, sc->irq_res, 323 #if FWOHCI_TASKQUEUE 324 INTR_TYPE_NET | INTR_MPSAFE, 325 #else 326 INTR_TYPE_NET, 327 #endif 328 (driver_intr_t *) fwohci_intr, sc, &sc->ih); 329 #if defined(__DragonFly__) || __FreeBSD_version < 500000 330 /* XXX splcam() should mask this irq for sbp.c*/ 331 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM, 332 (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_cam); 333 /* XXX splbio() should mask this irq for physio()/fwmem_strategy() */ 334 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_BIO, 335 (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_bio); 336 #endif 337 if (err) { 338 device_printf(self, "Could not setup irq, %d\n", err); 339 fwohci_pci_detach(self); 340 return ENXIO; 341 } 342 343 err = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, 344 /*boundary*/0, 345 #if BOUNCE_BUFFER_TEST 346 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT, 347 #else 348 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 349 #endif 350 /*highaddr*/BUS_SPACE_MAXADDR, 351 /*filter*/NULL, /*filterarg*/NULL, 352 /*maxsize*/0x100000, 353 /*nsegments*/0x20, 354 /*maxsegsz*/0x8000, 355 /*flags*/BUS_DMA_ALLOCNOW, 356 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102 357 /*lockfunc*/busdma_lock_mutex, 358 /*lockarg*/&Giant, 359 #endif 360 &sc->fc.dmat); 361 if (err != 0) { 362 printf("fwohci_pci_attach: Could not allocate DMA tag " 363 "- error %d\n", err); 364 return (ENOMEM); 365 } 366 367 err = fwohci_init(sc, self); 368 369 if (err) { 370 device_printf(self, "fwohci_init failed with err=%d\n", err); 371 fwohci_pci_detach(self); 372 return EIO; 373 } 374 375 /* probe and attach a child device(firewire) */ 376 bus_generic_probe(self); 377 bus_generic_attach(self); 378 379 return 0; 380 } 381 382 static int 383 fwohci_pci_detach(device_t self) 384 { 385 fwohci_softc_t *sc = device_get_softc(self); 386 int s; 387 388 389 s = splfw(); 390 391 if (sc->bsr) 392 fwohci_stop(sc, self); 393 394 bus_generic_detach(self); 395 if (sc->fc.bdev) { 396 device_delete_child(self, sc->fc.bdev); 397 sc->fc.bdev = NULL; 398 } 399 400 /* disable interrupts that might have been switched on */ 401 if (sc->bst && sc->bsh) 402 bus_space_write_4(sc->bst, sc->bsh, 403 FWOHCI_INTMASKCLR, OHCI_INT_EN); 404 405 if (sc->irq_res) { 406 int err = bus_teardown_intr(self, sc->irq_res, sc->ih); 407 if (err) 408 /* XXX or should we panic? */ 409 device_printf(self, "Could not tear down irq, %d\n", 410 err); 411 #if defined(__DragonFly__) || __FreeBSD_version < 500000 412 bus_teardown_intr(self, sc->irq_res, sc->ih_cam); 413 bus_teardown_intr(self, sc->irq_res, sc->ih_bio); 414 #endif 415 sc->ih = NULL; 416 } 417 418 if (sc->irq_res) { 419 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res); 420 sc->irq_res = NULL; 421 } 422 423 if (sc->bsr) { 424 bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr); 425 sc->bsr = NULL; 426 sc->bst = 0; 427 sc->bsh = 0; 428 } 429 430 fwohci_detach(sc, self); 431 splx(s); 432 433 return 0; 434 } 435 436 static int 437 fwohci_pci_suspend(device_t dev) 438 { 439 fwohci_softc_t *sc = device_get_softc(dev); 440 int err; 441 442 device_printf(dev, "fwohci_pci_suspend\n"); 443 err = bus_generic_suspend(dev); 444 if (err) 445 return err; 446 fwohci_stop(sc, dev); 447 return 0; 448 } 449 450 static int 451 fwohci_pci_resume(device_t dev) 452 { 453 fwohci_softc_t *sc = device_get_softc(dev); 454 455 fwohci_pci_init(dev); 456 fwohci_resume(sc, dev); 457 return 0; 458 } 459 460 static int 461 fwohci_pci_shutdown(device_t dev) 462 { 463 fwohci_softc_t *sc = device_get_softc(dev); 464 465 bus_generic_shutdown(dev); 466 fwohci_stop(sc, dev); 467 return 0; 468 } 469 470 static device_t 471 fwohci_pci_add_child(device_t dev, int order, const char *name, int unit) 472 { 473 struct fwohci_softc *sc; 474 device_t child; 475 int s, err = 0; 476 477 sc = (struct fwohci_softc *)device_get_softc(dev); 478 child = device_add_child(dev, name, unit); 479 if (child == NULL) 480 return (child); 481 482 sc->fc.bdev = child; 483 device_set_ivars(child, (void *)&sc->fc); 484 485 err = device_probe_and_attach(child); 486 if (err) { 487 device_printf(dev, "probe_and_attach failed with err=%d\n", 488 err); 489 fwohci_pci_detach(dev); 490 device_delete_child(dev, child); 491 return NULL; 492 } 493 494 /* XXX 495 * Clear the bus reset event flag to start transactions even when 496 * interrupt is disabled during the boot process. 497 */ 498 DELAY(250); /* 2 cycles */ 499 s = splfw(); 500 fwohci_poll((void *)sc, 0, -1); 501 splx(s); 502 503 return (child); 504 } 505 506 static device_method_t fwohci_methods[] = { 507 /* Device interface */ 508 DEVMETHOD(device_probe, fwohci_pci_probe), 509 DEVMETHOD(device_attach, fwohci_pci_attach), 510 DEVMETHOD(device_detach, fwohci_pci_detach), 511 DEVMETHOD(device_suspend, fwohci_pci_suspend), 512 DEVMETHOD(device_resume, fwohci_pci_resume), 513 DEVMETHOD(device_shutdown, fwohci_pci_shutdown), 514 515 /* Bus interface */ 516 DEVMETHOD(bus_add_child, fwohci_pci_add_child), 517 DEVMETHOD(bus_print_child, bus_generic_print_child), 518 519 { 0, 0 } 520 }; 521 522 static driver_t fwohci_driver = { 523 "fwohci", 524 fwohci_methods, 525 sizeof(fwohci_softc_t), 526 }; 527 528 static devclass_t fwohci_devclass; 529 530 #ifdef FWOHCI_MODULE 531 MODULE_DEPEND(fwohci, firewire, 1, 1, 1); 532 #endif 533 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0); 534 DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0); 535