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