staging:iio:adc:ad7606 move to info_mask_(shared_by_type/separate)
[linux/fpc-iii.git] / arch / sparc / kernel / prom_common.c
blob9f20566b0773a8b17003176c0159aea062935f39
1 /* prom_common.c: OF device tree support common code.
3 * Paul Mackerras August 1996.
4 * Copyright (C) 1996-2005 Paul Mackerras.
6 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
7 * {engebret|bergner}@us.ibm.com
9 * Adapted for sparc by David S. Miller davem@davemloft.net
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/export.h>
19 #include <linux/errno.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/of.h>
23 #include <linux/of_pdt.h>
24 #include <asm/prom.h>
25 #include <asm/oplib.h>
27 #include "prom.h"
29 struct device_node *of_console_device;
30 EXPORT_SYMBOL(of_console_device);
32 char *of_console_path;
33 EXPORT_SYMBOL(of_console_path);
35 char *of_console_options;
36 EXPORT_SYMBOL(of_console_options);
38 int of_getintprop_default(struct device_node *np, const char *name, int def)
40 struct property *prop;
41 int len;
43 prop = of_find_property(np, name, &len);
44 if (!prop || len != 4)
45 return def;
47 return *(int *) prop->value;
49 EXPORT_SYMBOL(of_getintprop_default);
51 DEFINE_MUTEX(of_set_property_mutex);
52 EXPORT_SYMBOL(of_set_property_mutex);
54 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
56 struct property **prevp;
57 void *new_val;
58 int err;
60 new_val = kmemdup(val, len, GFP_KERNEL);
61 if (!new_val)
62 return -ENOMEM;
64 err = -ENODEV;
66 mutex_lock(&of_set_property_mutex);
67 raw_spin_lock(&devtree_lock);
68 prevp = &dp->properties;
69 while (*prevp) {
70 struct property *prop = *prevp;
72 if (!strcasecmp(prop->name, name)) {
73 void *old_val = prop->value;
74 int ret;
76 ret = prom_setprop(dp->phandle, name, val, len);
78 err = -EINVAL;
79 if (ret >= 0) {
80 prop->value = new_val;
81 prop->length = len;
83 if (OF_IS_DYNAMIC(prop))
84 kfree(old_val);
86 OF_MARK_DYNAMIC(prop);
88 err = 0;
90 break;
92 prevp = &(*prevp)->next;
94 raw_spin_unlock(&devtree_lock);
95 mutex_unlock(&of_set_property_mutex);
97 /* XXX Upate procfs if necessary... */
99 return err;
101 EXPORT_SYMBOL(of_set_property);
103 int of_find_in_proplist(const char *list, const char *match, int len)
105 while (len > 0) {
106 int l;
108 if (!strcmp(list, match))
109 return 1;
110 l = strlen(list) + 1;
111 list += l;
112 len -= l;
114 return 0;
116 EXPORT_SYMBOL(of_find_in_proplist);
119 * SPARC32 and SPARC64's prom_nextprop() do things differently
120 * here, despite sharing the same interface. SPARC32 doesn't fill in 'buf',
121 * returning NULL on an error. SPARC64 fills in 'buf', but sets it to an
122 * empty string upon error.
124 static int __init handle_nextprop_quirks(char *buf, const char *name)
126 if (!name || strlen(name) == 0)
127 return -1;
129 #ifdef CONFIG_SPARC32
130 strcpy(buf, name);
131 #endif
132 return 0;
135 static int __init prom_common_nextprop(phandle node, char *prev, char *buf)
137 const char *name;
139 buf[0] = '\0';
140 name = prom_nextprop(node, prev, buf);
141 return handle_nextprop_quirks(buf, name);
144 unsigned int prom_early_allocated __initdata;
146 static struct of_pdt_ops prom_sparc_ops __initdata = {
147 .nextprop = prom_common_nextprop,
148 .getproplen = prom_getproplen,
149 .getproperty = prom_getproperty,
150 .getchild = prom_getchild,
151 .getsibling = prom_getsibling,
154 void __init prom_build_devicetree(void)
156 of_pdt_build_devicetree(prom_root_node, &prom_sparc_ops);
157 of_console_init();
159 pr_info("PROM: Built device tree with %u bytes of memory.\n",
160 prom_early_allocated);