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