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