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 | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 7 | #include <dm/device-internal.h> |
| 8 | #include <dm/lists.h> |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 9 | #include <dm/of_extra.h> |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 10 | #include <dm/root.h> |
Masahiro Yamada | b4f2dda | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 11 | #include <dm/test.h> |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 12 | #include <dm/uclass-internal.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 13 | #include <test/test.h> |
Masahiro Yamada | b4f2dda | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 14 | #include <test/ut.h> |
| 15 | |
| 16 | static int dm_test_ofnode_compatible(struct unit_test_state *uts) |
| 17 | { |
| 18 | ofnode root_node = ofnode_path("/"); |
| 19 | |
| 20 | ut_assert(ofnode_valid(root_node)); |
| 21 | ut_assert(ofnode_device_is_compatible(root_node, "sandbox")); |
| 22 | |
| 23 | return 0; |
| 24 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 25 | 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] | 26 | |
Patrick Delaunay | 04fcfe7 | 2020-09-24 17:26:20 +0200 | [diff] [blame] | 27 | static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts) |
| 28 | { |
| 29 | /* test invalid phandle */ |
| 30 | ut_assert(!ofnode_valid(ofnode_get_by_phandle(0))); |
| 31 | ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1))); |
| 32 | |
| 33 | /* test first valid phandle */ |
| 34 | ut_assert(ofnode_valid(ofnode_get_by_phandle(1))); |
| 35 | |
| 36 | /* test unknown phandle */ |
| 37 | ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000))); |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 42 | |
Jens Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 43 | static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts) |
| 44 | { |
| 45 | const char propname[] = "compatible"; |
| 46 | const char propval[] = "denx,u-boot-fdt-test"; |
| 47 | const char *str; |
| 48 | ofnode node = ofnode_null(); |
| 49 | |
| 50 | /* Find first matching node, there should be at least one */ |
| 51 | node = ofnode_by_prop_value(node, propname, propval, sizeof(propval)); |
| 52 | ut_assert(ofnode_valid(node)); |
| 53 | str = ofnode_read_string(node, propname); |
| 54 | ut_assert(str && !strcmp(str, propval)); |
| 55 | |
| 56 | /* Find the rest of the matching nodes */ |
| 57 | while (true) { |
| 58 | node = ofnode_by_prop_value(node, propname, propval, |
| 59 | sizeof(propval)); |
| 60 | if (!ofnode_valid(node)) |
| 61 | break; |
| 62 | str = ofnode_read_string(node, propname); |
| 63 | ut_assert(str && !strcmp(str, propval)); |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 68 | DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT); |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 69 | |
| 70 | static int dm_test_ofnode_fmap(struct unit_test_state *uts) |
| 71 | { |
| 72 | struct fmap_entry entry; |
| 73 | ofnode node; |
| 74 | |
| 75 | node = ofnode_path("/cros-ec/flash"); |
| 76 | ut_assert(ofnode_valid(node)); |
| 77 | ut_assertok(ofnode_read_fmap_entry(node, &entry)); |
| 78 | ut_asserteq(0x08000000, entry.offset); |
| 79 | ut_asserteq(0x20000, entry.length); |
| 80 | |
| 81 | return 0; |
| 82 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 83 | 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] | 84 | |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 85 | static int dm_test_ofnode_read(struct unit_test_state *uts) |
| 86 | { |
| 87 | const u32 *val; |
| 88 | ofnode node; |
| 89 | int size; |
| 90 | |
| 91 | node = ofnode_path("/a-test"); |
| 92 | ut_assert(ofnode_valid(node)); |
| 93 | |
| 94 | val = ofnode_read_prop(node, "int-value", &size); |
| 95 | ut_assertnonnull(val); |
| 96 | ut_asserteq(4, size); |
| 97 | ut_asserteq(1234, fdt32_to_cpu(val[0])); |
| 98 | |
| 99 | val = ofnode_read_prop(node, "missing", &size); |
| 100 | ut_assertnull(val); |
| 101 | ut_asserteq(-FDT_ERR_NOTFOUND, size); |
| 102 | |
| 103 | /* Check it works without a size parameter */ |
| 104 | val = ofnode_read_prop(node, "missing", NULL); |
| 105 | ut_assertnull(val); |
| 106 | |
| 107 | return 0; |
| 108 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 109 | 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] | 110 | |
Patrick Delaunay | 8cd2801 | 2020-09-25 09:41:16 +0200 | [diff] [blame] | 111 | static int dm_test_ofnode_phandle(struct unit_test_state *uts) |
| 112 | { |
| 113 | struct ofnode_phandle_args args; |
| 114 | ofnode node; |
| 115 | int ret; |
| 116 | const char prop[] = "test-gpios"; |
| 117 | const char cell[] = "#gpio-cells"; |
| 118 | const char prop2[] = "phandle-value"; |
| 119 | |
| 120 | node = ofnode_path("/a-test"); |
| 121 | ut_assert(ofnode_valid(node)); |
| 122 | |
| 123 | /* Test ofnode_count_phandle_with_args with cell name */ |
| 124 | ret = ofnode_count_phandle_with_args(node, "missing", cell, 0); |
| 125 | ut_asserteq(-ENOENT, ret); |
| 126 | ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0); |
| 127 | ut_asserteq(-EINVAL, ret); |
| 128 | ret = ofnode_count_phandle_with_args(node, prop, cell, 0); |
| 129 | ut_asserteq(5, ret); |
| 130 | |
| 131 | /* Test ofnode_parse_phandle_with_args with cell name */ |
| 132 | ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0, |
| 133 | &args); |
| 134 | ut_asserteq(-ENOENT, ret); |
| 135 | ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0, |
| 136 | &args); |
| 137 | ut_asserteq(-EINVAL, ret); |
| 138 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args); |
| 139 | ut_assertok(ret); |
| 140 | ut_asserteq(1, args.args_count); |
| 141 | ut_asserteq(1, args.args[0]); |
| 142 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args); |
| 143 | ut_assertok(ret); |
| 144 | ut_asserteq(1, args.args_count); |
| 145 | ut_asserteq(4, args.args[0]); |
| 146 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args); |
| 147 | ut_assertok(ret); |
| 148 | ut_asserteq(5, args.args_count); |
| 149 | ut_asserteq(5, args.args[0]); |
| 150 | ut_asserteq(1, args.args[4]); |
| 151 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args); |
| 152 | ut_asserteq(-ENOENT, ret); |
| 153 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args); |
| 154 | ut_assertok(ret); |
| 155 | ut_asserteq(1, args.args_count); |
| 156 | ut_asserteq(12, args.args[0]); |
| 157 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args); |
| 158 | ut_asserteq(-ENOENT, ret); |
| 159 | |
| 160 | /* Test ofnode_count_phandle_with_args with cell count */ |
| 161 | ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2); |
| 162 | ut_asserteq(-ENOENT, ret); |
| 163 | ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1); |
| 164 | ut_asserteq(3, ret); |
| 165 | |
| 166 | /* Test ofnode_parse_phandle_with_args with cell count */ |
| 167 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args); |
| 168 | ut_assertok(ret); |
| 169 | ut_asserteq(1, ofnode_valid(args.node)); |
| 170 | ut_asserteq(1, args.args_count); |
| 171 | ut_asserteq(10, args.args[0]); |
| 172 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args); |
| 173 | ut_asserteq(-EINVAL, ret); |
| 174 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args); |
| 175 | ut_assertok(ret); |
| 176 | ut_asserteq(1, ofnode_valid(args.node)); |
| 177 | ut_asserteq(1, args.args_count); |
| 178 | ut_asserteq(30, args.args[0]); |
| 179 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args); |
| 180 | ut_asserteq(-ENOENT, ret); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 185 | |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 186 | static int dm_test_ofnode_read_chosen(struct unit_test_state *uts) |
| 187 | { |
| 188 | const char *str; |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 189 | const u32 *val; |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 190 | ofnode node; |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 191 | int size; |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 192 | |
| 193 | str = ofnode_read_chosen_string("setting"); |
| 194 | ut_assertnonnull(str); |
| 195 | ut_asserteq_str("sunrise ohoka", str); |
| 196 | ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting")); |
| 197 | |
| 198 | node = ofnode_get_chosen_node("other-node"); |
| 199 | ut_assert(ofnode_valid(node)); |
| 200 | ut_asserteq_str("c-test@5", ofnode_get_name(node)); |
| 201 | |
| 202 | node = ofnode_get_chosen_node("setting"); |
| 203 | ut_assert(!ofnode_valid(node)); |
| 204 | |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 205 | val = ofnode_read_chosen_prop("int-values", &size); |
| 206 | ut_assertnonnull(val); |
| 207 | ut_asserteq(8, size); |
| 208 | ut_asserteq(0x1937, fdt32_to_cpu(val[0])); |
| 209 | ut_asserteq(72993, fdt32_to_cpu(val[1])); |
| 210 | |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 211 | return 0; |
| 212 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 213 | 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] | 214 | |
Michal Simek | 92a8862 | 2020-07-28 12:51:08 +0200 | [diff] [blame] | 215 | static int dm_test_ofnode_read_aliases(struct unit_test_state *uts) |
| 216 | { |
| 217 | const void *val; |
| 218 | ofnode node; |
| 219 | int size; |
| 220 | |
Michael Walle | 7efcdfd | 2021-02-25 16:51:11 +0100 | [diff] [blame] | 221 | node = ofnode_get_aliases_node("ethernet3"); |
Michal Simek | 92a8862 | 2020-07-28 12:51:08 +0200 | [diff] [blame] | 222 | ut_assert(ofnode_valid(node)); |
| 223 | ut_asserteq_str("sbe5", ofnode_get_name(node)); |
| 224 | |
| 225 | node = ofnode_get_aliases_node("unknown"); |
| 226 | ut_assert(!ofnode_valid(node)); |
| 227 | |
| 228 | val = ofnode_read_aliases_prop("spi0", &size); |
| 229 | ut_assertnonnull(val); |
| 230 | ut_asserteq(7, size); |
| 231 | ut_asserteq_str("/spi@0", (const char *)val); |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 236 | |
developer | cf8bc13 | 2020-05-02 11:35:10 +0200 | [diff] [blame] | 237 | static int dm_test_ofnode_get_child_count(struct unit_test_state *uts) |
| 238 | { |
| 239 | ofnode node, child_node; |
| 240 | u32 val; |
| 241 | |
| 242 | node = ofnode_path("/i-test"); |
| 243 | ut_assert(ofnode_valid(node)); |
| 244 | |
| 245 | val = ofnode_get_child_count(node); |
| 246 | ut_asserteq(3, val); |
| 247 | |
| 248 | child_node = ofnode_first_subnode(node); |
| 249 | ut_assert(ofnode_valid(child_node)); |
| 250 | val = ofnode_get_child_count(child_node); |
| 251 | ut_asserteq(0, val); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | DM_TEST(dm_test_ofnode_get_child_count, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 256 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 5de5b3b | 2020-11-28 17:50:02 -0700 | [diff] [blame] | 257 | |
| 258 | static int dm_test_ofnode_is_enabled(struct unit_test_state *uts) |
| 259 | { |
| 260 | ofnode root_node = ofnode_path("/"); |
| 261 | ofnode node = ofnode_path("/usb@0"); |
| 262 | |
| 263 | ut_assert(ofnode_is_enabled(root_node)); |
| 264 | ut_assert(!ofnode_is_enabled(node)); |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | 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] | 269 | |
| 270 | static int dm_test_ofnode_get_reg(struct unit_test_state *uts) |
| 271 | { |
| 272 | ofnode node; |
| 273 | fdt_addr_t addr; |
| 274 | fdt_size_t size; |
| 275 | |
| 276 | node = ofnode_path("/translation-test@8000"); |
| 277 | ut_assert(ofnode_valid(node)); |
| 278 | addr = ofnode_get_addr(node); |
| 279 | size = ofnode_get_size(node); |
| 280 | ut_asserteq(0x8000, addr); |
| 281 | ut_asserteq(0x4000, size); |
| 282 | |
| 283 | node = ofnode_path("/translation-test@8000/dev@1,100"); |
| 284 | ut_assert(ofnode_valid(node)); |
| 285 | addr = ofnode_get_addr(node); |
| 286 | size = ofnode_get_size(node); |
| 287 | ut_asserteq(0x9000, addr); |
| 288 | ut_asserteq(0x1000, size); |
| 289 | |
| 290 | node = ofnode_path("/emul-mux-controller"); |
| 291 | ut_assert(ofnode_valid(node)); |
| 292 | addr = ofnode_get_addr(node); |
| 293 | size = ofnode_get_size(node); |
Patrice Chotard | b4c5211 | 2022-01-04 08:42:48 +0100 | [diff] [blame] | 294 | ut_asserteq_64(FDT_ADDR_T_NONE, addr); |
Chen Guanqiao | be0512f | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 295 | ut_asserteq(FDT_SIZE_T_NONE, size); |
| 296 | |
Marek Behún | 177ab7f | 2021-05-26 14:08:17 +0200 | [diff] [blame] | 297 | node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42"); |
| 298 | ut_assert(ofnode_valid(node)); |
| 299 | addr = ofnode_get_addr_size_index_notrans(node, 0, &size); |
| 300 | ut_asserteq_64(0x42, addr); |
| 301 | |
Chen Guanqiao | be0512f | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | 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] | 305 | |
| 306 | static int dm_test_ofnode_get_path(struct unit_test_state *uts) |
| 307 | { |
| 308 | const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42"; |
| 309 | char buf[64]; |
| 310 | ofnode node; |
| 311 | int res; |
| 312 | |
| 313 | node = ofnode_path(path); |
| 314 | ut_assert(ofnode_valid(node)); |
| 315 | |
| 316 | res = ofnode_get_path(node, buf, 64); |
| 317 | ut_asserteq(0, res); |
| 318 | ut_asserteq_str(path, buf); |
| 319 | |
| 320 | res = ofnode_get_path(node, buf, 32); |
| 321 | ut_asserteq(-ENOSPC, res); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | 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] | 326 | |
| 327 | static int dm_test_ofnode_conf(struct unit_test_state *uts) |
| 328 | { |
| 329 | ut_assert(!ofnode_conf_read_bool("missing")); |
| 330 | ut_assert(ofnode_conf_read_bool("testing-bool")); |
| 331 | |
| 332 | ut_asserteq(123, ofnode_conf_read_int("testing-int", 0)); |
| 333 | ut_asserteq(6, ofnode_conf_read_int("missing", 6)); |
| 334 | |
| 335 | ut_assertnull(ofnode_conf_read_str("missing")); |
| 336 | ut_asserteq_str("testing", ofnode_conf_read_str("testing-str")); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | DM_TEST(dm_test_ofnode_conf, 0); |
Michael Walle | f1f8740 | 2021-10-15 15:15:18 +0200 | [diff] [blame] | 341 | |
| 342 | static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts) |
| 343 | { |
| 344 | const char compatible[] = "denx,u-boot-fdt-test"; |
| 345 | bool found = false; |
| 346 | ofnode node; |
| 347 | |
| 348 | ofnode_for_each_compatible_node(node, compatible) { |
| 349 | ut_assert(ofnode_device_is_compatible(node, compatible)); |
| 350 | found = true; |
| 351 | } |
| 352 | |
| 353 | /* There should be at least one matching node */ |
| 354 | ut_assert(found); |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | 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] | 359 | |
| 360 | static int dm_test_ofnode_string(struct unit_test_state *uts) |
| 361 | { |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 362 | const char **val; |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 363 | const char *out; |
| 364 | ofnode node; |
| 365 | |
| 366 | node = ofnode_path("/a-test"); |
| 367 | ut_assert(ofnode_valid(node)); |
| 368 | |
| 369 | /* single string */ |
| 370 | ut_asserteq(1, ofnode_read_string_count(node, "str-value")); |
| 371 | ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out)); |
| 372 | ut_asserteq_str("test string", out); |
| 373 | ut_asserteq(0, ofnode_stringlist_search(node, "str-value", |
| 374 | "test string")); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 375 | ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val)); |
| 376 | ut_asserteq_str("test string", val[0]); |
| 377 | ut_assertnull(val[1]); |
| 378 | free(val); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 379 | |
| 380 | /* list of strings */ |
| 381 | ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names")); |
| 382 | ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0, |
| 383 | &out)); |
| 384 | ut_asserteq_str("mux0", out); |
| 385 | ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names", |
| 386 | "mux0")); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 387 | ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names", |
| 388 | &val)); |
| 389 | ut_asserteq_str("mux0", val[0]); |
| 390 | ut_asserteq_str("mux1", val[1]); |
| 391 | ut_asserteq_str("mux2", val[2]); |
| 392 | ut_asserteq_str("mux3", val[3]); |
| 393 | ut_asserteq_str("mux4", val[4]); |
| 394 | ut_assertnull(val[5]); |
| 395 | free(val); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 396 | |
| 397 | ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4, |
| 398 | &out)); |
| 399 | ut_asserteq_str("mux4", out); |
| 400 | ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names", |
| 401 | "mux4")); |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | DM_TEST(dm_test_ofnode_string, 0); |
| 406 | |
| 407 | static int dm_test_ofnode_string_err(struct unit_test_state *uts) |
| 408 | { |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 409 | const char **val; |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 410 | const char *out; |
| 411 | ofnode node; |
| 412 | |
| 413 | /* |
| 414 | * Test error codes only on livetree, as they are different with |
| 415 | * flattree |
| 416 | */ |
| 417 | node = ofnode_path("/a-test"); |
| 418 | ut_assert(ofnode_valid(node)); |
| 419 | |
| 420 | /* non-existent property */ |
| 421 | ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing")); |
| 422 | ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0, |
| 423 | &out)); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 424 | ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val)); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 425 | |
| 426 | /* empty property */ |
| 427 | ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value")); |
| 428 | ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0, |
| 429 | &out)); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 430 | ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value", |
| 431 | &val)); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 432 | |
| 433 | /* badly formatted string list */ |
| 434 | ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value")); |
| 435 | ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0, |
| 436 | &out)); |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 437 | ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value", |
| 438 | &val)); |
Simon Glass | 7302539 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 439 | |
| 440 | /* out of range / not found */ |
| 441 | ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1, |
| 442 | &out)); |
| 443 | ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value", |
| 444 | "other")); |
| 445 | |
| 446 | /* negative value for index is not allowed, so don't test for that */ |
| 447 | |
| 448 | ut_asserteq(-ENODATA, ofnode_read_string_index(node, |
| 449 | "mux-control-names", 5, |
| 450 | &out)); |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE); |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 455 | |
| 456 | static int dm_test_ofnode_get_phy(struct unit_test_state *uts) |
| 457 | { |
| 458 | ofnode eth_node, phy_node; |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 459 | phy_interface_t mode; |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 460 | u32 reg; |
| 461 | |
| 462 | eth_node = ofnode_path("/phy-test-eth"); |
| 463 | ut_assert(ofnode_valid(eth_node)); |
| 464 | |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 465 | mode = ofnode_read_phy_mode(eth_node); |
| 466 | ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX); |
| 467 | |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 468 | phy_node = ofnode_get_phy_node(eth_node); |
| 469 | ut_assert(ofnode_valid(phy_node)); |
| 470 | |
| 471 | reg = ofnode_read_u32_default(phy_node, "reg", -1U); |
| 472 | ut_asserteq_64(0x1, reg); |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | DM_TEST(dm_test_ofnode_get_phy, 0); |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 477 | |
| 478 | /** |
| 479 | * make_ofnode_fdt() - Create an FDT for testing with ofnode |
| 480 | * |
| 481 | * The size is set to the minimum needed |
| 482 | * |
| 483 | * @uts: Test state |
| 484 | * @fdt: Place to write FDT |
| 485 | * @size: Maximum size of space for fdt |
| 486 | */ |
| 487 | static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size) |
| 488 | { |
| 489 | ut_assertok(fdt_create(fdt, size)); |
| 490 | ut_assertok(fdt_finish_reservemap(fdt)); |
| 491 | ut_assert(fdt_begin_node(fdt, "") >= 0); |
| 492 | |
| 493 | ut_assert(fdt_begin_node(fdt, "aliases") >= 0); |
| 494 | ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc")); |
| 495 | ut_assertok(fdt_end_node(fdt)); |
| 496 | |
| 497 | ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0); |
| 498 | ut_assertok(fdt_end_node(fdt)); |
| 499 | |
| 500 | ut_assertok(fdt_end_node(fdt)); |
| 501 | ut_assertok(fdt_finish(fdt)); |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int dm_test_ofnode_root(struct unit_test_state *uts) |
| 507 | { |
| 508 | struct device_node *root = NULL; |
| 509 | char fdt[256]; |
| 510 | oftree tree; |
| 511 | ofnode node; |
| 512 | |
| 513 | /* Check that aliases work on the control FDT */ |
| 514 | node = ofnode_get_aliases_node("ethernet3"); |
| 515 | ut_assert(ofnode_valid(node)); |
| 516 | ut_asserteq_str("sbe5", ofnode_get_name(node)); |
| 517 | |
| 518 | ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt))); |
| 519 | if (of_live_active()) { |
| 520 | ut_assertok(unflatten_device_tree(fdt, &root)); |
| 521 | tree.np = root; |
| 522 | } else { |
| 523 | tree.fdt = fdt; |
| 524 | } |
| 525 | |
| 526 | /* Make sure they don't work on this new tree */ |
| 527 | node = ofnode_path_root(tree, "mmc0"); |
| 528 | ut_assert(!ofnode_valid(node)); |
| 529 | |
| 530 | /* It should appear in the new tree */ |
| 531 | node = ofnode_path_root(tree, "/new-mmc"); |
| 532 | ut_assert(ofnode_valid(node)); |
| 533 | |
| 534 | /* ...and not in the control FDT */ |
| 535 | node = ofnode_path_root(oftree_default(), "/new-mmc"); |
| 536 | ut_assert(!ofnode_valid(node)); |
| 537 | |
| 538 | free(root); |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT); |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 543 | |
| 544 | static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts) |
| 545 | { |
| 546 | struct udevice *dev; |
| 547 | ofnode node; |
| 548 | |
Simon Glass | 7f9b580 | 2022-09-06 20:26:59 -0600 | [diff] [blame] | 549 | /* temporarily disable this test due to a failure fixed later */ |
| 550 | if (!of_live_active()) |
| 551 | return 0; |
| 552 | |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 553 | /* Test enabling devices */ |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 554 | node = ofnode_path("/usb@2"); |
| 555 | |
Simon Glass | 3ee3d15 | 2022-07-30 15:52:13 -0600 | [diff] [blame] | 556 | ut_assert(!ofnode_is_enabled(node)); |
| 557 | ut_assertok(ofnode_set_enabled(node, true)); |
| 558 | ut_asserteq(true, ofnode_is_enabled(node)); |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 559 | |
| 560 | device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node, |
| 561 | &dev); |
| 562 | ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev)); |
| 563 | |
| 564 | /* Test string property setting */ |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 565 | ut_assert(device_is_compatible(dev, "sandbox,usb")); |
| 566 | ofnode_write_string(node, "compatible", "gdsys,super-usb"); |
| 567 | ut_assert(device_is_compatible(dev, "gdsys,super-usb")); |
| 568 | ofnode_write_string(node, "compatible", "sandbox,usb"); |
| 569 | ut_assert(device_is_compatible(dev, "sandbox,usb")); |
| 570 | |
| 571 | /* Test setting generic properties */ |
| 572 | |
| 573 | /* Non-existent in DTB */ |
| 574 | ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev)); |
| 575 | /* reg = 0x42, size = 0x100 */ |
Simon Glass | 5e2cd5e | 2022-07-30 15:52:10 -0600 | [diff] [blame] | 576 | ut_assertok(ofnode_write_prop(node, "reg", |
| 577 | "\x00\x00\x00\x42\x00\x00\x01\x00", 8)); |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 578 | ut_asserteq(0x42, dev_read_addr(dev)); |
| 579 | |
| 580 | /* Test disabling devices */ |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 581 | device_remove(dev, DM_REMOVE_NORMAL); |
| 582 | device_unbind(dev); |
| 583 | |
Simon Glass | 3ee3d15 | 2022-07-30 15:52:13 -0600 | [diff] [blame] | 584 | ut_assert(ofnode_is_enabled(node)); |
| 585 | ut_assertok(ofnode_set_enabled(node, false)); |
| 586 | ut_assert(!ofnode_is_enabled(node)); |
Simon Glass | 270a443 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 587 | |
| 588 | return 0; |
| 589 | } |
Simon Glass | b75dc16 | 2022-07-30 15:52:11 -0600 | [diff] [blame] | 590 | DM_TEST(dm_test_ofnode_livetree_writing, |
Simon Glass | 3ee3d15 | 2022-07-30 15:52:13 -0600 | [diff] [blame] | 591 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_OR_FLAT); |
Simon Glass | d28e31e | 2022-07-30 15:52:14 -0600 | [diff] [blame] | 592 | |
| 593 | static int dm_test_ofnode_u32(struct unit_test_state *uts) |
| 594 | { |
| 595 | ofnode node; |
| 596 | |
| 597 | node = ofnode_path("/lcd"); |
| 598 | ut_assert(ofnode_valid(node)); |
| 599 | ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123)); |
| 600 | ut_assertok(ofnode_write_u32(node, "xres", 1367)); |
| 601 | ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123)); |
| 602 | ut_assertok(ofnode_write_u32(node, "xres", 1366)); |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | DM_TEST(dm_test_ofnode_u32, |
| 607 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_OR_FLAT); |
Simon Glass | 56bc332 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 608 | |
| 609 | static int dm_test_ofnode_add_subnode(struct unit_test_state *uts) |
| 610 | { |
| 611 | ofnode node, check, subnode; |
| 612 | char buf[128]; |
| 613 | |
| 614 | /* temporarily disable this test due to a failure fixed later */ |
| 615 | if (!of_live_active()) |
| 616 | return 0; |
| 617 | |
| 618 | node = ofnode_path("/lcd"); |
| 619 | ut_assert(ofnode_valid(node)); |
| 620 | ut_assertok(ofnode_add_subnode(node, "edmund", &subnode)); |
| 621 | check = ofnode_path("/lcd/edmund"); |
| 622 | ut_asserteq(subnode.of_offset, check.of_offset); |
| 623 | ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf))); |
| 624 | ut_asserteq_str("/lcd/edmund", buf); |
| 625 | |
| 626 | if (of_live_active()) { |
| 627 | struct device_node *child; |
| 628 | |
| 629 | ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund", |
| 630 | 2, &child)); |
| 631 | ut_asserteq_str("ed", child->name); |
| 632 | ut_asserteq_str("/lcd/ed", child->full_name); |
| 633 | check = ofnode_path("/lcd/ed"); |
| 634 | ut_asserteq_ptr(child, check.np); |
| 635 | ut_assertok(ofnode_get_path(np_to_ofnode(child), buf, |
| 636 | sizeof(buf))); |
| 637 | ut_asserteq_str("/lcd/ed", buf); |
| 638 | } |
| 639 | |
| 640 | /* An existing node should be returned with -EEXIST */ |
| 641 | ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check)); |
| 642 | ut_asserteq(subnode.of_offset, check.of_offset); |
| 643 | |
| 644 | /* add a root node */ |
| 645 | node = ofnode_path("/"); |
| 646 | ut_assert(ofnode_valid(node)); |
| 647 | ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode)); |
| 648 | check = ofnode_path("/lcd2"); |
| 649 | ut_asserteq(subnode.of_offset, check.of_offset); |
| 650 | ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf))); |
| 651 | ut_asserteq_str("/lcd2", buf); |
| 652 | |
| 653 | if (of_live_active()) { |
| 654 | ulong start; |
| 655 | int i; |
| 656 | |
| 657 | /* |
| 658 | * Make sure each of the three malloc()checks in |
| 659 | * of_add_subnode() work |
| 660 | */ |
| 661 | for (i = 0; i < 3; i++) { |
| 662 | malloc_enable_testing(i); |
| 663 | start = ut_check_free(); |
| 664 | ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony", |
| 665 | &check)); |
| 666 | ut_assertok(ut_check_delta(start)); |
| 667 | } |
| 668 | |
| 669 | /* This should pass since we allow 3 allocations */ |
| 670 | malloc_enable_testing(3); |
| 671 | ut_assertok(ofnode_add_subnode(node, "anthony", &check)); |
| 672 | malloc_disable_testing(); |
| 673 | } |
| 674 | |
Simon Glass | 238afb5 | 2022-09-06 20:27:03 -0600 | [diff] [blame^] | 675 | /* write to the empty node */ |
| 676 | ut_assertok(ofnode_write_string(subnode, "example", "text")); |
| 677 | |
Simon Glass | 56bc332 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | DM_TEST(dm_test_ofnode_add_subnode, |
| 681 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_OR_FLAT); |