1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
15 #include <dm/device-internal.h>
16 #include <dm/devres.h>
17 #include <dm/uclass-internal.h>
20 #include <dm/of_access.h>
21 #include <test/test.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
28 const struct dm_test_pdata *pdata = dev->plat;
29 struct dm_test_priv *priv = dev_get_priv(dev);
31 *pingret = pingval + pdata->ping_add;
32 priv->ping_total += *pingret;
37 static const struct test_ops test_ops = {
38 .ping = testfdt_drv_ping,
41 static int testfdt_of_to_plat(struct udevice *dev)
43 struct dm_test_pdata *pdata = dev_get_plat(dev);
45 pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
47 pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
53 static int testfdt_drv_probe(struct udevice *dev)
55 struct dm_test_priv *priv = dev_get_priv(dev);
57 priv->ping_total += DM_TEST_START_TOTAL;
60 * If this device is on a bus, the uclass_flag will be set before
61 * calling this function. In the meantime the uclass_postp is
62 * initlized to a value -1. These are used respectively by
63 * dm_test_bus_child_pre_probe_uclass() and
64 * dm_test_bus_child_post_probe_uclass().
66 priv->uclass_total += priv->uclass_flag;
67 priv->uclass_postp = -1;
72 static const struct udevice_id testfdt_ids[] = {
74 .compatible = "denx,u-boot-fdt-test",
75 .data = DM_TEST_TYPE_FIRST },
77 .compatible = "google,another-fdt-test",
78 .data = DM_TEST_TYPE_SECOND },
82 U_BOOT_DRIVER(testfdt_drv) = {
83 .name = "testfdt_drv",
84 .of_match = testfdt_ids,
85 .id = UCLASS_TEST_FDT,
86 .of_to_plat = testfdt_of_to_plat,
87 .probe = testfdt_drv_probe,
89 .priv_auto = sizeof(struct dm_test_priv),
90 .plat_auto = sizeof(struct dm_test_pdata),
93 static const struct udevice_id testfdt1_ids[] = {
95 .compatible = "denx,u-boot-fdt-test1",
96 .data = DM_TEST_TYPE_FIRST },
100 U_BOOT_DRIVER(testfdt1_drv) = {
101 .name = "testfdt1_drv",
102 .of_match = testfdt1_ids,
103 .id = UCLASS_TEST_FDT,
104 .of_to_plat = testfdt_of_to_plat,
105 .probe = testfdt_drv_probe,
107 .priv_auto = sizeof(struct dm_test_priv),
108 .plat_auto = sizeof(struct dm_test_pdata),
109 .flags = DM_FLAG_PRE_RELOC,
112 /* From here is the testfdt uclass code */
113 int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
115 const struct test_ops *ops = device_get_ops(dev);
120 return ops->ping(dev, pingval, pingret);
123 UCLASS_DRIVER(testfdt) = {
125 .id = UCLASS_TEST_FDT,
126 .flags = DM_UC_FLAG_SEQ_ALIAS,
129 static const struct udevice_id testfdtm_ids[] = {
130 { .compatible = "denx,u-boot-fdtm-test" },
134 U_BOOT_DRIVER(testfdtm_drv) = {
135 .name = "testfdtm_drv",
136 .of_match = testfdtm_ids,
137 .id = UCLASS_TEST_FDT_MANUAL,
140 UCLASS_DRIVER(testfdtm) = {
142 .id = UCLASS_TEST_FDT_MANUAL,
143 .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
146 struct dm_testprobe_pdata {
150 static int testprobe_drv_probe(struct udevice *dev)
152 struct dm_testprobe_pdata *pdata = dev_get_plat(dev);
154 return pdata->probe_err;
157 static const struct udevice_id testprobe_ids[] = {
158 { .compatible = "denx,u-boot-probe-test" },
162 U_BOOT_DRIVER(testprobe_drv) = {
163 .name = "testprobe_drv",
164 .of_match = testprobe_ids,
165 .id = UCLASS_TEST_PROBE,
166 .probe = testprobe_drv_probe,
167 .plat_auto = sizeof(struct dm_testprobe_pdata),
170 UCLASS_DRIVER(testprobe) = {
172 .id = UCLASS_TEST_PROBE,
173 .flags = DM_UC_FLAG_SEQ_ALIAS,
176 struct dm_testdevres_pdata {
180 struct dm_testdevres_priv {
185 static int testdevres_drv_bind(struct udevice *dev)
187 struct dm_testdevres_pdata *pdata = dev_get_plat(dev);
189 pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
194 static int testdevres_drv_of_to_plat(struct udevice *dev)
196 struct dm_testdevres_priv *priv = dev_get_priv(dev);
198 priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0);
203 static int testdevres_drv_probe(struct udevice *dev)
205 struct dm_testdevres_priv *priv = dev_get_priv(dev);
207 priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0);
212 static const struct udevice_id testdevres_ids[] = {
213 { .compatible = "denx,u-boot-devres-test" },
217 U_BOOT_DRIVER(testdevres_drv) = {
218 .name = "testdevres_drv",
219 .of_match = testdevres_ids,
220 .id = UCLASS_TEST_DEVRES,
221 .bind = testdevres_drv_bind,
222 .of_to_plat = testdevres_drv_of_to_plat,
223 .probe = testdevres_drv_probe,
224 .plat_auto = sizeof(struct dm_testdevres_pdata),
225 .priv_auto = sizeof(struct dm_testdevres_priv),
228 UCLASS_DRIVER(testdevres) = {
229 .name = "testdevres",
230 .id = UCLASS_TEST_DEVRES,
231 .flags = DM_UC_FLAG_SEQ_ALIAS,
234 int dm_check_devices(struct unit_test_state *uts, int num_devices)
241 * Now check that the ping adds are what we expect. This is using the
242 * ping-add property in each node.
244 for (i = 0; i < num_devices; i++) {
247 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
251 * Get the 'ping-expect' property, which tells us what the
252 * ping add should be. We don't use the plat because we
253 * want to test the code that sets that up
254 * (testfdt_drv_probe()).
256 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
258 debug("dev=%d, base=%d: %s\n", i, base,
259 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
261 ut_assert(!dm_check_operations(uts, dev, base,
268 /* Test that FDT-based binding works correctly */
269 static int dm_test_fdt(struct unit_test_state *uts)
271 const int num_devices = 9;
277 ret = dm_extended_scan(false);
280 ret = uclass_get(UCLASS_TEST_FDT, &uc);
283 /* These are num_devices compatible root-level device tree nodes */
284 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
286 /* Each should have platform data but no private data */
287 for (i = 0; i < num_devices; i++) {
288 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
290 ut_assert(!dev_get_priv(dev));
291 ut_assert(dev->plat);
294 ut_assertok(dm_check_devices(uts, num_devices));
298 DM_TEST(dm_test_fdt, 0);
300 static int dm_test_alias_highest_id(struct unit_test_state *uts)
304 ret = dev_read_alias_highest_id("eth");
307 ret = dev_read_alias_highest_id("gpio");
310 ret = dev_read_alias_highest_id("pci");
313 ret = dev_read_alias_highest_id("i2c");
316 ret = dev_read_alias_highest_id("deadbeef");
317 ut_asserteq(-1, ret);
321 DM_TEST(dm_test_alias_highest_id, 0);
323 static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
328 ret = dm_scan_fdt(true);
331 ret = uclass_get(UCLASS_TEST_FDT, &uc);
335 * These are 2 pre-reloc devices:
336 * one with "u-boot,dm-pre-reloc" property (a-test node), and the other
337 * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node).
339 ut_asserteq(2, list_count_items(&uc->dev_head));
343 DM_TEST(dm_test_fdt_pre_reloc, 0);
345 /* Test that sequence numbers are allocated properly */
346 static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
350 /* A few basic santiy tests */
351 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
352 ut_asserteq_str("b-test", dev->name);
353 ut_asserteq(3, dev_seq(dev));
355 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev));
356 ut_asserteq_str("a-test", dev->name);
357 ut_asserteq(8, dev_seq(dev));
360 * This device has no alias so gets the next value after all available
361 * aliases. The last alias is testfdt12
363 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 13, true, &dev));
364 ut_asserteq_str("d-test", dev->name);
365 ut_asserteq(13, dev_seq(dev));
367 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9, true,
369 ut_asserteq_ptr(NULL, dev);
372 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
373 ut_asserteq_str("e-test", dev->name);
374 ut_asserteq(6, dev_seq(dev));
377 * Note that c-test nodes are not probed since it is not a top-level
380 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
381 ut_asserteq_str("b-test", dev->name);
382 ut_asserteq(3, dev_seq(dev));
385 * d-test wants sequence number 3 also, but it can't have it because
386 * b-test gets it first.
388 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
389 ut_asserteq_str("d-test", dev->name);
390 ut_asserteq(13, dev_seq(dev));
392 /* g-test gets the next value after f-test */
393 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 15, &dev));
394 ut_asserteq_str("g-test", dev->name);
395 ut_asserteq(15, dev_seq(dev));
397 /* And we should still have holes in our sequence numbers */
398 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 0, true,
400 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 1, true,
402 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2, true,
404 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4, true,
406 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7, true,
408 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9, true,
410 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 10,
412 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 11,
417 DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
419 /* More tests for sequence numbers */
420 static int dm_test_fdt_uclass_seq_manual(struct unit_test_state *uts)
425 * Since DM_UC_FLAG_NO_AUTO_SEQ is set for this uclass, only testfdtm1
426 * should get a sequence number assigned
428 ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 0, &dev));
429 ut_asserteq_str("testfdtm0", dev->name);
430 ut_asserteq(-1, dev_seq(dev));
432 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT_MANUAL, 1, &dev));
433 ut_asserteq_str("testfdtm1", dev->name);
434 ut_asserteq(1, dev_seq(dev));
436 ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 2, &dev));
437 ut_asserteq_str("testfdtm2", dev->name);
438 ut_asserteq(-1, dev_seq(dev));
442 DM_TEST(dm_test_fdt_uclass_seq_manual, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
444 /* Test that we can find a device by device tree offset */
445 static int dm_test_fdt_offset(struct unit_test_state *uts)
447 const void *blob = gd->fdt_blob;
451 node = fdt_path_offset(blob, "/e-test");
453 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
455 ut_asserteq_str("e-test", dev->name);
457 /* This node should not be bound */
458 node = fdt_path_offset(blob, "/junk");
460 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
463 /* This is not a top level node so should not be probed */
464 node = fdt_path_offset(blob, "/some-bus/c-test@5");
466 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
471 DM_TEST(dm_test_fdt_offset,
472 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
475 * Test various error conditions with uclass_first_device() and
476 * uclass_next_device()
478 static int dm_test_first_next_device(struct unit_test_state *uts)
480 struct dm_testprobe_pdata *pdata;
481 struct udevice *dev, *parent = NULL;
485 /* There should be 4 devices */
486 for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
488 ret = uclass_next_device(&dev)) {
490 parent = dev_get_parent(dev);
493 ut_asserteq(4, count);
495 /* Remove them and try again, with an error on the second one */
496 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
497 pdata = dev_get_plat(dev);
498 pdata->probe_err = -ENOMEM;
499 device_remove(parent, DM_REMOVE_NORMAL);
500 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
501 ut_asserteq(-ENOMEM, uclass_next_device(&dev));
502 ut_asserteq_ptr(dev, NULL);
504 /* Now an error on the first one */
505 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
506 pdata = dev_get_plat(dev);
507 pdata->probe_err = -ENOENT;
508 device_remove(parent, DM_REMOVE_NORMAL);
509 ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
513 DM_TEST(dm_test_first_next_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
515 /* Test iteration through devices in a uclass */
516 static int dm_test_uclass_foreach(struct unit_test_state *uts)
523 uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc)
525 ut_asserteq(9, count);
528 uclass_foreach_dev(dev, uc)
530 ut_asserteq(9, count);
534 DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
537 * check_devices() - Check return values and pointers
539 * This runs through a full sequence of uclass_first_device_check()...
540 * uclass_next_device_check() checking that the return values and devices
544 * @devlist: List of expected devices
545 * @mask: Indicates which devices should return an error. Device n should
546 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
549 static int check_devices(struct unit_test_state *uts,
550 struct udevice *devlist[], int mask)
556 expected_ret = (mask & 1) ? -ENOENT : 0;
558 ut_asserteq(expected_ret,
559 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
560 for (i = 0; i < 4; i++) {
561 ut_asserteq_ptr(devlist[i], dev);
562 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
564 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
566 ut_asserteq_ptr(NULL, dev);
571 /* Test uclass_first_device_check() and uclass_next_device_check() */
572 static int dm_test_first_next_ok_device(struct unit_test_state *uts)
574 struct dm_testprobe_pdata *pdata;
575 struct udevice *dev, *parent = NULL, *devlist[4];
579 /* There should be 4 devices */
581 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
583 ret = uclass_next_device_check(&dev)) {
585 devlist[count++] = dev;
586 parent = dev_get_parent(dev);
588 ut_asserteq(4, count);
589 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
590 ut_assertok(check_devices(uts, devlist, 0));
592 /* Remove them and try again, with an error on the second one */
593 pdata = dev_get_plat(devlist[1]);
594 pdata->probe_err = -ENOENT - 1;
595 device_remove(parent, DM_REMOVE_NORMAL);
596 ut_assertok(check_devices(uts, devlist, 1 << 1));
598 /* Now an error on the first one */
599 pdata = dev_get_plat(devlist[0]);
600 pdata->probe_err = -ENOENT - 0;
601 device_remove(parent, DM_REMOVE_NORMAL);
602 ut_assertok(check_devices(uts, devlist, 3 << 0));
604 /* Now errors on all */
605 pdata = dev_get_plat(devlist[2]);
606 pdata->probe_err = -ENOENT - 2;
607 pdata = dev_get_plat(devlist[3]);
608 pdata->probe_err = -ENOENT - 3;
609 device_remove(parent, DM_REMOVE_NORMAL);
610 ut_assertok(check_devices(uts, devlist, 0xf << 0));
614 DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
616 static const struct udevice_id fdt_dummy_ids[] = {
617 { .compatible = "denx,u-boot-fdt-dummy", },
621 UCLASS_DRIVER(fdt_dummy) = {
623 .id = UCLASS_TEST_DUMMY,
624 .flags = DM_UC_FLAG_SEQ_ALIAS,
627 U_BOOT_DRIVER(fdt_dummy_drv) = {
628 .name = "fdt_dummy_drv",
629 .of_match = fdt_dummy_ids,
630 .id = UCLASS_TEST_DUMMY,
633 static int dm_test_fdt_translation(struct unit_test_state *uts)
638 /* Some simple translations */
639 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
640 ut_asserteq_str("dev@0,0", dev->name);
641 ut_asserteq(0x8000, dev_read_addr(dev));
643 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
644 ut_asserteq_str("dev@1,100", dev->name);
645 ut_asserteq(0x9000, dev_read_addr(dev));
647 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, true, &dev));
648 ut_asserteq_str("dev@2,200", dev->name);
649 ut_asserteq(0xA000, dev_read_addr(dev));
651 /* No translation for busses with #size-cells == 0 */
652 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, true, &dev));
653 ut_asserteq_str("dev@42", dev->name);
654 ut_asserteq(0x42, dev_read_addr(dev));
656 /* dma address translation */
657 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
658 dma_addr[0] = cpu_to_be32(0);
659 dma_addr[1] = cpu_to_be32(0);
660 ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
662 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
663 dma_addr[0] = cpu_to_be32(1);
664 dma_addr[1] = cpu_to_be32(0x100);
665 ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
669 DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
671 static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts)
673 struct udevice *gpio, *dev;
676 /* Test for missing reg property */
677 ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio));
678 ut_assertnull(devfdt_get_addr_ptr(gpio));
680 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
681 ptr = devfdt_get_addr_ptr(dev);
682 ut_asserteq_ptr((void *)0x8000, ptr);
686 DM_TEST(dm_test_fdt_get_addr_ptr_flat,
687 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
689 static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
695 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
697 addr = devfdt_get_addr(dev);
698 ut_asserteq(0x8000, addr);
700 paddr = map_physmem(addr, 0, MAP_NOCACHE);
701 ut_assertnonnull(paddr);
702 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
706 DM_TEST(dm_test_fdt_remap_addr_flat,
707 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
709 static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
716 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
718 addr = devfdt_get_addr_size_index(dev, 0, &size);
719 ut_asserteq(0x8000, addr);
720 ut_asserteq(0x1000, size);
722 paddr = map_physmem(addr, 0, MAP_NOCACHE);
723 ut_assertnonnull(paddr);
724 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
728 DM_TEST(dm_test_fdt_remap_addr_index_flat,
729 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
731 static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
738 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
740 addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
741 ut_asserteq(0x8000, addr);
742 ut_asserteq(0x1000, size);
744 paddr = map_physmem(addr, 0, MAP_NOCACHE);
745 ut_assertnonnull(paddr);
746 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
750 DM_TEST(dm_test_fdt_remap_addr_name_flat,
751 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
753 static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
759 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
761 addr = dev_read_addr(dev);
762 ut_asserteq(0x8000, addr);
764 paddr = map_physmem(addr, 0, MAP_NOCACHE);
765 ut_assertnonnull(paddr);
766 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
770 DM_TEST(dm_test_fdt_remap_addr_live,
771 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
773 static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
780 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
782 addr = dev_read_addr_size_index(dev, 0, &size);
783 ut_asserteq(0x8000, addr);
784 ut_asserteq(0x1000, size);
786 paddr = map_physmem(addr, 0, MAP_NOCACHE);
787 ut_assertnonnull(paddr);
788 ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
792 DM_TEST(dm_test_fdt_remap_addr_index_live,
793 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
795 static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
802 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
804 addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
805 ut_asserteq(0x8000, addr);
806 ut_asserteq(0x1000, size);
808 paddr = map_physmem(addr, 0, MAP_NOCACHE);
809 ut_assertnonnull(paddr);
810 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
814 DM_TEST(dm_test_fdt_remap_addr_name_live,
815 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
817 static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
822 if (!of_live_active()) {
823 printf("Live tree not active; ignore test\n");
827 /* Test enabling devices */
829 node = ofnode_path("/usb@2");
831 ut_assert(!of_device_is_available(ofnode_to_np(node)));
832 ofnode_set_enabled(node, true);
833 ut_assert(of_device_is_available(ofnode_to_np(node)));
835 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
837 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, true, &dev));
839 /* Test string property setting */
841 ut_assert(device_is_compatible(dev, "sandbox,usb"));
842 ofnode_write_string(node, "compatible", "gdsys,super-usb");
843 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
844 ofnode_write_string(node, "compatible", "sandbox,usb");
845 ut_assert(device_is_compatible(dev, "sandbox,usb"));
847 /* Test setting generic properties */
849 /* Non-existent in DTB */
850 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
851 /* reg = 0x42, size = 0x100 */
852 ut_assertok(ofnode_write_prop(node, "reg", 8,
853 "\x00\x00\x00\x42\x00\x00\x01\x00"));
854 ut_asserteq(0x42, dev_read_addr(dev));
856 /* Test disabling devices */
858 device_remove(dev, DM_REMOVE_NORMAL);
861 ut_assert(of_device_is_available(ofnode_to_np(node)));
862 ofnode_set_enabled(node, false);
863 ut_assert(!of_device_is_available(ofnode_to_np(node)));
867 DM_TEST(dm_test_fdt_livetree_writing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
869 static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
873 if (!of_live_active()) {
874 printf("Live tree not active; ignore test\n");
878 node = ofnode_path("/usb@2");
880 /* Test enabling devices */
882 ut_assert(!of_device_is_available(ofnode_to_np(node)));
883 dev_enable_by_path("/usb@2");
884 ut_assert(of_device_is_available(ofnode_to_np(node)));
886 /* Test disabling devices */
888 ut_assert(of_device_is_available(ofnode_to_np(node)));
889 dev_disable_by_path("/usb@2");
890 ut_assert(!of_device_is_available(ofnode_to_np(node)));
894 DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA |
897 /* Test a few uclass phandle functions */
898 static int dm_test_fdt_phandle(struct unit_test_state *uts)
900 struct udevice *back, *dev, *dev2;
902 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
903 ut_assertnonnull(back);
904 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
905 back, "missing", &dev));
906 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
907 "power-supply", &dev));
908 ut_assertnonnull(dev);
909 ut_asserteq(0, device_active(dev));
910 ut_asserteq_str("ldo1", dev->name);
911 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
912 "power-supply", &dev2));
913 ut_asserteq_ptr(dev, dev2);
917 DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
919 /* Test device_find_first_child_by_uclass() */
920 static int dm_test_first_child(struct unit_test_state *uts)
922 struct udevice *i2c, *dev, *dev2;
924 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
925 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
926 ut_asserteq_str("rtc@43", dev->name);
927 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
928 ut_asserteq_ptr(dev, dev2);
929 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
930 ut_asserteq_str("rtc@61", dev2->name);
932 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
934 ut_asserteq_str("eeprom@2c", dev->name);
935 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
936 ut_asserteq_ptr(dev, dev2);
938 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
939 UCLASS_VIDEO, &dev));
940 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
944 DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
946 /* Test integer functions in dm_read_...() */
947 static int dm_test_read_int(struct unit_test_state *uts)
955 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
956 ut_asserteq_str("a-test", dev->name);
957 ut_assertok(dev_read_u32(dev, "int-value", &val32));
958 ut_asserteq(1234, val32);
960 ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
961 ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
963 ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
964 ut_asserteq(1234, val32);
966 ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
967 ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
969 ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
970 ut_assertok(dev_read_s32(dev, "uint-value", &sval));
971 ut_asserteq(-1234, sval);
974 ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
975 ut_assertok(dev_read_u32u(dev, "uint-value", &val));
976 ut_asserteq(-1234, val);
978 ut_assertok(dev_read_u64(dev, "int64-value", &val64));
979 ut_asserteq_64(0x1111222233334444, val64);
981 ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64));
982 ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6));
984 ut_asserteq_64(0x1111222233334444,
985 dev_read_u64_default(dev, "int64-value", 6));
989 DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
991 static int dm_test_read_int_index(struct unit_test_state *uts)
996 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
997 ut_asserteq_str("a-test", dev->name);
999 ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
1000 ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
1002 ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
1003 ut_asserteq(5678, val32);
1004 ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
1005 ut_asserteq(9123, val32);
1006 ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
1007 ut_asserteq(4567, val32);
1008 ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
1011 ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
1012 ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
1013 ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
1014 ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
1018 DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1020 static int dm_test_read_phandle(struct unit_test_state *uts)
1022 struct udevice *dev;
1023 struct ofnode_phandle_args args;
1025 const char prop[] = "test-gpios";
1026 const char cell[] = "#gpio-cells";
1027 const char prop2[] = "phandle-value";
1029 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1030 ut_asserteq_str("a-test", dev->name);
1032 /* Test dev_count_phandle_with_args with cell name */
1033 ret = dev_count_phandle_with_args(dev, "missing", cell, 0);
1034 ut_asserteq(-ENOENT, ret);
1035 ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0);
1036 ut_asserteq(-EINVAL, ret);
1037 ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0));
1039 /* Test dev_read_phandle_with_args with cell name */
1040 ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args);
1041 ut_asserteq(-ENOENT, ret);
1042 ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args);
1043 ut_asserteq(-EINVAL, ret);
1044 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args));
1045 ut_asserteq(1, args.args_count);
1046 ut_asserteq(1, args.args[0]);
1047 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args));
1048 ut_asserteq(1, args.args_count);
1049 ut_asserteq(4, args.args[0]);
1050 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args));
1051 ut_asserteq(5, args.args_count);
1052 ut_asserteq(5, args.args[0]);
1053 ut_asserteq(1, args.args[4]);
1054 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args);
1055 ut_asserteq(-ENOENT, ret);
1056 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args));
1057 ut_asserteq(1, args.args_count);
1058 ut_asserteq(12, args.args[0]);
1059 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args);
1060 ut_asserteq(-ENOENT, ret);
1062 /* Test dev_count_phandle_with_args with cell count */
1063 ret = dev_count_phandle_with_args(dev, "missing", NULL, 2);
1064 ut_asserteq(-ENOENT, ret);
1065 ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1));
1067 /* Test dev_read_phandle_with_args with cell count */
1068 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args));
1069 ut_asserteq(1, ofnode_valid(args.node));
1070 ut_asserteq(1, args.args_count);
1071 ut_asserteq(10, args.args[0]);
1072 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args);
1073 ut_asserteq(-EINVAL, ret);
1074 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args));
1075 ut_asserteq(1, ofnode_valid(args.node));
1076 ut_asserteq(1, args.args_count);
1077 ut_asserteq(30, args.args[0]);
1078 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args);
1079 ut_asserteq(-ENOENT, ret);
1083 DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1085 /* Test iteration through devices by drvdata */
1086 static int dm_test_uclass_drvdata(struct unit_test_state *uts)
1088 struct udevice *dev;
1090 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1091 DM_TEST_TYPE_FIRST, &dev));
1092 ut_asserteq_str("a-test", dev->name);
1094 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1095 DM_TEST_TYPE_SECOND, &dev));
1096 ut_asserteq_str("d-test", dev->name);
1098 ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT,
1104 DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1106 /* Test device_first_child_ofdata_err(), etc. */
1107 static int dm_test_child_ofdata(struct unit_test_state *uts)
1109 struct udevice *bus, *dev;
1112 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1114 device_foreach_child_of_to_plat(dev, bus) {
1115 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1116 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1119 ut_asserteq(3, count);
1123 DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1125 /* Test device_first_child_err(), etc. */
1126 static int dm_test_first_child_probe(struct unit_test_state *uts)
1128 struct udevice *bus, *dev;
1131 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1133 device_foreach_child_probe(dev, bus) {
1134 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1135 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
1138 ut_asserteq(3, count);
1142 DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1144 /* Test that ofdata is read for parents before children */
1145 static int dm_test_ofdata_order(struct unit_test_state *uts)
1147 struct udevice *bus, *dev;
1149 ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus));
1150 ut_assertnonnull(bus);
1151 ut_assert(!(bus->flags & DM_FLAG_PLATDATA_VALID));
1153 ut_assertok(device_find_first_child(bus, &dev));
1154 ut_assertnonnull(dev);
1155 ut_assert(!(dev->flags & DM_FLAG_PLATDATA_VALID));
1157 /* read the child's ofdata which should cause the parent's to be read */
1158 ut_assertok(device_of_to_plat(dev));
1159 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1160 ut_assert(bus->flags & DM_FLAG_PLATDATA_VALID);
1162 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1163 ut_assert(!(bus->flags & DM_FLAG_ACTIVATED));
1167 DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);