#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.9 2023/04/20 08:28:03 skrll Exp $");
#include "clpscom.h"
#include "clpslcd.h"
#include "wmcom.h"
#include "wmlcd.h"
#include "epockbd.h"
#include "ksyms.h"
#include "opt_ddb.h"
#include "opt_md.h"
#include "opt_modular.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/lwp.h>
#include <sys/pmf.h>
#include <sys/reboot.h>
#include <sys/termios.h>
#include <uvm/uvm_extern.h>
#include <dev/cons.h>
#include <dev/md.h>
#include <arm/locore.h>
#include <arm/undefined.h>
#include <arm/arm32/machdep.h>
#include <arm/arm32/pmap.h>
#include <machine/bootconfig.h>
#include <machine/bootinfo.h>
#include <machine/epoc32.h>
#include <arm/clps711x/clpssocvar.h>
#include <epoc32/windermere/windermerevar.h>
#include <epoc32/windermere/windermerereg.h>
#include <epoc32/dev/epockbdvar.h>
#include <machine/db_machdep.h>
#include <ddb/db_extern.h>
#define KERNEL_OFFSET 0x00030000
#define KERNEL_TEXT_BASE (KERNEL_BASE + KERNEL_OFFSET)
#ifndef KERNEL_VM_BASE
#define KERNEL_VM_BASE (KERNEL_BASE + 0x00300000)
#endif
#define KERNEL_VM_SIZE 0x04000000
#define IRQ_STACK_SIZE 1
#define ABT_STACK_SIZE 1
#define UND_STACK_SIZE 1
BootConfig bootconfig;
static char bootargs[256];
char *boot_args = NULL;
vaddr_t physical_start;
vaddr_t physical_freestart;
vaddr_t physical_freeend;
vaddr_t physical_end;
u_int free_pages;
paddr_t msgbufphys;
enum {
KERNEL_PT_SYS = 0,
KERNEL_PT_KERNEL,
NUM_KERNEL_PTS
};
pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
char epoc32_model[256];
int epoc32_fb_width;
int epoc32_fb_height;
int epoc32_fb_addr;
static const struct pmap_devmap epoc32_devmap[] = {
DEVMAP_ENTRY(
ARM7XX_INTRREG_VBASE,
ARM7XX_INTRREG_BASE,
ARM7XX_INTRREG_SIZE
),
DEVMAP_ENTRY_END
};
static const struct pmap_devmap epoc32_fb_devmap[] = {
DEVMAP_ENTRY(
ARM7XX_FB_VBASE,
ARM7XX_FB_BASE,
ARM7XX_FB_SIZE
),
DEVMAP_ENTRY_END
};
vaddr_t
initarm(void *arg)
{
extern char _end[];
extern vaddr_t startup_pagetable;
extern struct btinfo_common bootinfo;
struct btinfo_common *btinfo = &bootinfo;
struct btinfo_model *model = NULL;
struct btinfo_memory *memory = NULL;
struct btinfo_video *video = NULL;
struct btinfo_bootargs *args = NULL;
u_int l1pagetable, _end_physical;
int loop, loop1, n, i;
if (set_cpufuncs())
panic("cpu not recognized!");
pmap_devmap_bootstrap(startup_pagetable, epoc32_devmap);
bootconfig.dramblocks = 0;
while (btinfo->type != BTINFO_NONE) {
switch (btinfo->type) {
case BTINFO_MODEL:
model = (struct btinfo_model *)btinfo;
btinfo = &(model + 1)->common;
strncpy(epoc32_model, model->model,
sizeof(epoc32_model));
break;
case BTINFO_MEMORY:
memory = (struct btinfo_memory *)btinfo;
btinfo = &(memory + 1)->common;
i = bootconfig.dramblocks;
bootconfig.dram[i].address = memory->address;
bootconfig.dram[i].pages = memory->size / PAGE_SIZE;
bootconfig.dramblocks++;
break;
case BTINFO_VIDEO:
video = (struct btinfo_video *)btinfo;
btinfo = &(video + 1)->common;
epoc32_fb_width = video->width;
epoc32_fb_height = video->height;
break;
case BTINFO_BOOTARGS:
args = (struct btinfo_bootargs *)btinfo;
btinfo = &(args + 1)->common;
memcpy(bootargs, args->bootargs,
uimin(sizeof(bootargs), sizeof(args->bootargs)));
bootargs[sizeof(bootargs) - 1] = '\0';
boot_args = bootargs;
break;
default:
#define NEXT_BOOTINFO(bi) (struct btinfo_common *)((char *)bi + (bi)->len)
btinfo = NEXT_BOOTINFO(btinfo);
}
}
if (bootconfig.dramblocks == 0)
panic("BTINFO_MEMORY not found");
consinit();
if (boot_args != NULL)
parse_mi_bootargs(boot_args);
physical_start = bootconfig.dram[0].address;
physical_freestart = bootconfig.dram[0].address;
physical_freeend = KERNEL_TEXT_BASE;
free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
#define valloc_pages(var, np) \
alloc_pages((var).pv_pa, (np)); \
(var).pv_va = KERNEL_BASE + (var).pv_pa - physical_start;
#define alloc_pages(var, np) \
physical_freeend -= ((np) * PAGE_SIZE); \
if (physical_freeend < physical_freestart) \
panic("initarm: out of memory"); \
(var) = physical_freeend; \
free_pages -= (np); \
memset((char *)(var), 0, ((np) * PAGE_SIZE));
loop1 = 0;
for (loop = 0; loop <= NUM_KERNEL_PTS; ++loop) {
if (((physical_freeend - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) == 0
&& kernel_l1pt.pv_pa == 0) {
valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
} else {
valloc_pages(kernel_pt_table[loop1],
L2_TABLE_SIZE / PAGE_SIZE);
++loop1;
}
}
if (!kernel_l1pt.pv_pa ||
(kernel_l1pt.pv_pa & (L1_TABLE_SIZE - 1)) != 0)
panic("initarm: Failed to align the kernel page directory");
alloc_pages(systempage.pv_pa, 1);
valloc_pages(irqstack, IRQ_STACK_SIZE);
valloc_pages(abtstack, ABT_STACK_SIZE);
valloc_pages(undstack, UND_STACK_SIZE);
valloc_pages(kernelstack, UPAGES);
alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
l1pagetable = kernel_l1pt.pv_va;
pmap_link_l2pt(l1pagetable, 0x00000000,
&kernel_pt_table[KERNEL_PT_SYS]);
pmap_link_l2pt(l1pagetable, KERNEL_BASE,
&kernel_pt_table[KERNEL_PT_KERNEL]);
pmap_curmaxkvaddr = KERNEL_VM_BASE;
{
extern char etext[];
size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
size_t datasize;
PhysMem *dram = bootconfig.dram;
u_int logical, physical, size;
textsize = (textsize + PGOFSET) & ~PGOFSET;
totalsize = (totalsize + PGOFSET) & ~PGOFSET;
datasize = totalsize - textsize;
logical = KERNEL_OFFSET;
physical = KERNEL_OFFSET;
i = 0;
size = dram[i].pages * PAGE_SIZE - physical;
while (1 ) {
size = pmap_map_chunk(l1pagetable,
KERNEL_BASE + logical, dram[i].address + physical,
textsize < size ? textsize : size,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
logical += size;
physical += size;
textsize -= size;
if (physical >= dram[i].pages * PAGE_SIZE) {
i++;
size = dram[i].pages * PAGE_SIZE;
physical = 0;
}
if (textsize == 0)
break;
}
size = dram[i].pages * PAGE_SIZE - physical;
while (1 ) {
size = pmap_map_chunk(l1pagetable,
KERNEL_BASE + logical, dram[i].address + physical,
datasize < size ? datasize : size,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
logical += size;
physical += size;
datasize -= size;
if (physical >= dram[i].pages * PAGE_SIZE) {
i++;
size = dram[i].pages * PAGE_SIZE;
physical = 0;
}
if (datasize == 0)
break;
}
_end_physical = dram[i].address + physical;
n = i;
physical_end = dram[n].address + dram[n].pages * PAGE_SIZE;
n++;
}
pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
UPAGES * PAGE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
L1_TABLE_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_PAGETABLE);
for (loop = 0; loop < NUM_KERNEL_PTS; ++loop)
pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
pmap_devmap_bootstrap(l1pagetable, epoc32_devmap);
pmap_devmap_bootstrap(l1pagetable, epoc32_fb_devmap);
epoc32_fb_addr = ARM7XX_FB_VBASE;
cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
cpu_setttb(kernel_l1pt.pv_pa, true);
cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
set_stackptr(PSR_IRQ32_MODE,
irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
set_stackptr(PSR_ABT32_MODE,
abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
set_stackptr(PSR_UND32_MODE,
undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
data_abort_handler_address = (u_int)data_abort_handler;
prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
undefined_handler_address = (u_int)undefinedinstruction_bounce;
undefined_init();
uvm_md_init();
uvm_page_physload(
atop(_end_physical), atop(physical_end),
atop(_end_physical), atop(physical_end),
VM_FREELIST_DEFAULT);
physmem = bootconfig.dram[0].pages;
for (i = 1; i < n; i++)
physmem += bootconfig.dram[i].pages;
if (physmem < 0x400000)
physical_end = 0;
for (loop = n; loop < bootconfig.dramblocks; loop++) {
size_t start = bootconfig.dram[loop].address;
size_t size = bootconfig.dram[loop].pages * PAGE_SIZE;
uvm_page_physload(atop(start), atop(start + size),
atop(start), atop(start + size), VM_FREELIST_DEFAULT);
physmem += bootconfig.dram[loop].pages;
if (physical_end == 0 && physmem >= 0x400000 / PAGE_SIZE)
physical_end = start + size;
}
pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
#ifdef __HAVE_MEMORY_DISK__
md_root_setconf(memory_disk, sizeof memory_disk);
#endif
#if NKSYMS || defined(DDB) || defined(MODULAR)
ddb_init(0, NULL, NULL);
#endif
#ifdef DDB
db_machine_init();
if (boothowto & RB_KDB)
Debugger();
#endif
return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
}
void
cpu_reboot(int howto, char *bootstr)
{
#ifdef DIAGNOSTIC
printf("boot: howto=%08x curproc=%p\n", howto, curproc);
#endif
if (cold) {
doshutdownhooks();
pmf_system_shutdown(boothowto);
printf("The operating system has halted.\n");
printf("Please press any key to reboot.\n\n");
cngetc();
printf("rebooting...\n");
cpu_reset();
}
if (!(howto & RB_NOSYNC))
bootsync();
splhigh();
if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
dumpsys();
doshutdownhooks();
pmf_system_shutdown(boothowto);
IRQdisable;
if (howto & RB_HALT) {
printf("The operating system has halted.\n");
printf("Please press any key to reboot.\n\n");
cngetc();
}
printf("rebooting...\n");
cpu_reset();
}
void
consinit(void)
{
static int consinit_called = 0;
#if (NWMCOM + NCLPSCOM) > 0
const tcflag_t mode = (TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8;
#endif
if (consinit_called)
return;
consinit_called = 1;
if (strcmp(epoc32_model, "SERIES5 R1") == 0) {
#if NCLPSLCD > 0
if (clpslcd_cnattach() == 0) {
#if NEPOCKBD > 0
epockbd_cnattach();
#endif
return;
}
#endif
#if NCLPSCOM > 0
if (clpscom_cnattach(ARM7XX_INTRREG_VBASE, 115200, mode) == 0)
return;
#endif
}
if (strcmp(epoc32_model, "SERIES5mx") == 0) {
vaddr_t vbase = ARM7XX_INTRREG_VBASE;
#if NWMCOM > 0
vaddr_t offset;
volatile uint8_t *gpio;
int irda;
#endif
#if NWMLCD > 0
if (wmlcd_cnattach() == 0) {
#if NEPOCKBD > 0
epockbd_cnattach();
#endif
return;
}
#endif
#if NWMCOM > 0
gpio = (uint8_t *)ARM7XX_INTRREG_VBASE + WINDERMERE_GPIO_OFFSET;
if (0) {
*(gpio + 0x08) |= 1 << 5;
offset = WINDERMERE_COM0_OFFSET;
irda = 1;
} else {
*(gpio + 0x08) |= 1 << 3;
offset = WINDERMERE_COM1_OFFSET;
irda = 0;
}
if (wmcom_cnattach(vbase + offset, 115200, mode, irda) == 0)
return;
#endif
}
if (strcmp(epoc32_model, "SERIES7") == 0) {
}
panic("can't init console");
}