xref: /openbmc/linux/arch/sparc/kernel/prom_common.c (revision e5ff0fe31d69e716f2599bcfb297ca3757e957c5)
1dfa76060SDavid S. Miller /* prom_common.c: OF device tree support common code.
2dfa76060SDavid S. Miller  *
3dfa76060SDavid S. Miller  * Paul Mackerras	August 1996.
4dfa76060SDavid S. Miller  * Copyright (C) 1996-2005 Paul Mackerras.
5dfa76060SDavid S. Miller  *
6dfa76060SDavid S. Miller  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
7dfa76060SDavid S. Miller  *    {engebret|bergner}@us.ibm.com
8dfa76060SDavid S. Miller  *
9dfa76060SDavid S. Miller  *  Adapted for sparc by David S. Miller davem@davemloft.net
10dfa76060SDavid S. Miller  *
11dfa76060SDavid S. Miller  *      This program is free software; you can redistribute it and/or
12dfa76060SDavid S. Miller  *      modify it under the terms of the GNU General Public License
13dfa76060SDavid S. Miller  *      as published by the Free Software Foundation; either version
14dfa76060SDavid S. Miller  *      2 of the License, or (at your option) any later version.
15dfa76060SDavid S. Miller  */
16dfa76060SDavid S. Miller 
17dfa76060SDavid S. Miller #include <linux/kernel.h>
18dfa76060SDavid S. Miller #include <linux/module.h>
19dfa76060SDavid S. Miller #include <linux/errno.h>
20dfa76060SDavid S. Miller #include <linux/mutex.h>
21dfa76060SDavid S. Miller #include <linux/slab.h>
22dfa76060SDavid S. Miller #include <linux/of.h>
23dfa76060SDavid S. Miller #include <asm/prom.h>
24dfa76060SDavid S. Miller #include <asm/oplib.h>
25dfa76060SDavid S. Miller 
26dfa76060SDavid S. Miller #include "prom.h"
27dfa76060SDavid S. Miller 
28dfa76060SDavid S. Miller struct device_node *of_find_node_by_phandle(phandle handle)
29dfa76060SDavid S. Miller {
30dfa76060SDavid S. Miller 	struct device_node *np;
31dfa76060SDavid S. Miller 
32dfa76060SDavid S. Miller 	for (np = allnodes; np; np = np->allnext)
33dfa76060SDavid S. Miller 		if (np->node == handle)
34dfa76060SDavid S. Miller 			break;
35dfa76060SDavid S. Miller 
36dfa76060SDavid S. Miller 	return np;
37dfa76060SDavid S. Miller }
38dfa76060SDavid S. Miller EXPORT_SYMBOL(of_find_node_by_phandle);
39dfa76060SDavid S. Miller 
40dfa76060SDavid S. Miller int of_getintprop_default(struct device_node *np, const char *name, int def)
41dfa76060SDavid S. Miller {
42dfa76060SDavid S. Miller 	struct property *prop;
43dfa76060SDavid S. Miller 	int len;
44dfa76060SDavid S. Miller 
45dfa76060SDavid S. Miller 	prop = of_find_property(np, name, &len);
46dfa76060SDavid S. Miller 	if (!prop || len != 4)
47dfa76060SDavid S. Miller 		return def;
48dfa76060SDavid S. Miller 
49dfa76060SDavid S. Miller 	return *(int *) prop->value;
50dfa76060SDavid S. Miller }
51dfa76060SDavid S. Miller EXPORT_SYMBOL(of_getintprop_default);
52dfa76060SDavid S. Miller 
53dfa76060SDavid S. Miller DEFINE_MUTEX(of_set_property_mutex);
54dfa76060SDavid S. Miller EXPORT_SYMBOL(of_set_property_mutex);
55dfa76060SDavid S. Miller 
56dfa76060SDavid S. Miller int of_set_property(struct device_node *dp, const char *name, void *val, int len)
57dfa76060SDavid S. Miller {
58dfa76060SDavid S. Miller 	struct property **prevp;
59dfa76060SDavid S. Miller 	void *new_val;
60dfa76060SDavid S. Miller 	int err;
61dfa76060SDavid S. Miller 
62dfa76060SDavid S. Miller 	new_val = kmalloc(len, GFP_KERNEL);
63dfa76060SDavid S. Miller 	if (!new_val)
64dfa76060SDavid S. Miller 		return -ENOMEM;
65dfa76060SDavid S. Miller 
66dfa76060SDavid S. Miller 	memcpy(new_val, val, len);
67dfa76060SDavid S. Miller 
68dfa76060SDavid S. Miller 	err = -ENODEV;
69dfa76060SDavid S. Miller 
70dfa76060SDavid S. Miller 	write_lock(&devtree_lock);
71dfa76060SDavid S. Miller 	prevp = &dp->properties;
72dfa76060SDavid S. Miller 	while (*prevp) {
73dfa76060SDavid S. Miller 		struct property *prop = *prevp;
74dfa76060SDavid S. Miller 
75dfa76060SDavid S. Miller 		if (!strcasecmp(prop->name, name)) {
76dfa76060SDavid S. Miller 			void *old_val = prop->value;
77dfa76060SDavid S. Miller 			int ret;
78dfa76060SDavid S. Miller 
79dfa76060SDavid S. Miller 			mutex_lock(&of_set_property_mutex);
80dfa76060SDavid S. Miller 			ret = prom_setprop(dp->node, name, val, len);
81dfa76060SDavid S. Miller 			mutex_unlock(&of_set_property_mutex);
82dfa76060SDavid S. Miller 
83dfa76060SDavid S. Miller 			err = -EINVAL;
84dfa76060SDavid S. Miller 			if (ret >= 0) {
85dfa76060SDavid S. Miller 				prop->value = new_val;
86dfa76060SDavid S. Miller 				prop->length = len;
87dfa76060SDavid S. Miller 
88dfa76060SDavid S. Miller 				if (OF_IS_DYNAMIC(prop))
89dfa76060SDavid S. Miller 					kfree(old_val);
90dfa76060SDavid S. Miller 
91dfa76060SDavid S. Miller 				OF_MARK_DYNAMIC(prop);
92dfa76060SDavid S. Miller 
93dfa76060SDavid S. Miller 				err = 0;
94dfa76060SDavid S. Miller 			}
95dfa76060SDavid S. Miller 			break;
96dfa76060SDavid S. Miller 		}
97dfa76060SDavid S. Miller 		prevp = &(*prevp)->next;
98dfa76060SDavid S. Miller 	}
99dfa76060SDavid S. Miller 	write_unlock(&devtree_lock);
100dfa76060SDavid S. Miller 
101dfa76060SDavid S. Miller 	/* XXX Upate procfs if necessary... */
102dfa76060SDavid S. Miller 
103dfa76060SDavid S. Miller 	return err;
104dfa76060SDavid S. Miller }
105dfa76060SDavid S. Miller EXPORT_SYMBOL(of_set_property);
106dfa76060SDavid S. Miller 
107dfa76060SDavid S. Miller int of_find_in_proplist(const char *list, const char *match, int len)
108dfa76060SDavid S. Miller {
109dfa76060SDavid S. Miller 	while (len > 0) {
110dfa76060SDavid S. Miller 		int l;
111dfa76060SDavid S. Miller 
112dfa76060SDavid S. Miller 		if (!strcmp(list, match))
113dfa76060SDavid S. Miller 			return 1;
114dfa76060SDavid S. Miller 		l = strlen(list) + 1;
115dfa76060SDavid S. Miller 		list += l;
116dfa76060SDavid S. Miller 		len -= l;
117dfa76060SDavid S. Miller 	}
118dfa76060SDavid S. Miller 	return 0;
119dfa76060SDavid S. Miller }
120dfa76060SDavid S. Miller EXPORT_SYMBOL(of_find_in_proplist);
121dfa76060SDavid S. Miller 
122*e5ff0fe3SDavid S. Miller unsigned int prom_unique_id;
123