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