blob: ba06535e4c2a696bb3e1e5a7281d0f2e3f8157c9 [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>
Simon Glass691d7192020-05-10 11:40:02 -06009#include <init.h>
Simon Glass10453152019-11-14 12:57:30 -070010#include <time.h>
Nobuhiro Iwamatsu4fb44e22012-06-13 16:29:47 +090011#include <asm/io.h>
12#include <asm/arch-armv7/globaltimer.h>
13#include <asm/arch/rmobile.h>
Simon Glassc05ed002020-05-10 11:40:11 -060014#include <linux/delay.h>
Nobuhiro Iwamatsu4fb44e22012-06-13 16:29:47 +090015
16static struct globaltimer *global_timer = \
17 (struct globaltimer *)GLOBAL_TIMER_BASE_ADDR;
18
19#define CLK2MHZ(clk) (clk / 1000 / 1000)
20static u64 get_cpu_global_timer(void)
21{
22 u32 low, high;
23 u64 timer;
24
25 u32 old = readl(&global_timer->cnt_h);
26 while (1) {
27 low = readl(&global_timer->cnt_l);
28 high = readl(&global_timer->cnt_h);
29 if (old == high)
30 break;
31 else
32 old = high;
33 }
34
35 timer = high;
36 return (u64)((timer << 32) | low);
37}
38
39static u64 get_time_us(void)
40{
41 u64 timer = get_cpu_global_timer();
42
43 timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1));
Nobuhiro Iwamatsu75797512013-11-28 17:52:46 +090044 do_div(timer, CLK2MHZ(CONFIG_SYS_CPU_CLK));
Nobuhiro Iwamatsu4fb44e22012-06-13 16:29:47 +090045 return timer;
46}
47
48static ulong get_time_ms(void)
49{
Nobuhiro Iwamatsu75797512013-11-28 17:52:46 +090050 u64 us = get_time_us();
51
52 do_div(us, 1000);
53 return us;
Nobuhiro Iwamatsu4fb44e22012-06-13 16:29:47 +090054}
55
56int timer_init(void)
57{
58 writel(0x01, &global_timer->ctl);
59 return 0;
60}
61
62void __udelay(unsigned long usec)
63{
64 u64 start, current;
65 u64 wait;
66
67 start = get_cpu_global_timer();
68 wait = (u64)((usec * CLK2MHZ(CONFIG_SYS_CPU_CLK)) >> 2);
69 do {
70 current = get_cpu_global_timer();
71 } while ((current - start) < wait);
72}
73
74ulong get_timer(ulong base)
75{
76 return get_time_ms() - base;
77}
78
79unsigned long long get_ticks(void)
80{
81 return get_cpu_global_timer();
82}
83
84ulong get_tbclk(void)
85{
86 return (ulong)(CONFIG_SYS_CPU_CLK >> 2);
87}