From a5352dfb66f5a5183d8bab1e7e6c5e2dfde9e5d0 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Tue, 13 Aug 2013 16:47:40 +0200 Subject: [PATCH 1/8] of: fix merge mode in of_unflatten_dtb 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 --- drivers/of/fdt.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index 76d6bb159..8481cf77c 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); From 7d505a9626e81377ac1312465b8114fbd7603626 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Tue, 13 Aug 2013 15:13:15 +0200 Subject: [PATCH 2/8] of: parse phandles during unflatten With this node->phandle becomes valid after unflattening a tree and not during of_probe later. Signed-off-by: Sascha Hauer --- 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 33dfd5a51..b8cdec57b 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 8481cf77c..5055eee43 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; From 55909550f8eed7d67117327ca7f6e5692988938d Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Tue, 13 Aug 2013 16:46:22 +0200 Subject: [PATCH 3/8] of: default to internal tree in of_find_node_by_path_from So that of_find_node_by_path_from can be used easier. Signed-off-by: Sascha Hauer --- drivers/of/base.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/of/base.c b/drivers/of/base.c index b8cdec57b..3ebd672d8 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; From 6c9e799a00cbdb50fa9129b57a59766010c63ffa Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Tue, 13 Aug 2013 16:48:51 +0200 Subject: [PATCH 4/8] of: introduce some new helpers 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 --- 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 3ebd672d8..8e9d38467 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -240,6 +240,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 @@ -255,6 +281,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. @@ -1257,6 +1339,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 diff --git a/include/of.h b/include/of.h index b99f0b2da..2c77ee6bf 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 */ From 7a15e03fca742e35f1a1bc3103015b8965860b94 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Tue, 13 Aug 2013 16:52:14 +0200 Subject: [PATCH 5/8] of_property command: allow to specify a node by alias With this not only a full path can be used to specify a node, but also an alias. Signed-off-by: Sascha Hauer --- 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 8ffe30b25..a544f71ce 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; From 6d06c4e318382a5c0bc1e2243b77f58d5ecd88ea Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Tue, 13 Aug 2013 16:53:15 +0200 Subject: [PATCH 6/8] of_property command: allow to set phandles 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 --- 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 a544f71ce..4518c7252 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 From 7f5c04321d8ee565f374cd630931a0fd1a315ace Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Tue, 13 Aug 2013 16:55:38 +0200 Subject: [PATCH 7/8] oftree command: Allow to specify node by alias Signed-off-by: Sascha Hauer --- commands/oftree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/oftree.c b/commands/oftree.c index 00e54dcd7..475f0199b 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; From da19582070da7918c901c26d001c6d0114291461 Mon Sep 17 00:00:00 2001 From: Renaud Barbier Date: Tue, 3 Sep 2013 15:54:24 +0100 Subject: [PATCH 8/8] of: base: import of_find_node_by_type Import of_find_node_by_type from Linux drivers/of/base.c - commit id d8dfad3. This function retrieves a node pointer based on the "device_type" property of the node. This is used by device tree update functions in PPC support. Signed-off-by: Renaud Barbier Signed-off-by: Sascha Hauer --- drivers/of/base.c | 29 +++++++++++++++++++++++++++++ include/of.h | 2 ++ 2 files changed, 31 insertions(+) diff --git a/drivers/of/base.c b/drivers/of/base.c index 8e9d38467..800b5a322 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -400,6 +400,35 @@ struct device_node *of_find_node_by_name(struct device_node *from, } EXPORT_SYMBOL(of_find_node_by_name); +/** + * of_find_node_by_type - Find a node by its "device_type" property + * @from: The node to start searching from, or NULL to start searching + * the entire device tree. The node you pass will not be + * searched, only the next one will; typically, you pass + * what the previous call returned. + * @type: The type string to match against. + * + * Returns a pointer to the node found or NULL. + */ +struct device_node *of_find_node_by_type(struct device_node *from, + const char *type) +{ + struct device_node *np; + const char *device_type; + int ret; + + if (!from) + from = root_node; + + of_tree_for_each_node_from(np, from) { + ret = of_property_read_string(np, "device_type", &device_type); + if (!ret && !of_node_cmp(device_type, type)) + return np; + } + return NULL; +} +EXPORT_SYMBOL(of_find_node_by_type); + /** * of_find_compatible_node - Find a node based on type and one of the * tokens in its "compatible" property diff --git a/include/of.h b/include/of.h index 2c77ee6bf..e5cd7506c 100644 --- a/include/of.h +++ b/include/of.h @@ -124,6 +124,8 @@ extern struct device_node *of_find_node_by_path_from(struct device_node *from, const char *path); extern struct device_node *of_find_node_by_path(const char *path); extern struct device_node *of_find_node_by_phandle(phandle phandle); +extern struct device_node *of_find_node_by_type(struct device_node *from, + const char *type); extern struct device_node *of_find_compatible_node(struct device_node *from, const char *type, const char *compat); extern const struct of_device_id *of_match_node(