2 * Copyright (C) 2009 Daniel Hellstrom (daniel@gaisler.com) Aeroflex Gaisler AB
3 * Copyright (C) 2009 Konrad Eisele (konrad@gaisler.com) Aeroflex Gaisler AB
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/errno.h>
9 #include <linux/mutex.h>
11 #include <linux/of_platform.h>
12 #include <linux/interrupt.h>
13 #include <linux/of_device.h>
15 #include <asm/oplib.h>
16 #include <asm/timer.h>
19 #include <asm/leon_amba.h>
20 #include <asm/traps.h>
21 #include <asm/cacheflush.h>
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 */
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
);
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
44 /* Return the last ACKed IRQ by the Extended IRQ controller. It has already
45 * been (automatically) ACKed when the CPU takes the trap.
47 static inline unsigned int leon_eirq_get(int cpu
)
49 return LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs
->intid
[cpu
]) & 0x1f;
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
)
56 int cpu
= hard_smp_processor_id();
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
);
63 /* The extended IRQ controller has been found, this function registers it */
64 void leon_eirq_setup(unsigned int eirq
)
66 unsigned long mask
, oldmask
;
69 if (eirq
< 1 || eirq
> 0xf) {
70 printk(KERN_ERR
"LEON EXT IRQ NUMBER BAD: %d\n", eirq
);
74 veirq
= leon_build_device_irq(eirq
, leon_handle_ext_irq
, "extirq", 0);
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.
82 oldmask
= LEON3_BYPASS_LOAD_PA(LEON_IMASK(0));
83 LEON3_BYPASS_STORE_PA(LEON_IMASK(0), (oldmask
| mask
));
84 sparc_leon_eirq
= eirq
;
87 static inline unsigned long get_irqmask(unsigned int irq
)
91 if (!irq
|| ((irq
> 0xf) && !sparc_leon_eirq
)
92 || ((irq
> 0x1f) && sparc_leon_eirq
)) {
94 "leon_get_irqmask: false irq number: %d\n", irq
);
97 mask
= LEON_HARD_INT(irq
);
102 static void leon_unmask_irq(struct irq_data
*data
)
104 unsigned long mask
, oldmask
, flags
;
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
);
113 static void leon_mask_irq(struct irq_data
*data
)
115 unsigned long mask
, oldmask
, flags
;
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
);
124 static unsigned int leon_startup_irq(struct irq_data
*data
)
127 leon_unmask_irq(data
);
131 static void leon_shutdown_irq(struct irq_data
*data
)
134 irq_unlink(data
->irq
);
137 /* Used by external level sensitive IRQ handlers on the LEON: ACK IRQ ctrl */
138 static void leon_eoi_irq(struct irq_data
*data
)
140 unsigned long mask
= (unsigned long)data
->chip_data
;
142 if (mask
& LEON_DO_ACK_HW
)
143 LEON3_BYPASS_STORE_PA(LEON_IACK
, mask
& ~LEON_DO_ACK_HW
);
146 static struct irq_chip leon_irq
= {
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
,
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
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
)
169 mask
= get_irqmask(real_irq
);
173 irq
= irq_alloc(real_irq
, real_irq
);
178 mask
|= LEON_DO_ACK_HW
;
180 irq_set_chip_and_handler_name(irq
, &leon_irq
,
182 irq_set_chip_data(irq
, (void *)mask
);
188 static unsigned int _leon_build_device_irq(struct platform_device
*op
,
189 unsigned int real_irq
)
191 return leon_build_device_irq(real_irq
, handle_simple_irq
, "edge", 0);
194 void __init
leon_init_timers(irq_handler_t counter_fn
)
197 struct device_node
*rootnp
, *np
, *nnp
;
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;
209 rootnp
= of_find_node_by_path("/ambapp0");
213 /* Find System ID: GRLIB build ID and optional CHIP ID */
214 pp
= of_find_property(rootnp
, "systemid", &len
);
216 amba_system_id
= *(unsigned long *)pp
->value
;
218 /* Find IRQMP IRQ Controller Registers base adr otherwise bail out */
219 np
= of_find_node_by_name(rootnp
, "GAISLER_IRQMP");
221 np
= of_find_node_by_name(rootnp
, "01_00d");
225 pp
= of_find_property(np
, "reg", &len
);
228 leon3_irqctrl_regs
= *(struct leon3_irqctrl_regs_map
**)pp
->value
;
230 /* Find GPTIMER Timer Registers base address otherwise bail out. */
233 np
= of_find_node_by_name(nnp
, "GAISLER_GPTIMER");
235 np
= of_find_node_by_name(nnp
, "01_011");
241 pp
= of_find_property(np
, "ampopts", &len
);
243 ampopts
= *(int *)pp
->value
;
245 /* Skip this instance, resource already
246 * allocated by other OS */
252 /* Select Timer-Instance on Timer Core. Default is zero */
253 leon3_gptimer_idx
= ampopts
& 0x7;
255 pp
= of_find_property(np
, "reg", &len
);
257 leon3_gptimer_regs
= *(struct leon3_gptimer_regs_map
**)
259 pp
= of_find_property(np
, "interrupts", &len
);
261 leon3_gptimer_irq
= *(unsigned int *)pp
->value
;
264 if (!(leon3_gptimer_regs
&& leon3_irqctrl_regs
&& leon3_gptimer_irq
))
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);
274 leon3_ticker_irq
= leon3_gptimer_irq
+ 1 + leon3_gptimer_idx
;
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");
282 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs
->e
[leon3_gptimer_idx
+1].val
,
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
,
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
297 * In AMP systems, Linux must run on CPU0 for the time being.
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
;
304 /* Probe extended IRQ controller */
305 eirq
= (LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs
->mpstatus
)
308 leon_eirq_setup(eirq
);
310 irq
= _leon_build_device_irq(NULL
, leon3_gptimer_irq
+leon3_gptimer_idx
);
311 err
= request_irq(irq
, counter_fn
, IRQF_TIMER
, "timer", NULL
);
313 printk(KERN_ERR
"unable to attach timer IRQ%d\n", irq
);
317 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs
->e
[leon3_gptimer_idx
].ctrl
,
321 LEON3_GPTIMER_IRQEN
);
324 /* Install per-cpu IRQ handler for broadcasted ticker */
325 irq
= leon_build_device_irq(leon3_ticker_irq
, handle_percpu_irq
,
327 err
= request_irq(irq
, leon_percpu_timer_interrupt
,
328 IRQF_PERCPU
| IRQF_TIMER
, "ticker",
331 printk(KERN_ERR
"unable to attach ticker IRQ%d\n", irq
);
335 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs
->e
[leon3_gptimer_idx
+1].ctrl
,
339 LEON3_GPTIMER_IRQEN
);
343 printk(KERN_ERR
"No Timer/irqctrl found\n");
348 void leon_clear_clock_irq(void)
352 void leon_load_profile_irq(int cpu
, unsigned int limit
)
357 void __init
leon_trans_init(struct device_node
*dp
)
359 if (strcmp(dp
->type
, "cpu") == 0 && strcmp(dp
->name
, "<NULL>") == 0) {
361 p
= of_find_property(dp
, "mid", (void *)0);
364 dp
->name
= prom_early_alloc(5 + 1);
365 memcpy(&mid
, p
->value
, p
->length
);
366 sprintf((char *)dp
->name
, "cpu%.2d", mid
);
371 void __initdata (*prom_amba_init
)(struct device_node
*dp
, struct device_node
***nextp
) = 0;
373 void __init
leon_node_init(struct device_node
*dp
, struct device_node
***nextp
)
375 if (prom_amba_init
&&
376 strcmp(dp
->type
, "ambapp") == 0 &&
377 strcmp(dp
->name
, "ambapp0") == 0) {
378 prom_amba_init(dp
, nextp
);
384 void leon_set_cpu_int(int cpu
, int level
)
387 mask
= get_irqmask(level
);
388 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs
->force
[cpu
], mask
);
391 static void leon_clear_ipi(int cpu
, int level
)
394 mask
= get_irqmask(level
);
395 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs
->force
[cpu
], mask
<<16);
398 static void leon_set_udt(int cpu
)
402 void leon_clear_profile_irq(int cpu
)
406 void leon_enable_irq_cpu(unsigned int irq_nr
, unsigned int cpu
)
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
);
418 void __init
leon_init_IRQ(void)
420 sparc_irq_config
.init_timers
= leon_init_timers
;
421 sparc_irq_config
.build_device_irq
= _leon_build_device_irq
;
423 BTFIXUPSET_CALL(clear_clock_irq
, leon_clear_clock_irq
,
425 BTFIXUPSET_CALL(load_profile_irq
, leon_load_profile_irq
,
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
);
436 void __init
leon_init(void)
438 of_pdt_build_more
= &leon_node_init
;