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