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