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 <ArmPlatform.h>
41 STATIC CONST CHAR16 mTimeZoneVariableName
[] = L
"PL031RtcTimeZone";
42 STATIC CONST CHAR16 mDaylightVariableName
[] = L
"PL031RtcDaylight";
43 STATIC BOOLEAN mPL031Initialized
= FALSE
;
44 STATIC EFI_EVENT mRtcVirtualAddrChangeEvent
;
45 STATIC UINTN mPL031RtcBase
;
46 STATIC EFI_RUNTIME_SERVICES
*mRT
;
55 // Check if this is a PrimeCell Peripheral
56 if ( (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PCELL_ID0
) != 0x0D)
57 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PCELL_ID1
) != 0xF0)
58 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PCELL_ID2
) != 0x05)
59 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PCELL_ID3
) != 0xB1)) {
60 Status
= EFI_NOT_FOUND
;
64 // Check if this PrimeCell Peripheral is the PL031 Real Time Clock
65 if ( (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PERIPH_ID0
) != 0x31)
66 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PERIPH_ID1
) != 0x10)
67 || ((MmioRead8 (mPL031RtcBase
+ PL031_RTC_PERIPH_ID2
) & 0xF) != 0x04)
68 || (MmioRead8 (mPL031RtcBase
+ PL031_RTC_PERIPH_ID3
) != 0x00)) {
69 Status
= EFI_NOT_FOUND
;
86 // Prepare the hardware
87 Status
= IdentifyPL031();
88 if (EFI_ERROR (Status
)) {
92 // Ensure interrupts are masked. We do not want RTC interrupts in UEFI
93 if ((MmioRead32 (mPL031RtcBase
+ PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER
) & PL031_SET_IRQ_MASK
) != PL031_SET_IRQ_MASK
) {
94 MmioOr32 (mPL031RtcBase
+ PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER
, PL031_SET_IRQ_MASK
);
97 // Clear any existing interrupts
98 if ((MmioRead32 (mPL031RtcBase
+ PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER
) & PL031_IRQ_TRIGGERED
) == PL031_IRQ_TRIGGERED
) {
99 MmioOr32 (mPL031RtcBase
+ PL031_RTC_ICR_IRQ_CLEAR_REGISTER
, PL031_CLEAR_IRQ
);
102 // Start the clock counter
103 if ((MmioRead32 (mPL031RtcBase
+ PL031_RTC_CR_CONTROL_REGISTER
) & PL031_RTC_ENABLED
) != PL031_RTC_ENABLED
) {
104 MmioOr32 (mPL031RtcBase
+ PL031_RTC_CR_CONTROL_REGISTER
, PL031_RTC_ENABLED
);
107 mPL031Initialized
= TRUE
;
114 Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to EFI_TIME
118 IN UINTN EpochSeconds
,
139 J
= (EpochSeconds
/ 86400) + 2440588;
143 c
= (((dg
/ 36524) + 1) * 3) / 4;
144 dc
= dg
- (c
* 36524);
147 a
= (((db
/ 365) + 1) * 3) / 4;
149 y
= (g
* 400) + (c
* 100) + (b
* 4) + a
;
150 m
= (((da
* 5) + 308) / 153) - 2;
151 d
= da
- (((m
+ 4) * 153) / 5) + 122;
153 Time
->Year
= y
- 4800 + ((m
+ 2) / 12);
154 Time
->Month
= ((m
+ 2) % 12) + 1;
157 ss
= EpochSeconds
% 60;
158 a
= (EpochSeconds
- ss
) / 60;
166 Time
->Nanosecond
= 0;
171 Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC)
181 UINTN JulianDate
; // Absolute Julian Date representation of the supplied Time
182 UINTN EpochDays
; // Number of days elapsed since EPOCH_JULIAN_DAY
185 a
= (14 - Time
->Month
) / 12 ;
186 y
= Time
->Year
+ 4800 - a
;
187 m
= Time
->Month
+ (12*a
) - 3;
189 JulianDate
= Time
->Day
+ ((153*m
+ 2)/5) + (365*y
) + (y
/4) - (y
/100) + (y
/400) - 32045;
191 ASSERT (JulianDate
>= EPOCH_JULIAN_DATE
);
192 EpochDays
= JulianDate
- EPOCH_JULIAN_DATE
;
194 EpochSeconds
= (EpochDays
* SEC_PER_DAY
) + ((UINTN
)Time
->Hour
* SEC_PER_HOUR
) + (Time
->Minute
* SEC_PER_MIN
) + Time
->Second
;
204 if (Time
->Year
% 4 == 0) {
205 if (Time
->Year
% 100 == 0) {
206 if (Time
->Year
% 400 == 0) {
224 STATIC CONST INTN DayOfMonth
[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
227 Time
->Day
> DayOfMonth
[Time
->Month
- 1] ||
228 (Time
->Month
== 2 && (!IsLeapYear (Time
) && Time
->Day
> 28))
237 Returns the current time and date information, and the time-keeping capabilities
238 of the hardware platform.
240 @param Time A pointer to storage to receive a snapshot of the current time.
241 @param Capabilities An optional pointer to a buffer to receive the real time clock
242 device's capabilities.
244 @retval EFI_SUCCESS The operation completed successfully.
245 @retval EFI_INVALID_PARAMETER Time is NULL.
246 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
247 @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.
254 OUT EFI_TIME_CAPABILITIES
*Capabilities
257 EFI_STATUS Status
= EFI_SUCCESS
;
263 // Initialize the hardware if not already done
264 if (!mPL031Initialized
) {
265 Status
= InitializePL031 ();
266 if (EFI_ERROR (Status
)) {
271 // Snapshot the time as early in the function call as possible
272 // On some platforms we may have access to a battery backed up hardware clock.
273 // If such RTC exists try to use it first.
274 Status
= ArmPlatformSysConfigGet (SYS_CFG_RTC
, &EpochSeconds
);
275 if (Status
== EFI_UNSUPPORTED
) {
276 // Battery backed up hardware RTC does not exist, revert to PL031
277 EpochSeconds
= MmioRead32 (mPL031RtcBase
+ PL031_RTC_DR_DATA_REGISTER
);
278 Status
= EFI_SUCCESS
;
279 } else if (EFI_ERROR (Status
)) {
280 // Battery backed up hardware RTC exists but could not be read due to error. Abort.
283 // Battery backed up hardware RTC exists and we read the time correctly from it.
284 // Now sync the PL031 to the new time.
285 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
288 // Ensure Time is a valid pointer
290 Status
= EFI_INVALID_PARAMETER
;
294 // Get the current time zone information from non-volatile storage
295 Size
= sizeof (TimeZone
);
296 Status
= mRT
->GetVariable (
297 (CHAR16
*)mTimeZoneVariableName
,
304 if (EFI_ERROR (Status
)) {
305 ASSERT(Status
!= EFI_INVALID_PARAMETER
);
306 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
308 if (Status
!= EFI_NOT_FOUND
)
311 // The time zone variable does not exist in non-volatile storage, so create it.
312 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
314 Status
= mRT
->SetVariable (
315 (CHAR16
*)mTimeZoneVariableName
,
317 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
319 (VOID
*)&(Time
->TimeZone
)
321 if (EFI_ERROR (Status
)) {
324 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
325 mTimeZoneVariableName
,
332 Time
->TimeZone
= TimeZone
;
334 // Check TimeZone bounds: -1440 to 1440 or 2047
335 if (((Time
->TimeZone
< -1440) || (Time
->TimeZone
> 1440))
336 && (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
)) {
337 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
340 // Adjust for the correct time zone
341 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
342 EpochSeconds
+= Time
->TimeZone
* SEC_PER_MIN
;
346 // Get the current daylight information from non-volatile storage
347 Size
= sizeof (Daylight
);
348 Status
= mRT
->GetVariable (
349 (CHAR16
*)mDaylightVariableName
,
356 if (EFI_ERROR (Status
)) {
357 ASSERT(Status
!= EFI_INVALID_PARAMETER
);
358 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
360 if (Status
!= EFI_NOT_FOUND
)
363 // The daylight variable does not exist in non-volatile storage, so create it.
366 Status
= mRT
->SetVariable (
367 (CHAR16
*)mDaylightVariableName
,
369 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
371 (VOID
*)&(Time
->Daylight
)
373 if (EFI_ERROR (Status
)) {
376 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
377 mDaylightVariableName
,
383 // Got the daylight information
384 Time
->Daylight
= Daylight
;
386 // Adjust for the correct period
387 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
388 // Convert to adjusted time, i.e. spring forwards one hour
389 EpochSeconds
+= SEC_PER_HOUR
;
393 // Convert from internal 32-bit time to UEFI time
394 EpochToEfiTime (EpochSeconds
, Time
);
396 // Update the Capabilities info
397 if (Capabilities
!= NULL
) {
398 // PL031 runs at frequency 1Hz
399 Capabilities
->Resolution
= PL031_COUNTS_PER_SECOND
;
400 // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000
401 Capabilities
->Accuracy
= (UINT32
)PcdGet32 (PcdPL031RtcPpmAccuracy
);
402 // FALSE: Setting the time does not clear the values below the resolution level
403 Capabilities
->SetsToZero
= FALSE
;
412 Sets the current local time and date information.
414 @param Time A pointer to the current time.
416 @retval EFI_SUCCESS The operation completed successfully.
417 @retval EFI_INVALID_PARAMETER A time field is out of range.
418 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
430 // Check the input parameters are within the range specified by UEFI
431 if ((Time
->Year
< 1900) ||
432 (Time
->Year
> 9999) ||
433 (Time
->Month
< 1 ) ||
434 (Time
->Month
> 12 ) ||
435 (!DayValid (Time
) ) ||
436 (Time
->Hour
> 23 ) ||
437 (Time
->Minute
> 59 ) ||
438 (Time
->Second
> 59 ) ||
439 (Time
->Nanosecond
> 999999999) ||
440 (!((Time
->TimeZone
== EFI_UNSPECIFIED_TIMEZONE
) || ((Time
->TimeZone
>= -1440) && (Time
->TimeZone
<= 1440)))) ||
441 (Time
->Daylight
& (~(EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
)))
443 Status
= EFI_INVALID_PARAMETER
;
447 // Because the PL031 is a 32-bit counter counting seconds,
448 // the maximum time span is just over 136 years.
449 // Time is stored in Unix Epoch format, so it starts in 1970,
450 // Therefore it can not exceed the year 2106.
451 if ((Time
->Year
< 1970) || (Time
->Year
>= 2106)) {
452 Status
= EFI_UNSUPPORTED
;
456 // Initialize the hardware if not already done
457 if (!mPL031Initialized
) {
458 Status
= InitializePL031 ();
459 if (EFI_ERROR (Status
)) {
464 EpochSeconds
= EfiTimeToEpoch (Time
);
466 // Adjust for the correct time zone, i.e. convert to UTC time zone
467 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
468 EpochSeconds
-= Time
->TimeZone
* SEC_PER_MIN
;
471 // TODO: Automatic Daylight activation
473 // Adjust for the correct period
474 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
475 // Convert to un-adjusted time, i.e. fall back one hour
476 EpochSeconds
-= SEC_PER_HOUR
;
479 // On some platforms we may have access to a battery backed up hardware clock.
481 // If such RTC exists then it must be updated first, before the PL031,
482 // to minimise any time drift. This is important because the battery backed-up
483 // RTC maintains the master time for the platform across reboots.
485 // If such RTC does not exist then the following function returns UNSUPPORTED.
486 Status
= ArmPlatformSysConfigSet (SYS_CFG_RTC
, EpochSeconds
);
487 if ((EFI_ERROR (Status
)) && (Status
!= EFI_UNSUPPORTED
)){
488 // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.
494 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
496 // The accesses to Variable Services can be very slow, because we may be writing to Flash.
497 // Do this after having set the RTC.
499 // Save the current time zone information into non-volatile storage
500 Status
= mRT
->SetVariable (
501 (CHAR16
*)mTimeZoneVariableName
,
503 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
504 sizeof (Time
->TimeZone
),
505 (VOID
*)&(Time
->TimeZone
)
507 if (EFI_ERROR (Status
)) {
510 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
511 mTimeZoneVariableName
,
517 // Save the current daylight information into non-volatile storage
518 Status
= mRT
->SetVariable (
519 (CHAR16
*)mDaylightVariableName
,
521 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
522 sizeof(Time
->Daylight
),
523 (VOID
*)&(Time
->Daylight
)
525 if (EFI_ERROR (Status
)) {
528 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
529 mDaylightVariableName
,
541 Returns the current wakeup alarm clock setting.
543 @param Enabled Indicates if the alarm is currently enabled or disabled.
544 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
545 @param Time The current alarm setting.
547 @retval EFI_SUCCESS The alarm settings were returned.
548 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
549 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
555 OUT BOOLEAN
*Enabled
,
556 OUT BOOLEAN
*Pending
,
560 // Not a required feature
561 return EFI_UNSUPPORTED
;
566 Sets the system wakeup alarm clock time.
568 @param Enabled Enable or disable the wakeup alarm.
569 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
571 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
572 Enable is FALSE, then the wakeup alarm was disabled.
573 @retval EFI_INVALID_PARAMETER A time field is out of range.
574 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
575 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
585 // Not a required feature
586 return EFI_UNSUPPORTED
;
590 Fixup internal data so that EFI can be call in virtual mode.
591 Call the passed in Child Notify event and convert any pointers in
594 @param[in] Event The Event that is being processed
595 @param[in] Context Event Context
599 LibRtcVirtualNotifyEvent (
605 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
606 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
607 // to virtual address. After the OS transitions to calling in virtual mode, all future
608 // runtime calls will be made in virtual mode.
610 EfiConvertPointer (0x0, (VOID
**)&mPL031RtcBase
);
611 EfiConvertPointer (0x0, (VOID
**)&mRT
);
616 This is the declaration of an EFI image entry point. This can be the entry point to an application
617 written to this specification, an EFI boot service driver, or an EFI runtime driver.
619 @param ImageHandle Handle that identifies the loaded image.
620 @param SystemTable System Table for this image.
622 @retval EFI_SUCCESS The operation completed successfully.
628 IN EFI_HANDLE ImageHandle
,
629 IN EFI_SYSTEM_TABLE
*SystemTable
635 // Initialize RTC Base Address
636 mPL031RtcBase
= PcdGet32 (PcdPL031RtcBase
);
638 // Declare the controller as EFI_MEMORY_RUNTIME
639 Status
= gDS
->AddMemorySpace (
640 EfiGcdMemoryTypeMemoryMappedIo
,
641 mPL031RtcBase
, SIZE_4KB
,
642 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
644 if (EFI_ERROR (Status
)) {
648 Status
= gDS
->SetMemorySpaceAttributes (mPL031RtcBase
, SIZE_4KB
, EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
);
649 if (EFI_ERROR (Status
)) {
653 // Setup the setters and getters
654 gRT
->GetTime
= LibGetTime
;
655 gRT
->SetTime
= LibSetTime
;
656 gRT
->GetWakeupTime
= LibGetWakeupTime
;
657 gRT
->SetWakeupTime
= LibSetWakeupTime
;
661 // Install the protocol
663 Status
= gBS
->InstallMultipleProtocolInterfaces (
665 &gEfiRealTimeClockArchProtocolGuid
, NULL
,
668 ASSERT_EFI_ERROR (Status
);
671 // Register for the virtual address change event
673 Status
= gBS
->CreateEventEx (
676 LibRtcVirtualNotifyEvent
,
678 &gEfiEventVirtualAddressChangeGuid
,
679 &mRtcVirtualAddrChangeEvent
681 ASSERT_EFI_ERROR (Status
);