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