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