1/* pdt.c: OF PROM device tree support 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 *  Adapted for multiple architectures by Andres Salomon <dilinger@queued.net>
11 *
12 *      This program is free software; you can redistribute it and/or
13 *      modify it under the terms of the GNU General Public License
14 *      as published by the Free Software Foundation; either version
15 *      2 of the License, or (at your option) any later version.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/errno.h>
21#include <linux/mutex.h>
22#include <linux/slab.h>
23#include <linux/of.h>
24#include <linux/of_pdt.h>
25
26static struct of_pdt_ops *of_pdt_prom_ops __initdata;
27
28void __initdata (*of_pdt_build_more)(struct device_node *dp);
29
30#if defined(CONFIG_SPARC)
31unsigned int of_pdt_unique_id __initdata;
32
33#define of_pdt_incr_unique_id(p) do { \
34	(p)->unique_id = of_pdt_unique_id++; \
35} while (0)
36
37static char * __init of_pdt_build_full_name(struct device_node *dp)
38{
39	int len, ourlen, plen;
40	char *n;
41
42	dp->path_component_name = build_path_component(dp);
43
44	plen = strlen(dp->parent->full_name);
45	ourlen = strlen(dp->path_component_name);
46	len = ourlen + plen + 2;
47
48	n = prom_early_alloc(len);
49	strcpy(n, dp->parent->full_name);
50	if (!of_node_is_root(dp->parent)) {
51		strcpy(n + plen, "/");
52		plen++;
53	}
54	strcpy(n + plen, dp->path_component_name);
55
56	return n;
57}
58
59#else /* CONFIG_SPARC */
60
61static inline void of_pdt_incr_unique_id(void *p) { }
62static inline void irq_trans_init(struct device_node *dp) { }
63
64static char * __init of_pdt_build_full_name(struct device_node *dp)
65{
66	static int failsafe_id = 0; /* for generating unique names on failure */
67	char *buf;
68	int len;
69
70	if (of_pdt_prom_ops->pkg2path(dp->phandle, NULL, 0, &len))
71		goto failsafe;
72
73	buf = prom_early_alloc(len + 1);
74	if (of_pdt_prom_ops->pkg2path(dp->phandle, buf, len, &len))
75		goto failsafe;
76	return buf;
77
78 failsafe:
79	buf = prom_early_alloc(strlen(dp->parent->full_name) +
80			       strlen(dp->name) + 16);
81	sprintf(buf, "%s/%s@unknown%i",
82		of_node_is_root(dp->parent) ? "" : dp->parent->full_name,
83		dp->name, failsafe_id++);
84	pr_err("%s: pkg2path failed; assigning %s\n", __func__, buf);
85	return buf;
86}
87
88#endif /* !CONFIG_SPARC */
89
90static struct property * __init of_pdt_build_one_prop(phandle node, char *prev,
91					       char *special_name,
92					       void *special_val,
93					       int special_len)
94{
95	static struct property *tmp = NULL;
96	struct property *p;
97	int err;
98
99	if (tmp) {
100		p = tmp;
101		memset(p, 0, sizeof(*p) + 32);
102		tmp = NULL;
103	} else {
104		p = prom_early_alloc(sizeof(struct property) + 32);
105		of_pdt_incr_unique_id(p);
106	}
107
108	p->name = (char *) (p + 1);
109	if (special_name) {
110		strcpy(p->name, special_name);
111		p->length = special_len;
112		p->value = prom_early_alloc(special_len);
113		memcpy(p->value, special_val, special_len);
114	} else {
115		err = of_pdt_prom_ops->nextprop(node, prev, p->name);
116		if (err) {
117			tmp = p;
118			return NULL;
119		}
120		p->length = of_pdt_prom_ops->getproplen(node, p->name);
121		if (p->length <= 0) {
122			p->length = 0;
123		} else {
124			int len;
125
126			p->value = prom_early_alloc(p->length + 1);
127			len = of_pdt_prom_ops->getproperty(node, p->name,
128					p->value, p->length);
129			if (len <= 0)
130				p->length = 0;
131			((unsigned char *)p->value)[p->length] = '\0';
132		}
133	}
134	return p;
135}
136
137static struct property * __init of_pdt_build_prop_list(phandle node)
138{
139	struct property *head, *tail;
140
141	head = tail = of_pdt_build_one_prop(node, NULL,
142				     ".node", &node, sizeof(node));
143
144	tail->next = of_pdt_build_one_prop(node, NULL, NULL, NULL, 0);
145	tail = tail->next;
146	while(tail) {
147		tail->next = of_pdt_build_one_prop(node, tail->name,
148					    NULL, NULL, 0);
149		tail = tail->next;
150	}
151
152	return head;
153}
154
155static char * __init of_pdt_get_one_property(phandle node, const char *name)
156{
157	char *buf = "<NULL>";
158	int len;
159
160	len = of_pdt_prom_ops->getproplen(node, name);
161	if (len > 0) {
162		buf = prom_early_alloc(len);
163		len = of_pdt_prom_ops->getproperty(node, name, buf, len);
164	}
165
166	return buf;
167}
168
169static struct device_node * __init of_pdt_create_node(phandle node,
170						    struct device_node *parent)
171{
172	struct device_node *dp;
173
174	if (!node)
175		return NULL;
176
177	dp = prom_early_alloc(sizeof(*dp));
178	of_node_init(dp);
179	of_pdt_incr_unique_id(dp);
180	dp->parent = parent;
181
182	dp->name = of_pdt_get_one_property(node, "name");
183	dp->type = of_pdt_get_one_property(node, "device_type");
184	dp->phandle = node;
185
186	dp->properties = of_pdt_build_prop_list(node);
187
188	irq_trans_init(dp);
189
190	return dp;
191}
192
193static struct device_node * __init of_pdt_build_tree(struct device_node *parent,
194						   phandle node)
195{
196	struct device_node *ret = NULL, *prev_sibling = NULL;
197	struct device_node *dp;
198
199	while (1) {
200		dp = of_pdt_create_node(node, parent);
201		if (!dp)
202			break;
203
204		if (prev_sibling)
205			prev_sibling->sibling = dp;
206
207		if (!ret)
208			ret = dp;
209		prev_sibling = dp;
210
211		dp->full_name = of_pdt_build_full_name(dp);
212
213		dp->child = of_pdt_build_tree(dp, of_pdt_prom_ops->getchild(node));
214
215		if (of_pdt_build_more)
216			of_pdt_build_more(dp);
217
218		node = of_pdt_prom_ops->getsibling(node);
219	}
220
221	return ret;
222}
223
224static void * __init kernel_tree_alloc(u64 size, u64 align)
225{
226	return prom_early_alloc(size);
227}
228
229void __init of_pdt_build_devicetree(phandle root_node, struct of_pdt_ops *ops)
230{
231	BUG_ON(!ops);
232	of_pdt_prom_ops = ops;
233
234	of_root = of_pdt_create_node(root_node, NULL);
235#if defined(CONFIG_SPARC)
236	of_root->path_component_name = "";
237#endif
238	of_root->full_name = "/";
239
240	of_root->child = of_pdt_build_tree(of_root,
241				of_pdt_prom_ops->getchild(of_root->phandle));
242
243	/* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
244	of_alias_scan(kernel_tree_alloc);
245}
246