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