2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-08-10 15:36:44 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2008 - 2013 Tensilica Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2021-12-14 18:36:40 +00:00
|
|
|
#include <clock_legacy.h>
|
2019-11-14 19:57:30 +00:00
|
|
|
#include <time.h>
|
2016-08-10 15:36:44 +00:00
|
|
|
#include <asm/global_data.h>
|
2020-05-10 17:40:11 +00:00
|
|
|
#include <linux/delay.h>
|
2016-08-10 15:36:44 +00:00
|
|
|
#include <linux/stringify.h>
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
#if XCHAL_HAVE_CCOUNT
|
|
|
|
static ulong get_ccount(void)
|
|
|
|
{
|
|
|
|
ulong ccount;
|
|
|
|
asm volatile ("rsr %0,"__stringify(CCOUNT) : "=a" (ccount));
|
|
|
|
return ccount;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static ulong fake_ccount;
|
|
|
|
#define get_ccount() fake_ccount
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void delay_cycles(unsigned cycles)
|
|
|
|
{
|
|
|
|
#if XCHAL_HAVE_CCOUNT
|
|
|
|
unsigned expiry = get_ccount() + cycles;
|
|
|
|
while ((signed)(expiry - get_ccount()) > 0)
|
|
|
|
;
|
|
|
|
#else
|
|
|
|
#warning "Without Xtensa timer option, timing will not be accurate."
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Approximate the cycle count by a loop iteration count.
|
|
|
|
* This is highly dependent on config and optimization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
volatile unsigned i;
|
|
|
|
for (i = cycles >> 4U; i > 0; --i)
|
|
|
|
;
|
|
|
|
fake_ccount += cycles;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delay (busy-wait) for a number of microseconds.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void __udelay(unsigned long usec)
|
|
|
|
{
|
|
|
|
ulong lo, hi, i;
|
2021-12-14 18:36:40 +00:00
|
|
|
ulong mhz = get_board_sys_clk() / 1000000;
|
2016-08-10 15:36:44 +00:00
|
|
|
|
|
|
|
/* Scale to support full 32-bit usec range */
|
|
|
|
|
|
|
|
lo = usec & ((1<<22)-1);
|
|
|
|
hi = usec >> 22UL;
|
|
|
|
for (i = 0; i < hi; ++i)
|
|
|
|
delay_cycles(mhz << 22);
|
|
|
|
delay_cycles(mhz * lo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the elapsed time (ticks) since 'base'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ulong get_timer(ulong base)
|
|
|
|
{
|
|
|
|
/* Don't tie up a timer; use cycle counter if available (or fake it) */
|
|
|
|
|
|
|
|
#if XCHAL_HAVE_CCOUNT
|
|
|
|
register ulong ccount;
|
|
|
|
__asm__ volatile ("rsr %0, CCOUNT" : "=a"(ccount));
|
2021-12-14 18:36:40 +00:00
|
|
|
return ccount / (get_board_sys_clk() / CONFIG_SYS_HZ) - base;
|
2016-08-10 15:36:44 +00:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* Add at least the overhead of this call (in cycles).
|
|
|
|
* Avoids hanging in case caller doesn't use udelay().
|
|
|
|
* Note that functions that don't call udelay() (such as
|
|
|
|
* the "sleep" command) will not get a significant delay
|
|
|
|
* because there is no time reference.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fake_ccount += 20;
|
2021-12-14 18:36:40 +00:00
|
|
|
return fake_ccount / (get_board_sys_clk() / CONFIG_SYS_HZ) - base;
|
2016-08-10 15:36:44 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is derived from ARM/PowerPC code (read timebase as long long).
|
|
|
|
* On Xtensa it just returns the timer value.
|
|
|
|
*/
|
|
|
|
unsigned long long get_ticks(void)
|
|
|
|
{
|
|
|
|
return get_timer(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is derived from ARM/PowerPC code (timebase clock frequency).
|
|
|
|
* On Xtensa it returns the number of timer ticks per second.
|
|
|
|
*/
|
|
|
|
ulong get_tbclk(void)
|
|
|
|
{
|
2016-09-06 13:17:38 +00:00
|
|
|
return CONFIG_SYS_HZ;
|
2016-08-10 15:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if XCHAL_HAVE_CCOUNT
|
|
|
|
unsigned long timer_get_us(void)
|
|
|
|
{
|
|
|
|
unsigned long ccount;
|
|
|
|
|
|
|
|
__asm__ volatile ("rsr %0, CCOUNT" : "=a"(ccount));
|
2021-12-14 18:36:40 +00:00
|
|
|
return ccount / (get_board_sys_clk() / 1000000);
|
2016-08-10 15:36:44 +00:00
|
|
|
}
|
|
|
|
#endif
|