2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
7 * Copyright (C) 2008 Nicolas Schichan <nschichan@freebox.fr>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/spinlock.h>
15 #include <asm/irq_cpu.h>
16 #include <asm/mipsregs.h>
17 #include <bcm63xx_cpu.h>
18 #include <bcm63xx_regs.h>
19 #include <bcm63xx_io.h>
20 #include <bcm63xx_irq.h>
23 static DEFINE_SPINLOCK(ipic_lock
);
24 static DEFINE_SPINLOCK(epic_lock
);
26 static u32 irq_stat_addr
[2];
27 static u32 irq_mask_addr
[2];
28 static void (*dispatch_internal
)(int cpu
);
29 static int is_ext_irq_cascaded
;
30 static unsigned int ext_irq_count
;
31 static unsigned int ext_irq_start
, ext_irq_end
;
32 static unsigned int ext_irq_cfg_reg1
, ext_irq_cfg_reg2
;
33 static void (*internal_irq_mask
)(struct irq_data
*d
);
34 static void (*internal_irq_unmask
)(struct irq_data
*d
, const struct cpumask
*m
);
37 static inline u32
get_ext_irq_perf_reg(int irq
)
40 return ext_irq_cfg_reg1
;
41 return ext_irq_cfg_reg2
;
44 static inline void handle_internal(int intbit
)
46 if (is_ext_irq_cascaded
&&
47 intbit
>= ext_irq_start
&& intbit
<= ext_irq_end
)
48 do_IRQ(intbit
- ext_irq_start
+ IRQ_EXTERNAL_BASE
);
50 do_IRQ(intbit
+ IRQ_INTERNAL_BASE
);
53 static inline int enable_irq_for_cpu(int cpu
, struct irq_data
*d
,
54 const struct cpumask
*m
)
56 bool enable
= cpu_online(cpu
);
60 enable
&= cpumask_test_cpu(cpu
, m
);
61 else if (irqd_affinity_was_set(d
))
62 enable
&= cpumask_test_cpu(cpu
, irq_data_get_affinity_mask(d
));
68 * dispatch internal devices IRQ (uart, enet, watchdog, ...). do not
69 * prioritize any interrupt relatively to another. the static counter
70 * will resume the loop where it ended the last time we left this
74 #define BUILD_IPIC_INTERNAL(width) \
75 void __dispatch_internal_##width(int cpu) \
77 u32 pending[width / 32]; \
78 unsigned int src, tgt; \
79 bool irqs_pending = false; \
80 static unsigned int i[2]; \
81 unsigned int *next = &i[cpu]; \
82 unsigned long flags; \
84 /* read registers in reverse order */ \
85 spin_lock_irqsave(&ipic_lock, flags); \
86 for (src = 0, tgt = (width / 32); src < (width / 32); src++) { \
89 val = bcm_readl(irq_stat_addr[cpu] + src * sizeof(u32)); \
90 val &= bcm_readl(irq_mask_addr[cpu] + src * sizeof(u32)); \
91 pending[--tgt] = val; \
94 irqs_pending = true; \
96 spin_unlock_irqrestore(&ipic_lock, flags); \
102 unsigned int to_call = *next; \
104 *next = (*next + 1) & (width - 1); \
105 if (pending[to_call / 32] & (1 << (to_call & 0x1f))) { \
106 handle_internal(to_call); \
112 static void __internal_irq_mask_##width(struct irq_data *d) \
115 unsigned irq = d->irq - IRQ_INTERNAL_BASE; \
116 unsigned reg = (irq / 32) ^ (width/32 - 1); \
117 unsigned bit = irq & 0x1f; \
118 unsigned long flags; \
121 spin_lock_irqsave(&ipic_lock, flags); \
122 for_each_present_cpu(cpu) { \
123 if (!irq_mask_addr[cpu]) \
126 val = bcm_readl(irq_mask_addr[cpu] + reg * sizeof(u32));\
127 val &= ~(1 << bit); \
128 bcm_writel(val, irq_mask_addr[cpu] + reg * sizeof(u32));\
130 spin_unlock_irqrestore(&ipic_lock, flags); \
133 static void __internal_irq_unmask_##width(struct irq_data *d, \
134 const struct cpumask *m) \
137 unsigned irq = d->irq - IRQ_INTERNAL_BASE; \
138 unsigned reg = (irq / 32) ^ (width/32 - 1); \
139 unsigned bit = irq & 0x1f; \
140 unsigned long flags; \
143 spin_lock_irqsave(&ipic_lock, flags); \
144 for_each_present_cpu(cpu) { \
145 if (!irq_mask_addr[cpu]) \
148 val = bcm_readl(irq_mask_addr[cpu] + reg * sizeof(u32));\
149 if (enable_irq_for_cpu(cpu, d, m)) \
152 val &= ~(1 << bit); \
153 bcm_writel(val, irq_mask_addr[cpu] + reg * sizeof(u32));\
155 spin_unlock_irqrestore(&ipic_lock, flags); \
158 BUILD_IPIC_INTERNAL(32);
159 BUILD_IPIC_INTERNAL(64);
161 asmlinkage
void plat_irq_dispatch(void)
166 cause
= read_c0_cause() & read_c0_status() & ST0_IM
;
171 if (cause
& CAUSEF_IP7
)
173 if (cause
& CAUSEF_IP0
)
175 if (cause
& CAUSEF_IP1
)
177 if (cause
& CAUSEF_IP2
)
178 dispatch_internal(0);
179 if (is_ext_irq_cascaded
) {
180 if (cause
& CAUSEF_IP3
)
181 dispatch_internal(1);
183 if (cause
& CAUSEF_IP3
)
185 if (cause
& CAUSEF_IP4
)
187 if (cause
& CAUSEF_IP5
)
189 if (cause
& CAUSEF_IP6
)
196 * internal IRQs operations: only mask/unmask on PERF irq mask
199 static void bcm63xx_internal_irq_mask(struct irq_data
*d
)
201 internal_irq_mask(d
);
204 static void bcm63xx_internal_irq_unmask(struct irq_data
*d
)
206 internal_irq_unmask(d
, NULL
);
210 * external IRQs operations: mask/unmask and clear on PERF external
211 * irq control register.
213 static void bcm63xx_external_irq_mask(struct irq_data
*d
)
215 unsigned int irq
= d
->irq
- IRQ_EXTERNAL_BASE
;
219 regaddr
= get_ext_irq_perf_reg(irq
);
220 spin_lock_irqsave(&epic_lock
, flags
);
221 reg
= bcm_perf_readl(regaddr
);
223 if (BCMCPU_IS_6348())
224 reg
&= ~EXTIRQ_CFG_MASK_6348(irq
% 4);
226 reg
&= ~EXTIRQ_CFG_MASK(irq
% 4);
228 bcm_perf_writel(reg
, regaddr
);
229 spin_unlock_irqrestore(&epic_lock
, flags
);
231 if (is_ext_irq_cascaded
)
232 internal_irq_mask(irq_get_irq_data(irq
+ ext_irq_start
));
235 static void bcm63xx_external_irq_unmask(struct irq_data
*d
)
237 unsigned int irq
= d
->irq
- IRQ_EXTERNAL_BASE
;
241 regaddr
= get_ext_irq_perf_reg(irq
);
242 spin_lock_irqsave(&epic_lock
, flags
);
243 reg
= bcm_perf_readl(regaddr
);
245 if (BCMCPU_IS_6348())
246 reg
|= EXTIRQ_CFG_MASK_6348(irq
% 4);
248 reg
|= EXTIRQ_CFG_MASK(irq
% 4);
250 bcm_perf_writel(reg
, regaddr
);
251 spin_unlock_irqrestore(&epic_lock
, flags
);
253 if (is_ext_irq_cascaded
)
254 internal_irq_unmask(irq_get_irq_data(irq
+ ext_irq_start
),
258 static void bcm63xx_external_irq_clear(struct irq_data
*d
)
260 unsigned int irq
= d
->irq
- IRQ_EXTERNAL_BASE
;
264 regaddr
= get_ext_irq_perf_reg(irq
);
265 spin_lock_irqsave(&epic_lock
, flags
);
266 reg
= bcm_perf_readl(regaddr
);
268 if (BCMCPU_IS_6348())
269 reg
|= EXTIRQ_CFG_CLEAR_6348(irq
% 4);
271 reg
|= EXTIRQ_CFG_CLEAR(irq
% 4);
273 bcm_perf_writel(reg
, regaddr
);
274 spin_unlock_irqrestore(&epic_lock
, flags
);
277 static int bcm63xx_external_irq_set_type(struct irq_data
*d
,
278 unsigned int flow_type
)
280 unsigned int irq
= d
->irq
- IRQ_EXTERNAL_BASE
;
282 int levelsense
, sense
, bothedge
;
285 flow_type
&= IRQ_TYPE_SENSE_MASK
;
287 if (flow_type
== IRQ_TYPE_NONE
)
288 flow_type
= IRQ_TYPE_LEVEL_LOW
;
290 levelsense
= sense
= bothedge
= 0;
292 case IRQ_TYPE_EDGE_BOTH
:
296 case IRQ_TYPE_EDGE_RISING
:
300 case IRQ_TYPE_EDGE_FALLING
:
303 case IRQ_TYPE_LEVEL_HIGH
:
308 case IRQ_TYPE_LEVEL_LOW
:
313 pr_err("bogus flow type combination given !\n");
317 regaddr
= get_ext_irq_perf_reg(irq
);
318 spin_lock_irqsave(&epic_lock
, flags
);
319 reg
= bcm_perf_readl(regaddr
);
322 switch (bcm63xx_get_cpu_id()) {
325 reg
|= EXTIRQ_CFG_LEVELSENSE_6348(irq
);
327 reg
&= ~EXTIRQ_CFG_LEVELSENSE_6348(irq
);
329 reg
|= EXTIRQ_CFG_SENSE_6348(irq
);
331 reg
&= ~EXTIRQ_CFG_SENSE_6348(irq
);
333 reg
|= EXTIRQ_CFG_BOTHEDGE_6348(irq
);
335 reg
&= ~EXTIRQ_CFG_BOTHEDGE_6348(irq
);
346 reg
|= EXTIRQ_CFG_LEVELSENSE(irq
);
348 reg
&= ~EXTIRQ_CFG_LEVELSENSE(irq
);
350 reg
|= EXTIRQ_CFG_SENSE(irq
);
352 reg
&= ~EXTIRQ_CFG_SENSE(irq
);
354 reg
|= EXTIRQ_CFG_BOTHEDGE(irq
);
356 reg
&= ~EXTIRQ_CFG_BOTHEDGE(irq
);
362 bcm_perf_writel(reg
, regaddr
);
363 spin_unlock_irqrestore(&epic_lock
, flags
);
365 irqd_set_trigger_type(d
, flow_type
);
366 if (flow_type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_LEVEL_HIGH
))
367 irq_set_handler_locked(d
, handle_level_irq
);
369 irq_set_handler_locked(d
, handle_edge_irq
);
371 return IRQ_SET_MASK_OK_NOCOPY
;
375 static int bcm63xx_internal_set_affinity(struct irq_data
*data
,
376 const struct cpumask
*dest
,
379 if (!irqd_irq_disabled(data
))
380 internal_irq_unmask(data
, dest
);
386 static struct irq_chip bcm63xx_internal_irq_chip
= {
387 .name
= "bcm63xx_ipic",
388 .irq_mask
= bcm63xx_internal_irq_mask
,
389 .irq_unmask
= bcm63xx_internal_irq_unmask
,
392 static struct irq_chip bcm63xx_external_irq_chip
= {
393 .name
= "bcm63xx_epic",
394 .irq_ack
= bcm63xx_external_irq_clear
,
396 .irq_mask
= bcm63xx_external_irq_mask
,
397 .irq_unmask
= bcm63xx_external_irq_unmask
,
399 .irq_set_type
= bcm63xx_external_irq_set_type
,
402 static struct irqaction cpu_ip2_cascade_action
= {
403 .handler
= no_action
,
404 .name
= "cascade_ip2",
405 .flags
= IRQF_NO_THREAD
,
409 static struct irqaction cpu_ip3_cascade_action
= {
410 .handler
= no_action
,
411 .name
= "cascade_ip3",
412 .flags
= IRQF_NO_THREAD
,
416 static struct irqaction cpu_ext_cascade_action
= {
417 .handler
= no_action
,
418 .name
= "cascade_extirq",
419 .flags
= IRQF_NO_THREAD
,
422 static void bcm63xx_init_irq(void)
426 irq_stat_addr
[0] = bcm63xx_regset_address(RSET_PERF
);
427 irq_mask_addr
[0] = bcm63xx_regset_address(RSET_PERF
);
428 irq_stat_addr
[1] = bcm63xx_regset_address(RSET_PERF
);
429 irq_mask_addr
[1] = bcm63xx_regset_address(RSET_PERF
);
431 switch (bcm63xx_get_cpu_id()) {
433 irq_stat_addr
[0] += PERF_IRQSTAT_3368_REG
;
434 irq_mask_addr
[0] += PERF_IRQMASK_3368_REG
;
435 irq_stat_addr
[1] = 0;
436 irq_mask_addr
[1] = 0;
439 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_3368
;
442 irq_stat_addr
[0] += PERF_IRQSTAT_6328_REG(0);
443 irq_mask_addr
[0] += PERF_IRQMASK_6328_REG(0);
444 irq_stat_addr
[1] += PERF_IRQSTAT_6328_REG(1);
445 irq_mask_addr
[1] += PERF_IRQMASK_6328_REG(1);
448 is_ext_irq_cascaded
= 1;
449 ext_irq_start
= BCM_6328_EXT_IRQ0
- IRQ_INTERNAL_BASE
;
450 ext_irq_end
= BCM_6328_EXT_IRQ3
- IRQ_INTERNAL_BASE
;
451 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6328
;
454 irq_stat_addr
[0] += PERF_IRQSTAT_6338_REG
;
455 irq_mask_addr
[0] += PERF_IRQMASK_6338_REG
;
456 irq_stat_addr
[1] = 0;
457 irq_mask_addr
[1] = 0;
460 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6338
;
463 irq_stat_addr
[0] += PERF_IRQSTAT_6345_REG
;
464 irq_mask_addr
[0] += PERF_IRQMASK_6345_REG
;
465 irq_stat_addr
[1] = 0;
466 irq_mask_addr
[1] = 0;
469 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6345
;
472 irq_stat_addr
[0] += PERF_IRQSTAT_6348_REG
;
473 irq_mask_addr
[0] += PERF_IRQMASK_6348_REG
;
474 irq_stat_addr
[1] = 0;
475 irq_mask_addr
[1] = 0;
478 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6348
;
481 irq_stat_addr
[0] += PERF_IRQSTAT_6358_REG(0);
482 irq_mask_addr
[0] += PERF_IRQMASK_6358_REG(0);
483 irq_stat_addr
[1] += PERF_IRQSTAT_6358_REG(1);
484 irq_mask_addr
[1] += PERF_IRQMASK_6358_REG(1);
487 is_ext_irq_cascaded
= 1;
488 ext_irq_start
= BCM_6358_EXT_IRQ0
- IRQ_INTERNAL_BASE
;
489 ext_irq_end
= BCM_6358_EXT_IRQ3
- IRQ_INTERNAL_BASE
;
490 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6358
;
493 irq_stat_addr
[0] += PERF_IRQSTAT_6362_REG(0);
494 irq_mask_addr
[0] += PERF_IRQMASK_6362_REG(0);
495 irq_stat_addr
[1] += PERF_IRQSTAT_6362_REG(1);
496 irq_mask_addr
[1] += PERF_IRQMASK_6362_REG(1);
499 is_ext_irq_cascaded
= 1;
500 ext_irq_start
= BCM_6362_EXT_IRQ0
- IRQ_INTERNAL_BASE
;
501 ext_irq_end
= BCM_6362_EXT_IRQ3
- IRQ_INTERNAL_BASE
;
502 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6362
;
505 irq_stat_addr
[0] += PERF_IRQSTAT_6368_REG(0);
506 irq_mask_addr
[0] += PERF_IRQMASK_6368_REG(0);
507 irq_stat_addr
[1] += PERF_IRQSTAT_6368_REG(1);
508 irq_mask_addr
[1] += PERF_IRQMASK_6368_REG(1);
511 is_ext_irq_cascaded
= 1;
512 ext_irq_start
= BCM_6368_EXT_IRQ0
- IRQ_INTERNAL_BASE
;
513 ext_irq_end
= BCM_6368_EXT_IRQ5
- IRQ_INTERNAL_BASE
;
514 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6368
;
515 ext_irq_cfg_reg2
= PERF_EXTIRQ_CFG_REG2_6368
;
521 if (irq_bits
== 32) {
522 dispatch_internal
= __dispatch_internal_32
;
523 internal_irq_mask
= __internal_irq_mask_32
;
524 internal_irq_unmask
= __internal_irq_unmask_32
;
526 dispatch_internal
= __dispatch_internal_64
;
527 internal_irq_mask
= __internal_irq_mask_64
;
528 internal_irq_unmask
= __internal_irq_unmask_64
;
532 void __init
arch_init_irq(void)
538 for (i
= IRQ_INTERNAL_BASE
; i
< NR_IRQS
; ++i
)
539 irq_set_chip_and_handler(i
, &bcm63xx_internal_irq_chip
,
542 for (i
= IRQ_EXTERNAL_BASE
; i
< IRQ_EXTERNAL_BASE
+ ext_irq_count
; ++i
)
543 irq_set_chip_and_handler(i
, &bcm63xx_external_irq_chip
,
546 if (!is_ext_irq_cascaded
) {
547 for (i
= 3; i
< 3 + ext_irq_count
; ++i
)
548 setup_irq(MIPS_CPU_IRQ_BASE
+ i
, &cpu_ext_cascade_action
);
551 setup_irq(MIPS_CPU_IRQ_BASE
+ 2, &cpu_ip2_cascade_action
);
553 if (is_ext_irq_cascaded
) {
554 setup_irq(MIPS_CPU_IRQ_BASE
+ 3, &cpu_ip3_cascade_action
);
555 bcm63xx_internal_irq_chip
.irq_set_affinity
=
556 bcm63xx_internal_set_affinity
;
558 cpumask_clear(irq_default_affinity
);
559 cpumask_set_cpu(smp_processor_id(), irq_default_affinity
);