1 // SPDX-License-Identifier: GPL-2.0+
7 #include <dm/device-internal.h>
9 #include <dm/of_extra.h>
12 #include <dm/uclass-internal.h>
13 #include <test/test.h>
16 static int dm_test_ofnode_compatible(struct unit_test_state *uts)
18 ofnode root_node = ofnode_path("/");
20 ut_assert(ofnode_valid(root_node));
21 ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
25 DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
27 static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
29 /* test invalid phandle */
30 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
31 ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
33 /* test first valid phandle */
34 ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
36 /* test unknown phandle */
37 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
41 DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
43 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
45 const char propname[] = "compatible";
46 const char propval[] = "denx,u-boot-fdt-test";
48 ofnode node = ofnode_null();
50 /* Find first matching node, there should be at least one */
51 node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
52 ut_assert(ofnode_valid(node));
53 str = ofnode_read_string(node, propname);
54 ut_assert(str && !strcmp(str, propval));
56 /* Find the rest of the matching nodes */
58 node = ofnode_by_prop_value(node, propname, propval,
60 if (!ofnode_valid(node))
62 str = ofnode_read_string(node, propname);
63 ut_assert(str && !strcmp(str, propval));
68 DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
70 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
72 struct fmap_entry entry;
75 node = ofnode_path("/cros-ec/flash");
76 ut_assert(ofnode_valid(node));
77 ut_assertok(ofnode_read_fmap_entry(node, &entry));
78 ut_asserteq(0x08000000, entry.offset);
79 ut_asserteq(0x20000, entry.length);
83 DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
85 static int dm_test_ofnode_read(struct unit_test_state *uts)
91 node = ofnode_path("/a-test");
92 ut_assert(ofnode_valid(node));
94 val = ofnode_read_prop(node, "int-value", &size);
95 ut_assertnonnull(val);
97 ut_asserteq(1234, fdt32_to_cpu(val[0]));
99 val = ofnode_read_prop(node, "missing", &size);
101 ut_asserteq(-FDT_ERR_NOTFOUND, size);
103 /* Check it works without a size parameter */
104 val = ofnode_read_prop(node, "missing", NULL);
109 DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
111 static int dm_test_ofnode_phandle(struct unit_test_state *uts)
113 struct ofnode_phandle_args args;
116 const char prop[] = "test-gpios";
117 const char cell[] = "#gpio-cells";
118 const char prop2[] = "phandle-value";
120 node = ofnode_path("/a-test");
121 ut_assert(ofnode_valid(node));
123 /* Test ofnode_count_phandle_with_args with cell name */
124 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
125 ut_asserteq(-ENOENT, ret);
126 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
127 ut_asserteq(-EINVAL, ret);
128 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
131 /* Test ofnode_parse_phandle_with_args with cell name */
132 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
134 ut_asserteq(-ENOENT, ret);
135 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
137 ut_asserteq(-EINVAL, ret);
138 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
140 ut_asserteq(1, args.args_count);
141 ut_asserteq(1, args.args[0]);
142 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
144 ut_asserteq(1, args.args_count);
145 ut_asserteq(4, args.args[0]);
146 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
148 ut_asserteq(5, args.args_count);
149 ut_asserteq(5, args.args[0]);
150 ut_asserteq(1, args.args[4]);
151 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
152 ut_asserteq(-ENOENT, ret);
153 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
155 ut_asserteq(1, args.args_count);
156 ut_asserteq(12, args.args[0]);
157 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
158 ut_asserteq(-ENOENT, ret);
160 /* Test ofnode_count_phandle_with_args with cell count */
161 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
162 ut_asserteq(-ENOENT, ret);
163 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
166 /* Test ofnode_parse_phandle_with_args with cell count */
167 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
169 ut_asserteq(1, ofnode_valid(args.node));
170 ut_asserteq(1, args.args_count);
171 ut_asserteq(10, args.args[0]);
172 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
173 ut_asserteq(-EINVAL, ret);
174 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
176 ut_asserteq(1, ofnode_valid(args.node));
177 ut_asserteq(1, args.args_count);
178 ut_asserteq(30, args.args[0]);
179 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
180 ut_asserteq(-ENOENT, ret);
184 DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
186 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
193 str = ofnode_read_chosen_string("setting");
194 ut_assertnonnull(str);
195 ut_asserteq_str("sunrise ohoka", str);
196 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
198 node = ofnode_get_chosen_node("other-node");
199 ut_assert(ofnode_valid(node));
200 ut_asserteq_str("c-test@5", ofnode_get_name(node));
202 node = ofnode_get_chosen_node("setting");
203 ut_assert(!ofnode_valid(node));
205 val = ofnode_read_chosen_prop("int-values", &size);
206 ut_assertnonnull(val);
207 ut_asserteq(8, size);
208 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
209 ut_asserteq(72993, fdt32_to_cpu(val[1]));
213 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
215 static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
221 node = ofnode_get_aliases_node("ethernet3");
222 ut_assert(ofnode_valid(node));
223 ut_asserteq_str("sbe5", ofnode_get_name(node));
225 node = ofnode_get_aliases_node("unknown");
226 ut_assert(!ofnode_valid(node));
228 val = ofnode_read_aliases_prop("spi0", &size);
229 ut_assertnonnull(val);
230 ut_asserteq(7, size);
231 ut_asserteq_str("/spi@0", (const char *)val);
235 DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
237 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
239 ofnode node, child_node;
242 node = ofnode_path("/i-test");
243 ut_assert(ofnode_valid(node));
245 val = ofnode_get_child_count(node);
248 child_node = ofnode_first_subnode(node);
249 ut_assert(ofnode_valid(child_node));
250 val = ofnode_get_child_count(child_node);
255 DM_TEST(dm_test_ofnode_get_child_count,
256 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
258 static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
260 ofnode root_node = ofnode_path("/");
261 ofnode node = ofnode_path("/usb@0");
263 ut_assert(ofnode_is_enabled(root_node));
264 ut_assert(!ofnode_is_enabled(node));
268 DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
270 static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
276 node = ofnode_path("/translation-test@8000");
277 ut_assert(ofnode_valid(node));
278 addr = ofnode_get_addr(node);
279 size = ofnode_get_size(node);
280 ut_asserteq(0x8000, addr);
281 ut_asserteq(0x4000, size);
283 node = ofnode_path("/translation-test@8000/dev@1,100");
284 ut_assert(ofnode_valid(node));
285 addr = ofnode_get_addr(node);
286 size = ofnode_get_size(node);
287 ut_asserteq(0x9000, addr);
288 ut_asserteq(0x1000, size);
290 node = ofnode_path("/emul-mux-controller");
291 ut_assert(ofnode_valid(node));
292 addr = ofnode_get_addr(node);
293 size = ofnode_get_size(node);
294 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
295 ut_asserteq(FDT_SIZE_T_NONE, size);
297 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
298 ut_assert(ofnode_valid(node));
299 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
300 ut_asserteq_64(0x42, addr);
304 DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
306 static int dm_test_ofnode_get_path(struct unit_test_state *uts)
308 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
313 node = ofnode_path(path);
314 ut_assert(ofnode_valid(node));
316 res = ofnode_get_path(node, buf, 64);
318 ut_asserteq_str(path, buf);
320 res = ofnode_get_path(node, buf, 32);
321 ut_asserteq(-ENOSPC, res);
325 DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
327 static int dm_test_ofnode_conf(struct unit_test_state *uts)
329 ut_assert(!ofnode_conf_read_bool("missing"));
330 ut_assert(ofnode_conf_read_bool("testing-bool"));
332 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
333 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
335 ut_assertnull(ofnode_conf_read_str("missing"));
336 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
340 DM_TEST(dm_test_ofnode_conf, 0);
342 static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
344 const char compatible[] = "denx,u-boot-fdt-test";
348 ofnode_for_each_compatible_node(node, compatible) {
349 ut_assert(ofnode_device_is_compatible(node, compatible));
353 /* There should be at least one matching node */
358 DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
360 static int dm_test_ofnode_string(struct unit_test_state *uts)
366 node = ofnode_path("/a-test");
367 ut_assert(ofnode_valid(node));
370 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
371 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
372 ut_asserteq_str("test string", out);
373 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
375 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
376 ut_asserteq_str("test string", val[0]);
377 ut_assertnull(val[1]);
380 /* list of strings */
381 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
382 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
384 ut_asserteq_str("mux0", out);
385 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
387 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
389 ut_asserteq_str("mux0", val[0]);
390 ut_asserteq_str("mux1", val[1]);
391 ut_asserteq_str("mux2", val[2]);
392 ut_asserteq_str("mux3", val[3]);
393 ut_asserteq_str("mux4", val[4]);
394 ut_assertnull(val[5]);
397 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
399 ut_asserteq_str("mux4", out);
400 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
405 DM_TEST(dm_test_ofnode_string, 0);
407 static int dm_test_ofnode_string_err(struct unit_test_state *uts)
414 * Test error codes only on livetree, as they are different with
417 node = ofnode_path("/a-test");
418 ut_assert(ofnode_valid(node));
420 /* non-existent property */
421 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
422 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
424 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
427 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
428 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
430 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
433 /* badly formatted string list */
434 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
435 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
437 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
440 /* out of range / not found */
441 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
443 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
446 /* negative value for index is not allowed, so don't test for that */
448 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
449 "mux-control-names", 5,
454 DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
456 static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
458 ofnode eth_node, phy_node;
459 phy_interface_t mode;
462 eth_node = ofnode_path("/phy-test-eth");
463 ut_assert(ofnode_valid(eth_node));
465 mode = ofnode_read_phy_mode(eth_node);
466 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
468 phy_node = ofnode_get_phy_node(eth_node);
469 ut_assert(ofnode_valid(phy_node));
471 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
472 ut_asserteq_64(0x1, reg);
476 DM_TEST(dm_test_ofnode_get_phy, 0);
479 * make_ofnode_fdt() - Create an FDT for testing with ofnode
481 * The size is set to the minimum needed
484 * @fdt: Place to write FDT
485 * @size: Maximum size of space for fdt
487 static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
489 ut_assertok(fdt_create(fdt, size));
490 ut_assertok(fdt_finish_reservemap(fdt));
491 ut_assert(fdt_begin_node(fdt, "") >= 0);
493 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
494 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
495 ut_assertok(fdt_end_node(fdt));
497 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
498 ut_assertok(fdt_end_node(fdt));
500 ut_assertok(fdt_end_node(fdt));
501 ut_assertok(fdt_finish(fdt));
506 static int dm_test_ofnode_root(struct unit_test_state *uts)
508 struct device_node *root = NULL;
513 /* Check that aliases work on the control FDT */
514 node = ofnode_get_aliases_node("ethernet3");
515 ut_assert(ofnode_valid(node));
516 ut_asserteq_str("sbe5", ofnode_get_name(node));
518 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
519 if (of_live_active()) {
520 ut_assertok(unflatten_device_tree(fdt, &root));
526 /* Make sure they don't work on this new tree */
527 node = ofnode_path_root(tree, "mmc0");
528 ut_assert(!ofnode_valid(node));
530 /* It should appear in the new tree */
531 node = ofnode_path_root(tree, "/new-mmc");
532 ut_assert(ofnode_valid(node));
534 /* ...and not in the control FDT */
535 node = ofnode_path_root(oftree_default(), "/new-mmc");
536 ut_assert(!ofnode_valid(node));
542 DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
544 static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
549 /* Test enabling devices */
550 node = ofnode_path("/usb@2");
552 ut_assert(!ofnode_is_enabled(node));
553 ut_assertok(ofnode_set_enabled(node, true));
554 ut_asserteq(true, ofnode_is_enabled(node));
556 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
558 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
560 /* Test string property setting */
561 ut_assert(device_is_compatible(dev, "sandbox,usb"));
562 ofnode_write_string(node, "compatible", "gdsys,super-usb");
563 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
564 ofnode_write_string(node, "compatible", "sandbox,usb");
565 ut_assert(device_is_compatible(dev, "sandbox,usb"));
567 /* Test setting generic properties */
569 /* Non-existent in DTB */
570 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
571 /* reg = 0x42, size = 0x100 */
572 ut_assertok(ofnode_write_prop(node, "reg",
573 "\x00\x00\x00\x42\x00\x00\x01\x00", 8));
574 ut_asserteq(0x42, dev_read_addr(dev));
576 /* Test disabling devices */
577 device_remove(dev, DM_REMOVE_NORMAL);
580 ut_assert(ofnode_is_enabled(node));
581 ut_assertok(ofnode_set_enabled(node, false));
582 ut_assert(!ofnode_is_enabled(node));
586 DM_TEST(dm_test_ofnode_livetree_writing,
587 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
589 static int dm_test_ofnode_u32(struct unit_test_state *uts)
593 node = ofnode_path("/lcd");
594 ut_assert(ofnode_valid(node));
595 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
596 ut_assertok(ofnode_write_u32(node, "xres", 1367));
597 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
598 ut_assertok(ofnode_write_u32(node, "xres", 1366));
602 DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
604 static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
606 ofnode node, check, subnode;
609 node = ofnode_path("/lcd");
610 ut_assert(ofnode_valid(node));
611 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
612 check = ofnode_path("/lcd/edmund");
613 ut_asserteq(subnode.of_offset, check.of_offset);
614 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
615 ut_asserteq_str("/lcd/edmund", buf);
617 if (of_live_active()) {
618 struct device_node *child;
620 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
622 ut_asserteq_str("ed", child->name);
623 ut_asserteq_str("/lcd/ed", child->full_name);
624 check = ofnode_path("/lcd/ed");
625 ut_asserteq_ptr(child, check.np);
626 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
628 ut_asserteq_str("/lcd/ed", buf);
631 /* An existing node should be returned with -EEXIST */
632 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
633 ut_asserteq(subnode.of_offset, check.of_offset);
635 /* add a root node */
636 node = ofnode_path("/");
637 ut_assert(ofnode_valid(node));
638 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
639 check = ofnode_path("/lcd2");
640 ut_asserteq(subnode.of_offset, check.of_offset);
641 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
642 ut_asserteq_str("/lcd2", buf);
644 if (of_live_active()) {
649 * Make sure each of the three malloc()checks in
650 * of_add_subnode() work
652 for (i = 0; i < 3; i++) {
653 malloc_enable_testing(i);
654 start = ut_check_free();
655 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
657 ut_assertok(ut_check_delta(start));
660 /* This should pass since we allow 3 allocations */
661 malloc_enable_testing(3);
662 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
663 malloc_disable_testing();
666 /* write to the empty node */
667 ut_assertok(ofnode_write_string(subnode, "example", "text"));
671 DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
673 static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
675 ofnode node, subnode;
679 node = ofnode_path("/buttons");
682 /* we expect "compatible" for each node */
683 ofnode_for_each_prop(prop, node)
685 ut_asserteq(1, count);
687 /* there are two nodes, each with 2 properties */
688 ofnode_for_each_subnode(subnode, node)
689 ofnode_for_each_prop(prop, subnode)
691 ut_asserteq(5, count);
695 DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);