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