1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
11 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 struct dm_test_parent_platdata {
26 FLAG_CHILD_PROBED = 10,
27 FLAG_CHILD_REMOVED = -7,
30 static struct dm_test_state *test_state;
32 static int testbus_drv_probe(struct udevice *dev)
34 return dm_scan_fdt_dev(dev);
37 static int testbus_child_post_bind(struct udevice *dev)
39 struct dm_test_parent_platdata *plat;
41 plat = dev_get_parent_platdata(dev);
43 plat->uclass_bind_flag = 2;
48 static int testbus_child_pre_probe(struct udevice *dev)
50 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
52 parent_data->flag += FLAG_CHILD_PROBED;
57 static int testbus_child_pre_probe_uclass(struct udevice *dev)
59 struct dm_test_priv *priv = dev_get_priv(dev);
66 static int testbus_child_post_probe_uclass(struct udevice *dev)
68 struct dm_test_priv *priv = dev_get_priv(dev);
75 static int testbus_child_post_remove(struct udevice *dev)
77 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
78 struct dm_test_state *dms = test_state;
80 parent_data->flag += FLAG_CHILD_REMOVED;
87 static const struct udevice_id testbus_ids[] = {
89 .compatible = "denx,u-boot-test-bus",
90 .data = DM_TEST_TYPE_FIRST },
94 U_BOOT_DRIVER(testbus_drv) = {
95 .name = "testbus_drv",
96 .of_match = testbus_ids,
97 .id = UCLASS_TEST_BUS,
98 .probe = testbus_drv_probe,
99 .child_post_bind = testbus_child_post_bind,
100 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
101 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
102 .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
103 .per_child_platdata_auto_alloc_size =
104 sizeof(struct dm_test_parent_platdata),
105 .child_pre_probe = testbus_child_pre_probe,
106 .child_post_remove = testbus_child_post_remove,
109 UCLASS_DRIVER(testbus) = {
111 .id = UCLASS_TEST_BUS,
112 .flags = DM_UC_FLAG_SEQ_ALIAS,
113 .child_pre_probe = testbus_child_pre_probe_uclass,
114 .child_post_probe = testbus_child_post_probe_uclass,
117 /* Test that we can probe for children */
118 static int dm_test_bus_children(struct unit_test_state *uts)
124 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
125 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
127 /* Probe the bus, which should yield 3 more devices */
128 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
131 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
132 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
134 ut_assert(!dm_check_devices(uts, num_devices));
138 DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
140 /* Test our functions for accessing children */
141 static int dm_test_bus_children_funcs(struct unit_test_state *uts)
143 const void *blob = gd->fdt_blob;
144 struct udevice *bus, *dev;
147 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
149 /* device_get_child() */
150 ut_assertok(device_get_child(bus, 0, &dev));
151 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
152 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
153 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
154 ut_asserteq_str("c-test@5", dev->name);
156 /* Device with sequence number 0 should be accessible */
157 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
158 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
159 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
160 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
161 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
162 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
164 /* There is no device with sequence number 2 */
165 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
166 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
167 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
169 /* Looking for something that is not a child */
170 node = fdt_path_offset(blob, "/junk");
171 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
172 node = fdt_path_offset(blob, "/d-test");
173 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
177 DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
179 static int dm_test_bus_children_of_offset(struct unit_test_state *uts)
181 const void *blob = gd->fdt_blob;
182 struct udevice *bus, *dev;
185 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
186 ut_assertnonnull(bus);
188 /* Find a valid child */
189 node = fdt_path_offset(blob, "/some-bus/c-test@1");
191 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
192 ut_assertnonnull(dev);
193 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
194 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
195 ut_assertnonnull(dev);
196 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
200 DM_TEST(dm_test_bus_children_of_offset,
201 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
203 /* Test that we can iterate through children */
204 static int dm_test_bus_children_iterators(struct unit_test_state *uts)
206 struct udevice *bus, *dev, *child;
208 /* Walk through the children one by one */
209 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
210 ut_assertok(device_find_first_child(bus, &dev));
211 ut_asserteq_str("c-test@5", dev->name);
212 ut_assertok(device_find_next_child(&dev));
213 ut_asserteq_str("c-test@0", dev->name);
214 ut_assertok(device_find_next_child(&dev));
215 ut_asserteq_str("c-test@1", dev->name);
216 ut_assertok(device_find_next_child(&dev));
217 ut_asserteq_ptr(dev, NULL);
219 /* Move to the next child without using device_find_first_child() */
220 ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
221 ut_asserteq_str("c-test@5", dev->name);
222 ut_assertok(device_find_next_child(&dev));
223 ut_asserteq_str("c-test@0", dev->name);
225 /* Try a device with no children */
226 ut_assertok(device_find_first_child(dev, &child));
227 ut_asserteq_ptr(child, NULL);
231 DM_TEST(dm_test_bus_children_iterators,
232 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
234 /* Test that the bus can store data about each child */
235 static int test_bus_parent_data(struct unit_test_state *uts)
237 struct dm_test_parent_data *parent_data;
238 struct udevice *bus, *dev;
242 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
244 /* Check that parent data is allocated */
245 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
246 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
247 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
248 parent_data = dev_get_parent_priv(dev);
249 ut_assert(NULL != parent_data);
251 /* Check that it starts at 0 and goes away when device is removed */
252 parent_data->sum += 5;
253 ut_asserteq(5, parent_data->sum);
254 device_remove(dev, DM_REMOVE_NORMAL);
255 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
257 /* Check that we can do this twice */
258 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
259 parent_data = dev_get_parent_priv(dev);
260 ut_assert(NULL != parent_data);
261 parent_data->sum += 5;
262 ut_asserteq(5, parent_data->sum);
264 /* Add parent data to all children */
265 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
267 uclass_foreach_dev(dev, uc) {
268 /* Ignore these if they are not on this bus */
269 if (dev->parent != bus) {
270 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
273 ut_assertok(device_probe(dev));
274 parent_data = dev_get_parent_priv(dev);
276 parent_data->sum = value;
280 /* Check it is still there */
282 uclass_foreach_dev(dev, uc) {
283 /* Ignore these if they are not on this bus */
284 if (dev->parent != bus)
286 parent_data = dev_get_parent_priv(dev);
288 ut_asserteq(value, parent_data->sum);
294 /* Test that the bus can store data about each child */
295 static int dm_test_bus_parent_data(struct unit_test_state *uts)
297 return test_bus_parent_data(uts);
299 DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
301 /* As above but the size is controlled by the uclass */
302 static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
309 /* Set the driver size to 0 so that the uclass size is used */
310 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
311 drv = (struct driver *)bus->driver;
312 size = drv->per_child_auto_alloc_size;
314 #ifdef CONFIG_SANDBOX
315 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
316 os_mprotect_allow(drv, sizeof(*drv));
318 bus->uclass->uc_drv->per_child_auto_alloc_size = size;
319 drv->per_child_auto_alloc_size = 0;
320 ret = test_bus_parent_data(uts);
323 bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
324 drv->per_child_auto_alloc_size = size;
328 DM_TEST(dm_test_bus_parent_data_uclass,
329 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
331 /* Test that the bus ops are called when a child is probed/removed */
332 static int dm_test_bus_parent_ops(struct unit_test_state *uts)
334 struct dm_test_parent_data *parent_data;
335 struct dm_test_state *dms = uts->priv;
336 struct udevice *bus, *dev;
340 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
341 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
343 uclass_foreach_dev(dev, uc) {
344 /* Ignore these if they are not on this bus */
345 if (dev->parent != bus)
347 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
349 ut_assertok(device_probe(dev));
350 parent_data = dev_get_parent_priv(dev);
351 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
354 uclass_foreach_dev(dev, uc) {
355 /* Ignore these if they are not on this bus */
356 if (dev->parent != bus)
358 parent_data = dev_get_parent_priv(dev);
359 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
360 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
361 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
362 ut_asserteq_ptr(dms->removed, dev);
368 DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
370 static int test_bus_parent_platdata(struct unit_test_state *uts)
372 struct dm_test_parent_platdata *plat;
373 struct udevice *bus, *dev;
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);
381 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
383 for (device_find_first_child(bus, &dev), child_count = 0;
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);
391 * Check that it is not affected by the device being
395 ut_asserteq(1, plat->count);
397 device_remove(dev, DM_REMOVE_NORMAL);
399 ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
400 ut_asserteq(1, plat->count);
401 ut_assertok(device_probe(dev));
404 ut_asserteq(3, child_count);
406 /* Removing the bus should also have no effect (it is still bound) */
407 device_remove(bus, DM_REMOVE_NORMAL);
408 for (device_find_first_child(bus, &dev), child_count = 0;
410 device_find_next_child(&dev)) {
411 /* Check that platform data is allocated */
412 plat = dev_get_parent_platdata(dev);
413 ut_assert(plat != NULL);
414 ut_asserteq(1, plat->count);
417 ut_asserteq(3, child_count);
419 /* Unbind all the children */
421 device_find_first_child(bus, &dev);
426 /* Now the child platdata should be removed and re-added */
428 for (device_find_first_child(bus, &dev), child_count = 0;
430 device_find_next_child(&dev)) {
431 /* Check that platform data is allocated */
432 plat = dev_get_parent_platdata(dev);
433 ut_assert(plat != NULL);
434 ut_asserteq(0, plat->count);
437 ut_asserteq(3, child_count);
442 /* Test that the bus can store platform data about each child */
443 static int dm_test_bus_parent_platdata(struct unit_test_state *uts)
445 return test_bus_parent_platdata(uts);
447 DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
449 /* As above but the size is controlled by the uclass */
450 static int dm_test_bus_parent_platdata_uclass(struct unit_test_state *uts)
457 /* Set the driver size to 0 so that the uclass size is used */
458 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
459 drv = (struct driver *)bus->driver;
460 size = drv->per_child_platdata_auto_alloc_size;
461 #ifdef CONFIG_SANDBOX
462 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
463 os_mprotect_allow(drv, sizeof(*drv));
465 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
466 drv->per_child_platdata_auto_alloc_size = 0;
467 ret = test_bus_parent_platdata(uts);
470 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
471 drv->per_child_platdata_auto_alloc_size = size;
475 DM_TEST(dm_test_bus_parent_platdata_uclass,
476 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
478 /* Test that the child post_bind method is called */
479 static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
481 struct dm_test_parent_platdata *plat;
482 struct udevice *bus, *dev;
485 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
486 for (device_find_first_child(bus, &dev), child_count = 0;
488 device_find_next_child(&dev)) {
489 /* Check that platform data is allocated */
490 plat = dev_get_parent_platdata(dev);
491 ut_assert(plat != NULL);
492 ut_asserteq(1, plat->bind_flag);
495 ut_asserteq(3, child_count);
499 DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
501 /* Test that the child post_bind method is called */
502 static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
504 struct dm_test_parent_platdata *plat;
505 struct udevice *bus, *dev;
508 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
509 for (device_find_first_child(bus, &dev), child_count = 0;
511 device_find_next_child(&dev)) {
512 /* Check that platform data is allocated */
513 plat = dev_get_parent_platdata(dev);
514 ut_assert(plat != NULL);
515 ut_asserteq(2, plat->uclass_bind_flag);
518 ut_asserteq(3, child_count);
522 DM_TEST(dm_test_bus_child_post_bind_uclass,
523 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
526 * Test that the bus' uclass' child_pre_probe() is called before the
527 * device's probe() method
529 static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
531 struct udevice *bus, *dev;
535 * See testfdt_drv_probe() which effectively checks that the uclass
536 * flag is set before that method is called
538 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
539 for (device_find_first_child(bus, &dev), child_count = 0;
541 device_find_next_child(&dev)) {
542 struct dm_test_priv *priv = dev_get_priv(dev);
544 /* Check that things happened in the right order */
545 ut_asserteq_ptr(NULL, priv);
546 ut_assertok(device_probe(dev));
548 priv = dev_get_priv(dev);
549 ut_assert(priv != NULL);
550 ut_asserteq(1, priv->uclass_flag);
551 ut_asserteq(1, priv->uclass_total);
554 ut_asserteq(3, child_count);
558 DM_TEST(dm_test_bus_child_pre_probe_uclass,
559 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
562 * Test that the bus' uclass' child_post_probe() is called after the
563 * device's probe() method
565 static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts)
567 struct udevice *bus, *dev;
571 * See testfdt_drv_probe() which effectively initializes that
572 * the uclass postp flag is set to a value
574 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
575 for (device_find_first_child(bus, &dev), child_count = 0;
577 device_find_next_child(&dev)) {
578 struct dm_test_priv *priv = dev_get_priv(dev);
580 /* Check that things happened in the right order */
581 ut_asserteq_ptr(NULL, priv);
582 ut_assertok(device_probe(dev));
584 priv = dev_get_priv(dev);
585 ut_assert(priv != NULL);
586 ut_asserteq(0, priv->uclass_postp);
589 ut_asserteq(3, child_count);
593 DM_TEST(dm_test_bus_child_post_probe_uclass,
594 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);