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