xref: /freebsd/sys/dev/firewire/fwohci_pci.c (revision ec994981447e8a974426660b5071bc405280af73)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2003 Hidetoshi Shimokawa
5  * Copyright (c) 1998-2002 Katsushi Kobayashi and Hidetoshi Shimokawa
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the acknowledgement as bellow:
18  *
19  *    This product includes software developed by K. Kobayashi and H. SHimokawa
20  *
21  * 4. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 #define BOUNCE_BUFFER_TEST	0
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/bus.h>
45 #include <sys/queue.h>
46 #include <machine/bus.h>
47 #include <sys/rman.h>
48 #include <sys/malloc.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <machine/resource.h>
52 
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcireg.h>
55 
56 #include <dev/firewire/firewire.h>
57 #include <dev/firewire/firewirereg.h>
58 
59 #include <dev/firewire/fwdma.h>
60 #include <dev/firewire/fwohcireg.h>
61 #include <dev/firewire/fwohcivar.h>
62 
63 static int fwohci_pci_attach(device_t self);
64 static int fwohci_pci_detach(device_t self);
65 
66 /*
67  * The probe routine.
68  */
69 static int
70 fwohci_pci_probe(device_t dev)
71 {
72 	uint32_t id;
73 
74 	id = pci_get_devid(dev);
75 	if (id == (FW_VENDORID_NATSEMI | FW_DEVICE_CS4210)) {
76 		device_set_desc(dev, "National Semiconductor CS4210");
77 		return BUS_PROBE_DEFAULT;
78 	}
79 	if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD861)) {
80 		device_set_desc(dev, "NEC uPD72861");
81 		return BUS_PROBE_DEFAULT;
82 	}
83 	if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD871)) {
84 		device_set_desc(dev, "NEC uPD72871/2");
85 		return BUS_PROBE_DEFAULT;
86 	}
87 	if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72870)) {
88 		device_set_desc(dev, "NEC uPD72870");
89 		return BUS_PROBE_DEFAULT;
90 	}
91 	if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72873)) {
92 		device_set_desc(dev, "NEC uPD72873");
93 		return BUS_PROBE_DEFAULT;
94 	}
95 	if (id == (FW_VENDORID_NEC | FW_DEVICE_UPD72874)) {
96 		device_set_desc(dev, "NEC uPD72874");
97 		return BUS_PROBE_DEFAULT;
98 	}
99 	if (id == (FW_VENDORID_SIS | FW_DEVICE_7007)) {
100 		/* It has no real identifier, using device id. */
101 		device_set_desc(dev, "SiS 7007");
102 		return BUS_PROBE_DEFAULT;
103 	}
104 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB22)) {
105 		device_set_desc(dev, "Texas Instruments TSB12LV22");
106 		return BUS_PROBE_DEFAULT;
107 	}
108 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB23)) {
109 		device_set_desc(dev, "Texas Instruments TSB12LV23");
110 		return BUS_PROBE_DEFAULT;
111 	}
112 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB26)) {
113 		device_set_desc(dev, "Texas Instruments TSB12LV26");
114 		return BUS_PROBE_DEFAULT;
115 	}
116 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43)) {
117 		device_set_desc(dev, "Texas Instruments TSB43AA22");
118 		return BUS_PROBE_DEFAULT;
119 	}
120 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43A)) {
121 		device_set_desc(dev, "Texas Instruments TSB43AB22/A");
122 		return BUS_PROBE_DEFAULT;
123 	}
124 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB21)) {
125 		device_set_desc(dev, "Texas Instruments TSB43AB21/A/AI/A-EP");
126 		return BUS_PROBE_DEFAULT;
127 	}
128 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB43AB23)) {
129 		device_set_desc(dev, "Texas Instruments TSB43AB23");
130 		return BUS_PROBE_DEFAULT;
131 	}
132 	if (id == (FW_VENDORID_TI | FW_DEVICE_TITSB82AA2)) {
133 		device_set_desc(dev, "Texas Instruments TSB82AA2");
134 		return BUS_PROBE_DEFAULT;
135 	}
136 	if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4450)) {
137 		device_set_desc(dev, "Texas Instruments PCI4450");
138 		return BUS_PROBE_DEFAULT;
139 	}
140 	if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4410A)) {
141 		device_set_desc(dev, "Texas Instruments PCI4410A");
142 		return BUS_PROBE_DEFAULT;
143 	}
144 	if (id == (FW_VENDORID_TI | FW_DEVICE_TIPCI4451)) {
145 		device_set_desc(dev, "Texas Instruments PCI4451");
146 		return BUS_PROBE_DEFAULT;
147 	}
148 	if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD1947)) {
149 		device_printf(dev, "Sony i.LINK (CXD1947) not supported\n");
150 		return ENXIO;
151 	}
152 	if (id == (FW_VENDORID_SONY | FW_DEVICE_CXD3222)) {
153 		device_set_desc(dev, "Sony i.LINK (CXD3222)");
154 		return BUS_PROBE_DEFAULT;
155 	}
156 	if (id == (FW_VENDORID_VIA | FW_DEVICE_VT6306)) {
157 		device_set_desc(dev, "VIA Fire II (VT6306)");
158 		return BUS_PROBE_DEFAULT;
159 	}
160 	if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C551)) {
161 		device_set_desc(dev, "Ricoh R5C551");
162 		return BUS_PROBE_DEFAULT;
163 	}
164 	if (id == (FW_VENDORID_RICOH | FW_DEVICE_R5C552)) {
165 		device_set_desc(dev, "Ricoh R5C552");
166 		return BUS_PROBE_DEFAULT;
167 	}
168 	if (id == (FW_VENDORID_APPLE | FW_DEVICE_PANGEA)) {
169 		device_set_desc(dev, "Apple Pangea");
170 		return BUS_PROBE_DEFAULT;
171 	}
172 	if (id == (FW_VENDORID_APPLE | FW_DEVICE_UNINORTH2)) {
173 		device_set_desc(dev, "Apple UniNorth 2");
174 		return BUS_PROBE_DEFAULT;
175 	}
176 	if (id == (FW_VENDORID_LUCENT | FW_DEVICE_FW322)) {
177 		device_set_desc(dev, "Lucent FW322/323");
178 		return BUS_PROBE_DEFAULT;
179 	}
180 	if (id == (FW_VENDORID_INTEL | FW_DEVICE_82372FB)) {
181 		device_set_desc(dev, "Intel 82372FB");
182 		return BUS_PROBE_DEFAULT;
183 	}
184 	if (id == (FW_VENDORID_ADAPTEC | FW_DEVICE_AIC5800)) {
185 		device_set_desc(dev, "Adaptec AHA-894x/AIC-5800");
186 		return BUS_PROBE_DEFAULT;
187 	}
188 	if (pci_get_class(dev) == PCIC_SERIALBUS
189 			&& pci_get_subclass(dev) == PCIS_SERIALBUS_FW
190 			&& pci_get_progif(dev) == PCI_INTERFACE_OHCI) {
191 		if (bootverbose)
192 			device_printf(dev, "vendor=%x, dev=%x\n",
193 			    pci_get_vendor(dev), pci_get_device(dev));
194 		device_set_desc(dev, "1394 Open Host Controller Interface");
195 		return BUS_PROBE_DEFAULT;
196 	}
197 
198 	return ENXIO;
199 }
200 
201 static int
202 fwohci_pci_init(device_t self)
203 {
204 	int olatency, latency, ocache_line, cache_line;
205 	uint16_t cmd;
206 
207 	cmd = pci_read_config(self, PCIR_COMMAND, 2);
208 	cmd |= PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN;
209 #if 1  /* for broken hardware */
210 	cmd &= ~PCIM_CMD_MWRICEN;
211 #endif
212 	pci_write_config(self, PCIR_COMMAND, cmd, 2);
213 
214 	latency = olatency = pci_read_config(self, PCIR_LATTIMER, 1);
215 #define DEF_LATENCY 0x20
216 	if (olatency < DEF_LATENCY) {
217 		latency = DEF_LATENCY;
218 		pci_write_config(self, PCIR_LATTIMER, latency, 1);
219 	}
220 
221 	cache_line = ocache_line = pci_read_config(self, PCIR_CACHELNSZ, 1);
222 #define DEF_CACHE_LINE 8
223 	if (ocache_line < DEF_CACHE_LINE) {
224 		cache_line = DEF_CACHE_LINE;
225 		pci_write_config(self, PCIR_CACHELNSZ, cache_line, 1);
226 	}
227 
228 	if (firewire_debug) {
229 		device_printf(self, "latency timer %d -> %d.\n",
230 			olatency, latency);
231 		device_printf(self, "cache size %d -> %d.\n",
232 			ocache_line, cache_line);
233 	}
234 
235 	return 0;
236 }
237 
238 static int
239 fwohci_pci_attach(device_t self)
240 {
241 	fwohci_softc_t *sc = device_get_softc(self);
242 	int err;
243 	int rid;
244 
245 #if 0
246 	if (bootverbose)
247 		firewire_debug = bootverbose;
248 #endif
249 
250 	mtx_init(FW_GMTX(&sc->fc), "firewire", NULL, MTX_DEF);
251 	fwohci_pci_init(self);
252 
253 	rid = PCI_CBMEM;
254 	sc->bsr = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 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_any(self, SYS_RES_IRQ, &rid,
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 	err = bus_setup_intr(self, sc->irq_res,
273 				INTR_TYPE_NET | INTR_MPSAFE,
274 				NULL, (driver_intr_t *) fwohci_intr,
275 				sc, &sc->ih);
276 
277 	if (err) {
278 		device_printf(self, "Could not setup irq, %d\n", err);
279 		fwohci_pci_detach(self);
280 		return ENXIO;
281 	}
282 
283 	err = bus_dma_tag_create(
284 				/*parent*/bus_get_dma_tag(self),
285 				/*alignment*/1,
286 				/*boundary*/0,
287 #if BOUNCE_BUFFER_TEST
288 				/*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
289 #else
290 				/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
291 #endif
292 				/*highaddr*/BUS_SPACE_MAXADDR,
293 				/*filter*/NULL, /*filterarg*/NULL,
294 				/*maxsize*/0x100000,
295 				/*nsegments*/0x20,
296 				/*maxsegsz*/0x8000,
297 				/*flags*/BUS_DMA_ALLOCNOW,
298 				/*lockfunc*/busdma_lock_mutex,
299 				/*lockarg*/FW_GMTX(&sc->fc),
300 				&sc->fc.dmat);
301 	if (err != 0) {
302 		device_printf(self, "fwohci_pci_attach: Could not allocate DMA "
303 		    "tag - error %d\n", err);
304 		fwohci_pci_detach(self);
305 		return (ENOMEM);
306 	}
307 
308 	err = fwohci_init(sc, self);
309 
310 	if (err != 0) {
311 		device_printf(self, "fwohci_init failed with err=%d\n", err);
312 		fwohci_pci_detach(self);
313 		return EIO;
314 	}
315 
316 	/* probe and attach a child device(firewire) */
317 	bus_generic_probe(self);
318 	bus_generic_attach(self);
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 	s = splfw();
330 
331 	if (sc->bsr)
332 		fwohci_stop(sc, self);
333 
334 	bus_generic_detach(self);
335 
336 	if (sc->fc.bdev) {
337 		device_delete_child(self, sc->fc.bdev);
338 		sc->fc.bdev = NULL;
339 	}
340 
341 	/* disable interrupts that might have been switched on */
342 	if (sc->bst && sc->bsh)
343 		bus_space_write_4(sc->bst, sc->bsh,
344 				  FWOHCI_INTMASKCLR, OHCI_INT_EN);
345 
346 	if (sc->irq_res) {
347 		int err;
348 		if (sc->ih) {
349 			err = bus_teardown_intr(self, sc->irq_res, sc->ih);
350 			if (err)
351 				device_printf(self,
352 					 "Could not tear down irq, %d\n", err);
353 			sc->ih = NULL;
354 		}
355 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->irq_res);
356 		sc->irq_res = NULL;
357 	}
358 
359 	if (sc->bsr) {
360 		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM, sc->bsr);
361 		sc->bsr = NULL;
362 		sc->bst = 0;
363 		sc->bsh = 0;
364 	}
365 
366 	fwohci_detach(sc, self);
367 	mtx_destroy(FW_GMTX(&sc->fc));
368 	splx(s);
369 
370 	return 0;
371 }
372 
373 static int
374 fwohci_pci_suspend(device_t dev)
375 {
376 	fwohci_softc_t *sc = device_get_softc(dev);
377 	int err;
378 
379 	device_printf(dev, "fwohci_pci_suspend\n");
380 	err = bus_generic_suspend(dev);
381 	if (err)
382 		return err;
383 	fwohci_stop(sc, dev);
384 	return 0;
385 }
386 
387 static int
388 fwohci_pci_resume(device_t dev)
389 {
390 	fwohci_softc_t *sc = device_get_softc(dev);
391 
392 	fwohci_pci_init(dev);
393 	fwohci_resume(sc, dev);
394 	return 0;
395 }
396 
397 static int
398 fwohci_pci_shutdown(device_t dev)
399 {
400 	fwohci_softc_t *sc = device_get_softc(dev);
401 
402 	bus_generic_shutdown(dev);
403 	fwohci_stop(sc, dev);
404 	return 0;
405 }
406 
407 static device_t
408 fwohci_pci_add_child(device_t dev, u_int order, const char *name, int unit)
409 {
410 	struct fwohci_softc *sc;
411 	device_t child;
412 	int err = 0;
413 
414 	sc = (struct fwohci_softc *)device_get_softc(dev);
415 	child = device_add_child(dev, name, unit);
416 	if (child == NULL)
417 		return (child);
418 
419 	sc->fc.bdev = child;
420 	device_set_ivars(child, &sc->fc);
421 
422 	err = device_probe_and_attach(child);
423 	if (err) {
424 		device_printf(dev, "probe_and_attach failed with err=%d\n",
425 		    err);
426 		fwohci_pci_detach(dev);
427 		device_delete_child(dev, child);
428 		return NULL;
429 	}
430 
431 	/* XXX
432 	 * Clear the bus reset event flag to start transactions even when
433 	 * interrupt is disabled during the boot process.
434 	 */
435 	if (cold) {
436 		int s;
437 		DELAY(250); /* 2 cycles */
438 		s = splfw();
439 		fwohci_poll(&sc->fc, 0, -1);
440 		splx(s);
441 	}
442 
443 	return (child);
444 }
445 
446 static device_method_t fwohci_methods[] = {
447 	/* Device interface */
448 	DEVMETHOD(device_probe,		fwohci_pci_probe),
449 	DEVMETHOD(device_attach,	fwohci_pci_attach),
450 	DEVMETHOD(device_detach,	fwohci_pci_detach),
451 	DEVMETHOD(device_suspend,	fwohci_pci_suspend),
452 	DEVMETHOD(device_resume,	fwohci_pci_resume),
453 	DEVMETHOD(device_shutdown,	fwohci_pci_shutdown),
454 
455 	/* Bus interface */
456 	DEVMETHOD(bus_add_child,	fwohci_pci_add_child),
457 
458 	DEVMETHOD_END
459 };
460 
461 static driver_t fwohci_driver = {
462 	"fwohci",
463 	fwohci_methods,
464 	sizeof(fwohci_softc_t),
465 };
466 
467 #ifdef FWOHCI_MODULE
468 MODULE_DEPEND(fwohci, firewire, 1, 1, 1);
469 #endif
470 DRIVER_MODULE(fwohci, pci, fwohci_driver, 0, 0);
471