xref: /netbsd/sys/arch/sparc64/dev/ebus.c (revision 6550d01e)
1 /*	$NetBSD: ebus.c,v 1.54 2010/03/11 03:54:56 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 1999, 2000, 2001 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ebus.c,v 1.54 2010/03/11 03:54:56 mrg Exp $");
31 
32 #include "opt_ddb.h"
33 
34 /*
35  * UltraSPARC 5 and beyond ebus support.
36  *
37  * note that this driver is not complete:
38  *	- interrupt establish is written and appears to work
39  *	- bus map code is written and appears to work
40  *	- ebus2 DMA code is completely unwritten, we just punt to
41  *	  the iommu.
42  */
43 
44 #ifdef DEBUG
45 #define	EDB_PROM	0x01
46 #define EDB_CHILD	0x02
47 #define	EDB_INTRMAP	0x04
48 #define EDB_BUSMAP	0x08
49 int ebus_debug = 0;
50 #define DPRINTF(l, s)   do { if (ebus_debug & l) printf s; } while (0)
51 #else
52 #define DPRINTF(l, s)
53 #endif
54 
55 #include <sys/param.h>
56 #include <sys/conf.h>
57 #include <sys/device.h>
58 #include <sys/errno.h>
59 #include <sys/extent.h>
60 #include <sys/malloc.h>
61 #include <sys/systm.h>
62 #include <sys/time.h>
63 
64 #define _SPARC_BUS_DMA_PRIVATE
65 #include <machine/bus.h>
66 #include <machine/autoconf.h>
67 #include <machine/openfirm.h>
68 
69 #include <dev/pci/pcivar.h>
70 #include <dev/pci/pcireg.h>
71 #include <dev/pci/pcidevs.h>
72 
73 #include <sparc64/dev/iommureg.h>
74 #include <sparc64/dev/iommuvar.h>
75 #include <sparc64/dev/psychoreg.h>
76 #include <sparc64/dev/psychovar.h>
77 #include <dev/ebus/ebusreg.h>
78 #include <dev/ebus/ebusvar.h>
79 
80 struct ebus_softc {
81 	struct device			sc_dev;
82 
83 	int				sc_node;
84 
85 	bus_space_tag_t			sc_memtag;	/* from pci */
86 	bus_space_tag_t			sc_iotag;	/* from pci */
87 	bus_space_tag_t			sc_childbustag;	/* pass to children */
88 	bus_dma_tag_t			sc_dmatag;
89 
90 	struct ebus_ranges		*sc_range;
91 	struct ebus_interrupt_map	*sc_intmap;
92 	struct ebus_interrupt_map_mask	sc_intmapmask;
93 
94 	int				sc_nrange;	/* counters */
95 	int				sc_nintmap;
96 };
97 
98 int	ebus_match(struct device *, struct cfdata *, void *);
99 void	ebus_attach(struct device *, struct device *, void *);
100 
101 CFATTACH_DECL(ebus, sizeof(struct ebus_softc),
102     ebus_match, ebus_attach, NULL, NULL);
103 
104 bus_space_tag_t ebus_alloc_bus_tag(struct ebus_softc *, int);
105 
106 int	ebus_setup_attach_args(struct ebus_softc *, int,
107 	    struct ebus_attach_args *);
108 void	ebus_destroy_attach_args(struct ebus_attach_args *);
109 int	ebus_print(void *, const char *);
110 void	ebus_find_ino(struct ebus_softc *, struct ebus_attach_args *);
111 
112 /*
113  * here are our bus space and bus DMA routines.
114  */
115 static paddr_t ebus_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
116 static int _ebus_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int, vaddr_t,
117 	bus_space_handle_t *);
118 static void *ebus_intr_establish(bus_space_tag_t, int, int, int (*)(void *),
119 	void *, void(*)(void));
120 
121 int
122 ebus_match(struct device *parent, struct cfdata *match, void *aux)
123 {
124 	struct pci_attach_args *pa = aux;
125 	char *name;
126 	int node;
127 
128 	/* Only attach if there's a PROM node. */
129 	node = PCITAG_NODE(pa->pa_tag);
130 	if (node == -1)
131 		return (0);
132 
133 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE)
134 		return (0);
135 
136 	/* Match a real ebus */
137 	name = prom_getpropstring(node, "name");
138 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
139 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS &&
140 		strcmp(name, "ebus") == 0)
141 		return (1);
142 
143 	/* Or a real ebus III */
144 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
145 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUSIII &&
146 		strcmp(name, "ebus") == 0)
147 		return (1);
148 
149 	/* Or a PCI-ISA bridge XXX I hope this is on-board. */
150 	if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA) {
151 		return (1);
152 	}
153 
154 	return (0);
155 }
156 
157 /*
158  * attach an ebus and all it's children.  this code is modeled
159  * after the sbus code which does similar things.
160  */
161 void
162 ebus_attach(struct device *parent, struct device *self, void *aux)
163 {
164 	struct ebus_softc *sc = (struct ebus_softc *)self;
165 	struct pci_attach_args *pa = aux;
166 	struct ebus_attach_args eba;
167 	struct ebus_interrupt_map_mask *immp;
168 	int node, nmapmask, error;
169 	char devinfo[256];
170 
171 	aprint_normal("\n");
172 	aprint_naive("\n");
173 
174 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
175 	aprint_normal_dev(self, "%s, revision 0x%02x\n", devinfo,
176 	    PCI_REVISION(pa->pa_class));
177 
178 	sc->sc_memtag = pa->pa_memt;
179 	sc->sc_iotag = pa->pa_iot;
180 	sc->sc_childbustag = ebus_alloc_bus_tag(sc, PCI_MEMORY_BUS_SPACE);
181 	sc->sc_dmatag = pa->pa_dmat;
182 
183 	node = PCITAG_NODE(pa->pa_tag);
184 	if (node == -1)
185 		panic("could not find ebus node");
186 
187 	sc->sc_node = node;
188 
189 	/*
190 	 * fill in our softc with information from the prom
191 	 */
192 	sc->sc_intmap = NULL;
193 	sc->sc_range = NULL;
194 	sc->sc_nintmap = 0;
195 	error = prom_getprop(node, "interrupt-map",
196 			sizeof(struct ebus_interrupt_map),
197 			&sc->sc_nintmap, &sc->sc_intmap);
198 	switch (error) {
199 	case 0:
200 		immp = &sc->sc_intmapmask;
201 		nmapmask = sizeof(*immp);
202 		error = prom_getprop(node, "interrupt-map-mask",
203 			    sizeof(struct ebus_interrupt_map_mask), &nmapmask,
204 			    &immp);
205 		if (error)
206 			panic("could not get ebus interrupt-map-mask, error %d",
207 			    error);
208 		if (nmapmask != 1)
209 			panic("ebus interrupt-map-mask is broken");
210 		break;
211 	case ENOENT:
212 		break;
213 	default:
214 		panic("ebus interrupt-map: error %d", error);
215 		break;
216 	}
217 
218 	error = prom_getprop(node, "ranges", sizeof(struct ebus_ranges),
219 	    &sc->sc_nrange, &sc->sc_range);
220 	if (error)
221 		panic("ebus ranges: error %d", error);
222 
223 	/*
224 	 * now attach all our children
225 	 */
226 	DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
227 	for (node = firstchild(node); node; node = nextsibling(node)) {
228 		char *name = prom_getpropstring(node, "name");
229 
230 		if (ebus_setup_attach_args(sc, node, &eba) != 0) {
231 			aprint_error("ebus_attach: %s: incomplete\n", name);
232 			continue;
233 		} else {
234 			DPRINTF(EDB_CHILD, ("- found child `%s', attaching\n",
235 			    eba.ea_name));
236 			(void)config_found(self, &eba, ebus_print);
237 		}
238 		ebus_destroy_attach_args(&eba);
239 	}
240 }
241 
242 int	ebus_setup_attach_args(struct ebus_softc *, int,
243 	    struct ebus_attach_args *);
244 int
245 ebus_setup_attach_args(struct ebus_softc *sc, int node,
246 	struct ebus_attach_args	*ea)
247 {
248 	int	n, rv;
249 
250 	memset(ea, 0, sizeof(struct ebus_attach_args));
251 	n = 0;
252 	rv = prom_getprop(node, "name", 1, &n, &ea->ea_name);
253 	if (rv != 0)
254 		return (rv);
255 	ea->ea_name[n] = '\0';
256 
257 	ea->ea_node = node;
258 	ea->ea_bustag = sc->sc_childbustag;
259 	ea->ea_dmatag = sc->sc_dmatag;
260 
261 	rv = prom_getprop(node, "reg", sizeof(struct ebus_regs), &ea->ea_nreg,
262 	    &ea->ea_reg);
263 	if (rv)
264 		return (rv);
265 
266 	rv = prom_getprop(node, "address", sizeof(uint32_t), &ea->ea_nvaddr,
267 	    &ea->ea_vaddr);
268 	if (rv != ENOENT) {
269 		if (rv)
270 			return (rv);
271 
272 		if (ea->ea_nreg != ea->ea_nvaddr)
273 			printf("ebus loses: device %s: %d regs and %d addrs\n",
274 			    ea->ea_name, ea->ea_nreg, ea->ea_nvaddr);
275 	} else
276 		ea->ea_nvaddr = 0;
277 
278 	if (prom_getprop(node, "interrupts", sizeof(uint32_t), &ea->ea_nintr,
279 	    &ea->ea_intr))
280 		ea->ea_nintr = 0;
281 	else
282 		ebus_find_ino(sc, ea);
283 
284 	return (0);
285 }
286 
287 void
288 ebus_destroy_attach_args(struct ebus_attach_args *ea)
289 {
290 
291 	if (ea->ea_name)
292 		free((void *)ea->ea_name, M_DEVBUF);
293 	if (ea->ea_reg)
294 		free((void *)ea->ea_reg, M_DEVBUF);
295 	if (ea->ea_intr)
296 		free((void *)ea->ea_intr, M_DEVBUF);
297 	if (ea->ea_vaddr)
298 		free((void *)ea->ea_vaddr, M_DEVBUF);
299 }
300 
301 int
302 ebus_print(void *aux, const char *p)
303 {
304 	struct ebus_attach_args *ea = aux;
305 	int i;
306 
307 	if (p)
308 		aprint_normal("%s at %s", ea->ea_name, p);
309 	for (i = 0; i < ea->ea_nreg; i++)
310 		aprint_normal("%s %x-%x", i == 0 ? " addr" : ",",
311 		    ea->ea_reg[i].lo,
312 		    ea->ea_reg[i].lo + ea->ea_reg[i].size - 1);
313 	for (i = 0; i < ea->ea_nintr; i++)
314 		aprint_normal(" ipl %d", ea->ea_intr[i]);
315 	return (UNCONF);
316 }
317 
318 
319 /*
320  * find the INO values for each interrupt and fill them in.
321  *
322  * for each "reg" property of this device, mask it's hi and lo
323  * values with the "interrupt-map-mask"'s hi/lo values, and also
324  * mask the interrupt number with the interrupt mask.  search the
325  * "interrupt-map" list for matching values of hi, lo and interrupt
326  * to give the INO for this interrupt.
327  */
328 void
329 ebus_find_ino(struct ebus_softc *sc, struct ebus_attach_args *ea)
330 {
331 	uint32_t hi, lo, intr;
332 	int i, j, k;
333 
334 	if (sc->sc_nintmap == 0) {
335 		for (i = 0; i < ea->ea_nintr; i++) {
336 			OF_mapintr(ea->ea_node, &ea->ea_intr[i],
337 				sizeof(ea->ea_intr[0]),
338 				sizeof(ea->ea_intr[0]));
339 		}
340 		return;
341 	}
342 
343 	DPRINTF(EDB_INTRMAP,
344 	    ("ebus_find_ino: searching %d interrupts", ea->ea_nintr));
345 
346 	for (j = 0; j < ea->ea_nintr; j++) {
347 
348 		intr = ea->ea_intr[j] & sc->sc_intmapmask.intr;
349 
350 		DPRINTF(EDB_INTRMAP,
351 		    ("; intr %x masked to %x", ea->ea_intr[j], intr));
352 		for (i = 0; i < ea->ea_nreg; i++) {
353 			hi = ea->ea_reg[i].hi & sc->sc_intmapmask.hi;
354 			lo = ea->ea_reg[i].lo & sc->sc_intmapmask.lo;
355 
356 			DPRINTF(EDB_INTRMAP,
357 			    ("; reg hi.lo %08x.%08x masked to %08x.%08x",
358 			    ea->ea_reg[i].hi, ea->ea_reg[i].lo, hi, lo));
359 			for (k = 0; k < sc->sc_nintmap; k++) {
360 				DPRINTF(EDB_INTRMAP,
361 				    ("; checking hi.lo %08x.%08x intr %x",
362 				    sc->sc_intmap[k].hi, sc->sc_intmap[k].lo,
363 				    sc->sc_intmap[k].intr));
364 				if (hi == sc->sc_intmap[k].hi &&
365 				    lo == sc->sc_intmap[k].lo &&
366 				    intr == sc->sc_intmap[k].intr) {
367 					ea->ea_intr[j] =
368 					    sc->sc_intmap[k].cintr;
369 					DPRINTF(EDB_INTRMAP,
370 					    ("; FOUND IT! changing to %d\n",
371 					    sc->sc_intmap[k].cintr));
372 					goto next_intr;
373 				}
374 			}
375 		}
376 next_intr:;
377 	}
378 }
379 
380 /*
381  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion
382  * about PCI physical addresses, which also applies to ebus.
383  */
384 bus_space_tag_t
385 ebus_alloc_bus_tag(struct ebus_softc *sc, int type)
386 {
387 	bus_space_tag_t bt;
388 
389 	bt = (bus_space_tag_t)
390 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
391 	if (bt == NULL)
392 		panic("could not allocate ebus bus tag");
393 
394 	memset(bt, 0, sizeof *bt);
395 	bt->cookie = sc;
396 	bt->parent = sc->sc_memtag;
397 	bt->type = type;
398 	bt->sparc_bus_map = _ebus_bus_map;
399 	bt->sparc_bus_mmap = ebus_bus_mmap;
400 	bt->sparc_intr_establish = ebus_intr_establish;
401 	return (bt);
402 }
403 
404 static int
405 _ebus_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size, int flags,
406 	vaddr_t va, bus_space_handle_t *hp)
407 {
408 	struct ebus_softc *sc = t->cookie;
409 	paddr_t offset;
410 	u_int bar;
411 	int i, ss;
412 
413 	bar = BUS_ADDR_IOSPACE(ba);
414 	offset = BUS_ADDR_PADDR(ba);
415 
416 	DPRINTF(EDB_BUSMAP,
417 		("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n",
418 		 (int)bar, (uint32_t)offset, (uint32_t)size,
419 		 flags, (void *)va));
420 
421 	for (i = 0; i < sc->sc_nrange; i++) {
422 		bus_addr_t pciaddr;
423 
424 		if (bar != sc->sc_range[i].child_hi)
425 			continue;
426 		if (offset < sc->sc_range[i].child_lo ||
427 		    (offset + size) >
428 		      (sc->sc_range[i].child_lo + sc->sc_range[i].size))
429 			continue;
430 
431 		/* Isolate address space and find the right tag */
432 		ss = (sc->sc_range[i].phys_hi>>24)&3;
433 		switch (ss) {
434 		case 1:	/* I/O space */
435 			t = sc->sc_iotag;
436 			break;
437 		case 2:	/* Memory space */
438 			t = sc->sc_memtag;
439 			break;
440 		case 0:	/* Config space */
441 		case 3:	/* 64-bit Memory space */
442 		default: /* WTF? */
443 			/* We don't handle these */
444 			panic("_ebus_bus_map: illegal space %x", ss);
445 			break;
446 		}
447 		pciaddr = ((bus_addr_t)sc->sc_range[i].phys_mid << 32UL) |
448 				       sc->sc_range[i].phys_lo;
449 		pciaddr += offset;
450 
451 		DPRINTF(EDB_BUSMAP,
452 			("_ebus_bus_map: mapping to PCI addr %x\n",
453 			 (uint32_t)pciaddr));
454 
455 		/* pass it onto the psycho */
456 		return (bus_space_map(t, pciaddr, size, flags, hp));
457 	}
458 	DPRINTF(EDB_BUSMAP, (": FAILED\n"));
459 	return (EINVAL);
460 }
461 
462 static paddr_t
463 ebus_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot,
464 	int flags)
465 {
466 	bus_addr_t offset = paddr;
467 	struct ebus_softc *sc = t->cookie;
468 	int i;
469 
470 	for (i = 0; i < sc->sc_nrange; i++) {
471 		bus_addr_t paddr1 =
472 		    ((bus_addr_t)sc->sc_range[i].child_hi << 32) |
473 		    sc->sc_range[i].child_lo;
474 
475 		if (offset != paddr1)
476 			continue;
477 
478 		DPRINTF(EDB_BUSMAP, ("\n_ebus_bus_mmap: mapping paddr %qx\n",
479 		    (unsigned long long)paddr1));
480 		return (bus_space_mmap(sc->sc_memtag, paddr1, off,
481 				       prot, flags));
482 	}
483 
484 	return (-1);
485 }
486 
487 /*
488  * install an interrupt handler for a ebus device
489  */
490 void *
491 ebus_intr_establish(bus_space_tag_t t, int pri, int level,
492 	int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
493 {
494 
495 	return (bus_intr_establish(t->parent, pri, level, handler, arg));
496 }
497