3 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
4 Copyright (c) 2011 - 2014, 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
) == 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
);
41 // Start the Metronome Timer ticking
42 MmioOr32 (SP804_TIMER_METRONOME_BASE
+ SP804_TIMER_CONTROL_REG
, SP804_TIMER_CTRL_ENABLE
);
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
);
50 // Start the Performance Timer ticking
51 MmioOr32 (SP804_TIMER_PERFORMANCE_BASE
+ SP804_TIMER_CONTROL_REG
, SP804_TIMER_CTRL_ENABLE
);
54 return RETURN_SUCCESS
;
58 Stalls the CPU for at least the given number of microseconds.
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)
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
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.
82 @param MicroSeconds The minimum number of microseconds to delay.
84 @return The value of MicroSeconds inputted.
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
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
);
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
));
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",
113 ((UINTN
)SP804_MAX_TICKS
/PcdGet32(PcdSP804TimerFrequencyInMHz
))));
115 ASSERT(DelayTicks64
< (UINT64
)SP804_MAX_TICKS
);
117 // From now on do calculations only in native bit size.
118 DelayTicks
= (UINTN
)DelayTicks64
;
120 // Calculate the target value of the timer.
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.
129 TargetTicks
= StartTicks
- DelayTicks
;
132 CurrentTicks
= MmioRead32 (SP804_TIMER_METRONOME_BASE
+ SP804_TIMER_CURRENT_REG
);
133 } while ((CurrentTicks
> TargetTicks
) && (CurrentTicks
<= StartTicks
));
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.
143 // The order of operations is essential to avoid arithmetic overflow problems
144 TargetTicks
= ((UINTN
)SP804_MAX_TICKS
- DelayTicks
) + StartTicks
;
146 // First wait for the wrap-around to occur
148 CurrentTicks
= MmioRead32 (SP804_TIMER_METRONOME_BASE
+ SP804_TIMER_CURRENT_REG
);
149 } while (CurrentTicks
<= StartTicks
);
151 // Then wait for the target
153 CurrentTicks
= MmioRead32 (SP804_TIMER_METRONOME_BASE
+ SP804_TIMER_CURRENT_REG
);
154 } while (CurrentTicks
> TargetTicks
);
161 Stalls the CPU for at least the given number of nanoseconds.
163 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
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.
168 @param NanoSeconds The minimum number of nanoseconds to delay.
170 @return The value of NanoSeconds inputted.
181 // Round up to 1us Tick Number
182 MicroSeconds
= NanoSeconds
/ 1000;
183 MicroSeconds
+= ((NanoSeconds
% 1000) == 0) ? 0 : 1;
185 MicroSecondDelay (MicroSeconds
);
191 Retrieves the current value of a 64-bit free running performance counter.
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().
198 @return The current value of the free running performance counter.
203 GetPerformanceCounter (
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
210 Value
= MmioRead32 (SP804_TIMER_PERFORMANCE_BASE
+ SP804_TIMER_CURRENT_REG
);
216 Retrieves the 64-bit frequency in Hz and the range of performance counter
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.
230 @param StartValue The value the performance counter starts with when it
232 @param EndValue The value that the performance counter ends with before
235 @return The frequency in Hz.
240 GetPerformanceCounterProperties (
241 OUT UINT64
*StartValue
, OPTIONAL
242 OUT UINT64
*EndValue OPTIONAL
245 if (StartValue
!= NULL
) {
246 // Timer starts with the reload value
247 *StartValue
= 0xFFFFFFFF;
250 if (EndValue
!= NULL
) {
251 // Timer counts down to 0x0
252 *EndValue
= (UINT64
)0ULL;
255 return PcdGet64 (PcdEmbeddedPerformanceCounterFrequencyInHz
);