#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.18 2025/12/20 10:51:02 skrll Exp $");
#include "opt_ddb.h"
#include "opt_kgdb.h"
#include "opt_modular.h"
#include "opt_multiprocessor.h"
#include <sys/param.h>
#include <sys/boot_flag.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/kcore.h>
#include <sys/ksyms.h>
#include <sys/mount.h>
#include <sys/reboot.h>
#include <sys/cpu.h>
#include <sys/bus.h>
#include <sys/mutex.h>
#include <uvm/uvm_extern.h>
#include <dev/cons.h>
#include "ksyms.h"
#if NKSYMS || defined(DDB) || defined(MODULAR)
#include <mips/db_machdep.h>
#include <ddb/db_extern.h>
#endif
#include <mips/cache.h>
#include <mips/locore.h>
#include <mips/cpuregs.h>
#include <mips/ingenic/ingenic_coreregs.h>
#include <mips/ingenic/ingenic_regs.h>
#include <mips/ingenic/ingenic_var.h>
#include "opt_ingenic.h"
struct vm_map *phys_map = NULL;
int maxmem;
int mem_cluster_cnt;
phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
void mach_init(void);
void ingenic_reset(void);
void ingenic_putchar_init(void);
void ingenic_puts(const char *);
void ingenic_com_cnattach(void);
#ifdef MULTIPROCESSOR
kmutex_t ingenic_ipi_lock;
#endif
CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN);
static void
cal_timer(void)
{
uint32_t cntfreq;
volatile uint32_t junk;
curcpu()->ci_cpu_freq = 1200000000;
cntfreq = 12000000;
curcpu()->ci_cctr_freq = cntfreq;
curcpu()->ci_cycles_per_hz = (cntfreq + hz / 2) / hz;
curcpu()->ci_divisor_delay = ((cntfreq + 500000) / 1000000);
writereg(JZ_TC_TECR, TESR_OST);
writereg(JZ_OST_CTRL, 0);
writereg(JZ_OST_CNT_LO, 0);
writereg(JZ_OST_CNT_HI, 0);
writereg(JZ_OST_DATA, 0xffffffff);
writereg(JZ_OST_CTRL, OSTC_EXT_EN | OSTC_MODE | OSTC_DIV_4);
writereg(JZ_TC_TESR, TESR_OST);
junk = readreg(JZ_OST_CNT_LO);
do {} while (junk == readreg(JZ_OST_CNT_LO));
}
#ifdef MULTIPROCESSOR
static void
ingenic_cpu_init(struct cpu_info *ci)
{
uint32_t reg;
reg = mips_cp0_corereim_read();
if (cpu_index(ci) == 1) {
reg |= REIM_MIRQ1_M;
} else
reg |= REIM_MIRQ0_M;
mips_cp0_corereim_write(reg);
printf("%s %d %08x\n", __func__, cpu_index(ci), reg);
}
static int
ingenic_send_ipi(struct cpu_info *ci, int tag)
{
uint32_t msg;
msg = 1 << tag;
mutex_enter(&ingenic_ipi_lock);
if (kcpuset_isset(cpus_running, cpu_index(ci))) {
if (cpu_index(ci) == 0) {
mips_cp0_corembox_write(msg, 0);
} else {
mips_cp0_corembox_write(msg, 1);
}
}
mutex_exit(&ingenic_ipi_lock);
return 0;
}
#endif
void
mach_init(void)
{
void *kernend;
uint32_t memsize;
extern char edata[], end[];
kernend = (void *)mips_round_page(end);
memset(edata, 0, (char *)kernend - edata);
ingenic_putchar_init();
cpu_setmodel("Ingenic XBurst");
mips_vector_init(NULL, false);
cal_timer();
uvm_md_init();
boothowto = RB_AUTOBOOT;
#ifdef KADB
boothowto |= RB_KDB;
#endif
memsize = 0x40000000;
printf("Memory size: 0x%08x\n", memsize);
physmem = btoc(memsize);
mem_clusters[0].start = PAGE_SIZE;
mem_clusters[0].size = 0x10000000 - PAGE_SIZE;
mem_clusters[1].start = 0x30000000;
mem_clusters[1].size = 0x30000000;
mem_cluster_cnt = 2;
mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
mem_clusters, mem_cluster_cnt, NULL, 0);
mips_init_msgbuf();
pmap_bootstrap();
mips_init_lwp0_uarea();
#ifdef MULTIPROCESSOR
mutex_init(&ingenic_ipi_lock, MUTEX_DEFAULT, IPL_HIGH);
mips_locoresw.lsw_send_ipi = ingenic_send_ipi;
mips_locoresw.lsw_cpu_init = ingenic_cpu_init;
#endif
apbus_init();
#ifdef DDB
if (boothowto & RB_KDB)
Debugger();
#endif
}
void
consinit(void)
{
apbus_init();
ingenic_com_cnattach();
}
void
cpu_startup(void)
{
cpu_startup_common();
}
void
cpu_reboot(int howto, char *bootstr)
{
static int waittime = -1;
savectx(curpcb);
if (boothowto & RB_HALT)
howto |= RB_HALT;
boothowto = howto;
if (cold) {
boothowto |= RB_HALT;
goto haltsys;
}
if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
waittime = 0;
vfs_shutdown();
}
splhigh();
if (boothowto & RB_DUMP)
dumpsys();
haltsys:
doshutdownhooks();
pmf_system_shutdown(boothowto);
#if 0
if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN)
if (board && board->ab_poweroff)
board->ab_poweroff();
#endif
if (boothowto & RB_HALT) {
printf("\n");
printf("The operating system has halted.\n");
printf("Please press any key to reboot.\n\n");
cnpollc(true);
cngetc();
cnpollc(false);
}
printf("resetting board...\n\n");
mips_icache_sync_all();
mips_dcache_wbinv_all();
ingenic_reset();
__asm volatile("jr %0" :: "r"(MIPS_RESET_EXC_VEC));
printf("Oops, back from reset\n\nSpinning...");
for (;;)
;
}
void
ingenic_reset(void)
{
writereg(JZ_WDOG_TCER, 0);
writereg(JZ_WDOG_TCNT, 0);
writereg(JZ_WDOG_TDR, 128);
writereg(JZ_WDOG_TCSR, TCSR_RTC_EN | TCSR_DIV_256);
writereg(JZ_WDOG_TCER, TCER_ENABLE);
}