Home | History | Annotate | Line # | Download | only in dev
ebus.c revision 1.11
      1  1.11  thorpej /*	$NetBSD: ebus.c,v 1.11 2003/01/01 02:20:47 thorpej Exp $ */
      2   1.1      uwe 
      3   1.1      uwe /*
      4   1.1      uwe  * Copyright (c) 1999, 2000 Matthew R. Green
      5   1.1      uwe  * All rights reserved.
      6   1.1      uwe  *
      7   1.1      uwe  * Redistribution and use in source and binary forms, with or without
      8   1.1      uwe  * modification, are permitted provided that the following conditions
      9   1.1      uwe  * are met:
     10   1.1      uwe  * 1. Redistributions of source code must retain the above copyright
     11   1.1      uwe  *    notice, this list of conditions and the following disclaimer.
     12   1.1      uwe  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      uwe  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      uwe  *    documentation and/or other materials provided with the distribution.
     15   1.1      uwe  * 3. The name of the author may not be used to endorse or promote products
     16   1.1      uwe  *    derived from this software without specific prior written permission.
     17   1.1      uwe  *
     18   1.1      uwe  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1      uwe  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1      uwe  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1      uwe  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1      uwe  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23   1.1      uwe  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24   1.1      uwe  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25   1.1      uwe  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26   1.1      uwe  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1      uwe  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1      uwe  * SUCH DAMAGE.
     29   1.1      uwe  */
     30   1.1      uwe 
     31   1.1      uwe /*
     32   1.1      uwe  * EBus support for PCI based SPARC systems (ms-IIep, Ultra).
     33   1.1      uwe  * EBus is documented in PCIO manual (Sun Part#: 802-7837-01).
     34   1.1      uwe  */
     35   1.1      uwe 
     36   1.3      uwe #if defined(DEBUG) && !defined(EBUS_DEBUG)
     37   1.3      uwe #define EBUS_DEBUG
     38   1.3      uwe #endif
     39   1.1      uwe 
     40   1.3      uwe #ifdef EBUS_DEBUG
     41   1.1      uwe #define	EDB_PROM	0x01
     42   1.1      uwe #define EDB_CHILD	0x02
     43   1.1      uwe #define	EDB_INTRMAP	0x04
     44   1.1      uwe #define EDB_BUSMAP	0x08
     45   1.1      uwe #define EDB_BUSDMA	0x10
     46   1.1      uwe #define EDB_INTR	0x20
     47   1.1      uwe int ebus_debug = 0;
     48   1.1      uwe #define DPRINTF(l, s)   do { if (ebus_debug & l) printf s; } while (0)
     49   1.1      uwe #else
     50   1.1      uwe #define DPRINTF(l, s)
     51   1.1      uwe #endif
     52   1.1      uwe 
     53   1.1      uwe #include <sys/param.h>
     54   1.1      uwe #include <sys/conf.h>
     55   1.1      uwe #include <sys/device.h>
     56   1.1      uwe #include <sys/errno.h>
     57   1.1      uwe #include <sys/extent.h>
     58   1.1      uwe #include <sys/malloc.h>
     59   1.1      uwe #include <sys/systm.h>
     60   1.1      uwe #include <sys/time.h>
     61   1.1      uwe 
     62   1.1      uwe #define _SPARC_BUS_DMA_PRIVATE
     63   1.1      uwe #include <machine/bus.h>
     64   1.1      uwe #include <machine/autoconf.h>
     65   1.1      uwe #include <machine/openfirm.h>
     66   1.1      uwe 
     67   1.1      uwe #include <dev/pci/pcivar.h>
     68   1.1      uwe #include <dev/pci/pcireg.h>
     69   1.1      uwe #include <dev/pci/pcidevs.h>
     70   1.1      uwe 
     71   1.1      uwe #include <dev/ofw/ofw_pci.h>
     72   1.1      uwe 
     73   1.4      uwe #include <dev/ebus/ebusreg.h>
     74   1.4      uwe #include <dev/ebus/ebusvar.h>
     75   1.1      uwe 
     76   1.1      uwe 
     77   1.4      uwe struct ebus_softc {
     78   1.4      uwe 	struct device			sc_dev;
     79   1.4      uwe 	struct device			*sc_parent;	/* PCI bus */
     80   1.4      uwe 
     81   1.4      uwe 	int				sc_node;	/* PROM node */
     82   1.4      uwe 
     83   1.4      uwe 	bus_space_tag_t			sc_bustag;	/* mem tag from pci */
     84   1.4      uwe 	bus_dma_tag_t			sc_dmatag;	/* XXX */
     85   1.4      uwe 
     86   1.4      uwe 	bus_space_tag_t			sc_childbustag;	/* EBus tag */
     87   1.4      uwe 
     88   1.4      uwe 	/*
     89   1.4      uwe 	 * "reg" contains exactly the info we'd get by processing
     90   1.4      uwe 	 * "ranges", so don't bother with "ranges" and use "reg" directly.
     91   1.4      uwe 	 */
     92   1.4      uwe 	struct ofw_pci_register		*sc_reg;
     93   1.4      uwe 	int				sc_nreg;
     94   1.4      uwe };
     95   1.4      uwe 
     96   1.1      uwe int	ebus_match(struct device *, struct cfdata *, void *);
     97   1.1      uwe void	ebus_attach(struct device *, struct device *, void *);
     98   1.1      uwe 
     99   1.8  thorpej CFATTACH_DECL(ebus, sizeof(struct ebus_softc),
    100   1.9  thorpej     ebus_match, ebus_attach, NULL, NULL);
    101   1.1      uwe 
    102   1.1      uwe int	ebus_setup_attach_args(struct ebus_softc *, int,
    103   1.1      uwe 			       struct ebus_attach_args *);
    104   1.1      uwe void	ebus_destroy_attach_args(struct ebus_attach_args *);
    105   1.1      uwe int	ebus_print(void *, const char *);
    106   1.1      uwe 
    107   1.1      uwe /*
    108   1.1      uwe  * here are our bus space and bus dma routines.
    109   1.1      uwe  */
    110   1.1      uwe static paddr_t	ebus_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
    111   1.5       pk static int	_ebus_bus_map(bus_space_tag_t, bus_addr_t,
    112   1.1      uwe 			      bus_size_t, int, vaddr_t, bus_space_handle_t *);
    113  1.10       pk static void	*ebus_intr_establish(bus_space_tag_t, int, int,
    114  1.10       pk 				     int (*)(void *), void *, void (*)(void));
    115   1.1      uwe 
    116   1.4      uwe static bus_space_tag_t	ebus_alloc_bus_tag(struct ebus_softc *);
    117   1.4      uwe static bus_dma_tag_t	ebus_alloc_dma_tag(struct ebus_softc *, bus_dma_tag_t);
    118   1.4      uwe 
    119   1.4      uwe 
    120   1.1      uwe /*
    121   1.1      uwe  * Working around PROM bogosity.
    122   1.1      uwe  *
    123   1.1      uwe  * EBus doesn't have official OFW binding.  sparc64 has a de-facto
    124   1.1      uwe  * standard but patching it in in prompatch.c and then decoding it
    125   1.1      uwe  * here would be an overkill for ms-IIep.
    126   1.1      uwe  *
    127   1.1      uwe  * So we assume that all ms-IIep based systems use PCIO chip only in
    128   1.1      uwe  * "motherboard mode" with interrupt lines wired directly to ms-IIep
    129   1.1      uwe  * interrupt inputs.
    130   1.1      uwe  *
    131   1.1      uwe  * Note that this is ineligible for prompatch.c, as we are not
    132   1.1      uwe  * correcting PROM to conform to some established standard, this hack
    133   1.1      uwe  * is tied to this version of ebus driver and as such it's better stay
    134   1.1      uwe  * private to the driver.
    135   1.1      uwe  */
    136   1.1      uwe 
    137   1.1      uwe struct msiiep_ebus_intr_wiring {
    138   1.1      uwe 	const char *name;	/* PROM node */
    139   1.1      uwe 	int line;		/* ms-IIep interrupt input */
    140   1.1      uwe };
    141   1.1      uwe 
    142   1.1      uwe static struct msiiep_ebus_intr_wiring krups_ebus_intr_wiring[] = {
    143   1.1      uwe 	{ "su", 0 }, { "8042", 0 }, { "sound", 3 }
    144   1.1      uwe };
    145   1.1      uwe 
    146   1.1      uwe 
    147   1.1      uwe struct msiiep_known_ebus_wiring {
    148   1.1      uwe 	const char *model;
    149   1.1      uwe 	struct msiiep_ebus_intr_wiring *map;
    150   1.1      uwe 	int mapsize;
    151   1.1      uwe };
    152   1.1      uwe 
    153   1.1      uwe #define MSIIEP_MODEL_WIRING(name, map) \
    154   1.1      uwe 	{ name, map, sizeof(map)/sizeof(map[0]) }
    155   1.1      uwe 
    156   1.1      uwe static struct msiiep_known_ebus_wiring known_models[] = {
    157   1.1      uwe 	MSIIEP_MODEL_WIRING("SUNW,501-4267", krups_ebus_intr_wiring),
    158   1.1      uwe 	{ NULL, NULL, 0}
    159   1.1      uwe };
    160   1.1      uwe 
    161   1.1      uwe 
    162   1.1      uwe /*
    163   1.1      uwe  * XXX: This assumes single EBus.  However I don't think any ms-IIep
    164   1.1      uwe  * system ever used more than one.  In any case, without looking at a
    165   1.1      uwe  * system with multiple PCIO chips I don't know how to correctly
    166   1.1      uwe  * program the driver to handle PROM glitches in them, so for the time
    167   1.1      uwe  * being just use globals.
    168   1.1      uwe  */
    169   1.1      uwe static struct msiiep_ebus_intr_wiring *wiring_map;
    170   1.1      uwe static int wiring_map_size;
    171   1.1      uwe 
    172   1.1      uwe static int ebus_init_wiring_table(struct ebus_softc *);
    173   1.1      uwe 
    174   1.1      uwe 
    175   1.1      uwe int
    176   1.1      uwe ebus_match(parent, match, aux)
    177   1.1      uwe 	struct device *parent;
    178   1.1      uwe 	struct cfdata *match;
    179   1.1      uwe 	void *aux;
    180   1.1      uwe {
    181   1.1      uwe 	struct pci_attach_args *pa = aux;
    182   1.1      uwe 	char name[10];
    183   1.1      uwe 	int node;
    184   1.1      uwe 
    185   1.1      uwe 	/* Only attach if there's a PROM node. */
    186   1.1      uwe 	node = PCITAG_NODE(pa->pa_tag);
    187   1.1      uwe 	if (node == -1)
    188   1.1      uwe 		return (0);
    189   1.1      uwe 
    190   1.1      uwe 	PROM_getpropstringA(node, "name", name, sizeof name);
    191   1.1      uwe 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_BRIDGE
    192   1.1      uwe 	    && PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN
    193   1.1      uwe 	    && PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_EBUS
    194   1.1      uwe 	    && strcmp(name, "ebus") == 0)
    195   1.1      uwe 		return (1);
    196   1.1      uwe 
    197   1.1      uwe 	return (0);
    198   1.1      uwe }
    199   1.1      uwe 
    200   1.1      uwe 
    201   1.1      uwe static int
    202   1.1      uwe ebus_init_wiring_table(sc)
    203   1.1      uwe 	struct ebus_softc *sc;
    204   1.1      uwe {
    205   1.1      uwe 	struct msiiep_known_ebus_wiring *p;
    206   1.1      uwe 	char buf[32];
    207   1.1      uwe 	char *model;
    208   1.1      uwe 
    209   1.1      uwe 	if (wiring_map != NULL) {
    210   1.1      uwe 		printf("%s: global ebus wiring map already initalized\n",
    211   1.1      uwe 		       sc->sc_dev.dv_xname);
    212   1.1      uwe 		return (0);
    213   1.1      uwe 	}
    214   1.1      uwe 
    215   1.1      uwe 	model = PROM_getpropstringA(prom_findroot(), "model",
    216   1.1      uwe 				    buf, sizeof(buf));
    217   1.1      uwe 	if (model == NULL)
    218   1.1      uwe 		panic("ebus_init_wiring_table: no \"model\" property");
    219   1.1      uwe 
    220   1.1      uwe 	for (p = known_models; p->model != NULL; ++p)
    221   1.1      uwe 		if (strcmp(model, p->model) == 0) {
    222   1.1      uwe 			wiring_map = p->map;
    223   1.1      uwe 			wiring_map_size = p->mapsize;
    224   1.1      uwe 			return (1);
    225   1.1      uwe 		}
    226   1.1      uwe 
    227   1.1      uwe 	/* not found?  we should have failed in pci_attach_hook then. */
    228   1.1      uwe 	panic("ebus_init_wiring_table: unknown model %s", model);
    229   1.1      uwe }
    230   1.1      uwe 
    231   1.1      uwe 
    232   1.1      uwe /*
    233   1.1      uwe  * attach an ebus and all it's children.  this code is modeled
    234   1.1      uwe  * after the sbus code which does similar things.
    235   1.1      uwe  */
    236   1.1      uwe void
    237   1.1      uwe ebus_attach(parent, self, aux)
    238   1.1      uwe 	struct device *parent, *self;
    239   1.1      uwe 	void *aux;
    240   1.1      uwe {
    241   1.1      uwe 	struct ebus_softc *sc = (struct ebus_softc *)self;
    242   1.1      uwe 	struct pci_attach_args *pa = aux;
    243   1.1      uwe 	struct ebus_attach_args ea;
    244   1.1      uwe 	int node, error;
    245   1.1      uwe 	char devinfo[256];
    246   1.1      uwe 
    247   1.1      uwe 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    248   1.2      uwe 	printf(": %s, revision 0x%02x\n",
    249   1.2      uwe 	       devinfo, PCI_REVISION(pa->pa_class));
    250   1.1      uwe 
    251   1.1      uwe 	node = PCITAG_NODE(pa->pa_tag);
    252   1.1      uwe 	if (node == -1)
    253   1.1      uwe 		panic("%s: unable to find ebus node", self->dv_xname);
    254   1.1      uwe 
    255   1.1      uwe 	if (ebus_init_wiring_table(sc) == 0)
    256   1.1      uwe 		return;
    257   1.1      uwe 
    258   1.1      uwe 	sc->sc_node = node;
    259   1.1      uwe 	sc->sc_parent = parent;	/* XXX: unused so far */
    260   1.1      uwe 	sc->sc_bustag = pa->pa_memt; /* EBus only does PCI MEM32 space */
    261   1.1      uwe 	sc->sc_childbustag = ebus_alloc_bus_tag(sc);
    262   1.1      uwe 	sc->sc_dmatag = ebus_alloc_dma_tag(sc, pa->pa_dmat);
    263   1.1      uwe 
    264   1.1      uwe 	/*
    265   1.1      uwe 	 * Setup ranges.  The interesting thing is that we use "reg"
    266   1.1      uwe 	 * not "ranges", since "reg" on ebus has exactly the data we'd
    267   1.1      uwe 	 * get by processing "ranges".
    268   1.1      uwe 	 *
    269   1.1      uwe 	 */
    270   1.1      uwe 	error = PROM_getprop(node, "reg", sizeof(struct ofw_pci_register),
    271   1.1      uwe 			     &sc->sc_nreg, (void **)&sc->sc_reg);
    272   1.1      uwe 	if (error)
    273   1.1      uwe 		panic("%s: unable to read ebus registers (error %d)",
    274   1.1      uwe 		      self->dv_xname, error);
    275   1.1      uwe 
    276   1.1      uwe 	/*
    277   1.1      uwe 	 * now attach all our children
    278   1.1      uwe 	 */
    279   1.1      uwe 	DPRINTF(EDB_CHILD, ("ebus node %08x, searching children...\n", node));
    280   1.1      uwe 	for (node = firstchild(node); node; node = nextsibling(node)) {
    281   1.1      uwe 		char *name = PROM_getpropstring(node, "name");
    282   1.1      uwe 
    283   1.1      uwe 		if (ebus_setup_attach_args(sc, node, &ea) != 0) {
    284   1.1      uwe 			printf("ebus_attach: %s: incomplete\n", name);
    285   1.1      uwe 			continue;
    286   1.1      uwe 		}
    287   1.1      uwe 		DPRINTF(EDB_CHILD,
    288   1.1      uwe 			("- found child `%s', attaching\n", ea.ea_name));
    289   1.1      uwe 		(void)config_found(self, &ea, ebus_print);
    290   1.1      uwe 		ebus_destroy_attach_args(&ea);
    291   1.1      uwe 	}
    292   1.1      uwe }
    293   1.1      uwe 
    294   1.1      uwe int
    295   1.1      uwe ebus_setup_attach_args(sc, node, ea)
    296   1.1      uwe 	struct ebus_softc *sc;
    297   1.1      uwe 	int node;
    298   1.1      uwe 	struct ebus_attach_args	*ea;
    299   1.1      uwe {
    300   1.1      uwe 	int n, err;
    301   1.1      uwe 
    302   1.1      uwe 	memset(ea, 0, sizeof(struct ebus_attach_args));
    303   1.1      uwe 
    304   1.1      uwe 	err = PROM_getprop(node, "name", 1, &n, (void **)&ea->ea_name);
    305   1.1      uwe 	if (err != 0)
    306   1.1      uwe 		return (err);
    307   1.1      uwe 	ea->ea_name[n] = '\0';
    308   1.1      uwe 
    309   1.1      uwe 	ea->ea_node = node;
    310   1.1      uwe 	ea->ea_bustag = sc->sc_childbustag;
    311   1.1      uwe 	ea->ea_dmatag = sc->sc_dmatag;
    312   1.1      uwe 
    313   1.4      uwe 	err = PROM_getprop(node, "reg", sizeof(struct ebus_regs),
    314   1.1      uwe 			   &ea->ea_nreg, (void **)&ea->ea_reg);
    315   1.1      uwe 	if (err != 0)
    316   1.1      uwe 		return (err);
    317   1.1      uwe 
    318   1.4      uwe 	/*
    319   1.4      uwe 	 * On Ultra the bar is the _offset_ of the BAR in PCI config
    320   1.4      uwe 	 * space but in (some?) ms-IIep systems (e.g. Krups) it's the
    321   1.4      uwe 	 * _number_ of the BAR - e.g. BAR1 is represented by 1 in
    322   1.4      uwe 	 * Krups PROM, while on Ultra it's 0x14.  Fix it here.
    323   1.4      uwe 	 */
    324   1.4      uwe 	for (n = 0; n < ea->ea_nreg; ++n)
    325   1.4      uwe 	    if (ea->ea_reg[n].hi < PCI_MAPREG_START) {
    326   1.4      uwe 		ea->ea_reg[n].hi = PCI_MAPREG_START
    327   1.4      uwe 		    + ea->ea_reg[n].hi * sizeof(pcireg_t);
    328   1.4      uwe 	    }
    329   1.4      uwe 
    330   1.4      uwe 
    331   1.1      uwe 	err = PROM_getprop(node, "address", sizeof(u_int32_t),
    332   1.1      uwe 			   &ea->ea_nvaddr, (void **)&ea->ea_vaddr);
    333   1.1      uwe 	if (err != ENOENT) {
    334   1.1      uwe 		if (err != 0)
    335   1.1      uwe 			return (err);
    336   1.1      uwe 
    337   1.1      uwe 		if (ea->ea_nreg != ea->ea_nvaddr)
    338   1.1      uwe 			printf("ebus loses: device %s: %d regs and %d addrs\n",
    339   1.1      uwe 			       ea->ea_name, ea->ea_nreg, ea->ea_nvaddr);
    340   1.1      uwe 	} else
    341   1.1      uwe 		ea->ea_nvaddr = 0;
    342   1.1      uwe 
    343   1.1      uwe 	/* XXX: "interrupts" hack */
    344   1.1      uwe 	for (n = 0; n < wiring_map_size; ++n) {
    345   1.1      uwe 		struct msiiep_ebus_intr_wiring *w = &wiring_map[n];
    346   1.1      uwe 		if (strcmp(w->name, ea->ea_name) == 0) {
    347   1.1      uwe 			ea->ea_intr = malloc(sizeof(u_int32_t),
    348   1.1      uwe 					     M_DEVBUF, M_NOWAIT);
    349   1.1      uwe 			ea->ea_intr[0] = w->line;
    350   1.1      uwe 			ea->ea_nintr = 1;
    351   1.1      uwe 			break;
    352   1.1      uwe 		}
    353   1.1      uwe 	}
    354   1.1      uwe 
    355   1.1      uwe 	return (0);
    356   1.1      uwe }
    357   1.1      uwe 
    358   1.1      uwe void
    359   1.1      uwe ebus_destroy_attach_args(ea)
    360   1.1      uwe 	struct ebus_attach_args	*ea;
    361   1.1      uwe {
    362   1.1      uwe 
    363   1.1      uwe 	if (ea->ea_name)
    364   1.1      uwe 		free((void *)ea->ea_name, M_DEVBUF);
    365   1.1      uwe 	if (ea->ea_reg)
    366   1.1      uwe 		free((void *)ea->ea_reg, M_DEVBUF);
    367   1.1      uwe 	if (ea->ea_intr)
    368   1.1      uwe 		free((void *)ea->ea_intr, M_DEVBUF);
    369   1.1      uwe 	if (ea->ea_vaddr)
    370   1.1      uwe 		free((void *)ea->ea_vaddr, M_DEVBUF);
    371   1.1      uwe }
    372   1.1      uwe 
    373   1.1      uwe int
    374   1.1      uwe ebus_print(aux, p)
    375   1.1      uwe 	void *aux;
    376   1.1      uwe 	const char *p;
    377   1.1      uwe {
    378   1.1      uwe 	struct ebus_attach_args *ea = aux;
    379   1.1      uwe 	int i;
    380   1.1      uwe 
    381   1.1      uwe 	if (p)
    382  1.11  thorpej 		aprint_normal("%s at %s", ea->ea_name, p);
    383   1.1      uwe 	for (i = 0; i < ea->ea_nreg; ++i)
    384  1.11  thorpej 		aprint_normal("%s bar %x offset 0x%x", i == 0 ? "" : ",",
    385   1.4      uwe 		       ea->ea_reg[i].hi, ea->ea_reg[i].lo);
    386   1.1      uwe 	for (i = 0; i < ea->ea_nintr; ++i)
    387  1.11  thorpej 		aprint_normal(" line %d", ea->ea_intr[i]);
    388   1.1      uwe 	return (UNCONF);
    389   1.1      uwe }
    390   1.1      uwe 
    391   1.1      uwe 
    392   1.1      uwe /*
    393   1.1      uwe  * bus space and bus dma methods below here
    394   1.1      uwe  */
    395   1.1      uwe 
    396   1.1      uwe bus_space_tag_t
    397   1.1      uwe ebus_alloc_bus_tag(sc)
    398   1.1      uwe 	struct ebus_softc *sc;
    399   1.1      uwe {
    400   1.1      uwe 	bus_space_tag_t bt;
    401   1.1      uwe 
    402   1.1      uwe 	bt = (bus_space_tag_t)
    403   1.1      uwe 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    404   1.1      uwe 	if (bt == NULL)
    405   1.3      uwe 		panic("unable to allocate ebus bus tag");
    406   1.1      uwe 
    407   1.1      uwe 	memset(bt, 0, sizeof *bt);
    408   1.1      uwe 	bt->cookie = sc;
    409   1.1      uwe 	bt->parent = sc->sc_bustag;
    410   1.1      uwe 	bt->sparc_bus_map = _ebus_bus_map;
    411   1.1      uwe 	bt->sparc_bus_mmap = ebus_bus_mmap;
    412   1.1      uwe 	bt->sparc_intr_establish = ebus_intr_establish;
    413   1.1      uwe 	return (bt);
    414   1.1      uwe }
    415   1.1      uwe 
    416   1.1      uwe 
    417   1.1      uwe bus_dma_tag_t
    418   1.1      uwe ebus_alloc_dma_tag(sc, pdt)
    419   1.1      uwe 	struct ebus_softc *sc;
    420   1.1      uwe 	bus_dma_tag_t pdt;
    421   1.1      uwe {
    422   1.1      uwe 	bus_dma_tag_t dt;
    423   1.1      uwe 
    424   1.1      uwe 	dt = (bus_dma_tag_t)
    425   1.1      uwe 		malloc(sizeof(struct sparc_bus_dma_tag), M_DEVBUF, M_NOWAIT);
    426   1.1      uwe 	if (dt == NULL)
    427   1.3      uwe 		panic("unable to allocate ebus dma tag");
    428   1.1      uwe 
    429   1.1      uwe 	memset(dt, 0, sizeof *dt);
    430   1.1      uwe 	dt->_cookie = sc;
    431   1.1      uwe #define PCOPY(x)	dt->x = pdt->x
    432   1.1      uwe 	PCOPY(_dmamap_create);
    433   1.1      uwe 	PCOPY(_dmamap_destroy);
    434   1.1      uwe 	PCOPY(_dmamap_load);
    435   1.1      uwe 	PCOPY(_dmamap_load_mbuf);
    436   1.1      uwe 	PCOPY(_dmamap_load_uio);
    437   1.1      uwe 	PCOPY(_dmamap_load_raw);
    438   1.1      uwe 	PCOPY(_dmamap_unload);
    439   1.1      uwe 	PCOPY(_dmamap_sync);
    440   1.1      uwe 	PCOPY(_dmamem_alloc);
    441   1.1      uwe 	PCOPY(_dmamem_free);
    442   1.1      uwe 	PCOPY(_dmamem_map);
    443   1.1      uwe 	PCOPY(_dmamem_unmap);
    444   1.1      uwe 	PCOPY(_dmamem_mmap);
    445   1.1      uwe #undef	PCOPY
    446   1.1      uwe 	return (dt);
    447   1.1      uwe }
    448   1.1      uwe 
    449   1.1      uwe /*
    450   1.1      uwe  * bus space support.  <sparc64/dev/psychoreg.h> has a discussion
    451   1.1      uwe  * about PCI physical addresses, which also applies to ebus.
    452   1.1      uwe  */
    453   1.1      uwe static int
    454   1.5       pk _ebus_bus_map(t, ba, size, flags, va, hp)
    455   1.1      uwe 	bus_space_tag_t t;
    456   1.5       pk 	bus_addr_t ba;	/* encodes bar/offset */
    457   1.1      uwe 	bus_size_t size;
    458   1.1      uwe 	int	flags;
    459   1.5       pk 	vaddr_t va;
    460   1.1      uwe 	bus_space_handle_t *hp;
    461   1.1      uwe {
    462   1.1      uwe 	struct ebus_softc *sc = t->cookie;
    463   1.1      uwe 	u_int bar;
    464   1.1      uwe 	paddr_t offset;
    465   1.1      uwe 	int i;
    466   1.1      uwe 
    467   1.5       pk 	bar = BUS_ADDR_IOSPACE(ba);
    468   1.5       pk 	offset = BUS_ADDR_PADDR(ba);
    469   1.1      uwe 
    470   1.1      uwe 	DPRINTF(EDB_BUSMAP,
    471   1.1      uwe 		("\n_ebus_bus_map: bar %d offset %08x sz %x flags %x va %p\n",
    472   1.1      uwe 		 (int)bar, (u_int32_t)offset, (u_int32_t)size,
    473   1.5       pk 		 flags, (void *)va));
    474   1.1      uwe 
    475   1.4      uwe 	/* EBus has only two BARs */
    476   1.4      uwe 	if (PCI_MAPREG_NUM(bar) > 1) {
    477   1.1      uwe 		DPRINTF(EDB_BUSMAP,
    478   1.1      uwe 			("\n_ebus_bus_map: impossible bar\n"));
    479   1.1      uwe 		return (EINVAL);
    480   1.1      uwe 	}
    481   1.1      uwe 
    482   1.1      uwe 	/*
    483   1.1      uwe 	 * Almost all of the interesting ebus children are mapped by
    484   1.1      uwe 	 * BAR1, the last entry in sc_reg[], so work our way backwards.
    485   1.1      uwe 	 */
    486   1.1      uwe 	for (i = sc->sc_nreg - 1; i >= 0; --i) {
    487   1.1      uwe 		bus_addr_t pciaddr;
    488   1.1      uwe 		u_int32_t ss;
    489   1.1      uwe 
    490   1.1      uwe 		/* EBus only does MEM32 */
    491   1.1      uwe 		ss  = sc->sc_reg[i].phys_hi & OFW_PCI_PHYS_HI_SPACEMASK;
    492   1.1      uwe 		if (ss != OFW_PCI_PHYS_HI_SPACE_MEM32)
    493   1.1      uwe 			continue;
    494   1.1      uwe 
    495   1.1      uwe 		if (bar != (sc->sc_reg[i].phys_hi
    496   1.1      uwe 			    & OFW_PCI_PHYS_HI_REGISTERMASK))
    497   1.1      uwe 			continue;
    498   1.1      uwe 
    499   1.1      uwe 		pciaddr = (bus_addr_t)sc->sc_reg[i].phys_lo + offset;
    500   1.1      uwe 
    501   1.1      uwe 		if (pciaddr + size > sc->sc_reg[i].phys_lo
    502   1.1      uwe 					+ sc->sc_reg[i].size_lo)
    503   1.1      uwe 			continue;
    504   1.1      uwe 
    505   1.1      uwe 		DPRINTF(EDB_BUSMAP,
    506   1.1      uwe 			("_ebus_bus_map: mapping to PCI addr %x\n",
    507   1.1      uwe 			 (u_int32_t)pciaddr));
    508   1.1      uwe 
    509   1.1      uwe 		/* pass it onto the pci controller */
    510   1.6      uwe 		return (bus_space_map2(sc->sc_bustag, pciaddr, size,
    511   1.5       pk 				       flags, va, hp));
    512   1.1      uwe 	}
    513   1.1      uwe 
    514   1.1      uwe 	DPRINTF(EDB_BUSMAP, (": FAILED\n"));
    515   1.1      uwe 	return (EINVAL);
    516   1.1      uwe }
    517   1.1      uwe 
    518   1.1      uwe static paddr_t
    519   1.5       pk ebus_bus_mmap(t, ba, off, prot, flags)
    520   1.1      uwe 	bus_space_tag_t t;
    521   1.5       pk 	bus_addr_t ba;
    522   1.1      uwe 	off_t off;
    523   1.1      uwe 	int prot;
    524   1.1      uwe 	int flags;
    525   1.1      uwe {
    526   1.1      uwe 
    527   1.1      uwe 	/* XXX: not implemetned yet */
    528   1.1      uwe 	return (-1);
    529   1.1      uwe }
    530   1.1      uwe 
    531   1.1      uwe /*
    532   1.1      uwe  * Install an interrupt handler for a EBus device.
    533   1.1      uwe  */
    534   1.1      uwe void *
    535  1.10       pk ebus_intr_establish(t, pri, level, handler, arg, fastvec)
    536   1.1      uwe 	bus_space_tag_t t;
    537   1.1      uwe 	int pri;
    538   1.1      uwe 	int level;
    539   1.1      uwe 	int (*handler)(void *);
    540   1.1      uwe 	void *arg;
    541  1.10       pk 	void (*fastvec)(void);	/* ignored */
    542   1.1      uwe {
    543  1.10       pk 	return (bus_intr_establish(t->parent, pri, level, handler, arg));
    544   1.1      uwe }
    545