xref: /freebsd/sys/dev/firewire/fwohci_pci.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
1 /*
2  * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the acknowledgement as bellow:
15  *
16  *    This product includes software developed by K. Kobayashi and H. SHimokawa
17  *
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/queue.h>
42 #include <machine/bus.h>
43 #include <sys/rman.h>
44 #include <sys/malloc.h>
45 #include <machine/resource.h>
46 
47 #include <pci/pcivar.h>
48 #include <pci/pcireg.h>
49 
50 #include <dev/firewire/firewire.h>
51 #include <dev/firewire/firewirereg.h>
52 
53 #include <dev/firewire/fwohcireg.h>
54 #include <dev/firewire/fwohcivar.h>
55 
56 static int fwohci_pci_attach(device_t self);
57 static int fwohci_pci_detach(device_t self);
58 
59 /*
60  * The probe routine.
61  */
62 static int
63 fwohci_pci_probe( device_t dev )
64 {
65 #if 1
66 	u_int32_t id;
67 
68 	id = pci_get_devid(dev);
69 	if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) {
70 		device_set_desc(dev, "NEC uPD72861");
71 		return 0;
72 	}
73 	if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) {
74 		device_set_desc(dev, "NEC uPD72871/2");
75 		return 0;
76 	}
77 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) {
78 		device_set_desc(dev, "Texas Instruments TSB12LV22");
79 		return 0;
80 	}
81 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
82 		device_set_desc(dev, "Texas Instruments TSB12LV23");
83 		return 0;
84 	}
85 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
86 		device_set_desc(dev, "Texas Instruments TSB12LV26");
87 		return 0;
88 	}
89 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
90 		device_set_desc(dev, "Texas Instruments TSB43AA22");
91 		return 0;
92 	}
93 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
94 		device_set_desc(dev, "Texas Instruments TSB43AB22/A");
95 		return 0;
96 	}
97 	if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
98 		device_set_desc(dev, "Texas Instruments PCI4450");
99 		return 0;
100 	}
101 	if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
102 		device_set_desc(dev, "Texas Instruments PCI4410A");
103 		return 0;
104 	}
105 	if (id == (FW_VENDORID_SONY | FW_DEVICE_CX3022)) {
106 		device_set_desc(dev, "SONY CX3022");
107 		return 0;
108 	}
109 	if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
110 		device_set_desc(dev, "VIA VT6306");
111 		return 0;
112 	}
113 	if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
114 		device_set_desc(dev, "Ricoh R5C552");
115 		return 0;
116 	}
117 	if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
118 		device_set_desc(dev, "Apple Pangea");
119 		return 0;
120 	}
121 	if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) {
122 		device_set_desc(dev, "Apple UniNorth");
123 		return 0;
124 	}
125 	if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
126 		device_set_desc(dev, "Lucent FW322/323");
127 		return 0;
128 	}
129 #endif
130 	if (pci_get_class(dev) == PCIC_SERIALBUS
131 			&& pci_get_subclass(dev) == PCIS_SERIALBUS_FW
132 			&& pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
133 		device_printf(dev, "vendor=%x, dev=%x\n", pci_get_vendor(dev),
134 			pci_get_device(dev));
135 		device_set_desc(dev, "1394 Open Host Controller Interface");
136 		return 0;
137 	}
138 
139 	return ENXIO;
140 }
141 
142 #if __FreeBSD_version < 500000
143 static void
144 fwohci_dummy_intr(void *arg)
145 {
146 	/* XXX do nothing */
147 }
148 #endif
149 
150 static int
151 fwohci_pci_init(device_t self)
152 {
153 	int latency, cache_line;
154 	u_int16_t cmd;
155 
156 	cmd = pci_read_config(self, PCIR_COMMAND, 2);
157 	cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN |
158 		PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN;
159 #if 1
160 	cmd &= ~PCIM_CMD_MWRICEN;
161 #endif
162 	pci_write_config(self, PCIR_COMMAND, cmd, 2);
163 
164 	latency = pci_read_config(self, PCIR_LATTIMER, 1);
165 #define DEF_LATENCY 0x20
166 	if( latency < DEF_LATENCY ) {
167 		latency = DEF_LATENCY;
168 		device_printf(self, "PCI bus latency was changing to");
169 		pci_write_config(self, PCIR_LATTIMER,latency, 1);
170 	} else
171 	 {
172 		device_printf(self, "PCI bus latency is");
173 	}
174 	printf(" %d.\n", (int) latency);
175 	cache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
176 #if 0
177 #define DEF_CACHE_LINE 0xc
178 	cache_line = DEF_CACHE_LINE;
179 	pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
180 #endif
181 	if (bootverbose)
182 		device_printf(self, "cache size %d.\n", (int) cache_line);
183 
184 	return 0;
185 }
186 
187 static int
188 fwohci_pci_attach(device_t self)
189 {
190 	fwohci_softc_t *sc = device_get_softc(self);
191 	int err;
192 	int rid, s;
193 #if __FreeBSD_version < 500000
194 	int intr;
195 	/* For the moment, put in a message stating what is wrong */
196 	intr = pci_read_config(self, PCIR_INTLINE, 1);
197 	if (intr == 0 || intr == 255) {
198 		device_printf(self, "Invalid irq %d\n", intr);
199 #ifdef __i386__
200 		device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n");
201 #endif
202 #if 0
203 		return ENXIO;
204 #endif
205 	}
206 #endif
207 
208 	fwohci_pci_init(self);
209 
210 	rid = PCI_CBMEM;
211 	sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
212 					0, ~0, 1, RF_ACTIVE);
213 	if (!sc->bsr) {
214 		device_printf(self, "Could not map memory\n");
215 		return ENXIO;
216         }
217 
218 	sc->bst = rman_get_bustag(sc->bsr);
219 	sc->bsh = rman_get_bushandle(sc->bsr);
220 
221 	rid = 0;
222 	sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
223 				     RF_SHAREABLE | RF_ACTIVE);
224 	if (sc->irq_res == NULL) {
225 		device_printf(self, "Could not allocate irq\n");
226 		fwohci_pci_detach(self);
227 		return ENXIO;
228 	}
229 
230 	sc->fc.bdev = device_add_child(self, "firewire", -1);
231 	if (!sc->fc.bdev) {
232 		device_printf(self, "Could not add firewire device\n");
233 		fwohci_pci_detach(self);
234 		return ENOMEM;
235 	}
236 	device_set_ivars(sc->fc.bdev, sc);
237 
238 	err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_NET,
239 		     (driver_intr_t *) fwohci_intr, sc, &sc->ih);
240 #if __FreeBSD_version < 500000
241 	/* XXX splcam() should mask this irq for sbp.c*/
242 	err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM,
243 		     (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_cam);
244 #endif
245 	if (err) {
246 		device_printf(self, "Could not setup irq, %d\n", err);
247 		fwohci_pci_detach(self);
248 		return ENXIO;
249 	}
250 
251 	err = fwohci_init(sc, self);
252 
253 	if (!err)
254 		err = device_probe_and_attach(sc->fc.bdev);
255 
256 	if (err) {
257 		device_printf(self, "FireWire init failed\n");
258 		fwohci_pci_detach(self);
259 		return EIO;
260 	}
261 
262 	/* XXX
263 	 * Clear the bus reset event flag to start transactions even when
264 	 * interrupt is disabled during the boot process.
265 	 */
266 #if 0
267 	DELAY(100);
268 #endif
269 	s = splfw();
270 	fwohci_intr((void *)sc);
271 	splx(s);
272 
273 	return 0;
274 }
275 
276 static int
277 fwohci_pci_detach(device_t self)
278 {
279 	fwohci_softc_t *sc = device_get_softc(self);
280 	int s;
281 
282 
283 	s = splfw();
284 
285 	fwohci_stop(sc, self);
286 	bus_generic_detach(self);
287 
288 	/* disable interrupts that might have been switched on */
289 	if (sc->bst && sc->bsh)
290 		bus_space_write_4(sc->bst, sc->bsh,
291 				  FWOHCI_INTMASKCLR, OHCI_INT_EN);
292 
293 	if (sc->irq_res) {
294 		int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
295 		if (err)
296 			/* XXX or should we panic? */
297 			device_printf(self, "Could not tear down irq, %d\n",
298 				      err);
299 #if __FreeBSD_version < 500000
300 		err = bus_teardown_intr(self, sc->irq_res, sc->ih_cam);
301 #endif
302 		sc->ih = NULL;
303 	}
304 
305 	if (sc->fc.bdev) {
306 		device_delete_child(self, sc->fc.bdev);
307 		sc->fc.bdev = NULL;
308 	}
309 
310 	if (sc->irq_res) {
311 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
312 		sc->irq_res = NULL;
313 	}
314 
315 	if (sc->bsr) {
316 		bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr);
317 		sc->bsr = NULL;
318 		sc->bst = 0;
319 		sc->bsh = 0;
320 	}
321 
322 	fwohci_detach(sc, self);
323 	splx(s);
324 
325 	return 0;
326 }
327 
328 static int
329 fwohci_pci_suspend(device_t dev)
330 {
331 	int err;
332 
333 	device_printf(dev, "fwohci_pci_suspend\n");
334 	err = bus_generic_suspend(dev);
335 	if (err)
336 		return err;
337 	/* fwohci_stop(dev); */
338 	return 0;
339 }
340 
341 static int
342 fwohci_pci_resume(device_t dev)
343 {
344 	fwohci_softc_t *sc = device_get_softc(dev);
345 
346 	device_printf(dev, "fwohci_pci_resume: power_state = 0x%08x\n",
347 					pci_get_powerstate(dev));
348 	fwohci_pci_init(dev);
349 	fwohci_resume(sc, dev);
350 	return 0;
351 }
352 
353 static int
354 fwohci_pci_shutdown(device_t dev)
355 {
356 	fwohci_softc_t *sc = device_get_softc(dev);
357 
358 	bus_generic_shutdown(dev);
359 	fwohci_stop(sc, dev);
360 	return 0;
361 }
362 
363 static device_method_t fwohci_methods[] = {
364 	/* Device interface */
365 	DEVMETHOD(device_probe,		fwohci_pci_probe),
366 	DEVMETHOD(device_attach,	fwohci_pci_attach),
367 	DEVMETHOD(device_detach,	fwohci_pci_detach),
368 	DEVMETHOD(device_suspend,	fwohci_pci_suspend),
369 	DEVMETHOD(device_resume,	fwohci_pci_resume),
370 	DEVMETHOD(device_shutdown,	fwohci_pci_shutdown),
371 
372 	/* Bus interface */
373 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
374 
375 	{ 0, 0 }
376 };
377 
378 static driver_t fwohci_driver = {
379 	"fwohci",
380 	fwohci_methods,
381 	sizeof(fwohci_softc_t),
382 };
383 
384 static devclass_t fwohci_devclass;
385 
386 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0);
387 DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0);
388