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