]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/of/base.c
[media] of: move common endpoint parsing to drivers/of
[mirror_ubuntu-hirsute-kernel.git] / drivers / of / base.c
CommitLineData
97e873e5
SR
1/*
2 * Procedures for creating, accessing and interpreting the device tree.
3 *
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
6 *
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
9 *
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
11 *
e91edcf5
GL
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13 * Grant Likely.
97e873e5
SR
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
611cad72 20#include <linux/ctype.h>
183912d3 21#include <linux/cpu.h>
97e873e5
SR
22#include <linux/module.h>
23#include <linux/of.h>
fd9fdb78 24#include <linux/of_graph.h>
581b605a 25#include <linux/spinlock.h>
5a0e3ad6 26#include <linux/slab.h>
a9f2f63a 27#include <linux/proc_fs.h>
581b605a 28
ced4eec9 29#include "of_private.h"
611cad72 30
ced4eec9 31LIST_HEAD(aliases_lookup);
611cad72 32
465aac6d
RD
33struct device_node *of_allnodes;
34EXPORT_SYMBOL(of_allnodes);
fc0bdae4 35struct device_node *of_chosen;
611cad72 36struct device_node *of_aliases;
5c19e952 37static struct device_node *of_stdout;
611cad72 38
ced4eec9 39DEFINE_MUTEX(of_aliases_mutex);
1ef4d424 40
581b605a
SR
41/* use when traversing tree through the allnext, child, sibling,
42 * or parent members of struct device_node.
43 */
d6d3c4e6 44DEFINE_RAW_SPINLOCK(devtree_lock);
97e873e5
SR
45
46int of_n_addr_cells(struct device_node *np)
47{
a9fadeef 48 const __be32 *ip;
97e873e5
SR
49
50 do {
51 if (np->parent)
52 np = np->parent;
53 ip = of_get_property(np, "#address-cells", NULL);
54 if (ip)
33714881 55 return be32_to_cpup(ip);
97e873e5
SR
56 } while (np->parent);
57 /* No #address-cells property for the root node */
58 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
59}
60EXPORT_SYMBOL(of_n_addr_cells);
61
62int of_n_size_cells(struct device_node *np)
63{
a9fadeef 64 const __be32 *ip;
97e873e5
SR
65
66 do {
67 if (np->parent)
68 np = np->parent;
69 ip = of_get_property(np, "#size-cells", NULL);
70 if (ip)
33714881 71 return be32_to_cpup(ip);
97e873e5
SR
72 } while (np->parent);
73 /* No #size-cells property for the root node */
74 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
75}
76EXPORT_SYMBOL(of_n_size_cells);
77
0c3f061c
RH
78#ifdef CONFIG_NUMA
79int __weak of_node_to_nid(struct device_node *np)
80{
81 return numa_node_id();
82}
83#endif
84
0f22dd39 85#if defined(CONFIG_OF_DYNAMIC)
923f7e30
GL
86/**
87 * of_node_get - Increment refcount of a node
88 * @node: Node to inc refcount, NULL is supported to
89 * simplify writing of callers
90 *
91 * Returns node.
92 */
93struct device_node *of_node_get(struct device_node *node)
94{
95 if (node)
96 kref_get(&node->kref);
97 return node;
98}
99EXPORT_SYMBOL(of_node_get);
100
101static inline struct device_node *kref_to_device_node(struct kref *kref)
102{
103 return container_of(kref, struct device_node, kref);
104}
105
106/**
107 * of_node_release - release a dynamically allocated node
108 * @kref: kref element of the node to be released
109 *
110 * In of_node_put() this function is passed to kref_put()
111 * as the destructor.
112 */
113static void of_node_release(struct kref *kref)
114{
115 struct device_node *node = kref_to_device_node(kref);
116 struct property *prop = node->properties;
117
118 /* We should never be releasing nodes that haven't been detached. */
119 if (!of_node_check_flag(node, OF_DETACHED)) {
120 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
121 dump_stack();
122 kref_init(&node->kref);
123 return;
124 }
125
126 if (!of_node_check_flag(node, OF_DYNAMIC))
127 return;
128
129 while (prop) {
130 struct property *next = prop->next;
131 kfree(prop->name);
132 kfree(prop->value);
133 kfree(prop);
134 prop = next;
135
136 if (!prop) {
137 prop = node->deadprops;
138 node->deadprops = NULL;
139 }
140 }
141 kfree(node->full_name);
142 kfree(node->data);
143 kfree(node);
144}
145
146/**
147 * of_node_put - Decrement refcount of a node
148 * @node: Node to dec refcount, NULL is supported to
149 * simplify writing of callers
150 *
151 */
152void of_node_put(struct device_node *node)
153{
154 if (node)
155 kref_put(&node->kref, of_node_release);
156}
157EXPORT_SYMBOL(of_node_put);
0f22dd39 158#endif /* CONFIG_OF_DYNAMIC */
923f7e30 159
28d0e36b
TG
160static struct property *__of_find_property(const struct device_node *np,
161 const char *name, int *lenp)
581b605a
SR
162{
163 struct property *pp;
164
64e4566f
TT
165 if (!np)
166 return NULL;
167
a3a7cab1 168 for (pp = np->properties; pp; pp = pp->next) {
581b605a 169 if (of_prop_cmp(pp->name, name) == 0) {
a3a7cab1 170 if (lenp)
581b605a
SR
171 *lenp = pp->length;
172 break;
173 }
174 }
28d0e36b
TG
175
176 return pp;
177}
178
179struct property *of_find_property(const struct device_node *np,
180 const char *name,
181 int *lenp)
182{
183 struct property *pp;
d6d3c4e6 184 unsigned long flags;
28d0e36b 185
d6d3c4e6 186 raw_spin_lock_irqsave(&devtree_lock, flags);
28d0e36b 187 pp = __of_find_property(np, name, lenp);
d6d3c4e6 188 raw_spin_unlock_irqrestore(&devtree_lock, flags);
581b605a
SR
189
190 return pp;
191}
192EXPORT_SYMBOL(of_find_property);
193
e91edcf5
GL
194/**
195 * of_find_all_nodes - Get next node in global list
196 * @prev: Previous node or NULL to start iteration
197 * of_node_put() will be called on it
198 *
199 * Returns a node pointer with refcount incremented, use
200 * of_node_put() on it when done.
201 */
202struct device_node *of_find_all_nodes(struct device_node *prev)
203{
204 struct device_node *np;
d25d8694 205 unsigned long flags;
e91edcf5 206
d25d8694 207 raw_spin_lock_irqsave(&devtree_lock, flags);
465aac6d 208 np = prev ? prev->allnext : of_allnodes;
e91edcf5
GL
209 for (; np != NULL; np = np->allnext)
210 if (of_node_get(np))
211 break;
212 of_node_put(prev);
d25d8694 213 raw_spin_unlock_irqrestore(&devtree_lock, flags);
e91edcf5
GL
214 return np;
215}
216EXPORT_SYMBOL(of_find_all_nodes);
217
28d0e36b
TG
218/*
219 * Find a property with a given name for a given node
220 * and return the value.
221 */
222static const void *__of_get_property(const struct device_node *np,
223 const char *name, int *lenp)
224{
225 struct property *pp = __of_find_property(np, name, lenp);
226
227 return pp ? pp->value : NULL;
228}
229
97e873e5
SR
230/*
231 * Find a property with a given name for a given node
232 * and return the value.
233 */
234const void *of_get_property(const struct device_node *np, const char *name,
28d0e36b 235 int *lenp)
97e873e5
SR
236{
237 struct property *pp = of_find_property(np, name, lenp);
238
239 return pp ? pp->value : NULL;
240}
241EXPORT_SYMBOL(of_get_property);
0081cbc3 242
183912d3
SH
243/*
244 * arch_match_cpu_phys_id - Match the given logical CPU and physical id
245 *
246 * @cpu: logical cpu index of a core/thread
247 * @phys_id: physical identifier of a core/thread
248 *
249 * CPU logical to physical index mapping is architecture specific.
250 * However this __weak function provides a default match of physical
251 * id to logical cpu index. phys_id provided here is usually values read
252 * from the device tree which must match the hardware internal registers.
253 *
254 * Returns true if the physical identifier and the logical cpu index
255 * correspond to the same core/thread, false otherwise.
256 */
257bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
258{
259 return (u32)phys_id == cpu;
260}
261
262/**
263 * Checks if the given "prop_name" property holds the physical id of the
264 * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
265 * NULL, local thread number within the core is returned in it.
266 */
267static bool __of_find_n_match_cpu_property(struct device_node *cpun,
268 const char *prop_name, int cpu, unsigned int *thread)
269{
270 const __be32 *cell;
271 int ac, prop_len, tid;
272 u64 hwid;
273
274 ac = of_n_addr_cells(cpun);
275 cell = of_get_property(cpun, prop_name, &prop_len);
f3cea45a 276 if (!cell || !ac)
183912d3 277 return false;
f3cea45a 278 prop_len /= sizeof(*cell) * ac;
183912d3
SH
279 for (tid = 0; tid < prop_len; tid++) {
280 hwid = of_read_number(cell, ac);
281 if (arch_match_cpu_phys_id(cpu, hwid)) {
282 if (thread)
283 *thread = tid;
284 return true;
285 }
286 cell += ac;
287 }
288 return false;
289}
290
d1cb9d1a
DM
291/*
292 * arch_find_n_match_cpu_physical_id - See if the given device node is
293 * for the cpu corresponding to logical cpu 'cpu'. Return true if so,
294 * else false. If 'thread' is non-NULL, the local thread number within the
295 * core is returned in it.
296 */
297bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
298 int cpu, unsigned int *thread)
299{
300 /* Check for non-standard "ibm,ppc-interrupt-server#s" property
301 * for thread ids on PowerPC. If it doesn't exist fallback to
302 * standard "reg" property.
303 */
304 if (IS_ENABLED(CONFIG_PPC) &&
305 __of_find_n_match_cpu_property(cpun,
306 "ibm,ppc-interrupt-server#s",
307 cpu, thread))
308 return true;
309
310 if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
311 return true;
312
313 return false;
314}
315
183912d3
SH
316/**
317 * of_get_cpu_node - Get device node associated with the given logical CPU
318 *
319 * @cpu: CPU number(logical index) for which device node is required
320 * @thread: if not NULL, local thread number within the physical core is
321 * returned
322 *
323 * The main purpose of this function is to retrieve the device node for the
324 * given logical CPU index. It should be used to initialize the of_node in
325 * cpu device. Once of_node in cpu device is populated, all the further
326 * references can use that instead.
327 *
328 * CPU logical to physical index mapping is architecture specific and is built
329 * before booting secondary cores. This function uses arch_match_cpu_phys_id
330 * which can be overridden by architecture specific implementation.
331 *
332 * Returns a node pointer for the logical cpu if found, else NULL.
333 */
334struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
335{
d1cb9d1a 336 struct device_node *cpun;
183912d3 337
d1cb9d1a
DM
338 for_each_node_by_type(cpun, "cpu") {
339 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
183912d3
SH
340 return cpun;
341 }
342 return NULL;
343}
344EXPORT_SYMBOL(of_get_cpu_node);
345
215a14cf
KH
346/**
347 * __of_device_is_compatible() - Check if the node matches given constraints
348 * @device: pointer to node
349 * @compat: required compatible string, NULL or "" for any match
350 * @type: required device_type value, NULL or "" for any match
351 * @name: required node name, NULL or "" for any match
352 *
353 * Checks if the given @compat, @type and @name strings match the
354 * properties of the given @device. A constraints can be skipped by
355 * passing NULL or an empty string as the constraint.
356 *
357 * Returns 0 for no match, and a positive integer on match. The return
358 * value is a relative score with larger values indicating better
359 * matches. The score is weighted for the most specific compatible value
360 * to get the highest score. Matching type is next, followed by matching
361 * name. Practically speaking, this results in the following priority
362 * order for matches:
363 *
364 * 1. specific compatible && type && name
365 * 2. specific compatible && type
366 * 3. specific compatible && name
367 * 4. specific compatible
368 * 5. general compatible && type && name
369 * 6. general compatible && type
370 * 7. general compatible && name
371 * 8. general compatible
372 * 9. type && name
373 * 10. type
374 * 11. name
0081cbc3 375 */
28d0e36b 376static int __of_device_is_compatible(const struct device_node *device,
215a14cf
KH
377 const char *compat, const char *type, const char *name)
378{
379 struct property *prop;
380 const char *cp;
381 int index = 0, score = 0;
382
383 /* Compatible match has highest priority */
384 if (compat && compat[0]) {
385 prop = __of_find_property(device, "compatible", NULL);
386 for (cp = of_prop_next_string(prop, NULL); cp;
387 cp = of_prop_next_string(prop, cp), index++) {
388 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
389 score = INT_MAX/2 - (index << 2);
390 break;
391 }
392 }
393 if (!score)
394 return 0;
395 }
0081cbc3 396
215a14cf
KH
397 /* Matching type is better than matching name */
398 if (type && type[0]) {
399 if (!device->type || of_node_cmp(type, device->type))
400 return 0;
401 score += 2;
0081cbc3
SR
402 }
403
215a14cf
KH
404 /* Matching name is a bit better than not */
405 if (name && name[0]) {
406 if (!device->name || of_node_cmp(name, device->name))
407 return 0;
408 score++;
409 }
410
411 return score;
0081cbc3 412}
28d0e36b
TG
413
414/** Checks if the given "compat" string matches one of the strings in
415 * the device's "compatible" property
416 */
417int of_device_is_compatible(const struct device_node *device,
418 const char *compat)
419{
d6d3c4e6 420 unsigned long flags;
28d0e36b
TG
421 int res;
422
d6d3c4e6 423 raw_spin_lock_irqsave(&devtree_lock, flags);
215a14cf 424 res = __of_device_is_compatible(device, compat, NULL, NULL);
d6d3c4e6 425 raw_spin_unlock_irqrestore(&devtree_lock, flags);
28d0e36b
TG
426 return res;
427}
0081cbc3 428EXPORT_SYMBOL(of_device_is_compatible);
e679c5f4 429
1f43cfb9 430/**
71a157e8 431 * of_machine_is_compatible - Test root of device tree for a given compatible value
1f43cfb9
GL
432 * @compat: compatible string to look for in root node's compatible property.
433 *
434 * Returns true if the root node has the given value in its
435 * compatible property.
436 */
71a157e8 437int of_machine_is_compatible(const char *compat)
1f43cfb9
GL
438{
439 struct device_node *root;
440 int rc = 0;
441
442 root = of_find_node_by_path("/");
443 if (root) {
444 rc = of_device_is_compatible(root, compat);
445 of_node_put(root);
446 }
447 return rc;
448}
71a157e8 449EXPORT_SYMBOL(of_machine_is_compatible);
1f43cfb9 450
834d97d4 451/**
c31a0c05 452 * __of_device_is_available - check if a device is available for use
834d97d4 453 *
c31a0c05 454 * @device: Node to check for availability, with locks already held
834d97d4
JB
455 *
456 * Returns 1 if the status property is absent or set to "okay" or "ok",
457 * 0 otherwise
458 */
c31a0c05 459static int __of_device_is_available(const struct device_node *device)
834d97d4
JB
460{
461 const char *status;
462 int statlen;
463
42ccd781
XL
464 if (!device)
465 return 0;
466
c31a0c05 467 status = __of_get_property(device, "status", &statlen);
834d97d4
JB
468 if (status == NULL)
469 return 1;
470
471 if (statlen > 0) {
472 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
473 return 1;
474 }
475
476 return 0;
477}
c31a0c05
SW
478
479/**
480 * of_device_is_available - check if a device is available for use
481 *
482 * @device: Node to check for availability
483 *
484 * Returns 1 if the status property is absent or set to "okay" or "ok",
485 * 0 otherwise
486 */
487int of_device_is_available(const struct device_node *device)
488{
489 unsigned long flags;
490 int res;
491
492 raw_spin_lock_irqsave(&devtree_lock, flags);
493 res = __of_device_is_available(device);
494 raw_spin_unlock_irqrestore(&devtree_lock, flags);
495 return res;
496
497}
834d97d4
JB
498EXPORT_SYMBOL(of_device_is_available);
499
e679c5f4
SR
500/**
501 * of_get_parent - Get a node's parent if any
502 * @node: Node to get parent
503 *
504 * Returns a node pointer with refcount incremented, use
505 * of_node_put() on it when done.
506 */
507struct device_node *of_get_parent(const struct device_node *node)
508{
509 struct device_node *np;
d6d3c4e6 510 unsigned long flags;
e679c5f4
SR
511
512 if (!node)
513 return NULL;
514
d6d3c4e6 515 raw_spin_lock_irqsave(&devtree_lock, flags);
e679c5f4 516 np = of_node_get(node->parent);
d6d3c4e6 517 raw_spin_unlock_irqrestore(&devtree_lock, flags);
e679c5f4
SR
518 return np;
519}
520EXPORT_SYMBOL(of_get_parent);
d1cd355a 521
f4eb0107
ME
522/**
523 * of_get_next_parent - Iterate to a node's parent
524 * @node: Node to get parent of
525 *
526 * This is like of_get_parent() except that it drops the
527 * refcount on the passed node, making it suitable for iterating
528 * through a node's parents.
529 *
530 * Returns a node pointer with refcount incremented, use
531 * of_node_put() on it when done.
532 */
533struct device_node *of_get_next_parent(struct device_node *node)
534{
535 struct device_node *parent;
d6d3c4e6 536 unsigned long flags;
f4eb0107
ME
537
538 if (!node)
539 return NULL;
540
d6d3c4e6 541 raw_spin_lock_irqsave(&devtree_lock, flags);
f4eb0107
ME
542 parent = of_node_get(node->parent);
543 of_node_put(node);
d6d3c4e6 544 raw_spin_unlock_irqrestore(&devtree_lock, flags);
f4eb0107
ME
545 return parent;
546}
6695be68 547EXPORT_SYMBOL(of_get_next_parent);
f4eb0107 548
d1cd355a
SR
549/**
550 * of_get_next_child - Iterate a node childs
551 * @node: parent node
552 * @prev: previous child of the parent node, or NULL to get first
553 *
554 * Returns a node pointer with refcount incremented, use
555 * of_node_put() on it when done.
556 */
557struct device_node *of_get_next_child(const struct device_node *node,
558 struct device_node *prev)
559{
560 struct device_node *next;
d6d3c4e6 561 unsigned long flags;
d1cd355a 562
d6d3c4e6 563 raw_spin_lock_irqsave(&devtree_lock, flags);
d1cd355a
SR
564 next = prev ? prev->sibling : node->child;
565 for (; next; next = next->sibling)
566 if (of_node_get(next))
567 break;
568 of_node_put(prev);
d6d3c4e6 569 raw_spin_unlock_irqrestore(&devtree_lock, flags);
d1cd355a
SR
570 return next;
571}
572EXPORT_SYMBOL(of_get_next_child);
1ef4d424 573
3296193d
TT
574/**
575 * of_get_next_available_child - Find the next available child node
576 * @node: parent node
577 * @prev: previous child of the parent node, or NULL to get first
578 *
579 * This function is like of_get_next_child(), except that it
580 * automatically skips any disabled nodes (i.e. status = "disabled").
581 */
582struct device_node *of_get_next_available_child(const struct device_node *node,
583 struct device_node *prev)
584{
585 struct device_node *next;
d25d8694 586 unsigned long flags;
3296193d 587
d25d8694 588 raw_spin_lock_irqsave(&devtree_lock, flags);
3296193d
TT
589 next = prev ? prev->sibling : node->child;
590 for (; next; next = next->sibling) {
c31a0c05 591 if (!__of_device_is_available(next))
3296193d
TT
592 continue;
593 if (of_node_get(next))
594 break;
595 }
596 of_node_put(prev);
d25d8694 597 raw_spin_unlock_irqrestore(&devtree_lock, flags);
3296193d
TT
598 return next;
599}
600EXPORT_SYMBOL(of_get_next_available_child);
601
9c19761a
SK
602/**
603 * of_get_child_by_name - Find the child node by name for a given parent
604 * @node: parent node
605 * @name: child name to look for.
606 *
607 * This function looks for child node for given matching name
608 *
609 * Returns a node pointer if found, with refcount incremented, use
610 * of_node_put() on it when done.
611 * Returns NULL if node is not found.
612 */
613struct device_node *of_get_child_by_name(const struct device_node *node,
614 const char *name)
615{
616 struct device_node *child;
617
618 for_each_child_of_node(node, child)
619 if (child->name && (of_node_cmp(child->name, name) == 0))
620 break;
621 return child;
622}
623EXPORT_SYMBOL(of_get_child_by_name);
624
1ef4d424
SR
625/**
626 * of_find_node_by_path - Find a node matching a full OF path
627 * @path: The full path to match
628 *
629 * Returns a node pointer with refcount incremented, use
630 * of_node_put() on it when done.
631 */
632struct device_node *of_find_node_by_path(const char *path)
633{
465aac6d 634 struct device_node *np = of_allnodes;
d6d3c4e6 635 unsigned long flags;
1ef4d424 636
d6d3c4e6 637 raw_spin_lock_irqsave(&devtree_lock, flags);
1ef4d424
SR
638 for (; np; np = np->allnext) {
639 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
640 && of_node_get(np))
641 break;
642 }
d6d3c4e6 643 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1ef4d424
SR
644 return np;
645}
646EXPORT_SYMBOL(of_find_node_by_path);
647
648/**
649 * of_find_node_by_name - Find a node by its "name" property
650 * @from: The node to start searching from or NULL, the node
651 * you pass will not be searched, only the next one
652 * will; typically, you pass what the previous call
653 * returned. of_node_put() will be called on it
654 * @name: The name string to match against
655 *
656 * Returns a node pointer with refcount incremented, use
657 * of_node_put() on it when done.
658 */
659struct device_node *of_find_node_by_name(struct device_node *from,
660 const char *name)
661{
662 struct device_node *np;
d6d3c4e6 663 unsigned long flags;
1ef4d424 664
d6d3c4e6 665 raw_spin_lock_irqsave(&devtree_lock, flags);
465aac6d 666 np = from ? from->allnext : of_allnodes;
1ef4d424
SR
667 for (; np; np = np->allnext)
668 if (np->name && (of_node_cmp(np->name, name) == 0)
669 && of_node_get(np))
670 break;
671 of_node_put(from);
d6d3c4e6 672 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1ef4d424
SR
673 return np;
674}
675EXPORT_SYMBOL(of_find_node_by_name);
676
677/**
678 * of_find_node_by_type - Find a node by its "device_type" property
679 * @from: The node to start searching from, or NULL to start searching
680 * the entire device tree. The node you pass will not be
681 * searched, only the next one will; typically, you pass
682 * what the previous call returned. of_node_put() will be
683 * called on from for you.
684 * @type: The type string to match against
685 *
686 * Returns a node pointer with refcount incremented, use
687 * of_node_put() on it when done.
688 */
689struct device_node *of_find_node_by_type(struct device_node *from,
690 const char *type)
691{
692 struct device_node *np;
d6d3c4e6 693 unsigned long flags;
1ef4d424 694
d6d3c4e6 695 raw_spin_lock_irqsave(&devtree_lock, flags);
465aac6d 696 np = from ? from->allnext : of_allnodes;
1ef4d424
SR
697 for (; np; np = np->allnext)
698 if (np->type && (of_node_cmp(np->type, type) == 0)
699 && of_node_get(np))
700 break;
701 of_node_put(from);
d6d3c4e6 702 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1ef4d424
SR
703 return np;
704}
705EXPORT_SYMBOL(of_find_node_by_type);
706
707/**
708 * of_find_compatible_node - Find a node based on type and one of the
709 * tokens in its "compatible" property
710 * @from: The node to start searching from or NULL, the node
711 * you pass will not be searched, only the next one
712 * will; typically, you pass what the previous call
713 * returned. of_node_put() will be called on it
714 * @type: The type string to match "device_type" or NULL to ignore
715 * @compatible: The string to match to one of the tokens in the device
716 * "compatible" list.
717 *
718 * Returns a node pointer with refcount incremented, use
719 * of_node_put() on it when done.
720 */
721struct device_node *of_find_compatible_node(struct device_node *from,
722 const char *type, const char *compatible)
723{
724 struct device_node *np;
d6d3c4e6 725 unsigned long flags;
1ef4d424 726
d6d3c4e6 727 raw_spin_lock_irqsave(&devtree_lock, flags);
465aac6d 728 np = from ? from->allnext : of_allnodes;
1ef4d424 729 for (; np; np = np->allnext) {
215a14cf 730 if (__of_device_is_compatible(np, compatible, type, NULL) &&
28d0e36b 731 of_node_get(np))
1ef4d424
SR
732 break;
733 }
734 of_node_put(from);
d6d3c4e6 735 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1ef4d424
SR
736 return np;
737}
738EXPORT_SYMBOL(of_find_compatible_node);
283029d1 739
1e291b14
ME
740/**
741 * of_find_node_with_property - Find a node which has a property with
742 * the given name.
743 * @from: The node to start searching from or NULL, the node
744 * you pass will not be searched, only the next one
745 * will; typically, you pass what the previous call
746 * returned. of_node_put() will be called on it
747 * @prop_name: The name of the property to look for.
748 *
749 * Returns a node pointer with refcount incremented, use
750 * of_node_put() on it when done.
751 */
752struct device_node *of_find_node_with_property(struct device_node *from,
753 const char *prop_name)
754{
755 struct device_node *np;
756 struct property *pp;
d6d3c4e6 757 unsigned long flags;
1e291b14 758
d6d3c4e6 759 raw_spin_lock_irqsave(&devtree_lock, flags);
465aac6d 760 np = from ? from->allnext : of_allnodes;
1e291b14 761 for (; np; np = np->allnext) {
a3a7cab1 762 for (pp = np->properties; pp; pp = pp->next) {
1e291b14
ME
763 if (of_prop_cmp(pp->name, prop_name) == 0) {
764 of_node_get(np);
765 goto out;
766 }
767 }
768 }
769out:
770 of_node_put(from);
d6d3c4e6 771 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1e291b14
ME
772 return np;
773}
774EXPORT_SYMBOL(of_find_node_with_property);
775
28d0e36b
TG
776static
777const struct of_device_id *__of_match_node(const struct of_device_id *matches,
778 const struct device_node *node)
283029d1 779{
215a14cf
KH
780 const struct of_device_id *best_match = NULL;
781 int score, best_score = 0;
782
a52f07ec
GL
783 if (!matches)
784 return NULL;
785
215a14cf
KH
786 for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
787 score = __of_device_is_compatible(node, matches->compatible,
788 matches->type, matches->name);
789 if (score > best_score) {
790 best_match = matches;
791 best_score = score;
792 }
4e8ca6ee 793 }
215a14cf
KH
794
795 return best_match;
283029d1 796}
28d0e36b
TG
797
798/**
799 * of_match_node - Tell if an device_node has a matching of_match structure
800 * @matches: array of of device match structures to search in
801 * @node: the of device structure to match against
802 *
71c5498e 803 * Low level utility function used by device matching.
28d0e36b
TG
804 */
805const struct of_device_id *of_match_node(const struct of_device_id *matches,
806 const struct device_node *node)
807{
808 const struct of_device_id *match;
d6d3c4e6 809 unsigned long flags;
28d0e36b 810
d6d3c4e6 811 raw_spin_lock_irqsave(&devtree_lock, flags);
28d0e36b 812 match = __of_match_node(matches, node);
d6d3c4e6 813 raw_spin_unlock_irqrestore(&devtree_lock, flags);
28d0e36b
TG
814 return match;
815}
283029d1
GL
816EXPORT_SYMBOL(of_match_node);
817
818/**
50c8af4c
SW
819 * of_find_matching_node_and_match - Find a node based on an of_device_id
820 * match table.
283029d1
GL
821 * @from: The node to start searching from or NULL, the node
822 * you pass will not be searched, only the next one
823 * will; typically, you pass what the previous call
824 * returned. of_node_put() will be called on it
825 * @matches: array of of device match structures to search in
50c8af4c 826 * @match Updated to point at the matches entry which matched
283029d1
GL
827 *
828 * Returns a node pointer with refcount incremented, use
829 * of_node_put() on it when done.
830 */
50c8af4c
SW
831struct device_node *of_find_matching_node_and_match(struct device_node *from,
832 const struct of_device_id *matches,
833 const struct of_device_id **match)
283029d1
GL
834{
835 struct device_node *np;
dc71bcf1 836 const struct of_device_id *m;
d6d3c4e6 837 unsigned long flags;
283029d1 838
50c8af4c
SW
839 if (match)
840 *match = NULL;
841
d6d3c4e6 842 raw_spin_lock_irqsave(&devtree_lock, flags);
465aac6d 843 np = from ? from->allnext : of_allnodes;
283029d1 844 for (; np; np = np->allnext) {
28d0e36b 845 m = __of_match_node(matches, np);
dc71bcf1 846 if (m && of_node_get(np)) {
50c8af4c 847 if (match)
dc71bcf1 848 *match = m;
283029d1 849 break;
50c8af4c 850 }
283029d1
GL
851 }
852 of_node_put(from);
d6d3c4e6 853 raw_spin_unlock_irqrestore(&devtree_lock, flags);
283029d1
GL
854 return np;
855}
80c2022e 856EXPORT_SYMBOL(of_find_matching_node_and_match);
3f07af49 857
3f07af49
GL
858/**
859 * of_modalias_node - Lookup appropriate modalias for a device node
860 * @node: pointer to a device tree node
861 * @modalias: Pointer to buffer that modalias value will be copied into
862 * @len: Length of modalias value
863 *
2ffe8c5f
GL
864 * Based on the value of the compatible property, this routine will attempt
865 * to choose an appropriate modalias value for a particular device tree node.
866 * It does this by stripping the manufacturer prefix (as delimited by a ',')
867 * from the first entry in the compatible list property.
3f07af49 868 *
2ffe8c5f 869 * This routine returns 0 on success, <0 on failure.
3f07af49
GL
870 */
871int of_modalias_node(struct device_node *node, char *modalias, int len)
872{
2ffe8c5f
GL
873 const char *compatible, *p;
874 int cplen;
3f07af49
GL
875
876 compatible = of_get_property(node, "compatible", &cplen);
2ffe8c5f 877 if (!compatible || strlen(compatible) > cplen)
3f07af49 878 return -ENODEV;
3f07af49 879 p = strchr(compatible, ',');
2ffe8c5f 880 strlcpy(modalias, p ? p + 1 : compatible, len);
3f07af49
GL
881 return 0;
882}
883EXPORT_SYMBOL_GPL(of_modalias_node);
884
89751a7c
JK
885/**
886 * of_find_node_by_phandle - Find a node given a phandle
887 * @handle: phandle of the node to find
888 *
889 * Returns a node pointer with refcount incremented, use
890 * of_node_put() on it when done.
891 */
892struct device_node *of_find_node_by_phandle(phandle handle)
893{
894 struct device_node *np;
d25d8694 895 unsigned long flags;
89751a7c 896
d25d8694 897 raw_spin_lock_irqsave(&devtree_lock, flags);
465aac6d 898 for (np = of_allnodes; np; np = np->allnext)
89751a7c
JK
899 if (np->phandle == handle)
900 break;
901 of_node_get(np);
d25d8694 902 raw_spin_unlock_irqrestore(&devtree_lock, flags);
89751a7c
JK
903 return np;
904}
905EXPORT_SYMBOL(of_find_node_by_phandle);
906
daeec1f0
TP
907/**
908 * of_find_property_value_of_size
909 *
910 * @np: device node from which the property value is to be read.
911 * @propname: name of the property to be searched.
912 * @len: requested length of property value
913 *
914 * Search for a property in a device node and valid the requested size.
915 * Returns the property value on success, -EINVAL if the property does not
916 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
917 * property data isn't large enough.
918 *
919 */
920static void *of_find_property_value_of_size(const struct device_node *np,
921 const char *propname, u32 len)
922{
923 struct property *prop = of_find_property(np, propname, NULL);
924
925 if (!prop)
926 return ERR_PTR(-EINVAL);
927 if (!prop->value)
928 return ERR_PTR(-ENODATA);
929 if (len > prop->length)
930 return ERR_PTR(-EOVERFLOW);
931
932 return prop->value;
933}
934
3daf3726
TP
935/**
936 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
937 *
938 * @np: device node from which the property value is to be read.
939 * @propname: name of the property to be searched.
940 * @index: index of the u32 in the list of values
941 * @out_value: pointer to return value, modified only if no error.
942 *
943 * Search for a property in a device node and read nth 32-bit value from
944 * it. Returns 0 on success, -EINVAL if the property does not exist,
945 * -ENODATA if property does not have a value, and -EOVERFLOW if the
946 * property data isn't large enough.
947 *
948 * The out_value is modified only if a valid u32 value can be decoded.
949 */
950int of_property_read_u32_index(const struct device_node *np,
951 const char *propname,
952 u32 index, u32 *out_value)
953{
daeec1f0
TP
954 const u32 *val = of_find_property_value_of_size(np, propname,
955 ((index + 1) * sizeof(*out_value)));
3daf3726 956
daeec1f0
TP
957 if (IS_ERR(val))
958 return PTR_ERR(val);
3daf3726 959
daeec1f0 960 *out_value = be32_to_cpup(((__be32 *)val) + index);
3daf3726
TP
961 return 0;
962}
963EXPORT_SYMBOL_GPL(of_property_read_u32_index);
964
be193249
VK
965/**
966 * of_property_read_u8_array - Find and read an array of u8 from a property.
967 *
968 * @np: device node from which the property value is to be read.
969 * @propname: name of the property to be searched.
792efb84 970 * @out_values: pointer to return value, modified only if return value is 0.
be193249
VK
971 * @sz: number of array elements to read
972 *
973 * Search for a property in a device node and read 8-bit value(s) from
974 * it. Returns 0 on success, -EINVAL if the property does not exist,
975 * -ENODATA if property does not have a value, and -EOVERFLOW if the
976 * property data isn't large enough.
977 *
978 * dts entry of array should be like:
979 * property = /bits/ 8 <0x50 0x60 0x70>;
980 *
792efb84 981 * The out_values is modified only if a valid u8 value can be decoded.
be193249
VK
982 */
983int of_property_read_u8_array(const struct device_node *np,
984 const char *propname, u8 *out_values, size_t sz)
985{
daeec1f0
TP
986 const u8 *val = of_find_property_value_of_size(np, propname,
987 (sz * sizeof(*out_values)));
be193249 988
daeec1f0
TP
989 if (IS_ERR(val))
990 return PTR_ERR(val);
be193249 991
be193249
VK
992 while (sz--)
993 *out_values++ = *val++;
994 return 0;
995}
996EXPORT_SYMBOL_GPL(of_property_read_u8_array);
997
998/**
999 * of_property_read_u16_array - Find and read an array of u16 from a property.
1000 *
1001 * @np: device node from which the property value is to be read.
1002 * @propname: name of the property to be searched.
792efb84 1003 * @out_values: pointer to return value, modified only if return value is 0.
be193249
VK
1004 * @sz: number of array elements to read
1005 *
1006 * Search for a property in a device node and read 16-bit value(s) from
1007 * it. Returns 0 on success, -EINVAL if the property does not exist,
1008 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1009 * property data isn't large enough.
1010 *
1011 * dts entry of array should be like:
1012 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
1013 *
792efb84 1014 * The out_values is modified only if a valid u16 value can be decoded.
be193249
VK
1015 */
1016int of_property_read_u16_array(const struct device_node *np,
1017 const char *propname, u16 *out_values, size_t sz)
1018{
daeec1f0
TP
1019 const __be16 *val = of_find_property_value_of_size(np, propname,
1020 (sz * sizeof(*out_values)));
be193249 1021
daeec1f0
TP
1022 if (IS_ERR(val))
1023 return PTR_ERR(val);
be193249 1024
be193249
VK
1025 while (sz--)
1026 *out_values++ = be16_to_cpup(val++);
1027 return 0;
1028}
1029EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1030
a3b85363 1031/**
0e373639
RH
1032 * of_property_read_u32_array - Find and read an array of 32 bit integers
1033 * from a property.
1034 *
a3b85363
TA
1035 * @np: device node from which the property value is to be read.
1036 * @propname: name of the property to be searched.
792efb84 1037 * @out_values: pointer to return value, modified only if return value is 0.
be193249 1038 * @sz: number of array elements to read
a3b85363 1039 *
0e373639 1040 * Search for a property in a device node and read 32-bit value(s) from
a3b85363
TA
1041 * it. Returns 0 on success, -EINVAL if the property does not exist,
1042 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1043 * property data isn't large enough.
1044 *
792efb84 1045 * The out_values is modified only if a valid u32 value can be decoded.
a3b85363 1046 */
aac285c6
JI
1047int of_property_read_u32_array(const struct device_node *np,
1048 const char *propname, u32 *out_values,
1049 size_t sz)
a3b85363 1050{
daeec1f0
TP
1051 const __be32 *val = of_find_property_value_of_size(np, propname,
1052 (sz * sizeof(*out_values)));
a3b85363 1053
daeec1f0
TP
1054 if (IS_ERR(val))
1055 return PTR_ERR(val);
0e373639 1056
0e373639
RH
1057 while (sz--)
1058 *out_values++ = be32_to_cpup(val++);
a3b85363
TA
1059 return 0;
1060}
0e373639 1061EXPORT_SYMBOL_GPL(of_property_read_u32_array);
a3b85363 1062
4cd7f7a3
JI
1063/**
1064 * of_property_read_u64 - Find and read a 64 bit integer from a property
1065 * @np: device node from which the property value is to be read.
1066 * @propname: name of the property to be searched.
1067 * @out_value: pointer to return value, modified only if return value is 0.
1068 *
1069 * Search for a property in a device node and read a 64-bit value from
1070 * it. Returns 0 on success, -EINVAL if the property does not exist,
1071 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1072 * property data isn't large enough.
1073 *
1074 * The out_value is modified only if a valid u64 value can be decoded.
1075 */
1076int of_property_read_u64(const struct device_node *np, const char *propname,
1077 u64 *out_value)
1078{
daeec1f0
TP
1079 const __be32 *val = of_find_property_value_of_size(np, propname,
1080 sizeof(*out_value));
4cd7f7a3 1081
daeec1f0
TP
1082 if (IS_ERR(val))
1083 return PTR_ERR(val);
1084
1085 *out_value = of_read_number(val, 2);
4cd7f7a3
JI
1086 return 0;
1087}
1088EXPORT_SYMBOL_GPL(of_property_read_u64);
1089
a3b85363
TA
1090/**
1091 * of_property_read_string - Find and read a string from a property
1092 * @np: device node from which the property value is to be read.
1093 * @propname: name of the property to be searched.
1094 * @out_string: pointer to null terminated return string, modified only if
1095 * return value is 0.
1096 *
1097 * Search for a property in a device tree node and retrieve a null
1098 * terminated string value (pointer to data, not a copy). Returns 0 on
1099 * success, -EINVAL if the property does not exist, -ENODATA if property
1100 * does not have a value, and -EILSEQ if the string is not null-terminated
1101 * within the length of the property data.
1102 *
1103 * The out_string pointer is modified only if a valid string can be decoded.
1104 */
aac285c6 1105int of_property_read_string(struct device_node *np, const char *propname,
f09bc831 1106 const char **out_string)
a3b85363
TA
1107{
1108 struct property *prop = of_find_property(np, propname, NULL);
1109 if (!prop)
1110 return -EINVAL;
1111 if (!prop->value)
1112 return -ENODATA;
1113 if (strnlen(prop->value, prop->length) >= prop->length)
1114 return -EILSEQ;
1115 *out_string = prop->value;
1116 return 0;
1117}
1118EXPORT_SYMBOL_GPL(of_property_read_string);
1119
4fcd15a0
BC
1120/**
1121 * of_property_read_string_index - Find and read a string from a multiple
1122 * strings property.
1123 * @np: device node from which the property value is to be read.
1124 * @propname: name of the property to be searched.
1125 * @index: index of the string in the list of strings
1126 * @out_string: pointer to null terminated return string, modified only if
1127 * return value is 0.
1128 *
1129 * Search for a property in a device tree node and retrieve a null
1130 * terminated string value (pointer to data, not a copy) in the list of strings
1131 * contained in that property.
1132 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1133 * property does not have a value, and -EILSEQ if the string is not
1134 * null-terminated within the length of the property data.
1135 *
1136 * The out_string pointer is modified only if a valid string can be decoded.
1137 */
1138int of_property_read_string_index(struct device_node *np, const char *propname,
1139 int index, const char **output)
1140{
1141 struct property *prop = of_find_property(np, propname, NULL);
1142 int i = 0;
1143 size_t l = 0, total = 0;
1144 const char *p;
1145
1146 if (!prop)
1147 return -EINVAL;
1148 if (!prop->value)
1149 return -ENODATA;
1150 if (strnlen(prop->value, prop->length) >= prop->length)
1151 return -EILSEQ;
1152
1153 p = prop->value;
1154
1155 for (i = 0; total < prop->length; total += l, p += l) {
1156 l = strlen(p) + 1;
88af7f58 1157 if (i++ == index) {
4fcd15a0
BC
1158 *output = p;
1159 return 0;
1160 }
1161 }
1162 return -ENODATA;
1163}
1164EXPORT_SYMBOL_GPL(of_property_read_string_index);
1165
7aff0fe3
GL
1166/**
1167 * of_property_match_string() - Find string in a list and return index
1168 * @np: pointer to node containing string list property
1169 * @propname: string list property name
1170 * @string: pointer to string to search for in string list
1171 *
1172 * This function searches a string list property and returns the index
1173 * of a specific string value.
1174 */
1175int of_property_match_string(struct device_node *np, const char *propname,
1176 const char *string)
1177{
1178 struct property *prop = of_find_property(np, propname, NULL);
1179 size_t l;
1180 int i;
1181 const char *p, *end;
1182
1183 if (!prop)
1184 return -EINVAL;
1185 if (!prop->value)
1186 return -ENODATA;
1187
1188 p = prop->value;
1189 end = p + prop->length;
1190
1191 for (i = 0; p < end; i++, p += l) {
1192 l = strlen(p) + 1;
1193 if (p + l > end)
1194 return -EILSEQ;
1195 pr_debug("comparing %s with %s\n", string, p);
1196 if (strcmp(string, p) == 0)
1197 return i; /* Found it; return index */
1198 }
1199 return -ENODATA;
1200}
1201EXPORT_SYMBOL_GPL(of_property_match_string);
4fcd15a0
BC
1202
1203/**
1204 * of_property_count_strings - Find and return the number of strings from a
1205 * multiple strings property.
1206 * @np: device node from which the property value is to be read.
1207 * @propname: name of the property to be searched.
1208 *
1209 * Search for a property in a device tree node and retrieve the number of null
1210 * terminated string contain in it. Returns the number of strings on
1211 * success, -EINVAL if the property does not exist, -ENODATA if property
1212 * does not have a value, and -EILSEQ if the string is not null-terminated
1213 * within the length of the property data.
1214 */
1215int of_property_count_strings(struct device_node *np, const char *propname)
1216{
1217 struct property *prop = of_find_property(np, propname, NULL);
1218 int i = 0;
1219 size_t l = 0, total = 0;
1220 const char *p;
1221
1222 if (!prop)
1223 return -EINVAL;
1224 if (!prop->value)
1225 return -ENODATA;
1226 if (strnlen(prop->value, prop->length) >= prop->length)
1227 return -EILSEQ;
1228
1229 p = prop->value;
1230
88af7f58 1231 for (i = 0; total < prop->length; total += l, p += l, i++)
4fcd15a0 1232 l = strlen(p) + 1;
88af7f58 1233
4fcd15a0
BC
1234 return i;
1235}
1236EXPORT_SYMBOL_GPL(of_property_count_strings);
1237
624cfca5
GL
1238void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1239{
1240 int i;
1241 printk("%s %s", msg, of_node_full_name(args->np));
1242 for (i = 0; i < args->args_count; i++)
1243 printk(i ? ",%08x" : ":%08x", args->args[i]);
1244 printk("\n");
1245}
1246
bd69f73f
GL
1247static int __of_parse_phandle_with_args(const struct device_node *np,
1248 const char *list_name,
035fd948
SW
1249 const char *cells_name,
1250 int cell_count, int index,
bd69f73f 1251 struct of_phandle_args *out_args)
64b60e09 1252{
15c9a0ac 1253 const __be32 *list, *list_end;
23ce04c0 1254 int rc = 0, size, cur_index = 0;
15c9a0ac 1255 uint32_t count = 0;
64b60e09 1256 struct device_node *node = NULL;
15c9a0ac 1257 phandle phandle;
64b60e09 1258
15c9a0ac 1259 /* Retrieve the phandle list property */
64b60e09 1260 list = of_get_property(np, list_name, &size);
15c9a0ac 1261 if (!list)
1af4c7f1 1262 return -ENOENT;
64b60e09
AV
1263 list_end = list + size / sizeof(*list);
1264
15c9a0ac 1265 /* Loop over the phandles until all the requested entry is found */
64b60e09 1266 while (list < list_end) {
23ce04c0 1267 rc = -EINVAL;
15c9a0ac 1268 count = 0;
64b60e09 1269
15c9a0ac
GL
1270 /*
1271 * If phandle is 0, then it is an empty entry with no
1272 * arguments. Skip forward to the next entry.
1273 */
9a6b2e58 1274 phandle = be32_to_cpup(list++);
15c9a0ac
GL
1275 if (phandle) {
1276 /*
1277 * Find the provider node and parse the #*-cells
91d9942c
SW
1278 * property to determine the argument length.
1279 *
1280 * This is not needed if the cell count is hard-coded
1281 * (i.e. cells_name not set, but cell_count is set),
1282 * except when we're going to return the found node
1283 * below.
15c9a0ac 1284 */
91d9942c
SW
1285 if (cells_name || cur_index == index) {
1286 node = of_find_node_by_phandle(phandle);
1287 if (!node) {
1288 pr_err("%s: could not find phandle\n",
1289 np->full_name);
1290 goto err;
1291 }
15c9a0ac 1292 }
035fd948
SW
1293
1294 if (cells_name) {
1295 if (of_property_read_u32(node, cells_name,
1296 &count)) {
1297 pr_err("%s: could not get %s for %s\n",
1298 np->full_name, cells_name,
1299 node->full_name);
1300 goto err;
1301 }
1302 } else {
1303 count = cell_count;
15c9a0ac 1304 }
64b60e09 1305
15c9a0ac
GL
1306 /*
1307 * Make sure that the arguments actually fit in the
1308 * remaining property data length
1309 */
1310 if (list + count > list_end) {
1311 pr_err("%s: arguments longer than property\n",
1312 np->full_name);
23ce04c0 1313 goto err;
15c9a0ac 1314 }
64b60e09
AV
1315 }
1316
15c9a0ac
GL
1317 /*
1318 * All of the error cases above bail out of the loop, so at
1319 * this point, the parsing is successful. If the requested
1320 * index matches, then fill the out_args structure and return,
1321 * or return -ENOENT for an empty entry.
1322 */
23ce04c0 1323 rc = -ENOENT;
15c9a0ac
GL
1324 if (cur_index == index) {
1325 if (!phandle)
23ce04c0 1326 goto err;
15c9a0ac
GL
1327
1328 if (out_args) {
1329 int i;
1330 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1331 count = MAX_PHANDLE_ARGS;
1332 out_args->np = node;
1333 out_args->args_count = count;
1334 for (i = 0; i < count; i++)
1335 out_args->args[i] = be32_to_cpup(list++);
b855f16b
TY
1336 } else {
1337 of_node_put(node);
15c9a0ac 1338 }
23ce04c0
GL
1339
1340 /* Found it! return success */
15c9a0ac 1341 return 0;
64b60e09 1342 }
64b60e09
AV
1343
1344 of_node_put(node);
1345 node = NULL;
15c9a0ac 1346 list += count;
64b60e09
AV
1347 cur_index++;
1348 }
1349
23ce04c0
GL
1350 /*
1351 * Unlock node before returning result; will be one of:
1352 * -ENOENT : index is for empty phandle
1353 * -EINVAL : parsing error on data
bd69f73f 1354 * [1..n] : Number of phandle (count mode; when index = -1)
23ce04c0 1355 */
bd69f73f 1356 rc = index < 0 ? cur_index : -ENOENT;
23ce04c0 1357 err:
15c9a0ac
GL
1358 if (node)
1359 of_node_put(node);
23ce04c0 1360 return rc;
64b60e09 1361}
bd69f73f 1362
5fba49e3
SW
1363/**
1364 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1365 * @np: Pointer to device node holding phandle property
1366 * @phandle_name: Name of property holding a phandle value
1367 * @index: For properties holding a table of phandles, this is the index into
1368 * the table
1369 *
1370 * Returns the device_node pointer with refcount incremented. Use
1371 * of_node_put() on it when done.
1372 */
1373struct device_node *of_parse_phandle(const struct device_node *np,
1374 const char *phandle_name, int index)
1375{
91d9942c
SW
1376 struct of_phandle_args args;
1377
1378 if (index < 0)
1379 return NULL;
5fba49e3 1380
91d9942c
SW
1381 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1382 index, &args))
5fba49e3
SW
1383 return NULL;
1384
91d9942c 1385 return args.np;
5fba49e3
SW
1386}
1387EXPORT_SYMBOL(of_parse_phandle);
1388
eded9dd4
SW
1389/**
1390 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1391 * @np: pointer to a device tree node containing a list
1392 * @list_name: property name that contains a list
1393 * @cells_name: property name that specifies phandles' arguments count
1394 * @index: index of a phandle to parse out
1395 * @out_args: optional pointer to output arguments structure (will be filled)
1396 *
1397 * This function is useful to parse lists of phandles and their arguments.
1398 * Returns 0 on success and fills out_args, on error returns appropriate
1399 * errno value.
1400 *
1401 * Caller is responsible to call of_node_put() on the returned out_args->node
1402 * pointer.
1403 *
1404 * Example:
1405 *
1406 * phandle1: node1 {
1407 * #list-cells = <2>;
1408 * }
1409 *
1410 * phandle2: node2 {
1411 * #list-cells = <1>;
1412 * }
1413 *
1414 * node3 {
1415 * list = <&phandle1 1 2 &phandle2 3>;
1416 * }
1417 *
1418 * To get a device_node of the `node2' node you may call this:
1419 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1420 */
bd69f73f
GL
1421int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1422 const char *cells_name, int index,
1423 struct of_phandle_args *out_args)
1424{
1425 if (index < 0)
1426 return -EINVAL;
035fd948
SW
1427 return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1428 index, out_args);
bd69f73f 1429}
15c9a0ac 1430EXPORT_SYMBOL(of_parse_phandle_with_args);
02af11b0 1431
035fd948
SW
1432/**
1433 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1434 * @np: pointer to a device tree node containing a list
1435 * @list_name: property name that contains a list
1436 * @cell_count: number of argument cells following the phandle
1437 * @index: index of a phandle to parse out
1438 * @out_args: optional pointer to output arguments structure (will be filled)
1439 *
1440 * This function is useful to parse lists of phandles and their arguments.
1441 * Returns 0 on success and fills out_args, on error returns appropriate
1442 * errno value.
1443 *
1444 * Caller is responsible to call of_node_put() on the returned out_args->node
1445 * pointer.
1446 *
1447 * Example:
1448 *
1449 * phandle1: node1 {
1450 * }
1451 *
1452 * phandle2: node2 {
1453 * }
1454 *
1455 * node3 {
1456 * list = <&phandle1 0 2 &phandle2 2 3>;
1457 * }
1458 *
1459 * To get a device_node of the `node2' node you may call this:
1460 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1461 */
1462int of_parse_phandle_with_fixed_args(const struct device_node *np,
1463 const char *list_name, int cell_count,
1464 int index, struct of_phandle_args *out_args)
1465{
1466 if (index < 0)
1467 return -EINVAL;
1468 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1469 index, out_args);
1470}
1471EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1472
bd69f73f
GL
1473/**
1474 * of_count_phandle_with_args() - Find the number of phandles references in a property
1475 * @np: pointer to a device tree node containing a list
1476 * @list_name: property name that contains a list
1477 * @cells_name: property name that specifies phandles' arguments count
1478 *
1479 * Returns the number of phandle + argument tuples within a property. It
1480 * is a typical pattern to encode a list of phandle and variable
1481 * arguments into a single property. The number of arguments is encoded
1482 * by a property in the phandle-target node. For example, a gpios
1483 * property would contain a list of GPIO specifies consisting of a
1484 * phandle and 1 or more arguments. The number of arguments are
1485 * determined by the #gpio-cells property in the node pointed to by the
1486 * phandle.
1487 */
1488int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1489 const char *cells_name)
1490{
035fd948
SW
1491 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1492 NULL);
bd69f73f
GL
1493}
1494EXPORT_SYMBOL(of_count_phandle_with_args);
1495
1cf3d8b3
NF
1496#if defined(CONFIG_OF_DYNAMIC)
1497static int of_property_notify(int action, struct device_node *np,
1498 struct property *prop)
1499{
1500 struct of_prop_reconfig pr;
1501
1502 pr.dn = np;
1503 pr.prop = prop;
1504 return of_reconfig_notify(action, &pr);
1505}
1506#else
1507static int of_property_notify(int action, struct device_node *np,
1508 struct property *prop)
1509{
1510 return 0;
1511}
1512#endif
1513
02af11b0 1514/**
79d1c712 1515 * of_add_property - Add a property to a node
02af11b0 1516 */
79d1c712 1517int of_add_property(struct device_node *np, struct property *prop)
02af11b0
GL
1518{
1519 struct property **next;
1520 unsigned long flags;
1cf3d8b3
NF
1521 int rc;
1522
1523 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1524 if (rc)
1525 return rc;
02af11b0
GL
1526
1527 prop->next = NULL;
d6d3c4e6 1528 raw_spin_lock_irqsave(&devtree_lock, flags);
02af11b0
GL
1529 next = &np->properties;
1530 while (*next) {
1531 if (strcmp(prop->name, (*next)->name) == 0) {
1532 /* duplicate ! don't insert it */
d6d3c4e6 1533 raw_spin_unlock_irqrestore(&devtree_lock, flags);
02af11b0
GL
1534 return -1;
1535 }
1536 next = &(*next)->next;
1537 }
1538 *next = prop;
d6d3c4e6 1539 raw_spin_unlock_irqrestore(&devtree_lock, flags);
02af11b0
GL
1540
1541#ifdef CONFIG_PROC_DEVICETREE
1542 /* try to add to proc as well if it was initialized */
1543 if (np->pde)
1544 proc_device_tree_add_prop(np->pde, prop);
1545#endif /* CONFIG_PROC_DEVICETREE */
1546
1547 return 0;
1548}
1549
1550/**
79d1c712 1551 * of_remove_property - Remove a property from a node.
02af11b0
GL
1552 *
1553 * Note that we don't actually remove it, since we have given out
1554 * who-knows-how-many pointers to the data using get-property.
1555 * Instead we just move the property to the "dead properties"
1556 * list, so it won't be found any more.
1557 */
79d1c712 1558int of_remove_property(struct device_node *np, struct property *prop)
02af11b0
GL
1559{
1560 struct property **next;
1561 unsigned long flags;
1562 int found = 0;
1cf3d8b3
NF
1563 int rc;
1564
1565 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1566 if (rc)
1567 return rc;
02af11b0 1568
d6d3c4e6 1569 raw_spin_lock_irqsave(&devtree_lock, flags);
02af11b0
GL
1570 next = &np->properties;
1571 while (*next) {
1572 if (*next == prop) {
1573 /* found the node */
1574 *next = prop->next;
1575 prop->next = np->deadprops;
1576 np->deadprops = prop;
1577 found = 1;
1578 break;
1579 }
1580 next = &(*next)->next;
1581 }
d6d3c4e6 1582 raw_spin_unlock_irqrestore(&devtree_lock, flags);
02af11b0
GL
1583
1584 if (!found)
1585 return -ENODEV;
1586
1587#ifdef CONFIG_PROC_DEVICETREE
1588 /* try to remove the proc node as well */
1589 if (np->pde)
1590 proc_device_tree_remove_prop(np->pde, prop);
1591#endif /* CONFIG_PROC_DEVICETREE */
1592
1593 return 0;
1594}
1595
1596/*
79d1c712 1597 * of_update_property - Update a property in a node, if the property does
475d0094 1598 * not exist, add it.
02af11b0
GL
1599 *
1600 * Note that we don't actually remove it, since we have given out
1601 * who-knows-how-many pointers to the data using get-property.
1602 * Instead we just move the property to the "dead properties" list,
1603 * and add the new property to the property list
1604 */
79d1c712 1605int of_update_property(struct device_node *np, struct property *newprop)
02af11b0 1606{
475d0094 1607 struct property **next, *oldprop;
02af11b0 1608 unsigned long flags;
1cf3d8b3
NF
1609 int rc, found = 0;
1610
1611 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1612 if (rc)
1613 return rc;
02af11b0 1614
475d0094
DA
1615 if (!newprop->name)
1616 return -EINVAL;
1617
1618 oldprop = of_find_property(np, newprop->name, NULL);
1619 if (!oldprop)
79d1c712 1620 return of_add_property(np, newprop);
475d0094 1621
d6d3c4e6 1622 raw_spin_lock_irqsave(&devtree_lock, flags);
02af11b0
GL
1623 next = &np->properties;
1624 while (*next) {
1625 if (*next == oldprop) {
1626 /* found the node */
1627 newprop->next = oldprop->next;
1628 *next = newprop;
1629 oldprop->next = np->deadprops;
1630 np->deadprops = oldprop;
1631 found = 1;
1632 break;
1633 }
1634 next = &(*next)->next;
1635 }
d6d3c4e6 1636 raw_spin_unlock_irqrestore(&devtree_lock, flags);
02af11b0
GL
1637
1638 if (!found)
1639 return -ENODEV;
1640
1641#ifdef CONFIG_PROC_DEVICETREE
1642 /* try to add to proc as well if it was initialized */
1643 if (np->pde)
1644 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1645#endif /* CONFIG_PROC_DEVICETREE */
1646
1647 return 0;
1648}
fcdeb7fe
GL
1649
1650#if defined(CONFIG_OF_DYNAMIC)
1651/*
1652 * Support for dynamic device trees.
1653 *
1654 * On some platforms, the device tree can be manipulated at runtime.
1655 * The routines in this section support adding, removing and changing
1656 * device tree nodes.
1657 */
1658
1cf3d8b3
NF
1659static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1660
1661int of_reconfig_notifier_register(struct notifier_block *nb)
1662{
1663 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1664}
1a9bd454 1665EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
1cf3d8b3
NF
1666
1667int of_reconfig_notifier_unregister(struct notifier_block *nb)
1668{
1669 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1670}
1a9bd454 1671EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
1cf3d8b3
NF
1672
1673int of_reconfig_notify(unsigned long action, void *p)
1674{
1675 int rc;
1676
1677 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1678 return notifier_to_errno(rc);
1679}
1680
e81b3295
NF
1681#ifdef CONFIG_PROC_DEVICETREE
1682static void of_add_proc_dt_entry(struct device_node *dn)
1683{
1684 struct proc_dir_entry *ent;
1685
1686 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1687 if (ent)
1688 proc_device_tree_add_node(dn, ent);
1689}
1690#else
1691static void of_add_proc_dt_entry(struct device_node *dn)
1692{
1693 return;
1694}
1695#endif
1696
fcdeb7fe
GL
1697/**
1698 * of_attach_node - Plug a device node into the tree and global list.
1699 */
1cf3d8b3 1700int of_attach_node(struct device_node *np)
fcdeb7fe
GL
1701{
1702 unsigned long flags;
1cf3d8b3
NF
1703 int rc;
1704
1705 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1706 if (rc)
1707 return rc;
fcdeb7fe 1708
d6d3c4e6 1709 raw_spin_lock_irqsave(&devtree_lock, flags);
fcdeb7fe 1710 np->sibling = np->parent->child;
465aac6d 1711 np->allnext = of_allnodes;
fcdeb7fe 1712 np->parent->child = np;
465aac6d 1713 of_allnodes = np;
d6d3c4e6 1714 raw_spin_unlock_irqrestore(&devtree_lock, flags);
e81b3295
NF
1715
1716 of_add_proc_dt_entry(np);
1cf3d8b3 1717 return 0;
fcdeb7fe
GL
1718}
1719
e81b3295
NF
1720#ifdef CONFIG_PROC_DEVICETREE
1721static void of_remove_proc_dt_entry(struct device_node *dn)
1722{
a8ca16ea 1723 proc_remove(dn->pde);
e81b3295
NF
1724}
1725#else
1726static void of_remove_proc_dt_entry(struct device_node *dn)
1727{
1728 return;
1729}
1730#endif
1731
fcdeb7fe
GL
1732/**
1733 * of_detach_node - "Unplug" a node from the device tree.
1734 *
1735 * The caller must hold a reference to the node. The memory associated with
1736 * the node is not freed until its refcount goes to zero.
1737 */
1cf3d8b3 1738int of_detach_node(struct device_node *np)
fcdeb7fe
GL
1739{
1740 struct device_node *parent;
1741 unsigned long flags;
1cf3d8b3
NF
1742 int rc = 0;
1743
1744 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1745 if (rc)
1746 return rc;
fcdeb7fe 1747
d6d3c4e6 1748 raw_spin_lock_irqsave(&devtree_lock, flags);
fcdeb7fe 1749
e81b3295
NF
1750 if (of_node_check_flag(np, OF_DETACHED)) {
1751 /* someone already detached it */
d6d3c4e6 1752 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1cf3d8b3 1753 return rc;
e81b3295
NF
1754 }
1755
fcdeb7fe 1756 parent = np->parent;
e81b3295 1757 if (!parent) {
d6d3c4e6 1758 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1cf3d8b3 1759 return rc;
e81b3295 1760 }
fcdeb7fe 1761
465aac6d
RD
1762 if (of_allnodes == np)
1763 of_allnodes = np->allnext;
fcdeb7fe
GL
1764 else {
1765 struct device_node *prev;
465aac6d 1766 for (prev = of_allnodes;
fcdeb7fe
GL
1767 prev->allnext != np;
1768 prev = prev->allnext)
1769 ;
1770 prev->allnext = np->allnext;
1771 }
1772
1773 if (parent->child == np)
1774 parent->child = np->sibling;
1775 else {
1776 struct device_node *prevsib;
1777 for (prevsib = np->parent->child;
1778 prevsib->sibling != np;
1779 prevsib = prevsib->sibling)
1780 ;
1781 prevsib->sibling = np->sibling;
1782 }
1783
1784 of_node_set_flag(np, OF_DETACHED);
d6d3c4e6 1785 raw_spin_unlock_irqrestore(&devtree_lock, flags);
e81b3295
NF
1786
1787 of_remove_proc_dt_entry(np);
1cf3d8b3 1788 return rc;
fcdeb7fe
GL
1789}
1790#endif /* defined(CONFIG_OF_DYNAMIC) */
1791
611cad72
SG
1792static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1793 int id, const char *stem, int stem_len)
1794{
1795 ap->np = np;
1796 ap->id = id;
1797 strncpy(ap->stem, stem, stem_len);
1798 ap->stem[stem_len] = 0;
1799 list_add_tail(&ap->link, &aliases_lookup);
1800 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
74a7f084 1801 ap->alias, ap->stem, ap->id, of_node_full_name(np));
611cad72
SG
1802}
1803
1804/**
1805 * of_alias_scan - Scan all properties of 'aliases' node
1806 *
1807 * The function scans all the properties of 'aliases' node and populate
1808 * the the global lookup table with the properties. It returns the
1809 * number of alias_prop found, or error code in error case.
1810 *
1811 * @dt_alloc: An allocator that provides a virtual address to memory
1812 * for the resulting tree
1813 */
1814void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1815{
1816 struct property *pp;
1817
1818 of_chosen = of_find_node_by_path("/chosen");
1819 if (of_chosen == NULL)
1820 of_chosen = of_find_node_by_path("/chosen@0");
5c19e952
SH
1821
1822 if (of_chosen) {
1823 const char *name;
1824
1825 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1826 if (name)
1827 of_stdout = of_find_node_by_path(name);
1828 }
1829
611cad72
SG
1830 of_aliases = of_find_node_by_path("/aliases");
1831 if (!of_aliases)
1832 return;
1833
8af0da93 1834 for_each_property_of_node(of_aliases, pp) {
611cad72
SG
1835 const char *start = pp->name;
1836 const char *end = start + strlen(start);
1837 struct device_node *np;
1838 struct alias_prop *ap;
1839 int id, len;
1840
1841 /* Skip those we do not want to proceed */
1842 if (!strcmp(pp->name, "name") ||
1843 !strcmp(pp->name, "phandle") ||
1844 !strcmp(pp->name, "linux,phandle"))
1845 continue;
1846
1847 np = of_find_node_by_path(pp->value);
1848 if (!np)
1849 continue;
1850
1851 /* walk the alias backwards to extract the id and work out
1852 * the 'stem' string */
1853 while (isdigit(*(end-1)) && end > start)
1854 end--;
1855 len = end - start;
1856
1857 if (kstrtoint(end, 10, &id) < 0)
1858 continue;
1859
1860 /* Allocate an alias_prop with enough space for the stem */
1861 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1862 if (!ap)
1863 continue;
0640332e 1864 memset(ap, 0, sizeof(*ap) + len + 1);
611cad72
SG
1865 ap->alias = start;
1866 of_alias_add(ap, np, id, start, len);
1867 }
1868}
1869
1870/**
1871 * of_alias_get_id - Get alias id for the given device_node
1872 * @np: Pointer to the given device_node
1873 * @stem: Alias stem of the given device_node
1874 *
1875 * The function travels the lookup table to get alias id for the given
1876 * device_node and alias stem. It returns the alias id if find it.
1877 */
1878int of_alias_get_id(struct device_node *np, const char *stem)
1879{
1880 struct alias_prop *app;
1881 int id = -ENODEV;
1882
1883 mutex_lock(&of_aliases_mutex);
1884 list_for_each_entry(app, &aliases_lookup, link) {
1885 if (strcmp(app->stem, stem) != 0)
1886 continue;
1887
1888 if (np == app->np) {
1889 id = app->id;
1890 break;
1891 }
1892 }
1893 mutex_unlock(&of_aliases_mutex);
1894
1895 return id;
1896}
1897EXPORT_SYMBOL_GPL(of_alias_get_id);
c541adc6
SW
1898
1899const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1900 u32 *pu)
1901{
1902 const void *curv = cur;
1903
1904 if (!prop)
1905 return NULL;
1906
1907 if (!cur) {
1908 curv = prop->value;
1909 goto out_val;
1910 }
1911
1912 curv += sizeof(*cur);
1913 if (curv >= prop->value + prop->length)
1914 return NULL;
1915
1916out_val:
1917 *pu = be32_to_cpup(curv);
1918 return curv;
1919}
1920EXPORT_SYMBOL_GPL(of_prop_next_u32);
1921
1922const char *of_prop_next_string(struct property *prop, const char *cur)
1923{
1924 const void *curv = cur;
1925
1926 if (!prop)
1927 return NULL;
1928
1929 if (!cur)
1930 return prop->value;
1931
1932 curv += strlen(cur) + 1;
1933 if (curv >= prop->value + prop->length)
1934 return NULL;
1935
1936 return curv;
1937}
1938EXPORT_SYMBOL_GPL(of_prop_next_string);
5c19e952
SH
1939
1940/**
1941 * of_device_is_stdout_path - check if a device node matches the
1942 * linux,stdout-path property
1943 *
1944 * Check if this device node matches the linux,stdout-path property
1945 * in the chosen node. return true if yes, false otherwise.
1946 */
1947int of_device_is_stdout_path(struct device_node *dn)
1948{
1949 if (!of_stdout)
1950 return false;
1951
1952 return of_stdout == dn;
1953}
1954EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
a3e31b45
SH
1955
1956/**
1957 * of_find_next_cache_node - Find a node's subsidiary cache
1958 * @np: node of type "cpu" or "cache"
1959 *
1960 * Returns a node pointer with refcount incremented, use
1961 * of_node_put() on it when done. Caller should hold a reference
1962 * to np.
1963 */
1964struct device_node *of_find_next_cache_node(const struct device_node *np)
1965{
1966 struct device_node *child;
1967 const phandle *handle;
1968
1969 handle = of_get_property(np, "l2-cache", NULL);
1970 if (!handle)
1971 handle = of_get_property(np, "next-level-cache", NULL);
1972
1973 if (handle)
1974 return of_find_node_by_phandle(be32_to_cpup(handle));
1975
1976 /* OF on pmac has nodes instead of properties named "l2-cache"
1977 * beneath CPU nodes.
1978 */
1979 if (!strcmp(np->type, "cpu"))
1980 for_each_child_of_node(np, child)
1981 if (!strcmp(child->type, "cache"))
1982 return child;
1983
1984 return NULL;
1985}
fd9fdb78 1986
f2a575f6
PZ
1987/**
1988 * of_graph_parse_endpoint() - parse common endpoint node properties
1989 * @node: pointer to endpoint device_node
1990 * @endpoint: pointer to the OF endpoint data structure
1991 *
1992 * The caller should hold a reference to @node.
1993 */
1994int of_graph_parse_endpoint(const struct device_node *node,
1995 struct of_endpoint *endpoint)
1996{
1997 struct device_node *port_node = of_get_parent(node);
1998
1999 memset(endpoint, 0, sizeof(*endpoint));
2000
2001 endpoint->local_node = node;
2002 /*
2003 * It doesn't matter whether the two calls below succeed.
2004 * If they don't then the default value 0 is used.
2005 */
2006 of_property_read_u32(port_node, "reg", &endpoint->port);
2007 of_property_read_u32(node, "reg", &endpoint->id);
2008
2009 of_node_put(port_node);
2010
2011 return 0;
2012}
2013EXPORT_SYMBOL(of_graph_parse_endpoint);
2014
fd9fdb78
PZ
2015/**
2016 * of_graph_get_next_endpoint() - get next endpoint node
2017 * @parent: pointer to the parent device node
2018 * @prev: previous endpoint node, or NULL to get first
2019 *
2020 * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2021 * of the passed @prev node is not decremented, the caller have to use
2022 * of_node_put() on it when done.
2023 */
2024struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2025 struct device_node *prev)
2026{
2027 struct device_node *endpoint;
2028 struct device_node *port = NULL;
2029
2030 if (!parent)
2031 return NULL;
2032
2033 if (!prev) {
2034 struct device_node *node;
2035 /*
2036 * It's the first call, we have to find a port subnode
2037 * within this node or within an optional 'ports' node.
2038 */
2039 node = of_get_child_by_name(parent, "ports");
2040 if (node)
2041 parent = node;
2042
2043 port = of_get_child_by_name(parent, "port");
2044
2045 if (port) {
2046 /* Found a port, get an endpoint. */
2047 endpoint = of_get_next_child(port, NULL);
2048 of_node_put(port);
2049 } else {
2050 endpoint = NULL;
2051 }
2052
2053 if (!endpoint)
2054 pr_err("%s(): no endpoint nodes specified for %s\n",
2055 __func__, parent->full_name);
2056 of_node_put(node);
fd9fdb78 2057
4329b93b
PZ
2058 return endpoint;
2059 }
fd9fdb78 2060
4329b93b
PZ
2061 port = of_get_parent(prev);
2062 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2063 __func__, prev->full_name))
2064 return NULL;
fd9fdb78 2065
4329b93b
PZ
2066 /* Avoid dropping prev node refcount to 0. */
2067 of_node_get(prev);
2068 endpoint = of_get_next_child(port, prev);
2069 if (endpoint) {
fd9fdb78 2070 of_node_put(port);
4329b93b 2071 return endpoint;
fd9fdb78
PZ
2072 }
2073
4329b93b
PZ
2074 /* No more endpoints under this port, try the next one. */
2075 do {
2076 port = of_get_next_child(parent, port);
2077 if (!port)
2078 return NULL;
2079 } while (of_node_cmp(port->name, "port"));
2080
2081 /* Pick up the first endpoint in this port. */
2082 endpoint = of_get_next_child(port, NULL);
2083 of_node_put(port);
2084
fd9fdb78
PZ
2085 return endpoint;
2086}
2087EXPORT_SYMBOL(of_graph_get_next_endpoint);
2088
2089/**
2090 * of_graph_get_remote_port_parent() - get remote port's parent node
2091 * @node: pointer to a local endpoint device_node
2092 *
2093 * Return: Remote device node associated with remote endpoint node linked
2094 * to @node. Use of_node_put() on it when done.
2095 */
2096struct device_node *of_graph_get_remote_port_parent(
2097 const struct device_node *node)
2098{
2099 struct device_node *np;
2100 unsigned int depth;
2101
2102 /* Get remote endpoint node. */
2103 np = of_parse_phandle(node, "remote-endpoint", 0);
2104
2105 /* Walk 3 levels up only if there is 'ports' node. */
2106 for (depth = 3; depth && np; depth--) {
2107 np = of_get_next_parent(np);
2108 if (depth == 2 && of_node_cmp(np->name, "ports"))
2109 break;
2110 }
2111 return np;
2112}
2113EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2114
2115/**
2116 * of_graph_get_remote_port() - get remote port node
2117 * @node: pointer to a local endpoint device_node
2118 *
2119 * Return: Remote port node associated with remote endpoint node linked
2120 * to @node. Use of_node_put() on it when done.
2121 */
2122struct device_node *of_graph_get_remote_port(const struct device_node *node)
2123{
2124 struct device_node *np;
2125
2126 /* Get remote endpoint node. */
2127 np = of_parse_phandle(node, "remote-endpoint", 0);
2128 if (!np)
2129 return NULL;
2130 return of_get_next_parent(np);
2131}
2132EXPORT_SYMBOL(of_graph_get_remote_port);