xref: /openbmc/linux/arch/sparc/kernel/prom_common.c (revision 3cfc535c5df8122af1258ae05aaf2770c033425d)
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>
23*3cfc535cSAndres Salomon #include <linux/of_pdt.h>
24dfa76060SDavid S. Miller #include <asm/prom.h>
25dfa76060SDavid S. Miller #include <asm/oplib.h>
26e63829deSKonrad Eisele #include <asm/leon.h>
27dfa76060SDavid S. Miller 
28dfa76060SDavid S. Miller #include "prom.h"
29dfa76060SDavid S. Miller 
30ad07aed8SDavid S. Miller struct device_node *of_console_device;
31ad07aed8SDavid S. Miller EXPORT_SYMBOL(of_console_device);
32ad07aed8SDavid S. Miller 
33ad07aed8SDavid S. Miller char *of_console_path;
34ad07aed8SDavid S. Miller EXPORT_SYMBOL(of_console_path);
35ad07aed8SDavid S. Miller 
36ad07aed8SDavid S. Miller char *of_console_options;
37ad07aed8SDavid S. Miller EXPORT_SYMBOL(of_console_options);
38ad07aed8SDavid S. Miller 
39dfa76060SDavid S. Miller int of_getintprop_default(struct device_node *np, const char *name, int def)
40dfa76060SDavid S. Miller {
41dfa76060SDavid S. Miller 	struct property *prop;
42dfa76060SDavid S. Miller 	int len;
43dfa76060SDavid S. Miller 
44dfa76060SDavid S. Miller 	prop = of_find_property(np, name, &len);
45dfa76060SDavid S. Miller 	if (!prop || len != 4)
46dfa76060SDavid S. Miller 		return def;
47dfa76060SDavid S. Miller 
48dfa76060SDavid S. Miller 	return *(int *) prop->value;
49dfa76060SDavid S. Miller }
50dfa76060SDavid S. Miller EXPORT_SYMBOL(of_getintprop_default);
51dfa76060SDavid S. Miller 
52dfa76060SDavid S. Miller DEFINE_MUTEX(of_set_property_mutex);
53dfa76060SDavid S. Miller EXPORT_SYMBOL(of_set_property_mutex);
54dfa76060SDavid S. Miller 
55dfa76060SDavid S. Miller int of_set_property(struct device_node *dp, const char *name, void *val, int len)
56dfa76060SDavid S. Miller {
57dfa76060SDavid S. Miller 	struct property **prevp;
58dfa76060SDavid S. Miller 	void *new_val;
59dfa76060SDavid S. Miller 	int err;
60dfa76060SDavid S. Miller 
61dfa76060SDavid S. Miller 	new_val = kmalloc(len, GFP_KERNEL);
62dfa76060SDavid S. Miller 	if (!new_val)
63dfa76060SDavid S. Miller 		return -ENOMEM;
64dfa76060SDavid S. Miller 
65dfa76060SDavid S. Miller 	memcpy(new_val, val, len);
66dfa76060SDavid S. Miller 
67dfa76060SDavid S. Miller 	err = -ENODEV;
68dfa76060SDavid S. Miller 
691c9d80ddSDavid S. Miller 	mutex_lock(&of_set_property_mutex);
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 
796016a363SGrant Likely 			ret = prom_setprop(dp->phandle, name, val, len);
80dfa76060SDavid S. Miller 
81dfa76060SDavid S. Miller 			err = -EINVAL;
82dfa76060SDavid S. Miller 			if (ret >= 0) {
83dfa76060SDavid S. Miller 				prop->value = new_val;
84dfa76060SDavid S. Miller 				prop->length = len;
85dfa76060SDavid S. Miller 
86dfa76060SDavid S. Miller 				if (OF_IS_DYNAMIC(prop))
87dfa76060SDavid S. Miller 					kfree(old_val);
88dfa76060SDavid S. Miller 
89dfa76060SDavid S. Miller 				OF_MARK_DYNAMIC(prop);
90dfa76060SDavid S. Miller 
91dfa76060SDavid S. Miller 				err = 0;
92dfa76060SDavid S. Miller 			}
93dfa76060SDavid S. Miller 			break;
94dfa76060SDavid S. Miller 		}
95dfa76060SDavid S. Miller 		prevp = &(*prevp)->next;
96dfa76060SDavid S. Miller 	}
97dfa76060SDavid S. Miller 	write_unlock(&devtree_lock);
981c9d80ddSDavid S. Miller 	mutex_unlock(&of_set_property_mutex);
99dfa76060SDavid S. Miller 
100dfa76060SDavid S. Miller 	/* XXX Upate procfs if necessary... */
101dfa76060SDavid S. Miller 
102dfa76060SDavid S. Miller 	return err;
103dfa76060SDavid S. Miller }
104dfa76060SDavid S. Miller EXPORT_SYMBOL(of_set_property);
105dfa76060SDavid S. Miller 
106dfa76060SDavid S. Miller int of_find_in_proplist(const char *list, const char *match, int len)
107dfa76060SDavid S. Miller {
108dfa76060SDavid S. Miller 	while (len > 0) {
109dfa76060SDavid S. Miller 		int l;
110dfa76060SDavid S. Miller 
111dfa76060SDavid S. Miller 		if (!strcmp(list, match))
112dfa76060SDavid S. Miller 			return 1;
113dfa76060SDavid S. Miller 		l = strlen(list) + 1;
114dfa76060SDavid S. Miller 		list += l;
115dfa76060SDavid S. Miller 		len -= l;
116dfa76060SDavid S. Miller 	}
117dfa76060SDavid S. Miller 	return 0;
118dfa76060SDavid S. Miller }
119dfa76060SDavid S. Miller EXPORT_SYMBOL(of_find_in_proplist);
120dfa76060SDavid S. Miller 
12123dc758eSDavid S. Miller unsigned int prom_early_allocated __initdata;
12223dc758eSDavid S. Miller 
123*3cfc535cSAndres Salomon void __init prom_build_devicetree(void)
124*3cfc535cSAndres Salomon {
125*3cfc535cSAndres Salomon 	of_pdt_build_devicetree(prom_root_node);
126*3cfc535cSAndres Salomon 	of_console_init();
127*3cfc535cSAndres Salomon 
128*3cfc535cSAndres Salomon 	pr_info("PROM: Built device tree with %u bytes of memory.\n",
129*3cfc535cSAndres Salomon 			prom_early_allocated);
130*3cfc535cSAndres Salomon }
131