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