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