Home | History | Annotate | Line # | Download | only in dev
sbus.c revision 1.23
      1  1.23        pk /*	$NetBSD: sbus.c,v 1.23 1998/04/07 20:43:58 pk 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.7  christos #include <sys/systm.h>
     90   1.1   deraadt #include <sys/device.h>
     91  1.17        pk #include <vm/vm.h>
     92   1.1   deraadt 
     93  1.19        pk #include <machine/bus.h>
     94  1.19        pk #include <sparc/dev/sbusreg.h>
     95  1.19        pk #include <sparc/dev/sbusvar.h>
     96  1.19        pk 
     97  1.19        pk #include <sparc/sparc/iommuvar.h>
     98   1.1   deraadt #include <machine/autoconf.h>
     99   1.1   deraadt 
    100   1.1   deraadt 
    101   1.8   thorpej void sbusreset __P((int));
    102   1.8   thorpej 
    103  1.19        pk static bus_space_tag_t sbus_alloc_bustag __P((struct sbus_softc *));
    104  1.19        pk static int sbus_get_intr __P((struct sbus_softc *, int, int *));
    105  1.22        pk static int sbus_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
    106  1.22        pk 			      int, bus_space_handle_t *));
    107  1.20        pk static int _sbus_bus_map __P((
    108  1.22        pk 		bus_space_tag_t,
    109  1.20        pk 		bus_type_t,		/*slot*/
    110  1.20        pk 		bus_addr_t,		/*offset*/
    111  1.20        pk 		bus_size_t,		/*size*/
    112  1.20        pk 		int,			/*flags*/
    113  1.20        pk 		vm_offset_t,		/*preferred virtual address */
    114  1.20        pk 		bus_space_handle_t *));
    115  1.23        pk static void *sbus_intr_establish __P((
    116  1.23        pk 		bus_space_tag_t,
    117  1.23        pk 		int,			/*level*/
    118  1.23        pk 		int,			/*flags*/
    119  1.23        pk 		int (*) __P((void *)),	/*handler*/
    120  1.23        pk 		void *));		/*handler arg*/
    121  1.19        pk 
    122  1.19        pk 
    123   1.1   deraadt /* autoconfiguration driver */
    124  1.19        pk int	sbus_match_mainbus __P((struct device *, struct cfdata *, void *));
    125  1.19        pk int	sbus_match_iommu __P((struct device *, struct cfdata *, void *));
    126  1.19        pk void	sbus_attach_mainbus __P((struct device *, struct device *, void *));
    127  1.19        pk void	sbus_attach_iommu __P((struct device *, struct device *, void *));
    128   1.8   thorpej 
    129  1.19        pk struct cfattach sbus_mainbus_ca = {
    130  1.19        pk 	sizeof(struct sbus_softc), sbus_match_mainbus, sbus_attach_mainbus
    131  1.19        pk };
    132  1.19        pk struct cfattach sbus_iommu_ca = {
    133  1.19        pk 	sizeof(struct sbus_softc), sbus_match_iommu, sbus_attach_iommu
    134   1.2   deraadt };
    135   1.7  christos 
    136  1.18   thorpej extern struct cfdriver sbus_cd;
    137   1.1   deraadt 
    138  1.19        pk /* If the PROM does not provide the `ranges' property, we make up our own */
    139  1.19        pk struct rom_range sbus_translations[] = {
    140  1.19        pk 	/* Assume a maximum of 4 Sbus slots, all mapped to on-board io space */
    141  1.19        pk 	{ 0, 0, PMAP_OBIO, SBUS_ADDR(0,0), 1 << 25 },
    142  1.19        pk 	{ 1, 0, PMAP_OBIO, SBUS_ADDR(1,0), 1 << 25 },
    143  1.19        pk 	{ 2, 0, PMAP_OBIO, SBUS_ADDR(2,0), 1 << 25 },
    144  1.19        pk 	{ 3, 0, PMAP_OBIO, SBUS_ADDR(3,0), 1 << 25 }
    145  1.19        pk };
    146  1.19        pk 
    147  1.19        pk /*
    148  1.19        pk  * Child devices receive the Sbus interrupt level in their attach
    149  1.19        pk  * arguments. We translate these to CPU IPLs using the following
    150  1.19        pk  * tables. Note: obio bus interrupt levels are identical to the
    151  1.19        pk  * processor IPL.
    152  1.19        pk  *
    153  1.19        pk  * The second set of tables is used when the Sbus interrupt level
    154  1.19        pk  * cannot be had from the PROM as an `interrupt' property. We then
    155  1.19        pk  * fall back on the `intr' property which contains the CPU IPL.
    156  1.19        pk  */
    157  1.19        pk 
    158  1.19        pk /* Translate Sbus interrupt level to processor IPL */
    159  1.19        pk static int intr_sbus2ipl_4c[] = {
    160  1.19        pk 	0, 1, 2, 3, 5, 7, 8, 9
    161  1.19        pk };
    162  1.19        pk static int intr_sbus2ipl_4m[] = {
    163  1.19        pk 	0, 2, 3, 5, 7, 9, 11, 13
    164  1.19        pk };
    165  1.19        pk 
    166  1.20        pk /*
    167  1.20        pk  * This value is or'ed into the attach args' interrupt level cookie
    168  1.20        pk  * if the interrupt level comes from an `intr' property, i.e. it is
    169  1.20        pk  * not an Sbus interrupt level.
    170  1.20        pk  */
    171  1.19        pk #define SBUS_INTR_COMPAT	0x80000000
    172  1.19        pk 
    173  1.19        pk 
    174   1.1   deraadt /*
    175   1.1   deraadt  * Print the location of some sbus-attached device (called just
    176   1.1   deraadt  * before attaching that device).  If `sbus' is not NULL, the
    177   1.1   deraadt  * device was found but not configured; print the sbus as well.
    178   1.1   deraadt  * Return UNCONF (config_find ignores this if the device was configured).
    179   1.1   deraadt  */
    180   1.1   deraadt int
    181  1.19        pk sbus_print(args, busname)
    182   1.1   deraadt 	void *args;
    183  1.19        pk 	const char *busname;
    184   1.1   deraadt {
    185  1.19        pk 	struct sbus_attach_args *sa = args;
    186   1.1   deraadt 
    187  1.19        pk 	if (busname)
    188  1.19        pk 		printf("%s at %s", sa->sa_name, busname);
    189  1.19        pk 	printf(" slot %d offset 0x%x", sa->sa_slot, sa->sa_offset);
    190  1.19        pk 	if (sa->sa_pri) {
    191  1.19        pk 		int level = sa->sa_pri;
    192  1.19        pk 		struct sbus_softc *sc =
    193  1.19        pk 			(struct sbus_softc *) sa->sa_bustag->cookie;
    194  1.19        pk 
    195  1.19        pk 		printf(" level %d", level & ~SBUS_INTR_COMPAT);
    196  1.19        pk 		if ((level & SBUS_INTR_COMPAT) == 0) {
    197  1.19        pk 			int ipl = sc->sc_intr2ipl[level];
    198  1.19        pk 			if (ipl != level)
    199  1.19        pk 				printf(" (ipl %d)", ipl);
    200  1.19        pk 		}
    201  1.19        pk 	}
    202   1.1   deraadt 	return (UNCONF);
    203   1.1   deraadt }
    204   1.1   deraadt 
    205   1.3   deraadt int
    206  1.19        pk sbus_match_mainbus(parent, cf, aux)
    207  1.19        pk 	struct device *parent;
    208  1.19        pk 	struct cfdata *cf;
    209  1.19        pk 	void *aux;
    210  1.19        pk {
    211  1.19        pk 	struct mainbus_attach_args *ma = aux;
    212  1.19        pk 
    213  1.19        pk 	if (CPU_ISSUN4)
    214  1.19        pk 		return (0);
    215  1.19        pk 
    216  1.19        pk 	return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
    217  1.19        pk }
    218  1.19        pk 
    219  1.19        pk int
    220  1.19        pk sbus_match_iommu(parent, cf, aux)
    221   1.3   deraadt 	struct device *parent;
    222  1.14        pk 	struct cfdata *cf;
    223  1.14        pk 	void *aux;
    224   1.3   deraadt {
    225  1.19        pk 	struct iommu_attach_args *ia = aux;
    226   1.3   deraadt 
    227   1.9        pk 	if (CPU_ISSUN4)
    228   1.3   deraadt 		return (0);
    229   1.9        pk 
    230  1.19        pk 	return (strcmp(cf->cf_driver->cd_name, ia->iom_name) == 0);
    231   1.3   deraadt }
    232   1.3   deraadt 
    233   1.1   deraadt /*
    234   1.1   deraadt  * Attach an Sbus.
    235   1.1   deraadt  */
    236   1.1   deraadt void
    237  1.19        pk sbus_attach_mainbus(parent, self, aux)
    238   1.1   deraadt 	struct device *parent;
    239   1.1   deraadt 	struct device *self;
    240   1.1   deraadt 	void *aux;
    241   1.1   deraadt {
    242  1.19        pk 	struct sbus_softc *sc = (struct sbus_softc *)self;
    243  1.19        pk 	struct mainbus_attach_args *ma = aux;
    244  1.19        pk 	int node = ma->ma_node;
    245   1.1   deraadt 
    246   1.1   deraadt 	/*
    247   1.1   deraadt 	 * XXX there is only one Sbus, for now -- do not know how to
    248   1.1   deraadt 	 * address children on others
    249   1.1   deraadt 	 */
    250   1.1   deraadt 	if (sc->sc_dev.dv_unit > 0) {
    251  1.13  christos 		printf(" unsupported\n");
    252   1.1   deraadt 		return;
    253   1.1   deraadt 	}
    254   1.1   deraadt 
    255  1.19        pk 	sc->sc_bustag = ma->ma_bustag;
    256  1.19        pk 	sc->sc_dmatag = ma->ma_dmatag;
    257  1.19        pk 
    258  1.19        pk 	/* Setup interrupt translation tables */
    259  1.19        pk 	sc->sc_intr2ipl = CPU_ISSUN4C
    260  1.19        pk 				? intr_sbus2ipl_4c
    261  1.19        pk 				: intr_sbus2ipl_4m;
    262  1.19        pk 
    263  1.19        pk 	/*
    264  1.19        pk 	 * Record clock frequency for synchronous SCSI.
    265  1.19        pk 	 * IS THIS THE CORRECT DEFAULT??
    266  1.19        pk 	 */
    267  1.19        pk 	sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
    268  1.19        pk 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    269  1.19        pk 
    270  1.19        pk 	sbus_attach(sc, "sbus", node, ma->ma_bp, NULL);
    271  1.19        pk }
    272  1.19        pk 
    273  1.19        pk void
    274  1.19        pk sbus_attach_iommu(parent, self, aux)
    275  1.19        pk 	struct device *parent;
    276  1.19        pk 	struct device *self;
    277  1.19        pk 	void *aux;
    278  1.19        pk {
    279  1.19        pk 	struct sbus_softc *sc = (struct sbus_softc *)self;
    280  1.19        pk 	struct iommu_attach_args *ia = aux;
    281  1.19        pk 	int node = ia->iom_node;
    282  1.19        pk 
    283  1.19        pk 	sc->sc_bustag = ia->iom_bustag;
    284  1.19        pk 	sc->sc_dmatag = ia->iom_dmatag;
    285  1.19        pk 
    286  1.19        pk 	/* Setup interrupt translation tables */
    287  1.19        pk 	sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
    288  1.19        pk 
    289   1.1   deraadt 	/*
    290   1.1   deraadt 	 * Record clock frequency for synchronous SCSI.
    291   1.1   deraadt 	 * IS THIS THE CORRECT DEFAULT??
    292   1.1   deraadt 	 */
    293   1.1   deraadt 	sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
    294  1.13  christos 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    295  1.10    abrown 
    296  1.19        pk 	sbus_attach(sc, "sbus", node, ia->iom_bp, NULL);
    297  1.19        pk }
    298  1.19        pk 
    299  1.19        pk void
    300  1.19        pk sbus_attach(sc, busname, busnode, bp, specials)
    301  1.19        pk 	struct sbus_softc *sc;
    302  1.19        pk 	char *busname;
    303  1.19        pk 	int busnode;
    304  1.19        pk 	struct bootpath *bp;
    305  1.19        pk 	const char * const *specials;
    306  1.19        pk {
    307  1.19        pk 	int node0, node, error;
    308  1.19        pk 	const char *sp;
    309  1.19        pk 	const char *const *ssp;
    310  1.19        pk 	bus_space_tag_t sbt;
    311  1.19        pk 	struct sbus_attach_args sa;
    312  1.19        pk 
    313  1.19        pk 	sbt = sbus_alloc_bustag(sc);
    314  1.19        pk 
    315  1.10    abrown 	/*
    316  1.10    abrown 	 * Get the SBus burst transfer size if burst transfers are supported
    317  1.10    abrown 	 */
    318  1.19        pk 	sc->sc_burst = getpropint(busnode, "burst-sizes", 0);
    319   1.1   deraadt 
    320  1.19        pk 	/* Propagate bootpath */
    321  1.19        pk 	if (bp != NULL && strcmp(bp->name, busname) == 0)
    322  1.19        pk 		bp++;
    323   1.1   deraadt 	else
    324  1.19        pk 		bp = NULL;
    325   1.1   deraadt 
    326  1.19        pk 	/*
    327  1.19        pk 	 * Collect address translations from the OBP.
    328  1.19        pk 	 */
    329  1.19        pk 	error = getpropA(busnode, "ranges", sizeof(struct rom_range),
    330  1.19        pk 			 &sc->sc_nrange, (void **)&sc->sc_range);
    331  1.19        pk 	switch (error) {
    332  1.19        pk 	case 0:
    333  1.19        pk 		break;
    334  1.19        pk 	case ENOENT:
    335  1.19        pk 		/* Fall back to our own `range' construction */
    336  1.19        pk 		sc->sc_range = sbus_translations;
    337  1.19        pk 		sc->sc_nrange =
    338  1.19        pk 			sizeof(sbus_translations)/sizeof(sbus_translations[0]);
    339  1.19        pk 		break;
    340  1.19        pk 	default:
    341  1.19        pk 		panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
    342  1.16        pk 	}
    343   1.9        pk 
    344   1.1   deraadt 	/*
    345   1.1   deraadt 	 * Loop through ROM children, fixing any relative addresses
    346   1.1   deraadt 	 * and then configuring each device.
    347  1.19        pk 	 * `specials' is an array of device names that are treated
    348  1.19        pk 	 * specially:
    349   1.1   deraadt 	 */
    350  1.19        pk 	node0 = firstchild(busnode);
    351  1.19        pk 	for (ssp = specials ; ssp != NULL && *(sp = *ssp) != 0; ssp++) {
    352  1.19        pk 		if ((node = findnode(node0, sp)) == 0) {
    353  1.19        pk 			panic("could not find %s amongst %s devices",
    354  1.19        pk 				sp, busname);
    355  1.19        pk 		}
    356  1.19        pk 
    357  1.19        pk 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    358  1.19        pk 					   node, bp, &sa) != 0) {
    359  1.19        pk 			panic("sbus_attach: %s: incomplete", sp);
    360  1.19        pk 		}
    361  1.19        pk 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
    362  1.19        pk 	}
    363  1.19        pk 
    364  1.19        pk 	for (node = node0; node; node = nextsibling(node)) {
    365  1.19        pk 		char *name = getpropstring(node, "name");
    366  1.19        pk 		for (ssp = specials, sp = NULL;
    367  1.19        pk 		     ssp != NULL && (sp = *ssp) != NULL;
    368  1.19        pk 		     ssp++)
    369  1.19        pk 			if (strcmp(name, sp) == 0)
    370  1.19        pk 				break;
    371  1.19        pk 
    372  1.19        pk 		if (sp != NULL)
    373  1.19        pk 			/* Already configured as an "early" device */
    374  1.19        pk 			continue;
    375  1.19        pk 
    376  1.19        pk 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    377  1.19        pk 					   node, bp, &sa) != 0) {
    378  1.19        pk 			printf("sbus_attach: %s: incomplete\n", name);
    379   1.1   deraadt 			continue;
    380  1.19        pk 		}
    381  1.19        pk 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
    382  1.19        pk 	}
    383  1.19        pk }
    384   1.9        pk 
    385  1.19        pk int
    386  1.19        pk sbus_setup_attach_args(sc, bustag, dmatag, node, bp, sa)
    387  1.19        pk 	struct sbus_softc	*sc;
    388  1.19        pk 	bus_space_tag_t		bustag;
    389  1.19        pk 	bus_dma_tag_t		dmatag;
    390  1.19        pk 	int			node;
    391  1.19        pk 	struct bootpath		*bp;
    392  1.19        pk 	struct sbus_attach_args	*sa;
    393  1.19        pk {
    394  1.19        pk 	struct	rom_reg romreg;
    395  1.19        pk 	int	base;
    396  1.19        pk 	int	error;
    397  1.19        pk 
    398  1.19        pk 	bzero(sa, sizeof(struct sbus_attach_args));
    399  1.19        pk 	sa->sa_name = getpropstring(node, "name");
    400  1.19        pk 	sa->sa_bustag = bustag;
    401  1.19        pk 	sa->sa_dmatag = dmatag;
    402  1.19        pk 	sa->sa_node = node;
    403  1.19        pk 	sa->sa_bp = bp;
    404  1.19        pk 
    405  1.19        pk 	if ((error = getprop_reg1(node, &romreg)) != 0)
    406  1.19        pk 		return (error);
    407  1.19        pk 
    408  1.19        pk 	/* We pass only the first "reg" property */
    409  1.19        pk 	base = (int)romreg.rr_paddr;
    410  1.19        pk 	if (SBUS_ABS(base)) {
    411  1.19        pk 		sa->sa_slot = SBUS_ABS_TO_SLOT(base);
    412  1.19        pk 		sa->sa_offset = SBUS_ABS_TO_OFFSET(base);
    413  1.19        pk 	} else {
    414  1.19        pk 		sa->sa_slot = romreg.rr_iospace;
    415  1.19        pk 		sa->sa_offset = base;
    416   1.9        pk 	}
    417  1.19        pk 	sa->sa_size = romreg.rr_len;
    418  1.19        pk 
    419  1.19        pk 	if ((error = sbus_get_intr(sc, node, &sa->sa_pri)) != 0)
    420  1.19        pk 		return (error);
    421  1.19        pk 
    422  1.19        pk 	if ((error = getprop_address1(node, &sa->sa_promvaddr)) != 0)
    423  1.19        pk 		return (error);
    424  1.19        pk 
    425  1.19        pk 	return (0);
    426   1.9        pk }
    427   1.9        pk 
    428  1.19        pk int
    429  1.22        pk _sbus_bus_map(t, btype, offset, size, flags, vaddr, hp)
    430  1.22        pk 	bus_space_tag_t t;
    431  1.20        pk 	bus_type_t btype;
    432  1.20        pk 	bus_addr_t offset;
    433  1.20        pk 	bus_size_t size;
    434  1.20        pk 	int	flags;
    435  1.19        pk 	vm_offset_t vaddr;
    436  1.19        pk 	bus_space_handle_t *hp;
    437   1.9        pk {
    438  1.22        pk 	struct sbus_softc *sc = t->cookie;
    439  1.20        pk 	int slot = btype;
    440  1.19        pk 	int i;
    441  1.19        pk 
    442  1.19        pk 	for (i = 0; i < sc->sc_nrange; i++) {
    443  1.19        pk 		bus_addr_t paddr;
    444  1.19        pk 		bus_type_t iospace;
    445  1.19        pk 
    446  1.19        pk 		if (sc->sc_range[i].cspace != slot)
    447  1.19        pk 			continue;
    448  1.19        pk 
    449  1.19        pk 		/* We've found the connection to the parent bus */
    450  1.19        pk 		paddr = sc->sc_range[i].poffset + offset;
    451  1.19        pk 		iospace = sc->sc_range[i].pspace;
    452  1.19        pk 		return (bus_space_map2(sc->sc_bustag, iospace, paddr,
    453  1.19        pk 					size, flags, vaddr, hp));
    454  1.19        pk 	}
    455  1.19        pk 
    456  1.19        pk 	return (EINVAL);
    457  1.19        pk }
    458   1.9        pk 
    459  1.19        pk int
    460  1.22        pk sbus_bus_mmap(t, btype, paddr, flags, hp)
    461  1.22        pk 	bus_space_tag_t t;
    462  1.19        pk 	bus_type_t btype;
    463  1.19        pk 	bus_addr_t paddr;
    464  1.19        pk 	int flags;
    465  1.22        pk 	bus_space_handle_t *hp;
    466  1.19        pk {
    467  1.19        pk 	int slot = (int)btype;
    468  1.19        pk 	int offset = (int)paddr;
    469  1.22        pk 	struct sbus_softc *sc = t->cookie;
    470  1.19        pk 	int i;
    471  1.19        pk 
    472  1.19        pk 	for (i = 0; i < sc->sc_nrange; i++) {
    473  1.19        pk 		bus_addr_t paddr;
    474  1.19        pk 		bus_addr_t iospace;
    475   1.9        pk 
    476  1.19        pk 		if (sc->sc_range[i].cspace != slot)
    477  1.19        pk 			continue;
    478   1.9        pk 
    479  1.19        pk 		paddr = sc->sc_range[i].poffset + offset;
    480  1.19        pk 		iospace = (bus_addr_t)sc->sc_range[i].pspace;
    481  1.22        pk 		return (bus_space_mmap(sc->sc_bustag, iospace, paddr,
    482  1.22        pk 				       flags, hp));
    483   1.1   deraadt 	}
    484  1.19        pk 
    485  1.19        pk 	return (-1);
    486   1.1   deraadt }
    487   1.1   deraadt 
    488  1.19        pk 
    489   1.1   deraadt /*
    490   1.1   deraadt  * Each attached device calls sbus_establish after it initializes
    491   1.1   deraadt  * its sbusdev portion.
    492   1.1   deraadt  */
    493   1.1   deraadt void
    494   1.1   deraadt sbus_establish(sd, dev)
    495   1.1   deraadt 	register struct sbusdev *sd;
    496   1.1   deraadt 	register struct device *dev;
    497   1.1   deraadt {
    498   1.9        pk 	register struct sbus_softc *sc;
    499   1.9        pk 	register struct device *curdev;
    500   1.9        pk 
    501   1.9        pk 	/*
    502   1.9        pk 	 * We have to look for the sbus by name, since it is not necessarily
    503   1.9        pk 	 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
    504   1.9        pk 	 * We don't just use the device structure of the above-attached
    505   1.9        pk 	 * sbus, since we might (in the future) support multiple sbus's.
    506   1.9        pk 	 */
    507   1.9        pk 	for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
    508   1.9        pk 		if (!curdev || !curdev->dv_xname)
    509  1.15        pk 			panic("sbus_establish: can't find sbus parent for %s",
    510  1.15        pk 			      sd->sd_dev->dv_xname
    511  1.15        pk 					? sd->sd_dev->dv_xname
    512  1.15        pk 					: "<unknown>" );
    513   1.9        pk 
    514   1.9        pk 		if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
    515  1.15        pk 			break;
    516   1.9        pk 	}
    517   1.9        pk 	sc = (struct sbus_softc *) curdev;
    518   1.1   deraadt 
    519   1.1   deraadt 	sd->sd_dev = dev;
    520   1.1   deraadt 	sd->sd_bchain = sc->sc_sbdev;
    521   1.1   deraadt 	sc->sc_sbdev = sd;
    522   1.1   deraadt }
    523   1.1   deraadt 
    524   1.1   deraadt /*
    525   1.1   deraadt  * Reset the given sbus. (???)
    526   1.1   deraadt  */
    527   1.1   deraadt void
    528   1.1   deraadt sbusreset(sbus)
    529   1.1   deraadt 	int sbus;
    530   1.1   deraadt {
    531   1.1   deraadt 	register struct sbusdev *sd;
    532   1.8   thorpej 	struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
    533   1.1   deraadt 	struct device *dev;
    534   1.1   deraadt 
    535  1.13  christos 	printf("reset %s:", sc->sc_dev.dv_xname);
    536   1.1   deraadt 	for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
    537   1.1   deraadt 		if (sd->sd_reset) {
    538   1.1   deraadt 			dev = sd->sd_dev;
    539   1.1   deraadt 			(*sd->sd_reset)(dev);
    540  1.13  christos 			printf(" %s", dev->dv_xname);
    541   1.1   deraadt 		}
    542   1.1   deraadt 	}
    543   1.1   deraadt }
    544  1.19        pk 
    545  1.19        pk 
    546  1.19        pk /*
    547  1.19        pk  * Get interrupt attributes for an Sbus device.
    548  1.19        pk  */
    549  1.19        pk int
    550  1.19        pk sbus_get_intr(sc, node, ip)
    551  1.19        pk 	struct sbus_softc *sc;
    552  1.19        pk 	int node;
    553  1.19        pk 	int *ip;
    554  1.19        pk {
    555  1.19        pk 	struct rom_intr *rip;
    556  1.19        pk 	int *ipl;
    557  1.19        pk 	int n;
    558  1.19        pk 
    559  1.19        pk 	/*
    560  1.19        pk 	 * The `interrupts' property contains the Sbus interrupt level.
    561  1.19        pk 	 */
    562  1.19        pk 	ipl = NULL;
    563  1.19        pk 	if (getpropA(node, "interrupts", sizeof(int), &n, (void **)&ipl) == 0) {
    564  1.19        pk 		*ip = ipl[0];
    565  1.19        pk 		free(ipl, M_DEVBUF);
    566  1.19        pk 		return (0);
    567  1.19        pk 	}
    568  1.19        pk 
    569  1.19        pk 	/*
    570  1.19        pk 	 * Fall back on `intr' property.
    571  1.19        pk 	 */
    572  1.19        pk 	rip = NULL;
    573  1.19        pk 	switch (getpropA(node, "intr", sizeof(*rip), &n, (void **)&rip)) {
    574  1.19        pk 	case 0:
    575  1.19        pk 		*ip = (rip[0].int_pri & 0xf) | SBUS_INTR_COMPAT;
    576  1.19        pk 		free(rip, M_DEVBUF);
    577  1.19        pk 		return (0);
    578  1.19        pk 	case ENOENT:
    579  1.19        pk 		*ip = 0;
    580  1.19        pk 		return (0);
    581  1.19        pk 	}
    582  1.19        pk 
    583  1.19        pk 	return (-1);
    584  1.19        pk }
    585  1.19        pk 
    586  1.19        pk 
    587  1.19        pk /*
    588  1.19        pk  * Install an interrupt handler for an Sbus device.
    589  1.19        pk  */
    590  1.19        pk void *
    591  1.22        pk sbus_intr_establish(t, level, flags, handler, arg)
    592  1.22        pk 	bus_space_tag_t t;
    593  1.19        pk 	int level;
    594  1.19        pk 	int flags;
    595  1.19        pk 	int (*handler) __P((void *));
    596  1.19        pk 	void *arg;
    597  1.19        pk {
    598  1.22        pk 	struct sbus_softc *sc = t->cookie;
    599  1.19        pk 	struct intrhand *ih;
    600  1.19        pk 	int ipl;
    601  1.19        pk 
    602  1.19        pk 	ih = (struct intrhand *)
    603  1.19        pk 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    604  1.19        pk 	if (ih == NULL)
    605  1.19        pk 		return (NULL);
    606  1.19        pk 
    607  1.19        pk 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0)
    608  1.19        pk 		ipl = level;
    609  1.19        pk 	else if ((level & SBUS_INTR_COMPAT) != 0)
    610  1.19        pk 		ipl = level & ~SBUS_INTR_COMPAT;
    611  1.19        pk 	else
    612  1.19        pk 		ipl = sc->sc_intr2ipl[level];
    613  1.19        pk 
    614  1.19        pk 	ih->ih_fun = handler;
    615  1.19        pk 	ih->ih_arg = arg;
    616  1.19        pk 	if ((flags & BUS_INTR_ESTABLISH_FASTTRAP) != 0)
    617  1.19        pk 		intr_fasttrap(ipl, (void (*)__P((void)))handler);
    618  1.19        pk 	else
    619  1.19        pk 		intr_establish(ipl, ih);
    620  1.19        pk 	return (ih);
    621  1.19        pk }
    622  1.19        pk 
    623  1.19        pk static bus_space_tag_t
    624  1.19        pk sbus_alloc_bustag(sc)
    625  1.19        pk 	struct sbus_softc *sc;
    626  1.19        pk {
    627  1.19        pk 	bus_space_tag_t sbt;
    628  1.19        pk 
    629  1.19        pk 	sbt = (bus_space_tag_t)
    630  1.19        pk 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    631  1.19        pk 	if (sbt == NULL)
    632  1.19        pk 		return (NULL);
    633  1.19        pk 
    634  1.19        pk 	bzero(sbt, sizeof *sbt);
    635  1.19        pk 	sbt->cookie = sc;
    636  1.22        pk 	sbt->parent = sc->sc_bustag;
    637  1.19        pk 	sbt->sparc_bus_map = _sbus_bus_map;
    638  1.19        pk 	sbt->sparc_bus_mmap = sbus_bus_mmap;
    639  1.19        pk 	sbt->sparc_intr_establish = sbus_intr_establish;
    640  1.19        pk 	return (sbt);
    641  1.19        pk }
    642