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