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