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,
104 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
106 /* check ofnode_device_is_compatible() with the 'other' FDT */
107 static int dm_test_ofnode_compatible_ot(struct unit_test_state *uts)
109 oftree otree = get_other_oftree(uts);
110 ofnode oroot = oftree_root(otree);
112 ut_assert(ofnode_valid(oroot));
113 ut_assert(ofnode_device_is_compatible(oroot, "sandbox-other"));
117 DM_TEST(dm_test_ofnode_compatible_ot, UT_TESTF_OTHER_FDT);
119 static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
121 /* test invalid phandle */
122 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
123 ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
125 /* test first valid phandle */
126 ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
128 /* test unknown phandle */
129 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
131 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
135 DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
137 static int dm_test_ofnode_get_by_phandle_ot(struct unit_test_state *uts)
139 oftree otree = get_other_oftree(uts);
142 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
143 node = oftree_get_by_phandle(otree, 1);
144 ut_assert(ofnode_valid(node));
145 ut_asserteq_str("target", ofnode_get_name(node));
149 DM_TEST(dm_test_ofnode_get_by_phandle_ot, UT_TESTF_OTHER_FDT);
151 static int check_prop_values(struct unit_test_state *uts, ofnode start,
152 const char *propname, const char *propval,
155 int proplen = strlen(propval) + 1;
160 /* Find first matching node, there should be at least one */
161 node = ofnode_by_prop_value(start, propname, propval, proplen);
162 ut_assert(ofnode_valid(node));
163 str = ofnode_read_string(node, propname);
164 ut_assert(str && !strcmp(str, propval));
166 /* Find the rest of the matching nodes */
169 node = ofnode_by_prop_value(node, propname, propval, proplen);
170 if (!ofnode_valid(node))
172 str = ofnode_read_string(node, propname);
173 ut_asserteq_str(propval, str);
176 ut_asserteq(expect_count, count);
181 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
183 ut_assertok(check_prop_values(uts, ofnode_null(), "compatible",
184 "denx,u-boot-fdt-test", 11));
188 DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
190 static int dm_test_ofnode_by_prop_value_ot(struct unit_test_state *uts)
192 oftree otree = get_other_oftree(uts);
194 ut_assertok(check_prop_values(uts, oftree_root(otree), "str-prop",
199 DM_TEST(dm_test_ofnode_by_prop_value_ot, UT_TESTF_OTHER_FDT);
201 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
203 struct fmap_entry entry;
206 node = ofnode_path("/cros-ec/flash");
207 ut_assert(ofnode_valid(node));
208 ut_assertok(ofnode_read_fmap_entry(node, &entry));
209 ut_asserteq(0x08000000, entry.offset);
210 ut_asserteq(0x20000, entry.length);
214 DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
216 static int dm_test_ofnode_read(struct unit_test_state *uts)
222 node = ofnode_path("/a-test");
223 ut_assert(ofnode_valid(node));
225 val = ofnode_read_prop(node, "int-value", &size);
226 ut_assertnonnull(val);
227 ut_asserteq(4, size);
228 ut_asserteq(1234, fdt32_to_cpu(val[0]));
230 val = ofnode_read_prop(node, "missing", &size);
232 ut_asserteq(-FDT_ERR_NOTFOUND, size);
234 /* Check it works without a size parameter */
235 val = ofnode_read_prop(node, "missing", NULL);
240 DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
242 static int dm_test_ofnode_read_ot(struct unit_test_state *uts)
244 oftree otree = get_other_oftree(uts);
249 node = oftree_path(otree, "/node/subnode");
250 ut_assert(ofnode_valid(node));
252 val = ofnode_read_prop(node, "str-prop", &size);
253 ut_assertnonnull(val);
254 ut_asserteq_str("other", val);
255 ut_asserteq(6, size);
259 DM_TEST(dm_test_ofnode_read_ot, UT_TESTF_OTHER_FDT);
261 static int dm_test_ofnode_phandle(struct unit_test_state *uts)
263 struct ofnode_phandle_args args;
266 const char prop[] = "test-gpios";
267 const char cell[] = "#gpio-cells";
268 const char prop2[] = "phandle-value";
270 node = ofnode_path("/a-test");
271 ut_assert(ofnode_valid(node));
273 /* Test ofnode_count_phandle_with_args with cell name */
274 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
275 ut_asserteq(-ENOENT, ret);
276 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
277 ut_asserteq(-EINVAL, ret);
278 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
281 /* Test ofnode_parse_phandle_with_args with cell name */
282 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
284 ut_asserteq(-ENOENT, ret);
285 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
287 ut_asserteq(-EINVAL, ret);
288 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
290 ut_asserteq(1, args.args_count);
291 ut_asserteq(1, args.args[0]);
292 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
294 ut_asserteq(1, args.args_count);
295 ut_asserteq(4, args.args[0]);
296 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
298 ut_asserteq(5, args.args_count);
299 ut_asserteq(5, args.args[0]);
300 ut_asserteq(1, args.args[4]);
301 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
302 ut_asserteq(-ENOENT, ret);
303 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
305 ut_asserteq(1, args.args_count);
306 ut_asserteq(12, args.args[0]);
307 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
308 ut_asserteq(-ENOENT, ret);
310 /* Test ofnode_count_phandle_with_args with cell count */
311 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
312 ut_asserteq(-ENOENT, ret);
313 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
316 /* Test ofnode_parse_phandle_with_args with cell count */
317 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
319 ut_asserteq(1, ofnode_valid(args.node));
320 ut_asserteq(1, args.args_count);
321 ut_asserteq(10, args.args[0]);
322 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
323 ut_asserteq(-EINVAL, ret);
324 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
326 ut_asserteq(1, ofnode_valid(args.node));
327 ut_asserteq(1, args.args_count);
328 ut_asserteq(30, args.args[0]);
329 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
330 ut_asserteq(-ENOENT, ret);
334 DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
336 static int dm_test_ofnode_phandle_ot(struct unit_test_state *uts)
338 oftree otree = get_other_oftree(uts);
339 struct ofnode_phandle_args args;
343 node = oftree_path(otree, "/node");
345 /* Test ofnode_count_phandle_with_args with cell name */
346 ret = ofnode_count_phandle_with_args(node, "missing", "#gpio-cells", 0);
347 ut_asserteq(-ENOENT, ret);
348 ret = ofnode_count_phandle_with_args(node, "target", "#invalid", 0);
349 ut_asserteq(-EINVAL, ret);
350 ret = ofnode_count_phandle_with_args(node, "target", "#gpio-cells", 0);
353 ret = ofnode_parse_phandle_with_args(node, "target", "#gpio-cells", 0,
356 ut_asserteq(2, args.args_count);
357 ut_asserteq(3, args.args[0]);
358 ut_asserteq(4, args.args[1]);
362 DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT);
364 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
371 str = ofnode_read_chosen_string("setting");
372 ut_assertnonnull(str);
373 ut_asserteq_str("sunrise ohoka", str);
374 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
376 node = ofnode_get_chosen_node("other-node");
377 ut_assert(ofnode_valid(node));
378 ut_asserteq_str("c-test@5", ofnode_get_name(node));
380 node = ofnode_get_chosen_node("setting");
381 ut_assert(!ofnode_valid(node));
383 val = ofnode_read_chosen_prop("int-values", &size);
384 ut_assertnonnull(val);
385 ut_asserteq(8, size);
386 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
387 ut_asserteq(72993, fdt32_to_cpu(val[1]));
391 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
393 static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
399 node = ofnode_get_aliases_node("ethernet3");
400 ut_assert(ofnode_valid(node));
401 ut_asserteq_str("sbe5", ofnode_get_name(node));
403 node = ofnode_get_aliases_node("unknown");
404 ut_assert(!ofnode_valid(node));
406 val = ofnode_read_aliases_prop("spi0", &size);
407 ut_assertnonnull(val);
408 ut_asserteq(7, size);
409 ut_asserteq_str("/spi@0", (const char *)val);
413 DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
415 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
417 ofnode node, child_node;
420 node = ofnode_path("/i-test");
421 ut_assert(ofnode_valid(node));
423 val = ofnode_get_child_count(node);
426 child_node = ofnode_first_subnode(node);
427 ut_assert(ofnode_valid(child_node));
428 val = ofnode_get_child_count(child_node);
433 DM_TEST(dm_test_ofnode_get_child_count,
434 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
436 static int dm_test_ofnode_get_child_count_ot(struct unit_test_state *uts)
438 oftree otree = get_other_oftree(uts);
439 ofnode node, child_node;
442 node = oftree_path(otree, "/node");
443 ut_assert(ofnode_valid(node));
445 val = ofnode_get_child_count(node);
448 child_node = ofnode_first_subnode(node);
449 ut_assert(ofnode_valid(child_node));
450 val = ofnode_get_child_count(child_node);
455 DM_TEST(dm_test_ofnode_get_child_count_ot, UT_TESTF_OTHER_FDT);
457 static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
459 ofnode root_node = ofnode_path("/");
460 ofnode node = ofnode_path("/usb@0");
462 ut_assert(ofnode_is_enabled(root_node));
463 ut_assert(!ofnode_is_enabled(node));
467 DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
469 static int dm_test_ofnode_is_enabled_ot(struct unit_test_state *uts)
471 oftree otree = get_other_oftree(uts);
472 ofnode root_node = oftree_root(otree);
473 ofnode node = oftree_path(otree, "/target");
475 ut_assert(ofnode_is_enabled(root_node));
476 ut_assert(!ofnode_is_enabled(node));
480 DM_TEST(dm_test_ofnode_is_enabled_ot, UT_TESTF_OTHER_FDT);
482 static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
488 node = ofnode_path("/translation-test@8000");
489 ut_assert(ofnode_valid(node));
490 addr = ofnode_get_addr(node);
491 size = ofnode_get_size(node);
492 ut_asserteq(0x8000, addr);
493 ut_asserteq(0x4000, size);
495 node = ofnode_path("/translation-test@8000/dev@1,100");
496 ut_assert(ofnode_valid(node));
497 addr = ofnode_get_addr(node);
498 size = ofnode_get_size(node);
499 ut_asserteq(0x9000, addr);
500 ut_asserteq(0x1000, size);
502 node = ofnode_path("/emul-mux-controller");
503 ut_assert(ofnode_valid(node));
504 addr = ofnode_get_addr(node);
505 size = ofnode_get_size(node);
506 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
507 ut_asserteq(FDT_SIZE_T_NONE, size);
509 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
510 ut_assert(ofnode_valid(node));
511 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
512 ut_asserteq_64(0x42, addr);
516 DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
518 static int dm_test_ofnode_get_reg_ot(struct unit_test_state *uts)
520 oftree otree = get_other_oftree(uts);
521 ofnode node = oftree_path(otree, "/target");
524 addr = ofnode_get_addr(node);
525 ut_asserteq(0x8000, addr);
529 DM_TEST(dm_test_ofnode_get_reg_ot, UT_TESTF_OTHER_FDT);
531 static int dm_test_ofnode_get_path(struct unit_test_state *uts)
533 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
537 node = ofnode_path(path);
538 ut_assert(ofnode_valid(node));
540 ut_assertok(ofnode_get_path(node, buf, sizeof(buf)));
541 ut_asserteq_str(path, buf);
543 ut_asserteq(-ENOSPC, ofnode_get_path(node, buf, 32));
545 ut_assertok(ofnode_get_path(ofnode_root(), buf, 32));
546 ut_asserteq_str("/", buf);
550 DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
552 static int dm_test_ofnode_get_path_ot(struct unit_test_state *uts)
554 oftree otree = get_other_oftree(uts);
555 const char *path = "/node/subnode";
556 ofnode node = oftree_path(otree, path);
559 ut_assert(ofnode_valid(node));
561 ut_assertok(ofnode_get_path(node, buf, sizeof(buf)));
562 ut_asserteq_str(path, buf);
564 ut_assertok(ofnode_get_path(oftree_root(otree), buf, 32));
565 ut_asserteq_str("/", buf);
569 DM_TEST(dm_test_ofnode_get_path_ot, UT_TESTF_OTHER_FDT);
571 static int dm_test_ofnode_conf(struct unit_test_state *uts)
573 ut_assert(!ofnode_conf_read_bool("missing"));
574 ut_assert(ofnode_conf_read_bool("testing-bool"));
576 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
577 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
579 ut_assertnull(ofnode_conf_read_str("missing"));
580 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
584 DM_TEST(dm_test_ofnode_conf, 0);
586 static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
588 const char compatible[] = "denx,u-boot-fdt-test";
592 ofnode_for_each_compatible_node(node, compatible) {
593 ut_assert(ofnode_device_is_compatible(node, compatible));
597 /* There should be at least one matching node */
602 DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
604 static int dm_test_ofnode_string(struct unit_test_state *uts)
610 node = ofnode_path("/a-test");
611 ut_assert(ofnode_valid(node));
614 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
615 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
616 ut_asserteq_str("test string", out);
617 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
619 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
620 ut_asserteq_str("test string", val[0]);
621 ut_assertnull(val[1]);
624 /* list of strings */
625 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
626 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
628 ut_asserteq_str("mux0", out);
629 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
631 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
633 ut_asserteq_str("mux0", val[0]);
634 ut_asserteq_str("mux1", val[1]);
635 ut_asserteq_str("mux2", val[2]);
636 ut_asserteq_str("mux3", val[3]);
637 ut_asserteq_str("mux4", val[4]);
638 ut_assertnull(val[5]);
641 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
643 ut_asserteq_str("mux4", out);
644 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
649 DM_TEST(dm_test_ofnode_string, 0);
651 static int dm_test_ofnode_string_err(struct unit_test_state *uts)
658 * Test error codes only on livetree, as they are different with
661 node = ofnode_path("/a-test");
662 ut_assert(ofnode_valid(node));
664 /* non-existent property */
665 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
666 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
668 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
671 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
672 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
674 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
677 /* badly formatted string list */
678 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
679 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
681 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
684 /* out of range / not found */
685 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
687 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
690 /* negative value for index is not allowed, so don't test for that */
692 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
693 "mux-control-names", 5,
698 DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
700 static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
702 ofnode eth_node, phy_node;
703 phy_interface_t mode;
706 eth_node = ofnode_path("/phy-test-eth");
707 ut_assert(ofnode_valid(eth_node));
709 mode = ofnode_read_phy_mode(eth_node);
710 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
712 phy_node = ofnode_get_phy_node(eth_node);
713 ut_assert(ofnode_valid(phy_node));
715 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
716 ut_asserteq_64(0x1, reg);
720 DM_TEST(dm_test_ofnode_get_phy, 0);
723 * make_ofnode_fdt() - Create an FDT for testing with ofnode
725 * The size is set to the minimum needed
728 * @fdt: Place to write FDT
729 * @size: Maximum size of space for fdt
730 * @id: id value to add to the tree ('id' property in root node)
732 static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size,
735 ut_assertok(fdt_create(fdt, size));
736 ut_assertok(fdt_finish_reservemap(fdt));
737 ut_assert(fdt_begin_node(fdt, "") >= 0);
739 ut_assertok(fdt_property_u32(fdt, "id", id));
741 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
742 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
743 ut_assertok(fdt_end_node(fdt));
745 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
746 ut_assertok(fdt_end_node(fdt));
748 ut_assertok(fdt_end_node(fdt));
749 ut_assertok(fdt_finish(fdt));
754 static int dm_test_ofnode_root(struct unit_test_state *uts)
761 /* Check that aliases work on the control FDT */
762 node = ofnode_get_aliases_node("ethernet3");
763 ut_assert(ofnode_valid(node));
764 ut_asserteq_str("sbe5", ofnode_get_name(node));
766 ut_assert(!oftree_valid(oftree_null()));
768 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt), 0));
769 ret = get_oftree(uts, fdt, &tree);
771 /* skip the rest of this test if multiple FDTs are not supported */
772 if (ret == -EOVERFLOW)
776 ut_assert(oftree_valid(tree));
778 /* Make sure they don't work on this new tree */
779 node = oftree_path(tree, "mmc0");
780 ut_assert(!ofnode_valid(node));
782 /* It should appear in the new tree */
783 node = oftree_path(tree, "/new-mmc");
784 ut_assert(ofnode_valid(node));
786 /* ...and not in the control FDT */
787 node = oftree_path(oftree_default(), "/new-mmc");
788 ut_assert(!ofnode_valid(node));
794 DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
796 static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
801 /* Test enabling devices */
802 node = ofnode_path("/usb@2");
804 ut_assert(!ofnode_is_enabled(node));
805 ut_assertok(ofnode_set_enabled(node, true));
806 ut_asserteq(true, ofnode_is_enabled(node));
808 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
810 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
812 /* Test string property setting */
813 ut_assert(device_is_compatible(dev, "sandbox,usb"));
814 ofnode_write_string(node, "compatible", "gdsys,super-usb");
815 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
816 ofnode_write_string(node, "compatible", "sandbox,usb");
817 ut_assert(device_is_compatible(dev, "sandbox,usb"));
819 /* Test setting generic properties */
821 /* Non-existent in DTB */
822 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
823 /* reg = 0x42, size = 0x100 */
824 ut_assertok(ofnode_write_prop(node, "reg",
825 "\x00\x00\x00\x42\x00\x00\x01\x00", 8));
826 ut_asserteq(0x42, dev_read_addr(dev));
828 /* Test disabling devices */
829 device_remove(dev, DM_REMOVE_NORMAL);
832 ut_assert(ofnode_is_enabled(node));
833 ut_assertok(ofnode_set_enabled(node, false));
834 ut_assert(!ofnode_is_enabled(node));
838 DM_TEST(dm_test_ofnode_livetree_writing,
839 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
841 static int dm_test_ofnode_u32(struct unit_test_state *uts)
846 node = ofnode_path("/lcd");
847 ut_assert(ofnode_valid(node));
848 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
849 ut_assertok(ofnode_write_u32(node, "xres", 1367));
850 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
851 ut_assertok(ofnode_write_u32(node, "xres", 1366));
853 node = ofnode_path("/backlight");
854 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
856 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
857 ut_asserteq(16, val);
858 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
859 ut_asserteq(255, val);
860 ut_asserteq(-EOVERFLOW,
861 ofnode_read_u32_index(node, "brightness-levels", 9, &val));
862 ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
866 DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
868 static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
873 node = ofnode_path("/a-test");
874 ut_assert(ofnode_valid(node));
875 ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
876 ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
877 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
880 memset(val, '\0', sizeof(val));
881 ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
882 ut_asserteq(0, val[0]);
883 ut_asserteq(5678, val[1]);
884 ut_asserteq(9123, val[2]);
885 ut_asserteq(4567, val[3]);
886 ut_asserteq(0, val[4]);
887 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
892 DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
894 static int dm_test_ofnode_u64(struct unit_test_state *uts)
899 node = ofnode_path("/a-test");
900 ut_assert(ofnode_valid(node));
901 ut_assertok(ofnode_read_u64(node, "int64-value", &val));
902 ut_asserteq_64(0x1111222233334444, val);
903 ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
907 DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
909 static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
911 ofnode node, check, subnode;
914 node = ofnode_path("/lcd");
915 ut_assert(ofnode_valid(node));
916 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
917 check = ofnode_path("/lcd/edmund");
918 ut_asserteq(subnode.of_offset, check.of_offset);
919 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
920 ut_asserteq_str("/lcd/edmund", buf);
922 if (of_live_active()) {
923 struct device_node *child;
925 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
927 ut_asserteq_str("ed", child->name);
928 ut_asserteq_str("/lcd/ed", child->full_name);
929 check = ofnode_path("/lcd/ed");
930 ut_asserteq_ptr(child, check.np);
931 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
933 ut_asserteq_str("/lcd/ed", buf);
936 /* An existing node should be returned with -EEXIST */
937 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
938 ut_asserteq(subnode.of_offset, check.of_offset);
940 /* add a root node */
941 node = ofnode_path("/");
942 ut_assert(ofnode_valid(node));
943 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
944 check = ofnode_path("/lcd2");
945 ut_asserteq(subnode.of_offset, check.of_offset);
946 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
947 ut_asserteq_str("/lcd2", buf);
949 if (of_live_active()) {
954 * Make sure each of the three malloc()checks in
955 * of_add_subnode() work
957 for (i = 0; i < 3; i++) {
958 malloc_enable_testing(i);
959 start = ut_check_free();
960 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
962 ut_assertok(ut_check_delta(start));
965 /* This should pass since we allow 3 allocations */
966 malloc_enable_testing(3);
967 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
968 malloc_disable_testing();
971 /* write to the empty node */
972 ut_assertok(ofnode_write_string(subnode, "example", "text"));
976 DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
978 static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
980 ofnode node, subnode;
984 node = ofnode_path("/buttons");
987 /* we expect "compatible" for each node */
988 ofnode_for_each_prop(prop, node)
990 ut_asserteq(1, count);
992 /* there are two nodes, each with 2 properties */
993 ofnode_for_each_subnode(subnode, node)
994 ofnode_for_each_prop(prop, subnode)
996 ut_asserteq(5, count);
1000 DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
1002 static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
1004 const char *compat = "denx,u-boot-fdt-test";
1009 for (node = ofnode_null();
1010 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
1012 ut_asserteq(11, count);
1016 DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
1018 static int dm_test_ofnode_by_compatible_ot(struct unit_test_state *uts)
1020 const char *compat = "sandbox-other2";
1021 oftree otree = get_other_oftree(uts);
1026 for (node = oftree_root(otree);
1027 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
1029 ut_asserteq(2, count);
1033 DM_TEST(dm_test_ofnode_by_compatible_ot, UT_TESTF_OTHER_FDT);
1035 static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
1037 ofnode node, subnode;
1039 node = ofnode_path("/buttons");
1041 subnode = ofnode_find_subnode(node, "btn1");
1042 ut_assert(ofnode_valid(subnode));
1043 ut_asserteq_str("btn1", ofnode_get_name(subnode));
1045 subnode = ofnode_find_subnode(node, "btn");
1046 ut_assert(!ofnode_valid(subnode));
1050 DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
1052 static int dm_test_ofnode_find_subnode_ot(struct unit_test_state *uts)
1054 oftree otree = get_other_oftree(uts);
1055 ofnode node, subnode;
1057 node = oftree_path(otree, "/node");
1059 subnode = ofnode_find_subnode(node, "subnode");
1060 ut_assert(ofnode_valid(subnode));
1061 ut_asserteq_str("subnode", ofnode_get_name(subnode));
1063 subnode = ofnode_find_subnode(node, "btn");
1064 ut_assert(!ofnode_valid(subnode));
1068 DM_TEST(dm_test_ofnode_find_subnode_ot, UT_TESTF_OTHER_FDT);
1070 static int dm_test_ofnode_get_name(struct unit_test_state *uts)
1074 node = ofnode_path("/buttons");
1075 ut_assert(ofnode_valid(node));
1076 ut_asserteq_str("buttons", ofnode_get_name(node));
1077 ut_asserteq_str("", ofnode_get_name(ofnode_root()));
1081 DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);
1083 /* try to access more FDTs than is supported */
1084 static int dm_test_ofnode_too_many(struct unit_test_state *uts)
1086 const int max_trees = CONFIG_IS_ENABLED(OFNODE_MULTI_TREE,
1087 (CONFIG_OFNODE_MULTI_TREE_MAX), (1));
1088 const int fdt_size = 256;
1089 const int num_trees = max_trees + 1;
1090 char fdt[num_trees][fdt_size];
1093 for (i = 0; i < num_trees; i++) {
1097 ut_assertok(make_ofnode_fdt(uts, fdt[i], fdt_size, i));
1098 ret = get_oftree(uts, fdt[i], &tree);
1101 * With flat tree we have the control FDT using one slot. Live
1102 * tree has no limit since it uses pointers, not integer tree
1105 if (of_live_active() || i < max_trees - 1) {
1109 * tree should be invalid when we try to register too
1112 ut_asserteq(-EOVERFLOW, ret);
1118 DM_TEST(dm_test_ofnode_too_many, UT_TESTF_SCAN_FDT);