]>
Commit | Line | Data |
---|---|---|
a3f98646 | 1 | /** @file\r |
2 | \r | |
3d70643b | 3 | Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r |
3402aac7 | 4 | \r |
538311f7 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
a3f98646 | 6 | \r |
7 | **/\r | |
8 | \r | |
570d030a | 9 | #include <Uefi.h>\r |
a3f98646 | 10 | \r |
11 | #include <Library/BaseLib.h>\r | |
12 | #include <Library/TimerLib.h>\r | |
13 | #include <Library/DebugLib.h>\r | |
14 | #include <Library/PcdLib.h>\r | |
15 | #include <Library/IoLib.h>\r | |
16 | #include <Library/OmapLib.h>\r | |
17 | \r | |
18 | #include <Omap3530/Omap3530.h>\r | |
19 | \r | |
570d030a | 20 | RETURN_STATUS\r |
21 | EFIAPI\r | |
22 | TimerConstructor (\r | |
23 | VOID\r | |
24 | )\r | |
25 | {\r | |
26 | UINTN Timer = PcdGet32(PcdOmap35xxFreeTimer);\r | |
27 | UINT32 TimerBaseAddress = TimerBase(Timer);\r | |
28 | \r | |
29 | if ((MmioRead32 (TimerBaseAddress + GPTIMER_TCLR) & TCLR_ST_ON) == 0) {\r | |
30 | // Set source clock for GPT3 & GPT4 to SYS_CLK\r | |
31 | MmioOr32 (CM_CLKSEL_PER, CM_CLKSEL_PER_CLKSEL_GPT3_SYS | CM_CLKSEL_PER_CLKSEL_GPT4_SYS);\r | |
32 | \r | |
33 | // Set count & reload registers\r | |
34 | MmioWrite32 (TimerBaseAddress + GPTIMER_TCRR, 0x00000000);\r | |
35 | MmioWrite32 (TimerBaseAddress + GPTIMER_TLDR, 0x00000000);\r | |
36 | \r | |
37 | // Disable interrupts\r | |
38 | MmioWrite32 (TimerBaseAddress + GPTIMER_TIER, TIER_TCAR_IT_DISABLE | TIER_OVF_IT_DISABLE | TIER_MAT_IT_DISABLE);\r | |
39 | \r | |
40 | // Start Timer\r | |
41 | MmioWrite32 (TimerBaseAddress + GPTIMER_TCLR, TCLR_AR_AUTORELOAD | TCLR_ST_ON);\r | |
42 | \r | |
43 | // Disable OMAP Watchdog timer (WDT2)\r | |
44 | MmioWrite32 (WDTIMER2_BASE + WSPR, 0xAAAA);\r | |
45 | DEBUG ((EFI_D_ERROR, "Magic delay to disable watchdog timers properly.\n"));\r | |
46 | MmioWrite32 (WDTIMER2_BASE + WSPR, 0x5555);\r | |
47 | }\r | |
48 | return EFI_SUCCESS;\r | |
49 | }\r | |
50 | \r | |
a3f98646 | 51 | UINTN\r |
52 | EFIAPI\r | |
53 | MicroSecondDelay (\r | |
54 | IN UINTN MicroSeconds\r | |
55 | )\r | |
56 | {\r | |
57 | UINT64 NanoSeconds;\r | |
3402aac7 | 58 | \r |
a3f98646 | 59 | NanoSeconds = MultU64x32(MicroSeconds, 1000);\r |
60 | \r | |
3402aac7 | 61 | while (NanoSeconds > (UINTN)-1) {\r |
a3f98646 | 62 | NanoSecondDelay((UINTN)-1);\r |
63 | NanoSeconds -= (UINTN)-1;\r | |
64 | }\r | |
65 | \r | |
66 | NanoSecondDelay(NanoSeconds);\r | |
67 | \r | |
68 | return MicroSeconds;\r | |
69 | }\r | |
70 | \r | |
71 | UINTN\r | |
72 | EFIAPI\r | |
73 | NanoSecondDelay (\r | |
74 | IN UINTN NanoSeconds\r | |
75 | )\r | |
76 | {\r | |
77 | UINT32 Delay;\r | |
78 | UINT32 StartTime;\r | |
79 | UINT32 CurrentTime;\r | |
80 | UINT32 ElapsedTime;\r | |
81 | UINT32 TimerCountRegister;\r | |
82 | \r | |
55bff42e | 83 | Delay = (NanoSeconds / PcdGet32(PcdEmbeddedPerformanceCounterPeriodInNanoseconds)) + 1;\r |
3402aac7 | 84 | \r |
43263288 | 85 | TimerCountRegister = TimerBase(PcdGet32(PcdOmap35xxFreeTimer)) + GPTIMER_TCRR;\r |
a3f98646 | 86 | \r |
43263288 | 87 | StartTime = MmioRead32 (TimerCountRegister);\r |
a3f98646 | 88 | \r |
3402aac7 | 89 | do\r |
a3f98646 | 90 | {\r |
43263288 | 91 | CurrentTime = MmioRead32 (TimerCountRegister);\r |
a3f98646 | 92 | ElapsedTime = CurrentTime - StartTime;\r |
93 | } while (ElapsedTime < Delay);\r | |
94 | \r | |
55bff42e | 95 | NanoSeconds = ElapsedTime * PcdGet32(PcdEmbeddedPerformanceCounterPeriodInNanoseconds);\r |
a3f98646 | 96 | \r |
97 | return NanoSeconds;\r | |
98 | }\r | |
99 | \r | |
100 | UINT64\r | |
101 | EFIAPI\r | |
102 | GetPerformanceCounter (\r | |
103 | VOID\r | |
104 | )\r | |
3402aac7 | 105 | {\r |
43263288 | 106 | return (UINT64)MmioRead32 (TimerBase(PcdGet32(PcdOmap35xxFreeTimer)) + GPTIMER_TCRR);\r |
a3f98646 | 107 | }\r |
108 | \r | |
109 | UINT64\r | |
110 | EFIAPI\r | |
111 | GetPerformanceCounterProperties (\r | |
112 | OUT UINT64 *StartValue, OPTIONAL\r | |
113 | OUT UINT64 *EndValue OPTIONAL\r | |
114 | )\r | |
115 | {\r | |
116 | if (StartValue != NULL) {\r | |
117 | // Timer starts with the reload value\r | |
43263288 | 118 | *StartValue = (UINT64)MmioRead32 (TimerBase(PcdGet32(PcdOmap35xxFreeTimer)) + GPTIMER_TLDR);\r |
a3f98646 | 119 | }\r |
3402aac7 | 120 | \r |
a3f98646 | 121 | if (EndValue != NULL) {\r |
122 | // Timer counts up to 0xFFFFFFFF\r | |
123 | *EndValue = 0xFFFFFFFF;\r | |
124 | }\r | |
3402aac7 | 125 | \r |
55bff42e | 126 | return PcdGet64(PcdEmbeddedPerformanceCounterFrequencyInHz);\r |
a3f98646 | 127 | }\r |
7d976f27 LL |
128 | \r |
129 | /**\r | |
130 | Converts elapsed ticks of performance counter to time in nanoseconds.\r | |
131 | \r | |
132 | This function converts the elapsed ticks of running performance counter to\r | |
133 | time value in unit of nanoseconds.\r | |
134 | \r | |
135 | @param Ticks The number of elapsed ticks of running performance counter.\r | |
136 | \r | |
137 | @return The elapsed time in nanoseconds.\r | |
138 | \r | |
139 | **/\r | |
140 | UINT64\r | |
141 | EFIAPI\r | |
142 | GetTimeInNanoSecond (\r | |
143 | IN UINT64 Ticks\r | |
144 | )\r | |
145 | {\r | |
146 | UINT32 Period;\r | |
147 | \r | |
148 | Period = PcdGet32 (PcdEmbeddedPerformanceCounterPeriodInNanoseconds);\r | |
149 | \r | |
150 | return (Ticks * Period);\r | |
151 | }\r |