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