]>
Commit | Line | Data |
---|---|---|
a3f98646 | 1 | /** @file |
2 | Template for Timer Architecture Protocol driver of the ARM flavor | |
3 | ||
3d70643b | 4 | Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR> |
a3f98646 | 5 | |
3d70643b | 6 | This program and the accompanying materials |
a3f98646 | 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 | ||
17 | #include <PiDxe.h> | |
18 | ||
19 | #include <Library/BaseLib.h> | |
20 | #include <Library/DebugLib.h> | |
21 | #include <Library/BaseMemoryLib.h> | |
22 | #include <Library/UefiBootServicesTableLib.h> | |
23 | #include <Library/UefiLib.h> | |
24 | #include <Library/PcdLib.h> | |
25 | #include <Library/IoLib.h> | |
26 | #include <Library/OmapLib.h> | |
27 | ||
28 | #include <Protocol/Timer.h> | |
29 | #include <Protocol/HardwareInterrupt.h> | |
a3f98646 | 30 | |
31 | #include <Omap3530/Omap3530.h> | |
32 | ||
33 | ||
34 | // The notification function to call on every timer interrupt. | |
35 | volatile EFI_TIMER_NOTIFY mTimerNotifyFunction = (EFI_TIMER_NOTIFY)NULL; | |
a3f98646 | 36 | |
37 | ||
38 | // The current period of the timer interrupt | |
39 | volatile UINT64 mTimerPeriod = 0; | |
40 | ||
41 | // Cached copy of the Hardware Interrupt protocol instance | |
42 | EFI_HARDWARE_INTERRUPT_PROTOCOL *gInterrupt = NULL; | |
43 | ||
44 | // Cached registers | |
45 | volatile UINT32 TISR; | |
46 | volatile UINT32 TCLR; | |
47 | volatile UINT32 TLDR; | |
48 | volatile UINT32 TCRR; | |
49 | volatile UINT32 TIER; | |
50 | ||
51 | // Cached interrupt vector | |
52 | volatile UINTN gVector; | |
53 | ||
54 | ||
026e30c4 | 55 | /** |
56 | ||
57 | C Interrupt Handler calledin the interrupt context when Source interrupt is active. | |
58 | ||
59 | ||
026e30c4 | 60 | @param Source Source of the interrupt. Hardware routing off a specific platform defines |
026e30c4 | 61 | what source means. |
62 | ||
63 | @param SystemContext Pointer to system register context. Mostly used by debuggers and will | |
026e30c4 | 64 | update the system context after the return from the interrupt if |
026e30c4 | 65 | modified. Don't change these values unless you know what you are doing |
66 | ||
a3f98646 | 67 | **/ |
68 | VOID | |
69 | EFIAPI | |
70 | TimerInterruptHandler ( | |
71 | IN HARDWARE_INTERRUPT_SOURCE Source, | |
72 | IN EFI_SYSTEM_CONTEXT SystemContext | |
73 | ) | |
74 | { | |
026e30c4 | 75 | EFI_TPL OriginalTPL; |
76 | ||
77 | ||
78 | ||
79 | // | |
026e30c4 | 80 | // DXE core uses this callback for the EFI timer tick. The DXE core uses locks |
026e30c4 | 81 | // that raise to TPL_HIGH and then restore back to current level. Thus we need |
026e30c4 | 82 | // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick. |
026e30c4 | 83 | // |
026e30c4 | 84 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL); |
85 | ||
a3f98646 | 86 | if (mTimerNotifyFunction) { |
87 | mTimerNotifyFunction(mTimerPeriod); | |
88 | } | |
89 | ||
90 | // Clear all timer interrupts | |
026e30c4 | 91 | MmioWrite32 (TISR, TISR_CLEAR_ALL); |
a3f98646 | 92 | |
93 | // Poll interrupt status bits to ensure clearing | |
43263288 | 94 | while ((MmioRead32 (TISR) & TISR_ALL_INTERRUPT_MASK) != TISR_NO_INTERRUPTS_PENDING); |
a3f98646 | 95 | |
96 | gBS->RestoreTPL (OriginalTPL); | |
97 | } | |
98 | ||
026e30c4 | 99 | /** |
026e30c4 | 100 | This function registers the handler NotifyFunction so it is called every time |
026e30c4 | 101 | the timer interrupt fires. It also passes the amount of time since the last |
026e30c4 | 102 | handler call to the NotifyFunction. If NotifyFunction is NULL, then the |
026e30c4 | 103 | handler is unregistered. If the handler is registered, then EFI_SUCCESS is |
026e30c4 | 104 | returned. If the CPU does not support registering a timer interrupt handler, |
026e30c4 | 105 | then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler |
026e30c4 | 106 | when a handler is already registered, then EFI_ALREADY_STARTED is returned. |
026e30c4 | 107 | If an attempt is made to unregister a handler when a handler is not registered, |
026e30c4 | 108 | then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to |
026e30c4 | 109 | register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR |
026e30c4 | 110 | is returned. |
111 | ||
026e30c4 | 112 | @param This The EFI_TIMER_ARCH_PROTOCOL instance. |
026e30c4 | 113 | @param NotifyFunction The function to call when a timer interrupt fires. This |
026e30c4 | 114 | function executes at TPL_HIGH_LEVEL. The DXE Core will |
026e30c4 | 115 | register a handler for the timer interrupt, so it can know |
026e30c4 | 116 | how much time has passed. This information is used to |
026e30c4 | 117 | signal timer based events. NULL will unregister the handler. |
026e30c4 | 118 | @retval EFI_SUCCESS The timer handler was registered. |
026e30c4 | 119 | @retval EFI_UNSUPPORTED The platform does not support timer interrupts. |
026e30c4 | 120 | @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already |
026e30c4 | 121 | registered. |
026e30c4 | 122 | @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not |
026e30c4 | 123 | previously registered. |
026e30c4 | 124 | @retval EFI_DEVICE_ERROR The timer handler could not be registered. |
125 | ||
026e30c4 | 126 | **/ |
a3f98646 | 127 | EFI_STATUS |
128 | EFIAPI | |
129 | TimerDriverRegisterHandler ( | |
130 | IN EFI_TIMER_ARCH_PROTOCOL *This, | |
131 | IN EFI_TIMER_NOTIFY NotifyFunction | |
132 | ) | |
133 | { | |
134 | if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) { | |
135 | return EFI_INVALID_PARAMETER; | |
136 | } | |
137 | ||
138 | if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) { | |
139 | return EFI_ALREADY_STARTED; | |
140 | } | |
141 | ||
142 | mTimerNotifyFunction = NotifyFunction; | |
143 | ||
144 | return EFI_SUCCESS; | |
145 | } | |
146 | ||
026e30c4 | 147 | /** |
148 | ||
149 | This function adjusts the period of timer interrupts to the value specified | |
026e30c4 | 150 | by TimerPeriod. If the timer period is updated, then the selected timer |
026e30c4 | 151 | period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If |
026e30c4 | 152 | the timer hardware is not programmable, then EFI_UNSUPPORTED is returned. |
026e30c4 | 153 | If an error occurs while attempting to update the timer period, then the |
026e30c4 | 154 | timer hardware will be put back in its state prior to this call, and |
026e30c4 | 155 | EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt |
026e30c4 | 156 | is disabled. This is not the same as disabling the CPU's interrupts. |
026e30c4 | 157 | Instead, it must either turn off the timer hardware, or it must adjust the |
026e30c4 | 158 | interrupt controller so that a CPU interrupt is not generated when the timer |
026e30c4 | 159 | interrupt fires. |
160 | ||
026e30c4 | 161 | @param This The EFI_TIMER_ARCH_PROTOCOL instance. |
026e30c4 | 162 | @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If |
026e30c4 | 163 | the timer hardware is not programmable, then EFI_UNSUPPORTED is |
026e30c4 | 164 | returned. If the timer is programmable, then the timer period |
026e30c4 | 165 | will be rounded up to the nearest timer period that is supported |
026e30c4 | 166 | by the timer hardware. If TimerPeriod is set to 0, then the |
026e30c4 | 167 | timer interrupts will be disabled. |
168 | ||
169 | ||
026e30c4 | 170 | @retval EFI_SUCCESS The timer period was changed. |
026e30c4 | 171 | @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt. |
026e30c4 | 172 | @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error. |
173 | ||
a3f98646 | 174 | **/ |
175 | EFI_STATUS | |
176 | EFIAPI | |
177 | TimerDriverSetTimerPeriod ( | |
178 | IN EFI_TIMER_ARCH_PROTOCOL *This, | |
179 | IN UINT64 TimerPeriod | |
180 | ) | |
181 | { | |
182 | EFI_STATUS Status; | |
183 | UINT64 TimerCount; | |
184 | INT32 LoadValue; | |
185 | ||
186 | if (TimerPeriod == 0) { | |
187 | // Turn off GPTIMER3 | |
026e30c4 | 188 | MmioWrite32 (TCLR, TCLR_ST_OFF); |
a3f98646 | 189 | |
190 | Status = gInterrupt->DisableInterruptSource(gInterrupt, gVector); | |
191 | } else { | |
192 | // Calculate required timer count | |
55bff42e | 193 | TimerCount = DivU64x32(TimerPeriod * 100, PcdGet32(PcdEmbeddedPerformanceCounterPeriodInNanoseconds)); |
a3f98646 | 194 | |
195 | // Set GPTIMER3 Load register | |
196 | LoadValue = (INT32) -TimerCount; | |
026e30c4 | 197 | MmioWrite32 (TLDR, LoadValue); |
198 | MmioWrite32 (TCRR, LoadValue); | |
a3f98646 | 199 | |
200 | // Enable Overflow interrupt | |
026e30c4 | 201 | MmioWrite32 (TIER, TIER_TCAR_IT_DISABLE | TIER_OVF_IT_ENABLE | TIER_MAT_IT_DISABLE); |
a3f98646 | 202 | |
203 | // Turn on GPTIMER3, it will reload at overflow | |
026e30c4 | 204 | MmioWrite32 (TCLR, TCLR_AR_AUTORELOAD | TCLR_ST_ON); |
a3f98646 | 205 | |
206 | Status = gInterrupt->EnableInterruptSource(gInterrupt, gVector); | |
207 | } | |
208 | ||
209 | // | |
210 | // Save the new timer period | |
211 | // | |
212 | mTimerPeriod = TimerPeriod; | |
213 | return Status; | |
214 | } | |
215 | ||
216 | ||
026e30c4 | 217 | /** |
026e30c4 | 218 | This function retrieves the period of timer interrupts in 100 ns units, |
026e30c4 | 219 | returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod |
026e30c4 | 220 | is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is |
026e30c4 | 221 | returned, then the timer is currently disabled. |
222 | ||
026e30c4 | 223 | @param This The EFI_TIMER_ARCH_PROTOCOL instance. |
026e30c4 | 224 | @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If |
026e30c4 | 225 | 0 is returned, then the timer is currently disabled. |
226 | ||
227 | ||
026e30c4 | 228 | @retval EFI_SUCCESS The timer period was returned in TimerPeriod. |
026e30c4 | 229 | @retval EFI_INVALID_PARAMETER TimerPeriod is NULL. |
230 | ||
a3f98646 | 231 | **/ |
232 | EFI_STATUS | |
233 | EFIAPI | |
234 | TimerDriverGetTimerPeriod ( | |
235 | IN EFI_TIMER_ARCH_PROTOCOL *This, | |
236 | OUT UINT64 *TimerPeriod | |
237 | ) | |
238 | { | |
239 | if (TimerPeriod == NULL) { | |
240 | return EFI_INVALID_PARAMETER; | |
241 | } | |
242 | ||
243 | *TimerPeriod = mTimerPeriod; | |
244 | return EFI_SUCCESS; | |
245 | } | |
246 | ||
026e30c4 | 247 | /** |
026e30c4 | 248 | This function generates a soft timer interrupt. If the platform does not support soft |
026e30c4 | 249 | timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned. |
026e30c4 | 250 | If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler() |
026e30c4 | 251 | service, then a soft timer interrupt will be generated. If the timer interrupt is |
026e30c4 | 252 | enabled when this service is called, then the registered handler will be invoked. The |
026e30c4 | 253 | registered handler should not be able to distinguish a hardware-generated timer |
026e30c4 | 254 | interrupt from a software-generated timer interrupt. |
255 | ||
026e30c4 | 256 | @param This The EFI_TIMER_ARCH_PROTOCOL instance. |
257 | ||
026e30c4 | 258 | @retval EFI_SUCCESS The soft timer interrupt was generated. |
026e30c4 | 259 | @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts. |
260 | ||
a3f98646 | 261 | **/ |
262 | EFI_STATUS | |
263 | EFIAPI | |
264 | TimerDriverGenerateSoftInterrupt ( | |
265 | IN EFI_TIMER_ARCH_PROTOCOL *This | |
266 | ) | |
267 | { | |
268 | return EFI_UNSUPPORTED; | |
269 | } | |
270 | ||
271 | ||
026e30c4 | 272 | /** |
026e30c4 | 273 | Interface stucture for the Timer Architectural Protocol. |
274 | ||
026e30c4 | 275 | @par Protocol Description: |
026e30c4 | 276 | This protocol provides the services to initialize a periodic timer |
026e30c4 | 277 | interrupt, and to register a handler that is called each time the timer |
026e30c4 | 278 | interrupt fires. It may also provide a service to adjust the rate of the |
026e30c4 | 279 | periodic timer interrupt. When a timer interrupt occurs, the handler is |
026e30c4 | 280 | passed the amount of time that has passed since the previous timer |
026e30c4 | 281 | interrupt. |
282 | ||
026e30c4 | 283 | @param RegisterHandler |
026e30c4 | 284 | Registers a handler that will be called each time the |
026e30c4 | 285 | timer interrupt fires. TimerPeriod defines the minimum |
026e30c4 | 286 | time between timer interrupts, so TimerPeriod will also |
026e30c4 | 287 | be the minimum time between calls to the registered |
026e30c4 | 288 | handler. |
289 | ||
026e30c4 | 290 | @param SetTimerPeriod |
026e30c4 | 291 | Sets the period of the timer interrupt in 100 nS units. |
026e30c4 | 292 | This function is optional, and may return EFI_UNSUPPORTED. |
026e30c4 | 293 | If this function is supported, then the timer period will |
026e30c4 | 294 | be rounded up to the nearest supported timer period. |
295 | ||
296 | ||
026e30c4 | 297 | @param GetTimerPeriod |
026e30c4 | 298 | Retrieves the period of the timer interrupt in 100 nS units. |
299 | ||
026e30c4 | 300 | @param GenerateSoftInterrupt |
026e30c4 | 301 | Generates a soft timer interrupt that simulates the firing of |
026c3d34 | 302 | the timer interrupt. This service can be used to invoke the registered handler if the timer interrupt has been masked for |
026e30c4 | 303 | a period of time. |
304 | ||
a3f98646 | 305 | **/ |
306 | EFI_TIMER_ARCH_PROTOCOL gTimer = { | |
307 | TimerDriverRegisterHandler, | |
308 | TimerDriverSetTimerPeriod, | |
309 | TimerDriverGetTimerPeriod, | |
310 | TimerDriverGenerateSoftInterrupt | |
311 | }; | |
312 | ||
a3f98646 | 313 | |
026e30c4 | 314 | /** |
026e30c4 | 315 | Initialize the state information for the Timer Architectural Protocol and |
026e30c4 | 316 | the Timer Debug support protocol that allows the debugger to break into a |
026e30c4 | 317 | running program. |
318 | ||
026e30c4 | 319 | @param ImageHandle of the loaded driver |
026e30c4 | 320 | @param SystemTable Pointer to the System Table |
321 | ||
026e30c4 | 322 | @retval EFI_SUCCESS Protocol registered |
026e30c4 | 323 | @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure |
026e30c4 | 324 | @retval EFI_DEVICE_ERROR Hardware problems |
325 | ||
a3f98646 | 326 | **/ |
327 | EFI_STATUS | |
328 | EFIAPI | |
329 | TimerInitialize ( | |
330 | IN EFI_HANDLE ImageHandle, | |
331 | IN EFI_SYSTEM_TABLE *SystemTable | |
332 | ) | |
333 | { | |
334 | EFI_HANDLE Handle = NULL; | |
335 | EFI_STATUS Status; | |
336 | UINT32 TimerBaseAddress; | |
337 | ||
338 | // Find the interrupt controller protocol. ASSERT if not found. | |
23d3998a | 339 | Status = gBS->LocateProtocol (&gHardwareInterruptProtocolGuid, NULL, (VOID **)&gInterrupt); |
a3f98646 | 340 | ASSERT_EFI_ERROR (Status); |
341 | ||
342 | // Set up the timer registers | |
23d3998a | 343 | TimerBaseAddress = TimerBase (FixedPcdGet32(PcdOmap35xxArchTimer)); |
a3f98646 | 344 | TISR = TimerBaseAddress + GPTIMER_TISR; |
345 | TCLR = TimerBaseAddress + GPTIMER_TCLR; | |
346 | TLDR = TimerBaseAddress + GPTIMER_TLDR; | |
347 | TCRR = TimerBaseAddress + GPTIMER_TCRR; | |
348 | TIER = TimerBaseAddress + GPTIMER_TIER; | |
349 | ||
350 | // Disable the timer | |
23d3998a | 351 | Status = TimerDriverSetTimerPeriod (&gTimer, 0); |
a3f98646 | 352 | ASSERT_EFI_ERROR (Status); |
353 | ||
354 | // Install interrupt handler | |
23d3998a | 355 | gVector = InterruptVectorForTimer (FixedPcdGet32(PcdOmap35xxArchTimer)); |
356 | Status = gInterrupt->RegisterInterruptSource (gInterrupt, gVector, TimerInterruptHandler); | |
a3f98646 | 357 | ASSERT_EFI_ERROR (Status); |
358 | ||
2d08d17d | 359 | // Turn on the functional clock for Timer |
360 | MmioOr32 (CM_FCLKEN_PER, CM_FCLKEN_PER_EN_GPT3_ENABLE); | |
361 | ||
a3f98646 | 362 | // Set up default timer |
23d3998a | 363 | Status = TimerDriverSetTimerPeriod (&gTimer, FixedPcdGet32(PcdTimerPeriod)); |
a3f98646 | 364 | ASSERT_EFI_ERROR (Status); |
365 | ||
366 | // Install the Timer Architectural Protocol onto a new handle | |
23d3998a | 367 | Status = gBS->InstallMultipleProtocolInterfaces ( |
368 | &Handle, | |
369 | &gEfiTimerArchProtocolGuid, &gTimer, | |
370 | NULL | |
371 | ); | |
a3f98646 | 372 | ASSERT_EFI_ERROR(Status); |
373 | ||
374 | return Status; | |
375 | } | |
376 |