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