test: dm: add MDIO test
authorAlex Marginean <alexm.osslist@gmail.com>
Mon, 3 Jun 2019 16:12:28 +0000 (19:12 +0300)
committerJoe Hershberger <joe.hershberger@ni.com>
Mon, 15 Jul 2019 18:32:25 +0000 (13:32 -0500)
A very simple test for DM_MDIO, mimicks a register write/read through the
sandbox bus to a dummy PHY.

Signed-off-by: Alex Marginean <alexm.osslist@gmail.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Tested-by: Bin Meng <bmeng.cn@gmail.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
arch/Kconfig
arch/sandbox/dts/test.dts
drivers/net/Kconfig
drivers/net/Makefile
drivers/net/mdio_sandbox.c [new file with mode: 0644]
test/dm/Makefile
test/dm/mdio.c [new file with mode: 0644]

index 355d2145223f0ec29fcb4d8191fdd23f960a9883..a946af816f27b73bcd2af7263c22e084a9bc03a4 100644 (file)
@@ -123,6 +123,8 @@ config SANDBOX
        imply DM_SOUND
        imply PCI_SANDBOX_EP
        imply PCH
+       imply PHYLIB
+       imply DM_MDIO
 
 config SH
        bool "SuperH architecture"
index 8147d9781ea416672b7356a1e109737871a290f2..531c1afc973e123b4e497d2be162a31938f6198b 100644 (file)
                dmas = <&dma 0>, <&dma 1>, <&dma 2>;
                dma-names = "m2m", "tx0", "rx0";
        };
+
+       mdio-test {
+               compatible = "sandbox,mdio";
+       };
 };
 
 #include "sandbox_pmic.dtsi"
index 6fba5a84dd9f30914cbfb09467b96f429dd630d0..635f8d72c2dccb35dcee4911dc9838eb8c7ed20b 100644 (file)
@@ -24,6 +24,16 @@ config DM_MDIO
          This is currently implemented in net/mdio-uclass.c
          Look in include/miiphy.h for details.
 
+config MDIO_SANDBOX
+       depends on DM_MDIO && SANDBOX
+       default y
+       bool "Sandbox: Mocked MDIO driver"
+       help
+         This driver implements dummy read/write/reset MDIO functions mimicking
+         a bus with a single PHY.
+
+         This driver is used in for testing in test/dm/mdio.c
+
 menuconfig NETDEVICES
        bool "Network device support"
        depends on NET
index 8d02a378964b88facdbaef74756f9bdc838d3765..40038427dbd822cfcf7a0b3b6bc65003c5d6043e 100644 (file)
@@ -77,3 +77,4 @@ obj-y += ti/
 obj-$(CONFIG_MEDIATEK_ETH) += mtk_eth.o
 obj-y += mscc_eswitch/
 obj-$(CONFIG_HIGMACV300_ETH) += higmacv300.o
+obj-$(CONFIG_MDIO_SANDBOX) += mdio_sandbox.o
diff --git a/drivers/net/mdio_sandbox.c b/drivers/net/mdio_sandbox.c
new file mode 100644 (file)
index 0000000..07515e0
--- /dev/null
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2019
+ * Alex Marginean, NXP
+ */
+
+#include <dm.h>
+#include <errno.h>
+#include <miiphy.h>
+
+#define SANDBOX_PHY_ADDR       5
+#define SANDBOX_PHY_REG                0
+
+struct mdio_sandbox_priv {
+       int enabled;
+       u16 reg;
+};
+
+static int mdio_sandbox_read(struct udevice *dev, int addr, int devad, int reg)
+{
+       struct mdio_sandbox_priv *priv = dev_get_priv(dev);
+
+       if (!priv->enabled)
+               return -ENODEV;
+
+       if (addr != SANDBOX_PHY_ADDR)
+               return -ENODEV;
+       if (devad != MDIO_DEVAD_NONE)
+               return -ENODEV;
+       if (reg != SANDBOX_PHY_REG)
+               return -ENODEV;
+
+       return priv->reg;
+}
+
+static int mdio_sandbox_write(struct udevice *dev, int addr, int devad, int reg,
+                             u16 val)
+{
+       struct mdio_sandbox_priv *priv = dev_get_priv(dev);
+
+       if (!priv->enabled)
+               return -ENODEV;
+
+       if (addr != SANDBOX_PHY_ADDR)
+               return -ENODEV;
+       if (devad != MDIO_DEVAD_NONE)
+               return -ENODEV;
+       if (reg != SANDBOX_PHY_REG)
+               return -ENODEV;
+
+       priv->reg = val;
+
+       return 0;
+}
+
+static int mdio_sandbox_reset(struct udevice *dev)
+{
+       struct mdio_sandbox_priv *priv = dev_get_priv(dev);
+
+       priv->reg = 0;
+
+       return 0;
+}
+
+static const struct mdio_ops mdio_sandbox_ops = {
+       .read = mdio_sandbox_read,
+       .write = mdio_sandbox_write,
+       .reset = mdio_sandbox_reset,
+};
+
+static int mdio_sandbox_probe(struct udevice *dev)
+{
+       struct mdio_sandbox_priv *priv = dev_get_priv(dev);
+
+       priv->enabled = 1;
+
+       return 0;
+}
+
+static const struct udevice_id mdio_sandbox_ids[] = {
+       { .compatible = "sandbox,mdio" },
+       { }
+};
+
+U_BOOT_DRIVER(mdio_sandbox) = {
+       .name           = "mdio_sandbox",
+       .id             = UCLASS_MDIO,
+       .of_match       = mdio_sandbox_ids,
+       .probe          = mdio_sandbox_probe,
+       .ops            = &mdio_sandbox_ops,
+       .priv_auto_alloc_size = sizeof(struct mdio_sandbox_priv),
+};
index 420bf811545593cff2dc56a65f91e14cdf456b56..6a36cc0a328983ab6bce316eb4124d2fcf415424 100644 (file)
@@ -62,4 +62,5 @@ obj-$(CONFIG_SOUND) += sound.o
 obj-$(CONFIG_TEE) += tee.o
 obj-$(CONFIG_VIRTIO_SANDBOX) += virtio.o
 obj-$(CONFIG_DMA) += dma.o
+obj-$(CONFIG_DM_MDIO) += mdio.o
 endif
diff --git a/test/dm/mdio.c b/test/dm/mdio.c
new file mode 100644 (file)
index 0000000..5b66255
--- /dev/null
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * (C) Copyright 2019
+ * Alex Marginean, NXP
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <dm/test.h>
+#include <misc.h>
+#include <test/ut.h>
+#include <miiphy.h>
+
+/* macros copied over from mdio_sandbox.c */
+#define SANDBOX_PHY_ADDR       5
+#define SANDBOX_PHY_REG                0
+
+#define TEST_REG_VALUE         0xabcd
+
+static int dm_test_mdio(struct unit_test_state *uts)
+{
+       struct uclass *uc;
+       struct udevice *dev;
+       struct mdio_ops *ops;
+       u16 reg;
+
+       ut_assertok(uclass_get(UCLASS_MDIO, &uc));
+
+       ut_assertok(uclass_get_device_by_name(UCLASS_MDIO, "mdio-test", &dev));
+
+       ops = mdio_get_ops(dev);
+       ut_assertnonnull(ops);
+       ut_assertnonnull(ops->read);
+       ut_assertnonnull(ops->write);
+
+       ut_assertok(ops->write(dev, SANDBOX_PHY_ADDR, MDIO_DEVAD_NONE,
+                              SANDBOX_PHY_REG, TEST_REG_VALUE));
+       reg = ops->read(dev, SANDBOX_PHY_ADDR, MDIO_DEVAD_NONE,
+                       SANDBOX_PHY_REG);
+       ut_asserteq(reg, TEST_REG_VALUE);
+
+       ut_assert(ops->read(dev, SANDBOX_PHY_ADDR + 1, MDIO_DEVAD_NONE,
+                           SANDBOX_PHY_REG) != 0);
+
+       ut_assertok(ops->reset(dev));
+       reg = ops->read(dev, SANDBOX_PHY_ADDR, MDIO_DEVAD_NONE,
+                       SANDBOX_PHY_REG);
+       ut_asserteq(reg, 0);
+
+       return 0;
+}
+
+DM_TEST(dm_test_mdio, DM_TESTF_SCAN_FDT);