1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
17 #include <asm/global_data.h>
19 #include <linux/delay.h>
21 #ifndef CONFIG_WD_PERIOD
22 # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
25 DECLARE_GLOBAL_DATA_PTR;
27 #ifdef CONFIG_SYS_TIMER_RATE
28 /* Returns tick rate in ticks per second */
29 ulong notrace get_tbclk(void)
31 return CONFIG_SYS_TIMER_RATE;
35 #ifdef CONFIG_SYS_TIMER_COUNTER
36 unsigned long notrace timer_read_counter(void)
38 #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
39 return ~readl(CONFIG_SYS_TIMER_COUNTER);
41 return readl(CONFIG_SYS_TIMER_COUNTER);
45 ulong timer_get_boot_us(void)
47 ulong count = timer_read_counter();
49 #if CONFIG_SYS_TIMER_RATE == 1000000
51 #elif CONFIG_SYS_TIMER_RATE > 1000000
52 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
53 #elif defined(CONFIG_SYS_TIMER_RATE)
54 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
56 /* Assume the counter is in microseconds */
62 extern unsigned long __weak timer_read_counter(void);
65 #if CONFIG_IS_ENABLED(TIMER)
66 ulong notrace get_tbclk(void)
69 #ifdef CONFIG_TIMER_EARLY
70 return timer_early_get_rate();
74 ret = dm_timer_init();
80 return timer_get_rate(gd->timer);
83 uint64_t notrace get_ticks(void)
89 #ifdef CONFIG_TIMER_EARLY
90 return timer_early_get_count();
94 ret = dm_timer_init();
96 panic("Could not initialize timer (err %d)\n", ret);
100 ret = timer_get_count(gd->timer, &count);
102 if (spl_phase() > PHASE_TPL)
103 panic("Could not read count from timer (err %d)\n",
106 panic("no timer (err %d)\n", ret);
112 #else /* !CONFIG_TIMER */
114 uint64_t __weak notrace get_ticks(void)
116 unsigned long now = timer_read_counter();
118 /* increment tbu if tbl has rolled over */
119 if (now < gd->timebase_l)
121 gd->timebase_l = now;
122 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
125 #endif /* CONFIG_TIMER */
127 /* Returns time in milliseconds */
128 static uint64_t notrace tick_to_time(uint64_t tick)
130 ulong div = get_tbclk();
132 tick *= CONFIG_SYS_HZ;
137 int __weak timer_init(void)
142 /* Returns time in milliseconds */
143 ulong __weak get_timer(ulong base)
145 return tick_to_time(get_ticks()) - base;
148 static uint64_t notrace tick_to_time_us(uint64_t tick)
150 ulong div = get_tbclk() / 1000;
152 tick *= CONFIG_SYS_HZ;
157 uint64_t __weak get_timer_us(uint64_t base)
159 return tick_to_time_us(get_ticks()) - base;
162 unsigned long __weak get_timer_us_long(unsigned long base)
164 return timer_get_us() - base;
167 unsigned long __weak notrace timer_get_us(void)
169 return tick_to_time(get_ticks() * 1000);
172 uint64_t usec_to_tick(unsigned long usec)
174 uint64_t tick = usec;
176 do_div(tick, 1000000);
180 void __weak __udelay(unsigned long usec)
184 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
186 while (get_ticks() < tmp+1) /* loop till event */
190 /* ------------------------------------------------------------------------- */
192 void udelay(unsigned long usec)
198 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;