]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ArmPlatformPkg/PL031RealTimeClockLib: Implement PL031 RTC drive
authoroliviermartin <oliviermartin@6f19259b-4bc3-4df7-8a09-765794883524>
Sat, 11 Jun 2011 11:20:27 +0000 (11:20 +0000)
committeroliviermartin <oliviermartin@6f19259b-4bc3-4df7-8a09-765794883524>
Sat, 11 Jun 2011 11:20:27 +0000 (11:20 +0000)
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@11793 6f19259b-4bc3-4df7-8a09-765794883524

ArmPlatformPkg/ArmRealViewEbPkg/Include/Platform/ArmPlatform.h
ArmPlatformPkg/ArmVExpressPkg/Include/Platform/CTA9x4/ArmPlatform.h
ArmPlatformPkg/Include/Drivers/PL031RealTimeClock.h [new file with mode: 0644]
ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.inf

index f053bdd9cc0e8a2cd53d1bcfbdeee43f3d52da08..bbe736c398d2bbc5ee846252caff621faf483e65 100644 (file)
@@ -92,6 +92,9 @@
 #define SP804_TIMER2_BASE                     (ARM_EB_BOARD_PERIPH_BASE + 0x12000)\r
 #define SP804_TIMER3_BASE                     (ARM_EB_BOARD_PERIPH_BASE + 0x12020)\r
 \r
+// PL301 RTC\r
+#define PL031_RTC_BASE                        (ARM_EB_BOARD_PERIPH_BASE + 0x17000)\r
+\r
 // Dynamic Memory Controller Base\r
 #define ARM_EB_DMC_BASE                     0x10018000\r
 \r
 //#define ARM_EB_L2x0_CTLR_BASE                   0x1E00A000*/\r
 \r
 \r
+// PL031 RTC - Other settings\r
+#define PL031_PPM_ACCURACY                      300000000\r
+\r
+\r
 /*******************************************\r
 // EFI Memory Map in Permanent Memory (DRAM)\r
 *******************************************/\r
index f66ff3396ac6c2661948e422108621335966d5d7..4e8c02d3fcfcd36688167426d01ba9a48e4ea4a9 100644 (file)
 #define ARM_VE_DECPROT_BIT_NMC_TZASC_LOCK       (1 << 4)\r
 #define ARM_VE_DECPROT_BIT_SMC_TZASC_LOCK       (1 << 5)\r
 \r
+// PL031 RTC - Other settings\r
+#define PL031_PPM_ACCURACY                      300000000\r
+\r
 \r
 // PL111 Lcd\r
 #define PL111_CLCD_CORE_TILE_VIDEO_MODE_OSC_ID  1\r
diff --git a/ArmPlatformPkg/Include/Drivers/PL031RealTimeClock.h b/ArmPlatformPkg/Include/Drivers/PL031RealTimeClock.h
new file mode 100644 (file)
index 0000000..8c26d66
--- /dev/null
@@ -0,0 +1,59 @@
+/** @file\r
+*\r
+*  Copyright (c) 2011, ARM Limited. All rights reserved.\r
+*\r
+*  This program and the accompanying materials\r
+*  are licensed and made available under the terms and conditions of the BSD License\r
+*  which accompanies this distribution.  The full text of the license may be found at\r
+*  http://opensource.org/licenses/bsd-license.php\r
+*\r
+*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+*\r
+**/\r
+\r
+\r
+#ifndef __PL031_REAL_TIME_CLOCK_H__\r
+#define __PL031_REAL_TIME_CLOCK_H__\r
+\r
+#include <Base.h>\r
+#include <ArmPlatform.h>\r
+\r
+// PL031 Registers\r
+#define PL031_RTC_DR_DATA_REGISTER                      (PL031_RTC_BASE + 0x000)\r
+#define PL031_RTC_MR_MATCH_REGISTER                     (PL031_RTC_BASE + 0x004)\r
+#define PL031_RTC_LR_LOAD_REGISTER                      (PL031_RTC_BASE + 0x008)\r
+#define PL031_RTC_CR_CONTROL_REGISTER                   (PL031_RTC_BASE + 0x00C)\r
+#define PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER      (PL031_RTC_BASE + 0x010)\r
+#define PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER           (PL031_RTC_BASE + 0x014)\r
+#define PL031_RTC_MIS_MASKED_IRQ_STATUS_REGISTER        (PL031_RTC_BASE + 0x018)\r
+#define PL031_RTC_ICR_IRQ_CLEAR_REGISTER                (PL031_RTC_BASE + 0x01C)\r
+#define PL031_RTC_PERIPH_ID0                            (PL031_RTC_BASE + 0xFE0)\r
+#define PL031_RTC_PERIPH_ID1                            (PL031_RTC_BASE + 0xFE4)\r
+#define PL031_RTC_PERIPH_ID2                            (PL031_RTC_BASE + 0xFE8)\r
+#define PL031_RTC_PERIPH_ID3                            (PL031_RTC_BASE + 0xFEC)\r
+#define PL031_RTC_PCELL_ID0                             (PL031_RTC_BASE + 0xFF0)\r
+#define PL031_RTC_PCELL_ID1                             (PL031_RTC_BASE + 0xFF4)\r
+#define PL031_RTC_PCELL_ID2                             (PL031_RTC_BASE + 0xFF8)\r
+#define PL031_RTC_PCELL_ID3                             (PL031_RTC_BASE + 0xFFC)\r
+\r
+// PL031 Values\r
+#define PL031_RTC_ENABLED                               0x00000001\r
+#define PL031_SET_IRQ_MASK                              0x00000001\r
+#define PL031_IRQ_TRIGGERED                             0x00000001\r
+#define PL031_CLEAR_IRQ                                 0x00000001\r
+\r
+#define PL031_COUNTS_PER_SECOND                         1\r
+\r
+// Define EPOCH (1970-JANUARY-01) in the Julian Date representation\r
+#define EPOCH_JULIAN_DATE                               2440588\r
+\r
+// Seconds per unit\r
+#define SEC_PER_MIN                                     ((UINTN)    60)\r
+#define SEC_PER_HOUR                                    ((UINTN)  3600)\r
+#define SEC_PER_DAY                                     ((UINTN) 86400)\r
+\r
+#define SEC_PER_MONTH                                   ((UINTN)  2,592,000)\r
+#define SEC_PER_YEAR                                    ((UINTN) 31,536,000)\r
+\r
+#endif\r
index fdda569e715912ff2d92d30f8ac79dec82dc714c..9bccd2f14f89fe27767056832bdaf90afc814fd2 100644 (file)
@@ -1,10 +1,10 @@
 /** @file\r
   Implement EFI RealTimeClock runtime services via RTC Lib.\r
-  \r
+\r
   Currently this driver does not support runtime virtual calling.\r
 \r
   Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
-  \r
+\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
 \r
 **/\r
 \r
+#include <Base.h>\r
+#include <Uefi.h>\r
 #include <PiDxe.h>\r
 #include <Library/BaseLib.h>\r
 #include <Library/DebugLib.h>\r
+#include <Library/UefiLib.h>\r
 #include <Library/IoLib.h>\r
 #include <Library/RealTimeClockLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/ArmPlatformSysConfigLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/UefiRuntimeServicesTableLib.h>\r
+#include <Protocol/RealTimeClock.h>\r
+#include <Guid/GlobalVariable.h>\r
+#include <ArmPlatform.h>\r
+#include <Drivers/PL031RealTimeClock.h>\r
+\r
+CHAR16        mTimeZoneVariableName[] = L"PL031_TimeZone";\r
+CHAR16        mDaylightVariableName[] = L"PL031_Daylight";\r
+BOOLEAN       mPL031Initialized = FALSE;\r
+\r
+EFI_STATUS\r
+IdentifyPL031 (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS    Status;\r
+\r
+  // Check if this is a PrimeCell Peripheral\r
+  if(    ( MmioRead8( PL031_RTC_PCELL_ID0 ) != 0x0D )\r
+      || ( MmioRead8( PL031_RTC_PCELL_ID1 ) != 0xF0 )\r
+      || ( MmioRead8( PL031_RTC_PCELL_ID2 ) != 0x05 )\r
+      || ( MmioRead8( PL031_RTC_PCELL_ID3 ) != 0xB1 ) ) {\r
+    Status = EFI_NOT_FOUND;\r
+    goto EXIT;\r
+  }\r
+\r
+  // Check if this PrimeCell Peripheral is the SP805 Watchdog Timer\r
+  if(    ( MmioRead8( PL031_RTC_PERIPH_ID0 ) != 0x31 )\r
+      || ( MmioRead8( PL031_RTC_PERIPH_ID1 ) != 0x10 )\r
+      || (( MmioRead8( PL031_RTC_PERIPH_ID2 ) & 0xF) != 0x04 )\r
+      || ( MmioRead8( PL031_RTC_PERIPH_ID3 ) != 0x00 ) ) {\r
+    Status = EFI_NOT_FOUND;\r
+    goto EXIT;\r
+  }\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+  EXIT:\r
+  return Status;\r
+}\r
+\r
+EFI_STATUS\r
+InitializePL031 (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS    Status;\r
+\r
+  // Prepare the hardware\r
+  Status = IdentifyPL031();\r
+  if (EFI_ERROR (Status)) {\r
+    goto EXIT;\r
+  }\r
+\r
+  // Ensure interrupts are masked. We do not want RTC interrupts in UEFI\r
+  if ( (MmioRead32( PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER ) & PL031_SET_IRQ_MASK) != PL031_SET_IRQ_MASK ) {\r
+    MmioOr32( PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER, PL031_SET_IRQ_MASK);\r
+  }\r
+\r
+  // Clear any existing interrupts\r
+  if ( (MmioRead32( PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER ) & PL031_IRQ_TRIGGERED) == PL031_IRQ_TRIGGERED ) {\r
+    MmioOr32( PL031_RTC_ICR_IRQ_CLEAR_REGISTER, PL031_CLEAR_IRQ);\r
+  }\r
+\r
+  // Start the clock counter\r
+  if ( (MmioRead32( PL031_RTC_CR_CONTROL_REGISTER ) & PL031_RTC_ENABLED) != PL031_RTC_ENABLED ) {\r
+    MmioOr32( PL031_RTC_CR_CONTROL_REGISTER, PL031_RTC_ENABLED);\r
+  }\r
+\r
+  mPL031Initialized = TRUE;\r
+\r
+  EXIT:\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Converts Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC) to EFI_TIME\r
+ **/\r
+VOID\r
+EpochToEfiTime (\r
+  IN  UINTN     EpochSeconds,\r
+  OUT EFI_TIME  *Time\r
+  )\r
+{\r
+  UINTN         a;\r
+  UINTN         b;\r
+  UINTN         c;\r
+  UINTN         d;\r
+  UINTN         g;\r
+  UINTN         j;\r
+  UINTN         m;\r
+  UINTN         y;\r
+  UINTN         da;\r
+  UINTN         db;\r
+  UINTN         dc;\r
+  UINTN         dg;\r
+  UINTN         hh;\r
+  UINTN         mm;\r
+  UINTN         ss;\r
+  UINTN         J;\r
+\r
+  if( Time->Daylight == TRUE) {\r
+\r
+  }\r
+\r
+  J  = (EpochSeconds / 86400) + 2440588;\r
+  j  = J + 32044;\r
+  g  = j / 146097;\r
+  dg = j % 146097;\r
+  c  = (((dg / 36524) + 1) * 3) / 4;\r
+  dc = dg - (c * 36524);\r
+  b  = dc / 1461;\r
+  db = dc % 1461;\r
+  a  = (((db / 365) + 1) * 3) / 4;\r
+  da = db - (a * 365);\r
+  y  = (g * 400) + (c * 100) + (b * 4) + a;\r
+  m  = (((da * 5) + 308) / 153) - 2;\r
+  d  = da - (((m + 4) * 153) / 5) + 122;\r
+\r
+  Time->Year  = y - 4800 + ((m + 2) / 12);\r
+  Time->Month = ((m + 2) % 12) + 1;\r
+  Time->Day   = d + 1;\r
+\r
+  ss = EpochSeconds % 60;\r
+  a  = (EpochSeconds - ss) / 60;\r
+  mm = a % 60;\r
+  b = (a - mm) / 60;\r
+  hh = b % 24;\r
+\r
+  Time->Hour        = hh;\r
+  Time->Minute      = mm;\r
+  Time->Second      = ss;\r
+  Time->Nanosecond  = 0;\r
+\r
+}\r
+\r
+/**\r
+  Converts EFI_TIME to Epoch seconds (elapsed since 1970 JANUARY 01, 00:00:00 UTC)\r
+ **/\r
+UINTN\r
+EfiTimeToEpoch (\r
+  IN  EFI_TIME  *Time\r
+  )\r
+{\r
+  UINTN a;\r
+  UINTN y;\r
+  UINTN m;\r
+  UINTN JulianDate;  // Absolute Julian Date representation of the supplied Time\r
+  UINTN EpochDays;   // Number of days elapsed since EPOCH_JULIAN_DAY\r
+  UINTN EpochSeconds;\r
+\r
+  a = (14 - Time->Month) / 12 ;\r
+  y = Time->Year + 4800 - a;\r
+  m = Time->Month + (12*a) - 3;\r
+\r
+  JulianDate = Time->Day + ((153*m + 2)/5) + (365*y) + (y/4) - (y/100) + (y/400) - 32045;\r
+\r
+  ASSERT( JulianDate > EPOCH_JULIAN_DATE );\r
+  EpochDays = JulianDate - EPOCH_JULIAN_DATE;\r
+\r
+  EpochSeconds = (EpochDays * SEC_PER_DAY) + ((UINTN)Time->Hour * SEC_PER_HOUR) + (Time->Minute * SEC_PER_MIN) + Time->Second;\r
+\r
+  return EpochSeconds;\r
+}\r
+\r
+BOOLEAN\r
+IsLeapYear (\r
+  IN EFI_TIME   *Time\r
+  )\r
+{\r
+  if (Time->Year % 4 == 0) {\r
+    if (Time->Year % 100 == 0) {\r
+      if (Time->Year % 400 == 0) {\r
+        return TRUE;\r
+      } else {\r
+        return FALSE;\r
+      }\r
+    } else {\r
+      return TRUE;\r
+    }\r
+  } else {\r
+    return FALSE;\r
+  }\r
+}\r
+\r
+BOOLEAN\r
+DayValid (\r
+  IN  EFI_TIME  *Time\r
+  )\r
+{\r
+  INTN  DayOfMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };\r
 \r
+  if (Time->Day < 1 ||\r
+      Time->Day > DayOfMonth[Time->Month - 1] ||\r
+      (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))\r
+      ) {\r
+    return FALSE;\r
+  }\r
+\r
+  return TRUE;\r
+}\r
 \r
 /**\r
   Returns the current time and date information, and the time-keeping capabilities\r
@@ -39,13 +245,122 @@ EFI_STATUS
 EFIAPI\r
 LibGetTime (\r
   OUT EFI_TIME                *Time,\r
-  OUT  EFI_TIME_CAPABILITIES  *Capabilities\r
+  OUT EFI_TIME_CAPABILITIES   *Capabilities\r
   )\r
 {\r
-  //\r
-  // Fill in Time and Capabilities via data from you RTC\r
-  //\r
-  return EFI_DEVICE_ERROR;\r
+  EFI_STATUS  Status = EFI_SUCCESS;\r
+  UINTN       EpochSeconds;\r
+  INT16       *TimeZone = 0;\r
+  UINTN       *Daylight = 0;\r
+\r
+  // Initialize the hardware if not already done\r
+  if( !mPL031Initialized ) {\r
+    Status = InitializePL031();\r
+    if (EFI_ERROR (Status)) {\r
+      goto EXIT;\r
+    }\r
+  }\r
+\r
+  // Snapshot the time as early in the function call as possible\r
+  // On some platforms we may have access to a battery backed up hardware clock.\r
+  // If such RTC exists try to use it first.\r
+  Status = ArmPlatformSysConfigGet (SYS_CFG_RTC, &EpochSeconds);\r
+  if (Status == EFI_UNSUPPORTED) {\r
+    // Battery backed up hardware RTC does not exist, revert to PL031\r
+    EpochSeconds = MmioRead32( PL031_RTC_DR_DATA_REGISTER );\r
+    Status = EFI_SUCCESS;\r
+  } else if (EFI_ERROR (Status)) {\r
+    // Battery backed up hardware RTC exists but could not be read due to error. Abort.\r
+    goto EXIT;\r
+  } else {\r
+    // Battery backed up hardware RTC exists and we read the time correctly from it.\r
+    // Now sync the PL031 to the new time.\r
+    MmioWrite32( PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);\r
+  }\r
+\r
+  // Ensure Time is a valid pointer\r
+  if( Time == NULL ) {\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto EXIT;\r
+  }\r
+\r
+  // Get the current time zone information from non-volatile storage\r
+  TimeZone = (INT16 *)GetVariable(mTimeZoneVariableName, &gEfiGlobalVariableGuid);\r
+\r
+  if( TimeZone == NULL ) {\r
+    // The time zone variable does not exist in non-volatile storage, so create it.\r
+    Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
+    // Store it\r
+    Status = gRT->SetVariable (\r
+                                mTimeZoneVariableName,\r
+                                &gEfiGlobalVariableGuid,\r
+                                EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+                                sizeof(Time->TimeZone),\r
+                                &(Time->TimeZone)\r
+                              );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG((EFI_D_ERROR,"LibGetTime: ERROR: TimeZone\n"));\r
+      goto EXIT;\r
+    }\r
+  } else {\r
+    // Got the time zone\r
+    Time->TimeZone = *TimeZone;\r
+    FreePool(TimeZone);\r
+\r
+    // Check TimeZone bounds:   -1440 to 1440 or 2047\r
+    if( (( Time->TimeZone < -1440 ) || ( Time->TimeZone > 1440 ))\r
+        && ( Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) ) {\r
+      Time->TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
+    }\r
+\r
+    // Adjust for the correct time zone\r
+    if( Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE ) {\r
+      EpochSeconds += Time->TimeZone * SEC_PER_MIN;\r
+    }\r
+  }\r
+\r
+  // Get the current daylight information from non-volatile storage\r
+  Daylight = (UINTN *)GetVariable(mDaylightVariableName, &gEfiGlobalVariableGuid);\r
+\r
+  if( Daylight == NULL ) {\r
+    // The daylight variable does not exist in non-volatile storage, so create it.\r
+    Time->Daylight = 0;\r
+    // Store it\r
+    Status = gRT->SetVariable (\r
+                                mDaylightVariableName,\r
+                                &gEfiGlobalVariableGuid,\r
+                                EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+                                sizeof(Time->Daylight),\r
+                                &(Time->Daylight)\r
+                              );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG((EFI_D_ERROR,"LibGetTime: ERROR: Daylight\n"));\r
+      goto EXIT;\r
+    }\r
+  } else {\r
+    // Got the daylight information\r
+    Time->Daylight = *Daylight;\r
+    FreePool(Daylight);\r
+\r
+    // Adjust for the correct period\r
+    if( (Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT ) {\r
+      // Convert to adjusted time, i.e. spring forwards one hour\r
+      EpochSeconds += SEC_PER_HOUR;\r
+    }\r
+  }\r
+\r
+  // Convert from internal 32-bit time to UEFI time\r
+  EpochToEfiTime( EpochSeconds, Time );\r
+\r
+  // Update the Capabilities info\r
+  if( Capabilities != NULL ) {\r
+    Capabilities->Resolution  = PL031_COUNTS_PER_SECOND;  /* PL031 runs at frequency 1Hz */\r
+    Capabilities->Accuracy    = PL031_PPM_ACCURACY;       /* Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000 */\r
+    Capabilities->SetsToZero  = FALSE;                    /* FALSE: Setting the time does not clear the values below the resolution level */\r
+  }\r
+\r
+  EXIT:\r
+  return Status;\r
 }\r
 \r
 \r
@@ -62,13 +377,107 @@ LibGetTime (
 EFI_STATUS\r
 EFIAPI\r
 LibSetTime (\r
-  IN EFI_TIME                *Time\r
+  IN  EFI_TIME                *Time\r
   )\r
 {\r
+  EFI_STATUS  Status;\r
+  UINTN       EpochSeconds;\r
+\r
+  // Because the PL031 is a 32-bit counter counting seconds,\r
+  // the maximum time span is just over 136 years.\r
+  // Time is stored in Unix Epoch format, so it starts in 1970,\r
+  // Therefore it can not exceed the year 2106.\r
+  // This is not a problem for UEFI, as the current spec limits the years\r
+  // to the range 1998 .. 2011\r
+\r
+  // Check the input parameters' range.\r
+  if ( ( Time->Year   < 1998 ) ||\r
+       ( Time->Year   > 2099 ) ||\r
+       ( Time->Month  < 1    ) ||\r
+       ( Time->Month  > 12   ) ||\r
+       (!DayValid (Time)     ) ||\r
+       ( Time->Hour   > 23   ) ||\r
+       ( Time->Minute > 59   ) ||\r
+       ( Time->Second > 59   ) ||\r
+       ( Time->Nanosecond > 999999999 ) ||\r
+       ( !((Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE) || ((Time->TimeZone >= -1440) && (Time->TimeZone <= 1440))) ) ||\r
+       ( Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)) )\r
+     ) {\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto EXIT;\r
+  }\r
+\r
+  // Initialize the hardware if not already done\r
+  if( !mPL031Initialized ) {\r
+    Status = InitializePL031();\r
+    if (EFI_ERROR (Status)) {\r
+      goto EXIT;\r
+    }\r
+  }\r
+\r
+  EpochSeconds = EfiTimeToEpoch( Time );\r
+\r
+  // Adjust for the correct time zone, i.e. convert to UTC time zone\r
+  if( Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE ) {\r
+    EpochSeconds -= Time->TimeZone * SEC_PER_MIN;\r
+  }\r
+\r
+  // TODO: Automatic Daylight activation\r
+\r
+  // Adjust for the correct period\r
+  if( (Time->Daylight & EFI_TIME_IN_DAYLIGHT) == EFI_TIME_IN_DAYLIGHT ) {\r
+    // Convert to un-adjusted time, i.e. fall back one hour\r
+    EpochSeconds -= SEC_PER_HOUR;\r
+  }\r
+\r
+  // On some platforms we may have access to a battery backed up hardware clock.\r
   //\r
-  // Use Time, to set the time in your RTC hardware\r
+  // If such RTC exists then it must be updated first, before the PL031,\r
+  // to minimise any time drift. This is important because the battery backed-up\r
+  // RTC maintains the master time for the platform across reboots.\r
   //\r
-  return EFI_DEVICE_ERROR;\r
+  // If such RTC does not exist then the following function returns UNSUPPORTED.\r
+  Status = ArmPlatformSysConfigSet (SYS_CFG_RTC, EpochSeconds);\r
+  if ((EFI_ERROR (Status)) && (Status != EFI_UNSUPPORTED)){\r
+    // Any status message except SUCCESS and UNSUPPORTED indicates a hardware failure.\r
+    goto EXIT;\r
+  }\r
+\r
+\r
+  // Set the PL031\r
+  MmioWrite32( PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);\r
+\r
+  // The accesses to Variable Services can be very slow, because we may be writing to Flash.\r
+  // Do this after having set the RTC.\r
+\r
+  // Save the current time zone information into non-volatile storage\r
+  Status = gRT->SetVariable (\r
+                              mTimeZoneVariableName,\r
+                              &gEfiGlobalVariableGuid,\r
+                              EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+                              sizeof(Time->TimeZone),\r
+                              &(Time->TimeZone)\r
+                            );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG((EFI_D_ERROR,"LibSetTime: ERROR: TimeZone\n"));\r
+    goto EXIT;\r
+  }\r
+\r
+  // Save the current daylight information into non-volatile storage\r
+  Status = gRT->SetVariable (\r
+                              mDaylightVariableName,\r
+                              &gEfiGlobalVariableGuid,\r
+                              EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
+                              sizeof(Time->Daylight),\r
+                              &(Time->Daylight)\r
+                            );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG((EFI_D_ERROR,"LibSetTime: ERROR: Daylight\n"));\r
+    goto EXIT;\r
+  }\r
+\r
+  EXIT:\r
+  return Status;\r
 }\r
 \r
 \r
@@ -140,10 +549,24 @@ LibRtcInitialize (
   IN EFI_SYSTEM_TABLE                      *SystemTable\r
   )\r
 {\r
-  //\r
-  // Do some initialization if reqruied to turn on the RTC\r
-  //\r
-  return EFI_SUCCESS;\r
+  EFI_STATUS    Status;\r
+  EFI_HANDLE    Handle;\r
+\r
+  // Setup the setters and getters\r
+  gRT->GetTime       = LibGetTime;\r
+  gRT->SetTime       = LibSetTime;\r
+  gRT->GetWakeupTime = LibGetWakeupTime;\r
+  gRT->SetWakeupTime = LibSetWakeupTime;\r
+\r
+  // Install the protocol\r
+  Handle = NULL;\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &Handle,\r
+                  &gEfiRealTimeClockArchProtocolGuid,  NULL,\r
+                  NULL\r
+                  );\r
+\r
+  return Status;\r
 }\r
 \r
 \r
@@ -164,8 +587,8 @@ LibRtcVirtualNotifyEvent (
 {\r
   //\r
   // Only needed if you are going to support the OS calling RTC functions in virtual mode.\r
-  // You will need to call EfiConvertPointer (). To convert any stored physical addresses \r
-  // to virtual address. After the OS transistions to calling in virtual mode, all future\r
+  // You will need to call EfiConvertPointer (). To convert any stored physical addresses\r
+  // to virtual address. After the OS transitions to calling in virtual mode, all future\r
   // runtime calls will be made in virtual mode.\r
   //\r
   return;\r
index 30181ab3b9c147d4370da2d6a8e593187058eab7..8a249ec8894f8b3355af14086502f299c753081e 100644 (file)
 [Packages]\r
   MdePkg/MdePkg.dec\r
   EmbeddedPkg/EmbeddedPkg.dec\r
+  ArmPlatformPkg/ArmPlatformPkg.dec\r
 \r
 [LibraryClasses]\r
   IoLib\r
+  UefiLib\r
   DebugLib\r
+  ArmPlatformSysConfigLib\r