3 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/BaseLib.h>
12 #include <Library/TimerLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/PcdLib.h>
15 #include <Library/IoLib.h>
16 #include <Library/OmapLib.h>
18 #include <Omap3530/Omap3530.h>
26 UINTN Timer
= PcdGet32(PcdOmap35xxFreeTimer
);
27 UINT32 TimerBaseAddress
= TimerBase(Timer
);
29 if ((MmioRead32 (TimerBaseAddress
+ GPTIMER_TCLR
) & TCLR_ST_ON
) == 0) {
30 // Set source clock for GPT3 & GPT4 to SYS_CLK
31 MmioOr32 (CM_CLKSEL_PER
, CM_CLKSEL_PER_CLKSEL_GPT3_SYS
| CM_CLKSEL_PER_CLKSEL_GPT4_SYS
);
33 // Set count & reload registers
34 MmioWrite32 (TimerBaseAddress
+ GPTIMER_TCRR
, 0x00000000);
35 MmioWrite32 (TimerBaseAddress
+ GPTIMER_TLDR
, 0x00000000);
38 MmioWrite32 (TimerBaseAddress
+ GPTIMER_TIER
, TIER_TCAR_IT_DISABLE
| TIER_OVF_IT_DISABLE
| TIER_MAT_IT_DISABLE
);
41 MmioWrite32 (TimerBaseAddress
+ GPTIMER_TCLR
, TCLR_AR_AUTORELOAD
| TCLR_ST_ON
);
43 // Disable OMAP Watchdog timer (WDT2)
44 MmioWrite32 (WDTIMER2_BASE
+ WSPR
, 0xAAAA);
45 DEBUG ((EFI_D_ERROR
, "Magic delay to disable watchdog timers properly.\n"));
46 MmioWrite32 (WDTIMER2_BASE
+ WSPR
, 0x5555);
59 NanoSeconds
= MultU64x32(MicroSeconds
, 1000);
61 while (NanoSeconds
> (UINTN
)-1) {
62 NanoSecondDelay((UINTN
)-1);
63 NanoSeconds
-= (UINTN
)-1;
66 NanoSecondDelay(NanoSeconds
);
81 UINT32 TimerCountRegister
;
83 Delay
= (NanoSeconds
/ PcdGet32(PcdEmbeddedPerformanceCounterPeriodInNanoseconds
)) + 1;
85 TimerCountRegister
= TimerBase(PcdGet32(PcdOmap35xxFreeTimer
)) + GPTIMER_TCRR
;
87 StartTime
= MmioRead32 (TimerCountRegister
);
91 CurrentTime
= MmioRead32 (TimerCountRegister
);
92 ElapsedTime
= CurrentTime
- StartTime
;
93 } while (ElapsedTime
< Delay
);
95 NanoSeconds
= ElapsedTime
* PcdGet32(PcdEmbeddedPerformanceCounterPeriodInNanoseconds
);
102 GetPerformanceCounter (
106 return (UINT64
)MmioRead32 (TimerBase(PcdGet32(PcdOmap35xxFreeTimer
)) + GPTIMER_TCRR
);
111 GetPerformanceCounterProperties (
112 OUT UINT64
*StartValue
, OPTIONAL
113 OUT UINT64
*EndValue OPTIONAL
116 if (StartValue
!= NULL
) {
117 // Timer starts with the reload value
118 *StartValue
= (UINT64
)MmioRead32 (TimerBase(PcdGet32(PcdOmap35xxFreeTimer
)) + GPTIMER_TLDR
);
121 if (EndValue
!= NULL
) {
122 // Timer counts up to 0xFFFFFFFF
123 *EndValue
= 0xFFFFFFFF;
126 return PcdGet64(PcdEmbeddedPerformanceCounterFrequencyInHz
);
130 Converts elapsed ticks of performance counter to time in nanoseconds.
132 This function converts the elapsed ticks of running performance counter to
133 time value in unit of nanoseconds.
135 @param Ticks The number of elapsed ticks of running performance counter.
137 @return The elapsed time in nanoseconds.
142 GetTimeInNanoSecond (
148 Period
= PcdGet32 (PcdEmbeddedPerformanceCounterPeriodInNanoseconds
);
150 return (Ticks
* Period
);