Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Google, Inc |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <malloc.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <dm/test.h> |
| 14 | #include <dm/root.h> |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame^] | 15 | #include <dm/device-internal.h> |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 16 | #include <dm/uclass-internal.h> |
| 17 | #include <dm/util.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 18 | #include <test/ut.h> |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 22 | static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret) |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 23 | { |
| 24 | const struct dm_test_pdata *pdata = dev->platdata; |
| 25 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 26 | |
| 27 | *pingret = pingval + pdata->ping_add; |
| 28 | priv->ping_total += *pingret; |
| 29 | |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | static const struct test_ops test_ops = { |
| 34 | .ping = testfdt_drv_ping, |
| 35 | }; |
| 36 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 37 | static int testfdt_ofdata_to_platdata(struct udevice *dev) |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 38 | { |
| 39 | struct dm_test_pdata *pdata = dev_get_platdata(dev); |
| 40 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 41 | 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] | 42 | "ping-add", -1); |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 43 | pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev), |
Simon Glass | eb9ef5f | 2014-07-23 06:54:57 -0600 | [diff] [blame] | 44 | "ping-expect"); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 49 | static int testfdt_drv_probe(struct udevice *dev) |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 50 | { |
| 51 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 52 | |
| 53 | priv->ping_total += DM_TEST_START_TOTAL; |
| 54 | |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 55 | /* |
| 56 | * If this device is on a bus, the uclass_flag will be set before |
| 57 | * calling this function. This is used by |
| 58 | * dm_test_bus_child_pre_probe_uclass(). |
| 59 | */ |
| 60 | priv->uclass_total += priv->uclass_flag; |
| 61 | |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
Simon Glass | ae7f451 | 2014-06-11 23:29:45 -0600 | [diff] [blame] | 65 | static const struct udevice_id testfdt_ids[] = { |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 66 | { |
| 67 | .compatible = "denx,u-boot-fdt-test", |
| 68 | .data = DM_TEST_TYPE_FIRST }, |
| 69 | { |
| 70 | .compatible = "google,another-fdt-test", |
| 71 | .data = DM_TEST_TYPE_SECOND }, |
| 72 | { } |
| 73 | }; |
| 74 | |
| 75 | U_BOOT_DRIVER(testfdt_drv) = { |
| 76 | .name = "testfdt_drv", |
| 77 | .of_match = testfdt_ids, |
| 78 | .id = UCLASS_TEST_FDT, |
| 79 | .ofdata_to_platdata = testfdt_ofdata_to_platdata, |
| 80 | .probe = testfdt_drv_probe, |
| 81 | .ops = &test_ops, |
| 82 | .priv_auto_alloc_size = sizeof(struct dm_test_priv), |
| 83 | .platdata_auto_alloc_size = sizeof(struct dm_test_pdata), |
| 84 | }; |
| 85 | |
| 86 | /* From here is the testfdt uclass code */ |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 87 | int testfdt_ping(struct udevice *dev, int pingval, int *pingret) |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 88 | { |
| 89 | const struct test_ops *ops = device_get_ops(dev); |
| 90 | |
| 91 | if (!ops->ping) |
| 92 | return -ENOSYS; |
| 93 | |
| 94 | return ops->ping(dev, pingval, pingret); |
| 95 | } |
| 96 | |
| 97 | UCLASS_DRIVER(testfdt) = { |
| 98 | .name = "testfdt", |
| 99 | .id = UCLASS_TEST_FDT, |
Simon Glass | 9cc36a2 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 100 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame^] | 103 | struct dm_testprobe_pdata { |
| 104 | int probe_err; |
| 105 | }; |
| 106 | |
| 107 | static int testprobe_drv_probe(struct udevice *dev) |
| 108 | { |
| 109 | struct dm_testprobe_pdata *pdata = dev_get_platdata(dev); |
| 110 | |
| 111 | return pdata->probe_err; |
| 112 | } |
| 113 | |
| 114 | static const struct udevice_id testprobe_ids[] = { |
| 115 | { .compatible = "denx,u-boot-probe-test" }, |
| 116 | { } |
| 117 | }; |
| 118 | |
| 119 | U_BOOT_DRIVER(testprobe_drv) = { |
| 120 | .name = "testprobe_drv", |
| 121 | .of_match = testprobe_ids, |
| 122 | .id = UCLASS_TEST_PROBE, |
| 123 | .probe = testprobe_drv_probe, |
| 124 | .platdata_auto_alloc_size = sizeof(struct dm_testprobe_pdata), |
| 125 | }; |
| 126 | |
| 127 | UCLASS_DRIVER(testprobe) = { |
| 128 | .name = "testprobe", |
| 129 | .id = UCLASS_TEST_PROBE, |
| 130 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 131 | }; |
| 132 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 133 | int dm_check_devices(struct unit_test_state *uts, int num_devices) |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 134 | { |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 135 | struct udevice *dev; |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 136 | int ret; |
| 137 | int i; |
| 138 | |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 139 | /* |
| 140 | * Now check that the ping adds are what we expect. This is using the |
| 141 | * ping-add property in each node. |
| 142 | */ |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 143 | for (i = 0; i < num_devices; i++) { |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 144 | uint32_t base; |
| 145 | |
| 146 | ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev); |
| 147 | ut_assert(!ret); |
| 148 | |
| 149 | /* |
Simon Glass | eb9ef5f | 2014-07-23 06:54:57 -0600 | [diff] [blame] | 150 | * Get the 'ping-expect' property, which tells us what the |
| 151 | * ping add should be. We don't use the platdata because we |
| 152 | * want to test the code that sets that up |
| 153 | * (testfdt_drv_probe()). |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 154 | */ |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 155 | base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev), |
Simon Glass | eb9ef5f | 2014-07-23 06:54:57 -0600 | [diff] [blame] | 156 | "ping-expect"); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 157 | debug("dev=%d, base=%d: %s\n", i, base, |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 158 | fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL)); |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 159 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 160 | ut_assert(!dm_check_operations(uts, dev, base, |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 161 | dev_get_priv(dev))); |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 166 | |
| 167 | /* Test that FDT-based binding works correctly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 168 | static int dm_test_fdt(struct unit_test_state *uts) |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 169 | { |
Simon Glass | 9cc36a2 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 170 | const int num_devices = 6; |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 171 | struct udevice *dev; |
| 172 | struct uclass *uc; |
| 173 | int ret; |
| 174 | int i; |
| 175 | |
| 176 | ret = dm_scan_fdt(gd->fdt_blob, false); |
| 177 | ut_assert(!ret); |
| 178 | |
| 179 | ret = uclass_get(UCLASS_TEST_FDT, &uc); |
| 180 | ut_assert(!ret); |
| 181 | |
| 182 | /* These are num_devices compatible root-level device tree nodes */ |
| 183 | ut_asserteq(num_devices, list_count_items(&uc->dev_head)); |
| 184 | |
Simon Glass | f8a8544 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 185 | /* Each should have platform data but no private data */ |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 186 | for (i = 0; i < num_devices; i++) { |
| 187 | ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev); |
| 188 | ut_assert(!ret); |
| 189 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | f8a8544 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 190 | ut_assert(dev->platdata); |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 191 | } |
| 192 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 193 | ut_assertok(dm_check_devices(uts, num_devices)); |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 194 | |
| 195 | return 0; |
| 196 | } |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 197 | DM_TEST(dm_test_fdt, 0); |
Simon Glass | 00606d7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 198 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 199 | static int dm_test_fdt_pre_reloc(struct unit_test_state *uts) |
Simon Glass | 00606d7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 200 | { |
| 201 | struct uclass *uc; |
| 202 | int ret; |
| 203 | |
| 204 | ret = dm_scan_fdt(gd->fdt_blob, true); |
| 205 | ut_assert(!ret); |
| 206 | |
| 207 | ret = uclass_get(UCLASS_TEST_FDT, &uc); |
| 208 | ut_assert(!ret); |
| 209 | |
| 210 | /* These is only one pre-reloc device */ |
| 211 | ut_asserteq(1, list_count_items(&uc->dev_head)); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | DM_TEST(dm_test_fdt_pre_reloc, 0); |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 216 | |
| 217 | /* Test that sequence numbers are allocated properly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 218 | static int dm_test_fdt_uclass_seq(struct unit_test_state *uts) |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 219 | { |
| 220 | struct udevice *dev; |
| 221 | |
| 222 | /* A few basic santiy tests */ |
| 223 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev)); |
| 224 | ut_asserteq_str("b-test", dev->name); |
| 225 | |
Simon Glass | 9cc36a2 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 226 | 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] | 227 | ut_asserteq_str("a-test", dev->name); |
| 228 | |
| 229 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5, |
| 230 | true, &dev)); |
| 231 | ut_asserteq_ptr(NULL, dev); |
| 232 | |
| 233 | /* Test aliases */ |
| 234 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev)); |
| 235 | ut_asserteq_str("e-test", dev->name); |
| 236 | |
| 237 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7, |
| 238 | true, &dev)); |
| 239 | |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 240 | /* |
| 241 | * Note that c-test nodes are not probed since it is not a top-level |
| 242 | * node |
| 243 | */ |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 244 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev)); |
| 245 | ut_asserteq_str("b-test", dev->name); |
| 246 | |
| 247 | /* |
| 248 | * d-test wants sequence number 3 also, but it can't have it because |
| 249 | * b-test gets it first. |
| 250 | */ |
| 251 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev)); |
| 252 | ut_asserteq_str("d-test", dev->name); |
| 253 | |
| 254 | /* d-test actually gets 0 */ |
| 255 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 0, &dev)); |
| 256 | ut_asserteq_str("d-test", dev->name); |
| 257 | |
| 258 | /* initially no one wants seq 1 */ |
| 259 | ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, |
| 260 | &dev)); |
| 261 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
Simon Glass | 9cc36a2 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 262 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev)); |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 263 | |
| 264 | /* But now that it is probed, we can find it */ |
| 265 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, &dev)); |
Simon Glass | 9cc36a2 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 266 | ut_asserteq_str("f-test", dev->name); |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | 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] | 271 | |
| 272 | /* Test that we can find a device by device tree offset */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 273 | static int dm_test_fdt_offset(struct unit_test_state *uts) |
Simon Glass | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 274 | { |
| 275 | const void *blob = gd->fdt_blob; |
| 276 | struct udevice *dev; |
| 277 | int node; |
| 278 | |
| 279 | node = fdt_path_offset(blob, "/e-test"); |
| 280 | ut_assert(node > 0); |
| 281 | ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node, |
| 282 | &dev)); |
| 283 | ut_asserteq_str("e-test", dev->name); |
| 284 | |
| 285 | /* This node should not be bound */ |
| 286 | node = fdt_path_offset(blob, "/junk"); |
| 287 | ut_assert(node > 0); |
| 288 | ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, |
| 289 | node, &dev)); |
| 290 | |
| 291 | /* This is not a top level node so should not be probed */ |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 292 | node = fdt_path_offset(blob, "/some-bus/c-test@5"); |
Simon Glass | f4cdead | 2014-07-23 06:55:14 -0600 | [diff] [blame] | 293 | ut_assert(node > 0); |
| 294 | ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, |
| 295 | node, &dev)); |
| 296 | |
| 297 | return 0; |
| 298 | } |
Simon Glass | 86b54ec | 2017-05-18 20:09:44 -0600 | [diff] [blame] | 299 | DM_TEST(dm_test_fdt_offset, |
| 300 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE); |
Simon Glass | 9856157 | 2017-04-23 20:10:44 -0600 | [diff] [blame^] | 301 | |
| 302 | /** |
| 303 | * Test various error conditions with uclass_first_device() and |
| 304 | * uclass_next_device() |
| 305 | */ |
| 306 | static int dm_test_first_next_device(struct unit_test_state *uts) |
| 307 | { |
| 308 | struct dm_testprobe_pdata *pdata; |
| 309 | struct udevice *dev, *parent = NULL; |
| 310 | int count; |
| 311 | int ret; |
| 312 | |
| 313 | /* There should be 4 devices */ |
| 314 | for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0; |
| 315 | dev; |
| 316 | ret = uclass_next_device(&dev)) { |
| 317 | count++; |
| 318 | parent = dev_get_parent(dev); |
| 319 | } |
| 320 | ut_assertok(ret); |
| 321 | ut_asserteq(4, count); |
| 322 | |
| 323 | /* Remove them and try again, with an error on the second one */ |
| 324 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev)); |
| 325 | pdata = dev_get_platdata(dev); |
| 326 | pdata->probe_err = -ENOMEM; |
| 327 | device_remove(parent, DM_REMOVE_NORMAL); |
| 328 | ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev)); |
| 329 | ut_asserteq(-ENOMEM, uclass_next_device(&dev)); |
| 330 | ut_asserteq_ptr(dev, NULL); |
| 331 | |
| 332 | /* Now an error on the first one */ |
| 333 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev)); |
| 334 | pdata = dev_get_platdata(dev); |
| 335 | pdata->probe_err = -ENOENT; |
| 336 | device_remove(parent, DM_REMOVE_NORMAL); |
| 337 | ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev)); |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | DM_TEST(dm_test_first_next_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |