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