blob: f146b52d62554bf2c211c846ab9fa0d545507a3e [file] [log] [blame]
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +09001// SPDX-License-Identifier: GPL-2.0+
2
3#include <common.h>
4#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06005#include <log.h>
Simon Glassef75c592022-07-30 15:52:08 -06006#include <of_live.h>
Simon Glass270a4432022-07-30 15:52:09 -06007#include <dm/device-internal.h>
8#include <dm/lists.h>
Simon Glass699c9ca2018-10-01 12:22:08 -06009#include <dm/of_extra.h>
Simon Glass270a4432022-07-30 15:52:09 -060010#include <dm/root.h>
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +090011#include <dm/test.h>
Simon Glass270a4432022-07-30 15:52:09 -060012#include <dm/uclass-internal.h>
Simon Glass75c4d412020-07-19 10:15:37 -060013#include <test/test.h>
Masahiro Yamadab4f2dda2018-04-27 01:02:02 +090014#include <test/ut.h>
15
16static int dm_test_ofnode_compatible(struct unit_test_state *uts)
17{
18 ofnode root_node = ofnode_path("/");
19
20 ut_assert(ofnode_valid(root_node));
21 ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
22
23 return 0;
24}
Simon Glass974dccd2020-07-28 19:41:12 -060025DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +020026
Patrick Delaunay04fcfe72020-09-24 17:26:20 +020027static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
28{
29 /* test invalid phandle */
30 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
31 ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
32
33 /* test first valid phandle */
34 ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
35
36 /* test unknown phandle */
37 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
38
39 return 0;
40}
41DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
42
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +020043static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
44{
45 const char propname[] = "compatible";
46 const char propval[] = "denx,u-boot-fdt-test";
47 const char *str;
48 ofnode node = ofnode_null();
49
50 /* Find first matching node, there should be at least one */
51 node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
52 ut_assert(ofnode_valid(node));
53 str = ofnode_read_string(node, propname);
54 ut_assert(str && !strcmp(str, propval));
55
56 /* Find the rest of the matching nodes */
57 while (true) {
58 node = ofnode_by_prop_value(node, propname, propval,
59 sizeof(propval));
60 if (!ofnode_valid(node))
61 break;
62 str = ofnode_read_string(node, propname);
63 ut_assert(str && !strcmp(str, propval));
64 }
65
66 return 0;
67}
Simon Glass974dccd2020-07-28 19:41:12 -060068DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
Simon Glass699c9ca2018-10-01 12:22:08 -060069
70static int dm_test_ofnode_fmap(struct unit_test_state *uts)
71{
72 struct fmap_entry entry;
73 ofnode node;
74
75 node = ofnode_path("/cros-ec/flash");
76 ut_assert(ofnode_valid(node));
77 ut_assertok(ofnode_read_fmap_entry(node, &entry));
78 ut_asserteq(0x08000000, entry.offset);
79 ut_asserteq(0x20000, entry.length);
80
81 return 0;
82}
Simon Glass974dccd2020-07-28 19:41:12 -060083DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf3455962020-01-27 08:49:43 -070084
Simon Glass0c2e9802020-01-27 08:49:44 -070085static int dm_test_ofnode_read(struct unit_test_state *uts)
86{
87 const u32 *val;
88 ofnode node;
89 int size;
90
91 node = ofnode_path("/a-test");
92 ut_assert(ofnode_valid(node));
93
94 val = ofnode_read_prop(node, "int-value", &size);
95 ut_assertnonnull(val);
96 ut_asserteq(4, size);
97 ut_asserteq(1234, fdt32_to_cpu(val[0]));
98
99 val = ofnode_read_prop(node, "missing", &size);
100 ut_assertnull(val);
101 ut_asserteq(-FDT_ERR_NOTFOUND, size);
102
103 /* Check it works without a size parameter */
104 val = ofnode_read_prop(node, "missing", NULL);
105 ut_assertnull(val);
106
107 return 0;
108}
Simon Glass974dccd2020-07-28 19:41:12 -0600109DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0c2e9802020-01-27 08:49:44 -0700110
Patrick Delaunay8cd28012020-09-25 09:41:16 +0200111static int dm_test_ofnode_phandle(struct unit_test_state *uts)
112{
113 struct ofnode_phandle_args args;
114 ofnode node;
115 int ret;
116 const char prop[] = "test-gpios";
117 const char cell[] = "#gpio-cells";
118 const char prop2[] = "phandle-value";
119
120 node = ofnode_path("/a-test");
121 ut_assert(ofnode_valid(node));
122
123 /* Test ofnode_count_phandle_with_args with cell name */
124 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
125 ut_asserteq(-ENOENT, ret);
126 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
127 ut_asserteq(-EINVAL, ret);
128 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
129 ut_asserteq(5, ret);
130
131 /* Test ofnode_parse_phandle_with_args with cell name */
132 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
133 &args);
134 ut_asserteq(-ENOENT, ret);
135 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
136 &args);
137 ut_asserteq(-EINVAL, ret);
138 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
139 ut_assertok(ret);
140 ut_asserteq(1, args.args_count);
141 ut_asserteq(1, args.args[0]);
142 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
143 ut_assertok(ret);
144 ut_asserteq(1, args.args_count);
145 ut_asserteq(4, args.args[0]);
146 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
147 ut_assertok(ret);
148 ut_asserteq(5, args.args_count);
149 ut_asserteq(5, args.args[0]);
150 ut_asserteq(1, args.args[4]);
151 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
152 ut_asserteq(-ENOENT, ret);
153 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
154 ut_assertok(ret);
155 ut_asserteq(1, args.args_count);
156 ut_asserteq(12, args.args[0]);
157 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
158 ut_asserteq(-ENOENT, ret);
159
160 /* Test ofnode_count_phandle_with_args with cell count */
161 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
162 ut_asserteq(-ENOENT, ret);
163 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
164 ut_asserteq(3, ret);
165
166 /* Test ofnode_parse_phandle_with_args with cell count */
167 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
168 ut_assertok(ret);
169 ut_asserteq(1, ofnode_valid(args.node));
170 ut_asserteq(1, args.args_count);
171 ut_asserteq(10, args.args[0]);
172 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
173 ut_asserteq(-EINVAL, ret);
174 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
175 ut_assertok(ret);
176 ut_asserteq(1, ofnode_valid(args.node));
177 ut_asserteq(1, args.args_count);
178 ut_asserteq(30, args.args[0]);
179 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
180 ut_asserteq(-ENOENT, ret);
181
182 return 0;
183}
184DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
185
Simon Glassf3455962020-01-27 08:49:43 -0700186static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
187{
188 const char *str;
Simon Glasse09223c2020-01-27 08:49:46 -0700189 const u32 *val;
Simon Glassf3455962020-01-27 08:49:43 -0700190 ofnode node;
Simon Glasse09223c2020-01-27 08:49:46 -0700191 int size;
Simon Glassf3455962020-01-27 08:49:43 -0700192
193 str = ofnode_read_chosen_string("setting");
194 ut_assertnonnull(str);
195 ut_asserteq_str("sunrise ohoka", str);
196 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
197
198 node = ofnode_get_chosen_node("other-node");
199 ut_assert(ofnode_valid(node));
200 ut_asserteq_str("c-test@5", ofnode_get_name(node));
201
202 node = ofnode_get_chosen_node("setting");
203 ut_assert(!ofnode_valid(node));
204
Simon Glasse09223c2020-01-27 08:49:46 -0700205 val = ofnode_read_chosen_prop("int-values", &size);
206 ut_assertnonnull(val);
207 ut_asserteq(8, size);
208 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
209 ut_asserteq(72993, fdt32_to_cpu(val[1]));
210
Simon Glassf3455962020-01-27 08:49:43 -0700211 return 0;
212}
Simon Glass974dccd2020-07-28 19:41:12 -0600213DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
developercf8bc132020-05-02 11:35:10 +0200214
Michal Simek92a88622020-07-28 12:51:08 +0200215static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
216{
217 const void *val;
218 ofnode node;
219 int size;
220
Michael Walle7efcdfd2021-02-25 16:51:11 +0100221 node = ofnode_get_aliases_node("ethernet3");
Michal Simek92a88622020-07-28 12:51:08 +0200222 ut_assert(ofnode_valid(node));
223 ut_asserteq_str("sbe5", ofnode_get_name(node));
224
225 node = ofnode_get_aliases_node("unknown");
226 ut_assert(!ofnode_valid(node));
227
228 val = ofnode_read_aliases_prop("spi0", &size);
229 ut_assertnonnull(val);
230 ut_asserteq(7, size);
231 ut_asserteq_str("/spi@0", (const char *)val);
232
233 return 0;
234}
235DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
236
developercf8bc132020-05-02 11:35:10 +0200237static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
238{
239 ofnode node, child_node;
240 u32 val;
241
242 node = ofnode_path("/i-test");
243 ut_assert(ofnode_valid(node));
244
245 val = ofnode_get_child_count(node);
246 ut_asserteq(3, val);
247
248 child_node = ofnode_first_subnode(node);
249 ut_assert(ofnode_valid(child_node));
250 val = ofnode_get_child_count(child_node);
251 ut_asserteq(0, val);
252
253 return 0;
254}
255DM_TEST(dm_test_ofnode_get_child_count,
Simon Glass974dccd2020-07-28 19:41:12 -0600256 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass5de5b3b2020-11-28 17:50:02 -0700257
258static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
259{
260 ofnode root_node = ofnode_path("/");
261 ofnode node = ofnode_path("/usb@0");
262
263 ut_assert(ofnode_is_enabled(root_node));
264 ut_assert(!ofnode_is_enabled(node));
265
266 return 0;
267}
268DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800269
270static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
271{
272 ofnode node;
273 fdt_addr_t addr;
274 fdt_size_t size;
275
276 node = ofnode_path("/translation-test@8000");
277 ut_assert(ofnode_valid(node));
278 addr = ofnode_get_addr(node);
279 size = ofnode_get_size(node);
280 ut_asserteq(0x8000, addr);
281 ut_asserteq(0x4000, size);
282
283 node = ofnode_path("/translation-test@8000/dev@1,100");
284 ut_assert(ofnode_valid(node));
285 addr = ofnode_get_addr(node);
286 size = ofnode_get_size(node);
287 ut_asserteq(0x9000, addr);
288 ut_asserteq(0x1000, size);
289
290 node = ofnode_path("/emul-mux-controller");
291 ut_assert(ofnode_valid(node));
292 addr = ofnode_get_addr(node);
293 size = ofnode_get_size(node);
Patrice Chotardb4c52112022-01-04 08:42:48 +0100294 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800295 ut_asserteq(FDT_SIZE_T_NONE, size);
296
Marek Behún177ab7f2021-05-26 14:08:17 +0200297 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
298 ut_assert(ofnode_valid(node));
299 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
300 ut_asserteq_64(0x42, addr);
301
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800302 return 0;
303}
304DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Marek Behúne897e3c2021-05-26 14:08:18 +0200305
306static int dm_test_ofnode_get_path(struct unit_test_state *uts)
307{
308 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
309 char buf[64];
310 ofnode node;
311 int res;
312
313 node = ofnode_path(path);
314 ut_assert(ofnode_valid(node));
315
316 res = ofnode_get_path(node, buf, 64);
317 ut_asserteq(0, res);
318 ut_asserteq_str(path, buf);
319
320 res = ofnode_get_path(node, buf, 32);
321 ut_asserteq(-ENOSPC, res);
322
Simon Glasse3be5fc2022-09-06 20:27:18 -0600323 res = ofnode_get_path(ofnode_root(), buf, 32);
324 ut_asserteq_str("/", buf);
325
Marek Behúne897e3c2021-05-26 14:08:18 +0200326 return 0;
327}
328DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0034d962021-08-07 07:24:01 -0600329
330static int dm_test_ofnode_conf(struct unit_test_state *uts)
331{
332 ut_assert(!ofnode_conf_read_bool("missing"));
333 ut_assert(ofnode_conf_read_bool("testing-bool"));
334
335 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
336 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
337
338 ut_assertnull(ofnode_conf_read_str("missing"));
339 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
340
341 return 0;
342}
343DM_TEST(dm_test_ofnode_conf, 0);
Michael Wallef1f87402021-10-15 15:15:18 +0200344
345static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
346{
347 const char compatible[] = "denx,u-boot-fdt-test";
348 bool found = false;
349 ofnode node;
350
351 ofnode_for_each_compatible_node(node, compatible) {
352 ut_assert(ofnode_device_is_compatible(node, compatible));
353 found = true;
354 }
355
356 /* There should be at least one matching node */
357 ut_assert(found);
358
359 return 0;
360}
361DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
Simon Glass73025392021-10-23 17:26:04 -0600362
363static int dm_test_ofnode_string(struct unit_test_state *uts)
364{
Simon Glass9580bfc2021-10-23 17:26:07 -0600365 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600366 const char *out;
367 ofnode node;
368
369 node = ofnode_path("/a-test");
370 ut_assert(ofnode_valid(node));
371
372 /* single string */
373 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
374 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
375 ut_asserteq_str("test string", out);
376 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
377 "test string"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600378 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
379 ut_asserteq_str("test string", val[0]);
380 ut_assertnull(val[1]);
381 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600382
383 /* list of strings */
384 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
385 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
386 &out));
387 ut_asserteq_str("mux0", out);
388 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
389 "mux0"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600390 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
391 &val));
392 ut_asserteq_str("mux0", val[0]);
393 ut_asserteq_str("mux1", val[1]);
394 ut_asserteq_str("mux2", val[2]);
395 ut_asserteq_str("mux3", val[3]);
396 ut_asserteq_str("mux4", val[4]);
397 ut_assertnull(val[5]);
398 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600399
400 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
401 &out));
402 ut_asserteq_str("mux4", out);
403 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
404 "mux4"));
405
406 return 0;
407}
408DM_TEST(dm_test_ofnode_string, 0);
409
410static int dm_test_ofnode_string_err(struct unit_test_state *uts)
411{
Simon Glass9580bfc2021-10-23 17:26:07 -0600412 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600413 const char *out;
414 ofnode node;
415
416 /*
417 * Test error codes only on livetree, as they are different with
418 * flattree
419 */
420 node = ofnode_path("/a-test");
421 ut_assert(ofnode_valid(node));
422
423 /* non-existent property */
424 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
425 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
426 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600427 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
Simon Glass73025392021-10-23 17:26:04 -0600428
429 /* empty property */
430 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
431 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
432 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600433 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
434 &val));
Simon Glass73025392021-10-23 17:26:04 -0600435
436 /* badly formatted string list */
437 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
438 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
439 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600440 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
441 &val));
Simon Glass73025392021-10-23 17:26:04 -0600442
443 /* out of range / not found */
444 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
445 &out));
446 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
447 "other"));
448
449 /* negative value for index is not allowed, so don't test for that */
450
451 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
452 "mux-control-names", 5,
453 &out));
454
455 return 0;
456}
457DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200458
459static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
460{
461 ofnode eth_node, phy_node;
Marek Behúnbc194772022-04-07 00:33:01 +0200462 phy_interface_t mode;
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200463 u32 reg;
464
465 eth_node = ofnode_path("/phy-test-eth");
466 ut_assert(ofnode_valid(eth_node));
467
Marek Behúnbc194772022-04-07 00:33:01 +0200468 mode = ofnode_read_phy_mode(eth_node);
469 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
470
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200471 phy_node = ofnode_get_phy_node(eth_node);
472 ut_assert(ofnode_valid(phy_node));
473
474 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
475 ut_asserteq_64(0x1, reg);
476
477 return 0;
478}
479DM_TEST(dm_test_ofnode_get_phy, 0);
Simon Glassef75c592022-07-30 15:52:08 -0600480
481/**
482 * make_ofnode_fdt() - Create an FDT for testing with ofnode
483 *
484 * The size is set to the minimum needed
485 *
486 * @uts: Test state
487 * @fdt: Place to write FDT
488 * @size: Maximum size of space for fdt
489 */
490static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
491{
492 ut_assertok(fdt_create(fdt, size));
493 ut_assertok(fdt_finish_reservemap(fdt));
494 ut_assert(fdt_begin_node(fdt, "") >= 0);
495
496 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
497 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
498 ut_assertok(fdt_end_node(fdt));
499
500 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
501 ut_assertok(fdt_end_node(fdt));
502
503 ut_assertok(fdt_end_node(fdt));
504 ut_assertok(fdt_finish(fdt));
505
506 return 0;
507}
508
509static int dm_test_ofnode_root(struct unit_test_state *uts)
510{
511 struct device_node *root = NULL;
512 char fdt[256];
513 oftree tree;
514 ofnode node;
515
516 /* Check that aliases work on the control FDT */
517 node = ofnode_get_aliases_node("ethernet3");
518 ut_assert(ofnode_valid(node));
519 ut_asserteq_str("sbe5", ofnode_get_name(node));
520
Simon Glass2b9b14582022-09-06 20:27:21 -0600521 ut_assert(!oftree_valid(oftree_null()));
522
Simon Glassef75c592022-07-30 15:52:08 -0600523 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
524 if (of_live_active()) {
525 ut_assertok(unflatten_device_tree(fdt, &root));
Simon Glass2b9b14582022-09-06 20:27:21 -0600526 tree = oftree_from_np(root);
Simon Glassef75c592022-07-30 15:52:08 -0600527 } else {
Simon Glass2b9b14582022-09-06 20:27:21 -0600528 tree = oftree_from_fdt(fdt);
Simon Glassef75c592022-07-30 15:52:08 -0600529 }
Simon Glass2b9b14582022-09-06 20:27:21 -0600530 ut_assert(oftree_valid(tree));
Simon Glassef75c592022-07-30 15:52:08 -0600531
532 /* Make sure they don't work on this new tree */
533 node = ofnode_path_root(tree, "mmc0");
534 ut_assert(!ofnode_valid(node));
535
536 /* It should appear in the new tree */
537 node = ofnode_path_root(tree, "/new-mmc");
538 ut_assert(ofnode_valid(node));
539
540 /* ...and not in the control FDT */
541 node = ofnode_path_root(oftree_default(), "/new-mmc");
542 ut_assert(!ofnode_valid(node));
543
544 free(root);
545
546 return 0;
547}
548DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
Simon Glass270a4432022-07-30 15:52:09 -0600549
550static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
551{
552 struct udevice *dev;
553 ofnode node;
554
Simon Glass270a4432022-07-30 15:52:09 -0600555 /* Test enabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600556 node = ofnode_path("/usb@2");
557
Simon Glass3ee3d152022-07-30 15:52:13 -0600558 ut_assert(!ofnode_is_enabled(node));
559 ut_assertok(ofnode_set_enabled(node, true));
560 ut_asserteq(true, ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600561
562 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
563 &dev);
564 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
565
566 /* Test string property setting */
Simon Glass270a4432022-07-30 15:52:09 -0600567 ut_assert(device_is_compatible(dev, "sandbox,usb"));
568 ofnode_write_string(node, "compatible", "gdsys,super-usb");
569 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
570 ofnode_write_string(node, "compatible", "sandbox,usb");
571 ut_assert(device_is_compatible(dev, "sandbox,usb"));
572
573 /* Test setting generic properties */
574
575 /* Non-existent in DTB */
576 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
577 /* reg = 0x42, size = 0x100 */
Simon Glass5e2cd5e2022-07-30 15:52:10 -0600578 ut_assertok(ofnode_write_prop(node, "reg",
579 "\x00\x00\x00\x42\x00\x00\x01\x00", 8));
Simon Glass270a4432022-07-30 15:52:09 -0600580 ut_asserteq(0x42, dev_read_addr(dev));
581
582 /* Test disabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600583 device_remove(dev, DM_REMOVE_NORMAL);
584 device_unbind(dev);
585
Simon Glass3ee3d152022-07-30 15:52:13 -0600586 ut_assert(ofnode_is_enabled(node));
587 ut_assertok(ofnode_set_enabled(node, false));
588 ut_assert(!ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600589
590 return 0;
591}
Simon Glassb75dc162022-07-30 15:52:11 -0600592DM_TEST(dm_test_ofnode_livetree_writing,
Simon Glass8ae9f962022-09-06 20:27:07 -0600593 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd28e31e2022-07-30 15:52:14 -0600594
595static int dm_test_ofnode_u32(struct unit_test_state *uts)
596{
597 ofnode node;
Simon Glasse3be5fc2022-09-06 20:27:18 -0600598 u32 val;
Simon Glassd28e31e2022-07-30 15:52:14 -0600599
600 node = ofnode_path("/lcd");
601 ut_assert(ofnode_valid(node));
602 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
603 ut_assertok(ofnode_write_u32(node, "xres", 1367));
604 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
605 ut_assertok(ofnode_write_u32(node, "xres", 1366));
606
Simon Glasse3be5fc2022-09-06 20:27:18 -0600607 node = ofnode_path("/backlight");
608 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
609 ut_asserteq(0, val);
610 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
611 ut_asserteq(16, val);
612 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
613 ut_asserteq(255, val);
614 ut_asserteq(-EOVERFLOW,
615 ofnode_read_u32_index(node, "brightness-levels", 9, &val));
616 ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
617
Simon Glassd28e31e2022-07-30 15:52:14 -0600618 return 0;
619}
Simon Glass8ae9f962022-09-06 20:27:07 -0600620DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass56bc3322022-09-06 20:27:02 -0600621
Simon Glasse3be5fc2022-09-06 20:27:18 -0600622static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
623{
624 ofnode node;
625 u32 val[10];
626
627 node = ofnode_path("/a-test");
628 ut_assert(ofnode_valid(node));
629 ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
630 ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
631 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
632 1));
633
634 memset(val, '\0', sizeof(val));
635 ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
636 ut_asserteq(0, val[0]);
637 ut_asserteq(5678, val[1]);
638 ut_asserteq(9123, val[2]);
639 ut_asserteq(4567, val[3]);
640 ut_asserteq(0, val[4]);
641 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
642 4));
643
644 return 0;
645}
646DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
647
648static int dm_test_ofnode_u64(struct unit_test_state *uts)
649{
650 ofnode node;
651 u64 val;
652
653 node = ofnode_path("/a-test");
654 ut_assert(ofnode_valid(node));
655 ut_assertok(ofnode_read_u64(node, "int64-value", &val));
656 ut_asserteq_64(0x1111222233334444, val);
657 ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
658
659 return 0;
660}
661DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
662
Simon Glass56bc3322022-09-06 20:27:02 -0600663static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
664{
665 ofnode node, check, subnode;
666 char buf[128];
667
Simon Glass56bc3322022-09-06 20:27:02 -0600668 node = ofnode_path("/lcd");
669 ut_assert(ofnode_valid(node));
670 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
671 check = ofnode_path("/lcd/edmund");
672 ut_asserteq(subnode.of_offset, check.of_offset);
673 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
674 ut_asserteq_str("/lcd/edmund", buf);
675
676 if (of_live_active()) {
677 struct device_node *child;
678
679 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
680 2, &child));
681 ut_asserteq_str("ed", child->name);
682 ut_asserteq_str("/lcd/ed", child->full_name);
683 check = ofnode_path("/lcd/ed");
684 ut_asserteq_ptr(child, check.np);
685 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
686 sizeof(buf)));
687 ut_asserteq_str("/lcd/ed", buf);
688 }
689
690 /* An existing node should be returned with -EEXIST */
691 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
692 ut_asserteq(subnode.of_offset, check.of_offset);
693
694 /* add a root node */
695 node = ofnode_path("/");
696 ut_assert(ofnode_valid(node));
697 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
698 check = ofnode_path("/lcd2");
699 ut_asserteq(subnode.of_offset, check.of_offset);
700 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
701 ut_asserteq_str("/lcd2", buf);
702
703 if (of_live_active()) {
704 ulong start;
705 int i;
706
707 /*
708 * Make sure each of the three malloc()checks in
709 * of_add_subnode() work
710 */
711 for (i = 0; i < 3; i++) {
712 malloc_enable_testing(i);
713 start = ut_check_free();
714 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
715 &check));
716 ut_assertok(ut_check_delta(start));
717 }
718
719 /* This should pass since we allow 3 allocations */
720 malloc_enable_testing(3);
721 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
722 malloc_disable_testing();
723 }
724
Simon Glass238afb52022-09-06 20:27:03 -0600725 /* write to the empty node */
726 ut_assertok(ofnode_write_string(subnode, "example", "text"));
727
Simon Glass56bc3322022-09-06 20:27:02 -0600728 return 0;
729}
Simon Glass8ae9f962022-09-06 20:27:07 -0600730DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass4caa79a2022-09-06 20:27:16 -0600731
732static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
733{
734 ofnode node, subnode;
735 struct ofprop prop;
736 int count;
737
738 node = ofnode_path("/buttons");
739 count = 0;
740
741 /* we expect "compatible" for each node */
742 ofnode_for_each_prop(prop, node)
743 count++;
744 ut_asserteq(1, count);
745
746 /* there are two nodes, each with 2 properties */
747 ofnode_for_each_subnode(subnode, node)
748 ofnode_for_each_prop(prop, subnode)
749 count++;
750 ut_asserteq(5, count);
751
752 return 0;
753}
754DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
Simon Glasse3be5fc2022-09-06 20:27:18 -0600755
756static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
757{
758 const char *compat = "denx,u-boot-fdt-test";
759 ofnode node;
760 int count;
761
762 count = 0;
763 for (node = ofnode_null();
764 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
765 count++;
766 ut_asserteq(11, count);
767
768 return 0;
769}
770DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
771
772static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
773{
774 ofnode node, subnode;
775
776 node = ofnode_path("/buttons");
777
778 subnode = ofnode_find_subnode(node, "btn1");
779 ut_assert(ofnode_valid(subnode));
780 ut_asserteq_str("btn1", ofnode_get_name(subnode));
781
782 subnode = ofnode_find_subnode(node, "btn");
783 ut_assert(!ofnode_valid(subnode));
784
785 return 0;
786}
787DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
788
789static int dm_test_ofnode_get_name(struct unit_test_state *uts)
790{
791 ofnode node;
792
793 node = ofnode_path("/buttons");
794 ut_assert(ofnode_valid(node));
795 ut_asserteq_str("buttons", ofnode_get_name(node));
796 ut_asserteq_str("", ofnode_get_name(ofnode_root()));
797
798 return 0;
799}
800DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);