Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 9f8037e | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 7 | #ifdef CONFIG_SANDBOX |
| 8 | #include <os.h> |
| 9 | #endif |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 11 | #include <dm/device.h> |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 12 | #include <dm/device-internal.h> |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 13 | #include <dm/test.h> |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 14 | #include <dm/uclass-internal.h> |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 15 | #include <dm/util.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 16 | #include <test/ut.h> |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 20 | struct dm_test_parent_platdata { |
| 21 | int count; |
Simon Glass | 0118ce7 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 22 | int bind_flag; |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 23 | int uclass_bind_flag; |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 24 | }; |
| 25 | |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 26 | enum { |
| 27 | FLAG_CHILD_PROBED = 10, |
| 28 | FLAG_CHILD_REMOVED = -7, |
| 29 | }; |
| 30 | |
| 31 | static struct dm_test_state *test_state; |
| 32 | |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 33 | static int testbus_drv_probe(struct udevice *dev) |
| 34 | { |
Simon Glass | 2e3f1ff | 2016-07-05 17:10:09 -0600 | [diff] [blame] | 35 | return dm_scan_fdt_dev(dev); |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 36 | } |
| 37 | |
Simon Glass | 0118ce7 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 38 | static int testbus_child_post_bind(struct udevice *dev) |
| 39 | { |
| 40 | struct dm_test_parent_platdata *plat; |
| 41 | |
| 42 | plat = dev_get_parent_platdata(dev); |
| 43 | plat->bind_flag = 1; |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 44 | plat->uclass_bind_flag = 2; |
Simon Glass | 0118ce7 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 49 | static int testbus_child_pre_probe(struct udevice *dev) |
| 50 | { |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 51 | struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev); |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 52 | |
| 53 | parent_data->flag += FLAG_CHILD_PROBED; |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 58 | static int testbus_child_pre_probe_uclass(struct udevice *dev) |
| 59 | { |
| 60 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 61 | |
| 62 | priv->uclass_flag++; |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Bin Meng | d92878a | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 67 | static int testbus_child_post_probe_uclass(struct udevice *dev) |
| 68 | { |
| 69 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 70 | |
| 71 | priv->uclass_postp++; |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 76 | static int testbus_child_post_remove(struct udevice *dev) |
| 77 | { |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 78 | struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev); |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 79 | struct dm_test_state *dms = test_state; |
| 80 | |
| 81 | parent_data->flag += FLAG_CHILD_REMOVED; |
| 82 | if (dms) |
| 83 | dms->removed = dev; |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 88 | static const struct udevice_id testbus_ids[] = { |
| 89 | { |
| 90 | .compatible = "denx,u-boot-test-bus", |
| 91 | .data = DM_TEST_TYPE_FIRST }, |
| 92 | { } |
| 93 | }; |
| 94 | |
| 95 | U_BOOT_DRIVER(testbus_drv) = { |
| 96 | .name = "testbus_drv", |
| 97 | .of_match = testbus_ids, |
| 98 | .id = UCLASS_TEST_BUS, |
| 99 | .probe = testbus_drv_probe, |
Simon Glass | 0118ce7 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 100 | .child_post_bind = testbus_child_post_bind, |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 101 | .priv_auto_alloc_size = sizeof(struct dm_test_priv), |
| 102 | .platdata_auto_alloc_size = sizeof(struct dm_test_pdata), |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 103 | .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data), |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 104 | .per_child_platdata_auto_alloc_size = |
| 105 | sizeof(struct dm_test_parent_platdata), |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 106 | .child_pre_probe = testbus_child_pre_probe, |
| 107 | .child_post_remove = testbus_child_post_remove, |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | UCLASS_DRIVER(testbus) = { |
| 111 | .name = "testbus", |
| 112 | .id = UCLASS_TEST_BUS, |
Simon Glass | 9cc36a2 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 113 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 114 | .child_pre_probe = testbus_child_pre_probe_uclass, |
Bin Meng | d92878a | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 115 | .child_post_probe = testbus_child_post_probe_uclass, |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | /* Test that we can probe for children */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 119 | static int dm_test_bus_children(struct unit_test_state *uts) |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 120 | { |
Bin Meng | 2786cd7 | 2018-10-10 22:07:01 -0700 | [diff] [blame] | 121 | int num_devices = 8; |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 122 | struct udevice *bus; |
| 123 | struct uclass *uc; |
| 124 | |
| 125 | ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc)); |
| 126 | ut_asserteq(num_devices, list_count_items(&uc->dev_head)); |
| 127 | |
| 128 | /* Probe the bus, which should yield 3 more devices */ |
| 129 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 130 | num_devices += 3; |
| 131 | |
| 132 | ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc)); |
| 133 | ut_asserteq(num_devices, list_count_items(&uc->dev_head)); |
| 134 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 135 | ut_assert(!dm_check_devices(uts, num_devices)); |
Simon Glass | 1ca7e20 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 997c87b | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 140 | |
| 141 | /* Test our functions for accessing children */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 142 | static int dm_test_bus_children_funcs(struct unit_test_state *uts) |
Simon Glass | 997c87b | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 143 | { |
| 144 | const void *blob = gd->fdt_blob; |
| 145 | struct udevice *bus, *dev; |
| 146 | int node; |
| 147 | |
| 148 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 149 | |
| 150 | /* device_get_child() */ |
| 151 | ut_assertok(device_get_child(bus, 0, &dev)); |
| 152 | ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev)); |
| 153 | ut_assertok(device_get_child_by_seq(bus, 5, &dev)); |
| 154 | ut_assert(dev->flags & DM_FLAG_ACTIVATED); |
| 155 | ut_asserteq_str("c-test@5", dev->name); |
| 156 | |
| 157 | /* Device with sequence number 0 should be accessible */ |
| 158 | ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev)); |
| 159 | ut_assertok(device_find_child_by_seq(bus, 0, true, &dev)); |
| 160 | ut_assert(!(dev->flags & DM_FLAG_ACTIVATED)); |
| 161 | ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev)); |
| 162 | ut_assertok(device_get_child_by_seq(bus, 0, &dev)); |
| 163 | ut_assert(dev->flags & DM_FLAG_ACTIVATED); |
| 164 | |
| 165 | /* There is no device with sequence number 2 */ |
| 166 | ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev)); |
| 167 | ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev)); |
| 168 | ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev)); |
| 169 | |
| 170 | /* Looking for something that is not a child */ |
| 171 | node = fdt_path_offset(blob, "/junk"); |
| 172 | ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev)); |
| 173 | node = fdt_path_offset(blob, "/d-test"); |
| 174 | ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev)); |
| 175 | |
Simon Glass | 298afb5 | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 179 | |
| 180 | static int dm_test_bus_children_of_offset(struct unit_test_state *uts) |
| 181 | { |
| 182 | const void *blob = gd->fdt_blob; |
| 183 | struct udevice *bus, *dev; |
| 184 | int node; |
| 185 | |
| 186 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
Simon Glass | 4f414d3 | 2017-06-07 10:28:44 -0600 | [diff] [blame] | 187 | ut_assertnonnull(bus); |
Simon Glass | 298afb5 | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 188 | |
Simon Glass | 997c87b | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 189 | /* Find a valid child */ |
| 190 | node = fdt_path_offset(blob, "/some-bus/c-test@1"); |
Simon Glass | 298afb5 | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 191 | ut_assert(node > 0); |
Simon Glass | 997c87b | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 192 | ut_assertok(device_find_child_by_of_offset(bus, node, &dev)); |
Simon Glass | 4f414d3 | 2017-06-07 10:28:44 -0600 | [diff] [blame] | 193 | ut_assertnonnull(dev); |
Simon Glass | 997c87b | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 194 | ut_assert(!(dev->flags & DM_FLAG_ACTIVATED)); |
| 195 | ut_assertok(device_get_child_by_of_offset(bus, node, &dev)); |
Simon Glass | 4f414d3 | 2017-06-07 10:28:44 -0600 | [diff] [blame] | 196 | ut_assertnonnull(dev); |
Simon Glass | 997c87b | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 197 | ut_assert(dev->flags & DM_FLAG_ACTIVATED); |
| 198 | |
| 199 | return 0; |
| 200 | } |
Simon Glass | 298afb5 | 2017-05-18 20:09:43 -0600 | [diff] [blame] | 201 | DM_TEST(dm_test_bus_children_of_offset, |
| 202 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE); |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 203 | |
Simon Glass | a8981d4 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 204 | /* Test that we can iterate through children */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 205 | static int dm_test_bus_children_iterators(struct unit_test_state *uts) |
Simon Glass | a8981d4 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 206 | { |
| 207 | struct udevice *bus, *dev, *child; |
| 208 | |
| 209 | /* Walk through the children one by one */ |
| 210 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 211 | ut_assertok(device_find_first_child(bus, &dev)); |
| 212 | ut_asserteq_str("c-test@5", dev->name); |
| 213 | ut_assertok(device_find_next_child(&dev)); |
| 214 | ut_asserteq_str("c-test@0", dev->name); |
| 215 | ut_assertok(device_find_next_child(&dev)); |
| 216 | ut_asserteq_str("c-test@1", dev->name); |
| 217 | ut_assertok(device_find_next_child(&dev)); |
| 218 | ut_asserteq_ptr(dev, NULL); |
| 219 | |
| 220 | /* Move to the next child without using device_find_first_child() */ |
| 221 | ut_assertok(device_find_child_by_seq(bus, 5, true, &dev)); |
| 222 | ut_asserteq_str("c-test@5", dev->name); |
| 223 | ut_assertok(device_find_next_child(&dev)); |
| 224 | ut_asserteq_str("c-test@0", dev->name); |
| 225 | |
| 226 | /* Try a device with no children */ |
| 227 | ut_assertok(device_find_first_child(dev, &child)); |
| 228 | ut_asserteq_ptr(child, NULL); |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | DM_TEST(dm_test_bus_children_iterators, |
| 233 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 234 | |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 235 | /* Test that the bus can store data about each child */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 236 | static int test_bus_parent_data(struct unit_test_state *uts) |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 237 | { |
| 238 | struct dm_test_parent_data *parent_data; |
| 239 | struct udevice *bus, *dev; |
| 240 | struct uclass *uc; |
| 241 | int value; |
| 242 | |
| 243 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 244 | |
| 245 | /* Check that parent data is allocated */ |
| 246 | ut_assertok(device_find_child_by_seq(bus, 0, true, &dev)); |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 247 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 248 | ut_assertok(device_get_child_by_seq(bus, 0, &dev)); |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 249 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 250 | ut_assert(NULL != parent_data); |
| 251 | |
| 252 | /* Check that it starts at 0 and goes away when device is removed */ |
| 253 | parent_data->sum += 5; |
| 254 | ut_asserteq(5, parent_data->sum); |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 255 | device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 256 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 257 | |
| 258 | /* Check that we can do this twice */ |
| 259 | ut_assertok(device_get_child_by_seq(bus, 0, &dev)); |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 260 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 261 | ut_assert(NULL != parent_data); |
| 262 | parent_data->sum += 5; |
| 263 | ut_asserteq(5, parent_data->sum); |
| 264 | |
| 265 | /* Add parent data to all children */ |
| 266 | ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc)); |
| 267 | value = 5; |
| 268 | uclass_foreach_dev(dev, uc) { |
| 269 | /* Ignore these if they are not on this bus */ |
| 270 | if (dev->parent != bus) { |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 271 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 272 | continue; |
| 273 | } |
| 274 | ut_assertok(device_probe(dev)); |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 275 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 276 | |
| 277 | parent_data->sum = value; |
| 278 | value += 5; |
| 279 | } |
| 280 | |
| 281 | /* Check it is still there */ |
| 282 | value = 5; |
| 283 | uclass_foreach_dev(dev, uc) { |
| 284 | /* Ignore these if they are not on this bus */ |
| 285 | if (dev->parent != bus) |
| 286 | continue; |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 287 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 288 | |
| 289 | ut_asserteq(value, parent_data->sum); |
| 290 | value += 5; |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | } |
Simon Glass | dac8db2 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 295 | /* Test that the bus can store data about each child */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 296 | static int dm_test_bus_parent_data(struct unit_test_state *uts) |
Simon Glass | dac8db2 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 297 | { |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 298 | return test_bus_parent_data(uts); |
Simon Glass | dac8db2 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 299 | } |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 300 | DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 301 | |
Simon Glass | dac8db2 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 302 | /* As above but the size is controlled by the uclass */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 303 | static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts) |
Simon Glass | dac8db2 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 304 | { |
Simon Glass | e23eb61 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 305 | struct driver *drv; |
Simon Glass | dac8db2 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 306 | struct udevice *bus; |
| 307 | int size; |
| 308 | int ret; |
| 309 | |
| 310 | /* Set the driver size to 0 so that the uclass size is used */ |
| 311 | ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus)); |
Simon Glass | e23eb61 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 312 | drv = (struct driver *)bus->driver; |
| 313 | size = drv->per_child_auto_alloc_size; |
Simon Glass | 9f8037e | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 314 | |
| 315 | #ifdef CONFIG_SANDBOX |
| 316 | os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv)); |
| 317 | os_mprotect_allow(drv, sizeof(*drv)); |
| 318 | #endif |
Simon Glass | dac8db2 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 319 | bus->uclass->uc_drv->per_child_auto_alloc_size = size; |
Simon Glass | e23eb61 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 320 | drv->per_child_auto_alloc_size = 0; |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 321 | ret = test_bus_parent_data(uts); |
Simon Glass | dac8db2 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 322 | if (ret) |
| 323 | return ret; |
| 324 | bus->uclass->uc_drv->per_child_auto_alloc_size = 0; |
Simon Glass | e23eb61 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 325 | drv->per_child_auto_alloc_size = size; |
Simon Glass | dac8db2 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | DM_TEST(dm_test_bus_parent_data_uclass, |
| 330 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 331 | |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 332 | /* Test that the bus ops are called when a child is probed/removed */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 333 | static int dm_test_bus_parent_ops(struct unit_test_state *uts) |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 334 | { |
| 335 | struct dm_test_parent_data *parent_data; |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 336 | struct dm_test_state *dms = uts->priv; |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 337 | struct udevice *bus, *dev; |
| 338 | struct uclass *uc; |
| 339 | |
| 340 | test_state = dms; |
| 341 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 342 | ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc)); |
| 343 | |
| 344 | uclass_foreach_dev(dev, uc) { |
| 345 | /* Ignore these if they are not on this bus */ |
| 346 | if (dev->parent != bus) |
| 347 | continue; |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 348 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 349 | |
| 350 | ut_assertok(device_probe(dev)); |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 351 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 352 | ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag); |
| 353 | } |
| 354 | |
| 355 | uclass_foreach_dev(dev, uc) { |
| 356 | /* Ignore these if they are not on this bus */ |
| 357 | if (dev->parent != bus) |
| 358 | continue; |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 359 | parent_data = dev_get_parent_priv(dev); |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 360 | ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag); |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 361 | ut_assertok(device_remove(dev, DM_REMOVE_NORMAL)); |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 362 | ut_asserteq_ptr(NULL, dev_get_parent_priv(dev)); |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 363 | ut_asserteq_ptr(dms->removed, dev); |
| 364 | } |
| 365 | test_state = NULL; |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 370 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 371 | static int test_bus_parent_platdata(struct unit_test_state *uts) |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 372 | { |
| 373 | struct dm_test_parent_platdata *plat; |
| 374 | struct udevice *bus, *dev; |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 375 | |
| 376 | /* Check that the bus has no children */ |
| 377 | ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus)); |
| 378 | device_find_first_child(bus, &dev); |
| 379 | ut_asserteq_ptr(NULL, dev); |
| 380 | |
| 381 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
| 382 | |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 383 | for (device_find_first_child(bus, &dev); |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 384 | dev; |
| 385 | device_find_next_child(&dev)) { |
| 386 | /* Check that platform data is allocated */ |
| 387 | plat = dev_get_parent_platdata(dev); |
| 388 | ut_assert(plat != NULL); |
| 389 | |
| 390 | /* |
| 391 | * Check that it is not affected by the device being |
| 392 | * probed/removed |
| 393 | */ |
| 394 | plat->count++; |
| 395 | ut_asserteq(1, plat->count); |
| 396 | device_probe(dev); |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 397 | device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 398 | |
| 399 | ut_asserteq_ptr(plat, dev_get_parent_platdata(dev)); |
| 400 | ut_asserteq(1, plat->count); |
| 401 | ut_assertok(device_probe(dev)); |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 402 | } |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 403 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 404 | |
| 405 | /* Removing the bus should also have no effect (it is still bound) */ |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 406 | device_remove(bus, DM_REMOVE_NORMAL); |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 407 | for (device_find_first_child(bus, &dev); |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 408 | dev; |
| 409 | device_find_next_child(&dev)) { |
| 410 | /* Check that platform data is allocated */ |
| 411 | plat = dev_get_parent_platdata(dev); |
| 412 | ut_assert(plat != NULL); |
| 413 | ut_asserteq(1, plat->count); |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 414 | } |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 415 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 416 | |
| 417 | /* Unbind all the children */ |
| 418 | do { |
| 419 | device_find_first_child(bus, &dev); |
| 420 | if (dev) |
| 421 | device_unbind(dev); |
| 422 | } while (dev); |
| 423 | |
| 424 | /* Now the child platdata should be removed and re-added */ |
| 425 | device_probe(bus); |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 426 | for (device_find_first_child(bus, &dev); |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 427 | dev; |
| 428 | device_find_next_child(&dev)) { |
| 429 | /* Check that platform data is allocated */ |
| 430 | plat = dev_get_parent_platdata(dev); |
| 431 | ut_assert(plat != NULL); |
| 432 | ut_asserteq(0, plat->count); |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 433 | } |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 434 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 435 | |
| 436 | return 0; |
| 437 | } |
Simon Glass | ba8da9d | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 438 | |
| 439 | /* Test that the bus can store platform data about each child */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 440 | static int dm_test_bus_parent_platdata(struct unit_test_state *uts) |
Simon Glass | ba8da9d | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 441 | { |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 442 | return test_bus_parent_platdata(uts); |
Simon Glass | ba8da9d | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 443 | } |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 444 | DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | ba8da9d | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 445 | |
| 446 | /* As above but the size is controlled by the uclass */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 447 | static int dm_test_bus_parent_platdata_uclass(struct unit_test_state *uts) |
Simon Glass | ba8da9d | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 448 | { |
| 449 | struct udevice *bus; |
Simon Glass | e23eb61 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 450 | struct driver *drv; |
Simon Glass | ba8da9d | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 451 | int size; |
| 452 | int ret; |
| 453 | |
| 454 | /* Set the driver size to 0 so that the uclass size is used */ |
| 455 | ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus)); |
Simon Glass | e23eb61 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 456 | drv = (struct driver *)bus->driver; |
| 457 | size = drv->per_child_platdata_auto_alloc_size; |
Simon Glass | 9f8037e | 2018-10-01 21:12:32 -0600 | [diff] [blame] | 458 | #ifdef CONFIG_SANDBOX |
| 459 | os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv)); |
| 460 | os_mprotect_allow(drv, sizeof(*drv)); |
| 461 | #endif |
Simon Glass | ba8da9d | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 462 | bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size; |
Simon Glass | e23eb61 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 463 | drv->per_child_platdata_auto_alloc_size = 0; |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 464 | ret = test_bus_parent_platdata(uts); |
Simon Glass | ba8da9d | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 465 | if (ret) |
| 466 | return ret; |
| 467 | bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0; |
Simon Glass | e23eb61 | 2015-03-25 12:21:51 -0600 | [diff] [blame] | 468 | drv->per_child_platdata_auto_alloc_size = size; |
Simon Glass | ba8da9d | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | DM_TEST(dm_test_bus_parent_platdata_uclass, |
| 473 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 0118ce7 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 474 | |
| 475 | /* Test that the child post_bind method is called */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 476 | static int dm_test_bus_child_post_bind(struct unit_test_state *uts) |
Simon Glass | 0118ce7 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 477 | { |
| 478 | struct dm_test_parent_platdata *plat; |
| 479 | struct udevice *bus, *dev; |
Simon Glass | 0118ce7 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 480 | |
| 481 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 482 | for (device_find_first_child(bus, &dev); |
Simon Glass | 0118ce7 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 483 | dev; |
| 484 | device_find_next_child(&dev)) { |
| 485 | /* Check that platform data is allocated */ |
| 486 | plat = dev_get_parent_platdata(dev); |
| 487 | ut_assert(plat != NULL); |
| 488 | ut_asserteq(1, plat->bind_flag); |
Simon Glass | 0118ce7 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 489 | } |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 490 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | 0118ce7 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 495 | |
| 496 | /* Test that the child post_bind method is called */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 497 | static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts) |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 498 | { |
| 499 | struct dm_test_parent_platdata *plat; |
| 500 | struct udevice *bus, *dev; |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 501 | |
| 502 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 503 | for (device_find_first_child(bus, &dev); |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 504 | dev; |
| 505 | device_find_next_child(&dev)) { |
| 506 | /* Check that platform data is allocated */ |
| 507 | plat = dev_get_parent_platdata(dev); |
| 508 | ut_assert(plat != NULL); |
| 509 | ut_asserteq(2, plat->uclass_bind_flag); |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 510 | } |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 511 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | 081f2fc | 2015-01-25 08:27:08 -0700 | [diff] [blame] | 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | DM_TEST(dm_test_bus_child_post_bind_uclass, |
| 516 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | * Test that the bus' uclass' child_pre_probe() is called before the |
| 520 | * device's probe() method |
| 521 | */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 522 | static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts) |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 523 | { |
| 524 | struct udevice *bus, *dev; |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 525 | |
| 526 | /* |
| 527 | * See testfdt_drv_probe() which effectively checks that the uclass |
| 528 | * flag is set before that method is called |
| 529 | */ |
| 530 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 531 | for (device_find_first_child(bus, &dev); |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 532 | dev; |
| 533 | device_find_next_child(&dev)) { |
| 534 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 535 | |
| 536 | /* Check that things happened in the right order */ |
| 537 | ut_asserteq_ptr(NULL, priv); |
| 538 | ut_assertok(device_probe(dev)); |
| 539 | |
| 540 | priv = dev_get_priv(dev); |
| 541 | ut_assert(priv != NULL); |
| 542 | ut_asserteq(1, priv->uclass_flag); |
| 543 | ut_asserteq(1, priv->uclass_total); |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 544 | } |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 545 | ut_asserteq(3, device_get_child_count(bus)); |
Simon Glass | 83c7e43 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | DM_TEST(dm_test_bus_child_pre_probe_uclass, |
| 550 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Bin Meng | d92878a | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 551 | |
| 552 | /* |
| 553 | * Test that the bus' uclass' child_post_probe() is called after the |
| 554 | * device's probe() method |
| 555 | */ |
| 556 | static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts) |
| 557 | { |
| 558 | struct udevice *bus, *dev; |
Bin Meng | d92878a | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 559 | |
| 560 | /* |
| 561 | * See testfdt_drv_probe() which effectively initializes that |
| 562 | * the uclass postp flag is set to a value |
| 563 | */ |
| 564 | ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 565 | for (device_find_first_child(bus, &dev); |
Bin Meng | d92878a | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 566 | dev; |
| 567 | device_find_next_child(&dev)) { |
| 568 | struct dm_test_priv *priv = dev_get_priv(dev); |
| 569 | |
| 570 | /* Check that things happened in the right order */ |
| 571 | ut_asserteq_ptr(NULL, priv); |
| 572 | ut_assertok(device_probe(dev)); |
| 573 | |
| 574 | priv = dev_get_priv(dev); |
| 575 | ut_assert(priv != NULL); |
| 576 | ut_asserteq(0, priv->uclass_postp); |
Bin Meng | d92878a | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 577 | } |
Lokesh Vutla | 240b932 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 578 | ut_asserteq(3, device_get_child_count(bus)); |
Bin Meng | d92878a | 2018-10-15 02:20:58 -0700 | [diff] [blame] | 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | DM_TEST(dm_test_bus_child_post_probe_uclass, |
| 583 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |