blob: 89a6aa6554cbbfd9d0f571dcd659b3ed9644ff14 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass1ca7e202014-07-23 06:55:18 -06002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glass1ca7e202014-07-23 06:55:18 -06004 */
5
6#include <common.h>
Simon Glass9f8037e2018-10-01 21:12:32 -06007#ifdef CONFIG_SANDBOX
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Simon Glass9f8037e2018-10-01 21:12:32 -06009#include <os.h>
10#endif
Simon Glass1ca7e202014-07-23 06:55:18 -060011#include <dm.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Lokesh Vutla240b9322019-09-04 16:01:26 +053013#include <dm/device.h>
Simon Glasse59f4582014-07-23 06:55:20 -060014#include <dm/device-internal.h>
Simon Glass1ca7e202014-07-23 06:55:18 -060015#include <dm/test.h>
Simon Glasscdc133b2015-01-25 08:27:01 -070016#include <dm/uclass-internal.h>
Simon Glass1ca7e202014-07-23 06:55:18 -060017#include <dm/util.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060018#include <test/test.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050019#include <test/ut.h>
Simon Glass1ca7e202014-07-23 06:55:18 -060020
21DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass1ca7e202014-07-23 06:55:18 -060023/* Test that we can probe for children */
Joe Hershbergere721b882015-05-20 14:27:27 -050024static int dm_test_bus_children(struct unit_test_state *uts)
Simon Glass1ca7e202014-07-23 06:55:18 -060025{
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +053026 int num_devices = 9;
Simon Glass1ca7e202014-07-23 06:55:18 -060027 struct udevice *bus;
28 struct uclass *uc;
29
30 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
31 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
32
33 /* Probe the bus, which should yield 3 more devices */
34 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
35 num_devices += 3;
36
37 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
38 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
39
Joe Hershbergere721b882015-05-20 14:27:27 -050040 ut_assert(!dm_check_devices(uts, num_devices));
Simon Glass1ca7e202014-07-23 06:55:18 -060041
42 return 0;
43}
Simon Glasse180c2b2020-07-28 19:41:12 -060044DM_TEST(dm_test_bus_children, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass997c87b2014-07-23 06:55:19 -060045
46/* Test our functions for accessing children */
Joe Hershbergere721b882015-05-20 14:27:27 -050047static int dm_test_bus_children_funcs(struct unit_test_state *uts)
Simon Glass997c87b2014-07-23 06:55:19 -060048{
49 const void *blob = gd->fdt_blob;
50 struct udevice *bus, *dev;
51 int node;
52
53 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
54
55 /* device_get_child() */
56 ut_assertok(device_get_child(bus, 0, &dev));
57 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
58 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
Simon Glass73466df2020-12-19 10:40:10 -070059 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass997c87b2014-07-23 06:55:19 -060060 ut_asserteq_str("c-test@5", dev->name);
61
62 /* Device with sequence number 0 should be accessible */
Simon Glass99175912020-12-16 21:20:29 -070063 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, &dev));
64 ut_assertok(device_find_child_by_seq(bus, 0, &dev));
Simon Glass73466df2020-12-19 10:40:10 -070065 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
Simon Glass99175912020-12-16 21:20:29 -070066 ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev));
Simon Glass997c87b2014-07-23 06:55:19 -060067 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glass73466df2020-12-19 10:40:10 -070068 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass99175912020-12-16 21:20:29 -070069 ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev));
Simon Glass997c87b2014-07-23 06:55:19 -060070
71 /* There is no device with sequence number 2 */
Simon Glass99175912020-12-16 21:20:29 -070072 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev));
73 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev));
Simon Glass997c87b2014-07-23 06:55:19 -060074 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
75
76 /* Looking for something that is not a child */
77 node = fdt_path_offset(blob, "/junk");
78 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
79 node = fdt_path_offset(blob, "/d-test");
80 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
81
Simon Glass298afb52017-05-18 20:09:43 -060082 return 0;
83}
Simon Glasse180c2b2020-07-28 19:41:12 -060084DM_TEST(dm_test_bus_children_funcs, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass298afb52017-05-18 20:09:43 -060085
86static int dm_test_bus_children_of_offset(struct unit_test_state *uts)
87{
88 const void *blob = gd->fdt_blob;
89 struct udevice *bus, *dev;
90 int node;
91
92 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glass4f414d32017-06-07 10:28:44 -060093 ut_assertnonnull(bus);
Simon Glass298afb52017-05-18 20:09:43 -060094
Simon Glass997c87b2014-07-23 06:55:19 -060095 /* Find a valid child */
96 node = fdt_path_offset(blob, "/some-bus/c-test@1");
Simon Glass298afb52017-05-18 20:09:43 -060097 ut_assert(node > 0);
Simon Glass997c87b2014-07-23 06:55:19 -060098 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
Simon Glass4f414d32017-06-07 10:28:44 -060099 ut_assertnonnull(dev);
Simon Glass73466df2020-12-19 10:40:10 -0700100 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
Simon Glass997c87b2014-07-23 06:55:19 -0600101 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
Simon Glass4f414d32017-06-07 10:28:44 -0600102 ut_assertnonnull(dev);
Simon Glass73466df2020-12-19 10:40:10 -0700103 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass997c87b2014-07-23 06:55:19 -0600104
105 return 0;
106}
Simon Glass298afb52017-05-18 20:09:43 -0600107DM_TEST(dm_test_bus_children_of_offset,
Simon Glasse180c2b2020-07-28 19:41:12 -0600108 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Simon Glasse59f4582014-07-23 06:55:20 -0600109
Simon Glassa8981d42014-10-13 23:41:49 -0600110/* Test that we can iterate through children */
Joe Hershbergere721b882015-05-20 14:27:27 -0500111static int dm_test_bus_children_iterators(struct unit_test_state *uts)
Simon Glassa8981d42014-10-13 23:41:49 -0600112{
113 struct udevice *bus, *dev, *child;
114
115 /* Walk through the children one by one */
116 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
117 ut_assertok(device_find_first_child(bus, &dev));
118 ut_asserteq_str("c-test@5", dev->name);
119 ut_assertok(device_find_next_child(&dev));
120 ut_asserteq_str("c-test@0", dev->name);
121 ut_assertok(device_find_next_child(&dev));
122 ut_asserteq_str("c-test@1", dev->name);
123 ut_assertok(device_find_next_child(&dev));
124 ut_asserteq_ptr(dev, NULL);
125
126 /* Move to the next child without using device_find_first_child() */
Simon Glass99175912020-12-16 21:20:29 -0700127 ut_assertok(device_find_child_by_seq(bus, 5, &dev));
Simon Glassa8981d42014-10-13 23:41:49 -0600128 ut_asserteq_str("c-test@5", dev->name);
129 ut_assertok(device_find_next_child(&dev));
130 ut_asserteq_str("c-test@0", dev->name);
131
132 /* Try a device with no children */
133 ut_assertok(device_find_first_child(dev, &child));
134 ut_asserteq_ptr(child, NULL);
135
136 return 0;
137}
138DM_TEST(dm_test_bus_children_iterators,
Simon Glasse180c2b2020-07-28 19:41:12 -0600139 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa8981d42014-10-13 23:41:49 -0600140
Simon Glasse59f4582014-07-23 06:55:20 -0600141/* Test that the bus can store data about each child */
Joe Hershbergere721b882015-05-20 14:27:27 -0500142static int test_bus_parent_data(struct unit_test_state *uts)
Simon Glasse59f4582014-07-23 06:55:20 -0600143{
144 struct dm_test_parent_data *parent_data;
145 struct udevice *bus, *dev;
146 struct uclass *uc;
147 int value;
148
149 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
150
151 /* Check that parent data is allocated */
Simon Glass99175912020-12-16 21:20:29 -0700152 ut_assertok(device_find_child_by_seq(bus, 0, &dev));
Simon Glassbcbe3d12015-09-28 23:32:01 -0600153 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glasse59f4582014-07-23 06:55:20 -0600154 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glassbcbe3d12015-09-28 23:32:01 -0600155 parent_data = dev_get_parent_priv(dev);
Simon Glasse59f4582014-07-23 06:55:20 -0600156 ut_assert(NULL != parent_data);
157
158 /* Check that it starts at 0 and goes away when device is removed */
159 parent_data->sum += 5;
160 ut_asserteq(5, parent_data->sum);
Stefan Roese706865a2017-03-20 12:51:48 +0100161 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassbcbe3d12015-09-28 23:32:01 -0600162 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glasse59f4582014-07-23 06:55:20 -0600163
164 /* Check that we can do this twice */
165 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glassbcbe3d12015-09-28 23:32:01 -0600166 parent_data = dev_get_parent_priv(dev);
Simon Glasse59f4582014-07-23 06:55:20 -0600167 ut_assert(NULL != parent_data);
168 parent_data->sum += 5;
169 ut_asserteq(5, parent_data->sum);
170
171 /* Add parent data to all children */
172 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
173 value = 5;
174 uclass_foreach_dev(dev, uc) {
175 /* Ignore these if they are not on this bus */
176 if (dev->parent != bus) {
Simon Glassbcbe3d12015-09-28 23:32:01 -0600177 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glasse59f4582014-07-23 06:55:20 -0600178 continue;
179 }
180 ut_assertok(device_probe(dev));
Simon Glassbcbe3d12015-09-28 23:32:01 -0600181 parent_data = dev_get_parent_priv(dev);
Simon Glasse59f4582014-07-23 06:55:20 -0600182
183 parent_data->sum = value;
184 value += 5;
185 }
186
187 /* Check it is still there */
188 value = 5;
189 uclass_foreach_dev(dev, uc) {
190 /* Ignore these if they are not on this bus */
191 if (dev->parent != bus)
192 continue;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600193 parent_data = dev_get_parent_priv(dev);
Simon Glasse59f4582014-07-23 06:55:20 -0600194
195 ut_asserteq(value, parent_data->sum);
196 value += 5;
197 }
198
199 return 0;
200}
Simon Glassdac8db22015-01-25 08:27:06 -0700201/* Test that the bus can store data about each child */
Joe Hershbergere721b882015-05-20 14:27:27 -0500202static int dm_test_bus_parent_data(struct unit_test_state *uts)
Simon Glassdac8db22015-01-25 08:27:06 -0700203{
Joe Hershbergere721b882015-05-20 14:27:27 -0500204 return test_bus_parent_data(uts);
Simon Glassdac8db22015-01-25 08:27:06 -0700205}
Simon Glasse180c2b2020-07-28 19:41:12 -0600206DM_TEST(dm_test_bus_parent_data, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa327dee2014-07-23 06:55:21 -0600207
Simon Glassdac8db22015-01-25 08:27:06 -0700208/* As above but the size is controlled by the uclass */
Joe Hershbergere721b882015-05-20 14:27:27 -0500209static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
Simon Glassdac8db22015-01-25 08:27:06 -0700210{
Simon Glasse23eb612015-03-25 12:21:51 -0600211 struct driver *drv;
Simon Glassdac8db22015-01-25 08:27:06 -0700212 struct udevice *bus;
213 int size;
214 int ret;
215
216 /* Set the driver size to 0 so that the uclass size is used */
217 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glasse23eb612015-03-25 12:21:51 -0600218 drv = (struct driver *)bus->driver;
Simon Glass41575d82020-12-03 16:55:17 -0700219 size = drv->per_child_auto;
Simon Glass9f8037e2018-10-01 21:12:32 -0600220
221#ifdef CONFIG_SANDBOX
222 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
223 os_mprotect_allow(drv, sizeof(*drv));
224#endif
Simon Glass41575d82020-12-03 16:55:17 -0700225 bus->uclass->uc_drv->per_child_auto = size;
226 drv->per_child_auto = 0;
Joe Hershbergere721b882015-05-20 14:27:27 -0500227 ret = test_bus_parent_data(uts);
Simon Glassdac8db22015-01-25 08:27:06 -0700228 if (ret)
229 return ret;
Simon Glass41575d82020-12-03 16:55:17 -0700230 bus->uclass->uc_drv->per_child_auto = 0;
231 drv->per_child_auto = size;
Simon Glassdac8db22015-01-25 08:27:06 -0700232
233 return 0;
234}
235DM_TEST(dm_test_bus_parent_data_uclass,
Simon Glasse180c2b2020-07-28 19:41:12 -0600236 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassdac8db22015-01-25 08:27:06 -0700237
Simon Glassa327dee2014-07-23 06:55:21 -0600238/* Test that the bus ops are called when a child is probed/removed */
Joe Hershbergere721b882015-05-20 14:27:27 -0500239static int dm_test_bus_parent_ops(struct unit_test_state *uts)
Simon Glassa327dee2014-07-23 06:55:21 -0600240{
241 struct dm_test_parent_data *parent_data;
242 struct udevice *bus, *dev;
243 struct uclass *uc;
244
Simon Glass079ac592020-12-23 08:11:18 -0700245 testbus_get_clear_removed();
Simon Glassa327dee2014-07-23 06:55:21 -0600246 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
247 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
248
249 uclass_foreach_dev(dev, uc) {
250 /* Ignore these if they are not on this bus */
251 if (dev->parent != bus)
252 continue;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600253 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glassa327dee2014-07-23 06:55:21 -0600254
255 ut_assertok(device_probe(dev));
Simon Glassbcbe3d12015-09-28 23:32:01 -0600256 parent_data = dev_get_parent_priv(dev);
Simon Glass079ac592020-12-23 08:11:18 -0700257 ut_asserteq(TEST_FLAG_CHILD_PROBED, parent_data->flag);
Simon Glassa327dee2014-07-23 06:55:21 -0600258 }
259
260 uclass_foreach_dev(dev, uc) {
261 /* Ignore these if they are not on this bus */
262 if (dev->parent != bus)
263 continue;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600264 parent_data = dev_get_parent_priv(dev);
Simon Glass079ac592020-12-23 08:11:18 -0700265 ut_asserteq(TEST_FLAG_CHILD_PROBED, parent_data->flag);
Stefan Roese706865a2017-03-20 12:51:48 +0100266 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassbcbe3d12015-09-28 23:32:01 -0600267 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass079ac592020-12-23 08:11:18 -0700268 ut_asserteq_ptr(testbus_get_clear_removed(), dev);
Simon Glassa327dee2014-07-23 06:55:21 -0600269 }
Simon Glassa327dee2014-07-23 06:55:21 -0600270
271 return 0;
272}
Simon Glasse180c2b2020-07-28 19:41:12 -0600273DM_TEST(dm_test_bus_parent_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glasscdc133b2015-01-25 08:27:01 -0700274
Simon Glasscaa4daa2020-12-03 16:55:18 -0700275static int test_bus_parent_plat(struct unit_test_state *uts)
Simon Glasscdc133b2015-01-25 08:27:01 -0700276{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700277 struct dm_test_parent_plat *plat;
Simon Glasscdc133b2015-01-25 08:27:01 -0700278 struct udevice *bus, *dev;
Simon Glasscdc133b2015-01-25 08:27:01 -0700279
280 /* Check that the bus has no children */
281 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
282 device_find_first_child(bus, &dev);
283 ut_asserteq_ptr(NULL, dev);
284
285 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
286
Lokesh Vutla240b9322019-09-04 16:01:26 +0530287 for (device_find_first_child(bus, &dev);
Simon Glasscdc133b2015-01-25 08:27:01 -0700288 dev;
289 device_find_next_child(&dev)) {
290 /* Check that platform data is allocated */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700291 plat = dev_get_parent_plat(dev);
Simon Glasscdc133b2015-01-25 08:27:01 -0700292 ut_assert(plat != NULL);
293
294 /*
295 * Check that it is not affected by the device being
296 * probed/removed
297 */
298 plat->count++;
299 ut_asserteq(1, plat->count);
300 device_probe(dev);
Stefan Roese706865a2017-03-20 12:51:48 +0100301 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glasscdc133b2015-01-25 08:27:01 -0700302
Simon Glasscaa4daa2020-12-03 16:55:18 -0700303 ut_asserteq_ptr(plat, dev_get_parent_plat(dev));
Simon Glasscdc133b2015-01-25 08:27:01 -0700304 ut_asserteq(1, plat->count);
305 ut_assertok(device_probe(dev));
Simon Glasscdc133b2015-01-25 08:27:01 -0700306 }
Lokesh Vutla240b9322019-09-04 16:01:26 +0530307 ut_asserteq(3, device_get_child_count(bus));
Simon Glasscdc133b2015-01-25 08:27:01 -0700308
309 /* Removing the bus should also have no effect (it is still bound) */
Stefan Roese706865a2017-03-20 12:51:48 +0100310 device_remove(bus, DM_REMOVE_NORMAL);
Lokesh Vutla240b9322019-09-04 16:01:26 +0530311 for (device_find_first_child(bus, &dev);
Simon Glasscdc133b2015-01-25 08:27:01 -0700312 dev;
313 device_find_next_child(&dev)) {
314 /* Check that platform data is allocated */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700315 plat = dev_get_parent_plat(dev);
Simon Glasscdc133b2015-01-25 08:27:01 -0700316 ut_assert(plat != NULL);
317 ut_asserteq(1, plat->count);
Simon Glasscdc133b2015-01-25 08:27:01 -0700318 }
Lokesh Vutla240b9322019-09-04 16:01:26 +0530319 ut_asserteq(3, device_get_child_count(bus));
Simon Glasscdc133b2015-01-25 08:27:01 -0700320
321 /* Unbind all the children */
322 do {
323 device_find_first_child(bus, &dev);
324 if (dev)
325 device_unbind(dev);
326 } while (dev);
327
Simon Glasscaa4daa2020-12-03 16:55:18 -0700328 /* Now the child plat should be removed and re-added */
Simon Glasscdc133b2015-01-25 08:27:01 -0700329 device_probe(bus);
Lokesh Vutla240b9322019-09-04 16:01:26 +0530330 for (device_find_first_child(bus, &dev);
Simon Glasscdc133b2015-01-25 08:27:01 -0700331 dev;
332 device_find_next_child(&dev)) {
333 /* Check that platform data is allocated */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700334 plat = dev_get_parent_plat(dev);
Simon Glasscdc133b2015-01-25 08:27:01 -0700335 ut_assert(plat != NULL);
336 ut_asserteq(0, plat->count);
Simon Glasscdc133b2015-01-25 08:27:01 -0700337 }
Lokesh Vutla240b9322019-09-04 16:01:26 +0530338 ut_asserteq(3, device_get_child_count(bus));
Simon Glasscdc133b2015-01-25 08:27:01 -0700339
340 return 0;
341}
Simon Glassba8da9d2015-01-25 08:27:02 -0700342
343/* Test that the bus can store platform data about each child */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700344static int dm_test_bus_parent_plat(struct unit_test_state *uts)
Simon Glassba8da9d2015-01-25 08:27:02 -0700345{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700346 return test_bus_parent_plat(uts);
Simon Glassba8da9d2015-01-25 08:27:02 -0700347}
Simon Glasscaa4daa2020-12-03 16:55:18 -0700348DM_TEST(dm_test_bus_parent_plat, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassba8da9d2015-01-25 08:27:02 -0700349
350/* As above but the size is controlled by the uclass */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700351static int dm_test_bus_parent_plat_uclass(struct unit_test_state *uts)
Simon Glassba8da9d2015-01-25 08:27:02 -0700352{
353 struct udevice *bus;
Simon Glasse23eb612015-03-25 12:21:51 -0600354 struct driver *drv;
Simon Glassba8da9d2015-01-25 08:27:02 -0700355 int size;
356 int ret;
357
358 /* Set the driver size to 0 so that the uclass size is used */
359 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glasse23eb612015-03-25 12:21:51 -0600360 drv = (struct driver *)bus->driver;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700361 size = drv->per_child_plat_auto;
Simon Glass9f8037e2018-10-01 21:12:32 -0600362#ifdef CONFIG_SANDBOX
363 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
364 os_mprotect_allow(drv, sizeof(*drv));
365#endif
Simon Glasscaa4daa2020-12-03 16:55:18 -0700366 bus->uclass->uc_drv->per_child_plat_auto = size;
367 drv->per_child_plat_auto = 0;
368 ret = test_bus_parent_plat(uts);
Simon Glassba8da9d2015-01-25 08:27:02 -0700369 if (ret)
370 return ret;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700371 bus->uclass->uc_drv->per_child_plat_auto = 0;
372 drv->per_child_plat_auto = size;
Simon Glassba8da9d2015-01-25 08:27:02 -0700373
374 return 0;
375}
Simon Glasscaa4daa2020-12-03 16:55:18 -0700376DM_TEST(dm_test_bus_parent_plat_uclass,
Simon Glasse180c2b2020-07-28 19:41:12 -0600377 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0118ce72015-01-25 08:27:03 -0700378
379/* Test that the child post_bind method is called */
Joe Hershbergere721b882015-05-20 14:27:27 -0500380static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
Simon Glass0118ce72015-01-25 08:27:03 -0700381{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700382 struct dm_test_parent_plat *plat;
Simon Glass0118ce72015-01-25 08:27:03 -0700383 struct udevice *bus, *dev;
Simon Glass0118ce72015-01-25 08:27:03 -0700384
385 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutla240b9322019-09-04 16:01:26 +0530386 for (device_find_first_child(bus, &dev);
Simon Glass0118ce72015-01-25 08:27:03 -0700387 dev;
388 device_find_next_child(&dev)) {
389 /* Check that platform data is allocated */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700390 plat = dev_get_parent_plat(dev);
Simon Glass0118ce72015-01-25 08:27:03 -0700391 ut_assert(plat != NULL);
392 ut_asserteq(1, plat->bind_flag);
Simon Glass0118ce72015-01-25 08:27:03 -0700393 }
Lokesh Vutla240b9322019-09-04 16:01:26 +0530394 ut_asserteq(3, device_get_child_count(bus));
Simon Glass0118ce72015-01-25 08:27:03 -0700395
396 return 0;
397}
Simon Glasse180c2b2020-07-28 19:41:12 -0600398DM_TEST(dm_test_bus_child_post_bind, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass081f2fc2015-01-25 08:27:08 -0700399
400/* Test that the child post_bind method is called */
Joe Hershbergere721b882015-05-20 14:27:27 -0500401static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
Simon Glass081f2fc2015-01-25 08:27:08 -0700402{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700403 struct dm_test_parent_plat *plat;
Simon Glass081f2fc2015-01-25 08:27:08 -0700404 struct udevice *bus, *dev;
Simon Glass081f2fc2015-01-25 08:27:08 -0700405
406 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutla240b9322019-09-04 16:01:26 +0530407 for (device_find_first_child(bus, &dev);
Simon Glass081f2fc2015-01-25 08:27:08 -0700408 dev;
409 device_find_next_child(&dev)) {
410 /* Check that platform data is allocated */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700411 plat = dev_get_parent_plat(dev);
Simon Glass081f2fc2015-01-25 08:27:08 -0700412 ut_assert(plat != NULL);
413 ut_asserteq(2, plat->uclass_bind_flag);
Simon Glass081f2fc2015-01-25 08:27:08 -0700414 }
Lokesh Vutla240b9322019-09-04 16:01:26 +0530415 ut_asserteq(3, device_get_child_count(bus));
Simon Glass081f2fc2015-01-25 08:27:08 -0700416
417 return 0;
418}
419DM_TEST(dm_test_bus_child_post_bind_uclass,
Simon Glasse180c2b2020-07-28 19:41:12 -0600420 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass83c7e432015-01-25 08:27:10 -0700421
422/*
423 * Test that the bus' uclass' child_pre_probe() is called before the
424 * device's probe() method
425 */
Joe Hershbergere721b882015-05-20 14:27:27 -0500426static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
Simon Glass83c7e432015-01-25 08:27:10 -0700427{
428 struct udevice *bus, *dev;
Simon Glass83c7e432015-01-25 08:27:10 -0700429
430 /*
431 * See testfdt_drv_probe() which effectively checks that the uclass
432 * flag is set before that method is called
433 */
434 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutla240b9322019-09-04 16:01:26 +0530435 for (device_find_first_child(bus, &dev);
Simon Glass83c7e432015-01-25 08:27:10 -0700436 dev;
437 device_find_next_child(&dev)) {
438 struct dm_test_priv *priv = dev_get_priv(dev);
439
440 /* Check that things happened in the right order */
441 ut_asserteq_ptr(NULL, priv);
442 ut_assertok(device_probe(dev));
443
444 priv = dev_get_priv(dev);
445 ut_assert(priv != NULL);
446 ut_asserteq(1, priv->uclass_flag);
447 ut_asserteq(1, priv->uclass_total);
Simon Glass83c7e432015-01-25 08:27:10 -0700448 }
Lokesh Vutla240b9322019-09-04 16:01:26 +0530449 ut_asserteq(3, device_get_child_count(bus));
Simon Glass83c7e432015-01-25 08:27:10 -0700450
451 return 0;
452}
453DM_TEST(dm_test_bus_child_pre_probe_uclass,
Simon Glasse180c2b2020-07-28 19:41:12 -0600454 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Bin Mengd92878a2018-10-15 02:20:58 -0700455
456/*
457 * Test that the bus' uclass' child_post_probe() is called after the
458 * device's probe() method
459 */
460static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts)
461{
462 struct udevice *bus, *dev;
Bin Mengd92878a2018-10-15 02:20:58 -0700463
464 /*
465 * See testfdt_drv_probe() which effectively initializes that
466 * the uclass postp flag is set to a value
467 */
468 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutla240b9322019-09-04 16:01:26 +0530469 for (device_find_first_child(bus, &dev);
Bin Mengd92878a2018-10-15 02:20:58 -0700470 dev;
471 device_find_next_child(&dev)) {
472 struct dm_test_priv *priv = dev_get_priv(dev);
473
474 /* Check that things happened in the right order */
475 ut_asserteq_ptr(NULL, priv);
476 ut_assertok(device_probe(dev));
477
478 priv = dev_get_priv(dev);
479 ut_assert(priv != NULL);
480 ut_asserteq(0, priv->uclass_postp);
Bin Mengd92878a2018-10-15 02:20:58 -0700481 }
Lokesh Vutla240b9322019-09-04 16:01:26 +0530482 ut_asserteq(3, device_get_child_count(bus));
Bin Mengd92878a2018-10-15 02:20:58 -0700483
484 return 0;
485}
486DM_TEST(dm_test_bus_child_post_probe_uclass,
Simon Glasse180c2b2020-07-28 19:41:12 -0600487 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);