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