blob: a338c7f567c1183a622d9125d6be7ca3ff938561 [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
Simon Glass4e766c22018-10-01 21:12:32 -06006#ifdef CONFIG_SANDBOX
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Simon Glass4e766c22018-10-01 21:12:32 -06008#include <os.h>
9#endif
Simon Glass40717422014-07-23 06:55:18 -060010#include <dm.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.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 Glass40717422014-07-23 06:55:18 -060022/* Test that we can probe for children */
Joe Hershberger3a77be52015-05-20 14:27:27 -050023static int dm_test_bus_children(struct unit_test_state *uts)
Simon Glass40717422014-07-23 06:55:18 -060024{
Jean-Jacques Hiblot73873402020-09-11 13:43:35 +053025 int num_devices = 9;
Simon Glass40717422014-07-23 06:55:18 -060026 struct udevice *bus;
27 struct uclass *uc;
28
29 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
30 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
31
32 /* Probe the bus, which should yield 3 more devices */
33 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
34 num_devices += 3;
35
36 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
37 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
38
Joe Hershberger3a77be52015-05-20 14:27:27 -050039 ut_assert(!dm_check_devices(uts, num_devices));
Simon Glass40717422014-07-23 06:55:18 -060040
41 return 0;
42}
Simon Glass974dccd2020-07-28 19:41:12 -060043DM_TEST(dm_test_bus_children, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass48d4e292014-07-23 06:55:19 -060044
45/* Test our functions for accessing children */
Joe Hershberger3a77be52015-05-20 14:27:27 -050046static int dm_test_bus_children_funcs(struct unit_test_state *uts)
Simon Glass48d4e292014-07-23 06:55:19 -060047{
48 const void *blob = gd->fdt_blob;
49 struct udevice *bus, *dev;
50 int node;
51
52 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
53
54 /* device_get_child() */
55 ut_assertok(device_get_child(bus, 0, &dev));
56 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
57 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
Simon Glass6211d762020-12-19 10:40:10 -070058 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass48d4e292014-07-23 06:55:19 -060059 ut_asserteq_str("c-test@5", dev->name);
60
61 /* Device with sequence number 0 should be accessible */
Simon Glass07e13382020-12-16 21:20:29 -070062 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, &dev));
63 ut_assertok(device_find_child_by_seq(bus, 0, &dev));
Simon Glass6211d762020-12-19 10:40:10 -070064 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
Simon Glass07e13382020-12-16 21:20:29 -070065 ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev));
Simon Glass48d4e292014-07-23 06:55:19 -060066 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glass6211d762020-12-19 10:40:10 -070067 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass07e13382020-12-16 21:20:29 -070068 ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev));
Simon Glass48d4e292014-07-23 06:55:19 -060069
70 /* There is no device with sequence number 2 */
Simon Glass07e13382020-12-16 21:20:29 -070071 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev));
72 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev));
Simon Glass48d4e292014-07-23 06:55:19 -060073 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
74
75 /* Looking for something that is not a child */
76 node = fdt_path_offset(blob, "/junk");
77 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
78 node = fdt_path_offset(blob, "/d-test");
79 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
80
Simon Glass9b4057a2017-05-18 20:09:43 -060081 return 0;
82}
Simon Glass974dccd2020-07-28 19:41:12 -060083DM_TEST(dm_test_bus_children_funcs, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass9b4057a2017-05-18 20:09:43 -060084
85static int dm_test_bus_children_of_offset(struct unit_test_state *uts)
86{
87 const void *blob = gd->fdt_blob;
88 struct udevice *bus, *dev;
89 int node;
90
91 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glassac855522017-06-07 10:28:44 -060092 ut_assertnonnull(bus);
Simon Glass9b4057a2017-05-18 20:09:43 -060093
Simon Glass48d4e292014-07-23 06:55:19 -060094 /* Find a valid child */
95 node = fdt_path_offset(blob, "/some-bus/c-test@1");
Simon Glass9b4057a2017-05-18 20:09:43 -060096 ut_assert(node > 0);
Simon Glass48d4e292014-07-23 06:55:19 -060097 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
Simon Glassac855522017-06-07 10:28:44 -060098 ut_assertnonnull(dev);
Simon Glass6211d762020-12-19 10:40:10 -070099 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
Simon Glass48d4e292014-07-23 06:55:19 -0600100 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
Simon Glassac855522017-06-07 10:28:44 -0600101 ut_assertnonnull(dev);
Simon Glass6211d762020-12-19 10:40:10 -0700102 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass48d4e292014-07-23 06:55:19 -0600103
104 return 0;
105}
Simon Glass9b4057a2017-05-18 20:09:43 -0600106DM_TEST(dm_test_bus_children_of_offset,
Simon Glass974dccd2020-07-28 19:41:12 -0600107 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Simon Glass60d971b2014-07-23 06:55:20 -0600108
Simon Glass44da7352014-10-13 23:41:49 -0600109/* Test that we can iterate through children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500110static int dm_test_bus_children_iterators(struct unit_test_state *uts)
Simon Glass44da7352014-10-13 23:41:49 -0600111{
112 struct udevice *bus, *dev, *child;
113
114 /* Walk through the children one by one */
115 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
116 ut_assertok(device_find_first_child(bus, &dev));
117 ut_asserteq_str("c-test@5", dev->name);
118 ut_assertok(device_find_next_child(&dev));
119 ut_asserteq_str("c-test@0", dev->name);
120 ut_assertok(device_find_next_child(&dev));
121 ut_asserteq_str("c-test@1", dev->name);
122 ut_assertok(device_find_next_child(&dev));
123 ut_asserteq_ptr(dev, NULL);
124
125 /* Move to the next child without using device_find_first_child() */
Simon Glass07e13382020-12-16 21:20:29 -0700126 ut_assertok(device_find_child_by_seq(bus, 5, &dev));
Simon Glass44da7352014-10-13 23:41:49 -0600127 ut_asserteq_str("c-test@5", dev->name);
128 ut_assertok(device_find_next_child(&dev));
129 ut_asserteq_str("c-test@0", dev->name);
130
131 /* Try a device with no children */
132 ut_assertok(device_find_first_child(dev, &child));
133 ut_asserteq_ptr(child, NULL);
134
135 return 0;
136}
137DM_TEST(dm_test_bus_children_iterators,
Simon Glass974dccd2020-07-28 19:41:12 -0600138 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass44da7352014-10-13 23:41:49 -0600139
Simon Glass60d971b2014-07-23 06:55:20 -0600140/* Test that the bus can store data about each child */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500141static int test_bus_parent_data(struct unit_test_state *uts)
Simon Glass60d971b2014-07-23 06:55:20 -0600142{
143 struct dm_test_parent_data *parent_data;
144 struct udevice *bus, *dev;
145 struct uclass *uc;
146 int value;
147
148 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
149
150 /* Check that parent data is allocated */
Simon Glass07e13382020-12-16 21:20:29 -0700151 ut_assertok(device_find_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600152 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600153 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600154 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600155 ut_assert(NULL != parent_data);
156
157 /* Check that it starts at 0 and goes away when device is removed */
158 parent_data->sum += 5;
159 ut_asserteq(5, parent_data->sum);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100160 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassde44acf2015-09-28 23:32:01 -0600161 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600162
163 /* Check that we can do this twice */
164 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600165 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600166 ut_assert(NULL != parent_data);
167 parent_data->sum += 5;
168 ut_asserteq(5, parent_data->sum);
169
170 /* Add parent data to all children */
171 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
172 value = 5;
173 uclass_foreach_dev(dev, uc) {
174 /* Ignore these if they are not on this bus */
175 if (dev->parent != bus) {
Simon Glassde44acf2015-09-28 23:32:01 -0600176 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600177 continue;
178 }
179 ut_assertok(device_probe(dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600180 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600181
182 parent_data->sum = value;
183 value += 5;
184 }
185
186 /* Check it is still there */
187 value = 5;
188 uclass_foreach_dev(dev, uc) {
189 /* Ignore these if they are not on this bus */
190 if (dev->parent != bus)
191 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600192 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600193
194 ut_asserteq(value, parent_data->sum);
195 value += 5;
196 }
197
198 return 0;
199}
Simon Glassc23b4282015-01-25 08:27:06 -0700200/* Test that the bus can store data about each child */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500201static int dm_test_bus_parent_data(struct unit_test_state *uts)
Simon Glassc23b4282015-01-25 08:27:06 -0700202{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500203 return test_bus_parent_data(uts);
Simon Glassc23b4282015-01-25 08:27:06 -0700204}
Simon Glass974dccd2020-07-28 19:41:12 -0600205DM_TEST(dm_test_bus_parent_data, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd45560d2014-07-23 06:55:21 -0600206
Simon Glassc23b4282015-01-25 08:27:06 -0700207/* As above but the size is controlled by the uclass */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500208static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
Simon Glassc23b4282015-01-25 08:27:06 -0700209{
Simon Glass2410d882015-03-25 12:21:51 -0600210 struct driver *drv;
Simon Glassc23b4282015-01-25 08:27:06 -0700211 struct udevice *bus;
212 int size;
213 int ret;
214
215 /* Set the driver size to 0 so that the uclass size is used */
216 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glass2410d882015-03-25 12:21:51 -0600217 drv = (struct driver *)bus->driver;
Simon Glass8a2b47f2020-12-03 16:55:17 -0700218 size = drv->per_child_auto;
Simon Glass4e766c22018-10-01 21:12:32 -0600219
220#ifdef CONFIG_SANDBOX
221 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
222 os_mprotect_allow(drv, sizeof(*drv));
223#endif
Simon Glass8a2b47f2020-12-03 16:55:17 -0700224 bus->uclass->uc_drv->per_child_auto = size;
225 drv->per_child_auto = 0;
Joe Hershberger3a77be52015-05-20 14:27:27 -0500226 ret = test_bus_parent_data(uts);
Simon Glassc23b4282015-01-25 08:27:06 -0700227 if (ret)
228 return ret;
Simon Glass8a2b47f2020-12-03 16:55:17 -0700229 bus->uclass->uc_drv->per_child_auto = 0;
230 drv->per_child_auto = size;
Simon Glassc23b4282015-01-25 08:27:06 -0700231
232 return 0;
233}
234DM_TEST(dm_test_bus_parent_data_uclass,
Simon Glass974dccd2020-07-28 19:41:12 -0600235 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassc23b4282015-01-25 08:27:06 -0700236
Simon Glassd45560d2014-07-23 06:55:21 -0600237/* Test that the bus ops are called when a child is probed/removed */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500238static int dm_test_bus_parent_ops(struct unit_test_state *uts)
Simon Glassd45560d2014-07-23 06:55:21 -0600239{
240 struct dm_test_parent_data *parent_data;
241 struct udevice *bus, *dev;
242 struct uclass *uc;
243
Simon Glass4bf89722020-12-23 08:11:18 -0700244 testbus_get_clear_removed();
Simon Glassd45560d2014-07-23 06:55:21 -0600245 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
246 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
247
248 uclass_foreach_dev(dev, uc) {
249 /* Ignore these if they are not on this bus */
250 if (dev->parent != bus)
251 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600252 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glassd45560d2014-07-23 06:55:21 -0600253
254 ut_assertok(device_probe(dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600255 parent_data = dev_get_parent_priv(dev);
Simon Glass4bf89722020-12-23 08:11:18 -0700256 ut_asserteq(TEST_FLAG_CHILD_PROBED, parent_data->flag);
Simon Glassd45560d2014-07-23 06:55:21 -0600257 }
258
259 uclass_foreach_dev(dev, uc) {
260 /* Ignore these if they are not on this bus */
261 if (dev->parent != bus)
262 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600263 parent_data = dev_get_parent_priv(dev);
Simon Glass4bf89722020-12-23 08:11:18 -0700264 ut_asserteq(TEST_FLAG_CHILD_PROBED, parent_data->flag);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100265 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassde44acf2015-09-28 23:32:01 -0600266 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass4bf89722020-12-23 08:11:18 -0700267 ut_asserteq_ptr(testbus_get_clear_removed(), dev);
Simon Glassd45560d2014-07-23 06:55:21 -0600268 }
Simon Glassd45560d2014-07-23 06:55:21 -0600269
270 return 0;
271}
Simon Glass974dccd2020-07-28 19:41:12 -0600272DM_TEST(dm_test_bus_parent_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass11b61732015-01-25 08:27:01 -0700273
Simon Glass71fa5b42020-12-03 16:55:18 -0700274static int test_bus_parent_plat(struct unit_test_state *uts)
Simon Glass11b61732015-01-25 08:27:01 -0700275{
Simon Glass71fa5b42020-12-03 16:55:18 -0700276 struct dm_test_parent_plat *plat;
Simon Glass11b61732015-01-25 08:27:01 -0700277 struct udevice *bus, *dev;
Simon Glass11b61732015-01-25 08:27:01 -0700278
279 /* Check that the bus has no children */
280 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
281 device_find_first_child(bus, &dev);
282 ut_asserteq_ptr(NULL, dev);
283
284 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
285
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530286 for (device_find_first_child(bus, &dev);
Simon Glass11b61732015-01-25 08:27:01 -0700287 dev;
288 device_find_next_child(&dev)) {
289 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700290 plat = dev_get_parent_plat(dev);
Simon Glass11b61732015-01-25 08:27:01 -0700291 ut_assert(plat != NULL);
292
293 /*
294 * Check that it is not affected by the device being
295 * probed/removed
296 */
297 plat->count++;
298 ut_asserteq(1, plat->count);
299 device_probe(dev);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100300 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass11b61732015-01-25 08:27:01 -0700301
Simon Glass71fa5b42020-12-03 16:55:18 -0700302 ut_asserteq_ptr(plat, dev_get_parent_plat(dev));
Simon Glass11b61732015-01-25 08:27:01 -0700303 ut_asserteq(1, plat->count);
304 ut_assertok(device_probe(dev));
Simon Glass11b61732015-01-25 08:27:01 -0700305 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530306 ut_asserteq(3, device_get_child_count(bus));
Simon Glass11b61732015-01-25 08:27:01 -0700307
308 /* Removing the bus should also have no effect (it is still bound) */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100309 device_remove(bus, DM_REMOVE_NORMAL);
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530310 for (device_find_first_child(bus, &dev);
Simon Glass11b61732015-01-25 08:27:01 -0700311 dev;
312 device_find_next_child(&dev)) {
313 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700314 plat = dev_get_parent_plat(dev);
Simon Glass11b61732015-01-25 08:27:01 -0700315 ut_assert(plat != NULL);
316 ut_asserteq(1, plat->count);
Simon Glass11b61732015-01-25 08:27:01 -0700317 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530318 ut_asserteq(3, device_get_child_count(bus));
Simon Glass11b61732015-01-25 08:27:01 -0700319
320 /* Unbind all the children */
321 do {
322 device_find_first_child(bus, &dev);
323 if (dev)
324 device_unbind(dev);
325 } while (dev);
326
Simon Glass71fa5b42020-12-03 16:55:18 -0700327 /* Now the child plat should be removed and re-added */
Simon Glass11b61732015-01-25 08:27:01 -0700328 device_probe(bus);
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530329 for (device_find_first_child(bus, &dev);
Simon Glass11b61732015-01-25 08:27:01 -0700330 dev;
331 device_find_next_child(&dev)) {
332 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700333 plat = dev_get_parent_plat(dev);
Simon Glass11b61732015-01-25 08:27:01 -0700334 ut_assert(plat != NULL);
335 ut_asserteq(0, plat->count);
Simon Glass11b61732015-01-25 08:27:01 -0700336 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530337 ut_asserteq(3, device_get_child_count(bus));
Simon Glass11b61732015-01-25 08:27:01 -0700338
339 return 0;
340}
Simon Glass57f95402015-01-25 08:27:02 -0700341
342/* Test that the bus can store platform data about each child */
Simon Glass71fa5b42020-12-03 16:55:18 -0700343static int dm_test_bus_parent_plat(struct unit_test_state *uts)
Simon Glass57f95402015-01-25 08:27:02 -0700344{
Simon Glass71fa5b42020-12-03 16:55:18 -0700345 return test_bus_parent_plat(uts);
Simon Glass57f95402015-01-25 08:27:02 -0700346}
Simon Glass71fa5b42020-12-03 16:55:18 -0700347DM_TEST(dm_test_bus_parent_plat, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass57f95402015-01-25 08:27:02 -0700348
349/* As above but the size is controlled by the uclass */
Simon Glass71fa5b42020-12-03 16:55:18 -0700350static int dm_test_bus_parent_plat_uclass(struct unit_test_state *uts)
Simon Glass57f95402015-01-25 08:27:02 -0700351{
352 struct udevice *bus;
Simon Glass2410d882015-03-25 12:21:51 -0600353 struct driver *drv;
Simon Glass57f95402015-01-25 08:27:02 -0700354 int size;
355 int ret;
356
357 /* Set the driver size to 0 so that the uclass size is used */
358 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glass2410d882015-03-25 12:21:51 -0600359 drv = (struct driver *)bus->driver;
Simon Glass71fa5b42020-12-03 16:55:18 -0700360 size = drv->per_child_plat_auto;
Simon Glass4e766c22018-10-01 21:12:32 -0600361#ifdef CONFIG_SANDBOX
362 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
363 os_mprotect_allow(drv, sizeof(*drv));
364#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700365 bus->uclass->uc_drv->per_child_plat_auto = size;
366 drv->per_child_plat_auto = 0;
367 ret = test_bus_parent_plat(uts);
Simon Glass57f95402015-01-25 08:27:02 -0700368 if (ret)
369 return ret;
Simon Glass71fa5b42020-12-03 16:55:18 -0700370 bus->uclass->uc_drv->per_child_plat_auto = 0;
371 drv->per_child_plat_auto = size;
Simon Glass57f95402015-01-25 08:27:02 -0700372
373 return 0;
374}
Simon Glass71fa5b42020-12-03 16:55:18 -0700375DM_TEST(dm_test_bus_parent_plat_uclass,
Simon Glass974dccd2020-07-28 19:41:12 -0600376 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa4a51a02015-01-25 08:27:03 -0700377
378/* Test that the child post_bind method is called */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500379static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
Simon Glassa4a51a02015-01-25 08:27:03 -0700380{
Simon Glass71fa5b42020-12-03 16:55:18 -0700381 struct dm_test_parent_plat *plat;
Simon Glassa4a51a02015-01-25 08:27:03 -0700382 struct udevice *bus, *dev;
Simon Glassa4a51a02015-01-25 08:27:03 -0700383
384 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530385 for (device_find_first_child(bus, &dev);
Simon Glassa4a51a02015-01-25 08:27:03 -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 Glassa4a51a02015-01-25 08:27:03 -0700390 ut_assert(plat != NULL);
391 ut_asserteq(1, plat->bind_flag);
Simon Glassa4a51a02015-01-25 08:27:03 -0700392 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530393 ut_asserteq(3, device_get_child_count(bus));
Simon Glassa4a51a02015-01-25 08:27:03 -0700394
395 return 0;
396}
Simon Glass974dccd2020-07-28 19:41:12 -0600397DM_TEST(dm_test_bus_child_post_bind, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700398
399/* Test that the child post_bind method is called */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500400static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700401{
Simon Glass71fa5b42020-12-03 16:55:18 -0700402 struct dm_test_parent_plat *plat;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700403 struct udevice *bus, *dev;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700404
405 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530406 for (device_find_first_child(bus, &dev);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700407 dev;
408 device_find_next_child(&dev)) {
409 /* Check that platform data is allocated */
Simon Glass71fa5b42020-12-03 16:55:18 -0700410 plat = dev_get_parent_plat(dev);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700411 ut_assert(plat != NULL);
412 ut_asserteq(2, plat->uclass_bind_flag);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700413 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530414 ut_asserteq(3, device_get_child_count(bus));
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700415
416 return 0;
417}
418DM_TEST(dm_test_bus_child_post_bind_uclass,
Simon Glass974dccd2020-07-28 19:41:12 -0600419 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass5104b982015-01-25 08:27:10 -0700420
421/*
422 * Test that the bus' uclass' child_pre_probe() is called before the
423 * device's probe() method
424 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500425static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
Simon Glass5104b982015-01-25 08:27:10 -0700426{
427 struct udevice *bus, *dev;
Simon Glass5104b982015-01-25 08:27:10 -0700428
429 /*
430 * See testfdt_drv_probe() which effectively checks that the uclass
431 * flag is set before that method is called
432 */
433 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530434 for (device_find_first_child(bus, &dev);
Simon Glass5104b982015-01-25 08:27:10 -0700435 dev;
436 device_find_next_child(&dev)) {
437 struct dm_test_priv *priv = dev_get_priv(dev);
438
439 /* Check that things happened in the right order */
440 ut_asserteq_ptr(NULL, priv);
441 ut_assertok(device_probe(dev));
442
443 priv = dev_get_priv(dev);
444 ut_assert(priv != NULL);
445 ut_asserteq(1, priv->uclass_flag);
446 ut_asserteq(1, priv->uclass_total);
Simon Glass5104b982015-01-25 08:27:10 -0700447 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530448 ut_asserteq(3, device_get_child_count(bus));
Simon Glass5104b982015-01-25 08:27:10 -0700449
450 return 0;
451}
452DM_TEST(dm_test_bus_child_pre_probe_uclass,
Simon Glass974dccd2020-07-28 19:41:12 -0600453 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Bin Mengd9bad172018-10-15 02:20:58 -0700454
455/*
456 * Test that the bus' uclass' child_post_probe() is called after the
457 * device's probe() method
458 */
459static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts)
460{
461 struct udevice *bus, *dev;
Bin Mengd9bad172018-10-15 02:20:58 -0700462
463 /*
464 * See testfdt_drv_probe() which effectively initializes that
465 * the uclass postp flag is set to a value
466 */
467 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530468 for (device_find_first_child(bus, &dev);
Bin Mengd9bad172018-10-15 02:20:58 -0700469 dev;
470 device_find_next_child(&dev)) {
471 struct dm_test_priv *priv = dev_get_priv(dev);
472
473 /* Check that things happened in the right order */
474 ut_asserteq_ptr(NULL, priv);
475 ut_assertok(device_probe(dev));
476
477 priv = dev_get_priv(dev);
478 ut_assert(priv != NULL);
479 ut_asserteq(0, priv->uclass_postp);
Bin Mengd9bad172018-10-15 02:20:58 -0700480 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530481 ut_asserteq(3, device_get_child_count(bus));
Bin Mengd9bad172018-10-15 02:20:58 -0700482
483 return 0;
484}
485DM_TEST(dm_test_bus_child_post_probe_uclass,
Simon Glass974dccd2020-07-28 19:41:12 -0600486 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);