]> git.proxmox.com Git - mirror_edk2.git/blob - ArmEbPkg/TimerDxe/Timer.c
Get TimerDxe driver compiling.
[mirror_edk2.git] / ArmEbPkg / TimerDxe / 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.<BR>
5
6 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 #include <ArmEb/ArmEb.h>
31
32
33 // The notification function to call on every timer interrupt.
34 volatile EFI_TIMER_NOTIFY mTimerNotifyFunction = (EFI_TIMER_NOTIFY)NULL;
35
36
37 // The current period of the timer interrupt
38 volatile UINT64 mTimerPeriod = 0;
39
40 // Cached copy of the Hardware Interrupt protocol instance
41 EFI_HARDWARE_INTERRUPT_PROTOCOL *gInterrupt = NULL;
42
43 // Cached interrupt vector
44 UINTN gVector;
45
46 UINT32 mLastTickCount;
47
48
49 /**
50
51 C Interrupt Handler calledin the interrupt context when Source interrupt is active.
52
53
54 @param Source Source of the interrupt. Hardware routing off a specific platform defines
55 what source means.
56
57 @param SystemContext Pointer to system register context. Mostly used by debuggers and will
58 update the system context after the return from the interrupt if
59 modified. Don't change these values unless you know what you are doing
60
61 **/
62 VOID
63 EFIAPI
64 TimerInterruptHandler (
65 IN HARDWARE_INTERRUPT_SOURCE Source,
66 IN EFI_SYSTEM_CONTEXT SystemContext
67 )
68 {
69 EFI_TPL OriginalTPL;
70
71 //
72 // DXE core uses this callback for the EFI timer tick. The DXE core uses locks
73 // that raise to TPL_HIGH and then restore back to current level. Thus we need
74 // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick.
75 //
76 OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
77
78 // clear the periodic interrupt
79 MmioWrite32 (EB_SP804_TIMER0_BASE + SP804_TIMER_INT_CLR_REG, 0);
80
81 // signal end of interrupt early to help avoid losing subsequent ticks from long duration handlers
82 gInterrupt->EndOfInterrupt (gInterrupt, Source);
83
84 if (mTimerNotifyFunction) {
85 mTimerNotifyFunction (mTimerPeriod);
86 }
87
88 gBS->RestoreTPL (OriginalTPL);
89 }
90
91
92 /**
93 This function registers the handler NotifyFunction so it is called every time
94 the timer interrupt fires. It also passes the amount of time since the last
95 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
96 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
97 returned. If the CPU does not support registering a timer interrupt handler,
98 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
99 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
100 If an attempt is made to unregister a handler when a handler is not registered,
101 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
102 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
103 is returned.
104
105 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
106 @param NotifyFunction The function to call when a timer interrupt fires. This
107 function executes at TPL_HIGH_LEVEL. The DXE Core will
108 register a handler for the timer interrupt, so it can know
109 how much time has passed. This information is used to
110 signal timer based events. NULL will unregister the handler.
111 @retval EFI_SUCCESS The timer handler was registered.
112 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
113 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
114 registered.
115 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
116 previously registered.
117 @retval EFI_DEVICE_ERROR The timer handler could not be registered.
118
119 **/
120 EFI_STATUS
121 EFIAPI
122 TimerDriverRegisterHandler (
123 IN EFI_TIMER_ARCH_PROTOCOL *This,
124 IN EFI_TIMER_NOTIFY NotifyFunction
125 )
126 {
127 if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) {
128 return EFI_INVALID_PARAMETER;
129 }
130
131 if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) {
132 return EFI_ALREADY_STARTED;
133 }
134
135 mTimerNotifyFunction = NotifyFunction;
136
137 return EFI_SUCCESS;
138 }
139
140 /**
141
142 This function adjusts the period of timer interrupts to the value specified
143 by TimerPeriod. If the timer period is updated, then the selected timer
144 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
145 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
146 If an error occurs while attempting to update the timer period, then the
147 timer hardware will be put back in its state prior to this call, and
148 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
149 is disabled. This is not the same as disabling the CPU's interrupts.
150 Instead, it must either turn off the timer hardware, or it must adjust the
151 interrupt controller so that a CPU interrupt is not generated when the timer
152 interrupt fires.
153
154 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
155 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
156 the timer hardware is not programmable, then EFI_UNSUPPORTED is
157 returned. If the timer is programmable, then the timer period
158 will be rounded up to the nearest timer period that is supported
159 by the timer hardware. If TimerPeriod is set to 0, then the
160 timer interrupts will be disabled.
161
162
163 @retval EFI_SUCCESS The timer period was changed.
164 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
165 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
166
167 **/
168 EFI_STATUS
169 EFIAPI
170 TimerDriverSetTimerPeriod (
171 IN EFI_TIMER_ARCH_PROTOCOL *This,
172 IN UINT64 TimerPeriod
173 )
174 {
175 EFI_STATUS Status;
176 UINT64 TimerTicks;
177
178 // always disable the timer
179 MmioAnd32 (EB_SP804_TIMER0_BASE + SP804_TIMER_CONTROL_REG, ~SP804_TIMER_CTRL_ENABLE);
180
181 if (TimerPeriod == 0) {
182 // leave timer disabled from above, and...
183 // disable timer 0/1 interrupt for a TimerPeriod of 0
184
185 Status = gInterrupt->DisableInterruptSource (gInterrupt, gVector);
186 } else {
187 // Convert TimerPeriod into 1MHz clock counts (us units = 100ns units / 10)
188 TimerTicks = DivU64x32 (TimerPeriod, 10);
189
190 // if it's larger than 32-bits, pin to highest value
191 if (TimerTicks > 0xffffffff) {
192 TimerTicks = 0xffffffff;
193 }
194
195 // Program the SP804 timer with the new count value
196 MmioWrite32 (EB_SP804_TIMER0_BASE + SP804_TIMER_LOAD_REG, TimerTicks);
197
198 // enable the timer
199 MmioOr32 (EB_SP804_TIMER0_BASE + SP804_TIMER_CONTROL_REG, SP804_TIMER_CTRL_ENABLE);
200
201 // enable timer 0/1 interrupts
202 Status = gInterrupt->EnableInterruptSource (gInterrupt, gVector);
203 }
204
205 //
206 // Save the new timer period
207 //
208 mTimerPeriod = TimerPeriod;
209 return Status;
210 }
211
212
213 /**
214 This function retrieves the period of timer interrupts in 100 ns units,
215 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
216 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
217 returned, then the timer is currently disabled.
218
219 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
220 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
221 0 is returned, then the timer is currently disabled.
222
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 This function generates a soft timer interrupt. If the platform does not support soft
245 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
246 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
247 service, then a soft timer interrupt will be generated. If the timer interrupt is
248 enabled when this service is called, then the registered handler will be invoked. The
249 registered handler should not be able to distinguish a hardware-generated timer
250 interrupt from a software-generated timer interrupt.
251
252 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
253
254 @retval EFI_SUCCESS The soft timer interrupt was generated.
255 @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
256
257 **/
258 EFI_STATUS
259 EFIAPI
260 TimerDriverGenerateSoftInterrupt (
261 IN EFI_TIMER_ARCH_PROTOCOL *This
262 )
263 {
264 return EFI_UNSUPPORTED;
265 }
266
267
268
269 /**
270 Interface stucture for the Timer Architectural Protocol.
271
272 @par Protocol Description:
273 This protocol provides the services to initialize a periodic timer
274 interrupt, and to register a handler that is called each time the timer
275 interrupt fires. It may also provide a service to adjust the rate of the
276 periodic timer interrupt. When a timer interrupt occurs, the handler is
277 passed the amount of time that has passed since the previous timer
278 interrupt.
279
280 @param RegisterHandler
281 Registers a handler that will be called each time the
282 timer interrupt fires. TimerPeriod defines the minimum
283 time between timer interrupts, so TimerPeriod will also
284 be the minimum time between calls to the registered
285 handler.
286
287 @param SetTimerPeriod
288 Sets the period of the timer interrupt in 100 nS units.
289 This function is optional, and may return EFI_UNSUPPORTED.
290 If this function is supported, then the timer period will
291 be rounded up to the nearest supported timer period.
292
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 registered handler if the timer interrupt has been masked for
300 a period of time.
301
302 **/
303 EFI_TIMER_ARCH_PROTOCOL gTimer = {
304 TimerDriverRegisterHandler,
305 TimerDriverSetTimerPeriod,
306 TimerDriverGetTimerPeriod,
307 TimerDriverGenerateSoftInterrupt
308 };
309
310
311 /**
312 Initialize the state information for the Timer Architectural Protocol and
313 the Timer Debug support protocol that allows the debugger to break into a
314 running program.
315
316 @param ImageHandle of the loaded driver
317 @param SystemTable Pointer to the System Table
318
319 @retval EFI_SUCCESS Protocol registered
320 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
321 @retval EFI_DEVICE_ERROR Hardware problems
322
323 **/
324 EFI_STATUS
325 EFIAPI
326 TimerInitialize (
327 IN EFI_HANDLE ImageHandle,
328 IN EFI_SYSTEM_TABLE *SystemTable
329 )
330 {
331 EFI_HANDLE Handle = NULL;
332 EFI_STATUS Status;
333 UINT32 TimerBaseAddress;
334
335 // configure free running timer (TIMER1) for 1MHz operation
336
337 // AND disable clock, OR configure 1MHz clock
338 MmioAndThenOr32 (EB_SP810_CTRL_BASE + SP810_SYS_CTRL_REG, ~SP810_SYS_CTRL_TIMER1_EN, SP810_SYS_CTRL_TIMER1_TIMCLK);
339
340 // Renable timer
341 MmioOr32 (EB_SP810_CTRL_BASE + SP810_SYS_CTRL_REG, SP810_SYS_CTRL_TIMER1_EN);
342
343
344 // configure timer 1 for free running operation, 32 bits, no prescaler, interrupt disabled
345 MmioWrite32 (EB_SP804_TIMER1_BASE + SP804_TIMER_CONTROL_REG, SP804_TIMER_CTRL_32BIT | SP804_PRESCALE_DIV_1);
346
347 // enable the free running timer
348 MmioOr32 (EB_SP804_TIMER1_BASE + SP804_TIMER_CONTROL_REG, SP804_TIMER_CTRL_ENABLE);
349
350 // record free running tick value (should be close to 0xffffffff)
351 mLastTickCount = MmioRead32 (EB_SP804_TIMER1_BASE + SP804_TIMER_CURRENT_REG);
352
353 // Disable the timer
354 Status = TimerDriverSetTimerPeriod (&gTimer, 0);
355 ASSERT_EFI_ERROR (Status);
356
357 // Install interrupt handler
358 gVector = EB_TIMER01_INTERRUPT_NUM;
359 Status = gInterrupt->RegisterInterruptSource (gInterrupt, gVector, TimerInterruptHandler);
360 ASSERT_EFI_ERROR (Status);
361
362
363 // configure periodic timer (TIMER0) for 1MHz operation
364 MmioAndThenOr32 (EB_SP810_CTRL_BASE + SP810_SYS_CTRL_REG, ~SP810_SYS_CTRL_TIMER0_EN, SP810_SYS_CTRL_TIMER0_TIMCLK);
365
366 // Renable timer
367 MmioOr32 (EB_SP810_CTRL_BASE + SP810_SYS_CTRL_REG, SP810_SYS_CTRL_TIMER0_EN);
368
369 // configure timer 0 for periodic operation, 32 bits, no prescaler, and interrupt enabled
370 MmioWrite32 (EB_SP804_TIMER0_BASE + SP804_TIMER_CONTROL_REG, SP804_TIMER_CTRL_PERIODIC | SP804_TIMER_CTRL_32BIT | SP804_PRESCALE_DIV_1 | SP804_TIMER_CTRL_INT_ENABLE);
371
372
373 // Set up default timer
374 Status = TimerDriverSetTimerPeriod (&gTimer, FixedPcdGet32(PcdTimerPeriod)); // TIMER_DEFAULT_PERIOD
375 ASSERT_EFI_ERROR (Status);
376
377 // Install the Timer Architectural Protocol onto a new handle
378 Status = gBS->InstallMultipleProtocolInterfaces(
379 &Handle,
380 &gEfiTimerArchProtocolGuid, &gTimer,
381 NULL
382 );
383 ASSERT_EFI_ERROR(Status);
384
385 return Status;
386 }
387