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