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 $
37 #define BOUNCE_BUFFER_TEST 0
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
45 #include <sys/queue.h>
47 #include <sys/malloc.h>
48 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
50 #include <sys/mutex.h>
52 #include <sys/thread2.h>
54 #if defined(__DragonFly__) || __FreeBSD_version < 500000
55 #include <machine/clock.h> /* for DELAY() */
59 #include <bus/pci/pcivar.h>
60 #include <bus/pci/pcireg.h>
63 #include "firewirereg.h"
66 #include "fwohcireg.h"
67 #include "fwohcivar.h"
69 #if __FreeBSD_version < 500000
70 #include <pci/pcivar.h>
71 #include <pci/pcireg.h>
73 #include <dev/pci/pcivar.h>
74 #include <dev/pci/pcireg.h>
77 #include <dev/firewire/firewire.h>
78 #include <dev/firewire/firewirereg.h>
80 #include <dev/firewire/fwdma.h>
81 #include <dev/firewire/fwohcireg.h>
82 #include <dev/firewire/fwohcivar.h>
85 static int fwohci_pci_attach(device_t self);
86 static int fwohci_pci_detach(device_t self);
92 fwohci_pci_probe( device_t dev )
97 id = pci_get_devid(dev);
98 if (id == (FW_VENDORID_NATSEMI | FW_DEVICE_CS4210)) {
99 device_set_desc(dev, "National Semiconductor CS4210");
102 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) {
103 device_set_desc(dev, "NEC uPD72861");
106 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) {
107 device_set_desc(dev, "NEC uPD72871/2");
110 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72870)) {
111 device_set_desc(dev, "NEC uPD72870");
114 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72874)) {
115 device_set_desc(dev, "NEC uPD72874");
118 if (id == (FW_VENDORID_SIS | FW_DEVICE_7007)) {
119 /* It has no real identifier, using device id. */
120 device_set_desc(dev, "SiS 7007");
123 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) {
124 device_set_desc(dev, "Texas Instruments TSB12LV22");
127 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
128 device_set_desc(dev, "Texas Instruments TSB12LV23");
131 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
132 device_set_desc(dev, "Texas Instruments TSB12LV26");
135 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
136 device_set_desc(dev, "Texas Instruments TSB43AA22");
139 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
140 device_set_desc(dev, "Texas Instruments TSB43AB22/A");
143 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB21)) {
144 device_set_desc(dev, "Texas Instruments TSB43AB21/A/AI/A-EP");
147 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) {
148 device_set_desc(dev, "Texas Instruments TSB43AB23");
151 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) {
152 device_set_desc(dev, "Texas Instruments TSB82AA2");
155 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
156 device_set_desc(dev, "Texas Instruments PCI4450");
159 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
160 device_set_desc(dev, "Texas Instruments PCI4410A");
163 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) {
164 device_set_desc(dev, "Texas Instruments PCI4451");
167 if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD1947)) {
168 device_set_desc(dev, "Sony i.LINK (CXD1947)");
171 if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD3222)) {
172 device_set_desc(dev, "Sony i.LINK (CXD3222)");
175 if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
176 device_set_desc(dev, "VIA Fire II (VT6306)");
179 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) {
180 device_set_desc(dev, "Ricoh R5C551");
183 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
184 device_set_desc(dev, "Ricoh R5C552");
187 if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
188 device_set_desc(dev, "Apple Pangea");
191 if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) {
192 device_set_desc(dev, "Apple UniNorth");
195 if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
196 device_set_desc(dev, "Lucent FW322/323");
199 if (id == (FW_VENDORID_INTEL | FW_DEVICE_82372FB)) {
200 device_set_desc(dev, "Intel 82372FB");
203 if (id == (FW_VENDORID_ADAPTEC | FW_DEVICE_AIC5800)) {
204 device_set_desc(dev, "Adaptec AHA-894x/AIC-5800");
208 if (pci_get_class(dev) == PCIC_SERIALBUS
209 && pci_get_subclass(dev) == PCIS_SERIALBUS_FW
210 && pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
211 device_printf(dev, "vendor=%x, dev=%x\n", pci_get_vendor(dev),
212 pci_get_device(dev));
213 device_set_desc(dev, "1394 Open Host Controller Interface");
221 fwohci_pci_init(device_t self)
223 int olatency, latency, ocache_line, cache_line;
227 * Clear PCIM_CMD_MWRICEN as per FreeBSD/1.20, but note that the
228 * problem may have been related to SERR and PERR being
229 * unconditionally enabled in that rev.
231 * Do not change the SERRESPEN or PERRESPEN bits. Use the BIOS
232 * values (probably off). This crashes some machines in fwohci_init().
234 * The theory here is that the device may not be properly initializing
235 * its on-chip memory, leaving some of it in a parity errored state,
236 * and enabling parity check may result in the device blowing up.
237 * It's also possible that some hardware is just plain broken. OpenBSD
238 * does not turn on SERRESPEN or PERRESPEN so we won't either.
240 cmd = pci_read_config(self, PCIR_COMMAND, 2);
241 cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN;
242 cmd &= ~PCIM_CMD_MWRICEN;
243 pci_write_config(self, PCIR_COMMAND, cmd, 2);
245 latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1);
246 #define DEF_LATENCY 0x20
247 if (olatency < DEF_LATENCY) {
248 latency = DEF_LATENCY;
249 pci_write_config(self, PCIR_LATTIMER, latency, 1);
252 cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
253 #define DEF_CACHE_LINE 8
254 if (ocache_line < DEF_CACHE_LINE) {
255 cache_line = DEF_CACHE_LINE;
256 pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
259 if (firewire_debug) {
260 device_printf(self, "latency timer %d -> %d.\n",
262 device_printf(self, "cache size %d -> %d.\n",
263 ocache_line, cache_line);
270 fwohci_pci_attach(device_t self)
272 fwohci_softc_t *sc = device_get_softc(self);
275 #if defined(__DragonFly__) || __FreeBSD_version < 500000
277 /* For the moment, put in a message stating what is wrong */
278 intr = pci_read_config(self, PCIR_INTLINE, 1);
279 if (intr == 0 || intr == 255) {
280 device_printf(self, "Invalid irq %d\n", intr);
282 device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n");
288 firewire_debug = bootverbose;
290 fwohci_pci_init(self);
293 sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
294 0, ~0, 1, RF_ACTIVE);
296 device_printf(self, "Could not map memory\n");
300 sc->bst = rman_get_bustag(sc->bsr);
301 sc->bsh = rman_get_bushandle(sc->bsr);
304 sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
305 RF_SHAREABLE | RF_ACTIVE);
306 if (sc->irq_res == NULL) {
307 device_printf(self, "Could not allocate irq\n");
308 fwohci_pci_detach(self);
313 err = bus_setup_intr(self, sc->irq_res,
319 (driver_intr_t *) fwohci_intr, sc, &sc->ih, NULL);
321 device_printf(self, "Could not setup irq, %d\n", err);
322 fwohci_pci_detach(self);
326 err = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
328 #if BOUNCE_BUFFER_TEST
329 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
331 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
333 /*highaddr*/BUS_SPACE_MAXADDR,
334 /*filter*/NULL, /*filterarg*/NULL,
338 /*flags*/BUS_DMA_ALLOCNOW,
339 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
340 /*lockfunc*/busdma_lock_mutex,
345 kprintf("fwohci_pci_attach: Could not allocate DMA tag "
346 "- error %d\n", err);
350 err = fwohci_init(sc, self);
353 device_printf(self, "fwohci_init failed with err=%d\n", err);
354 fwohci_pci_detach(self);
358 /* probe and attach a child device(firewire) */
359 bus_generic_probe(self);
360 bus_generic_attach(self);
366 fwohci_pci_detach(device_t self)
368 fwohci_softc_t *sc = device_get_softc(self);
372 fwohci_stop(sc, self);
374 bus_generic_detach(self);
376 device_delete_child(self, sc->fc.bdev);
380 /* disable interrupts that might have been switched on */
381 if (sc->bst && sc->bsh)
382 bus_space_write_4(sc->bst, sc->bsh,
383 FWOHCI_INTMASKCLR, OHCI_INT_EN);
386 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
388 /* XXX or should we panic? */
389 device_printf(self, "Could not tear down irq, %d\n",
395 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
400 bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr);
406 fwohci_detach(sc, self);
413 fwohci_pci_suspend(device_t dev)
415 fwohci_softc_t *sc = device_get_softc(dev);
418 device_printf(dev, "fwohci_pci_suspend\n");
419 err = bus_generic_suspend(dev);
422 fwohci_stop(sc, dev);
427 fwohci_pci_resume(device_t dev)
429 fwohci_softc_t *sc = device_get_softc(dev);
432 device_printf(dev, "fwohci_pci_resume: power_state = 0x%08x\n",
433 pci_get_powerstate(dev));
434 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
436 fwohci_pci_init(dev);
437 fwohci_resume(sc, dev);
442 fwohci_pci_shutdown(device_t dev)
444 fwohci_softc_t *sc = device_get_softc(dev);
446 bus_generic_shutdown(dev);
447 fwohci_stop(sc, dev);
452 fwohci_pci_add_child(device_t bus, device_t parent, int order, const char *name, int unit)
454 struct fwohci_softc *sc;
458 sc = (struct fwohci_softc *)device_get_softc(bus);
459 child = device_add_child(parent, name, unit);
464 device_set_ivars(child, (void *)&sc->fc);
466 err = device_probe_and_attach(child);
468 device_printf(parent, "probe_and_attach failed with err=%d\n",
470 fwohci_pci_detach(parent);
471 device_delete_child(parent, child);
476 * Clear the bus reset event flag to start transactions even when
477 * interrupt is disabled during the boot process.
479 DELAY(250); /* 2 cycles */
481 fwohci_poll((void *)sc, 0, -1);
487 static device_method_t fwohci_methods[] = {
488 /* Device interface */
489 DEVMETHOD(device_probe, fwohci_pci_probe),
490 DEVMETHOD(device_attach, fwohci_pci_attach),
491 DEVMETHOD(device_detach, fwohci_pci_detach),
492 DEVMETHOD(device_suspend, fwohci_pci_suspend),
493 DEVMETHOD(device_resume, fwohci_pci_resume),
494 DEVMETHOD(device_shutdown, fwohci_pci_shutdown),
497 DEVMETHOD(bus_add_child, fwohci_pci_add_child),
498 DEVMETHOD(bus_print_child, bus_generic_print_child),
503 static driver_t fwohci_driver = {
506 sizeof(fwohci_softc_t),
509 static devclass_t fwohci_devclass;
512 MODULE_DEPEND(fwohci, firewire, 1, 1, 1);
514 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, NULL, NULL);
515 DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, NULL, NULL);