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, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
22 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
24 const char propname[] = "compatible";
25 const char propval[] = "denx,u-boot-fdt-test";
27 ofnode node = ofnode_null();
29 /* Find first matching node, there should be at least one */
30 node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
31 ut_assert(ofnode_valid(node));
32 str = ofnode_read_string(node, propname);
33 ut_assert(str && !strcmp(str, propval));
35 /* Find the rest of the matching nodes */
37 node = ofnode_by_prop_value(node, propname, propval,
39 if (!ofnode_valid(node))
41 str = ofnode_read_string(node, propname);
42 ut_assert(str && !strcmp(str, propval));
47 DM_TEST(dm_test_ofnode_by_prop_value, DM_TESTF_SCAN_FDT);
49 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
51 struct fmap_entry entry;
54 node = ofnode_path("/cros-ec/flash");
55 ut_assert(ofnode_valid(node));
56 ut_assertok(ofnode_read_fmap_entry(node, &entry));
57 ut_asserteq(0x08000000, entry.offset);
58 ut_asserteq(0x20000, entry.length);
62 DM_TEST(dm_test_ofnode_fmap, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
64 static int dm_test_ofnode_read(struct unit_test_state *uts)
70 node = ofnode_path("/a-test");
71 ut_assert(ofnode_valid(node));
73 val = ofnode_read_prop(node, "int-value", &size);
74 ut_assertnonnull(val);
76 ut_asserteq(1234, fdt32_to_cpu(val[0]));
78 val = ofnode_read_prop(node, "missing", &size);
80 ut_asserteq(-FDT_ERR_NOTFOUND, size);
82 /* Check it works without a size parameter */
83 val = ofnode_read_prop(node, "missing", NULL);
88 DM_TEST(dm_test_ofnode_read, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
90 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
97 str = ofnode_read_chosen_string("setting");
98 ut_assertnonnull(str);
99 ut_asserteq_str("sunrise ohoka", str);
100 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
102 node = ofnode_get_chosen_node("other-node");
103 ut_assert(ofnode_valid(node));
104 ut_asserteq_str("c-test@5", ofnode_get_name(node));
106 node = ofnode_get_chosen_node("setting");
107 ut_assert(!ofnode_valid(node));
109 val = ofnode_read_chosen_prop("int-values", &size);
110 ut_assertnonnull(val);
111 ut_asserteq(8, size);
112 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
113 ut_asserteq(72993, fdt32_to_cpu(val[1]));
117 DM_TEST(dm_test_ofnode_read_chosen, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
119 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
121 ofnode node, child_node;
124 node = ofnode_path("/i-test");
125 ut_assert(ofnode_valid(node));
127 val = ofnode_get_child_count(node);
130 child_node = ofnode_first_subnode(node);
131 ut_assert(ofnode_valid(child_node));
132 val = ofnode_get_child_count(child_node);
137 DM_TEST(dm_test_ofnode_get_child_count,
138 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);