From df3bb750a722cf6155bee151c61fa0af9335d898 Mon Sep 17 00:00:00 2001 From: Mark Poliakov Date: Wed, 31 Mar 2021 00:19:32 +0300 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ Makefile | 18 ++++++++++++++++++ entry.S | 15 +++++++++++++++ kernel.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ link.ld | 20 ++++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 entry.S create mode 100644 kernel.c create mode 100644 link.ld diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..009c450 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/kernel.elf +/kernel.bin diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cbc7f8b --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +CFLAGS=-ffreestanding \ + -O0 +LDFLAGS=-nostdlib + +all: kernel.bin + +kernel.bin: kernel.elf + $(CROSS_COMPILE)objcopy -O binary $< $@ + +kernel.elf: entry.S kernel.c link.ld + $(CROSS_COMPILE)gcc $(CFLAGS) $(LDFLAGS) \ + -o $@ \ + -Tlink.ld \ + entry.S \ + kernel.c + +clean: + rm -f kernel.elf kernel.bin diff --git a/entry.S b/entry.S new file mode 100644 index 0000000..238db67 --- /dev/null +++ b/entry.S @@ -0,0 +1,15 @@ +.section .text.boot +.global _entry +.type _entry, %function +_entry: + adr x1, bsp_stack_top + mov sp, x1 + + bl kernel_main + b . +.size _entry, . - _entry + +.section .bss +bsp_stack_bottom: + .skip 65536 +bsp_stack_top: diff --git a/kernel.c b/kernel.c new file mode 100644 index 0000000..929ef6b --- /dev/null +++ b/kernel.c @@ -0,0 +1,46 @@ +#include + +#define UART0_BASE 0x05000000 +#define R_WDOG_BASE 0x07020400 + +#define WDOG_CTRL_KEY (0xA57 << 1) +#define WDOG_CTRL_RESTART (1 << 0) +#define WDOG_CFG_SYS (1 << 0) +#define WDOG_MODE_EN (1 << 0) + +struct watchdog { + uint32_t irq_en; + uint32_t irq_sta; + uint32_t res[2]; + uint32_t ctrl; + uint32_t cfg; + uint32_t mode; +}; + +static volatile struct watchdog *const r_wdog = (struct watchdog *) R_WDOG_BASE; + +static inline void delay(uint32_t t) { + for (uint32_t i = 0; i < t; ++i) { + asm volatile ("nop"); + } +} + +static void uart_tx(uint8_t byte) { + *((uint8_t *) UART0_BASE) = byte; +} + +static _Noreturn void board_reset(void) { + r_wdog->cfg = WDOG_CFG_SYS; + r_wdog->mode = WDOG_MODE_EN; + r_wdog->ctrl = WDOG_CTRL_KEY | WDOG_CTRL_RESTART; + while (1); +} + +_Noreturn void kernel_main(uintptr_t dt_phys_addr) { + for (const char *p = "It's alive\r\n"; *p; ++p) { + uart_tx(*p); + delay(1000); + } + + board_reset(); +} diff --git a/link.ld b/link.ld new file mode 100644 index 0000000..fa7b6ba --- /dev/null +++ b/link.ld @@ -0,0 +1,20 @@ +ENTRY(_entry); + +MEMORY { + ram : ORIGIN = 0x42000000, LENGTH = 992M +} + +SECTIONS { + .text : ALIGN(4K) { + *(.text.boot) + *(.text*) + }>ram + + .data : ALIGN(4K) { + *(.data*) + }>ram + + .bss : ALIGN(4K) { + *(.bss*) + }>ram +}