3 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
4 Copyright (c) 2011, ARM Limited. All rights reserved.
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
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.
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>
25 #define SP804_TIMER_METRONOME_BASE ((UINTN)PcdGet32 (PcdSP804TimerMetronomeBase))
26 #define SP804_TIMER_PERFORMANCE_BASE ((UINTN)PcdGet32 (PcdSP804TimerPerformanceBase))
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.
36 // Check if the Metronome Timer is already initialized
37 if (MmioRead32(SP804_TIMER_METRONOME_BASE
+ SP804_TIMER_CONTROL_REG
) & SP804_TIMER_CTRL_ENABLE
) {
38 return RETURN_SUCCESS
;
40 // Configure the Metronome Timer for free running operation, 32 bits, no prescaler, and interrupt disabled
41 MmioWrite32 (SP804_TIMER_METRONOME_BASE
+ SP804_TIMER_CONTROL_REG
, SP804_TIMER_CTRL_32BIT
| SP804_PRESCALE_DIV_1
);
43 // Start the Metronome Timer ticking
44 MmioOr32 (SP804_TIMER_METRONOME_BASE
+ SP804_TIMER_CONTROL_REG
, SP804_TIMER_CTRL_ENABLE
);
47 // Check if the Performance Timer is already initialized
48 if (MmioRead32(SP804_TIMER_PERFORMANCE_BASE
+ SP804_TIMER_CONTROL_REG
) & SP804_TIMER_CTRL_ENABLE
) {
49 return RETURN_SUCCESS
;
51 // Configure the Performance timer for free running operation, 32 bits, no prescaler, interrupt disabled
52 MmioWrite32 (SP804_TIMER_PERFORMANCE_BASE
+ SP804_TIMER_CONTROL_REG
, SP804_TIMER_CTRL_32BIT
| SP804_PRESCALE_DIV_1
);
54 // Start the Performance Timer ticking
55 MmioOr32 (SP804_TIMER_PERFORMANCE_BASE
+ SP804_TIMER_CONTROL_REG
, SP804_TIMER_CTRL_ENABLE
);
58 return RETURN_SUCCESS
;
62 Stalls the CPU for at least the given number of microseconds.
64 Stalls the CPU for the number of microseconds specified by MicroSeconds.
65 The hardware timer is 32 bits.
66 The maximum possible delay is (0xFFFFFFFF / TimerFrequencyMHz), i.e. ([32bits] / FreqInMHz)
68 +----------------+------------+----------+----------+
69 | TimerFrequency | MaxDelay | MaxDelay | MaxDelay |
70 | (MHz) | (us) | (s) | (min) |
71 +----------------+------------+----------+----------+
72 | 1 | 0xFFFFFFFF | 4294 | 71.5 |
73 | 5 | 0x33333333 | 859 | 14.3 |
74 | 10 | 0x19999999 | 429 | 7.2 |
75 | 50 | 0x051EB851 | 86 | 1.4 |
76 +----------------+------------+----------+----------+
77 If it becomes necessary to support higher delays, then consider using the
80 During this delay, the cpu is not yielded to any other process, with one exception:
81 events that are triggered off a timer and which execute at a higher TPL than
82 this function. These events may call MicroSecondDelay (or NanoSecondDelay) to
83 fulfil their own needs.
84 Therefore, this function must be re-entrant, as it may be interrupted and re-started.
86 @param MicroSeconds The minimum number of microseconds to delay.
88 @return The value of MicroSeconds inputted.
97 UINT64 DelayTicks64
; // Convert from microseconds to timer ticks, more bits to detect over-range conditions.
98 UINTN DelayTicks
; // Convert from microseconds to timer ticks, native size for general calculations.
99 UINTN StartTicks
; // Timer value snapshot at the start of the delay
100 UINTN TargetTicks
; // Timer value to signal the end of the delay
101 UINTN CurrentTicks
; // Current value of the 64-bit timer value at any given moment
103 // If we snapshot the timer at the start of the delay function then we minimise unaccounted overheads.
104 StartTicks
= MmioRead32 (SP804_TIMER_METRONOME_BASE
+ SP804_TIMER_CURRENT_REG
);
106 // We are operating at the limit of 32bits. For the range checking work in 64 bits to avoid overflows.
107 DelayTicks64
= MultU64x32((UINT64
)MicroSeconds
, PcdGet32(PcdSP804TimerFrequencyInMHz
));
109 // We are limited to 32 bits.
110 // If the specified delay is exactly equal to the max range of the timer,
111 // then the start will be equal to the stop plus one timer overflow (wrap-around).
112 // To avoid having to check for that, reduce the maximum acceptable range by 1 tick,
113 // i.e. reject delays equal or greater than the max range of the timer.
114 if (DelayTicks64
>= (UINT64
)SP804_MAX_TICKS
) {
115 DEBUG((EFI_D_ERROR
,"MicroSecondDelay: ERROR: MicroSeconds=%d exceed SP804 count range. Max MicroSeconds=%d\n",
117 ((UINTN
)SP804_MAX_TICKS
/PcdGet32(PcdSP804TimerFrequencyInMHz
))));
119 ASSERT(DelayTicks64
< (UINT64
)SP804_MAX_TICKS
);
121 // From now on do calculations only in native bit size.
122 DelayTicks
= (UINTN
)DelayTicks64
;
124 // Calculate the target value of the timer.
126 //Note: SP804 timer is counting down
127 if (StartTicks
>= DelayTicks
) {
128 // In this case we do not expect a wrap-around of the timer to occur.
129 // CurrentTicks must be less than StartTicks and higher than TargetTicks.
130 // If this is not the case, then the delay has been reached and may even have been exceeded if this
131 // function was suspended by a higher priority interrupt.
133 TargetTicks
= StartTicks
- DelayTicks
;
136 CurrentTicks
= MmioRead32 (SP804_TIMER_METRONOME_BASE
+ SP804_TIMER_CURRENT_REG
);
137 } while ((CurrentTicks
> TargetTicks
) && (CurrentTicks
<= StartTicks
));
140 // In this case TargetTicks is larger than StartTicks.
141 // This means we expect a wrap-around of the timer to occur and we must wait for it.
142 // Before the wrap-around, CurrentTicks must be less than StartTicks and less than TargetTicks.
143 // After the wrap-around, CurrentTicks must be larger than StartTicks and larger than TargetTicks.
144 // If this is not the case, then the delay has been reached and may even have been exceeded if this
145 // function was suspended by a higher priority interrupt.
147 // The order of operations is essential to avoid arithmetic overflow problems
148 TargetTicks
= ((UINTN
)SP804_MAX_TICKS
- DelayTicks
) + StartTicks
;
150 // First wait for the wrap-around to occur
152 CurrentTicks
= MmioRead32 (SP804_TIMER_METRONOME_BASE
+ SP804_TIMER_CURRENT_REG
);
153 } while (CurrentTicks
<= StartTicks
);
155 // Then wait for the target
157 CurrentTicks
= MmioRead32 (SP804_TIMER_METRONOME_BASE
+ SP804_TIMER_CURRENT_REG
);
158 } while (CurrentTicks
> TargetTicks
);
165 Stalls the CPU for at least the given number of nanoseconds.
167 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
169 When the timer frequency is 1MHz, each tick corresponds to 1 microsecond.
170 Therefore, the nanosecond delay will be rounded up to the nearest 1 microsecond.
172 @param NanoSeconds The minimum number of nanoseconds to delay.
174 @return The value of NanoSeconds inputted.
185 // Round up to 1us Tick Number
186 MicroSeconds
= NanoSeconds
/ 1000;
187 MicroSeconds
+= ((NanoSeconds
% 1000) == 0) ? 0 : 1;
189 MicroSecondDelay (MicroSeconds
);
195 Retrieves the current value of a 64-bit free running performance counter.
197 The counter can either count up by 1 or count down by 1. If the physical
198 performance counter counts by a larger increment, then the counter values
199 must be translated. The properties of the counter can be retrieved from
200 GetPerformanceCounterProperties().
202 @return The current value of the free running performance counter.
207 GetPerformanceCounter (
211 // Free running 64-bit/32-bit counter is needed here.
212 // Don't think we need this to boot, just to do performance profile
214 Value
= MmioRead32 (SP804_TIMER_PERFORMANCE_BASE
+ SP804_TIMER_CURRENT_REG
);
220 Retrieves the 64-bit frequency in Hz and the range of performance counter
223 If StartValue is not NULL, then the value that the performance counter starts
224 with immediately after is it rolls over is returned in StartValue. If
225 EndValue is not NULL, then the value that the performance counter end with
226 immediately before it rolls over is returned in EndValue. The 64-bit
227 frequency of the performance counter in Hz is always returned. If StartValue
228 is less than EndValue, then the performance counter counts up. If StartValue
229 is greater than EndValue, then the performance counter counts down. For
230 example, a 64-bit free running counter that counts up would have a StartValue
231 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
232 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
234 @param StartValue The value the performance counter starts with when it
236 @param EndValue The value that the performance counter ends with before
239 @return The frequency in Hz.
244 GetPerformanceCounterProperties (
245 OUT UINT64
*StartValue
, OPTIONAL
246 OUT UINT64
*EndValue OPTIONAL
249 if (StartValue
!= NULL
) {
250 // Timer starts with the reload value
251 *StartValue
= 0xFFFFFFFF;
254 if (EndValue
!= NULL
) {
255 // Timer counts down to 0x0
256 *EndValue
= (UINT64
)0ULL;
259 return PcdGet64 (PcdEmbeddedPerformanceCounterFrequencyInHz
);