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