blob: f88ccfde9ff273ea9dd3094c5c84161b3f1e5f4f [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 Glass97589732020-05-10 11:40:02 -06009#include <init.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070010#include <time.h>
Nobuhiro Iwamatsuf3db9da2012-06-13 16:29:47 +090011#include <asm/io.h>
12#include <asm/arch-armv7/globaltimer.h>
13#include <asm/arch/rmobile.h>
14
15static struct globaltimer *global_timer = \
16 (struct globaltimer *)GLOBAL_TIMER_BASE_ADDR;
17
18#define CLK2MHZ(clk) (clk / 1000 / 1000)
19static 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
38static u64 get_time_us(void)
39{
40 u64 timer = get_cpu_global_timer();
41
42 timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1));
Nobuhiro Iwamatsu174ad862013-11-28 17:52:46 +090043 do_div(timer, CLK2MHZ(CONFIG_SYS_CPU_CLK));
Nobuhiro Iwamatsuf3db9da2012-06-13 16:29:47 +090044 return timer;
45}
46
47static ulong get_time_ms(void)
48{
Nobuhiro Iwamatsu174ad862013-11-28 17:52:46 +090049 u64 us = get_time_us();
50
51 do_div(us, 1000);
52 return us;
Nobuhiro Iwamatsuf3db9da2012-06-13 16:29:47 +090053}
54
55int timer_init(void)
56{
57 writel(0x01, &global_timer->ctl);
58 return 0;
59}
60
61void __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
73ulong get_timer(ulong base)
74{
75 return get_time_ms() - base;
76}
77
78unsigned long long get_ticks(void)
79{
80 return get_cpu_global_timer();
81}
82
83ulong get_tbclk(void)
84{
85 return (ulong)(CONFIG_SYS_CPU_CLK >> 2);
86}