blob: 293c23b5e2541fbbbd89e0c3b737260077904867 [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>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Nobuhiro Iwamatsuf3db9da2012-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
Tom Rini6a5dccc2022-11-16 13:10:41 -050043 timer = ((timer << 2) + (CLK2MHZ(CFG_SYS_CPU_CLK) >> 1));
44 do_div(timer, CLK2MHZ(CFG_SYS_CPU_CLK));
Nobuhiro Iwamatsuf3db9da2012-06-13 16:29:47 +090045 return timer;
46}
47
48static ulong get_time_ms(void)
49{
Nobuhiro Iwamatsu174ad862013-11-28 17:52:46 +090050 u64 us = get_time_us();
51
52 do_div(us, 1000);
53 return us;
Nobuhiro Iwamatsuf3db9da2012-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();
Tom Rini6a5dccc2022-11-16 13:10:41 -050068 wait = (u64)((usec * CLK2MHZ(CFG_SYS_CPU_CLK)) >> 2);
Nobuhiro Iwamatsuf3db9da2012-06-13 16:29:47 +090069 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{
Tom Rini6a5dccc2022-11-16 13:10:41 -050086 return (ulong)(CFG_SYS_CPU_CLK >> 2);
Nobuhiro Iwamatsuf3db9da2012-06-13 16:29:47 +090087}