Add support for the Texas Instruments IEEE 1394 controllers designated by:
[dragonfly.git] / sys / bus / 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: src/sys/dev/firewire/fwohci_pci.c,v 1.38 2004/01/23 17:37:09 simokawa Exp $
35  * $DragonFly: src/sys/bus/firewire/fwohci_pci.c,v 1.6 2004/07/16 08:58:44 asmodai Exp $
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>
44 #include <sys/conf.h>
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>
50 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
51 #include <sys/lock.h>
52 #include <sys/mutex.h>
53 #endif
54 #include <machine/resource.h>
55
56 #if defined(__DragonFly__) || __FreeBSD_version < 500000
57 #include <machine/clock.h>              /* for DELAY() */
58 #endif
59
60 #ifdef __DragonFly__
61 #include <bus/pci/pcivar.h>
62 #include <bus/pci/pcireg.h>
63
64 #include "firewire.h"
65 #include "firewirereg.h"
66
67 #include "fwdma.h"
68 #include "fwohcireg.h"
69 #include "fwohcivar.h"
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
86
87 static int fwohci_pci_attach(device_t self);
88 static int fwohci_pci_detach(device_t self);
89
90 /*
91  * The probe routine.
92  */
93 static int
94 fwohci_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         }
136         if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB21A)) {
137                 device_set_desc(dev, "Texas Instruments TSB43AB21A");
138                 return 0;
139         }
140         if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) {
141                 device_set_desc(dev, "Texas Instruments TSB43AB23");
142                 return 0;
143         }
144         if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) {
145                 device_set_desc(dev, "Texas Instruments TSB82AA2");
146                 return 0;
147         }
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         }
188 #endif
189         if (pci_get_class(dev) == PCIC_SERIALBUS
190                         && pci_get_subclass(dev) == PCIS_SERIALBUS_FW
191                         && pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
192                 device_printf(dev, "vendor=%x, dev=%x\n", pci_get_vendor(dev),
193                         pci_get_device(dev));
194                 device_set_desc(dev, "1394 Open Host Controller Interface");
195                 return 0;
196         }
197
198         return ENXIO;
199 }
200
201 #if defined(__DragonFly__) || __FreeBSD_version < 500000
202 static void
203 fwohci_dummy_intr(void *arg)
204 {
205         /* XXX do nothing */
206 }
207 #endif
208
209 static int
210 fwohci_pci_init(device_t self)
211 {
212         int olatency, latency, ocache_line, cache_line;
213         u_int16_t cmd;
214
215         cmd = pci_read_config(self, PCIR_COMMAND, 2);
216         cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN |
217                 PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN;
218 #if 1
219         cmd &= ~PCIM_CMD_MWRICEN; 
220 #endif
221         pci_write_config(self, PCIR_COMMAND, cmd, 2);
222
223         latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1);
224 #define DEF_LATENCY 0x20
225         if (olatency < DEF_LATENCY) {
226                 latency = DEF_LATENCY;
227                 pci_write_config(self, PCIR_LATTIMER, latency, 1);
228         }
229
230         cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
231 #define DEF_CACHE_LINE 8
232         if (ocache_line < DEF_CACHE_LINE) {
233                 cache_line = DEF_CACHE_LINE;
234                 pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
235         }
236
237         if (firewire_debug) {
238                 device_printf(self, "latency timer %d -> %d.\n",
239                         olatency, latency);
240                 device_printf(self, "cache size %d -> %d.\n",
241                         ocache_line, cache_line);
242         }
243
244         return 0;
245 }
246
247 static int
248 fwohci_pci_attach(device_t self)
249 {
250         fwohci_softc_t *sc = device_get_softc(self);
251         int err;
252         int rid;
253 #if defined(__DragonFly__) || __FreeBSD_version < 500000
254         int intr;
255         /* For the moment, put in a message stating what is wrong */
256         intr = pci_read_config(self, PCIR_INTLINE, 1);
257         if (intr == 0 || intr == 255) {
258                 device_printf(self, "Invalid irq %d\n", intr);
259 #ifdef __i386__
260                 device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n");
261 #endif
262         }
263 #endif
264
265         if (bootverbose)
266                 firewire_debug = bootverbose;
267
268         fwohci_pci_init(self);
269
270         rid = PCI_CBMEM;
271         sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
272                                         0, ~0, 1, RF_ACTIVE);
273         if (!sc->bsr) {
274                 device_printf(self, "Could not map memory\n");
275                 return ENXIO;
276         }
277
278         sc->bst = rman_get_bustag(sc->bsr);
279         sc->bsh = rman_get_bushandle(sc->bsr);
280
281         rid = 0;
282         sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
283                                      RF_SHAREABLE | RF_ACTIVE);
284         if (sc->irq_res == NULL) {
285                 device_printf(self, "Could not allocate irq\n");
286                 fwohci_pci_detach(self);
287                 return ENXIO;
288         }
289
290
291         err = bus_setup_intr(self, sc->irq_res,
292 #if FWOHCI_TASKQUEUE
293                         INTR_TYPE_NET | INTR_MPSAFE,
294 #else
295                         INTR_TYPE_NET,
296 #endif
297                      (driver_intr_t *) fwohci_intr, sc, &sc->ih);
298 #if defined(__DragonFly__) || __FreeBSD_version < 500000
299         /* XXX splcam() should mask this irq for sbp.c*/
300         err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM,
301                      (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_cam);
302         /* XXX splbio() should mask this irq for physio()/fwmem_strategy() */
303         err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_BIO,
304                      (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_bio);
305 #endif
306         if (err) {
307                 device_printf(self, "Could not setup irq, %d\n", err);
308                 fwohci_pci_detach(self);
309                 return ENXIO;
310         }
311
312         err = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
313                                 /*boundary*/0,
314 #if BOUNCE_BUFFER_TEST
315                                 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
316 #else
317                                 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
318 #endif
319                                 /*highaddr*/BUS_SPACE_MAXADDR,
320                                 /*filter*/NULL, /*filterarg*/NULL,
321                                 /*maxsize*/0x100000,
322                                 /*nsegments*/0x20,
323                                 /*maxsegsz*/0x8000,
324                                 /*flags*/BUS_DMA_ALLOCNOW,
325 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
326                                 /*lockfunc*/busdma_lock_mutex,
327                                 /*lockarg*/&Giant,
328 #endif
329                                 &sc->fc.dmat);
330         if (err != 0) {
331                 printf("fwohci_pci_attach: Could not allocate DMA tag "
332                         "- error %d\n", err);
333                         return (ENOMEM);
334         }
335
336         err = fwohci_init(sc, self);
337
338         if (err) {
339                 device_printf(self, "fwohci_init failed with err=%d\n", err);
340                 fwohci_pci_detach(self);
341                 return EIO;
342         }
343
344         /* probe and attach a child device(firewire) */
345         bus_generic_probe(self);
346         bus_generic_attach(self);
347
348         return 0;
349 }
350
351 static int
352 fwohci_pci_detach(device_t self)
353 {
354         fwohci_softc_t *sc = device_get_softc(self);
355         int s;
356
357
358         s = splfw();
359
360         if (sc->bsr)
361                 fwohci_stop(sc, self);
362
363         bus_generic_detach(self);
364         if (sc->fc.bdev) {
365                 device_delete_child(self, sc->fc.bdev);
366                 sc->fc.bdev = NULL;
367         }
368
369         /* disable interrupts that might have been switched on */
370         if (sc->bst && sc->bsh)
371                 bus_space_write_4(sc->bst, sc->bsh,
372                                   FWOHCI_INTMASKCLR, OHCI_INT_EN);
373
374         if (sc->irq_res) {
375                 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
376                 if (err)
377                         /* XXX or should we panic? */
378                         device_printf(self, "Could not tear down irq, %d\n",
379                                       err);
380 #if defined(__DragonFly__) || __FreeBSD_version < 500000
381                 bus_teardown_intr(self, sc->irq_res, sc->ih_cam);
382                 bus_teardown_intr(self, sc->irq_res, sc->ih_bio);
383 #endif
384                 sc->ih = NULL;
385         }
386
387         if (sc->irq_res) {
388                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
389                 sc->irq_res = NULL;
390         }
391
392         if (sc->bsr) {
393                 bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr);
394                 sc->bsr = NULL;
395                 sc->bst = 0;
396                 sc->bsh = 0;
397         }
398
399         fwohci_detach(sc, self);
400         splx(s);
401
402         return 0;
403 }
404
405 static int
406 fwohci_pci_suspend(device_t dev)
407 {
408         fwohci_softc_t *sc = device_get_softc(dev);
409         int err;
410
411         device_printf(dev, "fwohci_pci_suspend\n");
412         err = bus_generic_suspend(dev);
413         if (err)
414                 return err;
415         fwohci_stop(sc, dev);
416         return 0;
417 }
418
419 static int
420 fwohci_pci_resume(device_t dev)
421 {
422         fwohci_softc_t *sc = device_get_softc(dev);
423
424 #ifndef BURN_BRIDGES
425         device_printf(dev, "fwohci_pci_resume: power_state = 0x%08x\n",
426                                         pci_get_powerstate(dev));
427         pci_set_powerstate(dev, PCI_POWERSTATE_D0);
428 #endif
429         fwohci_pci_init(dev);
430         fwohci_resume(sc, dev);
431         return 0;
432 }
433
434 static int
435 fwohci_pci_shutdown(device_t dev)
436 {
437         fwohci_softc_t *sc = device_get_softc(dev);
438
439         bus_generic_shutdown(dev);
440         fwohci_stop(sc, dev);
441         return 0;
442 }
443
444 static device_t
445 fwohci_pci_add_child(device_t dev, int order, const char *name, int unit)
446 {
447         struct fwohci_softc *sc;
448         device_t child;
449         int s, err = 0;
450
451         sc = (struct fwohci_softc *)device_get_softc(dev);
452         child = device_add_child(dev, name, unit);
453         if (child == NULL)
454                 return (child);
455
456         sc->fc.bdev = child;
457         device_set_ivars(child, (void *)&sc->fc);
458
459         err = device_probe_and_attach(child);
460         if (err) {
461                 device_printf(dev, "probe_and_attach failed with err=%d\n",
462                     err);
463                 fwohci_pci_detach(dev);
464                 device_delete_child(dev, child);
465                 return NULL;
466         }
467
468         /* XXX
469          * Clear the bus reset event flag to start transactions even when
470          * interrupt is disabled during the boot process.
471          */
472         DELAY(250); /* 2 cycles */
473         s = splfw();
474         fwohci_poll((void *)sc, 0, -1);
475         splx(s);
476
477         return (child);
478 }
479
480 static device_method_t fwohci_methods[] = {
481         /* Device interface */
482         DEVMETHOD(device_probe,         fwohci_pci_probe),
483         DEVMETHOD(device_attach,        fwohci_pci_attach),
484         DEVMETHOD(device_detach,        fwohci_pci_detach),
485         DEVMETHOD(device_suspend,       fwohci_pci_suspend),
486         DEVMETHOD(device_resume,        fwohci_pci_resume),
487         DEVMETHOD(device_shutdown,      fwohci_pci_shutdown),
488
489         /* Bus interface */
490         DEVMETHOD(bus_add_child,        fwohci_pci_add_child),
491         DEVMETHOD(bus_print_child,      bus_generic_print_child),
492
493         { 0, 0 }
494 };
495
496 static driver_t fwohci_driver = {
497         "fwohci",
498         fwohci_methods,
499         sizeof(fwohci_softc_t),
500 };
501
502 static devclass_t fwohci_devclass;
503
504 #ifdef FWOHCI_MODULE
505 MODULE_DEPEND(fwohci, firewire, 1, 1, 1);
506 #endif
507 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0);
508 DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0);