public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Jeremy Linton" <jeremy.linton@arm.com>
To: devel@edk2.groups.io
Cc: ardb+tianocore@kernel.org, quic_llindhol@quicinc.com,
	Jeremy Linton <jeremy.linton@arm.com>
Subject: [edk2-devel] [RFC 6/6] Platform/RaspberryPi: Add menu and build options for HW RTC
Date: Wed, 10 Jan 2024 18:04:26 -0600	[thread overview]
Message-ID: <20240111000426.2735007-7-jeremy.linton@arm.com> (raw)
In-Reply-To: <20240111000426.2735007-1-jeremy.linton@arm.com>

Now that the i2c drivers, config setup, and DS1307 driver are
in place, lets add the drivers to the build. We also add a menu
item to enable/disable it since the device I have:

https://www.amazon.com/Makerfire%C2%AE-Raspberry-Module-DS1307-Battery/dp/B00ZOXWHK4

Doesn't utilize the HAT protocol so it cannot be detected.

Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
---
 .../RaspberryPi/Drivers/ConfigDxe/ConfigDxe.c | 22 ++++++++++++++
 .../Drivers/ConfigDxe/ConfigDxe.inf           |  1 +
 .../Drivers/ConfigDxe/ConfigDxeHii.uni        |  6 ++++
 .../Drivers/ConfigDxe/ConfigDxeHii.vfr        | 16 ++++++++++
 Platform/RaspberryPi/Include/ConfigVars.h     |  4 +++
 Platform/RaspberryPi/RPi3/RPi3.dsc            |  7 +++++
 Platform/RaspberryPi/RPi4/RPi4.dsc            | 30 +++++++++++++++++++
 Platform/RaspberryPi/RPi4/RPi4.fdf            | 19 ++++++++++++
 Platform/RaspberryPi/RaspberryPi.dec          |  2 ++
 9 files changed, 107 insertions(+)

diff --git a/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.c b/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.c
index fec8f63ea8..0397941a06 100644
--- a/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.c
+++ b/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.c
@@ -319,6 +319,16 @@ SetupVariables (
       ASSERT_EFI_ERROR (Status);
     }
 
+    Size = sizeof (UINT32);
+    Status = gRT->GetVariable (L"HwRtc",
+                               &gConfigDxeFormSetGuid,
+                               NULL, &Size, &Var32);
+    if (EFI_ERROR (Status)) {
+      Status = PcdSet32S (PcdHwRtc, PcdGet32 (PcdHwRtc));
+      ASSERT_EFI_ERROR (Status);
+    }
+
+
   } else {
     /*
      * Disable PCIe and XHCI
@@ -716,6 +726,18 @@ ApplyVariables (
     GpioPinFuncSet (33, GPIO_FSEL_ALT3);
   }
 
+  // Assure I2C1 is selected on header
+  if (PcdGet32 (PcdHwRtc)) {
+    UINT32 ClockRate;
+    DEBUG ((DEBUG_INFO, "Enable SDA1\n"));
+    GpioPinFuncSet (2, GPIO_FSEL_ALT0);
+    GpioPinFuncSet (3, GPIO_FSEL_ALT0);
+
+    mFwProtocol->GetClockRate (RPI_MBOX_CLOCK_RATE_CORE, &ClockRate);
+    ClockRate/=50000; //50Khz slow it down a bit initially
+
+    MmioWrite32 (BCM2836_I2C1_OFFSET + BCM2836_SOC_REGISTERS + BCM2835_I2C_DIV, ClockRate ); //was 5dc which assumes a 150Mhz clock, when we are usually at 500Mhz?
+  }
 }
 
 
diff --git a/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.inf b/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.inf
index e422e5ba5c..4c213174ce 100644
--- a/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.inf
+++ b/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.inf
@@ -98,6 +98,7 @@
   gRaspberryPiTokenSpaceGuid.PcdMiniUartClockRate
   gRaspberryPiTokenSpaceGuid.PcdXhciReload
   gRaspberryPiTokenSpaceGuid.PcdEnableGpio
+  gRaspberryPiTokenSpaceGuid.PcdHwRtc
 
 [Depex]
   gPcdProtocolGuid AND gRaspberryPiFirmwareProtocolGuid
diff --git a/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxeHii.uni b/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxeHii.uni
index fb06d46a61..8da143d519 100644
--- a/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxeHii.uni
+++ b/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxeHii.uni
@@ -72,6 +72,12 @@
 #string STR_ADVANCED_ENABLEGPIO_DISABLE #language en-US "Disabled"
 #string STR_ADVANCED_ENABLEGPIO_ENABLE  #language en-US "Enable"
 
+#string STR_ADVANCED_ENABLEHWRTC_PROMPT  #language en-US "Enable Hardware RTC"
+#string STR_ADVANCED_ENABLEHWRTC_HELP    #language en-US "A DS1307 hardware real time clock is attached on I2C1."
+#string STR_ADVANCED_ENABLEHWRTC_DISABLE #language en-US "Disabled"
+#string STR_ADVANCED_ENABLEHWRTC_ENABLE  #language en-US "Enable"
+
+
 #string STR_ADVANCED_ASSET_TAG_PROMPT #language en-US "Asset Tag"
 #string STR_ADVANCED_ASSET_TAG_HELP   #language en-US "Set the system Asset Tag"
 
diff --git a/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxeHii.vfr b/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxeHii.vfr
index 04eb0a15a2..b7146b7e8c 100644
--- a/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxeHii.vfr
+++ b/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxeHii.vfr
@@ -71,6 +71,11 @@ formset
       name  = EnableGpio,
       guid  = CONFIGDXE_FORM_SET_GUID;
 
+    efivarstore ADVANCED_ENABLEHWRTC_VARSTORE_DATA,
+      attribute = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
+      name  = HwRtc,
+      guid  = CONFIGDXE_FORM_SET_GUID;
+
     efivarstore SYSTEM_TABLE_MODE_VARSTORE_DATA,
       attribute = EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
       name  = SystemTableMode,
@@ -259,6 +264,17 @@ formset
             option text = STRING_TOKEN(STR_ADVANCED_ENABLEGPIO_ENABLE), value = 1, flags = 0;
           endoneof;
         endif;
+
+        grayoutif ideqval SystemTableMode.Mode == SYSTEM_TABLE_MODE_DT;
+          oneof varid = HwRtc.Value,
+              prompt      = STRING_TOKEN(STR_ADVANCED_ENABLEHWRTC_PROMPT),
+              help        = STRING_TOKEN(STR_ADVANCED_ENABLEHWRTC_HELP),
+              flags       = NUMERIC_SIZE_4 | INTERACTIVE | RESET_REQUIRED,
+              option text = STRING_TOKEN(STR_ADVANCED_ENABLEHWRTC_DISABLE), value = 0, flags = DEFAULT;
+              option text = STRING_TOKEN(STR_ADVANCED_ENABLEHWRTC_ENABLE), value = 1, flags = 0;
+          endoneof;
+        endif;
+
 #endif
         string varid = AssetTag.AssetTag,
             prompt  = STRING_TOKEN(STR_ADVANCED_ASSET_TAG_PROMPT),
diff --git a/Platform/RaspberryPi/Include/ConfigVars.h b/Platform/RaspberryPi/Include/ConfigVars.h
index 43a39891d4..69dda96051 100644
--- a/Platform/RaspberryPi/Include/ConfigVars.h
+++ b/Platform/RaspberryPi/Include/ConfigVars.h
@@ -84,6 +84,10 @@ typedef struct {
   UINT32 Value;
 } ADVANCED_ENABLEGPIO_VARSTORE_DATA;
 
+typedef struct {
+  UINT32 Value;
+} ADVANCED_ENABLEHWRTC_VARSTORE_DATA;
+
 typedef struct {
 #define SYSTEM_TABLE_MODE_ACPI 0
 #define SYSTEM_TABLE_MODE_BOTH 1
diff --git a/Platform/RaspberryPi/RPi3/RPi3.dsc b/Platform/RaspberryPi/RPi3/RPi3.dsc
index 55da7d5870..10d57c5e03 100644
--- a/Platform/RaspberryPi/RPi3/RPi3.dsc
+++ b/Platform/RaspberryPi/RPi3/RPi3.dsc
@@ -541,6 +541,13 @@
   #
   gRaspberryPiTokenSpaceGuid.PcdEnableGpio|L"EnableGpio"|gConfigDxeFormSetGuid|0x0|1
 
+  # Utilize RTC on I2C1
+  #
+  # 0  - No
+  # 1  - Yes
+  #
+  gRaspberryPiTokenSpaceGuid.PcdHwRtc|L"HwRtc"|gConfigDxeFormSetGuid|0x0|0
+
   #
   # Common UEFI ones.
   #
diff --git a/Platform/RaspberryPi/RPi4/RPi4.dsc b/Platform/RaspberryPi/RPi4/RPi4.dsc
index a49b3433ac..170e45ae7e 100644
--- a/Platform/RaspberryPi/RPi4/RPi4.dsc
+++ b/Platform/RaspberryPi/RPi4/RPi4.dsc
@@ -368,6 +368,12 @@
   # Default platform supported RFC 4646 languages: (American) English
   gEfiMdePkgTokenSpaceGuid.PcdUefiVariableDefaultPlatformLangCodes|"en-US"
 
+  #
+  # RTC Pcds
+  #
+  gDs1307RtcLibTokenSpaceGuid.PcdI2cSlaveAddress|0x68
+  gDs1307RtcLibTokenSpaceGuid.PcdI2cBusFrequency|100000
+
 [LibraryClasses.common]
   ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf
   ArmMmuLib|ArmPkg/Library/ArmMmuLib/ArmMmuBaseLib.inf
@@ -562,6 +568,14 @@
   #
   gRaspberryPiTokenSpaceGuid.PcdEnableGpio|L"EnableGpio"|gConfigDxeFormSetGuid|0x0|0
 
+  # Utilize RTC on I2C1
+  #
+  # 0  - No
+  # 1  - Yes
+  #
+  gRaspberryPiTokenSpaceGuid.PcdHwRtc|L"HwRtc"|gConfigDxeFormSetGuid|0x0|0
+
+
   #
   # Common UEFI ones.
   #
@@ -659,6 +673,15 @@
     <LibraryClasses>
       RealTimeClockLib|EmbeddedPkg/Library/VirtualRealTimeClockLib/VirtualRealTimeClockLib.inf
   }
+  # Oh, confusing, we now have two runtime variable support drivers...
+  # Do the virtual one by default, but if the hardware RTC is configured then
+  # SOM the BcmI2CPlatform driver, which activates this one.
+  EmbeddedPkg/RealTimeClockRuntimeDxe/RealTimeClockRuntimeDxe.inf {
+    <Defines>
+       FILE_GUID = 9d539f19-5fb6-4088-a81d-aaeb90446751
+    <LibraryClasses>
+     RealTimeClockLib|Silicon/Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.inf
+  }
   EmbeddedPkg/MetronomeDxe/MetronomeDxe.inf
 
   MdeModulePkg/Universal/Console/ConPlatformDxe/ConPlatformDxe.inf
@@ -782,6 +805,13 @@
   MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf
 
   #
+  # RTC support
+  #
+  Platform/RaspberryPi/Drivers/BcmI2CPlatform/BcmI2CPlatform.inf
+  Silicon/Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.inf
+  Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.inf
+
+
   # UEFI application (Shell Embedded Boot Loader)
   #
   ShellPkg/Application/Shell/Shell.inf {
diff --git a/Platform/RaspberryPi/RPi4/RPi4.fdf b/Platform/RaspberryPi/RPi4/RPi4.fdf
index 8169277615..989d99a49f 100644
--- a/Platform/RaspberryPi/RPi4/RPi4.fdf
+++ b/Platform/RaspberryPi/RPi4/RPi4.fdf
@@ -288,6 +288,17 @@ READ_LOCK_STATUS   = TRUE
   #
   INF MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf
 
+  #
+  # I2C/RTC
+  #
+  INF Platform/RaspberryPi/Drivers/BcmI2CPlatform/BcmI2CPlatform.inf
+  INF Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.inf
+  INF RuleOverride = RTC_RUNTIME EmbeddedPkg/RealTimeClockRuntimeDxe/RealTimeClockRuntimeDxe.inf
+#  FILE DRIVER = 9d539f19-5fb6-4088-a81d-aaeb90446751 {
+#    SECTION  PE32 = EmbeddedPkg/RealTimeClockRuntimeDxe/RealTimeClockRuntimeDxe.inf
+#    SECTION  PE32 =  $(INF_OUTPUT)Build/RPi4/DEBUG_GCC5/AARCH64/RealTimeClock_9d539f19-5fb6-4088-a81d-aaeb90446751.efi
+#  }
+
   #
   # SCSI Bus and Disk Driver
   #
@@ -415,6 +426,14 @@ READ_LOCK_STATUS   = TRUE
     UI           STRING="$(MODULE_NAME)" Optional
   }
 
+[Rule.Common.DXE_RUNTIME_DRIVER.RTC_RUNTIME]
+  FILE DRIVER = 9d539f19-5fb6-4088-a81d-aaeb90446751 {
+    DXE_DEPEX    DXE_DEPEX              Optional $(INF_OUTPUT)/../../9d539f19-5fb6-4088-a81d-aaeb90446751/OUTPUT/$(MODULE_NAME).depex
+    PE32         PE32                   $(INF_OUTPUT)/../../9d539f19-5fb6-4088-a81d-aaeb90446751/OUTPUT/$(MODULE_NAME).efi
+    UI           STRING="$(MODULE_NAME)" Optional
+  }
+
+
 [Rule.Common.UEFI_APPLICATION]
   FILE APPLICATION = $(NAMED_GUID) {
     UI     STRING ="$(MODULE_NAME)"     Optional
diff --git a/Platform/RaspberryPi/RaspberryPi.dec b/Platform/RaspberryPi/RaspberryPi.dec
index cc56b436cf..9cf33c366b 100644
--- a/Platform/RaspberryPi/RaspberryPi.dec
+++ b/Platform/RaspberryPi/RaspberryPi.dec
@@ -26,6 +26,7 @@
   gRaspberryPiTokenSpaceGuid = {0xCD7CC258, 0x31DB, 0x11E6, {0x9F, 0xD3, 0x63, 0xB0, 0xB8, 0xEE, 0xD6, 0xB5}}
   gRaspberryPiEventResetGuid = {0xCD7CC258, 0x31DB, 0x11E6, {0x9F, 0xD3, 0x63, 0xB4, 0xB4, 0xE4, 0xD4, 0xB4}}
   gConfigDxeFormSetGuid = {0xCD7CC258, 0x31DB, 0x22E6, {0x9F, 0x22, 0x63, 0xB0, 0xB8, 0xEE, 0xD6, 0xB5}}
+  gBcmNonDiscoverableI2cMasterGuid = { 0x16066c21, 0x0ca6, 0x4f44, {0x82, 0xd9, 0xbe, 0x06, 0x96, 0x12, 0x0e, 0xfb}}
 
 [PcdsFixedAtBuild.common]
   #
@@ -75,3 +76,4 @@
   gRaspberryPiTokenSpaceGuid.PcdMiniUartClockRate|0|UINT32|0x00000023
   gRaspberryPiTokenSpaceGuid.PcdXhciReload|0|UINT32|0x00000024
   gRaspberryPiTokenSpaceGuid.PcdEnableGpio|0|UINT32|0x00000025
+  gRaspberryPiTokenSpaceGuid.PcdHwRtc|0|UINT32|0x00000026
-- 
2.43.0



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



      parent reply	other threads:[~2024-01-11  0:04 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-11  0:04 [edk2-devel] [RFC 0/6] RasberryPi: RTC HAT support Jeremy Linton
2024-01-11  0:04 ` [edk2-devel] [RFC 1/6] Silicon/Bcm283x: Document the I2C registers Jeremy Linton
2024-01-11  0:04 ` [edk2-devel] [RFC 2/6] Silicon/Bcm283x: Add core I2C drivers Jeremy Linton
2024-01-11  0:04 ` [edk2-devel] [RFC 3/6] Platform/RasberryPi: Create I2C driver bound to RTC Jeremy Linton
2024-01-11  0:04 ` [edk2-devel] [RFC 4/6] Silicon/Maxim: Fix runtime issues Jeremy Linton
2024-01-11  0:04 ` [edk2-devel] [RFC 5/6] Platform/RasberryPi: Add I2C1 to uefi runtime memory map Jeremy Linton
2024-01-11  0:04 ` Jeremy Linton [this message]

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=20240111000426.2735007-7-jeremy.linton@arm.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