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