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 3/6] Platform/RasberryPi: Create I2C driver bound to RTC
Date: Wed, 10 Jan 2024 18:04:23 -0600	[thread overview]
Message-ID: <20240111000426.2735007-4-jeremy.linton@arm.com> (raw)
In-Reply-To: <20240111000426.2735007-1-jeremy.linton@arm.com>

Now that we have a generic Bcm I2C driver lets instantiate one
against a possible RTC hat on the pi4.

Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
---
 .../Drivers/BcmI2CPlatform/BcmI2CPlatform.c   | 127 ++++++++++++++++++
 .../Drivers/BcmI2CPlatform/BcmI2CPlatform.inf |  54 ++++++++
 2 files changed, 181 insertions(+)
 create mode 100644 Platform/RaspberryPi/Drivers/BcmI2CPlatform/BcmI2CPlatform.c
 create mode 100644 Platform/RaspberryPi/Drivers/BcmI2CPlatform/BcmI2CPlatform.inf

diff --git a/Platform/RaspberryPi/Drivers/BcmI2CPlatform/BcmI2CPlatform.c b/Platform/RaspberryPi/Drivers/BcmI2CPlatform/BcmI2CPlatform.c
new file mode 100644
index 0000000000..11f927b848
--- /dev/null
+++ b/Platform/RaspberryPi/Drivers/BcmI2CPlatform/BcmI2CPlatform.c
@@ -0,0 +1,127 @@
+/** @file
+  Brcm/Rpi I2C DXE platform driver.
+
+  Copyright 2018-2020 NXP
+  Sourced and reworked from edk2/NXP I2C stack
+  Copyright 2022 Arm, Jeremy Linton
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+  This thing binds a I2C driver to a RTC..
+
+**/
+
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/PcdLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiLib.h>
+#include <IndustryStandard/Bcm2836.h>
+
+#include <Protocol/NonDiscoverableDevice.h>
+
+typedef struct {
+  EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR StartDesc;
+  UINT8 EndDesc;
+} ADDRESS_SPACE_DESCRIPTOR;
+
+#define BCM_I2C_NUM_CONTROLLERS 1 //actually 6 on the bcm2711, hack for now
+
+STATIC ADDRESS_SPACE_DESCRIPTOR mI2cDesc[BCM_I2C_NUM_CONTROLLERS];
+
+STATIC
+EFI_STATUS
+RegisterDevice (
+  IN  EFI_GUID                        *TypeGuid,
+  IN  ADDRESS_SPACE_DESCRIPTOR        *Desc,
+  OUT EFI_HANDLE                      *Handle
+  )
+{
+  NON_DISCOVERABLE_DEVICE             *Device;
+  EFI_STATUS                          Status;
+
+  Device = (NON_DISCOVERABLE_DEVICE *)AllocateZeroPool (sizeof (*Device));
+  if (Device == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  Device->Type = TypeGuid;
+  Device->DmaType = NonDiscoverableDeviceDmaTypeNonCoherent;
+  Device->Resources = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *)Desc;
+
+  Status = gBS->InstallMultipleProtocolInterfaces (Handle,
+                  &gEdkiiNonDiscoverableDeviceProtocolGuid, Device,
+                  NULL);
+  if (EFI_ERROR (Status)) {
+    goto FreeDevice;
+  }
+  return EFI_SUCCESS;
+
+FreeDevice:
+  FreePool (Device);
+
+  return Status;
+}
+
+VOID
+PopulateI2cInformation (
+  IN VOID
+  )
+{
+  UINT32 Index;
+
+  for (Index = 0; Index < ARRAY_SIZE (mI2cDesc); Index++) {
+    mI2cDesc[Index].StartDesc.Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR;
+    mI2cDesc[Index].StartDesc.Len = sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) - 3;
+    mI2cDesc[Index].StartDesc.ResType = ACPI_ADDRESS_SPACE_TYPE_MEM;
+    mI2cDesc[Index].StartDesc.GenFlag = 0;
+    mI2cDesc[Index].StartDesc.SpecificFlag = 0;
+    mI2cDesc[Index].StartDesc.AddrSpaceGranularity = 32;
+    mI2cDesc[Index].StartDesc.AddrRangeMin = BCM2836_I2C1_BASE_ADDRESS;
+    mI2cDesc[Index].StartDesc.AddrRangeMax = mI2cDesc[Index].StartDesc.AddrRangeMin + BCM2836_I2C1_LENGTH;
+    mI2cDesc[Index].StartDesc.AddrTranslationOffset = 0;
+    mI2cDesc[Index].StartDesc.AddrLen = BCM2836_I2C1_LENGTH;
+
+    mI2cDesc[Index].EndDesc = ACPI_END_TAG_DESCRIPTOR;
+  }
+}
+
+EFI_STATUS
+EFIAPI
+BcmI2CPlatformDxeEntryPoint (
+  IN EFI_HANDLE         ImageHandle,
+  IN EFI_SYSTEM_TABLE   *SystemTable
+  )
+{
+  EFI_STATUS                      Status;
+  EFI_HANDLE                      Handle;
+
+  Handle = NULL;
+
+  PopulateI2cInformation ();
+
+  if (PcdGet32 (PcdHwRtc))
+  {
+    DEBUG ((DEBUG_ERROR, "RTC I2C enable\n"));
+    // If we don't register this, the second rtc won't get enabled
+    // leaving the emulator in place.
+    Status = RegisterDevice (&gBcmNonDiscoverableI2cMasterGuid,
+               &mI2cDesc[0], &Handle);
+    ASSERT_EFI_ERROR (Status);
+
+    //
+    // Install the DS1307 I2C Master protocol on this handle so the RTC driver
+    // can identify it as the I2C master it can invoke directly.
+    //
+    Status = gBS->InstallProtocolInterface (&Handle,
+                    &gDs1307RealTimeClockLibI2cMasterProtocolGuid,
+                    EFI_NATIVE_INTERFACE, NULL);
+    ASSERT_EFI_ERROR (Status);
+  }  else  {
+    DEBUG ((DEBUG_ERROR, "RTC I2C disabled\n"));
+  }
+
+  return EFI_SUCCESS;
+}
diff --git a/Platform/RaspberryPi/Drivers/BcmI2CPlatform/BcmI2CPlatform.inf b/Platform/RaspberryPi/Drivers/BcmI2CPlatform/BcmI2CPlatform.inf
new file mode 100644
index 0000000000..aa5c1b51b2
--- /dev/null
+++ b/Platform/RaspberryPi/Drivers/BcmI2CPlatform/BcmI2CPlatform.inf
@@ -0,0 +1,54 @@
+## @file
+#
+#  Component description file for Bcm/Rpi I2C driver.
+#
+#  Copyright 2018-2020 NXP
+#  Sourced and reworked from edk2/NXP I2C stack
+#  Copyright 2022 Arm, Jeremy Linton
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001A
+  BASE_NAME                      = BcmI2CPlatformDxe
+  FILE_GUID                      = 1a23fe23-39bc-4bee-859d-ecb5bbb60484
+  MODULE_TYPE                    = DXE_DRIVER
+  VERSION_STRING                 = 1.0
+  ENTRY_POINT                    = BcmI2CPlatformDxeEntryPoint
+
+[Sources]
+  BcmI2CPlatform.c
+
+[Packages]
+  ArmPkg/ArmPkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  MdePkg/MdePkg.dec
+  Silicon/Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.dec
+  Silicon/Broadcom/Bcm283x/Bcm283x.dec
+  Platform/RaspberryPi/RaspberryPi.dec
+
+[LibraryClasses]
+  BaseLib
+  BaseMemoryLib
+  DebugLib
+  MemoryAllocationLib
+  PcdLib
+  UefiBootServicesTableLib
+  UefiDriverEntryPoint
+  UefiLib
+
+[Guids]
+  gBcmNonDiscoverableI2cMasterGuid
+
+[Protocols]
+  gEdkiiNonDiscoverableDeviceProtocolGuid        ## PRODUCES
+  gDs1307RealTimeClockLibI2cMasterProtocolGuid   ## PRODUCES
+
+[Pcd]
+  gBcm283xTokenSpaceGuid.PcdBcm283xRegistersAddress
+  gRaspberryPiTokenSpaceGuid.PcdHwRtc
+
+[Depex]
+  gRaspberryPiConfigAppliedProtocolGuid
-- 
2.43.0



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113560): https://edk2.groups.io/g/devel/message/113560
Mute This Topic: https://groups.io/mt/103653099/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 ` Jeremy Linton [this message]
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 ` [edk2-devel] [RFC 6/6] Platform/RaspberryPi: Add menu and build options for HW RTC Jeremy Linton

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-4-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