kernel tree reorganization stage 1: Major cvs repository work (not logged as
[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.3.2.11 2003/04/28 03:29:18 simokawa Exp $
35  * $DragonFly: src/sys/bus/firewire/fwohci_pci.c,v 1.3 2003/08/07 21:16:45 dillon 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/bus.h>
45 #include <sys/queue.h>
46 #include <machine/bus.h>
47 #include <sys/rman.h>
48 #include <sys/malloc.h>
49 #include <machine/resource.h>
50
51 #include <bus/pci/pcivar.h>
52 #include <bus/pci/pcireg.h>
53
54 #include "firewire.h"
55 #include "firewirereg.h"
56
57 #include "fwdma.h"
58 #include "fwohcireg.h"
59 #include "fwohcivar.h"
60
61 static int fwohci_pci_attach(device_t self);
62 static int fwohci_pci_detach(device_t self);
63
64 /*
65  * The probe routine.
66  */
67 static int
68 fwohci_pci_probe( device_t dev )
69 {
70 #if 1
71         u_int32_t id;
72
73         id = pci_get_devid(dev);
74         if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) {
75                 device_set_desc(dev, "NEC uPD72861");
76                 return 0;
77         }
78         if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) {
79                 device_set_desc(dev, "NEC uPD72871/2");
80                 return 0;
81         }
82         if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72870)) {
83                 device_set_desc(dev, "NEC uPD72870");
84                 return 0;
85         }
86         if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72874)) {
87                 device_set_desc(dev, "NEC uPD72874");
88                 return 0;
89         }
90         if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) {
91                 device_set_desc(dev, "Texas Instruments TSB12LV22");
92                 return 0;
93         }
94         if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
95                 device_set_desc(dev, "Texas Instruments TSB12LV23");
96                 return 0;
97         }
98         if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
99                 device_set_desc(dev, "Texas Instruments TSB12LV26");
100                 return 0;
101         }
102         if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
103                 device_set_desc(dev, "Texas Instruments TSB43AA22");
104                 return 0;
105         }
106         if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
107                 device_set_desc(dev, "Texas Instruments TSB43AB22/A");
108                 return 0;
109         }
110         if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) {
111                 device_set_desc(dev, "Texas Instruments TSB43AB23");
112                 return 0;
113         }
114         if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
115                 device_set_desc(dev, "Texas Instruments PCI4450");
116                 return 0;
117         }
118         if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
119                 device_set_desc(dev, "Texas Instruments PCI4410A");
120                 return 0;
121         }
122         if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) {
123                 device_set_desc(dev, "Texas Instruments PCI4451");
124                 return 0;
125         }
126         if (id == (FW_VENDORID_SONY | FW_DEVICE_CX3022)) {
127                 device_set_desc(dev, "Sony CX3022");
128                 return 0;
129         }
130         if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
131                 device_set_desc(dev, "VIA VT6306");
132                 return 0;
133         }
134         if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) {
135                 device_set_desc(dev, "Ricoh R5C551");
136                 return 0;
137         }
138         if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
139                 device_set_desc(dev, "Ricoh R5C552");
140                 return 0;
141         }
142         if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
143                 device_set_desc(dev, "Apple Pangea");
144                 return 0;
145         }
146         if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) {
147                 device_set_desc(dev, "Apple UniNorth");
148                 return 0;
149         }
150         if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
151                 device_set_desc(dev, "Lucent FW322/323");
152                 return 0;
153         }
154 #endif
155         if (pci_get_class(dev) == PCIC_SERIALBUS
156                         && pci_get_subclass(dev) == PCIS_SERIALBUS_FW
157                         && pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
158                 device_printf(dev, "vendor=%x, dev=%x\n", pci_get_vendor(dev),
159                         pci_get_device(dev));
160                 device_set_desc(dev, "1394 Open Host Controller Interface");
161                 return 0;
162         }
163
164         return ENXIO;
165 }
166
167 #if __FreeBSD_version < 500000
168 static void
169 fwohci_dummy_intr(void *arg)
170 {
171         /* XXX do nothing */
172 }
173 #endif
174
175 static int
176 fwohci_pci_init(device_t self)
177 {
178         int olatency, latency, ocache_line, cache_line;
179         u_int16_t cmd;
180
181         cmd = pci_read_config(self, PCIR_COMMAND, 2);
182         cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN |
183                 PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN;
184 #if 1
185         cmd &= ~PCIM_CMD_MWRICEN; 
186 #endif
187         pci_write_config(self, PCIR_COMMAND, cmd, 2);
188
189         latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1);
190 #define DEF_LATENCY 0x20
191         if (olatency < DEF_LATENCY) {
192                 latency = DEF_LATENCY;
193                 pci_write_config(self, PCIR_LATTIMER, latency, 1);
194         }
195
196         cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
197 #define DEF_CACHE_LINE 8
198         if (ocache_line < DEF_CACHE_LINE) {
199                 cache_line = DEF_CACHE_LINE;
200                 pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
201         }
202
203         if (firewire_debug) {
204                 device_printf(self, "latency timer %d -> %d.\n",
205                         olatency, latency);
206                 device_printf(self, "cache size %d -> %d.\n",
207                         ocache_line, cache_line);
208         }
209
210         return 0;
211 }
212
213 static int
214 fwohci_pci_attach(device_t self)
215 {
216         fwohci_softc_t *sc = device_get_softc(self);
217         int err;
218         int rid, s;
219 #if __FreeBSD_version < 500000
220         int intr;
221         /* For the moment, put in a message stating what is wrong */
222         intr = pci_read_config(self, PCIR_INTLINE, 1);
223         if (intr == 0 || intr == 255) {
224                 device_printf(self, "Invalid irq %d\n", intr);
225 #ifdef __i386__
226                 device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n");
227 #endif
228         }
229 #endif
230
231         if (bootverbose)
232                 firewire_debug = bootverbose;
233
234         fwohci_pci_init(self);
235
236         rid = PCI_CBMEM;
237         sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
238                                         0, ~0, 1, RF_ACTIVE);
239         if (!sc->bsr) {
240                 device_printf(self, "Could not map memory\n");
241                 return ENXIO;
242         }
243
244         sc->bst = rman_get_bustag(sc->bsr);
245         sc->bsh = rman_get_bushandle(sc->bsr);
246
247         rid = 0;
248         sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
249                                      RF_SHAREABLE | RF_ACTIVE);
250         if (sc->irq_res == NULL) {
251                 device_printf(self, "Could not allocate irq\n");
252                 fwohci_pci_detach(self);
253                 return ENXIO;
254         }
255
256         sc->fc.bdev = device_add_child(self, "firewire", -1);
257         if (!sc->fc.bdev) {
258                 device_printf(self, "Could not add firewire device\n");
259                 fwohci_pci_detach(self);
260                 return ENOMEM;
261         }
262         device_set_ivars(sc->fc.bdev, sc);
263
264         err = bus_setup_intr(self, sc->irq_res,
265 #if FWOHCI_TASKQUEUE
266                         INTR_TYPE_NET | INTR_MPSAFE,
267 #else
268                         INTR_TYPE_NET,
269 #endif
270                      (driver_intr_t *) fwohci_intr, sc, &sc->ih);
271 #if __FreeBSD_version < 500000
272         /* XXX splcam() should mask this irq for sbp.c*/
273         err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM,
274                      (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_cam);
275 #endif
276         if (err) {
277                 device_printf(self, "Could not setup irq, %d\n", err);
278                 fwohci_pci_detach(self);
279                 return ENXIO;
280         }
281
282         err = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
283                                 /*boundary*/0,
284 #if BOUNCE_BUFFER_TEST
285                                 /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
286 #else
287                                 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
288 #endif
289                                 /*highaddr*/BUS_SPACE_MAXADDR,
290                                 /*filter*/NULL, /*filterarg*/NULL,
291                                 /*maxsize*/0x100000,
292                                 /*nsegments*/0x20,
293                                 /*maxsegsz*/0x8000,
294                                 /*flags*/BUS_DMA_ALLOCNOW,
295                                 &sc->fc.dmat);
296         if (err != 0) {
297                 printf("fwohci_pci_attach: Could not allocate DMA tag "
298                         "- error %d\n", err);
299                         return (ENOMEM);
300         }
301
302         err = fwohci_init(sc, self);
303
304         if (!err)
305                 err = device_probe_and_attach(sc->fc.bdev);
306
307         if (err) {
308                 device_printf(self, "FireWire init failed\n");
309                 fwohci_pci_detach(self);
310                 return EIO;
311         }
312
313         /* XXX
314          * Clear the bus reset event flag to start transactions even when
315          * interrupt is disabled during the boot process.
316          */
317         s = splfw();
318         fwohci_intr((void *)sc);
319         splx(s);
320
321         return 0;
322 }
323
324 static int
325 fwohci_pci_detach(device_t self)
326 {
327         fwohci_softc_t *sc = device_get_softc(self);
328         int s;
329
330
331         s = splfw();
332
333         fwohci_stop(sc, self);
334         bus_generic_detach(self);
335
336         /* disable interrupts that might have been switched on */
337         if (sc->bst && sc->bsh)
338                 bus_space_write_4(sc->bst, sc->bsh,
339                                   FWOHCI_INTMASKCLR, OHCI_INT_EN);
340
341         if (sc->irq_res) {
342                 int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
343                 if (err)
344                         /* XXX or should we panic? */
345                         device_printf(self, "Could not tear down irq, %d\n",
346                                       err);
347 #if __FreeBSD_version < 500000
348                 err = bus_teardown_intr(self, sc->irq_res, sc->ih_cam);
349 #endif
350                 sc->ih = NULL;
351         }
352
353         if (sc->fc.bdev) {
354                 device_delete_child(self, sc->fc.bdev);
355                 sc->fc.bdev = NULL;
356         }
357
358         if (sc->irq_res) {
359                 bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
360                 sc->irq_res = NULL;
361         }
362
363         if (sc->bsr) {
364                 bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr);
365                 sc->bsr = NULL;
366                 sc->bst = 0;
367                 sc->bsh = 0;
368         }
369
370         fwohci_detach(sc, self);
371         splx(s);
372
373         return 0;
374 }
375
376 static int
377 fwohci_pci_suspend(device_t dev)
378 {
379         int err;
380
381         device_printf(dev, "fwohci_pci_suspend\n");
382         err = bus_generic_suspend(dev);
383         if (err)
384                 return err;
385         /* fwohci_stop(dev); */
386         return 0;
387 }
388
389 static int
390 fwohci_pci_resume(device_t dev)
391 {
392         fwohci_softc_t *sc = device_get_softc(dev);
393
394         device_printf(dev, "fwohci_pci_resume: power_state = 0x%08x\n",
395                                         pci_get_powerstate(dev));
396         fwohci_pci_init(dev);
397         fwohci_resume(sc, dev);
398         return 0;
399 }
400
401 static int
402 fwohci_pci_shutdown(device_t dev)
403 {
404         fwohci_softc_t *sc = device_get_softc(dev);
405
406         bus_generic_shutdown(dev);
407         fwohci_stop(sc, dev);
408         return 0;
409 }
410
411 static device_method_t fwohci_methods[] = {
412         /* Device interface */
413         DEVMETHOD(device_probe,         fwohci_pci_probe),
414         DEVMETHOD(device_attach,        fwohci_pci_attach),
415         DEVMETHOD(device_detach,        fwohci_pci_detach),
416         DEVMETHOD(device_suspend,       fwohci_pci_suspend),
417         DEVMETHOD(device_resume,        fwohci_pci_resume),
418         DEVMETHOD(device_shutdown,      fwohci_pci_shutdown),
419
420         /* Bus interface */
421         DEVMETHOD(bus_print_child,      bus_generic_print_child),
422
423         { 0, 0 }
424 };
425
426 static driver_t fwohci_driver = {
427         "fwohci",
428         fwohci_methods,
429         sizeof(fwohci_softc_t),
430 };
431
432 static devclass_t fwohci_devclass;
433
434 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0);
435 DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0);