Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
| 9 | #include <fdtdec.h> |
| 10 | #include <malloc.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <dm/test.h> |
| 13 | #include <dm/root.h> |
Simon Glass | 9c433fe | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 15 | #include <dm/uclass-internal.h> |
| 16 | #include <dm/util.h> |
Mario Six | 058ef5b | 2018-06-26 08:46:49 +0200 | [diff] [blame] | 17 | #include <dm/lists.h> |
| 18 | #include <dm/of_access.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 19 | #include <test/ut.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 23 | static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 24 | { |
| 25 | const struct dm_test_pdata *pdata = dev->platdata; |
| 26 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 27 | |
| 28 | *pingret = pingval + pdata->ping_add; |
| 29 | priv->ping_total += *pingret; |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | static const struct test_ops test_ops = { |
| 35 | .ping = testfdt_drv_ping, |
| 36 | }; |
| 37 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 38 | static int testfdt_ofdata_to_platdata(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 39 | { |
| 40 | struct dm_test_pdata *pdata = dev_get_platdata(dev); |
| 41 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 42 | pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 43 | "ping-add", -1); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 44 | pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev), |
Simon Glass | a7bb08a | 2014-07-23 06:54:57 -0600 | [diff] [blame] | 45 | "ping-expect"); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 50 | static int testfdt_drv_probe(struct udevice *dev) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 51 | { |
| 52 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 53 | |
| 54 | priv->ping_total += DM_TEST_START_TOTAL; |
| 55 | |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 56 | /* |
| 57 | * If this device is on a bus, the uclass_flag will be set before |
Bin Meng | d9bad17 | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 58 | * calling this function. In the meantime the uclass_postp is |
| 59 | * initlized to a value -1. These are used respectively by |
| 60 | * dm_test_bus_child_pre_probe_uclass() and |
| 61 | * dm_test_bus_child_post_probe_uclass(). |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 62 | */ |
| 63 | priv->uclass_total += priv->uclass_flag; |
Bin Meng | d9bad17 | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 64 | priv->uclass_postp = -1; |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 65 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
Simon Glass | 767827a | 2014-06-11 23:29:45 -0600 | [diff] [blame] | 69 | static const struct udevice_id testfdt_ids[] = { |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 70 | { |
| 71 | .compatible = "denx,u-boot-fdt-test", |
| 72 | .data = DM_TEST_TYPE_FIRST }, |
| 73 | { |
| 74 | .compatible = "google,another-fdt-test", |
| 75 | .data = DM_TEST_TYPE_SECOND }, |
| 76 | { } |
| 77 | }; |
| 78 | |
| 79 | U_BOOT_DRIVER(testfdt_drv) = { |
| 80 | .name = "testfdt_drv", |
| 81 | .of_match = testfdt_ids, |
| 82 | .id = UCLASS_TEST_FDT, |
| 83 | .ofdata_to_platdata = testfdt_ofdata_to_platdata, |
| 84 | .probe = testfdt_drv_probe, |
| 85 | .ops = &test_ops, |
| 86 | .priv_auto_alloc_size = sizeof(struct dm_test_priv), |
| 87 | .platdata_auto_alloc_size = sizeof(struct dm_test_pdata), |
| 88 | }; |
| 89 | |
Bin Meng | d9d2478 | 2018-10-10 22:07:01 -0700 | [diff] [blame] | 90 | static const struct udevice_id testfdt1_ids[] = { |
| 91 | { |
| 92 | .compatible = "denx,u-boot-fdt-test1", |
| 93 | .data = DM_TEST_TYPE_FIRST }, |
| 94 | { } |
| 95 | }; |
| 96 | |
| 97 | U_BOOT_DRIVER(testfdt1_drv) = { |
| 98 | .name = "testfdt1_drv", |
| 99 | .of_match = testfdt1_ids, |
| 100 | .id = UCLASS_TEST_FDT, |
| 101 | .ofdata_to_platdata = testfdt_ofdata_to_platdata, |
| 102 | .probe = testfdt_drv_probe, |
| 103 | .ops = &test_ops, |
| 104 | .priv_auto_alloc_size = sizeof(struct dm_test_priv), |
| 105 | .platdata_auto_alloc_size = sizeof(struct dm_test_pdata), |
| 106 | .flags = DM_FLAG_PRE_RELOC, |
| 107 | }; |
| 108 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 109 | /* From here is the testfdt uclass code */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 110 | int testfdt_ping(struct udevice *dev, int pingval, int *pingret) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 111 | { |
| 112 | const struct test_ops *ops = device_get_ops(dev); |
| 113 | |
| 114 | if (!ops->ping) |
| 115 | return -ENOSYS; |
| 116 | |
| 117 | return ops->ping(dev, pingval, pingret); |
| 118 | } |
| 119 | |
| 120 | UCLASS_DRIVER(testfdt) = { |
| 121 | .name = "testfdt", |
| 122 | .id = UCLASS_TEST_FDT, |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 123 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
Simon Glass | 9c433fe | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 126 | struct dm_testprobe_pdata { |
| 127 | int probe_err; |
| 128 | }; |
| 129 | |
| 130 | static int testprobe_drv_probe(struct udevice *dev) |
| 131 | { |
| 132 | struct dm_testprobe_pdata *pdata = dev_get_platdata(dev); |
| 133 | |
| 134 | return pdata->probe_err; |
| 135 | } |
| 136 | |
| 137 | static const struct udevice_id testprobe_ids[] = { |
| 138 | { .compatible = "denx,u-boot-probe-test" }, |
| 139 | { } |
| 140 | }; |
| 141 | |
| 142 | U_BOOT_DRIVER(testprobe_drv) = { |
| 143 | .name = "testprobe_drv", |
| 144 | .of_match = testprobe_ids, |
| 145 | .id = UCLASS_TEST_PROBE, |
| 146 | .probe = testprobe_drv_probe, |
| 147 | .platdata_auto_alloc_size = sizeof(struct dm_testprobe_pdata), |
| 148 | }; |
| 149 | |
| 150 | UCLASS_DRIVER(testprobe) = { |
| 151 | .name = "testprobe", |
| 152 | .id = UCLASS_TEST_PROBE, |
| 153 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 154 | }; |
| 155 | |
Simon Glass | 204675c | 2019-12-29 21:19:25 -0700 | [diff] [blame] | 156 | struct dm_testdevres_pdata { |
| 157 | void *ptr; |
| 158 | }; |
| 159 | |
| 160 | struct dm_testdevres_priv { |
| 161 | void *ptr; |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 162 | void *ptr_ofdata; |
Simon Glass | 204675c | 2019-12-29 21:19:25 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | static int testdevres_drv_bind(struct udevice *dev) |
| 166 | { |
| 167 | struct dm_testdevres_pdata *pdata = dev_get_platdata(dev); |
| 168 | |
| 169 | pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 174 | static int testdevres_drv_ofdata_to_platdata(struct udevice *dev) |
| 175 | { |
| 176 | struct dm_testdevres_priv *priv = dev_get_priv(dev); |
| 177 | |
| 178 | priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Simon Glass | 204675c | 2019-12-29 21:19:25 -0700 | [diff] [blame] | 183 | static int testdevres_drv_probe(struct udevice *dev) |
| 184 | { |
| 185 | struct dm_testdevres_priv *priv = dev_get_priv(dev); |
| 186 | |
| 187 | priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static const struct udevice_id testdevres_ids[] = { |
| 193 | { .compatible = "denx,u-boot-devres-test" }, |
| 194 | { } |
| 195 | }; |
| 196 | |
| 197 | U_BOOT_DRIVER(testdevres_drv) = { |
| 198 | .name = "testdevres_drv", |
| 199 | .of_match = testdevres_ids, |
| 200 | .id = UCLASS_TEST_DEVRES, |
| 201 | .bind = testdevres_drv_bind, |
Simon Glass | 0fe1537 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 202 | .ofdata_to_platdata = testdevres_drv_ofdata_to_platdata, |
Simon Glass | 204675c | 2019-12-29 21:19:25 -0700 | [diff] [blame] | 203 | .probe = testdevres_drv_probe, |
| 204 | .platdata_auto_alloc_size = sizeof(struct dm_testdevres_pdata), |
| 205 | .priv_auto_alloc_size = sizeof(struct dm_testdevres_priv), |
| 206 | }; |
| 207 | |
| 208 | UCLASS_DRIVER(testdevres) = { |
| 209 | .name = "testdevres", |
| 210 | .id = UCLASS_TEST_DEVRES, |
| 211 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 212 | }; |
| 213 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 214 | int dm_check_devices(struct unit_test_state *uts, int num_devices) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 215 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 216 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 217 | int ret; |
| 218 | int i; |
| 219 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 220 | /* |
| 221 | * Now check that the ping adds are what we expect. This is using the |
| 222 | * ping-add property in each node. |
| 223 | */ |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 224 | for (i = 0; i < num_devices; i++) { |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 225 | uint32_t base; |
| 226 | |
| 227 | ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev); |
| 228 | ut_assert(!ret); |
| 229 | |
| 230 | /* |
Simon Glass | a7bb08a | 2014-07-23 06:54:57 -0600 | [diff] [blame] | 231 | * Get the 'ping-expect' property, which tells us what the |
| 232 | * ping add should be. We don't use the platdata because we |
| 233 | * want to test the code that sets that up |
| 234 | * (testfdt_drv_probe()). |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 235 | */ |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 236 | base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev), |
Simon Glass | a7bb08a | 2014-07-23 06:54:57 -0600 | [diff] [blame] | 237 | "ping-expect"); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 238 | debug("dev=%d, base=%d: %s\n", i, base, |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 239 | fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 240 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 241 | ut_assert(!dm_check_operations(uts, dev, base, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 242 | dev_get_priv(dev))); |
| 243 | } |
| 244 | |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /* Test that FDT-based binding works correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 249 | static int dm_test_fdt(struct unit_test_state *uts) |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 250 | { |
Bin Meng | d9d2478 | 2018-10-10 22:07:01 -0700 | [diff] [blame] | 251 | const int num_devices = 8; |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 252 | struct udevice *dev; |
| 253 | struct uclass *uc; |
| 254 | int ret; |
| 255 | int i; |
| 256 | |
| 257 | ret = dm_scan_fdt(gd->fdt_blob, false); |
| 258 | ut_assert(!ret); |
| 259 | |
| 260 | ret = uclass_get(UCLASS_TEST_FDT, &uc); |
| 261 | ut_assert(!ret); |
| 262 | |
| 263 | /* These are num_devices compatible root-level device tree nodes */ |
| 264 | ut_asserteq(num_devices, list_count_items(&uc->dev_head)); |
| 265 | |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 266 | /* Each should have platform data but no private data */ |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 267 | for (i = 0; i < num_devices; i++) { |
| 268 | ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev); |
| 269 | ut_assert(!ret); |
| 270 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 271 | ut_assert(dev->platdata); |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 272 | } |
| 273 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 274 | ut_assertok(dm_check_devices(uts, num_devices)); |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 275 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | DM_TEST(dm_test_fdt, 0); |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 279 | |
Michal Simek | 4247cf4 | 2019-01-31 16:31:00 +0100 | [diff] [blame] | 280 | static int dm_test_alias_highest_id(struct unit_test_state *uts) |
| 281 | { |
| 282 | int ret; |
| 283 | |
| 284 | ret = dev_read_alias_highest_id("eth"); |
| 285 | ut_asserteq(5, ret); |
| 286 | |
| 287 | ret = dev_read_alias_highest_id("gpio"); |
| 288 | ut_asserteq(2, ret); |
| 289 | |
| 290 | ret = dev_read_alias_highest_id("pci"); |
| 291 | ut_asserteq(2, ret); |
| 292 | |
| 293 | ret = dev_read_alias_highest_id("i2c"); |
| 294 | ut_asserteq(0, ret); |
| 295 | |
| 296 | ret = dev_read_alias_highest_id("deadbeef"); |
| 297 | ut_asserteq(-1, ret); |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | DM_TEST(dm_test_alias_highest_id, 0); |
| 302 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 303 | static int dm_test_fdt_pre_reloc(struct unit_test_state *uts) |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 304 | { |
| 305 | struct uclass *uc; |
| 306 | int ret; |
| 307 | |
| 308 | ret = dm_scan_fdt(gd->fdt_blob, true); |
| 309 | ut_assert(!ret); |
| 310 | |
| 311 | ret = uclass_get(UCLASS_TEST_FDT, &uc); |
| 312 | ut_assert(!ret); |
| 313 | |
Bin Meng | d9d2478 | 2018-10-10 22:07:01 -0700 | [diff] [blame] | 314 | /* |
| 315 | * These are 2 pre-reloc devices: |
| 316 | * one with "u-boot,dm-pre-reloc" property (a-test node), and the other |
| 317 | * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node). |
| 318 | */ |
| 319 | ut_asserteq(2, list_count_items(&uc->dev_head)); |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | DM_TEST(dm_test_fdt_pre_reloc, 0); |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 324 | |
| 325 | /* Test that sequence numbers are allocated properly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 326 | static int dm_test_fdt_uclass_seq(struct unit_test_state *uts) |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 327 | { |
| 328 | struct udevice *dev; |
| 329 | |
| 330 | /* A few basic santiy tests */ |
| 331 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev)); |
| 332 | ut_asserteq_str("b-test", dev->name); |
| 333 | |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 334 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev)); |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 335 | ut_asserteq_str("a-test", dev->name); |
| 336 | |
| 337 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5, |
| 338 | true, &dev)); |
| 339 | ut_asserteq_ptr(NULL, dev); |
| 340 | |
| 341 | /* Test aliases */ |
| 342 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev)); |
| 343 | ut_asserteq_str("e-test", dev->name); |
| 344 | |
| 345 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7, |
| 346 | true, &dev)); |
| 347 | |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 348 | /* |
| 349 | * Note that c-test nodes are not probed since it is not a top-level |
| 350 | * node |
| 351 | */ |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 352 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev)); |
| 353 | ut_asserteq_str("b-test", dev->name); |
| 354 | |
| 355 | /* |
| 356 | * d-test wants sequence number 3 also, but it can't have it because |
| 357 | * b-test gets it first. |
| 358 | */ |
| 359 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev)); |
| 360 | ut_asserteq_str("d-test", dev->name); |
| 361 | |
| 362 | /* d-test actually gets 0 */ |
| 363 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 0, &dev)); |
| 364 | ut_asserteq_str("d-test", dev->name); |
| 365 | |
| 366 | /* initially no one wants seq 1 */ |
| 367 | ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, |
| 368 | &dev)); |
| 369 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 370 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev)); |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 371 | |
| 372 | /* But now that it is probed, we can find it */ |
| 373 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, &dev)); |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 374 | ut_asserteq_str("f-test", dev->name); |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 379 | |
| 380 | /* Test that we can find a device by device tree offset */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 381 | static int dm_test_fdt_offset(struct unit_test_state *uts) |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 382 | { |
| 383 | const void *blob = gd->fdt_blob; |
| 384 | struct udevice *dev; |
| 385 | int node; |
| 386 | |
| 387 | node = fdt_path_offset(blob, "/e-test"); |
| 388 | ut_assert(node > 0); |
| 389 | ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node, |
| 390 | &dev)); |
| 391 | ut_asserteq_str("e-test", dev->name); |
| 392 | |
| 393 | /* This node should not be bound */ |
| 394 | node = fdt_path_offset(blob, "/junk"); |
| 395 | ut_assert(node > 0); |
| 396 | ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, |
| 397 | node, &dev)); |
| 398 | |
| 399 | /* This is not a top level node so should not be probed */ |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 400 | node = fdt_path_offset(blob, "/some-bus/c-test@5"); |
Simon Glass | c1464ab | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 401 | ut_assert(node > 0); |
| 402 | ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, |
| 403 | node, &dev)); |
| 404 | |
| 405 | return 0; |
| 406 | } |
Simon Glass | 70b66e7 | 2017-05-18 20:09:44 -0600 | [diff] [blame] | 407 | DM_TEST(dm_test_fdt_offset, |
| 408 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE); |
Simon Glass | 9c433fe | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 409 | |
| 410 | /** |
| 411 | * Test various error conditions with uclass_first_device() and |
| 412 | * uclass_next_device() |
| 413 | */ |
| 414 | static int dm_test_first_next_device(struct unit_test_state *uts) |
| 415 | { |
| 416 | struct dm_testprobe_pdata *pdata; |
| 417 | struct udevice *dev, *parent = NULL; |
| 418 | int count; |
| 419 | int ret; |
| 420 | |
| 421 | /* There should be 4 devices */ |
| 422 | for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0; |
| 423 | dev; |
| 424 | ret = uclass_next_device(&dev)) { |
| 425 | count++; |
| 426 | parent = dev_get_parent(dev); |
| 427 | } |
| 428 | ut_assertok(ret); |
| 429 | ut_asserteq(4, count); |
| 430 | |
| 431 | /* Remove them and try again, with an error on the second one */ |
| 432 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev)); |
| 433 | pdata = dev_get_platdata(dev); |
| 434 | pdata->probe_err = -ENOMEM; |
| 435 | device_remove(parent, DM_REMOVE_NORMAL); |
| 436 | ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev)); |
| 437 | ut_asserteq(-ENOMEM, uclass_next_device(&dev)); |
| 438 | ut_asserteq_ptr(dev, NULL); |
| 439 | |
| 440 | /* Now an error on the first one */ |
| 441 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev)); |
| 442 | pdata = dev_get_platdata(dev); |
| 443 | pdata->probe_err = -ENOENT; |
| 444 | device_remove(parent, DM_REMOVE_NORMAL); |
| 445 | ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev)); |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | DM_TEST(dm_test_first_next_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 3ff83bc | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 450 | |
Simon Glass | 27c0e17 | 2020-02-06 09:54:49 -0700 | [diff] [blame] | 451 | /* Test iteration through devices in a uclass */ |
| 452 | static int dm_test_uclass_foreach(struct unit_test_state *uts) |
| 453 | { |
| 454 | struct udevice *dev; |
| 455 | struct uclass *uc; |
| 456 | int count; |
| 457 | |
| 458 | count = 0; |
| 459 | uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc) |
| 460 | count++; |
| 461 | ut_asserteq(8, count); |
| 462 | |
| 463 | count = 0; |
| 464 | uclass_foreach_dev(dev, uc) |
| 465 | count++; |
| 466 | ut_asserteq(8, count); |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | DM_TEST(dm_test_uclass_foreach, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 471 | |
Simon Glass | 3ff83bc | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 472 | /** |
| 473 | * check_devices() - Check return values and pointers |
| 474 | * |
| 475 | * This runs through a full sequence of uclass_first_device_check()... |
| 476 | * uclass_next_device_check() checking that the return values and devices |
| 477 | * are correct. |
| 478 | * |
| 479 | * @uts: Test state |
| 480 | * @devlist: List of expected devices |
| 481 | * @mask: Indicates which devices should return an error. Device n should |
| 482 | * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if |
| 483 | * bit n is clear. |
| 484 | */ |
| 485 | static int check_devices(struct unit_test_state *uts, |
| 486 | struct udevice *devlist[], int mask) |
| 487 | { |
| 488 | int expected_ret; |
| 489 | struct udevice *dev; |
| 490 | int i; |
| 491 | |
| 492 | expected_ret = (mask & 1) ? -ENOENT : 0; |
| 493 | mask >>= 1; |
| 494 | ut_asserteq(expected_ret, |
| 495 | uclass_first_device_check(UCLASS_TEST_PROBE, &dev)); |
| 496 | for (i = 0; i < 4; i++) { |
| 497 | ut_asserteq_ptr(devlist[i], dev); |
| 498 | expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0; |
| 499 | mask >>= 1; |
| 500 | ut_asserteq(expected_ret, uclass_next_device_check(&dev)); |
| 501 | } |
| 502 | ut_asserteq_ptr(NULL, dev); |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | /* Test uclass_first_device_check() and uclass_next_device_check() */ |
| 508 | static int dm_test_first_next_ok_device(struct unit_test_state *uts) |
| 509 | { |
| 510 | struct dm_testprobe_pdata *pdata; |
| 511 | struct udevice *dev, *parent = NULL, *devlist[4]; |
| 512 | int count; |
| 513 | int ret; |
| 514 | |
| 515 | /* There should be 4 devices */ |
| 516 | count = 0; |
| 517 | for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev); |
| 518 | dev; |
| 519 | ret = uclass_next_device_check(&dev)) { |
| 520 | ut_assertok(ret); |
| 521 | devlist[count++] = dev; |
| 522 | parent = dev_get_parent(dev); |
| 523 | } |
| 524 | ut_asserteq(4, count); |
| 525 | ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev)); |
| 526 | ut_assertok(check_devices(uts, devlist, 0)); |
| 527 | |
| 528 | /* Remove them and try again, with an error on the second one */ |
| 529 | pdata = dev_get_platdata(devlist[1]); |
| 530 | pdata->probe_err = -ENOENT - 1; |
| 531 | device_remove(parent, DM_REMOVE_NORMAL); |
| 532 | ut_assertok(check_devices(uts, devlist, 1 << 1)); |
| 533 | |
| 534 | /* Now an error on the first one */ |
| 535 | pdata = dev_get_platdata(devlist[0]); |
| 536 | pdata->probe_err = -ENOENT - 0; |
| 537 | device_remove(parent, DM_REMOVE_NORMAL); |
| 538 | ut_assertok(check_devices(uts, devlist, 3 << 0)); |
| 539 | |
| 540 | /* Now errors on all */ |
| 541 | pdata = dev_get_platdata(devlist[2]); |
| 542 | pdata->probe_err = -ENOENT - 2; |
| 543 | pdata = dev_get_platdata(devlist[3]); |
| 544 | pdata->probe_err = -ENOENT - 3; |
| 545 | device_remove(parent, DM_REMOVE_NORMAL); |
| 546 | ut_assertok(check_devices(uts, devlist, 0xf << 0)); |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | DM_TEST(dm_test_first_next_ok_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 551 | |
| 552 | static const struct udevice_id fdt_dummy_ids[] = { |
| 553 | { .compatible = "denx,u-boot-fdt-dummy", }, |
| 554 | { } |
| 555 | }; |
| 556 | |
| 557 | UCLASS_DRIVER(fdt_dummy) = { |
Eugeniu Rosca | 5ba71e5 | 2018-05-19 14:13:55 +0200 | [diff] [blame] | 558 | .name = "fdt-dummy", |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 559 | .id = UCLASS_TEST_DUMMY, |
| 560 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 561 | }; |
| 562 | |
| 563 | U_BOOT_DRIVER(fdt_dummy_drv) = { |
| 564 | .name = "fdt_dummy_drv", |
| 565 | .of_match = fdt_dummy_ids, |
| 566 | .id = UCLASS_TEST_DUMMY, |
| 567 | }; |
| 568 | |
| 569 | static int dm_test_fdt_translation(struct unit_test_state *uts) |
| 570 | { |
| 571 | struct udevice *dev; |
Fabien Dessenne | 22236e0 | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 572 | fdt32_t dma_addr[2]; |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 573 | |
| 574 | /* Some simple translations */ |
| 575 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); |
| 576 | ut_asserteq_str("dev@0,0", dev->name); |
| 577 | ut_asserteq(0x8000, dev_read_addr(dev)); |
| 578 | |
| 579 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev)); |
| 580 | ut_asserteq_str("dev@1,100", dev->name); |
| 581 | ut_asserteq(0x9000, dev_read_addr(dev)); |
| 582 | |
| 583 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, true, &dev)); |
| 584 | ut_asserteq_str("dev@2,200", dev->name); |
| 585 | ut_asserteq(0xA000, dev_read_addr(dev)); |
| 586 | |
| 587 | /* No translation for busses with #size-cells == 0 */ |
| 588 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, true, &dev)); |
| 589 | ut_asserteq_str("dev@42", dev->name); |
| 590 | ut_asserteq(0x42, dev_read_addr(dev)); |
| 591 | |
Fabien Dessenne | 22236e0 | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 592 | /* dma address translation */ |
| 593 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); |
| 594 | dma_addr[0] = cpu_to_be32(0); |
| 595 | dma_addr[1] = cpu_to_be32(0); |
| 596 | ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr)); |
| 597 | |
| 598 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev)); |
| 599 | dma_addr[0] = cpu_to_be32(1); |
| 600 | dma_addr[1] = cpu_to_be32(0x100); |
| 601 | ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr)); |
| 602 | |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 603 | return 0; |
| 604 | } |
| 605 | DM_TEST(dm_test_fdt_translation, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 606 | |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 607 | static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts) |
| 608 | { |
| 609 | struct udevice *dev; |
| 610 | fdt_addr_t addr; |
| 611 | void *paddr; |
| 612 | |
| 613 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); |
| 614 | |
| 615 | addr = devfdt_get_addr(dev); |
| 616 | ut_asserteq(0x8000, addr); |
| 617 | |
| 618 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 619 | ut_assertnonnull(paddr); |
| 620 | ut_asserteq_ptr(paddr, devfdt_remap_addr(dev)); |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | DM_TEST(dm_test_fdt_remap_addr_flat, |
| 625 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE); |
| 626 | |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 627 | static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts) |
| 628 | { |
| 629 | struct udevice *dev; |
| 630 | fdt_addr_t addr; |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 631 | fdt_size_t size; |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 632 | void *paddr; |
| 633 | |
| 634 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); |
| 635 | |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 636 | addr = devfdt_get_addr_size_index(dev, 0, &size); |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 637 | ut_asserteq(0x8000, addr); |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 638 | ut_asserteq(0x1000, size); |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 639 | |
| 640 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 641 | ut_assertnonnull(paddr); |
| 642 | ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0)); |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | DM_TEST(dm_test_fdt_remap_addr_index_flat, |
| 647 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE); |
| 648 | |
| 649 | static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts) |
| 650 | { |
| 651 | struct udevice *dev; |
| 652 | fdt_addr_t addr; |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 653 | fdt_size_t size; |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 654 | void *paddr; |
| 655 | |
| 656 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); |
| 657 | |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 658 | addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size); |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 659 | ut_asserteq(0x8000, addr); |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 660 | ut_asserteq(0x1000, size); |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 661 | |
| 662 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 663 | ut_assertnonnull(paddr); |
| 664 | ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0")); |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | DM_TEST(dm_test_fdt_remap_addr_name_flat, |
| 669 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE); |
| 670 | |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 671 | static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts) |
| 672 | { |
| 673 | struct udevice *dev; |
| 674 | fdt_addr_t addr; |
| 675 | void *paddr; |
| 676 | |
| 677 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); |
| 678 | |
| 679 | addr = dev_read_addr(dev); |
| 680 | ut_asserteq(0x8000, addr); |
| 681 | |
| 682 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 683 | ut_assertnonnull(paddr); |
| 684 | ut_asserteq_ptr(paddr, dev_remap_addr(dev)); |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | DM_TEST(dm_test_fdt_remap_addr_live, |
| 689 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Mario Six | 058ef5b | 2018-06-26 08:46:49 +0200 | [diff] [blame] | 690 | |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 691 | static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts) |
| 692 | { |
| 693 | struct udevice *dev; |
| 694 | fdt_addr_t addr; |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 695 | fdt_size_t size; |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 696 | void *paddr; |
| 697 | |
| 698 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); |
| 699 | |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 700 | addr = dev_read_addr_size_index(dev, 0, &size); |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 701 | ut_asserteq(0x8000, addr); |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 702 | ut_asserteq(0x1000, size); |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 703 | |
| 704 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 705 | ut_assertnonnull(paddr); |
| 706 | ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0)); |
| 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | DM_TEST(dm_test_fdt_remap_addr_index_live, |
| 711 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 712 | |
| 713 | static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts) |
| 714 | { |
| 715 | struct udevice *dev; |
| 716 | fdt_addr_t addr; |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 717 | fdt_size_t size; |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 718 | void *paddr; |
| 719 | |
| 720 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); |
| 721 | |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 722 | addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size); |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 723 | ut_asserteq(0x8000, addr); |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 724 | ut_asserteq(0x1000, size); |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 725 | |
| 726 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 727 | ut_assertnonnull(paddr); |
| 728 | ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0")); |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | DM_TEST(dm_test_fdt_remap_addr_name_live, |
| 733 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 734 | |
Mario Six | 058ef5b | 2018-06-26 08:46:49 +0200 | [diff] [blame] | 735 | static int dm_test_fdt_livetree_writing(struct unit_test_state *uts) |
| 736 | { |
| 737 | struct udevice *dev; |
| 738 | ofnode node; |
| 739 | |
| 740 | if (!of_live_active()) { |
| 741 | printf("Live tree not active; ignore test\n"); |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | /* Test enabling devices */ |
| 746 | |
| 747 | node = ofnode_path("/usb@2"); |
| 748 | |
| 749 | ut_assert(!of_device_is_available(ofnode_to_np(node))); |
| 750 | ofnode_set_enabled(node, true); |
| 751 | ut_assert(of_device_is_available(ofnode_to_np(node))); |
| 752 | |
| 753 | device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node, |
| 754 | &dev); |
| 755 | ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, true, &dev)); |
| 756 | |
| 757 | /* Test string property setting */ |
| 758 | |
| 759 | ut_assert(device_is_compatible(dev, "sandbox,usb")); |
| 760 | ofnode_write_string(node, "compatible", "gdsys,super-usb"); |
| 761 | ut_assert(device_is_compatible(dev, "gdsys,super-usb")); |
| 762 | ofnode_write_string(node, "compatible", "sandbox,usb"); |
| 763 | ut_assert(device_is_compatible(dev, "sandbox,usb")); |
| 764 | |
| 765 | /* Test setting generic properties */ |
| 766 | |
| 767 | /* Non-existent in DTB */ |
| 768 | ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev)); |
| 769 | /* reg = 0x42, size = 0x100 */ |
| 770 | ut_assertok(ofnode_write_prop(node, "reg", 8, |
| 771 | "\x00\x00\x00\x42\x00\x00\x01\x00")); |
| 772 | ut_asserteq(0x42, dev_read_addr(dev)); |
| 773 | |
| 774 | /* Test disabling devices */ |
| 775 | |
| 776 | device_remove(dev, DM_REMOVE_NORMAL); |
| 777 | device_unbind(dev); |
| 778 | |
| 779 | ut_assert(of_device_is_available(ofnode_to_np(node))); |
| 780 | ofnode_set_enabled(node, false); |
| 781 | ut_assert(!of_device_is_available(ofnode_to_np(node))); |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | DM_TEST(dm_test_fdt_livetree_writing, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Mario Six | d6f6dc3 | 2018-06-26 08:46:51 +0200 | [diff] [blame] | 786 | |
| 787 | static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts) |
| 788 | { |
| 789 | ofnode node; |
| 790 | |
| 791 | if (!of_live_active()) { |
| 792 | printf("Live tree not active; ignore test\n"); |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | node = ofnode_path("/usb@2"); |
| 797 | |
| 798 | /* Test enabling devices */ |
| 799 | |
| 800 | ut_assert(!of_device_is_available(ofnode_to_np(node))); |
| 801 | dev_enable_by_path("/usb@2"); |
| 802 | ut_assert(of_device_is_available(ofnode_to_np(node))); |
| 803 | |
| 804 | /* Test disabling devices */ |
| 805 | |
| 806 | ut_assert(of_device_is_available(ofnode_to_np(node))); |
| 807 | dev_disable_by_path("/usb@2"); |
| 808 | ut_assert(!of_device_is_available(ofnode_to_np(node))); |
| 809 | |
| 810 | return 0; |
| 811 | } |
| 812 | DM_TEST(dm_test_fdt_disable_enable_by_path, DM_TESTF_SCAN_PDATA | |
| 813 | DM_TESTF_SCAN_FDT); |
Simon Glass | dc3efdc | 2018-11-18 08:14:30 -0700 | [diff] [blame] | 814 | |
| 815 | /* Test a few uclass phandle functions */ |
| 816 | static int dm_test_fdt_phandle(struct unit_test_state *uts) |
| 817 | { |
| 818 | struct udevice *back, *dev, *dev2; |
| 819 | |
| 820 | ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back)); |
| 821 | ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR, |
| 822 | back, "missing", &dev)); |
| 823 | ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back, |
| 824 | "power-supply", &dev)); |
| 825 | ut_asserteq(0, device_active(dev)); |
| 826 | ut_asserteq_str("ldo1", dev->name); |
| 827 | ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back, |
| 828 | "power-supply", &dev2)); |
| 829 | ut_asserteq_ptr(dev, dev2); |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | DM_TEST(dm_test_fdt_phandle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | a11341a | 2018-11-18 08:14:31 -0700 | [diff] [blame] | 834 | |
| 835 | /* Test device_find_first_child_by_uclass() */ |
| 836 | static int dm_test_first_child(struct unit_test_state *uts) |
| 837 | { |
| 838 | struct udevice *i2c, *dev, *dev2; |
| 839 | |
| 840 | ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c)); |
| 841 | ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev)); |
| 842 | ut_asserteq_str("rtc@43", dev->name); |
| 843 | ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2)); |
| 844 | ut_asserteq_ptr(dev, dev2); |
| 845 | ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2)); |
| 846 | ut_asserteq_str("rtc@61", dev2->name); |
| 847 | |
| 848 | ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM, |
| 849 | &dev)); |
| 850 | ut_asserteq_str("eeprom@2c", dev->name); |
| 851 | ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2)); |
| 852 | ut_asserteq_ptr(dev, dev2); |
| 853 | |
| 854 | ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c, |
| 855 | UCLASS_VIDEO, &dev)); |
| 856 | ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev)); |
| 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | DM_TEST(dm_test_first_child, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 6df01f9 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 861 | |
| 862 | /* Test integer functions in dm_read_...() */ |
| 863 | static int dm_test_read_int(struct unit_test_state *uts) |
| 864 | { |
| 865 | struct udevice *dev; |
| 866 | u32 val32; |
| 867 | s32 sval; |
| 868 | uint val; |
| 869 | |
| 870 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); |
| 871 | ut_asserteq_str("a-test", dev->name); |
| 872 | ut_assertok(dev_read_u32(dev, "int-value", &val32)); |
| 873 | ut_asserteq(1234, val32); |
| 874 | |
| 875 | ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32)); |
| 876 | ut_asserteq(6, dev_read_u32_default(dev, "missing", 6)); |
| 877 | |
| 878 | ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6)); |
| 879 | ut_asserteq(1234, val32); |
| 880 | |
| 881 | ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval)); |
| 882 | ut_asserteq(6, dev_read_s32_default(dev, "missing", 6)); |
| 883 | |
| 884 | ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6)); |
| 885 | ut_assertok(dev_read_s32(dev, "uint-value", &sval)); |
| 886 | ut_asserteq(-1234, sval); |
| 887 | |
| 888 | val = 0; |
| 889 | ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val)); |
| 890 | ut_assertok(dev_read_u32u(dev, "uint-value", &val)); |
| 891 | ut_asserteq(-1234, val); |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | DM_TEST(dm_test_read_int, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 35cb2a4 | 2020-02-06 09:54:50 -0700 | [diff] [blame^] | 896 | |
| 897 | /* Test iteration through devices by drvdata */ |
| 898 | static int dm_test_uclass_drvdata(struct unit_test_state *uts) |
| 899 | { |
| 900 | struct udevice *dev; |
| 901 | |
| 902 | ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT, |
| 903 | DM_TEST_TYPE_FIRST, &dev)); |
| 904 | ut_asserteq_str("a-test", dev->name); |
| 905 | |
| 906 | ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT, |
| 907 | DM_TEST_TYPE_SECOND, &dev)); |
| 908 | ut_asserteq_str("d-test", dev->name); |
| 909 | |
| 910 | ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT, |
| 911 | DM_TEST_TYPE_COUNT, |
| 912 | &dev)); |
| 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | DM_TEST(dm_test_uclass_drvdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |