1 // SPDX-License-Identifier: GPL-2.0+
7 #include <dm/of_extra.h>
12 static int dm_test_ofnode_compatible(struct unit_test_state *uts)
14 ofnode root_node = ofnode_path("/");
16 ut_assert(ofnode_valid(root_node));
17 ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
21 DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
23 static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
25 /* test invalid phandle */
26 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
27 ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
29 /* test first valid phandle */
30 ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
32 /* test unknown phandle */
33 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
37 DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
39 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
41 const char propname[] = "compatible";
42 const char propval[] = "denx,u-boot-fdt-test";
44 ofnode node = ofnode_null();
46 /* Find first matching node, there should be at least one */
47 node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
48 ut_assert(ofnode_valid(node));
49 str = ofnode_read_string(node, propname);
50 ut_assert(str && !strcmp(str, propval));
52 /* Find the rest of the matching nodes */
54 node = ofnode_by_prop_value(node, propname, propval,
56 if (!ofnode_valid(node))
58 str = ofnode_read_string(node, propname);
59 ut_assert(str && !strcmp(str, propval));
64 DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
66 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
68 struct fmap_entry entry;
71 node = ofnode_path("/cros-ec/flash");
72 ut_assert(ofnode_valid(node));
73 ut_assertok(ofnode_read_fmap_entry(node, &entry));
74 ut_asserteq(0x08000000, entry.offset);
75 ut_asserteq(0x20000, entry.length);
79 DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
81 static int dm_test_ofnode_read(struct unit_test_state *uts)
87 node = ofnode_path("/a-test");
88 ut_assert(ofnode_valid(node));
90 val = ofnode_read_prop(node, "int-value", &size);
91 ut_assertnonnull(val);
93 ut_asserteq(1234, fdt32_to_cpu(val[0]));
95 val = ofnode_read_prop(node, "missing", &size);
97 ut_asserteq(-FDT_ERR_NOTFOUND, size);
99 /* Check it works without a size parameter */
100 val = ofnode_read_prop(node, "missing", NULL);
105 DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
107 static int dm_test_ofnode_phandle(struct unit_test_state *uts)
109 struct ofnode_phandle_args args;
112 const char prop[] = "test-gpios";
113 const char cell[] = "#gpio-cells";
114 const char prop2[] = "phandle-value";
116 node = ofnode_path("/a-test");
117 ut_assert(ofnode_valid(node));
119 /* Test ofnode_count_phandle_with_args with cell name */
120 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
121 ut_asserteq(-ENOENT, ret);
122 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
123 ut_asserteq(-EINVAL, ret);
124 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
127 /* Test ofnode_parse_phandle_with_args with cell name */
128 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
130 ut_asserteq(-ENOENT, ret);
131 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
133 ut_asserteq(-EINVAL, ret);
134 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
136 ut_asserteq(1, args.args_count);
137 ut_asserteq(1, args.args[0]);
138 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
140 ut_asserteq(1, args.args_count);
141 ut_asserteq(4, args.args[0]);
142 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
144 ut_asserteq(5, args.args_count);
145 ut_asserteq(5, args.args[0]);
146 ut_asserteq(1, args.args[4]);
147 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
148 ut_asserteq(-ENOENT, ret);
149 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
151 ut_asserteq(1, args.args_count);
152 ut_asserteq(12, args.args[0]);
153 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
154 ut_asserteq(-ENOENT, ret);
156 /* Test ofnode_count_phandle_with_args with cell count */
157 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
158 ut_asserteq(-ENOENT, ret);
159 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
162 /* Test ofnode_parse_phandle_with_args with cell count */
163 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
165 ut_asserteq(1, ofnode_valid(args.node));
166 ut_asserteq(1, args.args_count);
167 ut_asserteq(10, args.args[0]);
168 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
169 ut_asserteq(-EINVAL, ret);
170 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
172 ut_asserteq(1, ofnode_valid(args.node));
173 ut_asserteq(1, args.args_count);
174 ut_asserteq(30, args.args[0]);
175 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
176 ut_asserteq(-ENOENT, ret);
180 DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
182 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
189 str = ofnode_read_chosen_string("setting");
190 ut_assertnonnull(str);
191 ut_asserteq_str("sunrise ohoka", str);
192 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
194 node = ofnode_get_chosen_node("other-node");
195 ut_assert(ofnode_valid(node));
196 ut_asserteq_str("c-test@5", ofnode_get_name(node));
198 node = ofnode_get_chosen_node("setting");
199 ut_assert(!ofnode_valid(node));
201 val = ofnode_read_chosen_prop("int-values", &size);
202 ut_assertnonnull(val);
203 ut_asserteq(8, size);
204 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
205 ut_asserteq(72993, fdt32_to_cpu(val[1]));
209 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
211 static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
217 node = ofnode_get_aliases_node("ethernet3");
218 ut_assert(ofnode_valid(node));
219 ut_asserteq_str("sbe5", ofnode_get_name(node));
221 node = ofnode_get_aliases_node("unknown");
222 ut_assert(!ofnode_valid(node));
224 val = ofnode_read_aliases_prop("spi0", &size);
225 ut_assertnonnull(val);
226 ut_asserteq(7, size);
227 ut_asserteq_str("/spi@0", (const char *)val);
231 DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
233 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
235 ofnode node, child_node;
238 node = ofnode_path("/i-test");
239 ut_assert(ofnode_valid(node));
241 val = ofnode_get_child_count(node);
244 child_node = ofnode_first_subnode(node);
245 ut_assert(ofnode_valid(child_node));
246 val = ofnode_get_child_count(child_node);
251 DM_TEST(dm_test_ofnode_get_child_count,
252 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
254 static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
256 ofnode root_node = ofnode_path("/");
257 ofnode node = ofnode_path("/usb@0");
259 ut_assert(ofnode_is_enabled(root_node));
260 ut_assert(!ofnode_is_enabled(node));
264 DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
266 static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
272 node = ofnode_path("/translation-test@8000");
273 ut_assert(ofnode_valid(node));
274 addr = ofnode_get_addr(node);
275 size = ofnode_get_size(node);
276 ut_asserteq(0x8000, addr);
277 ut_asserteq(0x4000, size);
279 node = ofnode_path("/translation-test@8000/dev@1,100");
280 ut_assert(ofnode_valid(node));
281 addr = ofnode_get_addr(node);
282 size = ofnode_get_size(node);
283 ut_asserteq(0x9000, addr);
284 ut_asserteq(0x1000, size);
286 node = ofnode_path("/emul-mux-controller");
287 ut_assert(ofnode_valid(node));
288 addr = ofnode_get_addr(node);
289 size = ofnode_get_size(node);
290 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
291 ut_asserteq(FDT_SIZE_T_NONE, size);
293 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
294 ut_assert(ofnode_valid(node));
295 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
296 ut_asserteq_64(0x42, addr);
300 DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
302 static int dm_test_ofnode_get_path(struct unit_test_state *uts)
304 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
309 node = ofnode_path(path);
310 ut_assert(ofnode_valid(node));
312 res = ofnode_get_path(node, buf, 64);
314 ut_asserteq_str(path, buf);
316 res = ofnode_get_path(node, buf, 32);
317 ut_asserteq(-ENOSPC, res);
321 DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
323 static int dm_test_ofnode_conf(struct unit_test_state *uts)
325 ut_assert(!ofnode_conf_read_bool("missing"));
326 ut_assert(ofnode_conf_read_bool("testing-bool"));
328 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
329 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
331 ut_assertnull(ofnode_conf_read_str("missing"));
332 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
336 DM_TEST(dm_test_ofnode_conf, 0);
338 static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
340 const char compatible[] = "denx,u-boot-fdt-test";
344 ofnode_for_each_compatible_node(node, compatible) {
345 ut_assert(ofnode_device_is_compatible(node, compatible));
349 /* There should be at least one matching node */
354 DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
356 static int dm_test_ofnode_string(struct unit_test_state *uts)
362 node = ofnode_path("/a-test");
363 ut_assert(ofnode_valid(node));
366 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
367 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
368 ut_asserteq_str("test string", out);
369 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
371 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
372 ut_asserteq_str("test string", val[0]);
373 ut_assertnull(val[1]);
376 /* list of strings */
377 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
378 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
380 ut_asserteq_str("mux0", out);
381 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
383 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
385 ut_asserteq_str("mux0", val[0]);
386 ut_asserteq_str("mux1", val[1]);
387 ut_asserteq_str("mux2", val[2]);
388 ut_asserteq_str("mux3", val[3]);
389 ut_asserteq_str("mux4", val[4]);
390 ut_assertnull(val[5]);
393 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
395 ut_asserteq_str("mux4", out);
396 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
401 DM_TEST(dm_test_ofnode_string, 0);
403 static int dm_test_ofnode_string_err(struct unit_test_state *uts)
410 * Test error codes only on livetree, as they are different with
413 node = ofnode_path("/a-test");
414 ut_assert(ofnode_valid(node));
416 /* non-existent property */
417 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
418 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
420 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
423 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
424 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
426 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
429 /* badly formatted string list */
430 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
431 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
433 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
436 /* out of range / not found */
437 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
439 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
442 /* negative value for index is not allowed, so don't test for that */
444 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
445 "mux-control-names", 5,
450 DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
452 static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
454 ofnode eth_node, phy_node;
455 phy_interface_t mode;
458 eth_node = ofnode_path("/phy-test-eth");
459 ut_assert(ofnode_valid(eth_node));
461 mode = ofnode_read_phy_mode(eth_node);
462 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
464 phy_node = ofnode_get_phy_node(eth_node);
465 ut_assert(ofnode_valid(phy_node));
467 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
468 ut_asserteq_64(0x1, reg);
472 DM_TEST(dm_test_ofnode_get_phy, 0);
475 * make_ofnode_fdt() - Create an FDT for testing with ofnode
477 * The size is set to the minimum needed
480 * @fdt: Place to write FDT
481 * @size: Maximum size of space for fdt
483 static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
485 ut_assertok(fdt_create(fdt, size));
486 ut_assertok(fdt_finish_reservemap(fdt));
487 ut_assert(fdt_begin_node(fdt, "") >= 0);
489 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
490 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
491 ut_assertok(fdt_end_node(fdt));
493 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
494 ut_assertok(fdt_end_node(fdt));
496 ut_assertok(fdt_end_node(fdt));
497 ut_assertok(fdt_finish(fdt));
502 static int dm_test_ofnode_root(struct unit_test_state *uts)
504 struct device_node *root = NULL;
509 /* Check that aliases work on the control FDT */
510 node = ofnode_get_aliases_node("ethernet3");
511 ut_assert(ofnode_valid(node));
512 ut_asserteq_str("sbe5", ofnode_get_name(node));
514 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
515 if (of_live_active()) {
516 ut_assertok(unflatten_device_tree(fdt, &root));
522 /* Make sure they don't work on this new tree */
523 node = ofnode_path_root(tree, "mmc0");
524 ut_assert(!ofnode_valid(node));
526 /* It should appear in the new tree */
527 node = ofnode_path_root(tree, "/new-mmc");
528 ut_assert(ofnode_valid(node));
530 /* ...and not in the control FDT */
531 node = ofnode_path_root(oftree_default(), "/new-mmc");
532 ut_assert(!ofnode_valid(node));
538 DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);