colibri_imx6: fix video stdout in default environment
[oweals/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 <bootstage.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <init.h>
12 #include <time.h>
13 #include <timer.h>
14 #include <watchdog.h>
15 #include <div64.h>
16 #include <asm/io.h>
17 #include <linux/delay.h>
18
19 #ifndef CONFIG_WD_PERIOD
20 # define CONFIG_WD_PERIOD       (10 * 1000 * 1000)      /* 10 seconds default */
21 #endif
22
23 DECLARE_GLOBAL_DATA_PTR;
24
25 #ifdef CONFIG_SYS_TIMER_RATE
26 /* Returns tick rate in ticks per second */
27 ulong notrace get_tbclk(void)
28 {
29         return CONFIG_SYS_TIMER_RATE;
30 }
31 #endif
32
33 #ifdef CONFIG_SYS_TIMER_COUNTER
34 unsigned long notrace timer_read_counter(void)
35 {
36 #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
37         return ~readl(CONFIG_SYS_TIMER_COUNTER);
38 #else
39         return readl(CONFIG_SYS_TIMER_COUNTER);
40 #endif
41 }
42
43 ulong timer_get_boot_us(void)
44 {
45         ulong count = timer_read_counter();
46
47 #if CONFIG_SYS_TIMER_RATE == 1000000
48         return count;
49 #elif CONFIG_SYS_TIMER_RATE > 1000000
50         return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
51 #elif defined(CONFIG_SYS_TIMER_RATE)
52         return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
53 #else
54         /* Assume the counter is in microseconds */
55         return count;
56 #endif
57 }
58
59 #else
60 extern unsigned long __weak timer_read_counter(void);
61 #endif
62
63 #if CONFIG_IS_ENABLED(TIMER)
64 ulong notrace get_tbclk(void)
65 {
66         if (!gd->timer) {
67 #ifdef CONFIG_TIMER_EARLY
68                 return timer_early_get_rate();
69 #else
70                 int ret;
71
72                 ret = dm_timer_init();
73                 if (ret)
74                         return ret;
75 #endif
76         }
77
78         return timer_get_rate(gd->timer);
79 }
80
81 uint64_t notrace get_ticks(void)
82 {
83         u64 count;
84         int ret;
85
86         if (!gd->timer) {
87 #ifdef CONFIG_TIMER_EARLY
88                 return timer_early_get_count();
89 #else
90                 int ret;
91
92                 ret = dm_timer_init();
93                 if (ret)
94                         return ret;
95 #endif
96         }
97
98         ret = timer_get_count(gd->timer, &count);
99         if (ret)
100                 return ret;
101
102         return count;
103 }
104
105 #else /* !CONFIG_TIMER */
106
107 uint64_t __weak notrace get_ticks(void)
108 {
109         unsigned long now = timer_read_counter();
110
111         /* increment tbu if tbl has rolled over */
112         if (now < gd->timebase_l)
113                 gd->timebase_h++;
114         gd->timebase_l = now;
115         return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
116 }
117
118 #endif /* CONFIG_TIMER */
119
120 /* Returns time in milliseconds */
121 static uint64_t notrace tick_to_time(uint64_t tick)
122 {
123         ulong div = get_tbclk();
124
125         tick *= CONFIG_SYS_HZ;
126         do_div(tick, div);
127         return tick;
128 }
129
130 int __weak timer_init(void)
131 {
132         return 0;
133 }
134
135 /* Returns time in milliseconds */
136 ulong __weak get_timer(ulong base)
137 {
138         return tick_to_time(get_ticks()) - base;
139 }
140
141 static uint64_t notrace tick_to_time_us(uint64_t tick)
142 {
143         ulong div = get_tbclk() / 1000;
144
145         tick *= CONFIG_SYS_HZ;
146         do_div(tick, div);
147         return tick;
148 }
149
150 uint64_t __weak get_timer_us(uint64_t base)
151 {
152         return tick_to_time_us(get_ticks()) - base;
153 }
154
155 unsigned long __weak notrace timer_get_us(void)
156 {
157         return tick_to_time(get_ticks() * 1000);
158 }
159
160 uint64_t usec_to_tick(unsigned long usec)
161 {
162         uint64_t tick = usec;
163         tick *= get_tbclk();
164         do_div(tick, 1000000);
165         return tick;
166 }
167
168 void __weak __udelay(unsigned long usec)
169 {
170         uint64_t tmp;
171
172         tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
173
174         while (get_ticks() < tmp+1)     /* loop till event */
175                  /*NOP*/;
176 }
177
178 /* ------------------------------------------------------------------------- */
179
180 void udelay(unsigned long usec)
181 {
182         ulong kv;
183
184         do {
185                 WATCHDOG_RESET();
186                 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
187                 __udelay(kv);
188                 usec -= kv;
189         } while(usec);
190 }