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