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