xref: /rk3399_rockchip-uboot/test/dm/test-main.c (revision 3884c98c32cd5fb5b5b42185d5d0575659434bbf)
1 /*
2  * Copyright (c) 2013 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <command.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <malloc.h>
12 #include <asm/state.h>
13 #include <dm/test.h>
14 #include <dm/root.h>
15 #include <dm/uclass-internal.h>
16 #include <test/ut.h>
17 
18 DECLARE_GLOBAL_DATA_PTR;
19 
20 struct unit_test_state global_dm_test_state;
21 static struct dm_test_state _global_priv_dm_test_state;
22 
23 /* Get ready for testing */
24 static int dm_test_init(struct unit_test_state *uts)
25 {
26 	struct dm_test_state *dms = uts->priv;
27 
28 	memset(dms, '\0', sizeof(*dms));
29 	gd->dm_root = NULL;
30 	memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
31 
32 	ut_assertok(dm_init());
33 	dms->root = dm_root();
34 
35 	return 0;
36 }
37 
38 /* Ensure all the test devices are probed */
39 static int do_autoprobe(struct unit_test_state *uts)
40 {
41 	struct udevice *dev;
42 	int ret;
43 
44 	/* Scanning the uclass is enough to probe all the devices */
45 	for (ret = uclass_first_device(UCLASS_TEST, &dev);
46 	     dev;
47 	     ret = uclass_next_device(&dev))
48 		;
49 
50 	return ret;
51 }
52 
53 static int dm_test_destroy(struct unit_test_state *uts)
54 {
55 	int id;
56 
57 	for (id = 0; id < UCLASS_COUNT; id++) {
58 		struct uclass *uc;
59 
60 		/*
61 		 * If the uclass doesn't exist we don't want to create it. So
62 		 * check that here before we call uclass_find_device()/
63 		 */
64 		uc = uclass_find(id);
65 		if (!uc)
66 			continue;
67 		ut_assertok(uclass_destroy(uc));
68 	}
69 
70 	return 0;
71 }
72 
73 static int dm_test_main(const char *test_name)
74 {
75 	struct unit_test *tests = ll_entry_start(struct unit_test, dm_test);
76 	const int n_ents = ll_entry_count(struct unit_test, dm_test);
77 	struct unit_test_state *uts = &global_dm_test_state;
78 	uts->priv = &_global_priv_dm_test_state;
79 	struct unit_test *test;
80 	int run_count;
81 
82 	/*
83 	 * If we have no device tree, or it only has a root node, then these
84 	 * tests clearly aren't going to work...
85 	 */
86 	if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
87 		puts("Please run with test device tree:\n"
88 		     "    ./u-boot -d arch/sandbox/dts/test.dtb\n");
89 		ut_assert(gd->fdt_blob);
90 	}
91 
92 	if (!test_name)
93 		printf("Running %d driver model tests\n", n_ents);
94 
95 	run_count = 0;
96 	for (test = tests; test < tests + n_ents; test++) {
97 		const char *name = test->name;
98 
99 		/* All tests have this prefix */
100 		if (!strncmp(name, "dm_test_", 8))
101 			name += 8;
102 		if (test_name && strcmp(test_name, name))
103 			continue;
104 		printf("Test: %s\n", test->name);
105 		run_count++;
106 		ut_assertok(dm_test_init(uts));
107 
108 		uts->start = mallinfo();
109 		if (test->flags & DM_TESTF_SCAN_PDATA)
110 			ut_assertok(dm_scan_platdata(false));
111 		if (test->flags & DM_TESTF_PROBE_TEST)
112 			ut_assertok(do_autoprobe(uts));
113 		if (test->flags & DM_TESTF_SCAN_FDT)
114 			ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
115 
116 		test->func(uts);
117 		state_set_skip_delays(false);
118 
119 		ut_assertok(dm_test_destroy(uts));
120 	}
121 
122 	if (test_name && !run_count)
123 		printf("Test '%s' not found\n", test_name);
124 	else
125 		printf("Failures: %d\n", uts->fail_count);
126 
127 	gd->dm_root = NULL;
128 	ut_assertok(dm_init());
129 	dm_scan_platdata(false);
130 	dm_scan_fdt(gd->fdt_blob, false);
131 
132 	return uts->fail_count ? CMD_RET_FAILURE : 0;
133 }
134 
135 int do_ut_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
136 {
137 	const char *test_name = NULL;
138 
139 	if (argc > 1)
140 		test_name = argv[1];
141 
142 	return dm_test_main(test_name);
143 }
144