blob: 1114f34cbf8dbb4bb6016f66f36815b922ed4e92 [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>
8#include <test/ut.h>
9
10static 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}
19DM_TEST(dm_test_ofnode_compatible, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +020020
21static 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}
46DM_TEST(dm_test_ofnode_by_prop_value, DM_TESTF_SCAN_FDT);
Simon Glass699c9ca2018-10-01 12:22:08 -060047
48static 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}
61DM_TEST(dm_test_ofnode_fmap, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassf3455962020-01-27 08:49:43 -070062
Simon Glass0c2e9802020-01-27 08:49:44 -070063static 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}
87DM_TEST(dm_test_ofnode_read, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
88
Simon Glassf3455962020-01-27 08:49:43 -070089static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
90{
91 const char *str;
Simon Glasse09223c2020-01-27 08:49:46 -070092 const u32 *val;
Simon Glassf3455962020-01-27 08:49:43 -070093 ofnode node;
Simon Glasse09223c2020-01-27 08:49:46 -070094 int size;
Simon Glassf3455962020-01-27 08:49:43 -070095
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
Simon Glasse09223c2020-01-27 08:49:46 -0700108 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
Simon Glassf3455962020-01-27 08:49:43 -0700114 return 0;
115}
116DM_TEST(dm_test_ofnode_read_chosen, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
developercf8bc132020-05-02 11:35:10 +0200117
118static 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}
136DM_TEST(dm_test_ofnode_get_child_count,
137 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);