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