powerpc: Remove default kexec/crash_kernel ops assignments
[linux-2.6/x86.git] / arch / sparc / kernel / prom.c
blobeee5efcfe50eea300a42d43e887bf4a7b5a54c6a
1 /*
2 * Procedures for creating, accessing and interpreting the device tree.
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
10 * Adapted for sparc32 by David S. Miller davem@davemloft.net
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.
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>
25 #include <asm/prom.h>
26 #include <asm/oplib.h>
28 extern struct device_node *allnodes; /* temporary while merging */
30 extern rwlock_t devtree_lock; /* temporary while merging */
32 struct device_node *of_find_node_by_phandle(phandle handle)
34 struct device_node *np;
36 for (np = allnodes; np != 0; np = np->allnext)
37 if (np->node == handle)
38 break;
40 return np;
42 EXPORT_SYMBOL(of_find_node_by_phandle);
44 int of_getintprop_default(struct device_node *np, const char *name, int def)
46 struct property *prop;
47 int len;
49 prop = of_find_property(np, name, &len);
50 if (!prop || len != 4)
51 return def;
53 return *(int *) prop->value;
55 EXPORT_SYMBOL(of_getintprop_default);
57 DEFINE_MUTEX(of_set_property_mutex);
58 EXPORT_SYMBOL(of_set_property_mutex);
60 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
62 struct property **prevp;
63 void *new_val;
64 int err;
66 new_val = kmalloc(len, GFP_KERNEL);
67 if (!new_val)
68 return -ENOMEM;
70 memcpy(new_val, val, len);
72 err = -ENODEV;
74 write_lock(&devtree_lock);
75 prevp = &dp->properties;
76 while (*prevp) {
77 struct property *prop = *prevp;
79 if (!strcasecmp(prop->name, name)) {
80 void *old_val = prop->value;
81 int ret;
83 mutex_lock(&of_set_property_mutex);
84 ret = prom_setprop(dp->node, (char *) name, val, len);
85 mutex_unlock(&of_set_property_mutex);
87 err = -EINVAL;
88 if (ret >= 0) {
89 prop->value = new_val;
90 prop->length = len;
92 if (OF_IS_DYNAMIC(prop))
93 kfree(old_val);
95 OF_MARK_DYNAMIC(prop);
97 err = 0;
99 break;
101 prevp = &(*prevp)->next;
103 write_unlock(&devtree_lock);
105 /* XXX Upate procfs if necessary... */
107 return err;
109 EXPORT_SYMBOL(of_set_property);
111 int of_find_in_proplist(const char *list, const char *match, int len)
113 while (len > 0) {
114 int l;
116 if (!strcmp(list, match))
117 return 1;
118 l = strlen(list) + 1;
119 list += l;
120 len -= l;
122 return 0;
124 EXPORT_SYMBOL(of_find_in_proplist);
126 static unsigned int prom_early_allocated;
128 static void * __init prom_early_alloc(unsigned long size)
130 void *ret;
132 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
133 if (ret != NULL)
134 memset(ret, 0, size);
136 prom_early_allocated += size;
138 return ret;
141 static int is_root_node(const struct device_node *dp)
143 if (!dp)
144 return 0;
146 return (dp->parent == NULL);
149 /* The following routines deal with the black magic of fully naming a
150 * node.
152 * Certain well known named nodes are just the simple name string.
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.
159 * For children of the ROOT node, the naming convention is fixed and
160 * determined by whether this is a sun4u or sun4v system.
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.
166 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
168 struct linux_prom_registers *regs;
169 struct property *rprop;
171 rprop = of_find_property(dp, "reg", NULL);
172 if (!rprop)
173 return;
175 regs = rprop->value;
176 sprintf(tmp_buf, "%s@%x,%x",
177 dp->name,
178 regs->which_io, regs->phys_addr);
181 /* "name@slot,offset" */
182 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
184 struct linux_prom_registers *regs;
185 struct property *prop;
187 prop = of_find_property(dp, "reg", NULL);
188 if (!prop)
189 return;
191 regs = prop->value;
192 sprintf(tmp_buf, "%s@%x,%x",
193 dp->name,
194 regs->which_io,
195 regs->phys_addr);
198 /* "name@devnum[,func]" */
199 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
201 struct linux_prom_pci_registers *regs;
202 struct property *prop;
203 unsigned int devfn;
205 prop = of_find_property(dp, "reg", NULL);
206 if (!prop)
207 return;
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);
223 /* "name@addrhi,addrlo" */
224 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
226 struct linux_prom_registers *regs;
227 struct property *prop;
229 prop = of_find_property(dp, "reg", NULL);
230 if (!prop)
231 return;
233 regs = prop->value;
235 sprintf(tmp_buf, "%s@%x,%x",
236 dp->name,
237 regs->which_io, regs->phys_addr);
240 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
242 struct device_node *parent = dp->parent;
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);
253 /* "isa" is handled with platform naming */
256 /* Use platform naming convention. */
257 return sparc32_path_component(dp, tmp_buf);
260 static char * __init build_path_component(struct device_node *dp)
262 char tmp_buf[64], *n;
264 tmp_buf[0] = '\0';
265 __build_path_component(dp, tmp_buf);
266 if (tmp_buf[0] == '\0')
267 strcpy(tmp_buf, dp->name);
269 n = prom_early_alloc(strlen(tmp_buf) + 1);
270 strcpy(n, tmp_buf);
272 return n;
275 static char * __init build_full_name(struct device_node *dp)
277 int len, ourlen, plen;
278 char *n;
280 plen = strlen(dp->parent->full_name);
281 ourlen = strlen(dp->path_component_name);
282 len = ourlen + plen + 2;
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++;
290 strcpy(n + plen, dp->path_component_name);
292 return n;
295 static unsigned int unique_id;
297 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
299 static struct property *tmp = NULL;
300 struct property *p;
301 int len;
302 const char *name;
304 if (tmp) {
305 p = tmp;
306 memset(p, 0, sizeof(*p) + 32);
307 tmp = NULL;
308 } else {
309 p = prom_early_alloc(sizeof(struct property) + 32);
310 p->unique_id = unique_id++;
313 p->name = (char *) (p + 1);
314 if (special_name) {
315 strcpy(p->name, special_name);
316 p->length = special_len;
317 p->value = prom_early_alloc(special_len);
318 memcpy(p->value, special_val, special_len);
319 } else {
320 if (prev == NULL) {
321 name = prom_firstprop(node, NULL);
322 } else {
323 name = prom_nextprop(node, prev, NULL);
325 if (strlen(name) == 0) {
326 tmp = p;
327 return NULL;
329 strcpy(p->name, name);
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);
335 len = prom_getproperty(node, p->name, p->value,
336 p->length);
337 if (len <= 0)
338 p->length = 0;
339 ((unsigned char *)p->value)[p->length] = '\0';
342 return p;
345 static struct property * __init build_prop_list(phandle node)
347 struct property *head, *tail;
349 head = tail = build_one_prop(node, NULL,
350 ".node", &node, sizeof(node));
352 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
353 tail = tail->next;
354 while(tail) {
355 tail->next = build_one_prop(node, tail->name,
356 NULL, NULL, 0);
357 tail = tail->next;
360 return head;
363 static char * __init get_one_property(phandle node, char *name)
365 char *buf = "<NULL>";
366 int len;
368 len = prom_getproplen(node, name);
369 if (len > 0) {
370 buf = prom_early_alloc(len);
371 len = prom_getproperty(node, name, buf, len);
374 return buf;
377 static struct device_node * __init create_node(phandle node)
379 struct device_node *dp;
381 if (!node)
382 return NULL;
384 dp = prom_early_alloc(sizeof(*dp));
385 dp->unique_id = unique_id++;
387 kref_init(&dp->kref);
389 dp->name = get_one_property(node, "name");
390 dp->type = get_one_property(node, "device_type");
391 dp->node = node;
393 /* Build interrupts later... */
395 dp->properties = build_prop_list(node);
397 return dp;
400 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
402 struct device_node *dp;
404 dp = create_node(node);
405 if (dp) {
406 *(*nextp) = dp;
407 *nextp = &dp->allnext;
409 dp->parent = parent;
410 dp->path_component_name = build_path_component(dp);
411 dp->full_name = build_full_name(dp);
413 dp->child = build_tree(dp, prom_getchild(node), nextp);
415 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
418 return dp;
421 struct device_node *of_console_device;
422 EXPORT_SYMBOL(of_console_device);
424 char *of_console_path;
425 EXPORT_SYMBOL(of_console_path);
427 char *of_console_options;
428 EXPORT_SYMBOL(of_console_options);
430 extern void restore_current(void);
432 static void __init of_console_init(void)
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;
439 int skip, tmp, fd;
441 of_console_path = prom_early_alloc(256);
443 switch (prom_vers) {
444 case PROM_V0:
445 skip = 0;
446 switch (*romvec->pv_stdout) {
447 case PROMDEV_SCREEN:
448 type = "display";
449 break;
451 case PROMDEV_TTYB:
452 skip = 1;
453 /* FALLTHRU */
455 case PROMDEV_TTYA:
456 type = "serial";
457 break;
459 default:
460 prom_printf("Invalid PROM_V0 stdout value %u\n",
461 *romvec->pv_stdout);
462 prom_halt();
465 tmp = skip;
466 for_each_node_by_type(dp, type) {
467 if (!tmp--)
468 break;
470 if (!dp) {
471 prom_printf("Cannot find PROM_V0 console node.\n");
472 prom_halt();
474 of_console_device = dp;
476 strcpy(of_console_path, dp->full_name);
477 if (!strcmp(type, "serial")) {
478 strcat(of_console_path,
479 (skip ? ":b" : ":a"));
481 break;
483 default:
484 case PROM_V2:
485 case PROM_V3:
486 fd = *romvec->pv_v2bootargs.fd_stdout;
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);
493 if (!node) {
494 prom_printf("Cannot resolve stdout node from "
495 "instance %08x.\n", fd);
496 prom_halt();
498 dp = of_find_node_by_phandle(node);
499 type = of_get_property(dp, "device_type", NULL);
501 if (!type) {
502 prom_printf("Console stdout lacks "
503 "device_type property.\n");
504 prom_halt();
507 if (strcmp(type, "display") && strcmp(type, "serial")) {
508 prom_printf("Console device_type is neither display "
509 "nor serial.\n");
510 prom_halt();
513 of_console_device = dp;
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;
525 } else {
526 const char *path;
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();
534 strcpy(of_console_path, path);
536 break;
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;
546 prom_printf(msg, of_console_path);
547 printk(msg, of_console_path);
550 void __init prom_build_devicetree(void)
552 struct device_node **nextp;
554 allnodes = create_node(prom_root_node);
555 allnodes->path_component_name = "";
556 allnodes->full_name = "/";
558 nextp = &allnodes->allnext;
559 allnodes->child = build_tree(allnodes,
560 prom_getchild(allnodes->node),
561 &nextp);
562 of_console_init();
564 printk("PROM: Built device tree with %u bytes of memory.\n",
565 prom_early_allocated);