xref: /freebsd/sys/dev/firewire/fwohci_pci.c (revision c26f4c716890930bf3180af2ceca6c2c16ed6b5b)
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_vendor(dev) << 16) | pci_get_device(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_TI | FW_DEVICE_TITSB22)) {
74 		device_set_desc(dev, "Texas Instruments TSB12LV22");
75 		return 0;
76 	}
77 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
78 		device_set_desc(dev, "Texas Instruments TSB12LV23");
79 		return 0;
80 	}
81 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
82 		device_set_desc(dev, "Texas Instruments TSB12LV26");
83 		return 0;
84 	}
85 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
86 		device_set_desc(dev, "Texas Instruments TSB43AA22");
87 		return 0;
88 	}
89 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
90 		device_set_desc(dev, "Texas Instruments TSB43AB22/A");
91 		return 0;
92 	}
93 	if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
94 		device_set_desc(dev, "Texas Instruments PCI4450");
95 		return 0;
96 	}
97 	if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
98 		device_set_desc(dev, "Texas Instruments PCI4410A");
99 		return 0;
100 	}
101 	if (id == (FW_VENDORID_SONY | FW_DEVICE_CX3022)) {
102 		device_set_desc(dev, "SONY CX3022");
103 		return 0;
104 	}
105 	if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
106 		device_set_desc(dev, "VIA VT6306");
107 		return 0;
108 	}
109 	if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
110 		device_set_desc(dev, "Ricoh R5C552");
111 		return 0;
112 	}
113 	if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
114 		device_set_desc(dev, "Apple Pangea");
115 		return 0;
116 	}
117 	if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH)) {
118 		device_set_desc(dev, "Apple UniNorth");
119 		return 0;
120 	}
121 	if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
122 		device_set_desc(dev, "Lucent FW322/323");
123 		return 0;
124 	}
125 #endif
126 	if (pci_get_class(dev) == PCIC_SERIALBUS
127 			&& pci_get_subclass(dev) == PCIS_SERIALBUS_FW
128 			&& pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
129 		device_printf(dev, "vendor=%x, dev=%x\n", pci_get_vendor(dev),
130 			pci_get_device(dev));
131 		device_set_desc(dev, "1394 Open Host Controller Interface");
132 		return 0;
133 	}
134 
135 	return ENXIO;
136 }
137 
138 #if __FreeBSD_version < 500000
139 static void
140 fwohci_dummy_intr(void *arg)
141 {
142 	/* XXX do nothing */
143 }
144 #endif
145 
146 static int
147 fwohci_pci_init(device_t self)
148 {
149 	int latency, cache_line;
150 	u_int16_t cmd;
151 
152 	cmd = pci_read_config(self, PCIR_COMMAND, 2);
153 	cmd |= PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN;
154 	pci_write_config(self, PCIR_COMMAND, cmd, 2);
155 
156 	latency = pci_read_config(self, PCIR_LATTIMER, 1);
157 #define DEF_LATENCY 250 /* Derived from Max Bulk Transfer size 512 Bytes*/
158 	if( latency < DEF_LATENCY ) {
159 		latency = DEF_LATENCY;
160 		device_printf(self, "PCI bus latency was changing to");
161 		pci_write_config(self, PCIR_LATTIMER,latency, 1);
162 	} else
163 	 {
164 		device_printf(self, "PCI bus latency is");
165 	}
166 	printf(" %d.\n", (int) latency);
167 	cache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
168 #if 0
169 #define DEF_CACHE_LINE 0xc
170 	cache_line = DEF_CACHE_LINE;
171 	pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
172 #endif
173 	if (bootverbose)
174 		device_printf(self, "cache size %d.\n", (int) cache_line);
175 
176 	return 0;
177 }
178 
179 static int
180 fwohci_pci_attach(device_t self)
181 {
182 	fwohci_softc_t *sc = device_get_softc(self);
183 	int err;
184 	int rid;
185 #if __FreeBSD_version < 500000
186 	int intr;
187 	/* For the moment, put in a message stating what is wrong */
188 	intr = pci_read_config(self, PCIR_INTLINE, 1);
189 	if (intr == 0 || intr == 255) {
190 		device_printf(self, "Invalid irq %d\n", intr);
191 #ifdef __i386__
192 		device_printf(self, "Please switch PNP-OS to 'No' in BIOS\n");
193 #endif
194 #if 0
195 		return ENXIO;
196 #endif
197 	}
198 #endif
199 
200 	fwohci_pci_init(self);
201 
202 	rid = PCI_CBMEM;
203 	sc->bsr = bus_alloc_resource(self, SYS_RES_MEMORY, &rid,
204 					0, ~0, 1, RF_ACTIVE);
205 	if (!sc->bsr) {
206 		device_printf(self, "Could not map memory\n");
207 		return ENXIO;
208         }
209 
210 	sc->bst = rman_get_bustag(sc->bsr);
211 	sc->bsh = rman_get_bushandle(sc->bsr);
212 
213 	rid = 0;
214 	sc->irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid, 0, ~0, 1,
215 				     RF_SHAREABLE | RF_ACTIVE);
216 	if (sc->irq_res == NULL) {
217 		device_printf(self, "Could not allocate irq\n");
218 		fwohci_pci_detach(self);
219 		return ENXIO;
220 	}
221 
222 	sc->fc.bdev = device_add_child(self, "firewire", -1);
223 	if (!sc->fc.bdev) {
224 		device_printf(self, "Could not add firewire device\n");
225 		fwohci_pci_detach(self);
226 		return ENOMEM;
227 	}
228 	device_set_ivars(sc->fc.bdev, sc);
229 
230 	err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_NET,
231 		     (driver_intr_t *) fwohci_intr, sc, &sc->ih);
232 #if __FreeBSD_version < 500000
233 	/* XXX splcam() should mask this irq for sbp.c*/
234 	err = bus_setup_intr(self, sc->irq_res, INTR_TYPE_CAM,
235 		     (driver_intr_t *) fwohci_dummy_intr, sc, &sc->ih_cam);
236 #endif
237 	if (err) {
238 		device_printf(self, "Could not setup irq, %d\n", err);
239 		fwohci_pci_detach(self);
240 		return ENXIO;
241 	}
242 
243 	err = fwohci_init(sc, self);
244 
245 	if (!err)
246 		err = device_probe_and_attach(sc->fc.bdev);
247 
248 	if (err) {
249 		device_printf(self, "FireWire init failed\n");
250 		fwohci_pci_detach(self);
251 		return EIO;
252 	}
253 
254 	/* XXX
255 	 * Clear the bus reset event flag to start transactions even when
256 	 * interrupt is disabled during the boot process.
257 	 */
258 #if 0
259 	DELAY(100);
260 #endif
261 	fwohci_intr((void *)sc);
262 
263 	return 0;
264 }
265 
266 static int
267 fwohci_pci_detach(device_t self)
268 {
269 	fwohci_softc_t *sc = device_get_softc(self);
270 	int s;
271 
272 
273 	s = splfw();
274 
275 	fwohci_stop(sc, self);
276 	bus_generic_detach(self);
277 
278 	/* disable interrupts that might have been switched on */
279 	if (sc->bst && sc->bsh)
280 		bus_space_write_4(sc->bst, sc->bsh,
281 				  FWOHCI_INTMASKCLR, OHCI_INT_EN);
282 
283 	if (sc->irq_res) {
284 		int err = bus_teardown_intr(self, sc->irq_res, sc->ih);
285 		if (err)
286 			/* XXX or should we panic? */
287 			device_printf(self, "Could not tear down irq, %d\n",
288 				      err);
289 #if __FreeBSD_version < 500000
290 		err = bus_teardown_intr(self, sc->irq_res, sc->ih_cam);
291 #endif
292 		sc->ih = NULL;
293 	}
294 
295 	if (sc->fc.bdev) {
296 		device_delete_child(self, sc->fc.bdev);
297 		sc->fc.bdev = NULL;
298 	}
299 
300 	if (sc->irq_res) {
301 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
302 		sc->irq_res = NULL;
303 	}
304 
305 	if (sc->bsr) {
306 		bus_release_resource(self, SYS_RES_MEMORY,PCI_CBMEM,sc->bsr);
307 		sc->bsr = NULL;
308 		sc->bst = 0;
309 		sc->bsh = 0;
310 	}
311 
312 	fwohci_detach(sc, self);
313 	splx(s);
314 
315 	return 0;
316 }
317 
318 static int
319 fwohci_pci_suspend(device_t dev)
320 {
321 	int err;
322 
323 	device_printf(dev, "fwohci_pci_suspend\n");
324 	err = bus_generic_suspend(dev);
325 	if (err)
326 		return err;
327 	/* fwohci_stop(dev); */
328 	return 0;
329 }
330 
331 static int
332 fwohci_pci_resume(device_t dev)
333 {
334 	fwohci_softc_t *sc = device_get_softc(dev);
335 
336 	device_printf(dev, "fwohci_pci_resume: power_state = 0x%08x\n",
337 					pci_get_powerstate(dev));
338 	fwohci_pci_init(dev);
339 	fwohci_resume(sc, dev);
340 	return 0;
341 }
342 
343 static int
344 fwohci_pci_shutdown(device_t dev)
345 {
346 	fwohci_softc_t *sc = device_get_softc(dev);
347 
348 	bus_generic_shutdown(dev);
349 	fwohci_stop(sc, dev);
350 	return 0;
351 }
352 
353 static device_method_t fwohci_methods[] = {
354 	/* Device interface */
355 	DEVMETHOD(device_probe,		fwohci_pci_probe),
356 	DEVMETHOD(device_attach,	fwohci_pci_attach),
357 	DEVMETHOD(device_detach,	fwohci_pci_detach),
358 	DEVMETHOD(device_suspend,	fwohci_pci_suspend),
359 	DEVMETHOD(device_resume,	fwohci_pci_resume),
360 	DEVMETHOD(device_shutdown,	fwohci_pci_shutdown),
361 
362 	/* Bus interface */
363 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
364 
365 	{ 0, 0 }
366 };
367 
368 static driver_t fwohci_driver = {
369 	"fwohci",
370 	fwohci_methods,
371 	sizeof(fwohci_softc_t),
372 };
373 
374 static devclass_t fwohci_devclass;
375 
376 DRIVER_MODULE(fwohci, pci, fwohci_driver, fwohci_devclass, 0, 0);
377 DRIVER_MODULE(fwohci, cardbus, fwohci_driver, fwohci_devclass, 0, 0);
378