xref: /freebsd/sys/dev/firewire/fwohci_pci.c (revision 4b2eaea43fec8e8792be611dea204071a10b655a)
1 /*
2  * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the acknowledgement as bellow:
15  *
16  *    This product includes software developed by K. Kobayashi and H. SHimokawa
17  *
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/queue.h>
42 #include <machine/bus.h>
43 #include <sys/rman.h>
44 #include <machine/resource.h>
45 
46 #include <pci/pcivar.h>
47 #include <pci/pcireg.h>
48 
49 #include <dev/firewire/firewire.h>
50 #include <dev/firewire/firewirereg.h>
51 
52 #include <dev/firewire/fwohcireg.h>
53 #include <dev/firewire/fwohcivar.h>
54 
55 static int fwohci_pci_attach(device_t self);
56 static int fwohci_pci_detach(device_t self);
57 
58 /*
59  * The probe routine.
60  */
61 static int
62 fwohci_pci_probe( device_t dev )
63 {
64 #if 1
65 	u_int32_t id;
66 
67 	id = (pci_get_vendor(dev) << 16) | pci_get_device(dev);
68 	if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) {
69 		device_set_desc(dev, "NEC uPD72861");
70 		return 0;
71 	}
72 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) {
73 		device_set_desc(dev, "Texas Instruments TSB12LV22");
74 		return 0;
75 	}
76 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
77 		device_set_desc(dev, "Texas Instruments TSB12LV23");
78 		return 0;
79 	}
80 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
81 		device_set_desc(dev, "Texas Instruments TSB12LV26");
82 		return 0;
83 	}
84 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
85 		device_set_desc(dev, "Texas Instruments TSB43AA22");
86 		return 0;
87 	}
88 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
89 		device_set_desc(dev, "Texas Instruments TSB43AB22/A");
90 		return 0;
91 	}
92 	if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
93 		device_set_desc(dev, "Texas Instruments PCI4450");
94 		return 0;
95 	}
96 	if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
97 		device_set_desc(dev, "Texas Instruments PCI4410A");
98 		return 0;
99 	}
100 	if (id == (FW_VENDORID_SONY | FW_DEVICE_CX3022)) {
101 		device_set_desc(dev, "SONY CX3022");
102 		return 0;
103 	}
104 	if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
105 		device_set_desc(dev, "VIA VT6306");
106 		return 0;
107 	}
108 	if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
109 		device_set_desc(dev, "Ricoh R5C552");
110 		return 0;
111 	}
112 	if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
113 		device_set_desc(dev, "Apple Pangea");
114 		return 0;
115 	}
116 	if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) {
117 		device_set_desc(dev, "Apple UniNorth");
118 		return 0;
119 	}
120 	if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
121 		device_set_desc(dev, "Lucent FW322/323");
122 		return 0;
123 	}
124 #endif
125 	if (pci_get_class(dev) == PCIC_SERIALBUS
126 			&& pci_get_subclass(dev) == PCIS_SERIALBUS_FW
127 			&& pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
128 		device_printf(dev, "vendor=%x, dev=%x\n", pci_get_vendor(dev),
129 			pci_get_device(dev));
130 		device_set_desc(dev, "1394 Open Host Controller Interface");
131 		return 0;
132 	}
133 
134 	return ENXIO;
135 }
136 
137 #if __FreeBSD_version < 500000
138 static void
139 fwohci_dummy_intr(void *arg)
140 {
141 	/* XXX do nothing */
142 }
143 #endif
144 
145 static int
146 fwohci_pci_init(device_t self)
147 {
148 	int latency, cache_line;
149 	u_int16_t cmd;
150 
151 	cmd = pci_read_config(self, PCIR_COMMAND, 2);
152 	cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN;
153 	pci_write_config(self, PCIR_COMMAND, cmd, 2);
154 
155 	latency = pci_read_config(self, PCIR_LATTIMER, 1);
156 #define DEF_LATENCY 250 /* Derived from Max Bulk Transfer size 512 Bytes*/
157 	if( latency < DEF_LATENCY ) {
158 		latency = DEF_LATENCY;
159 		device_printf(self, "PCI bus latency was changing to");
160 		pci_write_config(self, PCIR_LATTIMER,latency, 1);
161 	} else
162 	 {
163 		device_printf(self, "PCI bus latency is");
164 	}
165 	printf(" %d.\n", (int) latency);
166 	cache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
167 #if 0
168 #define DEF_CACHE_LINE 0xc
169 	cache_line = DEF_CACHE_LINE;
170 	pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
171 #endif
172 	if (bootverbose)
173 		device_printf(self, "cache size %d.\n", (int) cache_line);
174 
175 	return 0;
176 }
177 
178 static int
179 fwohci_pci_attach(device_t self)
180 {
181 	fwohci_softc_t *sc = device_get_softc(self);
182 	int err;
183 	int rid;
184 #if __FreeBSD_version < 500000
185 	int intr;
186 	/* For the moment, put in a message stating what is wrong */
187 	intr = pci_read_config(self, PCIR_INTLINE, 1);
188 	if (intr == 0 || intr == 255) {
189 		device_printf(self, "Invalid irq %d\n", intr);
190 #ifdef __i386__
191 		device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n");
192 #endif
193 #if 0
194 		return ENXIO;
195 #endif
196 	}
197 #endif
198 
199 	fwohci_pci_init(self);
200 
201 	rid = PCI_CBMEM;
202 	sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
203 					0, ~0, 1, RF_ACTIVE);
204 	if (!sc->bsr) {
205 		device_printf(self, "Could not map memory\n");
206 		return ENXIO;
207         }
208 
209 	sc->bst = rman_get_bustag(sc->bsr);
210 	sc->bsh = rman_get_bushandle(sc->bsr);
211 
212 	rid = 0;
213 	sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
214 				     RF_SHAREABLE | RF_ACTIVE);
215 	if (sc->irq_res == NULL) {
216 		device_printf(self, "Could not allocate irq\n");
217 		fwohci_pci_detach(self);
218 		return ENXIO;
219 	}
220 
221 	sc->fc.bdev = device_add_child(self, "firewire", -1);
222 	if (!sc->fc.bdev) {
223 		device_printf(self, "Could not add firewire device\n");
224 		fwohci_pci_detach(self);
225 		return ENOMEM;
226 	}
227 	device_set_ivars(sc->fc.bdev, sc);
228 
229 	err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_NET,
230 		     (driver_intr_t *) fwohci_intr, sc, &sc->ih);
231 #if __FreeBSD_version < 500000
232 	/* XXX splcam() should mask this irq for sbp.c*/
233 	err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM,
234 		     (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_cam);
235 #endif
236 	if (err) {
237 		device_printf(self, "Could not setup irq, %d\n", err);
238 		fwohci_pci_detach(self);
239 		return ENXIO;
240 	}
241 
242 	err = fwohci_init(sc, self);
243 
244 	if (!err)
245 		err = device_probe_and_attach(sc->fc.bdev);
246 
247 	if (err) {
248 		device_printf(self, "FireWire init failed\n");
249 		fwohci_pci_detach(self);
250 		return EIO;
251 	}
252 
253 	return 0;
254 }
255 
256 static int
257 fwohci_pci_detach(device_t self)
258 {
259 	fwohci_softc_t *sc = device_get_softc(self);
260 	int s;
261 
262 
263 	s = splfw();
264 
265 	fwohci_shutdown(sc, self);
266 	bus_generic_detach(self);
267 
268 	/* disable interrupts that might have been switched on */
269 	if (sc->bst && sc->bsh)
270 		bus_space_write_4(sc->bst, sc->bsh,
271 				  FWOHCI_INTMASKCLR, OHCI_INT_EN);
272 
273 	if (sc->irq_res) {
274 		int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
275 		if (err)
276 			/* XXX or should we panic? */
277 			device_printf(self, "Could not tear down irq, %d\n",
278 				      err);
279 #if __FreeBSD_version < 500000
280 		err = bus_teardown_intr(self, sc->irq_res, sc->ih_cam);
281 #endif
282 		sc->ih = NULL;
283 	}
284 
285 	if (sc->fc.bdev) {
286 		device_delete_child(self, sc->fc.bdev);
287 		sc->fc.bdev = NULL;
288 	}
289 
290 	if (sc->irq_res) {
291 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
292 		sc->irq_res = NULL;
293 	}
294 
295 	if (sc->bsr) {
296 		bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr);
297 		sc->bsr = NULL;
298 		sc->bst = 0;
299 		sc->bsh = 0;
300 	}
301 
302 	fwohci_detach(sc, self);
303 	splx(s);
304 
305 	return 0;
306 }
307 
308 static int
309 fwohci_pci_suspend(device_t dev)
310 {
311 	int err;
312 
313 	device_printf(dev, "fwohci_pci_suspend\n");
314 	err = bus_generic_suspend(dev);
315 	if (err)
316 		return err;
317 	/* fwohci_shutdown(dev); */
318 	return 0;
319 }
320 
321 static int
322 fwohci_pci_resume(device_t dev)
323 {
324 	fwohci_softc_t *sc = device_get_softc(dev);
325 
326 	device_printf(dev, "fwohci_pci_resume: power_state = 0x%08x\n",
327 					pci_get_powerstate(dev));
328 	fwohci_pci_init(dev);
329 	fwohci_resume(sc, dev);
330 	return 0;
331 }
332 
333 static int
334 fwohci_pci_shutdown(device_t dev)
335 {
336 	fwohci_softc_t *sc = device_get_softc(dev);
337 
338 	fwohci_shutdown(sc, dev);
339 	return 0;
340 }
341 
342 static device_method_t fwohci_methods[] = {
343 	/* Device interface */
344 	DEVMETHOD(device_probe,		fwohci_pci_probe),
345 	DEVMETHOD(device_attach,	fwohci_pci_attach),
346 	DEVMETHOD(device_detach,	fwohci_pci_detach),
347 	DEVMETHOD(device_suspend,	fwohci_pci_suspend),
348 	DEVMETHOD(device_resume,	fwohci_pci_resume),
349 	DEVMETHOD(device_shutdown,	fwohci_pci_shutdown),
350 
351 	/* Bus interface */
352 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
353 
354 	{ 0, 0 }
355 };
356 
357 static driver_t fwohci_driver = {
358 	"fwohci",
359 	fwohci_methods,
360 	sizeof(fwohci_softc_t),
361 };
362 
363 static devclass_t fwohci_devclass;
364 
365 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0);
366 DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0);
367