blob: b73ab9882871ef164ff6086c7cafdf380f31d1e3 [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
Simon Glass95fd2092022-09-06 20:27:22 -060039 ut_assert(ofnode_valid(oftree_get_by_phandle(oftree_default(), 1)));
40
Patrick Delaunay04fcfe72020-09-24 17:26:20 +020041 return 0;
42}
43DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
44
Jens Wiklanderd9fd0ac2018-08-20 11:10:00 +020045static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
46{
47 const char propname[] = "compatible";
48 const char propval[] = "denx,u-boot-fdt-test";
49 const char *str;
50 ofnode node = ofnode_null();
51
52 /* Find first matching node, there should be at least one */
53 node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
54 ut_assert(ofnode_valid(node));
55 str = ofnode_read_string(node, propname);
56 ut_assert(str && !strcmp(str, propval));
57
58 /* Find the rest of the matching nodes */
59 while (true) {
60 node = ofnode_by_prop_value(node, propname, propval,
61 sizeof(propval));
62 if (!ofnode_valid(node))
63 break;
64 str = ofnode_read_string(node, propname);
65 ut_assert(str && !strcmp(str, propval));
66 }
67
68 return 0;
69}
Simon Glass974dccd2020-07-28 19:41:12 -060070DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
Simon Glass699c9ca2018-10-01 12:22:08 -060071
72static int dm_test_ofnode_fmap(struct unit_test_state *uts)
73{
74 struct fmap_entry entry;
75 ofnode node;
76
77 node = ofnode_path("/cros-ec/flash");
78 ut_assert(ofnode_valid(node));
79 ut_assertok(ofnode_read_fmap_entry(node, &entry));
80 ut_asserteq(0x08000000, entry.offset);
81 ut_asserteq(0x20000, entry.length);
82
83 return 0;
84}
Simon Glass974dccd2020-07-28 19:41:12 -060085DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf3455962020-01-27 08:49:43 -070086
Simon Glass0c2e9802020-01-27 08:49:44 -070087static int dm_test_ofnode_read(struct unit_test_state *uts)
88{
89 const u32 *val;
90 ofnode node;
91 int size;
92
93 node = ofnode_path("/a-test");
94 ut_assert(ofnode_valid(node));
95
96 val = ofnode_read_prop(node, "int-value", &size);
97 ut_assertnonnull(val);
98 ut_asserteq(4, size);
99 ut_asserteq(1234, fdt32_to_cpu(val[0]));
100
101 val = ofnode_read_prop(node, "missing", &size);
102 ut_assertnull(val);
103 ut_asserteq(-FDT_ERR_NOTFOUND, size);
104
105 /* Check it works without a size parameter */
106 val = ofnode_read_prop(node, "missing", NULL);
107 ut_assertnull(val);
108
109 return 0;
110}
Simon Glass974dccd2020-07-28 19:41:12 -0600111DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0c2e9802020-01-27 08:49:44 -0700112
Patrick Delaunay8cd28012020-09-25 09:41:16 +0200113static int dm_test_ofnode_phandle(struct unit_test_state *uts)
114{
115 struct ofnode_phandle_args args;
116 ofnode node;
117 int ret;
118 const char prop[] = "test-gpios";
119 const char cell[] = "#gpio-cells";
120 const char prop2[] = "phandle-value";
121
122 node = ofnode_path("/a-test");
123 ut_assert(ofnode_valid(node));
124
125 /* Test ofnode_count_phandle_with_args with cell name */
126 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
127 ut_asserteq(-ENOENT, ret);
128 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
129 ut_asserteq(-EINVAL, ret);
130 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
131 ut_asserteq(5, ret);
132
133 /* Test ofnode_parse_phandle_with_args with cell name */
134 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
135 &args);
136 ut_asserteq(-ENOENT, ret);
137 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
138 &args);
139 ut_asserteq(-EINVAL, ret);
140 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
141 ut_assertok(ret);
142 ut_asserteq(1, args.args_count);
143 ut_asserteq(1, args.args[0]);
144 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
145 ut_assertok(ret);
146 ut_asserteq(1, args.args_count);
147 ut_asserteq(4, args.args[0]);
148 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
149 ut_assertok(ret);
150 ut_asserteq(5, args.args_count);
151 ut_asserteq(5, args.args[0]);
152 ut_asserteq(1, args.args[4]);
153 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
154 ut_asserteq(-ENOENT, ret);
155 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
156 ut_assertok(ret);
157 ut_asserteq(1, args.args_count);
158 ut_asserteq(12, args.args[0]);
159 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
160 ut_asserteq(-ENOENT, ret);
161
162 /* Test ofnode_count_phandle_with_args with cell count */
163 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
164 ut_asserteq(-ENOENT, ret);
165 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
166 ut_asserteq(3, ret);
167
168 /* Test ofnode_parse_phandle_with_args with cell count */
169 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
170 ut_assertok(ret);
171 ut_asserteq(1, ofnode_valid(args.node));
172 ut_asserteq(1, args.args_count);
173 ut_asserteq(10, args.args[0]);
174 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
175 ut_asserteq(-EINVAL, ret);
176 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
177 ut_assertok(ret);
178 ut_asserteq(1, ofnode_valid(args.node));
179 ut_asserteq(1, args.args_count);
180 ut_asserteq(30, args.args[0]);
181 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
182 ut_asserteq(-ENOENT, ret);
183
184 return 0;
185}
186DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
187
Simon Glassf3455962020-01-27 08:49:43 -0700188static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
189{
190 const char *str;
Simon Glasse09223c2020-01-27 08:49:46 -0700191 const u32 *val;
Simon Glassf3455962020-01-27 08:49:43 -0700192 ofnode node;
Simon Glasse09223c2020-01-27 08:49:46 -0700193 int size;
Simon Glassf3455962020-01-27 08:49:43 -0700194
195 str = ofnode_read_chosen_string("setting");
196 ut_assertnonnull(str);
197 ut_asserteq_str("sunrise ohoka", str);
198 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
199
200 node = ofnode_get_chosen_node("other-node");
201 ut_assert(ofnode_valid(node));
202 ut_asserteq_str("c-test@5", ofnode_get_name(node));
203
204 node = ofnode_get_chosen_node("setting");
205 ut_assert(!ofnode_valid(node));
206
Simon Glasse09223c2020-01-27 08:49:46 -0700207 val = ofnode_read_chosen_prop("int-values", &size);
208 ut_assertnonnull(val);
209 ut_asserteq(8, size);
210 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
211 ut_asserteq(72993, fdt32_to_cpu(val[1]));
212
Simon Glassf3455962020-01-27 08:49:43 -0700213 return 0;
214}
Simon Glass974dccd2020-07-28 19:41:12 -0600215DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
developercf8bc132020-05-02 11:35:10 +0200216
Michal Simek92a88622020-07-28 12:51:08 +0200217static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
218{
219 const void *val;
220 ofnode node;
221 int size;
222
Michael Walle7efcdfd2021-02-25 16:51:11 +0100223 node = ofnode_get_aliases_node("ethernet3");
Michal Simek92a88622020-07-28 12:51:08 +0200224 ut_assert(ofnode_valid(node));
225 ut_asserteq_str("sbe5", ofnode_get_name(node));
226
227 node = ofnode_get_aliases_node("unknown");
228 ut_assert(!ofnode_valid(node));
229
230 val = ofnode_read_aliases_prop("spi0", &size);
231 ut_assertnonnull(val);
232 ut_asserteq(7, size);
233 ut_asserteq_str("/spi@0", (const char *)val);
234
235 return 0;
236}
237DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
238
developercf8bc132020-05-02 11:35:10 +0200239static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
240{
241 ofnode node, child_node;
242 u32 val;
243
244 node = ofnode_path("/i-test");
245 ut_assert(ofnode_valid(node));
246
247 val = ofnode_get_child_count(node);
248 ut_asserteq(3, val);
249
250 child_node = ofnode_first_subnode(node);
251 ut_assert(ofnode_valid(child_node));
252 val = ofnode_get_child_count(child_node);
253 ut_asserteq(0, val);
254
255 return 0;
256}
257DM_TEST(dm_test_ofnode_get_child_count,
Simon Glass974dccd2020-07-28 19:41:12 -0600258 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass5de5b3b2020-11-28 17:50:02 -0700259
260static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
261{
262 ofnode root_node = ofnode_path("/");
263 ofnode node = ofnode_path("/usb@0");
264
265 ut_assert(ofnode_is_enabled(root_node));
266 ut_assert(!ofnode_is_enabled(node));
267
268 return 0;
269}
270DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800271
272static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
273{
274 ofnode node;
275 fdt_addr_t addr;
276 fdt_size_t size;
277
278 node = ofnode_path("/translation-test@8000");
279 ut_assert(ofnode_valid(node));
280 addr = ofnode_get_addr(node);
281 size = ofnode_get_size(node);
282 ut_asserteq(0x8000, addr);
283 ut_asserteq(0x4000, size);
284
285 node = ofnode_path("/translation-test@8000/dev@1,100");
286 ut_assert(ofnode_valid(node));
287 addr = ofnode_get_addr(node);
288 size = ofnode_get_size(node);
289 ut_asserteq(0x9000, addr);
290 ut_asserteq(0x1000, size);
291
292 node = ofnode_path("/emul-mux-controller");
293 ut_assert(ofnode_valid(node));
294 addr = ofnode_get_addr(node);
295 size = ofnode_get_size(node);
Patrice Chotardb4c52112022-01-04 08:42:48 +0100296 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800297 ut_asserteq(FDT_SIZE_T_NONE, size);
298
Marek Behún177ab7f2021-05-26 14:08:17 +0200299 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
300 ut_assert(ofnode_valid(node));
301 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
302 ut_asserteq_64(0x42, addr);
303
Chen Guanqiaobe0512f2021-04-12 14:51:12 +0800304 return 0;
305}
306DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Marek Behúne897e3c2021-05-26 14:08:18 +0200307
308static int dm_test_ofnode_get_path(struct unit_test_state *uts)
309{
310 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
311 char buf[64];
312 ofnode node;
313 int res;
314
315 node = ofnode_path(path);
316 ut_assert(ofnode_valid(node));
317
318 res = ofnode_get_path(node, buf, 64);
319 ut_asserteq(0, res);
320 ut_asserteq_str(path, buf);
321
322 res = ofnode_get_path(node, buf, 32);
323 ut_asserteq(-ENOSPC, res);
324
Simon Glasse3be5fc2022-09-06 20:27:18 -0600325 res = ofnode_get_path(ofnode_root(), buf, 32);
326 ut_asserteq_str("/", buf);
327
Marek Behúne897e3c2021-05-26 14:08:18 +0200328 return 0;
329}
330DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0034d962021-08-07 07:24:01 -0600331
332static int dm_test_ofnode_conf(struct unit_test_state *uts)
333{
334 ut_assert(!ofnode_conf_read_bool("missing"));
335 ut_assert(ofnode_conf_read_bool("testing-bool"));
336
337 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
338 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
339
340 ut_assertnull(ofnode_conf_read_str("missing"));
341 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
342
343 return 0;
344}
345DM_TEST(dm_test_ofnode_conf, 0);
Michael Wallef1f87402021-10-15 15:15:18 +0200346
347static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
348{
349 const char compatible[] = "denx,u-boot-fdt-test";
350 bool found = false;
351 ofnode node;
352
353 ofnode_for_each_compatible_node(node, compatible) {
354 ut_assert(ofnode_device_is_compatible(node, compatible));
355 found = true;
356 }
357
358 /* There should be at least one matching node */
359 ut_assert(found);
360
361 return 0;
362}
363DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
Simon Glass73025392021-10-23 17:26:04 -0600364
365static int dm_test_ofnode_string(struct unit_test_state *uts)
366{
Simon Glass9580bfc2021-10-23 17:26:07 -0600367 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600368 const char *out;
369 ofnode node;
370
371 node = ofnode_path("/a-test");
372 ut_assert(ofnode_valid(node));
373
374 /* single string */
375 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
376 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
377 ut_asserteq_str("test string", out);
378 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
379 "test string"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600380 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
381 ut_asserteq_str("test string", val[0]);
382 ut_assertnull(val[1]);
383 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600384
385 /* list of strings */
386 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
387 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
388 &out));
389 ut_asserteq_str("mux0", out);
390 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
391 "mux0"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600392 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
393 &val));
394 ut_asserteq_str("mux0", val[0]);
395 ut_asserteq_str("mux1", val[1]);
396 ut_asserteq_str("mux2", val[2]);
397 ut_asserteq_str("mux3", val[3]);
398 ut_asserteq_str("mux4", val[4]);
399 ut_assertnull(val[5]);
400 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600401
402 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
403 &out));
404 ut_asserteq_str("mux4", out);
405 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
406 "mux4"));
407
408 return 0;
409}
410DM_TEST(dm_test_ofnode_string, 0);
411
412static int dm_test_ofnode_string_err(struct unit_test_state *uts)
413{
Simon Glass9580bfc2021-10-23 17:26:07 -0600414 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600415 const char *out;
416 ofnode node;
417
418 /*
419 * Test error codes only on livetree, as they are different with
420 * flattree
421 */
422 node = ofnode_path("/a-test");
423 ut_assert(ofnode_valid(node));
424
425 /* non-existent property */
426 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
427 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
428 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600429 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
Simon Glass73025392021-10-23 17:26:04 -0600430
431 /* empty property */
432 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
433 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
434 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600435 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
436 &val));
Simon Glass73025392021-10-23 17:26:04 -0600437
438 /* badly formatted string list */
439 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
440 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
441 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600442 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
443 &val));
Simon Glass73025392021-10-23 17:26:04 -0600444
445 /* out of range / not found */
446 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
447 &out));
448 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
449 "other"));
450
451 /* negative value for index is not allowed, so don't test for that */
452
453 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
454 "mux-control-names", 5,
455 &out));
456
457 return 0;
458}
459DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200460
461static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
462{
463 ofnode eth_node, phy_node;
Marek Behúnbc194772022-04-07 00:33:01 +0200464 phy_interface_t mode;
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200465 u32 reg;
466
467 eth_node = ofnode_path("/phy-test-eth");
468 ut_assert(ofnode_valid(eth_node));
469
Marek Behúnbc194772022-04-07 00:33:01 +0200470 mode = ofnode_read_phy_mode(eth_node);
471 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
472
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200473 phy_node = ofnode_get_phy_node(eth_node);
474 ut_assert(ofnode_valid(phy_node));
475
476 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
477 ut_asserteq_64(0x1, reg);
478
479 return 0;
480}
481DM_TEST(dm_test_ofnode_get_phy, 0);
Simon Glassef75c592022-07-30 15:52:08 -0600482
483/**
484 * make_ofnode_fdt() - Create an FDT for testing with ofnode
485 *
486 * The size is set to the minimum needed
487 *
488 * @uts: Test state
489 * @fdt: Place to write FDT
490 * @size: Maximum size of space for fdt
491 */
492static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
493{
494 ut_assertok(fdt_create(fdt, size));
495 ut_assertok(fdt_finish_reservemap(fdt));
496 ut_assert(fdt_begin_node(fdt, "") >= 0);
497
498 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
499 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
500 ut_assertok(fdt_end_node(fdt));
501
502 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
503 ut_assertok(fdt_end_node(fdt));
504
505 ut_assertok(fdt_end_node(fdt));
506 ut_assertok(fdt_finish(fdt));
507
508 return 0;
509}
510
511static int dm_test_ofnode_root(struct unit_test_state *uts)
512{
513 struct device_node *root = NULL;
514 char fdt[256];
515 oftree tree;
516 ofnode node;
517
518 /* Check that aliases work on the control FDT */
519 node = ofnode_get_aliases_node("ethernet3");
520 ut_assert(ofnode_valid(node));
521 ut_asserteq_str("sbe5", ofnode_get_name(node));
522
Simon Glass2b9b14582022-09-06 20:27:21 -0600523 ut_assert(!oftree_valid(oftree_null()));
524
Simon Glassef75c592022-07-30 15:52:08 -0600525 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
526 if (of_live_active()) {
527 ut_assertok(unflatten_device_tree(fdt, &root));
Simon Glass2b9b14582022-09-06 20:27:21 -0600528 tree = oftree_from_np(root);
Simon Glassef75c592022-07-30 15:52:08 -0600529 } else {
Simon Glass2b9b14582022-09-06 20:27:21 -0600530 tree = oftree_from_fdt(fdt);
Simon Glassef75c592022-07-30 15:52:08 -0600531 }
Simon Glass2b9b14582022-09-06 20:27:21 -0600532 ut_assert(oftree_valid(tree));
Simon Glassef75c592022-07-30 15:52:08 -0600533
534 /* Make sure they don't work on this new tree */
Simon Glass45ae59d2022-09-06 20:27:24 -0600535 node = oftree_path(tree, "mmc0");
Simon Glassef75c592022-07-30 15:52:08 -0600536 ut_assert(!ofnode_valid(node));
537
538 /* It should appear in the new tree */
Simon Glass45ae59d2022-09-06 20:27:24 -0600539 node = oftree_path(tree, "/new-mmc");
Simon Glassef75c592022-07-30 15:52:08 -0600540 ut_assert(ofnode_valid(node));
541
542 /* ...and not in the control FDT */
Simon Glass45ae59d2022-09-06 20:27:24 -0600543 node = oftree_path(oftree_default(), "/new-mmc");
Simon Glassef75c592022-07-30 15:52:08 -0600544 ut_assert(!ofnode_valid(node));
545
546 free(root);
547
548 return 0;
549}
550DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
Simon Glass270a4432022-07-30 15:52:09 -0600551
552static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
553{
554 struct udevice *dev;
555 ofnode node;
556
Simon Glass270a4432022-07-30 15:52:09 -0600557 /* Test enabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600558 node = ofnode_path("/usb@2");
559
Simon Glass3ee3d152022-07-30 15:52:13 -0600560 ut_assert(!ofnode_is_enabled(node));
561 ut_assertok(ofnode_set_enabled(node, true));
562 ut_asserteq(true, ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600563
564 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
565 &dev);
566 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
567
568 /* Test string property setting */
Simon Glass270a4432022-07-30 15:52:09 -0600569 ut_assert(device_is_compatible(dev, "sandbox,usb"));
570 ofnode_write_string(node, "compatible", "gdsys,super-usb");
571 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
572 ofnode_write_string(node, "compatible", "sandbox,usb");
573 ut_assert(device_is_compatible(dev, "sandbox,usb"));
574
575 /* Test setting generic properties */
576
577 /* Non-existent in DTB */
578 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
579 /* reg = 0x42, size = 0x100 */
Simon Glass5e2cd5e2022-07-30 15:52:10 -0600580 ut_assertok(ofnode_write_prop(node, "reg",
581 "\x00\x00\x00\x42\x00\x00\x01\x00", 8));
Simon Glass270a4432022-07-30 15:52:09 -0600582 ut_asserteq(0x42, dev_read_addr(dev));
583
584 /* Test disabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600585 device_remove(dev, DM_REMOVE_NORMAL);
586 device_unbind(dev);
587
Simon Glass3ee3d152022-07-30 15:52:13 -0600588 ut_assert(ofnode_is_enabled(node));
589 ut_assertok(ofnode_set_enabled(node, false));
590 ut_assert(!ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600591
592 return 0;
593}
Simon Glassb75dc162022-07-30 15:52:11 -0600594DM_TEST(dm_test_ofnode_livetree_writing,
Simon Glass8ae9f962022-09-06 20:27:07 -0600595 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd28e31e2022-07-30 15:52:14 -0600596
597static int dm_test_ofnode_u32(struct unit_test_state *uts)
598{
599 ofnode node;
Simon Glasse3be5fc2022-09-06 20:27:18 -0600600 u32 val;
Simon Glassd28e31e2022-07-30 15:52:14 -0600601
602 node = ofnode_path("/lcd");
603 ut_assert(ofnode_valid(node));
604 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
605 ut_assertok(ofnode_write_u32(node, "xres", 1367));
606 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
607 ut_assertok(ofnode_write_u32(node, "xres", 1366));
608
Simon Glasse3be5fc2022-09-06 20:27:18 -0600609 node = ofnode_path("/backlight");
610 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 0, &val));
611 ut_asserteq(0, val);
612 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 1, &val));
613 ut_asserteq(16, val);
614 ut_assertok(ofnode_read_u32_index(node, "brightness-levels", 8, &val));
615 ut_asserteq(255, val);
616 ut_asserteq(-EOVERFLOW,
617 ofnode_read_u32_index(node, "brightness-levels", 9, &val));
618 ut_asserteq(-EINVAL, ofnode_read_u32_index(node, "missing", 0, &val));
619
Simon Glassd28e31e2022-07-30 15:52:14 -0600620 return 0;
621}
Simon Glass8ae9f962022-09-06 20:27:07 -0600622DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass56bc3322022-09-06 20:27:02 -0600623
Simon Glasse3be5fc2022-09-06 20:27:18 -0600624static int dm_test_ofnode_u32_array(struct unit_test_state *uts)
625{
626 ofnode node;
627 u32 val[10];
628
629 node = ofnode_path("/a-test");
630 ut_assert(ofnode_valid(node));
631 ut_assertok(ofnode_read_u32_array(node, "int-value", val, 1));
632 ut_asserteq(-EINVAL, ofnode_read_u32_array(node, "missing", val, 1));
633 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "bool-value", val,
634 1));
635
636 memset(val, '\0', sizeof(val));
637 ut_assertok(ofnode_read_u32_array(node, "int-array", val + 1, 3));
638 ut_asserteq(0, val[0]);
639 ut_asserteq(5678, val[1]);
640 ut_asserteq(9123, val[2]);
641 ut_asserteq(4567, val[3]);
642 ut_asserteq(0, val[4]);
643 ut_asserteq(-EOVERFLOW, ofnode_read_u32_array(node, "int-array", val,
644 4));
645
646 return 0;
647}
648DM_TEST(dm_test_ofnode_u32_array, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
649
650static int dm_test_ofnode_u64(struct unit_test_state *uts)
651{
652 ofnode node;
653 u64 val;
654
655 node = ofnode_path("/a-test");
656 ut_assert(ofnode_valid(node));
657 ut_assertok(ofnode_read_u64(node, "int64-value", &val));
658 ut_asserteq_64(0x1111222233334444, val);
659 ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val));
660
661 return 0;
662}
663DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT);
664
Simon Glass56bc3322022-09-06 20:27:02 -0600665static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
666{
667 ofnode node, check, subnode;
668 char buf[128];
669
Simon Glass56bc3322022-09-06 20:27:02 -0600670 node = ofnode_path("/lcd");
671 ut_assert(ofnode_valid(node));
672 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
673 check = ofnode_path("/lcd/edmund");
674 ut_asserteq(subnode.of_offset, check.of_offset);
675 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
676 ut_asserteq_str("/lcd/edmund", buf);
677
678 if (of_live_active()) {
679 struct device_node *child;
680
681 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
682 2, &child));
683 ut_asserteq_str("ed", child->name);
684 ut_asserteq_str("/lcd/ed", child->full_name);
685 check = ofnode_path("/lcd/ed");
686 ut_asserteq_ptr(child, check.np);
687 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
688 sizeof(buf)));
689 ut_asserteq_str("/lcd/ed", buf);
690 }
691
692 /* An existing node should be returned with -EEXIST */
693 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
694 ut_asserteq(subnode.of_offset, check.of_offset);
695
696 /* add a root node */
697 node = ofnode_path("/");
698 ut_assert(ofnode_valid(node));
699 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
700 check = ofnode_path("/lcd2");
701 ut_asserteq(subnode.of_offset, check.of_offset);
702 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
703 ut_asserteq_str("/lcd2", buf);
704
705 if (of_live_active()) {
706 ulong start;
707 int i;
708
709 /*
710 * Make sure each of the three malloc()checks in
711 * of_add_subnode() work
712 */
713 for (i = 0; i < 3; i++) {
714 malloc_enable_testing(i);
715 start = ut_check_free();
716 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
717 &check));
718 ut_assertok(ut_check_delta(start));
719 }
720
721 /* This should pass since we allow 3 allocations */
722 malloc_enable_testing(3);
723 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
724 malloc_disable_testing();
725 }
726
Simon Glass238afb52022-09-06 20:27:03 -0600727 /* write to the empty node */
728 ut_assertok(ofnode_write_string(subnode, "example", "text"));
729
Simon Glass56bc3322022-09-06 20:27:02 -0600730 return 0;
731}
Simon Glass8ae9f962022-09-06 20:27:07 -0600732DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass4caa79a2022-09-06 20:27:16 -0600733
734static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
735{
736 ofnode node, subnode;
737 struct ofprop prop;
738 int count;
739
740 node = ofnode_path("/buttons");
741 count = 0;
742
743 /* we expect "compatible" for each node */
744 ofnode_for_each_prop(prop, node)
745 count++;
746 ut_asserteq(1, count);
747
748 /* there are two nodes, each with 2 properties */
749 ofnode_for_each_subnode(subnode, node)
750 ofnode_for_each_prop(prop, subnode)
751 count++;
752 ut_asserteq(5, count);
753
754 return 0;
755}
756DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
Simon Glasse3be5fc2022-09-06 20:27:18 -0600757
758static int dm_test_ofnode_by_compatible(struct unit_test_state *uts)
759{
760 const char *compat = "denx,u-boot-fdt-test";
761 ofnode node;
762 int count;
763
764 count = 0;
765 for (node = ofnode_null();
766 node = ofnode_by_compatible(node, compat), ofnode_valid(node);)
767 count++;
768 ut_asserteq(11, count);
769
770 return 0;
771}
772DM_TEST(dm_test_ofnode_by_compatible, UT_TESTF_SCAN_FDT);
773
774static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
775{
776 ofnode node, subnode;
777
778 node = ofnode_path("/buttons");
779
780 subnode = ofnode_find_subnode(node, "btn1");
781 ut_assert(ofnode_valid(subnode));
782 ut_asserteq_str("btn1", ofnode_get_name(subnode));
783
784 subnode = ofnode_find_subnode(node, "btn");
785 ut_assert(!ofnode_valid(subnode));
786
787 return 0;
788}
789DM_TEST(dm_test_ofnode_find_subnode, UT_TESTF_SCAN_FDT);
790
791static int dm_test_ofnode_get_name(struct unit_test_state *uts)
792{
793 ofnode node;
794
795 node = ofnode_path("/buttons");
796 ut_assert(ofnode_valid(node));
797 ut_asserteq_str("buttons", ofnode_get_name(node));
798 ut_asserteq_str("", ofnode_get_name(ofnode_root()));
799
800 return 0;
801}
802DM_TEST(dm_test_ofnode_get_name, UT_TESTF_SCAN_FDT);