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