1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2022 Google LLC
5 * There are two types of tests in this file:
6 * - normal ones which act on the control FDT (gd->fdt_blob or gd->of_root)
7 * - 'other' ones which act on the 'other' FDT (other.dts)
9 * The 'other' ones have an _ot suffix.
11 * The latter are used to check behaviour with multiple device trees,
12 * particularly with flat tree, where a tree ID is included in ofnode as part of
13 * the node offset. These tests are typically just for making sure that the
14 * offset makes it to libfdt correctly and that the resulting return value is
15 * correctly turned into an ofnode. The 'other' tests do not fully check the
16 * behaviour of each ofnode function, since that is done by the normal ones.
23 #include <dm/device-internal.h>
25 #include <dm/of_extra.h>
28 #include <dm/uclass-internal.h>
29 #include <test/test.h>
33 * get_other_oftree() - Convert a flat tree into an oftree object
36 * @return: oftree object for the 'other' FDT (see sandbox' other.dts)
38 oftree get_other_oftree(struct unit_test_state *uts)
43 tree = oftree_from_np(uts->of_other);
45 tree = oftree_from_fdt(uts->other_fdt);
47 /* An invalid tree may cause failure or crashes */
48 if (!oftree_valid(tree))
49 ut_reportf("test needs the UT_TESTF_OTHER_FDT flag");
54 static int dm_test_ofnode_compatible(struct unit_test_state *uts)
56 ofnode root_node = ofnode_path("/");
58 ut_assert(ofnode_valid(root_node));
59 ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
63 DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
65 static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
67 /* test invalid phandle */
68 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
69 ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
71 /* test first valid phandle */
72 ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
74 /* test unknown phandle */
75 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
77 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
81 DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
83 static int dm_test_ofnode_get_by_phandle_ot(struct unit_test_state *uts)
85 oftree otree = get_other_oftree(uts);
88 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
89 node = oftree_get_by_phandle(otree, 1);
90 ut_assert(ofnode_valid(node));
91 ut_asserteq_str("target", ofnode_get_name(node));
95 DM_TEST(dm_test_ofnode_get_by_phandle_ot, UT_TESTF_OTHER_FDT);
97 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
99 const char propname[] = "compatible";
100 const char propval[] = "denx,u-boot-fdt-test";
102 ofnode node = ofnode_null();
104 /* Find first matching node, there should be at least one */
105 node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
106 ut_assert(ofnode_valid(node));
107 str = ofnode_read_string(node, propname);
108 ut_assert(str && !strcmp(str, propval));
110 /* Find the rest of the matching nodes */
112 node = ofnode_by_prop_value(node, propname, propval,
114 if (!ofnode_valid(node))
116 str = ofnode_read_string(node, propname);
117 ut_assert(str && !strcmp(str, propval));
122 DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
124 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
126 struct fmap_entry entry;
129 node = ofnode_path("/cros-ec/flash");
130 ut_assert(ofnode_valid(node));
131 ut_assertok(ofnode_read_fmap_entry(node, &entry));
132 ut_asserteq(0x08000000, entry.offset);
133 ut_asserteq(0x20000, entry.length);
137 DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
139 static int dm_test_ofnode_read(struct unit_test_state *uts)
145 node = ofnode_path("/a-test");
146 ut_assert(ofnode_valid(node));
148 val = ofnode_read_prop(node, "int-value", &size);
149 ut_assertnonnull(val);
150 ut_asserteq(4, size);
151 ut_asserteq(1234, fdt32_to_cpu(val[0]));
153 val = ofnode_read_prop(node, "missing", &size);
155 ut_asserteq(-FDT_ERR_NOTFOUND, size);
157 /* Check it works without a size parameter */
158 val = ofnode_read_prop(node, "missing", NULL);
163 DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
165 static int dm_test_ofnode_phandle(struct unit_test_state *uts)
167 struct ofnode_phandle_args args;
170 const char prop[] = "test-gpios";
171 const char cell[] = "#gpio-cells";
172 const char prop2[] = "phandle-value";
174 node = ofnode_path("/a-test");
175 ut_assert(ofnode_valid(node));
177 /* Test ofnode_count_phandle_with_args with cell name */
178 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
179 ut_asserteq(-ENOENT, ret);
180 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
181 ut_asserteq(-EINVAL, ret);
182 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
185 /* Test ofnode_parse_phandle_with_args with cell name */
186 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
188 ut_asserteq(-ENOENT, ret);
189 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
191 ut_asserteq(-EINVAL, ret);
192 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
194 ut_asserteq(1, args.args_count);
195 ut_asserteq(1, args.args[0]);
196 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
198 ut_asserteq(1, args.args_count);
199 ut_asserteq(4, args.args[0]);
200 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
202 ut_asserteq(5, args.args_count);
203 ut_asserteq(5, args.args[0]);
204 ut_asserteq(1, args.args[4]);
205 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
206 ut_asserteq(-ENOENT, ret);
207 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
209 ut_asserteq(1, args.args_count);
210 ut_asserteq(12, args.args[0]);
211 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
212 ut_asserteq(-ENOENT, ret);
214 /* Test ofnode_count_phandle_with_args with cell count */
215 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
216 ut_asserteq(-ENOENT, ret);
217 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
220 /* Test ofnode_parse_phandle_with_args with cell count */
221 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
223 ut_asserteq(1, ofnode_valid(args.node));
224 ut_asserteq(1, args.args_count);
225 ut_asserteq(10, args.args[0]);
226 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
227 ut_asserteq(-EINVAL, ret);
228 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
230 ut_asserteq(1, ofnode_valid(args.node));
231 ut_asserteq(1, args.args_count);
232 ut_asserteq(30, args.args[0]);
233 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
234 ut_asserteq(-ENOENT, ret);
238 DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
240 static int dm_test_ofnode_phandle_ot(struct unit_test_state *uts)
242 oftree otree = get_other_oftree(uts);
243 struct ofnode_phandle_args args;
247 node = oftree_path(otree, "/node");
249 /* Test ofnode_count_phandle_with_args with cell name */
250 ret = ofnode_count_phandle_with_args(node, "missing", "#gpio-cells", 0);
251 ut_asserteq(-ENOENT, ret);
252 ret = ofnode_count_phandle_with_args(node, "target", "#invalid", 0);
253 ut_asserteq(-EINVAL, ret);
254 ret = ofnode_count_phandle_with_args(node, "target", "#gpio-cells", 0);
257 ret = ofnode_parse_phandle_with_args(node, "target", "#gpio-cells", 0,
260 ut_asserteq(2, args.args_count);
261 ut_asserteq(3, args.args[0]);
262 ut_asserteq(4, args.args[1]);
266 DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT);
268 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
275 str = ofnode_read_chosen_string("setting");
276 ut_assertnonnull(str);
277 ut_asserteq_str("sunrise ohoka", str);
278 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
280 node = ofnode_get_chosen_node("other-node");
281 ut_assert(ofnode_valid(node));
282 ut_asserteq_str("c-test@5", ofnode_get_name(node));
284 node = ofnode_get_chosen_node("setting");
285 ut_assert(!ofnode_valid(node));
287 val = ofnode_read_chosen_prop("int-values", &size);
288 ut_assertnonnull(val);
289 ut_asserteq(8, size);
290 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
291 ut_asserteq(72993, fdt32_to_cpu(val[1]));
295 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
297 static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
303 node = ofnode_get_aliases_node("ethernet3");
304 ut_assert(ofnode_valid(node));
305 ut_asserteq_str("sbe5", ofnode_get_name(node));
307 node = ofnode_get_aliases_node("unknown");
308 ut_assert(!ofnode_valid(node));
310 val = ofnode_read_aliases_prop("spi0", &size);
311 ut_assertnonnull(val);
312 ut_asserteq(7, size);
313 ut_asserteq_str("/spi@0", (const char *)val);
317 DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
319 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
321 ofnode node, child_node;
324 node = ofnode_path("/i-test");
325 ut_assert(ofnode_valid(node));
327 val = ofnode_get_child_count(node);
330 child_node = ofnode_first_subnode(node);
331 ut_assert(ofnode_valid(child_node));
332 val = ofnode_get_child_count(child_node);
337 DM_TEST(dm_test_ofnode_get_child_count,
338 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
340 static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
342 ofnode root_node = ofnode_path("/");
343 ofnode node = ofnode_path("/usb@0");
345 ut_assert(ofnode_is_enabled(root_node));
346 ut_assert(!ofnode_is_enabled(node));
350 DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
352 static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
358 node = ofnode_path("/translation-test@8000");
359 ut_assert(ofnode_valid(node));
360 addr = ofnode_get_addr(node);
361 size = ofnode_get_size(node);
362 ut_asserteq(0x8000, addr);
363 ut_asserteq(0x4000, size);
365 node = ofnode_path("/translation-test@8000/dev@1,100");
366 ut_assert(ofnode_valid(node));
367 addr = ofnode_get_addr(node);
368 size = ofnode_get_size(node);
369 ut_asserteq(0x9000, addr);
370 ut_asserteq(0x1000, size);
372 node = ofnode_path("/emul-mux-controller");
373 ut_assert(ofnode_valid(node));
374 addr = ofnode_get_addr(node);
375 size = ofnode_get_size(node);
376 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
377 ut_asserteq(FDT_SIZE_T_NONE, size);
379 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
380 ut_assert(ofnode_valid(node));
381 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
382 ut_asserteq_64(0x42, addr);
386 DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
388 static int dm_test_ofnode_get_path(struct unit_test_state *uts)
390 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
395 node = ofnode_path(path);
396 ut_assert(ofnode_valid(node));
398 res = ofnode_get_path(node, buf, 64);
400 ut_asserteq_str(path, buf);
402 res = ofnode_get_path(node, buf, 32);
403 ut_asserteq(-ENOSPC, res);
405 res = ofnode_get_path(ofnode_root(), buf, 32);
406 ut_asserteq_str("/", buf);
410 DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
412 static int dm_test_ofnode_conf(struct unit_test_state *uts)
414 ut_assert(!ofnode_conf_read_bool("missing"));
415 ut_assert(ofnode_conf_read_bool("testing-bool"));
417 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
418 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
420 ut_assertnull(ofnode_conf_read_str("missing"));
421 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
425 DM_TEST(dm_test_ofnode_conf, 0);
427 static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
429 const char compatible[] = "denx,u-boot-fdt-test";
433 ofnode_for_each_compatible_node(node, compatible) {
434 ut_assert(ofnode_device_is_compatible(node, compatible));
438 /* There should be at least one matching node */
443 DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
445 static int dm_test_ofnode_string(struct unit_test_state *uts)
451 node = ofnode_path("/a-test");
452 ut_assert(ofnode_valid(node));
455 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
456 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
457 ut_asserteq_str("test string", out);
458 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
460 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
461 ut_asserteq_str("test string", val[0]);
462 ut_assertnull(val[1]);
465 /* list of strings */
466 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
467 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
469 ut_asserteq_str("mux0", out);
470 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
472 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
474 ut_asserteq_str("mux0", val[0]);
475 ut_asserteq_str("mux1", val[1]);
476 ut_asserteq_str("mux2", val[2]);
477 ut_asserteq_str("mux3", val[3]);
478 ut_asserteq_str("mux4", val[4]);
479 ut_assertnull(val[5]);
482 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
484 ut_asserteq_str("mux4", out);
485 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
490 DM_TEST(dm_test_ofnode_string, 0);
492 static int dm_test_ofnode_string_err(struct unit_test_state *uts)
499 * Test error codes only on livetree, as they are different with
502 node = ofnode_path("/a-test");
503 ut_assert(ofnode_valid(node));
505 /* non-existent property */
506 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
507 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
509 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
512 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
513 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
515 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
518 /* badly formatted string list */
519 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
520 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
522 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
525 /* out of range / not found */
526 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
528 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
531 /* negative value for index is not allowed, so don't test for that */
533 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
534 "mux-control-names", 5,
539 DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
541 static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
543 ofnode eth_node, phy_node;
544 phy_interface_t mode;
547 eth_node = ofnode_path("/phy-test-eth");
548 ut_assert(ofnode_valid(eth_node));
550 mode = ofnode_read_phy_mode(eth_node);
551 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
553 phy_node = ofnode_get_phy_node(eth_node);
554 ut_assert(ofnode_valid(phy_node));
556 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
557 ut_asserteq_64(0x1, reg);
561 DM_TEST(dm_test_ofnode_get_phy, 0);
564 * make_ofnode_fdt() - Create an FDT for testing with ofnode
566 * The size is set to the minimum needed
569 * @fdt: Place to write FDT
570 * @size: Maximum size of space for fdt
572 static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
574 ut_assertok(fdt_create(fdt, size));
575 ut_assertok(fdt_finish_reservemap(fdt));
576 ut_assert(fdt_begin_node(fdt, "") >= 0);
578 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
579 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
580 ut_assertok(fdt_end_node(fdt));
582 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
583 ut_assertok(fdt_end_node(fdt));
585 ut_assertok(fdt_end_node(fdt));
586 ut_assertok(fdt_finish(fdt));
591 static int dm_test_ofnode_root(struct unit_test_state *uts)
593 struct device_node *root = NULL;
598 /* Check that aliases work on the control FDT */
599 node = ofnode_get_aliases_node("ethernet3");
600 ut_assert(ofnode_valid(node));
601 ut_asserteq_str("sbe5", ofnode_get_name(node));
603 ut_assert(!oftree_valid(oftree_null()));
605 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
606 if (of_live_active()) {
607 ut_assertok(unflatten_device_tree(fdt, &root));
608 tree = oftree_from_np(root);
610 tree = oftree_from_fdt(fdt);
612 ut_assert(oftree_valid(tree));
614 /* Make sure they don't work on this new tree */
615 node = oftree_path(tree, "mmc0");
616 ut_assert(!ofnode_valid(node));
618 /* It should appear in the new tree */
619 node = oftree_path(tree, "/new-mmc");
620 ut_assert(ofnode_valid(node));
622 /* ...and not in the control FDT */
623 node = oftree_path(oftree_default(), "/new-mmc");
624 ut_assert(!ofnode_valid(node));
630 DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
632 static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
637 /* Test enabling devices */
638 node = ofnode_path("/usb@2");
640 ut_assert(!ofnode_is_enabled(node));
641 ut_assertok(ofnode_set_enabled(node, true));
642 ut_asserteq(true, ofnode_is_enabled(node));
644 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
646 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
648 /* Test string property setting */
649 ut_assert(device_is_compatible(dev, "sandbox,usb"));
650 ofnode_write_string(node, "compatible", "gdsys,super-usb");
651 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
652 ofnode_write_string(node, "compatible", "sandbox,usb");
653 ut_assert(device_is_compatible(dev, "sandbox,usb"));
655 /* Test setting generic properties */
657 /* Non-existent in DTB */
658 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
659 /* reg = 0x42, size = 0x100 */
660 ut_assertok(ofnode_write_prop(node, "reg",
661 "\x00\x00\x00\x42\x00\x00\x01\x00", 8));
662 ut_asserteq(0x42, dev_read_addr(dev));
664 /* Test disabling devices */
665 device_remove(dev, DM_REMOVE_NORMAL);
668 ut_assert(ofnode_is_enabled(node));
669 ut_assertok(ofnode_set_enabled(node, false));
670 ut_assert(!ofnode_is_enabled(node));
674 DM_TEST(dm_test_ofnode_livetree_writing,
675 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
677 static int dm_test_ofnode_u32(struct unit_test_state *uts)
682 node = ofnode_path("/lcd");
683 ut_assert(ofnode_valid(node));
684 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
685 ut_assertok(ofnode_write_u32(node, "xres", 1367));
686 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
687 ut_assertok(ofnode_write_u32(node, "xres", 1366));
689 node = ofnode_path("/backlight");
690 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
692 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
693 ut_asserteq(16, val);
694 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
695 ut_asserteq(255, val);
696 ut_asserteq(-EOVERFLOW,
697 ofnode_read_u32_index(node, "brightness-levels", 9, &val));
698 ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
702 DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
704 static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
709 node = ofnode_path("/a-test");
710 ut_assert(ofnode_valid(node));
711 ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
712 ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
713 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
716 memset(val, '\0', sizeof(val));
717 ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
718 ut_asserteq(0, val[0]);
719 ut_asserteq(5678, val[1]);
720 ut_asserteq(9123, val[2]);
721 ut_asserteq(4567, val[3]);
722 ut_asserteq(0, val[4]);
723 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
728 DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
730 static int dm_test_ofnode_u64(struct unit_test_state *uts)
735 node = ofnode_path("/a-test");
736 ut_assert(ofnode_valid(node));
737 ut_assertok(ofnode_read_u64(node, "int64-value", &val));
738 ut_asserteq_64(0x1111222233334444, val);
739 ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
743 DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
745 static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
747 ofnode node, check, subnode;
750 node = ofnode_path("/lcd");
751 ut_assert(ofnode_valid(node));
752 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
753 check = ofnode_path("/lcd/edmund");
754 ut_asserteq(subnode.of_offset, check.of_offset);
755 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
756 ut_asserteq_str("/lcd/edmund", buf);
758 if (of_live_active()) {
759 struct device_node *child;
761 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
763 ut_asserteq_str("ed", child->name);
764 ut_asserteq_str("/lcd/ed", child->full_name);
765 check = ofnode_path("/lcd/ed");
766 ut_asserteq_ptr(child, check.np);
767 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
769 ut_asserteq_str("/lcd/ed", buf);
772 /* An existing node should be returned with -EEXIST */
773 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
774 ut_asserteq(subnode.of_offset, check.of_offset);
776 /* add a root node */
777 node = ofnode_path("/");
778 ut_assert(ofnode_valid(node));
779 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
780 check = ofnode_path("/lcd2");
781 ut_asserteq(subnode.of_offset, check.of_offset);
782 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
783 ut_asserteq_str("/lcd2", buf);
785 if (of_live_active()) {
790 * Make sure each of the three malloc()checks in
791 * of_add_subnode() work
793 for (i = 0; i < 3; i++) {
794 malloc_enable_testing(i);
795 start = ut_check_free();
796 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
798 ut_assertok(ut_check_delta(start));
801 /* This should pass since we allow 3 allocations */
802 malloc_enable_testing(3);
803 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
804 malloc_disable_testing();
807 /* write to the empty node */
808 ut_assertok(ofnode_write_string(subnode, "example", "text"));
812 DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
814 static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
816 ofnode node, subnode;
820 node = ofnode_path("/buttons");
823 /* we expect "compatible" for each node */
824 ofnode_for_each_prop(prop, node)
826 ut_asserteq(1, count);
828 /* there are two nodes, each with 2 properties */
829 ofnode_for_each_subnode(subnode, node)
830 ofnode_for_each_prop(prop, subnode)
832 ut_asserteq(5, count);
836 DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
838 static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
840 const char *compat = "denx,u-boot-fdt-test";
845 for (node = ofnode_null();
846 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
848 ut_asserteq(11, count);
852 DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
854 static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
856 ofnode node, subnode;
858 node = ofnode_path("/buttons");
860 subnode = ofnode_find_subnode(node, "btn1");
861 ut_assert(ofnode_valid(subnode));
862 ut_asserteq_str("btn1", ofnode_get_name(subnode));
864 subnode = ofnode_find_subnode(node, "btn");
865 ut_assert(!ofnode_valid(subnode));
869 DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
871 static int dm_test_ofnode_get_name(struct unit_test_state *uts)
875 node = ofnode_path("/buttons");
876 ut_assert(ofnode_valid(node));
877 ut_asserteq_str("buttons", ofnode_get_name(node));
878 ut_asserteq_str("", ofnode_get_name(ofnode_root()));
882 DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);