blob: 3c49243e6a1ee0f3c7f95d73e9b081581e44be36 [file] [log] [blame]
Ingo van Lil3eb90ba2009-11-24 14:09:21 +01001/*
2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Ingo van Lil3eb90ba2009-11-24 14:09:21 +01006 */
7
8#include <common.h>
Thomas Chouc8a7ba92015-10-09 13:46:34 +08009#include <dm.h>
10#include <errno.h>
11#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}
39#else
Rob Herring65ba7ad2013-11-08 08:40:43 -060040extern unsigned long __weak timer_read_counter(void);
Rob Herring8dfafdd2013-10-04 10:22:41 -050041#endif
42
Thomas Chouc8a7ba92015-10-09 13:46:34 +080043#ifdef CONFIG_TIMER
Thomas Chouc8a7ba92015-10-09 13:46:34 +080044ulong notrace get_tbclk(void)
45{
Simon Glassc95fec32016-02-24 09:14:49 -070046 if (!gd->timer) {
47#ifdef CONFIG_TIMER_EARLY
48 return timer_early_get_rate();
49#else
50 int ret;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080051
Simon Glassc95fec32016-02-24 09:14:49 -070052 ret = dm_timer_init();
53 if (ret)
54 return ret;
55#endif
56 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080057
58 return timer_get_rate(gd->timer);
59}
60
Bin Meng9ca07eb2015-11-24 13:31:17 -070061uint64_t notrace get_ticks(void)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080062{
Bin Meng9ca07eb2015-11-24 13:31:17 -070063 u64 count;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080064 int ret;
65
Simon Glassc95fec32016-02-24 09:14:49 -070066 if (!gd->timer) {
67#ifdef CONFIG_TIMER_EARLY
68 return timer_early_get_count();
69#else
70 int ret;
71
72 ret = dm_timer_init();
73 if (ret)
74 return ret;
75#endif
76 }
Thomas Chouc8a7ba92015-10-09 13:46:34 +080077
78 ret = timer_get_count(gd->timer, &count);
79 if (ret)
80 return ret;
81
82 return count;
83}
Bin Meng9ca07eb2015-11-24 13:31:17 -070084
85#else /* !CONFIG_TIMER */
Thomas Chouc8a7ba92015-10-09 13:46:34 +080086
Simon Glass19ea4672014-10-15 04:38:33 -060087uint64_t __weak notrace get_ticks(void)
Rob Herring8dfafdd2013-10-04 10:22:41 -050088{
89 unsigned long now = timer_read_counter();
90
91 /* increment tbu if tbl has rolled over */
92 if (now < gd->timebase_l)
93 gd->timebase_h++;
94 gd->timebase_l = now;
Simon Glass19ea4672014-10-15 04:38:33 -060095 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring8dfafdd2013-10-04 10:22:41 -050096}
97
Bin Meng9ca07eb2015-11-24 13:31:17 -070098#endif /* CONFIG_TIMER */
99
Pavel Machekfccacd32014-07-13 13:14:27 +0200100/* Returns time in milliseconds */
Simon Glass19ea4672014-10-15 04:38:33 -0600101static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500102{
Pavel Machekfccacd32014-07-13 13:14:27 +0200103 ulong div = get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500104
105 tick *= CONFIG_SYS_HZ;
106 do_div(tick, div);
107 return tick;
108}
109
Darwin Rambode351d62013-12-19 15:06:12 -0800110int __weak timer_init(void)
111{
112 return 0;
113}
114
Pavel Machekfccacd32014-07-13 13:14:27 +0200115/* Returns time in milliseconds */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500116ulong __weak get_timer(ulong base)
117{
118 return tick_to_time(get_ticks()) - base;
119}
120
121unsigned long __weak notrace timer_get_us(void)
122{
123 return tick_to_time(get_ticks() * 1000);
124}
Pavel Machekfccacd32014-07-13 13:14:27 +0200125
Simon Glass19ea4672014-10-15 04:38:33 -0600126static uint64_t usec_to_tick(unsigned long usec)
Rob Herring8dfafdd2013-10-04 10:22:41 -0500127{
Simon Glass19ea4672014-10-15 04:38:33 -0600128 uint64_t tick = usec;
Stephen Warren2cd1b572013-12-05 12:08:09 -0700129 tick *= get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -0500130 do_div(tick, 1000000);
131 return tick;
132}
133
134void __weak __udelay(unsigned long usec)
135{
Simon Glass19ea4672014-10-15 04:38:33 -0600136 uint64_t tmp;
Rob Herring8dfafdd2013-10-04 10:22:41 -0500137
Pavel Machekfccacd32014-07-13 13:14:27 +0200138 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500139
Pavel Machekfccacd32014-07-13 13:14:27 +0200140 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring8dfafdd2013-10-04 10:22:41 -0500141 /*NOP*/;
142}
143
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100144/* ------------------------------------------------------------------------- */
145
146void udelay(unsigned long usec)
147{
148 ulong kv;
149
150 do {
151 WATCHDOG_RESET();
152 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
153 __udelay (kv);
154 usec -= kv;
155 } while(usec);
156}