1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
18 #ifndef CONFIG_WD_PERIOD
19 # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
22 DECLARE_GLOBAL_DATA_PTR;
24 #ifdef CONFIG_SYS_TIMER_RATE
25 /* Returns tick rate in ticks per second */
26 ulong notrace get_tbclk(void)
28 return CONFIG_SYS_TIMER_RATE;
32 #ifdef CONFIG_SYS_TIMER_COUNTER
33 unsigned long notrace timer_read_counter(void)
35 #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
36 return ~readl(CONFIG_SYS_TIMER_COUNTER);
38 return readl(CONFIG_SYS_TIMER_COUNTER);
42 ulong timer_get_boot_us(void)
44 ulong count = timer_read_counter();
46 #if CONFIG_SYS_TIMER_RATE == 1000000
48 #elif CONFIG_SYS_TIMER_RATE > 1000000
49 return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000);
50 #elif defined(CONFIG_SYS_TIMER_RATE)
51 return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE;
53 /* Assume the counter is in microseconds */
59 extern unsigned long __weak timer_read_counter(void);
62 #if CONFIG_IS_ENABLED(TIMER)
63 ulong notrace get_tbclk(void)
66 #ifdef CONFIG_TIMER_EARLY
67 return timer_early_get_rate();
71 ret = dm_timer_init();
77 return timer_get_rate(gd->timer);
80 uint64_t notrace get_ticks(void)
86 #ifdef CONFIG_TIMER_EARLY
87 return timer_early_get_count();
91 ret = dm_timer_init();
97 ret = timer_get_count(gd->timer, &count);
104 #else /* !CONFIG_TIMER */
106 uint64_t __weak notrace get_ticks(void)
108 unsigned long now = timer_read_counter();
110 /* increment tbu if tbl has rolled over */
111 if (now < gd->timebase_l)
113 gd->timebase_l = now;
114 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
117 #endif /* CONFIG_TIMER */
119 /* Returns time in milliseconds */
120 static uint64_t notrace tick_to_time(uint64_t tick)
122 ulong div = get_tbclk();
124 tick *= CONFIG_SYS_HZ;
129 int __weak timer_init(void)
134 /* Returns time in milliseconds */
135 ulong __weak get_timer(ulong base)
137 return tick_to_time(get_ticks()) - base;
140 static uint64_t notrace tick_to_time_us(uint64_t tick)
142 ulong div = get_tbclk() / 1000;
144 tick *= CONFIG_SYS_HZ;
149 uint64_t __weak get_timer_us(uint64_t base)
151 return tick_to_time_us(get_ticks()) - base;
154 unsigned long __weak notrace timer_get_us(void)
156 return tick_to_time(get_ticks() * 1000);
159 uint64_t usec_to_tick(unsigned long usec)
161 uint64_t tick = usec;
163 do_div(tick, 1000000);
167 void __weak __udelay(unsigned long usec)
171 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
173 while (get_ticks() < tmp+1) /* loop till event */
177 /* ------------------------------------------------------------------------- */
179 void udelay(unsigned long usec)
185 kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;