blob: 43e862406822c299ba3e25d0f28bb539eb646f37 [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
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100028extern struct device_node *allnodes; /* temporary while merging */
David S. Miller942a6bd2006-06-23 15:53:31 -070029
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_find_node_by_phandle(phandle handle)
33{
34 struct device_node *np;
35
36 for (np = allnodes; np != 0; np = np->allnext)
37 if (np->node == handle)
38 break;
39
40 return np;
41}
42EXPORT_SYMBOL(of_find_node_by_phandle);
43
David S. Miller942a6bd2006-06-23 15:53:31 -070044int of_getintprop_default(struct device_node *np, const char *name, int def)
45{
46 struct property *prop;
47 int len;
48
49 prop = of_find_property(np, name, &len);
50 if (!prop || len != 4)
51 return def;
52
53 return *(int *) prop->value;
54}
55EXPORT_SYMBOL(of_getintprop_default);
56
David S. Miller2481d762008-08-19 21:56:35 -070057DEFINE_MUTEX(of_set_property_mutex);
58EXPORT_SYMBOL(of_set_property_mutex);
59
David S. Millerfb7cd9d2006-06-25 23:18:36 -070060int of_set_property(struct device_node *dp, const char *name, void *val, int len)
61{
62 struct property **prevp;
63 void *new_val;
64 int err;
65
66 new_val = kmalloc(len, GFP_KERNEL);
67 if (!new_val)
68 return -ENOMEM;
69
70 memcpy(new_val, val, len);
71
72 err = -ENODEV;
73
74 write_lock(&devtree_lock);
75 prevp = &dp->properties;
76 while (*prevp) {
77 struct property *prop = *prevp;
78
David S. Millera8b88142007-03-29 01:28:51 -070079 if (!strcasecmp(prop->name, name)) {
David S. Millerfb7cd9d2006-06-25 23:18:36 -070080 void *old_val = prop->value;
81 int ret;
82
David S. Miller2481d762008-08-19 21:56:35 -070083 mutex_lock(&of_set_property_mutex);
Martin Habets078830e2006-10-09 18:10:16 -070084 ret = prom_setprop(dp->node, (char *) name, val, len);
David S. Miller2481d762008-08-19 21:56:35 -070085 mutex_unlock(&of_set_property_mutex);
86
David S. Millerfb7cd9d2006-06-25 23:18:36 -070087 err = -EINVAL;
88 if (ret >= 0) {
89 prop->value = new_val;
90 prop->length = len;
91
92 if (OF_IS_DYNAMIC(prop))
93 kfree(old_val);
94
95 OF_MARK_DYNAMIC(prop);
96
97 err = 0;
98 }
99 break;
100 }
101 prevp = &(*prevp)->next;
102 }
103 write_unlock(&devtree_lock);
104
105 /* XXX Upate procfs if necessary... */
106
107 return err;
108}
109EXPORT_SYMBOL(of_set_property);
110
David S. Miller46bcea72007-08-07 18:46:36 -0700111int of_find_in_proplist(const char *list, const char *match, int len)
112{
113 while (len > 0) {
114 int l;
115
116 if (!strcmp(list, match))
117 return 1;
118 l = strlen(list) + 1;
119 list += l;
120 len -= l;
121 }
122 return 0;
123}
124EXPORT_SYMBOL(of_find_in_proplist);
125
David S. Miller942a6bd2006-06-23 15:53:31 -0700126static unsigned int prom_early_allocated;
127
128static void * __init prom_early_alloc(unsigned long size)
129{
130 void *ret;
131
132 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
133 if (ret != NULL)
134 memset(ret, 0, size);
135
136 prom_early_allocated += size;
137
138 return ret;
139}
140
141static int is_root_node(const struct device_node *dp)
142{
143 if (!dp)
144 return 0;
145
146 return (dp->parent == NULL);
147}
148
149/* The following routines deal with the black magic of fully naming a
150 * node.
151 *
152 * Certain well known named nodes are just the simple name string.
153 *
154 * Actual devices have an address specifier appended to the base name
155 * string, like this "foo@addr". The "addr" can be in any number of
156 * formats, and the platform plus the type of the node determine the
157 * format and how it is constructed.
158 *
159 * For children of the ROOT node, the naming convention is fixed and
160 * determined by whether this is a sun4u or sun4v system.
161 *
162 * For children of other nodes, it is bus type specific. So
163 * we walk up the tree until we discover a "device_type" property
164 * we recognize and we go from there.
165 */
166static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
167{
168 struct linux_prom_registers *regs;
169 struct property *rprop;
170
171 rprop = of_find_property(dp, "reg", NULL);
172 if (!rprop)
173 return;
174
175 regs = rprop->value;
176 sprintf(tmp_buf, "%s@%x,%x",
177 dp->name,
178 regs->which_io, regs->phys_addr);
179}
180
181/* "name@slot,offset" */
182static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
183{
184 struct linux_prom_registers *regs;
185 struct property *prop;
186
187 prop = of_find_property(dp, "reg", NULL);
188 if (!prop)
189 return;
190
191 regs = prop->value;
192 sprintf(tmp_buf, "%s@%x,%x",
193 dp->name,
194 regs->which_io,
195 regs->phys_addr);
196}
197
198/* "name@devnum[,func]" */
199static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
200{
201 struct linux_prom_pci_registers *regs;
202 struct property *prop;
203 unsigned int devfn;
204
205 prop = of_find_property(dp, "reg", NULL);
206 if (!prop)
207 return;
208
209 regs = prop->value;
210 devfn = (regs->phys_hi >> 8) & 0xff;
211 if (devfn & 0x07) {
212 sprintf(tmp_buf, "%s@%x,%x",
213 dp->name,
214 devfn >> 3,
215 devfn & 0x07);
216 } else {
217 sprintf(tmp_buf, "%s@%x",
218 dp->name,
219 devfn >> 3);
220 }
221}
222
223/* "name@addrhi,addrlo" */
224static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
225{
226 struct linux_prom_registers *regs;
227 struct property *prop;
228
229 prop = of_find_property(dp, "reg", NULL);
230 if (!prop)
231 return;
232
233 regs = prop->value;
234
235 sprintf(tmp_buf, "%s@%x,%x",
236 dp->name,
237 regs->which_io, regs->phys_addr);
238}
239
240static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
241{
242 struct device_node *parent = dp->parent;
243
244 if (parent != NULL) {
245 if (!strcmp(parent->type, "pci") ||
246 !strcmp(parent->type, "pciex"))
247 return pci_path_component(dp, tmp_buf);
248 if (!strcmp(parent->type, "sbus"))
249 return sbus_path_component(dp, tmp_buf);
250 if (!strcmp(parent->type, "ebus"))
251 return ebus_path_component(dp, tmp_buf);
252
253 /* "isa" is handled with platform naming */
254 }
255
256 /* Use platform naming convention. */
257 return sparc32_path_component(dp, tmp_buf);
258}
259
260static char * __init build_path_component(struct device_node *dp)
261{
262 char tmp_buf[64], *n;
263
264 tmp_buf[0] = '\0';
265 __build_path_component(dp, tmp_buf);
266 if (tmp_buf[0] == '\0')
267 strcpy(tmp_buf, dp->name);
268
269 n = prom_early_alloc(strlen(tmp_buf) + 1);
270 strcpy(n, tmp_buf);
271
272 return n;
273}
274
275static char * __init build_full_name(struct device_node *dp)
276{
277 int len, ourlen, plen;
278 char *n;
279
280 plen = strlen(dp->parent->full_name);
281 ourlen = strlen(dp->path_component_name);
282 len = ourlen + plen + 2;
283
284 n = prom_early_alloc(len);
285 strcpy(n, dp->parent->full_name);
286 if (!is_root_node(dp->parent)) {
287 strcpy(n + plen, "/");
288 plen++;
289 }
290 strcpy(n + plen, dp->path_component_name);
291
292 return n;
293}
294
David S. Miller87b385d2006-06-25 23:18:57 -0700295static unsigned int unique_id;
296
297static 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 -0700298{
299 static struct property *tmp = NULL;
300 struct property *p;
301 int len;
Bob Breuerf7785a62006-07-17 17:05:56 -0700302 const char *name;
David S. Miller942a6bd2006-06-23 15:53:31 -0700303
304 if (tmp) {
305 p = tmp;
306 memset(p, 0, sizeof(*p) + 32);
307 tmp = NULL;
David S. Miller87b385d2006-06-25 23:18:57 -0700308 } else {
David S. Miller942a6bd2006-06-23 15:53:31 -0700309 p = prom_early_alloc(sizeof(struct property) + 32);
David S. Miller87b385d2006-06-25 23:18:57 -0700310 p->unique_id = unique_id++;
311 }
David S. Miller942a6bd2006-06-23 15:53:31 -0700312
313 p->name = (char *) (p + 1);
David S. Miller87b385d2006-06-25 23:18:57 -0700314 if (special_name) {
Bob Breuerf7785a62006-07-17 17:05:56 -0700315 strcpy(p->name, special_name);
David S. Miller87b385d2006-06-25 23:18:57 -0700316 p->length = special_len;
317 p->value = prom_early_alloc(special_len);
318 memcpy(p->value, special_val, special_len);
David S. Miller942a6bd2006-06-23 15:53:31 -0700319 } else {
David S. Miller87b385d2006-06-25 23:18:57 -0700320 if (prev == NULL) {
Bob Breuerf7785a62006-07-17 17:05:56 -0700321 name = prom_firstprop(node, NULL);
David S. Miller87b385d2006-06-25 23:18:57 -0700322 } else {
Bob Breuerf7785a62006-07-17 17:05:56 -0700323 name = prom_nextprop(node, prev, NULL);
David S. Miller87b385d2006-06-25 23:18:57 -0700324 }
Bob Breuerf7785a62006-07-17 17:05:56 -0700325 if (strlen(name) == 0) {
David S. Miller87b385d2006-06-25 23:18:57 -0700326 tmp = p;
327 return NULL;
328 }
Bob Breuerf7785a62006-07-17 17:05:56 -0700329 strcpy(p->name, name);
David S. Miller87b385d2006-06-25 23:18:57 -0700330 p->length = prom_getproplen(node, p->name);
331 if (p->length <= 0) {
332 p->length = 0;
333 } else {
334 p->value = prom_early_alloc(p->length + 1);
Martin Habets078830e2006-10-09 18:10:16 -0700335 len = prom_getproperty(node, p->name, p->value,
336 p->length);
337 if (len <= 0)
338 p->length = 0;
David S. Miller87b385d2006-06-25 23:18:57 -0700339 ((unsigned char *)p->value)[p->length] = '\0';
340 }
David S. Miller942a6bd2006-06-23 15:53:31 -0700341 }
342 return p;
343}
344
345static struct property * __init build_prop_list(phandle node)
346{
347 struct property *head, *tail;
348
David S. Miller87b385d2006-06-25 23:18:57 -0700349 head = tail = build_one_prop(node, NULL,
350 ".node", &node, sizeof(node));
351
352 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
353 tail = tail->next;
David S. Miller942a6bd2006-06-23 15:53:31 -0700354 while(tail) {
David S. Miller87b385d2006-06-25 23:18:57 -0700355 tail->next = build_one_prop(node, tail->name,
356 NULL, NULL, 0);
David S. Miller942a6bd2006-06-23 15:53:31 -0700357 tail = tail->next;
358 }
359
360 return head;
361}
362
363static char * __init get_one_property(phandle node, char *name)
364{
365 char *buf = "<NULL>";
366 int len;
367
368 len = prom_getproplen(node, name);
369 if (len > 0) {
370 buf = prom_early_alloc(len);
371 len = prom_getproperty(node, name, buf, len);
372 }
373
374 return buf;
375}
376
377static struct device_node * __init create_node(phandle node)
378{
379 struct device_node *dp;
380
381 if (!node)
382 return NULL;
383
384 dp = prom_early_alloc(sizeof(*dp));
David S. Miller87b385d2006-06-25 23:18:57 -0700385 dp->unique_id = unique_id++;
David S. Miller942a6bd2006-06-23 15:53:31 -0700386
387 kref_init(&dp->kref);
388
389 dp->name = get_one_property(node, "name");
390 dp->type = get_one_property(node, "device_type");
391 dp->node = node;
392
393 /* Build interrupts later... */
394
395 dp->properties = build_prop_list(node);
396
397 return dp;
398}
399
400static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
401{
402 struct device_node *dp;
403
404 dp = create_node(node);
405 if (dp) {
406 *(*nextp) = dp;
407 *nextp = &dp->allnext;
408
409 dp->parent = parent;
410 dp->path_component_name = build_path_component(dp);
411 dp->full_name = build_full_name(dp);
412
413 dp->child = build_tree(dp, prom_getchild(node), nextp);
414
415 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
416 }
417
418 return dp;
419}
420
David S. Millerc73fcc82007-07-20 16:59:26 -0700421struct device_node *of_console_device;
422EXPORT_SYMBOL(of_console_device);
423
424char *of_console_path;
425EXPORT_SYMBOL(of_console_path);
426
427char *of_console_options;
428EXPORT_SYMBOL(of_console_options);
429
430extern void restore_current(void);
431
432static void __init of_console_init(void)
433{
434 char *msg = "OF stdout device is: %s\n";
435 struct device_node *dp;
436 unsigned long flags;
437 const char *type;
438 phandle node;
David S. Millerf623f382007-07-29 02:10:37 -0700439 int skip, tmp, fd;
David S. Millerc73fcc82007-07-20 16:59:26 -0700440
441 of_console_path = prom_early_alloc(256);
442
443 switch (prom_vers) {
444 case PROM_V0:
445 case PROM_SUN4:
446 skip = 0;
447 switch (*romvec->pv_stdout) {
448 case PROMDEV_SCREEN:
449 type = "display";
450 break;
451
452 case PROMDEV_TTYB:
453 skip = 1;
454 /* FALLTHRU */
455
456 case PROMDEV_TTYA:
457 type = "serial";
458 break;
459
460 default:
461 prom_printf("Invalid PROM_V0 stdout value %u\n",
462 *romvec->pv_stdout);
463 prom_halt();
464 }
465
David S. Millerf623f382007-07-29 02:10:37 -0700466 tmp = skip;
David S. Millerc73fcc82007-07-20 16:59:26 -0700467 for_each_node_by_type(dp, type) {
David S. Millerf623f382007-07-29 02:10:37 -0700468 if (!tmp--)
David S. Millerc73fcc82007-07-20 16:59:26 -0700469 break;
470 }
471 if (!dp) {
472 prom_printf("Cannot find PROM_V0 console node.\n");
473 prom_halt();
474 }
475 of_console_device = dp;
476
477 strcpy(of_console_path, dp->full_name);
478 if (!strcmp(type, "serial")) {
479 strcat(of_console_path,
480 (skip ? ":b" : ":a"));
481 }
482 break;
483
484 default:
485 case PROM_V2:
486 case PROM_V3:
487 fd = *romvec->pv_v2bootargs.fd_stdout;
488
489 spin_lock_irqsave(&prom_lock, flags);
490 node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
491 restore_current();
492 spin_unlock_irqrestore(&prom_lock, flags);
493
494 if (!node) {
495 prom_printf("Cannot resolve stdout node from "
496 "instance %08x.\n", fd);
497 prom_halt();
498 }
499 dp = of_find_node_by_phandle(node);
500 type = of_get_property(dp, "device_type", NULL);
501
502 if (!type) {
503 prom_printf("Console stdout lacks "
504 "device_type property.\n");
505 prom_halt();
506 }
507
508 if (strcmp(type, "display") && strcmp(type, "serial")) {
509 prom_printf("Console device_type is neither display "
510 "nor serial.\n");
511 prom_halt();
512 }
513
514 of_console_device = dp;
515
516 if (prom_vers == PROM_V2) {
517 strcpy(of_console_path, dp->full_name);
518 switch (*romvec->pv_stdout) {
519 case PROMDEV_TTYA:
520 strcat(of_console_path, ":a");
521 break;
522 case PROMDEV_TTYB:
523 strcat(of_console_path, ":b");
524 break;
525 }
526 } else {
527 const char *path;
528
529 dp = of_find_node_by_path("/");
530 path = of_get_property(dp, "stdout-path", NULL);
531 if (!path) {
532 prom_printf("No stdout-path in root node.\n");
533 prom_halt();
534 }
535 strcpy(of_console_path, path);
536 }
537 break;
538 }
539
540 of_console_options = strrchr(of_console_path, ':');
541 if (of_console_options) {
542 of_console_options++;
543 if (*of_console_options == '\0')
544 of_console_options = NULL;
545 }
546
547 prom_printf(msg, of_console_path);
548 printk(msg, of_console_path);
549}
550
David S. Miller942a6bd2006-06-23 15:53:31 -0700551void __init prom_build_devicetree(void)
552{
553 struct device_node **nextp;
554
555 allnodes = create_node(prom_root_node);
556 allnodes->path_component_name = "";
557 allnodes->full_name = "/";
558
559 nextp = &allnodes->allnext;
560 allnodes->child = build_tree(allnodes,
561 prom_getchild(allnodes->node),
562 &nextp);
David S. Millerc73fcc82007-07-20 16:59:26 -0700563 of_console_init();
564
David S. Miller942a6bd2006-06-23 15:53:31 -0700565 printk("PROM: Built device tree with %u bytes of memory.\n",
566 prom_early_allocated);
567}