Merge master.kernel.org:/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[powerpc.git] / arch / sparc64 / 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 sparc64 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 static struct device_node *allnodes;
29
30 /* use when traversing tree through the allnext, child, sibling,
31  * or parent members of struct device_node.
32  */
33 static DEFINE_RWLOCK(devtree_lock);
34
35 int of_device_is_compatible(struct device_node *device, const char *compat)
36 {
37         const char* cp;
38         int cplen, l;
39
40         cp = (char *) of_get_property(device, "compatible", &cplen);
41         if (cp == NULL)
42                 return 0;
43         while (cplen > 0) {
44                 if (strncmp(cp, compat, strlen(compat)) == 0)
45                         return 1;
46                 l = strlen(cp) + 1;
47                 cp += l;
48                 cplen -= l;
49         }
50
51         return 0;
52 }
53 EXPORT_SYMBOL(of_device_is_compatible);
54
55 struct device_node *of_get_parent(const struct device_node *node)
56 {
57         struct device_node *np;
58
59         if (!node)
60                 return NULL;
61
62         np = node->parent;
63
64         return np;
65 }
66 EXPORT_SYMBOL(of_get_parent);
67
68 struct device_node *of_get_next_child(const struct device_node *node,
69         struct device_node *prev)
70 {
71         struct device_node *next;
72
73         next = prev ? prev->sibling : node->child;
74         for (; next != 0; next = next->sibling) {
75                 break;
76         }
77
78         return next;
79 }
80 EXPORT_SYMBOL(of_get_next_child);
81
82 struct device_node *of_find_node_by_path(const char *path)
83 {
84         struct device_node *np = allnodes;
85
86         for (; np != 0; np = np->allnext) {
87                 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
88                         break;
89         }
90
91         return np;
92 }
93 EXPORT_SYMBOL(of_find_node_by_path);
94
95 struct device_node *of_find_node_by_phandle(phandle handle)
96 {
97         struct device_node *np;
98
99         for (np = allnodes; np != 0; np = np->allnext)
100                 if (np->node == handle)
101                         break;
102
103         return np;
104 }
105 EXPORT_SYMBOL(of_find_node_by_phandle);
106
107 struct device_node *of_find_node_by_name(struct device_node *from,
108         const char *name)
109 {
110         struct device_node *np;
111
112         np = from ? from->allnext : allnodes;
113         for (; np != NULL; np = np->allnext)
114                 if (np->name != NULL && strcmp(np->name, name) == 0)
115                         break;
116
117         return np;
118 }
119 EXPORT_SYMBOL(of_find_node_by_name);
120
121 struct device_node *of_find_node_by_type(struct device_node *from,
122         const char *type)
123 {
124         struct device_node *np;
125
126         np = from ? from->allnext : allnodes;
127         for (; np != 0; np = np->allnext)
128                 if (np->type != 0 && strcmp(np->type, type) == 0)
129                         break;
130
131         return np;
132 }
133 EXPORT_SYMBOL(of_find_node_by_type);
134
135 struct device_node *of_find_compatible_node(struct device_node *from,
136         const char *type, const char *compatible)
137 {
138         struct device_node *np;
139
140         np = from ? from->allnext : allnodes;
141         for (; np != 0; np = np->allnext) {
142                 if (type != NULL
143                     && !(np->type != 0 && strcmp(np->type, type) == 0))
144                         continue;
145                 if (of_device_is_compatible(np, compatible))
146                         break;
147         }
148
149         return np;
150 }
151 EXPORT_SYMBOL(of_find_compatible_node);
152
153 struct property *of_find_property(struct device_node *np, const char *name,
154                                   int *lenp)
155 {
156         struct property *pp;
157
158         for (pp = np->properties; pp != 0; pp = pp->next) {
159                 if (strcmp(pp->name, name) == 0) {
160                         if (lenp != 0)
161                                 *lenp = pp->length;
162                         break;
163                 }
164         }
165         return pp;
166 }
167 EXPORT_SYMBOL(of_find_property);
168
169 /*
170  * Find a property with a given name for a given node
171  * and return the value.
172  */
173 void *of_get_property(struct device_node *np, const char *name, int *lenp)
174 {
175         struct property *pp = of_find_property(np,name,lenp);
176         return pp ? pp->value : NULL;
177 }
178 EXPORT_SYMBOL(of_get_property);
179
180 int of_getintprop_default(struct device_node *np, const char *name, int def)
181 {
182         struct property *prop;
183         int len;
184
185         prop = of_find_property(np, name, &len);
186         if (!prop || len != 4)
187                 return def;
188
189         return *(int *) prop->value;
190 }
191 EXPORT_SYMBOL(of_getintprop_default);
192
193 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
194 {
195         struct property **prevp;
196         void *new_val;
197         int err;
198
199         new_val = kmalloc(len, GFP_KERNEL);
200         if (!new_val)
201                 return -ENOMEM;
202
203         memcpy(new_val, val, len);
204
205         err = -ENODEV;
206
207         write_lock(&devtree_lock);
208         prevp = &dp->properties;
209         while (*prevp) {
210                 struct property *prop = *prevp;
211
212                 if (!strcmp(prop->name, name)) {
213                         void *old_val = prop->value;
214                         int ret;
215
216                         ret = prom_setprop(dp->node, name, val, len);
217                         err = -EINVAL;
218                         if (ret >= 0) {
219                                 prop->value = new_val;
220                                 prop->length = len;
221
222                                 if (OF_IS_DYNAMIC(prop))
223                                         kfree(old_val);
224
225                                 OF_MARK_DYNAMIC(prop);
226
227                                 err = 0;
228                         }
229                         break;
230                 }
231                 prevp = &(*prevp)->next;
232         }
233         write_unlock(&devtree_lock);
234
235         /* XXX Upate procfs if necessary... */
236
237         return err;
238 }
239 EXPORT_SYMBOL(of_set_property);
240
241 static unsigned int prom_early_allocated;
242
243 static void * __init prom_early_alloc(unsigned long size)
244 {
245         void *ret;
246
247         ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
248         if (ret != NULL)
249                 memset(ret, 0, size);
250
251         prom_early_allocated += size;
252
253         return ret;
254 }
255
256 static int is_root_node(const struct device_node *dp)
257 {
258         if (!dp)
259                 return 0;
260
261         return (dp->parent == NULL);
262 }
263
264 /* The following routines deal with the black magic of fully naming a
265  * node.
266  *
267  * Certain well known named nodes are just the simple name string.
268  *
269  * Actual devices have an address specifier appended to the base name
270  * string, like this "foo@addr".  The "addr" can be in any number of
271  * formats, and the platform plus the type of the node determine the
272  * format and how it is constructed.
273  *
274  * For children of the ROOT node, the naming convention is fixed and
275  * determined by whether this is a sun4u or sun4v system.
276  *
277  * For children of other nodes, it is bus type specific.  So
278  * we walk up the tree until we discover a "device_type" property
279  * we recognize and we go from there.
280  *
281  * As an example, the boot device on my workstation has a full path:
282  *
283  *      /pci@1e,600000/ide@d/disk@0,0:c
284  */
285 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
286 {
287         struct linux_prom64_registers *regs;
288         struct property *rprop;
289         u32 high_bits, low_bits, type;
290
291         rprop = of_find_property(dp, "reg", NULL);
292         if (!rprop)
293                 return;
294
295         regs = rprop->value;
296         if (!is_root_node(dp->parent)) {
297                 sprintf(tmp_buf, "%s@%x,%x",
298                         dp->name,
299                         (unsigned int) (regs->phys_addr >> 32UL),
300                         (unsigned int) (regs->phys_addr & 0xffffffffUL));
301                 return;
302         }
303
304         type = regs->phys_addr >> 60UL;
305         high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
306         low_bits = (regs->phys_addr & 0xffffffffUL);
307
308         if (type == 0 || type == 8) {
309                 const char *prefix = (type == 0) ? "m" : "i";
310
311                 if (low_bits)
312                         sprintf(tmp_buf, "%s@%s%x,%x",
313                                 dp->name, prefix,
314                                 high_bits, low_bits);
315                 else
316                         sprintf(tmp_buf, "%s@%s%x",
317                                 dp->name,
318                                 prefix,
319                                 high_bits);
320         } else if (type == 12) {
321                 sprintf(tmp_buf, "%s@%x",
322                         dp->name, high_bits);
323         }
324 }
325
326 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
327 {
328         struct linux_prom64_registers *regs;
329         struct property *prop;
330
331         prop = of_find_property(dp, "reg", NULL);
332         if (!prop)
333                 return;
334
335         regs = prop->value;
336         if (!is_root_node(dp->parent)) {
337                 sprintf(tmp_buf, "%s@%x,%x",
338                         dp->name,
339                         (unsigned int) (regs->phys_addr >> 32UL),
340                         (unsigned int) (regs->phys_addr & 0xffffffffUL));
341                 return;
342         }
343
344         prop = of_find_property(dp, "upa-portid", NULL);
345         if (!prop)
346                 prop = of_find_property(dp, "portid", NULL);
347         if (prop) {
348                 unsigned long mask = 0xffffffffUL;
349
350                 if (tlb_type >= cheetah)
351                         mask = 0x7fffff;
352
353                 sprintf(tmp_buf, "%s@%x,%x",
354                         dp->name,
355                         *(u32 *)prop->value,
356                         (unsigned int) (regs->phys_addr & mask));
357         }
358 }
359
360 /* "name@slot,offset"  */
361 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
362 {
363         struct linux_prom_registers *regs;
364         struct property *prop;
365
366         prop = of_find_property(dp, "reg", NULL);
367         if (!prop)
368                 return;
369
370         regs = prop->value;
371         sprintf(tmp_buf, "%s@%x,%x",
372                 dp->name,
373                 regs->which_io,
374                 regs->phys_addr);
375 }
376
377 /* "name@devnum[,func]" */
378 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
379 {
380         struct linux_prom_pci_registers *regs;
381         struct property *prop;
382         unsigned int devfn;
383
384         prop = of_find_property(dp, "reg", NULL);
385         if (!prop)
386                 return;
387
388         regs = prop->value;
389         devfn = (regs->phys_hi >> 8) & 0xff;
390         if (devfn & 0x07) {
391                 sprintf(tmp_buf, "%s@%x,%x",
392                         dp->name,
393                         devfn >> 3,
394                         devfn & 0x07);
395         } else {
396                 sprintf(tmp_buf, "%s@%x",
397                         dp->name,
398                         devfn >> 3);
399         }
400 }
401
402 /* "name@UPA_PORTID,offset" */
403 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
404 {
405         struct linux_prom64_registers *regs;
406         struct property *prop;
407
408         prop = of_find_property(dp, "reg", NULL);
409         if (!prop)
410                 return;
411
412         regs = prop->value;
413
414         prop = of_find_property(dp, "upa-portid", NULL);
415         if (!prop)
416                 return;
417
418         sprintf(tmp_buf, "%s@%x,%x",
419                 dp->name,
420                 *(u32 *) prop->value,
421                 (unsigned int) (regs->phys_addr & 0xffffffffUL));
422 }
423
424 /* "name@reg" */
425 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
426 {
427         struct property *prop;
428         u32 *regs;
429
430         prop = of_find_property(dp, "reg", NULL);
431         if (!prop)
432                 return;
433
434         regs = prop->value;
435
436         sprintf(tmp_buf, "%s@%x", dp->name, *regs);
437 }
438
439 /* "name@addrhi,addrlo" */
440 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
441 {
442         struct linux_prom64_registers *regs;
443         struct property *prop;
444
445         prop = of_find_property(dp, "reg", NULL);
446         if (!prop)
447                 return;
448
449         regs = prop->value;
450
451         sprintf(tmp_buf, "%s@%x,%x",
452                 dp->name,
453                 (unsigned int) (regs->phys_addr >> 32UL),
454                 (unsigned int) (regs->phys_addr & 0xffffffffUL));
455 }
456
457 /* "name@bus,addr" */
458 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
459 {
460         struct property *prop;
461         u32 *regs;
462
463         prop = of_find_property(dp, "reg", NULL);
464         if (!prop)
465                 return;
466
467         regs = prop->value;
468
469         /* This actually isn't right... should look at the #address-cells
470          * property of the i2c bus node etc. etc.
471          */
472         sprintf(tmp_buf, "%s@%x,%x",
473                 dp->name, regs[0], regs[1]);
474 }
475
476 /* "name@reg0[,reg1]" */
477 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
478 {
479         struct property *prop;
480         u32 *regs;
481
482         prop = of_find_property(dp, "reg", NULL);
483         if (!prop)
484                 return;
485
486         regs = prop->value;
487
488         if (prop->length == sizeof(u32) || regs[1] == 1) {
489                 sprintf(tmp_buf, "%s@%x",
490                         dp->name, regs[0]);
491         } else {
492                 sprintf(tmp_buf, "%s@%x,%x",
493                         dp->name, regs[0], regs[1]);
494         }
495 }
496
497 /* "name@reg0reg1[,reg2reg3]" */
498 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
499 {
500         struct property *prop;
501         u32 *regs;
502
503         prop = of_find_property(dp, "reg", NULL);
504         if (!prop)
505                 return;
506
507         regs = prop->value;
508
509         if (regs[2] || regs[3]) {
510                 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
511                         dp->name, regs[0], regs[1], regs[2], regs[3]);
512         } else {
513                 sprintf(tmp_buf, "%s@%08x%08x",
514                         dp->name, regs[0], regs[1]);
515         }
516 }
517
518 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
519 {
520         struct device_node *parent = dp->parent;
521
522         if (parent != NULL) {
523                 if (!strcmp(parent->type, "pci") ||
524                     !strcmp(parent->type, "pciex"))
525                         return pci_path_component(dp, tmp_buf);
526                 if (!strcmp(parent->type, "sbus"))
527                         return sbus_path_component(dp, tmp_buf);
528                 if (!strcmp(parent->type, "upa"))
529                         return upa_path_component(dp, tmp_buf);
530                 if (!strcmp(parent->type, "ebus"))
531                         return ebus_path_component(dp, tmp_buf);
532                 if (!strcmp(parent->name, "usb") ||
533                     !strcmp(parent->name, "hub"))
534                         return usb_path_component(dp, tmp_buf);
535                 if (!strcmp(parent->type, "i2c"))
536                         return i2c_path_component(dp, tmp_buf);
537                 if (!strcmp(parent->type, "firewire"))
538                         return ieee1394_path_component(dp, tmp_buf);
539                 if (!strcmp(parent->type, "virtual-devices"))
540                         return vdev_path_component(dp, tmp_buf);
541
542                 /* "isa" is handled with platform naming */
543         }
544
545         /* Use platform naming convention.  */
546         if (tlb_type == hypervisor)
547                 return sun4v_path_component(dp, tmp_buf);
548         else
549                 return sun4u_path_component(dp, tmp_buf);
550 }
551
552 static char * __init build_path_component(struct device_node *dp)
553 {
554         char tmp_buf[64], *n;
555
556         tmp_buf[0] = '\0';
557         __build_path_component(dp, tmp_buf);
558         if (tmp_buf[0] == '\0')
559                 strcpy(tmp_buf, dp->name);
560
561         n = prom_early_alloc(strlen(tmp_buf) + 1);
562         strcpy(n, tmp_buf);
563
564         return n;
565 }
566
567 static char * __init build_full_name(struct device_node *dp)
568 {
569         int len, ourlen, plen;
570         char *n;
571
572         plen = strlen(dp->parent->full_name);
573         ourlen = strlen(dp->path_component_name);
574         len = ourlen + plen + 2;
575
576         n = prom_early_alloc(len);
577         strcpy(n, dp->parent->full_name);
578         if (!is_root_node(dp->parent)) {
579                 strcpy(n + plen, "/");
580                 plen++;
581         }
582         strcpy(n + plen, dp->path_component_name);
583
584         return n;
585 }
586
587 static unsigned int unique_id;
588
589 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
590 {
591         static struct property *tmp = NULL;
592         struct property *p;
593
594         if (tmp) {
595                 p = tmp;
596                 memset(p, 0, sizeof(*p) + 32);
597                 tmp = NULL;
598         } else {
599                 p = prom_early_alloc(sizeof(struct property) + 32);
600                 p->unique_id = unique_id++;
601         }
602
603         p->name = (char *) (p + 1);
604         if (special_name) {
605                 strcpy(p->name, special_name);
606                 p->length = special_len;
607                 p->value = prom_early_alloc(special_len);
608                 memcpy(p->value, special_val, special_len);
609         } else {
610                 if (prev == NULL) {
611                         prom_firstprop(node, p->name);
612                 } else {
613                         prom_nextprop(node, prev, p->name);
614                 }
615                 if (strlen(p->name) == 0) {
616                         tmp = p;
617                         return NULL;
618                 }
619                 p->length = prom_getproplen(node, p->name);
620                 if (p->length <= 0) {
621                         p->length = 0;
622                 } else {
623                         p->value = prom_early_alloc(p->length + 1);
624                         prom_getproperty(node, p->name, p->value, p->length);
625                         ((unsigned char *)p->value)[p->length] = '\0';
626                 }
627         }
628         return p;
629 }
630
631 static struct property * __init build_prop_list(phandle node)
632 {
633         struct property *head, *tail;
634
635         head = tail = build_one_prop(node, NULL,
636                                      ".node", &node, sizeof(node));
637
638         tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
639         tail = tail->next;
640         while(tail) {
641                 tail->next = build_one_prop(node, tail->name,
642                                             NULL, NULL, 0);
643                 tail = tail->next;
644         }
645
646         return head;
647 }
648
649 static char * __init get_one_property(phandle node, const char *name)
650 {
651         char *buf = "<NULL>";
652         int len;
653
654         len = prom_getproplen(node, name);
655         if (len > 0) {
656                 buf = prom_early_alloc(len);
657                 prom_getproperty(node, name, buf, len);
658         }
659
660         return buf;
661 }
662
663 static struct device_node * __init create_node(phandle node)
664 {
665         struct device_node *dp;
666
667         if (!node)
668                 return NULL;
669
670         dp = prom_early_alloc(sizeof(*dp));
671         dp->unique_id = unique_id++;
672
673         kref_init(&dp->kref);
674
675         dp->name = get_one_property(node, "name");
676         dp->type = get_one_property(node, "device_type");
677         dp->node = node;
678
679         /* Build interrupts later... */
680
681         dp->properties = build_prop_list(node);
682
683         return dp;
684 }
685
686 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
687 {
688         struct device_node *dp;
689
690         dp = create_node(node);
691         if (dp) {
692                 *(*nextp) = dp;
693                 *nextp = &dp->allnext;
694
695                 dp->parent = parent;
696                 dp->path_component_name = build_path_component(dp);
697                 dp->full_name = build_full_name(dp);
698
699                 dp->child = build_tree(dp, prom_getchild(node), nextp);
700
701                 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
702         }
703
704         return dp;
705 }
706
707 void __init prom_build_devicetree(void)
708 {
709         struct device_node **nextp;
710
711         allnodes = create_node(prom_root_node);
712         allnodes->path_component_name = "";
713         allnodes->full_name = "/";
714
715         nextp = &allnodes->allnext;
716         allnodes->child = build_tree(allnodes,
717                                      prom_getchild(allnodes->node),
718                                      &nextp);
719         printk("PROM: Built device tree with %u bytes of memory.\n",
720                prom_early_allocated);
721 }