2 Implement EFI RealTimeClock runtime services via RTC Lib.
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Library/BaseLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/UefiLib.h>
22 #include <Library/IoLib.h>
23 #include <Library/RealTimeClockLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/ArmPlatformSysConfigLib.h>
27 #include <Library/DxeServicesTableLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/UefiRuntimeServicesTableLib.h>
30 #include <Library/UefiRuntimeLib.h>
32 #include <Protocol/RealTimeClock.h>
34 #include <Guid/GlobalVariable.h>
35 #include <Guid/EventGroup.h>
37 #include <Drivers/PL031RealTimeClock.h>
39 #include <Library/TimeBaseLib.h>
41 #include <ArmPlatform.h>
43 STATIC BOOLEAN mPL031Initialized
= FALSE
;
44 STATIC EFI_EVENT mRtcVirtualAddrChangeEvent
;
45 STATIC UINTN mPL031RtcBase
;
54 // Check if this is a PrimeCell Peripheral
55 if ( (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PCELL_ID0
) != 0x0D)
56 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PCELL_ID1
) != 0xF0)
57 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PCELL_ID2
) != 0x05)
58 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PCELL_ID3
) != 0xB1)) {
59 Status
= EFI_NOT_FOUND
;
63 // Check if this PrimeCell Peripheral is the PL031 Real Time Clock
64 if ( (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PERIPH_ID0
) != 0x31)
65 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PERIPH_ID1
) != 0x10)
66 || ((MmioRead8 (mPL031RtcBase
+ PL031_RTC_PERIPH_ID2
) & 0xF) != 0x04)
67 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PERIPH_ID3
) != 0x00)) {
68 Status
= EFI_NOT_FOUND
;
85 // Prepare the hardware
86 Status
= IdentifyPL031();
87 if (EFI_ERROR (Status
)) {
91 // Ensure interrupts are masked. We do not want RTC interrupts in UEFI
92 if ((MmioRead32 (mPL031RtcBase
+ PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER
) & PL031_SET_IRQ_MASK
) != PL031_SET_IRQ_MASK
) {
93 MmioOr32 (mPL031RtcBase
+ PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER
, PL031_SET_IRQ_MASK
);
96 // Clear any existing interrupts
97 if ((MmioRead32 (mPL031RtcBase
+ PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER
) & PL031_IRQ_TRIGGERED
) == PL031_IRQ_TRIGGERED
) {
98 MmioOr32 (mPL031RtcBase
+ PL031_RTC_ICR_IRQ_CLEAR_REGISTER
, PL031_CLEAR_IRQ
);
101 // Start the clock counter
102 if ((MmioRead32 (mPL031RtcBase
+ PL031_RTC_CR_CONTROL_REGISTER
) & PL031_RTC_ENABLED
) != PL031_RTC_ENABLED
) {
103 MmioOr32 (mPL031RtcBase
+ PL031_RTC_CR_CONTROL_REGISTER
, PL031_RTC_ENABLED
);
106 mPL031Initialized
= TRUE
;
113 Returns the current time and date information, and the time-keeping capabilities
114 of the hardware platform.
116 @param Time A pointer to storage to receive a snapshot of the current time.
117 @param Capabilities An optional pointer to a buffer to receive the real time clock
118 device's capabilities.
120 @retval EFI_SUCCESS The operation completed successfully.
121 @retval EFI_INVALID_PARAMETER Time is NULL.
122 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
123 @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.
130 OUT EFI_TIME_CAPABILITIES
*Capabilities
133 EFI_STATUS Status
= EFI_SUCCESS
;
136 // Initialize the hardware if not already done
137 if (!mPL031Initialized
) {
138 Status
= InitializePL031 ();
139 if (EFI_ERROR (Status
)) {
144 // Snapshot the time as early in the function call as possible
145 // On some platforms we may have access to a battery backed up hardware clock.
146 // If such RTC exists try to use it first.
147 Status
= ArmPlatformSysConfigGet (SYS_CFG_RTC
, &EpochSeconds
);
148 if (Status
== EFI_UNSUPPORTED
) {
149 // Battery backed up hardware RTC does not exist, revert to PL031
150 EpochSeconds
= MmioRead32 (mPL031RtcBase
+ PL031_RTC_DR_DATA_REGISTER
);
151 Status
= EFI_SUCCESS
;
152 } else if (EFI_ERROR (Status
)) {
153 // Battery backed up hardware RTC exists but could not be read due to error. Abort.
156 // Battery backed up hardware RTC exists and we read the time correctly from it.
157 // Now sync the PL031 to the new time.
158 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
161 // Ensure Time is a valid pointer
163 return EFI_INVALID_PARAMETER
;
166 // Adjust for the correct time zone
167 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
168 EpochSeconds
+= Time
->TimeZone
* SEC_PER_MIN
;
171 // Adjust for the correct period
172 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
173 // Convert to adjusted time, i.e. spring forwards one hour
174 EpochSeconds
+= SEC_PER_HOUR
;
177 // Convert from internal 32-bit time to UEFI time
178 EpochToEfiTime (EpochSeconds
, Time
);
180 // Update the Capabilities info
181 if (Capabilities
!= NULL
) {
182 // PL031 runs at frequency 1Hz
183 Capabilities
->Resolution
= PL031_COUNTS_PER_SECOND
;
184 // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000
185 Capabilities
->Accuracy
= (UINT32
)PcdGet32 (PcdPL031RtcPpmAccuracy
);
186 // FALSE: Setting the time does not clear the values below the resolution level
187 Capabilities
->SetsToZero
= FALSE
;
195 Sets the current local time and date information.
197 @param Time A pointer to the current time.
199 @retval EFI_SUCCESS The operation completed successfully.
200 @retval EFI_INVALID_PARAMETER A time field is out of range.
201 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
213 // Because the PL031 is a 32-bit counter counting seconds,
214 // the maximum time span is just over 136 years.
215 // Time is stored in Unix Epoch format, so it starts in 1970,
216 // Therefore it can not exceed the year 2106.
217 if ((Time
->Year
< 1970) || (Time
->Year
>= 2106)) {
218 return EFI_UNSUPPORTED
;
221 // Initialize the hardware if not already done
222 if (!mPL031Initialized
) {
223 Status
= InitializePL031 ();
224 if (EFI_ERROR (Status
)) {
229 EpochSeconds
= EfiTimeToEpoch (Time
);
231 // Adjust for the correct time zone, i.e. convert to UTC time zone
232 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
233 EpochSeconds
-= Time
->TimeZone
* SEC_PER_MIN
;
236 // Adjust for the correct period
237 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
238 // Convert to un-adjusted time, i.e. fall back one hour
239 EpochSeconds
-= SEC_PER_HOUR
;
242 // On some platforms we may have access to a battery backed up hardware clock.
244 // If such RTC exists then it must be updated first, before the PL031,
245 // to minimise any time drift. This is important because the battery backed-up
246 // RTC maintains the master time for the platform across reboots.
248 // If such RTC does not exist then the following function returns UNSUPPORTED.
249 Status
= ArmPlatformSysConfigSet (SYS_CFG_RTC
, EpochSeconds
);
250 if ((EFI_ERROR (Status
)) && (Status
!= EFI_UNSUPPORTED
)){
251 // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.
256 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
263 Returns the current wakeup alarm clock setting.
265 @param Enabled Indicates if the alarm is currently enabled or disabled.
266 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
267 @param Time The current alarm setting.
269 @retval EFI_SUCCESS The alarm settings were returned.
270 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
271 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
277 OUT BOOLEAN
*Enabled
,
278 OUT BOOLEAN
*Pending
,
282 // Not a required feature
283 return EFI_UNSUPPORTED
;
288 Sets the system wakeup alarm clock time.
290 @param Enabled Enable or disable the wakeup alarm.
291 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
293 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
294 Enable is FALSE, then the wakeup alarm was disabled.
295 @retval EFI_INVALID_PARAMETER A time field is out of range.
296 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
297 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
307 // Not a required feature
308 return EFI_UNSUPPORTED
;
312 Fixup internal data so that EFI can be call in virtual mode.
313 Call the passed in Child Notify event and convert any pointers in
316 @param[in] Event The Event that is being processed
317 @param[in] Context Event Context
321 LibRtcVirtualNotifyEvent (
327 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
328 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
329 // to virtual address. After the OS transitions to calling in virtual mode, all future
330 // runtime calls will be made in virtual mode.
332 EfiConvertPointer (0x0, (VOID
**)&mPL031RtcBase
);
337 This is the declaration of an EFI image entry point. This can be the entry point to an application
338 written to this specification, an EFI boot service driver, or an EFI runtime driver.
340 @param ImageHandle Handle that identifies the loaded image.
341 @param SystemTable System Table for this image.
343 @retval EFI_SUCCESS The operation completed successfully.
349 IN EFI_HANDLE ImageHandle
,
350 IN EFI_SYSTEM_TABLE
*SystemTable
356 // Initialize RTC Base Address
357 mPL031RtcBase
= PcdGet32 (PcdPL031RtcBase
);
359 // Declare the controller as EFI_MEMORY_RUNTIME
360 Status
= gDS
->AddMemorySpace (
361 EfiGcdMemoryTypeMemoryMappedIo
,
362 mPL031RtcBase
, SIZE_4KB
,
363 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
365 if (EFI_ERROR (Status
)) {
369 Status
= gDS
->SetMemorySpaceAttributes (mPL031RtcBase
, SIZE_4KB
, EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
);
370 if (EFI_ERROR (Status
)) {
374 // Install the protocol
376 Status
= gBS
->InstallMultipleProtocolInterfaces (
378 &gEfiRealTimeClockArchProtocolGuid
, NULL
,
381 ASSERT_EFI_ERROR (Status
);
384 // Register for the virtual address change event
386 Status
= gBS
->CreateEventEx (
389 LibRtcVirtualNotifyEvent
,
391 &gEfiEventVirtualAddressChangeGuid
,
392 &mRtcVirtualAddrChangeEvent
394 ASSERT_EFI_ERROR (Status
);