xref: /dragonfly/sys/bus/firewire/fwohci_pci.c (revision f746689a)
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.38 2004/01/23 17:37:09 simokawa Exp $
35  * $DragonFly: src/sys/bus/firewire/fwohci_pci.c,v 1.23 2006/12/22 23:12:16 swildner 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/conf.h>
45 #include <sys/bus.h>
46 #include <sys/queue.h>
47 #include <sys/rman.h>
48 #include <sys/malloc.h>
49 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #endif
53 #include <sys/thread2.h>
54 
55 #if defined(__DragonFly__) || __FreeBSD_version < 500000
56 #include <machine/clock.h>		/* for DELAY() */
57 #endif
58 
59 #ifdef __DragonFly__
60 #include <bus/pci/pcivar.h>
61 #include <bus/pci/pcireg.h>
62 
63 #include "firewire.h"
64 #include "firewirereg.h"
65 
66 #include "fwdma.h"
67 #include "fwohcireg.h"
68 #include "fwohcivar.h"
69 #else
70 #if __FreeBSD_version < 500000
71 #include <pci/pcivar.h>
72 #include <pci/pcireg.h>
73 #else
74 #include <dev/pci/pcivar.h>
75 #include <dev/pci/pcireg.h>
76 #endif
77 
78 #include <dev/firewire/firewire.h>
79 #include <dev/firewire/firewirereg.h>
80 
81 #include <dev/firewire/fwdma.h>
82 #include <dev/firewire/fwohcireg.h>
83 #include <dev/firewire/fwohcivar.h>
84 #endif
85 
86 static int fwohci_pci_attach(device_t self);
87 static int fwohci_pci_detach(device_t self);
88 
89 /*
90  * The probe routine.
91  */
92 static int
93 fwohci_pci_probe( device_t dev )
94 {
95 #if 1
96 	u_int32_t id;
97 
98 	id = pci_get_devid(dev);
99 	if (id == (FW_VENDORID_NATSEMI | FW_DEVICE_CS4210)) {
100 		device_set_desc(dev, "National Semiconductor CS4210");
101 		return 0;
102 	}
103 	if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) {
104 		device_set_desc(dev, "NEC uPD72861");
105 		return 0;
106 	}
107 	if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) {
108 		device_set_desc(dev, "NEC uPD72871/2");
109 		return 0;
110 	}
111 	if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72870)) {
112 		device_set_desc(dev, "NEC uPD72870");
113 		return 0;
114 	}
115 	if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72874)) {
116 		device_set_desc(dev, "NEC uPD72874");
117 		return 0;
118 	}
119 	if (id == (FW_VENDORID_SIS | FW_DEVICE_7007)) {
120 		/* It has no real identifier, using device id. */
121 		device_set_desc(dev, "SiS 7007");
122 		return 0;
123 	}
124 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) {
125 		device_set_desc(dev, "Texas Instruments TSB12LV22");
126 		return 0;
127 	}
128 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
129 		device_set_desc(dev, "Texas Instruments TSB12LV23");
130 		return 0;
131 	}
132 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
133 		device_set_desc(dev, "Texas Instruments TSB12LV26");
134 		return 0;
135 	}
136 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
137 		device_set_desc(dev, "Texas Instruments TSB43AA22");
138 		return 0;
139 	}
140 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
141 		device_set_desc(dev, "Texas Instruments TSB43AB22/A");
142 		return 0;
143 	}
144 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB21)) {
145 		device_set_desc(dev, "Texas Instruments TSB43AB21/A/AI/A-EP");
146 		return 0;
147 	}
148 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) {
149 		device_set_desc(dev, "Texas Instruments TSB43AB23");
150 		return 0;
151 	}
152 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) {
153 		device_set_desc(dev, "Texas Instruments TSB82AA2");
154 		return 0;
155 	}
156 	if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
157 		device_set_desc(dev, "Texas Instruments PCI4450");
158 		return 0;
159 	}
160 	if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
161 		device_set_desc(dev, "Texas Instruments PCI4410A");
162 		return 0;
163 	}
164 	if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) {
165 		device_set_desc(dev, "Texas Instruments PCI4451");
166 		return 0;
167 	}
168 	if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD1947)) {
169 		device_set_desc(dev, "Sony i.LINK (CXD1947)");
170 		return 0;
171 	}
172 	if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD3222)) {
173 		device_set_desc(dev, "Sony i.LINK (CXD3222)");
174 		return 0;
175 	}
176 	if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
177 		device_set_desc(dev, "VIA Fire II (VT6306)");
178 		return 0;
179 	}
180 	if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) {
181 		device_set_desc(dev, "Ricoh R5C551");
182 		return 0;
183 	}
184 	if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
185 		device_set_desc(dev, "Ricoh R5C552");
186 		return 0;
187 	}
188 	if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
189 		device_set_desc(dev, "Apple Pangea");
190 		return 0;
191 	}
192 	if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) {
193 		device_set_desc(dev, "Apple UniNorth");
194 		return 0;
195 	}
196 	if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
197 		device_set_desc(dev, "Lucent FW322/323");
198 		return 0;
199 	}
200 	if (id == (FW_VENDORID_INTEL | FW_DEVICE_82372FB)) {
201 		device_set_desc(dev, "Intel 82372FB");
202 		return 0;
203 	}
204 	if (id == (FW_VENDORID_ADAPTEC | FW_DEVICE_AIC5800)) {
205 		device_set_desc(dev, "Adaptec AHA-894x/AIC-5800");
206 		return 0;
207 	}
208 #endif
209 	if (pci_get_class(dev) == PCIC_SERIALBUS
210 			&& pci_get_subclass(dev) == PCIS_SERIALBUS_FW
211 			&& pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
212 		device_printf(dev, "vendor=%x, dev=%x\n", pci_get_vendor(dev),
213 			pci_get_device(dev));
214 		device_set_desc(dev, "1394 Open Host Controller Interface");
215 		return 0;
216 	}
217 
218 	return ENXIO;
219 }
220 
221 static int
222 fwohci_pci_init(device_t self)
223 {
224 	int olatency, latency, ocache_line, cache_line;
225 	u_int16_t cmd;
226 
227 	/*
228 	 * Clear PCIM_CMD_MWRICEN as per FreeBSD/1.20, but note that the
229 	 * problem may have been related to SERR and PERR being
230 	 * unconditionally enabled in that rev.
231 	 *
232 	 * Do not change the SERRESPEN or PERRESPEN bits.  Use the BIOS
233 	 * values (probably off).  This crashes some machines in fwohci_init().
234 	 *
235 	 * The theory here is that the device may not be properly initializing
236 	 * its on-chip memory, leaving some of it in a parity errored state,
237 	 * and enabling parity check may result in the device blowing up.
238 	 * It's also possible that some hardware is just plain broken.  OpenBSD
239 	 * does not turn on SERRESPEN or PERRESPEN so we won't either.
240 	 */
241 	cmd = pci_read_config(self, PCIR_COMMAND, 2);
242 	cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN;
243 	cmd &= ~PCIM_CMD_MWRICEN;
244 	pci_write_config(self, PCIR_COMMAND, cmd, 2);
245 
246 	latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1);
247 #define DEF_LATENCY 0x20
248 	if (olatency < DEF_LATENCY) {
249 		latency = DEF_LATENCY;
250 		pci_write_config(self, PCIR_LATTIMER, latency, 1);
251 	}
252 
253 	cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
254 #define DEF_CACHE_LINE 8
255 	if (ocache_line < DEF_CACHE_LINE) {
256 		cache_line = DEF_CACHE_LINE;
257 		pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
258 	}
259 
260 	if (firewire_debug) {
261 		device_printf(self, "latency timer %d -> %d.\n",
262 			olatency, latency);
263 		device_printf(self, "cache size %d -> %d.\n",
264 			ocache_line, cache_line);
265 	}
266 
267 	return 0;
268 }
269 
270 static int
271 fwohci_pci_attach(device_t self)
272 {
273 	fwohci_softc_t *sc = device_get_softc(self);
274 	int err;
275 	int rid;
276 #if defined(__DragonFly__) || __FreeBSD_version < 500000
277 	int intr;
278 	/* For the moment, put in a message stating what is wrong */
279 	intr = pci_read_config(self, PCIR_INTLINE, 1);
280 	if (intr == 0 || intr == 255) {
281 		device_printf(self, "Invalid irq %d\n", intr);
282 #ifdef __i386__
283 		device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n");
284 #endif
285 	}
286 #endif
287 
288 	if (bootverbose)
289 		firewire_debug = bootverbose;
290 
291 	fwohci_pci_init(self);
292 
293 	rid = PCI_CBMEM;
294 	sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
295 					0, ~0, 1, RF_ACTIVE);
296 	if (!sc->bsr) {
297 		device_printf(self, "Could not map memory\n");
298 		return ENXIO;
299         }
300 
301 	sc->bst = rman_get_bustag(sc->bsr);
302 	sc->bsh = rman_get_bushandle(sc->bsr);
303 
304 	rid = 0;
305 	sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
306 				     RF_SHAREABLE | RF_ACTIVE);
307 	if (sc->irq_res == NULL) {
308 		device_printf(self, "Could not allocate irq\n");
309 		fwohci_pci_detach(self);
310 		return ENXIO;
311 	}
312 
313 
314 	err = bus_setup_intr(self, sc->irq_res,
315 #if FWOHCI_TASKQUEUE
316 			INTR_MPSAFE,
317 #else
318 			0,
319 #endif
320 		     (driver_intr_t *) fwohci_intr, sc, &sc->ih, NULL);
321 	if (err) {
322 		device_printf(self, "Could not setup irq, %d\n", err);
323 		fwohci_pci_detach(self);
324 		return ENXIO;
325 	}
326 
327 	err = bus_dma_tag_create(/*parent*/NULL, /*alignment*/1,
328 				/*boundary*/0,
329 #if BOUNCE_BUFFER_TEST
330 				/*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
331 #else
332 				/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
333 #endif
334 				/*highaddr*/BUS_SPACE_MAXADDR,
335 				/*filter*/NULL, /*filterarg*/NULL,
336 				/*maxsize*/0x100000,
337 				/*nsegments*/0x20,
338 				/*maxsegsz*/0x8000,
339 				/*flags*/BUS_DMA_ALLOCNOW,
340 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
341 				/*lockfunc*/busdma_lock_mutex,
342 				/*lockarg*/&Giant,
343 #endif
344 				&sc->fc.dmat);
345 	if (err != 0) {
346 		kprintf("fwohci_pci_attach: Could not allocate DMA tag "
347 			"- error %d\n", err);
348 			return (ENOMEM);
349 	}
350 
351 	err = fwohci_init(sc, self);
352 
353 	if (err) {
354 		device_printf(self, "fwohci_init failed with err=%d\n", err);
355 		fwohci_pci_detach(self);
356 		return EIO;
357 	}
358 
359 	/* probe and attach a child device(firewire) */
360 	bus_generic_probe(self);
361 	bus_generic_attach(self);
362 
363 	return 0;
364 }
365 
366 static int
367 fwohci_pci_detach(device_t self)
368 {
369 	fwohci_softc_t *sc = device_get_softc(self);
370 
371 	crit_enter();
372 	if (sc->bsr)
373 		fwohci_stop(sc, self);
374 
375 	bus_generic_detach(self);
376 	if (sc->fc.bdev) {
377 		device_delete_child(self, sc->fc.bdev);
378 		sc->fc.bdev = NULL;
379 	}
380 
381 	/* disable interrupts that might have been switched on */
382 	if (sc->bst && sc->bsh)
383 		bus_space_write_4(sc->bst, sc->bsh,
384 				  FWOHCI_INTMASKCLR, OHCI_INT_EN);
385 
386 	if (sc->irq_res) {
387 		int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
388 		if (err)
389 			/* XXX or should we panic? */
390 			device_printf(self, "Could not tear down irq, %d\n",
391 				      err);
392 		sc->ih = NULL;
393 	}
394 
395 	if (sc->irq_res) {
396 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
397 		sc->irq_res = NULL;
398 	}
399 
400 	if (sc->bsr) {
401 		bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr);
402 		sc->bsr = NULL;
403 		sc->bst = 0;
404 		sc->bsh = 0;
405 	}
406 
407 	fwohci_detach(sc, self);
408 	crit_exit();
409 
410 	return 0;
411 }
412 
413 static int
414 fwohci_pci_suspend(device_t dev)
415 {
416 	fwohci_softc_t *sc = device_get_softc(dev);
417 	int err;
418 
419 	device_printf(dev, "fwohci_pci_suspend\n");
420 	err = bus_generic_suspend(dev);
421 	if (err)
422 		return err;
423 	fwohci_stop(sc, dev);
424 	return 0;
425 }
426 
427 static int
428 fwohci_pci_resume(device_t dev)
429 {
430 	fwohci_softc_t *sc = device_get_softc(dev);
431 
432 #ifndef BURN_BRIDGES
433 	device_printf(dev, "fwohci_pci_resume: power_state = 0x%08x\n",
434 					pci_get_powerstate(dev));
435 	pci_set_powerstate(dev, PCI_POWERSTATE_D0);
436 #endif
437 	fwohci_pci_init(dev);
438 	fwohci_resume(sc, dev);
439 	return 0;
440 }
441 
442 static int
443 fwohci_pci_shutdown(device_t dev)
444 {
445 	fwohci_softc_t *sc = device_get_softc(dev);
446 
447 	bus_generic_shutdown(dev);
448 	fwohci_stop(sc, dev);
449 	return 0;
450 }
451 
452 static device_t
453 fwohci_pci_add_child(device_t bus, device_t parent, int order, const char *name, int unit)
454 {
455 	struct fwohci_softc *sc;
456 	device_t child;
457 	int err = 0;
458 
459 	sc = (struct fwohci_softc *)device_get_softc(bus);
460 	child = device_add_child(parent, name, unit);
461 	if (child == NULL)
462 		return (child);
463 
464 	sc->fc.bdev = child;
465 	device_set_ivars(child, (void *)&sc->fc);
466 
467 	err = device_probe_and_attach(child);
468 	if (err) {
469 		device_printf(parent, "probe_and_attach failed with err=%d\n",
470 		    err);
471 		fwohci_pci_detach(parent);
472 		device_delete_child(parent, child);
473 		return NULL;
474 	}
475 
476 	/* XXX
477 	 * Clear the bus reset event flag to start transactions even when
478 	 * interrupt is disabled during the boot process.
479 	 */
480 	DELAY(250); /* 2 cycles */
481 	crit_enter();
482 	fwohci_poll((void *)sc, 0, -1);
483 	crit_exit();
484 
485 	return (child);
486 }
487 
488 static device_method_t fwohci_methods[] = {
489 	/* Device interface */
490 	DEVMETHOD(device_probe,		fwohci_pci_probe),
491 	DEVMETHOD(device_attach,	fwohci_pci_attach),
492 	DEVMETHOD(device_detach,	fwohci_pci_detach),
493 	DEVMETHOD(device_suspend,	fwohci_pci_suspend),
494 	DEVMETHOD(device_resume,	fwohci_pci_resume),
495 	DEVMETHOD(device_shutdown,	fwohci_pci_shutdown),
496 
497 	/* Bus interface */
498 	DEVMETHOD(bus_add_child,	fwohci_pci_add_child),
499 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
500 
501 	{ 0, 0 }
502 };
503 
504 static driver_t fwohci_driver = {
505 	"fwohci",
506 	fwohci_methods,
507 	sizeof(fwohci_softc_t),
508 };
509 
510 static devclass_t fwohci_devclass;
511 
512 #ifdef FWOHCI_MODULE
513 MODULE_DEPEND(fwohci, firewire, 1, 1, 1);
514 #endif
515 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0);
516 DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0);
517