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