]> Git Repo - u-boot.git/blame - lib/time.c
power: pmic: rk8xx: Support sysreset shutdown method
[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
7#include <common.h>
2f8a6db5 8#include <clock_legacy.h>
52f24238 9#include <bootstage.h>
c8a7ba9e
TC
10#include <dm.h>
11#include <errno.h>
691d719d 12#include <init.h>
6d1a8ebe 13#include <spl.h>
1045315d 14#include <time.h>
c8a7ba9e 15#include <timer.h>
3eb90bad 16#include <watchdog.h>
8dfafdde 17#include <div64.h>
401d1c4f 18#include <asm/global_data.h>
8dfafdde 19#include <asm/io.h>
c05ed00a 20#include <linux/delay.h>
3eb90bad
IL
21
22#ifndef CONFIG_WD_PERIOD
fccacd3b 23# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
3eb90bad
IL
24#endif
25
8dfafdde
RH
26DECLARE_GLOBAL_DATA_PTR;
27
28#ifdef CONFIG_SYS_TIMER_RATE
fccacd3b 29/* Returns tick rate in ticks per second */
8dfafdde
RH
30ulong notrace get_tbclk(void)
31{
32 return CONFIG_SYS_TIMER_RATE;
33}
34#endif
35
36#ifdef CONFIG_SYS_TIMER_COUNTER
37unsigned long notrace timer_read_counter(void)
38{
39#ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
40 return ~readl(CONFIG_SYS_TIMER_COUNTER);
41#else
42 return readl(CONFIG_SYS_TIMER_COUNTER);
43#endif
44}
9fb34b01
SG
45
46ulong timer_get_boot_us(void)
47{
48 ulong count = timer_read_counter();
49
50#if CONFIG_SYS_TIMER_RATE == 1000000
51 return count;
52#elif CONFIG_SYS_TIMER_RATE > 1000000
53 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
54#elif defined(CONFIG_SYS_TIMER_RATE)
55 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
56#else
57 /* Assume the counter is in microseconds */
58 return count;
59#endif
60}
61
8dfafdde 62#else
65ba7add 63extern unsigned long __weak timer_read_counter(void);
8dfafdde
RH
64#endif
65
f00c2628 66#if CONFIG_IS_ENABLED(TIMER)
c8a7ba9e
TC
67ulong notrace get_tbclk(void)
68{
c95fec31
SG
69 if (!gd->timer) {
70#ifdef CONFIG_TIMER_EARLY
71 return timer_early_get_rate();
72#else
73 int ret;
c8a7ba9e 74
c95fec31
SG
75 ret = dm_timer_init();
76 if (ret)
77 return ret;
78#endif
79 }
c8a7ba9e
TC
80
81 return timer_get_rate(gd->timer);
82}
83
9ca07ebb 84uint64_t notrace get_ticks(void)
c8a7ba9e 85{
9ca07ebb 86 u64 count;
c8a7ba9e
TC
87 int ret;
88
c95fec31
SG
89 if (!gd->timer) {
90#ifdef CONFIG_TIMER_EARLY
91 return timer_early_get_count();
92#else
93 int ret;
94
95 ret = dm_timer_init();
96 if (ret)
4b2be78a 97 panic("Could not initialize timer (err %d)\n", ret);
c95fec31
SG
98#endif
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 {
198 WATCHDOG_RESET();
199 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
07e11146 200 __udelay(kv);
3eb90bad
IL
201 usec -= kv;
202 } while(usec);
203}
This page took 0.307478 seconds and 4 git commands to generate.