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