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/module.h>
14 #include <linux/irq.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>
22 static void __dispatch_internal(void) __maybe_unused
;
23 static void __dispatch_internal_64(void) __maybe_unused
;
24 static void __internal_irq_mask_32(unsigned int irq
) __maybe_unused
;
25 static void __internal_irq_mask_64(unsigned int irq
) __maybe_unused
;
26 static void __internal_irq_unmask_32(unsigned int irq
) __maybe_unused
;
27 static void __internal_irq_unmask_64(unsigned int irq
) __maybe_unused
;
29 #ifndef BCMCPU_RUNTIME_DETECT
30 #ifdef CONFIG_BCM63XX_CPU_3368
31 #define irq_stat_reg PERF_IRQSTAT_3368_REG
32 #define irq_mask_reg PERF_IRQMASK_3368_REG
34 #define is_ext_irq_cascaded 0
35 #define ext_irq_start 0
37 #define ext_irq_count 4
38 #define ext_irq_cfg_reg1 PERF_EXTIRQ_CFG_REG_3368
39 #define ext_irq_cfg_reg2 0
41 #ifdef CONFIG_BCM63XX_CPU_6328
42 #define irq_stat_reg PERF_IRQSTAT_6328_REG
43 #define irq_mask_reg PERF_IRQMASK_6328_REG
45 #define is_ext_irq_cascaded 1
46 #define ext_irq_start (BCM_6328_EXT_IRQ0 - IRQ_INTERNAL_BASE)
47 #define ext_irq_end (BCM_6328_EXT_IRQ3 - IRQ_INTERNAL_BASE)
48 #define ext_irq_count 4
49 #define ext_irq_cfg_reg1 PERF_EXTIRQ_CFG_REG_6328
50 #define ext_irq_cfg_reg2 0
52 #ifdef CONFIG_BCM63XX_CPU_6338
53 #define irq_stat_reg PERF_IRQSTAT_6338_REG
54 #define irq_mask_reg PERF_IRQMASK_6338_REG
56 #define is_ext_irq_cascaded 0
57 #define ext_irq_start 0
59 #define ext_irq_count 4
60 #define ext_irq_cfg_reg1 PERF_EXTIRQ_CFG_REG_6338
61 #define ext_irq_cfg_reg2 0
63 #ifdef CONFIG_BCM63XX_CPU_6345
64 #define irq_stat_reg PERF_IRQSTAT_6345_REG
65 #define irq_mask_reg PERF_IRQMASK_6345_REG
67 #define is_ext_irq_cascaded 0
68 #define ext_irq_start 0
70 #define ext_irq_count 4
71 #define ext_irq_cfg_reg1 PERF_EXTIRQ_CFG_REG_6345
72 #define ext_irq_cfg_reg2 0
74 #ifdef CONFIG_BCM63XX_CPU_6348
75 #define irq_stat_reg PERF_IRQSTAT_6348_REG
76 #define irq_mask_reg PERF_IRQMASK_6348_REG
78 #define is_ext_irq_cascaded 0
79 #define ext_irq_start 0
81 #define ext_irq_count 4
82 #define ext_irq_cfg_reg1 PERF_EXTIRQ_CFG_REG_6348
83 #define ext_irq_cfg_reg2 0
85 #ifdef CONFIG_BCM63XX_CPU_6358
86 #define irq_stat_reg PERF_IRQSTAT_6358_REG
87 #define irq_mask_reg PERF_IRQMASK_6358_REG
89 #define is_ext_irq_cascaded 1
90 #define ext_irq_start (BCM_6358_EXT_IRQ0 - IRQ_INTERNAL_BASE)
91 #define ext_irq_end (BCM_6358_EXT_IRQ3 - IRQ_INTERNAL_BASE)
92 #define ext_irq_count 4
93 #define ext_irq_cfg_reg1 PERF_EXTIRQ_CFG_REG_6358
94 #define ext_irq_cfg_reg2 0
96 #ifdef CONFIG_BCM63XX_CPU_6362
97 #define irq_stat_reg PERF_IRQSTAT_6362_REG
98 #define irq_mask_reg PERF_IRQMASK_6362_REG
100 #define is_ext_irq_cascaded 1
101 #define ext_irq_start (BCM_6362_EXT_IRQ0 - IRQ_INTERNAL_BASE)
102 #define ext_irq_end (BCM_6362_EXT_IRQ3 - IRQ_INTERNAL_BASE)
103 #define ext_irq_count 4
104 #define ext_irq_cfg_reg1 PERF_EXTIRQ_CFG_REG_6362
105 #define ext_irq_cfg_reg2 0
107 #ifdef CONFIG_BCM63XX_CPU_6368
108 #define irq_stat_reg PERF_IRQSTAT_6368_REG
109 #define irq_mask_reg PERF_IRQMASK_6368_REG
111 #define is_ext_irq_cascaded 1
112 #define ext_irq_start (BCM_6368_EXT_IRQ0 - IRQ_INTERNAL_BASE)
113 #define ext_irq_end (BCM_6368_EXT_IRQ5 - IRQ_INTERNAL_BASE)
114 #define ext_irq_count 6
115 #define ext_irq_cfg_reg1 PERF_EXTIRQ_CFG_REG_6368
116 #define ext_irq_cfg_reg2 PERF_EXTIRQ_CFG_REG2_6368
120 #define dispatch_internal __dispatch_internal
121 #define internal_irq_mask __internal_irq_mask_32
122 #define internal_irq_unmask __internal_irq_unmask_32
124 #define dispatch_internal __dispatch_internal_64
125 #define internal_irq_mask __internal_irq_mask_64
126 #define internal_irq_unmask __internal_irq_unmask_64
129 #define irq_stat_addr (bcm63xx_regset_address(RSET_PERF) + irq_stat_reg)
130 #define irq_mask_addr (bcm63xx_regset_address(RSET_PERF) + irq_mask_reg)
132 static inline void bcm63xx_init_irq(void)
135 #else /* ! BCMCPU_RUNTIME_DETECT */
137 static u32 irq_stat_addr
, irq_mask_addr
;
138 static void (*dispatch_internal
)(void);
139 static int is_ext_irq_cascaded
;
140 static unsigned int ext_irq_count
;
141 static unsigned int ext_irq_start
, ext_irq_end
;
142 static unsigned int ext_irq_cfg_reg1
, ext_irq_cfg_reg2
;
143 static void (*internal_irq_mask
)(unsigned int irq
);
144 static void (*internal_irq_unmask
)(unsigned int irq
);
146 static void bcm63xx_init_irq(void)
150 irq_stat_addr
= bcm63xx_regset_address(RSET_PERF
);
151 irq_mask_addr
= bcm63xx_regset_address(RSET_PERF
);
153 switch (bcm63xx_get_cpu_id()) {
155 irq_stat_addr
+= PERF_IRQSTAT_3368_REG
;
156 irq_mask_addr
+= PERF_IRQMASK_3368_REG
;
159 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_3368
;
162 irq_stat_addr
+= PERF_IRQSTAT_6328_REG
;
163 irq_mask_addr
+= PERF_IRQMASK_6328_REG
;
166 is_ext_irq_cascaded
= 1;
167 ext_irq_start
= BCM_6328_EXT_IRQ0
- IRQ_INTERNAL_BASE
;
168 ext_irq_end
= BCM_6328_EXT_IRQ3
- IRQ_INTERNAL_BASE
;
169 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6328
;
172 irq_stat_addr
+= PERF_IRQSTAT_6338_REG
;
173 irq_mask_addr
+= PERF_IRQMASK_6338_REG
;
176 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6338
;
179 irq_stat_addr
+= PERF_IRQSTAT_6345_REG
;
180 irq_mask_addr
+= PERF_IRQMASK_6345_REG
;
183 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6345
;
186 irq_stat_addr
+= PERF_IRQSTAT_6348_REG
;
187 irq_mask_addr
+= PERF_IRQMASK_6348_REG
;
190 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6348
;
193 irq_stat_addr
+= PERF_IRQSTAT_6358_REG
;
194 irq_mask_addr
+= PERF_IRQMASK_6358_REG
;
197 is_ext_irq_cascaded
= 1;
198 ext_irq_start
= BCM_6358_EXT_IRQ0
- IRQ_INTERNAL_BASE
;
199 ext_irq_end
= BCM_6358_EXT_IRQ3
- IRQ_INTERNAL_BASE
;
200 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6358
;
203 irq_stat_addr
+= PERF_IRQSTAT_6362_REG
;
204 irq_mask_addr
+= PERF_IRQMASK_6362_REG
;
207 is_ext_irq_cascaded
= 1;
208 ext_irq_start
= BCM_6362_EXT_IRQ0
- IRQ_INTERNAL_BASE
;
209 ext_irq_end
= BCM_6362_EXT_IRQ3
- IRQ_INTERNAL_BASE
;
210 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6362
;
213 irq_stat_addr
+= PERF_IRQSTAT_6368_REG
;
214 irq_mask_addr
+= PERF_IRQMASK_6368_REG
;
217 is_ext_irq_cascaded
= 1;
218 ext_irq_start
= BCM_6368_EXT_IRQ0
- IRQ_INTERNAL_BASE
;
219 ext_irq_end
= BCM_6368_EXT_IRQ5
- IRQ_INTERNAL_BASE
;
220 ext_irq_cfg_reg1
= PERF_EXTIRQ_CFG_REG_6368
;
221 ext_irq_cfg_reg2
= PERF_EXTIRQ_CFG_REG2_6368
;
227 if (irq_bits
== 32) {
228 dispatch_internal
= __dispatch_internal
;
229 internal_irq_mask
= __internal_irq_mask_32
;
230 internal_irq_unmask
= __internal_irq_unmask_32
;
232 dispatch_internal
= __dispatch_internal_64
;
233 internal_irq_mask
= __internal_irq_mask_64
;
234 internal_irq_unmask
= __internal_irq_unmask_64
;
237 #endif /* ! BCMCPU_RUNTIME_DETECT */
239 static inline u32
get_ext_irq_perf_reg(int irq
)
242 return ext_irq_cfg_reg1
;
243 return ext_irq_cfg_reg2
;
246 static inline void handle_internal(int intbit
)
248 if (is_ext_irq_cascaded
&&
249 intbit
>= ext_irq_start
&& intbit
<= ext_irq_end
)
250 do_IRQ(intbit
- ext_irq_start
+ IRQ_EXTERNAL_BASE
);
252 do_IRQ(intbit
+ IRQ_INTERNAL_BASE
);
256 * dispatch internal devices IRQ (uart, enet, watchdog, ...). do not
257 * prioritize any interrupt relatively to another. the static counter
258 * will resume the loop where it ended the last time we left this
261 static void __dispatch_internal(void)
266 pending
= bcm_readl(irq_stat_addr
) & bcm_readl(irq_mask_addr
);
275 if (pending
& (1 << to_call
)) {
276 handle_internal(to_call
);
282 static void __dispatch_internal_64(void)
287 pending
= bcm_readq(irq_stat_addr
) & bcm_readq(irq_mask_addr
);
296 if (pending
& (1ull << to_call
)) {
297 handle_internal(to_call
);
303 asmlinkage
void plat_irq_dispatch(void)
308 cause
= read_c0_cause() & read_c0_status() & ST0_IM
;
313 if (cause
& CAUSEF_IP7
)
315 if (cause
& CAUSEF_IP0
)
317 if (cause
& CAUSEF_IP1
)
319 if (cause
& CAUSEF_IP2
)
321 if (!is_ext_irq_cascaded
) {
322 if (cause
& CAUSEF_IP3
)
324 if (cause
& CAUSEF_IP4
)
326 if (cause
& CAUSEF_IP5
)
328 if (cause
& CAUSEF_IP6
)
335 * internal IRQs operations: only mask/unmask on PERF irq mask
338 static void __internal_irq_mask_32(unsigned int irq
)
342 mask
= bcm_readl(irq_mask_addr
);
344 bcm_writel(mask
, irq_mask_addr
);
347 static void __internal_irq_mask_64(unsigned int irq
)
351 mask
= bcm_readq(irq_mask_addr
);
352 mask
&= ~(1ull << irq
);
353 bcm_writeq(mask
, irq_mask_addr
);
356 static void __internal_irq_unmask_32(unsigned int irq
)
360 mask
= bcm_readl(irq_mask_addr
);
362 bcm_writel(mask
, irq_mask_addr
);
365 static void __internal_irq_unmask_64(unsigned int irq
)
369 mask
= bcm_readq(irq_mask_addr
);
370 mask
|= (1ull << irq
);
371 bcm_writeq(mask
, irq_mask_addr
);
374 static void bcm63xx_internal_irq_mask(struct irq_data
*d
)
376 internal_irq_mask(d
->irq
- IRQ_INTERNAL_BASE
);
379 static void bcm63xx_internal_irq_unmask(struct irq_data
*d
)
381 internal_irq_unmask(d
->irq
- IRQ_INTERNAL_BASE
);
385 * external IRQs operations: mask/unmask and clear on PERF external
386 * irq control register.
388 static void bcm63xx_external_irq_mask(struct irq_data
*d
)
390 unsigned int irq
= d
->irq
- IRQ_EXTERNAL_BASE
;
393 regaddr
= get_ext_irq_perf_reg(irq
);
394 reg
= bcm_perf_readl(regaddr
);
396 if (BCMCPU_IS_6348())
397 reg
&= ~EXTIRQ_CFG_MASK_6348(irq
% 4);
399 reg
&= ~EXTIRQ_CFG_MASK(irq
% 4);
401 bcm_perf_writel(reg
, regaddr
);
402 if (is_ext_irq_cascaded
)
403 internal_irq_mask(irq
+ ext_irq_start
);
406 static void bcm63xx_external_irq_unmask(struct irq_data
*d
)
408 unsigned int irq
= d
->irq
- IRQ_EXTERNAL_BASE
;
411 regaddr
= get_ext_irq_perf_reg(irq
);
412 reg
= bcm_perf_readl(regaddr
);
414 if (BCMCPU_IS_6348())
415 reg
|= EXTIRQ_CFG_MASK_6348(irq
% 4);
417 reg
|= EXTIRQ_CFG_MASK(irq
% 4);
419 bcm_perf_writel(reg
, regaddr
);
421 if (is_ext_irq_cascaded
)
422 internal_irq_unmask(irq
+ ext_irq_start
);
425 static void bcm63xx_external_irq_clear(struct irq_data
*d
)
427 unsigned int irq
= d
->irq
- IRQ_EXTERNAL_BASE
;
430 regaddr
= get_ext_irq_perf_reg(irq
);
431 reg
= bcm_perf_readl(regaddr
);
433 if (BCMCPU_IS_6348())
434 reg
|= EXTIRQ_CFG_CLEAR_6348(irq
% 4);
436 reg
|= EXTIRQ_CFG_CLEAR(irq
% 4);
438 bcm_perf_writel(reg
, regaddr
);
441 static int bcm63xx_external_irq_set_type(struct irq_data
*d
,
442 unsigned int flow_type
)
444 unsigned int irq
= d
->irq
- IRQ_EXTERNAL_BASE
;
446 int levelsense
, sense
, bothedge
;
448 flow_type
&= IRQ_TYPE_SENSE_MASK
;
450 if (flow_type
== IRQ_TYPE_NONE
)
451 flow_type
= IRQ_TYPE_LEVEL_LOW
;
453 levelsense
= sense
= bothedge
= 0;
455 case IRQ_TYPE_EDGE_BOTH
:
459 case IRQ_TYPE_EDGE_RISING
:
463 case IRQ_TYPE_EDGE_FALLING
:
466 case IRQ_TYPE_LEVEL_HIGH
:
471 case IRQ_TYPE_LEVEL_LOW
:
476 printk(KERN_ERR
"bogus flow type combination given !\n");
480 regaddr
= get_ext_irq_perf_reg(irq
);
481 reg
= bcm_perf_readl(regaddr
);
484 switch (bcm63xx_get_cpu_id()) {
487 reg
|= EXTIRQ_CFG_LEVELSENSE_6348(irq
);
489 reg
&= ~EXTIRQ_CFG_LEVELSENSE_6348(irq
);
491 reg
|= EXTIRQ_CFG_SENSE_6348(irq
);
493 reg
&= ~EXTIRQ_CFG_SENSE_6348(irq
);
495 reg
|= EXTIRQ_CFG_BOTHEDGE_6348(irq
);
497 reg
&= ~EXTIRQ_CFG_BOTHEDGE_6348(irq
);
508 reg
|= EXTIRQ_CFG_LEVELSENSE(irq
);
510 reg
&= ~EXTIRQ_CFG_LEVELSENSE(irq
);
512 reg
|= EXTIRQ_CFG_SENSE(irq
);
514 reg
&= ~EXTIRQ_CFG_SENSE(irq
);
516 reg
|= EXTIRQ_CFG_BOTHEDGE(irq
);
518 reg
&= ~EXTIRQ_CFG_BOTHEDGE(irq
);
524 bcm_perf_writel(reg
, regaddr
);
526 irqd_set_trigger_type(d
, flow_type
);
527 if (flow_type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_LEVEL_HIGH
))
528 __irq_set_handler_locked(d
->irq
, handle_level_irq
);
530 __irq_set_handler_locked(d
->irq
, handle_edge_irq
);
532 return IRQ_SET_MASK_OK_NOCOPY
;
535 static struct irq_chip bcm63xx_internal_irq_chip
= {
536 .name
= "bcm63xx_ipic",
537 .irq_mask
= bcm63xx_internal_irq_mask
,
538 .irq_unmask
= bcm63xx_internal_irq_unmask
,
541 static struct irq_chip bcm63xx_external_irq_chip
= {
542 .name
= "bcm63xx_epic",
543 .irq_ack
= bcm63xx_external_irq_clear
,
545 .irq_mask
= bcm63xx_external_irq_mask
,
546 .irq_unmask
= bcm63xx_external_irq_unmask
,
548 .irq_set_type
= bcm63xx_external_irq_set_type
,
551 static struct irqaction cpu_ip2_cascade_action
= {
552 .handler
= no_action
,
553 .name
= "cascade_ip2",
554 .flags
= IRQF_NO_THREAD
,
557 static struct irqaction cpu_ext_cascade_action
= {
558 .handler
= no_action
,
559 .name
= "cascade_extirq",
560 .flags
= IRQF_NO_THREAD
,
563 void __init
arch_init_irq(void)
569 for (i
= IRQ_INTERNAL_BASE
; i
< NR_IRQS
; ++i
)
570 irq_set_chip_and_handler(i
, &bcm63xx_internal_irq_chip
,
573 for (i
= IRQ_EXTERNAL_BASE
; i
< IRQ_EXTERNAL_BASE
+ ext_irq_count
; ++i
)
574 irq_set_chip_and_handler(i
, &bcm63xx_external_irq_chip
,
577 if (!is_ext_irq_cascaded
) {
578 for (i
= 3; i
< 3 + ext_irq_count
; ++i
)
579 setup_irq(MIPS_CPU_IRQ_BASE
+ i
, &cpu_ext_cascade_action
);
582 setup_irq(MIPS_CPU_IRQ_BASE
+ 2, &cpu_ip2_cascade_action
);