usb: phy: rcar-gen2-usb: always use 'dev' variable in probe() method
[platform/adaptation/renesas_rcar/renesas_kernel.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_match_string() - Find string in a list and return index
1154  * @np: pointer to node containing string list property
1155  * @propname: string list property name
1156  * @string: pointer to string to search for in string list
1157  *
1158  * This function searches a string list property and returns the index
1159  * of a specific string value.
1160  */
1161 int of_property_match_string(struct device_node *np, const char *propname,
1162                              const char *string)
1163 {
1164         struct property *prop = of_find_property(np, propname, NULL);
1165         size_t l;
1166         int i;
1167         const char *p, *end;
1168
1169         if (!prop)
1170                 return -EINVAL;
1171         if (!prop->value)
1172                 return -ENODATA;
1173
1174         p = prop->value;
1175         end = p + prop->length;
1176
1177         for (i = 0; p < end; i++, p += l) {
1178                 l = strnlen(p, end - p) + 1;
1179                 if (p + l > end)
1180                         return -EILSEQ;
1181                 pr_debug("comparing %s with %s\n", string, p);
1182                 if (strcmp(string, p) == 0)
1183                         return i; /* Found it; return index */
1184         }
1185         return -ENODATA;
1186 }
1187 EXPORT_SYMBOL_GPL(of_property_match_string);
1188
1189 /**
1190  * of_property_read_string_util() - Utility helper for parsing string properties
1191  * @np:         device node from which the property value is to be read.
1192  * @propname:   name of the property to be searched.
1193  * @out_strs:   output array of string pointers.
1194  * @sz:         number of array elements to read.
1195  * @skip:       Number of strings to skip over at beginning of list.
1196  *
1197  * Don't call this function directly. It is a utility helper for the
1198  * of_property_read_string*() family of functions.
1199  */
1200 int of_property_read_string_helper(struct device_node *np, const char *propname,
1201                                    const char **out_strs, size_t sz, int skip)
1202 {
1203         struct property *prop = of_find_property(np, propname, NULL);
1204         int l = 0, i = 0;
1205         const char *p, *end;
1206
1207         if (!prop)
1208                 return -EINVAL;
1209         if (!prop->value)
1210                 return -ENODATA;
1211         p = prop->value;
1212         end = p + prop->length;
1213
1214         for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
1215                 l = strnlen(p, end - p) + 1;
1216                 if (p + l > end)
1217                         return -EILSEQ;
1218                 if (out_strs && i >= skip)
1219                         *out_strs++ = p;
1220         }
1221         i -= skip;
1222         return i <= 0 ? -ENODATA : i;
1223 }
1224 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
1225
1226 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1227 {
1228         int i;
1229         printk("%s %s", msg, of_node_full_name(args->np));
1230         for (i = 0; i < args->args_count; i++)
1231                 printk(i ? ",%08x" : ":%08x", args->args[i]);
1232         printk("\n");
1233 }
1234
1235 static int __of_parse_phandle_with_args(const struct device_node *np,
1236                                         const char *list_name,
1237                                         const char *cells_name,
1238                                         int cell_count, int index,
1239                                         struct of_phandle_args *out_args)
1240 {
1241         const __be32 *list, *list_end;
1242         int rc = 0, size, cur_index = 0;
1243         uint32_t count = 0;
1244         struct device_node *node = NULL;
1245         phandle phandle;
1246
1247         /* Retrieve the phandle list property */
1248         list = of_get_property(np, list_name, &size);
1249         if (!list)
1250                 return -ENOENT;
1251         list_end = list + size / sizeof(*list);
1252
1253         /* Loop over the phandles until all the requested entry is found */
1254         while (list < list_end) {
1255                 rc = -EINVAL;
1256                 count = 0;
1257
1258                 /*
1259                  * If phandle is 0, then it is an empty entry with no
1260                  * arguments.  Skip forward to the next entry.
1261                  */
1262                 phandle = be32_to_cpup(list++);
1263                 if (phandle) {
1264                         /*
1265                          * Find the provider node and parse the #*-cells
1266                          * property to determine the argument length.
1267                          *
1268                          * This is not needed if the cell count is hard-coded
1269                          * (i.e. cells_name not set, but cell_count is set),
1270                          * except when we're going to return the found node
1271                          * below.
1272                          */
1273                         if (cells_name || cur_index == index) {
1274                                 node = of_find_node_by_phandle(phandle);
1275                                 if (!node) {
1276                                         pr_err("%s: could not find phandle\n",
1277                                                 np->full_name);
1278                                         goto err;
1279                                 }
1280                         }
1281
1282                         if (cells_name) {
1283                                 if (of_property_read_u32(node, cells_name,
1284                                                          &count)) {
1285                                         pr_err("%s: could not get %s for %s\n",
1286                                                 np->full_name, cells_name,
1287                                                 node->full_name);
1288                                         goto err;
1289                                 }
1290                         } else {
1291                                 count = cell_count;
1292                         }
1293
1294                         /*
1295                          * Make sure that the arguments actually fit in the
1296                          * remaining property data length
1297                          */
1298                         if (list + count > list_end) {
1299                                 pr_err("%s: arguments longer than property\n",
1300                                          np->full_name);
1301                                 goto err;
1302                         }
1303                 }
1304
1305                 /*
1306                  * All of the error cases above bail out of the loop, so at
1307                  * this point, the parsing is successful. If the requested
1308                  * index matches, then fill the out_args structure and return,
1309                  * or return -ENOENT for an empty entry.
1310                  */
1311                 rc = -ENOENT;
1312                 if (cur_index == index) {
1313                         if (!phandle)
1314                                 goto err;
1315
1316                         if (out_args) {
1317                                 int i;
1318                                 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1319                                         count = MAX_PHANDLE_ARGS;
1320                                 out_args->np = node;
1321                                 out_args->args_count = count;
1322                                 for (i = 0; i < count; i++)
1323                                         out_args->args[i] = be32_to_cpup(list++);
1324                         } else {
1325                                 of_node_put(node);
1326                         }
1327
1328                         /* Found it! return success */
1329                         return 0;
1330                 }
1331
1332                 of_node_put(node);
1333                 node = NULL;
1334                 list += count;
1335                 cur_index++;
1336         }
1337
1338         /*
1339          * Unlock node before returning result; will be one of:
1340          * -ENOENT : index is for empty phandle
1341          * -EINVAL : parsing error on data
1342          * [1..n]  : Number of phandle (count mode; when index = -1)
1343          */
1344         rc = index < 0 ? cur_index : -ENOENT;
1345  err:
1346         if (node)
1347                 of_node_put(node);
1348         return rc;
1349 }
1350
1351 /**
1352  * of_parse_phandle - Resolve a phandle property to a device_node pointer
1353  * @np: Pointer to device node holding phandle property
1354  * @phandle_name: Name of property holding a phandle value
1355  * @index: For properties holding a table of phandles, this is the index into
1356  *         the table
1357  *
1358  * Returns the device_node pointer with refcount incremented.  Use
1359  * of_node_put() on it when done.
1360  */
1361 struct device_node *of_parse_phandle(const struct device_node *np,
1362                                      const char *phandle_name, int index)
1363 {
1364         struct of_phandle_args args;
1365
1366         if (index < 0)
1367                 return NULL;
1368
1369         if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1370                                          index, &args))
1371                 return NULL;
1372
1373         return args.np;
1374 }
1375 EXPORT_SYMBOL(of_parse_phandle);
1376
1377 /**
1378  * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1379  * @np:         pointer to a device tree node containing a list
1380  * @list_name:  property name that contains a list
1381  * @cells_name: property name that specifies phandles' arguments count
1382  * @index:      index of a phandle to parse out
1383  * @out_args:   optional pointer to output arguments structure (will be filled)
1384  *
1385  * This function is useful to parse lists of phandles and their arguments.
1386  * Returns 0 on success and fills out_args, on error returns appropriate
1387  * errno value.
1388  *
1389  * Caller is responsible to call of_node_put() on the returned out_args->node
1390  * pointer.
1391  *
1392  * Example:
1393  *
1394  * phandle1: node1 {
1395  *      #list-cells = <2>;
1396  * }
1397  *
1398  * phandle2: node2 {
1399  *      #list-cells = <1>;
1400  * }
1401  *
1402  * node3 {
1403  *      list = <&phandle1 1 2 &phandle2 3>;
1404  * }
1405  *
1406  * To get a device_node of the `node2' node you may call this:
1407  * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1408  */
1409 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1410                                 const char *cells_name, int index,
1411                                 struct of_phandle_args *out_args)
1412 {
1413         if (index < 0)
1414                 return -EINVAL;
1415         return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1416                                             index, out_args);
1417 }
1418 EXPORT_SYMBOL(of_parse_phandle_with_args);
1419
1420 /**
1421  * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1422  * @np:         pointer to a device tree node containing a list
1423  * @list_name:  property name that contains a list
1424  * @cell_count: number of argument cells following the phandle
1425  * @index:      index of a phandle to parse out
1426  * @out_args:   optional pointer to output arguments structure (will be filled)
1427  *
1428  * This function is useful to parse lists of phandles and their arguments.
1429  * Returns 0 on success and fills out_args, on error returns appropriate
1430  * errno value.
1431  *
1432  * Caller is responsible to call of_node_put() on the returned out_args->node
1433  * pointer.
1434  *
1435  * Example:
1436  *
1437  * phandle1: node1 {
1438  * }
1439  *
1440  * phandle2: node2 {
1441  * }
1442  *
1443  * node3 {
1444  *      list = <&phandle1 0 2 &phandle2 2 3>;
1445  * }
1446  *
1447  * To get a device_node of the `node2' node you may call this:
1448  * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1449  */
1450 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1451                                 const char *list_name, int cell_count,
1452                                 int index, struct of_phandle_args *out_args)
1453 {
1454         if (index < 0)
1455                 return -EINVAL;
1456         return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1457                                            index, out_args);
1458 }
1459 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1460
1461 /**
1462  * of_count_phandle_with_args() - Find the number of phandles references in a property
1463  * @np:         pointer to a device tree node containing a list
1464  * @list_name:  property name that contains a list
1465  * @cells_name: property name that specifies phandles' arguments count
1466  *
1467  * Returns the number of phandle + argument tuples within a property. It
1468  * is a typical pattern to encode a list of phandle and variable
1469  * arguments into a single property. The number of arguments is encoded
1470  * by a property in the phandle-target node. For example, a gpios
1471  * property would contain a list of GPIO specifies consisting of a
1472  * phandle and 1 or more arguments. The number of arguments are
1473  * determined by the #gpio-cells property in the node pointed to by the
1474  * phandle.
1475  */
1476 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1477                                 const char *cells_name)
1478 {
1479         return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1480                                             NULL);
1481 }
1482 EXPORT_SYMBOL(of_count_phandle_with_args);
1483
1484 #if defined(CONFIG_OF_DYNAMIC)
1485 static int of_property_notify(int action, struct device_node *np,
1486                               struct property *prop)
1487 {
1488         struct of_prop_reconfig pr;
1489
1490         pr.dn = np;
1491         pr.prop = prop;
1492         return of_reconfig_notify(action, &pr);
1493 }
1494 #else
1495 static int of_property_notify(int action, struct device_node *np,
1496                               struct property *prop)
1497 {
1498         return 0;
1499 }
1500 #endif
1501
1502 /**
1503  * of_add_property - Add a property to a node
1504  */
1505 int of_add_property(struct device_node *np, struct property *prop)
1506 {
1507         struct property **next;
1508         unsigned long flags;
1509         int rc;
1510
1511         rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1512         if (rc)
1513                 return rc;
1514
1515         prop->next = NULL;
1516         raw_spin_lock_irqsave(&devtree_lock, flags);
1517         next = &np->properties;
1518         while (*next) {
1519                 if (strcmp(prop->name, (*next)->name) == 0) {
1520                         /* duplicate ! don't insert it */
1521                         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1522                         return -1;
1523                 }
1524                 next = &(*next)->next;
1525         }
1526         *next = prop;
1527         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1528
1529 #ifdef CONFIG_PROC_DEVICETREE
1530         /* try to add to proc as well if it was initialized */
1531         if (np->pde)
1532                 proc_device_tree_add_prop(np->pde, prop);
1533 #endif /* CONFIG_PROC_DEVICETREE */
1534
1535         return 0;
1536 }
1537
1538 /**
1539  * of_remove_property - Remove a property from a node.
1540  *
1541  * Note that we don't actually remove it, since we have given out
1542  * who-knows-how-many pointers to the data using get-property.
1543  * Instead we just move the property to the "dead properties"
1544  * list, so it won't be found any more.
1545  */
1546 int of_remove_property(struct device_node *np, struct property *prop)
1547 {
1548         struct property **next;
1549         unsigned long flags;
1550         int found = 0;
1551         int rc;
1552
1553         rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1554         if (rc)
1555                 return rc;
1556
1557         raw_spin_lock_irqsave(&devtree_lock, flags);
1558         next = &np->properties;
1559         while (*next) {
1560                 if (*next == prop) {
1561                         /* found the node */
1562                         *next = prop->next;
1563                         prop->next = np->deadprops;
1564                         np->deadprops = prop;
1565                         found = 1;
1566                         break;
1567                 }
1568                 next = &(*next)->next;
1569         }
1570         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1571
1572         if (!found)
1573                 return -ENODEV;
1574
1575 #ifdef CONFIG_PROC_DEVICETREE
1576         /* try to remove the proc node as well */
1577         if (np->pde)
1578                 proc_device_tree_remove_prop(np->pde, prop);
1579 #endif /* CONFIG_PROC_DEVICETREE */
1580
1581         return 0;
1582 }
1583
1584 /*
1585  * of_update_property - Update a property in a node, if the property does
1586  * not exist, add it.
1587  *
1588  * Note that we don't actually remove it, since we have given out
1589  * who-knows-how-many pointers to the data using get-property.
1590  * Instead we just move the property to the "dead properties" list,
1591  * and add the new property to the property list
1592  */
1593 int of_update_property(struct device_node *np, struct property *newprop)
1594 {
1595         struct property **next, *oldprop;
1596         unsigned long flags;
1597         int rc, found = 0;
1598
1599         rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1600         if (rc)
1601                 return rc;
1602
1603         if (!newprop->name)
1604                 return -EINVAL;
1605
1606         oldprop = of_find_property(np, newprop->name, NULL);
1607         if (!oldprop)
1608                 return of_add_property(np, newprop);
1609
1610         raw_spin_lock_irqsave(&devtree_lock, flags);
1611         next = &np->properties;
1612         while (*next) {
1613                 if (*next == oldprop) {
1614                         /* found the node */
1615                         newprop->next = oldprop->next;
1616                         *next = newprop;
1617                         oldprop->next = np->deadprops;
1618                         np->deadprops = oldprop;
1619                         found = 1;
1620                         break;
1621                 }
1622                 next = &(*next)->next;
1623         }
1624         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1625
1626         if (!found)
1627                 return -ENODEV;
1628
1629 #ifdef CONFIG_PROC_DEVICETREE
1630         /* try to add to proc as well if it was initialized */
1631         if (np->pde)
1632                 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1633 #endif /* CONFIG_PROC_DEVICETREE */
1634
1635         return 0;
1636 }
1637
1638 #if defined(CONFIG_OF_DYNAMIC)
1639 /*
1640  * Support for dynamic device trees.
1641  *
1642  * On some platforms, the device tree can be manipulated at runtime.
1643  * The routines in this section support adding, removing and changing
1644  * device tree nodes.
1645  */
1646
1647 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1648
1649 int of_reconfig_notifier_register(struct notifier_block *nb)
1650 {
1651         return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1652 }
1653 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
1654
1655 int of_reconfig_notifier_unregister(struct notifier_block *nb)
1656 {
1657         return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1658 }
1659 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
1660
1661 int of_reconfig_notify(unsigned long action, void *p)
1662 {
1663         int rc;
1664
1665         rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1666         return notifier_to_errno(rc);
1667 }
1668
1669 #ifdef CONFIG_PROC_DEVICETREE
1670 static void of_add_proc_dt_entry(struct device_node *dn)
1671 {
1672         struct proc_dir_entry *ent;
1673
1674         ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1675         if (ent)
1676                 proc_device_tree_add_node(dn, ent);
1677 }
1678 #else
1679 static void of_add_proc_dt_entry(struct device_node *dn)
1680 {
1681         return;
1682 }
1683 #endif
1684
1685 /**
1686  * of_attach_node - Plug a device node into the tree and global list.
1687  */
1688 int of_attach_node(struct device_node *np)
1689 {
1690         unsigned long flags;
1691         int rc;
1692
1693         rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1694         if (rc)
1695                 return rc;
1696
1697         raw_spin_lock_irqsave(&devtree_lock, flags);
1698         np->sibling = np->parent->child;
1699         np->allnext = of_allnodes;
1700         np->parent->child = np;
1701         of_allnodes = np;
1702         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1703
1704         of_add_proc_dt_entry(np);
1705         return 0;
1706 }
1707
1708 #ifdef CONFIG_PROC_DEVICETREE
1709 static void of_remove_proc_dt_entry(struct device_node *dn)
1710 {
1711         proc_remove(dn->pde);
1712 }
1713 #else
1714 static void of_remove_proc_dt_entry(struct device_node *dn)
1715 {
1716         return;
1717 }
1718 #endif
1719
1720 /**
1721  * of_detach_node - "Unplug" a node from the device tree.
1722  *
1723  * The caller must hold a reference to the node.  The memory associated with
1724  * the node is not freed until its refcount goes to zero.
1725  */
1726 int of_detach_node(struct device_node *np)
1727 {
1728         struct device_node *parent;
1729         unsigned long flags;
1730         int rc = 0;
1731
1732         rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1733         if (rc)
1734                 return rc;
1735
1736         raw_spin_lock_irqsave(&devtree_lock, flags);
1737
1738         if (of_node_check_flag(np, OF_DETACHED)) {
1739                 /* someone already detached it */
1740                 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1741                 return rc;
1742         }
1743
1744         parent = np->parent;
1745         if (!parent) {
1746                 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1747                 return rc;
1748         }
1749
1750         if (of_allnodes == np)
1751                 of_allnodes = np->allnext;
1752         else {
1753                 struct device_node *prev;
1754                 for (prev = of_allnodes;
1755                      prev->allnext != np;
1756                      prev = prev->allnext)
1757                         ;
1758                 prev->allnext = np->allnext;
1759         }
1760
1761         if (parent->child == np)
1762                 parent->child = np->sibling;
1763         else {
1764                 struct device_node *prevsib;
1765                 for (prevsib = np->parent->child;
1766                      prevsib->sibling != np;
1767                      prevsib = prevsib->sibling)
1768                         ;
1769                 prevsib->sibling = np->sibling;
1770         }
1771
1772         of_node_set_flag(np, OF_DETACHED);
1773         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1774
1775         of_remove_proc_dt_entry(np);
1776         return rc;
1777 }
1778 #endif /* defined(CONFIG_OF_DYNAMIC) */
1779
1780 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1781                          int id, const char *stem, int stem_len)
1782 {
1783         ap->np = np;
1784         ap->id = id;
1785         strncpy(ap->stem, stem, stem_len);
1786         ap->stem[stem_len] = 0;
1787         list_add_tail(&ap->link, &aliases_lookup);
1788         pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1789                  ap->alias, ap->stem, ap->id, of_node_full_name(np));
1790 }
1791
1792 /**
1793  * of_alias_scan - Scan all properties of 'aliases' node
1794  *
1795  * The function scans all the properties of 'aliases' node and populate
1796  * the the global lookup table with the properties.  It returns the
1797  * number of alias_prop found, or error code in error case.
1798  *
1799  * @dt_alloc:   An allocator that provides a virtual address to memory
1800  *              for the resulting tree
1801  */
1802 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1803 {
1804         struct property *pp;
1805
1806         of_chosen = of_find_node_by_path("/chosen");
1807         if (of_chosen == NULL)
1808                 of_chosen = of_find_node_by_path("/chosen@0");
1809
1810         if (of_chosen) {
1811                 const char *name;
1812
1813                 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1814                 if (name)
1815                         of_stdout = of_find_node_by_path(name);
1816         }
1817
1818         of_aliases = of_find_node_by_path("/aliases");
1819         if (!of_aliases)
1820                 return;
1821
1822         for_each_property_of_node(of_aliases, pp) {
1823                 const char *start = pp->name;
1824                 const char *end = start + strlen(start);
1825                 struct device_node *np;
1826                 struct alias_prop *ap;
1827                 int id, len;
1828
1829                 /* Skip those we do not want to proceed */
1830                 if (!strcmp(pp->name, "name") ||
1831                     !strcmp(pp->name, "phandle") ||
1832                     !strcmp(pp->name, "linux,phandle"))
1833                         continue;
1834
1835                 np = of_find_node_by_path(pp->value);
1836                 if (!np)
1837                         continue;
1838
1839                 /* walk the alias backwards to extract the id and work out
1840                  * the 'stem' string */
1841                 while (isdigit(*(end-1)) && end > start)
1842                         end--;
1843                 len = end - start;
1844
1845                 if (kstrtoint(end, 10, &id) < 0)
1846                         continue;
1847
1848                 /* Allocate an alias_prop with enough space for the stem */
1849                 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1850                 if (!ap)
1851                         continue;
1852                 memset(ap, 0, sizeof(*ap) + len + 1);
1853                 ap->alias = start;
1854                 of_alias_add(ap, np, id, start, len);
1855         }
1856 }
1857
1858 /**
1859  * of_alias_get_id - Get alias id for the given device_node
1860  * @np:         Pointer to the given device_node
1861  * @stem:       Alias stem of the given device_node
1862  *
1863  * The function travels the lookup table to get alias id for the given
1864  * device_node and alias stem.  It returns the alias id if find it.
1865  */
1866 int of_alias_get_id(struct device_node *np, const char *stem)
1867 {
1868         struct alias_prop *app;
1869         int id = -ENODEV;
1870
1871         mutex_lock(&of_aliases_mutex);
1872         list_for_each_entry(app, &aliases_lookup, link) {
1873                 if (strcmp(app->stem, stem) != 0)
1874                         continue;
1875
1876                 if (np == app->np) {
1877                         id = app->id;
1878                         break;
1879                 }
1880         }
1881         mutex_unlock(&of_aliases_mutex);
1882
1883         return id;
1884 }
1885 EXPORT_SYMBOL_GPL(of_alias_get_id);
1886
1887 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1888                                u32 *pu)
1889 {
1890         const void *curv = cur;
1891
1892         if (!prop)
1893                 return NULL;
1894
1895         if (!cur) {
1896                 curv = prop->value;
1897                 goto out_val;
1898         }
1899
1900         curv += sizeof(*cur);
1901         if (curv >= prop->value + prop->length)
1902                 return NULL;
1903
1904 out_val:
1905         *pu = be32_to_cpup(curv);
1906         return curv;
1907 }
1908 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1909
1910 const char *of_prop_next_string(struct property *prop, const char *cur)
1911 {
1912         const void *curv = cur;
1913
1914         if (!prop)
1915                 return NULL;
1916
1917         if (!cur)
1918                 return prop->value;
1919
1920         curv += strlen(cur) + 1;
1921         if (curv >= prop->value + prop->length)
1922                 return NULL;
1923
1924         return curv;
1925 }
1926 EXPORT_SYMBOL_GPL(of_prop_next_string);
1927
1928 /**
1929  * of_device_is_stdout_path - check if a device node matches the
1930  *                            linux,stdout-path property
1931  *
1932  * Check if this device node matches the linux,stdout-path property
1933  * in the chosen node. return true if yes, false otherwise.
1934  */
1935 int of_device_is_stdout_path(struct device_node *dn)
1936 {
1937         if (!of_stdout)
1938                 return false;
1939
1940         return of_stdout == dn;
1941 }
1942 EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
1943
1944 /**
1945  *      of_find_next_cache_node - Find a node's subsidiary cache
1946  *      @np:    node of type "cpu" or "cache"
1947  *
1948  *      Returns a node pointer with refcount incremented, use
1949  *      of_node_put() on it when done.  Caller should hold a reference
1950  *      to np.
1951  */
1952 struct device_node *of_find_next_cache_node(const struct device_node *np)
1953 {
1954         struct device_node *child;
1955         const phandle *handle;
1956
1957         handle = of_get_property(np, "l2-cache", NULL);
1958         if (!handle)
1959                 handle = of_get_property(np, "next-level-cache", NULL);
1960
1961         if (handle)
1962                 return of_find_node_by_phandle(be32_to_cpup(handle));
1963
1964         /* OF on pmac has nodes instead of properties named "l2-cache"
1965          * beneath CPU nodes.
1966          */
1967         if (!strcmp(np->type, "cpu"))
1968                 for_each_child_of_node(np, child)
1969                         if (!strcmp(child->type, "cache"))
1970                                 return child;
1971
1972         return NULL;
1973 }
1974
1975 /**
1976  * of_graph_get_next_endpoint() - get next endpoint node
1977  * @parent: pointer to the parent device node
1978  * @prev: previous endpoint node, or NULL to get first
1979  *
1980  * Return: An 'endpoint' node pointer with refcount incremented. Refcount
1981  * of the passed @prev node is not decremented, the caller have to use
1982  * of_node_put() on it when done.
1983  */
1984 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
1985                                         struct device_node *prev)
1986 {
1987         struct device_node *endpoint;
1988         struct device_node *port = NULL;
1989
1990         if (!parent)
1991                 return NULL;
1992
1993         if (!prev) {
1994                 struct device_node *node;
1995                 /*
1996                  * It's the first call, we have to find a port subnode
1997                  * within this node or within an optional 'ports' node.
1998                  */
1999                 node = of_get_child_by_name(parent, "ports");
2000                 if (node)
2001                         parent = node;
2002
2003                 port = of_get_child_by_name(parent, "port");
2004
2005                 if (port) {
2006                         /* Found a port, get an endpoint. */
2007                         endpoint = of_get_next_child(port, NULL);
2008                         of_node_put(port);
2009                 } else {
2010                         endpoint = NULL;
2011                 }
2012
2013                 if (!endpoint)
2014                         pr_err("%s(): no endpoint nodes specified for %s\n",
2015                                __func__, parent->full_name);
2016                 of_node_put(node);
2017         } else {
2018                 port = of_get_parent(prev);
2019                 if (!port)
2020                         /* Hm, has someone given us the root node ?... */
2021                         return NULL;
2022
2023                 /* Avoid dropping prev node refcount to 0. */
2024                 of_node_get(prev);
2025                 endpoint = of_get_next_child(port, prev);
2026                 if (endpoint) {
2027                         of_node_put(port);
2028                         return endpoint;
2029                 }
2030
2031                 /* No more endpoints under this port, try the next one. */
2032                 do {
2033                         port = of_get_next_child(parent, port);
2034                         if (!port)
2035                                 return NULL;
2036                 } while (of_node_cmp(port->name, "port"));
2037
2038                 /* Pick up the first endpoint in this port. */
2039                 endpoint = of_get_next_child(port, NULL);
2040                 of_node_put(port);
2041         }
2042
2043         return endpoint;
2044 }
2045 EXPORT_SYMBOL(of_graph_get_next_endpoint);
2046
2047 /**
2048  * of_graph_get_remote_port_parent() - get remote port's parent node
2049  * @node: pointer to a local endpoint device_node
2050  *
2051  * Return: Remote device node associated with remote endpoint node linked
2052  *         to @node. Use of_node_put() on it when done.
2053  */
2054 struct device_node *of_graph_get_remote_port_parent(
2055                                const struct device_node *node)
2056 {
2057         struct device_node *np;
2058         unsigned int depth;
2059
2060         /* Get remote endpoint node. */
2061         np = of_parse_phandle(node, "remote-endpoint", 0);
2062
2063         /* Walk 3 levels up only if there is 'ports' node. */
2064         for (depth = 3; depth && np; depth--) {
2065                 np = of_get_next_parent(np);
2066                 if (depth == 2 && of_node_cmp(np->name, "ports"))
2067                         break;
2068         }
2069         return np;
2070 }
2071 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2072
2073 /**
2074  * of_graph_get_remote_port() - get remote port node
2075  * @node: pointer to a local endpoint device_node
2076  *
2077  * Return: Remote port node associated with remote endpoint node linked
2078  *         to @node. Use of_node_put() on it when done.
2079  */
2080 struct device_node *of_graph_get_remote_port(const struct device_node *node)
2081 {
2082         struct device_node *np;
2083
2084         /* Get remote endpoint node. */
2085         np = of_parse_phandle(node, "remote-endpoint", 0);
2086         if (!np)
2087                 return NULL;
2088         return of_get_next_parent(np);
2089 }
2090 EXPORT_SYMBOL(of_graph_get_remote_port);