dm: core: Add a way to look up a phandle in an oftree
[platform/kernel/u-boot.git] / test / dm / ofnode.c
1 // SPDX-License-Identifier: GPL-2.0+
2
3 #include <common.h>
4 #include <dm.h>
5 #include <log.h>
6 #include <of_live.h>
7 #include <dm/device-internal.h>
8 #include <dm/lists.h>
9 #include <dm/of_extra.h>
10 #include <dm/root.h>
11 #include <dm/test.h>
12 #include <dm/uclass-internal.h>
13 #include <test/test.h>
14 #include <test/ut.h>
15
16 static int dm_test_ofnode_compatible(struct unit_test_state *uts)
17 {
18         ofnode root_node = ofnode_path("/");
19
20         ut_assert(ofnode_valid(root_node));
21         ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
22
23         return 0;
24 }
25 DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
26
27 static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
28 {
29         /* test invalid phandle */
30         ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
31         ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
32
33         /* test first valid phandle */
34         ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
35
36         /* test unknown phandle */
37         ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
38
39         ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
40
41         return 0;
42 }
43 DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
44
45 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
46 {
47         const char propname[] = "compatible";
48         const char propval[] = "denx,u-boot-fdt-test";
49         const char *str;
50         ofnode node = ofnode_null();
51
52         /* Find first matching node, there should be at least one */
53         node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
54         ut_assert(ofnode_valid(node));
55         str = ofnode_read_string(node, propname);
56         ut_assert(str && !strcmp(str, propval));
57
58         /* Find the rest of the matching nodes */
59         while (true) {
60                 node = ofnode_by_prop_value(node, propname, propval,
61                                             sizeof(propval));
62                 if (!ofnode_valid(node))
63                         break;
64                 str = ofnode_read_string(node, propname);
65                 ut_assert(str && !strcmp(str, propval));
66         }
67
68         return 0;
69 }
70 DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
71
72 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
73 {
74         struct fmap_entry entry;
75         ofnode node;
76
77         node = ofnode_path("/cros-ec/flash");
78         ut_assert(ofnode_valid(node));
79         ut_assertok(ofnode_read_fmap_entry(node, &entry));
80         ut_asserteq(0x08000000, entry.offset);
81         ut_asserteq(0x20000, entry.length);
82
83         return 0;
84 }
85 DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
86
87 static int dm_test_ofnode_read(struct unit_test_state *uts)
88 {
89         const u32 *val;
90         ofnode node;
91         int size;
92
93         node = ofnode_path("/a-test");
94         ut_assert(ofnode_valid(node));
95
96         val = ofnode_read_prop(node, "int-value", &size);
97         ut_assertnonnull(val);
98         ut_asserteq(4, size);
99         ut_asserteq(1234, fdt32_to_cpu(val[0]));
100
101         val = ofnode_read_prop(node, "missing", &size);
102         ut_assertnull(val);
103         ut_asserteq(-FDT_ERR_NOTFOUND, size);
104
105         /* Check it works without a size parameter */
106         val = ofnode_read_prop(node, "missing", NULL);
107         ut_assertnull(val);
108
109         return 0;
110 }
111 DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
112
113 static int dm_test_ofnode_phandle(struct unit_test_state *uts)
114 {
115         struct ofnode_phandle_args args;
116         ofnode node;
117         int ret;
118         const char prop[] = "test-gpios";
119         const char cell[] = "#gpio-cells";
120         const char prop2[] = "phandle-value";
121
122         node = ofnode_path("/a-test");
123         ut_assert(ofnode_valid(node));
124
125         /* Test ofnode_count_phandle_with_args with cell name */
126         ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
127         ut_asserteq(-ENOENT, ret);
128         ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
129         ut_asserteq(-EINVAL, ret);
130         ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
131         ut_asserteq(5, ret);
132
133         /* Test ofnode_parse_phandle_with_args with cell name */
134         ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
135                                              &args);
136         ut_asserteq(-ENOENT, ret);
137         ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
138                                              &args);
139         ut_asserteq(-EINVAL, ret);
140         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
141         ut_assertok(ret);
142         ut_asserteq(1, args.args_count);
143         ut_asserteq(1, args.args[0]);
144         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
145         ut_assertok(ret);
146         ut_asserteq(1, args.args_count);
147         ut_asserteq(4, args.args[0]);
148         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
149         ut_assertok(ret);
150         ut_asserteq(5, args.args_count);
151         ut_asserteq(5, args.args[0]);
152         ut_asserteq(1, args.args[4]);
153         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
154         ut_asserteq(-ENOENT, ret);
155         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
156         ut_assertok(ret);
157         ut_asserteq(1, args.args_count);
158         ut_asserteq(12, args.args[0]);
159         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
160         ut_asserteq(-ENOENT, ret);
161
162         /* Test ofnode_count_phandle_with_args with cell count */
163         ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
164         ut_asserteq(-ENOENT, ret);
165         ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
166         ut_asserteq(3, ret);
167
168         /* Test ofnode_parse_phandle_with_args with cell count */
169         ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
170         ut_assertok(ret);
171         ut_asserteq(1, ofnode_valid(args.node));
172         ut_asserteq(1, args.args_count);
173         ut_asserteq(10, args.args[0]);
174         ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
175         ut_asserteq(-EINVAL, ret);
176         ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
177         ut_assertok(ret);
178         ut_asserteq(1, ofnode_valid(args.node));
179         ut_asserteq(1, args.args_count);
180         ut_asserteq(30, args.args[0]);
181         ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
182         ut_asserteq(-ENOENT, ret);
183
184         return 0;
185 }
186 DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
187
188 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
189 {
190         const char *str;
191         const u32 *val;
192         ofnode node;
193         int size;
194
195         str = ofnode_read_chosen_string("setting");
196         ut_assertnonnull(str);
197         ut_asserteq_str("sunrise ohoka", str);
198         ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
199
200         node = ofnode_get_chosen_node("other-node");
201         ut_assert(ofnode_valid(node));
202         ut_asserteq_str("c-test@5", ofnode_get_name(node));
203
204         node = ofnode_get_chosen_node("setting");
205         ut_assert(!ofnode_valid(node));
206
207         val = ofnode_read_chosen_prop("int-values", &size);
208         ut_assertnonnull(val);
209         ut_asserteq(8, size);
210         ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
211         ut_asserteq(72993, fdt32_to_cpu(val[1]));
212
213         return 0;
214 }
215 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
216
217 static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
218 {
219         const void *val;
220         ofnode node;
221         int size;
222
223         node = ofnode_get_aliases_node("ethernet3");
224         ut_assert(ofnode_valid(node));
225         ut_asserteq_str("sbe5", ofnode_get_name(node));
226
227         node = ofnode_get_aliases_node("unknown");
228         ut_assert(!ofnode_valid(node));
229
230         val = ofnode_read_aliases_prop("spi0", &size);
231         ut_assertnonnull(val);
232         ut_asserteq(7, size);
233         ut_asserteq_str("/spi@0", (const char *)val);
234
235         return 0;
236 }
237 DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
238
239 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
240 {
241         ofnode node, child_node;
242         u32 val;
243
244         node = ofnode_path("/i-test");
245         ut_assert(ofnode_valid(node));
246
247         val = ofnode_get_child_count(node);
248         ut_asserteq(3, val);
249
250         child_node = ofnode_first_subnode(node);
251         ut_assert(ofnode_valid(child_node));
252         val = ofnode_get_child_count(child_node);
253         ut_asserteq(0, val);
254
255         return 0;
256 }
257 DM_TEST(dm_test_ofnode_get_child_count,
258         UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
259
260 static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
261 {
262         ofnode root_node = ofnode_path("/");
263         ofnode node = ofnode_path("/usb@0");
264
265         ut_assert(ofnode_is_enabled(root_node));
266         ut_assert(!ofnode_is_enabled(node));
267
268         return 0;
269 }
270 DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
271
272 static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
273 {
274         ofnode node;
275         fdt_addr_t addr;
276         fdt_size_t size;
277
278         node = ofnode_path("/translation-test@8000");
279         ut_assert(ofnode_valid(node));
280         addr = ofnode_get_addr(node);
281         size = ofnode_get_size(node);
282         ut_asserteq(0x8000, addr);
283         ut_asserteq(0x4000, size);
284
285         node = ofnode_path("/translation-test@8000/dev@1,100");
286         ut_assert(ofnode_valid(node));
287         addr = ofnode_get_addr(node);
288         size = ofnode_get_size(node);
289         ut_asserteq(0x9000, addr);
290         ut_asserteq(0x1000, size);
291
292         node = ofnode_path("/emul-mux-controller");
293         ut_assert(ofnode_valid(node));
294         addr = ofnode_get_addr(node);
295         size = ofnode_get_size(node);
296         ut_asserteq_64(FDT_ADDR_T_NONE, addr);
297         ut_asserteq(FDT_SIZE_T_NONE, size);
298
299         node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
300         ut_assert(ofnode_valid(node));
301         addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
302         ut_asserteq_64(0x42, addr);
303
304         return 0;
305 }
306 DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
307
308 static int dm_test_ofnode_get_path(struct unit_test_state *uts)
309 {
310         const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
311         char buf[64];
312         ofnode node;
313         int res;
314
315         node = ofnode_path(path);
316         ut_assert(ofnode_valid(node));
317
318         res = ofnode_get_path(node, buf, 64);
319         ut_asserteq(0, res);
320         ut_asserteq_str(path, buf);
321
322         res = ofnode_get_path(node, buf, 32);
323         ut_asserteq(-ENOSPC, res);
324
325         res = ofnode_get_path(ofnode_root(), buf, 32);
326         ut_asserteq_str("/", buf);
327
328         return 0;
329 }
330 DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
331
332 static int dm_test_ofnode_conf(struct unit_test_state *uts)
333 {
334         ut_assert(!ofnode_conf_read_bool("missing"));
335         ut_assert(ofnode_conf_read_bool("testing-bool"));
336
337         ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
338         ut_asserteq(6, ofnode_conf_read_int("missing", 6));
339
340         ut_assertnull(ofnode_conf_read_str("missing"));
341         ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
342
343         return 0;
344 }
345 DM_TEST(dm_test_ofnode_conf, 0);
346
347 static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
348 {
349         const char compatible[] = "denx,u-boot-fdt-test";
350         bool found = false;
351         ofnode node;
352
353         ofnode_for_each_compatible_node(node, compatible) {
354                 ut_assert(ofnode_device_is_compatible(node, compatible));
355                 found = true;
356         }
357
358         /* There should be at least one matching node */
359         ut_assert(found);
360
361         return 0;
362 }
363 DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
364
365 static int dm_test_ofnode_string(struct unit_test_state *uts)
366 {
367         const char **val;
368         const char *out;
369         ofnode node;
370
371         node = ofnode_path("/a-test");
372         ut_assert(ofnode_valid(node));
373
374         /* single string */
375         ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
376         ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
377         ut_asserteq_str("test string", out);
378         ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
379                                                 "test string"));
380         ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
381         ut_asserteq_str("test string", val[0]);
382         ut_assertnull(val[1]);
383         free(val);
384
385         /* list of strings */
386         ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
387         ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
388                                              &out));
389         ut_asserteq_str("mux0", out);
390         ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
391                                                 "mux0"));
392         ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
393                                                &val));
394         ut_asserteq_str("mux0", val[0]);
395         ut_asserteq_str("mux1", val[1]);
396         ut_asserteq_str("mux2", val[2]);
397         ut_asserteq_str("mux3", val[3]);
398         ut_asserteq_str("mux4", val[4]);
399         ut_assertnull(val[5]);
400         free(val);
401
402         ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
403                                              &out));
404         ut_asserteq_str("mux4", out);
405         ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
406                                                 "mux4"));
407
408         return 0;
409 }
410 DM_TEST(dm_test_ofnode_string, 0);
411
412 static int dm_test_ofnode_string_err(struct unit_test_state *uts)
413 {
414         const char **val;
415         const char *out;
416         ofnode node;
417
418         /*
419          * Test error codes only on livetree, as they are different with
420          * flattree
421          */
422         node = ofnode_path("/a-test");
423         ut_assert(ofnode_valid(node));
424
425         /* non-existent property */
426         ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
427         ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
428                                                       &out));
429         ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
430
431         /* empty property */
432         ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
433         ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
434                                                        &out));
435         ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
436                                                      &val));
437
438         /* badly formatted string list */
439         ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
440         ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
441                                                        &out));
442         ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
443                                                      &val));
444
445         /* out of range / not found */
446         ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
447                                                        &out));
448         ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
449                                                        "other"));
450
451         /* negative value for index is not allowed, so don't test for that */
452
453         ut_asserteq(-ENODATA, ofnode_read_string_index(node,
454                                                        "mux-control-names", 5,
455                                                        &out));
456
457         return 0;
458 }
459 DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
460
461 static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
462 {
463         ofnode eth_node, phy_node;
464         phy_interface_t mode;
465         u32 reg;
466
467         eth_node = ofnode_path("/phy-test-eth");
468         ut_assert(ofnode_valid(eth_node));
469
470         mode = ofnode_read_phy_mode(eth_node);
471         ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
472
473         phy_node = ofnode_get_phy_node(eth_node);
474         ut_assert(ofnode_valid(phy_node));
475
476         reg = ofnode_read_u32_default(phy_node, "reg", -1U);
477         ut_asserteq_64(0x1, reg);
478
479         return 0;
480 }
481 DM_TEST(dm_test_ofnode_get_phy, 0);
482
483 /**
484  * make_ofnode_fdt() - Create an FDT for testing with ofnode
485  *
486  * The size is set to the minimum needed
487  *
488  * @uts: Test state
489  * @fdt: Place to write FDT
490  * @size: Maximum size of space for fdt
491  */
492 static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
493 {
494         ut_assertok(fdt_create(fdt, size));
495         ut_assertok(fdt_finish_reservemap(fdt));
496         ut_assert(fdt_begin_node(fdt, "") >= 0);
497
498         ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
499         ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
500         ut_assertok(fdt_end_node(fdt));
501
502         ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
503         ut_assertok(fdt_end_node(fdt));
504
505         ut_assertok(fdt_end_node(fdt));
506         ut_assertok(fdt_finish(fdt));
507
508         return 0;
509 }
510
511 static int dm_test_ofnode_root(struct unit_test_state *uts)
512 {
513         struct device_node *root = NULL;
514         char fdt[256];
515         oftree tree;
516         ofnode node;
517
518         /* Check that aliases work on the control FDT */
519         node = ofnode_get_aliases_node("ethernet3");
520         ut_assert(ofnode_valid(node));
521         ut_asserteq_str("sbe5", ofnode_get_name(node));
522
523         ut_assert(!oftree_valid(oftree_null()));
524
525         ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
526         if (of_live_active()) {
527                 ut_assertok(unflatten_device_tree(fdt, &root));
528                 tree = oftree_from_np(root);
529         } else {
530                 tree = oftree_from_fdt(fdt);
531         }
532         ut_assert(oftree_valid(tree));
533
534         /* Make sure they don't work on this new tree */
535         node = ofnode_path_root(tree, "mmc0");
536         ut_assert(!ofnode_valid(node));
537
538         /* It should appear in the new tree */
539         node = ofnode_path_root(tree, "/new-mmc");
540         ut_assert(ofnode_valid(node));
541
542         /* ...and not in the control FDT */
543         node = ofnode_path_root(oftree_default(), "/new-mmc");
544         ut_assert(!ofnode_valid(node));
545
546         free(root);
547
548         return 0;
549 }
550 DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
551
552 static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
553 {
554         struct udevice *dev;
555         ofnode node;
556
557         /* Test enabling devices */
558         node = ofnode_path("/usb@2");
559
560         ut_assert(!ofnode_is_enabled(node));
561         ut_assertok(ofnode_set_enabled(node, true));
562         ut_asserteq(true, ofnode_is_enabled(node));
563
564         device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
565                                    &dev);
566         ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
567
568         /* Test string property setting */
569         ut_assert(device_is_compatible(dev, "sandbox,usb"));
570         ofnode_write_string(node, "compatible", "gdsys,super-usb");
571         ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
572         ofnode_write_string(node, "compatible", "sandbox,usb");
573         ut_assert(device_is_compatible(dev, "sandbox,usb"));
574
575         /* Test setting generic properties */
576
577         /* Non-existent in DTB */
578         ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
579         /* reg = 0x42, size = 0x100 */
580         ut_assertok(ofnode_write_prop(node, "reg",
581                                       "\x00\x00\x00\x42\x00\x00\x01\x00", 8));
582         ut_asserteq(0x42, dev_read_addr(dev));
583
584         /* Test disabling devices */
585         device_remove(dev, DM_REMOVE_NORMAL);
586         device_unbind(dev);
587
588         ut_assert(ofnode_is_enabled(node));
589         ut_assertok(ofnode_set_enabled(node, false));
590         ut_assert(!ofnode_is_enabled(node));
591
592         return 0;
593 }
594 DM_TEST(dm_test_ofnode_livetree_writing,
595         UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
596
597 static int dm_test_ofnode_u32(struct unit_test_state *uts)
598 {
599         ofnode node;
600         u32 val;
601
602         node = ofnode_path("/lcd");
603         ut_assert(ofnode_valid(node));
604         ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
605         ut_assertok(ofnode_write_u32(node, "xres", 1367));
606         ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
607         ut_assertok(ofnode_write_u32(node, "xres", 1366));
608
609         node = ofnode_path("/backlight");
610         ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
611         ut_asserteq(0, val);
612         ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
613         ut_asserteq(16, val);
614         ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
615         ut_asserteq(255, val);
616         ut_asserteq(-EOVERFLOW,
617                     ofnode_read_u32_index(node, "brightness-levels", 9, &val));
618         ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
619
620         return 0;
621 }
622 DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
623
624 static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
625 {
626         ofnode node;
627         u32 val[10];
628
629         node = ofnode_path("/a-test");
630         ut_assert(ofnode_valid(node));
631         ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
632         ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
633         ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
634                                                       1));
635
636         memset(val, '\0', sizeof(val));
637         ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
638         ut_asserteq(0, val[0]);
639         ut_asserteq(5678, val[1]);
640         ut_asserteq(9123, val[2]);
641         ut_asserteq(4567, val[3]);
642         ut_asserteq(0, val[4]);
643         ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
644                                                       4));
645
646         return 0;
647 }
648 DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
649
650 static int dm_test_ofnode_u64(struct unit_test_state *uts)
651 {
652         ofnode node;
653         u64 val;
654
655         node = ofnode_path("/a-test");
656         ut_assert(ofnode_valid(node));
657         ut_assertok(ofnode_read_u64(node, "int64-value", &val));
658         ut_asserteq_64(0x1111222233334444, val);
659         ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
660
661         return 0;
662 }
663 DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
664
665 static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
666 {
667         ofnode node, check, subnode;
668         char buf[128];
669
670         node = ofnode_path("/lcd");
671         ut_assert(ofnode_valid(node));
672         ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
673         check = ofnode_path("/lcd/edmund");
674         ut_asserteq(subnode.of_offset, check.of_offset);
675         ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
676         ut_asserteq_str("/lcd/edmund", buf);
677
678         if (of_live_active()) {
679                 struct device_node *child;
680
681                 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
682                                            2, &child));
683                 ut_asserteq_str("ed", child->name);
684                 ut_asserteq_str("/lcd/ed", child->full_name);
685                 check = ofnode_path("/lcd/ed");
686                 ut_asserteq_ptr(child, check.np);
687                 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
688                                             sizeof(buf)));
689                 ut_asserteq_str("/lcd/ed", buf);
690         }
691
692         /* An existing node should be returned with -EEXIST */
693         ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
694         ut_asserteq(subnode.of_offset, check.of_offset);
695
696         /* add a root node */
697         node = ofnode_path("/");
698         ut_assert(ofnode_valid(node));
699         ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
700         check = ofnode_path("/lcd2");
701         ut_asserteq(subnode.of_offset, check.of_offset);
702         ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
703         ut_asserteq_str("/lcd2", buf);
704
705         if (of_live_active()) {
706                 ulong start;
707                 int i;
708
709                 /*
710                  * Make sure each of the three malloc()checks in
711                  * of_add_subnode() work
712                  */
713                 for (i = 0; i < 3; i++) {
714                         malloc_enable_testing(i);
715                         start = ut_check_free();
716                         ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
717                                                                 &check));
718                         ut_assertok(ut_check_delta(start));
719                 }
720
721                 /* This should pass since we allow 3 allocations */
722                 malloc_enable_testing(3);
723                 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
724                 malloc_disable_testing();
725         }
726
727         /* write to the empty node */
728         ut_assertok(ofnode_write_string(subnode, "example", "text"));
729
730         return 0;
731 }
732 DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
733
734 static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
735 {
736         ofnode node, subnode;
737         struct ofprop prop;
738         int count;
739
740         node = ofnode_path("/buttons");
741         count = 0;
742
743         /* we expect "compatible" for each node */
744         ofnode_for_each_prop(prop, node)
745                 count++;
746         ut_asserteq(1, count);
747
748         /* there are two nodes, each with 2 properties */
749         ofnode_for_each_subnode(subnode, node)
750                 ofnode_for_each_prop(prop, subnode)
751                         count++;
752         ut_asserteq(5, count);
753
754         return 0;
755 }
756 DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
757
758 static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
759 {
760         const char *compat = "denx,u-boot-fdt-test";
761         ofnode node;
762         int count;
763
764         count = 0;
765         for (node = ofnode_null();
766              node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
767                 count++;
768         ut_asserteq(11, count);
769
770         return 0;
771 }
772 DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
773
774 static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
775 {
776         ofnode node, subnode;
777
778         node = ofnode_path("/buttons");
779
780         subnode = ofnode_find_subnode(node, "btn1");
781         ut_assert(ofnode_valid(subnode));
782         ut_asserteq_str("btn1", ofnode_get_name(subnode));
783
784         subnode = ofnode_find_subnode(node, "btn");
785         ut_assert(!ofnode_valid(subnode));
786
787         return 0;
788 }
789 DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
790
791 static int dm_test_ofnode_get_name(struct unit_test_state *uts)
792 {
793         ofnode node;
794
795         node = ofnode_path("/buttons");
796         ut_assert(ofnode_valid(node));
797         ut_asserteq_str("buttons", ofnode_get_name(node));
798         ut_asserteq_str("", ofnode_get_name(ofnode_root()));
799
800         return 0;
801 }
802 DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);