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