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