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 | { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 28 | const struct dm_test_pdata *pdata = dev->plat; |
Simon Glass | 2e7d35d | 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 | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 41 | static int testfdt_of_to_plat(struct udevice *dev) |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 42 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 43 | struct dm_test_pdata *pdata = dev_get_plat(dev); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 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, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 86 | .of_to_plat = testfdt_of_to_plat, |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 87 | .probe = testfdt_drv_probe, |
| 88 | .ops = &test_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 89 | .priv_auto = sizeof(struct dm_test_priv), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 90 | .plat_auto = sizeof(struct dm_test_pdata), |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 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, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 104 | .of_to_plat = testfdt_of_to_plat, |
Bin Meng | 2786cd7 | 2018-10-10 22:07:01 -0700 | [diff] [blame] | 105 | .probe = testfdt_drv_probe, |
| 106 | .ops = &test_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 107 | .priv_auto = sizeof(struct dm_test_priv), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 108 | .plat_auto = sizeof(struct dm_test_pdata), |
Bin Meng | 2786cd7 | 2018-10-10 22:07:01 -0700 | [diff] [blame] | 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 | 93f44e8 | 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 | 9856157 | 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 | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 152 | struct dm_testprobe_pdata *pdata = dev_get_plat(dev); |
Simon Glass | 9856157 | 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 | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 167 | .plat_auto = sizeof(struct dm_testprobe_pdata), |
Simon Glass | 9856157 | 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 | dc12ebb | 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 | 42a0ce5 | 2019-12-29 21:19:28 -0700 | [diff] [blame] | 182 | void *ptr_ofdata; |
Simon Glass | dc12ebb | 2019-12-29 21:19:25 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | static int testdevres_drv_bind(struct udevice *dev) |
| 186 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 187 | struct dm_testdevres_pdata *pdata = dev_get_plat(dev); |
Simon Glass | dc12ebb | 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 | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 194 | static int testdevres_drv_of_to_plat(struct udevice *dev) |
Simon Glass | 42a0ce5 | 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 | dc12ebb | 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 | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 222 | .of_to_plat = testdevres_drv_of_to_plat, |
Simon Glass | dc12ebb | 2019-12-29 21:19:25 -0700 | [diff] [blame] | 223 | .probe = testdevres_drv_probe, |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 224 | .plat_auto = sizeof(struct dm_testdevres_pdata), |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 225 | .priv_auto = sizeof(struct dm_testdevres_priv), |
Simon Glass | dc12ebb | 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 | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 234 | int dm_check_devices(struct unit_test_state *uts, int num_devices) |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 235 | { |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 236 | struct udevice *dev; |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 237 | int ret; |
| 238 | int i; |
| 239 | |
Simon Glass | 2e7d35d | 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 | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 244 | for (i = 0; i < num_devices; i++) { |
Simon Glass | 2e7d35d | 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 | eb9ef5f | 2014-07-23 06:54:57 -0600 | [diff] [blame] | 251 | * Get the 'ping-expect' property, which tells us what the |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 252 | * ping add should be. We don't use the plat because we |
Simon Glass | eb9ef5f | 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 | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 255 | */ |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 256 | base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev), |
Simon Glass | eb9ef5f | 2014-07-23 06:54:57 -0600 | [diff] [blame] | 257 | "ping-expect"); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 258 | debug("dev=%d, base=%d: %s\n", i, base, |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 259 | fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL)); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 260 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 261 | ut_assert(!dm_check_operations(uts, dev, base, |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 262 | dev_get_priv(dev))); |
| 263 | } |
| 264 | |
| 265 | return 0; |
| 266 | } |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 267 | |
| 268 | /* Test that FDT-based binding works correctly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 269 | static int dm_test_fdt(struct unit_test_state *uts) |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 270 | { |
Jean-Jacques Hiblot | 88e6a60 | 2020-09-11 13:43:35 +0530 | [diff] [blame] | 271 | const int num_devices = 9; |
Simon Glass | 1ca7e20 | 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 | 8ee05b5 | 2020-11-28 17:50:10 -0700 | [diff] [blame] | 277 | ret = dm_extended_scan(false); |
Simon Glass | 1ca7e20 | 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 | f8a8544 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 286 | /* Each should have platform data but no private data */ |
Simon Glass | 1ca7e20 | 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 | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 291 | ut_assert(dev->plat); |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 292 | } |
| 293 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 294 | ut_assertok(dm_check_devices(uts, num_devices)); |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 295 | |
| 296 | return 0; |
| 297 | } |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 298 | DM_TEST(dm_test_fdt, 0); |
Simon Glass | 00606d7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 299 | |
Michal Simek | a93eb57 | 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 | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 308 | ut_asserteq(3, ret); |
Michal Simek | a93eb57 | 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 | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 323 | static int dm_test_fdt_pre_reloc(struct unit_test_state *uts) |
Simon Glass | 00606d7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 324 | { |
| 325 | struct uclass *uc; |
| 326 | int ret; |
| 327 | |
Simon Glass | 725e4fc | 2020-11-28 17:50:09 -0700 | [diff] [blame] | 328 | ret = dm_scan_fdt(true); |
Simon Glass | 00606d7 | 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 | 2786cd7 | 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 | 00606d7 | 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 | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 344 | |
| 345 | /* Test that sequence numbers are allocated properly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 346 | static int dm_test_fdt_uclass_seq(struct unit_test_state *uts) |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 347 | { |
| 348 | struct udevice *dev; |
| 349 | |
| 350 | /* A few basic santiy tests */ |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 351 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, &dev)); |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 352 | ut_asserteq_str("b-test", dev->name); |
Simon Glass | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 353 | ut_asserteq(3, dev_seq(dev)); |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 354 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 355 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, &dev)); |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 356 | ut_asserteq_str("a-test", dev->name); |
Simon Glass | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 357 | ut_asserteq(8, dev_seq(dev)); |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 358 | |
Simon Glass | 981426e | 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 | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 363 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 13, &dev)); |
Simon Glass | 981426e | 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 | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 367 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9, |
Simon Glass | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 368 | &dev)); |
Simon Glass | 5a66a8f | 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 | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 374 | ut_asserteq(6, dev_seq(dev)); |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 375 | |
Simon Glass | 1ca7e20 | 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 | 5a66a8f | 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 | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 382 | ut_asserteq(3, dev_seq(dev)); |
Simon Glass | 5a66a8f | 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 | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 390 | ut_asserteq(13, dev_seq(dev)); |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 391 | |
Simon Glass | 981426e | 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 | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 396 | |
Simon Glass | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 397 | /* And we should still have holes in our sequence numbers */ |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 398 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 0, |
Simon Glass | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 399 | &dev)); |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 400 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 1, |
Simon Glass | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 401 | &dev)); |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 402 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2, |
Simon Glass | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 403 | &dev)); |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 404 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4, |
Simon Glass | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 405 | &dev)); |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 406 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7, |
Simon Glass | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 407 | &dev)); |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 408 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9, |
Simon Glass | 981426e | 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 | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 411 | &dev)); |
Simon Glass | 981426e | 2020-12-16 21:20:26 -0700 | [diff] [blame] | 412 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 11, |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 413 | &dev)); |
Michael Walle | be1a6e9 | 2020-06-02 01:47:09 +0200 | [diff] [blame] | 414 | |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 415 | return 0; |
| 416 | } |
Simon Glass | e180c2b | 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 | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 418 | |
Simon Glass | 93f44e8 | 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 | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 444 | /* Test that we can find a device by device tree offset */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 445 | static int dm_test_fdt_offset(struct unit_test_state *uts) |
Simon Glass | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 446 | { |
| 447 | const void *blob = gd->fdt_blob; |
| 448 | struct udevice *dev; |
| 449 | int node; |
| 450 | |
| 451 | node = fdt_path_offset(blob, "/e-test"); |
| 452 | ut_assert(node > 0); |
| 453 | ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node, |
| 454 | &dev)); |
| 455 | ut_asserteq_str("e-test", dev->name); |
| 456 | |
| 457 | /* This node should not be bound */ |
| 458 | node = fdt_path_offset(blob, "/junk"); |
| 459 | ut_assert(node > 0); |
| 460 | ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, |
| 461 | node, &dev)); |
| 462 | |
| 463 | /* This is not a top level node so should not be probed */ |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 464 | node = fdt_path_offset(blob, "/some-bus/c-test@5"); |
Simon Glass | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 465 | ut_assert(node > 0); |
| 466 | ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, |
| 467 | node, &dev)); |
| 468 | |
| 469 | return 0; |
| 470 | } |
Simon Glass | 86b54ec | 2017-05-18 20:09:44 -0600 | [diff] [blame] | 471 | DM_TEST(dm_test_fdt_offset, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 472 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * Test various error conditions with uclass_first_device() and |
| 476 | * uclass_next_device() |
| 477 | */ |
| 478 | static int dm_test_first_next_device(struct unit_test_state *uts) |
| 479 | { |
| 480 | struct dm_testprobe_pdata *pdata; |
| 481 | struct udevice *dev, *parent = NULL; |
| 482 | int count; |
| 483 | int ret; |
| 484 | |
| 485 | /* There should be 4 devices */ |
| 486 | for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0; |
| 487 | dev; |
| 488 | ret = uclass_next_device(&dev)) { |
| 489 | count++; |
| 490 | parent = dev_get_parent(dev); |
| 491 | } |
| 492 | ut_assertok(ret); |
| 493 | ut_asserteq(4, count); |
| 494 | |
| 495 | /* Remove them and try again, with an error on the second one */ |
| 496 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev)); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 497 | pdata = dev_get_plat(dev); |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 498 | pdata->probe_err = -ENOMEM; |
| 499 | device_remove(parent, DM_REMOVE_NORMAL); |
| 500 | ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev)); |
| 501 | ut_asserteq(-ENOMEM, uclass_next_device(&dev)); |
| 502 | ut_asserteq_ptr(dev, NULL); |
| 503 | |
| 504 | /* Now an error on the first one */ |
| 505 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev)); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 506 | pdata = dev_get_plat(dev); |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 507 | pdata->probe_err = -ENOENT; |
| 508 | device_remove(parent, DM_REMOVE_NORMAL); |
| 509 | ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev)); |
| 510 | |
| 511 | return 0; |
| 512 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 513 | 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] | 514 | |
Simon Glass | 3cf0fba | 2020-02-06 09:54:49 -0700 | [diff] [blame] | 515 | /* Test iteration through devices in a uclass */ |
| 516 | static int dm_test_uclass_foreach(struct unit_test_state *uts) |
| 517 | { |
| 518 | struct udevice *dev; |
| 519 | struct uclass *uc; |
| 520 | int count; |
| 521 | |
| 522 | count = 0; |
| 523 | uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc) |
| 524 | count++; |
Jean-Jacques Hiblot | 88e6a60 | 2020-09-11 13:43:35 +0530 | [diff] [blame] | 525 | ut_asserteq(9, count); |
Simon Glass | 3cf0fba | 2020-02-06 09:54:49 -0700 | [diff] [blame] | 526 | |
| 527 | count = 0; |
| 528 | uclass_foreach_dev(dev, uc) |
| 529 | count++; |
Jean-Jacques Hiblot | 88e6a60 | 2020-09-11 13:43:35 +0530 | [diff] [blame] | 530 | ut_asserteq(9, count); |
Simon Glass | 3cf0fba | 2020-02-06 09:54:49 -0700 | [diff] [blame] | 531 | |
| 532 | return 0; |
| 533 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 534 | 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] | 535 | |
Simon Glass | 95ce385 | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 536 | /** |
| 537 | * check_devices() - Check return values and pointers |
| 538 | * |
| 539 | * This runs through a full sequence of uclass_first_device_check()... |
| 540 | * uclass_next_device_check() checking that the return values and devices |
| 541 | * are correct. |
| 542 | * |
| 543 | * @uts: Test state |
| 544 | * @devlist: List of expected devices |
| 545 | * @mask: Indicates which devices should return an error. Device n should |
| 546 | * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if |
| 547 | * bit n is clear. |
| 548 | */ |
| 549 | static int check_devices(struct unit_test_state *uts, |
| 550 | struct udevice *devlist[], int mask) |
| 551 | { |
| 552 | int expected_ret; |
| 553 | struct udevice *dev; |
| 554 | int i; |
| 555 | |
| 556 | expected_ret = (mask & 1) ? -ENOENT : 0; |
| 557 | mask >>= 1; |
| 558 | ut_asserteq(expected_ret, |
| 559 | uclass_first_device_check(UCLASS_TEST_PROBE, &dev)); |
| 560 | for (i = 0; i < 4; i++) { |
| 561 | ut_asserteq_ptr(devlist[i], dev); |
| 562 | expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0; |
| 563 | mask >>= 1; |
| 564 | ut_asserteq(expected_ret, uclass_next_device_check(&dev)); |
| 565 | } |
| 566 | ut_asserteq_ptr(NULL, dev); |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /* Test uclass_first_device_check() and uclass_next_device_check() */ |
| 572 | static int dm_test_first_next_ok_device(struct unit_test_state *uts) |
| 573 | { |
| 574 | struct dm_testprobe_pdata *pdata; |
| 575 | struct udevice *dev, *parent = NULL, *devlist[4]; |
| 576 | int count; |
| 577 | int ret; |
| 578 | |
| 579 | /* There should be 4 devices */ |
| 580 | count = 0; |
| 581 | for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev); |
| 582 | dev; |
| 583 | ret = uclass_next_device_check(&dev)) { |
| 584 | ut_assertok(ret); |
| 585 | devlist[count++] = dev; |
| 586 | parent = dev_get_parent(dev); |
| 587 | } |
| 588 | ut_asserteq(4, count); |
| 589 | ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev)); |
| 590 | ut_assertok(check_devices(uts, devlist, 0)); |
| 591 | |
| 592 | /* Remove them and try again, with an error on the second one */ |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 593 | pdata = dev_get_plat(devlist[1]); |
Simon Glass | 95ce385 | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 594 | pdata->probe_err = -ENOENT - 1; |
| 595 | device_remove(parent, DM_REMOVE_NORMAL); |
| 596 | ut_assertok(check_devices(uts, devlist, 1 << 1)); |
| 597 | |
| 598 | /* Now an error on the first one */ |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 599 | pdata = dev_get_plat(devlist[0]); |
Simon Glass | 95ce385 | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 600 | pdata->probe_err = -ENOENT - 0; |
| 601 | device_remove(parent, DM_REMOVE_NORMAL); |
| 602 | ut_assertok(check_devices(uts, devlist, 3 << 0)); |
| 603 | |
| 604 | /* Now errors on all */ |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 605 | pdata = dev_get_plat(devlist[2]); |
Simon Glass | 95ce385 | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 606 | pdata->probe_err = -ENOENT - 2; |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 607 | pdata = dev_get_plat(devlist[3]); |
Simon Glass | 95ce385 | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 608 | pdata->probe_err = -ENOENT - 3; |
| 609 | device_remove(parent, DM_REMOVE_NORMAL); |
| 610 | ut_assertok(check_devices(uts, devlist, 0xf << 0)); |
| 611 | |
| 612 | return 0; |
| 613 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 614 | 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] | 615 | |
| 616 | static const struct udevice_id fdt_dummy_ids[] = { |
| 617 | { .compatible = "denx,u-boot-fdt-dummy", }, |
| 618 | { } |
| 619 | }; |
| 620 | |
| 621 | UCLASS_DRIVER(fdt_dummy) = { |
Eugeniu Rosca | 507cef3 | 2018-05-19 14:13:55 +0200 | [diff] [blame] | 622 | .name = "fdt-dummy", |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 623 | .id = UCLASS_TEST_DUMMY, |
| 624 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 625 | }; |
| 626 | |
| 627 | U_BOOT_DRIVER(fdt_dummy_drv) = { |
| 628 | .name = "fdt_dummy_drv", |
| 629 | .of_match = fdt_dummy_ids, |
| 630 | .id = UCLASS_TEST_DUMMY, |
| 631 | }; |
| 632 | |
| 633 | static int dm_test_fdt_translation(struct unit_test_state *uts) |
| 634 | { |
| 635 | struct udevice *dev; |
Fabien Dessenne | 641067f | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 636 | fdt32_t dma_addr[2]; |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 637 | |
| 638 | /* Some simple translations */ |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 639 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 640 | ut_asserteq_str("dev@0,0", dev->name); |
| 641 | ut_asserteq(0x8000, dev_read_addr(dev)); |
| 642 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 643 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev)); |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 644 | ut_asserteq_str("dev@1,100", dev->name); |
| 645 | ut_asserteq(0x9000, dev_read_addr(dev)); |
| 646 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 647 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, &dev)); |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 648 | ut_asserteq_str("dev@2,200", dev->name); |
| 649 | ut_asserteq(0xA000, dev_read_addr(dev)); |
| 650 | |
| 651 | /* No translation for busses with #size-cells == 0 */ |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 652 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, &dev)); |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 653 | ut_asserteq_str("dev@42", dev->name); |
| 654 | ut_asserteq(0x42, dev_read_addr(dev)); |
| 655 | |
Fabien Dessenne | 641067f | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 656 | /* dma address translation */ |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 657 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Fabien Dessenne | 641067f | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 658 | dma_addr[0] = cpu_to_be32(0); |
| 659 | dma_addr[1] = cpu_to_be32(0); |
| 660 | ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr)); |
| 661 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 662 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev)); |
Fabien Dessenne | 641067f | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 663 | dma_addr[0] = cpu_to_be32(1); |
| 664 | dma_addr[1] = cpu_to_be32(0x100); |
| 665 | ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr)); |
| 666 | |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 667 | return 0; |
| 668 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 669 | 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] | 670 | |
Ovidiu Panait | 6e64830 | 2020-08-03 22:17:36 +0300 | [diff] [blame] | 671 | static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts) |
| 672 | { |
| 673 | struct udevice *gpio, *dev; |
| 674 | void *ptr; |
| 675 | |
| 676 | /* Test for missing reg property */ |
| 677 | ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio)); |
| 678 | ut_assertnull(devfdt_get_addr_ptr(gpio)); |
| 679 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 680 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Ovidiu Panait | 6e64830 | 2020-08-03 22:17:36 +0300 | [diff] [blame] | 681 | ptr = devfdt_get_addr_ptr(dev); |
| 682 | ut_asserteq_ptr((void *)0x8000, ptr); |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | DM_TEST(dm_test_fdt_get_addr_ptr_flat, |
| 687 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
| 688 | |
Álvaro Fernández Rojas | 30a90f5 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 689 | static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts) |
| 690 | { |
| 691 | struct udevice *dev; |
| 692 | fdt_addr_t addr; |
| 693 | void *paddr; |
| 694 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 695 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Álvaro Fernández Rojas | 30a90f5 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 696 | |
| 697 | addr = devfdt_get_addr(dev); |
| 698 | ut_asserteq(0x8000, addr); |
| 699 | |
| 700 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 701 | ut_assertnonnull(paddr); |
| 702 | ut_asserteq_ptr(paddr, devfdt_remap_addr(dev)); |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | DM_TEST(dm_test_fdt_remap_addr_flat, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 707 | 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] | 708 | |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 709 | static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts) |
| 710 | { |
| 711 | struct udevice *dev; |
| 712 | fdt_addr_t addr; |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 713 | fdt_size_t size; |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 714 | void *paddr; |
| 715 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 716 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 717 | |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 718 | addr = devfdt_get_addr_size_index(dev, 0, &size); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 719 | ut_asserteq(0x8000, addr); |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 720 | ut_asserteq(0x1000, size); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 721 | |
| 722 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 723 | ut_assertnonnull(paddr); |
| 724 | ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0)); |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | DM_TEST(dm_test_fdt_remap_addr_index_flat, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 729 | 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] | 730 | |
| 731 | static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts) |
| 732 | { |
| 733 | struct udevice *dev; |
| 734 | fdt_addr_t addr; |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 735 | fdt_size_t size; |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 736 | void *paddr; |
| 737 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 738 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 739 | |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 740 | 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] | 741 | ut_asserteq(0x8000, addr); |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 742 | ut_asserteq(0x1000, size); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 743 | |
| 744 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 745 | ut_assertnonnull(paddr); |
| 746 | ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0")); |
| 747 | |
| 748 | return 0; |
| 749 | } |
| 750 | DM_TEST(dm_test_fdt_remap_addr_name_flat, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 751 | 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] | 752 | |
Álvaro Fernández Rojas | 30a90f5 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 753 | static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts) |
| 754 | { |
| 755 | struct udevice *dev; |
| 756 | fdt_addr_t addr; |
| 757 | void *paddr; |
| 758 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 759 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Álvaro Fernández Rojas | 30a90f5 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 760 | |
| 761 | addr = dev_read_addr(dev); |
| 762 | ut_asserteq(0x8000, addr); |
| 763 | |
| 764 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 765 | ut_assertnonnull(paddr); |
| 766 | ut_asserteq_ptr(paddr, dev_remap_addr(dev)); |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | DM_TEST(dm_test_fdt_remap_addr_live, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 771 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Mario Six | 2ea4d0d | 2018-06-26 08:46:49 +0200 | [diff] [blame] | 772 | |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 773 | static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts) |
| 774 | { |
| 775 | struct udevice *dev; |
| 776 | fdt_addr_t addr; |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 777 | fdt_size_t size; |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 778 | void *paddr; |
| 779 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 780 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 781 | |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 782 | addr = dev_read_addr_size_index(dev, 0, &size); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 783 | ut_asserteq(0x8000, addr); |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 784 | ut_asserteq(0x1000, size); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 785 | |
| 786 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 787 | ut_assertnonnull(paddr); |
| 788 | ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0)); |
| 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | DM_TEST(dm_test_fdt_remap_addr_index_live, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 793 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 794 | |
| 795 | static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts) |
| 796 | { |
| 797 | struct udevice *dev; |
| 798 | fdt_addr_t addr; |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 799 | fdt_size_t size; |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 800 | void *paddr; |
| 801 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 802 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 803 | |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 804 | 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] | 805 | ut_asserteq(0x8000, addr); |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 806 | ut_asserteq(0x1000, size); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 807 | |
| 808 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 809 | ut_assertnonnull(paddr); |
| 810 | ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0")); |
| 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | DM_TEST(dm_test_fdt_remap_addr_name_live, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 815 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 816 | |
Mario Six | 2ea4d0d | 2018-06-26 08:46:49 +0200 | [diff] [blame] | 817 | static int dm_test_fdt_livetree_writing(struct unit_test_state *uts) |
| 818 | { |
| 819 | struct udevice *dev; |
| 820 | ofnode node; |
| 821 | |
| 822 | if (!of_live_active()) { |
| 823 | printf("Live tree not active; ignore test\n"); |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | /* Test enabling devices */ |
| 828 | |
| 829 | node = ofnode_path("/usb@2"); |
| 830 | |
| 831 | ut_assert(!of_device_is_available(ofnode_to_np(node))); |
| 832 | ofnode_set_enabled(node, true); |
| 833 | ut_assert(of_device_is_available(ofnode_to_np(node))); |
| 834 | |
| 835 | device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node, |
| 836 | &dev); |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame^] | 837 | ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev)); |
Mario Six | 2ea4d0d | 2018-06-26 08:46:49 +0200 | [diff] [blame] | 838 | |
| 839 | /* Test string property setting */ |
| 840 | |
| 841 | ut_assert(device_is_compatible(dev, "sandbox,usb")); |
| 842 | ofnode_write_string(node, "compatible", "gdsys,super-usb"); |
| 843 | ut_assert(device_is_compatible(dev, "gdsys,super-usb")); |
| 844 | ofnode_write_string(node, "compatible", "sandbox,usb"); |
| 845 | ut_assert(device_is_compatible(dev, "sandbox,usb")); |
| 846 | |
| 847 | /* Test setting generic properties */ |
| 848 | |
| 849 | /* Non-existent in DTB */ |
| 850 | ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev)); |
| 851 | /* reg = 0x42, size = 0x100 */ |
| 852 | ut_assertok(ofnode_write_prop(node, "reg", 8, |
| 853 | "\x00\x00\x00\x42\x00\x00\x01\x00")); |
| 854 | ut_asserteq(0x42, dev_read_addr(dev)); |
| 855 | |
| 856 | /* Test disabling devices */ |
| 857 | |
| 858 | device_remove(dev, DM_REMOVE_NORMAL); |
| 859 | device_unbind(dev); |
| 860 | |
| 861 | ut_assert(of_device_is_available(ofnode_to_np(node))); |
| 862 | ofnode_set_enabled(node, false); |
| 863 | ut_assert(!of_device_is_available(ofnode_to_np(node))); |
| 864 | |
| 865 | return 0; |
| 866 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 867 | 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] | 868 | |
| 869 | static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts) |
| 870 | { |
| 871 | ofnode node; |
| 872 | |
| 873 | if (!of_live_active()) { |
| 874 | printf("Live tree not active; ignore test\n"); |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | node = ofnode_path("/usb@2"); |
| 879 | |
| 880 | /* Test enabling devices */ |
| 881 | |
| 882 | ut_assert(!of_device_is_available(ofnode_to_np(node))); |
| 883 | dev_enable_by_path("/usb@2"); |
| 884 | ut_assert(of_device_is_available(ofnode_to_np(node))); |
| 885 | |
| 886 | /* Test disabling devices */ |
| 887 | |
| 888 | ut_assert(of_device_is_available(ofnode_to_np(node))); |
| 889 | dev_disable_by_path("/usb@2"); |
| 890 | ut_assert(!of_device_is_available(ofnode_to_np(node))); |
| 891 | |
| 892 | return 0; |
| 893 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 894 | DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA | |
| 895 | UT_TESTF_SCAN_FDT); |
Simon Glass | d0b4f68 | 2018-11-18 08:14:30 -0700 | [diff] [blame] | 896 | |
| 897 | /* Test a few uclass phandle functions */ |
| 898 | static int dm_test_fdt_phandle(struct unit_test_state *uts) |
| 899 | { |
| 900 | struct udevice *back, *dev, *dev2; |
| 901 | |
| 902 | ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back)); |
Heinrich Schuchardt | 331caea | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 903 | ut_assertnonnull(back); |
Simon Glass | d0b4f68 | 2018-11-18 08:14:30 -0700 | [diff] [blame] | 904 | ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR, |
| 905 | back, "missing", &dev)); |
| 906 | ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back, |
| 907 | "power-supply", &dev)); |
Heinrich Schuchardt | 331caea | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 908 | ut_assertnonnull(dev); |
Simon Glass | d0b4f68 | 2018-11-18 08:14:30 -0700 | [diff] [blame] | 909 | ut_asserteq(0, device_active(dev)); |
| 910 | ut_asserteq_str("ldo1", dev->name); |
| 911 | ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back, |
| 912 | "power-supply", &dev2)); |
| 913 | ut_asserteq_ptr(dev, dev2); |
| 914 | |
| 915 | return 0; |
| 916 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 917 | 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] | 918 | |
| 919 | /* Test device_find_first_child_by_uclass() */ |
| 920 | static int dm_test_first_child(struct unit_test_state *uts) |
| 921 | { |
| 922 | struct udevice *i2c, *dev, *dev2; |
| 923 | |
| 924 | ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c)); |
| 925 | ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev)); |
| 926 | ut_asserteq_str("rtc@43", dev->name); |
| 927 | ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2)); |
| 928 | ut_asserteq_ptr(dev, dev2); |
| 929 | ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2)); |
| 930 | ut_asserteq_str("rtc@61", dev2->name); |
| 931 | |
| 932 | ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM, |
| 933 | &dev)); |
| 934 | ut_asserteq_str("eeprom@2c", dev->name); |
| 935 | ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2)); |
| 936 | ut_asserteq_ptr(dev, dev2); |
| 937 | |
| 938 | ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c, |
| 939 | UCLASS_VIDEO, &dev)); |
| 940 | ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev)); |
| 941 | |
| 942 | return 0; |
| 943 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 944 | 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] | 945 | |
| 946 | /* Test integer functions in dm_read_...() */ |
| 947 | static int dm_test_read_int(struct unit_test_state *uts) |
| 948 | { |
| 949 | struct udevice *dev; |
| 950 | u32 val32; |
| 951 | s32 sval; |
| 952 | uint val; |
Dario Binacchi | 70573c6 | 2020-03-29 18:04:40 +0200 | [diff] [blame] | 953 | u64 val64; |
Simon Glass | a1b17e4 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 954 | |
| 955 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); |
| 956 | ut_asserteq_str("a-test", dev->name); |
| 957 | ut_assertok(dev_read_u32(dev, "int-value", &val32)); |
| 958 | ut_asserteq(1234, val32); |
| 959 | |
| 960 | ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32)); |
| 961 | ut_asserteq(6, dev_read_u32_default(dev, "missing", 6)); |
| 962 | |
| 963 | ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6)); |
| 964 | ut_asserteq(1234, val32); |
| 965 | |
| 966 | ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval)); |
| 967 | ut_asserteq(6, dev_read_s32_default(dev, "missing", 6)); |
| 968 | |
| 969 | ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6)); |
| 970 | ut_assertok(dev_read_s32(dev, "uint-value", &sval)); |
| 971 | ut_asserteq(-1234, sval); |
| 972 | |
| 973 | val = 0; |
| 974 | ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val)); |
| 975 | ut_assertok(dev_read_u32u(dev, "uint-value", &val)); |
| 976 | ut_asserteq(-1234, val); |
| 977 | |
Dario Binacchi | 70573c6 | 2020-03-29 18:04:40 +0200 | [diff] [blame] | 978 | ut_assertok(dev_read_u64(dev, "int64-value", &val64)); |
| 979 | ut_asserteq_64(0x1111222233334444, val64); |
| 980 | |
| 981 | ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64)); |
| 982 | ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6)); |
| 983 | |
| 984 | ut_asserteq_64(0x1111222233334444, |
| 985 | dev_read_u64_default(dev, "int64-value", 6)); |
| 986 | |
Simon Glass | a1b17e4 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 987 | return 0; |
| 988 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 989 | 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] | 990 | |
Dario Binacchi | 4bb7075 | 2020-03-29 18:04:41 +0200 | [diff] [blame] | 991 | static int dm_test_read_int_index(struct unit_test_state *uts) |
| 992 | { |
| 993 | struct udevice *dev; |
| 994 | u32 val32; |
| 995 | |
| 996 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); |
| 997 | ut_asserteq_str("a-test", dev->name); |
| 998 | |
| 999 | ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32)); |
| 1000 | ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19)); |
| 1001 | |
| 1002 | ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32)); |
| 1003 | ut_asserteq(5678, val32); |
| 1004 | ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32)); |
| 1005 | ut_asserteq(9123, val32); |
| 1006 | ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32)); |
| 1007 | ut_asserteq(4567, val32); |
| 1008 | ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3, |
| 1009 | &val32)); |
| 1010 | |
| 1011 | ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2)); |
| 1012 | ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2)); |
| 1013 | ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2)); |
| 1014 | ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2)); |
| 1015 | |
| 1016 | return 0; |
| 1017 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1018 | 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] | 1019 | |
Patrick Delaunay | cc72f3e | 2020-09-25 09:41:16 +0200 | [diff] [blame] | 1020 | static int dm_test_read_phandle(struct unit_test_state *uts) |
| 1021 | { |
| 1022 | struct udevice *dev; |
| 1023 | struct ofnode_phandle_args args; |
| 1024 | int ret; |
| 1025 | const char prop[] = "test-gpios"; |
| 1026 | const char cell[] = "#gpio-cells"; |
| 1027 | const char prop2[] = "phandle-value"; |
| 1028 | |
| 1029 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); |
| 1030 | ut_asserteq_str("a-test", dev->name); |
| 1031 | |
| 1032 | /* Test dev_count_phandle_with_args with cell name */ |
| 1033 | ret = dev_count_phandle_with_args(dev, "missing", cell, 0); |
| 1034 | ut_asserteq(-ENOENT, ret); |
| 1035 | ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0); |
| 1036 | ut_asserteq(-EINVAL, ret); |
| 1037 | ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0)); |
| 1038 | |
| 1039 | /* Test dev_read_phandle_with_args with cell name */ |
| 1040 | ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args); |
| 1041 | ut_asserteq(-ENOENT, ret); |
| 1042 | ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args); |
| 1043 | ut_asserteq(-EINVAL, ret); |
| 1044 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args)); |
| 1045 | ut_asserteq(1, args.args_count); |
| 1046 | ut_asserteq(1, args.args[0]); |
| 1047 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args)); |
| 1048 | ut_asserteq(1, args.args_count); |
| 1049 | ut_asserteq(4, args.args[0]); |
| 1050 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args)); |
| 1051 | ut_asserteq(5, args.args_count); |
| 1052 | ut_asserteq(5, args.args[0]); |
| 1053 | ut_asserteq(1, args.args[4]); |
| 1054 | ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args); |
| 1055 | ut_asserteq(-ENOENT, ret); |
| 1056 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args)); |
| 1057 | ut_asserteq(1, args.args_count); |
| 1058 | ut_asserteq(12, args.args[0]); |
| 1059 | ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args); |
| 1060 | ut_asserteq(-ENOENT, ret); |
| 1061 | |
| 1062 | /* Test dev_count_phandle_with_args with cell count */ |
| 1063 | ret = dev_count_phandle_with_args(dev, "missing", NULL, 2); |
| 1064 | ut_asserteq(-ENOENT, ret); |
| 1065 | ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1)); |
| 1066 | |
| 1067 | /* Test dev_read_phandle_with_args with cell count */ |
| 1068 | ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args)); |
| 1069 | ut_asserteq(1, ofnode_valid(args.node)); |
| 1070 | ut_asserteq(1, args.args_count); |
| 1071 | ut_asserteq(10, args.args[0]); |
| 1072 | ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args); |
| 1073 | ut_asserteq(-EINVAL, ret); |
| 1074 | ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args)); |
| 1075 | ut_asserteq(1, ofnode_valid(args.node)); |
| 1076 | ut_asserteq(1, args.args_count); |
| 1077 | ut_asserteq(30, args.args[0]); |
| 1078 | ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args); |
| 1079 | ut_asserteq(-ENOENT, ret); |
| 1080 | |
| 1081 | return 0; |
| 1082 | } |
| 1083 | DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 1084 | |
Simon Glass | 5016234 | 2020-02-06 09:54:50 -0700 | [diff] [blame] | 1085 | /* Test iteration through devices by drvdata */ |
| 1086 | static int dm_test_uclass_drvdata(struct unit_test_state *uts) |
| 1087 | { |
| 1088 | struct udevice *dev; |
| 1089 | |
| 1090 | ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT, |
| 1091 | DM_TEST_TYPE_FIRST, &dev)); |
| 1092 | ut_asserteq_str("a-test", dev->name); |
| 1093 | |
| 1094 | ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT, |
| 1095 | DM_TEST_TYPE_SECOND, &dev)); |
| 1096 | ut_asserteq_str("d-test", dev->name); |
| 1097 | |
| 1098 | ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT, |
| 1099 | DM_TEST_TYPE_COUNT, |
| 1100 | &dev)); |
| 1101 | |
| 1102 | return 0; |
| 1103 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1104 | 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] | 1105 | |
Simon Glass | f262d4c | 2020-01-27 08:49:47 -0700 | [diff] [blame] | 1106 | /* Test device_first_child_ofdata_err(), etc. */ |
| 1107 | static int dm_test_child_ofdata(struct unit_test_state *uts) |
| 1108 | { |
| 1109 | struct udevice *bus, *dev; |
| 1110 | int count; |
| 1111 | |
| 1112 | ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus)); |
| 1113 | count = 0; |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1114 | device_foreach_child_of_to_plat(dev, bus) { |
Simon Glass | f262d4c | 2020-01-27 08:49:47 -0700 | [diff] [blame] | 1115 | ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID); |
| 1116 | ut_assert(!(dev->flags & DM_FLAG_ACTIVATED)); |
| 1117 | count++; |
| 1118 | } |
| 1119 | ut_asserteq(3, count); |
| 1120 | |
| 1121 | return 0; |
| 1122 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1123 | 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] | 1124 | |
| 1125 | /* Test device_first_child_err(), etc. */ |
| 1126 | static int dm_test_first_child_probe(struct unit_test_state *uts) |
| 1127 | { |
| 1128 | struct udevice *bus, *dev; |
| 1129 | int count; |
| 1130 | |
| 1131 | ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus)); |
| 1132 | count = 0; |
| 1133 | device_foreach_child_probe(dev, bus) { |
| 1134 | ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID); |
| 1135 | ut_assert(dev->flags & DM_FLAG_ACTIVATED); |
| 1136 | count++; |
| 1137 | } |
| 1138 | ut_asserteq(3, count); |
| 1139 | |
| 1140 | return 0; |
| 1141 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1142 | 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] | 1143 | |
| 1144 | /* Test that ofdata is read for parents before children */ |
| 1145 | static int dm_test_ofdata_order(struct unit_test_state *uts) |
| 1146 | { |
| 1147 | struct udevice *bus, *dev; |
| 1148 | |
| 1149 | ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus)); |
| 1150 | ut_assertnonnull(bus); |
| 1151 | ut_assert(!(bus->flags & DM_FLAG_PLATDATA_VALID)); |
| 1152 | |
| 1153 | ut_assertok(device_find_first_child(bus, &dev)); |
| 1154 | ut_assertnonnull(dev); |
| 1155 | ut_assert(!(dev->flags & DM_FLAG_PLATDATA_VALID)); |
| 1156 | |
| 1157 | /* read the child's ofdata which should cause the parent's to be read */ |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1158 | ut_assertok(device_of_to_plat(dev)); |
Simon Glass | b0dcc87 | 2020-04-05 15:38:19 -0600 | [diff] [blame] | 1159 | ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID); |
| 1160 | ut_assert(bus->flags & DM_FLAG_PLATDATA_VALID); |
| 1161 | |
| 1162 | ut_assert(!(dev->flags & DM_FLAG_ACTIVATED)); |
| 1163 | ut_assert(!(bus->flags & DM_FLAG_ACTIVATED)); |
| 1164 | |
| 1165 | return 0; |
| 1166 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1167 | DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |