dm: core: Introduce dev_read_alias_highest_id()
[oweals/u-boot.git] / drivers / core / of_access.c
index 93a65604967bfb0f2c74646fd13331346b8ec8e5..945b81448ccef04fc9aa308695ea3f82e38d5003 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Originally from Linux v4.9
  * Paul Mackerras      August 1996.
  *
  * This file follows drivers/of/base.c with functions in the same order as the
  * Linux version.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
-#include <libfdt.h>
+#include <linux/libfdt.h>
 #include <dm/of_access.h>
 #include <linux/ctype.h>
 #include <linux/err.h>
@@ -96,6 +95,30 @@ int of_n_size_cells(const struct device_node *np)
        return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
 }
 
+int of_simple_addr_cells(const struct device_node *np)
+{
+       const __be32 *ip;
+
+       ip = of_get_property(np, "#address-cells", NULL);
+       if (ip)
+               return be32_to_cpup(ip);
+
+       /* Return a default of 2 to match fdt_address_cells()*/
+       return 2;
+}
+
+int of_simple_size_cells(const struct device_node *np)
+{
+       const __be32 *ip;
+
+       ip = of_get_property(np, "#size-cells", NULL);
+       if (ip)
+               return be32_to_cpup(ip);
+
+       /* Return a default of 2 to match fdt_size_cells()*/
+       return 2;
+}
+
 struct property *of_find_property(const struct device_node *np,
                                  const char *name, int *lenp)
 {
@@ -353,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;
@@ -434,6 +484,26 @@ int of_read_u32_array(const struct device_node *np, const char *propname,
        return 0;
 }
 
+int of_read_u64(const struct device_node *np, const char *propname, u64 *outp)
+{
+       const __be64 *val;
+
+       debug("%s: %s: ", __func__, propname);
+       if (!np)
+               return -EINVAL;
+       val = of_find_property_value_of_size(np, propname, sizeof(*outp));
+       if (IS_ERR(val)) {
+               debug("(not found)\n");
+               return PTR_ERR(val);
+       }
+
+       *outp = be64_to_cpup(val);
+       debug("%#llx (%lld)\n", (unsigned long long)*outp,
+              (unsigned long long)*outp);
+
+       return 0;
+}
+
 int of_property_match_string(const struct device_node *np, const char *propname,
                             const char *string)
 {
@@ -641,6 +711,13 @@ int of_parse_phandle_with_args(const struct device_node *np,
                                            index, out_args);
 }
 
+int of_count_phandle_with_args(const struct device_node *np,
+                              const char *list_name, const char *cells_name)
+{
+       return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
+                                           -1, NULL);
+}
+
 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
                         int id, const char *stem, int stem_len)
 {
@@ -735,6 +812,24 @@ int of_alias_get_id(const struct device_node *np, const char *stem)
        return id;
 }
 
+int of_alias_get_highest_id(const char *stem)
+{
+       struct alias_prop *app;
+       int id = -1;
+
+       mutex_lock(&of_mutex);
+       list_for_each_entry(app, &aliases_lookup, link) {
+               if (strcmp(app->stem, stem) != 0)
+                       continue;
+
+               if (app->id > id)
+                       id = app->id;
+       }
+       mutex_unlock(&of_mutex);
+
+       return id;
+}
+
 struct device_node *of_get_stdout(void)
 {
        return of_stdout;