blob: cc6944ec345d8358af96da4a9be46ca76dae7c5d [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>
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>
17#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010019
20#ifndef CONFIG_WD_PERIOD
Pavel Machekfccacd32014-07-13 13:14:27 +020021# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010022#endif
23
Rob Herring8dfafdd2013-10-04 10:22:41 -050024DECLARE_GLOBAL_DATA_PTR;
25
26#ifdef CONFIG_SYS_TIMER_RATE
Pavel Machekfccacd32014-07-13 13:14:27 +020027/* Returns tick rate in ticks per second */
Rob Herring8dfafdd2013-10-04 10:22:41 -050028ulong notrace get_tbclk(void)
29{
30 return CONFIG_SYS_TIMER_RATE;
31}
32#endif
33
34#ifdef CONFIG_SYS_TIMER_COUNTER
35unsigned long notrace timer_read_counter(void)
36{
37#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
38 return ~readl(CONFIG_SYS_TIMER_COUNTER);
39#else
40 return readl(CONFIG_SYS_TIMER_COUNTER);
41#endif
42}
Simon Glass9fb34b02017-05-22 05:05:22 -060043
44ulong timer_get_boot_us(void)
45{
46 ulong count = timer_read_counter();
47
48#if CONFIG_SYS_TIMER_RATE == 1000000
49 return count;
50#elif CONFIG_SYS_TIMER_RATE > 1000000
51 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
52#elif defined(CONFIG_SYS_TIMER_RATE)
53 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
54#else
55 /* Assume the counter is in microseconds */
56 return count;
57#endif
58}
59
Rob Herring8dfafdd2013-10-04 10:22:41 -050060#else
Rob Herring65ba7ad2013-11-08 08:40:43 -060061extern unsigned long __weak timer_read_counter(void);
Rob Herring8dfafdd2013-10-04 10:22:41 -050062#endif
63
Kever Yangf00c2622019-03-29 22:17:33 +080064#if CONFIG_IS_ENABLED(TIMER)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080065ulong notrace get_tbclk(void)
66{
Simon Glassc95fec32016-02-24 09:14:49 -070067 if (!gd->timer) {
68#ifdef CONFIG_TIMER_EARLY
69 return timer_early_get_rate();
70#else
71 int ret;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080072
Simon Glassc95fec32016-02-24 09:14:49 -070073 ret = dm_timer_init();
74 if (ret)
75 return ret;
76#endif
77 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080078
79 return timer_get_rate(gd->timer);
80}
81
Bin Meng9ca07eb2015-11-24 13:31:17 -070082uint64_t notrace get_ticks(void)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080083{
Bin Meng9ca07eb2015-11-24 13:31:17 -070084 u64 count;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080085 int ret;
86
Simon Glassc95fec32016-02-24 09:14:49 -070087 if (!gd->timer) {
88#ifdef CONFIG_TIMER_EARLY
89 return timer_early_get_count();
90#else
91 int ret;
92
93 ret = dm_timer_init();
94 if (ret)
Sean Anderson4b2be782020-09-09 16:24:56 -040095 panic("Could not initialize timer (err %d)\n", ret);
Simon Glassc95fec32016-02-24 09:14:49 -070096#endif
97 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080098
99 ret = timer_get_count(gd->timer, &count);
Simon Glass6d1a8eb2020-12-23 08:11:16 -0700100 if (ret) {
101 if (spl_phase() > PHASE_TPL)
102 panic("Could not read count from timer (err %d)\n",
103 ret);
104 else
105 panic("no timer (err %d)\n", ret);
106 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800107
108 return count;
109}
Bin Meng9ca07eb2015-11-24 13:31:17 -0700110
111#else /* !CONFIG_TIMER */
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800112
Simon Glass19ea4672014-10-15 04:38:33 -0600113uint64_t __weak notrace get_ticks(void)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500114{
115 unsigned long now = timer_read_counter();
116
117 /* increment tbu if tbl has rolled over */
118 if (now < gd->timebase_l)
119 gd->timebase_h++;
120 gd->timebase_l = now;
Simon Glass19ea4672014-10-15 04:38:33 -0600121 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500122}
123
Bin Meng9ca07eb2015-11-24 13:31:17 -0700124#endif /* CONFIG_TIMER */
125
Pavel Machekfccacd32014-07-13 13:14:27 +0200126/* Returns time in milliseconds */
Simon Glass19ea4672014-10-15 04:38:33 -0600127static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500128{
Pavel Machekfccacd32014-07-13 13:14:27 +0200129 ulong div = get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500130
131 tick *= CONFIG_SYS_HZ;
132 do_div(tick, div);
133 return tick;
134}
135
Darwin Rambode351d62013-12-19 15:06:12 -0800136int __weak timer_init(void)
137{
138 return 0;
139}
140
Pavel Machekfccacd32014-07-13 13:14:27 +0200141/* Returns time in milliseconds */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500142ulong __weak get_timer(ulong base)
143{
144 return tick_to_time(get_ticks()) - base;
145}
146
Marek Vasut80e7e7c2019-10-15 22:43:41 +0200147static uint64_t notrace tick_to_time_us(uint64_t tick)
148{
149 ulong div = get_tbclk() / 1000;
150
151 tick *= CONFIG_SYS_HZ;
152 do_div(tick, div);
153 return tick;
154}
155
156uint64_t __weak get_timer_us(uint64_t base)
157{
158 return tick_to_time_us(get_ticks()) - base;
159}
160
Simon Glasse1ddf672020-07-09 18:43:14 -0600161unsigned long __weak get_timer_us_long(unsigned long base)
162{
163 return timer_get_us() - base;
164}
165
Rob Herring8dfafdd2013-10-04 10:22:41 -0500166unsigned long __weak notrace timer_get_us(void)
167{
168 return tick_to_time(get_ticks() * 1000);
169}
Pavel Machekfccacd32014-07-13 13:14:27 +0200170
Heinrich Schuchardt6a853db2019-06-02 21:02:10 +0200171uint64_t usec_to_tick(unsigned long usec)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500172{
Simon Glass19ea4672014-10-15 04:38:33 -0600173 uint64_t tick = usec;
Stephen Warren2cd1b572013-12-05 12:08:09 -0700174 tick *= get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500175 do_div(tick, 1000000);
176 return tick;
177}
178
179void __weak __udelay(unsigned long usec)
180{
Simon Glass19ea4672014-10-15 04:38:33 -0600181 uint64_t tmp;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500182
Pavel Machekfccacd32014-07-13 13:14:27 +0200183 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500184
Pavel Machekfccacd32014-07-13 13:14:27 +0200185 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500186 /*NOP*/;
187}
188
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100189/* ------------------------------------------------------------------------- */
190
191void udelay(unsigned long usec)
192{
193 ulong kv;
194
195 do {
196 WATCHDOG_RESET();
197 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
Simon Glass07e11142020-05-10 11:40:10 -0600198 __udelay(kv);
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100199 usec -= kv;
200 } while(usec);
201}