Home | History | Annotate | Line # | Download | only in dev
sbus.c revision 1.47
      1 /*	$NetBSD: sbus.c,v 1.47 2002/08/25 16:05:41 thorpej 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/autoconf.h>
     96 #include <machine/bus.h>
     97 #include <sparc/dev/sbusreg.h>
     98 #include <dev/sbus/sbusvar.h>
     99 #include <dev/sbus/xboxvar.h>
    100 
    101 #include <sparc/sparc/iommuvar.h>
    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 openprom_intr **, int *));
    108 static paddr_t sbus_bus_mmap __P((bus_space_tag_t, bus_addr_t, off_t,
    109 				  int, int));
    110 static int _sbus_bus_map __P((
    111 		bus_space_tag_t,
    112 		bus_addr_t,		/*coded slot+offset*/
    113 		bus_size_t,		/*size*/
    114 		int,			/*flags*/
    115 		vaddr_t,		/*preferred virtual address */
    116 		bus_space_handle_t *));
    117 static void *sbus_intr_establish __P((
    118 		bus_space_tag_t,
    119 		int,			/*Sbus interrupt level*/
    120 		int,			/*`device class' priority*/
    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 openprom_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].oi_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_map(ma->ma_bustag,
    292 			  ma->ma_paddr,
    293 			  ma->ma_size,
    294 			  BUS_SPACE_MAP_LINEAR,
    295 			  &sc->sc_bh) != 0) {
    296 		panic("%s: can't map sbusbusreg", self->dv_xname);
    297 	}
    298 #endif
    299 
    300 	/* Setup interrupt translation tables */
    301 	sc->sc_intr2ipl = CPU_ISSUN4C
    302 				? intr_sbus2ipl_4c
    303 				: intr_sbus2ipl_4m;
    304 
    305 	/*
    306 	 * Record clock frequency for synchronous SCSI.
    307 	 * IS THIS THE CORRECT DEFAULT??
    308 	 */
    309 	sc->sc_clockfreq = PROM_getpropint(node, "clock-frequency", 25*1000*1000);
    310 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    311 
    312 	sbus_sc = sc;
    313 	sbus_attach_common(sc, "sbus", node, NULL);
    314 }
    315 
    316 
    317 void
    318 sbus_attach_iommu(parent, self, aux)
    319 	struct device *parent;
    320 	struct device *self;
    321 	void *aux;
    322 {
    323 	struct sbus_softc *sc = (struct sbus_softc *)self;
    324 	struct iommu_attach_args *ia = aux;
    325 	int node = ia->iom_node;
    326 
    327 	sc->sc_bustag = ia->iom_bustag;
    328 	sc->sc_dmatag = ia->iom_dmatag;
    329 
    330 	if (ia->iom_nreg == 0)
    331 		panic("%s: no Sbus registers", self->dv_xname);
    332 
    333 	if (bus_space_map(ia->iom_bustag,
    334 			  BUS_ADDR(ia->iom_reg[0].oa_space,
    335 				   ia->iom_reg[0].oa_base),
    336 			  (bus_size_t)ia->iom_reg[0].oa_size,
    337 			  BUS_SPACE_MAP_LINEAR,
    338 			  &sc->sc_bh) != 0) {
    339 		panic("%s: can't map sbusbusreg", self->dv_xname);
    340 	}
    341 
    342 	/* Setup interrupt translation tables */
    343 	sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
    344 
    345 	/*
    346 	 * Record clock frequency for synchronous SCSI.
    347 	 * IS THIS THE CORRECT DEFAULT??
    348 	 */
    349 	sc->sc_clockfreq = PROM_getpropint(node, "clock-frequency", 25*1000*1000);
    350 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    351 
    352 	sbus_sc = sc;
    353 	sbuserr_handler = sbus_error;
    354 	sbus_attach_common(sc, "sbus", node, NULL);
    355 }
    356 
    357 void
    358 sbus_attach_xbox(parent, self, aux)
    359 	struct device *parent;
    360 	struct device *self;
    361 	void *aux;
    362 {
    363 	struct sbus_softc *sc = (struct sbus_softc *)self;
    364 	struct xbox_attach_args *xa = aux;
    365 	int node = xa->xa_node;
    366 
    367 	sc->sc_bustag = xa->xa_bustag;
    368 	sc->sc_dmatag = xa->xa_dmatag;
    369 
    370 	/* Setup interrupt translation tables */
    371 	sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
    372 
    373 	/*
    374 	 * Record clock frequency for synchronous SCSI.
    375 	 * IS THIS THE CORRECT DEFAULT??
    376 	 */
    377 	sc->sc_clockfreq = PROM_getpropint(node, "clock-frequency", 25*1000*1000);
    378 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    379 
    380 	sbus_attach_common(sc, "sbus", node, NULL);
    381 }
    382 
    383 void
    384 sbus_attach_common(sc, busname, busnode, specials)
    385 	struct sbus_softc *sc;
    386 	char *busname;
    387 	int busnode;
    388 	const char * const *specials;
    389 {
    390 	int node0, node, error;
    391 	const char *sp;
    392 	const char *const *ssp;
    393 	bus_space_tag_t sbt;
    394 	struct sbus_attach_args sa;
    395 
    396 	sbt = sbus_alloc_bustag(sc);
    397 
    398 	/*
    399 	 * Get the SBus burst transfer size if burst transfers are supported
    400 	 */
    401 	sc->sc_burst = PROM_getpropint(busnode, "burst-sizes", 0);
    402 
    403 
    404 	if (CPU_ISSUN4M) {
    405 		/*
    406 		 * Some models (e.g. SS20) erroneously report 64-bit
    407 		 * burst capability. We mask it out here for all SUN4Ms,
    408 		 * since probably no member of that class supports
    409 		 * 64-bit Sbus bursts.
    410 		 */
    411 		sc->sc_burst &= ~SBUS_BURST_64;
    412 	}
    413 
    414 	/*
    415 	 * Collect address translations from the OBP.
    416 	 */
    417 	error = PROM_getprop(busnode, "ranges", sizeof(struct rom_range),
    418 			&sc->sc_nrange, (void **)&sc->sc_range);
    419 	switch (error) {
    420 	case 0:
    421 		break;
    422 	case ENOENT:
    423 		/* Fall back to our own `range' construction */
    424 		sc->sc_range = sbus_translations;
    425 		sc->sc_nrange =
    426 			sizeof(sbus_translations)/sizeof(sbus_translations[0]);
    427 		break;
    428 	default:
    429 		panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
    430 	}
    431 
    432 	/*
    433 	 * Loop through ROM children, fixing any relative addresses
    434 	 * and then configuring each device.
    435 	 * `specials' is an array of device names that are treated
    436 	 * specially:
    437 	 */
    438 	node0 = firstchild(busnode);
    439 	for (ssp = specials ; ssp != NULL && *(sp = *ssp) != 0; ssp++) {
    440 		if ((node = findnode(node0, sp)) == 0) {
    441 			panic("could not find %s amongst %s devices",
    442 				sp, busname);
    443 		}
    444 
    445 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    446 					   node, &sa) != 0) {
    447 			panic("sbus_attach: %s: incomplete", sp);
    448 		}
    449 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
    450 		sbus_destroy_attach_args(&sa);
    451 	}
    452 
    453 	for (node = node0; node; node = nextsibling(node)) {
    454 		char *name = PROM_getpropstring(node, "name");
    455 		for (ssp = specials, sp = NULL;
    456 		     ssp != NULL && (sp = *ssp) != NULL;
    457 		     ssp++)
    458 			if (strcmp(name, sp) == 0)
    459 				break;
    460 
    461 		if (sp != NULL)
    462 			/* Already configured as an "early" device */
    463 			continue;
    464 
    465 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    466 					   node, &sa) != 0) {
    467 			printf("sbus_attach: %s: incomplete\n", name);
    468 			continue;
    469 		}
    470 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
    471 		sbus_destroy_attach_args(&sa);
    472 	}
    473 }
    474 
    475 int
    476 sbus_setup_attach_args(sc, bustag, dmatag, node, sa)
    477 	struct sbus_softc	*sc;
    478 	bus_space_tag_t		bustag;
    479 	bus_dma_tag_t		dmatag;
    480 	int			node;
    481 	struct sbus_attach_args	*sa;
    482 {
    483 	int n, error;
    484 
    485 	bzero(sa, sizeof(struct sbus_attach_args));
    486 	error = PROM_getprop(node, "name", 1, &n, (void **)&sa->sa_name);
    487 	if (error != 0)
    488 		return (error);
    489 	sa->sa_name[n] = '\0';
    490 
    491 	sa->sa_bustag = bustag;
    492 	sa->sa_dmatag = dmatag;
    493 	sa->sa_node = node;
    494 	sa->sa_frequency = sc->sc_clockfreq;
    495 
    496 	error = PROM_getprop(node, "reg", sizeof(struct openprom_addr),
    497 			&sa->sa_nreg, (void **)&sa->sa_reg);
    498 	if (error != 0) {
    499 		char buf[32];
    500 		if (error != ENOENT ||
    501 		    !node_has_property(node, "device_type") ||
    502 		    strcmp(PROM_getpropstringA(node, "device_type", buf, sizeof buf),
    503 			   "hierarchical") != 0)
    504 			return (error);
    505 	}
    506 	for (n = 0; n < sa->sa_nreg; n++) {
    507 		/* Convert to relative addressing, if necessary */
    508 		u_int32_t base = sa->sa_reg[n].oa_base;
    509 		if (SBUS_ABS(base)) {
    510 			sa->sa_reg[n].oa_space = SBUS_ABS_TO_SLOT(base);
    511 			sa->sa_reg[n].oa_base = SBUS_ABS_TO_OFFSET(base);
    512 		}
    513 	}
    514 
    515 	if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr)) != 0)
    516 		return (error);
    517 
    518 	error = PROM_getprop(node, "address", sizeof(u_int32_t),
    519 			 &sa->sa_npromvaddrs, (void **)&sa->sa_promvaddrs);
    520 	if (error != 0 && error != ENOENT)
    521 		return (error);
    522 
    523 	return (0);
    524 }
    525 
    526 void
    527 sbus_destroy_attach_args(sa)
    528 	struct sbus_attach_args	*sa;
    529 {
    530 	if (sa->sa_name != NULL)
    531 		free(sa->sa_name, M_DEVBUF);
    532 
    533 	if (sa->sa_nreg != 0)
    534 		free(sa->sa_reg, M_DEVBUF);
    535 
    536 	if (sa->sa_intr)
    537 		free(sa->sa_intr, M_DEVBUF);
    538 
    539 	if (sa->sa_promvaddrs)
    540 		free(sa->sa_promvaddrs, M_DEVBUF);
    541 
    542 	bzero(sa, sizeof(struct sbus_attach_args));/*DEBUG*/
    543 }
    544 
    545 
    546 int
    547 _sbus_bus_map(t, ba, size, flags, va, hp)
    548 	bus_space_tag_t t;
    549 	bus_addr_t ba;
    550 	bus_size_t size;
    551 	int	flags;
    552 	vaddr_t va;
    553 	bus_space_handle_t *hp;
    554 {
    555 	struct sbus_softc *sc = t->cookie;
    556 	bus_addr_t addr;
    557 	int error;
    558 
    559 	error = bus_translate_address_generic(sc->sc_range, sc->sc_nrange,
    560 	    ba, &addr);
    561 	if (error)
    562 		return (error);
    563 	return (bus_space_map2(sc->sc_bustag, addr, size, flags, va, hp));
    564 }
    565 
    566 static paddr_t
    567 sbus_bus_mmap(t, ba, off, prot, flags)
    568 	bus_space_tag_t t;
    569 	bus_addr_t ba;
    570 	off_t off;
    571 	int prot;
    572 	int flags;
    573 {
    574 	struct sbus_softc *sc = t->cookie;
    575 	bus_addr_t addr;
    576 	int error;
    577 
    578 	error = bus_translate_address_generic(sc->sc_range, sc->sc_nrange,
    579 	    ba, &addr);
    580 	if (error)
    581 		return (-1);
    582 	return (bus_space_mmap(sc->sc_bustag, addr, off, prot, flags));
    583 }
    584 
    585 bus_addr_t
    586 sbus_bus_addr(t, btype, offset)
    587 	bus_space_tag_t t;
    588 	u_int btype;
    589 	u_int offset;
    590 {
    591 
    592 	/* XXX: sbus_bus_addr should be g/c'ed */
    593 	return (BUS_ADDR(btype, offset));
    594 }
    595 
    596 
    597 /*
    598  * Each attached device calls sbus_establish after it initializes
    599  * its sbusdev portion.
    600  */
    601 void
    602 sbus_establish(sd, dev)
    603 	register struct sbusdev *sd;
    604 	register struct device *dev;
    605 {
    606 	register struct sbus_softc *sc;
    607 	register struct device *curdev;
    608 
    609 	/*
    610 	 * We have to look for the sbus by name, since it is not necessarily
    611 	 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
    612 	 * We don't just use the device structure of the above-attached
    613 	 * sbus, since we might (in the future) support multiple sbus's.
    614 	 */
    615 	for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
    616 		if (!curdev || !curdev->dv_xname)
    617 			panic("sbus_establish: can't find sbus parent for %s",
    618 			      sd->sd_dev->dv_xname
    619 					? sd->sd_dev->dv_xname
    620 					: "<unknown>" );
    621 
    622 		if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
    623 			break;
    624 	}
    625 	sc = (struct sbus_softc *) curdev;
    626 
    627 	sd->sd_dev = dev;
    628 	sd->sd_bchain = sc->sc_sbdev;
    629 	sc->sc_sbdev = sd;
    630 }
    631 
    632 /*
    633  * Reset the given sbus. (???)
    634  */
    635 void
    636 sbusreset(sbus)
    637 	int sbus;
    638 {
    639 	register struct sbusdev *sd;
    640 	struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
    641 	struct device *dev;
    642 
    643 	printf("reset %s:", sc->sc_dev.dv_xname);
    644 	for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
    645 		if (sd->sd_reset) {
    646 			dev = sd->sd_dev;
    647 			(*sd->sd_reset)(dev);
    648 			printf(" %s", dev->dv_xname);
    649 		}
    650 	}
    651 }
    652 
    653 
    654 /*
    655  * Get interrupt attributes for an Sbus device.
    656  */
    657 int
    658 sbus_get_intr(sc, node, ipp, np)
    659 	struct sbus_softc *sc;
    660 	int node;
    661 	struct openprom_intr **ipp;
    662 	int *np;
    663 {
    664 	int error, n;
    665 	u_int32_t *ipl = NULL;
    666 
    667 	/*
    668 	 * The `interrupts' property contains the Sbus interrupt level.
    669 	 */
    670 	if (PROM_getprop(node, "interrupts", sizeof(int), np,
    671 			 (void **)&ipl) == 0) {
    672 		/* Change format to an `struct openprom_intr' array */
    673 		struct openprom_intr *ip;
    674 		ip = malloc(*np * sizeof(struct openprom_intr), M_DEVBUF,
    675 		    M_NOWAIT);
    676 		if (ip == NULL) {
    677 			free(ipl, M_DEVBUF);
    678 			return (ENOMEM);
    679 		}
    680 		for (n = 0; n < *np; n++) {
    681 			ip[n].oi_pri = ipl[n];
    682 			ip[n].oi_vec = 0;
    683 		}
    684 		free(ipl, M_DEVBUF);
    685 		*ipp = ip;
    686 		return (0);
    687 	}
    688 
    689 	/*
    690 	 * Fall back on `intr' property.
    691 	 */
    692 	*ipp = NULL;
    693 	error = PROM_getprop(node, "intr", sizeof(struct openprom_intr),
    694 			np, (void **)ipp);
    695 	switch (error) {
    696 	case 0:
    697 		for (n = *np; n-- > 0;) {
    698 			(*ipp)[n].oi_pri &= 0xf;
    699 			(*ipp)[n].oi_pri |= SBUS_INTR_COMPAT;
    700 		}
    701 		break;
    702 	case ENOENT:
    703 		error = 0;
    704 		break;
    705 	}
    706 
    707 	return (error);
    708 }
    709 
    710 
    711 /*
    712  * Install an interrupt handler for an Sbus device.
    713  */
    714 void *
    715 sbus_intr_establish(t, pri, level, flags, handler, arg)
    716 	bus_space_tag_t t;
    717 	int pri;
    718 	int level;
    719 	int flags;
    720 	int (*handler) __P((void *));
    721 	void *arg;
    722 {
    723 	struct sbus_softc *sc = t->cookie;
    724 	struct intrhand *ih;
    725 	int pil;
    726 
    727 	ih = (struct intrhand *)
    728 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    729 	if (ih == NULL)
    730 		return (NULL);
    731 
    732 	/*
    733 	 * Translate Sbus interrupt priority to CPU interrupt level
    734 	 */
    735 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0)
    736 		pil = pri;
    737 	else if ((pri & SBUS_INTR_COMPAT) != 0)
    738 		pil = pri & ~SBUS_INTR_COMPAT;
    739 	else
    740 		pil = sc->sc_intr2ipl[pri];
    741 
    742 	ih->ih_fun = handler;
    743 	ih->ih_arg = arg;
    744 	if ((flags & BUS_INTR_ESTABLISH_FASTTRAP) != 0)
    745 		intr_fasttrap(pil, (void (*)__P((void)))handler);
    746 	else
    747 		intr_establish(pil, ih);
    748 	return (ih);
    749 }
    750 
    751 static bus_space_tag_t
    752 sbus_alloc_bustag(sc)
    753 	struct sbus_softc *sc;
    754 {
    755 	bus_space_tag_t sbt;
    756 
    757 	sbt = (bus_space_tag_t)
    758 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    759 	if (sbt == NULL)
    760 		return (NULL);
    761 
    762 	bzero(sbt, sizeof *sbt);
    763 	sbt->cookie = sc;
    764 	sbt->parent = sc->sc_bustag;
    765 	sbt->sparc_bus_map = _sbus_bus_map;
    766 	sbt->sparc_bus_mmap = sbus_bus_mmap;
    767 	sbt->sparc_intr_establish = sbus_intr_establish;
    768 	return (sbt);
    769 }
    770 
    771 int
    772 sbus_error()
    773 {
    774 	struct sbus_softc *sc = sbus_sc;
    775 	bus_space_handle_t bh = sc->sc_bh;
    776 	u_int32_t afsr, afva;
    777 	char bits[64];
    778 static	int straytime, nstray;
    779 	int timesince;
    780 
    781 	afsr = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFSR_REG);
    782 	afva = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFAR_REG);
    783 	printf("sbus error:\n\tAFSR %s\n",
    784 		bitmask_snprintf(afsr, SBUS_AFSR_BITS, bits, sizeof(bits)));
    785 	printf("\taddress: 0x%x%x\n", afsr & SBUS_AFSR_PAH, afva);
    786 
    787 	/* For now, do the same dance as on stray interrupts */
    788 	timesince = time.tv_sec - straytime;
    789 	if (timesince <= 10) {
    790 		if (++nstray > 9)
    791 			panic("too many SBus errors");
    792 	} else {
    793 		straytime = time.tv_sec;
    794 		nstray = 1;
    795 	}
    796 
    797 	/* Unlock registers and clear interrupt */
    798 	bus_space_write_4(sc->sc_bustag, bh, SBUS_AFSR_REG, afsr);
    799 
    800 	return (0);
    801 }
    802