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