Home | History | Annotate | Line # | Download | only in dev
sbus.c revision 1.79
      1  1.79       chs /*	$NetBSD: sbus.c,v 1.79 2019/11/10 21:16:32 chs Exp $ */
      2  1.20        pk 
      3  1.20        pk /*-
      4  1.20        pk  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.20        pk  * All rights reserved.
      6  1.20        pk  *
      7  1.20        pk  * This code is derived from software contributed to The NetBSD Foundation
      8  1.20        pk  * by Paul Kranenburg.
      9  1.20        pk  *
     10  1.20        pk  * Redistribution and use in source and binary forms, with or without
     11  1.20        pk  * modification, are permitted provided that the following conditions
     12  1.20        pk  * are met:
     13  1.20        pk  * 1. Redistributions of source code must retain the above copyright
     14  1.20        pk  *    notice, this list of conditions and the following disclaimer.
     15  1.20        pk  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.20        pk  *    notice, this list of conditions and the following disclaimer in the
     17  1.20        pk  *    documentation and/or other materials provided with the distribution.
     18  1.20        pk  *
     19  1.20        pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.20        pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.20        pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.20        pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.20        pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.20        pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.20        pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.20        pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.20        pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.20        pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.20        pk  * POSSIBILITY OF SUCH DAMAGE.
     30  1.20        pk  */
     31   1.4   deraadt 
     32   1.1   deraadt /*
     33   1.1   deraadt  * Copyright (c) 1992, 1993
     34   1.1   deraadt  *	The Regents of the University of California.  All rights reserved.
     35   1.1   deraadt  *
     36   1.1   deraadt  * This software was developed by the Computer Systems Engineering group
     37   1.1   deraadt  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     38   1.1   deraadt  * contributed to Berkeley.
     39   1.1   deraadt  *
     40   1.1   deraadt  * All advertising materials mentioning features or use of this software
     41   1.1   deraadt  * must display the following acknowledgement:
     42   1.1   deraadt  *	This product includes software developed by the University of
     43   1.1   deraadt  *	California, Lawrence Berkeley Laboratory.
     44   1.1   deraadt  *
     45   1.1   deraadt  * Redistribution and use in source and binary forms, with or without
     46   1.1   deraadt  * modification, are permitted provided that the following conditions
     47   1.1   deraadt  * are met:
     48   1.1   deraadt  * 1. Redistributions of source code must retain the above copyright
     49   1.1   deraadt  *    notice, this list of conditions and the following disclaimer.
     50   1.1   deraadt  * 2. Redistributions in binary form must reproduce the above copyright
     51   1.1   deraadt  *    notice, this list of conditions and the following disclaimer in the
     52   1.1   deraadt  *    documentation and/or other materials provided with the distribution.
     53  1.58       agc  * 3. Neither the name of the University nor the names of its contributors
     54   1.1   deraadt  *    may be used to endorse or promote products derived from this software
     55   1.1   deraadt  *    without specific prior written permission.
     56   1.1   deraadt  *
     57   1.1   deraadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58   1.1   deraadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59   1.1   deraadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60   1.1   deraadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61   1.1   deraadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62   1.1   deraadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63   1.1   deraadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64   1.1   deraadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65   1.1   deraadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66   1.1   deraadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67   1.1   deraadt  * SUCH DAMAGE.
     68   1.1   deraadt  *
     69   1.1   deraadt  *	@(#)sbus.c	8.1 (Berkeley) 6/11/93
     70   1.1   deraadt  */
     71   1.1   deraadt 
     72   1.1   deraadt /*
     73   1.1   deraadt  * Sbus stuff.
     74   1.1   deraadt  */
     75  1.57     lukem 
     76  1.57     lukem #include <sys/cdefs.h>
     77  1.79       chs __KERNEL_RCSID(0, "$NetBSD: sbus.c,v 1.79 2019/11/10 21:16:32 chs Exp $");
     78   1.1   deraadt 
     79   1.1   deraadt #include <sys/param.h>
     80  1.16        pk #include <sys/malloc.h>
     81  1.32        pk #include <sys/kernel.h>
     82   1.7  christos #include <sys/systm.h>
     83   1.1   deraadt #include <sys/device.h>
     84  1.37       mrg 
     85  1.37       mrg #include <uvm/uvm_extern.h>
     86   1.1   deraadt 
     87  1.47   thorpej #include <machine/autoconf.h>
     88  1.75    dyoung #include <sys/bus.h>
     89  1.19        pk #include <sparc/dev/sbusreg.h>
     90  1.25        pk #include <dev/sbus/sbusvar.h>
     91  1.25        pk #include <dev/sbus/xboxvar.h>
     92  1.19        pk 
     93  1.19        pk #include <sparc/sparc/iommuvar.h>
     94   1.1   deraadt 
     95  1.65       uwe void sbusreset(int);
     96   1.8   thorpej 
     97  1.65       uwe static int sbus_get_intr(struct sbus_softc *, int,
     98  1.65       uwe 			 struct openprom_intr **, int *);
     99  1.65       uwe static void *sbus_intr_establish(
    100  1.23        pk 		bus_space_tag_t,
    101  1.65       uwe 		int,			/* Sbus interrupt level */
    102  1.65       uwe 		int,			/* `device class' priority */
    103  1.65       uwe 		int (*)(void *),	/* handler */
    104  1.65       uwe 		void *,			/* handler arg */
    105  1.65       uwe 		void (*)(void));	/* fast handler */
    106  1.19        pk 
    107  1.19        pk 
    108   1.1   deraadt /* autoconfiguration driver */
    109  1.69  macallan int	sbus_match_mainbus(device_t, struct cfdata *, void *);
    110  1.69  macallan int	sbus_match_iommu(device_t, struct cfdata *, void *);
    111  1.69  macallan int	sbus_match_xbox(device_t, struct cfdata *, void *);
    112  1.69  macallan void	sbus_attach_mainbus(device_t, device_t, void *);
    113  1.69  macallan void	sbus_attach_iommu(device_t, device_t, void *);
    114  1.69  macallan void	sbus_attach_xbox(device_t, device_t, void *);
    115   1.8   thorpej 
    116  1.78       jdc #if (defined(SUN4M) && !defined(MSIIEP)) || defined(SUN4D)
    117  1.65       uwe static	int sbus_error(void);
    118  1.77      matt extern	int (*sbuserr_handler)(void);
    119  1.78       jdc #endif
    120  1.32        pk 
    121  1.69  macallan CFATTACH_DECL_NEW(sbus_mainbus, sizeof(struct sbus_softc),
    122  1.52   thorpej     sbus_match_mainbus, sbus_attach_mainbus, NULL, NULL);
    123  1.51   thorpej 
    124  1.69  macallan CFATTACH_DECL_NEW(sbus_iommu, sizeof(struct sbus_softc),
    125  1.52   thorpej     sbus_match_iommu, sbus_attach_iommu, NULL, NULL);
    126  1.51   thorpej 
    127  1.69  macallan CFATTACH_DECL_NEW(sbus_xbox, sizeof(struct sbus_softc),
    128  1.52   thorpej     sbus_match_xbox, sbus_attach_xbox, NULL, NULL);
    129   1.7  christos 
    130  1.18   thorpej extern struct cfdriver sbus_cd;
    131   1.1   deraadt 
    132  1.63       chs static int sbus_mainbus_attached;
    133  1.63       chs 
    134  1.32        pk /* The "primary" Sbus */
    135  1.32        pk struct sbus_softc *sbus_sc;
    136  1.32        pk 
    137  1.19        pk /* If the PROM does not provide the `ranges' property, we make up our own */
    138  1.46   thorpej struct openprom_range sbus_translations[] = {
    139  1.19        pk 	/* Assume a maximum of 4 Sbus slots, all mapped to on-board io space */
    140  1.19        pk 	{ 0, 0, PMAP_OBIO, SBUS_ADDR(0,0), 1 << 25 },
    141  1.19        pk 	{ 1, 0, PMAP_OBIO, SBUS_ADDR(1,0), 1 << 25 },
    142  1.19        pk 	{ 2, 0, PMAP_OBIO, SBUS_ADDR(2,0), 1 << 25 },
    143  1.19        pk 	{ 3, 0, PMAP_OBIO, SBUS_ADDR(3,0), 1 << 25 }
    144  1.19        pk };
    145  1.19        pk 
    146  1.19        pk /*
    147  1.19        pk  * Child devices receive the Sbus interrupt level in their attach
    148  1.19        pk  * arguments. We translate these to CPU IPLs using the following
    149  1.19        pk  * tables. Note: obio bus interrupt levels are identical to the
    150  1.19        pk  * processor IPL.
    151  1.19        pk  *
    152  1.19        pk  * The second set of tables is used when the Sbus interrupt level
    153  1.19        pk  * cannot be had from the PROM as an `interrupt' property. We then
    154  1.19        pk  * fall back on the `intr' property which contains the CPU IPL.
    155  1.19        pk  */
    156  1.19        pk 
    157  1.19        pk /* Translate Sbus interrupt level to processor IPL */
    158  1.19        pk static int intr_sbus2ipl_4c[] = {
    159  1.19        pk 	0, 1, 2, 3, 5, 7, 8, 9
    160  1.19        pk };
    161  1.19        pk static int intr_sbus2ipl_4m[] = {
    162  1.19        pk 	0, 2, 3, 5, 7, 9, 11, 13
    163  1.19        pk };
    164  1.19        pk 
    165  1.20        pk /*
    166  1.20        pk  * This value is or'ed into the attach args' interrupt level cookie
    167  1.20        pk  * if the interrupt level comes from an `intr' property, i.e. it is
    168  1.20        pk  * not an Sbus interrupt level.
    169  1.20        pk  */
    170  1.19        pk #define SBUS_INTR_COMPAT	0x80000000
    171  1.19        pk 
    172  1.19        pk 
    173   1.1   deraadt /*
    174   1.1   deraadt  * Print the location of some sbus-attached device (called just
    175   1.1   deraadt  * before attaching that device).  If `sbus' is not NULL, the
    176   1.1   deraadt  * device was found but not configured; print the sbus as well.
    177   1.1   deraadt  * Return UNCONF (config_find ignores this if the device was configured).
    178   1.1   deraadt  */
    179   1.1   deraadt int
    180  1.65       uwe sbus_print(void *args, const char *busname)
    181   1.1   deraadt {
    182  1.19        pk 	struct sbus_attach_args *sa = args;
    183  1.26        pk 	int i;
    184   1.1   deraadt 
    185  1.19        pk 	if (busname)
    186  1.56   thorpej 		aprint_normal("%s at %s", sa->sa_name, busname);
    187  1.56   thorpej 	aprint_normal(" slot %d offset 0x%x", sa->sa_slot, sa->sa_offset);
    188  1.26        pk 	for (i = 0; i < sa->sa_nintr; i++) {
    189  1.65       uwe 		uint32_t level = sa->sa_intr[i].oi_pri;
    190  1.19        pk 		struct sbus_softc *sc =
    191  1.19        pk 			(struct sbus_softc *) sa->sa_bustag->cookie;
    192  1.19        pk 
    193  1.56   thorpej 		aprint_normal(" level %d", level & ~SBUS_INTR_COMPAT);
    194  1.19        pk 		if ((level & SBUS_INTR_COMPAT) == 0) {
    195  1.19        pk 			int ipl = sc->sc_intr2ipl[level];
    196  1.19        pk 			if (ipl != level)
    197  1.56   thorpej 				aprint_normal(" (ipl %d)", ipl);
    198  1.19        pk 		}
    199  1.19        pk 	}
    200   1.1   deraadt 	return (UNCONF);
    201   1.1   deraadt }
    202   1.1   deraadt 
    203   1.3   deraadt int
    204  1.69  macallan sbus_match_mainbus(device_t parent, struct cfdata *cf, void *aux)
    205  1.19        pk {
    206  1.19        pk 	struct mainbus_attach_args *ma = aux;
    207  1.19        pk 
    208  1.63       chs 	if (CPU_ISSUN4 || sbus_mainbus_attached)
    209  1.19        pk 		return (0);
    210  1.19        pk 
    211  1.49   thorpej 	return (strcmp(cf->cf_name, ma->ma_name) == 0);
    212  1.19        pk }
    213  1.19        pk 
    214  1.19        pk int
    215  1.69  macallan sbus_match_iommu(device_t parent, struct cfdata *cf, void *aux)
    216   1.3   deraadt {
    217  1.19        pk 	struct iommu_attach_args *ia = aux;
    218   1.3   deraadt 
    219   1.9        pk 	if (CPU_ISSUN4)
    220   1.3   deraadt 		return (0);
    221   1.9        pk 
    222  1.49   thorpej 	return (strcmp(cf->cf_name, ia->iom_name) == 0);
    223   1.3   deraadt }
    224   1.3   deraadt 
    225  1.24        pk int
    226  1.69  macallan sbus_match_xbox(device_t parent, struct cfdata *cf, void *aux)
    227  1.24        pk {
    228  1.24        pk 	struct xbox_attach_args *xa = aux;
    229  1.24        pk 
    230  1.24        pk 	if (CPU_ISSUN4)
    231  1.24        pk 		return (0);
    232  1.24        pk 
    233  1.49   thorpej 	return (strcmp(cf->cf_name, xa->xa_name) == 0);
    234  1.24        pk }
    235  1.24        pk 
    236   1.1   deraadt /*
    237   1.1   deraadt  * Attach an Sbus.
    238   1.1   deraadt  */
    239   1.1   deraadt void
    240  1.69  macallan sbus_attach_mainbus(device_t parent, device_t self, void *aux)
    241   1.1   deraadt {
    242  1.69  macallan 	struct sbus_softc *sc = device_private(self);
    243  1.19        pk 	struct mainbus_attach_args *ma = aux;
    244  1.19        pk 	int node = ma->ma_node;
    245   1.1   deraadt 
    246  1.63       chs 	sbus_mainbus_attached = 1;
    247   1.1   deraadt 
    248  1.69  macallan 	sc->sc_dev = self;
    249  1.19        pk 	sc->sc_bustag = ma->ma_bustag;
    250  1.19        pk 	sc->sc_dmatag = ma->ma_dmatag;
    251  1.19        pk 
    252  1.33        pk #if 0	/* sbus at mainbus (sun4c): `reg' prop is not control space */
    253  1.32        pk 	if (ma->ma_size == 0)
    254  1.69  macallan 		printf("%s: no Sbus registers", device_xname(self));
    255  1.32        pk 
    256  1.45        pk 	if (bus_space_map(ma->ma_bustag,
    257  1.45        pk 			  ma->ma_paddr,
    258  1.45        pk 			  ma->ma_size,
    259  1.32        pk 			  BUS_SPACE_MAP_LINEAR,
    260  1.45        pk 			  &sc->sc_bh) != 0) {
    261  1.69  macallan 		panic("%s: can't map sbusbusreg", device_xname(self));
    262  1.32        pk 	}
    263  1.33        pk #endif
    264  1.32        pk 
    265  1.19        pk 	/* Setup interrupt translation tables */
    266  1.19        pk 	sc->sc_intr2ipl = CPU_ISSUN4C
    267  1.19        pk 				? intr_sbus2ipl_4c
    268  1.19        pk 				: intr_sbus2ipl_4m;
    269  1.19        pk 
    270  1.19        pk 	/*
    271  1.19        pk 	 * Record clock frequency for synchronous SCSI.
    272  1.19        pk 	 * IS THIS THE CORRECT DEFAULT??
    273  1.19        pk 	 */
    274  1.60        pk 	sc->sc_clockfreq = prom_getpropint(node, "clock-frequency", 25*1000*1000);
    275  1.19        pk 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    276  1.19        pk 
    277  1.32        pk 	sbus_sc = sc;
    278  1.36        pk 	sbus_attach_common(sc, "sbus", node, NULL);
    279  1.19        pk }
    280  1.19        pk 
    281  1.32        pk 
    282  1.19        pk void
    283  1.69  macallan sbus_attach_iommu(device_t parent, device_t self, void *aux)
    284  1.19        pk {
    285  1.69  macallan 	struct sbus_softc *sc = device_private(self);
    286  1.19        pk 	struct iommu_attach_args *ia = aux;
    287  1.19        pk 	int node = ia->iom_node;
    288  1.19        pk 
    289  1.69  macallan 	sc->sc_dev = self;
    290  1.19        pk 	sc->sc_bustag = ia->iom_bustag;
    291  1.19        pk 	sc->sc_dmatag = ia->iom_dmatag;
    292  1.19        pk 
    293  1.32        pk 	if (ia->iom_nreg == 0)
    294  1.69  macallan 		panic("%s: no Sbus registers", device_xname(self));
    295  1.32        pk 
    296  1.45        pk 	if (bus_space_map(ia->iom_bustag,
    297  1.46   thorpej 			  BUS_ADDR(ia->iom_reg[0].oa_space,
    298  1.46   thorpej 				   ia->iom_reg[0].oa_base),
    299  1.46   thorpej 			  (bus_size_t)ia->iom_reg[0].oa_size,
    300  1.32        pk 			  BUS_SPACE_MAP_LINEAR,
    301  1.45        pk 			  &sc->sc_bh) != 0) {
    302  1.69  macallan 		panic("%s: can't map sbusbusreg", device_xname(self));
    303  1.32        pk 	}
    304  1.32        pk 
    305  1.19        pk 	/* Setup interrupt translation tables */
    306  1.19        pk 	sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
    307  1.19        pk 
    308   1.1   deraadt 	/*
    309   1.1   deraadt 	 * Record clock frequency for synchronous SCSI.
    310   1.1   deraadt 	 * IS THIS THE CORRECT DEFAULT??
    311   1.1   deraadt 	 */
    312  1.60        pk 	sc->sc_clockfreq = prom_getpropint(node, "clock-frequency", 25*1000*1000);
    313  1.13  christos 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    314  1.10    abrown 
    315  1.32        pk 	sbus_sc = sc;
    316  1.78       jdc #if (defined(SUN4M) && !defined(MSIIEP)) || defined(SUN4D)
    317  1.32        pk 	sbuserr_handler = sbus_error;
    318  1.78       jdc #endif
    319  1.36        pk 	sbus_attach_common(sc, "sbus", node, NULL);
    320  1.24        pk }
    321  1.24        pk 
    322  1.24        pk void
    323  1.69  macallan sbus_attach_xbox(device_t parent, device_t self, void *aux)
    324  1.24        pk {
    325  1.69  macallan 	struct sbus_softc *sc = device_private(self);
    326  1.24        pk 	struct xbox_attach_args *xa = aux;
    327  1.24        pk 	int node = xa->xa_node;
    328  1.24        pk 
    329  1.24        pk 	sc->sc_bustag = xa->xa_bustag;
    330  1.24        pk 	sc->sc_dmatag = xa->xa_dmatag;
    331  1.24        pk 
    332  1.24        pk 	/* Setup interrupt translation tables */
    333  1.24        pk 	sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
    334  1.24        pk 
    335  1.24        pk 	/*
    336  1.24        pk 	 * Record clock frequency for synchronous SCSI.
    337  1.24        pk 	 * IS THIS THE CORRECT DEFAULT??
    338  1.24        pk 	 */
    339  1.60        pk 	sc->sc_clockfreq = prom_getpropint(node, "clock-frequency", 25*1000*1000);
    340  1.24        pk 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    341  1.24        pk 
    342  1.36        pk 	sbus_attach_common(sc, "sbus", node, NULL);
    343  1.19        pk }
    344  1.19        pk 
    345  1.19        pk void
    346  1.65       uwe sbus_attach_common(struct sbus_softc *sc, const char *busname, int busnode,
    347  1.65       uwe 		   const char * const *specials)
    348  1.19        pk {
    349  1.19        pk 	int node0, node, error;
    350  1.19        pk 	const char *sp;
    351  1.19        pk 	const char *const *ssp;
    352  1.19        pk 	bus_space_tag_t sbt;
    353  1.19        pk 	struct sbus_attach_args sa;
    354  1.19        pk 
    355  1.62        pk 	if ((sbt = bus_space_tag_alloc(sc->sc_bustag, sc)) == NULL) {
    356  1.69  macallan 		printf("%s: attach: out of memory\n",
    357  1.69  macallan 		    device_xname(sc->sc_dev));
    358  1.62        pk 		return;
    359  1.62        pk 	}
    360  1.62        pk 	sbt->sparc_intr_establish = sbus_intr_establish;
    361  1.19        pk 
    362  1.10    abrown 	/*
    363  1.10    abrown 	 * Get the SBus burst transfer size if burst transfers are supported
    364  1.10    abrown 	 */
    365  1.60        pk 	sc->sc_burst = prom_getpropint(busnode, "burst-sizes", 0);
    366  1.35        pk 
    367  1.35        pk 
    368  1.35        pk 	if (CPU_ISSUN4M) {
    369  1.35        pk 		/*
    370  1.35        pk 		 * Some models (e.g. SS20) erroneously report 64-bit
    371  1.35        pk 		 * burst capability. We mask it out here for all SUN4Ms,
    372  1.35        pk 		 * since probably no member of that class supports
    373  1.35        pk 		 * 64-bit Sbus bursts.
    374  1.35        pk 		 */
    375  1.35        pk 		sc->sc_burst &= ~SBUS_BURST_64;
    376  1.35        pk 	}
    377   1.1   deraadt 
    378  1.19        pk 	/*
    379  1.19        pk 	 * Collect address translations from the OBP.
    380  1.19        pk 	 */
    381  1.60        pk 	error = prom_getprop(busnode, "ranges", sizeof(struct rom_range),
    382  1.59       mrg 			&sbt->nranges, &sbt->ranges);
    383  1.19        pk 	switch (error) {
    384  1.19        pk 	case 0:
    385  1.19        pk 		break;
    386  1.19        pk 	case ENOENT:
    387  1.19        pk 		/* Fall back to our own `range' construction */
    388  1.48   thorpej 		sbt->ranges = sbus_translations;
    389  1.48   thorpej 		sbt->nranges =
    390  1.19        pk 			sizeof(sbus_translations)/sizeof(sbus_translations[0]);
    391  1.19        pk 		break;
    392  1.19        pk 	default:
    393  1.69  macallan 		panic("%s: error getting ranges property",
    394  1.69  macallan 		    device_xname(sc->sc_dev));
    395  1.16        pk 	}
    396   1.9        pk 
    397   1.1   deraadt 	/*
    398   1.1   deraadt 	 * Loop through ROM children, fixing any relative addresses
    399   1.1   deraadt 	 * and then configuring each device.
    400  1.19        pk 	 * `specials' is an array of device names that are treated
    401  1.19        pk 	 * specially:
    402   1.1   deraadt 	 */
    403  1.19        pk 	node0 = firstchild(busnode);
    404  1.19        pk 	for (ssp = specials ; ssp != NULL && *(sp = *ssp) != 0; ssp++) {
    405  1.19        pk 		if ((node = findnode(node0, sp)) == 0) {
    406  1.19        pk 			panic("could not find %s amongst %s devices",
    407  1.19        pk 				sp, busname);
    408  1.19        pk 		}
    409  1.19        pk 
    410  1.19        pk 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    411  1.36        pk 					   node, &sa) != 0) {
    412  1.19        pk 			panic("sbus_attach: %s: incomplete", sp);
    413  1.19        pk 		}
    414  1.69  macallan 		(void) config_found(sc->sc_dev, (void *)&sa, sbus_print);
    415  1.26        pk 		sbus_destroy_attach_args(&sa);
    416  1.19        pk 	}
    417  1.19        pk 
    418  1.19        pk 	for (node = node0; node; node = nextsibling(node)) {
    419  1.60        pk 		char *name = prom_getpropstring(node, "name");
    420  1.19        pk 		for (ssp = specials, sp = NULL;
    421  1.19        pk 		     ssp != NULL && (sp = *ssp) != NULL;
    422  1.19        pk 		     ssp++)
    423  1.19        pk 			if (strcmp(name, sp) == 0)
    424  1.19        pk 				break;
    425  1.19        pk 
    426  1.19        pk 		if (sp != NULL)
    427  1.19        pk 			/* Already configured as an "early" device */
    428  1.19        pk 			continue;
    429  1.19        pk 
    430  1.19        pk 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    431  1.36        pk 					   node, &sa) != 0) {
    432  1.19        pk 			printf("sbus_attach: %s: incomplete\n", name);
    433   1.1   deraadt 			continue;
    434  1.19        pk 		}
    435  1.69  macallan 		(void) config_found(sc->sc_dev, (void *)&sa, sbus_print);
    436  1.26        pk 		sbus_destroy_attach_args(&sa);
    437  1.19        pk 	}
    438  1.19        pk }
    439   1.9        pk 
    440  1.19        pk int
    441  1.65       uwe sbus_setup_attach_args(struct sbus_softc *sc,
    442  1.65       uwe 		       bus_space_tag_t bustag, bus_dma_tag_t dmatag, int node,
    443  1.65       uwe 		       struct sbus_attach_args *sa)
    444  1.19        pk {
    445  1.32        pk 	int n, error;
    446  1.19        pk 
    447  1.72    cegger 	memset(sa, 0, sizeof(struct sbus_attach_args));
    448  1.60        pk 	error = prom_getprop(node, "name", 1, &n, &sa->sa_name);
    449  1.26        pk 	if (error != 0)
    450  1.26        pk 		return (error);
    451  1.76       mrg 	KASSERT(sa->sa_name[n-1] == '\0');
    452  1.26        pk 
    453  1.19        pk 	sa->sa_bustag = bustag;
    454  1.19        pk 	sa->sa_dmatag = dmatag;
    455  1.19        pk 	sa->sa_node = node;
    456  1.39       eeh 	sa->sa_frequency = sc->sc_clockfreq;
    457  1.19        pk 
    458  1.60        pk 	error = prom_getprop(node, "reg", sizeof(struct openprom_addr),
    459  1.59       mrg 			&sa->sa_nreg, &sa->sa_reg);
    460  1.26        pk 	if (error != 0) {
    461  1.26        pk 		char buf[32];
    462  1.26        pk 		if (error != ENOENT ||
    463  1.26        pk 		    !node_has_property(node, "device_type") ||
    464  1.60        pk 		    strcmp(prom_getpropstringA(node, "device_type", buf, sizeof buf),
    465  1.26        pk 			   "hierarchical") != 0)
    466  1.26        pk 			return (error);
    467  1.26        pk 	}
    468  1.26        pk 	for (n = 0; n < sa->sa_nreg; n++) {
    469  1.26        pk 		/* Convert to relative addressing, if necessary */
    470  1.65       uwe 		uint32_t base = sa->sa_reg[n].oa_base;
    471  1.26        pk 		if (SBUS_ABS(base)) {
    472  1.46   thorpej 			sa->sa_reg[n].oa_space = SBUS_ABS_TO_SLOT(base);
    473  1.46   thorpej 			sa->sa_reg[n].oa_base = SBUS_ABS_TO_OFFSET(base);
    474  1.26        pk 		}
    475   1.9        pk 	}
    476  1.19        pk 
    477  1.26        pk 	if ((error = sbus_get_intr(sc, node, &sa->sa_intr, &sa->sa_nintr)) != 0)
    478  1.19        pk 		return (error);
    479  1.19        pk 
    480  1.65       uwe 	error = prom_getprop(node, "address", sizeof(uint32_t),
    481  1.59       mrg 			 &sa->sa_npromvaddrs, &sa->sa_promvaddrs);
    482  1.26        pk 	if (error != 0 && error != ENOENT)
    483  1.19        pk 		return (error);
    484  1.19        pk 
    485  1.19        pk 	return (0);
    486   1.9        pk }
    487   1.9        pk 
    488  1.26        pk void
    489  1.65       uwe sbus_destroy_attach_args(struct sbus_attach_args *sa)
    490  1.26        pk {
    491  1.65       uwe 
    492  1.26        pk 	if (sa->sa_name != NULL)
    493  1.26        pk 		free(sa->sa_name, M_DEVBUF);
    494  1.26        pk 
    495  1.26        pk 	if (sa->sa_nreg != 0)
    496  1.26        pk 		free(sa->sa_reg, M_DEVBUF);
    497  1.26        pk 
    498  1.26        pk 	if (sa->sa_intr)
    499  1.26        pk 		free(sa->sa_intr, M_DEVBUF);
    500  1.26        pk 
    501  1.26        pk 	if (sa->sa_promvaddrs)
    502  1.26        pk 		free(sa->sa_promvaddrs, M_DEVBUF);
    503  1.26        pk 
    504  1.72    cegger 	memset(sa, 0, sizeof(struct sbus_attach_args));/*DEBUG*/
    505  1.26        pk }
    506  1.26        pk 
    507  1.40       eeh bus_addr_t
    508  1.65       uwe sbus_bus_addr(bus_space_tag_t t, u_int btype, u_int offset)
    509  1.40       eeh {
    510  1.40       eeh 
    511  1.44       uwe 	/* XXX: sbus_bus_addr should be g/c'ed */
    512  1.44       uwe 	return (BUS_ADDR(btype, offset));
    513   1.1   deraadt }
    514   1.1   deraadt 
    515  1.19        pk 
    516   1.1   deraadt /*
    517  1.19        pk  * Get interrupt attributes for an Sbus device.
    518  1.19        pk  */
    519  1.65       uwe static int
    520  1.65       uwe sbus_get_intr(struct sbus_softc *sc, int node,
    521  1.65       uwe 	      struct openprom_intr **ipp, int *np)
    522  1.19        pk {
    523  1.26        pk 	int error, n;
    524  1.65       uwe 	uint32_t *ipl = NULL;
    525  1.19        pk 
    526  1.19        pk 	/*
    527  1.19        pk 	 * The `interrupts' property contains the Sbus interrupt level.
    528  1.19        pk 	 */
    529  1.60        pk 	if (prom_getprop(node, "interrupts", sizeof(int), np,
    530  1.59       mrg 			 &ipl) == 0) {
    531  1.46   thorpej 		/* Change format to an `struct openprom_intr' array */
    532  1.46   thorpej 		struct openprom_intr *ip;
    533  1.46   thorpej 		ip = malloc(*np * sizeof(struct openprom_intr), M_DEVBUF,
    534  1.79       chs 		    M_WAITOK);
    535  1.26        pk 		for (n = 0; n < *np; n++) {
    536  1.46   thorpej 			ip[n].oi_pri = ipl[n];
    537  1.46   thorpej 			ip[n].oi_vec = 0;
    538  1.26        pk 		}
    539  1.19        pk 		free(ipl, M_DEVBUF);
    540  1.26        pk 		*ipp = ip;
    541  1.19        pk 		return (0);
    542  1.19        pk 	}
    543  1.19        pk 
    544  1.19        pk 	/*
    545  1.19        pk 	 * Fall back on `intr' property.
    546  1.19        pk 	 */
    547  1.26        pk 	*ipp = NULL;
    548  1.60        pk 	error = prom_getprop(node, "intr", sizeof(struct openprom_intr),
    549  1.59       mrg 			np, ipp);
    550  1.26        pk 	switch (error) {
    551  1.19        pk 	case 0:
    552  1.26        pk 		for (n = *np; n-- > 0;) {
    553  1.46   thorpej 			(*ipp)[n].oi_pri &= 0xf;
    554  1.46   thorpej 			(*ipp)[n].oi_pri |= SBUS_INTR_COMPAT;
    555  1.26        pk 		}
    556  1.26        pk 		break;
    557  1.19        pk 	case ENOENT:
    558  1.26        pk 		error = 0;
    559  1.26        pk 		break;
    560  1.19        pk 	}
    561  1.19        pk 
    562  1.26        pk 	return (error);
    563  1.19        pk }
    564  1.19        pk 
    565  1.19        pk 
    566  1.19        pk /*
    567  1.19        pk  * Install an interrupt handler for an Sbus device.
    568  1.19        pk  */
    569  1.65       uwe static void *
    570  1.65       uwe sbus_intr_establish(bus_space_tag_t t, int pri, int level,
    571  1.65       uwe 		    int (*handler)(void *), void *arg,
    572  1.65       uwe 		    void (*fastvec)(void))
    573  1.19        pk {
    574  1.22        pk 	struct sbus_softc *sc = t->cookie;
    575  1.19        pk 	struct intrhand *ih;
    576  1.38        pk 	int pil;
    577  1.19        pk 
    578  1.79       chs 	ih = malloc(sizeof(struct intrhand), M_DEVBUF, M_WAITOK);
    579  1.19        pk 
    580  1.38        pk 	/*
    581  1.38        pk 	 * Translate Sbus interrupt priority to CPU interrupt level
    582  1.38        pk 	 */
    583  1.54        pk 	if ((pri & SBUS_INTR_COMPAT) != 0)
    584  1.38        pk 		pil = pri & ~SBUS_INTR_COMPAT;
    585  1.19        pk 	else
    586  1.38        pk 		pil = sc->sc_intr2ipl[pri];
    587  1.19        pk 
    588  1.19        pk 	ih->ih_fun = handler;
    589  1.19        pk 	ih->ih_arg = arg;
    590  1.74       mrg 	intr_establish(pil, level, ih, fastvec, false);
    591  1.19        pk 	return (ih);
    592  1.19        pk }
    593  1.19        pk 
    594  1.78       jdc #if (defined(SUN4M) && !defined(MSIIEP)) || defined(SUN4D)
    595  1.65       uwe static int
    596  1.65       uwe sbus_error(void)
    597  1.32        pk {
    598  1.32        pk 	struct sbus_softc *sc = sbus_sc;
    599  1.32        pk 	bus_space_handle_t bh = sc->sc_bh;
    600  1.65       uwe 	uint32_t afsr, afva;
    601  1.32        pk 	char bits[64];
    602  1.32        pk static	int straytime, nstray;
    603  1.32        pk 	int timesince;
    604  1.32        pk 
    605  1.32        pk 	afsr = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFSR_REG);
    606  1.32        pk 	afva = bus_space_read_4(sc->sc_bustag, bh, SBUS_AFAR_REG);
    607  1.71  christos 	snprintb(bits, sizeof(bits), SBUS_AFSR_BITS, afsr);
    608  1.71  christos 	printf("sbus error:\n\tAFSR %s\n", bits);
    609  1.32        pk 	printf("\taddress: 0x%x%x\n", afsr & SBUS_AFSR_PAH, afva);
    610  1.32        pk 
    611  1.32        pk 	/* For now, do the same dance as on stray interrupts */
    612  1.67    kardel 	timesince = time_uptime - straytime;
    613  1.32        pk 	if (timesince <= 10) {
    614  1.32        pk 		if (++nstray > 9)
    615  1.32        pk 			panic("too many SBus errors");
    616  1.32        pk 	} else {
    617  1.67    kardel 		straytime = time_uptime;
    618  1.32        pk 		nstray = 1;
    619  1.32        pk 	}
    620  1.32        pk 
    621  1.32        pk 	/* Unlock registers and clear interrupt */
    622  1.32        pk 	bus_space_write_4(sc->sc_bustag, bh, SBUS_AFSR_REG, afsr);
    623  1.32        pk 
    624  1.32        pk 	return (0);
    625  1.19        pk }
    626  1.78       jdc #endif
    627