xref: /rk3399_rockchip-uboot/test/dm/test-main.c (revision c166c47ba3a699df58e2ade8935158df65b034ba)
12e7d35d2SSimon Glass /*
22e7d35d2SSimon Glass  * Copyright (c) 2013 Google, Inc
32e7d35d2SSimon Glass  *
42e7d35d2SSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
52e7d35d2SSimon Glass  */
62e7d35d2SSimon Glass 
72e7d35d2SSimon Glass #include <common.h>
840441e0bSJoe Hershberger #include <command.h>
924b852a7SSimon Glass #include <console.h>
102e7d35d2SSimon Glass #include <dm.h>
112e7d35d2SSimon Glass #include <errno.h>
12756ac0bbSSimon Glass #include <malloc.h>
133884c98cSSimon Glass #include <asm/state.h>
142e7d35d2SSimon Glass #include <dm/test.h>
152e7d35d2SSimon Glass #include <dm/root.h>
162e7d35d2SSimon Glass #include <dm/uclass-internal.h>
17e721b882SJoe Hershberger #include <test/ut.h>
182e7d35d2SSimon Glass 
192e7d35d2SSimon Glass DECLARE_GLOBAL_DATA_PTR;
202e7d35d2SSimon Glass 
21e721b882SJoe Hershberger struct unit_test_state global_dm_test_state;
22e721b882SJoe Hershberger static struct dm_test_state _global_priv_dm_test_state;
232e7d35d2SSimon Glass 
242e7d35d2SSimon Glass /* Get ready for testing */
25*c166c47bSSimon Glass static int dm_test_init(struct unit_test_state *uts, bool of_live)
262e7d35d2SSimon Glass {
27e721b882SJoe Hershberger 	struct dm_test_state *dms = uts->priv;
28e721b882SJoe Hershberger 
292e7d35d2SSimon Glass 	memset(dms, '\0', sizeof(*dms));
302e7d35d2SSimon Glass 	gd->dm_root = NULL;
312e7d35d2SSimon Glass 	memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
3234b744beSSimon Glass 	state_reset_for_test(state_get_current());
332e7d35d2SSimon Glass 
34*c166c47bSSimon Glass #ifdef CONFIG_OF_LIVE
35*c166c47bSSimon Glass 	/* Determine whether to make the live tree available */
36*c166c47bSSimon Glass 	gd->of_root = of_live ? uts->of_root : NULL;
37*c166c47bSSimon Glass #endif
38*c166c47bSSimon Glass 	ut_assertok(dm_init(of_live));
392e7d35d2SSimon Glass 	dms->root = dm_root();
402e7d35d2SSimon Glass 
412e7d35d2SSimon Glass 	return 0;
422e7d35d2SSimon Glass }
432e7d35d2SSimon Glass 
442e7d35d2SSimon Glass /* Ensure all the test devices are probed */
45e721b882SJoe Hershberger static int do_autoprobe(struct unit_test_state *uts)
462e7d35d2SSimon Glass {
4754c5d08aSHeiko Schocher 	struct udevice *dev;
482e7d35d2SSimon Glass 	int ret;
492e7d35d2SSimon Glass 
502e7d35d2SSimon Glass 	/* Scanning the uclass is enough to probe all the devices */
512e7d35d2SSimon Glass 	for (ret = uclass_first_device(UCLASS_TEST, &dev);
522e7d35d2SSimon Glass 	     dev;
532e7d35d2SSimon Glass 	     ret = uclass_next_device(&dev))
542e7d35d2SSimon Glass 		;
552e7d35d2SSimon Glass 
562e7d35d2SSimon Glass 	return ret;
572e7d35d2SSimon Glass }
582e7d35d2SSimon Glass 
59e721b882SJoe Hershberger static int dm_test_destroy(struct unit_test_state *uts)
602e7d35d2SSimon Glass {
612e7d35d2SSimon Glass 	int id;
622e7d35d2SSimon Glass 
632e7d35d2SSimon Glass 	for (id = 0; id < UCLASS_COUNT; id++) {
642e7d35d2SSimon Glass 		struct uclass *uc;
652e7d35d2SSimon Glass 
662e7d35d2SSimon Glass 		/*
672e7d35d2SSimon Glass 		 * If the uclass doesn't exist we don't want to create it. So
682e7d35d2SSimon Glass 		 * check that here before we call uclass_find_device()/
692e7d35d2SSimon Glass 		 */
702e7d35d2SSimon Glass 		uc = uclass_find(id);
712e7d35d2SSimon Glass 		if (!uc)
722e7d35d2SSimon Glass 			continue;
732e7d35d2SSimon Glass 		ut_assertok(uclass_destroy(uc));
742e7d35d2SSimon Glass 	}
752e7d35d2SSimon Glass 
762e7d35d2SSimon Glass 	return 0;
772e7d35d2SSimon Glass }
782e7d35d2SSimon Glass 
79*c166c47bSSimon Glass static int dm_do_test(struct unit_test_state *uts, struct unit_test *test,
80*c166c47bSSimon Glass 		      bool of_live)
81f86db10cSSimon Glass {
82f86db10cSSimon Glass 	struct sandbox_state *state = state_get_current();
83801587bdSSimon Glass 	const char *fname = strrchr(test->file, '/') + 1;
84f86db10cSSimon Glass 
85*c166c47bSSimon Glass 	printf("Test: %s: %s%s\n", test->name, fname,
86*c166c47bSSimon Glass 	       !of_live ? " (flat tree)" : "");
87*c166c47bSSimon Glass 	ut_assertok(dm_test_init(uts, of_live));
88f86db10cSSimon Glass 
89f86db10cSSimon Glass 	uts->start = mallinfo();
90f86db10cSSimon Glass 	if (test->flags & DM_TESTF_SCAN_PDATA)
91f86db10cSSimon Glass 		ut_assertok(dm_scan_platdata(false));
92f86db10cSSimon Glass 	if (test->flags & DM_TESTF_PROBE_TEST)
93f86db10cSSimon Glass 		ut_assertok(do_autoprobe(uts));
94f86db10cSSimon Glass 	if (test->flags & DM_TESTF_SCAN_FDT)
95f86db10cSSimon Glass 		ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
96f86db10cSSimon Glass 
97f86db10cSSimon Glass 	/*
98f86db10cSSimon Glass 	 * Silence the console and rely on console reocrding to get
99f86db10cSSimon Glass 	 * our output.
100f86db10cSSimon Glass 	 */
101f86db10cSSimon Glass 	console_record_reset();
102f86db10cSSimon Glass 	if (!state->show_test_output)
103f86db10cSSimon Glass 		gd->flags |= GD_FLG_SILENT;
104f86db10cSSimon Glass 	test->func(uts);
105f86db10cSSimon Glass 	gd->flags &= ~GD_FLG_SILENT;
106f86db10cSSimon Glass 	state_set_skip_delays(false);
107f86db10cSSimon Glass 
108f86db10cSSimon Glass 	ut_assertok(dm_test_destroy(uts));
109f86db10cSSimon Glass 
110f86db10cSSimon Glass 	return 0;
111f86db10cSSimon Glass }
112f86db10cSSimon Glass 
11340441e0bSJoe Hershberger static int dm_test_main(const char *test_name)
1142e7d35d2SSimon Glass {
115e721b882SJoe Hershberger 	struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
116e721b882SJoe Hershberger 	const int n_ents = ll_entry_count(struct unit_test, dm_test);
117e721b882SJoe Hershberger 	struct unit_test_state *uts = &global_dm_test_state;
118e721b882SJoe Hershberger 	struct unit_test *test;
119c02790ceSSimon Glass 	int run_count;
1202e7d35d2SSimon Glass 
121*c166c47bSSimon Glass 	uts->priv = &_global_priv_dm_test_state;
12226e1beccSStephen Warren 	uts->fail_count = 0;
12326e1beccSStephen Warren 
1242e7d35d2SSimon Glass 	/*
1252e7d35d2SSimon Glass 	 * If we have no device tree, or it only has a root node, then these
1262e7d35d2SSimon Glass 	 * tests clearly aren't going to work...
1272e7d35d2SSimon Glass 	 */
1282e7d35d2SSimon Glass 	if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
1292e7d35d2SSimon Glass 		puts("Please run with test device tree:\n"
130f64000c3SPrzemyslaw Marczak 		     "    ./u-boot -d arch/sandbox/dts/test.dtb\n");
1312e7d35d2SSimon Glass 		ut_assert(gd->fdt_blob);
1322e7d35d2SSimon Glass 	}
1332e7d35d2SSimon Glass 
13457f54d55SSimon Glass 	if (!test_name)
1352e7d35d2SSimon Glass 		printf("Running %d driver model tests\n", n_ents);
1362e7d35d2SSimon Glass 
137c02790ceSSimon Glass 	run_count = 0;
138*c166c47bSSimon Glass #ifdef CONFIG_OF_LIVE
139*c166c47bSSimon Glass 	uts->of_root = gd->of_root;
140*c166c47bSSimon Glass #endif
1412e7d35d2SSimon Glass 	for (test = tests; test < tests + n_ents; test++) {
142c02790ceSSimon Glass 		const char *name = test->name;
143c02790ceSSimon Glass 
144c02790ceSSimon Glass 		/* All tests have this prefix */
145c02790ceSSimon Glass 		if (!strncmp(name, "dm_test_", 8))
146c02790ceSSimon Glass 			name += 8;
147c02790ceSSimon Glass 		if (test_name && strcmp(test_name, name))
14857f54d55SSimon Glass 			continue;
149*c166c47bSSimon Glass 		ut_assertok(dm_do_test(uts, test, false));
150c02790ceSSimon Glass 		run_count++;
1512e7d35d2SSimon Glass 	}
1522e7d35d2SSimon Glass 
153c02790ceSSimon Glass 	if (test_name && !run_count)
154c02790ceSSimon Glass 		printf("Test '%s' not found\n", test_name);
155c02790ceSSimon Glass 	else
156e721b882SJoe Hershberger 		printf("Failures: %d\n", uts->fail_count);
1572e7d35d2SSimon Glass 
158b6227d39SJoe Hershberger 	gd->dm_root = NULL;
15919c8205eSSimon Glass 	ut_assertok(dm_init(false));
160b6227d39SJoe Hershberger 	dm_scan_platdata(false);
161b6227d39SJoe Hershberger 	dm_scan_fdt(gd->fdt_blob, false);
162b6227d39SJoe Hershberger 
1637cccc66aSJoe Hershberger 	return uts->fail_count ? CMD_RET_FAILURE : 0;
1642e7d35d2SSimon Glass }
16540441e0bSJoe Hershberger 
16640441e0bSJoe Hershberger int do_ut_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
16740441e0bSJoe Hershberger {
16840441e0bSJoe Hershberger 	const char *test_name = NULL;
16940441e0bSJoe Hershberger 
17040441e0bSJoe Hershberger 	if (argc > 1)
17140441e0bSJoe Hershberger 		test_name = argv[1];
17240441e0bSJoe Hershberger 
17340441e0bSJoe Hershberger 	return dm_test_main(test_name);
17440441e0bSJoe Hershberger }
175