#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: lubbock_machdep.c,v 1.47 2025/12/22 07:45:46 skrll Exp $");
#include "opt_arm_debug.h"
#include "opt_console.h"
#include "opt_ddb.h"
#include "opt_kgdb.h"
#include "opt_md.h"
#include "opt_com.h"
#include "lcd.h"
#include <sys/param.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/exec.h>
#include <sys/proc.h>
#include <sys/msgbuf.h>
#include <sys/reboot.h>
#include <sys/termios.h>
#include <sys/ksyms.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/conf.h>
#include <uvm/uvm_extern.h>
#include <dev/cons.h>
#include <dev/md.h>
#include <dev/ic/smc91cxxreg.h>
#include <machine/db_machdep.h>
#include <ddb/db_sym.h>
#include <ddb/db_extern.h>
#ifdef KGDB
#include <sys/kgdb.h>
#endif
#include <machine/bootconfig.h>
#include <arm/locore.h>
#include <arm/undefined.h>
#include <arm/arm32/machdep.h>
#include <arm/xscale/pxa2x0reg.h>
#include <arm/xscale/pxa2x0var.h>
#include <arm/xscale/pxa2x0_gpio.h>
#include <arm/sa11x0/sa1111_reg.h>
#include <evbarm/lubbock/lubbock_reg.h>
#include <evbarm/lubbock/lubbock_var.h>
#define KERNEL_TEXT_BASE (KERNEL_BASE + 0x00200000)
#define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000)
#define KERNEL_VM_SIZE 0x0C000000
BootConfig bootconfig;
char *boot_args = NULL;
char *boot_file = NULL;
vaddr_t physical_start;
vaddr_t physical_freestart;
vaddr_t physical_freeend;
vaddr_t physical_end;
u_int free_pages;
#ifndef PMAP_STATIC_L1S
int max_processes = 64;
#endif
pv_addr_t minidataclean;
paddr_t msgbufphys;
#define KERNEL_PT_SYS 0
#define KERNEL_PT_KERNEL 1
#define KERNEL_PT_KERNEL_NUM 4
#define KERNEL_PT_VMDATA (KERNEL_PT_KERNEL+KERNEL_PT_KERNEL_NUM)
#define KERNEL_PT_VMDATA_NUM 4
#define NUM_KERNEL_PTS (KERNEL_PT_VMDATA + KERNEL_PT_VMDATA_NUM)
pv_addr_t kernel_pt_table[NUM_KERNEL_PTS];
#if 0
void process_kernel_args(char *);
#endif
void consinit(void);
void kgdb_port_init(void);
void change_clock(uint32_t v);
bs_protos(bs_notimpl);
#include "com.h"
#if NCOM > 0
#include <dev/ic/comreg.h>
#include <dev/ic/comvar.h>
#endif
#ifndef CONSPEED
#define CONSPEED B115200
#endif
#ifndef CONMODE
#define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
#endif
int comcnspeed = CONSPEED;
int comcnmode = CONMODE;
static struct pxa2x0_gpioconf boarddep_gpioconf[] = {
{ 44, GPIO_ALT_FN_1_IN },
{ 45, GPIO_ALT_FN_2_OUT },
{ 29, GPIO_ALT_FN_1_IN },
{ -1 }
};
static struct pxa2x0_gpioconf *lubbock_gpioconf[] = {
pxa25x_com_btuart_gpioconf,
pxa25x_com_ffuart_gpioconf,
#if 0
pxa25x_com_stuart_gpioconf,
#endif
pxa25x_pcic_gpioconf,
pxa25x_pxaacu_gpioconf,
boarddep_gpioconf,
NULL
};
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();
}
static inline
pd_entry_t *
read_ttb(void)
{
long ttb;
__asm volatile("mrc p15, 0, %0, c2, c0, 0" : "=r" (ttb));
return (pd_entry_t *)(ttb & ~((1<<14)-1));
}
static const struct pmap_devmap lubbock_devmap[] = {
DEVMAP_ENTRY(
LUBBOCK_OBIO_VBASE,
LUBBOCK_OBIO_PBASE,
LUBBOCK_OBIO_SIZE
),
DEVMAP_ENTRY(
LUBBOCK_GPIO_VBASE,
PXA2X0_GPIO_BASE,
PXA250_GPIO_SIZE
),
DEVMAP_ENTRY(
LUBBOCK_CLKMAN_VBASE,
PXA2X0_CLKMAN_BASE,
PXA2X0_CLKMAN_SIZE
),
DEVMAP_ENTRY(
LUBBOCK_INTCTL_VBASE,
PXA2X0_INTCTL_BASE,
PXA2X0_INTCTL_SIZE
),
DEVMAP_ENTRY(
LUBBOCK_FFUART_VBASE,
PXA2X0_FFUART_BASE,
4 * COM_NPORTS
),
DEVMAP_ENTRY(
LUBBOCK_BTUART_VBASE,
PXA2X0_BTUART_BASE,
4 * COM_NPORTS
),
DEVMAP_ENTRY_END
};
vaddr_t
initarm(void *arg)
{
int loop;
int loop1;
u_int l1pagetable;
paddr_t memstart;
psize_t memsize;
int led_data = 0;
#define LEDSTEP_P() ioreg_write(LUBBOCK_OBIO_PBASE+LUBBOCK_HEXLED, led_data++)
#define LEDSTEP() hex_led(led_data++)
LEDSTEP_P();
pmap_devmap_bootstrap((vaddr_t)read_ttb(), lubbock_devmap);
LEDSTEP_P();
ioreg_write(LUBBOCK_CLKMAN_VBASE + 0x08, 2);
pxa2x0_intr_bootstrap(LUBBOCK_INTCTL_VBASE);
LEDSTEP();
if (set_cpufuncs())
panic("cpu not recognized!");
LEDSTEP();
#if 0
#endif
pxa2x0_gpio_bootstrap(LUBBOCK_GPIO_VBASE);
pxa2x0_gpio_config(lubbock_gpioconf);
ioreg_write(LUBBOCK_CLKMAN_VBASE+CLKMAN_CKEN, CKEN_FFUART|CKEN_BTUART |
ioreg_read(LUBBOCK_CLKMAN_VBASE+CLKMAN_CKEN));
LEDSTEP();
consinit();
LEDSTEP();
#ifdef KGDB
kgdb_port_init();
LEDSTEP();
#endif
printf("\nNetBSD/evbarm (lubbock) booting ...\n");
{
uint32_t tmp =
ioreg_read(PXA2X0_MEMCTL_BASE+MEMCTL_MSC1);
ioreg_write(PXA2X0_MEMCTL_BASE+MEMCTL_MSC1,
(tmp & 0xffff) | (0x3881<<16));
}
{
uint32_t tmp;
ioreg_write(PXA2X0_MEMCTL_BASE+MEMCTL_MECR,
MECR_NOS|MECR_CIT);
tmp = ioreg_read(LUBBOCK_SACC_PBASE+SACCSBI_SKCR);
ioreg_write(LUBBOCK_SACC_PBASE+SACCSBI_SKCR,
(tmp & ~(1<<4)) | (1<<0));
}
#if 0
process_kernel_args((char *)nwbootinfo.bt_args);
#endif
{
int processor_card_id;
processor_card_id = 0x000f &
ioreg_read(LUBBOCK_OBIO_VBASE+LUBBOCK_MISCRD);
switch(processor_card_id){
case 0:
memstart = 0xa0000000;
memsize = 0x04000000;
break;
case 1:
memstart = 0xa0000000;
memsize = 0x04000000;
break;
default:
memstart = 0xa0000000;
memsize = 0x04000000;
}
}
printf("initarm: Configuring system ...\n");
bootconfig.dramblocks = 1;
bootconfig.dram[0].address = memstart;
bootconfig.dram[0].pages = memsize / PAGE_SIZE;
physical_start = bootconfig.dram[0].address;
physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
physical_freestart = 0xa0009000UL;
physical_freeend = 0xa0200000UL;
physmem = (physical_end - physical_start) / PAGE_SIZE;
#ifdef VERBOSE_INIT_ARM
printf("physmemory: 0x%"PRIxPSIZE" pages at 0x%08lx -> 0x%08lx\n", physmem,
physical_start, physical_end - 1);
#endif
#ifdef VERBOSE_INIT_ARM
printf("Allocating page tables\n");
#endif
free_pages = (physical_freeend - physical_freestart) / PAGE_SIZE;
#ifdef VERBOSE_INIT_ARM
printf("freestart = 0x%08lx, free_pages = %d (0x%08x)\n",
physical_freestart, free_pages, free_pages);
#endif
#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;
kernel_l1pt.pv_pa = 0;
kernel_l1pt.pv_va = 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");
LEDSTEP();
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);
KASSERT(xscale_minidata_clean_size <= PAGE_SIZE);
valloc_pages(minidataclean, 1);
#ifdef VERBOSE_INIT_ARM
printf("IRQ stack: p0x%08lx v0x%08lx\n", irqstack.pv_pa,
irqstack.pv_va);
printf("ABT stack: p0x%08lx v0x%08lx\n", abtstack.pv_pa,
abtstack.pv_va);
printf("UND stack: p0x%08lx v0x%08lx\n", undstack.pv_pa,
undstack.pv_va);
printf("SVC stack: p0x%08lx v0x%08lx\n", kernelstack.pv_pa,
kernelstack.pv_va);
#endif
alloc_pages(msgbufphys, round_page(MSGBUFSIZE) / PAGE_SIZE);
#ifdef VERBOSE_INIT_ARM
printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
#endif
l1pagetable = kernel_l1pt.pv_pa;
pmap_link_l2pt(l1pagetable, 0x00000000,
&kernel_pt_table[KERNEL_PT_SYS]);
for (loop = 0; loop < KERNEL_PT_KERNEL_NUM; loop++)
pmap_link_l2pt(l1pagetable, KERNEL_BASE + loop * 0x00400000,
&kernel_pt_table[KERNEL_PT_KERNEL + loop]);
for (loop = 0; loop < KERNEL_PT_VMDATA_NUM; loop++)
pmap_link_l2pt(l1pagetable, KERNEL_VM_BASE + loop * 0x00400000,
&kernel_pt_table[KERNEL_PT_VMDATA + loop]);
pmap_curmaxkvaddr =
KERNEL_VM_BASE + (KERNEL_PT_VMDATA_NUM * 0x00400000);
#ifdef VERBOSE_INIT_ARM
printf("Mapping kernel\n");
#endif
{
extern char etext[], _end[];
size_t textsize = (uintptr_t) etext - KERNEL_TEXT_BASE;
size_t totalsize = (uintptr_t) _end - KERNEL_TEXT_BASE;
u_int logical;
textsize = (textsize + PGOFSET) & ~PGOFSET;
totalsize = (totalsize + PGOFSET) & ~PGOFSET;
logical = 0x00200000;
logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
physical_start + logical, textsize,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
logical += pmap_map_chunk(l1pagetable, KERNEL_BASE + logical,
physical_start + logical, totalsize - textsize,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
}
#ifdef VERBOSE_INIT_ARM
printf("Constructing L2 page tables\n");
#endif
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);
}
xscale_setup_minidata(l1pagetable, minidataclean.pv_va,
minidataclean.pv_pa);
#if 1
pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
VM_PROT_READ|VM_PROT_WRITE, PTE_NOCACHE);
#else
pmap_map_entry(l1pagetable, vector_page, systempage.pv_pa,
VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
#endif
pmap_devmap_bootstrap(l1pagetable, lubbock_devmap);
xscale_cache_clean_addr = 0xff000000U;
{
extern char _end[];
physical_freestart = physical_start +
(((((uintptr_t) _end) + PGOFSET) & ~PGOFSET) -
KERNEL_BASE);
physical_freeend = physical_end;
free_pages =
(physical_freeend - physical_freestart) / PAGE_SIZE;
}
#ifdef VERBOSE_INIT_ARM
printf("freestart = 0x%08lx, free_pages = %d (0x%x)\n",
physical_freestart, free_pages, free_pages);
printf("switching to new L1 page table @%#lx...", kernel_l1pt.pv_pa);
#endif
LEDSTEP();
cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
cpu_setttb(kernel_l1pt.pv_pa, true);
cpu_tlb_flushID();
cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
LEDSTEP();
uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
#ifdef VERBOSE_INIT_ARM
printf("bootstrap done.\n");
#endif
arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
printf("init subsystems: stacks ");
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);
printf("vectors ");
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;
printf("undefined ");
undefined_init();
printf("page ");
uvm_md_init();
uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
atop(physical_freestart), atop(physical_freeend),
VM_FREELIST_DEFAULT);
printf("pmap ");
LEDSTEP();
pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
LEDSTEP();
#ifdef __HAVE_MEMORY_DISK__
md_root_setconf(memory_disk, sizeof memory_disk);
#endif
{
uint16_t sw = ioreg16_read(LUBBOCK_OBIO_VBASE+LUBBOCK_USERSW);
if (0 == (sw & (1<<13)))
boothowto |= RB_KDB;
if (0 == (sw & (1<<12)))
boothowto |= RB_SINGLE;
}
LEDSTEP();
#ifdef KGDB
if (boothowto & RB_KDB) {
kgdb_debug_init = 1;
kgdb_connect(1);
}
#endif
#ifdef DDB
db_machine_init();
ddb_init(0, NULL, NULL);
if (boothowto & RB_KDB)
Debugger();
#endif
return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
}
#if 0
void
process_kernel_args(char *args)
{
boothowto = 0;
strncpy(bootargs, args, MAX_BOOT_STRING);
args = bootargs;
boot_file = bootargs;
while (*args != ' ' && *args != 0)
++args;
if (*args != 0)
*args++ = 0;
while (*args == ' ')
++args;
boot_args = args;
printf("bootfile: %s\n", boot_file);
printf("bootargs: %s\n", boot_args);
parse_mi_bootargs(boot_args);
}
#endif
#ifdef KGDB
#ifndef KGDB_DEVNAME
#define KGDB_DEVNAME "ffuart"
#endif
const char kgdb_devname[] = KGDB_DEVNAME;
#if (NCOM > 0)
#ifndef KGDB_DEVMODE
#define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)
#endif
int comkgdbmode = KGDB_DEVMODE;
#endif
#endif
void
consinit(void)
{
static int consinit_called = 0;
uint32_t ckenreg = ioreg_read(LUBBOCK_CLKMAN_VBASE+CLKMAN_CKEN);
#if 0
char *console = CONSDEVNAME;
#endif
if (consinit_called != 0)
return;
consinit_called = 1;
#if NCOM > 0
#ifdef FFUARTCONSOLE
if (0 == (ioreg_read(LUBBOCK_OBIO_VBASE+LUBBOCK_USERSW) & (1<<15))) {
}
#ifdef KGDB
else if (0 == strcmp(kgdb_devname, "ffuart")) {
}
#endif
else if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_FFUART_BASE,
comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
#if 0
pxa2x0_clkman_config(CKEN_FFUART, 1);
#else
ioreg_write(LUBBOCK_CLKMAN_VBASE+CLKMAN_CKEN,
ckenreg|CKEN_FFUART);
#endif
return;
}
#endif
#ifdef BTUARTCONSOLE
#ifdef KGDB
if (0 == strcmp(kgdb_devname, "btuart")) {
} else
#endif
if (0 == comcnattach(&pxa2x0_a4x_bs_tag, PXA2X0_BTUART_BASE,
comcnspeed, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comcnmode)) {
ioreg_write(LUBBOCK_CLKMAN_VBASE+CLKMAN_CKEN,
ckenreg|CKEN_BTUART);
return;
}
#endif
#endif
}
#ifdef KGDB
void
kgdb_port_init(void)
{
#if (NCOM > 0) && defined(COM_PXA2X0)
paddr_t paddr = 0;
uint32_t ckenreg = ioreg_read(LUBBOCK_CLKMAN_VBASE+CLKMAN_CKEN);
if (0 == strcmp(kgdb_devname, "ffuart")) {
paddr = PXA2X0_FFUART_BASE;
ckenreg |= CKEN_FFUART;
}
else if (0 == strcmp(kgdb_devname, "btuart")) {
paddr = PXA2X0_BTUART_BASE;
ckenreg |= CKEN_BTUART;
}
if (paddr &&
0 == com_kgdb_attach(&pxa2x0_a4x_bs_tag, paddr,
kgdb_rate, PXA2X0_COM_FREQ, COM_TYPE_PXA2x0, comkgdbmode)) {
ioreg_write(LUBBOCK_CLKMAN_VBASE+CLKMAN_CKEN, ckenreg);
}
#endif
}
#endif
#if 0
unsigned short led_control_value = 0;
void
hex_led_blank(uint32_t value, int blank)
{
int save = disable_interrupts(I32_bit);
ioreg_write(LUBBOCK_OBIO_VBASE+0x10, value);
led_control_value = (led_control_value & 0xff)
| ((blank & 0xff)<<8);
ioreg_write(LUBBOCK_OBIO_VBASE+0x40, led_control_value);
restore_interrupts(save);
}
#endif