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 // The timezone setting also reflects the DST setting of the clock
168 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
169 EpochSeconds
+= Time
->TimeZone
* SEC_PER_MIN
;
170 } else if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
171 // Convert to adjusted time, i.e. spring forwards one hour
172 EpochSeconds
+= SEC_PER_HOUR
;
175 // Convert from internal 32-bit time to UEFI time
176 EpochToEfiTime (EpochSeconds
, Time
);
178 // Update the Capabilities info
179 if (Capabilities
!= NULL
) {
180 // PL031 runs at frequency 1Hz
181 Capabilities
->Resolution
= PL031_COUNTS_PER_SECOND
;
182 // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000
183 Capabilities
->Accuracy
= (UINT32
)PcdGet32 (PcdPL031RtcPpmAccuracy
);
184 // FALSE: Setting the time does not clear the values below the resolution level
185 Capabilities
->SetsToZero
= FALSE
;
193 Sets the current local time and date information.
195 @param Time A pointer to the current time.
197 @retval EFI_SUCCESS The operation completed successfully.
198 @retval EFI_INVALID_PARAMETER A time field is out of range.
199 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
211 // Because the PL031 is a 32-bit counter counting seconds,
212 // the maximum time span is just over 136 years.
213 // Time is stored in Unix Epoch format, so it starts in 1970,
214 // Therefore it can not exceed the year 2106.
215 if ((Time
->Year
< 1970) || (Time
->Year
>= 2106)) {
216 return EFI_UNSUPPORTED
;
219 // Initialize the hardware if not already done
220 if (!mPL031Initialized
) {
221 Status
= InitializePL031 ();
222 if (EFI_ERROR (Status
)) {
227 EpochSeconds
= EfiTimeToEpoch (Time
);
229 // Adjust for the correct time zone, i.e. convert to UTC time zone
230 // The timezone setting also reflects the DST setting of the clock
231 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
232 EpochSeconds
-= Time
->TimeZone
* SEC_PER_MIN
;
233 } else if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
234 // Convert to un-adjusted time, i.e. fall back one hour
235 EpochSeconds
-= SEC_PER_HOUR
;
238 // On some platforms we may have access to a battery backed up hardware clock.
240 // If such RTC exists then it must be updated first, before the PL031,
241 // to minimise any time drift. This is important because the battery backed-up
242 // RTC maintains the master time for the platform across reboots.
244 // If such RTC does not exist then the following function returns UNSUPPORTED.
245 Status
= ArmPlatformSysConfigSet (SYS_CFG_RTC
, EpochSeconds
);
246 if ((EFI_ERROR (Status
)) && (Status
!= EFI_UNSUPPORTED
)){
247 // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.
252 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
259 Returns the current wakeup alarm clock setting.
261 @param Enabled Indicates if the alarm is currently enabled or disabled.
262 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
263 @param Time The current alarm setting.
265 @retval EFI_SUCCESS The alarm settings were returned.
266 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
267 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
273 OUT BOOLEAN
*Enabled
,
274 OUT BOOLEAN
*Pending
,
278 // Not a required feature
279 return EFI_UNSUPPORTED
;
284 Sets the system wakeup alarm clock time.
286 @param Enabled Enable or disable the wakeup alarm.
287 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
289 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
290 Enable is FALSE, then the wakeup alarm was disabled.
291 @retval EFI_INVALID_PARAMETER A time field is out of range.
292 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
293 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
303 // Not a required feature
304 return EFI_UNSUPPORTED
;
308 Fixup internal data so that EFI can be call in virtual mode.
309 Call the passed in Child Notify event and convert any pointers in
312 @param[in] Event The Event that is being processed
313 @param[in] Context Event Context
317 LibRtcVirtualNotifyEvent (
323 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
324 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
325 // to virtual address. After the OS transitions to calling in virtual mode, all future
326 // runtime calls will be made in virtual mode.
328 EfiConvertPointer (0x0, (VOID
**)&mPL031RtcBase
);
333 This is the declaration of an EFI image entry point. This can be the entry point to an application
334 written to this specification, an EFI boot service driver, or an EFI runtime driver.
336 @param ImageHandle Handle that identifies the loaded image.
337 @param SystemTable System Table for this image.
339 @retval EFI_SUCCESS The operation completed successfully.
345 IN EFI_HANDLE ImageHandle
,
346 IN EFI_SYSTEM_TABLE
*SystemTable
352 // Initialize RTC Base Address
353 mPL031RtcBase
= PcdGet32 (PcdPL031RtcBase
);
355 // Declare the controller as EFI_MEMORY_RUNTIME
356 Status
= gDS
->AddMemorySpace (
357 EfiGcdMemoryTypeMemoryMappedIo
,
358 mPL031RtcBase
, SIZE_4KB
,
359 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
361 if (EFI_ERROR (Status
)) {
365 Status
= gDS
->SetMemorySpaceAttributes (mPL031RtcBase
, SIZE_4KB
, EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
);
366 if (EFI_ERROR (Status
)) {
370 // Install the protocol
372 Status
= gBS
->InstallMultipleProtocolInterfaces (
374 &gEfiRealTimeClockArchProtocolGuid
, NULL
,
377 ASSERT_EFI_ERROR (Status
);
380 // Register for the virtual address change event
382 Status
= gBS
->CreateEventEx (
385 LibRtcVirtualNotifyEvent
,
387 &gEfiEventVirtualAddressChangeGuid
,
388 &mRtcVirtualAddrChangeEvent
390 ASSERT_EFI_ERROR (Status
);