]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ArmPlatformPkg / Library / PL031RealTimeClockLib / PL031RealTimeClockLib.c
1 /** @file
2 Implement EFI RealTimeClock runtime services via RTC Lib.
3
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2011 - 2020, Arm Limited. All rights reserved.<BR>
6 Copyright (c) 2019, Linaro Ltd. All rights reserved.<BR>
7
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #include <PiDxe.h>
13
14 #include <Guid/EventGroup.h>
15 #include <Guid/GlobalVariable.h>
16
17 #include <Library/BaseLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/DxeServicesTableLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/PcdLib.h>
23 #include <Library/RealTimeClockLib.h>
24 #include <Library/TimeBaseLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/UefiLib.h>
27 #include <Library/UefiRuntimeServicesTableLib.h>
28 #include <Library/UefiRuntimeLib.h>
29
30 #include <Protocol/RealTimeClock.h>
31
32 #include "PL031RealTimeClock.h"
33
34 STATIC BOOLEAN mPL031Initialized = FALSE;
35 STATIC EFI_EVENT mRtcVirtualAddrChangeEvent;
36 STATIC UINTN mPL031RtcBase;
37
38 EFI_STATUS
39 IdentifyPL031 (
40 VOID
41 )
42 {
43 EFI_STATUS Status;
44
45 // Check if this is a PrimeCell Peripheral
46 if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID0) != 0x0D)
47 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID1) != 0xF0)
48 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID2) != 0x05)
49 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID3) != 0xB1))
50 {
51 Status = EFI_NOT_FOUND;
52 goto EXIT;
53 }
54
55 // Check if this PrimeCell Peripheral is the PL031 Real Time Clock
56 if ( (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID0) != 0x31)
57 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID1) != 0x10)
58 || ((MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID2) & 0xF) != 0x04)
59 || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID3) != 0x00))
60 {
61 Status = EFI_NOT_FOUND;
62 goto EXIT;
63 }
64
65 Status = EFI_SUCCESS;
66
67 EXIT:
68 return Status;
69 }
70
71 EFI_STATUS
72 InitializePL031 (
73 VOID
74 )
75 {
76 EFI_STATUS Status;
77
78 // Prepare the hardware
79 Status = IdentifyPL031 ();
80 if (EFI_ERROR (Status)) {
81 goto EXIT;
82 }
83
84 // Ensure interrupts are masked. We do not want RTC interrupts in UEFI
85 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER) & PL031_SET_IRQ_MASK) != 0) {
86 MmioWrite32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER, 0);
87 }
88
89 // Clear any existing interrupts
90 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER) & PL031_IRQ_TRIGGERED) == PL031_IRQ_TRIGGERED) {
91 MmioOr32 (mPL031RtcBase + PL031_RTC_ICR_IRQ_CLEAR_REGISTER, PL031_CLEAR_IRQ);
92 }
93
94 // Start the clock counter
95 if ((MmioRead32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER) & PL031_RTC_ENABLED) != PL031_RTC_ENABLED) {
96 MmioOr32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER, PL031_RTC_ENABLED);
97 }
98
99 mPL031Initialized = TRUE;
100
101 EXIT:
102 return Status;
103 }
104
105 /**
106 Returns the current time and date information, and the time-keeping capabilities
107 of the hardware platform.
108
109 @param Time A pointer to storage to receive a snapshot of the current time.
110 @param Capabilities An optional pointer to a buffer to receive the real time clock
111 device's capabilities.
112
113 @retval EFI_SUCCESS The operation completed successfully.
114 @retval EFI_INVALID_PARAMETER Time is NULL.
115 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
116 @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.
117
118 **/
119 EFI_STATUS
120 EFIAPI
121 LibGetTime (
122 OUT EFI_TIME *Time,
123 OUT EFI_TIME_CAPABILITIES *Capabilities
124 )
125 {
126 EFI_STATUS Status;
127 UINT32 EpochSeconds;
128
129 // Ensure Time is a valid pointer
130 if (Time == NULL) {
131 return EFI_INVALID_PARAMETER;
132 }
133
134 // Initialize the hardware if not already done
135 if (!mPL031Initialized) {
136 Status = InitializePL031 ();
137 if (EFI_ERROR (Status)) {
138 return Status;
139 }
140 }
141
142 EpochSeconds = MmioRead32 (mPL031RtcBase + PL031_RTC_DR_DATA_REGISTER);
143
144 // Adjust for the correct time zone
145 // The timezone setting also reflects the DST setting of the clock
146 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
147 EpochSeconds += Time->TimeZone * SEC_PER_MIN;
148 } else if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
149 // Convert to adjusted time, i.e. spring forwards one hour
150 EpochSeconds += SEC_PER_HOUR;
151 }
152
153 // Convert from internal 32-bit time to UEFI time
154 EpochToEfiTime (EpochSeconds, Time);
155
156 // Update the Capabilities info
157 if (Capabilities != NULL) {
158 // PL031 runs at frequency 1Hz
159 Capabilities->Resolution = PL031_COUNTS_PER_SECOND;
160 // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000
161 Capabilities->Accuracy = (UINT32)PcdGet32 (PcdPL031RtcPpmAccuracy);
162 // FALSE: Setting the time does not clear the values below the resolution level
163 Capabilities->SetsToZero = FALSE;
164 }
165
166 return EFI_SUCCESS;
167 }
168
169 /**
170 Sets the current local time and date information.
171
172 @param Time A pointer to the current time.
173
174 @retval EFI_SUCCESS The operation completed successfully.
175 @retval EFI_INVALID_PARAMETER A time field is out of range.
176 @retval EFI_DEVICE_ERROR The time could not be set due to hardware error.
177
178 **/
179 EFI_STATUS
180 EFIAPI
181 LibSetTime (
182 IN EFI_TIME *Time
183 )
184 {
185 EFI_STATUS Status;
186 UINT32 EpochSeconds;
187
188 // Because the PL031 is a 32-bit counter counting seconds,
189 // the maximum time span is just over 136 years.
190 // Time is stored in Unix Epoch format, so it starts in 1970,
191 // Therefore it can not exceed the year 2106.
192 if ((Time->Year < 1970) || (Time->Year >= 2106)) {
193 return EFI_UNSUPPORTED;
194 }
195
196 // Initialize the hardware if not already done
197 if (!mPL031Initialized) {
198 Status = InitializePL031 ();
199 if (EFI_ERROR (Status)) {
200 return Status;
201 }
202 }
203
204 EpochSeconds = (UINT32)EfiTimeToEpoch (Time);
205
206 // Adjust for the correct time zone, i.e. convert to UTC time zone
207 // The timezone setting also reflects the DST setting of the clock
208 if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {
209 EpochSeconds -= Time->TimeZone * SEC_PER_MIN;
210 } else if ((Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT) {
211 // Convert to un-adjusted time, i.e. fall back one hour
212 EpochSeconds -= SEC_PER_HOUR;
213 }
214
215 // Set the PL031
216 MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);
217
218 return EFI_SUCCESS;
219 }
220
221 /**
222 Returns the current wakeup alarm clock setting.
223
224 @param Enabled Indicates if the alarm is currently enabled or disabled.
225 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
226 @param Time The current alarm setting.
227
228 @retval EFI_SUCCESS The alarm settings were returned.
229 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
230 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
231
232 **/
233 EFI_STATUS
234 EFIAPI
235 LibGetWakeupTime (
236 OUT BOOLEAN *Enabled,
237 OUT BOOLEAN *Pending,
238 OUT EFI_TIME *Time
239 )
240 {
241 // Not a required feature
242 return EFI_UNSUPPORTED;
243 }
244
245 /**
246 Sets the system wakeup alarm clock time.
247
248 @param Enabled Enable or disable the wakeup alarm.
249 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
250
251 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
252 Enable is FALSE, then the wakeup alarm was disabled.
253 @retval EFI_INVALID_PARAMETER A time field is out of range.
254 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
255 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
256
257 **/
258 EFI_STATUS
259 EFIAPI
260 LibSetWakeupTime (
261 IN BOOLEAN Enabled,
262 OUT EFI_TIME *Time
263 )
264 {
265 // Not a required feature
266 return EFI_UNSUPPORTED;
267 }
268
269 /**
270 Fixup internal data so that EFI can be call in virtual mode.
271 Call the passed in Child Notify event and convert any pointers in
272 lib to virtual mode.
273
274 @param[in] Event The Event that is being processed
275 @param[in] Context Event Context
276 **/
277 VOID
278 EFIAPI
279 LibRtcVirtualNotifyEvent (
280 IN EFI_EVENT Event,
281 IN VOID *Context
282 )
283 {
284 //
285 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
286 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
287 // to virtual address. After the OS transitions to calling in virtual mode, all future
288 // runtime calls will be made in virtual mode.
289 //
290 EfiConvertPointer (0x0, (VOID **)&mPL031RtcBase);
291 return;
292 }
293
294 /**
295 This is the declaration of an EFI image entry point. This can be the entry point to an application
296 written to this specification, an EFI boot service driver, or an EFI runtime driver.
297
298 @param ImageHandle Handle that identifies the loaded image.
299 @param SystemTable System Table for this image.
300
301 @retval EFI_SUCCESS The operation completed successfully.
302
303 **/
304 EFI_STATUS
305 EFIAPI
306 LibRtcInitialize (
307 IN EFI_HANDLE ImageHandle,
308 IN EFI_SYSTEM_TABLE *SystemTable
309 )
310 {
311 EFI_STATUS Status;
312 EFI_HANDLE Handle;
313
314 // Initialize RTC Base Address
315 mPL031RtcBase = PcdGet32 (PcdPL031RtcBase);
316
317 // Declare the controller as EFI_MEMORY_RUNTIME
318 Status = gDS->AddMemorySpace (
319 EfiGcdMemoryTypeMemoryMappedIo,
320 mPL031RtcBase,
321 SIZE_4KB,
322 EFI_MEMORY_UC | EFI_MEMORY_RUNTIME
323 );
324 if (EFI_ERROR (Status)) {
325 return Status;
326 }
327
328 Status = gDS->SetMemorySpaceAttributes (mPL031RtcBase, SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
329 if (EFI_ERROR (Status)) {
330 return Status;
331 }
332
333 // Install the protocol
334 Handle = NULL;
335 Status = gBS->InstallMultipleProtocolInterfaces (
336 &Handle,
337 &gEfiRealTimeClockArchProtocolGuid,
338 NULL,
339 NULL
340 );
341 ASSERT_EFI_ERROR (Status);
342
343 //
344 // Register for the virtual address change event
345 //
346 Status = gBS->CreateEventEx (
347 EVT_NOTIFY_SIGNAL,
348 TPL_NOTIFY,
349 LibRtcVirtualNotifyEvent,
350 NULL,
351 &gEfiEventVirtualAddressChangeGuid,
352 &mRtcVirtualAddrChangeEvent
353 );
354 ASSERT_EFI_ERROR (Status);
355
356 return Status;
357 }