public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-devel] [RFC 0/6] RasberryPi: RTC HAT support
@ 2024-01-11  0:04 Jeremy Linton
  2024-01-11  0:04 ` [edk2-devel] [RFC 1/6] Silicon/Bcm283x: Document the I2C registers Jeremy Linton
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Jeremy Linton @ 2024-01-11  0:04 UTC (permalink / raw)
  To: devel; +Cc: ardb+tianocore, quic_llindhol, Jeremy Linton

There are a number of Dallas 1307 based RTC HATs available for the RPi
that don't follow the HAT ID protocol but are simple, inexpensive and
widely available. Lets add an I2C driver and then allow the user to
enable them in the advanced menu. Once enabled and detected we then
dynamically swap them in to provide UEFI runtime RTC.

Its this latter bit of logic which seems to be unique to this platform
and probably should be closely reviewed.

Jeremy Linton (6):
  Silicon/Bcm283x: Document the I2C registers
  Silicon/Bcm283x: Add core I2C drivers
  Platform/RasberryPi: Create I2C driver bound to RTC
  Silicon/Maxim: Fix runtime issues
  Platform/RasberryPi: Add I2C1 to uefi runtime memory map
  Platform/RaspberryPi: Add menu and build options for HW RTC

 .../Drivers/BcmI2CPlatform/BcmI2CPlatform.c   | 127 +++++++
 .../Drivers/BcmI2CPlatform/BcmI2CPlatform.inf |  54 +++
 .../RaspberryPi/Drivers/ConfigDxe/ConfigDxe.c |  30 ++
 .../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 +
 .../Include/IndustryStandard/Bcm2836.h        |  34 ++
 .../Broadcom/Drivers/I2cDxe/ComponentName.c   | 181 ++++++++++
 .../Broadcom/Drivers/I2cDxe/DriverBinding.c   | 237 ++++++++++++++
 Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.c      | 309 ++++++++++++++++++
 Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.h      |  55 ++++
 Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.inf    |  56 ++++
 .../Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.c |  36 +-
 .../Library/Ds1307RtcLib/Ds1307RtcLib.inf     |   7 +-
 19 files changed, 1208 insertions(+), 3 deletions(-)
 create mode 100644 Platform/RaspberryPi/Drivers/BcmI2CPlatform/BcmI2CPlatform.c
 create mode 100644 Platform/RaspberryPi/Drivers/BcmI2CPlatform/BcmI2CPlatform.inf
 create mode 100644 Silicon/Broadcom/Drivers/I2cDxe/ComponentName.c
 create mode 100644 Silicon/Broadcom/Drivers/I2cDxe/DriverBinding.c
 create mode 100644 Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.c
 create mode 100644 Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.h
 create mode 100644 Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.inf

-- 
2.43.0



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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [edk2-devel] [RFC 1/6] Silicon/Bcm283x: Document the I2C registers
  2024-01-11  0:04 [edk2-devel] [RFC 0/6] RasberryPi: RTC HAT support Jeremy Linton
@ 2024-01-11  0:04 ` Jeremy Linton
  2024-01-11  0:04 ` [edk2-devel] [RFC 2/6] Silicon/Bcm283x: Add core I2C drivers Jeremy Linton
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Jeremy Linton @ 2024-01-11  0:04 UTC (permalink / raw)
  To: devel; +Cc: ardb+tianocore, quic_llindhol, Jeremy Linton

Since we are now using I2C in edk2, its helpful if we document
the register names.

Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
---
 .../Include/IndustryStandard/Bcm2836.h        | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836.h b/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836.h
index 55a446a86c..6dc8921346 100644
--- a/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836.h
+++ b/Silicon/Broadcom/Bcm283x/Include/IndustryStandard/Bcm2836.h
@@ -97,6 +97,40 @@
 #define BCM2836_I2C2_BASE_ADDRESS                           (BCM2836_SOC_REGISTERS + BCM2836_I2C2_OFFSET)
 #define BCM2836_I2C2_LENGTH                                 0x00000020
 
+#define BCM2836_I2C3_OFFSET                                 0x00205600
+#define BCM2836_I2C3_BASE_ADDRESS                           (BCM2836_SOC_REGISTERS + BCM2836_I2C3_OFFSET)
+#define BCM2836_I2C3_LENGTH                                 0x00000020
+
+#define BCM2836_I2C4_OFFSET                                 0x00205800
+#define BCM2836_I2C4_BASE_ADDRESS                           (BCM2836_SOC_REGISTERS + BCM2836_I2C4_OFFSET)
+#define BCM2836_I2C4_LENGTH                                 0x00000020
+
+#define BCM2836_I2C5_OFFSET                                 0x00205a00 //2711 doc says 205a80?!
+#define BCM2836_I2C5_BASE_ADDRESS                           (BCM2836_SOC_REGISTERS + BCM2836_I2C5_OFFSET)
+#define BCM2836_I2C5_LENGTH                                 0x00000020
+
+#define BCM2836_I2C6_OFFSET                                 0x00205c00
+#define BCM2836_I2C6_BASE_ADDRESS                           (BCM2836_SOC_REGISTERS + BCM2836_I2C6_OFFSET)
+#define BCM2836_I2C6_LENGTH                                 0x00000020
+
+#define BCM2836_I2C20_OFFSET                                0x00f04500 //2711 DCC0
+#define BCM2836_I2C20_BASE_ADDRESS                          (BCM2836_SOC_REGISTERS + BCM2836_I2C20_OFFSET)
+#define BCM2836_I2C20_LENGTH                                0x00000020
+
+#define BCM2836_I2C21_OFFSET                                0x00f09500 //2711 DCC1
+#define BCM2836_I2C21_BASE_ADDRESS                          (BCM2836_SOC_REGISTERS + BCM2836_I2C21_OFFSET)
+#define BCM2836_I2C21_LENGTH                                0x00000020
+
+/* I2C register offsets */
+#define BCM2835_I2C_C                                      0x00
+#define BCM2835_I2C_S                                      0x04
+#define BCM2835_I2C_DLEN                                   0x08
+#define BCM2835_I2C_A                                      0x0c
+#define BCM2835_I2C_FIFO                                   0x10
+#define BCM2835_I2C_DIV                                    0x14
+#define BCM2835_I2C_DEL                                    0x18
+#define BCM2835_I2C_CLKT                                   0x1c
+
 #define BCM2836_SPI0_OFFSET                                 0x00204000
 #define BCM2836_SPI0_BASE_ADDRESS                           (BCM2836_SOC_REGISTERS + BCM2836_SPI0_OFFSET)
 #define BCM2836_SPI0_LENGTH                                 0x00000020
-- 
2.43.0



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



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [edk2-devel] [RFC 2/6] Silicon/Bcm283x: Add core I2C drivers
  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 ` Jeremy Linton
  2024-01-11  0:04 ` [edk2-devel] [RFC 3/6] Platform/RasberryPi: Create I2C driver bound to RTC Jeremy Linton
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Jeremy Linton @ 2024-01-11  0:04 UTC (permalink / raw)
  To: devel; +Cc: ardb+tianocore, quic_llindhol, Jeremy Linton

There are a number of I2C interfaces on the rpi, some of which are
available on the GPIO connector and are utilized by various HATs. In
particular we are interested in the RTCs (usually based on DS1037)
which are attached to GPIO2/3 which can be pin muxed to I2C #1.

This commit adds a basic runtime utilizable I2C Dxe in preparation
for a platform driver which can bind it to the ds1307 RTC driver.

Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
---
 .../Broadcom/Drivers/I2cDxe/ComponentName.c   | 181 ++++++++++
 .../Broadcom/Drivers/I2cDxe/DriverBinding.c   | 237 ++++++++++++++
 Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.c      | 309 ++++++++++++++++++
 Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.h      |  55 ++++
 Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.inf    |  56 ++++
 5 files changed, 838 insertions(+)
 create mode 100644 Silicon/Broadcom/Drivers/I2cDxe/ComponentName.c
 create mode 100644 Silicon/Broadcom/Drivers/I2cDxe/DriverBinding.c
 create mode 100644 Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.c
 create mode 100644 Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.h
 create mode 100644 Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.inf

diff --git a/Silicon/Broadcom/Drivers/I2cDxe/ComponentName.c b/Silicon/Broadcom/Drivers/I2cDxe/ComponentName.c
new file mode 100644
index 0000000000..75b4fdb3fe
--- /dev/null
+++ b/Silicon/Broadcom/Drivers/I2cDxe/ComponentName.c
@@ -0,0 +1,181 @@
+/** @file
+
+  Copyright 2018-2019 NXP
+  Sourced and reworked from edk2/NXP I2C stack
+  Copyright 2022 Arm, Jeremy Linton
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include "I2cDxe.h"
+
+STATIC EFI_UNICODE_STRING_TABLE mBcmI2cDriverNameTable[] = {
+  {
+    "en",
+    (CHAR16 *)L"Bcm I2C Driver"
+  },
+  { }
+};
+
+STATIC EFI_UNICODE_STRING_TABLE mBcmI2cControllerNameTable[] = {
+  {
+    "en",
+    (CHAR16 *)L"Bcm I2C Controller"
+  },
+  { }
+};
+
+/**
+  Retrieves a Unicode string that is the user readable name of the driver.
+
+  This function retrieves the user readable name of a driver in the form of a
+  Unicode string. If the driver specified by This has a user readable name in
+  the language specified by Language, then a pointer to the driver name is
+  returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
+  by This does not support the language specified by Language,
+  then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
+                                EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  Language[in]          A pointer to a Null-terminated ASCII string
+                                array indicating the language. This is the
+                                language of the driver name that the caller is
+                                requesting, and it must match one of the
+                                languages specified in SupportedLanguages. The
+                                number of languages supported by a driver is up
+                                to the driver writer. Language is specified
+                                in RFC 4646 or ISO 639-2 language code format.
+
+  @param  DriverName[out]       A pointer to the Unicode string to return.
+                                This Unicode string is the name of the
+                                driver specified by This in the language
+                                specified by Language.
+
+  @retval EFI_SUCCESS           The Unicode string for the Driver specified by
+                                This and the language specified by Language was
+                                returned in DriverName.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER DriverName is NULL.
+
+  @retval EFI_UNSUPPORTED       The driver specified by This does not support
+                                the language specified by Language.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+BcmI2cGetDriverName (
+  IN  EFI_COMPONENT_NAME2_PROTOCOL  *This,
+  IN  CHAR8                         *Language,
+  OUT CHAR16                        **DriverName
+  )
+{
+  return LookupUnicodeString2 (Language,
+                               This->SupportedLanguages,
+                               mBcmI2cDriverNameTable,
+                               DriverName,
+                               FALSE);
+}
+
+/**
+  Retrieves a Unicode string that is the user readable name of the controller
+  that is being managed by a driver.
+
+  This function retrieves the user readable name of the controller specified by
+  ControllerHandle and ChildHandle in the form of a Unicode string. If the
+  driver specified by This has a user readable name in the language specified by
+  Language, then a pointer to the controller name is returned in ControllerName,
+  and EFI_SUCCESS is returned.  If the driver specified by This is not currently
+  managing the controller specified by ControllerHandle and ChildHandle,
+  then EFI_UNSUPPORTED is returned.  If the driver specified by This does not
+  support the language specified by Language, then EFI_UNSUPPORTED is returned.
+
+  @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
+                                EFI_COMPONENT_NAME_PROTOCOL instance.
+
+  @param  ControllerHandle[in]  The handle of a controller that the driver
+                                specified by This is managing.  This handle
+                                specifies the controller whose name is to be
+                                returned.
+
+  @param  ChildHandle[in]       The handle of the child controller to retrieve
+                                the name of.  This is an optional parameter that
+                                may be NULL.  It will be NULL for device
+                                drivers.  It will also be NULL for a bus drivers
+                                that wish to retrieve the name of the bus
+                                controller.  It will not be NULL for a bus
+                                driver that wishes to retrieve the name of a
+                                child controller.
+
+  @param  Language[in]          A pointer to a Null-terminated ASCII string
+                                array indicating the language.  This is the
+                                language of the driver name that the caller is
+                                requesting, and it must match one of the
+                                languages specified in SupportedLanguages. The
+                                number of languages supported by a driver is up
+                                to the driver writer. Language is specified in
+                                RFC 4646 or ISO 639-2 language code format.
+
+  @param  ControllerName[out]   A pointer to the Unicode string to return.
+                                This Unicode string is the name of the
+                                controller specified by ControllerHandle and
+                                ChildHandle in the language specified by
+                                Language from the point of view of the driver
+                                specified by This.
+
+  @retval EFI_SUCCESS           The Unicode string for the user readable name in
+                                the language specified by Language for the
+                                driver specified by This was returned in
+                                DriverName.
+
+  @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
+
+  @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
+                                EFI_HANDLE.
+
+  @retval EFI_INVALID_PARAMETER Language is NULL.
+
+  @retval EFI_INVALID_PARAMETER ControllerName is NULL.
+
+  @retval EFI_UNSUPPORTED       The driver specified by This is not currently
+                                managing the controller specified by
+                                ControllerHandle and ChildHandle.
+
+  @retval EFI_UNSUPPORTED       The driver specified by This does not support
+                                the language specified by Language.
+
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+BcmI2cGetControllerName (
+  IN  EFI_COMPONENT_NAME2_PROTOCOL                    *This,
+  IN  EFI_HANDLE                                      ControllerHandle,
+  IN  EFI_HANDLE                                      ChildHandle        OPTIONAL,
+  IN  CHAR8                                           *Language,
+  OUT CHAR16                                          **ControllerName
+  )
+{
+  if (ChildHandle != NULL) {
+    return EFI_UNSUPPORTED;
+  }
+
+  return LookupUnicodeString2 (Language,
+                               This->SupportedLanguages,
+                               mBcmI2cControllerNameTable,
+                               ControllerName,
+                               FALSE);
+}
+
+//
+// EFI Component Name 2 Protocol
+//
+EFI_COMPONENT_NAME2_PROTOCOL gBcmI2cDriverComponentName2 = {
+  BcmI2cGetDriverName,
+  BcmI2cGetControllerName,
+  "en"
+};
diff --git a/Silicon/Broadcom/Drivers/I2cDxe/DriverBinding.c b/Silicon/Broadcom/Drivers/I2cDxe/DriverBinding.c
new file mode 100644
index 0000000000..a6cd7987eb
--- /dev/null
+++ b/Silicon/Broadcom/Drivers/I2cDxe/DriverBinding.c
@@ -0,0 +1,237 @@
+/** @file
+
+  Copyright 2018-2019 NXP
+  Sourced and reworked from edk2/NXP I2C stack
+  Copyright 2022 Arm, Jeremy Linton
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+
+#include <Protocol/DriverBinding.h>
+
+#include "I2cDxe.h"
+
+/**
+  Tests to see if this driver supports a given controller.
+
+  @param  This[in]                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL
+                                   instance.
+  @param  ControllerHandle[in]     The handle of the controller to test.
+  @param  RemainingDevicePath[in]  The remaining device path.
+                                   (Ignored - this is not a bus driver.)
+
+  @retval EFI_SUCCESS              The driver supports this controller.
+  @retval EFI_ALREADY_STARTED      The device specified by ControllerHandle is
+                                   already being managed by the driver specified
+                                   by This.
+  @retval EFI_UNSUPPORTED          The device specified by ControllerHandle is
+                                   not supported by the driver specified by This.
+
+**/
+EFI_STATUS
+EFIAPI
+BcmI2cDriverBindingSupported (
+  IN EFI_DRIVER_BINDING_PROTOCOL  *This,
+  IN EFI_HANDLE                   ControllerHandle,
+  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
+  )
+{
+  NON_DISCOVERABLE_DEVICE    *Dev;
+  EFI_STATUS                 Status;
+
+  //
+  //  Connect to the non-discoverable device
+  //
+  Status = gBS->OpenProtocol (ControllerHandle,
+                              &gEdkiiNonDiscoverableDeviceProtocolGuid,
+                              (VOID **) &Dev,
+                              This->DriverBindingHandle,
+                              ControllerHandle,
+                              EFI_OPEN_PROTOCOL_BY_DRIVER);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  if (CompareGuid (Dev->Type, &gBcmNonDiscoverableI2cMasterGuid)) {
+    Status = EFI_SUCCESS;
+  } else {
+    Status = EFI_UNSUPPORTED;
+  }
+
+  //
+  // Clean up.
+  //
+  gBS->CloseProtocol (ControllerHandle,
+                      &gEdkiiNonDiscoverableDeviceProtocolGuid,
+                      This->DriverBindingHandle,
+                      ControllerHandle);
+
+  return Status;
+}
+
+
+/**
+  Starts a device controller or a bus controller.
+
+  @param[in]  This                 A pointer to the EFI_DRIVER_BINDING_PROTOCOL
+                                   instance.
+  @param[in]  ControllerHandle     The handle of the device to start. This
+                                   handle must support a protocol interface that
+                                   supplies an I/O abstraction to the driver.
+  @param[in]  RemainingDevicePath  The remaining portion of the device path.
+                                   (Ignored - this is not a bus driver.)
+
+  @retval EFI_SUCCESS              The device was started.
+  @retval EFI_DEVICE_ERROR         The device could not be started due to a
+                                   device error.
+  @retval EFI_OUT_OF_RESOURCES     The request could not be completed due to a
+                                   lack of resources.
+
+**/
+EFI_STATUS
+EFIAPI
+BcmI2cDriverBindingStart (
+  IN EFI_DRIVER_BINDING_PROTOCOL  *This,
+  IN EFI_HANDLE                   ControllerHandle,
+  IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath OPTIONAL
+  )
+{
+  return BcmI2cInit (This->DriverBindingHandle, ControllerHandle);
+}
+
+
+/**
+  Stops a device controller or a bus controller.
+
+  @param[in]  This              A pointer to the EFI_DRIVER_BINDING_PROTOCOL
+                                instance.
+  @param[in]  ControllerHandle  A handle to the device being stopped. The handle
+                                must support a bus specific I/O protocol for the
+                                driver to use to stop the device.
+  @param[in]  NumberOfChildren  The number of child device handles in
+                                ChildHandleBuffer.
+  @param[in]  ChildHandleBuffer An array of child handles to be freed. May be
+                                NULL if NumberOfChildren is 0.
+
+  @retval EFI_SUCCESS           The device was stopped.
+  @retval EFI_DEVICE_ERROR      The device could not be stopped due to a device
+                                error.
+
+**/
+EFI_STATUS
+EFIAPI
+BcmI2cDriverBindingStop (
+  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,
+  IN  EFI_HANDLE                  ControllerHandle,
+  IN  UINTN                       NumberOfChildren,
+  IN  EFI_HANDLE                  *ChildHandleBuffer OPTIONAL
+  )
+{
+  return BcmI2cRelease (This->DriverBindingHandle, ControllerHandle);
+}
+
+
+STATIC EFI_DRIVER_BINDING_PROTOCOL  gBcmI2cDriverBinding = {
+  BcmI2cDriverBindingSupported,
+  BcmI2cDriverBindingStart,
+  BcmI2cDriverBindingStop,
+  0xa,
+  NULL,
+  NULL
+};
+
+
+/**
+  The entry point of I2c UEFI Driver.
+
+  @param  ImageHandle                The image handle of the UEFI Driver.
+  @param  SystemTable                A pointer to the EFI System Table.
+
+  @retval  EFI_SUCCESS               The Driver or UEFI Driver exited normally.
+  @retval  EFI_INCOMPATIBLE_VERSION  _gUefiDriverRevision is greater than
+                                     SystemTable->Hdr.Revision.
+
+**/
+EFI_STATUS
+EFIAPI
+I2cDxeEntryPoint (
+  IN  EFI_HANDLE          ImageHandle,
+  IN  EFI_SYSTEM_TABLE    *SystemTable
+  )
+{
+  EFI_STATUS    Status;
+
+  //
+  //  Add the driver to the list of drivers
+  //
+  Status = EfiLibInstallDriverBindingComponentName2 (
+             ImageHandle, SystemTable, &gBcmI2cDriverBinding, ImageHandle,
+             NULL, &gBcmI2cDriverComponentName2);
+  ASSERT_EFI_ERROR (Status);
+
+  return EFI_SUCCESS;
+}
+
+
+/**
+  Unload function for the I2c UEFI Driver.
+
+  @param  ImageHandle[in]        The allocated handle for the EFI image
+
+  @retval EFI_SUCCESS            The driver was unloaded successfully
+  @retval EFI_INVALID_PARAMETER  ImageHandle is not a valid image handle.
+
+**/
+EFI_STATUS
+EFIAPI
+I2cDxeUnload (
+  IN EFI_HANDLE  ImageHandle
+  )
+{
+  EFI_STATUS  Status;
+  EFI_HANDLE  *HandleBuffer;
+  UINTN       HandleCount;
+  UINTN       Index;
+
+  //
+  // Retrieve all USB I/O handles in the handle database
+  //
+  Status = gBS->LocateHandleBuffer (ByProtocol,
+                                    &gEdkiiNonDiscoverableDeviceProtocolGuid,
+                                    NULL,
+                                    &HandleCount,
+                                    &HandleBuffer);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  //
+  // Disconnect the driver from the handles in the handle database
+  //
+  for (Index = 0; Index < HandleCount; Index++) {
+    Status = gBS->DisconnectController (HandleBuffer[Index],
+                                        gImageHandle,
+                                        NULL);
+  }
+
+  //
+  // Free the handle array
+  //
+  gBS->FreePool (HandleBuffer);
+
+  //
+  // Uninstall protocols installed by the driver in its entrypoint
+  //
+  Status = gBS->UninstallMultipleProtocolInterfaces (ImageHandle,
+                  &gEfiDriverBindingProtocolGuid,
+                  &gBcmI2cDriverBinding,
+                  NULL
+                  );
+
+  return EFI_SUCCESS;
+}
diff --git a/Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.c b/Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.c
new file mode 100644
index 0000000000..8aedaa1f58
--- /dev/null
+++ b/Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.c
@@ -0,0 +1,309 @@
+/** I2cDxe.c
+  I2c driver APIs for read, write, initialize, set speed and reset
+
+  Sourced and reworked from edk2/NXP I2C stack
+  Copyright 2022 Arm, Jeremy Linton
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/DevicePathLib.h>
+#include <Library/IoLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/TimerLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiLib.h>
+#include <Library/UefiRuntimeLib.h>
+
+#include <IndustryStandard/Bcm2836.h>
+
+#include "I2cDxe.h"
+
+STATIC CONST EFI_I2C_CONTROLLER_CAPABILITIES mI2cControllerCapabilities = {
+  0,
+  0,
+  0,
+  0
+};
+
+/**
+  Function to set i2c bus frequency
+
+  @param   This            Pointer to I2c master protocol
+  @param   BusClockHertz   value to be set
+
+  @retval EFI_SUCCESS      Operation successfull
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+SetBusFrequency (
+  IN CONST EFI_I2C_MASTER_PROTOCOL   *This,
+  IN OUT UINTN                       *BusClockHertz
+ )
+{
+  UINTN                    I2cBase;
+  UINT64                   I2cClock;
+  BCM_I2C_MASTER           *I2c;
+
+  I2c = BCM_I2C_FROM_THIS (This);
+
+  I2cBase = I2c->ControllerBase;
+
+  // depend on ConfigDxe? On the NXP this only sets the clock and resets the bus
+  // Here we are hardcoding the I2C clock until we have a need not to.
+  I2cClock = 50000;
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Function to reset I2c Controller
+
+  @param  This             Pointer to I2c master protocol
+
+  @return EFI_SUCCESS      Operation successfull
+**/
+STATIC
+EFI_STATUS
+EFIAPI
+Reset (
+  IN CONST EFI_I2C_MASTER_PROTOCOL *This
+  )
+{
+  return EFI_SUCCESS;
+}
+
+STATIC
+void
+StatusPoll(
+  UINTN              I2cBase,
+  UINTN              Mask
+    )
+{
+  UINTN              Retry;
+
+  Retry = 0;
+  while ((MmioRead32 (I2cBase + BCM2835_I2C_S) & Mask) != Mask) {
+    Retry++;
+  }
+}
+
+
+
+STATIC
+EFI_STATUS
+SingleTransfer(
+  UINTN              I2cBase,
+  UINTN              SlaveAddress,
+  EFI_I2C_OPERATION  *Operation
+)
+{
+  EFI_STATUS               Status;
+  UINTN                    Index;
+  UINTN                    FifoState;
+
+  // clear all the status
+  MmioWrite32 (I2cBase + BCM2835_I2C_C, 0x10 ); // fifo clear
+  MmioWrite32 (I2cBase + BCM2835_I2C_S, 0x302);
+
+  // don't support 10 bit addr for now. (see 3.3 in 2711 manual)
+  // the problem with arm is that you never know if there are undocumented
+  // acces restrictions (aka 8 bit reg, but it needs to be read with a 32-bit instr)
+  MmioWrite8 (I2cBase + BCM2835_I2C_A, (UINT8)SlaveAddress);
+  MmioWrite32 (I2cBase + BCM2835_I2C_DLEN, Operation->LengthInBytes );
+
+  if (Operation->Flags & I2C_FLAG_READ) {
+    FifoState = 0x20; // fifo has data
+    MmioWrite32 (I2cBase + BCM2835_I2C_C, 0x8081 ); // Enable, start, fifo clear, read
+  } else {
+    FifoState = 0x10; // fifo can accept data
+    MmioWrite32 (I2cBase + BCM2835_I2C_C, 0x8080 ); // Enable, start, fifo clear, write
+  }
+
+  for (Index = 0; Index < Operation->LengthInBytes; Index++) {
+
+    StatusPoll (I2cBase, FifoState);
+
+    if (Operation->Flags & I2C_FLAG_READ) {
+      Operation->Buffer[Index] = MmioRead8 (I2cBase + BCM2835_I2C_FIFO);
+    } else {
+      MmioWrite8 (I2cBase + BCM2835_I2C_FIFO, Operation->Buffer[Index]);
+    }
+  }
+
+  StatusPoll (I2cBase, 0x02);
+
+  Status = 0;
+  return Status;
+}
+
+volatile UINTN forcewrite;
+
+STATIC
+EFI_STATUS
+EFIAPI
+StartRequest (
+  IN CONST EFI_I2C_MASTER_PROTOCOL *This,
+  IN UINTN                         SlaveAddress,
+  IN EFI_I2C_REQUEST_PACKET        *RequestPacket,
+  IN EFI_EVENT                     Event            OPTIONAL,
+  OUT EFI_STATUS                   *I2cStatus       OPTIONAL
+  )
+{
+  BCM_I2C_MASTER           *I2c;
+  UINTN                    I2cBase;
+  EFI_STATUS               Status;
+  EFI_TPL                  Tpl;
+  BOOLEAN                  AtRuntime;
+  UINTN                    Index;
+
+  AtRuntime = EfiAtRuntime ();
+  if (!AtRuntime) {
+    Tpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
+  }
+
+  I2c = BCM_I2C_FROM_THIS (This);
+
+  I2cBase = I2c->ControllerBase;
+
+  for (Index = 0; Index < RequestPacket->OperationCount; Index++ ) {
+
+    Status = SingleTransfer (I2cBase, SlaveAddress,  &RequestPacket->Operation[Index]);
+
+    if (EFI_ERROR (Status)) {
+      break;
+    }
+  }
+
+  if (!AtRuntime) {
+    gBS->RestoreTPL (Tpl);
+  }
+
+  return Status;
+}
+
+STATIC
+VOID
+EFIAPI
+BcmI2cVirtualAddressChangeEvent (
+  IN EFI_EVENT Event,
+  IN VOID *Context
+  )
+{
+  BCM_I2C_MASTER            *I2c;
+
+  I2c = (BCM_I2C_MASTER *)Context;
+
+  EfiConvertPointer (0x0, (VOID**)&I2c->ControllerBase);
+  EfiConvertPointer (0x0, (VOID**)&I2c);
+}
+
+
+EFI_STATUS
+BcmI2cInit (
+  IN EFI_HANDLE             DriverBindingHandle,
+  IN EFI_HANDLE             ControllerHandle
+  )
+{
+  EFI_STATUS                RetVal;
+  NON_DISCOVERABLE_DEVICE   *Dev;
+  BCM_I2C_MASTER            *I2c;
+  EFI_EVENT                 VirtualAddressChangeEvent;
+
+  RetVal = gBS->OpenProtocol (ControllerHandle,
+                              &gEdkiiNonDiscoverableDeviceProtocolGuid,
+                              (VOID **)&Dev, DriverBindingHandle,
+                              ControllerHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);
+  if (EFI_ERROR (RetVal)) {
+    return RetVal;
+  }
+
+  I2c = AllocateRuntimeZeroPool (sizeof (BCM_I2C_MASTER));
+
+  I2c->Signature                            = BCM_I2C_SIGNATURE;
+  I2c->I2cMaster.SetBusFrequency            = SetBusFrequency;
+  I2c->I2cMaster.Reset                      = Reset;
+  I2c->I2cMaster.StartRequest               = StartRequest;
+  I2c->I2cMaster.I2cControllerCapabilities  = &mI2cControllerCapabilities;
+  I2c->Dev                                  = Dev;
+
+  CopyGuid (&I2c->DevicePath.Vendor.Guid, &gEfiCallerIdGuid);
+  I2c->DevicePath.MmioBase = I2c->Dev->Resources[0].AddrRangeMin;
+  I2c->ControllerBase = I2c->Dev->Resources[0].AddrRangeMin;
+  SetDevicePathNodeLength (&I2c->DevicePath.Vendor,
+    sizeof (I2c->DevicePath) - sizeof (I2c->DevicePath.End));
+  SetDevicePathEndNode (&I2c->DevicePath.End);
+
+  RetVal = gBS->InstallMultipleProtocolInterfaces (&ControllerHandle,
+                  &gEfiI2cMasterProtocolGuid, (VOID**)&I2c->I2cMaster,
+                  &gEfiDevicePathProtocolGuid, &I2c->DevicePath,
+                  NULL);
+
+  if (EFI_ERROR (RetVal)) {
+    FreePool (I2c);
+    gBS->CloseProtocol (ControllerHandle,
+                        &gEdkiiNonDiscoverableDeviceProtocolGuid,
+                        DriverBindingHandle,
+                        ControllerHandle);
+  } else {
+    RetVal = gBS->CreateEventEx (
+                  EVT_NOTIFY_SIGNAL,
+                  TPL_NOTIFY,
+                  BcmI2cVirtualAddressChangeEvent,
+                  (VOID *)I2c,
+                  &gEfiEventVirtualAddressChangeGuid,
+                  &VirtualAddressChangeEvent
+                );
+
+    ASSERT_EFI_ERROR (RetVal);
+
+  }
+
+  return RetVal;
+}
+
+EFI_STATUS
+BcmI2cRelease (
+  IN EFI_HANDLE                 DriverBindingHandle,
+  IN EFI_HANDLE                 ControllerHandle
+  )
+{
+  EFI_I2C_MASTER_PROTOCOL       *I2cMaster;
+  EFI_STATUS                    RetVal;
+  BCM_I2C_MASTER                *I2c;
+
+  RetVal = gBS->HandleProtocol (ControllerHandle,
+                                &gEfiI2cMasterProtocolGuid,
+                                (VOID **)&I2cMaster);
+  ASSERT_EFI_ERROR (RetVal);
+  if (EFI_ERROR (RetVal)) {
+    return RetVal;
+  }
+
+  I2c = BCM_I2C_FROM_THIS (I2cMaster);
+
+  RetVal = gBS->UninstallMultipleProtocolInterfaces (ControllerHandle,
+                  &gEfiI2cMasterProtocolGuid, I2cMaster,
+                  &gEfiDevicePathProtocolGuid, &I2c->DevicePath,
+                  NULL);
+  if (EFI_ERROR (RetVal)) {
+    return RetVal;
+  }
+
+  RetVal = gBS->CloseProtocol (ControllerHandle,
+                               &gEdkiiNonDiscoverableDeviceProtocolGuid,
+                               DriverBindingHandle,
+                               ControllerHandle);
+  ASSERT_EFI_ERROR (RetVal);
+  if (EFI_ERROR (RetVal)) {
+    return RetVal;
+  }
+
+  gBS->FreePool (I2c);
+
+  return EFI_SUCCESS;
+}
diff --git a/Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.h b/Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.h
new file mode 100644
index 0000000000..e9ddc0e7bd
--- /dev/null
+++ b/Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.h
@@ -0,0 +1,55 @@
+/** I2cDxe.h
+  Header defining the constant, base address amd function for I2C controller
+
+  Copyright 2017-2020 NXP
+  Sourced and reworked from edk2/NXP I2C stack
+  Copyright 2022 Arm, Jeremy Linton
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef I2C_DXE_H_
+#define I2C_DXE_H_
+
+#include <Library/UefiLib.h>
+#include <Uefi.h>
+
+#include <Protocol/I2cMaster.h>
+#include <Protocol/NonDiscoverableDevice.h>
+
+#define BCM_I2C_SIGNATURE         SIGNATURE_32 ('B', 'I', '2', 'C')
+#define BCM_I2C_FROM_THIS(a)      CR ((a), BCM_I2C_MASTER, \
+                                    I2cMaster, BCM_I2C_SIGNATURE)
+
+extern EFI_COMPONENT_NAME2_PROTOCOL gBcmI2cDriverComponentName2;
+
+#pragma pack(1)
+typedef struct {
+  VENDOR_DEVICE_PATH              Vendor;
+  UINT64                          MmioBase;
+  EFI_DEVICE_PATH_PROTOCOL        End;
+} BCM_I2C_DEVICE_PATH;
+#pragma pack()
+
+typedef struct {
+  UINT32                          Signature;
+  EFI_I2C_MASTER_PROTOCOL         I2cMaster;
+  BCM_I2C_DEVICE_PATH             DevicePath;
+  NON_DISCOVERABLE_DEVICE         *Dev;
+  UINTN                           ControllerBase;
+} BCM_I2C_MASTER;
+
+EFI_STATUS
+BcmI2cInit (
+  IN EFI_HANDLE  DriverBindingHandle,
+  IN EFI_HANDLE  ControllerHandle
+  );
+
+EFI_STATUS
+BcmI2cRelease (
+  IN EFI_HANDLE  DriverBindingHandle,
+  IN EFI_HANDLE  ControllerHandle
+  );
+
+#endif //I2C_DXE_H_
diff --git a/Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.inf b/Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.inf
new file mode 100644
index 0000000000..d8b6130616
--- /dev/null
+++ b/Silicon/Broadcom/Drivers/I2cDxe/I2cDxe.inf
@@ -0,0 +1,56 @@
+#  @file
+#
+#  Component description file for I2c driver
+#
+#  Copyright (c) 2015, Freescale Semiconductor, Inc. All rights reserved.
+#  Copyright 2017-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                      = I2cDxe
+  FILE_GUID                      = 09e767d9-9c1e-405d-86c3-fb7ce355f948
+  MODULE_TYPE                    = DXE_RUNTIME_DRIVER
+  VERSION_STRING                 = 1.0
+  ENTRY_POINT                    = I2cDxeEntryPoint
+  UNLOAD                         = I2cDxeUnload
+
+[Sources.common]
+  ComponentName.c
+  DriverBinding.c
+  I2cDxe.c
+
+[LibraryClasses]
+  ArmLib
+  BaseMemoryLib
+  DevicePathLib
+  IoLib
+  MemoryAllocationLib
+  PcdLib
+  TimerLib
+  UefiBootServicesTableLib
+  UefiDriverEntryPoint
+  UefiLib
+  UefiRuntimeLib
+
+[Guids]
+  gBcmNonDiscoverableI2cMasterGuid
+  gEfiEventVirtualAddressChangeGuid
+
+[Packages]
+  MdeModulePkg/MdeModulePkg.dec
+  MdePkg/MdePkg.dec
+  Platform/RaspberryPi/RaspberryPi.dec
+  Silicon/Broadcom/Bcm283x/Bcm283x.dec
+
+[Protocols]
+  gEdkiiNonDiscoverableDeviceProtocolGuid    ## TO_START
+  gEfiI2cMasterProtocolGuid                  ## BY_START
+
+[Depex]
+  TRUE
-- 
2.43.0



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



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [edk2-devel] [RFC 3/6] Platform/RasberryPi: Create I2C driver bound to RTC
  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
  2024-01-11  0:04 ` [edk2-devel] [RFC 4/6] Silicon/Maxim: Fix runtime issues Jeremy Linton
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Jeremy Linton @ 2024-01-11  0:04 UTC (permalink / raw)
  To: devel; +Cc: ardb+tianocore, quic_llindhol, Jeremy Linton

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]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [edk2-devel] [RFC 4/6] Silicon/Maxim: Fix runtime issues
  2024-01-11  0:04 [edk2-devel] [RFC 0/6] RasberryPi: RTC HAT support Jeremy Linton
                   ` (2 preceding siblings ...)
  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 ` 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
  5 siblings, 0 replies; 7+ messages in thread
From: Jeremy Linton @ 2024-01-11  0:04 UTC (permalink / raw)
  To: devel; +Cc: ardb+tianocore, quic_llindhol, Jeremy Linton

The Ds1307 is ideally a runtime RTC but its use of the I2C
protocol might make it better if it updated the I2C_MASTER_PROTOCOL
callbacks it uses, although... hmmm maybe think about this
a bit more.

Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
---
 .../Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.c | 36 +++++++++++++++++--
 .../Library/Ds1307RtcLib/Ds1307RtcLib.inf     |  7 +++-
 2 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/Silicon/Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.c b/Silicon/Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.c
index 444e011248..ede890448f 100644
--- a/Silicon/Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.c
+++ b/Silicon/Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.c
@@ -18,14 +18,40 @@
 #include <Library/RealTimeClockLib.h>
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/UefiLib.h>
+#include <Library/UefiRuntimeLib.h>
 #include <Protocol/I2cMaster.h>
 
 #include "Ds1307Rtc.h"
 
 STATIC VOID                       *mDriverEventRegistration;
 STATIC EFI_HANDLE                 mI2cMasterHandle;
+STATIC EFI_EVENT                  mRtcVirtualAddrChangeEvent;
 STATIC EFI_I2C_MASTER_PROTOCOL    *mI2cMaster;
 
+
+/**
+  Fixup internal data so that EFI can be call in virtual mode.
+  Call the passed in Child Notify event and convert any pointers in
+  lib to virtual mode.
+
+  @param[in]    Event   The Event that is being processed
+  @param[in]    Context Event Context
+**/
+VOID
+EFIAPI
+LibRtcVirtualNotifyEvent (
+  IN EFI_EVENT        Event,
+  IN VOID             *Context
+  )
+{
+  EfiConvertPointer (0x0, (VOID **)&mI2cMaster->SetBusFrequency);
+  EfiConvertPointer (0x0, (VOID **)&mI2cMaster->Reset);
+  EfiConvertPointer (0x0, (VOID **)&mI2cMaster->StartRequest);
+  EfiConvertPointer (0x0, (VOID **)&mI2cMaster->I2cControllerCapabilities);
+  EfiConvertPointer (0x0, (VOID **)&mI2cMaster);
+}
+
+
 /**
   Read RTC register.
   Data Read-Slave Transmitter Mode
@@ -54,7 +80,7 @@ RtcRead (
 
   Req.OperationCount = 2;
 
-  Req.SetAddressOp.Flags = 0;
+  Req.SetAddressOp.Flags = 0; //I2C_FLAG_WRITE
   Req.SetAddressOp.LengthInBytes = sizeof (RtcRegAddr);
   Req.SetAddressOp.Buffer = &RtcRegAddr;
 
@@ -98,7 +124,7 @@ RtcWrite (
   Buffer[0] = RtcRegAddr;
   Buffer[1] = Val;
 
-  Req.SetAddressOp.Flags = 0;
+  Req.SetAddressOp.Flags = 0; //I2C_FLAG_WRITE
   Req.SetAddressOp.LengthInBytes = sizeof (Buffer);
   Req.SetAddressOp.Buffer = Buffer;
 
@@ -375,5 +401,11 @@ LibRtcInitialize (
     NULL,
     &mDriverEventRegistration);
 
+  Status = gBS->CreateEventEx (EVT_NOTIFY_SIGNAL, TPL_NOTIFY,
+                  LibRtcVirtualNotifyEvent, NULL,
+                  &gEfiEventVirtualAddressChangeGuid,
+                  &mRtcVirtualAddrChangeEvent);
+  ASSERT_EFI_ERROR (Status);
+
   return EFI_SUCCESS;
 }
diff --git a/Silicon/Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.inf b/Silicon/Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.inf
index b92f658bfc..9d8cf16d28 100644
--- a/Silicon/Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.inf
+++ b/Silicon/Maxim/Library/Ds1307RtcLib/Ds1307RtcLib.inf
@@ -28,6 +28,9 @@
   UefiBootServicesTableLib
   UefiLib
 
+[Guids]
+  gEfiEventVirtualAddressChangeGuid
+
 [Protocols]
   gEfiI2cMasterProtocolGuid                          ## CONSUMES
   gDs1307RealTimeClockLibI2cMasterProtocolGuid       ## CONSUMES
@@ -36,5 +39,7 @@
   gDs1307RtcLibTokenSpaceGuid.PcdI2cSlaveAddress
   gDs1307RtcLibTokenSpaceGuid.PcdI2cBusFrequency
 
+# 2.15 EDK II INF, .. the generic (i.e depex).. are not permitted
+# yet it ANDs this into the parent's depex
 [Depex]
-  gDs1307RealTimeClockLibI2cMasterProtocolGuid
+  gDs1307RealTimeClockLibI2cMasterProtocolGuid AND gEfiI2cMasterProtocolGuid
-- 
2.43.0



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



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [edk2-devel] [RFC 5/6] Platform/RasberryPi: Add I2C1 to uefi runtime memory map
  2024-01-11  0:04 [edk2-devel] [RFC 0/6] RasberryPi: RTC HAT support Jeremy Linton
                   ` (3 preceding siblings ...)
  2024-01-11  0:04 ` [edk2-devel] [RFC 4/6] Silicon/Maxim: Fix runtime issues Jeremy Linton
@ 2024-01-11  0:04 ` Jeremy Linton
  2024-01-11  0:04 ` [edk2-devel] [RFC 6/6] Platform/RaspberryPi: Add menu and build options for HW RTC Jeremy Linton
  5 siblings, 0 replies; 7+ messages in thread
From: Jeremy Linton @ 2024-01-11  0:04 UTC (permalink / raw)
  To: devel; +Cc: ardb+tianocore, quic_llindhol, Jeremy Linton

We intend to allow the OS to update the RTC after exit boot services.
THis means we need to assure that the I2C IO map is marked correctly.

Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
---
 Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.c b/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.c
index 377ef438ff..fec8f63ea8 100644
--- a/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.c
+++ b/Platform/RaspberryPi/Drivers/ConfigDxe/ConfigDxe.c
@@ -505,6 +505,7 @@ ApplyVariables (
   }
 
   if (mModelFamily == 4) {
+    // add memoryspaces for the runtime flash/variable store
     Status = gDS->AddMemorySpace (EfiGcdMemoryTypeMemoryMappedIo, BCM2836_SPI0_BASE_ADDRESS,
                                   SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
     ASSERT_EFI_ERROR (Status);
@@ -517,6 +518,13 @@ ApplyVariables (
     Status = gDS->SetMemorySpaceAttributes (GPIO_BASE_ADDRESS,
                                             SIZE_4KB, EFI_MEMORY_UC|EFI_MEMORY_RUNTIME);
 
+    // add memoryspace for the runtime rtc/i2c
+    Status = gDS->AddMemorySpace (EfiGcdMemoryTypeMemoryMappedIo, BCM2836_I2C1_BASE_ADDRESS,
+                                  SIZE_4KB, EFI_MEMORY_UC | EFI_MEMORY_RUNTIME);
+    ASSERT_EFI_ERROR (Status);
+    Status = gDS->SetMemorySpaceAttributes (BCM2836_I2C1_BASE_ADDRESS,
+                                            SIZE_4KB, EFI_MEMORY_UC|EFI_MEMORY_RUNTIME);
+
     ASSERT_EFI_ERROR (Status);
   }
 
-- 
2.43.0



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



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [edk2-devel] [RFC 6/6] Platform/RaspberryPi: Add menu and build options for HW RTC
  2024-01-11  0:04 [edk2-devel] [RFC 0/6] RasberryPi: RTC HAT support Jeremy Linton
                   ` (4 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: Jeremy Linton @ 2024-01-11  0:04 UTC (permalink / raw)
  To: devel; +Cc: ardb+tianocore, quic_llindhol, Jeremy Linton

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]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-01-11  0:04 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [edk2-devel] [RFC 6/6] Platform/RaspberryPi: Add menu and build options for HW RTC Jeremy Linton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox