xref: /netbsd-src/sys/arch/sparc64/dev/ebus.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: ebus.c,v 1.64 2019/11/10 21:16:33 chs 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.64 2019/11/10 21:16:33 chs 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 #define EDB_BUSDMA	0x10
50 #define EDB_INTR	0x20
51 int ebus_debug = 0x0;
52 #define DPRINTF(l, s)   do { if (ebus_debug & l) printf s; } while (0)
53 #else
54 #define DPRINTF(l, s)
55 #endif
56 
57 #include <sys/param.h>
58 #include <sys/conf.h>
59 #include <sys/device.h>
60 #include <sys/errno.h>
61 #include <sys/extent.h>
62 #include <sys/malloc.h>
63 #include <sys/systm.h>
64 #include <sys/time.h>
65 
66 #define _SPARC_BUS_DMA_PRIVATE
67 #include <sys/bus.h>
68 #include <machine/autoconf.h>
69 #include <machine/openfirm.h>
70 
71 #include <dev/pci/pcivar.h>
72 #include <dev/pci/pcireg.h>
73 #include <dev/pci/pcidevs.h>
74 
75 #include <dev/ebus/ebusreg.h>
76 #include <dev/ebus/ebusvar.h>
77 #include <sparc64/dev/ebusvar.h>
78 
79 int	ebus_match(device_t, cfdata_t, void *);
80 void	ebus_attach(device_t, device_t, void *);
81 
82 CFATTACH_DECL_NEW(ebus, sizeof(struct ebus_softc),
83     ebus_match, ebus_attach, NULL, NULL);
84 
85 /*
86  * here are our bus space and bus DMA routines.
87  */
88 static int _ebus_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int, vaddr_t,
89 	bus_space_handle_t *);
90 static void *ebus_intr_establish(bus_space_tag_t, int, int, int (*)(void *),
91 	void *, void(*)(void));
92 
93 int
94 ebus_match(device_t parent, cfdata_t match, void *aux)
95 {
96 	struct pci_attach_args *pa = aux;
97 	char *name;
98 	int node;
99 
100 	/* Only attach if there's a PROM node. */
101 	node = PCITAG_NODE(pa->pa_tag);
102 	if (node == -1)
103 		return (0);
104 
105 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE)
106 		return (0);
107 
108 	/* Match a real ebus */
109 	name = prom_getpropstring(node, "name");
110 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
111 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS &&
112 		strcmp(name, "ebus") == 0)
113 		return (1);
114 
115 	/* Or a real ebus III */
116 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
117 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUSIII &&
118 		strcmp(name, "ebus") == 0)
119 		return (1);
120 
121 	/* Or a PCI-ISA bridge XXX I hope this is on-board. */
122 	if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA) {
123 		return (1);
124 	}
125 
126 	/* Or the Altera bridge on SPARCle */
127 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ALTERA &&
128 	    PCI_PRODUCT(pa->pa_id) == 0 &&
129 		strcmp(name, "ebus") == 0)
130 		return (1);
131 
132 	return (0);
133 }
134 
135 /*
136  * attach an ebus and all its children.  this code is modeled
137  * after the sbus code which does similar things.
138  */
139 void
140 ebus_attach(device_t parent, device_t self, void *aux)
141 {
142 	struct ebus_softc *sc = device_private(self);
143 	struct pci_attach_args *pa = aux;
144 	struct ebus_attach_args eba;
145 	struct ebus_interrupt_map_mask *immp;
146 	int node, nmapmask, error;
147 	char devinfo[256];
148 
149 	sc->sc_dev = self;
150 
151 	aprint_naive("\n");
152 
153 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
154 	aprint_normal(": %s, revision 0x%02x\n", devinfo,
155 	    PCI_REVISION(pa->pa_class));
156 
157 	sc->sc_memtag = pa->pa_memt;
158 	sc->sc_iotag = pa->pa_iot;
159 	sc->sc_childbustag = ebus_alloc_bus_tag(sc, PCI_MEMORY_BUS_SPACE);
160 	sc->sc_dmatag = pa->pa_dmat;
161 
162 	node = PCITAG_NODE(pa->pa_tag);
163 	if (node == -1)
164 		panic("could not find ebus node");
165 
166 	sc->sc_node = node;
167 
168 	/*
169 	 * fill in our softc with information from the prom
170 	 */
171 	sc->sc_intmap = NULL;
172 	sc->sc_range = NULL;
173 	sc->sc_nintmap = 0;
174 	error = prom_getprop(node, "interrupt-map",
175 			sizeof(struct ebus_interrupt_map),
176 			&sc->sc_nintmap, &sc->sc_intmap);
177 	switch (error) {
178 	case 0:
179 		immp = &sc->sc_intmapmask;
180 		nmapmask = sizeof(*immp);
181 		error = prom_getprop(node, "interrupt-map-mask",
182 			    sizeof(struct ebus_interrupt_map_mask), &nmapmask,
183 			    &immp);
184 		if (error)
185 			panic("could not get ebus interrupt-map-mask, error %d",
186 			    error);
187 		if (nmapmask != 1)
188 			panic("ebus interrupt-map-mask is broken");
189 		break;
190 	case ENOENT:
191 		break;
192 	default:
193 		panic("ebus interrupt-map: error %d", error);
194 		break;
195 	}
196 
197 	error = prom_getprop(node, "ranges", sizeof(struct ebus_ranges),
198 	    &sc->sc_nrange, &sc->sc_range);
199 	if (error)
200 		panic("ebus ranges: error %d", error);
201 
202 	/*
203 	 * now attach all our children
204 	 */
205 	DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
206 	for (node = firstchild(node); node; node = nextsibling(node)) {
207 		char *name = prom_getpropstring(node, "name");
208 
209 		if (ebus_setup_attach_args(sc, node, &eba) != 0) {
210 			aprint_error("ebus_attach: %s: incomplete\n", name);
211 			continue;
212 		} else {
213 			DPRINTF(EDB_CHILD, ("- found child `%s', attaching\n",
214 			    eba.ea_name));
215 			(void)config_found(self, &eba, ebus_print);
216 		}
217 		ebus_destroy_attach_args(&eba);
218 	}
219 }
220 
221 int	ebus_setup_attach_args(struct ebus_softc *, int,
222 	    struct ebus_attach_args *);
223 int
224 ebus_setup_attach_args(struct ebus_softc *sc, int node,
225 	struct ebus_attach_args	*ea)
226 {
227 	int	n, rv;
228 
229 	memset(ea, 0, sizeof(struct ebus_attach_args));
230 	n = 0;
231 	rv = prom_getprop(node, "name", 1, &n, &ea->ea_name);
232 	if (rv != 0)
233 		return (rv);
234 	KASSERT(ea->ea_name[n-1] == '\0');
235 
236 	ea->ea_node = node;
237 	ea->ea_bustag = sc->sc_childbustag;
238 	ea->ea_dmatag = sc->sc_dmatag;
239 
240 	rv = prom_getprop(node, "reg", sizeof(struct ebus_regs), &ea->ea_nreg,
241 	    &ea->ea_reg);
242 	if (rv)
243 		return (rv);
244 
245 	rv = prom_getprop(node, "address", sizeof(uint32_t), &ea->ea_nvaddr,
246 	    &ea->ea_vaddr);
247 	if (rv != ENOENT) {
248 		if (rv)
249 			return (rv);
250 
251 		if (ea->ea_nreg != ea->ea_nvaddr)
252 			printf("ebus loses: device %s: %d regs and %d addrs\n",
253 			    ea->ea_name, ea->ea_nreg, ea->ea_nvaddr);
254 	} else
255 		ea->ea_nvaddr = 0;
256 
257 	if (prom_getprop(node, "interrupts", sizeof(uint32_t), &ea->ea_nintr,
258 	    &ea->ea_intr))
259 		ea->ea_nintr = 0;
260 	else
261 		ebus_find_ino(sc, ea);
262 
263 	return (0);
264 }
265 
266 void
267 ebus_destroy_attach_args(struct ebus_attach_args *ea)
268 {
269 
270 	if (ea->ea_name)
271 		free((void *)ea->ea_name, M_DEVBUF);
272 	if (ea->ea_reg)
273 		free((void *)ea->ea_reg, M_DEVBUF);
274 	if (ea->ea_intr)
275 		free((void *)ea->ea_intr, M_DEVBUF);
276 	if (ea->ea_vaddr)
277 		free((void *)ea->ea_vaddr, M_DEVBUF);
278 }
279 
280 int
281 ebus_print(void *aux, const char *p)
282 {
283 	struct ebus_attach_args *ea = aux;
284 	int i;
285 
286 	if (p)
287 		aprint_normal("%s at %s", ea->ea_name, p);
288 	for (i = 0; i < ea->ea_nreg; i++)
289 		aprint_normal("%s %x-%x", i == 0 ? " addr" : ",",
290 		    ea->ea_reg[i].lo,
291 		    ea->ea_reg[i].lo + ea->ea_reg[i].size - 1);
292 	for (i = 0; i < ea->ea_nintr; i++)
293 		aprint_normal(" ipl %x", ea->ea_intr[i]);
294 	return (UNCONF);
295 }
296 
297 
298 /*
299  * find the INO values for each interrupt and fill them in.
300  *
301  * for each "reg" property of this device, mask its hi and lo
302  * values with the "interrupt-map-mask"'s hi/lo values, and also
303  * mask the interrupt number with the interrupt mask.  search the
304  * "interrupt-map" list for matching values of hi, lo and interrupt
305  * to give the INO for this interrupt.
306  */
307 void
308 ebus_find_ino(struct ebus_softc *sc, struct ebus_attach_args *ea)
309 {
310 	uint32_t hi, lo, intr;
311 	int i, j, k;
312 
313 	if (sc->sc_nintmap == 0) {
314 		for (i = 0; i < ea->ea_nintr; i++) {
315 			OF_mapintr(ea->ea_node, &ea->ea_intr[i],
316 				sizeof(ea->ea_intr[0]),
317 				sizeof(ea->ea_intr[0]));
318 		}
319 		return;
320 	}
321 
322 	DPRINTF(EDB_INTRMAP,
323 	    ("ebus_find_ino: searching %d interrupts", ea->ea_nintr));
324 
325 	for (j = 0; j < ea->ea_nintr; j++) {
326 
327 		intr = ea->ea_intr[j] & sc->sc_intmapmask.intr;
328 
329 		DPRINTF(EDB_INTRMAP,
330 		    ("; intr %x masked to %x", ea->ea_intr[j], intr));
331 		for (i = 0; i < ea->ea_nreg; i++) {
332 			hi = ea->ea_reg[i].hi & sc->sc_intmapmask.hi;
333 			lo = ea->ea_reg[i].lo & sc->sc_intmapmask.lo;
334 
335 			DPRINTF(EDB_INTRMAP,
336 			    ("; reg hi.lo %08x.%08x masked to %08x.%08x",
337 			    ea->ea_reg[i].hi, ea->ea_reg[i].lo, hi, lo));
338 			for (k = 0; k < sc->sc_nintmap; k++) {
339 				DPRINTF(EDB_INTRMAP,
340 				    ("; checking hi.lo %08x.%08x intr %x",
341 				    sc->sc_intmap[k].hi, sc->sc_intmap[k].lo,
342 				    sc->sc_intmap[k].intr));
343 				if (hi == sc->sc_intmap[k].hi &&
344 				    lo == sc->sc_intmap[k].lo &&
345 				    intr == sc->sc_intmap[k].intr) {
346 					ea->ea_intr[j] =
347 					    sc->sc_intmap[k].cintr;
348 					DPRINTF(EDB_INTRMAP,
349 					    ("; FOUND IT! changing to %d\n",
350 					    sc->sc_intmap[k].cintr));
351 					goto next_intr;
352 				}
353 			}
354 		}
355 next_intr:;
356 	}
357 }
358 
359 /*
360  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion
361  * about PCI physical addresses, which also applies to ebus.
362  */
363 bus_space_tag_t
364 ebus_alloc_bus_tag(struct ebus_softc *sc, int type)
365 {
366 	bus_space_tag_t bt;
367 
368 	bt = malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_WAITOK | M_ZERO);
369 	bt->cookie = sc;
370 	bt->parent = sc->sc_memtag;
371 	bt->type = type;
372 	bt->sparc_bus_map = _ebus_bus_map;
373 	bt->sparc_bus_mmap = ebus_bus_mmap;
374 	bt->sparc_intr_establish = ebus_intr_establish;
375 	return (bt);
376 }
377 
378 static int
379 _ebus_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size, int flags,
380 	vaddr_t va, bus_space_handle_t *hp)
381 {
382 	struct ebus_softc *sc = t->cookie;
383 	paddr_t offset;
384 	u_int bar;
385 	int i, ss;
386 
387 	bar = BUS_ADDR_IOSPACE(ba);
388 	offset = BUS_ADDR_PADDR(ba);
389 
390 	DPRINTF(EDB_BUSMAP,
391 		("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n",
392 		 (int)bar, (uint32_t)offset, (uint32_t)size,
393 		 flags, (void *)va));
394 
395 	for (i = 0; i < sc->sc_nrange; i++) {
396 		bus_addr_t pciaddr;
397 
398 		if (bar != sc->sc_range[i].child_hi)
399 			continue;
400 		if (offset < sc->sc_range[i].child_lo ||
401 		    (offset + size) >
402 		      (sc->sc_range[i].child_lo + sc->sc_range[i].size))
403 			continue;
404 
405 		/* Isolate address space and find the right tag */
406 		ss = (sc->sc_range[i].phys_hi>>24)&3;
407 		switch (ss) {
408 		case 1:	/* I/O space */
409 			t = sc->sc_iotag;
410 			break;
411 		case 2:	/* Memory space */
412 			t = sc->sc_memtag;
413 			break;
414 		case 0:	/* Config space */
415 		case 3:	/* 64-bit Memory space */
416 		default: /* WTF? */
417 			/* We don't handle these */
418 			panic("_ebus_bus_map: illegal space %x", ss);
419 			break;
420 		}
421 		pciaddr = ((bus_addr_t)sc->sc_range[i].phys_mid << 32UL) |
422 				       sc->sc_range[i].phys_lo;
423 		pciaddr += offset;
424 
425 		DPRINTF(EDB_BUSMAP,
426 			("_ebus_bus_map: mapping to PCI addr %x\n",
427 			 (uint32_t)pciaddr));
428 
429 		/* pass it onto the psycho */
430 		return (bus_space_map(t, pciaddr, size, flags, hp));
431 	}
432 	DPRINTF(EDB_BUSMAP, (": FAILED\n"));
433 	return (EINVAL);
434 }
435 
436 paddr_t
437 ebus_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot,
438 	int flags)
439 {
440 	bus_addr_t offset = paddr;
441 	struct ebus_softc *sc = t->cookie;
442 	int i;
443 
444 	for (i = 0; i < sc->sc_nrange; i++) {
445 		bus_addr_t paddr1 =
446 		    ((bus_addr_t)sc->sc_range[i].child_hi << 32) |
447 		    sc->sc_range[i].child_lo;
448 
449 		if (offset != paddr1)
450 			continue;
451 
452 		DPRINTF(EDB_BUSMAP, ("\n_ebus_bus_mmap: mapping paddr %qx\n",
453 		    (unsigned long long)paddr1));
454 		return (bus_space_mmap(sc->sc_memtag, paddr1, off,
455 				       prot, flags));
456 	}
457 
458 	return (-1);
459 }
460 
461 /*
462  * install an interrupt handler for a ebus device
463  */
464 void *
465 ebus_intr_establish(bus_space_tag_t t, int pri, int level,
466 	int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
467 {
468 
469 	return (bus_intr_establish(t->parent, pri, level, handler, arg));
470 }
471