blob: 1395f28715ca14d0f67538ede875ad53149166d1 [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 Glass495a5dc2019-11-14 12:57:30 -070011#include <time.h>
Thomas Choufb798b12015-10-09 13:46:34 +080012#include <timer.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010013#include <watchdog.h>
Rob Herring57156942013-10-04 10:22:41 -050014#include <div64.h>
15#include <asm/io.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010016
17#ifndef CONFIG_WD_PERIOD
Pavel Machekcb21e372014-07-13 13:14:27 +020018# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010019#endif
20
Rob Herring57156942013-10-04 10:22:41 -050021DECLARE_GLOBAL_DATA_PTR;
22
23#ifdef CONFIG_SYS_TIMER_RATE
Pavel Machekcb21e372014-07-13 13:14:27 +020024/* Returns tick rate in ticks per second */
Rob Herring57156942013-10-04 10:22:41 -050025ulong notrace get_tbclk(void)
26{
27 return CONFIG_SYS_TIMER_RATE;
28}
29#endif
30
31#ifdef CONFIG_SYS_TIMER_COUNTER
32unsigned long notrace timer_read_counter(void)
33{
34#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
35 return ~readl(CONFIG_SYS_TIMER_COUNTER);
36#else
37 return readl(CONFIG_SYS_TIMER_COUNTER);
38#endif
39}
Simon Glass037ef5c2017-05-22 05:05:22 -060040
41ulong timer_get_boot_us(void)
42{
43 ulong count = timer_read_counter();
44
45#if CONFIG_SYS_TIMER_RATE == 1000000
46 return count;
47#elif CONFIG_SYS_TIMER_RATE > 1000000
48 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
49#elif defined(CONFIG_SYS_TIMER_RATE)
50 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
51#else
52 /* Assume the counter is in microseconds */
53 return count;
54#endif
55}
56
Rob Herring57156942013-10-04 10:22:41 -050057#else
Rob Herring316c5c82013-11-08 08:40:43 -060058extern unsigned long __weak timer_read_counter(void);
Rob Herring57156942013-10-04 10:22:41 -050059#endif
60
Kever Yangf0b50242019-03-29 22:17:33 +080061#if CONFIG_IS_ENABLED(TIMER)
Thomas Choufb798b12015-10-09 13:46:34 +080062ulong notrace get_tbclk(void)
63{
Simon Glass32f6c172016-02-24 09:14:49 -070064 if (!gd->timer) {
65#ifdef CONFIG_TIMER_EARLY
66 return timer_early_get_rate();
67#else
68 int ret;
Thomas Choufb798b12015-10-09 13:46:34 +080069
Simon Glass32f6c172016-02-24 09:14:49 -070070 ret = dm_timer_init();
71 if (ret)
72 return ret;
73#endif
74 }
Thomas Choufb798b12015-10-09 13:46:34 +080075
76 return timer_get_rate(gd->timer);
77}
78
Bin Mengab841b62015-11-24 13:31:17 -070079uint64_t notrace get_ticks(void)
Thomas Choufb798b12015-10-09 13:46:34 +080080{
Bin Mengab841b62015-11-24 13:31:17 -070081 u64 count;
Thomas Choufb798b12015-10-09 13:46:34 +080082 int ret;
83
Simon Glass32f6c172016-02-24 09:14:49 -070084 if (!gd->timer) {
85#ifdef CONFIG_TIMER_EARLY
86 return timer_early_get_count();
87#else
88 int ret;
89
90 ret = dm_timer_init();
91 if (ret)
92 return ret;
93#endif
94 }
Thomas Choufb798b12015-10-09 13:46:34 +080095
96 ret = timer_get_count(gd->timer, &count);
97 if (ret)
98 return ret;
99
100 return count;
101}
Bin Mengab841b62015-11-24 13:31:17 -0700102
103#else /* !CONFIG_TIMER */
Thomas Choufb798b12015-10-09 13:46:34 +0800104
Simon Glass0e103162014-10-15 04:38:33 -0600105uint64_t __weak notrace get_ticks(void)
Rob Herring57156942013-10-04 10:22:41 -0500106{
107 unsigned long now = timer_read_counter();
108
109 /* increment tbu if tbl has rolled over */
110 if (now < gd->timebase_l)
111 gd->timebase_h++;
112 gd->timebase_l = now;
Simon Glass0e103162014-10-15 04:38:33 -0600113 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring57156942013-10-04 10:22:41 -0500114}
115
Bin Mengab841b62015-11-24 13:31:17 -0700116#endif /* CONFIG_TIMER */
117
Pavel Machekcb21e372014-07-13 13:14:27 +0200118/* Returns time in milliseconds */
Simon Glass0e103162014-10-15 04:38:33 -0600119static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring57156942013-10-04 10:22:41 -0500120{
Pavel Machekcb21e372014-07-13 13:14:27 +0200121 ulong div = get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -0500122
123 tick *= CONFIG_SYS_HZ;
124 do_div(tick, div);
125 return tick;
126}
127
Darwin Rambof283e4f2013-12-19 15:06:12 -0800128int __weak timer_init(void)
129{
130 return 0;
131}
132
Pavel Machekcb21e372014-07-13 13:14:27 +0200133/* Returns time in milliseconds */
Rob Herring57156942013-10-04 10:22:41 -0500134ulong __weak get_timer(ulong base)
135{
136 return tick_to_time(get_ticks()) - base;
137}
138
Marek Vasutebc50332019-10-15 22:43:41 +0200139static uint64_t notrace tick_to_time_us(uint64_t tick)
140{
141 ulong div = get_tbclk() / 1000;
142
143 tick *= CONFIG_SYS_HZ;
144 do_div(tick, div);
145 return tick;
146}
147
148uint64_t __weak get_timer_us(uint64_t base)
149{
150 return tick_to_time_us(get_ticks()) - base;
151}
152
Rob Herring57156942013-10-04 10:22:41 -0500153unsigned long __weak notrace timer_get_us(void)
154{
155 return tick_to_time(get_ticks() * 1000);
156}
Pavel Machekcb21e372014-07-13 13:14:27 +0200157
Heinrich Schuchardtdaf69fc2019-06-02 21:02:10 +0200158uint64_t usec_to_tick(unsigned long usec)
Rob Herring57156942013-10-04 10:22:41 -0500159{
Simon Glass0e103162014-10-15 04:38:33 -0600160 uint64_t tick = usec;
Stephen Warrenff81b482013-12-05 12:08:09 -0700161 tick *= get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -0500162 do_div(tick, 1000000);
163 return tick;
164}
165
166void __weak __udelay(unsigned long usec)
167{
Simon Glass0e103162014-10-15 04:38:33 -0600168 uint64_t tmp;
Rob Herring57156942013-10-04 10:22:41 -0500169
Pavel Machekcb21e372014-07-13 13:14:27 +0200170 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring57156942013-10-04 10:22:41 -0500171
Pavel Machekcb21e372014-07-13 13:14:27 +0200172 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring57156942013-10-04 10:22:41 -0500173 /*NOP*/;
174}
175
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100176/* ------------------------------------------------------------------------- */
177
178void udelay(unsigned long usec)
179{
180 ulong kv;
181
182 do {
183 WATCHDOG_RESET();
184 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
185 __udelay (kv);
186 usec -= kv;
187 } while(usec);
188}