Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * External interrupt handling for AT32AP CPUs |
| 3 | * |
| 4 | * Copyright (C) 2006 Atmel Corporation |
| 5 | * |
| 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. |
| 9 | */ |
| 10 | |
| 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 18 | |
| 19 | #include <asm/io.h> |
| 20 | |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 21 | /* EIC register offsets */ |
| 22 | #define EIC_IER 0x0000 |
| 23 | #define EIC_IDR 0x0004 |
| 24 | #define EIC_IMR 0x0008 |
| 25 | #define EIC_ISR 0x000c |
| 26 | #define EIC_ICR 0x0010 |
| 27 | #define EIC_MODE 0x0014 |
| 28 | #define EIC_EDGE 0x0018 |
| 29 | #define EIC_LEVEL 0x001c |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 30 | #define EIC_NMIC 0x0024 |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 31 | |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 32 | /* Bitfields in NMIC */ |
Haavard Skinnemoen | e7ba176 | 2007-10-10 14:58:29 +0200 | [diff] [blame] | 33 | #define EIC_NMIC_ENABLE (1 << 0) |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 34 | |
| 35 | /* Bit manipulation macros */ |
| 36 | #define EIC_BIT(name) \ |
| 37 | (1 << EIC_##name##_OFFSET) |
| 38 | #define EIC_BF(name,value) \ |
| 39 | (((value) & ((1 << EIC_##name##_SIZE) - 1)) \ |
| 40 | << EIC_##name##_OFFSET) |
| 41 | #define EIC_BFEXT(name,value) \ |
| 42 | (((value) >> EIC_##name##_OFFSET) \ |
| 43 | & ((1 << EIC_##name##_SIZE) - 1)) |
| 44 | #define EIC_BFINS(name,value,old) \ |
| 45 | (((old) & ~(((1 << EIC_##name##_SIZE) - 1) \ |
| 46 | << EIC_##name##_OFFSET)) \ |
| 47 | | EIC_BF(name,value)) |
| 48 | |
| 49 | /* Register access macros */ |
| 50 | #define eic_readl(port,reg) \ |
| 51 | __raw_readl((port)->regs + EIC_##reg) |
| 52 | #define eic_writel(port,reg,value) \ |
| 53 | __raw_writel((value), (port)->regs + EIC_##reg) |
| 54 | |
| 55 | struct eic { |
| 56 | void __iomem *regs; |
| 57 | struct irq_chip *chip; |
| 58 | unsigned int first_irq; |
| 59 | }; |
| 60 | |
Haavard Skinnemoen | e7ba176 | 2007-10-10 14:58:29 +0200 | [diff] [blame] | 61 | static struct eic *nmi_eic; |
| 62 | static bool nmi_enabled; |
| 63 | |
Wanlong Gao | 8faf9e3 | 2011-04-10 14:14:43 +0800 | [diff] [blame] | 64 | static void eic_ack_irq(struct irq_data *d) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 65 | { |
Wanlong Gao | 8faf9e3 | 2011-04-10 14:14:43 +0800 | [diff] [blame] | 66 | struct eic *eic = irq_data_get_irq_chip_data(d); |
Thomas Gleixner | 7776e23 | 2011-02-06 17:29:01 +0100 | [diff] [blame] | 67 | eic_writel(eic, ICR, 1 << (d->irq - eic->first_irq)); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Wanlong Gao | 8faf9e3 | 2011-04-10 14:14:43 +0800 | [diff] [blame] | 70 | static void eic_mask_irq(struct irq_data *d) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 71 | { |
Wanlong Gao | 8faf9e3 | 2011-04-10 14:14:43 +0800 | [diff] [blame] | 72 | struct eic *eic = irq_data_get_irq_chip_data(d); |
Thomas Gleixner | 7776e23 | 2011-02-06 17:29:01 +0100 | [diff] [blame] | 73 | eic_writel(eic, IDR, 1 << (d->irq - eic->first_irq)); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Wanlong Gao | 8faf9e3 | 2011-04-10 14:14:43 +0800 | [diff] [blame] | 76 | static void eic_mask_ack_irq(struct irq_data *d) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 77 | { |
Wanlong Gao | 8faf9e3 | 2011-04-10 14:14:43 +0800 | [diff] [blame] | 78 | struct eic *eic = irq_data_get_irq_chip_data(d); |
Thomas Gleixner | 7776e23 | 2011-02-06 17:29:01 +0100 | [diff] [blame] | 79 | eic_writel(eic, ICR, 1 << (d->irq - eic->first_irq)); |
| 80 | eic_writel(eic, IDR, 1 << (d->irq - eic->first_irq)); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Wanlong Gao | 8faf9e3 | 2011-04-10 14:14:43 +0800 | [diff] [blame] | 83 | static void eic_unmask_irq(struct irq_data *d) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 84 | { |
Wanlong Gao | 8faf9e3 | 2011-04-10 14:14:43 +0800 | [diff] [blame] | 85 | struct eic *eic = irq_data_get_irq_chip_data(d); |
Thomas Gleixner | 7776e23 | 2011-02-06 17:29:01 +0100 | [diff] [blame] | 86 | eic_writel(eic, IER, 1 << (d->irq - eic->first_irq)); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Wanlong Gao | 8faf9e3 | 2011-04-10 14:14:43 +0800 | [diff] [blame] | 89 | static int eic_set_irq_type(struct irq_data *d, unsigned int flow_type) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 90 | { |
Wanlong Gao | 8faf9e3 | 2011-04-10 14:14:43 +0800 | [diff] [blame] | 91 | struct eic *eic = irq_data_get_irq_chip_data(d); |
Thomas Gleixner | 7776e23 | 2011-02-06 17:29:01 +0100 | [diff] [blame] | 92 | unsigned int irq = d->irq; |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 93 | unsigned int i = irq - eic->first_irq; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 94 | u32 mode, edge, level; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 95 | |
David Brownell | 58febc0 | 2007-01-23 20:21:36 -0800 | [diff] [blame] | 96 | flow_type &= IRQ_TYPE_SENSE_MASK; |
Haavard Skinnemoen | 01cb087 | 2006-12-04 12:00:03 +0100 | [diff] [blame] | 97 | if (flow_type == IRQ_TYPE_NONE) |
| 98 | flow_type = IRQ_TYPE_LEVEL_LOW; |
| 99 | |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 100 | mode = eic_readl(eic, MODE); |
| 101 | edge = eic_readl(eic, EDGE); |
| 102 | level = eic_readl(eic, LEVEL); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 103 | |
| 104 | switch (flow_type) { |
| 105 | case IRQ_TYPE_LEVEL_LOW: |
| 106 | mode |= 1 << i; |
| 107 | level &= ~(1 << i); |
| 108 | break; |
| 109 | case IRQ_TYPE_LEVEL_HIGH: |
| 110 | mode |= 1 << i; |
| 111 | level |= 1 << i; |
| 112 | break; |
| 113 | case IRQ_TYPE_EDGE_RISING: |
| 114 | mode &= ~(1 << i); |
| 115 | edge |= 1 << i; |
| 116 | break; |
| 117 | case IRQ_TYPE_EDGE_FALLING: |
| 118 | mode &= ~(1 << i); |
| 119 | edge &= ~(1 << i); |
| 120 | break; |
| 121 | default: |
Thomas Gleixner | 62ec05d1 | 2011-03-24 17:24:04 +0100 | [diff] [blame] | 122 | return -EINVAL; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Thomas Gleixner | 62ec05d1 | 2011-03-24 17:24:04 +0100 | [diff] [blame] | 125 | eic_writel(eic, MODE, mode); |
| 126 | eic_writel(eic, EDGE, edge); |
| 127 | eic_writel(eic, LEVEL, level); |
David Brownell | 58febc0 | 2007-01-23 20:21:36 -0800 | [diff] [blame] | 128 | |
Thomas Gleixner | 62ec05d1 | 2011-03-24 17:24:04 +0100 | [diff] [blame] | 129 | irqd_set_trigger_type(d, flow_type); |
| 130 | if (flow_type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH)) |
| 131 | __irq_set_handler_locked(irq, handle_level_irq); |
| 132 | else |
| 133 | __irq_set_handler_locked(irq, handle_edge_irq); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 134 | |
Thomas Gleixner | 62ec05d1 | 2011-03-24 17:24:04 +0100 | [diff] [blame] | 135 | return IRQ_SET_MASK_OK_NOCOPY; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Haavard Skinnemoen | 8629896 | 2007-10-22 15:51:04 +0200 | [diff] [blame] | 138 | static struct irq_chip eic_chip = { |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 139 | .name = "eic", |
Thomas Gleixner | 7776e23 | 2011-02-06 17:29:01 +0100 | [diff] [blame] | 140 | .irq_ack = eic_ack_irq, |
| 141 | .irq_mask = eic_mask_irq, |
| 142 | .irq_mask_ack = eic_mask_ack_irq, |
| 143 | .irq_unmask = eic_unmask_irq, |
| 144 | .irq_set_type = eic_set_irq_type, |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 147 | static void demux_eic_irq(unsigned int irq, struct irq_desc *desc) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 148 | { |
Thomas Gleixner | d75f1bf | 2011-03-24 16:39:32 +0100 | [diff] [blame] | 149 | struct eic *eic = irq_desc_get_handler_data(desc); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 150 | unsigned long status, pending; |
David Brownell | e4f586f | 2007-12-18 20:50:28 -0800 | [diff] [blame] | 151 | unsigned int i; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 152 | |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 153 | status = eic_readl(eic, ISR); |
| 154 | pending = status & eic_readl(eic, IMR); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 155 | |
| 156 | while (pending) { |
| 157 | i = fls(pending) - 1; |
| 158 | pending &= ~(1 << i); |
| 159 | |
David Brownell | e4f586f | 2007-12-18 20:50:28 -0800 | [diff] [blame] | 160 | generic_handle_irq(i + eic->first_irq); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 161 | } |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Haavard Skinnemoen | e7ba176 | 2007-10-10 14:58:29 +0200 | [diff] [blame] | 164 | int nmi_enable(void) |
| 165 | { |
| 166 | nmi_enabled = true; |
| 167 | |
| 168 | if (nmi_eic) |
| 169 | eic_writel(nmi_eic, NMIC, EIC_NMIC_ENABLE); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | void nmi_disable(void) |
| 175 | { |
| 176 | if (nmi_eic) |
| 177 | eic_writel(nmi_eic, NMIC, 0); |
| 178 | |
| 179 | nmi_enabled = false; |
| 180 | } |
| 181 | |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 182 | static int __init eic_probe(struct platform_device *pdev) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 183 | { |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 184 | struct eic *eic; |
| 185 | struct resource *regs; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 186 | unsigned int i; |
Thomas Gleixner | 4b1135a | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 187 | unsigned int nr_of_irqs; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 188 | unsigned int int_irq; |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 189 | int ret; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 190 | u32 pattern; |
| 191 | |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 192 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 193 | int_irq = platform_get_irq(pdev, 0); |
Uwe Kleine-König | c7d8763 | 2011-02-09 11:28:04 +0100 | [diff] [blame] | 194 | if (!regs || (int)int_irq <= 0) { |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 195 | dev_dbg(&pdev->dev, "missing regs and/or irq resource\n"); |
| 196 | return -ENXIO; |
| 197 | } |
| 198 | |
| 199 | ret = -ENOMEM; |
| 200 | eic = kzalloc(sizeof(struct eic), GFP_KERNEL); |
| 201 | if (!eic) { |
| 202 | dev_dbg(&pdev->dev, "no memory for eic structure\n"); |
| 203 | goto err_kzalloc; |
| 204 | } |
| 205 | |
| 206 | eic->first_irq = EIM_IRQ_BASE + 32 * pdev->id; |
| 207 | eic->regs = ioremap(regs->start, regs->end - regs->start + 1); |
| 208 | if (!eic->regs) { |
| 209 | dev_dbg(&pdev->dev, "failed to map regs\n"); |
| 210 | goto err_ioremap; |
| 211 | } |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * Find out how many interrupt lines that are actually |
| 215 | * implemented in hardware. |
| 216 | */ |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 217 | eic_writel(eic, IDR, ~0UL); |
| 218 | eic_writel(eic, MODE, ~0UL); |
| 219 | pattern = eic_readl(eic, MODE); |
Thomas Gleixner | 4b1135a | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 220 | nr_of_irqs = fls(pattern); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 221 | |
Haavard Skinnemoen | d6c49a7 | 2008-01-24 16:56:53 +0100 | [diff] [blame] | 222 | /* Trigger on low level unless overridden by driver */ |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 223 | eic_writel(eic, EDGE, 0UL); |
Haavard Skinnemoen | d6c49a7 | 2008-01-24 16:56:53 +0100 | [diff] [blame] | 224 | eic_writel(eic, LEVEL, 0UL); |
Haavard Skinnemoen | 01cb087 | 2006-12-04 12:00:03 +0100 | [diff] [blame] | 225 | |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 226 | eic->chip = &eic_chip; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 227 | |
Thomas Gleixner | 4b1135a | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 228 | for (i = 0; i < nr_of_irqs; i++) { |
Thomas Gleixner | d75f1bf | 2011-03-24 16:39:32 +0100 | [diff] [blame] | 229 | irq_set_chip_and_handler(eic->first_irq + i, &eic_chip, |
Haavard Skinnemoen | d6c49a7 | 2008-01-24 16:56:53 +0100 | [diff] [blame] | 230 | handle_level_irq); |
Thomas Gleixner | d75f1bf | 2011-03-24 16:39:32 +0100 | [diff] [blame] | 231 | irq_set_chip_data(eic->first_irq + i, eic); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Thomas Gleixner | d75f1bf | 2011-03-24 16:39:32 +0100 | [diff] [blame] | 234 | irq_set_chained_handler(int_irq, demux_eic_irq); |
| 235 | irq_set_handler_data(int_irq, eic); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 236 | |
Haavard Skinnemoen | e7ba176 | 2007-10-10 14:58:29 +0200 | [diff] [blame] | 237 | if (pdev->id == 0) { |
| 238 | nmi_eic = eic; |
| 239 | if (nmi_enabled) |
| 240 | /* |
| 241 | * Someone tried to enable NMI before we were |
| 242 | * ready. Do it now. |
| 243 | */ |
| 244 | nmi_enable(); |
| 245 | } |
| 246 | |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 247 | dev_info(&pdev->dev, |
| 248 | "External Interrupt Controller at 0x%p, IRQ %u\n", |
| 249 | eic->regs, int_irq); |
| 250 | dev_info(&pdev->dev, |
| 251 | "Handling %u external IRQs, starting with IRQ %u\n", |
Thomas Gleixner | 4b1135a | 2008-10-16 15:33:18 +0200 | [diff] [blame] | 252 | nr_of_irqs, eic->first_irq); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 253 | |
| 254 | return 0; |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 255 | |
| 256 | err_ioremap: |
| 257 | kfree(eic); |
| 258 | err_kzalloc: |
| 259 | return ret; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 260 | } |
Haavard Skinnemoen | 7a5b805 | 2007-06-04 12:58:30 +0200 | [diff] [blame] | 261 | |
| 262 | static struct platform_driver eic_driver = { |
| 263 | .driver = { |
| 264 | .name = "at32_eic", |
| 265 | }, |
| 266 | }; |
| 267 | |
| 268 | static int __init eic_init(void) |
| 269 | { |
| 270 | return platform_driver_probe(&eic_driver, eic_probe); |
| 271 | } |
| 272 | arch_initcall(eic_init); |