2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2013-12-14 03:47:35 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2013
|
|
|
|
* David Feng <fenghua@phytium.com.cn>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-05-10 17:40:00 +00:00
|
|
|
#include <bootstage.h>
|
2013-12-14 03:47:35 +00:00
|
|
|
#include <command.h>
|
2019-11-14 19:57:26 +00:00
|
|
|
#include <time.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2013-12-14 03:47:35 +00:00
|
|
|
#include <asm/system.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2013-12-14 03:47:35 +00:00
|
|
|
|
2016-11-03 00:56:25 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2013-12-14 03:47:35 +00:00
|
|
|
/*
|
|
|
|
* Generic timer implementation of get_tbclk()
|
|
|
|
*/
|
|
|
|
unsigned long get_tbclk(void)
|
|
|
|
{
|
|
|
|
unsigned long cntfrq;
|
|
|
|
asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq));
|
|
|
|
return cntfrq;
|
|
|
|
}
|
|
|
|
|
2018-06-27 00:42:52 +00:00
|
|
|
#ifdef CONFIG_SYS_FSL_ERRATUM_A008585
|
2013-12-14 03:47:35 +00:00
|
|
|
/*
|
2018-06-27 00:42:52 +00:00
|
|
|
* FSL erratum A-008585 says that the ARM generic timer counter "has the
|
|
|
|
* potential to contain an erroneous value for a small number of core
|
|
|
|
* clock cycles every time the timer value changes".
|
|
|
|
* This sometimes leads to a consecutive counter read returning a lower
|
|
|
|
* value than the previous one, thus reporting the time to go backwards.
|
|
|
|
* The workaround is to read the counter twice and only return when the value
|
|
|
|
* was the same in both reads.
|
|
|
|
* Assumes that the CPU runs in much higher frequency than the timer.
|
2013-12-14 03:47:35 +00:00
|
|
|
*/
|
|
|
|
unsigned long timer_read_counter(void)
|
|
|
|
{
|
|
|
|
unsigned long cntpct;
|
2015-03-21 02:28:05 +00:00
|
|
|
unsigned long temp;
|
2018-06-27 00:42:52 +00:00
|
|
|
|
2013-12-14 03:47:35 +00:00
|
|
|
isb();
|
|
|
|
asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
|
2015-03-21 02:28:05 +00:00
|
|
|
asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
|
|
|
|
while (temp != cntpct) {
|
|
|
|
asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
|
|
|
|
asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
|
|
|
|
}
|
2018-06-27 00:42:52 +00:00
|
|
|
|
2013-12-14 03:47:35 +00:00
|
|
|
return cntpct;
|
|
|
|
}
|
2018-06-27 00:42:53 +00:00
|
|
|
#elif CONFIG_SUNXI_A64_TIMER_ERRATUM
|
|
|
|
/*
|
|
|
|
* This erratum sometimes flips the lower 11 bits of the counter value
|
|
|
|
* to all 0's or all 1's, leading to jumps forwards or backwards.
|
|
|
|
* Backwards jumps might be interpreted all roll-overs and be treated as
|
|
|
|
* huge jumps forward.
|
|
|
|
* The workaround is to check whether the lower 11 bits of the counter are
|
|
|
|
* all 0 or all 1, then discard this value and read again.
|
|
|
|
* This occasionally discards valid values, but will catch all erroneous
|
|
|
|
* reads and fixes the problem reliably. Also this mostly requires only a
|
|
|
|
* single read, so does not have any significant overhead.
|
|
|
|
* The algorithm was conceived by Samuel Holland.
|
|
|
|
*/
|
|
|
|
unsigned long timer_read_counter(void)
|
|
|
|
{
|
|
|
|
unsigned long cntpct;
|
|
|
|
|
|
|
|
isb();
|
|
|
|
do {
|
|
|
|
asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
|
|
|
|
} while (((cntpct + 1) & GENMASK(10, 0)) <= 1);
|
|
|
|
|
|
|
|
return cntpct;
|
|
|
|
}
|
2018-06-27 00:42:52 +00:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* timer_read_counter() using the Arm Generic Timer (aka arch timer).
|
|
|
|
*/
|
|
|
|
unsigned long timer_read_counter(void)
|
|
|
|
{
|
|
|
|
unsigned long cntpct;
|
|
|
|
|
|
|
|
isb();
|
|
|
|
asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
|
|
|
|
|
|
|
|
return cntpct;
|
|
|
|
}
|
|
|
|
#endif
|
2015-12-08 08:24:26 +00:00
|
|
|
|
2017-04-05 23:53:17 +00:00
|
|
|
uint64_t get_ticks(void)
|
2016-11-03 00:56:25 +00:00
|
|
|
{
|
|
|
|
unsigned long ticks = timer_read_counter();
|
|
|
|
|
|
|
|
gd->arch.tbl = ticks;
|
|
|
|
|
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
2015-12-08 08:24:26 +00:00
|
|
|
unsigned long usec2ticks(unsigned long usec)
|
|
|
|
{
|
|
|
|
ulong ticks;
|
|
|
|
if (usec < 1000)
|
|
|
|
ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
|
|
|
|
else
|
|
|
|
ticks = ((usec / 10) * (get_tbclk() / 100000));
|
|
|
|
|
|
|
|
return ticks;
|
|
|
|
}
|
2018-05-15 14:47:02 +00:00
|
|
|
|
|
|
|
ulong timer_get_boot_us(void)
|
|
|
|
{
|
|
|
|
u64 val = get_ticks() * 1000000;
|
|
|
|
|
|
|
|
return val / get_tbclk();
|
|
|
|
}
|