xref: /openbmc/u-boot/arch/arm/mach-rmobile/timer.c (revision d66ff4ba)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2012 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
4  * (C) Copyright 2012 Renesas Solutions Corp.
5  */
6 
7 #include <common.h>
8 #include <div64.h>
9 #include <asm/io.h>
10 #include <asm/arch-armv7/globaltimer.h>
11 #include <asm/arch/rmobile.h>
12 
13 static struct globaltimer *global_timer = \
14 		(struct globaltimer *)GLOBAL_TIMER_BASE_ADDR;
15 
16 #define CLK2MHZ(clk)	(clk / 1000 / 1000)
17 static u64 get_cpu_global_timer(void)
18 {
19 	u32 low, high;
20 	u64 timer;
21 
22 	u32 old = readl(&global_timer->cnt_h);
23 	while (1) {
24 		low = readl(&global_timer->cnt_l);
25 		high = readl(&global_timer->cnt_h);
26 		if (old == high)
27 			break;
28 		else
29 			old = high;
30 	}
31 
32 	timer = high;
33 	return (u64)((timer << 32) | low);
34 }
35 
36 static u64 get_time_us(void)
37 {
38 	u64 timer = get_cpu_global_timer();
39 
40 	timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1));
41 	do_div(timer, CLK2MHZ(CONFIG_SYS_CPU_CLK));
42 	return timer;
43 }
44 
45 static ulong get_time_ms(void)
46 {
47 	u64 us = get_time_us();
48 
49 	do_div(us, 1000);
50 	return us;
51 }
52 
53 int timer_init(void)
54 {
55 	writel(0x01, &global_timer->ctl);
56 	return 0;
57 }
58 
59 void __udelay(unsigned long usec)
60 {
61 	u64 start, current;
62 	u64 wait;
63 
64 	start = get_cpu_global_timer();
65 	wait = (u64)((usec * CLK2MHZ(CONFIG_SYS_CPU_CLK)) >> 2);
66 	do {
67 		current = get_cpu_global_timer();
68 	} while ((current - start) < wait);
69 }
70 
71 ulong get_timer(ulong base)
72 {
73 	return get_time_ms() - base;
74 }
75 
76 unsigned long long get_ticks(void)
77 {
78 	return get_cpu_global_timer();
79 }
80 
81 ulong get_tbclk(void)
82 {
83 	return (ulong)(CONFIG_SYS_CPU_CLK >> 2);
84 }
85