2 Implement EFI RealTimeClock runtime services via RTC Lib.
4 Currently this driver does not support runtime virtual calling.
6 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
7 Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Library/BaseLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/UefiLib.h>
24 #include <Library/IoLib.h>
25 #include <Library/RealTimeClockLib.h>
26 #include <Library/MemoryAllocationLib.h>
27 #include <Library/PcdLib.h>
28 #include <Library/ArmPlatformSysConfigLib.h>
29 #include <Library/DxeServicesTableLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/UefiRuntimeServicesTableLib.h>
32 #include <Library/UefiRuntimeLib.h>
34 #include <Protocol/RealTimeClock.h>
36 #include <Guid/GlobalVariable.h>
37 #include <Guid/EventGroup.h>
39 #include <Drivers/PL031RealTimeClock.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 Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to EFI_TIME
119 IN UINTN EpochSeconds
,
140 J
= (EpochSeconds
/ 86400) + 2440588;
144 c
= (((dg
/ 36524) + 1) * 3) / 4;
145 dc
= dg
- (c
* 36524);
148 a
= (((db
/ 365) + 1) * 3) / 4;
150 y
= (g
* 400) + (c
* 100) + (b
* 4) + a
;
151 m
= (((da
* 5) + 308) / 153) - 2;
152 d
= da
- (((m
+ 4) * 153) / 5) + 122;
154 Time
->Year
= y
- 4800 + ((m
+ 2) / 12);
155 Time
->Month
= ((m
+ 2) % 12) + 1;
158 ss
= EpochSeconds
% 60;
159 a
= (EpochSeconds
- ss
) / 60;
167 Time
->Nanosecond
= 0;
172 Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC)
182 UINTN JulianDate
; // Absolute Julian Date representation of the supplied Time
183 UINTN EpochDays
; // Number of days elapsed since EPOCH_JULIAN_DAY
186 a
= (14 - Time
->Month
) / 12 ;
187 y
= Time
->Year
+ 4800 - a
;
188 m
= Time
->Month
+ (12*a
) - 3;
190 JulianDate
= Time
->Day
+ ((153*m
+ 2)/5) + (365*y
) + (y
/4) - (y
/100) + (y
/400) - 32045;
192 ASSERT (JulianDate
>= EPOCH_JULIAN_DATE
);
193 EpochDays
= JulianDate
- EPOCH_JULIAN_DATE
;
195 EpochSeconds
= (EpochDays
* SEC_PER_DAY
) + ((UINTN
)Time
->Hour
* SEC_PER_HOUR
) + (Time
->Minute
* SEC_PER_MIN
) + Time
->Second
;
205 if (Time
->Year
% 4 == 0) {
206 if (Time
->Year
% 100 == 0) {
207 if (Time
->Year
% 400 == 0) {
225 INTN DayOfMonth
[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
228 Time
->Day
> DayOfMonth
[Time
->Month
- 1] ||
229 (Time
->Month
== 2 && (!IsLeapYear (Time
) && Time
->Day
> 28))
238 Returns the current time and date information, and the time-keeping capabilities
239 of the hardware platform.
241 @param Time A pointer to storage to receive a snapshot of the current time.
242 @param Capabilities An optional pointer to a buffer to receive the real time clock
243 device's capabilities.
245 @retval EFI_SUCCESS The operation completed successfully.
246 @retval EFI_INVALID_PARAMETER Time is NULL.
247 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
248 @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.
255 OUT EFI_TIME_CAPABILITIES
*Capabilities
258 EFI_STATUS Status
= EFI_SUCCESS
;
264 // Initialize the hardware if not already done
265 if (!mPL031Initialized
) {
266 Status
= InitializePL031 ();
267 if (EFI_ERROR (Status
)) {
272 // Snapshot the time as early in the function call as possible
273 // On some platforms we may have access to a battery backed up hardware clock.
274 // If such RTC exists try to use it first.
275 Status
= ArmPlatformSysConfigGet (SYS_CFG_RTC
, &EpochSeconds
);
276 if (Status
== EFI_UNSUPPORTED
) {
277 // Battery backed up hardware RTC does not exist, revert to PL031
278 EpochSeconds
= MmioRead32 (mPL031RtcBase
+ PL031_RTC_DR_DATA_REGISTER
);
279 Status
= EFI_SUCCESS
;
280 } else if (EFI_ERROR (Status
)) {
281 // Battery backed up hardware RTC exists but could not be read due to error. Abort.
284 // Battery backed up hardware RTC exists and we read the time correctly from it.
285 // Now sync the PL031 to the new time.
286 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
289 // Ensure Time is a valid pointer
291 Status
= EFI_INVALID_PARAMETER
;
295 // Get the current time zone information from non-volatile storage
296 Size
= sizeof (TimeZone
);
297 Status
= gRT
->GetVariable (
298 (CHAR16
*)mTimeZoneVariableName
,
305 if (EFI_ERROR (Status
)) {
306 ASSERT(Status
!= EFI_INVALID_PARAMETER
);
307 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
309 if (Status
!= EFI_NOT_FOUND
)
312 // The time zone variable does not exist in non-volatile storage, so create it.
313 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
315 Status
= gRT
->SetVariable (
316 (CHAR16
*)mTimeZoneVariableName
,
318 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
320 (VOID
*)&(Time
->TimeZone
)
322 if (EFI_ERROR (Status
)) {
325 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
326 mTimeZoneVariableName
,
333 Time
->TimeZone
= TimeZone
;
335 // Check TimeZone bounds: -1440 to 1440 or 2047
336 if (((Time
->TimeZone
< -1440) || (Time
->TimeZone
> 1440))
337 && (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
)) {
338 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
341 // Adjust for the correct time zone
342 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
343 EpochSeconds
+= Time
->TimeZone
* SEC_PER_MIN
;
347 // Get the current daylight information from non-volatile storage
348 Size
= sizeof (Daylight
);
349 Status
= gRT
->GetVariable (
350 (CHAR16
*)mDaylightVariableName
,
357 if (EFI_ERROR (Status
)) {
358 ASSERT(Status
!= EFI_INVALID_PARAMETER
);
359 ASSERT(Status
!= EFI_BUFFER_TOO_SMALL
);
361 if (Status
!= EFI_NOT_FOUND
)
364 // The daylight variable does not exist in non-volatile storage, so create it.
367 Status
= gRT
->SetVariable (
368 (CHAR16
*)mDaylightVariableName
,
370 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
372 (VOID
*)&(Time
->Daylight
)
374 if (EFI_ERROR (Status
)) {
377 "LibGetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
378 mDaylightVariableName
,
384 // Got the daylight information
385 Time
->Daylight
= Daylight
;
387 // Adjust for the correct period
388 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
389 // Convert to adjusted time, i.e. spring forwards one hour
390 EpochSeconds
+= SEC_PER_HOUR
;
394 // Convert from internal 32-bit time to UEFI time
395 EpochToEfiTime (EpochSeconds
, Time
);
397 // Update the Capabilities info
398 if (Capabilities
!= NULL
) {
399 // PL031 runs at frequency 1Hz
400 Capabilities
->Resolution
= PL031_COUNTS_PER_SECOND
;
401 // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000
402 Capabilities
->Accuracy
= (UINT32
)PcdGet32 (PcdPL031RtcPpmAccuracy
);
403 // FALSE: Setting the time does not clear the values below the resolution level
404 Capabilities
->SetsToZero
= FALSE
;
413 Sets the current local time and date information.
415 @param Time A pointer to the current time.
417 @retval EFI_SUCCESS The operation completed successfully.
418 @retval EFI_INVALID_PARAMETER A time field is out of range.
419 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
431 // Check the input parameters are within the range specified by UEFI
432 if ((Time
->Year
< 1900) ||
433 (Time
->Year
> 9999) ||
434 (Time
->Month
< 1 ) ||
435 (Time
->Month
> 12 ) ||
436 (!DayValid (Time
) ) ||
437 (Time
->Hour
> 23 ) ||
438 (Time
->Minute
> 59 ) ||
439 (Time
->Second
> 59 ) ||
440 (Time
->Nanosecond
> 999999999) ||
441 (!((Time
->TimeZone
== EFI_UNSPECIFIED_TIMEZONE
) || ((Time
->TimeZone
>= -1440) && (Time
->TimeZone
<= 1440)))) ||
442 (Time
->Daylight
& (~(EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
)))
444 Status
= EFI_INVALID_PARAMETER
;
448 // Because the PL031 is a 32-bit counter counting seconds,
449 // the maximum time span is just over 136 years.
450 // Time is stored in Unix Epoch format, so it starts in 1970,
451 // Therefore it can not exceed the year 2106.
452 if ((Time
->Year
< 1970) || (Time
->Year
>= 2106)) {
453 Status
= EFI_UNSUPPORTED
;
457 // Initialize the hardware if not already done
458 if (!mPL031Initialized
) {
459 Status
= InitializePL031 ();
460 if (EFI_ERROR (Status
)) {
465 EpochSeconds
= EfiTimeToEpoch (Time
);
467 // Adjust for the correct time zone, i.e. convert to UTC time zone
468 if (Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
469 EpochSeconds
-= Time
->TimeZone
* SEC_PER_MIN
;
472 // TODO: Automatic Daylight activation
474 // Adjust for the correct period
475 if ((Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
476 // Convert to un-adjusted time, i.e. fall back one hour
477 EpochSeconds
-= SEC_PER_HOUR
;
480 // On some platforms we may have access to a battery backed up hardware clock.
482 // If such RTC exists then it must be updated first, before the PL031,
483 // to minimise any time drift. This is important because the battery backed-up
484 // RTC maintains the master time for the platform across reboots.
486 // If such RTC does not exist then the following function returns UNSUPPORTED.
487 Status
= ArmPlatformSysConfigSet (SYS_CFG_RTC
, EpochSeconds
);
488 if ((EFI_ERROR (Status
)) && (Status
!= EFI_UNSUPPORTED
)){
489 // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.
495 MmioWrite32 (mPL031RtcBase
+ PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
497 // The accesses to Variable Services can be very slow, because we may be writing to Flash.
498 // Do this after having set the RTC.
500 // Save the current time zone information into non-volatile storage
501 Status
= gRT
->SetVariable (
502 (CHAR16
*)mTimeZoneVariableName
,
504 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
505 sizeof (Time
->TimeZone
),
506 (VOID
*)&(Time
->TimeZone
)
508 if (EFI_ERROR (Status
)) {
511 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
512 mTimeZoneVariableName
,
518 // Save the current daylight information into non-volatile storage
519 Status
= gRT
->SetVariable (
520 (CHAR16
*)mDaylightVariableName
,
522 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
523 sizeof(Time
->Daylight
),
524 (VOID
*)&(Time
->Daylight
)
526 if (EFI_ERROR (Status
)) {
529 "LibSetTime: Failed to save %s variable to non-volatile storage, Status = %r\n",
530 mDaylightVariableName
,
542 Returns the current wakeup alarm clock setting.
544 @param Enabled Indicates if the alarm is currently enabled or disabled.
545 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
546 @param Time The current alarm setting.
548 @retval EFI_SUCCESS The alarm settings were returned.
549 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
550 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
556 OUT BOOLEAN
*Enabled
,
557 OUT BOOLEAN
*Pending
,
561 // Not a required feature
562 return EFI_UNSUPPORTED
;
567 Sets the system wakeup alarm clock time.
569 @param Enabled Enable or disable the wakeup alarm.
570 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
572 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
573 Enable is FALSE, then the wakeup alarm was disabled.
574 @retval EFI_INVALID_PARAMETER A time field is out of range.
575 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
576 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
586 // Not a required feature
587 return EFI_UNSUPPORTED
;
591 Fixup internal data so that EFI can be call in virtual mode.
592 Call the passed in Child Notify event and convert any pointers in
595 @param[in] Event The Event that is being processed
596 @param[in] Context Event Context
600 LibRtcVirtualNotifyEvent (
606 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
607 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
608 // to virtual address. After the OS transitions to calling in virtual mode, all future
609 // runtime calls will be made in virtual mode.
611 EfiConvertPointer (0x0, (VOID
**)&mPL031RtcBase
);
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
;
659 // Install the protocol
661 Status
= gBS
->InstallMultipleProtocolInterfaces (
663 &gEfiRealTimeClockArchProtocolGuid
, NULL
,
666 ASSERT_EFI_ERROR (Status
);
669 // Register for the virtual address change event
671 Status
= gBS
->CreateEventEx (
674 LibRtcVirtualNotifyEvent
,
676 &gEfiEventVirtualAddressChangeGuid
,
677 &mRtcVirtualAddrChangeEvent
679 ASSERT_EFI_ERROR (Status
);