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