xref: /openbmc/linux/arch/riscv/include/asm/timex.h (revision 068ac0db)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012 Regents of the University of California
4  */
5 
6 #ifndef _ASM_RISCV_TIMEX_H
7 #define _ASM_RISCV_TIMEX_H
8 
9 #include <asm/csr.h>
10 #include <asm/mmio.h>
11 
12 typedef unsigned long cycles_t;
13 
14 extern u64 __iomem *riscv_time_val;
15 extern u64 __iomem *riscv_time_cmp;
16 
17 #ifdef CONFIG_64BIT
18 #define mmio_get_cycles()	readq_relaxed(riscv_time_val)
19 #else
20 #define mmio_get_cycles()	readl_relaxed(riscv_time_val)
21 #define mmio_get_cycles_hi()	readl_relaxed(((u32 *)riscv_time_val) + 1)
22 #endif
23 
24 static inline cycles_t get_cycles(void)
25 {
26 	if (IS_ENABLED(CONFIG_RISCV_SBI))
27 		return csr_read(CSR_TIME);
28 	return mmio_get_cycles();
29 }
30 #define get_cycles get_cycles
31 
32 #ifdef CONFIG_64BIT
33 static inline u64 get_cycles64(void)
34 {
35 	return get_cycles();
36 }
37 #else /* CONFIG_64BIT */
38 static inline u32 get_cycles_hi(void)
39 {
40 	if (IS_ENABLED(CONFIG_RISCV_SBI))
41 		return csr_read(CSR_TIMEH);
42 	return mmio_get_cycles_hi();
43 }
44 
45 static inline u64 get_cycles64(void)
46 {
47 	u32 hi, lo;
48 
49 	do {
50 		hi = get_cycles_hi();
51 		lo = get_cycles();
52 	} while (hi != get_cycles_hi());
53 
54 	return ((u64)hi << 32) | lo;
55 }
56 #endif /* CONFIG_64BIT */
57 
58 #define ARCH_HAS_READ_CURRENT_TIMER
59 static inline int read_current_timer(unsigned long *timer_val)
60 {
61 	*timer_val = get_cycles();
62 	return 0;
63 }
64 
65 #endif /* _ASM_RISCV_TIMEX_H */
66