blob: 74eaedd5b860f1c1fd42faa187d5cda802d0ae8e [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0+
Stephen Rothwell97e873e2007-05-01 16:26:07 +10002/*
3 * Procedures for creating, accessing and interpreting the device tree.
4 *
5 * Paul Mackerras August 1996.
6 * Copyright (C) 1996-2005 Paul Mackerras.
7 *
8 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
9 * {engebret|bergner}@us.ibm.com
10 *
11 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
12 *
Grant Likelye91edcf2009-10-15 10:58:09 -060013 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
14 * Grant Likely.
Stephen Rothwell97e873e2007-05-01 16:26:07 +100015 */
Rob Herring606ad422016-06-15 08:32:18 -050016
17#define pr_fmt(fmt) "OF: " fmt
18
Grant Likely3482f2c2014-03-27 17:18:55 -070019#include <linux/console.h>
Shawn Guo611cad72011-08-15 15:28:14 +080020#include <linux/ctype.h>
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +010021#include <linux/cpu.h>
Stephen Rothwell97e873e2007-05-01 16:26:07 +100022#include <linux/module.h>
23#include <linux/of.h>
Sudeep Holla5fa23532017-01-16 10:40:43 +000024#include <linux/of_device.h>
Philipp Zabelfd9fdb72014-02-10 22:01:48 +010025#include <linux/of_graph.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100026#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Grant Likely75b57ec2014-02-20 18:02:11 +000028#include <linux/string.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070029#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100030
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080031#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080032
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080033LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080034
Grant Likely5063e252014-10-03 16:28:27 +010035struct device_node *of_root;
36EXPORT_SYMBOL(of_root);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070037struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080038struct device_node *of_aliases;
Grant Likelya752ee52014-03-28 08:12:18 -070039struct device_node *of_stdout;
Leif Lindholm7914a7c2014-11-27 17:56:07 +000040static const char *of_stdout_options;
Shawn Guo611cad72011-08-15 15:28:14 +080041
Grant Likely8a2b22a22014-07-23 17:05:06 -060042struct kset *of_kset;
Grant Likely75b57ec2014-02-20 18:02:11 +000043
44/*
Grant Likely8a2b22a22014-07-23 17:05:06 -060045 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
46 * This mutex must be held whenever modifications are being made to the
47 * device tree. The of_{attach,detach}_node() and
48 * of_{add,remove,update}_property() helpers make sure this happens.
Grant Likely75b57ec2014-02-20 18:02:11 +000049 */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +030050DEFINE_MUTEX(of_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100051
Grant Likely5063e252014-10-03 16:28:27 +010052/* use when traversing tree through the child, sibling,
Stephen Rothwell581b6052007-04-24 16:46:53 +100053 * or parent members of struct device_node.
54 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050055DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100056
Rob Herringf42b0e182018-08-27 07:50:47 -050057bool of_node_name_eq(const struct device_node *np, const char *name)
58{
59 const char *node_name;
60 size_t len;
61
62 if (!np)
63 return false;
64
65 node_name = kbasename(np->full_name);
66 len = strchrnul(node_name, '@') - node_name;
67
68 return (strlen(name) == len) && (strncmp(node_name, name, len) == 0);
69}
70
71bool of_node_name_prefix(const struct device_node *np, const char *prefix)
72{
73 if (!np)
74 return false;
75
76 return strncmp(kbasename(np->full_name), prefix, strlen(prefix)) == 0;
77}
78
Stephen Rothwell97e873e2007-05-01 16:26:07 +100079int of_n_addr_cells(struct device_node *np)
80{
Sergei Shtylyov88329632017-07-23 19:55:47 +030081 u32 cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100082
83 do {
84 if (np->parent)
85 np = np->parent;
Sergei Shtylyov88329632017-07-23 19:55:47 +030086 if (!of_property_read_u32(np, "#address-cells", &cells))
87 return cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100088 } while (np->parent);
89 /* No #address-cells property for the root node */
90 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
91}
92EXPORT_SYMBOL(of_n_addr_cells);
93
94int of_n_size_cells(struct device_node *np)
95{
Sergei Shtylyov88329632017-07-23 19:55:47 +030096 u32 cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100097
98 do {
99 if (np->parent)
100 np = np->parent;
Sergei Shtylyov88329632017-07-23 19:55:47 +0300101 if (!of_property_read_u32(np, "#size-cells", &cells))
102 return cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000103 } while (np->parent);
104 /* No #size-cells property for the root node */
105 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
106}
107EXPORT_SYMBOL(of_n_size_cells);
108
Rob Herring0c3f0612013-09-17 10:42:50 -0500109#ifdef CONFIG_NUMA
110int __weak of_node_to_nid(struct device_node *np)
111{
Konstantin Khlebnikovc8fff7b2015-04-08 19:59:20 +0300112 return NUMA_NO_NODE;
Rob Herring0c3f0612013-09-17 10:42:50 -0500113}
114#endif
115
Frank Rowand0b3ce782018-03-04 16:14:47 -0800116static struct device_node **phandle_cache;
117static u32 phandle_cache_mask;
118
119/*
120 * Assumptions behind phandle_cache implementation:
121 * - phandle property values are in a contiguous range of 1..n
122 *
123 * If the assumptions do not hold, then
124 * - the phandle lookup overhead reduction provided by the cache
125 * will likely be less
126 */
Frank Rowandb9952b52018-07-12 14:00:07 -0700127void of_populate_phandle_cache(void)
Frank Rowand0b3ce782018-03-04 16:14:47 -0800128{
129 unsigned long flags;
130 u32 cache_entries;
131 struct device_node *np;
132 u32 phandles = 0;
133
134 raw_spin_lock_irqsave(&devtree_lock, flags);
135
136 kfree(phandle_cache);
137 phandle_cache = NULL;
138
139 for_each_of_allnodes(np)
140 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
141 phandles++;
142
Rob Herringe54192b2018-09-11 09:28:14 -0500143 if (!phandles)
144 goto out;
145
Frank Rowand0b3ce782018-03-04 16:14:47 -0800146 cache_entries = roundup_pow_of_two(phandles);
147 phandle_cache_mask = cache_entries - 1;
148
149 phandle_cache = kcalloc(cache_entries, sizeof(*phandle_cache),
150 GFP_ATOMIC);
151 if (!phandle_cache)
152 goto out;
153
154 for_each_of_allnodes(np)
155 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
156 phandle_cache[np->phandle & phandle_cache_mask] = np;
157
158out:
159 raw_spin_unlock_irqrestore(&devtree_lock, flags);
160}
161
Frank Rowandb9952b52018-07-12 14:00:07 -0700162int of_free_phandle_cache(void)
Frank Rowand0b3ce782018-03-04 16:14:47 -0800163{
164 unsigned long flags;
165
166 raw_spin_lock_irqsave(&devtree_lock, flags);
167
168 kfree(phandle_cache);
169 phandle_cache = NULL;
170
171 raw_spin_unlock_irqrestore(&devtree_lock, flags);
172
173 return 0;
174}
Frank Rowandb9952b52018-07-12 14:00:07 -0700175#if !defined(CONFIG_MODULES)
Frank Rowand0b3ce782018-03-04 16:14:47 -0800176late_initcall_sync(of_free_phandle_cache);
177#endif
178
Sudeep Holla194ec932015-05-14 15:28:24 +0100179void __init of_core_init(void)
Grant Likely75b57ec2014-02-20 18:02:11 +0000180{
181 struct device_node *np;
182
Frank Rowand0b3ce782018-03-04 16:14:47 -0800183 of_populate_phandle_cache();
184
Grant Likely75b57ec2014-02-20 18:02:11 +0000185 /* Create the kset, and register existing nodes */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300186 mutex_lock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000187 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
188 if (!of_kset) {
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300189 mutex_unlock(&of_mutex);
Rob Herring606ad422016-06-15 08:32:18 -0500190 pr_err("failed to register existing nodes\n");
Sudeep Holla194ec932015-05-14 15:28:24 +0100191 return;
Grant Likely75b57ec2014-02-20 18:02:11 +0000192 }
193 for_each_of_allnodes(np)
Grant Likely8a2b22a22014-07-23 17:05:06 -0600194 __of_attach_node_sysfs(np);
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300195 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000196
Grant Likely83570412012-11-06 21:03:27 +0000197 /* Symlink in /proc as required by userspace ABI */
Grant Likely5063e252014-10-03 16:28:27 +0100198 if (of_root)
Grant Likely75b57ec2014-02-20 18:02:11 +0000199 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000200}
Grant Likely75b57ec2014-02-20 18:02:11 +0000201
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500202static struct property *__of_find_property(const struct device_node *np,
203 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000204{
205 struct property *pp;
206
Timur Tabi64e45662008-05-08 05:19:59 +1000207 if (!np)
208 return NULL;
209
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530210 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000211 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530212 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000213 *lenp = pp->length;
214 break;
215 }
216 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500217
218 return pp;
219}
220
221struct property *of_find_property(const struct device_node *np,
222 const char *name,
223 int *lenp)
224{
225 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500226 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500227
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500228 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500229 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500230 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000231
232 return pp;
233}
234EXPORT_SYMBOL(of_find_property);
235
Grant Likely5063e252014-10-03 16:28:27 +0100236struct device_node *__of_find_all_nodes(struct device_node *prev)
237{
238 struct device_node *np;
239 if (!prev) {
240 np = of_root;
241 } else if (prev->child) {
242 np = prev->child;
243 } else {
244 /* Walk back up looking for a sibling, or the end of the structure */
245 np = prev;
246 while (np->parent && !np->sibling)
247 np = np->parent;
248 np = np->sibling; /* Might be null at the end of the tree */
249 }
250 return np;
251}
252
Grant Likelye91edcf2009-10-15 10:58:09 -0600253/**
254 * of_find_all_nodes - Get next node in global list
255 * @prev: Previous node or NULL to start iteration
256 * of_node_put() will be called on it
257 *
258 * Returns a node pointer with refcount incremented, use
259 * of_node_put() on it when done.
260 */
261struct device_node *of_find_all_nodes(struct device_node *prev)
262{
263 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000264 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600265
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000266 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100267 np = __of_find_all_nodes(prev);
268 of_node_get(np);
Grant Likelye91edcf2009-10-15 10:58:09 -0600269 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000270 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600271 return np;
272}
273EXPORT_SYMBOL(of_find_all_nodes);
274
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000275/*
276 * Find a property with a given name for a given node
277 * and return the value.
278 */
Grant Likelya25095d2014-07-15 23:25:43 -0600279const void *__of_get_property(const struct device_node *np,
280 const char *name, int *lenp)
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500281{
282 struct property *pp = __of_find_property(np, name, lenp);
283
284 return pp ? pp->value : NULL;
285}
286
287/*
288 * Find a property with a given name for a given node
289 * and return the value.
290 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000291const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500292 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000293{
294 struct property *pp = of_find_property(np, name, lenp);
295
296 return pp ? pp->value : NULL;
297}
298EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000299
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100300/*
301 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
302 *
303 * @cpu: logical cpu index of a core/thread
304 * @phys_id: physical identifier of a core/thread
305 *
306 * CPU logical to physical index mapping is architecture specific.
307 * However this __weak function provides a default match of physical
308 * id to logical cpu index. phys_id provided here is usually values read
309 * from the device tree which must match the hardware internal registers.
310 *
311 * Returns true if the physical identifier and the logical cpu index
312 * correspond to the same core/thread, false otherwise.
313 */
314bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
315{
316 return (u32)phys_id == cpu;
317}
318
319/**
320 * Checks if the given "prop_name" property holds the physical id of the
321 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
322 * NULL, local thread number within the core is returned in it.
323 */
324static bool __of_find_n_match_cpu_property(struct device_node *cpun,
325 const char *prop_name, int cpu, unsigned int *thread)
326{
327 const __be32 *cell;
328 int ac, prop_len, tid;
329 u64 hwid;
330
331 ac = of_n_addr_cells(cpun);
332 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100333 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100334 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100335 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100336 for (tid = 0; tid < prop_len; tid++) {
337 hwid = of_read_number(cell, ac);
338 if (arch_match_cpu_phys_id(cpu, hwid)) {
339 if (thread)
340 *thread = tid;
341 return true;
342 }
343 cell += ac;
344 }
345 return false;
346}
347
David Millerd1cb9d12013-10-03 17:24:51 -0400348/*
349 * arch_find_n_match_cpu_physical_id - See if the given device node is
350 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
351 * else false. If 'thread' is non-NULL, the local thread number within the
352 * core is returned in it.
353 */
354bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
355 int cpu, unsigned int *thread)
356{
357 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
358 * for thread ids on PowerPC. If it doesn't exist fallback to
359 * standard "reg" property.
360 */
361 if (IS_ENABLED(CONFIG_PPC) &&
362 __of_find_n_match_cpu_property(cpun,
363 "ibm,ppc-interrupt-server#s",
364 cpu, thread))
365 return true;
366
Masahiro Yamada510bd062015-10-28 12:05:27 +0900367 return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
David Millerd1cb9d12013-10-03 17:24:51 -0400368}
369
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100370/**
371 * of_get_cpu_node - Get device node associated with the given logical CPU
372 *
373 * @cpu: CPU number(logical index) for which device node is required
374 * @thread: if not NULL, local thread number within the physical core is
375 * returned
376 *
377 * The main purpose of this function is to retrieve the device node for the
378 * given logical CPU index. It should be used to initialize the of_node in
379 * cpu device. Once of_node in cpu device is populated, all the further
380 * references can use that instead.
381 *
382 * CPU logical to physical index mapping is architecture specific and is built
383 * before booting secondary cores. This function uses arch_match_cpu_phys_id
384 * which can be overridden by architecture specific implementation.
385 *
Masahiro Yamada1c986e32016-04-20 10:18:46 +0900386 * Returns a node pointer for the logical cpu with refcount incremented, use
387 * of_node_put() on it when done. Returns NULL if not found.
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100388 */
389struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
390{
David Millerd1cb9d12013-10-03 17:24:51 -0400391 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100392
David Millerd1cb9d12013-10-03 17:24:51 -0400393 for_each_node_by_type(cpun, "cpu") {
394 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100395 return cpun;
396 }
397 return NULL;
398}
399EXPORT_SYMBOL(of_get_cpu_node);
400
Kevin Hao215a14c2014-02-19 16:15:45 +0800401/**
Suzuki K Poulosea0e71cd2018-01-02 11:25:27 +0000402 * of_cpu_node_to_id: Get the logical CPU number for a given device_node
403 *
404 * @cpu_node: Pointer to the device_node for CPU.
405 *
406 * Returns the logical CPU number of the given CPU device_node.
407 * Returns -ENODEV if the CPU is not found.
408 */
409int of_cpu_node_to_id(struct device_node *cpu_node)
410{
411 int cpu;
412 bool found = false;
413 struct device_node *np;
414
415 for_each_possible_cpu(cpu) {
416 np = of_cpu_device_node_get(cpu);
417 found = (cpu_node == np);
418 of_node_put(np);
419 if (found)
420 return cpu;
421 }
422
423 return -ENODEV;
424}
425EXPORT_SYMBOL(of_cpu_node_to_id);
426
427/**
Kevin Hao215a14c2014-02-19 16:15:45 +0800428 * __of_device_is_compatible() - Check if the node matches given constraints
429 * @device: pointer to node
430 * @compat: required compatible string, NULL or "" for any match
431 * @type: required device_type value, NULL or "" for any match
432 * @name: required node name, NULL or "" for any match
433 *
434 * Checks if the given @compat, @type and @name strings match the
435 * properties of the given @device. A constraints can be skipped by
436 * passing NULL or an empty string as the constraint.
437 *
438 * Returns 0 for no match, and a positive integer on match. The return
439 * value is a relative score with larger values indicating better
440 * matches. The score is weighted for the most specific compatible value
441 * to get the highest score. Matching type is next, followed by matching
442 * name. Practically speaking, this results in the following priority
443 * order for matches:
444 *
445 * 1. specific compatible && type && name
446 * 2. specific compatible && type
447 * 3. specific compatible && name
448 * 4. specific compatible
449 * 5. general compatible && type && name
450 * 6. general compatible && type
451 * 7. general compatible && name
452 * 8. general compatible
453 * 9. type && name
454 * 10. type
455 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000456 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500457static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800458 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000459{
Kevin Hao215a14c2014-02-19 16:15:45 +0800460 struct property *prop;
461 const char *cp;
462 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000463
Kevin Hao215a14c2014-02-19 16:15:45 +0800464 /* Compatible match has highest priority */
465 if (compat && compat[0]) {
466 prop = __of_find_property(device, "compatible", NULL);
467 for (cp = of_prop_next_string(prop, NULL); cp;
468 cp = of_prop_next_string(prop, cp), index++) {
469 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
470 score = INT_MAX/2 - (index << 2);
471 break;
472 }
473 }
474 if (!score)
475 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000476 }
477
Kevin Hao215a14c2014-02-19 16:15:45 +0800478 /* Matching type is better than matching name */
479 if (type && type[0]) {
480 if (!device->type || of_node_cmp(type, device->type))
481 return 0;
482 score += 2;
483 }
484
485 /* Matching name is a bit better than not */
486 if (name && name[0]) {
487 if (!device->name || of_node_cmp(name, device->name))
488 return 0;
489 score++;
490 }
491
492 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000493}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500494
495/** Checks if the given "compat" string matches one of the strings in
496 * the device's "compatible" property
497 */
498int of_device_is_compatible(const struct device_node *device,
499 const char *compat)
500{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500501 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500502 int res;
503
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500504 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800505 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500506 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500507 return res;
508}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000509EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000510
Benjamin Herrenschmidtb9c13fe32016-07-08 08:35:59 +1000511/** Checks if the device is compatible with any of the entries in
512 * a NULL terminated array of strings. Returns the best match
513 * score or 0.
514 */
515int of_device_compatible_match(struct device_node *device,
516 const char *const *compat)
517{
518 unsigned int tmp, score = 0;
519
520 if (!compat)
521 return 0;
522
523 while (*compat) {
524 tmp = of_device_is_compatible(device, *compat);
525 if (tmp > score)
526 score = tmp;
527 compat++;
528 }
529
530 return score;
531}
532
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000533/**
Grant Likely71a157e2010-02-01 21:34:14 -0700534 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700535 * @compat: compatible string to look for in root node's compatible property.
536 *
Kevin Cernekee25c7a1d2014-11-12 12:54:00 -0800537 * Returns a positive integer if the root node has the given value in its
Grant Likely1f43cfb2010-01-28 13:47:25 -0700538 * compatible property.
539 */
Grant Likely71a157e2010-02-01 21:34:14 -0700540int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700541{
542 struct device_node *root;
543 int rc = 0;
544
545 root = of_find_node_by_path("/");
546 if (root) {
547 rc = of_device_is_compatible(root, compat);
548 of_node_put(root);
549 }
550 return rc;
551}
Grant Likely71a157e2010-02-01 21:34:14 -0700552EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700553
554/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700555 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100556 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700557 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100558 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800559 * Returns true if the status property is absent or set to "okay" or "ok",
560 * false otherwise
Josh Boyer834d97d2008-03-27 00:33:14 +1100561 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800562static bool __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100563{
564 const char *status;
565 int statlen;
566
Xiubo Li42ccd782014-01-13 11:07:28 +0800567 if (!device)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800568 return false;
Xiubo Li42ccd782014-01-13 11:07:28 +0800569
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700570 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100571 if (status == NULL)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800572 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100573
574 if (statlen > 0) {
575 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800576 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100577 }
578
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800579 return false;
Josh Boyer834d97d2008-03-27 00:33:14 +1100580}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700581
582/**
583 * of_device_is_available - check if a device is available for use
584 *
585 * @device: Node to check for availability
586 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800587 * Returns true if the status property is absent or set to "okay" or "ok",
588 * false otherwise
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700589 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800590bool of_device_is_available(const struct device_node *device)
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700591{
592 unsigned long flags;
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800593 bool res;
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700594
595 raw_spin_lock_irqsave(&devtree_lock, flags);
596 res = __of_device_is_available(device);
597 raw_spin_unlock_irqrestore(&devtree_lock, flags);
598 return res;
599
600}
Josh Boyer834d97d2008-03-27 00:33:14 +1100601EXPORT_SYMBOL(of_device_is_available);
602
603/**
Kevin Cernekee37786c72015-04-09 13:05:14 -0700604 * of_device_is_big_endian - check if a device has BE registers
605 *
606 * @device: Node to check for endianness
607 *
608 * Returns true if the device has a "big-endian" property, or if the kernel
609 * was compiled for BE *and* the device has a "native-endian" property.
610 * Returns false otherwise.
611 *
612 * Callers would nominally use ioread32be/iowrite32be if
613 * of_device_is_big_endian() == true, or readl/writel otherwise.
614 */
615bool of_device_is_big_endian(const struct device_node *device)
616{
617 if (of_property_read_bool(device, "big-endian"))
618 return true;
619 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
620 of_property_read_bool(device, "native-endian"))
621 return true;
622 return false;
623}
624EXPORT_SYMBOL(of_device_is_big_endian);
625
626/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000627 * of_get_parent - Get a node's parent if any
628 * @node: Node to get parent
629 *
630 * Returns a node pointer with refcount incremented, use
631 * of_node_put() on it when done.
632 */
633struct device_node *of_get_parent(const struct device_node *node)
634{
635 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500636 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000637
638 if (!node)
639 return NULL;
640
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500641 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000642 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500643 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000644 return np;
645}
646EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000647
648/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000649 * of_get_next_parent - Iterate to a node's parent
650 * @node: Node to get parent of
651 *
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +0200652 * This is like of_get_parent() except that it drops the
653 * refcount on the passed node, making it suitable for iterating
654 * through a node's parents.
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000655 *
656 * Returns a node pointer with refcount incremented, use
657 * of_node_put() on it when done.
658 */
659struct device_node *of_get_next_parent(struct device_node *node)
660{
661 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500662 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000663
664 if (!node)
665 return NULL;
666
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500667 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000668 parent = of_node_get(node->parent);
669 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500670 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000671 return parent;
672}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300673EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000674
Grant Likely0d0e02d2014-05-22 01:04:17 +0900675static struct device_node *__of_get_next_child(const struct device_node *node,
676 struct device_node *prev)
677{
678 struct device_node *next;
679
Florian Fainelli43cb4362014-05-28 10:39:02 -0700680 if (!node)
681 return NULL;
682
Grant Likely0d0e02d2014-05-22 01:04:17 +0900683 next = prev ? prev->sibling : node->child;
684 for (; next; next = next->sibling)
685 if (of_node_get(next))
686 break;
687 of_node_put(prev);
688 return next;
689}
690#define __for_each_child_of_node(parent, child) \
691 for (child = __of_get_next_child(parent, NULL); child != NULL; \
692 child = __of_get_next_child(parent, child))
693
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000694/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000695 * of_get_next_child - Iterate a node childs
696 * @node: parent node
697 * @prev: previous child of the parent node, or NULL to get first
698 *
Baruch Siach64808272015-03-19 14:03:41 +0200699 * Returns a node pointer with refcount incremented, use of_node_put() on
700 * it when done. Returns NULL when prev is the last child. Decrements the
701 * refcount of prev.
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000702 */
703struct device_node *of_get_next_child(const struct device_node *node,
704 struct device_node *prev)
705{
706 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500707 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000708
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500709 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely0d0e02d2014-05-22 01:04:17 +0900710 next = __of_get_next_child(node, prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500711 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000712 return next;
713}
714EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000715
716/**
Timur Tabi32961932012-08-14 13:20:23 +0000717 * of_get_next_available_child - Find the next available child node
718 * @node: parent node
719 * @prev: previous child of the parent node, or NULL to get first
720 *
721 * This function is like of_get_next_child(), except that it
722 * automatically skips any disabled nodes (i.e. status = "disabled").
723 */
724struct device_node *of_get_next_available_child(const struct device_node *node,
725 struct device_node *prev)
726{
727 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000728 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000729
Florian Fainelli43cb4362014-05-28 10:39:02 -0700730 if (!node)
731 return NULL;
732
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000733 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000734 next = prev ? prev->sibling : node->child;
735 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700736 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000737 continue;
738 if (of_node_get(next))
739 break;
740 }
741 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000742 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000743 return next;
744}
745EXPORT_SYMBOL(of_get_next_available_child);
746
747/**
Johan Hovold36156f92018-08-27 10:21:45 +0200748 * of_get_compatible_child - Find compatible child node
749 * @parent: parent node
750 * @compatible: compatible string
751 *
752 * Lookup child node whose compatible property contains the given compatible
753 * string.
754 *
755 * Returns a node pointer with refcount incremented, use of_node_put() on it
756 * when done; or NULL if not found.
757 */
758struct device_node *of_get_compatible_child(const struct device_node *parent,
759 const char *compatible)
760{
761 struct device_node *child;
762
763 for_each_child_of_node(parent, child) {
764 if (of_device_is_compatible(child, compatible))
765 break;
766 }
767
768 return child;
769}
770EXPORT_SYMBOL(of_get_compatible_child);
771
772/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100773 * of_get_child_by_name - Find the child node by name for a given parent
774 * @node: parent node
775 * @name: child name to look for.
776 *
777 * This function looks for child node for given matching name
778 *
779 * Returns a node pointer if found, with refcount incremented, use
780 * of_node_put() on it when done.
781 * Returns NULL if node is not found.
782 */
783struct device_node *of_get_child_by_name(const struct device_node *node,
784 const char *name)
785{
786 struct device_node *child;
787
788 for_each_child_of_node(node, child)
789 if (child->name && (of_node_cmp(child->name, name) == 0))
790 break;
791 return child;
792}
793EXPORT_SYMBOL(of_get_child_by_name);
794
Frank Rowande0a58f32017-10-17 16:36:31 -0700795struct device_node *__of_find_node_by_path(struct device_node *parent,
Grant Likelyc22e6502014-03-14 17:07:12 +0000796 const char *path)
797{
798 struct device_node *child;
Leif Lindholm106937e2015-03-06 16:52:53 +0000799 int len;
Grant Likelyc22e6502014-03-14 17:07:12 +0000800
Brian Norris721a09e2015-03-17 12:30:31 -0700801 len = strcspn(path, "/:");
Grant Likelyc22e6502014-03-14 17:07:12 +0000802 if (!len)
803 return NULL;
804
805 __for_each_child_of_node(parent, child) {
Rob Herring95e6b1f2017-06-01 18:00:00 -0500806 const char *name = kbasename(child->full_name);
Grant Likelyc22e6502014-03-14 17:07:12 +0000807 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
808 return child;
809 }
810 return NULL;
811}
812
Rob Herring27497e12017-06-02 12:43:18 -0500813struct device_node *__of_find_node_by_full_path(struct device_node *node,
814 const char *path)
815{
816 const char *separator = strchr(path, ':');
817
818 while (node && *path == '/') {
819 struct device_node *tmp = node;
820
821 path++; /* Increment past '/' delimiter */
822 node = __of_find_node_by_path(node, path);
823 of_node_put(tmp);
824 path = strchrnul(path, '/');
825 if (separator && separator < path)
826 break;
827 }
828 return node;
829}
830
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100831/**
Leif Lindholm75c28c02014-11-28 11:34:28 +0000832 * of_find_node_opts_by_path - Find a node matching a full OF path
Grant Likelyc22e6502014-03-14 17:07:12 +0000833 * @path: Either the full path to match, or if the path does not
834 * start with '/', the name of a property of the /aliases
835 * node (an alias). In the case of an alias, the node
836 * matching the alias' value will be returned.
Leif Lindholm75c28c02014-11-28 11:34:28 +0000837 * @opts: Address of a pointer into which to store the start of
838 * an options string appended to the end of the path with
839 * a ':' separator.
Grant Likelyc22e6502014-03-14 17:07:12 +0000840 *
841 * Valid paths:
842 * /foo/bar Full path
843 * foo Valid alias
844 * foo/bar Valid alias + relative path
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000845 *
846 * Returns a node pointer with refcount incremented, use
847 * of_node_put() on it when done.
848 */
Leif Lindholm75c28c02014-11-28 11:34:28 +0000849struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000850{
Grant Likelyc22e6502014-03-14 17:07:12 +0000851 struct device_node *np = NULL;
852 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500853 unsigned long flags;
Leif Lindholm75c28c02014-11-28 11:34:28 +0000854 const char *separator = strchr(path, ':');
855
856 if (opts)
857 *opts = separator ? separator + 1 : NULL;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000858
Grant Likelyc22e6502014-03-14 17:07:12 +0000859 if (strcmp(path, "/") == 0)
Grant Likely5063e252014-10-03 16:28:27 +0100860 return of_node_get(of_root);
Grant Likelyc22e6502014-03-14 17:07:12 +0000861
862 /* The path could begin with an alias */
863 if (*path != '/') {
Leif Lindholm106937e2015-03-06 16:52:53 +0000864 int len;
865 const char *p = separator;
866
867 if (!p)
868 p = strchrnul(path, '/');
869 len = p - path;
Grant Likelyc22e6502014-03-14 17:07:12 +0000870
871 /* of_aliases must not be NULL */
872 if (!of_aliases)
873 return NULL;
874
875 for_each_property_of_node(of_aliases, pp) {
876 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
877 np = of_find_node_by_path(pp->value);
878 break;
879 }
880 }
881 if (!np)
882 return NULL;
883 path = p;
884 }
885
886 /* Step down the tree matching path components */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500887 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyc22e6502014-03-14 17:07:12 +0000888 if (!np)
Grant Likely5063e252014-10-03 16:28:27 +0100889 np = of_node_get(of_root);
Rob Herring27497e12017-06-02 12:43:18 -0500890 np = __of_find_node_by_full_path(np, path);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500891 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000892 return np;
893}
Leif Lindholm75c28c02014-11-28 11:34:28 +0000894EXPORT_SYMBOL(of_find_node_opts_by_path);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000895
896/**
897 * of_find_node_by_name - Find a node by its "name" property
Stephen Boyd02a876b2017-11-17 08:53:21 -0800898 * @from: The node to start searching from or NULL; the node
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000899 * you pass will not be searched, only the next one
Stephen Boyd02a876b2017-11-17 08:53:21 -0800900 * will. Typically, you pass what the previous call
901 * returned. of_node_put() will be called on @from.
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000902 * @name: The name string to match against
903 *
904 * Returns a node pointer with refcount incremented, use
905 * of_node_put() on it when done.
906 */
907struct device_node *of_find_node_by_name(struct device_node *from,
908 const char *name)
909{
910 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500911 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000912
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500913 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100914 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000915 if (np->name && (of_node_cmp(np->name, name) == 0)
916 && of_node_get(np))
917 break;
918 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500919 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000920 return np;
921}
922EXPORT_SYMBOL(of_find_node_by_name);
923
924/**
925 * of_find_node_by_type - Find a node by its "device_type" property
926 * @from: The node to start searching from, or NULL to start searching
927 * the entire device tree. The node you pass will not be
928 * searched, only the next one will; typically, you pass
929 * what the previous call returned. of_node_put() will be
930 * called on from for you.
931 * @type: The type string to match against
932 *
933 * Returns a node pointer with refcount incremented, use
934 * of_node_put() on it when done.
935 */
936struct device_node *of_find_node_by_type(struct device_node *from,
937 const char *type)
938{
939 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500940 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000941
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500942 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100943 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000944 if (np->type && (of_node_cmp(np->type, type) == 0)
945 && of_node_get(np))
946 break;
947 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500948 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000949 return np;
950}
951EXPORT_SYMBOL(of_find_node_by_type);
952
953/**
954 * of_find_compatible_node - Find a node based on type and one of the
955 * tokens in its "compatible" property
956 * @from: The node to start searching from or NULL, the node
957 * you pass will not be searched, only the next one
958 * will; typically, you pass what the previous call
959 * returned. of_node_put() will be called on it
960 * @type: The type string to match "device_type" or NULL to ignore
961 * @compatible: The string to match to one of the tokens in the device
962 * "compatible" list.
963 *
964 * Returns a node pointer with refcount incremented, use
965 * of_node_put() on it when done.
966 */
967struct device_node *of_find_compatible_node(struct device_node *from,
968 const char *type, const char *compatible)
969{
970 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500971 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000972
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500973 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100974 for_each_of_allnodes_from(from, np)
Kevin Hao215a14c2014-02-19 16:15:45 +0800975 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500976 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000977 break;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000978 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500979 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000980 return np;
981}
982EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100983
984/**
Michael Ellerman1e291b142008-11-12 18:54:42 +0000985 * of_find_node_with_property - Find a node which has a property with
986 * the given name.
987 * @from: The node to start searching from or NULL, the node
988 * you pass will not be searched, only the next one
989 * will; typically, you pass what the previous call
990 * returned. of_node_put() will be called on it
991 * @prop_name: The name of the property to look for.
992 *
993 * Returns a node pointer with refcount incremented, use
994 * of_node_put() on it when done.
995 */
996struct device_node *of_find_node_with_property(struct device_node *from,
997 const char *prop_name)
998{
999 struct device_node *np;
1000 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001001 unsigned long flags;
Michael Ellerman1e291b142008-11-12 18:54:42 +00001002
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001003 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001004 for_each_of_allnodes_from(from, np) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +05301005 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b142008-11-12 18:54:42 +00001006 if (of_prop_cmp(pp->name, prop_name) == 0) {
1007 of_node_get(np);
1008 goto out;
1009 }
1010 }
1011 }
1012out:
1013 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001014 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b142008-11-12 18:54:42 +00001015 return np;
1016}
1017EXPORT_SYMBOL(of_find_node_with_property);
1018
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001019static
1020const struct of_device_id *__of_match_node(const struct of_device_id *matches,
1021 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +11001022{
Kevin Hao215a14c2014-02-19 16:15:45 +08001023 const struct of_device_id *best_match = NULL;
1024 int score, best_score = 0;
1025
Grant Likelya52f07e2011-03-18 10:21:29 -06001026 if (!matches)
1027 return NULL;
1028
Kevin Hao215a14c2014-02-19 16:15:45 +08001029 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
1030 score = __of_device_is_compatible(node, matches->compatible,
1031 matches->type, matches->name);
1032 if (score > best_score) {
1033 best_match = matches;
1034 best_score = score;
1035 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +08001036 }
Kevin Hao215a14c2014-02-19 16:15:45 +08001037
1038 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +11001039}
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001040
1041/**
Geert Uytterhoevenc50949d2014-10-22 11:44:54 +02001042 * of_match_node - Tell if a device_node has a matching of_match structure
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001043 * @matches: array of of device match structures to search in
1044 * @node: the of device structure to match against
1045 *
Kevin Hao71c5498e2014-02-18 15:57:29 +08001046 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001047 */
1048const struct of_device_id *of_match_node(const struct of_device_id *matches,
1049 const struct device_node *node)
1050{
1051 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001052 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001053
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001054 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001055 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001056 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001057 return match;
1058}
Grant Likely283029d2008-01-09 06:20:40 +11001059EXPORT_SYMBOL(of_match_node);
1060
1061/**
Stephen Warren50c8af42012-11-20 16:12:20 -07001062 * of_find_matching_node_and_match - Find a node based on an of_device_id
1063 * match table.
Grant Likely283029d2008-01-09 06:20:40 +11001064 * @from: The node to start searching from or NULL, the node
1065 * you pass will not be searched, only the next one
1066 * will; typically, you pass what the previous call
1067 * returned. of_node_put() will be called on it
1068 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -07001069 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +11001070 *
1071 * Returns a node pointer with refcount incremented, use
1072 * of_node_put() on it when done.
1073 */
Stephen Warren50c8af42012-11-20 16:12:20 -07001074struct device_node *of_find_matching_node_and_match(struct device_node *from,
1075 const struct of_device_id *matches,
1076 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +11001077{
1078 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001079 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001080 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +11001081
Stephen Warren50c8af42012-11-20 16:12:20 -07001082 if (match)
1083 *match = NULL;
1084
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001085 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001086 for_each_of_allnodes_from(from, np) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001087 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001088 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -07001089 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001090 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +11001091 break;
Stephen Warren50c8af42012-11-20 16:12:20 -07001092 }
Grant Likely283029d2008-01-09 06:20:40 +11001093 }
1094 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001095 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +11001096 return np;
1097}
Grant Likely80c20222012-12-19 10:45:36 +00001098EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -04001099
1100/**
Grant Likely3f07af42008-07-25 22:25:13 -04001101 * of_modalias_node - Lookup appropriate modalias for a device node
1102 * @node: pointer to a device tree node
1103 * @modalias: Pointer to buffer that modalias value will be copied into
1104 * @len: Length of modalias value
1105 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001106 * Based on the value of the compatible property, this routine will attempt
1107 * to choose an appropriate modalias value for a particular device tree node.
1108 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1109 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -04001110 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001111 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001112 */
1113int of_modalias_node(struct device_node *node, char *modalias, int len)
1114{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001115 const char *compatible, *p;
1116 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001117
1118 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001119 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001120 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001121 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001122 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001123 return 0;
1124}
1125EXPORT_SYMBOL_GPL(of_modalias_node);
1126
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001127/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001128 * of_find_node_by_phandle - Find a node given a phandle
1129 * @handle: phandle of the node to find
1130 *
1131 * Returns a node pointer with refcount incremented, use
1132 * of_node_put() on it when done.
1133 */
1134struct device_node *of_find_node_by_phandle(phandle handle)
1135{
Frank Rowand0b3ce782018-03-04 16:14:47 -08001136 struct device_node *np = NULL;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001137 unsigned long flags;
Frank Rowand0b3ce782018-03-04 16:14:47 -08001138 phandle masked_handle;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001139
Grant Likelyfc59b442014-10-02 13:08:02 +01001140 if (!handle)
1141 return NULL;
1142
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001143 raw_spin_lock_irqsave(&devtree_lock, flags);
Frank Rowand0b3ce782018-03-04 16:14:47 -08001144
1145 masked_handle = handle & phandle_cache_mask;
1146
1147 if (phandle_cache) {
1148 if (phandle_cache[masked_handle] &&
1149 handle == phandle_cache[masked_handle]->phandle)
1150 np = phandle_cache[masked_handle];
1151 }
1152
1153 if (!np) {
1154 for_each_of_allnodes(np)
1155 if (np->phandle == handle) {
1156 if (phandle_cache)
1157 phandle_cache[masked_handle] = np;
1158 break;
1159 }
1160 }
1161
Jeremy Kerr89751a72010-02-01 21:34:11 -07001162 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001163 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001164 return np;
1165}
1166EXPORT_SYMBOL(of_find_node_by_phandle);
1167
Grant Likely624cfca2013-10-11 22:05:10 +01001168void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1169{
1170 int i;
Rob Herring0d638a02017-06-01 15:50:55 -05001171 printk("%s %pOF", msg, args->np);
Marcin Nowakowski4aa66342016-12-01 09:00:55 +01001172 for (i = 0; i < args->args_count; i++) {
1173 const char delim = i ? ',' : ':';
1174
1175 pr_cont("%c%08x", delim, args->args[i]);
1176 }
1177 pr_cont("\n");
Grant Likely624cfca2013-10-11 22:05:10 +01001178}
1179
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001180int of_phandle_iterator_init(struct of_phandle_iterator *it,
1181 const struct device_node *np,
1182 const char *list_name,
1183 const char *cells_name,
1184 int cell_count)
1185{
1186 const __be32 *list;
1187 int size;
1188
1189 memset(it, 0, sizeof(*it));
1190
1191 list = of_get_property(np, list_name, &size);
1192 if (!list)
1193 return -ENOENT;
1194
1195 it->cells_name = cells_name;
1196 it->cell_count = cell_count;
1197 it->parent = np;
1198 it->list_end = list + size / sizeof(*list);
1199 it->phandle_end = list;
1200 it->cur = list;
1201
1202 return 0;
1203}
Kuninori Morimoto00bab232017-04-20 01:31:16 +00001204EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001205
Joerg Roedelcd209b42016-04-04 17:49:18 +02001206int of_phandle_iterator_next(struct of_phandle_iterator *it)
1207{
1208 uint32_t count = 0;
1209
1210 if (it->node) {
1211 of_node_put(it->node);
1212 it->node = NULL;
1213 }
1214
1215 if (!it->cur || it->phandle_end >= it->list_end)
1216 return -ENOENT;
1217
1218 it->cur = it->phandle_end;
1219
1220 /* If phandle is 0, then it is an empty entry with no arguments. */
1221 it->phandle = be32_to_cpup(it->cur++);
1222
1223 if (it->phandle) {
1224
1225 /*
1226 * Find the provider node and parse the #*-cells property to
1227 * determine the argument length.
1228 */
1229 it->node = of_find_node_by_phandle(it->phandle);
1230
1231 if (it->cells_name) {
1232 if (!it->node) {
Rob Herring0d638a02017-06-01 15:50:55 -05001233 pr_err("%pOF: could not find phandle\n",
1234 it->parent);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001235 goto err;
1236 }
1237
1238 if (of_property_read_u32(it->node, it->cells_name,
1239 &count)) {
Rob Herring0d638a02017-06-01 15:50:55 -05001240 pr_err("%pOF: could not get %s for %pOF\n",
1241 it->parent,
Joerg Roedelcd209b42016-04-04 17:49:18 +02001242 it->cells_name,
Rob Herring0d638a02017-06-01 15:50:55 -05001243 it->node);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001244 goto err;
1245 }
1246 } else {
1247 count = it->cell_count;
1248 }
1249
1250 /*
1251 * Make sure that the arguments actually fit in the remaining
1252 * property data length
1253 */
1254 if (it->cur + count > it->list_end) {
Rob Herring0d638a02017-06-01 15:50:55 -05001255 pr_err("%pOF: arguments longer than property\n",
1256 it->parent);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001257 goto err;
1258 }
1259 }
1260
1261 it->phandle_end = it->cur + count;
1262 it->cur_count = count;
1263
1264 return 0;
1265
1266err:
1267 if (it->node) {
1268 of_node_put(it->node);
1269 it->node = NULL;
1270 }
1271
1272 return -EINVAL;
1273}
Kuninori Morimoto00bab232017-04-20 01:31:16 +00001274EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001275
Joerg Roedelabdaa772016-04-04 17:49:21 +02001276int of_phandle_iterator_args(struct of_phandle_iterator *it,
1277 uint32_t *args,
1278 int size)
1279{
1280 int i, count;
1281
1282 count = it->cur_count;
1283
1284 if (WARN_ON(size < count))
1285 count = size;
1286
1287 for (i = 0; i < count; i++)
1288 args[i] = be32_to_cpup(it->cur++);
1289
1290 return count;
1291}
1292
Grant Likelybd69f732013-02-10 22:57:21 +00001293static int __of_parse_phandle_with_args(const struct device_node *np,
1294 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001295 const char *cells_name,
1296 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001297 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001298{
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001299 struct of_phandle_iterator it;
1300 int rc, cur_index = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001301
Grant Likely15c9a0a2011-12-12 09:25:57 -07001302 /* Loop over the phandles until all the requested entry is found */
Joerg Roedelf623ce92016-04-04 17:49:20 +02001303 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
Grant Likely15c9a0a2011-12-12 09:25:57 -07001304 /*
Joerg Roedelcd209b42016-04-04 17:49:18 +02001305 * All of the error cases bail out of the loop, so at
Grant Likely15c9a0a2011-12-12 09:25:57 -07001306 * this point, the parsing is successful. If the requested
1307 * index matches, then fill the out_args structure and return,
1308 * or return -ENOENT for an empty entry.
1309 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001310 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001311 if (cur_index == index) {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001312 if (!it.phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001313 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001314
Grant Likely15c9a0a2011-12-12 09:25:57 -07001315 if (out_args) {
Joerg Roedelabdaa772016-04-04 17:49:21 +02001316 int c;
1317
1318 c = of_phandle_iterator_args(&it,
1319 out_args->args,
1320 MAX_PHANDLE_ARGS);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001321 out_args->np = it.node;
Joerg Roedelabdaa772016-04-04 17:49:21 +02001322 out_args->args_count = c;
Tang Yuantianb855f162013-04-10 11:36:39 +08001323 } else {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001324 of_node_put(it.node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001325 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001326
1327 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001328 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001329 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001330
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001331 cur_index++;
1332 }
1333
Grant Likely23ce04c2013-02-12 21:21:49 +00001334 /*
1335 * Unlock node before returning result; will be one of:
1336 * -ENOENT : index is for empty phandle
1337 * -EINVAL : parsing error on data
1338 */
Joerg Roedelcd209b42016-04-04 17:49:18 +02001339
Grant Likely23ce04c2013-02-12 21:21:49 +00001340 err:
Markus Elfringbeab47d2016-07-19 22:42:22 +02001341 of_node_put(it.node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001342 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001343}
Grant Likelybd69f732013-02-10 22:57:21 +00001344
Stephen Warreneded9dd2013-08-14 15:27:08 -06001345/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001346 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1347 * @np: Pointer to device node holding phandle property
1348 * @phandle_name: Name of property holding a phandle value
1349 * @index: For properties holding a table of phandles, this is the index into
1350 * the table
1351 *
1352 * Returns the device_node pointer with refcount incremented. Use
1353 * of_node_put() on it when done.
1354 */
1355struct device_node *of_parse_phandle(const struct device_node *np,
1356 const char *phandle_name, int index)
1357{
Stephen Warren91d99422013-08-14 15:27:11 -06001358 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001359
Stephen Warren91d99422013-08-14 15:27:11 -06001360 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001361 return NULL;
1362
Stephen Warren91d99422013-08-14 15:27:11 -06001363 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1364 index, &args))
1365 return NULL;
1366
1367 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001368}
1369EXPORT_SYMBOL(of_parse_phandle);
1370
1371/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001372 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1373 * @np: pointer to a device tree node containing a list
1374 * @list_name: property name that contains a list
1375 * @cells_name: property name that specifies phandles' arguments count
1376 * @index: index of a phandle to parse out
1377 * @out_args: optional pointer to output arguments structure (will be filled)
1378 *
1379 * This function is useful to parse lists of phandles and their arguments.
1380 * Returns 0 on success and fills out_args, on error returns appropriate
1381 * errno value.
1382 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001383 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warreneded9dd2013-08-14 15:27:08 -06001384 * pointer.
1385 *
1386 * Example:
1387 *
1388 * phandle1: node1 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001389 * #list-cells = <2>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001390 * }
1391 *
1392 * phandle2: node2 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001393 * #list-cells = <1>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001394 * }
1395 *
1396 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001397 * list = <&phandle1 1 2 &phandle2 3>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001398 * }
1399 *
1400 * To get a device_node of the `node2' node you may call this:
1401 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1402 */
Grant Likelybd69f732013-02-10 22:57:21 +00001403int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1404 const char *cells_name, int index,
1405 struct of_phandle_args *out_args)
1406{
1407 if (index < 0)
1408 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001409 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1410 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001411}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001412EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001413
Grant Likelybd69f732013-02-10 22:57:21 +00001414/**
Stephen Boydbd6f2fd2018-01-30 18:36:16 -08001415 * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it
1416 * @np: pointer to a device tree node containing a list
1417 * @list_name: property name that contains a list
1418 * @stem_name: stem of property names that specify phandles' arguments count
1419 * @index: index of a phandle to parse out
1420 * @out_args: optional pointer to output arguments structure (will be filled)
1421 *
1422 * This function is useful to parse lists of phandles and their arguments.
1423 * Returns 0 on success and fills out_args, on error returns appropriate errno
1424 * value. The difference between this function and of_parse_phandle_with_args()
1425 * is that this API remaps a phandle if the node the phandle points to has
1426 * a <@stem_name>-map property.
1427 *
1428 * Caller is responsible to call of_node_put() on the returned out_args->np
1429 * pointer.
1430 *
1431 * Example:
1432 *
1433 * phandle1: node1 {
1434 * #list-cells = <2>;
1435 * }
1436 *
1437 * phandle2: node2 {
1438 * #list-cells = <1>;
1439 * }
1440 *
1441 * phandle3: node3 {
1442 * #list-cells = <1>;
1443 * list-map = <0 &phandle2 3>,
1444 * <1 &phandle2 2>,
1445 * <2 &phandle1 5 1>;
1446 * list-map-mask = <0x3>;
1447 * };
1448 *
1449 * node4 {
1450 * list = <&phandle1 1 2 &phandle3 0>;
1451 * }
1452 *
1453 * To get a device_node of the `node2' node you may call this:
1454 * of_parse_phandle_with_args(node4, "list", "list", 1, &args);
1455 */
1456int of_parse_phandle_with_args_map(const struct device_node *np,
1457 const char *list_name,
1458 const char *stem_name,
1459 int index, struct of_phandle_args *out_args)
1460{
1461 char *cells_name, *map_name = NULL, *mask_name = NULL;
1462 char *pass_name = NULL;
1463 struct device_node *cur, *new = NULL;
1464 const __be32 *map, *mask, *pass;
1465 static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 };
1466 static const __be32 dummy_pass[] = { [0 ... MAX_PHANDLE_ARGS] = 0 };
1467 __be32 initial_match_array[MAX_PHANDLE_ARGS];
1468 const __be32 *match_array = initial_match_array;
1469 int i, ret, map_len, match;
1470 u32 list_size, new_size;
1471
1472 if (index < 0)
1473 return -EINVAL;
1474
1475 cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name);
1476 if (!cells_name)
1477 return -ENOMEM;
1478
1479 ret = -ENOMEM;
1480 map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name);
1481 if (!map_name)
1482 goto free;
1483
1484 mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name);
1485 if (!mask_name)
1486 goto free;
1487
1488 pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name);
1489 if (!pass_name)
1490 goto free;
1491
1492 ret = __of_parse_phandle_with_args(np, list_name, cells_name, 0, index,
1493 out_args);
1494 if (ret)
1495 goto free;
1496
1497 /* Get the #<list>-cells property */
1498 cur = out_args->np;
1499 ret = of_property_read_u32(cur, cells_name, &list_size);
1500 if (ret < 0)
1501 goto put;
1502
1503 /* Precalculate the match array - this simplifies match loop */
1504 for (i = 0; i < list_size; i++)
1505 initial_match_array[i] = cpu_to_be32(out_args->args[i]);
1506
1507 ret = -EINVAL;
1508 while (cur) {
1509 /* Get the <list>-map property */
1510 map = of_get_property(cur, map_name, &map_len);
1511 if (!map) {
1512 ret = 0;
1513 goto free;
1514 }
1515 map_len /= sizeof(u32);
1516
1517 /* Get the <list>-map-mask property (optional) */
1518 mask = of_get_property(cur, mask_name, NULL);
1519 if (!mask)
1520 mask = dummy_mask;
1521 /* Iterate through <list>-map property */
1522 match = 0;
1523 while (map_len > (list_size + 1) && !match) {
1524 /* Compare specifiers */
1525 match = 1;
1526 for (i = 0; i < list_size; i++, map_len--)
1527 match &= !((match_array[i] ^ *map++) & mask[i]);
1528
1529 of_node_put(new);
1530 new = of_find_node_by_phandle(be32_to_cpup(map));
1531 map++;
1532 map_len--;
1533
1534 /* Check if not found */
1535 if (!new)
1536 goto put;
1537
1538 if (!of_device_is_available(new))
1539 match = 0;
1540
1541 ret = of_property_read_u32(new, cells_name, &new_size);
1542 if (ret)
1543 goto put;
1544
1545 /* Check for malformed properties */
1546 if (WARN_ON(new_size > MAX_PHANDLE_ARGS))
1547 goto put;
1548 if (map_len < new_size)
1549 goto put;
1550
1551 /* Move forward by new node's #<list>-cells amount */
1552 map += new_size;
1553 map_len -= new_size;
1554 }
1555 if (!match)
1556 goto put;
1557
1558 /* Get the <list>-map-pass-thru property (optional) */
1559 pass = of_get_property(cur, pass_name, NULL);
1560 if (!pass)
1561 pass = dummy_pass;
1562
1563 /*
1564 * Successfully parsed a <list>-map translation; copy new
1565 * specifier into the out_args structure, keeping the
1566 * bits specified in <list>-map-pass-thru.
1567 */
1568 match_array = map - new_size;
1569 for (i = 0; i < new_size; i++) {
1570 __be32 val = *(map - new_size + i);
1571
1572 if (i < list_size) {
1573 val &= ~pass[i];
1574 val |= cpu_to_be32(out_args->args[i]) & pass[i];
1575 }
1576
1577 out_args->args[i] = be32_to_cpu(val);
1578 }
1579 out_args->args_count = list_size = new_size;
1580 /* Iterate again with new provider */
1581 out_args->np = new;
1582 of_node_put(cur);
1583 cur = new;
1584 }
1585put:
1586 of_node_put(cur);
1587 of_node_put(new);
1588free:
1589 kfree(mask_name);
1590 kfree(map_name);
1591 kfree(cells_name);
1592 kfree(pass_name);
1593
1594 return ret;
1595}
1596EXPORT_SYMBOL(of_parse_phandle_with_args_map);
1597
1598/**
Stephen Warren035fd942013-08-14 15:27:10 -06001599 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1600 * @np: pointer to a device tree node containing a list
1601 * @list_name: property name that contains a list
1602 * @cell_count: number of argument cells following the phandle
1603 * @index: index of a phandle to parse out
1604 * @out_args: optional pointer to output arguments structure (will be filled)
1605 *
1606 * This function is useful to parse lists of phandles and their arguments.
1607 * Returns 0 on success and fills out_args, on error returns appropriate
1608 * errno value.
1609 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001610 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warren035fd942013-08-14 15:27:10 -06001611 * pointer.
1612 *
1613 * Example:
1614 *
1615 * phandle1: node1 {
1616 * }
1617 *
1618 * phandle2: node2 {
1619 * }
1620 *
1621 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001622 * list = <&phandle1 0 2 &phandle2 2 3>;
Stephen Warren035fd942013-08-14 15:27:10 -06001623 * }
1624 *
1625 * To get a device_node of the `node2' node you may call this:
1626 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1627 */
1628int of_parse_phandle_with_fixed_args(const struct device_node *np,
1629 const char *list_name, int cell_count,
1630 int index, struct of_phandle_args *out_args)
1631{
1632 if (index < 0)
1633 return -EINVAL;
1634 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1635 index, out_args);
1636}
1637EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1638
1639/**
Grant Likelybd69f732013-02-10 22:57:21 +00001640 * of_count_phandle_with_args() - Find the number of phandles references in a property
1641 * @np: pointer to a device tree node containing a list
1642 * @list_name: property name that contains a list
1643 * @cells_name: property name that specifies phandles' arguments count
1644 *
1645 * Returns the number of phandle + argument tuples within a property. It
1646 * is a typical pattern to encode a list of phandle and variable
1647 * arguments into a single property. The number of arguments is encoded
1648 * by a property in the phandle-target node. For example, a gpios
1649 * property would contain a list of GPIO specifies consisting of a
1650 * phandle and 1 or more arguments. The number of arguments are
1651 * determined by the #gpio-cells property in the node pointed to by the
1652 * phandle.
1653 */
1654int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1655 const char *cells_name)
1656{
Joerg Roedel2021bd02016-04-04 17:49:19 +02001657 struct of_phandle_iterator it;
1658 int rc, cur_index = 0;
1659
1660 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
1661 if (rc)
1662 return rc;
1663
1664 while ((rc = of_phandle_iterator_next(&it)) == 0)
1665 cur_index += 1;
1666
1667 if (rc != -ENOENT)
1668 return rc;
1669
1670 return cur_index;
Grant Likelybd69f732013-02-10 22:57:21 +00001671}
1672EXPORT_SYMBOL(of_count_phandle_with_args);
1673
Grant Likely02af11b2009-11-23 20:16:45 -07001674/**
Xiubo Li62664f62014-01-22 13:57:39 +08001675 * __of_add_property - Add a property to a node without lock operations
1676 */
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001677int __of_add_property(struct device_node *np, struct property *prop)
Xiubo Li62664f62014-01-22 13:57:39 +08001678{
1679 struct property **next;
1680
1681 prop->next = NULL;
1682 next = &np->properties;
1683 while (*next) {
1684 if (strcmp(prop->name, (*next)->name) == 0)
1685 /* duplicate ! don't insert it */
1686 return -EEXIST;
1687
1688 next = &(*next)->next;
1689 }
1690 *next = prop;
1691
1692 return 0;
1693}
1694
1695/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001696 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001697 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001698int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001699{
Grant Likely02af11b2009-11-23 20:16:45 -07001700 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001701 int rc;
1702
Grant Likely8a2b22a22014-07-23 17:05:06 -06001703 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001704
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001705 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001706 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001707 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001708
Grant Likely8a2b22a22014-07-23 17:05:06 -06001709 if (!rc)
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001710 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001711
Grant Likely8a2b22a22014-07-23 17:05:06 -06001712 mutex_unlock(&of_mutex);
1713
Grant Likely259092a2014-07-16 12:48:23 -06001714 if (!rc)
1715 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1716
Xiubo Li62664f62014-01-22 13:57:39 +08001717 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001718}
1719
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001720int __of_remove_property(struct device_node *np, struct property *prop)
1721{
1722 struct property **next;
1723
1724 for (next = &np->properties; *next; next = &(*next)->next) {
1725 if (*next == prop)
1726 break;
1727 }
1728 if (*next == NULL)
1729 return -ENODEV;
1730
1731 /* found the node */
1732 *next = prop->next;
1733 prop->next = np->deadprops;
1734 np->deadprops = prop;
1735
1736 return 0;
1737}
1738
Grant Likely02af11b2009-11-23 20:16:45 -07001739/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001740 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001741 *
1742 * Note that we don't actually remove it, since we have given out
1743 * who-knows-how-many pointers to the data using get-property.
1744 * Instead we just move the property to the "dead properties"
1745 * list, so it won't be found any more.
1746 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001747int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001748{
Grant Likely02af11b2009-11-23 20:16:45 -07001749 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001750 int rc;
1751
Suraj Jitindar Singh201b3fe2016-04-28 15:34:54 +10001752 if (!prop)
1753 return -ENODEV;
1754
Grant Likely8a2b22a22014-07-23 17:05:06 -06001755 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001756
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001757 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001758 rc = __of_remove_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001759 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001760
Grant Likely8a2b22a22014-07-23 17:05:06 -06001761 if (!rc)
1762 __of_remove_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001763
Grant Likely8a2b22a22014-07-23 17:05:06 -06001764 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +00001765
Grant Likely259092a2014-07-16 12:48:23 -06001766 if (!rc)
1767 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1768
Grant Likely8a2b22a22014-07-23 17:05:06 -06001769 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001770}
1771
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001772int __of_update_property(struct device_node *np, struct property *newprop,
1773 struct property **oldpropp)
1774{
1775 struct property **next, *oldprop;
1776
1777 for (next = &np->properties; *next; next = &(*next)->next) {
1778 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1779 break;
1780 }
1781 *oldpropp = oldprop = *next;
1782
1783 if (oldprop) {
1784 /* replace the node */
1785 newprop->next = oldprop->next;
1786 *next = newprop;
1787 oldprop->next = np->deadprops;
1788 np->deadprops = oldprop;
1789 } else {
1790 /* new node */
1791 newprop->next = NULL;
1792 *next = newprop;
1793 }
Grant Likely02af11b2009-11-23 20:16:45 -07001794
1795 return 0;
1796}
1797
1798/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001799 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001800 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001801 *
1802 * Note that we don't actually remove it, since we have given out
1803 * who-knows-how-many pointers to the data using get-property.
1804 * Instead we just move the property to the "dead properties" list,
1805 * and add the new property to the property list
1806 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001807int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001808{
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001809 struct property *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001810 unsigned long flags;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001811 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001812
Dong Aisheng475d0092012-07-11 15:16:37 +10001813 if (!newprop->name)
1814 return -EINVAL;
1815
Grant Likely8a2b22a22014-07-23 17:05:06 -06001816 mutex_lock(&of_mutex);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001817
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001818 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001819 rc = __of_update_property(np, newprop, &oldprop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001820 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001821
Grant Likely8a2b22a22014-07-23 17:05:06 -06001822 if (!rc)
1823 __of_update_property_sysfs(np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001824
Grant Likely8a2b22a22014-07-23 17:05:06 -06001825 mutex_unlock(&of_mutex);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001826
Grant Likely259092a2014-07-16 12:48:23 -06001827 if (!rc)
1828 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001829
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001830 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001831}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001832
Shawn Guo611cad72011-08-15 15:28:14 +08001833static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1834 int id, const char *stem, int stem_len)
1835{
1836 ap->np = np;
1837 ap->id = id;
1838 strncpy(ap->stem, stem, stem_len);
1839 ap->stem[stem_len] = 0;
1840 list_add_tail(&ap->link, &aliases_lookup);
Rob Herring0d638a02017-06-01 15:50:55 -05001841 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1842 ap->alias, ap->stem, ap->id, np);
Shawn Guo611cad72011-08-15 15:28:14 +08001843}
1844
1845/**
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001846 * of_alias_scan - Scan all properties of the 'aliases' node
Shawn Guo611cad72011-08-15 15:28:14 +08001847 *
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001848 * The function scans all the properties of the 'aliases' node and populates
1849 * the global lookup table with the properties. It returns the
1850 * number of alias properties found, or an error code in case of failure.
Shawn Guo611cad72011-08-15 15:28:14 +08001851 *
1852 * @dt_alloc: An allocator that provides a virtual address to memory
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001853 * for storing the resulting tree
Shawn Guo611cad72011-08-15 15:28:14 +08001854 */
1855void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1856{
1857 struct property *pp;
1858
Laurentiu Tudor7dbe5842014-08-27 17:09:39 +03001859 of_aliases = of_find_node_by_path("/aliases");
Shawn Guo611cad72011-08-15 15:28:14 +08001860 of_chosen = of_find_node_by_path("/chosen");
1861 if (of_chosen == NULL)
1862 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02001863
1864 if (of_chosen) {
Grant Likelya752ee52014-03-28 08:12:18 -07001865 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
Sergei Shtylyovb0d9d922017-07-23 19:55:48 +03001866 const char *name = NULL;
1867
1868 if (of_property_read_string(of_chosen, "stdout-path", &name))
1869 of_property_read_string(of_chosen, "linux,stdout-path",
1870 &name);
Grant Likelya752ee52014-03-28 08:12:18 -07001871 if (IS_ENABLED(CONFIG_PPC) && !name)
Sergei Shtylyovb0d9d922017-07-23 19:55:48 +03001872 of_property_read_string(of_aliases, "stdout", &name);
Peter Hurleyf64255b2015-03-17 16:46:33 -04001873 if (name)
Leif Lindholm7914a7c2014-11-27 17:56:07 +00001874 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001875 }
1876
Shawn Guo611cad72011-08-15 15:28:14 +08001877 if (!of_aliases)
1878 return;
1879
Dong Aisheng8af0da92011-12-22 20:19:24 +08001880 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001881 const char *start = pp->name;
1882 const char *end = start + strlen(start);
1883 struct device_node *np;
1884 struct alias_prop *ap;
1885 int id, len;
1886
1887 /* Skip those we do not want to proceed */
1888 if (!strcmp(pp->name, "name") ||
1889 !strcmp(pp->name, "phandle") ||
1890 !strcmp(pp->name, "linux,phandle"))
1891 continue;
1892
1893 np = of_find_node_by_path(pp->value);
1894 if (!np)
1895 continue;
1896
1897 /* walk the alias backwards to extract the id and work out
1898 * the 'stem' string */
1899 while (isdigit(*(end-1)) && end > start)
1900 end--;
1901 len = end - start;
1902
1903 if (kstrtoint(end, 10, &id) < 0)
1904 continue;
1905
1906 /* Allocate an alias_prop with enough space for the stem */
Paul Burtonde96ec22016-09-23 16:38:27 +01001907 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
Shawn Guo611cad72011-08-15 15:28:14 +08001908 if (!ap)
1909 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01001910 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001911 ap->alias = start;
1912 of_alias_add(ap, np, id, start, len);
1913 }
1914}
1915
1916/**
1917 * of_alias_get_id - Get alias id for the given device_node
1918 * @np: Pointer to the given device_node
1919 * @stem: Alias stem of the given device_node
1920 *
Geert Uytterhoeven5a53a07f2014-03-11 11:23:38 +01001921 * The function travels the lookup table to get the alias id for the given
1922 * device_node and alias stem. It returns the alias id if found.
Shawn Guo611cad72011-08-15 15:28:14 +08001923 */
1924int of_alias_get_id(struct device_node *np, const char *stem)
1925{
1926 struct alias_prop *app;
1927 int id = -ENODEV;
1928
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001929 mutex_lock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001930 list_for_each_entry(app, &aliases_lookup, link) {
1931 if (strcmp(app->stem, stem) != 0)
1932 continue;
1933
1934 if (np == app->np) {
1935 id = app->id;
1936 break;
1937 }
1938 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001939 mutex_unlock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001940
1941 return id;
1942}
1943EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001944
Wolfram Sang351d2242015-03-12 17:17:58 +01001945/**
1946 * of_alias_get_highest_id - Get highest alias id for the given stem
1947 * @stem: Alias stem to be examined
1948 *
1949 * The function travels the lookup table to get the highest alias id for the
1950 * given alias stem. It returns the alias id if found.
1951 */
1952int of_alias_get_highest_id(const char *stem)
1953{
1954 struct alias_prop *app;
1955 int id = -ENODEV;
1956
1957 mutex_lock(&of_mutex);
1958 list_for_each_entry(app, &aliases_lookup, link) {
1959 if (strcmp(app->stem, stem) != 0)
1960 continue;
1961
1962 if (app->id > id)
1963 id = app->id;
1964 }
1965 mutex_unlock(&of_mutex);
1966
1967 return id;
1968}
1969EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
1970
Sascha Hauer5c19e952013-08-05 14:40:44 +02001971/**
Grant Likely3482f2c2014-03-27 17:18:55 -07001972 * of_console_check() - Test and setup console for DT setup
1973 * @dn - Pointer to device node
1974 * @name - Name to use for preferred console without index. ex. "ttyS"
1975 * @index - Index to use for preferred console.
Sascha Hauer5c19e952013-08-05 14:40:44 +02001976 *
Grant Likely3482f2c2014-03-27 17:18:55 -07001977 * Check if the given device node matches the stdout-path property in the
1978 * /chosen node. If it does then register it as the preferred console and return
1979 * TRUE. Otherwise return FALSE.
Sascha Hauer5c19e952013-08-05 14:40:44 +02001980 */
Grant Likely3482f2c2014-03-27 17:18:55 -07001981bool of_console_check(struct device_node *dn, char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +02001982{
Grant Likely3482f2c2014-03-27 17:18:55 -07001983 if (!dn || dn != of_stdout || console_set_on_cmdline)
Sascha Hauer5c19e952013-08-05 14:40:44 +02001984 return false;
Sergey Senozhatskydb179e02017-09-26 15:25:10 +09001985
1986 /*
1987 * XXX: cast `options' to char pointer to suppress complication
1988 * warnings: printk, UART and console drivers expect char pointer.
1989 */
1990 return !add_preferred_console(name, index, (char *)of_stdout_options);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001991}
Grant Likely3482f2c2014-03-27 17:18:55 -07001992EXPORT_SYMBOL_GPL(of_console_check);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01001993
1994/**
1995 * of_find_next_cache_node - Find a node's subsidiary cache
1996 * @np: node of type "cpu" or "cache"
1997 *
1998 * Returns a node pointer with refcount incremented, use
1999 * of_node_put() on it when done. Caller should hold a reference
2000 * to np.
2001 */
2002struct device_node *of_find_next_cache_node(const struct device_node *np)
2003{
Rob Herring91d96742017-05-04 12:30:07 -05002004 struct device_node *child, *cache_node;
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002005
Rob Herring91d96742017-05-04 12:30:07 -05002006 cache_node = of_parse_phandle(np, "l2-cache", 0);
2007 if (!cache_node)
2008 cache_node = of_parse_phandle(np, "next-level-cache", 0);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002009
Rob Herring91d96742017-05-04 12:30:07 -05002010 if (cache_node)
2011 return cache_node;
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002012
2013 /* OF on pmac has nodes instead of properties named "l2-cache"
2014 * beneath CPU nodes.
2015 */
2016 if (!strcmp(np->type, "cpu"))
2017 for_each_child_of_node(np, child)
2018 if (!strcmp(child->type, "cache"))
2019 return child;
2020
2021 return NULL;
2022}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002023
2024/**
Sudeep Holla5fa23532017-01-16 10:40:43 +00002025 * of_find_last_cache_level - Find the level at which the last cache is
2026 * present for the given logical cpu
2027 *
2028 * @cpu: cpu number(logical index) for which the last cache level is needed
2029 *
2030 * Returns the the level at which the last cache is present. It is exactly
2031 * same as the total number of cache levels for the given logical cpu.
2032 */
2033int of_find_last_cache_level(unsigned int cpu)
2034{
2035 u32 cache_level = 0;
2036 struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
2037
2038 while (np) {
2039 prev = np;
2040 of_node_put(np);
2041 np = of_find_next_cache_node(np);
2042 }
2043
2044 of_property_read_u32(prev, "cache-level", &cache_level);
2045
2046 return cache_level;
2047}