]> git.proxmox.com Git - mirror_edk2.git/blob - Omap35xxPkg/Library/Omap35xxTimerLib/TimerLib.c
a69cad83a9293a08b7ff134e57794ee4e38ee081
[mirror_edk2.git] / Omap35xxPkg / Library / Omap35xxTimerLib / TimerLib.c
1 /** @file
2
3 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
4
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Uefi.h>
10
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>
17
18 #include <Omap3530/Omap3530.h>
19
20 RETURN_STATUS
21 EFIAPI
22 TimerConstructor (
23 VOID
24 )
25 {
26 UINTN Timer = PcdGet32(PcdOmap35xxFreeTimer);
27 UINT32 TimerBaseAddress = TimerBase(Timer);
28
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);
32
33 // Set count & reload registers
34 MmioWrite32 (TimerBaseAddress + GPTIMER_TCRR, 0x00000000);
35 MmioWrite32 (TimerBaseAddress + GPTIMER_TLDR, 0x00000000);
36
37 // Disable interrupts
38 MmioWrite32 (TimerBaseAddress + GPTIMER_TIER, TIER_TCAR_IT_DISABLE | TIER_OVF_IT_DISABLE | TIER_MAT_IT_DISABLE);
39
40 // Start Timer
41 MmioWrite32 (TimerBaseAddress + GPTIMER_TCLR, TCLR_AR_AUTORELOAD | TCLR_ST_ON);
42
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);
47 }
48 return EFI_SUCCESS;
49 }
50
51 UINTN
52 EFIAPI
53 MicroSecondDelay (
54 IN UINTN MicroSeconds
55 )
56 {
57 UINT64 NanoSeconds;
58
59 NanoSeconds = MultU64x32(MicroSeconds, 1000);
60
61 while (NanoSeconds > (UINTN)-1) {
62 NanoSecondDelay((UINTN)-1);
63 NanoSeconds -= (UINTN)-1;
64 }
65
66 NanoSecondDelay(NanoSeconds);
67
68 return MicroSeconds;
69 }
70
71 UINTN
72 EFIAPI
73 NanoSecondDelay (
74 IN UINTN NanoSeconds
75 )
76 {
77 UINT32 Delay;
78 UINT32 StartTime;
79 UINT32 CurrentTime;
80 UINT32 ElapsedTime;
81 UINT32 TimerCountRegister;
82
83 Delay = (NanoSeconds / PcdGet32(PcdEmbeddedPerformanceCounterPeriodInNanoseconds)) + 1;
84
85 TimerCountRegister = TimerBase(PcdGet32(PcdOmap35xxFreeTimer)) + GPTIMER_TCRR;
86
87 StartTime = MmioRead32 (TimerCountRegister);
88
89 do
90 {
91 CurrentTime = MmioRead32 (TimerCountRegister);
92 ElapsedTime = CurrentTime - StartTime;
93 } while (ElapsedTime < Delay);
94
95 NanoSeconds = ElapsedTime * PcdGet32(PcdEmbeddedPerformanceCounterPeriodInNanoseconds);
96
97 return NanoSeconds;
98 }
99
100 UINT64
101 EFIAPI
102 GetPerformanceCounter (
103 VOID
104 )
105 {
106 return (UINT64)MmioRead32 (TimerBase(PcdGet32(PcdOmap35xxFreeTimer)) + GPTIMER_TCRR);
107 }
108
109 UINT64
110 EFIAPI
111 GetPerformanceCounterProperties (
112 OUT UINT64 *StartValue, OPTIONAL
113 OUT UINT64 *EndValue OPTIONAL
114 )
115 {
116 if (StartValue != NULL) {
117 // Timer starts with the reload value
118 *StartValue = (UINT64)MmioRead32 (TimerBase(PcdGet32(PcdOmap35xxFreeTimer)) + GPTIMER_TLDR);
119 }
120
121 if (EndValue != NULL) {
122 // Timer counts up to 0xFFFFFFFF
123 *EndValue = 0xFFFFFFFF;
124 }
125
126 return PcdGet64(PcdEmbeddedPerformanceCounterFrequencyInHz);
127 }
128
129 /**
130 Converts elapsed ticks of performance counter to time in nanoseconds.
131
132 This function converts the elapsed ticks of running performance counter to
133 time value in unit of nanoseconds.
134
135 @param Ticks The number of elapsed ticks of running performance counter.
136
137 @return The elapsed time in nanoseconds.
138
139 **/
140 UINT64
141 EFIAPI
142 GetTimeInNanoSecond (
143 IN UINT64 Ticks
144 )
145 {
146 UINT32 Period;
147
148 Period = PcdGet32 (PcdEmbeddedPerformanceCounterPeriodInNanoseconds);
149
150 return (Ticks * Period);
151 }