blob: 88bc50405ff39df143db028cfc6a90d988120506 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ingo van Lilf0f778a2009-11-24 14:09:21 +01002/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Ingo van Lilf0f778a2009-11-24 14:09:21 +01005 */
6
7#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -06008#include <bootstage.h>
Thomas Choufb798b12015-10-09 13:46:34 +08009#include <dm.h>
10#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070012#include <time.h>
Thomas Choufb798b12015-10-09 13:46:34 +080013#include <timer.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010014#include <watchdog.h>
Rob Herring57156942013-10-04 10:22:41 -050015#include <div64.h>
16#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060017#include <linux/delay.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010018
19#ifndef CONFIG_WD_PERIOD
Pavel Machekcb21e372014-07-13 13:14:27 +020020# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010021#endif
22
Rob Herring57156942013-10-04 10:22:41 -050023DECLARE_GLOBAL_DATA_PTR;
24
25#ifdef CONFIG_SYS_TIMER_RATE
Pavel Machekcb21e372014-07-13 13:14:27 +020026/* Returns tick rate in ticks per second */
Rob Herring57156942013-10-04 10:22:41 -050027ulong notrace get_tbclk(void)
28{
29 return CONFIG_SYS_TIMER_RATE;
30}
31#endif
32
33#ifdef CONFIG_SYS_TIMER_COUNTER
34unsigned long notrace timer_read_counter(void)
35{
36#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
37 return ~readl(CONFIG_SYS_TIMER_COUNTER);
38#else
39 return readl(CONFIG_SYS_TIMER_COUNTER);
40#endif
41}
Simon Glass037ef5c2017-05-22 05:05:22 -060042
43ulong timer_get_boot_us(void)
44{
45 ulong count = timer_read_counter();
46
47#if CONFIG_SYS_TIMER_RATE == 1000000
48 return count;
49#elif CONFIG_SYS_TIMER_RATE > 1000000
50 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
51#elif defined(CONFIG_SYS_TIMER_RATE)
52 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
53#else
54 /* Assume the counter is in microseconds */
55 return count;
56#endif
57}
58
Rob Herring57156942013-10-04 10:22:41 -050059#else
Rob Herring316c5c82013-11-08 08:40:43 -060060extern unsigned long __weak timer_read_counter(void);
Rob Herring57156942013-10-04 10:22:41 -050061#endif
62
Kever Yangf0b50242019-03-29 22:17:33 +080063#if CONFIG_IS_ENABLED(TIMER)
Thomas Choufb798b12015-10-09 13:46:34 +080064ulong notrace get_tbclk(void)
65{
Simon Glass32f6c172016-02-24 09:14:49 -070066 if (!gd->timer) {
67#ifdef CONFIG_TIMER_EARLY
68 return timer_early_get_rate();
69#else
70 int ret;
Thomas Choufb798b12015-10-09 13:46:34 +080071
Simon Glass32f6c172016-02-24 09:14:49 -070072 ret = dm_timer_init();
73 if (ret)
74 return ret;
75#endif
76 }
Thomas Choufb798b12015-10-09 13:46:34 +080077
78 return timer_get_rate(gd->timer);
79}
80
Bin Mengab841b62015-11-24 13:31:17 -070081uint64_t notrace get_ticks(void)
Thomas Choufb798b12015-10-09 13:46:34 +080082{
Bin Mengab841b62015-11-24 13:31:17 -070083 u64 count;
Thomas Choufb798b12015-10-09 13:46:34 +080084 int ret;
85
Simon Glass32f6c172016-02-24 09:14:49 -070086 if (!gd->timer) {
87#ifdef CONFIG_TIMER_EARLY
88 return timer_early_get_count();
89#else
90 int ret;
91
92 ret = dm_timer_init();
93 if (ret)
Sean Anderson11da6362020-09-09 16:24:56 -040094 panic("Could not initialize timer (err %d)\n", ret);
Simon Glass32f6c172016-02-24 09:14:49 -070095#endif
96 }
Thomas Choufb798b12015-10-09 13:46:34 +080097
98 ret = timer_get_count(gd->timer, &count);
99 if (ret)
Sean Anderson11da6362020-09-09 16:24:56 -0400100 panic("Could not read count from timer (err %d)\n", ret);
Thomas Choufb798b12015-10-09 13:46:34 +0800101
102 return count;
103}
Bin Mengab841b62015-11-24 13:31:17 -0700104
105#else /* !CONFIG_TIMER */
Thomas Choufb798b12015-10-09 13:46:34 +0800106
Simon Glass0e103162014-10-15 04:38:33 -0600107uint64_t __weak notrace get_ticks(void)
Rob Herring57156942013-10-04 10:22:41 -0500108{
109 unsigned long now = timer_read_counter();
110
111 /* increment tbu if tbl has rolled over */
112 if (now < gd->timebase_l)
113 gd->timebase_h++;
114 gd->timebase_l = now;
Simon Glass0e103162014-10-15 04:38:33 -0600115 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring57156942013-10-04 10:22:41 -0500116}
117
Bin Mengab841b62015-11-24 13:31:17 -0700118#endif /* CONFIG_TIMER */
119
Pavel Machekcb21e372014-07-13 13:14:27 +0200120/* Returns time in milliseconds */
Simon Glass0e103162014-10-15 04:38:33 -0600121static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring57156942013-10-04 10:22:41 -0500122{
Pavel Machekcb21e372014-07-13 13:14:27 +0200123 ulong div = get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -0500124
125 tick *= CONFIG_SYS_HZ;
126 do_div(tick, div);
127 return tick;
128}
129
Darwin Rambof283e4f2013-12-19 15:06:12 -0800130int __weak timer_init(void)
131{
132 return 0;
133}
134
Pavel Machekcb21e372014-07-13 13:14:27 +0200135/* Returns time in milliseconds */
Rob Herring57156942013-10-04 10:22:41 -0500136ulong __weak get_timer(ulong base)
137{
138 return tick_to_time(get_ticks()) - base;
139}
140
Marek Vasutebc50332019-10-15 22:43:41 +0200141static uint64_t notrace tick_to_time_us(uint64_t tick)
142{
143 ulong div = get_tbclk() / 1000;
144
145 tick *= CONFIG_SYS_HZ;
146 do_div(tick, div);
147 return tick;
148}
149
150uint64_t __weak get_timer_us(uint64_t base)
151{
152 return tick_to_time_us(get_ticks()) - base;
153}
154
Simon Glass82213d82020-07-09 18:43:14 -0600155unsigned long __weak get_timer_us_long(unsigned long base)
156{
157 return timer_get_us() - base;
158}
159
Rob Herring57156942013-10-04 10:22:41 -0500160unsigned long __weak notrace timer_get_us(void)
161{
162 return tick_to_time(get_ticks() * 1000);
163}
Pavel Machekcb21e372014-07-13 13:14:27 +0200164
Heinrich Schuchardtdaf69fc2019-06-02 21:02:10 +0200165uint64_t usec_to_tick(unsigned long usec)
Rob Herring57156942013-10-04 10:22:41 -0500166{
Simon Glass0e103162014-10-15 04:38:33 -0600167 uint64_t tick = usec;
Stephen Warrenff81b482013-12-05 12:08:09 -0700168 tick *= get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -0500169 do_div(tick, 1000000);
170 return tick;
171}
172
173void __weak __udelay(unsigned long usec)
174{
Simon Glass0e103162014-10-15 04:38:33 -0600175 uint64_t tmp;
Rob Herring57156942013-10-04 10:22:41 -0500176
Pavel Machekcb21e372014-07-13 13:14:27 +0200177 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring57156942013-10-04 10:22:41 -0500178
Pavel Machekcb21e372014-07-13 13:14:27 +0200179 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring57156942013-10-04 10:22:41 -0500180 /*NOP*/;
181}
182
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100183/* ------------------------------------------------------------------------- */
184
185void udelay(unsigned long usec)
186{
187 ulong kv;
188
189 do {
190 WATCHDOG_RESET();
191 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
Simon Glass0db4b942020-05-10 11:40:10 -0600192 __udelay(kv);
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100193 usec -= kv;
194 } while(usec);
195}