blob: d88edafb1962d70653dff76e18b4c52f8aba07f1 [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
Tom Rini8c70baa2021-12-14 13:36:40 -05007#include <clock_legacy.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 Glass1b9c4b02020-12-23 08:11:16 -070012#include <spl.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070013#include <time.h>
Thomas Choufb798b12015-10-09 13:46:34 +080014#include <timer.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010015#include <watchdog.h>
Rob Herring57156942013-10-04 10:22:41 -050016#include <div64.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Rob Herring57156942013-10-04 10:22:41 -050018#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010020
Tom Rini364d0022023-01-10 11:19:45 -050021#ifndef CFG_WD_PERIOD
22# define CFG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010023#endif
24
Rob Herring57156942013-10-04 10:22:41 -050025DECLARE_GLOBAL_DATA_PTR;
26
Tom Rini6a5dccc2022-11-16 13:10:41 -050027#ifdef CFG_SYS_TIMER_RATE
Pavel Machekcb21e372014-07-13 13:14:27 +020028/* Returns tick rate in ticks per second */
Rob Herring57156942013-10-04 10:22:41 -050029ulong notrace get_tbclk(void)
30{
Tom Rini6a5dccc2022-11-16 13:10:41 -050031 return CFG_SYS_TIMER_RATE;
Rob Herring57156942013-10-04 10:22:41 -050032}
33#endif
34
Tom Rini6a5dccc2022-11-16 13:10:41 -050035#ifdef CFG_SYS_TIMER_COUNTER
Rob Herring57156942013-10-04 10:22:41 -050036unsigned long notrace timer_read_counter(void)
37{
38#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
Tom Rini6a5dccc2022-11-16 13:10:41 -050039 return ~readl(CFG_SYS_TIMER_COUNTER);
Rob Herring57156942013-10-04 10:22:41 -050040#else
Tom Rini6a5dccc2022-11-16 13:10:41 -050041 return readl(CFG_SYS_TIMER_COUNTER);
Rob Herring57156942013-10-04 10:22:41 -050042#endif
43}
Simon Glass037ef5c2017-05-22 05:05:22 -060044
45ulong timer_get_boot_us(void)
46{
47 ulong count = timer_read_counter();
48
Tom Rini6a5dccc2022-11-16 13:10:41 -050049#ifdef CFG_SYS_TIMER_RATE
50 const ulong timer_rate = CFG_SYS_TIMER_RATE;
Michael Walle48d7dc62022-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 Glass037ef5c2017-05-22 05:05:22 -060058#else
59 /* Assume the counter is in microseconds */
60 return count;
61#endif
62}
63
Rob Herring57156942013-10-04 10:22:41 -050064#else
Harald Seiler70085152023-01-05 01:08:47 +010065extern unsigned long timer_read_counter(void);
Rob Herring57156942013-10-04 10:22:41 -050066#endif
67
Kever Yangf0b50242019-03-29 22:17:33 +080068#if CONFIG_IS_ENABLED(TIMER)
Thomas Choufb798b12015-10-09 13:46:34 +080069ulong notrace get_tbclk(void)
70{
Simon Glass32f6c172016-02-24 09:14:49 -070071 if (!gd->timer) {
Simon Glass32f6c172016-02-24 09:14:49 -070072 int ret;
Thomas Choufb798b12015-10-09 13:46:34 +080073
Simon Glassb479f222023-01-15 14:15:43 -070074 if (IS_ENABLED(CONFIG_TIMER_EARLY))
75 return timer_early_get_rate();
76
Simon Glass32f6c172016-02-24 09:14:49 -070077 ret = dm_timer_init();
78 if (ret)
79 return ret;
Simon Glass32f6c172016-02-24 09:14:49 -070080 }
Thomas Choufb798b12015-10-09 13:46:34 +080081
82 return timer_get_rate(gd->timer);
83}
84
Bin Mengab841b62015-11-24 13:31:17 -070085uint64_t notrace get_ticks(void)
Thomas Choufb798b12015-10-09 13:46:34 +080086{
Bin Mengab841b62015-11-24 13:31:17 -070087 u64 count;
Thomas Choufb798b12015-10-09 13:46:34 +080088 int ret;
89
Simon Glass32f6c172016-02-24 09:14:49 -070090 if (!gd->timer) {
Simon Glass32f6c172016-02-24 09:14:49 -070091 int ret;
92
Simon Glassb479f222023-01-15 14:15:43 -070093 if (IS_ENABLED(CONFIG_TIMER_EARLY))
94 return timer_early_get_count();
95
Simon Glass32f6c172016-02-24 09:14:49 -070096 ret = dm_timer_init();
97 if (ret)
Sean Anderson11da6362020-09-09 16:24:56 -040098 panic("Could not initialize timer (err %d)\n", ret);
Simon Glass32f6c172016-02-24 09:14:49 -070099 }
Thomas Choufb798b12015-10-09 13:46:34 +0800100
101 ret = timer_get_count(gd->timer, &count);
Simon Glass1b9c4b02020-12-23 08:11:16 -0700102 if (ret) {
Simon Glassd4dce4a2024-09-29 19:49:36 -0600103 if (xpl_phase() > PHASE_TPL)
Simon Glass1b9c4b02020-12-23 08:11:16 -0700104 panic("Could not read count from timer (err %d)\n",
105 ret);
106 else
107 panic("no timer (err %d)\n", ret);
108 }
Thomas Choufb798b12015-10-09 13:46:34 +0800109
110 return count;
111}
Bin Mengab841b62015-11-24 13:31:17 -0700112
113#else /* !CONFIG_TIMER */
Thomas Choufb798b12015-10-09 13:46:34 +0800114
Simon Glass0e103162014-10-15 04:38:33 -0600115uint64_t __weak notrace get_ticks(void)
Rob Herring57156942013-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 Glass0e103162014-10-15 04:38:33 -0600123 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring57156942013-10-04 10:22:41 -0500124}
125
Bin Mengab841b62015-11-24 13:31:17 -0700126#endif /* CONFIG_TIMER */
127
Pavel Machekcb21e372014-07-13 13:14:27 +0200128/* Returns time in milliseconds */
Simon Glass0e103162014-10-15 04:38:33 -0600129static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring57156942013-10-04 10:22:41 -0500130{
Pavel Machekcb21e372014-07-13 13:14:27 +0200131 ulong div = get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -0500132
133 tick *= CONFIG_SYS_HZ;
134 do_div(tick, div);
135 return tick;
136}
137
Darwin Rambof283e4f2013-12-19 15:06:12 -0800138int __weak timer_init(void)
139{
140 return 0;
141}
142
Pavel Machekcb21e372014-07-13 13:14:27 +0200143/* Returns time in milliseconds */
Rob Herring57156942013-10-04 10:22:41 -0500144ulong __weak get_timer(ulong base)
145{
146 return tick_to_time(get_ticks()) - base;
147}
148
Marek Vasutebc50332019-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 Glass82213d82020-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 Herring57156942013-10-04 10:22:41 -0500168unsigned long __weak notrace timer_get_us(void)
169{
170 return tick_to_time(get_ticks() * 1000);
171}
Pavel Machekcb21e372014-07-13 13:14:27 +0200172
Heinrich Schuchardtdaf69fc2019-06-02 21:02:10 +0200173uint64_t usec_to_tick(unsigned long usec)
Rob Herring57156942013-10-04 10:22:41 -0500174{
Simon Glass0e103162014-10-15 04:38:33 -0600175 uint64_t tick = usec;
Stephen Warrenff81b482013-12-05 12:08:09 -0700176 tick *= get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -0500177 do_div(tick, 1000000);
178 return tick;
179}
180
181void __weak __udelay(unsigned long usec)
182{
Simon Glass0e103162014-10-15 04:38:33 -0600183 uint64_t tmp;
Rob Herring57156942013-10-04 10:22:41 -0500184
Pavel Machekcb21e372014-07-13 13:14:27 +0200185 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring57156942013-10-04 10:22:41 -0500186
Pavel Machekcb21e372014-07-13 13:14:27 +0200187 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring57156942013-10-04 10:22:41 -0500188 /*NOP*/;
189}
190
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100191/* ------------------------------------------------------------------------- */
192
193void udelay(unsigned long usec)
194{
195 ulong kv;
196
197 do {
Stefan Roese80877fa2022-09-02 14:10:46 +0200198 schedule();
Tom Rini364d0022023-01-10 11:19:45 -0500199 kv = usec > CFG_WD_PERIOD ? CFG_WD_PERIOD : usec;
Simon Glass0db4b942020-05-10 11:40:10 -0600200 __udelay(kv);
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100201 usec -= kv;
202 } while(usec);
203}