blob: 33011b88ed3f0192d827753ef13c8559beeadb9e [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
Michal Simekb1078c32018-09-20 13:41:52 +020019#include <linux/bitmap.h>
Grant Likely3482f2c2014-03-27 17:18:55 -070020#include <linux/console.h>
Shawn Guo611cad72011-08-15 15:28:14 +080021#include <linux/ctype.h>
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +010022#include <linux/cpu.h>
Stephen Rothwell97e873e2007-05-01 16:26:07 +100023#include <linux/module.h>
24#include <linux/of.h>
Sudeep Holla5fa23532017-01-16 10:40:43 +000025#include <linux/of_device.h>
Philipp Zabelfd9fdb72014-02-10 22:01:48 +010026#include <linux/of_graph.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100027#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Grant Likely75b57ec2014-02-20 18:02:11 +000029#include <linux/string.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070030#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100031
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080032#include "of_private.h"
Shawn Guo611cad72011-08-15 15:28:14 +080033
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080034LIST_HEAD(aliases_lookup);
Shawn Guo611cad72011-08-15 15:28:14 +080035
Grant Likely5063e252014-10-03 16:28:27 +010036struct device_node *of_root;
37EXPORT_SYMBOL(of_root);
Grant Likelyfc0bdae2010-02-14 07:13:55 -070038struct device_node *of_chosen;
Shawn Guo611cad72011-08-15 15:28:14 +080039struct device_node *of_aliases;
Grant Likelya752ee52014-03-28 08:12:18 -070040struct device_node *of_stdout;
Leif Lindholm7914a7c2014-11-27 17:56:07 +000041static const char *of_stdout_options;
Shawn Guo611cad72011-08-15 15:28:14 +080042
Grant Likely8a2b22a22014-07-23 17:05:06 -060043struct kset *of_kset;
Grant Likely75b57ec2014-02-20 18:02:11 +000044
45/*
Grant Likely8a2b22a22014-07-23 17:05:06 -060046 * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
47 * This mutex must be held whenever modifications are being made to the
48 * device tree. The of_{attach,detach}_node() and
49 * of_{add,remove,update}_property() helpers make sure this happens.
Grant Likely75b57ec2014-02-20 18:02:11 +000050 */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +030051DEFINE_MUTEX(of_mutex);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100052
Grant Likely5063e252014-10-03 16:28:27 +010053/* use when traversing tree through the child, sibling,
Stephen Rothwell581b6052007-04-24 16:46:53 +100054 * or parent members of struct device_node.
55 */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -050056DEFINE_RAW_SPINLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100057
Rob Herringf42b0e182018-08-27 07:50:47 -050058bool of_node_name_eq(const struct device_node *np, const char *name)
59{
60 const char *node_name;
61 size_t len;
62
63 if (!np)
64 return false;
65
66 node_name = kbasename(np->full_name);
67 len = strchrnul(node_name, '@') - node_name;
68
69 return (strlen(name) == len) && (strncmp(node_name, name, len) == 0);
70}
71
72bool of_node_name_prefix(const struct device_node *np, const char *prefix)
73{
74 if (!np)
75 return false;
76
77 return strncmp(kbasename(np->full_name), prefix, strlen(prefix)) == 0;
78}
79
Stephen Rothwell97e873e2007-05-01 16:26:07 +100080int of_n_addr_cells(struct device_node *np)
81{
Sergei Shtylyov88329632017-07-23 19:55:47 +030082 u32 cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100083
84 do {
85 if (np->parent)
86 np = np->parent;
Sergei Shtylyov88329632017-07-23 19:55:47 +030087 if (!of_property_read_u32(np, "#address-cells", &cells))
88 return cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100089 } while (np->parent);
90 /* No #address-cells property for the root node */
91 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
92}
93EXPORT_SYMBOL(of_n_addr_cells);
94
95int of_n_size_cells(struct device_node *np)
96{
Sergei Shtylyov88329632017-07-23 19:55:47 +030097 u32 cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100098
99 do {
100 if (np->parent)
101 np = np->parent;
Sergei Shtylyov88329632017-07-23 19:55:47 +0300102 if (!of_property_read_u32(np, "#size-cells", &cells))
103 return cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000104 } while (np->parent);
105 /* No #size-cells property for the root node */
106 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
107}
108EXPORT_SYMBOL(of_n_size_cells);
109
Rob Herring0c3f0612013-09-17 10:42:50 -0500110#ifdef CONFIG_NUMA
111int __weak of_node_to_nid(struct device_node *np)
112{
Konstantin Khlebnikovc8fff7b2015-04-08 19:59:20 +0300113 return NUMA_NO_NODE;
Rob Herring0c3f0612013-09-17 10:42:50 -0500114}
115#endif
116
Frank Rowand0b3ce782018-03-04 16:14:47 -0800117static struct device_node **phandle_cache;
118static u32 phandle_cache_mask;
119
120/*
121 * Assumptions behind phandle_cache implementation:
122 * - phandle property values are in a contiguous range of 1..n
123 *
124 * If the assumptions do not hold, then
125 * - the phandle lookup overhead reduction provided by the cache
126 * will likely be less
127 */
Frank Rowandb9952b52018-07-12 14:00:07 -0700128void of_populate_phandle_cache(void)
Frank Rowand0b3ce782018-03-04 16:14:47 -0800129{
130 unsigned long flags;
131 u32 cache_entries;
132 struct device_node *np;
133 u32 phandles = 0;
134
135 raw_spin_lock_irqsave(&devtree_lock, flags);
136
137 kfree(phandle_cache);
138 phandle_cache = NULL;
139
140 for_each_of_allnodes(np)
141 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
142 phandles++;
143
Rob Herringe54192b2018-09-11 09:28:14 -0500144 if (!phandles)
145 goto out;
146
Frank Rowand0b3ce782018-03-04 16:14:47 -0800147 cache_entries = roundup_pow_of_two(phandles);
148 phandle_cache_mask = cache_entries - 1;
149
150 phandle_cache = kcalloc(cache_entries, sizeof(*phandle_cache),
151 GFP_ATOMIC);
152 if (!phandle_cache)
153 goto out;
154
155 for_each_of_allnodes(np)
156 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
157 phandle_cache[np->phandle & phandle_cache_mask] = np;
158
159out:
160 raw_spin_unlock_irqrestore(&devtree_lock, flags);
161}
162
Frank Rowandb9952b52018-07-12 14:00:07 -0700163int of_free_phandle_cache(void)
Frank Rowand0b3ce782018-03-04 16:14:47 -0800164{
165 unsigned long flags;
166
167 raw_spin_lock_irqsave(&devtree_lock, flags);
168
169 kfree(phandle_cache);
170 phandle_cache = NULL;
171
172 raw_spin_unlock_irqrestore(&devtree_lock, flags);
173
174 return 0;
175}
Frank Rowandb9952b52018-07-12 14:00:07 -0700176#if !defined(CONFIG_MODULES)
Frank Rowand0b3ce782018-03-04 16:14:47 -0800177late_initcall_sync(of_free_phandle_cache);
178#endif
179
Sudeep Holla194ec932015-05-14 15:28:24 +0100180void __init of_core_init(void)
Grant Likely75b57ec2014-02-20 18:02:11 +0000181{
182 struct device_node *np;
183
Frank Rowand0b3ce782018-03-04 16:14:47 -0800184 of_populate_phandle_cache();
185
Grant Likely75b57ec2014-02-20 18:02:11 +0000186 /* Create the kset, and register existing nodes */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300187 mutex_lock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000188 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
189 if (!of_kset) {
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300190 mutex_unlock(&of_mutex);
Rob Herring606ad422016-06-15 08:32:18 -0500191 pr_err("failed to register existing nodes\n");
Sudeep Holla194ec932015-05-14 15:28:24 +0100192 return;
Grant Likely75b57ec2014-02-20 18:02:11 +0000193 }
194 for_each_of_allnodes(np)
Grant Likely8a2b22a22014-07-23 17:05:06 -0600195 __of_attach_node_sysfs(np);
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300196 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000197
Grant Likely83570412012-11-06 21:03:27 +0000198 /* Symlink in /proc as required by userspace ABI */
Grant Likely5063e252014-10-03 16:28:27 +0100199 if (of_root)
Grant Likely75b57ec2014-02-20 18:02:11 +0000200 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000201}
Grant Likely75b57ec2014-02-20 18:02:11 +0000202
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500203static struct property *__of_find_property(const struct device_node *np,
204 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000205{
206 struct property *pp;
207
Timur Tabi64e45662008-05-08 05:19:59 +1000208 if (!np)
209 return NULL;
210
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530211 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000212 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530213 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000214 *lenp = pp->length;
215 break;
216 }
217 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500218
219 return pp;
220}
221
222struct property *of_find_property(const struct device_node *np,
223 const char *name,
224 int *lenp)
225{
226 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500227 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500228
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500229 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500230 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500231 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000232
233 return pp;
234}
235EXPORT_SYMBOL(of_find_property);
236
Grant Likely5063e252014-10-03 16:28:27 +0100237struct device_node *__of_find_all_nodes(struct device_node *prev)
238{
239 struct device_node *np;
240 if (!prev) {
241 np = of_root;
242 } else if (prev->child) {
243 np = prev->child;
244 } else {
245 /* Walk back up looking for a sibling, or the end of the structure */
246 np = prev;
247 while (np->parent && !np->sibling)
248 np = np->parent;
249 np = np->sibling; /* Might be null at the end of the tree */
250 }
251 return np;
252}
253
Grant Likelye91edcf2009-10-15 10:58:09 -0600254/**
255 * of_find_all_nodes - Get next node in global list
256 * @prev: Previous node or NULL to start iteration
257 * of_node_put() will be called on it
258 *
259 * Returns a node pointer with refcount incremented, use
260 * of_node_put() on it when done.
261 */
262struct device_node *of_find_all_nodes(struct device_node *prev)
263{
264 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000265 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600266
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000267 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100268 np = __of_find_all_nodes(prev);
269 of_node_get(np);
Grant Likelye91edcf2009-10-15 10:58:09 -0600270 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000271 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600272 return np;
273}
274EXPORT_SYMBOL(of_find_all_nodes);
275
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000276/*
277 * Find a property with a given name for a given node
278 * and return the value.
279 */
Grant Likelya25095d2014-07-15 23:25:43 -0600280const void *__of_get_property(const struct device_node *np,
281 const char *name, int *lenp)
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500282{
283 struct property *pp = __of_find_property(np, name, lenp);
284
285 return pp ? pp->value : NULL;
286}
287
288/*
289 * Find a property with a given name for a given node
290 * and return the value.
291 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000292const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500293 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000294{
295 struct property *pp = of_find_property(np, name, lenp);
296
297 return pp ? pp->value : NULL;
298}
299EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000300
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100301/*
302 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
303 *
304 * @cpu: logical cpu index of a core/thread
305 * @phys_id: physical identifier of a core/thread
306 *
307 * CPU logical to physical index mapping is architecture specific.
308 * However this __weak function provides a default match of physical
309 * id to logical cpu index. phys_id provided here is usually values read
310 * from the device tree which must match the hardware internal registers.
311 *
312 * Returns true if the physical identifier and the logical cpu index
313 * correspond to the same core/thread, false otherwise.
314 */
315bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
316{
317 return (u32)phys_id == cpu;
318}
319
320/**
321 * Checks if the given "prop_name" property holds the physical id of the
322 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
323 * NULL, local thread number within the core is returned in it.
324 */
325static bool __of_find_n_match_cpu_property(struct device_node *cpun,
326 const char *prop_name, int cpu, unsigned int *thread)
327{
328 const __be32 *cell;
329 int ac, prop_len, tid;
330 u64 hwid;
331
332 ac = of_n_addr_cells(cpun);
333 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100334 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100335 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100336 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100337 for (tid = 0; tid < prop_len; tid++) {
338 hwid = of_read_number(cell, ac);
339 if (arch_match_cpu_phys_id(cpu, hwid)) {
340 if (thread)
341 *thread = tid;
342 return true;
343 }
344 cell += ac;
345 }
346 return false;
347}
348
David Millerd1cb9d12013-10-03 17:24:51 -0400349/*
350 * arch_find_n_match_cpu_physical_id - See if the given device node is
351 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
352 * else false. If 'thread' is non-NULL, the local thread number within the
353 * core is returned in it.
354 */
355bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
356 int cpu, unsigned int *thread)
357{
358 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
359 * for thread ids on PowerPC. If it doesn't exist fallback to
360 * standard "reg" property.
361 */
362 if (IS_ENABLED(CONFIG_PPC) &&
363 __of_find_n_match_cpu_property(cpun,
364 "ibm,ppc-interrupt-server#s",
365 cpu, thread))
366 return true;
367
Masahiro Yamada510bd062015-10-28 12:05:27 +0900368 return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
David Millerd1cb9d12013-10-03 17:24:51 -0400369}
370
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100371/**
372 * of_get_cpu_node - Get device node associated with the given logical CPU
373 *
374 * @cpu: CPU number(logical index) for which device node is required
375 * @thread: if not NULL, local thread number within the physical core is
376 * returned
377 *
378 * The main purpose of this function is to retrieve the device node for the
379 * given logical CPU index. It should be used to initialize the of_node in
380 * cpu device. Once of_node in cpu device is populated, all the further
381 * references can use that instead.
382 *
383 * CPU logical to physical index mapping is architecture specific and is built
384 * before booting secondary cores. This function uses arch_match_cpu_phys_id
385 * which can be overridden by architecture specific implementation.
386 *
Masahiro Yamada1c986e32016-04-20 10:18:46 +0900387 * Returns a node pointer for the logical cpu with refcount incremented, use
388 * of_node_put() on it when done. Returns NULL if not found.
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100389 */
390struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
391{
David Millerd1cb9d12013-10-03 17:24:51 -0400392 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100393
David Millerd1cb9d12013-10-03 17:24:51 -0400394 for_each_node_by_type(cpun, "cpu") {
395 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100396 return cpun;
397 }
398 return NULL;
399}
400EXPORT_SYMBOL(of_get_cpu_node);
401
Kevin Hao215a14c2014-02-19 16:15:45 +0800402/**
Suzuki K Poulosea0e71cd2018-01-02 11:25:27 +0000403 * of_cpu_node_to_id: Get the logical CPU number for a given device_node
404 *
405 * @cpu_node: Pointer to the device_node for CPU.
406 *
407 * Returns the logical CPU number of the given CPU device_node.
408 * Returns -ENODEV if the CPU is not found.
409 */
410int of_cpu_node_to_id(struct device_node *cpu_node)
411{
412 int cpu;
413 bool found = false;
414 struct device_node *np;
415
416 for_each_possible_cpu(cpu) {
417 np = of_cpu_device_node_get(cpu);
418 found = (cpu_node == np);
419 of_node_put(np);
420 if (found)
421 return cpu;
422 }
423
424 return -ENODEV;
425}
426EXPORT_SYMBOL(of_cpu_node_to_id);
427
428/**
Kevin Hao215a14c2014-02-19 16:15:45 +0800429 * __of_device_is_compatible() - Check if the node matches given constraints
430 * @device: pointer to node
431 * @compat: required compatible string, NULL or "" for any match
432 * @type: required device_type value, NULL or "" for any match
433 * @name: required node name, NULL or "" for any match
434 *
435 * Checks if the given @compat, @type and @name strings match the
436 * properties of the given @device. A constraints can be skipped by
437 * passing NULL or an empty string as the constraint.
438 *
439 * Returns 0 for no match, and a positive integer on match. The return
440 * value is a relative score with larger values indicating better
441 * matches. The score is weighted for the most specific compatible value
442 * to get the highest score. Matching type is next, followed by matching
443 * name. Practically speaking, this results in the following priority
444 * order for matches:
445 *
446 * 1. specific compatible && type && name
447 * 2. specific compatible && type
448 * 3. specific compatible && name
449 * 4. specific compatible
450 * 5. general compatible && type && name
451 * 6. general compatible && type
452 * 7. general compatible && name
453 * 8. general compatible
454 * 9. type && name
455 * 10. type
456 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000457 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500458static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800459 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000460{
Kevin Hao215a14c2014-02-19 16:15:45 +0800461 struct property *prop;
462 const char *cp;
463 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000464
Kevin Hao215a14c2014-02-19 16:15:45 +0800465 /* Compatible match has highest priority */
466 if (compat && compat[0]) {
467 prop = __of_find_property(device, "compatible", NULL);
468 for (cp = of_prop_next_string(prop, NULL); cp;
469 cp = of_prop_next_string(prop, cp), index++) {
470 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
471 score = INT_MAX/2 - (index << 2);
472 break;
473 }
474 }
475 if (!score)
476 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000477 }
478
Kevin Hao215a14c2014-02-19 16:15:45 +0800479 /* Matching type is better than matching name */
480 if (type && type[0]) {
481 if (!device->type || of_node_cmp(type, device->type))
482 return 0;
483 score += 2;
484 }
485
486 /* Matching name is a bit better than not */
487 if (name && name[0]) {
488 if (!device->name || of_node_cmp(name, device->name))
489 return 0;
490 score++;
491 }
492
493 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000494}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500495
496/** Checks if the given "compat" string matches one of the strings in
497 * the device's "compatible" property
498 */
499int of_device_is_compatible(const struct device_node *device,
500 const char *compat)
501{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500502 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500503 int res;
504
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500505 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800506 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500507 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500508 return res;
509}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000510EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000511
Benjamin Herrenschmidtb9c13fe32016-07-08 08:35:59 +1000512/** Checks if the device is compatible with any of the entries in
513 * a NULL terminated array of strings. Returns the best match
514 * score or 0.
515 */
516int of_device_compatible_match(struct device_node *device,
517 const char *const *compat)
518{
519 unsigned int tmp, score = 0;
520
521 if (!compat)
522 return 0;
523
524 while (*compat) {
525 tmp = of_device_is_compatible(device, *compat);
526 if (tmp > score)
527 score = tmp;
528 compat++;
529 }
530
531 return score;
532}
533
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000534/**
Grant Likely71a157e2010-02-01 21:34:14 -0700535 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700536 * @compat: compatible string to look for in root node's compatible property.
537 *
Kevin Cernekee25c7a1d2014-11-12 12:54:00 -0800538 * Returns a positive integer if the root node has the given value in its
Grant Likely1f43cfb2010-01-28 13:47:25 -0700539 * compatible property.
540 */
Grant Likely71a157e2010-02-01 21:34:14 -0700541int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700542{
543 struct device_node *root;
544 int rc = 0;
545
546 root = of_find_node_by_path("/");
547 if (root) {
548 rc = of_device_is_compatible(root, compat);
549 of_node_put(root);
550 }
551 return rc;
552}
Grant Likely71a157e2010-02-01 21:34:14 -0700553EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700554
555/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700556 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100557 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700558 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100559 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800560 * Returns true if the status property is absent or set to "okay" or "ok",
561 * false otherwise
Josh Boyer834d97d2008-03-27 00:33:14 +1100562 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800563static bool __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100564{
565 const char *status;
566 int statlen;
567
Xiubo Li42ccd782014-01-13 11:07:28 +0800568 if (!device)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800569 return false;
Xiubo Li42ccd782014-01-13 11:07:28 +0800570
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700571 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100572 if (status == NULL)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800573 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100574
575 if (statlen > 0) {
576 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800577 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100578 }
579
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800580 return false;
Josh Boyer834d97d2008-03-27 00:33:14 +1100581}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700582
583/**
584 * of_device_is_available - check if a device is available for use
585 *
586 * @device: Node to check for availability
587 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800588 * Returns true if the status property is absent or set to "okay" or "ok",
589 * false otherwise
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700590 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800591bool of_device_is_available(const struct device_node *device)
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700592{
593 unsigned long flags;
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800594 bool res;
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700595
596 raw_spin_lock_irqsave(&devtree_lock, flags);
597 res = __of_device_is_available(device);
598 raw_spin_unlock_irqrestore(&devtree_lock, flags);
599 return res;
600
601}
Josh Boyer834d97d2008-03-27 00:33:14 +1100602EXPORT_SYMBOL(of_device_is_available);
603
604/**
Kevin Cernekee37786c72015-04-09 13:05:14 -0700605 * of_device_is_big_endian - check if a device has BE registers
606 *
607 * @device: Node to check for endianness
608 *
609 * Returns true if the device has a "big-endian" property, or if the kernel
610 * was compiled for BE *and* the device has a "native-endian" property.
611 * Returns false otherwise.
612 *
613 * Callers would nominally use ioread32be/iowrite32be if
614 * of_device_is_big_endian() == true, or readl/writel otherwise.
615 */
616bool of_device_is_big_endian(const struct device_node *device)
617{
618 if (of_property_read_bool(device, "big-endian"))
619 return true;
620 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
621 of_property_read_bool(device, "native-endian"))
622 return true;
623 return false;
624}
625EXPORT_SYMBOL(of_device_is_big_endian);
626
627/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000628 * of_get_parent - Get a node's parent if any
629 * @node: Node to get parent
630 *
631 * Returns a node pointer with refcount incremented, use
632 * of_node_put() on it when done.
633 */
634struct device_node *of_get_parent(const struct device_node *node)
635{
636 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500637 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000638
639 if (!node)
640 return NULL;
641
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500642 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000643 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500644 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000645 return np;
646}
647EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000648
649/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000650 * of_get_next_parent - Iterate to a node's parent
651 * @node: Node to get parent of
652 *
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +0200653 * This is like of_get_parent() except that it drops the
654 * refcount on the passed node, making it suitable for iterating
655 * through a node's parents.
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000656 *
657 * Returns a node pointer with refcount incremented, use
658 * of_node_put() on it when done.
659 */
660struct device_node *of_get_next_parent(struct device_node *node)
661{
662 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500663 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000664
665 if (!node)
666 return NULL;
667
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500668 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000669 parent = of_node_get(node->parent);
670 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500671 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000672 return parent;
673}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300674EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000675
Grant Likely0d0e02d2014-05-22 01:04:17 +0900676static struct device_node *__of_get_next_child(const struct device_node *node,
677 struct device_node *prev)
678{
679 struct device_node *next;
680
Florian Fainelli43cb4362014-05-28 10:39:02 -0700681 if (!node)
682 return NULL;
683
Grant Likely0d0e02d2014-05-22 01:04:17 +0900684 next = prev ? prev->sibling : node->child;
685 for (; next; next = next->sibling)
686 if (of_node_get(next))
687 break;
688 of_node_put(prev);
689 return next;
690}
691#define __for_each_child_of_node(parent, child) \
692 for (child = __of_get_next_child(parent, NULL); child != NULL; \
693 child = __of_get_next_child(parent, child))
694
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000695/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000696 * of_get_next_child - Iterate a node childs
697 * @node: parent node
698 * @prev: previous child of the parent node, or NULL to get first
699 *
Baruch Siach64808272015-03-19 14:03:41 +0200700 * Returns a node pointer with refcount incremented, use of_node_put() on
701 * it when done. Returns NULL when prev is the last child. Decrements the
702 * refcount of prev.
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000703 */
704struct device_node *of_get_next_child(const struct device_node *node,
705 struct device_node *prev)
706{
707 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500708 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000709
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500710 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely0d0e02d2014-05-22 01:04:17 +0900711 next = __of_get_next_child(node, prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500712 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000713 return next;
714}
715EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000716
717/**
Timur Tabi32961932012-08-14 13:20:23 +0000718 * of_get_next_available_child - Find the next available child node
719 * @node: parent node
720 * @prev: previous child of the parent node, or NULL to get first
721 *
722 * This function is like of_get_next_child(), except that it
723 * automatically skips any disabled nodes (i.e. status = "disabled").
724 */
725struct device_node *of_get_next_available_child(const struct device_node *node,
726 struct device_node *prev)
727{
728 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000729 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000730
Florian Fainelli43cb4362014-05-28 10:39:02 -0700731 if (!node)
732 return NULL;
733
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000734 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000735 next = prev ? prev->sibling : node->child;
736 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700737 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000738 continue;
739 if (of_node_get(next))
740 break;
741 }
742 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000743 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000744 return next;
745}
746EXPORT_SYMBOL(of_get_next_available_child);
747
748/**
Johan Hovold36156f92018-08-27 10:21:45 +0200749 * of_get_compatible_child - Find compatible child node
750 * @parent: parent node
751 * @compatible: compatible string
752 *
753 * Lookup child node whose compatible property contains the given compatible
754 * string.
755 *
756 * Returns a node pointer with refcount incremented, use of_node_put() on it
757 * when done; or NULL if not found.
758 */
759struct device_node *of_get_compatible_child(const struct device_node *parent,
760 const char *compatible)
761{
762 struct device_node *child;
763
764 for_each_child_of_node(parent, child) {
765 if (of_device_is_compatible(child, compatible))
766 break;
767 }
768
769 return child;
770}
771EXPORT_SYMBOL(of_get_compatible_child);
772
773/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100774 * of_get_child_by_name - Find the child node by name for a given parent
775 * @node: parent node
776 * @name: child name to look for.
777 *
778 * This function looks for child node for given matching name
779 *
780 * Returns a node pointer if found, with refcount incremented, use
781 * of_node_put() on it when done.
782 * Returns NULL if node is not found.
783 */
784struct device_node *of_get_child_by_name(const struct device_node *node,
785 const char *name)
786{
787 struct device_node *child;
788
789 for_each_child_of_node(node, child)
790 if (child->name && (of_node_cmp(child->name, name) == 0))
791 break;
792 return child;
793}
794EXPORT_SYMBOL(of_get_child_by_name);
795
Frank Rowande0a58f32017-10-17 16:36:31 -0700796struct device_node *__of_find_node_by_path(struct device_node *parent,
Grant Likelyc22e6502014-03-14 17:07:12 +0000797 const char *path)
798{
799 struct device_node *child;
Leif Lindholm106937e2015-03-06 16:52:53 +0000800 int len;
Grant Likelyc22e6502014-03-14 17:07:12 +0000801
Brian Norris721a09e2015-03-17 12:30:31 -0700802 len = strcspn(path, "/:");
Grant Likelyc22e6502014-03-14 17:07:12 +0000803 if (!len)
804 return NULL;
805
806 __for_each_child_of_node(parent, child) {
Rob Herring95e6b1f2017-06-01 18:00:00 -0500807 const char *name = kbasename(child->full_name);
Grant Likelyc22e6502014-03-14 17:07:12 +0000808 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
809 return child;
810 }
811 return NULL;
812}
813
Rob Herring27497e12017-06-02 12:43:18 -0500814struct device_node *__of_find_node_by_full_path(struct device_node *node,
815 const char *path)
816{
817 const char *separator = strchr(path, ':');
818
819 while (node && *path == '/') {
820 struct device_node *tmp = node;
821
822 path++; /* Increment past '/' delimiter */
823 node = __of_find_node_by_path(node, path);
824 of_node_put(tmp);
825 path = strchrnul(path, '/');
826 if (separator && separator < path)
827 break;
828 }
829 return node;
830}
831
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100832/**
Leif Lindholm75c28c02014-11-28 11:34:28 +0000833 * of_find_node_opts_by_path - Find a node matching a full OF path
Grant Likelyc22e6502014-03-14 17:07:12 +0000834 * @path: Either the full path to match, or if the path does not
835 * start with '/', the name of a property of the /aliases
836 * node (an alias). In the case of an alias, the node
837 * matching the alias' value will be returned.
Leif Lindholm75c28c02014-11-28 11:34:28 +0000838 * @opts: Address of a pointer into which to store the start of
839 * an options string appended to the end of the path with
840 * a ':' separator.
Grant Likelyc22e6502014-03-14 17:07:12 +0000841 *
842 * Valid paths:
843 * /foo/bar Full path
844 * foo Valid alias
845 * foo/bar Valid alias + relative path
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000846 *
847 * Returns a node pointer with refcount incremented, use
848 * of_node_put() on it when done.
849 */
Leif Lindholm75c28c02014-11-28 11:34:28 +0000850struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000851{
Grant Likelyc22e6502014-03-14 17:07:12 +0000852 struct device_node *np = NULL;
853 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500854 unsigned long flags;
Leif Lindholm75c28c02014-11-28 11:34:28 +0000855 const char *separator = strchr(path, ':');
856
857 if (opts)
858 *opts = separator ? separator + 1 : NULL;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000859
Grant Likelyc22e6502014-03-14 17:07:12 +0000860 if (strcmp(path, "/") == 0)
Grant Likely5063e252014-10-03 16:28:27 +0100861 return of_node_get(of_root);
Grant Likelyc22e6502014-03-14 17:07:12 +0000862
863 /* The path could begin with an alias */
864 if (*path != '/') {
Leif Lindholm106937e2015-03-06 16:52:53 +0000865 int len;
866 const char *p = separator;
867
868 if (!p)
869 p = strchrnul(path, '/');
870 len = p - path;
Grant Likelyc22e6502014-03-14 17:07:12 +0000871
872 /* of_aliases must not be NULL */
873 if (!of_aliases)
874 return NULL;
875
876 for_each_property_of_node(of_aliases, pp) {
877 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
878 np = of_find_node_by_path(pp->value);
879 break;
880 }
881 }
882 if (!np)
883 return NULL;
884 path = p;
885 }
886
887 /* Step down the tree matching path components */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500888 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyc22e6502014-03-14 17:07:12 +0000889 if (!np)
Grant Likely5063e252014-10-03 16:28:27 +0100890 np = of_node_get(of_root);
Rob Herring27497e12017-06-02 12:43:18 -0500891 np = __of_find_node_by_full_path(np, path);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500892 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000893 return np;
894}
Leif Lindholm75c28c02014-11-28 11:34:28 +0000895EXPORT_SYMBOL(of_find_node_opts_by_path);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000896
897/**
898 * of_find_node_by_name - Find a node by its "name" property
Stephen Boyd02a876b2017-11-17 08:53:21 -0800899 * @from: The node to start searching from or NULL; the node
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000900 * you pass will not be searched, only the next one
Stephen Boyd02a876b2017-11-17 08:53:21 -0800901 * will. Typically, you pass what the previous call
902 * returned. of_node_put() will be called on @from.
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000903 * @name: The name string to match against
904 *
905 * Returns a node pointer with refcount incremented, use
906 * of_node_put() on it when done.
907 */
908struct device_node *of_find_node_by_name(struct device_node *from,
909 const char *name)
910{
911 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500912 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000913
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500914 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100915 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000916 if (np->name && (of_node_cmp(np->name, name) == 0)
917 && of_node_get(np))
918 break;
919 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500920 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000921 return np;
922}
923EXPORT_SYMBOL(of_find_node_by_name);
924
925/**
926 * of_find_node_by_type - Find a node by its "device_type" property
927 * @from: The node to start searching from, or NULL to start searching
928 * the entire device tree. The node you pass will not be
929 * searched, only the next one will; typically, you pass
930 * what the previous call returned. of_node_put() will be
931 * called on from for you.
932 * @type: The type string to match against
933 *
934 * Returns a node pointer with refcount incremented, use
935 * of_node_put() on it when done.
936 */
937struct device_node *of_find_node_by_type(struct device_node *from,
938 const char *type)
939{
940 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500941 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000942
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500943 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100944 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000945 if (np->type && (of_node_cmp(np->type, type) == 0)
946 && of_node_get(np))
947 break;
948 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500949 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000950 return np;
951}
952EXPORT_SYMBOL(of_find_node_by_type);
953
954/**
955 * of_find_compatible_node - Find a node based on type and one of the
956 * tokens in its "compatible" property
957 * @from: The node to start searching from or NULL, the node
958 * you pass will not be searched, only the next one
959 * will; typically, you pass what the previous call
960 * returned. of_node_put() will be called on it
961 * @type: The type string to match "device_type" or NULL to ignore
962 * @compatible: The string to match to one of the tokens in the device
963 * "compatible" list.
964 *
965 * Returns a node pointer with refcount incremented, use
966 * of_node_put() on it when done.
967 */
968struct device_node *of_find_compatible_node(struct device_node *from,
969 const char *type, const char *compatible)
970{
971 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500972 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000973
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500974 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100975 for_each_of_allnodes_from(from, np)
Kevin Hao215a14c2014-02-19 16:15:45 +0800976 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500977 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000978 break;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000979 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500980 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000981 return np;
982}
983EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100984
985/**
Michael Ellerman1e291b142008-11-12 18:54:42 +0000986 * of_find_node_with_property - Find a node which has a property with
987 * the given name.
988 * @from: The node to start searching from or NULL, the node
989 * you pass will not be searched, only the next one
990 * will; typically, you pass what the previous call
991 * returned. of_node_put() will be called on it
992 * @prop_name: The name of the property to look for.
993 *
994 * Returns a node pointer with refcount incremented, use
995 * of_node_put() on it when done.
996 */
997struct device_node *of_find_node_with_property(struct device_node *from,
998 const char *prop_name)
999{
1000 struct device_node *np;
1001 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001002 unsigned long flags;
Michael Ellerman1e291b142008-11-12 18:54:42 +00001003
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001004 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001005 for_each_of_allnodes_from(from, np) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +05301006 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b142008-11-12 18:54:42 +00001007 if (of_prop_cmp(pp->name, prop_name) == 0) {
1008 of_node_get(np);
1009 goto out;
1010 }
1011 }
1012 }
1013out:
1014 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001015 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b142008-11-12 18:54:42 +00001016 return np;
1017}
1018EXPORT_SYMBOL(of_find_node_with_property);
1019
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001020static
1021const struct of_device_id *__of_match_node(const struct of_device_id *matches,
1022 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +11001023{
Kevin Hao215a14c2014-02-19 16:15:45 +08001024 const struct of_device_id *best_match = NULL;
1025 int score, best_score = 0;
1026
Grant Likelya52f07e2011-03-18 10:21:29 -06001027 if (!matches)
1028 return NULL;
1029
Kevin Hao215a14c2014-02-19 16:15:45 +08001030 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
1031 score = __of_device_is_compatible(node, matches->compatible,
1032 matches->type, matches->name);
1033 if (score > best_score) {
1034 best_match = matches;
1035 best_score = score;
1036 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +08001037 }
Kevin Hao215a14c2014-02-19 16:15:45 +08001038
1039 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +11001040}
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001041
1042/**
Geert Uytterhoevenc50949d2014-10-22 11:44:54 +02001043 * of_match_node - Tell if a device_node has a matching of_match structure
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001044 * @matches: array of of device match structures to search in
1045 * @node: the of device structure to match against
1046 *
Kevin Hao71c54982014-02-18 15:57:29 +08001047 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001048 */
1049const struct of_device_id *of_match_node(const struct of_device_id *matches,
1050 const struct device_node *node)
1051{
1052 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001053 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001054
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001055 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001056 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001057 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001058 return match;
1059}
Grant Likely283029d2008-01-09 06:20:40 +11001060EXPORT_SYMBOL(of_match_node);
1061
1062/**
Stephen Warren50c8af42012-11-20 16:12:20 -07001063 * of_find_matching_node_and_match - Find a node based on an of_device_id
1064 * match table.
Grant Likely283029d2008-01-09 06:20:40 +11001065 * @from: The node to start searching from or NULL, the node
1066 * you pass will not be searched, only the next one
1067 * will; typically, you pass what the previous call
1068 * returned. of_node_put() will be called on it
1069 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -07001070 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +11001071 *
1072 * Returns a node pointer with refcount incremented, use
1073 * of_node_put() on it when done.
1074 */
Stephen Warren50c8af42012-11-20 16:12:20 -07001075struct device_node *of_find_matching_node_and_match(struct device_node *from,
1076 const struct of_device_id *matches,
1077 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +11001078{
1079 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001080 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001081 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +11001082
Stephen Warren50c8af42012-11-20 16:12:20 -07001083 if (match)
1084 *match = NULL;
1085
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001086 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001087 for_each_of_allnodes_from(from, np) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001088 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001089 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -07001090 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001091 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +11001092 break;
Stephen Warren50c8af42012-11-20 16:12:20 -07001093 }
Grant Likely283029d2008-01-09 06:20:40 +11001094 }
1095 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001096 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +11001097 return np;
1098}
Grant Likely80c20222012-12-19 10:45:36 +00001099EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -04001100
1101/**
Grant Likely3f07af42008-07-25 22:25:13 -04001102 * of_modalias_node - Lookup appropriate modalias for a device node
1103 * @node: pointer to a device tree node
1104 * @modalias: Pointer to buffer that modalias value will be copied into
1105 * @len: Length of modalias value
1106 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001107 * Based on the value of the compatible property, this routine will attempt
1108 * to choose an appropriate modalias value for a particular device tree node.
1109 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1110 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -04001111 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001112 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001113 */
1114int of_modalias_node(struct device_node *node, char *modalias, int len)
1115{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001116 const char *compatible, *p;
1117 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001118
1119 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001120 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001121 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001122 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001123 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001124 return 0;
1125}
1126EXPORT_SYMBOL_GPL(of_modalias_node);
1127
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001128/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001129 * of_find_node_by_phandle - Find a node given a phandle
1130 * @handle: phandle of the node to find
1131 *
1132 * Returns a node pointer with refcount incremented, use
1133 * of_node_put() on it when done.
1134 */
1135struct device_node *of_find_node_by_phandle(phandle handle)
1136{
Frank Rowand0b3ce782018-03-04 16:14:47 -08001137 struct device_node *np = NULL;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001138 unsigned long flags;
Frank Rowand0b3ce782018-03-04 16:14:47 -08001139 phandle masked_handle;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001140
Grant Likelyfc59b442014-10-02 13:08:02 +01001141 if (!handle)
1142 return NULL;
1143
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001144 raw_spin_lock_irqsave(&devtree_lock, flags);
Frank Rowand0b3ce782018-03-04 16:14:47 -08001145
1146 masked_handle = handle & phandle_cache_mask;
1147
1148 if (phandle_cache) {
1149 if (phandle_cache[masked_handle] &&
1150 handle == phandle_cache[masked_handle]->phandle)
1151 np = phandle_cache[masked_handle];
1152 }
1153
1154 if (!np) {
1155 for_each_of_allnodes(np)
1156 if (np->phandle == handle) {
1157 if (phandle_cache)
1158 phandle_cache[masked_handle] = np;
1159 break;
1160 }
1161 }
1162
Jeremy Kerr89751a72010-02-01 21:34:11 -07001163 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001164 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001165 return np;
1166}
1167EXPORT_SYMBOL(of_find_node_by_phandle);
1168
Grant Likely624cfca2013-10-11 22:05:10 +01001169void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1170{
1171 int i;
Rob Herring0d638a02017-06-01 15:50:55 -05001172 printk("%s %pOF", msg, args->np);
Marcin Nowakowski4aa66342016-12-01 09:00:55 +01001173 for (i = 0; i < args->args_count; i++) {
1174 const char delim = i ? ',' : ':';
1175
1176 pr_cont("%c%08x", delim, args->args[i]);
1177 }
1178 pr_cont("\n");
Grant Likely624cfca2013-10-11 22:05:10 +01001179}
1180
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001181int of_phandle_iterator_init(struct of_phandle_iterator *it,
1182 const struct device_node *np,
1183 const char *list_name,
1184 const char *cells_name,
1185 int cell_count)
1186{
1187 const __be32 *list;
1188 int size;
1189
1190 memset(it, 0, sizeof(*it));
1191
1192 list = of_get_property(np, list_name, &size);
1193 if (!list)
1194 return -ENOENT;
1195
1196 it->cells_name = cells_name;
1197 it->cell_count = cell_count;
1198 it->parent = np;
1199 it->list_end = list + size / sizeof(*list);
1200 it->phandle_end = list;
1201 it->cur = list;
1202
1203 return 0;
1204}
Kuninori Morimoto00bab232017-04-20 01:31:16 +00001205EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001206
Joerg Roedelcd209b42016-04-04 17:49:18 +02001207int of_phandle_iterator_next(struct of_phandle_iterator *it)
1208{
1209 uint32_t count = 0;
1210
1211 if (it->node) {
1212 of_node_put(it->node);
1213 it->node = NULL;
1214 }
1215
1216 if (!it->cur || it->phandle_end >= it->list_end)
1217 return -ENOENT;
1218
1219 it->cur = it->phandle_end;
1220
1221 /* If phandle is 0, then it is an empty entry with no arguments. */
1222 it->phandle = be32_to_cpup(it->cur++);
1223
1224 if (it->phandle) {
1225
1226 /*
1227 * Find the provider node and parse the #*-cells property to
1228 * determine the argument length.
1229 */
1230 it->node = of_find_node_by_phandle(it->phandle);
1231
1232 if (it->cells_name) {
1233 if (!it->node) {
Rob Herring0d638a02017-06-01 15:50:55 -05001234 pr_err("%pOF: could not find phandle\n",
1235 it->parent);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001236 goto err;
1237 }
1238
1239 if (of_property_read_u32(it->node, it->cells_name,
1240 &count)) {
Rob Herring0d638a02017-06-01 15:50:55 -05001241 pr_err("%pOF: could not get %s for %pOF\n",
1242 it->parent,
Joerg Roedelcd209b42016-04-04 17:49:18 +02001243 it->cells_name,
Rob Herring0d638a02017-06-01 15:50:55 -05001244 it->node);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001245 goto err;
1246 }
1247 } else {
1248 count = it->cell_count;
1249 }
1250
1251 /*
1252 * Make sure that the arguments actually fit in the remaining
1253 * property data length
1254 */
1255 if (it->cur + count > it->list_end) {
Rob Herring0d638a02017-06-01 15:50:55 -05001256 pr_err("%pOF: arguments longer than property\n",
1257 it->parent);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001258 goto err;
1259 }
1260 }
1261
1262 it->phandle_end = it->cur + count;
1263 it->cur_count = count;
1264
1265 return 0;
1266
1267err:
1268 if (it->node) {
1269 of_node_put(it->node);
1270 it->node = NULL;
1271 }
1272
1273 return -EINVAL;
1274}
Kuninori Morimoto00bab232017-04-20 01:31:16 +00001275EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001276
Joerg Roedelabdaa772016-04-04 17:49:21 +02001277int of_phandle_iterator_args(struct of_phandle_iterator *it,
1278 uint32_t *args,
1279 int size)
1280{
1281 int i, count;
1282
1283 count = it->cur_count;
1284
1285 if (WARN_ON(size < count))
1286 count = size;
1287
1288 for (i = 0; i < count; i++)
1289 args[i] = be32_to_cpup(it->cur++);
1290
1291 return count;
1292}
1293
Grant Likelybd69f732013-02-10 22:57:21 +00001294static int __of_parse_phandle_with_args(const struct device_node *np,
1295 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001296 const char *cells_name,
1297 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001298 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001299{
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001300 struct of_phandle_iterator it;
1301 int rc, cur_index = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001302
Grant Likely15c9a0a2011-12-12 09:25:57 -07001303 /* Loop over the phandles until all the requested entry is found */
Joerg Roedelf623ce92016-04-04 17:49:20 +02001304 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
Grant Likely15c9a0a2011-12-12 09:25:57 -07001305 /*
Joerg Roedelcd209b42016-04-04 17:49:18 +02001306 * All of the error cases bail out of the loop, so at
Grant Likely15c9a0a2011-12-12 09:25:57 -07001307 * this point, the parsing is successful. If the requested
1308 * index matches, then fill the out_args structure and return,
1309 * or return -ENOENT for an empty entry.
1310 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001311 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001312 if (cur_index == index) {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001313 if (!it.phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001314 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001315
Grant Likely15c9a0a2011-12-12 09:25:57 -07001316 if (out_args) {
Joerg Roedelabdaa772016-04-04 17:49:21 +02001317 int c;
1318
1319 c = of_phandle_iterator_args(&it,
1320 out_args->args,
1321 MAX_PHANDLE_ARGS);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001322 out_args->np = it.node;
Joerg Roedelabdaa772016-04-04 17:49:21 +02001323 out_args->args_count = c;
Tang Yuantianb855f162013-04-10 11:36:39 +08001324 } else {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001325 of_node_put(it.node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001326 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001327
1328 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001329 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001330 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001331
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001332 cur_index++;
1333 }
1334
Grant Likely23ce04c2013-02-12 21:21:49 +00001335 /*
1336 * Unlock node before returning result; will be one of:
1337 * -ENOENT : index is for empty phandle
1338 * -EINVAL : parsing error on data
1339 */
Joerg Roedelcd209b42016-04-04 17:49:18 +02001340
Grant Likely23ce04c2013-02-12 21:21:49 +00001341 err:
Markus Elfringbeab47d2016-07-19 22:42:22 +02001342 of_node_put(it.node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001343 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001344}
Grant Likelybd69f732013-02-10 22:57:21 +00001345
Stephen Warreneded9dd2013-08-14 15:27:08 -06001346/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001347 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1348 * @np: Pointer to device node holding phandle property
1349 * @phandle_name: Name of property holding a phandle value
1350 * @index: For properties holding a table of phandles, this is the index into
1351 * the table
1352 *
1353 * Returns the device_node pointer with refcount incremented. Use
1354 * of_node_put() on it when done.
1355 */
1356struct device_node *of_parse_phandle(const struct device_node *np,
1357 const char *phandle_name, int index)
1358{
Stephen Warren91d99422013-08-14 15:27:11 -06001359 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001360
Stephen Warren91d99422013-08-14 15:27:11 -06001361 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001362 return NULL;
1363
Stephen Warren91d99422013-08-14 15:27:11 -06001364 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1365 index, &args))
1366 return NULL;
1367
1368 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001369}
1370EXPORT_SYMBOL(of_parse_phandle);
1371
1372/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001373 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1374 * @np: pointer to a device tree node containing a list
1375 * @list_name: property name that contains a list
1376 * @cells_name: property name that specifies phandles' arguments count
1377 * @index: index of a phandle to parse out
1378 * @out_args: optional pointer to output arguments structure (will be filled)
1379 *
1380 * This function is useful to parse lists of phandles and their arguments.
1381 * Returns 0 on success and fills out_args, on error returns appropriate
1382 * errno value.
1383 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001384 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warreneded9dd2013-08-14 15:27:08 -06001385 * pointer.
1386 *
1387 * Example:
1388 *
1389 * phandle1: node1 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001390 * #list-cells = <2>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001391 * }
1392 *
1393 * phandle2: node2 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001394 * #list-cells = <1>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001395 * }
1396 *
1397 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001398 * list = <&phandle1 1 2 &phandle2 3>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001399 * }
1400 *
1401 * To get a device_node of the `node2' node you may call this:
1402 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1403 */
Grant Likelybd69f732013-02-10 22:57:21 +00001404int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1405 const char *cells_name, int index,
1406 struct of_phandle_args *out_args)
1407{
1408 if (index < 0)
1409 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001410 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1411 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001412}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001413EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001414
Grant Likelybd69f732013-02-10 22:57:21 +00001415/**
Stephen Boydbd6f2fd2018-01-30 18:36:16 -08001416 * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it
1417 * @np: pointer to a device tree node containing a list
1418 * @list_name: property name that contains a list
1419 * @stem_name: stem of property names that specify phandles' arguments count
1420 * @index: index of a phandle to parse out
1421 * @out_args: optional pointer to output arguments structure (will be filled)
1422 *
1423 * This function is useful to parse lists of phandles and their arguments.
1424 * Returns 0 on success and fills out_args, on error returns appropriate errno
1425 * value. The difference between this function and of_parse_phandle_with_args()
1426 * is that this API remaps a phandle if the node the phandle points to has
1427 * a <@stem_name>-map property.
1428 *
1429 * Caller is responsible to call of_node_put() on the returned out_args->np
1430 * pointer.
1431 *
1432 * Example:
1433 *
1434 * phandle1: node1 {
1435 * #list-cells = <2>;
1436 * }
1437 *
1438 * phandle2: node2 {
1439 * #list-cells = <1>;
1440 * }
1441 *
1442 * phandle3: node3 {
1443 * #list-cells = <1>;
1444 * list-map = <0 &phandle2 3>,
1445 * <1 &phandle2 2>,
1446 * <2 &phandle1 5 1>;
1447 * list-map-mask = <0x3>;
1448 * };
1449 *
1450 * node4 {
1451 * list = <&phandle1 1 2 &phandle3 0>;
1452 * }
1453 *
1454 * To get a device_node of the `node2' node you may call this:
1455 * of_parse_phandle_with_args(node4, "list", "list", 1, &args);
1456 */
1457int of_parse_phandle_with_args_map(const struct device_node *np,
1458 const char *list_name,
1459 const char *stem_name,
1460 int index, struct of_phandle_args *out_args)
1461{
1462 char *cells_name, *map_name = NULL, *mask_name = NULL;
1463 char *pass_name = NULL;
1464 struct device_node *cur, *new = NULL;
1465 const __be32 *map, *mask, *pass;
1466 static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 };
1467 static const __be32 dummy_pass[] = { [0 ... MAX_PHANDLE_ARGS] = 0 };
1468 __be32 initial_match_array[MAX_PHANDLE_ARGS];
1469 const __be32 *match_array = initial_match_array;
1470 int i, ret, map_len, match;
1471 u32 list_size, new_size;
1472
1473 if (index < 0)
1474 return -EINVAL;
1475
1476 cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name);
1477 if (!cells_name)
1478 return -ENOMEM;
1479
1480 ret = -ENOMEM;
1481 map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name);
1482 if (!map_name)
1483 goto free;
1484
1485 mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name);
1486 if (!mask_name)
1487 goto free;
1488
1489 pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name);
1490 if (!pass_name)
1491 goto free;
1492
1493 ret = __of_parse_phandle_with_args(np, list_name, cells_name, 0, index,
1494 out_args);
1495 if (ret)
1496 goto free;
1497
1498 /* Get the #<list>-cells property */
1499 cur = out_args->np;
1500 ret = of_property_read_u32(cur, cells_name, &list_size);
1501 if (ret < 0)
1502 goto put;
1503
1504 /* Precalculate the match array - this simplifies match loop */
1505 for (i = 0; i < list_size; i++)
1506 initial_match_array[i] = cpu_to_be32(out_args->args[i]);
1507
1508 ret = -EINVAL;
1509 while (cur) {
1510 /* Get the <list>-map property */
1511 map = of_get_property(cur, map_name, &map_len);
1512 if (!map) {
1513 ret = 0;
1514 goto free;
1515 }
1516 map_len /= sizeof(u32);
1517
1518 /* Get the <list>-map-mask property (optional) */
1519 mask = of_get_property(cur, mask_name, NULL);
1520 if (!mask)
1521 mask = dummy_mask;
1522 /* Iterate through <list>-map property */
1523 match = 0;
1524 while (map_len > (list_size + 1) && !match) {
1525 /* Compare specifiers */
1526 match = 1;
1527 for (i = 0; i < list_size; i++, map_len--)
1528 match &= !((match_array[i] ^ *map++) & mask[i]);
1529
1530 of_node_put(new);
1531 new = of_find_node_by_phandle(be32_to_cpup(map));
1532 map++;
1533 map_len--;
1534
1535 /* Check if not found */
1536 if (!new)
1537 goto put;
1538
1539 if (!of_device_is_available(new))
1540 match = 0;
1541
1542 ret = of_property_read_u32(new, cells_name, &new_size);
1543 if (ret)
1544 goto put;
1545
1546 /* Check for malformed properties */
1547 if (WARN_ON(new_size > MAX_PHANDLE_ARGS))
1548 goto put;
1549 if (map_len < new_size)
1550 goto put;
1551
1552 /* Move forward by new node's #<list>-cells amount */
1553 map += new_size;
1554 map_len -= new_size;
1555 }
1556 if (!match)
1557 goto put;
1558
1559 /* Get the <list>-map-pass-thru property (optional) */
1560 pass = of_get_property(cur, pass_name, NULL);
1561 if (!pass)
1562 pass = dummy_pass;
1563
1564 /*
1565 * Successfully parsed a <list>-map translation; copy new
1566 * specifier into the out_args structure, keeping the
1567 * bits specified in <list>-map-pass-thru.
1568 */
1569 match_array = map - new_size;
1570 for (i = 0; i < new_size; i++) {
1571 __be32 val = *(map - new_size + i);
1572
1573 if (i < list_size) {
1574 val &= ~pass[i];
1575 val |= cpu_to_be32(out_args->args[i]) & pass[i];
1576 }
1577
1578 out_args->args[i] = be32_to_cpu(val);
1579 }
1580 out_args->args_count = list_size = new_size;
1581 /* Iterate again with new provider */
1582 out_args->np = new;
1583 of_node_put(cur);
1584 cur = new;
1585 }
1586put:
1587 of_node_put(cur);
1588 of_node_put(new);
1589free:
1590 kfree(mask_name);
1591 kfree(map_name);
1592 kfree(cells_name);
1593 kfree(pass_name);
1594
1595 return ret;
1596}
1597EXPORT_SYMBOL(of_parse_phandle_with_args_map);
1598
1599/**
Stephen Warren035fd942013-08-14 15:27:10 -06001600 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1601 * @np: pointer to a device tree node containing a list
1602 * @list_name: property name that contains a list
1603 * @cell_count: number of argument cells following the phandle
1604 * @index: index of a phandle to parse out
1605 * @out_args: optional pointer to output arguments structure (will be filled)
1606 *
1607 * This function is useful to parse lists of phandles and their arguments.
1608 * Returns 0 on success and fills out_args, on error returns appropriate
1609 * errno value.
1610 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001611 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warren035fd942013-08-14 15:27:10 -06001612 * pointer.
1613 *
1614 * Example:
1615 *
1616 * phandle1: node1 {
1617 * }
1618 *
1619 * phandle2: node2 {
1620 * }
1621 *
1622 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001623 * list = <&phandle1 0 2 &phandle2 2 3>;
Stephen Warren035fd942013-08-14 15:27:10 -06001624 * }
1625 *
1626 * To get a device_node of the `node2' node you may call this:
1627 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1628 */
1629int of_parse_phandle_with_fixed_args(const struct device_node *np,
1630 const char *list_name, int cell_count,
1631 int index, struct of_phandle_args *out_args)
1632{
1633 if (index < 0)
1634 return -EINVAL;
1635 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1636 index, out_args);
1637}
1638EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1639
1640/**
Grant Likelybd69f732013-02-10 22:57:21 +00001641 * of_count_phandle_with_args() - Find the number of phandles references in a property
1642 * @np: pointer to a device tree node containing a list
1643 * @list_name: property name that contains a list
1644 * @cells_name: property name that specifies phandles' arguments count
1645 *
1646 * Returns the number of phandle + argument tuples within a property. It
1647 * is a typical pattern to encode a list of phandle and variable
1648 * arguments into a single property. The number of arguments is encoded
1649 * by a property in the phandle-target node. For example, a gpios
1650 * property would contain a list of GPIO specifies consisting of a
1651 * phandle and 1 or more arguments. The number of arguments are
1652 * determined by the #gpio-cells property in the node pointed to by the
1653 * phandle.
1654 */
1655int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1656 const char *cells_name)
1657{
Joerg Roedel2021bd02016-04-04 17:49:19 +02001658 struct of_phandle_iterator it;
1659 int rc, cur_index = 0;
1660
1661 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
1662 if (rc)
1663 return rc;
1664
1665 while ((rc = of_phandle_iterator_next(&it)) == 0)
1666 cur_index += 1;
1667
1668 if (rc != -ENOENT)
1669 return rc;
1670
1671 return cur_index;
Grant Likelybd69f732013-02-10 22:57:21 +00001672}
1673EXPORT_SYMBOL(of_count_phandle_with_args);
1674
Grant Likely02af11b2009-11-23 20:16:45 -07001675/**
Xiubo Li62664f62014-01-22 13:57:39 +08001676 * __of_add_property - Add a property to a node without lock operations
1677 */
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001678int __of_add_property(struct device_node *np, struct property *prop)
Xiubo Li62664f62014-01-22 13:57:39 +08001679{
1680 struct property **next;
1681
1682 prop->next = NULL;
1683 next = &np->properties;
1684 while (*next) {
1685 if (strcmp(prop->name, (*next)->name) == 0)
1686 /* duplicate ! don't insert it */
1687 return -EEXIST;
1688
1689 next = &(*next)->next;
1690 }
1691 *next = prop;
1692
1693 return 0;
1694}
1695
1696/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001697 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001698 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001699int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001700{
Grant Likely02af11b2009-11-23 20:16:45 -07001701 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001702 int rc;
1703
Grant Likely8a2b22a22014-07-23 17:05:06 -06001704 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001705
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001706 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001707 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001708 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001709
Grant Likely8a2b22a22014-07-23 17:05:06 -06001710 if (!rc)
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001711 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001712
Grant Likely8a2b22a22014-07-23 17:05:06 -06001713 mutex_unlock(&of_mutex);
1714
Grant Likely259092a2014-07-16 12:48:23 -06001715 if (!rc)
1716 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1717
Xiubo Li62664f62014-01-22 13:57:39 +08001718 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001719}
1720
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001721int __of_remove_property(struct device_node *np, struct property *prop)
1722{
1723 struct property **next;
1724
1725 for (next = &np->properties; *next; next = &(*next)->next) {
1726 if (*next == prop)
1727 break;
1728 }
1729 if (*next == NULL)
1730 return -ENODEV;
1731
1732 /* found the node */
1733 *next = prop->next;
1734 prop->next = np->deadprops;
1735 np->deadprops = prop;
1736
1737 return 0;
1738}
1739
Grant Likely02af11b2009-11-23 20:16:45 -07001740/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001741 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001742 *
1743 * Note that we don't actually remove it, since we have given out
1744 * who-knows-how-many pointers to the data using get-property.
1745 * Instead we just move the property to the "dead properties"
1746 * list, so it won't be found any more.
1747 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001748int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001749{
Grant Likely02af11b2009-11-23 20:16:45 -07001750 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001751 int rc;
1752
Suraj Jitindar Singh201b3fe2016-04-28 15:34:54 +10001753 if (!prop)
1754 return -ENODEV;
1755
Grant Likely8a2b22a22014-07-23 17:05:06 -06001756 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001757
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001758 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001759 rc = __of_remove_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001760 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001761
Grant Likely8a2b22a22014-07-23 17:05:06 -06001762 if (!rc)
1763 __of_remove_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001764
Grant Likely8a2b22a22014-07-23 17:05:06 -06001765 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +00001766
Grant Likely259092a2014-07-16 12:48:23 -06001767 if (!rc)
1768 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1769
Grant Likely8a2b22a22014-07-23 17:05:06 -06001770 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001771}
1772
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001773int __of_update_property(struct device_node *np, struct property *newprop,
1774 struct property **oldpropp)
1775{
1776 struct property **next, *oldprop;
1777
1778 for (next = &np->properties; *next; next = &(*next)->next) {
1779 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1780 break;
1781 }
1782 *oldpropp = oldprop = *next;
1783
1784 if (oldprop) {
1785 /* replace the node */
1786 newprop->next = oldprop->next;
1787 *next = newprop;
1788 oldprop->next = np->deadprops;
1789 np->deadprops = oldprop;
1790 } else {
1791 /* new node */
1792 newprop->next = NULL;
1793 *next = newprop;
1794 }
Grant Likely02af11b2009-11-23 20:16:45 -07001795
1796 return 0;
1797}
1798
1799/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001800 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001801 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001802 *
1803 * Note that we don't actually remove it, since we have given out
1804 * who-knows-how-many pointers to the data using get-property.
1805 * Instead we just move the property to the "dead properties" list,
1806 * and add the new property to the property list
1807 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001808int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001809{
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001810 struct property *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001811 unsigned long flags;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001812 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001813
Dong Aisheng475d0092012-07-11 15:16:37 +10001814 if (!newprop->name)
1815 return -EINVAL;
1816
Grant Likely8a2b22a22014-07-23 17:05:06 -06001817 mutex_lock(&of_mutex);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001818
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001819 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001820 rc = __of_update_property(np, newprop, &oldprop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001821 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001822
Grant Likely8a2b22a22014-07-23 17:05:06 -06001823 if (!rc)
1824 __of_update_property_sysfs(np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001825
Grant Likely8a2b22a22014-07-23 17:05:06 -06001826 mutex_unlock(&of_mutex);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001827
Grant Likely259092a2014-07-16 12:48:23 -06001828 if (!rc)
1829 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001830
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001831 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001832}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001833
Shawn Guo611cad72011-08-15 15:28:14 +08001834static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1835 int id, const char *stem, int stem_len)
1836{
1837 ap->np = np;
1838 ap->id = id;
1839 strncpy(ap->stem, stem, stem_len);
1840 ap->stem[stem_len] = 0;
1841 list_add_tail(&ap->link, &aliases_lookup);
Rob Herring0d638a02017-06-01 15:50:55 -05001842 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1843 ap->alias, ap->stem, ap->id, np);
Shawn Guo611cad72011-08-15 15:28:14 +08001844}
1845
1846/**
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001847 * of_alias_scan - Scan all properties of the 'aliases' node
Shawn Guo611cad72011-08-15 15:28:14 +08001848 *
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001849 * The function scans all the properties of the 'aliases' node and populates
1850 * the global lookup table with the properties. It returns the
1851 * number of alias properties found, or an error code in case of failure.
Shawn Guo611cad72011-08-15 15:28:14 +08001852 *
1853 * @dt_alloc: An allocator that provides a virtual address to memory
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001854 * for storing the resulting tree
Shawn Guo611cad72011-08-15 15:28:14 +08001855 */
1856void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1857{
1858 struct property *pp;
1859
Laurentiu Tudor7dbe5842014-08-27 17:09:39 +03001860 of_aliases = of_find_node_by_path("/aliases");
Shawn Guo611cad72011-08-15 15:28:14 +08001861 of_chosen = of_find_node_by_path("/chosen");
1862 if (of_chosen == NULL)
1863 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02001864
1865 if (of_chosen) {
Grant Likelya752ee52014-03-28 08:12:18 -07001866 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
Sergei Shtylyovb0d9d922017-07-23 19:55:48 +03001867 const char *name = NULL;
1868
1869 if (of_property_read_string(of_chosen, "stdout-path", &name))
1870 of_property_read_string(of_chosen, "linux,stdout-path",
1871 &name);
Grant Likelya752ee52014-03-28 08:12:18 -07001872 if (IS_ENABLED(CONFIG_PPC) && !name)
Sergei Shtylyovb0d9d922017-07-23 19:55:48 +03001873 of_property_read_string(of_aliases, "stdout", &name);
Peter Hurleyf64255b2015-03-17 16:46:33 -04001874 if (name)
Leif Lindholm7914a7c2014-11-27 17:56:07 +00001875 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001876 }
1877
Shawn Guo611cad72011-08-15 15:28:14 +08001878 if (!of_aliases)
1879 return;
1880
Dong Aisheng8af0da92011-12-22 20:19:24 +08001881 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001882 const char *start = pp->name;
1883 const char *end = start + strlen(start);
1884 struct device_node *np;
1885 struct alias_prop *ap;
1886 int id, len;
1887
1888 /* Skip those we do not want to proceed */
1889 if (!strcmp(pp->name, "name") ||
1890 !strcmp(pp->name, "phandle") ||
1891 !strcmp(pp->name, "linux,phandle"))
1892 continue;
1893
1894 np = of_find_node_by_path(pp->value);
1895 if (!np)
1896 continue;
1897
1898 /* walk the alias backwards to extract the id and work out
1899 * the 'stem' string */
1900 while (isdigit(*(end-1)) && end > start)
1901 end--;
1902 len = end - start;
1903
1904 if (kstrtoint(end, 10, &id) < 0)
1905 continue;
1906
1907 /* Allocate an alias_prop with enough space for the stem */
Paul Burtonde96ec22016-09-23 16:38:27 +01001908 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
Shawn Guo611cad72011-08-15 15:28:14 +08001909 if (!ap)
1910 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01001911 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001912 ap->alias = start;
1913 of_alias_add(ap, np, id, start, len);
1914 }
1915}
1916
1917/**
1918 * of_alias_get_id - Get alias id for the given device_node
1919 * @np: Pointer to the given device_node
1920 * @stem: Alias stem of the given device_node
1921 *
Geert Uytterhoeven5a53a072014-03-11 11:23:38 +01001922 * The function travels the lookup table to get the alias id for the given
1923 * device_node and alias stem. It returns the alias id if found.
Shawn Guo611cad72011-08-15 15:28:14 +08001924 */
1925int of_alias_get_id(struct device_node *np, const char *stem)
1926{
1927 struct alias_prop *app;
1928 int id = -ENODEV;
1929
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001930 mutex_lock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001931 list_for_each_entry(app, &aliases_lookup, link) {
1932 if (strcmp(app->stem, stem) != 0)
1933 continue;
1934
1935 if (np == app->np) {
1936 id = app->id;
1937 break;
1938 }
1939 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001940 mutex_unlock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001941
1942 return id;
1943}
1944EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001945
Wolfram Sang351d2242015-03-12 17:17:58 +01001946/**
Michal Simekb1078c32018-09-20 13:41:52 +02001947 * of_alias_get_alias_list - Get alias list for the given device driver
1948 * @matches: Array of OF device match structures to search in
1949 * @stem: Alias stem of the given device_node
1950 * @bitmap: Bitmap field pointer
1951 * @nbits: Maximum number of alias ID which can be recorded it bitmap
1952 *
1953 * The function travels the lookup table to record alias ids for the given
1954 * device match structures and alias stem.
1955 *
1956 * Return: 0 or -ENOSYS when !CONFIG_OF
1957 */
1958int of_alias_get_alias_list(const struct of_device_id *matches,
1959 const char *stem, unsigned long *bitmap,
1960 unsigned int nbits)
1961{
1962 struct alias_prop *app;
1963
1964 /* Zero bitmap field to make sure that all the time it is clean */
1965 bitmap_zero(bitmap, nbits);
1966
1967 mutex_lock(&of_mutex);
1968 pr_debug("%s: Looking for stem: %s\n", __func__, stem);
1969 list_for_each_entry(app, &aliases_lookup, link) {
1970 pr_debug("%s: stem: %s, id: %d\n",
1971 __func__, app->stem, app->id);
1972
1973 if (strcmp(app->stem, stem) != 0) {
1974 pr_debug("%s: stem comparison doesn't passed %s\n",
1975 __func__, app->stem);
1976 continue;
1977 }
1978
1979 if (app->id >= nbits) {
1980 pr_debug("%s: ID %d greater then bitmap field %d\n",
1981 __func__, app->id, nbits);
1982 continue;
1983 }
1984
1985 if (of_match_node(matches, app->np)) {
1986 pr_debug("%s: Allocated ID %d\n", __func__, app->id);
1987 set_bit(app->id, bitmap);
1988 }
1989 /* Alias exist but it not compatible with matches */
1990 }
1991 mutex_unlock(&of_mutex);
1992
1993 return 0;
1994}
1995EXPORT_SYMBOL_GPL(of_alias_get_alias_list);
1996
1997/**
Wolfram Sang351d2242015-03-12 17:17:58 +01001998 * of_alias_get_highest_id - Get highest alias id for the given stem
1999 * @stem: Alias stem to be examined
2000 *
2001 * The function travels the lookup table to get the highest alias id for the
2002 * given alias stem. It returns the alias id if found.
2003 */
2004int of_alias_get_highest_id(const char *stem)
2005{
2006 struct alias_prop *app;
2007 int id = -ENODEV;
2008
2009 mutex_lock(&of_mutex);
2010 list_for_each_entry(app, &aliases_lookup, link) {
2011 if (strcmp(app->stem, stem) != 0)
2012 continue;
2013
2014 if (app->id > id)
2015 id = app->id;
2016 }
2017 mutex_unlock(&of_mutex);
2018
2019 return id;
2020}
2021EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
2022
Sascha Hauer5c19e952013-08-05 14:40:44 +02002023/**
Grant Likely3482f2c2014-03-27 17:18:55 -07002024 * of_console_check() - Test and setup console for DT setup
2025 * @dn - Pointer to device node
2026 * @name - Name to use for preferred console without index. ex. "ttyS"
2027 * @index - Index to use for preferred console.
Sascha Hauer5c19e952013-08-05 14:40:44 +02002028 *
Grant Likely3482f2c2014-03-27 17:18:55 -07002029 * Check if the given device node matches the stdout-path property in the
2030 * /chosen node. If it does then register it as the preferred console and return
2031 * TRUE. Otherwise return FALSE.
Sascha Hauer5c19e952013-08-05 14:40:44 +02002032 */
Grant Likely3482f2c2014-03-27 17:18:55 -07002033bool of_console_check(struct device_node *dn, char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +02002034{
Grant Likely3482f2c2014-03-27 17:18:55 -07002035 if (!dn || dn != of_stdout || console_set_on_cmdline)
Sascha Hauer5c19e952013-08-05 14:40:44 +02002036 return false;
Sergey Senozhatskydb179e02017-09-26 15:25:10 +09002037
2038 /*
2039 * XXX: cast `options' to char pointer to suppress complication
2040 * warnings: printk, UART and console drivers expect char pointer.
2041 */
2042 return !add_preferred_console(name, index, (char *)of_stdout_options);
Sascha Hauer5c19e952013-08-05 14:40:44 +02002043}
Grant Likely3482f2c2014-03-27 17:18:55 -07002044EXPORT_SYMBOL_GPL(of_console_check);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002045
2046/**
2047 * of_find_next_cache_node - Find a node's subsidiary cache
2048 * @np: node of type "cpu" or "cache"
2049 *
2050 * Returns a node pointer with refcount incremented, use
2051 * of_node_put() on it when done. Caller should hold a reference
2052 * to np.
2053 */
2054struct device_node *of_find_next_cache_node(const struct device_node *np)
2055{
Rob Herring91d96742017-05-04 12:30:07 -05002056 struct device_node *child, *cache_node;
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002057
Rob Herring91d96742017-05-04 12:30:07 -05002058 cache_node = of_parse_phandle(np, "l2-cache", 0);
2059 if (!cache_node)
2060 cache_node = of_parse_phandle(np, "next-level-cache", 0);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002061
Rob Herring91d96742017-05-04 12:30:07 -05002062 if (cache_node)
2063 return cache_node;
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002064
2065 /* OF on pmac has nodes instead of properties named "l2-cache"
2066 * beneath CPU nodes.
2067 */
2068 if (!strcmp(np->type, "cpu"))
2069 for_each_child_of_node(np, child)
2070 if (!strcmp(child->type, "cache"))
2071 return child;
2072
2073 return NULL;
2074}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002075
2076/**
Sudeep Holla5fa23532017-01-16 10:40:43 +00002077 * of_find_last_cache_level - Find the level at which the last cache is
2078 * present for the given logical cpu
2079 *
2080 * @cpu: cpu number(logical index) for which the last cache level is needed
2081 *
2082 * Returns the the level at which the last cache is present. It is exactly
2083 * same as the total number of cache levels for the given logical cpu.
2084 */
2085int of_find_last_cache_level(unsigned int cpu)
2086{
2087 u32 cache_level = 0;
2088 struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
2089
2090 while (np) {
2091 prev = np;
2092 of_node_put(np);
2093 np = of_find_next_cache_node(np);
2094 }
2095
2096 of_property_read_u32(prev, "cache-level", &cache_level);
2097
2098 return cache_level;
2099}