At present dm_test_ofnode_root() does this manually. Add some inline
functions to handle it, so this code can be centralised.

Add oftree functions to produce a null tree and to check whether a tree
is valid or not.

Signed-off-by: Simon Glass <[email protected]>
---

(no changes since v1)

 include/dm/ofnode.h | 63 +++++++++++++++++++++++++++++++++++++++++++++
 test/dm/ofnode.c    |  7 +++--
 2 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index 0423c072402..661f38bdc69 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -173,6 +173,38 @@ static inline bool ofnode_equal(ofnode ref1, ofnode ref2)
        return ref1.of_offset == ref2.of_offset;
 }
 
+/**
+ * oftree_valid() - check if an oftree is valid
+ *
+ * @tree: Reference containing oftree
+ * Return: true if the reference contains a valid oftree, false if node
+ */
+static inline bool oftree_valid(oftree tree)
+{
+       if (of_live_active())
+               return tree.np;
+       else
+               return tree.fdt;
+}
+
+/**
+ * oftree_null() - Obtain a null oftree
+ *
+ * This returns an oftree which points to no tree. It works both with the flat
+ * tree and livetree.
+ */
+static inline oftree oftree_null(void)
+{
+       oftree tree;
+
+       if (of_live_active())
+               tree.np = NULL;
+       else
+               tree.fdt = NULL;
+
+       return tree;
+}
+
 /**
  * ofnode_null() - Obtain a null ofnode
  *
@@ -234,6 +266,37 @@ static inline oftree oftree_default(void)
        return tree;
 }
 
+/**
+ * oftree_from_np() - Returns an oftree from a node pointer
+ *
+ * @root: Root node of the tree
+ * Returns: reference to the given node
+ */
+static inline oftree oftree_from_np(struct device_node *root)
+{
+       oftree tree;
+
+       tree.np = root;
+
+       return tree;
+}
+
+/**
+ * oftree_from_fdt() - Returns an oftree from a flat device tree pointer
+ *
+ * @fdt: Device tree to use
+ *
+ * Returns: reference to the given node
+ */
+static inline oftree oftree_from_fdt(void *fdt)
+{
+       oftree tree;
+
+       tree.fdt = fdt;
+
+       return tree;
+}
+
 /**
  * ofnode_name_eq() - Check if the node name is equivalent to a given name
  *                    ignoring the unit address
diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c
index eac0c50364a..f146b52d625 100644
--- a/test/dm/ofnode.c
+++ b/test/dm/ofnode.c
@@ -518,13 +518,16 @@ static int dm_test_ofnode_root(struct unit_test_state 
*uts)
        ut_assert(ofnode_valid(node));
        ut_asserteq_str("sbe5", ofnode_get_name(node));
 
+       ut_assert(!oftree_valid(oftree_null()));
+
        ut_assertok(make_ofnode_fdt(uts, fdt, sizeof(fdt)));
        if (of_live_active()) {
                ut_assertok(unflatten_device_tree(fdt, &root));
-               tree.np = root;
+               tree = oftree_from_np(root);
        } else {
-               tree.fdt = fdt;
+               tree = oftree_from_fdt(fdt);
        }
+       ut_assert(oftree_valid(tree));
 
        /* Make sure they don't work on this new tree */
        node = ofnode_path_root(tree, "mmc0");
-- 
2.37.2.789.g6183377224-goog

Reply via email to