blob: bf74955793fe591cf7e189db2bace95f9667352f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu4fb44e22012-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 Iwamatsu4fb44e22012-06-13 16:29:47 +09005 */
6
7#include <common.h>
Nobuhiro Iwamatsu75797512013-11-28 17:52:46 +09008#include <div64.h>
Nobuhiro Iwamatsu4fb44e22012-06-13 16:29:47 +09009#include <asm/io.h>
10#include <asm/arch-armv7/globaltimer.h>
11#include <asm/arch/rmobile.h>
12
13static struct globaltimer *global_timer = \
14 (struct globaltimer *)GLOBAL_TIMER_BASE_ADDR;
15
16#define CLK2MHZ(clk) (clk / 1000 / 1000)
17static 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
36static u64 get_time_us(void)
37{
38 u64 timer = get_cpu_global_timer();
39
40 timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1));
Nobuhiro Iwamatsu75797512013-11-28 17:52:46 +090041 do_div(timer, CLK2MHZ(CONFIG_SYS_CPU_CLK));
Nobuhiro Iwamatsu4fb44e22012-06-13 16:29:47 +090042 return timer;
43}
44
45static ulong get_time_ms(void)
46{
Nobuhiro Iwamatsu75797512013-11-28 17:52:46 +090047 u64 us = get_time_us();
48
49 do_div(us, 1000);
50 return us;
Nobuhiro Iwamatsu4fb44e22012-06-13 16:29:47 +090051}
52
53int timer_init(void)
54{
55 writel(0x01, &global_timer->ctl);
56 return 0;
57}
58
59void __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
71ulong get_timer(ulong base)
72{
73 return get_time_ms() - base;
74}
75
76unsigned long long get_ticks(void)
77{
78 return get_cpu_global_timer();
79}
80
81ulong get_tbclk(void)
82{
83 return (ulong)(CONFIG_SYS_CPU_CLK >> 2);
84}