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