[U-Boot] [PATCH 09/16] dm: board: Add tests for the board uclass

Simon Glass sjg at chromium.org
Sun Mar 19 18:59:28 UTC 2017


Add some tests which define some devices and check the operation of the
various init functions.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 arch/sandbox/dts/test.dts        | 12 +++++++
 arch/sandbox/include/asm/state.h |  3 ++
 arch/sandbox/include/asm/test.h  |  9 +++++
 drivers/misc/Makefile            |  1 +
 drivers/misc/board_sandbox.c     | 44 ++++++++++++++++++++++++
 test/dm/Makefile                 |  1 +
 test/dm/board.c                  | 74 ++++++++++++++++++++++++++++++++++++++++
 7 files changed, 144 insertions(+)
 create mode 100644 drivers/misc/board_sandbox.c
 create mode 100644 test/dm/board.c

diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index fff175d1b7..084c3dff63 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -52,6 +52,18 @@
 		reg = <2 1>;
 	};
 
+	board-test0 {
+		compatible = "sandbox,board-test0";
+	};
+
+	board-test1 {
+		compatible = "sandbox,board-test1";
+	};
+
+	board-test2 {
+		compatible = "sandbox,board-test2";
+	};
+
 	b-test {
 		reg = <3 1>;
 		compatible = "denx,u-boot-fdt-test";
diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h
index 149f28d873..d531531d72 100644
--- a/arch/sandbox/include/asm/state.h
+++ b/arch/sandbox/include/asm/state.h
@@ -9,6 +9,7 @@
 #include <config.h>
 #include <sysreset.h>
 #include <stdbool.h>
+#include <asm/test.h>
 #include <linux/stringify.h>
 
 /**
@@ -65,6 +66,8 @@ struct sandbox_state {
 	enum state_terminal_raw term_raw;	/* Terminal raw/cooked */
 	bool skip_delays;		/* Ignore any time delays (for test) */
 	bool show_test_output;		/* Don't suppress stdout in tests */
+	/* Return values for board_sandbox */
+	int board_sandbox_ret[BOARD_TEST_COUNT];
 
 	/* Pointer to information for each SPI bus/cs */
 	struct sandbox_spi_info spi[CONFIG_SANDBOX_SPI_MAX_BUS]
diff --git a/arch/sandbox/include/asm/test.h b/arch/sandbox/include/asm/test.h
index 451a78e590..12b3b9bc1e 100644
--- a/arch/sandbox/include/asm/test.h
+++ b/arch/sandbox/include/asm/test.h
@@ -79,4 +79,13 @@ long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time);
 
 int sandbox_usb_keyb_add_string(struct udevice *dev, const char *str);
 
+/* For testing the BOARD uclass */
+enum {
+	BOARD_TEST0,
+	BOARD_TEST1,
+	BOARD_TEST2,
+
+	BOARD_TEST_COUNT,
+};
+
 #endif
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index e3151ea097..88015cc8af 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -8,6 +8,7 @@
 obj-$(CONFIG_MISC) += misc-uclass.o
 obj-$(CONFIG_ALI152X) += ali512x.o
 obj-$(CONFIG_ALTERA_SYSID) += altera_sysid.o
+obj-$(CONFIG_SANDBOX) += board_sandbox.o
 obj-$(CONFIG_DS4510)  += ds4510.o
 obj-$(CONFIG_CBMEM_CONSOLE) += cbmem_console.o
 ifndef CONFIG_SPL_BUILD
diff --git a/drivers/misc/board_sandbox.c b/drivers/misc/board_sandbox.c
new file mode 100644
index 0000000000..9ccdbfbbe8
--- /dev/null
+++ b/drivers/misc/board_sandbox.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2017 Google, Inc
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <board.h>
+#include <dm.h>
+#include <asm/state.h>
+#include <asm/test.h>
+
+static int board_sandbox_phase(struct udevice *dev, enum board_phase_t phase)
+{
+	struct sandbox_state *state = state_get_current();
+	int id = dev_get_driver_data(dev);
+
+	return state->board_sandbox_ret[id];
+}
+
+static int board_sandbox_probe(struct udevice *dev)
+{
+	return board_support_phase(dev, BOARD_PHASE_TEST);
+}
+
+static const struct board_ops board_sandbox_ops = {
+	.phase		= board_sandbox_phase,
+};
+
+
+static const struct udevice_id board_sandbox_ids[] = {
+	{ .compatible = "sandbox,board-test0", BOARD_TEST0 },
+	{ .compatible = "sandbox,board-test1", BOARD_TEST1 },
+	{ .compatible = "sandbox,board-test2", BOARD_TEST2 },
+	{ }
+};
+
+U_BOOT_DRIVER(board_sandbox_drv) = {
+	.name		= "board_sandbox",
+	.id		= UCLASS_BOARD,
+	.ops		= &board_sandbox_ops,
+	.of_match	= board_sandbox_ids,
+	.probe		= board_sandbox_probe,
+};
diff --git a/test/dm/Makefile b/test/dm/Makefile
index 1885e17c38..c84a966708 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..986746632b
--- /dev/null
+++ b/test/dm/board.c
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2015 Google, Inc
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <asm/state.h>
+#include <asm/test.h>
+#include <dm/test.h>
+#include <test/ut.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* Test invoking a board phase with three active devices */
+static int dm_test_board(struct unit_test_state *uts)
+{
+	struct sandbox_state *state = state_get_current();
+
+	/* We should start with a count of 0 for our test phase */
+	ut_asserteq(0, gd->phase_count[BOARD_PHASE_TEST]);
+
+	/* Check that we can detect there being no driver */
+	ut_asserteq(-ENOSYS, board_walk_phase_count(BOARD_PHASE_INVALID,
+						    false));
+	ut_asserteq(0, board_walk_opt_phase(BOARD_PHASE_INVALID));
+	ut_asserteq(-ENOSYS, board_walk_phase(BOARD_PHASE_INVALID));
+
+	/* If no devices respond, we should get no action */
+	state->board_sandbox_ret[BOARD_TEST0] = -ENOSYS;
+	state->board_sandbox_ret[BOARD_TEST1] = -ENOSYS;
+	state->board_sandbox_ret[BOARD_TEST2] = -ENOSYS;
+	ut_asserteq(-ENOSYS, board_walk_phase_count(BOARD_PHASE_TEST, false));
+	ut_asserteq(0, board_walk_opt_phase(BOARD_PHASE_TEST));
+	ut_asserteq(0, gd->phase_count[BOARD_PHASE_TEST]);
+
+	/* Enable the first device */
+	state->board_sandbox_ret[BOARD_TEST0] = 0;
+	ut_asserteq(1, board_walk_phase_count(BOARD_PHASE_TEST, false));
+	ut_asserteq(1, gd->phase_count[BOARD_PHASE_TEST]);
+
+	/* Enable the second device too */
+	state->board_sandbox_ret[BOARD_TEST1] = 0;
+	ut_asserteq(2, board_walk_phase_count(BOARD_PHASE_TEST, false));
+	ut_asserteq(3, gd->phase_count[BOARD_PHASE_TEST]);
+
+	/* Enable all three devices */
+	state->board_sandbox_ret[BOARD_TEST2] = 0;
+	ut_asserteq(3, board_walk_phase_count(BOARD_PHASE_TEST, false));
+	ut_asserteq(6, gd->phase_count[BOARD_PHASE_TEST]);
+
+	/*
+	 * Check that the first device can claim the phase and lock out the
+	 * other devices.
+	 */
+	state->board_sandbox_ret[BOARD_TEST0] = BOARD_PHASE_CLAIMED;
+	ut_asserteq(1, board_walk_phase_count(BOARD_PHASE_TEST, false));
+	ut_asserteq(0, board_walk_phase(BOARD_PHASE_TEST));
+	ut_asserteq(0, board_walk_opt_phase(BOARD_PHASE_TEST));
+	ut_asserteq(9, gd->phase_count[BOARD_PHASE_TEST]);
+
+	/* Any error should be reported, but previous devices should still get
+	 * to process the phase.
+	 */
+	state->board_sandbox_ret[BOARD_TEST0] = 0;
+	state->board_sandbox_ret[BOARD_TEST1] = -ENOENT;
+	ut_asserteq(-ENOENT, board_walk_phase_count(BOARD_PHASE_TEST, false));
+	ut_asserteq(-ENOENT, board_walk_phase(BOARD_PHASE_TEST));
+	ut_asserteq(-ENOENT, board_walk_opt_phase(BOARD_PHASE_TEST));
+	ut_asserteq(12, gd->phase_count[BOARD_PHASE_TEST]);
+
+	return 0;
+}
+DM_TEST(dm_test_board, DM_TESTF_SCAN_FDT);
-- 
2.12.0.367.g23dc2f6d3c-goog



More information about the U-Boot mailing list