Skip to contentSkip to conversation

Convert fdtdec_setup_memory_banksize() to use livetree

PATCH New Jens Wiklander 2 participants 9 messages Aug 20, 2018 – Oct 2, 2018 Library code

lore
Share & tools
lore
#0 lore raw

v2 cover Convert fdtdec_setup_memory_banksize() to use livetree

Hi,

As requested while reviewing
452bc121027d ("fdt: fix fdtdec_setup_memory_banksize()")
heres a patch set converting fdtdec_setup_memory_banksize() to use livetree.

v2:
* Added test code for the new function ofnode_by_prop_value()
* The patch "fdt: fdtdec_setup_memory_banksize() use livetree":

Thanks,
Jens

Jens Wiklander (3):
ofnode: add ofnode_by_prop_value()
fdt: fdtdec_setup_memory_banksize() use livetree
test: ofnode: test ofnode_by_prop_value()

 drivers/core/of_access.c | 27 +++++++++++++++++++++++
 drivers/core/ofnode.c    | 14 ++++++++++++
 include/dm/of_access.h   | 16 ++++++++++++++
 include/dm/ofnode.h      | 14 ++++++++++++
 lib/fdtdec.c             | 46 ++++++++++++++++++----------------------
 test/dm/ofnode.c         | 27 +++++++++++++++++++++++
 6 files changed, 119 insertions(+), 25 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/3 ofnode: add ofnode_by_prop_value()

Adds ofnode_by_prop_value() to search for nodes with a given property and value, an ofnode version of fdt_node_offset_by_prop_value().

---
 drivers/core/of_access.c | 27 +++++++++++++++++++++++++++
 drivers/core/ofnode.c    | 14 ++++++++++++++
 include/dm/of_access.h   | 16 ++++++++++++++++
 include/dm/ofnode.h      | 14 ++++++++++++++
 4 files changed, 71 insertions(+)
drivers/core/of_access.c +27 -0
index 0729dfcdb3b8..14c020a687b7 100644
@@ -376,6 +376,33 @@ struct device_node *of_find_compatible_node(struct device_node *from,
 	return np;
 }
 
+static int of_device_has_prop_value(const struct device_node *device,
+				    const char *propname, const void *propval,
+				    int proplen)
+{
+	struct property *prop = of_find_property(device, propname, NULL);
+
+	if (!prop || !prop->value || prop->length != proplen)
+		return 0;
+	return !memcmp(prop->value, propval, proplen);
+}
+
+struct device_node *of_find_node_by_prop_value(struct device_node *from,
+					       const char *propname,
+					       const void *propval, int proplen)
+{
+	struct device_node *np;
+
+	for_each_of_allnodes_from(from, np) {
+		if (of_device_has_prop_value(np, propname, propval, proplen) &&
+		    of_node_get(np))
+			break;
+	}
+	of_node_put(from);
+
+	return np;
+}
+
 struct device_node *of_find_node_by_phandle(phandle handle)
 {
 	struct device_node *np;
drivers/core/ofnode.c +14 -0
index 0cfb0fbabb00..a7e192772324 100644
@@ -777,3 +777,17 @@ ofnode ofnode_by_compatible(ofnode from, const char *compat)
 				gd->fdt_blob, ofnode_to_offset(from), compat));
 	}
 }
+
+ofnode ofnode_by_prop_value(ofnode from, const char *propname,
+			    const void *propval, int proplen)
+{
+	if (of_live_active()) {
+		return np_to_ofnode(of_find_node_by_prop_value(
+			(struct device_node *)ofnode_to_np(from), propname,
+			propval, proplen));
+	} else {
+		return offset_to_ofnode(fdt_node_offset_by_prop_value(
+				gd->fdt_blob, ofnode_to_offset(from),
+				propname, propval, proplen));
+	}
+}
include/dm/of_access.h +16 -0
index dd1abb8e97b4..5ed1a0cdb427 100644
@@ -193,6 +193,22 @@ static inline struct device_node *of_find_node_by_path(const char *path)
 struct device_node *of_find_compatible_node(struct device_node *from,
 				const char *type, const char *compatible);
 
+/**
+ * of_find_node_by_prop_value() - find a node with a given property value
+ *
+ * Find a node based on a property value.
+ * @from: Node to start searching from or NULL. the node you pass will not be
+ *	searched, only the next one will; typically, you pass what the previous
+ *	call returned.
+ * @propname: property name to check
+ * @propval: property value to search for
+ * @proplen: length of the value in propval
+ * @return node pointer or NULL if not found
+ */
+struct device_node *of_find_node_by_prop_value(struct device_node *from,
+					       const char *propname,
+					       const void *propval,
+					       int proplen);
 /**
  * of_find_node_by_phandle() - Find a node given a phandle
  *
include/dm/ofnode.h +14 -0
index ab36b74c4ca4..c06d77849c73 100644
@@ -702,6 +702,20 @@ int ofnode_read_resource_byname(ofnode node, const char *name,
  */
 ofnode ofnode_by_compatible(ofnode from, const char *compat);
 
+/**
+ * ofnode_by_prop_value() - Find the next node with given property value
+ *
+ * Find the next node after @from that has a @propname with a value
+ * @propval and a length @proplen.
+ *
+ * @from: ofnode to start from (use ofnode_null() to start at the
+ * beginning) @propname: property name to check @propval: property value to
+ * search for @proplen: length of the value in propval @return ofnode
+ * found, or ofnode_null() if none
+ */
+ofnode ofnode_by_prop_value(ofnode from, const char *propname,
+			    const void *propval, int proplen);
+
 /**
  * ofnode_for_each_subnode() - iterate over all subnodes of a parent
  *
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 1/3 ofnode: add ofnode_by_prop_value()

Quoted 10 lines · Jens Wiklander

Adds ofnode_by_prop_value() to search for nodes with a given property and value, an ofnode version of fdt_node_offset_by_prop_value().

---
 drivers/core/of_access.c | 27 +++++++++++++++++++++++++++
 drivers/core/ofnode.c    | 14 ++++++++++++++
 include/dm/of_access.h   | 16 ++++++++++++++++
 include/dm/ofnode.h      | 14 ++++++++++++++
 4 files changed, 71 insertions(+)
#3 lore raw

PATCH v2 1/3 ofnode: add ofnode_by_prop_value()

Quoted 12 lines · Simon Glass
Quoted 10 lines · Jens Wiklander

Adds ofnode_by_prop_value() to search for nodes with a given property and value, an ofnode version of fdt_node_offset_by_prop_value().

---
 drivers/core/of_access.c | 27 +++++++++++++++++++++++++++
 drivers/core/ofnode.c    | 14 ++++++++++++++
 include/dm/of_access.h   | 16 ++++++++++++++++
 include/dm/ofnode.h      | 14 ++++++++++++++
 4 files changed, 71 insertions(+)
Quoted 1 line

Applied to u-boot-dm, and now in mainline, thanks!

#4 lore raw

PATCH v2 2/3 fdt: fdtdec_setup_memory_banksize() use livetree

In reply to Jens Wiklander

Converts fdtdec_setup_memory_banksize() to use ofnode functions instead.

---
 lib/fdtdec.c | 46 +++++++++++++++++++++-------------------------
 1 file changed, 21 insertions(+), 25 deletions(-)
lib/fdtdec.c +21 -25
index a208589c48ae..fef2f88f9d10 100644
@@ -16,6 +16,7 @@
 #include <serial.h>
 #include <asm/sections.h>
 #include <linux/ctype.h>
+#include <linux/ioport.h>
 #include <linux/lzo.h>
 
 DECLARE_GLOBAL_DATA_PTR;
@@ -1182,43 +1183,34 @@ int fdtdec_setup_mem_size_base(void)
 
 #if defined(CONFIG_NR_DRAM_BANKS)
 
-static int get_next_memory_node(const void *blob, int startoffset)
+static ofnode get_next_memory_node(ofnode mem)
 {
-	int mem = -1;
-
 	do {
-		mem = fdt_node_offset_by_prop_value(gd->fdt_blob, mem,
-						    "device_type", "memory", 7);
-	} while (!fdtdec_get_is_enabled(blob, mem));
+		mem = ofnode_by_prop_value(mem, "device_type", "memory", 7);
+	} while (ofnode_valid(mem) && !ofnode_is_available(mem));
 
 	return mem;
 }
 
 int fdtdec_setup_memory_banksize(void)
 {
-	int bank, ret, mem, reg = 0;
-	struct fdt_resource res;
+	int bank, reg = 0;
+	struct resource res;
+	ofnode mem;
 
-	mem = get_next_memory_node(gd->fdt_blob, -1);
-	if (mem < 0) {
-		debug("%s: Missing /memory node\n", __func__);
-		return -EINVAL;
-	}
+	mem = get_next_memory_node(ofnode_null());
+	if (!ofnode_valid(mem))
+		goto missing_node;
 
 	for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
-		ret = fdt_get_resource(gd->fdt_blob, mem, "reg", reg++, &res);
-		if (ret == -FDT_ERR_NOTFOUND) {
+		while (ofnode_read_resource(mem, reg++, &res)) {
 			reg = 0;
-			mem = get_next_memory_node(gd->fdt_blob, mem);
-			if (mem == -FDT_ERR_NOTFOUND)
-				break;
-
-			ret = fdt_get_resource(gd->fdt_blob, mem, "reg", reg++, &res);
-			if (ret == -FDT_ERR_NOTFOUND)
-				break;
-		}
-		if (ret != 0) {
-			return -EINVAL;
+			mem = get_next_memory_node(mem);
+			if (!ofnode_valid(mem)) {
+				if (bank)
+					return 0;
+				goto missing_node;
+			}
 		}
 
 		gd->bd->bi_dram[bank].start = (phys_addr_t)res.start;
@@ -1232,6 +1224,10 @@ int fdtdec_setup_memory_banksize(void)
 	}
 
 	return 0;
+
+missing_node:
+	debug("%s: Missing /memory node\n", __func__);
+	return -EINVAL;
 }
 #endif
 
Signature · 1 line
2.17.1
#5 lore raw

PATCH v2 2/3 fdt: fdtdec_setup_memory_banksize() use livetree

Quoted 7 lines · Jens Wiklander

Converts fdtdec_setup_memory_banksize() to use ofnode functions instead.

---
 lib/fdtdec.c | 46 +++++++++++++++++++++-------------------------
 1 file changed, 21 insertions(+), 25 deletions(-)

Applied to u-boot-dm, and now in mainline, thanks!

#6 lore raw

PATCH v2 3/3 test: ofnode: test ofnode_by_prop_value()

In reply to Jens Wiklander

Test ofnode_by_prop_value()

---
 test/dm/ofnode.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
test/dm/ofnode.c +27 -0
index 8db1f9857f7d..907d1ddbdb6f 100644
@@ -15,3 +15,30 @@ static int dm_test_ofnode_compatible(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_ofnode_compatible, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+static int dm_test_ofnode_by_prop_value(struct unit_test_state *uts)
+{
+	const char propname[] = "compatible";
+	const char propval[] = "denx,u-boot-fdt-test";
+	const char *str;
+	ofnode node = ofnode_null();
+
+	/* Find first matching node, there should be at least one */
+	node = ofnode_by_prop_value(node, propname, propval, sizeof(propval));
+	ut_assert(ofnode_valid(node));
+	str = ofnode_read_string(node, propname);
+	ut_assert(str && !strcmp(str, propval));
+
+	/* Find the rest of the matching nodes */
+	while (true) {
+		node = ofnode_by_prop_value(node, propname, propval,
+					    sizeof(propval));
+		if (!ofnode_valid(node))
+			break;
+		str = ofnode_read_string(node, propname);
+		ut_assert(str && !strcmp(str, propval));
+	}
+
+	return 0;
+}
+DM_TEST(dm_test_ofnode_by_prop_value, DM_TESTF_SCAN_FDT);
Signature · 1 line
2.17.1