Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Tests for the core driver model code |
| 4 | * |
| 5 | * Copyright (c) 2013 Google, Inc |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <errno.h> |
| 10 | #include <dm.h> |
| 11 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
| 16 | #include <dm/root.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 17 | #include <dm/util.h> |
| 18 | #include <dm/test.h> |
| 19 | #include <dm/uclass-internal.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 20 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 21 | #include <test/ut.h> |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 22 | |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
| 25 | enum { |
| 26 | TEST_INTVAL1 = 0, |
| 27 | TEST_INTVAL2 = 3, |
| 28 | TEST_INTVAL3 = 6, |
| 29 | TEST_INTVAL_MANUAL = 101112, |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 30 | TEST_INTVAL_PRE_RELOC = 7, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static const struct dm_test_pdata test_pdata[] = { |
| 34 | { .ping_add = TEST_INTVAL1, }, |
| 35 | { .ping_add = TEST_INTVAL2, }, |
| 36 | { .ping_add = TEST_INTVAL3, }, |
| 37 | }; |
| 38 | |
| 39 | static const struct dm_test_pdata test_pdata_manual = { |
| 40 | .ping_add = TEST_INTVAL_MANUAL, |
| 41 | }; |
| 42 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 43 | static const struct dm_test_pdata test_pdata_pre_reloc = { |
| 44 | .ping_add = TEST_INTVAL_PRE_RELOC, |
| 45 | }; |
| 46 | |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 47 | U_BOOT_DRVINFO(dm_test_info1) = { |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 48 | .name = "test_drv", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 49 | .plat = &test_pdata[0], |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 52 | U_BOOT_DRVINFO(dm_test_info2) = { |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 53 | .name = "test_drv", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 54 | .plat = &test_pdata[1], |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 57 | U_BOOT_DRVINFO(dm_test_info3) = { |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 58 | .name = "test_drv", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 59 | .plat = &test_pdata[2], |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static struct driver_info driver_info_manual = { |
| 63 | .name = "test_manual_drv", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 64 | .plat = &test_pdata_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 67 | static struct driver_info driver_info_pre_reloc = { |
| 68 | .name = "test_pre_reloc_drv", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 69 | .plat = &test_pdata_pre_reloc, |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 70 | }; |
| 71 | |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 72 | static struct driver_info driver_info_act_dma = { |
| 73 | .name = "test_act_dma_drv", |
| 74 | }; |
| 75 | |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 76 | static struct driver_info driver_info_vital_clk = { |
| 77 | .name = "test_vital_clk_drv", |
| 78 | }; |
| 79 | |
| 80 | static struct driver_info driver_info_act_dma_vital_clk = { |
| 81 | .name = "test_act_dma_vital_clk_drv", |
| 82 | }; |
| 83 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 84 | void dm_leak_check_start(struct unit_test_state *uts) |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 85 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 86 | uts->start = mallinfo(); |
| 87 | if (!uts->start.uordblks) |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 88 | puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n"); |
| 89 | } |
| 90 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 91 | int dm_leak_check_end(struct unit_test_state *uts) |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 92 | { |
| 93 | struct mallinfo end; |
Simon Glass | 94d2218 | 2015-09-12 08:45:20 -0600 | [diff] [blame] | 94 | int id, diff; |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 95 | |
| 96 | /* Don't delete the root class, since we started with that */ |
| 97 | for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) { |
| 98 | struct uclass *uc; |
| 99 | |
| 100 | uc = uclass_find(id); |
| 101 | if (!uc) |
| 102 | continue; |
| 103 | ut_assertok(uclass_destroy(uc)); |
| 104 | } |
| 105 | |
| 106 | end = mallinfo(); |
Simon Glass | 94d2218 | 2015-09-12 08:45:20 -0600 | [diff] [blame] | 107 | diff = end.uordblks - uts->start.uordblks; |
| 108 | if (diff > 0) |
| 109 | printf("Leak: lost %#xd bytes\n", diff); |
| 110 | else if (diff < 0) |
| 111 | printf("Leak: gained %#xd bytes\n", -diff); |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 112 | ut_asserteq(uts->start.uordblks, end.uordblks); |
Simon Glass | 0927a6f | 2014-10-04 11:29:50 -0600 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 117 | /* Test that binding with plat occurs correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 118 | static int dm_test_autobind(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 119 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 120 | struct dm_test_state *dms = uts->priv; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 121 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * We should have a single class (UCLASS_ROOT) and a single root |
| 125 | * device with no children. |
| 126 | */ |
| 127 | ut_assert(dms->root); |
Simon Glass | 784cd9e | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 128 | ut_asserteq(1, list_count_items(gd->uclass_root)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 129 | ut_asserteq(0, list_count_items(&gd->dm_root->child_head)); |
| 130 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]); |
| 131 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 132 | ut_assertok(dm_scan_plat(false)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 133 | |
| 134 | /* We should have our test class now at least, plus more children */ |
Simon Glass | 784cd9e | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 135 | ut_assert(1 < list_count_items(gd->uclass_root)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 136 | ut_assert(0 < list_count_items(&gd->dm_root->child_head)); |
| 137 | |
| 138 | /* Our 3 dm_test_infox children should be bound to the test uclass */ |
| 139 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]); |
| 140 | |
| 141 | /* No devices should be probed */ |
| 142 | list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node) |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 143 | ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 144 | |
| 145 | /* Our test driver should have been bound 3 times */ |
| 146 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | DM_TEST(dm_test_autobind, 0); |
| 151 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 152 | /* Test that binding with uclass plat allocation occurs correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 153 | static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts) |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 154 | { |
| 155 | struct dm_test_perdev_uc_pdata *uc_pdata; |
| 156 | struct udevice *dev; |
| 157 | struct uclass *uc; |
| 158 | |
| 159 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 160 | ut_assert(uc); |
| 161 | |
| 162 | /** |
| 163 | * Test if test uclass driver requires allocation for the uclass |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 164 | * platform data and then check the dev->uclass_plat pointer. |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 165 | */ |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 166 | ut_assert(uc->uc_drv->per_device_plat_auto); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 167 | |
| 168 | for (uclass_find_first_device(UCLASS_TEST, &dev); |
| 169 | dev; |
| 170 | uclass_find_next_device(&dev)) { |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 171 | ut_assertnonnull(dev); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 172 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 173 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 174 | ut_assert(uc_pdata); |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 179 | DM_TEST(dm_test_autobind_uclass_pdata_alloc, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 180 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 181 | /* Test that binding with uclass plat setting occurs correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 182 | static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts) |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 183 | { |
| 184 | struct dm_test_perdev_uc_pdata *uc_pdata; |
| 185 | struct udevice *dev; |
| 186 | |
| 187 | /** |
| 188 | * In the test_postbind() method of test uclass driver, the uclass |
| 189 | * platform data should be set to three test int values - test it. |
| 190 | */ |
| 191 | for (uclass_find_first_device(UCLASS_TEST, &dev); |
| 192 | dev; |
| 193 | uclass_find_next_device(&dev)) { |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 194 | ut_assertnonnull(dev); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 195 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 196 | uc_pdata = dev_get_uclass_plat(dev); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 197 | ut_assert(uc_pdata); |
| 198 | ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1); |
| 199 | ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2); |
| 200 | ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3); |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 205 | DM_TEST(dm_test_autobind_uclass_pdata_valid, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 34cbe31 | 2015-04-15 13:07:19 +0200 | [diff] [blame] | 206 | |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 207 | /* Test that autoprobe finds all the expected devices */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 208 | static int dm_test_autoprobe(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 209 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 210 | struct dm_test_state *dms = uts->priv; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 211 | int expected_base_add; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 212 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 213 | struct uclass *uc; |
| 214 | int i; |
| 215 | |
| 216 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 217 | ut_assert(uc); |
| 218 | |
| 219 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 220 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 221 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]); |
| 222 | |
| 223 | /* The root device should not be activated until needed */ |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 224 | ut_assert(dev_get_flags(dms->root) & DM_FLAG_ACTIVATED); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 225 | |
| 226 | /* |
| 227 | * We should be able to find the three test devices, and they should |
| 228 | * all be activated as they are used (lazy activation, required by |
| 229 | * U-Boot) |
| 230 | */ |
| 231 | for (i = 0; i < 3; i++) { |
| 232 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 233 | ut_assert(dev); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 234 | ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 235 | "Driver %d/%s already activated", i, dev->name); |
| 236 | |
| 237 | /* This should activate it */ |
| 238 | ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); |
| 239 | ut_assert(dev); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 240 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 241 | |
| 242 | /* Activating a device should activate the root device */ |
| 243 | if (!i) |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 244 | ut_assert(dev_get_flags(dms->root) & DM_FLAG_ACTIVATED); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 247 | /* |
| 248 | * Our 3 dm_test_info children should be passed to pre_probe and |
| 249 | * post_probe |
| 250 | */ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 251 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]); |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 252 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 253 | |
| 254 | /* Also we can check the per-device data */ |
| 255 | expected_base_add = 0; |
| 256 | for (i = 0; i < 3; i++) { |
| 257 | struct dm_test_uclass_perdev_priv *priv; |
| 258 | struct dm_test_pdata *pdata; |
| 259 | |
| 260 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 261 | ut_assert(dev); |
| 262 | |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 263 | priv = dev_get_uclass_priv(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 264 | ut_assert(priv); |
| 265 | ut_asserteq(expected_base_add, priv->base_add); |
| 266 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 267 | pdata = dev_get_plat(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 268 | expected_base_add += pdata->ping_add; |
| 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 273 | DM_TEST(dm_test_autoprobe, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 274 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 275 | /* Check that we see the correct plat in each device */ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 276 | static int dm_test_plat(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 277 | { |
| 278 | const struct dm_test_pdata *pdata; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 279 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 280 | int i; |
| 281 | |
| 282 | for (i = 0; i < 3; i++) { |
| 283 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 284 | ut_assert(dev); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 285 | pdata = dev_get_plat(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 286 | ut_assert(pdata->ping_add == test_pdata[i].ping_add); |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 291 | DM_TEST(dm_test_plat, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 292 | |
| 293 | /* Test that we can bind, probe, remove, unbind a driver */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 294 | static int dm_test_lifecycle(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 295 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 296 | struct dm_test_state *dms = uts->priv; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 297 | int op_count[DM_TEST_OP_COUNT]; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 298 | struct udevice *dev, *test_dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 299 | int pingret; |
| 300 | int ret; |
| 301 | |
| 302 | memcpy(op_count, dm_testdrv_op_count, sizeof(op_count)); |
| 303 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 304 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 305 | &dev)); |
| 306 | ut_assert(dev); |
| 307 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] |
| 308 | == op_count[DM_TEST_OP_BIND] + 1); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 309 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 310 | |
| 311 | /* Probe the device - it should fail allocating private data */ |
| 312 | dms->force_fail_alloc = 1; |
| 313 | ret = device_probe(dev); |
| 314 | ut_assert(ret == -ENOMEM); |
| 315 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE] |
| 316 | == op_count[DM_TEST_OP_PROBE] + 1); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 317 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 318 | |
| 319 | /* Try again without the alloc failure */ |
| 320 | dms->force_fail_alloc = 0; |
| 321 | ut_assertok(device_probe(dev)); |
| 322 | ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE] |
| 323 | == op_count[DM_TEST_OP_PROBE] + 2); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 324 | ut_assert(dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 325 | |
| 326 | /* This should be device 3 in the uclass */ |
| 327 | ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); |
| 328 | ut_assert(dev == test_dev); |
| 329 | |
| 330 | /* Try ping */ |
| 331 | ut_assertok(test_ping(dev, 100, &pingret)); |
| 332 | ut_assert(pingret == 102); |
| 333 | |
| 334 | /* Now remove device 3 */ |
| 335 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 336 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 337 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]); |
| 338 | |
| 339 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 340 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]); |
| 341 | ut_assertok(device_unbind(dev)); |
| 342 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 343 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]); |
| 344 | |
| 345 | return 0; |
| 346 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 347 | DM_TEST(dm_test_lifecycle, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 348 | |
| 349 | /* Test that we can bind/unbind and the lists update correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 350 | static int dm_test_ordering(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 351 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 352 | struct dm_test_state *dms = uts->priv; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 353 | struct udevice *dev, *dev_penultimate, *dev_last, *test_dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 354 | int pingret; |
| 355 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 356 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 357 | &dev)); |
| 358 | ut_assert(dev); |
| 359 | |
| 360 | /* Bind two new devices (numbers 4 and 5) */ |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 361 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 362 | &dev_penultimate)); |
| 363 | ut_assert(dev_penultimate); |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 364 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 365 | &dev_last)); |
| 366 | ut_assert(dev_last); |
| 367 | |
| 368 | /* Now remove device 3 */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 369 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 370 | ut_assertok(device_unbind(dev)); |
| 371 | |
| 372 | /* The device numbering should have shifted down one */ |
| 373 | ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); |
| 374 | ut_assert(dev_penultimate == test_dev); |
| 375 | ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev)); |
| 376 | ut_assert(dev_last == test_dev); |
| 377 | |
| 378 | /* Add back the original device 3, now in position 5 */ |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 379 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
| 380 | &dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 381 | ut_assert(dev); |
| 382 | |
| 383 | /* Try ping */ |
| 384 | ut_assertok(test_ping(dev, 100, &pingret)); |
| 385 | ut_assert(pingret == 102); |
| 386 | |
| 387 | /* Remove 3 and 4 */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 388 | ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 389 | ut_assertok(device_unbind(dev_penultimate)); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 390 | ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 391 | ut_assertok(device_unbind(dev_last)); |
| 392 | |
| 393 | /* Our device should now be in position 3 */ |
| 394 | ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); |
| 395 | ut_assert(dev == test_dev); |
| 396 | |
| 397 | /* Now remove device 3 */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 398 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 399 | ut_assertok(device_unbind(dev)); |
| 400 | |
| 401 | return 0; |
| 402 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 403 | DM_TEST(dm_test_ordering, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 404 | |
| 405 | /* Check that we can perform operations on a device (do a ping) */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 406 | int dm_check_operations(struct unit_test_state *uts, struct udevice *dev, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 407 | uint32_t base, struct dm_test_priv *priv) |
| 408 | { |
| 409 | int expected; |
| 410 | int pingret; |
| 411 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 412 | /* Getting the child device should allocate plat / priv */ |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 413 | ut_assertok(testfdt_ping(dev, 10, &pingret)); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 414 | ut_assert(dev_get_priv(dev)); |
| 415 | ut_assert(dev_get_plat(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 416 | |
| 417 | expected = 10 + base; |
| 418 | ut_asserteq(expected, pingret); |
| 419 | |
| 420 | /* Do another ping */ |
| 421 | ut_assertok(testfdt_ping(dev, 20, &pingret)); |
| 422 | expected = 20 + base; |
| 423 | ut_asserteq(expected, pingret); |
| 424 | |
| 425 | /* Now check the ping_total */ |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 426 | priv = dev_get_priv(dev); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 427 | ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2, |
| 428 | priv->ping_total); |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | /* Check that we can perform operations on devices */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 434 | static int dm_test_operations(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 435 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 436 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 437 | int i; |
| 438 | |
| 439 | /* |
| 440 | * Now check that the ping adds are what we expect. This is using the |
| 441 | * ping-add property in each node. |
| 442 | */ |
| 443 | for (i = 0; i < ARRAY_SIZE(test_pdata); i++) { |
| 444 | uint32_t base; |
| 445 | |
| 446 | ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); |
| 447 | |
| 448 | /* |
| 449 | * Get the 'reg' property, which tells us what the ping add |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 450 | * should be. We don't use the plat because we want |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 451 | * to test the code that sets that up (testfdt_drv_probe()). |
| 452 | */ |
| 453 | base = test_pdata[i].ping_add; |
| 454 | debug("dev=%d, base=%d\n", i, base); |
| 455 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 456 | ut_assert(!dm_check_operations(uts, dev, base, dev_get_priv(dev))); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | return 0; |
| 460 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 461 | DM_TEST(dm_test_operations, UT_TESTF_SCAN_PDATA); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 462 | |
| 463 | /* Remove all drivers and check that things work */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 464 | static int dm_test_remove(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 465 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 466 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 467 | int i; |
| 468 | |
| 469 | for (i = 0; i < 3; i++) { |
| 470 | ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); |
| 471 | ut_assert(dev); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 472 | ut_assertf(dev_get_flags(dev) & DM_FLAG_ACTIVATED, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 473 | "Driver %d/%s not activated", i, dev->name); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 474 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 475 | ut_assertf(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED), |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 476 | "Driver %d/%s should have deactivated", i, |
| 477 | dev->name); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 478 | ut_assert(!dev_get_priv(dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 483 | DM_TEST(dm_test_remove, UT_TESTF_SCAN_PDATA | UT_TESTF_PROBE_TEST); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 484 | |
| 485 | /* Remove and recreate everything, check for memory leaks */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 486 | static int dm_test_leak(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 487 | { |
| 488 | int i; |
| 489 | |
| 490 | for (i = 0; i < 2; i++) { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 491 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 492 | int ret; |
| 493 | int id; |
| 494 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 495 | dm_leak_check_start(uts); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 496 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 497 | ut_assertok(dm_scan_plat(false)); |
Simon Glass | 5039cab | 2020-11-28 17:50:09 -0700 | [diff] [blame] | 498 | ut_assertok(dm_scan_fdt(false)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 499 | |
| 500 | /* Scanning the uclass is enough to probe all the devices */ |
| 501 | for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) { |
| 502 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 503 | dev; |
| 504 | ret = uclass_next_device(&dev)) |
| 505 | ; |
| 506 | ut_assertok(ret); |
| 507 | } |
| 508 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 509 | ut_assertok(dm_leak_check_end(uts)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | DM_TEST(dm_test_leak, 0); |
| 515 | |
| 516 | /* Test uclass init/destroy methods */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 517 | static int dm_test_uclass(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 518 | { |
| 519 | struct uclass *uc; |
| 520 | |
| 521 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 522 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); |
| 523 | ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 524 | ut_assert(uclass_get_priv(uc)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 525 | |
| 526 | ut_assertok(uclass_destroy(uc)); |
| 527 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); |
| 528 | ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | DM_TEST(dm_test_uclass, 0); |
| 533 | |
| 534 | /** |
| 535 | * create_children() - Create children of a parent node |
| 536 | * |
| 537 | * @dms: Test system state |
| 538 | * @parent: Parent device |
| 539 | * @count: Number of children to create |
| 540 | * @key: Key value to put in first child. Subsequence children |
| 541 | * receive an incrementing value |
| 542 | * @child: If not NULL, then the child device pointers are written into |
| 543 | * this array. |
| 544 | * @return 0 if OK, -ve on error |
| 545 | */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 546 | static int create_children(struct unit_test_state *uts, struct udevice *parent, |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 547 | int count, int key, struct udevice *child[]) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 548 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 549 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 550 | int i; |
| 551 | |
| 552 | for (i = 0; i < count; i++) { |
| 553 | struct dm_test_pdata *pdata; |
| 554 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 555 | ut_assertok(device_bind_by_name(parent, false, |
| 556 | &driver_info_manual, &dev)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 557 | pdata = calloc(1, sizeof(*pdata)); |
| 558 | pdata->ping_add = key + i; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 559 | dev_set_plat(dev, pdata); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 560 | if (child) |
| 561 | child[i] = dev; |
| 562 | } |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | #define NODE_COUNT 10 |
| 568 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 569 | static int dm_test_children(struct unit_test_state *uts) |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 570 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 571 | struct dm_test_state *dms = uts->priv; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 572 | struct udevice *top[NODE_COUNT]; |
| 573 | struct udevice *child[NODE_COUNT]; |
| 574 | struct udevice *grandchild[NODE_COUNT]; |
| 575 | struct udevice *dev; |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 576 | int total; |
| 577 | int ret; |
| 578 | int i; |
| 579 | |
| 580 | /* We don't care about the numbering for this test */ |
| 581 | dms->skip_post_probe = 1; |
| 582 | |
| 583 | ut_assert(NODE_COUNT > 5); |
| 584 | |
| 585 | /* First create 10 top-level children */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 586 | ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 587 | |
| 588 | /* Now a few have their own children */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 589 | ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL)); |
| 590 | ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 591 | |
| 592 | /* And grandchildren */ |
| 593 | for (i = 0; i < NODE_COUNT; i++) |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 594 | ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i, |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 595 | i == 2 ? grandchild : NULL)); |
| 596 | |
| 597 | /* Check total number of devices */ |
| 598 | total = NODE_COUNT * (3 + NODE_COUNT); |
| 599 | ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]); |
| 600 | |
| 601 | /* Try probing one of the grandchildren */ |
| 602 | ut_assertok(uclass_get_device(UCLASS_TEST, |
| 603 | NODE_COUNT * 3 + 2 * NODE_COUNT, &dev)); |
| 604 | ut_asserteq_ptr(grandchild[0], dev); |
| 605 | |
| 606 | /* |
| 607 | * This should have probed the child and top node also, for a total |
| 608 | * of 3 nodes. |
| 609 | */ |
| 610 | ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]); |
| 611 | |
| 612 | /* Probe the other grandchildren */ |
| 613 | for (i = 1; i < NODE_COUNT; i++) |
| 614 | ut_assertok(device_probe(grandchild[i])); |
| 615 | |
| 616 | ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]); |
| 617 | |
| 618 | /* Probe everything */ |
| 619 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 620 | dev; |
| 621 | ret = uclass_next_device(&dev)) |
| 622 | ; |
| 623 | ut_assertok(ret); |
| 624 | |
| 625 | ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]); |
| 626 | |
| 627 | /* Remove a top-level child and check that the children are removed */ |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 628 | ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 629 | ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]); |
| 630 | dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0; |
| 631 | |
| 632 | /* Try one with grandchildren */ |
| 633 | ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev)); |
| 634 | ut_asserteq_ptr(dev, top[5]); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 635 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 636 | ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT), |
| 637 | dm_testdrv_op_count[DM_TEST_OP_REMOVE]); |
| 638 | |
| 639 | /* Try the same with unbind */ |
| 640 | ut_assertok(device_unbind(top[2])); |
| 641 | ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 642 | dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0; |
| 643 | |
| 644 | /* Try one with grandchildren */ |
| 645 | ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev)); |
| 646 | ut_asserteq_ptr(dev, top[6]); |
| 647 | ut_assertok(device_unbind(top[5])); |
| 648 | ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT), |
| 649 | dm_testdrv_op_count[DM_TEST_OP_UNBIND]); |
| 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | DM_TEST(dm_test_children, 0); |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 654 | |
Claudiu Beznea | bf5b823 | 2020-09-07 17:46:33 +0300 | [diff] [blame] | 655 | static int dm_test_device_reparent(struct unit_test_state *uts) |
| 656 | { |
| 657 | struct dm_test_state *dms = uts->priv; |
| 658 | struct udevice *top[NODE_COUNT]; |
| 659 | struct udevice *child[NODE_COUNT]; |
| 660 | struct udevice *grandchild[NODE_COUNT]; |
| 661 | struct udevice *dev; |
| 662 | int total; |
| 663 | int ret; |
| 664 | int i; |
| 665 | |
| 666 | /* We don't care about the numbering for this test */ |
| 667 | dms->skip_post_probe = 1; |
| 668 | |
| 669 | ut_assert(NODE_COUNT > 5); |
| 670 | |
| 671 | /* First create 10 top-level children */ |
| 672 | ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top)); |
| 673 | |
| 674 | /* Now a few have their own children */ |
| 675 | ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL)); |
| 676 | ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child)); |
| 677 | |
| 678 | /* And grandchildren */ |
| 679 | for (i = 0; i < NODE_COUNT; i++) |
| 680 | ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i, |
| 681 | i == 2 ? grandchild : NULL)); |
| 682 | |
| 683 | /* Check total number of devices */ |
| 684 | total = NODE_COUNT * (3 + NODE_COUNT); |
| 685 | ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]); |
| 686 | |
| 687 | /* Probe everything */ |
| 688 | for (i = 0; i < total; i++) |
| 689 | ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); |
| 690 | |
| 691 | /* Re-parent top-level children with no grandchildren. */ |
| 692 | ut_assertok(device_reparent(top[3], top[0])); |
| 693 | /* try to get devices */ |
| 694 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 695 | dev; |
| 696 | ret = uclass_find_next_device(&dev)) { |
| 697 | ut_assert(!ret); |
| 698 | ut_assertnonnull(dev); |
| 699 | } |
| 700 | |
| 701 | ut_assertok(device_reparent(top[4], top[0])); |
| 702 | /* try to get devices */ |
| 703 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 704 | dev; |
| 705 | ret = uclass_find_next_device(&dev)) { |
| 706 | ut_assert(!ret); |
| 707 | ut_assertnonnull(dev); |
| 708 | } |
| 709 | |
| 710 | /* Re-parent top-level children with grandchildren. */ |
| 711 | ut_assertok(device_reparent(top[2], top[0])); |
| 712 | /* try to get devices */ |
| 713 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 714 | dev; |
| 715 | ret = uclass_find_next_device(&dev)) { |
| 716 | ut_assert(!ret); |
| 717 | ut_assertnonnull(dev); |
| 718 | } |
| 719 | |
| 720 | ut_assertok(device_reparent(top[5], top[2])); |
| 721 | /* try to get devices */ |
| 722 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 723 | dev; |
| 724 | ret = uclass_find_next_device(&dev)) { |
| 725 | ut_assert(!ret); |
| 726 | ut_assertnonnull(dev); |
| 727 | } |
| 728 | |
| 729 | /* Re-parent grandchildren. */ |
| 730 | ut_assertok(device_reparent(grandchild[0], top[1])); |
| 731 | /* try to get devices */ |
| 732 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 733 | dev; |
| 734 | ret = uclass_find_next_device(&dev)) { |
| 735 | ut_assert(!ret); |
| 736 | ut_assertnonnull(dev); |
| 737 | } |
| 738 | |
| 739 | ut_assertok(device_reparent(grandchild[1], top[1])); |
| 740 | /* try to get devices */ |
| 741 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 742 | dev; |
| 743 | ret = uclass_find_next_device(&dev)) { |
| 744 | ut_assert(!ret); |
| 745 | ut_assertnonnull(dev); |
| 746 | } |
| 747 | |
| 748 | /* Remove re-pareneted devices. */ |
| 749 | ut_assertok(device_remove(top[3], DM_REMOVE_NORMAL)); |
| 750 | /* try to get devices */ |
| 751 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 752 | dev; |
| 753 | ret = uclass_find_next_device(&dev)) { |
| 754 | ut_assert(!ret); |
| 755 | ut_assertnonnull(dev); |
| 756 | } |
| 757 | |
| 758 | ut_assertok(device_remove(top[4], DM_REMOVE_NORMAL)); |
| 759 | /* try to get devices */ |
| 760 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 761 | dev; |
| 762 | ret = uclass_find_next_device(&dev)) { |
| 763 | ut_assert(!ret); |
| 764 | ut_assertnonnull(dev); |
| 765 | } |
| 766 | |
| 767 | ut_assertok(device_remove(top[5], DM_REMOVE_NORMAL)); |
| 768 | /* try to get devices */ |
| 769 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 770 | dev; |
| 771 | ret = uclass_find_next_device(&dev)) { |
| 772 | ut_assert(!ret); |
| 773 | ut_assertnonnull(dev); |
| 774 | } |
| 775 | |
| 776 | ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL)); |
| 777 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 778 | dev; |
| 779 | ret = uclass_find_next_device(&dev)) { |
| 780 | ut_assert(!ret); |
| 781 | ut_assertnonnull(dev); |
| 782 | } |
| 783 | |
| 784 | ut_assertok(device_remove(grandchild[0], DM_REMOVE_NORMAL)); |
| 785 | /* try to get devices */ |
| 786 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 787 | dev; |
| 788 | ret = uclass_find_next_device(&dev)) { |
| 789 | ut_assert(!ret); |
| 790 | ut_assertnonnull(dev); |
| 791 | } |
| 792 | |
| 793 | ut_assertok(device_remove(grandchild[1], DM_REMOVE_NORMAL)); |
| 794 | /* try to get devices */ |
| 795 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 796 | dev; |
| 797 | ret = uclass_find_next_device(&dev)) { |
| 798 | ut_assert(!ret); |
| 799 | ut_assertnonnull(dev); |
| 800 | } |
| 801 | |
| 802 | /* Try the same with unbind */ |
| 803 | ut_assertok(device_unbind(top[3])); |
| 804 | ut_assertok(device_unbind(top[4])); |
| 805 | ut_assertok(device_unbind(top[5])); |
| 806 | ut_assertok(device_unbind(top[2])); |
| 807 | |
| 808 | ut_assertok(device_unbind(grandchild[0])); |
| 809 | ut_assertok(device_unbind(grandchild[1])); |
| 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | DM_TEST(dm_test_device_reparent, 0); |
| 814 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 815 | /* Test that pre-relocation devices work as expected */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 816 | static int dm_test_pre_reloc(struct unit_test_state *uts) |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 817 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 818 | struct dm_test_state *dms = uts->priv; |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 819 | struct udevice *dev; |
| 820 | |
| 821 | /* The normal driver should refuse to bind before relocation */ |
| 822 | ut_asserteq(-EPERM, device_bind_by_name(dms->root, true, |
| 823 | &driver_info_manual, &dev)); |
| 824 | |
| 825 | /* But this one is marked pre-reloc */ |
| 826 | ut_assertok(device_bind_by_name(dms->root, true, |
| 827 | &driver_info_pre_reloc, &dev)); |
| 828 | |
| 829 | return 0; |
| 830 | } |
| 831 | DM_TEST(dm_test_pre_reloc, 0); |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 832 | |
Stefan Roese | eaffda7 | 2017-03-27 11:02:43 +0200 | [diff] [blame] | 833 | /* |
| 834 | * Test that removal of devices, either via the "normal" device_remove() |
| 835 | * API or via the device driver selective flag works as expected |
| 836 | */ |
| 837 | static int dm_test_remove_active_dma(struct unit_test_state *uts) |
| 838 | { |
| 839 | struct dm_test_state *dms = uts->priv; |
| 840 | struct udevice *dev; |
| 841 | |
| 842 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma, |
| 843 | &dev)); |
| 844 | ut_assert(dev); |
| 845 | |
| 846 | /* Probe the device */ |
| 847 | ut_assertok(device_probe(dev)); |
| 848 | |
| 849 | /* Test if device is active right now */ |
| 850 | ut_asserteq(true, device_active(dev)); |
| 851 | |
| 852 | /* Remove the device via selective remove flag */ |
| 853 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 854 | |
| 855 | /* Test if device is inactive right now */ |
| 856 | ut_asserteq(false, device_active(dev)); |
| 857 | |
| 858 | /* Probe the device again */ |
| 859 | ut_assertok(device_probe(dev)); |
| 860 | |
| 861 | /* Test if device is active right now */ |
| 862 | ut_asserteq(true, device_active(dev)); |
| 863 | |
| 864 | /* Remove the device via "normal" remove API */ |
| 865 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
| 866 | |
| 867 | /* Test if device is inactive right now */ |
| 868 | ut_asserteq(false, device_active(dev)); |
| 869 | |
| 870 | /* |
| 871 | * Test if a device without the active DMA flags is not removed upon |
| 872 | * the active DMA remove call |
| 873 | */ |
| 874 | ut_assertok(device_unbind(dev)); |
| 875 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
| 876 | &dev)); |
| 877 | ut_assert(dev); |
| 878 | |
| 879 | /* Probe the device */ |
| 880 | ut_assertok(device_probe(dev)); |
| 881 | |
| 882 | /* Test if device is active right now */ |
| 883 | ut_asserteq(true, device_active(dev)); |
| 884 | |
| 885 | /* Remove the device via selective remove flag */ |
| 886 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 887 | |
| 888 | /* Test if device is still active right now */ |
| 889 | ut_asserteq(true, device_active(dev)); |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | DM_TEST(dm_test_remove_active_dma, 0); |
| 894 | |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 895 | /* Test removal of 'vital' devices */ |
| 896 | static int dm_test_remove_vital(struct unit_test_state *uts) |
| 897 | { |
| 898 | struct dm_test_state *dms = uts->priv; |
| 899 | struct udevice *normal, *dma, *vital, *dma_vital; |
| 900 | |
| 901 | /* Skip the behaviour in test_post_probe() */ |
| 902 | dms->skip_post_probe = 1; |
| 903 | |
| 904 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual, |
| 905 | &normal)); |
| 906 | ut_assertnonnull(normal); |
| 907 | |
| 908 | ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma, |
| 909 | &dma)); |
| 910 | ut_assertnonnull(dma); |
| 911 | |
| 912 | ut_assertok(device_bind_by_name(dms->root, false, |
| 913 | &driver_info_vital_clk, &vital)); |
| 914 | ut_assertnonnull(vital); |
| 915 | |
| 916 | ut_assertok(device_bind_by_name(dms->root, false, |
| 917 | &driver_info_act_dma_vital_clk, |
| 918 | &dma_vital)); |
| 919 | ut_assertnonnull(dma_vital); |
| 920 | |
| 921 | /* Probe the devices */ |
| 922 | ut_assertok(device_probe(normal)); |
| 923 | ut_assertok(device_probe(dma)); |
| 924 | ut_assertok(device_probe(vital)); |
| 925 | ut_assertok(device_probe(dma_vital)); |
| 926 | |
| 927 | /* Check that devices are active right now */ |
| 928 | ut_asserteq(true, device_active(normal)); |
| 929 | ut_asserteq(true, device_active(dma)); |
| 930 | ut_asserteq(true, device_active(vital)); |
| 931 | ut_asserteq(true, device_active(dma_vital)); |
| 932 | |
| 933 | /* Remove active devices via selective remove flag */ |
| 934 | dm_remove_devices_flags(DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_ALL); |
| 935 | |
| 936 | /* |
| 937 | * Check that this only has an effect on the dma device, since two |
| 938 | * devices are vital and the third does not have active DMA |
| 939 | */ |
| 940 | ut_asserteq(true, device_active(normal)); |
| 941 | ut_asserteq(false, device_active(dma)); |
| 942 | ut_asserteq(true, device_active(vital)); |
| 943 | ut_asserteq(true, device_active(dma_vital)); |
| 944 | |
| 945 | /* Remove active devices via selective remove flag */ |
| 946 | ut_assertok(device_probe(dma)); |
| 947 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); |
| 948 | |
| 949 | /* This should have affected both active-dma devices */ |
| 950 | ut_asserteq(true, device_active(normal)); |
| 951 | ut_asserteq(false, device_active(dma)); |
| 952 | ut_asserteq(true, device_active(vital)); |
| 953 | ut_asserteq(false, device_active(dma_vital)); |
| 954 | |
| 955 | /* Remove non-vital devices */ |
| 956 | ut_assertok(device_probe(dma)); |
| 957 | ut_assertok(device_probe(dma_vital)); |
| 958 | dm_remove_devices_flags(DM_REMOVE_NON_VITAL); |
| 959 | |
| 960 | /* This should have affected only non-vital devices */ |
| 961 | ut_asserteq(false, device_active(normal)); |
| 962 | ut_asserteq(false, device_active(dma)); |
| 963 | ut_asserteq(true, device_active(vital)); |
| 964 | ut_asserteq(true, device_active(dma_vital)); |
| 965 | |
| 966 | /* Remove vital devices via normal remove flag */ |
| 967 | ut_assertok(device_probe(normal)); |
| 968 | ut_assertok(device_probe(dma)); |
| 969 | dm_remove_devices_flags(DM_REMOVE_NORMAL); |
| 970 | |
| 971 | /* Check that all devices are inactive right now */ |
| 972 | ut_asserteq(false, device_active(normal)); |
| 973 | ut_asserteq(false, device_active(dma)); |
| 974 | ut_asserteq(false, device_active(vital)); |
| 975 | ut_asserteq(false, device_active(dma_vital)); |
| 976 | |
| 977 | return 0; |
| 978 | } |
| 979 | DM_TEST(dm_test_remove_vital, 0); |
| 980 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 981 | static int dm_test_uclass_before_ready(struct unit_test_state *uts) |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 982 | { |
| 983 | struct uclass *uc; |
| 984 | |
| 985 | ut_assertok(uclass_get(UCLASS_TEST, &uc)); |
| 986 | |
Simon Glass | 51a0eac | 2015-04-19 07:21:02 -0600 | [diff] [blame] | 987 | gd->dm_root = NULL; |
| 988 | gd->dm_root_f = NULL; |
| 989 | memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root)); |
| 990 | |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 991 | ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST)); |
| 992 | |
| 993 | return 0; |
| 994 | } |
Simon Glass | de70867 | 2014-07-23 06:55:15 -0600 | [diff] [blame] | 995 | DM_TEST(dm_test_uclass_before_ready, 0); |
Simon Glass | 98fd5d1 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 996 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 997 | static int dm_test_uclass_devices_find(struct unit_test_state *uts) |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 998 | { |
| 999 | struct udevice *dev; |
| 1000 | int ret; |
| 1001 | |
| 1002 | for (ret = uclass_find_first_device(UCLASS_TEST, &dev); |
| 1003 | dev; |
| 1004 | ret = uclass_find_next_device(&dev)) { |
| 1005 | ut_assert(!ret); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 1006 | ut_assertnonnull(dev); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
Simon Glass | 0bb4427 | 2019-09-25 08:55:55 -0600 | [diff] [blame] | 1009 | ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev)); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 1010 | ut_assertnull(dev); |
Marcel Ziswiler | 75ec16f | 2019-02-01 16:01:07 +0100 | [diff] [blame] | 1011 | |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1012 | return 0; |
| 1013 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1014 | DM_TEST(dm_test_uclass_devices_find, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1015 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1016 | static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts) |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 1017 | { |
| 1018 | struct udevice *finddev; |
| 1019 | struct udevice *testdev; |
| 1020 | int findret, ret; |
| 1021 | |
| 1022 | /* |
| 1023 | * For each test device found in fdt like: "a-test", "b-test", etc., |
| 1024 | * use its name and try to find it by uclass_find_device_by_name(). |
| 1025 | * Then, on success check if: |
| 1026 | * - current 'testdev' name is equal to the returned 'finddev' name |
| 1027 | * - current 'testdev' pointer is equal to the returned 'finddev' |
| 1028 | * |
| 1029 | * We assume that, each uclass's device name is unique, so if not, then |
| 1030 | * this will fail on checking condition: testdev == finddev, since the |
| 1031 | * uclass_find_device_by_name(), returns the first device by given name. |
| 1032 | */ |
| 1033 | for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev); |
| 1034 | testdev; |
| 1035 | ret = uclass_find_next_device(&testdev)) { |
| 1036 | ut_assertok(ret); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 1037 | ut_assertnonnull(testdev); |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 1038 | |
| 1039 | findret = uclass_find_device_by_name(UCLASS_TEST_FDT, |
| 1040 | testdev->name, |
| 1041 | &finddev); |
| 1042 | |
| 1043 | ut_assertok(findret); |
| 1044 | ut_assert(testdev); |
| 1045 | ut_asserteq_str(testdev->name, finddev->name); |
| 1046 | ut_asserteq_ptr(testdev, finddev); |
| 1047 | } |
| 1048 | |
| 1049 | return 0; |
| 1050 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1051 | DM_TEST(dm_test_uclass_devices_find_by_name, UT_TESTF_SCAN_FDT); |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 1052 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1053 | static int dm_test_uclass_devices_get(struct unit_test_state *uts) |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1054 | { |
| 1055 | struct udevice *dev; |
| 1056 | int ret; |
| 1057 | |
| 1058 | for (ret = uclass_first_device(UCLASS_TEST, &dev); |
| 1059 | dev; |
| 1060 | ret = uclass_next_device(&dev)) { |
| 1061 | ut_assert(!ret); |
| 1062 | ut_assert(dev); |
| 1063 | ut_assert(device_active(dev)); |
| 1064 | } |
| 1065 | |
| 1066 | return 0; |
| 1067 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1068 | DM_TEST(dm_test_uclass_devices_get, UT_TESTF_SCAN_PDATA); |
Przemyslaw Marczak | 1e0a7f2 | 2015-04-15 13:07:20 +0200 | [diff] [blame] | 1069 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1070 | static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts) |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 1071 | { |
| 1072 | struct udevice *finddev; |
| 1073 | struct udevice *testdev; |
| 1074 | int ret, findret; |
| 1075 | |
| 1076 | /* |
| 1077 | * For each test device found in fdt like: "a-test", "b-test", etc., |
| 1078 | * use its name and try to get it by uclass_get_device_by_name(). |
| 1079 | * On success check if: |
| 1080 | * - returned finddev' is active |
| 1081 | * - current 'testdev' name is equal to the returned 'finddev' name |
| 1082 | * - current 'testdev' pointer is equal to the returned 'finddev' |
| 1083 | * |
| 1084 | * We asserts that the 'testdev' is active on each loop entry, so we |
| 1085 | * could be sure that the 'finddev' is activated too, but for sure |
| 1086 | * we check it again. |
| 1087 | * |
| 1088 | * We assume that, each uclass's device name is unique, so if not, then |
| 1089 | * this will fail on checking condition: testdev == finddev, since the |
| 1090 | * uclass_get_device_by_name(), returns the first device by given name. |
| 1091 | */ |
| 1092 | for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev); |
| 1093 | testdev; |
| 1094 | ret = uclass_next_device(&testdev)) { |
| 1095 | ut_assertok(ret); |
| 1096 | ut_assert(testdev); |
| 1097 | ut_assert(device_active(testdev)); |
| 1098 | |
| 1099 | findret = uclass_get_device_by_name(UCLASS_TEST_FDT, |
| 1100 | testdev->name, |
| 1101 | &finddev); |
| 1102 | |
| 1103 | ut_assertok(findret); |
| 1104 | ut_assert(finddev); |
| 1105 | ut_assert(device_active(finddev)); |
| 1106 | ut_asserteq_str(testdev->name, finddev->name); |
| 1107 | ut_asserteq_ptr(testdev, finddev); |
| 1108 | } |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1112 | DM_TEST(dm_test_uclass_devices_get_by_name, UT_TESTF_SCAN_FDT); |
Przemyslaw Marczak | 2eff02f | 2015-04-20 13:32:33 +0200 | [diff] [blame] | 1113 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 1114 | static int dm_test_device_get_uclass_id(struct unit_test_state *uts) |
Simon Glass | 98fd5d1 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 1115 | { |
| 1116 | struct udevice *dev; |
| 1117 | |
| 1118 | ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev)); |
| 1119 | ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev)); |
| 1120 | |
| 1121 | return 0; |
| 1122 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1123 | DM_TEST(dm_test_device_get_uclass_id, UT_TESTF_SCAN_PDATA); |
Simon Glass | 70e35b4 | 2017-12-28 13:14:15 -0700 | [diff] [blame] | 1124 | |
| 1125 | static int dm_test_uclass_names(struct unit_test_state *uts) |
| 1126 | { |
| 1127 | ut_asserteq_str("test", uclass_get_name(UCLASS_TEST)); |
| 1128 | ut_asserteq(UCLASS_TEST, uclass_get_by_name("test")); |
| 1129 | |
| 1130 | return 0; |
| 1131 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1132 | DM_TEST(dm_test_uclass_names, UT_TESTF_SCAN_PDATA); |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 1133 | |
| 1134 | static int dm_test_inactive_child(struct unit_test_state *uts) |
| 1135 | { |
| 1136 | struct dm_test_state *dms = uts->priv; |
| 1137 | struct udevice *parent, *dev1, *dev2; |
| 1138 | |
| 1139 | /* Skip the behaviour in test_post_probe() */ |
| 1140 | dms->skip_post_probe = 1; |
| 1141 | |
| 1142 | ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent)); |
| 1143 | |
| 1144 | /* |
| 1145 | * Create a child but do not activate it. Calling the function again |
| 1146 | * should return the same child. |
| 1147 | */ |
| 1148 | ut_asserteq(-ENODEV, device_find_first_inactive_child(parent, |
| 1149 | UCLASS_TEST, &dev1)); |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 1150 | ut_assertok(device_bind(parent, DM_DRIVER_GET(test_drv), |
Simon Glass | 884870f | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 1151 | "test_child", 0, ofnode_null(), &dev1)); |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 1152 | |
| 1153 | ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST, |
| 1154 | &dev2)); |
| 1155 | ut_asserteq_ptr(dev1, dev2); |
| 1156 | |
| 1157 | ut_assertok(device_probe(dev1)); |
| 1158 | ut_asserteq(-ENODEV, device_find_first_inactive_child(parent, |
| 1159 | UCLASS_TEST, &dev2)); |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 1163 | DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA); |
Simon Glass | 6a109b3 | 2020-12-16 21:20:10 -0700 | [diff] [blame] | 1164 | |
| 1165 | /* Make sure all bound devices have a sequence number */ |
| 1166 | static int dm_test_all_have_seq(struct unit_test_state *uts) |
| 1167 | { |
| 1168 | struct udevice *dev; |
| 1169 | struct uclass *uc; |
| 1170 | |
Simon Glass | 784cd9e | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 1171 | list_for_each_entry(uc, gd->uclass_root, sibling_node) { |
Simon Glass | 6a109b3 | 2020-12-16 21:20:10 -0700 | [diff] [blame] | 1172 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 1173 | if (dev->seq_ == -1) |
Simon Glass | 6a109b3 | 2020-12-16 21:20:10 -0700 | [diff] [blame] | 1174 | printf("Device '%s' has no seq (%d)\n", |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 1175 | dev->name, dev->seq_); |
| 1176 | ut_assert(dev->seq_ != -1); |
Simon Glass | 6a109b3 | 2020-12-16 21:20:10 -0700 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | return 0; |
| 1181 | } |
| 1182 | DM_TEST(dm_test_all_have_seq, UT_TESTF_SCAN_PDATA); |
Nicolas Saenz Julienne | 892e9b4 | 2021-01-12 13:55:25 +0100 | [diff] [blame] | 1183 | |
| 1184 | static int dm_test_dma_offset(struct unit_test_state *uts) |
| 1185 | { |
| 1186 | struct udevice *dev; |
| 1187 | ofnode node; |
| 1188 | |
| 1189 | /* Make sure the bus's dma-ranges aren't taken into account here */ |
| 1190 | node = ofnode_path("/mmio-bus@0"); |
| 1191 | ut_assert(ofnode_valid(node)); |
| 1192 | ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev)); |
| 1193 | ut_asserteq_64(0, dev->dma_offset); |
| 1194 | |
| 1195 | /* Device behind a bus with dma-ranges */ |
| 1196 | node = ofnode_path("/mmio-bus@0/subnode@0"); |
| 1197 | ut_assert(ofnode_valid(node)); |
| 1198 | ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev)); |
| 1199 | ut_asserteq_64(-0x10000000ULL, dev->dma_offset); |
| 1200 | |
| 1201 | /* This one has no dma-ranges */ |
| 1202 | node = ofnode_path("/mmio-bus@1"); |
| 1203 | ut_assert(ofnode_valid(node)); |
| 1204 | ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev)); |
| 1205 | node = ofnode_path("/mmio-bus@1/subnode@0"); |
| 1206 | ut_assert(ofnode_valid(node)); |
| 1207 | ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev)); |
| 1208 | ut_asserteq_64(0, dev->dma_offset); |
| 1209 | |
| 1210 | return 0; |
| 1211 | } |
| 1212 | DM_TEST(dm_test_dma_offset, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |