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