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