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: ard.biesheuvel@arm.com, leif@nuviainc.com, pete@akeo.ie,
	samer.el-haj-mahmoud@arm.com, awarkentin@vmware.com,
	Jeremy Linton <jeremy.linton@arm.com>
Subject: [PATCH v4 2/7] Platform/RaspberryPi: Add further mailbox helpers
Date: Tue,  5 Jan 2021 10:34:15 -0600	[thread overview]
Message-ID: <20210105163420.1711652-3-jeremy.linton@arm.com> (raw)
In-Reply-To: <20210105163420.1711652-1-jeremy.linton@arm.com>

Lets add some further mailbox helpers and convert the existing
RpiFirmwareSetLed into a generic SetGpio() function.

Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
Reviewed-by: Andrei Warkentin <awarkentin@vmware.com>
---
 .../Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c        | 240 ++++++++++++++++++++-
 .../RaspberryPi/Include/Protocol/RpiFirmware.h     |  25 +++
 2 files changed, 255 insertions(+), 10 deletions(-)

diff --git a/Platform/RaspberryPi/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c b/Platform/RaspberryPi/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c
index ade91c9f34..bf74148bbb 100644
--- a/Platform/RaspberryPi/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c
+++ b/Platform/RaspberryPi/Drivers/RpiFirmwareDxe/RpiFirmwareDxe.c
@@ -1090,7 +1090,6 @@ RpiFirmwareSetClockRate (
   return EFI_SUCCESS;
 }
 
-
 #pragma pack()
 typedef struct {
   UINT32                    ClockId;
@@ -1152,6 +1151,17 @@ RpiFirmwareGetClockRate (
 STATIC
 EFI_STATUS
 EFIAPI
+RpiFirmwareGetCurrentClockState (
+  IN  UINT32    ClockId,
+  OUT UINT32    *ClockState
+  )
+{
+  return RpiFirmwareGetClockRate (ClockId, RPI_MBOX_GET_CLOCK_STATE, ClockState);
+}
+
+STATIC
+EFI_STATUS
+EFIAPI
 RpiFirmwareGetCurrentClockRate (
   IN  UINT32    ClockId,
   OUT UINT32    *ClockRate
@@ -1181,6 +1191,63 @@ RpiFirmwareGetMinClockRate (
 {
   return RpiFirmwareGetClockRate (ClockId, RPI_MBOX_GET_MIN_CLOCK_RATE, ClockRate);
 }
+
+#pragma pack()
+typedef struct {
+  UINT32                    ClockId;
+  UINT32                    ClockState;
+} RPI_FW_GET_CLOCK_STATE_TAG;
+
+typedef struct {
+  RPI_FW_BUFFER_HEAD         BufferHead;
+  RPI_FW_TAG_HEAD            TagHead;
+  RPI_FW_GET_CLOCK_STATE_TAG TagBody;
+  UINT32                     EndTag;
+} RPI_FW_SET_CLOCK_STATE_CMD;
+#pragma pack()
+
+STATIC
+EFI_STATUS
+RpiFirmwareSetClockState (
+  IN  UINT32 ClockId,
+  IN  UINT32 ClockState
+  )
+{
+  RPI_FW_SET_CLOCK_STATE_CMD  *Cmd;
+  EFI_STATUS                  Status;
+  UINT32                      Result;
+
+  if (!AcquireSpinLockOrFail (&mMailboxLock)) {
+    DEBUG ((DEBUG_ERROR, "%a: failed to acquire spinlock\n", __FUNCTION__));
+    return EFI_DEVICE_ERROR;
+  }
+
+  Cmd = mDmaBuffer;
+  ZeroMem (Cmd, sizeof (*Cmd));
+
+  Cmd->BufferHead.BufferSize  = sizeof (*Cmd);
+  Cmd->BufferHead.Response    = 0;
+  Cmd->TagHead.TagId          = RPI_MBOX_SET_CLOCK_STATE;
+  Cmd->TagHead.TagSize        = sizeof (Cmd->TagBody);
+  Cmd->TagHead.TagValueSize   = 0;
+  Cmd->TagBody.ClockId        = ClockId;
+  Cmd->TagBody.ClockState     = ClockState;
+  Cmd->EndTag                 = 0;
+
+  Status = MailboxTransaction (Cmd->BufferHead.BufferSize, RPI_MBOX_VC_CHANNEL, &Result);
+
+  ReleaseSpinLock (&mMailboxLock);
+
+  if (EFI_ERROR (Status) ||
+      Cmd->BufferHead.Response != RPI_MBOX_RESP_SUCCESS) {
+    DEBUG ((DEBUG_ERROR,
+      "%a: mailbox transaction error: Status == %r, Response == 0x%x\n",
+      __FUNCTION__, Status, Cmd->BufferHead.Response));
+    return EFI_DEVICE_ERROR;
+  }
+
+  return EFI_SUCCESS;
+}
 
 #pragma pack()
 typedef struct {
@@ -1199,8 +1266,9 @@ typedef struct {
 STATIC
 VOID
 EFIAPI
-RpiFirmwareSetLed (
-  IN  BOOLEAN On
+RpiFirmwareSetGpio (
+  IN  UINT32  Gpio,
+  IN  BOOLEAN State
   )
 {
   RPI_FW_SET_GPIO_CMD *Cmd;
@@ -1220,14 +1288,10 @@ RpiFirmwareSetLed (
   Cmd->TagHead.TagId          = RPI_MBOX_SET_GPIO;
   Cmd->TagHead.TagSize        = sizeof (Cmd->TagBody);
   /*
-   * GPIO_PIN_2 = Activity LED
-   * GPIO_PIN_4 = HDMI Detect (Input / Active Low)
-   * GPIO_PIN_7 = Power LED (Input / Active Low)
-   *
    * There's also a 128 pin offset.
    */
-  Cmd->TagBody.Pin = 128 + 2;
-  Cmd->TagBody.State = On;
+  Cmd->TagBody.Pin = 128 + Gpio;
+  Cmd->TagBody.State = State;
   Cmd->TagHead.TagValueSize   = 0;
   Cmd->EndTag                 = 0;
 
@@ -1242,6 +1306,16 @@ RpiFirmwareSetLed (
       __FUNCTION__, Status, Cmd->BufferHead.Response));
   }
 }
+
+STATIC
+VOID
+EFIAPI
+RpiFirmwareSetLed (
+  IN  BOOLEAN On
+  )
+{
+  RpiFirmwareSetGpio (RPI_EXP_GPIO_LED, On);
+}
 
 #pragma pack()
 typedef struct {
@@ -1299,6 +1373,149 @@ RpiFirmwareNotifyXhciReset (
   return Status;
 }
 
+#pragma pack()
+typedef struct {
+  UINT32                       Gpio;
+  UINT32                       Direction;
+  UINT32                       Polarity;
+  UINT32                       TermEn;
+  UINT32                       TermPullUp;
+} RPI_FW_GPIO_GET_CFG_TAG;
+
+typedef struct {
+  RPI_FW_BUFFER_HEAD           BufferHead;
+  RPI_FW_TAG_HEAD              TagHead;
+  RPI_FW_GPIO_GET_CFG_TAG      TagBody;
+  UINT32                       EndTag;
+} RPI_FW_NOTIFY_GPIO_GET_CFG_CMD;
+#pragma pack()
+
+
+STATIC
+EFI_STATUS
+EFIAPI
+RpiFirmwareNotifyGpioGetCfg (
+  IN UINTN  Gpio,
+  IN UINT32 *Polarity
+  )
+{
+  RPI_FW_NOTIFY_GPIO_GET_CFG_CMD *Cmd;
+  EFI_STATUS                   Status;
+  UINT32                       Result;
+
+  if (!AcquireSpinLockOrFail (&mMailboxLock)) {
+    DEBUG ((DEBUG_ERROR, "%a: failed to acquire spinlock\n", __FUNCTION__));
+    return EFI_DEVICE_ERROR;
+  }
+
+  Cmd = mDmaBuffer;
+  ZeroMem (Cmd, sizeof (*Cmd));
+
+  Cmd->BufferHead.BufferSize  = sizeof (*Cmd);
+  Cmd->BufferHead.Response    = 0;
+  Cmd->TagHead.TagId          = RPI_MBOX_GET_GPIO_CONFIG;
+  Cmd->TagHead.TagSize        = sizeof (Cmd->TagBody);
+  Cmd->TagBody.Gpio = 128 + Gpio;
+
+  Cmd->TagHead.TagValueSize   = 0;
+  Cmd->EndTag                 = 0;
+
+  Status = MailboxTransaction (Cmd->BufferHead.BufferSize, RPI_MBOX_VC_CHANNEL, &Result);
+
+  *Polarity = Cmd->TagBody.Polarity;
+
+  ReleaseSpinLock (&mMailboxLock);
+
+  if (EFI_ERROR (Status) ||
+      Cmd->BufferHead.Response != RPI_MBOX_RESP_SUCCESS) {
+    DEBUG ((DEBUG_ERROR,
+      "%a: mailbox  transaction error: Status == %r, Response == 0x%x\n",
+      __FUNCTION__, Status, Cmd->BufferHead.Response));
+  }
+
+  return Status;
+}
+
+
+#pragma pack()
+typedef struct {
+  UINT32                       Gpio;
+  UINT32                       Direction;
+  UINT32                       Polarity;
+  UINT32                       TermEn;
+  UINT32                       TermPullUp;
+  UINT32                       State;
+} RPI_FW_GPIO_SET_CFG_TAG;
+
+typedef struct {
+  RPI_FW_BUFFER_HEAD           BufferHead;
+  RPI_FW_TAG_HEAD              TagHead;
+  RPI_FW_GPIO_SET_CFG_TAG      TagBody;
+  UINT32                       EndTag;
+} RPI_FW_NOTIFY_GPIO_SET_CFG_CMD;
+#pragma pack()
+
+
+STATIC
+EFI_STATUS
+EFIAPI
+RpiFirmwareNotifyGpioSetCfg (
+  IN UINTN Gpio,
+  IN UINTN Direction,
+  IN UINTN State
+  )
+{
+  RPI_FW_NOTIFY_GPIO_SET_CFG_CMD *Cmd;
+  EFI_STATUS                   Status;
+  UINT32                       Result;
+
+  Status = RpiFirmwareNotifyGpioGetCfg (Gpio, &Result);
+  if (EFI_ERROR (Status)) {
+	  DEBUG ((DEBUG_ERROR, "%a: Failed to get GPIO polarity\n", __FUNCTION__));
+	  Result = 0; //default polarity
+  }
+
+
+  if (!AcquireSpinLockOrFail (&mMailboxLock)) {
+    DEBUG ((DEBUG_ERROR, "%a: failed to acquire spinlock\n", __FUNCTION__));
+    return EFI_DEVICE_ERROR;
+  }
+
+  Cmd = mDmaBuffer;
+  ZeroMem (Cmd, sizeof (*Cmd));
+
+  Cmd->BufferHead.BufferSize  = sizeof (*Cmd);
+  Cmd->BufferHead.Response    = 0;
+  Cmd->TagHead.TagId          = RPI_MBOX_SET_GPIO_CONFIG;
+  Cmd->TagHead.TagSize        = sizeof (Cmd->TagBody);
+
+  Cmd->TagBody.Gpio = 128 + Gpio;
+  Cmd->TagBody.Direction = Direction;
+  Cmd->TagBody.Polarity = Result;
+  Cmd->TagBody.TermEn = 0;
+  Cmd->TagBody.TermPullUp = 0;
+  Cmd->TagBody.State = State;
+
+  Cmd->TagHead.TagValueSize   = 0;
+  Cmd->EndTag                 = 0;
+
+  Status = MailboxTransaction (Cmd->BufferHead.BufferSize, RPI_MBOX_VC_CHANNEL, &Result);
+
+  ReleaseSpinLock (&mMailboxLock);
+
+  if (EFI_ERROR (Status) ||
+      Cmd->BufferHead.Response != RPI_MBOX_RESP_SUCCESS) {
+    DEBUG ((DEBUG_ERROR,
+      "%a: mailbox  transaction error: Status == %r, Response == 0x%x\n",
+      __FUNCTION__, Status, Cmd->BufferHead.Response));
+  }
+
+  RpiFirmwareSetGpio (Gpio,!State);
+
+
+  return Status;
+}
+
 STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL mRpiFirmwareProtocol = {
   RpiFirmwareSetPowerState,
   RpiFirmwareGetMacAddress,
@@ -1321,7 +1538,10 @@ STATIC RASPBERRY_PI_FIRMWARE_PROTOCOL mRpiFirmwareProtocol = {
   RpiFirmwareGetCpuName,
   RpiFirmwareGetArmMemory,
   RPiFirmwareGetModelInstalledMB,
-  RpiFirmwareNotifyXhciReset
+  RpiFirmwareNotifyXhciReset,
+  RpiFirmwareGetCurrentClockState,
+  RpiFirmwareSetClockState,
+  RpiFirmwareNotifyGpioSetCfg
 };
 
 /**
diff --git a/Platform/RaspberryPi/Include/Protocol/RpiFirmware.h b/Platform/RaspberryPi/Include/Protocol/RpiFirmware.h
index 56a8d15a38..d841608e57 100644
--- a/Platform/RaspberryPi/Include/Protocol/RpiFirmware.h
+++ b/Platform/RaspberryPi/Include/Protocol/RpiFirmware.h
@@ -37,6 +37,20 @@ EFI_STATUS
 
 typedef
 EFI_STATUS
+(EFIAPI *GET_CLOCK_STATE) (
+  IN  UINT32    ClockId,
+  OUT UINT32    *ClockState
+  );
+
+typedef
+EFI_STATUS
+(EFIAPI *SET_CLOCK_STATE) (
+  IN  UINT32 ClockId,
+  IN  UINT32 ClockState
+  );
+
+typedef
+EFI_STATUS
 (EFIAPI *GET_CLOCK_RATE) (
   IN  UINT32    ClockId,
   OUT UINT32    *ClockRate
@@ -149,6 +163,14 @@ EFI_STATUS
   UINTN FunctionNumber
   );
 
+typedef 
+EFI_STATUS
+(EFIAPI *GPIO_SET_CFG) (
+  UINTN Gpio,
+  UINTN Direction,
+  UINTN State
+  );
+
 typedef struct {
   SET_POWER_STATE        SetPowerState;
   GET_MAC_ADDRESS        GetMacAddress;
@@ -172,6 +194,9 @@ typedef struct {
   GET_ARM_MEM            GetArmMem;
   GET_MODEL_INSTALLED_MB GetModelInstalledMB;
   NOTIFY_XHCI_RESET      NotifyXhciReset;
+  GET_CLOCK_STATE        GetClockState;
+  SET_CLOCK_STATE        SetClockState;
+  GPIO_SET_CFG           SetGpioConfig;
 } RASPBERRY_PI_FIRMWARE_PROTOCOL;
 
 extern EFI_GUID gRaspberryPiFirmwareProtocolGuid;
-- 
2.13.7


  parent reply	other threads:[~2021-01-05 16:34 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-05 16:34 [PATCH v4 0/7] rpi4: Enable eMMC2 controller Jeremy Linton
2021-01-05 16:34 ` [PATCH v4 1/7] Platform/RaspberryPi: Update VPU mailbox constants Jeremy Linton
2021-01-05 16:34 ` Jeremy Linton [this message]
2021-01-05 16:34 ` [PATCH v4 3/7] Platform/RaspberryPi: Split MMC register definitions Jeremy Linton
2021-01-05 18:45   ` [edk2-devel] " Philippe Mathieu-Daudé
2021-01-05 16:34 ` [PATCH v4 4/7] Platform/RaspberryPi/Arasan: Add write delay and voltage/clock config Jeremy Linton
2021-01-05 16:34 ` [PATCH v4 5/7] Platform/RaspberryPi/Arasan: Select the correct base frequency Jeremy Linton
2021-01-05 16:34 ` [PATCH v4 6/7] Platform/RaspberryPi: Power up SD, and tweak GPIOs Jeremy Linton
2021-01-05 16:34 ` [PATCH v4 7/7] Platform/RaspberryPi: Correct device path removal Jeremy Linton
2021-01-06  9:11 ` [PATCH v4 0/7] rpi4: Enable eMMC2 controller Ard Biesheuvel
2021-01-06 11:23   ` Andrei Warkentin
2021-01-06 14:04   ` [edk2-devel] " 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=20210105163420.1711652-3-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