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