blob: 134a307f1b205cc519c4dd2ca5f5f69ef5f9a79d [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
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +090054static int dm_test_ofnode_compatible(struct unit_test_state *uts)
55{
56 ofnode root_node = ofnode_path("/");
57
58 ut_assert(ofnode_valid(root_node));
59 ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
60
61 return 0;
62}
Simon Glass974dccd2020-07-28 19:41:12 -060063DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +020064
Patrick Delaunay04fcfe72020-09-24 17:26:20 +020065static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
66{
67 /* test invalid phandle */
68 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
69 ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
70
71 /* test first valid phandle */
72 ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
73
74 /* test unknown phandle */
75 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
76
Simon Glass95fd2092022-09-06 20:27:22 -060077 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
78
Patrick Delaunay04fcfe72020-09-24 17:26:20 +020079 return 0;
80}
81DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
82
Simon Glassf912a722022-09-06 20:27:27 -060083static int dm_test_ofnode_get_by_phandle_ot(struct unit_test_state *uts)
84{
85 oftree otree = get_other_oftree(uts);
86 ofnode node;
87
88 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
89 node = oftree_get_by_phandle(otree, 1);
90 ut_assert(ofnode_valid(node));
91 ut_asserteq_str("target", ofnode_get_name(node));
92
93 return 0;
94}
95DM_TEST(dm_test_ofnode_get_by_phandle_ot, UT_TESTF_OTHER_FDT);
96
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +020097static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
98{
99 const char propname[] = "compatible";
100 const char propval[] = "denx,u-boot-fdt-test";
101 const char *str;
102 ofnode node = ofnode_null();
103
104 /* Find first matching node, there should be at least one */
105 node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
106 ut_assert(ofnode_valid(node));
107 str = ofnode_read_string(node, propname);
108 ut_assert(str && !strcmp(str, propval));
109
110 /* Find the rest of the matching nodes */
111 while (true) {
112 node = ofnode_by_prop_value(node, propname, propval,
113 sizeof(propval));
114 if (!ofnode_valid(node))
115 break;
116 str = ofnode_read_string(node, propname);
117 ut_assert(str && !strcmp(str, propval));
118 }
119
120 return 0;
121}
Simon Glass974dccd2020-07-28 19:41:12 -0600122DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
Simon Glass699c9ca2018-10-01 12:22:08 -0600123
124static int dm_test_ofnode_fmap(struct unit_test_state *uts)
125{
126 struct fmap_entry entry;
127 ofnode node;
128
129 node = ofnode_path("/cros-ec/flash");
130 ut_assert(ofnode_valid(node));
131 ut_assertok(ofnode_read_fmap_entry(node, &entry));
132 ut_asserteq(0x08000000, entry.offset);
133 ut_asserteq(0x20000, entry.length);
134
135 return 0;
136}
Simon Glass974dccd2020-07-28 19:41:12 -0600137DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf3455962020-01-27 08:49:43 -0700138
Simon Glass0c2e9802020-01-27 08:49:44 -0700139static int dm_test_ofnode_read(struct unit_test_state *uts)
140{
141 const u32 *val;
142 ofnode node;
143 int size;
144
145 node = ofnode_path("/a-test");
146 ut_assert(ofnode_valid(node));
147
148 val = ofnode_read_prop(node, "int-value", &size);
149 ut_assertnonnull(val);
150 ut_asserteq(4, size);
151 ut_asserteq(1234, fdt32_to_cpu(val[0]));
152
153 val = ofnode_read_prop(node, "missing", &size);
154 ut_assertnull(val);
155 ut_asserteq(-FDT_ERR_NOTFOUND, size);
156
157 /* Check it works without a size parameter */
158 val = ofnode_read_prop(node, "missing", NULL);
159 ut_assertnull(val);
160
161 return 0;
162}
Simon Glass974dccd2020-07-28 19:41:12 -0600163DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0c2e9802020-01-27 08:49:44 -0700164
Patrick Delaunay8cd28012020-09-25 09:41:16 +0200165static int dm_test_ofnode_phandle(struct unit_test_state *uts)
166{
167 struct ofnode_phandle_args args;
168 ofnode node;
169 int ret;
170 const char prop[] = "test-gpios";
171 const char cell[] = "#gpio-cells";
172 const char prop2[] = "phandle-value";
173
174 node = ofnode_path("/a-test");
175 ut_assert(ofnode_valid(node));
176
177 /* Test ofnode_count_phandle_with_args with cell name */
178 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
179 ut_asserteq(-ENOENT, ret);
180 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
181 ut_asserteq(-EINVAL, ret);
182 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
183 ut_asserteq(5, ret);
184
185 /* Test ofnode_parse_phandle_with_args with cell name */
186 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
187 &args);
188 ut_asserteq(-ENOENT, ret);
189 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
190 &args);
191 ut_asserteq(-EINVAL, ret);
192 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
193 ut_assertok(ret);
194 ut_asserteq(1, args.args_count);
195 ut_asserteq(1, args.args[0]);
196 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
197 ut_assertok(ret);
198 ut_asserteq(1, args.args_count);
199 ut_asserteq(4, args.args[0]);
200 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
201 ut_assertok(ret);
202 ut_asserteq(5, args.args_count);
203 ut_asserteq(5, args.args[0]);
204 ut_asserteq(1, args.args[4]);
205 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
206 ut_asserteq(-ENOENT, ret);
207 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
208 ut_assertok(ret);
209 ut_asserteq(1, args.args_count);
210 ut_asserteq(12, args.args[0]);
211 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
212 ut_asserteq(-ENOENT, ret);
213
214 /* Test ofnode_count_phandle_with_args with cell count */
215 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
216 ut_asserteq(-ENOENT, ret);
217 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
218 ut_asserteq(3, ret);
219
220 /* Test ofnode_parse_phandle_with_args with cell count */
221 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
222 ut_assertok(ret);
223 ut_asserteq(1, ofnode_valid(args.node));
224 ut_asserteq(1, args.args_count);
225 ut_asserteq(10, args.args[0]);
226 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
227 ut_asserteq(-EINVAL, ret);
228 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
229 ut_assertok(ret);
230 ut_asserteq(1, ofnode_valid(args.node));
231 ut_asserteq(1, args.args_count);
232 ut_asserteq(30, args.args[0]);
233 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
234 ut_asserteq(-ENOENT, ret);
235
236 return 0;
237}
238DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
239
Simon Glassf912a722022-09-06 20:27:27 -0600240static int dm_test_ofnode_phandle_ot(struct unit_test_state *uts)
241{
242 oftree otree = get_other_oftree(uts);
243 struct ofnode_phandle_args args;
244 ofnode node;
245 int ret;
246
247 node = oftree_path(otree, "/node");
248
249 /* Test ofnode_count_phandle_with_args with cell name */
250 ret = ofnode_count_phandle_with_args(node, "missing", "#gpio-cells", 0);
251 ut_asserteq(-ENOENT, ret);
252 ret = ofnode_count_phandle_with_args(node, "target", "#invalid", 0);
253 ut_asserteq(-EINVAL, ret);
254 ret = ofnode_count_phandle_with_args(node, "target", "#gpio-cells", 0);
255 ut_asserteq(1, ret);
256
257 ret = ofnode_parse_phandle_with_args(node, "target", "#gpio-cells", 0,
258 0, &args);
259 ut_assertok(ret);
260 ut_asserteq(2, args.args_count);
261 ut_asserteq(3, args.args[0]);
262 ut_asserteq(4, args.args[1]);
263
264 return 0;
265}
266DM_TEST(dm_test_ofnode_phandle_ot, UT_TESTF_OTHER_FDT);
267
Simon Glassf3455962020-01-27 08:49:43 -0700268static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
269{
270 const char *str;
Simon Glasse09223c2020-01-27 08:49:46 -0700271 const u32 *val;
Simon Glassf3455962020-01-27 08:49:43 -0700272 ofnode node;
Simon Glasse09223c2020-01-27 08:49:46 -0700273 int size;
Simon Glassf3455962020-01-27 08:49:43 -0700274
275 str = ofnode_read_chosen_string("setting");
276 ut_assertnonnull(str);
277 ut_asserteq_str("sunrise ohoka", str);
278 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
279
280 node = ofnode_get_chosen_node("other-node");
281 ut_assert(ofnode_valid(node));
282 ut_asserteq_str("c-test@5", ofnode_get_name(node));
283
284 node = ofnode_get_chosen_node("setting");
285 ut_assert(!ofnode_valid(node));
286
Simon Glasse09223c2020-01-27 08:49:46 -0700287 val = ofnode_read_chosen_prop("int-values", &size);
288 ut_assertnonnull(val);
289 ut_asserteq(8, size);
290 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
291 ut_asserteq(72993, fdt32_to_cpu(val[1]));
292
Simon Glassf3455962020-01-27 08:49:43 -0700293 return 0;
294}
Simon Glass974dccd2020-07-28 19:41:12 -0600295DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
developercf8bc132020-05-02 11:35:10 +0200296
Michal Simek92a88622020-07-28 12:51:08 +0200297static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
298{
299 const void *val;
300 ofnode node;
301 int size;
302
Michael Walle7efcdfd2021-02-25 16:51:11 +0100303 node = ofnode_get_aliases_node("ethernet3");
Michal Simek92a88622020-07-28 12:51:08 +0200304 ut_assert(ofnode_valid(node));
305 ut_asserteq_str("sbe5", ofnode_get_name(node));
306
307 node = ofnode_get_aliases_node("unknown");
308 ut_assert(!ofnode_valid(node));
309
310 val = ofnode_read_aliases_prop("spi0", &size);
311 ut_assertnonnull(val);
312 ut_asserteq(7, size);
313 ut_asserteq_str("/spi@0", (const char *)val);
314
315 return 0;
316}
317DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
318
developercf8bc132020-05-02 11:35:10 +0200319static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
320{
321 ofnode node, child_node;
322 u32 val;
323
324 node = ofnode_path("/i-test");
325 ut_assert(ofnode_valid(node));
326
327 val = ofnode_get_child_count(node);
328 ut_asserteq(3, val);
329
330 child_node = ofnode_first_subnode(node);
331 ut_assert(ofnode_valid(child_node));
332 val = ofnode_get_child_count(child_node);
333 ut_asserteq(0, val);
334
335 return 0;
336}
337DM_TEST(dm_test_ofnode_get_child_count,
Simon Glass974dccd2020-07-28 19:41:12 -0600338 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass5de5b3b2020-11-28 17:50:02 -0700339
340static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
341{
342 ofnode root_node = ofnode_path("/");
343 ofnode node = ofnode_path("/usb@0");
344
345 ut_assert(ofnode_is_enabled(root_node));
346 ut_assert(!ofnode_is_enabled(node));
347
348 return 0;
349}
350DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800351
352static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
353{
354 ofnode node;
355 fdt_addr_t addr;
356 fdt_size_t size;
357
358 node = ofnode_path("/translation-test@8000");
359 ut_assert(ofnode_valid(node));
360 addr = ofnode_get_addr(node);
361 size = ofnode_get_size(node);
362 ut_asserteq(0x8000, addr);
363 ut_asserteq(0x4000, size);
364
365 node = ofnode_path("/translation-test@8000/dev@1,100");
366 ut_assert(ofnode_valid(node));
367 addr = ofnode_get_addr(node);
368 size = ofnode_get_size(node);
369 ut_asserteq(0x9000, addr);
370 ut_asserteq(0x1000, size);
371
372 node = ofnode_path("/emul-mux-controller");
373 ut_assert(ofnode_valid(node));
374 addr = ofnode_get_addr(node);
375 size = ofnode_get_size(node);
Patrice Chotardb4c52112022-01-04 08:42:48 +0100376 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800377 ut_asserteq(FDT_SIZE_T_NONE, size);
378
Marek Behún177ab7f2021-05-26 14:08:17 +0200379 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
380 ut_assert(ofnode_valid(node));
381 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
382 ut_asserteq_64(0x42, addr);
383
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800384 return 0;
385}
386DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Marek Behúne897e3c2021-05-26 14:08:18 +0200387
388static int dm_test_ofnode_get_path(struct unit_test_state *uts)
389{
390 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
391 char buf[64];
392 ofnode node;
393 int res;
394
395 node = ofnode_path(path);
396 ut_assert(ofnode_valid(node));
397
398 res = ofnode_get_path(node, buf, 64);
399 ut_asserteq(0, res);
400 ut_asserteq_str(path, buf);
401
402 res = ofnode_get_path(node, buf, 32);
403 ut_asserteq(-ENOSPC, res);
404
Simon Glasse3be5fc2022-09-06 20:27:18 -0600405 res = ofnode_get_path(ofnode_root(), buf, 32);
406 ut_asserteq_str("/", buf);
407
Marek Behúne897e3c2021-05-26 14:08:18 +0200408 return 0;
409}
410DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0034d962021-08-07 07:24:01 -0600411
412static int dm_test_ofnode_conf(struct unit_test_state *uts)
413{
414 ut_assert(!ofnode_conf_read_bool("missing"));
415 ut_assert(ofnode_conf_read_bool("testing-bool"));
416
417 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
418 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
419
420 ut_assertnull(ofnode_conf_read_str("missing"));
421 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
422
423 return 0;
424}
425DM_TEST(dm_test_ofnode_conf, 0);
Michael Wallef1f87402021-10-15 15:15:18 +0200426
427static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
428{
429 const char compatible[] = "denx,u-boot-fdt-test";
430 bool found = false;
431 ofnode node;
432
433 ofnode_for_each_compatible_node(node, compatible) {
434 ut_assert(ofnode_device_is_compatible(node, compatible));
435 found = true;
436 }
437
438 /* There should be at least one matching node */
439 ut_assert(found);
440
441 return 0;
442}
443DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
Simon Glass73025392021-10-23 17:26:04 -0600444
445static int dm_test_ofnode_string(struct unit_test_state *uts)
446{
Simon Glass9580bfc2021-10-23 17:26:07 -0600447 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600448 const char *out;
449 ofnode node;
450
451 node = ofnode_path("/a-test");
452 ut_assert(ofnode_valid(node));
453
454 /* single string */
455 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
456 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
457 ut_asserteq_str("test string", out);
458 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
459 "test string"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600460 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
461 ut_asserteq_str("test string", val[0]);
462 ut_assertnull(val[1]);
463 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600464
465 /* list of strings */
466 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
467 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
468 &out));
469 ut_asserteq_str("mux0", out);
470 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
471 "mux0"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600472 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
473 &val));
474 ut_asserteq_str("mux0", val[0]);
475 ut_asserteq_str("mux1", val[1]);
476 ut_asserteq_str("mux2", val[2]);
477 ut_asserteq_str("mux3", val[3]);
478 ut_asserteq_str("mux4", val[4]);
479 ut_assertnull(val[5]);
480 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600481
482 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
483 &out));
484 ut_asserteq_str("mux4", out);
485 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
486 "mux4"));
487
488 return 0;
489}
490DM_TEST(dm_test_ofnode_string, 0);
491
492static int dm_test_ofnode_string_err(struct unit_test_state *uts)
493{
Simon Glass9580bfc2021-10-23 17:26:07 -0600494 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600495 const char *out;
496 ofnode node;
497
498 /*
499 * Test error codes only on livetree, as they are different with
500 * flattree
501 */
502 node = ofnode_path("/a-test");
503 ut_assert(ofnode_valid(node));
504
505 /* non-existent property */
506 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
507 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
508 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600509 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
Simon Glass73025392021-10-23 17:26:04 -0600510
511 /* empty property */
512 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
513 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
514 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600515 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
516 &val));
Simon Glass73025392021-10-23 17:26:04 -0600517
518 /* badly formatted string list */
519 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
520 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
521 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600522 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
523 &val));
Simon Glass73025392021-10-23 17:26:04 -0600524
525 /* out of range / not found */
526 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
527 &out));
528 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
529 "other"));
530
531 /* negative value for index is not allowed, so don't test for that */
532
533 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
534 "mux-control-names", 5,
535 &out));
536
537 return 0;
538}
539DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200540
541static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
542{
543 ofnode eth_node, phy_node;
Marek Behúnbc194772022-04-07 00:33:01 +0200544 phy_interface_t mode;
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200545 u32 reg;
546
547 eth_node = ofnode_path("/phy-test-eth");
548 ut_assert(ofnode_valid(eth_node));
549
Marek Behúnbc194772022-04-07 00:33:01 +0200550 mode = ofnode_read_phy_mode(eth_node);
551 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
552
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200553 phy_node = ofnode_get_phy_node(eth_node);
554 ut_assert(ofnode_valid(phy_node));
555
556 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
557 ut_asserteq_64(0x1, reg);
558
559 return 0;
560}
561DM_TEST(dm_test_ofnode_get_phy, 0);
Simon Glassef75c592022-07-30 15:52:08 -0600562
563/**
564 * make_ofnode_fdt() - Create an FDT for testing with ofnode
565 *
566 * The size is set to the minimum needed
567 *
568 * @uts: Test state
569 * @fdt: Place to write FDT
570 * @size: Maximum size of space for fdt
571 */
572static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
573{
574 ut_assertok(fdt_create(fdt, size));
575 ut_assertok(fdt_finish_reservemap(fdt));
576 ut_assert(fdt_begin_node(fdt, "") >= 0);
577
578 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
579 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
580 ut_assertok(fdt_end_node(fdt));
581
582 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
583 ut_assertok(fdt_end_node(fdt));
584
585 ut_assertok(fdt_end_node(fdt));
586 ut_assertok(fdt_finish(fdt));
587
588 return 0;
589}
590
591static int dm_test_ofnode_root(struct unit_test_state *uts)
592{
593 struct device_node *root = NULL;
594 char fdt[256];
595 oftree tree;
596 ofnode node;
597
598 /* Check that aliases work on the control FDT */
599 node = ofnode_get_aliases_node("ethernet3");
600 ut_assert(ofnode_valid(node));
601 ut_asserteq_str("sbe5", ofnode_get_name(node));
602
Simon Glass2b9b14582022-09-06 20:27:21 -0600603 ut_assert(!oftree_valid(oftree_null()));
604
Simon Glassef75c592022-07-30 15:52:08 -0600605 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
606 if (of_live_active()) {
607 ut_assertok(unflatten_device_tree(fdt, &root));
Simon Glass2b9b14582022-09-06 20:27:21 -0600608 tree = oftree_from_np(root);
Simon Glassef75c592022-07-30 15:52:08 -0600609 } else {
Simon Glass2b9b14582022-09-06 20:27:21 -0600610 tree = oftree_from_fdt(fdt);
Simon Glassef75c592022-07-30 15:52:08 -0600611 }
Simon Glass2b9b14582022-09-06 20:27:21 -0600612 ut_assert(oftree_valid(tree));
Simon Glassef75c592022-07-30 15:52:08 -0600613
614 /* Make sure they don't work on this new tree */
Simon Glass45ae59d2022-09-06 20:27:24 -0600615 node = oftree_path(tree, "mmc0");
Simon Glassef75c592022-07-30 15:52:08 -0600616 ut_assert(!ofnode_valid(node));
617
618 /* It should appear in the new tree */
Simon Glass45ae59d2022-09-06 20:27:24 -0600619 node = oftree_path(tree, "/new-mmc");
Simon Glassef75c592022-07-30 15:52:08 -0600620 ut_assert(ofnode_valid(node));
621
622 /* ...and not in the control FDT */
Simon Glass45ae59d2022-09-06 20:27:24 -0600623 node = oftree_path(oftree_default(), "/new-mmc");
Simon Glassef75c592022-07-30 15:52:08 -0600624 ut_assert(!ofnode_valid(node));
625
626 free(root);
627
628 return 0;
629}
630DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
Simon Glass270a4432022-07-30 15:52:09 -0600631
632static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
633{
634 struct udevice *dev;
635 ofnode node;
636
Simon Glass270a4432022-07-30 15:52:09 -0600637 /* Test enabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600638 node = ofnode_path("/usb@2");
639
Simon Glass3ee3d152022-07-30 15:52:13 -0600640 ut_assert(!ofnode_is_enabled(node));
641 ut_assertok(ofnode_set_enabled(node, true));
642 ut_asserteq(true, ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600643
644 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
645 &dev);
646 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
647
648 /* Test string property setting */
Simon Glass270a4432022-07-30 15:52:09 -0600649 ut_assert(device_is_compatible(dev, "sandbox,usb"));
650 ofnode_write_string(node, "compatible", "gdsys,super-usb");
651 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
652 ofnode_write_string(node, "compatible", "sandbox,usb");
653 ut_assert(device_is_compatible(dev, "sandbox,usb"));
654
655 /* Test setting generic properties */
656
657 /* Non-existent in DTB */
658 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
659 /* reg = 0x42, size = 0x100 */
Simon Glass5e2cd5e2022-07-30 15:52:10 -0600660 ut_assertok(ofnode_write_prop(node, "reg",
661 "\x00\x00\x00\x42\x00\x00\x01\x00", 8));
Simon Glass270a4432022-07-30 15:52:09 -0600662 ut_asserteq(0x42, dev_read_addr(dev));
663
664 /* Test disabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600665 device_remove(dev, DM_REMOVE_NORMAL);
666 device_unbind(dev);
667
Simon Glass3ee3d152022-07-30 15:52:13 -0600668 ut_assert(ofnode_is_enabled(node));
669 ut_assertok(ofnode_set_enabled(node, false));
670 ut_assert(!ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600671
672 return 0;
673}
Simon Glassb75dc162022-07-30 15:52:11 -0600674DM_TEST(dm_test_ofnode_livetree_writing,
Simon Glass8ae9f962022-09-06 20:27:07 -0600675 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd28e31e2022-07-30 15:52:14 -0600676
677static int dm_test_ofnode_u32(struct unit_test_state *uts)
678{
679 ofnode node;
Simon Glasse3be5fc2022-09-06 20:27:18 -0600680 u32 val;
Simon Glassd28e31e2022-07-30 15:52:14 -0600681
682 node = ofnode_path("/lcd");
683 ut_assert(ofnode_valid(node));
684 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
685 ut_assertok(ofnode_write_u32(node, "xres", 1367));
686 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
687 ut_assertok(ofnode_write_u32(node, "xres", 1366));
688
Simon Glasse3be5fc2022-09-06 20:27:18 -0600689 node = ofnode_path("/backlight");
690 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
691 ut_asserteq(0, val);
692 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
693 ut_asserteq(16, val);
694 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
695 ut_asserteq(255, val);
696 ut_asserteq(-EOVERFLOW,
697 ofnode_read_u32_index(node, "brightness-levels", 9, &val));
698 ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
699
Simon Glassd28e31e2022-07-30 15:52:14 -0600700 return 0;
701}
Simon Glass8ae9f962022-09-06 20:27:07 -0600702DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass56bc3322022-09-06 20:27:02 -0600703
Simon Glasse3be5fc2022-09-06 20:27:18 -0600704static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
705{
706 ofnode node;
707 u32 val[10];
708
709 node = ofnode_path("/a-test");
710 ut_assert(ofnode_valid(node));
711 ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
712 ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
713 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
714 1));
715
716 memset(val, '\0', sizeof(val));
717 ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
718 ut_asserteq(0, val[0]);
719 ut_asserteq(5678, val[1]);
720 ut_asserteq(9123, val[2]);
721 ut_asserteq(4567, val[3]);
722 ut_asserteq(0, val[4]);
723 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
724 4));
725
726 return 0;
727}
728DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
729
730static int dm_test_ofnode_u64(struct unit_test_state *uts)
731{
732 ofnode node;
733 u64 val;
734
735 node = ofnode_path("/a-test");
736 ut_assert(ofnode_valid(node));
737 ut_assertok(ofnode_read_u64(node, "int64-value", &val));
738 ut_asserteq_64(0x1111222233334444, val);
739 ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
740
741 return 0;
742}
743DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
744
Simon Glass56bc3322022-09-06 20:27:02 -0600745static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
746{
747 ofnode node, check, subnode;
748 char buf[128];
749
Simon Glass56bc3322022-09-06 20:27:02 -0600750 node = ofnode_path("/lcd");
751 ut_assert(ofnode_valid(node));
752 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
753 check = ofnode_path("/lcd/edmund");
754 ut_asserteq(subnode.of_offset, check.of_offset);
755 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
756 ut_asserteq_str("/lcd/edmund", buf);
757
758 if (of_live_active()) {
759 struct device_node *child;
760
761 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
762 2, &child));
763 ut_asserteq_str("ed", child->name);
764 ut_asserteq_str("/lcd/ed", child->full_name);
765 check = ofnode_path("/lcd/ed");
766 ut_asserteq_ptr(child, check.np);
767 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
768 sizeof(buf)));
769 ut_asserteq_str("/lcd/ed", buf);
770 }
771
772 /* An existing node should be returned with -EEXIST */
773 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
774 ut_asserteq(subnode.of_offset, check.of_offset);
775
776 /* add a root node */
777 node = ofnode_path("/");
778 ut_assert(ofnode_valid(node));
779 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
780 check = ofnode_path("/lcd2");
781 ut_asserteq(subnode.of_offset, check.of_offset);
782 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
783 ut_asserteq_str("/lcd2", buf);
784
785 if (of_live_active()) {
786 ulong start;
787 int i;
788
789 /*
790 * Make sure each of the three malloc()checks in
791 * of_add_subnode() work
792 */
793 for (i = 0; i < 3; i++) {
794 malloc_enable_testing(i);
795 start = ut_check_free();
796 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
797 &check));
798 ut_assertok(ut_check_delta(start));
799 }
800
801 /* This should pass since we allow 3 allocations */
802 malloc_enable_testing(3);
803 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
804 malloc_disable_testing();
805 }
806
Simon Glass238afb52022-09-06 20:27:03 -0600807 /* write to the empty node */
808 ut_assertok(ofnode_write_string(subnode, "example", "text"));
809
Simon Glass56bc3322022-09-06 20:27:02 -0600810 return 0;
811}
Simon Glass8ae9f962022-09-06 20:27:07 -0600812DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass4caa79a2022-09-06 20:27:16 -0600813
814static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
815{
816 ofnode node, subnode;
817 struct ofprop prop;
818 int count;
819
820 node = ofnode_path("/buttons");
821 count = 0;
822
823 /* we expect "compatible" for each node */
824 ofnode_for_each_prop(prop, node)
825 count++;
826 ut_asserteq(1, count);
827
828 /* there are two nodes, each with 2 properties */
829 ofnode_for_each_subnode(subnode, node)
830 ofnode_for_each_prop(prop, subnode)
831 count++;
832 ut_asserteq(5, count);
833
834 return 0;
835}
836DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
Simon Glasse3be5fc2022-09-06 20:27:18 -0600837
838static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
839{
840 const char *compat = "denx,u-boot-fdt-test";
841 ofnode node;
842 int count;
843
844 count = 0;
845 for (node = ofnode_null();
846 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
847 count++;
848 ut_asserteq(11, count);
849
850 return 0;
851}
852DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
853
854static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
855{
856 ofnode node, subnode;
857
858 node = ofnode_path("/buttons");
859
860 subnode = ofnode_find_subnode(node, "btn1");
861 ut_assert(ofnode_valid(subnode));
862 ut_asserteq_str("btn1", ofnode_get_name(subnode));
863
864 subnode = ofnode_find_subnode(node, "btn");
865 ut_assert(!ofnode_valid(subnode));
866
867 return 0;
868}
869DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
870
871static int dm_test_ofnode_get_name(struct unit_test_state *uts)
872{
873 ofnode node;
874
875 node = ofnode_path("/buttons");
876 ut_assert(ofnode_valid(node));
877 ut_asserteq_str("buttons", ofnode_get_name(node));
878 ut_asserteq_str("", ofnode_get_name(ofnode_root()));
879
880 return 0;
881}
882DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);