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