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