2021-05-18 09:57:25 +08:00
|
|
|
/*
|
2021-05-21 18:39:41 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2021-05-18 09:57:25 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2018/10/28 Bernard The unify RISC-V porting code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
|
|
|
#include "sbi.h"
|
2021-05-21 17:03:30 +08:00
|
|
|
#include "tick.h"
|
|
|
|
#include <riscv_io.h>
|
|
|
|
#include <encoding.h>
|
|
|
|
|
|
|
|
#define VIRT_CLINT_TIMEBASE_FREQ (10000000)
|
2021-05-18 09:57:25 +08:00
|
|
|
|
|
|
|
static volatile uint64_t time_elapsed = 0;
|
|
|
|
static volatile unsigned long tick_cycles = 0;
|
|
|
|
|
|
|
|
static uint64_t get_ticks()
|
|
|
|
{
|
|
|
|
__asm__ __volatile__(
|
|
|
|
"rdtime %0"
|
|
|
|
: "=r"(time_elapsed));
|
|
|
|
return time_elapsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tick_isr(void)
|
|
|
|
{
|
2021-05-21 17:03:30 +08:00
|
|
|
int tick_cycles = VIRT_CLINT_TIMEBASE_FREQ / RT_TICK_PER_SECOND;
|
2021-05-18 09:57:25 +08:00
|
|
|
rt_tick_increase();
|
2021-05-21 17:03:30 +08:00
|
|
|
#ifdef RISCV_S_MODE
|
2021-05-18 09:57:25 +08:00
|
|
|
sbi_set_timer(get_ticks() + tick_cycles);
|
2021-05-21 17:03:30 +08:00
|
|
|
#else
|
2021-10-19 15:00:46 +08:00
|
|
|
*(uint64_t*)CLINT_MTIMECMP(__raw_hartid()) = *(uint64_t*)CLINT_MTIME + tick_cycles;
|
2021-05-21 17:03:30 +08:00
|
|
|
#endif
|
2021-05-18 09:57:25 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets and enable the timer interrupt */
|
|
|
|
int rt_hw_tick_init(void)
|
|
|
|
{
|
2021-05-21 17:03:30 +08:00
|
|
|
unsigned long interval = VIRT_CLINT_TIMEBASE_FREQ / RT_TICK_PER_SECOND;
|
2021-05-18 09:57:25 +08:00
|
|
|
|
2021-05-21 17:03:30 +08:00
|
|
|
#ifdef RISCV_S_MODE
|
2021-05-18 09:57:25 +08:00
|
|
|
/* Clear the Supervisor-Timer bit in SIE */
|
|
|
|
clear_csr(sie, SIP_STIP);
|
|
|
|
|
|
|
|
/* calculate the tick cycles */
|
|
|
|
// tick_cycles = interval * sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / CLINT_CLOCK_DIV / 1000ULL - 1;
|
|
|
|
tick_cycles = 40000;
|
|
|
|
/* Set timer */
|
|
|
|
sbi_set_timer(get_ticks() + tick_cycles);
|
2021-05-21 18:39:41 +08:00
|
|
|
|
2021-05-18 09:57:25 +08:00
|
|
|
/* Enable the Supervisor-Timer bit in SIE */
|
|
|
|
set_csr(sie, SIP_STIP);
|
2021-05-21 17:03:30 +08:00
|
|
|
#else
|
|
|
|
clear_csr(mie, MIP_MTIP);
|
|
|
|
clear_csr(mip, MIP_MTIP);
|
2021-10-19 15:00:46 +08:00
|
|
|
*(uint64_t*)CLINT_MTIMECMP(__raw_hartid()) = *(uint64_t*)CLINT_MTIME + interval;
|
2021-05-21 17:03:30 +08:00
|
|
|
set_csr(mie, MIP_MTIP);
|
|
|
|
#endif
|
2021-05-18 09:57:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|