NetBSD/sys/arch/sparc/dev/obio.c

412 lines
9.2 KiB
C
Raw Normal View History

1994-11-05 12:28:06 +03:00
/* $NetBSD: obio.c,v 1.8 1994/11/05 09:28:06 deraadt Exp $ */
1994-08-24 13:16:46 +04:00
/*
* Copyright (c) 1993, 1994 Theo de Raadt
* All rights reserved.
*
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Theo de Raadt.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/
#include <sys/param.h>
#include <sys/device.h>
#include <sys/malloc.h>
#ifdef DEBUG
#include <sys/proc.h>
#include <sys/syslog.h>
#endif
#include <vm/vm.h>
#include <machine/autoconf.h>
#include <machine/pmap.h>
#include <machine/oldmon.h>
1994-10-26 10:09:41 +03:00
#include <machine/cpu.h>
#include <machine/ctlreg.h>
#include <sparc/sparc/asm.h>
#include <sparc/sparc/vaddrs.h>
1994-08-24 13:16:46 +04:00
1994-10-15 08:48:47 +03:00
struct bus_softc {
1994-08-24 13:16:46 +04:00
struct device sc_dev; /* base device */
int nothing;
};
/* autoconfiguration driver */
1994-10-15 08:48:47 +03:00
static int busmatch(struct device *, struct cfdata *, void *);
1994-08-24 13:16:46 +04:00
static void obioattach(struct device *, struct device *, void *);
1994-10-15 08:48:47 +03:00
static void vmesattach(struct device *, struct device *, void *);
static void vmelattach(struct device *, struct device *, void *);
struct cfdriver obiocd = { NULL, "obio", busmatch, obioattach,
DV_DULL, sizeof(struct bus_softc)
};
struct cfdriver vmelcd = { NULL, "vmel", busmatch, vmelattach,
DV_DULL, sizeof(struct bus_softc)
1994-08-24 13:16:46 +04:00
};
1994-10-15 08:48:47 +03:00
struct cfdriver vmescd = { NULL, "vmes", busmatch, vmesattach,
DV_DULL, sizeof(struct bus_softc)
};
static void busattach(struct device *, struct device *, void *, int);
1994-08-24 13:16:46 +04:00
1994-10-15 08:48:47 +03:00
void * bus_map __P((void *, int, int));
void * bus_tmp __P((void *, int));
void bus_untmp __P((void));
1994-08-24 13:16:46 +04:00
int
1994-10-15 08:48:47 +03:00
busmatch(parent, cf, aux)
1994-08-24 13:16:46 +04:00
struct device *parent;
struct cfdata *cf;
1994-10-03 00:59:56 +03:00
void *aux;
1994-08-24 13:16:46 +04:00
{
1994-10-03 00:59:56 +03:00
register struct confargs *ca = aux;
register struct romaux *ra = &ca->ca_ra;
1994-08-24 13:16:46 +04:00
if (cputyp != CPU_SUN4)
1994-10-03 00:59:56 +03:00
return (0);
return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
1994-08-24 13:16:46 +04:00
}
int
1994-10-15 08:48:47 +03:00
busprint(args, obio)
1994-08-24 13:16:46 +04:00
void *args;
char *obio;
{
register struct confargs *ca = args;
if (ca->ca_ra.ra_name == NULL)
ca->ca_ra.ra_name = "<unknown>";
if (obio)
1994-10-03 00:59:56 +03:00
printf("[%s at %s]", ca->ca_ra.ra_name, obio);
printf(" addr %x", ca->ca_ra.ra_paddr);
1994-11-03 02:16:28 +03:00
if (ca->ca_ra.ra_intr[0].int_vec != -1)
printf(" vec 0x%x", ca->ca_ra.ra_intr[0].int_vec);
1994-08-24 13:16:46 +04:00
return (UNCONF);
}
void
1994-10-15 08:48:47 +03:00
busattach(parent, self, args, bustype)
1994-08-24 13:16:46 +04:00
struct device *parent, *self;
1994-10-03 00:59:56 +03:00
void *args;
1994-10-15 08:48:47 +03:00
int bustype;
1994-08-24 13:16:46 +04:00
{
1994-10-15 08:48:47 +03:00
register struct bus_softc *sc = (struct bus_softc *)self;
extern struct cfdata cfdata[];
1994-10-03 00:59:56 +03:00
register struct confargs *ca = args;
struct confargs oca;
1994-08-24 13:16:46 +04:00
register short *p;
struct cfdata *cf;
1994-10-15 08:48:47 +03:00
caddr_t tmp;
1994-08-24 13:16:46 +04:00
if (sc->sc_dev.dv_unit > 0) {
printf(" unsupported\n");
return;
}
1994-10-15 08:48:47 +03:00
printf("\n");
1994-08-24 13:16:46 +04:00
for (cf = cfdata; cf->cf_driver; cf++) {
if (cf->cf_fstate == FSTATE_FOUND)
continue;
for (p = cf->cf_parents; *p >= 0; p++)
1994-10-03 00:59:56 +03:00
if (self->dv_cfdata == &cfdata[*p]) {
oca.ca_ra.ra_iospace = -1;
oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
oca.ca_ra.ra_len = 0;
1994-10-15 08:48:47 +03:00
tmp = NULL;
1994-10-03 00:59:56 +03:00
if (oca.ca_ra.ra_paddr)
1994-10-15 08:48:47 +03:00
tmp = bus_tmp(oca.ca_ra.ra_paddr,
bustype);
oca.ca_ra.ra_vaddr = tmp;
1994-10-03 00:59:56 +03:00
oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
1994-10-26 10:09:41 +03:00
if (bustype == BUS_VME16 || bustype == BUS_VME32)
oca.ca_ra.ra_intr[0].int_vec = cf->cf_loc[2];
else
1994-11-05 12:28:06 +03:00
oca.ca_ra.ra_intr[0].int_vec = -1;
1994-10-03 00:59:56 +03:00
oca.ca_ra.ra_nintr = 1;
oca.ca_ra.ra_name = cf->cf_driver->cd_name;
oca.ca_ra.ra_bp = ca->ca_ra.ra_bp;
1994-10-15 08:48:47 +03:00
oca.ca_bustype = bustype;
1994-10-03 00:59:56 +03:00
if ((*cf->cf_driver->cd_match)(self, cf, &oca) == 0)
continue;
1994-10-03 00:59:56 +03:00
/*
* check if XXmatch routine replaced the
* temporary mapping with a real mapping.
*/
1994-10-15 08:48:47 +03:00
if (tmp == oca.ca_ra.ra_vaddr)
1994-10-03 00:59:56 +03:00
oca.ca_ra.ra_vaddr = NULL;
/*
* or if it has asked us to create a mapping..
* (which won't be seen on future XXmatch calls,
* so not as useful as it seems.)
*/
if (oca.ca_ra.ra_len)
oca.ca_ra.ra_vaddr =
1994-10-15 08:48:47 +03:00
bus_map(oca.ca_ra.ra_paddr,
oca.ca_ra.ra_len, oca.ca_bustype);
1994-10-03 00:59:56 +03:00
1994-10-15 08:48:47 +03:00
config_attach(self, cf, &oca, busprint);
1994-08-24 13:16:46 +04:00
}
}
1994-10-15 08:48:47 +03:00
bus_untmp();
}
void
obioattach(parent, self, args)
struct device *parent, *self;
void *args;
{
busattach(parent, self, args, BUS_OBIO);
}
1994-10-26 10:09:41 +03:00
struct intrhand **vmeints;
1994-10-15 08:48:47 +03:00
void
vmesattach(parent, self, args)
struct device *parent, *self;
void *args;
{
1994-10-26 10:09:41 +03:00
if (vmeints == NULL) {
vmeints = (struct intrhand **)malloc(256 *
sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
bzero(vmeints, 256 * sizeof(struct intrhand *));
}
1994-10-15 08:48:47 +03:00
busattach(parent, self, args, BUS_VME16);
}
1994-10-15 08:48:47 +03:00
void
vmelattach(parent, self, args)
struct device *parent, *self;
void *args;
{
1994-10-26 10:09:41 +03:00
if (vmeints == NULL) {
vmeints = (struct intrhand **)malloc(256 *
sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
bzero(vmeints, 256 * sizeof(struct intrhand *));
}
1994-10-15 08:48:47 +03:00
busattach(parent, self, args, BUS_VME32);
}
1994-10-26 10:09:41 +03:00
int pil_to_vme[] = {
-1, /* pil 0 */
-1, /* pil 1 */
1, /* pil 2 */
2, /* pil 3 */
-1, /* pil 4 */
3, /* pil 5 */
-1, /* pil 6 */
4, /* pil 7 */
-1, /* pil 8 */
5, /* pil 9 */
-1, /* pil 10 */
6, /* pil 11 */
-1, /* pil 12 */
7, /* pil 13 */
-1, /* pil 14 */
-1, /* pil 15 */
};
int
vmeintr(arg)
void *arg;
{
int level = (int)arg, vec;
struct intrhand *ih;
int i = 0;
vec = ldcontrolb(AC_VMEINTVEC | (pil_to_vme[level] << 1) | 1);
if (vec == -1) {
printf("vme: spurious interrupt\n");
return 0;
}
1994-10-26 10:09:41 +03:00
for (ih = vmeints[vec]; ih; ih = ih->ih_next)
if (ih->ih_fun)
i += (ih->ih_fun)(ih->ih_arg);
return (i);
}
void
vmeintr_establish(vec, level, ih)
int vec, level;
struct intrhand *ih;
{
struct intrhand *ihs;
1994-11-05 12:28:06 +03:00
if (vec == -1)
panic("vmeintr_establish: uninitialized vec\n");
1994-10-26 10:09:41 +03:00
if (vmeints[vec] == NULL)
vmeints[vec] = ih;
else {
for (ihs = vmeints[vec]; ihs->ih_next; ihs = ihs->ih_next)
;
ihs->ih_next = ih;
}
/* ensure the interrupt subsystem will call us at this level */
for (ihs = intrhand[level]; ihs; ihs = ihs->ih_next)
if (ihs->ih_fun == vmeintr)
return;
ihs = (struct intrhand *)malloc(sizeof(struct intrhand),
M_TEMP, M_NOWAIT);
if (ihs == NULL)
panic("vme_addirq");
bzero(ihs, sizeof *ihs);
ihs->ih_fun = vmeintr;
ihs->ih_arg = (void *)level;
intr_establish(level, ihs);
}
1994-10-15 08:48:47 +03:00
#define getpte(va) lda(va, ASI_PTE)
/*
* If we can find a mapping that was established by the rom, use it.
* Else, create a new mapping.
*/
void *
1994-10-15 08:48:47 +03:00
bus_map(pa, len, bustype)
void *pa;
int len;
1994-10-15 08:48:47 +03:00
int bustype;
{
1994-10-03 00:59:56 +03:00
u_long pf = (u_long)pa >> PGSHIFT;
u_long va, pte;
1994-10-15 08:48:47 +03:00
int pgtype;
switch (bt2pmt[bustype]) {
case PMAP_OBIO:
pgtype = PG_OBIO;
break;
case PMAP_VME32:
pgtype = PG_VME32;
break;
case PMAP_VME16:
pgtype = PG_VME16;
break;
}
1994-10-03 00:59:56 +03:00
if (len <= NBPG) {
for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
pte = getpte(va);
1994-10-15 08:48:47 +03:00
if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
1994-10-03 00:59:56 +03:00
(pte & PG_PFNUM) == pf)
return ((void *)va);
}
}
1994-10-15 08:48:47 +03:00
return mapiodev(pa, len, bustype);
}
void *
1994-10-15 08:48:47 +03:00
bus_tmp(pa, bustype)
void *pa;
1994-10-15 08:48:47 +03:00
int bustype;
{
1994-10-03 00:59:56 +03:00
vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
1994-10-15 08:48:47 +03:00
int pmtype = bt2pmt[bustype];
1994-10-03 00:59:56 +03:00
pmap_enter(kernel_pmap, TMPMAP_VA,
1994-10-15 08:48:47 +03:00
addr | pmtype | PMAP_NC,
VM_PROT_READ | VM_PROT_WRITE, 1);
return ((void *)TMPMAP_VA);
}
void
1994-10-15 08:48:47 +03:00
bus_untmp()
{
pmap_remove(kernel_pmap, TMPMAP_VA, TMPMAP_VA+NBPG);
1994-08-24 13:16:46 +04:00
}
void
wzero(b, l)
1994-11-05 12:28:06 +03:00
u_char *b;
int l;
{
1994-11-05 12:28:06 +03:00
register u_char *be = b + l;
register u_short *sp;
if (l <= 0)
return;
/* front, */
if ((u_long)b & 1)
*b++ = 0;
/* back, */
if (b != be && ((u_long)be & 1) != 0) {
be--;
*be = 0;
}
/* and middle. */
sp = (u_short *)b;
while (sp != (u_short *)be)
*sp++ = 0;
}
void
wcopy(b1, b2, l)
u_char *b1, *b2;
u_long l;
{
register u_short *sp;
register int bstore = 0;
register u_char *b1e;
if (l <= 0)
return;
/* front, */
if ((u_long)b1 & 1) {
*b2++ = *b1++;
l--;
}
/* middle, */
sp = (u_short *)b1;
b1e = b1 + l;
if (l & 1)
b1e--;
bstore = (u_long)b2 & 1;
while (sp < (u_short *)b1e) {
if (bstore) {
b2[1] = *sp & 0xff;
b2[0] = *sp >> 8;
} else
*((short *)b2) = *sp;
sp++;
b2 += 2;
}
/* and back. */
if (l & 1)
*b2 = *b1e;
}