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