[SPARC64]: Fix typo in clock_probe().
[linux-2.6] / 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_n_addr_cells(struct device_node *np)
194 {
195         int* ip;
196         do {
197                 if (np->parent)
198                         np = np->parent;
199                 ip = of_get_property(np, "#address-cells", NULL);
200                 if (ip != NULL)
201                         return *ip;
202         } while (np->parent);
203         /* No #address-cells property for the root node, default to 2 */
204         return 2;
205 }
206 EXPORT_SYMBOL(of_n_addr_cells);
207
208 int of_n_size_cells(struct device_node *np)
209 {
210         int* ip;
211         do {
212                 if (np->parent)
213                         np = np->parent;
214                 ip = of_get_property(np, "#size-cells", NULL);
215                 if (ip != NULL)
216                         return *ip;
217         } while (np->parent);
218         /* No #size-cells property for the root node, default to 1 */
219         return 1;
220 }
221 EXPORT_SYMBOL(of_n_size_cells);
222
223 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
224 {
225         struct property **prevp;
226         void *new_val;
227         int err;
228
229         new_val = kmalloc(len, GFP_KERNEL);
230         if (!new_val)
231                 return -ENOMEM;
232
233         memcpy(new_val, val, len);
234
235         err = -ENODEV;
236
237         write_lock(&devtree_lock);
238         prevp = &dp->properties;
239         while (*prevp) {
240                 struct property *prop = *prevp;
241
242                 if (!strcmp(prop->name, name)) {
243                         void *old_val = prop->value;
244                         int ret;
245
246                         ret = prom_setprop(dp->node, name, val, len);
247                         err = -EINVAL;
248                         if (ret >= 0) {
249                                 prop->value = new_val;
250                                 prop->length = len;
251
252                                 if (OF_IS_DYNAMIC(prop))
253                                         kfree(old_val);
254
255                                 OF_MARK_DYNAMIC(prop);
256
257                                 err = 0;
258                         }
259                         break;
260                 }
261                 prevp = &(*prevp)->next;
262         }
263         write_unlock(&devtree_lock);
264
265         /* XXX Upate procfs if necessary... */
266
267         return err;
268 }
269 EXPORT_SYMBOL(of_set_property);
270
271 static unsigned int prom_early_allocated;
272
273 static void * __init prom_early_alloc(unsigned long size)
274 {
275         void *ret;
276
277         ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
278         if (ret != NULL)
279                 memset(ret, 0, size);
280
281         prom_early_allocated += size;
282
283         return ret;
284 }
285
286 static int is_root_node(const struct device_node *dp)
287 {
288         if (!dp)
289                 return 0;
290
291         return (dp->parent == NULL);
292 }
293
294 /* The following routines deal with the black magic of fully naming a
295  * node.
296  *
297  * Certain well known named nodes are just the simple name string.
298  *
299  * Actual devices have an address specifier appended to the base name
300  * string, like this "foo@addr".  The "addr" can be in any number of
301  * formats, and the platform plus the type of the node determine the
302  * format and how it is constructed.
303  *
304  * For children of the ROOT node, the naming convention is fixed and
305  * determined by whether this is a sun4u or sun4v system.
306  *
307  * For children of other nodes, it is bus type specific.  So
308  * we walk up the tree until we discover a "device_type" property
309  * we recognize and we go from there.
310  *
311  * As an example, the boot device on my workstation has a full path:
312  *
313  *      /pci@1e,600000/ide@d/disk@0,0:c
314  */
315 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
316 {
317         struct linux_prom64_registers *regs;
318         struct property *rprop;
319         u32 high_bits, low_bits, type;
320
321         rprop = of_find_property(dp, "reg", NULL);
322         if (!rprop)
323                 return;
324
325         regs = rprop->value;
326         if (!is_root_node(dp->parent)) {
327                 sprintf(tmp_buf, "%s@%x,%x",
328                         dp->name,
329                         (unsigned int) (regs->phys_addr >> 32UL),
330                         (unsigned int) (regs->phys_addr & 0xffffffffUL));
331                 return;
332         }
333
334         type = regs->phys_addr >> 60UL;
335         high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
336         low_bits = (regs->phys_addr & 0xffffffffUL);
337
338         if (type == 0 || type == 8) {
339                 const char *prefix = (type == 0) ? "m" : "i";
340
341                 if (low_bits)
342                         sprintf(tmp_buf, "%s@%s%x,%x",
343                                 dp->name, prefix,
344                                 high_bits, low_bits);
345                 else
346                         sprintf(tmp_buf, "%s@%s%x",
347                                 dp->name,
348                                 prefix,
349                                 high_bits);
350         } else if (type == 12) {
351                 sprintf(tmp_buf, "%s@%x",
352                         dp->name, high_bits);
353         }
354 }
355
356 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
357 {
358         struct linux_prom64_registers *regs;
359         struct property *prop;
360
361         prop = of_find_property(dp, "reg", NULL);
362         if (!prop)
363                 return;
364
365         regs = prop->value;
366         if (!is_root_node(dp->parent)) {
367                 sprintf(tmp_buf, "%s@%x,%x",
368                         dp->name,
369                         (unsigned int) (regs->phys_addr >> 32UL),
370                         (unsigned int) (regs->phys_addr & 0xffffffffUL));
371                 return;
372         }
373
374         prop = of_find_property(dp, "upa-portid", NULL);
375         if (!prop)
376                 prop = of_find_property(dp, "portid", NULL);
377         if (prop) {
378                 unsigned long mask = 0xffffffffUL;
379
380                 if (tlb_type >= cheetah)
381                         mask = 0x7fffff;
382
383                 sprintf(tmp_buf, "%s@%x,%x",
384                         dp->name,
385                         *(u32 *)prop->value,
386                         (unsigned int) (regs->phys_addr & mask));
387         }
388 }
389
390 /* "name@slot,offset"  */
391 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
392 {
393         struct linux_prom_registers *regs;
394         struct property *prop;
395
396         prop = of_find_property(dp, "reg", NULL);
397         if (!prop)
398                 return;
399
400         regs = prop->value;
401         sprintf(tmp_buf, "%s@%x,%x",
402                 dp->name,
403                 regs->which_io,
404                 regs->phys_addr);
405 }
406
407 /* "name@devnum[,func]" */
408 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
409 {
410         struct linux_prom_pci_registers *regs;
411         struct property *prop;
412         unsigned int devfn;
413
414         prop = of_find_property(dp, "reg", NULL);
415         if (!prop)
416                 return;
417
418         regs = prop->value;
419         devfn = (regs->phys_hi >> 8) & 0xff;
420         if (devfn & 0x07) {
421                 sprintf(tmp_buf, "%s@%x,%x",
422                         dp->name,
423                         devfn >> 3,
424                         devfn & 0x07);
425         } else {
426                 sprintf(tmp_buf, "%s@%x",
427                         dp->name,
428                         devfn >> 3);
429         }
430 }
431
432 /* "name@UPA_PORTID,offset" */
433 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
434 {
435         struct linux_prom64_registers *regs;
436         struct property *prop;
437
438         prop = of_find_property(dp, "reg", NULL);
439         if (!prop)
440                 return;
441
442         regs = prop->value;
443
444         prop = of_find_property(dp, "upa-portid", NULL);
445         if (!prop)
446                 return;
447
448         sprintf(tmp_buf, "%s@%x,%x",
449                 dp->name,
450                 *(u32 *) prop->value,
451                 (unsigned int) (regs->phys_addr & 0xffffffffUL));
452 }
453
454 /* "name@reg" */
455 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
456 {
457         struct property *prop;
458         u32 *regs;
459
460         prop = of_find_property(dp, "reg", NULL);
461         if (!prop)
462                 return;
463
464         regs = prop->value;
465
466         sprintf(tmp_buf, "%s@%x", dp->name, *regs);
467 }
468
469 /* "name@addrhi,addrlo" */
470 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
471 {
472         struct linux_prom64_registers *regs;
473         struct property *prop;
474
475         prop = of_find_property(dp, "reg", NULL);
476         if (!prop)
477                 return;
478
479         regs = prop->value;
480
481         sprintf(tmp_buf, "%s@%x,%x",
482                 dp->name,
483                 (unsigned int) (regs->phys_addr >> 32UL),
484                 (unsigned int) (regs->phys_addr & 0xffffffffUL));
485 }
486
487 /* "name@bus,addr" */
488 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
489 {
490         struct property *prop;
491         u32 *regs;
492
493         prop = of_find_property(dp, "reg", NULL);
494         if (!prop)
495                 return;
496
497         regs = prop->value;
498
499         /* This actually isn't right... should look at the #address-cells
500          * property of the i2c bus node etc. etc.
501          */
502         sprintf(tmp_buf, "%s@%x,%x",
503                 dp->name, regs[0], regs[1]);
504 }
505
506 /* "name@reg0[,reg1]" */
507 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
508 {
509         struct property *prop;
510         u32 *regs;
511
512         prop = of_find_property(dp, "reg", NULL);
513         if (!prop)
514                 return;
515
516         regs = prop->value;
517
518         if (prop->length == sizeof(u32) || regs[1] == 1) {
519                 sprintf(tmp_buf, "%s@%x",
520                         dp->name, regs[0]);
521         } else {
522                 sprintf(tmp_buf, "%s@%x,%x",
523                         dp->name, regs[0], regs[1]);
524         }
525 }
526
527 /* "name@reg0reg1[,reg2reg3]" */
528 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
529 {
530         struct property *prop;
531         u32 *regs;
532
533         prop = of_find_property(dp, "reg", NULL);
534         if (!prop)
535                 return;
536
537         regs = prop->value;
538
539         if (regs[2] || regs[3]) {
540                 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
541                         dp->name, regs[0], regs[1], regs[2], regs[3]);
542         } else {
543                 sprintf(tmp_buf, "%s@%08x%08x",
544                         dp->name, regs[0], regs[1]);
545         }
546 }
547
548 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
549 {
550         struct device_node *parent = dp->parent;
551
552         if (parent != NULL) {
553                 if (!strcmp(parent->type, "pci") ||
554                     !strcmp(parent->type, "pciex"))
555                         return pci_path_component(dp, tmp_buf);
556                 if (!strcmp(parent->type, "sbus"))
557                         return sbus_path_component(dp, tmp_buf);
558                 if (!strcmp(parent->type, "upa"))
559                         return upa_path_component(dp, tmp_buf);
560                 if (!strcmp(parent->type, "ebus"))
561                         return ebus_path_component(dp, tmp_buf);
562                 if (!strcmp(parent->name, "usb") ||
563                     !strcmp(parent->name, "hub"))
564                         return usb_path_component(dp, tmp_buf);
565                 if (!strcmp(parent->type, "i2c"))
566                         return i2c_path_component(dp, tmp_buf);
567                 if (!strcmp(parent->type, "firewire"))
568                         return ieee1394_path_component(dp, tmp_buf);
569                 if (!strcmp(parent->type, "virtual-devices"))
570                         return vdev_path_component(dp, tmp_buf);
571
572                 /* "isa" is handled with platform naming */
573         }
574
575         /* Use platform naming convention.  */
576         if (tlb_type == hypervisor)
577                 return sun4v_path_component(dp, tmp_buf);
578         else
579                 return sun4u_path_component(dp, tmp_buf);
580 }
581
582 static char * __init build_path_component(struct device_node *dp)
583 {
584         char tmp_buf[64], *n;
585
586         tmp_buf[0] = '\0';
587         __build_path_component(dp, tmp_buf);
588         if (tmp_buf[0] == '\0')
589                 strcpy(tmp_buf, dp->name);
590
591         n = prom_early_alloc(strlen(tmp_buf) + 1);
592         strcpy(n, tmp_buf);
593
594         return n;
595 }
596
597 static char * __init build_full_name(struct device_node *dp)
598 {
599         int len, ourlen, plen;
600         char *n;
601
602         plen = strlen(dp->parent->full_name);
603         ourlen = strlen(dp->path_component_name);
604         len = ourlen + plen + 2;
605
606         n = prom_early_alloc(len);
607         strcpy(n, dp->parent->full_name);
608         if (!is_root_node(dp->parent)) {
609                 strcpy(n + plen, "/");
610                 plen++;
611         }
612         strcpy(n + plen, dp->path_component_name);
613
614         return n;
615 }
616
617 static unsigned int unique_id;
618
619 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
620 {
621         static struct property *tmp = NULL;
622         struct property *p;
623
624         if (tmp) {
625                 p = tmp;
626                 memset(p, 0, sizeof(*p) + 32);
627                 tmp = NULL;
628         } else {
629                 p = prom_early_alloc(sizeof(struct property) + 32);
630                 p->unique_id = unique_id++;
631         }
632
633         p->name = (char *) (p + 1);
634         if (special_name) {
635                 strcpy(p->name, special_name);
636                 p->length = special_len;
637                 p->value = prom_early_alloc(special_len);
638                 memcpy(p->value, special_val, special_len);
639         } else {
640                 if (prev == NULL) {
641                         prom_firstprop(node, p->name);
642                 } else {
643                         prom_nextprop(node, prev, p->name);
644                 }
645                 if (strlen(p->name) == 0) {
646                         tmp = p;
647                         return NULL;
648                 }
649                 p->length = prom_getproplen(node, p->name);
650                 if (p->length <= 0) {
651                         p->length = 0;
652                 } else {
653                         p->value = prom_early_alloc(p->length + 1);
654                         prom_getproperty(node, p->name, p->value, p->length);
655                         ((unsigned char *)p->value)[p->length] = '\0';
656                 }
657         }
658         return p;
659 }
660
661 static struct property * __init build_prop_list(phandle node)
662 {
663         struct property *head, *tail;
664
665         head = tail = build_one_prop(node, NULL,
666                                      ".node", &node, sizeof(node));
667
668         tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
669         tail = tail->next;
670         while(tail) {
671                 tail->next = build_one_prop(node, tail->name,
672                                             NULL, NULL, 0);
673                 tail = tail->next;
674         }
675
676         return head;
677 }
678
679 static char * __init get_one_property(phandle node, const char *name)
680 {
681         char *buf = "<NULL>";
682         int len;
683
684         len = prom_getproplen(node, name);
685         if (len > 0) {
686                 buf = prom_early_alloc(len);
687                 prom_getproperty(node, name, buf, len);
688         }
689
690         return buf;
691 }
692
693 static struct device_node * __init create_node(phandle node)
694 {
695         struct device_node *dp;
696
697         if (!node)
698                 return NULL;
699
700         dp = prom_early_alloc(sizeof(*dp));
701         dp->unique_id = unique_id++;
702
703         kref_init(&dp->kref);
704
705         dp->name = get_one_property(node, "name");
706         dp->type = get_one_property(node, "device_type");
707         dp->node = node;
708
709         /* Build interrupts later... */
710
711         dp->properties = build_prop_list(node);
712
713         return dp;
714 }
715
716 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
717 {
718         struct device_node *dp;
719
720         dp = create_node(node);
721         if (dp) {
722                 *(*nextp) = dp;
723                 *nextp = &dp->allnext;
724
725                 dp->parent = parent;
726                 dp->path_component_name = build_path_component(dp);
727                 dp->full_name = build_full_name(dp);
728
729                 dp->child = build_tree(dp, prom_getchild(node), nextp);
730
731                 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
732         }
733
734         return dp;
735 }
736
737 void __init prom_build_devicetree(void)
738 {
739         struct device_node **nextp;
740
741         allnodes = create_node(prom_root_node);
742         allnodes->path_component_name = "";
743         allnodes->full_name = "/";
744
745         nextp = &allnodes->allnext;
746         allnodes->child = build_tree(allnodes,
747                                      prom_getchild(allnodes->node),
748                                      &nextp);
749         printk("PROM: Built device tree with %u bytes of memory.\n",
750                prom_early_allocated);
751 }