blob: 60ddb1d6b1495a752c6d3a797fb7f62045cec324 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass40717422014-07-23 06:55:18 -06002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glass40717422014-07-23 06:55:18 -06004 */
5
6#include <common.h>
Simon Glass4e766c22018-10-01 21:12:32 -06007#ifdef CONFIG_SANDBOX
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass4e766c22018-10-01 21:12:32 -06009#include <os.h>
10#endif
Simon Glass40717422014-07-23 06:55:18 -060011#include <dm.h>
Lokesh Vutlafa45a082019-09-04 16:01:26 +053012#include <dm/device.h>
Simon Glass60d971b2014-07-23 06:55:20 -060013#include <dm/device-internal.h>
Simon Glass40717422014-07-23 06:55:18 -060014#include <dm/test.h>
Simon Glass11b61732015-01-25 08:27:01 -070015#include <dm/uclass-internal.h>
Simon Glass40717422014-07-23 06:55:18 -060016#include <dm/util.h>
Simon Glass75c4d412020-07-19 10:15:37 -060017#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050018#include <test/ut.h>
Simon Glass40717422014-07-23 06:55:18 -060019
20DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass71fa5b42020-12-03 16:55:18 -070022struct dm_test_parent_plat {
Simon Glass11b61732015-01-25 08:27:01 -070023 int count;
Simon Glassa4a51a02015-01-25 08:27:03 -070024 int bind_flag;
Simon Glassf4c9b3e2015-01-25 08:27:08 -070025 int uclass_bind_flag;
Simon Glass11b61732015-01-25 08:27:01 -070026};
27
Simon Glassd45560d2014-07-23 06:55:21 -060028enum {
29 FLAG_CHILD_PROBED = 10,
30 FLAG_CHILD_REMOVED = -7,
31};
32
33static struct dm_test_state *test_state;
34
Simon Glass40717422014-07-23 06:55:18 -060035static int testbus_drv_probe(struct udevice *dev)
36{
Simon Glass09128c52016-07-05 17:10:09 -060037 return dm_scan_fdt_dev(dev);
Simon Glass40717422014-07-23 06:55:18 -060038}
39
Simon Glassa4a51a02015-01-25 08:27:03 -070040static int testbus_child_post_bind(struct udevice *dev)
41{
Simon Glass71fa5b42020-12-03 16:55:18 -070042 struct dm_test_parent_plat *plat;
Simon Glassa4a51a02015-01-25 08:27:03 -070043
Simon Glass71fa5b42020-12-03 16:55:18 -070044 plat = dev_get_parent_plat(dev);
Simon Glassa4a51a02015-01-25 08:27:03 -070045 plat->bind_flag = 1;
Simon Glassf4c9b3e2015-01-25 08:27:08 -070046 plat->uclass_bind_flag = 2;
Simon Glassa4a51a02015-01-25 08:27:03 -070047
48 return 0;
49}
50
Simon Glassd45560d2014-07-23 06:55:21 -060051static int testbus_child_pre_probe(struct udevice *dev)
52{
Simon Glassde44acf2015-09-28 23:32:01 -060053 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
Simon Glassd45560d2014-07-23 06:55:21 -060054
55 parent_data->flag += FLAG_CHILD_PROBED;
56
57 return 0;
58}
59
Simon Glass5104b982015-01-25 08:27:10 -070060static int testbus_child_pre_probe_uclass(struct udevice *dev)
61{
62 struct dm_test_priv *priv = dev_get_priv(dev);
63
64 priv->uclass_flag++;
65
66 return 0;
67}
68
Bin Mengd9bad172018-10-15 02:20:58 -070069static int testbus_child_post_probe_uclass(struct udevice *dev)
70{
71 struct dm_test_priv *priv = dev_get_priv(dev);
72
73 priv->uclass_postp++;
74
75 return 0;
76}
77
Simon Glassd45560d2014-07-23 06:55:21 -060078static int testbus_child_post_remove(struct udevice *dev)
79{
Simon Glassde44acf2015-09-28 23:32:01 -060080 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
Simon Glassd45560d2014-07-23 06:55:21 -060081 struct dm_test_state *dms = test_state;
82
83 parent_data->flag += FLAG_CHILD_REMOVED;
84 if (dms)
85 dms->removed = dev;
86
87 return 0;
88}
89
Simon Glass40717422014-07-23 06:55:18 -060090static const struct udevice_id testbus_ids[] = {
91 {
92 .compatible = "denx,u-boot-test-bus",
93 .data = DM_TEST_TYPE_FIRST },
94 { }
95};
96
97U_BOOT_DRIVER(testbus_drv) = {
98 .name = "testbus_drv",
99 .of_match = testbus_ids,
100 .id = UCLASS_TEST_BUS,
101 .probe = testbus_drv_probe,
Simon Glassa4a51a02015-01-25 08:27:03 -0700102 .child_post_bind = testbus_child_post_bind,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700103 .priv_auto = sizeof(struct dm_test_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700104 .plat_auto = sizeof(struct dm_test_pdata),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700105 .per_child_auto = sizeof(struct dm_test_parent_data),
Simon Glass33b2efb2020-12-03 16:55:22 -0700106 .per_child_plat_auto = sizeof(struct dm_test_parent_plat),
Simon Glassd45560d2014-07-23 06:55:21 -0600107 .child_pre_probe = testbus_child_pre_probe,
108 .child_post_remove = testbus_child_post_remove,
Simon Glass40717422014-07-23 06:55:18 -0600109};
110
111UCLASS_DRIVER(testbus) = {
112 .name = "testbus",
113 .id = UCLASS_TEST_BUS,
Simon Glass0ccb0972015-01-25 08:27:05 -0700114 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass5104b982015-01-25 08:27:10 -0700115 .child_pre_probe = testbus_child_pre_probe_uclass,
Bin Mengd9bad172018-10-15 02:20:58 -0700116 .child_post_probe = testbus_child_post_probe_uclass,
Simon Glass40717422014-07-23 06:55:18 -0600117};
118
119/* Test that we can probe for children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500120static int dm_test_bus_children(struct unit_test_state *uts)
Simon Glass40717422014-07-23 06:55:18 -0600121{
Jean-Jacques Hiblot73873402020-09-11 13:43:35 +0530122 int num_devices = 9;
Simon Glass40717422014-07-23 06:55:18 -0600123 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 Hershberger3a77be52015-05-20 14:27:27 -0500136 ut_assert(!dm_check_devices(uts, num_devices));
Simon Glass40717422014-07-23 06:55:18 -0600137
138 return 0;
139}
Simon Glass974dccd2020-07-28 19:41:12 -0600140DM_TEST(dm_test_bus_children, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass48d4e292014-07-23 06:55:19 -0600141
142/* Test our functions for accessing children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500143static int dm_test_bus_children_funcs(struct unit_test_state *uts)
Simon Glass48d4e292014-07-23 06:55:19 -0600144{
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 */
Simon Glass07e13382020-12-16 21:20:29 -0700159 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, &dev));
160 ut_assertok(device_find_child_by_seq(bus, 0, &dev));
Simon Glass48d4e292014-07-23 06:55:19 -0600161 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
Simon Glass07e13382020-12-16 21:20:29 -0700162 ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev));
Simon Glass48d4e292014-07-23 06:55:19 -0600163 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
164 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
Simon Glass07e13382020-12-16 21:20:29 -0700165 ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev));
Simon Glass48d4e292014-07-23 06:55:19 -0600166
167 /* There is no device with sequence number 2 */
Simon Glass07e13382020-12-16 21:20:29 -0700168 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev));
169 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev));
Simon Glass48d4e292014-07-23 06:55:19 -0600170 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
171
172 /* Looking for something that is not a child */
173 node = fdt_path_offset(blob, "/junk");
174 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
175 node = fdt_path_offset(blob, "/d-test");
176 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
177
Simon Glass9b4057a2017-05-18 20:09:43 -0600178 return 0;
179}
Simon Glass974dccd2020-07-28 19:41:12 -0600180DM_TEST(dm_test_bus_children_funcs, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass9b4057a2017-05-18 20:09:43 -0600181
182static int dm_test_bus_children_of_offset(struct unit_test_state *uts)
183{
184 const void *blob = gd->fdt_blob;
185 struct udevice *bus, *dev;
186 int node;
187
188 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glassac855522017-06-07 10:28:44 -0600189 ut_assertnonnull(bus);
Simon Glass9b4057a2017-05-18 20:09:43 -0600190
Simon Glass48d4e292014-07-23 06:55:19 -0600191 /* Find a valid child */
192 node = fdt_path_offset(blob, "/some-bus/c-test@1");
Simon Glass9b4057a2017-05-18 20:09:43 -0600193 ut_assert(node > 0);
Simon Glass48d4e292014-07-23 06:55:19 -0600194 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
Simon Glassac855522017-06-07 10:28:44 -0600195 ut_assertnonnull(dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600196 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
197 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
Simon Glassac855522017-06-07 10:28:44 -0600198 ut_assertnonnull(dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600199 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
200
201 return 0;
202}
Simon Glass9b4057a2017-05-18 20:09:43 -0600203DM_TEST(dm_test_bus_children_of_offset,
Simon Glass974dccd2020-07-28 19:41:12 -0600204 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Simon Glass60d971b2014-07-23 06:55:20 -0600205
Simon Glass44da7352014-10-13 23:41:49 -0600206/* Test that we can iterate through children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500207static int dm_test_bus_children_iterators(struct unit_test_state *uts)
Simon Glass44da7352014-10-13 23:41:49 -0600208{
209 struct udevice *bus, *dev, *child;
210
211 /* Walk through the children one by one */
212 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
213 ut_assertok(device_find_first_child(bus, &dev));
214 ut_asserteq_str("c-test@5", dev->name);
215 ut_assertok(device_find_next_child(&dev));
216 ut_asserteq_str("c-test@0", dev->name);
217 ut_assertok(device_find_next_child(&dev));
218 ut_asserteq_str("c-test@1", dev->name);
219 ut_assertok(device_find_next_child(&dev));
220 ut_asserteq_ptr(dev, NULL);
221
222 /* Move to the next child without using device_find_first_child() */
Simon Glass07e13382020-12-16 21:20:29 -0700223 ut_assertok(device_find_child_by_seq(bus, 5, &dev));
Simon Glass44da7352014-10-13 23:41:49 -0600224 ut_asserteq_str("c-test@5", dev->name);
225 ut_assertok(device_find_next_child(&dev));
226 ut_asserteq_str("c-test@0", dev->name);
227
228 /* Try a device with no children */
229 ut_assertok(device_find_first_child(dev, &child));
230 ut_asserteq_ptr(child, NULL);
231
232 return 0;
233}
234DM_TEST(dm_test_bus_children_iterators,
Simon Glass974dccd2020-07-28 19:41:12 -0600235 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass44da7352014-10-13 23:41:49 -0600236
Simon Glass60d971b2014-07-23 06:55:20 -0600237/* Test that the bus can store data about each child */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500238static int test_bus_parent_data(struct unit_test_state *uts)
Simon Glass60d971b2014-07-23 06:55:20 -0600239{
240 struct dm_test_parent_data *parent_data;
241 struct udevice *bus, *dev;
242 struct uclass *uc;
243 int value;
244
245 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
246
247 /* Check that parent data is allocated */
Simon Glass07e13382020-12-16 21:20:29 -0700248 ut_assertok(device_find_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600249 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600250 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600251 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600252 ut_assert(NULL != parent_data);
253
254 /* Check that it starts at 0 and goes away when device is removed */
255 parent_data->sum += 5;
256 ut_asserteq(5, parent_data->sum);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100257 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassde44acf2015-09-28 23:32:01 -0600258 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600259
260 /* Check that we can do this twice */
261 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600262 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600263 ut_assert(NULL != parent_data);
264 parent_data->sum += 5;
265 ut_asserteq(5, parent_data->sum);
266
267 /* Add parent data to all children */
268 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
269 value = 5;
270 uclass_foreach_dev(dev, uc) {
271 /* Ignore these if they are not on this bus */
272 if (dev->parent != bus) {
Simon Glassde44acf2015-09-28 23:32:01 -0600273 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600274 continue;
275 }
276 ut_assertok(device_probe(dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600277 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600278
279 parent_data->sum = value;
280 value += 5;
281 }
282
283 /* Check it is still there */
284 value = 5;
285 uclass_foreach_dev(dev, uc) {
286 /* Ignore these if they are not on this bus */
287 if (dev->parent != bus)
288 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600289 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600290
291 ut_asserteq(value, parent_data->sum);
292 value += 5;
293 }
294
295 return 0;
296}
Simon Glassc23b4282015-01-25 08:27:06 -0700297/* Test that the bus can store data about each child */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500298static int dm_test_bus_parent_data(struct unit_test_state *uts)
Simon Glassc23b4282015-01-25 08:27:06 -0700299{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500300 return test_bus_parent_data(uts);
Simon Glassc23b4282015-01-25 08:27:06 -0700301}
Simon Glass974dccd2020-07-28 19:41:12 -0600302DM_TEST(dm_test_bus_parent_data, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd45560d2014-07-23 06:55:21 -0600303
Simon Glassc23b4282015-01-25 08:27:06 -0700304/* As above but the size is controlled by the uclass */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500305static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
Simon Glassc23b4282015-01-25 08:27:06 -0700306{
Simon Glass2410d882015-03-25 12:21:51 -0600307 struct driver *drv;
Simon Glassc23b4282015-01-25 08:27:06 -0700308 struct udevice *bus;
309 int size;
310 int ret;
311
312 /* Set the driver size to 0 so that the uclass size is used */
313 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glass2410d882015-03-25 12:21:51 -0600314 drv = (struct driver *)bus->driver;
Simon Glass8a2b47f2020-12-03 16:55:17 -0700315 size = drv->per_child_auto;
Simon Glass4e766c22018-10-01 21:12:32 -0600316
317#ifdef CONFIG_SANDBOX
318 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
319 os_mprotect_allow(drv, sizeof(*drv));
320#endif
Simon Glass8a2b47f2020-12-03 16:55:17 -0700321 bus->uclass->uc_drv->per_child_auto = size;
322 drv->per_child_auto = 0;
Joe Hershberger3a77be52015-05-20 14:27:27 -0500323 ret = test_bus_parent_data(uts);
Simon Glassc23b4282015-01-25 08:27:06 -0700324 if (ret)
325 return ret;
Simon Glass8a2b47f2020-12-03 16:55:17 -0700326 bus->uclass->uc_drv->per_child_auto = 0;
327 drv->per_child_auto = size;
Simon Glassc23b4282015-01-25 08:27:06 -0700328
329 return 0;
330}
331DM_TEST(dm_test_bus_parent_data_uclass,
Simon Glass974dccd2020-07-28 19:41:12 -0600332 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassc23b4282015-01-25 08:27:06 -0700333
Simon Glassd45560d2014-07-23 06:55:21 -0600334/* Test that the bus ops are called when a child is probed/removed */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500335static int dm_test_bus_parent_ops(struct unit_test_state *uts)
Simon Glassd45560d2014-07-23 06:55:21 -0600336{
337 struct dm_test_parent_data *parent_data;
Joe Hershberger3a77be52015-05-20 14:27:27 -0500338 struct dm_test_state *dms = uts->priv;
Simon Glassd45560d2014-07-23 06:55:21 -0600339 struct udevice *bus, *dev;
340 struct uclass *uc;
341
342 test_state = dms;
343 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
344 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
345
346 uclass_foreach_dev(dev, uc) {
347 /* Ignore these if they are not on this bus */
348 if (dev->parent != bus)
349 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600350 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glassd45560d2014-07-23 06:55:21 -0600351
352 ut_assertok(device_probe(dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600353 parent_data = dev_get_parent_priv(dev);
Simon Glassd45560d2014-07-23 06:55:21 -0600354 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
355 }
356
357 uclass_foreach_dev(dev, uc) {
358 /* Ignore these if they are not on this bus */
359 if (dev->parent != bus)
360 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600361 parent_data = dev_get_parent_priv(dev);
Simon Glassd45560d2014-07-23 06:55:21 -0600362 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100363 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassde44acf2015-09-28 23:32:01 -0600364 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glassd45560d2014-07-23 06:55:21 -0600365 ut_asserteq_ptr(dms->removed, dev);
366 }
367 test_state = NULL;
368
369 return 0;
370}
Simon Glass974dccd2020-07-28 19:41:12 -0600371DM_TEST(dm_test_bus_parent_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass11b61732015-01-25 08:27:01 -0700372
Simon Glass71fa5b42020-12-03 16:55:18 -0700373static int test_bus_parent_plat(struct unit_test_state *uts)
Simon Glass11b61732015-01-25 08:27:01 -0700374{
Simon Glass71fa5b42020-12-03 16:55:18 -0700375 struct dm_test_parent_plat *plat;
Simon Glass11b61732015-01-25 08:27:01 -0700376 struct udevice *bus, *dev;
Simon Glass11b61732015-01-25 08:27:01 -0700377
378 /* Check that the bus has no children */
379 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
380 device_find_first_child(bus, &dev);
381 ut_asserteq_ptr(NULL, dev);
382
383 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
384
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530385 for (device_find_first_child(bus, &dev);
Simon Glass11b61732015-01-25 08:27:01 -0700386 dev;
387 device_find_next_child(&dev)) {
388 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700389 plat = dev_get_parent_plat(dev);
Simon Glass11b61732015-01-25 08:27:01 -0700390 ut_assert(plat != NULL);
391
392 /*
393 * Check that it is not affected by the device being
394 * probed/removed
395 */
396 plat->count++;
397 ut_asserteq(1, plat->count);
398 device_probe(dev);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100399 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass11b61732015-01-25 08:27:01 -0700400
Simon Glass71fa5b42020-12-03 16:55:18 -0700401 ut_asserteq_ptr(plat, dev_get_parent_plat(dev));
Simon Glass11b61732015-01-25 08:27:01 -0700402 ut_asserteq(1, plat->count);
403 ut_assertok(device_probe(dev));
Simon Glass11b61732015-01-25 08:27:01 -0700404 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530405 ut_asserteq(3, device_get_child_count(bus));
Simon Glass11b61732015-01-25 08:27:01 -0700406
407 /* Removing the bus should also have no effect (it is still bound) */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100408 device_remove(bus, DM_REMOVE_NORMAL);
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530409 for (device_find_first_child(bus, &dev);
Simon Glass11b61732015-01-25 08:27:01 -0700410 dev;
411 device_find_next_child(&dev)) {
412 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700413 plat = dev_get_parent_plat(dev);
Simon Glass11b61732015-01-25 08:27:01 -0700414 ut_assert(plat != NULL);
415 ut_asserteq(1, plat->count);
Simon Glass11b61732015-01-25 08:27:01 -0700416 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530417 ut_asserteq(3, device_get_child_count(bus));
Simon Glass11b61732015-01-25 08:27:01 -0700418
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
Simon Glass71fa5b42020-12-03 16:55:18 -0700426 /* Now the child plat should be removed and re-added */
Simon Glass11b61732015-01-25 08:27:01 -0700427 device_probe(bus);
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530428 for (device_find_first_child(bus, &dev);
Simon Glass11b61732015-01-25 08:27:01 -0700429 dev;
430 device_find_next_child(&dev)) {
431 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700432 plat = dev_get_parent_plat(dev);
Simon Glass11b61732015-01-25 08:27:01 -0700433 ut_assert(plat != NULL);
434 ut_asserteq(0, plat->count);
Simon Glass11b61732015-01-25 08:27:01 -0700435 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530436 ut_asserteq(3, device_get_child_count(bus));
Simon Glass11b61732015-01-25 08:27:01 -0700437
438 return 0;
439}
Simon Glass57f95402015-01-25 08:27:02 -0700440
441/* Test that the bus can store platform data about each child */
Simon Glass71fa5b42020-12-03 16:55:18 -0700442static int dm_test_bus_parent_plat(struct unit_test_state *uts)
Simon Glass57f95402015-01-25 08:27:02 -0700443{
Simon Glass71fa5b42020-12-03 16:55:18 -0700444 return test_bus_parent_plat(uts);
Simon Glass57f95402015-01-25 08:27:02 -0700445}
Simon Glass71fa5b42020-12-03 16:55:18 -0700446DM_TEST(dm_test_bus_parent_plat, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass57f95402015-01-25 08:27:02 -0700447
448/* As above but the size is controlled by the uclass */
Simon Glass71fa5b42020-12-03 16:55:18 -0700449static int dm_test_bus_parent_plat_uclass(struct unit_test_state *uts)
Simon Glass57f95402015-01-25 08:27:02 -0700450{
451 struct udevice *bus;
Simon Glass2410d882015-03-25 12:21:51 -0600452 struct driver *drv;
Simon Glass57f95402015-01-25 08:27:02 -0700453 int size;
454 int ret;
455
456 /* Set the driver size to 0 so that the uclass size is used */
457 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glass2410d882015-03-25 12:21:51 -0600458 drv = (struct driver *)bus->driver;
Simon Glass71fa5b42020-12-03 16:55:18 -0700459 size = drv->per_child_plat_auto;
Simon Glass4e766c22018-10-01 21:12:32 -0600460#ifdef CONFIG_SANDBOX
461 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
462 os_mprotect_allow(drv, sizeof(*drv));
463#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700464 bus->uclass->uc_drv->per_child_plat_auto = size;
465 drv->per_child_plat_auto = 0;
466 ret = test_bus_parent_plat(uts);
Simon Glass57f95402015-01-25 08:27:02 -0700467 if (ret)
468 return ret;
Simon Glass71fa5b42020-12-03 16:55:18 -0700469 bus->uclass->uc_drv->per_child_plat_auto = 0;
470 drv->per_child_plat_auto = size;
Simon Glass57f95402015-01-25 08:27:02 -0700471
472 return 0;
473}
Simon Glass71fa5b42020-12-03 16:55:18 -0700474DM_TEST(dm_test_bus_parent_plat_uclass,
Simon Glass974dccd2020-07-28 19:41:12 -0600475 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa4a51a02015-01-25 08:27:03 -0700476
477/* Test that the child post_bind method is called */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500478static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
Simon Glassa4a51a02015-01-25 08:27:03 -0700479{
Simon Glass71fa5b42020-12-03 16:55:18 -0700480 struct dm_test_parent_plat *plat;
Simon Glassa4a51a02015-01-25 08:27:03 -0700481 struct udevice *bus, *dev;
Simon Glassa4a51a02015-01-25 08:27:03 -0700482
483 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530484 for (device_find_first_child(bus, &dev);
Simon Glassa4a51a02015-01-25 08:27:03 -0700485 dev;
486 device_find_next_child(&dev)) {
487 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700488 plat = dev_get_parent_plat(dev);
Simon Glassa4a51a02015-01-25 08:27:03 -0700489 ut_assert(plat != NULL);
490 ut_asserteq(1, plat->bind_flag);
Simon Glassa4a51a02015-01-25 08:27:03 -0700491 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530492 ut_asserteq(3, device_get_child_count(bus));
Simon Glassa4a51a02015-01-25 08:27:03 -0700493
494 return 0;
495}
Simon Glass974dccd2020-07-28 19:41:12 -0600496DM_TEST(dm_test_bus_child_post_bind, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700497
498/* Test that the child post_bind method is called */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500499static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700500{
Simon Glass71fa5b42020-12-03 16:55:18 -0700501 struct dm_test_parent_plat *plat;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700502 struct udevice *bus, *dev;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700503
504 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530505 for (device_find_first_child(bus, &dev);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700506 dev;
507 device_find_next_child(&dev)) {
508 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700509 plat = dev_get_parent_plat(dev);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700510 ut_assert(plat != NULL);
511 ut_asserteq(2, plat->uclass_bind_flag);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700512 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530513 ut_asserteq(3, device_get_child_count(bus));
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700514
515 return 0;
516}
517DM_TEST(dm_test_bus_child_post_bind_uclass,
Simon Glass974dccd2020-07-28 19:41:12 -0600518 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass5104b982015-01-25 08:27:10 -0700519
520/*
521 * Test that the bus' uclass' child_pre_probe() is called before the
522 * device's probe() method
523 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500524static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
Simon Glass5104b982015-01-25 08:27:10 -0700525{
526 struct udevice *bus, *dev;
Simon Glass5104b982015-01-25 08:27:10 -0700527
528 /*
529 * See testfdt_drv_probe() which effectively checks that the uclass
530 * flag is set before that method is called
531 */
532 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530533 for (device_find_first_child(bus, &dev);
Simon Glass5104b982015-01-25 08:27:10 -0700534 dev;
535 device_find_next_child(&dev)) {
536 struct dm_test_priv *priv = dev_get_priv(dev);
537
538 /* Check that things happened in the right order */
539 ut_asserteq_ptr(NULL, priv);
540 ut_assertok(device_probe(dev));
541
542 priv = dev_get_priv(dev);
543 ut_assert(priv != NULL);
544 ut_asserteq(1, priv->uclass_flag);
545 ut_asserteq(1, priv->uclass_total);
Simon Glass5104b982015-01-25 08:27:10 -0700546 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530547 ut_asserteq(3, device_get_child_count(bus));
Simon Glass5104b982015-01-25 08:27:10 -0700548
549 return 0;
550}
551DM_TEST(dm_test_bus_child_pre_probe_uclass,
Simon Glass974dccd2020-07-28 19:41:12 -0600552 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Bin Mengd9bad172018-10-15 02:20:58 -0700553
554/*
555 * Test that the bus' uclass' child_post_probe() is called after the
556 * device's probe() method
557 */
558static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts)
559{
560 struct udevice *bus, *dev;
Bin Mengd9bad172018-10-15 02:20:58 -0700561
562 /*
563 * See testfdt_drv_probe() which effectively initializes that
564 * the uclass postp flag is set to a value
565 */
566 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530567 for (device_find_first_child(bus, &dev);
Bin Mengd9bad172018-10-15 02:20:58 -0700568 dev;
569 device_find_next_child(&dev)) {
570 struct dm_test_priv *priv = dev_get_priv(dev);
571
572 /* Check that things happened in the right order */
573 ut_asserteq_ptr(NULL, priv);
574 ut_assertok(device_probe(dev));
575
576 priv = dev_get_priv(dev);
577 ut_assert(priv != NULL);
578 ut_asserteq(0, priv->uclass_postp);
Bin Mengd9bad172018-10-15 02:20:58 -0700579 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530580 ut_asserteq(3, device_get_child_count(bus));
Bin Mengd9bad172018-10-15 02:20:58 -0700581
582 return 0;
583}
584DM_TEST(dm_test_bus_child_post_probe_uclass,
Simon Glass974dccd2020-07-28 19:41:12 -0600585 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);