Masahiro Yamada | 04ca871 | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 5e96925 | 2022-09-06 20:27:27 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2022 Google LLC |
| 4 | * |
| 5 | * There are two types of tests in this file: |
| 6 | * - normal ones which act on the control FDT (gd->fdt_blob or gd->of_root) |
| 7 | * - 'other' ones which act on the 'other' FDT (other.dts) |
| 8 | * |
| 9 | * The 'other' ones have an _ot suffix. |
| 10 | * |
| 11 | * The latter are used to check behaviour with multiple device trees, |
| 12 | * particularly with flat tree, where a tree ID is included in ofnode as part of |
| 13 | * the node offset. These tests are typically just for making sure that the |
| 14 | * offset makes it to libfdt correctly and that the resulting return value is |
| 15 | * correctly turned into an ofnode. The 'other' tests do not fully check the |
| 16 | * behaviour of each ofnode function, since that is done by the normal ones. |
| 17 | */ |
Masahiro Yamada | 04ca871 | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 18 | |
| 19 | #include <common.h> |
| 20 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 21 | #include <log.h> |
Simon Glass | 3310484 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 22 | #include <of_live.h> |
Simon Glass | 6571559 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 23 | #include <dm/device-internal.h> |
| 24 | #include <dm/lists.h> |
Simon Glass | e6c5c94 | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 25 | #include <dm/of_extra.h> |
Simon Glass | 6571559 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 26 | #include <dm/root.h> |
Masahiro Yamada | 04ca871 | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 27 | #include <dm/test.h> |
Simon Glass | 6571559 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 28 | #include <dm/uclass-internal.h> |
Simon Glass | 0e1fad4 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 29 | #include <test/test.h> |
Masahiro Yamada | 04ca871 | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 30 | #include <test/ut.h> |
| 31 | |
Simon Glass | 5e96925 | 2022-09-06 20:27:27 -0600 | [diff] [blame] | 32 | /** |
| 33 | * get_other_oftree() - Convert a flat tree into an oftree object |
| 34 | * |
| 35 | * @uts: Test state |
| 36 | * @return: oftree object for the 'other' FDT (see sandbox' other.dts) |
| 37 | */ |
| 38 | oftree get_other_oftree(struct unit_test_state *uts) |
| 39 | { |
| 40 | oftree tree; |
| 41 | |
| 42 | if (of_live_active()) |
| 43 | tree = oftree_from_np(uts->of_other); |
| 44 | else |
| 45 | tree = oftree_from_fdt(uts->other_fdt); |
| 46 | |
| 47 | /* An invalid tree may cause failure or crashes */ |
| 48 | if (!oftree_valid(tree)) |
| 49 | ut_reportf("test needs the UT_TESTF_OTHER_FDT flag"); |
| 50 | |
| 51 | return tree; |
| 52 | } |
| 53 | |
Simon Glass | 88a1ae8 | 2022-09-06 20:27:29 -0600 | [diff] [blame] | 54 | /** |
| 55 | * get_oftree() - Convert a flat tree into an oftree object |
| 56 | * |
| 57 | * @uts: Test state |
| 58 | * @fdt: Pointer to flat tree |
| 59 | * @treep: Returns the tree, on success |
| 60 | * Return: 0 if OK, 1 if the tree failed to unflatten, -EOVERFLOW if there are |
| 61 | * too many flat trees to allow another one to be registers (see |
| 62 | * oftree_ensure()) |
| 63 | */ |
| 64 | int get_oftree(struct unit_test_state *uts, void *fdt, oftree *treep) |
| 65 | { |
| 66 | oftree tree; |
| 67 | |
| 68 | if (of_live_active()) { |
| 69 | struct device_node *root; |
| 70 | |
| 71 | ut_assertok(unflatten_device_tree(fdt, &root)); |
| 72 | tree = oftree_from_np(root); |
| 73 | } else { |
| 74 | tree = oftree_from_fdt(fdt); |
| 75 | if (!oftree_valid(tree)) |
| 76 | return -EOVERFLOW; |
| 77 | } |
| 78 | *treep = tree; |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * free_oftree() - Free memory used by get_oftree() |
| 85 | * |
| 86 | * @tree: Tree to free |
| 87 | */ |
| 88 | void free_oftree(oftree tree) |
| 89 | { |
| 90 | if (of_live_active()) |
| 91 | free(tree.np); |
| 92 | } |
| 93 | |
Masahiro Yamada | 04ca871 | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 94 | static int dm_test_ofnode_compatible(struct unit_test_state *uts) |
| 95 | { |
| 96 | ofnode root_node = ofnode_path("/"); |
| 97 | |
| 98 | ut_assert(ofnode_valid(root_node)); |
| 99 | ut_assert(ofnode_device_is_compatible(root_node, "sandbox")); |
| 100 | |
| 101 | return 0; |
| 102 | } |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 103 | DM_TEST(dm_test_ofnode_compatible, |
| 104 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 105 | |
| 106 | /* check ofnode_device_is_compatible() with the 'other' FDT */ |
| 107 | static int dm_test_ofnode_compatible_ot(struct unit_test_state *uts) |
| 108 | { |
| 109 | oftree otree = get_other_oftree(uts); |
| 110 | ofnode oroot = oftree_root(otree); |
| 111 | |
| 112 | ut_assert(ofnode_valid(oroot)); |
| 113 | ut_assert(ofnode_device_is_compatible(oroot, "sandbox-other")); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | DM_TEST(dm_test_ofnode_compatible_ot, UT_TESTF_OTHER_FDT); |
Jens Wiklander | 9bc7e96 | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 118 | |
Patrick Delaunay | 6d9949f | 2020-09-24 17:26:20 +0200 | [diff] [blame] | 119 | static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts) |
| 120 | { |
| 121 | /* test invalid phandle */ |
| 122 | ut_assert(!ofnode_valid(ofnode_get_by_phandle(0))); |
| 123 | ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1))); |
| 124 | |
| 125 | /* test first valid phandle */ |
| 126 | ut_assert(ofnode_valid(ofnode_get_by_phandle(1))); |
| 127 | |
| 128 | /* test unknown phandle */ |
| 129 | ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000))); |
| 130 | |
Simon Glass | 928d267 | 2022-09-06 20:27:22 -0600 | [diff] [blame] | 131 | ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1))); |
| 132 | |
Patrick Delaunay | 6d9949f | 2020-09-24 17:26:20 +0200 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 136 | |
Simon Glass | 5e96925 | 2022-09-06 20:27:27 -0600 | [diff] [blame] | 137 | static int dm_test_ofnode_get_by_phandle_ot(struct unit_test_state *uts) |
| 138 | { |
| 139 | oftree otree = get_other_oftree(uts); |
| 140 | ofnode node; |
| 141 | |
| 142 | ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1))); |
| 143 | node = oftree_get_by_phandle(otree, 1); |
| 144 | ut_assert(ofnode_valid(node)); |
| 145 | ut_asserteq_str("target", ofnode_get_name(node)); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | DM_TEST(dm_test_ofnode_get_by_phandle_ot, UT_TESTF_OTHER_FDT); |
| 150 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 151 | static int check_prop_values(struct unit_test_state *uts, ofnode start, |
| 152 | const char *propname, const char *propval, |
| 153 | int expect_count) |
Jens Wiklander | 9bc7e96 | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 154 | { |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 155 | int proplen = strlen(propval) + 1; |
Jens Wiklander | 9bc7e96 | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 156 | const char *str; |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 157 | ofnode node; |
| 158 | int count; |
Jens Wiklander | 9bc7e96 | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 159 | |
| 160 | /* Find first matching node, there should be at least one */ |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 161 | node = ofnode_by_prop_value(start, propname, propval, proplen); |
Jens Wiklander | 9bc7e96 | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 162 | ut_assert(ofnode_valid(node)); |
| 163 | str = ofnode_read_string(node, propname); |
| 164 | ut_assert(str && !strcmp(str, propval)); |
| 165 | |
| 166 | /* Find the rest of the matching nodes */ |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 167 | count = 1; |
Jens Wiklander | 9bc7e96 | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 168 | while (true) { |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 169 | node = ofnode_by_prop_value(node, propname, propval, proplen); |
Jens Wiklander | 9bc7e96 | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 170 | if (!ofnode_valid(node)) |
| 171 | break; |
| 172 | str = ofnode_read_string(node, propname); |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 173 | ut_asserteq_str(propval, str); |
| 174 | count++; |
Jens Wiklander | 9bc7e96 | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 175 | } |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 176 | ut_asserteq(expect_count, count); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts) |
| 182 | { |
| 183 | ut_assertok(check_prop_values(uts, ofnode_null(), "compatible", |
| 184 | "denx,u-boot-fdt-test", 11)); |
Jens Wiklander | 9bc7e96 | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 188 | DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT); |
Simon Glass | e6c5c94 | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 189 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 190 | static int dm_test_ofnode_by_prop_value_ot(struct unit_test_state *uts) |
| 191 | { |
| 192 | oftree otree = get_other_oftree(uts); |
| 193 | |
| 194 | ut_assertok(check_prop_values(uts, oftree_root(otree), "str-prop", |
| 195 | "other", 2)); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | DM_TEST(dm_test_ofnode_by_prop_value_ot, UT_TESTF_OTHER_FDT); |
| 200 | |
Simon Glass | e6c5c94 | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 201 | static int dm_test_ofnode_fmap(struct unit_test_state *uts) |
| 202 | { |
| 203 | struct fmap_entry entry; |
| 204 | ofnode node; |
| 205 | |
| 206 | node = ofnode_path("/cros-ec/flash"); |
| 207 | ut_assert(ofnode_valid(node)); |
| 208 | ut_assertok(ofnode_read_fmap_entry(node, &entry)); |
| 209 | ut_asserteq(0x08000000, entry.offset); |
| 210 | ut_asserteq(0x20000, entry.length); |
| 211 | |
| 212 | return 0; |
| 213 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 214 | 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] | 215 | |
Simon Glass | a8167d8 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 216 | static int dm_test_ofnode_read(struct unit_test_state *uts) |
| 217 | { |
| 218 | const u32 *val; |
| 219 | ofnode node; |
| 220 | int size; |
| 221 | |
| 222 | node = ofnode_path("/a-test"); |
| 223 | ut_assert(ofnode_valid(node)); |
| 224 | |
| 225 | val = ofnode_read_prop(node, "int-value", &size); |
| 226 | ut_assertnonnull(val); |
| 227 | ut_asserteq(4, size); |
| 228 | ut_asserteq(1234, fdt32_to_cpu(val[0])); |
| 229 | |
| 230 | val = ofnode_read_prop(node, "missing", &size); |
| 231 | ut_assertnull(val); |
| 232 | ut_asserteq(-FDT_ERR_NOTFOUND, size); |
| 233 | |
| 234 | /* Check it works without a size parameter */ |
| 235 | val = ofnode_read_prop(node, "missing", NULL); |
| 236 | ut_assertnull(val); |
| 237 | |
| 238 | return 0; |
| 239 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 240 | 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] | 241 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 242 | static int dm_test_ofnode_read_ot(struct unit_test_state *uts) |
| 243 | { |
| 244 | oftree otree = get_other_oftree(uts); |
| 245 | const char *val; |
| 246 | ofnode node; |
| 247 | int size; |
| 248 | |
| 249 | node = oftree_path(otree, "/node/subnode"); |
| 250 | ut_assert(ofnode_valid(node)); |
| 251 | |
| 252 | val = ofnode_read_prop(node, "str-prop", &size); |
| 253 | ut_assertnonnull(val); |
| 254 | ut_asserteq_str("other", val); |
| 255 | ut_asserteq(6, size); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | DM_TEST(dm_test_ofnode_read_ot, UT_TESTF_OTHER_FDT); |
| 260 | |
Patrick Delaunay | cc72f3e | 2020-09-25 09:41:16 +0200 | [diff] [blame] | 261 | static int dm_test_ofnode_phandle(struct unit_test_state *uts) |
| 262 | { |
| 263 | struct ofnode_phandle_args args; |
| 264 | ofnode node; |
| 265 | int ret; |
| 266 | const char prop[] = "test-gpios"; |
| 267 | const char cell[] = "#gpio-cells"; |
| 268 | const char prop2[] = "phandle-value"; |
| 269 | |
| 270 | node = ofnode_path("/a-test"); |
| 271 | ut_assert(ofnode_valid(node)); |
| 272 | |
| 273 | /* Test ofnode_count_phandle_with_args with cell name */ |
| 274 | ret = ofnode_count_phandle_with_args(node, "missing", cell, 0); |
| 275 | ut_asserteq(-ENOENT, ret); |
| 276 | ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0); |
| 277 | ut_asserteq(-EINVAL, ret); |
| 278 | ret = ofnode_count_phandle_with_args(node, prop, cell, 0); |
| 279 | ut_asserteq(5, ret); |
| 280 | |
| 281 | /* Test ofnode_parse_phandle_with_args with cell name */ |
| 282 | ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0, |
| 283 | &args); |
| 284 | ut_asserteq(-ENOENT, ret); |
| 285 | ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0, |
| 286 | &args); |
| 287 | ut_asserteq(-EINVAL, ret); |
| 288 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args); |
| 289 | ut_assertok(ret); |
| 290 | ut_asserteq(1, args.args_count); |
| 291 | ut_asserteq(1, args.args[0]); |
| 292 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args); |
| 293 | ut_assertok(ret); |
| 294 | ut_asserteq(1, args.args_count); |
| 295 | ut_asserteq(4, args.args[0]); |
| 296 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args); |
| 297 | ut_assertok(ret); |
| 298 | ut_asserteq(5, args.args_count); |
| 299 | ut_asserteq(5, args.args[0]); |
| 300 | ut_asserteq(1, args.args[4]); |
| 301 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args); |
| 302 | ut_asserteq(-ENOENT, ret); |
| 303 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args); |
| 304 | ut_assertok(ret); |
| 305 | ut_asserteq(1, args.args_count); |
| 306 | ut_asserteq(12, args.args[0]); |
| 307 | ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args); |
| 308 | ut_asserteq(-ENOENT, ret); |
| 309 | |
| 310 | /* Test ofnode_count_phandle_with_args with cell count */ |
| 311 | ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2); |
| 312 | ut_asserteq(-ENOENT, ret); |
| 313 | ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1); |
| 314 | ut_asserteq(3, ret); |
| 315 | |
| 316 | /* Test ofnode_parse_phandle_with_args with cell count */ |
| 317 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args); |
| 318 | ut_assertok(ret); |
| 319 | ut_asserteq(1, ofnode_valid(args.node)); |
| 320 | ut_asserteq(1, args.args_count); |
| 321 | ut_asserteq(10, args.args[0]); |
| 322 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args); |
| 323 | ut_asserteq(-EINVAL, ret); |
| 324 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args); |
| 325 | ut_assertok(ret); |
| 326 | ut_asserteq(1, ofnode_valid(args.node)); |
| 327 | ut_asserteq(1, args.args_count); |
| 328 | ut_asserteq(30, args.args[0]); |
| 329 | ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args); |
| 330 | ut_asserteq(-ENOENT, ret); |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 335 | |
Simon Glass | 5e96925 | 2022-09-06 20:27:27 -0600 | [diff] [blame] | 336 | static int dm_test_ofnode_phandle_ot(struct unit_test_state *uts) |
| 337 | { |
| 338 | oftree otree = get_other_oftree(uts); |
| 339 | struct ofnode_phandle_args args; |
| 340 | ofnode node; |
| 341 | int ret; |
| 342 | |
| 343 | node = oftree_path(otree, "/node"); |
| 344 | |
| 345 | /* Test ofnode_count_phandle_with_args with cell name */ |
| 346 | ret = ofnode_count_phandle_with_args(node, "missing", "#gpio-cells", 0); |
| 347 | ut_asserteq(-ENOENT, ret); |
| 348 | ret = ofnode_count_phandle_with_args(node, "target", "#invalid", 0); |
| 349 | ut_asserteq(-EINVAL, ret); |
| 350 | ret = ofnode_count_phandle_with_args(node, "target", "#gpio-cells", 0); |
| 351 | ut_asserteq(1, ret); |
| 352 | |
| 353 | ret = ofnode_parse_phandle_with_args(node, "target", "#gpio-cells", 0, |
| 354 | 0, &args); |
| 355 | ut_assertok(ret); |
| 356 | ut_asserteq(2, args.args_count); |
| 357 | ut_asserteq(3, args.args[0]); |
| 358 | ut_asserteq(4, args.args[1]); |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT); |
| 363 | |
Simon Glass | 14ca9f7 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 364 | static int dm_test_ofnode_read_chosen(struct unit_test_state *uts) |
| 365 | { |
| 366 | const char *str; |
Simon Glass | bd933bf | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 367 | const u32 *val; |
Simon Glass | 14ca9f7 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 368 | ofnode node; |
Simon Glass | bd933bf | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 369 | int size; |
Simon Glass | 14ca9f7 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 370 | |
| 371 | str = ofnode_read_chosen_string("setting"); |
| 372 | ut_assertnonnull(str); |
| 373 | ut_asserteq_str("sunrise ohoka", str); |
| 374 | ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting")); |
| 375 | |
| 376 | node = ofnode_get_chosen_node("other-node"); |
| 377 | ut_assert(ofnode_valid(node)); |
| 378 | ut_asserteq_str("c-test@5", ofnode_get_name(node)); |
| 379 | |
| 380 | node = ofnode_get_chosen_node("setting"); |
| 381 | ut_assert(!ofnode_valid(node)); |
| 382 | |
Simon Glass | bd933bf | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 383 | val = ofnode_read_chosen_prop("int-values", &size); |
| 384 | ut_assertnonnull(val); |
| 385 | ut_asserteq(8, size); |
| 386 | ut_asserteq(0x1937, fdt32_to_cpu(val[0])); |
| 387 | ut_asserteq(72993, fdt32_to_cpu(val[1])); |
| 388 | |
Simon Glass | 14ca9f7 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 389 | return 0; |
| 390 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 391 | 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] | 392 | |
Michal Simek | 305d318 | 2020-07-28 12:51:08 +0200 | [diff] [blame] | 393 | static int dm_test_ofnode_read_aliases(struct unit_test_state *uts) |
| 394 | { |
| 395 | const void *val; |
| 396 | ofnode node; |
| 397 | int size; |
| 398 | |
Michael Walle | 82a3c9e | 2021-02-25 16:51:11 +0100 | [diff] [blame] | 399 | node = ofnode_get_aliases_node("ethernet3"); |
Michal Simek | 305d318 | 2020-07-28 12:51:08 +0200 | [diff] [blame] | 400 | ut_assert(ofnode_valid(node)); |
| 401 | ut_asserteq_str("sbe5", ofnode_get_name(node)); |
| 402 | |
| 403 | node = ofnode_get_aliases_node("unknown"); |
| 404 | ut_assert(!ofnode_valid(node)); |
| 405 | |
| 406 | val = ofnode_read_aliases_prop("spi0", &size); |
| 407 | ut_assertnonnull(val); |
| 408 | ut_asserteq(7, size); |
| 409 | ut_asserteq_str("/spi@0", (const char *)val); |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 414 | |
Chunfeng Yun | bf6ad91 | 2020-05-02 11:35:10 +0200 | [diff] [blame] | 415 | static int dm_test_ofnode_get_child_count(struct unit_test_state *uts) |
| 416 | { |
| 417 | ofnode node, child_node; |
| 418 | u32 val; |
| 419 | |
| 420 | node = ofnode_path("/i-test"); |
| 421 | ut_assert(ofnode_valid(node)); |
| 422 | |
| 423 | val = ofnode_get_child_count(node); |
| 424 | ut_asserteq(3, val); |
| 425 | |
| 426 | child_node = ofnode_first_subnode(node); |
| 427 | ut_assert(ofnode_valid(child_node)); |
| 428 | val = ofnode_get_child_count(child_node); |
| 429 | ut_asserteq(0, val); |
| 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | DM_TEST(dm_test_ofnode_get_child_count, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 434 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 0de1b07 | 2020-11-28 17:50:02 -0700 | [diff] [blame] | 435 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 436 | static int dm_test_ofnode_get_child_count_ot(struct unit_test_state *uts) |
| 437 | { |
| 438 | oftree otree = get_other_oftree(uts); |
| 439 | ofnode node, child_node; |
| 440 | u32 val; |
| 441 | |
| 442 | node = oftree_path(otree, "/node"); |
| 443 | ut_assert(ofnode_valid(node)); |
| 444 | |
| 445 | val = ofnode_get_child_count(node); |
| 446 | ut_asserteq(2, val); |
| 447 | |
| 448 | child_node = ofnode_first_subnode(node); |
| 449 | ut_assert(ofnode_valid(child_node)); |
| 450 | val = ofnode_get_child_count(child_node); |
| 451 | ut_asserteq(0, val); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | DM_TEST(dm_test_ofnode_get_child_count_ot, UT_TESTF_OTHER_FDT); |
| 456 | |
Simon Glass | 0de1b07 | 2020-11-28 17:50:02 -0700 | [diff] [blame] | 457 | static int dm_test_ofnode_is_enabled(struct unit_test_state *uts) |
| 458 | { |
| 459 | ofnode root_node = ofnode_path("/"); |
| 460 | ofnode node = ofnode_path("/usb@0"); |
| 461 | |
| 462 | ut_assert(ofnode_is_enabled(root_node)); |
| 463 | ut_assert(!ofnode_is_enabled(node)); |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Chen Guanqiao | 61772bc | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 468 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 469 | static int dm_test_ofnode_is_enabled_ot(struct unit_test_state *uts) |
| 470 | { |
| 471 | oftree otree = get_other_oftree(uts); |
| 472 | ofnode root_node = oftree_root(otree); |
| 473 | ofnode node = oftree_path(otree, "/target"); |
| 474 | |
| 475 | ut_assert(ofnode_is_enabled(root_node)); |
| 476 | ut_assert(!ofnode_is_enabled(node)); |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | DM_TEST(dm_test_ofnode_is_enabled_ot, UT_TESTF_OTHER_FDT); |
| 481 | |
Chen Guanqiao | 61772bc | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 482 | static int dm_test_ofnode_get_reg(struct unit_test_state *uts) |
| 483 | { |
| 484 | ofnode node; |
| 485 | fdt_addr_t addr; |
| 486 | fdt_size_t size; |
| 487 | |
| 488 | node = ofnode_path("/translation-test@8000"); |
| 489 | ut_assert(ofnode_valid(node)); |
| 490 | addr = ofnode_get_addr(node); |
| 491 | size = ofnode_get_size(node); |
| 492 | ut_asserteq(0x8000, addr); |
| 493 | ut_asserteq(0x4000, size); |
| 494 | |
| 495 | node = ofnode_path("/translation-test@8000/dev@1,100"); |
| 496 | ut_assert(ofnode_valid(node)); |
| 497 | addr = ofnode_get_addr(node); |
| 498 | size = ofnode_get_size(node); |
| 499 | ut_asserteq(0x9000, addr); |
| 500 | ut_asserteq(0x1000, size); |
| 501 | |
| 502 | node = ofnode_path("/emul-mux-controller"); |
| 503 | ut_assert(ofnode_valid(node)); |
| 504 | addr = ofnode_get_addr(node); |
| 505 | size = ofnode_get_size(node); |
Patrice Chotard | 9876ae7 | 2022-01-04 08:42:48 +0100 | [diff] [blame] | 506 | ut_asserteq_64(FDT_ADDR_T_NONE, addr); |
Chen Guanqiao | 61772bc | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 507 | ut_asserteq(FDT_SIZE_T_NONE, size); |
| 508 | |
Marek Behún | 31a7b71 | 2021-05-26 14:08:17 +0200 | [diff] [blame] | 509 | node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42"); |
| 510 | ut_assert(ofnode_valid(node)); |
| 511 | addr = ofnode_get_addr_size_index_notrans(node, 0, &size); |
| 512 | ut_asserteq_64(0x42, addr); |
| 513 | |
Chen Guanqiao | 61772bc | 2021-04-12 14:51:12 +0800 | [diff] [blame] | 514 | return 0; |
| 515 | } |
| 516 | DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Marek Behún | 0e116be | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 517 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 518 | static int dm_test_ofnode_get_reg_ot(struct unit_test_state *uts) |
| 519 | { |
| 520 | oftree otree = get_other_oftree(uts); |
| 521 | ofnode node = oftree_path(otree, "/target"); |
| 522 | fdt_addr_t addr; |
| 523 | |
| 524 | addr = ofnode_get_addr(node); |
| 525 | ut_asserteq(0x8000, addr); |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | DM_TEST(dm_test_ofnode_get_reg_ot, UT_TESTF_OTHER_FDT); |
| 530 | |
Marek Behún | 0e116be | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 531 | static int dm_test_ofnode_get_path(struct unit_test_state *uts) |
| 532 | { |
| 533 | const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42"; |
| 534 | char buf[64]; |
| 535 | ofnode node; |
Marek Behún | 0e116be | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 536 | |
| 537 | node = ofnode_path(path); |
| 538 | ut_assert(ofnode_valid(node)); |
| 539 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 540 | ut_assertok(ofnode_get_path(node, buf, sizeof(buf))); |
Marek Behún | 0e116be | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 541 | ut_asserteq_str(path, buf); |
| 542 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 543 | ut_asserteq(-ENOSPC, ofnode_get_path(node, buf, 32)); |
Marek Behún | 0e116be | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 544 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 545 | ut_assertok(ofnode_get_path(ofnode_root(), buf, 32)); |
Simon Glass | 66d0d0c | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 546 | ut_asserteq_str("/", buf); |
| 547 | |
Marek Behún | 0e116be | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 7de8bd0 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 551 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 552 | static int dm_test_ofnode_get_path_ot(struct unit_test_state *uts) |
| 553 | { |
| 554 | oftree otree = get_other_oftree(uts); |
| 555 | const char *path = "/node/subnode"; |
| 556 | ofnode node = oftree_path(otree, path); |
| 557 | char buf[64]; |
| 558 | |
| 559 | ut_assert(ofnode_valid(node)); |
| 560 | |
| 561 | ut_assertok(ofnode_get_path(node, buf, sizeof(buf))); |
| 562 | ut_asserteq_str(path, buf); |
| 563 | |
| 564 | ut_assertok(ofnode_get_path(oftree_root(otree), buf, 32)); |
| 565 | ut_asserteq_str("/", buf); |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | DM_TEST(dm_test_ofnode_get_path_ot, UT_TESTF_OTHER_FDT); |
| 570 | |
Simon Glass | 7de8bd0 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 571 | static int dm_test_ofnode_conf(struct unit_test_state *uts) |
| 572 | { |
| 573 | ut_assert(!ofnode_conf_read_bool("missing")); |
| 574 | ut_assert(ofnode_conf_read_bool("testing-bool")); |
| 575 | |
| 576 | ut_asserteq(123, ofnode_conf_read_int("testing-int", 0)); |
| 577 | ut_asserteq(6, ofnode_conf_read_int("missing", 6)); |
| 578 | |
| 579 | ut_assertnull(ofnode_conf_read_str("missing")); |
| 580 | ut_asserteq_str("testing", ofnode_conf_read_str("testing-str")); |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | DM_TEST(dm_test_ofnode_conf, 0); |
Michael Walle | bce039a | 2021-10-15 15:15:18 +0200 | [diff] [blame] | 585 | |
| 586 | static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts) |
| 587 | { |
| 588 | const char compatible[] = "denx,u-boot-fdt-test"; |
| 589 | bool found = false; |
| 590 | ofnode node; |
| 591 | |
| 592 | ofnode_for_each_compatible_node(node, compatible) { |
| 593 | ut_assert(ofnode_device_is_compatible(node, compatible)); |
| 594 | found = true; |
| 595 | } |
| 596 | |
| 597 | /* There should be at least one matching node */ |
| 598 | ut_assert(found); |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT); |
Simon Glass | fb933d0 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 603 | |
| 604 | static int dm_test_ofnode_string(struct unit_test_state *uts) |
| 605 | { |
Simon Glass | 075bfc9 | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 606 | const char **val; |
Simon Glass | fb933d0 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 607 | const char *out; |
| 608 | ofnode node; |
| 609 | |
| 610 | node = ofnode_path("/a-test"); |
| 611 | ut_assert(ofnode_valid(node)); |
| 612 | |
| 613 | /* single string */ |
| 614 | ut_asserteq(1, ofnode_read_string_count(node, "str-value")); |
| 615 | ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out)); |
| 616 | ut_asserteq_str("test string", out); |
| 617 | ut_asserteq(0, ofnode_stringlist_search(node, "str-value", |
| 618 | "test string")); |
Simon Glass | 075bfc9 | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 619 | ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val)); |
| 620 | ut_asserteq_str("test string", val[0]); |
| 621 | ut_assertnull(val[1]); |
| 622 | free(val); |
Simon Glass | fb933d0 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 623 | |
| 624 | /* list of strings */ |
| 625 | ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names")); |
| 626 | ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0, |
| 627 | &out)); |
| 628 | ut_asserteq_str("mux0", out); |
| 629 | ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names", |
| 630 | "mux0")); |
Simon Glass | 075bfc9 | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 631 | ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names", |
| 632 | &val)); |
| 633 | ut_asserteq_str("mux0", val[0]); |
| 634 | ut_asserteq_str("mux1", val[1]); |
| 635 | ut_asserteq_str("mux2", val[2]); |
| 636 | ut_asserteq_str("mux3", val[3]); |
| 637 | ut_asserteq_str("mux4", val[4]); |
| 638 | ut_assertnull(val[5]); |
| 639 | free(val); |
Simon Glass | fb933d0 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 640 | |
| 641 | ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4, |
| 642 | &out)); |
| 643 | ut_asserteq_str("mux4", out); |
| 644 | ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names", |
| 645 | "mux4")); |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | DM_TEST(dm_test_ofnode_string, 0); |
| 650 | |
| 651 | static int dm_test_ofnode_string_err(struct unit_test_state *uts) |
| 652 | { |
Simon Glass | 075bfc9 | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 653 | const char **val; |
Simon Glass | fb933d0 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 654 | const char *out; |
| 655 | ofnode node; |
| 656 | |
| 657 | /* |
| 658 | * Test error codes only on livetree, as they are different with |
| 659 | * flattree |
| 660 | */ |
| 661 | node = ofnode_path("/a-test"); |
| 662 | ut_assert(ofnode_valid(node)); |
| 663 | |
| 664 | /* non-existent property */ |
| 665 | ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing")); |
| 666 | ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0, |
| 667 | &out)); |
Simon Glass | 075bfc9 | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 668 | ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val)); |
Simon Glass | fb933d0 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 669 | |
| 670 | /* empty property */ |
| 671 | ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value")); |
| 672 | ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0, |
| 673 | &out)); |
Simon Glass | 075bfc9 | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 674 | ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value", |
| 675 | &val)); |
Simon Glass | fb933d0 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 676 | |
| 677 | /* badly formatted string list */ |
| 678 | ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value")); |
| 679 | ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0, |
| 680 | &out)); |
Simon Glass | 075bfc9 | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 681 | ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value", |
| 682 | &val)); |
Simon Glass | fb933d0 | 2021-10-23 17:26:04 -0600 | [diff] [blame] | 683 | |
| 684 | /* out of range / not found */ |
| 685 | ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1, |
| 686 | &out)); |
| 687 | ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value", |
| 688 | "other")); |
| 689 | |
| 690 | /* negative value for index is not allowed, so don't test for that */ |
| 691 | |
| 692 | ut_asserteq(-ENODATA, ofnode_read_string_index(node, |
| 693 | "mux-control-names", 5, |
| 694 | &out)); |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE); |
Marek Behún | f3dd213 | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 699 | |
| 700 | static int dm_test_ofnode_get_phy(struct unit_test_state *uts) |
| 701 | { |
| 702 | ofnode eth_node, phy_node; |
Marek Behún | 123ca11 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 703 | phy_interface_t mode; |
Marek Behún | f3dd213 | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 704 | u32 reg; |
| 705 | |
| 706 | eth_node = ofnode_path("/phy-test-eth"); |
| 707 | ut_assert(ofnode_valid(eth_node)); |
| 708 | |
Marek Behún | 123ca11 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 709 | mode = ofnode_read_phy_mode(eth_node); |
| 710 | ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX); |
| 711 | |
Marek Behún | f3dd213 | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 712 | phy_node = ofnode_get_phy_node(eth_node); |
| 713 | ut_assert(ofnode_valid(phy_node)); |
| 714 | |
| 715 | reg = ofnode_read_u32_default(phy_node, "reg", -1U); |
| 716 | ut_asserteq_64(0x1, reg); |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | DM_TEST(dm_test_ofnode_get_phy, 0); |
Simon Glass | 3310484 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 721 | |
| 722 | /** |
| 723 | * make_ofnode_fdt() - Create an FDT for testing with ofnode |
| 724 | * |
| 725 | * The size is set to the minimum needed |
| 726 | * |
| 727 | * @uts: Test state |
| 728 | * @fdt: Place to write FDT |
| 729 | * @size: Maximum size of space for fdt |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 730 | * @id: id value to add to the tree ('id' property in root node) |
Simon Glass | 3310484 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 731 | */ |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 732 | static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size, |
| 733 | int id) |
Simon Glass | 3310484 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 734 | { |
| 735 | ut_assertok(fdt_create(fdt, size)); |
| 736 | ut_assertok(fdt_finish_reservemap(fdt)); |
| 737 | ut_assert(fdt_begin_node(fdt, "") >= 0); |
| 738 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 739 | ut_assertok(fdt_property_u32(fdt, "id", id)); |
| 740 | |
Simon Glass | 3310484 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 741 | ut_assert(fdt_begin_node(fdt, "aliases") >= 0); |
| 742 | ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc")); |
| 743 | ut_assertok(fdt_end_node(fdt)); |
| 744 | |
| 745 | ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0); |
| 746 | ut_assertok(fdt_end_node(fdt)); |
| 747 | |
| 748 | ut_assertok(fdt_end_node(fdt)); |
| 749 | ut_assertok(fdt_finish(fdt)); |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | static int dm_test_ofnode_root(struct unit_test_state *uts) |
| 755 | { |
Simon Glass | 3310484 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 756 | ofnode node; |
| 757 | |
| 758 | /* Check that aliases work on the control FDT */ |
| 759 | node = ofnode_get_aliases_node("ethernet3"); |
| 760 | ut_assert(ofnode_valid(node)); |
| 761 | ut_asserteq_str("sbe5", ofnode_get_name(node)); |
| 762 | |
Simon Glass | 085d594 | 2022-09-06 20:27:21 -0600 | [diff] [blame] | 763 | ut_assert(!oftree_valid(oftree_null())); |
| 764 | |
Simon Glass | c43635b | 2022-10-20 18:22:49 -0600 | [diff] [blame] | 765 | return 0; |
| 766 | } |
| 767 | DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT); |
| 768 | |
| 769 | static int dm_test_ofnode_root_mult(struct unit_test_state *uts) |
| 770 | { |
| 771 | char fdt[256]; |
| 772 | oftree tree; |
| 773 | ofnode node; |
| 774 | |
| 775 | /* skip this test if multiple FDTs are not supported */ |
| 776 | if (!IS_ENABLED(CONFIG_OFNODE_MULTI_TREE)) |
| 777 | return -EAGAIN; |
| 778 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 779 | ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt), 0)); |
Simon Glass | c43635b | 2022-10-20 18:22:49 -0600 | [diff] [blame] | 780 | ut_assertok(get_oftree(uts, fdt, &tree)); |
Simon Glass | 085d594 | 2022-09-06 20:27:21 -0600 | [diff] [blame] | 781 | ut_assert(oftree_valid(tree)); |
Simon Glass | 3310484 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 782 | |
| 783 | /* Make sure they don't work on this new tree */ |
Simon Glass | b7bd94f | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 784 | node = oftree_path(tree, "mmc0"); |
Simon Glass | 3310484 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 785 | ut_assert(!ofnode_valid(node)); |
| 786 | |
| 787 | /* It should appear in the new tree */ |
Simon Glass | b7bd94f | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 788 | node = oftree_path(tree, "/new-mmc"); |
Simon Glass | 3310484 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 789 | ut_assert(ofnode_valid(node)); |
| 790 | |
| 791 | /* ...and not in the control FDT */ |
Simon Glass | b7bd94f | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 792 | node = oftree_path(oftree_default(), "/new-mmc"); |
Simon Glass | 3310484 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 793 | ut_assert(!ofnode_valid(node)); |
| 794 | |
Simon Glass | 88a1ae8 | 2022-09-06 20:27:29 -0600 | [diff] [blame] | 795 | free_oftree(tree); |
Simon Glass | 3310484 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 796 | |
| 797 | return 0; |
| 798 | } |
Simon Glass | c43635b | 2022-10-20 18:22:49 -0600 | [diff] [blame] | 799 | DM_TEST(dm_test_ofnode_root_mult, UT_TESTF_SCAN_FDT); |
Simon Glass | 6571559 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 800 | |
| 801 | static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts) |
| 802 | { |
| 803 | struct udevice *dev; |
| 804 | ofnode node; |
| 805 | |
Simon Glass | 6571559 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 806 | /* Test enabling devices */ |
Simon Glass | 6571559 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 807 | node = ofnode_path("/usb@2"); |
| 808 | |
Simon Glass | 39e42be | 2022-07-30 15:52:13 -0600 | [diff] [blame] | 809 | ut_assert(!ofnode_is_enabled(node)); |
| 810 | ut_assertok(ofnode_set_enabled(node, true)); |
| 811 | ut_asserteq(true, ofnode_is_enabled(node)); |
Simon Glass | 6571559 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 812 | |
| 813 | device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node, |
| 814 | &dev); |
| 815 | ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev)); |
| 816 | |
| 817 | /* Test string property setting */ |
Simon Glass | 6571559 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 818 | ut_assert(device_is_compatible(dev, "sandbox,usb")); |
| 819 | ofnode_write_string(node, "compatible", "gdsys,super-usb"); |
| 820 | ut_assert(device_is_compatible(dev, "gdsys,super-usb")); |
| 821 | ofnode_write_string(node, "compatible", "sandbox,usb"); |
| 822 | ut_assert(device_is_compatible(dev, "sandbox,usb")); |
| 823 | |
| 824 | /* Test setting generic properties */ |
| 825 | |
| 826 | /* Non-existent in DTB */ |
| 827 | ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev)); |
| 828 | /* reg = 0x42, size = 0x100 */ |
Simon Glass | be0789a | 2022-07-30 15:52:10 -0600 | [diff] [blame] | 829 | ut_assertok(ofnode_write_prop(node, "reg", |
Simon Glass | 0b58eaa | 2022-09-06 20:27:32 -0600 | [diff] [blame] | 830 | "\x00\x00\x00\x42\x00\x00\x01\x00", 8, |
| 831 | false)); |
Simon Glass | 6571559 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 832 | ut_asserteq(0x42, dev_read_addr(dev)); |
| 833 | |
| 834 | /* Test disabling devices */ |
Simon Glass | 6571559 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 835 | device_remove(dev, DM_REMOVE_NORMAL); |
| 836 | device_unbind(dev); |
| 837 | |
Simon Glass | 39e42be | 2022-07-30 15:52:13 -0600 | [diff] [blame] | 838 | ut_assert(ofnode_is_enabled(node)); |
| 839 | ut_assertok(ofnode_set_enabled(node, false)); |
| 840 | ut_assert(!ofnode_is_enabled(node)); |
Simon Glass | 6571559 | 2022-07-30 15:52:09 -0600 | [diff] [blame] | 841 | |
| 842 | return 0; |
| 843 | } |
Simon Glass | b7eaa4f | 2022-07-30 15:52:11 -0600 | [diff] [blame] | 844 | DM_TEST(dm_test_ofnode_livetree_writing, |
Simon Glass | 2b90e0d | 2022-09-06 20:27:07 -0600 | [diff] [blame] | 845 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 55f7990 | 2022-07-30 15:52:14 -0600 | [diff] [blame] | 846 | |
Simon Glass | 0b58eaa | 2022-09-06 20:27:32 -0600 | [diff] [blame] | 847 | static int check_write_prop(struct unit_test_state *uts, ofnode node) |
| 848 | { |
| 849 | char prop[] = "middle-name"; |
| 850 | char name[10]; |
| 851 | int len; |
| 852 | |
| 853 | strcpy(name, "cecil"); |
| 854 | len = strlen(name) + 1; |
| 855 | ut_assertok(ofnode_write_prop(node, prop, name, len, false)); |
| 856 | ut_asserteq_str(name, ofnode_read_string(node, prop)); |
| 857 | |
| 858 | /* change the underlying value, this should mess up the live tree */ |
| 859 | strcpy(name, "tony"); |
| 860 | if (of_live_active()) { |
| 861 | ut_asserteq_str(name, ofnode_read_string(node, prop)); |
| 862 | } else { |
| 863 | ut_asserteq_str("cecil", ofnode_read_string(node, prop)); |
| 864 | } |
| 865 | |
| 866 | /* try again, this time copying the property */ |
| 867 | strcpy(name, "mary"); |
| 868 | ut_assertok(ofnode_write_prop(node, prop, name, len, true)); |
| 869 | ut_asserteq_str(name, ofnode_read_string(node, prop)); |
| 870 | strcpy(name, "leah"); |
| 871 | |
| 872 | /* both flattree and livetree behave the same */ |
| 873 | ut_asserteq_str("mary", ofnode_read_string(node, prop)); |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | /* writing the tree with and without copying the property */ |
| 879 | static int dm_test_ofnode_write_copy(struct unit_test_state *uts) |
| 880 | { |
| 881 | ofnode node; |
| 882 | |
| 883 | node = ofnode_path("/a-test"); |
| 884 | ut_assertok(check_write_prop(uts, node)); |
| 885 | |
| 886 | return 0; |
| 887 | } |
| 888 | DM_TEST(dm_test_ofnode_write_copy, UT_TESTF_SCAN_FDT); |
| 889 | |
| 890 | static int dm_test_ofnode_write_copy_ot(struct unit_test_state *uts) |
| 891 | { |
| 892 | oftree otree = get_other_oftree(uts); |
| 893 | ofnode node, check_node; |
| 894 | |
| 895 | node = oftree_path(otree, "/node"); |
| 896 | ut_assertok(check_write_prop(uts, node)); |
| 897 | |
| 898 | /* make sure the control FDT is not touched */ |
| 899 | check_node = ofnode_path("/node"); |
| 900 | ut_assertnull(ofnode_read_string(check_node, "middle-name")); |
| 901 | |
| 902 | return 0; |
| 903 | } |
| 904 | DM_TEST(dm_test_ofnode_write_copy_ot, UT_TESTF_OTHER_FDT); |
| 905 | |
Simon Glass | 55f7990 | 2022-07-30 15:52:14 -0600 | [diff] [blame] | 906 | static int dm_test_ofnode_u32(struct unit_test_state *uts) |
| 907 | { |
| 908 | ofnode node; |
Simon Glass | 66d0d0c | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 909 | u32 val; |
Simon Glass | 55f7990 | 2022-07-30 15:52:14 -0600 | [diff] [blame] | 910 | |
| 911 | node = ofnode_path("/lcd"); |
| 912 | ut_assert(ofnode_valid(node)); |
| 913 | ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123)); |
| 914 | ut_assertok(ofnode_write_u32(node, "xres", 1367)); |
| 915 | ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123)); |
| 916 | ut_assertok(ofnode_write_u32(node, "xres", 1366)); |
| 917 | |
Simon Glass | 66d0d0c | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 918 | node = ofnode_path("/backlight"); |
| 919 | ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val)); |
| 920 | ut_asserteq(0, val); |
| 921 | ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val)); |
| 922 | ut_asserteq(16, val); |
| 923 | ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val)); |
| 924 | ut_asserteq(255, val); |
| 925 | ut_asserteq(-EOVERFLOW, |
| 926 | ofnode_read_u32_index(node, "brightness-levels", 9, &val)); |
| 927 | ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val)); |
| 928 | |
Simon Glass | 55f7990 | 2022-07-30 15:52:14 -0600 | [diff] [blame] | 929 | return 0; |
| 930 | } |
Simon Glass | 2b90e0d | 2022-09-06 20:27:07 -0600 | [diff] [blame] | 931 | DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | ffe9039 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 932 | |
Simon Glass | 66d0d0c | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 933 | static int dm_test_ofnode_u32_array(struct unit_test_state *uts) |
| 934 | { |
| 935 | ofnode node; |
| 936 | u32 val[10]; |
| 937 | |
| 938 | node = ofnode_path("/a-test"); |
| 939 | ut_assert(ofnode_valid(node)); |
| 940 | ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1)); |
| 941 | ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1)); |
| 942 | ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val, |
| 943 | 1)); |
| 944 | |
| 945 | memset(val, '\0', sizeof(val)); |
| 946 | ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3)); |
| 947 | ut_asserteq(0, val[0]); |
| 948 | ut_asserteq(5678, val[1]); |
| 949 | ut_asserteq(9123, val[2]); |
| 950 | ut_asserteq(4567, val[3]); |
| 951 | ut_asserteq(0, val[4]); |
| 952 | ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val, |
| 953 | 4)); |
| 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 958 | |
| 959 | static int dm_test_ofnode_u64(struct unit_test_state *uts) |
| 960 | { |
| 961 | ofnode node; |
| 962 | u64 val; |
| 963 | |
| 964 | node = ofnode_path("/a-test"); |
| 965 | ut_assert(ofnode_valid(node)); |
| 966 | ut_assertok(ofnode_read_u64(node, "int64-value", &val)); |
| 967 | ut_asserteq_64(0x1111222233334444, val); |
| 968 | ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val)); |
| 969 | |
Michal Simek | fa12dfa | 2023-08-25 11:37:46 +0200 | [diff] [blame^] | 970 | ut_assertok(ofnode_read_u64_index(node, "int64-array", 0, &val)); |
| 971 | ut_asserteq_64(0x1111222233334444, val); |
| 972 | ut_assertok(ofnode_read_u64_index(node, "int64-array", 1, &val)); |
| 973 | ut_asserteq_64(0x4444333322221111, val); |
| 974 | ut_asserteq(-EOVERFLOW, |
| 975 | ofnode_read_u64_index(node, "int64-array", 2, &val)); |
| 976 | ut_asserteq(-EINVAL, ofnode_read_u64_index(node, "missing", 0, &val)); |
| 977 | |
Simon Glass | 66d0d0c | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 978 | return 0; |
| 979 | } |
| 980 | DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT); |
| 981 | |
Simon Glass | ffe9039 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 982 | static int dm_test_ofnode_add_subnode(struct unit_test_state *uts) |
| 983 | { |
| 984 | ofnode node, check, subnode; |
| 985 | char buf[128]; |
| 986 | |
Simon Glass | ffe9039 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 987 | node = ofnode_path("/lcd"); |
| 988 | ut_assert(ofnode_valid(node)); |
| 989 | ut_assertok(ofnode_add_subnode(node, "edmund", &subnode)); |
| 990 | check = ofnode_path("/lcd/edmund"); |
| 991 | ut_asserteq(subnode.of_offset, check.of_offset); |
| 992 | ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf))); |
| 993 | ut_asserteq_str("/lcd/edmund", buf); |
| 994 | |
| 995 | if (of_live_active()) { |
| 996 | struct device_node *child; |
| 997 | |
| 998 | ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund", |
| 999 | 2, &child)); |
| 1000 | ut_asserteq_str("ed", child->name); |
| 1001 | ut_asserteq_str("/lcd/ed", child->full_name); |
| 1002 | check = ofnode_path("/lcd/ed"); |
| 1003 | ut_asserteq_ptr(child, check.np); |
| 1004 | ut_assertok(ofnode_get_path(np_to_ofnode(child), buf, |
| 1005 | sizeof(buf))); |
| 1006 | ut_asserteq_str("/lcd/ed", buf); |
| 1007 | } |
| 1008 | |
| 1009 | /* An existing node should be returned with -EEXIST */ |
| 1010 | ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check)); |
| 1011 | ut_asserteq(subnode.of_offset, check.of_offset); |
| 1012 | |
| 1013 | /* add a root node */ |
| 1014 | node = ofnode_path("/"); |
| 1015 | ut_assert(ofnode_valid(node)); |
| 1016 | ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode)); |
| 1017 | check = ofnode_path("/lcd2"); |
| 1018 | ut_asserteq(subnode.of_offset, check.of_offset); |
| 1019 | ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf))); |
| 1020 | ut_asserteq_str("/lcd2", buf); |
| 1021 | |
| 1022 | if (of_live_active()) { |
| 1023 | ulong start; |
| 1024 | int i; |
| 1025 | |
| 1026 | /* |
| 1027 | * Make sure each of the three malloc()checks in |
| 1028 | * of_add_subnode() work |
| 1029 | */ |
| 1030 | for (i = 0; i < 3; i++) { |
| 1031 | malloc_enable_testing(i); |
| 1032 | start = ut_check_free(); |
| 1033 | ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony", |
| 1034 | &check)); |
| 1035 | ut_assertok(ut_check_delta(start)); |
| 1036 | } |
| 1037 | |
| 1038 | /* This should pass since we allow 3 allocations */ |
| 1039 | malloc_enable_testing(3); |
| 1040 | ut_assertok(ofnode_add_subnode(node, "anthony", &check)); |
| 1041 | malloc_disable_testing(); |
| 1042 | } |
| 1043 | |
Simon Glass | c3a194d | 2022-09-06 20:27:03 -0600 | [diff] [blame] | 1044 | /* write to the empty node */ |
| 1045 | ut_assertok(ofnode_write_string(subnode, "example", "text")); |
| 1046 | |
Simon Glass | ffe9039 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 1047 | return 0; |
| 1048 | } |
Simon Glass | 2b90e0d | 2022-09-06 20:27:07 -0600 | [diff] [blame] | 1049 | DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 52ad21a | 2022-09-06 20:27:16 -0600 | [diff] [blame] | 1050 | |
| 1051 | static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts) |
| 1052 | { |
| 1053 | ofnode node, subnode; |
| 1054 | struct ofprop prop; |
| 1055 | int count; |
| 1056 | |
Dzmitry Sankouski | 298ffdd | 2023-01-22 18:21:23 +0300 | [diff] [blame] | 1057 | node = ofnode_path("/ofnode-foreach"); |
Simon Glass | 52ad21a | 2022-09-06 20:27:16 -0600 | [diff] [blame] | 1058 | count = 0; |
| 1059 | |
| 1060 | /* we expect "compatible" for each node */ |
| 1061 | ofnode_for_each_prop(prop, node) |
| 1062 | count++; |
| 1063 | ut_asserteq(1, count); |
| 1064 | |
| 1065 | /* there are two nodes, each with 2 properties */ |
| 1066 | ofnode_for_each_subnode(subnode, node) |
| 1067 | ofnode_for_each_prop(prop, subnode) |
| 1068 | count++; |
| 1069 | ut_asserteq(5, count); |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT); |
Simon Glass | 66d0d0c | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 1074 | |
| 1075 | static int dm_test_ofnode_by_compatible(struct unit_test_state *uts) |
| 1076 | { |
| 1077 | const char *compat = "denx,u-boot-fdt-test"; |
| 1078 | ofnode node; |
| 1079 | int count; |
| 1080 | |
| 1081 | count = 0; |
| 1082 | for (node = ofnode_null(); |
| 1083 | node = ofnode_by_compatible(node, compat), ofnode_valid(node);) |
| 1084 | count++; |
| 1085 | ut_asserteq(11, count); |
| 1086 | |
| 1087 | return 0; |
| 1088 | } |
| 1089 | DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT); |
| 1090 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 1091 | static int dm_test_ofnode_by_compatible_ot(struct unit_test_state *uts) |
| 1092 | { |
| 1093 | const char *compat = "sandbox-other2"; |
| 1094 | oftree otree = get_other_oftree(uts); |
| 1095 | ofnode node; |
| 1096 | int count; |
| 1097 | |
| 1098 | count = 0; |
| 1099 | for (node = oftree_root(otree); |
| 1100 | node = ofnode_by_compatible(node, compat), ofnode_valid(node);) |
| 1101 | count++; |
| 1102 | ut_asserteq(2, count); |
| 1103 | |
| 1104 | return 0; |
| 1105 | } |
| 1106 | DM_TEST(dm_test_ofnode_by_compatible_ot, UT_TESTF_OTHER_FDT); |
| 1107 | |
Simon Glass | 66d0d0c | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 1108 | static int dm_test_ofnode_find_subnode(struct unit_test_state *uts) |
| 1109 | { |
| 1110 | ofnode node, subnode; |
| 1111 | |
| 1112 | node = ofnode_path("/buttons"); |
| 1113 | |
| 1114 | subnode = ofnode_find_subnode(node, "btn1"); |
| 1115 | ut_assert(ofnode_valid(subnode)); |
| 1116 | ut_asserteq_str("btn1", ofnode_get_name(subnode)); |
| 1117 | |
| 1118 | subnode = ofnode_find_subnode(node, "btn"); |
| 1119 | ut_assert(!ofnode_valid(subnode)); |
| 1120 | |
| 1121 | return 0; |
| 1122 | } |
| 1123 | DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT); |
| 1124 | |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 1125 | static int dm_test_ofnode_find_subnode_ot(struct unit_test_state *uts) |
| 1126 | { |
| 1127 | oftree otree = get_other_oftree(uts); |
| 1128 | ofnode node, subnode; |
| 1129 | |
| 1130 | node = oftree_path(otree, "/node"); |
| 1131 | |
| 1132 | subnode = ofnode_find_subnode(node, "subnode"); |
| 1133 | ut_assert(ofnode_valid(subnode)); |
| 1134 | ut_asserteq_str("subnode", ofnode_get_name(subnode)); |
| 1135 | |
| 1136 | subnode = ofnode_find_subnode(node, "btn"); |
| 1137 | ut_assert(!ofnode_valid(subnode)); |
| 1138 | |
| 1139 | return 0; |
| 1140 | } |
| 1141 | DM_TEST(dm_test_ofnode_find_subnode_ot, UT_TESTF_OTHER_FDT); |
| 1142 | |
Simon Glass | 66d0d0c | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 1143 | static int dm_test_ofnode_get_name(struct unit_test_state *uts) |
| 1144 | { |
| 1145 | ofnode node; |
| 1146 | |
| 1147 | node = ofnode_path("/buttons"); |
| 1148 | ut_assert(ofnode_valid(node)); |
| 1149 | ut_asserteq_str("buttons", ofnode_get_name(node)); |
| 1150 | ut_asserteq_str("", ofnode_get_name(ofnode_root())); |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
| 1154 | DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT); |
Simon Glass | 47a677c | 2022-09-06 20:27:30 -0600 | [diff] [blame] | 1155 | |
| 1156 | /* try to access more FDTs than is supported */ |
| 1157 | static int dm_test_ofnode_too_many(struct unit_test_state *uts) |
| 1158 | { |
| 1159 | const int max_trees = CONFIG_IS_ENABLED(OFNODE_MULTI_TREE, |
| 1160 | (CONFIG_OFNODE_MULTI_TREE_MAX), (1)); |
| 1161 | const int fdt_size = 256; |
| 1162 | const int num_trees = max_trees + 1; |
| 1163 | char fdt[num_trees][fdt_size]; |
| 1164 | int i; |
| 1165 | |
| 1166 | for (i = 0; i < num_trees; i++) { |
| 1167 | oftree tree; |
| 1168 | int ret; |
| 1169 | |
| 1170 | ut_assertok(make_ofnode_fdt(uts, fdt[i], fdt_size, i)); |
| 1171 | ret = get_oftree(uts, fdt[i], &tree); |
| 1172 | |
| 1173 | /* |
| 1174 | * With flat tree we have the control FDT using one slot. Live |
| 1175 | * tree has no limit since it uses pointers, not integer tree |
| 1176 | * IDs |
| 1177 | */ |
| 1178 | if (of_live_active() || i < max_trees - 1) { |
| 1179 | ut_assertok(ret); |
| 1180 | } else { |
| 1181 | /* |
| 1182 | * tree should be invalid when we try to register too |
| 1183 | * many trees |
| 1184 | */ |
| 1185 | ut_asserteq(-EOVERFLOW, ret); |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | return 0; |
| 1190 | } |
| 1191 | DM_TEST(dm_test_ofnode_too_many, UT_TESTF_SCAN_FDT); |
Simon Glass | db1ef1e | 2022-09-06 20:27:33 -0600 | [diff] [blame] | 1192 | |
| 1193 | static int check_copy_props(struct unit_test_state *uts, ofnode src, |
| 1194 | ofnode dst) |
| 1195 | { |
| 1196 | u32 reg[2], val; |
| 1197 | |
| 1198 | ut_assertok(ofnode_copy_props(src, dst)); |
| 1199 | |
| 1200 | ut_assertok(ofnode_read_u32(dst, "ping-expect", &val)); |
| 1201 | ut_asserteq(3, val); |
| 1202 | |
| 1203 | ut_asserteq_str("denx,u-boot-fdt-test", |
| 1204 | ofnode_read_string(dst, "compatible")); |
| 1205 | |
| 1206 | /* check that a property with the same name is overwritten */ |
| 1207 | ut_assertok(ofnode_read_u32_array(dst, "reg", reg, ARRAY_SIZE(reg))); |
| 1208 | ut_asserteq(3, reg[0]); |
| 1209 | ut_asserteq(1, reg[1]); |
| 1210 | |
| 1211 | /* reset the compatible so the live tree does not change */ |
| 1212 | ut_assertok(ofnode_write_string(dst, "compatible", "nothing")); |
| 1213 | |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | static int dm_test_ofnode_copy_props(struct unit_test_state *uts) |
| 1218 | { |
| 1219 | ofnode src, dst; |
| 1220 | |
| 1221 | /* |
| 1222 | * These nodes are chosen so that the src node is before the destination |
| 1223 | * node in the tree. This doesn't matter with livetree, but with |
| 1224 | * flattree any attempt to insert a property earlier in the tree will |
| 1225 | * mess up the offsets after it. |
| 1226 | */ |
| 1227 | src = ofnode_path("/b-test"); |
| 1228 | dst = ofnode_path("/some-bus"); |
| 1229 | |
| 1230 | ut_assertok(check_copy_props(uts, src, dst)); |
| 1231 | |
| 1232 | /* check a property that is in the destination already */ |
| 1233 | ut_asserteq_str("mux0", ofnode_read_string(dst, "mux-control-names")); |
| 1234 | |
| 1235 | return 0; |
| 1236 | } |
| 1237 | DM_TEST(dm_test_ofnode_copy_props, UT_TESTF_SCAN_FDT); |
| 1238 | |
| 1239 | static int dm_test_ofnode_copy_props_ot(struct unit_test_state *uts) |
| 1240 | { |
| 1241 | ofnode src, dst; |
| 1242 | oftree otree = get_other_oftree(uts); |
| 1243 | |
| 1244 | src = ofnode_path("/b-test"); |
| 1245 | dst = oftree_path(otree, "/node/subnode2"); |
| 1246 | ut_assertok(check_copy_props(uts, src, dst)); |
| 1247 | |
| 1248 | return 0; |
| 1249 | } |
| 1250 | DM_TEST(dm_test_ofnode_copy_props_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT); |
Simon Glass | 9cf39bb | 2023-06-01 10:22:40 -0600 | [diff] [blame] | 1251 | |
| 1252 | /* check that the livetree is aligned to a structure boundary */ |
| 1253 | static int dm_test_livetree_align(struct unit_test_state *uts) |
| 1254 | { |
| 1255 | const int align = __alignof__(struct unit_test_state); |
| 1256 | struct device_node *node; |
| 1257 | u32 *sentinel; |
| 1258 | ulong start; |
| 1259 | |
| 1260 | start = (ulong)gd_of_root(); |
| 1261 | ut_asserteq(start, ALIGN(start, align)); |
| 1262 | |
| 1263 | node = gd_of_root(); |
| 1264 | sentinel = (void *)node - sizeof(u32); |
| 1265 | |
| 1266 | /* |
| 1267 | * The sentinel should be overwritten with the root node. If it isn't, |
| 1268 | * then the root node is not at the very start of the livetree memory |
| 1269 | * area, and free(root) will fail to free the memory used by the |
| 1270 | * livetree. |
| 1271 | */ |
| 1272 | ut_assert(*sentinel != BAD_OF_ROOT); |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | DM_TEST(dm_test_livetree_align, UT_TESTF_LIVE_TREE); |
Simon Glass | a8f2ac2 | 2023-06-01 10:22:42 -0600 | [diff] [blame] | 1277 | |
| 1278 | /* check that it is possible to load an arbitrary livetree */ |
| 1279 | static int dm_test_livetree_ensure(struct unit_test_state *uts) |
| 1280 | { |
| 1281 | oftree tree; |
| 1282 | ofnode node; |
| 1283 | |
| 1284 | /* read from other.dtb */ |
| 1285 | ut_assertok(test_load_other_fdt(uts)); |
| 1286 | tree = oftree_from_fdt(uts->other_fdt); |
| 1287 | ut_assert(oftree_valid(tree)); |
| 1288 | node = oftree_path(tree, "/node/subnode"); |
| 1289 | ut_assert(ofnode_valid(node)); |
| 1290 | ut_asserteq_str("sandbox-other2", |
| 1291 | ofnode_read_string(node, "compatible")); |
| 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | DM_TEST(dm_test_livetree_ensure, 0); |