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> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 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/of_access.h> |
Patrick Delaunay | 269fa84 | 2021-05-21 12:25:00 +0200 | [diff] [blame] | 21 | #include <linux/ioport.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 | |
Michael Walle | 82a3c9e | 2021-02-25 16:51:11 +0100 | [diff] [blame] | 185 | ret = dev_read_alias_highest_id("ethernet"); |
Sean Anderson | bedb182 | 2022-05-05 13:11:30 -0400 | [diff] [blame] | 186 | ut_asserteq(8, ret); |
Michal Simek | a93eb57 | 2019-01-31 16:31:00 +0100 | [diff] [blame] | 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 | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 332 | ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test), |
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 | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 352 | ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test), |
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 | 074197a | 2021-02-03 06:01:09 -0700 | [diff] [blame] | 356 | ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test), |
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 | /** |
Michal Suchanek | dfecd63 | 2022-10-12 21:57:50 +0200 | [diff] [blame] | 395 | * Test various error conditions with uclass_first_device(), |
| 396 | * uclass_next_device(), and uclass_probe_all() |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 397 | */ |
Michal Suchanek | dfecd63 | 2022-10-12 21:57:50 +0200 | [diff] [blame] | 398 | static int dm_test_first_next_device_probeall(struct unit_test_state *uts) |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 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 */ |
Michal Suchanek | f219547 | 2022-10-12 21:58:09 +0200 | [diff] [blame] | 406 | for (uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0; |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 407 | dev; |
Michal Suchanek | f219547 | 2022-10-12 21:58:09 +0200 | [diff] [blame] | 408 | uclass_next_device(&dev)) { |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 409 | count++; |
| 410 | parent = dev_get_parent(dev); |
| 411 | } |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 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); |
Michal Suchanek | f219547 | 2022-10-12 21:58:09 +0200 | [diff] [blame] | 419 | for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev), |
| 420 | count = 0; |
| 421 | dev; |
| 422 | ret = uclass_next_device_check(&dev)) { |
| 423 | if (!ret) |
| 424 | count++; |
| 425 | else |
| 426 | ut_asserteq(-ENOMEM, ret); |
| 427 | parent = dev_get_parent(dev); |
| 428 | } |
| 429 | ut_asserteq(3, count); |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 430 | |
| 431 | /* Now an error on the first one */ |
| 432 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev)); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 433 | pdata = dev_get_plat(dev); |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 434 | pdata->probe_err = -ENOENT; |
| 435 | device_remove(parent, DM_REMOVE_NORMAL); |
Michal Suchanek | f219547 | 2022-10-12 21:58:09 +0200 | [diff] [blame] | 436 | for (uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0; |
| 437 | dev; |
| 438 | uclass_next_device(&dev)) { |
| 439 | count++; |
| 440 | parent = dev_get_parent(dev); |
| 441 | } |
| 442 | ut_asserteq(2, count); |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 443 | |
Michal Suchanek | dfecd63 | 2022-10-12 21:57:50 +0200 | [diff] [blame] | 444 | /* Now that broken devices are set up test probe_all */ |
| 445 | device_remove(parent, DM_REMOVE_NORMAL); |
| 446 | /* There are broken devices so an error should be returned */ |
| 447 | ut_assert(uclass_probe_all(UCLASS_TEST_PROBE) < 0); |
| 448 | /* but non-error device should be probed nonetheless */ |
| 449 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 2, &dev)); |
| 450 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); |
| 451 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 3, &dev)); |
| 452 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); |
| 453 | |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame] | 454 | return 0; |
| 455 | } |
Michal Suchanek | dfecd63 | 2022-10-12 21:57:50 +0200 | [diff] [blame] | 456 | DM_TEST(dm_test_first_next_device_probeall, |
| 457 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 95ce385 | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 458 | |
Simon Glass | 3cf0fba | 2020-02-06 09:54:49 -0700 | [diff] [blame] | 459 | /* Test iteration through devices in a uclass */ |
| 460 | static int dm_test_uclass_foreach(struct unit_test_state *uts) |
| 461 | { |
| 462 | struct udevice *dev; |
| 463 | struct uclass *uc; |
| 464 | int count; |
| 465 | |
| 466 | count = 0; |
| 467 | uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc) |
| 468 | count++; |
Jean-Jacques Hiblot | 88e6a60 | 2020-09-11 13:43:35 +0530 | [diff] [blame] | 469 | ut_asserteq(9, count); |
Simon Glass | 3cf0fba | 2020-02-06 09:54:49 -0700 | [diff] [blame] | 470 | |
| 471 | count = 0; |
| 472 | uclass_foreach_dev(dev, uc) |
| 473 | count++; |
Jean-Jacques Hiblot | 88e6a60 | 2020-09-11 13:43:35 +0530 | [diff] [blame] | 474 | ut_asserteq(9, count); |
Simon Glass | 3cf0fba | 2020-02-06 09:54:49 -0700 | [diff] [blame] | 475 | |
| 476 | return 0; |
| 477 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 478 | 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] | 479 | |
Simon Glass | 95ce385 | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 480 | /** |
| 481 | * check_devices() - Check return values and pointers |
| 482 | * |
| 483 | * This runs through a full sequence of uclass_first_device_check()... |
| 484 | * uclass_next_device_check() checking that the return values and devices |
| 485 | * are correct. |
| 486 | * |
| 487 | * @uts: Test state |
| 488 | * @devlist: List of expected devices |
| 489 | * @mask: Indicates which devices should return an error. Device n should |
| 490 | * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if |
| 491 | * bit n is clear. |
| 492 | */ |
| 493 | static int check_devices(struct unit_test_state *uts, |
| 494 | struct udevice *devlist[], int mask) |
| 495 | { |
| 496 | int expected_ret; |
| 497 | struct udevice *dev; |
| 498 | int i; |
| 499 | |
| 500 | expected_ret = (mask & 1) ? -ENOENT : 0; |
| 501 | mask >>= 1; |
| 502 | ut_asserteq(expected_ret, |
| 503 | uclass_first_device_check(UCLASS_TEST_PROBE, &dev)); |
| 504 | for (i = 0; i < 4; i++) { |
| 505 | ut_asserteq_ptr(devlist[i], dev); |
| 506 | expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0; |
| 507 | mask >>= 1; |
| 508 | ut_asserteq(expected_ret, uclass_next_device_check(&dev)); |
| 509 | } |
| 510 | ut_asserteq_ptr(NULL, dev); |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | /* Test uclass_first_device_check() and uclass_next_device_check() */ |
| 516 | static int dm_test_first_next_ok_device(struct unit_test_state *uts) |
| 517 | { |
| 518 | struct dm_testprobe_pdata *pdata; |
| 519 | struct udevice *dev, *parent = NULL, *devlist[4]; |
| 520 | int count; |
| 521 | int ret; |
| 522 | |
| 523 | /* There should be 4 devices */ |
| 524 | count = 0; |
| 525 | for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev); |
| 526 | dev; |
| 527 | ret = uclass_next_device_check(&dev)) { |
| 528 | ut_assertok(ret); |
| 529 | devlist[count++] = dev; |
| 530 | parent = dev_get_parent(dev); |
| 531 | } |
| 532 | ut_asserteq(4, count); |
| 533 | ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev)); |
| 534 | ut_assertok(check_devices(uts, devlist, 0)); |
| 535 | |
| 536 | /* Remove them and try again, with an error on the second one */ |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 537 | pdata = dev_get_plat(devlist[1]); |
Simon Glass | 95ce385 | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 538 | pdata->probe_err = -ENOENT - 1; |
| 539 | device_remove(parent, DM_REMOVE_NORMAL); |
| 540 | ut_assertok(check_devices(uts, devlist, 1 << 1)); |
| 541 | |
| 542 | /* Now an error on the first one */ |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 543 | pdata = dev_get_plat(devlist[0]); |
Simon Glass | 95ce385 | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 544 | pdata->probe_err = -ENOENT - 0; |
| 545 | device_remove(parent, DM_REMOVE_NORMAL); |
| 546 | ut_assertok(check_devices(uts, devlist, 3 << 0)); |
| 547 | |
| 548 | /* Now errors on all */ |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 549 | pdata = dev_get_plat(devlist[2]); |
Simon Glass | 95ce385 | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 550 | pdata->probe_err = -ENOENT - 2; |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 551 | pdata = dev_get_plat(devlist[3]); |
Simon Glass | 95ce385 | 2017-04-23 20:10:45 -0600 | [diff] [blame] | 552 | pdata->probe_err = -ENOENT - 3; |
| 553 | device_remove(parent, DM_REMOVE_NORMAL); |
| 554 | ut_assertok(check_devices(uts, devlist, 0xf << 0)); |
| 555 | |
| 556 | return 0; |
| 557 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 558 | 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] | 559 | |
| 560 | static const struct udevice_id fdt_dummy_ids[] = { |
| 561 | { .compatible = "denx,u-boot-fdt-dummy", }, |
| 562 | { } |
| 563 | }; |
| 564 | |
| 565 | UCLASS_DRIVER(fdt_dummy) = { |
Eugeniu Rosca | 507cef3 | 2018-05-19 14:13:55 +0200 | [diff] [blame] | 566 | .name = "fdt-dummy", |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 567 | .id = UCLASS_TEST_DUMMY, |
| 568 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 569 | }; |
| 570 | |
| 571 | U_BOOT_DRIVER(fdt_dummy_drv) = { |
| 572 | .name = "fdt_dummy_drv", |
| 573 | .of_match = fdt_dummy_ids, |
| 574 | .id = UCLASS_TEST_DUMMY, |
| 575 | }; |
| 576 | |
| 577 | static int dm_test_fdt_translation(struct unit_test_state *uts) |
| 578 | { |
| 579 | struct udevice *dev; |
Fabien Dessenne | 641067f | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 580 | fdt32_t dma_addr[2]; |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 581 | |
| 582 | /* Some simple translations */ |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 583 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 584 | ut_asserteq_str("dev@0,0", dev->name); |
| 585 | ut_asserteq(0x8000, dev_read_addr(dev)); |
| 586 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 587 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev)); |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 588 | ut_asserteq_str("dev@1,100", dev->name); |
| 589 | ut_asserteq(0x9000, dev_read_addr(dev)); |
| 590 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 591 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, &dev)); |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 592 | ut_asserteq_str("dev@2,200", dev->name); |
| 593 | ut_asserteq(0xA000, dev_read_addr(dev)); |
| 594 | |
Yuepeng Xing | 7943ae2 | 2022-12-02 14:23:07 +0800 | [diff] [blame] | 595 | /* No translation for buses with #size-cells == 0 */ |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 596 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, &dev)); |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 597 | ut_asserteq_str("dev@42", dev->name); |
| 598 | ut_asserteq(0x42, dev_read_addr(dev)); |
| 599 | |
Fabien Dessenne | 641067f | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 600 | /* dma address translation */ |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 601 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Fabien Dessenne | 641067f | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 602 | dma_addr[0] = cpu_to_be32(0); |
| 603 | dma_addr[1] = cpu_to_be32(0); |
| 604 | ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr)); |
| 605 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 606 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev)); |
Fabien Dessenne | 641067f | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 607 | dma_addr[0] = cpu_to_be32(1); |
| 608 | dma_addr[1] = cpu_to_be32(0x100); |
| 609 | ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr)); |
| 610 | |
Mario Six | e8d5291 | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 611 | return 0; |
| 612 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 613 | 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] | 614 | |
Ovidiu Panait | 6e64830 | 2020-08-03 22:17:36 +0300 | [diff] [blame] | 615 | static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts) |
| 616 | { |
| 617 | struct udevice *gpio, *dev; |
| 618 | void *ptr; |
| 619 | |
| 620 | /* Test for missing reg property */ |
| 621 | ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio)); |
| 622 | ut_assertnull(devfdt_get_addr_ptr(gpio)); |
| 623 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 624 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Ovidiu Panait | 6e64830 | 2020-08-03 22:17:36 +0300 | [diff] [blame] | 625 | ptr = devfdt_get_addr_ptr(dev); |
| 626 | ut_asserteq_ptr((void *)0x8000, ptr); |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | DM_TEST(dm_test_fdt_get_addr_ptr_flat, |
| 631 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
| 632 | |
Álvaro Fernández Rojas | 30a90f5 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 633 | static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts) |
| 634 | { |
| 635 | struct udevice *dev; |
| 636 | fdt_addr_t addr; |
| 637 | void *paddr; |
| 638 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 639 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
Álvaro Fernández Rojas | 30a90f5 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 640 | |
| 641 | addr = devfdt_get_addr(dev); |
| 642 | ut_asserteq(0x8000, addr); |
| 643 | |
| 644 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 645 | ut_assertnonnull(paddr); |
| 646 | ut_asserteq_ptr(paddr, devfdt_remap_addr(dev)); |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | DM_TEST(dm_test_fdt_remap_addr_flat, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 651 | 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] | 652 | |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 653 | static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts) |
| 654 | { |
| 655 | struct udevice *dev; |
| 656 | fdt_addr_t addr; |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 657 | fdt_size_t size; |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 658 | void *paddr; |
| 659 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 660 | 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] | 661 | |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 662 | addr = devfdt_get_addr_size_index(dev, 0, &size); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 663 | ut_asserteq(0x8000, addr); |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 664 | ut_asserteq(0x1000, size); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 665 | |
| 666 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 667 | ut_assertnonnull(paddr); |
| 668 | ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0)); |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | DM_TEST(dm_test_fdt_remap_addr_index_flat, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 673 | 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] | 674 | |
| 675 | static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts) |
| 676 | { |
| 677 | struct udevice *dev; |
| 678 | fdt_addr_t addr; |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 679 | fdt_size_t size; |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 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 | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 683 | |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 684 | 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] | 685 | ut_asserteq(0x8000, addr); |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 686 | ut_asserteq(0x1000, size); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 687 | |
| 688 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 689 | ut_assertnonnull(paddr); |
| 690 | ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0")); |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | DM_TEST(dm_test_fdt_remap_addr_name_flat, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 695 | 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] | 696 | |
Álvaro Fernández Rojas | 30a90f5 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 697 | static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts) |
| 698 | { |
| 699 | struct udevice *dev; |
| 700 | fdt_addr_t addr; |
| 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 | 30a90f5 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 704 | |
| 705 | addr = dev_read_addr(dev); |
| 706 | ut_asserteq(0x8000, addr); |
| 707 | |
| 708 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 709 | ut_assertnonnull(paddr); |
| 710 | ut_asserteq_ptr(paddr, dev_remap_addr(dev)); |
| 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | DM_TEST(dm_test_fdt_remap_addr_live, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 715 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Mario Six | 2ea4d0d | 2018-06-26 08:46:49 +0200 | [diff] [blame] | 716 | |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 717 | static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts) |
| 718 | { |
| 719 | struct udevice *dev; |
| 720 | fdt_addr_t addr; |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 721 | fdt_size_t size; |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 722 | void *paddr; |
| 723 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 724 | 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] | 725 | |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 726 | addr = dev_read_addr_size_index(dev, 0, &size); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 727 | ut_asserteq(0x8000, addr); |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 728 | ut_asserteq(0x1000, size); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 729 | |
| 730 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 731 | ut_assertnonnull(paddr); |
| 732 | ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0)); |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | DM_TEST(dm_test_fdt_remap_addr_index_live, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 737 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 738 | |
| 739 | static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts) |
| 740 | { |
| 741 | struct udevice *dev; |
| 742 | fdt_addr_t addr; |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 743 | fdt_size_t size; |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 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 | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 747 | |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 748 | 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] | 749 | ut_asserteq(0x8000, addr); |
Sekhar Nori | f5b9047 | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 750 | ut_asserteq(0x1000, size); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 751 | |
| 752 | paddr = map_physmem(addr, 0, MAP_NOCACHE); |
| 753 | ut_assertnonnull(paddr); |
| 754 | ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0")); |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | DM_TEST(dm_test_fdt_remap_addr_name_live, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 759 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Álvaro Fernández Rojas | 7959882 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 760 | |
Mario Six | 172942a | 2018-06-26 08:46:51 +0200 | [diff] [blame] | 761 | static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts) |
| 762 | { |
| 763 | ofnode node; |
| 764 | |
| 765 | if (!of_live_active()) { |
| 766 | printf("Live tree not active; ignore test\n"); |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | node = ofnode_path("/usb@2"); |
| 771 | |
| 772 | /* Test enabling devices */ |
| 773 | |
| 774 | ut_assert(!of_device_is_available(ofnode_to_np(node))); |
| 775 | dev_enable_by_path("/usb@2"); |
| 776 | ut_assert(of_device_is_available(ofnode_to_np(node))); |
| 777 | |
| 778 | /* Test disabling devices */ |
| 779 | |
| 780 | ut_assert(of_device_is_available(ofnode_to_np(node))); |
| 781 | dev_disable_by_path("/usb@2"); |
| 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_disable_enable_by_path, UT_TESTF_SCAN_PDATA | |
| 787 | UT_TESTF_SCAN_FDT); |
Simon Glass | d0b4f68 | 2018-11-18 08:14:30 -0700 | [diff] [blame] | 788 | |
| 789 | /* Test a few uclass phandle functions */ |
| 790 | static int dm_test_fdt_phandle(struct unit_test_state *uts) |
| 791 | { |
| 792 | struct udevice *back, *dev, *dev2; |
| 793 | |
| 794 | ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back)); |
Heinrich Schuchardt | 331caea | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 795 | ut_assertnonnull(back); |
Simon Glass | d0b4f68 | 2018-11-18 08:14:30 -0700 | [diff] [blame] | 796 | ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR, |
| 797 | back, "missing", &dev)); |
| 798 | ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back, |
| 799 | "power-supply", &dev)); |
Heinrich Schuchardt | 331caea | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 800 | ut_assertnonnull(dev); |
Simon Glass | d0b4f68 | 2018-11-18 08:14:30 -0700 | [diff] [blame] | 801 | ut_asserteq(0, device_active(dev)); |
| 802 | ut_asserteq_str("ldo1", dev->name); |
| 803 | ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back, |
| 804 | "power-supply", &dev2)); |
| 805 | ut_asserteq_ptr(dev, dev2); |
| 806 | |
| 807 | return 0; |
| 808 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 809 | 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] | 810 | |
| 811 | /* Test device_find_first_child_by_uclass() */ |
| 812 | static int dm_test_first_child(struct unit_test_state *uts) |
| 813 | { |
| 814 | struct udevice *i2c, *dev, *dev2; |
| 815 | |
| 816 | ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c)); |
| 817 | ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev)); |
| 818 | ut_asserteq_str("rtc@43", dev->name); |
| 819 | ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2)); |
| 820 | ut_asserteq_ptr(dev, dev2); |
| 821 | ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2)); |
| 822 | ut_asserteq_str("rtc@61", dev2->name); |
| 823 | |
| 824 | ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM, |
| 825 | &dev)); |
| 826 | ut_asserteq_str("eeprom@2c", dev->name); |
| 827 | ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2)); |
| 828 | ut_asserteq_ptr(dev, dev2); |
| 829 | |
| 830 | ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c, |
| 831 | UCLASS_VIDEO, &dev)); |
| 832 | ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev)); |
| 833 | |
| 834 | return 0; |
| 835 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 836 | 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] | 837 | |
| 838 | /* Test integer functions in dm_read_...() */ |
| 839 | static int dm_test_read_int(struct unit_test_state *uts) |
| 840 | { |
| 841 | struct udevice *dev; |
Stefan Herbrechtsmeier | b471bdc | 2022-06-14 15:21:30 +0200 | [diff] [blame] | 842 | u8 val8; |
| 843 | u16 val16; |
Simon Glass | a1b17e4 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 844 | u32 val32; |
| 845 | s32 sval; |
| 846 | uint val; |
Dario Binacchi | 70573c6 | 2020-03-29 18:04:40 +0200 | [diff] [blame] | 847 | u64 val64; |
Simon Glass | a1b17e4 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 848 | |
| 849 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); |
| 850 | ut_asserteq_str("a-test", dev->name); |
Stefan Herbrechtsmeier | b471bdc | 2022-06-14 15:21:30 +0200 | [diff] [blame] | 851 | |
| 852 | ut_assertok(dev_read_u8(dev, "int8-value", &val8)); |
| 853 | ut_asserteq(0x12, val8); |
| 854 | |
| 855 | ut_asserteq(-EINVAL, dev_read_u8(dev, "missing", &val8)); |
| 856 | ut_asserteq(6, dev_read_u8_default(dev, "missing", 6)); |
| 857 | |
| 858 | ut_asserteq(0x12, dev_read_u8_default(dev, "int8-value", 6)); |
| 859 | |
| 860 | ut_assertok(dev_read_u16(dev, "int16-value", &val16)); |
| 861 | ut_asserteq(0x1234, val16); |
| 862 | |
| 863 | ut_asserteq(-EINVAL, dev_read_u16(dev, "missing", &val16)); |
| 864 | ut_asserteq(6, dev_read_u16_default(dev, "missing", 6)); |
| 865 | |
| 866 | ut_asserteq(0x1234, dev_read_u16_default(dev, "int16-value", 6)); |
| 867 | |
Simon Glass | a1b17e4 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 868 | ut_assertok(dev_read_u32(dev, "int-value", &val32)); |
| 869 | ut_asserteq(1234, val32); |
| 870 | |
| 871 | ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32)); |
| 872 | ut_asserteq(6, dev_read_u32_default(dev, "missing", 6)); |
| 873 | |
| 874 | ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6)); |
| 875 | ut_asserteq(1234, val32); |
| 876 | |
| 877 | ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval)); |
| 878 | ut_asserteq(6, dev_read_s32_default(dev, "missing", 6)); |
| 879 | |
| 880 | ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6)); |
| 881 | ut_assertok(dev_read_s32(dev, "uint-value", &sval)); |
| 882 | ut_asserteq(-1234, sval); |
| 883 | |
| 884 | val = 0; |
| 885 | ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val)); |
| 886 | ut_assertok(dev_read_u32u(dev, "uint-value", &val)); |
| 887 | ut_asserteq(-1234, val); |
| 888 | |
Dario Binacchi | 70573c6 | 2020-03-29 18:04:40 +0200 | [diff] [blame] | 889 | ut_assertok(dev_read_u64(dev, "int64-value", &val64)); |
| 890 | ut_asserteq_64(0x1111222233334444, val64); |
| 891 | |
| 892 | ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64)); |
| 893 | ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6)); |
| 894 | |
| 895 | ut_asserteq_64(0x1111222233334444, |
| 896 | dev_read_u64_default(dev, "int64-value", 6)); |
| 897 | |
Simon Glass | a1b17e4 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 898 | return 0; |
| 899 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 900 | 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] | 901 | |
Dario Binacchi | 4bb7075 | 2020-03-29 18:04:41 +0200 | [diff] [blame] | 902 | static int dm_test_read_int_index(struct unit_test_state *uts) |
| 903 | { |
| 904 | struct udevice *dev; |
| 905 | u32 val32; |
| 906 | |
| 907 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); |
| 908 | ut_asserteq_str("a-test", dev->name); |
| 909 | |
| 910 | ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32)); |
| 911 | ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19)); |
| 912 | |
| 913 | ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32)); |
| 914 | ut_asserteq(5678, val32); |
| 915 | ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32)); |
| 916 | ut_asserteq(9123, val32); |
| 917 | ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32)); |
| 918 | ut_asserteq(4567, val32); |
| 919 | ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3, |
| 920 | &val32)); |
| 921 | |
| 922 | ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2)); |
| 923 | ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2)); |
| 924 | ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2)); |
| 925 | ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2)); |
| 926 | |
| 927 | return 0; |
| 928 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 929 | 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] | 930 | |
Patrick Delaunay | cc72f3e | 2020-09-25 09:41:16 +0200 | [diff] [blame] | 931 | static int dm_test_read_phandle(struct unit_test_state *uts) |
| 932 | { |
| 933 | struct udevice *dev; |
| 934 | struct ofnode_phandle_args args; |
| 935 | int ret; |
| 936 | const char prop[] = "test-gpios"; |
| 937 | const char cell[] = "#gpio-cells"; |
| 938 | const char prop2[] = "phandle-value"; |
| 939 | |
| 940 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); |
| 941 | ut_asserteq_str("a-test", dev->name); |
| 942 | |
| 943 | /* Test dev_count_phandle_with_args with cell name */ |
| 944 | ret = dev_count_phandle_with_args(dev, "missing", cell, 0); |
| 945 | ut_asserteq(-ENOENT, ret); |
| 946 | ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0); |
| 947 | ut_asserteq(-EINVAL, ret); |
| 948 | ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0)); |
| 949 | |
| 950 | /* Test dev_read_phandle_with_args with cell name */ |
| 951 | ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args); |
| 952 | ut_asserteq(-ENOENT, ret); |
| 953 | ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args); |
| 954 | ut_asserteq(-EINVAL, ret); |
| 955 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args)); |
| 956 | ut_asserteq(1, args.args_count); |
| 957 | ut_asserteq(1, args.args[0]); |
| 958 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args)); |
| 959 | ut_asserteq(1, args.args_count); |
| 960 | ut_asserteq(4, args.args[0]); |
| 961 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args)); |
| 962 | ut_asserteq(5, args.args_count); |
| 963 | ut_asserteq(5, args.args[0]); |
| 964 | ut_asserteq(1, args.args[4]); |
| 965 | ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args); |
| 966 | ut_asserteq(-ENOENT, ret); |
| 967 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args)); |
| 968 | ut_asserteq(1, args.args_count); |
| 969 | ut_asserteq(12, args.args[0]); |
| 970 | ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args); |
| 971 | ut_asserteq(-ENOENT, ret); |
| 972 | |
| 973 | /* Test dev_count_phandle_with_args with cell count */ |
| 974 | ret = dev_count_phandle_with_args(dev, "missing", NULL, 2); |
| 975 | ut_asserteq(-ENOENT, ret); |
| 976 | ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1)); |
| 977 | |
| 978 | /* Test dev_read_phandle_with_args with cell count */ |
| 979 | ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args)); |
| 980 | ut_asserteq(1, ofnode_valid(args.node)); |
| 981 | ut_asserteq(1, args.args_count); |
| 982 | ut_asserteq(10, args.args[0]); |
| 983 | ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args); |
| 984 | ut_asserteq(-EINVAL, ret); |
| 985 | ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args)); |
| 986 | ut_asserteq(1, ofnode_valid(args.node)); |
| 987 | ut_asserteq(1, args.args_count); |
| 988 | ut_asserteq(30, args.args[0]); |
| 989 | ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args); |
| 990 | ut_asserteq(-ENOENT, ret); |
| 991 | |
| 992 | return 0; |
| 993 | } |
| 994 | DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 995 | |
Simon Glass | 5016234 | 2020-02-06 09:54:50 -0700 | [diff] [blame] | 996 | /* Test iteration through devices by drvdata */ |
| 997 | static int dm_test_uclass_drvdata(struct unit_test_state *uts) |
| 998 | { |
| 999 | struct udevice *dev; |
| 1000 | |
| 1001 | ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT, |
| 1002 | DM_TEST_TYPE_FIRST, &dev)); |
| 1003 | ut_asserteq_str("a-test", dev->name); |
| 1004 | |
| 1005 | ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT, |
| 1006 | DM_TEST_TYPE_SECOND, &dev)); |
| 1007 | ut_asserteq_str("d-test", dev->name); |
| 1008 | |
| 1009 | ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT, |
| 1010 | DM_TEST_TYPE_COUNT, |
| 1011 | &dev)); |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1015 | 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] | 1016 | |
Simon Glass | f262d4c | 2020-01-27 08:49:47 -0700 | [diff] [blame] | 1017 | /* Test device_first_child_ofdata_err(), etc. */ |
| 1018 | static int dm_test_child_ofdata(struct unit_test_state *uts) |
| 1019 | { |
| 1020 | struct udevice *bus, *dev; |
| 1021 | int count; |
| 1022 | |
| 1023 | ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus)); |
| 1024 | count = 0; |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1025 | device_foreach_child_of_to_plat(dev, bus) { |
Simon Glass | 73466df | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 1026 | ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID); |
| 1027 | ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED)); |
Simon Glass | f262d4c | 2020-01-27 08:49:47 -0700 | [diff] [blame] | 1028 | count++; |
| 1029 | } |
| 1030 | ut_asserteq(3, count); |
| 1031 | |
| 1032 | return 0; |
| 1033 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1034 | 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] | 1035 | |
| 1036 | /* Test device_first_child_err(), etc. */ |
| 1037 | static int dm_test_first_child_probe(struct unit_test_state *uts) |
| 1038 | { |
| 1039 | struct udevice *bus, *dev; |
| 1040 | int count; |
| 1041 | |
| 1042 | ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus)); |
| 1043 | count = 0; |
| 1044 | device_foreach_child_probe(dev, bus) { |
Simon Glass | 73466df | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 1045 | ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID); |
| 1046 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); |
Simon Glass | 903e83e | 2020-01-27 08:49:48 -0700 | [diff] [blame] | 1047 | count++; |
| 1048 | } |
| 1049 | ut_asserteq(3, count); |
| 1050 | |
| 1051 | return 0; |
| 1052 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1053 | 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] | 1054 | |
| 1055 | /* Test that ofdata is read for parents before children */ |
| 1056 | static int dm_test_ofdata_order(struct unit_test_state *uts) |
| 1057 | { |
| 1058 | struct udevice *bus, *dev; |
| 1059 | |
| 1060 | ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus)); |
| 1061 | ut_assertnonnull(bus); |
Simon Glass | 73466df | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 1062 | ut_assert(!(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID)); |
Simon Glass | b0dcc87 | 2020-04-05 15:38:19 -0600 | [diff] [blame] | 1063 | |
| 1064 | ut_assertok(device_find_first_child(bus, &dev)); |
| 1065 | ut_assertnonnull(dev); |
Simon Glass | 73466df | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 1066 | ut_assert(!(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)); |
Simon Glass | b0dcc87 | 2020-04-05 15:38:19 -0600 | [diff] [blame] | 1067 | |
| 1068 | /* 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] | 1069 | ut_assertok(device_of_to_plat(dev)); |
Simon Glass | 73466df | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 1070 | ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID); |
| 1071 | ut_assert(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID); |
Simon Glass | b0dcc87 | 2020-04-05 15:38:19 -0600 | [diff] [blame] | 1072 | |
Simon Glass | 73466df | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 1073 | ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED)); |
| 1074 | ut_assert(!(dev_get_flags(bus) & DM_FLAG_ACTIVATED)); |
Simon Glass | b0dcc87 | 2020-04-05 15:38:19 -0600 | [diff] [blame] | 1075 | |
| 1076 | return 0; |
| 1077 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1078 | 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] | 1079 | |
| 1080 | /* Test dev_decode_display_timing() */ |
| 1081 | static int dm_test_decode_display_timing(struct unit_test_state *uts) |
| 1082 | { |
| 1083 | struct udevice *dev; |
| 1084 | struct display_timing timing; |
| 1085 | |
| 1086 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); |
| 1087 | ut_asserteq_str("a-test", dev->name); |
| 1088 | |
| 1089 | ut_assertok(dev_decode_display_timing(dev, 0, &timing)); |
| 1090 | ut_assert(timing.hactive.typ == 240); |
| 1091 | ut_assert(timing.hback_porch.typ == 7); |
| 1092 | ut_assert(timing.hfront_porch.typ == 6); |
| 1093 | ut_assert(timing.hsync_len.typ == 1); |
| 1094 | ut_assert(timing.vactive.typ == 320); |
| 1095 | ut_assert(timing.vback_porch.typ == 5); |
| 1096 | ut_assert(timing.vfront_porch.typ == 8); |
| 1097 | ut_assert(timing.vsync_len.typ == 2); |
| 1098 | ut_assert(timing.pixelclock.typ == 6500000); |
| 1099 | ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH); |
| 1100 | ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW)); |
| 1101 | ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH)); |
| 1102 | ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_LOW); |
| 1103 | ut_assert(timing.flags & DISPLAY_FLAGS_DE_HIGH); |
| 1104 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW)); |
| 1105 | ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE); |
| 1106 | ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)); |
| 1107 | ut_assert(timing.flags & DISPLAY_FLAGS_INTERLACED); |
| 1108 | ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLESCAN); |
| 1109 | ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLECLK); |
| 1110 | |
| 1111 | ut_assertok(dev_decode_display_timing(dev, 1, &timing)); |
| 1112 | ut_assert(timing.hactive.typ == 480); |
| 1113 | ut_assert(timing.hback_porch.typ == 59); |
| 1114 | ut_assert(timing.hfront_porch.typ == 10); |
| 1115 | ut_assert(timing.hsync_len.typ == 12); |
| 1116 | ut_assert(timing.vactive.typ == 800); |
| 1117 | ut_assert(timing.vback_porch.typ == 15); |
| 1118 | ut_assert(timing.vfront_porch.typ == 17); |
| 1119 | ut_assert(timing.vsync_len.typ == 16); |
| 1120 | ut_assert(timing.pixelclock.typ == 9000000); |
| 1121 | ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH)); |
| 1122 | ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_LOW); |
| 1123 | ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH); |
| 1124 | ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW)); |
| 1125 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH)); |
| 1126 | ut_assert(timing.flags & DISPLAY_FLAGS_DE_LOW); |
| 1127 | ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)); |
| 1128 | ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE); |
| 1129 | ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED)); |
| 1130 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN)); |
| 1131 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK)); |
| 1132 | |
| 1133 | ut_assertok(dev_decode_display_timing(dev, 2, &timing)); |
| 1134 | ut_assert(timing.hactive.typ == 800); |
| 1135 | ut_assert(timing.hback_porch.typ == 89); |
| 1136 | ut_assert(timing.hfront_porch.typ == 164); |
| 1137 | ut_assert(timing.hsync_len.typ == 11); |
| 1138 | ut_assert(timing.vactive.typ == 480); |
| 1139 | ut_assert(timing.vback_porch.typ == 23); |
| 1140 | ut_assert(timing.vfront_porch.typ == 10); |
| 1141 | ut_assert(timing.vsync_len.typ == 13); |
| 1142 | ut_assert(timing.pixelclock.typ == 33500000); |
| 1143 | ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH)); |
| 1144 | ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW)); |
| 1145 | ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH)); |
| 1146 | ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW)); |
| 1147 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH)); |
| 1148 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW)); |
| 1149 | ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)); |
| 1150 | ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)); |
| 1151 | ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED)); |
| 1152 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN)); |
| 1153 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK)); |
| 1154 | |
| 1155 | ut_assert(dev_decode_display_timing(dev, 3, &timing)); |
| 1156 | return 0; |
| 1157 | } |
| 1158 | DM_TEST(dm_test_decode_display_timing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Patrick Delaunay | 269fa84 | 2021-05-21 12:25:00 +0200 | [diff] [blame] | 1159 | |
| 1160 | /* Test read_resourcee() */ |
| 1161 | static int dm_test_read_resource(struct unit_test_state *uts) |
| 1162 | { |
| 1163 | struct udevice *dev; |
| 1164 | struct resource res; |
| 1165 | |
| 1166 | /* test resource without translation */ |
| 1167 | ut_assertok(uclass_find_device_by_name(UCLASS_SIMPLE_BUS, "syscon@2", &dev)); |
| 1168 | ut_assertok(dev_read_resource(dev, 0, &res)); |
| 1169 | ut_asserteq(0x40, res.start); |
| 1170 | ut_asserteq(0x44, res.end); |
| 1171 | ut_assertok(dev_read_resource(dev, 1, &res)); |
| 1172 | ut_asserteq(0x48, res.start); |
| 1173 | ut_asserteq(0x4d, res.end); |
| 1174 | |
| 1175 | /* test resource with translation */ |
| 1176 | ut_assertok(uclass_find_device_by_name(UCLASS_TEST_DUMMY, "dev@1,100", &dev)); |
| 1177 | ut_assertok(dev_read_resource(dev, 0, &res)); |
| 1178 | ut_asserteq(0x9000, res.start); |
| 1179 | ut_asserteq(0x9fff, res.end); |
| 1180 | |
| 1181 | /* test named resource */ |
| 1182 | ut_assertok(uclass_find_device_by_name(UCLASS_TEST_DUMMY, "dev@0,0", &dev)); |
| 1183 | ut_assertok(dev_read_resource_byname(dev, "sandbox-dummy-0", &res)); |
| 1184 | ut_asserteq(0x8000, res.start); |
| 1185 | ut_asserteq(0x8fff, res.end); |
| 1186 | |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | DM_TEST(dm_test_read_resource, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |