blob: 0f65ff939fb934191fb29c17a051d9e6c125f5ec [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
323 return 0;
324}
325DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0034d962021-08-07 07:24:01 -0600326
327static int dm_test_ofnode_conf(struct unit_test_state *uts)
328{
329 ut_assert(!ofnode_conf_read_bool("missing"));
330 ut_assert(ofnode_conf_read_bool("testing-bool"));
331
332 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
333 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
334
335 ut_assertnull(ofnode_conf_read_str("missing"));
336 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
337
338 return 0;
339}
340DM_TEST(dm_test_ofnode_conf, 0);
Michael Wallef1f87402021-10-15 15:15:18 +0200341
342static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
343{
344 const char compatible[] = "denx,u-boot-fdt-test";
345 bool found = false;
346 ofnode node;
347
348 ofnode_for_each_compatible_node(node, compatible) {
349 ut_assert(ofnode_device_is_compatible(node, compatible));
350 found = true;
351 }
352
353 /* There should be at least one matching node */
354 ut_assert(found);
355
356 return 0;
357}
358DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
Simon Glass73025392021-10-23 17:26:04 -0600359
360static int dm_test_ofnode_string(struct unit_test_state *uts)
361{
Simon Glass9580bfc2021-10-23 17:26:07 -0600362 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600363 const char *out;
364 ofnode node;
365
366 node = ofnode_path("/a-test");
367 ut_assert(ofnode_valid(node));
368
369 /* single string */
370 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
371 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
372 ut_asserteq_str("test string", out);
373 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
374 "test string"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600375 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
376 ut_asserteq_str("test string", val[0]);
377 ut_assertnull(val[1]);
378 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600379
380 /* list of strings */
381 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
382 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
383 &out));
384 ut_asserteq_str("mux0", out);
385 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
386 "mux0"));
Simon Glass9580bfc2021-10-23 17:26:07 -0600387 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
388 &val));
389 ut_asserteq_str("mux0", val[0]);
390 ut_asserteq_str("mux1", val[1]);
391 ut_asserteq_str("mux2", val[2]);
392 ut_asserteq_str("mux3", val[3]);
393 ut_asserteq_str("mux4", val[4]);
394 ut_assertnull(val[5]);
395 free(val);
Simon Glass73025392021-10-23 17:26:04 -0600396
397 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
398 &out));
399 ut_asserteq_str("mux4", out);
400 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
401 "mux4"));
402
403 return 0;
404}
405DM_TEST(dm_test_ofnode_string, 0);
406
407static int dm_test_ofnode_string_err(struct unit_test_state *uts)
408{
Simon Glass9580bfc2021-10-23 17:26:07 -0600409 const char **val;
Simon Glass73025392021-10-23 17:26:04 -0600410 const char *out;
411 ofnode node;
412
413 /*
414 * Test error codes only on livetree, as they are different with
415 * flattree
416 */
417 node = ofnode_path("/a-test");
418 ut_assert(ofnode_valid(node));
419
420 /* non-existent property */
421 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
422 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
423 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600424 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
Simon Glass73025392021-10-23 17:26:04 -0600425
426 /* empty property */
427 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
428 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
429 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600430 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
431 &val));
Simon Glass73025392021-10-23 17:26:04 -0600432
433 /* badly formatted string list */
434 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
435 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
436 &out));
Simon Glass9580bfc2021-10-23 17:26:07 -0600437 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
438 &val));
Simon Glass73025392021-10-23 17:26:04 -0600439
440 /* out of range / not found */
441 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
442 &out));
443 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
444 "other"));
445
446 /* negative value for index is not allowed, so don't test for that */
447
448 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
449 "mux-control-names", 5,
450 &out));
451
452 return 0;
453}
454DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200455
456static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
457{
458 ofnode eth_node, phy_node;
Marek Behúnbc194772022-04-07 00:33:01 +0200459 phy_interface_t mode;
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200460 u32 reg;
461
462 eth_node = ofnode_path("/phy-test-eth");
463 ut_assert(ofnode_valid(eth_node));
464
Marek Behúnbc194772022-04-07 00:33:01 +0200465 mode = ofnode_read_phy_mode(eth_node);
466 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
467
Marek Behúnf4f1ddc2022-04-07 00:32:57 +0200468 phy_node = ofnode_get_phy_node(eth_node);
469 ut_assert(ofnode_valid(phy_node));
470
471 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
472 ut_asserteq_64(0x1, reg);
473
474 return 0;
475}
476DM_TEST(dm_test_ofnode_get_phy, 0);
Simon Glassef75c592022-07-30 15:52:08 -0600477
478/**
479 * make_ofnode_fdt() - Create an FDT for testing with ofnode
480 *
481 * The size is set to the minimum needed
482 *
483 * @uts: Test state
484 * @fdt: Place to write FDT
485 * @size: Maximum size of space for fdt
486 */
487static int make_ofnode_fdt(struct unit_test_state *uts, void *fdt, int size)
488{
489 ut_assertok(fdt_create(fdt, size));
490 ut_assertok(fdt_finish_reservemap(fdt));
491 ut_assert(fdt_begin_node(fdt, "") >= 0);
492
493 ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
494 ut_assertok(fdt_property_string(fdt, "mmc0", "/new-mmc"));
495 ut_assertok(fdt_end_node(fdt));
496
497 ut_assert(fdt_begin_node(fdt, "new-mmc") >= 0);
498 ut_assertok(fdt_end_node(fdt));
499
500 ut_assertok(fdt_end_node(fdt));
501 ut_assertok(fdt_finish(fdt));
502
503 return 0;
504}
505
506static int dm_test_ofnode_root(struct unit_test_state *uts)
507{
508 struct device_node *root = NULL;
509 char fdt[256];
510 oftree tree;
511 ofnode node;
512
513 /* Check that aliases work on the control FDT */
514 node = ofnode_get_aliases_node("ethernet3");
515 ut_assert(ofnode_valid(node));
516 ut_asserteq_str("sbe5", ofnode_get_name(node));
517
518 ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
519 if (of_live_active()) {
520 ut_assertok(unflatten_device_tree(fdt, &root));
521 tree.np = root;
522 } else {
523 tree.fdt = fdt;
524 }
525
526 /* Make sure they don't work on this new tree */
527 node = ofnode_path_root(tree, "mmc0");
528 ut_assert(!ofnode_valid(node));
529
530 /* It should appear in the new tree */
531 node = ofnode_path_root(tree, "/new-mmc");
532 ut_assert(ofnode_valid(node));
533
534 /* ...and not in the control FDT */
535 node = ofnode_path_root(oftree_default(), "/new-mmc");
536 ut_assert(!ofnode_valid(node));
537
538 free(root);
539
540 return 0;
541}
542DM_TEST(dm_test_ofnode_root, UT_TESTF_SCAN_FDT);
Simon Glass270a4432022-07-30 15:52:09 -0600543
544static int dm_test_ofnode_livetree_writing(struct unit_test_state *uts)
545{
546 struct udevice *dev;
547 ofnode node;
548
Simon Glass7f9b5802022-09-06 20:26:59 -0600549 /* temporarily disable this test due to a failure fixed later */
550 if (!of_live_active())
551 return 0;
552
Simon Glass270a4432022-07-30 15:52:09 -0600553 /* Test enabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600554 node = ofnode_path("/usb@2");
555
Simon Glass3ee3d152022-07-30 15:52:13 -0600556 ut_assert(!ofnode_is_enabled(node));
557 ut_assertok(ofnode_set_enabled(node, true));
558 ut_asserteq(true, ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600559
560 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
561 &dev);
562 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
563
564 /* Test string property setting */
Simon Glass270a4432022-07-30 15:52:09 -0600565 ut_assert(device_is_compatible(dev, "sandbox,usb"));
566 ofnode_write_string(node, "compatible", "gdsys,super-usb");
567 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
568 ofnode_write_string(node, "compatible", "sandbox,usb");
569 ut_assert(device_is_compatible(dev, "sandbox,usb"));
570
571 /* Test setting generic properties */
572
573 /* Non-existent in DTB */
574 ut_asserteq_64(FDT_ADDR_T_NONE, dev_read_addr(dev));
575 /* reg = 0x42, size = 0x100 */
Simon Glass5e2cd5e2022-07-30 15:52:10 -0600576 ut_assertok(ofnode_write_prop(node, "reg",
577 "\x00\x00\x00\x42\x00\x00\x01\x00", 8));
Simon Glass270a4432022-07-30 15:52:09 -0600578 ut_asserteq(0x42, dev_read_addr(dev));
579
580 /* Test disabling devices */
Simon Glass270a4432022-07-30 15:52:09 -0600581 device_remove(dev, DM_REMOVE_NORMAL);
582 device_unbind(dev);
583
Simon Glass3ee3d152022-07-30 15:52:13 -0600584 ut_assert(ofnode_is_enabled(node));
585 ut_assertok(ofnode_set_enabled(node, false));
586 ut_assert(!ofnode_is_enabled(node));
Simon Glass270a4432022-07-30 15:52:09 -0600587
588 return 0;
589}
Simon Glassb75dc162022-07-30 15:52:11 -0600590DM_TEST(dm_test_ofnode_livetree_writing,
Simon Glass3ee3d152022-07-30 15:52:13 -0600591 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_OR_FLAT);
Simon Glassd28e31e2022-07-30 15:52:14 -0600592
593static int dm_test_ofnode_u32(struct unit_test_state *uts)
594{
595 ofnode node;
596
597 node = ofnode_path("/lcd");
598 ut_assert(ofnode_valid(node));
599 ut_asserteq(1366, ofnode_read_u32_default(node, "xres", 123));
600 ut_assertok(ofnode_write_u32(node, "xres", 1367));
601 ut_asserteq(1367, ofnode_read_u32_default(node, "xres", 123));
602 ut_assertok(ofnode_write_u32(node, "xres", 1366));
603
604 return 0;
605}
606DM_TEST(dm_test_ofnode_u32,
607 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_OR_FLAT);
Simon Glass56bc3322022-09-06 20:27:02 -0600608
609static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
610{
611 ofnode node, check, subnode;
612 char buf[128];
613
614 /* temporarily disable this test due to a failure fixed later */
615 if (!of_live_active())
616 return 0;
617
618 node = ofnode_path("/lcd");
619 ut_assert(ofnode_valid(node));
620 ut_assertok(ofnode_add_subnode(node, "edmund", &subnode));
621 check = ofnode_path("/lcd/edmund");
622 ut_asserteq(subnode.of_offset, check.of_offset);
623 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
624 ut_asserteq_str("/lcd/edmund", buf);
625
626 if (of_live_active()) {
627 struct device_node *child;
628
629 ut_assertok(of_add_subnode((void *)ofnode_to_np(node), "edmund",
630 2, &child));
631 ut_asserteq_str("ed", child->name);
632 ut_asserteq_str("/lcd/ed", child->full_name);
633 check = ofnode_path("/lcd/ed");
634 ut_asserteq_ptr(child, check.np);
635 ut_assertok(ofnode_get_path(np_to_ofnode(child), buf,
636 sizeof(buf)));
637 ut_asserteq_str("/lcd/ed", buf);
638 }
639
640 /* An existing node should be returned with -EEXIST */
641 ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &check));
642 ut_asserteq(subnode.of_offset, check.of_offset);
643
644 /* add a root node */
645 node = ofnode_path("/");
646 ut_assert(ofnode_valid(node));
647 ut_assertok(ofnode_add_subnode(node, "lcd2", &subnode));
648 check = ofnode_path("/lcd2");
649 ut_asserteq(subnode.of_offset, check.of_offset);
650 ut_assertok(ofnode_get_path(subnode, buf, sizeof(buf)));
651 ut_asserteq_str("/lcd2", buf);
652
653 if (of_live_active()) {
654 ulong start;
655 int i;
656
657 /*
658 * Make sure each of the three malloc()checks in
659 * of_add_subnode() work
660 */
661 for (i = 0; i < 3; i++) {
662 malloc_enable_testing(i);
663 start = ut_check_free();
664 ut_asserteq(-ENOMEM, ofnode_add_subnode(node, "anthony",
665 &check));
666 ut_assertok(ut_check_delta(start));
667 }
668
669 /* This should pass since we allow 3 allocations */
670 malloc_enable_testing(3);
671 ut_assertok(ofnode_add_subnode(node, "anthony", &check));
672 malloc_disable_testing();
673 }
674
Simon Glass238afb52022-09-06 20:27:03 -0600675 /* write to the empty node */
676 ut_assertok(ofnode_write_string(subnode, "example", "text"));
677
Simon Glass56bc3322022-09-06 20:27:02 -0600678 return 0;
679}
680DM_TEST(dm_test_ofnode_add_subnode,
681 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_OR_FLAT);