[go: nahoru, domu]

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