Home | History | Annotate | Line # | Download | only in dev
ebus.c revision 1.29.4.2
      1  1.29.4.2  nathanw /*	$NetBSD: ebus.c,v 1.29.4.2 2002/04/01 07:43:00 nathanw Exp $	*/
      2  1.29.4.2  nathanw 
      3  1.29.4.2  nathanw /*
      4  1.29.4.2  nathanw  * Copyright (c) 1999, 2000, 2001 Matthew R. Green
      5  1.29.4.2  nathanw  * All rights reserved.
      6  1.29.4.2  nathanw  *
      7  1.29.4.2  nathanw  * Redistribution and use in source and binary forms, with or without
      8  1.29.4.2  nathanw  * modification, are permitted provided that the following conditions
      9  1.29.4.2  nathanw  * are met:
     10  1.29.4.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     11  1.29.4.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     12  1.29.4.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.29.4.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     14  1.29.4.2  nathanw  *    documentation and/or other materials provided with the distribution.
     15  1.29.4.2  nathanw  * 3. The name of the author may not be used to endorse or promote products
     16  1.29.4.2  nathanw  *    derived from this software without specific prior written permission.
     17  1.29.4.2  nathanw  *
     18  1.29.4.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.29.4.2  nathanw  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.29.4.2  nathanw  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.29.4.2  nathanw  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.29.4.2  nathanw  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  1.29.4.2  nathanw  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  1.29.4.2  nathanw  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  1.29.4.2  nathanw  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  1.29.4.2  nathanw  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.29.4.2  nathanw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.29.4.2  nathanw  * SUCH DAMAGE.
     29  1.29.4.2  nathanw  */
     30  1.29.4.2  nathanw 
     31  1.29.4.2  nathanw #include "opt_ddb.h"
     32  1.29.4.2  nathanw 
     33  1.29.4.2  nathanw /*
     34  1.29.4.2  nathanw  * UltraSPARC 5 and beyond ebus support.
     35  1.29.4.2  nathanw  *
     36  1.29.4.2  nathanw  * note that this driver is not complete:
     37  1.29.4.2  nathanw  *	- interrupt establish is written and appears to work
     38  1.29.4.2  nathanw  *	- bus map code is written and appears to work
     39  1.29.4.2  nathanw  *	- ebus2 dma code is completely unwritten, we just punt to
     40  1.29.4.2  nathanw  *	  the iommu.
     41  1.29.4.2  nathanw  */
     42  1.29.4.2  nathanw 
     43  1.29.4.2  nathanw #ifdef DEBUG
     44  1.29.4.2  nathanw #define	EDB_PROM	0x01
     45  1.29.4.2  nathanw #define EDB_CHILD	0x02
     46  1.29.4.2  nathanw #define	EDB_INTRMAP	0x04
     47  1.29.4.2  nathanw #define EDB_BUSMAP	0x08
     48  1.29.4.2  nathanw int ebus_debug = 0;
     49  1.29.4.2  nathanw #define DPRINTF(l, s)   do { if (ebus_debug & l) printf s; } while (0)
     50  1.29.4.2  nathanw #else
     51  1.29.4.2  nathanw #define DPRINTF(l, s)
     52  1.29.4.2  nathanw #endif
     53  1.29.4.2  nathanw 
     54  1.29.4.2  nathanw #include <sys/param.h>
     55  1.29.4.2  nathanw #include <sys/conf.h>
     56  1.29.4.2  nathanw #include <sys/device.h>
     57  1.29.4.2  nathanw #include <sys/errno.h>
     58  1.29.4.2  nathanw #include <sys/extent.h>
     59  1.29.4.2  nathanw #include <sys/malloc.h>
     60  1.29.4.2  nathanw #include <sys/systm.h>
     61  1.29.4.2  nathanw #include <sys/time.h>
     62  1.29.4.2  nathanw 
     63  1.29.4.2  nathanw #define _SPARC_BUS_DMA_PRIVATE
     64  1.29.4.2  nathanw #include <machine/bus.h>
     65  1.29.4.2  nathanw #include <machine/autoconf.h>
     66  1.29.4.2  nathanw #include <machine/openfirm.h>
     67  1.29.4.2  nathanw 
     68  1.29.4.2  nathanw #include <dev/pci/pcivar.h>
     69  1.29.4.2  nathanw #include <dev/pci/pcireg.h>
     70  1.29.4.2  nathanw #include <dev/pci/pcidevs.h>
     71  1.29.4.2  nathanw 
     72  1.29.4.2  nathanw #include <sparc64/dev/iommureg.h>
     73  1.29.4.2  nathanw #include <sparc64/dev/iommuvar.h>
     74  1.29.4.2  nathanw #include <sparc64/dev/psychoreg.h>
     75  1.29.4.2  nathanw #include <sparc64/dev/psychovar.h>
     76  1.29.4.2  nathanw #include <dev/ebus/ebusreg.h>
     77  1.29.4.2  nathanw #include <dev/ebus/ebusvar.h>
     78  1.29.4.2  nathanw #include <sparc64/sparc64/cache.h>
     79  1.29.4.2  nathanw 
     80  1.29.4.2  nathanw struct ebus_softc {
     81  1.29.4.2  nathanw 	struct device			sc_dev;
     82  1.29.4.2  nathanw 
     83  1.29.4.2  nathanw 	int				sc_node;
     84  1.29.4.2  nathanw 
     85  1.29.4.2  nathanw 	bus_space_tag_t			sc_memtag;	/* from pci */
     86  1.29.4.2  nathanw 	bus_space_tag_t			sc_iotag;	/* from pci */
     87  1.29.4.2  nathanw 	bus_space_tag_t			sc_childbustag;	/* pass to children */
     88  1.29.4.2  nathanw 	bus_dma_tag_t			sc_dmatag;
     89  1.29.4.2  nathanw 
     90  1.29.4.2  nathanw 	struct ebus_ranges		*sc_range;
     91  1.29.4.2  nathanw 	struct ebus_interrupt_map	*sc_intmap;
     92  1.29.4.2  nathanw 	struct ebus_interrupt_map_mask	sc_intmapmask;
     93  1.29.4.2  nathanw 
     94  1.29.4.2  nathanw 	int				sc_nrange;	/* counters */
     95  1.29.4.2  nathanw 	int				sc_nintmap;
     96  1.29.4.2  nathanw };
     97  1.29.4.2  nathanw 
     98  1.29.4.2  nathanw int	ebus_match __P((struct device *, struct cfdata *, void *));
     99  1.29.4.2  nathanw void	ebus_attach __P((struct device *, struct device *, void *));
    100  1.29.4.2  nathanw 
    101  1.29.4.2  nathanw struct cfattach ebus_ca = {
    102  1.29.4.2  nathanw 	sizeof(struct ebus_softc), ebus_match, ebus_attach
    103  1.29.4.2  nathanw };
    104  1.29.4.2  nathanw 
    105  1.29.4.2  nathanw bus_space_tag_t ebus_alloc_bus_tag __P((struct ebus_softc *, int));
    106  1.29.4.2  nathanw 
    107  1.29.4.2  nathanw int	ebus_setup_attach_args __P((struct ebus_softc *, int,
    108  1.29.4.2  nathanw 	    struct ebus_attach_args *));
    109  1.29.4.2  nathanw void	ebus_destroy_attach_args __P((struct ebus_attach_args *));
    110  1.29.4.2  nathanw int	ebus_print __P((void *, const char *));
    111  1.29.4.2  nathanw void	ebus_find_ino __P((struct ebus_softc *, struct ebus_attach_args *));
    112  1.29.4.2  nathanw int	ebus_find_node __P((struct pci_attach_args *));
    113  1.29.4.2  nathanw 
    114  1.29.4.2  nathanw /*
    115  1.29.4.2  nathanw  * here are our bus space and bus dma routines.
    116  1.29.4.2  nathanw  */
    117  1.29.4.2  nathanw static paddr_t ebus_bus_mmap __P((bus_space_tag_t, bus_addr_t, off_t, int, int));
    118  1.29.4.2  nathanw static int _ebus_bus_map __P((bus_space_tag_t, bus_addr_t, bus_size_t, int,
    119  1.29.4.2  nathanw 			      vaddr_t, bus_space_handle_t *));
    120  1.29.4.2  nathanw static void *ebus_intr_establish __P((bus_space_tag_t, int, int, int,
    121  1.29.4.2  nathanw 				int (*) __P((void *)), void *));
    122  1.29.4.2  nathanw 
    123  1.29.4.2  nathanw int
    124  1.29.4.2  nathanw ebus_match(parent, match, aux)
    125  1.29.4.2  nathanw 	struct device *parent;
    126  1.29.4.2  nathanw 	struct cfdata *match;
    127  1.29.4.2  nathanw 	void *aux;
    128  1.29.4.2  nathanw {
    129  1.29.4.2  nathanw 	struct pci_attach_args *pa = aux;
    130  1.29.4.2  nathanw 	char name[10];
    131  1.29.4.2  nathanw 	int node;
    132  1.29.4.2  nathanw 
    133  1.29.4.2  nathanw 	/* Only attach if there's a PROM node. */
    134  1.29.4.2  nathanw 	node = PCITAG_NODE(pa->pa_tag);
    135  1.29.4.2  nathanw 	if (node == -1) return (0);
    136  1.29.4.2  nathanw 
    137  1.29.4.2  nathanw 	/* Match a real ebus */
    138  1.29.4.2  nathanw 	OF_getprop(node, "name", &name, sizeof(name));
    139  1.29.4.2  nathanw 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
    140  1.29.4.2  nathanw 	    PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
    141  1.29.4.2  nathanw 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS &&
    142  1.29.4.2  nathanw 		strcmp(name, "ebus") == 0)
    143  1.29.4.2  nathanw 		return (1);
    144  1.29.4.2  nathanw 
    145  1.29.4.2  nathanw 	/* Or a real ebus III */
    146  1.29.4.2  nathanw 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
    147  1.29.4.2  nathanw 	    PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
    148  1.29.4.2  nathanw 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUSIII &&
    149  1.29.4.2  nathanw 		strcmp(name, "ebus") == 0)
    150  1.29.4.2  nathanw 		return (1);
    151  1.29.4.2  nathanw 
    152  1.29.4.2  nathanw 	/* Or a PCI-ISA bridge XXX I hope this is on-board. */
    153  1.29.4.2  nathanw 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE &&
    154  1.29.4.2  nathanw 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_BRIDGE_ISA) {
    155  1.29.4.2  nathanw 		return (1);
    156  1.29.4.2  nathanw 	}
    157  1.29.4.2  nathanw 
    158  1.29.4.2  nathanw 	return (0);
    159  1.29.4.2  nathanw }
    160  1.29.4.2  nathanw 
    161  1.29.4.2  nathanw /*
    162  1.29.4.2  nathanw  * attach an ebus and all it's children.  this code is modeled
    163  1.29.4.2  nathanw  * after the sbus code which does similar things.
    164  1.29.4.2  nathanw  */
    165  1.29.4.2  nathanw void
    166  1.29.4.2  nathanw ebus_attach(parent, self, aux)
    167  1.29.4.2  nathanw 	struct device *parent, *self;
    168  1.29.4.2  nathanw 	void *aux;
    169  1.29.4.2  nathanw {
    170  1.29.4.2  nathanw 	struct ebus_softc *sc = (struct ebus_softc *)self;
    171  1.29.4.2  nathanw 	struct pci_attach_args *pa = aux;
    172  1.29.4.2  nathanw 	struct ebus_attach_args eba;
    173  1.29.4.2  nathanw 	struct ebus_interrupt_map_mask *immp;
    174  1.29.4.2  nathanw 	int node, nmapmask, error;
    175  1.29.4.2  nathanw 	char devinfo[256];
    176  1.29.4.2  nathanw 
    177  1.29.4.2  nathanw 	printf("\n");
    178  1.29.4.2  nathanw 
    179  1.29.4.2  nathanw 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    180  1.29.4.2  nathanw 	printf("%s: %s, revision 0x%02x\n", self->dv_xname, devinfo,
    181  1.29.4.2  nathanw 	    PCI_REVISION(pa->pa_class));
    182  1.29.4.2  nathanw 
    183  1.29.4.2  nathanw 	sc->sc_memtag = pa->pa_memt;
    184  1.29.4.2  nathanw 	sc->sc_iotag = pa->pa_iot;
    185  1.29.4.2  nathanw 	sc->sc_childbustag = ebus_alloc_bus_tag(sc, PCI_MEMORY_BUS_SPACE);
    186  1.29.4.2  nathanw 	sc->sc_dmatag = pa->pa_dmat;
    187  1.29.4.2  nathanw 
    188  1.29.4.2  nathanw 	node = PCITAG_NODE(pa->pa_tag);
    189  1.29.4.2  nathanw 	if (node == -1)
    190  1.29.4.2  nathanw 		panic("could not find ebus node");
    191  1.29.4.2  nathanw 
    192  1.29.4.2  nathanw 	sc->sc_node = node;
    193  1.29.4.2  nathanw 
    194  1.29.4.2  nathanw 	/*
    195  1.29.4.2  nathanw 	 * fill in our softc with information from the prom
    196  1.29.4.2  nathanw 	 */
    197  1.29.4.2  nathanw 	sc->sc_intmap = NULL;
    198  1.29.4.2  nathanw 	sc->sc_range = NULL;
    199  1.29.4.2  nathanw 	error = PROM_getprop(node, "interrupt-map",
    200  1.29.4.2  nathanw 			sizeof(struct ebus_interrupt_map),
    201  1.29.4.2  nathanw 			&sc->sc_nintmap, (void **)&sc->sc_intmap);
    202  1.29.4.2  nathanw 	switch (error) {
    203  1.29.4.2  nathanw 	case 0:
    204  1.29.4.2  nathanw 		immp = &sc->sc_intmapmask;
    205  1.29.4.2  nathanw 		error = PROM_getprop(node, "interrupt-map-mask",
    206  1.29.4.2  nathanw 			    sizeof(struct ebus_interrupt_map_mask), &nmapmask,
    207  1.29.4.2  nathanw 			    (void **)&immp);
    208  1.29.4.2  nathanw 		if (error)
    209  1.29.4.2  nathanw 			panic("could not get ebus interrupt-map-mask");
    210  1.29.4.2  nathanw 		if (nmapmask != 1)
    211  1.29.4.2  nathanw 			panic("ebus interrupt-map-mask is broken");
    212  1.29.4.2  nathanw 		break;
    213  1.29.4.2  nathanw 	case ENOENT:
    214  1.29.4.2  nathanw 		break;
    215  1.29.4.2  nathanw 	default:
    216  1.29.4.2  nathanw 		panic("ebus interrupt-map: error %d", error);
    217  1.29.4.2  nathanw 		break;
    218  1.29.4.2  nathanw 	}
    219  1.29.4.2  nathanw 
    220  1.29.4.2  nathanw 	error = PROM_getprop(node, "ranges", sizeof(struct ebus_ranges),
    221  1.29.4.2  nathanw 	    &sc->sc_nrange, (void **)&sc->sc_range);
    222  1.29.4.2  nathanw 	if (error)
    223  1.29.4.2  nathanw 		panic("ebus ranges: error %d", error);
    224  1.29.4.2  nathanw 
    225  1.29.4.2  nathanw 	/*
    226  1.29.4.2  nathanw 	 * now attach all our children
    227  1.29.4.2  nathanw 	 */
    228  1.29.4.2  nathanw 	DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
    229  1.29.4.2  nathanw 	for (node = firstchild(node); node; node = nextsibling(node)) {
    230  1.29.4.2  nathanw 		char *name = PROM_getpropstring(node, "name");
    231  1.29.4.2  nathanw 
    232  1.29.4.2  nathanw 		if (ebus_setup_attach_args(sc, node, &eba) != 0) {
    233  1.29.4.2  nathanw 			printf("ebus_attach: %s: incomplete\n", name);
    234  1.29.4.2  nathanw 			continue;
    235  1.29.4.2  nathanw 		} else {
    236  1.29.4.2  nathanw 			DPRINTF(EDB_CHILD, ("- found child `%s', attaching\n",
    237  1.29.4.2  nathanw 			    eba.ea_name));
    238  1.29.4.2  nathanw 			(void)config_found(self, &eba, ebus_print);
    239  1.29.4.2  nathanw 		}
    240  1.29.4.2  nathanw 		ebus_destroy_attach_args(&eba);
    241  1.29.4.2  nathanw 	}
    242  1.29.4.2  nathanw }
    243  1.29.4.2  nathanw 
    244  1.29.4.2  nathanw int
    245  1.29.4.2  nathanw ebus_setup_attach_args(sc, node, ea)
    246  1.29.4.2  nathanw 	struct ebus_softc	*sc;
    247  1.29.4.2  nathanw 	int			node;
    248  1.29.4.2  nathanw 	struct ebus_attach_args	*ea;
    249  1.29.4.2  nathanw {
    250  1.29.4.2  nathanw 	int	n, rv;
    251  1.29.4.2  nathanw 
    252  1.29.4.2  nathanw 	bzero(ea, sizeof(struct ebus_attach_args));
    253  1.29.4.2  nathanw 	rv = PROM_getprop(node, "name", 1, &n, (void **)&ea->ea_name);
    254  1.29.4.2  nathanw 	if (rv != 0)
    255  1.29.4.2  nathanw 		return (rv);
    256  1.29.4.2  nathanw 	ea->ea_name[n] = '\0';
    257  1.29.4.2  nathanw 
    258  1.29.4.2  nathanw 	ea->ea_node = node;
    259  1.29.4.2  nathanw 	ea->ea_bustag = sc->sc_childbustag;
    260  1.29.4.2  nathanw 	ea->ea_dmatag = sc->sc_dmatag;
    261  1.29.4.2  nathanw 
    262  1.29.4.2  nathanw 	rv = PROM_getprop(node, "reg", sizeof(struct ebus_regs), &ea->ea_nreg,
    263  1.29.4.2  nathanw 	    (void **)&ea->ea_reg);
    264  1.29.4.2  nathanw 	if (rv)
    265  1.29.4.2  nathanw 		return (rv);
    266  1.29.4.2  nathanw 
    267  1.29.4.2  nathanw 	rv = PROM_getprop(node, "address", sizeof(u_int32_t), &ea->ea_nvaddr,
    268  1.29.4.2  nathanw 	    (void **)&ea->ea_vaddr);
    269  1.29.4.2  nathanw 	if (rv != ENOENT) {
    270  1.29.4.2  nathanw 		if (rv)
    271  1.29.4.2  nathanw 			return (rv);
    272  1.29.4.2  nathanw 
    273  1.29.4.2  nathanw 		if (ea->ea_nreg != ea->ea_nvaddr)
    274  1.29.4.2  nathanw 			printf("ebus loses: device %s: %d regs and %d addrs\n",
    275  1.29.4.2  nathanw 			    ea->ea_name, ea->ea_nreg, ea->ea_nvaddr);
    276  1.29.4.2  nathanw 	} else
    277  1.29.4.2  nathanw 		ea->ea_nvaddr = 0;
    278  1.29.4.2  nathanw 
    279  1.29.4.2  nathanw 	if (PROM_getprop(node, "interrupts", sizeof(u_int32_t), &ea->ea_nintr,
    280  1.29.4.2  nathanw 	    (void **)&ea->ea_intr))
    281  1.29.4.2  nathanw 		ea->ea_nintr = 0;
    282  1.29.4.2  nathanw 	else
    283  1.29.4.2  nathanw 		ebus_find_ino(sc, ea);
    284  1.29.4.2  nathanw 
    285  1.29.4.2  nathanw 	return (0);
    286  1.29.4.2  nathanw }
    287  1.29.4.2  nathanw 
    288  1.29.4.2  nathanw void
    289  1.29.4.2  nathanw ebus_destroy_attach_args(ea)
    290  1.29.4.2  nathanw 	struct ebus_attach_args	*ea;
    291  1.29.4.2  nathanw {
    292  1.29.4.2  nathanw 
    293  1.29.4.2  nathanw 	if (ea->ea_name)
    294  1.29.4.2  nathanw 		free((void *)ea->ea_name, M_DEVBUF);
    295  1.29.4.2  nathanw 	if (ea->ea_reg)
    296  1.29.4.2  nathanw 		free((void *)ea->ea_reg, M_DEVBUF);
    297  1.29.4.2  nathanw 	if (ea->ea_intr)
    298  1.29.4.2  nathanw 		free((void *)ea->ea_intr, M_DEVBUF);
    299  1.29.4.2  nathanw 	if (ea->ea_vaddr)
    300  1.29.4.2  nathanw 		free((void *)ea->ea_vaddr, M_DEVBUF);
    301  1.29.4.2  nathanw }
    302  1.29.4.2  nathanw 
    303  1.29.4.2  nathanw int
    304  1.29.4.2  nathanw ebus_print(aux, p)
    305  1.29.4.2  nathanw 	void *aux;
    306  1.29.4.2  nathanw 	const char *p;
    307  1.29.4.2  nathanw {
    308  1.29.4.2  nathanw 	struct ebus_attach_args *ea = aux;
    309  1.29.4.2  nathanw 	int i;
    310  1.29.4.2  nathanw 
    311  1.29.4.2  nathanw 	if (p)
    312  1.29.4.2  nathanw 		printf("%s at %s", ea->ea_name, p);
    313  1.29.4.2  nathanw 	for (i = 0; i < ea->ea_nreg; i++)
    314  1.29.4.2  nathanw 		printf("%s %x-%x", i == 0 ? " addr" : ",",
    315  1.29.4.2  nathanw 		    ea->ea_reg[i].lo,
    316  1.29.4.2  nathanw 		    ea->ea_reg[i].lo + ea->ea_reg[i].size - 1);
    317  1.29.4.2  nathanw 	for (i = 0; i < ea->ea_nintr; i++)
    318  1.29.4.2  nathanw 		printf(" ipl %d", ea->ea_intr[i]);
    319  1.29.4.2  nathanw 	return (UNCONF);
    320  1.29.4.2  nathanw }
    321  1.29.4.2  nathanw 
    322  1.29.4.2  nathanw 
    323  1.29.4.2  nathanw /*
    324  1.29.4.2  nathanw  * find the INO values for each interrupt and fill them in.
    325  1.29.4.2  nathanw  *
    326  1.29.4.2  nathanw  * for each "reg" property of this device, mask it's hi and lo
    327  1.29.4.2  nathanw  * values with the "interrupt-map-mask"'s hi/lo values, and also
    328  1.29.4.2  nathanw  * mask the interrupt number with the interrupt mask.  search the
    329  1.29.4.2  nathanw  * "interrupt-map" list for matching values of hi, lo and interrupt
    330  1.29.4.2  nathanw  * to give the INO for this interrupt.
    331  1.29.4.2  nathanw  */
    332  1.29.4.2  nathanw void
    333  1.29.4.2  nathanw ebus_find_ino(sc, ea)
    334  1.29.4.2  nathanw 	struct ebus_softc *sc;
    335  1.29.4.2  nathanw 	struct ebus_attach_args *ea;
    336  1.29.4.2  nathanw {
    337  1.29.4.2  nathanw 	u_int32_t hi, lo, intr;
    338  1.29.4.2  nathanw 	int i, j, k;
    339  1.29.4.2  nathanw 
    340  1.29.4.2  nathanw 	if (sc->sc_nintmap == 0) {
    341  1.29.4.2  nathanw 		for (i = 0; i < ea->ea_nintr; i++) {
    342  1.29.4.2  nathanw 			OF_mapintr(ea->ea_node, &ea->ea_intr[i],
    343  1.29.4.2  nathanw 				sizeof(ea->ea_intr[0]),
    344  1.29.4.2  nathanw 				sizeof(ea->ea_intr[0]));
    345  1.29.4.2  nathanw 		}
    346  1.29.4.2  nathanw 		return;
    347  1.29.4.2  nathanw 	}
    348  1.29.4.2  nathanw 
    349  1.29.4.2  nathanw 	DPRINTF(EDB_INTRMAP,
    350  1.29.4.2  nathanw 	    ("ebus_find_ino: searching %d interrupts", ea->ea_nintr));
    351  1.29.4.2  nathanw 
    352  1.29.4.2  nathanw 	for (j = 0; j < ea->ea_nintr; j++) {
    353  1.29.4.2  nathanw 
    354  1.29.4.2  nathanw 		intr = ea->ea_intr[j] & sc->sc_intmapmask.intr;
    355  1.29.4.2  nathanw 
    356  1.29.4.2  nathanw 		DPRINTF(EDB_INTRMAP,
    357  1.29.4.2  nathanw 		    ("; intr %x masked to %x", ea->ea_intr[j], intr));
    358  1.29.4.2  nathanw 		for (i = 0; i < ea->ea_nreg; i++) {
    359  1.29.4.2  nathanw 			hi = ea->ea_reg[i].hi & sc->sc_intmapmask.hi;
    360  1.29.4.2  nathanw 			lo = ea->ea_reg[i].lo & sc->sc_intmapmask.lo;
    361  1.29.4.2  nathanw 
    362  1.29.4.2  nathanw 			DPRINTF(EDB_INTRMAP,
    363  1.29.4.2  nathanw 			    ("; reg hi.lo %08x.%08x masked to %08x.%08x",
    364  1.29.4.2  nathanw 			    ea->ea_reg[i].hi, ea->ea_reg[i].lo, hi, lo));
    365  1.29.4.2  nathanw 			for (k = 0; k < sc->sc_nintmap; k++) {
    366  1.29.4.2  nathanw 				DPRINTF(EDB_INTRMAP,
    367  1.29.4.2  nathanw 				    ("; checking hi.lo %08x.%08x intr %x",
    368  1.29.4.2  nathanw 				    sc->sc_intmap[k].hi, sc->sc_intmap[k].lo,
    369  1.29.4.2  nathanw 				    sc->sc_intmap[k].intr));
    370  1.29.4.2  nathanw 				if (hi == sc->sc_intmap[k].hi &&
    371  1.29.4.2  nathanw 				    lo == sc->sc_intmap[k].lo &&
    372  1.29.4.2  nathanw 				    intr == sc->sc_intmap[k].intr) {
    373  1.29.4.2  nathanw 					ea->ea_intr[j] =
    374  1.29.4.2  nathanw 					    sc->sc_intmap[k].cintr;
    375  1.29.4.2  nathanw 					DPRINTF(EDB_INTRMAP,
    376  1.29.4.2  nathanw 					    ("; FOUND IT! changing to %d\n",
    377  1.29.4.2  nathanw 					    sc->sc_intmap[k].cintr));
    378  1.29.4.2  nathanw 					goto next_intr;
    379  1.29.4.2  nathanw 				}
    380  1.29.4.2  nathanw 			}
    381  1.29.4.2  nathanw 		}
    382  1.29.4.2  nathanw next_intr:;
    383  1.29.4.2  nathanw 	}
    384  1.29.4.2  nathanw }
    385  1.29.4.2  nathanw 
    386  1.29.4.2  nathanw /*
    387  1.29.4.2  nathanw  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion
    388  1.29.4.2  nathanw  * about PCI physical addresses, which also applies to ebus.
    389  1.29.4.2  nathanw  */
    390  1.29.4.2  nathanw bus_space_tag_t
    391  1.29.4.2  nathanw ebus_alloc_bus_tag(sc, type)
    392  1.29.4.2  nathanw 	struct ebus_softc *sc;
    393  1.29.4.2  nathanw 	int type;
    394  1.29.4.2  nathanw {
    395  1.29.4.2  nathanw 	bus_space_tag_t bt;
    396  1.29.4.2  nathanw 
    397  1.29.4.2  nathanw 	bt = (bus_space_tag_t)
    398  1.29.4.2  nathanw 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    399  1.29.4.2  nathanw 	if (bt == NULL)
    400  1.29.4.2  nathanw 		panic("could not allocate ebus bus tag");
    401  1.29.4.2  nathanw 
    402  1.29.4.2  nathanw 	bzero(bt, sizeof *bt);
    403  1.29.4.2  nathanw 	bt->cookie = sc;
    404  1.29.4.2  nathanw 	bt->parent = sc->sc_memtag;
    405  1.29.4.2  nathanw 	bt->type = type;
    406  1.29.4.2  nathanw 	bt->sparc_bus_map = _ebus_bus_map;
    407  1.29.4.2  nathanw 	bt->sparc_bus_mmap = ebus_bus_mmap;
    408  1.29.4.2  nathanw 	bt->sparc_intr_establish = ebus_intr_establish;
    409  1.29.4.2  nathanw 	return (bt);
    410  1.29.4.2  nathanw }
    411  1.29.4.2  nathanw 
    412  1.29.4.2  nathanw static int
    413  1.29.4.2  nathanw _ebus_bus_map(t, ba, size, flags, va, hp)
    414  1.29.4.2  nathanw 	bus_space_tag_t t;
    415  1.29.4.2  nathanw 	bus_addr_t ba;
    416  1.29.4.2  nathanw 	bus_size_t size;
    417  1.29.4.2  nathanw 	int	flags;
    418  1.29.4.2  nathanw 	vaddr_t va;
    419  1.29.4.2  nathanw 	bus_space_handle_t *hp;
    420  1.29.4.2  nathanw {
    421  1.29.4.2  nathanw 	struct ebus_softc *sc = t->cookie;
    422  1.29.4.2  nathanw 	paddr_t offset;
    423  1.29.4.2  nathanw 	u_int bar;
    424  1.29.4.2  nathanw 	int i, ss;
    425  1.29.4.2  nathanw 
    426  1.29.4.2  nathanw 	bar = BUS_ADDR_IOSPACE(ba);
    427  1.29.4.2  nathanw 	offset = BUS_ADDR_PADDR(ba);
    428  1.29.4.2  nathanw 
    429  1.29.4.2  nathanw 	DPRINTF(EDB_BUSMAP,
    430  1.29.4.2  nathanw 		("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n",
    431  1.29.4.2  nathanw 		 (int)bar, (u_int32_t)offset, (u_int32_t)size,
    432  1.29.4.2  nathanw 		 flags, (void *)va));
    433  1.29.4.2  nathanw 
    434  1.29.4.2  nathanw 	for (i = 0; i < sc->sc_nrange; i++) {
    435  1.29.4.2  nathanw 		bus_addr_t pciaddr;
    436  1.29.4.2  nathanw 
    437  1.29.4.2  nathanw 		if (bar != sc->sc_range[i].child_hi)
    438  1.29.4.2  nathanw 			continue;
    439  1.29.4.2  nathanw 		if (offset < sc->sc_range[i].child_lo ||
    440  1.29.4.2  nathanw 		    (offset + size) >
    441  1.29.4.2  nathanw 		      (sc->sc_range[i].child_lo + sc->sc_range[i].size))
    442  1.29.4.2  nathanw 			continue;
    443  1.29.4.2  nathanw 
    444  1.29.4.2  nathanw 		/* Isolate address space and find the right tag */
    445  1.29.4.2  nathanw 		ss = (sc->sc_range[i].phys_hi>>24)&3;
    446  1.29.4.2  nathanw 		switch (ss) {
    447  1.29.4.2  nathanw 		case 1:	/* I/O space */
    448  1.29.4.2  nathanw 			t = sc->sc_iotag;
    449  1.29.4.2  nathanw 			break;
    450  1.29.4.2  nathanw 		case 2:	/* Memory space */
    451  1.29.4.2  nathanw 			t = sc->sc_memtag;
    452  1.29.4.2  nathanw 			break;
    453  1.29.4.2  nathanw 		case 0:	/* Config space */
    454  1.29.4.2  nathanw 		case 3:	/* 64-bit Memory space */
    455  1.29.4.2  nathanw 		default: /* WTF? */
    456  1.29.4.2  nathanw 			/* We don't handle these */
    457  1.29.4.2  nathanw 			panic("_ebus_bus_map: illegal space %x", ss);
    458  1.29.4.2  nathanw 			break;
    459  1.29.4.2  nathanw 		}
    460  1.29.4.2  nathanw 		pciaddr = ((bus_addr_t)sc->sc_range[i].phys_mid << 32UL) |
    461  1.29.4.2  nathanw 				       sc->sc_range[i].phys_lo;
    462  1.29.4.2  nathanw 		pciaddr += offset;
    463  1.29.4.2  nathanw 
    464  1.29.4.2  nathanw 		DPRINTF(EDB_BUSMAP,
    465  1.29.4.2  nathanw 			("_ebus_bus_map: mapping to PCI addr %x\n",
    466  1.29.4.2  nathanw 			 (u_int32_t)pciaddr));
    467  1.29.4.2  nathanw 
    468  1.29.4.2  nathanw 		/* pass it onto the psycho */
    469  1.29.4.2  nathanw 		return (bus_space_map(t, pciaddr, size, flags, hp));
    470  1.29.4.2  nathanw 	}
    471  1.29.4.2  nathanw 	DPRINTF(EDB_BUSMAP, (": FAILED\n"));
    472  1.29.4.2  nathanw 	return (EINVAL);
    473  1.29.4.2  nathanw }
    474  1.29.4.2  nathanw 
    475  1.29.4.2  nathanw static paddr_t
    476  1.29.4.2  nathanw ebus_bus_mmap(t, paddr, off, prot, flags)
    477  1.29.4.2  nathanw 	bus_space_tag_t t;
    478  1.29.4.2  nathanw 	bus_addr_t paddr;
    479  1.29.4.2  nathanw 	off_t off;
    480  1.29.4.2  nathanw 	int prot;
    481  1.29.4.2  nathanw 	int flags;
    482  1.29.4.2  nathanw {
    483  1.29.4.2  nathanw 	bus_addr_t offset = paddr;
    484  1.29.4.2  nathanw 	struct ebus_softc *sc = t->cookie;
    485  1.29.4.2  nathanw 	int i;
    486  1.29.4.2  nathanw 
    487  1.29.4.2  nathanw 	for (i = 0; i < sc->sc_nrange; i++) {
    488  1.29.4.2  nathanw 		bus_addr_t paddr = ((bus_addr_t)sc->sc_range[i].child_hi << 32) |
    489  1.29.4.2  nathanw 		    sc->sc_range[i].child_lo;
    490  1.29.4.2  nathanw 
    491  1.29.4.2  nathanw 		if (offset != paddr)
    492  1.29.4.2  nathanw 			continue;
    493  1.29.4.2  nathanw 
    494  1.29.4.2  nathanw 		DPRINTF(EDB_BUSMAP, ("\n_ebus_bus_mmap: mapping paddr %qx\n",
    495  1.29.4.2  nathanw 		    (unsigned long long)paddr));
    496  1.29.4.2  nathanw 		return (bus_space_mmap(sc->sc_memtag, paddr, off,
    497  1.29.4.2  nathanw 				       prot, flags));
    498  1.29.4.2  nathanw 	}
    499  1.29.4.2  nathanw 
    500  1.29.4.2  nathanw 	return (-1);
    501  1.29.4.2  nathanw }
    502  1.29.4.2  nathanw 
    503  1.29.4.2  nathanw /*
    504  1.29.4.2  nathanw  * install an interrupt handler for a ebus device
    505  1.29.4.2  nathanw  */
    506  1.29.4.2  nathanw void *
    507  1.29.4.2  nathanw ebus_intr_establish(t, pri, level, flags, handler, arg)
    508  1.29.4.2  nathanw 	bus_space_tag_t t;
    509  1.29.4.2  nathanw 	int pri;
    510  1.29.4.2  nathanw 	int level;
    511  1.29.4.2  nathanw 	int flags;
    512  1.29.4.2  nathanw 	int (*handler) __P((void *));
    513  1.29.4.2  nathanw 	void *arg;
    514  1.29.4.2  nathanw {
    515  1.29.4.2  nathanw 
    516  1.29.4.2  nathanw 	return (bus_intr_establish(t->parent, pri, level, flags, handler, arg));
    517  1.29.4.2  nathanw }
    518