#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.122 2023/10/14 08:05:25 andvar Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/reboot.h>
#include <sys/conf.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/device_impl.h>
#include <sys/disklabel.h>
#include <sys/disk.h>
#include <sys/proc.h>
#include <machine/cpu.h>
#include <amiga/amiga/cfdev.h>
#include <amiga/amiga/device.h>
#include <amiga/amiga/custom.h>
#ifdef DRACO
#include <amiga/amiga/drcustom.h>
#endif
#ifdef P5PB_CONSOLE
#include <amiga/pci/p5pbvar.h>
#endif
#include "acafh.h"
#if NACAFH > 0
#include <amiga/dev/acafhvar.h>
#endif
static void findroot(void);
void mbattach(device_t, device_t, void *);
int mbprint(void *, const char *);
int mbmatch(device_t, cfdata_t, void *);
#include <sys/kernel.h>
u_long boot_partition;
int amiga_realconfig;
void
cpu_configure(void)
{
int s;
#ifdef DEBUG_KERNEL_START
int i;
#endif
amiga_realconfig = 1;
#ifdef DRACO
if (is_draco()) {
*draco_intena &= ~DRIRQ_GLOBAL;
} else
#endif
custom.intena = INTF_INTEN;
s = splhigh();
if (config_rootfound("mainbus", NULL) == NULL)
panic("no mainbus found");
#ifdef DEBUG_KERNEL_START
printf("survived autoconf, going to enable interrupts\n");
#endif
#ifdef DRACO
if (is_draco()) {
*draco_intena |= DRIRQ_GLOBAL;
} else
#endif
{
custom.intena = INTF_SETCLR | INTF_INTEN;
custom.intena = INTF_SETCLR | INTF_SOFTINT;
}
#ifdef DEBUG_KERNEL_START
for (i=splhigh(); i>=s ;i-=0x100) {
splx(i);
printf("%d...", (i>>8) & 7);
}
printf("survived interrupt enable\n");
#else
splx(s);
#endif
#ifdef DEBUG_KERNEL_START
printf("survived configure...\n");
#endif
}
void
cpu_rootconf(void)
{
findroot();
#ifdef DEBUG_KERNEL_START
printf("survived findroot()\n");
#endif
rootconf();
}
int
simple_devprint(void *aux, const char *pnp)
{
return(QUIET);
}
int
matchname(const char *fp, const char *sp)
{
int len;
len = strlen(fp);
if (strlen(sp) != len)
return(0);
if (memcmp(fp, sp, len) == 0)
return(1);
return(0);
}
int
amiga_config_found(cfdata_t pcfp, device_t parent, void *aux, cfprint_t pfn,
const struct cfargs *cfargs)
{
struct device temp;
cfdata_t cf;
const struct cfattach *ca;
int rv = 0;
if (amiga_realconfig) {
rv = config_found(parent, aux, pfn, cfargs) != NULL;
goto out;
}
if (parent == NULL) {
memset(&temp, 0, sizeof temp);
parent = &temp;
}
parent->dv_cfdata = pcfp;
parent->dv_cfdriver = config_cfdriver_lookup(pcfp->cf_name);
parent->dv_unit = pcfp->cf_unit;
if ((cf = config_search(parent, aux, cfargs)) != NULL) {
ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
if (ca != NULL) {
(*ca->ca_attach)(parent, NULL, aux);
rv = 1;
}
}
parent->dv_cfdata = NULL;
out:
return rv;
}
void
config_console(void)
{
cfdata_t cf;
config_init();
cf = config_rootsearch(NULL, "mainbus", NULL);
if (cf == NULL) {
panic("no mainbus");
}
amiga_config_found(cf, NULL, __UNCONST("clock"), NULL, CFARGS_NONE);
#ifdef DRACO
if (!(is_draco()))
#endif
amiga_config_found(cf, NULL, __UNCONST("grfcc"), NULL,
CFARGS_NONE);
amiga_config_found(cf, NULL, __UNCONST("zbus"), NULL, CFARGS_NONE);
}
CFATTACH_DECL_NEW(mainbus, 0,
mbmatch, mbattach, NULL, NULL);
int
mbmatch(device_t parent, cfdata_t cf, void *aux)
{
#if 0
static int mainbus_matched = 0;
if (mainbus_matched)
return (0);
mainbus_matched = 1;
#endif
return (1);
}
void
mbattach(device_t parent, device_t self, void *aux)
{
printf("\n");
config_found(self, __UNCONST("clock"), simple_devprint, CFARGS_NONE);
if (is_a3000() || is_a4000()) {
config_found(self, __UNCONST("a34kbbc"), simple_devprint,
CFARGS_NONE);
} else
#ifdef DRACO
if (!is_draco())
#endif
{
config_found(self, __UNCONST("a2kbbc"), simple_devprint,
CFARGS_NONE);
}
#ifdef DRACO
if (is_draco()) {
config_found(self, __UNCONST("drbbc"), simple_devprint,
CFARGS_NONE);
config_found(self, __UNCONST("kbd"), simple_devprint,
CFARGS_NONE);
config_found(self, __UNCONST("drsc"), simple_devprint,
CFARGS_NONE);
config_found(self, __UNCONST("drsupio"), simple_devprint,
CFARGS_NONE);
} else
#endif
{
config_found(self, __UNCONST("ser"), simple_devprint,
CFARGS_NONE);
config_found(self, __UNCONST("par"), simple_devprint,
CFARGS_NONE);
config_found(self, __UNCONST("kbd"), simple_devprint,
CFARGS_NONE);
config_found(self, __UNCONST("ms"), simple_devprint,
CFARGS_NONE);
config_found(self, __UNCONST("grfcc"), simple_devprint,
CFARGS_NONE);
config_found(self, __UNCONST("amidisplaycc"), simple_devprint,
CFARGS_NONE);
config_found(self, __UNCONST("fdc"), simple_devprint,
CFARGS_NONE);
}
if (is_a4000() || is_a1200() || is_a600())
config_found(self, __UNCONST("wdc"), simple_devprint,
CFARGS_NONE);
if (is_a4000())
config_found(self, __UNCONST("afsc"), simple_devprint,
CFARGS_NONE);
if (is_a3000())
config_found(self, __UNCONST("ahsc"), simple_devprint,
CFARGS_NONE);
if (is_a600() || is_a1200())
config_found(self, __UNCONST("pccard"), simple_devprint,
CFARGS_NONE);
if (is_a1200())
config_found(self, __UNCONST("a1k2cp"), simple_devprint,
CFARGS_NONE);
#ifdef DRACO
if (!is_draco())
#endif
config_found(self, __UNCONST("aucc"), simple_devprint,
CFARGS_NONE);
#if NACAFH > 0
if (!is_a600() && !is_a1200() && !is_a3000() && !is_a4000())
if (acafh_mbattach_probe() == true)
config_found(self, __UNCONST("acafh"), simple_devprint,
CFARGS_NONE);
#endif
config_found(self, __UNCONST("zbus"), simple_devprint, CFARGS_NONE);
}
int
mbprint(void *aux, const char *pnp)
{
if (pnp)
aprint_normal("%s at %s", (char *)aux, pnp);
return(UNCONF);
}
#include <sys/fcntl.h>
#include <sys/proc.h>
#include "fd.h"
#include "sd.h"
#include "cd.h"
#include "wd.h"
#if NFD > 0
extern struct cfdriver fd_cd;
extern const struct bdevsw fd_bdevsw;
#endif
#if NSD > 0
extern struct cfdriver sd_cd;
extern const struct bdevsw sd_bdevsw;
#endif
#if NCD > 0
extern struct cfdriver cd_cd;
extern const struct bdevsw cd_bdevsw;
#endif
#if NWD > 0
extern struct cfdriver wd_cd;
extern const struct bdevsw wd_bdevsw;
#endif
struct cfdriver *genericconf[] = {
#if NFD > 0
&fd_cd,
#endif
#if NSD > 0
&sd_cd,
#endif
#if NWD > 0
&wd_cd,
#endif
#if NCD > 0
&cd_cd,
#endif
NULL,
};
void
findroot(void)
{
struct disk *dkp;
struct partition *pp;
device_t *devs;
int i, maj, unit;
const struct bdevsw *bdp;
#if NSD > 0
#ifdef DEBUG_KERNEL_START
printf("Boot partition offset is %ld\n", boot_partition);
#endif
if (boot_partition != 0) {
for (unit = 0; unit < sd_cd.cd_ndevs; ++unit) {
#ifdef DEBUG_KERNEL_START
printf("probing for sd%d\n", unit);
#endif
if (device_lookup(&sd_cd,unit) == NULL)
continue;
devs = sd_cd.cd_devs;
if ((dkp = disk_find(device_xname(device_lookup(&sd_cd, unit)))) == NULL)
continue;
if (dkp->dk_driver == NULL ||
dkp->dk_driver->d_strategy == NULL)
continue;
bdp = &sd_bdevsw;
maj = bdevsw_lookup_major(bdp);
if ((*bdp->d_open)(MAKEDISKDEV(maj, unit, RAW_PART),
FREAD | FNONBLOCK, 0, curlwp))
continue;
(*bdp->d_close)(MAKEDISKDEV(maj, unit, RAW_PART),
FREAD | FNONBLOCK, 0, curlwp);
pp = &dkp->dk_label->d_partitions[0];
for (i = 0; i < dkp->dk_label->d_npartitions;
i++, pp++) {
#ifdef DEBUG_KERNEL_START
printf("sd%d%c type %d offset %d size %d\n",
unit, i+'a', pp->p_fstype,
pp->p_offset, pp->p_size);
#endif
if (pp->p_size == 0 ||
(pp->p_fstype != FS_BSDFFS &&
pp->p_fstype != FS_SWAP))
continue;
if (pp->p_offset == boot_partition) {
if (booted_device == NULL) {
booted_device = devs[unit];
booted_partition = i;
} else
printf("Ambiguous boot device\n");
}
}
}
}
if (booted_device != NULL)
return;
#endif
for (i = 0; genericconf[i] != NULL; i++) {
for (unit = genericconf[i]->cd_ndevs - 1; unit >= 0; unit--) {
if (genericconf[i]->cd_devs[unit] == NULL)
continue;
devs = (device_t *)genericconf[i]->cd_devs;
if ((dkp = disk_find(device_xname(devs[unit]))) == NULL)
continue;
if (dkp->dk_driver == NULL ||
dkp->dk_driver->d_strategy == NULL)
continue;
bdp = NULL;
#if NFD > 0
if (fd_bdevsw.d_strategy == dkp->dk_driver->d_strategy)
bdp = &fd_bdevsw;
#endif
#if NSD > 0
if (sd_bdevsw.d_strategy == dkp->dk_driver->d_strategy)
bdp = &sd_bdevsw;
#endif
#if NWD > 0
if (wd_bdevsw.d_strategy == dkp->dk_driver->d_strategy)
bdp = &wd_bdevsw;
#endif
#if NCD > 0
if (cd_bdevsw.d_strategy == dkp->dk_driver->d_strategy)
bdp = &cd_bdevsw;
#endif
#ifdef DIAGNOSTIC
if (bdp == NULL)
panic("findroot: impossible");
#endif
maj = bdevsw_lookup_major(bdp);
if ((*bdp->d_open)(MAKEDISKDEV(maj, unit, RAW_PART),
FREAD|FNONBLOCK, 0, &lwp0))
continue;
(void)(*bdp->d_close)(MAKEDISKDEV(maj, unit, RAW_PART),
FREAD|FNONBLOCK, 0, &lwp0);
pp = &dkp->dk_label->d_partitions[0];
if (pp->p_size != 0 && pp->p_fstype == FS_BSDFFS) {
booted_device = devs[unit];
booted_partition = 0;
return;
}
}
}
}
int a3000_flag = 1;
#ifdef A4000
int a4000_flag = 1;
#else
int a4000_flag = 0;
#endif
int
is_a3000(void)
{
extern long boot_fphystart;
short sc;
if ((machineid >> 16) == 3000)
return (1);
if (machineid >> 16)
return (0);
if (boot_fphystart < 0x07000000)
return(0);
for (sc = 0; sc < ncfdev; sc++) {
switch (cfdev[sc].rom.manid) {
case 2026:
switch (cfdev[sc].rom.prodid) {
case 0:
case 1:
return(1);
case 150:
case 105:
case 187:
return(0);
}
break;
case 2112:
switch (cfdev[sc].rom.prodid) {
case 242:
return(0);
}
break;
}
}
return (a3000_flag);
}
int
is_a4000(void)
{
if ((machineid >> 16) == 4000)
return (1);
if ((machineid >> 16) == 1200)
return (0);
#ifdef DRACO
if (is_draco())
return (0);
#endif
if ((custom.deniseid & 0xff) == 0xf8)
return (1);
#ifdef DEBUG
if (a4000_flag)
printf("Denise ID = %04x\n", (unsigned short)custom.deniseid);
#endif
if (machineid >> 16)
return (0);
return (a4000_flag);
}
int
is_a1200(void)
{
if ((machineid >> 16) == 1200)
return (1);
return (0);
}
int
is_a600(void)
{
if ((machineid >> 16) == 600)
return (1);
return (0);
}
void
device_register(device_t dev, void *aux)
{
#ifdef P5PB_CONSOLE
p5pb_device_register(dev, aux);
#endif
}