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_phandle(struct unit_test_state *uts)
108 struct ofnode_phandle_args args;
111 const char prop[] = "test-gpios";
112 const char cell[] = "#gpio-cells";
113 const char prop2[] = "phandle-value";
115 node = ofnode_path("/a-test");
116 ut_assert(ofnode_valid(node));
118 /* Test ofnode_count_phandle_with_args with cell name */
119 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
120 ut_asserteq(-ENOENT, ret);
121 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
122 ut_asserteq(-EINVAL, ret);
123 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
126 /* Test ofnode_parse_phandle_with_args with cell name */
127 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
129 ut_asserteq(-ENOENT, ret);
130 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
132 ut_asserteq(-EINVAL, ret);
133 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
135 ut_asserteq(1, args.args_count);
136 ut_asserteq(1, args.args[0]);
137 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
139 ut_asserteq(1, args.args_count);
140 ut_asserteq(4, args.args[0]);
141 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
143 ut_asserteq(5, args.args_count);
144 ut_asserteq(5, args.args[0]);
145 ut_asserteq(1, args.args[4]);
146 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
147 ut_asserteq(-ENOENT, ret);
148 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
150 ut_asserteq(1, args.args_count);
151 ut_asserteq(12, args.args[0]);
152 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
153 ut_asserteq(-ENOENT, ret);
155 /* Test ofnode_count_phandle_with_args with cell count */
156 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
157 ut_asserteq(-ENOENT, ret);
158 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
161 /* Test ofnode_parse_phandle_with_args with cell count */
162 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
164 ut_asserteq(1, ofnode_valid(args.node));
165 ut_asserteq(1, args.args_count);
166 ut_asserteq(10, args.args[0]);
167 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
168 ut_asserteq(-EINVAL, ret);
169 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
171 ut_asserteq(1, ofnode_valid(args.node));
172 ut_asserteq(1, args.args_count);
173 ut_asserteq(30, args.args[0]);
174 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
175 ut_asserteq(-ENOENT, ret);
179 DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
181 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
188 str = ofnode_read_chosen_string("setting");
189 ut_assertnonnull(str);
190 ut_asserteq_str("sunrise ohoka", str);
191 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
193 node = ofnode_get_chosen_node("other-node");
194 ut_assert(ofnode_valid(node));
195 ut_asserteq_str("c-test@5", ofnode_get_name(node));
197 node = ofnode_get_chosen_node("setting");
198 ut_assert(!ofnode_valid(node));
200 val = ofnode_read_chosen_prop("int-values", &size);
201 ut_assertnonnull(val);
202 ut_asserteq(8, size);
203 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
204 ut_asserteq(72993, fdt32_to_cpu(val[1]));
208 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
210 static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
216 node = ofnode_get_aliases_node("ethernet3");
217 ut_assert(ofnode_valid(node));
218 ut_asserteq_str("sbe5", ofnode_get_name(node));
220 node = ofnode_get_aliases_node("unknown");
221 ut_assert(!ofnode_valid(node));
223 val = ofnode_read_aliases_prop("spi0", &size);
224 ut_assertnonnull(val);
225 ut_asserteq(7, size);
226 ut_asserteq_str("/spi@0", (const char *)val);
230 DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
232 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
234 ofnode node, child_node;
237 node = ofnode_path("/i-test");
238 ut_assert(ofnode_valid(node));
240 val = ofnode_get_child_count(node);
243 child_node = ofnode_first_subnode(node);
244 ut_assert(ofnode_valid(child_node));
245 val = ofnode_get_child_count(child_node);
250 DM_TEST(dm_test_ofnode_get_child_count,
251 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
253 static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
255 ofnode root_node = ofnode_path("/");
256 ofnode node = ofnode_path("/usb@0");
258 ut_assert(ofnode_is_enabled(root_node));
259 ut_assert(!ofnode_is_enabled(node));
263 DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
265 static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
271 node = ofnode_path("/translation-test@8000");
272 ut_assert(ofnode_valid(node));
273 addr = ofnode_get_addr(node);
274 size = ofnode_get_size(node);
275 ut_asserteq(0x8000, addr);
276 ut_asserteq(0x4000, size);
278 node = ofnode_path("/translation-test@8000/dev@1,100");
279 ut_assert(ofnode_valid(node));
280 addr = ofnode_get_addr(node);
281 size = ofnode_get_size(node);
282 ut_asserteq(0x9000, addr);
283 ut_asserteq(0x1000, size);
285 node = ofnode_path("/emul-mux-controller");
286 ut_assert(ofnode_valid(node));
287 addr = ofnode_get_addr(node);
288 size = ofnode_get_size(node);
289 ut_asserteq(FDT_ADDR_T_NONE, addr);
290 ut_asserteq(FDT_SIZE_T_NONE, size);
292 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
293 ut_assert(ofnode_valid(node));
294 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
295 ut_asserteq_64(0x42, addr);
299 DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
301 static int dm_test_ofnode_get_path(struct unit_test_state *uts)
303 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
308 node = ofnode_path(path);
309 ut_assert(ofnode_valid(node));
311 res = ofnode_get_path(node, buf, 64);
313 ut_asserteq_str(path, buf);
315 res = ofnode_get_path(node, buf, 32);
316 ut_asserteq(-ENOSPC, res);
320 DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
322 static int dm_test_ofnode_conf(struct unit_test_state *uts)
324 ut_assert(!ofnode_conf_read_bool("missing"));
325 ut_assert(ofnode_conf_read_bool("testing-bool"));
327 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
328 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
330 ut_assertnull(ofnode_conf_read_str("missing"));
331 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
335 DM_TEST(dm_test_ofnode_conf, 0);