1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
18 #include <linux/delay.h>
20 #ifndef CONFIG_WD_PERIOD
21 # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
24 DECLARE_GLOBAL_DATA_PTR;
26 #ifdef CONFIG_SYS_TIMER_RATE
27 /* Returns tick rate in ticks per second */
28 ulong notrace get_tbclk(void)
30 return CONFIG_SYS_TIMER_RATE;
34 #ifdef CONFIG_SYS_TIMER_COUNTER
35 unsigned long notrace timer_read_counter(void)
37 #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
38 return ~readl(CONFIG_SYS_TIMER_COUNTER);
40 return readl(CONFIG_SYS_TIMER_COUNTER);
44 ulong timer_get_boot_us(void)
46 ulong count = timer_read_counter();
48 #if CONFIG_SYS_TIMER_RATE == 1000000
50 #elif CONFIG_SYS_TIMER_RATE > 1000000
51 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
52 #elif defined(CONFIG_SYS_TIMER_RATE)
53 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
55 /* Assume the counter is in microseconds */
61 extern unsigned long __weak timer_read_counter(void);
64 #if CONFIG_IS_ENABLED(TIMER)
65 ulong notrace get_tbclk(void)
68 #ifdef CONFIG_TIMER_EARLY
69 return timer_early_get_rate();
73 ret = dm_timer_init();
79 return timer_get_rate(gd->timer);
82 uint64_t notrace get_ticks(void)
88 #ifdef CONFIG_TIMER_EARLY
89 return timer_early_get_count();
93 ret = dm_timer_init();
95 panic("Could not initialize timer (err %d)\n", ret);
99 ret = timer_get_count(gd->timer, &count);
101 if (spl_phase() > PHASE_TPL)
102 panic("Could not read count from timer (err %d)\n",
105 panic("no timer (err %d)\n", ret);
111 #else /* !CONFIG_TIMER */
113 uint64_t __weak notrace get_ticks(void)
115 unsigned long now = timer_read_counter();
117 /* increment tbu if tbl has rolled over */
118 if (now < gd->timebase_l)
120 gd->timebase_l = now;
121 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
124 #endif /* CONFIG_TIMER */
126 /* Returns time in milliseconds */
127 static uint64_t notrace tick_to_time(uint64_t tick)
129 ulong div = get_tbclk();
131 tick *= CONFIG_SYS_HZ;
136 int __weak timer_init(void)
141 /* Returns time in milliseconds */
142 ulong __weak get_timer(ulong base)
144 return tick_to_time(get_ticks()) - base;
147 static uint64_t notrace tick_to_time_us(uint64_t tick)
149 ulong div = get_tbclk() / 1000;
151 tick *= CONFIG_SYS_HZ;
156 uint64_t __weak get_timer_us(uint64_t base)
158 return tick_to_time_us(get_ticks()) - base;
161 unsigned long __weak get_timer_us_long(unsigned long base)
163 return timer_get_us() - base;
166 unsigned long __weak notrace timer_get_us(void)
168 return tick_to_time(get_ticks() * 1000);
171 uint64_t usec_to_tick(unsigned long usec)
173 uint64_t tick = usec;
175 do_div(tick, 1000000);
179 void __weak __udelay(unsigned long usec)
183 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
185 while (get_ticks() < tmp+1) /* loop till event */
189 /* ------------------------------------------------------------------------- */
191 void udelay(unsigned long usec)
197 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;