]> Git Repo - u-boot.git/blame - lib/time.c
Merge patch series "Arm: npcm: modify npcm8xx boot setting"
[u-boot.git] / lib / time.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
3eb90bad
IL
2/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, [email protected].
3eb90bad
IL
5 */
6
2f8a6db5 7#include <clock_legacy.h>
52f24238 8#include <bootstage.h>
c8a7ba9e
TC
9#include <dm.h>
10#include <errno.h>
691d719d 11#include <init.h>
6d1a8ebe 12#include <spl.h>
1045315d 13#include <time.h>
c8a7ba9e 14#include <timer.h>
3eb90bad 15#include <watchdog.h>
8dfafdde 16#include <div64.h>
401d1c4f 17#include <asm/global_data.h>
8dfafdde 18#include <asm/io.h>
c05ed00a 19#include <linux/delay.h>
3eb90bad 20
6e7df1d1
TR
21#ifndef CFG_WD_PERIOD
22# define CFG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
3eb90bad
IL
23#endif
24
8dfafdde
RH
25DECLARE_GLOBAL_DATA_PTR;
26
65cc0e2a 27#ifdef CFG_SYS_TIMER_RATE
fccacd3b 28/* Returns tick rate in ticks per second */
8dfafdde
RH
29ulong notrace get_tbclk(void)
30{
65cc0e2a 31 return CFG_SYS_TIMER_RATE;
8dfafdde
RH
32}
33#endif
34
65cc0e2a 35#ifdef CFG_SYS_TIMER_COUNTER
8dfafdde
RH
36unsigned long notrace timer_read_counter(void)
37{
38#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
65cc0e2a 39 return ~readl(CFG_SYS_TIMER_COUNTER);
8dfafdde 40#else
65cc0e2a 41 return readl(CFG_SYS_TIMER_COUNTER);
8dfafdde
RH
42#endif
43}
9fb34b01
SG
44
45ulong timer_get_boot_us(void)
46{
47 ulong count = timer_read_counter();
48
65cc0e2a
TR
49#ifdef CFG_SYS_TIMER_RATE
50 const ulong timer_rate = CFG_SYS_TIMER_RATE;
616278bd
MW
51
52 if (timer_rate == 1000000)
53 return count;
54 else if (timer_rate > 1000000)
55 return lldiv(count, timer_rate / 1000000);
56 else
57 return (unsigned long long)count * 1000000 / timer_rate;
9fb34b01
SG
58#else
59 /* Assume the counter is in microseconds */
60 return count;
61#endif
62}
63
8dfafdde 64#else
ea3d28ec 65extern unsigned long timer_read_counter(void);
8dfafdde
RH
66#endif
67
f00c2628 68#if CONFIG_IS_ENABLED(TIMER)
c8a7ba9e
TC
69ulong notrace get_tbclk(void)
70{
c95fec31 71 if (!gd->timer) {
c95fec31 72 int ret;
c8a7ba9e 73
64d445a3
SG
74 if (IS_ENABLED(CONFIG_TIMER_EARLY))
75 return timer_early_get_rate();
76
c95fec31
SG
77 ret = dm_timer_init();
78 if (ret)
79 return ret;
c95fec31 80 }
c8a7ba9e
TC
81
82 return timer_get_rate(gd->timer);
83}
84
9ca07ebb 85uint64_t notrace get_ticks(void)
c8a7ba9e 86{
9ca07ebb 87 u64 count;
c8a7ba9e
TC
88 int ret;
89
c95fec31 90 if (!gd->timer) {
c95fec31
SG
91 int ret;
92
64d445a3
SG
93 if (IS_ENABLED(CONFIG_TIMER_EARLY))
94 return timer_early_get_count();
95
c95fec31
SG
96 ret = dm_timer_init();
97 if (ret)
4b2be78a 98 panic("Could not initialize timer (err %d)\n", ret);
c95fec31 99 }
c8a7ba9e
TC
100
101 ret = timer_get_count(gd->timer, &count);
6d1a8ebe
SG
102 if (ret) {
103 if (spl_phase() > PHASE_TPL)
104 panic("Could not read count from timer (err %d)\n",
105 ret);
106 else
107 panic("no timer (err %d)\n", ret);
108 }
c8a7ba9e
TC
109
110 return count;
111}
9ca07ebb
BM
112
113#else /* !CONFIG_TIMER */
c8a7ba9e 114
19ea4678 115uint64_t __weak notrace get_ticks(void)
8dfafdde
RH
116{
117 unsigned long now = timer_read_counter();
118
119 /* increment tbu if tbl has rolled over */
120 if (now < gd->timebase_l)
121 gd->timebase_h++;
122 gd->timebase_l = now;
19ea4678 123 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
8dfafdde
RH
124}
125
9ca07ebb
BM
126#endif /* CONFIG_TIMER */
127
fccacd3b 128/* Returns time in milliseconds */
19ea4678 129static uint64_t notrace tick_to_time(uint64_t tick)
8dfafdde 130{
fccacd3b 131 ulong div = get_tbclk();
8dfafdde
RH
132
133 tick *= CONFIG_SYS_HZ;
134 do_div(tick, div);
135 return tick;
136}
137
de351d6b
DR
138int __weak timer_init(void)
139{
140 return 0;
141}
142
fccacd3b 143/* Returns time in milliseconds */
8dfafdde
RH
144ulong __weak get_timer(ulong base)
145{
146 return tick_to_time(get_ticks()) - base;
147}
148
80e7e7c2
MV
149static uint64_t notrace tick_to_time_us(uint64_t tick)
150{
151 ulong div = get_tbclk() / 1000;
152
153 tick *= CONFIG_SYS_HZ;
154 do_div(tick, div);
155 return tick;
156}
157
158uint64_t __weak get_timer_us(uint64_t base)
159{
160 return tick_to_time_us(get_ticks()) - base;
161}
162
e1ddf67c
SG
163unsigned long __weak get_timer_us_long(unsigned long base)
164{
165 return timer_get_us() - base;
166}
167
8dfafdde
RH
168unsigned long __weak notrace timer_get_us(void)
169{
170 return tick_to_time(get_ticks() * 1000);
171}
fccacd3b 172
6a853dbc 173uint64_t usec_to_tick(unsigned long usec)
8dfafdde 174{
19ea4678 175 uint64_t tick = usec;
2cd1b572 176 tick *= get_tbclk();
8dfafdde
RH
177 do_div(tick, 1000000);
178 return tick;
179}
180
181void __weak __udelay(unsigned long usec)
182{
19ea4678 183 uint64_t tmp;
8dfafdde 184
fccacd3b 185 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
8dfafdde 186
fccacd3b 187 while (get_ticks() < tmp+1) /* loop till event */
8dfafdde
RH
188 /*NOP*/;
189}
190
3eb90bad
IL
191/* ------------------------------------------------------------------------- */
192
193void udelay(unsigned long usec)
194{
195 ulong kv;
196
197 do {
29caf930 198 schedule();
6e7df1d1 199 kv = usec > CFG_WD_PERIOD ? CFG_WD_PERIOD : usec;
07e11146 200 __udelay(kv);
3eb90bad
IL
201 usec -= kv;
202 } while(usec);
203}
This page took 0.361137 seconds and 4 git commands to generate.