Masahiro Yamada | b4f2dda | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
| 3 | #include <common.h> |
| 4 | #include <dm.h> |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 5 | #include <dm/of_extra.h> |
Masahiro Yamada | b4f2dda | 2018-04-27 01:02:02 +0900 | [diff] [blame] | 6 | #include <dm/test.h> |
| 7 | #include <test/ut.h> |
| 8 | |
| 9 | static int dm_test_ofnode_compatible(struct unit_test_state *uts) |
| 10 | { |
| 11 | ofnode root_node = ofnode_path("/"); |
| 12 | |
| 13 | ut_assert(ofnode_valid(root_node)); |
| 14 | ut_assert(ofnode_device_is_compatible(root_node, "sandbox")); |
| 15 | |
| 16 | return 0; |
| 17 | } |
| 18 | DM_TEST(dm_test_ofnode_compatible, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Jens Wiklander | d9fd0ac | 2018-08-20 11:10:00 +0200 | [diff] [blame] | 19 | |
| 20 | static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts) |
| 21 | { |
| 22 | const char propname[] = "compatible"; |
| 23 | const char propval[] = "denx,u-boot-fdt-test"; |
| 24 | const char *str; |
| 25 | ofnode node = ofnode_null(); |
| 26 | |
| 27 | /* Find first matching node, there should be at least one */ |
| 28 | node = ofnode_by_prop_value(node, propname, propval, sizeof(propval)); |
| 29 | ut_assert(ofnode_valid(node)); |
| 30 | str = ofnode_read_string(node, propname); |
| 31 | ut_assert(str && !strcmp(str, propval)); |
| 32 | |
| 33 | /* Find the rest of the matching nodes */ |
| 34 | while (true) { |
| 35 | node = ofnode_by_prop_value(node, propname, propval, |
| 36 | sizeof(propval)); |
| 37 | if (!ofnode_valid(node)) |
| 38 | break; |
| 39 | str = ofnode_read_string(node, propname); |
| 40 | ut_assert(str && !strcmp(str, propval)); |
| 41 | } |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | DM_TEST(dm_test_ofnode_by_prop_value, DM_TESTF_SCAN_FDT); |
Simon Glass | 699c9ca | 2018-10-01 12:22:08 -0600 | [diff] [blame] | 46 | |
| 47 | static int dm_test_ofnode_fmap(struct unit_test_state *uts) |
| 48 | { |
| 49 | struct fmap_entry entry; |
| 50 | ofnode node; |
| 51 | |
| 52 | node = ofnode_path("/cros-ec/flash"); |
| 53 | ut_assert(ofnode_valid(node)); |
| 54 | ut_assertok(ofnode_read_fmap_entry(node, &entry)); |
| 55 | ut_asserteq(0x08000000, entry.offset); |
| 56 | ut_asserteq(0x20000, entry.length); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | DM_TEST(dm_test_ofnode_fmap, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 61 | |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 62 | static int dm_test_ofnode_read(struct unit_test_state *uts) |
| 63 | { |
| 64 | const u32 *val; |
| 65 | ofnode node; |
| 66 | int size; |
| 67 | |
| 68 | node = ofnode_path("/a-test"); |
| 69 | ut_assert(ofnode_valid(node)); |
| 70 | |
| 71 | val = ofnode_read_prop(node, "int-value", &size); |
| 72 | ut_assertnonnull(val); |
| 73 | ut_asserteq(4, size); |
| 74 | ut_asserteq(1234, fdt32_to_cpu(val[0])); |
| 75 | |
| 76 | val = ofnode_read_prop(node, "missing", &size); |
| 77 | ut_assertnull(val); |
| 78 | ut_asserteq(-FDT_ERR_NOTFOUND, size); |
| 79 | |
| 80 | /* Check it works without a size parameter */ |
| 81 | val = ofnode_read_prop(node, "missing", NULL); |
| 82 | ut_assertnull(val); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | DM_TEST(dm_test_ofnode_read, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 87 | |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 88 | static int dm_test_ofnode_read_chosen(struct unit_test_state *uts) |
| 89 | { |
| 90 | const char *str; |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 91 | const u32 *val; |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 92 | ofnode node; |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 93 | int size; |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 94 | |
| 95 | str = ofnode_read_chosen_string("setting"); |
| 96 | ut_assertnonnull(str); |
| 97 | ut_asserteq_str("sunrise ohoka", str); |
| 98 | ut_asserteq_ptr(NULL, ofnode_read_chosen_string("no-setting")); |
| 99 | |
| 100 | node = ofnode_get_chosen_node("other-node"); |
| 101 | ut_assert(ofnode_valid(node)); |
| 102 | ut_asserteq_str("c-test@5", ofnode_get_name(node)); |
| 103 | |
| 104 | node = ofnode_get_chosen_node("setting"); |
| 105 | ut_assert(!ofnode_valid(node)); |
| 106 | |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 107 | val = ofnode_read_chosen_prop("int-values", &size); |
| 108 | ut_assertnonnull(val); |
| 109 | ut_asserteq(8, size); |
| 110 | ut_asserteq(0x1937, fdt32_to_cpu(val[0])); |
| 111 | ut_asserteq(72993, fdt32_to_cpu(val[1])); |
| 112 | |
Simon Glass | f345596 | 2020-01-27 08:49:43 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | DM_TEST(dm_test_ofnode_read_chosen, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |