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