#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: smdk2410_machdep.c,v 1.47 2024/02/20 23:36:01 andvar Exp $");
#include "opt_arm_debug.h"
#include "opt_console.h"
#include "opt_ddb.h"
#include "opt_kgdb.h"
#include "opt_md.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/intr.h>
#include <uvm/uvm_extern.h>
#include <dev/cons.h>
#include <dev/md.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/s3c2xx0/s3c2410reg.h>
#include <arm/s3c2xx0/s3c2410var.h>
#include "ksyms.h"
#ifndef SDRAM_START
#define SDRAM_START S3C2410_SDRAM_START
#endif
#ifndef SDRAM_SIZE
#define SDRAM_SIZE (32*1024*1024)
#endif
#define SMDK2410_IO_VBASE 0xfd000000
#define KERNEL_TEXT_BASE (KERNEL_BASE + 0x00200000)
#define KERNEL_VM_BASE (KERNEL_BASE + 0x01000000)
#define KERNEL_VM_SIZE 0x0C000000
#if defined(MEMORY_DISK_DYNAMIC) && defined(MEMORY_DISK_ROOT_ADDR)
#define DO_MEMORY_DISK
#ifdef MEMORY_DISK_ROOT_ROM
#else
#endif
#endif
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
paddr_t msgbufphys;
#define KERNEL_PT_SYS 0
#define KERNEL_PT_KERNEL 1
#define KERNEL_PT_KERNEL_NUM 2
#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];
void consinit(void);
void kgdb_port_init(void);
#include "com.h"
#if NCOM > 0
#include <dev/ic/comreg.h>
#include <dev/ic/comvar.h>
#endif
#include "sscom.h"
#if NSSCOM > 0
#include "opt_sscom.h"
#include <arm/s3c2xx0/sscom_var.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;
void
cpu_reboot(int howto, char *bootstr)
{
#ifdef DIAGNOSTIC
printf("boot: howto=%08x curproc=%p\n", howto, curproc);
#endif
cpu_reset_address_paddr = vtophys((u_int)s3c2410_softreset);
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();
}
#define _V(n) (SMDK2410_IO_VBASE + (n) * L1_S_SIZE)
#define GPIO_VBASE _V(0)
#define INTCTL_VBASE _V(1)
#define CLKMAN_VBASE _V(2)
#define UART_VBASE _V(3)
#ifdef MEMORY_DISK_DYNAMIC
#define MEMORY_DISK_VADDR _V(4)
#endif
static const struct pmap_devmap smdk2410_devmap[] = {
DEVMAP_ENTRY(
GPIO_VBASE,
S3C2410_GPIO_BASE,
S3C2410_GPIO_SIZE
),
DEVMAP_ENTRY(
INTCTL_VBASE,
S3C2410_INTCTL_BASE,
S3C2410_INTCTL_SIZE
),
DEVMAP_ENTRY(
CLKMAN_VBASE,
S3C2410_CLKMAN_BASE,
S3C24X0_CLKMAN_SIZE
),
DEVMAP_ENTRY(
UART_VBASE,
S3C2410_UART0_BASE,
S3C2410_UART_BASE(3) - S3C2410_UART0_BASE
),
DEVMAP_ENTRY_END
};
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));
}
#define ioreg_read8(a) (*(volatile uint8_t *)(a))
#define ioreg_write8(a,v) (*(volatile uint8_t *)(a)=(v))
#define ioreg_read32(a) (*(volatile uint32_t *)(a))
#define ioreg_write32(a,v) (*(volatile uint32_t *)(a)=(v))
vaddr_t
initarm(void *arg)
{
int loop;
int loop1;
u_int l1pagetable;
extern int etext __asm("_etext");
extern int end __asm("_end");
int progress_counter = 0;
#ifdef DO_MEMORY_DISK
vaddr_t md_root_start;
#define MD_ROOT_SIZE (MEMORY_DISK_ROOT_SIZE * DEV_BSIZE)
#endif
#define gpio_read8(reg) ioreg_read8(GPIO_VBASE + (reg))
#define LEDSTEP() __LED(progress_counter++)
#define pdatf (*(volatile uint8_t *)(S3C2410_GPIO_BASE+GPIO_PFDAT))
#define __LED(x) (pdatf = (pdatf & ~0xf0) | (~(x) & 0xf0))
LEDSTEP();
ioreg_write32(S3C2410_MEMCTL_BASE + MEMCTL_BWSCON,
(BWSCON_ST|BWSCON_WS) << BWSCON_BANK_SHIFT(2) |
(BWSCON_ST|BWSCON_WS) << BWSCON_BANK_SHIFT(3) |
ioreg_read32(S3C2410_MEMCTL_BASE + MEMCTL_BWSCON));
ioreg_write32(S3C2410_MEMCTL_BASE + MEMCTL_BANKCON(3),
(0<<BANKCON_TACS_SHIFT)|(1<<BANKCON_TCOS_SHIFT)|
(7<<BANKCON_TACC_SHIFT)|(0<<BANKCON_TOCH_SHIFT)|
(0<<BANKCON_TCAH_SHIFT));
if (set_cpufuncs())
panic("cpu not recognized!");
LEDSTEP();
pmap_devmap_bootstrap((vaddr_t)read_ttb(), smdk2410_devmap);
#undef pdatf
#define pdatf (*(volatile uint8_t *)(GPIO_VBASE+GPIO_PFDAT))
LEDSTEP();
ioreg_write32(INTCTL_VBASE + INTCTL_INTMSK, ~0);
s3c2xx0_intr_bootstrap(INTCTL_VBASE);
consinit();
#ifdef VERBOSE_INIT_ARM
printf("consinit done\n");
#endif
#ifdef KGDB
LEDSTEP();
kgdb_port_init();
#endif
LEDSTEP();
#ifdef VERBOSE_INIT_ARM
printf("\nNetBSD/evbarm (SMDK2410) booting ...\n");
#endif
bootconfig.dramblocks = 1;
bootconfig.dram[0].address = SDRAM_START;
bootconfig.dram[0].pages = SDRAM_SIZE / PAGE_SIZE;
physical_start = bootconfig.dram[0].address;
physical_end = physical_start + (bootconfig.dram[0].pages * PAGE_SIZE);
#ifdef DO_MEMORY_DISK
#ifdef MEMORY_DISK_ROOT_ROM
md_root_start = MEMORY_DISK_ROOT_ADDR;
boothowto |= RB_RDONLY;
#else
md_root_start = ((physical_end - MD_ROOT_SIZE) & ~(L1_S_SIZE-1));
printf("Reserve %ld bytes for memory disk\n",
physical_end - md_root_start);
memcpy((void *)md_root_start, (void *)MEMORY_DISK_ROOT_ADDR,
MD_ROOT_SIZE);
physical_end = md_root_start;
#endif
#endif
physical_freestart = SDRAM_START;
physical_freeend = SDRAM_START + 0x00200000;
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;
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\n");
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);
#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);
LEDSTEP();
#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
{
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);
}
#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
#ifdef MEMORY_DISK_DYNAMIC
pmap_map_chunk(l1pagetable, MEMORY_DISK_VADDR, md_root_start,
MD_ROOT_SIZE, VM_PROT_READ | VM_PROT_WRITE, PTE_CACHE);
md_root_setconf((void *)md_root_start, MD_ROOT_SIZE);
#endif
pmap_devmap_bootstrap(l1pagetable, smdk2410_devmap);
{
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));
uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
#ifdef VERBOSE_INIT_ARM
printf("done!\n");
#endif
LEDSTEP();
#ifdef VERBOSE_INIT_ARM
printf("bootstrap done.\n");
#endif
arm32_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
#ifdef VERBOSE_INIT_ARM
printf("init subsystems: stacks ");
#endif
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);
LEDSTEP();
#ifdef VERBOSE_INIT_ARM
printf("vectors ");
#endif
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;
#ifdef VERBOSE_INIT_ARM
printf("undefined ");
#endif
undefined_init();
LEDSTEP();
#ifdef VERBOSE_INIT_ARM
printf("page ");
#endif
uvm_md_init();
uvm_page_physload(atop(physical_freestart), atop(physical_freeend),
atop(physical_freestart), atop(physical_freeend),
VM_FREELIST_DEFAULT);
LEDSTEP();
#ifdef VERBOSE_INIT_ARM
printf("pmap ");
#endif
pmap_bootstrap(KERNEL_VM_BASE, KERNEL_VM_BASE + KERNEL_VM_SIZE);
LEDSTEP();
#ifdef VERBOSE_INIT_ARM
printf("irq ");
#endif
#ifdef VERBOSE_INIT_ARM
printf("done.\n");
#endif
#ifdef BOOTHOWTO
boothowto |= BOOTHOWTO;
#endif
{
uint8_t gpio = ~gpio_read8(GPIO_PFDAT);
if (gpio & (1<<0))
boothowto ^= RB_SINGLE;
if (gpio & (1<<2))
boothowto ^= RB_KDB;
#ifdef VERBOSE_INIT_ARM
printf( "sw: %x boothowto: %x\n", gpio, boothowto );
#endif
}
#ifdef KGDB
if (boothowto & RB_KDB) {
kgdb_debug_init = 1;
kgdb_connect(1);
}
#endif
#ifdef DDB
db_machine_init();
if (boothowto & RB_KDB)
Debugger();
#endif
return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
}
void
consinit(void)
{
static int consinit_done = 0;
bus_space_tag_t iot = &s3c2xx0_bs_tag;
int pclk;
if (consinit_done != 0)
return;
consinit_done = 1;
s3c24x0_clock_freq2(CLKMAN_VBASE, NULL, NULL, &pclk);
#if NSSCOM > 0
#ifdef SSCOM0CONSOLE
if (0 == s3c2410_sscom_cnattach(iot, 0, comcnspeed,
pclk, comcnmode))
return;
#endif
#ifdef SSCOM1CONSOLE
if (0 == s3c2410_sscom_cnattach(iot, 1, comcnspeed,
pclk, comcnmode))
return;
#endif
#endif
#if NCOM>0 && defined(CONCOMADDR)
if (comcnattach(&isa_io_bs_tag, CONCOMADDR, comcnspeed,
COM_FREQ, COM_TYPE_NORMAL, comcnmode))
panic("can't init serial console @%x", CONCOMADDR);
return;
#endif
consinit_done = 0;
}
#ifdef KGDB
#if (NSSCOM > 0)
#ifdef KGDB_DEVNAME
const char kgdb_devname[] = KGDB_DEVNAME;
#else
const char kgdb_devname[] = "";
#endif
#ifndef KGDB_DEVMODE
#define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE|CSTOPB|PARENB))|CS8)
#endif
int kgdb_sscom_mode = KGDB_DEVMODE;
#endif
void
kgdb_port_init(void)
{
#if (NSSCOM > 0)
int unit = -1;
int pclk;
if (strcmp(kgdb_devname, "sscom0") == 0)
unit = 0;
else if (strcmp(kgdb_devname, "sscom1") == 0)
unit = 1;
if (unit >= 0) {
s3c24x0_clock_freq2(CLKMAN_VBASE, NULL, NULL, &pclk);
s3c2410_sscom_kgdb_attach(&s3c2xx0_bs_tag,
unit, kgdb_rate, pclk, kgdb_sscom_mode);
}
#endif
}
#endif
static struct arm32_dma_range smdk2410_dma_ranges[1];
bus_dma_tag_t
s3c2xx0_bus_dma_init(struct arm32_bus_dma_tag *dma_tag_template)
{
extern paddr_t physical_start, physical_end;
struct arm32_bus_dma_tag *dmat;
smdk2410_dma_ranges[0].dr_sysbase = physical_start;
smdk2410_dma_ranges[0].dr_busbase = physical_start;
smdk2410_dma_ranges[0].dr_len = physical_end - physical_start;
#if 1
dmat = dma_tag_template;
#else
dmat = malloc(sizeof *dmat, M_DEVBUF, M_WAITOK);
*dmat = *dma_tag_template;
#endif
dmat->_ranges = smdk2410_dma_ranges;
dmat->_nranges = 1;
return dmat;
}