blob: 5252190af53f3c01acf01fcba597022c629ac1ca [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>
Tom Rini8c70baa2021-12-14 13:36:40 -05008#include <clock_legacy.h>
Simon Glass1ea97892020-05-10 11:40:00 -06009#include <bootstage.h>
Thomas Choufb798b12015-10-09 13:46:34 +080010#include <dm.h>
11#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Simon Glass1b9c4b02020-12-23 08:11:16 -070013#include <spl.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070014#include <time.h>
Thomas Choufb798b12015-10-09 13:46:34 +080015#include <timer.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010016#include <watchdog.h>
Rob Herring57156942013-10-04 10:22:41 -050017#include <div64.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Rob Herring57156942013-10-04 10:22:41 -050019#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010021
Tom Rini364d0022023-01-10 11:19:45 -050022#ifndef CFG_WD_PERIOD
23# define CFG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010024#endif
25
Rob Herring57156942013-10-04 10:22:41 -050026DECLARE_GLOBAL_DATA_PTR;
27
Tom Rini6a5dccc2022-11-16 13:10:41 -050028#ifdef CFG_SYS_TIMER_RATE
Pavel Machekcb21e372014-07-13 13:14:27 +020029/* Returns tick rate in ticks per second */
Rob Herring57156942013-10-04 10:22:41 -050030ulong notrace get_tbclk(void)
31{
Tom Rini6a5dccc2022-11-16 13:10:41 -050032 return CFG_SYS_TIMER_RATE;
Rob Herring57156942013-10-04 10:22:41 -050033}
34#endif
35
Tom Rini6a5dccc2022-11-16 13:10:41 -050036#ifdef CFG_SYS_TIMER_COUNTER
Rob Herring57156942013-10-04 10:22:41 -050037unsigned long notrace timer_read_counter(void)
38{
39#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
Tom Rini6a5dccc2022-11-16 13:10:41 -050040 return ~readl(CFG_SYS_TIMER_COUNTER);
Rob Herring57156942013-10-04 10:22:41 -050041#else
Tom Rini6a5dccc2022-11-16 13:10:41 -050042 return readl(CFG_SYS_TIMER_COUNTER);
Rob Herring57156942013-10-04 10:22:41 -050043#endif
44}
Simon Glass037ef5c2017-05-22 05:05:22 -060045
46ulong timer_get_boot_us(void)
47{
48 ulong count = timer_read_counter();
49
Tom Rini6a5dccc2022-11-16 13:10:41 -050050#ifdef CFG_SYS_TIMER_RATE
51 const ulong timer_rate = CFG_SYS_TIMER_RATE;
Michael Walle48d7dc62022-08-17 21:37:48 +020052
53 if (timer_rate == 1000000)
54 return count;
55 else if (timer_rate > 1000000)
56 return lldiv(count, timer_rate / 1000000);
57 else
58 return (unsigned long long)count * 1000000 / timer_rate;
Simon Glass037ef5c2017-05-22 05:05:22 -060059#else
60 /* Assume the counter is in microseconds */
61 return count;
62#endif
63}
64
Rob Herring57156942013-10-04 10:22:41 -050065#else
Harald Seiler70085152023-01-05 01:08:47 +010066extern unsigned long timer_read_counter(void);
Rob Herring57156942013-10-04 10:22:41 -050067#endif
68
Kever Yangf0b50242019-03-29 22:17:33 +080069#if CONFIG_IS_ENABLED(TIMER)
Thomas Choufb798b12015-10-09 13:46:34 +080070ulong notrace get_tbclk(void)
71{
Simon Glass32f6c172016-02-24 09:14:49 -070072 if (!gd->timer) {
73#ifdef CONFIG_TIMER_EARLY
74 return timer_early_get_rate();
75#else
76 int ret;
Thomas Choufb798b12015-10-09 13:46:34 +080077
Simon Glass32f6c172016-02-24 09:14:49 -070078 ret = dm_timer_init();
79 if (ret)
80 return ret;
81#endif
82 }
Thomas Choufb798b12015-10-09 13:46:34 +080083
84 return timer_get_rate(gd->timer);
85}
86
Bin Mengab841b62015-11-24 13:31:17 -070087uint64_t notrace get_ticks(void)
Thomas Choufb798b12015-10-09 13:46:34 +080088{
Bin Mengab841b62015-11-24 13:31:17 -070089 u64 count;
Thomas Choufb798b12015-10-09 13:46:34 +080090 int ret;
91
Simon Glass32f6c172016-02-24 09:14:49 -070092 if (!gd->timer) {
93#ifdef CONFIG_TIMER_EARLY
94 return timer_early_get_count();
95#else
96 int ret;
97
98 ret = dm_timer_init();
99 if (ret)
Sean Anderson11da6362020-09-09 16:24:56 -0400100 panic("Could not initialize timer (err %d)\n", ret);
Simon Glass32f6c172016-02-24 09:14:49 -0700101#endif
102 }
Thomas Choufb798b12015-10-09 13:46:34 +0800103
104 ret = timer_get_count(gd->timer, &count);
Simon Glass1b9c4b02020-12-23 08:11:16 -0700105 if (ret) {
106 if (spl_phase() > PHASE_TPL)
107 panic("Could not read count from timer (err %d)\n",
108 ret);
109 else
110 panic("no timer (err %d)\n", ret);
111 }
Thomas Choufb798b12015-10-09 13:46:34 +0800112
113 return count;
114}
Bin Mengab841b62015-11-24 13:31:17 -0700115
116#else /* !CONFIG_TIMER */
Thomas Choufb798b12015-10-09 13:46:34 +0800117
Simon Glass0e103162014-10-15 04:38:33 -0600118uint64_t __weak notrace get_ticks(void)
Rob Herring57156942013-10-04 10:22:41 -0500119{
120 unsigned long now = timer_read_counter();
121
122 /* increment tbu if tbl has rolled over */
123 if (now < gd->timebase_l)
124 gd->timebase_h++;
125 gd->timebase_l = now;
Simon Glass0e103162014-10-15 04:38:33 -0600126 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring57156942013-10-04 10:22:41 -0500127}
128
Bin Mengab841b62015-11-24 13:31:17 -0700129#endif /* CONFIG_TIMER */
130
Pavel Machekcb21e372014-07-13 13:14:27 +0200131/* Returns time in milliseconds */
Simon Glass0e103162014-10-15 04:38:33 -0600132static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring57156942013-10-04 10:22:41 -0500133{
Pavel Machekcb21e372014-07-13 13:14:27 +0200134 ulong div = get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -0500135
136 tick *= CONFIG_SYS_HZ;
137 do_div(tick, div);
138 return tick;
139}
140
Darwin Rambof283e4f2013-12-19 15:06:12 -0800141int __weak timer_init(void)
142{
143 return 0;
144}
145
Pavel Machekcb21e372014-07-13 13:14:27 +0200146/* Returns time in milliseconds */
Rob Herring57156942013-10-04 10:22:41 -0500147ulong __weak get_timer(ulong base)
148{
149 return tick_to_time(get_ticks()) - base;
150}
151
Marek Vasutebc50332019-10-15 22:43:41 +0200152static uint64_t notrace tick_to_time_us(uint64_t tick)
153{
154 ulong div = get_tbclk() / 1000;
155
156 tick *= CONFIG_SYS_HZ;
157 do_div(tick, div);
158 return tick;
159}
160
161uint64_t __weak get_timer_us(uint64_t base)
162{
163 return tick_to_time_us(get_ticks()) - base;
164}
165
Simon Glass82213d82020-07-09 18:43:14 -0600166unsigned long __weak get_timer_us_long(unsigned long base)
167{
168 return timer_get_us() - base;
169}
170
Rob Herring57156942013-10-04 10:22:41 -0500171unsigned long __weak notrace timer_get_us(void)
172{
173 return tick_to_time(get_ticks() * 1000);
174}
Pavel Machekcb21e372014-07-13 13:14:27 +0200175
Heinrich Schuchardtdaf69fc2019-06-02 21:02:10 +0200176uint64_t usec_to_tick(unsigned long usec)
Rob Herring57156942013-10-04 10:22:41 -0500177{
Simon Glass0e103162014-10-15 04:38:33 -0600178 uint64_t tick = usec;
Stephen Warrenff81b482013-12-05 12:08:09 -0700179 tick *= get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -0500180 do_div(tick, 1000000);
181 return tick;
182}
183
184void __weak __udelay(unsigned long usec)
185{
Simon Glass0e103162014-10-15 04:38:33 -0600186 uint64_t tmp;
Rob Herring57156942013-10-04 10:22:41 -0500187
Pavel Machekcb21e372014-07-13 13:14:27 +0200188 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring57156942013-10-04 10:22:41 -0500189
Pavel Machekcb21e372014-07-13 13:14:27 +0200190 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring57156942013-10-04 10:22:41 -0500191 /*NOP*/;
192}
193
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100194/* ------------------------------------------------------------------------- */
195
196void udelay(unsigned long usec)
197{
198 ulong kv;
199
200 do {
Stefan Roese80877fa2022-09-02 14:10:46 +0200201 schedule();
Tom Rini364d0022023-01-10 11:19:45 -0500202 kv = usec > CFG_WD_PERIOD ? CFG_WD_PERIOD : usec;
Simon Glass0db4b942020-05-10 11:40:10 -0600203 __udelay(kv);
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100204 usec -= kv;
205 } while(usec);
206}