blob: dee71ee5e545f27dd9ef852bd70a5a489784e09f [file] [log] [blame]
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +09001// SPDX-License-Identifier: GPL-2.0+
Simon Glassf912a722022-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 Yamadab4f2dda2018-04-27 01:02:02 +090018
19#include <common.h>
20#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060021#include <log.h>
Simon Glassef75c592022-07-30 15:52:08 -060022#include <of_live.h>
Simon Glass270a4432022-07-30 15:52:09 -060023#include <dm/device-internal.h>
24#include <dm/lists.h>
Simon Glass699c9ca2018-10-01 12:22:08 -060025#include <dm/of_extra.h>
Simon Glass270a4432022-07-30 15:52:09 -060026#include <dm/root.h>
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +090027#include <dm/test.h>
Simon Glass270a4432022-07-30 15:52:09 -060028#include <dm/uclass-internal.h>
Simon Glass75c4d412020-07-19 10:15:37 -060029#include <test/test.h>
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +090030#include <test/ut.h>
31
Simon Glassf912a722022-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 Glassdad97512022-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 Yamadab4f2dda2018-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 Glass9e7a42a2022-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}
117DM_TEST(dm_test_ofnode_compatible_ot, UT_TESTF_OTHER_FDT);
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200118
Patrick Delaunay04fcfe72020-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 Glass95fd2092022-09-06 20:27:22 -0600131 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
132
Patrick Delaunay04fcfe72020-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 Glassf912a722022-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}
149DM_TEST(dm_test_ofnode_get_by_phandle_ot, UT_TESTF_OTHER_FDT);
150
Simon Glass9e7a42a2022-09-06 20:27:30 -0600151static int check_prop_values(struct unit_test_state *uts, ofnode start,
152 const char *propname, const char *propval,
153 int expect_count)
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200154{
Simon Glass9e7a42a2022-09-06 20:27:30 -0600155 int proplen = strlen(propval) + 1;
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200156 const char *str;
Simon Glass9e7a42a2022-09-06 20:27:30 -0600157 ofnode node;
158 int count;
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200159
160 /* Find first matching node, there should be at least one */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600161 node = ofnode_by_prop_value(start, propname, propval, proplen);
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200162 ut_assert(ofnode_valid(node));
163 str = ofnode_read_string(node, propname);
164 ut_assert(str && !strcmp(str, propval));
165
166 /* Find the rest of the matching nodes */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600167 count = 1;
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200168 while (true) {
Simon Glass9e7a42a2022-09-06 20:27:30 -0600169 node = ofnode_by_prop_value(node, propname, propval, proplen);
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200170 if (!ofnode_valid(node))
171 break;
172 str = ofnode_read_string(node, propname);
Simon Glass9e7a42a2022-09-06 20:27:30 -0600173 ut_asserteq_str(propval, str);
174 count++;
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200175 }
Simon Glass9e7a42a2022-09-06 20:27:30 -0600176 ut_asserteq(expect_count, count);
177
178 return 0;
179}
180
181static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
182{
183 ut_assertok(check_prop_values(uts, ofnode_null(), "compatible",
184 "denx,u-boot-fdt-test", 11));
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +0200185
186 return 0;
187}
Simon Glass974dccd2020-07-28 19:41:12 -0600188DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
Simon Glass699c9ca2018-10-01 12:22:08 -0600189
Simon Glass9e7a42a2022-09-06 20:27:30 -0600190static int dm_test_ofnode_by_prop_value_ot(struct unit_test_state *uts)
191{
192 oftree otree = get_other_oftree(uts);
193
194 ut_assertok(check_prop_values(uts, oftree_root(otree), "str-prop",
195 "other", 2));
196
197 return 0;
198}
199DM_TEST(dm_test_ofnode_by_prop_value_ot, UT_TESTF_OTHER_FDT);
200
Simon Glass699c9ca2018-10-01 12:22:08 -0600201static int dm_test_ofnode_fmap(struct unit_test_state *uts)
202{
203 struct fmap_entry entry;
204 ofnode node;
205
206 node = ofnode_path("/cros-ec/flash");
207 ut_assert(ofnode_valid(node));
208 ut_assertok(ofnode_read_fmap_entry(node, &entry));
209 ut_asserteq(0x08000000, entry.offset);
210 ut_asserteq(0x20000, entry.length);
211
212 return 0;
213}
Simon Glass974dccd2020-07-28 19:41:12 -0600214DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf3455962020-01-27 08:49:43 -0700215
Simon Glass0c2e9802020-01-27 08:49:44 -0700216static int dm_test_ofnode_read(struct unit_test_state *uts)
217{
218 const u32 *val;
219 ofnode node;
220 int size;
221
222 node = ofnode_path("/a-test");
223 ut_assert(ofnode_valid(node));
224
225 val = ofnode_read_prop(node, "int-value", &size);
226 ut_assertnonnull(val);
227 ut_asserteq(4, size);
228 ut_asserteq(1234, fdt32_to_cpu(val[0]));
229
230 val = ofnode_read_prop(node, "missing", &size);
231 ut_assertnull(val);
232 ut_asserteq(-FDT_ERR_NOTFOUND, size);
233
234 /* Check it works without a size parameter */
235 val = ofnode_read_prop(node, "missing", NULL);
236 ut_assertnull(val);
237
238 return 0;
239}
Simon Glass974dccd2020-07-28 19:41:12 -0600240DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0c2e9802020-01-27 08:49:44 -0700241
Simon Glass9e7a42a2022-09-06 20:27:30 -0600242static int dm_test_ofnode_read_ot(struct unit_test_state *uts)
243{
244 oftree otree = get_other_oftree(uts);
245 const char *val;
246 ofnode node;
247 int size;
248
249 node = oftree_path(otree, "/node/subnode");
250 ut_assert(ofnode_valid(node));
251
252 val = ofnode_read_prop(node, "str-prop", &size);
253 ut_assertnonnull(val);
254 ut_asserteq_str("other", val);
255 ut_asserteq(6, size);
256
257 return 0;
258}
259DM_TEST(dm_test_ofnode_read_ot, UT_TESTF_OTHER_FDT);
260
Patrick Delaunay8cd28012020-09-25 09:41:16 +0200261static int dm_test_ofnode_phandle(struct unit_test_state *uts)
262{
263 struct ofnode_phandle_args args;
264 ofnode node;
265 int ret;
266 const char prop[] = "test-gpios";
267 const char cell[] = "#gpio-cells";
268 const char prop2[] = "phandle-value";
269
270 node = ofnode_path("/a-test");
271 ut_assert(ofnode_valid(node));
272
273 /* Test ofnode_count_phandle_with_args with cell name */
274 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
275 ut_asserteq(-ENOENT, ret);
276 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
277 ut_asserteq(-EINVAL, ret);
278 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
279 ut_asserteq(5, ret);
280
281 /* Test ofnode_parse_phandle_with_args with cell name */
282 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
283 &args);
284 ut_asserteq(-ENOENT, ret);
285 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
286 &args);
287 ut_asserteq(-EINVAL, ret);
288 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
289 ut_assertok(ret);
290 ut_asserteq(1, args.args_count);
291 ut_asserteq(1, args.args[0]);
292 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
293 ut_assertok(ret);
294 ut_asserteq(1, args.args_count);
295 ut_asserteq(4, args.args[0]);
296 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
297 ut_assertok(ret);
298 ut_asserteq(5, args.args_count);
299 ut_asserteq(5, args.args[0]);
300 ut_asserteq(1, args.args[4]);
301 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
302 ut_asserteq(-ENOENT, ret);
303 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
304 ut_assertok(ret);
305 ut_asserteq(1, args.args_count);
306 ut_asserteq(12, args.args[0]);
307 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
308 ut_asserteq(-ENOENT, ret);
309
310 /* Test ofnode_count_phandle_with_args with cell count */
311 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
312 ut_asserteq(-ENOENT, ret);
313 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
314 ut_asserteq(3, ret);
315
316 /* Test ofnode_parse_phandle_with_args with cell count */
317 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
318 ut_assertok(ret);
319 ut_asserteq(1, ofnode_valid(args.node));
320 ut_asserteq(1, args.args_count);
321 ut_asserteq(10, args.args[0]);
322 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
323 ut_asserteq(-EINVAL, ret);
324 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
325 ut_assertok(ret);
326 ut_asserteq(1, ofnode_valid(args.node));
327 ut_asserteq(1, args.args_count);
328 ut_asserteq(30, args.args[0]);
329 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
330 ut_asserteq(-ENOENT, ret);
331
332 return 0;
333}
334DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
335
Simon Glassf912a722022-09-06 20:27:27 -0600336static int dm_test_ofnode_phandle_ot(struct unit_test_state *uts)
337{
338 oftree otree = get_other_oftree(uts);
339 struct ofnode_phandle_args args;
340 ofnode node;
341 int ret;
342
343 node = oftree_path(otree, "/node");
344
345 /* Test ofnode_count_phandle_with_args with cell name */
346 ret = ofnode_count_phandle_with_args(node, "missing", "#gpio-cells", 0);
347 ut_asserteq(-ENOENT, ret);
348 ret = ofnode_count_phandle_with_args(node, "target", "#invalid", 0);
349 ut_asserteq(-EINVAL, ret);
350 ret = ofnode_count_phandle_with_args(node, "target", "#gpio-cells", 0);
351 ut_asserteq(1, ret);
352
353 ret = ofnode_parse_phandle_with_args(node, "target", "#gpio-cells", 0,
354 0, &args);
355 ut_assertok(ret);
356 ut_asserteq(2, args.args_count);
357 ut_asserteq(3, args.args[0]);
358 ut_asserteq(4, args.args[1]);
359
360 return 0;
361}
362DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT);
363
Simon Glassf3455962020-01-27 08:49:43 -0700364static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
365{
366 const char *str;
Simon Glasse09223c2020-01-27 08:49:46 -0700367 const u32 *val;
Simon Glassf3455962020-01-27 08:49:43 -0700368 ofnode node;
Simon Glasse09223c2020-01-27 08:49:46 -0700369 int size;
Simon Glassf3455962020-01-27 08:49:43 -0700370
371 str = ofnode_read_chosen_string("setting");
372 ut_assertnonnull(str);
373 ut_asserteq_str("sunrise ohoka", str);
374 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
375
376 node = ofnode_get_chosen_node("other-node");
377 ut_assert(ofnode_valid(node));
378 ut_asserteq_str("c-test@5", ofnode_get_name(node));
379
380 node = ofnode_get_chosen_node("setting");
381 ut_assert(!ofnode_valid(node));
382
Simon Glasse09223c2020-01-27 08:49:46 -0700383 val = ofnode_read_chosen_prop("int-values", &size);
384 ut_assertnonnull(val);
385 ut_asserteq(8, size);
386 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
387 ut_asserteq(72993, fdt32_to_cpu(val[1]));
388
Simon Glassf3455962020-01-27 08:49:43 -0700389 return 0;
390}
Simon Glass974dccd2020-07-28 19:41:12 -0600391DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
developercf8bc132020-05-02 11:35:10 +0200392
Michal Simek92a88622020-07-28 12:51:08 +0200393static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
394{
395 const void *val;
396 ofnode node;
397 int size;
398
Michael Walle7efcdfd2021-02-25 16:51:11 +0100399 node = ofnode_get_aliases_node("ethernet3");
Michal Simek92a88622020-07-28 12:51:08 +0200400 ut_assert(ofnode_valid(node));
401 ut_asserteq_str("sbe5", ofnode_get_name(node));
402
403 node = ofnode_get_aliases_node("unknown");
404 ut_assert(!ofnode_valid(node));
405
406 val = ofnode_read_aliases_prop("spi0", &size);
407 ut_assertnonnull(val);
408 ut_asserteq(7, size);
409 ut_asserteq_str("/spi@0", (const char *)val);
410
411 return 0;
412}
413DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
414
developercf8bc132020-05-02 11:35:10 +0200415static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
416{
417 ofnode node, child_node;
418 u32 val;
419
420 node = ofnode_path("/i-test");
421 ut_assert(ofnode_valid(node));
422
423 val = ofnode_get_child_count(node);
424 ut_asserteq(3, val);
425
426 child_node = ofnode_first_subnode(node);
427 ut_assert(ofnode_valid(child_node));
428 val = ofnode_get_child_count(child_node);
429 ut_asserteq(0, val);
430
431 return 0;
432}
433DM_TEST(dm_test_ofnode_get_child_count,
Simon Glass974dccd2020-07-28 19:41:12 -0600434 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass5de5b3b2020-11-28 17:50:02 -0700435
Simon Glass9e7a42a2022-09-06 20:27:30 -0600436static int dm_test_ofnode_get_child_count_ot(struct unit_test_state *uts)
437{
438 oftree otree = get_other_oftree(uts);
439 ofnode node, child_node;
440 u32 val;
441
442 node = oftree_path(otree, "/node");
443 ut_assert(ofnode_valid(node));
444
445 val = ofnode_get_child_count(node);
446 ut_asserteq(2, val);
447
448 child_node = ofnode_first_subnode(node);
449 ut_assert(ofnode_valid(child_node));
450 val = ofnode_get_child_count(child_node);
451 ut_asserteq(0, val);
452
453 return 0;
454}
455DM_TEST(dm_test_ofnode_get_child_count_ot, UT_TESTF_OTHER_FDT);
456
Simon Glass5de5b3b2020-11-28 17:50:02 -0700457static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
458{
459 ofnode root_node = ofnode_path("/");
460 ofnode node = ofnode_path("/usb@0");
461
462 ut_assert(ofnode_is_enabled(root_node));
463 ut_assert(!ofnode_is_enabled(node));
464
465 return 0;
466}
467DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800468
Simon Glass9e7a42a2022-09-06 20:27:30 -0600469static int dm_test_ofnode_is_enabled_ot(struct unit_test_state *uts)
470{
471 oftree otree = get_other_oftree(uts);
472 ofnode root_node = oftree_root(otree);
473 ofnode node = oftree_path(otree, "/target");
474
475 ut_assert(ofnode_is_enabled(root_node));
476 ut_assert(!ofnode_is_enabled(node));
477
478 return 0;
479}
480DM_TEST(dm_test_ofnode_is_enabled_ot, UT_TESTF_OTHER_FDT);
481
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800482static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
483{
484 ofnode node;
485 fdt_addr_t addr;
486 fdt_size_t size;
487
488 node = ofnode_path("/translation-test@8000");
489 ut_assert(ofnode_valid(node));
490 addr = ofnode_get_addr(node);
491 size = ofnode_get_size(node);
492 ut_asserteq(0x8000, addr);
493 ut_asserteq(0x4000, size);
494
495 node = ofnode_path("/translation-test@8000/dev@1,100");
496 ut_assert(ofnode_valid(node));
497 addr = ofnode_get_addr(node);
498 size = ofnode_get_size(node);
499 ut_asserteq(0x9000, addr);
500 ut_asserteq(0x1000, size);
501
502 node = ofnode_path("/emul-mux-controller");
503 ut_assert(ofnode_valid(node));
504 addr = ofnode_get_addr(node);
505 size = ofnode_get_size(node);
Patrice Chotardb4c52112022-01-04 08:42:48 +0100506 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800507 ut_asserteq(FDT_SIZE_T_NONE, size);
508
Marek Behún177ab7f2021-05-26 14:08:17 +0200509 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
510 ut_assert(ofnode_valid(node));
511 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
512 ut_asserteq_64(0x42, addr);
513
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800514 return 0;
515}
516DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Marek Behúne897e3c2021-05-26 14:08:18 +0200517
Simon Glass9e7a42a2022-09-06 20:27:30 -0600518static int dm_test_ofnode_get_reg_ot(struct unit_test_state *uts)
519{
520 oftree otree = get_other_oftree(uts);
521 ofnode node = oftree_path(otree, "/target");
522 fdt_addr_t addr;
523
524 addr = ofnode_get_addr(node);
525 ut_asserteq(0x8000, addr);
526
527 return 0;
528}
529DM_TEST(dm_test_ofnode_get_reg_ot, UT_TESTF_OTHER_FDT);
530
Marek Behúne897e3c2021-05-26 14:08:18 +0200531static int dm_test_ofnode_get_path(struct unit_test_state *uts)
532{
533 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
534 char buf[64];
535 ofnode node;
Marek Behúne897e3c2021-05-26 14:08:18 +0200536
537 node = ofnode_path(path);
538 ut_assert(ofnode_valid(node));
539
Simon Glass9e7a42a2022-09-06 20:27:30 -0600540 ut_assertok(ofnode_get_path(node, buf, sizeof(buf)));
Marek Behúne897e3c2021-05-26 14:08:18 +0200541 ut_asserteq_str(path, buf);
542
Simon Glass9e7a42a2022-09-06 20:27:30 -0600543 ut_asserteq(-ENOSPC, ofnode_get_path(node, buf, 32));
Marek Behúne897e3c2021-05-26 14:08:18 +0200544
Simon Glass9e7a42a2022-09-06 20:27:30 -0600545 ut_assertok(ofnode_get_path(ofnode_root(), buf, 32));
Simon Glasse3be5fc2022-09-06 20:27:18 -0600546 ut_asserteq_str("/", buf);
547
Marek Behúne897e3c2021-05-26 14:08:18 +0200548 return 0;
549}
550DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0034d962021-08-07 07:24:01 -0600551
Simon Glass9e7a42a2022-09-06 20:27:30 -0600552static int dm_test_ofnode_get_path_ot(struct unit_test_state *uts)
553{
554 oftree otree = get_other_oftree(uts);
555 const char *path = "/node/subnode";
556 ofnode node = oftree_path(otree, path);
557 char buf[64];
558
559 ut_assert(ofnode_valid(node));
560
561 ut_assertok(ofnode_get_path(node, buf, sizeof(buf)));
562 ut_asserteq_str(path, buf);
563
564 ut_assertok(ofnode_get_path(oftree_root(otree), buf, 32));
565 ut_asserteq_str("/", buf);
566
567 return 0;
568}
569DM_TEST(dm_test_ofnode_get_path_ot, UT_TESTF_OTHER_FDT);
570
Simon Glass0034d962021-08-07 07:24:01 -0600571static int dm_test_ofnode_conf(struct unit_test_state *uts)
572{
573 ut_assert(!ofnode_conf_read_bool("missing"));
574 ut_assert(ofnode_conf_read_bool("testing-bool"));
575
576 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
577 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
578
579 ut_assertnull(ofnode_conf_read_str("missing"));
580 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
581
582 return 0;
583}
584DM_TEST(dm_test_ofnode_conf, 0);
Michael Wallef1f87402021-10-15 15:15:18 +0200585
Michal Simek43c42bd2023-08-31 08:59:05 +0200586static int dm_test_ofnode_options(struct unit_test_state *uts)
587{
Michal Simek6a7c1ce2023-08-31 09:04:27 +0200588 u64 bootscr_address, bootscr_offset;
589 u64 bootscr_flash_offset, bootscr_flash_size;
Michal Simek43c42bd2023-08-31 08:59:05 +0200590
591 ut_assertok(ofnode_read_bootscript_address(&bootscr_address,
592 &bootscr_offset));
593 ut_asserteq_64(0, bootscr_address);
594 ut_asserteq_64(0x12345678, bootscr_offset);
595
Michal Simek6a7c1ce2023-08-31 09:04:27 +0200596 ut_assertok(ofnode_read_bootscript_flash(&bootscr_flash_offset,
597 &bootscr_flash_size));
598 ut_asserteq_64(0, bootscr_flash_offset);
599 ut_asserteq_64(0x2000, bootscr_flash_size);
600
Michal Simek43c42bd2023-08-31 08:59:05 +0200601 return 0;
602}
603DM_TEST(dm_test_ofnode_options, 0);
604
Michael Wallef1f87402021-10-15 15:15:18 +0200605static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
606{
607 const char compatible[] = "denx,u-boot-fdt-test";
608 bool found = false;
609 ofnode node;
610
611 ofnode_for_each_compatible_node(node, compatible) {
612 ut_assert(ofnode_device_is_compatible(node, compatible));
613 found = true;
614 }
615
616 /* There should be at least one matching node */
617 ut_assert(found);
618
619 return 0;
620}
621DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
Simon Glass73025392021-10-23 17:26:04 -0600622
623static int dm_test_ofnode_string(struct unit_test_state *uts)
624{
Simon Glass9580bfc2021-10-23 17:26:07 -0600625 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600626 const char *out;
627 ofnode node;
628
629 node = ofnode_path("/a-test");
630 ut_assert(ofnode_valid(node));
631
632 /* single string */
633 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
634 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
635 ut_asserteq_str("test string", out);
636 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
637 "test string"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600638 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
639 ut_asserteq_str("test string", val[0]);
640 ut_assertnull(val[1]);
641 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600642
643 /* list of strings */
644 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
645 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
646 &out));
647 ut_asserteq_str("mux0", out);
648 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
649 "mux0"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600650 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
651 &val));
652 ut_asserteq_str("mux0", val[0]);
653 ut_asserteq_str("mux1", val[1]);
654 ut_asserteq_str("mux2", val[2]);
655 ut_asserteq_str("mux3", val[3]);
656 ut_asserteq_str("mux4", val[4]);
657 ut_assertnull(val[5]);
658 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600659
660 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
661 &out));
662 ut_asserteq_str("mux4", out);
663 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
664 "mux4"));
665
666 return 0;
667}
668DM_TEST(dm_test_ofnode_string, 0);
669
670static int dm_test_ofnode_string_err(struct unit_test_state *uts)
671{
Simon Glass9580bfc2021-10-23 17:26:07 -0600672 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600673 const char *out;
674 ofnode node;
675
676 /*
677 * Test error codes only on livetree, as they are different with
678 * flattree
679 */
680 node = ofnode_path("/a-test");
681 ut_assert(ofnode_valid(node));
682
683 /* non-existent property */
684 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
685 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
686 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600687 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
Simon Glass73025392021-10-23 17:26:04 -0600688
689 /* empty property */
690 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
691 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
692 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600693 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
694 &val));
Simon Glass73025392021-10-23 17:26:04 -0600695
696 /* badly formatted string list */
697 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
698 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
699 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600700 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
701 &val));
Simon Glass73025392021-10-23 17:26:04 -0600702
703 /* out of range / not found */
704 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
705 &out));
706 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
707 "other"));
708
709 /* negative value for index is not allowed, so don't test for that */
710
711 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
712 "mux-control-names", 5,
713 &out));
714
715 return 0;
716}
717DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200718
719static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
720{
721 ofnode eth_node, phy_node;
Marek Behúnbc194772022-04-07 00:33:01 +0200722 phy_interface_t mode;
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200723 u32 reg;
724
725 eth_node = ofnode_path("/phy-test-eth");
726 ut_assert(ofnode_valid(eth_node));
727
Marek Behúnbc194772022-04-07 00:33:01 +0200728 mode = ofnode_read_phy_mode(eth_node);
729 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
730
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200731 phy_node = ofnode_get_phy_node(eth_node);
732 ut_assert(ofnode_valid(phy_node));
733
734 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
735 ut_asserteq_64(0x1, reg);
736
737 return 0;
738}
739DM_TEST(dm_test_ofnode_get_phy, 0);
Simon Glassef75c592022-07-30 15:52:08 -0600740
741/**
742 * make_ofnode_fdt() - Create an FDT for testing with ofnode
743 *
744 * The size is set to the minimum needed
745 *
746 * @uts: Test state
747 * @fdt: Place to write FDT
748 * @size: Maximum size of space for fdt
Simon Glass9e7a42a2022-09-06 20:27:30 -0600749 * @id: id value to add to the tree ('id' property in root node)
Simon Glassef75c592022-07-30 15:52:08 -0600750 */
Simon Glass9e7a42a2022-09-06 20:27:30 -0600751static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size,
752 int id)
Simon Glassef75c592022-07-30 15:52:08 -0600753{
754 ut_assertok(fdt_create(fdt, size));
755 ut_assertok(fdt_finish_reservemap(fdt));
756 ut_assert(fdt_begin_node(fdt, "") >= 0);
757
Simon Glass9e7a42a2022-09-06 20:27:30 -0600758 ut_assertok(fdt_property_u32(fdt, "id", id));
759
Simon Glassef75c592022-07-30 15:52:08 -0600760 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
761 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
762 ut_assertok(fdt_end_node(fdt));
763
764 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
765 ut_assertok(fdt_end_node(fdt));
766
767 ut_assertok(fdt_end_node(fdt));
768 ut_assertok(fdt_finish(fdt));
769
770 return 0;
771}
772
773static int dm_test_ofnode_root(struct unit_test_state *uts)
774{
Simon Glassef75c592022-07-30 15:52:08 -0600775 ofnode node;
776
777 /* Check that aliases work on the control FDT */
778 node = ofnode_get_aliases_node("ethernet3");
779 ut_assert(ofnode_valid(node));
780 ut_asserteq_str("sbe5", ofnode_get_name(node));
781
Simon Glass2b9b14582022-09-06 20:27:21 -0600782 ut_assert(!oftree_valid(oftree_null()));
783
Simon Glassc7599442022-10-20 18:22:49 -0600784 return 0;
785}
786DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
Simon Glassdad97512022-09-06 20:27:29 -0600787
Simon Glassc7599442022-10-20 18:22:49 -0600788static int dm_test_ofnode_root_mult(struct unit_test_state *uts)
789{
790 char fdt[256];
791 oftree tree;
792 ofnode node;
Simon Glassdad97512022-09-06 20:27:29 -0600793
Simon Glassc7599442022-10-20 18:22:49 -0600794 /* skip this test if multiple FDTs are not supported */
795 if (!IS_ENABLED(CONFIG_OFNODE_MULTI_TREE))
796 return -EAGAIN;
797
798 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt), 0));
799 ut_assertok(get_oftree(uts, fdt, &tree));
Simon Glass2b9b14582022-09-06 20:27:21 -0600800 ut_assert(oftree_valid(tree));
Simon Glassef75c592022-07-30 15:52:08 -0600801
802 /* Make sure they don't work on this new tree */
Simon Glass45ae59d2022-09-06 20:27:24 -0600803 node = oftree_path(tree, "mmc0");
Simon Glassef75c592022-07-30 15:52:08 -0600804 ut_assert(!ofnode_valid(node));
805
806 /* It should appear in the new tree */
Simon Glass45ae59d2022-09-06 20:27:24 -0600807 node = oftree_path(tree, "/new-mmc");
Simon Glassef75c592022-07-30 15:52:08 -0600808 ut_assert(ofnode_valid(node));
809
810 /* ...and not in the control FDT */
Simon Glass45ae59d2022-09-06 20:27:24 -0600811 node = oftree_path(oftree_default(), "/new-mmc");
Simon Glassef75c592022-07-30 15:52:08 -0600812 ut_assert(!ofnode_valid(node));
813
Simon Glassdad97512022-09-06 20:27:29 -0600814 free_oftree(tree);
Simon Glassef75c592022-07-30 15:52:08 -0600815
816 return 0;
817}
Simon Glassc7599442022-10-20 18:22:49 -0600818DM_TEST(dm_test_ofnode_root_mult, UT_TESTF_SCAN_FDT);
Simon Glass270a4432022-07-30 15:52:09 -0600819
820static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
821{
822 struct udevice *dev;
823 ofnode node;
824
Simon Glass270a4432022-07-30 15:52:09 -0600825 /* Test enabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600826 node = ofnode_path("/usb@2");
827
Simon Glass3ee3d152022-07-30 15:52:13 -0600828 ut_assert(!ofnode_is_enabled(node));
829 ut_assertok(ofnode_set_enabled(node, true));
830 ut_asserteq(true, ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600831
832 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
833 &dev);
834 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
835
836 /* Test string property setting */
Simon Glass270a4432022-07-30 15:52:09 -0600837 ut_assert(device_is_compatible(dev, "sandbox,usb"));
838 ofnode_write_string(node, "compatible", "gdsys,super-usb");
839 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
840 ofnode_write_string(node, "compatible", "sandbox,usb");
841 ut_assert(device_is_compatible(dev, "sandbox,usb"));
842
843 /* Test setting generic properties */
844
845 /* Non-existent in DTB */
846 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
847 /* reg = 0x42, size = 0x100 */
Simon Glass5e2cd5e2022-07-30 15:52:10 -0600848 ut_assertok(ofnode_write_prop(node, "reg",
Simon Glass17abed02022-09-06 20:27:32 -0600849 "\x00\x00\x00\x42\x00\x00\x01\x00", 8,
850 false));
Simon Glass270a4432022-07-30 15:52:09 -0600851 ut_asserteq(0x42, dev_read_addr(dev));
852
853 /* Test disabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600854 device_remove(dev, DM_REMOVE_NORMAL);
855 device_unbind(dev);
856
Simon Glass3ee3d152022-07-30 15:52:13 -0600857 ut_assert(ofnode_is_enabled(node));
858 ut_assertok(ofnode_set_enabled(node, false));
859 ut_assert(!ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600860
861 return 0;
862}
Simon Glassb75dc162022-07-30 15:52:11 -0600863DM_TEST(dm_test_ofnode_livetree_writing,
Simon Glass8ae9f962022-09-06 20:27:07 -0600864 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd28e31e2022-07-30 15:52:14 -0600865
Simon Glass17abed02022-09-06 20:27:32 -0600866static int check_write_prop(struct unit_test_state *uts, ofnode node)
867{
868 char prop[] = "middle-name";
869 char name[10];
870 int len;
871
872 strcpy(name, "cecil");
873 len = strlen(name) + 1;
874 ut_assertok(ofnode_write_prop(node, prop, name, len, false));
875 ut_asserteq_str(name, ofnode_read_string(node, prop));
876
877 /* change the underlying value, this should mess up the live tree */
878 strcpy(name, "tony");
879 if (of_live_active()) {
880 ut_asserteq_str(name, ofnode_read_string(node, prop));
881 } else {
882 ut_asserteq_str("cecil", ofnode_read_string(node, prop));
883 }
884
885 /* try again, this time copying the property */
886 strcpy(name, "mary");
887 ut_assertok(ofnode_write_prop(node, prop, name, len, true));
888 ut_asserteq_str(name, ofnode_read_string(node, prop));
889 strcpy(name, "leah");
890
891 /* both flattree and livetree behave the same */
892 ut_asserteq_str("mary", ofnode_read_string(node, prop));
893
894 return 0;
895}
896
897/* writing the tree with and without copying the property */
898static int dm_test_ofnode_write_copy(struct unit_test_state *uts)
899{
900 ofnode node;
901
902 node = ofnode_path("/a-test");
903 ut_assertok(check_write_prop(uts, node));
904
905 return 0;
906}
907DM_TEST(dm_test_ofnode_write_copy, UT_TESTF_SCAN_FDT);
908
909static int dm_test_ofnode_write_copy_ot(struct unit_test_state *uts)
910{
911 oftree otree = get_other_oftree(uts);
912 ofnode node, check_node;
913
914 node = oftree_path(otree, "/node");
915 ut_assertok(check_write_prop(uts, node));
916
917 /* make sure the control FDT is not touched */
918 check_node = ofnode_path("/node");
919 ut_assertnull(ofnode_read_string(check_node, "middle-name"));
920
921 return 0;
922}
923DM_TEST(dm_test_ofnode_write_copy_ot, UT_TESTF_OTHER_FDT);
924
Simon Glassd28e31e2022-07-30 15:52:14 -0600925static int dm_test_ofnode_u32(struct unit_test_state *uts)
926{
927 ofnode node;
Simon Glasse3be5fc2022-09-06 20:27:18 -0600928 u32 val;
Simon Glassd28e31e2022-07-30 15:52:14 -0600929
930 node = ofnode_path("/lcd");
931 ut_assert(ofnode_valid(node));
932 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
933 ut_assertok(ofnode_write_u32(node, "xres", 1367));
934 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
935 ut_assertok(ofnode_write_u32(node, "xres", 1366));
936
Simon Glasse3be5fc2022-09-06 20:27:18 -0600937 node = ofnode_path("/backlight");
938 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
939 ut_asserteq(0, val);
940 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
941 ut_asserteq(16, val);
942 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
943 ut_asserteq(255, val);
944 ut_asserteq(-EOVERFLOW,
945 ofnode_read_u32_index(node, "brightness-levels", 9, &val));
946 ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
947
Simon Glassd28e31e2022-07-30 15:52:14 -0600948 return 0;
949}
Simon Glass8ae9f962022-09-06 20:27:07 -0600950DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass56bc3322022-09-06 20:27:02 -0600951
Simon Glasse3be5fc2022-09-06 20:27:18 -0600952static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
953{
954 ofnode node;
955 u32 val[10];
956
957 node = ofnode_path("/a-test");
958 ut_assert(ofnode_valid(node));
959 ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
960 ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
961 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
962 1));
963
964 memset(val, '\0', sizeof(val));
965 ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
966 ut_asserteq(0, val[0]);
967 ut_asserteq(5678, val[1]);
968 ut_asserteq(9123, val[2]);
969 ut_asserteq(4567, val[3]);
970 ut_asserteq(0, val[4]);
971 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
972 4));
973
974 return 0;
975}
976DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
977
978static int dm_test_ofnode_u64(struct unit_test_state *uts)
979{
980 ofnode node;
981 u64 val;
982
983 node = ofnode_path("/a-test");
984 ut_assert(ofnode_valid(node));
985 ut_assertok(ofnode_read_u64(node, "int64-value", &val));
986 ut_asserteq_64(0x1111222233334444, val);
987 ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
988
Michal Simek08a194e2023-08-25 11:37:46 +0200989 ut_assertok(ofnode_read_u64_index(node, "int64-array", 0, &val));
990 ut_asserteq_64(0x1111222233334444, val);
991 ut_assertok(ofnode_read_u64_index(node, "int64-array", 1, &val));
992 ut_asserteq_64(0x4444333322221111, val);
993 ut_asserteq(-EOVERFLOW,
994 ofnode_read_u64_index(node, "int64-array", 2, &val));
995 ut_asserteq(-EINVAL, ofnode_read_u64_index(node, "missing", 0, &val));
996
Simon Glasse3be5fc2022-09-06 20:27:18 -0600997 return 0;
998}
999DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
1000
Simon Glass56bc3322022-09-06 20:27:02 -06001001static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
1002{
1003 ofnode node, check, subnode;
1004 char buf[128];
1005
Simon Glass56bc3322022-09-06 20:27:02 -06001006 node = ofnode_path("/lcd");
1007 ut_assert(ofnode_valid(node));
1008 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
1009 check = ofnode_path("/lcd/edmund");
1010 ut_asserteq(subnode.of_offset, check.of_offset);
1011 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
1012 ut_asserteq_str("/lcd/edmund", buf);
1013
1014 if (of_live_active()) {
1015 struct device_node *child;
1016
1017 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
1018 2, &child));
1019 ut_asserteq_str("ed", child->name);
1020 ut_asserteq_str("/lcd/ed", child->full_name);
1021 check = ofnode_path("/lcd/ed");
1022 ut_asserteq_ptr(child, check.np);
1023 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
1024 sizeof(buf)));
1025 ut_asserteq_str("/lcd/ed", buf);
1026 }
1027
1028 /* An existing node should be returned with -EEXIST */
1029 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
1030 ut_asserteq(subnode.of_offset, check.of_offset);
1031
1032 /* add a root node */
1033 node = ofnode_path("/");
1034 ut_assert(ofnode_valid(node));
1035 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
1036 check = ofnode_path("/lcd2");
1037 ut_asserteq(subnode.of_offset, check.of_offset);
1038 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
1039 ut_asserteq_str("/lcd2", buf);
1040
1041 if (of_live_active()) {
1042 ulong start;
1043 int i;
1044
1045 /*
1046 * Make sure each of the three malloc()checks in
1047 * of_add_subnode() work
1048 */
1049 for (i = 0; i < 3; i++) {
1050 malloc_enable_testing(i);
1051 start = ut_check_free();
1052 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
1053 &check));
1054 ut_assertok(ut_check_delta(start));
1055 }
1056
1057 /* This should pass since we allow 3 allocations */
1058 malloc_enable_testing(3);
1059 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
1060 malloc_disable_testing();
1061 }
1062
Simon Glass238afb52022-09-06 20:27:03 -06001063 /* write to the empty node */
1064 ut_assertok(ofnode_write_string(subnode, "example", "text"));
1065
Simon Glass56bc3322022-09-06 20:27:02 -06001066 return 0;
1067}
Simon Glass8ae9f962022-09-06 20:27:07 -06001068DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass4caa79a2022-09-06 20:27:16 -06001069
1070static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
1071{
1072 ofnode node, subnode;
1073 struct ofprop prop;
1074 int count;
1075
Dzmitry Sankouski54f4c832023-01-22 18:21:23 +03001076 node = ofnode_path("/ofnode-foreach");
Simon Glass4caa79a2022-09-06 20:27:16 -06001077 count = 0;
1078
1079 /* we expect "compatible" for each node */
1080 ofnode_for_each_prop(prop, node)
1081 count++;
1082 ut_asserteq(1, count);
1083
1084 /* there are two nodes, each with 2 properties */
1085 ofnode_for_each_subnode(subnode, node)
1086 ofnode_for_each_prop(prop, subnode)
1087 count++;
1088 ut_asserteq(5, count);
1089
1090 return 0;
1091}
1092DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
Simon Glasse3be5fc2022-09-06 20:27:18 -06001093
1094static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
1095{
1096 const char *compat = "denx,u-boot-fdt-test";
1097 ofnode node;
1098 int count;
1099
1100 count = 0;
1101 for (node = ofnode_null();
1102 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
1103 count++;
1104 ut_asserteq(11, count);
1105
1106 return 0;
1107}
1108DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
1109
Simon Glass9e7a42a2022-09-06 20:27:30 -06001110static int dm_test_ofnode_by_compatible_ot(struct unit_test_state *uts)
1111{
1112 const char *compat = "sandbox-other2";
1113 oftree otree = get_other_oftree(uts);
1114 ofnode node;
1115 int count;
1116
1117 count = 0;
1118 for (node = oftree_root(otree);
1119 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
1120 count++;
1121 ut_asserteq(2, count);
1122
1123 return 0;
1124}
1125DM_TEST(dm_test_ofnode_by_compatible_ot, UT_TESTF_OTHER_FDT);
1126
Simon Glasse3be5fc2022-09-06 20:27:18 -06001127static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
1128{
1129 ofnode node, subnode;
1130
1131 node = ofnode_path("/buttons");
1132
1133 subnode = ofnode_find_subnode(node, "btn1");
1134 ut_assert(ofnode_valid(subnode));
1135 ut_asserteq_str("btn1", ofnode_get_name(subnode));
1136
1137 subnode = ofnode_find_subnode(node, "btn");
1138 ut_assert(!ofnode_valid(subnode));
1139
1140 return 0;
1141}
1142DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
1143
Simon Glass9e7a42a2022-09-06 20:27:30 -06001144static int dm_test_ofnode_find_subnode_ot(struct unit_test_state *uts)
1145{
1146 oftree otree = get_other_oftree(uts);
1147 ofnode node, subnode;
1148
1149 node = oftree_path(otree, "/node");
1150
1151 subnode = ofnode_find_subnode(node, "subnode");
1152 ut_assert(ofnode_valid(subnode));
1153 ut_asserteq_str("subnode", ofnode_get_name(subnode));
1154
1155 subnode = ofnode_find_subnode(node, "btn");
1156 ut_assert(!ofnode_valid(subnode));
1157
1158 return 0;
1159}
1160DM_TEST(dm_test_ofnode_find_subnode_ot, UT_TESTF_OTHER_FDT);
1161
Simon Glasse3be5fc2022-09-06 20:27:18 -06001162static int dm_test_ofnode_get_name(struct unit_test_state *uts)
1163{
1164 ofnode node;
1165
1166 node = ofnode_path("/buttons");
1167 ut_assert(ofnode_valid(node));
1168 ut_asserteq_str("buttons", ofnode_get_name(node));
1169 ut_asserteq_str("", ofnode_get_name(ofnode_root()));
1170
1171 return 0;
1172}
1173DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);
Simon Glass9e7a42a2022-09-06 20:27:30 -06001174
1175/* try to access more FDTs than is supported */
1176static int dm_test_ofnode_too_many(struct unit_test_state *uts)
1177{
1178 const int max_trees = CONFIG_IS_ENABLED(OFNODE_MULTI_TREE,
1179 (CONFIG_OFNODE_MULTI_TREE_MAX), (1));
1180 const int fdt_size = 256;
1181 const int num_trees = max_trees + 1;
1182 char fdt[num_trees][fdt_size];
1183 int i;
1184
1185 for (i = 0; i < num_trees; i++) {
1186 oftree tree;
1187 int ret;
1188
1189 ut_assertok(make_ofnode_fdt(uts, fdt[i], fdt_size, i));
1190 ret = get_oftree(uts, fdt[i], &tree);
1191
1192 /*
1193 * With flat tree we have the control FDT using one slot. Live
1194 * tree has no limit since it uses pointers, not integer tree
1195 * IDs
1196 */
1197 if (of_live_active() || i < max_trees - 1) {
1198 ut_assertok(ret);
1199 } else {
1200 /*
1201 * tree should be invalid when we try to register too
1202 * many trees
1203 */
1204 ut_asserteq(-EOVERFLOW, ret);
1205 }
1206 }
1207
1208 return 0;
1209}
1210DM_TEST(dm_test_ofnode_too_many, UT_TESTF_SCAN_FDT);
Simon Glass7a7229a2022-09-06 20:27:33 -06001211
Simon Glass68164892023-09-26 08:14:37 -06001212static int check_copy_props(struct unit_test_state *uts, ofnode dst, ofnode src)
Simon Glass7a7229a2022-09-06 20:27:33 -06001213{
1214 u32 reg[2], val;
1215
Simon Glass68164892023-09-26 08:14:37 -06001216 ut_assertok(ofnode_copy_props(dst, src));
Simon Glass7a7229a2022-09-06 20:27:33 -06001217
1218 ut_assertok(ofnode_read_u32(dst, "ping-expect", &val));
1219 ut_asserteq(3, val);
1220
1221 ut_asserteq_str("denx,u-boot-fdt-test",
1222 ofnode_read_string(dst, "compatible"));
1223
1224 /* check that a property with the same name is overwritten */
1225 ut_assertok(ofnode_read_u32_array(dst, "reg", reg, ARRAY_SIZE(reg)));
1226 ut_asserteq(3, reg[0]);
1227 ut_asserteq(1, reg[1]);
1228
1229 /* reset the compatible so the live tree does not change */
1230 ut_assertok(ofnode_write_string(dst, "compatible", "nothing"));
1231
1232 return 0;
1233}
1234
1235static int dm_test_ofnode_copy_props(struct unit_test_state *uts)
1236{
1237 ofnode src, dst;
1238
1239 /*
1240 * These nodes are chosen so that the src node is before the destination
1241 * node in the tree. This doesn't matter with livetree, but with
1242 * flattree any attempt to insert a property earlier in the tree will
1243 * mess up the offsets after it.
1244 */
1245 src = ofnode_path("/b-test");
1246 dst = ofnode_path("/some-bus");
1247
Simon Glass68164892023-09-26 08:14:37 -06001248 ut_assertok(check_copy_props(uts, dst, src));
Simon Glass7a7229a2022-09-06 20:27:33 -06001249
1250 /* check a property that is in the destination already */
1251 ut_asserteq_str("mux0", ofnode_read_string(dst, "mux-control-names"));
1252
1253 return 0;
1254}
1255DM_TEST(dm_test_ofnode_copy_props, UT_TESTF_SCAN_FDT);
1256
1257static int dm_test_ofnode_copy_props_ot(struct unit_test_state *uts)
1258{
1259 ofnode src, dst;
1260 oftree otree = get_other_oftree(uts);
1261
1262 src = ofnode_path("/b-test");
1263 dst = oftree_path(otree, "/node/subnode2");
Simon Glass68164892023-09-26 08:14:37 -06001264 ut_assertok(check_copy_props(uts, dst, src));
Simon Glass7a7229a2022-09-06 20:27:33 -06001265
1266 return 0;
1267}
1268DM_TEST(dm_test_ofnode_copy_props_ot, UT_TESTF_SCAN_FDT | UT_TESTF_OTHER_FDT);
Simon Glasse88e2fe2023-06-01 10:22:40 -06001269
1270/* check that the livetree is aligned to a structure boundary */
1271static int dm_test_livetree_align(struct unit_test_state *uts)
1272{
1273 const int align = __alignof__(struct unit_test_state);
1274 struct device_node *node;
1275 u32 *sentinel;
1276 ulong start;
1277
1278 start = (ulong)gd_of_root();
1279 ut_asserteq(start, ALIGN(start, align));
1280
1281 node = gd_of_root();
1282 sentinel = (void *)node - sizeof(u32);
1283
1284 /*
1285 * The sentinel should be overwritten with the root node. If it isn't,
1286 * then the root node is not at the very start of the livetree memory
1287 * area, and free(root) will fail to free the memory used by the
1288 * livetree.
1289 */
1290 ut_assert(*sentinel != BAD_OF_ROOT);
1291
1292 return 0;
1293}
1294DM_TEST(dm_test_livetree_align, UT_TESTF_LIVE_TREE);
Simon Glass722281b2023-06-01 10:22:42 -06001295
1296/* check that it is possible to load an arbitrary livetree */
1297static int dm_test_livetree_ensure(struct unit_test_state *uts)
1298{
1299 oftree tree;
1300 ofnode node;
1301
1302 /* read from other.dtb */
1303 ut_assertok(test_load_other_fdt(uts));
1304 tree = oftree_from_fdt(uts->other_fdt);
1305 ut_assert(oftree_valid(tree));
1306 node = oftree_path(tree, "/node/subnode");
1307 ut_assert(ofnode_valid(node));
1308 ut_asserteq_str("sandbox-other2",
1309 ofnode_read_string(node, "compatible"));
1310
1311 return 0;
1312}
1313DM_TEST(dm_test_livetree_ensure, 0);