xref: /netbsd-src/sys/arch/sparc/dev/sbus.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /*	$NetBSD: sbus.c,v 1.64 2005/06/04 04:40:57 tsutsui 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.64 2005/06/04 04:40:57 tsutsui 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 static int sbus_mainbus_attached;
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 || sbus_mainbus_attached)
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 	sbus_mainbus_attached = 1;
266 
267 	sc->sc_bustag = ma->ma_bustag;
268 	sc->sc_dmatag = ma->ma_dmatag;
269 
270 #if 0	/* sbus at mainbus (sun4c): `reg' prop is not control space */
271 	if (ma->ma_size == 0)
272 		printf("%s: no Sbus registers", self->dv_xname);
273 
274 	if (bus_space_map(ma->ma_bustag,
275 			  ma->ma_paddr,
276 			  ma->ma_size,
277 			  BUS_SPACE_MAP_LINEAR,
278 			  &sc->sc_bh) != 0) {
279 		panic("%s: can't map sbusbusreg", self->dv_xname);
280 	}
281 #endif
282 
283 	/* Setup interrupt translation tables */
284 	sc->sc_intr2ipl = CPU_ISSUN4C
285 				? intr_sbus2ipl_4c
286 				: intr_sbus2ipl_4m;
287 
288 	/*
289 	 * Record clock frequency for synchronous SCSI.
290 	 * IS THIS THE CORRECT DEFAULT??
291 	 */
292 	sc->sc_clockfreq = prom_getpropint(node, "clock-frequency", 25*1000*1000);
293 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
294 
295 	sbus_sc = sc;
296 	sbus_attach_common(sc, "sbus", node, NULL);
297 }
298 
299 
300 void
301 sbus_attach_iommu(parent, self, aux)
302 	struct device *parent;
303 	struct device *self;
304 	void *aux;
305 {
306 	struct sbus_softc *sc = (struct sbus_softc *)self;
307 	struct iommu_attach_args *ia = aux;
308 	int node = ia->iom_node;
309 
310 	sc->sc_bustag = ia->iom_bustag;
311 	sc->sc_dmatag = ia->iom_dmatag;
312 
313 	if (ia->iom_nreg == 0)
314 		panic("%s: no Sbus registers", self->dv_xname);
315 
316 	if (bus_space_map(ia->iom_bustag,
317 			  BUS_ADDR(ia->iom_reg[0].oa_space,
318 				   ia->iom_reg[0].oa_base),
319 			  (bus_size_t)ia->iom_reg[0].oa_size,
320 			  BUS_SPACE_MAP_LINEAR,
321 			  &sc->sc_bh) != 0) {
322 		panic("%s: can't map sbusbusreg", self->dv_xname);
323 	}
324 
325 	/* Setup interrupt translation tables */
326 	sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
327 
328 	/*
329 	 * Record clock frequency for synchronous SCSI.
330 	 * IS THIS THE CORRECT DEFAULT??
331 	 */
332 	sc->sc_clockfreq = prom_getpropint(node, "clock-frequency", 25*1000*1000);
333 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
334 
335 	sbus_sc = sc;
336 	sbuserr_handler = sbus_error;
337 	sbus_attach_common(sc, "sbus", node, NULL);
338 }
339 
340 void
341 sbus_attach_xbox(parent, self, aux)
342 	struct device *parent;
343 	struct device *self;
344 	void *aux;
345 {
346 	struct sbus_softc *sc = (struct sbus_softc *)self;
347 	struct xbox_attach_args *xa = aux;
348 	int node = xa->xa_node;
349 
350 	sc->sc_bustag = xa->xa_bustag;
351 	sc->sc_dmatag = xa->xa_dmatag;
352 
353 	/* Setup interrupt translation tables */
354 	sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
355 
356 	/*
357 	 * Record clock frequency for synchronous SCSI.
358 	 * IS THIS THE CORRECT DEFAULT??
359 	 */
360 	sc->sc_clockfreq = prom_getpropint(node, "clock-frequency", 25*1000*1000);
361 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
362 
363 	sbus_attach_common(sc, "sbus", node, NULL);
364 }
365 
366 void
367 sbus_attach_common(sc, busname, busnode, specials)
368 	struct sbus_softc *sc;
369 	const char *busname;
370 	int busnode;
371 	const char * const *specials;
372 {
373 	int node0, node, error;
374 	const char *sp;
375 	const char *const *ssp;
376 	bus_space_tag_t sbt;
377 	struct sbus_attach_args sa;
378 
379 	if ((sbt = bus_space_tag_alloc(sc->sc_bustag, sc)) == NULL) {
380 		printf("%s: attach: out of memory\n", sc->sc_dev.dv_xname);
381 		return;
382 	}
383 	sbt->sparc_intr_establish = sbus_intr_establish;
384 
385 	/*
386 	 * Get the SBus burst transfer size if burst transfers are supported
387 	 */
388 	sc->sc_burst = prom_getpropint(busnode, "burst-sizes", 0);
389 
390 
391 	if (CPU_ISSUN4M) {
392 		/*
393 		 * Some models (e.g. SS20) erroneously report 64-bit
394 		 * burst capability. We mask it out here for all SUN4Ms,
395 		 * since probably no member of that class supports
396 		 * 64-bit Sbus bursts.
397 		 */
398 		sc->sc_burst &= ~SBUS_BURST_64;
399 	}
400 
401 	/*
402 	 * Collect address translations from the OBP.
403 	 */
404 	error = prom_getprop(busnode, "ranges", sizeof(struct rom_range),
405 			&sbt->nranges, &sbt->ranges);
406 	switch (error) {
407 	case 0:
408 		break;
409 	case ENOENT:
410 		/* Fall back to our own `range' construction */
411 		sbt->ranges = sbus_translations;
412 		sbt->nranges =
413 			sizeof(sbus_translations)/sizeof(sbus_translations[0]);
414 		break;
415 	default:
416 		panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
417 	}
418 
419 	/*
420 	 * Loop through ROM children, fixing any relative addresses
421 	 * and then configuring each device.
422 	 * `specials' is an array of device names that are treated
423 	 * specially:
424 	 */
425 	node0 = firstchild(busnode);
426 	for (ssp = specials ; ssp != NULL && *(sp = *ssp) != 0; ssp++) {
427 		if ((node = findnode(node0, sp)) == 0) {
428 			panic("could not find %s amongst %s devices",
429 				sp, busname);
430 		}
431 
432 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
433 					   node, &sa) != 0) {
434 			panic("sbus_attach: %s: incomplete", sp);
435 		}
436 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
437 		sbus_destroy_attach_args(&sa);
438 	}
439 
440 	for (node = node0; node; node = nextsibling(node)) {
441 		char *name = prom_getpropstring(node, "name");
442 		for (ssp = specials, sp = NULL;
443 		     ssp != NULL && (sp = *ssp) != NULL;
444 		     ssp++)
445 			if (strcmp(name, sp) == 0)
446 				break;
447 
448 		if (sp != NULL)
449 			/* Already configured as an "early" device */
450 			continue;
451 
452 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
453 					   node, &sa) != 0) {
454 			printf("sbus_attach: %s: incomplete\n", name);
455 			continue;
456 		}
457 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
458 		sbus_destroy_attach_args(&sa);
459 	}
460 }
461 
462 int
463 sbus_setup_attach_args(sc, bustag, dmatag, node, sa)
464 	struct sbus_softc	*sc;
465 	bus_space_tag_t		bustag;
466 	bus_dma_tag_t		dmatag;
467 	int			node;
468 	struct sbus_attach_args	*sa;
469 {
470 	int n, error;
471 
472 	bzero(sa, sizeof(struct sbus_attach_args));
473 	error = prom_getprop(node, "name", 1, &n, &sa->sa_name);
474 	if (error != 0)
475 		return (error);
476 	sa->sa_name[n] = '\0';
477 
478 	sa->sa_bustag = bustag;
479 	sa->sa_dmatag = dmatag;
480 	sa->sa_node = node;
481 	sa->sa_frequency = sc->sc_clockfreq;
482 
483 	error = prom_getprop(node, "reg", sizeof(struct openprom_addr),
484 			&sa->sa_nreg, &sa->sa_reg);
485 	if (error != 0) {
486 		char buf[32];
487 		if (error != ENOENT ||
488 		    !node_has_property(node, "device_type") ||
489 		    strcmp(prom_getpropstringA(node, "device_type", buf, sizeof buf),
490 			   "hierarchical") != 0)
491 			return (error);
492 	}
493 	for (n = 0; n < sa->sa_nreg; n++) {
494 		/* Convert to relative addressing, if necessary */
495 		u_int32_t base = sa->sa_reg[n].oa_base;
496 		if (SBUS_ABS(base)) {
497 			sa->sa_reg[n].oa_space = SBUS_ABS_TO_SLOT(base);
498 			sa->sa_reg[n].oa_base = SBUS_ABS_TO_OFFSET(base);
499 		}
500 	}
501 
502 	if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr)) != 0)
503 		return (error);
504 
505 	error = prom_getprop(node, "address", sizeof(u_int32_t),
506 			 &sa->sa_npromvaddrs, &sa->sa_promvaddrs);
507 	if (error != 0 && error != ENOENT)
508 		return (error);
509 
510 	return (0);
511 }
512 
513 void
514 sbus_destroy_attach_args(sa)
515 	struct sbus_attach_args	*sa;
516 {
517 	if (sa->sa_name != NULL)
518 		free(sa->sa_name, M_DEVBUF);
519 
520 	if (sa->sa_nreg != 0)
521 		free(sa->sa_reg, M_DEVBUF);
522 
523 	if (sa->sa_intr)
524 		free(sa->sa_intr, M_DEVBUF);
525 
526 	if (sa->sa_promvaddrs)
527 		free(sa->sa_promvaddrs, M_DEVBUF);
528 
529 	bzero(sa, sizeof(struct sbus_attach_args));/*DEBUG*/
530 }
531 
532 bus_addr_t
533 sbus_bus_addr(t, btype, offset)
534 	bus_space_tag_t t;
535 	u_int btype;
536 	u_int offset;
537 {
538 
539 	/* XXX: sbus_bus_addr should be g/c'ed */
540 	return (BUS_ADDR(btype, offset));
541 }
542 
543 
544 /*
545  * Each attached device calls sbus_establish after it initializes
546  * its sbusdev portion.
547  */
548 void
549 sbus_establish(sd, dev)
550 	register struct sbusdev *sd;
551 	register struct device *dev;
552 {
553 	register struct sbus_softc *sc;
554 	register struct device *curdev;
555 
556 	/*
557 	 * We have to look for the sbus by name, since it is not necessarily
558 	 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
559 	 * We don't just use the device structure of the above-attached
560 	 * sbus, since we might (in the future) support multiple sbus's.
561 	 */
562 	for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
563 		if (!curdev || !curdev->dv_xname)
564 			panic("sbus_establish: can't find sbus parent for %s",
565 			      sd->sd_dev->dv_xname
566 					? sd->sd_dev->dv_xname
567 					: "<unknown>" );
568 
569 		if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
570 			break;
571 	}
572 	sc = (struct sbus_softc *) curdev;
573 
574 	sd->sd_dev = dev;
575 	sd->sd_bchain = sc->sc_sbdev;
576 	sc->sc_sbdev = sd;
577 }
578 
579 /*
580  * Reset the given sbus. (???)
581  */
582 void
583 sbusreset(sbus)
584 	int sbus;
585 {
586 	register struct sbusdev *sd;
587 	struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
588 	struct device *dev;
589 
590 	printf("reset %s:", sc->sc_dev.dv_xname);
591 	for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
592 		if (sd->sd_reset) {
593 			dev = sd->sd_dev;
594 			(*sd->sd_reset)(dev);
595 			printf(" %s", dev->dv_xname);
596 		}
597 	}
598 }
599 
600 
601 /*
602  * Get interrupt attributes for an Sbus device.
603  */
604 int
605 sbus_get_intr(sc, node, ipp, np)
606 	struct sbus_softc *sc;
607 	int node;
608 	struct openprom_intr **ipp;
609 	int *np;
610 {
611 	int error, n;
612 	u_int32_t *ipl = NULL;
613 
614 	/*
615 	 * The `interrupts' property contains the Sbus interrupt level.
616 	 */
617 	if (prom_getprop(node, "interrupts", sizeof(int), np,
618 			 &ipl) == 0) {
619 		/* Change format to an `struct openprom_intr' array */
620 		struct openprom_intr *ip;
621 		ip = malloc(*np * sizeof(struct openprom_intr), M_DEVBUF,
622 		    M_NOWAIT);
623 		if (ip == NULL) {
624 			free(ipl, M_DEVBUF);
625 			return (ENOMEM);
626 		}
627 		for (n = 0; n < *np; n++) {
628 			ip[n].oi_pri = ipl[n];
629 			ip[n].oi_vec = 0;
630 		}
631 		free(ipl, M_DEVBUF);
632 		*ipp = ip;
633 		return (0);
634 	}
635 
636 	/*
637 	 * Fall back on `intr' property.
638 	 */
639 	*ipp = NULL;
640 	error = prom_getprop(node, "intr", sizeof(struct openprom_intr),
641 			np, ipp);
642 	switch (error) {
643 	case 0:
644 		for (n = *np; n-- > 0;) {
645 			(*ipp)[n].oi_pri &= 0xf;
646 			(*ipp)[n].oi_pri |= SBUS_INTR_COMPAT;
647 		}
648 		break;
649 	case ENOENT:
650 		error = 0;
651 		break;
652 	}
653 
654 	return (error);
655 }
656 
657 
658 /*
659  * Install an interrupt handler for an Sbus device.
660  */
661 void *
662 sbus_intr_establish(t, pri, level, handler, arg, fastvec)
663 	bus_space_tag_t t;
664 	int pri;
665 	int level;
666 	int (*handler) __P((void *));
667 	void *arg;
668 	void (*fastvec) __P((void));
669 {
670 	struct sbus_softc *sc = t->cookie;
671 	struct intrhand *ih;
672 	int pil;
673 
674 	ih = (struct intrhand *)
675 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
676 	if (ih == NULL)
677 		return (NULL);
678 
679 	/*
680 	 * Translate Sbus interrupt priority to CPU interrupt level
681 	 */
682 	if ((pri & SBUS_INTR_COMPAT) != 0)
683 		pil = pri & ~SBUS_INTR_COMPAT;
684 	else
685 		pil = sc->sc_intr2ipl[pri];
686 
687 	ih->ih_fun = handler;
688 	ih->ih_arg = arg;
689 	intr_establish(pil, level, ih, fastvec);
690 	return (ih);
691 }
692 
693 int
694 sbus_error()
695 {
696 	struct sbus_softc *sc = sbus_sc;
697 	bus_space_handle_t bh = sc->sc_bh;
698 	u_int32_t afsr, afva;
699 	char bits[64];
700 static	int straytime, nstray;
701 	int timesince;
702 
703 	afsr = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFSR_REG);
704 	afva = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFAR_REG);
705 	printf("sbus error:\n\tAFSR %s\n",
706 		bitmask_snprintf(afsr, SBUS_AFSR_BITS, bits, sizeof(bits)));
707 	printf("\taddress: 0x%x%x\n", afsr & SBUS_AFSR_PAH, afva);
708 
709 	/* For now, do the same dance as on stray interrupts */
710 	timesince = time.tv_sec - straytime;
711 	if (timesince <= 10) {
712 		if (++nstray > 9)
713 			panic("too many SBus errors");
714 	} else {
715 		straytime = time.tv_sec;
716 		nstray = 1;
717 	}
718 
719 	/* Unlock registers and clear interrupt */
720 	bus_space_write_4(sc->sc_bustag, bh, SBUS_AFSR_REG, afsr);
721 
722 	return (0);
723 }
724