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