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