+
+static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
+{
+ struct udevice *dev;
+ fdt_addr_t addr;
+ void *paddr;
+
+ ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
+
+ addr = devfdt_get_addr(dev);
+ ut_asserteq(0x8000, addr);
+
+ paddr = map_physmem(addr, 0, MAP_NOCACHE);
+ ut_assertnonnull(paddr);
+ ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
+
+ return 0;
+}
+DM_TEST(dm_test_fdt_remap_addr_flat,
+ DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
+
+static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
+{
+ struct udevice *dev;
+ fdt_addr_t addr;
+ void *paddr;
+
+ ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
+
+ addr = devfdt_get_addr_index(dev, 0);
+ ut_asserteq(0x8000, addr);
+
+ paddr = map_physmem(addr, 0, MAP_NOCACHE);
+ ut_assertnonnull(paddr);
+ ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
+
+ return 0;
+}
+DM_TEST(dm_test_fdt_remap_addr_index_flat,
+ DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
+
+static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
+{
+ struct udevice *dev;
+ fdt_addr_t addr;
+ void *paddr;
+
+ ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
+
+ addr = devfdt_get_addr_name(dev, "sandbox-dummy-0");
+ ut_asserteq(0x8000, addr);
+
+ paddr = map_physmem(addr, 0, MAP_NOCACHE);
+ ut_assertnonnull(paddr);
+ ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
+
+ return 0;
+}
+DM_TEST(dm_test_fdt_remap_addr_name_flat,
+ DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
+
+static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
+{
+ struct udevice *dev;
+ fdt_addr_t addr;
+ void *paddr;
+
+ ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
+
+ addr = dev_read_addr(dev);
+ ut_asserteq(0x8000, addr);
+
+ paddr = map_physmem(addr, 0, MAP_NOCACHE);
+ ut_assertnonnull(paddr);
+ ut_asserteq_ptr(paddr, dev_remap_addr(dev));
+
+ return 0;
+}
+DM_TEST(dm_test_fdt_remap_addr_live,
+ DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
+{
+ struct udevice *dev;
+ fdt_addr_t addr;
+ void *paddr;
+
+ ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
+
+ addr = dev_read_addr_index(dev, 0);
+ ut_asserteq(0x8000, addr);
+
+ paddr = map_physmem(addr, 0, MAP_NOCACHE);
+ ut_assertnonnull(paddr);
+ ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
+
+ return 0;
+}
+DM_TEST(dm_test_fdt_remap_addr_index_live,
+ DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
+{
+ struct udevice *dev;
+ fdt_addr_t addr;
+ void *paddr;
+
+ ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
+
+ addr = dev_read_addr_name(dev, "sandbox-dummy-0");
+ ut_asserteq(0x8000, addr);
+
+ paddr = map_physmem(addr, 0, MAP_NOCACHE);
+ ut_assertnonnull(paddr);
+ ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
+
+ return 0;
+}
+DM_TEST(dm_test_fdt_remap_addr_name_live,
+ DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
+{
+ struct udevice *dev;
+ ofnode node;
+
+ if (!of_live_active()) {
+ printf("Live tree not active; ignore test\n");
+ return 0;
+ }
+
+ /* Test enabling devices */
+
+ node = ofnode_path("/usb@2");
+
+ ut_assert(!of_device_is_available(ofnode_to_np(node)));
+ ofnode_set_enabled(node, true);
+ ut_assert(of_device_is_available(ofnode_to_np(node)));
+
+ device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
+ &dev);
+ ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, true, &dev));
+
+ /* Test string property setting */
+
+ ut_assert(device_is_compatible(dev, "sandbox,usb"));
+ ofnode_write_string(node, "compatible", "gdsys,super-usb");
+ ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
+ ofnode_write_string(node, "compatible", "sandbox,usb");
+ ut_assert(device_is_compatible(dev, "sandbox,usb"));
+
+ /* Test setting generic properties */
+
+ /* Non-existent in DTB */
+ ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
+ /* reg = 0x42, size = 0x100 */
+ ut_assertok(ofnode_write_prop(node, "reg", 8,
+ "\x00\x00\x00\x42\x00\x00\x01\x00"));
+ ut_asserteq(0x42, dev_read_addr(dev));
+
+ /* Test disabling devices */
+
+ device_remove(dev, DM_REMOVE_NORMAL);
+ device_unbind(dev);
+
+ ut_assert(of_device_is_available(ofnode_to_np(node)));
+ ofnode_set_enabled(node, false);
+ ut_assert(!of_device_is_available(ofnode_to_np(node)));
+
+ return 0;
+}
+DM_TEST(dm_test_fdt_livetree_writing, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
+{
+ ofnode node;
+
+ if (!of_live_active()) {
+ printf("Live tree not active; ignore test\n");
+ return 0;
+ }
+
+ node = ofnode_path("/usb@2");
+
+ /* Test enabling devices */
+
+ ut_assert(!of_device_is_available(ofnode_to_np(node)));
+ dev_enable_by_path("/usb@2");
+ ut_assert(of_device_is_available(ofnode_to_np(node)));
+
+ /* Test disabling devices */
+
+ ut_assert(of_device_is_available(ofnode_to_np(node)));
+ dev_disable_by_path("/usb@2");
+ ut_assert(!of_device_is_available(ofnode_to_np(node)));
+
+ return 0;
+}
+DM_TEST(dm_test_fdt_disable_enable_by_path, DM_TESTF_SCAN_PDATA |
+ DM_TESTF_SCAN_FDT);
+
+/* Test a few uclass phandle functions */
+static int dm_test_fdt_phandle(struct unit_test_state *uts)
+{
+ struct udevice *back, *dev, *dev2;
+
+ ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
+ ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
+ back, "missing", &dev));
+ ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
+ "power-supply", &dev));
+ ut_asserteq(0, device_active(dev));
+ ut_asserteq_str("ldo1", dev->name);
+ ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
+ "power-supply", &dev2));
+ ut_asserteq_ptr(dev, dev2);
+
+ return 0;
+}
+DM_TEST(dm_test_fdt_phandle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test device_find_first_child_by_uclass() */
+static int dm_test_first_child(struct unit_test_state *uts)
+{
+ struct udevice *i2c, *dev, *dev2;
+
+ ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
+ ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
+ ut_asserteq_str("rtc@43", dev->name);
+ ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
+ ut_asserteq_ptr(dev, dev2);
+ ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
+ ut_asserteq_str("rtc@61", dev2->name);
+
+ ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
+ &dev));
+ ut_asserteq_str("eeprom@2c", dev->name);
+ ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
+ ut_asserteq_ptr(dev, dev2);
+
+ ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
+ UCLASS_VIDEO, &dev));
+ ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
+
+ return 0;
+}
+DM_TEST(dm_test_first_child, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);