f88ccfde9ff273ea9dd3094c5c84161b3f1e5f4f
[oweals/u-boot.git] / arch / arm / mach-rmobile / timer.c
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 <init.h>
10 #include <time.h>
11 #include <asm/io.h>
12 #include <asm/arch-armv7/globaltimer.h>
13 #include <asm/arch/rmobile.h>
14
15 static struct globaltimer *global_timer = \
16                 (struct globaltimer *)GLOBAL_TIMER_BASE_ADDR;
17
18 #define CLK2MHZ(clk)    (clk / 1000 / 1000)
19 static u64 get_cpu_global_timer(void)
20 {
21         u32 low, high;
22         u64 timer;
23
24         u32 old = readl(&global_timer->cnt_h);
25         while (1) {
26                 low = readl(&global_timer->cnt_l);
27                 high = readl(&global_timer->cnt_h);
28                 if (old == high)
29                         break;
30                 else
31                         old = high;
32         }
33
34         timer = high;
35         return (u64)((timer << 32) | low);
36 }
37
38 static u64 get_time_us(void)
39 {
40         u64 timer = get_cpu_global_timer();
41
42         timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1));
43         do_div(timer, CLK2MHZ(CONFIG_SYS_CPU_CLK));
44         return timer;
45 }
46
47 static ulong get_time_ms(void)
48 {
49         u64 us = get_time_us();
50
51         do_div(us, 1000);
52         return us;
53 }
54
55 int timer_init(void)
56 {
57         writel(0x01, &global_timer->ctl);
58         return 0;
59 }
60
61 void __udelay(unsigned long usec)
62 {
63         u64 start, current;
64         u64 wait;
65
66         start = get_cpu_global_timer();
67         wait = (u64)((usec * CLK2MHZ(CONFIG_SYS_CPU_CLK)) >> 2);
68         do {
69                 current = get_cpu_global_timer();
70         } while ((current - start) < wait);
71 }
72
73 ulong get_timer(ulong base)
74 {
75         return get_time_ms() - base;
76 }
77
78 unsigned long long get_ticks(void)
79 {
80         return get_cpu_global_timer();
81 }
82
83 ulong get_tbclk(void)
84 {
85         return (ulong)(CONFIG_SYS_CPU_CLK >> 2);
86 }