common: Drop bootstage.h from common header
[oweals/u-boot.git] / arch / arm / cpu / armv8 / generic_timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2013
4  * David Feng <fenghua@phytium.com.cn>
5  */
6
7 #include <common.h>
8 #include <bootstage.h>
9 #include <command.h>
10 #include <time.h>
11 #include <asm/system.h>
12
13 DECLARE_GLOBAL_DATA_PTR;
14
15 /*
16  * Generic timer implementation of get_tbclk()
17  */
18 unsigned long get_tbclk(void)
19 {
20         unsigned long cntfrq;
21         asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq));
22         return cntfrq;
23 }
24
25 #ifdef CONFIG_SYS_FSL_ERRATUM_A008585
26 /*
27  * FSL erratum A-008585 says that the ARM generic timer counter "has the
28  * potential to contain an erroneous value for a small number of core
29  * clock cycles every time the timer value changes".
30  * This sometimes leads to a consecutive counter read returning a lower
31  * value than the previous one, thus reporting the time to go backwards.
32  * The workaround is to read the counter twice and only return when the value
33  * was the same in both reads.
34  * Assumes that the CPU runs in much higher frequency than the timer.
35  */
36 unsigned long timer_read_counter(void)
37 {
38         unsigned long cntpct;
39         unsigned long temp;
40
41         isb();
42         asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
43         asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
44         while (temp != cntpct) {
45                 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
46                 asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
47         }
48
49         return cntpct;
50 }
51 #elif CONFIG_SUNXI_A64_TIMER_ERRATUM
52 /*
53  * This erratum sometimes flips the lower 11 bits of the counter value
54  * to all 0's or all 1's, leading to jumps forwards or backwards.
55  * Backwards jumps might be interpreted all roll-overs and be treated as
56  * huge jumps forward.
57  * The workaround is to check whether the lower 11 bits of the counter are
58  * all 0 or all 1, then discard this value and read again.
59  * This occasionally discards valid values, but will catch all erroneous
60  * reads and fixes the problem reliably. Also this mostly requires only a
61  * single read, so does not have any significant overhead.
62  * The algorithm was conceived by Samuel Holland.
63  */
64 unsigned long timer_read_counter(void)
65 {
66         unsigned long cntpct;
67
68         isb();
69         do {
70                 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
71         } while (((cntpct + 1) & GENMASK(10, 0)) <= 1);
72
73         return cntpct;
74 }
75 #else
76 /*
77  * timer_read_counter() using the Arm Generic Timer (aka arch timer).
78  */
79 unsigned long timer_read_counter(void)
80 {
81         unsigned long cntpct;
82
83         isb();
84         asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
85
86         return cntpct;
87 }
88 #endif
89
90 uint64_t get_ticks(void)
91 {
92         unsigned long ticks = timer_read_counter();
93
94         gd->arch.tbl = ticks;
95
96         return ticks;
97 }
98
99 unsigned long usec2ticks(unsigned long usec)
100 {
101         ulong ticks;
102         if (usec < 1000)
103                 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
104         else
105                 ticks = ((usec / 10) * (get_tbclk() / 100000));
106
107         return ticks;
108 }
109
110 ulong timer_get_boot_us(void)
111 {
112         u64 val = get_ticks() * 1000000;
113
114         return val / get_tbclk();
115 }