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