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>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 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/ArmPlatformSysConfigLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/UefiRuntimeServicesTableLib.h>
30 #include <Protocol/RealTimeClock.h>
31 #include <Guid/GlobalVariable.h>
32 #include <ArmPlatform.h>
33 #include <Drivers/PL031RealTimeClock.h>
35 CHAR16 mTimeZoneVariableName
[] = L
"PL031_TimeZone";
36 CHAR16 mDaylightVariableName
[] = L
"PL031_Daylight";
37 BOOLEAN mPL031Initialized
= FALSE
;
46 // Check if this is a PrimeCell Peripheral
47 if( ( MmioRead8( PL031_RTC_PCELL_ID0
) != 0x0D )
48 || ( MmioRead8( PL031_RTC_PCELL_ID1
) != 0xF0 )
49 || ( MmioRead8( PL031_RTC_PCELL_ID2
) != 0x05 )
50 || ( MmioRead8( PL031_RTC_PCELL_ID3
) != 0xB1 ) ) {
51 Status
= EFI_NOT_FOUND
;
55 // Check if this PrimeCell Peripheral is the SP805 Watchdog Timer
56 if( ( MmioRead8( PL031_RTC_PERIPH_ID0
) != 0x31 )
57 || ( MmioRead8( PL031_RTC_PERIPH_ID1
) != 0x10 )
58 || (( MmioRead8( PL031_RTC_PERIPH_ID2
) & 0xF) != 0x04 )
59 || ( MmioRead8( PL031_RTC_PERIPH_ID3
) != 0x00 ) ) {
60 Status
= EFI_NOT_FOUND
;
77 // Prepare the hardware
78 Status
= IdentifyPL031();
79 if (EFI_ERROR (Status
)) {
83 // Ensure interrupts are masked. We do not want RTC interrupts in UEFI
84 if ( (MmioRead32( PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER
) & PL031_SET_IRQ_MASK
) != PL031_SET_IRQ_MASK
) {
85 MmioOr32( PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER
, PL031_SET_IRQ_MASK
);
88 // Clear any existing interrupts
89 if ( (MmioRead32( PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER
) & PL031_IRQ_TRIGGERED
) == PL031_IRQ_TRIGGERED
) {
90 MmioOr32( PL031_RTC_ICR_IRQ_CLEAR_REGISTER
, PL031_CLEAR_IRQ
);
93 // Start the clock counter
94 if ( (MmioRead32( PL031_RTC_CR_CONTROL_REGISTER
) & PL031_RTC_ENABLED
) != PL031_RTC_ENABLED
) {
95 MmioOr32( PL031_RTC_CR_CONTROL_REGISTER
, PL031_RTC_ENABLED
);
98 mPL031Initialized
= TRUE
;
105 Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to EFI_TIME
109 IN UINTN EpochSeconds
,
130 if( Time
->Daylight
== TRUE
) {
134 J
= (EpochSeconds
/ 86400) + 2440588;
138 c
= (((dg
/ 36524) + 1) * 3) / 4;
139 dc
= dg
- (c
* 36524);
142 a
= (((db
/ 365) + 1) * 3) / 4;
144 y
= (g
* 400) + (c
* 100) + (b
* 4) + a
;
145 m
= (((da
* 5) + 308) / 153) - 2;
146 d
= da
- (((m
+ 4) * 153) / 5) + 122;
148 Time
->Year
= y
- 4800 + ((m
+ 2) / 12);
149 Time
->Month
= ((m
+ 2) % 12) + 1;
152 ss
= EpochSeconds
% 60;
153 a
= (EpochSeconds
- ss
) / 60;
161 Time
->Nanosecond
= 0;
166 Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC)
176 UINTN JulianDate
; // Absolute Julian Date representation of the supplied Time
177 UINTN EpochDays
; // Number of days elapsed since EPOCH_JULIAN_DAY
180 a
= (14 - Time
->Month
) / 12 ;
181 y
= Time
->Year
+ 4800 - a
;
182 m
= Time
->Month
+ (12*a
) - 3;
184 JulianDate
= Time
->Day
+ ((153*m
+ 2)/5) + (365*y
) + (y
/4) - (y
/100) + (y
/400) - 32045;
186 ASSERT( JulianDate
> EPOCH_JULIAN_DATE
);
187 EpochDays
= JulianDate
- EPOCH_JULIAN_DATE
;
189 EpochSeconds
= (EpochDays
* SEC_PER_DAY
) + ((UINTN
)Time
->Hour
* SEC_PER_HOUR
) + (Time
->Minute
* SEC_PER_MIN
) + Time
->Second
;
199 if (Time
->Year
% 4 == 0) {
200 if (Time
->Year
% 100 == 0) {
201 if (Time
->Year
% 400 == 0) {
219 INTN DayOfMonth
[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
222 Time
->Day
> DayOfMonth
[Time
->Month
- 1] ||
223 (Time
->Month
== 2 && (!IsLeapYear (Time
) && Time
->Day
> 28))
232 Returns the current time and date information, and the time-keeping capabilities
233 of the hardware platform.
235 @param Time A pointer to storage to receive a snapshot of the current time.
236 @param Capabilities An optional pointer to a buffer to receive the real time clock
237 device's capabilities.
239 @retval EFI_SUCCESS The operation completed successfully.
240 @retval EFI_INVALID_PARAMETER Time is NULL.
241 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
248 OUT EFI_TIME_CAPABILITIES
*Capabilities
251 EFI_STATUS Status
= EFI_SUCCESS
;
256 // Initialize the hardware if not already done
257 if( !mPL031Initialized
) {
258 Status
= InitializePL031();
259 if (EFI_ERROR (Status
)) {
264 // Snapshot the time as early in the function call as possible
265 // On some platforms we may have access to a battery backed up hardware clock.
266 // If such RTC exists try to use it first.
267 Status
= ArmPlatformSysConfigGet (SYS_CFG_RTC
, &EpochSeconds
);
268 if (Status
== EFI_UNSUPPORTED
) {
269 // Battery backed up hardware RTC does not exist, revert to PL031
270 EpochSeconds
= MmioRead32( PL031_RTC_DR_DATA_REGISTER
);
271 Status
= EFI_SUCCESS
;
272 } else if (EFI_ERROR (Status
)) {
273 // Battery backed up hardware RTC exists but could not be read due to error. Abort.
276 // Battery backed up hardware RTC exists and we read the time correctly from it.
277 // Now sync the PL031 to the new time.
278 MmioWrite32( PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
281 // Ensure Time is a valid pointer
283 Status
= EFI_INVALID_PARAMETER
;
287 // Get the current time zone information from non-volatile storage
288 TimeZone
= (INT16
*)GetVariable(mTimeZoneVariableName
, &gEfiGlobalVariableGuid
);
290 if( TimeZone
== NULL
) {
291 // The time zone variable does not exist in non-volatile storage, so create it.
292 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
294 Status
= gRT
->SetVariable (
295 mTimeZoneVariableName
,
296 &gEfiGlobalVariableGuid
,
297 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
298 sizeof(Time
->TimeZone
),
301 if (EFI_ERROR (Status
)) {
302 DEBUG((EFI_D_ERROR
,"LibGetTime: ERROR: TimeZone\n"));
307 Time
->TimeZone
= *TimeZone
;
310 // Check TimeZone bounds: -1440 to 1440 or 2047
311 if( (( Time
->TimeZone
< -1440 ) || ( Time
->TimeZone
> 1440 ))
312 && ( Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) ) {
313 Time
->TimeZone
= EFI_UNSPECIFIED_TIMEZONE
;
316 // Adjust for the correct time zone
317 if( Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
318 EpochSeconds
+= Time
->TimeZone
* SEC_PER_MIN
;
322 // Get the current daylight information from non-volatile storage
323 Daylight
= (UINTN
*)GetVariable(mDaylightVariableName
, &gEfiGlobalVariableGuid
);
325 if( Daylight
== NULL
) {
326 // The daylight variable does not exist in non-volatile storage, so create it.
329 Status
= gRT
->SetVariable (
330 mDaylightVariableName
,
331 &gEfiGlobalVariableGuid
,
332 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
333 sizeof(Time
->Daylight
),
336 if (EFI_ERROR (Status
)) {
337 DEBUG((EFI_D_ERROR
,"LibGetTime: ERROR: Daylight\n"));
341 // Got the daylight information
342 Time
->Daylight
= *Daylight
;
345 // Adjust for the correct period
346 if( (Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
347 // Convert to adjusted time, i.e. spring forwards one hour
348 EpochSeconds
+= SEC_PER_HOUR
;
352 // Convert from internal 32-bit time to UEFI time
353 EpochToEfiTime( EpochSeconds
, Time
);
355 // Update the Capabilities info
356 if( Capabilities
!= NULL
) {
357 Capabilities
->Resolution
= PL031_COUNTS_PER_SECOND
; /* PL031 runs at frequency 1Hz */
358 Capabilities
->Accuracy
= PL031_PPM_ACCURACY
; /* Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000 */
359 Capabilities
->SetsToZero
= FALSE
; /* FALSE: Setting the time does not clear the values below the resolution level */
368 Sets the current local time and date information.
370 @param Time A pointer to the current time.
372 @retval EFI_SUCCESS The operation completed successfully.
373 @retval EFI_INVALID_PARAMETER A time field is out of range.
374 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
386 // Because the PL031 is a 32-bit counter counting seconds,
387 // the maximum time span is just over 136 years.
388 // Time is stored in Unix Epoch format, so it starts in 1970,
389 // Therefore it can not exceed the year 2106.
390 // This is not a problem for UEFI, as the current spec limits the years
391 // to the range 1998 .. 2011
393 // Check the input parameters' range.
394 if ( ( Time
->Year
< 1998 ) ||
395 ( Time
->Year
> 2099 ) ||
396 ( Time
->Month
< 1 ) ||
397 ( Time
->Month
> 12 ) ||
398 (!DayValid (Time
) ) ||
399 ( Time
->Hour
> 23 ) ||
400 ( Time
->Minute
> 59 ) ||
401 ( Time
->Second
> 59 ) ||
402 ( Time
->Nanosecond
> 999999999 ) ||
403 ( !((Time
->TimeZone
== EFI_UNSPECIFIED_TIMEZONE
) || ((Time
->TimeZone
>= -1440) && (Time
->TimeZone
<= 1440))) ) ||
404 ( Time
->Daylight
& (~(EFI_TIME_ADJUST_DAYLIGHT
| EFI_TIME_IN_DAYLIGHT
)) )
406 Status
= EFI_INVALID_PARAMETER
;
410 // Initialize the hardware if not already done
411 if( !mPL031Initialized
) {
412 Status
= InitializePL031();
413 if (EFI_ERROR (Status
)) {
418 EpochSeconds
= EfiTimeToEpoch( Time
);
420 // Adjust for the correct time zone, i.e. convert to UTC time zone
421 if( Time
->TimeZone
!= EFI_UNSPECIFIED_TIMEZONE
) {
422 EpochSeconds
-= Time
->TimeZone
* SEC_PER_MIN
;
425 // TODO: Automatic Daylight activation
427 // Adjust for the correct period
428 if( (Time
->Daylight
& EFI_TIME_IN_DAYLIGHT
) == EFI_TIME_IN_DAYLIGHT
) {
429 // Convert to un-adjusted time, i.e. fall back one hour
430 EpochSeconds
-= SEC_PER_HOUR
;
433 // On some platforms we may have access to a battery backed up hardware clock.
435 // If such RTC exists then it must be updated first, before the PL031,
436 // to minimise any time drift. This is important because the battery backed-up
437 // RTC maintains the master time for the platform across reboots.
439 // If such RTC does not exist then the following function returns UNSUPPORTED.
440 Status
= ArmPlatformSysConfigSet (SYS_CFG_RTC
, EpochSeconds
);
441 if ((EFI_ERROR (Status
)) && (Status
!= EFI_UNSUPPORTED
)){
442 // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.
448 MmioWrite32( PL031_RTC_LR_LOAD_REGISTER
, EpochSeconds
);
450 // The accesses to Variable Services can be very slow, because we may be writing to Flash.
451 // Do this after having set the RTC.
453 // Save the current time zone information into non-volatile storage
454 Status
= gRT
->SetVariable (
455 mTimeZoneVariableName
,
456 &gEfiGlobalVariableGuid
,
457 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
458 sizeof(Time
->TimeZone
),
461 if (EFI_ERROR (Status
)) {
462 DEBUG((EFI_D_ERROR
,"LibSetTime: ERROR: TimeZone\n"));
466 // Save the current daylight information into non-volatile storage
467 Status
= gRT
->SetVariable (
468 mDaylightVariableName
,
469 &gEfiGlobalVariableGuid
,
470 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
471 sizeof(Time
->Daylight
),
474 if (EFI_ERROR (Status
)) {
475 DEBUG((EFI_D_ERROR
,"LibSetTime: ERROR: Daylight\n"));
485 Returns the current wakeup alarm clock setting.
487 @param Enabled Indicates if the alarm is currently enabled or disabled.
488 @param Pending Indicates if the alarm signal is pending and requires acknowledgement.
489 @param Time The current alarm setting.
491 @retval EFI_SUCCESS The alarm settings were returned.
492 @retval EFI_INVALID_PARAMETER Any parameter is NULL.
493 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
499 OUT BOOLEAN
*Enabled
,
500 OUT BOOLEAN
*Pending
,
504 // Not a required feature
505 return EFI_UNSUPPORTED
;
510 Sets the system wakeup alarm clock time.
512 @param Enabled Enable or disable the wakeup alarm.
513 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
515 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled. If
516 Enable is FALSE, then the wakeup alarm was disabled.
517 @retval EFI_INVALID_PARAMETER A time field is out of range.
518 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
519 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
529 // Not a required feature
530 return EFI_UNSUPPORTED
;
536 This is the declaration of an EFI image entry point. This can be the entry point to an application
537 written to this specification, an EFI boot service driver, or an EFI runtime driver.
539 @param ImageHandle Handle that identifies the loaded image.
540 @param SystemTable System Table for this image.
542 @retval EFI_SUCCESS The operation completed successfully.
548 IN EFI_HANDLE ImageHandle
,
549 IN EFI_SYSTEM_TABLE
*SystemTable
555 // Setup the setters and getters
556 gRT
->GetTime
= LibGetTime
;
557 gRT
->SetTime
= LibSetTime
;
558 gRT
->GetWakeupTime
= LibGetWakeupTime
;
559 gRT
->SetWakeupTime
= LibSetWakeupTime
;
561 // Install the protocol
563 Status
= gBS
->InstallMultipleProtocolInterfaces (
565 &gEfiRealTimeClockArchProtocolGuid
, NULL
,
574 Fixup internal data so that EFI can be call in virtual mode.
575 Call the passed in Child Notify event and convert any pointers in
578 @param[in] Event The Event that is being processed
579 @param[in] Context Event Context
583 LibRtcVirtualNotifyEvent (
589 // Only needed if you are going to support the OS calling RTC functions in virtual mode.
590 // You will need to call EfiConvertPointer (). To convert any stored physical addresses
591 // to virtual address. After the OS transitions to calling in virtual mode, all future
592 // runtime calls will be made in virtual mode.