X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=PcAtChipsetPkg%2FPcatRealTimeClockRuntimeDxe%2FPcRtc.c;h=9ec309c6b997d90983a179fc07292ad8a9294481;hp=ab4ea8206a10d5dce81bc8fb325a9b78e33e1454;hb=fe32096778cf248e36d6d00cd958d73fb903126f;hpb=44d52203a651445e78369f93a3ea22a8b565e9ad diff --git a/PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c b/PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c index ab4ea8206a..9ec309c6b9 100644 --- a/PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c +++ b/PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c @@ -1,7 +1,7 @@ /** @file RTC Architectural Protocol GUID as defined in DxeCis 0.96. -Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -100,10 +100,11 @@ PcRtcInit ( RTC_REGISTER_A RegisterA; RTC_REGISTER_B RegisterB; RTC_REGISTER_D RegisterD; - UINT8 Century; EFI_TIME Time; UINTN DataSize; UINT32 TimerVar; + BOOLEAN Enabled; + BOOLEAN Pending; // // Acquire RTC Lock to make access to RTC atomic @@ -161,8 +162,6 @@ PcRtcInit ( Time.Month = RtcRead (RTC_ADDRESS_MONTH); Time.Year = RtcRead (RTC_ADDRESS_YEAR); - Century = RtcRead (RTC_ADDRESS_CENTURY); - // // Set RTC configuration after get original time // The value of bit AIE should be reserved. @@ -199,7 +198,7 @@ PcRtcInit ( // // Validate time fields // - Status = ConvertRtcTimeToEfiTime (&Time, Century, RegisterB); + Status = ConvertRtcTimeToEfiTime (&Time, RegisterB); if (!EFI_ERROR (Status)) { Status = RtcTimeFieldsValid (&Time); } @@ -216,7 +215,7 @@ PcRtcInit ( Time.Hour = RTC_INIT_HOUR; Time.Day = RTC_INIT_DAY; Time.Month = RTC_INIT_MONTH; - Time.Year = RTC_INIT_YEAR; + Time.Year = PcdGet16 (PcdMinimalValidYear); Time.Nanosecond = 0; Time.TimeZone = EFI_UNSPECIFIED_TIMEZONE; Time.Daylight = 0; @@ -226,11 +225,96 @@ PcRtcInit ( // Reset time value according to new RTC configuration // Status = PcRtcSetTime (&Time, Global); - if(!EFI_ERROR (Status)) { + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + // + // Reset wakeup time value to valid state when wakeup alarm is disabled and wakeup time is invalid. + // Global variable has already had valid SavedTimeZone and Daylight, + // so we can use them to get and set wakeup time. + // + Status = PcRtcGetWakeupTime (&Enabled, &Pending, &Time, Global); + if ((Enabled) || (!EFI_ERROR (Status))) { return EFI_SUCCESS; - } else { + } + + // + // When wakeup time is disabled and invalid, reset wakeup time register to valid state + // but keep wakeup alarm disabled. + // + Time.Second = RTC_INIT_SECOND; + Time.Minute = RTC_INIT_MINUTE; + Time.Hour = RTC_INIT_HOUR; + Time.Day = RTC_INIT_DAY; + Time.Month = RTC_INIT_MONTH; + Time.Year = PcdGet16 (PcdMinimalValidYear); + Time.Nanosecond = 0; + Time.TimeZone = Global->SavedTimeZone; + Time.Daylight = Global->Daylight;; + + // + // Acquire RTC Lock to make access to RTC atomic + // + if (!EfiAtRuntime ()) { + EfiAcquireLock (&Global->RtcLock); + } + // + // Wait for up to 0.1 seconds for the RTC to be updated + // + Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout)); + if (EFI_ERROR (Status)) { + if (!EfiAtRuntime ()) { + EfiReleaseLock (&Global->RtcLock); + } + return EFI_DEVICE_ERROR; + } + + ConvertEfiTimeToRtcTime (&Time, RegisterB); + + // + // Set the Y/M/D info to variable as it has no corresponding hw registers. + // + Status = EfiSetVariable ( + L"RTCALARM", + &gEfiCallerIdGuid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, + sizeof (Time), + &Time + ); + if (EFI_ERROR (Status)) { + if (!EfiAtRuntime ()) { + EfiReleaseLock (&Global->RtcLock); + } return EFI_DEVICE_ERROR; } + + // + // Inhibit updates of the RTC + // + RegisterB.Bits.Set = 1; + RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); + + // + // Set RTC alarm time registers + // + RtcWrite (RTC_ADDRESS_SECONDS_ALARM, Time.Second); + RtcWrite (RTC_ADDRESS_MINUTES_ALARM, Time.Minute); + RtcWrite (RTC_ADDRESS_HOURS_ALARM, Time.Hour); + + // + // Allow updates of the RTC registers + // + RegisterB.Bits.Set = 0; + RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); + + // + // Release RTC Lock. + // + if (!EfiAtRuntime ()) { + EfiReleaseLock (&Global->RtcLock); + } + return EFI_SUCCESS; } /** @@ -256,7 +340,6 @@ PcRtcGetTime ( { EFI_STATUS Status; RTC_REGISTER_B RegisterB; - UINT8 Century; // // Check parameters for null pointer @@ -296,8 +379,6 @@ PcRtcGetTime ( Time->Month = RtcRead (RTC_ADDRESS_MONTH); Time->Year = RtcRead (RTC_ADDRESS_YEAR); - Century = RtcRead (RTC_ADDRESS_CENTURY); - // // Release RTC Lock. // @@ -314,7 +395,7 @@ PcRtcGetTime ( // // Make sure all field values are in correct range // - Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB); + Status = ConvertRtcTimeToEfiTime (Time, RegisterB); if (!EFI_ERROR (Status)) { Status = RtcTimeFieldsValid (Time); } @@ -360,7 +441,6 @@ PcRtcSetTime ( EFI_STATUS Status; EFI_TIME RtcTime; RTC_REGISTER_B RegisterB; - UINT8 Century; UINT32 TimerVar; if (Time == NULL) { @@ -392,6 +472,26 @@ PcRtcSetTime ( } return Status; } + + // + // Write timezone and daylight to RTC variable + // + TimerVar = Time->Daylight; + TimerVar = (UINT32) ((TimerVar << 16) | (UINT16)(Time->TimeZone)); + Status = EfiSetVariable ( + L"RTC", + &gEfiCallerIdGuid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, + sizeof (TimerVar), + &TimerVar + ); + if (EFI_ERROR (Status)) { + if (!EfiAtRuntime ()) { + EfiReleaseLock (&Global->RtcLock); + } + return EFI_DEVICE_ERROR; + } + // // Read Register B, and inhibit updates of the RTC // @@ -399,7 +499,7 @@ PcRtcSetTime ( RegisterB.Bits.Set = 1; RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); - ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century); + ConvertEfiTimeToRtcTime (&RtcTime, RegisterB); RtcWrite (RTC_ADDRESS_SECONDS, RtcTime.Second); RtcWrite (RTC_ADDRESS_MINUTES, RtcTime.Minute); @@ -407,7 +507,6 @@ PcRtcSetTime ( RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH, RtcTime.Day); RtcWrite (RTC_ADDRESS_MONTH, RtcTime.Month); RtcWrite (RTC_ADDRESS_YEAR, (UINT8) RtcTime.Year); - RtcWrite (RTC_ADDRESS_CENTURY, Century); // // Allow updates of the RTC registers @@ -427,17 +526,6 @@ PcRtcSetTime ( Global->SavedTimeZone = Time->TimeZone; Global->Daylight = Time->Daylight; - TimerVar = Time->Daylight; - TimerVar = (UINT32) ((TimerVar << 16) | (UINT16)(Time->TimeZone)); - Status = EfiSetVariable ( - L"RTC", - &gEfiCallerIdGuid, - EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE, - sizeof (TimerVar), - &TimerVar - ); - ASSERT_EFI_ERROR (Status); - return EFI_SUCCESS; } @@ -468,7 +556,6 @@ PcRtcGetWakeupTime ( EFI_STATUS Status; RTC_REGISTER_B RegisterB; RTC_REGISTER_C RegisterC; - UINT8 Century; EFI_TIME RtcTime; UINTN DataSize; @@ -516,8 +603,6 @@ PcRtcGetWakeupTime ( Time->TimeZone = Global->SavedTimeZone; Time->Daylight = Global->Daylight; - Century = RtcRead (RTC_ADDRESS_CENTURY); - // // Get the alarm info from variable // @@ -548,7 +633,7 @@ PcRtcGetWakeupTime ( // // Make sure all field values are in correct range // - Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB); + Status = ConvertRtcTimeToEfiTime (Time, RegisterB); if (!EFI_ERROR (Status)) { Status = RtcTimeFieldsValid (Time); } @@ -584,7 +669,6 @@ PcRtcSetWakeupTime ( EFI_STATUS Status; EFI_TIME RtcTime; RTC_REGISTER_B RegisterB; - UINT8 Century; EFI_TIME_CAPABILITIES Capabilities; ZeroMem (&RtcTime, sizeof (RtcTime)); @@ -635,27 +719,13 @@ PcRtcSetWakeupTime ( return EFI_DEVICE_ERROR; } // - // Read Register B, and inhibit updates of the RTC + // Read Register B // - RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B); - - RegisterB.Bits.Set = 1; - RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); + RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B); if (Enable) { - ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century); - - // - // Set RTC alarm time - // - RtcWrite (RTC_ADDRESS_SECONDS_ALARM, RtcTime.Second); - RtcWrite (RTC_ADDRESS_MINUTES_ALARM, RtcTime.Minute); - RtcWrite (RTC_ADDRESS_HOURS_ALARM, RtcTime.Hour); - - RegisterB.Bits.Aie = 1; - + ConvertEfiTimeToRtcTime (&RtcTime, RegisterB); } else { - RegisterB.Bits.Aie = 0; // // if the alarm is disable, record the current setting. // @@ -668,11 +738,6 @@ PcRtcSetWakeupTime ( RtcTime.TimeZone = Global->SavedTimeZone; RtcTime.Daylight = Global->Daylight; } - // - // Allow updates of the RTC registers - // - RegisterB.Bits.Set = 0; - RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); // // Set the Y/M/D info to variable as it has no corresponding hw registers. @@ -685,8 +750,36 @@ PcRtcSetWakeupTime ( &RtcTime ); if (EFI_ERROR (Status)) { + if (!EfiAtRuntime ()) { + EfiReleaseLock (&Global->RtcLock); + } return EFI_DEVICE_ERROR; } + + // + // Inhibit updates of the RTC + // + RegisterB.Bits.Set = 1; + RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); + + if (Enable) { + // + // Set RTC alarm time + // + RtcWrite (RTC_ADDRESS_SECONDS_ALARM, RtcTime.Second); + RtcWrite (RTC_ADDRESS_MINUTES_ALARM, RtcTime.Minute); + RtcWrite (RTC_ADDRESS_HOURS_ALARM, RtcTime.Hour); + + RegisterB.Bits.Aie = 1; + + } else { + RegisterB.Bits.Aie = 0; + } + // + // Allow updates of the RTC registers + // + RegisterB.Bits.Set = 0; + RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data); // // Release RTC Lock. @@ -732,7 +825,6 @@ CheckAndConvertBcd8ToDecimal8 ( @param Time On input, the time data read from RTC to convert On output, the time converted to UEFI format - @param Century Value of century read from RTC. @param RegisterB Value of Register B of RTC, indicating data mode and hour format. @@ -743,11 +835,11 @@ CheckAndConvertBcd8ToDecimal8 ( EFI_STATUS ConvertRtcTimeToEfiTime ( IN OUT EFI_TIME *Time, - IN UINT8 Century, IN RTC_REGISTER_B RegisterB ) { BOOLEAN IsPM; + UINT8 Century; if ((Time->Hour & 0x80) != 0) { IsPM = TRUE; @@ -765,14 +857,21 @@ ConvertRtcTimeToEfiTime ( Time->Minute = CheckAndConvertBcd8ToDecimal8 (Time->Minute); Time->Second = CheckAndConvertBcd8ToDecimal8 (Time->Second); } - Century = CheckAndConvertBcd8ToDecimal8 (Century); if (Time->Year == 0xff || Time->Month == 0xff || Time->Day == 0xff || - Time->Hour == 0xff || Time->Minute == 0xff || Time->Second == 0xff || - Century == 0xff) { + Time->Hour == 0xff || Time->Minute == 0xff || Time->Second == 0xff) { return EFI_INVALID_PARAMETER; } + // + // For minimal/maximum year range [1970, 2069], + // Century is 19 if RTC year >= 70, + // Century is 20 otherwise. + // + Century = (UINT8) (PcdGet16 (PcdMinimalValidYear) / 100); + if (Time->Year < PcdGet16 (PcdMinimalValidYear) % 100) { + Century++; + } Time->Year = (UINT16) (Century * 100 + Time->Year); // @@ -850,8 +949,8 @@ RtcTimeFieldsValid ( IN EFI_TIME *Time ) { - if (Time->Year < 1998 || - Time->Year > 2099 || + if (Time->Year < PcdGet16 (PcdMinimalValidYear) || + Time->Year > PcdGet16 (PcdMaximalValidYear) || Time->Month < 1 || Time->Month > 12 || (!DayValid (Time)) || @@ -948,14 +1047,11 @@ IsLeapYear ( @param Time On input, the time data read from UEFI to convert On output, the time converted to RTC format @param RegisterB Value of Register B of RTC, indicating data mode - @param Century It is set according to EFI_TIME Time. - **/ VOID ConvertEfiTimeToRtcTime ( IN OUT EFI_TIME *Time, - IN RTC_REGISTER_B RegisterB, - OUT UINT8 *Century + IN RTC_REGISTER_B RegisterB ) { BOOLEAN IsPM; @@ -976,10 +1072,8 @@ ConvertEfiTimeToRtcTime ( } } // - // Set the Time/Date/Daylight Savings values. + // Set the Time/Date values. // - *Century = DecimalToBcd8 ((UINT8) (Time->Year / 100)); - Time->Year = (UINT16) (Time->Year % 100); if (RegisterB.Bits.Dm == 0) {