Get rid of bus_{disable,enable}_intr(), it wasn't generic enough for
[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 $
e9cb6d99 35 * $DragonFly: src/sys/bus/firewire/fwohci_pci.c,v 1.17 2005/05/24 20:58:45 dillon 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);
88436e3a
JR
100 if (id == (FW_VENDORID_NATSEMI | FW_DEVICE_CS4210)) {
101 device_set_desc(dev, "National Semiconductor CS4210");
102 return 0;
103 }
984263bc
MD
104 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) {
105 device_set_desc(dev, "NEC uPD72861");
106 return 0;
107 }
108 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) {
109 device_set_desc(dev, "NEC uPD72871/2");
110 return 0;
111 }
112 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72870)) {
113 device_set_desc(dev, "NEC uPD72870");
114 return 0;
115 }
116 if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72874)) {
117 device_set_desc(dev, "NEC uPD72874");
118 return 0;
119 }
3672879e
JR
120 if (id == (FW_VENDORID_SIS | FW_DEVICE_7007)) {
121 /* It has no real identifier, using device id. */
122 device_set_desc(dev, "SiS 7007");
123 return 0;
124 }
984263bc
MD
125 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) {
126 device_set_desc(dev, "Texas Instruments TSB12LV22");
127 return 0;
128 }
129 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
130 device_set_desc(dev, "Texas Instruments TSB12LV23");
131 return 0;
132 }
133 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
134 device_set_desc(dev, "Texas Instruments TSB12LV26");
135 return 0;
136 }
137 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
138 device_set_desc(dev, "Texas Instruments TSB43AA22");
139 return 0;
140 }
141 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
142 device_set_desc(dev, "Texas Instruments TSB43AB22/A");
143 return 0;
144 }
6256cfa5
JR
145 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB21)) {
146 device_set_desc(dev, "Texas Instruments TSB43AB21/A/AI/A-EP");
af93067d
JR
147 return 0;
148 }
984263bc
MD
149 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) {
150 device_set_desc(dev, "Texas Instruments TSB43AB23");
151 return 0;
152 }
78748771
JS
153 if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) {
154 device_set_desc(dev, "Texas Instruments TSB82AA2");
155 return 0;
156 }
984263bc
MD
157 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
158 device_set_desc(dev, "Texas Instruments PCI4450");
159 return 0;
160 }
161 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
162 device_set_desc(dev, "Texas Instruments PCI4410A");
163 return 0;
164 }
165 if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) {
166 device_set_desc(dev, "Texas Instruments PCI4451");
167 return 0;
168 }
82a871e4 169 if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD1947)) {
85fe3d34 170 device_set_desc(dev, "Sony i.LINK (CXD1947)");
82a871e4
JR
171 return 0;
172 }
cd7e33c7 173 if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD3222)) {
85fe3d34 174 device_set_desc(dev, "Sony i.LINK (CXD3222)");
984263bc
MD
175 return 0;
176 }
177 if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
f94b333c 178 device_set_desc(dev, "VIA Fire II (VT6306)");
984263bc
MD
179 return 0;
180 }
181 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) {
182 device_set_desc(dev, "Ricoh R5C551");
183 return 0;
184 }
185 if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
186 device_set_desc(dev, "Ricoh R5C552");
187 return 0;
188 }
189 if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
190 device_set_desc(dev, "Apple Pangea");
191 return 0;
192 }
193 if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) {
194 device_set_desc(dev, "Apple UniNorth");
195 return 0;
196 }
197 if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
198 device_set_desc(dev, "Lucent FW322/323");
199 return 0;
200 }
169a6807
JR
201 if (id == (FW_VENDORID_INTEL | FW_DEVICE_82372FB)) {
202 device_set_desc(dev, "Intel 82372FB");
203 return 0;
204 }
6f4f7256
JR
205 if (id == (FW_VENDORID_ADAPTEC | FW_DEVICE_AIC5800)) {
206 device_set_desc(dev, "Adaptec AHA-894x/AIC-5800");
207 return 0;
208 }
984263bc
MD
209#endif
210 if (pci_get_class(dev) == PCIC_SERIALBUS
211 && pci_get_subclass(dev) == PCIS_SERIALBUS_FW
212 && pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
213 device_printf(dev, "vendor=%x, dev=%x\n", pci_get_vendor(dev),
214 pci_get_device(dev));
215 device_set_desc(dev, "1394 Open Host Controller Interface");
216 return 0;
217 }
218
219 return ENXIO;
220}
221
90dec0df 222#if defined(__DragonFly__) || __FreeBSD_version < 500000
984263bc
MD
223static void
224fwohci_dummy_intr(void *arg)
225{
226 /* XXX do nothing */
227}
228#endif
229
230static int
231fwohci_pci_init(device_t self)
232{
233 int olatency, latency, ocache_line, cache_line;
234 u_int16_t cmd;
235
60955de8
MD
236 /*
237 * Clear PCIM_CMD_MWRICEN as per FreeBSD/1.20, but note that the
238 * problem may have been related to SERR and PERR being
239 * unconditionally enabled in that rev.
240 *
241 * Do not change the SERRESPEN or PERRESPEN bits. Use the BIOS
242 * values (probably off). This crashes some machines in fwohci_init().
243 *
244 * The theory here is that the device may not be properly initializing
245 * its on-chip memory, leaving some of it in a parity errored state,
246 * and enabling parity check may result in the device blowing up.
247 * It's also possible that some hardware is just plain broken. OpenBSD
248 * does not turn on SERRESPEN or PERRESPEN so we won't either.
249 */
984263bc 250 cmd = pci_read_config(self, PCIR_COMMAND, 2);
60955de8 251 cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN;
984263bc 252 cmd &= ~PCIM_CMD_MWRICEN;
984263bc
MD
253 pci_write_config(self, PCIR_COMMAND, cmd, 2);
254
255 latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1);
256#define DEF_LATENCY 0x20
257 if (olatency < DEF_LATENCY) {
258 latency = DEF_LATENCY;
259 pci_write_config(self, PCIR_LATTIMER, latency, 1);
260 }
261
262 cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
263#define DEF_CACHE_LINE 8
264 if (ocache_line < DEF_CACHE_LINE) {
265 cache_line = DEF_CACHE_LINE;
266 pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
267 }
268
269 if (firewire_debug) {
270 device_printf(self, "latency timer %d -> %d.\n",
271 olatency, latency);
272 device_printf(self, "cache size %d -> %d.\n",
273 ocache_line, cache_line);
274 }
275
276 return 0;
277}
278
279static int
280fwohci_pci_attach(device_t self)
281{
282 fwohci_softc_t *sc = device_get_softc(self);
283 int err;
78748771 284 int rid;
90dec0df 285#if defined(__DragonFly__) || __FreeBSD_version < 500000
984263bc
MD
286 int intr;
287 /* For the moment, put in a message stating what is wrong */
288 intr = pci_read_config(self, PCIR_INTLINE, 1);
289 if (intr == 0 || intr == 255) {
290 device_printf(self, "Invalid irq %d\n", intr);
291#ifdef __i386__
292 device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n");
293#endif
294 }
295#endif
296
297 if (bootverbose)
298 firewire_debug = bootverbose;
299
300 fwohci_pci_init(self);
301
302 rid = PCI_CBMEM;
303 sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
304 0, ~0, 1, RF_ACTIVE);
305 if (!sc->bsr) {
306 device_printf(self, "Could not map memory\n");
307 return ENXIO;
308 }
309
310 sc->bst = rman_get_bustag(sc->bsr);
311 sc->bsh = rman_get_bushandle(sc->bsr);
312
313 rid = 0;
314 sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
315 RF_SHAREABLE | RF_ACTIVE);
316 if (sc->irq_res == NULL) {
317 device_printf(self, "Could not allocate irq\n");
318 fwohci_pci_detach(self);
319 return ENXIO;
320 }
321
984263bc
MD
322
323 err = bus_setup_intr(self, sc->irq_res,
324#if FWOHCI_TASKQUEUE
325 INTR_TYPE_NET | INTR_MPSAFE,
326#else
327 INTR_TYPE_NET,
328#endif
e9cb6d99 329 (driver_intr_t *) fwohci_intr, sc, &sc->ih, NULL);
90dec0df 330#if defined(__DragonFly__) || __FreeBSD_version < 500000
984263bc
MD
331 /* XXX splcam() should mask this irq for sbp.c*/
332 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM,
e9cb6d99
MD
333 (driver_intr_t *) fwohci_dummy_intr, sc,
334 &sc->ih_cam, NULL);
78748771
JS
335 /* XXX splbio() should mask this irq for physio()/fwmem_strategy() */
336 err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_BIO,
e9cb6d99
MD
337 (driver_intr_t *) fwohci_dummy_intr, sc,
338 &sc->ih_bio, NULL);
984263bc
MD
339#endif
340 if (err) {
341 device_printf(self, "Could not setup irq, %d\n", err);
342 fwohci_pci_detach(self);
343 return ENXIO;
344 }
345
346 err = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
347 /*boundary*/0,
348#if BOUNCE_BUFFER_TEST
349 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
350#else
351 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
352#endif
353 /*highaddr*/BUS_SPACE_MAXADDR,
354 /*filter*/NULL, /*filterarg*/NULL,
355 /*maxsize*/0x100000,
356 /*nsegments*/0x20,
357 /*maxsegsz*/0x8000,
358 /*flags*/BUS_DMA_ALLOCNOW,
90dec0df 359#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
78748771
JS
360 /*lockfunc*/busdma_lock_mutex,
361 /*lockarg*/&Giant,
362#endif
984263bc
MD
363 &sc->fc.dmat);
364 if (err != 0) {
365 printf("fwohci_pci_attach: Could not allocate DMA tag "
366 "- error %d\n", err);
367 return (ENOMEM);
368 }
369
370 err = fwohci_init(sc, self);
371
984263bc 372 if (err) {
78748771 373 device_printf(self, "fwohci_init failed with err=%d\n", err);
984263bc
MD
374 fwohci_pci_detach(self);
375 return EIO;
376 }
377
78748771
JS
378 /* probe and attach a child device(firewire) */
379 bus_generic_probe(self);
380 bus_generic_attach(self);
984263bc
MD
381
382 return 0;
383}
384
385static int
386fwohci_pci_detach(device_t self)
387{
388 fwohci_softc_t *sc = device_get_softc(self);
389 int s;
390
391
392 s = splfw();
393
78748771
JS
394 if (sc->bsr)
395 fwohci_stop(sc, self);
396
984263bc 397 bus_generic_detach(self);
78748771
JS
398 if (sc->fc.bdev) {
399 device_delete_child(self, sc->fc.bdev);
400 sc->fc.bdev = NULL;
401 }
984263bc
MD
402
403 /* disable interrupts that might have been switched on */
404 if (sc->bst && sc->bsh)
405 bus_space_write_4(sc->bst, sc->bsh,
406 FWOHCI_INTMASKCLR, OHCI_INT_EN);
407
408 if (sc->irq_res) {
409 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
410 if (err)
411 /* XXX or should we panic? */
412 device_printf(self, "Could not tear down irq, %d\n",
413 err);
90dec0df 414#if defined(__DragonFly__) || __FreeBSD_version < 500000
78748771
JS
415 bus_teardown_intr(self, sc->irq_res, sc->ih_cam);
416 bus_teardown_intr(self, sc->irq_res, sc->ih_bio);
984263bc
MD
417#endif
418 sc->ih = NULL;
419 }
420
984263bc
MD
421 if (sc->irq_res) {
422 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
423 sc->irq_res = NULL;
424 }
425
426 if (sc->bsr) {
427 bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr);
428 sc->bsr = NULL;
429 sc->bst = 0;
430 sc->bsh = 0;
431 }
432
433 fwohci_detach(sc, self);
434 splx(s);
435
436 return 0;
437}
438
439static int
440fwohci_pci_suspend(device_t dev)
441{
78748771 442 fwohci_softc_t *sc = device_get_softc(dev);
984263bc
MD
443 int err;
444
445 device_printf(dev, "fwohci_pci_suspend\n");
446 err = bus_generic_suspend(dev);
447 if (err)
448 return err;
78748771 449 fwohci_stop(sc, dev);
984263bc
MD
450 return 0;
451}
452
453static int
454fwohci_pci_resume(device_t dev)
455{
456 fwohci_softc_t *sc = device_get_softc(dev);
457
78748771 458#ifndef BURN_BRIDGES
984263bc
MD
459 device_printf(dev, "fwohci_pci_resume: power_state = 0x%08x\n",
460 pci_get_powerstate(dev));
78748771
JS
461 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
462#endif
984263bc
MD
463 fwohci_pci_init(dev);
464 fwohci_resume(sc, dev);
465 return 0;
466}
467
468static int
469fwohci_pci_shutdown(device_t dev)
470{
471 fwohci_softc_t *sc = device_get_softc(dev);
472
473 bus_generic_shutdown(dev);
474 fwohci_stop(sc, dev);
475 return 0;
476}
477
78748771
JS
478static device_t
479fwohci_pci_add_child(device_t dev, int order, const char *name, int unit)
480{
481 struct fwohci_softc *sc;
482 device_t child;
483 int s, err = 0;
484
485 sc = (struct fwohci_softc *)device_get_softc(dev);
486 child = device_add_child(dev, name, unit);
487 if (child == NULL)
488 return (child);
489
490 sc->fc.bdev = child;
491 device_set_ivars(child, (void *)&sc->fc);
492
493 err = device_probe_and_attach(child);
494 if (err) {
495 device_printf(dev, "probe_and_attach failed with err=%d\n",
496 err);
497 fwohci_pci_detach(dev);
498 device_delete_child(dev, child);
499 return NULL;
500 }
501
502 /* XXX
503 * Clear the bus reset event flag to start transactions even when
504 * interrupt is disabled during the boot process.
505 */
506 DELAY(250); /* 2 cycles */
507 s = splfw();
508 fwohci_poll((void *)sc, 0, -1);
509 splx(s);
510
511 return (child);
512}
513
984263bc
MD
514static device_method_t fwohci_methods[] = {
515 /* Device interface */
516 DEVMETHOD(device_probe, fwohci_pci_probe),
517 DEVMETHOD(device_attach, fwohci_pci_attach),
518 DEVMETHOD(device_detach, fwohci_pci_detach),
519 DEVMETHOD(device_suspend, fwohci_pci_suspend),
520 DEVMETHOD(device_resume, fwohci_pci_resume),
521 DEVMETHOD(device_shutdown, fwohci_pci_shutdown),
522
523 /* Bus interface */
78748771 524 DEVMETHOD(bus_add_child, fwohci_pci_add_child),
984263bc
MD
525 DEVMETHOD(bus_print_child, bus_generic_print_child),
526
527 { 0, 0 }
528};
529
530static driver_t fwohci_driver = {
531 "fwohci",
532 fwohci_methods,
533 sizeof(fwohci_softc_t),
534};
535
536static devclass_t fwohci_devclass;
537
78748771
JS
538#ifdef FWOHCI_MODULE
539MODULE_DEPEND(fwohci, firewire, 1, 1, 1);
540#endif
984263bc
MD
541DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0);
542DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0);