2018-12-11 21:54:06 +08:00
|
|
|
/*
|
2019-01-24 14:11:10 +08:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Western Digital Corporation or its affiliates.
|
2018-12-11 21:54:06 +08:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anup Patel <anup.patel@wdc.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sbi/riscv_asm.h>
|
|
|
|
#include <sbi/riscv_encoding.h>
|
|
|
|
#include <sbi/sbi_platform.h>
|
|
|
|
#include <sbi/sbi_timer.h>
|
|
|
|
|
|
|
|
#if __riscv_xlen == 32
|
|
|
|
u64 get_ticks(void)
|
|
|
|
{
|
|
|
|
u32 lo, hi, tmp;
|
|
|
|
__asm__ __volatile__ (
|
|
|
|
"1:\n"
|
|
|
|
"rdtimeh %0\n"
|
|
|
|
"rdtime %1\n"
|
|
|
|
"rdtimeh %2\n"
|
|
|
|
"bne %0, %2, 1b"
|
|
|
|
: "=&r" (hi), "=&r" (lo), "=&r" (tmp));
|
|
|
|
return ((u64)hi << 32) | lo;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
u64 get_ticks(void)
|
|
|
|
{
|
|
|
|
unsigned long n;
|
|
|
|
|
|
|
|
__asm__ __volatile__ (
|
|
|
|
"rdtime %0"
|
|
|
|
: "=r" (n));
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
u64 sbi_timer_value(struct sbi_scratch *scratch)
|
|
|
|
{
|
|
|
|
struct sbi_platform *plat = sbi_platform_ptr(scratch);
|
|
|
|
|
2019-01-23 10:33:20 +08:00
|
|
|
if (sbi_platform_has_timer_value(plat))
|
2018-12-11 21:54:06 +08:00
|
|
|
return sbi_platform_timer_value(plat);
|
|
|
|
else
|
|
|
|
return get_ticks();
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:17:03 +08:00
|
|
|
void sbi_timer_event_stop(struct sbi_scratch *scratch)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
2019-01-22 17:17:03 +08:00
|
|
|
sbi_platform_timer_event_stop(sbi_platform_ptr(scratch));
|
2018-12-11 21:54:06 +08:00
|
|
|
}
|
|
|
|
|
2019-01-22 17:17:03 +08:00
|
|
|
void sbi_timer_event_start(struct sbi_scratch *scratch, u64 next_event)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
|
|
|
sbi_platform_timer_event_start(sbi_platform_ptr(scratch),
|
2019-01-22 17:17:03 +08:00
|
|
|
next_event);
|
2019-02-13 10:32:06 +08:00
|
|
|
csr_clear(CSR_MIP, MIP_STIP);
|
|
|
|
csr_set(CSR_MIE, MIP_MTIP);
|
2018-12-11 21:54:06 +08:00
|
|
|
}
|
|
|
|
|
2019-01-22 17:17:03 +08:00
|
|
|
void sbi_timer_process(struct sbi_scratch *scratch)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
2019-02-13 10:32:06 +08:00
|
|
|
csr_clear(CSR_MIE, MIP_MTIP);
|
|
|
|
csr_set(CSR_MIP, MIP_STIP);
|
2018-12-11 21:54:06 +08:00
|
|
|
}
|
|
|
|
|
2019-01-22 17:17:03 +08:00
|
|
|
int sbi_timer_init(struct sbi_scratch *scratch, bool cold_boot)
|
2018-12-11 21:54:06 +08:00
|
|
|
{
|
2018-12-26 21:14:54 +08:00
|
|
|
return sbi_platform_timer_init(sbi_platform_ptr(scratch),
|
2019-01-22 17:17:03 +08:00
|
|
|
cold_boot);
|
2018-12-11 21:54:06 +08:00
|
|
|
}
|