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