dm: core: Split ofnode_path_root() into two functions
authorSimon Glass <sjg@chromium.org>
Wed, 7 Sep 2022 02:27:24 +0000 (20:27 -0600)
committerTom Rini <trini@konsulko.com>
Fri, 30 Sep 2022 02:43:43 +0000 (22:43 -0400)
This function turns out to be a little confusing since it looks up a path
and also registers the tree. Split it into two, one that gets the root
node and one that looks up a path, so the purpose is clear.

Registering the tree will happen in a function to be added in a later
patch, called oftree_from_fdt().

Signed-off-by: Simon Glass <sjg@chromium.org>
boot/vbe_simple.c
drivers/core/ofnode.c
include/dm/ofnode.h
test/boot/vbe_simple.c
test/dm/ofnode.c

index 0fc5738..61b6322 100644 (file)
@@ -240,7 +240,7 @@ static int bootmeth_vbe_simple_ft_fixup(void *ctx, struct event *event)
                        continue;
 
                /* Check if there is a node to fix up */
-               node = ofnode_path_root(tree, "/chosen/fwupd");
+               node = oftree_path(tree, "/chosen/fwupd");
                if (!ofnode_valid(node))
                        continue;
                node = ofnode_find_subnode(node, dev->name);
index b1ba8c5..7f6c47f 100644 (file)
 #include <linux/ioport.h>
 #include <asm/global_data.h>
 
+/**
+ * ofnode_from_tree_offset() - get an ofnode from a tree offset (flat tree)
+ *
+ * Looks up the tree and returns an ofnode with the correct of_offset
+ *
+ * If @offset is < 0 then this returns an ofnode with that offset
+ *
+ * @tree: tree to check
+ * @offset: offset within that tree (can be < 0)
+ * @return node for that offset
+ */
+static ofnode ofnode_from_tree_offset(oftree tree, int offset)
+{
+       ofnode node;
+
+       node.of_offset = offset;
+
+       return node;
+}
+
 bool ofnode_name_eq(ofnode node, const char *name)
 {
        const char *node_name;
@@ -640,15 +660,27 @@ ofnode ofnode_path(const char *path)
                return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
 }
 
-ofnode ofnode_path_root(oftree tree, const char *path)
+ofnode oftree_root(oftree tree)
 {
-       if (of_live_active())
+       if (of_live_active()) {
+               return np_to_ofnode(tree.np);
+       } else {
+               return ofnode_from_tree_offset(tree, 0);
+       }
+}
+
+ofnode oftree_path(oftree tree, const char *path)
+{
+       if (of_live_active()) {
                return np_to_ofnode(of_find_node_opts_by_path(tree.np, path,
                                                              NULL));
-       else if (*path != '/' && tree.fdt != gd->fdt_blob)
+       } else if (*path != '/' && tree.fdt != gd->fdt_blob) {
                return ofnode_null();  /* Aliases only on control FDT */
-       else
-               return offset_to_ofnode(fdt_path_offset(tree.fdt, path));
+       } else {
+               int offset = fdt_path_offset(tree.fdt, path);
+
+               return ofnode_from_tree_offset(tree, offset);
+       }
 }
 
 const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
index a53cffb..ad179a6 100644 (file)
@@ -855,18 +855,28 @@ int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
 ofnode ofnode_path(const char *path);
 
 /**
- * ofnode_path_root() - find a node by full path from a root node
+ * oftree_path() - find a node by full path from a root node
  *
  * @tree: Device tree to use
  * @path: Full path to node, e.g. "/bus/spi@1"
  * Return: reference to the node found. Use ofnode_valid() to check if it exists
  */
-ofnode ofnode_path_root(oftree tree, const char *path);
+ofnode oftree_path(oftree tree, const char *path);
+
+/**
+ * oftree_root() - get the root node of a tree
+ *
+ * @tree: Device tree to use
+ * Return: reference to the root node
+ */
+ofnode oftree_root(oftree tree);
 
 /**
  * ofnode_read_chosen_prop() - get the value of a chosen property
  *
- * This looks for a property within the /chosen node and returns its value
+ * This looks for a property within the /chosen node and returns its value.
+ *
+ * This only works with the control FDT.
  *
  * @propname: Property name to look for
  * @sizep: Returns size of property, or  `FDT_ERR_...` error code if function
index 2f6979c..3f03fc3 100644 (file)
@@ -96,7 +96,7 @@ static int vbe_simple_test_base(struct unit_test_state *uts)
        fixup.tree.np = np;
        ut_assertok(event_notify(EVT_FT_FIXUP, &fixup, sizeof(fixup)));
 
-       node = ofnode_path_root(fixup.tree, "/chosen/fwupd/firmware0");
+       node = oftree_path(fixup.tree, "/chosen/fwupd/firmware0");
 
        version = ofnode_read_string(node, "cur-version");
        ut_assertnonnull(version);
index f6bb046..b73ab98 100644 (file)
@@ -532,15 +532,15 @@ static int dm_test_ofnode_root(struct unit_test_state *uts)
        ut_assert(oftree_valid(tree));
 
        /* Make sure they don't work on this new tree */
-       node = ofnode_path_root(tree, "mmc0");
+       node = oftree_path(tree, "mmc0");
        ut_assert(!ofnode_valid(node));
 
        /* It should appear in the new tree */
-       node = ofnode_path_root(tree, "/new-mmc");
+       node = oftree_path(tree, "/new-mmc");
        ut_assert(ofnode_valid(node));
 
        /* ...and not in the control FDT */
-       node = ofnode_path_root(oftree_default(), "/new-mmc");
+       node = oftree_path(oftree_default(), "/new-mmc");
        ut_assert(!ofnode_valid(node));
 
        free(root);