blob: 96074b84af69342f1e58b10a5c9fe4ab9299d264 [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
22#ifndef CONFIG_WD_PERIOD
Pavel Machekfccacd32014-07-13 13:14:27 +020023# define CONFIG_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
28#ifdef CONFIG_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{
32 return CONFIG_SYS_TIMER_RATE;
33}
34#endif
35
36#ifdef CONFIG_SYS_TIMER_COUNTER
37unsigned long notrace timer_read_counter(void)
38{
39#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
40 return ~readl(CONFIG_SYS_TIMER_COUNTER);
41#else
42 return readl(CONFIG_SYS_TIMER_COUNTER);
43#endif
44}
Simon Glass9fb34b02017-05-22 05:05:22 -060045
46ulong timer_get_boot_us(void)
47{
48 ulong count = timer_read_counter();
49
50#if CONFIG_SYS_TIMER_RATE == 1000000
51 return count;
52#elif CONFIG_SYS_TIMER_RATE > 1000000
53 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
54#elif defined(CONFIG_SYS_TIMER_RATE)
55 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
56#else
57 /* Assume the counter is in microseconds */
58 return count;
59#endif
60}
61
Rob Herring8dfafdd2013-10-04 10:22:41 -050062#else
Rob Herring65ba7ad2013-11-08 08:40:43 -060063extern unsigned long __weak timer_read_counter(void);
Rob Herring8dfafdd2013-10-04 10:22:41 -050064#endif
65
Kever Yangf00c2622019-03-29 22:17:33 +080066#if CONFIG_IS_ENABLED(TIMER)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080067ulong notrace get_tbclk(void)
68{
Simon Glassc95fec32016-02-24 09:14:49 -070069 if (!gd->timer) {
70#ifdef CONFIG_TIMER_EARLY
71 return timer_early_get_rate();
72#else
73 int ret;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080074
Simon Glassc95fec32016-02-24 09:14:49 -070075 ret = dm_timer_init();
76 if (ret)
77 return ret;
78#endif
79 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080080
81 return timer_get_rate(gd->timer);
82}
83
Bin Meng9ca07eb2015-11-24 13:31:17 -070084uint64_t notrace get_ticks(void)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080085{
Bin Meng9ca07eb2015-11-24 13:31:17 -070086 u64 count;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080087 int ret;
88
Simon Glassc95fec32016-02-24 09:14:49 -070089 if (!gd->timer) {
90#ifdef CONFIG_TIMER_EARLY
91 return timer_early_get_count();
92#else
93 int ret;
94
95 ret = dm_timer_init();
96 if (ret)
Sean Anderson4b2be782020-09-09 16:24:56 -040097 panic("Could not initialize timer (err %d)\n", ret);
Simon Glassc95fec32016-02-24 09:14:49 -070098#endif
99 }
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 {
198 WATCHDOG_RESET();
199 kv = usec > CONFIG_WD_PERIOD ? CONFIG_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}