d8fafeacb4961eb42c326f337e4f770fe59c7f21
[deliverable/linux.git] / arch / sparc / kernel / leon_kernel.c
1 /*
2 * Copyright (C) 2009 Daniel Hellstrom (daniel@gaisler.com) Aeroflex Gaisler AB
3 * Copyright (C) 2009 Konrad Eisele (konrad@gaisler.com) Aeroflex Gaisler AB
4 */
5
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/errno.h>
9 #include <linux/mutex.h>
10 #include <linux/of.h>
11 #include <linux/of_platform.h>
12 #include <linux/interrupt.h>
13 #include <linux/of_device.h>
14
15 #include <asm/oplib.h>
16 #include <asm/timer.h>
17 #include <asm/prom.h>
18 #include <asm/leon.h>
19 #include <asm/leon_amba.h>
20 #include <asm/traps.h>
21 #include <asm/cacheflush.h>
22 #include <asm/smp.h>
23
24 #include "prom.h"
25 #include "irq.h"
26
27 struct leon3_irqctrl_regs_map *leon3_irqctrl_regs; /* interrupt controller base address */
28 struct leon3_gptimer_regs_map *leon3_gptimer_regs; /* timer controller base address */
29
30 int leondebug_irq_disable;
31 int leon_debug_irqout;
32 static int dummy_master_l10_counter;
33 unsigned long amba_system_id;
34 static DEFINE_SPINLOCK(leon_irq_lock);
35
36 unsigned long leon3_gptimer_irq; /* interrupt controller irq number */
37 unsigned long leon3_gptimer_idx; /* Timer Index (0..6) within Timer Core */
38 int leon3_ticker_irq; /* Timer ticker IRQ */
39 unsigned int sparc_leon_eirq;
40 #define LEON_IMASK(cpu) (&leon3_irqctrl_regs->mask[cpu])
41 #define LEON_IACK (&leon3_irqctrl_regs->iclear)
42 #define LEON_DO_ACK_HW 1
43
44 /* Return the last ACKed IRQ by the Extended IRQ controller. It has already
45 * been (automatically) ACKed when the CPU takes the trap.
46 */
47 static inline unsigned int leon_eirq_get(int cpu)
48 {
49 return LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->intid[cpu]) & 0x1f;
50 }
51
52 /* Handle one or multiple IRQs from the extended interrupt controller */
53 static void leon_handle_ext_irq(unsigned int irq, struct irq_desc *desc)
54 {
55 unsigned int eirq;
56 int cpu = hard_smp_processor_id();
57
58 eirq = leon_eirq_get(cpu);
59 if ((eirq & 0x10) && irq_map[eirq]->irq) /* bit4 tells if IRQ happened */
60 generic_handle_irq(irq_map[eirq]->irq);
61 }
62
63 /* The extended IRQ controller has been found, this function registers it */
64 void leon_eirq_setup(unsigned int eirq)
65 {
66 unsigned long mask, oldmask;
67 unsigned int veirq;
68
69 if (eirq < 1 || eirq > 0xf) {
70 printk(KERN_ERR "LEON EXT IRQ NUMBER BAD: %d\n", eirq);
71 return;
72 }
73
74 veirq = leon_build_device_irq(eirq, leon_handle_ext_irq, "extirq", 0);
75
76 /*
77 * Unmask the Extended IRQ, the IRQs routed through the Ext-IRQ
78 * controller have a mask-bit of their own, so this is safe.
79 */
80 irq_link(veirq);
81 mask = 1 << eirq;
82 oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(0));
83 LEON3_BYPASS_STORE_PA(LEON_IMASK(0), (oldmask | mask));
84 sparc_leon_eirq = eirq;
85 }
86
87 static inline unsigned long get_irqmask(unsigned int irq)
88 {
89 unsigned long mask;
90
91 if (!irq || ((irq > 0xf) && !sparc_leon_eirq)
92 || ((irq > 0x1f) && sparc_leon_eirq)) {
93 printk(KERN_ERR
94 "leon_get_irqmask: false irq number: %d\n", irq);
95 mask = 0;
96 } else {
97 mask = LEON_HARD_INT(irq);
98 }
99 return mask;
100 }
101
102 static void leon_unmask_irq(struct irq_data *data)
103 {
104 unsigned long mask, oldmask, flags;
105
106 mask = (unsigned long)data->chip_data;
107 spin_lock_irqsave(&leon_irq_lock, flags);
108 oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(0));
109 LEON3_BYPASS_STORE_PA(LEON_IMASK(0), (oldmask | mask));
110 spin_unlock_irqrestore(&leon_irq_lock, flags);
111 }
112
113 static void leon_mask_irq(struct irq_data *data)
114 {
115 unsigned long mask, oldmask, flags;
116
117 mask = (unsigned long)data->chip_data;
118 spin_lock_irqsave(&leon_irq_lock, flags);
119 oldmask = LEON3_BYPASS_LOAD_PA(LEON_IMASK(0));
120 LEON3_BYPASS_STORE_PA(LEON_IMASK(0), (oldmask & ~mask));
121 spin_unlock_irqrestore(&leon_irq_lock, flags);
122 }
123
124 static unsigned int leon_startup_irq(struct irq_data *data)
125 {
126 irq_link(data->irq);
127 leon_unmask_irq(data);
128 return 0;
129 }
130
131 static void leon_shutdown_irq(struct irq_data *data)
132 {
133 leon_mask_irq(data);
134 irq_unlink(data->irq);
135 }
136
137 /* Used by external level sensitive IRQ handlers on the LEON: ACK IRQ ctrl */
138 static void leon_eoi_irq(struct irq_data *data)
139 {
140 unsigned long mask = (unsigned long)data->chip_data;
141
142 if (mask & LEON_DO_ACK_HW)
143 LEON3_BYPASS_STORE_PA(LEON_IACK, mask & ~LEON_DO_ACK_HW);
144 }
145
146 static struct irq_chip leon_irq = {
147 .name = "leon",
148 .irq_startup = leon_startup_irq,
149 .irq_shutdown = leon_shutdown_irq,
150 .irq_mask = leon_mask_irq,
151 .irq_unmask = leon_unmask_irq,
152 .irq_eoi = leon_eoi_irq,
153 };
154
155 /*
156 * Build a LEON IRQ for the edge triggered LEON IRQ controller:
157 * Edge (normal) IRQ - handle_simple_irq, ack=DONT-CARE, never ack
158 * Level IRQ (PCI|Level-GPIO) - handle_fasteoi_irq, ack=1, ack after ISR
159 * Per-CPU Edge - handle_percpu_irq, ack=0
160 */
161 unsigned int leon_build_device_irq(unsigned int real_irq,
162 irq_flow_handler_t flow_handler,
163 const char *name, int do_ack)
164 {
165 unsigned int irq;
166 unsigned long mask;
167
168 irq = 0;
169 mask = get_irqmask(real_irq);
170 if (mask == 0)
171 goto out;
172
173 irq = irq_alloc(real_irq, real_irq);
174 if (irq == 0)
175 goto out;
176
177 if (do_ack)
178 mask |= LEON_DO_ACK_HW;
179
180 irq_set_chip_and_handler_name(irq, &leon_irq,
181 flow_handler, name);
182 irq_set_chip_data(irq, (void *)mask);
183
184 out:
185 return irq;
186 }
187
188 static unsigned int _leon_build_device_irq(struct platform_device *op,
189 unsigned int real_irq)
190 {
191 return leon_build_device_irq(real_irq, handle_simple_irq, "edge", 0);
192 }
193
194 void __init leon_init_timers(irq_handler_t counter_fn)
195 {
196 int irq, eirq;
197 struct device_node *rootnp, *np, *nnp;
198 struct property *pp;
199 int len;
200 int cpu, icsel;
201 int ampopts;
202 int err;
203
204 leondebug_irq_disable = 0;
205 leon_debug_irqout = 0;
206 master_l10_counter = (unsigned int *)&dummy_master_l10_counter;
207 dummy_master_l10_counter = 0;
208
209 rootnp = of_find_node_by_path("/ambapp0");
210 if (!rootnp)
211 goto bad;
212
213 /* Find System ID: GRLIB build ID and optional CHIP ID */
214 pp = of_find_property(rootnp, "systemid", &len);
215 if (pp)
216 amba_system_id = *(unsigned long *)pp->value;
217
218 /* Find IRQMP IRQ Controller Registers base adr otherwise bail out */
219 np = of_find_node_by_name(rootnp, "GAISLER_IRQMP");
220 if (!np) {
221 np = of_find_node_by_name(rootnp, "01_00d");
222 if (!np)
223 goto bad;
224 }
225 pp = of_find_property(np, "reg", &len);
226 if (!pp)
227 goto bad;
228 leon3_irqctrl_regs = *(struct leon3_irqctrl_regs_map **)pp->value;
229
230 /* Find GPTIMER Timer Registers base address otherwise bail out. */
231 nnp = rootnp;
232 do {
233 np = of_find_node_by_name(nnp, "GAISLER_GPTIMER");
234 if (!np) {
235 np = of_find_node_by_name(nnp, "01_011");
236 if (!np)
237 goto bad;
238 }
239
240 ampopts = 0;
241 pp = of_find_property(np, "ampopts", &len);
242 if (pp) {
243 ampopts = *(int *)pp->value;
244 if (ampopts == 0) {
245 /* Skip this instance, resource already
246 * allocated by other OS */
247 nnp = np;
248 continue;
249 }
250 }
251
252 /* Select Timer-Instance on Timer Core. Default is zero */
253 leon3_gptimer_idx = ampopts & 0x7;
254
255 pp = of_find_property(np, "reg", &len);
256 if (pp)
257 leon3_gptimer_regs = *(struct leon3_gptimer_regs_map **)
258 pp->value;
259 pp = of_find_property(np, "interrupts", &len);
260 if (pp)
261 leon3_gptimer_irq = *(unsigned int *)pp->value;
262 } while (0);
263
264 if (!(leon3_gptimer_regs && leon3_irqctrl_regs && leon3_gptimer_irq))
265 goto bad;
266
267 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].val, 0);
268 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].rld,
269 (((1000000 / HZ) - 1)));
270 LEON3_BYPASS_STORE_PA(
271 &leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl, 0);
272
273 #ifdef CONFIG_SMP
274 leon3_ticker_irq = leon3_gptimer_irq + 1 + leon3_gptimer_idx;
275
276 if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs->config) &
277 (1<<LEON3_GPTIMER_SEPIRQ))) {
278 printk(KERN_ERR "timer not configured with separate irqs\n");
279 BUG();
280 }
281
282 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].val,
283 0);
284 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].rld,
285 (((1000000/HZ) - 1)));
286 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl,
287 0);
288 #endif
289
290 /*
291 * The IRQ controller may (if implemented) consist of multiple
292 * IRQ controllers, each mapped on a 4Kb boundary.
293 * Each CPU may be routed to different IRQCTRLs, however
294 * we assume that all CPUs (in SMP system) is routed to the
295 * same IRQ Controller, and for non-SMP only one IRQCTRL is
296 * accessed anyway.
297 * In AMP systems, Linux must run on CPU0 for the time being.
298 */
299 cpu = sparc_leon3_cpuid();
300 icsel = LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->icsel[cpu/8]);
301 icsel = (icsel >> ((7 - (cpu&0x7)) * 4)) & 0xf;
302 leon3_irqctrl_regs += icsel;
303
304 /* Probe extended IRQ controller */
305 eirq = (LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs->mpstatus)
306 >> 16) & 0xf;
307 if (eirq != 0)
308 leon_eirq_setup(eirq);
309
310 irq = _leon_build_device_irq(NULL, leon3_gptimer_irq+leon3_gptimer_idx);
311 err = request_irq(irq, counter_fn, IRQF_TIMER, "timer", NULL);
312 if (err) {
313 printk(KERN_ERR "unable to attach timer IRQ%d\n", irq);
314 prom_halt();
315 }
316
317 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx].ctrl,
318 LEON3_GPTIMER_EN |
319 LEON3_GPTIMER_RL |
320 LEON3_GPTIMER_LD |
321 LEON3_GPTIMER_IRQEN);
322
323 #ifdef CONFIG_SMP
324 /* Install per-cpu IRQ handler for broadcasted ticker */
325 irq = leon_build_device_irq(leon3_ticker_irq, handle_percpu_irq,
326 "per-cpu", 0);
327 err = request_irq(irq, leon_percpu_timer_interrupt,
328 IRQF_PERCPU | IRQF_TIMER, "ticker",
329 NULL);
330 if (err) {
331 printk(KERN_ERR "unable to attach ticker IRQ%d\n", irq);
332 prom_halt();
333 }
334
335 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs->e[leon3_gptimer_idx+1].ctrl,
336 LEON3_GPTIMER_EN |
337 LEON3_GPTIMER_RL |
338 LEON3_GPTIMER_LD |
339 LEON3_GPTIMER_IRQEN);
340 #endif
341 return;
342 bad:
343 printk(KERN_ERR "No Timer/irqctrl found\n");
344 BUG();
345 return;
346 }
347
348 void leon_clear_clock_irq(void)
349 {
350 }
351
352 void leon_load_profile_irq(int cpu, unsigned int limit)
353 {
354 BUG();
355 }
356
357 void __init leon_trans_init(struct device_node *dp)
358 {
359 if (strcmp(dp->type, "cpu") == 0 && strcmp(dp->name, "<NULL>") == 0) {
360 struct property *p;
361 p = of_find_property(dp, "mid", (void *)0);
362 if (p) {
363 int mid;
364 dp->name = prom_early_alloc(5 + 1);
365 memcpy(&mid, p->value, p->length);
366 sprintf((char *)dp->name, "cpu%.2d", mid);
367 }
368 }
369 }
370
371 void __initdata (*prom_amba_init)(struct device_node *dp, struct device_node ***nextp) = 0;
372
373 void __init leon_node_init(struct device_node *dp, struct device_node ***nextp)
374 {
375 if (prom_amba_init &&
376 strcmp(dp->type, "ambapp") == 0 &&
377 strcmp(dp->name, "ambapp0") == 0) {
378 prom_amba_init(dp, nextp);
379 }
380 }
381
382 #ifdef CONFIG_SMP
383
384 void leon_set_cpu_int(int cpu, int level)
385 {
386 unsigned long mask;
387 mask = get_irqmask(level);
388 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask);
389 }
390
391 static void leon_clear_ipi(int cpu, int level)
392 {
393 unsigned long mask;
394 mask = get_irqmask(level);
395 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs->force[cpu], mask<<16);
396 }
397
398 static void leon_set_udt(int cpu)
399 {
400 }
401
402 void leon_clear_profile_irq(int cpu)
403 {
404 }
405
406 void leon_enable_irq_cpu(unsigned int irq_nr, unsigned int cpu)
407 {
408 unsigned long mask, flags, *addr;
409 mask = get_irqmask(irq_nr);
410 spin_lock_irqsave(&leon_irq_lock, flags);
411 addr = (unsigned long *)LEON_IMASK(cpu);
412 LEON3_BYPASS_STORE_PA(addr, (LEON3_BYPASS_LOAD_PA(addr) | mask));
413 spin_unlock_irqrestore(&leon_irq_lock, flags);
414 }
415
416 #endif
417
418 void __init leon_init_IRQ(void)
419 {
420 sparc_irq_config.init_timers = leon_init_timers;
421 sparc_irq_config.build_device_irq = _leon_build_device_irq;
422
423 BTFIXUPSET_CALL(clear_clock_irq, leon_clear_clock_irq,
424 BTFIXUPCALL_NORM);
425 BTFIXUPSET_CALL(load_profile_irq, leon_load_profile_irq,
426 BTFIXUPCALL_NOP);
427
428 #ifdef CONFIG_SMP
429 BTFIXUPSET_CALL(set_cpu_int, leon_set_cpu_int, BTFIXUPCALL_NORM);
430 BTFIXUPSET_CALL(clear_cpu_int, leon_clear_ipi, BTFIXUPCALL_NORM);
431 BTFIXUPSET_CALL(set_irq_udt, leon_set_udt, BTFIXUPCALL_NORM);
432 #endif
433
434 }
435
436 void __init leon_init(void)
437 {
438 of_pdt_build_more = &leon_node_init;
439 }
This page took 0.039508 seconds and 4 git commands to generate.