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