Merge branch 'master' of git://www.denx.de/git/u-boot-socfpga
[oweals/u-boot.git] / test / dm / core.c
index ff5c2a749c5e37dbbcca5340dabdf18374a8e5c2..91be1e5d4382d827cf1be47214f5e9d580bc19e9 100644 (file)
@@ -129,6 +129,61 @@ static int dm_test_autobind(struct dm_test_state *dms)
 }
 DM_TEST(dm_test_autobind, 0);
 
+/* Test that binding with uclass platdata allocation occurs correctly */
+static int dm_test_autobind_uclass_pdata_alloc(struct dm_test_state *dms)
+{
+       struct dm_test_perdev_uc_pdata *uc_pdata;
+       struct udevice *dev;
+       struct uclass *uc;
+
+       ut_assertok(uclass_get(UCLASS_TEST, &uc));
+       ut_assert(uc);
+
+       /**
+        * Test if test uclass driver requires allocation for the uclass
+        * platform data and then check the dev->uclass_platdata pointer.
+        */
+       ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size);
+
+       for (uclass_find_first_device(UCLASS_TEST, &dev);
+            dev;
+            uclass_find_next_device(&dev)) {
+               ut_assert(dev);
+
+               uc_pdata = dev_get_uclass_platdata(dev);
+               ut_assert(uc_pdata);
+       }
+
+       return 0;
+}
+DM_TEST(dm_test_autobind_uclass_pdata_alloc, DM_TESTF_SCAN_PDATA);
+
+/* Test that binding with uclass platdata setting occurs correctly */
+static int dm_test_autobind_uclass_pdata_valid(struct dm_test_state *dms)
+{
+       struct dm_test_perdev_uc_pdata *uc_pdata;
+       struct udevice *dev;
+
+       /**
+        * In the test_postbind() method of test uclass driver, the uclass
+        * platform data should be set to three test int values - test it.
+        */
+       for (uclass_find_first_device(UCLASS_TEST, &dev);
+            dev;
+            uclass_find_next_device(&dev)) {
+               ut_assert(dev);
+
+               uc_pdata = dev_get_uclass_platdata(dev);
+               ut_assert(uc_pdata);
+               ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
+               ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
+               ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
+       }
+
+       return 0;
+}
+DM_TEST(dm_test_autobind_uclass_pdata_valid, DM_TESTF_SCAN_PDATA);
+
 /* Test that autoprobe finds all the expected devices */
 static int dm_test_autoprobe(struct dm_test_state *dms)
 {
@@ -141,6 +196,7 @@ static int dm_test_autoprobe(struct dm_test_state *dms)
        ut_assert(uc);
 
        ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
+       ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
        ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
 
        /* The root device should not be activated until needed */
@@ -167,8 +223,12 @@ static int dm_test_autoprobe(struct dm_test_state *dms)
                        ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
        }
 
-       /* Our 3 dm_test_infox children should be passed to post_probe */
+       /*
+        * Our 3 dm_test_info children should be passed to pre_probe and
+        * post_probe
+        */
        ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
+       ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
 
        /* Also we can check the per-device data */
        expected_base_add = 0;
@@ -179,7 +239,7 @@ static int dm_test_autoprobe(struct dm_test_state *dms)
                ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
                ut_assert(dev);
 
-               priv = dev->uclass_priv;
+               priv = dev_get_uclass_priv(dev);
                ut_assert(priv);
                ut_asserteq(expected_base_add, priv->base_add);
 
@@ -591,10 +651,137 @@ static int dm_test_uclass_before_ready(struct dm_test_state *dms)
 
        ut_assertok(uclass_get(UCLASS_TEST, &uc));
 
-       memset(gd, '\0', sizeof(*gd));
+       gd->dm_root = NULL;
+       gd->dm_root_f = NULL;
+       memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
+
        ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
 
        return 0;
 }
-
 DM_TEST(dm_test_uclass_before_ready, 0);
+
+static int dm_test_uclass_devices_find(struct dm_test_state *dms)
+{
+       struct udevice *dev;
+       int ret;
+
+       for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
+            dev;
+            ret = uclass_find_next_device(&dev)) {
+               ut_assert(!ret);
+               ut_assert(dev);
+       }
+
+       return 0;
+}
+DM_TEST(dm_test_uclass_devices_find, DM_TESTF_SCAN_PDATA);
+
+static int dm_test_uclass_devices_find_by_name(struct dm_test_state *dms)
+{
+       struct udevice *finddev;
+       struct udevice *testdev;
+       int findret, ret;
+
+       /*
+        * For each test device found in fdt like: "a-test", "b-test", etc.,
+        * use its name and try to find it by uclass_find_device_by_name().
+        * Then, on success check if:
+        * - current 'testdev' name is equal to the returned 'finddev' name
+        * - current 'testdev' pointer is equal to the returned 'finddev'
+        *
+        * We assume that, each uclass's device name is unique, so if not, then
+        * this will fail on checking condition: testdev == finddev, since the
+        * uclass_find_device_by_name(), returns the first device by given name.
+       */
+       for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
+            testdev;
+            ret = uclass_find_next_device(&testdev)) {
+               ut_assertok(ret);
+               ut_assert(testdev);
+
+               findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
+                                                    testdev->name,
+                                                    &finddev);
+
+               ut_assertok(findret);
+               ut_assert(testdev);
+               ut_asserteq_str(testdev->name, finddev->name);
+               ut_asserteq_ptr(testdev, finddev);
+       }
+
+       return 0;
+}
+DM_TEST(dm_test_uclass_devices_find_by_name, DM_TESTF_SCAN_FDT);
+
+static int dm_test_uclass_devices_get(struct dm_test_state *dms)
+{
+       struct udevice *dev;
+       int ret;
+
+       for (ret = uclass_first_device(UCLASS_TEST, &dev);
+            dev;
+            ret = uclass_next_device(&dev)) {
+               ut_assert(!ret);
+               ut_assert(dev);
+               ut_assert(device_active(dev));
+       }
+
+       return 0;
+}
+DM_TEST(dm_test_uclass_devices_get, DM_TESTF_SCAN_PDATA);
+
+static int dm_test_uclass_devices_get_by_name(struct dm_test_state *dms)
+{
+       struct udevice *finddev;
+       struct udevice *testdev;
+       int ret, findret;
+
+       /*
+        * For each test device found in fdt like: "a-test", "b-test", etc.,
+        * use its name and try to get it by uclass_get_device_by_name().
+        * On success check if:
+        * - returned finddev' is active
+        * - current 'testdev' name is equal to the returned 'finddev' name
+        * - current 'testdev' pointer is equal to the returned 'finddev'
+        *
+        * We asserts that the 'testdev' is active on each loop entry, so we
+        * could be sure that the 'finddev' is activated too, but for sure
+        * we check it again.
+        *
+        * We assume that, each uclass's device name is unique, so if not, then
+        * this will fail on checking condition: testdev == finddev, since the
+        * uclass_get_device_by_name(), returns the first device by given name.
+       */
+       for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
+            testdev;
+            ret = uclass_next_device(&testdev)) {
+               ut_assertok(ret);
+               ut_assert(testdev);
+               ut_assert(device_active(testdev));
+
+               findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
+                                                   testdev->name,
+                                                   &finddev);
+
+               ut_assertok(findret);
+               ut_assert(finddev);
+               ut_assert(device_active(finddev));
+               ut_asserteq_str(testdev->name, finddev->name);
+               ut_asserteq_ptr(testdev, finddev);
+       }
+
+       return 0;
+}
+DM_TEST(dm_test_uclass_devices_get_by_name, DM_TESTF_SCAN_FDT);
+
+static int dm_test_device_get_uclass_id(struct dm_test_state *dms)
+{
+       struct udevice *dev;
+
+       ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
+       ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
+
+       return 0;
+}
+DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);