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.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
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>
45 #include <sys/queue.h>
46 #include <machine/bus.h>
48 #include <sys/malloc.h>
50 #include <sys/mutex.h>
51 #include <machine/resource.h>
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcireg.h>
56 #include <dev/firewire/firewire.h>
57 #include <dev/firewire/firewirereg.h>
59 #include <dev/firewire/fwdma.h>
60 #include <dev/firewire/fwohcireg.h>
61 #include <dev/firewire/fwohcivar.h>
63 static int fwohci_pci_attach(device_t self);
64 static int fwohci_pci_detach(device_t self);
70 fwohci_pci_probe(device_t dev)
74 id = pci_get_devid(dev);
75 if (id == (FW_VENDORID_NATSEMI | FW_DEVICE_CS4210)) {
76 device_set_desc(dev, "National Semiconductor CS4210");
77 return BUS_PROBE_DEFAULT;
79 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) {
80 device_set_desc(dev, "NEC uPD72861");
81 return BUS_PROBE_DEFAULT;
83 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) {
84 device_set_desc(dev, "NEC uPD72871/2");
85 return BUS_PROBE_DEFAULT;
87 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72870)) {
88 device_set_desc(dev, "NEC uPD72870");
89 return BUS_PROBE_DEFAULT;
91 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72873)) {
92 device_set_desc(dev, "NEC uPD72873");
93 return BUS_PROBE_DEFAULT;
95 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72874)) {
96 device_set_desc(dev, "NEC uPD72874");
97 return BUS_PROBE_DEFAULT;
99 if (id == (FW_VENDORID_SIS | FW_DEVICE_7007)) {
100 /* It has no real identifier, using device id. */
101 device_set_desc(dev, "SiS 7007");
102 return BUS_PROBE_DEFAULT;
104 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) {
105 device_set_desc(dev, "Texas Instruments TSB12LV22");
106 return BUS_PROBE_DEFAULT;
108 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
109 device_set_desc(dev, "Texas Instruments TSB12LV23");
110 return BUS_PROBE_DEFAULT;
112 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
113 device_set_desc(dev, "Texas Instruments TSB12LV26");
114 return BUS_PROBE_DEFAULT;
116 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
117 device_set_desc(dev, "Texas Instruments TSB43AA22");
118 return BUS_PROBE_DEFAULT;
120 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
121 device_set_desc(dev, "Texas Instruments TSB43AB22/A");
122 return BUS_PROBE_DEFAULT;
124 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB21)) {
125 device_set_desc(dev, "Texas Instruments TSB43AB21/A/AI/A-EP");
126 return BUS_PROBE_DEFAULT;
128 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) {
129 device_set_desc(dev, "Texas Instruments TSB43AB23");
130 return BUS_PROBE_DEFAULT;
132 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) {
133 device_set_desc(dev, "Texas Instruments TSB82AA2");
134 return BUS_PROBE_DEFAULT;
136 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
137 device_set_desc(dev, "Texas Instruments PCI4450");
138 return BUS_PROBE_DEFAULT;
140 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
141 device_set_desc(dev, "Texas Instruments PCI4410A");
142 return BUS_PROBE_DEFAULT;
144 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) {
145 device_set_desc(dev, "Texas Instruments PCI4451");
146 return BUS_PROBE_DEFAULT;
148 if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD1947)) {
149 device_printf(dev, "Sony i.LINK (CXD1947) not supported\n");
152 if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD3222)) {
153 device_set_desc(dev, "Sony i.LINK (CXD3222)");
154 return BUS_PROBE_DEFAULT;
156 if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
157 device_set_desc(dev, "VIA Fire II (VT6306)");
158 return BUS_PROBE_DEFAULT;
160 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) {
161 device_set_desc(dev, "Ricoh R5C551");
162 return BUS_PROBE_DEFAULT;
164 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
165 device_set_desc(dev, "Ricoh R5C552");
166 return BUS_PROBE_DEFAULT;
168 if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
169 device_set_desc(dev, "Apple Pangea");
170 return BUS_PROBE_DEFAULT;
172 if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) {
173 device_set_desc(dev, "Apple UniNorth");
174 return BUS_PROBE_DEFAULT;
176 if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
177 device_set_desc(dev, "Lucent FW322/323");
178 return BUS_PROBE_DEFAULT;
180 if (id == (FW_VENDORID_INTEL | FW_DEVICE_82372FB)) {
181 device_set_desc(dev, "Intel 82372FB");
182 return BUS_PROBE_DEFAULT;
184 if (id == (FW_VENDORID_ADAPTEC | FW_DEVICE_AIC5800)) {
185 device_set_desc(dev, "Adaptec AHA-894x/AIC-5800");
186 return BUS_PROBE_DEFAULT;
188 if (id == (FW_VENDORID_SUN | FW_DEVICE_PCIO2FW)) {
189 device_set_desc(dev, "Sun PCIO-2");
190 return BUS_PROBE_DEFAULT;
192 if (pci_get_class(dev) == PCIC_SERIALBUS
193 && pci_get_subclass(dev) == PCIS_SERIALBUS_FW
194 && pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
196 device_printf(dev, "vendor=%x, dev=%x\n",
197 pci_get_vendor(dev), pci_get_device(dev));
198 device_set_desc(dev, "1394 Open Host Controller Interface");
199 return BUS_PROBE_DEFAULT;
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_BUSMASTEREN | PCIM_CMD_MWRICEN;
213 #if 1 /* for broken hardware */
214 cmd &= ~PCIM_CMD_MWRICEN;
216 pci_write_config(self, PCIR_COMMAND, cmd, 2);
219 * Some Sun PCIO-2 FireWire controllers have their intpin register
220 * bogusly set to 0, although it should be 3. Correct that.
222 if (pci_get_devid(self) == (FW_VENDORID_SUN | FW_DEVICE_PCIO2FW) &&
223 pci_get_intpin(self) == 0)
224 pci_set_intpin(self, 3);
226 latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1);
227 #define DEF_LATENCY 0x20
228 if (olatency < DEF_LATENCY) {
229 latency = DEF_LATENCY;
230 pci_write_config(self, PCIR_LATTIMER, latency, 1);
233 cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
234 #define DEF_CACHE_LINE 8
235 if (ocache_line < DEF_CACHE_LINE) {
236 cache_line = DEF_CACHE_LINE;
237 pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
240 if (firewire_debug) {
241 device_printf(self, "latency timer %d -> %d.\n",
243 device_printf(self, "cache size %d -> %d.\n",
244 ocache_line, cache_line);
251 fwohci_pci_attach(device_t self)
253 fwohci_softc_t *sc = device_get_softc(self);
259 firewire_debug = bootverbose;
262 mtx_init(FW_GMTX(&sc->fc), "firewire", NULL, MTX_DEF);
263 fwohci_pci_init(self);
266 sc->bsr = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
268 device_printf(self, "Could not map memory\n");
272 sc->bst = rman_get_bustag(sc->bsr);
273 sc->bsh = rman_get_bushandle(sc->bsr);
276 sc->irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
277 RF_SHAREABLE | RF_ACTIVE);
278 if (sc->irq_res == NULL) {
279 device_printf(self, "Could not allocate irq\n");
280 fwohci_pci_detach(self);
284 err = bus_setup_intr(self, sc->irq_res,
285 INTR_TYPE_NET | INTR_MPSAFE,
286 NULL, (driver_intr_t *) fwohci_intr,
290 device_printf(self, "Could not setup irq, %d\n", err);
291 fwohci_pci_detach(self);
295 err = bus_dma_tag_create(
296 /*parent*/bus_get_dma_tag(self),
299 #if BOUNCE_BUFFER_TEST
300 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
302 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
304 /*highaddr*/BUS_SPACE_MAXADDR,
305 /*filter*/NULL, /*filterarg*/NULL,
309 /*flags*/BUS_DMA_ALLOCNOW,
310 /*lockfunc*/busdma_lock_mutex,
311 /*lockarg*/FW_GMTX(&sc->fc),
314 device_printf(self, "fwohci_pci_attach: Could not allocate DMA "
315 "tag - error %d\n", err);
316 fwohci_pci_detach(self);
320 err = fwohci_init(sc, self);
323 device_printf(self, "fwohci_init failed with err=%d\n", err);
324 fwohci_pci_detach(self);
328 /* probe and attach a child device(firewire) */
329 bus_generic_probe(self);
330 bus_generic_attach(self);
336 fwohci_pci_detach(device_t self)
338 fwohci_softc_t *sc = device_get_softc(self);
344 fwohci_stop(sc, self);
346 bus_generic_detach(self);
349 device_delete_child(self, sc->fc.bdev);
353 /* disable interrupts that might have been switched on */
354 if (sc->bst && sc->bsh)
355 bus_space_write_4(sc->bst, sc->bsh,
356 FWOHCI_INTMASKCLR, OHCI_INT_EN);
361 err = bus_teardown_intr(self, sc->irq_res, sc->ih);
364 "Could not tear down irq, %d\n", err);
367 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
372 bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, sc->bsr);
378 fwohci_detach(sc, self);
379 mtx_destroy(FW_GMTX(&sc->fc));
386 fwohci_pci_suspend(device_t dev)
388 fwohci_softc_t *sc = device_get_softc(dev);
391 device_printf(dev, "fwohci_pci_suspend\n");
392 err = bus_generic_suspend(dev);
395 fwohci_stop(sc, dev);
400 fwohci_pci_resume(device_t dev)
402 fwohci_softc_t *sc = device_get_softc(dev);
404 fwohci_pci_init(dev);
405 fwohci_resume(sc, dev);
410 fwohci_pci_shutdown(device_t dev)
412 fwohci_softc_t *sc = device_get_softc(dev);
414 bus_generic_shutdown(dev);
415 fwohci_stop(sc, dev);
420 fwohci_pci_add_child(device_t dev, u_int order, const char *name, int unit)
422 struct fwohci_softc *sc;
426 sc = (struct fwohci_softc *)device_get_softc(dev);
427 child = device_add_child(dev, name, unit);
432 device_set_ivars(child, &sc->fc);
434 err = device_probe_and_attach(child);
436 device_printf(dev, "probe_and_attach failed with err=%d\n",
438 fwohci_pci_detach(dev);
439 device_delete_child(dev, child);
444 * Clear the bus reset event flag to start transactions even when
445 * interrupt is disabled during the boot process.
449 DELAY(250); /* 2 cycles */
451 fwohci_poll(&sc->fc, 0, -1);
458 static device_method_t fwohci_methods[] = {
459 /* Device interface */
460 DEVMETHOD(device_probe, fwohci_pci_probe),
461 DEVMETHOD(device_attach, fwohci_pci_attach),
462 DEVMETHOD(device_detach, fwohci_pci_detach),
463 DEVMETHOD(device_suspend, fwohci_pci_suspend),
464 DEVMETHOD(device_resume, fwohci_pci_resume),
465 DEVMETHOD(device_shutdown, fwohci_pci_shutdown),
468 DEVMETHOD(bus_add_child, fwohci_pci_add_child),
473 static driver_t fwohci_driver = {
476 sizeof(fwohci_softc_t),
479 static devclass_t fwohci_devclass;
482 MODULE_DEPEND(fwohci, firewire, 1, 1, 1);
484 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0);