Initial commit

This commit is contained in:
Mark Poliakov 2021-03-31 00:19:32 +03:00
commit df3bb750a7
5 changed files with 101 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/kernel.elf
/kernel.bin

18
Makefile Normal file
View File

@ -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

15
entry.S Normal file
View File

@ -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:

46
kernel.c Normal file
View File

@ -0,0 +1,46 @@
#include <stdint.h>
#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();
}

20
link.ld Normal file
View File

@ -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
}