blob: e01acc4fe92250a47b79c36ec0f3676f3987eedf [file] [log] [blame]
Masahiro Yamada04ca8712018-04-27 01:02:02 +09001// SPDX-License-Identifier: GPL-2.0+
2
3#include <common.h>
4#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06005#include <log.h>
Simon Glasse6c5c942018-10-01 12:22:08 -06006#include <dm/of_extra.h>
Masahiro Yamada04ca8712018-04-27 01:02:02 +09007#include <dm/test.h>
Simon Glass0e1fad42020-07-19 10:15:37 -06008#include <test/test.h>
Masahiro Yamada04ca8712018-04-27 01:02:02 +09009#include <test/ut.h>
10
11static 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}
20DM_TEST(dm_test_ofnode_compatible, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Jens Wiklander9bc7e962018-08-20 11:10:00 +020021
22static 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}
47DM_TEST(dm_test_ofnode_by_prop_value, DM_TESTF_SCAN_FDT);
Simon Glasse6c5c942018-10-01 12:22:08 -060048
49static 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}
62DM_TEST(dm_test_ofnode_fmap, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass14ca9f72020-01-27 08:49:43 -070063
Simon Glassa8167d82020-01-27 08:49:44 -070064static 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}
88DM_TEST(dm_test_ofnode_read, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
89
Simon Glass14ca9f72020-01-27 08:49:43 -070090static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
91{
92 const char *str;
Simon Glassbd933bf2020-01-27 08:49:46 -070093 const u32 *val;
Simon Glass14ca9f72020-01-27 08:49:43 -070094 ofnode node;
Simon Glassbd933bf2020-01-27 08:49:46 -070095 int size;
Simon Glass14ca9f72020-01-27 08:49:43 -070096
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
Simon Glassbd933bf2020-01-27 08:49:46 -0700109 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
Simon Glass14ca9f72020-01-27 08:49:43 -0700115 return 0;
116}
117DM_TEST(dm_test_ofnode_read_chosen, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Chunfeng Yunbf6ad912020-05-02 11:35:10 +0200118
119static 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}
137DM_TEST(dm_test_ofnode_get_child_count,
138 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);