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