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_UPD72874)) { 99 device_set_desc(dev, "NEC uPD72874"); 100 return 0; 101 } 102 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) { 103 device_set_desc(dev, "Texas Instruments TSB12LV22"); 104 return 0; 105 } 106 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) { 107 device_set_desc(dev, "Texas Instruments TSB12LV23"); 108 return 0; 109 } 110 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) { 111 device_set_desc(dev, "Texas Instruments TSB12LV26"); 112 return 0; 113 } 114 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) { 115 device_set_desc(dev, "Texas Instruments TSB43AA22"); 116 return 0; 117 } 118 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) { 119 device_set_desc(dev, "Texas Instruments TSB43AB22/A"); 120 return 0; 121 } 122 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) { 123 device_set_desc(dev, "Texas Instruments TSB43AB23"); 124 return 0; 125 } 126 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) { 127 device_set_desc(dev, "Texas Instruments TSB82AA2"); 128 return 0; 129 } 130 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) { 131 device_set_desc(dev, "Texas Instruments PCI4450"); 132 return 0; 133 } 134 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) { 135 device_set_desc(dev, "Texas Instruments PCI4410A"); 136 return 0; 137 } 138 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) { 139 device_set_desc(dev, "Texas Instruments PCI4451"); 140 return 0; 141 } 142 if (id == (FW_VENDORID_SONY | FW_DEVICE_CX3022)) { 143 device_set_desc(dev, "Sony CX3022"); 144 return 0; 145 } 146 if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) { 147 device_set_desc(dev, "VIA VT6306"); 148 return 0; 149 } 150 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) { 151 device_set_desc(dev, "Ricoh R5C551"); 152 return 0; 153 } 154 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) { 155 device_set_desc(dev, "Ricoh R5C552"); 156 return 0; 157 } 158 if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) { 159 device_set_desc(dev, "Apple Pangea"); 160 return 0; 161 } 162 if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) { 163 device_set_desc(dev, "Apple UniNorth"); 164 return 0; 165 } 166 if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) { 167 device_set_desc(dev, "Lucent FW322/323"); 168 return 0; 169 } 170 #endif 171 if (pci_get_class(dev) == PCIC_SERIALBUS 172 && pci_get_subclass(dev) == PCIS_SERIALBUS_FW 173 && pci_get_progif(dev) == PCI_INTERFACE_OHCI) { 174 device_printf(dev, "vendor=%x, dev=%x\n", pci_get_vendor(dev), 175 pci_get_device(dev)); 176 device_set_desc(dev, "1394 Open Host Controller Interface"); 177 return 0; 178 } 179 180 return ENXIO; 181 } 182 183 #if __FreeBSD_version < 500000 184 static void 185 fwohci_dummy_intr(void *arg) 186 { 187 /* XXX do nothing */ 188 } 189 #endif 190 191 static int 192 fwohci_pci_init(device_t self) 193 { 194 int olatency, latency, ocache_line, cache_line; 195 u_int16_t cmd; 196 197 cmd = pci_read_config(self, PCIR_COMMAND, 2); 198 cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN | 199 PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN; 200 #if 1 201 cmd &= ~PCIM_CMD_MWRICEN; 202 #endif 203 pci_write_config(self, PCIR_COMMAND, cmd, 2); 204 205 latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1); 206 #define DEF_LATENCY 0x20 207 if (olatency < DEF_LATENCY) { 208 latency = DEF_LATENCY; 209 pci_write_config(self, PCIR_LATTIMER, latency, 1); 210 } 211 212 cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1); 213 #define DEF_CACHE_LINE 8 214 if (ocache_line < DEF_CACHE_LINE) { 215 cache_line = DEF_CACHE_LINE; 216 pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1); 217 } 218 219 if (firewire_debug) { 220 device_printf(self, "latency timer %d -> %d.\n", 221 olatency, latency); 222 device_printf(self, "cache size %d -> %d.\n", 223 ocache_line, cache_line); 224 } 225 226 return 0; 227 } 228 229 static int 230 fwohci_pci_attach(device_t self) 231 { 232 fwohci_softc_t *sc = device_get_softc(self); 233 int err; 234 int rid, s; 235 #if __FreeBSD_version < 500000 236 int intr; 237 /* For the moment, put in a message stating what is wrong */ 238 intr = pci_read_config(self, PCIR_INTLINE, 1); 239 if (intr == 0 || intr == 255) { 240 device_printf(self, "Invalid irq %d\n", intr); 241 #ifdef __i386__ 242 device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n"); 243 #endif 244 } 245 #endif 246 247 if (bootverbose) 248 firewire_debug = bootverbose; 249 250 fwohci_pci_init(self); 251 252 rid = PCI_CBMEM; 253 sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid, 254 0, ~0, 1, RF_ACTIVE); 255 if (!sc->bsr) { 256 device_printf(self, "Could not map memory\n"); 257 return ENXIO; 258 } 259 260 sc->bst = rman_get_bustag(sc->bsr); 261 sc->bsh = rman_get_bushandle(sc->bsr); 262 263 rid = 0; 264 sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1, 265 RF_SHAREABLE | RF_ACTIVE); 266 if (sc->irq_res == NULL) { 267 device_printf(self, "Could not allocate irq\n"); 268 fwohci_pci_detach(self); 269 return ENXIO; 270 } 271 272 sc->fc.bdev = device_add_child(self, "firewire", -1); 273 if (!sc->fc.bdev) { 274 device_printf(self, "Could not add firewire device\n"); 275 fwohci_pci_detach(self); 276 return ENOMEM; 277 } 278 device_set_ivars(sc->fc.bdev, sc); 279 280 err = bus_setup_intr(self, sc->irq_res, 281 #if FWOHCI_TASKQUEUE 282 INTR_TYPE_NET | INTR_MPSAFE, 283 #else 284 INTR_TYPE_NET, 285 #endif 286 (driver_intr_t *) fwohci_intr, sc, &sc->ih); 287 #if __FreeBSD_version < 500000 288 /* XXX splcam() should mask this irq for sbp.c*/ 289 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM, 290 (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_cam); 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 err = device_probe_and_attach(sc->fc.bdev); 326 327 if (err) { 328 device_printf(self, "FireWire init failed\n"); 329 fwohci_pci_detach(self); 330 return EIO; 331 } 332 333 /* XXX 334 * Clear the bus reset event flag to start transactions even when 335 * interrupt is disabled during the boot process. 336 */ 337 DELAY(250); /* 2 cycles */ 338 s = splfw(); 339 fwohci_poll((void *)sc, 0, -1); 340 splx(s); 341 342 return 0; 343 } 344 345 static int 346 fwohci_pci_detach(device_t self) 347 { 348 fwohci_softc_t *sc = device_get_softc(self); 349 int s; 350 351 352 s = splfw(); 353 354 if (sc->bsr) 355 fwohci_stop(sc, self); 356 357 bus_generic_detach(self); 358 if (sc->fc.bdev) { 359 device_delete_child(self, sc->fc.bdev); 360 sc->fc.bdev = NULL; 361 } 362 363 /* disable interrupts that might have been switched on */ 364 if (sc->bst && sc->bsh) 365 bus_space_write_4(sc->bst, sc->bsh, 366 FWOHCI_INTMASKCLR, OHCI_INT_EN); 367 368 if (sc->irq_res) { 369 int err = bus_teardown_intr(self, sc->irq_res, sc->ih); 370 if (err) 371 /* XXX or should we panic? */ 372 device_printf(self, "Could not tear down irq, %d\n", 373 err); 374 #if __FreeBSD_version < 500000 375 err = bus_teardown_intr(self, sc->irq_res, sc->ih_cam); 376 #endif 377 sc->ih = NULL; 378 } 379 380 if (sc->irq_res) { 381 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res); 382 sc->irq_res = NULL; 383 } 384 385 if (sc->bsr) { 386 bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr); 387 sc->bsr = NULL; 388 sc->bst = 0; 389 sc->bsh = 0; 390 } 391 392 fwohci_detach(sc, self); 393 splx(s); 394 395 return 0; 396 } 397 398 static int 399 fwohci_pci_suspend(device_t dev) 400 { 401 fwohci_softc_t *sc = device_get_softc(dev); 402 int err; 403 404 device_printf(dev, "fwohci_pci_suspend\n"); 405 err = bus_generic_suspend(dev); 406 if (err) 407 return err; 408 fwohci_stop(sc, dev); 409 return 0; 410 } 411 412 static int 413 fwohci_pci_resume(device_t dev) 414 { 415 fwohci_softc_t *sc = device_get_softc(dev); 416 417 #ifndef BURN_BRIDGES 418 device_printf(dev, "fwohci_pci_resume: power_state = 0x%08x\n", 419 pci_get_powerstate(dev)); 420 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 421 #endif 422 fwohci_pci_init(dev); 423 fwohci_resume(sc, dev); 424 return 0; 425 } 426 427 static int 428 fwohci_pci_shutdown(device_t dev) 429 { 430 fwohci_softc_t *sc = device_get_softc(dev); 431 432 bus_generic_shutdown(dev); 433 fwohci_stop(sc, dev); 434 return 0; 435 } 436 437 static device_method_t fwohci_methods[] = { 438 /* Device interface */ 439 DEVMETHOD(device_probe, fwohci_pci_probe), 440 DEVMETHOD(device_attach, fwohci_pci_attach), 441 DEVMETHOD(device_detach, fwohci_pci_detach), 442 DEVMETHOD(device_suspend, fwohci_pci_suspend), 443 DEVMETHOD(device_resume, fwohci_pci_resume), 444 DEVMETHOD(device_shutdown, fwohci_pci_shutdown), 445 446 /* Bus interface */ 447 DEVMETHOD(bus_print_child, bus_generic_print_child), 448 449 { 0, 0 } 450 }; 451 452 static driver_t fwohci_driver = { 453 "fwohci", 454 fwohci_methods, 455 sizeof(fwohci_softc_t), 456 }; 457 458 static devclass_t fwohci_devclass; 459 460 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0); 461 DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0); 462