xref: /netbsd-src/sys/arch/sparc/dev/sbus.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: sbus.c,v 1.56 2003/01/01 02:20:47 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1992, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * This software was developed by the Computer Systems Engineering group
44  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
45  * contributed to Berkeley.
46  *
47  * All advertising materials mentioning features or use of this software
48  * must display the following acknowledgement:
49  *	This product includes software developed by the University of
50  *	California, Lawrence Berkeley Laboratory.
51  *
52  * Redistribution and use in source and binary forms, with or without
53  * modification, are permitted provided that the following conditions
54  * are met:
55  * 1. Redistributions of source code must retain the above copyright
56  *    notice, this list of conditions and the following disclaimer.
57  * 2. Redistributions in binary form must reproduce the above copyright
58  *    notice, this list of conditions and the following disclaimer in the
59  *    documentation and/or other materials provided with the distribution.
60  * 3. All advertising materials mentioning features or use of this software
61  *    must display the following acknowledgement:
62  *	This product includes software developed by the University of
63  *	California, Berkeley and its contributors.
64  * 4. Neither the name of the University nor the names of its contributors
65  *    may be used to endorse or promote products derived from this software
66  *    without specific prior written permission.
67  *
68  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78  * SUCH DAMAGE.
79  *
80  *	@(#)sbus.c	8.1 (Berkeley) 6/11/93
81  */
82 
83 /*
84  * Sbus stuff.
85  */
86 
87 #include <sys/param.h>
88 #include <sys/malloc.h>
89 #include <sys/kernel.h>
90 #include <sys/systm.h>
91 #include <sys/device.h>
92 
93 #include <uvm/uvm_extern.h>
94 
95 #include <machine/autoconf.h>
96 #include <machine/bus.h>
97 #include <sparc/dev/sbusreg.h>
98 #include <dev/sbus/sbusvar.h>
99 #include <dev/sbus/xboxvar.h>
100 
101 #include <sparc/sparc/iommuvar.h>
102 
103 void sbusreset __P((int));
104 
105 static bus_space_tag_t sbus_alloc_bustag __P((struct sbus_softc *));
106 static int sbus_get_intr __P((struct sbus_softc *, int,
107 			      struct openprom_intr **, int *));
108 static void *sbus_intr_establish __P((
109 		bus_space_tag_t,
110 		int,			/*Sbus interrupt level*/
111 		int,			/*`device class' priority*/
112 		int (*) __P((void *)),	/*handler*/
113 		void *,			/*handler arg*/
114 		void (*) __P((void))));	/*fast handler*/
115 
116 
117 /* autoconfiguration driver */
118 int	sbus_match_mainbus __P((struct device *, struct cfdata *, void *));
119 int	sbus_match_iommu __P((struct device *, struct cfdata *, void *));
120 int	sbus_match_xbox __P((struct device *, struct cfdata *, void *));
121 void	sbus_attach_mainbus __P((struct device *, struct device *, void *));
122 void	sbus_attach_iommu __P((struct device *, struct device *, void *));
123 void	sbus_attach_xbox __P((struct device *, struct device *, void *));
124 
125 static	int sbus_error __P((void));
126 int	(*sbuserr_handler) __P((void));
127 
128 CFATTACH_DECL(sbus_mainbus, sizeof(struct sbus_softc),
129     sbus_match_mainbus, sbus_attach_mainbus, NULL, NULL);
130 
131 CFATTACH_DECL(sbus_iommu, sizeof(struct sbus_softc),
132     sbus_match_iommu, sbus_attach_iommu, NULL, NULL);
133 
134 CFATTACH_DECL(sbus_xbox, sizeof(struct sbus_softc),
135     sbus_match_xbox, sbus_attach_xbox, NULL, NULL);
136 
137 extern struct cfdriver sbus_cd;
138 
139 /* The "primary" Sbus */
140 struct sbus_softc *sbus_sc;
141 
142 /* If the PROM does not provide the `ranges' property, we make up our own */
143 struct openprom_range sbus_translations[] = {
144 	/* Assume a maximum of 4 Sbus slots, all mapped to on-board io space */
145 	{ 0, 0, PMAP_OBIO, SBUS_ADDR(0,0), 1 << 25 },
146 	{ 1, 0, PMAP_OBIO, SBUS_ADDR(1,0), 1 << 25 },
147 	{ 2, 0, PMAP_OBIO, SBUS_ADDR(2,0), 1 << 25 },
148 	{ 3, 0, PMAP_OBIO, SBUS_ADDR(3,0), 1 << 25 }
149 };
150 
151 /*
152  * Child devices receive the Sbus interrupt level in their attach
153  * arguments. We translate these to CPU IPLs using the following
154  * tables. Note: obio bus interrupt levels are identical to the
155  * processor IPL.
156  *
157  * The second set of tables is used when the Sbus interrupt level
158  * cannot be had from the PROM as an `interrupt' property. We then
159  * fall back on the `intr' property which contains the CPU IPL.
160  */
161 
162 /* Translate Sbus interrupt level to processor IPL */
163 static int intr_sbus2ipl_4c[] = {
164 	0, 1, 2, 3, 5, 7, 8, 9
165 };
166 static int intr_sbus2ipl_4m[] = {
167 	0, 2, 3, 5, 7, 9, 11, 13
168 };
169 
170 /*
171  * This value is or'ed into the attach args' interrupt level cookie
172  * if the interrupt level comes from an `intr' property, i.e. it is
173  * not an Sbus interrupt level.
174  */
175 #define SBUS_INTR_COMPAT	0x80000000
176 
177 
178 /*
179  * Print the location of some sbus-attached device (called just
180  * before attaching that device).  If `sbus' is not NULL, the
181  * device was found but not configured; print the sbus as well.
182  * Return UNCONF (config_find ignores this if the device was configured).
183  */
184 int
185 sbus_print(args, busname)
186 	void *args;
187 	const char *busname;
188 {
189 	struct sbus_attach_args *sa = args;
190 	int i;
191 
192 	if (busname)
193 		aprint_normal("%s at %s", sa->sa_name, busname);
194 	aprint_normal(" slot %d offset 0x%x", sa->sa_slot, sa->sa_offset);
195 	for (i = 0; i < sa->sa_nintr; i++) {
196 		u_int32_t level = sa->sa_intr[i].oi_pri;
197 		struct sbus_softc *sc =
198 			(struct sbus_softc *) sa->sa_bustag->cookie;
199 
200 		aprint_normal(" level %d", level & ~SBUS_INTR_COMPAT);
201 		if ((level & SBUS_INTR_COMPAT) == 0) {
202 			int ipl = sc->sc_intr2ipl[level];
203 			if (ipl != level)
204 				aprint_normal(" (ipl %d)", ipl);
205 		}
206 	}
207 	return (UNCONF);
208 }
209 
210 int
211 sbus_match_mainbus(parent, cf, aux)
212 	struct device *parent;
213 	struct cfdata *cf;
214 	void *aux;
215 {
216 	struct mainbus_attach_args *ma = aux;
217 
218 	if (CPU_ISSUN4)
219 		return (0);
220 
221 	return (strcmp(cf->cf_name, ma->ma_name) == 0);
222 }
223 
224 int
225 sbus_match_iommu(parent, cf, aux)
226 	struct device *parent;
227 	struct cfdata *cf;
228 	void *aux;
229 {
230 	struct iommu_attach_args *ia = aux;
231 
232 	if (CPU_ISSUN4)
233 		return (0);
234 
235 	return (strcmp(cf->cf_name, ia->iom_name) == 0);
236 }
237 
238 int
239 sbus_match_xbox(parent, cf, aux)
240 	struct device *parent;
241 	struct cfdata *cf;
242 	void *aux;
243 {
244 	struct xbox_attach_args *xa = aux;
245 
246 	if (CPU_ISSUN4)
247 		return (0);
248 
249 	return (strcmp(cf->cf_name, xa->xa_name) == 0);
250 }
251 
252 /*
253  * Attach an Sbus.
254  */
255 void
256 sbus_attach_mainbus(parent, self, aux)
257 	struct device *parent;
258 	struct device *self;
259 	void *aux;
260 {
261 	struct sbus_softc *sc = (struct sbus_softc *)self;
262 	struct mainbus_attach_args *ma = aux;
263 	int node = ma->ma_node;
264 
265 	/*
266 	 * XXX there is only one Sbus, for now -- do not know how to
267 	 * address children on others
268 	 */
269 	if (sc->sc_dev.dv_unit > 0) {
270 		printf(" unsupported\n");
271 		return;
272 	}
273 
274 	sc->sc_bustag = ma->ma_bustag;
275 	sc->sc_dmatag = ma->ma_dmatag;
276 
277 #if 0	/* sbus at mainbus (sun4c): `reg' prop is not control space */
278 	if (ma->ma_size == 0)
279 		printf("%s: no Sbus registers", self->dv_xname);
280 
281 	if (bus_space_map(ma->ma_bustag,
282 			  ma->ma_paddr,
283 			  ma->ma_size,
284 			  BUS_SPACE_MAP_LINEAR,
285 			  &sc->sc_bh) != 0) {
286 		panic("%s: can't map sbusbusreg", self->dv_xname);
287 	}
288 #endif
289 
290 	/* Setup interrupt translation tables */
291 	sc->sc_intr2ipl = CPU_ISSUN4C
292 				? intr_sbus2ipl_4c
293 				: intr_sbus2ipl_4m;
294 
295 	/*
296 	 * Record clock frequency for synchronous SCSI.
297 	 * IS THIS THE CORRECT DEFAULT??
298 	 */
299 	sc->sc_clockfreq = PROM_getpropint(node, "clock-frequency", 25*1000*1000);
300 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
301 
302 	sbus_sc = sc;
303 	sbus_attach_common(sc, "sbus", node, NULL);
304 }
305 
306 
307 void
308 sbus_attach_iommu(parent, self, aux)
309 	struct device *parent;
310 	struct device *self;
311 	void *aux;
312 {
313 	struct sbus_softc *sc = (struct sbus_softc *)self;
314 	struct iommu_attach_args *ia = aux;
315 	int node = ia->iom_node;
316 
317 	sc->sc_bustag = ia->iom_bustag;
318 	sc->sc_dmatag = ia->iom_dmatag;
319 
320 	if (ia->iom_nreg == 0)
321 		panic("%s: no Sbus registers", self->dv_xname);
322 
323 	if (bus_space_map(ia->iom_bustag,
324 			  BUS_ADDR(ia->iom_reg[0].oa_space,
325 				   ia->iom_reg[0].oa_base),
326 			  (bus_size_t)ia->iom_reg[0].oa_size,
327 			  BUS_SPACE_MAP_LINEAR,
328 			  &sc->sc_bh) != 0) {
329 		panic("%s: can't map sbusbusreg", self->dv_xname);
330 	}
331 
332 	/* Setup interrupt translation tables */
333 	sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
334 
335 	/*
336 	 * Record clock frequency for synchronous SCSI.
337 	 * IS THIS THE CORRECT DEFAULT??
338 	 */
339 	sc->sc_clockfreq = PROM_getpropint(node, "clock-frequency", 25*1000*1000);
340 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
341 
342 	sbus_sc = sc;
343 	sbuserr_handler = sbus_error;
344 	sbus_attach_common(sc, "sbus", node, NULL);
345 }
346 
347 void
348 sbus_attach_xbox(parent, self, aux)
349 	struct device *parent;
350 	struct device *self;
351 	void *aux;
352 {
353 	struct sbus_softc *sc = (struct sbus_softc *)self;
354 	struct xbox_attach_args *xa = aux;
355 	int node = xa->xa_node;
356 
357 	sc->sc_bustag = xa->xa_bustag;
358 	sc->sc_dmatag = xa->xa_dmatag;
359 
360 	/* Setup interrupt translation tables */
361 	sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
362 
363 	/*
364 	 * Record clock frequency for synchronous SCSI.
365 	 * IS THIS THE CORRECT DEFAULT??
366 	 */
367 	sc->sc_clockfreq = PROM_getpropint(node, "clock-frequency", 25*1000*1000);
368 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
369 
370 	sbus_attach_common(sc, "sbus", node, NULL);
371 }
372 
373 void
374 sbus_attach_common(sc, busname, busnode, specials)
375 	struct sbus_softc *sc;
376 	char *busname;
377 	int busnode;
378 	const char * const *specials;
379 {
380 	int node0, node, error;
381 	const char *sp;
382 	const char *const *ssp;
383 	bus_space_tag_t sbt;
384 	struct sbus_attach_args sa;
385 
386 	sbt = sbus_alloc_bustag(sc);
387 
388 	/*
389 	 * Get the SBus burst transfer size if burst transfers are supported
390 	 */
391 	sc->sc_burst = PROM_getpropint(busnode, "burst-sizes", 0);
392 
393 
394 	if (CPU_ISSUN4M) {
395 		/*
396 		 * Some models (e.g. SS20) erroneously report 64-bit
397 		 * burst capability. We mask it out here for all SUN4Ms,
398 		 * since probably no member of that class supports
399 		 * 64-bit Sbus bursts.
400 		 */
401 		sc->sc_burst &= ~SBUS_BURST_64;
402 	}
403 
404 	/*
405 	 * Collect address translations from the OBP.
406 	 */
407 	error = PROM_getprop(busnode, "ranges", sizeof(struct rom_range),
408 			&sbt->nranges, (void **) &sbt->ranges);
409 	switch (error) {
410 	case 0:
411 		break;
412 	case ENOENT:
413 		/* Fall back to our own `range' construction */
414 		sbt->ranges = sbus_translations;
415 		sbt->nranges =
416 			sizeof(sbus_translations)/sizeof(sbus_translations[0]);
417 		break;
418 	default:
419 		panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
420 	}
421 
422 	/*
423 	 * Loop through ROM children, fixing any relative addresses
424 	 * and then configuring each device.
425 	 * `specials' is an array of device names that are treated
426 	 * specially:
427 	 */
428 	node0 = firstchild(busnode);
429 	for (ssp = specials ; ssp != NULL && *(sp = *ssp) != 0; ssp++) {
430 		if ((node = findnode(node0, sp)) == 0) {
431 			panic("could not find %s amongst %s devices",
432 				sp, busname);
433 		}
434 
435 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
436 					   node, &sa) != 0) {
437 			panic("sbus_attach: %s: incomplete", sp);
438 		}
439 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
440 		sbus_destroy_attach_args(&sa);
441 	}
442 
443 	for (node = node0; node; node = nextsibling(node)) {
444 		char *name = PROM_getpropstring(node, "name");
445 		for (ssp = specials, sp = NULL;
446 		     ssp != NULL && (sp = *ssp) != NULL;
447 		     ssp++)
448 			if (strcmp(name, sp) == 0)
449 				break;
450 
451 		if (sp != NULL)
452 			/* Already configured as an "early" device */
453 			continue;
454 
455 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
456 					   node, &sa) != 0) {
457 			printf("sbus_attach: %s: incomplete\n", name);
458 			continue;
459 		}
460 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
461 		sbus_destroy_attach_args(&sa);
462 	}
463 }
464 
465 int
466 sbus_setup_attach_args(sc, bustag, dmatag, node, sa)
467 	struct sbus_softc	*sc;
468 	bus_space_tag_t		bustag;
469 	bus_dma_tag_t		dmatag;
470 	int			node;
471 	struct sbus_attach_args	*sa;
472 {
473 	int n, error;
474 
475 	bzero(sa, sizeof(struct sbus_attach_args));
476 	error = PROM_getprop(node, "name", 1, &n, (void **)&sa->sa_name);
477 	if (error != 0)
478 		return (error);
479 	sa->sa_name[n] = '\0';
480 
481 	sa->sa_bustag = bustag;
482 	sa->sa_dmatag = dmatag;
483 	sa->sa_node = node;
484 	sa->sa_frequency = sc->sc_clockfreq;
485 
486 	error = PROM_getprop(node, "reg", sizeof(struct openprom_addr),
487 			&sa->sa_nreg, (void **)&sa->sa_reg);
488 	if (error != 0) {
489 		char buf[32];
490 		if (error != ENOENT ||
491 		    !node_has_property(node, "device_type") ||
492 		    strcmp(PROM_getpropstringA(node, "device_type", buf, sizeof buf),
493 			   "hierarchical") != 0)
494 			return (error);
495 	}
496 	for (n = 0; n < sa->sa_nreg; n++) {
497 		/* Convert to relative addressing, if necessary */
498 		u_int32_t base = sa->sa_reg[n].oa_base;
499 		if (SBUS_ABS(base)) {
500 			sa->sa_reg[n].oa_space = SBUS_ABS_TO_SLOT(base);
501 			sa->sa_reg[n].oa_base = SBUS_ABS_TO_OFFSET(base);
502 		}
503 	}
504 
505 	if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr)) != 0)
506 		return (error);
507 
508 	error = PROM_getprop(node, "address", sizeof(u_int32_t),
509 			 &sa->sa_npromvaddrs, (void **)&sa->sa_promvaddrs);
510 	if (error != 0 && error != ENOENT)
511 		return (error);
512 
513 	return (0);
514 }
515 
516 void
517 sbus_destroy_attach_args(sa)
518 	struct sbus_attach_args	*sa;
519 {
520 	if (sa->sa_name != NULL)
521 		free(sa->sa_name, M_DEVBUF);
522 
523 	if (sa->sa_nreg != 0)
524 		free(sa->sa_reg, M_DEVBUF);
525 
526 	if (sa->sa_intr)
527 		free(sa->sa_intr, M_DEVBUF);
528 
529 	if (sa->sa_promvaddrs)
530 		free(sa->sa_promvaddrs, M_DEVBUF);
531 
532 	bzero(sa, sizeof(struct sbus_attach_args));/*DEBUG*/
533 }
534 
535 bus_addr_t
536 sbus_bus_addr(t, btype, offset)
537 	bus_space_tag_t t;
538 	u_int btype;
539 	u_int offset;
540 {
541 
542 	/* XXX: sbus_bus_addr should be g/c'ed */
543 	return (BUS_ADDR(btype, offset));
544 }
545 
546 
547 /*
548  * Each attached device calls sbus_establish after it initializes
549  * its sbusdev portion.
550  */
551 void
552 sbus_establish(sd, dev)
553 	register struct sbusdev *sd;
554 	register struct device *dev;
555 {
556 	register struct sbus_softc *sc;
557 	register struct device *curdev;
558 
559 	/*
560 	 * We have to look for the sbus by name, since it is not necessarily
561 	 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
562 	 * We don't just use the device structure of the above-attached
563 	 * sbus, since we might (in the future) support multiple sbus's.
564 	 */
565 	for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
566 		if (!curdev || !curdev->dv_xname)
567 			panic("sbus_establish: can't find sbus parent for %s",
568 			      sd->sd_dev->dv_xname
569 					? sd->sd_dev->dv_xname
570 					: "<unknown>" );
571 
572 		if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
573 			break;
574 	}
575 	sc = (struct sbus_softc *) curdev;
576 
577 	sd->sd_dev = dev;
578 	sd->sd_bchain = sc->sc_sbdev;
579 	sc->sc_sbdev = sd;
580 }
581 
582 /*
583  * Reset the given sbus. (???)
584  */
585 void
586 sbusreset(sbus)
587 	int sbus;
588 {
589 	register struct sbusdev *sd;
590 	struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
591 	struct device *dev;
592 
593 	printf("reset %s:", sc->sc_dev.dv_xname);
594 	for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
595 		if (sd->sd_reset) {
596 			dev = sd->sd_dev;
597 			(*sd->sd_reset)(dev);
598 			printf(" %s", dev->dv_xname);
599 		}
600 	}
601 }
602 
603 
604 /*
605  * Get interrupt attributes for an Sbus device.
606  */
607 int
608 sbus_get_intr(sc, node, ipp, np)
609 	struct sbus_softc *sc;
610 	int node;
611 	struct openprom_intr **ipp;
612 	int *np;
613 {
614 	int error, n;
615 	u_int32_t *ipl = NULL;
616 
617 	/*
618 	 * The `interrupts' property contains the Sbus interrupt level.
619 	 */
620 	if (PROM_getprop(node, "interrupts", sizeof(int), np,
621 			 (void **)&ipl) == 0) {
622 		/* Change format to an `struct openprom_intr' array */
623 		struct openprom_intr *ip;
624 		ip = malloc(*np * sizeof(struct openprom_intr), M_DEVBUF,
625 		    M_NOWAIT);
626 		if (ip == NULL) {
627 			free(ipl, M_DEVBUF);
628 			return (ENOMEM);
629 		}
630 		for (n = 0; n < *np; n++) {
631 			ip[n].oi_pri = ipl[n];
632 			ip[n].oi_vec = 0;
633 		}
634 		free(ipl, M_DEVBUF);
635 		*ipp = ip;
636 		return (0);
637 	}
638 
639 	/*
640 	 * Fall back on `intr' property.
641 	 */
642 	*ipp = NULL;
643 	error = PROM_getprop(node, "intr", sizeof(struct openprom_intr),
644 			np, (void **)ipp);
645 	switch (error) {
646 	case 0:
647 		for (n = *np; n-- > 0;) {
648 			(*ipp)[n].oi_pri &= 0xf;
649 			(*ipp)[n].oi_pri |= SBUS_INTR_COMPAT;
650 		}
651 		break;
652 	case ENOENT:
653 		error = 0;
654 		break;
655 	}
656 
657 	return (error);
658 }
659 
660 
661 /*
662  * Install an interrupt handler for an Sbus device.
663  */
664 void *
665 sbus_intr_establish(t, pri, level, handler, arg, fastvec)
666 	bus_space_tag_t t;
667 	int pri;
668 	int level;
669 	int (*handler) __P((void *));
670 	void *arg;
671 	void (*fastvec) __P((void));
672 {
673 	struct sbus_softc *sc = t->cookie;
674 	struct intrhand *ih;
675 	int pil;
676 
677 	ih = (struct intrhand *)
678 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
679 	if (ih == NULL)
680 		return (NULL);
681 
682 	/*
683 	 * Translate Sbus interrupt priority to CPU interrupt level
684 	 */
685 	if ((pri & SBUS_INTR_COMPAT) != 0)
686 		pil = pri & ~SBUS_INTR_COMPAT;
687 	else
688 		pil = sc->sc_intr2ipl[pri];
689 
690 	ih->ih_fun = handler;
691 	ih->ih_arg = arg;
692 	intr_establish(pil, level, ih, fastvec);
693 	return (ih);
694 }
695 
696 static bus_space_tag_t
697 sbus_alloc_bustag(sc)
698 	struct sbus_softc *sc;
699 {
700 	bus_space_tag_t sbt;
701 
702 	sbt = (bus_space_tag_t)
703 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
704 	if (sbt == NULL)
705 		return (NULL);
706 
707 	bzero(sbt, sizeof *sbt);
708 	sbt->cookie = sc;
709 	sbt->parent = sc->sc_bustag;
710 	sbt->sparc_bus_map = sc->sc_bustag->sparc_bus_map;
711 	sbt->sparc_bus_mmap = sc->sc_bustag->sparc_bus_mmap;
712 	sbt->sparc_intr_establish = sbus_intr_establish;
713 	return (sbt);
714 }
715 
716 int
717 sbus_error()
718 {
719 	struct sbus_softc *sc = sbus_sc;
720 	bus_space_handle_t bh = sc->sc_bh;
721 	u_int32_t afsr, afva;
722 	char bits[64];
723 static	int straytime, nstray;
724 	int timesince;
725 
726 	afsr = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFSR_REG);
727 	afva = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFAR_REG);
728 	printf("sbus error:\n\tAFSR %s\n",
729 		bitmask_snprintf(afsr, SBUS_AFSR_BITS, bits, sizeof(bits)));
730 	printf("\taddress: 0x%x%x\n", afsr & SBUS_AFSR_PAH, afva);
731 
732 	/* For now, do the same dance as on stray interrupts */
733 	timesince = time.tv_sec - straytime;
734 	if (timesince <= 10) {
735 		if (++nstray > 9)
736 			panic("too many SBus errors");
737 	} else {
738 		straytime = time.tv_sec;
739 		nstray = 1;
740 	}
741 
742 	/* Unlock registers and clear interrupt */
743 	bus_space_write_4(sc->sc_bustag, bh, SBUS_AFSR_REG, afsr);
744 
745 	return (0);
746 }
747