#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.134 2026/04/28 03:29:09 thorpej Exp $");
#include "opt_ddb.h"
#include "opt_kgdb.h"
#include "opt_modular.h"
#include "opt_panicbutton.h"
#include "opt_m68k_arch.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/reboot.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/device.h>
#include <sys/mbuf.h>
#include <sys/msgbuf.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/mount.h>
#include <sys/exec.h>
#include <sys/core.h>
#include <sys/kauth.h>
#include <sys/kcore.h>
#include <sys/vnode.h>
#include <sys/syscallargs.h>
#include <sys/ksyms.h>
#include <sys/module.h>
#ifdef KGDB
#include <sys/kgdb.h>
#endif
#include <sys/boot_flag.h>
#include <sys/exec_elf.h>
#include <sys/cpu.h>
#include <uvm/uvm_extern.h>
#include <sys/sysctl.h>
#include <machine/bootinfo.h>
#include <machine/board.h>
#include <machine/cpu.h>
#include <machine/reg.h>
#include <machine/pcb.h>
#include <machine/psl.h>
#include <machine/pte.h>
#include <machine/kcore.h>
#include <m68k/seglist.h>
#include <luna68k/dev/siottyvar.h>
#include <dev/cons.h>
#include <dev/mm.h>
#if defined(DDB)
#include <machine/db_machdep.h>
#include <ddb/db_sym.h>
#include <ddb/db_extern.h>
#endif
#include "ksyms.h"
int maxmem;
extern u_int lowram;
void machine_init(paddr_t);
void nmihand(struct frame);
#if NKSYMS || defined(DDB) || defined(MODULAR)
vsize_t symtab_size(vaddr_t);
#endif
extern char end[];
extern void *esym;
int machtype;
int sysconsole;
extern void omfb_cnattach(void);
extern void ws_cnattach(void);
int delay_divisor = delay_divisor_est40(25);
struct pmap_bootmap machine_bootmap[] = {
{ .pmbm_vaddr = LUNA68K_IO0_TT_BASE,
.pmbm_size = LUNA68K_IO0_TT_SIZE,
.pmbm_flags = PMBM_F_KEEPOUT },
{ .pmbm_vaddr = LUNA68K_IO1_TT_BASE,
.pmbm_size = LUNA68K_IO1_TT_SIZE,
.pmbm_flags = PMBM_F_KEEPOUT },
{ .pmbm_vaddr = -1 },
};
void
machine_init(paddr_t nextpa)
{
volatile uint8_t *pio0 = (void *)OBIO_PIO0_BASE;
int sw1;
char *cp;
extern char bootarg[64];
#if 1
cn_tab = &siottycons;
#else
cn_tab = &romcons;
#endif
phys_seg_list[0].ps_start = 0;
phys_seg_list[0].ps_end = m68k_ptob(maxmem);
machine_init_common(nextpa);
pio0[3] = 0xb6;
pio0[2] = 1 << 6;
pio0[3] = 0xb6;
sw1 = pio0[0];
sw1 ^= 0xff;
sysconsole = !(sw1 & 0x2);
if ((bootdev & B_MAGICMASK) == B_DEVMAGIC) {
return;
}
bootdev = MAKEBOOTDEV(0, LUNA68K_BOOTADPT_SPC, 0, 0, 0);
boothowto = 0;
bootarg[63] = '\0';
for (cp = bootarg; *cp != '\0'; cp++) {
if (*cp == '-') {
char c;
while ((c = *cp) != '\0' && c != ' ') {
BOOT_FLAG(c, boothowto);
cp++;
}
} else if (*cp == 'E' && memcmp("ENADDR=", cp, 7) == 0) {
bootdev =
MAKEBOOTDEV(0, LUNA68K_BOOTADPT_LANCE, 0, 0, 0);
}
}
}
void
consinit(void)
{
if (sysconsole == 0) {
cn_tab = &siottycons;
(*cn_tab->cn_init)(cn_tab);
} else {
omfb_cnattach();
ws_cnattach();
}
#if NKSYMS || defined(DDB) || defined(MODULAR)
ksyms_addsyms_elf((esym != NULL) ? 1 : 0, (void *)&end, esym);
#endif
#ifdef DDB
if (boothowto & RB_KDB)
cpu_Debugger();
#endif
}
#if NKSYMS || defined(DDB) || defined(MODULAR)
vsize_t
symtab_size(vaddr_t hdr)
{
int i;
Elf_Ehdr *ehdr;
Elf_Shdr *shp;
vaddr_t maxsym;
ehdr = (void *)hdr;
if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
ehdr->e_ident[EI_MAG3] != ELFMAG3 ||
ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
return 0;
}
maxsym = 0;
shp = (Elf_Shdr *)(hdr + ehdr->e_shoff);
for (i = 0; i < ehdr->e_shnum; i++) {
if (shp[i].sh_type != SHT_SYMTAB &&
shp[i].sh_type != SHT_STRTAB) {
continue;
}
maxsym = uimax(maxsym, shp[i].sh_offset + shp[i].sh_size);
}
return maxsym;
}
#endif
void
machine_set_model(void)
{
switch (cputype) {
#ifdef M68030
case CPU_68030:
cpu_setmodel("LUNA-I");
machtype = LUNA_I;
cpuspeed_khz = 20*1000;
delay_divisor = delay_divisor_est(20);
hz = 60;
break;
#endif
#ifdef M68040
case CPU_68040:
cpu_setmodel("LUNA-II");
machtype = LUNA_II;
cpuspeed_khz = 25*1000;
delay_divisor = delay_divisor_est40(25);
break;
#endif
default:
panic("unknown CPU type");
}
}
SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
{
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "machdep", NULL,
NULL, 0, NULL, 0,
CTL_MACHDEP, CTL_EOL);
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_STRUCT, "console_device", NULL,
sysctl_consdev, 0, NULL, sizeof(dev_t),
CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
}
void
machine_powerdown(void)
{
volatile uint8_t *pio = (void *)OBIO_PIO1_BASE;
printf("power is going down.\n");
DELAY(100000);
pio[3] = 0x94;
pio[2] = 0 << 4;
for (;;)
;
}
void luna68k_abort(const char *);
static int innmihand;
void
nmihand(struct frame frame)
{
if (innmihand)
return;
innmihand = 1;
luna68k_abort("ABORT SWITCH");
innmihand = 0;
}
void
luna68k_abort(const char *cp)
{
#ifdef DDB
printf("%s\n", cp);
cpu_Debugger();
#else
#ifdef PANICBUTTON
panic(cp);
#else
printf("%s ignored\n", cp);
#endif
#endif
}
#ifdef notyet
int romcngetc(dev_t);
void romcnputc(dev_t, int);
struct consdev romcons = {
NULL,
NULL,
romcngetc,
romcnputc,
nullcnpollc,
makedev(7, 0),
CN_DEAD,
};
#define __ ((int **)PROM_ADDR)
#define GETC() (*(int (*)())__[6])()
#define PUTC(x) (*(void (*)())__[7])(x)
#define ROMPUTC(x) \
({ \
register _r; \
__asm volatile (" \
movc %%vbr,%0 ; \
movel %0,%%sp@- ; \
clrl %0 ; \
movc %0,%%vbr" \
: "=r" (_r)); \
PUTC(x); \
__asm volatile (" \
movel %%sp@+,%0 ; \
movc %0,%%vbr" \
: "=r" (_r)); \
})
#define ROMGETC() \
({ \
register _r, _c; \
__asm volatile (" \
movc %%vbr,%0 ; \
movel %0,%%sp@- ; \
clrl %0 ; \
movc %0,%%vbr" \
: "=r" (_r)); \
_c = GETC(); \
__asm volatile (" \
movel %%sp@+,%0 ; \
movc %0,%%vbr" \
: "=r" (_r)); \
_c; \
})
void
romcnputc(dev_t dev, int c)
{
int s;
s = splhigh();
ROMPUTC(c);
splx(s);
}
int
romcngetc(dev_t dev)
{
int s, c;
do {
s = splhigh();
c = ROMGETC();
splx(s);
} while (c == -1);
return c;
}
#endif