blob: 872f73d521e1e0f3e231a1add6591418e1a43c21 [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
Tom Rini2f8a6db2021-12-14 13:36:40 -05007#include <clock_legacy.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 Glass6d1a8eb2020-12-23 08:11:16 -070012#include <spl.h>
Simon Glass10453152019-11-14 12:57:30 -070013#include <time.h>
Thomas Chouc8a7ba92015-10-09 13:46:34 +080014#include <timer.h>
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010015#include <watchdog.h>
Rob Herring8dfafdd2013-10-04 10:22:41 -050016#include <div64.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Rob Herring8dfafdd2013-10-04 10:22:41 -050018#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060019#include <linux/delay.h>
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010020
Tom Rini6e7df1d2023-01-10 11:19:45 -050021#ifndef CFG_WD_PERIOD
22# define CFG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010023#endif
24
Rob Herring8dfafdd2013-10-04 10:22:41 -050025DECLARE_GLOBAL_DATA_PTR;
26
Tom Rini65cc0e22022-11-16 13:10:41 -050027#ifdef CFG_SYS_TIMER_RATE
Pavel Machekfccacd32014-07-13 13:14:27 +020028/* Returns tick rate in ticks per second */
Rob Herring8dfafdd2013-10-04 10:22:41 -050029ulong notrace get_tbclk(void)
30{
Tom Rini65cc0e22022-11-16 13:10:41 -050031 return CFG_SYS_TIMER_RATE;
Rob Herring8dfafdd2013-10-04 10:22:41 -050032}
33#endif
34
Tom Rini65cc0e22022-11-16 13:10:41 -050035#ifdef CFG_SYS_TIMER_COUNTER
Rob Herring8dfafdd2013-10-04 10:22:41 -050036unsigned long notrace timer_read_counter(void)
37{
38#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
Tom Rini65cc0e22022-11-16 13:10:41 -050039 return ~readl(CFG_SYS_TIMER_COUNTER);
Rob Herring8dfafdd2013-10-04 10:22:41 -050040#else
Tom Rini65cc0e22022-11-16 13:10:41 -050041 return readl(CFG_SYS_TIMER_COUNTER);
Rob Herring8dfafdd2013-10-04 10:22:41 -050042#endif
43}
Simon Glass9fb34b02017-05-22 05:05:22 -060044
45ulong timer_get_boot_us(void)
46{
47 ulong count = timer_read_counter();
48
Tom Rini65cc0e22022-11-16 13:10:41 -050049#ifdef CFG_SYS_TIMER_RATE
50 const ulong timer_rate = CFG_SYS_TIMER_RATE;
Michael Walle616278b2022-08-17 21:37:48 +020051
52 if (timer_rate == 1000000)
53 return count;
54 else if (timer_rate > 1000000)
55 return lldiv(count, timer_rate / 1000000);
56 else
57 return (unsigned long long)count * 1000000 / timer_rate;
Simon Glass9fb34b02017-05-22 05:05:22 -060058#else
59 /* Assume the counter is in microseconds */
60 return count;
61#endif
62}
63
Rob Herring8dfafdd2013-10-04 10:22:41 -050064#else
Harald Seilerea3d28e2023-01-05 01:08:47 +010065extern unsigned long timer_read_counter(void);
Rob Herring8dfafdd2013-10-04 10:22:41 -050066#endif
67
Kever Yangf00c2622019-03-29 22:17:33 +080068#if CONFIG_IS_ENABLED(TIMER)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080069ulong notrace get_tbclk(void)
70{
Simon Glassc95fec32016-02-24 09:14:49 -070071 if (!gd->timer) {
Simon Glassc95fec32016-02-24 09:14:49 -070072 int ret;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080073
Simon Glass64d445a2023-01-15 14:15:43 -070074 if (IS_ENABLED(CONFIG_TIMER_EARLY))
75 return timer_early_get_rate();
76
Simon Glassc95fec32016-02-24 09:14:49 -070077 ret = dm_timer_init();
78 if (ret)
79 return ret;
Simon Glassc95fec32016-02-24 09:14:49 -070080 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080081
82 return timer_get_rate(gd->timer);
83}
84
Bin Meng9ca07eb2015-11-24 13:31:17 -070085uint64_t notrace get_ticks(void)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080086{
Bin Meng9ca07eb2015-11-24 13:31:17 -070087 u64 count;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080088 int ret;
89
Simon Glassc95fec32016-02-24 09:14:49 -070090 if (!gd->timer) {
Simon Glassc95fec32016-02-24 09:14:49 -070091 int ret;
92
Simon Glass64d445a2023-01-15 14:15:43 -070093 if (IS_ENABLED(CONFIG_TIMER_EARLY))
94 return timer_early_get_count();
95
Simon Glassc95fec32016-02-24 09:14:49 -070096 ret = dm_timer_init();
97 if (ret)
Sean Anderson4b2be782020-09-09 16:24:56 -040098 panic("Could not initialize timer (err %d)\n", ret);
Simon Glassc95fec32016-02-24 09:14:49 -070099 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800100
101 ret = timer_get_count(gd->timer, &count);
Simon Glass6d1a8eb2020-12-23 08:11:16 -0700102 if (ret) {
103 if (spl_phase() > PHASE_TPL)
104 panic("Could not read count from timer (err %d)\n",
105 ret);
106 else
107 panic("no timer (err %d)\n", ret);
108 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800109
110 return count;
111}
Bin Meng9ca07eb2015-11-24 13:31:17 -0700112
113#else /* !CONFIG_TIMER */
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800114
Simon Glass19ea4672014-10-15 04:38:33 -0600115uint64_t __weak notrace get_ticks(void)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500116{
117 unsigned long now = timer_read_counter();
118
119 /* increment tbu if tbl has rolled over */
120 if (now < gd->timebase_l)
121 gd->timebase_h++;
122 gd->timebase_l = now;
Simon Glass19ea4672014-10-15 04:38:33 -0600123 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500124}
125
Bin Meng9ca07eb2015-11-24 13:31:17 -0700126#endif /* CONFIG_TIMER */
127
Pavel Machekfccacd32014-07-13 13:14:27 +0200128/* Returns time in milliseconds */
Simon Glass19ea4672014-10-15 04:38:33 -0600129static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500130{
Pavel Machekfccacd32014-07-13 13:14:27 +0200131 ulong div = get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500132
133 tick *= CONFIG_SYS_HZ;
134 do_div(tick, div);
135 return tick;
136}
137
Darwin Rambode351d62013-12-19 15:06:12 -0800138int __weak timer_init(void)
139{
140 return 0;
141}
142
Pavel Machekfccacd32014-07-13 13:14:27 +0200143/* Returns time in milliseconds */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500144ulong __weak get_timer(ulong base)
145{
146 return tick_to_time(get_ticks()) - base;
147}
148
Marek Vasut80e7e7c2019-10-15 22:43:41 +0200149static uint64_t notrace tick_to_time_us(uint64_t tick)
150{
151 ulong div = get_tbclk() / 1000;
152
153 tick *= CONFIG_SYS_HZ;
154 do_div(tick, div);
155 return tick;
156}
157
158uint64_t __weak get_timer_us(uint64_t base)
159{
160 return tick_to_time_us(get_ticks()) - base;
161}
162
Simon Glasse1ddf672020-07-09 18:43:14 -0600163unsigned long __weak get_timer_us_long(unsigned long base)
164{
165 return timer_get_us() - base;
166}
167
Rob Herring8dfafdd2013-10-04 10:22:41 -0500168unsigned long __weak notrace timer_get_us(void)
169{
170 return tick_to_time(get_ticks() * 1000);
171}
Pavel Machekfccacd32014-07-13 13:14:27 +0200172
Heinrich Schuchardt6a853db2019-06-02 21:02:10 +0200173uint64_t usec_to_tick(unsigned long usec)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500174{
Simon Glass19ea4672014-10-15 04:38:33 -0600175 uint64_t tick = usec;
Stephen Warren2cd1b572013-12-05 12:08:09 -0700176 tick *= get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500177 do_div(tick, 1000000);
178 return tick;
179}
180
181void __weak __udelay(unsigned long usec)
182{
Simon Glass19ea4672014-10-15 04:38:33 -0600183 uint64_t tmp;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500184
Pavel Machekfccacd32014-07-13 13:14:27 +0200185 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500186
Pavel Machekfccacd32014-07-13 13:14:27 +0200187 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500188 /*NOP*/;
189}
190
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100191/* ------------------------------------------------------------------------- */
192
193void udelay(unsigned long usec)
194{
195 ulong kv;
196
197 do {
Stefan Roese29caf932022-09-02 14:10:46 +0200198 schedule();
Tom Rini6e7df1d2023-01-10 11:19:45 -0500199 kv = usec > CFG_WD_PERIOD ? CFG_WD_PERIOD : usec;
Simon Glass07e11142020-05-10 11:40:10 -0600200 __udelay(kv);
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100201 usec -= kv;
202 } while(usec);
203}