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");
55 * get_oftree() - Convert a flat tree into an oftree object
58 * @fdt: Pointer to flat tree
59 * @treep: Returns the tree, on success
60 * Return: 0 if OK, 1 if the tree failed to unflatten, -EOVERFLOW if there are
61 * too many flat trees to allow another one to be registers (see
64 int get_oftree(struct unit_test_state *uts, void *fdt, oftree *treep)
68 if (of_live_active()) {
69 struct device_node *root;
71 ut_assertok(unflatten_device_tree(fdt, &root));
72 tree = oftree_from_np(root);
74 tree = oftree_from_fdt(fdt);
75 if (!oftree_valid(tree))
84 * free_oftree() - Free memory used by get_oftree()
88 void free_oftree(oftree tree)
94 static int dm_test_ofnode_compatible(struct unit_test_state *uts)
96 ofnode root_node = ofnode_path("/");
98 ut_assert(ofnode_valid(root_node));
99 ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
103 DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
105 static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
107 /* test invalid phandle */
108 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
109 ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
111 /* test first valid phandle */
112 ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
114 /* test unknown phandle */
115 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
117 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
121 DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
123 static int dm_test_ofnode_get_by_phandle_ot(struct unit_test_state *uts)
125 oftree otree = get_other_oftree(uts);
128 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
129 node = oftree_get_by_phandle(otree, 1);
130 ut_assert(ofnode_valid(node));
131 ut_asserteq_str("target", ofnode_get_name(node));
135 DM_TEST(dm_test_ofnode_get_by_phandle_ot, UT_TESTF_OTHER_FDT);
137 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
139 const char propname[] = "compatible";
140 const char propval[] = "denx,u-boot-fdt-test";
142 ofnode node = ofnode_null();
144 /* Find first matching node, there should be at least one */
145 node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
146 ut_assert(ofnode_valid(node));
147 str = ofnode_read_string(node, propname);
148 ut_assert(str && !strcmp(str, propval));
150 /* Find the rest of the matching nodes */
152 node = ofnode_by_prop_value(node, propname, propval,
154 if (!ofnode_valid(node))
156 str = ofnode_read_string(node, propname);
157 ut_assert(str && !strcmp(str, propval));
162 DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
164 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
166 struct fmap_entry entry;
169 node = ofnode_path("/cros-ec/flash");
170 ut_assert(ofnode_valid(node));
171 ut_assertok(ofnode_read_fmap_entry(node, &entry));
172 ut_asserteq(0x08000000, entry.offset);
173 ut_asserteq(0x20000, entry.length);
177 DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
179 static int dm_test_ofnode_read(struct unit_test_state *uts)
185 node = ofnode_path("/a-test");
186 ut_assert(ofnode_valid(node));
188 val = ofnode_read_prop(node, "int-value", &size);
189 ut_assertnonnull(val);
190 ut_asserteq(4, size);
191 ut_asserteq(1234, fdt32_to_cpu(val[0]));
193 val = ofnode_read_prop(node, "missing", &size);
195 ut_asserteq(-FDT_ERR_NOTFOUND, size);
197 /* Check it works without a size parameter */
198 val = ofnode_read_prop(node, "missing", NULL);
203 DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
205 static int dm_test_ofnode_phandle(struct unit_test_state *uts)
207 struct ofnode_phandle_args args;
210 const char prop[] = "test-gpios";
211 const char cell[] = "#gpio-cells";
212 const char prop2[] = "phandle-value";
214 node = ofnode_path("/a-test");
215 ut_assert(ofnode_valid(node));
217 /* Test ofnode_count_phandle_with_args with cell name */
218 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
219 ut_asserteq(-ENOENT, ret);
220 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
221 ut_asserteq(-EINVAL, ret);
222 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
225 /* Test ofnode_parse_phandle_with_args with cell name */
226 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
228 ut_asserteq(-ENOENT, ret);
229 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
231 ut_asserteq(-EINVAL, ret);
232 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
234 ut_asserteq(1, args.args_count);
235 ut_asserteq(1, args.args[0]);
236 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
238 ut_asserteq(1, args.args_count);
239 ut_asserteq(4, args.args[0]);
240 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
242 ut_asserteq(5, args.args_count);
243 ut_asserteq(5, args.args[0]);
244 ut_asserteq(1, args.args[4]);
245 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
246 ut_asserteq(-ENOENT, ret);
247 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
249 ut_asserteq(1, args.args_count);
250 ut_asserteq(12, args.args[0]);
251 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
252 ut_asserteq(-ENOENT, ret);
254 /* Test ofnode_count_phandle_with_args with cell count */
255 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
256 ut_asserteq(-ENOENT, ret);
257 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
260 /* Test ofnode_parse_phandle_with_args with cell count */
261 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
263 ut_asserteq(1, ofnode_valid(args.node));
264 ut_asserteq(1, args.args_count);
265 ut_asserteq(10, args.args[0]);
266 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
267 ut_asserteq(-EINVAL, ret);
268 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
270 ut_asserteq(1, ofnode_valid(args.node));
271 ut_asserteq(1, args.args_count);
272 ut_asserteq(30, args.args[0]);
273 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
274 ut_asserteq(-ENOENT, ret);
278 DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
280 static int dm_test_ofnode_phandle_ot(struct unit_test_state *uts)
282 oftree otree = get_other_oftree(uts);
283 struct ofnode_phandle_args args;
287 node = oftree_path(otree, "/node");
289 /* Test ofnode_count_phandle_with_args with cell name */
290 ret = ofnode_count_phandle_with_args(node, "missing", "#gpio-cells", 0);
291 ut_asserteq(-ENOENT, ret);
292 ret = ofnode_count_phandle_with_args(node, "target", "#invalid", 0);
293 ut_asserteq(-EINVAL, ret);
294 ret = ofnode_count_phandle_with_args(node, "target", "#gpio-cells", 0);
297 ret = ofnode_parse_phandle_with_args(node, "target", "#gpio-cells", 0,
300 ut_asserteq(2, args.args_count);
301 ut_asserteq(3, args.args[0]);
302 ut_asserteq(4, args.args[1]);
306 DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT);
308 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
315 str = ofnode_read_chosen_string("setting");
316 ut_assertnonnull(str);
317 ut_asserteq_str("sunrise ohoka", str);
318 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
320 node = ofnode_get_chosen_node("other-node");
321 ut_assert(ofnode_valid(node));
322 ut_asserteq_str("c-test@5", ofnode_get_name(node));
324 node = ofnode_get_chosen_node("setting");
325 ut_assert(!ofnode_valid(node));
327 val = ofnode_read_chosen_prop("int-values", &size);
328 ut_assertnonnull(val);
329 ut_asserteq(8, size);
330 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
331 ut_asserteq(72993, fdt32_to_cpu(val[1]));
335 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
337 static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
343 node = ofnode_get_aliases_node("ethernet3");
344 ut_assert(ofnode_valid(node));
345 ut_asserteq_str("sbe5", ofnode_get_name(node));
347 node = ofnode_get_aliases_node("unknown");
348 ut_assert(!ofnode_valid(node));
350 val = ofnode_read_aliases_prop("spi0", &size);
351 ut_assertnonnull(val);
352 ut_asserteq(7, size);
353 ut_asserteq_str("/spi@0", (const char *)val);
357 DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
359 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
361 ofnode node, child_node;
364 node = ofnode_path("/i-test");
365 ut_assert(ofnode_valid(node));
367 val = ofnode_get_child_count(node);
370 child_node = ofnode_first_subnode(node);
371 ut_assert(ofnode_valid(child_node));
372 val = ofnode_get_child_count(child_node);
377 DM_TEST(dm_test_ofnode_get_child_count,
378 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
380 static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
382 ofnode root_node = ofnode_path("/");
383 ofnode node = ofnode_path("/usb@0");
385 ut_assert(ofnode_is_enabled(root_node));
386 ut_assert(!ofnode_is_enabled(node));
390 DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
392 static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
398 node = ofnode_path("/translation-test@8000");
399 ut_assert(ofnode_valid(node));
400 addr = ofnode_get_addr(node);
401 size = ofnode_get_size(node);
402 ut_asserteq(0x8000, addr);
403 ut_asserteq(0x4000, size);
405 node = ofnode_path("/translation-test@8000/dev@1,100");
406 ut_assert(ofnode_valid(node));
407 addr = ofnode_get_addr(node);
408 size = ofnode_get_size(node);
409 ut_asserteq(0x9000, addr);
410 ut_asserteq(0x1000, size);
412 node = ofnode_path("/emul-mux-controller");
413 ut_assert(ofnode_valid(node));
414 addr = ofnode_get_addr(node);
415 size = ofnode_get_size(node);
416 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
417 ut_asserteq(FDT_SIZE_T_NONE, size);
419 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
420 ut_assert(ofnode_valid(node));
421 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
422 ut_asserteq_64(0x42, addr);
426 DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
428 static int dm_test_ofnode_get_path(struct unit_test_state *uts)
430 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
435 node = ofnode_path(path);
436 ut_assert(ofnode_valid(node));
438 res = ofnode_get_path(node, buf, 64);
440 ut_asserteq_str(path, buf);
442 res = ofnode_get_path(node, buf, 32);
443 ut_asserteq(-ENOSPC, res);
445 res = ofnode_get_path(ofnode_root(), buf, 32);
446 ut_asserteq_str("/", buf);
450 DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
452 static int dm_test_ofnode_conf(struct unit_test_state *uts)
454 ut_assert(!ofnode_conf_read_bool("missing"));
455 ut_assert(ofnode_conf_read_bool("testing-bool"));
457 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
458 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
460 ut_assertnull(ofnode_conf_read_str("missing"));
461 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
465 DM_TEST(dm_test_ofnode_conf, 0);
467 static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
469 const char compatible[] = "denx,u-boot-fdt-test";
473 ofnode_for_each_compatible_node(node, compatible) {
474 ut_assert(ofnode_device_is_compatible(node, compatible));
478 /* There should be at least one matching node */
483 DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
485 static int dm_test_ofnode_string(struct unit_test_state *uts)
491 node = ofnode_path("/a-test");
492 ut_assert(ofnode_valid(node));
495 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
496 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
497 ut_asserteq_str("test string", out);
498 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
500 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
501 ut_asserteq_str("test string", val[0]);
502 ut_assertnull(val[1]);
505 /* list of strings */
506 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
507 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
509 ut_asserteq_str("mux0", out);
510 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
512 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
514 ut_asserteq_str("mux0", val[0]);
515 ut_asserteq_str("mux1", val[1]);
516 ut_asserteq_str("mux2", val[2]);
517 ut_asserteq_str("mux3", val[3]);
518 ut_asserteq_str("mux4", val[4]);
519 ut_assertnull(val[5]);
522 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
524 ut_asserteq_str("mux4", out);
525 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
530 DM_TEST(dm_test_ofnode_string, 0);
532 static int dm_test_ofnode_string_err(struct unit_test_state *uts)
539 * Test error codes only on livetree, as they are different with
542 node = ofnode_path("/a-test");
543 ut_assert(ofnode_valid(node));
545 /* non-existent property */
546 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
547 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
549 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
552 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
553 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
555 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
558 /* badly formatted string list */
559 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
560 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
562 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
565 /* out of range / not found */
566 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
568 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
571 /* negative value for index is not allowed, so don't test for that */
573 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
574 "mux-control-names", 5,
579 DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
581 static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
583 ofnode eth_node, phy_node;
584 phy_interface_t mode;
587 eth_node = ofnode_path("/phy-test-eth");
588 ut_assert(ofnode_valid(eth_node));
590 mode = ofnode_read_phy_mode(eth_node);
591 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
593 phy_node = ofnode_get_phy_node(eth_node);
594 ut_assert(ofnode_valid(phy_node));
596 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
597 ut_asserteq_64(0x1, reg);
601 DM_TEST(dm_test_ofnode_get_phy, 0);
604 * make_ofnode_fdt() - Create an FDT for testing with ofnode
606 * The size is set to the minimum needed
609 * @fdt: Place to write FDT
610 * @size: Maximum size of space for fdt
612 static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
614 ut_assertok(fdt_create(fdt, size));
615 ut_assertok(fdt_finish_reservemap(fdt));
616 ut_assert(fdt_begin_node(fdt, "") >= 0);
618 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
619 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
620 ut_assertok(fdt_end_node(fdt));
622 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
623 ut_assertok(fdt_end_node(fdt));
625 ut_assertok(fdt_end_node(fdt));
626 ut_assertok(fdt_finish(fdt));
631 static int dm_test_ofnode_root(struct unit_test_state *uts)
638 /* Check that aliases work on the control FDT */
639 node = ofnode_get_aliases_node("ethernet3");
640 ut_assert(ofnode_valid(node));
641 ut_asserteq_str("sbe5", ofnode_get_name(node));
643 ut_assert(!oftree_valid(oftree_null()));
645 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
646 ret = get_oftree(uts, fdt, &tree);
648 /* skip the rest of this test if multiple FDTs are not supported */
649 if (ret == -EOVERFLOW)
653 ut_assert(oftree_valid(tree));
655 /* Make sure they don't work on this new tree */
656 node = oftree_path(tree, "mmc0");
657 ut_assert(!ofnode_valid(node));
659 /* It should appear in the new tree */
660 node = oftree_path(tree, "/new-mmc");
661 ut_assert(ofnode_valid(node));
663 /* ...and not in the control FDT */
664 node = oftree_path(oftree_default(), "/new-mmc");
665 ut_assert(!ofnode_valid(node));
671 DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
673 static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
678 /* Test enabling devices */
679 node = ofnode_path("/usb@2");
681 ut_assert(!ofnode_is_enabled(node));
682 ut_assertok(ofnode_set_enabled(node, true));
683 ut_asserteq(true, ofnode_is_enabled(node));
685 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
687 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
689 /* Test string property setting */
690 ut_assert(device_is_compatible(dev, "sandbox,usb"));
691 ofnode_write_string(node, "compatible", "gdsys,super-usb");
692 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
693 ofnode_write_string(node, "compatible", "sandbox,usb");
694 ut_assert(device_is_compatible(dev, "sandbox,usb"));
696 /* Test setting generic properties */
698 /* Non-existent in DTB */
699 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
700 /* reg = 0x42, size = 0x100 */
701 ut_assertok(ofnode_write_prop(node, "reg",
702 "\x00\x00\x00\x42\x00\x00\x01\x00", 8));
703 ut_asserteq(0x42, dev_read_addr(dev));
705 /* Test disabling devices */
706 device_remove(dev, DM_REMOVE_NORMAL);
709 ut_assert(ofnode_is_enabled(node));
710 ut_assertok(ofnode_set_enabled(node, false));
711 ut_assert(!ofnode_is_enabled(node));
715 DM_TEST(dm_test_ofnode_livetree_writing,
716 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
718 static int dm_test_ofnode_u32(struct unit_test_state *uts)
723 node = ofnode_path("/lcd");
724 ut_assert(ofnode_valid(node));
725 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
726 ut_assertok(ofnode_write_u32(node, "xres", 1367));
727 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
728 ut_assertok(ofnode_write_u32(node, "xres", 1366));
730 node = ofnode_path("/backlight");
731 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
733 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
734 ut_asserteq(16, val);
735 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
736 ut_asserteq(255, val);
737 ut_asserteq(-EOVERFLOW,
738 ofnode_read_u32_index(node, "brightness-levels", 9, &val));
739 ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
743 DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
745 static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
750 node = ofnode_path("/a-test");
751 ut_assert(ofnode_valid(node));
752 ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
753 ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
754 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
757 memset(val, '\0', sizeof(val));
758 ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
759 ut_asserteq(0, val[0]);
760 ut_asserteq(5678, val[1]);
761 ut_asserteq(9123, val[2]);
762 ut_asserteq(4567, val[3]);
763 ut_asserteq(0, val[4]);
764 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
769 DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
771 static int dm_test_ofnode_u64(struct unit_test_state *uts)
776 node = ofnode_path("/a-test");
777 ut_assert(ofnode_valid(node));
778 ut_assertok(ofnode_read_u64(node, "int64-value", &val));
779 ut_asserteq_64(0x1111222233334444, val);
780 ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
784 DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
786 static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
788 ofnode node, check, subnode;
791 node = ofnode_path("/lcd");
792 ut_assert(ofnode_valid(node));
793 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
794 check = ofnode_path("/lcd/edmund");
795 ut_asserteq(subnode.of_offset, check.of_offset);
796 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
797 ut_asserteq_str("/lcd/edmund", buf);
799 if (of_live_active()) {
800 struct device_node *child;
802 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
804 ut_asserteq_str("ed", child->name);
805 ut_asserteq_str("/lcd/ed", child->full_name);
806 check = ofnode_path("/lcd/ed");
807 ut_asserteq_ptr(child, check.np);
808 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
810 ut_asserteq_str("/lcd/ed", buf);
813 /* An existing node should be returned with -EEXIST */
814 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
815 ut_asserteq(subnode.of_offset, check.of_offset);
817 /* add a root node */
818 node = ofnode_path("/");
819 ut_assert(ofnode_valid(node));
820 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
821 check = ofnode_path("/lcd2");
822 ut_asserteq(subnode.of_offset, check.of_offset);
823 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
824 ut_asserteq_str("/lcd2", buf);
826 if (of_live_active()) {
831 * Make sure each of the three malloc()checks in
832 * of_add_subnode() work
834 for (i = 0; i < 3; i++) {
835 malloc_enable_testing(i);
836 start = ut_check_free();
837 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
839 ut_assertok(ut_check_delta(start));
842 /* This should pass since we allow 3 allocations */
843 malloc_enable_testing(3);
844 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
845 malloc_disable_testing();
848 /* write to the empty node */
849 ut_assertok(ofnode_write_string(subnode, "example", "text"));
853 DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
855 static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
857 ofnode node, subnode;
861 node = ofnode_path("/buttons");
864 /* we expect "compatible" for each node */
865 ofnode_for_each_prop(prop, node)
867 ut_asserteq(1, count);
869 /* there are two nodes, each with 2 properties */
870 ofnode_for_each_subnode(subnode, node)
871 ofnode_for_each_prop(prop, subnode)
873 ut_asserteq(5, count);
877 DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
879 static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
881 const char *compat = "denx,u-boot-fdt-test";
886 for (node = ofnode_null();
887 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
889 ut_asserteq(11, count);
893 DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
895 static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
897 ofnode node, subnode;
899 node = ofnode_path("/buttons");
901 subnode = ofnode_find_subnode(node, "btn1");
902 ut_assert(ofnode_valid(subnode));
903 ut_asserteq_str("btn1", ofnode_get_name(subnode));
905 subnode = ofnode_find_subnode(node, "btn");
906 ut_assert(!ofnode_valid(subnode));
910 DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
912 static int dm_test_ofnode_get_name(struct unit_test_state *uts)
916 node = ofnode_path("/buttons");
917 ut_assert(ofnode_valid(node));
918 ut_asserteq_str("buttons", ofnode_get_name(node));
919 ut_asserteq_str("", ofnode_get_name(ofnode_root()));
923 DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);