blob: 46e63294fef016254ceefd1ec1d787015f250fd4 [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>
8#include <command.h>
Simon Glass45c78902019-11-14 12:57:26 -07009#include <time.h>
David Feng85fd5f12013-12-14 11:47:35 +080010#include <asm/system.h>
11
Andre Przywarade223fd2016-11-03 00:56:25 +000012DECLARE_GLOBAL_DATA_PTR;
13
David Feng85fd5f12013-12-14 11:47:35 +080014/*
15 * Generic timer implementation of get_tbclk()
16 */
17unsigned long get_tbclk(void)
18{
19 unsigned long cntfrq;
20 asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq));
21 return cntfrq;
22}
23
Andre Przywara60b78652018-06-27 01:42:52 +010024#ifdef CONFIG_SYS_FSL_ERRATUM_A008585
David Feng85fd5f12013-12-14 11:47:35 +080025/*
Andre Przywara60b78652018-06-27 01:42:52 +010026 * FSL erratum A-008585 says that the ARM generic timer counter "has the
27 * potential to contain an erroneous value for a small number of core
28 * clock cycles every time the timer value changes".
29 * This sometimes leads to a consecutive counter read returning a lower
30 * value than the previous one, thus reporting the time to go backwards.
31 * The workaround is to read the counter twice and only return when the value
32 * was the same in both reads.
33 * Assumes that the CPU runs in much higher frequency than the timer.
David Feng85fd5f12013-12-14 11:47:35 +080034 */
35unsigned long timer_read_counter(void)
36{
37 unsigned long cntpct;
York Suna7686cf2015-03-20 19:28:05 -070038 unsigned long temp;
Andre Przywara60b78652018-06-27 01:42:52 +010039
David Feng85fd5f12013-12-14 11:47:35 +080040 isb();
41 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
York Suna7686cf2015-03-20 19:28:05 -070042 asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
43 while (temp != cntpct) {
44 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
45 asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
46 }
Andre Przywara60b78652018-06-27 01:42:52 +010047
David Feng85fd5f12013-12-14 11:47:35 +080048 return cntpct;
49}
Andre Przywarad1de0bb2018-06-27 01:42:53 +010050#elif CONFIG_SUNXI_A64_TIMER_ERRATUM
51/*
52 * This erratum sometimes flips the lower 11 bits of the counter value
53 * to all 0's or all 1's, leading to jumps forwards or backwards.
54 * Backwards jumps might be interpreted all roll-overs and be treated as
55 * huge jumps forward.
56 * The workaround is to check whether the lower 11 bits of the counter are
57 * all 0 or all 1, then discard this value and read again.
58 * This occasionally discards valid values, but will catch all erroneous
59 * reads and fixes the problem reliably. Also this mostly requires only a
60 * single read, so does not have any significant overhead.
61 * The algorithm was conceived by Samuel Holland.
62 */
63unsigned long timer_read_counter(void)
64{
65 unsigned long cntpct;
66
67 isb();
68 do {
69 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
70 } while (((cntpct + 1) & GENMASK(10, 0)) <= 1);
71
72 return cntpct;
73}
Andre Przywara60b78652018-06-27 01:42:52 +010074#else
75/*
76 * timer_read_counter() using the Arm Generic Timer (aka arch timer).
77 */
78unsigned long timer_read_counter(void)
79{
80 unsigned long cntpct;
81
82 isb();
83 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
84
85 return cntpct;
86}
87#endif
Aneesh Bansald1074b42015-12-08 13:54:26 +053088
Simon Glasse9873932017-04-05 17:53:17 -060089uint64_t get_ticks(void)
Andre Przywarade223fd2016-11-03 00:56:25 +000090{
91 unsigned long ticks = timer_read_counter();
92
93 gd->arch.tbl = ticks;
94
95 return ticks;
96}
97
Aneesh Bansald1074b42015-12-08 13:54:26 +053098unsigned long usec2ticks(unsigned long usec)
99{
100 ulong ticks;
101 if (usec < 1000)
102 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
103 else
104 ticks = ((usec / 10) * (get_tbclk() / 100000));
105
106 return ticks;
107}
Michal Simek16d73b92018-05-15 16:47:02 +0200108
109ulong timer_get_boot_us(void)
110{
111 u64 val = get_ticks() * 1000000;
112
113 return val / get_tbclk();
114}