blob: 8bfb706602698eae425ab24774a0b201c3be6a17 [file] [log] [blame]
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +09001// SPDX-License-Identifier: GPL-2.0+
2
3#include <common.h>
4#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06005#include <log.h>
Simon Glass699c9ca2018-10-01 12:22:08 -06006#include <dm/of_extra.h>
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +09007#include <dm/test.h>
Simon Glass75c4d412020-07-19 10:15:37 -06008#include <test/test.h>
Masahiro Yamadab4f2dda2018-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}
Simon Glass974dccd2020-07-28 19:41:12 -060020DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Jens Wiklanderd9fd0ac2018-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}
Simon Glass974dccd2020-07-28 19:41:12 -060047DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
Simon Glass699c9ca2018-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}
Simon Glass974dccd2020-07-28 19:41:12 -060062DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf3455962020-01-27 08:49:43 -070063
Simon Glass0c2e9802020-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}
Simon Glass974dccd2020-07-28 19:41:12 -060088DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0c2e9802020-01-27 08:49:44 -070089
Simon Glassf3455962020-01-27 08:49:43 -070090static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
91{
92 const char *str;
Simon Glasse09223c2020-01-27 08:49:46 -070093 const u32 *val;
Simon Glassf3455962020-01-27 08:49:43 -070094 ofnode node;
Simon Glasse09223c2020-01-27 08:49:46 -070095 int size;
Simon Glassf3455962020-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 Glasse09223c2020-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 Glassf3455962020-01-27 08:49:43 -0700115 return 0;
116}
Simon Glass974dccd2020-07-28 19:41:12 -0600117DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
developercf8bc132020-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,
Simon Glass974dccd2020-07-28 19:41:12 -0600138 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);