mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] of_property and phandles
@ 2013-08-14  8:11 Sascha Hauer
  2013-08-14  8:11 ` [PATCH 1/7] of: fix merge mode in of_unflatten_dtb Sascha Hauer
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Sascha Hauer @ 2013-08-14  8:11 UTC (permalink / raw)
  To: barebox

This adds the support for phandles to the of_property command.

The of_property command is able to manipulate devicetree properties,
but so far it hasn't been able to specify phandles. With this
series the following is possible:

of_property -s /soc/somenode propname </soc/someothernode 0xdeadbeef>

The /soc/someothernode is inserted into the property as phandle.

The projected usecase currently is to chose a display out of several
possible during runtime.

Sascha

----------------------------------------------------------------
Sascha Hauer (7):
      of: fix merge mode in of_unflatten_dtb
      of: parse phandles during unflatten
      of: default to internal tree in of_find_node_by_path_from
      of: introduce some new helpers
      of_property command: allow to specify a node by alias
      of_property command: allow to set phandles
      oftree command: Allow to specify node by alias

 commands/of_property.c |  36 ++++++++++++--
 commands/oftree.c      |   2 +-
 drivers/of/base.c      | 126 ++++++++++++++++++++++++++++++++++++++++++-------
 drivers/of/fdt.c       |   5 +-
 include/of.h           |  14 ++++++
 5 files changed, 160 insertions(+), 23 deletions(-)

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

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

* [PATCH 1/7] of: fix merge mode in of_unflatten_dtb
  2013-08-14  8:11 [PATCH] of_property and phandles Sascha Hauer
@ 2013-08-14  8:11 ` Sascha Hauer
  2013-08-14  8:11 ` [PATCH 2/7] of: parse phandles during unflatten Sascha Hauer
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Sascha Hauer @ 2013-08-14  8:11 UTC (permalink / raw)
  To: barebox

In merge mode a property may be overwritte with new values. When
this happens the length has to be adjusted.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/of/fdt.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 76d6bb1..8481cf7 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -185,6 +185,7 @@ struct device_node *of_unflatten_dtb(struct device_node *root, void *infdt)
 			if (merge && p) {
 				free(p->value);
 				p->value = xzalloc(len);
+				p->length = len;
 				memcpy(p->value, nodep, len);
 			} else {
 				of_new_property(node, name, nodep, len);
-- 
1.8.4.rc2


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

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

* [PATCH 2/7] of: parse phandles during unflatten
  2013-08-14  8:11 [PATCH] of_property and phandles Sascha Hauer
  2013-08-14  8:11 ` [PATCH 1/7] of: fix merge mode in of_unflatten_dtb Sascha Hauer
@ 2013-08-14  8:11 ` Sascha Hauer
  2013-08-14  8:11 ` [PATCH 3/7] of: default to internal tree in of_find_node_by_path_from Sascha Hauer
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Sascha Hauer @ 2013-08-14  8:11 UTC (permalink / raw)
  To: barebox

With this node->phandle becomes valid after unflattening a tree
and not during of_probe later.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/of/base.c | 22 ++++------------------
 drivers/of/fdt.c  |  4 +++-
 2 files changed, 7 insertions(+), 19 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 33dfd5a..b8cdec5 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -1568,22 +1568,6 @@ int of_add_memory(struct device_node *node, bool dump)
 	return 0;
 }
 
-static void __of_parse_phandles(struct device_node *node)
-{
-	struct device_node *n;
-	phandle phandle;
-	int ret;
-
-	ret = of_property_read_u32(node, "phandle", &phandle);
-	if (!ret) {
-		node->phandle = phandle;
-		list_add_tail(&node->phandles, &phandle_list);
-	}
-
-	list_for_each_entry(n, &node->children, parent_list)
-		__of_parse_phandles(n);
-}
-
 struct device_node *of_chosen;
 const char *of_model;
 
@@ -1602,7 +1586,7 @@ const struct of_device_id of_default_bus_match_table[] = {
 
 int of_probe(void)
 {
-	struct device_node *memory;
+	struct device_node *memory, *node;
 
 	if(!root_node)
 		return -ENODEV;
@@ -1610,7 +1594,9 @@ int of_probe(void)
 	of_chosen = of_find_node_by_path("/chosen");
 	of_property_read_string(root_node, "model", &of_model);
 
-	__of_parse_phandles(root_node);
+	of_tree_for_each_node_from(node, root_node)
+		if (node->phandle)
+			list_add_tail(&node->phandles, &phandle_list);
 
 	memory = of_find_node_by_path("/memory");
 	if (memory)
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 8481cf7..5055eee 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -188,7 +188,9 @@ struct device_node *of_unflatten_dtb(struct device_node *root, void *infdt)
 				p->length = len;
 				memcpy(p->value, nodep, len);
 			} else {
-				of_new_property(node, name, nodep, len);
+				p = of_new_property(node, name, nodep, len);
+				if (!strcmp(name, "phandle") && len == 4)
+					node->phandle = be32_to_cpup(p->value);
 			}
 
 			break;
-- 
1.8.4.rc2


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

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

* [PATCH 3/7] of: default to internal tree in of_find_node_by_path_from
  2013-08-14  8:11 [PATCH] of_property and phandles Sascha Hauer
  2013-08-14  8:11 ` [PATCH 1/7] of: fix merge mode in of_unflatten_dtb Sascha Hauer
  2013-08-14  8:11 ` [PATCH 2/7] of: parse phandles during unflatten Sascha Hauer
@ 2013-08-14  8:11 ` Sascha Hauer
  2013-08-14  8:11 ` [PATCH 4/7] of: introduce some new helpers Sascha Hauer
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Sascha Hauer @ 2013-08-14  8:11 UTC (permalink / raw)
  To: barebox

So that of_find_node_by_path_from can be used easier.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/of/base.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index b8cdec5..3ebd672 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -1211,6 +1211,9 @@ struct device_node *of_find_node_by_path_from(struct device_node *from,
 {
 	char *slash, *p, *freep;
 
+	if (!from)
+		from = root_node;
+
 	if (!from || !path || *path != '/')
 		return NULL;
 
-- 
1.8.4.rc2


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

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

* [PATCH 4/7] of: introduce some new helpers
  2013-08-14  8:11 [PATCH] of_property and phandles Sascha Hauer
                   ` (2 preceding siblings ...)
  2013-08-14  8:11 ` [PATCH 3/7] of: default to internal tree in of_find_node_by_path_from Sascha Hauer
@ 2013-08-14  8:11 ` Sascha Hauer
  2013-08-14  8:11 ` [PATCH 5/7] of_property command: allow to specify a node by alias Sascha Hauer
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Sascha Hauer @ 2013-08-14  8:11 UTC (permalink / raw)
  To: barebox

of_get_tree_max_phandle - get the maximum phandle of a tree. Needed for
                          creating new phandles without conflicts.
of_node_create_phandle - create a phandle for a node which doesn't have one.
of_find_node_by_alias - find a node by alias name
of_find_node_by_path_or_alias - find a node by full path or alias name
of_find_root_node - find the root node for a given device node

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/of/base.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/of.h      |  14 ++++++++
 2 files changed, 115 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 3ebd672..8e9d384 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -241,6 +241,32 @@ const char *of_alias_get(struct device_node *np)
 EXPORT_SYMBOL_GPL(of_alias_get);
 
 /*
+ * of_find_node_by_alias - Find a node given an alias name
+ * @root:    the root node of the tree. If NULL, use internal tree
+ * @alias:   the alias name to find
+ */
+struct device_node *of_find_node_by_alias(struct device_node *root, const char *alias)
+{
+	struct device_node *aliasnp;
+	int ret;
+	const char *path;
+
+	if (!root)
+		root = root_node;
+
+	aliasnp = of_find_node_by_path_from(root, "/aliases");
+	if (!aliasnp)
+		return NULL;
+
+	ret = of_property_read_string(aliasnp, alias, &path);
+	if (ret)
+		return NULL;
+
+	return of_find_node_by_path_from(root, path);
+}
+EXPORT_SYMBOL_GPL(of_find_node_by_alias);
+
+/*
  * of_find_node_by_phandle - Find a node given a phandle
  * @handle:    phandle of the node to find
  */
@@ -256,6 +282,62 @@ struct device_node *of_find_node_by_phandle(phandle phandle)
 EXPORT_SYMBOL(of_find_node_by_phandle);
 
 /*
+ * of_get_tree_max_phandle - Find the maximum phandle of a tree
+ * @root:    root node of the tree to search in. If NULL use the
+ *           internal tree.
+ */
+phandle of_get_tree_max_phandle(struct device_node *root)
+{
+	struct device_node *n;
+	phandle max;
+
+	if (!root)
+		root = root_node;
+
+	if (!root)
+		return 0;
+
+	max = root->phandle;
+
+	of_tree_for_each_node_from(n, root) {
+		if (n->phandle > max)
+			max = n->phandle;
+	}
+
+	return max;
+}
+EXPORT_SYMBOL(of_get_tree_max_phandle);
+
+/*
+ * of_node_create_phandle - create a phandle for a node
+ * @node:    The node to create a phandle in
+ *
+ * returns the new phandle or the existing phandle if the node
+ * already has a phandle.
+ */
+phandle of_node_create_phandle(struct device_node *node)
+{
+	phandle p;
+	struct device_node *root;
+
+	if (node->phandle)
+		return node->phandle;
+
+	root = of_find_root_node(node);
+
+	p = of_get_tree_max_phandle(root) + 1;
+
+	node->phandle = p;
+
+	p = cpu_to_be32(p);
+
+	of_set_property(node, "phandle", &p, sizeof(p), 1);
+
+	return node->phandle;
+}
+EXPORT_SYMBOL(of_node_create_phandle);
+
+/*
  * Find a property with a given name for a given node
  * and return the value.
  */
@@ -1258,6 +1340,25 @@ struct device_node *of_find_node_by_path(const char *path)
 EXPORT_SYMBOL(of_find_node_by_path);
 
 /**
+ *	of_find_node_by_path_or_alias - Find a node matching a full OF path
+ *	or an alias
+ *	@root:	The root node. If NULL the internal tree is used
+ *	@str:	the full path or alias
+ *
+ *	Returns a pointer to the node found or NULL.
+ */
+struct device_node *of_find_node_by_path_or_alias(struct device_node *root,
+		const char *str)
+{
+	if (*str ==  '/')
+		return of_find_node_by_path_from(root, str);
+	else
+		return of_find_node_by_alias(root, str);
+
+}
+EXPORT_SYMBOL(of_find_node_by_path_or_alias);
+
+/**
  * of_modalias_node - Lookup appropriate modalias for a device node
  * @node:	pointer to a device tree node
  * @modalias:	Pointer to buffer that modalias value will be copied into
diff --git a/include/of.h b/include/of.h
index b99f0b2..2c77ee6 100644
--- a/include/of.h
+++ b/include/of.h
@@ -689,4 +689,18 @@ int of_device_enable_path(const char *path);
 int of_device_disable(struct device_node *node);
 int of_device_disable_path(const char *path);
 
+phandle of_get_tree_max_phandle(struct device_node *root);
+phandle of_node_create_phandle(struct device_node *node);
+struct device_node *of_find_node_by_alias(struct device_node *root,
+		const char *alias);
+struct device_node *of_find_node_by_path_or_alias(struct device_node *root,
+		const char *str);
+
+static inline struct device_node *of_find_root_node(struct device_node *node)
+{
+	while (node->parent)
+		node = node->parent;
+
+	return node;
+}
 #endif /* __OF_H */
-- 
1.8.4.rc2


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

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

* [PATCH 5/7] of_property command: allow to specify a node by alias
  2013-08-14  8:11 [PATCH] of_property and phandles Sascha Hauer
                   ` (3 preceding siblings ...)
  2013-08-14  8:11 ` [PATCH 4/7] of: introduce some new helpers Sascha Hauer
@ 2013-08-14  8:11 ` Sascha Hauer
  2013-08-14  8:11 ` [PATCH 6/7] of_property command: allow to set phandles Sascha Hauer
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Sascha Hauer @ 2013-08-14  8:11 UTC (permalink / raw)
  To: barebox

With this not only a full path can be used to specify a node, but also
an alias.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/of_property.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/commands/of_property.c b/commands/of_property.c
index 8ffe30b..a544f71 100644
--- a/commands/of_property.c
+++ b/commands/of_property.c
@@ -196,7 +196,7 @@ static int do_of_property(int argc, char *argv[])
 
 	if (optind < argc) {
 		path = argv[optind];
-		node = of_find_node_by_path(path);
+		node = of_find_node_by_path_or_alias(NULL, path);
 		if (!node) {
 			printf("Cannot find nodepath %s\n", path);
 			return -ENOENT;
-- 
1.8.4.rc2


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

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

* [PATCH 6/7] of_property command: allow to set phandles
  2013-08-14  8:11 [PATCH] of_property and phandles Sascha Hauer
                   ` (4 preceding siblings ...)
  2013-08-14  8:11 ` [PATCH 5/7] of_property command: allow to specify a node by alias Sascha Hauer
@ 2013-08-14  8:11 ` Sascha Hauer
  2013-08-14  8:11 ` [PATCH 7/7] oftree command: Allow to specify node by alias Sascha Hauer
  2013-08-14  9:06 ` [PATCH] of_property and phandles Jan Lübbe
  7 siblings, 0 replies; 9+ messages in thread
From: Sascha Hauer @ 2013-08-14  8:11 UTC (permalink / raw)
  To: barebox

With this properties with phandles can be created. A phandle
is created when cells are parsed (in '<' '>') and a cell does
not begin with a digit.
The phandles can be specified either by alias or by full path.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/of_property.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

diff --git a/commands/of_property.c b/commands/of_property.c
index a544f71..4518c72 100644
--- a/commands/of_property.c
+++ b/commands/of_property.c
@@ -55,7 +55,36 @@ static int of_parse_prop_cells(char * const *newval, int count, char *data, int
 		}
 
 		cp = newp;
-		tmp = simple_strtoul(cp, &newp, 0);
+
+		if (isdigit(*cp)) {
+			tmp = simple_strtoul(cp, &newp, 0);
+		} else {
+			struct device_node *n;
+			char *str;
+			int len = 0;
+
+			str = cp;
+			while (*str && *str != '>' && *str != ' ') {
+				str++;
+				len++;
+			}
+
+			str = xzalloc(len + 1);
+			strncpy(str, cp, len);
+
+			n = of_find_node_by_path_or_alias(NULL, str);
+			if (!n)
+				printf("Cannot find node '%s'\n", str);
+
+			free(str);
+
+			if (!n)
+				return -EINVAL;
+
+			tmp = of_node_create_phandle(n);
+			newp += len;
+		}
+
 		*(__be32 *)data = __cpu_to_be32(tmp);
 		data  += 4;
 		*len += 4;
@@ -276,7 +305,8 @@ BAREBOX_CMD_HELP_USAGE("of_property [OPTIONS] [NODE] [PROPERTY] [VALUES]\n")
 BAREBOX_CMD_HELP_OPT  ("-s",  "set property to value\n")
 BAREBOX_CMD_HELP_OPT  ("-d",  "delete property\n")
 BAREBOX_CMD_HELP_TEXT ("\nvalid formats for values:\n")
-BAREBOX_CMD_HELP_TEXT ("<0x00112233 4 05> - an array of cells\n")
+BAREBOX_CMD_HELP_TEXT ("<0x00112233 4 05> - an array of cells. cells not beginning with a digit are\n")
+BAREBOX_CMD_HELP_TEXT ("                    interpreted as node pathes and converted to phandles\n")
 BAREBOX_CMD_HELP_TEXT ("[00 11 22 .. nn]  - byte stream\n")
 BAREBOX_CMD_HELP_TEXT ("If the value does not start with '<' or '[' it is interpreted as strings\n")
 BAREBOX_CMD_HELP_END
-- 
1.8.4.rc2


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

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

* [PATCH 7/7] oftree command: Allow to specify node by alias
  2013-08-14  8:11 [PATCH] of_property and phandles Sascha Hauer
                   ` (5 preceding siblings ...)
  2013-08-14  8:11 ` [PATCH 6/7] of_property command: allow to set phandles Sascha Hauer
@ 2013-08-14  8:11 ` Sascha Hauer
  2013-08-14  9:06 ` [PATCH] of_property and phandles Jan Lübbe
  7 siblings, 0 replies; 9+ messages in thread
From: Sascha Hauer @ 2013-08-14  8:11 UTC (permalink / raw)
  To: barebox

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/oftree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/commands/oftree.c b/commands/oftree.c
index 00e54dc..475f019 100644
--- a/commands/oftree.c
+++ b/commands/oftree.c
@@ -164,7 +164,7 @@ static int do_oftree(int argc, char *argv[])
 			of_print_nodes(root, 0);
 			of_delete_node(root);
 		} else {
-			struct device_node *n = of_find_node_by_path(node);
+			struct device_node *n = of_find_node_by_path_or_alias(NULL, node);
 			if (!n) {
 				ret = -ENOENT;
 				goto out;
-- 
1.8.4.rc2


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

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

* Re: [PATCH] of_property and phandles
  2013-08-14  8:11 [PATCH] of_property and phandles Sascha Hauer
                   ` (6 preceding siblings ...)
  2013-08-14  8:11 ` [PATCH 7/7] oftree command: Allow to specify node by alias Sascha Hauer
@ 2013-08-14  9:06 ` Jan Lübbe
  7 siblings, 0 replies; 9+ messages in thread
From: Jan Lübbe @ 2013-08-14  9:06 UTC (permalink / raw)
  To: barebox

On Wed, 2013-08-14 at 10:11 +0200, Sascha Hauer wrote:
> This adds the support for phandles to the of_property command.
> 
> The of_property command is able to manipulate devicetree properties,
> but so far it hasn't been able to specify phandles. With this
> series the following is possible:
> 
> of_property -s /soc/somenode propname </soc/someothernode 0xdeadbeef>
> 
> The /soc/someothernode is inserted into the property as phandle.
> 
> The projected usecase currently is to chose a display out of several
> possible during runtime.

Thanks, the series looks good.

Regards,
Jan
-- 
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] 9+ messages in thread

end of thread, other threads:[~2013-08-14  9:05 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-14  8:11 [PATCH] of_property and phandles Sascha Hauer
2013-08-14  8:11 ` [PATCH 1/7] of: fix merge mode in of_unflatten_dtb Sascha Hauer
2013-08-14  8:11 ` [PATCH 2/7] of: parse phandles during unflatten Sascha Hauer
2013-08-14  8:11 ` [PATCH 3/7] of: default to internal tree in of_find_node_by_path_from Sascha Hauer
2013-08-14  8:11 ` [PATCH 4/7] of: introduce some new helpers Sascha Hauer
2013-08-14  8:11 ` [PATCH 5/7] of_property command: allow to specify a node by alias Sascha Hauer
2013-08-14  8:11 ` [PATCH 6/7] of_property command: allow to set phandles Sascha Hauer
2013-08-14  8:11 ` [PATCH 7/7] oftree command: Allow to specify node by alias Sascha Hauer
2013-08-14  9:06 ` [PATCH] of_property and phandles Jan Lübbe

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