1 // SPDX-License-Identifier: GPL-2.0+
6 #include <dm/of_extra.h>
11 static int dm_test_ofnode_compatible(struct unit_test_state *uts)
13 ofnode root_node = ofnode_path("/");
15 ut_assert(ofnode_valid(root_node));
16 ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
20 DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
22 static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
24 /* test invalid phandle */
25 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
26 ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
28 /* test first valid phandle */
29 ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
31 /* test unknown phandle */
32 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
36 DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
38 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
40 const char propname[] = "compatible";
41 const char propval[] = "denx,u-boot-fdt-test";
43 ofnode node = ofnode_null();
45 /* Find first matching node, there should be at least one */
46 node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
47 ut_assert(ofnode_valid(node));
48 str = ofnode_read_string(node, propname);
49 ut_assert(str && !strcmp(str, propval));
51 /* Find the rest of the matching nodes */
53 node = ofnode_by_prop_value(node, propname, propval,
55 if (!ofnode_valid(node))
57 str = ofnode_read_string(node, propname);
58 ut_assert(str && !strcmp(str, propval));
63 DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
65 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
67 struct fmap_entry entry;
70 node = ofnode_path("/cros-ec/flash");
71 ut_assert(ofnode_valid(node));
72 ut_assertok(ofnode_read_fmap_entry(node, &entry));
73 ut_asserteq(0x08000000, entry.offset);
74 ut_asserteq(0x20000, entry.length);
78 DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
80 static int dm_test_ofnode_read(struct unit_test_state *uts)
86 node = ofnode_path("/a-test");
87 ut_assert(ofnode_valid(node));
89 val = ofnode_read_prop(node, "int-value", &size);
90 ut_assertnonnull(val);
92 ut_asserteq(1234, fdt32_to_cpu(val[0]));
94 val = ofnode_read_prop(node, "missing", &size);
96 ut_asserteq(-FDT_ERR_NOTFOUND, size);
98 /* Check it works without a size parameter */
99 val = ofnode_read_prop(node, "missing", NULL);
104 DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
106 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
113 str = ofnode_read_chosen_string("setting");
114 ut_assertnonnull(str);
115 ut_asserteq_str("sunrise ohoka", str);
116 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
118 node = ofnode_get_chosen_node("other-node");
119 ut_assert(ofnode_valid(node));
120 ut_asserteq_str("c-test@5", ofnode_get_name(node));
122 node = ofnode_get_chosen_node("setting");
123 ut_assert(!ofnode_valid(node));
125 val = ofnode_read_chosen_prop("int-values", &size);
126 ut_assertnonnull(val);
127 ut_asserteq(8, size);
128 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
129 ut_asserteq(72993, fdt32_to_cpu(val[1]));
133 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
135 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
137 ofnode node, child_node;
140 node = ofnode_path("/i-test");
141 ut_assert(ofnode_valid(node));
143 val = ofnode_get_child_count(node);
146 child_node = ofnode_first_subnode(node);
147 ut_assert(ofnode_valid(child_node));
148 val = ofnode_get_child_count(child_node);
153 DM_TEST(dm_test_ofnode_get_child_count,
154 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);