blob: 8f83372cbca40c534cee1513bd5b5b043f4704a4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
David Feng85fd5f12013-12-14 11:47:35 +08002/*
3 * (C) Copyright 2013
4 * David Feng <fenghua@phytium.com.cn>
David Feng85fd5f12013-12-14 11:47:35 +08005 */
6
7#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -06008#include <bootstage.h>
David Feng85fd5f12013-12-14 11:47:35 +08009#include <command.h>
Simon Glass45c78902019-11-14 12:57:26 -070010#include <time.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
David Feng85fd5f12013-12-14 11:47:35 +080012#include <asm/system.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060013#include <linux/bitops.h>
David Feng85fd5f12013-12-14 11:47:35 +080014
Andre Przywarade223fd2016-11-03 00:56:25 +000015DECLARE_GLOBAL_DATA_PTR;
16
David Feng85fd5f12013-12-14 11:47:35 +080017/*
18 * Generic timer implementation of get_tbclk()
19 */
Simon Glassb89ed9d2022-12-21 16:08:16 -070020unsigned long notrace get_tbclk(void)
David Feng85fd5f12013-12-14 11:47:35 +080021{
22 unsigned long cntfrq;
23 asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq));
24 return cntfrq;
25}
26
Andre Przywara60b78652018-06-27 01:42:52 +010027#ifdef CONFIG_SYS_FSL_ERRATUM_A008585
David Feng85fd5f12013-12-14 11:47:35 +080028/*
Andre Przywara60b78652018-06-27 01:42:52 +010029 * FSL erratum A-008585 says that the ARM generic timer counter "has the
30 * potential to contain an erroneous value for a small number of core
31 * clock cycles every time the timer value changes".
32 * This sometimes leads to a consecutive counter read returning a lower
33 * value than the previous one, thus reporting the time to go backwards.
34 * The workaround is to read the counter twice and only return when the value
35 * was the same in both reads.
36 * Assumes that the CPU runs in much higher frequency than the timer.
David Feng85fd5f12013-12-14 11:47:35 +080037 */
38unsigned long timer_read_counter(void)
39{
40 unsigned long cntpct;
York Suna7686cf2015-03-20 19:28:05 -070041 unsigned long temp;
Andre Przywara60b78652018-06-27 01:42:52 +010042
David Feng85fd5f12013-12-14 11:47:35 +080043 isb();
44 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
York Suna7686cf2015-03-20 19:28:05 -070045 asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
46 while (temp != cntpct) {
47 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
48 asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
49 }
Andre Przywara60b78652018-06-27 01:42:52 +010050
David Feng85fd5f12013-12-14 11:47:35 +080051 return cntpct;
52}
Andre Przywarad1de0bb2018-06-27 01:42:53 +010053#elif CONFIG_SUNXI_A64_TIMER_ERRATUM
54/*
55 * This erratum sometimes flips the lower 11 bits of the counter value
56 * to all 0's or all 1's, leading to jumps forwards or backwards.
57 * Backwards jumps might be interpreted all roll-overs and be treated as
58 * huge jumps forward.
59 * The workaround is to check whether the lower 11 bits of the counter are
60 * all 0 or all 1, then discard this value and read again.
61 * This occasionally discards valid values, but will catch all erroneous
62 * reads and fixes the problem reliably. Also this mostly requires only a
63 * single read, so does not have any significant overhead.
64 * The algorithm was conceived by Samuel Holland.
65 */
66unsigned long timer_read_counter(void)
67{
68 unsigned long cntpct;
69
70 isb();
71 do {
72 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
73 } while (((cntpct + 1) & GENMASK(10, 0)) <= 1);
74
75 return cntpct;
76}
Andre Przywara60b78652018-06-27 01:42:52 +010077#else
78/*
79 * timer_read_counter() using the Arm Generic Timer (aka arch timer).
80 */
Simon Glassb89ed9d2022-12-21 16:08:16 -070081unsigned long notrace timer_read_counter(void)
Andre Przywara60b78652018-06-27 01:42:52 +010082{
83 unsigned long cntpct;
84
85 isb();
86 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
87
88 return cntpct;
89}
90#endif
Aneesh Bansald1074b42015-12-08 13:54:26 +053091
Simon Glassb89ed9d2022-12-21 16:08:16 -070092uint64_t notrace get_ticks(void)
Andre Przywarade223fd2016-11-03 00:56:25 +000093{
94 unsigned long ticks = timer_read_counter();
95
96 gd->arch.tbl = ticks;
97
98 return ticks;
99}
100
Aneesh Bansald1074b42015-12-08 13:54:26 +0530101unsigned long usec2ticks(unsigned long usec)
102{
103 ulong ticks;
104 if (usec < 1000)
105 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
106 else
107 ticks = ((usec / 10) * (get_tbclk() / 100000));
108
109 return ticks;
110}
Michal Simek16d73b92018-05-15 16:47:02 +0200111
112ulong timer_get_boot_us(void)
113{
114 u64 val = get_ticks() * 1000000;
115
116 return val / get_tbclk();
117}