2 Template for Timer Architecture Protocol driver of the ARM flavor
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/BaseLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include <Library/UefiLib.h>
18 #include <Library/PcdLib.h>
19 #include <Library/IoLib.h>
20 #include <Library/OmapLib.h>
22 #include <Protocol/Timer.h>
23 #include <Protocol/HardwareInterrupt.h>
25 #include <Omap3530/Omap3530.h>
28 // The notification function to call on every timer interrupt.
29 volatile EFI_TIMER_NOTIFY mTimerNotifyFunction
= (EFI_TIMER_NOTIFY
)NULL
;
32 // The current period of the timer interrupt
33 volatile UINT64 mTimerPeriod
= 0;
35 // Cached copy of the Hardware Interrupt protocol instance
36 EFI_HARDWARE_INTERRUPT_PROTOCOL
*gInterrupt
= NULL
;
45 // Cached interrupt vector
46 volatile UINTN gVector
;
51 C Interrupt Handler calledin the interrupt context when Source interrupt is active.
54 @param Source Source of the interrupt. Hardware routing off a specific platform defines
57 @param SystemContext Pointer to system register context. Mostly used by debuggers and will
58 update the system context after the return from the interrupt if
59 modified. Don't change these values unless you know what you are doing
64 TimerInterruptHandler (
65 IN HARDWARE_INTERRUPT_SOURCE Source
,
66 IN EFI_SYSTEM_CONTEXT SystemContext
74 // DXE core uses this callback for the EFI timer tick. The DXE core uses locks
75 // that raise to TPL_HIGH and then restore back to current level. Thus we need
76 // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick.
78 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
80 if (mTimerNotifyFunction
) {
81 mTimerNotifyFunction(mTimerPeriod
);
84 // Clear all timer interrupts
85 MmioWrite32 (TISR
, TISR_CLEAR_ALL
);
87 // Poll interrupt status bits to ensure clearing
88 while ((MmioRead32 (TISR
) & TISR_ALL_INTERRUPT_MASK
) != TISR_NO_INTERRUPTS_PENDING
);
90 gBS
->RestoreTPL (OriginalTPL
);
94 This function registers the handler NotifyFunction so it is called every time
95 the timer interrupt fires. It also passes the amount of time since the last
96 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
97 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
98 returned. If the CPU does not support registering a timer interrupt handler,
99 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
100 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
101 If an attempt is made to unregister a handler when a handler is not registered,
102 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
103 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
106 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
107 @param NotifyFunction The function to call when a timer interrupt fires. This
108 function executes at TPL_HIGH_LEVEL. The DXE Core will
109 register a handler for the timer interrupt, so it can know
110 how much time has passed. This information is used to
111 signal timer based events. NULL will unregister the handler.
112 @retval EFI_SUCCESS The timer handler was registered.
113 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
114 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
116 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
117 previously registered.
118 @retval EFI_DEVICE_ERROR The timer handler could not be registered.
123 TimerDriverRegisterHandler (
124 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
125 IN EFI_TIMER_NOTIFY NotifyFunction
128 if ((NotifyFunction
== NULL
) && (mTimerNotifyFunction
== NULL
)) {
129 return EFI_INVALID_PARAMETER
;
132 if ((NotifyFunction
!= NULL
) && (mTimerNotifyFunction
!= NULL
)) {
133 return EFI_ALREADY_STARTED
;
136 mTimerNotifyFunction
= NotifyFunction
;
143 This function adjusts the period of timer interrupts to the value specified
144 by TimerPeriod. If the timer period is updated, then the selected timer
145 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
146 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
147 If an error occurs while attempting to update the timer period, then the
148 timer hardware will be put back in its state prior to this call, and
149 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
150 is disabled. This is not the same as disabling the CPU's interrupts.
151 Instead, it must either turn off the timer hardware, or it must adjust the
152 interrupt controller so that a CPU interrupt is not generated when the timer
155 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
156 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
157 the timer hardware is not programmable, then EFI_UNSUPPORTED is
158 returned. If the timer is programmable, then the timer period
159 will be rounded up to the nearest timer period that is supported
160 by the timer hardware. If TimerPeriod is set to 0, then the
161 timer interrupts will be disabled.
164 @retval EFI_SUCCESS The timer period was changed.
165 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
166 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
171 TimerDriverSetTimerPeriod (
172 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
173 IN UINT64 TimerPeriod
180 if (TimerPeriod
== 0) {
182 MmioWrite32 (TCLR
, TCLR_ST_OFF
);
184 Status
= gInterrupt
->DisableInterruptSource(gInterrupt
, gVector
);
186 // Calculate required timer count
187 TimerCount
= DivU64x32(TimerPeriod
* 100, PcdGet32(PcdEmbeddedPerformanceCounterPeriodInNanoseconds
));
189 // Set GPTIMER3 Load register
190 LoadValue
= (INT32
) -TimerCount
;
191 MmioWrite32 (TLDR
, LoadValue
);
192 MmioWrite32 (TCRR
, LoadValue
);
194 // Enable Overflow interrupt
195 MmioWrite32 (TIER
, TIER_TCAR_IT_DISABLE
| TIER_OVF_IT_ENABLE
| TIER_MAT_IT_DISABLE
);
197 // Turn on GPTIMER3, it will reload at overflow
198 MmioWrite32 (TCLR
, TCLR_AR_AUTORELOAD
| TCLR_ST_ON
);
200 Status
= gInterrupt
->EnableInterruptSource(gInterrupt
, gVector
);
204 // Save the new timer period
206 mTimerPeriod
= TimerPeriod
;
212 This function retrieves the period of timer interrupts in 100 ns units,
213 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
214 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
215 returned, then the timer is currently disabled.
217 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
218 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
219 0 is returned, then the timer is currently disabled.
222 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
223 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
228 TimerDriverGetTimerPeriod (
229 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
230 OUT UINT64
*TimerPeriod
233 if (TimerPeriod
== NULL
) {
234 return EFI_INVALID_PARAMETER
;
237 *TimerPeriod
= mTimerPeriod
;
242 This function generates a soft timer interrupt. If the platform does not support soft
243 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
244 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
245 service, then a soft timer interrupt will be generated. If the timer interrupt is
246 enabled when this service is called, then the registered handler will be invoked. The
247 registered handler should not be able to distinguish a hardware-generated timer
248 interrupt from a software-generated timer interrupt.
250 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
252 @retval EFI_SUCCESS The soft timer interrupt was generated.
253 @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
258 TimerDriverGenerateSoftInterrupt (
259 IN EFI_TIMER_ARCH_PROTOCOL
*This
262 return EFI_UNSUPPORTED
;
267 Interface stucture for the Timer Architectural Protocol.
269 @par Protocol Description:
270 This protocol provides the services to initialize a periodic timer
271 interrupt, and to register a handler that is called each time the timer
272 interrupt fires. It may also provide a service to adjust the rate of the
273 periodic timer interrupt. When a timer interrupt occurs, the handler is
274 passed the amount of time that has passed since the previous timer
277 @param RegisterHandler
278 Registers a handler that will be called each time the
279 timer interrupt fires. TimerPeriod defines the minimum
280 time between timer interrupts, so TimerPeriod will also
281 be the minimum time between calls to the registered
284 @param SetTimerPeriod
285 Sets the period of the timer interrupt in 100 nS units.
286 This function is optional, and may return EFI_UNSUPPORTED.
287 If this function is supported, then the timer period will
288 be rounded up to the nearest supported timer period.
291 @param GetTimerPeriod
292 Retrieves the period of the timer interrupt in 100 nS units.
294 @param GenerateSoftInterrupt
295 Generates a soft timer interrupt that simulates the firing of
296 the timer interrupt. This service can be used to invoke the registered handler if the timer interrupt has been masked for
300 EFI_TIMER_ARCH_PROTOCOL gTimer
= {
301 TimerDriverRegisterHandler
,
302 TimerDriverSetTimerPeriod
,
303 TimerDriverGetTimerPeriod
,
304 TimerDriverGenerateSoftInterrupt
309 Initialize the state information for the Timer Architectural Protocol and
310 the Timer Debug support protocol that allows the debugger to break into a
313 @param ImageHandle of the loaded driver
314 @param SystemTable Pointer to the System Table
316 @retval EFI_SUCCESS Protocol registered
317 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
318 @retval EFI_DEVICE_ERROR Hardware problems
324 IN EFI_HANDLE ImageHandle
,
325 IN EFI_SYSTEM_TABLE
*SystemTable
328 EFI_HANDLE Handle
= NULL
;
330 UINT32 TimerBaseAddress
;
332 // Find the interrupt controller protocol. ASSERT if not found.
333 Status
= gBS
->LocateProtocol (&gHardwareInterruptProtocolGuid
, NULL
, (VOID
**)&gInterrupt
);
334 ASSERT_EFI_ERROR (Status
);
336 // Set up the timer registers
337 TimerBaseAddress
= TimerBase (FixedPcdGet32(PcdOmap35xxArchTimer
));
338 TISR
= TimerBaseAddress
+ GPTIMER_TISR
;
339 TCLR
= TimerBaseAddress
+ GPTIMER_TCLR
;
340 TLDR
= TimerBaseAddress
+ GPTIMER_TLDR
;
341 TCRR
= TimerBaseAddress
+ GPTIMER_TCRR
;
342 TIER
= TimerBaseAddress
+ GPTIMER_TIER
;
345 Status
= TimerDriverSetTimerPeriod (&gTimer
, 0);
346 ASSERT_EFI_ERROR (Status
);
348 // Install interrupt handler
349 gVector
= InterruptVectorForTimer (FixedPcdGet32(PcdOmap35xxArchTimer
));
350 Status
= gInterrupt
->RegisterInterruptSource (gInterrupt
, gVector
, TimerInterruptHandler
);
351 ASSERT_EFI_ERROR (Status
);
353 // Turn on the functional clock for Timer
354 MmioOr32 (CM_FCLKEN_PER
, CM_FCLKEN_PER_EN_GPT3_ENABLE
);
356 // Set up default timer
357 Status
= TimerDriverSetTimerPeriod (&gTimer
, FixedPcdGet32(PcdTimerPeriod
));
358 ASSERT_EFI_ERROR (Status
);
360 // Install the Timer Architectural Protocol onto a new handle
361 Status
= gBS
->InstallMultipleProtocolInterfaces (
363 &gEfiTimerArchProtocolGuid
, &gTimer
,
366 ASSERT_EFI_ERROR(Status
);