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)));
39 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
43 DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
45 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
47 const char propname[] = "compatible";
48 const char propval[] = "denx,u-boot-fdt-test";
50 ofnode node = ofnode_null();
52 /* Find first matching node, there should be at least one */
53 node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
54 ut_assert(ofnode_valid(node));
55 str = ofnode_read_string(node, propname);
56 ut_assert(str && !strcmp(str, propval));
58 /* Find the rest of the matching nodes */
60 node = ofnode_by_prop_value(node, propname, propval,
62 if (!ofnode_valid(node))
64 str = ofnode_read_string(node, propname);
65 ut_assert(str && !strcmp(str, propval));
70 DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
72 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
74 struct fmap_entry entry;
77 node = ofnode_path("/cros-ec/flash");
78 ut_assert(ofnode_valid(node));
79 ut_assertok(ofnode_read_fmap_entry(node, &entry));
80 ut_asserteq(0x08000000, entry.offset);
81 ut_asserteq(0x20000, entry.length);
85 DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
87 static int dm_test_ofnode_read(struct unit_test_state *uts)
93 node = ofnode_path("/a-test");
94 ut_assert(ofnode_valid(node));
96 val = ofnode_read_prop(node, "int-value", &size);
97 ut_assertnonnull(val);
99 ut_asserteq(1234, fdt32_to_cpu(val[0]));
101 val = ofnode_read_prop(node, "missing", &size);
103 ut_asserteq(-FDT_ERR_NOTFOUND, size);
105 /* Check it works without a size parameter */
106 val = ofnode_read_prop(node, "missing", NULL);
111 DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
113 static int dm_test_ofnode_phandle(struct unit_test_state *uts)
115 struct ofnode_phandle_args args;
118 const char prop[] = "test-gpios";
119 const char cell[] = "#gpio-cells";
120 const char prop2[] = "phandle-value";
122 node = ofnode_path("/a-test");
123 ut_assert(ofnode_valid(node));
125 /* Test ofnode_count_phandle_with_args with cell name */
126 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
127 ut_asserteq(-ENOENT, ret);
128 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
129 ut_asserteq(-EINVAL, ret);
130 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
133 /* Test ofnode_parse_phandle_with_args with cell name */
134 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
136 ut_asserteq(-ENOENT, ret);
137 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
139 ut_asserteq(-EINVAL, ret);
140 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
142 ut_asserteq(1, args.args_count);
143 ut_asserteq(1, args.args[0]);
144 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
146 ut_asserteq(1, args.args_count);
147 ut_asserteq(4, args.args[0]);
148 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
150 ut_asserteq(5, args.args_count);
151 ut_asserteq(5, args.args[0]);
152 ut_asserteq(1, args.args[4]);
153 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
154 ut_asserteq(-ENOENT, ret);
155 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
157 ut_asserteq(1, args.args_count);
158 ut_asserteq(12, args.args[0]);
159 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
160 ut_asserteq(-ENOENT, ret);
162 /* Test ofnode_count_phandle_with_args with cell count */
163 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
164 ut_asserteq(-ENOENT, ret);
165 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
168 /* Test ofnode_parse_phandle_with_args with cell count */
169 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
171 ut_asserteq(1, ofnode_valid(args.node));
172 ut_asserteq(1, args.args_count);
173 ut_asserteq(10, args.args[0]);
174 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
175 ut_asserteq(-EINVAL, ret);
176 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
178 ut_asserteq(1, ofnode_valid(args.node));
179 ut_asserteq(1, args.args_count);
180 ut_asserteq(30, args.args[0]);
181 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
182 ut_asserteq(-ENOENT, ret);
186 DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
188 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
195 str = ofnode_read_chosen_string("setting");
196 ut_assertnonnull(str);
197 ut_asserteq_str("sunrise ohoka", str);
198 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
200 node = ofnode_get_chosen_node("other-node");
201 ut_assert(ofnode_valid(node));
202 ut_asserteq_str("c-test@5", ofnode_get_name(node));
204 node = ofnode_get_chosen_node("setting");
205 ut_assert(!ofnode_valid(node));
207 val = ofnode_read_chosen_prop("int-values", &size);
208 ut_assertnonnull(val);
209 ut_asserteq(8, size);
210 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
211 ut_asserteq(72993, fdt32_to_cpu(val[1]));
215 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
217 static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
223 node = ofnode_get_aliases_node("ethernet3");
224 ut_assert(ofnode_valid(node));
225 ut_asserteq_str("sbe5", ofnode_get_name(node));
227 node = ofnode_get_aliases_node("unknown");
228 ut_assert(!ofnode_valid(node));
230 val = ofnode_read_aliases_prop("spi0", &size);
231 ut_assertnonnull(val);
232 ut_asserteq(7, size);
233 ut_asserteq_str("/spi@0", (const char *)val);
237 DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
239 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
241 ofnode node, child_node;
244 node = ofnode_path("/i-test");
245 ut_assert(ofnode_valid(node));
247 val = ofnode_get_child_count(node);
250 child_node = ofnode_first_subnode(node);
251 ut_assert(ofnode_valid(child_node));
252 val = ofnode_get_child_count(child_node);
257 DM_TEST(dm_test_ofnode_get_child_count,
258 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
260 static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
262 ofnode root_node = ofnode_path("/");
263 ofnode node = ofnode_path("/usb@0");
265 ut_assert(ofnode_is_enabled(root_node));
266 ut_assert(!ofnode_is_enabled(node));
270 DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
272 static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
278 node = ofnode_path("/translation-test@8000");
279 ut_assert(ofnode_valid(node));
280 addr = ofnode_get_addr(node);
281 size = ofnode_get_size(node);
282 ut_asserteq(0x8000, addr);
283 ut_asserteq(0x4000, size);
285 node = ofnode_path("/translation-test@8000/dev@1,100");
286 ut_assert(ofnode_valid(node));
287 addr = ofnode_get_addr(node);
288 size = ofnode_get_size(node);
289 ut_asserteq(0x9000, addr);
290 ut_asserteq(0x1000, size);
292 node = ofnode_path("/emul-mux-controller");
293 ut_assert(ofnode_valid(node));
294 addr = ofnode_get_addr(node);
295 size = ofnode_get_size(node);
296 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
297 ut_asserteq(FDT_SIZE_T_NONE, size);
299 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
300 ut_assert(ofnode_valid(node));
301 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
302 ut_asserteq_64(0x42, addr);
306 DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
308 static int dm_test_ofnode_get_path(struct unit_test_state *uts)
310 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
315 node = ofnode_path(path);
316 ut_assert(ofnode_valid(node));
318 res = ofnode_get_path(node, buf, 64);
320 ut_asserteq_str(path, buf);
322 res = ofnode_get_path(node, buf, 32);
323 ut_asserteq(-ENOSPC, res);
325 res = ofnode_get_path(ofnode_root(), buf, 32);
326 ut_asserteq_str("/", buf);
330 DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
332 static int dm_test_ofnode_conf(struct unit_test_state *uts)
334 ut_assert(!ofnode_conf_read_bool("missing"));
335 ut_assert(ofnode_conf_read_bool("testing-bool"));
337 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
338 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
340 ut_assertnull(ofnode_conf_read_str("missing"));
341 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
345 DM_TEST(dm_test_ofnode_conf, 0);
347 static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
349 const char compatible[] = "denx,u-boot-fdt-test";
353 ofnode_for_each_compatible_node(node, compatible) {
354 ut_assert(ofnode_device_is_compatible(node, compatible));
358 /* There should be at least one matching node */
363 DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
365 static int dm_test_ofnode_string(struct unit_test_state *uts)
371 node = ofnode_path("/a-test");
372 ut_assert(ofnode_valid(node));
375 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
376 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
377 ut_asserteq_str("test string", out);
378 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
380 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
381 ut_asserteq_str("test string", val[0]);
382 ut_assertnull(val[1]);
385 /* list of strings */
386 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
387 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
389 ut_asserteq_str("mux0", out);
390 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
392 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
394 ut_asserteq_str("mux0", val[0]);
395 ut_asserteq_str("mux1", val[1]);
396 ut_asserteq_str("mux2", val[2]);
397 ut_asserteq_str("mux3", val[3]);
398 ut_asserteq_str("mux4", val[4]);
399 ut_assertnull(val[5]);
402 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
404 ut_asserteq_str("mux4", out);
405 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
410 DM_TEST(dm_test_ofnode_string, 0);
412 static int dm_test_ofnode_string_err(struct unit_test_state *uts)
419 * Test error codes only on livetree, as they are different with
422 node = ofnode_path("/a-test");
423 ut_assert(ofnode_valid(node));
425 /* non-existent property */
426 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
427 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
429 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
432 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
433 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
435 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
438 /* badly formatted string list */
439 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
440 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
442 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
445 /* out of range / not found */
446 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
448 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
451 /* negative value for index is not allowed, so don't test for that */
453 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
454 "mux-control-names", 5,
459 DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
461 static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
463 ofnode eth_node, phy_node;
464 phy_interface_t mode;
467 eth_node = ofnode_path("/phy-test-eth");
468 ut_assert(ofnode_valid(eth_node));
470 mode = ofnode_read_phy_mode(eth_node);
471 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
473 phy_node = ofnode_get_phy_node(eth_node);
474 ut_assert(ofnode_valid(phy_node));
476 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
477 ut_asserteq_64(0x1, reg);
481 DM_TEST(dm_test_ofnode_get_phy, 0);
484 * make_ofnode_fdt() - Create an FDT for testing with ofnode
486 * The size is set to the minimum needed
489 * @fdt: Place to write FDT
490 * @size: Maximum size of space for fdt
492 static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
494 ut_assertok(fdt_create(fdt, size));
495 ut_assertok(fdt_finish_reservemap(fdt));
496 ut_assert(fdt_begin_node(fdt, "") >= 0);
498 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
499 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
500 ut_assertok(fdt_end_node(fdt));
502 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
503 ut_assertok(fdt_end_node(fdt));
505 ut_assertok(fdt_end_node(fdt));
506 ut_assertok(fdt_finish(fdt));
511 static int dm_test_ofnode_root(struct unit_test_state *uts)
513 struct device_node *root = NULL;
518 /* Check that aliases work on the control FDT */
519 node = ofnode_get_aliases_node("ethernet3");
520 ut_assert(ofnode_valid(node));
521 ut_asserteq_str("sbe5", ofnode_get_name(node));
523 ut_assert(!oftree_valid(oftree_null()));
525 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
526 if (of_live_active()) {
527 ut_assertok(unflatten_device_tree(fdt, &root));
528 tree = oftree_from_np(root);
530 tree = oftree_from_fdt(fdt);
532 ut_assert(oftree_valid(tree));
534 /* Make sure they don't work on this new tree */
535 node = ofnode_path_root(tree, "mmc0");
536 ut_assert(!ofnode_valid(node));
538 /* It should appear in the new tree */
539 node = ofnode_path_root(tree, "/new-mmc");
540 ut_assert(ofnode_valid(node));
542 /* ...and not in the control FDT */
543 node = ofnode_path_root(oftree_default(), "/new-mmc");
544 ut_assert(!ofnode_valid(node));
550 DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
552 static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
557 /* Test enabling devices */
558 node = ofnode_path("/usb@2");
560 ut_assert(!ofnode_is_enabled(node));
561 ut_assertok(ofnode_set_enabled(node, true));
562 ut_asserteq(true, ofnode_is_enabled(node));
564 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
566 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
568 /* Test string property setting */
569 ut_assert(device_is_compatible(dev, "sandbox,usb"));
570 ofnode_write_string(node, "compatible", "gdsys,super-usb");
571 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
572 ofnode_write_string(node, "compatible", "sandbox,usb");
573 ut_assert(device_is_compatible(dev, "sandbox,usb"));
575 /* Test setting generic properties */
577 /* Non-existent in DTB */
578 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
579 /* reg = 0x42, size = 0x100 */
580 ut_assertok(ofnode_write_prop(node, "reg",
581 "\x00\x00\x00\x42\x00\x00\x01\x00", 8));
582 ut_asserteq(0x42, dev_read_addr(dev));
584 /* Test disabling devices */
585 device_remove(dev, DM_REMOVE_NORMAL);
588 ut_assert(ofnode_is_enabled(node));
589 ut_assertok(ofnode_set_enabled(node, false));
590 ut_assert(!ofnode_is_enabled(node));
594 DM_TEST(dm_test_ofnode_livetree_writing,
595 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
597 static int dm_test_ofnode_u32(struct unit_test_state *uts)
602 node = ofnode_path("/lcd");
603 ut_assert(ofnode_valid(node));
604 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
605 ut_assertok(ofnode_write_u32(node, "xres", 1367));
606 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
607 ut_assertok(ofnode_write_u32(node, "xres", 1366));
609 node = ofnode_path("/backlight");
610 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
612 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
613 ut_asserteq(16, val);
614 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
615 ut_asserteq(255, val);
616 ut_asserteq(-EOVERFLOW,
617 ofnode_read_u32_index(node, "brightness-levels", 9, &val));
618 ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
622 DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
624 static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
629 node = ofnode_path("/a-test");
630 ut_assert(ofnode_valid(node));
631 ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
632 ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
633 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
636 memset(val, '\0', sizeof(val));
637 ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
638 ut_asserteq(0, val[0]);
639 ut_asserteq(5678, val[1]);
640 ut_asserteq(9123, val[2]);
641 ut_asserteq(4567, val[3]);
642 ut_asserteq(0, val[4]);
643 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
648 DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
650 static int dm_test_ofnode_u64(struct unit_test_state *uts)
655 node = ofnode_path("/a-test");
656 ut_assert(ofnode_valid(node));
657 ut_assertok(ofnode_read_u64(node, "int64-value", &val));
658 ut_asserteq_64(0x1111222233334444, val);
659 ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
663 DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
665 static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
667 ofnode node, check, subnode;
670 node = ofnode_path("/lcd");
671 ut_assert(ofnode_valid(node));
672 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
673 check = ofnode_path("/lcd/edmund");
674 ut_asserteq(subnode.of_offset, check.of_offset);
675 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
676 ut_asserteq_str("/lcd/edmund", buf);
678 if (of_live_active()) {
679 struct device_node *child;
681 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
683 ut_asserteq_str("ed", child->name);
684 ut_asserteq_str("/lcd/ed", child->full_name);
685 check = ofnode_path("/lcd/ed");
686 ut_asserteq_ptr(child, check.np);
687 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
689 ut_asserteq_str("/lcd/ed", buf);
692 /* An existing node should be returned with -EEXIST */
693 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
694 ut_asserteq(subnode.of_offset, check.of_offset);
696 /* add a root node */
697 node = ofnode_path("/");
698 ut_assert(ofnode_valid(node));
699 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
700 check = ofnode_path("/lcd2");
701 ut_asserteq(subnode.of_offset, check.of_offset);
702 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
703 ut_asserteq_str("/lcd2", buf);
705 if (of_live_active()) {
710 * Make sure each of the three malloc()checks in
711 * of_add_subnode() work
713 for (i = 0; i < 3; i++) {
714 malloc_enable_testing(i);
715 start = ut_check_free();
716 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
718 ut_assertok(ut_check_delta(start));
721 /* This should pass since we allow 3 allocations */
722 malloc_enable_testing(3);
723 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
724 malloc_disable_testing();
727 /* write to the empty node */
728 ut_assertok(ofnode_write_string(subnode, "example", "text"));
732 DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
734 static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
736 ofnode node, subnode;
740 node = ofnode_path("/buttons");
743 /* we expect "compatible" for each node */
744 ofnode_for_each_prop(prop, node)
746 ut_asserteq(1, count);
748 /* there are two nodes, each with 2 properties */
749 ofnode_for_each_subnode(subnode, node)
750 ofnode_for_each_prop(prop, subnode)
752 ut_asserteq(5, count);
756 DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
758 static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
760 const char *compat = "denx,u-boot-fdt-test";
765 for (node = ofnode_null();
766 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
768 ut_asserteq(11, count);
772 DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
774 static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
776 ofnode node, subnode;
778 node = ofnode_path("/buttons");
780 subnode = ofnode_find_subnode(node, "btn1");
781 ut_assert(ofnode_valid(subnode));
782 ut_asserteq_str("btn1", ofnode_get_name(subnode));
784 subnode = ofnode_find_subnode(node, "btn");
785 ut_assert(!ofnode_valid(subnode));
789 DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
791 static int dm_test_ofnode_get_name(struct unit_test_state *uts)
795 node = ofnode_path("/buttons");
796 ut_assert(ofnode_valid(node));
797 ut_asserteq_str("buttons", ofnode_get_name(node));
798 ut_asserteq_str("", ofnode_get_name(ofnode_root()));
802 DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);