From 331048471dee5c1d9cede54382256e6cfaee2370 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 30 Jul 2022 15:52:08 -0600 Subject: dm: core: Introduce support for multiple trees At present ofnode only works with a single device tree, for the most part. This is the control FDT used by U-Boot. When booting an OS we may obtain a different device tree and want to modify it. Add some initial support for this into the ofnode API. Note that we don't permit aliases in this other device tree, since the of_access implementation maintains a list of aliases collected at start-up. Also, we don't need aliases to do fixups in the other FDT. So make sure that flat tree and live tree processing are consistent in this area. Signed-off-by: Simon Glass --- drivers/core/ofnode.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'drivers/core/ofnode.c') diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c index a59832ebbfb..bd41ef503c2 100644 --- a/drivers/core/ofnode.c +++ b/drivers/core/ofnode.c @@ -552,6 +552,17 @@ ofnode ofnode_path(const char *path) return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path)); } +ofnode ofnode_path_root(oftree tree, const char *path) +{ + if (of_live_active()) + return np_to_ofnode(of_find_node_opts_by_path(tree.np, path, + NULL)); + else if (*path != '/' && tree.fdt != gd->fdt_blob) + return ofnode_null(); /* Aliases only on control FDT */ + else + return offset_to_ofnode(fdt_path_offset(tree.fdt, path)); +} + const void *ofnode_read_chosen_prop(const char *propname, int *sizep) { ofnode chosen_node; -- cgit v1.2.3 From be0789a8ee024e685f070dbd8c58736ea3891654 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 30 Jul 2022 15:52:10 -0600 Subject: dm: core: Swap parameters of ofnode_write_prop() It is normal for the length to come after the value in libfdt. Follow this same convention with ofnode. Signed-off-by: Simon Glass --- drivers/core/ofnode.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'drivers/core/ofnode.c') diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c index bd41ef503c2..1c9542a3567 100644 --- a/drivers/core/ofnode.c +++ b/drivers/core/ofnode.c @@ -1105,8 +1105,8 @@ ofnode ofnode_by_prop_value(ofnode from, const char *propname, } } -int ofnode_write_prop(ofnode node, const char *propname, int len, - const void *value) +int ofnode_write_prop(ofnode node, const char *propname, const void *value, + int len) { const struct device_node *np = ofnode_to_np(node); struct property *pp; @@ -1161,7 +1161,7 @@ int ofnode_write_string(ofnode node, const char *propname, const char *value) debug("%s: %s = %s", __func__, propname, value); - return ofnode_write_prop(node, propname, strlen(value) + 1, value); + return ofnode_write_prop(node, propname, value, strlen(value) + 1); } int ofnode_set_enabled(ofnode node, bool value) -- cgit v1.2.3 From 39e42be12b9456e604ac3e228973b1cb1136864c Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 30 Jul 2022 15:52:13 -0600 Subject: dm: core: Allow writing to a flat tree with ofnode In generally it is not permitted to implement an ofnode function only for flat tree or live tree. Both must be supported. Also the code for live tree access should be in of_access.c rather than ofnode.c which is really just for holding the API-conversion code. Update ofnode_write_prop() accordingly and fix the test so it can work with flat tree too. Signed-off-by: Simon Glass --- drivers/core/ofnode.c | 51 +++++---------------------------------------------- 1 file changed, 5 insertions(+), 46 deletions(-) (limited to 'drivers/core/ofnode.c') diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c index 1c9542a3567..b7a55589a1c 100644 --- a/drivers/core/ofnode.c +++ b/drivers/core/ofnode.c @@ -1108,55 +1108,17 @@ ofnode ofnode_by_prop_value(ofnode from, const char *propname, int ofnode_write_prop(ofnode node, const char *propname, const void *value, int len) { - const struct device_node *np = ofnode_to_np(node); - struct property *pp; - struct property *pp_last = NULL; - struct property *new; - - if (!of_live_active()) - return -ENOSYS; - - if (!np) - return -EINVAL; - - for (pp = np->properties; pp; pp = pp->next) { - if (strcmp(pp->name, propname) == 0) { - /* Property exists -> change value */ - pp->value = (void *)value; - pp->length = len; - return 0; - } - pp_last = pp; - } - - if (!pp_last) - return -ENOENT; - - /* Property does not exist -> append new property */ - new = malloc(sizeof(struct property)); - if (!new) - return -ENOMEM; - - new->name = strdup(propname); - if (!new->name) { - free(new); - return -ENOMEM; - } - - new->value = (void *)value; - new->length = len; - new->next = NULL; - - pp_last->next = new; + if (of_live_active()) + return of_write_prop(ofnode_to_npw(node), propname, len, value); + else + return fdt_setprop((void *)gd->fdt_blob, ofnode_to_offset(node), + propname, value, len); return 0; } int ofnode_write_string(ofnode node, const char *propname, const char *value) { - if (!of_live_active()) - return -ENOSYS; - assert(ofnode_valid(node)); debug("%s: %s = %s", __func__, propname, value); @@ -1166,9 +1128,6 @@ int ofnode_write_string(ofnode node, const char *propname, const char *value) int ofnode_set_enabled(ofnode node, bool value) { - if (!of_live_active()) - return -ENOSYS; - assert(ofnode_valid(node)); if (value) -- cgit v1.2.3 From 55f7990bfeb92c172065d5b53c59d5306cc554ca Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 30 Jul 2022 15:52:14 -0600 Subject: dm: core: Add support for writing u32 with ofnode Add a new function to write an integer to an ofnode (live tree or flat tree). Signed-off-by: Simon Glass --- drivers/core/ofnode.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'drivers/core/ofnode.c') diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c index b7a55589a1c..45ea84e9fb8 100644 --- a/drivers/core/ofnode.c +++ b/drivers/core/ofnode.c @@ -1126,6 +1126,21 @@ int ofnode_write_string(ofnode node, const char *propname, const char *value) return ofnode_write_prop(node, propname, value, strlen(value) + 1); } +int ofnode_write_u32(ofnode node, const char *propname, u32 value) +{ + fdt32_t *val; + + assert(ofnode_valid(node)); + + log_debug("%s = %x", propname, value); + val = malloc(sizeof(*val)); + if (!val) + return -ENOMEM; + *val = cpu_to_fdt32(value); + + return ofnode_write_prop(node, propname, val, sizeof(value)); +} + int ofnode_set_enabled(ofnode node, bool value) { assert(ofnode_valid(node)); -- cgit v1.2.3