xref: /openbmc/linux/arch/sparc/kernel/prom_common.c (revision f0a4cf31017ac8179a2fe295c1bd8e821d7674f4)
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>
18066bcacaSPaul Gortmaker #include <linux/export.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>
233cfc535cSAndres 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 
61*f0a4cf31SThomas Meyer 	new_val = kmemdup(val, len, GFP_KERNEL);
62dfa76060SDavid S. Miller 	if (!new_val)
63dfa76060SDavid S. Miller 		return -ENOMEM;
64dfa76060SDavid S. Miller 
65dfa76060SDavid S. Miller 	err = -ENODEV;
66dfa76060SDavid S. Miller 
671c9d80ddSDavid S. Miller 	mutex_lock(&of_set_property_mutex);
68dfa76060SDavid S. Miller 	write_lock(&devtree_lock);
69dfa76060SDavid S. Miller 	prevp = &dp->properties;
70dfa76060SDavid S. Miller 	while (*prevp) {
71dfa76060SDavid S. Miller 		struct property *prop = *prevp;
72dfa76060SDavid S. Miller 
73dfa76060SDavid S. Miller 		if (!strcasecmp(prop->name, name)) {
74dfa76060SDavid S. Miller 			void *old_val = prop->value;
75dfa76060SDavid S. Miller 			int ret;
76dfa76060SDavid S. Miller 
776016a363SGrant Likely 			ret = prom_setprop(dp->phandle, name, val, len);
78dfa76060SDavid S. Miller 
79dfa76060SDavid S. Miller 			err = -EINVAL;
80dfa76060SDavid S. Miller 			if (ret >= 0) {
81dfa76060SDavid S. Miller 				prop->value = new_val;
82dfa76060SDavid S. Miller 				prop->length = len;
83dfa76060SDavid S. Miller 
84dfa76060SDavid S. Miller 				if (OF_IS_DYNAMIC(prop))
85dfa76060SDavid S. Miller 					kfree(old_val);
86dfa76060SDavid S. Miller 
87dfa76060SDavid S. Miller 				OF_MARK_DYNAMIC(prop);
88dfa76060SDavid S. Miller 
89dfa76060SDavid S. Miller 				err = 0;
90dfa76060SDavid S. Miller 			}
91dfa76060SDavid S. Miller 			break;
92dfa76060SDavid S. Miller 		}
93dfa76060SDavid S. Miller 		prevp = &(*prevp)->next;
94dfa76060SDavid S. Miller 	}
95dfa76060SDavid S. Miller 	write_unlock(&devtree_lock);
961c9d80ddSDavid S. Miller 	mutex_unlock(&of_set_property_mutex);
97dfa76060SDavid S. Miller 
98dfa76060SDavid S. Miller 	/* XXX Upate procfs if necessary... */
99dfa76060SDavid S. Miller 
100dfa76060SDavid S. Miller 	return err;
101dfa76060SDavid S. Miller }
102dfa76060SDavid S. Miller EXPORT_SYMBOL(of_set_property);
103dfa76060SDavid S. Miller 
104dfa76060SDavid S. Miller int of_find_in_proplist(const char *list, const char *match, int len)
105dfa76060SDavid S. Miller {
106dfa76060SDavid S. Miller 	while (len > 0) {
107dfa76060SDavid S. Miller 		int l;
108dfa76060SDavid S. Miller 
109dfa76060SDavid S. Miller 		if (!strcmp(list, match))
110dfa76060SDavid S. Miller 			return 1;
111dfa76060SDavid S. Miller 		l = strlen(list) + 1;
112dfa76060SDavid S. Miller 		list += l;
113dfa76060SDavid S. Miller 		len -= l;
114dfa76060SDavid S. Miller 	}
115dfa76060SDavid S. Miller 	return 0;
116dfa76060SDavid S. Miller }
117dfa76060SDavid S. Miller EXPORT_SYMBOL(of_find_in_proplist);
118dfa76060SDavid S. Miller 
119f90c34bdSAndres Salomon /*
120f90c34bdSAndres Salomon  * SPARC32 and SPARC64's prom_nextprop() do things differently
121f90c34bdSAndres Salomon  * here, despite sharing the same interface.  SPARC32 doesn't fill in 'buf',
122f90c34bdSAndres Salomon  * returning NULL on an error.  SPARC64 fills in 'buf', but sets it to an
123f90c34bdSAndres Salomon  * empty string upon error.
124f90c34bdSAndres Salomon  */
125f90c34bdSAndres Salomon static int __init handle_nextprop_quirks(char *buf, const char *name)
126f90c34bdSAndres Salomon {
127f90c34bdSAndres Salomon 	if (!name || strlen(name) == 0)
128f90c34bdSAndres Salomon 		return -1;
129f90c34bdSAndres Salomon 
130f90c34bdSAndres Salomon #ifdef CONFIG_SPARC32
131f90c34bdSAndres Salomon 	strcpy(buf, name);
132f90c34bdSAndres Salomon #endif
133f90c34bdSAndres Salomon 	return 0;
134f90c34bdSAndres Salomon }
135f90c34bdSAndres Salomon 
136f90c34bdSAndres Salomon static int __init prom_common_nextprop(phandle node, char *prev, char *buf)
137f90c34bdSAndres Salomon {
138f90c34bdSAndres Salomon 	const char *name;
139f90c34bdSAndres Salomon 
140f90c34bdSAndres Salomon 	buf[0] = '\0';
141f90c34bdSAndres Salomon 	name = prom_nextprop(node, prev, buf);
142f90c34bdSAndres Salomon 	return handle_nextprop_quirks(buf, name);
143f90c34bdSAndres Salomon }
144f90c34bdSAndres Salomon 
14523dc758eSDavid S. Miller unsigned int prom_early_allocated __initdata;
14623dc758eSDavid S. Miller 
147f90c34bdSAndres Salomon static struct of_pdt_ops prom_sparc_ops __initdata = {
148f90c34bdSAndres Salomon 	.nextprop = prom_common_nextprop,
149f90c34bdSAndres Salomon 	.getproplen = prom_getproplen,
150f90c34bdSAndres Salomon 	.getproperty = prom_getproperty,
151f90c34bdSAndres Salomon 	.getchild = prom_getchild,
152f90c34bdSAndres Salomon 	.getsibling = prom_getsibling,
153f90c34bdSAndres Salomon };
154f90c34bdSAndres Salomon 
1553cfc535cSAndres Salomon void __init prom_build_devicetree(void)
1563cfc535cSAndres Salomon {
157f90c34bdSAndres Salomon 	of_pdt_build_devicetree(prom_root_node, &prom_sparc_ops);
1583cfc535cSAndres Salomon 	of_console_init();
1593cfc535cSAndres Salomon 
1603cfc535cSAndres Salomon 	pr_info("PROM: Built device tree with %u bytes of memory.\n",
1613cfc535cSAndres Salomon 			prom_early_allocated);
1623cfc535cSAndres Salomon }
163