[U-Boot] [PATCH v2 3/3] test: Add tests for board uclass
Mario Six
mario.six at gdsys.cc
Fri Apr 27 12:51:34 UTC 2018
Add tests for the new board uclass.
Signed-off-by: Mario Six <mario.six at gdsys.cc>
---
v1 -> v2:
New in v2
---
arch/sandbox/dts/test.dts | 4 ++
configs/sandbox64_defconfig | 2 +
configs/sandbox_defconfig | 2 +
configs/sandbox_flattree_defconfig | 2 +
configs/sandbox_noblk_defconfig | 2 +
configs/sandbox_spl_defconfig | 2 +
drivers/board/Kconfig | 7 ++-
drivers/board/Makefile | 1 +
drivers/board/sandbox.c | 108 +++++++++++++++++++++++++++++++++++++
drivers/board/sandbox.h | 13 +++++
test/dm/Makefile | 1 +
test/dm/board.c | 57 ++++++++++++++++++++
12 files changed, 200 insertions(+), 1 deletion(-)
create mode 100644 drivers/board/sandbox.c
create mode 100644 drivers/board/sandbox.h
create mode 100644 test/dm/board.c
diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 06d0e8ce85..4f5f1c8474 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -535,6 +535,10 @@
};
};
};
+
+ board {
+ compatible = "sandbox,board_sandbox";
+ };
};
#include "sandbox_pmic.dtsi"
diff --git a/configs/sandbox64_defconfig b/configs/sandbox64_defconfig
index ff60508735..8e665c2073 100644
--- a/configs/sandbox64_defconfig
+++ b/configs/sandbox64_defconfig
@@ -87,6 +87,8 @@ CONFIG_CPU=y
CONFIG_DM_DEMO=y
CONFIG_DM_DEMO_SIMPLE=y
CONFIG_DM_DEMO_SHAPE=y
+CONFIG_BOARD=y
+CONFIG_BOARD_SANDBOX=y
CONFIG_PM8916_GPIO=y
CONFIG_SANDBOX_GPIO=y
CONFIG_DM_I2C_COMPAT=y
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index fd39519bed..3a2b9755bd 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -87,6 +87,8 @@ CONFIG_CPU=y
CONFIG_DM_DEMO=y
CONFIG_DM_DEMO_SIMPLE=y
CONFIG_DM_DEMO_SHAPE=y
+CONFIG_BOARD=y
+CONFIG_BOARD_SANDBOX=y
CONFIG_PM8916_GPIO=y
CONFIG_SANDBOX_GPIO=y
CONFIG_DM_I2C_COMPAT=y
diff --git a/configs/sandbox_flattree_defconfig b/configs/sandbox_flattree_defconfig
index 9b8d033838..d64b018510 100644
--- a/configs/sandbox_flattree_defconfig
+++ b/configs/sandbox_flattree_defconfig
@@ -69,6 +69,8 @@ CONFIG_CPU=y
CONFIG_DM_DEMO=y
CONFIG_DM_DEMO_SIMPLE=y
CONFIG_DM_DEMO_SHAPE=y
+CONFIG_BOARD=y
+CONFIG_BOARD_SANDBOX=y
CONFIG_PM8916_GPIO=y
CONFIG_SANDBOX_GPIO=y
CONFIG_DM_I2C_COMPAT=y
diff --git a/configs/sandbox_noblk_defconfig b/configs/sandbox_noblk_defconfig
index 8bdd4edcda..5e8a448547 100644
--- a/configs/sandbox_noblk_defconfig
+++ b/configs/sandbox_noblk_defconfig
@@ -76,6 +76,8 @@ CONFIG_CPU=y
CONFIG_DM_DEMO=y
CONFIG_DM_DEMO_SIMPLE=y
CONFIG_DM_DEMO_SHAPE=y
+CONFIG_BOARD=y
+CONFIG_BOARD_SANDBOX=y
CONFIG_PM8916_GPIO=y
CONFIG_SANDBOX_GPIO=y
CONFIG_DM_I2C_COMPAT=y
diff --git a/configs/sandbox_spl_defconfig b/configs/sandbox_spl_defconfig
index c308628199..d0e58dbffa 100644
--- a/configs/sandbox_spl_defconfig
+++ b/configs/sandbox_spl_defconfig
@@ -90,6 +90,8 @@ CONFIG_CPU=y
CONFIG_DM_DEMO=y
CONFIG_DM_DEMO_SIMPLE=y
CONFIG_DM_DEMO_SHAPE=y
+CONFIG_BOARD=y
+CONFIG_BOARD_SANDBOX=y
CONFIG_PM8916_GPIO=y
CONFIG_SANDBOX_GPIO=y
CONFIG_DM_I2C_COMPAT=y
diff --git a/drivers/board/Kconfig b/drivers/board/Kconfig
index cc1cf27205..2a3fc9c049 100644
--- a/drivers/board/Kconfig
+++ b/drivers/board/Kconfig
@@ -10,8 +10,13 @@ if BOARD
config BOARD_GAZERBEAM
- bool "Enable device information for the Gazerbeam board"
+ bool "Enable board driver for the Gazerbeam board"
help
Support querying device information for the gdsys Gazerbeam board.
+config BOARD_SANDBOX
+ bool "Enable board driver for the Sandbox board"
+ help
+ Support querying device information for the Sandbox boards.
+
endif
diff --git a/drivers/board/Makefile b/drivers/board/Makefile
index 397706245a..5a6e782e3b 100644
--- a/drivers/board/Makefile
+++ b/drivers/board/Makefile
@@ -7,3 +7,4 @@
obj-$(CONFIG_BOARD) += board-uclass.o
obj-$(CONFIG_BOARD_GAZERBEAM) += gazerbeam.o
+obj-$(CONFIG_BOARD_SANDBOX) += sandbox.o
diff --git a/drivers/board/sandbox.c b/drivers/board/sandbox.c
new file mode 100644
index 0000000000..3fd6d77780
--- /dev/null
+++ b/drivers/board/sandbox.c
@@ -0,0 +1,108 @@
+/*
+ * (C) Copyright 2018
+ * Mario Six, Guntermann & Drunck GmbH, mario.six at gdsys.cc
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <board.h>
+
+#include "sandbox.h"
+
+struct board_sandbox_priv {
+ bool called_detect;
+ int test_i1;
+ int test_i2;
+};
+
+char vacation_spots[][64] = {"R'lyeh", "Dreamlands", "Plateau of Leng",
+ "Carcosa", "Yuggoth", "The Nameless City"};
+
+int board_sandbox_detect(struct udevice *dev)
+{
+ struct board_sandbox_priv *priv = dev_get_priv(dev);
+
+ priv->called_detect = true;
+ priv->test_i2 = 100;
+
+ return 0;
+}
+
+int board_sandbox_get_bool(struct udevice *dev, int id, bool *val)
+{
+ struct board_sandbox_priv *priv = dev_get_priv(dev);
+
+ switch (id) {
+ case BOOL_CALLED_DETECT:
+ /* Checks if the dectect method has been called */
+ *val = priv->called_detect;
+ return 0;
+ }
+
+ return -ENOENT;
+}
+
+int board_sandbox_get_int(struct udevice *dev, int id, int *val)
+{
+ struct board_sandbox_priv *priv = dev_get_priv(dev);
+
+ switch (id) {
+ case INT_TEST1:
+ *val = priv->test_i1;
+ /* Increments with every call */
+ priv->test_i1++;
+ return 0;
+ case INT_TEST2:
+ *val = priv->test_i2;
+ /* Decrements with every call */
+ priv->test_i2--;
+ return 0;
+ }
+
+ return -ENOENT;
+}
+
+int board_sandbox_get_str(struct udevice *dev, int id, size_t size, char *val)
+{
+ struct board_sandbox_priv *priv = dev_get_priv(dev);
+ int i1 = priv->test_i1;
+ int i2 = priv->test_i2;
+ int index = (i1 * i2) % ARRAY_SIZE(vacation_spots);
+
+ switch (id) {
+ case STR_VACATIONSPOT:
+ /* Picks a vacation spot depending on i1 and i2 */
+ snprintf(val, size, vacation_spots[index]);
+ return 0;
+ }
+
+ return -ENOENT;
+}
+
+static const struct udevice_id board_sandbox_ids[] = {
+ { .compatible = "sandbox,board_sandbox" },
+ { /* sentinel */ }
+};
+
+static const struct board_ops board_sandbox_ops = {
+ .detect = board_sandbox_detect,
+ .get_bool = board_sandbox_get_bool,
+ .get_int = board_sandbox_get_int,
+ .get_str = board_sandbox_get_str,
+};
+
+int board_sandbox_probe(struct udevice *dev)
+{
+ return 0;
+}
+
+U_BOOT_DRIVER(board_sandbox) = {
+ .name = "board_sandbox",
+ .id = UCLASS_BOARD,
+ .of_match = board_sandbox_ids,
+ .ops = &board_sandbox_ops,
+ .priv_auto_alloc_size = sizeof(struct board_sandbox_priv),
+ .probe = board_sandbox_probe,
+};
diff --git a/drivers/board/sandbox.h b/drivers/board/sandbox.h
new file mode 100644
index 0000000000..4f5a02d13a
--- /dev/null
+++ b/drivers/board/sandbox.h
@@ -0,0 +1,13 @@
+/*
+ * (C) Copyright 2018
+ * Mario Six, Guntermann & Drunck GmbH, mario.six at gdsys.cc
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+enum {
+ BOOL_CALLED_DETECT,
+ INT_TEST1,
+ INT_TEST2,
+ STR_VACATIONSPOT,
+};
diff --git a/test/dm/Makefile b/test/dm/Makefile
index 513c4561ad..0583f45c7b 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -15,6 +15,7 @@ obj-$(CONFIG_UT_DM) += test-uclass.o
# subsystem you must add sandbox tests here.
obj-$(CONFIG_UT_DM) += core.o
ifneq ($(CONFIG_SANDBOX),)
+obj-$(CONFIG_BOARD) += board.o
obj-$(CONFIG_BLK) += blk.o
obj-$(CONFIG_CLK) += clk.o
obj-$(CONFIG_DM_ETH) += eth.o
diff --git a/test/dm/board.c b/test/dm/board.c
new file mode 100644
index 0000000000..700c280193
--- /dev/null
+++ b/test/dm/board.c
@@ -0,0 +1,57 @@
+/*
+ * (C) Copyright 2018
+ * Mario Six, Guntermann & Drunck GmbH, mario.six at gdsys.cc
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <dm/test.h>
+#include <board.h>
+#include <test/ut.h>
+
+#include "../../drivers/board/sandbox.h"
+
+static int dm_test_board(struct unit_test_state *uts)
+{
+ struct udevice *board;
+ bool called_detect;
+ char str[64];
+ int i;
+
+ get_board(&board);
+ ut_assert(board);
+
+ board_get_bool(board, BOOL_CALLED_DETECT, &called_detect);
+ ut_assert(!called_detect);
+
+ board_detect(board);
+
+ board_get_bool(board, BOOL_CALLED_DETECT, &called_detect);
+ ut_assert(called_detect);
+
+ board_get_str(board, STR_VACATIONSPOT, sizeof(str), str);
+ ut_assertok(strcmp(str, "R'lyeh"));
+
+ board_get_int(board, INT_TEST1, &i);
+ ut_asserteq(0, i);
+
+ board_get_int(board, INT_TEST2, &i);
+ ut_asserteq(100, i);
+
+ board_get_str(board, STR_VACATIONSPOT, sizeof(str), str);
+ ut_assertok(strcmp(str, "Carcosa"));
+
+ board_get_int(board, INT_TEST1, &i);
+ ut_asserteq(1, i);
+
+ board_get_int(board, INT_TEST2, &i);
+ ut_asserteq(99, i);
+
+ board_get_str(board, STR_VACATIONSPOT, sizeof(str), str);
+ ut_assertok(strcmp(str, "Yuggoth"));
+
+ return 0;
+}
+DM_TEST(dm_test_board, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
--
2.16.1
More information about the U-Boot
mailing list