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