blob: fe84d56b7c5a45310f8e1d95286be228190290f0 [file] [log] [blame]
David S. Millerdfa76062008-12-04 20:28:22 -08001/* prom_common.c: OF device tree support common code.
2 *
3 * Paul Mackerras August 1996.
4 * Copyright (C) 1996-2005 Paul Mackerras.
5 *
6 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
7 * {engebret|bergner}@us.ibm.com
8 *
9 * Adapted for sparc by David S. Miller davem@davemloft.net
10 *
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.
15 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/errno.h>
20#include <linux/mutex.h>
21#include <linux/slab.h>
22#include <linux/of.h>
Andres Salomon3cfc5352010-10-10 21:42:33 -060023#include <linux/of_pdt.h>
David S. Millerdfa76062008-12-04 20:28:22 -080024#include <asm/prom.h>
25#include <asm/oplib.h>
Konrad Eiselee63829d2009-08-17 00:13:32 +000026#include <asm/leon.h>
David S. Millerdfa76062008-12-04 20:28:22 -080027
28#include "prom.h"
29
David S. Millerad07aed2008-12-05 15:20:26 -080030struct device_node *of_console_device;
31EXPORT_SYMBOL(of_console_device);
32
33char *of_console_path;
34EXPORT_SYMBOL(of_console_path);
35
36char *of_console_options;
37EXPORT_SYMBOL(of_console_options);
38
David S. Millerdfa76062008-12-04 20:28:22 -080039int of_getintprop_default(struct device_node *np, const char *name, int def)
40{
41 struct property *prop;
42 int len;
43
44 prop = of_find_property(np, name, &len);
45 if (!prop || len != 4)
46 return def;
47
48 return *(int *) prop->value;
49}
50EXPORT_SYMBOL(of_getintprop_default);
51
52DEFINE_MUTEX(of_set_property_mutex);
53EXPORT_SYMBOL(of_set_property_mutex);
54
55int of_set_property(struct device_node *dp, const char *name, void *val, int len)
56{
57 struct property **prevp;
58 void *new_val;
59 int err;
60
61 new_val = kmalloc(len, GFP_KERNEL);
62 if (!new_val)
63 return -ENOMEM;
64
65 memcpy(new_val, val, len);
66
67 err = -ENODEV;
68
David S. Miller1c9d80d2009-11-08 17:41:20 -080069 mutex_lock(&of_set_property_mutex);
David S. Millerdfa76062008-12-04 20:28:22 -080070 write_lock(&devtree_lock);
71 prevp = &dp->properties;
72 while (*prevp) {
73 struct property *prop = *prevp;
74
75 if (!strcasecmp(prop->name, name)) {
76 void *old_val = prop->value;
77 int ret;
78
Grant Likely6016a362010-01-28 14:06:53 -070079 ret = prom_setprop(dp->phandle, name, val, len);
David S. Millerdfa76062008-12-04 20:28:22 -080080
81 err = -EINVAL;
82 if (ret >= 0) {
83 prop->value = new_val;
84 prop->length = len;
85
86 if (OF_IS_DYNAMIC(prop))
87 kfree(old_val);
88
89 OF_MARK_DYNAMIC(prop);
90
91 err = 0;
92 }
93 break;
94 }
95 prevp = &(*prevp)->next;
96 }
97 write_unlock(&devtree_lock);
David S. Miller1c9d80d2009-11-08 17:41:20 -080098 mutex_unlock(&of_set_property_mutex);
David S. Millerdfa76062008-12-04 20:28:22 -080099
100 /* XXX Upate procfs if necessary... */
101
102 return err;
103}
104EXPORT_SYMBOL(of_set_property);
105
106int of_find_in_proplist(const char *list, const char *match, int len)
107{
108 while (len > 0) {
109 int l;
110
111 if (!strcmp(list, match))
112 return 1;
113 l = strlen(list) + 1;
114 list += l;
115 len -= l;
116 }
117 return 0;
118}
119EXPORT_SYMBOL(of_find_in_proplist);
120
David S. Miller23dc7582008-12-05 18:16:48 -0800121unsigned int prom_early_allocated __initdata;
122
Andres Salomon3cfc5352010-10-10 21:42:33 -0600123void __init prom_build_devicetree(void)
124{
125 of_pdt_build_devicetree(prom_root_node);
126 of_console_init();
127
128 pr_info("PROM: Built device tree with %u bytes of memory.\n",
129 prom_early_allocated);
130}