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_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 if (bootverbose) 191 device_printf(dev, "vendor=%x, dev=%x\n", 192 pci_get_vendor(dev), pci_get_device(dev)); 193 device_set_desc(dev, "1394 Open Host Controller Interface"); 194 return 0; 195 } 196 197 return ENXIO; 198 } 199 200 #if defined(__DragonFly__) || __FreeBSD_version < 500000 201 static void 202 fwohci_dummy_intr(void *arg) 203 { 204 /* XXX do nothing */ 205 } 206 #endif 207 208 static int 209 fwohci_pci_init(device_t self) 210 { 211 int olatency, latency, ocache_line, cache_line; 212 uint16_t cmd; 213 214 cmd = pci_read_config(self, PCIR_COMMAND, 2); 215 cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN | 216 PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN; 217 #if 1 218 cmd &= ~PCIM_CMD_MWRICEN; 219 #endif 220 pci_write_config(self, PCIR_COMMAND, cmd, 2); 221 222 latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1); 223 #define DEF_LATENCY 0x20 224 if (olatency < DEF_LATENCY) { 225 latency = DEF_LATENCY; 226 pci_write_config(self, PCIR_LATTIMER, latency, 1); 227 } 228 229 cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1); 230 #define DEF_CACHE_LINE 8 231 if (ocache_line < DEF_CACHE_LINE) { 232 cache_line = DEF_CACHE_LINE; 233 pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1); 234 } 235 236 if (firewire_debug) { 237 device_printf(self, "latency timer %d -> %d.\n", 238 olatency, latency); 239 device_printf(self, "cache size %d -> %d.\n", 240 ocache_line, cache_line); 241 } 242 243 return 0; 244 } 245 246 static int 247 fwohci_pci_attach(device_t self) 248 { 249 fwohci_softc_t *sc = device_get_softc(self); 250 int err; 251 int rid; 252 #if defined(__DragonFly__) || __FreeBSD_version < 500000 253 int intr; 254 /* For the moment, put in a message stating what is wrong */ 255 intr = pci_read_config(self, PCIR_INTLINE, 1); 256 if (intr == 0 || intr == 255) { 257 device_printf(self, "Invalid irq %d\n", intr); 258 #ifdef __i386__ 259 device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n"); 260 #endif 261 } 262 #endif 263 264 if (bootverbose) 265 firewire_debug = bootverbose; 266 267 fwohci_pci_init(self); 268 269 rid = PCI_CBMEM; 270 #if __FreeBSD_version >= 502109 271 sc->bsr = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE); 272 #else 273 sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid, 274 0, ~0, 1, RF_ACTIVE); 275 #endif 276 if (!sc->bsr) { 277 device_printf(self, "Could not map memory\n"); 278 return ENXIO; 279 } 280 281 sc->bst = rman_get_bustag(sc->bsr); 282 sc->bsh = rman_get_bushandle(sc->bsr); 283 284 rid = 0; 285 #if __FreeBSD_version >= 502109 286 sc->irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 287 RF_SHAREABLE | RF_ACTIVE); 288 #else 289 sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1, 290 RF_SHAREABLE | RF_ACTIVE); 291 #endif 292 if (sc->irq_res == NULL) { 293 device_printf(self, "Could not allocate irq\n"); 294 fwohci_pci_detach(self); 295 return ENXIO; 296 } 297 298 299 err = bus_setup_intr(self, sc->irq_res, 300 #if FWOHCI_TASKQUEUE 301 INTR_TYPE_NET | INTR_MPSAFE, 302 #else 303 INTR_TYPE_NET, 304 #endif 305 (driver_intr_t *) fwohci_intr, sc, &sc->ih); 306 #if defined(__DragonFly__) || __FreeBSD_version < 500000 307 /* XXX splcam() should mask this irq for sbp.c*/ 308 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM, 309 (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_cam); 310 /* XXX splbio() should mask this irq for physio()/fwmem_strategy() */ 311 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_BIO, 312 (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_bio); 313 #endif 314 if (err) { 315 device_printf(self, "Could not setup irq, %d\n", err); 316 fwohci_pci_detach(self); 317 return ENXIO; 318 } 319 320 err = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1, 321 /*boundary*/0, 322 #if BOUNCE_BUFFER_TEST 323 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT, 324 #else 325 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 326 #endif 327 /*highaddr*/BUS_SPACE_MAXADDR, 328 /*filter*/NULL, /*filterarg*/NULL, 329 /*maxsize*/0x100000, 330 /*nsegments*/0x20, 331 /*maxsegsz*/0x8000, 332 /*flags*/BUS_DMA_ALLOCNOW, 333 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102 334 /*lockfunc*/busdma_lock_mutex, 335 /*lockarg*/&Giant, 336 #endif 337 &sc->fc.dmat); 338 if (err != 0) { 339 printf("fwohci_pci_attach: Could not allocate DMA tag " 340 "- error %d\n", err); 341 return (ENOMEM); 342 } 343 344 err = fwohci_init(sc, self); 345 346 if (err) { 347 device_printf(self, "fwohci_init failed with err=%d\n", err); 348 fwohci_pci_detach(self); 349 return EIO; 350 } 351 352 /* probe and attach a child device(firewire) */ 353 bus_generic_probe(self); 354 bus_generic_attach(self); 355 356 return 0; 357 } 358 359 static int 360 fwohci_pci_detach(device_t self) 361 { 362 fwohci_softc_t *sc = device_get_softc(self); 363 int s; 364 365 366 s = splfw(); 367 368 if (sc->bsr) 369 fwohci_stop(sc, self); 370 371 bus_generic_detach(self); 372 if (sc->fc.bdev) { 373 device_delete_child(self, sc->fc.bdev); 374 sc->fc.bdev = NULL; 375 } 376 377 /* disable interrupts that might have been switched on */ 378 if (sc->bst && sc->bsh) 379 bus_space_write_4(sc->bst, sc->bsh, 380 FWOHCI_INTMASKCLR, OHCI_INT_EN); 381 382 if (sc->irq_res) { 383 int err = bus_teardown_intr(self, sc->irq_res, sc->ih); 384 if (err) 385 /* XXX or should we panic? */ 386 device_printf(self, "Could not tear down irq, %d\n", 387 err); 388 #if defined(__DragonFly__) || __FreeBSD_version < 500000 389 bus_teardown_intr(self, sc->irq_res, sc->ih_cam); 390 bus_teardown_intr(self, sc->irq_res, sc->ih_bio); 391 #endif 392 sc->ih = NULL; 393 } 394 395 if (sc->irq_res) { 396 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res); 397 sc->irq_res = NULL; 398 } 399 400 if (sc->bsr) { 401 bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr); 402 sc->bsr = NULL; 403 sc->bst = 0; 404 sc->bsh = 0; 405 } 406 407 fwohci_detach(sc, self); 408 splx(s); 409 410 return 0; 411 } 412 413 static int 414 fwohci_pci_suspend(device_t dev) 415 { 416 fwohci_softc_t *sc = device_get_softc(dev); 417 int err; 418 419 device_printf(dev, "fwohci_pci_suspend\n"); 420 err = bus_generic_suspend(dev); 421 if (err) 422 return err; 423 fwohci_stop(sc, dev); 424 return 0; 425 } 426 427 static int 428 fwohci_pci_resume(device_t dev) 429 { 430 fwohci_softc_t *sc = device_get_softc(dev); 431 432 fwohci_pci_init(dev); 433 fwohci_resume(sc, dev); 434 return 0; 435 } 436 437 static int 438 fwohci_pci_shutdown(device_t dev) 439 { 440 fwohci_softc_t *sc = device_get_softc(dev); 441 442 bus_generic_shutdown(dev); 443 fwohci_stop(sc, dev); 444 return 0; 445 } 446 447 static device_t 448 fwohci_pci_add_child(device_t dev, int order, const char *name, int unit) 449 { 450 struct fwohci_softc *sc; 451 device_t child; 452 int s, err = 0; 453 454 sc = (struct fwohci_softc *)device_get_softc(dev); 455 child = device_add_child(dev, name, unit); 456 if (child == NULL) 457 return (child); 458 459 sc->fc.bdev = child; 460 device_set_ivars(child, (void *)&sc->fc); 461 462 err = device_probe_and_attach(child); 463 if (err) { 464 device_printf(dev, "probe_and_attach failed with err=%d\n", 465 err); 466 fwohci_pci_detach(dev); 467 device_delete_child(dev, child); 468 return NULL; 469 } 470 471 /* XXX 472 * Clear the bus reset event flag to start transactions even when 473 * interrupt is disabled during the boot process. 474 */ 475 DELAY(250); /* 2 cycles */ 476 s = splfw(); 477 fwohci_poll((void *)sc, 0, -1); 478 splx(s); 479 480 return (child); 481 } 482 483 static device_method_t fwohci_methods[] = { 484 /* Device interface */ 485 DEVMETHOD(device_probe, fwohci_pci_probe), 486 DEVMETHOD(device_attach, fwohci_pci_attach), 487 DEVMETHOD(device_detach, fwohci_pci_detach), 488 DEVMETHOD(device_suspend, fwohci_pci_suspend), 489 DEVMETHOD(device_resume, fwohci_pci_resume), 490 DEVMETHOD(device_shutdown, fwohci_pci_shutdown), 491 492 /* Bus interface */ 493 DEVMETHOD(bus_add_child, fwohci_pci_add_child), 494 DEVMETHOD(bus_print_child, bus_generic_print_child), 495 496 { 0, 0 } 497 }; 498 499 static driver_t fwohci_driver = { 500 "fwohci", 501 fwohci_methods, 502 sizeof(fwohci_softc_t), 503 }; 504 505 static devclass_t fwohci_devclass; 506 507 #ifdef FWOHCI_MODULE 508 MODULE_DEPEND(fwohci, firewire, 1, 1, 1); 509 #endif 510 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0); 511 DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0); 512