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 CONST CHAR16 mTimeZoneVariableName
[] = L
"PL031RtcTimeZone";
44 STATIC CONST CHAR16 mDaylightVariableName
[] = L
"PL031RtcDaylight";
45 STATIC BOOLEAN mPL031Initialized
= FALSE
;
46 STATIC EFI_EVENT mRtcVirtualAddrChangeEvent
;
47 STATIC UINTN mPL031RtcBase
;
56 // Check if this is a PrimeCell Peripheral
57 if ( (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PCELL_ID0
) != 0x0D)
58 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PCELL_ID1
) != 0xF0)
59 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PCELL_ID2
) != 0x05)
60 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PCELL_ID3
) != 0xB1)) {
61 Status
= EFI_NOT_FOUND
;
65 // Check if this PrimeCell Peripheral is the PL031 Real Time Clock
66 if ( (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PERIPH_ID0
) != 0x31)
67 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PERIPH_ID1
) != 0x10)
68 || ((MmioRead8 (mPL031RtcBase
+ PL031_RTC_PERIPH_ID2
) & 0xF) != 0x04)
69 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PERIPH_ID3
) != 0x00)) {
70 Status
= EFI_NOT_FOUND
;
87 // Prepare the hardware
88 Status
= IdentifyPL031();
89 if (EFI_ERROR (Status
)) {
93 // Ensure interrupts are masked. We do not want RTC interrupts in UEFI
94 if ((MmioRead32 (mPL031RtcBase
+ PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER
) & PL031_SET_IRQ_MASK
) != PL031_SET_IRQ_MASK
) {
95 MmioOr32 (mPL031RtcBase
+ PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER
, PL031_SET_IRQ_MASK
);
98 // Clear any existing interrupts
99 if ((MmioRead32 (mPL031RtcBase
+ PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER
) & PL031_IRQ_TRIGGERED
) == PL031_IRQ_TRIGGERED
) {
100 MmioOr32 (mPL031RtcBase
+ PL031_RTC_ICR_IRQ_CLEAR_REGISTER
, PL031_CLEAR_IRQ
);
103 // Start the clock counter
104 if ((MmioRead32 (mPL031RtcBase
+ PL031_RTC_CR_CONTROL_REGISTER
) & PL031_RTC_ENABLED
) != PL031_RTC_ENABLED
) {
105 MmioOr32 (mPL031RtcBase
+ PL031_RTC_CR_CONTROL_REGISTER
, PL031_RTC_ENABLED
);
108 mPL031Initialized
= TRUE
;
115 Returns the current time and date information, and the time-keeping capabilities
116 of the hardware platform.
118 @param Time A pointer to storage to receive a snapshot of the current time.
119 @param Capabilities An optional pointer to a buffer to receive the real time clock
120 device's capabilities.
122 @retval EFI_SUCCESS The operation completed successfully.
123 @retval EFI_INVALID_PARAMETER Time is NULL.
124 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
125 @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.
132 OUT EFI_TIME_CAPABILITIES
*Capabilities
135 EFI_STATUS Status
= EFI_SUCCESS
;
141 // Initialize the hardware if not already done
142 if (!mPL031Initialized
) {
143 Status
= InitializePL031 ();
144 if (EFI_ERROR (Status
)) {
149 // Snapshot the time as early in the function call as possible
150 // On some platforms we may have access to a battery backed up hardware clock.
151 // If such RTC exists try to use it first.
152 Status
= ArmPlatformSysConfigGet (SYS_CFG_RTC
, &EpochSeconds
);
153 if (Status
== EFI_UNSUPPORTED
) {
154 // Battery backed up hardware RTC does not exist, revert to PL031
155 EpochSeconds
= MmioRead32 (mPL031RtcBase
+ PL031_RTC_DR_DATA_REGISTER
);
156 Status
= EFI_SUCCESS
;
157 } else if (EFI_ERROR (Status
)) {
158 // Battery backed up hardware RTC exists but could not be read due to error. Abort.
161 // Battery backed up hardware RTC exists and we read the time correctly from it.
162 // Now sync the PL031 to the new time.
163 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
166 // Ensure Time is a valid pointer
168 Status
= EFI_INVALID_PARAMETER
;
172 // Get the current time zone information from non-volatile storage
173 Size
= sizeof (TimeZone
);
174 Status
= EfiGetVariable (
175 (CHAR16
*)mTimeZoneVariableName
,
182 if (EFI_ERROR (Status
)) {
183 ASSERT(Status
!= EFI_INVALID_PARAMETER
);
184 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
186 if (Status
!= EFI_NOT_FOUND
)
189 // The time zone variable does not exist in non-volatile storage, so create it.
190 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
192 Status
= EfiSetVariable (
193 (CHAR16
*)mTimeZoneVariableName
,
195 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
197 (VOID
*)&(Time
->TimeZone
)
199 if (EFI_ERROR (Status
)) {
202 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
203 mTimeZoneVariableName
,
210 Time
->TimeZone
= TimeZone
;
212 // Check TimeZone bounds: -1440 to 1440 or 2047
213 if (((Time
->TimeZone
< -1440) || (Time
->TimeZone
> 1440))
214 && (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
)) {
215 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
218 // Adjust for the correct time zone
219 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
220 EpochSeconds
+= Time
->TimeZone
* SEC_PER_MIN
;
224 // Get the current daylight information from non-volatile storage
225 Size
= sizeof (Daylight
);
226 Status
= EfiGetVariable (
227 (CHAR16
*)mDaylightVariableName
,
234 if (EFI_ERROR (Status
)) {
235 ASSERT(Status
!= EFI_INVALID_PARAMETER
);
236 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
238 if (Status
!= EFI_NOT_FOUND
)
241 // The daylight variable does not exist in non-volatile storage, so create it.
244 Status
= EfiSetVariable (
245 (CHAR16
*)mDaylightVariableName
,
247 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
249 (VOID
*)&(Time
->Daylight
)
251 if (EFI_ERROR (Status
)) {
254 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
255 mDaylightVariableName
,
261 // Got the daylight information
262 Time
->Daylight
= Daylight
;
264 // Adjust for the correct period
265 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
266 // Convert to adjusted time, i.e. spring forwards one hour
267 EpochSeconds
+= SEC_PER_HOUR
;
271 // Convert from internal 32-bit time to UEFI time
272 EpochToEfiTime (EpochSeconds
, Time
);
274 // Update the Capabilities info
275 if (Capabilities
!= NULL
) {
276 // PL031 runs at frequency 1Hz
277 Capabilities
->Resolution
= PL031_COUNTS_PER_SECOND
;
278 // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000
279 Capabilities
->Accuracy
= (UINT32
)PcdGet32 (PcdPL031RtcPpmAccuracy
);
280 // FALSE: Setting the time does not clear the values below the resolution level
281 Capabilities
->SetsToZero
= FALSE
;
290 Sets the current local time and date information.
292 @param Time A pointer to the current time.
294 @retval EFI_SUCCESS The operation completed successfully.
295 @retval EFI_INVALID_PARAMETER A time field is out of range.
296 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
308 // Check the input parameters are within the range specified by UEFI
309 if ((Time
->Year
< 1900) ||
310 (Time
->Year
> 9999) ||
311 (Time
->Month
< 1 ) ||
312 (Time
->Month
> 12 ) ||
313 (!IsDayValid (Time
) ) ||
314 (Time
->Hour
> 23 ) ||
315 (Time
->Minute
> 59 ) ||
316 (Time
->Second
> 59 ) ||
317 (Time
->Nanosecond
> 999999999) ||
318 (!((Time
->TimeZone
== EFI_UNSPECIFIED_TIMEZONE
) || ((Time
->TimeZone
>= -1440) && (Time
->TimeZone
<= 1440)))) ||
319 (Time
->Daylight
& (~(EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
)))
321 Status
= EFI_INVALID_PARAMETER
;
325 // Because the PL031 is a 32-bit counter counting seconds,
326 // the maximum time span is just over 136 years.
327 // Time is stored in Unix Epoch format, so it starts in 1970,
328 // Therefore it can not exceed the year 2106.
329 if ((Time
->Year
< 1970) || (Time
->Year
>= 2106)) {
330 Status
= EFI_UNSUPPORTED
;
334 // Initialize the hardware if not already done
335 if (!mPL031Initialized
) {
336 Status
= InitializePL031 ();
337 if (EFI_ERROR (Status
)) {
342 EpochSeconds
= EfiTimeToEpoch (Time
);
344 // Adjust for the correct time zone, i.e. convert to UTC time zone
345 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
346 EpochSeconds
-= Time
->TimeZone
* SEC_PER_MIN
;
349 // TODO: Automatic Daylight activation
351 // Adjust for the correct period
352 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
353 // Convert to un-adjusted time, i.e. fall back one hour
354 EpochSeconds
-= SEC_PER_HOUR
;
357 // On some platforms we may have access to a battery backed up hardware clock.
359 // If such RTC exists then it must be updated first, before the PL031,
360 // to minimise any time drift. This is important because the battery backed-up
361 // RTC maintains the master time for the platform across reboots.
363 // If such RTC does not exist then the following function returns UNSUPPORTED.
364 Status
= ArmPlatformSysConfigSet (SYS_CFG_RTC
, EpochSeconds
);
365 if ((EFI_ERROR (Status
)) && (Status
!= EFI_UNSUPPORTED
)){
366 // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.
372 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
374 // The accesses to Variable Services can be very slow, because we may be writing to Flash.
375 // Do this after having set the RTC.
377 // Save the current time zone information into non-volatile storage
378 Status
= EfiSetVariable (
379 (CHAR16
*)mTimeZoneVariableName
,
381 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
382 sizeof (Time
->TimeZone
),
383 (VOID
*)&(Time
->TimeZone
)
385 if (EFI_ERROR (Status
)) {
388 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
389 mTimeZoneVariableName
,
395 // Save the current daylight information into non-volatile storage
396 Status
= EfiSetVariable (
397 (CHAR16
*)mDaylightVariableName
,
399 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
400 sizeof(Time
->Daylight
),
401 (VOID
*)&(Time
->Daylight
)
403 if (EFI_ERROR (Status
)) {
406 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
407 mDaylightVariableName
,
419 Returns the current wakeup alarm clock setting.
421 @param Enabled Indicates if the alarm is currently enabled or disabled.
422 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
423 @param Time The current alarm setting.
425 @retval EFI_SUCCESS The alarm settings were returned.
426 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
427 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
433 OUT BOOLEAN
*Enabled
,
434 OUT BOOLEAN
*Pending
,
438 // Not a required feature
439 return EFI_UNSUPPORTED
;
444 Sets the system wakeup alarm clock time.
446 @param Enabled Enable or disable the wakeup alarm.
447 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
449 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
450 Enable is FALSE, then the wakeup alarm was disabled.
451 @retval EFI_INVALID_PARAMETER A time field is out of range.
452 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
453 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
463 // Not a required feature
464 return EFI_UNSUPPORTED
;
468 Fixup internal data so that EFI can be call in virtual mode.
469 Call the passed in Child Notify event and convert any pointers in
472 @param[in] Event The Event that is being processed
473 @param[in] Context Event Context
477 LibRtcVirtualNotifyEvent (
483 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
484 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
485 // to virtual address. After the OS transitions to calling in virtual mode, all future
486 // runtime calls will be made in virtual mode.
488 EfiConvertPointer (0x0, (VOID
**)&mPL031RtcBase
);
493 This is the declaration of an EFI image entry point. This can be the entry point to an application
494 written to this specification, an EFI boot service driver, or an EFI runtime driver.
496 @param ImageHandle Handle that identifies the loaded image.
497 @param SystemTable System Table for this image.
499 @retval EFI_SUCCESS The operation completed successfully.
505 IN EFI_HANDLE ImageHandle
,
506 IN EFI_SYSTEM_TABLE
*SystemTable
512 // Initialize RTC Base Address
513 mPL031RtcBase
= PcdGet32 (PcdPL031RtcBase
);
515 // Declare the controller as EFI_MEMORY_RUNTIME
516 Status
= gDS
->AddMemorySpace (
517 EfiGcdMemoryTypeMemoryMappedIo
,
518 mPL031RtcBase
, SIZE_4KB
,
519 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
521 if (EFI_ERROR (Status
)) {
525 Status
= gDS
->SetMemorySpaceAttributes (mPL031RtcBase
, SIZE_4KB
, EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
);
526 if (EFI_ERROR (Status
)) {
530 // Install the protocol
532 Status
= gBS
->InstallMultipleProtocolInterfaces (
534 &gEfiRealTimeClockArchProtocolGuid
, NULL
,
537 ASSERT_EFI_ERROR (Status
);
540 // Register for the virtual address change event
542 Status
= gBS
->CreateEventEx (
545 LibRtcVirtualNotifyEvent
,
547 &gEfiEventVirtualAddressChangeGuid
,
548 &mRtcVirtualAddrChangeEvent
550 ASSERT_EFI_ERROR (Status
);