Merge https://gitlab.denx.de/u-boot/custodians/u-boot-riscv
[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 <dm/of_extra.h>
7 #include <dm/test.h>
8 #include <test/ut.h>
9
10 static int dm_test_ofnode_compatible(struct unit_test_state *uts)
11 {
12         ofnode root_node = ofnode_path("/");
13
14         ut_assert(ofnode_valid(root_node));
15         ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
16
17         return 0;
18 }
19 DM_TEST(dm_test_ofnode_compatible, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
20
21 static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
22 {
23         const char propname[] = "compatible";
24         const char propval[] = "denx,u-boot-fdt-test";
25         const char *str;
26         ofnode node = ofnode_null();
27
28         /* Find first matching node, there should be at least one */
29         node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
30         ut_assert(ofnode_valid(node));
31         str = ofnode_read_string(node, propname);
32         ut_assert(str && !strcmp(str, propval));
33
34         /* Find the rest of the matching nodes */
35         while (true) {
36                 node = ofnode_by_prop_value(node, propname, propval,
37                                             sizeof(propval));
38                 if (!ofnode_valid(node))
39                         break;
40                 str = ofnode_read_string(node, propname);
41                 ut_assert(str && !strcmp(str, propval));
42         }
43
44         return 0;
45 }
46 DM_TEST(dm_test_ofnode_by_prop_value, DM_TESTF_SCAN_FDT);
47
48 static int dm_test_ofnode_fmap(struct unit_test_state *uts)
49 {
50         struct fmap_entry entry;
51         ofnode node;
52
53         node = ofnode_path("/cros-ec/flash");
54         ut_assert(ofnode_valid(node));
55         ut_assertok(ofnode_read_fmap_entry(node, &entry));
56         ut_asserteq(0x08000000, entry.offset);
57         ut_asserteq(0x20000, entry.length);
58
59         return 0;
60 }
61 DM_TEST(dm_test_ofnode_fmap, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
62
63 static int dm_test_ofnode_read(struct unit_test_state *uts)
64 {
65         const u32 *val;
66         ofnode node;
67         int size;
68
69         node = ofnode_path("/a-test");
70         ut_assert(ofnode_valid(node));
71
72         val = ofnode_read_prop(node, "int-value", &size);
73         ut_assertnonnull(val);
74         ut_asserteq(4, size);
75         ut_asserteq(1234, fdt32_to_cpu(val[0]));
76
77         val = ofnode_read_prop(node, "missing", &size);
78         ut_assertnull(val);
79         ut_asserteq(-FDT_ERR_NOTFOUND, size);
80
81         /* Check it works without a size parameter */
82         val = ofnode_read_prop(node, "missing", NULL);
83         ut_assertnull(val);
84
85         return 0;
86 }
87 DM_TEST(dm_test_ofnode_read, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
88
89 static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
90 {
91         const char *str;
92         const u32 *val;
93         ofnode node;
94         int size;
95
96         str = ofnode_read_chosen_string("setting");
97         ut_assertnonnull(str);
98         ut_asserteq_str("sunrise ohoka", str);
99         ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
100
101         node = ofnode_get_chosen_node("other-node");
102         ut_assert(ofnode_valid(node));
103         ut_asserteq_str("c-test@5", ofnode_get_name(node));
104
105         node = ofnode_get_chosen_node("setting");
106         ut_assert(!ofnode_valid(node));
107
108         val = ofnode_read_chosen_prop("int-values", &size);
109         ut_assertnonnull(val);
110         ut_asserteq(8, size);
111         ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
112         ut_asserteq(72993, fdt32_to_cpu(val[1]));
113
114         return 0;
115 }
116 DM_TEST(dm_test_ofnode_read_chosen, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
117
118 static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
119 {
120         ofnode node, child_node;
121         u32 val;
122
123         node = ofnode_path("/i-test");
124         ut_assert(ofnode_valid(node));
125
126         val = ofnode_get_child_count(node);
127         ut_asserteq(3, val);
128
129         child_node = ofnode_first_subnode(node);
130         ut_assert(ofnode_valid(child_node));
131         val = ofnode_get_child_count(child_node);
132         ut_asserteq(0, val);
133
134         return 0;
135 }
136 DM_TEST(dm_test_ofnode_get_child_count,
137         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);