2 * External interrupt handling for AT32AP CPUs
4 * Copyright (C) 2006 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/platform_device.h>
16 #include <linux/random.h>
20 #include <asm/arch/sm.h>
24 static void eim_ack_irq(unsigned int irq
)
26 struct at32_sm
*sm
= get_irq_chip_data(irq
);
27 sm_writel(sm
, EIM_ICR
, 1 << (irq
- sm
->eim_first_irq
));
30 static void eim_mask_irq(unsigned int irq
)
32 struct at32_sm
*sm
= get_irq_chip_data(irq
);
33 sm_writel(sm
, EIM_IDR
, 1 << (irq
- sm
->eim_first_irq
));
36 static void eim_mask_ack_irq(unsigned int irq
)
38 struct at32_sm
*sm
= get_irq_chip_data(irq
);
39 sm_writel(sm
, EIM_ICR
, 1 << (irq
- sm
->eim_first_irq
));
40 sm_writel(sm
, EIM_IDR
, 1 << (irq
- sm
->eim_first_irq
));
43 static void eim_unmask_irq(unsigned int irq
)
45 struct at32_sm
*sm
= get_irq_chip_data(irq
);
46 sm_writel(sm
, EIM_IER
, 1 << (irq
- sm
->eim_first_irq
));
49 static int eim_set_irq_type(unsigned int irq
, unsigned int flow_type
)
51 struct at32_sm
*sm
= get_irq_chip_data(irq
);
52 unsigned int i
= irq
- sm
->eim_first_irq
;
53 u32 mode
, edge
, level
;
57 flow_type
&= IRQ_TYPE_SENSE_MASK
;
59 spin_lock_irqsave(&sm
->lock
, flags
);
61 mode
= sm_readl(sm
, EIM_MODE
);
62 edge
= sm_readl(sm
, EIM_EDGE
);
63 level
= sm_readl(sm
, EIM_LEVEL
);
66 case IRQ_TYPE_LEVEL_LOW
:
70 case IRQ_TYPE_LEVEL_HIGH
:
74 case IRQ_TYPE_EDGE_RISING
:
78 case IRQ_TYPE_EDGE_FALLING
:
87 sm_writel(sm
, EIM_MODE
, mode
);
88 sm_writel(sm
, EIM_EDGE
, edge
);
89 sm_writel(sm
, EIM_LEVEL
, level
);
91 spin_unlock_irqrestore(&sm
->lock
, flags
);
96 struct irq_chip eim_chip
= {
100 .mask_ack
= eim_mask_ack_irq
,
101 .unmask
= eim_unmask_irq
,
102 .set_type
= eim_set_irq_type
,
105 static void demux_eim_irq(unsigned int irq
, struct irq_desc
*desc
)
107 struct at32_sm
*sm
= desc
->handler_data
;
108 struct irq_desc
*ext_desc
;
109 unsigned long status
, pending
;
110 unsigned int i
, ext_irq
;
112 spin_lock(&sm
->lock
);
114 status
= sm_readl(sm
, EIM_ISR
);
115 pending
= status
& sm_readl(sm
, EIM_IMR
);
118 i
= fls(pending
) - 1;
119 pending
&= ~(1 << i
);
121 ext_irq
= i
+ sm
->eim_first_irq
;
122 ext_desc
= irq_desc
+ ext_irq
;
123 ext_desc
->handle_irq(ext_irq
, ext_desc
);
126 spin_unlock(&sm
->lock
);
129 static int __init
eim_init(void)
131 struct at32_sm
*sm
= &system_manager
;
133 unsigned int nr_irqs
;
134 unsigned int int_irq
;
138 * The EIM is really the same module as SM, so register
139 * mapping, etc. has been taken care of already.
143 * Find out how many interrupt lines that are actually
144 * implemented in hardware.
146 sm_writel(sm
, EIM_IDR
, ~0UL);
147 sm_writel(sm
, EIM_MODE
, ~0UL);
148 pattern
= sm_readl(sm
, EIM_MODE
);
149 nr_irqs
= fls(pattern
);
151 sm
->eim_chip
= &eim_chip
;
153 for (i
= 0; i
< nr_irqs
; i
++) {
154 set_irq_chip(sm
->eim_first_irq
+ i
, &eim_chip
);
155 set_irq_chip_data(sm
->eim_first_irq
+ i
, sm
);
158 int_irq
= platform_get_irq_byname(sm
->pdev
, "eim");
160 set_irq_chained_handler(int_irq
, demux_eim_irq
);
161 set_irq_data(int_irq
, sm
);
163 printk("EIM: External Interrupt Module at 0x%p, IRQ %u\n",
165 printk("EIM: Handling %u external IRQs, starting with IRQ %u\n",
166 nr_irqs
, sm
->eim_first_irq
);
170 arch_initcall(eim_init
);