[U-Boot] [PATCH] mailbox: implement a sandbox test
Stephen Warren
swarren at wwwdotorg.org
Sat May 14 00:59:28 CEST 2016
From: Stephen Warren <swarren at nvidia.com>
This adds a sandbox mailbox implementation (provider), a test client
device, instantiates them both from Sandbox's DT, and adds a DM test
that excercises everything.
Signed-off-by: Stephen Warren <swarren at nvidia.com>
---
This depends on "Add a mailbox driver framework/uclass".
arch/sandbox/dts/test.dts | 11 ++++
arch/sandbox/include/asm/mbox.h | 14 +++++
configs/sandbox_defconfig | 2 +
drivers/mailbox/Kconfig | 7 +++
drivers/mailbox/Makefile | 2 +
drivers/mailbox/sandbox-mbox-test.c | 54 +++++++++++++++++++
drivers/mailbox/sandbox-mbox.c | 103 ++++++++++++++++++++++++++++++++++++
test/dm/Makefile | 1 +
test/dm/mailbox.c | 31 +++++++++++
9 files changed, 225 insertions(+)
create mode 100644 arch/sandbox/include/asm/mbox.h
create mode 100644 drivers/mailbox/sandbox-mbox-test.c
create mode 100644 drivers/mailbox/sandbox-mbox.c
create mode 100644 test/dm/mailbox.c
diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 89300096a5ab..686c215aea72 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -216,6 +216,17 @@
};
};
+ mbox: mbox {
+ compatible = "sandbox,mbox";
+ #mbox-cells = <1>;
+ };
+
+ mbox-test {
+ compatible = "sandbox,mbox-test";
+ mboxes = <&mbox 100>, <&mbox 1>;
+ mbox-names = "other", "test";
+ };
+
mmc {
compatible = "sandbox,mmc";
};
diff --git a/arch/sandbox/include/asm/mbox.h b/arch/sandbox/include/asm/mbox.h
new file mode 100644
index 000000000000..f4129ad1a471
--- /dev/null
+++ b/arch/sandbox/include/asm/mbox.h
@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2016, NVIDIA CORPORATION.
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+
+#include <common.h>
+
+struct udevice;
+
+int sandbox_mbox_test_get(struct udevice *dev);
+int sandbox_mbox_test_send(struct udevice *dev, uint32_t msg);
+int sandbox_mbox_test_recv(struct udevice *dev, uint32_t *msg);
+int sandbox_mbox_test_free(struct udevice *dev);
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index 9ad570c7fc49..cdada23c516f 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -167,3 +167,5 @@ CONFIG_UNIT_TEST=y
CONFIG_UT_TIME=y
CONFIG_UT_DM=y
CONFIG_UT_ENV=y
+CONFIG_DM_MAILBOX=y
+CONFIG_SANDBOX_MBOX=y
diff --git a/drivers/mailbox/Kconfig b/drivers/mailbox/Kconfig
index 8ae8edd28c52..9649b705895b 100644
--- a/drivers/mailbox/Kconfig
+++ b/drivers/mailbox/Kconfig
@@ -10,6 +10,13 @@ config DM_MAILBOX
the basis of a variety of inter-process/inter-CPU communication
protocols.
+config SANDBOX_MBOX
+ bool "Enable the sandbox mailbox test driver"
+ depends on DM_MAILBOX && SANDBOX
+ help
+ Enable support for a test mailbox implementation, which simply echos
+ back a modified version of any message that is sent.
+
config TEGRA_HSP
bool "Enable Tegra HSP controller support"
depends on DM_MAILBOX && TEGRA
diff --git a/drivers/mailbox/Makefile b/drivers/mailbox/Makefile
index b5bc84032573..155dbeb09942 100644
--- a/drivers/mailbox/Makefile
+++ b/drivers/mailbox/Makefile
@@ -3,4 +3,6 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_DM_MAILBOX) += mailbox-uclass.o
+obj-$(CONFIG_SANDBOX_MBOX) += sandbox-mbox.o
+obj-$(CONFIG_SANDBOX_MBOX) += sandbox-mbox-test.o
obj-$(CONFIG_TEGRA_HSP) += tegra-hsp.o
diff --git a/drivers/mailbox/sandbox-mbox-test.c b/drivers/mailbox/sandbox-mbox-test.c
new file mode 100644
index 000000000000..658ffc675659
--- /dev/null
+++ b/drivers/mailbox/sandbox-mbox-test.c
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2016, NVIDIA CORPORATION.
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+
+#include <common.h>
+#include <asm/io.h>
+#include <dm.h>
+#include <mailbox_client.h>
+
+struct sandbox_mbox_test {
+ struct mbox_chan chan;
+};
+
+int sandbox_mbox_test_get(struct udevice *dev)
+{
+ struct sandbox_mbox_test *sbmt = dev_get_priv(dev);
+
+ return mbox_get_by_name(dev, "test", &sbmt->chan);
+}
+
+int sandbox_mbox_test_send(struct udevice *dev, uint32_t msg)
+{
+ struct sandbox_mbox_test *sbmt = dev_get_priv(dev);
+
+ return mbox_send(&sbmt->chan, &msg);
+}
+
+int sandbox_mbox_test_recv(struct udevice *dev, uint32_t *msg)
+{
+ struct sandbox_mbox_test *sbmt = dev_get_priv(dev);
+
+ return mbox_recv(&sbmt->chan, msg, 100);
+}
+
+int sandbox_mbox_test_free(struct udevice *dev)
+{
+ struct sandbox_mbox_test *sbmt = dev_get_priv(dev);
+
+ return mbox_free(&sbmt->chan);
+}
+
+static const struct udevice_id sandbox_mbox_test_ids[] = {
+ { .compatible = "sandbox,mbox-test" },
+ { }
+};
+
+U_BOOT_DRIVER(sandbox_mbox_test) = {
+ .name = "sandbox_mbox_test",
+ .id = UCLASS_TEST,
+ .of_match = sandbox_mbox_test_ids,
+ .priv_auto_alloc_size = sizeof(struct sandbox_mbox_test),
+};
diff --git a/drivers/mailbox/sandbox-mbox.c b/drivers/mailbox/sandbox-mbox.c
new file mode 100644
index 000000000000..2cb1e50ef371
--- /dev/null
+++ b/drivers/mailbox/sandbox-mbox.c
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2016, NVIDIA CORPORATION.
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+
+#include <common.h>
+#include <asm/io.h>
+#include <dm.h>
+#include <mailbox_uclass.h>
+
+#define SANDBOX_MBOX_CHANNELS 2
+
+struct sandbox_mbox_chan {
+ bool rx_msg_valid;
+ uint32_t rx_msg;
+};
+
+struct sandbox_mbox {
+ struct sandbox_mbox_chan chans[SANDBOX_MBOX_CHANNELS];
+};
+
+static int sandbox_mbox_request(struct mbox_chan *chan)
+{
+ debug("%s(chan=%p)\n", __func__, chan);
+
+ if (chan->id >= SANDBOX_MBOX_CHANNELS)
+ return -EINVAL;
+
+ return 0;
+}
+
+static int sandbox_mbox_free(struct mbox_chan *chan)
+{
+ debug("%s(chan=%p)\n", __func__, chan);
+
+ return 0;
+}
+
+static int sandbox_mbox_send(struct mbox_chan *chan, const void *data)
+{
+ struct sandbox_mbox *sbm = dev_get_priv(chan->dev);
+ const uint32_t *pmsg = data;
+
+ debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
+
+ sbm->chans[chan->id].rx_msg = *pmsg ^ 0x12345678;
+ sbm->chans[chan->id].rx_msg_valid = true;
+
+ return 0;
+}
+
+static int sandbox_mbox_recv(struct mbox_chan *chan, void *data)
+{
+ struct sandbox_mbox *sbm = dev_get_priv(chan->dev);
+ uint32_t *pmsg = data;
+
+ debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
+
+ if (!sbm->chans[chan->id].rx_msg_valid)
+ return -ENODATA;
+
+ *pmsg = sbm->chans[chan->id].rx_msg;
+ sbm->chans[chan->id].rx_msg_valid = false;
+
+ return 0;
+}
+
+static int sandbox_mbox_bind(struct udevice *dev)
+{
+ debug("%s(dev=%p)\n", __func__, dev);
+
+ return 0;
+}
+
+static int sandbox_mbox_probe(struct udevice *dev)
+{
+ debug("%s(dev=%p)\n", __func__, dev);
+
+ return 0;
+}
+
+static const struct udevice_id sandbox_mbox_ids[] = {
+ { .compatible = "sandbox,mbox" },
+ { }
+};
+
+struct mbox_ops sandbox_mbox_mbox_ops = {
+ .request = sandbox_mbox_request,
+ .free = sandbox_mbox_free,
+ .send = sandbox_mbox_send,
+ .recv = sandbox_mbox_recv,
+};
+
+U_BOOT_DRIVER(sandbox_mbox) = {
+ .name = "sandbox_mbox",
+ .id = UCLASS_MAILBOX,
+ .of_match = sandbox_mbox_ids,
+ .bind = sandbox_mbox_bind,
+ .probe = sandbox_mbox_probe,
+ .priv_auto_alloc_size = sizeof(struct sandbox_mbox),
+ .ops = &sandbox_mbox_mbox_ops,
+};
diff --git a/test/dm/Makefile b/test/dm/Makefile
index fd781e82cfe8..9eaf04b9ba98 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -21,6 +21,7 @@ obj-$(CONFIG_DM_ETH) += eth.o
obj-$(CONFIG_DM_GPIO) += gpio.o
obj-$(CONFIG_DM_I2C) += i2c.o
obj-$(CONFIG_LED) += led.o
+obj-$(CONFIG_DM_MAILBOX) += mailbox.o
obj-$(CONFIG_DM_MMC) += mmc.o
obj-$(CONFIG_DM_PCI) += pci.o
obj-$(CONFIG_RAM) += ram.o
diff --git a/test/dm/mailbox.c b/test/dm/mailbox.c
new file mode 100644
index 000000000000..5d444ba270b6
--- /dev/null
+++ b/test/dm/mailbox.c
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2016, NVIDIA CORPORATION.
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <dm/test.h>
+#include <asm/mbox.h>
+#include <test/ut.h>
+
+static int dm_test_mailbox(struct unit_test_state *uts)
+{
+ struct udevice *dev;
+ uint32_t msg;
+
+ ut_assertok(uclass_get_device_by_name(UCLASS_TEST, "mbox-test", &dev));
+ ut_assertok(sandbox_mbox_test_get(dev));
+
+ ut_asserteq(-ETIMEDOUT, sandbox_mbox_test_recv(dev, &msg));
+ ut_assertok(sandbox_mbox_test_send(dev, 0xaaff9955UL));
+ ut_assertok(sandbox_mbox_test_recv(dev, &msg));
+ ut_asserteq(msg, 0xaaff9955UL ^ 0x12345678);
+ ut_asserteq(-ETIMEDOUT, sandbox_mbox_test_recv(dev, &msg));
+
+ ut_assertok(sandbox_mbox_test_free(dev));
+
+ return 0;
+}
+DM_TEST(dm_test_mailbox, DM_TESTF_SCAN_FDT);
--
2.8.2
More information about the U-Boot
mailing list