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