1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
8 #include <clock_legacy.h>
18 #include <asm/global_data.h>
20 #include <linux/delay.h>
23 # define CFG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
26 DECLARE_GLOBAL_DATA_PTR;
28 #ifdef CFG_SYS_TIMER_RATE
29 /* Returns tick rate in ticks per second */
30 ulong notrace get_tbclk(void)
32 return CFG_SYS_TIMER_RATE;
36 #ifdef CFG_SYS_TIMER_COUNTER
37 unsigned long notrace timer_read_counter(void)
39 #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
40 return ~readl(CFG_SYS_TIMER_COUNTER);
42 return readl(CFG_SYS_TIMER_COUNTER);
46 ulong timer_get_boot_us(void)
48 ulong count = timer_read_counter();
50 #ifdef CFG_SYS_TIMER_RATE
51 const ulong timer_rate = CFG_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 timer_read_counter(void);
69 #if CONFIG_IS_ENABLED(TIMER)
70 ulong notrace get_tbclk(void)
75 if (IS_ENABLED(CONFIG_TIMER_EARLY))
76 return timer_early_get_rate();
78 ret = dm_timer_init();
83 return timer_get_rate(gd->timer);
86 uint64_t notrace get_ticks(void)
94 if (IS_ENABLED(CONFIG_TIMER_EARLY))
95 return timer_early_get_count();
97 ret = dm_timer_init();
99 panic("Could not initialize timer (err %d)\n", ret);
102 ret = timer_get_count(gd->timer, &count);
104 if (spl_phase() > PHASE_TPL)
105 panic("Could not read count from timer (err %d)\n",
108 panic("no timer (err %d)\n", ret);
114 #else /* !CONFIG_TIMER */
116 uint64_t __weak notrace get_ticks(void)
118 unsigned long now = timer_read_counter();
120 /* increment tbu if tbl has rolled over */
121 if (now < gd->timebase_l)
123 gd->timebase_l = now;
124 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
127 #endif /* CONFIG_TIMER */
129 /* Returns time in milliseconds */
130 static uint64_t notrace tick_to_time(uint64_t tick)
132 ulong div = get_tbclk();
134 tick *= CONFIG_SYS_HZ;
139 int __weak timer_init(void)
144 /* Returns time in milliseconds */
145 ulong __weak get_timer(ulong base)
147 return tick_to_time(get_ticks()) - base;
150 static uint64_t notrace tick_to_time_us(uint64_t tick)
152 ulong div = get_tbclk() / 1000;
154 tick *= CONFIG_SYS_HZ;
159 uint64_t __weak get_timer_us(uint64_t base)
161 return tick_to_time_us(get_ticks()) - base;
164 unsigned long __weak get_timer_us_long(unsigned long base)
166 return timer_get_us() - base;
169 unsigned long __weak notrace timer_get_us(void)
171 return tick_to_time(get_ticks() * 1000);
174 uint64_t usec_to_tick(unsigned long usec)
176 uint64_t tick = usec;
178 do_div(tick, 1000000);
182 void __weak __udelay(unsigned long usec)
186 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
188 while (get_ticks() < tmp+1) /* loop till event */
192 /* ------------------------------------------------------------------------- */
194 void udelay(unsigned long usec)
200 kv = usec > CFG_WD_PERIOD ? CFG_WD_PERIOD : usec;