]>
Commit | Line | Data |
---|---|---|
942a6bdd DM |
1 | /* |
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 | ||
1ef4d424 | 28 | extern struct device_node *allnodes; /* temporary while merging */ |
942a6bdd | 29 | |
581b605a | 30 | extern rwlock_t devtree_lock; /* temporary while merging */ |
fb7cd9d9 | 31 | |
942a6bdd DM |
32 | struct 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 | } | |
42 | EXPORT_SYMBOL(of_find_node_by_phandle); | |
43 | ||
942a6bdd DM |
44 | int 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 | } | |
55 | EXPORT_SYMBOL(of_getintprop_default); | |
56 | ||
2481d766 DM |
57 | DEFINE_MUTEX(of_set_property_mutex); |
58 | EXPORT_SYMBOL(of_set_property_mutex); | |
59 | ||
fb7cd9d9 DM |
60 | int 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 | ||
a8b8814b | 79 | if (!strcasecmp(prop->name, name)) { |
fb7cd9d9 DM |
80 | void *old_val = prop->value; |
81 | int ret; | |
82 | ||
2481d766 | 83 | mutex_lock(&of_set_property_mutex); |
078830ea | 84 | ret = prom_setprop(dp->node, (char *) name, val, len); |
2481d766 DM |
85 | mutex_unlock(&of_set_property_mutex); |
86 | ||
fb7cd9d9 DM |
87 | 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 | } | |
109 | EXPORT_SYMBOL(of_set_property); | |
110 | ||
46bcea77 DM |
111 | int 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 | } | |
124 | EXPORT_SYMBOL(of_find_in_proplist); | |
125 | ||
942a6bdd DM |
126 | static unsigned int prom_early_allocated; |
127 | ||
128 | static 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 | ||
141 | static 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 | */ | |
166 | static 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" */ | |
182 | static 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]" */ | |
199 | static 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" */ | |
224 | static 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 | ||
240 | static 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 | ||
260 | static 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 | ||
275 | static 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 | ||
87b385da DM |
295 | static unsigned int unique_id; |
296 | ||
297 | static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len) | |
942a6bdd DM |
298 | { |
299 | static struct property *tmp = NULL; | |
300 | struct property *p; | |
301 | int len; | |
f7785a64 | 302 | const char *name; |
942a6bdd DM |
303 | |
304 | if (tmp) { | |
305 | p = tmp; | |
306 | memset(p, 0, sizeof(*p) + 32); | |
307 | tmp = NULL; | |
87b385da | 308 | } else { |
942a6bdd | 309 | p = prom_early_alloc(sizeof(struct property) + 32); |
87b385da DM |
310 | p->unique_id = unique_id++; |
311 | } | |
942a6bdd DM |
312 | |
313 | p->name = (char *) (p + 1); | |
87b385da | 314 | if (special_name) { |
f7785a64 | 315 | strcpy(p->name, special_name); |
87b385da DM |
316 | p->length = special_len; |
317 | p->value = prom_early_alloc(special_len); | |
318 | memcpy(p->value, special_val, special_len); | |
942a6bdd | 319 | } else { |
87b385da | 320 | if (prev == NULL) { |
f7785a64 | 321 | name = prom_firstprop(node, NULL); |
87b385da | 322 | } else { |
f7785a64 | 323 | name = prom_nextprop(node, prev, NULL); |
87b385da | 324 | } |
f7785a64 | 325 | if (strlen(name) == 0) { |
87b385da DM |
326 | tmp = p; |
327 | return NULL; | |
328 | } | |
f7785a64 | 329 | strcpy(p->name, name); |
87b385da DM |
330 | 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); | |
078830ea MH |
335 | len = prom_getproperty(node, p->name, p->value, |
336 | p->length); | |
337 | if (len <= 0) | |
338 | p->length = 0; | |
87b385da DM |
339 | ((unsigned char *)p->value)[p->length] = '\0'; |
340 | } | |
942a6bdd DM |
341 | } |
342 | return p; | |
343 | } | |
344 | ||
345 | static struct property * __init build_prop_list(phandle node) | |
346 | { | |
347 | struct property *head, *tail; | |
348 | ||
87b385da DM |
349 | 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; | |
942a6bdd | 354 | while(tail) { |
87b385da DM |
355 | tail->next = build_one_prop(node, tail->name, |
356 | NULL, NULL, 0); | |
942a6bdd DM |
357 | tail = tail->next; |
358 | } | |
359 | ||
360 | return head; | |
361 | } | |
362 | ||
363 | static 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 | ||
377 | static 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)); | |
87b385da | 385 | dp->unique_id = unique_id++; |
942a6bdd DM |
386 | |
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 | ||
400 | static 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 | ||
c73fcc84 DM |
421 | struct device_node *of_console_device; |
422 | EXPORT_SYMBOL(of_console_device); | |
423 | ||
424 | char *of_console_path; | |
425 | EXPORT_SYMBOL(of_console_path); | |
426 | ||
427 | char *of_console_options; | |
428 | EXPORT_SYMBOL(of_console_options); | |
429 | ||
430 | extern void restore_current(void); | |
431 | ||
432 | static 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; | |
f623f388 | 439 | int skip, tmp, fd; |
c73fcc84 DM |
440 | |
441 | of_console_path = prom_early_alloc(256); | |
442 | ||
443 | switch (prom_vers) { | |
444 | case PROM_V0: | |
c73fcc84 DM |
445 | skip = 0; |
446 | switch (*romvec->pv_stdout) { | |
447 | case PROMDEV_SCREEN: | |
448 | type = "display"; | |
449 | break; | |
450 | ||
451 | case PROMDEV_TTYB: | |
452 | skip = 1; | |
453 | /* FALLTHRU */ | |
454 | ||
455 | case PROMDEV_TTYA: | |
456 | type = "serial"; | |
457 | break; | |
458 | ||
459 | default: | |
460 | prom_printf("Invalid PROM_V0 stdout value %u\n", | |
461 | *romvec->pv_stdout); | |
462 | prom_halt(); | |
463 | } | |
464 | ||
f623f388 | 465 | tmp = skip; |
c73fcc84 | 466 | for_each_node_by_type(dp, type) { |
f623f388 | 467 | if (!tmp--) |
c73fcc84 DM |
468 | break; |
469 | } | |
470 | if (!dp) { | |
471 | prom_printf("Cannot find PROM_V0 console node.\n"); | |
472 | prom_halt(); | |
473 | } | |
474 | of_console_device = dp; | |
475 | ||
476 | strcpy(of_console_path, dp->full_name); | |
477 | if (!strcmp(type, "serial")) { | |
478 | strcat(of_console_path, | |
479 | (skip ? ":b" : ":a")); | |
480 | } | |
481 | break; | |
482 | ||
483 | default: | |
484 | case PROM_V2: | |
485 | case PROM_V3: | |
486 | fd = *romvec->pv_v2bootargs.fd_stdout; | |
487 | ||
488 | spin_lock_irqsave(&prom_lock, flags); | |
489 | node = (*romvec->pv_v2devops.v2_inst2pkg)(fd); | |
490 | restore_current(); | |
491 | spin_unlock_irqrestore(&prom_lock, flags); | |
492 | ||
493 | if (!node) { | |
494 | prom_printf("Cannot resolve stdout node from " | |
495 | "instance %08x.\n", fd); | |
496 | prom_halt(); | |
497 | } | |
498 | dp = of_find_node_by_phandle(node); | |
499 | type = of_get_property(dp, "device_type", NULL); | |
500 | ||
501 | if (!type) { | |
502 | prom_printf("Console stdout lacks " | |
503 | "device_type property.\n"); | |
504 | prom_halt(); | |
505 | } | |
506 | ||
507 | if (strcmp(type, "display") && strcmp(type, "serial")) { | |
508 | prom_printf("Console device_type is neither display " | |
509 | "nor serial.\n"); | |
510 | prom_halt(); | |
511 | } | |
512 | ||
513 | of_console_device = dp; | |
514 | ||
515 | if (prom_vers == PROM_V2) { | |
516 | strcpy(of_console_path, dp->full_name); | |
517 | switch (*romvec->pv_stdout) { | |
518 | case PROMDEV_TTYA: | |
519 | strcat(of_console_path, ":a"); | |
520 | break; | |
521 | case PROMDEV_TTYB: | |
522 | strcat(of_console_path, ":b"); | |
523 | break; | |
524 | } | |
525 | } else { | |
526 | const char *path; | |
527 | ||
528 | dp = of_find_node_by_path("/"); | |
529 | path = of_get_property(dp, "stdout-path", NULL); | |
530 | if (!path) { | |
531 | prom_printf("No stdout-path in root node.\n"); | |
532 | prom_halt(); | |
533 | } | |
534 | strcpy(of_console_path, path); | |
535 | } | |
536 | break; | |
537 | } | |
538 | ||
539 | of_console_options = strrchr(of_console_path, ':'); | |
540 | if (of_console_options) { | |
541 | of_console_options++; | |
542 | if (*of_console_options == '\0') | |
543 | of_console_options = NULL; | |
544 | } | |
545 | ||
546 | prom_printf(msg, of_console_path); | |
547 | printk(msg, of_console_path); | |
548 | } | |
549 | ||
942a6bdd DM |
550 | void __init prom_build_devicetree(void) |
551 | { | |
552 | struct device_node **nextp; | |
553 | ||
554 | allnodes = create_node(prom_root_node); | |
555 | allnodes->path_component_name = ""; | |
556 | allnodes->full_name = "/"; | |
557 | ||
558 | nextp = &allnodes->allnext; | |
559 | allnodes->child = build_tree(allnodes, | |
560 | prom_getchild(allnodes->node), | |
561 | &nextp); | |
c73fcc84 DM |
562 | of_console_init(); |
563 | ||
942a6bdd DM |
564 | printk("PROM: Built device tree with %u bytes of memory.\n", |
565 | prom_early_allocated); | |
566 | } |