blob: 61ae1db62d76528b5c49312aec72b407ce051807 [file] [log] [blame]
Masahiro Yamada04ca8712018-04-27 01:02:02 +09001// SPDX-License-Identifier: GPL-2.0+
2
3#include <common.h>
4#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06005#include <log.h>
Simon Glasse6c5c942018-10-01 12:22:08 -06006#include <dm/of_extra.h>
Masahiro Yamada04ca8712018-04-27 01:02:02 +09007#include <dm/test.h>
Simon Glass0e1fad42020-07-19 10:15:37 -06008#include <test/test.h>
Masahiro Yamada04ca8712018-04-27 01:02:02 +09009#include <test/ut.h>
10
11static int dm_test_ofnode_compatible(struct unit_test_state *uts)
12{
13 ofnode root_node = ofnode_path("/");
14
15 ut_assert(ofnode_valid(root_node));
16 ut_assert(ofnode_device_is_compatible(root_node, "sandbox"));
17
18 return 0;
19}
Simon Glasse180c2b2020-07-28 19:41:12 -060020DM_TEST(dm_test_ofnode_compatible, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Jens Wiklander9bc7e962018-08-20 11:10:00 +020021
Patrick Delaunay6d9949f2020-09-24 17:26:20 +020022static int dm_test_ofnode_get_by_phandle(struct unit_test_state *uts)
23{
24 /* test invalid phandle */
25 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0)));
26 ut_assert(!ofnode_valid(ofnode_get_by_phandle(-1)));
27
28 /* test first valid phandle */
29 ut_assert(ofnode_valid(ofnode_get_by_phandle(1)));
30
31 /* test unknown phandle */
32 ut_assert(!ofnode_valid(ofnode_get_by_phandle(0x1000000)));
33
34 return 0;
35}
36DM_TEST(dm_test_ofnode_get_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
37
Jens Wiklander9bc7e962018-08-20 11:10:00 +020038static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
39{
40 const char propname[] = "compatible";
41 const char propval[] = "denx,u-boot-fdt-test";
42 const char *str;
43 ofnode node = ofnode_null();
44
45 /* Find first matching node, there should be at least one */
46 node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
47 ut_assert(ofnode_valid(node));
48 str = ofnode_read_string(node, propname);
49 ut_assert(str && !strcmp(str, propval));
50
51 /* Find the rest of the matching nodes */
52 while (true) {
53 node = ofnode_by_prop_value(node, propname, propval,
54 sizeof(propval));
55 if (!ofnode_valid(node))
56 break;
57 str = ofnode_read_string(node, propname);
58 ut_assert(str && !strcmp(str, propval));
59 }
60
61 return 0;
62}
Simon Glasse180c2b2020-07-28 19:41:12 -060063DM_TEST(dm_test_ofnode_by_prop_value, UT_TESTF_SCAN_FDT);
Simon Glasse6c5c942018-10-01 12:22:08 -060064
65static int dm_test_ofnode_fmap(struct unit_test_state *uts)
66{
67 struct fmap_entry entry;
68 ofnode node;
69
70 node = ofnode_path("/cros-ec/flash");
71 ut_assert(ofnode_valid(node));
72 ut_assertok(ofnode_read_fmap_entry(node, &entry));
73 ut_asserteq(0x08000000, entry.offset);
74 ut_asserteq(0x20000, entry.length);
75
76 return 0;
77}
Simon Glasse180c2b2020-07-28 19:41:12 -060078DM_TEST(dm_test_ofnode_fmap, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass14ca9f72020-01-27 08:49:43 -070079
Simon Glassa8167d82020-01-27 08:49:44 -070080static int dm_test_ofnode_read(struct unit_test_state *uts)
81{
82 const u32 *val;
83 ofnode node;
84 int size;
85
86 node = ofnode_path("/a-test");
87 ut_assert(ofnode_valid(node));
88
89 val = ofnode_read_prop(node, "int-value", &size);
90 ut_assertnonnull(val);
91 ut_asserteq(4, size);
92 ut_asserteq(1234, fdt32_to_cpu(val[0]));
93
94 val = ofnode_read_prop(node, "missing", &size);
95 ut_assertnull(val);
96 ut_asserteq(-FDT_ERR_NOTFOUND, size);
97
98 /* Check it works without a size parameter */
99 val = ofnode_read_prop(node, "missing", NULL);
100 ut_assertnull(val);
101
102 return 0;
103}
Simon Glasse180c2b2020-07-28 19:41:12 -0600104DM_TEST(dm_test_ofnode_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa8167d82020-01-27 08:49:44 -0700105
Patrick Delaunaycc72f3e2020-09-25 09:41:16 +0200106static int dm_test_ofnode_phandle(struct unit_test_state *uts)
107{
108 struct ofnode_phandle_args args;
109 ofnode node;
110 int ret;
111 const char prop[] = "test-gpios";
112 const char cell[] = "#gpio-cells";
113 const char prop2[] = "phandle-value";
114
115 node = ofnode_path("/a-test");
116 ut_assert(ofnode_valid(node));
117
118 /* Test ofnode_count_phandle_with_args with cell name */
119 ret = ofnode_count_phandle_with_args(node, "missing", cell, 0);
120 ut_asserteq(-ENOENT, ret);
121 ret = ofnode_count_phandle_with_args(node, prop, "#invalid", 0);
122 ut_asserteq(-EINVAL, ret);
123 ret = ofnode_count_phandle_with_args(node, prop, cell, 0);
124 ut_asserteq(5, ret);
125
126 /* Test ofnode_parse_phandle_with_args with cell name */
127 ret = ofnode_parse_phandle_with_args(node, "missing", cell, 0, 0,
128 &args);
129 ut_asserteq(-ENOENT, ret);
130 ret = ofnode_parse_phandle_with_args(node, prop, "#invalid", 0, 0,
131 &args);
132 ut_asserteq(-EINVAL, ret);
133 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 0, &args);
134 ut_assertok(ret);
135 ut_asserteq(1, args.args_count);
136 ut_asserteq(1, args.args[0]);
137 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 1, &args);
138 ut_assertok(ret);
139 ut_asserteq(1, args.args_count);
140 ut_asserteq(4, args.args[0]);
141 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 2, &args);
142 ut_assertok(ret);
143 ut_asserteq(5, args.args_count);
144 ut_asserteq(5, args.args[0]);
145 ut_asserteq(1, args.args[4]);
146 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 3, &args);
147 ut_asserteq(-ENOENT, ret);
148 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 4, &args);
149 ut_assertok(ret);
150 ut_asserteq(1, args.args_count);
151 ut_asserteq(12, args.args[0]);
152 ret = ofnode_parse_phandle_with_args(node, prop, cell, 0, 5, &args);
153 ut_asserteq(-ENOENT, ret);
154
155 /* Test ofnode_count_phandle_with_args with cell count */
156 ret = ofnode_count_phandle_with_args(node, "missing", NULL, 2);
157 ut_asserteq(-ENOENT, ret);
158 ret = ofnode_count_phandle_with_args(node, prop2, NULL, 1);
159 ut_asserteq(3, ret);
160
161 /* Test ofnode_parse_phandle_with_args with cell count */
162 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 0, &args);
163 ut_assertok(ret);
164 ut_asserteq(1, ofnode_valid(args.node));
165 ut_asserteq(1, args.args_count);
166 ut_asserteq(10, args.args[0]);
167 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 1, &args);
168 ut_asserteq(-EINVAL, ret);
169 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 2, &args);
170 ut_assertok(ret);
171 ut_asserteq(1, ofnode_valid(args.node));
172 ut_asserteq(1, args.args_count);
173 ut_asserteq(30, args.args[0]);
174 ret = ofnode_parse_phandle_with_args(node, prop2, NULL, 1, 3, &args);
175 ut_asserteq(-ENOENT, ret);
176
177 return 0;
178}
179DM_TEST(dm_test_ofnode_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
180
Simon Glass14ca9f72020-01-27 08:49:43 -0700181static int dm_test_ofnode_read_chosen(struct unit_test_state *uts)
182{
183 const char *str;
Simon Glassbd933bf2020-01-27 08:49:46 -0700184 const u32 *val;
Simon Glass14ca9f72020-01-27 08:49:43 -0700185 ofnode node;
Simon Glassbd933bf2020-01-27 08:49:46 -0700186 int size;
Simon Glass14ca9f72020-01-27 08:49:43 -0700187
188 str = ofnode_read_chosen_string("setting");
189 ut_assertnonnull(str);
190 ut_asserteq_str("sunrise ohoka", str);
191 ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting"));
192
193 node = ofnode_get_chosen_node("other-node");
194 ut_assert(ofnode_valid(node));
195 ut_asserteq_str("c-test@5", ofnode_get_name(node));
196
197 node = ofnode_get_chosen_node("setting");
198 ut_assert(!ofnode_valid(node));
199
Simon Glassbd933bf2020-01-27 08:49:46 -0700200 val = ofnode_read_chosen_prop("int-values", &size);
201 ut_assertnonnull(val);
202 ut_asserteq(8, size);
203 ut_asserteq(0x1937, fdt32_to_cpu(val[0]));
204 ut_asserteq(72993, fdt32_to_cpu(val[1]));
205
Simon Glass14ca9f72020-01-27 08:49:43 -0700206 return 0;
207}
Simon Glasse180c2b2020-07-28 19:41:12 -0600208DM_TEST(dm_test_ofnode_read_chosen, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chunfeng Yunbf6ad912020-05-02 11:35:10 +0200209
Michal Simek305d3182020-07-28 12:51:08 +0200210static int dm_test_ofnode_read_aliases(struct unit_test_state *uts)
211{
212 const void *val;
213 ofnode node;
214 int size;
215
Michael Walle82a3c9e2021-02-25 16:51:11 +0100216 node = ofnode_get_aliases_node("ethernet3");
Michal Simek305d3182020-07-28 12:51:08 +0200217 ut_assert(ofnode_valid(node));
218 ut_asserteq_str("sbe5", ofnode_get_name(node));
219
220 node = ofnode_get_aliases_node("unknown");
221 ut_assert(!ofnode_valid(node));
222
223 val = ofnode_read_aliases_prop("spi0", &size);
224 ut_assertnonnull(val);
225 ut_asserteq(7, size);
226 ut_asserteq_str("/spi@0", (const char *)val);
227
228 return 0;
229}
230DM_TEST(dm_test_ofnode_read_aliases, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
231
Chunfeng Yunbf6ad912020-05-02 11:35:10 +0200232static int dm_test_ofnode_get_child_count(struct unit_test_state *uts)
233{
234 ofnode node, child_node;
235 u32 val;
236
237 node = ofnode_path("/i-test");
238 ut_assert(ofnode_valid(node));
239
240 val = ofnode_get_child_count(node);
241 ut_asserteq(3, val);
242
243 child_node = ofnode_first_subnode(node);
244 ut_assert(ofnode_valid(child_node));
245 val = ofnode_get_child_count(child_node);
246 ut_asserteq(0, val);
247
248 return 0;
249}
250DM_TEST(dm_test_ofnode_get_child_count,
Simon Glasse180c2b2020-07-28 19:41:12 -0600251 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0de1b072020-11-28 17:50:02 -0700252
253static int dm_test_ofnode_is_enabled(struct unit_test_state *uts)
254{
255 ofnode root_node = ofnode_path("/");
256 ofnode node = ofnode_path("/usb@0");
257
258 ut_assert(ofnode_is_enabled(root_node));
259 ut_assert(!ofnode_is_enabled(node));
260
261 return 0;
262}
263DM_TEST(dm_test_ofnode_is_enabled, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800264
265static int dm_test_ofnode_get_reg(struct unit_test_state *uts)
266{
267 ofnode node;
268 fdt_addr_t addr;
269 fdt_size_t size;
270
271 node = ofnode_path("/translation-test@8000");
272 ut_assert(ofnode_valid(node));
273 addr = ofnode_get_addr(node);
274 size = ofnode_get_size(node);
275 ut_asserteq(0x8000, addr);
276 ut_asserteq(0x4000, size);
277
278 node = ofnode_path("/translation-test@8000/dev@1,100");
279 ut_assert(ofnode_valid(node));
280 addr = ofnode_get_addr(node);
281 size = ofnode_get_size(node);
282 ut_asserteq(0x9000, addr);
283 ut_asserteq(0x1000, size);
284
285 node = ofnode_path("/emul-mux-controller");
286 ut_assert(ofnode_valid(node));
287 addr = ofnode_get_addr(node);
288 size = ofnode_get_size(node);
Patrice Chotard9876ae72022-01-04 08:42:48 +0100289 ut_asserteq_64(FDT_ADDR_T_NONE, addr);
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800290 ut_asserteq(FDT_SIZE_T_NONE, size);
291
Marek Behún31a7b712021-05-26 14:08:17 +0200292 node = ofnode_path("/translation-test@8000/noxlatebus@3,300/dev@42");
293 ut_assert(ofnode_valid(node));
294 addr = ofnode_get_addr_size_index_notrans(node, 0, &size);
295 ut_asserteq_64(0x42, addr);
296
Chen Guanqiao61772bc2021-04-12 14:51:12 +0800297 return 0;
298}
299DM_TEST(dm_test_ofnode_get_reg, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Marek Behún0e116be2021-05-26 14:08:18 +0200300
301static int dm_test_ofnode_get_path(struct unit_test_state *uts)
302{
303 const char *path = "/translation-test@8000/noxlatebus@3,300/dev@42";
304 char buf[64];
305 ofnode node;
306 int res;
307
308 node = ofnode_path(path);
309 ut_assert(ofnode_valid(node));
310
311 res = ofnode_get_path(node, buf, 64);
312 ut_asserteq(0, res);
313 ut_asserteq_str(path, buf);
314
315 res = ofnode_get_path(node, buf, 32);
316 ut_asserteq(-ENOSPC, res);
317
318 return 0;
319}
320DM_TEST(dm_test_ofnode_get_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7de8bd02021-08-07 07:24:01 -0600321
322static int dm_test_ofnode_conf(struct unit_test_state *uts)
323{
324 ut_assert(!ofnode_conf_read_bool("missing"));
325 ut_assert(ofnode_conf_read_bool("testing-bool"));
326
327 ut_asserteq(123, ofnode_conf_read_int("testing-int", 0));
328 ut_asserteq(6, ofnode_conf_read_int("missing", 6));
329
330 ut_assertnull(ofnode_conf_read_str("missing"));
331 ut_asserteq_str("testing", ofnode_conf_read_str("testing-str"));
332
333 return 0;
334}
335DM_TEST(dm_test_ofnode_conf, 0);
Michael Wallebce039a2021-10-15 15:15:18 +0200336
337static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts)
338{
339 const char compatible[] = "denx,u-boot-fdt-test";
340 bool found = false;
341 ofnode node;
342
343 ofnode_for_each_compatible_node(node, compatible) {
344 ut_assert(ofnode_device_is_compatible(node, compatible));
345 found = true;
346 }
347
348 /* There should be at least one matching node */
349 ut_assert(found);
350
351 return 0;
352}
353DM_TEST(dm_test_ofnode_for_each_compatible_node, UT_TESTF_SCAN_FDT);
Simon Glassfb933d02021-10-23 17:26:04 -0600354
355static int dm_test_ofnode_string(struct unit_test_state *uts)
356{
Simon Glass075bfc92021-10-23 17:26:07 -0600357 const char **val;
Simon Glassfb933d02021-10-23 17:26:04 -0600358 const char *out;
359 ofnode node;
360
361 node = ofnode_path("/a-test");
362 ut_assert(ofnode_valid(node));
363
364 /* single string */
365 ut_asserteq(1, ofnode_read_string_count(node, "str-value"));
366 ut_assertok(ofnode_read_string_index(node, "str-value", 0, &out));
367 ut_asserteq_str("test string", out);
368 ut_asserteq(0, ofnode_stringlist_search(node, "str-value",
369 "test string"));
Simon Glass075bfc92021-10-23 17:26:07 -0600370 ut_asserteq(1, ofnode_read_string_list(node, "str-value", &val));
371 ut_asserteq_str("test string", val[0]);
372 ut_assertnull(val[1]);
373 free(val);
Simon Glassfb933d02021-10-23 17:26:04 -0600374
375 /* list of strings */
376 ut_asserteq(5, ofnode_read_string_count(node, "mux-control-names"));
377 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 0,
378 &out));
379 ut_asserteq_str("mux0", out);
380 ut_asserteq(0, ofnode_stringlist_search(node, "mux-control-names",
381 "mux0"));
Simon Glass075bfc92021-10-23 17:26:07 -0600382 ut_asserteq(5, ofnode_read_string_list(node, "mux-control-names",
383 &val));
384 ut_asserteq_str("mux0", val[0]);
385 ut_asserteq_str("mux1", val[1]);
386 ut_asserteq_str("mux2", val[2]);
387 ut_asserteq_str("mux3", val[3]);
388 ut_asserteq_str("mux4", val[4]);
389 ut_assertnull(val[5]);
390 free(val);
Simon Glassfb933d02021-10-23 17:26:04 -0600391
392 ut_assertok(ofnode_read_string_index(node, "mux-control-names", 4,
393 &out));
394 ut_asserteq_str("mux4", out);
395 ut_asserteq(4, ofnode_stringlist_search(node, "mux-control-names",
396 "mux4"));
397
398 return 0;
399}
400DM_TEST(dm_test_ofnode_string, 0);
401
402static int dm_test_ofnode_string_err(struct unit_test_state *uts)
403{
Simon Glass075bfc92021-10-23 17:26:07 -0600404 const char **val;
Simon Glassfb933d02021-10-23 17:26:04 -0600405 const char *out;
406 ofnode node;
407
408 /*
409 * Test error codes only on livetree, as they are different with
410 * flattree
411 */
412 node = ofnode_path("/a-test");
413 ut_assert(ofnode_valid(node));
414
415 /* non-existent property */
416 ut_asserteq(-EINVAL, ofnode_read_string_count(node, "missing"));
417 ut_asserteq(-EINVAL, ofnode_read_string_index(node, "missing", 0,
418 &out));
Simon Glass075bfc92021-10-23 17:26:07 -0600419 ut_asserteq(-EINVAL, ofnode_read_string_list(node, "missing", &val));
Simon Glassfb933d02021-10-23 17:26:04 -0600420
421 /* empty property */
422 ut_asserteq(-ENODATA, ofnode_read_string_count(node, "bool-value"));
423 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "bool-value", 0,
424 &out));
Simon Glass075bfc92021-10-23 17:26:07 -0600425 ut_asserteq(-ENODATA, ofnode_read_string_list(node, "bool-value",
426 &val));
Simon Glassfb933d02021-10-23 17:26:04 -0600427
428 /* badly formatted string list */
429 ut_asserteq(-EILSEQ, ofnode_read_string_count(node, "int64-value"));
430 ut_asserteq(-EILSEQ, ofnode_read_string_index(node, "int64-value", 0,
431 &out));
Simon Glass075bfc92021-10-23 17:26:07 -0600432 ut_asserteq(-EILSEQ, ofnode_read_string_list(node, "int64-value",
433 &val));
Simon Glassfb933d02021-10-23 17:26:04 -0600434
435 /* out of range / not found */
436 ut_asserteq(-ENODATA, ofnode_read_string_index(node, "str-value", 1,
437 &out));
438 ut_asserteq(-ENODATA, ofnode_stringlist_search(node, "str-value",
439 "other"));
440
441 /* negative value for index is not allowed, so don't test for that */
442
443 ut_asserteq(-ENODATA, ofnode_read_string_index(node,
444 "mux-control-names", 5,
445 &out));
446
447 return 0;
448}
449DM_TEST(dm_test_ofnode_string_err, UT_TESTF_LIVE_TREE);
Marek Behúnf3dd2132022-04-07 00:32:57 +0200450
451static int dm_test_ofnode_get_phy(struct unit_test_state *uts)
452{
453 ofnode eth_node, phy_node;
Marek Behún123ca112022-04-07 00:33:01 +0200454 phy_interface_t mode;
Marek Behúnf3dd2132022-04-07 00:32:57 +0200455 u32 reg;
456
457 eth_node = ofnode_path("/phy-test-eth");
458 ut_assert(ofnode_valid(eth_node));
459
Marek Behún123ca112022-04-07 00:33:01 +0200460 mode = ofnode_read_phy_mode(eth_node);
461 ut_assert(mode == PHY_INTERFACE_MODE_2500BASEX);
462
Marek Behúnf3dd2132022-04-07 00:32:57 +0200463 phy_node = ofnode_get_phy_node(eth_node);
464 ut_assert(ofnode_valid(phy_node));
465
466 reg = ofnode_read_u32_default(phy_node, "reg", -1U);
467 ut_asserteq_64(0x1, reg);
468
469 return 0;
470}
471DM_TEST(dm_test_ofnode_get_phy, 0);