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 #if CONFIG_SYS_TIMER_RATE == 1000000
52 #elif CONFIG_SYS_TIMER_RATE > 1000000
53 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
54 #elif defined(CONFIG_SYS_TIMER_RATE)
55 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
57 /* Assume the counter is in microseconds */
63 extern unsigned long __weak timer_read_counter(void);
66 #if CONFIG_IS_ENABLED(TIMER)
67 ulong notrace get_tbclk(void)
70 #ifdef CONFIG_TIMER_EARLY
71 return timer_early_get_rate();
75 ret = dm_timer_init();
81 return timer_get_rate(gd->timer);
84 uint64_t notrace get_ticks(void)
90 #ifdef CONFIG_TIMER_EARLY
91 return timer_early_get_count();
95 ret = dm_timer_init();
97 panic("Could not initialize timer (err %d)\n", ret);
101 ret = timer_get_count(gd->timer, &count);
103 if (spl_phase() > PHASE_TPL)
104 panic("Could not read count from timer (err %d)\n",
107 panic("no timer (err %d)\n", ret);
113 #else /* !CONFIG_TIMER */
115 uint64_t __weak notrace get_ticks(void)
117 unsigned long now = timer_read_counter();
119 /* increment tbu if tbl has rolled over */
120 if (now < gd->timebase_l)
122 gd->timebase_l = now;
123 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
126 #endif /* CONFIG_TIMER */
128 /* Returns time in milliseconds */
129 static uint64_t notrace tick_to_time(uint64_t tick)
131 ulong div = get_tbclk();
133 tick *= CONFIG_SYS_HZ;
138 int __weak timer_init(void)
143 /* Returns time in milliseconds */
144 ulong __weak get_timer(ulong base)
146 return tick_to_time(get_ticks()) - base;
149 static uint64_t notrace tick_to_time_us(uint64_t tick)
151 ulong div = get_tbclk() / 1000;
153 tick *= CONFIG_SYS_HZ;
158 uint64_t __weak get_timer_us(uint64_t base)
160 return tick_to_time_us(get_ticks()) - base;
163 unsigned long __weak get_timer_us_long(unsigned long base)
165 return timer_get_us() - base;
168 unsigned long __weak notrace timer_get_us(void)
170 return tick_to_time(get_ticks() * 1000);
173 uint64_t usec_to_tick(unsigned long usec)
175 uint64_t tick = usec;
177 do_div(tick, 1000000);
181 void __weak __udelay(unsigned long usec)
185 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
187 while (get_ticks() < tmp+1) /* loop till event */
191 /* ------------------------------------------------------------------------- */
193 void udelay(unsigned long usec)
199 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;