xref: /OK3568_Linux_fs/kernel/arch/sparc/kernel/prom_common.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* prom_common.c: OF device tree support common code.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Paul Mackerras	August 1996.
5*4882a593Smuzhiyun  * Copyright (C) 1996-2005 Paul Mackerras.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8*4882a593Smuzhiyun  *    {engebret|bergner}@us.ibm.com
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *  Adapted for sparc by David S. Miller davem@davemloft.net
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/export.h>
15*4882a593Smuzhiyun #include <linux/errno.h>
16*4882a593Smuzhiyun #include <linux/mutex.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/of.h>
19*4882a593Smuzhiyun #include <linux/of_pdt.h>
20*4882a593Smuzhiyun #include <asm/prom.h>
21*4882a593Smuzhiyun #include <asm/oplib.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include "prom.h"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun struct device_node *of_console_device;
26*4882a593Smuzhiyun EXPORT_SYMBOL(of_console_device);
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun char *of_console_path;
29*4882a593Smuzhiyun EXPORT_SYMBOL(of_console_path);
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun char *of_console_options;
32*4882a593Smuzhiyun EXPORT_SYMBOL(of_console_options);
33*4882a593Smuzhiyun 
of_getintprop_default(struct device_node * np,const char * name,int def)34*4882a593Smuzhiyun int of_getintprop_default(struct device_node *np, const char *name, int def)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	struct property *prop;
37*4882a593Smuzhiyun 	int len;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	prop = of_find_property(np, name, &len);
40*4882a593Smuzhiyun 	if (!prop || len != 4)
41*4882a593Smuzhiyun 		return def;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	return *(int *) prop->value;
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun EXPORT_SYMBOL(of_getintprop_default);
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun DEFINE_MUTEX(of_set_property_mutex);
48*4882a593Smuzhiyun EXPORT_SYMBOL(of_set_property_mutex);
49*4882a593Smuzhiyun 
of_set_property(struct device_node * dp,const char * name,void * val,int len)50*4882a593Smuzhiyun int of_set_property(struct device_node *dp, const char *name, void *val, int len)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	struct property **prevp;
53*4882a593Smuzhiyun 	unsigned long flags;
54*4882a593Smuzhiyun 	void *new_val;
55*4882a593Smuzhiyun 	int err;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	new_val = kmemdup(val, len, GFP_KERNEL);
58*4882a593Smuzhiyun 	if (!new_val)
59*4882a593Smuzhiyun 		return -ENOMEM;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	err = -ENODEV;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	mutex_lock(&of_set_property_mutex);
64*4882a593Smuzhiyun 	raw_spin_lock_irqsave(&devtree_lock, flags);
65*4882a593Smuzhiyun 	prevp = &dp->properties;
66*4882a593Smuzhiyun 	while (*prevp) {
67*4882a593Smuzhiyun 		struct property *prop = *prevp;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 		if (!strcasecmp(prop->name, name)) {
70*4882a593Smuzhiyun 			void *old_val = prop->value;
71*4882a593Smuzhiyun 			int ret;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 			ret = prom_setprop(dp->phandle, name, val, len);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 			err = -EINVAL;
76*4882a593Smuzhiyun 			if (ret >= 0) {
77*4882a593Smuzhiyun 				prop->value = new_val;
78*4882a593Smuzhiyun 				prop->length = len;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 				if (OF_IS_DYNAMIC(prop))
81*4882a593Smuzhiyun 					kfree(old_val);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 				OF_MARK_DYNAMIC(prop);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 				err = 0;
86*4882a593Smuzhiyun 			}
87*4882a593Smuzhiyun 			break;
88*4882a593Smuzhiyun 		}
89*4882a593Smuzhiyun 		prevp = &(*prevp)->next;
90*4882a593Smuzhiyun 	}
91*4882a593Smuzhiyun 	raw_spin_unlock_irqrestore(&devtree_lock, flags);
92*4882a593Smuzhiyun 	mutex_unlock(&of_set_property_mutex);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	/* XXX Upate procfs if necessary... */
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	return err;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun EXPORT_SYMBOL(of_set_property);
99*4882a593Smuzhiyun 
of_find_in_proplist(const char * list,const char * match,int len)100*4882a593Smuzhiyun int of_find_in_proplist(const char *list, const char *match, int len)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	while (len > 0) {
103*4882a593Smuzhiyun 		int l;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 		if (!strcmp(list, match))
106*4882a593Smuzhiyun 			return 1;
107*4882a593Smuzhiyun 		l = strlen(list) + 1;
108*4882a593Smuzhiyun 		list += l;
109*4882a593Smuzhiyun 		len -= l;
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun 	return 0;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun EXPORT_SYMBOL(of_find_in_proplist);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /*
116*4882a593Smuzhiyun  * SPARC32 and SPARC64's prom_nextprop() do things differently
117*4882a593Smuzhiyun  * here, despite sharing the same interface.  SPARC32 doesn't fill in 'buf',
118*4882a593Smuzhiyun  * returning NULL on an error.  SPARC64 fills in 'buf', but sets it to an
119*4882a593Smuzhiyun  * empty string upon error.
120*4882a593Smuzhiyun  */
handle_nextprop_quirks(char * buf,const char * name)121*4882a593Smuzhiyun static int __init handle_nextprop_quirks(char *buf, const char *name)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	if (!name || strlen(name) == 0)
124*4882a593Smuzhiyun 		return -1;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun #ifdef CONFIG_SPARC32
127*4882a593Smuzhiyun 	strcpy(buf, name);
128*4882a593Smuzhiyun #endif
129*4882a593Smuzhiyun 	return 0;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
prom_common_nextprop(phandle node,char * prev,char * buf)132*4882a593Smuzhiyun static int __init prom_common_nextprop(phandle node, char *prev, char *buf)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	const char *name;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	buf[0] = '\0';
137*4882a593Smuzhiyun 	name = prom_nextprop(node, prev, buf);
138*4882a593Smuzhiyun 	return handle_nextprop_quirks(buf, name);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun unsigned int prom_early_allocated __initdata;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun static struct of_pdt_ops prom_sparc_ops __initdata = {
144*4882a593Smuzhiyun 	.nextprop = prom_common_nextprop,
145*4882a593Smuzhiyun 	.getproplen = prom_getproplen,
146*4882a593Smuzhiyun 	.getproperty = prom_getproperty,
147*4882a593Smuzhiyun 	.getchild = prom_getchild,
148*4882a593Smuzhiyun 	.getsibling = prom_getsibling,
149*4882a593Smuzhiyun };
150*4882a593Smuzhiyun 
prom_build_devicetree(void)151*4882a593Smuzhiyun void __init prom_build_devicetree(void)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	of_pdt_build_devicetree(prom_root_node, &prom_sparc_ops);
154*4882a593Smuzhiyun 	of_console_init();
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	pr_info("PROM: Built device tree with %u bytes of memory.\n",
157*4882a593Smuzhiyun 			prom_early_allocated);
158*4882a593Smuzhiyun }
159