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