[SPARC]: Fix serial console device detection.
[linux-2.6.git] / arch / sparc / kernel / prom.c
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
28 extern struct device_node *allnodes;    /* temporary while merging */
29
30 extern rwlock_t devtree_lock;   /* temporary while merging */
31
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
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
57 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
58 {
59         struct property **prevp;
60         void *new_val;
61         int err;
62
63         new_val = kmalloc(len, GFP_KERNEL);
64         if (!new_val)
65                 return -ENOMEM;
66
67         memcpy(new_val, val, len);
68
69         err = -ENODEV;
70
71         write_lock(&devtree_lock);
72         prevp = &dp->properties;
73         while (*prevp) {
74                 struct property *prop = *prevp;
75
76                 if (!strcasecmp(prop->name, name)) {
77                         void *old_val = prop->value;
78                         int ret;
79
80                         ret = prom_setprop(dp->node, (char *) name, val, len);
81                         err = -EINVAL;
82                         if (ret >= 0) {
83                                 prop->value = new_val;
84                                 prop->length = len;
85
86                                 if (OF_IS_DYNAMIC(prop))
87                                         kfree(old_val);
88
89                                 OF_MARK_DYNAMIC(prop);
90
91                                 err = 0;
92                         }
93                         break;
94                 }
95                 prevp = &(*prevp)->next;
96         }
97         write_unlock(&devtree_lock);
98
99         /* XXX Upate procfs if necessary... */
100
101         return err;
102 }
103 EXPORT_SYMBOL(of_set_property);
104
105 static unsigned int prom_early_allocated;
106
107 static void * __init prom_early_alloc(unsigned long size)
108 {
109         void *ret;
110
111         ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
112         if (ret != NULL)
113                 memset(ret, 0, size);
114
115         prom_early_allocated += size;
116
117         return ret;
118 }
119
120 static int is_root_node(const struct device_node *dp)
121 {
122         if (!dp)
123                 return 0;
124
125         return (dp->parent == NULL);
126 }
127
128 /* The following routines deal with the black magic of fully naming a
129  * node.
130  *
131  * Certain well known named nodes are just the simple name string.
132  *
133  * Actual devices have an address specifier appended to the base name
134  * string, like this "foo@addr".  The "addr" can be in any number of
135  * formats, and the platform plus the type of the node determine the
136  * format and how it is constructed.
137  *
138  * For children of the ROOT node, the naming convention is fixed and
139  * determined by whether this is a sun4u or sun4v system.
140  *
141  * For children of other nodes, it is bus type specific.  So
142  * we walk up the tree until we discover a "device_type" property
143  * we recognize and we go from there.
144  */
145 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
146 {
147         struct linux_prom_registers *regs;
148         struct property *rprop;
149
150         rprop = of_find_property(dp, "reg", NULL);
151         if (!rprop)
152                 return;
153
154         regs = rprop->value;
155         sprintf(tmp_buf, "%s@%x,%x",
156                 dp->name,
157                 regs->which_io, regs->phys_addr);
158 }
159
160 /* "name@slot,offset"  */
161 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
162 {
163         struct linux_prom_registers *regs;
164         struct property *prop;
165
166         prop = of_find_property(dp, "reg", NULL);
167         if (!prop)
168                 return;
169
170         regs = prop->value;
171         sprintf(tmp_buf, "%s@%x,%x",
172                 dp->name,
173                 regs->which_io,
174                 regs->phys_addr);
175 }
176
177 /* "name@devnum[,func]" */
178 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
179 {
180         struct linux_prom_pci_registers *regs;
181         struct property *prop;
182         unsigned int devfn;
183
184         prop = of_find_property(dp, "reg", NULL);
185         if (!prop)
186                 return;
187
188         regs = prop->value;
189         devfn = (regs->phys_hi >> 8) & 0xff;
190         if (devfn & 0x07) {
191                 sprintf(tmp_buf, "%s@%x,%x",
192                         dp->name,
193                         devfn >> 3,
194                         devfn & 0x07);
195         } else {
196                 sprintf(tmp_buf, "%s@%x",
197                         dp->name,
198                         devfn >> 3);
199         }
200 }
201
202 /* "name@addrhi,addrlo" */
203 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
204 {
205         struct linux_prom_registers *regs;
206         struct property *prop;
207
208         prop = of_find_property(dp, "reg", NULL);
209         if (!prop)
210                 return;
211
212         regs = prop->value;
213
214         sprintf(tmp_buf, "%s@%x,%x",
215                 dp->name,
216                 regs->which_io, regs->phys_addr);
217 }
218
219 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
220 {
221         struct device_node *parent = dp->parent;
222
223         if (parent != NULL) {
224                 if (!strcmp(parent->type, "pci") ||
225                     !strcmp(parent->type, "pciex"))
226                         return pci_path_component(dp, tmp_buf);
227                 if (!strcmp(parent->type, "sbus"))
228                         return sbus_path_component(dp, tmp_buf);
229                 if (!strcmp(parent->type, "ebus"))
230                         return ebus_path_component(dp, tmp_buf);
231
232                 /* "isa" is handled with platform naming */
233         }
234
235         /* Use platform naming convention.  */
236         return sparc32_path_component(dp, tmp_buf);
237 }
238
239 static char * __init build_path_component(struct device_node *dp)
240 {
241         char tmp_buf[64], *n;
242
243         tmp_buf[0] = '\0';
244         __build_path_component(dp, tmp_buf);
245         if (tmp_buf[0] == '\0')
246                 strcpy(tmp_buf, dp->name);
247
248         n = prom_early_alloc(strlen(tmp_buf) + 1);
249         strcpy(n, tmp_buf);
250
251         return n;
252 }
253
254 static char * __init build_full_name(struct device_node *dp)
255 {
256         int len, ourlen, plen;
257         char *n;
258
259         plen = strlen(dp->parent->full_name);
260         ourlen = strlen(dp->path_component_name);
261         len = ourlen + plen + 2;
262
263         n = prom_early_alloc(len);
264         strcpy(n, dp->parent->full_name);
265         if (!is_root_node(dp->parent)) {
266                 strcpy(n + plen, "/");
267                 plen++;
268         }
269         strcpy(n + plen, dp->path_component_name);
270
271         return n;
272 }
273
274 static unsigned int unique_id;
275
276 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
277 {
278         static struct property *tmp = NULL;
279         struct property *p;
280         int len;
281         const char *name;
282
283         if (tmp) {
284                 p = tmp;
285                 memset(p, 0, sizeof(*p) + 32);
286                 tmp = NULL;
287         } else {
288                 p = prom_early_alloc(sizeof(struct property) + 32);
289                 p->unique_id = unique_id++;
290         }
291
292         p->name = (char *) (p + 1);
293         if (special_name) {
294                 strcpy(p->name, special_name);
295                 p->length = special_len;
296                 p->value = prom_early_alloc(special_len);
297                 memcpy(p->value, special_val, special_len);
298         } else {
299                 if (prev == NULL) {
300                         name = prom_firstprop(node, NULL);
301                 } else {
302                         name = prom_nextprop(node, prev, NULL);
303                 }
304                 if (strlen(name) == 0) {
305                         tmp = p;
306                         return NULL;
307                 }
308                 strcpy(p->name, name);
309                 p->length = prom_getproplen(node, p->name);
310                 if (p->length <= 0) {
311                         p->length = 0;
312                 } else {
313                         p->value = prom_early_alloc(p->length + 1);
314                         len = prom_getproperty(node, p->name, p->value,
315                                                p->length);
316                         if (len <= 0)
317                                 p->length = 0;
318                         ((unsigned char *)p->value)[p->length] = '\0';
319                 }
320         }
321         return p;
322 }
323
324 static struct property * __init build_prop_list(phandle node)
325 {
326         struct property *head, *tail;
327
328         head = tail = build_one_prop(node, NULL,
329                                      ".node", &node, sizeof(node));
330
331         tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
332         tail = tail->next;
333         while(tail) {
334                 tail->next = build_one_prop(node, tail->name,
335                                             NULL, NULL, 0);
336                 tail = tail->next;
337         }
338
339         return head;
340 }
341
342 static char * __init get_one_property(phandle node, char *name)
343 {
344         char *buf = "<NULL>";
345         int len;
346
347         len = prom_getproplen(node, name);
348         if (len > 0) {
349                 buf = prom_early_alloc(len);
350                 len = prom_getproperty(node, name, buf, len);
351         }
352
353         return buf;
354 }
355
356 static struct device_node * __init create_node(phandle node)
357 {
358         struct device_node *dp;
359
360         if (!node)
361                 return NULL;
362
363         dp = prom_early_alloc(sizeof(*dp));
364         dp->unique_id = unique_id++;
365
366         kref_init(&dp->kref);
367
368         dp->name = get_one_property(node, "name");
369         dp->type = get_one_property(node, "device_type");
370         dp->node = node;
371
372         /* Build interrupts later... */
373
374         dp->properties = build_prop_list(node);
375
376         return dp;
377 }
378
379 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
380 {
381         struct device_node *dp;
382
383         dp = create_node(node);
384         if (dp) {
385                 *(*nextp) = dp;
386                 *nextp = &dp->allnext;
387
388                 dp->parent = parent;
389                 dp->path_component_name = build_path_component(dp);
390                 dp->full_name = build_full_name(dp);
391
392                 dp->child = build_tree(dp, prom_getchild(node), nextp);
393
394                 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
395         }
396
397         return dp;
398 }
399
400 struct device_node *of_console_device;
401 EXPORT_SYMBOL(of_console_device);
402
403 char *of_console_path;
404 EXPORT_SYMBOL(of_console_path);
405
406 char *of_console_options;
407 EXPORT_SYMBOL(of_console_options);
408
409 extern void restore_current(void);
410
411 static void __init of_console_init(void)
412 {
413         char *msg = "OF stdout device is: %s\n";
414         struct device_node *dp;
415         unsigned long flags;
416         const char *type;
417         phandle node;
418         int skip, fd;
419
420         of_console_path = prom_early_alloc(256);
421
422         switch (prom_vers) {
423         case PROM_V0:
424         case PROM_SUN4:
425                 skip = 0;
426                 switch (*romvec->pv_stdout) {
427                 case PROMDEV_SCREEN:
428                         type = "display";
429                         break;
430
431                 case PROMDEV_TTYB:
432                         skip = 1;
433                         /* FALLTHRU */
434
435                 case PROMDEV_TTYA:
436                         type = "serial";
437                         break;
438
439                 default:
440                         prom_printf("Invalid PROM_V0 stdout value %u\n",
441                                     *romvec->pv_stdout);
442                         prom_halt();
443                 }
444
445                 for_each_node_by_type(dp, type) {
446                         if (!skip--)
447                                 break;
448                 }
449                 if (!dp) {
450                         prom_printf("Cannot find PROM_V0 console node.\n");
451                         prom_halt();
452                 }
453                 of_console_device = dp;
454
455                 strcpy(of_console_path, dp->full_name);
456                 if (!strcmp(type, "serial")) {
457                         strcat(of_console_path,
458                                (skip ? ":b" : ":a"));
459                 }
460                 break;
461
462         default:
463         case PROM_V2:
464         case PROM_V3:
465                 fd = *romvec->pv_v2bootargs.fd_stdout;
466
467                 spin_lock_irqsave(&prom_lock, flags);
468                 node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
469                 restore_current();
470                 spin_unlock_irqrestore(&prom_lock, flags);
471
472                 if (!node) {
473                         prom_printf("Cannot resolve stdout node from "
474                                     "instance %08x.\n", fd);
475                         prom_halt();
476                 }
477                 dp = of_find_node_by_phandle(node);
478                 type = of_get_property(dp, "device_type", NULL);
479
480                 if (!type) {
481                         prom_printf("Console stdout lacks "
482                                     "device_type property.\n");
483                         prom_halt();
484                 }
485
486                 if (strcmp(type, "display") && strcmp(type, "serial")) {
487                         prom_printf("Console device_type is neither display "
488                                     "nor serial.\n");
489                         prom_halt();
490                 }
491
492                 of_console_device = dp;
493
494                 if (prom_vers == PROM_V2) {
495                         strcpy(of_console_path, dp->full_name);
496                         switch (*romvec->pv_stdout) {
497                         case PROMDEV_TTYA:
498                                 strcat(of_console_path, ":a");
499                                 break;
500                         case PROMDEV_TTYB:
501                                 strcat(of_console_path, ":b");
502                                 break;
503                         }
504                 } else {
505                         const char *path;
506
507                         dp = of_find_node_by_path("/");
508                         path = of_get_property(dp, "stdout-path", NULL);
509                         if (!path) {
510                                 prom_printf("No stdout-path in root node.\n");
511                                 prom_halt();
512                         }
513                         strcpy(of_console_path, path);
514                 }
515                 break;
516         }
517
518         of_console_options = strrchr(of_console_path, ':');
519         if (of_console_options) {
520                 of_console_options++;
521                 if (*of_console_options == '\0')
522                         of_console_options = NULL;
523         }
524
525         prom_printf(msg, of_console_path);
526         printk(msg, of_console_path);
527 }
528
529 void __init prom_build_devicetree(void)
530 {
531         struct device_node **nextp;
532
533         allnodes = create_node(prom_root_node);
534         allnodes->path_component_name = "";
535         allnodes->full_name = "/";
536
537         nextp = &allnodes->allnext;
538         allnodes->child = build_tree(allnodes,
539                                      prom_getchild(allnodes->node),
540                                      &nextp);
541         of_console_init();
542
543         printk("PROM: Built device tree with %u bytes of memory.\n",
544                prom_early_allocated);
545 }