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