Masahiro Yamada | b4f2dda | 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 | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 5 | #include <log.h> |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame^] | 6 | #include <of_live.h> |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 7 | #include <dm/of_extra.h> |
Masahiro Yamada | b4f2dda | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 8 | #include <dm/test.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 9 | #include <test/test.h> |
Masahiro Yamada | b4f2dda | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 10 | #include <test/ut.h> |
| 11 | |
| 12 | static int dm_test_ofnode_compatible(struct unit_test_state *uts) |
| 13 | { |
| 14 | ofnode root_node = ofnode_path("/"); |
| 15 | |
| 16 | ut_assert(ofnode_valid(root_node)); |
| 17 | ut_assert(ofnode_device_is_compatible(root_node, "sandbox")); |
| 18 | |
| 19 | return 0; |
| 20 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 21 | DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Jens Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 22 | |
Patrick Delaunay | 04fcfe7 | 2020-09-24 17:26:20 +0200 | [diff] [blame] | 23 | static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts) |
| 24 | { |
| 25 | /* test invalid phandle */ |
| 26 | ut_assert(!ofnode_valid(ofnode_get_by_phandle(0))); |
| 27 | ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1))); |
| 28 | |
| 29 | /* test first valid phandle */ |
| 30 | ut_assert(ofnode_valid(ofnode_get_by_phandle(1))); |
| 31 | |
| 32 | /* test unknown phandle */ |
| 33 | ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000))); |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 38 | |
Jens Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 39 | static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts) |
| 40 | { |
| 41 | const char propname[] = "compatible"; |
| 42 | const char propval[] = "denx,u-boot-fdt-test"; |
| 43 | const char *str; |
| 44 | ofnode node = ofnode_null(); |
| 45 | |
| 46 | /* Find first matching node, there should be at least one */ |
| 47 | node = ofnode_by_prop_value(node, propname, propval, sizeof(propval)); |
| 48 | ut_assert(ofnode_valid(node)); |
| 49 | str = ofnode_read_string(node, propname); |
| 50 | ut_assert(str && !strcmp(str, propval)); |
| 51 | |
| 52 | /* Find the rest of the matching nodes */ |
| 53 | while (true) { |
| 54 | node = ofnode_by_prop_value(node, propname, propval, |
| 55 | sizeof(propval)); |
| 56 | if (!ofnode_valid(node)) |
| 57 | break; |
| 58 | str = ofnode_read_string(node, propname); |
| 59 | ut_assert(str && !strcmp(str, propval)); |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 64 | DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT); |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 65 | |
| 66 | static int dm_test_ofnode_fmap(struct unit_test_state *uts) |
| 67 | { |
| 68 | struct fmap_entry entry; |
| 69 | ofnode node; |
| 70 | |
| 71 | node = ofnode_path("/cros-ec/flash"); |
| 72 | ut_assert(ofnode_valid(node)); |
| 73 | ut_assertok(ofnode_read_fmap_entry(node, &entry)); |
| 74 | ut_asserteq(0x08000000, entry.offset); |
| 75 | ut_asserteq(0x20000, entry.length); |
| 76 | |
| 77 | return 0; |
| 78 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 79 | DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 80 | |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 81 | static int dm_test_ofnode_read(struct unit_test_state *uts) |
| 82 | { |
| 83 | const u32 *val; |
| 84 | ofnode node; |
| 85 | int size; |
| 86 | |
| 87 | node = ofnode_path("/a-test"); |
| 88 | ut_assert(ofnode_valid(node)); |
| 89 | |
| 90 | val = ofnode_read_prop(node, "int-value", &size); |
| 91 | ut_assertnonnull(val); |
| 92 | ut_asserteq(4, size); |
| 93 | ut_asserteq(1234, fdt32_to_cpu(val[0])); |
| 94 | |
| 95 | val = ofnode_read_prop(node, "missing", &size); |
| 96 | ut_assertnull(val); |
| 97 | ut_asserteq(-FDT_ERR_NOTFOUND, size); |
| 98 | |
| 99 | /* Check it works without a size parameter */ |
| 100 | val = ofnode_read_prop(node, "missing", NULL); |
| 101 | ut_assertnull(val); |
| 102 | |
| 103 | return 0; |
| 104 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 105 | DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 106 | |
Patrick Delaunay | 8cd2801 | 2020-09-25 09:41:16 +0200 | [diff] [blame] | 107 | static int dm_test_ofnode_phandle(struct unit_test_state *uts) |
| 108 | { |
| 109 | struct ofnode_phandle_args args; |
| 110 | ofnode node; |
| 111 | int ret; |
| 112 | const char prop[] = "test-gpios"; |
| 113 | const char cell[] = "#gpio-cells"; |
| 114 | const char prop2[] = "phandle-value"; |
| 115 | |
| 116 | node = ofnode_path("/a-test"); |
| 117 | ut_assert(ofnode_valid(node)); |
| 118 | |
| 119 | /* Test ofnode_count_phandle_with_args with cell name */ |
| 120 | ret = ofnode_count_phandle_with_args(node, "missing", cell, 0); |
| 121 | ut_asserteq(-ENOENT, ret); |
| 122 | ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0); |
| 123 | ut_asserteq(-EINVAL, ret); |
| 124 | ret = ofnode_count_phandle_with_args(node, prop, cell, 0); |
| 125 | ut_asserteq(5, ret); |
| 126 | |
| 127 | /* Test ofnode_parse_phandle_with_args with cell name */ |
| 128 | ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0, |
| 129 | &args); |
| 130 | ut_asserteq(-ENOENT, ret); |
| 131 | ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0, |
| 132 | &args); |
| 133 | ut_asserteq(-EINVAL, ret); |
| 134 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args); |
| 135 | ut_assertok(ret); |
| 136 | ut_asserteq(1, args.args_count); |
| 137 | ut_asserteq(1, args.args[0]); |
| 138 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args); |
| 139 | ut_assertok(ret); |
| 140 | ut_asserteq(1, args.args_count); |
| 141 | ut_asserteq(4, args.args[0]); |
| 142 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args); |
| 143 | ut_assertok(ret); |
| 144 | ut_asserteq(5, args.args_count); |
| 145 | ut_asserteq(5, args.args[0]); |
| 146 | ut_asserteq(1, args.args[4]); |
| 147 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args); |
| 148 | ut_asserteq(-ENOENT, ret); |
| 149 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args); |
| 150 | ut_assertok(ret); |
| 151 | ut_asserteq(1, args.args_count); |
| 152 | ut_asserteq(12, args.args[0]); |
| 153 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args); |
| 154 | ut_asserteq(-ENOENT, ret); |
| 155 | |
| 156 | /* Test ofnode_count_phandle_with_args with cell count */ |
| 157 | ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2); |
| 158 | ut_asserteq(-ENOENT, ret); |
| 159 | ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1); |
| 160 | ut_asserteq(3, ret); |
| 161 | |
| 162 | /* Test ofnode_parse_phandle_with_args with cell count */ |
| 163 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args); |
| 164 | ut_assertok(ret); |
| 165 | ut_asserteq(1, ofnode_valid(args.node)); |
| 166 | ut_asserteq(1, args.args_count); |
| 167 | ut_asserteq(10, args.args[0]); |
| 168 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args); |
| 169 | ut_asserteq(-EINVAL, ret); |
| 170 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args); |
| 171 | ut_assertok(ret); |
| 172 | ut_asserteq(1, ofnode_valid(args.node)); |
| 173 | ut_asserteq(1, args.args_count); |
| 174 | ut_asserteq(30, args.args[0]); |
| 175 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args); |
| 176 | ut_asserteq(-ENOENT, ret); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 181 | |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 182 | static int dm_test_ofnode_read_chosen(struct unit_test_state *uts) |
| 183 | { |
| 184 | const char *str; |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 185 | const u32 *val; |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 186 | ofnode node; |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 187 | int size; |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 188 | |
| 189 | str = ofnode_read_chosen_string("setting"); |
| 190 | ut_assertnonnull(str); |
| 191 | ut_asserteq_str("sunrise ohoka", str); |
| 192 | ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting")); |
| 193 | |
| 194 | node = ofnode_get_chosen_node("other-node"); |
| 195 | ut_assert(ofnode_valid(node)); |
| 196 | ut_asserteq_str("c-test@5", ofnode_get_name(node)); |
| 197 | |
| 198 | node = ofnode_get_chosen_node("setting"); |
| 199 | ut_assert(!ofnode_valid(node)); |
| 200 | |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 201 | val = ofnode_read_chosen_prop("int-values", &size); |
| 202 | ut_assertnonnull(val); |
| 203 | ut_asserteq(8, size); |
| 204 | ut_asserteq(0x1937, fdt32_to_cpu(val[0])); |
| 205 | ut_asserteq(72993, fdt32_to_cpu(val[1])); |
| 206 | |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 209 | DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
developer | cf8bc13 | 2020-05-02 11:35:10 +0200 | [diff] [blame] | 210 | |
Michal Simek | 92a8862 | 2020-07-28 12:51:08 +0200 | [diff] [blame] | 211 | static int dm_test_ofnode_read_aliases(struct unit_test_state *uts) |
| 212 | { |
| 213 | const void *val; |
| 214 | ofnode node; |
| 215 | int size; |
| 216 | |
Michael Walle | 7efcdfd | 2021-02-25 16:51:11 +0100 | [diff] [blame] | 217 | node = ofnode_get_aliases_node("ethernet3"); |
Michal Simek | 92a8862 | 2020-07-28 12:51:08 +0200 | [diff] [blame] | 218 | ut_assert(ofnode_valid(node)); |
| 219 | ut_asserteq_str("sbe5", ofnode_get_name(node)); |
| 220 | |
| 221 | node = ofnode_get_aliases_node("unknown"); |
| 222 | ut_assert(!ofnode_valid(node)); |
| 223 | |
| 224 | val = ofnode_read_aliases_prop("spi0", &size); |
| 225 | ut_assertnonnull(val); |
| 226 | ut_asserteq(7, size); |
| 227 | ut_asserteq_str("/spi@0", (const char *)val); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 232 | |
developer | cf8bc13 | 2020-05-02 11:35:10 +0200 | [diff] [blame] | 233 | static int dm_test_ofnode_get_child_count(struct unit_test_state *uts) |
| 234 | { |
| 235 | ofnode node, child_node; |
| 236 | u32 val; |
| 237 | |
| 238 | node = ofnode_path("/i-test"); |
| 239 | ut_assert(ofnode_valid(node)); |
| 240 | |
| 241 | val = ofnode_get_child_count(node); |
| 242 | ut_asserteq(3, val); |
| 243 | |
| 244 | child_node = ofnode_first_subnode(node); |
| 245 | ut_assert(ofnode_valid(child_node)); |
| 246 | val = ofnode_get_child_count(child_node); |
| 247 | ut_asserteq(0, val); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | DM_TEST(dm_test_ofnode_get_child_count, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 252 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 5de5b3b | 2020-11-28 17:50:02 -0700 | [diff] [blame] | 253 | |
| 254 | static int dm_test_ofnode_is_enabled(struct unit_test_state *uts) |
| 255 | { |
| 256 | ofnode root_node = ofnode_path("/"); |
| 257 | ofnode node = ofnode_path("/usb@0"); |
| 258 | |
| 259 | ut_assert(ofnode_is_enabled(root_node)); |
| 260 | ut_assert(!ofnode_is_enabled(node)); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Chen Guanqiao | be0512f | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 265 | |
| 266 | static int dm_test_ofnode_get_reg(struct unit_test_state *uts) |
| 267 | { |
| 268 | ofnode node; |
| 269 | fdt_addr_t addr; |
| 270 | fdt_size_t size; |
| 271 | |
| 272 | node = ofnode_path("/translation-test@8000"); |
| 273 | ut_assert(ofnode_valid(node)); |
| 274 | addr = ofnode_get_addr(node); |
| 275 | size = ofnode_get_size(node); |
| 276 | ut_asserteq(0x8000, addr); |
| 277 | ut_asserteq(0x4000, size); |
| 278 | |
| 279 | node = ofnode_path("/translation-test@8000/dev@1,100"); |
| 280 | ut_assert(ofnode_valid(node)); |
| 281 | addr = ofnode_get_addr(node); |
| 282 | size = ofnode_get_size(node); |
| 283 | ut_asserteq(0x9000, addr); |
| 284 | ut_asserteq(0x1000, size); |
| 285 | |
| 286 | node = ofnode_path("/emul-mux-controller"); |
| 287 | ut_assert(ofnode_valid(node)); |
| 288 | addr = ofnode_get_addr(node); |
| 289 | size = ofnode_get_size(node); |
Patrice Chotard | b4c5211 | 2022-01-04 08:42:48 +0100 | [diff] [blame] | 290 | ut_asserteq_64(FDT_ADDR_T_NONE, addr); |
Chen Guanqiao | be0512f | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 291 | ut_asserteq(FDT_SIZE_T_NONE, size); |
| 292 | |
Marek Behún | 177ab7f | 2021-05-26 14:08:17 +0200 | [diff] [blame] | 293 | node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42"); |
| 294 | ut_assert(ofnode_valid(node)); |
| 295 | addr = ofnode_get_addr_size_index_notrans(node, 0, &size); |
| 296 | ut_asserteq_64(0x42, addr); |
| 297 | |
Chen Guanqiao | be0512f | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Marek Behún | e897e3c | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 301 | |
| 302 | static int dm_test_ofnode_get_path(struct unit_test_state *uts) |
| 303 | { |
| 304 | const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42"; |
| 305 | char buf[64]; |
| 306 | ofnode node; |
| 307 | int res; |
| 308 | |
| 309 | node = ofnode_path(path); |
| 310 | ut_assert(ofnode_valid(node)); |
| 311 | |
| 312 | res = ofnode_get_path(node, buf, 64); |
| 313 | ut_asserteq(0, res); |
| 314 | ut_asserteq_str(path, buf); |
| 315 | |
| 316 | res = ofnode_get_path(node, buf, 32); |
| 317 | ut_asserteq(-ENOSPC, res); |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 0034d96 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 322 | |
| 323 | static int dm_test_ofnode_conf(struct unit_test_state *uts) |
| 324 | { |
| 325 | ut_assert(!ofnode_conf_read_bool("missing")); |
| 326 | ut_assert(ofnode_conf_read_bool("testing-bool")); |
| 327 | |
| 328 | ut_asserteq(123, ofnode_conf_read_int("testing-int", 0)); |
| 329 | ut_asserteq(6, ofnode_conf_read_int("missing", 6)); |
| 330 | |
| 331 | ut_assertnull(ofnode_conf_read_str("missing")); |
| 332 | ut_asserteq_str("testing", ofnode_conf_read_str("testing-str")); |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | DM_TEST(dm_test_ofnode_conf, 0); |
Michael Walle | f1f8740 | 2021-10-15 15:15:18 +0200 | [diff] [blame] | 337 | |
| 338 | static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts) |
| 339 | { |
| 340 | const char compatible[] = "denx,u-boot-fdt-test"; |
| 341 | bool found = false; |
| 342 | ofnode node; |
| 343 | |
| 344 | ofnode_for_each_compatible_node(node, compatible) { |
| 345 | ut_assert(ofnode_device_is_compatible(node, compatible)); |
| 346 | found = true; |
| 347 | } |
| 348 | |
| 349 | /* There should be at least one matching node */ |
| 350 | ut_assert(found); |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 355 | |
| 356 | static int dm_test_ofnode_string(struct unit_test_state *uts) |
| 357 | { |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 358 | const char **val; |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 359 | const char *out; |
| 360 | ofnode node; |
| 361 | |
| 362 | node = ofnode_path("/a-test"); |
| 363 | ut_assert(ofnode_valid(node)); |
| 364 | |
| 365 | /* single string */ |
| 366 | ut_asserteq(1, ofnode_read_string_count(node, "str-value")); |
| 367 | ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out)); |
| 368 | ut_asserteq_str("test string", out); |
| 369 | ut_asserteq(0, ofnode_stringlist_search(node, "str-value", |
| 370 | "test string")); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 371 | ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val)); |
| 372 | ut_asserteq_str("test string", val[0]); |
| 373 | ut_assertnull(val[1]); |
| 374 | free(val); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 375 | |
| 376 | /* list of strings */ |
| 377 | ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names")); |
| 378 | ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0, |
| 379 | &out)); |
| 380 | ut_asserteq_str("mux0", out); |
| 381 | ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names", |
| 382 | "mux0")); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 383 | ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names", |
| 384 | &val)); |
| 385 | ut_asserteq_str("mux0", val[0]); |
| 386 | ut_asserteq_str("mux1", val[1]); |
| 387 | ut_asserteq_str("mux2", val[2]); |
| 388 | ut_asserteq_str("mux3", val[3]); |
| 389 | ut_asserteq_str("mux4", val[4]); |
| 390 | ut_assertnull(val[5]); |
| 391 | free(val); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 392 | |
| 393 | ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4, |
| 394 | &out)); |
| 395 | ut_asserteq_str("mux4", out); |
| 396 | ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names", |
| 397 | "mux4")); |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | DM_TEST(dm_test_ofnode_string, 0); |
| 402 | |
| 403 | static int dm_test_ofnode_string_err(struct unit_test_state *uts) |
| 404 | { |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 405 | const char **val; |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 406 | const char *out; |
| 407 | ofnode node; |
| 408 | |
| 409 | /* |
| 410 | * Test error codes only on livetree, as they are different with |
| 411 | * flattree |
| 412 | */ |
| 413 | node = ofnode_path("/a-test"); |
| 414 | ut_assert(ofnode_valid(node)); |
| 415 | |
| 416 | /* non-existent property */ |
| 417 | ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing")); |
| 418 | ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0, |
| 419 | &out)); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 420 | ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val)); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 421 | |
| 422 | /* empty property */ |
| 423 | ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value")); |
| 424 | ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0, |
| 425 | &out)); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 426 | ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value", |
| 427 | &val)); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 428 | |
| 429 | /* badly formatted string list */ |
| 430 | ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value")); |
| 431 | ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0, |
| 432 | &out)); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 433 | ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value", |
| 434 | &val)); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 435 | |
| 436 | /* out of range / not found */ |
| 437 | ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1, |
| 438 | &out)); |
| 439 | ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value", |
| 440 | "other")); |
| 441 | |
| 442 | /* negative value for index is not allowed, so don't test for that */ |
| 443 | |
| 444 | ut_asserteq(-ENODATA, ofnode_read_string_index(node, |
| 445 | "mux-control-names", 5, |
| 446 | &out)); |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE); |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 451 | |
| 452 | static int dm_test_ofnode_get_phy(struct unit_test_state *uts) |
| 453 | { |
| 454 | ofnode eth_node, phy_node; |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 455 | phy_interface_t mode; |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 456 | u32 reg; |
| 457 | |
| 458 | eth_node = ofnode_path("/phy-test-eth"); |
| 459 | ut_assert(ofnode_valid(eth_node)); |
| 460 | |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 461 | mode = ofnode_read_phy_mode(eth_node); |
| 462 | ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX); |
| 463 | |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 464 | phy_node = ofnode_get_phy_node(eth_node); |
| 465 | ut_assert(ofnode_valid(phy_node)); |
| 466 | |
| 467 | reg = ofnode_read_u32_default(phy_node, "reg", -1U); |
| 468 | ut_asserteq_64(0x1, reg); |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | DM_TEST(dm_test_ofnode_get_phy, 0); |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame^] | 473 | |
| 474 | /** |
| 475 | * make_ofnode_fdt() - Create an FDT for testing with ofnode |
| 476 | * |
| 477 | * The size is set to the minimum needed |
| 478 | * |
| 479 | * @uts: Test state |
| 480 | * @fdt: Place to write FDT |
| 481 | * @size: Maximum size of space for fdt |
| 482 | */ |
| 483 | static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size) |
| 484 | { |
| 485 | ut_assertok(fdt_create(fdt, size)); |
| 486 | ut_assertok(fdt_finish_reservemap(fdt)); |
| 487 | ut_assert(fdt_begin_node(fdt, "") >= 0); |
| 488 | |
| 489 | ut_assert(fdt_begin_node(fdt, "aliases") >= 0); |
| 490 | ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc")); |
| 491 | ut_assertok(fdt_end_node(fdt)); |
| 492 | |
| 493 | ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0); |
| 494 | ut_assertok(fdt_end_node(fdt)); |
| 495 | |
| 496 | ut_assertok(fdt_end_node(fdt)); |
| 497 | ut_assertok(fdt_finish(fdt)); |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static int dm_test_ofnode_root(struct unit_test_state *uts) |
| 503 | { |
| 504 | struct device_node *root = NULL; |
| 505 | char fdt[256]; |
| 506 | oftree tree; |
| 507 | ofnode node; |
| 508 | |
| 509 | /* Check that aliases work on the control FDT */ |
| 510 | node = ofnode_get_aliases_node("ethernet3"); |
| 511 | ut_assert(ofnode_valid(node)); |
| 512 | ut_asserteq_str("sbe5", ofnode_get_name(node)); |
| 513 | |
| 514 | ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt))); |
| 515 | if (of_live_active()) { |
| 516 | ut_assertok(unflatten_device_tree(fdt, &root)); |
| 517 | tree.np = root; |
| 518 | } else { |
| 519 | tree.fdt = fdt; |
| 520 | } |
| 521 | |
| 522 | /* Make sure they don't work on this new tree */ |
| 523 | node = ofnode_path_root(tree, "mmc0"); |
| 524 | ut_assert(!ofnode_valid(node)); |
| 525 | |
| 526 | /* It should appear in the new tree */ |
| 527 | node = ofnode_path_root(tree, "/new-mmc"); |
| 528 | ut_assert(ofnode_valid(node)); |
| 529 | |
| 530 | /* ...and not in the control FDT */ |
| 531 | node = ofnode_path_root(oftree_default(), "/new-mmc"); |
| 532 | ut_assert(!ofnode_valid(node)); |
| 533 | |
| 534 | free(root); |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT); |