dm: core: Introduce support for multiple trees
[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/of_extra.h>
8 #include <dm/test.h>
9 #include <test/test.h>
10 #include <test/ut.h>
11
12 static int dm_test_ofnode_compatible(struct unit_test_state *uts)
13 {
14         ofnode root_node = ofnode_path("/");
15
16         ut_assert(ofnode_valid(root_node));
17         ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
18
19         return 0;
20 }
21 DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
22
23 static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
24 {
25         /* test invalid phandle */
26         ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
27         ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
28
29         /* test first valid phandle */
30         ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
31
32         /* test unknown phandle */
33         ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
34
35         return 0;
36 }
37 DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
38
39 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
40 {
41         const char propname[] = "compatible";
42         const char propval[] = "denx,u-boot-fdt-test";
43         const char *str;
44         ofnode node = ofnode_null();
45
46         /* Find first matching node, there should be at least one */
47         node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
48         ut_assert(ofnode_valid(node));
49         str = ofnode_read_string(node, propname);
50         ut_assert(str && !strcmp(str, propval));
51
52         /* Find the rest of the matching nodes */
53         while (true) {
54                 node = ofnode_by_prop_value(node, propname, propval,
55                                             sizeof(propval));
56                 if (!ofnode_valid(node))
57                         break;
58                 str = ofnode_read_string(node, propname);
59                 ut_assert(str && !strcmp(str, propval));
60         }
61
62         return 0;
63 }
64 DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
65
66 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
67 {
68         struct fmap_entry entry;
69         ofnode node;
70
71         node = ofnode_path("/cros-ec/flash");
72         ut_assert(ofnode_valid(node));
73         ut_assertok(ofnode_read_fmap_entry(node, &entry));
74         ut_asserteq(0x08000000, entry.offset);
75         ut_asserteq(0x20000, entry.length);
76
77         return 0;
78 }
79 DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
80
81 static int dm_test_ofnode_read(struct unit_test_state *uts)
82 {
83         const u32 *val;
84         ofnode node;
85         int size;
86
87         node = ofnode_path("/a-test");
88         ut_assert(ofnode_valid(node));
89
90         val = ofnode_read_prop(node, "int-value", &size);
91         ut_assertnonnull(val);
92         ut_asserteq(4, size);
93         ut_asserteq(1234, fdt32_to_cpu(val[0]));
94
95         val = ofnode_read_prop(node, "missing", &size);
96         ut_assertnull(val);
97         ut_asserteq(-FDT_ERR_NOTFOUND, size);
98
99         /* Check it works without a size parameter */
100         val = ofnode_read_prop(node, "missing", NULL);
101         ut_assertnull(val);
102
103         return 0;
104 }
105 DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
106
107 static int dm_test_ofnode_phandle(struct unit_test_state *uts)
108 {
109         struct ofnode_phandle_args args;
110         ofnode node;
111         int ret;
112         const char prop[] = "test-gpios";
113         const char cell[] = "#gpio-cells";
114         const char prop2[] = "phandle-value";
115
116         node = ofnode_path("/a-test");
117         ut_assert(ofnode_valid(node));
118
119         /* Test ofnode_count_phandle_with_args with cell name */
120         ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
121         ut_asserteq(-ENOENT, ret);
122         ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
123         ut_asserteq(-EINVAL, ret);
124         ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
125         ut_asserteq(5, ret);
126
127         /* Test ofnode_parse_phandle_with_args with cell name */
128         ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
129                                              &args);
130         ut_asserteq(-ENOENT, ret);
131         ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
132                                              &args);
133         ut_asserteq(-EINVAL, ret);
134         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
135         ut_assertok(ret);
136         ut_asserteq(1, args.args_count);
137         ut_asserteq(1, args.args[0]);
138         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
139         ut_assertok(ret);
140         ut_asserteq(1, args.args_count);
141         ut_asserteq(4, args.args[0]);
142         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
143         ut_assertok(ret);
144         ut_asserteq(5, args.args_count);
145         ut_asserteq(5, args.args[0]);
146         ut_asserteq(1, args.args[4]);
147         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
148         ut_asserteq(-ENOENT, ret);
149         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
150         ut_assertok(ret);
151         ut_asserteq(1, args.args_count);
152         ut_asserteq(12, args.args[0]);
153         ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
154         ut_asserteq(-ENOENT, ret);
155
156         /* Test ofnode_count_phandle_with_args with cell count */
157         ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
158         ut_asserteq(-ENOENT, ret);
159         ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
160         ut_asserteq(3, ret);
161
162         /* Test ofnode_parse_phandle_with_args with cell count */
163         ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
164         ut_assertok(ret);
165         ut_asserteq(1, ofnode_valid(args.node));
166         ut_asserteq(1, args.args_count);
167         ut_asserteq(10, args.args[0]);
168         ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
169         ut_asserteq(-EINVAL, ret);
170         ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
171         ut_assertok(ret);
172         ut_asserteq(1, ofnode_valid(args.node));
173         ut_asserteq(1, args.args_count);
174         ut_asserteq(30, args.args[0]);
175         ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
176         ut_asserteq(-ENOENT, ret);
177
178         return 0;
179 }
180 DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
181
182 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
183 {
184         const char *str;
185         const u32 *val;
186         ofnode node;
187         int size;
188
189         str = ofnode_read_chosen_string("setting");
190         ut_assertnonnull(str);
191         ut_asserteq_str("sunrise ohoka", str);
192         ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
193
194         node = ofnode_get_chosen_node("other-node");
195         ut_assert(ofnode_valid(node));
196         ut_asserteq_str("c-test@5", ofnode_get_name(node));
197
198         node = ofnode_get_chosen_node("setting");
199         ut_assert(!ofnode_valid(node));
200
201         val = ofnode_read_chosen_prop("int-values", &size);
202         ut_assertnonnull(val);
203         ut_asserteq(8, size);
204         ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
205         ut_asserteq(72993, fdt32_to_cpu(val[1]));
206
207         return 0;
208 }
209 DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
210
211 static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
212 {
213         const void *val;
214         ofnode node;
215         int size;
216
217         node = ofnode_get_aliases_node("ethernet3");
218         ut_assert(ofnode_valid(node));
219         ut_asserteq_str("sbe5", ofnode_get_name(node));
220
221         node = ofnode_get_aliases_node("unknown");
222         ut_assert(!ofnode_valid(node));
223
224         val = ofnode_read_aliases_prop("spi0", &size);
225         ut_assertnonnull(val);
226         ut_asserteq(7, size);
227         ut_asserteq_str("/spi@0", (const char *)val);
228
229         return 0;
230 }
231 DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
232
233 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
234 {
235         ofnode node, child_node;
236         u32 val;
237
238         node = ofnode_path("/i-test");
239         ut_assert(ofnode_valid(node));
240
241         val = ofnode_get_child_count(node);
242         ut_asserteq(3, val);
243
244         child_node = ofnode_first_subnode(node);
245         ut_assert(ofnode_valid(child_node));
246         val = ofnode_get_child_count(child_node);
247         ut_asserteq(0, val);
248
249         return 0;
250 }
251 DM_TEST(dm_test_ofnode_get_child_count,
252         UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
253
254 static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
255 {
256         ofnode root_node = ofnode_path("/");
257         ofnode node = ofnode_path("/usb@0");
258
259         ut_assert(ofnode_is_enabled(root_node));
260         ut_assert(!ofnode_is_enabled(node));
261
262         return 0;
263 }
264 DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
265
266 static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
267 {
268         ofnode node;
269         fdt_addr_t addr;
270         fdt_size_t size;
271
272         node = ofnode_path("/translation-test@8000");
273         ut_assert(ofnode_valid(node));
274         addr = ofnode_get_addr(node);
275         size = ofnode_get_size(node);
276         ut_asserteq(0x8000, addr);
277         ut_asserteq(0x4000, size);
278
279         node = ofnode_path("/translation-test@8000/dev@1,100");
280         ut_assert(ofnode_valid(node));
281         addr = ofnode_get_addr(node);
282         size = ofnode_get_size(node);
283         ut_asserteq(0x9000, addr);
284         ut_asserteq(0x1000, size);
285
286         node = ofnode_path("/emul-mux-controller");
287         ut_assert(ofnode_valid(node));
288         addr = ofnode_get_addr(node);
289         size = ofnode_get_size(node);
290         ut_asserteq_64(FDT_ADDR_T_NONE, addr);
291         ut_asserteq(FDT_SIZE_T_NONE, size);
292
293         node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
294         ut_assert(ofnode_valid(node));
295         addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
296         ut_asserteq_64(0x42, addr);
297
298         return 0;
299 }
300 DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
301
302 static int dm_test_ofnode_get_path(struct unit_test_state *uts)
303 {
304         const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
305         char buf[64];
306         ofnode node;
307         int res;
308
309         node = ofnode_path(path);
310         ut_assert(ofnode_valid(node));
311
312         res = ofnode_get_path(node, buf, 64);
313         ut_asserteq(0, res);
314         ut_asserteq_str(path, buf);
315
316         res = ofnode_get_path(node, buf, 32);
317         ut_asserteq(-ENOSPC, res);
318
319         return 0;
320 }
321 DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
322
323 static int dm_test_ofnode_conf(struct unit_test_state *uts)
324 {
325         ut_assert(!ofnode_conf_read_bool("missing"));
326         ut_assert(ofnode_conf_read_bool("testing-bool"));
327
328         ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
329         ut_asserteq(6, ofnode_conf_read_int("missing", 6));
330
331         ut_assertnull(ofnode_conf_read_str("missing"));
332         ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
333
334         return 0;
335 }
336 DM_TEST(dm_test_ofnode_conf, 0);
337
338 static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
339 {
340         const char compatible[] = "denx,u-boot-fdt-test";
341         bool found = false;
342         ofnode node;
343
344         ofnode_for_each_compatible_node(node, compatible) {
345                 ut_assert(ofnode_device_is_compatible(node, compatible));
346                 found = true;
347         }
348
349         /* There should be at least one matching node */
350         ut_assert(found);
351
352         return 0;
353 }
354 DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
355
356 static int dm_test_ofnode_string(struct unit_test_state *uts)
357 {
358         const char **val;
359         const char *out;
360         ofnode node;
361
362         node = ofnode_path("/a-test");
363         ut_assert(ofnode_valid(node));
364
365         /* single string */
366         ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
367         ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
368         ut_asserteq_str("test string", out);
369         ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
370                                                 "test string"));
371         ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
372         ut_asserteq_str("test string", val[0]);
373         ut_assertnull(val[1]);
374         free(val);
375
376         /* list of strings */
377         ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
378         ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
379                                              &out));
380         ut_asserteq_str("mux0", out);
381         ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
382                                                 "mux0"));
383         ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
384                                                &val));
385         ut_asserteq_str("mux0", val[0]);
386         ut_asserteq_str("mux1", val[1]);
387         ut_asserteq_str("mux2", val[2]);
388         ut_asserteq_str("mux3", val[3]);
389         ut_asserteq_str("mux4", val[4]);
390         ut_assertnull(val[5]);
391         free(val);
392
393         ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
394                                              &out));
395         ut_asserteq_str("mux4", out);
396         ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
397                                                 "mux4"));
398
399         return 0;
400 }
401 DM_TEST(dm_test_ofnode_string, 0);
402
403 static int dm_test_ofnode_string_err(struct unit_test_state *uts)
404 {
405         const char **val;
406         const char *out;
407         ofnode node;
408
409         /*
410          * Test error codes only on livetree, as they are different with
411          * flattree
412          */
413         node = ofnode_path("/a-test");
414         ut_assert(ofnode_valid(node));
415
416         /* non-existent property */
417         ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
418         ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
419                                                       &out));
420         ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
421
422         /* empty property */
423         ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
424         ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
425                                                        &out));
426         ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
427                                                      &val));
428
429         /* badly formatted string list */
430         ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
431         ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
432                                                        &out));
433         ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
434                                                      &val));
435
436         /* out of range / not found */
437         ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
438                                                        &out));
439         ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
440                                                        "other"));
441
442         /* negative value for index is not allowed, so don't test for that */
443
444         ut_asserteq(-ENODATA, ofnode_read_string_index(node,
445                                                        "mux-control-names", 5,
446                                                        &out));
447
448         return 0;
449 }
450 DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
451
452 static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
453 {
454         ofnode eth_node, phy_node;
455         phy_interface_t mode;
456         u32 reg;
457
458         eth_node = ofnode_path("/phy-test-eth");
459         ut_assert(ofnode_valid(eth_node));
460
461         mode = ofnode_read_phy_mode(eth_node);
462         ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
463
464         phy_node = ofnode_get_phy_node(eth_node);
465         ut_assert(ofnode_valid(phy_node));
466
467         reg = ofnode_read_u32_default(phy_node, "reg", -1U);
468         ut_asserteq_64(0x1, reg);
469
470         return 0;
471 }
472 DM_TEST(dm_test_ofnode_get_phy, 0);
473
474 /**
475  * make_ofnode_fdt() - Create an FDT for testing with ofnode
476  *
477  * The size is set to the minimum needed
478  *
479  * @uts: Test state
480  * @fdt: Place to write FDT
481  * @size: Maximum size of space for fdt
482  */
483 static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
484 {
485         ut_assertok(fdt_create(fdt, size));
486         ut_assertok(fdt_finish_reservemap(fdt));
487         ut_assert(fdt_begin_node(fdt, "") >= 0);
488
489         ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
490         ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
491         ut_assertok(fdt_end_node(fdt));
492
493         ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
494         ut_assertok(fdt_end_node(fdt));
495
496         ut_assertok(fdt_end_node(fdt));
497         ut_assertok(fdt_finish(fdt));
498
499         return 0;
500 }
501
502 static int dm_test_ofnode_root(struct unit_test_state *uts)
503 {
504         struct device_node *root = NULL;
505         char fdt[256];
506         oftree tree;
507         ofnode node;
508
509         /* Check that aliases work on the control FDT */
510         node = ofnode_get_aliases_node("ethernet3");
511         ut_assert(ofnode_valid(node));
512         ut_asserteq_str("sbe5", ofnode_get_name(node));
513
514         ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
515         if (of_live_active()) {
516                 ut_assertok(unflatten_device_tree(fdt, &root));
517                 tree.np = root;
518         } else {
519                 tree.fdt = fdt;
520         }
521
522         /* Make sure they don't work on this new tree */
523         node = ofnode_path_root(tree, "mmc0");
524         ut_assert(!ofnode_valid(node));
525
526         /* It should appear in the new tree */
527         node = ofnode_path_root(tree, "/new-mmc");
528         ut_assert(ofnode_valid(node));
529
530         /* ...and not in the control FDT */
531         node = ofnode_path_root(oftree_default(), "/new-mmc");
532         ut_assert(!ofnode_valid(node));
533
534         free(root);
535
536         return 0;
537 }
538 DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);