Masahiro Yamada | 04ca871 | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
| 3 | #include <common.h> |
| 4 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 5 | #include <log.h> |
Simon Glass | e6c5c94 | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 6 | #include <dm/of_extra.h> |
Masahiro Yamada | 04ca871 | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 7 | #include <dm/test.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 8 | #include <test/test.h> |
Masahiro Yamada | 04ca871 | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 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 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 20 | DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Jens Wiklander | 9bc7e96 | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 21 | |
Patrick Delaunay | 6d9949f | 2020-09-24 17:26:20 +0200 | [diff] [blame] | 22 | static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts) |
| 23 | { |
| 24 | /* test invalid phandle */ |
| 25 | ut_assert(!ofnode_valid(ofnode_get_by_phandle(0))); |
| 26 | ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1))); |
| 27 | |
| 28 | /* test first valid phandle */ |
| 29 | ut_assert(ofnode_valid(ofnode_get_by_phandle(1))); |
| 30 | |
| 31 | /* test unknown phandle */ |
| 32 | ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000))); |
| 33 | |
| 34 | return 0; |
| 35 | } |
| 36 | DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 37 | |
Jens Wiklander | 9bc7e96 | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 38 | static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts) |
| 39 | { |
| 40 | const char propname[] = "compatible"; |
| 41 | const char propval[] = "denx,u-boot-fdt-test"; |
| 42 | const char *str; |
| 43 | ofnode node = ofnode_null(); |
| 44 | |
| 45 | /* Find first matching node, there should be at least one */ |
| 46 | node = ofnode_by_prop_value(node, propname, propval, sizeof(propval)); |
| 47 | ut_assert(ofnode_valid(node)); |
| 48 | str = ofnode_read_string(node, propname); |
| 49 | ut_assert(str && !strcmp(str, propval)); |
| 50 | |
| 51 | /* Find the rest of the matching nodes */ |
| 52 | while (true) { |
| 53 | node = ofnode_by_prop_value(node, propname, propval, |
| 54 | sizeof(propval)); |
| 55 | if (!ofnode_valid(node)) |
| 56 | break; |
| 57 | str = ofnode_read_string(node, propname); |
| 58 | ut_assert(str && !strcmp(str, propval)); |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 63 | DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT); |
Simon Glass | e6c5c94 | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 64 | |
| 65 | static int dm_test_ofnode_fmap(struct unit_test_state *uts) |
| 66 | { |
| 67 | struct fmap_entry entry; |
| 68 | ofnode node; |
| 69 | |
| 70 | node = ofnode_path("/cros-ec/flash"); |
| 71 | ut_assert(ofnode_valid(node)); |
| 72 | ut_assertok(ofnode_read_fmap_entry(node, &entry)); |
| 73 | ut_asserteq(0x08000000, entry.offset); |
| 74 | ut_asserteq(0x20000, entry.length); |
| 75 | |
| 76 | return 0; |
| 77 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 78 | DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 14ca9f7 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 79 | |
Simon Glass | a8167d8 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 80 | static int dm_test_ofnode_read(struct unit_test_state *uts) |
| 81 | { |
| 82 | const u32 *val; |
| 83 | ofnode node; |
| 84 | int size; |
| 85 | |
| 86 | node = ofnode_path("/a-test"); |
| 87 | ut_assert(ofnode_valid(node)); |
| 88 | |
| 89 | val = ofnode_read_prop(node, "int-value", &size); |
| 90 | ut_assertnonnull(val); |
| 91 | ut_asserteq(4, size); |
| 92 | ut_asserteq(1234, fdt32_to_cpu(val[0])); |
| 93 | |
| 94 | val = ofnode_read_prop(node, "missing", &size); |
| 95 | ut_assertnull(val); |
| 96 | ut_asserteq(-FDT_ERR_NOTFOUND, size); |
| 97 | |
| 98 | /* Check it works without a size parameter */ |
| 99 | val = ofnode_read_prop(node, "missing", NULL); |
| 100 | ut_assertnull(val); |
| 101 | |
| 102 | return 0; |
| 103 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 104 | DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | a8167d8 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 105 | |
Patrick Delaunay | cc72f3e | 2020-09-25 09:41:16 +0200 | [diff] [blame] | 106 | static int dm_test_ofnode_phandle(struct unit_test_state *uts) |
| 107 | { |
| 108 | struct ofnode_phandle_args args; |
| 109 | ofnode node; |
| 110 | int ret; |
| 111 | const char prop[] = "test-gpios"; |
| 112 | const char cell[] = "#gpio-cells"; |
| 113 | const char prop2[] = "phandle-value"; |
| 114 | |
| 115 | node = ofnode_path("/a-test"); |
| 116 | ut_assert(ofnode_valid(node)); |
| 117 | |
| 118 | /* Test ofnode_count_phandle_with_args with cell name */ |
| 119 | ret = ofnode_count_phandle_with_args(node, "missing", cell, 0); |
| 120 | ut_asserteq(-ENOENT, ret); |
| 121 | ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0); |
| 122 | ut_asserteq(-EINVAL, ret); |
| 123 | ret = ofnode_count_phandle_with_args(node, prop, cell, 0); |
| 124 | ut_asserteq(5, ret); |
| 125 | |
| 126 | /* Test ofnode_parse_phandle_with_args with cell name */ |
| 127 | ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0, |
| 128 | &args); |
| 129 | ut_asserteq(-ENOENT, ret); |
| 130 | ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0, |
| 131 | &args); |
| 132 | ut_asserteq(-EINVAL, ret); |
| 133 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args); |
| 134 | ut_assertok(ret); |
| 135 | ut_asserteq(1, args.args_count); |
| 136 | ut_asserteq(1, args.args[0]); |
| 137 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args); |
| 138 | ut_assertok(ret); |
| 139 | ut_asserteq(1, args.args_count); |
| 140 | ut_asserteq(4, args.args[0]); |
| 141 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args); |
| 142 | ut_assertok(ret); |
| 143 | ut_asserteq(5, args.args_count); |
| 144 | ut_asserteq(5, args.args[0]); |
| 145 | ut_asserteq(1, args.args[4]); |
| 146 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args); |
| 147 | ut_asserteq(-ENOENT, ret); |
| 148 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args); |
| 149 | ut_assertok(ret); |
| 150 | ut_asserteq(1, args.args_count); |
| 151 | ut_asserteq(12, args.args[0]); |
| 152 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args); |
| 153 | ut_asserteq(-ENOENT, ret); |
| 154 | |
| 155 | /* Test ofnode_count_phandle_with_args with cell count */ |
| 156 | ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2); |
| 157 | ut_asserteq(-ENOENT, ret); |
| 158 | ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1); |
| 159 | ut_asserteq(3, ret); |
| 160 | |
| 161 | /* Test ofnode_parse_phandle_with_args with cell count */ |
| 162 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args); |
| 163 | ut_assertok(ret); |
| 164 | ut_asserteq(1, ofnode_valid(args.node)); |
| 165 | ut_asserteq(1, args.args_count); |
| 166 | ut_asserteq(10, args.args[0]); |
| 167 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args); |
| 168 | ut_asserteq(-EINVAL, ret); |
| 169 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args); |
| 170 | ut_assertok(ret); |
| 171 | ut_asserteq(1, ofnode_valid(args.node)); |
| 172 | ut_asserteq(1, args.args_count); |
| 173 | ut_asserteq(30, args.args[0]); |
| 174 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args); |
| 175 | ut_asserteq(-ENOENT, ret); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 180 | |
Simon Glass | 14ca9f7 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 181 | static int dm_test_ofnode_read_chosen(struct unit_test_state *uts) |
| 182 | { |
| 183 | const char *str; |
Simon Glass | bd933bf | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 184 | const u32 *val; |
Simon Glass | 14ca9f7 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 185 | ofnode node; |
Simon Glass | bd933bf | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 186 | int size; |
Simon Glass | 14ca9f7 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 187 | |
| 188 | str = ofnode_read_chosen_string("setting"); |
| 189 | ut_assertnonnull(str); |
| 190 | ut_asserteq_str("sunrise ohoka", str); |
| 191 | ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting")); |
| 192 | |
| 193 | node = ofnode_get_chosen_node("other-node"); |
| 194 | ut_assert(ofnode_valid(node)); |
| 195 | ut_asserteq_str("c-test@5", ofnode_get_name(node)); |
| 196 | |
| 197 | node = ofnode_get_chosen_node("setting"); |
| 198 | ut_assert(!ofnode_valid(node)); |
| 199 | |
Simon Glass | bd933bf | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 200 | val = ofnode_read_chosen_prop("int-values", &size); |
| 201 | ut_assertnonnull(val); |
| 202 | ut_asserteq(8, size); |
| 203 | ut_asserteq(0x1937, fdt32_to_cpu(val[0])); |
| 204 | ut_asserteq(72993, fdt32_to_cpu(val[1])); |
| 205 | |
Simon Glass | 14ca9f7 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 206 | return 0; |
| 207 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 208 | DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Chunfeng Yun | bf6ad91 | 2020-05-02 11:35:10 +0200 | [diff] [blame] | 209 | |
Michal Simek | 305d318 | 2020-07-28 12:51:08 +0200 | [diff] [blame] | 210 | static int dm_test_ofnode_read_aliases(struct unit_test_state *uts) |
| 211 | { |
| 212 | const void *val; |
| 213 | ofnode node; |
| 214 | int size; |
| 215 | |
| 216 | node = ofnode_get_aliases_node("eth3"); |
| 217 | ut_assert(ofnode_valid(node)); |
| 218 | ut_asserteq_str("sbe5", ofnode_get_name(node)); |
| 219 | |
| 220 | node = ofnode_get_aliases_node("unknown"); |
| 221 | ut_assert(!ofnode_valid(node)); |
| 222 | |
| 223 | val = ofnode_read_aliases_prop("spi0", &size); |
| 224 | ut_assertnonnull(val); |
| 225 | ut_asserteq(7, size); |
| 226 | ut_asserteq_str("/spi@0", (const char *)val); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 231 | |
Chunfeng Yun | bf6ad91 | 2020-05-02 11:35:10 +0200 | [diff] [blame] | 232 | static int dm_test_ofnode_get_child_count(struct unit_test_state *uts) |
| 233 | { |
| 234 | ofnode node, child_node; |
| 235 | u32 val; |
| 236 | |
| 237 | node = ofnode_path("/i-test"); |
| 238 | ut_assert(ofnode_valid(node)); |
| 239 | |
| 240 | val = ofnode_get_child_count(node); |
| 241 | ut_asserteq(3, val); |
| 242 | |
| 243 | child_node = ofnode_first_subnode(node); |
| 244 | ut_assert(ofnode_valid(child_node)); |
| 245 | val = ofnode_get_child_count(child_node); |
| 246 | ut_asserteq(0, val); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | DM_TEST(dm_test_ofnode_get_child_count, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 251 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |