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