]> git.proxmox.com Git - mirror_edk2.git/blobdiff - PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c
PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe: Replace RTC macros
[mirror_edk2.git] / PcAtChipsetPkg / PcatRealTimeClockRuntimeDxe / PcRtc.c
index 4a7fca3299b92a816550f5c6ec0ec66f18bd4807..c032e16217c3d858b93186a5cf49eae8bd21833e 100644 (file)
@@ -1,8 +1,10 @@
 /** @file\r
   RTC Architectural Protocol GUID as defined in DxeCis 0.96.\r
 \r
-Copyright (c) 2006 - 2007, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2017, AMD Inc. All rights reserved.<BR>\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
@@ -14,6 +16,16 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "PcRtc.h"\r
 \r
+//\r
+// Days of month.\r
+//\r
+UINTN mDayOfMonth[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };\r
+\r
+//\r
+// The name of NV variable to store the timezone and daylight saving information.\r
+//\r
+CHAR16 mTimeZoneVariableName[] = L"RTC";\r
+\r
 /**\r
   Compare the Hour, Minute and Second of the From time and the To time.\r
   \r
@@ -100,10 +112,11 @@ PcRtcInit (
   RTC_REGISTER_A  RegisterA;\r
   RTC_REGISTER_B  RegisterB;\r
   RTC_REGISTER_D  RegisterD;\r
-  UINT8           Century;\r
   EFI_TIME        Time;\r
   UINTN           DataSize;\r
   UINT32          TimerVar;\r
+  BOOLEAN         Enabled;\r
+  BOOLEAN         Pending;\r
 \r
   //\r
   // Acquire RTC Lock to make access to RTC atomic\r
@@ -117,7 +130,7 @@ PcRtcInit (
   // Make sure Division Chain is properly configured,\r
   // or RTC clock won't "tick" -- time won't increment\r
   //\r
-  RegisterA.Data = RTC_INIT_REGISTER_A;\r
+  RegisterA.Data = FixedPcdGet8 (PcdInitialValueRtcRegisterA);\r
   RtcWrite (RTC_ADDRESS_REGISTER_A, RegisterA.Data);\r
 \r
   //\r
@@ -133,7 +146,7 @@ PcRtcInit (
   //\r
   // Clear RTC register D\r
   //\r
-  RegisterD.Data = RTC_INIT_REGISTER_D;\r
+  RegisterD.Data = FixedPcdGet8 (PcdInitialValueRtcRegisterD);\r
   RtcWrite (RTC_ADDRESS_REGISTER_D, RegisterD.Data);\r
 \r
   //\r
@@ -141,6 +154,11 @@ PcRtcInit (
   //\r
   Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
   if (EFI_ERROR (Status)) {\r
+    //\r
+    // Set the variable with default value if the RTC is functioning incorrectly.\r
+    //\r
+    Global->SavedTimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
+    Global->Daylight      = 0;\r
     if (!EfiAtRuntime ()) {\r
       EfiReleaseLock (&Global->RtcLock);\r
     }\r
@@ -156,13 +174,12 @@ PcRtcInit (
   Time.Month  = RtcRead (RTC_ADDRESS_MONTH);\r
   Time.Year   = RtcRead (RTC_ADDRESS_YEAR);\r
 \r
-  Century = RtcRead (RTC_ADDRESS_CENTURY);\r
-  \r
   //\r
   // Set RTC configuration after get original time\r
   // The value of bit AIE should be reserved.\r
   //\r
-  RtcWrite (RTC_ADDRESS_REGISTER_B, (UINT8)(RTC_INIT_REGISTER_B | (RegisterB.Data & BIT5)));\r
+  RegisterB.Data = FixedPcdGet8 (PcdInitialValueRtcRegisterB) | (RegisterB.Data & BIT5);\r
+  RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
 \r
   //\r
   // Release RTC Lock.\r
@@ -177,11 +194,11 @@ PcRtcInit (
   //\r
   DataSize = sizeof (UINT32);\r
   Status = EfiGetVariable (\r
-             L"RTC",\r
+             mTimeZoneVariableName,\r
              &gEfiCallerIdGuid,\r
              NULL,\r
              &DataSize,\r
-             (VOID *) &TimerVar\r
+             &TimerVar\r
              );\r
   if (!EFI_ERROR (Status)) {\r
     Time.TimeZone = (INT16) TimerVar;\r
@@ -194,29 +211,123 @@ PcRtcInit (
   //\r
   // Validate time fields\r
   //\r
-  Status = ConvertRtcTimeToEfiTime (&Time, Century, RegisterB);\r
+  Status = ConvertRtcTimeToEfiTime (&Time, RegisterB);\r
   if (!EFI_ERROR (Status)) {\r
     Status = RtcTimeFieldsValid (&Time);\r
   }\r
   if (EFI_ERROR (Status)) {\r
+    //\r
+    // Report Status Code to indicate that the RTC has bad date and time\r
+    //\r
+    REPORT_STATUS_CODE (\r
+      EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
+      (EFI_SOFTWARE_DXE_RT_DRIVER | EFI_SW_EC_BAD_DATE_TIME)\r
+      );\r
     Time.Second = RTC_INIT_SECOND;\r
     Time.Minute = RTC_INIT_MINUTE;\r
     Time.Hour   = RTC_INIT_HOUR;\r
     Time.Day    = RTC_INIT_DAY;\r
     Time.Month  = RTC_INIT_MONTH;\r
-    Time.Year   = RTC_INIT_YEAR;\r
+    Time.Year   = PcdGet16 (PcdMinimalValidYear);\r
     Time.Nanosecond  = 0;\r
+    Time.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
+    Time.Daylight = 0;\r
   }\r
 \r
   //\r
   // Reset time value according to new RTC configuration\r
   //\r
   Status = PcRtcSetTime (&Time, Global);\r
-  if(!EFI_ERROR (Status)) {\r
+  if (EFI_ERROR (Status)) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+  \r
+  //\r
+  // Reset wakeup time value to valid state when wakeup alarm is disabled and wakeup time is invalid.\r
+  // Global variable has already had valid SavedTimeZone and Daylight,\r
+  // so we can use them to get and set wakeup time.\r
+  //\r
+  Status = PcRtcGetWakeupTime (&Enabled, &Pending, &Time, Global);\r
+  if ((Enabled) || (!EFI_ERROR (Status))) {\r
     return EFI_SUCCESS;\r
-  } else {\r
+  }\r
+  \r
+  //\r
+  // When wakeup time is disabled and invalid, reset wakeup time register to valid state \r
+  // but keep wakeup alarm disabled.\r
+  //\r
+  Time.Second = RTC_INIT_SECOND;\r
+  Time.Minute = RTC_INIT_MINUTE;\r
+  Time.Hour   = RTC_INIT_HOUR;\r
+  Time.Day    = RTC_INIT_DAY;\r
+  Time.Month  = RTC_INIT_MONTH;\r
+  Time.Year   = PcdGet16 (PcdMinimalValidYear);\r
+  Time.Nanosecond  = 0;\r
+  Time.TimeZone = Global->SavedTimeZone;\r
+  Time.Daylight = Global->Daylight;;\r
+\r
+  //\r
+  // Acquire RTC Lock to make access to RTC atomic\r
+  //\r
+  if (!EfiAtRuntime ()) {\r
+    EfiAcquireLock (&Global->RtcLock);\r
+  }\r
+  //\r
+  // Wait for up to 0.1 seconds for the RTC to be updated\r
+  //\r
+  Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
+  if (EFI_ERROR (Status)) {\r
+    if (!EfiAtRuntime ()) {\r
+    EfiReleaseLock (&Global->RtcLock);\r
+    }\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  ConvertEfiTimeToRtcTime (&Time, RegisterB);\r
+\r
+  //\r
+  // Set the Y/M/D info to variable as it has no corresponding hw registers.\r
+  //\r
+  Status =  EfiSetVariable (\r
+              L"RTCALARM",\r
+              &gEfiCallerIdGuid,\r
+              EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+              sizeof (Time),\r
+              &Time\r
+              );\r
+  if (EFI_ERROR (Status)) {\r
+    if (!EfiAtRuntime ()) {\r
+      EfiReleaseLock (&Global->RtcLock);\r
+    }\r
     return EFI_DEVICE_ERROR;\r
   }\r
+  \r
+  //\r
+  // Inhibit updates of the RTC\r
+  //\r
+  RegisterB.Bits.Set  = 1;\r
+  RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
\r
+  //\r
+  // Set RTC alarm time registers\r
+  //\r
+  RtcWrite (RTC_ADDRESS_SECONDS_ALARM, Time.Second);\r
+  RtcWrite (RTC_ADDRESS_MINUTES_ALARM, Time.Minute);\r
+  RtcWrite (RTC_ADDRESS_HOURS_ALARM, Time.Hour);\r
+\r
+  //\r
+  // Allow updates of the RTC registers\r
+  //\r
+  RegisterB.Bits.Set = 0;\r
+  RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
\r
+  //\r
+  // Release RTC Lock.\r
+  //\r
+  if (!EfiAtRuntime ()) {\r
+    EfiReleaseLock (&Global->RtcLock);\r
+  }\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -242,7 +353,6 @@ PcRtcGetTime (
 {\r
   EFI_STATUS      Status;\r
   RTC_REGISTER_B  RegisterB;\r
-  UINT8           Century;\r
 \r
   //\r
   // Check parameters for null pointer\r
@@ -282,8 +392,6 @@ PcRtcGetTime (
   Time->Month   = RtcRead (RTC_ADDRESS_MONTH);\r
   Time->Year    = RtcRead (RTC_ADDRESS_YEAR);\r
 \r
-  Century = RtcRead (RTC_ADDRESS_CENTURY);\r
-  \r
   //\r
   // Release RTC Lock.\r
   //\r
@@ -300,7 +408,7 @@ PcRtcGetTime (
   //\r
   // Make sure all field values are in correct range\r
   //\r
-  Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB);\r
+  Status = ConvertRtcTimeToEfiTime (Time, RegisterB);\r
   if (!EFI_ERROR (Status)) {\r
     Status = RtcTimeFieldsValid (Time);\r
   }\r
@@ -346,7 +454,6 @@ PcRtcSetTime (
   EFI_STATUS      Status;\r
   EFI_TIME        RtcTime;\r
   RTC_REGISTER_B  RegisterB;\r
-  UINT8           Century;\r
   UINT32          TimerVar;\r
 \r
   if (Time == NULL) {\r
@@ -378,14 +485,55 @@ PcRtcSetTime (
      }\r
     return Status;\r
   }\r
+  \r
+  //\r
+  // Write timezone and daylight to RTC variable\r
+  //\r
+  if ((Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE) && (Time->Daylight == 0)) {\r
+    Status = EfiSetVariable (\r
+               mTimeZoneVariableName,\r
+               &gEfiCallerIdGuid,\r
+               0,\r
+               0,\r
+               NULL\r
+               );\r
+    if (Status == EFI_NOT_FOUND) {\r
+      Status = EFI_SUCCESS;\r
+    }\r
+  } else {\r
+    TimerVar = Time->Daylight;\r
+    TimerVar = (UINT32) ((TimerVar << 16) | (UINT16)(Time->TimeZone));\r
+    Status = EfiSetVariable (\r
+               mTimeZoneVariableName,\r
+               &gEfiCallerIdGuid,\r
+               EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+               sizeof (TimerVar),\r
+               &TimerVar\r
+               );\r
+  }\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    if (!EfiAtRuntime ()) {\r
+      EfiReleaseLock (&Global->RtcLock);\r
+    }\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   //\r
   // Read Register B, and inhibit updates of the RTC\r
   //\r
   RegisterB.Data      = RtcRead (RTC_ADDRESS_REGISTER_B);\r
-  RegisterB.Bits.SET  = 1;\r
+  RegisterB.Bits.Set  = 1;\r
   RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
 \r
-  ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century);\r
+  //\r
+  // Store the century value to RTC before converting to BCD format.\r
+  //\r
+  if (Global->CenturyRtcAddress != 0) {\r
+    RtcWrite (Global->CenturyRtcAddress, DecimalToBcd8 ((UINT8) (RtcTime.Year / 100)));\r
+  }\r
+\r
+  ConvertEfiTimeToRtcTime (&RtcTime, RegisterB);\r
 \r
   RtcWrite (RTC_ADDRESS_SECONDS, RtcTime.Second);\r
   RtcWrite (RTC_ADDRESS_MINUTES, RtcTime.Minute);\r
@@ -393,12 +541,11 @@ PcRtcSetTime (
   RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH, RtcTime.Day);\r
   RtcWrite (RTC_ADDRESS_MONTH, RtcTime.Month);\r
   RtcWrite (RTC_ADDRESS_YEAR, (UINT8) RtcTime.Year);\r
-  RtcWrite (RTC_ADDRESS_CENTURY, Century);\r
 \r
   //\r
   // Allow updates of the RTC registers\r
   //\r
-  RegisterB.Bits.SET = 0;\r
+  RegisterB.Bits.Set = 0;\r
   RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
 \r
   //\r
@@ -413,17 +560,6 @@ PcRtcSetTime (
   Global->SavedTimeZone = Time->TimeZone;\r
   Global->Daylight      = Time->Daylight;\r
 \r
-  TimerVar = Time->Daylight;\r
-  TimerVar = (UINT32) ((TimerVar << 16) | Time->TimeZone);\r
-  Status =  EfiSetVariable (\r
-              L"RTC",\r
-              &gEfiCallerIdGuid,\r
-              EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
-              sizeof (TimerVar),\r
-              &TimerVar\r
-              );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -454,7 +590,8 @@ PcRtcGetWakeupTime (
   EFI_STATUS      Status;\r
   RTC_REGISTER_B  RegisterB;\r
   RTC_REGISTER_C  RegisterC;\r
-  UINT8           Century;\r
+  EFI_TIME        RtcTime;\r
+  UINTN           DataSize;\r
 \r
   //\r
   // Check parameters for null pointers\r
@@ -488,25 +625,38 @@ PcRtcGetWakeupTime (
   //\r
   // Get the Time/Date/Daylight Savings values.\r
   //\r
-  *Enabled = RegisterB.Bits.AIE;\r
-  if (*Enabled) {\r
-    Time->Second  = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
-    Time->Minute  = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
-    Time->Hour    = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
-    Time->Day     = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
-    Time->Month   = RtcRead (RTC_ADDRESS_MONTH);\r
-    Time->Year    = RtcRead (RTC_ADDRESS_YEAR);\r
-  } else {\r
-    Time->Second  = 0;\r
-    Time->Minute  = 0;\r
-    Time->Hour    = 0;\r
-    Time->Day     = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
-    Time->Month   = RtcRead (RTC_ADDRESS_MONTH);\r
-    Time->Year    = RtcRead (RTC_ADDRESS_YEAR);\r
+  *Enabled = RegisterB.Bits.Aie;\r
+  *Pending = RegisterC.Bits.Af;\r
+\r
+  Time->Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
+  Time->Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
+  Time->Hour   = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
+  Time->Day    = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
+  Time->Month  = RtcRead (RTC_ADDRESS_MONTH);\r
+  Time->Year   = RtcRead (RTC_ADDRESS_YEAR);\r
+  Time->TimeZone = Global->SavedTimeZone;\r
+  Time->Daylight = Global->Daylight;\r
+\r
+  //\r
+  // Get the alarm info from variable\r
+  //\r
+  DataSize = sizeof (EFI_TIME);\r
+  Status = EfiGetVariable (\r
+              L"RTCALARM",\r
+              &gEfiCallerIdGuid,\r
+              NULL,\r
+              &DataSize,\r
+              &RtcTime\r
+              );\r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // The alarm variable exists. In this case, we read variable to get info.\r
+    //\r
+    Time->Day   = RtcTime.Day;\r
+    Time->Month = RtcTime.Month;\r
+    Time->Year  = RtcTime.Year;\r
   }\r
 \r
-  Century = RtcRead (RTC_ADDRESS_CENTURY);\r
-  \r
   //\r
   // Release RTC Lock.\r
   //\r
@@ -514,16 +664,10 @@ PcRtcGetWakeupTime (
     EfiReleaseLock (&Global->RtcLock);\r
   }\r
 \r
-  //\r
-  // Get the variable that contains the TimeZone and Daylight fields\r
-  //\r
-  Time->TimeZone  = Global->SavedTimeZone;\r
-  Time->Daylight  = Global->Daylight;\r
-\r
   //\r
   // Make sure all field values are in correct range\r
   //\r
-  Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB);\r
+  Status = ConvertRtcTimeToEfiTime (Time, RegisterB);\r
   if (!EFI_ERROR (Status)) {\r
     Status = RtcTimeFieldsValid (Time);\r
   }\r
@@ -531,8 +675,6 @@ PcRtcGetWakeupTime (
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
-  *Pending = RegisterC.Bits.AF;\r
-\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -561,9 +703,10 @@ PcRtcSetWakeupTime (
   EFI_STATUS            Status;\r
   EFI_TIME              RtcTime;\r
   RTC_REGISTER_B        RegisterB;\r
-  UINT8                 Century;\r
   EFI_TIME_CAPABILITIES Capabilities;\r
 \r
+  ZeroMem (&RtcTime, sizeof (RtcTime));\r
+\r
   if (Enable) {\r
 \r
     if (Time == NULL) {\r
@@ -610,16 +753,50 @@ PcRtcSetWakeupTime (
     return EFI_DEVICE_ERROR;\r
   }\r
   //\r
-  // Read Register B, and inhibit updates of the RTC\r
+  // Read Register B\r
   //\r
-  RegisterB.Data      = RtcRead (RTC_ADDRESS_REGISTER_B);\r
+  RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
 \r
-  RegisterB.Bits.SET  = 1;\r
+  if (Enable) {\r
+    ConvertEfiTimeToRtcTime (&RtcTime, RegisterB);\r
+  } else {\r
+    //\r
+    // if the alarm is disable, record the current setting.\r
+    //\r
+    RtcTime.Second  = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
+    RtcTime.Minute  = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
+    RtcTime.Hour    = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
+    RtcTime.Day     = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
+    RtcTime.Month   = RtcRead (RTC_ADDRESS_MONTH);\r
+    RtcTime.Year    = RtcRead (RTC_ADDRESS_YEAR);\r
+    RtcTime.TimeZone = Global->SavedTimeZone;\r
+    RtcTime.Daylight = Global->Daylight;\r
+  }\r
+\r
+  //\r
+  // Set the Y/M/D info to variable as it has no corresponding hw registers.\r
+  //\r
+  Status =  EfiSetVariable (\r
+              L"RTCALARM",\r
+              &gEfiCallerIdGuid,\r
+              EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
+              sizeof (RtcTime),\r
+              &RtcTime\r
+              );\r
+  if (EFI_ERROR (Status)) {\r
+    if (!EfiAtRuntime ()) {\r
+      EfiReleaseLock (&Global->RtcLock);\r
+    }\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+  \r
+  //\r
+  // Inhibit updates of the RTC\r
+  //\r
+  RegisterB.Bits.Set  = 1;\r
   RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
 \r
   if (Enable) {\r
-    ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century);\r
-\r
     //\r
     // Set RTC alarm time\r
     //\r
@@ -627,15 +804,15 @@ PcRtcSetWakeupTime (
     RtcWrite (RTC_ADDRESS_MINUTES_ALARM, RtcTime.Minute);\r
     RtcWrite (RTC_ADDRESS_HOURS_ALARM, RtcTime.Hour);\r
 \r
-    RegisterB.Bits.AIE = 1;\r
+    RegisterB.Bits.Aie = 1;\r
 \r
   } else {\r
-    RegisterB.Bits.AIE = 0;\r
+    RegisterB.Bits.Aie = 0;\r
   }\r
   //\r
   // Allow updates of the RTC registers\r
   //\r
-  RegisterB.Bits.SET = 0;\r
+  RegisterB.Bits.Set = 0;\r
   RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
 \r
   //\r
@@ -682,7 +859,6 @@ CheckAndConvertBcd8ToDecimal8 (
 \r
   @param   Time       On input, the time data read from RTC to convert\r
                       On output, the time converted to UEFI format\r
-  @param   Century    Value of century read from RTC.\r
   @param   RegisterB  Value of Register B of RTC, indicating data mode\r
                       and hour format.\r
 \r
@@ -693,11 +869,11 @@ CheckAndConvertBcd8ToDecimal8 (
 EFI_STATUS\r
 ConvertRtcTimeToEfiTime (\r
   IN OUT EFI_TIME        *Time,\r
-  IN     UINT8           Century,\r
   IN     RTC_REGISTER_B  RegisterB\r
   )\r
 {\r
   BOOLEAN IsPM;\r
+  UINT8   Century;\r
 \r
   if ((Time->Hour & 0x80) != 0) {\r
     IsPM = TRUE;\r
@@ -707,7 +883,7 @@ ConvertRtcTimeToEfiTime (
 \r
   Time->Hour = (UINT8) (Time->Hour & 0x7f);\r
 \r
-  if (RegisterB.Bits.DM == 0) {\r
+  if (RegisterB.Bits.Dm == 0) {\r
     Time->Year    = CheckAndConvertBcd8ToDecimal8 ((UINT8) Time->Year);\r
     Time->Month   = CheckAndConvertBcd8ToDecimal8 (Time->Month);\r
     Time->Day     = CheckAndConvertBcd8ToDecimal8 (Time->Day);\r
@@ -715,20 +891,27 @@ ConvertRtcTimeToEfiTime (
     Time->Minute  = CheckAndConvertBcd8ToDecimal8 (Time->Minute);\r
     Time->Second  = CheckAndConvertBcd8ToDecimal8 (Time->Second);\r
   }\r
-  Century       = CheckAndConvertBcd8ToDecimal8 (Century);\r
 \r
   if (Time->Year == 0xff || Time->Month == 0xff || Time->Day == 0xff ||\r
-      Time->Hour == 0xff || Time->Minute == 0xff || Time->Second == 0xff ||\r
-      Century == 0xff) {\r
+      Time->Hour == 0xff || Time->Minute == 0xff || Time->Second == 0xff) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // For minimal/maximum year range [1970, 2069],\r
+  //   Century is 19 if RTC year >= 70,\r
+  //   Century is 20 otherwise.\r
+  //\r
+  Century = (UINT8) (PcdGet16 (PcdMinimalValidYear) / 100);\r
+  if (Time->Year < PcdGet16 (PcdMinimalValidYear) % 100) {\r
+    Century++;\r
+  }\r
   Time->Year = (UINT16) (Century * 100 + Time->Year);\r
 \r
   //\r
   // If time is in 12 hour format, convert it to 24 hour format\r
   //\r
-  if (RegisterB.Bits.MIL == 0) {\r
+  if (RegisterB.Bits.Mil == 0) {\r
     if (IsPM && Time->Hour < 12) {\r
       Time->Hour = (UINT8) (Time->Hour + 12);\r
     }\r
@@ -764,7 +947,7 @@ RtcWaitToUpdate (
   //\r
   RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);\r
 \r
-  if (RegisterD.Bits.VRT == 0) {\r
+  if (RegisterD.Bits.Vrt == 0) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
   //\r
@@ -772,14 +955,14 @@ RtcWaitToUpdate (
   //\r
   Timeout         = (Timeout / 10) + 1;\r
   RegisterA.Data  = RtcRead (RTC_ADDRESS_REGISTER_A);\r
-  while (RegisterA.Bits.UIP == 1 && Timeout > 0) {\r
+  while (RegisterA.Bits.Uip == 1 && Timeout > 0) {\r
     MicroSecondDelay (10);\r
     RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);\r
     Timeout--;\r
   }\r
 \r
   RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);\r
-  if (Timeout == 0 || RegisterD.Bits.VRT == 0) {\r
+  if (Timeout == 0 || RegisterD.Bits.Vrt == 0) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
@@ -800,8 +983,8 @@ RtcTimeFieldsValid (
   IN EFI_TIME *Time\r
   )\r
 {\r
-  if (Time->Year < 1998 ||\r
-      Time->Year > 2099 ||\r
+  if (Time->Year < PcdGet16 (PcdMinimalValidYear) ||\r
+      Time->Year > PcdGet16 (PcdMaximalValidYear) ||\r
       Time->Month < 1 ||\r
       Time->Month > 12 ||\r
       (!DayValid (Time)) ||\r
@@ -830,16 +1013,13 @@ DayValid (
   IN  EFI_TIME  *Time\r
   )\r
 {\r
-\r
-  INTN  DayOfMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };\r
-\r
   //\r
   // The validity of Time->Month field should be checked before\r
   //\r
   ASSERT (Time->Month >=1);\r
   ASSERT (Time->Month <=12);\r
   if (Time->Day < 1 ||\r
-      Time->Day > DayOfMonth[Time->Month - 1] ||\r
+      Time->Day > mDayOfMonth[Time->Month - 1] ||\r
       (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))\r
       ) {\r
     return FALSE;\r
@@ -886,14 +1066,11 @@ IsLeapYear (
   @param   Time       On input, the time data read from UEFI to convert\r
                       On output, the time converted to RTC format\r
   @param   RegisterB  Value of Register B of RTC, indicating data mode\r
-  @param   Century    It is set according to EFI_TIME Time.\r
-\r
 **/\r
 VOID\r
 ConvertEfiTimeToRtcTime (\r
   IN OUT EFI_TIME        *Time,\r
-  IN     RTC_REGISTER_B  RegisterB,\r
-     OUT UINT8           *Century\r
+  IN     RTC_REGISTER_B  RegisterB\r
   )\r
 {\r
   BOOLEAN IsPM;\r
@@ -902,7 +1079,7 @@ ConvertEfiTimeToRtcTime (
   //\r
   // Adjust hour field if RTC is in 12 hour mode\r
   //\r
-  if (RegisterB.Bits.MIL == 0) {\r
+  if (RegisterB.Bits.Mil == 0) {\r
     if (Time->Hour < 12) {\r
       IsPM = FALSE;\r
     }\r
@@ -914,13 +1091,11 @@ ConvertEfiTimeToRtcTime (
     }\r
   }\r
   //\r
-  // Set the Time/Date/Daylight Savings values.\r
+  // Set the Time/Date values.\r
   //\r
-  *Century    = DecimalToBcd8 ((UINT8) (Time->Year / 100));\r
-\r
   Time->Year  = (UINT16) (Time->Year % 100);\r
 \r
-  if (RegisterB.Bits.DM == 0) {\r
+  if (RegisterB.Bits.Dm == 0) {\r
     Time->Year    = DecimalToBcd8 ((UINT8) Time->Year);\r
     Time->Month   = DecimalToBcd8 (Time->Month);\r
     Time->Day     = DecimalToBcd8 (Time->Day);\r
@@ -931,7 +1106,7 @@ ConvertEfiTimeToRtcTime (
   //\r
   // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.\r
   //\r
-  if (RegisterB.Bits.MIL == 0 && IsPM) {\r
+  if (RegisterB.Bits.Mil == 0 && IsPM) {\r
     Time->Hour = (UINT8) (Time->Hour | 0x80);\r
   }\r
 }\r
@@ -980,7 +1155,6 @@ IsWithinOneDay (
   IN EFI_TIME  *To\r
   )\r
 {\r
-  UINT8   DayOfMonth[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\r
   BOOLEAN Adjacent;\r
 \r
   Adjacent = FALSE;\r
@@ -1009,7 +1183,7 @@ IsWithinOneDay (
             Adjacent = TRUE;\r
           }\r
         }\r
-      } else if (From->Day == DayOfMonth[From->Month - 1]) {\r
+      } else if (From->Day == mDayOfMonth[From->Month - 1]) {\r
         if ((CompareHMS(From, To) >= 0)) {\r
            Adjacent = TRUE;\r
         }\r
@@ -1028,3 +1202,136 @@ IsWithinOneDay (
   return Adjacent;\r
 }\r
 \r
+/**\r
+  This function find ACPI table with the specified signature in RSDT or XSDT.\r
+\r
+  @param Sdt              ACPI RSDT or XSDT.\r
+  @param Signature        ACPI table signature.\r
+  @param TablePointerSize Size of table pointer: 4 or 8.\r
+\r
+  @return ACPI table or NULL if not found.\r
+**/\r
+VOID *\r
+ScanTableInSDT (\r
+  IN EFI_ACPI_DESCRIPTION_HEADER    *Sdt,\r
+  IN UINT32                         Signature,\r
+  IN UINTN                          TablePointerSize\r
+  )\r
+{\r
+  UINTN                          Index;\r
+  UINTN                          EntryCount;\r
+  UINTN                          EntryBase;\r
+  EFI_ACPI_DESCRIPTION_HEADER    *Table;\r
+\r
+  EntryCount = (Sdt->Length - sizeof (EFI_ACPI_DESCRIPTION_HEADER)) / TablePointerSize;\r
+\r
+  EntryBase = (UINTN) (Sdt + 1);\r
+  for (Index = 0; Index < EntryCount; Index++) {\r
+    //\r
+    // When TablePointerSize is 4 while sizeof (VOID *) is 8, make sure the upper 4 bytes are zero.\r
+    //\r
+    Table = 0;\r
+    CopyMem (&Table, (VOID *) (EntryBase + Index * TablePointerSize), TablePointerSize);\r
+\r
+    if (Table == NULL) {\r
+      continue;\r
+    }\r
+\r
+    if (Table->Signature == Signature) {\r
+      return Table;\r
+    }\r
+  }\r
+\r
+  return NULL;\r
+}\r
+\r
+/**\r
+  Get the century RTC address from the ACPI FADT table.\r
+\r
+  @return  The century RTC address or 0 if not found.\r
+**/\r
+UINT8\r
+GetCenturyRtcAddress (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                                    Status;\r
+  EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER  *Rsdp;\r
+  EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE     *Fadt;\r
+\r
+  Status = EfiGetSystemConfigurationTable (&gEfiAcpiTableGuid, (VOID **) &Rsdp);\r
+  if (EFI_ERROR (Status)) {\r
+    Status = EfiGetSystemConfigurationTable (&gEfiAcpi10TableGuid, (VOID **) &Rsdp);\r
+  }\r
+\r
+  if (EFI_ERROR (Status) || (Rsdp == NULL)) {\r
+    return 0;\r
+  }\r
+\r
+  Fadt = NULL;\r
+\r
+  //\r
+  // Find FADT in XSDT\r
+  //\r
+  if (Rsdp->Revision >= EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER_REVISION && Rsdp->XsdtAddress != 0) {\r
+    Fadt = ScanTableInSDT (\r
+             (EFI_ACPI_DESCRIPTION_HEADER *) (UINTN) Rsdp->XsdtAddress,\r
+             EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE,\r
+             sizeof (UINTN)\r
+             );\r
+  }\r
+\r
+  //\r
+  // Find FADT in RSDT\r
+  //\r
+  if (Fadt == NULL && Rsdp->RsdtAddress != 0) {\r
+    Fadt = ScanTableInSDT (\r
+             (EFI_ACPI_DESCRIPTION_HEADER *) (UINTN) Rsdp->RsdtAddress,\r
+             EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE,\r
+             sizeof (UINT32)\r
+             );\r
+  }\r
+\r
+  if ((Fadt != NULL) &&\r
+      (Fadt->Century > RTC_ADDRESS_REGISTER_D) && (Fadt->Century < 0x80)\r
+      ) {\r
+    return Fadt->Century;\r
+  } else {\r
+    return 0;\r
+  }\r
+}\r
+\r
+/**\r
+  Notification function of ACPI Table change.\r
+\r
+  This is a notification function registered on ACPI Table change event.\r
+  It saves the Century address stored in ACPI FADT table.\r
+\r
+  @param  Event        Event whose notification function is being invoked.\r
+  @param  Context      Pointer to the notification function's context.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+PcRtcAcpiTableChangeCallback (\r
+  IN EFI_EVENT        Event,\r
+  IN VOID             *Context\r
+  )\r
+{\r
+  EFI_STATUS          Status;\r
+  EFI_TIME            Time;\r
+  UINT8               CenturyRtcAddress;\r
+  UINT8               Century;\r
+\r
+  CenturyRtcAddress = GetCenturyRtcAddress ();\r
+  if ((CenturyRtcAddress != 0) && (mModuleGlobal.CenturyRtcAddress != CenturyRtcAddress)) {\r
+    mModuleGlobal.CenturyRtcAddress = CenturyRtcAddress;\r
+    Status = PcRtcGetTime (&Time, NULL, &mModuleGlobal);\r
+    if (!EFI_ERROR (Status)) {\r
+      Century = (UINT8) (Time.Year / 100);\r
+      Century = DecimalToBcd8 (Century);\r
+      DEBUG ((EFI_D_INFO, "PcRtc: Write 0x%x to CMOS location 0x%x\n", Century, mModuleGlobal.CenturyRtcAddress));\r
+      RtcWrite (mModuleGlobal.CenturyRtcAddress, Century);\r
+    }\r
+  }\r
+}\r