1 /** @file
2
3 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
4 Copyright (c) 2011 - 2014, ARM Limited. All rights reserved.
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <Base.h>
17
18 #include <Library/BaseLib.h>
19 #include <Library/TimerLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/IoLib.h>
23 #include <Drivers/SP804Timer.h>
24
25 #define SP804_TIMER_METRONOME_BASE ((UINTN)PcdGet32 (PcdSP804TimerMetronomeBase))
26 #define SP804_TIMER_PERFORMANCE_BASE ((UINTN)PcdGet32 (PcdSP804TimerPerformanceBase))
27
28 // Setup SP810's Timer2 for managing delay functions. And Timer3 for Performance counter
29 // Note: ArmVE's Timer0 and Timer1 are used by TimerDxe.
30 RETURN_STATUS
31 EFIAPI
TimerConstructor(VOID)32 TimerConstructor (
33 VOID
34 )
35 {
36 // Check if the Metronome Timer is already initialized
37 if ((MmioRead32 (SP804_TIMER_METRONOME_BASE + SP804_TIMER_CONTROL_REG) & SP804_TIMER_CTRL_ENABLE) == 0) {
38 // Configure the Metronome Timer for free running operation, 32 bits, no prescaler, and interrupt disabled
39 MmioWrite32 (SP804_TIMER_METRONOME_BASE + SP804_TIMER_CONTROL_REG, SP804_TIMER_CTRL_32BIT | SP804_PRESCALE_DIV_1);
40
41 // Start the Metronome Timer ticking
42 MmioOr32 (SP804_TIMER_METRONOME_BASE + SP804_TIMER_CONTROL_REG, SP804_TIMER_CTRL_ENABLE);
43 }
44
45 // Check if the Performance Timer is already initialized
46 if ((MmioRead32 (SP804_TIMER_PERFORMANCE_BASE + SP804_TIMER_CONTROL_REG) & SP804_TIMER_CTRL_ENABLE) == 0) {
47 // Configure the Performance timer for free running operation, 32 bits, no prescaler, interrupt disabled
48 MmioWrite32 (SP804_TIMER_PERFORMANCE_BASE + SP804_TIMER_CONTROL_REG, SP804_TIMER_CTRL_32BIT | SP804_PRESCALE_DIV_1);
49
50 // Start the Performance Timer ticking
51 MmioOr32 (SP804_TIMER_PERFORMANCE_BASE + SP804_TIMER_CONTROL_REG, SP804_TIMER_CTRL_ENABLE);
52 }
53
54 return RETURN_SUCCESS;
55 }
56
57 /**
58 Stalls the CPU for at least the given number of microseconds.
59
60 Stalls the CPU for the number of microseconds specified by MicroSeconds.
61 The hardware timer is 32 bits.
62 The maximum possible delay is (0xFFFFFFFF / TimerFrequencyMHz), i.e. ([32bits] / FreqInMHz)
63 For example:
64 +----------------+------------+----------+----------+
65 | TimerFrequency | MaxDelay | MaxDelay | MaxDelay |
66 | (MHz) | (us) | (s) | (min) |
67 +----------------+------------+----------+----------+
68 | 1 | 0xFFFFFFFF | 4294 | 71.5 |
69 | 5 | 0x33333333 | 859 | 14.3 |
70 | 10 | 0x19999999 | 429 | 7.2 |
71 | 50 | 0x051EB851 | 86 | 1.4 |
72 +----------------+------------+----------+----------+
73 If it becomes necessary to support higher delays, then consider using the
74 real time clock.
75
76 During this delay, the cpu is not yielded to any other process, with one exception:
77 events that are triggered off a timer and which execute at a higher TPL than
78 this function. These events may call MicroSecondDelay (or NanoSecondDelay) to
79 fulfil their own needs.
80 Therefore, this function must be re-entrant, as it may be interrupted and re-started.
81
82 @param MicroSeconds The minimum number of microseconds to delay.
83
84 @return The value of MicroSeconds inputted.
85
86 **/
87 UINTN
88 EFIAPI
MicroSecondDelay(IN UINTN MicroSeconds)89 MicroSecondDelay (
90 IN UINTN MicroSeconds
91 )
92 {
93 UINT64 DelayTicks64; // Convert from microseconds to timer ticks, more bits to detect over-range conditions.
94 UINTN DelayTicks; // Convert from microseconds to timer ticks, native size for general calculations.
95 UINTN StartTicks; // Timer value snapshot at the start of the delay
96 UINTN TargetTicks; // Timer value to signal the end of the delay
97 UINTN CurrentTicks; // Current value of the 64-bit timer value at any given moment
98
99 // If we snapshot the timer at the start of the delay function then we minimise unaccounted overheads.
100 StartTicks = MmioRead32 (SP804_TIMER_METRONOME_BASE + SP804_TIMER_CURRENT_REG);
101
102 // We are operating at the limit of 32bits. For the range checking work in 64 bits to avoid overflows.
103 DelayTicks64 = MultU64x32((UINT64)MicroSeconds, PcdGet32(PcdSP804TimerFrequencyInMHz));
104
105 // We are limited to 32 bits.
106 // If the specified delay is exactly equal to the max range of the timer,
107 // then the start will be equal to the stop plus one timer overflow (wrap-around).
108 // To avoid having to check for that, reduce the maximum acceptable range by 1 tick,
109 // i.e. reject delays equal or greater than the max range of the timer.
110 if (DelayTicks64 >= (UINT64)SP804_MAX_TICKS) {
111 DEBUG((EFI_D_ERROR,"MicroSecondDelay: ERROR: MicroSeconds=%d exceed SP804 count range. Max MicroSeconds=%d\n",
112 MicroSeconds,
113 ((UINTN)SP804_MAX_TICKS/PcdGet32(PcdSP804TimerFrequencyInMHz))));
114 }
115 ASSERT(DelayTicks64 < (UINT64)SP804_MAX_TICKS);
116
117 // From now on do calculations only in native bit size.
118 DelayTicks = (UINTN)DelayTicks64;
119
120 // Calculate the target value of the timer.
121
122 //Note: SP804 timer is counting down
123 if (StartTicks >= DelayTicks) {
124 // In this case we do not expect a wrap-around of the timer to occur.
125 // CurrentTicks must be less than StartTicks and higher than TargetTicks.
126 // If this is not the case, then the delay has been reached and may even have been exceeded if this
127 // function was suspended by a higher priority interrupt.
128
129 TargetTicks = StartTicks - DelayTicks;
130
131 do {
132 CurrentTicks = MmioRead32 (SP804_TIMER_METRONOME_BASE + SP804_TIMER_CURRENT_REG);
133 } while ((CurrentTicks > TargetTicks) && (CurrentTicks <= StartTicks));
134
135 } else {
136 // In this case TargetTicks is larger than StartTicks.
137 // This means we expect a wrap-around of the timer to occur and we must wait for it.
138 // Before the wrap-around, CurrentTicks must be less than StartTicks and less than TargetTicks.
139 // After the wrap-around, CurrentTicks must be larger than StartTicks and larger than TargetTicks.
140 // If this is not the case, then the delay has been reached and may even have been exceeded if this
141 // function was suspended by a higher priority interrupt.
142
143 // The order of operations is essential to avoid arithmetic overflow problems
144 TargetTicks = ((UINTN)SP804_MAX_TICKS - DelayTicks) + StartTicks;
145
146 // First wait for the wrap-around to occur
147 do {
148 CurrentTicks = MmioRead32 (SP804_TIMER_METRONOME_BASE + SP804_TIMER_CURRENT_REG);
149 } while (CurrentTicks <= StartTicks);
150
151 // Then wait for the target
152 do {
153 CurrentTicks = MmioRead32 (SP804_TIMER_METRONOME_BASE + SP804_TIMER_CURRENT_REG);
154 } while (CurrentTicks > TargetTicks);
155 }
156
157 return MicroSeconds;
158 }
159
160 /**
161 Stalls the CPU for at least the given number of nanoseconds.
162
163 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
164
165 When the timer frequency is 1MHz, each tick corresponds to 1 microsecond.
166 Therefore, the nanosecond delay will be rounded up to the nearest 1 microsecond.
167
168 @param NanoSeconds The minimum number of nanoseconds to delay.
169
170 @return The value of NanoSeconds inputted.
171
172 **/
173 UINTN
174 EFIAPI
NanoSecondDelay(IN UINTN NanoSeconds)175 NanoSecondDelay (
176 IN UINTN NanoSeconds
177 )
178 {
179 UINTN MicroSeconds;
180
181 // Round up to 1us Tick Number
182 MicroSeconds = NanoSeconds / 1000;
183 MicroSeconds += ((NanoSeconds % 1000) == 0) ? 0 : 1;
184
185 MicroSecondDelay (MicroSeconds);
186
187 return NanoSeconds;
188 }
189
190 /**
191 Retrieves the current value of a 64-bit free running performance counter.
192
193 The counter can either count up by 1 or count down by 1. If the physical
194 performance counter counts by a larger increment, then the counter values
195 must be translated. The properties of the counter can be retrieved from
196 GetPerformanceCounterProperties().
197
198 @return The current value of the free running performance counter.
199
200 **/
201 UINT64
202 EFIAPI
GetPerformanceCounter(VOID)203 GetPerformanceCounter (
204 VOID
205 )
206 {
207 // Free running 64-bit/32-bit counter is needed here.
208 // Don't think we need this to boot, just to do performance profile
209 UINT64 Value;
210 Value = MmioRead32 (SP804_TIMER_PERFORMANCE_BASE + SP804_TIMER_CURRENT_REG);
211 return Value;
212 }
213
214
215 /**
216 Retrieves the 64-bit frequency in Hz and the range of performance counter
217 values.
218
219 If StartValue is not NULL, then the value that the performance counter starts
220 with immediately after is it rolls over is returned in StartValue. If
221 EndValue is not NULL, then the value that the performance counter end with
222 immediately before it rolls over is returned in EndValue. The 64-bit
223 frequency of the performance counter in Hz is always returned. If StartValue
224 is less than EndValue, then the performance counter counts up. If StartValue
225 is greater than EndValue, then the performance counter counts down. For
226 example, a 64-bit free running counter that counts up would have a StartValue
227 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
228 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
229
230 @param StartValue The value the performance counter starts with when it
231 rolls over.
232 @param EndValue The value that the performance counter ends with before
233 it rolls over.
234
235 @return The frequency in Hz.
236
237 **/
238 UINT64
239 EFIAPI
GetPerformanceCounterProperties(OUT UINT64 * StartValue,OPTIONAL OUT UINT64 * EndValue OPTIONAL)240 GetPerformanceCounterProperties (
241 OUT UINT64 *StartValue, OPTIONAL
242 OUT UINT64 *EndValue OPTIONAL
243 )
244 {
245 if (StartValue != NULL) {
246 // Timer starts with the reload value
247 *StartValue = 0xFFFFFFFF;
248 }
249
250 if (EndValue != NULL) {
251 // Timer counts down to 0x0
252 *EndValue = (UINT64)0ULL;
253 }
254
255 return PcdGet64 (PcdEmbeddedPerformanceCounterFrequencyInHz);
256 }
257