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