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