public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Narinder Dhillon" <ndhillon@marvell.com>
To: <devel@edk2.groups.io>
Cc: <quic_llindhol@quicinc.com>, <mw@semihalf.com>,
	<sbalcerak@marvell.com>, Narinder Dhillon <ndhillon@marvell.com>
Subject: [edk2-devel] [edk2-platforms PATCH v2 5/8] Silicon/Marvell: RTC driver
Date: Wed, 20 Dec 2023 16:54:24 -0800	[thread overview]
Message-ID: <20231221005427.13932-6-ndhillon@marvell.com> (raw)
In-Reply-To: <20231221005427.13932-1-ndhillon@marvell.com>

From: Narinder Dhillon <ndhillon@marvell.com>

Marvell Odyssey SoC does not have RTC on chip. This patch provides a
dummy RTC driver to generate architectural protocol and help boot
Odyssey SoC.

Signed-off-by: Narinder Dhillon <ndhillon@marvell.com>
---
 .../Marvell/Drivers/Null/RtcNull/RtcNullDxe.c | 280 ++++++++++++++++++
 .../Marvell/Drivers/Null/RtcNull/RtcNullDxe.h |  37 +++
 .../Drivers/Null/RtcNull/RtcNullDxe.inf       |  46 +++
 3 files changed, 363 insertions(+)
 create mode 100644 Silicon/Marvell/Drivers/Null/RtcNull/RtcNullDxe.c
 create mode 100644 Silicon/Marvell/Drivers/Null/RtcNull/RtcNullDxe.h
 create mode 100644 Silicon/Marvell/Drivers/Null/RtcNull/RtcNullDxe.inf

diff --git a/Silicon/Marvell/Drivers/Null/RtcNull/RtcNullDxe.c b/Silicon/Marvell/Drivers/Null/RtcNull/RtcNullDxe.c
new file mode 100644
index 0000000000..8a7956f35d
--- /dev/null
+++ b/Silicon/Marvell/Drivers/Null/RtcNull/RtcNullDxe.c
@@ -0,0 +1,280 @@
+/** @file
+*
+* SPDX-License-Identifier: BSD-2-Clause-Patent
+* https://spdx.org/licenses
+*
+* Copyright (C) 2022 Marvell
+*
+* Source file for NULL RTC Driver
+*
+**/
+
+#include <Uefi.h>                                 // Base defines
+#include <Library/DebugLib.h>                     // DEBUG
+#include <Library/MemoryAllocationLib.h>          // AllocateRuntimeZeroPool
+#include <Library/BaseMemoryLib.h>                // ZeroMem
+#include <Library/UefiBootServicesTableLib.h>     // gBS
+#include <Library/UefiRuntimeLib.h>               // EfiConvertPointer
+#include <Library/UefiRuntimeServicesTableLib.h>  // gRT
+
+#include "RtcNullDxe.h"
+
+// all variables used across the driver
+RTC_NULL_PRIVATE_DATA        *mRtcPrivateData;
+STATIC EFI_EVENT              mRtcVirtualAddressChangeEvent;
+
+STATIC CONST INTN             DayOfMonth[12] =
+                                { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+
+STATIC
+BOOLEAN
+IsLeapYear(IN EFI_TIME   *Time)
+{
+  if (Time->Year % 4 == 0) {
+    if (Time->Year % 100 == 0) {
+      if (Time->Year % 400 == 0) {
+        return TRUE;
+      } else {
+        return FALSE;
+      }
+    } else {
+      return TRUE;
+    }
+  } else {
+    return FALSE;
+  }
+}
+
+BOOLEAN DayValid(IN  EFI_TIME  *Time)
+{
+  if (Time->Day < 1 ||
+    Time->Day > DayOfMonth[Time->Month - 1] ||
+    (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))) {
+    return FALSE;
+  }
+
+  return TRUE;
+}
+
+EFI_STATUS
+GetDateTime(
+  IN  RTC_NULL_PRIVATE_DATA *PrivateData,
+  OUT EFI_TIME               *Time)
+{
+
+  if (PrivateData == NULL || Time == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  ZeroMem(Time, sizeof(EFI_TIME));
+
+  return EFI_SUCCESS;
+}
+
+EFI_STATUS
+SetDateTime(IN RTC_NULL_PRIVATE_DATA *PrivateData,
+                  IN EFI_TIME               *Time)
+{
+  if (PrivateData == NULL || Time == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if ( (Time->Month < 1) || (Time->Month > 12) ||
+       (Time->Second > 59) || (Time->Minute > 59) ||
+       (Time->Hour > 23) || (!DayValid(Time)) ||
+       (Time->Year < 1998) || (Time->Year > 2099) ||
+       (Time->Nanosecond > 999999999) ||
+       (Time->TimeZone < -1440) || ((Time->TimeZone > 1440) &&
+         (Time->TimeZone != 2047))) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Returns the current time and date information, and the time-keeping capabilities
+  of the hardware platform.
+
+  @param  Time                  A pointer to storage to receive a snapshot of the current time.
+  @param  Capabilities          An optional pointer to a buffer to receive the real time clock
+                                device's capabilities.
+
+  @retval EFI_SUCCESS           The operation completed successfully.
+  @retval EFI_INVALID_PARAMETER Time is NULL.
+  @retval EFI_DEVICE_ERROR      The time could not be retrieved due to hardware error.
+
+**/
+EFI_STATUS
+    EFIAPI
+GetTime(OUT EFI_TIME * Time, OUT EFI_TIME_CAPABILITIES * Capabilities)
+{
+  if (Time == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if (mRtcPrivateData->Initialized == FALSE) {
+    return EFI_UNSUPPORTED;
+  }
+
+  return GetDateTime (mRtcPrivateData, Time);
+}
+
+
+
+/**
+  Sets the current local time and date information.
+
+  @param  Time                  A pointer to the current time.
+
+  @retval EFI_SUCCESS           The operation completed successfully.
+  @retval EFI_INVALID_PARAMETER A time field is out of range.
+  @retval EFI_DEVICE_ERROR      The time could not be set due due to hardware error.
+
+**/
+EFI_STATUS EFIAPI SetTime(IN EFI_TIME * Time)
+{
+  if (Time == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  if (mRtcPrivateData->Initialized == FALSE) {
+    return EFI_UNSUPPORTED;
+  }
+
+  return SetDateTime (mRtcPrivateData, Time);
+}
+
+
+/**
+  Returns the current wakeup alarm clock setting.
+
+  @param  Enabled               Indicates if the alarm is currently enabled or disabled.
+  @param  Pending               Indicates if the alarm signal is pending and requires acknowledgement.
+  @param  Time                  The current alarm setting.
+
+  @retval EFI_SUCCESS           The alarm settings were returned.
+  @retval EFI_INVALID_PARAMETER Any parameter is NULL.
+  @retval EFI_DEVICE_ERROR      The wakeup time could not be retrieved due to a hardware error.
+
+**/
+EFI_STATUS
+    EFIAPI
+GetWakeupTime(OUT BOOLEAN * Enabled,
+              OUT BOOLEAN * Pending, OUT EFI_TIME * Time)
+{
+  return EFI_UNSUPPORTED;
+}
+
+
+/**
+  Sets the system wakeup alarm clock time.
+
+  @param  Enabled               Enable or disable the wakeup alarm.
+  @param  Time                  If Enable is TRUE, the time to set the wakeup alarm for.
+
+  @retval EFI_SUCCESS           If Enable is TRUE, then the wakeup alarm was enabled. If
+                                Enable is FALSE, then the wakeup alarm was disabled.
+  @retval EFI_INVALID_PARAMETER A time field is out of range.
+  @retval EFI_DEVICE_ERROR      The wakeup time could not be set due to a hardware error.
+  @retval EFI_UNSUPPORTED       A wakeup timer is not supported on this platform.
+
+**/
+EFI_STATUS EFIAPI SetWakeupTime(IN BOOLEAN Enabled, OUT EFI_TIME * Time)
+{
+    return EFI_UNSUPPORTED;
+}
+
+
+// Convert the mSmbus as well since the SmbusLib leaves this to the runtine DXEs
+
+EFIAPI VOID
+RtcVirtualNotifyEvent(IN EFI_EVENT Event, IN VOID * Context)
+{
+  EfiConvertPointer (0x0, (VOID **) &mRtcPrivateData);
+}
+
+/**
+  The Entry Point of module. It follows the standard UEFI driver model.
+
+  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
+  @param[in] SystemTable    A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS   The entry point is executed successfully.
+  @retval other         Some error occurs when executing this entry point.
+
+**/
+EFI_STATUS
+    EFIAPI
+RtcNullDxeInitialize (
+  IN EFI_HANDLE ImageHandle,
+  IN EFI_SYSTEM_TABLE * SystemTable)
+{
+  EFI_TIME               Time;
+  RTC_NULL_PRIVATE_DATA *Private = NULL;
+  EFI_STATUS             Status = EFI_SUCCESS;
+
+  DEBUG ((DEBUG_INFO, "RtcNullDxeInitialize\n"));
+
+  /* Allocate the private data */
+  Private = AllocateRuntimeZeroPool (sizeof (RTC_NULL_PRIVATE_DATA));
+
+  if (Private == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    DEBUG ((DEBUG_ERROR, "RtcDxeInitialize: %r\n", Status));
+    goto Exit;
+  }
+
+  mRtcPrivateData = Private;
+
+  Private->Initialized = FALSE;
+  Private->Bus         = 0xFF;
+  Private->SlaveAddr   = 0xFF;
+
+  /* Check clock and init it to UNIX start time */
+  Status = GetDateTime (mRtcPrivateData, &Time);
+
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "RtcNullDxeInitialize: %r\n", Status));
+    goto Exit;
+  }
+
+  if (Time.Year == 1900) {
+    Time.Day = 1;
+    Time.Month = 1;
+    Time.Year = 1998;
+    Time.Second = 0;
+    Time.Minute = 0;
+    Time.Hour = 0;
+    Time.Daylight = 0;
+    Time.TimeZone = 0;
+
+    Status = SetDateTime (mRtcPrivateData, &Time);
+
+    if (EFI_ERROR (Status)) {
+      DEBUG ((DEBUG_ERROR, "RtcDxeInitialize: %r\n", Status));
+      goto Exit;
+    }
+  }
+
+Exit:
+  gRT->GetTime       = GetTime;
+  gRT->SetTime       = SetTime;
+  gRT->GetWakeupTime = GetWakeupTime;
+  gRT->SetWakeupTime = SetWakeupTime;
+
+  Status = gBS->InstallMultipleProtocolInterfaces (&Private->RtcHandle,
+                                                   &gEfiRealTimeClockArchProtocolGuid,
+                                                   NULL,
+                                                   NULL);
+
+  Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL,
+                               TPL_NOTIFY,
+                               RtcVirtualNotifyEvent,
+                               NULL,
+                               &gEfiEventVirtualAddressChangeGuid,
+                               &mRtcVirtualAddressChangeEvent);
+  ASSERT_EFI_ERROR(Status);
+
+  return Status;
+}
diff --git a/Silicon/Marvell/Drivers/Null/RtcNull/RtcNullDxe.h b/Silicon/Marvell/Drivers/Null/RtcNull/RtcNullDxe.h
new file mode 100644
index 0000000000..dca99ef8f9
--- /dev/null
+++ b/Silicon/Marvell/Drivers/Null/RtcNull/RtcNullDxe.h
@@ -0,0 +1,37 @@
+/** @file
+*
+* SPDX-License-Identifier: BSD-2-Clause-Patent
+* https://spdx.org/licenses
+*
+* Copyright (C) 2022 Marvell
+*
+* Header file for NULL RTC Driver
+*
+**/
+
+#ifndef _RTC_NULL_DXE_H_
+#define _RTC_NULL_DXE_H_
+
+#include <Uefi.h>
+
+#include <Library/BaseLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/DebugLib.h>
+#include <Library/UefiBootServicesTableLib.h> // gBS
+#include <Library/BaseMemoryLib.h>            // ZeroMem
+
+//
+//  Private data for driver.
+//
+#define RTC_NULL_DXE_PRIVATE_DATA_SIGNATURE  SIGNATURE_32( 'R', 'T', 'C', '_' )
+
+typedef struct {
+    UINT32     Signature;
+    UINT8      Bus;
+    UINT8      SlaveAddr;
+    EFI_HANDLE RtcHandle;
+    BOOLEAN    Initialized;
+} RTC_NULL_PRIVATE_DATA;
+
+
+#endif                          //_RTC_NULL_DXE_H_
diff --git a/Silicon/Marvell/Drivers/Null/RtcNull/RtcNullDxe.inf b/Silicon/Marvell/Drivers/Null/RtcNull/RtcNullDxe.inf
new file mode 100644
index 0000000000..d262e971fc
--- /dev/null
+++ b/Silicon/Marvell/Drivers/Null/RtcNull/RtcNullDxe.inf
@@ -0,0 +1,46 @@
+#/** @file
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#  https://spdx.org/licenses
+#
+#  Copyright (C) 2022 Marvell
+#  Module description file of RTC NULL driver.
+#
+#**/
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = RtcNullDxe
+  FILE_GUID                      = 9c0a0971-b0f6-442e-ac01-0a3eb52c457d
+  MODULE_TYPE                    = DXE_RUNTIME_DRIVER
+  VERSION_STRING                 = 1.0
+
+  ENTRY_POINT                    = RtcNullDxeInitialize
+
+
+[Sources]
+  RtcNullDxe.c
+  RtcNullDxe.h
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+
+[LibraryClasses]
+  DebugLib
+  MemoryAllocationLib
+  UefiDriverEntryPoint
+  BaseMemoryLib
+  UefiBootServicesTableLib
+  UefiRuntimeLib
+  UefiRuntimeServicesTableLib
+
+[Guids]
+  gEfiEventVirtualAddressChangeGuid
+
+[Protocols]
+  gEfiRealTimeClockArchProtocolGuid      ## PRODUCES
+
+[Depex]
+  TRUE
+
-- 
2.34.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#112783): https://edk2.groups.io/g/devel/message/112783
Mute This Topic: https://groups.io/mt/103292513/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



  parent reply	other threads:[~2023-12-21  0:54 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-21  0:54 [edk2-devel] [edk2-platforms PATCH v2 0/8] Silicon/Marvell/OdysseyPkg: Narinder Dhillon
2023-12-21  0:54 ` [edk2-devel] [edk2-platforms PATCH v2 1/8] Silicon/Marvell: New Marvell Odyssey processor Narinder Dhillon
2024-01-12 11:14   ` Marcin Wojtas via groups.io
2023-12-21  0:54 ` [edk2-devel] [edk2-platforms PATCH v2 2/8] Silicon/Marvell: Odyssey ArmPlatformLib Narinder Dhillon
2024-01-12 11:27   ` Marcin Wojtas via groups.io
2023-12-21  0:54 ` [edk2-devel] [edk2-platforms PATCH v2 3/8] Silicon/Marvell: Odyssey SmcLib Narinder Dhillon
2024-01-12 10:48   ` Marcin Wojtas via groups.io
2023-12-21  0:54 ` [edk2-devel] [edk2-platforms PATCH v2 4/8] Silicon/Marvell: Odyssey watchdog driver Narinder Dhillon
2024-01-12 11:34   ` Marcin Wojtas via groups.io
2024-01-15 20:26     ` Narinder Dhillon
2023-12-21  0:54 ` Narinder Dhillon [this message]
2024-01-12 11:44   ` [edk2-devel] [edk2-platforms PATCH v2 5/8] Silicon/Marvell: RTC driver Marcin Wojtas via groups.io
2023-12-21  0:54 ` [edk2-devel] [edk2-platforms PATCH v2 6/8] Silicon/Marvell: Device tree driver Narinder Dhillon
2024-01-12 12:00   ` Marcin Wojtas via groups.io
2024-01-12 12:01     ` Marcin Wojtas via groups.io
2023-12-21  0:54 ` [edk2-devel] [edk2-platforms PATCH v2 7/8] Silicon/Marvell: Driver to dump board configuration Narinder Dhillon
2024-01-12 12:18   ` Marcin Wojtas via groups.io
2024-01-13  0:58     ` Narinder Dhillon
2023-12-21  0:54 ` [edk2-devel] [edk2-platforms PATCH v2 8/8] Silicon/Marvell: Odyssey project description files Narinder Dhillon
2024-01-12 13:25 ` [edk2-devel] [edk2-platforms PATCH v2 0/8] Silicon/Marvell/OdysseyPkg: Marcin Wojtas via groups.io

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231221005427.13932-6-ndhillon@marvell.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox