ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sparc / kernel / devices.c
1 /* devices.c: Initial scan of the prom device tree for important
2  *            Sparc device nodes which we need to find.
3  *
4  * This is based on the sparc64 version, but sun4m doesn't always use
5  * the hardware MIDs, so be careful.
6  *
7  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
8  */
9
10 #include <linux/config.h>
11 #include <linux/kernel.h>
12 #include <linux/threads.h>
13 #include <linux/string.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16
17 #include <asm/page.h>
18 #include <asm/oplib.h>
19 #include <asm/smp.h>
20 #include <asm/system.h>
21 #include <asm/cpudata.h>
22
23 extern void cpu_probe(void);
24 extern void clock_stop_probe(void); /* tadpole.c */
25 extern void sun4c_probe_memerr_reg(void);
26
27 static char *cpu_mid_prop(void)
28 {
29         if (sparc_cpu_model == sun4d)
30                 return "cpu-id";
31         return "mid";
32 }
33
34 static int check_cpu_node(int nd, int *cur_inst,
35                           int (*compare)(int, int, void *), void *compare_arg,
36                           int *prom_node, int *mid)
37 {
38         char node_str[128];
39
40         prom_getstring(nd, "device_type", node_str, sizeof(node_str));
41         if (strcmp(node_str, "cpu"))
42                 return -ENODEV;
43         
44         if (!compare(nd, *cur_inst, compare_arg)) {
45                 if (prom_node)
46                         *prom_node = nd;
47                 if (mid) {
48                         *mid = prom_getintdefault(nd, cpu_mid_prop(), 0);
49                         if (sparc_cpu_model == sun4m)
50                                 *mid &= 3;
51                 }
52                 return 0;
53         }
54
55         (*cur_inst)++;
56
57         return -ENODEV;
58 }
59
60 static int __cpu_find_by(int (*compare)(int, int, void *), void *compare_arg,
61                          int *prom_node, int *mid)
62 {
63         int nd, cur_inst, err;
64
65         nd = prom_root_node;
66         cur_inst = 0;
67
68         err = check_cpu_node(nd, &cur_inst, compare, compare_arg,
69                              prom_node, mid);
70         if (!err)
71                 return 0;
72
73         nd = prom_getchild(nd);
74         while ((nd = prom_getsibling(nd)) != 0) {
75                 err = check_cpu_node(nd, &cur_inst, compare, compare_arg,
76                                      prom_node, mid);
77                 if (!err)
78                         return 0;
79         }
80
81         return -ENODEV;
82 }
83
84 static int cpu_instance_compare(int nd, int instance, void *_arg)
85 {
86         int desired_instance = (int) _arg;
87
88         if (instance == desired_instance)
89                 return 0;
90         return -ENODEV;
91 }
92
93 int cpu_find_by_instance(int instance, int *prom_node, int *mid)
94 {
95         return __cpu_find_by(cpu_instance_compare, (void *)instance,
96                              prom_node, mid);
97 }
98
99 static int cpu_mid_compare(int nd, int instance, void *_arg)
100 {
101         int desired_mid = (int) _arg;
102         int this_mid;
103
104         this_mid = prom_getintdefault(nd, cpu_mid_prop(), 0);
105         if (this_mid == desired_mid
106             || (sparc_cpu_model == sun4m && (this_mid & 3) == desired_mid))
107                 return 0;
108         return -ENODEV;
109 }
110
111 int cpu_find_by_mid(int mid, int *prom_node)
112 {
113         return __cpu_find_by(cpu_mid_compare, (void *)mid,
114                              prom_node, NULL);
115 }
116
117 /* sun4m uses truncated mids since we base the cpuid on the ttable/irqset
118  * address (0-3).  This gives us the true hardware mid, which might have
119  * some other bits set.  On 4d hardware and software mids are the same.
120  */
121 int cpu_get_hwmid(int prom_node)
122 {
123         return prom_getintdefault(prom_node, cpu_mid_prop(), -ENODEV);
124 }
125
126 void __init device_scan(void)
127 {
128         prom_printf("Booting Linux...\n");
129
130 #ifndef CONFIG_SMP
131         {
132                 int err, cpu_node;
133                 err = cpu_find_by_instance(0, &cpu_node, NULL);
134                 if (err) {
135                         /* Probably a sun4e, Sun is trying to trick us ;-) */
136                         prom_printf("No cpu nodes, cannot continue\n");
137                         prom_halt();
138                 }
139                 cpu_data(0).clock_tick = prom_getintdefault(cpu_node,
140                                                             "clock-frequency",
141                                                             0);
142         }
143 #endif /* !CONFIG_SMP */
144
145         cpu_probe();
146 #ifdef CONFIG_SUN_AUXIO
147         {
148                 extern void auxio_probe(void);
149                 extern void auxio_power_probe(void);
150                 auxio_probe();
151                 auxio_power_probe();
152         }
153 #endif
154         clock_stop_probe();
155
156         if (ARCH_SUN4C_SUN4)
157                 sun4c_probe_memerr_reg();
158
159         return;
160 }