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