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