1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
12 #include <dm/device.h>
13 #include <dm/device-internal.h>
15 #include <dm/uclass-internal.h>
17 #include <test/test.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 struct dm_test_parent_plat {
29 FLAG_CHILD_PROBED = 10,
30 FLAG_CHILD_REMOVED = -7,
33 static struct dm_test_state *test_state;
35 static int testbus_drv_probe(struct udevice *dev)
37 return dm_scan_fdt_dev(dev);
40 static int testbus_child_post_bind(struct udevice *dev)
42 struct dm_test_parent_plat *plat;
44 plat = dev_get_parent_plat(dev);
46 plat->uclass_bind_flag = 2;
51 static int testbus_child_pre_probe(struct udevice *dev)
53 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
55 parent_data->flag += FLAG_CHILD_PROBED;
60 static int testbus_child_pre_probe_uclass(struct udevice *dev)
62 struct dm_test_priv *priv = dev_get_priv(dev);
69 static int testbus_child_post_probe_uclass(struct udevice *dev)
71 struct dm_test_priv *priv = dev_get_priv(dev);
78 static int testbus_child_post_remove(struct udevice *dev)
80 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
81 struct dm_test_state *dms = test_state;
83 parent_data->flag += FLAG_CHILD_REMOVED;
90 static const struct udevice_id testbus_ids[] = {
92 .compatible = "denx,u-boot-test-bus",
93 .data = DM_TEST_TYPE_FIRST },
97 U_BOOT_DRIVER(testbus_drv) = {
98 .name = "testbus_drv",
99 .of_match = testbus_ids,
100 .id = UCLASS_TEST_BUS,
101 .probe = testbus_drv_probe,
102 .child_post_bind = testbus_child_post_bind,
103 .priv_auto = sizeof(struct dm_test_priv),
104 .plat_auto = sizeof(struct dm_test_pdata),
105 .per_child_auto = sizeof(struct dm_test_parent_data),
106 .per_child_plat_auto = sizeof(struct dm_test_parent_plat),
107 .child_pre_probe = testbus_child_pre_probe,
108 .child_post_remove = testbus_child_post_remove,
111 UCLASS_DRIVER(testbus) = {
113 .id = UCLASS_TEST_BUS,
114 .flags = DM_UC_FLAG_SEQ_ALIAS,
115 .child_pre_probe = testbus_child_pre_probe_uclass,
116 .child_post_probe = testbus_child_post_probe_uclass,
119 /* Test that we can probe for children */
120 static int dm_test_bus_children(struct unit_test_state *uts)
126 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
127 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
129 /* Probe the bus, which should yield 3 more devices */
130 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
133 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
134 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
136 ut_assert(!dm_check_devices(uts, num_devices));
140 DM_TEST(dm_test_bus_children, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
142 /* Test our functions for accessing children */
143 static int dm_test_bus_children_funcs(struct unit_test_state *uts)
145 const void *blob = gd->fdt_blob;
146 struct udevice *bus, *dev;
149 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
151 /* device_get_child() */
152 ut_assertok(device_get_child(bus, 0, &dev));
153 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
154 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
155 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
156 ut_asserteq_str("c-test@5", dev->name);
158 /* Device with sequence number 0 should be accessible */
159 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, &dev));
160 ut_assertok(device_find_child_by_seq(bus, 0, &dev));
161 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
162 ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev));
163 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
164 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
165 ut_asserteq(0, device_find_child_by_seq(bus, 0, &dev));
167 /* There is no device with sequence number 2 */
168 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev));
169 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, &dev));
170 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
172 /* Looking for something that is not a child */
173 node = fdt_path_offset(blob, "/junk");
174 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
175 node = fdt_path_offset(blob, "/d-test");
176 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
180 DM_TEST(dm_test_bus_children_funcs, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
182 static int dm_test_bus_children_of_offset(struct unit_test_state *uts)
184 const void *blob = gd->fdt_blob;
185 struct udevice *bus, *dev;
188 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
189 ut_assertnonnull(bus);
191 /* Find a valid child */
192 node = fdt_path_offset(blob, "/some-bus/c-test@1");
194 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
195 ut_assertnonnull(dev);
196 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
197 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
198 ut_assertnonnull(dev);
199 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
203 DM_TEST(dm_test_bus_children_of_offset,
204 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
206 /* Test that we can iterate through children */
207 static int dm_test_bus_children_iterators(struct unit_test_state *uts)
209 struct udevice *bus, *dev, *child;
211 /* Walk through the children one by one */
212 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
213 ut_assertok(device_find_first_child(bus, &dev));
214 ut_asserteq_str("c-test@5", dev->name);
215 ut_assertok(device_find_next_child(&dev));
216 ut_asserteq_str("c-test@0", dev->name);
217 ut_assertok(device_find_next_child(&dev));
218 ut_asserteq_str("c-test@1", dev->name);
219 ut_assertok(device_find_next_child(&dev));
220 ut_asserteq_ptr(dev, NULL);
222 /* Move to the next child without using device_find_first_child() */
223 ut_assertok(device_find_child_by_seq(bus, 5, &dev));
224 ut_asserteq_str("c-test@5", dev->name);
225 ut_assertok(device_find_next_child(&dev));
226 ut_asserteq_str("c-test@0", dev->name);
228 /* Try a device with no children */
229 ut_assertok(device_find_first_child(dev, &child));
230 ut_asserteq_ptr(child, NULL);
234 DM_TEST(dm_test_bus_children_iterators,
235 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
237 /* Test that the bus can store data about each child */
238 static int test_bus_parent_data(struct unit_test_state *uts)
240 struct dm_test_parent_data *parent_data;
241 struct udevice *bus, *dev;
245 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
247 /* Check that parent data is allocated */
248 ut_assertok(device_find_child_by_seq(bus, 0, &dev));
249 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
250 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
251 parent_data = dev_get_parent_priv(dev);
252 ut_assert(NULL != parent_data);
254 /* Check that it starts at 0 and goes away when device is removed */
255 parent_data->sum += 5;
256 ut_asserteq(5, parent_data->sum);
257 device_remove(dev, DM_REMOVE_NORMAL);
258 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
260 /* Check that we can do this twice */
261 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
262 parent_data = dev_get_parent_priv(dev);
263 ut_assert(NULL != parent_data);
264 parent_data->sum += 5;
265 ut_asserteq(5, parent_data->sum);
267 /* Add parent data to all children */
268 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
270 uclass_foreach_dev(dev, uc) {
271 /* Ignore these if they are not on this bus */
272 if (dev->parent != bus) {
273 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
276 ut_assertok(device_probe(dev));
277 parent_data = dev_get_parent_priv(dev);
279 parent_data->sum = value;
283 /* Check it is still there */
285 uclass_foreach_dev(dev, uc) {
286 /* Ignore these if they are not on this bus */
287 if (dev->parent != bus)
289 parent_data = dev_get_parent_priv(dev);
291 ut_asserteq(value, parent_data->sum);
297 /* Test that the bus can store data about each child */
298 static int dm_test_bus_parent_data(struct unit_test_state *uts)
300 return test_bus_parent_data(uts);
302 DM_TEST(dm_test_bus_parent_data, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
304 /* As above but the size is controlled by the uclass */
305 static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
312 /* Set the driver size to 0 so that the uclass size is used */
313 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
314 drv = (struct driver *)bus->driver;
315 size = drv->per_child_auto;
317 #ifdef CONFIG_SANDBOX
318 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
319 os_mprotect_allow(drv, sizeof(*drv));
321 bus->uclass->uc_drv->per_child_auto = size;
322 drv->per_child_auto = 0;
323 ret = test_bus_parent_data(uts);
326 bus->uclass->uc_drv->per_child_auto = 0;
327 drv->per_child_auto = size;
331 DM_TEST(dm_test_bus_parent_data_uclass,
332 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
334 /* Test that the bus ops are called when a child is probed/removed */
335 static int dm_test_bus_parent_ops(struct unit_test_state *uts)
337 struct dm_test_parent_data *parent_data;
338 struct dm_test_state *dms = uts->priv;
339 struct udevice *bus, *dev;
343 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
344 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
346 uclass_foreach_dev(dev, uc) {
347 /* Ignore these if they are not on this bus */
348 if (dev->parent != bus)
350 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
352 ut_assertok(device_probe(dev));
353 parent_data = dev_get_parent_priv(dev);
354 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
357 uclass_foreach_dev(dev, uc) {
358 /* Ignore these if they are not on this bus */
359 if (dev->parent != bus)
361 parent_data = dev_get_parent_priv(dev);
362 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
363 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
364 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
365 ut_asserteq_ptr(dms->removed, dev);
371 DM_TEST(dm_test_bus_parent_ops, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
373 static int test_bus_parent_plat(struct unit_test_state *uts)
375 struct dm_test_parent_plat *plat;
376 struct udevice *bus, *dev;
378 /* Check that the bus has no children */
379 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
380 device_find_first_child(bus, &dev);
381 ut_asserteq_ptr(NULL, dev);
383 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
385 for (device_find_first_child(bus, &dev);
387 device_find_next_child(&dev)) {
388 /* Check that platform data is allocated */
389 plat = dev_get_parent_plat(dev);
390 ut_assert(plat != NULL);
393 * Check that it is not affected by the device being
397 ut_asserteq(1, plat->count);
399 device_remove(dev, DM_REMOVE_NORMAL);
401 ut_asserteq_ptr(plat, dev_get_parent_plat(dev));
402 ut_asserteq(1, plat->count);
403 ut_assertok(device_probe(dev));
405 ut_asserteq(3, device_get_child_count(bus));
407 /* Removing the bus should also have no effect (it is still bound) */
408 device_remove(bus, DM_REMOVE_NORMAL);
409 for (device_find_first_child(bus, &dev);
411 device_find_next_child(&dev)) {
412 /* Check that platform data is allocated */
413 plat = dev_get_parent_plat(dev);
414 ut_assert(plat != NULL);
415 ut_asserteq(1, plat->count);
417 ut_asserteq(3, device_get_child_count(bus));
419 /* Unbind all the children */
421 device_find_first_child(bus, &dev);
426 /* Now the child plat should be removed and re-added */
428 for (device_find_first_child(bus, &dev);
430 device_find_next_child(&dev)) {
431 /* Check that platform data is allocated */
432 plat = dev_get_parent_plat(dev);
433 ut_assert(plat != NULL);
434 ut_asserteq(0, plat->count);
436 ut_asserteq(3, device_get_child_count(bus));
441 /* Test that the bus can store platform data about each child */
442 static int dm_test_bus_parent_plat(struct unit_test_state *uts)
444 return test_bus_parent_plat(uts);
446 DM_TEST(dm_test_bus_parent_plat, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
448 /* As above but the size is controlled by the uclass */
449 static int dm_test_bus_parent_plat_uclass(struct unit_test_state *uts)
456 /* Set the driver size to 0 so that the uclass size is used */
457 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
458 drv = (struct driver *)bus->driver;
459 size = drv->per_child_plat_auto;
460 #ifdef CONFIG_SANDBOX
461 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
462 os_mprotect_allow(drv, sizeof(*drv));
464 bus->uclass->uc_drv->per_child_plat_auto = size;
465 drv->per_child_plat_auto = 0;
466 ret = test_bus_parent_plat(uts);
469 bus->uclass->uc_drv->per_child_plat_auto = 0;
470 drv->per_child_plat_auto = size;
474 DM_TEST(dm_test_bus_parent_plat_uclass,
475 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
477 /* Test that the child post_bind method is called */
478 static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
480 struct dm_test_parent_plat *plat;
481 struct udevice *bus, *dev;
483 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
484 for (device_find_first_child(bus, &dev);
486 device_find_next_child(&dev)) {
487 /* Check that platform data is allocated */
488 plat = dev_get_parent_plat(dev);
489 ut_assert(plat != NULL);
490 ut_asserteq(1, plat->bind_flag);
492 ut_asserteq(3, device_get_child_count(bus));
496 DM_TEST(dm_test_bus_child_post_bind, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
498 /* Test that the child post_bind method is called */
499 static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
501 struct dm_test_parent_plat *plat;
502 struct udevice *bus, *dev;
504 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
505 for (device_find_first_child(bus, &dev);
507 device_find_next_child(&dev)) {
508 /* Check that platform data is allocated */
509 plat = dev_get_parent_plat(dev);
510 ut_assert(plat != NULL);
511 ut_asserteq(2, plat->uclass_bind_flag);
513 ut_asserteq(3, device_get_child_count(bus));
517 DM_TEST(dm_test_bus_child_post_bind_uclass,
518 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
521 * Test that the bus' uclass' child_pre_probe() is called before the
522 * device's probe() method
524 static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
526 struct udevice *bus, *dev;
529 * See testfdt_drv_probe() which effectively checks that the uclass
530 * flag is set before that method is called
532 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
533 for (device_find_first_child(bus, &dev);
535 device_find_next_child(&dev)) {
536 struct dm_test_priv *priv = dev_get_priv(dev);
538 /* Check that things happened in the right order */
539 ut_asserteq_ptr(NULL, priv);
540 ut_assertok(device_probe(dev));
542 priv = dev_get_priv(dev);
543 ut_assert(priv != NULL);
544 ut_asserteq(1, priv->uclass_flag);
545 ut_asserteq(1, priv->uclass_total);
547 ut_asserteq(3, device_get_child_count(bus));
551 DM_TEST(dm_test_bus_child_pre_probe_uclass,
552 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
555 * Test that the bus' uclass' child_post_probe() is called after the
556 * device's probe() method
558 static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts)
560 struct udevice *bus, *dev;
563 * See testfdt_drv_probe() which effectively initializes that
564 * the uclass postp flag is set to a value
566 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
567 for (device_find_first_child(bus, &dev);
569 device_find_next_child(&dev)) {
570 struct dm_test_priv *priv = dev_get_priv(dev);
572 /* Check that things happened in the right order */
573 ut_asserteq_ptr(NULL, priv);
574 ut_assertok(device_probe(dev));
576 priv = dev_get_priv(dev);
577 ut_assert(priv != NULL);
578 ut_asserteq(0, priv->uclass_postp);
580 ut_asserteq(3, device_get_child_count(bus));
584 DM_TEST(dm_test_bus_child_post_probe_uclass,
585 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);