blob: 70f5fd08891bb2d64302782b546689857684c82a [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 Likely8a2b22a2014-07-23 17:05:06 -060042struct kset *of_kset;
Grant Likely75b57ec2014-02-20 18:02:11 +000043
44/*
Grant Likely8a2b22a2014-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}
Rob Herring1db5dec2018-10-22 09:03:54 -050070EXPORT_SYMBOL(of_node_name_eq);
Rob Herringf42b0e182018-08-27 07:50:47 -050071
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}
Rob Herring1db5dec2018-10-22 09:03:54 -050079EXPORT_SYMBOL(of_node_name_prefix);
Rob Herringf42b0e182018-08-27 07:50:47 -050080
Stephen Rothwell97e873e2007-05-01 16:26:07 +100081int of_n_addr_cells(struct device_node *np)
82{
Sergei Shtylyov88329632017-07-23 19:55:47 +030083 u32 cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100084
85 do {
86 if (np->parent)
87 np = np->parent;
Sergei Shtylyov88329632017-07-23 19:55:47 +030088 if (!of_property_read_u32(np, "#address-cells", &cells))
89 return cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100090 } while (np->parent);
91 /* No #address-cells property for the root node */
92 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
93}
94EXPORT_SYMBOL(of_n_addr_cells);
95
96int of_n_size_cells(struct device_node *np)
97{
Sergei Shtylyov88329632017-07-23 19:55:47 +030098 u32 cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100099
100 do {
101 if (np->parent)
102 np = np->parent;
Sergei Shtylyov88329632017-07-23 19:55:47 +0300103 if (!of_property_read_u32(np, "#size-cells", &cells))
104 return cells;
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000105 } while (np->parent);
106 /* No #size-cells property for the root node */
107 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
108}
109EXPORT_SYMBOL(of_n_size_cells);
110
Rob Herring0c3f0612013-09-17 10:42:50 -0500111#ifdef CONFIG_NUMA
112int __weak of_node_to_nid(struct device_node *np)
113{
Konstantin Khlebnikovc8fff7b2015-04-08 19:59:20 +0300114 return NUMA_NO_NODE;
Rob Herring0c3f0612013-09-17 10:42:50 -0500115}
116#endif
117
Frank Rowand0b3ce782018-03-04 16:14:47 -0800118static struct device_node **phandle_cache;
119static u32 phandle_cache_mask;
120
121/*
122 * Assumptions behind phandle_cache implementation:
123 * - phandle property values are in a contiguous range of 1..n
124 *
125 * If the assumptions do not hold, then
126 * - the phandle lookup overhead reduction provided by the cache
127 * will likely be less
128 */
Frank Rowandb9952b52018-07-12 14:00:07 -0700129void of_populate_phandle_cache(void)
Frank Rowand0b3ce782018-03-04 16:14:47 -0800130{
131 unsigned long flags;
132 u32 cache_entries;
133 struct device_node *np;
134 u32 phandles = 0;
135
136 raw_spin_lock_irqsave(&devtree_lock, flags);
137
138 kfree(phandle_cache);
139 phandle_cache = NULL;
140
141 for_each_of_allnodes(np)
142 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
143 phandles++;
144
Rob Herringe54192b2018-09-11 09:28:14 -0500145 if (!phandles)
146 goto out;
147
Frank Rowand0b3ce782018-03-04 16:14:47 -0800148 cache_entries = roundup_pow_of_two(phandles);
149 phandle_cache_mask = cache_entries - 1;
150
151 phandle_cache = kcalloc(cache_entries, sizeof(*phandle_cache),
152 GFP_ATOMIC);
153 if (!phandle_cache)
154 goto out;
155
156 for_each_of_allnodes(np)
157 if (np->phandle && np->phandle != OF_PHANDLE_ILLEGAL)
158 phandle_cache[np->phandle & phandle_cache_mask] = np;
159
160out:
161 raw_spin_unlock_irqrestore(&devtree_lock, flags);
162}
163
Frank Rowandb9952b52018-07-12 14:00:07 -0700164int of_free_phandle_cache(void)
Frank Rowand0b3ce782018-03-04 16:14:47 -0800165{
166 unsigned long flags;
167
168 raw_spin_lock_irqsave(&devtree_lock, flags);
169
170 kfree(phandle_cache);
171 phandle_cache = NULL;
172
173 raw_spin_unlock_irqrestore(&devtree_lock, flags);
174
175 return 0;
176}
Frank Rowandb9952b52018-07-12 14:00:07 -0700177#if !defined(CONFIG_MODULES)
Frank Rowand0b3ce782018-03-04 16:14:47 -0800178late_initcall_sync(of_free_phandle_cache);
179#endif
180
Sudeep Holla194ec9362015-05-14 15:28:24 +0100181void __init of_core_init(void)
Grant Likely75b57ec2014-02-20 18:02:11 +0000182{
183 struct device_node *np;
184
Frank Rowand0b3ce782018-03-04 16:14:47 -0800185 of_populate_phandle_cache();
186
Grant Likely75b57ec2014-02-20 18:02:11 +0000187 /* Create the kset, and register existing nodes */
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300188 mutex_lock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000189 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
190 if (!of_kset) {
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300191 mutex_unlock(&of_mutex);
Rob Herring606ad422016-06-15 08:32:18 -0500192 pr_err("failed to register existing nodes\n");
Sudeep Holla194ec9362015-05-14 15:28:24 +0100193 return;
Grant Likely75b57ec2014-02-20 18:02:11 +0000194 }
195 for_each_of_allnodes(np)
Grant Likely8a2b22a2014-07-23 17:05:06 -0600196 __of_attach_node_sysfs(np);
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300197 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +0000198
Grant Likely83570412012-11-06 21:03:27 +0000199 /* Symlink in /proc as required by userspace ABI */
Grant Likely5063e252014-10-03 16:28:27 +0100200 if (of_root)
Grant Likely75b57ec2014-02-20 18:02:11 +0000201 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
Grant Likely75b57ec2014-02-20 18:02:11 +0000202}
Grant Likely75b57ec2014-02-20 18:02:11 +0000203
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500204static struct property *__of_find_property(const struct device_node *np,
205 const char *name, int *lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000206{
207 struct property *pp;
208
Timur Tabi64e45662008-05-08 05:19:59 +1000209 if (!np)
210 return NULL;
211
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530212 for (pp = np->properties; pp; pp = pp->next) {
Stephen Rothwell581b6052007-04-24 16:46:53 +1000213 if (of_prop_cmp(pp->name, name) == 0) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +0530214 if (lenp)
Stephen Rothwell581b6052007-04-24 16:46:53 +1000215 *lenp = pp->length;
216 break;
217 }
218 }
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500219
220 return pp;
221}
222
223struct property *of_find_property(const struct device_node *np,
224 const char *name,
225 int *lenp)
226{
227 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500228 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500229
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500230 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500231 pp = __of_find_property(np, name, lenp);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500232 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell581b6052007-04-24 16:46:53 +1000233
234 return pp;
235}
236EXPORT_SYMBOL(of_find_property);
237
Grant Likely5063e252014-10-03 16:28:27 +0100238struct device_node *__of_find_all_nodes(struct device_node *prev)
239{
240 struct device_node *np;
241 if (!prev) {
242 np = of_root;
243 } else if (prev->child) {
244 np = prev->child;
245 } else {
246 /* Walk back up looking for a sibling, or the end of the structure */
247 np = prev;
248 while (np->parent && !np->sibling)
249 np = np->parent;
250 np = np->sibling; /* Might be null at the end of the tree */
251 }
252 return np;
253}
254
Grant Likelye91edcf2009-10-15 10:58:09 -0600255/**
256 * of_find_all_nodes - Get next node in global list
257 * @prev: Previous node or NULL to start iteration
258 * of_node_put() will be called on it
259 *
260 * Returns a node pointer with refcount incremented, use
261 * of_node_put() on it when done.
262 */
263struct device_node *of_find_all_nodes(struct device_node *prev)
264{
265 struct device_node *np;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000266 unsigned long flags;
Grant Likelye91edcf2009-10-15 10:58:09 -0600267
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000268 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100269 np = __of_find_all_nodes(prev);
270 of_node_get(np);
Grant Likelye91edcf2009-10-15 10:58:09 -0600271 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000272 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likelye91edcf2009-10-15 10:58:09 -0600273 return np;
274}
275EXPORT_SYMBOL(of_find_all_nodes);
276
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000277/*
278 * Find a property with a given name for a given node
279 * and return the value.
280 */
Grant Likelya25095d2014-07-15 23:25:43 -0600281const void *__of_get_property(const struct device_node *np,
282 const char *name, int *lenp)
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500283{
284 struct property *pp = __of_find_property(np, name, lenp);
285
286 return pp ? pp->value : NULL;
287}
288
289/*
290 * Find a property with a given name for a given node
291 * and return the value.
292 */
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000293const void *of_get_property(const struct device_node *np, const char *name,
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500294 int *lenp)
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000295{
296 struct property *pp = of_find_property(np, name, lenp);
297
298 return pp ? pp->value : NULL;
299}
300EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000301
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100302/*
303 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
304 *
305 * @cpu: logical cpu index of a core/thread
306 * @phys_id: physical identifier of a core/thread
307 *
308 * CPU logical to physical index mapping is architecture specific.
309 * However this __weak function provides a default match of physical
310 * id to logical cpu index. phys_id provided here is usually values read
311 * from the device tree which must match the hardware internal registers.
312 *
313 * Returns true if the physical identifier and the logical cpu index
314 * correspond to the same core/thread, false otherwise.
315 */
316bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
317{
318 return (u32)phys_id == cpu;
319}
320
321/**
322 * Checks if the given "prop_name" property holds the physical id of the
323 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
324 * NULL, local thread number within the core is returned in it.
325 */
326static bool __of_find_n_match_cpu_property(struct device_node *cpun,
327 const char *prop_name, int cpu, unsigned int *thread)
328{
329 const __be32 *cell;
330 int ac, prop_len, tid;
331 u64 hwid;
332
333 ac = of_n_addr_cells(cpun);
334 cell = of_get_property(cpun, prop_name, &prop_len);
Grant Likelyf3cea452013-10-04 17:24:26 +0100335 if (!cell || !ac)
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100336 return false;
Grant Likelyf3cea452013-10-04 17:24:26 +0100337 prop_len /= sizeof(*cell) * ac;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100338 for (tid = 0; tid < prop_len; tid++) {
339 hwid = of_read_number(cell, ac);
340 if (arch_match_cpu_phys_id(cpu, hwid)) {
341 if (thread)
342 *thread = tid;
343 return true;
344 }
345 cell += ac;
346 }
347 return false;
348}
349
David Millerd1cb9d12013-10-03 17:24:51 -0400350/*
351 * arch_find_n_match_cpu_physical_id - See if the given device node is
352 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
353 * else false. If 'thread' is non-NULL, the local thread number within the
354 * core is returned in it.
355 */
356bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
357 int cpu, unsigned int *thread)
358{
359 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
360 * for thread ids on PowerPC. If it doesn't exist fallback to
361 * standard "reg" property.
362 */
363 if (IS_ENABLED(CONFIG_PPC) &&
364 __of_find_n_match_cpu_property(cpun,
365 "ibm,ppc-interrupt-server#s",
366 cpu, thread))
367 return true;
368
Masahiro Yamada510bd062015-10-28 12:05:27 +0900369 return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
David Millerd1cb9d12013-10-03 17:24:51 -0400370}
371
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100372/**
373 * of_get_cpu_node - Get device node associated with the given logical CPU
374 *
375 * @cpu: CPU number(logical index) for which device node is required
376 * @thread: if not NULL, local thread number within the physical core is
377 * returned
378 *
379 * The main purpose of this function is to retrieve the device node for the
380 * given logical CPU index. It should be used to initialize the of_node in
381 * cpu device. Once of_node in cpu device is populated, all the further
382 * references can use that instead.
383 *
384 * CPU logical to physical index mapping is architecture specific and is built
385 * before booting secondary cores. This function uses arch_match_cpu_phys_id
386 * which can be overridden by architecture specific implementation.
387 *
Masahiro Yamada1c986e32016-04-20 10:18:46 +0900388 * Returns a node pointer for the logical cpu with refcount incremented, use
389 * of_node_put() on it when done. Returns NULL if not found.
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100390 */
391struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
392{
David Millerd1cb9d12013-10-03 17:24:51 -0400393 struct device_node *cpun;
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100394
David Millerd1cb9d12013-10-03 17:24:51 -0400395 for_each_node_by_type(cpun, "cpu") {
396 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
Sudeep KarkadaNagesha183912d2013-08-15 14:01:40 +0100397 return cpun;
398 }
399 return NULL;
400}
401EXPORT_SYMBOL(of_get_cpu_node);
402
Kevin Hao215a14c2014-02-19 16:15:45 +0800403/**
Suzuki K Poulosea0e71cd2018-01-02 11:25:27 +0000404 * of_cpu_node_to_id: Get the logical CPU number for a given device_node
405 *
406 * @cpu_node: Pointer to the device_node for CPU.
407 *
408 * Returns the logical CPU number of the given CPU device_node.
409 * Returns -ENODEV if the CPU is not found.
410 */
411int of_cpu_node_to_id(struct device_node *cpu_node)
412{
413 int cpu;
414 bool found = false;
415 struct device_node *np;
416
417 for_each_possible_cpu(cpu) {
418 np = of_cpu_device_node_get(cpu);
419 found = (cpu_node == np);
420 of_node_put(np);
421 if (found)
422 return cpu;
423 }
424
425 return -ENODEV;
426}
427EXPORT_SYMBOL(of_cpu_node_to_id);
428
429/**
Kevin Hao215a14c2014-02-19 16:15:45 +0800430 * __of_device_is_compatible() - Check if the node matches given constraints
431 * @device: pointer to node
432 * @compat: required compatible string, NULL or "" for any match
433 * @type: required device_type value, NULL or "" for any match
434 * @name: required node name, NULL or "" for any match
435 *
436 * Checks if the given @compat, @type and @name strings match the
437 * properties of the given @device. A constraints can be skipped by
438 * passing NULL or an empty string as the constraint.
439 *
440 * Returns 0 for no match, and a positive integer on match. The return
441 * value is a relative score with larger values indicating better
442 * matches. The score is weighted for the most specific compatible value
443 * to get the highest score. Matching type is next, followed by matching
444 * name. Practically speaking, this results in the following priority
445 * order for matches:
446 *
447 * 1. specific compatible && type && name
448 * 2. specific compatible && type
449 * 3. specific compatible && name
450 * 4. specific compatible
451 * 5. general compatible && type && name
452 * 6. general compatible && type
453 * 7. general compatible && name
454 * 8. general compatible
455 * 9. type && name
456 * 10. type
457 * 11. name
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000458 */
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500459static int __of_device_is_compatible(const struct device_node *device,
Kevin Hao215a14c2014-02-19 16:15:45 +0800460 const char *compat, const char *type, const char *name)
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000461{
Kevin Hao215a14c2014-02-19 16:15:45 +0800462 struct property *prop;
463 const char *cp;
464 int index = 0, score = 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000465
Kevin Hao215a14c2014-02-19 16:15:45 +0800466 /* Compatible match has highest priority */
467 if (compat && compat[0]) {
468 prop = __of_find_property(device, "compatible", NULL);
469 for (cp = of_prop_next_string(prop, NULL); cp;
470 cp = of_prop_next_string(prop, cp), index++) {
471 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
472 score = INT_MAX/2 - (index << 2);
473 break;
474 }
475 }
476 if (!score)
477 return 0;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000478 }
479
Kevin Hao215a14c2014-02-19 16:15:45 +0800480 /* Matching type is better than matching name */
481 if (type && type[0]) {
482 if (!device->type || of_node_cmp(type, device->type))
483 return 0;
484 score += 2;
485 }
486
487 /* Matching name is a bit better than not */
488 if (name && name[0]) {
489 if (!device->name || of_node_cmp(name, device->name))
490 return 0;
491 score++;
492 }
493
494 return score;
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000495}
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500496
497/** Checks if the given "compat" string matches one of the strings in
498 * the device's "compatible" property
499 */
500int of_device_is_compatible(const struct device_node *device,
501 const char *compat)
502{
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500503 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500504 int res;
505
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500506 raw_spin_lock_irqsave(&devtree_lock, flags);
Kevin Hao215a14c2014-02-19 16:15:45 +0800507 res = __of_device_is_compatible(device, compat, NULL, NULL);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500508 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500509 return res;
510}
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000511EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000512
Benjamin Herrenschmidtb9c13fe2016-07-08 08:35:59 +1000513/** Checks if the device is compatible with any of the entries in
514 * a NULL terminated array of strings. Returns the best match
515 * score or 0.
516 */
517int of_device_compatible_match(struct device_node *device,
518 const char *const *compat)
519{
520 unsigned int tmp, score = 0;
521
522 if (!compat)
523 return 0;
524
525 while (*compat) {
526 tmp = of_device_is_compatible(device, *compat);
527 if (tmp > score)
528 score = tmp;
529 compat++;
530 }
531
532 return score;
533}
534
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000535/**
Grant Likely71a157e2010-02-01 21:34:14 -0700536 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700537 * @compat: compatible string to look for in root node's compatible property.
538 *
Kevin Cernekee25c7a1d2014-11-12 12:54:00 -0800539 * Returns a positive integer if the root node has the given value in its
Grant Likely1f43cfb2010-01-28 13:47:25 -0700540 * compatible property.
541 */
Grant Likely71a157e2010-02-01 21:34:14 -0700542int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700543{
544 struct device_node *root;
545 int rc = 0;
546
547 root = of_find_node_by_path("/");
548 if (root) {
549 rc = of_device_is_compatible(root, compat);
550 of_node_put(root);
551 }
552 return rc;
553}
Grant Likely71a157e2010-02-01 21:34:14 -0700554EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700555
556/**
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700557 * __of_device_is_available - check if a device is available for use
Josh Boyer834d97d2008-03-27 00:33:14 +1100558 *
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700559 * @device: Node to check for availability, with locks already held
Josh Boyer834d97d2008-03-27 00:33:14 +1100560 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800561 * Returns true if the status property is absent or set to "okay" or "ok",
562 * false otherwise
Josh Boyer834d97d2008-03-27 00:33:14 +1100563 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800564static bool __of_device_is_available(const struct device_node *device)
Josh Boyer834d97d2008-03-27 00:33:14 +1100565{
566 const char *status;
567 int statlen;
568
Xiubo Li42ccd782014-01-13 11:07:28 +0800569 if (!device)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800570 return false;
Xiubo Li42ccd782014-01-13 11:07:28 +0800571
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700572 status = __of_get_property(device, "status", &statlen);
Josh Boyer834d97d2008-03-27 00:33:14 +1100573 if (status == NULL)
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800574 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100575
576 if (statlen > 0) {
577 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800578 return true;
Josh Boyer834d97d2008-03-27 00:33:14 +1100579 }
580
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800581 return false;
Josh Boyer834d97d2008-03-27 00:33:14 +1100582}
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700583
584/**
585 * of_device_is_available - check if a device is available for use
586 *
587 * @device: Node to check for availability
588 *
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800589 * Returns true if the status property is absent or set to "okay" or "ok",
590 * false otherwise
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700591 */
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800592bool of_device_is_available(const struct device_node *device)
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700593{
594 unsigned long flags;
Kevin Cernekee53a4ab92014-11-12 12:54:01 -0800595 bool res;
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700596
597 raw_spin_lock_irqsave(&devtree_lock, flags);
598 res = __of_device_is_available(device);
599 raw_spin_unlock_irqrestore(&devtree_lock, flags);
600 return res;
601
602}
Josh Boyer834d97d2008-03-27 00:33:14 +1100603EXPORT_SYMBOL(of_device_is_available);
604
605/**
Kevin Cernekee37786c72015-04-09 13:05:14 -0700606 * of_device_is_big_endian - check if a device has BE registers
607 *
608 * @device: Node to check for endianness
609 *
610 * Returns true if the device has a "big-endian" property, or if the kernel
611 * was compiled for BE *and* the device has a "native-endian" property.
612 * Returns false otherwise.
613 *
614 * Callers would nominally use ioread32be/iowrite32be if
615 * of_device_is_big_endian() == true, or readl/writel otherwise.
616 */
617bool of_device_is_big_endian(const struct device_node *device)
618{
619 if (of_property_read_bool(device, "big-endian"))
620 return true;
621 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
622 of_property_read_bool(device, "native-endian"))
623 return true;
624 return false;
625}
626EXPORT_SYMBOL(of_device_is_big_endian);
627
628/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000629 * of_get_parent - Get a node's parent if any
630 * @node: Node to get parent
631 *
632 * Returns a node pointer with refcount incremented, use
633 * of_node_put() on it when done.
634 */
635struct device_node *of_get_parent(const struct device_node *node)
636{
637 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500638 unsigned long flags;
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000639
640 if (!node)
641 return NULL;
642
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500643 raw_spin_lock_irqsave(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000644 np = of_node_get(node->parent);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500645 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000646 return np;
647}
648EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000649
650/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000651 * of_get_next_parent - Iterate to a node's parent
652 * @node: Node to get parent of
653 *
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +0200654 * This is like of_get_parent() except that it drops the
655 * refcount on the passed node, making it suitable for iterating
656 * through a node's parents.
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000657 *
658 * Returns a node pointer with refcount incremented, use
659 * of_node_put() on it when done.
660 */
661struct device_node *of_get_next_parent(struct device_node *node)
662{
663 struct device_node *parent;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500664 unsigned long flags;
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000665
666 if (!node)
667 return NULL;
668
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500669 raw_spin_lock_irqsave(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000670 parent = of_node_get(node->parent);
671 of_node_put(node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500672 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000673 return parent;
674}
Guennadi Liakhovetski6695be62013-04-02 12:28:11 -0300675EXPORT_SYMBOL(of_get_next_parent);
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000676
Grant Likely0d0e02d2014-05-22 01:04:17 +0900677static struct device_node *__of_get_next_child(const struct device_node *node,
678 struct device_node *prev)
679{
680 struct device_node *next;
681
Florian Fainelli43cb4362014-05-28 10:39:02 -0700682 if (!node)
683 return NULL;
684
Grant Likely0d0e02d2014-05-22 01:04:17 +0900685 next = prev ? prev->sibling : node->child;
686 for (; next; next = next->sibling)
687 if (of_node_get(next))
688 break;
689 of_node_put(prev);
690 return next;
691}
692#define __for_each_child_of_node(parent, child) \
693 for (child = __of_get_next_child(parent, NULL); child != NULL; \
694 child = __of_get_next_child(parent, child))
695
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000696/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000697 * of_get_next_child - Iterate a node childs
698 * @node: parent node
699 * @prev: previous child of the parent node, or NULL to get first
700 *
Baruch Siach64808272015-03-19 14:03:41 +0200701 * Returns a node pointer with refcount incremented, use of_node_put() on
702 * it when done. Returns NULL when prev is the last child. Decrements the
703 * refcount of prev.
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000704 */
705struct device_node *of_get_next_child(const struct device_node *node,
706 struct device_node *prev)
707{
708 struct device_node *next;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500709 unsigned long flags;
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000710
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500711 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely0d0e02d2014-05-22 01:04:17 +0900712 next = __of_get_next_child(node, prev);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500713 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000714 return next;
715}
716EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000717
718/**
Timur Tabi32961932012-08-14 13:20:23 +0000719 * of_get_next_available_child - Find the next available child node
720 * @node: parent node
721 * @prev: previous child of the parent node, or NULL to get first
722 *
723 * This function is like of_get_next_child(), except that it
724 * automatically skips any disabled nodes (i.e. status = "disabled").
725 */
726struct device_node *of_get_next_available_child(const struct device_node *node,
727 struct device_node *prev)
728{
729 struct device_node *next;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000730 unsigned long flags;
Timur Tabi32961932012-08-14 13:20:23 +0000731
Florian Fainelli43cb4362014-05-28 10:39:02 -0700732 if (!node)
733 return NULL;
734
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000735 raw_spin_lock_irqsave(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000736 next = prev ? prev->sibling : node->child;
737 for (; next; next = next->sibling) {
Stephen Warrenc31a0c02013-02-11 14:15:32 -0700738 if (!__of_device_is_available(next))
Timur Tabi32961932012-08-14 13:20:23 +0000739 continue;
740 if (of_node_get(next))
741 break;
742 }
743 of_node_put(prev);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +1000744 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Timur Tabi32961932012-08-14 13:20:23 +0000745 return next;
746}
747EXPORT_SYMBOL(of_get_next_available_child);
748
749/**
Johan Hovold36156f92018-08-27 10:21:45 +0200750 * of_get_compatible_child - Find compatible child node
751 * @parent: parent node
752 * @compatible: compatible string
753 *
754 * Lookup child node whose compatible property contains the given compatible
755 * string.
756 *
757 * Returns a node pointer with refcount incremented, use of_node_put() on it
758 * when done; or NULL if not found.
759 */
760struct device_node *of_get_compatible_child(const struct device_node *parent,
761 const char *compatible)
762{
763 struct device_node *child;
764
765 for_each_child_of_node(parent, child) {
766 if (of_device_is_compatible(child, compatible))
767 break;
768 }
769
770 return child;
771}
772EXPORT_SYMBOL(of_get_compatible_child);
773
774/**
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100775 * of_get_child_by_name - Find the child node by name for a given parent
776 * @node: parent node
777 * @name: child name to look for.
778 *
779 * This function looks for child node for given matching name
780 *
781 * Returns a node pointer if found, with refcount incremented, use
782 * of_node_put() on it when done.
783 * Returns NULL if node is not found.
784 */
785struct device_node *of_get_child_by_name(const struct device_node *node,
786 const char *name)
787{
788 struct device_node *child;
789
790 for_each_child_of_node(node, child)
791 if (child->name && (of_node_cmp(child->name, name) == 0))
792 break;
793 return child;
794}
795EXPORT_SYMBOL(of_get_child_by_name);
796
Frank Rowande0a58f32017-10-17 16:36:31 -0700797struct device_node *__of_find_node_by_path(struct device_node *parent,
Grant Likelyc22e6502014-03-14 17:07:12 +0000798 const char *path)
799{
800 struct device_node *child;
Leif Lindholm106937e2015-03-06 16:52:53 +0000801 int len;
Grant Likelyc22e6502014-03-14 17:07:12 +0000802
Brian Norris721a09e2015-03-17 12:30:31 -0700803 len = strcspn(path, "/:");
Grant Likelyc22e6502014-03-14 17:07:12 +0000804 if (!len)
805 return NULL;
806
807 __for_each_child_of_node(parent, child) {
Rob Herring95e6b1f2017-06-01 18:00:00 -0500808 const char *name = kbasename(child->full_name);
Grant Likelyc22e6502014-03-14 17:07:12 +0000809 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
810 return child;
811 }
812 return NULL;
813}
814
Rob Herring27497e12017-06-02 12:43:18 -0500815struct device_node *__of_find_node_by_full_path(struct device_node *node,
816 const char *path)
817{
818 const char *separator = strchr(path, ':');
819
820 while (node && *path == '/') {
821 struct device_node *tmp = node;
822
823 path++; /* Increment past '/' delimiter */
824 node = __of_find_node_by_path(node, path);
825 of_node_put(tmp);
826 path = strchrnul(path, '/');
827 if (separator && separator < path)
828 break;
829 }
830 return node;
831}
832
Srinivas Kandagatla9c197612012-09-18 08:10:28 +0100833/**
Leif Lindholm75c28c02014-11-28 11:34:28 +0000834 * of_find_node_opts_by_path - Find a node matching a full OF path
Grant Likelyc22e6502014-03-14 17:07:12 +0000835 * @path: Either the full path to match, or if the path does not
836 * start with '/', the name of a property of the /aliases
837 * node (an alias). In the case of an alias, the node
838 * matching the alias' value will be returned.
Leif Lindholm75c28c02014-11-28 11:34:28 +0000839 * @opts: Address of a pointer into which to store the start of
840 * an options string appended to the end of the path with
841 * a ':' separator.
Grant Likelyc22e6502014-03-14 17:07:12 +0000842 *
843 * Valid paths:
844 * /foo/bar Full path
845 * foo Valid alias
846 * foo/bar Valid alias + relative path
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000847 *
848 * Returns a node pointer with refcount incremented, use
849 * of_node_put() on it when done.
850 */
Leif Lindholm75c28c02014-11-28 11:34:28 +0000851struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000852{
Grant Likelyc22e6502014-03-14 17:07:12 +0000853 struct device_node *np = NULL;
854 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500855 unsigned long flags;
Leif Lindholm75c28c02014-11-28 11:34:28 +0000856 const char *separator = strchr(path, ':');
857
858 if (opts)
859 *opts = separator ? separator + 1 : NULL;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000860
Grant Likelyc22e6502014-03-14 17:07:12 +0000861 if (strcmp(path, "/") == 0)
Grant Likely5063e252014-10-03 16:28:27 +0100862 return of_node_get(of_root);
Grant Likelyc22e6502014-03-14 17:07:12 +0000863
864 /* The path could begin with an alias */
865 if (*path != '/') {
Leif Lindholm106937e2015-03-06 16:52:53 +0000866 int len;
867 const char *p = separator;
868
869 if (!p)
870 p = strchrnul(path, '/');
871 len = p - path;
Grant Likelyc22e6502014-03-14 17:07:12 +0000872
873 /* of_aliases must not be NULL */
874 if (!of_aliases)
875 return NULL;
876
877 for_each_property_of_node(of_aliases, pp) {
878 if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
879 np = of_find_node_by_path(pp->value);
880 break;
881 }
882 }
883 if (!np)
884 return NULL;
885 path = p;
886 }
887
888 /* Step down the tree matching path components */
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500889 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likelyc22e6502014-03-14 17:07:12 +0000890 if (!np)
Grant Likely5063e252014-10-03 16:28:27 +0100891 np = of_node_get(of_root);
Rob Herring27497e12017-06-02 12:43:18 -0500892 np = __of_find_node_by_full_path(np, path);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500893 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000894 return np;
895}
Leif Lindholm75c28c02014-11-28 11:34:28 +0000896EXPORT_SYMBOL(of_find_node_opts_by_path);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000897
898/**
899 * of_find_node_by_name - Find a node by its "name" property
Stephen Boyd02a876b2017-11-17 08:53:21 -0800900 * @from: The node to start searching from or NULL; the node
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000901 * you pass will not be searched, only the next one
Stephen Boyd02a876b2017-11-17 08:53:21 -0800902 * will. Typically, you pass what the previous call
903 * returned. of_node_put() will be called on @from.
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000904 * @name: The name string to match against
905 *
906 * Returns a node pointer with refcount incremented, use
907 * of_node_put() on it when done.
908 */
909struct device_node *of_find_node_by_name(struct device_node *from,
910 const char *name)
911{
912 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500913 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000914
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500915 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100916 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000917 if (np->name && (of_node_cmp(np->name, name) == 0)
918 && of_node_get(np))
919 break;
920 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500921 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000922 return np;
923}
924EXPORT_SYMBOL(of_find_node_by_name);
925
926/**
927 * of_find_node_by_type - Find a node by its "device_type" property
928 * @from: The node to start searching from, or NULL to start searching
929 * the entire device tree. The node you pass will not be
930 * searched, only the next one will; typically, you pass
931 * what the previous call returned. of_node_put() will be
932 * called on from for you.
933 * @type: The type string to match against
934 *
935 * Returns a node pointer with refcount incremented, use
936 * of_node_put() on it when done.
937 */
938struct device_node *of_find_node_by_type(struct device_node *from,
939 const char *type)
940{
941 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500942 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000943
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500944 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100945 for_each_of_allnodes_from(from, np)
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000946 if (np->type && (of_node_cmp(np->type, type) == 0)
947 && of_node_get(np))
948 break;
949 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500950 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000951 return np;
952}
953EXPORT_SYMBOL(of_find_node_by_type);
954
955/**
956 * of_find_compatible_node - Find a node based on type and one of the
957 * tokens in its "compatible" property
958 * @from: The node to start searching from or NULL, the node
959 * you pass will not be searched, only the next one
960 * will; typically, you pass what the previous call
961 * returned. of_node_put() will be called on it
962 * @type: The type string to match "device_type" or NULL to ignore
963 * @compatible: The string to match to one of the tokens in the device
964 * "compatible" list.
965 *
966 * Returns a node pointer with refcount incremented, use
967 * of_node_put() on it when done.
968 */
969struct device_node *of_find_compatible_node(struct device_node *from,
970 const char *type, const char *compatible)
971{
972 struct device_node *np;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500973 unsigned long flags;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000974
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500975 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +0100976 for_each_of_allnodes_from(from, np)
Kevin Hao215a14c2014-02-19 16:15:45 +0800977 if (__of_device_is_compatible(np, compatible, type, NULL) &&
Thomas Gleixner28d0e362013-01-25 13:21:47 -0500978 of_node_get(np))
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000979 break;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000980 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -0500981 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000982 return np;
983}
984EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100985
986/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000987 * of_find_node_with_property - Find a node which has a property with
988 * the given name.
989 * @from: The node to start searching from or NULL, the node
990 * you pass will not be searched, only the next one
991 * will; typically, you pass what the previous call
992 * returned. of_node_put() will be called on it
993 * @prop_name: The name of the property to look for.
994 *
995 * Returns a node pointer with refcount incremented, use
996 * of_node_put() on it when done.
997 */
998struct device_node *of_find_node_with_property(struct device_node *from,
999 const char *prop_name)
1000{
1001 struct device_node *np;
1002 struct property *pp;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001003 unsigned long flags;
Michael Ellerman1e291b12008-11-12 18:54:42 +00001004
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001005 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001006 for_each_of_allnodes_from(from, np) {
Sachin Kamata3a7cab2012-06-27 09:44:45 +05301007 for (pp = np->properties; pp; pp = pp->next) {
Michael Ellerman1e291b12008-11-12 18:54:42 +00001008 if (of_prop_cmp(pp->name, prop_name) == 0) {
1009 of_node_get(np);
1010 goto out;
1011 }
1012 }
1013 }
1014out:
1015 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001016 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Michael Ellerman1e291b12008-11-12 18:54:42 +00001017 return np;
1018}
1019EXPORT_SYMBOL(of_find_node_with_property);
1020
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001021static
1022const struct of_device_id *__of_match_node(const struct of_device_id *matches,
1023 const struct device_node *node)
Grant Likely283029d2008-01-09 06:20:40 +11001024{
Kevin Hao215a14c2014-02-19 16:15:45 +08001025 const struct of_device_id *best_match = NULL;
1026 int score, best_score = 0;
1027
Grant Likelya52f07e2011-03-18 10:21:29 -06001028 if (!matches)
1029 return NULL;
1030
Kevin Hao215a14c2014-02-19 16:15:45 +08001031 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
1032 score = __of_device_is_compatible(node, matches->compatible,
1033 matches->type, matches->name);
1034 if (score > best_score) {
1035 best_match = matches;
1036 best_score = score;
1037 }
Kevin Hao4e8ca6e2014-02-14 13:22:45 +08001038 }
Kevin Hao215a14c2014-02-19 16:15:45 +08001039
1040 return best_match;
Grant Likely283029d2008-01-09 06:20:40 +11001041}
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001042
1043/**
Geert Uytterhoevenc50949d2014-10-22 11:44:54 +02001044 * of_match_node - Tell if a device_node has a matching of_match structure
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001045 * @matches: array of of device match structures to search in
1046 * @node: the of device structure to match against
1047 *
Kevin Hao71c54982014-02-18 15:57:29 +08001048 * Low level utility function used by device matching.
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001049 */
1050const struct of_device_id *of_match_node(const struct of_device_id *matches,
1051 const struct device_node *node)
1052{
1053 const struct of_device_id *match;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001054 unsigned long flags;
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001055
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001056 raw_spin_lock_irqsave(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001057 match = __of_match_node(matches, node);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001058 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001059 return match;
1060}
Grant Likely283029d2008-01-09 06:20:40 +11001061EXPORT_SYMBOL(of_match_node);
1062
1063/**
Stephen Warren50c8af42012-11-20 16:12:20 -07001064 * of_find_matching_node_and_match - Find a node based on an of_device_id
1065 * match table.
Grant Likely283029d2008-01-09 06:20:40 +11001066 * @from: The node to start searching from or NULL, the node
1067 * you pass will not be searched, only the next one
1068 * will; typically, you pass what the previous call
1069 * returned. of_node_put() will be called on it
1070 * @matches: array of of device match structures to search in
Stephen Warren50c8af42012-11-20 16:12:20 -07001071 * @match Updated to point at the matches entry which matched
Grant Likely283029d2008-01-09 06:20:40 +11001072 *
1073 * Returns a node pointer with refcount incremented, use
1074 * of_node_put() on it when done.
1075 */
Stephen Warren50c8af42012-11-20 16:12:20 -07001076struct device_node *of_find_matching_node_and_match(struct device_node *from,
1077 const struct of_device_id *matches,
1078 const struct of_device_id **match)
Grant Likely283029d2008-01-09 06:20:40 +11001079{
1080 struct device_node *np;
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001081 const struct of_device_id *m;
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001082 unsigned long flags;
Grant Likely283029d2008-01-09 06:20:40 +11001083
Stephen Warren50c8af42012-11-20 16:12:20 -07001084 if (match)
1085 *match = NULL;
1086
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001087 raw_spin_lock_irqsave(&devtree_lock, flags);
Grant Likely5063e252014-10-03 16:28:27 +01001088 for_each_of_allnodes_from(from, np) {
Thomas Gleixner28d0e362013-01-25 13:21:47 -05001089 m = __of_match_node(matches, np);
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001090 if (m && of_node_get(np)) {
Stephen Warren50c8af42012-11-20 16:12:20 -07001091 if (match)
Thomas Abrahamdc71bcf2013-01-19 10:20:42 -08001092 *match = m;
Grant Likely283029d2008-01-09 06:20:40 +11001093 break;
Stephen Warren50c8af42012-11-20 16:12:20 -07001094 }
Grant Likely283029d2008-01-09 06:20:40 +11001095 }
1096 of_node_put(from);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001097 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely283029d2008-01-09 06:20:40 +11001098 return np;
1099}
Grant Likely80c20222012-12-19 10:45:36 +00001100EXPORT_SYMBOL(of_find_matching_node_and_match);
Grant Likely3f07af42008-07-25 22:25:13 -04001101
1102/**
Grant Likely3f07af42008-07-25 22:25:13 -04001103 * of_modalias_node - Lookup appropriate modalias for a device node
1104 * @node: pointer to a device tree node
1105 * @modalias: Pointer to buffer that modalias value will be copied into
1106 * @len: Length of modalias value
1107 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001108 * Based on the value of the compatible property, this routine will attempt
1109 * to choose an appropriate modalias value for a particular device tree node.
1110 * It does this by stripping the manufacturer prefix (as delimited by a ',')
1111 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -04001112 *
Grant Likely2ffe8c52010-06-08 07:48:19 -06001113 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -04001114 */
1115int of_modalias_node(struct device_node *node, char *modalias, int len)
1116{
Grant Likely2ffe8c52010-06-08 07:48:19 -06001117 const char *compatible, *p;
1118 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -04001119
1120 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -06001121 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -04001122 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -04001123 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -06001124 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -04001125 return 0;
1126}
1127EXPORT_SYMBOL_GPL(of_modalias_node);
1128
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001129/**
Jeremy Kerr89751a72010-02-01 21:34:11 -07001130 * of_find_node_by_phandle - Find a node given a phandle
1131 * @handle: phandle of the node to find
1132 *
1133 * Returns a node pointer with refcount incremented, use
1134 * of_node_put() on it when done.
1135 */
1136struct device_node *of_find_node_by_phandle(phandle handle)
1137{
Frank Rowand0b3ce782018-03-04 16:14:47 -08001138 struct device_node *np = NULL;
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001139 unsigned long flags;
Frank Rowand0b3ce782018-03-04 16:14:47 -08001140 phandle masked_handle;
Jeremy Kerr89751a72010-02-01 21:34:11 -07001141
Grant Likelyfc59b442014-10-02 13:08:02 +01001142 if (!handle)
1143 return NULL;
1144
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001145 raw_spin_lock_irqsave(&devtree_lock, flags);
Frank Rowand0b3ce782018-03-04 16:14:47 -08001146
1147 masked_handle = handle & phandle_cache_mask;
1148
1149 if (phandle_cache) {
1150 if (phandle_cache[masked_handle] &&
1151 handle == phandle_cache[masked_handle]->phandle)
1152 np = phandle_cache[masked_handle];
1153 }
1154
1155 if (!np) {
1156 for_each_of_allnodes(np)
1157 if (np->phandle == handle) {
1158 if (phandle_cache)
1159 phandle_cache[masked_handle] = np;
1160 break;
1161 }
1162 }
1163
Jeremy Kerr89751a72010-02-01 21:34:11 -07001164 of_node_get(np);
Benjamin Herrenschmidtd25d8692013-06-12 15:39:04 +10001165 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Jeremy Kerr89751a72010-02-01 21:34:11 -07001166 return np;
1167}
1168EXPORT_SYMBOL(of_find_node_by_phandle);
1169
Grant Likely624cfca2013-10-11 22:05:10 +01001170void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1171{
1172 int i;
Rob Herring0d638a02017-06-01 15:50:55 -05001173 printk("%s %pOF", msg, args->np);
Marcin Nowakowski4aa66342016-12-01 09:00:55 +01001174 for (i = 0; i < args->args_count; i++) {
1175 const char delim = i ? ',' : ':';
1176
1177 pr_cont("%c%08x", delim, args->args[i]);
1178 }
1179 pr_cont("\n");
Grant Likely624cfca2013-10-11 22:05:10 +01001180}
1181
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001182int of_phandle_iterator_init(struct of_phandle_iterator *it,
1183 const struct device_node *np,
1184 const char *list_name,
1185 const char *cells_name,
1186 int cell_count)
1187{
1188 const __be32 *list;
1189 int size;
1190
1191 memset(it, 0, sizeof(*it));
1192
1193 list = of_get_property(np, list_name, &size);
1194 if (!list)
1195 return -ENOENT;
1196
1197 it->cells_name = cells_name;
1198 it->cell_count = cell_count;
1199 it->parent = np;
1200 it->list_end = list + size / sizeof(*list);
1201 it->phandle_end = list;
1202 it->cur = list;
1203
1204 return 0;
1205}
Kuninori Morimoto00bab232017-04-20 01:31:16 +00001206EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001207
Joerg Roedelcd209b42016-04-04 17:49:18 +02001208int of_phandle_iterator_next(struct of_phandle_iterator *it)
1209{
1210 uint32_t count = 0;
1211
1212 if (it->node) {
1213 of_node_put(it->node);
1214 it->node = NULL;
1215 }
1216
1217 if (!it->cur || it->phandle_end >= it->list_end)
1218 return -ENOENT;
1219
1220 it->cur = it->phandle_end;
1221
1222 /* If phandle is 0, then it is an empty entry with no arguments. */
1223 it->phandle = be32_to_cpup(it->cur++);
1224
1225 if (it->phandle) {
1226
1227 /*
1228 * Find the provider node and parse the #*-cells property to
1229 * determine the argument length.
1230 */
1231 it->node = of_find_node_by_phandle(it->phandle);
1232
1233 if (it->cells_name) {
1234 if (!it->node) {
Rob Herring0d638a02017-06-01 15:50:55 -05001235 pr_err("%pOF: could not find phandle\n",
1236 it->parent);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001237 goto err;
1238 }
1239
1240 if (of_property_read_u32(it->node, it->cells_name,
1241 &count)) {
Rob Herring0d638a02017-06-01 15:50:55 -05001242 pr_err("%pOF: could not get %s for %pOF\n",
1243 it->parent,
Joerg Roedelcd209b42016-04-04 17:49:18 +02001244 it->cells_name,
Rob Herring0d638a02017-06-01 15:50:55 -05001245 it->node);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001246 goto err;
1247 }
1248 } else {
1249 count = it->cell_count;
1250 }
1251
1252 /*
1253 * Make sure that the arguments actually fit in the remaining
1254 * property data length
1255 */
1256 if (it->cur + count > it->list_end) {
Rob Herring0d638a02017-06-01 15:50:55 -05001257 pr_err("%pOF: arguments longer than property\n",
1258 it->parent);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001259 goto err;
1260 }
1261 }
1262
1263 it->phandle_end = it->cur + count;
1264 it->cur_count = count;
1265
1266 return 0;
1267
1268err:
1269 if (it->node) {
1270 of_node_put(it->node);
1271 it->node = NULL;
1272 }
1273
1274 return -EINVAL;
1275}
Kuninori Morimoto00bab232017-04-20 01:31:16 +00001276EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
Joerg Roedelcd209b42016-04-04 17:49:18 +02001277
Joerg Roedelabdaa772016-04-04 17:49:21 +02001278int of_phandle_iterator_args(struct of_phandle_iterator *it,
1279 uint32_t *args,
1280 int size)
1281{
1282 int i, count;
1283
1284 count = it->cur_count;
1285
1286 if (WARN_ON(size < count))
1287 count = size;
1288
1289 for (i = 0; i < count; i++)
1290 args[i] = be32_to_cpup(it->cur++);
1291
1292 return count;
1293}
1294
Grant Likelybd69f732013-02-10 22:57:21 +00001295static int __of_parse_phandle_with_args(const struct device_node *np,
1296 const char *list_name,
Stephen Warren035fd942013-08-14 15:27:10 -06001297 const char *cells_name,
1298 int cell_count, int index,
Grant Likelybd69f732013-02-10 22:57:21 +00001299 struct of_phandle_args *out_args)
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001300{
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001301 struct of_phandle_iterator it;
1302 int rc, cur_index = 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001303
Grant Likely15c9a0a2011-12-12 09:25:57 -07001304 /* Loop over the phandles until all the requested entry is found */
Joerg Roedelf623ce92016-04-04 17:49:20 +02001305 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
Grant Likely15c9a0a2011-12-12 09:25:57 -07001306 /*
Joerg Roedelcd209b42016-04-04 17:49:18 +02001307 * All of the error cases bail out of the loop, so at
Grant Likely15c9a0a2011-12-12 09:25:57 -07001308 * this point, the parsing is successful. If the requested
1309 * index matches, then fill the out_args structure and return,
1310 * or return -ENOENT for an empty entry.
1311 */
Grant Likely23ce04c2013-02-12 21:21:49 +00001312 rc = -ENOENT;
Grant Likely15c9a0a2011-12-12 09:25:57 -07001313 if (cur_index == index) {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001314 if (!it.phandle)
Grant Likely23ce04c2013-02-12 21:21:49 +00001315 goto err;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001316
Grant Likely15c9a0a2011-12-12 09:25:57 -07001317 if (out_args) {
Joerg Roedelabdaa772016-04-04 17:49:21 +02001318 int c;
1319
1320 c = of_phandle_iterator_args(&it,
1321 out_args->args,
1322 MAX_PHANDLE_ARGS);
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001323 out_args->np = it.node;
Joerg Roedelabdaa772016-04-04 17:49:21 +02001324 out_args->args_count = c;
Tang Yuantianb855f162013-04-10 11:36:39 +08001325 } else {
Joerg Roedel74e1fbb2016-04-04 17:49:17 +02001326 of_node_put(it.node);
Grant Likely15c9a0a2011-12-12 09:25:57 -07001327 }
Grant Likely23ce04c2013-02-12 21:21:49 +00001328
1329 /* Found it! return success */
Grant Likely15c9a0a2011-12-12 09:25:57 -07001330 return 0;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001331 }
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001332
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001333 cur_index++;
1334 }
1335
Grant Likely23ce04c2013-02-12 21:21:49 +00001336 /*
1337 * Unlock node before returning result; will be one of:
1338 * -ENOENT : index is for empty phandle
1339 * -EINVAL : parsing error on data
1340 */
Joerg Roedelcd209b42016-04-04 17:49:18 +02001341
Grant Likely23ce04c2013-02-12 21:21:49 +00001342 err:
Markus Elfringbeab47d2016-07-19 22:42:22 +02001343 of_node_put(it.node);
Grant Likely23ce04c2013-02-12 21:21:49 +00001344 return rc;
Anton Vorontsov64b60e02008-10-10 04:43:17 +00001345}
Grant Likelybd69f732013-02-10 22:57:21 +00001346
Stephen Warreneded9dd2013-08-14 15:27:08 -06001347/**
Stephen Warren5fba49e2013-08-14 15:27:09 -06001348 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1349 * @np: Pointer to device node holding phandle property
1350 * @phandle_name: Name of property holding a phandle value
1351 * @index: For properties holding a table of phandles, this is the index into
1352 * the table
1353 *
1354 * Returns the device_node pointer with refcount incremented. Use
1355 * of_node_put() on it when done.
1356 */
1357struct device_node *of_parse_phandle(const struct device_node *np,
1358 const char *phandle_name, int index)
1359{
Stephen Warren91d99422013-08-14 15:27:11 -06001360 struct of_phandle_args args;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001361
Stephen Warren91d99422013-08-14 15:27:11 -06001362 if (index < 0)
Stephen Warren5fba49e2013-08-14 15:27:09 -06001363 return NULL;
1364
Stephen Warren91d99422013-08-14 15:27:11 -06001365 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1366 index, &args))
1367 return NULL;
1368
1369 return args.np;
Stephen Warren5fba49e2013-08-14 15:27:09 -06001370}
1371EXPORT_SYMBOL(of_parse_phandle);
1372
1373/**
Stephen Warreneded9dd2013-08-14 15:27:08 -06001374 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1375 * @np: pointer to a device tree node containing a list
1376 * @list_name: property name that contains a list
1377 * @cells_name: property name that specifies phandles' arguments count
1378 * @index: index of a phandle to parse out
1379 * @out_args: optional pointer to output arguments structure (will be filled)
1380 *
1381 * This function is useful to parse lists of phandles and their arguments.
1382 * Returns 0 on success and fills out_args, on error returns appropriate
1383 * errno value.
1384 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001385 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warreneded9dd2013-08-14 15:27:08 -06001386 * pointer.
1387 *
1388 * Example:
1389 *
1390 * phandle1: node1 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001391 * #list-cells = <2>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001392 * }
1393 *
1394 * phandle2: node2 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001395 * #list-cells = <1>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001396 * }
1397 *
1398 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001399 * list = <&phandle1 1 2 &phandle2 3>;
Stephen Warreneded9dd2013-08-14 15:27:08 -06001400 * }
1401 *
1402 * To get a device_node of the `node2' node you may call this:
1403 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1404 */
Grant Likelybd69f732013-02-10 22:57:21 +00001405int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1406 const char *cells_name, int index,
1407 struct of_phandle_args *out_args)
1408{
1409 if (index < 0)
1410 return -EINVAL;
Stephen Warren035fd942013-08-14 15:27:10 -06001411 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1412 index, out_args);
Grant Likelybd69f732013-02-10 22:57:21 +00001413}
Grant Likely15c9a0a2011-12-12 09:25:57 -07001414EXPORT_SYMBOL(of_parse_phandle_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -07001415
Grant Likelybd69f732013-02-10 22:57:21 +00001416/**
Stephen Boydbd6f2fd2018-01-30 18:36:16 -08001417 * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it
1418 * @np: pointer to a device tree node containing a list
1419 * @list_name: property name that contains a list
1420 * @stem_name: stem of property names that specify phandles' arguments count
1421 * @index: index of a phandle to parse out
1422 * @out_args: optional pointer to output arguments structure (will be filled)
1423 *
1424 * This function is useful to parse lists of phandles and their arguments.
1425 * Returns 0 on success and fills out_args, on error returns appropriate errno
1426 * value. The difference between this function and of_parse_phandle_with_args()
1427 * is that this API remaps a phandle if the node the phandle points to has
1428 * a <@stem_name>-map property.
1429 *
1430 * Caller is responsible to call of_node_put() on the returned out_args->np
1431 * pointer.
1432 *
1433 * Example:
1434 *
1435 * phandle1: node1 {
1436 * #list-cells = <2>;
1437 * }
1438 *
1439 * phandle2: node2 {
1440 * #list-cells = <1>;
1441 * }
1442 *
1443 * phandle3: node3 {
1444 * #list-cells = <1>;
1445 * list-map = <0 &phandle2 3>,
1446 * <1 &phandle2 2>,
1447 * <2 &phandle1 5 1>;
1448 * list-map-mask = <0x3>;
1449 * };
1450 *
1451 * node4 {
1452 * list = <&phandle1 1 2 &phandle3 0>;
1453 * }
1454 *
1455 * To get a device_node of the `node2' node you may call this:
1456 * of_parse_phandle_with_args(node4, "list", "list", 1, &args);
1457 */
1458int of_parse_phandle_with_args_map(const struct device_node *np,
1459 const char *list_name,
1460 const char *stem_name,
1461 int index, struct of_phandle_args *out_args)
1462{
1463 char *cells_name, *map_name = NULL, *mask_name = NULL;
1464 char *pass_name = NULL;
1465 struct device_node *cur, *new = NULL;
1466 const __be32 *map, *mask, *pass;
1467 static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 };
1468 static const __be32 dummy_pass[] = { [0 ... MAX_PHANDLE_ARGS] = 0 };
1469 __be32 initial_match_array[MAX_PHANDLE_ARGS];
1470 const __be32 *match_array = initial_match_array;
1471 int i, ret, map_len, match;
1472 u32 list_size, new_size;
1473
1474 if (index < 0)
1475 return -EINVAL;
1476
1477 cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name);
1478 if (!cells_name)
1479 return -ENOMEM;
1480
1481 ret = -ENOMEM;
1482 map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name);
1483 if (!map_name)
1484 goto free;
1485
1486 mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name);
1487 if (!mask_name)
1488 goto free;
1489
1490 pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name);
1491 if (!pass_name)
1492 goto free;
1493
1494 ret = __of_parse_phandle_with_args(np, list_name, cells_name, 0, index,
1495 out_args);
1496 if (ret)
1497 goto free;
1498
1499 /* Get the #<list>-cells property */
1500 cur = out_args->np;
1501 ret = of_property_read_u32(cur, cells_name, &list_size);
1502 if (ret < 0)
1503 goto put;
1504
1505 /* Precalculate the match array - this simplifies match loop */
1506 for (i = 0; i < list_size; i++)
1507 initial_match_array[i] = cpu_to_be32(out_args->args[i]);
1508
1509 ret = -EINVAL;
1510 while (cur) {
1511 /* Get the <list>-map property */
1512 map = of_get_property(cur, map_name, &map_len);
1513 if (!map) {
1514 ret = 0;
1515 goto free;
1516 }
1517 map_len /= sizeof(u32);
1518
1519 /* Get the <list>-map-mask property (optional) */
1520 mask = of_get_property(cur, mask_name, NULL);
1521 if (!mask)
1522 mask = dummy_mask;
1523 /* Iterate through <list>-map property */
1524 match = 0;
1525 while (map_len > (list_size + 1) && !match) {
1526 /* Compare specifiers */
1527 match = 1;
1528 for (i = 0; i < list_size; i++, map_len--)
1529 match &= !((match_array[i] ^ *map++) & mask[i]);
1530
1531 of_node_put(new);
1532 new = of_find_node_by_phandle(be32_to_cpup(map));
1533 map++;
1534 map_len--;
1535
1536 /* Check if not found */
1537 if (!new)
1538 goto put;
1539
1540 if (!of_device_is_available(new))
1541 match = 0;
1542
1543 ret = of_property_read_u32(new, cells_name, &new_size);
1544 if (ret)
1545 goto put;
1546
1547 /* Check for malformed properties */
1548 if (WARN_ON(new_size > MAX_PHANDLE_ARGS))
1549 goto put;
1550 if (map_len < new_size)
1551 goto put;
1552
1553 /* Move forward by new node's #<list>-cells amount */
1554 map += new_size;
1555 map_len -= new_size;
1556 }
1557 if (!match)
1558 goto put;
1559
1560 /* Get the <list>-map-pass-thru property (optional) */
1561 pass = of_get_property(cur, pass_name, NULL);
1562 if (!pass)
1563 pass = dummy_pass;
1564
1565 /*
1566 * Successfully parsed a <list>-map translation; copy new
1567 * specifier into the out_args structure, keeping the
1568 * bits specified in <list>-map-pass-thru.
1569 */
1570 match_array = map - new_size;
1571 for (i = 0; i < new_size; i++) {
1572 __be32 val = *(map - new_size + i);
1573
1574 if (i < list_size) {
1575 val &= ~pass[i];
1576 val |= cpu_to_be32(out_args->args[i]) & pass[i];
1577 }
1578
1579 out_args->args[i] = be32_to_cpu(val);
1580 }
1581 out_args->args_count = list_size = new_size;
1582 /* Iterate again with new provider */
1583 out_args->np = new;
1584 of_node_put(cur);
1585 cur = new;
1586 }
1587put:
1588 of_node_put(cur);
1589 of_node_put(new);
1590free:
1591 kfree(mask_name);
1592 kfree(map_name);
1593 kfree(cells_name);
1594 kfree(pass_name);
1595
1596 return ret;
1597}
1598EXPORT_SYMBOL(of_parse_phandle_with_args_map);
1599
1600/**
Stephen Warren035fd942013-08-14 15:27:10 -06001601 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1602 * @np: pointer to a device tree node containing a list
1603 * @list_name: property name that contains a list
1604 * @cell_count: number of argument cells following the phandle
1605 * @index: index of a phandle to parse out
1606 * @out_args: optional pointer to output arguments structure (will be filled)
1607 *
1608 * This function is useful to parse lists of phandles and their arguments.
1609 * Returns 0 on success and fills out_args, on error returns appropriate
1610 * errno value.
1611 *
Geert Uytterhoevend94a75c2014-10-22 11:44:52 +02001612 * Caller is responsible to call of_node_put() on the returned out_args->np
Stephen Warren035fd942013-08-14 15:27:10 -06001613 * pointer.
1614 *
1615 * Example:
1616 *
1617 * phandle1: node1 {
1618 * }
1619 *
1620 * phandle2: node2 {
1621 * }
1622 *
1623 * node3 {
Geert Uytterhoevenc0e848d2014-10-22 11:44:55 +02001624 * list = <&phandle1 0 2 &phandle2 2 3>;
Stephen Warren035fd942013-08-14 15:27:10 -06001625 * }
1626 *
1627 * To get a device_node of the `node2' node you may call this:
1628 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1629 */
1630int of_parse_phandle_with_fixed_args(const struct device_node *np,
1631 const char *list_name, int cell_count,
1632 int index, struct of_phandle_args *out_args)
1633{
1634 if (index < 0)
1635 return -EINVAL;
1636 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1637 index, out_args);
1638}
1639EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1640
1641/**
Grant Likelybd69f732013-02-10 22:57:21 +00001642 * of_count_phandle_with_args() - Find the number of phandles references in a property
1643 * @np: pointer to a device tree node containing a list
1644 * @list_name: property name that contains a list
1645 * @cells_name: property name that specifies phandles' arguments count
1646 *
1647 * Returns the number of phandle + argument tuples within a property. It
1648 * is a typical pattern to encode a list of phandle and variable
1649 * arguments into a single property. The number of arguments is encoded
1650 * by a property in the phandle-target node. For example, a gpios
1651 * property would contain a list of GPIO specifies consisting of a
1652 * phandle and 1 or more arguments. The number of arguments are
1653 * determined by the #gpio-cells property in the node pointed to by the
1654 * phandle.
1655 */
1656int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1657 const char *cells_name)
1658{
Joerg Roedel2021bd02016-04-04 17:49:19 +02001659 struct of_phandle_iterator it;
1660 int rc, cur_index = 0;
1661
1662 rc = of_phandle_iterator_init(&it, np, list_name, cells_name, 0);
1663 if (rc)
1664 return rc;
1665
1666 while ((rc = of_phandle_iterator_next(&it)) == 0)
1667 cur_index += 1;
1668
1669 if (rc != -ENOENT)
1670 return rc;
1671
1672 return cur_index;
Grant Likelybd69f732013-02-10 22:57:21 +00001673}
1674EXPORT_SYMBOL(of_count_phandle_with_args);
1675
Grant Likely02af11b2009-11-23 20:16:45 -07001676/**
Xiubo Li62664f62014-01-22 13:57:39 +08001677 * __of_add_property - Add a property to a node without lock operations
1678 */
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001679int __of_add_property(struct device_node *np, struct property *prop)
Xiubo Li62664f62014-01-22 13:57:39 +08001680{
1681 struct property **next;
1682
1683 prop->next = NULL;
1684 next = &np->properties;
1685 while (*next) {
1686 if (strcmp(prop->name, (*next)->name) == 0)
1687 /* duplicate ! don't insert it */
1688 return -EEXIST;
1689
1690 next = &(*next)->next;
1691 }
1692 *next = prop;
1693
1694 return 0;
1695}
1696
1697/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001698 * of_add_property - Add a property to a node
Grant Likely02af11b2009-11-23 20:16:45 -07001699 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001700int of_add_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001701{
Grant Likely02af11b2009-11-23 20:16:45 -07001702 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001703 int rc;
1704
Grant Likely8a2b22a2014-07-23 17:05:06 -06001705 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001706
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001707 raw_spin_lock_irqsave(&devtree_lock, flags);
Xiubo Li62664f62014-01-22 13:57:39 +08001708 rc = __of_add_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001709 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001710
Grant Likely8a2b22a2014-07-23 17:05:06 -06001711 if (!rc)
Pantelis Antoniou0829f6d2013-12-13 20:08:59 +02001712 __of_add_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001713
Grant Likely8a2b22a2014-07-23 17:05:06 -06001714 mutex_unlock(&of_mutex);
1715
Grant Likely259092a2014-07-16 12:48:23 -06001716 if (!rc)
1717 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1718
Xiubo Li62664f62014-01-22 13:57:39 +08001719 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001720}
1721
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001722int __of_remove_property(struct device_node *np, struct property *prop)
1723{
1724 struct property **next;
1725
1726 for (next = &np->properties; *next; next = &(*next)->next) {
1727 if (*next == prop)
1728 break;
1729 }
1730 if (*next == NULL)
1731 return -ENODEV;
1732
1733 /* found the node */
1734 *next = prop->next;
1735 prop->next = np->deadprops;
1736 np->deadprops = prop;
1737
1738 return 0;
1739}
1740
Grant Likely02af11b2009-11-23 20:16:45 -07001741/**
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001742 * of_remove_property - Remove a property from a node.
Grant Likely02af11b2009-11-23 20:16:45 -07001743 *
1744 * Note that we don't actually remove it, since we have given out
1745 * who-knows-how-many pointers to the data using get-property.
1746 * Instead we just move the property to the "dead properties"
1747 * list, so it won't be found any more.
1748 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001749int of_remove_property(struct device_node *np, struct property *prop)
Grant Likely02af11b2009-11-23 20:16:45 -07001750{
Grant Likely02af11b2009-11-23 20:16:45 -07001751 unsigned long flags;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001752 int rc;
1753
Suraj Jitindar Singh201b3fe2016-04-28 15:34:54 +10001754 if (!prop)
1755 return -ENODEV;
1756
Grant Likely8a2b22a2014-07-23 17:05:06 -06001757 mutex_lock(&of_mutex);
Grant Likely02af11b2009-11-23 20:16:45 -07001758
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001759 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001760 rc = __of_remove_property(np, prop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001761 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Grant Likely02af11b2009-11-23 20:16:45 -07001762
Grant Likely8a2b22a2014-07-23 17:05:06 -06001763 if (!rc)
1764 __of_remove_property_sysfs(np, prop);
Grant Likely02af11b2009-11-23 20:16:45 -07001765
Grant Likely8a2b22a2014-07-23 17:05:06 -06001766 mutex_unlock(&of_mutex);
Grant Likely75b57ec2014-02-20 18:02:11 +00001767
Grant Likely259092a2014-07-16 12:48:23 -06001768 if (!rc)
1769 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1770
Grant Likely8a2b22a2014-07-23 17:05:06 -06001771 return rc;
Grant Likely02af11b2009-11-23 20:16:45 -07001772}
1773
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001774int __of_update_property(struct device_node *np, struct property *newprop,
1775 struct property **oldpropp)
1776{
1777 struct property **next, *oldprop;
1778
1779 for (next = &np->properties; *next; next = &(*next)->next) {
1780 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1781 break;
1782 }
1783 *oldpropp = oldprop = *next;
1784
1785 if (oldprop) {
1786 /* replace the node */
1787 newprop->next = oldprop->next;
1788 *next = newprop;
1789 oldprop->next = np->deadprops;
1790 np->deadprops = oldprop;
1791 } else {
1792 /* new node */
1793 newprop->next = NULL;
1794 *next = newprop;
1795 }
Grant Likely02af11b2009-11-23 20:16:45 -07001796
1797 return 0;
1798}
1799
1800/*
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001801 * of_update_property - Update a property in a node, if the property does
Dong Aisheng475d0092012-07-11 15:16:37 +10001802 * not exist, add it.
Grant Likely02af11b2009-11-23 20:16:45 -07001803 *
1804 * Note that we don't actually remove it, since we have given out
1805 * who-knows-how-many pointers to the data using get-property.
1806 * Instead we just move the property to the "dead properties" list,
1807 * and add the new property to the property list
1808 */
Nathan Fontenot79d1c712012-10-02 16:58:46 +00001809int of_update_property(struct device_node *np, struct property *newprop)
Grant Likely02af11b2009-11-23 20:16:45 -07001810{
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001811 struct property *oldprop;
Grant Likely02af11b2009-11-23 20:16:45 -07001812 unsigned long flags;
Xiubo Li947fdaad02014-04-17 15:48:29 +08001813 int rc;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001814
Dong Aisheng475d0092012-07-11 15:16:37 +10001815 if (!newprop->name)
1816 return -EINVAL;
1817
Grant Likely8a2b22a2014-07-23 17:05:06 -06001818 mutex_lock(&of_mutex);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001819
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001820 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +03001821 rc = __of_update_property(np, newprop, &oldprop);
Thomas Gleixnerd6d3c4e2013-02-06 15:30:56 -05001822 raw_spin_unlock_irqrestore(&devtree_lock, flags);
Nathan Fontenote81b3292012-10-02 16:55:01 +00001823
Grant Likely8a2b22a2014-07-23 17:05:06 -06001824 if (!rc)
1825 __of_update_property_sysfs(np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001826
Grant Likely8a2b22a2014-07-23 17:05:06 -06001827 mutex_unlock(&of_mutex);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001828
Grant Likely259092a2014-07-16 12:48:23 -06001829 if (!rc)
1830 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001831
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001832 return rc;
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001833}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -07001834
Shawn Guo611cad72011-08-15 15:28:14 +08001835static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1836 int id, const char *stem, int stem_len)
1837{
1838 ap->np = np;
1839 ap->id = id;
1840 strncpy(ap->stem, stem, stem_len);
1841 ap->stem[stem_len] = 0;
1842 list_add_tail(&ap->link, &aliases_lookup);
Rob Herring0d638a02017-06-01 15:50:55 -05001843 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1844 ap->alias, ap->stem, ap->id, np);
Shawn Guo611cad72011-08-15 15:28:14 +08001845}
1846
1847/**
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001848 * of_alias_scan - Scan all properties of the 'aliases' node
Shawn Guo611cad72011-08-15 15:28:14 +08001849 *
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001850 * The function scans all the properties of the 'aliases' node and populates
1851 * the global lookup table with the properties. It returns the
1852 * number of alias properties found, or an error code in case of failure.
Shawn Guo611cad72011-08-15 15:28:14 +08001853 *
1854 * @dt_alloc: An allocator that provides a virtual address to memory
Geert Uytterhoeven1821dda2014-10-22 11:44:53 +02001855 * for storing the resulting tree
Shawn Guo611cad72011-08-15 15:28:14 +08001856 */
1857void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1858{
1859 struct property *pp;
1860
Laurentiu Tudor7dbe5842014-08-27 17:09:39 +03001861 of_aliases = of_find_node_by_path("/aliases");
Shawn Guo611cad72011-08-15 15:28:14 +08001862 of_chosen = of_find_node_by_path("/chosen");
1863 if (of_chosen == NULL)
1864 of_chosen = of_find_node_by_path("/chosen@0");
Sascha Hauer5c19e952013-08-05 14:40:44 +02001865
1866 if (of_chosen) {
Grant Likelya752ee52014-03-28 08:12:18 -07001867 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
Sergei Shtylyovb0d9d922017-07-23 19:55:48 +03001868 const char *name = NULL;
1869
1870 if (of_property_read_string(of_chosen, "stdout-path", &name))
1871 of_property_read_string(of_chosen, "linux,stdout-path",
1872 &name);
Grant Likelya752ee52014-03-28 08:12:18 -07001873 if (IS_ENABLED(CONFIG_PPC) && !name)
Sergei Shtylyovb0d9d922017-07-23 19:55:48 +03001874 of_property_read_string(of_aliases, "stdout", &name);
Peter Hurleyf64255b2015-03-17 16:46:33 -04001875 if (name)
Leif Lindholm7914a7c2014-11-27 17:56:07 +00001876 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001877 }
1878
Shawn Guo611cad72011-08-15 15:28:14 +08001879 if (!of_aliases)
1880 return;
1881
Dong Aisheng8af0da92011-12-22 20:19:24 +08001882 for_each_property_of_node(of_aliases, pp) {
Shawn Guo611cad72011-08-15 15:28:14 +08001883 const char *start = pp->name;
1884 const char *end = start + strlen(start);
1885 struct device_node *np;
1886 struct alias_prop *ap;
1887 int id, len;
1888
1889 /* Skip those we do not want to proceed */
1890 if (!strcmp(pp->name, "name") ||
1891 !strcmp(pp->name, "phandle") ||
1892 !strcmp(pp->name, "linux,phandle"))
1893 continue;
1894
1895 np = of_find_node_by_path(pp->value);
1896 if (!np)
1897 continue;
1898
1899 /* walk the alias backwards to extract the id and work out
1900 * the 'stem' string */
1901 while (isdigit(*(end-1)) && end > start)
1902 end--;
1903 len = end - start;
1904
1905 if (kstrtoint(end, 10, &id) < 0)
1906 continue;
1907
1908 /* Allocate an alias_prop with enough space for the stem */
Paul Burtonde96ec22016-09-23 16:38:27 +01001909 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
Shawn Guo611cad72011-08-15 15:28:14 +08001910 if (!ap)
1911 continue;
Grant Likely0640332e2013-08-28 21:24:17 +01001912 memset(ap, 0, sizeof(*ap) + len + 1);
Shawn Guo611cad72011-08-15 15:28:14 +08001913 ap->alias = start;
1914 of_alias_add(ap, np, id, start, len);
1915 }
1916}
1917
1918/**
1919 * of_alias_get_id - Get alias id for the given device_node
1920 * @np: Pointer to the given device_node
1921 * @stem: Alias stem of the given device_node
1922 *
Geert Uytterhoeven5a53a072014-03-11 11:23:38 +01001923 * The function travels the lookup table to get the alias id for the given
1924 * device_node and alias stem. It returns the alias id if found.
Shawn Guo611cad72011-08-15 15:28:14 +08001925 */
1926int of_alias_get_id(struct device_node *np, const char *stem)
1927{
1928 struct alias_prop *app;
1929 int id = -ENODEV;
1930
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001931 mutex_lock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001932 list_for_each_entry(app, &aliases_lookup, link) {
1933 if (strcmp(app->stem, stem) != 0)
1934 continue;
1935
1936 if (np == app->np) {
1937 id = app->id;
1938 break;
1939 }
1940 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +03001941 mutex_unlock(&of_mutex);
Shawn Guo611cad72011-08-15 15:28:14 +08001942
1943 return id;
1944}
1945EXPORT_SYMBOL_GPL(of_alias_get_id);
Stephen Warrenc541adc2012-04-04 09:27:46 -06001946
Wolfram Sang351d2242015-03-12 17:17:58 +01001947/**
1948 * of_alias_get_highest_id - Get highest alias id for the given stem
1949 * @stem: Alias stem to be examined
1950 *
1951 * The function travels the lookup table to get the highest alias id for the
1952 * given alias stem. It returns the alias id if found.
1953 */
1954int of_alias_get_highest_id(const char *stem)
1955{
1956 struct alias_prop *app;
1957 int id = -ENODEV;
1958
1959 mutex_lock(&of_mutex);
1960 list_for_each_entry(app, &aliases_lookup, link) {
1961 if (strcmp(app->stem, stem) != 0)
1962 continue;
1963
1964 if (app->id > id)
1965 id = app->id;
1966 }
1967 mutex_unlock(&of_mutex);
1968
1969 return id;
1970}
1971EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
1972
Sascha Hauer5c19e952013-08-05 14:40:44 +02001973/**
Grant Likely3482f2c2014-03-27 17:18:55 -07001974 * of_console_check() - Test and setup console for DT setup
1975 * @dn - Pointer to device node
1976 * @name - Name to use for preferred console without index. ex. "ttyS"
1977 * @index - Index to use for preferred console.
Sascha Hauer5c19e952013-08-05 14:40:44 +02001978 *
Grant Likely3482f2c2014-03-27 17:18:55 -07001979 * Check if the given device node matches the stdout-path property in the
1980 * /chosen node. If it does then register it as the preferred console and return
1981 * TRUE. Otherwise return FALSE.
Sascha Hauer5c19e952013-08-05 14:40:44 +02001982 */
Grant Likely3482f2c2014-03-27 17:18:55 -07001983bool of_console_check(struct device_node *dn, char *name, int index)
Sascha Hauer5c19e952013-08-05 14:40:44 +02001984{
Grant Likely3482f2c2014-03-27 17:18:55 -07001985 if (!dn || dn != of_stdout || console_set_on_cmdline)
Sascha Hauer5c19e952013-08-05 14:40:44 +02001986 return false;
Sergey Senozhatskydb179e02017-09-26 15:25:10 +09001987
1988 /*
1989 * XXX: cast `options' to char pointer to suppress complication
1990 * warnings: printk, UART and console drivers expect char pointer.
1991 */
1992 return !add_preferred_console(name, index, (char *)of_stdout_options);
Sascha Hauer5c19e952013-08-05 14:40:44 +02001993}
Grant Likely3482f2c2014-03-27 17:18:55 -07001994EXPORT_SYMBOL_GPL(of_console_check);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01001995
1996/**
1997 * of_find_next_cache_node - Find a node's subsidiary cache
1998 * @np: node of type "cpu" or "cache"
1999 *
2000 * Returns a node pointer with refcount incremented, use
2001 * of_node_put() on it when done. Caller should hold a reference
2002 * to np.
2003 */
2004struct device_node *of_find_next_cache_node(const struct device_node *np)
2005{
Rob Herring91d96742017-05-04 12:30:07 -05002006 struct device_node *child, *cache_node;
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002007
Rob Herring91d96742017-05-04 12:30:07 -05002008 cache_node = of_parse_phandle(np, "l2-cache", 0);
2009 if (!cache_node)
2010 cache_node = of_parse_phandle(np, "next-level-cache", 0);
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002011
Rob Herring91d96742017-05-04 12:30:07 -05002012 if (cache_node)
2013 return cache_node;
Sudeep KarkadaNageshaa3e31b42013-09-18 11:53:05 +01002014
2015 /* OF on pmac has nodes instead of properties named "l2-cache"
2016 * beneath CPU nodes.
2017 */
2018 if (!strcmp(np->type, "cpu"))
2019 for_each_child_of_node(np, child)
2020 if (!strcmp(child->type, "cache"))
2021 return child;
2022
2023 return NULL;
2024}
Philipp Zabelfd9fdb72014-02-10 22:01:48 +01002025
2026/**
Sudeep Holla5fa23532017-01-16 10:40:43 +00002027 * of_find_last_cache_level - Find the level at which the last cache is
2028 * present for the given logical cpu
2029 *
2030 * @cpu: cpu number(logical index) for which the last cache level is needed
2031 *
2032 * Returns the the level at which the last cache is present. It is exactly
2033 * same as the total number of cache levels for the given logical cpu.
2034 */
2035int of_find_last_cache_level(unsigned int cpu)
2036{
2037 u32 cache_level = 0;
2038 struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
2039
2040 while (np) {
2041 prev = np;
2042 of_node_put(np);
2043 np = of_find_next_cache_node(np);
2044 }
2045
2046 of_property_read_u32(prev, "cache-level", &cache_level);
2047
2048 return cache_level;
2049}