Add support for the Adaptec AIC5800 based IEEE 1394 cards.
[dragonfly.git] / sys / bus / firewire / fwohci_pci.c
CommitLineData
984263bc
MD
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 *
78748771 34 * $FreeBSD: src/sys/dev/firewire/fwohci_pci.c,v 1.38 2004/01/23 17:37:09 simokawa Exp $
6f4f7256 35 * $DragonFly: src/sys/bus/firewire/fwohci_pci.c,v 1.9 2004/07/16 12:15:51 asmodai Exp $
984263bc
MD
36 */
37
38#define BOUNCE_BUFFER_TEST 0
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/module.h>
78748771 44#include <sys/conf.h>
984263bc
MD
45#include <sys/bus.h>
46#include <sys/queue.h>
47#include <machine/bus.h>
48#include <sys/rman.h>
49#include <sys/malloc.h>
90dec0df 50#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
78748771
JS
51#include <sys/lock.h>
52#include <sys/mutex.h>
53#endif
984263bc
MD
54#include <machine/resource.h>
55
90dec0df 56#if defined(__DragonFly__) || __FreeBSD_version < 500000
78748771
JS
57#include <machine/clock.h> /* for DELAY() */
58#endif
59
60#ifdef __DragonFly__
1f2de5d4
MD
61#include <bus/pci/pcivar.h>
62#include <bus/pci/pcireg.h>
984263bc 63
1f2de5d4
MD
64#include "firewire.h"
65#include "firewirereg.h"
984263bc 66
1f2de5d4
MD
67#include "fwdma.h"
68#include "fwohcireg.h"
69#include "fwohcivar.h"
78748771
JS
70#else
71#if __FreeBSD_version < 500000
72#include <pci/pcivar.h>
73#include <pci/pcireg.h>
74#else
75#include <dev/pci/pcivar.h>
76#include <dev/pci/pcireg.h>
77#endif
78
79#include <dev/firewire/firewire.h>
80#include <dev/firewire/firewirereg.h>
81
82#include <dev/firewire/fwdma.h>
83#include <dev/firewire/fwohcireg.h>
84#include <dev/firewire/fwohcivar.h>
85#endif
984263bc
MD
86
87static int fwohci_pci_attach(device_t self);
88static int fwohci_pci_detach(device_t self);
89
90/*
91 * The probe routine.
92 */
93static int
94fwohci_pci_probe( device_t dev )
95{
96#if 1
97 u_int32_t id;
98
99 id = pci_get_devid(dev);
100 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) {
101 device_set_desc(dev, "NEC uPD72861");
102 return 0;
103 }
104 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) {
105 device_set_desc(dev, "NEC uPD72871/2");
106 return 0;
107 }
108 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72870)) {
109 device_set_desc(dev, "NEC uPD72870");
110 return 0;
111 }
112 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72874)) {
113 device_set_desc(dev, "NEC uPD72874");
114 return 0;
115 }
116 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) {
117 device_set_desc(dev, "Texas Instruments TSB12LV22");
118 return 0;
119 }
120 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
121 device_set_desc(dev, "Texas Instruments TSB12LV23");
122 return 0;
123 }
124 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
125 device_set_desc(dev, "Texas Instruments TSB12LV26");
126 return 0;
127 }
128 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
129 device_set_desc(dev, "Texas Instruments TSB43AA22");
130 return 0;
131 }
132 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
133 device_set_desc(dev, "Texas Instruments TSB43AB22/A");
134 return 0;
135 }
6256cfa5
JR
136 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB21)) {
137 device_set_desc(dev, "Texas Instruments TSB43AB21/A/AI/A-EP");
af93067d
JR
138 return 0;
139 }
984263bc
MD
140 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) {
141 device_set_desc(dev, "Texas Instruments TSB43AB23");
142 return 0;
143 }
78748771
JS
144 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) {
145 device_set_desc(dev, "Texas Instruments TSB82AA2");
146 return 0;
147 }
984263bc
MD
148 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
149 device_set_desc(dev, "Texas Instruments PCI4450");
150 return 0;
151 }
152 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
153 device_set_desc(dev, "Texas Instruments PCI4410A");
154 return 0;
155 }
156 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) {
157 device_set_desc(dev, "Texas Instruments PCI4451");
158 return 0;
159 }
160 if (id == (FW_VENDORID_SONY | FW_DEVICE_CX3022)) {
161 device_set_desc(dev, "Sony CX3022");
162 return 0;
163 }
164 if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
165 device_set_desc(dev, "VIA VT6306");
166 return 0;
167 }
168 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) {
169 device_set_desc(dev, "Ricoh R5C551");
170 return 0;
171 }
172 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
173 device_set_desc(dev, "Ricoh R5C552");
174 return 0;
175 }
176 if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
177 device_set_desc(dev, "Apple Pangea");
178 return 0;
179 }
180 if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) {
181 device_set_desc(dev, "Apple UniNorth");
182 return 0;
183 }
184 if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
185 device_set_desc(dev, "Lucent FW322/323");
186 return 0;
187 }
169a6807
JR
188 if (id == (FW_VENDORID_INTEL | FW_DEVICE_82372FB)) {
189 device_set_desc(dev, "Intel 82372FB");
190 return 0;
191 }
6f4f7256
JR
192 if (id == (FW_VENDORID_ADAPTEC | FW_DEVICE_AIC5800)) {
193 device_set_desc(dev, "Adaptec AHA-894x/AIC-5800");
194 return 0;
195 }
984263bc
MD
196#endif
197 if (pci_get_class(dev) == PCIC_SERIALBUS
198 && pci_get_subclass(dev) == PCIS_SERIALBUS_FW
199 && pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
200 device_printf(dev, "vendor=%x, dev=%x\n", pci_get_vendor(dev),
201 pci_get_device(dev));
202 device_set_desc(dev, "1394 Open Host Controller Interface");
203 return 0;
204 }
205
206 return ENXIO;
207}
208
90dec0df 209#if defined(__DragonFly__) || __FreeBSD_version < 500000
984263bc
MD
210static void
211fwohci_dummy_intr(void *arg)
212{
213 /* XXX do nothing */
214}
215#endif
216
217static int
218fwohci_pci_init(device_t self)
219{
220 int olatency, latency, ocache_line, cache_line;
221 u_int16_t cmd;
222
223 cmd = pci_read_config(self, PCIR_COMMAND, 2);
224 cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN |
225 PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN;
226#if 1
227 cmd &= ~PCIM_CMD_MWRICEN;
228#endif
229 pci_write_config(self, PCIR_COMMAND, cmd, 2);
230
231 latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1);
232#define DEF_LATENCY 0x20
233 if (olatency < DEF_LATENCY) {
234 latency = DEF_LATENCY;
235 pci_write_config(self, PCIR_LATTIMER, latency, 1);
236 }
237
238 cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
239#define DEF_CACHE_LINE 8
240 if (ocache_line < DEF_CACHE_LINE) {
241 cache_line = DEF_CACHE_LINE;
242 pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
243 }
244
245 if (firewire_debug) {
246 device_printf(self, "latency timer %d -> %d.\n",
247 olatency, latency);
248 device_printf(self, "cache size %d -> %d.\n",
249 ocache_line, cache_line);
250 }
251
252 return 0;
253}
254
255static int
256fwohci_pci_attach(device_t self)
257{
258 fwohci_softc_t *sc = device_get_softc(self);
259 int err;
78748771 260 int rid;
90dec0df 261#if defined(__DragonFly__) || __FreeBSD_version < 500000
984263bc
MD
262 int intr;
263 /* For the moment, put in a message stating what is wrong */
264 intr = pci_read_config(self, PCIR_INTLINE, 1);
265 if (intr == 0 || intr == 255) {
266 device_printf(self, "Invalid irq %d\n", intr);
267#ifdef __i386__
268 device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n");
269#endif
270 }
271#endif
272
273 if (bootverbose)
274 firewire_debug = bootverbose;
275
276 fwohci_pci_init(self);
277
278 rid = PCI_CBMEM;
279 sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
280 0, ~0, 1, RF_ACTIVE);
281 if (!sc->bsr) {
282 device_printf(self, "Could not map memory\n");
283 return ENXIO;
284 }
285
286 sc->bst = rman_get_bustag(sc->bsr);
287 sc->bsh = rman_get_bushandle(sc->bsr);
288
289 rid = 0;
290 sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
291 RF_SHAREABLE | RF_ACTIVE);
292 if (sc->irq_res == NULL) {
293 device_printf(self, "Could not allocate irq\n");
294 fwohci_pci_detach(self);
295 return ENXIO;
296 }
297
984263bc
MD
298
299 err = bus_setup_intr(self, sc->irq_res,
300#if FWOHCI_TASKQUEUE
301 INTR_TYPE_NET | INTR_MPSAFE,
302#else
303 INTR_TYPE_NET,
304#endif
305 (driver_intr_t *) fwohci_intr, sc, &sc->ih);
90dec0df 306#if defined(__DragonFly__) || __FreeBSD_version < 500000
984263bc
MD
307 /* XXX splcam() should mask this irq for sbp.c*/
308 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM,
309 (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_cam);
78748771
JS
310 /* XXX splbio() should mask this irq for physio()/fwmem_strategy() */
311 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_BIO,
312 (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_bio);
984263bc
MD
313#endif
314 if (err) {
315 device_printf(self, "Could not setup irq, %d\n", err);
316 fwohci_pci_detach(self);
317 return ENXIO;
318 }
319
320 err = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
321 /*boundary*/0,
322#if BOUNCE_BUFFER_TEST
323 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
324#else
325 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
326#endif
327 /*highaddr*/BUS_SPACE_MAXADDR,
328 /*filter*/NULL, /*filterarg*/NULL,
329 /*maxsize*/0x100000,
330 /*nsegments*/0x20,
331 /*maxsegsz*/0x8000,
332 /*flags*/BUS_DMA_ALLOCNOW,
90dec0df 333#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
78748771
JS
334 /*lockfunc*/busdma_lock_mutex,
335 /*lockarg*/&Giant,
336#endif
984263bc
MD
337 &sc->fc.dmat);
338 if (err != 0) {
339 printf("fwohci_pci_attach: Could not allocate DMA tag "
340 "- error %d\n", err);
341 return (ENOMEM);
342 }
343
344 err = fwohci_init(sc, self);
345
984263bc 346 if (err) {
78748771 347 device_printf(self, "fwohci_init failed with err=%d\n", err);
984263bc
MD
348 fwohci_pci_detach(self);
349 return EIO;
350 }
351
78748771
JS
352 /* probe and attach a child device(firewire) */
353 bus_generic_probe(self);
354 bus_generic_attach(self);
984263bc
MD
355
356 return 0;
357}
358
359static int
360fwohci_pci_detach(device_t self)
361{
362 fwohci_softc_t *sc = device_get_softc(self);
363 int s;
364
365
366 s = splfw();
367
78748771
JS
368 if (sc->bsr)
369 fwohci_stop(sc, self);
370
984263bc 371 bus_generic_detach(self);
78748771
JS
372 if (sc->fc.bdev) {
373 device_delete_child(self, sc->fc.bdev);
374 sc->fc.bdev = NULL;
375 }
984263bc
MD
376
377 /* disable interrupts that might have been switched on */
378 if (sc->bst && sc->bsh)
379 bus_space_write_4(sc->bst, sc->bsh,
380 FWOHCI_INTMASKCLR, OHCI_INT_EN);
381
382 if (sc->irq_res) {
383 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
384 if (err)
385 /* XXX or should we panic? */
386 device_printf(self, "Could not tear down irq, %d\n",
387 err);
90dec0df 388#if defined(__DragonFly__) || __FreeBSD_version < 500000
78748771
JS
389 bus_teardown_intr(self, sc->irq_res, sc->ih_cam);
390 bus_teardown_intr(self, sc->irq_res, sc->ih_bio);
984263bc
MD
391#endif
392 sc->ih = NULL;
393 }
394
984263bc
MD
395 if (sc->irq_res) {
396 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
397 sc->irq_res = NULL;
398 }
399
400 if (sc->bsr) {
401 bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr);
402 sc->bsr = NULL;
403 sc->bst = 0;
404 sc->bsh = 0;
405 }
406
407 fwohci_detach(sc, self);
408 splx(s);
409
410 return 0;
411}
412
413static int
414fwohci_pci_suspend(device_t dev)
415{
78748771 416 fwohci_softc_t *sc = device_get_softc(dev);
984263bc
MD
417 int err;
418
419 device_printf(dev, "fwohci_pci_suspend\n");
420 err = bus_generic_suspend(dev);
421 if (err)
422 return err;
78748771 423 fwohci_stop(sc, dev);
984263bc
MD
424 return 0;
425}
426
427static int
428fwohci_pci_resume(device_t dev)
429{
430 fwohci_softc_t *sc = device_get_softc(dev);
431
78748771 432#ifndef BURN_BRIDGES
984263bc
MD
433 device_printf(dev, "fwohci_pci_resume: power_state = 0x%08x\n",
434 pci_get_powerstate(dev));
78748771
JS
435 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
436#endif
984263bc
MD
437 fwohci_pci_init(dev);
438 fwohci_resume(sc, dev);
439 return 0;
440}
441
442static int
443fwohci_pci_shutdown(device_t dev)
444{
445 fwohci_softc_t *sc = device_get_softc(dev);
446
447 bus_generic_shutdown(dev);
448 fwohci_stop(sc, dev);
449 return 0;
450}
451
78748771
JS
452static device_t
453fwohci_pci_add_child(device_t dev, int order, const char *name, int unit)
454{
455 struct fwohci_softc *sc;
456 device_t child;
457 int s, err = 0;
458
459 sc = (struct fwohci_softc *)device_get_softc(dev);
460 child = device_add_child(dev, name, unit);
461 if (child == NULL)
462 return (child);
463
464 sc->fc.bdev = child;
465 device_set_ivars(child, (void *)&sc->fc);
466
467 err = device_probe_and_attach(child);
468 if (err) {
469 device_printf(dev, "probe_and_attach failed with err=%d\n",
470 err);
471 fwohci_pci_detach(dev);
472 device_delete_child(dev, child);
473 return NULL;
474 }
475
476 /* XXX
477 * Clear the bus reset event flag to start transactions even when
478 * interrupt is disabled during the boot process.
479 */
480 DELAY(250); /* 2 cycles */
481 s = splfw();
482 fwohci_poll((void *)sc, 0, -1);
483 splx(s);
484
485 return (child);
486}
487
984263bc
MD
488static device_method_t fwohci_methods[] = {
489 /* Device interface */
490 DEVMETHOD(device_probe, fwohci_pci_probe),
491 DEVMETHOD(device_attach, fwohci_pci_attach),
492 DEVMETHOD(device_detach, fwohci_pci_detach),
493 DEVMETHOD(device_suspend, fwohci_pci_suspend),
494 DEVMETHOD(device_resume, fwohci_pci_resume),
495 DEVMETHOD(device_shutdown, fwohci_pci_shutdown),
496
497 /* Bus interface */
78748771 498 DEVMETHOD(bus_add_child, fwohci_pci_add_child),
984263bc
MD
499 DEVMETHOD(bus_print_child, bus_generic_print_child),
500
501 { 0, 0 }
502};
503
504static driver_t fwohci_driver = {
505 "fwohci",
506 fwohci_methods,
507 sizeof(fwohci_softc_t),
508};
509
510static devclass_t fwohci_devclass;
511
78748771
JS
512#ifdef FWOHCI_MODULE
513MODULE_DEPEND(fwohci, firewire, 1, 1, 1);
514#endif
984263bc
MD
515DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0);
516DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0);