2 * Copyright (c) 2003 Hidetoshi Shimokawa
3 * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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:
17 * This product includes software developed by K. Kobayashi and H. SHimokawa
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.
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.
34 * $FreeBSD: src/sys/dev/firewire/fwohci_pci.c,v 1.38 2004/01/23 17:37:09 simokawa Exp $
35 * $DragonFly: src/sys/bus/firewire/fwohci_pci.c,v 1.4 2004/02/05 13:32:08 joerg Exp $
38 #define BOUNCE_BUFFER_TEST 0
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
46 #include <sys/queue.h>
47 #include <machine/bus.h>
49 #include <sys/malloc.h>
50 #if __FreeBSD_version >= 501102
52 #include <sys/mutex.h>
54 #include <machine/resource.h>
56 #if __FreeBSD_version < 500000
57 #include <machine/clock.h> /* for DELAY() */
61 #include <bus/pci/pcivar.h>
62 #include <bus/pci/pcireg.h>
65 #include "firewirereg.h"
68 #include "fwohcireg.h"
69 #include "fwohcivar.h"
71 #if __FreeBSD_version < 500000
72 #include <pci/pcivar.h>
73 #include <pci/pcireg.h>
75 #include <dev/pci/pcivar.h>
76 #include <dev/pci/pcireg.h>
79 #include <dev/firewire/firewire.h>
80 #include <dev/firewire/firewirereg.h>
82 #include <dev/firewire/fwdma.h>
83 #include <dev/firewire/fwohcireg.h>
84 #include <dev/firewire/fwohcivar.h>
87 static int fwohci_pci_attach(device_t self);
88 static int fwohci_pci_detach(device_t self);
94 fwohci_pci_probe( device_t dev )
99 id = pci_get_devid(dev);
100 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) {
101 device_set_desc(dev, "NEC uPD72861");
104 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) {
105 device_set_desc(dev, "NEC uPD72871/2");
108 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72870)) {
109 device_set_desc(dev, "NEC uPD72870");
112 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72874)) {
113 device_set_desc(dev, "NEC uPD72874");
116 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) {
117 device_set_desc(dev, "Texas Instruments TSB12LV22");
120 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
121 device_set_desc(dev, "Texas Instruments TSB12LV23");
124 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
125 device_set_desc(dev, "Texas Instruments TSB12LV26");
128 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
129 device_set_desc(dev, "Texas Instruments TSB43AA22");
132 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
133 device_set_desc(dev, "Texas Instruments TSB43AB22/A");
136 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) {
137 device_set_desc(dev, "Texas Instruments TSB43AB23");
140 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) {
141 device_set_desc(dev, "Texas Instruments TSB82AA2");
144 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
145 device_set_desc(dev, "Texas Instruments PCI4450");
148 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
149 device_set_desc(dev, "Texas Instruments PCI4410A");
152 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) {
153 device_set_desc(dev, "Texas Instruments PCI4451");
156 if (id == (FW_VENDORID_SONY | FW_DEVICE_CX3022)) {
157 device_set_desc(dev, "Sony CX3022");
160 if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
161 device_set_desc(dev, "VIA VT6306");
164 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) {
165 device_set_desc(dev, "Ricoh R5C551");
168 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
169 device_set_desc(dev, "Ricoh R5C552");
172 if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
173 device_set_desc(dev, "Apple Pangea");
176 if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) {
177 device_set_desc(dev, "Apple UniNorth");
180 if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
181 device_set_desc(dev, "Lucent FW322/323");
185 if (pci_get_class(dev) == PCIC_SERIALBUS
186 && pci_get_subclass(dev) == PCIS_SERIALBUS_FW
187 && pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
188 device_printf(dev, "vendor=%x, dev=%x\n", pci_get_vendor(dev),
189 pci_get_device(dev));
190 device_set_desc(dev, "1394 Open Host Controller Interface");
197 #if __FreeBSD_version < 500000
199 fwohci_dummy_intr(void *arg)
206 fwohci_pci_init(device_t self)
208 int olatency, latency, ocache_line, cache_line;
211 cmd = pci_read_config(self, PCIR_COMMAND, 2);
212 cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN |
213 PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN;
215 cmd &= ~PCIM_CMD_MWRICEN;
217 pci_write_config(self, PCIR_COMMAND, cmd, 2);
219 latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1);
220 #define DEF_LATENCY 0x20
221 if (olatency < DEF_LATENCY) {
222 latency = DEF_LATENCY;
223 pci_write_config(self, PCIR_LATTIMER, latency, 1);
226 cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
227 #define DEF_CACHE_LINE 8
228 if (ocache_line < DEF_CACHE_LINE) {
229 cache_line = DEF_CACHE_LINE;
230 pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
233 if (firewire_debug) {
234 device_printf(self, "latency timer %d -> %d.\n",
236 device_printf(self, "cache size %d -> %d.\n",
237 ocache_line, cache_line);
244 fwohci_pci_attach(device_t self)
246 fwohci_softc_t *sc = device_get_softc(self);
249 #if __FreeBSD_version < 500000
251 /* For the moment, put in a message stating what is wrong */
252 intr = pci_read_config(self, PCIR_INTLINE, 1);
253 if (intr == 0 || intr == 255) {
254 device_printf(self, "Invalid irq %d\n", intr);
256 device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n");
262 firewire_debug = bootverbose;
264 fwohci_pci_init(self);
267 sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
268 0, ~0, 1, RF_ACTIVE);
270 device_printf(self, "Could not map memory\n");
274 sc->bst = rman_get_bustag(sc->bsr);
275 sc->bsh = rman_get_bushandle(sc->bsr);
278 sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
279 RF_SHAREABLE | RF_ACTIVE);
280 if (sc->irq_res == NULL) {
281 device_printf(self, "Could not allocate irq\n");
282 fwohci_pci_detach(self);
287 err = bus_setup_intr(self, sc->irq_res,
289 INTR_TYPE_NET | INTR_MPSAFE,
293 (driver_intr_t *) fwohci_intr, sc, &sc->ih);
294 #if __FreeBSD_version < 500000
295 /* XXX splcam() should mask this irq for sbp.c*/
296 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM,
297 (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_cam);
298 /* XXX splbio() should mask this irq for physio()/fwmem_strategy() */
299 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_BIO,
300 (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_bio);
303 device_printf(self, "Could not setup irq, %d\n", err);
304 fwohci_pci_detach(self);
308 err = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
310 #if BOUNCE_BUFFER_TEST
311 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
313 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
315 /*highaddr*/BUS_SPACE_MAXADDR,
316 /*filter*/NULL, /*filterarg*/NULL,
320 /*flags*/BUS_DMA_ALLOCNOW,
321 #if __FreeBSD_version >= 501102
322 /*lockfunc*/busdma_lock_mutex,
327 printf("fwohci_pci_attach: Could not allocate DMA tag "
328 "- error %d\n", err);
332 err = fwohci_init(sc, self);
335 device_printf(self, "fwohci_init failed with err=%d\n", err);
336 fwohci_pci_detach(self);
340 /* probe and attach a child device(firewire) */
341 bus_generic_probe(self);
342 bus_generic_attach(self);
348 fwohci_pci_detach(device_t self)
350 fwohci_softc_t *sc = device_get_softc(self);
357 fwohci_stop(sc, self);
359 bus_generic_detach(self);
361 device_delete_child(self, sc->fc.bdev);
365 /* disable interrupts that might have been switched on */
366 if (sc->bst && sc->bsh)
367 bus_space_write_4(sc->bst, sc->bsh,
368 FWOHCI_INTMASKCLR, OHCI_INT_EN);
371 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
373 /* XXX or should we panic? */
374 device_printf(self, "Could not tear down irq, %d\n",
376 #if __FreeBSD_version < 500000
377 bus_teardown_intr(self, sc->irq_res, sc->ih_cam);
378 bus_teardown_intr(self, sc->irq_res, sc->ih_bio);
384 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
389 bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr);
395 fwohci_detach(sc, self);
402 fwohci_pci_suspend(device_t dev)
404 fwohci_softc_t *sc = device_get_softc(dev);
407 device_printf(dev, "fwohci_pci_suspend\n");
408 err = bus_generic_suspend(dev);
411 fwohci_stop(sc, dev);
416 fwohci_pci_resume(device_t dev)
418 fwohci_softc_t *sc = device_get_softc(dev);
421 device_printf(dev, "fwohci_pci_resume: power_state = 0x%08x\n",
422 pci_get_powerstate(dev));
423 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
425 fwohci_pci_init(dev);
426 fwohci_resume(sc, dev);
431 fwohci_pci_shutdown(device_t dev)
433 fwohci_softc_t *sc = device_get_softc(dev);
435 bus_generic_shutdown(dev);
436 fwohci_stop(sc, dev);
441 fwohci_pci_add_child(device_t dev, int order, const char *name, int unit)
443 struct fwohci_softc *sc;
447 sc = (struct fwohci_softc *)device_get_softc(dev);
448 child = device_add_child(dev, name, unit);
453 device_set_ivars(child, (void *)&sc->fc);
455 err = device_probe_and_attach(child);
457 device_printf(dev, "probe_and_attach failed with err=%d\n",
459 fwohci_pci_detach(dev);
460 device_delete_child(dev, child);
465 * Clear the bus reset event flag to start transactions even when
466 * interrupt is disabled during the boot process.
468 DELAY(250); /* 2 cycles */
470 fwohci_poll((void *)sc, 0, -1);
476 static device_method_t fwohci_methods[] = {
477 /* Device interface */
478 DEVMETHOD(device_probe, fwohci_pci_probe),
479 DEVMETHOD(device_attach, fwohci_pci_attach),
480 DEVMETHOD(device_detach, fwohci_pci_detach),
481 DEVMETHOD(device_suspend, fwohci_pci_suspend),
482 DEVMETHOD(device_resume, fwohci_pci_resume),
483 DEVMETHOD(device_shutdown, fwohci_pci_shutdown),
486 DEVMETHOD(bus_add_child, fwohci_pci_add_child),
487 DEVMETHOD(bus_print_child, bus_generic_print_child),
492 static driver_t fwohci_driver = {
495 sizeof(fwohci_softc_t),
498 static devclass_t fwohci_devclass;
501 MODULE_DEPEND(fwohci, firewire, 1, 1, 1);
503 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0);
504 DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0);