Merge tag 'uniphier-v2019.10' of https://gitlab.denx.de/u-boot/custodians/u-boot...
[oweals/u-boot.git] / test / dm / core.c
index 07b2419ea4a3fe31c3cde2349ac57deec12a934e..edd55b05d6e293ebe9856b0e7da3d894a778fe79 100644 (file)
@@ -1,9 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Tests for the core driver model code
  *
  * Copyright (c) 2013 Google, Inc
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
@@ -67,6 +66,10 @@ static struct driver_info driver_info_pre_reloc = {
        .platdata = &test_pdata_pre_reloc,
 };
 
+static struct driver_info driver_info_act_dma = {
+       .name = "test_act_dma_drv",
+};
+
 void dm_leak_check_start(struct unit_test_state *uts)
 {
        uts->start = mallinfo();
@@ -656,6 +659,68 @@ static int dm_test_pre_reloc(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_pre_reloc, 0);
 
+/*
+ * Test that removal of devices, either via the "normal" device_remove()
+ * API or via the device driver selective flag works as expected
+ */
+static int dm_test_remove_active_dma(struct unit_test_state *uts)
+{
+       struct dm_test_state *dms = uts->priv;
+       struct udevice *dev;
+
+       ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
+                                       &dev));
+       ut_assert(dev);
+
+       /* Probe the device */
+       ut_assertok(device_probe(dev));
+
+       /* Test if device is active right now */
+       ut_asserteq(true, device_active(dev));
+
+       /* Remove the device via selective remove flag */
+       dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
+
+       /* Test if device is inactive right now */
+       ut_asserteq(false, device_active(dev));
+
+       /* Probe the device again */
+       ut_assertok(device_probe(dev));
+
+       /* Test if device is active right now */
+       ut_asserteq(true, device_active(dev));
+
+       /* Remove the device via "normal" remove API */
+       ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
+
+       /* Test if device is inactive right now */
+       ut_asserteq(false, device_active(dev));
+
+       /*
+        * Test if a device without the active DMA flags is not removed upon
+        * the active DMA remove call
+        */
+       ut_assertok(device_unbind(dev));
+       ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
+                                       &dev));
+       ut_assert(dev);
+
+       /* Probe the device */
+       ut_assertok(device_probe(dev));
+
+       /* Test if device is active right now */
+       ut_asserteq(true, device_active(dev));
+
+       /* Remove the device via selective remove flag */
+       dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
+
+       /* Test if device is still active right now */
+       ut_asserteq(true, device_active(dev));
+
+       return 0;
+}
+DM_TEST(dm_test_remove_active_dma, 0);
+
 static int dm_test_uclass_before_ready(struct unit_test_state *uts)
 {
        struct uclass *uc;
@@ -684,6 +749,10 @@ static int dm_test_uclass_devices_find(struct unit_test_state *uts)
                ut_assert(dev);
        }
 
+       ret = uclass_find_first_device(UCLASS_TEST_DUMMY, &dev);
+       ut_assert(ret == -ENODEV);
+       ut_assert(!dev);
+
        return 0;
 }
 DM_TEST(dm_test_uclass_devices_find, DM_TESTF_SCAN_PDATA);
@@ -796,3 +865,43 @@ static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
        return 0;
 }
 DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);
+
+static int dm_test_uclass_names(struct unit_test_state *uts)
+{
+       ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
+       ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
+
+       return 0;
+}
+DM_TEST(dm_test_uclass_names, DM_TESTF_SCAN_PDATA);
+
+static int dm_test_inactive_child(struct unit_test_state *uts)
+{
+       struct dm_test_state *dms = uts->priv;
+       struct udevice *parent, *dev1, *dev2;
+
+       /* Skip the behaviour in test_post_probe() */
+       dms->skip_post_probe = 1;
+
+       ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
+
+       /*
+        * Create a child but do not activate it. Calling the function again
+        * should return the same child.
+        */
+       ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
+                                                       UCLASS_TEST, &dev1));
+       ut_assertok(device_bind_ofnode(parent, DM_GET_DRIVER(test_drv),
+                                      "test_child", 0, ofnode_null(), &dev1));
+
+       ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
+                                                    &dev2));
+       ut_asserteq_ptr(dev1, dev2);
+
+       ut_assertok(device_probe(dev1));
+       ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
+                                                       UCLASS_TEST, &dev2));
+
+       return 0;
+}
+DM_TEST(dm_test_inactive_child, DM_TESTF_SCAN_PDATA);