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