Merge branch 'intelfb-patches' of git://git.kernel.org/pub/scm/linux/kernel/git/airli...
[linux-2.6] / arch / sparc64 / 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  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5  */
6
7 #include <linux/config.h>
8 #include <linux/kernel.h>
9 #include <linux/threads.h>
10 #include <linux/init.h>
11 #include <linux/ioport.h>
12 #include <linux/string.h>
13 #include <linux/spinlock.h>
14 #include <linux/errno.h>
15 #include <linux/bootmem.h>
16
17 #include <asm/page.h>
18 #include <asm/oplib.h>
19 #include <asm/system.h>
20 #include <asm/smp.h>
21 #include <asm/spitfire.h>
22 #include <asm/timer.h>
23 #include <asm/cpudata.h>
24 #include <asm/vdev.h>
25 #include <asm/irq.h>
26
27 /* Used to synchronize acceses to NatSemi SUPER I/O chip configure
28  * operations in asm/ns87303.h
29  */
30 DEFINE_SPINLOCK(ns87303_lock);
31
32 extern void cpu_probe(void);
33 extern void central_probe(void);
34
35 u32 sun4v_vdev_devhandle;
36 struct device_node *sun4v_vdev_root;
37
38 struct vdev_intmap {
39         unsigned int phys;
40         unsigned int irq;
41         unsigned int cnode;
42         unsigned int cinterrupt;
43 };
44
45 struct vdev_intmask {
46         unsigned int phys;
47         unsigned int interrupt;
48         unsigned int __unused;
49 };
50
51 static struct vdev_intmap *vdev_intmap;
52 static int vdev_num_intmap;
53 static struct vdev_intmask *vdev_intmask;
54
55 static void __init sun4v_virtual_device_probe(void)
56 {
57         struct linux_prom64_registers *regs;
58         struct property *prop;
59         struct device_node *dp;
60         int sz;
61
62         if (tlb_type != hypervisor)
63                 return;
64
65         dp = of_find_node_by_name(NULL, "virtual-devices");
66         if (!dp) {
67                 prom_printf("SUN4V: Fatal error, no virtual-devices node.\n");
68                 prom_halt();
69         }
70
71         sun4v_vdev_root = dp;
72
73         prop = of_find_property(dp, "reg", NULL);
74         regs = prop->value;
75         sun4v_vdev_devhandle = (regs[0].phys_addr >> 32UL) & 0x0fffffff;
76
77         prop = of_find_property(dp, "interrupt-map", &sz);
78         vdev_intmap = prop->value;
79         vdev_num_intmap = sz / sizeof(struct vdev_intmap);
80
81         prop = of_find_property(dp, "interrupt-map-mask", NULL);
82         vdev_intmask = prop->value;
83
84         printk("%s: Virtual Device Bus devhandle[%x]\n",
85                dp->full_name, sun4v_vdev_devhandle);
86 }
87
88 unsigned int sun4v_vdev_device_interrupt(struct device_node *dev_node)
89 {
90         struct property *prop;
91         unsigned int irq, reg;
92         int i;
93
94         prop = of_find_property(dev_node, "interrupts", NULL);
95         if (!prop) {
96                 printk("VDEV: Cannot get \"interrupts\" "
97                        "property for OBP node %s\n",
98                        dev_node->full_name);
99                 return 0;
100         }
101         irq = *(unsigned int *) prop->value;
102
103         prop = of_find_property(dev_node, "reg", NULL);
104         if (!prop) {
105                 printk("VDEV: Cannot get \"reg\" "
106                        "property for OBP node %s\n",
107                        dev_node->full_name);
108                 return 0;
109         }
110         reg = *(unsigned int *) prop->value;
111
112         for (i = 0; i < vdev_num_intmap; i++) {
113                 if (vdev_intmap[i].phys == (reg & vdev_intmask->phys) &&
114                     vdev_intmap[i].irq == (irq & vdev_intmask->interrupt)) {
115                         irq = vdev_intmap[i].cinterrupt;
116                         break;
117                 }
118         }
119
120         if (i == vdev_num_intmap) {
121                 printk("VDEV: No matching interrupt map entry "
122                        "for OBP node %s\n", dev_node->full_name);
123                 return 0;
124         }
125
126         return sun4v_build_irq(sun4v_vdev_devhandle, irq);
127 }
128
129 static const char *cpu_mid_prop(void)
130 {
131         if (tlb_type == spitfire)
132                 return "upa-portid";
133         return "portid";
134 }
135
136 static int get_cpu_mid(struct device_node *dp)
137 {
138         struct property *prop;
139
140         if (tlb_type == hypervisor) {
141                 struct linux_prom64_registers *reg;
142                 int len;
143
144                 prop = of_find_property(dp, "cpuid", &len);
145                 if (prop && len == 4)
146                         return *(int *) prop->value;
147
148                 prop = of_find_property(dp, "reg", NULL);
149                 reg = prop->value;
150                 return (reg[0].phys_addr >> 32) & 0x0fffffffUL;
151         } else {
152                 const char *prop_name = cpu_mid_prop();
153
154                 prop = of_find_property(dp, prop_name, NULL);
155                 if (prop)
156                         return *(int *) prop->value;
157                 return 0;
158         }
159 }
160
161 static int check_cpu_node(struct device_node *dp, int *cur_inst,
162                           int (*compare)(struct device_node *, int, void *),
163                           void *compare_arg,
164                           struct device_node **dev_node, int *mid)
165 {
166         if (strcmp(dp->type, "cpu"))
167                 return -ENODEV;
168
169         if (!compare(dp, *cur_inst, compare_arg)) {
170                 if (dev_node)
171                         *dev_node = dp;
172                 if (mid)
173                         *mid = get_cpu_mid(dp);
174                 return 0;
175         }
176
177         (*cur_inst)++;
178
179         return -ENODEV;
180 }
181
182 static int __cpu_find_by(int (*compare)(struct device_node *, int, void *),
183                          void *compare_arg,
184                          struct device_node **dev_node, int *mid)
185 {
186         struct device_node *dp;
187         int cur_inst;
188
189         cur_inst = 0;
190         for_each_node_by_type(dp, "cpu") {
191                 int err = check_cpu_node(dp, &cur_inst,
192                                          compare, compare_arg,
193                                          dev_node, mid);
194                 if (err == 0)
195                         return 0;
196         }
197
198         return -ENODEV;
199 }
200
201 static int cpu_instance_compare(struct device_node *dp, int instance, void *_arg)
202 {
203         int desired_instance = (int) (long) _arg;
204
205         if (instance == desired_instance)
206                 return 0;
207         return -ENODEV;
208 }
209
210 int cpu_find_by_instance(int instance, struct device_node **dev_node, int *mid)
211 {
212         return __cpu_find_by(cpu_instance_compare, (void *)(long)instance,
213                              dev_node, mid);
214 }
215
216 static int cpu_mid_compare(struct device_node *dp, int instance, void *_arg)
217 {
218         int desired_mid = (int) (long) _arg;
219         int this_mid;
220
221         this_mid = get_cpu_mid(dp);
222         if (this_mid == desired_mid)
223                 return 0;
224         return -ENODEV;
225 }
226
227 int cpu_find_by_mid(int mid, struct device_node **dev_node)
228 {
229         return __cpu_find_by(cpu_mid_compare, (void *)(long)mid,
230                              dev_node, NULL);
231 }
232
233 void __init device_scan(void)
234 {
235         /* FIX ME FAST... -DaveM */
236         ioport_resource.end = 0xffffffffffffffffUL;
237
238         prom_printf("Booting Linux...\n");
239
240 #ifndef CONFIG_SMP
241         {
242                 struct device_node *dp;
243                 int err, def;
244
245                 err = cpu_find_by_instance(0, &dp, NULL);
246                 if (err) {
247                         prom_printf("No cpu nodes, cannot continue\n");
248                         prom_halt();
249                 }
250                 cpu_data(0).clock_tick =
251                         of_getintprop_default(dp, "clock-frequency", 0);
252
253                 def = ((tlb_type == hypervisor) ?
254                        (8 * 1024) :
255                        (16 * 1024));
256                 cpu_data(0).dcache_size = of_getintprop_default(dp,
257                                                                 "dcache-size",
258                                                                 def);
259
260                 def = 32;
261                 cpu_data(0).dcache_line_size =
262                         of_getintprop_default(dp, "dcache-line-size", def);
263
264                 def = 16 * 1024;
265                 cpu_data(0).icache_size = of_getintprop_default(dp,
266                                                                 "icache-size",
267                                                                 def);
268
269                 def = 32;
270                 cpu_data(0).icache_line_size =
271                         of_getintprop_default(dp, "icache-line-size", def);
272
273                 def = ((tlb_type == hypervisor) ?
274                        (3 * 1024 * 1024) :
275                        (4 * 1024 * 1024));
276                 cpu_data(0).ecache_size = of_getintprop_default(dp,
277                                                                 "ecache-size",
278                                                                 def);
279
280                 def = 64;
281                 cpu_data(0).ecache_line_size =
282                         of_getintprop_default(dp, "ecache-line-size", def);
283                 printk("CPU[0]: Caches "
284                        "D[sz(%d):line_sz(%d)] "
285                        "I[sz(%d):line_sz(%d)] "
286                        "E[sz(%d):line_sz(%d)]\n",
287                        cpu_data(0).dcache_size, cpu_data(0).dcache_line_size,
288                        cpu_data(0).icache_size, cpu_data(0).icache_line_size,
289                        cpu_data(0).ecache_size, cpu_data(0).ecache_line_size);
290         }
291 #endif
292
293         sun4v_virtual_device_probe();
294         central_probe();
295
296         cpu_probe();
297 }