1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2008 - 2013 Tensilica Inc.
8 #include <asm/global_data.h>
9 #include <linux/delay.h>
10 #include <linux/stringify.h>
12 DECLARE_GLOBAL_DATA_PTR;
15 static ulong get_ccount(void)
18 asm volatile ("rsr %0,"__stringify(CCOUNT) : "=a" (ccount));
22 static ulong fake_ccount;
23 #define get_ccount() fake_ccount
26 static void delay_cycles(unsigned cycles)
29 unsigned expiry = get_ccount() + cycles;
30 while ((signed)(expiry - get_ccount()) > 0)
33 #warning "Without Xtensa timer option, timing will not be accurate."
36 * Approximate the cycle count by a loop iteration count.
37 * This is highly dependent on config and optimization.
41 for (i = cycles >> 4U; i > 0; --i)
43 fake_ccount += cycles;
48 * Delay (busy-wait) for a number of microseconds.
51 void __udelay(unsigned long usec)
54 ulong mhz = CONFIG_SYS_CLK_FREQ / 1000000;
56 /* Scale to support full 32-bit usec range */
58 lo = usec & ((1<<22)-1);
60 for (i = 0; i < hi; ++i)
61 delay_cycles(mhz << 22);
62 delay_cycles(mhz * lo);
67 * Return the elapsed time (ticks) since 'base'.
70 ulong get_timer(ulong base)
72 /* Don't tie up a timer; use cycle counter if available (or fake it) */
75 register ulong ccount;
76 __asm__ volatile ("rsr %0, CCOUNT" : "=a"(ccount));
77 return ccount / (CONFIG_SYS_CLK_FREQ / CONFIG_SYS_HZ) - base;
80 * Add at least the overhead of this call (in cycles).
81 * Avoids hanging in case caller doesn't use udelay().
82 * Note that functions that don't call udelay() (such as
83 * the "sleep" command) will not get a significant delay
84 * because there is no time reference.
88 return fake_ccount / (CONFIG_SYS_CLK_FREQ / CONFIG_SYS_HZ) - base;
94 * This function is derived from ARM/PowerPC code (read timebase as long long).
95 * On Xtensa it just returns the timer value.
97 unsigned long long get_ticks(void)
103 * This function is derived from ARM/PowerPC code (timebase clock frequency).
104 * On Xtensa it returns the number of timer ticks per second.
106 ulong get_tbclk(void)
108 return CONFIG_SYS_HZ;
111 #if XCHAL_HAVE_CCOUNT
112 unsigned long timer_get_us(void)
114 unsigned long ccount;
116 __asm__ volatile ("rsr %0, CCOUNT" : "=a"(ccount));
117 return ccount / (CONFIG_SYS_CLK_FREQ / 1000000);