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>
26 struct leon3_irqctrl_regs_map
*leon3_irqctrl_regs
; /* interrupt controller base address */
27 struct leon3_gptimer_regs_map
*leon3_gptimer_regs
; /* timer controller base address */
28 struct amba_apb_device leon_percpu_timer_dev
[16];
30 int leondebug_irq_disable
;
31 int leon_debug_irqout
;
32 static int dummy_master_l10_counter
;
33 unsigned long amba_system_id
;
35 unsigned long leon3_gptimer_irq
; /* interrupt controller irq number */
36 unsigned long leon3_gptimer_idx
; /* Timer Index (0..6) within Timer Core */
37 unsigned int sparc_leon_eirq
;
38 #define LEON_IMASK ((&leon3_irqctrl_regs->mask[0]))
40 /* Return the IRQ of the pending IRQ on the extended IRQ controller */
41 int sparc_leon_eirq_get(int eirq
, int cpu
)
43 return LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs
->intid
[cpu
]) & 0x1f;
46 irqreturn_t
sparc_leon_eirq_isr(int dummy
, void *dev_id
)
48 printk(KERN_ERR
"sparc_leon_eirq_isr: ERROR EXTENDED IRQ\n");
52 /* The extended IRQ controller has been found, this function registers it */
53 void sparc_leon_eirq_register(int eirq
)
57 /* Register a "BAD" handler for this interrupt, it should never happen */
58 irq
= request_irq(eirq
, sparc_leon_eirq_isr
,
59 (IRQF_DISABLED
| SA_STATIC_ALLOC
), "extirq", NULL
);
63 "sparc_leon_eirq_register: unable to attach IRQ%d\n",
66 sparc_leon_eirq
= eirq
;
71 static inline unsigned long get_irqmask(unsigned int irq
)
75 if (!irq
|| ((irq
> 0xf) && !sparc_leon_eirq
)
76 || ((irq
> 0x1f) && sparc_leon_eirq
)) {
78 "leon_get_irqmask: false irq number: %d\n", irq
);
81 mask
= LEON_HARD_INT(irq
);
86 static void leon_unmask_irq(struct irq_data
*data
)
88 unsigned long mask
, flags
;
90 mask
= (unsigned long)data
->chip_data
;
91 local_irq_save(flags
);
92 LEON3_BYPASS_STORE_PA(LEON_IMASK
,
93 (LEON3_BYPASS_LOAD_PA(LEON_IMASK
) | (mask
)));
94 local_irq_restore(flags
);
97 static void leon_mask_irq(struct irq_data
*data
)
99 unsigned long mask
, flags
;
101 mask
= (unsigned long)data
->chip_data
;
102 local_irq_save(flags
);
103 LEON3_BYPASS_STORE_PA(LEON_IMASK
,
104 (LEON3_BYPASS_LOAD_PA(LEON_IMASK
) & ~(mask
)));
105 local_irq_restore(flags
);
109 static unsigned int leon_startup_irq(struct irq_data
*data
)
112 leon_unmask_irq(data
);
116 static void leon_shutdown_irq(struct irq_data
*data
)
119 irq_unlink(data
->irq
);
122 static struct irq_chip leon_irq
= {
124 .irq_startup
= leon_startup_irq
,
125 .irq_shutdown
= leon_shutdown_irq
,
126 .irq_mask
= leon_mask_irq
,
127 .irq_unmask
= leon_unmask_irq
,
130 static unsigned int leon_build_device_irq(struct platform_device
*op
,
131 unsigned int real_irq
)
137 mask
= get_irqmask(real_irq
);
141 irq
= irq_alloc(real_irq
, real_irq
);
145 irq_set_chip_and_handler_name(irq
, &leon_irq
,
146 handle_simple_irq
, "edge");
147 irq_set_chip_data(irq
, (void *)mask
);
153 void __init
leon_init_timers(irq_handler_t counter_fn
)
156 struct device_node
*rootnp
, *np
, *nnp
;
163 leondebug_irq_disable
= 0;
164 leon_debug_irqout
= 0;
165 master_l10_counter
= (unsigned int *)&dummy_master_l10_counter
;
166 dummy_master_l10_counter
= 0;
168 rootnp
= of_find_node_by_path("/ambapp0");
172 /* Find System ID: GRLIB build ID and optional CHIP ID */
173 pp
= of_find_property(rootnp
, "systemid", &len
);
175 amba_system_id
= *(unsigned long *)pp
->value
;
177 /* Find IRQMP IRQ Controller Registers base adr otherwise bail out */
178 np
= of_find_node_by_name(rootnp
, "GAISLER_IRQMP");
180 np
= of_find_node_by_name(rootnp
, "01_00d");
184 pp
= of_find_property(np
, "reg", &len
);
187 leon3_irqctrl_regs
= *(struct leon3_irqctrl_regs_map
**)pp
->value
;
189 /* Find GPTIMER Timer Registers base address otherwise bail out. */
192 np
= of_find_node_by_name(nnp
, "GAISLER_GPTIMER");
194 np
= of_find_node_by_name(nnp
, "01_011");
200 pp
= of_find_property(np
, "ampopts", &len
);
202 ampopts
= *(int *)pp
->value
;
204 /* Skip this instance, resource already
205 * allocated by other OS */
211 /* Select Timer-Instance on Timer Core. Default is zero */
212 leon3_gptimer_idx
= ampopts
& 0x7;
214 pp
= of_find_property(np
, "reg", &len
);
216 leon3_gptimer_regs
= *(struct leon3_gptimer_regs_map
**)
218 pp
= of_find_property(np
, "interrupts", &len
);
220 leon3_gptimer_irq
= *(unsigned int *)pp
->value
;
223 if (leon3_gptimer_regs
&& leon3_irqctrl_regs
&& leon3_gptimer_irq
) {
224 LEON3_BYPASS_STORE_PA(
225 &leon3_gptimer_regs
->e
[leon3_gptimer_idx
].val
, 0);
226 LEON3_BYPASS_STORE_PA(
227 &leon3_gptimer_regs
->e
[leon3_gptimer_idx
].rld
,
228 (((1000000 / HZ
) - 1)));
229 LEON3_BYPASS_STORE_PA(
230 &leon3_gptimer_regs
->e
[leon3_gptimer_idx
].ctrl
, 0);
233 leon_percpu_timer_dev
[0].start
= (int)leon3_gptimer_regs
;
234 leon_percpu_timer_dev
[0].irq
= leon3_gptimer_irq
+ 1 +
237 if (!(LEON3_BYPASS_LOAD_PA(&leon3_gptimer_regs
->config
) &
238 (1<<LEON3_GPTIMER_SEPIRQ
))) {
239 prom_printf("irq timer not configured with separate irqs\n");
243 LEON3_BYPASS_STORE_PA(
244 &leon3_gptimer_regs
->e
[leon3_gptimer_idx
+1].val
, 0);
245 LEON3_BYPASS_STORE_PA(
246 &leon3_gptimer_regs
->e
[leon3_gptimer_idx
+1].rld
,
247 (((1000000/HZ
) - 1)));
248 LEON3_BYPASS_STORE_PA(
249 &leon3_gptimer_regs
->e
[leon3_gptimer_idx
+1].ctrl
, 0);
253 * The IRQ controller may (if implemented) consist of multiple
254 * IRQ controllers, each mapped on a 4Kb boundary.
255 * Each CPU may be routed to different IRQCTRLs, however
256 * we assume that all CPUs (in SMP system) is routed to the
257 * same IRQ Controller, and for non-SMP only one IRQCTRL is
259 * In AMP systems, Linux must run on CPU0 for the time being.
261 cpu
= sparc_leon3_cpuid();
262 icsel
= LEON3_BYPASS_LOAD_PA(&leon3_irqctrl_regs
->icsel
[cpu
/8]);
263 icsel
= (icsel
>> ((7 - (cpu
&0x7)) * 4)) & 0xf;
264 leon3_irqctrl_regs
+= icsel
;
269 irq
= leon_build_device_irq(NULL
, leon3_gptimer_irq
+ leon3_gptimer_idx
);
270 err
= request_irq(irq
, counter_fn
, IRQF_TIMER
, "timer", NULL
);
273 printk(KERN_ERR
"leon_time_init: unable to attach IRQ%d\n",
274 LEON_INTERRUPT_TIMER1
);
281 struct tt_entry
*trap_table
= &sparc_ttable
[SP_TRAP_IRQ1
+ (leon_percpu_timer_dev
[0].irq
- 1)];
283 /* For SMP we use the level 14 ticker, however the bootup code
284 * has copied the firmwares level 14 vector into boot cpu's
285 * trap table, we must fix this now or we get squashed.
287 local_irq_save(flags
);
289 patchme_maybe_smp_msg
[0] = 0x01000000; /* NOP out the branch */
291 /* Adjust so that we jump directly to smpleon_ticker */
292 trap_table
->inst_three
+= smpleon_ticker
- real_irq_entry
;
294 local_flush_cache_all();
295 local_irq_restore(flags
);
299 if (leon3_gptimer_regs
) {
300 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs
->e
[leon3_gptimer_idx
].ctrl
,
303 LEON3_GPTIMER_LD
| LEON3_GPTIMER_IRQEN
);
306 LEON3_BYPASS_STORE_PA(&leon3_gptimer_regs
->e
[leon3_gptimer_idx
+1].ctrl
,
310 LEON3_GPTIMER_IRQEN
);
316 printk(KERN_ERR
"No Timer/irqctrl found\n");
321 void leon_clear_clock_irq(void)
325 void leon_load_profile_irq(int cpu
, unsigned int limit
)
333 void __init
leon_trans_init(struct device_node
*dp
)
335 if (strcmp(dp
->type
, "cpu") == 0 && strcmp(dp
->name
, "<NULL>") == 0) {
337 p
= of_find_property(dp
, "mid", (void *)0);
340 dp
->name
= prom_early_alloc(5 + 1);
341 memcpy(&mid
, p
->value
, p
->length
);
342 sprintf((char *)dp
->name
, "cpu%.2d", mid
);
347 void __initdata (*prom_amba_init
)(struct device_node
*dp
, struct device_node
***nextp
) = 0;
349 void __init
leon_node_init(struct device_node
*dp
, struct device_node
***nextp
)
351 if (prom_amba_init
&&
352 strcmp(dp
->type
, "ambapp") == 0 &&
353 strcmp(dp
->name
, "ambapp0") == 0) {
354 prom_amba_init(dp
, nextp
);
360 void leon_set_cpu_int(int cpu
, int level
)
363 mask
= get_irqmask(level
);
364 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs
->force
[cpu
], mask
);
367 static void leon_clear_ipi(int cpu
, int level
)
370 mask
= get_irqmask(level
);
371 LEON3_BYPASS_STORE_PA(&leon3_irqctrl_regs
->force
[cpu
], mask
<<16);
374 static void leon_set_udt(int cpu
)
378 void leon_clear_profile_irq(int cpu
)
382 void leon_enable_irq_cpu(unsigned int irq_nr
, unsigned int cpu
)
384 unsigned long mask
, flags
, *addr
;
385 mask
= get_irqmask(irq_nr
);
386 local_irq_save(flags
);
387 addr
= (unsigned long *)&(leon3_irqctrl_regs
->mask
[cpu
]);
388 LEON3_BYPASS_STORE_PA(addr
, (LEON3_BYPASS_LOAD_PA(addr
) | (mask
)));
389 local_irq_restore(flags
);
394 void __init
leon_init_IRQ(void)
396 sparc_irq_config
.init_timers
= leon_init_timers
;
397 sparc_irq_config
.build_device_irq
= leon_build_device_irq
;
399 BTFIXUPSET_CALL(clear_clock_irq
, leon_clear_clock_irq
,
401 BTFIXUPSET_CALL(load_profile_irq
, leon_load_profile_irq
,
405 BTFIXUPSET_CALL(set_cpu_int
, leon_set_cpu_int
, BTFIXUPCALL_NORM
);
406 BTFIXUPSET_CALL(clear_cpu_int
, leon_clear_ipi
, BTFIXUPCALL_NORM
);
407 BTFIXUPSET_CALL(set_irq_udt
, leon_set_udt
, BTFIXUPCALL_NORM
);
412 void __init
leon_init(void)
414 of_pdt_build_more
= &leon_node_init
;