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