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 struct dm_testprobe_pdata {
133 static int testprobe_drv_probe(struct udevice *dev)
135 struct dm_testprobe_pdata *pdata = dev_get_plat(dev);
137 return pdata->probe_err;
140 static const struct udevice_id testprobe_ids[] = {
141 { .compatible = "denx,u-boot-probe-test" },
145 U_BOOT_DRIVER(testprobe_drv) = {
146 .name = "testprobe_drv",
147 .of_match = testprobe_ids,
148 .id = UCLASS_TEST_PROBE,
149 .probe = testprobe_drv_probe,
150 .plat_auto = sizeof(struct dm_testprobe_pdata),
153 UCLASS_DRIVER(testprobe) = {
155 .id = UCLASS_TEST_PROBE,
156 .flags = DM_UC_FLAG_SEQ_ALIAS,
159 struct dm_testdevres_pdata {
163 struct dm_testdevres_priv {
168 static int testdevres_drv_bind(struct udevice *dev)
170 struct dm_testdevres_pdata *pdata = dev_get_plat(dev);
172 pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
177 static int testdevres_drv_of_to_plat(struct udevice *dev)
179 struct dm_testdevres_priv *priv = dev_get_priv(dev);
181 priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0);
186 static int testdevres_drv_probe(struct udevice *dev)
188 struct dm_testdevres_priv *priv = dev_get_priv(dev);
190 priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0);
195 static const struct udevice_id testdevres_ids[] = {
196 { .compatible = "denx,u-boot-devres-test" },
200 U_BOOT_DRIVER(testdevres_drv) = {
201 .name = "testdevres_drv",
202 .of_match = testdevres_ids,
203 .id = UCLASS_TEST_DEVRES,
204 .bind = testdevres_drv_bind,
205 .of_to_plat = testdevres_drv_of_to_plat,
206 .probe = testdevres_drv_probe,
207 .plat_auto = sizeof(struct dm_testdevres_pdata),
208 .priv_auto = sizeof(struct dm_testdevres_priv),
211 UCLASS_DRIVER(testdevres) = {
212 .name = "testdevres",
213 .id = UCLASS_TEST_DEVRES,
214 .flags = DM_UC_FLAG_SEQ_ALIAS,
217 int dm_check_devices(struct unit_test_state *uts, int num_devices)
224 * Now check that the ping adds are what we expect. This is using the
225 * ping-add property in each node.
227 for (i = 0; i < num_devices; i++) {
230 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
234 * Get the 'ping-expect' property, which tells us what the
235 * ping add should be. We don't use the plat because we
236 * want to test the code that sets that up
237 * (testfdt_drv_probe()).
239 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
241 debug("dev=%d, base=%d: %s\n", i, base,
242 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
244 ut_assert(!dm_check_operations(uts, dev, base,
251 /* Test that FDT-based binding works correctly */
252 static int dm_test_fdt(struct unit_test_state *uts)
254 const int num_devices = 9;
260 ret = dm_extended_scan(false);
263 ret = uclass_get(UCLASS_TEST_FDT, &uc);
266 /* These are num_devices compatible root-level device tree nodes */
267 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
269 /* Each should have platform data but no private data */
270 for (i = 0; i < num_devices; i++) {
271 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
273 ut_assert(!dev_get_priv(dev));
274 ut_assert(dev->plat);
277 ut_assertok(dm_check_devices(uts, num_devices));
281 DM_TEST(dm_test_fdt, 0);
283 static int dm_test_alias_highest_id(struct unit_test_state *uts)
287 ret = dev_read_alias_highest_id("eth");
290 ret = dev_read_alias_highest_id("gpio");
293 ret = dev_read_alias_highest_id("pci");
296 ret = dev_read_alias_highest_id("i2c");
299 ret = dev_read_alias_highest_id("deadbeef");
300 ut_asserteq(-1, ret);
304 DM_TEST(dm_test_alias_highest_id, 0);
306 static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
311 ret = dm_scan_fdt(true);
314 ret = uclass_get(UCLASS_TEST_FDT, &uc);
318 * These are 2 pre-reloc devices:
319 * one with "u-boot,dm-pre-reloc" property (a-test node), and the other
320 * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node).
322 ut_asserteq(2, list_count_items(&uc->dev_head));
326 DM_TEST(dm_test_fdt_pre_reloc, 0);
328 /* Test that sequence numbers are allocated properly */
329 static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
333 /* A few basic santiy tests */
334 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
335 ut_asserteq_str("b-test", dev->name);
336 ut_asserteq(3, dev_seq(dev));
338 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev));
339 ut_asserteq_str("a-test", dev->name);
340 ut_asserteq(8, dev_seq(dev));
343 * This device has no alias so gets the next value after all available
344 * aliases. The last alias is testfdt12
346 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 13, true, &dev));
347 ut_asserteq_str("d-test", dev->name);
348 ut_asserteq(13, dev_seq(dev));
350 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9, true,
352 ut_asserteq_ptr(NULL, dev);
355 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
356 ut_asserteq_str("e-test", dev->name);
357 ut_asserteq(6, dev_seq(dev));
360 * Note that c-test nodes are not probed since it is not a top-level
363 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
364 ut_asserteq_str("b-test", dev->name);
365 ut_asserteq(3, dev_seq(dev));
368 * d-test wants sequence number 3 also, but it can't have it because
369 * b-test gets it first.
371 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
372 ut_asserteq_str("d-test", dev->name);
373 ut_asserteq(13, dev_seq(dev));
375 /* g-test gets the next value after f-test */
376 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 15, &dev));
377 ut_asserteq_str("g-test", dev->name);
378 ut_asserteq(15, dev_seq(dev));
380 /* And we should still have holes in our sequence numbers */
381 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 0, true,
383 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 1, true,
385 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2, true,
387 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4, true,
389 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7, true,
391 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9, true,
393 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 10,
395 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 11,
400 DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
402 /* Test that we can find a device by device tree offset */
403 static int dm_test_fdt_offset(struct unit_test_state *uts)
405 const void *blob = gd->fdt_blob;
409 node = fdt_path_offset(blob, "/e-test");
411 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
413 ut_asserteq_str("e-test", dev->name);
415 /* This node should not be bound */
416 node = fdt_path_offset(blob, "/junk");
418 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
421 /* This is not a top level node so should not be probed */
422 node = fdt_path_offset(blob, "/some-bus/c-test@5");
424 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
429 DM_TEST(dm_test_fdt_offset,
430 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
433 * Test various error conditions with uclass_first_device() and
434 * uclass_next_device()
436 static int dm_test_first_next_device(struct unit_test_state *uts)
438 struct dm_testprobe_pdata *pdata;
439 struct udevice *dev, *parent = NULL;
443 /* There should be 4 devices */
444 for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
446 ret = uclass_next_device(&dev)) {
448 parent = dev_get_parent(dev);
451 ut_asserteq(4, count);
453 /* Remove them and try again, with an error on the second one */
454 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
455 pdata = dev_get_plat(dev);
456 pdata->probe_err = -ENOMEM;
457 device_remove(parent, DM_REMOVE_NORMAL);
458 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
459 ut_asserteq(-ENOMEM, uclass_next_device(&dev));
460 ut_asserteq_ptr(dev, NULL);
462 /* Now an error on the first one */
463 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
464 pdata = dev_get_plat(dev);
465 pdata->probe_err = -ENOENT;
466 device_remove(parent, DM_REMOVE_NORMAL);
467 ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
471 DM_TEST(dm_test_first_next_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
473 /* Test iteration through devices in a uclass */
474 static int dm_test_uclass_foreach(struct unit_test_state *uts)
481 uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc)
483 ut_asserteq(9, count);
486 uclass_foreach_dev(dev, uc)
488 ut_asserteq(9, count);
492 DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
495 * check_devices() - Check return values and pointers
497 * This runs through a full sequence of uclass_first_device_check()...
498 * uclass_next_device_check() checking that the return values and devices
502 * @devlist: List of expected devices
503 * @mask: Indicates which devices should return an error. Device n should
504 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
507 static int check_devices(struct unit_test_state *uts,
508 struct udevice *devlist[], int mask)
514 expected_ret = (mask & 1) ? -ENOENT : 0;
516 ut_asserteq(expected_ret,
517 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
518 for (i = 0; i < 4; i++) {
519 ut_asserteq_ptr(devlist[i], dev);
520 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
522 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
524 ut_asserteq_ptr(NULL, dev);
529 /* Test uclass_first_device_check() and uclass_next_device_check() */
530 static int dm_test_first_next_ok_device(struct unit_test_state *uts)
532 struct dm_testprobe_pdata *pdata;
533 struct udevice *dev, *parent = NULL, *devlist[4];
537 /* There should be 4 devices */
539 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
541 ret = uclass_next_device_check(&dev)) {
543 devlist[count++] = dev;
544 parent = dev_get_parent(dev);
546 ut_asserteq(4, count);
547 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
548 ut_assertok(check_devices(uts, devlist, 0));
550 /* Remove them and try again, with an error on the second one */
551 pdata = dev_get_plat(devlist[1]);
552 pdata->probe_err = -ENOENT - 1;
553 device_remove(parent, DM_REMOVE_NORMAL);
554 ut_assertok(check_devices(uts, devlist, 1 << 1));
556 /* Now an error on the first one */
557 pdata = dev_get_plat(devlist[0]);
558 pdata->probe_err = -ENOENT - 0;
559 device_remove(parent, DM_REMOVE_NORMAL);
560 ut_assertok(check_devices(uts, devlist, 3 << 0));
562 /* Now errors on all */
563 pdata = dev_get_plat(devlist[2]);
564 pdata->probe_err = -ENOENT - 2;
565 pdata = dev_get_plat(devlist[3]);
566 pdata->probe_err = -ENOENT - 3;
567 device_remove(parent, DM_REMOVE_NORMAL);
568 ut_assertok(check_devices(uts, devlist, 0xf << 0));
572 DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
574 static const struct udevice_id fdt_dummy_ids[] = {
575 { .compatible = "denx,u-boot-fdt-dummy", },
579 UCLASS_DRIVER(fdt_dummy) = {
581 .id = UCLASS_TEST_DUMMY,
582 .flags = DM_UC_FLAG_SEQ_ALIAS,
585 U_BOOT_DRIVER(fdt_dummy_drv) = {
586 .name = "fdt_dummy_drv",
587 .of_match = fdt_dummy_ids,
588 .id = UCLASS_TEST_DUMMY,
591 static int dm_test_fdt_translation(struct unit_test_state *uts)
596 /* Some simple translations */
597 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
598 ut_asserteq_str("dev@0,0", dev->name);
599 ut_asserteq(0x8000, dev_read_addr(dev));
601 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
602 ut_asserteq_str("dev@1,100", dev->name);
603 ut_asserteq(0x9000, dev_read_addr(dev));
605 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, true, &dev));
606 ut_asserteq_str("dev@2,200", dev->name);
607 ut_asserteq(0xA000, dev_read_addr(dev));
609 /* No translation for busses with #size-cells == 0 */
610 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, true, &dev));
611 ut_asserteq_str("dev@42", dev->name);
612 ut_asserteq(0x42, dev_read_addr(dev));
614 /* dma address translation */
615 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
616 dma_addr[0] = cpu_to_be32(0);
617 dma_addr[1] = cpu_to_be32(0);
618 ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
620 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
621 dma_addr[0] = cpu_to_be32(1);
622 dma_addr[1] = cpu_to_be32(0x100);
623 ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
627 DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
629 static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts)
631 struct udevice *gpio, *dev;
634 /* Test for missing reg property */
635 ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio));
636 ut_assertnull(devfdt_get_addr_ptr(gpio));
638 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
639 ptr = devfdt_get_addr_ptr(dev);
640 ut_asserteq_ptr((void *)0x8000, ptr);
644 DM_TEST(dm_test_fdt_get_addr_ptr_flat,
645 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
647 static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
653 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
655 addr = devfdt_get_addr(dev);
656 ut_asserteq(0x8000, addr);
658 paddr = map_physmem(addr, 0, MAP_NOCACHE);
659 ut_assertnonnull(paddr);
660 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
664 DM_TEST(dm_test_fdt_remap_addr_flat,
665 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
667 static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
674 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
676 addr = devfdt_get_addr_size_index(dev, 0, &size);
677 ut_asserteq(0x8000, addr);
678 ut_asserteq(0x1000, size);
680 paddr = map_physmem(addr, 0, MAP_NOCACHE);
681 ut_assertnonnull(paddr);
682 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
686 DM_TEST(dm_test_fdt_remap_addr_index_flat,
687 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
689 static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
696 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
698 addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
699 ut_asserteq(0x8000, addr);
700 ut_asserteq(0x1000, size);
702 paddr = map_physmem(addr, 0, MAP_NOCACHE);
703 ut_assertnonnull(paddr);
704 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
708 DM_TEST(dm_test_fdt_remap_addr_name_flat,
709 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
711 static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
717 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
719 addr = dev_read_addr(dev);
720 ut_asserteq(0x8000, addr);
722 paddr = map_physmem(addr, 0, MAP_NOCACHE);
723 ut_assertnonnull(paddr);
724 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
728 DM_TEST(dm_test_fdt_remap_addr_live,
729 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
731 static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
738 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
740 addr = dev_read_addr_size_index(dev, 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, dev_remap_addr_index(dev, 0));
750 DM_TEST(dm_test_fdt_remap_addr_index_live,
751 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
753 static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
760 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
762 addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
763 ut_asserteq(0x8000, addr);
764 ut_asserteq(0x1000, size);
766 paddr = map_physmem(addr, 0, MAP_NOCACHE);
767 ut_assertnonnull(paddr);
768 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
772 DM_TEST(dm_test_fdt_remap_addr_name_live,
773 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
775 static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
780 if (!of_live_active()) {
781 printf("Live tree not active; ignore test\n");
785 /* Test enabling devices */
787 node = ofnode_path("/usb@2");
789 ut_assert(!of_device_is_available(ofnode_to_np(node)));
790 ofnode_set_enabled(node, true);
791 ut_assert(of_device_is_available(ofnode_to_np(node)));
793 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
795 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, true, &dev));
797 /* Test string property setting */
799 ut_assert(device_is_compatible(dev, "sandbox,usb"));
800 ofnode_write_string(node, "compatible", "gdsys,super-usb");
801 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
802 ofnode_write_string(node, "compatible", "sandbox,usb");
803 ut_assert(device_is_compatible(dev, "sandbox,usb"));
805 /* Test setting generic properties */
807 /* Non-existent in DTB */
808 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
809 /* reg = 0x42, size = 0x100 */
810 ut_assertok(ofnode_write_prop(node, "reg", 8,
811 "\x00\x00\x00\x42\x00\x00\x01\x00"));
812 ut_asserteq(0x42, dev_read_addr(dev));
814 /* Test disabling devices */
816 device_remove(dev, DM_REMOVE_NORMAL);
819 ut_assert(of_device_is_available(ofnode_to_np(node)));
820 ofnode_set_enabled(node, false);
821 ut_assert(!of_device_is_available(ofnode_to_np(node)));
825 DM_TEST(dm_test_fdt_livetree_writing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
827 static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
831 if (!of_live_active()) {
832 printf("Live tree not active; ignore test\n");
836 node = ofnode_path("/usb@2");
838 /* Test enabling devices */
840 ut_assert(!of_device_is_available(ofnode_to_np(node)));
841 dev_enable_by_path("/usb@2");
842 ut_assert(of_device_is_available(ofnode_to_np(node)));
844 /* Test disabling devices */
846 ut_assert(of_device_is_available(ofnode_to_np(node)));
847 dev_disable_by_path("/usb@2");
848 ut_assert(!of_device_is_available(ofnode_to_np(node)));
852 DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA |
855 /* Test a few uclass phandle functions */
856 static int dm_test_fdt_phandle(struct unit_test_state *uts)
858 struct udevice *back, *dev, *dev2;
860 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
861 ut_assertnonnull(back);
862 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
863 back, "missing", &dev));
864 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
865 "power-supply", &dev));
866 ut_assertnonnull(dev);
867 ut_asserteq(0, device_active(dev));
868 ut_asserteq_str("ldo1", dev->name);
869 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
870 "power-supply", &dev2));
871 ut_asserteq_ptr(dev, dev2);
875 DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
877 /* Test device_find_first_child_by_uclass() */
878 static int dm_test_first_child(struct unit_test_state *uts)
880 struct udevice *i2c, *dev, *dev2;
882 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
883 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
884 ut_asserteq_str("rtc@43", dev->name);
885 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
886 ut_asserteq_ptr(dev, dev2);
887 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
888 ut_asserteq_str("rtc@61", dev2->name);
890 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
892 ut_asserteq_str("eeprom@2c", dev->name);
893 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
894 ut_asserteq_ptr(dev, dev2);
896 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
897 UCLASS_VIDEO, &dev));
898 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
902 DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
904 /* Test integer functions in dm_read_...() */
905 static int dm_test_read_int(struct unit_test_state *uts)
913 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
914 ut_asserteq_str("a-test", dev->name);
915 ut_assertok(dev_read_u32(dev, "int-value", &val32));
916 ut_asserteq(1234, val32);
918 ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
919 ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
921 ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
922 ut_asserteq(1234, val32);
924 ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
925 ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
927 ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
928 ut_assertok(dev_read_s32(dev, "uint-value", &sval));
929 ut_asserteq(-1234, sval);
932 ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
933 ut_assertok(dev_read_u32u(dev, "uint-value", &val));
934 ut_asserteq(-1234, val);
936 ut_assertok(dev_read_u64(dev, "int64-value", &val64));
937 ut_asserteq_64(0x1111222233334444, val64);
939 ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64));
940 ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6));
942 ut_asserteq_64(0x1111222233334444,
943 dev_read_u64_default(dev, "int64-value", 6));
947 DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
949 static int dm_test_read_int_index(struct unit_test_state *uts)
954 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
955 ut_asserteq_str("a-test", dev->name);
957 ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
958 ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
960 ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
961 ut_asserteq(5678, val32);
962 ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
963 ut_asserteq(9123, val32);
964 ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
965 ut_asserteq(4567, val32);
966 ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
969 ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
970 ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
971 ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
972 ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
976 DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
978 static int dm_test_read_phandle(struct unit_test_state *uts)
981 struct ofnode_phandle_args args;
983 const char prop[] = "test-gpios";
984 const char cell[] = "#gpio-cells";
985 const char prop2[] = "phandle-value";
987 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
988 ut_asserteq_str("a-test", dev->name);
990 /* Test dev_count_phandle_with_args with cell name */
991 ret = dev_count_phandle_with_args(dev, "missing", cell, 0);
992 ut_asserteq(-ENOENT, ret);
993 ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0);
994 ut_asserteq(-EINVAL, ret);
995 ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0));
997 /* Test dev_read_phandle_with_args with cell name */
998 ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args);
999 ut_asserteq(-ENOENT, ret);
1000 ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args);
1001 ut_asserteq(-EINVAL, ret);
1002 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args));
1003 ut_asserteq(1, args.args_count);
1004 ut_asserteq(1, args.args[0]);
1005 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args));
1006 ut_asserteq(1, args.args_count);
1007 ut_asserteq(4, args.args[0]);
1008 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args));
1009 ut_asserteq(5, args.args_count);
1010 ut_asserteq(5, args.args[0]);
1011 ut_asserteq(1, args.args[4]);
1012 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args);
1013 ut_asserteq(-ENOENT, ret);
1014 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args));
1015 ut_asserteq(1, args.args_count);
1016 ut_asserteq(12, args.args[0]);
1017 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args);
1018 ut_asserteq(-ENOENT, ret);
1020 /* Test dev_count_phandle_with_args with cell count */
1021 ret = dev_count_phandle_with_args(dev, "missing", NULL, 2);
1022 ut_asserteq(-ENOENT, ret);
1023 ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1));
1025 /* Test dev_read_phandle_with_args with cell count */
1026 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args));
1027 ut_asserteq(1, ofnode_valid(args.node));
1028 ut_asserteq(1, args.args_count);
1029 ut_asserteq(10, args.args[0]);
1030 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args);
1031 ut_asserteq(-EINVAL, ret);
1032 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args));
1033 ut_asserteq(1, ofnode_valid(args.node));
1034 ut_asserteq(1, args.args_count);
1035 ut_asserteq(30, args.args[0]);
1036 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args);
1037 ut_asserteq(-ENOENT, ret);
1041 DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1043 /* Test iteration through devices by drvdata */
1044 static int dm_test_uclass_drvdata(struct unit_test_state *uts)
1046 struct udevice *dev;
1048 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1049 DM_TEST_TYPE_FIRST, &dev));
1050 ut_asserteq_str("a-test", dev->name);
1052 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1053 DM_TEST_TYPE_SECOND, &dev));
1054 ut_asserteq_str("d-test", dev->name);
1056 ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT,
1062 DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1064 /* Test device_first_child_ofdata_err(), etc. */
1065 static int dm_test_child_ofdata(struct unit_test_state *uts)
1067 struct udevice *bus, *dev;
1070 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1072 device_foreach_child_of_to_plat(dev, bus) {
1073 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1074 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1077 ut_asserteq(3, count);
1081 DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1083 /* Test device_first_child_err(), etc. */
1084 static int dm_test_first_child_probe(struct unit_test_state *uts)
1086 struct udevice *bus, *dev;
1089 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1091 device_foreach_child_probe(dev, bus) {
1092 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1093 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
1096 ut_asserteq(3, count);
1100 DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1102 /* Test that ofdata is read for parents before children */
1103 static int dm_test_ofdata_order(struct unit_test_state *uts)
1105 struct udevice *bus, *dev;
1107 ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus));
1108 ut_assertnonnull(bus);
1109 ut_assert(!(bus->flags & DM_FLAG_PLATDATA_VALID));
1111 ut_assertok(device_find_first_child(bus, &dev));
1112 ut_assertnonnull(dev);
1113 ut_assert(!(dev->flags & DM_FLAG_PLATDATA_VALID));
1115 /* read the child's ofdata which should cause the parent's to be read */
1116 ut_assertok(device_of_to_plat(dev));
1117 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1118 ut_assert(bus->flags & DM_FLAG_PLATDATA_VALID);
1120 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1121 ut_assert(!(bus->flags & DM_FLAG_ACTIVATED));
1125 DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);