blob: 00f4a1ac8fb3c86791441366bddc4331f86c610b [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>
Tom Rini2f8a6db2021-12-14 13:36:40 -05008#include <clock_legacy.h>
Simon Glass52f24232020-05-10 11:40:00 -06009#include <bootstage.h>
Thomas Chouc8a7ba92015-10-09 13:46:34 +080010#include <dm.h>
11#include <errno.h>
Simon Glass691d7192020-05-10 11:40:02 -060012#include <init.h>
Simon Glass6d1a8eb2020-12-23 08:11:16 -070013#include <spl.h>
Simon Glass10453152019-11-14 12:57:30 -070014#include <time.h>
Thomas Chouc8a7ba92015-10-09 13:46:34 +080015#include <timer.h>
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010016#include <watchdog.h>
Rob Herring8dfafdd2013-10-04 10:22:41 -050017#include <div64.h>
Simon Glass401d1c42020-10-30 21:38:53 -060018#include <asm/global_data.h>
Rob Herring8dfafdd2013-10-04 10:22:41 -050019#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060020#include <linux/delay.h>
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010021
Tom Rini6e7df1d2023-01-10 11:19:45 -050022#ifndef CFG_WD_PERIOD
23# define CFG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010024#endif
25
Rob Herring8dfafdd2013-10-04 10:22:41 -050026DECLARE_GLOBAL_DATA_PTR;
27
Tom Rini65cc0e22022-11-16 13:10:41 -050028#ifdef CFG_SYS_TIMER_RATE
Pavel Machekfccacd32014-07-13 13:14:27 +020029/* Returns tick rate in ticks per second */
Rob Herring8dfafdd2013-10-04 10:22:41 -050030ulong notrace get_tbclk(void)
31{
Tom Rini65cc0e22022-11-16 13:10:41 -050032 return CFG_SYS_TIMER_RATE;
Rob Herring8dfafdd2013-10-04 10:22:41 -050033}
34#endif
35
Tom Rini65cc0e22022-11-16 13:10:41 -050036#ifdef CFG_SYS_TIMER_COUNTER
Rob Herring8dfafdd2013-10-04 10:22:41 -050037unsigned long notrace timer_read_counter(void)
38{
39#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
Tom Rini65cc0e22022-11-16 13:10:41 -050040 return ~readl(CFG_SYS_TIMER_COUNTER);
Rob Herring8dfafdd2013-10-04 10:22:41 -050041#else
Tom Rini65cc0e22022-11-16 13:10:41 -050042 return readl(CFG_SYS_TIMER_COUNTER);
Rob Herring8dfafdd2013-10-04 10:22:41 -050043#endif
44}
Simon Glass9fb34b02017-05-22 05:05:22 -060045
46ulong timer_get_boot_us(void)
47{
48 ulong count = timer_read_counter();
49
Tom Rini65cc0e22022-11-16 13:10:41 -050050#ifdef CFG_SYS_TIMER_RATE
51 const ulong timer_rate = CFG_SYS_TIMER_RATE;
Michael Walle616278b2022-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 Glass9fb34b02017-05-22 05:05:22 -060059#else
60 /* Assume the counter is in microseconds */
61 return count;
62#endif
63}
64
Rob Herring8dfafdd2013-10-04 10:22:41 -050065#else
Harald Seilerea3d28e2023-01-05 01:08:47 +010066extern unsigned long timer_read_counter(void);
Rob Herring8dfafdd2013-10-04 10:22:41 -050067#endif
68
Kever Yangf00c2622019-03-29 22:17:33 +080069#if CONFIG_IS_ENABLED(TIMER)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080070ulong notrace get_tbclk(void)
71{
Simon Glassc95fec32016-02-24 09:14:49 -070072 if (!gd->timer) {
Simon Glassc95fec32016-02-24 09:14:49 -070073 int ret;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080074
Simon Glass64d445a2023-01-15 14:15:43 -070075 if (IS_ENABLED(CONFIG_TIMER_EARLY))
76 return timer_early_get_rate();
77
Simon Glassc95fec32016-02-24 09:14:49 -070078 ret = dm_timer_init();
79 if (ret)
80 return ret;
Simon Glassc95fec32016-02-24 09:14:49 -070081 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080082
83 return timer_get_rate(gd->timer);
84}
85
Bin Meng9ca07eb2015-11-24 13:31:17 -070086uint64_t notrace get_ticks(void)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080087{
Bin Meng9ca07eb2015-11-24 13:31:17 -070088 u64 count;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080089 int ret;
90
Simon Glassc95fec32016-02-24 09:14:49 -070091 if (!gd->timer) {
Simon Glassc95fec32016-02-24 09:14:49 -070092 int ret;
93
Simon Glass64d445a2023-01-15 14:15:43 -070094 if (IS_ENABLED(CONFIG_TIMER_EARLY))
95 return timer_early_get_count();
96
Simon Glassc95fec32016-02-24 09:14:49 -070097 ret = dm_timer_init();
98 if (ret)
Sean Anderson4b2be782020-09-09 16:24:56 -040099 panic("Could not initialize timer (err %d)\n", ret);
Simon Glassc95fec32016-02-24 09:14:49 -0700100 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800101
102 ret = timer_get_count(gd->timer, &count);
Simon Glass6d1a8eb2020-12-23 08:11:16 -0700103 if (ret) {
104 if (spl_phase() > PHASE_TPL)
105 panic("Could not read count from timer (err %d)\n",
106 ret);
107 else
108 panic("no timer (err %d)\n", ret);
109 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800110
111 return count;
112}
Bin Meng9ca07eb2015-11-24 13:31:17 -0700113
114#else /* !CONFIG_TIMER */
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800115
Simon Glass19ea4672014-10-15 04:38:33 -0600116uint64_t __weak notrace get_ticks(void)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500117{
118 unsigned long now = timer_read_counter();
119
120 /* increment tbu if tbl has rolled over */
121 if (now < gd->timebase_l)
122 gd->timebase_h++;
123 gd->timebase_l = now;
Simon Glass19ea4672014-10-15 04:38:33 -0600124 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500125}
126
Bin Meng9ca07eb2015-11-24 13:31:17 -0700127#endif /* CONFIG_TIMER */
128
Pavel Machekfccacd32014-07-13 13:14:27 +0200129/* Returns time in milliseconds */
Simon Glass19ea4672014-10-15 04:38:33 -0600130static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500131{
Pavel Machekfccacd32014-07-13 13:14:27 +0200132 ulong div = get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500133
134 tick *= CONFIG_SYS_HZ;
135 do_div(tick, div);
136 return tick;
137}
138
Darwin Rambode351d62013-12-19 15:06:12 -0800139int __weak timer_init(void)
140{
141 return 0;
142}
143
Pavel Machekfccacd32014-07-13 13:14:27 +0200144/* Returns time in milliseconds */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500145ulong __weak get_timer(ulong base)
146{
147 return tick_to_time(get_ticks()) - base;
148}
149
Marek Vasut80e7e7c2019-10-15 22:43:41 +0200150static uint64_t notrace tick_to_time_us(uint64_t tick)
151{
152 ulong div = get_tbclk() / 1000;
153
154 tick *= CONFIG_SYS_HZ;
155 do_div(tick, div);
156 return tick;
157}
158
159uint64_t __weak get_timer_us(uint64_t base)
160{
161 return tick_to_time_us(get_ticks()) - base;
162}
163
Simon Glasse1ddf672020-07-09 18:43:14 -0600164unsigned long __weak get_timer_us_long(unsigned long base)
165{
166 return timer_get_us() - base;
167}
168
Rob Herring8dfafdd2013-10-04 10:22:41 -0500169unsigned long __weak notrace timer_get_us(void)
170{
171 return tick_to_time(get_ticks() * 1000);
172}
Pavel Machekfccacd32014-07-13 13:14:27 +0200173
Heinrich Schuchardt6a853db2019-06-02 21:02:10 +0200174uint64_t usec_to_tick(unsigned long usec)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500175{
Simon Glass19ea4672014-10-15 04:38:33 -0600176 uint64_t tick = usec;
Stephen Warren2cd1b572013-12-05 12:08:09 -0700177 tick *= get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500178 do_div(tick, 1000000);
179 return tick;
180}
181
182void __weak __udelay(unsigned long usec)
183{
Simon Glass19ea4672014-10-15 04:38:33 -0600184 uint64_t tmp;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500185
Pavel Machekfccacd32014-07-13 13:14:27 +0200186 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500187
Pavel Machekfccacd32014-07-13 13:14:27 +0200188 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500189 /*NOP*/;
190}
191
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100192/* ------------------------------------------------------------------------- */
193
194void udelay(unsigned long usec)
195{
196 ulong kv;
197
198 do {
Stefan Roese29caf932022-09-02 14:10:46 +0200199 schedule();
Tom Rini6e7df1d2023-01-10 11:19:45 -0500200 kv = usec > CFG_WD_PERIOD ? CFG_WD_PERIOD : usec;
Simon Glass07e11142020-05-10 11:40:10 -0600201 __udelay(kv);
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100202 usec -= kv;
203 } while(usec);
204}