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
;
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 Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to EFI_TIME
117 IN UINTN EpochSeconds
,
138 J
= (EpochSeconds
/ 86400) + 2440588;
142 c
= (((dg
/ 36524) + 1) * 3) / 4;
143 dc
= dg
- (c
* 36524);
146 a
= (((db
/ 365) + 1) * 3) / 4;
148 y
= (g
* 400) + (c
* 100) + (b
* 4) + a
;
149 m
= (((da
* 5) + 308) / 153) - 2;
150 d
= da
- (((m
+ 4) * 153) / 5) + 122;
152 Time
->Year
= y
- 4800 + ((m
+ 2) / 12);
153 Time
->Month
= ((m
+ 2) % 12) + 1;
156 ss
= EpochSeconds
% 60;
157 a
= (EpochSeconds
- ss
) / 60;
165 Time
->Nanosecond
= 0;
170 Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC)
180 UINTN JulianDate
; // Absolute Julian Date representation of the supplied Time
181 UINTN EpochDays
; // Number of days elapsed since EPOCH_JULIAN_DAY
184 a
= (14 - Time
->Month
) / 12 ;
185 y
= Time
->Year
+ 4800 - a
;
186 m
= Time
->Month
+ (12*a
) - 3;
188 JulianDate
= Time
->Day
+ ((153*m
+ 2)/5) + (365*y
) + (y
/4) - (y
/100) + (y
/400) - 32045;
190 ASSERT (JulianDate
>= EPOCH_JULIAN_DATE
);
191 EpochDays
= JulianDate
- EPOCH_JULIAN_DATE
;
193 EpochSeconds
= (EpochDays
* SEC_PER_DAY
) + ((UINTN
)Time
->Hour
* SEC_PER_HOUR
) + (Time
->Minute
* SEC_PER_MIN
) + Time
->Second
;
203 if (Time
->Year
% 4 == 0) {
204 if (Time
->Year
% 100 == 0) {
205 if (Time
->Year
% 400 == 0) {
223 STATIC CONST INTN DayOfMonth
[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
226 Time
->Day
> DayOfMonth
[Time
->Month
- 1] ||
227 (Time
->Month
== 2 && (!IsLeapYear (Time
) && Time
->Day
> 28))
236 Returns the current time and date information, and the time-keeping capabilities
237 of the hardware platform.
239 @param Time A pointer to storage to receive a snapshot of the current time.
240 @param Capabilities An optional pointer to a buffer to receive the real time clock
241 device's capabilities.
243 @retval EFI_SUCCESS The operation completed successfully.
244 @retval EFI_INVALID_PARAMETER Time is NULL.
245 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
246 @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.
253 OUT EFI_TIME_CAPABILITIES
*Capabilities
256 EFI_STATUS Status
= EFI_SUCCESS
;
262 // Initialize the hardware if not already done
263 if (!mPL031Initialized
) {
264 Status
= InitializePL031 ();
265 if (EFI_ERROR (Status
)) {
270 // Snapshot the time as early in the function call as possible
271 // On some platforms we may have access to a battery backed up hardware clock.
272 // If such RTC exists try to use it first.
273 Status
= ArmPlatformSysConfigGet (SYS_CFG_RTC
, &EpochSeconds
);
274 if (Status
== EFI_UNSUPPORTED
) {
275 // Battery backed up hardware RTC does not exist, revert to PL031
276 EpochSeconds
= MmioRead32 (mPL031RtcBase
+ PL031_RTC_DR_DATA_REGISTER
);
277 Status
= EFI_SUCCESS
;
278 } else if (EFI_ERROR (Status
)) {
279 // Battery backed up hardware RTC exists but could not be read due to error. Abort.
282 // Battery backed up hardware RTC exists and we read the time correctly from it.
283 // Now sync the PL031 to the new time.
284 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
287 // Ensure Time is a valid pointer
289 Status
= EFI_INVALID_PARAMETER
;
293 // Get the current time zone information from non-volatile storage
294 Size
= sizeof (TimeZone
);
295 Status
= EfiGetVariable (
296 (CHAR16
*)mTimeZoneVariableName
,
303 if (EFI_ERROR (Status
)) {
304 ASSERT(Status
!= EFI_INVALID_PARAMETER
);
305 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
307 if (Status
!= EFI_NOT_FOUND
)
310 // The time zone variable does not exist in non-volatile storage, so create it.
311 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
313 Status
= EfiSetVariable (
314 (CHAR16
*)mTimeZoneVariableName
,
316 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
318 (VOID
*)&(Time
->TimeZone
)
320 if (EFI_ERROR (Status
)) {
323 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
324 mTimeZoneVariableName
,
331 Time
->TimeZone
= TimeZone
;
333 // Check TimeZone bounds: -1440 to 1440 or 2047
334 if (((Time
->TimeZone
< -1440) || (Time
->TimeZone
> 1440))
335 && (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
)) {
336 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
339 // Adjust for the correct time zone
340 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
341 EpochSeconds
+= Time
->TimeZone
* SEC_PER_MIN
;
345 // Get the current daylight information from non-volatile storage
346 Size
= sizeof (Daylight
);
347 Status
= EfiGetVariable (
348 (CHAR16
*)mDaylightVariableName
,
355 if (EFI_ERROR (Status
)) {
356 ASSERT(Status
!= EFI_INVALID_PARAMETER
);
357 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
359 if (Status
!= EFI_NOT_FOUND
)
362 // The daylight variable does not exist in non-volatile storage, so create it.
365 Status
= EfiSetVariable (
366 (CHAR16
*)mDaylightVariableName
,
368 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
370 (VOID
*)&(Time
->Daylight
)
372 if (EFI_ERROR (Status
)) {
375 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
376 mDaylightVariableName
,
382 // Got the daylight information
383 Time
->Daylight
= Daylight
;
385 // Adjust for the correct period
386 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
387 // Convert to adjusted time, i.e. spring forwards one hour
388 EpochSeconds
+= SEC_PER_HOUR
;
392 // Convert from internal 32-bit time to UEFI time
393 EpochToEfiTime (EpochSeconds
, Time
);
395 // Update the Capabilities info
396 if (Capabilities
!= NULL
) {
397 // PL031 runs at frequency 1Hz
398 Capabilities
->Resolution
= PL031_COUNTS_PER_SECOND
;
399 // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000
400 Capabilities
->Accuracy
= (UINT32
)PcdGet32 (PcdPL031RtcPpmAccuracy
);
401 // FALSE: Setting the time does not clear the values below the resolution level
402 Capabilities
->SetsToZero
= FALSE
;
411 Sets the current local time and date information.
413 @param Time A pointer to the current time.
415 @retval EFI_SUCCESS The operation completed successfully.
416 @retval EFI_INVALID_PARAMETER A time field is out of range.
417 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
429 // Check the input parameters are within the range specified by UEFI
430 if ((Time
->Year
< 1900) ||
431 (Time
->Year
> 9999) ||
432 (Time
->Month
< 1 ) ||
433 (Time
->Month
> 12 ) ||
434 (!DayValid (Time
) ) ||
435 (Time
->Hour
> 23 ) ||
436 (Time
->Minute
> 59 ) ||
437 (Time
->Second
> 59 ) ||
438 (Time
->Nanosecond
> 999999999) ||
439 (!((Time
->TimeZone
== EFI_UNSPECIFIED_TIMEZONE
) || ((Time
->TimeZone
>= -1440) && (Time
->TimeZone
<= 1440)))) ||
440 (Time
->Daylight
& (~(EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
)))
442 Status
= EFI_INVALID_PARAMETER
;
446 // Because the PL031 is a 32-bit counter counting seconds,
447 // the maximum time span is just over 136 years.
448 // Time is stored in Unix Epoch format, so it starts in 1970,
449 // Therefore it can not exceed the year 2106.
450 if ((Time
->Year
< 1970) || (Time
->Year
>= 2106)) {
451 Status
= EFI_UNSUPPORTED
;
455 // Initialize the hardware if not already done
456 if (!mPL031Initialized
) {
457 Status
= InitializePL031 ();
458 if (EFI_ERROR (Status
)) {
463 EpochSeconds
= EfiTimeToEpoch (Time
);
465 // Adjust for the correct time zone, i.e. convert to UTC time zone
466 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
467 EpochSeconds
-= Time
->TimeZone
* SEC_PER_MIN
;
470 // TODO: Automatic Daylight activation
472 // Adjust for the correct period
473 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
474 // Convert to un-adjusted time, i.e. fall back one hour
475 EpochSeconds
-= SEC_PER_HOUR
;
478 // On some platforms we may have access to a battery backed up hardware clock.
480 // If such RTC exists then it must be updated first, before the PL031,
481 // to minimise any time drift. This is important because the battery backed-up
482 // RTC maintains the master time for the platform across reboots.
484 // If such RTC does not exist then the following function returns UNSUPPORTED.
485 Status
= ArmPlatformSysConfigSet (SYS_CFG_RTC
, EpochSeconds
);
486 if ((EFI_ERROR (Status
)) && (Status
!= EFI_UNSUPPORTED
)){
487 // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.
493 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
495 // The accesses to Variable Services can be very slow, because we may be writing to Flash.
496 // Do this after having set the RTC.
498 // Save the current time zone information into non-volatile storage
499 Status
= EfiSetVariable (
500 (CHAR16
*)mTimeZoneVariableName
,
502 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
503 sizeof (Time
->TimeZone
),
504 (VOID
*)&(Time
->TimeZone
)
506 if (EFI_ERROR (Status
)) {
509 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
510 mTimeZoneVariableName
,
516 // Save the current daylight information into non-volatile storage
517 Status
= EfiSetVariable (
518 (CHAR16
*)mDaylightVariableName
,
520 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
521 sizeof(Time
->Daylight
),
522 (VOID
*)&(Time
->Daylight
)
524 if (EFI_ERROR (Status
)) {
527 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
528 mDaylightVariableName
,
540 Returns the current wakeup alarm clock setting.
542 @param Enabled Indicates if the alarm is currently enabled or disabled.
543 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
544 @param Time The current alarm setting.
546 @retval EFI_SUCCESS The alarm settings were returned.
547 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
548 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
554 OUT BOOLEAN
*Enabled
,
555 OUT BOOLEAN
*Pending
,
559 // Not a required feature
560 return EFI_UNSUPPORTED
;
565 Sets the system wakeup alarm clock time.
567 @param Enabled Enable or disable the wakeup alarm.
568 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
570 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
571 Enable is FALSE, then the wakeup alarm was disabled.
572 @retval EFI_INVALID_PARAMETER A time field is out of range.
573 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
574 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
584 // Not a required feature
585 return EFI_UNSUPPORTED
;
589 Fixup internal data so that EFI can be call in virtual mode.
590 Call the passed in Child Notify event and convert any pointers in
593 @param[in] Event The Event that is being processed
594 @param[in] Context Event Context
598 LibRtcVirtualNotifyEvent (
604 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
605 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
606 // to virtual address. After the OS transitions to calling in virtual mode, all future
607 // runtime calls will be made in virtual mode.
609 EfiConvertPointer (0x0, (VOID
**)&mPL031RtcBase
);
614 This is the declaration of an EFI image entry point. This can be the entry point to an application
615 written to this specification, an EFI boot service driver, or an EFI runtime driver.
617 @param ImageHandle Handle that identifies the loaded image.
618 @param SystemTable System Table for this image.
620 @retval EFI_SUCCESS The operation completed successfully.
626 IN EFI_HANDLE ImageHandle
,
627 IN EFI_SYSTEM_TABLE
*SystemTable
633 // Initialize RTC Base Address
634 mPL031RtcBase
= PcdGet32 (PcdPL031RtcBase
);
636 // Declare the controller as EFI_MEMORY_RUNTIME
637 Status
= gDS
->AddMemorySpace (
638 EfiGcdMemoryTypeMemoryMappedIo
,
639 mPL031RtcBase
, SIZE_4KB
,
640 EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
642 if (EFI_ERROR (Status
)) {
646 Status
= gDS
->SetMemorySpaceAttributes (mPL031RtcBase
, SIZE_4KB
, EFI_MEMORY_UC
| EFI_MEMORY_RUNTIME
);
647 if (EFI_ERROR (Status
)) {
651 // Install the protocol
653 Status
= gBS
->InstallMultipleProtocolInterfaces (
655 &gEfiRealTimeClockArchProtocolGuid
, NULL
,
658 ASSERT_EFI_ERROR (Status
);
661 // Register for the virtual address change event
663 Status
= gBS
->CreateEventEx (
666 LibRtcVirtualNotifyEvent
,
668 &gEfiEventVirtualAddressChangeGuid
,
669 &mRtcVirtualAddrChangeEvent
671 ASSERT_EFI_ERROR (Status
);