blob: 65db0f6cda29367c6f4b2eee1666a18a3d578100 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ingo van Lil3eb90ba2009-11-24 14:09:21 +01002/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Ingo van Lil3eb90ba2009-11-24 14:09:21 +01005 */
6
7#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -06008#include <bootstage.h>
Thomas Chouc8a7ba92015-10-09 13:46:34 +08009#include <dm.h>
10#include <errno.h>
Simon Glass691d7192020-05-10 11:40:02 -060011#include <init.h>
Simon Glass10453152019-11-14 12:57:30 -070012#include <time.h>
Thomas Chouc8a7ba92015-10-09 13:46:34 +080013#include <timer.h>
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010014#include <watchdog.h>
Rob Herring8dfafdd2013-10-04 10:22:41 -050015#include <div64.h>
16#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060017#include <linux/delay.h>
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010018
19#ifndef CONFIG_WD_PERIOD
Pavel Machekfccacd32014-07-13 13:14:27 +020020# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010021#endif
22
Rob Herring8dfafdd2013-10-04 10:22:41 -050023DECLARE_GLOBAL_DATA_PTR;
24
25#ifdef CONFIG_SYS_TIMER_RATE
Pavel Machekfccacd32014-07-13 13:14:27 +020026/* Returns tick rate in ticks per second */
Rob Herring8dfafdd2013-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 Glass9fb34b02017-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 Herring8dfafdd2013-10-04 10:22:41 -050059#else
Rob Herring65ba7ad2013-11-08 08:40:43 -060060extern unsigned long __weak timer_read_counter(void);
Rob Herring8dfafdd2013-10-04 10:22:41 -050061#endif
62
Kever Yangf00c2622019-03-29 22:17:33 +080063#if CONFIG_IS_ENABLED(TIMER)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080064ulong notrace get_tbclk(void)
65{
Simon Glassc95fec32016-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 Chouc8a7ba92015-10-09 13:46:34 +080071
Simon Glassc95fec32016-02-24 09:14:49 -070072 ret = dm_timer_init();
73 if (ret)
74 return ret;
75#endif
76 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080077
78 return timer_get_rate(gd->timer);
79}
80
Bin Meng9ca07eb2015-11-24 13:31:17 -070081uint64_t notrace get_ticks(void)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080082{
Bin Meng9ca07eb2015-11-24 13:31:17 -070083 u64 count;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080084 int ret;
85
Simon Glassc95fec32016-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)
94 return ret;
95#endif
96 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080097
98 ret = timer_get_count(gd->timer, &count);
99 if (ret)
100 return ret;
101
102 return count;
103}
Bin Meng9ca07eb2015-11-24 13:31:17 -0700104
105#else /* !CONFIG_TIMER */
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800106
Simon Glass19ea4672014-10-15 04:38:33 -0600107uint64_t __weak notrace get_ticks(void)
Rob Herring8dfafdd2013-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 Glass19ea4672014-10-15 04:38:33 -0600115 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500116}
117
Bin Meng9ca07eb2015-11-24 13:31:17 -0700118#endif /* CONFIG_TIMER */
119
Pavel Machekfccacd32014-07-13 13:14:27 +0200120/* Returns time in milliseconds */
Simon Glass19ea4672014-10-15 04:38:33 -0600121static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500122{
Pavel Machekfccacd32014-07-13 13:14:27 +0200123 ulong div = get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500124
125 tick *= CONFIG_SYS_HZ;
126 do_div(tick, div);
127 return tick;
128}
129
Darwin Rambode351d62013-12-19 15:06:12 -0800130int __weak timer_init(void)
131{
132 return 0;
133}
134
Pavel Machekfccacd32014-07-13 13:14:27 +0200135/* Returns time in milliseconds */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500136ulong __weak get_timer(ulong base)
137{
138 return tick_to_time(get_ticks()) - base;
139}
140
Marek Vasut80e7e7c2019-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
Rob Herring8dfafdd2013-10-04 10:22:41 -0500155unsigned long __weak notrace timer_get_us(void)
156{
157 return tick_to_time(get_ticks() * 1000);
158}
Pavel Machekfccacd32014-07-13 13:14:27 +0200159
Heinrich Schuchardt6a853db2019-06-02 21:02:10 +0200160uint64_t usec_to_tick(unsigned long usec)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500161{
Simon Glass19ea4672014-10-15 04:38:33 -0600162 uint64_t tick = usec;
Stephen Warren2cd1b572013-12-05 12:08:09 -0700163 tick *= get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500164 do_div(tick, 1000000);
165 return tick;
166}
167
168void __weak __udelay(unsigned long usec)
169{
Simon Glass19ea4672014-10-15 04:38:33 -0600170 uint64_t tmp;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500171
Pavel Machekfccacd32014-07-13 13:14:27 +0200172 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500173
Pavel Machekfccacd32014-07-13 13:14:27 +0200174 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500175 /*NOP*/;
176}
177
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100178/* ------------------------------------------------------------------------- */
179
180void udelay(unsigned long usec)
181{
182 ulong kv;
183
184 do {
185 WATCHDOG_RESET();
186 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
Simon Glass07e11142020-05-10 11:40:10 -0600187 __udelay(kv);
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100188 usec -= kv;
189 } while(usec);
190}