xref: /openbmc/u-boot/arch/riscv/lib/rdtime.c (revision 7e40d0a3)
1*511107d8SAnup Patel // SPDX-License-Identifier: GPL-2.0+
2*511107d8SAnup Patel /*
3*511107d8SAnup Patel  * Copyright (C) 2018, Anup Patel <anup@brainfault.org>
4*511107d8SAnup Patel  * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
5*511107d8SAnup Patel  *
6*511107d8SAnup Patel  * The riscv_get_time() API implementation that is using the
7*511107d8SAnup Patel  * standard rdtime instruction.
8*511107d8SAnup Patel  */
9*511107d8SAnup Patel 
10*511107d8SAnup Patel #include <common.h>
11*511107d8SAnup Patel 
12*511107d8SAnup Patel /* Implement the API required by RISC-V timer driver */
riscv_get_time(u64 * time)13*511107d8SAnup Patel int riscv_get_time(u64 *time)
14*511107d8SAnup Patel {
15*511107d8SAnup Patel #ifdef CONFIG_64BIT
16*511107d8SAnup Patel 	u64 n;
17*511107d8SAnup Patel 
18*511107d8SAnup Patel 	__asm__ __volatile__ (
19*511107d8SAnup Patel 		"rdtime %0"
20*511107d8SAnup Patel 		: "=r" (n));
21*511107d8SAnup Patel 
22*511107d8SAnup Patel 	*time = n;
23*511107d8SAnup Patel #else
24*511107d8SAnup Patel 	u32 lo, hi, tmp;
25*511107d8SAnup Patel 
26*511107d8SAnup Patel 	__asm__ __volatile__ (
27*511107d8SAnup Patel 		"1:\n"
28*511107d8SAnup Patel 		"rdtimeh %0\n"
29*511107d8SAnup Patel 		"rdtime %1\n"
30*511107d8SAnup Patel 		"rdtimeh %2\n"
31*511107d8SAnup Patel 		"bne %0, %2, 1b"
32*511107d8SAnup Patel 		: "=&r" (hi), "=&r" (lo), "=&r" (tmp));
33*511107d8SAnup Patel 
34*511107d8SAnup Patel 	*time = ((u64)hi << 32) | lo;
35*511107d8SAnup Patel #endif
36*511107d8SAnup Patel 
37*511107d8SAnup Patel 	return 0;
38*511107d8SAnup Patel }
39