Simon Glass | 217293e | 2020-10-25 20:38:29 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
| 3 | #include <common.h> |
| 4 | #include <dm.h> |
Simon Glass | b325248 | 2020-10-03 11:31:28 -0600 | [diff] [blame] | 5 | #include <dt-structs.h> |
Simon Glass | 217293e | 2020-10-25 20:38:29 -0600 | [diff] [blame] | 6 | #include <dm/test.h> |
| 7 | #include <test/test.h> |
| 8 | #include <test/ut.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Simon Glass | 217293e | 2020-10-25 20:38:29 -0600 | [diff] [blame] | 10 | |
| 11 | /* Test that we can find a device using of-platdata */ |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 12 | static int dm_test_of_plat_base(struct unit_test_state *uts) |
Simon Glass | 217293e | 2020-10-25 20:38:29 -0600 | [diff] [blame] | 13 | { |
| 14 | struct udevice *dev; |
| 15 | |
| 16 | ut_assertok(uclass_first_device_err(UCLASS_SERIAL, &dev)); |
| 17 | ut_asserteq_str("sandbox_serial", dev->name); |
| 18 | |
| 19 | return 0; |
| 20 | } |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 21 | DM_TEST(dm_test_of_plat_base, UT_TESTF_SCAN_PDATA); |
Simon Glass | b325248 | 2020-10-03 11:31:28 -0600 | [diff] [blame] | 22 | |
| 23 | /* Test that we can read properties from a device */ |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 24 | static int dm_test_of_plat_props(struct unit_test_state *uts) |
Simon Glass | b325248 | 2020-10-03 11:31:28 -0600 | [diff] [blame] | 25 | { |
| 26 | struct dtd_sandbox_spl_test *plat; |
| 27 | struct udevice *dev; |
| 28 | int i; |
| 29 | |
Simon Glass | 8828052 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 30 | /* Skip the clock */ |
Simon Glass | b325248 | 2020-10-03 11:31:28 -0600 | [diff] [blame] | 31 | ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev)); |
Simon Glass | 8828052 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 32 | ut_asserteq_str("sandbox_clk_test", dev->name); |
| 33 | |
| 34 | ut_assertok(uclass_next_device_err(&dev)); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 35 | plat = dev_get_plat(dev); |
Simon Glass | b325248 | 2020-10-03 11:31:28 -0600 | [diff] [blame] | 36 | ut_assert(plat->boolval); |
| 37 | ut_asserteq(1, plat->intval); |
| 38 | ut_asserteq(4, ARRAY_SIZE(plat->intarray)); |
| 39 | ut_asserteq(2, plat->intarray[0]); |
| 40 | ut_asserteq(3, plat->intarray[1]); |
| 41 | ut_asserteq(4, plat->intarray[2]); |
| 42 | ut_asserteq(0, plat->intarray[3]); |
| 43 | ut_asserteq(5, plat->byteval); |
| 44 | ut_asserteq(3, ARRAY_SIZE(plat->bytearray)); |
| 45 | ut_asserteq(6, plat->bytearray[0]); |
| 46 | ut_asserteq(0, plat->bytearray[1]); |
| 47 | ut_asserteq(0, plat->bytearray[2]); |
| 48 | ut_asserteq(9, ARRAY_SIZE(plat->longbytearray)); |
| 49 | for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++) |
| 50 | ut_asserteq(9 + i, plat->longbytearray[i]); |
| 51 | ut_asserteq_str("message", plat->stringval); |
| 52 | ut_asserteq(3, ARRAY_SIZE(plat->stringarray)); |
| 53 | ut_asserteq_str("multi-word", plat->stringarray[0]); |
| 54 | ut_asserteq_str("message", plat->stringarray[1]); |
| 55 | ut_asserteq_str("", plat->stringarray[2]); |
| 56 | |
| 57 | ut_assertok(uclass_next_device_err(&dev)); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 58 | plat = dev_get_plat(dev); |
Simon Glass | b325248 | 2020-10-03 11:31:28 -0600 | [diff] [blame] | 59 | ut_assert(!plat->boolval); |
| 60 | ut_asserteq(3, plat->intval); |
| 61 | ut_asserteq(5, plat->intarray[0]); |
| 62 | ut_asserteq(0, plat->intarray[1]); |
| 63 | ut_asserteq(0, plat->intarray[2]); |
| 64 | ut_asserteq(0, plat->intarray[3]); |
| 65 | ut_asserteq(8, plat->byteval); |
| 66 | ut_asserteq(3, ARRAY_SIZE(plat->bytearray)); |
| 67 | ut_asserteq(1, plat->bytearray[0]); |
| 68 | ut_asserteq(0x23, plat->bytearray[1]); |
| 69 | ut_asserteq(0x34, plat->bytearray[2]); |
| 70 | for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++) |
| 71 | ut_asserteq(i < 4 ? 9 + i : 0, plat->longbytearray[i]); |
| 72 | ut_asserteq_str("message2", plat->stringval); |
| 73 | ut_asserteq_str("another", plat->stringarray[0]); |
| 74 | ut_asserteq_str("multi-word", plat->stringarray[1]); |
| 75 | ut_asserteq_str("message", plat->stringarray[2]); |
| 76 | |
| 77 | ut_assertok(uclass_next_device_err(&dev)); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 78 | plat = dev_get_plat(dev); |
Simon Glass | b325248 | 2020-10-03 11:31:28 -0600 | [diff] [blame] | 79 | ut_assert(!plat->boolval); |
| 80 | ut_asserteq_str("one", plat->stringarray[0]); |
| 81 | ut_asserteq_str("", plat->stringarray[1]); |
| 82 | ut_asserteq_str("", plat->stringarray[2]); |
| 83 | |
| 84 | ut_assertok(uclass_next_device_err(&dev)); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 85 | plat = dev_get_plat(dev); |
Simon Glass | b325248 | 2020-10-03 11:31:28 -0600 | [diff] [blame] | 86 | ut_assert(!plat->boolval); |
| 87 | ut_asserteq_str("spl", plat->stringarray[0]); |
| 88 | |
| 89 | ut_asserteq(-ENODEV, uclass_next_device_err(&dev)); |
| 90 | |
| 91 | return 0; |
| 92 | } |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 93 | DM_TEST(dm_test_of_plat_props, UT_TESTF_SCAN_PDATA); |
Simon Glass | 36af37b | 2020-10-03 11:31:31 -0600 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * find_driver_info - recursively find the driver_info for a device |
| 97 | * |
| 98 | * This sets found[idx] to true when it finds the driver_info record for a |
| 99 | * device, where idx is the index in the driver_info linker list. |
| 100 | * |
| 101 | * @uts: Test state |
| 102 | * @parent: Parent to search |
| 103 | * @found: bool array to update |
| 104 | * @return 0 if OK, non-zero on error |
| 105 | */ |
| 106 | static int find_driver_info(struct unit_test_state *uts, struct udevice *parent, |
| 107 | bool found[]) |
| 108 | { |
| 109 | struct udevice *dev; |
| 110 | |
| 111 | /* If not the root device, find the entry that caused it to be bound */ |
| 112 | if (parent->parent) { |
Simon Glass | 36af37b | 2020-10-03 11:31:31 -0600 | [diff] [blame] | 113 | const int n_ents = |
| 114 | ll_entry_count(struct driver_info, driver_info); |
Simon Glass | 36af37b | 2020-10-03 11:31:31 -0600 | [diff] [blame] | 115 | int idx = -1; |
Simon Glass | a294ead | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 116 | int i; |
Simon Glass | 36af37b | 2020-10-03 11:31:31 -0600 | [diff] [blame] | 117 | |
Simon Glass | a294ead | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 118 | for (i = 0; i < n_ents; i++) { |
| 119 | const struct driver_rt *drt = gd_dm_driver_rt() + i; |
| 120 | |
| 121 | if (drt->dev == parent) { |
| 122 | idx = i; |
Simon Glass | 36af37b | 2020-10-03 11:31:31 -0600 | [diff] [blame] | 123 | found[idx] = true; |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | ut_assert(idx != -1); |
| 129 | } |
| 130 | |
| 131 | device_foreach_child(dev, parent) { |
| 132 | int ret; |
| 133 | |
| 134 | ret = find_driver_info(uts, dev, found); |
| 135 | if (ret < 0) |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /* Check that every device is recorded in its driver_info struct */ |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 143 | static int dm_test_of_plat_dev(struct unit_test_state *uts) |
Simon Glass | 36af37b | 2020-10-03 11:31:31 -0600 | [diff] [blame] | 144 | { |
Simon Glass | 36af37b | 2020-10-03 11:31:31 -0600 | [diff] [blame] | 145 | const int n_ents = ll_entry_count(struct driver_info, driver_info); |
| 146 | bool found[n_ents]; |
| 147 | uint i; |
| 148 | |
| 149 | /* Record the indexes that are found */ |
| 150 | memset(found, '\0', sizeof(found)); |
| 151 | ut_assertok(find_driver_info(uts, gd->dm_root, found)); |
| 152 | |
| 153 | /* Make sure that the driver entries without devices have no ->dev */ |
| 154 | for (i = 0; i < n_ents; i++) { |
Simon Glass | a294ead | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 155 | const struct driver_rt *drt = gd_dm_driver_rt() + i; |
Simon Glass | 36af37b | 2020-10-03 11:31:31 -0600 | [diff] [blame] | 156 | struct udevice *dev; |
| 157 | |
| 158 | if (found[i]) { |
| 159 | /* Make sure we can find it */ |
Simon Glass | a294ead | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 160 | ut_assertnonnull(drt->dev); |
Simon Glass | cc469b7 | 2021-03-15 17:25:28 +1300 | [diff] [blame] | 161 | ut_assertok(device_get_by_ofplat_idx(i, &dev)); |
Simon Glass | a294ead | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 162 | ut_asserteq_ptr(dev, drt->dev); |
Simon Glass | 36af37b | 2020-10-03 11:31:31 -0600 | [diff] [blame] | 163 | } else { |
Simon Glass | a294ead | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 164 | ut_assertnull(drt->dev); |
Simon Glass | cc469b7 | 2021-03-15 17:25:28 +1300 | [diff] [blame] | 165 | ut_asserteq(-ENOENT, device_get_by_ofplat_idx(i, &dev)); |
Simon Glass | 36af37b | 2020-10-03 11:31:31 -0600 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | } |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 171 | DM_TEST(dm_test_of_plat_dev, UT_TESTF_SCAN_PDATA); |
Simon Glass | 8828052 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 172 | |
| 173 | /* Test handling of phandles that point to other devices */ |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 174 | static int dm_test_of_plat_phandle(struct unit_test_state *uts) |
Simon Glass | 8828052 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 175 | { |
| 176 | struct dtd_sandbox_clk_test *plat; |
| 177 | struct udevice *dev, *clk; |
| 178 | |
| 179 | ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev)); |
| 180 | ut_asserteq_str("sandbox_clk_test", dev->name); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 181 | plat = dev_get_plat(dev); |
Simon Glass | 8828052 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 182 | |
Simon Glass | cc469b7 | 2021-03-15 17:25:28 +1300 | [diff] [blame] | 183 | ut_assertok(device_get_by_ofplat_idx(plat->clocks[0].idx, &clk)); |
Simon Glass | 8813986 | 2021-03-15 17:25:24 +1300 | [diff] [blame] | 184 | ut_asserteq_str("sandbox_fixed_clock", clk->name); |
Simon Glass | 8828052 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 185 | |
Simon Glass | cc469b7 | 2021-03-15 17:25:28 +1300 | [diff] [blame] | 186 | ut_assertok(device_get_by_ofplat_idx(plat->clocks[1].idx, &clk)); |
Simon Glass | 8828052 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 187 | ut_asserteq_str("sandbox_clk", clk->name); |
| 188 | ut_asserteq(1, plat->clocks[1].arg[0]); |
| 189 | |
Simon Glass | cc469b7 | 2021-03-15 17:25:28 +1300 | [diff] [blame] | 190 | ut_assertok(device_get_by_ofplat_idx(plat->clocks[2].idx, &clk)); |
Simon Glass | 8828052 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 191 | ut_asserteq_str("sandbox_clk", clk->name); |
| 192 | ut_asserteq(0, plat->clocks[2].arg[0]); |
| 193 | |
Simon Glass | cc469b7 | 2021-03-15 17:25:28 +1300 | [diff] [blame] | 194 | ut_assertok(device_get_by_ofplat_idx(plat->clocks[3].idx, &clk)); |
Simon Glass | 8828052 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 195 | ut_asserteq_str("sandbox_clk", clk->name); |
| 196 | ut_asserteq(3, plat->clocks[3].arg[0]); |
| 197 | |
Simon Glass | cc469b7 | 2021-03-15 17:25:28 +1300 | [diff] [blame] | 198 | ut_assertok(device_get_by_ofplat_idx(plat->clocks[4].idx, &clk)); |
Simon Glass | 8828052 | 2020-10-03 11:31:32 -0600 | [diff] [blame] | 199 | ut_asserteq_str("sandbox_clk", clk->name); |
| 200 | ut_asserteq(2, plat->clocks[4].arg[0]); |
| 201 | |
| 202 | return 0; |
| 203 | } |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 204 | DM_TEST(dm_test_of_plat_phandle, UT_TESTF_SCAN_PDATA); |
Simon Glass | fbe27a5 | 2020-10-03 11:31:36 -0600 | [diff] [blame] | 205 | |
| 206 | #if CONFIG_IS_ENABLED(OF_PLATDATA_PARENT) |
| 207 | /* Test that device parents are correctly set up */ |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 208 | static int dm_test_of_plat_parent(struct unit_test_state *uts) |
Simon Glass | fbe27a5 | 2020-10-03 11:31:36 -0600 | [diff] [blame] | 209 | { |
Simon Glass | d85f2c4 | 2021-01-21 13:57:09 -0700 | [diff] [blame] | 210 | struct udevice *dev, *bus; |
Simon Glass | fbe27a5 | 2020-10-03 11:31:36 -0600 | [diff] [blame] | 211 | |
Simon Glass | d85f2c4 | 2021-01-21 13:57:09 -0700 | [diff] [blame] | 212 | ut_assertok(uclass_first_device_err(UCLASS_SIMPLE_BUS, &bus)); |
| 213 | ut_assertok(device_first_child_err(bus, &dev)); |
| 214 | ut_asserteq_ptr(bus, dev_get_parent(dev)); |
Simon Glass | fbe27a5 | 2020-10-03 11:31:36 -0600 | [diff] [blame] | 215 | |
| 216 | return 0; |
| 217 | } |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 218 | DM_TEST(dm_test_of_plat_parent, UT_TESTF_SCAN_PDATA); |
Simon Glass | fbe27a5 | 2020-10-03 11:31:36 -0600 | [diff] [blame] | 219 | #endif |