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