blob: e9f6861b9843773f780ec3bd278340dcb55a2a2e [file] [log] [blame]
Ingo van Lilf0f778a2009-11-24 14:09:21 +01001/*
2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Ingo van Lilf0f778a2009-11-24 14:09:21 +01006 */
7
8#include <common.h>
Thomas Choufb798b12015-10-09 13:46:34 +08009#include <dm.h>
10#include <errno.h>
11#include <timer.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010012#include <watchdog.h>
Rob Herring57156942013-10-04 10:22:41 -050013#include <div64.h>
14#include <asm/io.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010015
16#ifndef CONFIG_WD_PERIOD
Pavel Machekcb21e372014-07-13 13:14:27 +020017# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010018#endif
19
Rob Herring57156942013-10-04 10:22:41 -050020DECLARE_GLOBAL_DATA_PTR;
21
22#ifdef CONFIG_SYS_TIMER_RATE
Pavel Machekcb21e372014-07-13 13:14:27 +020023/* Returns tick rate in ticks per second */
Rob Herring57156942013-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 Herring316c5c82013-11-08 08:40:43 -060040extern unsigned long __weak timer_read_counter(void);
Rob Herring57156942013-10-04 10:22:41 -050041#endif
42
Thomas Choufb798b12015-10-09 13:46:34 +080043#ifdef CONFIG_TIMER
Thomas Choufb798b12015-10-09 13:46:34 +080044ulong notrace get_tbclk(void)
45{
46 int ret;
47
48 ret = dm_timer_init();
49 if (ret)
50 return ret;
51
52 return timer_get_rate(gd->timer);
53}
54
Bin Mengab841b62015-11-24 13:31:17 -070055uint64_t notrace get_ticks(void)
Thomas Choufb798b12015-10-09 13:46:34 +080056{
Bin Mengab841b62015-11-24 13:31:17 -070057 u64 count;
Thomas Choufb798b12015-10-09 13:46:34 +080058 int ret;
59
60 ret = dm_timer_init();
61 if (ret)
62 return ret;
63
64 ret = timer_get_count(gd->timer, &count);
65 if (ret)
66 return ret;
67
68 return count;
69}
Bin Mengab841b62015-11-24 13:31:17 -070070
71#else /* !CONFIG_TIMER */
Thomas Choufb798b12015-10-09 13:46:34 +080072
Simon Glass0e103162014-10-15 04:38:33 -060073uint64_t __weak notrace get_ticks(void)
Rob Herring57156942013-10-04 10:22:41 -050074{
75 unsigned long now = timer_read_counter();
76
77 /* increment tbu if tbl has rolled over */
78 if (now < gd->timebase_l)
79 gd->timebase_h++;
80 gd->timebase_l = now;
Simon Glass0e103162014-10-15 04:38:33 -060081 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring57156942013-10-04 10:22:41 -050082}
83
Bin Mengab841b62015-11-24 13:31:17 -070084#endif /* CONFIG_TIMER */
85
Pavel Machekcb21e372014-07-13 13:14:27 +020086/* Returns time in milliseconds */
Simon Glass0e103162014-10-15 04:38:33 -060087static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring57156942013-10-04 10:22:41 -050088{
Pavel Machekcb21e372014-07-13 13:14:27 +020089 ulong div = get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -050090
91 tick *= CONFIG_SYS_HZ;
92 do_div(tick, div);
93 return tick;
94}
95
Darwin Rambof283e4f2013-12-19 15:06:12 -080096int __weak timer_init(void)
97{
98 return 0;
99}
100
Pavel Machekcb21e372014-07-13 13:14:27 +0200101/* Returns time in milliseconds */
Rob Herring57156942013-10-04 10:22:41 -0500102ulong __weak get_timer(ulong base)
103{
104 return tick_to_time(get_ticks()) - base;
105}
106
107unsigned long __weak notrace timer_get_us(void)
108{
109 return tick_to_time(get_ticks() * 1000);
110}
Pavel Machekcb21e372014-07-13 13:14:27 +0200111
Simon Glass0e103162014-10-15 04:38:33 -0600112static uint64_t usec_to_tick(unsigned long usec)
Rob Herring57156942013-10-04 10:22:41 -0500113{
Simon Glass0e103162014-10-15 04:38:33 -0600114 uint64_t tick = usec;
Stephen Warrenff81b482013-12-05 12:08:09 -0700115 tick *= get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -0500116 do_div(tick, 1000000);
117 return tick;
118}
119
120void __weak __udelay(unsigned long usec)
121{
Simon Glass0e103162014-10-15 04:38:33 -0600122 uint64_t tmp;
Rob Herring57156942013-10-04 10:22:41 -0500123
Pavel Machekcb21e372014-07-13 13:14:27 +0200124 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring57156942013-10-04 10:22:41 -0500125
Pavel Machekcb21e372014-07-13 13:14:27 +0200126 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring57156942013-10-04 10:22:41 -0500127 /*NOP*/;
128}
129
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100130/* ------------------------------------------------------------------------- */
131
132void udelay(unsigned long usec)
133{
134 ulong kv;
135
136 do {
137 WATCHDOG_RESET();
138 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
139 __udelay (kv);
140 usec -= kv;
141 } while(usec);
142}
Anatolij Gustschin589c2ff2011-10-12 02:31:39 +0000143
144void mdelay(unsigned long msec)
145{
146 while (msec--)
147 udelay(1000);
148}