]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/time.c
Merge branch 'master' of git://www.denx.de/git/u-boot-imx
[people/ms/u-boot.git] / lib / time.c
CommitLineData
3eb90bad
IL
1/*
2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
3eb90bad
IL
6 */
7
8#include <common.h>
c8a7ba9e
TC
9#include <dm.h>
10#include <errno.h>
11#include <timer.h>
3eb90bad 12#include <watchdog.h>
8dfafdde
RH
13#include <div64.h>
14#include <asm/io.h>
3eb90bad
IL
15
16#ifndef CONFIG_WD_PERIOD
fccacd3b 17# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
3eb90bad
IL
18#endif
19
8dfafdde
RH
20DECLARE_GLOBAL_DATA_PTR;
21
22#ifdef CONFIG_SYS_TIMER_RATE
fccacd3b 23/* Returns tick rate in ticks per second */
8dfafdde
RH
24ulong 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
65ba7add 40extern unsigned long __weak timer_read_counter(void);
8dfafdde
RH
41#endif
42
c8a7ba9e 43#ifdef CONFIG_TIMER
c8a7ba9e
TC
44ulong notrace get_tbclk(void)
45{
c95fec31
SG
46 if (!gd->timer) {
47#ifdef CONFIG_TIMER_EARLY
48 return timer_early_get_rate();
49#else
50 int ret;
c8a7ba9e 51
c95fec31
SG
52 ret = dm_timer_init();
53 if (ret)
54 return ret;
55#endif
56 }
c8a7ba9e
TC
57
58 return timer_get_rate(gd->timer);
59}
60
9ca07ebb 61uint64_t notrace get_ticks(void)
c8a7ba9e 62{
9ca07ebb 63 u64 count;
c8a7ba9e
TC
64 int ret;
65
c95fec31
SG
66 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 }
c8a7ba9e
TC
77
78 ret = timer_get_count(gd->timer, &count);
79 if (ret)
80 return ret;
81
82 return count;
83}
9ca07ebb
BM
84
85#else /* !CONFIG_TIMER */
c8a7ba9e 86
19ea4678 87uint64_t __weak notrace get_ticks(void)
8dfafdde
RH
88{
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;
19ea4678 95 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
8dfafdde
RH
96}
97
9ca07ebb
BM
98#endif /* CONFIG_TIMER */
99
fccacd3b 100/* Returns time in milliseconds */
19ea4678 101static uint64_t notrace tick_to_time(uint64_t tick)
8dfafdde 102{
fccacd3b 103 ulong div = get_tbclk();
8dfafdde
RH
104
105 tick *= CONFIG_SYS_HZ;
106 do_div(tick, div);
107 return tick;
108}
109
de351d6b
DR
110int __weak timer_init(void)
111{
112 return 0;
113}
114
fccacd3b 115/* Returns time in milliseconds */
8dfafdde
RH
116ulong __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}
fccacd3b 125
19ea4678 126static uint64_t usec_to_tick(unsigned long usec)
8dfafdde 127{
19ea4678 128 uint64_t tick = usec;
2cd1b572 129 tick *= get_tbclk();
8dfafdde
RH
130 do_div(tick, 1000000);
131 return tick;
132}
133
134void __weak __udelay(unsigned long usec)
135{
19ea4678 136 uint64_t tmp;
8dfafdde 137
fccacd3b 138 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
8dfafdde 139
fccacd3b 140 while (get_ticks() < tmp+1) /* loop till event */
8dfafdde
RH
141 /*NOP*/;
142}
143
3eb90bad
IL
144/* ------------------------------------------------------------------------- */
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}