Home | History | Annotate | Line # | Download | only in dev
sbus.c revision 1.19
      1 /*	$NetBSD: sbus.c,v 1.19 1998/03/21 19:55:31 pk Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratory.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. All advertising materials mentioning features or use of this software
     25  *    must display the following acknowledgement:
     26  *	This product includes software developed by the University of
     27  *	California, Berkeley and its contributors.
     28  * 4. Neither the name of the University nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  *
     44  *	@(#)sbus.c	8.1 (Berkeley) 6/11/93
     45  */
     46 
     47 /*
     48  * Sbus stuff.
     49  */
     50 
     51 #include <sys/param.h>
     52 #include <sys/malloc.h>
     53 #include <sys/systm.h>
     54 #include <sys/device.h>
     55 #include <vm/vm.h>
     56 
     57 #include <machine/bus.h>
     58 #include <sparc/dev/sbusreg.h>
     59 #include <sparc/dev/sbusvar.h>
     60 
     61 #include <sparc/sparc/iommuvar.h>
     62 #include <machine/autoconf.h>
     63 
     64 
     65 void sbusreset __P((int));
     66 
     67 static bus_space_tag_t sbus_alloc_bustag __P((struct sbus_softc *));
     68 static int sbus_get_intr __P((struct sbus_softc *, int, int *));
     69 static int sbus_bus_mmap __P((void *, bus_type_t, bus_addr_t, int));
     70 
     71 
     72 /* autoconfiguration driver */
     73 int	sbus_match_mainbus __P((struct device *, struct cfdata *, void *));
     74 int	sbus_match_iommu __P((struct device *, struct cfdata *, void *));
     75 void	sbus_attach_mainbus __P((struct device *, struct device *, void *));
     76 void	sbus_attach_iommu __P((struct device *, struct device *, void *));
     77 
     78 struct cfattach sbus_mainbus_ca = {
     79 	sizeof(struct sbus_softc), sbus_match_mainbus, sbus_attach_mainbus
     80 };
     81 struct cfattach sbus_iommu_ca = {
     82 	sizeof(struct sbus_softc), sbus_match_iommu, sbus_attach_iommu
     83 };
     84 
     85 extern struct cfdriver sbus_cd;
     86 
     87 /* If the PROM does not provide the `ranges' property, we make up our own */
     88 struct rom_range sbus_translations[] = {
     89 	/* Assume a maximum of 4 Sbus slots, all mapped to on-board io space */
     90 	{ 0, 0, PMAP_OBIO, SBUS_ADDR(0,0), 1 << 25 },
     91 	{ 1, 0, PMAP_OBIO, SBUS_ADDR(1,0), 1 << 25 },
     92 	{ 2, 0, PMAP_OBIO, SBUS_ADDR(2,0), 1 << 25 },
     93 	{ 3, 0, PMAP_OBIO, SBUS_ADDR(3,0), 1 << 25 }
     94 };
     95 
     96 /*
     97  * Child devices receive the Sbus interrupt level in their attach
     98  * arguments. We translate these to CPU IPLs using the following
     99  * tables. Note: obio bus interrupt levels are identical to the
    100  * processor IPL.
    101  *
    102  * The second set of tables is used when the Sbus interrupt level
    103  * cannot be had from the PROM as an `interrupt' property. We then
    104  * fall back on the `intr' property which contains the CPU IPL.
    105  */
    106 
    107 /* Translate Sbus interrupt level to processor IPL */
    108 static int intr_sbus2ipl_4c[] = {
    109 	0, 1, 2, 3, 5, 7, 8, 9
    110 };
    111 static int intr_sbus2ipl_4m[] = {
    112 	0, 2, 3, 5, 7, 9, 11, 13
    113 };
    114 
    115 #if 0
    116 /* Table to translate `intr' property values to Sbus interrupt levels */
    117 static int intr2sbus_4c[] = {
    118 	0, 1, 2, 3, -1, 4, -1, 5, 6, 7, -1, -1, -1, -1, -1
    119 };
    120 static int intr2sbus_4m[] = {
    121 	0, -1, 1, 2, -1, 3, -1, 4, -1, 5, -1, 6, -1, 7, -1, -1
    122 };
    123 #endif
    124 
    125 #define SBUS_INTR_COMPAT	0x80000000
    126 
    127 
    128 /*
    129  * Print the location of some sbus-attached device (called just
    130  * before attaching that device).  If `sbus' is not NULL, the
    131  * device was found but not configured; print the sbus as well.
    132  * Return UNCONF (config_find ignores this if the device was configured).
    133  */
    134 int
    135 sbus_print(args, busname)
    136 	void *args;
    137 	const char *busname;
    138 {
    139 	struct sbus_attach_args *sa = args;
    140 
    141 	if (busname)
    142 		printf("%s at %s", sa->sa_name, busname);
    143 	printf(" slot %d offset 0x%x", sa->sa_slot, sa->sa_offset);
    144 	if (sa->sa_pri) {
    145 		int level = sa->sa_pri;
    146 		struct sbus_softc *sc =
    147 			(struct sbus_softc *) sa->sa_bustag->cookie;
    148 
    149 		printf(" level %d", level & ~SBUS_INTR_COMPAT);
    150 		if ((level & SBUS_INTR_COMPAT) == 0) {
    151 			int ipl = sc->sc_intr2ipl[level];
    152 			if (ipl != level)
    153 				printf(" (ipl %d)", ipl);
    154 		}
    155 	}
    156 	return (UNCONF);
    157 }
    158 
    159 int
    160 sbus_match_mainbus(parent, cf, aux)
    161 	struct device *parent;
    162 	struct cfdata *cf;
    163 	void *aux;
    164 {
    165 	struct mainbus_attach_args *ma = aux;
    166 
    167 	if (CPU_ISSUN4)
    168 		return (0);
    169 
    170 	return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
    171 }
    172 
    173 int
    174 sbus_match_iommu(parent, cf, aux)
    175 	struct device *parent;
    176 	struct cfdata *cf;
    177 	void *aux;
    178 {
    179 	struct iommu_attach_args *ia = aux;
    180 
    181 	if (CPU_ISSUN4)
    182 		return (0);
    183 
    184 	return (strcmp(cf->cf_driver->cd_name, ia->iom_name) == 0);
    185 }
    186 
    187 /*
    188  * Attach an Sbus.
    189  */
    190 void
    191 sbus_attach_mainbus(parent, self, aux)
    192 	struct device *parent;
    193 	struct device *self;
    194 	void *aux;
    195 {
    196 	struct sbus_softc *sc = (struct sbus_softc *)self;
    197 	struct mainbus_attach_args *ma = aux;
    198 	int node = ma->ma_node;
    199 
    200 	/*
    201 	 * XXX there is only one Sbus, for now -- do not know how to
    202 	 * address children on others
    203 	 */
    204 	if (sc->sc_dev.dv_unit > 0) {
    205 		printf(" unsupported\n");
    206 		return;
    207 	}
    208 
    209 	sc->sc_bustag = ma->ma_bustag;
    210 	sc->sc_dmatag = ma->ma_dmatag;
    211 
    212 	/* Setup interrupt translation tables */
    213 	sc->sc_intr2ipl = CPU_ISSUN4C
    214 				? intr_sbus2ipl_4c
    215 				: intr_sbus2ipl_4m;
    216 
    217 #if 0 /* this won't work */
    218 	sc->sc_intr_compat = CPU_ISSUN4C
    219 				? intr2sbus_4c
    220 				: intr2sbus_4m;
    221 #endif
    222 
    223 	/*
    224 	 * Record clock frequency for synchronous SCSI.
    225 	 * IS THIS THE CORRECT DEFAULT??
    226 	 */
    227 	sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
    228 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    229 
    230 	sbus_attach(sc, "sbus", node, ma->ma_bp, NULL);
    231 }
    232 
    233 void
    234 sbus_attach_iommu(parent, self, aux)
    235 	struct device *parent;
    236 	struct device *self;
    237 	void *aux;
    238 {
    239 	struct sbus_softc *sc = (struct sbus_softc *)self;
    240 	struct iommu_attach_args *ia = aux;
    241 	int node = ia->iom_node;
    242 
    243 	sc->sc_bustag = ia->iom_bustag;
    244 	sc->sc_dmatag = ia->iom_dmatag;
    245 
    246 	/* Setup interrupt translation tables */
    247 	sc->sc_intr2ipl = CPU_ISSUN4C ? intr_sbus2ipl_4c : intr_sbus2ipl_4m;
    248 
    249 #if 0 /* this won't work */
    250 	sc->sc_intr_compat = CPU_ISSUN4C ? intr2sbus_4c : intr2sbus_4m;
    251 #endif
    252 
    253 	/*
    254 	 * Record clock frequency for synchronous SCSI.
    255 	 * IS THIS THE CORRECT DEFAULT??
    256 	 */
    257 	sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
    258 	printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
    259 
    260 	sbus_attach(sc, "sbus", node, ia->iom_bp, NULL);
    261 }
    262 
    263 void
    264 sbus_attach(sc, busname, busnode, bp, specials)
    265 	struct sbus_softc *sc;
    266 	char *busname;
    267 	int busnode;
    268 	struct bootpath *bp;
    269 	const char * const *specials;
    270 {
    271 	int node0, node, error;
    272 	const char *sp;
    273 	const char *const *ssp;
    274 	bus_space_tag_t sbt;
    275 	struct sbus_attach_args sa;
    276 
    277 	sbt = sbus_alloc_bustag(sc);
    278 
    279 	/*
    280 	 * Get the SBus burst transfer size if burst transfers are supported
    281 	 */
    282 	sc->sc_burst = getpropint(busnode, "burst-sizes", 0);
    283 
    284 	/* Propagate bootpath */
    285 	if (bp != NULL && strcmp(bp->name, busname) == 0)
    286 		bp++;
    287 	else
    288 		bp = NULL;
    289 
    290 	/*
    291 	 * Collect address translations from the OBP.
    292 	 */
    293 	error = getpropA(busnode, "ranges", sizeof(struct rom_range),
    294 			 &sc->sc_nrange, (void **)&sc->sc_range);
    295 	switch (error) {
    296 	case 0:
    297 		break;
    298 	case ENOENT:
    299 		/* Fall back to our own `range' construction */
    300 		sc->sc_range = sbus_translations;
    301 		sc->sc_nrange =
    302 			sizeof(sbus_translations)/sizeof(sbus_translations[0]);
    303 		break;
    304 	default:
    305 		panic("%s: error getting ranges property", sc->sc_dev.dv_xname);
    306 	}
    307 
    308 	/*
    309 	 * Loop through ROM children, fixing any relative addresses
    310 	 * and then configuring each device.
    311 	 * `specials' is an array of device names that are treated
    312 	 * specially:
    313 	 */
    314 	node0 = firstchild(busnode);
    315 	for (ssp = specials ; ssp != NULL && *(sp = *ssp) != 0; ssp++) {
    316 		if ((node = findnode(node0, sp)) == 0) {
    317 			panic("could not find %s amongst %s devices",
    318 				sp, busname);
    319 		}
    320 
    321 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    322 					   node, bp, &sa) != 0) {
    323 			panic("sbus_attach: %s: incomplete", sp);
    324 		}
    325 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
    326 	}
    327 
    328 	for (node = node0; node; node = nextsibling(node)) {
    329 		char *name = getpropstring(node, "name");
    330 		for (ssp = specials, sp = NULL;
    331 		     ssp != NULL && (sp = *ssp) != NULL;
    332 		     ssp++)
    333 			if (strcmp(name, sp) == 0)
    334 				break;
    335 
    336 		if (sp != NULL)
    337 			/* Already configured as an "early" device */
    338 			continue;
    339 
    340 		if (sbus_setup_attach_args(sc, sbt, sc->sc_dmatag,
    341 					   node, bp, &sa) != 0) {
    342 			printf("sbus_attach: %s: incomplete\n", name);
    343 			continue;
    344 		}
    345 		(void) config_found(&sc->sc_dev, (void *)&sa, sbus_print);
    346 	}
    347 }
    348 
    349 int
    350 sbus_setup_attach_args(sc, bustag, dmatag, node, bp, sa)
    351 	struct sbus_softc	*sc;
    352 	bus_space_tag_t		bustag;
    353 	bus_dma_tag_t		dmatag;
    354 	int			node;
    355 	struct bootpath		*bp;
    356 	struct sbus_attach_args	*sa;
    357 {
    358 	struct	rom_reg romreg;
    359 	int	base;
    360 	int	error;
    361 
    362 	bzero(sa, sizeof(struct sbus_attach_args));
    363 	sa->sa_name = getpropstring(node, "name");
    364 	sa->sa_bustag = bustag;
    365 	sa->sa_dmatag = dmatag;
    366 	sa->sa_node = node;
    367 	sa->sa_bp = bp;
    368 
    369 	if ((error = getprop_reg1(node, &romreg)) != 0)
    370 		return (error);
    371 
    372 	/* We pass only the first "reg" property */
    373 	base = (int)romreg.rr_paddr;
    374 	if (SBUS_ABS(base)) {
    375 		sa->sa_slot = SBUS_ABS_TO_SLOT(base);
    376 		sa->sa_offset = SBUS_ABS_TO_OFFSET(base);
    377 	} else {
    378 		sa->sa_slot = romreg.rr_iospace;
    379 		sa->sa_offset = base;
    380 	}
    381 	sa->sa_size = romreg.rr_len;
    382 
    383 	if ((error = sbus_get_intr(sc, node, &sa->sa_pri)) != 0)
    384 		return (error);
    385 
    386 	if ((error = getprop_address1(node, &sa->sa_promvaddr)) != 0)
    387 		return (error);
    388 
    389 	return (0);
    390 }
    391 
    392 int
    393 sbus_bus_map(t, slot, offset, size, flags, vaddr, hp)
    394 	bus_space_tag_t t;
    395 	int slot, offset, size, flags;
    396 	vm_offset_t vaddr;
    397 	bus_space_handle_t *hp;
    398 {
    399 	struct sbus_softc *sc = t->cookie;
    400 	int i;
    401 
    402 	for (i = 0; i < sc->sc_nrange; i++) {
    403 		bus_addr_t paddr;
    404 		bus_type_t iospace;
    405 
    406 		if (sc->sc_range[i].cspace != slot)
    407 			continue;
    408 
    409 		/* We've found the connection to the parent bus */
    410 		paddr = sc->sc_range[i].poffset + offset;
    411 		iospace = sc->sc_range[i].pspace;
    412 		return (bus_space_map2(sc->sc_bustag, iospace, paddr,
    413 					size, flags, vaddr, hp));
    414 	}
    415 
    416 	return (EINVAL);
    417 }
    418 
    419 int
    420 sbus_bus_mmap(cookie, btype, paddr, flags)
    421 	void *cookie;
    422 	bus_type_t btype;
    423 	bus_addr_t paddr;
    424 	int flags;
    425 {
    426 	int slot = (int)btype;
    427 	int offset = (int)paddr;
    428 	struct sbus_softc *sc = cookie;
    429 	int i;
    430 
    431 	for (i = 0; i < sc->sc_nrange; i++) {
    432 		bus_addr_t paddr;
    433 		bus_addr_t iospace;
    434 
    435 		if (sc->sc_range[i].cspace != slot)
    436 			continue;
    437 
    438 		paddr = sc->sc_range[i].poffset + offset;
    439 		iospace = (bus_addr_t)sc->sc_range[i].pspace;
    440 		return (bus_space_mmap(sc->sc_bustag, iospace, paddr, flags));
    441 	}
    442 
    443 	return (-1);
    444 }
    445 
    446 
    447 /*
    448  * Each attached device calls sbus_establish after it initializes
    449  * its sbusdev portion.
    450  */
    451 void
    452 sbus_establish(sd, dev)
    453 	register struct sbusdev *sd;
    454 	register struct device *dev;
    455 {
    456 	register struct sbus_softc *sc;
    457 	register struct device *curdev;
    458 
    459 	/*
    460 	 * We have to look for the sbus by name, since it is not necessarily
    461 	 * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
    462 	 * We don't just use the device structure of the above-attached
    463 	 * sbus, since we might (in the future) support multiple sbus's.
    464 	 */
    465 	for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
    466 		if (!curdev || !curdev->dv_xname)
    467 			panic("sbus_establish: can't find sbus parent for %s",
    468 			      sd->sd_dev->dv_xname
    469 					? sd->sd_dev->dv_xname
    470 					: "<unknown>" );
    471 
    472 		if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
    473 			break;
    474 	}
    475 	sc = (struct sbus_softc *) curdev;
    476 
    477 	sd->sd_dev = dev;
    478 	sd->sd_bchain = sc->sc_sbdev;
    479 	sc->sc_sbdev = sd;
    480 }
    481 
    482 /*
    483  * Reset the given sbus. (???)
    484  */
    485 void
    486 sbusreset(sbus)
    487 	int sbus;
    488 {
    489 	register struct sbusdev *sd;
    490 	struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
    491 	struct device *dev;
    492 
    493 	printf("reset %s:", sc->sc_dev.dv_xname);
    494 	for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
    495 		if (sd->sd_reset) {
    496 			dev = sd->sd_dev;
    497 			(*sd->sd_reset)(dev);
    498 			printf(" %s", dev->dv_xname);
    499 		}
    500 	}
    501 }
    502 
    503 
    504 /*
    505  * Get interrupt attributes for an Sbus device.
    506  */
    507 int
    508 sbus_get_intr(sc, node, ip)
    509 	struct sbus_softc *sc;
    510 	int node;
    511 	int *ip;
    512 {
    513 	struct rom_intr *rip;
    514 	int *ipl;
    515 	int n;
    516 
    517 	/*
    518 	 * The `interrupts' property contains the Sbus interrupt level.
    519 	 */
    520 	ipl = NULL;
    521 	if (getpropA(node, "interrupts", sizeof(int), &n, (void **)&ipl) == 0) {
    522 		*ip = ipl[0];
    523 		free(ipl, M_DEVBUF);
    524 		return (0);
    525 	}
    526 
    527 	/*
    528 	 * Fall back on `intr' property.
    529 	 */
    530 	rip = NULL;
    531 	switch (getpropA(node, "intr", sizeof(*rip), &n, (void **)&rip)) {
    532 	case 0:
    533 		*ip = (rip[0].int_pri & 0xf) | SBUS_INTR_COMPAT;
    534 		free(rip, M_DEVBUF);
    535 		return (0);
    536 	case ENOENT:
    537 		*ip = 0;
    538 		return (0);
    539 	}
    540 
    541 	return (-1);
    542 }
    543 
    544 
    545 /*
    546  * Install an interrupt handler for an Sbus device.
    547  */
    548 void *
    549 sbus_intr_establish(cookie, level, flags, handler, arg)
    550         void *cookie;
    551 	int level;
    552 	int flags;
    553 	int (*handler) __P((void *));
    554 	void *arg;
    555 {
    556 	struct sbus_softc *sc = cookie;
    557 	struct intrhand *ih;
    558 	int ipl;
    559 
    560 	ih = (struct intrhand *)
    561 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
    562 	if (ih == NULL)
    563 		return (NULL);
    564 
    565 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) != 0)
    566 		ipl = level;
    567 	else if ((level & SBUS_INTR_COMPAT) != 0)
    568 		ipl = level & ~SBUS_INTR_COMPAT;
    569 	else
    570 		ipl = sc->sc_intr2ipl[level];
    571 
    572 	ih->ih_fun = handler;
    573 	ih->ih_arg = arg;
    574 	if ((flags & BUS_INTR_ESTABLISH_FASTTRAP) != 0)
    575 		intr_fasttrap(ipl, (void (*)__P((void)))handler);
    576 	else
    577 		intr_establish(ipl, ih);
    578 	return (ih);
    579 }
    580 
    581 static bus_space_tag_t
    582 sbus_alloc_bustag(sc)
    583 	struct sbus_softc *sc;
    584 {
    585 	bus_space_tag_t sbt;
    586 
    587 	sbt = (bus_space_tag_t)
    588 		malloc(sizeof(struct sparc_bus_space_tag), M_DEVBUF, M_NOWAIT);
    589 	if (sbt == NULL)
    590 		return (NULL);
    591 
    592 	bzero(sbt, sizeof *sbt);
    593 	sbt->cookie = sc;
    594 #if notyet
    595 	sbt->sparc_bus_map = _sbus_bus_map;
    596 #endif
    597 	sbt->sparc_bus_mmap = sbus_bus_mmap;
    598 	sbt->sparc_intr_establish = sbus_intr_establish;
    599 	return (sbt);
    600 }
    601 
    602