Firmwares/bare-metal-bsp/env/rtl/init.c

108 lines
1.8 KiB
C
Raw Permalink Normal View History

2018-09-25 18:31:29 +02:00
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include "platform.h"
#include "encoding.h"
extern int main(int argc, char** argv);
extern void trap_entry();
2023-08-20 15:00:51 +02:00
static unsigned long mtime_lo(void)
2018-09-25 18:31:29 +02:00
{
2023-08-20 15:00:51 +02:00
unsigned long ret;
__asm volatile("rdtime %0":"=r"(ret));
return ret;
2018-09-25 18:31:29 +02:00
}
2023-08-20 15:00:51 +02:00
#if __riscv_xlen==32
static uint32_t mtime_hi(void)
2018-09-25 18:31:29 +02:00
{
2023-08-20 15:00:51 +02:00
unsigned long ret;
__asm volatile("rdtimeh %0":"=r"(ret));
return ret;
2018-09-25 18:31:29 +02:00
}
uint64_t get_timer_value()
{
while (1) {
2023-08-20 15:00:51 +02:00
uint32_t hi = mtime_hi();
uint32_t lo = mtime_lo();
if (hi == mtime_hi())
2018-09-25 18:31:29 +02:00
return ((uint64_t)hi << 32) | lo;
}
2023-08-20 15:00:51 +02:00
}
#elif __riscv_xlen==64
uint64_t get_timer_value()
{
return mtime_lo();
}
2018-09-25 18:31:29 +02:00
#endif
2023-08-20 15:00:51 +02:00
unsigned long get_timer_freq()
{
return 32768;
2018-09-25 18:31:29 +02:00
}
2023-08-20 15:00:51 +02:00
unsigned long get_cpu_freq()
2018-09-25 18:31:29 +02:00
{
2023-08-20 15:00:51 +02:00
return 10000000;
2018-09-25 18:31:29 +02:00
}
2023-08-20 15:00:51 +02:00
void init_pll(void){
}
2018-09-25 18:31:29 +02:00
#ifdef USE_PLIC
extern void handle_m_ext_interrupt();
#endif
#ifdef USE_M_TIME
extern void handle_m_time_interrupt();
#endif
uintptr_t handle_trap(uintptr_t mcause, uintptr_t epc)
{
if (0){
#ifdef USE_PLIC
// External Machine-Level interrupt from PLIC
} else if ((mcause & MCAUSE_INT) && ((mcause & MCAUSE_CAUSE) == IRQ_M_EXT)) {
handle_m_ext_interrupt();
#endif
#ifdef USE_M_TIME
// External Machine-Level interrupt from PLIC
} else if ((mcause & MCAUSE_INT) && ((mcause & MCAUSE_CAUSE) == IRQ_M_TIMER)){
handle_m_time_interrupt();
#endif
}
else {
2023-08-20 15:00:51 +02:00
write(1, "trap\n", 5);
2018-09-25 18:31:29 +02:00
_exit(1 + mcause);
}
return epc;
}
void _init()
{
2023-08-20 15:00:51 +02:00
#ifndef NO_INIT
init_pll();
2018-09-25 18:31:29 +02:00
printf("core freq at %d Hz\n", get_cpu_freq());
write_csr(mtvec, &trap_entry);
2023-08-20 15:00:51 +02:00
if (read_csr(misa) & (1 << ('F' - 'A'))) { // if F extension is present
write_csr(mstatus, MSTATUS_FS); // allow FPU instructions without trapping
write_csr(fcsr, 0); // initialize rounding mode, undefined at reset
}
#endif
2018-09-25 18:31:29 +02:00
}
void _fini()
{
}