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