]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPlatformPkg/Drivers/SP804TimerDxe/SP804Timer.c
Arm Packages: Fixed coding style/Line endings to follow EDK2 coding convention
[mirror_edk2.git] / ArmPlatformPkg / Drivers / SP804TimerDxe / SP804Timer.c
CommitLineData
1d5d0ae9 1/** @file
2 Template for Timer Architecture Protocol driver of the ARM flavor
3
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5
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
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
27#include <Protocol/Timer.h>
28#include <Protocol/HardwareInterrupt.h>
29
30#include <Drivers/SP804Timer.h>
5cc45b70 31
11c20f4e 32#define SP804_TIMER_PERIODIC_BASE ((UINTN)PcdGet32 (PcdSP804TimerPeriodicBase))
33#define SP804_TIMER_METRONOME_BASE ((UINTN)PcdGet32 (PcdSP804TimerMetronomeBase))
34#define SP804_TIMER_PERFORMANCE_BASE ((UINTN)PcdGet32 (PcdSP804TimerPerformanceBase))
1d5d0ae9 35
36// The notification function to call on every timer interrupt.
11c20f4e 37EFI_TIMER_NOTIFY mTimerNotifyFunction = (EFI_TIMER_NOTIFY)NULL;
38EFI_EVENT EfiExitBootServicesEvent = (EFI_EVENT)NULL;
1d5d0ae9 39
40// The current period of the timer interrupt
11c20f4e 41UINT64 mTimerPeriod = 0;
1d5d0ae9 42
43// Cached copy of the Hardware Interrupt protocol instance
44EFI_HARDWARE_INTERRUPT_PROTOCOL *gInterrupt = NULL;
45
46// Cached interrupt vector
47UINTN gVector;
48
1d5d0ae9 49
50/**
51
52 C Interrupt Handler called in the interrupt context when Source interrupt is active.
53
54
55 @param Source Source of the interrupt. Hardware routing off a specific platform defines
56 what source means.
57
58 @param SystemContext Pointer to system register context. Mostly used by debuggers and will
59 update the system context after the return from the interrupt if
60 modified. Don't change these values unless you know what you are doing
61
62**/
63VOID
64EFIAPI
65TimerInterruptHandler (
66 IN HARDWARE_INTERRUPT_SOURCE Source,
67 IN EFI_SYSTEM_CONTEXT SystemContext
68 )
69{
70 EFI_TPL OriginalTPL;
71
72 //
73 // DXE core uses this callback for the EFI timer tick. The DXE core uses locks
74 // that raise to TPL_HIGH and then restore back to current level. Thus we need
75 // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick.
76 //
77 OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
78
e100fa8c 79 // If the interrupt is shared then we must check if this interrupt source is the one associated to this Timer
5cc45b70 80 if (MmioRead32 (SP804_TIMER_PERIODIC_BASE + SP804_TIMER_MSK_INT_STS_REG) != 0) {
11c20f4e 81 // Clear the periodic interrupt
5cc45b70 82 MmioWrite32 (SP804_TIMER_PERIODIC_BASE + SP804_TIMER_INT_CLR_REG, 0);
1d5d0ae9 83
11c20f4e 84 // Signal end of interrupt early to help avoid losing subsequent ticks from long duration handlers
e100fa8c 85 gInterrupt->EndOfInterrupt (gInterrupt, Source);
1d5d0ae9 86
e100fa8c 87 if (mTimerNotifyFunction) {
88 mTimerNotifyFunction (mTimerPeriod);
89 }
1d5d0ae9 90 }
91
92 gBS->RestoreTPL (OriginalTPL);
93}
94
95/**
96 This function registers the handler NotifyFunction so it is called every time
97 the timer interrupt fires. It also passes the amount of time since the last
98 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
99 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
100 returned. If the CPU does not support registering a timer interrupt handler,
101 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
102 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
103 If an attempt is made to unregister a handler when a handler is not registered,
104 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
105 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
106 is returned.
107
108 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
109 @param NotifyFunction The function to call when a timer interrupt fires. This
110 function executes at TPL_HIGH_LEVEL. The DXE Core will
111 register a handler for the timer interrupt, so it can know
112 how much time has passed. This information is used to
113 signal timer based events. NULL will unregister the handler.
114 @retval EFI_SUCCESS The timer handler was registered.
115 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
116 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
117 registered.
118 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
119 previously registered.
120 @retval EFI_DEVICE_ERROR The timer handler could not be registered.
121
122**/
123EFI_STATUS
124EFIAPI
125TimerDriverRegisterHandler (
126 IN EFI_TIMER_ARCH_PROTOCOL *This,
127 IN EFI_TIMER_NOTIFY NotifyFunction
128 )
129{
130 if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) {
131 return EFI_INVALID_PARAMETER;
132 }
133
134 if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) {
135 return EFI_ALREADY_STARTED;
136 }
137
138 mTimerNotifyFunction = NotifyFunction;
139
140 return EFI_SUCCESS;
141}
142
143/**
5cc45b70 144 Make sure all Dual Timers are disabled
1d5d0ae9 145**/
146VOID
147EFIAPI
148ExitBootServicesEvent (
149 IN EFI_EVENT Event,
150 IN VOID *Context
151 )
152{
5cc45b70 153 // Disable 'Periodic Operation' timer if enabled
154 if (MmioRead32(SP804_TIMER_PERIODIC_BASE + SP804_TIMER_CONTROL_REG) & SP804_TIMER_CTRL_ENABLE) {
155 MmioAnd32 (SP804_TIMER_PERIODIC_BASE + SP804_TIMER_CONTROL_REG, 0);
156 }
1d5d0ae9 157
5cc45b70 158 // Disable 'Metronome/Delay' timer if enabled
159 if (MmioRead32(SP804_TIMER_METRONOME_BASE + SP804_TIMER_CONTROL_REG) & SP804_TIMER_CTRL_ENABLE) {
160 MmioAnd32 (SP804_TIMER_METRONOME_BASE + SP804_TIMER_CONTROL_REG, 0);
161 }
1d5d0ae9 162
5cc45b70 163 // Disable 'Performance' timer if enabled
164 if (MmioRead32(SP804_TIMER_PERFORMANCE_BASE + SP804_TIMER_CONTROL_REG) & SP804_TIMER_CTRL_ENABLE) {
165 MmioAnd32 (SP804_TIMER_PERFORMANCE_BASE + SP804_TIMER_CONTROL_REG, 0);
166 }
1d5d0ae9 167}
168
169/**
170
171 This function adjusts the period of timer interrupts to the value specified
172 by TimerPeriod. If the timer period is updated, then the selected timer
173 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
174 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
175 If an error occurs while attempting to update the timer period, then the
176 timer hardware will be put back in its state prior to this call, and
177 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
178 is disabled. This is not the same as disabling the CPU's interrupts.
179 Instead, it must either turn off the timer hardware, or it must adjust the
180 interrupt controller so that a CPU interrupt is not generated when the timer
181 interrupt fires.
182
183 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
184 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
185 the timer hardware is not programmable, then EFI_UNSUPPORTED is
186 returned. If the timer is programmable, then the timer period
187 will be rounded up to the nearest timer period that is supported
188 by the timer hardware. If TimerPeriod is set to 0, then the
189 timer interrupts will be disabled.
190
191
192 @retval EFI_SUCCESS The timer period was changed.
193 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
194 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
195
196**/
197EFI_STATUS
198EFIAPI
199TimerDriverSetTimerPeriod (
200 IN EFI_TIMER_ARCH_PROTOCOL *This,
201 IN UINT64 TimerPeriod
202 )
203{
204 EFI_STATUS Status;
205 UINT64 TimerTicks;
206
207 // always disable the timer
5cc45b70 208 MmioAnd32 (SP804_TIMER_PERIODIC_BASE + SP804_TIMER_CONTROL_REG, ~SP804_TIMER_CTRL_ENABLE);
1d5d0ae9 209
210 if (TimerPeriod == 0) {
23792dea 211 // Leave timer disabled from above, and...
1d5d0ae9 212
23792dea 213 // Disable timer 0/1 interrupt for a TimerPeriod of 0
1d5d0ae9 214 Status = gInterrupt->DisableInterruptSource (gInterrupt, gVector);
215 } else {
11c20f4e 216 // Convert TimerPeriod into 1MHz clock counts (us units = 100ns units * 10)
1d5d0ae9 217 TimerTicks = DivU64x32 (TimerPeriod, 10);
5cc45b70 218 TimerTicks = MultU64x32 (TimerTicks, PcdGet32(PcdSP804TimerFrequencyInMHz));
1d5d0ae9 219
220 // if it's larger than 32-bits, pin to highest value
221 if (TimerTicks > 0xffffffff) {
1d5d0ae9 222 TimerTicks = 0xffffffff;
1d5d0ae9 223 }
224
225 // Program the SP804 timer with the new count value
5cc45b70 226 MmioWrite32 (SP804_TIMER_PERIODIC_BASE + SP804_TIMER_LOAD_REG, TimerTicks);
1d5d0ae9 227
228 // enable the timer
5cc45b70 229 MmioOr32 (SP804_TIMER_PERIODIC_BASE + SP804_TIMER_CONTROL_REG, SP804_TIMER_CTRL_ENABLE);
1d5d0ae9 230
231 // enable timer 0/1 interrupts
232 Status = gInterrupt->EnableInterruptSource (gInterrupt, gVector);
233 }
234
235 // Save the new timer period
236 mTimerPeriod = TimerPeriod;
237 return Status;
238}
239
240/**
241 This function retrieves the period of timer interrupts in 100 ns units,
242 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
243 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
244 returned, then the timer is currently disabled.
245
246 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
247 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
248 0 is returned, then the timer is currently disabled.
249
250
251 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
252 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
253
254**/
255EFI_STATUS
256EFIAPI
257TimerDriverGetTimerPeriod (
258 IN EFI_TIMER_ARCH_PROTOCOL *This,
259 OUT UINT64 *TimerPeriod
260 )
261{
262 if (TimerPeriod == NULL) {
263 return EFI_INVALID_PARAMETER;
264 }
265
266 *TimerPeriod = mTimerPeriod;
267 return EFI_SUCCESS;
268}
269
270/**
271 This function generates a soft timer interrupt. If the platform does not support soft
272 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
273 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
274 service, then a soft timer interrupt will be generated. If the timer interrupt is
275 enabled when this service is called, then the registered handler will be invoked. The
276 registered handler should not be able to distinguish a hardware-generated timer
277 interrupt from a software-generated timer interrupt.
278
279 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
280
281 @retval EFI_SUCCESS The soft timer interrupt was generated.
282 @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
283
284**/
285EFI_STATUS
286EFIAPI
287TimerDriverGenerateSoftInterrupt (
288 IN EFI_TIMER_ARCH_PROTOCOL *This
289 )
290{
291 return EFI_UNSUPPORTED;
292}
293
294/**
295 Interface structure for the Timer Architectural Protocol.
296
297 @par Protocol Description:
298 This protocol provides the services to initialize a periodic timer
299 interrupt, and to register a handler that is called each time the timer
300 interrupt fires. It may also provide a service to adjust the rate of the
301 periodic timer interrupt. When a timer interrupt occurs, the handler is
302 passed the amount of time that has passed since the previous timer
303 interrupt.
304
305 @param RegisterHandler
306 Registers a handler that will be called each time the
307 timer interrupt fires. TimerPeriod defines the minimum
308 time between timer interrupts, so TimerPeriod will also
309 be the minimum time between calls to the registered
310 handler.
311
312 @param SetTimerPeriod
313 Sets the period of the timer interrupt in 100 nS units.
314 This function is optional, and may return EFI_UNSUPPORTED.
315 If this function is supported, then the timer period will
316 be rounded up to the nearest supported timer period.
317
318
319 @param GetTimerPeriod
320 Retrieves the period of the timer interrupt in 100 nS units.
321
322 @param GenerateSoftInterrupt
323 Generates a soft timer interrupt that simulates the firing of
324 the timer interrupt. This service can be used to invoke the registered handler if the timer interrupt has been masked for
325 a period of time.
326
327**/
328EFI_TIMER_ARCH_PROTOCOL gTimer = {
329 TimerDriverRegisterHandler,
330 TimerDriverSetTimerPeriod,
331 TimerDriverGetTimerPeriod,
332 TimerDriverGenerateSoftInterrupt
333};
334
335
336/**
337 Initialize the state information for the Timer Architectural Protocol and
338 the Timer Debug support protocol that allows the debugger to break into a
339 running program.
340
341 @param ImageHandle of the loaded driver
342 @param SystemTable Pointer to the System Table
343
344 @retval EFI_SUCCESS Protocol registered
345 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
346 @retval EFI_DEVICE_ERROR Hardware problems
347
348**/
349EFI_STATUS
350EFIAPI
351TimerInitialize (
352 IN EFI_HANDLE ImageHandle,
353 IN EFI_SYSTEM_TABLE *SystemTable
354 )
355{
356 EFI_HANDLE Handle = NULL;
357 EFI_STATUS Status;
358
359 // Find the interrupt controller protocol. ASSERT if not found.
360 Status = gBS->LocateProtocol (&gHardwareInterruptProtocolGuid, NULL, (VOID **)&gInterrupt);
361 ASSERT_EFI_ERROR (Status);
362
1d5d0ae9 363 // Disable the timer
364 Status = TimerDriverSetTimerPeriod (&gTimer, 0);
365 ASSERT_EFI_ERROR (Status);
366
367 // Install interrupt handler
5cc45b70 368 gVector = PcdGet32(PcdSP804TimerPeriodicInterruptNum);
1d5d0ae9 369 Status = gInterrupt->RegisterInterruptSource (gInterrupt, gVector, TimerInterruptHandler);
370 ASSERT_EFI_ERROR (Status);
371
1d5d0ae9 372 // configure timer 0 for periodic operation, 32 bits, no prescaler, and interrupt enabled
5cc45b70 373 MmioWrite32 (SP804_TIMER_PERIODIC_BASE + SP804_TIMER_CONTROL_REG, SP804_TIMER_CTRL_PERIODIC | SP804_TIMER_CTRL_32BIT | SP804_PRESCALE_DIV_1 | SP804_TIMER_CTRL_INT_ENABLE);
1d5d0ae9 374
375 // Set up default timer
376 Status = TimerDriverSetTimerPeriod (&gTimer, FixedPcdGet32(PcdTimerPeriod)); // TIMER_DEFAULT_PERIOD
377 ASSERT_EFI_ERROR (Status);
378
379 // Install the Timer Architectural Protocol onto a new handle
380 Status = gBS->InstallMultipleProtocolInterfaces(
381 &Handle,
382 &gEfiTimerArchProtocolGuid, &gTimer,
383 NULL
384 );
385 ASSERT_EFI_ERROR(Status);
386
387 // Register for an ExitBootServicesEvent
388 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY, ExitBootServicesEvent, NULL, &EfiExitBootServicesEvent);
389 ASSERT_EFI_ERROR (Status);
390
391 return Status;
392}