2 Implement EFI RealTimeClock runtime services via RTC Lib.
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>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include <Guid/EventGroup.h>
15 #include <Guid/GlobalVariable.h>
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>
30 #include <Protocol/RealTimeClock.h>
32 #include "PL031RealTimeClock.h"
34 STATIC BOOLEAN mPL031Initialized
= FALSE
;
35 STATIC EFI_EVENT mRtcVirtualAddrChangeEvent
;
36 STATIC UINTN mPL031RtcBase
;
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))
51 Status
= EFI_NOT_FOUND
;
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))
61 Status
= EFI_NOT_FOUND
;
78 // Prepare the hardware
79 Status
= IdentifyPL031 ();
80 if (EFI_ERROR (Status
)) {
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);
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
);
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
);
99 mPL031Initialized
= TRUE
;
106 Returns the current time and date information, and the time-keeping capabilities
107 of the hardware platform.
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.
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.
123 OUT EFI_TIME_CAPABILITIES
*Capabilities
129 // Ensure Time is a valid pointer
131 return EFI_INVALID_PARAMETER
;
134 // Initialize the hardware if not already done
135 if (!mPL031Initialized
) {
136 Status
= InitializePL031 ();
137 if (EFI_ERROR (Status
)) {
142 EpochSeconds
= MmioRead32 (mPL031RtcBase
+ PL031_RTC_DR_DATA_REGISTER
);
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
;
153 // Convert from internal 32-bit time to UEFI time
154 EpochToEfiTime (EpochSeconds
, Time
);
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
;
170 Sets the current local time and date information.
172 @param Time A pointer to the current time.
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 due to hardware error.
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
;
196 // Initialize the hardware if not already done
197 if (!mPL031Initialized
) {
198 Status
= InitializePL031 ();
199 if (EFI_ERROR (Status
)) {
204 EpochSeconds
= (UINT32
)EfiTimeToEpoch (Time
);
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
;
216 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
222 Returns the current wakeup alarm clock setting.
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.
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.
236 OUT BOOLEAN
*Enabled
,
237 OUT BOOLEAN
*Pending
,
241 // Not a required feature
242 return EFI_UNSUPPORTED
;
246 Sets the system wakeup alarm clock time.
248 @param Enabled Enable or disable the wakeup alarm.
249 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
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.
265 // Not a required feature
266 return EFI_UNSUPPORTED
;
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
274 @param[in] Event The Event that is being processed
275 @param[in] Context Event Context
279 LibRtcVirtualNotifyEvent (
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.
290 EfiConvertPointer (0x0, (VOID
**)&mPL031RtcBase
);
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.
298 @param ImageHandle Handle that identifies the loaded image.
299 @param SystemTable System Table for this image.
301 @retval EFI_SUCCESS The operation completed successfully.
307 IN EFI_HANDLE ImageHandle
,
308 IN EFI_SYSTEM_TABLE
*SystemTable
314 // Initialize RTC Base Address
315 mPL031RtcBase
= PcdGet32 (PcdPL031RtcBase
);
317 // Declare the controller as EFI_MEMORY_RUNTIME
318 Status
= gDS
->AddMemorySpace (
319 EfiGcdMemoryTypeMemoryMappedIo
,
322 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
324 if (EFI_ERROR (Status
)) {
328 Status
= gDS
->SetMemorySpaceAttributes (mPL031RtcBase
, SIZE_4KB
, EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
);
329 if (EFI_ERROR (Status
)) {
333 // Install the protocol
335 Status
= gBS
->InstallMultipleProtocolInterfaces (
337 &gEfiRealTimeClockArchProtocolGuid
,
341 ASSERT_EFI_ERROR (Status
);
344 // Register for the virtual address change event
346 Status
= gBS
->CreateEventEx (
349 LibRtcVirtualNotifyEvent
,
351 &gEfiEventVirtualAddressChangeGuid
,
352 &mRtcVirtualAddrChangeEvent
354 ASSERT_EFI_ERROR (Status
);