1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for the core driver model code
5 * Copyright (c) 2013 Google, Inc
13 #include <dm/device-internal.h>
17 #include <dm/uclass-internal.h>
20 DECLARE_GLOBAL_DATA_PTR;
26 TEST_INTVAL_MANUAL = 101112,
27 TEST_INTVAL_PRE_RELOC = 7,
30 static const struct dm_test_pdata test_pdata[] = {
31 { .ping_add = TEST_INTVAL1, },
32 { .ping_add = TEST_INTVAL2, },
33 { .ping_add = TEST_INTVAL3, },
36 static const struct dm_test_pdata test_pdata_manual = {
37 .ping_add = TEST_INTVAL_MANUAL,
40 static const struct dm_test_pdata test_pdata_pre_reloc = {
41 .ping_add = TEST_INTVAL_PRE_RELOC,
44 U_BOOT_DEVICE(dm_test_info1) = {
46 .platdata = &test_pdata[0],
49 U_BOOT_DEVICE(dm_test_info2) = {
51 .platdata = &test_pdata[1],
54 U_BOOT_DEVICE(dm_test_info3) = {
56 .platdata = &test_pdata[2],
59 static struct driver_info driver_info_manual = {
60 .name = "test_manual_drv",
61 .platdata = &test_pdata_manual,
64 static struct driver_info driver_info_pre_reloc = {
65 .name = "test_pre_reloc_drv",
66 .platdata = &test_pdata_pre_reloc,
69 static struct driver_info driver_info_act_dma = {
70 .name = "test_act_dma_drv",
73 void dm_leak_check_start(struct unit_test_state *uts)
75 uts->start = mallinfo();
76 if (!uts->start.uordblks)
77 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
80 int dm_leak_check_end(struct unit_test_state *uts)
85 /* Don't delete the root class, since we started with that */
86 for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
92 ut_assertok(uclass_destroy(uc));
96 diff = end.uordblks - uts->start.uordblks;
98 printf("Leak: lost %#xd bytes\n", diff);
100 printf("Leak: gained %#xd bytes\n", -diff);
101 ut_asserteq(uts->start.uordblks, end.uordblks);
106 /* Test that binding with platdata occurs correctly */
107 static int dm_test_autobind(struct unit_test_state *uts)
109 struct dm_test_state *dms = uts->priv;
113 * We should have a single class (UCLASS_ROOT) and a single root
114 * device with no children.
116 ut_assert(dms->root);
117 ut_asserteq(1, list_count_items(&gd->uclass_root));
118 ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
119 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
121 ut_assertok(dm_scan_platdata(false));
123 /* We should have our test class now at least, plus more children */
124 ut_assert(1 < list_count_items(&gd->uclass_root));
125 ut_assert(0 < list_count_items(&gd->dm_root->child_head));
127 /* Our 3 dm_test_infox children should be bound to the test uclass */
128 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
130 /* No devices should be probed */
131 list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
132 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
134 /* Our test driver should have been bound 3 times */
135 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
139 DM_TEST(dm_test_autobind, 0);
141 /* Test that binding with uclass platdata allocation occurs correctly */
142 static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
144 struct dm_test_perdev_uc_pdata *uc_pdata;
148 ut_assertok(uclass_get(UCLASS_TEST, &uc));
152 * Test if test uclass driver requires allocation for the uclass
153 * platform data and then check the dev->uclass_platdata pointer.
155 ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size);
157 for (uclass_find_first_device(UCLASS_TEST, &dev);
159 uclass_find_next_device(&dev)) {
162 uc_pdata = dev_get_uclass_platdata(dev);
168 DM_TEST(dm_test_autobind_uclass_pdata_alloc, DM_TESTF_SCAN_PDATA);
170 /* Test that binding with uclass platdata setting occurs correctly */
171 static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
173 struct dm_test_perdev_uc_pdata *uc_pdata;
177 * In the test_postbind() method of test uclass driver, the uclass
178 * platform data should be set to three test int values - test it.
180 for (uclass_find_first_device(UCLASS_TEST, &dev);
182 uclass_find_next_device(&dev)) {
185 uc_pdata = dev_get_uclass_platdata(dev);
187 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
188 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
189 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
194 DM_TEST(dm_test_autobind_uclass_pdata_valid, DM_TESTF_SCAN_PDATA);
196 /* Test that autoprobe finds all the expected devices */
197 static int dm_test_autoprobe(struct unit_test_state *uts)
199 struct dm_test_state *dms = uts->priv;
200 int expected_base_add;
205 ut_assertok(uclass_get(UCLASS_TEST, &uc));
208 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
209 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
210 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
212 /* The root device should not be activated until needed */
213 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
216 * We should be able to find the three test devices, and they should
217 * all be activated as they are used (lazy activation, required by
220 for (i = 0; i < 3; i++) {
221 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
223 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
224 "Driver %d/%s already activated", i, dev->name);
226 /* This should activate it */
227 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
229 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
231 /* Activating a device should activate the root device */
233 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
237 * Our 3 dm_test_info children should be passed to pre_probe and
240 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
241 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
243 /* Also we can check the per-device data */
244 expected_base_add = 0;
245 for (i = 0; i < 3; i++) {
246 struct dm_test_uclass_perdev_priv *priv;
247 struct dm_test_pdata *pdata;
249 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
252 priv = dev_get_uclass_priv(dev);
254 ut_asserteq(expected_base_add, priv->base_add);
256 pdata = dev->platdata;
257 expected_base_add += pdata->ping_add;
262 DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA);
264 /* Check that we see the correct platdata in each device */
265 static int dm_test_platdata(struct unit_test_state *uts)
267 const struct dm_test_pdata *pdata;
271 for (i = 0; i < 3; i++) {
272 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
274 pdata = dev->platdata;
275 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
280 DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA);
282 /* Test that we can bind, probe, remove, unbind a driver */
283 static int dm_test_lifecycle(struct unit_test_state *uts)
285 struct dm_test_state *dms = uts->priv;
286 int op_count[DM_TEST_OP_COUNT];
287 struct udevice *dev, *test_dev;
291 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
293 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
296 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
297 == op_count[DM_TEST_OP_BIND] + 1);
298 ut_assert(!dev->priv);
300 /* Probe the device - it should fail allocating private data */
301 dms->force_fail_alloc = 1;
302 ret = device_probe(dev);
303 ut_assert(ret == -ENOMEM);
304 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
305 == op_count[DM_TEST_OP_PROBE] + 1);
306 ut_assert(!dev->priv);
308 /* Try again without the alloc failure */
309 dms->force_fail_alloc = 0;
310 ut_assertok(device_probe(dev));
311 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
312 == op_count[DM_TEST_OP_PROBE] + 2);
313 ut_assert(dev->priv);
315 /* This should be device 3 in the uclass */
316 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
317 ut_assert(dev == test_dev);
320 ut_assertok(test_ping(dev, 100, &pingret));
321 ut_assert(pingret == 102);
323 /* Now remove device 3 */
324 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
325 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
326 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
328 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
329 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
330 ut_assertok(device_unbind(dev));
331 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
332 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
336 DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
338 /* Test that we can bind/unbind and the lists update correctly */
339 static int dm_test_ordering(struct unit_test_state *uts)
341 struct dm_test_state *dms = uts->priv;
342 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
345 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
349 /* Bind two new devices (numbers 4 and 5) */
350 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
352 ut_assert(dev_penultimate);
353 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
357 /* Now remove device 3 */
358 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
359 ut_assertok(device_unbind(dev));
361 /* The device numbering should have shifted down one */
362 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
363 ut_assert(dev_penultimate == test_dev);
364 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
365 ut_assert(dev_last == test_dev);
367 /* Add back the original device 3, now in position 5 */
368 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
373 ut_assertok(test_ping(dev, 100, &pingret));
374 ut_assert(pingret == 102);
377 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
378 ut_assertok(device_unbind(dev_penultimate));
379 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
380 ut_assertok(device_unbind(dev_last));
382 /* Our device should now be in position 3 */
383 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
384 ut_assert(dev == test_dev);
386 /* Now remove device 3 */
387 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
388 ut_assertok(device_unbind(dev));
392 DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA);
394 /* Check that we can perform operations on a device (do a ping) */
395 int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
396 uint32_t base, struct dm_test_priv *priv)
401 /* Getting the child device should allocate platdata / priv */
402 ut_assertok(testfdt_ping(dev, 10, &pingret));
403 ut_assert(dev->priv);
404 ut_assert(dev->platdata);
406 expected = 10 + base;
407 ut_asserteq(expected, pingret);
409 /* Do another ping */
410 ut_assertok(testfdt_ping(dev, 20, &pingret));
411 expected = 20 + base;
412 ut_asserteq(expected, pingret);
414 /* Now check the ping_total */
416 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
422 /* Check that we can perform operations on devices */
423 static int dm_test_operations(struct unit_test_state *uts)
429 * Now check that the ping adds are what we expect. This is using the
430 * ping-add property in each node.
432 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
435 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
438 * Get the 'reg' property, which tells us what the ping add
439 * should be. We don't use the platdata because we want
440 * to test the code that sets that up (testfdt_drv_probe()).
442 base = test_pdata[i].ping_add;
443 debug("dev=%d, base=%d\n", i, base);
445 ut_assert(!dm_check_operations(uts, dev, base, dev->priv));
450 DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA);
452 /* Remove all drivers and check that things work */
453 static int dm_test_remove(struct unit_test_state *uts)
458 for (i = 0; i < 3; i++) {
459 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
461 ut_assertf(dev->flags & DM_FLAG_ACTIVATED,
462 "Driver %d/%s not activated", i, dev->name);
463 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
464 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
465 "Driver %d/%s should have deactivated", i,
467 ut_assert(!dev->priv);
472 DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
474 /* Remove and recreate everything, check for memory leaks */
475 static int dm_test_leak(struct unit_test_state *uts)
479 for (i = 0; i < 2; i++) {
484 dm_leak_check_start(uts);
486 ut_assertok(dm_scan_platdata(false));
487 ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
489 /* Scanning the uclass is enough to probe all the devices */
490 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
491 for (ret = uclass_first_device(UCLASS_TEST, &dev);
493 ret = uclass_next_device(&dev))
498 ut_assertok(dm_leak_check_end(uts));
503 DM_TEST(dm_test_leak, 0);
505 /* Test uclass init/destroy methods */
506 static int dm_test_uclass(struct unit_test_state *uts)
510 ut_assertok(uclass_get(UCLASS_TEST, &uc));
511 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
512 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
515 ut_assertok(uclass_destroy(uc));
516 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
517 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
521 DM_TEST(dm_test_uclass, 0);
524 * create_children() - Create children of a parent node
526 * @dms: Test system state
527 * @parent: Parent device
528 * @count: Number of children to create
529 * @key: Key value to put in first child. Subsequence children
530 * receive an incrementing value
531 * @child: If not NULL, then the child device pointers are written into
533 * @return 0 if OK, -ve on error
535 static int create_children(struct unit_test_state *uts, struct udevice *parent,
536 int count, int key, struct udevice *child[])
541 for (i = 0; i < count; i++) {
542 struct dm_test_pdata *pdata;
544 ut_assertok(device_bind_by_name(parent, false,
545 &driver_info_manual, &dev));
546 pdata = calloc(1, sizeof(*pdata));
547 pdata->ping_add = key + i;
548 dev->platdata = pdata;
556 #define NODE_COUNT 10
558 static int dm_test_children(struct unit_test_state *uts)
560 struct dm_test_state *dms = uts->priv;
561 struct udevice *top[NODE_COUNT];
562 struct udevice *child[NODE_COUNT];
563 struct udevice *grandchild[NODE_COUNT];
569 /* We don't care about the numbering for this test */
570 dms->skip_post_probe = 1;
572 ut_assert(NODE_COUNT > 5);
574 /* First create 10 top-level children */
575 ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
577 /* Now a few have their own children */
578 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
579 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
581 /* And grandchildren */
582 for (i = 0; i < NODE_COUNT; i++)
583 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
584 i == 2 ? grandchild : NULL));
586 /* Check total number of devices */
587 total = NODE_COUNT * (3 + NODE_COUNT);
588 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
590 /* Try probing one of the grandchildren */
591 ut_assertok(uclass_get_device(UCLASS_TEST,
592 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
593 ut_asserteq_ptr(grandchild[0], dev);
596 * This should have probed the child and top node also, for a total
599 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
601 /* Probe the other grandchildren */
602 for (i = 1; i < NODE_COUNT; i++)
603 ut_assertok(device_probe(grandchild[i]));
605 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
607 /* Probe everything */
608 for (ret = uclass_first_device(UCLASS_TEST, &dev);
610 ret = uclass_next_device(&dev))
614 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
616 /* Remove a top-level child and check that the children are removed */
617 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
618 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
619 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
621 /* Try one with grandchildren */
622 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
623 ut_asserteq_ptr(dev, top[5]);
624 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
625 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
626 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
628 /* Try the same with unbind */
629 ut_assertok(device_unbind(top[2]));
630 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
631 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
633 /* Try one with grandchildren */
634 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
635 ut_asserteq_ptr(dev, top[6]);
636 ut_assertok(device_unbind(top[5]));
637 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
638 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
642 DM_TEST(dm_test_children, 0);
644 /* Test that pre-relocation devices work as expected */
645 static int dm_test_pre_reloc(struct unit_test_state *uts)
647 struct dm_test_state *dms = uts->priv;
650 /* The normal driver should refuse to bind before relocation */
651 ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
652 &driver_info_manual, &dev));
654 /* But this one is marked pre-reloc */
655 ut_assertok(device_bind_by_name(dms->root, true,
656 &driver_info_pre_reloc, &dev));
660 DM_TEST(dm_test_pre_reloc, 0);
663 * Test that removal of devices, either via the "normal" device_remove()
664 * API or via the device driver selective flag works as expected
666 static int dm_test_remove_active_dma(struct unit_test_state *uts)
668 struct dm_test_state *dms = uts->priv;
671 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
675 /* Probe the device */
676 ut_assertok(device_probe(dev));
678 /* Test if device is active right now */
679 ut_asserteq(true, device_active(dev));
681 /* Remove the device via selective remove flag */
682 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
684 /* Test if device is inactive right now */
685 ut_asserteq(false, device_active(dev));
687 /* Probe the device again */
688 ut_assertok(device_probe(dev));
690 /* Test if device is active right now */
691 ut_asserteq(true, device_active(dev));
693 /* Remove the device via "normal" remove API */
694 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
696 /* Test if device is inactive right now */
697 ut_asserteq(false, device_active(dev));
700 * Test if a device without the active DMA flags is not removed upon
701 * the active DMA remove call
703 ut_assertok(device_unbind(dev));
704 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
708 /* Probe the device */
709 ut_assertok(device_probe(dev));
711 /* Test if device is active right now */
712 ut_asserteq(true, device_active(dev));
714 /* Remove the device via selective remove flag */
715 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
717 /* Test if device is still active right now */
718 ut_asserteq(true, device_active(dev));
722 DM_TEST(dm_test_remove_active_dma, 0);
724 static int dm_test_uclass_before_ready(struct unit_test_state *uts)
728 ut_assertok(uclass_get(UCLASS_TEST, &uc));
731 gd->dm_root_f = NULL;
732 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
734 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
738 DM_TEST(dm_test_uclass_before_ready, 0);
740 static int dm_test_uclass_devices_find(struct unit_test_state *uts)
745 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
747 ret = uclass_find_next_device(&dev)) {
752 ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
757 DM_TEST(dm_test_uclass_devices_find, DM_TESTF_SCAN_PDATA);
759 static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
761 struct udevice *finddev;
762 struct udevice *testdev;
766 * For each test device found in fdt like: "a-test", "b-test", etc.,
767 * use its name and try to find it by uclass_find_device_by_name().
768 * Then, on success check if:
769 * - current 'testdev' name is equal to the returned 'finddev' name
770 * - current 'testdev' pointer is equal to the returned 'finddev'
772 * We assume that, each uclass's device name is unique, so if not, then
773 * this will fail on checking condition: testdev == finddev, since the
774 * uclass_find_device_by_name(), returns the first device by given name.
776 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
778 ret = uclass_find_next_device(&testdev)) {
782 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
786 ut_assertok(findret);
788 ut_asserteq_str(testdev->name, finddev->name);
789 ut_asserteq_ptr(testdev, finddev);
794 DM_TEST(dm_test_uclass_devices_find_by_name, DM_TESTF_SCAN_FDT);
796 static int dm_test_uclass_devices_get(struct unit_test_state *uts)
801 for (ret = uclass_first_device(UCLASS_TEST, &dev);
803 ret = uclass_next_device(&dev)) {
806 ut_assert(device_active(dev));
811 DM_TEST(dm_test_uclass_devices_get, DM_TESTF_SCAN_PDATA);
813 static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
815 struct udevice *finddev;
816 struct udevice *testdev;
820 * For each test device found in fdt like: "a-test", "b-test", etc.,
821 * use its name and try to get it by uclass_get_device_by_name().
822 * On success check if:
823 * - returned finddev' is active
824 * - current 'testdev' name is equal to the returned 'finddev' name
825 * - current 'testdev' pointer is equal to the returned 'finddev'
827 * We asserts that the 'testdev' is active on each loop entry, so we
828 * could be sure that the 'finddev' is activated too, but for sure
831 * We assume that, each uclass's device name is unique, so if not, then
832 * this will fail on checking condition: testdev == finddev, since the
833 * uclass_get_device_by_name(), returns the first device by given name.
835 for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
837 ret = uclass_next_device(&testdev)) {
840 ut_assert(device_active(testdev));
842 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
846 ut_assertok(findret);
848 ut_assert(device_active(finddev));
849 ut_asserteq_str(testdev->name, finddev->name);
850 ut_asserteq_ptr(testdev, finddev);
855 DM_TEST(dm_test_uclass_devices_get_by_name, DM_TESTF_SCAN_FDT);
857 static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
861 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
862 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
866 DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);
868 static int dm_test_uclass_names(struct unit_test_state *uts)
870 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
871 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
875 DM_TEST(dm_test_uclass_names, DM_TESTF_SCAN_PDATA);
877 static int dm_test_inactive_child(struct unit_test_state *uts)
879 struct dm_test_state *dms = uts->priv;
880 struct udevice *parent, *dev1, *dev2;
882 /* Skip the behaviour in test_post_probe() */
883 dms->skip_post_probe = 1;
885 ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
888 * Create a child but do not activate it. Calling the function again
889 * should return the same child.
891 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
892 UCLASS_TEST, &dev1));
893 ut_assertok(device_bind_ofnode(parent, DM_GET_DRIVER(test_drv),
894 "test_child", 0, ofnode_null(), &dev1));
896 ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
898 ut_asserteq_ptr(dev1, dev2);
900 ut_assertok(device_probe(dev1));
901 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
902 UCLASS_TEST, &dev2));
906 DM_TEST(dm_test_inactive_child, DM_TESTF_SCAN_PDATA);