1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
17 #include <linux/delay.h>
19 #ifndef CONFIG_WD_PERIOD
20 # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
23 DECLARE_GLOBAL_DATA_PTR;
25 #ifdef CONFIG_SYS_TIMER_RATE
26 /* Returns tick rate in ticks per second */
27 ulong notrace get_tbclk(void)
29 return CONFIG_SYS_TIMER_RATE;
33 #ifdef CONFIG_SYS_TIMER_COUNTER
34 unsigned long notrace timer_read_counter(void)
36 #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
37 return ~readl(CONFIG_SYS_TIMER_COUNTER);
39 return readl(CONFIG_SYS_TIMER_COUNTER);
43 ulong timer_get_boot_us(void)
45 ulong count = timer_read_counter();
47 #if CONFIG_SYS_TIMER_RATE == 1000000
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;
54 /* Assume the counter is in microseconds */
60 extern unsigned long __weak timer_read_counter(void);
63 #if CONFIG_IS_ENABLED(TIMER)
64 ulong notrace get_tbclk(void)
67 #ifdef CONFIG_TIMER_EARLY
68 return timer_early_get_rate();
72 ret = dm_timer_init();
78 return timer_get_rate(gd->timer);
81 uint64_t notrace get_ticks(void)
87 #ifdef CONFIG_TIMER_EARLY
88 return timer_early_get_count();
92 ret = dm_timer_init();
98 ret = timer_get_count(gd->timer, &count);
105 #else /* !CONFIG_TIMER */
107 uint64_t __weak notrace get_ticks(void)
109 unsigned long now = timer_read_counter();
111 /* increment tbu if tbl has rolled over */
112 if (now < gd->timebase_l)
114 gd->timebase_l = now;
115 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
118 #endif /* CONFIG_TIMER */
120 /* Returns time in milliseconds */
121 static uint64_t notrace tick_to_time(uint64_t tick)
123 ulong div = get_tbclk();
125 tick *= CONFIG_SYS_HZ;
130 int __weak timer_init(void)
135 /* Returns time in milliseconds */
136 ulong __weak get_timer(ulong base)
138 return tick_to_time(get_ticks()) - base;
141 static uint64_t notrace tick_to_time_us(uint64_t tick)
143 ulong div = get_tbclk() / 1000;
145 tick *= CONFIG_SYS_HZ;
150 uint64_t __weak get_timer_us(uint64_t base)
152 return tick_to_time_us(get_ticks()) - base;
155 unsigned long __weak get_timer_us_long(unsigned long base)
157 return timer_get_us() - base;
160 unsigned long __weak notrace timer_get_us(void)
162 return tick_to_time(get_ticks() * 1000);
165 uint64_t usec_to_tick(unsigned long usec)
167 uint64_t tick = usec;
169 do_div(tick, 1000000);
173 void __weak __udelay(unsigned long usec)
177 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
179 while (get_ticks() < tmp+1) /* loop till event */
183 /* ------------------------------------------------------------------------- */
185 void udelay(unsigned long usec)
191 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;