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