1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 #ifndef CONFIG_WD_PERIOD
16 # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
19 DECLARE_GLOBAL_DATA_PTR;
21 #ifdef CONFIG_SYS_TIMER_RATE
22 /* Returns tick rate in ticks per second */
23 ulong notrace get_tbclk(void)
25 return CONFIG_SYS_TIMER_RATE;
29 #ifdef CONFIG_SYS_TIMER_COUNTER
30 unsigned long notrace timer_read_counter(void)
32 #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
33 return ~readl(CONFIG_SYS_TIMER_COUNTER);
35 return readl(CONFIG_SYS_TIMER_COUNTER);
39 ulong timer_get_boot_us(void)
41 ulong count = timer_read_counter();
43 #if CONFIG_SYS_TIMER_RATE == 1000000
45 #elif CONFIG_SYS_TIMER_RATE > 1000000
46 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
47 #elif defined(CONFIG_SYS_TIMER_RATE)
48 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
50 /* Assume the counter is in microseconds */
56 extern unsigned long __weak timer_read_counter(void);
59 #if CONFIG_IS_ENABLED(TIMER)
60 ulong notrace get_tbclk(void)
63 #ifdef CONFIG_TIMER_EARLY
64 return timer_early_get_rate();
68 ret = dm_timer_init();
74 return timer_get_rate(gd->timer);
77 uint64_t notrace get_ticks(void)
83 #ifdef CONFIG_TIMER_EARLY
84 return timer_early_get_count();
88 ret = dm_timer_init();
94 ret = timer_get_count(gd->timer, &count);
101 #else /* !CONFIG_TIMER */
103 uint64_t __weak notrace get_ticks(void)
105 unsigned long now = timer_read_counter();
107 /* increment tbu if tbl has rolled over */
108 if (now < gd->timebase_l)
110 gd->timebase_l = now;
111 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
114 #endif /* CONFIG_TIMER */
116 /* Returns time in milliseconds */
117 static uint64_t notrace tick_to_time(uint64_t tick)
119 ulong div = get_tbclk();
121 tick *= CONFIG_SYS_HZ;
126 int __weak timer_init(void)
131 /* Returns time in milliseconds */
132 ulong __weak get_timer(ulong base)
134 return tick_to_time(get_ticks()) - base;
137 unsigned long __weak notrace timer_get_us(void)
139 return tick_to_time(get_ticks() * 1000);
142 static uint64_t usec_to_tick(unsigned long usec)
144 uint64_t tick = usec;
146 do_div(tick, 1000000);
150 void __weak __udelay(unsigned long usec)
154 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
156 while (get_ticks() < tmp+1) /* loop till event */
160 /* ------------------------------------------------------------------------- */
162 void udelay(unsigned long usec)
168 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;