#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.32 2021/08/07 16:19:06 thorpej Exp $");
#include "opt_kgdb.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/reboot.h>
#include "locators.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/intr.h>
#include <machine/promlib.h>
#ifdef KGDB
#include <sys/kgdb.h>
#endif
void
cpu_configure(void)
{
if (boothowto & RB_KDB) {
#ifdef KGDB
kgdb_connect(1);
#else
Debugger();
#endif
}
if (config_rootfound("mainbus", NULL) == NULL)
panic("%s: mainbus not found", __func__);
printf("enabling interrupts\n");
(void)spl0();
}
static int mainbus_match(device_t, cfdata_t, void *);
static void mainbus_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(mainbus, 0,
mainbus_match, mainbus_attach, NULL, NULL);
static int
mainbus_match(device_t parent, cfdata_t cf, void *aux)
{
return 1;
}
static void
mainbus_attach(device_t parent, device_t self, void *args)
{
struct mainbus_attach_args ma;
const char *const *cpp;
static const char *const special[] = {
"obio",
"obmem",
NULL
};
aprint_normal("\n");
ma.ma_bustag = &mainbus_space_tag;
ma.ma_dmatag = &mainbus_dma_tag;
ma.ma_paddr = LOCATOR_FORBIDDEN;
ma.ma_pri = LOCATOR_FORBIDDEN;
for (cpp = special; *cpp != NULL; cpp++) {
ma.ma_name = *cpp;
(void)config_found(self, &ma, NULL, CFARGS_NONE);
}
ma.ma_name = NULL;
(void)config_found(self, &ma, NULL, CFARGS_NONE);
ma.ma_name = "pcons";
(void)config_found(self, &ma, NULL, CFARGS_NONE);
}
int
sun68k_bus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
{
struct mainbus_attach_args *map = aux;
struct mainbus_attach_args ma;
if (map->ma_name != NULL && strcmp(map->ma_name, cf->cf_name) != 0)
return 0;
#ifdef DIAGNOSTIC
if (cf->cf_fstate == FSTATE_STAR)
panic("%s: FSTATE_STAR", __func__);
#endif
ma = *map;
ma.ma_name = NULL;
#ifdef DIAGNOSTIC
#define BAD_LOCATOR(ma_loc, what) \
panic("%s: %s %s for: %s%d", __func__, \
map->ma_loc == LOCATOR_REQUIRED ? "missing" : "unexpected", \
what, cf->cf_name, cf->cf_unit)
#else
#define BAD_LOCATOR(ma_loc, what) return 0
#endif
#define CHECK_LOCATOR(ma_loc, cf_loc, what) \
if ((map->ma_loc == LOCATOR_FORBIDDEN && cf->cf_loc != -1) || \
(map->ma_loc == LOCATOR_REQUIRED && cf->cf_loc == -1)) \
BAD_LOCATOR(ma_loc, what); \
else \
ma.ma_loc = cf->cf_loc
CHECK_LOCATOR(ma_paddr, cf_loc[MBIOCF_ADDR], "address");
CHECK_LOCATOR(ma_pri, cf_loc[MBIOCF_IPL], "ipl");
if (config_probe(parent, cf, &ma)) {
config_attach(parent, cf, &ma, sun68k_bus_print, CFARGS_NONE);
}
return 0;
}
int
sun68k_bus_print(void *args, const char *name)
{
struct mainbus_attach_args *ma = args;
if (name)
aprint_normal("%s:", name);
if (ma->ma_paddr != -1)
aprint_normal(" addr 0x%x", (unsigned int) ma->ma_paddr);
if (ma->ma_pri != -1)
aprint_normal(" ipl %d", ma->ma_pri);
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 },
{ "ec", net_find },
{ "le", net_find },
#if NSCSIBUS > 0
{ "sd", scsi_find },
#endif
{ "xy", xx_find },
{ "xd", xx_find },
{ "", 0 },
};
static const char *str2hex(const char *, int *);
static const char *
str2hex(const char *p, int *_val)
{
int val;
int c;
for (val = 0;; val = (val << 4) + c, p++) {
c = *((const unsigned char *)p);
if (c >= 'a')
c-= ('a' + 10);
else if (c >= 'A')
c -= ('A' + 10);
else if (c >= '0')
c -= '0';
if (c < 0 || c > 15)
break;
}
*_val = val;
return p;
}
void
cpu_rootconf(void)
{
struct prom_n2f *nf;
const char *devname;
findfunc_t find;
char promname[4];
char partname[4];
const char *prompath;
int prom_ctlr, prom_unit, prom_part;
prompath = prom_getbootpath();
if (prompath == NULL)
prompath = "zz(0,0,0)";
promname[0] = *(prompath++);
promname[1] = *(prompath++);
promname[2] = '\0';
prom_ctlr = prom_unit = prom_part = 0;
if (*prompath == '(' &&
*(prompath = str2hex(++prompath, &prom_ctlr)) == ',' &&
*(prompath = str2hex(++prompath, &prom_unit)) == ',')
(void)str2hex(++prompath, &prom_part);
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, prom_ctlr, prom_unit);
if (booted_device) {
devname = device_xname(booted_device);
if (device_class(booted_device) == DV_DISK) {
booted_partition = prom_part & 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;
if ((scsibus = device_find_by_driver_unit("scsibus", ctlr)) == NULL)
return NULL;
target = prom_sd_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);
}