1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
12 #include <asm/global_data.h>
17 #include <dm/device-internal.h>
18 #include <dm/devres.h>
19 #include <dm/uclass-internal.h>
21 #include <dm/of_access.h>
22 #include <linux/ioport.h>
23 #include <test/test.h>
26 DECLARE_GLOBAL_DATA_PTR;
28 struct dm_testprobe_pdata {
32 static int testprobe_drv_probe(struct udevice *dev)
34 struct dm_testprobe_pdata *pdata = dev_get_plat(dev);
36 return pdata->probe_err;
39 static const struct udevice_id testprobe_ids[] = {
40 { .compatible = "denx,u-boot-probe-test" },
44 U_BOOT_DRIVER(testprobe_drv) = {
45 .name = "testprobe_drv",
46 .of_match = testprobe_ids,
47 .id = UCLASS_TEST_PROBE,
48 .probe = testprobe_drv_probe,
49 .plat_auto = sizeof(struct dm_testprobe_pdata),
52 UCLASS_DRIVER(testprobe) = {
54 .id = UCLASS_TEST_PROBE,
55 .flags = DM_UC_FLAG_SEQ_ALIAS,
58 struct dm_testdevres_pdata {
62 struct dm_testdevres_priv {
67 static int testdevres_drv_bind(struct udevice *dev)
69 struct dm_testdevres_pdata *pdata = dev_get_plat(dev);
71 pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
76 static int testdevres_drv_of_to_plat(struct udevice *dev)
78 struct dm_testdevres_priv *priv = dev_get_priv(dev);
80 priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0);
85 static int testdevres_drv_probe(struct udevice *dev)
87 struct dm_testdevres_priv *priv = dev_get_priv(dev);
89 priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0);
94 static const struct udevice_id testdevres_ids[] = {
95 { .compatible = "denx,u-boot-devres-test" },
99 U_BOOT_DRIVER(testdevres_drv) = {
100 .name = "testdevres_drv",
101 .of_match = testdevres_ids,
102 .id = UCLASS_TEST_DEVRES,
103 .bind = testdevres_drv_bind,
104 .of_to_plat = testdevres_drv_of_to_plat,
105 .probe = testdevres_drv_probe,
106 .plat_auto = sizeof(struct dm_testdevres_pdata),
107 .priv_auto = sizeof(struct dm_testdevres_priv),
110 UCLASS_DRIVER(testdevres) = {
111 .name = "testdevres",
112 .id = UCLASS_TEST_DEVRES,
113 .flags = DM_UC_FLAG_SEQ_ALIAS,
116 int dm_check_devices(struct unit_test_state *uts, int num_devices)
123 * Now check that the ping adds are what we expect. This is using the
124 * ping-add property in each node.
126 for (i = 0; i < num_devices; i++) {
129 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
133 * Get the 'ping-expect' property, which tells us what the
134 * ping add should be. We don't use the plat because we
135 * want to test the code that sets that up
136 * (testfdt_drv_probe()).
138 base = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
140 debug("dev=%d, base=%d: %s\n", i, base,
141 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
143 ut_assert(!dm_check_operations(uts, dev, base,
150 /* Test that FDT-based binding works correctly */
151 static int dm_test_fdt(struct unit_test_state *uts)
153 const int num_devices = 9;
159 ret = dm_extended_scan(false);
162 ret = uclass_get(UCLASS_TEST_FDT, &uc);
165 /* These are num_devices compatible root-level device tree nodes */
166 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
168 /* Each should have platform data but no private data */
169 for (i = 0; i < num_devices; i++) {
170 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
172 ut_assert(!dev_get_priv(dev));
173 ut_assert(dev_get_plat(dev));
176 ut_assertok(dm_check_devices(uts, num_devices));
180 DM_TEST(dm_test_fdt, 0);
182 static int dm_test_alias_highest_id(struct unit_test_state *uts)
186 ret = dev_read_alias_highest_id("ethernet");
189 ret = dev_read_alias_highest_id("gpio");
192 ret = dev_read_alias_highest_id("pci");
195 ret = dev_read_alias_highest_id("i2c");
198 ret = dev_read_alias_highest_id("deadbeef");
199 ut_asserteq(-1, ret);
203 DM_TEST(dm_test_alias_highest_id, 0);
205 static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
210 ret = dm_scan_fdt(true);
213 ret = uclass_get(UCLASS_TEST_FDT, &uc);
217 * These are 2 pre-reloc devices:
218 * one with "bootph-all" property (a-test node), and the other
219 * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node).
221 ut_asserteq(2, list_count_items(&uc->dev_head));
225 DM_TEST(dm_test_fdt_pre_reloc, 0);
227 /* Test that sequence numbers are allocated properly */
228 static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
232 /* A few basic santiy tests */
233 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
234 ut_asserteq_str("b-test", dev->name);
235 ut_asserteq(3, dev_seq(dev));
237 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, &dev));
238 ut_asserteq_str("a-test", dev->name);
239 ut_asserteq(8, dev_seq(dev));
242 * This device has no alias so gets the next value after all available
243 * aliases. The last alias is testfdt12
245 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 13, &dev));
246 ut_asserteq_str("d-test", dev->name);
247 ut_asserteq(13, dev_seq(dev));
249 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
251 ut_asserteq_ptr(NULL, dev);
254 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
255 ut_asserteq_str("e-test", dev->name);
256 ut_asserteq(6, dev_seq(dev));
259 * Note that c-test nodes are not probed since it is not a top-level
262 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
263 ut_asserteq_str("b-test", dev->name);
264 ut_asserteq(3, dev_seq(dev));
267 * d-test wants sequence number 3 also, but it can't have it because
268 * b-test gets it first.
270 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
271 ut_asserteq_str("d-test", dev->name);
272 ut_asserteq(13, dev_seq(dev));
274 /* g-test gets the next value after f-test */
275 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 15, &dev));
276 ut_asserteq_str("g-test", dev->name);
277 ut_asserteq(15, dev_seq(dev));
279 /* And we should still have holes in our sequence numbers */
280 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 0,
282 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 1,
284 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2,
286 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4,
288 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
290 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
292 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 10,
294 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 11,
299 DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
301 /* More tests for sequence numbers */
302 static int dm_test_fdt_uclass_seq_manual(struct unit_test_state *uts)
307 * Since DM_UC_FLAG_NO_AUTO_SEQ is set for this uclass, only testfdtm1
308 * should get a sequence number assigned
310 ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 0, &dev));
311 ut_asserteq_str("testfdtm0", dev->name);
312 ut_asserteq(-1, dev_seq(dev));
314 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT_MANUAL, 1, &dev));
315 ut_asserteq_str("testfdtm1", dev->name);
316 ut_asserteq(1, dev_seq(dev));
318 ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 2, &dev));
319 ut_asserteq_str("testfdtm2", dev->name);
320 ut_asserteq(-1, dev_seq(dev));
324 DM_TEST(dm_test_fdt_uclass_seq_manual, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
326 static int dm_test_fdt_uclass_seq_more(struct unit_test_state *uts)
331 /* Check creating a device with an alias */
332 node = ofnode_path("/some-bus/c-test@1");
333 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test),
334 "c-test@1", NULL, node, &dev));
335 ut_asserteq(12, dev_seq(dev));
336 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 12, &dev));
337 ut_asserteq_str("c-test@1", dev->name);
340 * Now bind a device without an alias. It should not get the next
341 * sequence number after all aliases, and existing bound devices. The
342 * last alias is 12, so we have:
351 * So next available is 19
353 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test),
354 "fred", NULL, ofnode_null(), &dev));
355 ut_asserteq(19, dev_seq(dev));
357 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test),
358 "fred2", NULL, ofnode_null(), &dev));
359 ut_asserteq(20, dev_seq(dev));
363 DM_TEST(dm_test_fdt_uclass_seq_more, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
365 /* Test that we can find a device by device tree offset */
366 static int dm_test_fdt_offset(struct unit_test_state *uts)
368 const void *blob = gd->fdt_blob;
372 node = fdt_path_offset(blob, "/e-test");
374 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
376 ut_asserteq_str("e-test", dev->name);
378 /* This node should not be bound */
379 node = fdt_path_offset(blob, "/junk");
381 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
384 /* This is not a top level node so should not be probed */
385 node = fdt_path_offset(blob, "/some-bus/c-test@5");
387 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
392 DM_TEST(dm_test_fdt_offset,
393 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
396 * Test various error conditions with uclass_first_device(),
397 * uclass_next_device(), and uclass_probe_all()
399 static int dm_test_first_next_device_probeall(struct unit_test_state *uts)
401 struct dm_testprobe_pdata *pdata;
402 struct udevice *dev, *parent = NULL;
406 /* There should be 4 devices */
407 for (uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
409 uclass_next_device(&dev)) {
411 parent = dev_get_parent(dev);
413 ut_asserteq(4, count);
415 /* Remove them and try again, with an error on the second one */
416 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
417 pdata = dev_get_plat(dev);
418 pdata->probe_err = -ENOMEM;
419 device_remove(parent, DM_REMOVE_NORMAL);
420 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev),
423 ret = uclass_next_device_check(&dev)) {
427 ut_asserteq(-ENOMEM, ret);
428 parent = dev_get_parent(dev);
430 ut_asserteq(3, count);
432 /* Now an error on the first one */
433 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
434 pdata = dev_get_plat(dev);
435 pdata->probe_err = -ENOENT;
436 device_remove(parent, DM_REMOVE_NORMAL);
437 for (uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
439 uclass_next_device(&dev)) {
441 parent = dev_get_parent(dev);
443 ut_asserteq(2, count);
445 /* Now that broken devices are set up test probe_all */
446 device_remove(parent, DM_REMOVE_NORMAL);
447 /* There are broken devices so an error should be returned */
448 ut_assert(uclass_probe_all(UCLASS_TEST_PROBE) < 0);
449 /* but non-error device should be probed nonetheless */
450 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 2, &dev));
451 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
452 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 3, &dev));
453 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
457 DM_TEST(dm_test_first_next_device_probeall,
458 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
460 /* Test iteration through devices in a uclass */
461 static int dm_test_uclass_foreach(struct unit_test_state *uts)
468 uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc)
470 ut_asserteq(9, count);
473 uclass_foreach_dev(dev, uc)
475 ut_asserteq(9, count);
479 DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
482 * check_devices() - Check return values and pointers
484 * This runs through a full sequence of uclass_first_device_check()...
485 * uclass_next_device_check() checking that the return values and devices
489 * @devlist: List of expected devices
490 * @mask: Indicates which devices should return an error. Device n should
491 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
494 static int check_devices(struct unit_test_state *uts,
495 struct udevice *devlist[], int mask)
501 expected_ret = (mask & 1) ? -ENOENT : 0;
503 ut_asserteq(expected_ret,
504 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
505 for (i = 0; i < 4; i++) {
506 ut_asserteq_ptr(devlist[i], dev);
507 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
509 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
511 ut_asserteq_ptr(NULL, dev);
516 /* Test uclass_first_device_check() and uclass_next_device_check() */
517 static int dm_test_first_next_ok_device(struct unit_test_state *uts)
519 struct dm_testprobe_pdata *pdata;
520 struct udevice *dev, *parent = NULL, *devlist[4];
524 /* There should be 4 devices */
526 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
528 ret = uclass_next_device_check(&dev)) {
530 devlist[count++] = dev;
531 parent = dev_get_parent(dev);
533 ut_asserteq(4, count);
534 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
535 ut_assertok(check_devices(uts, devlist, 0));
537 /* Remove them and try again, with an error on the second one */
538 pdata = dev_get_plat(devlist[1]);
539 pdata->probe_err = -ENOENT - 1;
540 device_remove(parent, DM_REMOVE_NORMAL);
541 ut_assertok(check_devices(uts, devlist, 1 << 1));
543 /* Now an error on the first one */
544 pdata = dev_get_plat(devlist[0]);
545 pdata->probe_err = -ENOENT - 0;
546 device_remove(parent, DM_REMOVE_NORMAL);
547 ut_assertok(check_devices(uts, devlist, 3 << 0));
549 /* Now errors on all */
550 pdata = dev_get_plat(devlist[2]);
551 pdata->probe_err = -ENOENT - 2;
552 pdata = dev_get_plat(devlist[3]);
553 pdata->probe_err = -ENOENT - 3;
554 device_remove(parent, DM_REMOVE_NORMAL);
555 ut_assertok(check_devices(uts, devlist, 0xf << 0));
559 DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
561 static const struct udevice_id fdt_dummy_ids[] = {
562 { .compatible = "denx,u-boot-fdt-dummy", },
566 UCLASS_DRIVER(fdt_dummy) = {
568 .id = UCLASS_TEST_DUMMY,
569 .flags = DM_UC_FLAG_SEQ_ALIAS,
572 U_BOOT_DRIVER(fdt_dummy_drv) = {
573 .name = "fdt_dummy_drv",
574 .of_match = fdt_dummy_ids,
575 .id = UCLASS_TEST_DUMMY,
578 static int dm_test_fdt_translation(struct unit_test_state *uts)
583 /* Some simple translations */
584 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
585 ut_asserteq_str("dev@0,0", dev->name);
586 ut_asserteq(0x8000, dev_read_addr(dev));
588 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
589 ut_asserteq_str("dev@1,100", dev->name);
590 ut_asserteq(0x9000, dev_read_addr(dev));
592 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, &dev));
593 ut_asserteq_str("dev@2,200", dev->name);
594 ut_asserteq(0xA000, dev_read_addr(dev));
596 /* No translation for buses with #size-cells == 0 */
597 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, &dev));
598 ut_asserteq_str("dev@42", dev->name);
599 ut_asserteq(0x42, dev_read_addr(dev));
601 /* dma address translation */
602 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
603 dma_addr[0] = cpu_to_be32(0);
604 dma_addr[1] = cpu_to_be32(0);
605 ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
607 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
608 dma_addr[0] = cpu_to_be32(1);
609 dma_addr[1] = cpu_to_be32(0x100);
610 ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
614 DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
616 static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts)
618 struct udevice *gpio, *dev;
622 /* Test for missing reg property */
623 ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio));
624 ut_assertnull(devfdt_get_addr_ptr(gpio));
626 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
627 ptr = devfdt_get_addr_ptr(dev);
629 paddr = map_sysmem(0x8000, 0);
630 ut_asserteq_ptr(paddr, ptr);
634 DM_TEST(dm_test_fdt_get_addr_ptr_flat,
635 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
637 static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
643 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
645 addr = devfdt_get_addr(dev);
646 ut_asserteq(0x8000, addr);
648 paddr = map_physmem(addr, 0, MAP_NOCACHE);
649 ut_assertnonnull(paddr);
650 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
654 DM_TEST(dm_test_fdt_remap_addr_flat,
655 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
657 static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
664 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
666 addr = devfdt_get_addr_size_index(dev, 0, &size);
667 ut_asserteq(0x8000, addr);
668 ut_asserteq(0x1000, size);
670 paddr = map_physmem(addr, 0, MAP_NOCACHE);
671 ut_assertnonnull(paddr);
672 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
676 DM_TEST(dm_test_fdt_remap_addr_index_flat,
677 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
679 static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
686 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
688 addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
689 ut_asserteq(0x8000, addr);
690 ut_asserteq(0x1000, size);
692 paddr = map_physmem(addr, 0, MAP_NOCACHE);
693 ut_assertnonnull(paddr);
694 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
698 DM_TEST(dm_test_fdt_remap_addr_name_flat,
699 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
701 static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
707 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
709 addr = dev_read_addr(dev);
710 ut_asserteq(0x8000, addr);
712 paddr = map_physmem(addr, 0, MAP_NOCACHE);
713 ut_assertnonnull(paddr);
714 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
718 DM_TEST(dm_test_fdt_remap_addr_live,
719 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
721 static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
728 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
730 addr = dev_read_addr_size_index(dev, 0, &size);
731 ut_asserteq(0x8000, addr);
732 ut_asserteq(0x1000, size);
734 paddr = map_physmem(addr, 0, MAP_NOCACHE);
735 ut_assertnonnull(paddr);
736 ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
740 DM_TEST(dm_test_fdt_remap_addr_index_live,
741 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
743 static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
750 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
752 addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
753 ut_asserteq(0x8000, addr);
754 ut_asserteq(0x1000, size);
756 paddr = map_physmem(addr, 0, MAP_NOCACHE);
757 ut_assertnonnull(paddr);
758 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
762 DM_TEST(dm_test_fdt_remap_addr_name_live,
763 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
765 static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
769 if (!of_live_active()) {
770 printf("Live tree not active; ignore test\n");
774 node = ofnode_path("/usb@2");
776 /* Test enabling devices */
778 ut_assert(!of_device_is_available(ofnode_to_np(node)));
779 dev_enable_by_path("/usb@2");
780 ut_assert(of_device_is_available(ofnode_to_np(node)));
782 /* Test disabling devices */
784 ut_assert(of_device_is_available(ofnode_to_np(node)));
785 dev_disable_by_path("/usb@2");
786 ut_assert(!of_device_is_available(ofnode_to_np(node)));
790 DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA |
793 /* Test a few uclass phandle functions */
794 static int dm_test_fdt_phandle(struct unit_test_state *uts)
796 struct udevice *back, *dev, *dev2;
798 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
799 ut_assertnonnull(back);
800 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
801 back, "missing", &dev));
802 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
803 "power-supply", &dev));
804 ut_assertnonnull(dev);
805 ut_asserteq(0, device_active(dev));
806 ut_asserteq_str("ldo1", dev->name);
807 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
808 "power-supply", &dev2));
809 ut_asserteq_ptr(dev, dev2);
813 DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
815 /* Test device_find_first_child_by_uclass() */
816 static int dm_test_first_child(struct unit_test_state *uts)
818 struct udevice *i2c, *dev, *dev2;
820 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
821 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
822 ut_asserteq_str("rtc@43", dev->name);
823 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
824 ut_asserteq_ptr(dev, dev2);
825 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
826 ut_asserteq_str("rtc@61", dev2->name);
828 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
830 ut_asserteq_str("eeprom@2c", dev->name);
831 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
832 ut_asserteq_ptr(dev, dev2);
834 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
835 UCLASS_VIDEO, &dev));
836 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
840 DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
842 /* Test integer functions in dm_read_...() */
843 static int dm_test_read_int(struct unit_test_state *uts)
853 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
854 ut_asserteq_str("a-test", dev->name);
856 ut_assertok(dev_read_u8(dev, "int8-value", &val8));
857 ut_asserteq(0x12, val8);
859 ut_asserteq(-EINVAL, dev_read_u8(dev, "missing", &val8));
860 ut_asserteq(6, dev_read_u8_default(dev, "missing", 6));
862 ut_asserteq(0x12, dev_read_u8_default(dev, "int8-value", 6));
864 ut_assertok(dev_read_u16(dev, "int16-value", &val16));
865 ut_asserteq(0x1234, val16);
867 ut_asserteq(-EINVAL, dev_read_u16(dev, "missing", &val16));
868 ut_asserteq(6, dev_read_u16_default(dev, "missing", 6));
870 ut_asserteq(0x1234, dev_read_u16_default(dev, "int16-value", 6));
872 ut_assertok(dev_read_u32(dev, "int-value", &val32));
873 ut_asserteq(1234, val32);
875 ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
876 ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
878 ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
879 ut_asserteq(1234, val32);
881 ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
882 ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
884 ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
885 ut_assertok(dev_read_s32(dev, "uint-value", &sval));
886 ut_asserteq(-1234, sval);
889 ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
890 ut_assertok(dev_read_u32u(dev, "uint-value", &val));
891 ut_asserteq(-1234, val);
893 ut_assertok(dev_read_u64(dev, "int64-value", &val64));
894 ut_asserteq_64(0x1111222233334444, val64);
896 ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64));
897 ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6));
899 ut_asserteq_64(0x1111222233334444,
900 dev_read_u64_default(dev, "int64-value", 6));
904 DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
906 static int dm_test_read_int_index(struct unit_test_state *uts)
911 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
912 ut_asserteq_str("a-test", dev->name);
914 ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
915 ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
917 ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
918 ut_asserteq(5678, val32);
919 ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
920 ut_asserteq(9123, val32);
921 ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
922 ut_asserteq(4567, val32);
923 ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
926 ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
927 ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
928 ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
929 ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
933 DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
935 static int dm_test_read_phandle(struct unit_test_state *uts)
938 struct ofnode_phandle_args args;
940 const char prop[] = "test-gpios";
941 const char cell[] = "#gpio-cells";
942 const char prop2[] = "phandle-value";
944 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
945 ut_asserteq_str("a-test", dev->name);
947 /* Test dev_count_phandle_with_args with cell name */
948 ret = dev_count_phandle_with_args(dev, "missing", cell, 0);
949 ut_asserteq(-ENOENT, ret);
950 ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0);
951 ut_asserteq(-EINVAL, ret);
952 ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0));
954 /* Test dev_read_phandle_with_args with cell name */
955 ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args);
956 ut_asserteq(-ENOENT, ret);
957 ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args);
958 ut_asserteq(-EINVAL, ret);
959 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args));
960 ut_asserteq(1, args.args_count);
961 ut_asserteq(1, args.args[0]);
962 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args));
963 ut_asserteq(1, args.args_count);
964 ut_asserteq(4, args.args[0]);
965 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args));
966 ut_asserteq(5, args.args_count);
967 ut_asserteq(5, args.args[0]);
968 ut_asserteq(1, args.args[4]);
969 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args);
970 ut_asserteq(-ENOENT, ret);
971 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args));
972 ut_asserteq(1, args.args_count);
973 ut_asserteq(12, args.args[0]);
974 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args);
975 ut_asserteq(-ENOENT, ret);
977 /* Test dev_count_phandle_with_args with cell count */
978 ret = dev_count_phandle_with_args(dev, "missing", NULL, 2);
979 ut_asserteq(-ENOENT, ret);
980 ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1));
982 /* Test dev_read_phandle_with_args with cell count */
983 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args));
984 ut_asserteq(1, ofnode_valid(args.node));
985 ut_asserteq(1, args.args_count);
986 ut_asserteq(10, args.args[0]);
987 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args);
988 ut_asserteq(-EINVAL, ret);
989 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args));
990 ut_asserteq(1, ofnode_valid(args.node));
991 ut_asserteq(1, args.args_count);
992 ut_asserteq(30, args.args[0]);
993 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args);
994 ut_asserteq(-ENOENT, ret);
998 DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1000 /* Test iteration through devices by drvdata */
1001 static int dm_test_uclass_drvdata(struct unit_test_state *uts)
1003 struct udevice *dev;
1005 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1006 DM_TEST_TYPE_FIRST, &dev));
1007 ut_asserteq_str("a-test", dev->name);
1009 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1010 DM_TEST_TYPE_SECOND, &dev));
1011 ut_asserteq_str("d-test", dev->name);
1013 ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT,
1019 DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1021 /* Test device_first_child_ofdata_err(), etc. */
1022 static int dm_test_child_ofdata(struct unit_test_state *uts)
1024 struct udevice *bus, *dev;
1027 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1029 device_foreach_child_of_to_plat(dev, bus) {
1030 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1031 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
1034 ut_asserteq(3, count);
1038 DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1040 /* Test device_first_child_err(), etc. */
1041 static int dm_test_first_child_probe(struct unit_test_state *uts)
1043 struct udevice *bus, *dev;
1046 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1048 device_foreach_child_probe(dev, bus) {
1049 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1050 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
1053 ut_asserteq(3, count);
1057 DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1059 /* Test that ofdata is read for parents before children */
1060 static int dm_test_ofdata_order(struct unit_test_state *uts)
1062 struct udevice *bus, *dev;
1064 ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus));
1065 ut_assertnonnull(bus);
1066 ut_assert(!(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID));
1068 ut_assertok(device_find_first_child(bus, &dev));
1069 ut_assertnonnull(dev);
1070 ut_assert(!(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID));
1072 /* read the child's ofdata which should cause the parent's to be read */
1073 ut_assertok(device_of_to_plat(dev));
1074 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1075 ut_assert(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID);
1077 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
1078 ut_assert(!(dev_get_flags(bus) & DM_FLAG_ACTIVATED));
1082 DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1084 /* Test dev_decode_display_timing() */
1085 static int dm_test_decode_display_timing(struct unit_test_state *uts)
1087 struct udevice *dev;
1088 struct display_timing timing;
1090 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1091 ut_asserteq_str("a-test", dev->name);
1093 ut_assertok(dev_decode_display_timing(dev, 0, &timing));
1094 ut_assert(timing.hactive.typ == 240);
1095 ut_assert(timing.hback_porch.typ == 7);
1096 ut_assert(timing.hfront_porch.typ == 6);
1097 ut_assert(timing.hsync_len.typ == 1);
1098 ut_assert(timing.vactive.typ == 320);
1099 ut_assert(timing.vback_porch.typ == 5);
1100 ut_assert(timing.vfront_porch.typ == 8);
1101 ut_assert(timing.vsync_len.typ == 2);
1102 ut_assert(timing.pixelclock.typ == 6500000);
1103 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH);
1104 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1105 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1106 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_LOW);
1107 ut_assert(timing.flags & DISPLAY_FLAGS_DE_HIGH);
1108 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1109 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE);
1110 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1111 ut_assert(timing.flags & DISPLAY_FLAGS_INTERLACED);
1112 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLESCAN);
1113 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLECLK);
1115 ut_assertok(dev_decode_display_timing(dev, 1, &timing));
1116 ut_assert(timing.hactive.typ == 480);
1117 ut_assert(timing.hback_porch.typ == 59);
1118 ut_assert(timing.hfront_porch.typ == 10);
1119 ut_assert(timing.hsync_len.typ == 12);
1120 ut_assert(timing.vactive.typ == 800);
1121 ut_assert(timing.vback_porch.typ == 15);
1122 ut_assert(timing.vfront_porch.typ == 17);
1123 ut_assert(timing.vsync_len.typ == 16);
1124 ut_assert(timing.pixelclock.typ == 9000000);
1125 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH));
1126 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_LOW);
1127 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH);
1128 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW));
1129 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH));
1130 ut_assert(timing.flags & DISPLAY_FLAGS_DE_LOW);
1131 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE));
1132 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE);
1133 ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED));
1134 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN));
1135 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK));
1137 ut_assertok(dev_decode_display_timing(dev, 2, &timing));
1138 ut_assert(timing.hactive.typ == 800);
1139 ut_assert(timing.hback_porch.typ == 89);
1140 ut_assert(timing.hfront_porch.typ == 164);
1141 ut_assert(timing.hsync_len.typ == 11);
1142 ut_assert(timing.vactive.typ == 480);
1143 ut_assert(timing.vback_porch.typ == 23);
1144 ut_assert(timing.vfront_porch.typ == 10);
1145 ut_assert(timing.vsync_len.typ == 13);
1146 ut_assert(timing.pixelclock.typ == 33500000);
1147 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH));
1148 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1149 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1150 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW));
1151 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH));
1152 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1153 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE));
1154 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1155 ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED));
1156 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN));
1157 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK));
1159 ut_assert(dev_decode_display_timing(dev, 3, &timing));
1162 DM_TEST(dm_test_decode_display_timing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1164 /* Test dev_decode_panel_timing() */
1165 static int dm_test_decode_panel_timing(struct unit_test_state *uts)
1167 struct udevice *dev;
1168 struct display_timing timing;
1170 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1171 ut_asserteq_str("a-test", dev->name);
1173 ut_assertok(dev_decode_panel_timing(dev, &timing));
1174 ut_assert(timing.hactive.typ == 240);
1175 ut_assert(timing.hback_porch.typ == 7);
1176 ut_assert(timing.hfront_porch.typ == 6);
1177 ut_assert(timing.hsync_len.typ == 1);
1178 ut_assert(timing.vactive.typ == 320);
1179 ut_assert(timing.vback_porch.typ == 5);
1180 ut_assert(timing.vfront_porch.typ == 8);
1181 ut_assert(timing.vsync_len.typ == 2);
1182 ut_assert(timing.pixelclock.typ == 6500000);
1183 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH);
1184 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1185 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1186 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_LOW);
1187 ut_assert(timing.flags & DISPLAY_FLAGS_DE_HIGH);
1188 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1189 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE);
1190 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1191 ut_assert(timing.flags & DISPLAY_FLAGS_INTERLACED);
1192 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLESCAN);
1193 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLECLK);
1197 DM_TEST(dm_test_decode_panel_timing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1199 /* Test read_resourcee() */
1200 static int dm_test_read_resource(struct unit_test_state *uts)
1202 struct udevice *dev;
1203 struct resource res;
1205 /* test resource without translation */
1206 ut_assertok(uclass_find_device_by_name(UCLASS_SIMPLE_BUS, "syscon@2", &dev));
1207 ut_assertok(dev_read_resource(dev, 0, &res));
1208 ut_asserteq(0x40, res.start);
1209 ut_asserteq(0x44, res.end);
1210 ut_assertok(dev_read_resource(dev, 1, &res));
1211 ut_asserteq(0x48, res.start);
1212 ut_asserteq(0x4d, res.end);
1214 /* test resource with translation */
1215 ut_assertok(uclass_find_device_by_name(UCLASS_TEST_DUMMY, "dev@1,100", &dev));
1216 ut_assertok(dev_read_resource(dev, 0, &res));
1217 ut_asserteq(0x9000, res.start);
1218 ut_asserteq(0x9fff, res.end);
1220 /* test named resource */
1221 ut_assertok(uclass_find_device_by_name(UCLASS_TEST_DUMMY, "dev@0,0", &dev));
1222 ut_assertok(dev_read_resource_byname(dev, "sandbox-dummy-0", &res));
1223 ut_asserteq(0x8000, res.start);
1224 ut_asserteq(0x8fff, res.end);
1229 DM_TEST(dm_test_read_resource, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);