blob: 1ad45adb6059c7a574c2d5775f8b7b36e0ad3f7c [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
8#include <os.h>
9#endif
Simon Glass40717422014-07-23 06:55:18 -060010#include <dm.h>
Lokesh Vutlafa45a082019-09-04 16:01:26 +053011#include <dm/device.h>
Simon Glass60d971b2014-07-23 06:55:20 -060012#include <dm/device-internal.h>
Simon Glass40717422014-07-23 06:55:18 -060013#include <dm/test.h>
Simon Glass11b61732015-01-25 08:27:01 -070014#include <dm/uclass-internal.h>
Simon Glass40717422014-07-23 06:55:18 -060015#include <dm/util.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050016#include <test/ut.h>
Simon Glass40717422014-07-23 06:55:18 -060017
18DECLARE_GLOBAL_DATA_PTR;
19
Simon Glass11b61732015-01-25 08:27:01 -070020struct dm_test_parent_platdata {
21 int count;
Simon Glassa4a51a02015-01-25 08:27:03 -070022 int bind_flag;
Simon Glassf4c9b3e2015-01-25 08:27:08 -070023 int uclass_bind_flag;
Simon Glass11b61732015-01-25 08:27:01 -070024};
25
Simon Glassd45560d2014-07-23 06:55:21 -060026enum {
27 FLAG_CHILD_PROBED = 10,
28 FLAG_CHILD_REMOVED = -7,
29};
30
31static struct dm_test_state *test_state;
32
Simon Glass40717422014-07-23 06:55:18 -060033static int testbus_drv_probe(struct udevice *dev)
34{
Simon Glass09128c52016-07-05 17:10:09 -060035 return dm_scan_fdt_dev(dev);
Simon Glass40717422014-07-23 06:55:18 -060036}
37
Simon Glassa4a51a02015-01-25 08:27:03 -070038static int testbus_child_post_bind(struct udevice *dev)
39{
40 struct dm_test_parent_platdata *plat;
41
42 plat = dev_get_parent_platdata(dev);
43 plat->bind_flag = 1;
Simon Glassf4c9b3e2015-01-25 08:27:08 -070044 plat->uclass_bind_flag = 2;
Simon Glassa4a51a02015-01-25 08:27:03 -070045
46 return 0;
47}
48
Simon Glassd45560d2014-07-23 06:55:21 -060049static int testbus_child_pre_probe(struct udevice *dev)
50{
Simon Glassde44acf2015-09-28 23:32:01 -060051 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
Simon Glassd45560d2014-07-23 06:55:21 -060052
53 parent_data->flag += FLAG_CHILD_PROBED;
54
55 return 0;
56}
57
Simon Glass5104b982015-01-25 08:27:10 -070058static int testbus_child_pre_probe_uclass(struct udevice *dev)
59{
60 struct dm_test_priv *priv = dev_get_priv(dev);
61
62 priv->uclass_flag++;
63
64 return 0;
65}
66
Bin Mengd9bad172018-10-15 02:20:58 -070067static int testbus_child_post_probe_uclass(struct udevice *dev)
68{
69 struct dm_test_priv *priv = dev_get_priv(dev);
70
71 priv->uclass_postp++;
72
73 return 0;
74}
75
Simon Glassd45560d2014-07-23 06:55:21 -060076static int testbus_child_post_remove(struct udevice *dev)
77{
Simon Glassde44acf2015-09-28 23:32:01 -060078 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
Simon Glassd45560d2014-07-23 06:55:21 -060079 struct dm_test_state *dms = test_state;
80
81 parent_data->flag += FLAG_CHILD_REMOVED;
82 if (dms)
83 dms->removed = dev;
84
85 return 0;
86}
87
Simon Glass40717422014-07-23 06:55:18 -060088static const struct udevice_id testbus_ids[] = {
89 {
90 .compatible = "denx,u-boot-test-bus",
91 .data = DM_TEST_TYPE_FIRST },
92 { }
93};
94
95U_BOOT_DRIVER(testbus_drv) = {
96 .name = "testbus_drv",
97 .of_match = testbus_ids,
98 .id = UCLASS_TEST_BUS,
99 .probe = testbus_drv_probe,
Simon Glassa4a51a02015-01-25 08:27:03 -0700100 .child_post_bind = testbus_child_post_bind,
Simon Glass40717422014-07-23 06:55:18 -0600101 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
102 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
Simon Glass60d971b2014-07-23 06:55:20 -0600103 .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
Simon Glass11b61732015-01-25 08:27:01 -0700104 .per_child_platdata_auto_alloc_size =
105 sizeof(struct dm_test_parent_platdata),
Simon Glassd45560d2014-07-23 06:55:21 -0600106 .child_pre_probe = testbus_child_pre_probe,
107 .child_post_remove = testbus_child_post_remove,
Simon Glass40717422014-07-23 06:55:18 -0600108};
109
110UCLASS_DRIVER(testbus) = {
111 .name = "testbus",
112 .id = UCLASS_TEST_BUS,
Simon Glass0ccb0972015-01-25 08:27:05 -0700113 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass5104b982015-01-25 08:27:10 -0700114 .child_pre_probe = testbus_child_pre_probe_uclass,
Bin Mengd9bad172018-10-15 02:20:58 -0700115 .child_post_probe = testbus_child_post_probe_uclass,
Simon Glass40717422014-07-23 06:55:18 -0600116};
117
118/* Test that we can probe for children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500119static int dm_test_bus_children(struct unit_test_state *uts)
Simon Glass40717422014-07-23 06:55:18 -0600120{
Bin Mengd9d24782018-10-10 22:07:01 -0700121 int num_devices = 8;
Simon Glass40717422014-07-23 06:55:18 -0600122 struct udevice *bus;
123 struct uclass *uc;
124
125 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
126 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
127
128 /* Probe the bus, which should yield 3 more devices */
129 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
130 num_devices += 3;
131
132 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
133 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
134
Joe Hershberger3a77be52015-05-20 14:27:27 -0500135 ut_assert(!dm_check_devices(uts, num_devices));
Simon Glass40717422014-07-23 06:55:18 -0600136
137 return 0;
138}
139DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass48d4e292014-07-23 06:55:19 -0600140
141/* Test our functions for accessing children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500142static int dm_test_bus_children_funcs(struct unit_test_state *uts)
Simon Glass48d4e292014-07-23 06:55:19 -0600143{
144 const void *blob = gd->fdt_blob;
145 struct udevice *bus, *dev;
146 int node;
147
148 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
149
150 /* device_get_child() */
151 ut_assertok(device_get_child(bus, 0, &dev));
152 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
153 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
154 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
155 ut_asserteq_str("c-test@5", dev->name);
156
157 /* Device with sequence number 0 should be accessible */
158 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
159 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
160 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
161 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
162 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
163 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
164
165 /* There is no device with sequence number 2 */
166 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
167 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
168 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
169
170 /* Looking for something that is not a child */
171 node = fdt_path_offset(blob, "/junk");
172 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
173 node = fdt_path_offset(blob, "/d-test");
174 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
175
Simon Glass9b4057a2017-05-18 20:09:43 -0600176 return 0;
177}
178DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
179
180static int dm_test_bus_children_of_offset(struct unit_test_state *uts)
181{
182 const void *blob = gd->fdt_blob;
183 struct udevice *bus, *dev;
184 int node;
185
186 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glassac855522017-06-07 10:28:44 -0600187 ut_assertnonnull(bus);
Simon Glass9b4057a2017-05-18 20:09:43 -0600188
Simon Glass48d4e292014-07-23 06:55:19 -0600189 /* Find a valid child */
190 node = fdt_path_offset(blob, "/some-bus/c-test@1");
Simon Glass9b4057a2017-05-18 20:09:43 -0600191 ut_assert(node > 0);
Simon Glass48d4e292014-07-23 06:55:19 -0600192 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
Simon Glassac855522017-06-07 10:28:44 -0600193 ut_assertnonnull(dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600194 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
195 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
Simon Glassac855522017-06-07 10:28:44 -0600196 ut_assertnonnull(dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600197 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
198
199 return 0;
200}
Simon Glass9b4057a2017-05-18 20:09:43 -0600201DM_TEST(dm_test_bus_children_of_offset,
202 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
Simon Glass60d971b2014-07-23 06:55:20 -0600203
Simon Glass44da7352014-10-13 23:41:49 -0600204/* Test that we can iterate through children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500205static int dm_test_bus_children_iterators(struct unit_test_state *uts)
Simon Glass44da7352014-10-13 23:41:49 -0600206{
207 struct udevice *bus, *dev, *child;
208
209 /* Walk through the children one by one */
210 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
211 ut_assertok(device_find_first_child(bus, &dev));
212 ut_asserteq_str("c-test@5", dev->name);
213 ut_assertok(device_find_next_child(&dev));
214 ut_asserteq_str("c-test@0", dev->name);
215 ut_assertok(device_find_next_child(&dev));
216 ut_asserteq_str("c-test@1", dev->name);
217 ut_assertok(device_find_next_child(&dev));
218 ut_asserteq_ptr(dev, NULL);
219
220 /* Move to the next child without using device_find_first_child() */
221 ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
222 ut_asserteq_str("c-test@5", dev->name);
223 ut_assertok(device_find_next_child(&dev));
224 ut_asserteq_str("c-test@0", dev->name);
225
226 /* Try a device with no children */
227 ut_assertok(device_find_first_child(dev, &child));
228 ut_asserteq_ptr(child, NULL);
229
230 return 0;
231}
232DM_TEST(dm_test_bus_children_iterators,
233 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
234
Simon Glass60d971b2014-07-23 06:55:20 -0600235/* Test that the bus can store data about each child */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500236static int test_bus_parent_data(struct unit_test_state *uts)
Simon Glass60d971b2014-07-23 06:55:20 -0600237{
238 struct dm_test_parent_data *parent_data;
239 struct udevice *bus, *dev;
240 struct uclass *uc;
241 int value;
242
243 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
244
245 /* Check that parent data is allocated */
246 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600247 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600248 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600249 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600250 ut_assert(NULL != parent_data);
251
252 /* Check that it starts at 0 and goes away when device is removed */
253 parent_data->sum += 5;
254 ut_asserteq(5, parent_data->sum);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100255 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassde44acf2015-09-28 23:32:01 -0600256 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600257
258 /* Check that we can do this twice */
259 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600260 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600261 ut_assert(NULL != parent_data);
262 parent_data->sum += 5;
263 ut_asserteq(5, parent_data->sum);
264
265 /* Add parent data to all children */
266 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
267 value = 5;
268 uclass_foreach_dev(dev, uc) {
269 /* Ignore these if they are not on this bus */
270 if (dev->parent != bus) {
Simon Glassde44acf2015-09-28 23:32:01 -0600271 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600272 continue;
273 }
274 ut_assertok(device_probe(dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600275 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600276
277 parent_data->sum = value;
278 value += 5;
279 }
280
281 /* Check it is still there */
282 value = 5;
283 uclass_foreach_dev(dev, uc) {
284 /* Ignore these if they are not on this bus */
285 if (dev->parent != bus)
286 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600287 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600288
289 ut_asserteq(value, parent_data->sum);
290 value += 5;
291 }
292
293 return 0;
294}
Simon Glassc23b4282015-01-25 08:27:06 -0700295/* Test that the bus can store data about each child */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500296static int dm_test_bus_parent_data(struct unit_test_state *uts)
Simon Glassc23b4282015-01-25 08:27:06 -0700297{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500298 return test_bus_parent_data(uts);
Simon Glassc23b4282015-01-25 08:27:06 -0700299}
Simon Glass60d971b2014-07-23 06:55:20 -0600300DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassd45560d2014-07-23 06:55:21 -0600301
Simon Glassc23b4282015-01-25 08:27:06 -0700302/* As above but the size is controlled by the uclass */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500303static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
Simon Glassc23b4282015-01-25 08:27:06 -0700304{
Simon Glass2410d882015-03-25 12:21:51 -0600305 struct driver *drv;
Simon Glassc23b4282015-01-25 08:27:06 -0700306 struct udevice *bus;
307 int size;
308 int ret;
309
310 /* Set the driver size to 0 so that the uclass size is used */
311 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glass2410d882015-03-25 12:21:51 -0600312 drv = (struct driver *)bus->driver;
313 size = drv->per_child_auto_alloc_size;
Simon Glass4e766c22018-10-01 21:12:32 -0600314
315#ifdef CONFIG_SANDBOX
316 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
317 os_mprotect_allow(drv, sizeof(*drv));
318#endif
Simon Glassc23b4282015-01-25 08:27:06 -0700319 bus->uclass->uc_drv->per_child_auto_alloc_size = size;
Simon Glass2410d882015-03-25 12:21:51 -0600320 drv->per_child_auto_alloc_size = 0;
Joe Hershberger3a77be52015-05-20 14:27:27 -0500321 ret = test_bus_parent_data(uts);
Simon Glassc23b4282015-01-25 08:27:06 -0700322 if (ret)
323 return ret;
324 bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
Simon Glass2410d882015-03-25 12:21:51 -0600325 drv->per_child_auto_alloc_size = size;
Simon Glassc23b4282015-01-25 08:27:06 -0700326
327 return 0;
328}
329DM_TEST(dm_test_bus_parent_data_uclass,
330 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
331
Simon Glassd45560d2014-07-23 06:55:21 -0600332/* Test that the bus ops are called when a child is probed/removed */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500333static int dm_test_bus_parent_ops(struct unit_test_state *uts)
Simon Glassd45560d2014-07-23 06:55:21 -0600334{
335 struct dm_test_parent_data *parent_data;
Joe Hershberger3a77be52015-05-20 14:27:27 -0500336 struct dm_test_state *dms = uts->priv;
Simon Glassd45560d2014-07-23 06:55:21 -0600337 struct udevice *bus, *dev;
338 struct uclass *uc;
339
340 test_state = dms;
341 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
342 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
343
344 uclass_foreach_dev(dev, uc) {
345 /* Ignore these if they are not on this bus */
346 if (dev->parent != bus)
347 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600348 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glassd45560d2014-07-23 06:55:21 -0600349
350 ut_assertok(device_probe(dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600351 parent_data = dev_get_parent_priv(dev);
Simon Glassd45560d2014-07-23 06:55:21 -0600352 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
353 }
354
355 uclass_foreach_dev(dev, uc) {
356 /* Ignore these if they are not on this bus */
357 if (dev->parent != bus)
358 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600359 parent_data = dev_get_parent_priv(dev);
Simon Glassd45560d2014-07-23 06:55:21 -0600360 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100361 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassde44acf2015-09-28 23:32:01 -0600362 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glassd45560d2014-07-23 06:55:21 -0600363 ut_asserteq_ptr(dms->removed, dev);
364 }
365 test_state = NULL;
366
367 return 0;
368}
369DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass11b61732015-01-25 08:27:01 -0700370
Joe Hershberger3a77be52015-05-20 14:27:27 -0500371static int test_bus_parent_platdata(struct unit_test_state *uts)
Simon Glass11b61732015-01-25 08:27:01 -0700372{
373 struct dm_test_parent_platdata *plat;
374 struct udevice *bus, *dev;
Simon Glass11b61732015-01-25 08:27:01 -0700375
376 /* Check that the bus has no children */
377 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
378 device_find_first_child(bus, &dev);
379 ut_asserteq_ptr(NULL, dev);
380
381 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
382
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530383 for (device_find_first_child(bus, &dev);
Simon Glass11b61732015-01-25 08:27:01 -0700384 dev;
385 device_find_next_child(&dev)) {
386 /* Check that platform data is allocated */
387 plat = dev_get_parent_platdata(dev);
388 ut_assert(plat != NULL);
389
390 /*
391 * Check that it is not affected by the device being
392 * probed/removed
393 */
394 plat->count++;
395 ut_asserteq(1, plat->count);
396 device_probe(dev);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100397 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass11b61732015-01-25 08:27:01 -0700398
399 ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
400 ut_asserteq(1, plat->count);
401 ut_assertok(device_probe(dev));
Simon Glass11b61732015-01-25 08:27:01 -0700402 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530403 ut_asserteq(3, device_get_child_count(bus));
Simon Glass11b61732015-01-25 08:27:01 -0700404
405 /* Removing the bus should also have no effect (it is still bound) */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100406 device_remove(bus, DM_REMOVE_NORMAL);
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530407 for (device_find_first_child(bus, &dev);
Simon Glass11b61732015-01-25 08:27:01 -0700408 dev;
409 device_find_next_child(&dev)) {
410 /* Check that platform data is allocated */
411 plat = dev_get_parent_platdata(dev);
412 ut_assert(plat != NULL);
413 ut_asserteq(1, plat->count);
Simon Glass11b61732015-01-25 08:27:01 -0700414 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530415 ut_asserteq(3, device_get_child_count(bus));
Simon Glass11b61732015-01-25 08:27:01 -0700416
417 /* Unbind all the children */
418 do {
419 device_find_first_child(bus, &dev);
420 if (dev)
421 device_unbind(dev);
422 } while (dev);
423
424 /* Now the child platdata should be removed and re-added */
425 device_probe(bus);
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530426 for (device_find_first_child(bus, &dev);
Simon Glass11b61732015-01-25 08:27:01 -0700427 dev;
428 device_find_next_child(&dev)) {
429 /* Check that platform data is allocated */
430 plat = dev_get_parent_platdata(dev);
431 ut_assert(plat != NULL);
432 ut_asserteq(0, plat->count);
Simon Glass11b61732015-01-25 08:27:01 -0700433 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530434 ut_asserteq(3, device_get_child_count(bus));
Simon Glass11b61732015-01-25 08:27:01 -0700435
436 return 0;
437}
Simon Glass57f95402015-01-25 08:27:02 -0700438
439/* Test that the bus can store platform data about each child */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500440static int dm_test_bus_parent_platdata(struct unit_test_state *uts)
Simon Glass57f95402015-01-25 08:27:02 -0700441{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500442 return test_bus_parent_platdata(uts);
Simon Glass57f95402015-01-25 08:27:02 -0700443}
Simon Glass11b61732015-01-25 08:27:01 -0700444DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass57f95402015-01-25 08:27:02 -0700445
446/* As above but the size is controlled by the uclass */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500447static int dm_test_bus_parent_platdata_uclass(struct unit_test_state *uts)
Simon Glass57f95402015-01-25 08:27:02 -0700448{
449 struct udevice *bus;
Simon Glass2410d882015-03-25 12:21:51 -0600450 struct driver *drv;
Simon Glass57f95402015-01-25 08:27:02 -0700451 int size;
452 int ret;
453
454 /* Set the driver size to 0 so that the uclass size is used */
455 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glass2410d882015-03-25 12:21:51 -0600456 drv = (struct driver *)bus->driver;
457 size = drv->per_child_platdata_auto_alloc_size;
Simon Glass4e766c22018-10-01 21:12:32 -0600458#ifdef CONFIG_SANDBOX
459 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
460 os_mprotect_allow(drv, sizeof(*drv));
461#endif
Simon Glass57f95402015-01-25 08:27:02 -0700462 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
Simon Glass2410d882015-03-25 12:21:51 -0600463 drv->per_child_platdata_auto_alloc_size = 0;
Joe Hershberger3a77be52015-05-20 14:27:27 -0500464 ret = test_bus_parent_platdata(uts);
Simon Glass57f95402015-01-25 08:27:02 -0700465 if (ret)
466 return ret;
467 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
Simon Glass2410d882015-03-25 12:21:51 -0600468 drv->per_child_platdata_auto_alloc_size = size;
Simon Glass57f95402015-01-25 08:27:02 -0700469
470 return 0;
471}
472DM_TEST(dm_test_bus_parent_platdata_uclass,
473 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassa4a51a02015-01-25 08:27:03 -0700474
475/* Test that the child post_bind method is called */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500476static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
Simon Glassa4a51a02015-01-25 08:27:03 -0700477{
478 struct dm_test_parent_platdata *plat;
479 struct udevice *bus, *dev;
Simon Glassa4a51a02015-01-25 08:27:03 -0700480
481 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530482 for (device_find_first_child(bus, &dev);
Simon Glassa4a51a02015-01-25 08:27:03 -0700483 dev;
484 device_find_next_child(&dev)) {
485 /* Check that platform data is allocated */
486 plat = dev_get_parent_platdata(dev);
487 ut_assert(plat != NULL);
488 ut_asserteq(1, plat->bind_flag);
Simon Glassa4a51a02015-01-25 08:27:03 -0700489 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530490 ut_asserteq(3, device_get_child_count(bus));
Simon Glassa4a51a02015-01-25 08:27:03 -0700491
492 return 0;
493}
494DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700495
496/* Test that the child post_bind method is called */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500497static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700498{
499 struct dm_test_parent_platdata *plat;
500 struct udevice *bus, *dev;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700501
502 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530503 for (device_find_first_child(bus, &dev);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700504 dev;
505 device_find_next_child(&dev)) {
506 /* Check that platform data is allocated */
507 plat = dev_get_parent_platdata(dev);
508 ut_assert(plat != NULL);
509 ut_asserteq(2, plat->uclass_bind_flag);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700510 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530511 ut_asserteq(3, device_get_child_count(bus));
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700512
513 return 0;
514}
515DM_TEST(dm_test_bus_child_post_bind_uclass,
516 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass5104b982015-01-25 08:27:10 -0700517
518/*
519 * Test that the bus' uclass' child_pre_probe() is called before the
520 * device's probe() method
521 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500522static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
Simon Glass5104b982015-01-25 08:27:10 -0700523{
524 struct udevice *bus, *dev;
Simon Glass5104b982015-01-25 08:27:10 -0700525
526 /*
527 * See testfdt_drv_probe() which effectively checks that the uclass
528 * flag is set before that method is called
529 */
530 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530531 for (device_find_first_child(bus, &dev);
Simon Glass5104b982015-01-25 08:27:10 -0700532 dev;
533 device_find_next_child(&dev)) {
534 struct dm_test_priv *priv = dev_get_priv(dev);
535
536 /* Check that things happened in the right order */
537 ut_asserteq_ptr(NULL, priv);
538 ut_assertok(device_probe(dev));
539
540 priv = dev_get_priv(dev);
541 ut_assert(priv != NULL);
542 ut_asserteq(1, priv->uclass_flag);
543 ut_asserteq(1, priv->uclass_total);
Simon Glass5104b982015-01-25 08:27:10 -0700544 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530545 ut_asserteq(3, device_get_child_count(bus));
Simon Glass5104b982015-01-25 08:27:10 -0700546
547 return 0;
548}
549DM_TEST(dm_test_bus_child_pre_probe_uclass,
550 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Bin Mengd9bad172018-10-15 02:20:58 -0700551
552/*
553 * Test that the bus' uclass' child_post_probe() is called after the
554 * device's probe() method
555 */
556static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts)
557{
558 struct udevice *bus, *dev;
Bin Mengd9bad172018-10-15 02:20:58 -0700559
560 /*
561 * See testfdt_drv_probe() which effectively initializes that
562 * the uclass postp flag is set to a value
563 */
564 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530565 for (device_find_first_child(bus, &dev);
Bin Mengd9bad172018-10-15 02:20:58 -0700566 dev;
567 device_find_next_child(&dev)) {
568 struct dm_test_priv *priv = dev_get_priv(dev);
569
570 /* Check that things happened in the right order */
571 ut_asserteq_ptr(NULL, priv);
572 ut_assertok(device_probe(dev));
573
574 priv = dev_get_priv(dev);
575 ut_assert(priv != NULL);
576 ut_asserteq(0, priv->uclass_postp);
Bin Mengd9bad172018-10-15 02:20:58 -0700577 }
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530578 ut_asserteq(3, device_get_child_count(bus));
Bin Mengd9bad172018-10-15 02:20:58 -0700579
580 return 0;
581}
582DM_TEST(dm_test_bus_child_post_probe_uclass,
583 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);