]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
mailbox: apple: Add driver for Apple IOP mailbox
authorMark Kettenis <kettenis@openbsd.org>
Sat, 22 Jan 2022 19:38:12 +0000 (20:38 +0100)
committerTom Rini <trini@konsulko.com>
Thu, 10 Feb 2022 21:44:23 +0000 (16:44 -0500)
This mailbox driver provides a communication channel with the
Apple IOP controllers found on Apple SoCs.  These IOP controllers
are used to implement various functions such as the System
Manegement Controller (SMC) and NVMe storage.  It allows sending
and receiving a 96-bit message over a single channel.

The header file with the struct used for mailbox messages is taken
straight from Linux.

Signed-off-by: Mark Kettenis <kettenis@openbsd.org>
Signed-off-by: Sven Peter <sven@svenpeter.dev>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested on: Macbook Air M1
Tested-by: Simon Glass <sjg@chromium.org>
arch/arm/Kconfig
drivers/mailbox/Kconfig
drivers/mailbox/Makefile
drivers/mailbox/apple-mbox.c [new file with mode: 0644]
include/linux/apple-mailbox.h [new file with mode: 0644]

index 9a62b55786958527475e13d29e0964af099683f7..ce51d87ac069408a001a8f06958744e697b77f9f 100644 (file)
@@ -931,6 +931,7 @@ config ARCH_APPLE
        select DM
        select DM_GPIO
        select DM_KEYBOARD
+       select DM_MAILBOX
        select DM_SERIAL
        select DM_USB
        select DM_VIDEO
index dd4b0ac0c3393e9ec802c3f6e983fe96192d9ca7..73db2af0b8154b6508d678a941eb517b7d303970 100644 (file)
@@ -10,6 +10,17 @@ config DM_MAILBOX
          the basis of a variety of inter-process/inter-CPU communication
          protocols.
 
+config APPLE_MBOX
+       bool "Enable Apple IOP controller support"
+       depends on DM_MAILBOX && ARCH_APPLE
+       default y
+       help
+         Enable support for the mailboxes that provide a comminucation
+         channel with Apple IOP controllers integrated on Apple SoCs.
+         These IOP controllers are used to implement various functions
+         such as the System Management Controller (SMC) and NVMe and this
+         driver is required to get that functionality up and running.
+
 config SANDBOX_MBOX
        bool "Enable the sandbox mailbox test driver"
        depends on DM_MAILBOX && SANDBOX
index d2ace8cd212e339d29fa189ef36d98c8d3e67096..59e8d0de93cacfcbd9e10b0796a7abc53653715e 100644 (file)
@@ -4,6 +4,7 @@
 #
 
 obj-$(CONFIG_$(SPL_)DM_MAILBOX) += mailbox-uclass.o
+obj-$(CONFIG_APPLE_MBOX) += apple-mbox.o
 obj-$(CONFIG_SANDBOX_MBOX) += sandbox-mbox.o
 obj-$(CONFIG_SANDBOX_MBOX) += sandbox-mbox-test.o
 obj-$(CONFIG_STM32_IPCC) += stm32-ipcc.o
diff --git a/drivers/mailbox/apple-mbox.c b/drivers/mailbox/apple-mbox.c
new file mode 100644 (file)
index 0000000..30c8e2f
--- /dev/null
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <mailbox-uclass.h>
+#include <asm/io.h>
+#include <linux/apple-mailbox.h>
+#include <linux/delay.h>
+
+#define REG_A2I_STAT   0x110
+#define  REG_A2I_STAT_EMPTY    BIT(17)
+#define  REG_A2I_STAT_FULL     BIT(16)
+#define REG_I2A_STAT   0x114
+#define  REG_I2A_STAT_EMPTY    BIT(17)
+#define  REG_I2A_STAT_FULL     BIT(16)
+#define REG_A2I_MSG0   0x800
+#define REG_A2I_MSG1   0x808
+#define REG_I2A_MSG0   0x830
+#define REG_I2A_MSG1   0x838
+
+struct apple_mbox_priv {
+       void *base;
+};
+
+static int apple_mbox_of_xlate(struct mbox_chan *chan,
+                              struct ofnode_phandle_args *args)
+{
+       if (args->args_count != 0)
+               return -EINVAL;
+
+       return 0;
+}
+
+static int apple_mbox_send(struct mbox_chan *chan, const void *data)
+{
+       struct apple_mbox_priv *priv = dev_get_priv(chan->dev);
+       const struct apple_mbox_msg *msg = data;
+
+       writeq(msg->msg0, priv->base + REG_A2I_MSG0);
+       writeq(msg->msg1, priv->base + REG_A2I_MSG1);
+       while (readl(priv->base + REG_A2I_STAT) & REG_A2I_STAT_FULL)
+               udelay(1);
+
+       return 0;
+}
+
+static int apple_mbox_recv(struct mbox_chan *chan, void *data)
+{
+       struct apple_mbox_priv *priv = dev_get_priv(chan->dev);
+       struct apple_mbox_msg *msg = data;
+
+       if (readl(priv->base + REG_I2A_STAT) & REG_I2A_STAT_EMPTY)
+               return -ENODATA;
+
+       msg->msg0 = readq(priv->base + REG_I2A_MSG0);
+       msg->msg1 = readq(priv->base + REG_I2A_MSG1);
+       return 0;
+}
+
+struct mbox_ops apple_mbox_ops = {
+       .of_xlate = apple_mbox_of_xlate,
+       .send = apple_mbox_send,
+       .recv = apple_mbox_recv,
+};
+
+static int apple_mbox_probe(struct udevice *dev)
+{
+       struct apple_mbox_priv *priv = dev_get_priv(dev);
+
+       priv->base = dev_read_addr_ptr(dev);
+       if (!priv->base)
+               return -EINVAL;
+
+       return 0;
+}
+
+static const struct udevice_id apple_mbox_of_match[] = {
+       { .compatible = "apple,asc-mailbox-v4" },
+       { /* sentinel */ }
+};
+
+U_BOOT_DRIVER(apple_mbox) = {
+       .name = "apple-mbox",
+       .id = UCLASS_MAILBOX,
+       .of_match = apple_mbox_of_match,
+       .probe = apple_mbox_probe,
+       .priv_auto = sizeof(struct apple_mbox_priv),
+       .ops = &apple_mbox_ops,
+};
diff --git a/include/linux/apple-mailbox.h b/include/linux/apple-mailbox.h
new file mode 100644 (file)
index 0000000..720fbb7
--- /dev/null
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
+/*
+ * Apple mailbox message format
+ *
+ * Copyright (C) 2021 The Asahi Linux Contributors
+ */
+
+#ifndef _LINUX_APPLE_MAILBOX_H_
+#define _LINUX_APPLE_MAILBOX_H_
+
+#include <linux/types.h>
+
+/* encodes a single 96bit message sent over the single channel */
+struct apple_mbox_msg {
+       u64 msg0;
+       u32 msg1;
+};
+
+#endif