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