a3f98646 |
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. |
36 | volatile EFI_TIMER_NOTIFY mTimerNotifyFunction = (EFI_TIMER_NOTIFY)NULL; |
37 | volatile EFI_PERIODIC_CALLBACK mTimerPeriodicCallback = (EFI_PERIODIC_CALLBACK)NULL; |
38 | |
39 | |
40 | // The current period of the timer interrupt |
41 | volatile UINT64 mTimerPeriod = 0; |
42 | |
43 | // Cached copy of the Hardware Interrupt protocol instance |
44 | EFI_HARDWARE_INTERRUPT_PROTOCOL *gInterrupt = NULL; |
45 | |
46 | // Cached registers |
47 | volatile UINT32 TISR; |
48 | volatile UINT32 TCLR; |
49 | volatile UINT32 TLDR; |
50 | volatile UINT32 TCRR; |
51 | volatile UINT32 TIER; |
52 | |
53 | // Cached interrupt vector |
54 | volatile UINTN gVector; |
55 | |
56 | |
026e30c4 |
57 | /** |
58 | |
59 | C Interrupt Handler calledin the interrupt context when Source interrupt is active. |
60 | |
61 | |
62 | |
63 | @param Source Source of the interrupt. Hardware routing off a specific platform defines |
64 | |
65 | what source means. |
66 | |
67 | @param SystemContext Pointer to system register context. Mostly used by debuggers and will |
68 | |
69 | update the system context after the return from the interrupt if |
70 | |
71 | modified. Don't change these values unless you know what you are doing |
72 | |
73 | |
74 | |
a3f98646 |
75 | **/ |
76 | VOID |
77 | EFIAPI |
78 | TimerInterruptHandler ( |
79 | IN HARDWARE_INTERRUPT_SOURCE Source, |
80 | IN EFI_SYSTEM_CONTEXT SystemContext |
81 | ) |
82 | { |
026e30c4 |
83 | EFI_TPL OriginalTPL; |
84 | |
85 | |
86 | |
87 | // |
88 | |
89 | // DXE core uses this callback for the EFI timer tick. The DXE core uses locks |
90 | |
91 | // that raise to TPL_HIGH and then restore back to current level. Thus we need |
92 | |
93 | // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick. |
94 | |
95 | // |
96 | |
97 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL); |
98 | |
a3f98646 |
99 | |
100 | if (mTimerPeriodicCallback) { |
101 | mTimerPeriodicCallback(SystemContext); |
102 | } |
103 | |
104 | if (mTimerNotifyFunction) { |
105 | mTimerNotifyFunction(mTimerPeriod); |
106 | } |
107 | |
108 | // Clear all timer interrupts |
026e30c4 |
109 | MmioWrite32 (TISR, TISR_CLEAR_ALL); |
a3f98646 |
110 | |
111 | // Poll interrupt status bits to ensure clearing |
112 | while ((MmioRead32(TISR) & TISR_ALL_INTERRUPT_MASK) != TISR_NO_INTERRUPTS_PENDING); |
113 | |
114 | gBS->RestoreTPL (OriginalTPL); |
115 | } |
116 | |
026e30c4 |
117 | /** |
118 | |
119 | This function registers the handler NotifyFunction so it is called every time |
120 | |
121 | the timer interrupt fires. It also passes the amount of time since the last |
122 | |
123 | handler call to the NotifyFunction. If NotifyFunction is NULL, then the |
124 | |
125 | handler is unregistered. If the handler is registered, then EFI_SUCCESS is |
126 | |
127 | returned. If the CPU does not support registering a timer interrupt handler, |
128 | |
129 | then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler |
130 | |
131 | when a handler is already registered, then EFI_ALREADY_STARTED is returned. |
132 | |
133 | If an attempt is made to unregister a handler when a handler is not registered, |
134 | |
135 | then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to |
136 | |
137 | register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR |
138 | |
139 | is returned. |
140 | |
141 | |
142 | |
143 | @param This The EFI_TIMER_ARCH_PROTOCOL instance. |
144 | |
145 | @param NotifyFunction The function to call when a timer interrupt fires. This |
146 | |
147 | function executes at TPL_HIGH_LEVEL. The DXE Core will |
148 | |
149 | register a handler for the timer interrupt, so it can know |
150 | |
151 | how much time has passed. This information is used to |
152 | |
153 | signal timer based events. NULL will unregister the handler. |
154 | |
155 | |
156 | |
157 | @retval EFI_SUCCESS The timer handler was registered. |
158 | |
159 | @retval EFI_UNSUPPORTED The platform does not support timer interrupts. |
160 | |
161 | @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already |
162 | |
163 | registered. |
164 | |
165 | @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not |
166 | |
167 | previously registered. |
168 | |
169 | @retval EFI_DEVICE_ERROR The timer handler could not be registered. |
170 | |
171 | |
172 | |
173 | **/ |
174 | |
a3f98646 |
175 | EFI_STATUS |
176 | EFIAPI |
177 | TimerDriverRegisterHandler ( |
178 | IN EFI_TIMER_ARCH_PROTOCOL *This, |
179 | IN EFI_TIMER_NOTIFY NotifyFunction |
180 | ) |
181 | { |
182 | if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) { |
183 | return EFI_INVALID_PARAMETER; |
184 | } |
185 | |
186 | if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) { |
187 | return EFI_ALREADY_STARTED; |
188 | } |
189 | |
190 | mTimerNotifyFunction = NotifyFunction; |
191 | |
192 | return EFI_SUCCESS; |
193 | } |
194 | |
026e30c4 |
195 | /** |
196 | |
197 | This function adjusts the period of timer interrupts to the value specified |
198 | |
199 | by TimerPeriod. If the timer period is updated, then the selected timer |
200 | |
201 | period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If |
202 | |
203 | the timer hardware is not programmable, then EFI_UNSUPPORTED is returned. |
204 | |
205 | If an error occurs while attempting to update the timer period, then the |
206 | |
207 | timer hardware will be put back in its state prior to this call, and |
208 | |
209 | EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt |
210 | |
211 | is disabled. This is not the same as disabling the CPU's interrupts. |
212 | |
213 | Instead, it must either turn off the timer hardware, or it must adjust the |
214 | |
215 | interrupt controller so that a CPU interrupt is not generated when the timer |
216 | |
217 | interrupt fires. |
218 | |
219 | |
220 | |
221 | @param This The EFI_TIMER_ARCH_PROTOCOL instance. |
222 | |
223 | @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If |
224 | |
225 | the timer hardware is not programmable, then EFI_UNSUPPORTED is |
226 | |
227 | returned. If the timer is programmable, then the timer period |
228 | |
229 | will be rounded up to the nearest timer period that is supported |
230 | |
231 | by the timer hardware. If TimerPeriod is set to 0, then the |
232 | |
233 | timer interrupts will be disabled. |
234 | |
235 | |
236 | |
237 | @retval EFI_SUCCESS The timer period was changed. |
238 | |
239 | @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt. |
240 | |
241 | @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error. |
242 | |
243 | |
244 | |
a3f98646 |
245 | **/ |
246 | EFI_STATUS |
247 | EFIAPI |
248 | TimerDriverSetTimerPeriod ( |
249 | IN EFI_TIMER_ARCH_PROTOCOL *This, |
250 | IN UINT64 TimerPeriod |
251 | ) |
252 | { |
253 | EFI_STATUS Status; |
254 | UINT64 TimerCount; |
255 | INT32 LoadValue; |
256 | |
257 | if (TimerPeriod == 0) { |
258 | // Turn off GPTIMER3 |
026e30c4 |
259 | MmioWrite32 (TCLR, TCLR_ST_OFF); |
a3f98646 |
260 | |
261 | Status = gInterrupt->DisableInterruptSource(gInterrupt, gVector); |
262 | } else { |
263 | // Calculate required timer count |
264 | TimerCount = DivU64x32(TimerPeriod * 100, PcdGet32(PcdEmbeddedFdPerformanceCounterPeriodInNanoseconds)); |
265 | |
266 | // Set GPTIMER3 Load register |
267 | LoadValue = (INT32) -TimerCount; |
026e30c4 |
268 | MmioWrite32 (TLDR, LoadValue); |
269 | MmioWrite32 (TCRR, LoadValue); |
a3f98646 |
270 | |
271 | // Enable Overflow interrupt |
026e30c4 |
272 | MmioWrite32 (TIER, TIER_TCAR_IT_DISABLE | TIER_OVF_IT_ENABLE | TIER_MAT_IT_DISABLE); |
a3f98646 |
273 | |
274 | // Turn on GPTIMER3, it will reload at overflow |
026e30c4 |
275 | MmioWrite32 (TCLR, TCLR_AR_AUTORELOAD | TCLR_ST_ON); |
a3f98646 |
276 | |
277 | Status = gInterrupt->EnableInterruptSource(gInterrupt, gVector); |
278 | } |
279 | |
280 | // |
281 | // Save the new timer period |
282 | // |
283 | mTimerPeriod = TimerPeriod; |
284 | return Status; |
285 | } |
286 | |
287 | |
026e30c4 |
288 | /** |
289 | |
290 | This function retrieves the period of timer interrupts in 100 ns units, |
291 | |
292 | returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod |
293 | |
294 | is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is |
295 | |
296 | returned, then the timer is currently disabled. |
297 | |
298 | |
299 | |
300 | @param This The EFI_TIMER_ARCH_PROTOCOL instance. |
301 | |
302 | @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If |
303 | |
304 | 0 is returned, then the timer is currently disabled. |
305 | |
306 | |
307 | |
308 | @retval EFI_SUCCESS The timer period was returned in TimerPeriod. |
309 | |
310 | @retval EFI_INVALID_PARAMETER TimerPeriod is NULL. |
311 | |
312 | |
313 | |
a3f98646 |
314 | **/ |
315 | EFI_STATUS |
316 | EFIAPI |
317 | TimerDriverGetTimerPeriod ( |
318 | IN EFI_TIMER_ARCH_PROTOCOL *This, |
319 | OUT UINT64 *TimerPeriod |
320 | ) |
321 | { |
322 | if (TimerPeriod == NULL) { |
323 | return EFI_INVALID_PARAMETER; |
324 | } |
325 | |
326 | *TimerPeriod = mTimerPeriod; |
327 | return EFI_SUCCESS; |
328 | } |
329 | |
026e30c4 |
330 | /** |
331 | |
332 | This function generates a soft timer interrupt. If the platform does not support soft |
333 | |
334 | timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned. |
335 | |
336 | If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler() |
337 | |
338 | service, then a soft timer interrupt will be generated. If the timer interrupt is |
339 | |
340 | enabled when this service is called, then the registered handler will be invoked. The |
341 | |
342 | registered handler should not be able to distinguish a hardware-generated timer |
343 | |
344 | interrupt from a software-generated timer interrupt. |
345 | |
346 | |
347 | |
348 | @param This The EFI_TIMER_ARCH_PROTOCOL instance. |
349 | |
350 | |
351 | |
352 | @retval EFI_SUCCESS The soft timer interrupt was generated. |
353 | |
354 | @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts. |
355 | |
356 | |
357 | |
a3f98646 |
358 | **/ |
359 | EFI_STATUS |
360 | EFIAPI |
361 | TimerDriverGenerateSoftInterrupt ( |
362 | IN EFI_TIMER_ARCH_PROTOCOL *This |
363 | ) |
364 | { |
365 | return EFI_UNSUPPORTED; |
366 | } |
367 | |
368 | |
369 | EFI_STATUS |
370 | EFIAPI |
371 | TimerDriverRegisterPeriodicCallback ( |
372 | IN TIMER_DEBUG_SUPPORT_PROTOCOL *This, |
373 | IN EFI_PERIODIC_CALLBACK PeriodicCallback |
374 | ) |
375 | { |
376 | if ((PeriodicCallback == NULL) && (mTimerPeriodicCallback == NULL)) { |
377 | return EFI_INVALID_PARAMETER; |
378 | } |
379 | |
380 | if ((PeriodicCallback != NULL) && (mTimerPeriodicCallback != NULL)) { |
381 | return EFI_ALREADY_STARTED; |
382 | } |
383 | |
384 | mTimerPeriodicCallback = PeriodicCallback; |
385 | |
386 | return EFI_SUCCESS; |
387 | } |
388 | |
389 | |
026e30c4 |
390 | /** |
391 | |
392 | Interface stucture for the Timer Architectural Protocol. |
393 | |
394 | |
395 | |
396 | @par Protocol Description: |
397 | |
398 | This protocol provides the services to initialize a periodic timer |
399 | |
400 | interrupt, and to register a handler that is called each time the timer |
401 | |
402 | interrupt fires. It may also provide a service to adjust the rate of the |
403 | |
404 | periodic timer interrupt. When a timer interrupt occurs, the handler is |
405 | |
406 | passed the amount of time that has passed since the previous timer |
407 | |
408 | interrupt. |
409 | |
410 | |
411 | |
412 | @param RegisterHandler |
413 | |
414 | Registers a handler that will be called each time the |
415 | |
416 | timer interrupt fires. TimerPeriod defines the minimum |
417 | |
418 | time between timer interrupts, so TimerPeriod will also |
419 | |
420 | be the minimum time between calls to the registered |
421 | |
422 | handler. |
423 | |
424 | |
425 | |
426 | @param SetTimerPeriod |
427 | |
428 | Sets the period of the timer interrupt in 100 nS units. |
429 | |
430 | This function is optional, and may return EFI_UNSUPPORTED. |
431 | |
432 | If this function is supported, then the timer period will |
433 | |
434 | be rounded up to the nearest supported timer period. |
435 | |
436 | |
437 | |
438 | @param GetTimerPeriod |
439 | |
440 | Retrieves the period of the timer interrupt in 100 nS units. |
441 | |
442 | |
443 | |
444 | @param GenerateSoftInterrupt |
445 | |
446 | Generates a soft timer interrupt that simulates the firing of |
447 | |
448 | the timer interrupt. This service can be used to invoke the |
449 | |
450 | registered handler if the timer interrupt has been masked for |
451 | |
452 | a period of time. |
453 | |
454 | |
455 | |
a3f98646 |
456 | **/ |
457 | EFI_TIMER_ARCH_PROTOCOL gTimer = { |
458 | TimerDriverRegisterHandler, |
459 | TimerDriverSetTimerPeriod, |
460 | TimerDriverGetTimerPeriod, |
461 | TimerDriverGenerateSoftInterrupt |
462 | }; |
463 | |
464 | TIMER_DEBUG_SUPPORT_PROTOCOL gTimerDebugSupport = { |
465 | TimerDriverRegisterPeriodicCallback |
466 | }; |
467 | |
468 | |
026e30c4 |
469 | /** |
470 | |
471 | Initialize the state information for the Timer Architectural Protocol and |
472 | |
473 | the Timer Debug support protocol that allows the debugger to break into a |
474 | |
475 | running program. |
476 | |
477 | |
478 | |
479 | @param ImageHandle of the loaded driver |
480 | |
481 | @param SystemTable Pointer to the System Table |
482 | |
483 | |
484 | |
485 | @retval EFI_SUCCESS Protocol registered |
486 | |
487 | @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure |
488 | |
489 | @retval EFI_DEVICE_ERROR Hardware problems |
490 | |
491 | |
492 | |
a3f98646 |
493 | **/ |
494 | EFI_STATUS |
495 | EFIAPI |
496 | TimerInitialize ( |
497 | IN EFI_HANDLE ImageHandle, |
498 | IN EFI_SYSTEM_TABLE *SystemTable |
499 | ) |
500 | { |
501 | EFI_HANDLE Handle = NULL; |
502 | EFI_STATUS Status; |
503 | UINT32 TimerBaseAddress; |
504 | |
505 | // Find the interrupt controller protocol. ASSERT if not found. |
506 | Status = gBS->LocateProtocol(&gHardwareInterruptProtocolGuid, NULL, (VOID **)&gInterrupt); |
507 | ASSERT_EFI_ERROR (Status); |
508 | |
509 | // Set up the timer registers |
510 | TimerBaseAddress = TimerBase(FixedPcdGet32(PcdBeagleArchTimer)); |
511 | TISR = TimerBaseAddress + GPTIMER_TISR; |
512 | TCLR = TimerBaseAddress + GPTIMER_TCLR; |
513 | TLDR = TimerBaseAddress + GPTIMER_TLDR; |
514 | TCRR = TimerBaseAddress + GPTIMER_TCRR; |
515 | TIER = TimerBaseAddress + GPTIMER_TIER; |
516 | |
517 | // Disable the timer |
518 | Status = TimerDriverSetTimerPeriod(&gTimer, 0); |
519 | ASSERT_EFI_ERROR (Status); |
520 | |
521 | // Install interrupt handler |
522 | gVector = InterruptVectorForTimer(FixedPcdGet32(PcdBeagleArchTimer)); |
523 | Status = gInterrupt->RegisterInterruptSource(gInterrupt, gVector, TimerInterruptHandler); |
524 | ASSERT_EFI_ERROR (Status); |
525 | |
526 | // Set up default timer |
527 | Status = TimerDriverSetTimerPeriod(&gTimer, FixedPcdGet32(PcdTimerPeriod)); |
528 | ASSERT_EFI_ERROR (Status); |
529 | |
530 | // Install the Timer Architectural Protocol onto a new handle |
531 | Status = gBS->InstallMultipleProtocolInterfaces(&Handle, |
532 | &gEfiTimerArchProtocolGuid, &gTimer, |
533 | &gTimerDebugSupportProtocolGuid, &gTimerDebugSupport, |
534 | NULL); |
535 | ASSERT_EFI_ERROR(Status); |
536 | |
537 | return Status; |
538 | } |
539 | |