dm: core: Create a function to get a live tree in a test
[platform/kernel/u-boot.git] / test / dm / ofnode.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2022 Google LLC
4  *
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)
8  *
9  * The 'other' ones have an _ot suffix.
10  *
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.
17  */
18
19 #include <common.h>
20 #include <dm.h>
21 #include <log.h>
22 #include <of_live.h>
23 #include <dm/device-internal.h>
24 #include <dm/lists.h>
25 #include <dm/of_extra.h>
26 #include <dm/root.h>
27 #include <dm/test.h>
28 #include <dm/uclass-internal.h>
29 #include <test/test.h>
30 #include <test/ut.h>
31
32 /**
33  * get_other_oftree() - Convert a flat tree into an oftree object
34  *
35  * @uts: Test state
36  * @return: oftree object for the 'other' FDT (see sandbox' other.dts)
37  */
38 oftree get_other_oftree(struct unit_test_state *uts)
39 {
40         oftree tree;
41
42         if (of_live_active())
43                 tree = oftree_from_np(uts->of_other);
44         else
45                 tree = oftree_from_fdt(uts->other_fdt);
46
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");
50
51         return tree;
52 }
53
54 /**
55  * get_oftree() - Convert a flat tree into an oftree object
56  *
57  * @uts: Test state
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
62  * oftree_ensure())
63  */
64 int get_oftree(struct unit_test_state *uts, void *fdt, oftree *treep)
65 {
66         oftree tree;
67
68         if (of_live_active()) {
69                 struct device_node *root;
70
71                 ut_assertok(unflatten_device_tree(fdt, &root));
72                 tree = oftree_from_np(root);
73         } else {
74                 tree = oftree_from_fdt(fdt);
75                 if (!oftree_valid(tree))
76                         return -EOVERFLOW;
77         }
78         *treep = tree;
79
80         return 0;
81 }
82
83 /**
84  * free_oftree() - Free memory used by get_oftree()
85  *
86  * @tree: Tree to free
87  */
88 void free_oftree(oftree tree)
89 {
90         if (of_live_active())
91                 free(tree.np);
92 }
93
94 static int dm_test_ofnode_compatible(struct unit_test_state *uts)
95 {
96         ofnode root_node = ofnode_path("/");
97
98         ut_assert(ofnode_valid(root_node));
99         ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
100
101         return 0;
102 }
103 DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
104
105 static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
106 {
107         /* test invalid phandle */
108         ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
109         ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
110
111         /* test first valid phandle */
112         ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
113
114         /* test unknown phandle */
115         ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
116
117         ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
118
119         return 0;
120 }
121 DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
122
123 static int dm_test_ofnode_get_by_phandle_ot(struct unit_test_state *uts)
124 {
125         oftree otree = get_other_oftree(uts);
126         ofnode node;
127
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));
132
133         return 0;
134 }
135 DM_TEST(dm_test_ofnode_get_by_phandle_ot, UT_TESTF_OTHER_FDT);
136
137 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
138 {
139         const char propname[] = "compatible";
140         const char propval[] = "denx,u-boot-fdt-test";
141         const char *str;
142         ofnode node = ofnode_null();
143
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));
149
150         /* Find the rest of the matching nodes */
151         while (true) {
152                 node = ofnode_by_prop_value(node, propname, propval,
153                                             sizeof(propval));
154                 if (!ofnode_valid(node))
155                         break;
156                 str = ofnode_read_string(node, propname);
157                 ut_assert(str && !strcmp(str, propval));
158         }
159
160         return 0;
161 }
162 DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
163
164 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
165 {
166         struct fmap_entry entry;
167         ofnode node;
168
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);
174
175         return 0;
176 }
177 DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
178
179 static int dm_test_ofnode_read(struct unit_test_state *uts)
180 {
181         const u32 *val;
182         ofnode node;
183         int size;
184
185         node = ofnode_path("/a-test");
186         ut_assert(ofnode_valid(node));
187
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]));
192
193         val = ofnode_read_prop(node, "missing", &size);
194         ut_assertnull(val);
195         ut_asserteq(-FDT_ERR_NOTFOUND, size);
196
197         /* Check it works without a size parameter */
198         val = ofnode_read_prop(node, "missing", NULL);
199         ut_assertnull(val);
200
201         return 0;
202 }
203 DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
204
205 static int dm_test_ofnode_phandle(struct unit_test_state *uts)
206 {
207         struct ofnode_phandle_args args;
208         ofnode node;
209         int ret;
210         const char prop[] = "test-gpios";
211         const char cell[] = "#gpio-cells";
212         const char prop2[] = "phandle-value";
213
214         node = ofnode_path("/a-test");
215         ut_assert(ofnode_valid(node));
216
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);
223         ut_asserteq(5, ret);
224
225         /* Test ofnode_parse_phandle_with_args with cell name */
226         ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
227                                              &args);
228         ut_asserteq(-ENOENT, ret);
229         ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
230                                              &args);
231         ut_asserteq(-EINVAL, ret);
232         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
233         ut_assertok(ret);
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);
237         ut_assertok(ret);
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);
241         ut_assertok(ret);
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);
248         ut_assertok(ret);
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);
253
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);
258         ut_asserteq(3, ret);
259
260         /* Test ofnode_parse_phandle_with_args with cell count */
261         ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
262         ut_assertok(ret);
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);
269         ut_assertok(ret);
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);
275
276         return 0;
277 }
278 DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
279
280 static int dm_test_ofnode_phandle_ot(struct unit_test_state *uts)
281 {
282         oftree otree = get_other_oftree(uts);
283         struct ofnode_phandle_args args;
284         ofnode node;
285         int ret;
286
287         node = oftree_path(otree, "/node");
288
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);
295         ut_asserteq(1, ret);
296
297         ret = ofnode_parse_phandle_with_args(node, "target", "#gpio-cells", 0,
298                                              0, &args);
299         ut_assertok(ret);
300         ut_asserteq(2, args.args_count);
301         ut_asserteq(3, args.args[0]);
302         ut_asserteq(4, args.args[1]);
303
304         return 0;
305 }
306 DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT);
307
308 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
309 {
310         const char *str;
311         const u32 *val;
312         ofnode node;
313         int size;
314
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"));
319
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));
323
324         node = ofnode_get_chosen_node("setting");
325         ut_assert(!ofnode_valid(node));
326
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]));
332
333         return 0;
334 }
335 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
336
337 static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
338 {
339         const void *val;
340         ofnode node;
341         int size;
342
343         node = ofnode_get_aliases_node("ethernet3");
344         ut_assert(ofnode_valid(node));
345         ut_asserteq_str("sbe5", ofnode_get_name(node));
346
347         node = ofnode_get_aliases_node("unknown");
348         ut_assert(!ofnode_valid(node));
349
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);
354
355         return 0;
356 }
357 DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
358
359 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
360 {
361         ofnode node, child_node;
362         u32 val;
363
364         node = ofnode_path("/i-test");
365         ut_assert(ofnode_valid(node));
366
367         val = ofnode_get_child_count(node);
368         ut_asserteq(3, val);
369
370         child_node = ofnode_first_subnode(node);
371         ut_assert(ofnode_valid(child_node));
372         val = ofnode_get_child_count(child_node);
373         ut_asserteq(0, val);
374
375         return 0;
376 }
377 DM_TEST(dm_test_ofnode_get_child_count,
378         UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
379
380 static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
381 {
382         ofnode root_node = ofnode_path("/");
383         ofnode node = ofnode_path("/usb@0");
384
385         ut_assert(ofnode_is_enabled(root_node));
386         ut_assert(!ofnode_is_enabled(node));
387
388         return 0;
389 }
390 DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
391
392 static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
393 {
394         ofnode node;
395         fdt_addr_t addr;
396         fdt_size_t size;
397
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);
404
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);
411
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);
418
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);
423
424         return 0;
425 }
426 DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
427
428 static int dm_test_ofnode_get_path(struct unit_test_state *uts)
429 {
430         const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
431         char buf[64];
432         ofnode node;
433         int res;
434
435         node = ofnode_path(path);
436         ut_assert(ofnode_valid(node));
437
438         res = ofnode_get_path(node, buf, 64);
439         ut_asserteq(0, res);
440         ut_asserteq_str(path, buf);
441
442         res = ofnode_get_path(node, buf, 32);
443         ut_asserteq(-ENOSPC, res);
444
445         res = ofnode_get_path(ofnode_root(), buf, 32);
446         ut_asserteq_str("/", buf);
447
448         return 0;
449 }
450 DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
451
452 static int dm_test_ofnode_conf(struct unit_test_state *uts)
453 {
454         ut_assert(!ofnode_conf_read_bool("missing"));
455         ut_assert(ofnode_conf_read_bool("testing-bool"));
456
457         ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
458         ut_asserteq(6, ofnode_conf_read_int("missing", 6));
459
460         ut_assertnull(ofnode_conf_read_str("missing"));
461         ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
462
463         return 0;
464 }
465 DM_TEST(dm_test_ofnode_conf, 0);
466
467 static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
468 {
469         const char compatible[] = "denx,u-boot-fdt-test";
470         bool found = false;
471         ofnode node;
472
473         ofnode_for_each_compatible_node(node, compatible) {
474                 ut_assert(ofnode_device_is_compatible(node, compatible));
475                 found = true;
476         }
477
478         /* There should be at least one matching node */
479         ut_assert(found);
480
481         return 0;
482 }
483 DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
484
485 static int dm_test_ofnode_string(struct unit_test_state *uts)
486 {
487         const char **val;
488         const char *out;
489         ofnode node;
490
491         node = ofnode_path("/a-test");
492         ut_assert(ofnode_valid(node));
493
494         /* single string */
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",
499                                                 "test string"));
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]);
503         free(val);
504
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,
508                                              &out));
509         ut_asserteq_str("mux0", out);
510         ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
511                                                 "mux0"));
512         ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
513                                                &val));
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]);
520         free(val);
521
522         ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
523                                              &out));
524         ut_asserteq_str("mux4", out);
525         ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
526                                                 "mux4"));
527
528         return 0;
529 }
530 DM_TEST(dm_test_ofnode_string, 0);
531
532 static int dm_test_ofnode_string_err(struct unit_test_state *uts)
533 {
534         const char **val;
535         const char *out;
536         ofnode node;
537
538         /*
539          * Test error codes only on livetree, as they are different with
540          * flattree
541          */
542         node = ofnode_path("/a-test");
543         ut_assert(ofnode_valid(node));
544
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,
548                                                       &out));
549         ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
550
551         /* empty property */
552         ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
553         ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
554                                                        &out));
555         ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
556                                                      &val));
557
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,
561                                                        &out));
562         ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
563                                                      &val));
564
565         /* out of range / not found */
566         ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
567                                                        &out));
568         ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
569                                                        "other"));
570
571         /* negative value for index is not allowed, so don't test for that */
572
573         ut_asserteq(-ENODATA, ofnode_read_string_index(node,
574                                                        "mux-control-names", 5,
575                                                        &out));
576
577         return 0;
578 }
579 DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
580
581 static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
582 {
583         ofnode eth_node, phy_node;
584         phy_interface_t mode;
585         u32 reg;
586
587         eth_node = ofnode_path("/phy-test-eth");
588         ut_assert(ofnode_valid(eth_node));
589
590         mode = ofnode_read_phy_mode(eth_node);
591         ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
592
593         phy_node = ofnode_get_phy_node(eth_node);
594         ut_assert(ofnode_valid(phy_node));
595
596         reg = ofnode_read_u32_default(phy_node, "reg", -1U);
597         ut_asserteq_64(0x1, reg);
598
599         return 0;
600 }
601 DM_TEST(dm_test_ofnode_get_phy, 0);
602
603 /**
604  * make_ofnode_fdt() - Create an FDT for testing with ofnode
605  *
606  * The size is set to the minimum needed
607  *
608  * @uts: Test state
609  * @fdt: Place to write FDT
610  * @size: Maximum size of space for fdt
611  */
612 static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
613 {
614         ut_assertok(fdt_create(fdt, size));
615         ut_assertok(fdt_finish_reservemap(fdt));
616         ut_assert(fdt_begin_node(fdt, "") >= 0);
617
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));
621
622         ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
623         ut_assertok(fdt_end_node(fdt));
624
625         ut_assertok(fdt_end_node(fdt));
626         ut_assertok(fdt_finish(fdt));
627
628         return 0;
629 }
630
631 static int dm_test_ofnode_root(struct unit_test_state *uts)
632 {
633         char fdt[256];
634         oftree tree;
635         ofnode node;
636         int ret;
637
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));
642
643         ut_assert(!oftree_valid(oftree_null()));
644
645         ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
646         ret = get_oftree(uts, fdt, &tree);
647
648         /* skip the rest of this test if multiple FDTs are not supported */
649         if (ret == -EOVERFLOW)
650                 return 0;
651
652         ut_assertok(ret);
653         ut_assert(oftree_valid(tree));
654
655         /* Make sure they don't work on this new tree */
656         node = oftree_path(tree, "mmc0");
657         ut_assert(!ofnode_valid(node));
658
659         /* It should appear in the new tree */
660         node = oftree_path(tree, "/new-mmc");
661         ut_assert(ofnode_valid(node));
662
663         /* ...and not in the control FDT */
664         node = oftree_path(oftree_default(), "/new-mmc");
665         ut_assert(!ofnode_valid(node));
666
667         free_oftree(tree);
668
669         return 0;
670 }
671 DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
672
673 static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
674 {
675         struct udevice *dev;
676         ofnode node;
677
678         /* Test enabling devices */
679         node = ofnode_path("/usb@2");
680
681         ut_assert(!ofnode_is_enabled(node));
682         ut_assertok(ofnode_set_enabled(node, true));
683         ut_asserteq(true, ofnode_is_enabled(node));
684
685         device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
686                                    &dev);
687         ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
688
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"));
695
696         /* Test setting generic properties */
697
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));
704
705         /* Test disabling devices */
706         device_remove(dev, DM_REMOVE_NORMAL);
707         device_unbind(dev);
708
709         ut_assert(ofnode_is_enabled(node));
710         ut_assertok(ofnode_set_enabled(node, false));
711         ut_assert(!ofnode_is_enabled(node));
712
713         return 0;
714 }
715 DM_TEST(dm_test_ofnode_livetree_writing,
716         UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
717
718 static int dm_test_ofnode_u32(struct unit_test_state *uts)
719 {
720         ofnode node;
721         u32 val;
722
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));
729
730         node = ofnode_path("/backlight");
731         ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
732         ut_asserteq(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));
740
741         return 0;
742 }
743 DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
744
745 static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
746 {
747         ofnode node;
748         u32 val[10];
749
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,
755                                                       1));
756
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,
765                                                       4));
766
767         return 0;
768 }
769 DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
770
771 static int dm_test_ofnode_u64(struct unit_test_state *uts)
772 {
773         ofnode node;
774         u64 val;
775
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));
781
782         return 0;
783 }
784 DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
785
786 static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
787 {
788         ofnode node, check, subnode;
789         char buf[128];
790
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);
798
799         if (of_live_active()) {
800                 struct device_node *child;
801
802                 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
803                                            2, &child));
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,
809                                             sizeof(buf)));
810                 ut_asserteq_str("/lcd/ed", buf);
811         }
812
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);
816
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);
825
826         if (of_live_active()) {
827                 ulong start;
828                 int i;
829
830                 /*
831                  * Make sure each of the three malloc()checks in
832                  * of_add_subnode() work
833                  */
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",
838                                                                 &check));
839                         ut_assertok(ut_check_delta(start));
840                 }
841
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();
846         }
847
848         /* write to the empty node */
849         ut_assertok(ofnode_write_string(subnode, "example", "text"));
850
851         return 0;
852 }
853 DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
854
855 static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
856 {
857         ofnode node, subnode;
858         struct ofprop prop;
859         int count;
860
861         node = ofnode_path("/buttons");
862         count = 0;
863
864         /* we expect "compatible" for each node */
865         ofnode_for_each_prop(prop, node)
866                 count++;
867         ut_asserteq(1, count);
868
869         /* there are two nodes, each with 2 properties */
870         ofnode_for_each_subnode(subnode, node)
871                 ofnode_for_each_prop(prop, subnode)
872                         count++;
873         ut_asserteq(5, count);
874
875         return 0;
876 }
877 DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
878
879 static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
880 {
881         const char *compat = "denx,u-boot-fdt-test";
882         ofnode node;
883         int count;
884
885         count = 0;
886         for (node = ofnode_null();
887              node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
888                 count++;
889         ut_asserteq(11, count);
890
891         return 0;
892 }
893 DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
894
895 static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
896 {
897         ofnode node, subnode;
898
899         node = ofnode_path("/buttons");
900
901         subnode = ofnode_find_subnode(node, "btn1");
902         ut_assert(ofnode_valid(subnode));
903         ut_asserteq_str("btn1", ofnode_get_name(subnode));
904
905         subnode = ofnode_find_subnode(node, "btn");
906         ut_assert(!ofnode_valid(subnode));
907
908         return 0;
909 }
910 DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
911
912 static int dm_test_ofnode_get_name(struct unit_test_state *uts)
913 {
914         ofnode node;
915
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()));
920
921         return 0;
922 }
923 DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);