1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for the core driver model code
5 * Copyright (c) 2013 Google, Inc
14 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
21 DECLARE_GLOBAL_DATA_PTR;
27 TEST_INTVAL_MANUAL = 101112,
28 TEST_INTVAL_PRE_RELOC = 7,
31 static const struct dm_test_pdata test_pdata[] = {
32 { .ping_add = TEST_INTVAL1, },
33 { .ping_add = TEST_INTVAL2, },
34 { .ping_add = TEST_INTVAL3, },
37 static const struct dm_test_pdata test_pdata_manual = {
38 .ping_add = TEST_INTVAL_MANUAL,
41 static const struct dm_test_pdata test_pdata_pre_reloc = {
42 .ping_add = TEST_INTVAL_PRE_RELOC,
45 U_BOOT_DEVICE(dm_test_info1) = {
47 .platdata = &test_pdata[0],
50 U_BOOT_DEVICE(dm_test_info2) = {
52 .platdata = &test_pdata[1],
55 U_BOOT_DEVICE(dm_test_info3) = {
57 .platdata = &test_pdata[2],
60 static struct driver_info driver_info_manual = {
61 .name = "test_manual_drv",
62 .platdata = &test_pdata_manual,
65 static struct driver_info driver_info_pre_reloc = {
66 .name = "test_pre_reloc_drv",
67 .platdata = &test_pdata_pre_reloc,
70 static struct driver_info driver_info_act_dma = {
71 .name = "test_act_dma_drv",
74 void dm_leak_check_start(struct unit_test_state *uts)
76 uts->start = mallinfo();
77 if (!uts->start.uordblks)
78 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
81 int dm_leak_check_end(struct unit_test_state *uts)
86 /* Don't delete the root class, since we started with that */
87 for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
93 ut_assertok(uclass_destroy(uc));
97 diff = end.uordblks - uts->start.uordblks;
99 printf("Leak: lost %#xd bytes\n", diff);
101 printf("Leak: gained %#xd bytes\n", -diff);
102 ut_asserteq(uts->start.uordblks, end.uordblks);
107 /* Test that binding with platdata occurs correctly */
108 static int dm_test_autobind(struct unit_test_state *uts)
110 struct dm_test_state *dms = uts->priv;
114 * We should have a single class (UCLASS_ROOT) and a single root
115 * device with no children.
117 ut_assert(dms->root);
118 ut_asserteq(1, list_count_items(&gd->uclass_root));
119 ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
120 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
122 ut_assertok(dm_scan_platdata(false));
124 /* We should have our test class now at least, plus more children */
125 ut_assert(1 < list_count_items(&gd->uclass_root));
126 ut_assert(0 < list_count_items(&gd->dm_root->child_head));
128 /* Our 3 dm_test_infox children should be bound to the test uclass */
129 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
131 /* No devices should be probed */
132 list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
133 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
135 /* Our test driver should have been bound 3 times */
136 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
140 DM_TEST(dm_test_autobind, 0);
142 /* Test that binding with uclass platdata allocation occurs correctly */
143 static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
145 struct dm_test_perdev_uc_pdata *uc_pdata;
149 ut_assertok(uclass_get(UCLASS_TEST, &uc));
153 * Test if test uclass driver requires allocation for the uclass
154 * platform data and then check the dev->uclass_platdata pointer.
156 ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size);
158 for (uclass_find_first_device(UCLASS_TEST, &dev);
160 uclass_find_next_device(&dev)) {
163 uc_pdata = dev_get_uclass_platdata(dev);
169 DM_TEST(dm_test_autobind_uclass_pdata_alloc, DM_TESTF_SCAN_PDATA);
171 /* Test that binding with uclass platdata setting occurs correctly */
172 static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
174 struct dm_test_perdev_uc_pdata *uc_pdata;
178 * In the test_postbind() method of test uclass driver, the uclass
179 * platform data should be set to three test int values - test it.
181 for (uclass_find_first_device(UCLASS_TEST, &dev);
183 uclass_find_next_device(&dev)) {
186 uc_pdata = dev_get_uclass_platdata(dev);
188 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
189 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
190 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
195 DM_TEST(dm_test_autobind_uclass_pdata_valid, DM_TESTF_SCAN_PDATA);
197 /* Test that autoprobe finds all the expected devices */
198 static int dm_test_autoprobe(struct unit_test_state *uts)
200 struct dm_test_state *dms = uts->priv;
201 int expected_base_add;
206 ut_assertok(uclass_get(UCLASS_TEST, &uc));
209 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
210 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
211 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
213 /* The root device should not be activated until needed */
214 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
217 * We should be able to find the three test devices, and they should
218 * all be activated as they are used (lazy activation, required by
221 for (i = 0; i < 3; i++) {
222 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
224 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
225 "Driver %d/%s already activated", i, dev->name);
227 /* This should activate it */
228 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
230 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
232 /* Activating a device should activate the root device */
234 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
238 * Our 3 dm_test_info children should be passed to pre_probe and
241 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
242 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
244 /* Also we can check the per-device data */
245 expected_base_add = 0;
246 for (i = 0; i < 3; i++) {
247 struct dm_test_uclass_perdev_priv *priv;
248 struct dm_test_pdata *pdata;
250 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
253 priv = dev_get_uclass_priv(dev);
255 ut_asserteq(expected_base_add, priv->base_add);
257 pdata = dev->platdata;
258 expected_base_add += pdata->ping_add;
263 DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA);
265 /* Check that we see the correct platdata in each device */
266 static int dm_test_platdata(struct unit_test_state *uts)
268 const struct dm_test_pdata *pdata;
272 for (i = 0; i < 3; i++) {
273 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
275 pdata = dev->platdata;
276 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
281 DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA);
283 /* Test that we can bind, probe, remove, unbind a driver */
284 static int dm_test_lifecycle(struct unit_test_state *uts)
286 struct dm_test_state *dms = uts->priv;
287 int op_count[DM_TEST_OP_COUNT];
288 struct udevice *dev, *test_dev;
292 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
294 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
297 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
298 == op_count[DM_TEST_OP_BIND] + 1);
299 ut_assert(!dev->priv);
301 /* Probe the device - it should fail allocating private data */
302 dms->force_fail_alloc = 1;
303 ret = device_probe(dev);
304 ut_assert(ret == -ENOMEM);
305 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
306 == op_count[DM_TEST_OP_PROBE] + 1);
307 ut_assert(!dev->priv);
309 /* Try again without the alloc failure */
310 dms->force_fail_alloc = 0;
311 ut_assertok(device_probe(dev));
312 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
313 == op_count[DM_TEST_OP_PROBE] + 2);
314 ut_assert(dev->priv);
316 /* This should be device 3 in the uclass */
317 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
318 ut_assert(dev == test_dev);
321 ut_assertok(test_ping(dev, 100, &pingret));
322 ut_assert(pingret == 102);
324 /* Now remove device 3 */
325 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
326 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
327 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
329 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
330 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
331 ut_assertok(device_unbind(dev));
332 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
333 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
337 DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
339 /* Test that we can bind/unbind and the lists update correctly */
340 static int dm_test_ordering(struct unit_test_state *uts)
342 struct dm_test_state *dms = uts->priv;
343 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
346 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
350 /* Bind two new devices (numbers 4 and 5) */
351 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
353 ut_assert(dev_penultimate);
354 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
358 /* Now remove device 3 */
359 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
360 ut_assertok(device_unbind(dev));
362 /* The device numbering should have shifted down one */
363 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
364 ut_assert(dev_penultimate == test_dev);
365 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
366 ut_assert(dev_last == test_dev);
368 /* Add back the original device 3, now in position 5 */
369 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
374 ut_assertok(test_ping(dev, 100, &pingret));
375 ut_assert(pingret == 102);
378 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
379 ut_assertok(device_unbind(dev_penultimate));
380 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
381 ut_assertok(device_unbind(dev_last));
383 /* Our device should now be in position 3 */
384 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
385 ut_assert(dev == test_dev);
387 /* Now remove device 3 */
388 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
389 ut_assertok(device_unbind(dev));
393 DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA);
395 /* Check that we can perform operations on a device (do a ping) */
396 int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
397 uint32_t base, struct dm_test_priv *priv)
402 /* Getting the child device should allocate platdata / priv */
403 ut_assertok(testfdt_ping(dev, 10, &pingret));
404 ut_assert(dev->priv);
405 ut_assert(dev->platdata);
407 expected = 10 + base;
408 ut_asserteq(expected, pingret);
410 /* Do another ping */
411 ut_assertok(testfdt_ping(dev, 20, &pingret));
412 expected = 20 + base;
413 ut_asserteq(expected, pingret);
415 /* Now check the ping_total */
417 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
423 /* Check that we can perform operations on devices */
424 static int dm_test_operations(struct unit_test_state *uts)
430 * Now check that the ping adds are what we expect. This is using the
431 * ping-add property in each node.
433 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
436 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
439 * Get the 'reg' property, which tells us what the ping add
440 * should be. We don't use the platdata because we want
441 * to test the code that sets that up (testfdt_drv_probe()).
443 base = test_pdata[i].ping_add;
444 debug("dev=%d, base=%d\n", i, base);
446 ut_assert(!dm_check_operations(uts, dev, base, dev->priv));
451 DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA);
453 /* Remove all drivers and check that things work */
454 static int dm_test_remove(struct unit_test_state *uts)
459 for (i = 0; i < 3; i++) {
460 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
462 ut_assertf(dev->flags & DM_FLAG_ACTIVATED,
463 "Driver %d/%s not activated", i, dev->name);
464 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
465 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
466 "Driver %d/%s should have deactivated", i,
468 ut_assert(!dev->priv);
473 DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
475 /* Remove and recreate everything, check for memory leaks */
476 static int dm_test_leak(struct unit_test_state *uts)
480 for (i = 0; i < 2; i++) {
485 dm_leak_check_start(uts);
487 ut_assertok(dm_scan_platdata(false));
488 ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
490 /* Scanning the uclass is enough to probe all the devices */
491 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
492 for (ret = uclass_first_device(UCLASS_TEST, &dev);
494 ret = uclass_next_device(&dev))
499 ut_assertok(dm_leak_check_end(uts));
504 DM_TEST(dm_test_leak, 0);
506 /* Test uclass init/destroy methods */
507 static int dm_test_uclass(struct unit_test_state *uts)
511 ut_assertok(uclass_get(UCLASS_TEST, &uc));
512 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
513 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
516 ut_assertok(uclass_destroy(uc));
517 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
518 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
522 DM_TEST(dm_test_uclass, 0);
525 * create_children() - Create children of a parent node
527 * @dms: Test system state
528 * @parent: Parent device
529 * @count: Number of children to create
530 * @key: Key value to put in first child. Subsequence children
531 * receive an incrementing value
532 * @child: If not NULL, then the child device pointers are written into
534 * @return 0 if OK, -ve on error
536 static int create_children(struct unit_test_state *uts, struct udevice *parent,
537 int count, int key, struct udevice *child[])
542 for (i = 0; i < count; i++) {
543 struct dm_test_pdata *pdata;
545 ut_assertok(device_bind_by_name(parent, false,
546 &driver_info_manual, &dev));
547 pdata = calloc(1, sizeof(*pdata));
548 pdata->ping_add = key + i;
549 dev->platdata = pdata;
557 #define NODE_COUNT 10
559 static int dm_test_children(struct unit_test_state *uts)
561 struct dm_test_state *dms = uts->priv;
562 struct udevice *top[NODE_COUNT];
563 struct udevice *child[NODE_COUNT];
564 struct udevice *grandchild[NODE_COUNT];
570 /* We don't care about the numbering for this test */
571 dms->skip_post_probe = 1;
573 ut_assert(NODE_COUNT > 5);
575 /* First create 10 top-level children */
576 ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
578 /* Now a few have their own children */
579 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
580 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
582 /* And grandchildren */
583 for (i = 0; i < NODE_COUNT; i++)
584 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
585 i == 2 ? grandchild : NULL));
587 /* Check total number of devices */
588 total = NODE_COUNT * (3 + NODE_COUNT);
589 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
591 /* Try probing one of the grandchildren */
592 ut_assertok(uclass_get_device(UCLASS_TEST,
593 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
594 ut_asserteq_ptr(grandchild[0], dev);
597 * This should have probed the child and top node also, for a total
600 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
602 /* Probe the other grandchildren */
603 for (i = 1; i < NODE_COUNT; i++)
604 ut_assertok(device_probe(grandchild[i]));
606 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
608 /* Probe everything */
609 for (ret = uclass_first_device(UCLASS_TEST, &dev);
611 ret = uclass_next_device(&dev))
615 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
617 /* Remove a top-level child and check that the children are removed */
618 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
619 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
620 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
622 /* Try one with grandchildren */
623 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
624 ut_asserteq_ptr(dev, top[5]);
625 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
626 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
627 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
629 /* Try the same with unbind */
630 ut_assertok(device_unbind(top[2]));
631 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
632 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
634 /* Try one with grandchildren */
635 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
636 ut_asserteq_ptr(dev, top[6]);
637 ut_assertok(device_unbind(top[5]));
638 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
639 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
643 DM_TEST(dm_test_children, 0);
645 /* Test that pre-relocation devices work as expected */
646 static int dm_test_pre_reloc(struct unit_test_state *uts)
648 struct dm_test_state *dms = uts->priv;
651 /* The normal driver should refuse to bind before relocation */
652 ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
653 &driver_info_manual, &dev));
655 /* But this one is marked pre-reloc */
656 ut_assertok(device_bind_by_name(dms->root, true,
657 &driver_info_pre_reloc, &dev));
661 DM_TEST(dm_test_pre_reloc, 0);
664 * Test that removal of devices, either via the "normal" device_remove()
665 * API or via the device driver selective flag works as expected
667 static int dm_test_remove_active_dma(struct unit_test_state *uts)
669 struct dm_test_state *dms = uts->priv;
672 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
676 /* Probe the device */
677 ut_assertok(device_probe(dev));
679 /* Test if device is active right now */
680 ut_asserteq(true, device_active(dev));
682 /* Remove the device via selective remove flag */
683 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
685 /* Test if device is inactive right now */
686 ut_asserteq(false, device_active(dev));
688 /* Probe the device again */
689 ut_assertok(device_probe(dev));
691 /* Test if device is active right now */
692 ut_asserteq(true, device_active(dev));
694 /* Remove the device via "normal" remove API */
695 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
697 /* Test if device is inactive right now */
698 ut_asserteq(false, device_active(dev));
701 * Test if a device without the active DMA flags is not removed upon
702 * the active DMA remove call
704 ut_assertok(device_unbind(dev));
705 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
709 /* Probe the device */
710 ut_assertok(device_probe(dev));
712 /* Test if device is active right now */
713 ut_asserteq(true, device_active(dev));
715 /* Remove the device via selective remove flag */
716 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
718 /* Test if device is still active right now */
719 ut_asserteq(true, device_active(dev));
723 DM_TEST(dm_test_remove_active_dma, 0);
725 static int dm_test_uclass_before_ready(struct unit_test_state *uts)
729 ut_assertok(uclass_get(UCLASS_TEST, &uc));
732 gd->dm_root_f = NULL;
733 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
735 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
739 DM_TEST(dm_test_uclass_before_ready, 0);
741 static int dm_test_uclass_devices_find(struct unit_test_state *uts)
746 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
748 ret = uclass_find_next_device(&dev)) {
753 ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
758 DM_TEST(dm_test_uclass_devices_find, DM_TESTF_SCAN_PDATA);
760 static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
762 struct udevice *finddev;
763 struct udevice *testdev;
767 * For each test device found in fdt like: "a-test", "b-test", etc.,
768 * use its name and try to find it by uclass_find_device_by_name().
769 * Then, on success check if:
770 * - current 'testdev' name is equal to the returned 'finddev' name
771 * - current 'testdev' pointer is equal to the returned 'finddev'
773 * We assume that, each uclass's device name is unique, so if not, then
774 * this will fail on checking condition: testdev == finddev, since the
775 * uclass_find_device_by_name(), returns the first device by given name.
777 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
779 ret = uclass_find_next_device(&testdev)) {
783 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
787 ut_assertok(findret);
789 ut_asserteq_str(testdev->name, finddev->name);
790 ut_asserteq_ptr(testdev, finddev);
795 DM_TEST(dm_test_uclass_devices_find_by_name, DM_TESTF_SCAN_FDT);
797 static int dm_test_uclass_devices_get(struct unit_test_state *uts)
802 for (ret = uclass_first_device(UCLASS_TEST, &dev);
804 ret = uclass_next_device(&dev)) {
807 ut_assert(device_active(dev));
812 DM_TEST(dm_test_uclass_devices_get, DM_TESTF_SCAN_PDATA);
814 static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
816 struct udevice *finddev;
817 struct udevice *testdev;
821 * For each test device found in fdt like: "a-test", "b-test", etc.,
822 * use its name and try to get it by uclass_get_device_by_name().
823 * On success check if:
824 * - returned finddev' is active
825 * - current 'testdev' name is equal to the returned 'finddev' name
826 * - current 'testdev' pointer is equal to the returned 'finddev'
828 * We asserts that the 'testdev' is active on each loop entry, so we
829 * could be sure that the 'finddev' is activated too, but for sure
832 * We assume that, each uclass's device name is unique, so if not, then
833 * this will fail on checking condition: testdev == finddev, since the
834 * uclass_get_device_by_name(), returns the first device by given name.
836 for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
838 ret = uclass_next_device(&testdev)) {
841 ut_assert(device_active(testdev));
843 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
847 ut_assertok(findret);
849 ut_assert(device_active(finddev));
850 ut_asserteq_str(testdev->name, finddev->name);
851 ut_asserteq_ptr(testdev, finddev);
856 DM_TEST(dm_test_uclass_devices_get_by_name, DM_TESTF_SCAN_FDT);
858 static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
862 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
863 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
867 DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);
869 static int dm_test_uclass_names(struct unit_test_state *uts)
871 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
872 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
876 DM_TEST(dm_test_uclass_names, DM_TESTF_SCAN_PDATA);
878 static int dm_test_inactive_child(struct unit_test_state *uts)
880 struct dm_test_state *dms = uts->priv;
881 struct udevice *parent, *dev1, *dev2;
883 /* Skip the behaviour in test_post_probe() */
884 dms->skip_post_probe = 1;
886 ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
889 * Create a child but do not activate it. Calling the function again
890 * should return the same child.
892 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
893 UCLASS_TEST, &dev1));
894 ut_assertok(device_bind_ofnode(parent, DM_GET_DRIVER(test_drv),
895 "test_child", 0, ofnode_null(), &dev1));
897 ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
899 ut_asserteq_ptr(dev1, dev2);
901 ut_assertok(device_probe(dev1));
902 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
903 UCLASS_TEST, &dev2));
907 DM_TEST(dm_test_inactive_child, DM_TESTF_SCAN_PDATA);