1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2009
7 #include <clock_legacy.h>
17 #include <asm/global_data.h>
19 #include <linux/delay.h>
22 # define CFG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
25 DECLARE_GLOBAL_DATA_PTR;
27 #ifdef CFG_SYS_TIMER_RATE
28 /* Returns tick rate in ticks per second */
29 ulong notrace get_tbclk(void)
31 return CFG_SYS_TIMER_RATE;
35 #ifdef CFG_SYS_TIMER_COUNTER
36 unsigned long notrace timer_read_counter(void)
38 #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN
39 return ~readl(CFG_SYS_TIMER_COUNTER);
41 return readl(CFG_SYS_TIMER_COUNTER);
45 ulong timer_get_boot_us(void)
47 ulong count = timer_read_counter();
49 #ifdef CFG_SYS_TIMER_RATE
50 const ulong timer_rate = CFG_SYS_TIMER_RATE;
52 if (timer_rate == 1000000)
54 else if (timer_rate > 1000000)
55 return lldiv(count, timer_rate / 1000000);
57 return (unsigned long long)count * 1000000 / timer_rate;
59 /* Assume the counter is in microseconds */
65 extern unsigned long timer_read_counter(void);
68 #if CONFIG_IS_ENABLED(TIMER)
69 ulong notrace get_tbclk(void)
74 if (IS_ENABLED(CONFIG_TIMER_EARLY))
75 return timer_early_get_rate();
77 ret = dm_timer_init();
82 return timer_get_rate(gd->timer);
85 uint64_t notrace get_ticks(void)
93 if (IS_ENABLED(CONFIG_TIMER_EARLY))
94 return timer_early_get_count();
96 ret = dm_timer_init();
98 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 > CFG_WD_PERIOD ? CFG_WD_PERIOD : usec;