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