blob: bd51d2113cc05987f3f6098ecdfbd86596f5ee51 [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
143 cache_entries = roundup_pow_of_two(phandles);
144 phandle_cache_mask = cache_entries - 1;
145
146 phandle_cache = kcalloc(cache_entries, sizeof(*phandle_cache),
147 GFP_ATOMIC);
148 if (!phandle_cache)
149 goto out;
150
151 for_each_of_allnodes(np)
152 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
153 phandle_cache[np->phandle & phandle_cache_mask] = np;
154
155out:
156 raw_spin_unlock_irqrestore(&devtree_lock, flags);
157}
158
Frank Rowandb9952b52018-07-12 14:00:07 -0700159int of_free_phandle_cache(void)
Frank Rowand0b3ce782018-03-04 16:14:47 -0800160{
161 unsigned long flags;
162
163 raw_spin_lock_irqsave(&devtree_lock, flags);
164
165 kfree(phandle_cache);
166 phandle_cache = NULL;
167
168 raw_spin_unlock_irqrestore(&devtree_lock, flags);
169
170 return 0;
171}
Frank Rowandb9952b52018-07-12 14:00:07 -0700172#if !defined(CONFIG_MODULES)
Frank Rowand0b3ce782018-03-04 16:14:47 -0800173late_initcall_sync(of_free_phandle_cache);
174#endif
175
Sudeep Holla194ec932015-05-14 15:28:24 +0100176void __init of_core_init(void)
Grant Likely75b57ec2014-02-20 18:02:11 +0000177{
178 struct device_node *np;
179
Frank Rowand0b3ce782018-03-04 16:14:47 -0800180 of_populate_phandle_cache();
181
Grant Likely75b57ec2014-02-20 18:02:11 +0000182 /* Create the kset, and register existing nodes */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300183 mutex_lock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000184 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
185 if (!of_kset) {
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300186 mutex_unlock(&of_mutex);
Rob Herring606ad422016-06-15 08:32:18 -0500187 pr_err("failed to register existing nodes\n");
Sudeep Holla194ec932015-05-14 15:28:24 +0100188 return;
Grant Likely75b57ec2014-02-20 18:02:11 +0000189 }
190 for_each_of_allnodes(np)
Grant Likely8a2b22a22014-07-23 17:05:06 -0600191 __of_attach_node_sysfs(np);
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300192 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000193
Grant Likely83570412012-11-06 21:03:27 +0000194 /* Symlink in /proc as required by userspace ABI */
Grant Likely5063e252014-10-03 16:28:27 +0100195 if (of_root)
Grant Likely75b57ec2014-02-20 18:02:11 +0000196 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000197}
Grant Likely75b57ec2014-02-20 18:02:11 +0000198
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500199static struct property *__of_find_property(const struct device_node *np,
200 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000201{
202 struct property *pp;
203
Timur Tabi64e45662008-05-08 05:19:59 +1000204 if (!np)
205 return NULL;
206
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530207 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000208 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530209 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000210 *lenp = pp->length;
211 break;
212 }
213 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500214
215 return pp;
216}
217
218struct property *of_find_property(const struct device_node *np,
219 const char *name,
220 int *lenp)
221{
222 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500223 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500224
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500225 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500226 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500227 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000228
229 return pp;
230}
231EXPORT_SYMBOL(of_find_property);
232
Grant Likely5063e252014-10-03 16:28:27 +0100233struct device_node *__of_find_all_nodes(struct device_node *prev)
234{
235 struct device_node *np;
236 if (!prev) {
237 np = of_root;
238 } else if (prev->child) {
239 np = prev->child;
240 } else {
241 /* Walk back up looking for a sibling, or the end of the structure */
242 np = prev;
243 while (np->parent && !np->sibling)
244 np = np->parent;
245 np = np->sibling; /* Might be null at the end of the tree */
246 }
247 return np;
248}
249
Grant Likelye91edcf2009-10-15 10:58:09 -0600250/**
251 * of_find_all_nodes - Get next node in global list
252 * @prev: Previous node or NULL to start iteration
253 * of_node_put() will be called on it
254 *
255 * Returns a node pointer with refcount incremented, use
256 * of_node_put() on it when done.
257 */
258struct device_node *of_find_all_nodes(struct device_node *prev)
259{
260 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000261 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600262
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000263 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100264 np = __of_find_all_nodes(prev);
265 of_node_get(np);
Grant Likelye91edcf2009-10-15 10:58:09 -0600266 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000267 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600268 return np;
269}
270EXPORT_SYMBOL(of_find_all_nodes);
271
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000272/*
273 * Find a property with a given name for a given node
274 * and return the value.
275 */
Grant Likelya25095d2014-07-15 23:25:43 -0600276const void *__of_get_property(const struct device_node *np,
277 const char *name, int *lenp)
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500278{
279 struct property *pp = __of_find_property(np, name, lenp);
280
281 return pp ? pp->value : NULL;
282}
283
284/*
285 * Find a property with a given name for a given node
286 * and return the value.
287 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000288const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500289 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000290{
291 struct property *pp = of_find_property(np, name, lenp);
292
293 return pp ? pp->value : NULL;
294}
295EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000296
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100297/*
298 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
299 *
300 * @cpu: logical cpu index of a core/thread
301 * @phys_id: physical identifier of a core/thread
302 *
303 * CPU logical to physical index mapping is architecture specific.
304 * However this __weak function provides a default match of physical
305 * id to logical cpu index. phys_id provided here is usually values read
306 * from the device tree which must match the hardware internal registers.
307 *
308 * Returns true if the physical identifier and the logical cpu index
309 * correspond to the same core/thread, false otherwise.
310 */
311bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
312{
313 return (u32)phys_id == cpu;
314}
315
316/**
317 * Checks if the given "prop_name" property holds the physical id of the
318 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
319 * NULL, local thread number within the core is returned in it.
320 */
321static bool __of_find_n_match_cpu_property(struct device_node *cpun,
322 const char *prop_name, int cpu, unsigned int *thread)
323{
324 const __be32 *cell;
325 int ac, prop_len, tid;
326 u64 hwid;
327
328 ac = of_n_addr_cells(cpun);
329 cell = of_get_property(cpun, prop_name, &prop_len);
Rob Herring6487c152018-08-27 13:46:51 -0500330 if (!cell && !ac && arch_match_cpu_phys_id(cpu, 0))
331 return true;
Grant Likelyf3cea452013-10-04 17:24:26 +0100332 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100333 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100334 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100335 for (tid = 0; tid < prop_len; tid++) {
336 hwid = of_read_number(cell, ac);
337 if (arch_match_cpu_phys_id(cpu, hwid)) {
338 if (thread)
339 *thread = tid;
340 return true;
341 }
342 cell += ac;
343 }
344 return false;
345}
346
David Millerd1cb9d12013-10-03 17:24:51 -0400347/*
348 * arch_find_n_match_cpu_physical_id - See if the given device node is
349 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
350 * else false. If 'thread' is non-NULL, the local thread number within the
351 * core is returned in it.
352 */
353bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
354 int cpu, unsigned int *thread)
355{
356 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
357 * for thread ids on PowerPC. If it doesn't exist fallback to
358 * standard "reg" property.
359 */
360 if (IS_ENABLED(CONFIG_PPC) &&
361 __of_find_n_match_cpu_property(cpun,
362 "ibm,ppc-interrupt-server#s",
363 cpu, thread))
364 return true;
365
Masahiro Yamada510bd062015-10-28 12:05:27 +0900366 return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
David Millerd1cb9d12013-10-03 17:24:51 -0400367}
368
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100369/**
370 * of_get_cpu_node - Get device node associated with the given logical CPU
371 *
372 * @cpu: CPU number(logical index) for which device node is required
373 * @thread: if not NULL, local thread number within the physical core is
374 * returned
375 *
376 * The main purpose of this function is to retrieve the device node for the
377 * given logical CPU index. It should be used to initialize the of_node in
378 * cpu device. Once of_node in cpu device is populated, all the further
379 * references can use that instead.
380 *
381 * CPU logical to physical index mapping is architecture specific and is built
382 * before booting secondary cores. This function uses arch_match_cpu_phys_id
383 * which can be overridden by architecture specific implementation.
384 *
Masahiro Yamada1c986e32016-04-20 10:18:46 +0900385 * Returns a node pointer for the logical cpu with refcount incremented, use
386 * of_node_put() on it when done. Returns NULL if not found.
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100387 */
388struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
389{
David Millerd1cb9d12013-10-03 17:24:51 -0400390 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100391
David Millerd1cb9d12013-10-03 17:24:51 -0400392 for_each_node_by_type(cpun, "cpu") {
393 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100394 return cpun;
395 }
396 return NULL;
397}
398EXPORT_SYMBOL(of_get_cpu_node);
399
Kevin Hao215a14c2014-02-19 16:15:45 +0800400/**
Suzuki K Poulosea0e71cd2018-01-02 11:25:27 +0000401 * of_cpu_node_to_id: Get the logical CPU number for a given device_node
402 *
403 * @cpu_node: Pointer to the device_node for CPU.
404 *
405 * Returns the logical CPU number of the given CPU device_node.
406 * Returns -ENODEV if the CPU is not found.
407 */
408int of_cpu_node_to_id(struct device_node *cpu_node)
409{
410 int cpu;
411 bool found = false;
412 struct device_node *np;
413
414 for_each_possible_cpu(cpu) {
415 np = of_cpu_device_node_get(cpu);
416 found = (cpu_node == np);
417 of_node_put(np);
418 if (found)
419 return cpu;
420 }
421
422 return -ENODEV;
423}
424EXPORT_SYMBOL(of_cpu_node_to_id);
425
426/**
Kevin Hao215a14c2014-02-19 16:15:45 +0800427 * __of_device_is_compatible() - Check if the node matches given constraints
428 * @device: pointer to node
429 * @compat: required compatible string, NULL or "" for any match
430 * @type: required device_type value, NULL or "" for any match
431 * @name: required node name, NULL or "" for any match
432 *
433 * Checks if the given @compat, @type and @name strings match the
434 * properties of the given @device. A constraints can be skipped by
435 * passing NULL or an empty string as the constraint.
436 *
437 * Returns 0 for no match, and a positive integer on match. The return
438 * value is a relative score with larger values indicating better
439 * matches. The score is weighted for the most specific compatible value
440 * to get the highest score. Matching type is next, followed by matching
441 * name. Practically speaking, this results in the following priority
442 * order for matches:
443 *
444 * 1. specific compatible && type && name
445 * 2. specific compatible && type
446 * 3. specific compatible && name
447 * 4. specific compatible
448 * 5. general compatible && type && name
449 * 6. general compatible && type
450 * 7. general compatible && name
451 * 8. general compatible
452 * 9. type && name
453 * 10. type
454 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000455 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500456static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800457 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000458{
Kevin Hao215a14c2014-02-19 16:15:45 +0800459 struct property *prop;
460 const char *cp;
461 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000462
Kevin Hao215a14c2014-02-19 16:15:45 +0800463 /* Compatible match has highest priority */
464 if (compat && compat[0]) {
465 prop = __of_find_property(device, "compatible", NULL);
466 for (cp = of_prop_next_string(prop, NULL); cp;
467 cp = of_prop_next_string(prop, cp), index++) {
468 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
469 score = INT_MAX/2 - (index << 2);
470 break;
471 }
472 }
473 if (!score)
474 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000475 }
476
Kevin Hao215a14c2014-02-19 16:15:45 +0800477 /* Matching type is better than matching name */
478 if (type && type[0]) {
479 if (!device->type || of_node_cmp(type, device->type))
480 return 0;
481 score += 2;
482 }
483
484 /* Matching name is a bit better than not */
485 if (name && name[0]) {
486 if (!device->name || of_node_cmp(name, device->name))
487 return 0;
488 score++;
489 }
490
491 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000492}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500493
494/** Checks if the given "compat" string matches one of the strings in
495 * the device's "compatible" property
496 */
497int of_device_is_compatible(const struct device_node *device,
498 const char *compat)
499{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500500 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500501 int res;
502
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500503 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800504 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500505 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500506 return res;
507}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000508EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000509
Benjamin Herrenschmidtb9c13fe32016-07-08 08:35:59 +1000510/** Checks if the device is compatible with any of the entries in
511 * a NULL terminated array of strings. Returns the best match
512 * score or 0.
513 */
514int of_device_compatible_match(struct device_node *device,
515 const char *const *compat)
516{
517 unsigned int tmp, score = 0;
518
519 if (!compat)
520 return 0;
521
522 while (*compat) {
523 tmp = of_device_is_compatible(device, *compat);
524 if (tmp > score)
525 score = tmp;
526 compat++;
527 }
528
529 return score;
530}
531
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000532/**
Grant Likely71a157e2010-02-01 21:34:14 -0700533 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700534 * @compat: compatible string to look for in root node's compatible property.
535 *
Kevin Cernekee25c7a1d2014-11-12 12:54:00 -0800536 * Returns a positive integer if the root node has the given value in its
Grant Likely1f43cfb2010-01-28 13:47:25 -0700537 * compatible property.
538 */
Grant Likely71a157e2010-02-01 21:34:14 -0700539int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700540{
541 struct device_node *root;
542 int rc = 0;
543
544 root = of_find_node_by_path("/");
545 if (root) {
546 rc = of_device_is_compatible(root, compat);
547 of_node_put(root);
548 }
549 return rc;
550}
Grant Likely71a157e2010-02-01 21:34:14 -0700551EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700552
553/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700554 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100555 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700556 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100557 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800558 * Returns true if the status property is absent or set to "okay" or "ok",
559 * false otherwise
Josh Boyer834d97d2008-03-27 00:33:14 +1100560 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800561static bool __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100562{
563 const char *status;
564 int statlen;
565
Xiubo Li42ccd782014-01-13 11:07:28 +0800566 if (!device)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800567 return false;
Xiubo Li42ccd782014-01-13 11:07:28 +0800568
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700569 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100570 if (status == NULL)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800571 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100572
573 if (statlen > 0) {
574 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800575 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100576 }
577
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800578 return false;
Josh Boyer834d97d2008-03-27 00:33:14 +1100579}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700580
581/**
582 * of_device_is_available - check if a device is available for use
583 *
584 * @device: Node to check for availability
585 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800586 * Returns true if the status property is absent or set to "okay" or "ok",
587 * false otherwise
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700588 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800589bool of_device_is_available(const struct device_node *device)
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700590{
591 unsigned long flags;
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800592 bool res;
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700593
594 raw_spin_lock_irqsave(&devtree_lock, flags);
595 res = __of_device_is_available(device);
596 raw_spin_unlock_irqrestore(&devtree_lock, flags);
597 return res;
598
599}
Josh Boyer834d97d2008-03-27 00:33:14 +1100600EXPORT_SYMBOL(of_device_is_available);
601
602/**
Kevin Cernekee37786c72015-04-09 13:05:14 -0700603 * of_device_is_big_endian - check if a device has BE registers
604 *
605 * @device: Node to check for endianness
606 *
607 * Returns true if the device has a "big-endian" property, or if the kernel
608 * was compiled for BE *and* the device has a "native-endian" property.
609 * Returns false otherwise.
610 *
611 * Callers would nominally use ioread32be/iowrite32be if
612 * of_device_is_big_endian() == true, or readl/writel otherwise.
613 */
614bool of_device_is_big_endian(const struct device_node *device)
615{
616 if (of_property_read_bool(device, "big-endian"))
617 return true;
618 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
619 of_property_read_bool(device, "native-endian"))
620 return true;
621 return false;
622}
623EXPORT_SYMBOL(of_device_is_big_endian);
624
625/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000626 * of_get_parent - Get a node's parent if any
627 * @node: Node to get parent
628 *
629 * Returns a node pointer with refcount incremented, use
630 * of_node_put() on it when done.
631 */
632struct device_node *of_get_parent(const struct device_node *node)
633{
634 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500635 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000636
637 if (!node)
638 return NULL;
639
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500640 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000641 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500642 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000643 return np;
644}
645EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000646
647/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000648 * of_get_next_parent - Iterate to a node's parent
649 * @node: Node to get parent of
650 *
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +0200651 * This is like of_get_parent() except that it drops the
652 * refcount on the passed node, making it suitable for iterating
653 * through a node's parents.
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000654 *
655 * Returns a node pointer with refcount incremented, use
656 * of_node_put() on it when done.
657 */
658struct device_node *of_get_next_parent(struct device_node *node)
659{
660 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500661 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000662
663 if (!node)
664 return NULL;
665
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500666 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000667 parent = of_node_get(node->parent);
668 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500669 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000670 return parent;
671}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300672EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000673
Grant Likely0d0e02d2014-05-22 01:04:17 +0900674static struct device_node *__of_get_next_child(const struct device_node *node,
675 struct device_node *prev)
676{
677 struct device_node *next;
678
Florian Fainelli43cb4362014-05-28 10:39:02 -0700679 if (!node)
680 return NULL;
681
Grant Likely0d0e02d2014-05-22 01:04:17 +0900682 next = prev ? prev->sibling : node->child;
683 for (; next; next = next->sibling)
684 if (of_node_get(next))
685 break;
686 of_node_put(prev);
687 return next;
688}
689#define __for_each_child_of_node(parent, child) \
690 for (child = __of_get_next_child(parent, NULL); child != NULL; \
691 child = __of_get_next_child(parent, child))
692
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000693/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000694 * of_get_next_child - Iterate a node childs
695 * @node: parent node
696 * @prev: previous child of the parent node, or NULL to get first
697 *
Baruch Siach64808272015-03-19 14:03:41 +0200698 * Returns a node pointer with refcount incremented, use of_node_put() on
699 * it when done. Returns NULL when prev is the last child. Decrements the
700 * refcount of prev.
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000701 */
702struct device_node *of_get_next_child(const struct device_node *node,
703 struct device_node *prev)
704{
705 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500706 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000707
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500708 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely0d0e02d2014-05-22 01:04:17 +0900709 next = __of_get_next_child(node, prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500710 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000711 return next;
712}
713EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000714
715/**
Timur Tabi32961932012-08-14 13:20:23 +0000716 * of_get_next_available_child - Find the next available child node
717 * @node: parent node
718 * @prev: previous child of the parent node, or NULL to get first
719 *
720 * This function is like of_get_next_child(), except that it
721 * automatically skips any disabled nodes (i.e. status = "disabled").
722 */
723struct device_node *of_get_next_available_child(const struct device_node *node,
724 struct device_node *prev)
725{
726 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000727 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000728
Florian Fainelli43cb4362014-05-28 10:39:02 -0700729 if (!node)
730 return NULL;
731
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000732 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000733 next = prev ? prev->sibling : node->child;
734 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700735 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000736 continue;
737 if (of_node_get(next))
738 break;
739 }
740 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000741 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000742 return next;
743}
744EXPORT_SYMBOL(of_get_next_available_child);
745
746/**
Rob Herringf1f207e2018-08-22 15:04:40 -0500747 * of_get_next_cpu_node - Iterate on cpu nodes
748 * @prev: previous child of the /cpus node, or NULL to get first
749 *
750 * Returns a cpu node pointer with refcount incremented, use of_node_put()
751 * on it when done. Returns NULL when prev is the last child. Decrements
752 * the refcount of prev.
753 */
754struct device_node *of_get_next_cpu_node(struct device_node *prev)
755{
756 struct device_node *next = NULL;
757 unsigned long flags;
758 struct device_node *node;
759
760 if (!prev)
761 node = of_find_node_by_path("/cpus");
762
763 raw_spin_lock_irqsave(&devtree_lock, flags);
764 if (prev)
765 next = prev->sibling;
766 else if (node) {
767 next = node->child;
768 of_node_put(node);
769 }
770 for (; next; next = next->sibling) {
771 if (!(of_node_name_eq(next, "cpu") ||
772 (next->type && !of_node_cmp(next->type, "cpu"))))
773 continue;
774 if (!__of_device_is_available(next))
775 continue;
776 if (of_node_get(next))
777 break;
778 }
779 of_node_put(prev);
780 raw_spin_unlock_irqrestore(&devtree_lock, flags);
781 return next;
782}
783EXPORT_SYMBOL(of_get_next_cpu_node);
784
785/**
Johan Hovold36156f92018-08-27 10:21:45 +0200786 * of_get_compatible_child - Find compatible child node
787 * @parent: parent node
788 * @compatible: compatible string
789 *
790 * Lookup child node whose compatible property contains the given compatible
791 * string.
792 *
793 * Returns a node pointer with refcount incremented, use of_node_put() on it
794 * when done; or NULL if not found.
795 */
796struct device_node *of_get_compatible_child(const struct device_node *parent,
797 const char *compatible)
798{
799 struct device_node *child;
800
801 for_each_child_of_node(parent, child) {
802 if (of_device_is_compatible(child, compatible))
803 break;
804 }
805
806 return child;
807}
808EXPORT_SYMBOL(of_get_compatible_child);
809
810/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100811 * of_get_child_by_name - Find the child node by name for a given parent
812 * @node: parent node
813 * @name: child name to look for.
814 *
815 * This function looks for child node for given matching name
816 *
817 * Returns a node pointer if found, with refcount incremented, use
818 * of_node_put() on it when done.
819 * Returns NULL if node is not found.
820 */
821struct device_node *of_get_child_by_name(const struct device_node *node,
822 const char *name)
823{
824 struct device_node *child;
825
826 for_each_child_of_node(node, child)
827 if (child->name && (of_node_cmp(child->name, name) == 0))
828 break;
829 return child;
830}
831EXPORT_SYMBOL(of_get_child_by_name);
832
Frank Rowande0a58f32017-10-17 16:36:31 -0700833struct device_node *__of_find_node_by_path(struct device_node *parent,
Grant Likelyc22e6502014-03-14 17:07:12 +0000834 const char *path)
835{
836 struct device_node *child;
Leif Lindholm106937e2015-03-06 16:52:53 +0000837 int len;
Grant Likelyc22e6502014-03-14 17:07:12 +0000838
Brian Norris721a09e2015-03-17 12:30:31 -0700839 len = strcspn(path, "/:");
Grant Likelyc22e6502014-03-14 17:07:12 +0000840 if (!len)
841 return NULL;
842
843 __for_each_child_of_node(parent, child) {
Rob Herring95e6b1f2017-06-01 18:00:00 -0500844 const char *name = kbasename(child->full_name);
Grant Likelyc22e6502014-03-14 17:07:12 +0000845 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
846 return child;
847 }
848 return NULL;
849}
850
Rob Herring27497e12017-06-02 12:43:18 -0500851struct device_node *__of_find_node_by_full_path(struct device_node *node,
852 const char *path)
853{
854 const char *separator = strchr(path, ':');
855
856 while (node && *path == '/') {
857 struct device_node *tmp = node;
858
859 path++; /* Increment past '/' delimiter */
860 node = __of_find_node_by_path(node, path);
861 of_node_put(tmp);
862 path = strchrnul(path, '/');
863 if (separator && separator < path)
864 break;
865 }
866 return node;
867}
868
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100869/**
Leif Lindholm75c28c02014-11-28 11:34:28 +0000870 * of_find_node_opts_by_path - Find a node matching a full OF path
Grant Likelyc22e6502014-03-14 17:07:12 +0000871 * @path: Either the full path to match, or if the path does not
872 * start with '/', the name of a property of the /aliases
873 * node (an alias). In the case of an alias, the node
874 * matching the alias' value will be returned.
Leif Lindholm75c28c02014-11-28 11:34:28 +0000875 * @opts: Address of a pointer into which to store the start of
876 * an options string appended to the end of the path with
877 * a ':' separator.
Grant Likelyc22e6502014-03-14 17:07:12 +0000878 *
879 * Valid paths:
880 * /foo/bar Full path
881 * foo Valid alias
882 * foo/bar Valid alias + relative path
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000883 *
884 * Returns a node pointer with refcount incremented, use
885 * of_node_put() on it when done.
886 */
Leif Lindholm75c28c02014-11-28 11:34:28 +0000887struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000888{
Grant Likelyc22e6502014-03-14 17:07:12 +0000889 struct device_node *np = NULL;
890 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500891 unsigned long flags;
Leif Lindholm75c28c02014-11-28 11:34:28 +0000892 const char *separator = strchr(path, ':');
893
894 if (opts)
895 *opts = separator ? separator + 1 : NULL;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000896
Grant Likelyc22e6502014-03-14 17:07:12 +0000897 if (strcmp(path, "/") == 0)
Grant Likely5063e252014-10-03 16:28:27 +0100898 return of_node_get(of_root);
Grant Likelyc22e6502014-03-14 17:07:12 +0000899
900 /* The path could begin with an alias */
901 if (*path != '/') {
Leif Lindholm106937e2015-03-06 16:52:53 +0000902 int len;
903 const char *p = separator;
904
905 if (!p)
906 p = strchrnul(path, '/');
907 len = p - path;
Grant Likelyc22e6502014-03-14 17:07:12 +0000908
909 /* of_aliases must not be NULL */
910 if (!of_aliases)
911 return NULL;
912
913 for_each_property_of_node(of_aliases, pp) {
914 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
915 np = of_find_node_by_path(pp->value);
916 break;
917 }
918 }
919 if (!np)
920 return NULL;
921 path = p;
922 }
923
924 /* Step down the tree matching path components */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500925 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyc22e6502014-03-14 17:07:12 +0000926 if (!np)
Grant Likely5063e252014-10-03 16:28:27 +0100927 np = of_node_get(of_root);
Rob Herring27497e12017-06-02 12:43:18 -0500928 np = __of_find_node_by_full_path(np, path);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500929 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000930 return np;
931}
Leif Lindholm75c28c02014-11-28 11:34:28 +0000932EXPORT_SYMBOL(of_find_node_opts_by_path);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000933
934/**
935 * of_find_node_by_name - Find a node by its "name" property
Stephen Boyd02a876b2017-11-17 08:53:21 -0800936 * @from: The node to start searching from or NULL; the node
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000937 * you pass will not be searched, only the next one
Stephen Boyd02a876b2017-11-17 08:53:21 -0800938 * will. Typically, you pass what the previous call
939 * returned. of_node_put() will be called on @from.
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000940 * @name: The name string to match against
941 *
942 * Returns a node pointer with refcount incremented, use
943 * of_node_put() on it when done.
944 */
945struct device_node *of_find_node_by_name(struct device_node *from,
946 const char *name)
947{
948 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500949 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000950
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500951 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100952 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000953 if (np->name && (of_node_cmp(np->name, name) == 0)
954 && of_node_get(np))
955 break;
956 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500957 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000958 return np;
959}
960EXPORT_SYMBOL(of_find_node_by_name);
961
962/**
963 * of_find_node_by_type - Find a node by its "device_type" property
964 * @from: The node to start searching from, or NULL to start searching
965 * the entire device tree. The node you pass will not be
966 * searched, only the next one will; typically, you pass
967 * what the previous call returned. of_node_put() will be
968 * called on from for you.
969 * @type: The type string to match against
970 *
971 * Returns a node pointer with refcount incremented, use
972 * of_node_put() on it when done.
973 */
974struct device_node *of_find_node_by_type(struct device_node *from,
975 const char *type)
976{
977 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500978 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000979
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500980 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100981 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000982 if (np->type && (of_node_cmp(np->type, type) == 0)
983 && of_node_get(np))
984 break;
985 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500986 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000987 return np;
988}
989EXPORT_SYMBOL(of_find_node_by_type);
990
991/**
992 * of_find_compatible_node - Find a node based on type and one of the
993 * tokens in its "compatible" property
994 * @from: The node to start searching from or NULL, the node
995 * you pass will not be searched, only the next one
996 * will; typically, you pass what the previous call
997 * returned. of_node_put() will be called on it
998 * @type: The type string to match "device_type" or NULL to ignore
999 * @compatible: The string to match to one of the tokens in the device
1000 * "compatible" list.
1001 *
1002 * Returns a node pointer with refcount incremented, use
1003 * of_node_put() on it when done.
1004 */
1005struct device_node *of_find_compatible_node(struct device_node *from,
1006 const char *type, const char *compatible)
1007{
1008 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001009 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001010
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001011 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001012 for_each_of_allnodes_from(from, np)
Kevin Hao215a14c2014-02-19 16:15:45 +08001013 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001014 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001015 break;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001016 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001017 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +10001018 return np;
1019}
1020EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +11001021
1022/**
Michael Ellerman1e291b142008-11-12 18:54:42 +00001023 * of_find_node_with_property - Find a node which has a property with
1024 * the given name.
1025 * @from: The node to start searching from or NULL, the node
1026 * you pass will not be searched, only the next one
1027 * will; typically, you pass what the previous call
1028 * returned. of_node_put() will be called on it
1029 * @prop_name: The name of the property to look for.
1030 *
1031 * Returns a node pointer with refcount incremented, use
1032 * of_node_put() on it when done.
1033 */
1034struct device_node *of_find_node_with_property(struct device_node *from,
1035 const char *prop_name)
1036{
1037 struct device_node *np;
1038 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001039 unsigned long flags;
Michael Ellerman1e291b142008-11-12 18:54:42 +00001040
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001041 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001042 for_each_of_allnodes_from(from, np) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +05301043 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b142008-11-12 18:54:42 +00001044 if (of_prop_cmp(pp->name, prop_name) == 0) {
1045 of_node_get(np);
1046 goto out;
1047 }
1048 }
1049 }
1050out:
1051 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001052 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b142008-11-12 18:54:42 +00001053 return np;
1054}
1055EXPORT_SYMBOL(of_find_node_with_property);
1056
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001057static
1058const struct of_device_id *__of_match_node(const struct of_device_id *matches,
1059 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +11001060{
Kevin Hao215a14c2014-02-19 16:15:45 +08001061 const struct of_device_id *best_match = NULL;
1062 int score, best_score = 0;
1063
Grant Likelya52f07e2011-03-18 10:21:29 -06001064 if (!matches)
1065 return NULL;
1066
Kevin Hao215a14c2014-02-19 16:15:45 +08001067 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
1068 score = __of_device_is_compatible(node, matches->compatible,
1069 matches->type, matches->name);
1070 if (score > best_score) {
1071 best_match = matches;
1072 best_score = score;
1073 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +08001074 }
Kevin Hao215a14c2014-02-19 16:15:45 +08001075
1076 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +11001077}
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001078
1079/**
Geert Uytterhoevenc50949d2014-10-22 11:44:54 +02001080 * of_match_node - Tell if a device_node has a matching of_match structure
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001081 * @matches: array of of device match structures to search in
1082 * @node: the of device structure to match against
1083 *
Kevin Hao71c54982014-02-18 15:57:29 +08001084 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001085 */
1086const struct of_device_id *of_match_node(const struct of_device_id *matches,
1087 const struct device_node *node)
1088{
1089 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001090 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001091
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001092 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001093 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001094 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001095 return match;
1096}
Grant Likely283029d2008-01-09 06:20:40 +11001097EXPORT_SYMBOL(of_match_node);
1098
1099/**
Stephen Warren50c8af42012-11-20 16:12:20 -07001100 * of_find_matching_node_and_match - Find a node based on an of_device_id
1101 * match table.
Grant Likely283029d2008-01-09 06:20:40 +11001102 * @from: The node to start searching from or NULL, the node
1103 * you pass will not be searched, only the next one
1104 * will; typically, you pass what the previous call
1105 * returned. of_node_put() will be called on it
1106 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -07001107 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +11001108 *
1109 * Returns a node pointer with refcount incremented, use
1110 * of_node_put() on it when done.
1111 */
Stephen Warren50c8af42012-11-20 16:12:20 -07001112struct device_node *of_find_matching_node_and_match(struct device_node *from,
1113 const struct of_device_id *matches,
1114 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +11001115{
1116 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001117 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001118 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +11001119
Stephen Warren50c8af42012-11-20 16:12:20 -07001120 if (match)
1121 *match = NULL;
1122
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001123 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001124 for_each_of_allnodes_from(from, np) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001125 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001126 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -07001127 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001128 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +11001129 break;
Stephen Warren50c8af42012-11-20 16:12:20 -07001130 }
Grant Likely283029d2008-01-09 06:20:40 +11001131 }
1132 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001133 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +11001134 return np;
1135}
Grant Likely80c20222012-12-19 10:45:36 +00001136EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -04001137
1138/**
Grant Likely3f07af42008-07-25 22:25:13 -04001139 * of_modalias_node - Lookup appropriate modalias for a device node
1140 * @node: pointer to a device tree node
1141 * @modalias: Pointer to buffer that modalias value will be copied into
1142 * @len: Length of modalias value
1143 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001144 * Based on the value of the compatible property, this routine will attempt
1145 * to choose an appropriate modalias value for a particular device tree node.
1146 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1147 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -04001148 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001149 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001150 */
1151int of_modalias_node(struct device_node *node, char *modalias, int len)
1152{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001153 const char *compatible, *p;
1154 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001155
1156 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001157 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001158 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001159 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001160 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001161 return 0;
1162}
1163EXPORT_SYMBOL_GPL(of_modalias_node);
1164
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001165/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001166 * of_find_node_by_phandle - Find a node given a phandle
1167 * @handle: phandle of the node to find
1168 *
1169 * Returns a node pointer with refcount incremented, use
1170 * of_node_put() on it when done.
1171 */
1172struct device_node *of_find_node_by_phandle(phandle handle)
1173{
Frank Rowand0b3ce782018-03-04 16:14:47 -08001174 struct device_node *np = NULL;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001175 unsigned long flags;
Frank Rowand0b3ce782018-03-04 16:14:47 -08001176 phandle masked_handle;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001177
Grant Likelyfc59b442014-10-02 13:08:02 +01001178 if (!handle)
1179 return NULL;
1180
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001181 raw_spin_lock_irqsave(&devtree_lock, flags);
Frank Rowand0b3ce782018-03-04 16:14:47 -08001182
1183 masked_handle = handle & phandle_cache_mask;
1184
1185 if (phandle_cache) {
1186 if (phandle_cache[masked_handle] &&
1187 handle == phandle_cache[masked_handle]->phandle)
1188 np = phandle_cache[masked_handle];
1189 }
1190
1191 if (!np) {
1192 for_each_of_allnodes(np)
1193 if (np->phandle == handle) {
1194 if (phandle_cache)
1195 phandle_cache[masked_handle] = np;
1196 break;
1197 }
1198 }
1199
Jeremy Kerr89751a72010-02-01 21:34:11 -07001200 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001201 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001202 return np;
1203}
1204EXPORT_SYMBOL(of_find_node_by_phandle);
1205
Grant Likely624cfca2013-10-11 22:05:10 +01001206void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1207{
1208 int i;
Rob Herring0d638a02017-06-01 15:50:55 -05001209 printk("%s %pOF", msg, args->np);
Marcin Nowakowski4aa66342016-12-01 09:00:55 +01001210 for (i = 0; i < args->args_count; i++) {
1211 const char delim = i ? ',' : ':';
1212
1213 pr_cont("%c%08x", delim, args->args[i]);
1214 }
1215 pr_cont("\n");
Grant Likely624cfca2013-10-11 22:05:10 +01001216}
1217
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001218int of_phandle_iterator_init(struct of_phandle_iterator *it,
1219 const struct device_node *np,
1220 const char *list_name,
1221 const char *cells_name,
1222 int cell_count)
1223{
1224 const __be32 *list;
1225 int size;
1226
1227 memset(it, 0, sizeof(*it));
1228
1229 list = of_get_property(np, list_name, &size);
1230 if (!list)
1231 return -ENOENT;
1232
1233 it->cells_name = cells_name;
1234 it->cell_count = cell_count;
1235 it->parent = np;
1236 it->list_end = list + size / sizeof(*list);
1237 it->phandle_end = list;
1238 it->cur = list;
1239
1240 return 0;
1241}
Kuninori Morimoto00bab232017-04-20 01:31:16 +00001242EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001243
Joerg Roedelcd209b42016-04-04 17:49:18 +02001244int of_phandle_iterator_next(struct of_phandle_iterator *it)
1245{
1246 uint32_t count = 0;
1247
1248 if (it->node) {
1249 of_node_put(it->node);
1250 it->node = NULL;
1251 }
1252
1253 if (!it->cur || it->phandle_end >= it->list_end)
1254 return -ENOENT;
1255
1256 it->cur = it->phandle_end;
1257
1258 /* If phandle is 0, then it is an empty entry with no arguments. */
1259 it->phandle = be32_to_cpup(it->cur++);
1260
1261 if (it->phandle) {
1262
1263 /*
1264 * Find the provider node and parse the #*-cells property to
1265 * determine the argument length.
1266 */
1267 it->node = of_find_node_by_phandle(it->phandle);
1268
1269 if (it->cells_name) {
1270 if (!it->node) {
Rob Herring0d638a02017-06-01 15:50:55 -05001271 pr_err("%pOF: could not find phandle\n",
1272 it->parent);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001273 goto err;
1274 }
1275
1276 if (of_property_read_u32(it->node, it->cells_name,
1277 &count)) {
Rob Herring0d638a02017-06-01 15:50:55 -05001278 pr_err("%pOF: could not get %s for %pOF\n",
1279 it->parent,
Joerg Roedelcd209b42016-04-04 17:49:18 +02001280 it->cells_name,
Rob Herring0d638a02017-06-01 15:50:55 -05001281 it->node);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001282 goto err;
1283 }
1284 } else {
1285 count = it->cell_count;
1286 }
1287
1288 /*
1289 * Make sure that the arguments actually fit in the remaining
1290 * property data length
1291 */
1292 if (it->cur + count > it->list_end) {
Rob Herring0d638a02017-06-01 15:50:55 -05001293 pr_err("%pOF: arguments longer than property\n",
1294 it->parent);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001295 goto err;
1296 }
1297 }
1298
1299 it->phandle_end = it->cur + count;
1300 it->cur_count = count;
1301
1302 return 0;
1303
1304err:
1305 if (it->node) {
1306 of_node_put(it->node);
1307 it->node = NULL;
1308 }
1309
1310 return -EINVAL;
1311}
Kuninori Morimoto00bab232017-04-20 01:31:16 +00001312EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001313
Joerg Roedelabdaa772016-04-04 17:49:21 +02001314int of_phandle_iterator_args(struct of_phandle_iterator *it,
1315 uint32_t *args,
1316 int size)
1317{
1318 int i, count;
1319
1320 count = it->cur_count;
1321
1322 if (WARN_ON(size < count))
1323 count = size;
1324
1325 for (i = 0; i < count; i++)
1326 args[i] = be32_to_cpup(it->cur++);
1327
1328 return count;
1329}
1330
Grant Likelybd69f732013-02-10 22:57:21 +00001331static int __of_parse_phandle_with_args(const struct device_node *np,
1332 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001333 const char *cells_name,
1334 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001335 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001336{
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001337 struct of_phandle_iterator it;
1338 int rc, cur_index = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001339
Grant Likely15c9a0a2011-12-12 09:25:57 -07001340 /* Loop over the phandles until all the requested entry is found */
Joerg Roedelf623ce92016-04-04 17:49:20 +02001341 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
Grant Likely15c9a0a2011-12-12 09:25:57 -07001342 /*
Joerg Roedelcd209b42016-04-04 17:49:18 +02001343 * All of the error cases bail out of the loop, so at
Grant Likely15c9a0a2011-12-12 09:25:57 -07001344 * this point, the parsing is successful. If the requested
1345 * index matches, then fill the out_args structure and return,
1346 * or return -ENOENT for an empty entry.
1347 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001348 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001349 if (cur_index == index) {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001350 if (!it.phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001351 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001352
Grant Likely15c9a0a2011-12-12 09:25:57 -07001353 if (out_args) {
Joerg Roedelabdaa772016-04-04 17:49:21 +02001354 int c;
1355
1356 c = of_phandle_iterator_args(&it,
1357 out_args->args,
1358 MAX_PHANDLE_ARGS);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001359 out_args->np = it.node;
Joerg Roedelabdaa772016-04-04 17:49:21 +02001360 out_args->args_count = c;
Tang Yuantianb855f162013-04-10 11:36:39 +08001361 } else {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001362 of_node_put(it.node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001363 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001364
1365 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001366 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001367 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001368
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001369 cur_index++;
1370 }
1371
Grant Likely23ce04c2013-02-12 21:21:49 +00001372 /*
1373 * Unlock node before returning result; will be one of:
1374 * -ENOENT : index is for empty phandle
1375 * -EINVAL : parsing error on data
1376 */
Joerg Roedelcd209b42016-04-04 17:49:18 +02001377
Grant Likely23ce04c2013-02-12 21:21:49 +00001378 err:
Markus Elfringbeab47d2016-07-19 22:42:22 +02001379 of_node_put(it.node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001380 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001381}
Grant Likelybd69f732013-02-10 22:57:21 +00001382
Stephen Warreneded9dd2013-08-14 15:27:08 -06001383/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001384 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1385 * @np: Pointer to device node holding phandle property
1386 * @phandle_name: Name of property holding a phandle value
1387 * @index: For properties holding a table of phandles, this is the index into
1388 * the table
1389 *
1390 * Returns the device_node pointer with refcount incremented. Use
1391 * of_node_put() on it when done.
1392 */
1393struct device_node *of_parse_phandle(const struct device_node *np,
1394 const char *phandle_name, int index)
1395{
Stephen Warren91d99422013-08-14 15:27:11 -06001396 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001397
Stephen Warren91d99422013-08-14 15:27:11 -06001398 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001399 return NULL;
1400
Stephen Warren91d99422013-08-14 15:27:11 -06001401 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1402 index, &args))
1403 return NULL;
1404
1405 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001406}
1407EXPORT_SYMBOL(of_parse_phandle);
1408
1409/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001410 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1411 * @np: pointer to a device tree node containing a list
1412 * @list_name: property name that contains a list
1413 * @cells_name: property name that specifies phandles' arguments count
1414 * @index: index of a phandle to parse out
1415 * @out_args: optional pointer to output arguments structure (will be filled)
1416 *
1417 * This function is useful to parse lists of phandles and their arguments.
1418 * Returns 0 on success and fills out_args, on error returns appropriate
1419 * errno value.
1420 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001421 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warreneded9dd2013-08-14 15:27:08 -06001422 * pointer.
1423 *
1424 * Example:
1425 *
1426 * phandle1: node1 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001427 * #list-cells = <2>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001428 * }
1429 *
1430 * phandle2: node2 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001431 * #list-cells = <1>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001432 * }
1433 *
1434 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001435 * list = <&phandle1 1 2 &phandle2 3>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001436 * }
1437 *
1438 * To get a device_node of the `node2' node you may call this:
1439 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1440 */
Grant Likelybd69f732013-02-10 22:57:21 +00001441int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1442 const char *cells_name, int index,
1443 struct of_phandle_args *out_args)
1444{
1445 if (index < 0)
1446 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001447 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1448 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001449}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001450EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001451
Grant Likelybd69f732013-02-10 22:57:21 +00001452/**
Stephen Boydbd6f2fd2018-01-30 18:36:16 -08001453 * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it
1454 * @np: pointer to a device tree node containing a list
1455 * @list_name: property name that contains a list
1456 * @stem_name: stem of property names that specify phandles' arguments count
1457 * @index: index of a phandle to parse out
1458 * @out_args: optional pointer to output arguments structure (will be filled)
1459 *
1460 * This function is useful to parse lists of phandles and their arguments.
1461 * Returns 0 on success and fills out_args, on error returns appropriate errno
1462 * value. The difference between this function and of_parse_phandle_with_args()
1463 * is that this API remaps a phandle if the node the phandle points to has
1464 * a <@stem_name>-map property.
1465 *
1466 * Caller is responsible to call of_node_put() on the returned out_args->np
1467 * pointer.
1468 *
1469 * Example:
1470 *
1471 * phandle1: node1 {
1472 * #list-cells = <2>;
1473 * }
1474 *
1475 * phandle2: node2 {
1476 * #list-cells = <1>;
1477 * }
1478 *
1479 * phandle3: node3 {
1480 * #list-cells = <1>;
1481 * list-map = <0 &phandle2 3>,
1482 * <1 &phandle2 2>,
1483 * <2 &phandle1 5 1>;
1484 * list-map-mask = <0x3>;
1485 * };
1486 *
1487 * node4 {
1488 * list = <&phandle1 1 2 &phandle3 0>;
1489 * }
1490 *
1491 * To get a device_node of the `node2' node you may call this:
1492 * of_parse_phandle_with_args(node4, "list", "list", 1, &args);
1493 */
1494int of_parse_phandle_with_args_map(const struct device_node *np,
1495 const char *list_name,
1496 const char *stem_name,
1497 int index, struct of_phandle_args *out_args)
1498{
1499 char *cells_name, *map_name = NULL, *mask_name = NULL;
1500 char *pass_name = NULL;
1501 struct device_node *cur, *new = NULL;
1502 const __be32 *map, *mask, *pass;
1503 static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 };
1504 static const __be32 dummy_pass[] = { [0 ... MAX_PHANDLE_ARGS] = 0 };
1505 __be32 initial_match_array[MAX_PHANDLE_ARGS];
1506 const __be32 *match_array = initial_match_array;
1507 int i, ret, map_len, match;
1508 u32 list_size, new_size;
1509
1510 if (index < 0)
1511 return -EINVAL;
1512
1513 cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name);
1514 if (!cells_name)
1515 return -ENOMEM;
1516
1517 ret = -ENOMEM;
1518 map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name);
1519 if (!map_name)
1520 goto free;
1521
1522 mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name);
1523 if (!mask_name)
1524 goto free;
1525
1526 pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name);
1527 if (!pass_name)
1528 goto free;
1529
1530 ret = __of_parse_phandle_with_args(np, list_name, cells_name, 0, index,
1531 out_args);
1532 if (ret)
1533 goto free;
1534
1535 /* Get the #<list>-cells property */
1536 cur = out_args->np;
1537 ret = of_property_read_u32(cur, cells_name, &list_size);
1538 if (ret < 0)
1539 goto put;
1540
1541 /* Precalculate the match array - this simplifies match loop */
1542 for (i = 0; i < list_size; i++)
1543 initial_match_array[i] = cpu_to_be32(out_args->args[i]);
1544
1545 ret = -EINVAL;
1546 while (cur) {
1547 /* Get the <list>-map property */
1548 map = of_get_property(cur, map_name, &map_len);
1549 if (!map) {
1550 ret = 0;
1551 goto free;
1552 }
1553 map_len /= sizeof(u32);
1554
1555 /* Get the <list>-map-mask property (optional) */
1556 mask = of_get_property(cur, mask_name, NULL);
1557 if (!mask)
1558 mask = dummy_mask;
1559 /* Iterate through <list>-map property */
1560 match = 0;
1561 while (map_len > (list_size + 1) && !match) {
1562 /* Compare specifiers */
1563 match = 1;
1564 for (i = 0; i < list_size; i++, map_len--)
1565 match &= !((match_array[i] ^ *map++) & mask[i]);
1566
1567 of_node_put(new);
1568 new = of_find_node_by_phandle(be32_to_cpup(map));
1569 map++;
1570 map_len--;
1571
1572 /* Check if not found */
1573 if (!new)
1574 goto put;
1575
1576 if (!of_device_is_available(new))
1577 match = 0;
1578
1579 ret = of_property_read_u32(new, cells_name, &new_size);
1580 if (ret)
1581 goto put;
1582
1583 /* Check for malformed properties */
1584 if (WARN_ON(new_size > MAX_PHANDLE_ARGS))
1585 goto put;
1586 if (map_len < new_size)
1587 goto put;
1588
1589 /* Move forward by new node's #<list>-cells amount */
1590 map += new_size;
1591 map_len -= new_size;
1592 }
1593 if (!match)
1594 goto put;
1595
1596 /* Get the <list>-map-pass-thru property (optional) */
1597 pass = of_get_property(cur, pass_name, NULL);
1598 if (!pass)
1599 pass = dummy_pass;
1600
1601 /*
1602 * Successfully parsed a <list>-map translation; copy new
1603 * specifier into the out_args structure, keeping the
1604 * bits specified in <list>-map-pass-thru.
1605 */
1606 match_array = map - new_size;
1607 for (i = 0; i < new_size; i++) {
1608 __be32 val = *(map - new_size + i);
1609
1610 if (i < list_size) {
1611 val &= ~pass[i];
1612 val |= cpu_to_be32(out_args->args[i]) & pass[i];
1613 }
1614
1615 out_args->args[i] = be32_to_cpu(val);
1616 }
1617 out_args->args_count = list_size = new_size;
1618 /* Iterate again with new provider */
1619 out_args->np = new;
1620 of_node_put(cur);
1621 cur = new;
1622 }
1623put:
1624 of_node_put(cur);
1625 of_node_put(new);
1626free:
1627 kfree(mask_name);
1628 kfree(map_name);
1629 kfree(cells_name);
1630 kfree(pass_name);
1631
1632 return ret;
1633}
1634EXPORT_SYMBOL(of_parse_phandle_with_args_map);
1635
1636/**
Stephen Warren035fd942013-08-14 15:27:10 -06001637 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1638 * @np: pointer to a device tree node containing a list
1639 * @list_name: property name that contains a list
1640 * @cell_count: number of argument cells following the phandle
1641 * @index: index of a phandle to parse out
1642 * @out_args: optional pointer to output arguments structure (will be filled)
1643 *
1644 * This function is useful to parse lists of phandles and their arguments.
1645 * Returns 0 on success and fills out_args, on error returns appropriate
1646 * errno value.
1647 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001648 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warren035fd942013-08-14 15:27:10 -06001649 * pointer.
1650 *
1651 * Example:
1652 *
1653 * phandle1: node1 {
1654 * }
1655 *
1656 * phandle2: node2 {
1657 * }
1658 *
1659 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001660 * list = <&phandle1 0 2 &phandle2 2 3>;
Stephen Warren035fd942013-08-14 15:27:10 -06001661 * }
1662 *
1663 * To get a device_node of the `node2' node you may call this:
1664 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1665 */
1666int of_parse_phandle_with_fixed_args(const struct device_node *np,
1667 const char *list_name, int cell_count,
1668 int index, struct of_phandle_args *out_args)
1669{
1670 if (index < 0)
1671 return -EINVAL;
1672 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1673 index, out_args);
1674}
1675EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1676
1677/**
Grant Likelybd69f732013-02-10 22:57:21 +00001678 * of_count_phandle_with_args() - Find the number of phandles references in a property
1679 * @np: pointer to a device tree node containing a list
1680 * @list_name: property name that contains a list
1681 * @cells_name: property name that specifies phandles' arguments count
1682 *
1683 * Returns the number of phandle + argument tuples within a property. It
1684 * is a typical pattern to encode a list of phandle and variable
1685 * arguments into a single property. The number of arguments is encoded
1686 * by a property in the phandle-target node. For example, a gpios
1687 * property would contain a list of GPIO specifies consisting of a
1688 * phandle and 1 or more arguments. The number of arguments are
1689 * determined by the #gpio-cells property in the node pointed to by the
1690 * phandle.
1691 */
1692int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1693 const char *cells_name)
1694{
Joerg Roedel2021bd02016-04-04 17:49:19 +02001695 struct of_phandle_iterator it;
1696 int rc, cur_index = 0;
1697
1698 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
1699 if (rc)
1700 return rc;
1701
1702 while ((rc = of_phandle_iterator_next(&it)) == 0)
1703 cur_index += 1;
1704
1705 if (rc != -ENOENT)
1706 return rc;
1707
1708 return cur_index;
Grant Likelybd69f732013-02-10 22:57:21 +00001709}
1710EXPORT_SYMBOL(of_count_phandle_with_args);
1711
Grant Likely02af11b2009-11-23 20:16:45 -07001712/**
Xiubo Li62664f62014-01-22 13:57:39 +08001713 * __of_add_property - Add a property to a node without lock operations
1714 */
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001715int __of_add_property(struct device_node *np, struct property *prop)
Xiubo Li62664f62014-01-22 13:57:39 +08001716{
1717 struct property **next;
1718
1719 prop->next = NULL;
1720 next = &np->properties;
1721 while (*next) {
1722 if (strcmp(prop->name, (*next)->name) == 0)
1723 /* duplicate ! don't insert it */
1724 return -EEXIST;
1725
1726 next = &(*next)->next;
1727 }
1728 *next = prop;
1729
1730 return 0;
1731}
1732
1733/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001734 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001735 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001736int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001737{
Grant Likely02af11b2009-11-23 20:16:45 -07001738 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001739 int rc;
1740
Grant Likely8a2b22a22014-07-23 17:05:06 -06001741 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001742
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001743 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001744 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001745 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001746
Grant Likely8a2b22a22014-07-23 17:05:06 -06001747 if (!rc)
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001748 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001749
Grant Likely8a2b22a22014-07-23 17:05:06 -06001750 mutex_unlock(&of_mutex);
1751
Grant Likely259092a2014-07-16 12:48:23 -06001752 if (!rc)
1753 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1754
Xiubo Li62664f62014-01-22 13:57:39 +08001755 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001756}
1757
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001758int __of_remove_property(struct device_node *np, struct property *prop)
1759{
1760 struct property **next;
1761
1762 for (next = &np->properties; *next; next = &(*next)->next) {
1763 if (*next == prop)
1764 break;
1765 }
1766 if (*next == NULL)
1767 return -ENODEV;
1768
1769 /* found the node */
1770 *next = prop->next;
1771 prop->next = np->deadprops;
1772 np->deadprops = prop;
1773
1774 return 0;
1775}
1776
Grant Likely02af11b2009-11-23 20:16:45 -07001777/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001778 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001779 *
1780 * Note that we don't actually remove it, since we have given out
1781 * who-knows-how-many pointers to the data using get-property.
1782 * Instead we just move the property to the "dead properties"
1783 * list, so it won't be found any more.
1784 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001785int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001786{
Grant Likely02af11b2009-11-23 20:16:45 -07001787 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001788 int rc;
1789
Suraj Jitindar Singh201b3fe2016-04-28 15:34:54 +10001790 if (!prop)
1791 return -ENODEV;
1792
Grant Likely8a2b22a22014-07-23 17:05:06 -06001793 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001794
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001795 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001796 rc = __of_remove_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001797 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001798
Grant Likely8a2b22a22014-07-23 17:05:06 -06001799 if (!rc)
1800 __of_remove_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001801
Grant Likely8a2b22a22014-07-23 17:05:06 -06001802 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +00001803
Grant Likely259092a2014-07-16 12:48:23 -06001804 if (!rc)
1805 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1806
Grant Likely8a2b22a22014-07-23 17:05:06 -06001807 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001808}
1809
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001810int __of_update_property(struct device_node *np, struct property *newprop,
1811 struct property **oldpropp)
1812{
1813 struct property **next, *oldprop;
1814
1815 for (next = &np->properties; *next; next = &(*next)->next) {
1816 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1817 break;
1818 }
1819 *oldpropp = oldprop = *next;
1820
1821 if (oldprop) {
1822 /* replace the node */
1823 newprop->next = oldprop->next;
1824 *next = newprop;
1825 oldprop->next = np->deadprops;
1826 np->deadprops = oldprop;
1827 } else {
1828 /* new node */
1829 newprop->next = NULL;
1830 *next = newprop;
1831 }
Grant Likely02af11b2009-11-23 20:16:45 -07001832
1833 return 0;
1834}
1835
1836/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001837 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001838 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001839 *
1840 * Note that we don't actually remove it, since we have given out
1841 * who-knows-how-many pointers to the data using get-property.
1842 * Instead we just move the property to the "dead properties" list,
1843 * and add the new property to the property list
1844 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001845int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001846{
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001847 struct property *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001848 unsigned long flags;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001849 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001850
Dong Aisheng475d0092012-07-11 15:16:37 +10001851 if (!newprop->name)
1852 return -EINVAL;
1853
Grant Likely8a2b22a22014-07-23 17:05:06 -06001854 mutex_lock(&of_mutex);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001855
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001856 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001857 rc = __of_update_property(np, newprop, &oldprop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001858 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001859
Grant Likely8a2b22a22014-07-23 17:05:06 -06001860 if (!rc)
1861 __of_update_property_sysfs(np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001862
Grant Likely8a2b22a22014-07-23 17:05:06 -06001863 mutex_unlock(&of_mutex);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001864
Grant Likely259092a2014-07-16 12:48:23 -06001865 if (!rc)
1866 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001867
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001868 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001869}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001870
Shawn Guo611cad72011-08-15 15:28:14 +08001871static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1872 int id, const char *stem, int stem_len)
1873{
1874 ap->np = np;
1875 ap->id = id;
1876 strncpy(ap->stem, stem, stem_len);
1877 ap->stem[stem_len] = 0;
1878 list_add_tail(&ap->link, &aliases_lookup);
Rob Herring0d638a02017-06-01 15:50:55 -05001879 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1880 ap->alias, ap->stem, ap->id, np);
Shawn Guo611cad72011-08-15 15:28:14 +08001881}
1882
1883/**
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001884 * of_alias_scan - Scan all properties of the 'aliases' node
Shawn Guo611cad72011-08-15 15:28:14 +08001885 *
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001886 * The function scans all the properties of the 'aliases' node and populates
1887 * the global lookup table with the properties. It returns the
1888 * number of alias properties found, or an error code in case of failure.
Shawn Guo611cad72011-08-15 15:28:14 +08001889 *
1890 * @dt_alloc: An allocator that provides a virtual address to memory
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001891 * for storing the resulting tree
Shawn Guo611cad72011-08-15 15:28:14 +08001892 */
1893void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1894{
1895 struct property *pp;
1896
Laurentiu Tudor7dbe5842014-08-27 17:09:39 +03001897 of_aliases = of_find_node_by_path("/aliases");
Shawn Guo611cad72011-08-15 15:28:14 +08001898 of_chosen = of_find_node_by_path("/chosen");
1899 if (of_chosen == NULL)
1900 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02001901
1902 if (of_chosen) {
Grant Likelya752ee52014-03-28 08:12:18 -07001903 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
Sergei Shtylyovb0d9d922017-07-23 19:55:48 +03001904 const char *name = NULL;
1905
1906 if (of_property_read_string(of_chosen, "stdout-path", &name))
1907 of_property_read_string(of_chosen, "linux,stdout-path",
1908 &name);
Grant Likelya752ee52014-03-28 08:12:18 -07001909 if (IS_ENABLED(CONFIG_PPC) && !name)
Sergei Shtylyovb0d9d922017-07-23 19:55:48 +03001910 of_property_read_string(of_aliases, "stdout", &name);
Peter Hurleyf64255b2015-03-17 16:46:33 -04001911 if (name)
Leif Lindholm7914a7c2014-11-27 17:56:07 +00001912 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001913 }
1914
Shawn Guo611cad72011-08-15 15:28:14 +08001915 if (!of_aliases)
1916 return;
1917
Dong Aisheng8af0da92011-12-22 20:19:24 +08001918 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001919 const char *start = pp->name;
1920 const char *end = start + strlen(start);
1921 struct device_node *np;
1922 struct alias_prop *ap;
1923 int id, len;
1924
1925 /* Skip those we do not want to proceed */
1926 if (!strcmp(pp->name, "name") ||
1927 !strcmp(pp->name, "phandle") ||
1928 !strcmp(pp->name, "linux,phandle"))
1929 continue;
1930
1931 np = of_find_node_by_path(pp->value);
1932 if (!np)
1933 continue;
1934
1935 /* walk the alias backwards to extract the id and work out
1936 * the 'stem' string */
1937 while (isdigit(*(end-1)) && end > start)
1938 end--;
1939 len = end - start;
1940
1941 if (kstrtoint(end, 10, &id) < 0)
1942 continue;
1943
1944 /* Allocate an alias_prop with enough space for the stem */
Paul Burtonde96ec22016-09-23 16:38:27 +01001945 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
Shawn Guo611cad72011-08-15 15:28:14 +08001946 if (!ap)
1947 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01001948 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001949 ap->alias = start;
1950 of_alias_add(ap, np, id, start, len);
1951 }
1952}
1953
1954/**
1955 * of_alias_get_id - Get alias id for the given device_node
1956 * @np: Pointer to the given device_node
1957 * @stem: Alias stem of the given device_node
1958 *
Geert Uytterhoeven5a53a072014-03-11 11:23:38 +01001959 * The function travels the lookup table to get the alias id for the given
1960 * device_node and alias stem. It returns the alias id if found.
Shawn Guo611cad72011-08-15 15:28:14 +08001961 */
1962int of_alias_get_id(struct device_node *np, const char *stem)
1963{
1964 struct alias_prop *app;
1965 int id = -ENODEV;
1966
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001967 mutex_lock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001968 list_for_each_entry(app, &aliases_lookup, link) {
1969 if (strcmp(app->stem, stem) != 0)
1970 continue;
1971
1972 if (np == app->np) {
1973 id = app->id;
1974 break;
1975 }
1976 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001977 mutex_unlock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001978
1979 return id;
1980}
1981EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001982
Wolfram Sang351d2242015-03-12 17:17:58 +01001983/**
1984 * of_alias_get_highest_id - Get highest alias id for the given stem
1985 * @stem: Alias stem to be examined
1986 *
1987 * The function travels the lookup table to get the highest alias id for the
1988 * given alias stem. It returns the alias id if found.
1989 */
1990int of_alias_get_highest_id(const char *stem)
1991{
1992 struct alias_prop *app;
1993 int id = -ENODEV;
1994
1995 mutex_lock(&of_mutex);
1996 list_for_each_entry(app, &aliases_lookup, link) {
1997 if (strcmp(app->stem, stem) != 0)
1998 continue;
1999
2000 if (app->id > id)
2001 id = app->id;
2002 }
2003 mutex_unlock(&of_mutex);
2004
2005 return id;
2006}
2007EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
2008
Sascha Hauer5c19e952013-08-05 14:40:44 +02002009/**
Grant Likely3482f2c2014-03-27 17:18:55 -07002010 * of_console_check() - Test and setup console for DT setup
2011 * @dn - Pointer to device node
2012 * @name - Name to use for preferred console without index. ex. "ttyS"
2013 * @index - Index to use for preferred console.
Sascha Hauer5c19e952013-08-05 14:40:44 +02002014 *
Grant Likely3482f2c2014-03-27 17:18:55 -07002015 * Check if the given device node matches the stdout-path property in the
2016 * /chosen node. If it does then register it as the preferred console and return
2017 * TRUE. Otherwise return FALSE.
Sascha Hauer5c19e952013-08-05 14:40:44 +02002018 */
Grant Likely3482f2c2014-03-27 17:18:55 -07002019bool of_console_check(struct device_node *dn, char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +02002020{
Grant Likely3482f2c2014-03-27 17:18:55 -07002021 if (!dn || dn != of_stdout || console_set_on_cmdline)
Sascha Hauer5c19e952013-08-05 14:40:44 +02002022 return false;
Sergey Senozhatskydb179e02017-09-26 15:25:10 +09002023
2024 /*
2025 * XXX: cast `options' to char pointer to suppress complication
2026 * warnings: printk, UART and console drivers expect char pointer.
2027 */
2028 return !add_preferred_console(name, index, (char *)of_stdout_options);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002029}
Grant Likely3482f2c2014-03-27 17:18:55 -07002030EXPORT_SYMBOL_GPL(of_console_check);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002031
2032/**
2033 * of_find_next_cache_node - Find a node's subsidiary cache
2034 * @np: node of type "cpu" or "cache"
2035 *
2036 * Returns a node pointer with refcount incremented, use
2037 * of_node_put() on it when done. Caller should hold a reference
2038 * to np.
2039 */
2040struct device_node *of_find_next_cache_node(const struct device_node *np)
2041{
Rob Herring91d96742017-05-04 12:30:07 -05002042 struct device_node *child, *cache_node;
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002043
Rob Herring91d96742017-05-04 12:30:07 -05002044 cache_node = of_parse_phandle(np, "l2-cache", 0);
2045 if (!cache_node)
2046 cache_node = of_parse_phandle(np, "next-level-cache", 0);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002047
Rob Herring91d96742017-05-04 12:30:07 -05002048 if (cache_node)
2049 return cache_node;
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002050
2051 /* OF on pmac has nodes instead of properties named "l2-cache"
2052 * beneath CPU nodes.
2053 */
Rob Herringf6707fd2018-08-27 09:50:09 -05002054 if (IS_ENABLED(CONFIG_PPC_PMAC) && !strcmp(np->type, "cpu"))
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002055 for_each_child_of_node(np, child)
2056 if (!strcmp(child->type, "cache"))
2057 return child;
2058
2059 return NULL;
2060}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002061
2062/**
Sudeep Holla5fa23532017-01-16 10:40:43 +00002063 * of_find_last_cache_level - Find the level at which the last cache is
2064 * present for the given logical cpu
2065 *
2066 * @cpu: cpu number(logical index) for which the last cache level is needed
2067 *
2068 * Returns the the level at which the last cache is present. It is exactly
2069 * same as the total number of cache levels for the given logical cpu.
2070 */
2071int of_find_last_cache_level(unsigned int cpu)
2072{
2073 u32 cache_level = 0;
2074 struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
2075
2076 while (np) {
2077 prev = np;
2078 of_node_put(np);
2079 np = of_find_next_cache_node(np);
2080 }
2081
2082 of_property_read_u32(prev, "cache-level", &cache_level);
2083
2084 return cache_level;
2085}