]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/of/base.c
of: base: Fix english spelling in of_alias_get_alias_list()
[mirror_ubuntu-hirsute-kernel.git] / drivers / of / base.c
CommitLineData
af6074fc 1// SPDX-License-Identifier: GPL-2.0+
97e873e5
SR
2/*
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 *
e91edcf5
GL
13 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
14 * Grant Likely.
97e873e5 15 */
606ad42a
RH
16
17#define pr_fmt(fmt) "OF: " fmt
18
b1078c35 19#include <linux/bitmap.h>
3482f2c5 20#include <linux/console.h>
611cad72 21#include <linux/ctype.h>
183912d3 22#include <linux/cpu.h>
97e873e5
SR
23#include <linux/module.h>
24#include <linux/of.h>
5fa23530 25#include <linux/of_device.h>
fd9fdb78 26#include <linux/of_graph.h>
581b605a 27#include <linux/spinlock.h>
5a0e3ad6 28#include <linux/slab.h>
75b57ecf 29#include <linux/string.h>
a9f2f63a 30#include <linux/proc_fs.h>
581b605a 31
ced4eec9 32#include "of_private.h"
611cad72 33
ced4eec9 34LIST_HEAD(aliases_lookup);
611cad72 35
5063e25a
GL
36struct device_node *of_root;
37EXPORT_SYMBOL(of_root);
fc0bdae4 38struct device_node *of_chosen;
611cad72 39struct device_node *of_aliases;
a752ee56 40struct device_node *of_stdout;
7914a7c5 41static const char *of_stdout_options;
611cad72 42
8a2b22a2 43struct kset *of_kset;
75b57ecf
GL
44
45/*
8a2b22a2
GL
46 * 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.
75b57ecf 50 */
c05aba2b 51DEFINE_MUTEX(of_mutex);
1ef4d424 52
5063e25a 53/* use when traversing tree through the child, sibling,
581b605a
SR
54 * or parent members of struct device_node.
55 */
d6d3c4e6 56DEFINE_RAW_SPINLOCK(devtree_lock);
97e873e5 57
f42b0e18
RH
58bool 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
97e873e5
SR
80int of_n_addr_cells(struct device_node *np)
81{
8832963d 82 u32 cells;
97e873e5
SR
83
84 do {
85 if (np->parent)
86 np = np->parent;
8832963d
SS
87 if (!of_property_read_u32(np, "#address-cells", &cells))
88 return cells;
97e873e5
SR
89 } 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{
8832963d 97 u32 cells;
97e873e5
SR
98
99 do {
100 if (np->parent)
101 np = np->parent;
8832963d
SS
102 if (!of_property_read_u32(np, "#size-cells", &cells))
103 return cells;
97e873e5
SR
104 } 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
0c3f061c
RH
110#ifdef CONFIG_NUMA
111int __weak of_node_to_nid(struct device_node *np)
112{
c8fff7bc 113 return NUMA_NO_NODE;
0c3f061c
RH
114}
115#endif
116
0b3ce78e
FR
117static 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 */
b9952b52 128void of_populate_phandle_cache(void)
0b3ce78e
FR
129{
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
e54192b4
RH
144 if (!phandles)
145 goto out;
146
0b3ce78e
FR
147 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
b9952b52 163int of_free_phandle_cache(void)
0b3ce78e
FR
164{
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}
b9952b52 176#if !defined(CONFIG_MODULES)
0b3ce78e
FR
177late_initcall_sync(of_free_phandle_cache);
178#endif
179
194ec936 180void __init of_core_init(void)
75b57ecf
GL
181{
182 struct device_node *np;
183
0b3ce78e
FR
184 of_populate_phandle_cache();
185
75b57ecf 186 /* Create the kset, and register existing nodes */
c05aba2b 187 mutex_lock(&of_mutex);
75b57ecf
GL
188 of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
189 if (!of_kset) {
c05aba2b 190 mutex_unlock(&of_mutex);
606ad42a 191 pr_err("failed to register existing nodes\n");
194ec936 192 return;
75b57ecf
GL
193 }
194 for_each_of_allnodes(np)
8a2b22a2 195 __of_attach_node_sysfs(np);
c05aba2b 196 mutex_unlock(&of_mutex);
75b57ecf 197
8357041a 198 /* Symlink in /proc as required by userspace ABI */
5063e25a 199 if (of_root)
75b57ecf 200 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
75b57ecf 201}
75b57ecf 202
28d0e36b
TG
203static struct property *__of_find_property(const struct device_node *np,
204 const char *name, int *lenp)
581b605a
SR
205{
206 struct property *pp;
207
64e4566f
TT
208 if (!np)
209 return NULL;
210
a3a7cab1 211 for (pp = np->properties; pp; pp = pp->next) {
581b605a 212 if (of_prop_cmp(pp->name, name) == 0) {
a3a7cab1 213 if (lenp)
581b605a
SR
214 *lenp = pp->length;
215 break;
216 }
217 }
28d0e36b
TG
218
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;
d6d3c4e6 227 unsigned long flags;
28d0e36b 228
d6d3c4e6 229 raw_spin_lock_irqsave(&devtree_lock, flags);
28d0e36b 230 pp = __of_find_property(np, name, lenp);
d6d3c4e6 231 raw_spin_unlock_irqrestore(&devtree_lock, flags);
581b605a
SR
232
233 return pp;
234}
235EXPORT_SYMBOL(of_find_property);
236
5063e25a
GL
237struct 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
e91edcf5
GL
254/**
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;
d25d8694 265 unsigned long flags;
e91edcf5 266
d25d8694 267 raw_spin_lock_irqsave(&devtree_lock, flags);
5063e25a
GL
268 np = __of_find_all_nodes(prev);
269 of_node_get(np);
e91edcf5 270 of_node_put(prev);
d25d8694 271 raw_spin_unlock_irqrestore(&devtree_lock, flags);
e91edcf5
GL
272 return np;
273}
274EXPORT_SYMBOL(of_find_all_nodes);
275
28d0e36b
TG
276/*
277 * Find a property with a given name for a given node
278 * and return the value.
279 */
a25095d4
GL
280const void *__of_get_property(const struct device_node *np,
281 const char *name, int *lenp)
28d0e36b
TG
282{
283 struct property *pp = __of_find_property(np, name, lenp);
284
285 return pp ? pp->value : NULL;
286}
287
97e873e5
SR
288/*
289 * Find a property with a given name for a given node
290 * and return the value.
291 */
292const void *of_get_property(const struct device_node *np, const char *name,
28d0e36b 293 int *lenp)
97e873e5
SR
294{
295 struct property *pp = of_find_property(np, name, lenp);
296
297 return pp ? pp->value : NULL;
298}
299EXPORT_SYMBOL(of_get_property);
0081cbc3 300
183912d3
SH
301/*
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);
f3cea45a 334 if (!cell || !ac)
183912d3 335 return false;
f3cea45a 336 prop_len /= sizeof(*cell) * ac;
183912d3
SH
337 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
d1cb9d1a
DM
349/*
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
510bd068 368 return __of_find_n_match_cpu_property(cpun, "reg", cpu, thread);
d1cb9d1a
DM
369}
370
183912d3
SH
371/**
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 *
1c986e36
MY
387 * 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.
183912d3
SH
389 */
390struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
391{
d1cb9d1a 392 struct device_node *cpun;
183912d3 393
d1cb9d1a
DM
394 for_each_node_by_type(cpun, "cpu") {
395 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
183912d3
SH
396 return cpun;
397 }
398 return NULL;
399}
400EXPORT_SYMBOL(of_get_cpu_node);
401
a0e71cd9
SP
402/**
403 * 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
215a14cf
KH
428/**
429 * __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
0081cbc3 457 */
28d0e36b 458static int __of_device_is_compatible(const struct device_node *device,
215a14cf
KH
459 const char *compat, const char *type, const char *name)
460{
461 struct property *prop;
462 const char *cp;
463 int index = 0, score = 0;
464
465 /* 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;
477 }
0081cbc3 478
215a14cf
KH
479 /* 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;
0081cbc3
SR
484 }
485
215a14cf
KH
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;
0081cbc3 494}
28d0e36b
TG
495
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{
d6d3c4e6 502 unsigned long flags;
28d0e36b
TG
503 int res;
504
d6d3c4e6 505 raw_spin_lock_irqsave(&devtree_lock, flags);
215a14cf 506 res = __of_device_is_compatible(device, compat, NULL, NULL);
d6d3c4e6 507 raw_spin_unlock_irqrestore(&devtree_lock, flags);
28d0e36b
TG
508 return res;
509}
0081cbc3 510EXPORT_SYMBOL(of_device_is_compatible);
e679c5f4 511
b9c13fe3
BH
512/** 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
1f43cfb9 534/**
71a157e8 535 * of_machine_is_compatible - Test root of device tree for a given compatible value
1f43cfb9
GL
536 * @compat: compatible string to look for in root node's compatible property.
537 *
25c7a1de 538 * Returns a positive integer if the root node has the given value in its
1f43cfb9
GL
539 * compatible property.
540 */
71a157e8 541int of_machine_is_compatible(const char *compat)
1f43cfb9
GL
542{
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}
71a157e8 553EXPORT_SYMBOL(of_machine_is_compatible);
1f43cfb9 554
834d97d4 555/**
c31a0c05 556 * __of_device_is_available - check if a device is available for use
834d97d4 557 *
c31a0c05 558 * @device: Node to check for availability, with locks already held
834d97d4 559 *
53a4ab96
KC
560 * Returns true if the status property is absent or set to "okay" or "ok",
561 * false otherwise
834d97d4 562 */
53a4ab96 563static bool __of_device_is_available(const struct device_node *device)
834d97d4
JB
564{
565 const char *status;
566 int statlen;
567
42ccd781 568 if (!device)
53a4ab96 569 return false;
42ccd781 570
c31a0c05 571 status = __of_get_property(device, "status", &statlen);
834d97d4 572 if (status == NULL)
53a4ab96 573 return true;
834d97d4
JB
574
575 if (statlen > 0) {
576 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
53a4ab96 577 return true;
834d97d4
JB
578 }
579
53a4ab96 580 return false;
834d97d4 581}
c31a0c05
SW
582
583/**
584 * of_device_is_available - check if a device is available for use
585 *
586 * @device: Node to check for availability
587 *
53a4ab96
KC
588 * Returns true if the status property is absent or set to "okay" or "ok",
589 * false otherwise
c31a0c05 590 */
53a4ab96 591bool of_device_is_available(const struct device_node *device)
c31a0c05
SW
592{
593 unsigned long flags;
53a4ab96 594 bool res;
c31a0c05
SW
595
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}
834d97d4
JB
602EXPORT_SYMBOL(of_device_is_available);
603
37786c7f
KC
604/**
605 * 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
e679c5f4
SR
627/**
628 * 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;
d6d3c4e6 637 unsigned long flags;
e679c5f4
SR
638
639 if (!node)
640 return NULL;
641
d6d3c4e6 642 raw_spin_lock_irqsave(&devtree_lock, flags);
e679c5f4 643 np = of_node_get(node->parent);
d6d3c4e6 644 raw_spin_unlock_irqrestore(&devtree_lock, flags);
e679c5f4
SR
645 return np;
646}
647EXPORT_SYMBOL(of_get_parent);
d1cd355a 648
f4eb0107
ME
649/**
650 * of_get_next_parent - Iterate to a node's parent
651 * @node: Node to get parent of
652 *
c0e848d8
GU
653 * 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.
f4eb0107
ME
656 *
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;
d6d3c4e6 663 unsigned long flags;
f4eb0107
ME
664
665 if (!node)
666 return NULL;
667
d6d3c4e6 668 raw_spin_lock_irqsave(&devtree_lock, flags);
f4eb0107
ME
669 parent = of_node_get(node->parent);
670 of_node_put(node);
d6d3c4e6 671 raw_spin_unlock_irqrestore(&devtree_lock, flags);
f4eb0107
ME
672 return parent;
673}
6695be68 674EXPORT_SYMBOL(of_get_next_parent);
f4eb0107 675
0d0e02d6
GL
676static struct device_node *__of_get_next_child(const struct device_node *node,
677 struct device_node *prev)
678{
679 struct device_node *next;
680
43cb4367
FF
681 if (!node)
682 return NULL;
683
0d0e02d6
GL
684 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
d1cd355a
SR
695/**
696 * 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 *
64808273
BS
700 * 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.
d1cd355a
SR
703 */
704struct device_node *of_get_next_child(const struct device_node *node,
705 struct device_node *prev)
706{
707 struct device_node *next;
d6d3c4e6 708 unsigned long flags;
d1cd355a 709
d6d3c4e6 710 raw_spin_lock_irqsave(&devtree_lock, flags);
0d0e02d6 711 next = __of_get_next_child(node, prev);
d6d3c4e6 712 raw_spin_unlock_irqrestore(&devtree_lock, flags);
d1cd355a
SR
713 return next;
714}
715EXPORT_SYMBOL(of_get_next_child);
1ef4d424 716
3296193d
TT
717/**
718 * 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;
d25d8694 729 unsigned long flags;
3296193d 730
43cb4367
FF
731 if (!node)
732 return NULL;
733
d25d8694 734 raw_spin_lock_irqsave(&devtree_lock, flags);
3296193d
TT
735 next = prev ? prev->sibling : node->child;
736 for (; next; next = next->sibling) {
c31a0c05 737 if (!__of_device_is_available(next))
3296193d
TT
738 continue;
739 if (of_node_get(next))
740 break;
741 }
742 of_node_put(prev);
d25d8694 743 raw_spin_unlock_irqrestore(&devtree_lock, flags);
3296193d
TT
744 return next;
745}
746EXPORT_SYMBOL(of_get_next_available_child);
747
36156f92
JH
748/**
749 * 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
9c19761a
SK
773/**
774 * 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
e0a58f3e 796struct device_node *__of_find_node_by_path(struct device_node *parent,
c22e650e
GL
797 const char *path)
798{
799 struct device_node *child;
106937e8 800 int len;
c22e650e 801
721a09e9 802 len = strcspn(path, "/:");
c22e650e
GL
803 if (!len)
804 return NULL;
805
806 __for_each_child_of_node(parent, child) {
95e6b1fa 807 const char *name = kbasename(child->full_name);
c22e650e
GL
808 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
809 return child;
810 }
811 return NULL;
812}
813
27497e11
RH
814struct 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
1ef4d424 832/**
75c28c09 833 * of_find_node_opts_by_path - Find a node matching a full OF path
c22e650e
GL
834 * @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.
75c28c09
LL
838 * @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.
c22e650e
GL
841 *
842 * Valid paths:
843 * /foo/bar Full path
844 * foo Valid alias
845 * foo/bar Valid alias + relative path
1ef4d424
SR
846 *
847 * Returns a node pointer with refcount incremented, use
848 * of_node_put() on it when done.
849 */
75c28c09 850struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
1ef4d424 851{
c22e650e
GL
852 struct device_node *np = NULL;
853 struct property *pp;
d6d3c4e6 854 unsigned long flags;
75c28c09
LL
855 const char *separator = strchr(path, ':');
856
857 if (opts)
858 *opts = separator ? separator + 1 : NULL;
1ef4d424 859
c22e650e 860 if (strcmp(path, "/") == 0)
5063e25a 861 return of_node_get(of_root);
c22e650e
GL
862
863 /* The path could begin with an alias */
864 if (*path != '/') {
106937e8
LL
865 int len;
866 const char *p = separator;
867
868 if (!p)
869 p = strchrnul(path, '/');
870 len = p - path;
c22e650e
GL
871
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 */
d6d3c4e6 888 raw_spin_lock_irqsave(&devtree_lock, flags);
c22e650e 889 if (!np)
5063e25a 890 np = of_node_get(of_root);
27497e11 891 np = __of_find_node_by_full_path(np, path);
d6d3c4e6 892 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1ef4d424
SR
893 return np;
894}
75c28c09 895EXPORT_SYMBOL(of_find_node_opts_by_path);
1ef4d424
SR
896
897/**
898 * of_find_node_by_name - Find a node by its "name" property
02a876b5 899 * @from: The node to start searching from or NULL; the node
1ef4d424 900 * you pass will not be searched, only the next one
02a876b5
SB
901 * will. Typically, you pass what the previous call
902 * returned. of_node_put() will be called on @from.
1ef4d424
SR
903 * @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;
d6d3c4e6 912 unsigned long flags;
1ef4d424 913
d6d3c4e6 914 raw_spin_lock_irqsave(&devtree_lock, flags);
5063e25a 915 for_each_of_allnodes_from(from, np)
1ef4d424
SR
916 if (np->name && (of_node_cmp(np->name, name) == 0)
917 && of_node_get(np))
918 break;
919 of_node_put(from);
d6d3c4e6 920 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1ef4d424
SR
921 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;
d6d3c4e6 941 unsigned long flags;
1ef4d424 942
d6d3c4e6 943 raw_spin_lock_irqsave(&devtree_lock, flags);
5063e25a 944 for_each_of_allnodes_from(from, np)
1ef4d424
SR
945 if (np->type && (of_node_cmp(np->type, type) == 0)
946 && of_node_get(np))
947 break;
948 of_node_put(from);
d6d3c4e6 949 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1ef4d424
SR
950 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;
d6d3c4e6 972 unsigned long flags;
1ef4d424 973
d6d3c4e6 974 raw_spin_lock_irqsave(&devtree_lock, flags);
5063e25a 975 for_each_of_allnodes_from(from, np)
215a14cf 976 if (__of_device_is_compatible(np, compatible, type, NULL) &&
28d0e36b 977 of_node_get(np))
1ef4d424 978 break;
1ef4d424 979 of_node_put(from);
d6d3c4e6 980 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1ef4d424
SR
981 return np;
982}
983EXPORT_SYMBOL(of_find_compatible_node);
283029d1 984
1e291b14
ME
985/**
986 * 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;
d6d3c4e6 1002 unsigned long flags;
1e291b14 1003
d6d3c4e6 1004 raw_spin_lock_irqsave(&devtree_lock, flags);
5063e25a 1005 for_each_of_allnodes_from(from, np) {
a3a7cab1 1006 for (pp = np->properties; pp; pp = pp->next) {
1e291b14
ME
1007 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);
d6d3c4e6 1015 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1e291b14
ME
1016 return np;
1017}
1018EXPORT_SYMBOL(of_find_node_with_property);
1019
28d0e36b
TG
1020static
1021const struct of_device_id *__of_match_node(const struct of_device_id *matches,
1022 const struct device_node *node)
283029d1 1023{
215a14cf
KH
1024 const struct of_device_id *best_match = NULL;
1025 int score, best_score = 0;
1026
a52f07ec
GL
1027 if (!matches)
1028 return NULL;
1029
215a14cf
KH
1030 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 }
4e8ca6ee 1037 }
215a14cf
KH
1038
1039 return best_match;
283029d1 1040}
28d0e36b
TG
1041
1042/**
c50949d3 1043 * of_match_node - Tell if a device_node has a matching of_match structure
28d0e36b
TG
1044 * @matches: array of of device match structures to search in
1045 * @node: the of device structure to match against
1046 *
71c5498e 1047 * Low level utility function used by device matching.
28d0e36b
TG
1048 */
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;
d6d3c4e6 1053 unsigned long flags;
28d0e36b 1054
d6d3c4e6 1055 raw_spin_lock_irqsave(&devtree_lock, flags);
28d0e36b 1056 match = __of_match_node(matches, node);
d6d3c4e6 1057 raw_spin_unlock_irqrestore(&devtree_lock, flags);
28d0e36b
TG
1058 return match;
1059}
283029d1
GL
1060EXPORT_SYMBOL(of_match_node);
1061
1062/**
50c8af4c
SW
1063 * of_find_matching_node_and_match - Find a node based on an of_device_id
1064 * match table.
283029d1
GL
1065 * @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
50c8af4c 1070 * @match Updated to point at the matches entry which matched
283029d1
GL
1071 *
1072 * Returns a node pointer with refcount incremented, use
1073 * of_node_put() on it when done.
1074 */
50c8af4c
SW
1075struct 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)
283029d1
GL
1078{
1079 struct device_node *np;
dc71bcf1 1080 const struct of_device_id *m;
d6d3c4e6 1081 unsigned long flags;
283029d1 1082
50c8af4c
SW
1083 if (match)
1084 *match = NULL;
1085
d6d3c4e6 1086 raw_spin_lock_irqsave(&devtree_lock, flags);
5063e25a 1087 for_each_of_allnodes_from(from, np) {
28d0e36b 1088 m = __of_match_node(matches, np);
dc71bcf1 1089 if (m && of_node_get(np)) {
50c8af4c 1090 if (match)
dc71bcf1 1091 *match = m;
283029d1 1092 break;
50c8af4c 1093 }
283029d1
GL
1094 }
1095 of_node_put(from);
d6d3c4e6 1096 raw_spin_unlock_irqrestore(&devtree_lock, flags);
283029d1
GL
1097 return np;
1098}
80c2022e 1099EXPORT_SYMBOL(of_find_matching_node_and_match);
3f07af49 1100
3f07af49
GL
1101/**
1102 * 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 *
2ffe8c5f
GL
1107 * 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.
3f07af49 1111 *
2ffe8c5f 1112 * This routine returns 0 on success, <0 on failure.
3f07af49
GL
1113 */
1114int of_modalias_node(struct device_node *node, char *modalias, int len)
1115{
2ffe8c5f
GL
1116 const char *compatible, *p;
1117 int cplen;
3f07af49
GL
1118
1119 compatible = of_get_property(node, "compatible", &cplen);
2ffe8c5f 1120 if (!compatible || strlen(compatible) > cplen)
3f07af49 1121 return -ENODEV;
3f07af49 1122 p = strchr(compatible, ',');
2ffe8c5f 1123 strlcpy(modalias, p ? p + 1 : compatible, len);
3f07af49
GL
1124 return 0;
1125}
1126EXPORT_SYMBOL_GPL(of_modalias_node);
1127
89751a7c
JK
1128/**
1129 * 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{
0b3ce78e 1137 struct device_node *np = NULL;
d25d8694 1138 unsigned long flags;
0b3ce78e 1139 phandle masked_handle;
89751a7c 1140
fc59b447
GL
1141 if (!handle)
1142 return NULL;
1143
d25d8694 1144 raw_spin_lock_irqsave(&devtree_lock, flags);
0b3ce78e
FR
1145
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
89751a7c 1163 of_node_get(np);
d25d8694 1164 raw_spin_unlock_irqrestore(&devtree_lock, flags);
89751a7c
JK
1165 return np;
1166}
1167EXPORT_SYMBOL(of_find_node_by_phandle);
1168
624cfca5
GL
1169void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1170{
1171 int i;
0d638a07 1172 printk("%s %pOF", msg, args->np);
4aa66344
MN
1173 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");
624cfca5
GL
1179}
1180
74e1fbb1
JR
1181int 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)
64b60e09 1186{
74e1fbb1
JR
1187 const __be32 *list;
1188 int size;
1189
1190 memset(it, 0, sizeof(*it));
64b60e09
AV
1191
1192 list = of_get_property(np, list_name, &size);
15c9a0ac 1193 if (!list)
1af4c7f1 1194 return -ENOENT;
64b60e09 1195
74e1fbb1
JR
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}
00bab23f 1205EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
74e1fbb1 1206
cd209b41
JR
1207int 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) {
64b60e09 1225
15c9a0ac 1226 /*
cd209b41
JR
1227 * Find the provider node and parse the #*-cells property to
1228 * determine the argument length.
15c9a0ac 1229 */
cd209b41 1230 it->node = of_find_node_by_phandle(it->phandle);
035fd948 1231
cd209b41
JR
1232 if (it->cells_name) {
1233 if (!it->node) {
0d638a07
RH
1234 pr_err("%pOF: could not find phandle\n",
1235 it->parent);
cd209b41 1236 goto err;
15c9a0ac 1237 }
64b60e09 1238
cd209b41
JR
1239 if (of_property_read_u32(it->node, it->cells_name,
1240 &count)) {
0d638a07
RH
1241 pr_err("%pOF: could not get %s for %pOF\n",
1242 it->parent,
cd209b41 1243 it->cells_name,
0d638a07 1244 it->node);
23ce04c0 1245 goto err;
15c9a0ac 1246 }
cd209b41
JR
1247 } else {
1248 count = it->cell_count;
64b60e09
AV
1249 }
1250
15c9a0ac 1251 /*
cd209b41
JR
1252 * Make sure that the arguments actually fit in the remaining
1253 * property data length
1254 */
1255 if (it->cur + count > it->list_end) {
0d638a07
RH
1256 pr_err("%pOF: arguments longer than property\n",
1257 it->parent);
cd209b41
JR
1258 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}
00bab23f 1275EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
cd209b41 1276
abdaa77b
JR
1277int 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
bd69f73f
GL
1294static int __of_parse_phandle_with_args(const struct device_node *np,
1295 const char *list_name,
035fd948
SW
1296 const char *cells_name,
1297 int cell_count, int index,
bd69f73f 1298 struct of_phandle_args *out_args)
64b60e09 1299{
74e1fbb1
JR
1300 struct of_phandle_iterator it;
1301 int rc, cur_index = 0;
64b60e09 1302
15c9a0ac 1303 /* Loop over the phandles until all the requested entry is found */
f623ce95 1304 of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
15c9a0ac 1305 /*
cd209b41 1306 * All of the error cases bail out of the loop, so at
15c9a0ac
GL
1307 * 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 */
23ce04c0 1311 rc = -ENOENT;
15c9a0ac 1312 if (cur_index == index) {
74e1fbb1 1313 if (!it.phandle)
23ce04c0 1314 goto err;
15c9a0ac
GL
1315
1316 if (out_args) {
abdaa77b
JR
1317 int c;
1318
1319 c = of_phandle_iterator_args(&it,
1320 out_args->args,
1321 MAX_PHANDLE_ARGS);
74e1fbb1 1322 out_args->np = it.node;
abdaa77b 1323 out_args->args_count = c;
b855f16b 1324 } else {
74e1fbb1 1325 of_node_put(it.node);
15c9a0ac 1326 }
23ce04c0
GL
1327
1328 /* Found it! return success */
15c9a0ac 1329 return 0;
64b60e09 1330 }
64b60e09 1331
64b60e09
AV
1332 cur_index++;
1333 }
1334
23ce04c0
GL
1335 /*
1336 * Unlock node before returning result; will be one of:
1337 * -ENOENT : index is for empty phandle
1338 * -EINVAL : parsing error on data
1339 */
cd209b41 1340
23ce04c0 1341 err:
beab47d5 1342 of_node_put(it.node);
23ce04c0 1343 return rc;
64b60e09 1344}
bd69f73f 1345
5fba49e3
SW
1346/**
1347 * 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{
91d9942c
SW
1359 struct of_phandle_args args;
1360
1361 if (index < 0)
1362 return NULL;
5fba49e3 1363
91d9942c
SW
1364 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1365 index, &args))
5fba49e3
SW
1366 return NULL;
1367
91d9942c 1368 return args.np;
5fba49e3
SW
1369}
1370EXPORT_SYMBOL(of_parse_phandle);
1371
eded9dd4
SW
1372/**
1373 * 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 *
d94a75c1 1384 * Caller is responsible to call of_node_put() on the returned out_args->np
eded9dd4
SW
1385 * pointer.
1386 *
1387 * Example:
1388 *
1389 * phandle1: node1 {
c0e848d8 1390 * #list-cells = <2>;
eded9dd4
SW
1391 * }
1392 *
1393 * phandle2: node2 {
c0e848d8 1394 * #list-cells = <1>;
eded9dd4
SW
1395 * }
1396 *
1397 * node3 {
c0e848d8 1398 * list = <&phandle1 1 2 &phandle2 3>;
eded9dd4
SW
1399 * }
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 */
bd69f73f
GL
1404int 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;
035fd948
SW
1410 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1411 index, out_args);
bd69f73f 1412}
15c9a0ac 1413EXPORT_SYMBOL(of_parse_phandle_with_args);
02af11b0 1414
bd6f2fd5
SB
1415/**
1416 * 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
035fd948
SW
1599/**
1600 * 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 *
d94a75c1 1611 * Caller is responsible to call of_node_put() on the returned out_args->np
035fd948
SW
1612 * pointer.
1613 *
1614 * Example:
1615 *
1616 * phandle1: node1 {
1617 * }
1618 *
1619 * phandle2: node2 {
1620 * }
1621 *
1622 * node3 {
c0e848d8 1623 * list = <&phandle1 0 2 &phandle2 2 3>;
035fd948
SW
1624 * }
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
bd69f73f
GL
1640/**
1641 * 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{
2021bd01
JR
1658 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;
bd69f73f
GL
1672}
1673EXPORT_SYMBOL(of_count_phandle_with_args);
1674
62664f67
XL
1675/**
1676 * __of_add_property - Add a property to a node without lock operations
1677 */
d8c50088 1678int __of_add_property(struct device_node *np, struct property *prop)
62664f67
XL
1679{
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
02af11b0 1696/**
79d1c712 1697 * of_add_property - Add a property to a node
02af11b0 1698 */
79d1c712 1699int of_add_property(struct device_node *np, struct property *prop)
02af11b0 1700{
02af11b0 1701 unsigned long flags;
1cf3d8b3
NF
1702 int rc;
1703
8a2b22a2 1704 mutex_lock(&of_mutex);
02af11b0 1705
d6d3c4e6 1706 raw_spin_lock_irqsave(&devtree_lock, flags);
62664f67 1707 rc = __of_add_property(np, prop);
d6d3c4e6 1708 raw_spin_unlock_irqrestore(&devtree_lock, flags);
02af11b0 1709
8a2b22a2 1710 if (!rc)
0829f6d1 1711 __of_add_property_sysfs(np, prop);
02af11b0 1712
8a2b22a2
GL
1713 mutex_unlock(&of_mutex);
1714
259092a3
GL
1715 if (!rc)
1716 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1717
62664f67 1718 return rc;
02af11b0
GL
1719}
1720
d8c50088
PA
1721int __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
02af11b0 1740/**
79d1c712 1741 * of_remove_property - Remove a property from a node.
02af11b0
GL
1742 *
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 */
79d1c712 1748int of_remove_property(struct device_node *np, struct property *prop)
02af11b0 1749{
02af11b0 1750 unsigned long flags;
1cf3d8b3
NF
1751 int rc;
1752
201b3fe5
SJS
1753 if (!prop)
1754 return -ENODEV;
1755
8a2b22a2 1756 mutex_lock(&of_mutex);
02af11b0 1757
d6d3c4e6 1758 raw_spin_lock_irqsave(&devtree_lock, flags);
d8c50088 1759 rc = __of_remove_property(np, prop);
d6d3c4e6 1760 raw_spin_unlock_irqrestore(&devtree_lock, flags);
02af11b0 1761
8a2b22a2
GL
1762 if (!rc)
1763 __of_remove_property_sysfs(np, prop);
02af11b0 1764
8a2b22a2 1765 mutex_unlock(&of_mutex);
75b57ecf 1766
259092a3
GL
1767 if (!rc)
1768 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
02af11b0 1769
8a2b22a2 1770 return rc;
02af11b0
GL
1771}
1772
d8c50088
PA
1773int __of_update_property(struct device_node *np, struct property *newprop,
1774 struct property **oldpropp)
02af11b0 1775{
475d0094 1776 struct property **next, *oldprop;
02af11b0 1777
d8c50088
PA
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;
475d0094 1783
d8c50088 1784 if (oldprop) {
947fdaad 1785 /* replace the node */
d8c50088
PA
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;
02af11b0 1794 }
75b57ecf 1795
d8c50088
PA
1796 return 0;
1797}
1798
fcdeb7fe 1799/*
79d1c712 1800 * of_update_property - Update a property in a node, if the property does
475d0094 1801 * not exist, add it.
fcdeb7fe 1802 *
02af11b0
GL
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
fcdeb7fe 1807 */
79d1c712 1808int of_update_property(struct device_node *np, struct property *newprop)
fcdeb7fe 1809{
d8c50088 1810 struct property *oldprop;
fcdeb7fe 1811 unsigned long flags;
1cf3d8b3
NF
1812 int rc;
1813
d8c50088
PA
1814 if (!newprop->name)
1815 return -EINVAL;
1cf3d8b3 1816
8a2b22a2 1817 mutex_lock(&of_mutex);
fcdeb7fe 1818
d6d3c4e6 1819 raw_spin_lock_irqsave(&devtree_lock, flags);
d8c50088 1820 rc = __of_update_property(np, newprop, &oldprop);
d6d3c4e6 1821 raw_spin_unlock_irqrestore(&devtree_lock, flags);
fcdeb7fe 1822
8a2b22a2
GL
1823 if (!rc)
1824 __of_update_property_sysfs(np, newprop, oldprop);
fcdeb7fe 1825
8a2b22a2 1826 mutex_unlock(&of_mutex);
fcdeb7fe 1827
259092a3
GL
1828 if (!rc)
1829 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
e81b3295 1830
1cf3d8b3 1831 return rc;
fcdeb7fe 1832}
fcdeb7fe 1833
611cad72
SG
1834static 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);
0d638a07
RH
1842 pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
1843 ap->alias, ap->stem, ap->id, np);
611cad72
SG
1844}
1845
1846/**
1821dda4 1847 * of_alias_scan - Scan all properties of the 'aliases' node
611cad72 1848 *
1821dda4
GU
1849 * 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.
611cad72
SG
1852 *
1853 * @dt_alloc: An allocator that provides a virtual address to memory
1821dda4 1854 * for storing the resulting tree
611cad72
SG
1855 */
1856void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1857{
1858 struct property *pp;
1859
7dbe5849 1860 of_aliases = of_find_node_by_path("/aliases");
611cad72
SG
1861 of_chosen = of_find_node_by_path("/chosen");
1862 if (of_chosen == NULL)
1863 of_chosen = of_find_node_by_path("/chosen@0");
5c19e952
SH
1864
1865 if (of_chosen) {
a752ee56 1866 /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
b0d9d92f
SS
1867 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);
a752ee56 1872 if (IS_ENABLED(CONFIG_PPC) && !name)
b0d9d92f 1873 of_property_read_string(of_aliases, "stdout", &name);
f64255b5 1874 if (name)
7914a7c5 1875 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
5c19e952
SH
1876 }
1877
611cad72
SG
1878 if (!of_aliases)
1879 return;
1880
8af0da93 1881 for_each_property_of_node(of_aliases, pp) {
611cad72
SG
1882 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 */
de96ec2a 1908 ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
611cad72
SG
1909 if (!ap)
1910 continue;
0640332e 1911 memset(ap, 0, sizeof(*ap) + len + 1);
611cad72
SG
1912 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 *
5a53a07f
GU
1922 * 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.
611cad72
SG
1924 */
1925int of_alias_get_id(struct device_node *np, const char *stem)
1926{
1927 struct alias_prop *app;
1928 int id = -ENODEV;
1929
c05aba2b 1930 mutex_lock(&of_mutex);
611cad72
SG
1931 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 }
c05aba2b 1940 mutex_unlock(&of_mutex);
611cad72
SG
1941
1942 return id;
1943}
1944EXPORT_SYMBOL_GPL(of_alias_get_id);
c541adc6 1945
b1078c35
MS
1946/**
1947 * 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
7acf79b6 1951 * @nbits: Maximum number of alias IDs which can be recorded in bitmap
b1078c35
MS
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) {
7acf79b6 1974 pr_debug("%s: stem comparison didn't pass %s\n",
b1078c35
MS
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 }
7acf79b6 1989 /* Alias exists but is not compatible with matches */
b1078c35
MS
1990 }
1991 mutex_unlock(&of_mutex);
1992
1993 return 0;
1994}
1995EXPORT_SYMBOL_GPL(of_alias_get_alias_list);
1996
351d224f
WS
1997/**
1998 * 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
5c19e952 2023/**
3482f2c5
GL
2024 * 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.
2028 *
2029 * 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.
5c19e952 2032 */
3482f2c5 2033bool of_console_check(struct device_node *dn, char *name, int index)
5c19e952 2034{
3482f2c5 2035 if (!dn || dn != of_stdout || console_set_on_cmdline)
5c19e952 2036 return false;
db179e0d
SS
2037
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);
5c19e952 2043}
3482f2c5 2044EXPORT_SYMBOL_GPL(of_console_check);
a3e31b45
SH
2045
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{
91d96749 2056 struct device_node *child, *cache_node;
a3e31b45 2057
91d96749
RH
2058 cache_node = of_parse_phandle(np, "l2-cache", 0);
2059 if (!cache_node)
2060 cache_node = of_parse_phandle(np, "next-level-cache", 0);
a3e31b45 2061
91d96749
RH
2062 if (cache_node)
2063 return cache_node;
a3e31b45
SH
2064
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}
fd9fdb78 2075
5fa23530
SH
2076/**
2077 * 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}