Rob Herring | af6074f | 2017-12-27 12:55:14 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Procedures for creating, accessing and interpreting the device tree. |
| 4 | * |
| 5 | * Paul Mackerras August 1996. |
| 6 | * Copyright (C) 1996-2005 Paul Mackerras. |
| 7 | * |
| 8 | * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. |
| 9 | * {engebret|bergner}@us.ibm.com |
| 10 | * |
| 11 | * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net |
| 12 | * |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 13 | * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and |
| 14 | * Grant Likely. |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 15 | */ |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 16 | |
| 17 | #define pr_fmt(fmt) "OF: " fmt |
| 18 | |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 19 | #include <linux/console.h> |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 20 | #include <linux/ctype.h> |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 21 | #include <linux/cpu.h> |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/of.h> |
Sudeep Holla | 5fa2353 | 2017-01-16 10:40:43 +0000 | [diff] [blame] | 24 | #include <linux/of_device.h> |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 25 | #include <linux/of_graph.h> |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 26 | #include <linux/spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 28 | #include <linux/string.h> |
Jeremy Kerr | a9f2f63 | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 29 | #include <linux/proc_fs.h> |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 30 | |
Stepan Moskovchenko | ced4eec | 2012-12-06 14:55:41 -0800 | [diff] [blame] | 31 | #include "of_private.h" |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 32 | |
Stepan Moskovchenko | ced4eec | 2012-12-06 14:55:41 -0800 | [diff] [blame] | 33 | LIST_HEAD(aliases_lookup); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 34 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 35 | struct device_node *of_root; |
| 36 | EXPORT_SYMBOL(of_root); |
Grant Likely | fc0bdae | 2010-02-14 07:13:55 -0700 | [diff] [blame] | 37 | struct device_node *of_chosen; |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 38 | struct device_node *of_aliases; |
Grant Likely | a752ee5 | 2014-03-28 08:12:18 -0700 | [diff] [blame] | 39 | struct device_node *of_stdout; |
Leif Lindholm | 7914a7c | 2014-11-27 17:56:07 +0000 | [diff] [blame] | 40 | static const char *of_stdout_options; |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 41 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 42 | struct kset *of_kset; |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 43 | |
| 44 | /* |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 45 | * Used to protect the of_aliases, to hold off addition of nodes to sysfs. |
| 46 | * This mutex must be held whenever modifications are being made to the |
| 47 | * device tree. The of_{attach,detach}_node() and |
| 48 | * of_{add,remove,update}_property() helpers make sure this happens. |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 49 | */ |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 50 | DEFINE_MUTEX(of_mutex); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 51 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 52 | /* use when traversing tree through the child, sibling, |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 53 | * or parent members of struct device_node. |
| 54 | */ |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 55 | DEFINE_RAW_SPINLOCK(devtree_lock); |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 56 | |
Rob Herring | f42b0e18 | 2018-08-27 07:50:47 -0500 | [diff] [blame] | 57 | bool of_node_name_eq(const struct device_node *np, const char *name) |
| 58 | { |
| 59 | const char *node_name; |
| 60 | size_t len; |
| 61 | |
| 62 | if (!np) |
| 63 | return false; |
| 64 | |
| 65 | node_name = kbasename(np->full_name); |
| 66 | len = strchrnul(node_name, '@') - node_name; |
| 67 | |
| 68 | return (strlen(name) == len) && (strncmp(node_name, name, len) == 0); |
| 69 | } |
| 70 | |
| 71 | bool of_node_name_prefix(const struct device_node *np, const char *prefix) |
| 72 | { |
| 73 | if (!np) |
| 74 | return false; |
| 75 | |
| 76 | return strncmp(kbasename(np->full_name), prefix, strlen(prefix)) == 0; |
| 77 | } |
| 78 | |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 79 | int of_n_addr_cells(struct device_node *np) |
| 80 | { |
Sergei Shtylyov | 8832963 | 2017-07-23 19:55:47 +0300 | [diff] [blame] | 81 | u32 cells; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 82 | |
| 83 | do { |
| 84 | if (np->parent) |
| 85 | np = np->parent; |
Sergei Shtylyov | 8832963 | 2017-07-23 19:55:47 +0300 | [diff] [blame] | 86 | if (!of_property_read_u32(np, "#address-cells", &cells)) |
| 87 | return cells; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 88 | } while (np->parent); |
| 89 | /* No #address-cells property for the root node */ |
| 90 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 91 | } |
| 92 | EXPORT_SYMBOL(of_n_addr_cells); |
| 93 | |
| 94 | int of_n_size_cells(struct device_node *np) |
| 95 | { |
Sergei Shtylyov | 8832963 | 2017-07-23 19:55:47 +0300 | [diff] [blame] | 96 | u32 cells; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 97 | |
| 98 | do { |
| 99 | if (np->parent) |
| 100 | np = np->parent; |
Sergei Shtylyov | 8832963 | 2017-07-23 19:55:47 +0300 | [diff] [blame] | 101 | if (!of_property_read_u32(np, "#size-cells", &cells)) |
| 102 | return cells; |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 103 | } while (np->parent); |
| 104 | /* No #size-cells property for the root node */ |
| 105 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 106 | } |
| 107 | EXPORT_SYMBOL(of_n_size_cells); |
| 108 | |
Rob Herring | 0c3f061 | 2013-09-17 10:42:50 -0500 | [diff] [blame] | 109 | #ifdef CONFIG_NUMA |
| 110 | int __weak of_node_to_nid(struct device_node *np) |
| 111 | { |
Konstantin Khlebnikov | c8fff7b | 2015-04-08 19:59:20 +0300 | [diff] [blame] | 112 | return NUMA_NO_NODE; |
Rob Herring | 0c3f061 | 2013-09-17 10:42:50 -0500 | [diff] [blame] | 113 | } |
| 114 | #endif |
| 115 | |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 116 | static struct device_node **phandle_cache; |
| 117 | static u32 phandle_cache_mask; |
| 118 | |
| 119 | /* |
| 120 | * Assumptions behind phandle_cache implementation: |
| 121 | * - phandle property values are in a contiguous range of 1..n |
| 122 | * |
| 123 | * If the assumptions do not hold, then |
| 124 | * - the phandle lookup overhead reduction provided by the cache |
| 125 | * will likely be less |
| 126 | */ |
Frank Rowand | b9952b5 | 2018-07-12 14:00:07 -0700 | [diff] [blame] | 127 | void of_populate_phandle_cache(void) |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 128 | { |
| 129 | unsigned long flags; |
| 130 | u32 cache_entries; |
| 131 | struct device_node *np; |
| 132 | u32 phandles = 0; |
| 133 | |
| 134 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 135 | |
| 136 | kfree(phandle_cache); |
| 137 | phandle_cache = NULL; |
| 138 | |
| 139 | for_each_of_allnodes(np) |
| 140 | if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) |
| 141 | phandles++; |
| 142 | |
| 143 | cache_entries = roundup_pow_of_two(phandles); |
| 144 | phandle_cache_mask = cache_entries - 1; |
| 145 | |
| 146 | phandle_cache = kcalloc(cache_entries, sizeof(*phandle_cache), |
| 147 | GFP_ATOMIC); |
| 148 | if (!phandle_cache) |
| 149 | goto out; |
| 150 | |
| 151 | for_each_of_allnodes(np) |
| 152 | if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL) |
| 153 | phandle_cache[np->phandle & phandle_cache_mask] = np; |
| 154 | |
| 155 | out: |
| 156 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 157 | } |
| 158 | |
Frank Rowand | b9952b5 | 2018-07-12 14:00:07 -0700 | [diff] [blame] | 159 | int of_free_phandle_cache(void) |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 160 | { |
| 161 | unsigned long flags; |
| 162 | |
| 163 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 164 | |
| 165 | kfree(phandle_cache); |
| 166 | phandle_cache = NULL; |
| 167 | |
| 168 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 169 | |
| 170 | return 0; |
| 171 | } |
Frank Rowand | b9952b5 | 2018-07-12 14:00:07 -0700 | [diff] [blame] | 172 | #if !defined(CONFIG_MODULES) |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 173 | late_initcall_sync(of_free_phandle_cache); |
| 174 | #endif |
| 175 | |
Sudeep Holla | 194ec93 | 2015-05-14 15:28:24 +0100 | [diff] [blame] | 176 | void __init of_core_init(void) |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 177 | { |
| 178 | struct device_node *np; |
| 179 | |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 180 | of_populate_phandle_cache(); |
| 181 | |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 182 | /* Create the kset, and register existing nodes */ |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 183 | mutex_lock(&of_mutex); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 184 | of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj); |
| 185 | if (!of_kset) { |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 186 | mutex_unlock(&of_mutex); |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 187 | pr_err("failed to register existing nodes\n"); |
Sudeep Holla | 194ec93 | 2015-05-14 15:28:24 +0100 | [diff] [blame] | 188 | return; |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 189 | } |
| 190 | for_each_of_allnodes(np) |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 191 | __of_attach_node_sysfs(np); |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 192 | mutex_unlock(&of_mutex); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 193 | |
Grant Likely | 8357041 | 2012-11-06 21:03:27 +0000 | [diff] [blame] | 194 | /* Symlink in /proc as required by userspace ABI */ |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 195 | if (of_root) |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 196 | proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base"); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 197 | } |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 198 | |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 199 | static struct property *__of_find_property(const struct device_node *np, |
| 200 | const char *name, int *lenp) |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 201 | { |
| 202 | struct property *pp; |
| 203 | |
Timur Tabi | 64e4566 | 2008-05-08 05:19:59 +1000 | [diff] [blame] | 204 | if (!np) |
| 205 | return NULL; |
| 206 | |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 207 | for (pp = np->properties; pp; pp = pp->next) { |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 208 | if (of_prop_cmp(pp->name, name) == 0) { |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 209 | if (lenp) |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 210 | *lenp = pp->length; |
| 211 | break; |
| 212 | } |
| 213 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 214 | |
| 215 | return pp; |
| 216 | } |
| 217 | |
| 218 | struct property *of_find_property(const struct device_node *np, |
| 219 | const char *name, |
| 220 | int *lenp) |
| 221 | { |
| 222 | struct property *pp; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 223 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 224 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 225 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 226 | pp = __of_find_property(np, name, lenp); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 227 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 581b605 | 2007-04-24 16:46:53 +1000 | [diff] [blame] | 228 | |
| 229 | return pp; |
| 230 | } |
| 231 | EXPORT_SYMBOL(of_find_property); |
| 232 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 233 | struct device_node *__of_find_all_nodes(struct device_node *prev) |
| 234 | { |
| 235 | struct device_node *np; |
| 236 | if (!prev) { |
| 237 | np = of_root; |
| 238 | } else if (prev->child) { |
| 239 | np = prev->child; |
| 240 | } else { |
| 241 | /* Walk back up looking for a sibling, or the end of the structure */ |
| 242 | np = prev; |
| 243 | while (np->parent && !np->sibling) |
| 244 | np = np->parent; |
| 245 | np = np->sibling; /* Might be null at the end of the tree */ |
| 246 | } |
| 247 | return np; |
| 248 | } |
| 249 | |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 250 | /** |
| 251 | * of_find_all_nodes - Get next node in global list |
| 252 | * @prev: Previous node or NULL to start iteration |
| 253 | * of_node_put() will be called on it |
| 254 | * |
| 255 | * Returns a node pointer with refcount incremented, use |
| 256 | * of_node_put() on it when done. |
| 257 | */ |
| 258 | struct device_node *of_find_all_nodes(struct device_node *prev) |
| 259 | { |
| 260 | struct device_node *np; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 261 | unsigned long flags; |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 262 | |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 263 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 264 | np = __of_find_all_nodes(prev); |
| 265 | of_node_get(np); |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 266 | of_node_put(prev); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 267 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | e91edcf | 2009-10-15 10:58:09 -0600 | [diff] [blame] | 268 | return np; |
| 269 | } |
| 270 | EXPORT_SYMBOL(of_find_all_nodes); |
| 271 | |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 272 | /* |
| 273 | * Find a property with a given name for a given node |
| 274 | * and return the value. |
| 275 | */ |
Grant Likely | a25095d | 2014-07-15 23:25:43 -0600 | [diff] [blame] | 276 | const void *__of_get_property(const struct device_node *np, |
| 277 | const char *name, int *lenp) |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 278 | { |
| 279 | struct property *pp = __of_find_property(np, name, lenp); |
| 280 | |
| 281 | return pp ? pp->value : NULL; |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Find a property with a given name for a given node |
| 286 | * and return the value. |
| 287 | */ |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 288 | const void *of_get_property(const struct device_node *np, const char *name, |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 289 | int *lenp) |
Stephen Rothwell | 97e873e | 2007-05-01 16:26:07 +1000 | [diff] [blame] | 290 | { |
| 291 | struct property *pp = of_find_property(np, name, lenp); |
| 292 | |
| 293 | return pp ? pp->value : NULL; |
| 294 | } |
| 295 | EXPORT_SYMBOL(of_get_property); |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 296 | |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 297 | /* |
| 298 | * arch_match_cpu_phys_id - Match the given logical CPU and physical id |
| 299 | * |
| 300 | * @cpu: logical cpu index of a core/thread |
| 301 | * @phys_id: physical identifier of a core/thread |
| 302 | * |
| 303 | * CPU logical to physical index mapping is architecture specific. |
| 304 | * However this __weak function provides a default match of physical |
| 305 | * id to logical cpu index. phys_id provided here is usually values read |
| 306 | * from the device tree which must match the hardware internal registers. |
| 307 | * |
| 308 | * Returns true if the physical identifier and the logical cpu index |
| 309 | * correspond to the same core/thread, false otherwise. |
| 310 | */ |
| 311 | bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id) |
| 312 | { |
| 313 | return (u32)phys_id == cpu; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Checks if the given "prop_name" property holds the physical id of the |
| 318 | * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not |
| 319 | * NULL, local thread number within the core is returned in it. |
| 320 | */ |
| 321 | static bool __of_find_n_match_cpu_property(struct device_node *cpun, |
| 322 | const char *prop_name, int cpu, unsigned int *thread) |
| 323 | { |
| 324 | const __be32 *cell; |
| 325 | int ac, prop_len, tid; |
| 326 | u64 hwid; |
| 327 | |
| 328 | ac = of_n_addr_cells(cpun); |
| 329 | cell = of_get_property(cpun, prop_name, &prop_len); |
Rob Herring | 6487c15 | 2018-08-27 13:46:51 -0500 | [diff] [blame^] | 330 | if (!cell && !ac && arch_match_cpu_phys_id(cpu, 0)) |
| 331 | return true; |
Grant Likely | f3cea45 | 2013-10-04 17:24:26 +0100 | [diff] [blame] | 332 | if (!cell || !ac) |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 333 | return false; |
Grant Likely | f3cea45 | 2013-10-04 17:24:26 +0100 | [diff] [blame] | 334 | prop_len /= sizeof(*cell) * ac; |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 335 | for (tid = 0; tid < prop_len; tid++) { |
| 336 | hwid = of_read_number(cell, ac); |
| 337 | if (arch_match_cpu_phys_id(cpu, hwid)) { |
| 338 | if (thread) |
| 339 | *thread = tid; |
| 340 | return true; |
| 341 | } |
| 342 | cell += ac; |
| 343 | } |
| 344 | return false; |
| 345 | } |
| 346 | |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 347 | /* |
| 348 | * arch_find_n_match_cpu_physical_id - See if the given device node is |
| 349 | * for the cpu corresponding to logical cpu 'cpu'. Return true if so, |
| 350 | * else false. If 'thread' is non-NULL, the local thread number within the |
| 351 | * core is returned in it. |
| 352 | */ |
| 353 | bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun, |
| 354 | int cpu, unsigned int *thread) |
| 355 | { |
| 356 | /* Check for non-standard "ibm,ppc-interrupt-server#s" property |
| 357 | * for thread ids on PowerPC. If it doesn't exist fallback to |
| 358 | * standard "reg" property. |
| 359 | */ |
| 360 | if (IS_ENABLED(CONFIG_PPC) && |
| 361 | __of_find_n_match_cpu_property(cpun, |
| 362 | "ibm,ppc-interrupt-server#s", |
| 363 | cpu, thread)) |
| 364 | return true; |
| 365 | |
Masahiro Yamada | 510bd06 | 2015-10-28 12:05:27 +0900 | [diff] [blame] | 366 | return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread); |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 367 | } |
| 368 | |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 369 | /** |
| 370 | * of_get_cpu_node - Get device node associated with the given logical CPU |
| 371 | * |
| 372 | * @cpu: CPU number(logical index) for which device node is required |
| 373 | * @thread: if not NULL, local thread number within the physical core is |
| 374 | * returned |
| 375 | * |
| 376 | * The main purpose of this function is to retrieve the device node for the |
| 377 | * given logical CPU index. It should be used to initialize the of_node in |
| 378 | * cpu device. Once of_node in cpu device is populated, all the further |
| 379 | * references can use that instead. |
| 380 | * |
| 381 | * CPU logical to physical index mapping is architecture specific and is built |
| 382 | * before booting secondary cores. This function uses arch_match_cpu_phys_id |
| 383 | * which can be overridden by architecture specific implementation. |
| 384 | * |
Masahiro Yamada | 1c986e3 | 2016-04-20 10:18:46 +0900 | [diff] [blame] | 385 | * Returns a node pointer for the logical cpu with refcount incremented, use |
| 386 | * of_node_put() on it when done. Returns NULL if not found. |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 387 | */ |
| 388 | struct device_node *of_get_cpu_node(int cpu, unsigned int *thread) |
| 389 | { |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 390 | struct device_node *cpun; |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 391 | |
David Miller | d1cb9d1 | 2013-10-03 17:24:51 -0400 | [diff] [blame] | 392 | for_each_node_by_type(cpun, "cpu") { |
| 393 | if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread)) |
Sudeep KarkadaNagesha | 183912d | 2013-08-15 14:01:40 +0100 | [diff] [blame] | 394 | return cpun; |
| 395 | } |
| 396 | return NULL; |
| 397 | } |
| 398 | EXPORT_SYMBOL(of_get_cpu_node); |
| 399 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 400 | /** |
Suzuki K Poulose | a0e71cd | 2018-01-02 11:25:27 +0000 | [diff] [blame] | 401 | * of_cpu_node_to_id: Get the logical CPU number for a given device_node |
| 402 | * |
| 403 | * @cpu_node: Pointer to the device_node for CPU. |
| 404 | * |
| 405 | * Returns the logical CPU number of the given CPU device_node. |
| 406 | * Returns -ENODEV if the CPU is not found. |
| 407 | */ |
| 408 | int of_cpu_node_to_id(struct device_node *cpu_node) |
| 409 | { |
| 410 | int cpu; |
| 411 | bool found = false; |
| 412 | struct device_node *np; |
| 413 | |
| 414 | for_each_possible_cpu(cpu) { |
| 415 | np = of_cpu_device_node_get(cpu); |
| 416 | found = (cpu_node == np); |
| 417 | of_node_put(np); |
| 418 | if (found) |
| 419 | return cpu; |
| 420 | } |
| 421 | |
| 422 | return -ENODEV; |
| 423 | } |
| 424 | EXPORT_SYMBOL(of_cpu_node_to_id); |
| 425 | |
| 426 | /** |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 427 | * __of_device_is_compatible() - Check if the node matches given constraints |
| 428 | * @device: pointer to node |
| 429 | * @compat: required compatible string, NULL or "" for any match |
| 430 | * @type: required device_type value, NULL or "" for any match |
| 431 | * @name: required node name, NULL or "" for any match |
| 432 | * |
| 433 | * Checks if the given @compat, @type and @name strings match the |
| 434 | * properties of the given @device. A constraints can be skipped by |
| 435 | * passing NULL or an empty string as the constraint. |
| 436 | * |
| 437 | * Returns 0 for no match, and a positive integer on match. The return |
| 438 | * value is a relative score with larger values indicating better |
| 439 | * matches. The score is weighted for the most specific compatible value |
| 440 | * to get the highest score. Matching type is next, followed by matching |
| 441 | * name. Practically speaking, this results in the following priority |
| 442 | * order for matches: |
| 443 | * |
| 444 | * 1. specific compatible && type && name |
| 445 | * 2. specific compatible && type |
| 446 | * 3. specific compatible && name |
| 447 | * 4. specific compatible |
| 448 | * 5. general compatible && type && name |
| 449 | * 6. general compatible && type |
| 450 | * 7. general compatible && name |
| 451 | * 8. general compatible |
| 452 | * 9. type && name |
| 453 | * 10. type |
| 454 | * 11. name |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 455 | */ |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 456 | static int __of_device_is_compatible(const struct device_node *device, |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 457 | const char *compat, const char *type, const char *name) |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 458 | { |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 459 | struct property *prop; |
| 460 | const char *cp; |
| 461 | int index = 0, score = 0; |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 462 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 463 | /* Compatible match has highest priority */ |
| 464 | if (compat && compat[0]) { |
| 465 | prop = __of_find_property(device, "compatible", NULL); |
| 466 | for (cp = of_prop_next_string(prop, NULL); cp; |
| 467 | cp = of_prop_next_string(prop, cp), index++) { |
| 468 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) { |
| 469 | score = INT_MAX/2 - (index << 2); |
| 470 | break; |
| 471 | } |
| 472 | } |
| 473 | if (!score) |
| 474 | return 0; |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 475 | } |
| 476 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 477 | /* Matching type is better than matching name */ |
| 478 | if (type && type[0]) { |
| 479 | if (!device->type || of_node_cmp(type, device->type)) |
| 480 | return 0; |
| 481 | score += 2; |
| 482 | } |
| 483 | |
| 484 | /* Matching name is a bit better than not */ |
| 485 | if (name && name[0]) { |
| 486 | if (!device->name || of_node_cmp(name, device->name)) |
| 487 | return 0; |
| 488 | score++; |
| 489 | } |
| 490 | |
| 491 | return score; |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 492 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 493 | |
| 494 | /** Checks if the given "compat" string matches one of the strings in |
| 495 | * the device's "compatible" property |
| 496 | */ |
| 497 | int of_device_is_compatible(const struct device_node *device, |
| 498 | const char *compat) |
| 499 | { |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 500 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 501 | int res; |
| 502 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 503 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 504 | res = __of_device_is_compatible(device, compat, NULL, NULL); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 505 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 506 | return res; |
| 507 | } |
Stephen Rothwell | 0081cbc | 2007-05-01 16:29:19 +1000 | [diff] [blame] | 508 | EXPORT_SYMBOL(of_device_is_compatible); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 509 | |
Benjamin Herrenschmidt | b9c13fe3 | 2016-07-08 08:35:59 +1000 | [diff] [blame] | 510 | /** Checks if the device is compatible with any of the entries in |
| 511 | * a NULL terminated array of strings. Returns the best match |
| 512 | * score or 0. |
| 513 | */ |
| 514 | int of_device_compatible_match(struct device_node *device, |
| 515 | const char *const *compat) |
| 516 | { |
| 517 | unsigned int tmp, score = 0; |
| 518 | |
| 519 | if (!compat) |
| 520 | return 0; |
| 521 | |
| 522 | while (*compat) { |
| 523 | tmp = of_device_is_compatible(device, *compat); |
| 524 | if (tmp > score) |
| 525 | score = tmp; |
| 526 | compat++; |
| 527 | } |
| 528 | |
| 529 | return score; |
| 530 | } |
| 531 | |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 532 | /** |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 533 | * of_machine_is_compatible - Test root of device tree for a given compatible value |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 534 | * @compat: compatible string to look for in root node's compatible property. |
| 535 | * |
Kevin Cernekee | 25c7a1d | 2014-11-12 12:54:00 -0800 | [diff] [blame] | 536 | * Returns a positive integer if the root node has the given value in its |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 537 | * compatible property. |
| 538 | */ |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 539 | int of_machine_is_compatible(const char *compat) |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 540 | { |
| 541 | struct device_node *root; |
| 542 | int rc = 0; |
| 543 | |
| 544 | root = of_find_node_by_path("/"); |
| 545 | if (root) { |
| 546 | rc = of_device_is_compatible(root, compat); |
| 547 | of_node_put(root); |
| 548 | } |
| 549 | return rc; |
| 550 | } |
Grant Likely | 71a157e | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 551 | EXPORT_SYMBOL(of_machine_is_compatible); |
Grant Likely | 1f43cfb | 2010-01-28 13:47:25 -0700 | [diff] [blame] | 552 | |
| 553 | /** |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 554 | * __of_device_is_available - check if a device is available for use |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 555 | * |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 556 | * @device: Node to check for availability, with locks already held |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 557 | * |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 558 | * Returns true if the status property is absent or set to "okay" or "ok", |
| 559 | * false otherwise |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 560 | */ |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 561 | static bool __of_device_is_available(const struct device_node *device) |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 562 | { |
| 563 | const char *status; |
| 564 | int statlen; |
| 565 | |
Xiubo Li | 42ccd78 | 2014-01-13 11:07:28 +0800 | [diff] [blame] | 566 | if (!device) |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 567 | return false; |
Xiubo Li | 42ccd78 | 2014-01-13 11:07:28 +0800 | [diff] [blame] | 568 | |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 569 | status = __of_get_property(device, "status", &statlen); |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 570 | if (status == NULL) |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 571 | return true; |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 572 | |
| 573 | if (statlen > 0) { |
| 574 | if (!strcmp(status, "okay") || !strcmp(status, "ok")) |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 575 | return true; |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 576 | } |
| 577 | |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 578 | return false; |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 579 | } |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 580 | |
| 581 | /** |
| 582 | * of_device_is_available - check if a device is available for use |
| 583 | * |
| 584 | * @device: Node to check for availability |
| 585 | * |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 586 | * Returns true if the status property is absent or set to "okay" or "ok", |
| 587 | * false otherwise |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 588 | */ |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 589 | bool of_device_is_available(const struct device_node *device) |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 590 | { |
| 591 | unsigned long flags; |
Kevin Cernekee | 53a4ab9 | 2014-11-12 12:54:01 -0800 | [diff] [blame] | 592 | bool res; |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 593 | |
| 594 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 595 | res = __of_device_is_available(device); |
| 596 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 597 | return res; |
| 598 | |
| 599 | } |
Josh Boyer | 834d97d | 2008-03-27 00:33:14 +1100 | [diff] [blame] | 600 | EXPORT_SYMBOL(of_device_is_available); |
| 601 | |
| 602 | /** |
Kevin Cernekee | 37786c7 | 2015-04-09 13:05:14 -0700 | [diff] [blame] | 603 | * of_device_is_big_endian - check if a device has BE registers |
| 604 | * |
| 605 | * @device: Node to check for endianness |
| 606 | * |
| 607 | * Returns true if the device has a "big-endian" property, or if the kernel |
| 608 | * was compiled for BE *and* the device has a "native-endian" property. |
| 609 | * Returns false otherwise. |
| 610 | * |
| 611 | * Callers would nominally use ioread32be/iowrite32be if |
| 612 | * of_device_is_big_endian() == true, or readl/writel otherwise. |
| 613 | */ |
| 614 | bool of_device_is_big_endian(const struct device_node *device) |
| 615 | { |
| 616 | if (of_property_read_bool(device, "big-endian")) |
| 617 | return true; |
| 618 | if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) && |
| 619 | of_property_read_bool(device, "native-endian")) |
| 620 | return true; |
| 621 | return false; |
| 622 | } |
| 623 | EXPORT_SYMBOL(of_device_is_big_endian); |
| 624 | |
| 625 | /** |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 626 | * of_get_parent - Get a node's parent if any |
| 627 | * @node: Node to get parent |
| 628 | * |
| 629 | * Returns a node pointer with refcount incremented, use |
| 630 | * of_node_put() on it when done. |
| 631 | */ |
| 632 | struct device_node *of_get_parent(const struct device_node *node) |
| 633 | { |
| 634 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 635 | unsigned long flags; |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 636 | |
| 637 | if (!node) |
| 638 | return NULL; |
| 639 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 640 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 641 | np = of_node_get(node->parent); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 642 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | e679c5f | 2007-04-24 17:16:16 +1000 | [diff] [blame] | 643 | return np; |
| 644 | } |
| 645 | EXPORT_SYMBOL(of_get_parent); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 646 | |
| 647 | /** |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 648 | * of_get_next_parent - Iterate to a node's parent |
| 649 | * @node: Node to get parent of |
| 650 | * |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 651 | * This is like of_get_parent() except that it drops the |
| 652 | * refcount on the passed node, making it suitable for iterating |
| 653 | * through a node's parents. |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 654 | * |
| 655 | * Returns a node pointer with refcount incremented, use |
| 656 | * of_node_put() on it when done. |
| 657 | */ |
| 658 | struct device_node *of_get_next_parent(struct device_node *node) |
| 659 | { |
| 660 | struct device_node *parent; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 661 | unsigned long flags; |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 662 | |
| 663 | if (!node) |
| 664 | return NULL; |
| 665 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 666 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 667 | parent = of_node_get(node->parent); |
| 668 | of_node_put(node); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 669 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 670 | return parent; |
| 671 | } |
Guennadi Liakhovetski | 6695be6 | 2013-04-02 12:28:11 -0300 | [diff] [blame] | 672 | EXPORT_SYMBOL(of_get_next_parent); |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 673 | |
Grant Likely | 0d0e02d | 2014-05-22 01:04:17 +0900 | [diff] [blame] | 674 | static struct device_node *__of_get_next_child(const struct device_node *node, |
| 675 | struct device_node *prev) |
| 676 | { |
| 677 | struct device_node *next; |
| 678 | |
Florian Fainelli | 43cb436 | 2014-05-28 10:39:02 -0700 | [diff] [blame] | 679 | if (!node) |
| 680 | return NULL; |
| 681 | |
Grant Likely | 0d0e02d | 2014-05-22 01:04:17 +0900 | [diff] [blame] | 682 | next = prev ? prev->sibling : node->child; |
| 683 | for (; next; next = next->sibling) |
| 684 | if (of_node_get(next)) |
| 685 | break; |
| 686 | of_node_put(prev); |
| 687 | return next; |
| 688 | } |
| 689 | #define __for_each_child_of_node(parent, child) \ |
| 690 | for (child = __of_get_next_child(parent, NULL); child != NULL; \ |
| 691 | child = __of_get_next_child(parent, child)) |
| 692 | |
Michael Ellerman | f4eb010 | 2007-10-26 16:54:31 +1000 | [diff] [blame] | 693 | /** |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 694 | * of_get_next_child - Iterate a node childs |
| 695 | * @node: parent node |
| 696 | * @prev: previous child of the parent node, or NULL to get first |
| 697 | * |
Baruch Siach | 6480827 | 2015-03-19 14:03:41 +0200 | [diff] [blame] | 698 | * Returns a node pointer with refcount incremented, use of_node_put() on |
| 699 | * it when done. Returns NULL when prev is the last child. Decrements the |
| 700 | * refcount of prev. |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 701 | */ |
| 702 | struct device_node *of_get_next_child(const struct device_node *node, |
| 703 | struct device_node *prev) |
| 704 | { |
| 705 | struct device_node *next; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 706 | unsigned long flags; |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 707 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 708 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 0d0e02d | 2014-05-22 01:04:17 +0900 | [diff] [blame] | 709 | next = __of_get_next_child(node, prev); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 710 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | d1cd355 | 2007-04-24 17:21:29 +1000 | [diff] [blame] | 711 | return next; |
| 712 | } |
| 713 | EXPORT_SYMBOL(of_get_next_child); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 714 | |
| 715 | /** |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 716 | * of_get_next_available_child - Find the next available child node |
| 717 | * @node: parent node |
| 718 | * @prev: previous child of the parent node, or NULL to get first |
| 719 | * |
| 720 | * This function is like of_get_next_child(), except that it |
| 721 | * automatically skips any disabled nodes (i.e. status = "disabled"). |
| 722 | */ |
| 723 | struct device_node *of_get_next_available_child(const struct device_node *node, |
| 724 | struct device_node *prev) |
| 725 | { |
| 726 | struct device_node *next; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 727 | unsigned long flags; |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 728 | |
Florian Fainelli | 43cb436 | 2014-05-28 10:39:02 -0700 | [diff] [blame] | 729 | if (!node) |
| 730 | return NULL; |
| 731 | |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 732 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 733 | next = prev ? prev->sibling : node->child; |
| 734 | for (; next; next = next->sibling) { |
Stephen Warren | c31a0c0 | 2013-02-11 14:15:32 -0700 | [diff] [blame] | 735 | if (!__of_device_is_available(next)) |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 736 | continue; |
| 737 | if (of_node_get(next)) |
| 738 | break; |
| 739 | } |
| 740 | of_node_put(prev); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 741 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Timur Tabi | 3296193 | 2012-08-14 13:20:23 +0000 | [diff] [blame] | 742 | return next; |
| 743 | } |
| 744 | EXPORT_SYMBOL(of_get_next_available_child); |
| 745 | |
| 746 | /** |
Rob Herring | f1f207e | 2018-08-22 15:04:40 -0500 | [diff] [blame] | 747 | * of_get_next_cpu_node - Iterate on cpu nodes |
| 748 | * @prev: previous child of the /cpus node, or NULL to get first |
| 749 | * |
| 750 | * Returns a cpu node pointer with refcount incremented, use of_node_put() |
| 751 | * on it when done. Returns NULL when prev is the last child. Decrements |
| 752 | * the refcount of prev. |
| 753 | */ |
| 754 | struct device_node *of_get_next_cpu_node(struct device_node *prev) |
| 755 | { |
| 756 | struct device_node *next = NULL; |
| 757 | unsigned long flags; |
| 758 | struct device_node *node; |
| 759 | |
| 760 | if (!prev) |
| 761 | node = of_find_node_by_path("/cpus"); |
| 762 | |
| 763 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 764 | if (prev) |
| 765 | next = prev->sibling; |
| 766 | else if (node) { |
| 767 | next = node->child; |
| 768 | of_node_put(node); |
| 769 | } |
| 770 | for (; next; next = next->sibling) { |
| 771 | if (!(of_node_name_eq(next, "cpu") || |
| 772 | (next->type && !of_node_cmp(next->type, "cpu")))) |
| 773 | continue; |
| 774 | if (!__of_device_is_available(next)) |
| 775 | continue; |
| 776 | if (of_node_get(next)) |
| 777 | break; |
| 778 | } |
| 779 | of_node_put(prev); |
| 780 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 781 | return next; |
| 782 | } |
| 783 | EXPORT_SYMBOL(of_get_next_cpu_node); |
| 784 | |
| 785 | /** |
Johan Hovold | 36156f9 | 2018-08-27 10:21:45 +0200 | [diff] [blame] | 786 | * of_get_compatible_child - Find compatible child node |
| 787 | * @parent: parent node |
| 788 | * @compatible: compatible string |
| 789 | * |
| 790 | * Lookup child node whose compatible property contains the given compatible |
| 791 | * string. |
| 792 | * |
| 793 | * Returns a node pointer with refcount incremented, use of_node_put() on it |
| 794 | * when done; or NULL if not found. |
| 795 | */ |
| 796 | struct device_node *of_get_compatible_child(const struct device_node *parent, |
| 797 | const char *compatible) |
| 798 | { |
| 799 | struct device_node *child; |
| 800 | |
| 801 | for_each_child_of_node(parent, child) { |
| 802 | if (of_device_is_compatible(child, compatible)) |
| 803 | break; |
| 804 | } |
| 805 | |
| 806 | return child; |
| 807 | } |
| 808 | EXPORT_SYMBOL(of_get_compatible_child); |
| 809 | |
| 810 | /** |
Srinivas Kandagatla | 9c19761 | 2012-09-18 08:10:28 +0100 | [diff] [blame] | 811 | * of_get_child_by_name - Find the child node by name for a given parent |
| 812 | * @node: parent node |
| 813 | * @name: child name to look for. |
| 814 | * |
| 815 | * This function looks for child node for given matching name |
| 816 | * |
| 817 | * Returns a node pointer if found, with refcount incremented, use |
| 818 | * of_node_put() on it when done. |
| 819 | * Returns NULL if node is not found. |
| 820 | */ |
| 821 | struct device_node *of_get_child_by_name(const struct device_node *node, |
| 822 | const char *name) |
| 823 | { |
| 824 | struct device_node *child; |
| 825 | |
| 826 | for_each_child_of_node(node, child) |
| 827 | if (child->name && (of_node_cmp(child->name, name) == 0)) |
| 828 | break; |
| 829 | return child; |
| 830 | } |
| 831 | EXPORT_SYMBOL(of_get_child_by_name); |
| 832 | |
Frank Rowand | e0a58f3 | 2017-10-17 16:36:31 -0700 | [diff] [blame] | 833 | struct device_node *__of_find_node_by_path(struct device_node *parent, |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 834 | const char *path) |
| 835 | { |
| 836 | struct device_node *child; |
Leif Lindholm | 106937e | 2015-03-06 16:52:53 +0000 | [diff] [blame] | 837 | int len; |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 838 | |
Brian Norris | 721a09e | 2015-03-17 12:30:31 -0700 | [diff] [blame] | 839 | len = strcspn(path, "/:"); |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 840 | if (!len) |
| 841 | return NULL; |
| 842 | |
| 843 | __for_each_child_of_node(parent, child) { |
Rob Herring | 95e6b1f | 2017-06-01 18:00:00 -0500 | [diff] [blame] | 844 | const char *name = kbasename(child->full_name); |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 845 | if (strncmp(path, name, len) == 0 && (strlen(name) == len)) |
| 846 | return child; |
| 847 | } |
| 848 | return NULL; |
| 849 | } |
| 850 | |
Rob Herring | 27497e1 | 2017-06-02 12:43:18 -0500 | [diff] [blame] | 851 | struct device_node *__of_find_node_by_full_path(struct device_node *node, |
| 852 | const char *path) |
| 853 | { |
| 854 | const char *separator = strchr(path, ':'); |
| 855 | |
| 856 | while (node && *path == '/') { |
| 857 | struct device_node *tmp = node; |
| 858 | |
| 859 | path++; /* Increment past '/' delimiter */ |
| 860 | node = __of_find_node_by_path(node, path); |
| 861 | of_node_put(tmp); |
| 862 | path = strchrnul(path, '/'); |
| 863 | if (separator && separator < path) |
| 864 | break; |
| 865 | } |
| 866 | return node; |
| 867 | } |
| 868 | |
Srinivas Kandagatla | 9c19761 | 2012-09-18 08:10:28 +0100 | [diff] [blame] | 869 | /** |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 870 | * of_find_node_opts_by_path - Find a node matching a full OF path |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 871 | * @path: Either the full path to match, or if the path does not |
| 872 | * start with '/', the name of a property of the /aliases |
| 873 | * node (an alias). In the case of an alias, the node |
| 874 | * matching the alias' value will be returned. |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 875 | * @opts: Address of a pointer into which to store the start of |
| 876 | * an options string appended to the end of the path with |
| 877 | * a ':' separator. |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 878 | * |
| 879 | * Valid paths: |
| 880 | * /foo/bar Full path |
| 881 | * foo Valid alias |
| 882 | * foo/bar Valid alias + relative path |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 883 | * |
| 884 | * Returns a node pointer with refcount incremented, use |
| 885 | * of_node_put() on it when done. |
| 886 | */ |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 887 | struct device_node *of_find_node_opts_by_path(const char *path, const char **opts) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 888 | { |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 889 | struct device_node *np = NULL; |
| 890 | struct property *pp; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 891 | unsigned long flags; |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 892 | const char *separator = strchr(path, ':'); |
| 893 | |
| 894 | if (opts) |
| 895 | *opts = separator ? separator + 1 : NULL; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 896 | |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 897 | if (strcmp(path, "/") == 0) |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 898 | return of_node_get(of_root); |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 899 | |
| 900 | /* The path could begin with an alias */ |
| 901 | if (*path != '/') { |
Leif Lindholm | 106937e | 2015-03-06 16:52:53 +0000 | [diff] [blame] | 902 | int len; |
| 903 | const char *p = separator; |
| 904 | |
| 905 | if (!p) |
| 906 | p = strchrnul(path, '/'); |
| 907 | len = p - path; |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 908 | |
| 909 | /* of_aliases must not be NULL */ |
| 910 | if (!of_aliases) |
| 911 | return NULL; |
| 912 | |
| 913 | for_each_property_of_node(of_aliases, pp) { |
| 914 | if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) { |
| 915 | np = of_find_node_by_path(pp->value); |
| 916 | break; |
| 917 | } |
| 918 | } |
| 919 | if (!np) |
| 920 | return NULL; |
| 921 | path = p; |
| 922 | } |
| 923 | |
| 924 | /* Step down the tree matching path components */ |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 925 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | c22e650 | 2014-03-14 17:07:12 +0000 | [diff] [blame] | 926 | if (!np) |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 927 | np = of_node_get(of_root); |
Rob Herring | 27497e1 | 2017-06-02 12:43:18 -0500 | [diff] [blame] | 928 | np = __of_find_node_by_full_path(np, path); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 929 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 930 | return np; |
| 931 | } |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 932 | EXPORT_SYMBOL(of_find_node_opts_by_path); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 933 | |
| 934 | /** |
| 935 | * of_find_node_by_name - Find a node by its "name" property |
Stephen Boyd | 02a876b | 2017-11-17 08:53:21 -0800 | [diff] [blame] | 936 | * @from: The node to start searching from or NULL; the node |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 937 | * you pass will not be searched, only the next one |
Stephen Boyd | 02a876b | 2017-11-17 08:53:21 -0800 | [diff] [blame] | 938 | * will. Typically, you pass what the previous call |
| 939 | * returned. of_node_put() will be called on @from. |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 940 | * @name: The name string to match against |
| 941 | * |
| 942 | * Returns a node pointer with refcount incremented, use |
| 943 | * of_node_put() on it when done. |
| 944 | */ |
| 945 | struct device_node *of_find_node_by_name(struct device_node *from, |
| 946 | const char *name) |
| 947 | { |
| 948 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 949 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 950 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 951 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 952 | for_each_of_allnodes_from(from, np) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 953 | if (np->name && (of_node_cmp(np->name, name) == 0) |
| 954 | && of_node_get(np)) |
| 955 | break; |
| 956 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 957 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 958 | return np; |
| 959 | } |
| 960 | EXPORT_SYMBOL(of_find_node_by_name); |
| 961 | |
| 962 | /** |
| 963 | * of_find_node_by_type - Find a node by its "device_type" property |
| 964 | * @from: The node to start searching from, or NULL to start searching |
| 965 | * the entire device tree. The node you pass will not be |
| 966 | * searched, only the next one will; typically, you pass |
| 967 | * what the previous call returned. of_node_put() will be |
| 968 | * called on from for you. |
| 969 | * @type: The type string to match against |
| 970 | * |
| 971 | * Returns a node pointer with refcount incremented, use |
| 972 | * of_node_put() on it when done. |
| 973 | */ |
| 974 | struct device_node *of_find_node_by_type(struct device_node *from, |
| 975 | const char *type) |
| 976 | { |
| 977 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 978 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 979 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 980 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 981 | for_each_of_allnodes_from(from, np) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 982 | if (np->type && (of_node_cmp(np->type, type) == 0) |
| 983 | && of_node_get(np)) |
| 984 | break; |
| 985 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 986 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 987 | return np; |
| 988 | } |
| 989 | EXPORT_SYMBOL(of_find_node_by_type); |
| 990 | |
| 991 | /** |
| 992 | * of_find_compatible_node - Find a node based on type and one of the |
| 993 | * tokens in its "compatible" property |
| 994 | * @from: The node to start searching from or NULL, the node |
| 995 | * you pass will not be searched, only the next one |
| 996 | * will; typically, you pass what the previous call |
| 997 | * returned. of_node_put() will be called on it |
| 998 | * @type: The type string to match "device_type" or NULL to ignore |
| 999 | * @compatible: The string to match to one of the tokens in the device |
| 1000 | * "compatible" list. |
| 1001 | * |
| 1002 | * Returns a node pointer with refcount incremented, use |
| 1003 | * of_node_put() on it when done. |
| 1004 | */ |
| 1005 | struct device_node *of_find_compatible_node(struct device_node *from, |
| 1006 | const char *type, const char *compatible) |
| 1007 | { |
| 1008 | struct device_node *np; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1009 | unsigned long flags; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1010 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1011 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1012 | for_each_of_allnodes_from(from, np) |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 1013 | if (__of_device_is_compatible(np, compatible, type, NULL) && |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1014 | of_node_get(np)) |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1015 | break; |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1016 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1017 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Stephen Rothwell | 1ef4d42 | 2007-04-24 17:57:33 +1000 | [diff] [blame] | 1018 | return np; |
| 1019 | } |
| 1020 | EXPORT_SYMBOL(of_find_compatible_node); |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1021 | |
| 1022 | /** |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 1023 | * of_find_node_with_property - Find a node which has a property with |
| 1024 | * the given name. |
| 1025 | * @from: The node to start searching from or NULL, the node |
| 1026 | * you pass will not be searched, only the next one |
| 1027 | * will; typically, you pass what the previous call |
| 1028 | * returned. of_node_put() will be called on it |
| 1029 | * @prop_name: The name of the property to look for. |
| 1030 | * |
| 1031 | * Returns a node pointer with refcount incremented, use |
| 1032 | * of_node_put() on it when done. |
| 1033 | */ |
| 1034 | struct device_node *of_find_node_with_property(struct device_node *from, |
| 1035 | const char *prop_name) |
| 1036 | { |
| 1037 | struct device_node *np; |
| 1038 | struct property *pp; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1039 | unsigned long flags; |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 1040 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1041 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1042 | for_each_of_allnodes_from(from, np) { |
Sachin Kamat | a3a7cab | 2012-06-27 09:44:45 +0530 | [diff] [blame] | 1043 | for (pp = np->properties; pp; pp = pp->next) { |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 1044 | if (of_prop_cmp(pp->name, prop_name) == 0) { |
| 1045 | of_node_get(np); |
| 1046 | goto out; |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | out: |
| 1051 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1052 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Michael Ellerman | 1e291b14 | 2008-11-12 18:54:42 +0000 | [diff] [blame] | 1053 | return np; |
| 1054 | } |
| 1055 | EXPORT_SYMBOL(of_find_node_with_property); |
| 1056 | |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1057 | static |
| 1058 | const struct of_device_id *__of_match_node(const struct of_device_id *matches, |
| 1059 | const struct device_node *node) |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1060 | { |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 1061 | const struct of_device_id *best_match = NULL; |
| 1062 | int score, best_score = 0; |
| 1063 | |
Grant Likely | a52f07e | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 1064 | if (!matches) |
| 1065 | return NULL; |
| 1066 | |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 1067 | for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) { |
| 1068 | score = __of_device_is_compatible(node, matches->compatible, |
| 1069 | matches->type, matches->name); |
| 1070 | if (score > best_score) { |
| 1071 | best_match = matches; |
| 1072 | best_score = score; |
| 1073 | } |
Kevin Hao | 4e8ca6e | 2014-02-14 13:22:45 +0800 | [diff] [blame] | 1074 | } |
Kevin Hao | 215a14c | 2014-02-19 16:15:45 +0800 | [diff] [blame] | 1075 | |
| 1076 | return best_match; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1077 | } |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1078 | |
| 1079 | /** |
Geert Uytterhoeven | c50949d | 2014-10-22 11:44:54 +0200 | [diff] [blame] | 1080 | * of_match_node - Tell if a device_node has a matching of_match structure |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1081 | * @matches: array of of device match structures to search in |
| 1082 | * @node: the of device structure to match against |
| 1083 | * |
Kevin Hao | 71c5498 | 2014-02-18 15:57:29 +0800 | [diff] [blame] | 1084 | * Low level utility function used by device matching. |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1085 | */ |
| 1086 | const struct of_device_id *of_match_node(const struct of_device_id *matches, |
| 1087 | const struct device_node *node) |
| 1088 | { |
| 1089 | const struct of_device_id *match; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1090 | unsigned long flags; |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1091 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1092 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1093 | match = __of_match_node(matches, node); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1094 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1095 | return match; |
| 1096 | } |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1097 | EXPORT_SYMBOL(of_match_node); |
| 1098 | |
| 1099 | /** |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1100 | * of_find_matching_node_and_match - Find a node based on an of_device_id |
| 1101 | * match table. |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1102 | * @from: The node to start searching from or NULL, the node |
| 1103 | * you pass will not be searched, only the next one |
| 1104 | * will; typically, you pass what the previous call |
| 1105 | * returned. of_node_put() will be called on it |
| 1106 | * @matches: array of of device match structures to search in |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1107 | * @match Updated to point at the matches entry which matched |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1108 | * |
| 1109 | * Returns a node pointer with refcount incremented, use |
| 1110 | * of_node_put() on it when done. |
| 1111 | */ |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1112 | struct device_node *of_find_matching_node_and_match(struct device_node *from, |
| 1113 | const struct of_device_id *matches, |
| 1114 | const struct of_device_id **match) |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1115 | { |
| 1116 | struct device_node *np; |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 1117 | const struct of_device_id *m; |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1118 | unsigned long flags; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1119 | |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1120 | if (match) |
| 1121 | *match = NULL; |
| 1122 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1123 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1124 | for_each_of_allnodes_from(from, np) { |
Thomas Gleixner | 28d0e36 | 2013-01-25 13:21:47 -0500 | [diff] [blame] | 1125 | m = __of_match_node(matches, np); |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 1126 | if (m && of_node_get(np)) { |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1127 | if (match) |
Thomas Abraham | dc71bcf | 2013-01-19 10:20:42 -0800 | [diff] [blame] | 1128 | *match = m; |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1129 | break; |
Stephen Warren | 50c8af4 | 2012-11-20 16:12:20 -0700 | [diff] [blame] | 1130 | } |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1131 | } |
| 1132 | of_node_put(from); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1133 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 283029d | 2008-01-09 06:20:40 +1100 | [diff] [blame] | 1134 | return np; |
| 1135 | } |
Grant Likely | 80c2022 | 2012-12-19 10:45:36 +0000 | [diff] [blame] | 1136 | EXPORT_SYMBOL(of_find_matching_node_and_match); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1137 | |
| 1138 | /** |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1139 | * of_modalias_node - Lookup appropriate modalias for a device node |
| 1140 | * @node: pointer to a device tree node |
| 1141 | * @modalias: Pointer to buffer that modalias value will be copied into |
| 1142 | * @len: Length of modalias value |
| 1143 | * |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1144 | * Based on the value of the compatible property, this routine will attempt |
| 1145 | * to choose an appropriate modalias value for a particular device tree node. |
| 1146 | * It does this by stripping the manufacturer prefix (as delimited by a ',') |
| 1147 | * from the first entry in the compatible list property. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1148 | * |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1149 | * This routine returns 0 on success, <0 on failure. |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1150 | */ |
| 1151 | int of_modalias_node(struct device_node *node, char *modalias, int len) |
| 1152 | { |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1153 | const char *compatible, *p; |
| 1154 | int cplen; |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1155 | |
| 1156 | compatible = of_get_property(node, "compatible", &cplen); |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1157 | if (!compatible || strlen(compatible) > cplen) |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1158 | return -ENODEV; |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1159 | p = strchr(compatible, ','); |
Grant Likely | 2ffe8c5 | 2010-06-08 07:48:19 -0600 | [diff] [blame] | 1160 | strlcpy(modalias, p ? p + 1 : compatible, len); |
Grant Likely | 3f07af4 | 2008-07-25 22:25:13 -0400 | [diff] [blame] | 1161 | return 0; |
| 1162 | } |
| 1163 | EXPORT_SYMBOL_GPL(of_modalias_node); |
| 1164 | |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1165 | /** |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1166 | * of_find_node_by_phandle - Find a node given a phandle |
| 1167 | * @handle: phandle of the node to find |
| 1168 | * |
| 1169 | * Returns a node pointer with refcount incremented, use |
| 1170 | * of_node_put() on it when done. |
| 1171 | */ |
| 1172 | struct device_node *of_find_node_by_phandle(phandle handle) |
| 1173 | { |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 1174 | struct device_node *np = NULL; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 1175 | unsigned long flags; |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 1176 | phandle masked_handle; |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1177 | |
Grant Likely | fc59b44 | 2014-10-02 13:08:02 +0100 | [diff] [blame] | 1178 | if (!handle) |
| 1179 | return NULL; |
| 1180 | |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 1181 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Frank Rowand | 0b3ce78 | 2018-03-04 16:14:47 -0800 | [diff] [blame] | 1182 | |
| 1183 | masked_handle = handle & phandle_cache_mask; |
| 1184 | |
| 1185 | if (phandle_cache) { |
| 1186 | if (phandle_cache[masked_handle] && |
| 1187 | handle == phandle_cache[masked_handle]->phandle) |
| 1188 | np = phandle_cache[masked_handle]; |
| 1189 | } |
| 1190 | |
| 1191 | if (!np) { |
| 1192 | for_each_of_allnodes(np) |
| 1193 | if (np->phandle == handle) { |
| 1194 | if (phandle_cache) |
| 1195 | phandle_cache[masked_handle] = np; |
| 1196 | break; |
| 1197 | } |
| 1198 | } |
| 1199 | |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1200 | of_node_get(np); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 1201 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Jeremy Kerr | 89751a7 | 2010-02-01 21:34:11 -0700 | [diff] [blame] | 1202 | return np; |
| 1203 | } |
| 1204 | EXPORT_SYMBOL(of_find_node_by_phandle); |
| 1205 | |
Grant Likely | 624cfca | 2013-10-11 22:05:10 +0100 | [diff] [blame] | 1206 | void of_print_phandle_args(const char *msg, const struct of_phandle_args *args) |
| 1207 | { |
| 1208 | int i; |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1209 | printk("%s %pOF", msg, args->np); |
Marcin Nowakowski | 4aa6634 | 2016-12-01 09:00:55 +0100 | [diff] [blame] | 1210 | for (i = 0; i < args->args_count; i++) { |
| 1211 | const char delim = i ? ',' : ':'; |
| 1212 | |
| 1213 | pr_cont("%c%08x", delim, args->args[i]); |
| 1214 | } |
| 1215 | pr_cont("\n"); |
Grant Likely | 624cfca | 2013-10-11 22:05:10 +0100 | [diff] [blame] | 1216 | } |
| 1217 | |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1218 | int of_phandle_iterator_init(struct of_phandle_iterator *it, |
| 1219 | const struct device_node *np, |
| 1220 | const char *list_name, |
| 1221 | const char *cells_name, |
| 1222 | int cell_count) |
| 1223 | { |
| 1224 | const __be32 *list; |
| 1225 | int size; |
| 1226 | |
| 1227 | memset(it, 0, sizeof(*it)); |
| 1228 | |
| 1229 | list = of_get_property(np, list_name, &size); |
| 1230 | if (!list) |
| 1231 | return -ENOENT; |
| 1232 | |
| 1233 | it->cells_name = cells_name; |
| 1234 | it->cell_count = cell_count; |
| 1235 | it->parent = np; |
| 1236 | it->list_end = list + size / sizeof(*list); |
| 1237 | it->phandle_end = list; |
| 1238 | it->cur = list; |
| 1239 | |
| 1240 | return 0; |
| 1241 | } |
Kuninori Morimoto | 00bab23 | 2017-04-20 01:31:16 +0000 | [diff] [blame] | 1242 | EXPORT_SYMBOL_GPL(of_phandle_iterator_init); |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1243 | |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1244 | int of_phandle_iterator_next(struct of_phandle_iterator *it) |
| 1245 | { |
| 1246 | uint32_t count = 0; |
| 1247 | |
| 1248 | if (it->node) { |
| 1249 | of_node_put(it->node); |
| 1250 | it->node = NULL; |
| 1251 | } |
| 1252 | |
| 1253 | if (!it->cur || it->phandle_end >= it->list_end) |
| 1254 | return -ENOENT; |
| 1255 | |
| 1256 | it->cur = it->phandle_end; |
| 1257 | |
| 1258 | /* If phandle is 0, then it is an empty entry with no arguments. */ |
| 1259 | it->phandle = be32_to_cpup(it->cur++); |
| 1260 | |
| 1261 | if (it->phandle) { |
| 1262 | |
| 1263 | /* |
| 1264 | * Find the provider node and parse the #*-cells property to |
| 1265 | * determine the argument length. |
| 1266 | */ |
| 1267 | it->node = of_find_node_by_phandle(it->phandle); |
| 1268 | |
| 1269 | if (it->cells_name) { |
| 1270 | if (!it->node) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1271 | pr_err("%pOF: could not find phandle\n", |
| 1272 | it->parent); |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1273 | goto err; |
| 1274 | } |
| 1275 | |
| 1276 | if (of_property_read_u32(it->node, it->cells_name, |
| 1277 | &count)) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1278 | pr_err("%pOF: could not get %s for %pOF\n", |
| 1279 | it->parent, |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1280 | it->cells_name, |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1281 | it->node); |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1282 | goto err; |
| 1283 | } |
| 1284 | } else { |
| 1285 | count = it->cell_count; |
| 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | * Make sure that the arguments actually fit in the remaining |
| 1290 | * property data length |
| 1291 | */ |
| 1292 | if (it->cur + count > it->list_end) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1293 | pr_err("%pOF: arguments longer than property\n", |
| 1294 | it->parent); |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1295 | goto err; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | it->phandle_end = it->cur + count; |
| 1300 | it->cur_count = count; |
| 1301 | |
| 1302 | return 0; |
| 1303 | |
| 1304 | err: |
| 1305 | if (it->node) { |
| 1306 | of_node_put(it->node); |
| 1307 | it->node = NULL; |
| 1308 | } |
| 1309 | |
| 1310 | return -EINVAL; |
| 1311 | } |
Kuninori Morimoto | 00bab23 | 2017-04-20 01:31:16 +0000 | [diff] [blame] | 1312 | EXPORT_SYMBOL_GPL(of_phandle_iterator_next); |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1313 | |
Joerg Roedel | abdaa77 | 2016-04-04 17:49:21 +0200 | [diff] [blame] | 1314 | int of_phandle_iterator_args(struct of_phandle_iterator *it, |
| 1315 | uint32_t *args, |
| 1316 | int size) |
| 1317 | { |
| 1318 | int i, count; |
| 1319 | |
| 1320 | count = it->cur_count; |
| 1321 | |
| 1322 | if (WARN_ON(size < count)) |
| 1323 | count = size; |
| 1324 | |
| 1325 | for (i = 0; i < count; i++) |
| 1326 | args[i] = be32_to_cpup(it->cur++); |
| 1327 | |
| 1328 | return count; |
| 1329 | } |
| 1330 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1331 | static int __of_parse_phandle_with_args(const struct device_node *np, |
| 1332 | const char *list_name, |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1333 | const char *cells_name, |
| 1334 | int cell_count, int index, |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1335 | struct of_phandle_args *out_args) |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1336 | { |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1337 | struct of_phandle_iterator it; |
| 1338 | int rc, cur_index = 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1339 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1340 | /* Loop over the phandles until all the requested entry is found */ |
Joerg Roedel | f623ce9 | 2016-04-04 17:49:20 +0200 | [diff] [blame] | 1341 | of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) { |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1342 | /* |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1343 | * All of the error cases bail out of the loop, so at |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1344 | * this point, the parsing is successful. If the requested |
| 1345 | * index matches, then fill the out_args structure and return, |
| 1346 | * or return -ENOENT for an empty entry. |
| 1347 | */ |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1348 | rc = -ENOENT; |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1349 | if (cur_index == index) { |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1350 | if (!it.phandle) |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1351 | goto err; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1352 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1353 | if (out_args) { |
Joerg Roedel | abdaa77 | 2016-04-04 17:49:21 +0200 | [diff] [blame] | 1354 | int c; |
| 1355 | |
| 1356 | c = of_phandle_iterator_args(&it, |
| 1357 | out_args->args, |
| 1358 | MAX_PHANDLE_ARGS); |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1359 | out_args->np = it.node; |
Joerg Roedel | abdaa77 | 2016-04-04 17:49:21 +0200 | [diff] [blame] | 1360 | out_args->args_count = c; |
Tang Yuantian | b855f16 | 2013-04-10 11:36:39 +0800 | [diff] [blame] | 1361 | } else { |
Joerg Roedel | 74e1fbb | 2016-04-04 17:49:17 +0200 | [diff] [blame] | 1362 | of_node_put(it.node); |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1363 | } |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1364 | |
| 1365 | /* Found it! return success */ |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1366 | return 0; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1367 | } |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1368 | |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1369 | cur_index++; |
| 1370 | } |
| 1371 | |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1372 | /* |
| 1373 | * Unlock node before returning result; will be one of: |
| 1374 | * -ENOENT : index is for empty phandle |
| 1375 | * -EINVAL : parsing error on data |
| 1376 | */ |
Joerg Roedel | cd209b4 | 2016-04-04 17:49:18 +0200 | [diff] [blame] | 1377 | |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1378 | err: |
Markus Elfring | beab47d | 2016-07-19 22:42:22 +0200 | [diff] [blame] | 1379 | of_node_put(it.node); |
Grant Likely | 23ce04c | 2013-02-12 21:21:49 +0000 | [diff] [blame] | 1380 | return rc; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 1381 | } |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1382 | |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1383 | /** |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1384 | * of_parse_phandle - Resolve a phandle property to a device_node pointer |
| 1385 | * @np: Pointer to device node holding phandle property |
| 1386 | * @phandle_name: Name of property holding a phandle value |
| 1387 | * @index: For properties holding a table of phandles, this is the index into |
| 1388 | * the table |
| 1389 | * |
| 1390 | * Returns the device_node pointer with refcount incremented. Use |
| 1391 | * of_node_put() on it when done. |
| 1392 | */ |
| 1393 | struct device_node *of_parse_phandle(const struct device_node *np, |
| 1394 | const char *phandle_name, int index) |
| 1395 | { |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1396 | struct of_phandle_args args; |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1397 | |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1398 | if (index < 0) |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1399 | return NULL; |
| 1400 | |
Stephen Warren | 91d9942 | 2013-08-14 15:27:11 -0600 | [diff] [blame] | 1401 | if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, |
| 1402 | index, &args)) |
| 1403 | return NULL; |
| 1404 | |
| 1405 | return args.np; |
Stephen Warren | 5fba49e | 2013-08-14 15:27:09 -0600 | [diff] [blame] | 1406 | } |
| 1407 | EXPORT_SYMBOL(of_parse_phandle); |
| 1408 | |
| 1409 | /** |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1410 | * of_parse_phandle_with_args() - Find a node pointed by phandle in a list |
| 1411 | * @np: pointer to a device tree node containing a list |
| 1412 | * @list_name: property name that contains a list |
| 1413 | * @cells_name: property name that specifies phandles' arguments count |
| 1414 | * @index: index of a phandle to parse out |
| 1415 | * @out_args: optional pointer to output arguments structure (will be filled) |
| 1416 | * |
| 1417 | * This function is useful to parse lists of phandles and their arguments. |
| 1418 | * Returns 0 on success and fills out_args, on error returns appropriate |
| 1419 | * errno value. |
| 1420 | * |
Geert Uytterhoeven | d94a75c | 2014-10-22 11:44:52 +0200 | [diff] [blame] | 1421 | * Caller is responsible to call of_node_put() on the returned out_args->np |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1422 | * pointer. |
| 1423 | * |
| 1424 | * Example: |
| 1425 | * |
| 1426 | * phandle1: node1 { |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 1427 | * #list-cells = <2>; |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1428 | * } |
| 1429 | * |
| 1430 | * phandle2: node2 { |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 1431 | * #list-cells = <1>; |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1432 | * } |
| 1433 | * |
| 1434 | * node3 { |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 1435 | * list = <&phandle1 1 2 &phandle2 3>; |
Stephen Warren | eded9dd | 2013-08-14 15:27:08 -0600 | [diff] [blame] | 1436 | * } |
| 1437 | * |
| 1438 | * To get a device_node of the `node2' node you may call this: |
| 1439 | * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); |
| 1440 | */ |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1441 | int of_parse_phandle_with_args(const struct device_node *np, const char *list_name, |
| 1442 | const char *cells_name, int index, |
| 1443 | struct of_phandle_args *out_args) |
| 1444 | { |
| 1445 | if (index < 0) |
| 1446 | return -EINVAL; |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1447 | return __of_parse_phandle_with_args(np, list_name, cells_name, 0, |
| 1448 | index, out_args); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1449 | } |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1450 | EXPORT_SYMBOL(of_parse_phandle_with_args); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1451 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1452 | /** |
Stephen Boyd | bd6f2fd | 2018-01-30 18:36:16 -0800 | [diff] [blame] | 1453 | * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it |
| 1454 | * @np: pointer to a device tree node containing a list |
| 1455 | * @list_name: property name that contains a list |
| 1456 | * @stem_name: stem of property names that specify phandles' arguments count |
| 1457 | * @index: index of a phandle to parse out |
| 1458 | * @out_args: optional pointer to output arguments structure (will be filled) |
| 1459 | * |
| 1460 | * This function is useful to parse lists of phandles and their arguments. |
| 1461 | * Returns 0 on success and fills out_args, on error returns appropriate errno |
| 1462 | * value. The difference between this function and of_parse_phandle_with_args() |
| 1463 | * is that this API remaps a phandle if the node the phandle points to has |
| 1464 | * a <@stem_name>-map property. |
| 1465 | * |
| 1466 | * Caller is responsible to call of_node_put() on the returned out_args->np |
| 1467 | * pointer. |
| 1468 | * |
| 1469 | * Example: |
| 1470 | * |
| 1471 | * phandle1: node1 { |
| 1472 | * #list-cells = <2>; |
| 1473 | * } |
| 1474 | * |
| 1475 | * phandle2: node2 { |
| 1476 | * #list-cells = <1>; |
| 1477 | * } |
| 1478 | * |
| 1479 | * phandle3: node3 { |
| 1480 | * #list-cells = <1>; |
| 1481 | * list-map = <0 &phandle2 3>, |
| 1482 | * <1 &phandle2 2>, |
| 1483 | * <2 &phandle1 5 1>; |
| 1484 | * list-map-mask = <0x3>; |
| 1485 | * }; |
| 1486 | * |
| 1487 | * node4 { |
| 1488 | * list = <&phandle1 1 2 &phandle3 0>; |
| 1489 | * } |
| 1490 | * |
| 1491 | * To get a device_node of the `node2' node you may call this: |
| 1492 | * of_parse_phandle_with_args(node4, "list", "list", 1, &args); |
| 1493 | */ |
| 1494 | int of_parse_phandle_with_args_map(const struct device_node *np, |
| 1495 | const char *list_name, |
| 1496 | const char *stem_name, |
| 1497 | int index, struct of_phandle_args *out_args) |
| 1498 | { |
| 1499 | char *cells_name, *map_name = NULL, *mask_name = NULL; |
| 1500 | char *pass_name = NULL; |
| 1501 | struct device_node *cur, *new = NULL; |
| 1502 | const __be32 *map, *mask, *pass; |
| 1503 | static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 }; |
| 1504 | static const __be32 dummy_pass[] = { [0 ... MAX_PHANDLE_ARGS] = 0 }; |
| 1505 | __be32 initial_match_array[MAX_PHANDLE_ARGS]; |
| 1506 | const __be32 *match_array = initial_match_array; |
| 1507 | int i, ret, map_len, match; |
| 1508 | u32 list_size, new_size; |
| 1509 | |
| 1510 | if (index < 0) |
| 1511 | return -EINVAL; |
| 1512 | |
| 1513 | cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name); |
| 1514 | if (!cells_name) |
| 1515 | return -ENOMEM; |
| 1516 | |
| 1517 | ret = -ENOMEM; |
| 1518 | map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name); |
| 1519 | if (!map_name) |
| 1520 | goto free; |
| 1521 | |
| 1522 | mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name); |
| 1523 | if (!mask_name) |
| 1524 | goto free; |
| 1525 | |
| 1526 | pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name); |
| 1527 | if (!pass_name) |
| 1528 | goto free; |
| 1529 | |
| 1530 | ret = __of_parse_phandle_with_args(np, list_name, cells_name, 0, index, |
| 1531 | out_args); |
| 1532 | if (ret) |
| 1533 | goto free; |
| 1534 | |
| 1535 | /* Get the #<list>-cells property */ |
| 1536 | cur = out_args->np; |
| 1537 | ret = of_property_read_u32(cur, cells_name, &list_size); |
| 1538 | if (ret < 0) |
| 1539 | goto put; |
| 1540 | |
| 1541 | /* Precalculate the match array - this simplifies match loop */ |
| 1542 | for (i = 0; i < list_size; i++) |
| 1543 | initial_match_array[i] = cpu_to_be32(out_args->args[i]); |
| 1544 | |
| 1545 | ret = -EINVAL; |
| 1546 | while (cur) { |
| 1547 | /* Get the <list>-map property */ |
| 1548 | map = of_get_property(cur, map_name, &map_len); |
| 1549 | if (!map) { |
| 1550 | ret = 0; |
| 1551 | goto free; |
| 1552 | } |
| 1553 | map_len /= sizeof(u32); |
| 1554 | |
| 1555 | /* Get the <list>-map-mask property (optional) */ |
| 1556 | mask = of_get_property(cur, mask_name, NULL); |
| 1557 | if (!mask) |
| 1558 | mask = dummy_mask; |
| 1559 | /* Iterate through <list>-map property */ |
| 1560 | match = 0; |
| 1561 | while (map_len > (list_size + 1) && !match) { |
| 1562 | /* Compare specifiers */ |
| 1563 | match = 1; |
| 1564 | for (i = 0; i < list_size; i++, map_len--) |
| 1565 | match &= !((match_array[i] ^ *map++) & mask[i]); |
| 1566 | |
| 1567 | of_node_put(new); |
| 1568 | new = of_find_node_by_phandle(be32_to_cpup(map)); |
| 1569 | map++; |
| 1570 | map_len--; |
| 1571 | |
| 1572 | /* Check if not found */ |
| 1573 | if (!new) |
| 1574 | goto put; |
| 1575 | |
| 1576 | if (!of_device_is_available(new)) |
| 1577 | match = 0; |
| 1578 | |
| 1579 | ret = of_property_read_u32(new, cells_name, &new_size); |
| 1580 | if (ret) |
| 1581 | goto put; |
| 1582 | |
| 1583 | /* Check for malformed properties */ |
| 1584 | if (WARN_ON(new_size > MAX_PHANDLE_ARGS)) |
| 1585 | goto put; |
| 1586 | if (map_len < new_size) |
| 1587 | goto put; |
| 1588 | |
| 1589 | /* Move forward by new node's #<list>-cells amount */ |
| 1590 | map += new_size; |
| 1591 | map_len -= new_size; |
| 1592 | } |
| 1593 | if (!match) |
| 1594 | goto put; |
| 1595 | |
| 1596 | /* Get the <list>-map-pass-thru property (optional) */ |
| 1597 | pass = of_get_property(cur, pass_name, NULL); |
| 1598 | if (!pass) |
| 1599 | pass = dummy_pass; |
| 1600 | |
| 1601 | /* |
| 1602 | * Successfully parsed a <list>-map translation; copy new |
| 1603 | * specifier into the out_args structure, keeping the |
| 1604 | * bits specified in <list>-map-pass-thru. |
| 1605 | */ |
| 1606 | match_array = map - new_size; |
| 1607 | for (i = 0; i < new_size; i++) { |
| 1608 | __be32 val = *(map - new_size + i); |
| 1609 | |
| 1610 | if (i < list_size) { |
| 1611 | val &= ~pass[i]; |
| 1612 | val |= cpu_to_be32(out_args->args[i]) & pass[i]; |
| 1613 | } |
| 1614 | |
| 1615 | out_args->args[i] = be32_to_cpu(val); |
| 1616 | } |
| 1617 | out_args->args_count = list_size = new_size; |
| 1618 | /* Iterate again with new provider */ |
| 1619 | out_args->np = new; |
| 1620 | of_node_put(cur); |
| 1621 | cur = new; |
| 1622 | } |
| 1623 | put: |
| 1624 | of_node_put(cur); |
| 1625 | of_node_put(new); |
| 1626 | free: |
| 1627 | kfree(mask_name); |
| 1628 | kfree(map_name); |
| 1629 | kfree(cells_name); |
| 1630 | kfree(pass_name); |
| 1631 | |
| 1632 | return ret; |
| 1633 | } |
| 1634 | EXPORT_SYMBOL(of_parse_phandle_with_args_map); |
| 1635 | |
| 1636 | /** |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1637 | * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list |
| 1638 | * @np: pointer to a device tree node containing a list |
| 1639 | * @list_name: property name that contains a list |
| 1640 | * @cell_count: number of argument cells following the phandle |
| 1641 | * @index: index of a phandle to parse out |
| 1642 | * @out_args: optional pointer to output arguments structure (will be filled) |
| 1643 | * |
| 1644 | * This function is useful to parse lists of phandles and their arguments. |
| 1645 | * Returns 0 on success and fills out_args, on error returns appropriate |
| 1646 | * errno value. |
| 1647 | * |
Geert Uytterhoeven | d94a75c | 2014-10-22 11:44:52 +0200 | [diff] [blame] | 1648 | * Caller is responsible to call of_node_put() on the returned out_args->np |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1649 | * pointer. |
| 1650 | * |
| 1651 | * Example: |
| 1652 | * |
| 1653 | * phandle1: node1 { |
| 1654 | * } |
| 1655 | * |
| 1656 | * phandle2: node2 { |
| 1657 | * } |
| 1658 | * |
| 1659 | * node3 { |
Geert Uytterhoeven | c0e848d | 2014-10-22 11:44:55 +0200 | [diff] [blame] | 1660 | * list = <&phandle1 0 2 &phandle2 2 3>; |
Stephen Warren | 035fd94 | 2013-08-14 15:27:10 -0600 | [diff] [blame] | 1661 | * } |
| 1662 | * |
| 1663 | * To get a device_node of the `node2' node you may call this: |
| 1664 | * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args); |
| 1665 | */ |
| 1666 | int of_parse_phandle_with_fixed_args(const struct device_node *np, |
| 1667 | const char *list_name, int cell_count, |
| 1668 | int index, struct of_phandle_args *out_args) |
| 1669 | { |
| 1670 | if (index < 0) |
| 1671 | return -EINVAL; |
| 1672 | return __of_parse_phandle_with_args(np, list_name, NULL, cell_count, |
| 1673 | index, out_args); |
| 1674 | } |
| 1675 | EXPORT_SYMBOL(of_parse_phandle_with_fixed_args); |
| 1676 | |
| 1677 | /** |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1678 | * of_count_phandle_with_args() - Find the number of phandles references in a property |
| 1679 | * @np: pointer to a device tree node containing a list |
| 1680 | * @list_name: property name that contains a list |
| 1681 | * @cells_name: property name that specifies phandles' arguments count |
| 1682 | * |
| 1683 | * Returns the number of phandle + argument tuples within a property. It |
| 1684 | * is a typical pattern to encode a list of phandle and variable |
| 1685 | * arguments into a single property. The number of arguments is encoded |
| 1686 | * by a property in the phandle-target node. For example, a gpios |
| 1687 | * property would contain a list of GPIO specifies consisting of a |
| 1688 | * phandle and 1 or more arguments. The number of arguments are |
| 1689 | * determined by the #gpio-cells property in the node pointed to by the |
| 1690 | * phandle. |
| 1691 | */ |
| 1692 | int of_count_phandle_with_args(const struct device_node *np, const char *list_name, |
| 1693 | const char *cells_name) |
| 1694 | { |
Joerg Roedel | 2021bd0 | 2016-04-04 17:49:19 +0200 | [diff] [blame] | 1695 | struct of_phandle_iterator it; |
| 1696 | int rc, cur_index = 0; |
| 1697 | |
| 1698 | rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0); |
| 1699 | if (rc) |
| 1700 | return rc; |
| 1701 | |
| 1702 | while ((rc = of_phandle_iterator_next(&it)) == 0) |
| 1703 | cur_index += 1; |
| 1704 | |
| 1705 | if (rc != -ENOENT) |
| 1706 | return rc; |
| 1707 | |
| 1708 | return cur_index; |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 1709 | } |
| 1710 | EXPORT_SYMBOL(of_count_phandle_with_args); |
| 1711 | |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1712 | /** |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1713 | * __of_add_property - Add a property to a node without lock operations |
| 1714 | */ |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1715 | int __of_add_property(struct device_node *np, struct property *prop) |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1716 | { |
| 1717 | struct property **next; |
| 1718 | |
| 1719 | prop->next = NULL; |
| 1720 | next = &np->properties; |
| 1721 | while (*next) { |
| 1722 | if (strcmp(prop->name, (*next)->name) == 0) |
| 1723 | /* duplicate ! don't insert it */ |
| 1724 | return -EEXIST; |
| 1725 | |
| 1726 | next = &(*next)->next; |
| 1727 | } |
| 1728 | *next = prop; |
| 1729 | |
| 1730 | return 0; |
| 1731 | } |
| 1732 | |
| 1733 | /** |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1734 | * of_add_property - Add a property to a node |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1735 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1736 | int of_add_property(struct device_node *np, struct property *prop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1737 | { |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1738 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1739 | int rc; |
| 1740 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1741 | mutex_lock(&of_mutex); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1742 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1743 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1744 | rc = __of_add_property(np, prop); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1745 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1746 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1747 | if (!rc) |
Pantelis Antoniou | 0829f6d | 2013-12-13 20:08:59 +0200 | [diff] [blame] | 1748 | __of_add_property_sysfs(np, prop); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1749 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1750 | mutex_unlock(&of_mutex); |
| 1751 | |
Grant Likely | 259092a | 2014-07-16 12:48:23 -0600 | [diff] [blame] | 1752 | if (!rc) |
| 1753 | of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL); |
| 1754 | |
Xiubo Li | 62664f6 | 2014-01-22 13:57:39 +0800 | [diff] [blame] | 1755 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1758 | int __of_remove_property(struct device_node *np, struct property *prop) |
| 1759 | { |
| 1760 | struct property **next; |
| 1761 | |
| 1762 | for (next = &np->properties; *next; next = &(*next)->next) { |
| 1763 | if (*next == prop) |
| 1764 | break; |
| 1765 | } |
| 1766 | if (*next == NULL) |
| 1767 | return -ENODEV; |
| 1768 | |
| 1769 | /* found the node */ |
| 1770 | *next = prop->next; |
| 1771 | prop->next = np->deadprops; |
| 1772 | np->deadprops = prop; |
| 1773 | |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1777 | /** |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1778 | * of_remove_property - Remove a property from a node. |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1779 | * |
| 1780 | * Note that we don't actually remove it, since we have given out |
| 1781 | * who-knows-how-many pointers to the data using get-property. |
| 1782 | * Instead we just move the property to the "dead properties" |
| 1783 | * list, so it won't be found any more. |
| 1784 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1785 | int of_remove_property(struct device_node *np, struct property *prop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1786 | { |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1787 | unsigned long flags; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1788 | int rc; |
| 1789 | |
Suraj Jitindar Singh | 201b3fe | 2016-04-28 15:34:54 +1000 | [diff] [blame] | 1790 | if (!prop) |
| 1791 | return -ENODEV; |
| 1792 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1793 | mutex_lock(&of_mutex); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1794 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1795 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1796 | rc = __of_remove_property(np, prop); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1797 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1798 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1799 | if (!rc) |
| 1800 | __of_remove_property_sysfs(np, prop); |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1801 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1802 | mutex_unlock(&of_mutex); |
Grant Likely | 75b57ec | 2014-02-20 18:02:11 +0000 | [diff] [blame] | 1803 | |
Grant Likely | 259092a | 2014-07-16 12:48:23 -0600 | [diff] [blame] | 1804 | if (!rc) |
| 1805 | of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL); |
| 1806 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1807 | return rc; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1810 | int __of_update_property(struct device_node *np, struct property *newprop, |
| 1811 | struct property **oldpropp) |
| 1812 | { |
| 1813 | struct property **next, *oldprop; |
| 1814 | |
| 1815 | for (next = &np->properties; *next; next = &(*next)->next) { |
| 1816 | if (of_prop_cmp((*next)->name, newprop->name) == 0) |
| 1817 | break; |
| 1818 | } |
| 1819 | *oldpropp = oldprop = *next; |
| 1820 | |
| 1821 | if (oldprop) { |
| 1822 | /* replace the node */ |
| 1823 | newprop->next = oldprop->next; |
| 1824 | *next = newprop; |
| 1825 | oldprop->next = np->deadprops; |
| 1826 | np->deadprops = oldprop; |
| 1827 | } else { |
| 1828 | /* new node */ |
| 1829 | newprop->next = NULL; |
| 1830 | *next = newprop; |
| 1831 | } |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1832 | |
| 1833 | return 0; |
| 1834 | } |
| 1835 | |
| 1836 | /* |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1837 | * of_update_property - Update a property in a node, if the property does |
Dong Aisheng | 475d009 | 2012-07-11 15:16:37 +1000 | [diff] [blame] | 1838 | * not exist, add it. |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1839 | * |
| 1840 | * Note that we don't actually remove it, since we have given out |
| 1841 | * who-knows-how-many pointers to the data using get-property. |
| 1842 | * Instead we just move the property to the "dead properties" list, |
| 1843 | * and add the new property to the property list |
| 1844 | */ |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 1845 | int of_update_property(struct device_node *np, struct property *newprop) |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1846 | { |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1847 | struct property *oldprop; |
Grant Likely | 02af11b | 2009-11-23 20:16:45 -0700 | [diff] [blame] | 1848 | unsigned long flags; |
Xiubo Li | 947fdaad0 | 2014-04-17 15:48:29 +0800 | [diff] [blame] | 1849 | int rc; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1850 | |
Dong Aisheng | 475d009 | 2012-07-11 15:16:37 +1000 | [diff] [blame] | 1851 | if (!newprop->name) |
| 1852 | return -EINVAL; |
| 1853 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1854 | mutex_lock(&of_mutex); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1855 | |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1856 | raw_spin_lock_irqsave(&devtree_lock, flags); |
Pantelis Antoniou | d8c5008 | 2014-07-04 19:58:46 +0300 | [diff] [blame] | 1857 | rc = __of_update_property(np, newprop, &oldprop); |
Thomas Gleixner | d6d3c4e | 2013-02-06 15:30:56 -0500 | [diff] [blame] | 1858 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
Nathan Fontenot | e81b329 | 2012-10-02 16:55:01 +0000 | [diff] [blame] | 1859 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1860 | if (!rc) |
| 1861 | __of_update_property_sysfs(np, newprop, oldprop); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1862 | |
Grant Likely | 8a2b22a2 | 2014-07-23 17:05:06 -0600 | [diff] [blame] | 1863 | mutex_unlock(&of_mutex); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1864 | |
Grant Likely | 259092a | 2014-07-16 12:48:23 -0600 | [diff] [blame] | 1865 | if (!rc) |
| 1866 | of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop); |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1867 | |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1868 | return rc; |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1869 | } |
Grant Likely | fcdeb7f | 2010-01-29 05:04:33 -0700 | [diff] [blame] | 1870 | |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1871 | static void of_alias_add(struct alias_prop *ap, struct device_node *np, |
| 1872 | int id, const char *stem, int stem_len) |
| 1873 | { |
| 1874 | ap->np = np; |
| 1875 | ap->id = id; |
| 1876 | strncpy(ap->stem, stem, stem_len); |
| 1877 | ap->stem[stem_len] = 0; |
| 1878 | list_add_tail(&ap->link, &aliases_lookup); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1879 | pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n", |
| 1880 | ap->alias, ap->stem, ap->id, np); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | /** |
Geert Uytterhoeven | 1821dda | 2014-10-22 11:44:53 +0200 | [diff] [blame] | 1884 | * of_alias_scan - Scan all properties of the 'aliases' node |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1885 | * |
Geert Uytterhoeven | 1821dda | 2014-10-22 11:44:53 +0200 | [diff] [blame] | 1886 | * The function scans all the properties of the 'aliases' node and populates |
| 1887 | * the global lookup table with the properties. It returns the |
| 1888 | * number of alias properties found, or an error code in case of failure. |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1889 | * |
| 1890 | * @dt_alloc: An allocator that provides a virtual address to memory |
Geert Uytterhoeven | 1821dda | 2014-10-22 11:44:53 +0200 | [diff] [blame] | 1891 | * for storing the resulting tree |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1892 | */ |
| 1893 | void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)) |
| 1894 | { |
| 1895 | struct property *pp; |
| 1896 | |
Laurentiu Tudor | 7dbe584 | 2014-08-27 17:09:39 +0300 | [diff] [blame] | 1897 | of_aliases = of_find_node_by_path("/aliases"); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1898 | of_chosen = of_find_node_by_path("/chosen"); |
| 1899 | if (of_chosen == NULL) |
| 1900 | of_chosen = of_find_node_by_path("/chosen@0"); |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 1901 | |
| 1902 | if (of_chosen) { |
Grant Likely | a752ee5 | 2014-03-28 08:12:18 -0700 | [diff] [blame] | 1903 | /* linux,stdout-path and /aliases/stdout are for legacy compatibility */ |
Sergei Shtylyov | b0d9d92 | 2017-07-23 19:55:48 +0300 | [diff] [blame] | 1904 | const char *name = NULL; |
| 1905 | |
| 1906 | if (of_property_read_string(of_chosen, "stdout-path", &name)) |
| 1907 | of_property_read_string(of_chosen, "linux,stdout-path", |
| 1908 | &name); |
Grant Likely | a752ee5 | 2014-03-28 08:12:18 -0700 | [diff] [blame] | 1909 | if (IS_ENABLED(CONFIG_PPC) && !name) |
Sergei Shtylyov | b0d9d92 | 2017-07-23 19:55:48 +0300 | [diff] [blame] | 1910 | of_property_read_string(of_aliases, "stdout", &name); |
Peter Hurley | f64255b | 2015-03-17 16:46:33 -0400 | [diff] [blame] | 1911 | if (name) |
Leif Lindholm | 7914a7c | 2014-11-27 17:56:07 +0000 | [diff] [blame] | 1912 | of_stdout = of_find_node_opts_by_path(name, &of_stdout_options); |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 1913 | } |
| 1914 | |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1915 | if (!of_aliases) |
| 1916 | return; |
| 1917 | |
Dong Aisheng | 8af0da9 | 2011-12-22 20:19:24 +0800 | [diff] [blame] | 1918 | for_each_property_of_node(of_aliases, pp) { |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1919 | const char *start = pp->name; |
| 1920 | const char *end = start + strlen(start); |
| 1921 | struct device_node *np; |
| 1922 | struct alias_prop *ap; |
| 1923 | int id, len; |
| 1924 | |
| 1925 | /* Skip those we do not want to proceed */ |
| 1926 | if (!strcmp(pp->name, "name") || |
| 1927 | !strcmp(pp->name, "phandle") || |
| 1928 | !strcmp(pp->name, "linux,phandle")) |
| 1929 | continue; |
| 1930 | |
| 1931 | np = of_find_node_by_path(pp->value); |
| 1932 | if (!np) |
| 1933 | continue; |
| 1934 | |
| 1935 | /* walk the alias backwards to extract the id and work out |
| 1936 | * the 'stem' string */ |
| 1937 | while (isdigit(*(end-1)) && end > start) |
| 1938 | end--; |
| 1939 | len = end - start; |
| 1940 | |
| 1941 | if (kstrtoint(end, 10, &id) < 0) |
| 1942 | continue; |
| 1943 | |
| 1944 | /* Allocate an alias_prop with enough space for the stem */ |
Paul Burton | de96ec2 | 2016-09-23 16:38:27 +0100 | [diff] [blame] | 1945 | ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap)); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1946 | if (!ap) |
| 1947 | continue; |
Grant Likely | 0640332e | 2013-08-28 21:24:17 +0100 | [diff] [blame] | 1948 | memset(ap, 0, sizeof(*ap) + len + 1); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1949 | ap->alias = start; |
| 1950 | of_alias_add(ap, np, id, start, len); |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | /** |
| 1955 | * of_alias_get_id - Get alias id for the given device_node |
| 1956 | * @np: Pointer to the given device_node |
| 1957 | * @stem: Alias stem of the given device_node |
| 1958 | * |
Geert Uytterhoeven | 5a53a07 | 2014-03-11 11:23:38 +0100 | [diff] [blame] | 1959 | * The function travels the lookup table to get the alias id for the given |
| 1960 | * device_node and alias stem. It returns the alias id if found. |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1961 | */ |
| 1962 | int of_alias_get_id(struct device_node *np, const char *stem) |
| 1963 | { |
| 1964 | struct alias_prop *app; |
| 1965 | int id = -ENODEV; |
| 1966 | |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 1967 | mutex_lock(&of_mutex); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1968 | list_for_each_entry(app, &aliases_lookup, link) { |
| 1969 | if (strcmp(app->stem, stem) != 0) |
| 1970 | continue; |
| 1971 | |
| 1972 | if (np == app->np) { |
| 1973 | id = app->id; |
| 1974 | break; |
| 1975 | } |
| 1976 | } |
Pantelis Antoniou | c05aba2 | 2014-07-04 19:58:03 +0300 | [diff] [blame] | 1977 | mutex_unlock(&of_mutex); |
Shawn Guo | 611cad7 | 2011-08-15 15:28:14 +0800 | [diff] [blame] | 1978 | |
| 1979 | return id; |
| 1980 | } |
| 1981 | EXPORT_SYMBOL_GPL(of_alias_get_id); |
Stephen Warren | c541adc | 2012-04-04 09:27:46 -0600 | [diff] [blame] | 1982 | |
Wolfram Sang | 351d224 | 2015-03-12 17:17:58 +0100 | [diff] [blame] | 1983 | /** |
| 1984 | * of_alias_get_highest_id - Get highest alias id for the given stem |
| 1985 | * @stem: Alias stem to be examined |
| 1986 | * |
| 1987 | * The function travels the lookup table to get the highest alias id for the |
| 1988 | * given alias stem. It returns the alias id if found. |
| 1989 | */ |
| 1990 | int of_alias_get_highest_id(const char *stem) |
| 1991 | { |
| 1992 | struct alias_prop *app; |
| 1993 | int id = -ENODEV; |
| 1994 | |
| 1995 | mutex_lock(&of_mutex); |
| 1996 | list_for_each_entry(app, &aliases_lookup, link) { |
| 1997 | if (strcmp(app->stem, stem) != 0) |
| 1998 | continue; |
| 1999 | |
| 2000 | if (app->id > id) |
| 2001 | id = app->id; |
| 2002 | } |
| 2003 | mutex_unlock(&of_mutex); |
| 2004 | |
| 2005 | return id; |
| 2006 | } |
| 2007 | EXPORT_SYMBOL_GPL(of_alias_get_highest_id); |
| 2008 | |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2009 | /** |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2010 | * of_console_check() - Test and setup console for DT setup |
| 2011 | * @dn - Pointer to device node |
| 2012 | * @name - Name to use for preferred console without index. ex. "ttyS" |
| 2013 | * @index - Index to use for preferred console. |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2014 | * |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2015 | * Check if the given device node matches the stdout-path property in the |
| 2016 | * /chosen node. If it does then register it as the preferred console and return |
| 2017 | * TRUE. Otherwise return FALSE. |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2018 | */ |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2019 | bool of_console_check(struct device_node *dn, char *name, int index) |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2020 | { |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2021 | if (!dn || dn != of_stdout || console_set_on_cmdline) |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2022 | return false; |
Sergey Senozhatsky | db179e0 | 2017-09-26 15:25:10 +0900 | [diff] [blame] | 2023 | |
| 2024 | /* |
| 2025 | * XXX: cast `options' to char pointer to suppress complication |
| 2026 | * warnings: printk, UART and console drivers expect char pointer. |
| 2027 | */ |
| 2028 | return !add_preferred_console(name, index, (char *)of_stdout_options); |
Sascha Hauer | 5c19e95 | 2013-08-05 14:40:44 +0200 | [diff] [blame] | 2029 | } |
Grant Likely | 3482f2c | 2014-03-27 17:18:55 -0700 | [diff] [blame] | 2030 | EXPORT_SYMBOL_GPL(of_console_check); |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 2031 | |
| 2032 | /** |
| 2033 | * of_find_next_cache_node - Find a node's subsidiary cache |
| 2034 | * @np: node of type "cpu" or "cache" |
| 2035 | * |
| 2036 | * Returns a node pointer with refcount incremented, use |
| 2037 | * of_node_put() on it when done. Caller should hold a reference |
| 2038 | * to np. |
| 2039 | */ |
| 2040 | struct device_node *of_find_next_cache_node(const struct device_node *np) |
| 2041 | { |
Rob Herring | 91d9674 | 2017-05-04 12:30:07 -0500 | [diff] [blame] | 2042 | struct device_node *child, *cache_node; |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 2043 | |
Rob Herring | 91d9674 | 2017-05-04 12:30:07 -0500 | [diff] [blame] | 2044 | cache_node = of_parse_phandle(np, "l2-cache", 0); |
| 2045 | if (!cache_node) |
| 2046 | cache_node = of_parse_phandle(np, "next-level-cache", 0); |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 2047 | |
Rob Herring | 91d9674 | 2017-05-04 12:30:07 -0500 | [diff] [blame] | 2048 | if (cache_node) |
| 2049 | return cache_node; |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 2050 | |
| 2051 | /* OF on pmac has nodes instead of properties named "l2-cache" |
| 2052 | * beneath CPU nodes. |
| 2053 | */ |
Rob Herring | f6707fd | 2018-08-27 09:50:09 -0500 | [diff] [blame] | 2054 | if (IS_ENABLED(CONFIG_PPC_PMAC) && !strcmp(np->type, "cpu")) |
Sudeep KarkadaNagesha | a3e31b4 | 2013-09-18 11:53:05 +0100 | [diff] [blame] | 2055 | for_each_child_of_node(np, child) |
| 2056 | if (!strcmp(child->type, "cache")) |
| 2057 | return child; |
| 2058 | |
| 2059 | return NULL; |
| 2060 | } |
Philipp Zabel | fd9fdb7 | 2014-02-10 22:01:48 +0100 | [diff] [blame] | 2061 | |
| 2062 | /** |
Sudeep Holla | 5fa2353 | 2017-01-16 10:40:43 +0000 | [diff] [blame] | 2063 | * of_find_last_cache_level - Find the level at which the last cache is |
| 2064 | * present for the given logical cpu |
| 2065 | * |
| 2066 | * @cpu: cpu number(logical index) for which the last cache level is needed |
| 2067 | * |
| 2068 | * Returns the the level at which the last cache is present. It is exactly |
| 2069 | * same as the total number of cache levels for the given logical cpu. |
| 2070 | */ |
| 2071 | int of_find_last_cache_level(unsigned int cpu) |
| 2072 | { |
| 2073 | u32 cache_level = 0; |
| 2074 | struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu); |
| 2075 | |
| 2076 | while (np) { |
| 2077 | prev = np; |
| 2078 | of_node_put(np); |
| 2079 | np = of_find_next_cache_node(np); |
| 2080 | } |
| 2081 | |
| 2082 | of_property_read_u32(prev, "cache-level", &cache_level); |
| 2083 | |
| 2084 | return cache_level; |
| 2085 | } |