/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Paul Kranenburg.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)sbus.c 8.1 (Berkeley) 6/11/93
*/
/* The "primary" Sbus */
struct sbus_softc *sbus_sc;
/* If the PROM does not provide the `ranges' property, we make up our own */
struct openprom_range sbus_translations[] = {
/* Assume a maximum of 4 Sbus slots, all mapped to on-board io space */
{ 0, 0, PMAP_OBIO, SBUS_ADDR(0,0), 1 << 25 },
{ 1, 0, PMAP_OBIO, SBUS_ADDR(1,0), 1 << 25 },
{ 2, 0, PMAP_OBIO, SBUS_ADDR(2,0), 1 << 25 },
{ 3, 0, PMAP_OBIO, SBUS_ADDR(3,0), 1 << 25 }
};
/*
* Child devices receive the Sbus interrupt level in their attach
* arguments. We translate these to CPU IPLs using the following
* tables. Note: obio bus interrupt levels are identical to the
* processor IPL.
*
* The second set of tables is used when the Sbus interrupt level
* cannot be had from the PROM as an `interrupt' property. We then
* fall back on the `intr' property which contains the CPU IPL.
*/
/*
* This value is or'ed into the attach args' interrupt level cookie
* if the interrupt level comes from an `intr' property, i.e. it is
* not an Sbus interrupt level.
*/
#define SBUS_INTR_COMPAT 0x80000000
/*
* Print the location of some sbus-attached device (called just
* before attaching that device). If `sbus' is not NULL, the
* device was found but not configured; print the sbus as well.
* Return UNCONF (config_find ignores this if the device was configured).
*/
int
sbus_print(void *args, const char *busname)
{
struct sbus_attach_args *sa = args;
int i;
if (busname)
aprint_normal("%s at %s", sa->sa_name, busname);
aprint_normal(" slot %d offset 0x%x", sa->sa_slot, sa->sa_offset);
for (i = 0; i < sa->sa_nintr; i++) {
uint32_t level = sa->sa_intr[i].oi_pri;
struct sbus_softc *sc =
(struct sbus_softc *) sa->sa_bustag->cookie;
aprint_normal(" level %d", level & ~SBUS_INTR_COMPAT);
if ((level & SBUS_INTR_COMPAT) == 0) {
int ipl = sc->sc_intr2ipl[level];
if (ipl != level)
aprint_normal(" (ipl %d)", ipl);
}
}
return (UNCONF);
}
/*
* Record clock frequency for synchronous SCSI.
* IS THIS THE CORRECT DEFAULT??
*/
sc->sc_clockfreq = prom_getpropint(node, "clock-frequency", 25*1000*1000);
printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
/*
* Record clock frequency for synchronous SCSI.
* IS THIS THE CORRECT DEFAULT??
*/
sc->sc_clockfreq = prom_getpropint(node, "clock-frequency", 25*1000*1000);
printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
/*
* Record clock frequency for synchronous SCSI.
* IS THIS THE CORRECT DEFAULT??
*/
sc->sc_clockfreq = prom_getpropint(node, "clock-frequency", 25*1000*1000);
printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
if ((sbt = bus_space_tag_alloc(sc->sc_bustag, sc)) == NULL) {
printf("%s: attach: out of memory\n",
device_xname(sc->sc_dev));
return;
}
sbt->sparc_intr_establish = sbus_intr_establish;
/*
* Get the SBus burst transfer size if burst transfers are supported
*/
sc->sc_burst = prom_getpropint(busnode, "burst-sizes", 0);
if (CPU_ISSUN4M) {
/*
* Some models (e.g. SS20) erroneously report 64-bit
* burst capability. We mask it out here for all SUN4Ms,
* since probably no member of that class supports
* 64-bit Sbus bursts.
*/
sc->sc_burst &= ~SBUS_BURST_64;
}
/*
* Collect address translations from the OBP.
*/
error = prom_getprop(busnode, "ranges", sizeof(struct rom_range),
&sbt->nranges, &sbt->ranges);
switch (error) {
case 0:
break;
case ENOENT:
/* Fall back to our own `range' construction */
sbt->ranges = sbus_translations;
sbt->nranges =
sizeof(sbus_translations)/sizeof(sbus_translations[0]);
break;
default:
panic("%s: error getting ranges property",
device_xname(sc->sc_dev));
}
/*
* Loop through ROM children, fixing any relative addresses
* and then configuring each device.
* `specials' is an array of device names that are treated
* specially:
*/
node0 = firstchild(busnode);
for (ssp = specials ; ssp != NULL && *(sp = *ssp) != 0; ssp++) {
if ((node = findnode(node0, sp)) == 0) {
panic("could not find %s amongst %s devices",
sp, busname);
}
/* XXX: sbus_bus_addr should be g/c'ed */
return (BUS_ADDR(btype, offset));
}
/*
* Get interrupt attributes for an Sbus device.
*/
static int
sbus_get_intr(struct sbus_softc *sc, int node,
struct openprom_intr **ipp, int *np)
{
int error, n;
uint32_t *ipl = NULL;
/*
* The `interrupts' property contains the Sbus interrupt level.
*/
if (prom_getprop(node, "interrupts", sizeof(int), np,
&ipl) == 0) {
/* Change format to an `struct openprom_intr' array */
struct openprom_intr *ip;
ip = malloc(*np * sizeof(struct openprom_intr), M_DEVBUF,
M_WAITOK);
for (n = 0; n < *np; n++) {
ip[n].oi_pri = ipl[n];
ip[n].oi_vec = 0;
}
free(ipl, M_DEVBUF);
*ipp = ip;
return (0);
}
/*
* Fall back on `intr' property.
*/
*ipp = NULL;
error = prom_getprop(node, "intr", sizeof(struct openprom_intr),
np, ipp);
switch (error) {
case 0:
for (n = *np; n-- > 0;) {
(*ipp)[n].oi_pri &= 0xf;
(*ipp)[n].oi_pri |= SBUS_INTR_COMPAT;
}
break;
case ENOENT:
error = 0;
break;
}
return (error);
}
/*
* Install an interrupt handler for an Sbus device.
*/
static void *
sbus_intr_establish(bus_space_tag_t t, int pri, int level,
int (*handler)(void *), void *arg,
void (*fastvec)(void))
{
struct sbus_softc *sc = t->cookie;
struct intrhand *ih;
int pil;
ih = kmem_alloc(sizeof(struct intrhand), KM_SLEEP);
/*
* Translate Sbus interrupt priority to CPU interrupt level
*/
if ((pri & SBUS_INTR_COMPAT) != 0)
pil = pri & ~SBUS_INTR_COMPAT;
else
pil = sc->sc_intr2ipl[pri];
/* For now, do the same dance as on stray interrupts */
timesince = time_uptime - straytime;
if (timesince <= 10) {
if (++nstray > 9)
panic("too many SBus errors");
} else {
straytime = time_uptime;
nstray = 1;
}