xref: /openbmc/u-boot/test/dm/test-main.c (revision ed36323f)
1 /*
2  * Copyright (c) 2013 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <dm/test.h>
11 #include <dm/root.h>
12 #include <dm/uclass-internal.h>
13 #include <dm/ut.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 struct dm_test_state global_test_state;
18 
19 /* Get ready for testing */
20 static int dm_test_init(struct dm_test_state *dms)
21 {
22 	memset(dms, '\0', sizeof(*dms));
23 	gd->dm_root = NULL;
24 	memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count));
25 
26 	ut_assertok(dm_init());
27 	dms->root = dm_root();
28 
29 	return 0;
30 }
31 
32 /* Ensure all the test devices are probed */
33 static int do_autoprobe(struct dm_test_state *dms)
34 {
35 	struct udevice *dev;
36 	int ret;
37 
38 	/* Scanning the uclass is enough to probe all the devices */
39 	for (ret = uclass_first_device(UCLASS_TEST, &dev);
40 	     dev;
41 	     ret = uclass_next_device(&dev))
42 		;
43 
44 	return ret;
45 }
46 
47 static int dm_test_destroy(struct dm_test_state *dms)
48 {
49 	int id;
50 
51 	for (id = 0; id < UCLASS_COUNT; id++) {
52 		struct uclass *uc;
53 
54 		/*
55 		 * If the uclass doesn't exist we don't want to create it. So
56 		 * check that here before we call uclass_find_device()/
57 		 */
58 		uc = uclass_find(id);
59 		if (!uc)
60 			continue;
61 		ut_assertok(uclass_destroy(uc));
62 	}
63 
64 	return 0;
65 }
66 
67 int dm_test_main(void)
68 {
69 	struct dm_test *tests = ll_entry_start(struct dm_test, dm_test);
70 	const int n_ents = ll_entry_count(struct dm_test, dm_test);
71 	struct dm_test_state *dms = &global_test_state;
72 	struct dm_test *test;
73 
74 	/*
75 	 * If we have no device tree, or it only has a root node, then these
76 	 * tests clearly aren't going to work...
77 	 */
78 	if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) {
79 		puts("Please run with test device tree:\n"
80 		     "     dtc -I dts -O dtb test/dm/test.dts  -o test/dm/test.dtb\n"
81 		     "    ./u-boot -d test/dm/test.dtb\n");
82 		ut_assert(gd->fdt_blob);
83 	}
84 
85 	printf("Running %d driver model tests\n", n_ents);
86 
87 	for (test = tests; test < tests + n_ents; test++) {
88 		printf("Test: %s\n", test->name);
89 		ut_assertok(dm_test_init(dms));
90 
91 		if (test->flags & DM_TESTF_SCAN_PDATA)
92 			ut_assertok(dm_scan_platdata(false));
93 		if (test->flags & DM_TESTF_PROBE_TEST)
94 			ut_assertok(do_autoprobe(dms));
95 		if (test->flags & DM_TESTF_SCAN_FDT)
96 			ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
97 
98 		if (test->func(dms))
99 			break;
100 
101 		ut_assertok(dm_test_destroy(dms));
102 	}
103 
104 	printf("Failures: %d\n", dms->fail_count);
105 
106 	return 0;
107 }
108