]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
3eb90bad IL |
2 | /* |
3 | * (C) Copyright 2000-2009 | |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
3eb90bad IL |
5 | */ |
6 | ||
7 | #include <common.h> | |
2f8a6db5 | 8 | #include <clock_legacy.h> |
52f24238 | 9 | #include <bootstage.h> |
c8a7ba9e TC |
10 | #include <dm.h> |
11 | #include <errno.h> | |
691d719d | 12 | #include <init.h> |
6d1a8ebe | 13 | #include <spl.h> |
1045315d | 14 | #include <time.h> |
c8a7ba9e | 15 | #include <timer.h> |
3eb90bad | 16 | #include <watchdog.h> |
8dfafdde | 17 | #include <div64.h> |
401d1c4f | 18 | #include <asm/global_data.h> |
8dfafdde | 19 | #include <asm/io.h> |
c05ed00a | 20 | #include <linux/delay.h> |
3eb90bad | 21 | |
6e7df1d1 TR |
22 | #ifndef CFG_WD_PERIOD |
23 | # define CFG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */ | |
3eb90bad IL |
24 | #endif |
25 | ||
8dfafdde RH |
26 | DECLARE_GLOBAL_DATA_PTR; |
27 | ||
65cc0e2a | 28 | #ifdef CFG_SYS_TIMER_RATE |
fccacd3b | 29 | /* Returns tick rate in ticks per second */ |
8dfafdde RH |
30 | ulong notrace get_tbclk(void) |
31 | { | |
65cc0e2a | 32 | return CFG_SYS_TIMER_RATE; |
8dfafdde RH |
33 | } |
34 | #endif | |
35 | ||
65cc0e2a | 36 | #ifdef CFG_SYS_TIMER_COUNTER |
8dfafdde RH |
37 | unsigned long notrace timer_read_counter(void) |
38 | { | |
39 | #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN | |
65cc0e2a | 40 | return ~readl(CFG_SYS_TIMER_COUNTER); |
8dfafdde | 41 | #else |
65cc0e2a | 42 | return readl(CFG_SYS_TIMER_COUNTER); |
8dfafdde RH |
43 | #endif |
44 | } | |
9fb34b01 SG |
45 | |
46 | ulong timer_get_boot_us(void) | |
47 | { | |
48 | ulong count = timer_read_counter(); | |
49 | ||
65cc0e2a TR |
50 | #ifdef CFG_SYS_TIMER_RATE |
51 | const ulong timer_rate = CFG_SYS_TIMER_RATE; | |
616278bd MW |
52 | |
53 | if (timer_rate == 1000000) | |
54 | return count; | |
55 | else if (timer_rate > 1000000) | |
56 | return lldiv(count, timer_rate / 1000000); | |
57 | else | |
58 | return (unsigned long long)count * 1000000 / timer_rate; | |
9fb34b01 SG |
59 | #else |
60 | /* Assume the counter is in microseconds */ | |
61 | return count; | |
62 | #endif | |
63 | } | |
64 | ||
8dfafdde | 65 | #else |
ea3d28ec | 66 | extern unsigned long timer_read_counter(void); |
8dfafdde RH |
67 | #endif |
68 | ||
f00c2628 | 69 | #if CONFIG_IS_ENABLED(TIMER) |
c8a7ba9e TC |
70 | ulong notrace get_tbclk(void) |
71 | { | |
c95fec31 | 72 | if (!gd->timer) { |
c95fec31 | 73 | int ret; |
c8a7ba9e | 74 | |
64d445a3 SG |
75 | if (IS_ENABLED(CONFIG_TIMER_EARLY)) |
76 | return timer_early_get_rate(); | |
77 | ||
c95fec31 SG |
78 | ret = dm_timer_init(); |
79 | if (ret) | |
80 | return ret; | |
c95fec31 | 81 | } |
c8a7ba9e TC |
82 | |
83 | return timer_get_rate(gd->timer); | |
84 | } | |
85 | ||
9ca07ebb | 86 | uint64_t notrace get_ticks(void) |
c8a7ba9e | 87 | { |
9ca07ebb | 88 | u64 count; |
c8a7ba9e TC |
89 | int ret; |
90 | ||
c95fec31 | 91 | if (!gd->timer) { |
c95fec31 SG |
92 | int ret; |
93 | ||
64d445a3 SG |
94 | if (IS_ENABLED(CONFIG_TIMER_EARLY)) |
95 | return timer_early_get_count(); | |
96 | ||
c95fec31 SG |
97 | ret = dm_timer_init(); |
98 | if (ret) | |
4b2be78a | 99 | panic("Could not initialize timer (err %d)\n", ret); |
c95fec31 | 100 | } |
c8a7ba9e TC |
101 | |
102 | ret = timer_get_count(gd->timer, &count); | |
6d1a8ebe SG |
103 | if (ret) { |
104 | if (spl_phase() > PHASE_TPL) | |
105 | panic("Could not read count from timer (err %d)\n", | |
106 | ret); | |
107 | else | |
108 | panic("no timer (err %d)\n", ret); | |
109 | } | |
c8a7ba9e TC |
110 | |
111 | return count; | |
112 | } | |
9ca07ebb BM |
113 | |
114 | #else /* !CONFIG_TIMER */ | |
c8a7ba9e | 115 | |
19ea4678 | 116 | uint64_t __weak notrace get_ticks(void) |
8dfafdde RH |
117 | { |
118 | unsigned long now = timer_read_counter(); | |
119 | ||
120 | /* increment tbu if tbl has rolled over */ | |
121 | if (now < gd->timebase_l) | |
122 | gd->timebase_h++; | |
123 | gd->timebase_l = now; | |
19ea4678 | 124 | return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l; |
8dfafdde RH |
125 | } |
126 | ||
9ca07ebb BM |
127 | #endif /* CONFIG_TIMER */ |
128 | ||
fccacd3b | 129 | /* Returns time in milliseconds */ |
19ea4678 | 130 | static uint64_t notrace tick_to_time(uint64_t tick) |
8dfafdde | 131 | { |
fccacd3b | 132 | ulong div = get_tbclk(); |
8dfafdde RH |
133 | |
134 | tick *= CONFIG_SYS_HZ; | |
135 | do_div(tick, div); | |
136 | return tick; | |
137 | } | |
138 | ||
de351d6b DR |
139 | int __weak timer_init(void) |
140 | { | |
141 | return 0; | |
142 | } | |
143 | ||
fccacd3b | 144 | /* Returns time in milliseconds */ |
8dfafdde RH |
145 | ulong __weak get_timer(ulong base) |
146 | { | |
147 | return tick_to_time(get_ticks()) - base; | |
148 | } | |
149 | ||
80e7e7c2 MV |
150 | static uint64_t notrace tick_to_time_us(uint64_t tick) |
151 | { | |
152 | ulong div = get_tbclk() / 1000; | |
153 | ||
154 | tick *= CONFIG_SYS_HZ; | |
155 | do_div(tick, div); | |
156 | return tick; | |
157 | } | |
158 | ||
159 | uint64_t __weak get_timer_us(uint64_t base) | |
160 | { | |
161 | return tick_to_time_us(get_ticks()) - base; | |
162 | } | |
163 | ||
e1ddf67c SG |
164 | unsigned long __weak get_timer_us_long(unsigned long base) |
165 | { | |
166 | return timer_get_us() - base; | |
167 | } | |
168 | ||
8dfafdde RH |
169 | unsigned long __weak notrace timer_get_us(void) |
170 | { | |
171 | return tick_to_time(get_ticks() * 1000); | |
172 | } | |
fccacd3b | 173 | |
6a853dbc | 174 | uint64_t usec_to_tick(unsigned long usec) |
8dfafdde | 175 | { |
19ea4678 | 176 | uint64_t tick = usec; |
2cd1b572 | 177 | tick *= get_tbclk(); |
8dfafdde RH |
178 | do_div(tick, 1000000); |
179 | return tick; | |
180 | } | |
181 | ||
182 | void __weak __udelay(unsigned long usec) | |
183 | { | |
19ea4678 | 184 | uint64_t tmp; |
8dfafdde | 185 | |
fccacd3b | 186 | tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */ |
8dfafdde | 187 | |
fccacd3b | 188 | while (get_ticks() < tmp+1) /* loop till event */ |
8dfafdde RH |
189 | /*NOP*/; |
190 | } | |
191 | ||
3eb90bad IL |
192 | /* ------------------------------------------------------------------------- */ |
193 | ||
194 | void udelay(unsigned long usec) | |
195 | { | |
196 | ulong kv; | |
197 | ||
198 | do { | |
29caf930 | 199 | schedule(); |
6e7df1d1 | 200 | kv = usec > CFG_WD_PERIOD ? CFG_WD_PERIOD : usec; |
07e11146 | 201 | __udelay(kv); |
3eb90bad IL |
202 | usec -= kv; |
203 | } while(usec); | |
204 | } |