#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.82 2026/04/26 10:52:16 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/device.h>
#include "scsibus.h"
#if NSCSIBUS > 0
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsiconf.h>
#endif
#include <machine/autoconf.h>
#include <machine/cpu.h>
#include <machine/mon.h>
#include <sun3/sun3/machdep.h>
#if (defined(_SUN3_) + defined(_SUN3X_)) != 1
#error "Must have exactly one of: SUN3 and SUN3X options"
#endif
void
cpu_configure(void)
{
if (config_rootfound("mainbus", NULL) == NULL)
panic("%s: mainbus not found", __func__);
printf("enabling interrupts\n");
(void)spl0();
}
int
bus_scan(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
struct confargs *ca = aux;
#ifdef DIAGNOSTIC
if (cf->cf_fstate == FSTATE_STAR)
panic("%s: FSTATE_STAR", __func__);
#endif
ca->ca_paddr = cf->cf_paddr;
ca->ca_intpri = cf->cf_intpri;
ca->ca_intvec = cf->cf_intvec;
if (config_probe(parent, cf, ca)) {
config_attach(parent, cf, ca, bus_print, CFARGS_NONE);
}
return 0;
}
int
bus_print(void *args, const char *name)
{
struct confargs *ca = args;
if (name)
aprint_normal("%s:", name);
if (ca->ca_paddr != -1)
aprint_normal(" addr 0x%lx", ca->ca_paddr);
if (ca->ca_intpri != -1)
aprint_normal(" ipl %d", ca->ca_intpri);
if (ca->ca_intvec != -1)
aprint_normal(" vect 0x%x", ca->ca_intvec);
return UNCONF;
}
typedef device_t (*findfunc_t)(char *, int, int);
static device_t net_find(char *, int, int);
#if NSCSIBUS > 0
static device_t scsi_find(char *, int, int);
#endif
static device_t xx_find(char *, int, int);
struct prom_n2f {
const char name[4];
findfunc_t func;
};
static struct prom_n2f prom_dev_table[] = {
{ "ie", net_find },
{ "le", net_find },
#if NSCSIBUS > 0
{ "sd", scsi_find },
#endif
{ "xy", xx_find },
{ "xd", xx_find },
{ "", 0 },
};
void
cpu_rootconf(void)
{
struct bootparam *bp;
struct prom_n2f *nf;
const char *devname;
findfunc_t find;
char promname[4];
char partname[4];
bp = *romVectorPtr->bootParam;
promname[0] = bp->devName[0];
promname[1] = bp->devName[1];
promname[2] = '\0';
booted_device = NULL;
booted_partition = 0;
devname = "<unknown>";
partname[0] = '\0';
find = NULL;
for (nf = prom_dev_table; nf->func; nf++)
if (!strcmp(nf->name, promname)) {
find = nf->func;
break;
}
if (find)
booted_device = (*find)(promname, bp->ctlrNum, bp->unitNum);
if (booted_device) {
devname = device_xname(booted_device);
if (device_class(booted_device) == DV_DISK) {
booted_partition = bp->partNum & 7;
partname[0] = 'a' + booted_partition;
partname[1] = '\0';
}
}
printf("boot device: %s%s\n", devname, partname);
rootconf();
}
static device_t
net_find(char *name, int ctlr, int unit)
{
return device_find_by_driver_unit(name, ctlr);
}
#if NSCSIBUS > 0
static device_t
scsi_find(char *name, int ctlr, int unit)
{
device_t scsibus;
struct scsibus_softc *sbsc;
struct scsipi_periph *periph;
int target, lun;
scsibus = device_find_by_driver_unit("scsibus", ctlr);
if (scsibus == NULL)
return NULL;
target = (unit >> 3) & 7;
lun = unit & 7;
sbsc = device_private(scsibus);
periph = scsipi_lookup_periph(sbsc->sc_channel, target, lun);
if (periph == NULL)
return NULL;
return periph->periph_dev;
}
#endif
static device_t
xx_find(char *name, int ctlr, int unit)
{
return device_find_by_driver_unit(name, ctlr * 2 + unit);
}
int
bus_peek(int bustype, int pa, int sz)
{
void *va;
int rv;
va = bus_tmapin(bustype, pa);
switch (sz) {
case 1:
rv = peek_byte(va);
break;
case 2:
rv = peek_word(va);
break;
case 4:
rv = peek_long(va);
break;
default:
printf(" %s: invalid size=%d\n", __func__, sz);
rv = -1;
}
bus_tmapout(va);
return rv;
}
int
peek_byte(void *addr)
{
label_t faultbuf;
int x;
nofault = &faultbuf;
if (setjmp(&faultbuf))
x = -1;
else
x = *(volatile u_char *)addr;
nofault = NULL;
return x;
}
int
peek_word(void *addr)
{
label_t faultbuf;
int x;
nofault = &faultbuf;
if (setjmp(&faultbuf))
x = -1;
else
x = *(volatile u_short *)addr;
nofault = NULL;
return x;
}
int
peek_long(void *addr)
{
label_t faultbuf;
int x;
nofault = &faultbuf;
if (setjmp(&faultbuf))
x = -1;
else {
x = *(volatile int *)addr;
if (x == -1) {
printf("%s: uh-oh, actually read -1!\n", __func__);
x &= 0x7FFFffff;
}
}
nofault = NULL;
return x;
}