1
2
3
4
5
6
7#include <common.h>
8#include <bootstage.h>
9#include <command.h>
10#include <time.h>
11#include <asm/global_data.h>
12#include <asm/system.h>
13#include <linux/bitops.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17
18
19
20unsigned long get_tbclk(void)
21{
22 unsigned long cntfrq;
23 asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq));
24 return cntfrq;
25}
26
27#ifdef CONFIG_SYS_FSL_ERRATUM_A008585
28
29
30
31
32
33
34
35
36
37
38unsigned long timer_read_counter(void)
39{
40 unsigned long cntpct;
41 unsigned long temp;
42
43 isb();
44 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
45 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 }
50
51 return cntpct;
52}
53#elif CONFIG_SUNXI_A64_TIMER_ERRATUM
54
55
56
57
58
59
60
61
62
63
64
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}
77#else
78
79
80
81unsigned long timer_read_counter(void)
82{
83 unsigned long cntpct;
84
85 isb();
86 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
87
88 return cntpct;
89}
90#endif
91
92uint64_t get_ticks(void)
93{
94 unsigned long ticks = timer_read_counter();
95
96 gd->arch.tbl = ticks;
97
98 return ticks;
99}
100
101unsigned 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}
111
112ulong timer_get_boot_us(void)
113{
114 u64 val = get_ticks() * 1000000;
115
116 return val / get_tbclk();
117}
118