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>
49 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
51 #include <sys/mutex.h>
53 #include <machine/resource.h>
55 #if defined(__DragonFly__) || __FreeBSD_version < 500000
56 #include <machine/clock.h> /* for DELAY() */
60 #include <bus/pci/pcivar.h>
61 #include <bus/pci/pcireg.h>
64 #include "firewirereg.h"
67 #include "fwohcireg.h"
68 #include "fwohcivar.h"
70 #if __FreeBSD_version < 500000
71 #include <pci/pcivar.h>
72 #include <pci/pcireg.h>
74 #include <dev/pci/pcivar.h>
75 #include <dev/pci/pcireg.h>
78 #include <dev/firewire/firewire.h>
79 #include <dev/firewire/firewirereg.h>
81 #include <dev/firewire/fwdma.h>
82 #include <dev/firewire/fwohcireg.h>
83 #include <dev/firewire/fwohcivar.h>
86 static int fwohci_pci_attach(device_t self);
87 static int fwohci_pci_detach(device_t self);
93 fwohci_pci_probe( device_t dev )
98 id = pci_get_devid(dev);
99 if (id == (FW_VENDORID_NATSEMI | FW_DEVICE_CS4210)) {
100 device_set_desc(dev, "National Semiconductor CS4210");
101 return BUS_PROBE_DEFAULT;
103 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) {
104 device_set_desc(dev, "NEC uPD72861");
105 return BUS_PROBE_DEFAULT;
107 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) {
108 device_set_desc(dev, "NEC uPD72871/2");
109 return BUS_PROBE_DEFAULT;
111 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72870)) {
112 device_set_desc(dev, "NEC uPD72870");
113 return BUS_PROBE_DEFAULT;
115 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72873)) {
116 device_set_desc(dev, "NEC uPD72873");
117 return BUS_PROBE_DEFAULT;
119 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72874)) {
120 device_set_desc(dev, "NEC uPD72874");
121 return BUS_PROBE_DEFAULT;
123 if (id == (FW_VENDORID_SIS | FW_DEVICE_7007)) {
124 /* It has no real identifier, using device id. */
125 device_set_desc(dev, "SiS 7007");
126 return BUS_PROBE_DEFAULT;
128 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) {
129 device_set_desc(dev, "Texas Instruments TSB12LV22");
130 return BUS_PROBE_DEFAULT;
132 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
133 device_set_desc(dev, "Texas Instruments TSB12LV23");
134 return BUS_PROBE_DEFAULT;
136 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
137 device_set_desc(dev, "Texas Instruments TSB12LV26");
138 return BUS_PROBE_DEFAULT;
140 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
141 device_set_desc(dev, "Texas Instruments TSB43AA22");
142 return BUS_PROBE_DEFAULT;
144 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
145 device_set_desc(dev, "Texas Instruments TSB43AB22/A");
146 return BUS_PROBE_DEFAULT;
148 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB21)) {
149 device_set_desc(dev, "Texas Instruments TSB43AB21/A/AI/A-EP");
150 return BUS_PROBE_DEFAULT;
152 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) {
153 device_set_desc(dev, "Texas Instruments TSB43AB23");
154 return BUS_PROBE_DEFAULT;
156 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) {
157 device_set_desc(dev, "Texas Instruments TSB82AA2");
158 return BUS_PROBE_DEFAULT;
160 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
161 device_set_desc(dev, "Texas Instruments PCI4450");
162 return BUS_PROBE_DEFAULT;
164 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
165 device_set_desc(dev, "Texas Instruments PCI4410A");
166 return BUS_PROBE_DEFAULT;
168 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) {
169 device_set_desc(dev, "Texas Instruments PCI4451");
170 return BUS_PROBE_DEFAULT;
172 if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD1947)) {
173 device_printf(dev, "Sony i.LINK (CXD1947) not supported\n");
176 if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD3222)) {
177 device_set_desc(dev, "Sony i.LINK (CXD3222)");
178 return BUS_PROBE_DEFAULT;
180 if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
181 device_set_desc(dev, "VIA Fire II (VT6306)");
182 return BUS_PROBE_DEFAULT;
184 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) {
185 device_set_desc(dev, "Ricoh R5C551");
186 return BUS_PROBE_DEFAULT;
188 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
189 device_set_desc(dev, "Ricoh R5C552");
190 return BUS_PROBE_DEFAULT;
192 if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
193 device_set_desc(dev, "Apple Pangea");
194 return BUS_PROBE_DEFAULT;
196 if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) {
197 device_set_desc(dev, "Apple UniNorth");
198 return BUS_PROBE_DEFAULT;
200 if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
201 device_set_desc(dev, "Lucent FW322/323");
202 return BUS_PROBE_DEFAULT;
204 if (id == (FW_VENDORID_INTEL | FW_DEVICE_82372FB)) {
205 device_set_desc(dev, "Intel 82372FB");
206 return BUS_PROBE_DEFAULT;
208 if (id == (FW_VENDORID_ADAPTEC | FW_DEVICE_AIC5800)) {
209 device_set_desc(dev, "Adaptec AHA-894x/AIC-5800");
210 return BUS_PROBE_DEFAULT;
212 if (id == (FW_VENDORID_SUN | FW_DEVICE_PCIO2FW)) {
213 device_set_desc(dev, "Sun PCIO-2");
214 return BUS_PROBE_DEFAULT;
217 if (pci_get_class(dev) == PCIC_SERIALBUS
218 && pci_get_subclass(dev) == PCIS_SERIALBUS_FW
219 && pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
221 device_printf(dev, "vendor=%x, dev=%x\n",
222 pci_get_vendor(dev), pci_get_device(dev));
223 device_set_desc(dev, "1394 Open Host Controller Interface");
224 return BUS_PROBE_DEFAULT;
230 #if defined(__DragonFly__) || __FreeBSD_version < 500000
232 fwohci_dummy_intr(void *arg)
239 fwohci_pci_init(device_t self)
241 int olatency, latency, ocache_line, cache_line;
244 cmd = pci_read_config(self, PCIR_COMMAND, 2);
245 cmd |= PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN;
246 #if 1 /* for broken hardware */
247 cmd &= ~PCIM_CMD_MWRICEN;
249 pci_write_config(self, PCIR_COMMAND, cmd, 2);
252 * Some Sun PCIO-2 FireWire controllers have their intpin register
253 * bogusly set to 0, although it should be 3. Correct that.
255 if (pci_get_devid(self) == (FW_VENDORID_SUN | FW_DEVICE_PCIO2FW) &&
256 pci_get_intpin(self) == 0)
257 pci_set_intpin(self, 3);
259 latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1);
260 #define DEF_LATENCY 0x20
261 if (olatency < DEF_LATENCY) {
262 latency = DEF_LATENCY;
263 pci_write_config(self, PCIR_LATTIMER, latency, 1);
266 cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
267 #define DEF_CACHE_LINE 8
268 if (ocache_line < DEF_CACHE_LINE) {
269 cache_line = DEF_CACHE_LINE;
270 pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
273 if (firewire_debug) {
274 device_printf(self, "latency timer %d -> %d.\n",
276 device_printf(self, "cache size %d -> %d.\n",
277 ocache_line, cache_line);
284 fwohci_pci_attach(device_t self)
286 fwohci_softc_t *sc = device_get_softc(self);
289 #if defined(__DragonFly__) || __FreeBSD_version < 500000
291 /* For the moment, put in a message stating what is wrong */
292 intr = pci_read_config(self, PCIR_INTLINE, 1);
293 if (intr == 0 || intr == 255) {
294 device_printf(self, "Invalid irq %d\n", intr);
296 device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n");
303 firewire_debug = bootverbose;
306 mtx_init(FW_GMTX(&sc->fc), "firewire", NULL, MTX_DEF);
307 fwohci_pci_init(self);
310 #if __FreeBSD_version >= 502109
311 sc->bsr = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
313 sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
314 0, ~0, 1, RF_ACTIVE);
317 device_printf(self, "Could not map memory\n");
321 sc->bst = rman_get_bustag(sc->bsr);
322 sc->bsh = rman_get_bushandle(sc->bsr);
325 #if __FreeBSD_version >= 502109
326 sc->irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
327 RF_SHAREABLE | RF_ACTIVE);
329 sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
330 RF_SHAREABLE | RF_ACTIVE);
332 if (sc->irq_res == NULL) {
333 device_printf(self, "Could not allocate irq\n");
334 fwohci_pci_detach(self);
338 err = bus_setup_intr(self, sc->irq_res,
339 INTR_TYPE_NET | INTR_MPSAFE,
340 NULL, (driver_intr_t *) fwohci_intr,
343 #if defined(__DragonFly__) || __FreeBSD_version < 500000
344 /* XXX splcam() should mask this irq for sbp.c*/
345 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM,
346 (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_cam);
347 /* XXX splbio() should mask this irq for physio()/fwmem_strategy() */
348 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_BIO,
349 (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_bio);
352 device_printf(self, "Could not setup irq, %d\n", err);
353 fwohci_pci_detach(self);
357 err = bus_dma_tag_create(
358 #if defined(__FreeBSD__) && __FreeBSD_version >= 700020
359 /*parent*/bus_get_dma_tag(self),
365 #if BOUNCE_BUFFER_TEST
366 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
368 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
370 /*highaddr*/BUS_SPACE_MAXADDR,
371 /*filter*/NULL, /*filterarg*/NULL,
375 /*flags*/BUS_DMA_ALLOCNOW,
376 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
377 /*lockfunc*/busdma_lock_mutex,
378 /*lockarg*/FW_GMTX(&sc->fc),
382 printf("fwohci_pci_attach: Could not allocate DMA tag "
383 "- error %d\n", err);
387 err = fwohci_init(sc, self);
390 device_printf(self, "fwohci_init failed with err=%d\n", err);
391 fwohci_pci_detach(self);
395 /* probe and attach a child device(firewire) */
396 bus_generic_probe(self);
397 bus_generic_attach(self);
403 fwohci_pci_detach(device_t self)
405 fwohci_softc_t *sc = device_get_softc(self);
412 fwohci_stop(sc, self);
414 bus_generic_detach(self);
416 device_delete_child(self, sc->fc.bdev);
420 /* disable interrupts that might have been switched on */
421 if (sc->bst && sc->bsh)
422 bus_space_write_4(sc->bst, sc->bsh,
423 FWOHCI_INTMASKCLR, OHCI_INT_EN);
428 err = bus_teardown_intr(self, sc->irq_res, sc->ih);
431 "Could not tear down irq, %d\n", err);
432 #if defined(__DragonFly__) || __FreeBSD_version < 500000
433 bus_teardown_intr(self, sc->irq_res, sc->ih_cam);
434 bus_teardown_intr(self, sc->irq_res, sc->ih_bio);
438 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
443 bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr);
449 fwohci_detach(sc, self);
450 mtx_destroy(FW_GMTX(&sc->fc));
457 fwohci_pci_suspend(device_t dev)
459 fwohci_softc_t *sc = device_get_softc(dev);
462 device_printf(dev, "fwohci_pci_suspend\n");
463 err = bus_generic_suspend(dev);
466 fwohci_stop(sc, dev);
471 fwohci_pci_resume(device_t dev)
473 fwohci_softc_t *sc = device_get_softc(dev);
475 fwohci_pci_init(dev);
476 fwohci_resume(sc, dev);
481 fwohci_pci_shutdown(device_t dev)
483 fwohci_softc_t *sc = device_get_softc(dev);
485 bus_generic_shutdown(dev);
486 fwohci_stop(sc, dev);
491 fwohci_pci_add_child(device_t dev, u_int order, const char *name, int unit)
493 struct fwohci_softc *sc;
497 sc = (struct fwohci_softc *)device_get_softc(dev);
498 child = device_add_child(dev, name, unit);
503 device_set_ivars(child, (void *)&sc->fc);
505 err = device_probe_and_attach(child);
507 device_printf(dev, "probe_and_attach failed with err=%d\n",
509 fwohci_pci_detach(dev);
510 device_delete_child(dev, child);
515 * Clear the bus reset event flag to start transactions even when
516 * interrupt is disabled during the boot process.
520 DELAY(250); /* 2 cycles */
522 fwohci_poll((void *)sc, 0, -1);
529 static device_method_t fwohci_methods[] = {
530 /* Device interface */
531 DEVMETHOD(device_probe, fwohci_pci_probe),
532 DEVMETHOD(device_attach, fwohci_pci_attach),
533 DEVMETHOD(device_detach, fwohci_pci_detach),
534 DEVMETHOD(device_suspend, fwohci_pci_suspend),
535 DEVMETHOD(device_resume, fwohci_pci_resume),
536 DEVMETHOD(device_shutdown, fwohci_pci_shutdown),
539 DEVMETHOD(bus_add_child, fwohci_pci_add_child),
544 static driver_t fwohci_driver = {
547 sizeof(fwohci_softc_t),
550 static devclass_t fwohci_devclass;
553 MODULE_DEPEND(fwohci, firewire, 1, 1, 1);
555 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0);