Blame view

drivers/of/pdt.c 4.81 KB
af6074fc9   Rob Herring   of: Use SPDX lice...
1
  // SPDX-License-Identifier: GPL-2.0+
3cfc535c5   Andres Salomon   of/promtree: make...
2
  /* pdt.c: OF PROM device tree support code.
9bdf6bab4   Andres Salomon   sparc: break out ...
3
4
5
6
7
8
9
10
   *
   * Paul Mackerras	August 1996.
   * Copyright (C) 1996-2005 Paul Mackerras.
   *
   *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
   *    {engebret|bergner}@us.ibm.com
   *
   *  Adapted for sparc by David S. Miller davem@davemloft.net
3cfc535c5   Andres Salomon   of/promtree: make...
11
   *  Adapted for multiple architectures by Andres Salomon <dilinger@queued.net>
9bdf6bab4   Andres Salomon   sparc: break out ...
12
13
14
15
16
17
18
19
   */
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/errno.h>
  #include <linux/mutex.h>
  #include <linux/slab.h>
  #include <linux/of.h>
3cfc535c5   Andres Salomon   of/promtree: make...
20
  #include <linux/of_pdt.h>
f90c34bd6   Andres Salomon   of/promtree: no l...
21
22
  
  static struct of_pdt_ops *of_pdt_prom_ops __initdata;
9bdf6bab4   Andres Salomon   sparc: break out ...
23

3cfc535c5   Andres Salomon   of/promtree: make...
24
25
26
27
28
29
  #if defined(CONFIG_SPARC)
  unsigned int of_pdt_unique_id __initdata;
  
  #define of_pdt_incr_unique_id(p) do { \
  	(p)->unique_id = of_pdt_unique_id++; \
  } while (0)
a74ea43df   Andres Salomon   of/promtree: allo...
30
  static char * __init of_pdt_build_full_name(struct device_node *dp)
3cfc535c5   Andres Salomon   of/promtree: make...
31
  {
0c5eaa774   Rob Herring   of: Drop full pat...
32
  	return build_path_component(dp);
3cfc535c5   Andres Salomon   of/promtree: make...
33
  }
a74ea43df   Andres Salomon   of/promtree: allo...
34
  #else /* CONFIG_SPARC */
3cfc535c5   Andres Salomon   of/promtree: make...
35
36
37
  
  static inline void of_pdt_incr_unique_id(void *p) { }
  static inline void irq_trans_init(struct device_node *dp) { }
a74ea43df   Andres Salomon   of/promtree: allo...
38
  static char * __init of_pdt_build_full_name(struct device_node *dp)
3cfc535c5   Andres Salomon   of/promtree: make...
39
  {
a74ea43df   Andres Salomon   of/promtree: allo...
40
  	static int failsafe_id = 0; /* for generating unique names on failure */
0c5eaa774   Rob Herring   of: Drop full pat...
41
42
  	const char *name;
  	char path[256];
a74ea43df   Andres Salomon   of/promtree: allo...
43
44
  	char *buf;
  	int len;
0c5eaa774   Rob Herring   of: Drop full pat...
45
46
47
48
49
50
  	if (!of_pdt_prom_ops->pkg2path(dp->phandle, path, sizeof(path), &len)) {
  		name = kbasename(path);
  		buf = prom_early_alloc(strlen(name) + 1);
  		strcpy(buf, name);
  		return buf;
  	}
a74ea43df   Andres Salomon   of/promtree: allo...
51

0c5eaa774   Rob Herring   of: Drop full pat...
52
53
54
  	name = of_get_property(dp, "name", &len);
  	buf = prom_early_alloc(len + 16);
  	sprintf(buf, "%s@unknown%i", name, failsafe_id++);
a74ea43df   Andres Salomon   of/promtree: allo...
55
56
57
  	pr_err("%s: pkg2path failed; assigning %s
  ", __func__, buf);
  	return buf;
3cfc535c5   Andres Salomon   of/promtree: make...
58
59
60
  }
  
  #endif /* !CONFIG_SPARC */
9bdf6bab4   Andres Salomon   sparc: break out ...
61

ed4185029   Andres Salomon   of/promtree: add ...
62
  static struct property * __init of_pdt_build_one_prop(phandle node, char *prev,
9bdf6bab4   Andres Salomon   sparc: break out ...
63
64
65
66
67
68
  					       char *special_name,
  					       void *special_val,
  					       int special_len)
  {
  	static struct property *tmp = NULL;
  	struct property *p;
f90c34bd6   Andres Salomon   of/promtree: no l...
69
  	int err;
9bdf6bab4   Andres Salomon   sparc: break out ...
70
71
72
73
74
75
76
  
  	if (tmp) {
  		p = tmp;
  		memset(p, 0, sizeof(*p) + 32);
  		tmp = NULL;
  	} else {
  		p = prom_early_alloc(sizeof(struct property) + 32);
3cfc535c5   Andres Salomon   of/promtree: make...
77
  		of_pdt_incr_unique_id(p);
9bdf6bab4   Andres Salomon   sparc: break out ...
78
79
80
81
82
83
84
85
86
  	}
  
  	p->name = (char *) (p + 1);
  	if (special_name) {
  		strcpy(p->name, special_name);
  		p->length = special_len;
  		p->value = prom_early_alloc(special_len);
  		memcpy(p->value, special_val, special_len);
  	} else {
f90c34bd6   Andres Salomon   of/promtree: no l...
87
88
  		err = of_pdt_prom_ops->nextprop(node, prev, p->name);
  		if (err) {
9bdf6bab4   Andres Salomon   sparc: break out ...
89
90
91
  			tmp = p;
  			return NULL;
  		}
f90c34bd6   Andres Salomon   of/promtree: no l...
92
  		p->length = of_pdt_prom_ops->getproplen(node, p->name);
9bdf6bab4   Andres Salomon   sparc: break out ...
93
94
95
96
97
98
  		if (p->length <= 0) {
  			p->length = 0;
  		} else {
  			int len;
  
  			p->value = prom_early_alloc(p->length + 1);
f90c34bd6   Andres Salomon   of/promtree: no l...
99
100
  			len = of_pdt_prom_ops->getproperty(node, p->name,
  					p->value, p->length);
9bdf6bab4   Andres Salomon   sparc: break out ...
101
102
103
104
105
106
107
  			if (len <= 0)
  				p->length = 0;
  			((unsigned char *)p->value)[p->length] = '\0';
  		}
  	}
  	return p;
  }
ed4185029   Andres Salomon   of/promtree: add ...
108
  static struct property * __init of_pdt_build_prop_list(phandle node)
9bdf6bab4   Andres Salomon   sparc: break out ...
109
110
  {
  	struct property *head, *tail;
ed4185029   Andres Salomon   of/promtree: add ...
111
  	head = tail = of_pdt_build_one_prop(node, NULL,
9bdf6bab4   Andres Salomon   sparc: break out ...
112
  				     ".node", &node, sizeof(node));
ed4185029   Andres Salomon   of/promtree: add ...
113
  	tail->next = of_pdt_build_one_prop(node, NULL, NULL, NULL, 0);
9bdf6bab4   Andres Salomon   sparc: break out ...
114
115
  	tail = tail->next;
  	while(tail) {
ed4185029   Andres Salomon   of/promtree: add ...
116
  		tail->next = of_pdt_build_one_prop(node, tail->name,
9bdf6bab4   Andres Salomon   sparc: break out ...
117
118
119
120
121
122
  					    NULL, NULL, 0);
  		tail = tail->next;
  	}
  
  	return head;
  }
ed4185029   Andres Salomon   of/promtree: add ...
123
  static char * __init of_pdt_get_one_property(phandle node, const char *name)
9bdf6bab4   Andres Salomon   sparc: break out ...
124
125
126
  {
  	char *buf = "<NULL>";
  	int len;
f90c34bd6   Andres Salomon   of/promtree: no l...
127
  	len = of_pdt_prom_ops->getproplen(node, name);
9bdf6bab4   Andres Salomon   sparc: break out ...
128
129
  	if (len > 0) {
  		buf = prom_early_alloc(len);
f90c34bd6   Andres Salomon   of/promtree: no l...
130
  		len = of_pdt_prom_ops->getproperty(node, name, buf, len);
9bdf6bab4   Andres Salomon   sparc: break out ...
131
132
133
134
  	}
  
  	return buf;
  }
ed4185029   Andres Salomon   of/promtree: add ...
135
  static struct device_node * __init of_pdt_create_node(phandle node,
9bdf6bab4   Andres Salomon   sparc: break out ...
136
137
138
139
140
141
142
143
  						    struct device_node *parent)
  {
  	struct device_node *dp;
  
  	if (!node)
  		return NULL;
  
  	dp = prom_early_alloc(sizeof(*dp));
0829f6d1f   Pantelis Antoniou   of: device_node k...
144
  	of_node_init(dp);
3cfc535c5   Andres Salomon   of/promtree: make...
145
  	of_pdt_incr_unique_id(dp);
9bdf6bab4   Andres Salomon   sparc: break out ...
146
  	dp->parent = parent;
a74ea43df   Andres Salomon   of/promtree: allo...
147
  	dp->name = of_pdt_get_one_property(node, "name");
9bdf6bab4   Andres Salomon   sparc: break out ...
148
  	dp->phandle = node;
ed4185029   Andres Salomon   of/promtree: add ...
149
  	dp->properties = of_pdt_build_prop_list(node);
9bdf6bab4   Andres Salomon   sparc: break out ...
150

0c5eaa774   Rob Herring   of: Drop full pat...
151
  	dp->full_name = of_pdt_build_full_name(dp);
9bdf6bab4   Andres Salomon   sparc: break out ...
152
153
154
155
  	irq_trans_init(dp);
  
  	return dp;
  }
ed4185029   Andres Salomon   of/promtree: add ...
156
  static struct device_node * __init of_pdt_build_tree(struct device_node *parent,
5063e25a3   Grant Likely   of: Eliminate of_...
157
  						   phandle node)
9bdf6bab4   Andres Salomon   sparc: break out ...
158
159
160
161
162
  {
  	struct device_node *ret = NULL, *prev_sibling = NULL;
  	struct device_node *dp;
  
  	while (1) {
ed4185029   Andres Salomon   of/promtree: add ...
163
  		dp = of_pdt_create_node(node, parent);
9bdf6bab4   Andres Salomon   sparc: break out ...
164
165
166
167
168
169
170
171
172
  		if (!dp)
  			break;
  
  		if (prev_sibling)
  			prev_sibling->sibling = dp;
  
  		if (!ret)
  			ret = dp;
  		prev_sibling = dp;
5063e25a3   Grant Likely   of: Eliminate of_...
173
  		dp->child = of_pdt_build_tree(dp, of_pdt_prom_ops->getchild(node));
9bdf6bab4   Andres Salomon   sparc: break out ...
174

f90c34bd6   Andres Salomon   of/promtree: no l...
175
  		node = of_pdt_prom_ops->getsibling(node);
9bdf6bab4   Andres Salomon   sparc: break out ...
176
177
178
179
  	}
  
  	return ret;
  }
75c71848f   Sam Ravnborg   of/pdt: fix secti...
180
  static void * __init kernel_tree_alloc(u64 size, u64 align)
611cad720   Shawn Guo   dt: add of_alias_...
181
182
183
  {
  	return prom_early_alloc(size);
  }
f90c34bd6   Andres Salomon   of/promtree: no l...
184
  void __init of_pdt_build_devicetree(phandle root_node, struct of_pdt_ops *ops)
9bdf6bab4   Andres Salomon   sparc: break out ...
185
  {
f90c34bd6   Andres Salomon   of/promtree: no l...
186
187
  	BUG_ON(!ops);
  	of_pdt_prom_ops = ops;
5063e25a3   Grant Likely   of: Eliminate of_...
188
  	of_root = of_pdt_create_node(root_node, NULL);
5063e25a3   Grant Likely   of: Eliminate of_...
189
  	of_root->full_name = "/";
9bdf6bab4   Andres Salomon   sparc: break out ...
190

5063e25a3   Grant Likely   of: Eliminate of_...
191
192
  	of_root->child = of_pdt_build_tree(of_root,
  				of_pdt_prom_ops->getchild(of_root->phandle));
611cad720   Shawn Guo   dt: add of_alias_...
193

4c7d6361f   Robert P. J. Day   open firmware: "/...
194
  	/* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
611cad720   Shawn Guo   dt: add of_alias_...
195
  	of_alias_scan(kernel_tree_alloc);
9bdf6bab4   Andres Salomon   sparc: break out ...
196
  }