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