blob: 3bf678a23278db79e523e5fe1cb9db933d19ecd3 [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>
10#include <timer.h>
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010011#include <watchdog.h>
Rob Herring8dfafdd2013-10-04 10:22:41 -050012#include <div64.h>
13#include <asm/io.h>
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010014
15#ifndef CONFIG_WD_PERIOD
Pavel Machekfccacd32014-07-13 13:14:27 +020016# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010017#endif
18
Rob Herring8dfafdd2013-10-04 10:22:41 -050019DECLARE_GLOBAL_DATA_PTR;
20
21#ifdef CONFIG_SYS_TIMER_RATE
Pavel Machekfccacd32014-07-13 13:14:27 +020022/* Returns tick rate in ticks per second */
Rob Herring8dfafdd2013-10-04 10:22:41 -050023ulong notrace get_tbclk(void)
24{
25 return CONFIG_SYS_TIMER_RATE;
26}
27#endif
28
29#ifdef CONFIG_SYS_TIMER_COUNTER
30unsigned long notrace timer_read_counter(void)
31{
32#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
33 return ~readl(CONFIG_SYS_TIMER_COUNTER);
34#else
35 return readl(CONFIG_SYS_TIMER_COUNTER);
36#endif
37}
Simon Glass9fb34b02017-05-22 05:05:22 -060038
39ulong timer_get_boot_us(void)
40{
41 ulong count = timer_read_counter();
42
43#if CONFIG_SYS_TIMER_RATE == 1000000
44 return count;
45#elif CONFIG_SYS_TIMER_RATE > 1000000
46 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
47#elif defined(CONFIG_SYS_TIMER_RATE)
48 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
49#else
50 /* Assume the counter is in microseconds */
51 return count;
52#endif
53}
54
Rob Herring8dfafdd2013-10-04 10:22:41 -050055#else
Rob Herring65ba7ad2013-11-08 08:40:43 -060056extern unsigned long __weak timer_read_counter(void);
Rob Herring8dfafdd2013-10-04 10:22:41 -050057#endif
58
Thomas Chouc8a7ba92015-10-09 13:46:34 +080059#ifdef CONFIG_TIMER
Thomas Chouc8a7ba92015-10-09 13:46:34 +080060ulong notrace get_tbclk(void)
61{
Simon Glassc95fec32016-02-24 09:14:49 -070062 if (!gd->timer) {
63#ifdef CONFIG_TIMER_EARLY
64 return timer_early_get_rate();
65#else
66 int ret;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080067
Simon Glassc95fec32016-02-24 09:14:49 -070068 ret = dm_timer_init();
69 if (ret)
70 return ret;
71#endif
72 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080073
74 return timer_get_rate(gd->timer);
75}
76
Bin Meng9ca07eb2015-11-24 13:31:17 -070077uint64_t notrace get_ticks(void)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080078{
Bin Meng9ca07eb2015-11-24 13:31:17 -070079 u64 count;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080080 int ret;
81
Simon Glassc95fec32016-02-24 09:14:49 -070082 if (!gd->timer) {
83#ifdef CONFIG_TIMER_EARLY
84 return timer_early_get_count();
85#else
86 int ret;
87
88 ret = dm_timer_init();
89 if (ret)
90 return ret;
91#endif
92 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080093
94 ret = timer_get_count(gd->timer, &count);
95 if (ret)
96 return ret;
97
98 return count;
99}
Bin Meng9ca07eb2015-11-24 13:31:17 -0700100
101#else /* !CONFIG_TIMER */
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800102
Simon Glass19ea4672014-10-15 04:38:33 -0600103uint64_t __weak notrace get_ticks(void)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500104{
105 unsigned long now = timer_read_counter();
106
107 /* increment tbu if tbl has rolled over */
108 if (now < gd->timebase_l)
109 gd->timebase_h++;
110 gd->timebase_l = now;
Simon Glass19ea4672014-10-15 04:38:33 -0600111 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500112}
113
Bin Meng9ca07eb2015-11-24 13:31:17 -0700114#endif /* CONFIG_TIMER */
115
Pavel Machekfccacd32014-07-13 13:14:27 +0200116/* Returns time in milliseconds */
Simon Glass19ea4672014-10-15 04:38:33 -0600117static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500118{
Pavel Machekfccacd32014-07-13 13:14:27 +0200119 ulong div = get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500120
121 tick *= CONFIG_SYS_HZ;
122 do_div(tick, div);
123 return tick;
124}
125
Darwin Rambode351d62013-12-19 15:06:12 -0800126int __weak timer_init(void)
127{
128 return 0;
129}
130
Pavel Machekfccacd32014-07-13 13:14:27 +0200131/* Returns time in milliseconds */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500132ulong __weak get_timer(ulong base)
133{
134 return tick_to_time(get_ticks()) - base;
135}
136
137unsigned long __weak notrace timer_get_us(void)
138{
139 return tick_to_time(get_ticks() * 1000);
140}
Pavel Machekfccacd32014-07-13 13:14:27 +0200141
Simon Glass19ea4672014-10-15 04:38:33 -0600142static uint64_t usec_to_tick(unsigned long usec)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500143{
Simon Glass19ea4672014-10-15 04:38:33 -0600144 uint64_t tick = usec;
Stephen Warren2cd1b572013-12-05 12:08:09 -0700145 tick *= get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500146 do_div(tick, 1000000);
147 return tick;
148}
149
150void __weak __udelay(unsigned long usec)
151{
Simon Glass19ea4672014-10-15 04:38:33 -0600152 uint64_t tmp;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500153
Pavel Machekfccacd32014-07-13 13:14:27 +0200154 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500155
Pavel Machekfccacd32014-07-13 13:14:27 +0200156 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500157 /*NOP*/;
158}
159
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100160/* ------------------------------------------------------------------------- */
161
162void udelay(unsigned long usec)
163{
164 ulong kv;
165
166 do {
167 WATCHDOG_RESET();
168 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
169 __udelay (kv);
170 usec -= kv;
171 } while(usec);
172}