blob: 6f21d66a95ef0aac5fcb4f9b7747d27b95f74db0 [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
Masahiro Yamada04ca8712018-04-27 01:02:02 +090094static int dm_test_ofnode_compatible(struct unit_test_state *uts)
95{
96 ofnode root_node = ofnode_path("/");
97
98 ut_assert(ofnode_valid(root_node));
99 ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
100
101 return 0;
102}
Simon Glass47a677c2022-09-06 20:27:30 -0600103DM_TEST(dm_test_ofnode_compatible,
104 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
105
106/* check ofnode_device_is_compatible() with the 'other' FDT */
107static int dm_test_ofnode_compatible_ot(struct unit_test_state *uts)
108{
109 oftree otree = get_other_oftree(uts);
110 ofnode oroot = oftree_root(otree);
111
112 ut_assert(ofnode_valid(oroot));
113 ut_assert(ofnode_device_is_compatible(oroot, "sandbox-other"));
114
115 return 0;
116}
Simon Glass3a1fc172023-09-26 08:14:38 -0600117DM_TEST(dm_test_ofnode_compatible_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200118
Patrick Delaunay6d9949f2020-09-24 17:26:20 +0200119static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
120{
121 /* test invalid phandle */
122 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
123 ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
124
125 /* test first valid phandle */
126 ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
127
128 /* test unknown phandle */
129 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
130
Simon Glass928d2672022-09-06 20:27:22 -0600131 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
132
Patrick Delaunay6d9949f2020-09-24 17:26:20 +0200133 return 0;
134}
135DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
136
Simon Glass5e969252022-09-06 20:27:27 -0600137static int dm_test_ofnode_get_by_phandle_ot(struct unit_test_state *uts)
138{
139 oftree otree = get_other_oftree(uts);
140 ofnode node;
141
142 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
143 node = oftree_get_by_phandle(otree, 1);
144 ut_assert(ofnode_valid(node));
145 ut_asserteq_str("target", ofnode_get_name(node));
146
147 return 0;
148}
Simon Glass3a1fc172023-09-26 08:14:38 -0600149DM_TEST(dm_test_ofnode_get_by_phandle_ot,
150 UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass5e969252022-09-06 20:27:27 -0600151
Simon Glass47a677c2022-09-06 20:27:30 -0600152static int check_prop_values(struct unit_test_state *uts, ofnode start,
153 const char *propname, const char *propval,
154 int expect_count)
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200155{
Simon Glass47a677c2022-09-06 20:27:30 -0600156 int proplen = strlen(propval) + 1;
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200157 const char *str;
Simon Glass47a677c2022-09-06 20:27:30 -0600158 ofnode node;
159 int count;
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200160
161 /* Find first matching node, there should be at least one */
Simon Glass47a677c2022-09-06 20:27:30 -0600162 node = ofnode_by_prop_value(start, propname, propval, proplen);
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200163 ut_assert(ofnode_valid(node));
164 str = ofnode_read_string(node, propname);
165 ut_assert(str && !strcmp(str, propval));
166
167 /* Find the rest of the matching nodes */
Simon Glass47a677c2022-09-06 20:27:30 -0600168 count = 1;
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200169 while (true) {
Simon Glass47a677c2022-09-06 20:27:30 -0600170 node = ofnode_by_prop_value(node, propname, propval, proplen);
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200171 if (!ofnode_valid(node))
172 break;
173 str = ofnode_read_string(node, propname);
Simon Glass47a677c2022-09-06 20:27:30 -0600174 ut_asserteq_str(propval, str);
175 count++;
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200176 }
Simon Glass47a677c2022-09-06 20:27:30 -0600177 ut_asserteq(expect_count, count);
178
179 return 0;
180}
181
182static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
183{
184 ut_assertok(check_prop_values(uts, ofnode_null(), "compatible",
185 "denx,u-boot-fdt-test", 11));
Jens Wiklander9bc7e962018-08-20 11:10:00 +0200186
187 return 0;
188}
Simon Glasse180c2b2020-07-28 19:41:12 -0600189DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
Simon Glasse6c5c942018-10-01 12:22:08 -0600190
Simon Glass47a677c2022-09-06 20:27:30 -0600191static int dm_test_ofnode_by_prop_value_ot(struct unit_test_state *uts)
192{
193 oftree otree = get_other_oftree(uts);
194
195 ut_assertok(check_prop_values(uts, oftree_root(otree), "str-prop",
196 "other", 2));
197
198 return 0;
199}
Simon Glass3a1fc172023-09-26 08:14:38 -0600200DM_TEST(dm_test_ofnode_by_prop_value_ot,
201 UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600202
Simon Glasse6c5c942018-10-01 12:22:08 -0600203static int dm_test_ofnode_fmap(struct unit_test_state *uts)
204{
205 struct fmap_entry entry;
206 ofnode node;
207
208 node = ofnode_path("/cros-ec/flash");
209 ut_assert(ofnode_valid(node));
210 ut_assertok(ofnode_read_fmap_entry(node, &entry));
211 ut_asserteq(0x08000000, entry.offset);
212 ut_asserteq(0x20000, entry.length);
213
214 return 0;
215}
Simon Glasse180c2b2020-07-28 19:41:12 -0600216DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass14ca9f72020-01-27 08:49:43 -0700217
Simon Glassa8167d82020-01-27 08:49:44 -0700218static int dm_test_ofnode_read(struct unit_test_state *uts)
219{
220 const u32 *val;
221 ofnode node;
222 int size;
223
224 node = ofnode_path("/a-test");
225 ut_assert(ofnode_valid(node));
226
227 val = ofnode_read_prop(node, "int-value", &size);
228 ut_assertnonnull(val);
229 ut_asserteq(4, size);
230 ut_asserteq(1234, fdt32_to_cpu(val[0]));
231
232 val = ofnode_read_prop(node, "missing", &size);
233 ut_assertnull(val);
234 ut_asserteq(-FDT_ERR_NOTFOUND, size);
235
236 /* Check it works without a size parameter */
237 val = ofnode_read_prop(node, "missing", NULL);
238 ut_assertnull(val);
239
240 return 0;
241}
Simon Glass3a1fc172023-09-26 08:14:38 -0600242DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_FDT);
Simon Glassa8167d82020-01-27 08:49:44 -0700243
Simon Glass47a677c2022-09-06 20:27:30 -0600244static int dm_test_ofnode_read_ot(struct unit_test_state *uts)
245{
246 oftree otree = get_other_oftree(uts);
247 const char *val;
248 ofnode node;
249 int size;
250
251 node = oftree_path(otree, "/node/subnode");
252 ut_assert(ofnode_valid(node));
253
254 val = ofnode_read_prop(node, "str-prop", &size);
255 ut_assertnonnull(val);
256 ut_asserteq_str("other", val);
257 ut_asserteq(6, size);
258
259 return 0;
260}
Simon Glass3a1fc172023-09-26 08:14:38 -0600261DM_TEST(dm_test_ofnode_read_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600262
Patrick Delaunaycc72f3e2020-09-25 09:41:16 +0200263static int dm_test_ofnode_phandle(struct unit_test_state *uts)
264{
265 struct ofnode_phandle_args args;
266 ofnode node;
267 int ret;
268 const char prop[] = "test-gpios";
269 const char cell[] = "#gpio-cells";
270 const char prop2[] = "phandle-value";
271
272 node = ofnode_path("/a-test");
273 ut_assert(ofnode_valid(node));
274
275 /* Test ofnode_count_phandle_with_args with cell name */
276 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
277 ut_asserteq(-ENOENT, ret);
278 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
279 ut_asserteq(-EINVAL, ret);
280 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
281 ut_asserteq(5, ret);
282
283 /* Test ofnode_parse_phandle_with_args with cell name */
284 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
285 &args);
286 ut_asserteq(-ENOENT, ret);
287 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
288 &args);
289 ut_asserteq(-EINVAL, ret);
290 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
291 ut_assertok(ret);
292 ut_asserteq(1, args.args_count);
293 ut_asserteq(1, args.args[0]);
294 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
295 ut_assertok(ret);
296 ut_asserteq(1, args.args_count);
297 ut_asserteq(4, args.args[0]);
298 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
299 ut_assertok(ret);
300 ut_asserteq(5, args.args_count);
301 ut_asserteq(5, args.args[0]);
302 ut_asserteq(1, args.args[4]);
303 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
304 ut_asserteq(-ENOENT, ret);
305 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
306 ut_assertok(ret);
307 ut_asserteq(1, args.args_count);
308 ut_asserteq(12, args.args[0]);
309 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
310 ut_asserteq(-ENOENT, ret);
311
312 /* Test ofnode_count_phandle_with_args with cell count */
313 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
314 ut_asserteq(-ENOENT, ret);
315 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
316 ut_asserteq(3, ret);
317
318 /* Test ofnode_parse_phandle_with_args with cell count */
319 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
320 ut_assertok(ret);
321 ut_asserteq(1, ofnode_valid(args.node));
322 ut_asserteq(1, args.args_count);
323 ut_asserteq(10, args.args[0]);
324 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
325 ut_asserteq(-EINVAL, ret);
326 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
327 ut_assertok(ret);
328 ut_asserteq(1, ofnode_valid(args.node));
329 ut_asserteq(1, args.args_count);
330 ut_asserteq(30, args.args[0]);
331 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
332 ut_asserteq(-ENOENT, ret);
333
334 return 0;
335}
336DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
337
Simon Glass5e969252022-09-06 20:27:27 -0600338static int dm_test_ofnode_phandle_ot(struct unit_test_state *uts)
339{
340 oftree otree = get_other_oftree(uts);
341 struct ofnode_phandle_args args;
342 ofnode node;
343 int ret;
344
345 node = oftree_path(otree, "/node");
346
347 /* Test ofnode_count_phandle_with_args with cell name */
348 ret = ofnode_count_phandle_with_args(node, "missing", "#gpio-cells", 0);
349 ut_asserteq(-ENOENT, ret);
350 ret = ofnode_count_phandle_with_args(node, "target", "#invalid", 0);
351 ut_asserteq(-EINVAL, ret);
352 ret = ofnode_count_phandle_with_args(node, "target", "#gpio-cells", 0);
353 ut_asserteq(1, ret);
354
355 ret = ofnode_parse_phandle_with_args(node, "target", "#gpio-cells", 0,
356 0, &args);
357 ut_assertok(ret);
358 ut_asserteq(2, args.args_count);
359 ut_asserteq(3, args.args[0]);
360 ut_asserteq(4, args.args[1]);
361
362 return 0;
363}
364DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT);
365
Simon Glass14ca9f72020-01-27 08:49:43 -0700366static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
367{
368 const char *str;
Simon Glassbd933bf2020-01-27 08:49:46 -0700369 const u32 *val;
Simon Glass14ca9f72020-01-27 08:49:43 -0700370 ofnode node;
Simon Glassbd933bf2020-01-27 08:49:46 -0700371 int size;
Simon Glass14ca9f72020-01-27 08:49:43 -0700372
373 str = ofnode_read_chosen_string("setting");
374 ut_assertnonnull(str);
375 ut_asserteq_str("sunrise ohoka", str);
376 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
377
378 node = ofnode_get_chosen_node("other-node");
379 ut_assert(ofnode_valid(node));
380 ut_asserteq_str("c-test@5", ofnode_get_name(node));
381
382 node = ofnode_get_chosen_node("setting");
383 ut_assert(!ofnode_valid(node));
384
Simon Glassbd933bf2020-01-27 08:49:46 -0700385 val = ofnode_read_chosen_prop("int-values", &size);
386 ut_assertnonnull(val);
387 ut_asserteq(8, size);
388 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
389 ut_asserteq(72993, fdt32_to_cpu(val[1]));
390
Simon Glass14ca9f72020-01-27 08:49:43 -0700391 return 0;
392}
Simon Glasse180c2b2020-07-28 19:41:12 -0600393DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chunfeng Yunbf6ad912020-05-02 11:35:10 +0200394
Michal Simek305d3182020-07-28 12:51:08 +0200395static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
396{
397 const void *val;
398 ofnode node;
399 int size;
400
Michael Walle82a3c9e2021-02-25 16:51:11 +0100401 node = ofnode_get_aliases_node("ethernet3");
Michal Simek305d3182020-07-28 12:51:08 +0200402 ut_assert(ofnode_valid(node));
403 ut_asserteq_str("sbe5", ofnode_get_name(node));
404
405 node = ofnode_get_aliases_node("unknown");
406 ut_assert(!ofnode_valid(node));
407
408 val = ofnode_read_aliases_prop("spi0", &size);
409 ut_assertnonnull(val);
410 ut_asserteq(7, size);
411 ut_asserteq_str("/spi@0", (const char *)val);
412
413 return 0;
414}
415DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
416
Chunfeng Yunbf6ad912020-05-02 11:35:10 +0200417static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
418{
419 ofnode node, child_node;
420 u32 val;
421
422 node = ofnode_path("/i-test");
423 ut_assert(ofnode_valid(node));
424
425 val = ofnode_get_child_count(node);
426 ut_asserteq(3, val);
427
428 child_node = ofnode_first_subnode(node);
429 ut_assert(ofnode_valid(child_node));
430 val = ofnode_get_child_count(child_node);
431 ut_asserteq(0, val);
432
433 return 0;
434}
435DM_TEST(dm_test_ofnode_get_child_count,
Simon Glasse180c2b2020-07-28 19:41:12 -0600436 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0de1b072020-11-28 17:50:02 -0700437
Simon Glass47a677c2022-09-06 20:27:30 -0600438static int dm_test_ofnode_get_child_count_ot(struct unit_test_state *uts)
439{
440 oftree otree = get_other_oftree(uts);
441 ofnode node, child_node;
442 u32 val;
443
444 node = oftree_path(otree, "/node");
445 ut_assert(ofnode_valid(node));
446
447 val = ofnode_get_child_count(node);
448 ut_asserteq(2, val);
449
450 child_node = ofnode_first_subnode(node);
451 ut_assert(ofnode_valid(child_node));
452 val = ofnode_get_child_count(child_node);
453 ut_asserteq(0, val);
454
455 return 0;
456}
Simon Glass3a1fc172023-09-26 08:14:38 -0600457DM_TEST(dm_test_ofnode_get_child_count_ot,
458 UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600459
Simon Glass0de1b072020-11-28 17:50:02 -0700460static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
461{
462 ofnode root_node = ofnode_path("/");
463 ofnode node = ofnode_path("/usb@0");
464
465 ut_assert(ofnode_is_enabled(root_node));
466 ut_assert(!ofnode_is_enabled(node));
467
468 return 0;
469}
470DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800471
Simon Glass47a677c2022-09-06 20:27:30 -0600472static int dm_test_ofnode_is_enabled_ot(struct unit_test_state *uts)
473{
474 oftree otree = get_other_oftree(uts);
475 ofnode root_node = oftree_root(otree);
476 ofnode node = oftree_path(otree, "/target");
477
478 ut_assert(ofnode_is_enabled(root_node));
479 ut_assert(!ofnode_is_enabled(node));
480
481 return 0;
482}
483DM_TEST(dm_test_ofnode_is_enabled_ot, UT_TESTF_OTHER_FDT);
484
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800485static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
486{
487 ofnode node;
488 fdt_addr_t addr;
489 fdt_size_t size;
490
491 node = ofnode_path("/translation-test@8000");
492 ut_assert(ofnode_valid(node));
493 addr = ofnode_get_addr(node);
494 size = ofnode_get_size(node);
495 ut_asserteq(0x8000, addr);
496 ut_asserteq(0x4000, size);
497
498 node = ofnode_path("/translation-test@8000/dev@1,100");
499 ut_assert(ofnode_valid(node));
500 addr = ofnode_get_addr(node);
501 size = ofnode_get_size(node);
502 ut_asserteq(0x9000, addr);
503 ut_asserteq(0x1000, size);
504
505 node = ofnode_path("/emul-mux-controller");
506 ut_assert(ofnode_valid(node));
507 addr = ofnode_get_addr(node);
508 size = ofnode_get_size(node);
Patrice Chotard9876ae72022-01-04 08:42:48 +0100509 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800510 ut_asserteq(FDT_SIZE_T_NONE, size);
511
Marek Behún31a7b712021-05-26 14:08:17 +0200512 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
513 ut_assert(ofnode_valid(node));
514 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
515 ut_asserteq_64(0x42, addr);
516
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800517 return 0;
518}
519DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Marek Behún0e116be2021-05-26 14:08:18 +0200520
Simon Glass47a677c2022-09-06 20:27:30 -0600521static int dm_test_ofnode_get_reg_ot(struct unit_test_state *uts)
522{
523 oftree otree = get_other_oftree(uts);
524 ofnode node = oftree_path(otree, "/target");
525 fdt_addr_t addr;
526
527 addr = ofnode_get_addr(node);
528 ut_asserteq(0x8000, addr);
529
530 return 0;
531}
Simon Glass3a1fc172023-09-26 08:14:38 -0600532DM_TEST(dm_test_ofnode_get_reg_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600533
Marek Behún0e116be2021-05-26 14:08:18 +0200534static int dm_test_ofnode_get_path(struct unit_test_state *uts)
535{
536 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
537 char buf[64];
538 ofnode node;
Marek Behún0e116be2021-05-26 14:08:18 +0200539
540 node = ofnode_path(path);
541 ut_assert(ofnode_valid(node));
542
Simon Glass47a677c2022-09-06 20:27:30 -0600543 ut_assertok(ofnode_get_path(node, buf, sizeof(buf)));
Marek Behún0e116be2021-05-26 14:08:18 +0200544 ut_asserteq_str(path, buf);
545
Simon Glass47a677c2022-09-06 20:27:30 -0600546 ut_asserteq(-ENOSPC, ofnode_get_path(node, buf, 32));
Marek Behún0e116be2021-05-26 14:08:18 +0200547
Simon Glass47a677c2022-09-06 20:27:30 -0600548 ut_assertok(ofnode_get_path(ofnode_root(), buf, 32));
Simon Glass66d0d0c2022-09-06 20:27:18 -0600549 ut_asserteq_str("/", buf);
550
Marek Behún0e116be2021-05-26 14:08:18 +0200551 return 0;
552}
553DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7de8bd02021-08-07 07:24:01 -0600554
Simon Glass47a677c2022-09-06 20:27:30 -0600555static int dm_test_ofnode_get_path_ot(struct unit_test_state *uts)
556{
557 oftree otree = get_other_oftree(uts);
558 const char *path = "/node/subnode";
559 ofnode node = oftree_path(otree, path);
560 char buf[64];
561
562 ut_assert(ofnode_valid(node));
563
564 ut_assertok(ofnode_get_path(node, buf, sizeof(buf)));
565 ut_asserteq_str(path, buf);
566
567 ut_assertok(ofnode_get_path(oftree_root(otree), buf, 32));
568 ut_asserteq_str("/", buf);
569
570 return 0;
571}
Simon Glass3a1fc172023-09-26 08:14:38 -0600572DM_TEST(dm_test_ofnode_get_path_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -0600573
Simon Glass7de8bd02021-08-07 07:24:01 -0600574static int dm_test_ofnode_conf(struct unit_test_state *uts)
575{
576 ut_assert(!ofnode_conf_read_bool("missing"));
577 ut_assert(ofnode_conf_read_bool("testing-bool"));
578
579 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
580 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
581
582 ut_assertnull(ofnode_conf_read_str("missing"));
583 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
584
585 return 0;
586}
Simon Glass3a1fc172023-09-26 08:14:38 -0600587DM_TEST(dm_test_ofnode_conf, UT_TESTF_SCAN_FDT);
Michael Wallebce039a2021-10-15 15:15:18 +0200588
Michal Simekdb5e3492023-08-31 08:59:05 +0200589static int dm_test_ofnode_options(struct unit_test_state *uts)
590{
Michal Simek44f35e12023-08-31 09:04:27 +0200591 u64 bootscr_address, bootscr_offset;
592 u64 bootscr_flash_offset, bootscr_flash_size;
Michal Simekdb5e3492023-08-31 08:59:05 +0200593
594 ut_assertok(ofnode_read_bootscript_address(&bootscr_address,
595 &bootscr_offset));
596 ut_asserteq_64(0, bootscr_address);
597 ut_asserteq_64(0x12345678, bootscr_offset);
598
Michal Simek44f35e12023-08-31 09:04:27 +0200599 ut_assertok(ofnode_read_bootscript_flash(&bootscr_flash_offset,
600 &bootscr_flash_size));
601 ut_asserteq_64(0, bootscr_flash_offset);
602 ut_asserteq_64(0x2000, bootscr_flash_size);
603
Michal Simekdb5e3492023-08-31 08:59:05 +0200604 return 0;
605}
606DM_TEST(dm_test_ofnode_options, 0);
607
Michael Wallebce039a2021-10-15 15:15:18 +0200608static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
609{
610 const char compatible[] = "denx,u-boot-fdt-test";
611 bool found = false;
612 ofnode node;
613
614 ofnode_for_each_compatible_node(node, compatible) {
615 ut_assert(ofnode_device_is_compatible(node, compatible));
616 found = true;
617 }
618
619 /* There should be at least one matching node */
620 ut_assert(found);
621
622 return 0;
623}
624DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
Simon Glassfb933d02021-10-23 17:26:04 -0600625
626static int dm_test_ofnode_string(struct unit_test_state *uts)
627{
Simon Glass075bfc92021-10-23 17:26:07 -0600628 const char **val;
Simon Glassfb933d02021-10-23 17:26:04 -0600629 const char *out;
630 ofnode node;
631
632 node = ofnode_path("/a-test");
633 ut_assert(ofnode_valid(node));
634
635 /* single string */
636 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
637 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
638 ut_asserteq_str("test string", out);
639 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
640 "test string"));
Simon Glass075bfc92021-10-23 17:26:07 -0600641 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
642 ut_asserteq_str("test string", val[0]);
643 ut_assertnull(val[1]);
644 free(val);
Simon Glassfb933d02021-10-23 17:26:04 -0600645
646 /* list of strings */
647 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
648 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
649 &out));
650 ut_asserteq_str("mux0", out);
651 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
652 "mux0"));
Simon Glass075bfc92021-10-23 17:26:07 -0600653 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
654 &val));
655 ut_asserteq_str("mux0", val[0]);
656 ut_asserteq_str("mux1", val[1]);
657 ut_asserteq_str("mux2", val[2]);
658 ut_asserteq_str("mux3", val[3]);
659 ut_asserteq_str("mux4", val[4]);
660 ut_assertnull(val[5]);
661 free(val);
Simon Glassfb933d02021-10-23 17:26:04 -0600662
663 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
664 &out));
665 ut_asserteq_str("mux4", out);
666 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
667 "mux4"));
668
669 return 0;
670}
Simon Glass3a1fc172023-09-26 08:14:38 -0600671DM_TEST(dm_test_ofnode_string, UT_TESTF_SCAN_FDT);
Simon Glassfb933d02021-10-23 17:26:04 -0600672
673static int dm_test_ofnode_string_err(struct unit_test_state *uts)
674{
Simon Glass075bfc92021-10-23 17:26:07 -0600675 const char **val;
Simon Glassfb933d02021-10-23 17:26:04 -0600676 const char *out;
677 ofnode node;
678
679 /*
680 * Test error codes only on livetree, as they are different with
681 * flattree
682 */
683 node = ofnode_path("/a-test");
684 ut_assert(ofnode_valid(node));
685
686 /* non-existent property */
687 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
688 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
689 &out));
Simon Glass075bfc92021-10-23 17:26:07 -0600690 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
Simon Glassfb933d02021-10-23 17:26:04 -0600691
692 /* empty property */
693 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
694 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
695 &out));
Simon Glass075bfc92021-10-23 17:26:07 -0600696 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
697 &val));
Simon Glassfb933d02021-10-23 17:26:04 -0600698
699 /* badly formatted string list */
700 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
701 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
702 &out));
Simon Glass075bfc92021-10-23 17:26:07 -0600703 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
704 &val));
Simon Glassfb933d02021-10-23 17:26:04 -0600705
706 /* out of range / not found */
707 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
708 &out));
709 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
710 "other"));
711
712 /* negative value for index is not allowed, so don't test for that */
713
714 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
715 "mux-control-names", 5,
716 &out));
717
718 return 0;
719}
720DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
Marek Behúnf3dd2132022-04-07 00:32:57 +0200721
722static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
723{
724 ofnode eth_node, phy_node;
Marek Behún123ca112022-04-07 00:33:01 +0200725 phy_interface_t mode;
Marek Behúnf3dd2132022-04-07 00:32:57 +0200726 u32 reg;
727
728 eth_node = ofnode_path("/phy-test-eth");
729 ut_assert(ofnode_valid(eth_node));
730
Marek Behún123ca112022-04-07 00:33:01 +0200731 mode = ofnode_read_phy_mode(eth_node);
732 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
733
Marek Behúnf3dd2132022-04-07 00:32:57 +0200734 phy_node = ofnode_get_phy_node(eth_node);
735 ut_assert(ofnode_valid(phy_node));
736
737 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
738 ut_asserteq_64(0x1, reg);
739
740 return 0;
741}
Simon Glass3a1fc172023-09-26 08:14:38 -0600742DM_TEST(dm_test_ofnode_get_phy, UT_TESTF_SCAN_FDT);
Simon Glass33104842022-07-30 15:52:08 -0600743
744/**
745 * make_ofnode_fdt() - Create an FDT for testing with ofnode
746 *
747 * The size is set to the minimum needed
748 *
749 * @uts: Test state
750 * @fdt: Place to write FDT
751 * @size: Maximum size of space for fdt
Simon Glass47a677c2022-09-06 20:27:30 -0600752 * @id: id value to add to the tree ('id' property in root node)
Simon Glass33104842022-07-30 15:52:08 -0600753 */
Simon Glass47a677c2022-09-06 20:27:30 -0600754static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size,
755 int id)
Simon Glass33104842022-07-30 15:52:08 -0600756{
757 ut_assertok(fdt_create(fdt, size));
758 ut_assertok(fdt_finish_reservemap(fdt));
759 ut_assert(fdt_begin_node(fdt, "") >= 0);
760
Simon Glass47a677c2022-09-06 20:27:30 -0600761 ut_assertok(fdt_property_u32(fdt, "id", id));
762
Simon Glass33104842022-07-30 15:52:08 -0600763 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
764 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
765 ut_assertok(fdt_end_node(fdt));
766
767 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
768 ut_assertok(fdt_end_node(fdt));
769
770 ut_assertok(fdt_end_node(fdt));
771 ut_assertok(fdt_finish(fdt));
772
773 return 0;
774}
775
776static int dm_test_ofnode_root(struct unit_test_state *uts)
777{
Simon Glass33104842022-07-30 15:52:08 -0600778 ofnode node;
779
780 /* Check that aliases work on the control FDT */
781 node = ofnode_get_aliases_node("ethernet3");
782 ut_assert(ofnode_valid(node));
783 ut_asserteq_str("sbe5", ofnode_get_name(node));
784
Simon Glass085d5942022-09-06 20:27:21 -0600785 ut_assert(!oftree_valid(oftree_null()));
786
Simon Glassc43635b2022-10-20 18:22:49 -0600787 return 0;
788}
789DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
790
791static int dm_test_ofnode_root_mult(struct unit_test_state *uts)
792{
793 char fdt[256];
794 oftree tree;
795 ofnode node;
796
797 /* skip this test if multiple FDTs are not supported */
798 if (!IS_ENABLED(CONFIG_OFNODE_MULTI_TREE))
799 return -EAGAIN;
800
Simon Glass47a677c2022-09-06 20:27:30 -0600801 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt), 0));
Simon Glassc43635b2022-10-20 18:22:49 -0600802 ut_assertok(get_oftree(uts, fdt, &tree));
Simon Glass085d5942022-09-06 20:27:21 -0600803 ut_assert(oftree_valid(tree));
Simon Glass33104842022-07-30 15:52:08 -0600804
805 /* Make sure they don't work on this new tree */
Simon Glassb7bd94f2022-09-06 20:27:24 -0600806 node = oftree_path(tree, "mmc0");
Simon Glass33104842022-07-30 15:52:08 -0600807 ut_assert(!ofnode_valid(node));
808
809 /* It should appear in the new tree */
Simon Glassb7bd94f2022-09-06 20:27:24 -0600810 node = oftree_path(tree, "/new-mmc");
Simon Glass33104842022-07-30 15:52:08 -0600811 ut_assert(ofnode_valid(node));
812
813 /* ...and not in the control FDT */
Simon Glassb7bd94f2022-09-06 20:27:24 -0600814 node = oftree_path(oftree_default(), "/new-mmc");
Simon Glass33104842022-07-30 15:52:08 -0600815 ut_assert(!ofnode_valid(node));
816
Simon Glass88a1ae82022-09-06 20:27:29 -0600817 free_oftree(tree);
Simon Glass33104842022-07-30 15:52:08 -0600818
819 return 0;
820}
Simon Glassc43635b2022-10-20 18:22:49 -0600821DM_TEST(dm_test_ofnode_root_mult, UT_TESTF_SCAN_FDT);
Simon Glass65715592022-07-30 15:52:09 -0600822
823static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
824{
825 struct udevice *dev;
826 ofnode node;
827
Simon Glass65715592022-07-30 15:52:09 -0600828 /* Test enabling devices */
Simon Glass65715592022-07-30 15:52:09 -0600829 node = ofnode_path("/usb@2");
830
Simon Glass39e42be2022-07-30 15:52:13 -0600831 ut_assert(!ofnode_is_enabled(node));
832 ut_assertok(ofnode_set_enabled(node, true));
833 ut_asserteq(true, ofnode_is_enabled(node));
Simon Glass65715592022-07-30 15:52:09 -0600834
835 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
836 &dev);
837 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
838
839 /* Test string property setting */
Simon Glass65715592022-07-30 15:52:09 -0600840 ut_assert(device_is_compatible(dev, "sandbox,usb"));
841 ofnode_write_string(node, "compatible", "gdsys,super-usb");
842 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
843 ofnode_write_string(node, "compatible", "sandbox,usb");
844 ut_assert(device_is_compatible(dev, "sandbox,usb"));
845
846 /* Test setting generic properties */
847
848 /* Non-existent in DTB */
849 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
850 /* reg = 0x42, size = 0x100 */
Simon Glassbe0789a2022-07-30 15:52:10 -0600851 ut_assertok(ofnode_write_prop(node, "reg",
Simon Glass0b58eaa2022-09-06 20:27:32 -0600852 "\x00\x00\x00\x42\x00\x00\x01\x00", 8,
853 false));
Simon Glass65715592022-07-30 15:52:09 -0600854 ut_asserteq(0x42, dev_read_addr(dev));
855
856 /* Test disabling devices */
Simon Glass65715592022-07-30 15:52:09 -0600857 device_remove(dev, DM_REMOVE_NORMAL);
858 device_unbind(dev);
859
Simon Glass39e42be2022-07-30 15:52:13 -0600860 ut_assert(ofnode_is_enabled(node));
861 ut_assertok(ofnode_set_enabled(node, false));
862 ut_assert(!ofnode_is_enabled(node));
Simon Glass65715592022-07-30 15:52:09 -0600863
864 return 0;
865}
Simon Glassb7eaa4f2022-07-30 15:52:11 -0600866DM_TEST(dm_test_ofnode_livetree_writing,
Simon Glass2b90e0d2022-09-06 20:27:07 -0600867 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass55f79902022-07-30 15:52:14 -0600868
Simon Glass0b58eaa2022-09-06 20:27:32 -0600869static int check_write_prop(struct unit_test_state *uts, ofnode node)
870{
871 char prop[] = "middle-name";
872 char name[10];
873 int len;
874
875 strcpy(name, "cecil");
876 len = strlen(name) + 1;
877 ut_assertok(ofnode_write_prop(node, prop, name, len, false));
878 ut_asserteq_str(name, ofnode_read_string(node, prop));
879
880 /* change the underlying value, this should mess up the live tree */
881 strcpy(name, "tony");
882 if (of_live_active()) {
883 ut_asserteq_str(name, ofnode_read_string(node, prop));
884 } else {
885 ut_asserteq_str("cecil", ofnode_read_string(node, prop));
886 }
887
888 /* try again, this time copying the property */
889 strcpy(name, "mary");
890 ut_assertok(ofnode_write_prop(node, prop, name, len, true));
891 ut_asserteq_str(name, ofnode_read_string(node, prop));
892 strcpy(name, "leah");
893
894 /* both flattree and livetree behave the same */
895 ut_asserteq_str("mary", ofnode_read_string(node, prop));
896
897 return 0;
898}
899
900/* writing the tree with and without copying the property */
901static int dm_test_ofnode_write_copy(struct unit_test_state *uts)
902{
903 ofnode node;
904
905 node = ofnode_path("/a-test");
906 ut_assertok(check_write_prop(uts, node));
907
908 return 0;
909}
910DM_TEST(dm_test_ofnode_write_copy, UT_TESTF_SCAN_FDT);
911
912static int dm_test_ofnode_write_copy_ot(struct unit_test_state *uts)
913{
914 oftree otree = get_other_oftree(uts);
915 ofnode node, check_node;
916
917 node = oftree_path(otree, "/node");
918 ut_assertok(check_write_prop(uts, node));
919
920 /* make sure the control FDT is not touched */
921 check_node = ofnode_path("/node");
922 ut_assertnull(ofnode_read_string(check_node, "middle-name"));
923
924 return 0;
925}
Simon Glass3a1fc172023-09-26 08:14:38 -0600926DM_TEST(dm_test_ofnode_write_copy_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass0b58eaa2022-09-06 20:27:32 -0600927
Simon Glass55f79902022-07-30 15:52:14 -0600928static int dm_test_ofnode_u32(struct unit_test_state *uts)
929{
930 ofnode node;
Simon Glass66d0d0c2022-09-06 20:27:18 -0600931 u32 val;
Simon Glass55f79902022-07-30 15:52:14 -0600932
933 node = ofnode_path("/lcd");
934 ut_assert(ofnode_valid(node));
935 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
936 ut_assertok(ofnode_write_u32(node, "xres", 1367));
937 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
938 ut_assertok(ofnode_write_u32(node, "xres", 1366));
939
Simon Glass66d0d0c2022-09-06 20:27:18 -0600940 node = ofnode_path("/backlight");
941 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
942 ut_asserteq(0, val);
943 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
944 ut_asserteq(16, val);
945 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
946 ut_asserteq(255, val);
947 ut_asserteq(-EOVERFLOW,
948 ofnode_read_u32_index(node, "brightness-levels", 9, &val));
949 ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
950
Simon Glass55f79902022-07-30 15:52:14 -0600951 return 0;
952}
Simon Glass2b90e0d2022-09-06 20:27:07 -0600953DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassffe90392022-09-06 20:27:02 -0600954
Simon Glass66d0d0c2022-09-06 20:27:18 -0600955static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
956{
957 ofnode node;
958 u32 val[10];
959
960 node = ofnode_path("/a-test");
961 ut_assert(ofnode_valid(node));
962 ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
963 ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
964 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
965 1));
966
967 memset(val, '\0', sizeof(val));
968 ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
969 ut_asserteq(0, val[0]);
970 ut_asserteq(5678, val[1]);
971 ut_asserteq(9123, val[2]);
972 ut_asserteq(4567, val[3]);
973 ut_asserteq(0, val[4]);
974 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
975 4));
976
977 return 0;
978}
979DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
980
981static int dm_test_ofnode_u64(struct unit_test_state *uts)
982{
983 ofnode node;
984 u64 val;
985
986 node = ofnode_path("/a-test");
987 ut_assert(ofnode_valid(node));
988 ut_assertok(ofnode_read_u64(node, "int64-value", &val));
989 ut_asserteq_64(0x1111222233334444, val);
990 ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
991
Michal Simekfa12dfa2023-08-25 11:37:46 +0200992 ut_assertok(ofnode_read_u64_index(node, "int64-array", 0, &val));
993 ut_asserteq_64(0x1111222233334444, val);
994 ut_assertok(ofnode_read_u64_index(node, "int64-array", 1, &val));
995 ut_asserteq_64(0x4444333322221111, val);
996 ut_asserteq(-EOVERFLOW,
997 ofnode_read_u64_index(node, "int64-array", 2, &val));
998 ut_asserteq(-EINVAL, ofnode_read_u64_index(node, "missing", 0, &val));
999
Simon Glass66d0d0c2022-09-06 20:27:18 -06001000 return 0;
1001}
1002DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
1003
Simon Glassffe90392022-09-06 20:27:02 -06001004static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
1005{
1006 ofnode node, check, subnode;
1007 char buf[128];
1008
Simon Glassffe90392022-09-06 20:27:02 -06001009 node = ofnode_path("/lcd");
1010 ut_assert(ofnode_valid(node));
1011 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
1012 check = ofnode_path("/lcd/edmund");
1013 ut_asserteq(subnode.of_offset, check.of_offset);
1014 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
1015 ut_asserteq_str("/lcd/edmund", buf);
1016
1017 if (of_live_active()) {
1018 struct device_node *child;
1019
1020 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
1021 2, &child));
1022 ut_asserteq_str("ed", child->name);
1023 ut_asserteq_str("/lcd/ed", child->full_name);
1024 check = ofnode_path("/lcd/ed");
1025 ut_asserteq_ptr(child, check.np);
1026 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
1027 sizeof(buf)));
1028 ut_asserteq_str("/lcd/ed", buf);
1029 }
1030
1031 /* An existing node should be returned with -EEXIST */
1032 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
1033 ut_asserteq(subnode.of_offset, check.of_offset);
1034
1035 /* add a root node */
1036 node = ofnode_path("/");
1037 ut_assert(ofnode_valid(node));
1038 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
1039 check = ofnode_path("/lcd2");
1040 ut_asserteq(subnode.of_offset, check.of_offset);
1041 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
1042 ut_asserteq_str("/lcd2", buf);
1043
1044 if (of_live_active()) {
1045 ulong start;
1046 int i;
1047
1048 /*
1049 * Make sure each of the three malloc()checks in
1050 * of_add_subnode() work
1051 */
1052 for (i = 0; i < 3; i++) {
1053 malloc_enable_testing(i);
1054 start = ut_check_free();
1055 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
1056 &check));
1057 ut_assertok(ut_check_delta(start));
1058 }
1059
1060 /* This should pass since we allow 3 allocations */
1061 malloc_enable_testing(3);
1062 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
1063 malloc_disable_testing();
1064 }
1065
Simon Glassc3a194d2022-09-06 20:27:03 -06001066 /* write to the empty node */
1067 ut_assertok(ofnode_write_string(subnode, "example", "text"));
1068
Simon Glassffe90392022-09-06 20:27:02 -06001069 return 0;
1070}
Simon Glass2b90e0d2022-09-06 20:27:07 -06001071DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass52ad21a2022-09-06 20:27:16 -06001072
1073static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
1074{
1075 ofnode node, subnode;
1076 struct ofprop prop;
1077 int count;
1078
Dzmitry Sankouski298ffdd2023-01-22 18:21:23 +03001079 node = ofnode_path("/ofnode-foreach");
Simon Glass52ad21a2022-09-06 20:27:16 -06001080 count = 0;
1081
1082 /* we expect "compatible" for each node */
1083 ofnode_for_each_prop(prop, node)
1084 count++;
1085 ut_asserteq(1, count);
1086
1087 /* there are two nodes, each with 2 properties */
1088 ofnode_for_each_subnode(subnode, node)
1089 ofnode_for_each_prop(prop, subnode)
1090 count++;
1091 ut_asserteq(5, count);
1092
1093 return 0;
1094}
1095DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
Simon Glass66d0d0c2022-09-06 20:27:18 -06001096
1097static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
1098{
1099 const char *compat = "denx,u-boot-fdt-test";
1100 ofnode node;
1101 int count;
1102
1103 count = 0;
1104 for (node = ofnode_null();
1105 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
1106 count++;
1107 ut_asserteq(11, count);
1108
1109 return 0;
1110}
1111DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
1112
Simon Glass47a677c2022-09-06 20:27:30 -06001113static int dm_test_ofnode_by_compatible_ot(struct unit_test_state *uts)
1114{
1115 const char *compat = "sandbox-other2";
1116 oftree otree = get_other_oftree(uts);
1117 ofnode node;
1118 int count;
1119
1120 count = 0;
1121 for (node = oftree_root(otree);
1122 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
1123 count++;
1124 ut_asserteq(2, count);
1125
1126 return 0;
1127}
Simon Glass3a1fc172023-09-26 08:14:38 -06001128DM_TEST(dm_test_ofnode_by_compatible_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -06001129
Simon Glass66d0d0c2022-09-06 20:27:18 -06001130static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
1131{
1132 ofnode node, subnode;
1133
1134 node = ofnode_path("/buttons");
1135
1136 subnode = ofnode_find_subnode(node, "btn1");
1137 ut_assert(ofnode_valid(subnode));
1138 ut_asserteq_str("btn1", ofnode_get_name(subnode));
1139
1140 subnode = ofnode_find_subnode(node, "btn");
1141 ut_assert(!ofnode_valid(subnode));
1142
1143 return 0;
1144}
1145DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
1146
Simon Glass47a677c2022-09-06 20:27:30 -06001147static int dm_test_ofnode_find_subnode_ot(struct unit_test_state *uts)
1148{
1149 oftree otree = get_other_oftree(uts);
1150 ofnode node, subnode;
1151
1152 node = oftree_path(otree, "/node");
1153
1154 subnode = ofnode_find_subnode(node, "subnode");
1155 ut_assert(ofnode_valid(subnode));
1156 ut_asserteq_str("subnode", ofnode_get_name(subnode));
1157
1158 subnode = ofnode_find_subnode(node, "btn");
1159 ut_assert(!ofnode_valid(subnode));
1160
1161 return 0;
1162}
1163DM_TEST(dm_test_ofnode_find_subnode_ot, UT_TESTF_OTHER_FDT);
1164
Simon Glass66d0d0c2022-09-06 20:27:18 -06001165static int dm_test_ofnode_get_name(struct unit_test_state *uts)
1166{
1167 ofnode node;
1168
1169 node = ofnode_path("/buttons");
1170 ut_assert(ofnode_valid(node));
1171 ut_asserteq_str("buttons", ofnode_get_name(node));
1172 ut_asserteq_str("", ofnode_get_name(ofnode_root()));
1173
1174 return 0;
1175}
1176DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);
Simon Glass47a677c2022-09-06 20:27:30 -06001177
1178/* try to access more FDTs than is supported */
1179static int dm_test_ofnode_too_many(struct unit_test_state *uts)
1180{
1181 const int max_trees = CONFIG_IS_ENABLED(OFNODE_MULTI_TREE,
1182 (CONFIG_OFNODE_MULTI_TREE_MAX), (1));
1183 const int fdt_size = 256;
1184 const int num_trees = max_trees + 1;
1185 char fdt[num_trees][fdt_size];
1186 int i;
1187
1188 for (i = 0; i < num_trees; i++) {
1189 oftree tree;
1190 int ret;
1191
1192 ut_assertok(make_ofnode_fdt(uts, fdt[i], fdt_size, i));
1193 ret = get_oftree(uts, fdt[i], &tree);
1194
1195 /*
1196 * With flat tree we have the control FDT using one slot. Live
1197 * tree has no limit since it uses pointers, not integer tree
1198 * IDs
1199 */
1200 if (of_live_active() || i < max_trees - 1) {
1201 ut_assertok(ret);
1202 } else {
1203 /*
1204 * tree should be invalid when we try to register too
1205 * many trees
1206 */
1207 ut_asserteq(-EOVERFLOW, ret);
1208 }
1209 }
1210
1211 return 0;
1212}
1213DM_TEST(dm_test_ofnode_too_many, UT_TESTF_SCAN_FDT);
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001214
Simon Glass24797092023-09-26 08:14:37 -06001215static int check_copy_props(struct unit_test_state *uts, ofnode dst, ofnode src)
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001216{
1217 u32 reg[2], val;
1218
Simon Glass24797092023-09-26 08:14:37 -06001219 ut_assertok(ofnode_copy_props(dst, src));
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001220
1221 ut_assertok(ofnode_read_u32(dst, "ping-expect", &val));
1222 ut_asserteq(3, val);
1223
1224 ut_asserteq_str("denx,u-boot-fdt-test",
1225 ofnode_read_string(dst, "compatible"));
1226
1227 /* check that a property with the same name is overwritten */
1228 ut_assertok(ofnode_read_u32_array(dst, "reg", reg, ARRAY_SIZE(reg)));
1229 ut_asserteq(3, reg[0]);
1230 ut_asserteq(1, reg[1]);
1231
1232 /* reset the compatible so the live tree does not change */
1233 ut_assertok(ofnode_write_string(dst, "compatible", "nothing"));
1234
1235 return 0;
1236}
1237
1238static int dm_test_ofnode_copy_props(struct unit_test_state *uts)
1239{
1240 ofnode src, dst;
1241
1242 /*
1243 * These nodes are chosen so that the src node is before the destination
1244 * node in the tree. This doesn't matter with livetree, but with
1245 * flattree any attempt to insert a property earlier in the tree will
1246 * mess up the offsets after it.
1247 */
1248 src = ofnode_path("/b-test");
1249 dst = ofnode_path("/some-bus");
1250
Simon Glass24797092023-09-26 08:14:37 -06001251 ut_assertok(check_copy_props(uts, dst, src));
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001252
1253 /* check a property that is in the destination already */
1254 ut_asserteq_str("mux0", ofnode_read_string(dst, "mux-control-names"));
1255
1256 return 0;
1257}
1258DM_TEST(dm_test_ofnode_copy_props, UT_TESTF_SCAN_FDT);
1259
1260static int dm_test_ofnode_copy_props_ot(struct unit_test_state *uts)
1261{
1262 ofnode src, dst;
1263 oftree otree = get_other_oftree(uts);
1264
1265 src = ofnode_path("/b-test");
1266 dst = oftree_path(otree, "/node/subnode2");
Simon Glass24797092023-09-26 08:14:37 -06001267 ut_assertok(check_copy_props(uts, dst, src));
Simon Glassdb1ef1e2022-09-06 20:27:33 -06001268
1269 return 0;
1270}
1271DM_TEST(dm_test_ofnode_copy_props_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glass9cf39bb2023-06-01 10:22:40 -06001272
1273/* check that the livetree is aligned to a structure boundary */
1274static int dm_test_livetree_align(struct unit_test_state *uts)
1275{
1276 const int align = __alignof__(struct unit_test_state);
1277 struct device_node *node;
1278 u32 *sentinel;
1279 ulong start;
1280
1281 start = (ulong)gd_of_root();
1282 ut_asserteq(start, ALIGN(start, align));
1283
1284 node = gd_of_root();
1285 sentinel = (void *)node - sizeof(u32);
1286
1287 /*
1288 * The sentinel should be overwritten with the root node. If it isn't,
1289 * then the root node is not at the very start of the livetree memory
1290 * area, and free(root) will fail to free the memory used by the
1291 * livetree.
1292 */
1293 ut_assert(*sentinel != BAD_OF_ROOT);
1294
1295 return 0;
1296}
Simon Glass3a1fc172023-09-26 08:14:38 -06001297DM_TEST(dm_test_livetree_align, UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_TREE);
Simon Glassa8f2ac22023-06-01 10:22:42 -06001298
1299/* check that it is possible to load an arbitrary livetree */
1300static int dm_test_livetree_ensure(struct unit_test_state *uts)
1301{
1302 oftree tree;
1303 ofnode node;
1304
1305 /* read from other.dtb */
1306 ut_assertok(test_load_other_fdt(uts));
1307 tree = oftree_from_fdt(uts->other_fdt);
1308 ut_assert(oftree_valid(tree));
1309 node = oftree_path(tree, "/node/subnode");
1310 ut_assert(ofnode_valid(node));
1311 ut_asserteq_str("sandbox-other2",
1312 ofnode_read_string(node, "compatible"));
1313
1314 return 0;
1315}
Simon Glass3a1fc172023-09-26 08:14:38 -06001316DM_TEST(dm_test_livetree_ensure, UT_TESTF_SCAN_FDT);