1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 #include <clock_legacy.h>
18 #include <asm/global_data.h>
20 #include <linux/delay.h>
22 #ifndef CONFIG_WD_PERIOD
23 # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
26 DECLARE_GLOBAL_DATA_PTR;
28 #ifdef CONFIG_SYS_TIMER_RATE
29 /* Returns tick rate in ticks per second */
30 ulong notrace get_tbclk(void)
32 return CONFIG_SYS_TIMER_RATE;
36 #ifdef CONFIG_SYS_TIMER_COUNTER
37 unsigned long notrace timer_read_counter(void)
39 #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
40 return ~readl(CONFIG_SYS_TIMER_COUNTER);
42 return readl(CONFIG_SYS_TIMER_COUNTER);
46 ulong timer_get_boot_us(void)
48 ulong count = timer_read_counter();
50 #ifdef CONFIG_SYS_TIMER_RATE
51 const ulong timer_rate = CONFIG_SYS_TIMER_RATE;
53 if (timer_rate == 1000000)
55 else if (timer_rate > 1000000)
56 return lldiv(count, timer_rate / 1000000);
58 return (unsigned long long)count * 1000000 / timer_rate;
60 /* Assume the counter is in microseconds */
66 extern unsigned long __weak timer_read_counter(void);
69 #if CONFIG_IS_ENABLED(TIMER)
70 ulong notrace get_tbclk(void)
73 #ifdef CONFIG_TIMER_EARLY
74 return timer_early_get_rate();
78 ret = dm_timer_init();
84 return timer_get_rate(gd->timer);
87 uint64_t notrace get_ticks(void)
93 #ifdef CONFIG_TIMER_EARLY
94 return timer_early_get_count();
98 ret = dm_timer_init();
100 panic("Could not initialize timer (err %d)\n", ret);
104 ret = timer_get_count(gd->timer, &count);
106 if (spl_phase() > PHASE_TPL)
107 panic("Could not read count from timer (err %d)\n",
110 panic("no timer (err %d)\n", ret);
116 #else /* !CONFIG_TIMER */
118 uint64_t __weak notrace get_ticks(void)
120 unsigned long now = timer_read_counter();
122 /* increment tbu if tbl has rolled over */
123 if (now < gd->timebase_l)
125 gd->timebase_l = now;
126 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
129 #endif /* CONFIG_TIMER */
131 /* Returns time in milliseconds */
132 static uint64_t notrace tick_to_time(uint64_t tick)
134 ulong div = get_tbclk();
136 tick *= CONFIG_SYS_HZ;
141 int __weak timer_init(void)
146 /* Returns time in milliseconds */
147 ulong __weak get_timer(ulong base)
149 return tick_to_time(get_ticks()) - base;
152 static uint64_t notrace tick_to_time_us(uint64_t tick)
154 ulong div = get_tbclk() / 1000;
156 tick *= CONFIG_SYS_HZ;
161 uint64_t __weak get_timer_us(uint64_t base)
163 return tick_to_time_us(get_ticks()) - base;
166 unsigned long __weak get_timer_us_long(unsigned long base)
168 return timer_get_us() - base;
171 unsigned long __weak notrace timer_get_us(void)
173 return tick_to_time(get_ticks() * 1000);
176 uint64_t usec_to_tick(unsigned long usec)
178 uint64_t tick = usec;
180 do_div(tick, 1000000);
184 void __weak __udelay(unsigned long usec)
188 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
190 while (get_ticks() < tmp+1) /* loop till event */
194 /* ------------------------------------------------------------------------- */
196 void udelay(unsigned long usec)
202 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;