blob: 5459a9afbb997067c6dcf68a27fa287a97404c68 [file] [log] [blame]
Masahiro Yamada04ca8712018-04-27 01:02:02 +09001// SPDX-License-Identifier: GPL-2.0+
Simon Glass5e969252022-09-06 20:27:27 -06002/*
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 Yamada04ca8712018-04-27 01:02:02 +090018
19#include <common.h>
20#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060021#include <log.h>
Simon Glass33104842022-07-30 15:52:08 -060022#include <of_live.h>
Simon Glass65715592022-07-30 15:52:09 -060023#include <dm/device-internal.h>
24#include <dm/lists.h>
Simon Glasse6c5c942018-10-01 12:22:08 -060025#include <dm/of_extra.h>
Simon Glass65715592022-07-30 15:52:09 -060026#include <dm/root.h>
Masahiro Yamada04ca8712018-04-27 01:02:02 +090027#include <dm/test.h>
Simon Glass65715592022-07-30 15:52:09 -060028#include <dm/uclass-internal.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060029#include <test/test.h>
Masahiro Yamada04ca8712018-04-27 01:02:02 +090030#include <test/ut.h>
31
Simon Glass5e969252022-09-06 20:27:27 -060032/**
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 */
38oftree 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 Glass88a1ae82022-09-06 20:27:29 -060054/**
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 */
64int 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 */
88void free_oftree(oftree tree)
89{
90 if (of_live_active())
91 free(tree.np);
92}
93
Simon Glass9bf78a52023-09-26 08:14:39 -060094/* test ofnode_device_is_compatible() */
Masahiro Yamada04ca8712018-04-27 01:02:02 +090095static 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 Glass47a677c2022-09-06 20:27:30 -0600104DM_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 */
108static 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 Glass3a1fc172023-09-26 08:14:38 -0600118DM_TEST(dm_test_ofnode_compatible_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200119
Patrick Delaunay6d9949f2020-09-24 17:26:20 +0200120static 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 Glass928d2672022-09-06 20:27:22 -0600132 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
133
Patrick Delaunay6d9949f2020-09-24 17:26:20 +0200134 return 0;
135}
136DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
137
Simon Glass9bf78a52023-09-26 08:14:39 -0600138/* test oftree_get_by_phandle() with a the 'other' oftree */
Simon Glass5e969252022-09-06 20:27:27 -0600139static 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 Glass3a1fc172023-09-26 08:14:38 -0600151DM_TEST(dm_test_ofnode_get_by_phandle_ot,
152 UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass5e969252022-09-06 20:27:27 -0600153
Simon Glass47a677c2022-09-06 20:27:30 -0600154static int check_prop_values(struct unit_test_state *uts, ofnode start,
155 const char *propname, const char *propval,
156 int expect_count)
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200157{
Simon Glass47a677c2022-09-06 20:27:30 -0600158 int proplen = strlen(propval) + 1;
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200159 const char *str;
Simon Glass47a677c2022-09-06 20:27:30 -0600160 ofnode node;
161 int count;
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200162
163 /* Find first matching node, there should be at least one */
Simon Glass47a677c2022-09-06 20:27:30 -0600164 node = ofnode_by_prop_value(start, propname, propval, proplen);
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200165 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 Glass47a677c2022-09-06 20:27:30 -0600170 count = 1;
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200171 while (true) {
Simon Glass47a677c2022-09-06 20:27:30 -0600172 node = ofnode_by_prop_value(node, propname, propval, proplen);
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200173 if (!ofnode_valid(node))
174 break;
175 str = ofnode_read_string(node, propname);
Simon Glass47a677c2022-09-06 20:27:30 -0600176 ut_asserteq_str(propval, str);
177 count++;
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200178 }
Simon Glass47a677c2022-09-06 20:27:30 -0600179 ut_asserteq(expect_count, count);
180
181 return 0;
182}
183
184static 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 Wiklander9bc7e962018-08-20 11:10:00 +0200188
189 return 0;
190}
Simon Glasse180c2b2020-07-28 19:41:12 -0600191DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
Simon Glasse6c5c942018-10-01 12:22:08 -0600192
Simon Glass9bf78a52023-09-26 08:14:39 -0600193/* test ofnode_by_prop_value() with a the 'other' oftree */
Simon Glass47a677c2022-09-06 20:27:30 -0600194static 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 Glass3a1fc172023-09-26 08:14:38 -0600203DM_TEST(dm_test_ofnode_by_prop_value_ot,
204 UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600205
Simon Glass9bf78a52023-09-26 08:14:39 -0600206/* test ofnode_read_fmap_entry() */
Simon Glasse6c5c942018-10-01 12:22:08 -0600207static 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 Glasse180c2b2020-07-28 19:41:12 -0600220DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass14ca9f72020-01-27 08:49:43 -0700221
Simon Glass9bf78a52023-09-26 08:14:39 -0600222/* test ofnode_read_prop() */
Simon Glassa8167d82020-01-27 08:49:44 -0700223static 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 Glass3a1fc172023-09-26 08:14:38 -0600247DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_FDT);
Simon Glassa8167d82020-01-27 08:49:44 -0700248
Simon Glass9bf78a52023-09-26 08:14:39 -0600249/* test ofnode_read_prop() with the 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -0600250static 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 Glass3a1fc172023-09-26 08:14:38 -0600267DM_TEST(dm_test_ofnode_read_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600268
Simon Glass9bf78a52023-09-26 08:14:39 -0600269/* test ofnode_count_/parse_phandle_with_args() */
Patrick Delaunaycc72f3e2020-09-25 09:41:16 +0200270static 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}
343DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
344
Simon Glass9bf78a52023-09-26 08:14:39 -0600345/* test ofnode_count_/parse_phandle_with_args() with 'other' tree */
Simon Glass5e969252022-09-06 20:27:27 -0600346static 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}
372DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT);
373
Simon Glass9bf78a52023-09-26 08:14:39 -0600374/* test ofnode_read_chosen_string/node/prop() */
Simon Glass14ca9f72020-01-27 08:49:43 -0700375static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
376{
377 const char *str;
Simon Glassbd933bf2020-01-27 08:49:46 -0700378 const u32 *val;
Simon Glass14ca9f72020-01-27 08:49:43 -0700379 ofnode node;
Simon Glassbd933bf2020-01-27 08:49:46 -0700380 int size;
Simon Glass14ca9f72020-01-27 08:49:43 -0700381
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 Glassbd933bf2020-01-27 08:49:46 -0700394 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 Glass14ca9f72020-01-27 08:49:43 -0700400 return 0;
401}
Simon Glasse180c2b2020-07-28 19:41:12 -0600402DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chunfeng Yunbf6ad912020-05-02 11:35:10 +0200403
Simon Glass9bf78a52023-09-26 08:14:39 -0600404/* test ofnode_get_aliases_node/prop() */
Michal Simek305d3182020-07-28 12:51:08 +0200405static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
406{
407 const void *val;
408 ofnode node;
409 int size;
410
Michael Walle82a3c9e2021-02-25 16:51:11 +0100411 node = ofnode_get_aliases_node("ethernet3");
Michal Simek305d3182020-07-28 12:51:08 +0200412 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}
425DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
426
Chunfeng Yunbf6ad912020-05-02 11:35:10 +0200427static 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}
445DM_TEST(dm_test_ofnode_get_child_count,
Simon Glasse180c2b2020-07-28 19:41:12 -0600446 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0de1b072020-11-28 17:50:02 -0700447
Simon Glass9bf78a52023-09-26 08:14:39 -0600448/* test ofnode_get_child_count() with 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -0600449static 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 Glass3a1fc172023-09-26 08:14:38 -0600468DM_TEST(dm_test_ofnode_get_child_count_ot,
469 UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600470
Simon Glass0de1b072020-11-28 17:50:02 -0700471static 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}
481DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800482
Simon Glass9bf78a52023-09-26 08:14:39 -0600483/* test ofnode_is_enabled() with 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -0600484static 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}
495DM_TEST(dm_test_ofnode_is_enabled_ot, UT_TESTF_OTHER_FDT);
496
Simon Glass9bf78a52023-09-26 08:14:39 -0600497/* test ofnode_get_addr/size() */
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800498static 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 Chotard9876ae72022-01-04 08:42:48 +0100522 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800523 ut_asserteq(FDT_SIZE_T_NONE, size);
524
Marek Behún31a7b712021-05-26 14:08:17 +0200525 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 Guanqiao61772bc2021-04-12 14:51:12 +0800530 return 0;
531}
532DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Marek Behún0e116be2021-05-26 14:08:18 +0200533
Simon Glass9bf78a52023-09-26 08:14:39 -0600534/* test ofnode_get_addr() with 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -0600535static 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 Glass3a1fc172023-09-26 08:14:38 -0600546DM_TEST(dm_test_ofnode_get_reg_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600547
Marek Behún0e116be2021-05-26 14:08:18 +0200548static 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ún0e116be2021-05-26 14:08:18 +0200553
554 node = ofnode_path(path);
555 ut_assert(ofnode_valid(node));
556
Simon Glass47a677c2022-09-06 20:27:30 -0600557 ut_assertok(ofnode_get_path(node, buf, sizeof(buf)));
Marek Behún0e116be2021-05-26 14:08:18 +0200558 ut_asserteq_str(path, buf);
559
Simon Glass47a677c2022-09-06 20:27:30 -0600560 ut_asserteq(-ENOSPC, ofnode_get_path(node, buf, 32));
Marek Behún0e116be2021-05-26 14:08:18 +0200561
Simon Glass47a677c2022-09-06 20:27:30 -0600562 ut_assertok(ofnode_get_path(ofnode_root(), buf, 32));
Simon Glass66d0d0c2022-09-06 20:27:18 -0600563 ut_asserteq_str("/", buf);
564
Marek Behún0e116be2021-05-26 14:08:18 +0200565 return 0;
566}
567DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7de8bd02021-08-07 07:24:01 -0600568
Simon Glass9bf78a52023-09-26 08:14:39 -0600569/* test ofnode_get_path() with 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -0600570static 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 Glass3a1fc172023-09-26 08:14:38 -0600587DM_TEST(dm_test_ofnode_get_path_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600588
Simon Glass9bf78a52023-09-26 08:14:39 -0600589/* test ofnode_conf_read_bool/int/str() */
Simon Glass7de8bd02021-08-07 07:24:01 -0600590static 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 Glass3a1fc172023-09-26 08:14:38 -0600603DM_TEST(dm_test_ofnode_conf, UT_TESTF_SCAN_FDT);
Michael Wallebce039a2021-10-15 15:15:18 +0200604
Michal Simekdb5e3492023-08-31 08:59:05 +0200605static int dm_test_ofnode_options(struct unit_test_state *uts)
606{
Michal Simek44f35e12023-08-31 09:04:27 +0200607 u64 bootscr_address, bootscr_offset;
608 u64 bootscr_flash_offset, bootscr_flash_size;
Michal Simekdb5e3492023-08-31 08:59:05 +0200609
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 Simek44f35e12023-08-31 09:04:27 +0200615 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 Simekdb5e3492023-08-31 08:59:05 +0200620 return 0;
621}
622DM_TEST(dm_test_ofnode_options, 0);
623
Michael Wallebce039a2021-10-15 15:15:18 +0200624static 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}
640DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
Simon Glassfb933d02021-10-23 17:26:04 -0600641
Simon Glass9bf78a52023-09-26 08:14:39 -0600642/* test dm_test_ofnode_string_count/index/list() */
Simon Glassfb933d02021-10-23 17:26:04 -0600643static int dm_test_ofnode_string(struct unit_test_state *uts)
644{
Simon Glass075bfc92021-10-23 17:26:07 -0600645 const char **val;
Simon Glassfb933d02021-10-23 17:26:04 -0600646 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 Glass075bfc92021-10-23 17:26:07 -0600658 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 Glassfb933d02021-10-23 17:26:04 -0600662
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 Glass075bfc92021-10-23 17:26:07 -0600670 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 Glassfb933d02021-10-23 17:26:04 -0600679
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 Glass3a1fc172023-09-26 08:14:38 -0600688DM_TEST(dm_test_ofnode_string, UT_TESTF_SCAN_FDT);
Simon Glassfb933d02021-10-23 17:26:04 -0600689
Simon Glass9bf78a52023-09-26 08:14:39 -0600690/* test error returns from ofnode_read_string_count/index/list() */
Simon Glassfb933d02021-10-23 17:26:04 -0600691static int dm_test_ofnode_string_err(struct unit_test_state *uts)
692{
Simon Glass075bfc92021-10-23 17:26:07 -0600693 const char **val;
Simon Glassfb933d02021-10-23 17:26:04 -0600694 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 Glass075bfc92021-10-23 17:26:07 -0600708 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
Simon Glassfb933d02021-10-23 17:26:04 -0600709
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 Glass075bfc92021-10-23 17:26:07 -0600714 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
715 &val));
Simon Glassfb933d02021-10-23 17:26:04 -0600716
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 Glass075bfc92021-10-23 17:26:07 -0600721 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
722 &val));
Simon Glassfb933d02021-10-23 17:26:04 -0600723
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}
738DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
Marek Behúnf3dd2132022-04-07 00:32:57 +0200739
Simon Glass9bf78a52023-09-26 08:14:39 -0600740static int dm_test_ofnode_read_phy_mode(struct unit_test_state *uts)
Marek Behúnf3dd2132022-04-07 00:32:57 +0200741{
742 ofnode eth_node, phy_node;
Marek Behún123ca112022-04-07 00:33:01 +0200743 phy_interface_t mode;
Marek Behúnf3dd2132022-04-07 00:32:57 +0200744 u32 reg;
745
746 eth_node = ofnode_path("/phy-test-eth");
747 ut_assert(ofnode_valid(eth_node));
748
Marek Behún123ca112022-04-07 00:33:01 +0200749 mode = ofnode_read_phy_mode(eth_node);
750 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
751
Marek Behúnf3dd2132022-04-07 00:32:57 +0200752 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 Glass9bf78a52023-09-26 08:14:39 -0600760DM_TEST(dm_test_ofnode_read_phy_mode, UT_TESTF_SCAN_FDT);
Simon Glass33104842022-07-30 15:52:08 -0600761
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 Glass47a677c2022-09-06 20:27:30 -0600770 * @id: id value to add to the tree ('id' property in root node)
Simon Glass33104842022-07-30 15:52:08 -0600771 */
Simon Glass47a677c2022-09-06 20:27:30 -0600772static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size,
773 int id)
Simon Glass33104842022-07-30 15:52:08 -0600774{
775 ut_assertok(fdt_create(fdt, size));
776 ut_assertok(fdt_finish_reservemap(fdt));
777 ut_assert(fdt_begin_node(fdt, "") >= 0);
778
Simon Glass47a677c2022-09-06 20:27:30 -0600779 ut_assertok(fdt_property_u32(fdt, "id", id));
780
Simon Glass33104842022-07-30 15:52:08 -0600781 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 Glass9bf78a52023-09-26 08:14:39 -0600794/* Check that aliases work on the control FDT */
795static int dm_test_ofnode_aliases(struct unit_test_state *uts)
Simon Glass33104842022-07-30 15:52:08 -0600796{
Simon Glass33104842022-07-30 15:52:08 -0600797 ofnode node;
798
Simon Glass33104842022-07-30 15:52:08 -0600799 node = ofnode_get_aliases_node("ethernet3");
800 ut_assert(ofnode_valid(node));
801 ut_asserteq_str("sbe5", ofnode_get_name(node));
802
Simon Glass085d5942022-09-06 20:27:21 -0600803 ut_assert(!oftree_valid(oftree_null()));
804
Simon Glassc43635b2022-10-20 18:22:49 -0600805 return 0;
806}
Simon Glass9bf78a52023-09-26 08:14:39 -0600807DM_TEST(dm_test_ofnode_aliases, UT_TESTF_SCAN_FDT);
Simon Glassc43635b2022-10-20 18:22:49 -0600808
Simon Glass9bf78a52023-09-26 08:14:39 -0600809/**
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 Glassc43635b2022-10-20 18:22:49 -0600816static int dm_test_ofnode_root_mult(struct unit_test_state *uts)
817{
818 char fdt[256];
819 oftree tree;
820 ofnode node;
821
822 /* skip this test if multiple FDTs are not supported */
823 if (!IS_ENABLED(CONFIG_OFNODE_MULTI_TREE))
824 return -EAGAIN;
825
Simon Glass47a677c2022-09-06 20:27:30 -0600826 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt), 0));
Simon Glassc43635b2022-10-20 18:22:49 -0600827 ut_assertok(get_oftree(uts, fdt, &tree));
Simon Glass085d5942022-09-06 20:27:21 -0600828 ut_assert(oftree_valid(tree));
Simon Glass33104842022-07-30 15:52:08 -0600829
830 /* Make sure they don't work on this new tree */
Simon Glassb7bd94f2022-09-06 20:27:24 -0600831 node = oftree_path(tree, "mmc0");
Simon Glass33104842022-07-30 15:52:08 -0600832 ut_assert(!ofnode_valid(node));
833
834 /* It should appear in the new tree */
Simon Glassb7bd94f2022-09-06 20:27:24 -0600835 node = oftree_path(tree, "/new-mmc");
Simon Glass33104842022-07-30 15:52:08 -0600836 ut_assert(ofnode_valid(node));
837
838 /* ...and not in the control FDT */
Simon Glassb7bd94f2022-09-06 20:27:24 -0600839 node = oftree_path(oftree_default(), "/new-mmc");
Simon Glass33104842022-07-30 15:52:08 -0600840 ut_assert(!ofnode_valid(node));
841
Simon Glass88a1ae82022-09-06 20:27:29 -0600842 free_oftree(tree);
Simon Glass33104842022-07-30 15:52:08 -0600843
844 return 0;
845}
Simon Glassc43635b2022-10-20 18:22:49 -0600846DM_TEST(dm_test_ofnode_root_mult, UT_TESTF_SCAN_FDT);
Simon Glass65715592022-07-30 15:52:09 -0600847
Simon Glass9bf78a52023-09-26 08:14:39 -0600848/* test ofnode_set_enabled(), ofnode_write_prop() on a livetree */
Simon Glass65715592022-07-30 15:52:09 -0600849static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
850{
851 struct udevice *dev;
852 ofnode node;
853
Simon Glass65715592022-07-30 15:52:09 -0600854 /* Test enabling devices */
Simon Glass65715592022-07-30 15:52:09 -0600855 node = ofnode_path("/usb@2");
856
Simon Glass39e42be2022-07-30 15:52:13 -0600857 ut_assert(!ofnode_is_enabled(node));
858 ut_assertok(ofnode_set_enabled(node, true));
859 ut_asserteq(true, ofnode_is_enabled(node));
Simon Glass65715592022-07-30 15:52:09 -0600860
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 Glass65715592022-07-30 15:52:09 -0600866 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 Glassbe0789a2022-07-30 15:52:10 -0600877 ut_assertok(ofnode_write_prop(node, "reg",
Simon Glass0b58eaa2022-09-06 20:27:32 -0600878 "\x00\x00\x00\x42\x00\x00\x01\x00", 8,
879 false));
Simon Glass65715592022-07-30 15:52:09 -0600880 ut_asserteq(0x42, dev_read_addr(dev));
881
882 /* Test disabling devices */
Simon Glass65715592022-07-30 15:52:09 -0600883 device_remove(dev, DM_REMOVE_NORMAL);
884 device_unbind(dev);
885
Simon Glass39e42be2022-07-30 15:52:13 -0600886 ut_assert(ofnode_is_enabled(node));
887 ut_assertok(ofnode_set_enabled(node, false));
888 ut_assert(!ofnode_is_enabled(node));
Simon Glass65715592022-07-30 15:52:09 -0600889
890 return 0;
891}
Simon Glassb7eaa4f2022-07-30 15:52:11 -0600892DM_TEST(dm_test_ofnode_livetree_writing,
Simon Glass2b90e0d2022-09-06 20:27:07 -0600893 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass55f79902022-07-30 15:52:14 -0600894
Simon Glass0b58eaa2022-09-06 20:27:32 -0600895static 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 */
927static 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}
936DM_TEST(dm_test_ofnode_write_copy, UT_TESTF_SCAN_FDT);
937
Simon Glass9bf78a52023-09-26 08:14:39 -0600938/* test writing a property to the 'other' tree */
Simon Glass0b58eaa2022-09-06 20:27:32 -0600939static 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 Glass3a1fc172023-09-26 08:14:38 -0600953DM_TEST(dm_test_ofnode_write_copy_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass0b58eaa2022-09-06 20:27:32 -0600954
Simon Glass9bf78a52023-09-26 08:14:39 -0600955/* test ofnode_read_u32_index/default() */
Simon Glass55f79902022-07-30 15:52:14 -0600956static int dm_test_ofnode_u32(struct unit_test_state *uts)
957{
958 ofnode node;
Simon Glass66d0d0c2022-09-06 20:27:18 -0600959 u32 val;
Simon Glass55f79902022-07-30 15:52:14 -0600960
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 Glass66d0d0c2022-09-06 20:27:18 -0600968 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 Glass55f79902022-07-30 15:52:14 -0600979 return 0;
980}
Simon Glass2b90e0d2022-09-06 20:27:07 -0600981DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassffe90392022-09-06 20:27:02 -0600982
Simon Glass9bf78a52023-09-26 08:14:39 -0600983/* test ofnode_read_u32_array() */
Simon Glass66d0d0c2022-09-06 20:27:18 -0600984static 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}
1008DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1009
Simon Glass9bf78a52023-09-26 08:14:39 -06001010static int dm_test_ofnode_read_u64(struct unit_test_state *uts)
Simon Glass66d0d0c2022-09-06 20:27:18 -06001011{
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 Simekfa12dfa2023-08-25 11:37:46 +02001021 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 Glass66d0d0c2022-09-06 20:27:18 -06001029 return 0;
1030}
Simon Glass9bf78a52023-09-26 08:14:39 -06001031DM_TEST(dm_test_ofnode_read_u64, UT_TESTF_SCAN_FDT);
Simon Glass66d0d0c2022-09-06 20:27:18 -06001032
Simon Glassffe90392022-09-06 20:27:02 -06001033static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
1034{
1035 ofnode node, check, subnode;
1036 char buf[128];
1037
Simon Glassffe90392022-09-06 20:27:02 -06001038 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 Glassc3a194d2022-09-06 20:27:03 -06001095 /* write to the empty node */
1096 ut_assertok(ofnode_write_string(subnode, "example", "text"));
1097
Simon Glassffe90392022-09-06 20:27:02 -06001098 return 0;
1099}
Simon Glass2b90e0d2022-09-06 20:27:07 -06001100DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass52ad21a2022-09-06 20:27:16 -06001101
1102static 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 Sankouski298ffdd2023-01-22 18:21:23 +03001108 node = ofnode_path("/ofnode-foreach");
Simon Glass52ad21a2022-09-06 20:27:16 -06001109 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}
1124DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
Simon Glass66d0d0c2022-09-06 20:27:18 -06001125
1126static 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}
1140DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
1141
Simon Glass9bf78a52023-09-26 08:14:39 -06001142/* check ofnode_by_compatible() on the 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -06001143static 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 Glass3a1fc172023-09-26 08:14:38 -06001158DM_TEST(dm_test_ofnode_by_compatible_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -06001159
Simon Glass66d0d0c2022-09-06 20:27:18 -06001160static 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}
1175DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
1176
Simon Glass9bf78a52023-09-26 08:14:39 -06001177/* test ofnode_find_subnode() on the 'other' tree */
Simon Glass47a677c2022-09-06 20:27:30 -06001178static 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}
1194DM_TEST(dm_test_ofnode_find_subnode_ot, UT_TESTF_OTHER_FDT);
1195
Simon Glass66d0d0c2022-09-06 20:27:18 -06001196static 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}
1207DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -06001208
1209/* try to access more FDTs than is supported */
1210static 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}
1244DM_TEST(dm_test_ofnode_too_many, UT_TESTF_SCAN_FDT);
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001245
Simon Glass24797092023-09-26 08:14:37 -06001246static int check_copy_props(struct unit_test_state *uts, ofnode dst, ofnode src)
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001247{
1248 u32 reg[2], val;
1249
Simon Glass24797092023-09-26 08:14:37 -06001250 ut_assertok(ofnode_copy_props(dst, src));
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001251
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
1269static 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 Glass24797092023-09-26 08:14:37 -06001282 ut_assertok(check_copy_props(uts, dst, src));
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001283
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}
1289DM_TEST(dm_test_ofnode_copy_props, UT_TESTF_SCAN_FDT);
1290
Simon Glass9bf78a52023-09-26 08:14:39 -06001291/* test ofnode_copy_props() with the 'other' tree */
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001292static 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 Glass24797092023-09-26 08:14:37 -06001299 ut_assertok(check_copy_props(uts, dst, src));
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001300
1301 return 0;
1302}
1303DM_TEST(dm_test_ofnode_copy_props_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass9cf39bb2023-06-01 10:22:40 -06001304
1305/* check that the livetree is aligned to a structure boundary */
1306static 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 Glass3a1fc172023-09-26 08:14:38 -06001329DM_TEST(dm_test_livetree_align, UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_TREE);
Simon Glassa8f2ac22023-06-01 10:22:42 -06001330
1331/* check that it is possible to load an arbitrary livetree */
1332static 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 Glass3a1fc172023-09-26 08:14:38 -06001348DM_TEST(dm_test_livetree_ensure, UT_TESTF_SCAN_FDT);
Simon Glasse0c3c212023-09-26 08:14:40 -06001349
1350static 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}
1364DM_TEST(dm_test_oftree_new, UT_TESTF_SCAN_FDT);
Simon Glassc15862f2023-09-26 08:14:41 -06001365
1366static int check_copy_node(struct unit_test_state *uts, ofnode dst, ofnode src,
1367 ofnode *nodep)
1368{
1369 u32 reg[2], val;
1370 ofnode node;
1371
1372 ut_assertok(ofnode_copy_node(dst, "copy-test", src, &node));
1373
1374 ut_assertok(ofnode_read_u32(node, "ping-expect", &val));
1375 ut_asserteq(3, val);
1376
1377 ut_asserteq_str("denx,u-boot-fdt-test",
1378 ofnode_read_string(node, "compatible"));
1379
1380 /* check that a property with the same name is overwritten */
1381 ut_assertok(ofnode_read_u32_array(node, "reg", reg, ARRAY_SIZE(reg)));
1382 ut_asserteq(3, reg[0]);
1383 ut_asserteq(1, reg[1]);
1384
1385 /* reset the compatible so the live tree does not change */
1386 ut_assertok(ofnode_write_string(node, "compatible", "nothing"));
1387 *nodep = node;
1388
1389 return 0;
1390}
1391
1392static int dm_test_ofnode_copy_node(struct unit_test_state *uts)
1393{
1394 ofnode src, dst, node, try;
1395
1396 /*
1397 * These nodes are chosen so that the src node is before the destination
1398 * node in the tree. This doesn't matter with livetree, but with
1399 * flattree any attempt to insert a property earlier in the tree will
1400 * mess up the offsets after it.
1401 */
1402 src = ofnode_path("/b-test");
1403 dst = ofnode_path("/some-bus");
1404
1405 ut_assertok(check_copy_node(uts, dst, src, &node));
1406
1407 /* check trying to copy over an existing node */
1408 ut_asserteq(-EEXIST, ofnode_copy_node(dst, "copy-test", src, &try));
1409 ut_asserteq(try.of_offset, node.of_offset);
1410
1411 return 0;
1412}
1413DM_TEST(dm_test_ofnode_copy_node, UT_TESTF_SCAN_FDT);
1414
1415/* test ofnode_copy_node() with the 'other' tree */
1416static int dm_test_ofnode_copy_node_ot(struct unit_test_state *uts)
1417{
1418 oftree otree = get_other_oftree(uts);
1419 ofnode src, dst, node;
1420
1421 src = ofnode_path("/b-test");
1422 dst = oftree_path(otree, "/node/subnode2");
1423 ut_assertok(check_copy_node(uts, dst, src, &node));
1424
1425 return 0;
1426}
1427DM_TEST(dm_test_ofnode_copy_node_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);