crc16-ccitt: Rename file with CRC-16-CCITT implementation to crc16-ccitt.c
[platform/kernel/u-boot.git] / lib / time.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2009
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #include <common.h>
8 #include <clock_legacy.h>
9 #include <bootstage.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <init.h>
13 #include <spl.h>
14 #include <time.h>
15 #include <timer.h>
16 #include <watchdog.h>
17 #include <div64.h>
18 #include <asm/global_data.h>
19 #include <asm/io.h>
20 #include <linux/delay.h>
21
22 #ifndef CONFIG_WD_PERIOD
23 # define CONFIG_WD_PERIOD       (10 * 1000 * 1000)      /* 10 seconds default */
24 #endif
25
26 DECLARE_GLOBAL_DATA_PTR;
27
28 #ifdef CONFIG_SYS_TIMER_RATE
29 /* Returns tick rate in ticks per second */
30 ulong notrace get_tbclk(void)
31 {
32         return CONFIG_SYS_TIMER_RATE;
33 }
34 #endif
35
36 #ifdef CONFIG_SYS_TIMER_COUNTER
37 unsigned 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 }
45
46 ulong 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
62 #else
63 extern unsigned long __weak timer_read_counter(void);
64 #endif
65
66 #if CONFIG_IS_ENABLED(TIMER)
67 ulong notrace get_tbclk(void)
68 {
69         if (!gd->timer) {
70 #ifdef CONFIG_TIMER_EARLY
71                 return timer_early_get_rate();
72 #else
73                 int ret;
74
75                 ret = dm_timer_init();
76                 if (ret)
77                         return ret;
78 #endif
79         }
80
81         return timer_get_rate(gd->timer);
82 }
83
84 uint64_t notrace get_ticks(void)
85 {
86         u64 count;
87         int ret;
88
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)
97                         panic("Could not initialize timer (err %d)\n", ret);
98 #endif
99         }
100
101         ret = timer_get_count(gd->timer, &count);
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         }
109
110         return count;
111 }
112
113 #else /* !CONFIG_TIMER */
114
115 uint64_t __weak notrace get_ticks(void)
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;
123         return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
124 }
125
126 #endif /* CONFIG_TIMER */
127
128 /* Returns time in milliseconds */
129 static uint64_t notrace tick_to_time(uint64_t tick)
130 {
131         ulong div = get_tbclk();
132
133         tick *= CONFIG_SYS_HZ;
134         do_div(tick, div);
135         return tick;
136 }
137
138 int __weak timer_init(void)
139 {
140         return 0;
141 }
142
143 /* Returns time in milliseconds */
144 ulong __weak get_timer(ulong base)
145 {
146         return tick_to_time(get_ticks()) - base;
147 }
148
149 static 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
158 uint64_t __weak get_timer_us(uint64_t base)
159 {
160         return tick_to_time_us(get_ticks()) - base;
161 }
162
163 unsigned long __weak get_timer_us_long(unsigned long base)
164 {
165         return timer_get_us() - base;
166 }
167
168 unsigned long __weak notrace timer_get_us(void)
169 {
170         return tick_to_time(get_ticks() * 1000);
171 }
172
173 uint64_t usec_to_tick(unsigned long usec)
174 {
175         uint64_t tick = usec;
176         tick *= get_tbclk();
177         do_div(tick, 1000000);
178         return tick;
179 }
180
181 void __weak __udelay(unsigned long usec)
182 {
183         uint64_t tmp;
184
185         tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
186
187         while (get_ticks() < tmp+1)     /* loop till event */
188                  /*NOP*/;
189 }
190
191 /* ------------------------------------------------------------------------- */
192
193 void udelay(unsigned long usec)
194 {
195         ulong kv;
196
197         do {
198                 WATCHDOG_RESET();
199                 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
200                 __udelay(kv);
201                 usec -= kv;
202         } while(usec);
203 }