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