aboutsummaryrefslogtreecommitdiff
path: root/arch/riscv64/kernel/timer.c
blob: 98f8e16799f85f4821b9a49b55d52f89bdae61b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
 * @file timer.c
 * riscv64 implementation of arch-specific timers.
 */

#include <arch/timer.h>
#include <libfdt.h>
#include <csr.h>
#include <sbi.h>

ticks_t stat_timer(const void *fdt)
{
	int cpu_offset = fdt_path_offset(fdt, "/cpus");
	uint8_t *tf_reg = (uint8_t *)fdt_getprop(fdt, cpu_offset,
	                                         "timebase-frequency", NULL);
	return (ticks_t)fdt_load_int32_ptr(tf_reg);
}

void set_timer(ticks_t ticks)
{
	sbi_set_timer(ticks);
}

#if __riscv_xlen == 64
static ticks_t get_ticks64()
{
	ticks_t ticks = 0;
	csr_read(CSR_TIME, ticks);
	return ticks;
}
#else
static ticks_t get_ticks64()
{
	ticks_t ticksh, ticksl, check;

	/* avoid overflow between reading high and low */
	do {
		csr_read(CSR_TIMEH, ticksh);
		csr_read(CSR_TIME, ticksl);
		csr_read(CSR_TIMEH, check);
	} while (ticksh != check);

	return (ticksh << 32) | ticksl;
}
#endif

ticks_t current_ticks()
{
	return get_ticks64();
}