]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPlatformPkg/Library/PL031RealTimeClockLib/PL031RealTimeClockLib.c
ArmPlatformPkg: reorganize PL011 code
[mirror_edk2.git] / ArmPlatformPkg / Library / PL031RealTimeClockLib / PL031RealTimeClockLib.c
index fdda569e715912ff2d92d30f8ac79dec82dc714c..1334ad446cd9fea0912869357ea13e4560a33aa8 100644 (file)
@@ -1,10 +1,9 @@
 /** @file\r
   Implement EFI RealTimeClock runtime services via RTC Lib.\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
+  Copyright (c) 2011 - 2014, ARM Ltd. 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
 \r
 **/\r
 \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/PcdLib.h>\r
+#include <Library/DxeServicesTableLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/UefiRuntimeServicesTableLib.h>\r
+#include <Library/UefiRuntimeLib.h>\r
+\r
+#include <Protocol/RealTimeClock.h>\r
+\r
+#include <Guid/GlobalVariable.h>\r
+#include <Guid/EventGroup.h>\r
+\r
+#include <Drivers/PL031RealTimeClock.h>\r
+\r
+#include <Library/TimeBaseLib.h>\r
+\r
+STATIC BOOLEAN                mPL031Initialized = FALSE;\r
+STATIC EFI_EVENT              mRtcVirtualAddrChangeEvent;\r
+STATIC UINTN                  mPL031RtcBase;\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 (mPL031RtcBase + PL031_RTC_PCELL_ID0) != 0x0D)\r
+      || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID1) != 0xF0)\r
+      || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID2) != 0x05)\r
+      || (MmioRead8 (mPL031RtcBase + PL031_RTC_PCELL_ID3) != 0xB1)) {\r
+    Status = EFI_NOT_FOUND;\r
+    goto EXIT;\r
+  }\r
+\r
+  // Check if this PrimeCell Peripheral is the PL031 Real Time Clock\r
+  if (  (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID0) != 0x31)\r
+      || (MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID1) != 0x10)\r
+      || ((MmioRead8 (mPL031RtcBase + PL031_RTC_PERIPH_ID2) & 0xF) != 0x04)\r
+      || (MmioRead8 (mPL031RtcBase + 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 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER) & PL031_SET_IRQ_MASK) != PL031_SET_IRQ_MASK) {\r
+    MmioOr32 (mPL031RtcBase + PL031_RTC_IMSC_IRQ_MASK_SET_CLEAR_REGISTER, PL031_SET_IRQ_MASK);\r
+  }\r
+\r
+  // Clear any existing interrupts\r
+  if ((MmioRead32 (mPL031RtcBase + PL031_RTC_RIS_RAW_IRQ_STATUS_REGISTER) & PL031_IRQ_TRIGGERED) == PL031_IRQ_TRIGGERED) {\r
+    MmioOr32 (mPL031RtcBase + PL031_RTC_ICR_IRQ_CLEAR_REGISTER, PL031_CLEAR_IRQ);\r
+  }\r
+\r
+  // Start the clock counter\r
+  if ((MmioRead32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER) & PL031_RTC_ENABLED) != PL031_RTC_ENABLED) {\r
+    MmioOr32 (mPL031RtcBase + PL031_RTC_CR_CONTROL_REGISTER, PL031_RTC_ENABLED);\r
+  }\r
 \r
+  mPL031Initialized = TRUE;\r
+\r
+  EXIT:\r
+  return Status;\r
+}\r
 \r
 /**\r
   Returns the current time and date information, and the time-keeping capabilities\r
   of the hardware platform.\r
 \r
-  @param  Time                  A pointer to storage to receive a snapshot of the current time.\r
-  @param  Capabilities          An optional pointer to a buffer to receive the real time clock\r
-                                device's capabilities.\r
+  @param  Time                   A pointer to storage to receive a snapshot of the current time.\r
+  @param  Capabilities           An optional pointer to a buffer to receive the real time clock\r
+                                 device's capabilities.\r
 \r
-  @retval EFI_SUCCESS           The operation completed successfully.\r
-  @retval EFI_INVALID_PARAMETER Time is NULL.\r
-  @retval EFI_DEVICE_ERROR      The time could not be retrieved due to hardware error.\r
+  @retval EFI_SUCCESS            The operation completed successfully.\r
+  @retval EFI_INVALID_PARAMETER  Time is NULL.\r
+  @retval EFI_DEVICE_ERROR       The time could not be retrieved due to hardware error.\r
+  @retval EFI_SECURITY_VIOLATION The time could not be retrieved due to an authentication failure.\r
 \r
 **/\r
 EFI_STATUS\r
 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
+  UINT32      EpochSeconds;\r
+\r
+  // Ensure Time is a valid pointer\r
+  if (Time == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  // Initialize the hardware if not already done\r
+  if (!mPL031Initialized) {\r
+    Status = InitializePL031 ();\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  EpochSeconds = MmioRead32 (mPL031RtcBase + PL031_RTC_DR_DATA_REGISTER);\r
+\r
+  // Adjust for the correct time zone\r
+  // The timezone setting also reflects the DST setting of the clock\r
+  if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r
+    EpochSeconds += Time->TimeZone * SEC_PER_MIN;\r
+  } else 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
+  // Convert from internal 32-bit time to UEFI time\r
+  EpochToEfiTime (EpochSeconds, Time);\r
+\r
+  // Update the Capabilities info\r
+  if (Capabilities != NULL) {\r
+    // PL031 runs at frequency 1Hz\r
+    Capabilities->Resolution  = PL031_COUNTS_PER_SECOND;\r
+    // Accuracy in ppm multiplied by 1,000,000, e.g. for 50ppm set 50,000,000\r
+    Capabilities->Accuracy    = (UINT32)PcdGet32 (PcdPL031RtcPpmAccuracy);\r
+    // FALSE: Setting the time does not clear the values below the resolution level\r
+    Capabilities->SetsToZero  = FALSE;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 \r
@@ -62,13 +184,43 @@ LibGetTime (
 EFI_STATUS\r
 EFIAPI\r
 LibSetTime (\r
-  IN EFI_TIME                *Time\r
+  IN  EFI_TIME                *Time\r
   )\r
 {\r
-  //\r
-  // Use Time, to set the time in your RTC hardware\r
-  //\r
-  return EFI_DEVICE_ERROR;\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
+  if ((Time->Year < 1970) || (Time->Year >= 2106)) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  // Initialize the hardware if not already done\r
+  if (!mPL031Initialized) {\r
+    Status = InitializePL031 ();\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  EpochSeconds = EfiTimeToEpoch (Time);\r
+\r
+  // Adjust for the correct time zone, i.e. convert to UTC time zone\r
+  // The timezone setting also reflects the DST setting of the clock\r
+  if (Time->TimeZone != EFI_UNSPECIFIED_TIMEZONE) {\r
+    EpochSeconds -= Time->TimeZone * SEC_PER_MIN;\r
+  } else 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
+  // Set the PL031\r
+  MmioWrite32 (mPL031RtcBase + PL031_RTC_LR_LOAD_REGISTER, EpochSeconds);\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 \r
@@ -121,7 +273,30 @@ LibSetWakeupTime (
   return EFI_UNSUPPORTED;\r
 }\r
 \r
+/**\r
+  Fixup internal data so that EFI can be call in virtual mode.\r
+  Call the passed in Child Notify event and convert any pointers in\r
+  lib to virtual mode.\r
 \r
+  @param[in]    Event   The Event that is being processed\r
+  @param[in]    Context Event Context\r
+**/\r
+VOID\r
+EFIAPI\r
+LibRtcVirtualNotifyEvent (\r
+  IN EFI_EVENT        Event,\r
+  IN VOID             *Context\r
+  )\r
+{\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 transitions to calling in virtual mode, all future\r
+  // runtime calls will be made in virtual mode.\r
+  //\r
+  EfiConvertPointer (0x0, (VOID**)&mPL031RtcBase);\r
+  return;\r
+}\r
 \r
 /**\r
   This is the declaration of an EFI image entry point. This can be the entry point to an application\r
@@ -140,33 +315,48 @@ 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
-}\r
-\r
+  EFI_STATUS    Status;\r
+  EFI_HANDLE    Handle;\r
+\r
+  // Initialize RTC Base Address\r
+  mPL031RtcBase = PcdGet32 (PcdPL031RtcBase);\r
+\r
+  // Declare the controller as EFI_MEMORY_RUNTIME\r
+  Status = gDS->AddMemorySpace (\r
+                  EfiGcdMemoryTypeMemoryMappedIo,\r
+                  mPL031RtcBase, SIZE_4KB,\r
+                  EFI_MEMORY_UC | EFI_MEMORY_RUNTIME\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = gDS->SetMemorySpaceAttributes (mPL031RtcBase, SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  // Install the protocol\r
+  Handle = NULL;\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &Handle,\r
+                  &gEfiRealTimeClockArchProtocolGuid,  NULL,\r
+                  NULL\r
+                 );\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
-/**\r
-  Fixup internal data so that EFI can be call in virtual mode.\r
-  Call the passed in Child Notify event and convert any pointers in\r
-  lib to virtual mode.\r
-\r
-  @param[in]    Event   The Event that is being processed\r
-  @param[in]    Context Event Context\r
-**/\r
-VOID\r
-EFIAPI\r
-LibRtcVirtualNotifyEvent (\r
-  IN EFI_EVENT        Event,\r
-  IN VOID             *Context\r
-  )\r
-{\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
-  // runtime calls will be made in virtual mode.\r
+  // Register for the virtual address change event\r
   //\r
-  return;\r
+  Status = gBS->CreateEventEx (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  LibRtcVirtualNotifyEvent,\r
+                  NULL,\r
+                  &gEfiEventVirtualAddressChangeGuid,\r
+                  &mRtcVirtualAddrChangeEvent\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return Status;\r
 }\r