blob: 75de48f90032cc1f213fedd5ca57c263cd2b1c9d [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>
Thomas Chouc8a7ba92015-10-09 13:46:34 +08008#include <dm.h>
9#include <errno.h>
Simon Glass10453152019-11-14 12:57:30 -070010#include <time.h>
Thomas Chouc8a7ba92015-10-09 13:46:34 +080011#include <timer.h>
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010012#include <watchdog.h>
Rob Herring8dfafdd2013-10-04 10:22:41 -050013#include <div64.h>
14#include <asm/io.h>
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010015
16#ifndef CONFIG_WD_PERIOD
Pavel Machekfccacd32014-07-13 13:14:27 +020017# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010018#endif
19
Rob Herring8dfafdd2013-10-04 10:22:41 -050020DECLARE_GLOBAL_DATA_PTR;
21
22#ifdef CONFIG_SYS_TIMER_RATE
Pavel Machekfccacd32014-07-13 13:14:27 +020023/* Returns tick rate in ticks per second */
Rob Herring8dfafdd2013-10-04 10:22:41 -050024ulong notrace get_tbclk(void)
25{
26 return CONFIG_SYS_TIMER_RATE;
27}
28#endif
29
30#ifdef CONFIG_SYS_TIMER_COUNTER
31unsigned long notrace timer_read_counter(void)
32{
33#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
34 return ~readl(CONFIG_SYS_TIMER_COUNTER);
35#else
36 return readl(CONFIG_SYS_TIMER_COUNTER);
37#endif
38}
Simon Glass9fb34b02017-05-22 05:05:22 -060039
40ulong timer_get_boot_us(void)
41{
42 ulong count = timer_read_counter();
43
44#if CONFIG_SYS_TIMER_RATE == 1000000
45 return count;
46#elif CONFIG_SYS_TIMER_RATE > 1000000
47 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
48#elif defined(CONFIG_SYS_TIMER_RATE)
49 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
50#else
51 /* Assume the counter is in microseconds */
52 return count;
53#endif
54}
55
Rob Herring8dfafdd2013-10-04 10:22:41 -050056#else
Rob Herring65ba7ad2013-11-08 08:40:43 -060057extern unsigned long __weak timer_read_counter(void);
Rob Herring8dfafdd2013-10-04 10:22:41 -050058#endif
59
Kever Yangf00c2622019-03-29 22:17:33 +080060#if CONFIG_IS_ENABLED(TIMER)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080061ulong notrace get_tbclk(void)
62{
Simon Glassc95fec32016-02-24 09:14:49 -070063 if (!gd->timer) {
64#ifdef CONFIG_TIMER_EARLY
65 return timer_early_get_rate();
66#else
67 int ret;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080068
Simon Glassc95fec32016-02-24 09:14:49 -070069 ret = dm_timer_init();
70 if (ret)
71 return ret;
72#endif
73 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080074
75 return timer_get_rate(gd->timer);
76}
77
Bin Meng9ca07eb2015-11-24 13:31:17 -070078uint64_t notrace get_ticks(void)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080079{
Bin Meng9ca07eb2015-11-24 13:31:17 -070080 u64 count;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080081 int ret;
82
Simon Glassc95fec32016-02-24 09:14:49 -070083 if (!gd->timer) {
84#ifdef CONFIG_TIMER_EARLY
85 return timer_early_get_count();
86#else
87 int ret;
88
89 ret = dm_timer_init();
90 if (ret)
91 return ret;
92#endif
93 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080094
95 ret = timer_get_count(gd->timer, &count);
96 if (ret)
97 return ret;
98
99 return count;
100}
Bin Meng9ca07eb2015-11-24 13:31:17 -0700101
102#else /* !CONFIG_TIMER */
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800103
Simon Glass19ea4672014-10-15 04:38:33 -0600104uint64_t __weak notrace get_ticks(void)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500105{
106 unsigned long now = timer_read_counter();
107
108 /* increment tbu if tbl has rolled over */
109 if (now < gd->timebase_l)
110 gd->timebase_h++;
111 gd->timebase_l = now;
Simon Glass19ea4672014-10-15 04:38:33 -0600112 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500113}
114
Bin Meng9ca07eb2015-11-24 13:31:17 -0700115#endif /* CONFIG_TIMER */
116
Pavel Machekfccacd32014-07-13 13:14:27 +0200117/* Returns time in milliseconds */
Simon Glass19ea4672014-10-15 04:38:33 -0600118static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500119{
Pavel Machekfccacd32014-07-13 13:14:27 +0200120 ulong div = get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500121
122 tick *= CONFIG_SYS_HZ;
123 do_div(tick, div);
124 return tick;
125}
126
Darwin Rambode351d62013-12-19 15:06:12 -0800127int __weak timer_init(void)
128{
129 return 0;
130}
131
Pavel Machekfccacd32014-07-13 13:14:27 +0200132/* Returns time in milliseconds */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500133ulong __weak get_timer(ulong base)
134{
135 return tick_to_time(get_ticks()) - base;
136}
137
Marek Vasut80e7e7c2019-10-15 22:43:41 +0200138static uint64_t notrace tick_to_time_us(uint64_t tick)
139{
140 ulong div = get_tbclk() / 1000;
141
142 tick *= CONFIG_SYS_HZ;
143 do_div(tick, div);
144 return tick;
145}
146
147uint64_t __weak get_timer_us(uint64_t base)
148{
149 return tick_to_time_us(get_ticks()) - base;
150}
151
Rob Herring8dfafdd2013-10-04 10:22:41 -0500152unsigned long __weak notrace timer_get_us(void)
153{
154 return tick_to_time(get_ticks() * 1000);
155}
Pavel Machekfccacd32014-07-13 13:14:27 +0200156
Heinrich Schuchardt6a853db2019-06-02 21:02:10 +0200157uint64_t usec_to_tick(unsigned long usec)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500158{
Simon Glass19ea4672014-10-15 04:38:33 -0600159 uint64_t tick = usec;
Stephen Warren2cd1b572013-12-05 12:08:09 -0700160 tick *= get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500161 do_div(tick, 1000000);
162 return tick;
163}
164
165void __weak __udelay(unsigned long usec)
166{
Simon Glass19ea4672014-10-15 04:38:33 -0600167 uint64_t tmp;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500168
Pavel Machekfccacd32014-07-13 13:14:27 +0200169 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500170
Pavel Machekfccacd32014-07-13 13:14:27 +0200171 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500172 /*NOP*/;
173}
174
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100175/* ------------------------------------------------------------------------- */
176
177void udelay(unsigned long usec)
178{
179 ulong kv;
180
181 do {
182 WATCHDOG_RESET();
183 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
184 __udelay (kv);
185 usec -= kv;
186 } while(usec);
187}