blob: 0f5aab4326b3e7bf1d6f995e1a90d7d53079bc27 [file] [log] [blame]
David S. Miller942a6bd2006-06-23 15:53:31 -07001/*
2 * Procedures for creating, accessing and interpreting the device tree.
3 *
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
6 *
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
9 *
10 * Adapted for sparc32 by David S. Miller davem@davemloft.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/types.h>
20#include <linux/string.h>
21#include <linux/mm.h>
22#include <linux/bootmem.h>
23#include <linux/module.h>
24
25#include <asm/prom.h>
26#include <asm/oplib.h>
27
28static struct device_node *allnodes;
29
Stephen Rothwell581b6052007-04-24 16:46:53 +100030extern rwlock_t devtree_lock; /* temporary while merging */
David S. Millerfb7cd9d2006-06-25 23:18:36 -070031
David S. Miller942a6bd2006-06-23 15:53:31 -070032struct device_node *of_get_parent(const struct device_node *node)
33{
34 struct device_node *np;
35
36 if (!node)
37 return NULL;
38
39 np = node->parent;
40
41 return np;
42}
43EXPORT_SYMBOL(of_get_parent);
44
45struct device_node *of_get_next_child(const struct device_node *node,
46 struct device_node *prev)
47{
48 struct device_node *next;
49
50 next = prev ? prev->sibling : node->child;
51 for (; next != 0; next = next->sibling) {
52 break;
53 }
54
55 return next;
56}
57EXPORT_SYMBOL(of_get_next_child);
58
59struct device_node *of_find_node_by_path(const char *path)
60{
61 struct device_node *np = allnodes;
62
63 for (; np != 0; np = np->allnext) {
64 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
65 break;
66 }
67
68 return np;
69}
70EXPORT_SYMBOL(of_find_node_by_path);
71
72struct device_node *of_find_node_by_phandle(phandle handle)
73{
74 struct device_node *np;
75
76 for (np = allnodes; np != 0; np = np->allnext)
77 if (np->node == handle)
78 break;
79
80 return np;
81}
82EXPORT_SYMBOL(of_find_node_by_phandle);
83
84struct device_node *of_find_node_by_name(struct device_node *from,
85 const char *name)
86{
87 struct device_node *np;
88
89 np = from ? from->allnext : allnodes;
90 for (; np != NULL; np = np->allnext)
91 if (np->name != NULL && strcmp(np->name, name) == 0)
92 break;
93
94 return np;
95}
96EXPORT_SYMBOL(of_find_node_by_name);
97
98struct device_node *of_find_node_by_type(struct device_node *from,
99 const char *type)
100{
101 struct device_node *np;
102
103 np = from ? from->allnext : allnodes;
104 for (; np != 0; np = np->allnext)
105 if (np->type != 0 && strcmp(np->type, type) == 0)
106 break;
107
108 return np;
109}
110EXPORT_SYMBOL(of_find_node_by_type);
111
112struct device_node *of_find_compatible_node(struct device_node *from,
113 const char *type, const char *compatible)
114{
115 struct device_node *np;
116
117 np = from ? from->allnext : allnodes;
118 for (; np != 0; np = np->allnext) {
119 if (type != NULL
120 && !(np->type != 0 && strcmp(np->type, type) == 0))
121 continue;
122 if (of_device_is_compatible(np, compatible))
123 break;
124 }
125
126 return np;
127}
128EXPORT_SYMBOL(of_find_compatible_node);
129
David S. Miller942a6bd2006-06-23 15:53:31 -0700130int of_getintprop_default(struct device_node *np, const char *name, int def)
131{
132 struct property *prop;
133 int len;
134
135 prop = of_find_property(np, name, &len);
136 if (!prop || len != 4)
137 return def;
138
139 return *(int *) prop->value;
140}
141EXPORT_SYMBOL(of_getintprop_default);
142
David S. Millerfb7cd9d2006-06-25 23:18:36 -0700143int of_set_property(struct device_node *dp, const char *name, void *val, int len)
144{
145 struct property **prevp;
146 void *new_val;
147 int err;
148
149 new_val = kmalloc(len, GFP_KERNEL);
150 if (!new_val)
151 return -ENOMEM;
152
153 memcpy(new_val, val, len);
154
155 err = -ENODEV;
156
157 write_lock(&devtree_lock);
158 prevp = &dp->properties;
159 while (*prevp) {
160 struct property *prop = *prevp;
161
David S. Millera8b88142007-03-29 01:28:51 -0700162 if (!strcasecmp(prop->name, name)) {
David S. Millerfb7cd9d2006-06-25 23:18:36 -0700163 void *old_val = prop->value;
164 int ret;
165
Martin Habets078830e2006-10-09 18:10:16 -0700166 ret = prom_setprop(dp->node, (char *) name, val, len);
David S. Millerfb7cd9d2006-06-25 23:18:36 -0700167 err = -EINVAL;
168 if (ret >= 0) {
169 prop->value = new_val;
170 prop->length = len;
171
172 if (OF_IS_DYNAMIC(prop))
173 kfree(old_val);
174
175 OF_MARK_DYNAMIC(prop);
176
177 err = 0;
178 }
179 break;
180 }
181 prevp = &(*prevp)->next;
182 }
183 write_unlock(&devtree_lock);
184
185 /* XXX Upate procfs if necessary... */
186
187 return err;
188}
189EXPORT_SYMBOL(of_set_property);
190
David S. Miller942a6bd2006-06-23 15:53:31 -0700191static unsigned int prom_early_allocated;
192
193static void * __init prom_early_alloc(unsigned long size)
194{
195 void *ret;
196
197 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
198 if (ret != NULL)
199 memset(ret, 0, size);
200
201 prom_early_allocated += size;
202
203 return ret;
204}
205
206static int is_root_node(const struct device_node *dp)
207{
208 if (!dp)
209 return 0;
210
211 return (dp->parent == NULL);
212}
213
214/* The following routines deal with the black magic of fully naming a
215 * node.
216 *
217 * Certain well known named nodes are just the simple name string.
218 *
219 * Actual devices have an address specifier appended to the base name
220 * string, like this "foo@addr". The "addr" can be in any number of
221 * formats, and the platform plus the type of the node determine the
222 * format and how it is constructed.
223 *
224 * For children of the ROOT node, the naming convention is fixed and
225 * determined by whether this is a sun4u or sun4v system.
226 *
227 * For children of other nodes, it is bus type specific. So
228 * we walk up the tree until we discover a "device_type" property
229 * we recognize and we go from there.
230 */
231static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
232{
233 struct linux_prom_registers *regs;
234 struct property *rprop;
235
236 rprop = of_find_property(dp, "reg", NULL);
237 if (!rprop)
238 return;
239
240 regs = rprop->value;
241 sprintf(tmp_buf, "%s@%x,%x",
242 dp->name,
243 regs->which_io, regs->phys_addr);
244}
245
246/* "name@slot,offset" */
247static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
248{
249 struct linux_prom_registers *regs;
250 struct property *prop;
251
252 prop = of_find_property(dp, "reg", NULL);
253 if (!prop)
254 return;
255
256 regs = prop->value;
257 sprintf(tmp_buf, "%s@%x,%x",
258 dp->name,
259 regs->which_io,
260 regs->phys_addr);
261}
262
263/* "name@devnum[,func]" */
264static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
265{
266 struct linux_prom_pci_registers *regs;
267 struct property *prop;
268 unsigned int devfn;
269
270 prop = of_find_property(dp, "reg", NULL);
271 if (!prop)
272 return;
273
274 regs = prop->value;
275 devfn = (regs->phys_hi >> 8) & 0xff;
276 if (devfn & 0x07) {
277 sprintf(tmp_buf, "%s@%x,%x",
278 dp->name,
279 devfn >> 3,
280 devfn & 0x07);
281 } else {
282 sprintf(tmp_buf, "%s@%x",
283 dp->name,
284 devfn >> 3);
285 }
286}
287
288/* "name@addrhi,addrlo" */
289static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
290{
291 struct linux_prom_registers *regs;
292 struct property *prop;
293
294 prop = of_find_property(dp, "reg", NULL);
295 if (!prop)
296 return;
297
298 regs = prop->value;
299
300 sprintf(tmp_buf, "%s@%x,%x",
301 dp->name,
302 regs->which_io, regs->phys_addr);
303}
304
305static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
306{
307 struct device_node *parent = dp->parent;
308
309 if (parent != NULL) {
310 if (!strcmp(parent->type, "pci") ||
311 !strcmp(parent->type, "pciex"))
312 return pci_path_component(dp, tmp_buf);
313 if (!strcmp(parent->type, "sbus"))
314 return sbus_path_component(dp, tmp_buf);
315 if (!strcmp(parent->type, "ebus"))
316 return ebus_path_component(dp, tmp_buf);
317
318 /* "isa" is handled with platform naming */
319 }
320
321 /* Use platform naming convention. */
322 return sparc32_path_component(dp, tmp_buf);
323}
324
325static char * __init build_path_component(struct device_node *dp)
326{
327 char tmp_buf[64], *n;
328
329 tmp_buf[0] = '\0';
330 __build_path_component(dp, tmp_buf);
331 if (tmp_buf[0] == '\0')
332 strcpy(tmp_buf, dp->name);
333
334 n = prom_early_alloc(strlen(tmp_buf) + 1);
335 strcpy(n, tmp_buf);
336
337 return n;
338}
339
340static char * __init build_full_name(struct device_node *dp)
341{
342 int len, ourlen, plen;
343 char *n;
344
345 plen = strlen(dp->parent->full_name);
346 ourlen = strlen(dp->path_component_name);
347 len = ourlen + plen + 2;
348
349 n = prom_early_alloc(len);
350 strcpy(n, dp->parent->full_name);
351 if (!is_root_node(dp->parent)) {
352 strcpy(n + plen, "/");
353 plen++;
354 }
355 strcpy(n + plen, dp->path_component_name);
356
357 return n;
358}
359
David S. Miller87b385d2006-06-25 23:18:57 -0700360static unsigned int unique_id;
361
362static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
David S. Miller942a6bd2006-06-23 15:53:31 -0700363{
364 static struct property *tmp = NULL;
365 struct property *p;
366 int len;
Bob Breuerf7785a62006-07-17 17:05:56 -0700367 const char *name;
David S. Miller942a6bd2006-06-23 15:53:31 -0700368
369 if (tmp) {
370 p = tmp;
371 memset(p, 0, sizeof(*p) + 32);
372 tmp = NULL;
David S. Miller87b385d2006-06-25 23:18:57 -0700373 } else {
David S. Miller942a6bd2006-06-23 15:53:31 -0700374 p = prom_early_alloc(sizeof(struct property) + 32);
David S. Miller87b385d2006-06-25 23:18:57 -0700375 p->unique_id = unique_id++;
376 }
David S. Miller942a6bd2006-06-23 15:53:31 -0700377
378 p->name = (char *) (p + 1);
David S. Miller87b385d2006-06-25 23:18:57 -0700379 if (special_name) {
Bob Breuerf7785a62006-07-17 17:05:56 -0700380 strcpy(p->name, special_name);
David S. Miller87b385d2006-06-25 23:18:57 -0700381 p->length = special_len;
382 p->value = prom_early_alloc(special_len);
383 memcpy(p->value, special_val, special_len);
David S. Miller942a6bd2006-06-23 15:53:31 -0700384 } else {
David S. Miller87b385d2006-06-25 23:18:57 -0700385 if (prev == NULL) {
Bob Breuerf7785a62006-07-17 17:05:56 -0700386 name = prom_firstprop(node, NULL);
David S. Miller87b385d2006-06-25 23:18:57 -0700387 } else {
Bob Breuerf7785a62006-07-17 17:05:56 -0700388 name = prom_nextprop(node, prev, NULL);
David S. Miller87b385d2006-06-25 23:18:57 -0700389 }
Bob Breuerf7785a62006-07-17 17:05:56 -0700390 if (strlen(name) == 0) {
David S. Miller87b385d2006-06-25 23:18:57 -0700391 tmp = p;
392 return NULL;
393 }
Bob Breuerf7785a62006-07-17 17:05:56 -0700394 strcpy(p->name, name);
David S. Miller87b385d2006-06-25 23:18:57 -0700395 p->length = prom_getproplen(node, p->name);
396 if (p->length <= 0) {
397 p->length = 0;
398 } else {
399 p->value = prom_early_alloc(p->length + 1);
Martin Habets078830e2006-10-09 18:10:16 -0700400 len = prom_getproperty(node, p->name, p->value,
401 p->length);
402 if (len <= 0)
403 p->length = 0;
David S. Miller87b385d2006-06-25 23:18:57 -0700404 ((unsigned char *)p->value)[p->length] = '\0';
405 }
David S. Miller942a6bd2006-06-23 15:53:31 -0700406 }
407 return p;
408}
409
410static struct property * __init build_prop_list(phandle node)
411{
412 struct property *head, *tail;
413
David S. Miller87b385d2006-06-25 23:18:57 -0700414 head = tail = build_one_prop(node, NULL,
415 ".node", &node, sizeof(node));
416
417 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
418 tail = tail->next;
David S. Miller942a6bd2006-06-23 15:53:31 -0700419 while(tail) {
David S. Miller87b385d2006-06-25 23:18:57 -0700420 tail->next = build_one_prop(node, tail->name,
421 NULL, NULL, 0);
David S. Miller942a6bd2006-06-23 15:53:31 -0700422 tail = tail->next;
423 }
424
425 return head;
426}
427
428static char * __init get_one_property(phandle node, char *name)
429{
430 char *buf = "<NULL>";
431 int len;
432
433 len = prom_getproplen(node, name);
434 if (len > 0) {
435 buf = prom_early_alloc(len);
436 len = prom_getproperty(node, name, buf, len);
437 }
438
439 return buf;
440}
441
442static struct device_node * __init create_node(phandle node)
443{
444 struct device_node *dp;
445
446 if (!node)
447 return NULL;
448
449 dp = prom_early_alloc(sizeof(*dp));
David S. Miller87b385d2006-06-25 23:18:57 -0700450 dp->unique_id = unique_id++;
David S. Miller942a6bd2006-06-23 15:53:31 -0700451
452 kref_init(&dp->kref);
453
454 dp->name = get_one_property(node, "name");
455 dp->type = get_one_property(node, "device_type");
456 dp->node = node;
457
458 /* Build interrupts later... */
459
460 dp->properties = build_prop_list(node);
461
462 return dp;
463}
464
465static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
466{
467 struct device_node *dp;
468
469 dp = create_node(node);
470 if (dp) {
471 *(*nextp) = dp;
472 *nextp = &dp->allnext;
473
474 dp->parent = parent;
475 dp->path_component_name = build_path_component(dp);
476 dp->full_name = build_full_name(dp);
477
478 dp->child = build_tree(dp, prom_getchild(node), nextp);
479
480 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
481 }
482
483 return dp;
484}
485
486void __init prom_build_devicetree(void)
487{
488 struct device_node **nextp;
489
490 allnodes = create_node(prom_root_node);
491 allnodes->path_component_name = "";
492 allnodes->full_name = "/";
493
494 nextp = &allnodes->allnext;
495 allnodes->child = build_tree(allnodes,
496 prom_getchild(allnodes->node),
497 &nextp);
498 printk("PROM: Built device tree with %u bytes of memory.\n",
499 prom_early_allocated);
500}