[PATCH 52/67] dm: core: Rewrite ofnode_find_subnode() to use ofnode API

Simon Glass sjg at chromium.org
Wed Jan 1 23:09:38 CET 2025


This function currently processes things two different ways, so the
treatment of unit addresses differs depending on whether OF_LIVE is
enabled or not.

Rewrite it to use the ofnode API and add a test to check that unit
addresses can be matched correctly.

Unfortunately this increases code size by about 104 bytes on Thumb2,
mostly due to the extra logic in ofnode_name_eq(). We could fix that
by having two separate ofnode_name_eq() functions, only one of which
allows an @ sign in its parameter.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 drivers/core/ofnode.c | 24 +++++++++---------------
 include/dm/ofnode.h   |  4 +++-
 test/dm/ofnode.c      | 19 +++++++++++++++++++
 3 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index e6f390c710b..62e7a041fdf 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -580,28 +580,22 @@ int ofnode_read_size(ofnode node, const char *propname)
 
 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
 {
-	ofnode subnode;
+	ofnode subnode, found = ofnode_null();
 
 	assert(ofnode_valid(node));
 	log_debug("%s: %s: ", __func__, subnode_name);
 
-	if (ofnode_is_np(node)) {
-		struct device_node *np = ofnode_to_np(node);
-
-		for (np = np->child; np; np = np->sibling) {
-			if (!strcmp(subnode_name, np->name))
-				break;
+	ofnode_for_each_subnode(subnode, node) {
+		if (ofnode_name_eq(subnode, subnode_name)) {
+			found = subnode;
+			break;
 		}
-		subnode = np_to_ofnode(np);
-	} else {
-		int ooffset = fdt_subnode_offset(ofnode_to_fdt(node),
-				ofnode_to_offset(node), subnode_name);
-		subnode = noffset_to_ofnode(node, ooffset);
 	}
-	log_debug("%s\n", ofnode_valid(subnode) ?
-		  ofnode_get_name(subnode) : "<none>");
 
-	return subnode;
+	log_debug("%s\n", ofnode_valid(found) ?
+		  ofnode_get_name(found) : "<none>");
+
+	return found;
 }
 
 int ofnode_read_u32_array(ofnode node, const char *propname,
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index 9d73a321cbc..5e043616803 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -589,7 +589,9 @@ bool ofnode_read_bool(ofnode node, const char *propname);
  * ofnode_find_subnode() - find a named subnode of a parent node
  *
  * @node:	valid reference to parent node
- * @subnode_name: name of subnode to find
+ * @subnode_name: name of subnode to find, including any unit address. If the
+ *	unit address is omitted, any subnode which matches the name (excluding
+ *	any unit address) is returned
  * Return: reference to subnode (which can be invalid if there is no such
  * subnode)
  */
diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c
index ce996567c3c..47c890e0f92 100644
--- a/test/dm/ofnode.c
+++ b/test/dm/ofnode.c
@@ -1201,6 +1201,25 @@ static int dm_test_ofnode_find_subnode(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_ofnode_find_subnode, UTF_SCAN_FDT);
 
+/* check ofnode_find_subnode() with unit addresses */
+static int dm_test_ofnode_find_subnode_unit(struct unit_test_state *uts)
+{
+	ofnode node, subnode;
+
+	node = ofnode_path("/some-bus");
+	ut_assert(ofnode_valid(node));
+	subnode = ofnode_find_subnode(node, "c-test at 5");
+	ut_assert(ofnode_valid(subnode));
+	ut_asserteq_str("c-test at 5", ofnode_get_name(subnode));
+
+	subnode = ofnode_find_subnode(node, "c-test");
+	ut_assert(ofnode_valid(subnode));
+	ut_asserteq_str("c-test at 5", ofnode_get_name(subnode));
+
+	return 0;
+}
+DM_TEST(dm_test_ofnode_find_subnode_unit, UTF_SCAN_FDT);
+
 /* test ofnode_find_subnode() on the 'other' tree */
 static int dm_test_ofnode_find_subnode_ot(struct unit_test_state *uts)
 {
-- 
2.43.0



More information about the U-Boot mailing list