mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2 1/2] of: base: add helper to rename a node
@ 2019-02-12 15:10 Marco Felsch
  2019-02-12 15:10 ` [PATCH v2 2/2] memory: of_fixup: adapt to new memory layout Marco Felsch
  2019-02-12 20:23 ` [PATCH v2 1/2] of: base: add helper to rename a node Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Marco Felsch @ 2019-02-12 15:10 UTC (permalink / raw)
  To: barebox; +Cc: mfe

Sometimes it can be necessary to rename a node, e.g. a upstream node was
renamed and the whole board logic depends on the old naming scheme.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
Note:
In my v1 this patch was required for the followed, but now it isn't. So
feel free to add or drop it. IMHO it can be a useful helper in the
future.

Changelog:
v2:
 - const name parameter
 - free names before adding new ones
 - drop unnecessary else-path
 - adapt commit message

 drivers/of/base.c | 18 ++++++++++++++++++
 include/of.h      |  5 +++++
 2 files changed, 23 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index b082f0c656..48f1270baa 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -2094,6 +2094,24 @@ void of_delete_node(struct device_node *node)
 		of_set_root_node(NULL);
 }
 
+void of_rename_node(struct device_node *node, const char *name)
+{
+	struct device_node *parent;
+
+	if (!node)
+		return;
+
+	parent = node->parent;
+	if (parent) {
+		/* only child nodes have names */
+		free(node->name);
+		free(node->full_name);
+
+		node->name = xstrdup(name);
+		node->full_name = basprintf("%s/%s", parent->full_name, name);
+	}
+}
+
 int of_device_is_stdout_path(struct device_d *dev)
 {
 	struct device_node *dn;
diff --git a/include/of.h b/include/of.h
index 184acb4741..f829690865 100644
--- a/include/of.h
+++ b/include/of.h
@@ -158,6 +158,7 @@ extern struct device_node *of_create_node(struct device_node *root,
 extern struct device_node *of_copy_node(struct device_node *parent,
 				const struct device_node *other);
 extern void of_delete_node(struct device_node *node);
+extern void of_rename_node(struct device_node *node, const char *name);
 
 extern const char *of_get_machine_compatible(void);
 extern int of_machine_is_compatible(const char *compat);
@@ -605,6 +606,10 @@ static inline void of_delete_node(struct device_node *node)
 {
 }
 
+static inline void of_rename_node(struct device_node *node, const char *name)
+{
+}
+
 static inline int of_machine_is_compatible(const char *compat)
 {
 	return 0;
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] memory: of_fixup: adapt to new memory layout
  2019-02-12 15:10 [PATCH v2 1/2] of: base: add helper to rename a node Marco Felsch
@ 2019-02-12 15:10 ` Marco Felsch
  2019-02-12 20:23 ` [PATCH v2 1/2] of: base: add helper to rename a node Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Marco Felsch @ 2019-02-12 15:10 UTC (permalink / raw)
  To: barebox; +Cc: mfe

Since kernel 4.16 the memory nodes got a @<reg> suffix so the fixup
won't work correctly anymore, because instead of adapting the extisting
one the fixup creates a new node and keeps the old (maybe incorrect)
node.

To be compatible with the old and new layout delete the found memory
node and create a new one. The new node follows the new @<reg> style.

The patch also renames the node parameter to make it clearer.

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
Note:
I've tested the patch on a customer board with one memory bank and on
the imx53 loco-r board with two memory banks. The fixup applies
correctly on both boards. Now I have two memory nodes on the imx53
loco-r board instead of one compared to the upstream dts.

Changelog:
v2:
 - delete all memory nodes, not just the first found
 - add one /memory node per bank, previous one /memory node for all
   banks

 common/memory.c | 75 +++++++++++++++++++++++++++++++++++++------------
 1 file changed, 57 insertions(+), 18 deletions(-)

diff --git a/common/memory.c b/common/memory.c
index 00fa7c50ff..21b2b4f63a 100644
--- a/common/memory.c
+++ b/common/memory.c
@@ -224,39 +224,78 @@ int memory_bank_first_find_space(resource_size_t *retstart,
 
 #ifdef CONFIG_OFTREE
 
-static int of_memory_fixup(struct device_node *node, void *unused)
+static int of_memory_fixup(struct device_node *root, void *unused)
 {
 	struct memory_bank *bank;
 	int err;
-	int addr_cell_len, size_cell_len, len = 0;
-	struct device_node *memnode;
-	u8 tmp[16 * 16]; /* Up to 64-bit address + 64-bit size */
+	int addr_cell_len, size_cell_len;
+	struct device_node *memnode, *tmp, *np;
+	char *memnode_name;
 
-	memnode = of_create_node(node, "/memory");
-	if (!memnode)
-		return -ENOMEM;
+	/*
+	 * Since kernel 4.16 the memory node got a @<reg> suffix. To support
+	 * the old and the new style delete any found memory node and add it
+	 * again to be sure that the memory node exists only once. It shouldn't
+	 * bother older kernels if the memory node has this suffix so adding it
+	 * following the new style.
+	 */
 
-	err = of_property_write_string(memnode, "device_type", "memory");
-	if (err)
-		return err;
+	for_each_child_of_node_safe(root, tmp, np) {
+		const char *device_type;
 
-	addr_cell_len = of_n_addr_cells(memnode);
-	size_cell_len = of_n_size_cells(memnode);
+		err = of_property_read_string(np, "device_type", &device_type);
+		if (err || of_node_cmp("memory", device_type))
+			continue;
+
+		/* delete every found memory node */
+		of_delete_node(np);
+	}
+
+	addr_cell_len = of_n_addr_cells(root);
+	size_cell_len = of_n_size_cells(root);
 
 	for_each_memory_bank(bank) {
-		of_write_number(tmp + len, bank->start, addr_cell_len);
+		u8 tmp[16]; /* Up to 64-bit address + 64-bit size */
+		int len = 0;
+
+		/* Create a /memory node for each bank */
+		memnode_name = basprintf("/memory@%lx", bank->start);
+		if (!memnode_name) {
+			err = -ENOMEM;
+			goto err_out;
+		}
+
+		memnode = of_create_node(root, memnode_name);
+		if (!memnode) {
+			err = -ENOMEM;
+			goto err_free;
+		}
+
+		err = of_property_write_string(memnode, "device_type",
+					       "memory");
+		if (err)
+			goto err_free;
+
+		of_write_number(tmp, bank->start, addr_cell_len);
 		len += addr_cell_len * 4;
 		of_write_number(tmp + len, bank->size, size_cell_len);
 		len += size_cell_len * 4;
-	}
 
-	err = of_set_property(memnode, "reg", tmp, len, 1);
-	if (err) {
-		pr_err("could not set reg %s.\n", strerror(-err));
-		return err;
+		err = of_set_property(memnode, "reg", tmp, len, 1);
+		if (err) {
+			pr_err("could not set reg %s.\n", strerror(-err));
+			goto err_free;
+		}
+
+		free(memnode_name);
 	}
 
 	return 0;
+
+err_free:
+	free(memnode_name);
+err_out:
+	return err;
 }
 
 static int of_register_memory_fixup(void)
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 1/2] of: base: add helper to rename a node
  2019-02-12 15:10 [PATCH v2 1/2] of: base: add helper to rename a node Marco Felsch
  2019-02-12 15:10 ` [PATCH v2 2/2] memory: of_fixup: adapt to new memory layout Marco Felsch
@ 2019-02-12 20:23 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2019-02-12 20:23 UTC (permalink / raw)
  To: Marco Felsch; +Cc: barebox, mfe

On Tue, Feb 12, 2019 at 04:10:40PM +0100, Marco Felsch wrote:
> Sometimes it can be necessary to rename a node, e.g. a upstream node was
> renamed and the whole board logic depends on the old naming scheme.
> 
> Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
> ---
> Note:
> In my v1 this patch was required for the followed, but now it isn't. So
> feel free to add or drop it. IMHO it can be a useful helper in the
> future.

Indeed it can be, but as it's currently unused I only applied 2/2.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-02-12 20:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-12 15:10 [PATCH v2 1/2] of: base: add helper to rename a node Marco Felsch
2019-02-12 15:10 ` [PATCH v2 2/2] memory: of_fixup: adapt to new memory layout Marco Felsch
2019-02-12 20:23 ` [PATCH v2 1/2] of: base: add helper to rename a node Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox