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