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