1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright 2022-2023 NXP
4  */
5 
6 #ifndef __KERNEL_TIME_H
7 #define __KERNEL_TIME_H
8 
9 #include <kernel/clint.h>
10 #include <riscv.h>
11 
read_time(void)12 static inline __noprof uint64_t read_time(void)
13 {
14 	uint64_t time = 0;
15 	uint32_t hi __maybe_unused = 0;
16 	uint32_t lo __maybe_unused = 0;
17 
18 #ifdef CFG_RISCV_M_MODE
19 	time = clint_get_mtime();
20 #endif /*CFG_RISCV_M_MODE*/
21 
22 #ifdef CFG_RISCV_S_MODE
23 #ifdef RV32
24 	do {
25 		hi = read_csr(timeh);
26 		lo = read_csr(time);
27 	} while (hi != read_csr(timeh));
28 
29 	time =  SHIFT_U64(hi, 32) | lo;
30 #else /*RV64*/
31 	time = rdtime();
32 #endif /*RV32*/
33 #endif /*CFG_RISCV_S_MODE*/
34 
35 	return time;
36 }
37 
38 #endif /* __KERNEL_TIME_H */
39