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