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