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