dm: core: Complete phandle implementation using the other FDT
[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 static int dm_test_ofnode_compatible(struct unit_test_state *uts)
55 {
56         ofnode root_node = ofnode_path("/");
57
58         ut_assert(ofnode_valid(root_node));
59         ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
60
61         return 0;
62 }
63 DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
64
65 static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
66 {
67         /* test invalid phandle */
68         ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
69         ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
70
71         /* test first valid phandle */
72         ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
73
74         /* test unknown phandle */
75         ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
76
77         ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
78
79         return 0;
80 }
81 DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
82
83 static int dm_test_ofnode_get_by_phandle_ot(struct unit_test_state *uts)
84 {
85         oftree otree = get_other_oftree(uts);
86         ofnode node;
87
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));
92
93         return 0;
94 }
95 DM_TEST(dm_test_ofnode_get_by_phandle_ot, UT_TESTF_OTHER_FDT);
96
97 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
98 {
99         const char propname[] = "compatible";
100         const char propval[] = "denx,u-boot-fdt-test";
101         const char *str;
102         ofnode node = ofnode_null();
103
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));
109
110         /* Find the rest of the matching nodes */
111         while (true) {
112                 node = ofnode_by_prop_value(node, propname, propval,
113                                             sizeof(propval));
114                 if (!ofnode_valid(node))
115                         break;
116                 str = ofnode_read_string(node, propname);
117                 ut_assert(str && !strcmp(str, propval));
118         }
119
120         return 0;
121 }
122 DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
123
124 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
125 {
126         struct fmap_entry entry;
127         ofnode node;
128
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);
134
135         return 0;
136 }
137 DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
138
139 static int dm_test_ofnode_read(struct unit_test_state *uts)
140 {
141         const u32 *val;
142         ofnode node;
143         int size;
144
145         node = ofnode_path("/a-test");
146         ut_assert(ofnode_valid(node));
147
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]));
152
153         val = ofnode_read_prop(node, "missing", &size);
154         ut_assertnull(val);
155         ut_asserteq(-FDT_ERR_NOTFOUND, size);
156
157         /* Check it works without a size parameter */
158         val = ofnode_read_prop(node, "missing", NULL);
159         ut_assertnull(val);
160
161         return 0;
162 }
163 DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
164
165 static int dm_test_ofnode_phandle(struct unit_test_state *uts)
166 {
167         struct ofnode_phandle_args args;
168         ofnode node;
169         int ret;
170         const char prop[] = "test-gpios";
171         const char cell[] = "#gpio-cells";
172         const char prop2[] = "phandle-value";
173
174         node = ofnode_path("/a-test");
175         ut_assert(ofnode_valid(node));
176
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);
183         ut_asserteq(5, ret);
184
185         /* Test ofnode_parse_phandle_with_args with cell name */
186         ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
187                                              &args);
188         ut_asserteq(-ENOENT, ret);
189         ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
190                                              &args);
191         ut_asserteq(-EINVAL, ret);
192         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
193         ut_assertok(ret);
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);
197         ut_assertok(ret);
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);
201         ut_assertok(ret);
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);
208         ut_assertok(ret);
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);
213
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);
218         ut_asserteq(3, ret);
219
220         /* Test ofnode_parse_phandle_with_args with cell count */
221         ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
222         ut_assertok(ret);
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);
229         ut_assertok(ret);
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);
235
236         return 0;
237 }
238 DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
239
240 static int dm_test_ofnode_phandle_ot(struct unit_test_state *uts)
241 {
242         oftree otree = get_other_oftree(uts);
243         struct ofnode_phandle_args args;
244         ofnode node;
245         int ret;
246
247         node = oftree_path(otree, "/node");
248
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);
255         ut_asserteq(1, ret);
256
257         ret = ofnode_parse_phandle_with_args(node, "target", "#gpio-cells", 0,
258                                              0, &args);
259         ut_assertok(ret);
260         ut_asserteq(2, args.args_count);
261         ut_asserteq(3, args.args[0]);
262         ut_asserteq(4, args.args[1]);
263
264         return 0;
265 }
266 DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT);
267
268 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
269 {
270         const char *str;
271         const u32 *val;
272         ofnode node;
273         int size;
274
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"));
279
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));
283
284         node = ofnode_get_chosen_node("setting");
285         ut_assert(!ofnode_valid(node));
286
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]));
292
293         return 0;
294 }
295 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
296
297 static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
298 {
299         const void *val;
300         ofnode node;
301         int size;
302
303         node = ofnode_get_aliases_node("ethernet3");
304         ut_assert(ofnode_valid(node));
305         ut_asserteq_str("sbe5", ofnode_get_name(node));
306
307         node = ofnode_get_aliases_node("unknown");
308         ut_assert(!ofnode_valid(node));
309
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);
314
315         return 0;
316 }
317 DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
318
319 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
320 {
321         ofnode node, child_node;
322         u32 val;
323
324         node = ofnode_path("/i-test");
325         ut_assert(ofnode_valid(node));
326
327         val = ofnode_get_child_count(node);
328         ut_asserteq(3, val);
329
330         child_node = ofnode_first_subnode(node);
331         ut_assert(ofnode_valid(child_node));
332         val = ofnode_get_child_count(child_node);
333         ut_asserteq(0, val);
334
335         return 0;
336 }
337 DM_TEST(dm_test_ofnode_get_child_count,
338         UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
339
340 static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
341 {
342         ofnode root_node = ofnode_path("/");
343         ofnode node = ofnode_path("/usb@0");
344
345         ut_assert(ofnode_is_enabled(root_node));
346         ut_assert(!ofnode_is_enabled(node));
347
348         return 0;
349 }
350 DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
351
352 static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
353 {
354         ofnode node;
355         fdt_addr_t addr;
356         fdt_size_t size;
357
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);
364
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);
371
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);
378
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);
383
384         return 0;
385 }
386 DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
387
388 static int dm_test_ofnode_get_path(struct unit_test_state *uts)
389 {
390         const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
391         char buf[64];
392         ofnode node;
393         int res;
394
395         node = ofnode_path(path);
396         ut_assert(ofnode_valid(node));
397
398         res = ofnode_get_path(node, buf, 64);
399         ut_asserteq(0, res);
400         ut_asserteq_str(path, buf);
401
402         res = ofnode_get_path(node, buf, 32);
403         ut_asserteq(-ENOSPC, res);
404
405         res = ofnode_get_path(ofnode_root(), buf, 32);
406         ut_asserteq_str("/", buf);
407
408         return 0;
409 }
410 DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
411
412 static int dm_test_ofnode_conf(struct unit_test_state *uts)
413 {
414         ut_assert(!ofnode_conf_read_bool("missing"));
415         ut_assert(ofnode_conf_read_bool("testing-bool"));
416
417         ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
418         ut_asserteq(6, ofnode_conf_read_int("missing", 6));
419
420         ut_assertnull(ofnode_conf_read_str("missing"));
421         ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
422
423         return 0;
424 }
425 DM_TEST(dm_test_ofnode_conf, 0);
426
427 static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
428 {
429         const char compatible[] = "denx,u-boot-fdt-test";
430         bool found = false;
431         ofnode node;
432
433         ofnode_for_each_compatible_node(node, compatible) {
434                 ut_assert(ofnode_device_is_compatible(node, compatible));
435                 found = true;
436         }
437
438         /* There should be at least one matching node */
439         ut_assert(found);
440
441         return 0;
442 }
443 DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
444
445 static int dm_test_ofnode_string(struct unit_test_state *uts)
446 {
447         const char **val;
448         const char *out;
449         ofnode node;
450
451         node = ofnode_path("/a-test");
452         ut_assert(ofnode_valid(node));
453
454         /* single string */
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",
459                                                 "test string"));
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]);
463         free(val);
464
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,
468                                              &out));
469         ut_asserteq_str("mux0", out);
470         ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
471                                                 "mux0"));
472         ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
473                                                &val));
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]);
480         free(val);
481
482         ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
483                                              &out));
484         ut_asserteq_str("mux4", out);
485         ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
486                                                 "mux4"));
487
488         return 0;
489 }
490 DM_TEST(dm_test_ofnode_string, 0);
491
492 static int dm_test_ofnode_string_err(struct unit_test_state *uts)
493 {
494         const char **val;
495         const char *out;
496         ofnode node;
497
498         /*
499          * Test error codes only on livetree, as they are different with
500          * flattree
501          */
502         node = ofnode_path("/a-test");
503         ut_assert(ofnode_valid(node));
504
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,
508                                                       &out));
509         ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
510
511         /* empty property */
512         ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
513         ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
514                                                        &out));
515         ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
516                                                      &val));
517
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,
521                                                        &out));
522         ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
523                                                      &val));
524
525         /* out of range / not found */
526         ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
527                                                        &out));
528         ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
529                                                        "other"));
530
531         /* negative value for index is not allowed, so don't test for that */
532
533         ut_asserteq(-ENODATA, ofnode_read_string_index(node,
534                                                        "mux-control-names", 5,
535                                                        &out));
536
537         return 0;
538 }
539 DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
540
541 static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
542 {
543         ofnode eth_node, phy_node;
544         phy_interface_t mode;
545         u32 reg;
546
547         eth_node = ofnode_path("/phy-test-eth");
548         ut_assert(ofnode_valid(eth_node));
549
550         mode = ofnode_read_phy_mode(eth_node);
551         ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
552
553         phy_node = ofnode_get_phy_node(eth_node);
554         ut_assert(ofnode_valid(phy_node));
555
556         reg = ofnode_read_u32_default(phy_node, "reg", -1U);
557         ut_asserteq_64(0x1, reg);
558
559         return 0;
560 }
561 DM_TEST(dm_test_ofnode_get_phy, 0);
562
563 /**
564  * make_ofnode_fdt() - Create an FDT for testing with ofnode
565  *
566  * The size is set to the minimum needed
567  *
568  * @uts: Test state
569  * @fdt: Place to write FDT
570  * @size: Maximum size of space for fdt
571  */
572 static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
573 {
574         ut_assertok(fdt_create(fdt, size));
575         ut_assertok(fdt_finish_reservemap(fdt));
576         ut_assert(fdt_begin_node(fdt, "") >= 0);
577
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));
581
582         ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
583         ut_assertok(fdt_end_node(fdt));
584
585         ut_assertok(fdt_end_node(fdt));
586         ut_assertok(fdt_finish(fdt));
587
588         return 0;
589 }
590
591 static int dm_test_ofnode_root(struct unit_test_state *uts)
592 {
593         struct device_node *root = NULL;
594         char fdt[256];
595         oftree tree;
596         ofnode node;
597
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));
602
603         ut_assert(!oftree_valid(oftree_null()));
604
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);
609         } else {
610                 tree = oftree_from_fdt(fdt);
611         }
612         ut_assert(oftree_valid(tree));
613
614         /* Make sure they don't work on this new tree */
615         node = oftree_path(tree, "mmc0");
616         ut_assert(!ofnode_valid(node));
617
618         /* It should appear in the new tree */
619         node = oftree_path(tree, "/new-mmc");
620         ut_assert(ofnode_valid(node));
621
622         /* ...and not in the control FDT */
623         node = oftree_path(oftree_default(), "/new-mmc");
624         ut_assert(!ofnode_valid(node));
625
626         free(root);
627
628         return 0;
629 }
630 DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
631
632 static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
633 {
634         struct udevice *dev;
635         ofnode node;
636
637         /* Test enabling devices */
638         node = ofnode_path("/usb@2");
639
640         ut_assert(!ofnode_is_enabled(node));
641         ut_assertok(ofnode_set_enabled(node, true));
642         ut_asserteq(true, ofnode_is_enabled(node));
643
644         device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
645                                    &dev);
646         ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
647
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"));
654
655         /* Test setting generic properties */
656
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));
663
664         /* Test disabling devices */
665         device_remove(dev, DM_REMOVE_NORMAL);
666         device_unbind(dev);
667
668         ut_assert(ofnode_is_enabled(node));
669         ut_assertok(ofnode_set_enabled(node, false));
670         ut_assert(!ofnode_is_enabled(node));
671
672         return 0;
673 }
674 DM_TEST(dm_test_ofnode_livetree_writing,
675         UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
676
677 static int dm_test_ofnode_u32(struct unit_test_state *uts)
678 {
679         ofnode node;
680         u32 val;
681
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));
688
689         node = ofnode_path("/backlight");
690         ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
691         ut_asserteq(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));
699
700         return 0;
701 }
702 DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
703
704 static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
705 {
706         ofnode node;
707         u32 val[10];
708
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,
714                                                       1));
715
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,
724                                                       4));
725
726         return 0;
727 }
728 DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
729
730 static int dm_test_ofnode_u64(struct unit_test_state *uts)
731 {
732         ofnode node;
733         u64 val;
734
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));
740
741         return 0;
742 }
743 DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
744
745 static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
746 {
747         ofnode node, check, subnode;
748         char buf[128];
749
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);
757
758         if (of_live_active()) {
759                 struct device_node *child;
760
761                 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
762                                            2, &child));
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,
768                                             sizeof(buf)));
769                 ut_asserteq_str("/lcd/ed", buf);
770         }
771
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);
775
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);
784
785         if (of_live_active()) {
786                 ulong start;
787                 int i;
788
789                 /*
790                  * Make sure each of the three malloc()checks in
791                  * of_add_subnode() work
792                  */
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",
797                                                                 &check));
798                         ut_assertok(ut_check_delta(start));
799                 }
800
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();
805         }
806
807         /* write to the empty node */
808         ut_assertok(ofnode_write_string(subnode, "example", "text"));
809
810         return 0;
811 }
812 DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
813
814 static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
815 {
816         ofnode node, subnode;
817         struct ofprop prop;
818         int count;
819
820         node = ofnode_path("/buttons");
821         count = 0;
822
823         /* we expect "compatible" for each node */
824         ofnode_for_each_prop(prop, node)
825                 count++;
826         ut_asserteq(1, count);
827
828         /* there are two nodes, each with 2 properties */
829         ofnode_for_each_subnode(subnode, node)
830                 ofnode_for_each_prop(prop, subnode)
831                         count++;
832         ut_asserteq(5, count);
833
834         return 0;
835 }
836 DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
837
838 static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
839 {
840         const char *compat = "denx,u-boot-fdt-test";
841         ofnode node;
842         int count;
843
844         count = 0;
845         for (node = ofnode_null();
846              node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
847                 count++;
848         ut_asserteq(11, count);
849
850         return 0;
851 }
852 DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
853
854 static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
855 {
856         ofnode node, subnode;
857
858         node = ofnode_path("/buttons");
859
860         subnode = ofnode_find_subnode(node, "btn1");
861         ut_assert(ofnode_valid(subnode));
862         ut_asserteq_str("btn1", ofnode_get_name(subnode));
863
864         subnode = ofnode_find_subnode(node, "btn");
865         ut_assert(!ofnode_valid(subnode));
866
867         return 0;
868 }
869 DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
870
871 static int dm_test_ofnode_get_name(struct unit_test_state *uts)
872 {
873         ofnode node;
874
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()));
879
880         return 0;
881 }
882 DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);