blob: 38a9758292a5ef4182466d62df22de7e2606f61e [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>
Simon Glass1ea97892020-05-10 11:40:00 -06008#include <bootstage.h>
Thomas Choufb798b12015-10-09 13:46:34 +08009#include <dm.h>
10#include <errno.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Simon Glass1b9c4b02020-12-23 08:11:16 -070012#include <spl.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070013#include <time.h>
Thomas Choufb798b12015-10-09 13:46:34 +080014#include <timer.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010015#include <watchdog.h>
Rob Herring57156942013-10-04 10:22:41 -050016#include <div64.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Rob Herring57156942013-10-04 10:22:41 -050018#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010020
21#ifndef CONFIG_WD_PERIOD
Pavel Machekcb21e372014-07-13 13:14:27 +020022# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010023#endif
24
Rob Herring57156942013-10-04 10:22:41 -050025DECLARE_GLOBAL_DATA_PTR;
26
27#ifdef CONFIG_SYS_TIMER_RATE
Pavel Machekcb21e372014-07-13 13:14:27 +020028/* Returns tick rate in ticks per second */
Rob Herring57156942013-10-04 10:22:41 -050029ulong notrace get_tbclk(void)
30{
31 return CONFIG_SYS_TIMER_RATE;
32}
33#endif
34
35#ifdef CONFIG_SYS_TIMER_COUNTER
36unsigned long notrace timer_read_counter(void)
37{
38#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
39 return ~readl(CONFIG_SYS_TIMER_COUNTER);
40#else
41 return readl(CONFIG_SYS_TIMER_COUNTER);
42#endif
43}
Simon Glass037ef5c2017-05-22 05:05:22 -060044
45ulong timer_get_boot_us(void)
46{
47 ulong count = timer_read_counter();
48
49#if CONFIG_SYS_TIMER_RATE == 1000000
50 return count;
51#elif CONFIG_SYS_TIMER_RATE > 1000000
52 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
53#elif defined(CONFIG_SYS_TIMER_RATE)
54 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
55#else
56 /* Assume the counter is in microseconds */
57 return count;
58#endif
59}
60
Rob Herring57156942013-10-04 10:22:41 -050061#else
Rob Herring316c5c82013-11-08 08:40:43 -060062extern unsigned long __weak timer_read_counter(void);
Rob Herring57156942013-10-04 10:22:41 -050063#endif
64
Kever Yangf0b50242019-03-29 22:17:33 +080065#if CONFIG_IS_ENABLED(TIMER)
Thomas Choufb798b12015-10-09 13:46:34 +080066ulong notrace get_tbclk(void)
67{
Simon Glass32f6c172016-02-24 09:14:49 -070068 if (!gd->timer) {
69#ifdef CONFIG_TIMER_EARLY
70 return timer_early_get_rate();
71#else
72 int ret;
Thomas Choufb798b12015-10-09 13:46:34 +080073
Simon Glass32f6c172016-02-24 09:14:49 -070074 ret = dm_timer_init();
75 if (ret)
76 return ret;
77#endif
78 }
Thomas Choufb798b12015-10-09 13:46:34 +080079
80 return timer_get_rate(gd->timer);
81}
82
Bin Mengab841b62015-11-24 13:31:17 -070083uint64_t notrace get_ticks(void)
Thomas Choufb798b12015-10-09 13:46:34 +080084{
Bin Mengab841b62015-11-24 13:31:17 -070085 u64 count;
Thomas Choufb798b12015-10-09 13:46:34 +080086 int ret;
87
Simon Glass32f6c172016-02-24 09:14:49 -070088 if (!gd->timer) {
89#ifdef CONFIG_TIMER_EARLY
90 return timer_early_get_count();
91#else
92 int ret;
93
94 ret = dm_timer_init();
95 if (ret)
Sean Anderson11da6362020-09-09 16:24:56 -040096 panic("Could not initialize timer (err %d)\n", ret);
Simon Glass32f6c172016-02-24 09:14:49 -070097#endif
98 }
Thomas Choufb798b12015-10-09 13:46:34 +080099
100 ret = timer_get_count(gd->timer, &count);
Simon Glass1b9c4b02020-12-23 08:11:16 -0700101 if (ret) {
102 if (spl_phase() > PHASE_TPL)
103 panic("Could not read count from timer (err %d)\n",
104 ret);
105 else
106 panic("no timer (err %d)\n", ret);
107 }
Thomas Choufb798b12015-10-09 13:46:34 +0800108
109 return count;
110}
Bin Mengab841b62015-11-24 13:31:17 -0700111
112#else /* !CONFIG_TIMER */
Thomas Choufb798b12015-10-09 13:46:34 +0800113
Simon Glass0e103162014-10-15 04:38:33 -0600114uint64_t __weak notrace get_ticks(void)
Rob Herring57156942013-10-04 10:22:41 -0500115{
116 unsigned long now = timer_read_counter();
117
118 /* increment tbu if tbl has rolled over */
119 if (now < gd->timebase_l)
120 gd->timebase_h++;
121 gd->timebase_l = now;
Simon Glass0e103162014-10-15 04:38:33 -0600122 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring57156942013-10-04 10:22:41 -0500123}
124
Bin Mengab841b62015-11-24 13:31:17 -0700125#endif /* CONFIG_TIMER */
126
Pavel Machekcb21e372014-07-13 13:14:27 +0200127/* Returns time in milliseconds */
Simon Glass0e103162014-10-15 04:38:33 -0600128static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring57156942013-10-04 10:22:41 -0500129{
Pavel Machekcb21e372014-07-13 13:14:27 +0200130 ulong div = get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -0500131
132 tick *= CONFIG_SYS_HZ;
133 do_div(tick, div);
134 return tick;
135}
136
Darwin Rambof283e4f2013-12-19 15:06:12 -0800137int __weak timer_init(void)
138{
139 return 0;
140}
141
Pavel Machekcb21e372014-07-13 13:14:27 +0200142/* Returns time in milliseconds */
Rob Herring57156942013-10-04 10:22:41 -0500143ulong __weak get_timer(ulong base)
144{
145 return tick_to_time(get_ticks()) - base;
146}
147
Marek Vasutebc50332019-10-15 22:43:41 +0200148static uint64_t notrace tick_to_time_us(uint64_t tick)
149{
150 ulong div = get_tbclk() / 1000;
151
152 tick *= CONFIG_SYS_HZ;
153 do_div(tick, div);
154 return tick;
155}
156
157uint64_t __weak get_timer_us(uint64_t base)
158{
159 return tick_to_time_us(get_ticks()) - base;
160}
161
Simon Glass82213d82020-07-09 18:43:14 -0600162unsigned long __weak get_timer_us_long(unsigned long base)
163{
164 return timer_get_us() - base;
165}
166
Rob Herring57156942013-10-04 10:22:41 -0500167unsigned long __weak notrace timer_get_us(void)
168{
169 return tick_to_time(get_ticks() * 1000);
170}
Pavel Machekcb21e372014-07-13 13:14:27 +0200171
Heinrich Schuchardtdaf69fc2019-06-02 21:02:10 +0200172uint64_t usec_to_tick(unsigned long usec)
Rob Herring57156942013-10-04 10:22:41 -0500173{
Simon Glass0e103162014-10-15 04:38:33 -0600174 uint64_t tick = usec;
Stephen Warrenff81b482013-12-05 12:08:09 -0700175 tick *= get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -0500176 do_div(tick, 1000000);
177 return tick;
178}
179
180void __weak __udelay(unsigned long usec)
181{
Simon Glass0e103162014-10-15 04:38:33 -0600182 uint64_t tmp;
Rob Herring57156942013-10-04 10:22:41 -0500183
Pavel Machekcb21e372014-07-13 13:14:27 +0200184 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring57156942013-10-04 10:22:41 -0500185
Pavel Machekcb21e372014-07-13 13:14:27 +0200186 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring57156942013-10-04 10:22:41 -0500187 /*NOP*/;
188}
189
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100190/* ------------------------------------------------------------------------- */
191
192void udelay(unsigned long usec)
193{
194 ulong kv;
195
196 do {
197 WATCHDOG_RESET();
198 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
Simon Glass0db4b942020-05-10 11:40:10 -0600199 __udelay(kv);
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100200 usec -= kv;
201 } while(usec);
202}