mailbox: implement a sandbox test
authorStephen Warren <swarren@nvidia.com>
Mon, 16 May 2016 23:41:37 +0000 (17:41 -0600)
committerSimon Glass <sjg@chromium.org>
Fri, 27 May 2016 02:48:31 +0000 (20:48 -0600)
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@nvidia.com>
Acked-by: Simon Glass <sjg@chromium.org> # v1
arch/sandbox/dts/test.dts
arch/sandbox/include/asm/mbox.h [new file with mode: 0644]
configs/sandbox_defconfig
drivers/mailbox/Kconfig
drivers/mailbox/Makefile
drivers/mailbox/sandbox-mbox-test.c [new file with mode: 0644]
drivers/mailbox/sandbox-mbox.c [new file with mode: 0644]
test/dm/Makefile
test/dm/mailbox.c [new file with mode: 0644]

index 89300096a5ab6655e4bfb710ee295c51f2b49cc0..686c215aea72ccadbe591aa0bda7313a9b915d92 100644 (file)
                };
        };
 
+       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 (file)
index 0000000..2d7b7d0
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2016, NVIDIA CORPORATION.
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+
+#ifndef __SANDBOX_MBOX_H
+#define __SANDBOX_MBOX_H
+
+#include <common.h>
+
+#define SANDBOX_MBOX_PING_XOR 0x12345678
+
+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);
+
+#endif
index 6e30c5df8b6672b7ece61db8b9349260a9a2bf27..4eb3c224fdc075c0d313005bfe5c97f8d9c1ecb5 100644 (file)
@@ -170,3 +170,6 @@ CONFIG_UNIT_TEST=y
 CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
 CONFIG_UT_ENV=y
+CONFIG_MISC=y
+CONFIG_DM_MAILBOX=y
+CONFIG_SANDBOX_MBOX=y
index 295e6db0f28c79959d0c364aa4d6a833cf37804a..90875123901084ef4dec4c00c7e426ebd716d8b6 100644 (file)
@@ -10,4 +10,11 @@ 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.
+
 endmenu
index a2d96a4e3b9871ab46af947f2c52b3d5765a9759..bbae4def6d45a54a07bdab00b12a35877a206739 100644 (file)
@@ -3,3 +3,5 @@
 # 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
diff --git a/drivers/mailbox/sandbox-mbox-test.c b/drivers/mailbox/sandbox-mbox-test.c
new file mode 100644 (file)
index 0000000..02d161a
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2016, NVIDIA CORPORATION.
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <mailbox_client.h>
+#include <asm/io.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_MISC,
+       .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 (file)
index 0000000..1b7ac23
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2016, NVIDIA CORPORATION.
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <mailbox_uclass.h>
+#include <asm/io.h>
+#include <asm/mbox.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 ^ SANDBOX_MBOX_PING_XOR;
+       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,
+};
index fd781e82cfe8d848ccac55cb1b58bb2c8e383c70..9eaf04b9ba98cd06739e8cab87a06ae95bcb09d4 100644 (file)
@@ -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 (file)
index 0000000..be7bd6d
--- /dev/null
@@ -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_MISC, "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 ^ SANDBOX_MBOX_PING_XOR);
+       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);