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