1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 - 2013 Tensilica Inc.
7 #include <clock_legacy.h>
9 #include <asm/global_data.h>
10 #include <linux/delay.h>
11 #include <linux/stringify.h>
13 DECLARE_GLOBAL_DATA_PTR;
16 static ulong get_ccount(void)
19 asm volatile ("rsr %0,"__stringify(CCOUNT) : "=a" (ccount));
23 static ulong fake_ccount;
24 #define get_ccount() fake_ccount
27 static void delay_cycles(unsigned cycles)
30 unsigned expiry = get_ccount() + cycles;
31 while ((signed)(expiry - get_ccount()) > 0)
34 #warning "Without Xtensa timer option, timing will not be accurate."
37 * Approximate the cycle count by a loop iteration count.
38 * This is highly dependent on config and optimization.
42 for (i = cycles >> 4U; i > 0; --i)
44 fake_ccount += cycles;
49 * Delay (busy-wait) for a number of microseconds.
52 void __udelay(unsigned long usec)
55 ulong mhz = get_board_sys_clk() / 1000000;
57 /* Scale to support full 32-bit usec range */
59 lo = usec & ((1<<22)-1);
61 for (i = 0; i < hi; ++i)
62 delay_cycles(mhz << 22);
63 delay_cycles(mhz * lo);
68 * Return the elapsed time (ticks) since 'base'.
71 ulong get_timer(ulong base)
73 /* Don't tie up a timer; use cycle counter if available (or fake it) */
76 register ulong ccount;
77 __asm__ volatile ("rsr %0, CCOUNT" : "=a"(ccount));
78 return ccount / (get_board_sys_clk() / CONFIG_SYS_HZ) - base;
81 * Add at least the overhead of this call (in cycles).
82 * Avoids hanging in case caller doesn't use udelay().
83 * Note that functions that don't call udelay() (such as
84 * the "sleep" command) will not get a significant delay
85 * because there is no time reference.
89 return fake_ccount / (get_board_sys_clk() / CONFIG_SYS_HZ) - base;
95 * This function is derived from ARM/PowerPC code (read timebase as long long).
96 * On Xtensa it just returns the timer value.
98 unsigned long long get_ticks(void)
104 * This function is derived from ARM/PowerPC code (timebase clock frequency).
105 * On Xtensa it returns the number of timer ticks per second.
107 ulong get_tbclk(void)
109 return CONFIG_SYS_HZ;
112 #if XCHAL_HAVE_CCOUNT
113 unsigned long timer_get_us(void)
115 unsigned long ccount;
117 __asm__ volatile ("rsr %0, CCOUNT" : "=a"(ccount));
118 return ccount / (get_board_sys_clk() / 1000000);