Linux 2.6.31.6
[linux/fpc-iii.git] / arch / avr32 / mach-at32ap / intc.c
blob994c4545e2b7ecac3bc50b95db9e871aebf7d8e4
1 /*
2 * Copyright (C) 2006, 2008 Atmel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <linux/clk.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/platform_device.h>
15 #include <linux/sysdev.h>
17 #include <asm/io.h>
19 #include "intc.h"
21 struct intc {
22 void __iomem *regs;
23 struct irq_chip chip;
24 struct sys_device sysdev;
25 #ifdef CONFIG_PM
26 unsigned long suspend_ipr;
27 unsigned long saved_ipr[64];
28 #endif
31 extern struct platform_device at32_intc0_device;
34 * TODO: We may be able to implement mask/unmask by setting IxM flags
35 * in the status register.
37 static void intc_mask_irq(unsigned int irq)
42 static void intc_unmask_irq(unsigned int irq)
47 static struct intc intc0 = {
48 .chip = {
49 .name = "intc",
50 .mask = intc_mask_irq,
51 .unmask = intc_unmask_irq,
56 * All interrupts go via intc at some point.
58 asmlinkage void do_IRQ(int level, struct pt_regs *regs)
60 struct irq_desc *desc;
61 struct pt_regs *old_regs;
62 unsigned int irq;
63 unsigned long status_reg;
65 local_irq_disable();
67 old_regs = set_irq_regs(regs);
69 irq_enter();
71 irq = intc_readl(&intc0, INTCAUSE0 - 4 * level);
72 desc = irq_desc + irq;
73 desc->handle_irq(irq, desc);
76 * Clear all interrupt level masks so that we may handle
77 * interrupts during softirq processing. If this is a nested
78 * interrupt, interrupts must stay globally disabled until we
79 * return.
81 status_reg = sysreg_read(SR);
82 status_reg &= ~(SYSREG_BIT(I0M) | SYSREG_BIT(I1M)
83 | SYSREG_BIT(I2M) | SYSREG_BIT(I3M));
84 sysreg_write(SR, status_reg);
86 irq_exit();
88 set_irq_regs(old_regs);
91 void __init init_IRQ(void)
93 extern void _evba(void);
94 extern void irq_level0(void);
95 struct resource *regs;
96 struct clk *pclk;
97 unsigned int i;
98 u32 offset, readback;
100 regs = platform_get_resource(&at32_intc0_device, IORESOURCE_MEM, 0);
101 if (!regs) {
102 printk(KERN_EMERG "intc: no mmio resource defined\n");
103 goto fail;
105 pclk = clk_get(&at32_intc0_device.dev, "pclk");
106 if (IS_ERR(pclk)) {
107 printk(KERN_EMERG "intc: no clock defined\n");
108 goto fail;
111 clk_enable(pclk);
113 intc0.regs = ioremap(regs->start, regs->end - regs->start + 1);
114 if (!intc0.regs) {
115 printk(KERN_EMERG "intc: failed to map registers (0x%08lx)\n",
116 (unsigned long)regs->start);
117 goto fail;
121 * Initialize all interrupts to level 0 (lowest priority). The
122 * priority level may be changed by calling
123 * irq_set_priority().
126 offset = (unsigned long)&irq_level0 - (unsigned long)&_evba;
127 for (i = 0; i < NR_INTERNAL_IRQS; i++) {
128 intc_writel(&intc0, INTPR0 + 4 * i, offset);
129 readback = intc_readl(&intc0, INTPR0 + 4 * i);
130 if (readback == offset)
131 set_irq_chip_and_handler(i, &intc0.chip,
132 handle_simple_irq);
135 /* Unmask all interrupt levels */
136 sysreg_write(SR, (sysreg_read(SR)
137 & ~(SR_I3M | SR_I2M | SR_I1M | SR_I0M)));
139 return;
141 fail:
142 panic("Interrupt controller initialization failed!\n");
145 #ifdef CONFIG_PM
146 void intc_set_suspend_handler(unsigned long offset)
148 intc0.suspend_ipr = offset;
151 static int intc_suspend(struct sys_device *sdev, pm_message_t state)
153 struct intc *intc = container_of(sdev, struct intc, sysdev);
154 int i;
156 if (unlikely(!irqs_disabled())) {
157 pr_err("intc_suspend: called with interrupts enabled\n");
158 return -EINVAL;
161 if (unlikely(!intc->suspend_ipr)) {
162 pr_err("intc_suspend: suspend_ipr not initialized\n");
163 return -EINVAL;
166 for (i = 0; i < 64; i++) {
167 intc->saved_ipr[i] = intc_readl(intc, INTPR0 + 4 * i);
168 intc_writel(intc, INTPR0 + 4 * i, intc->suspend_ipr);
171 return 0;
174 static int intc_resume(struct sys_device *sdev)
176 struct intc *intc = container_of(sdev, struct intc, sysdev);
177 int i;
179 WARN_ON(!irqs_disabled());
181 for (i = 0; i < 64; i++)
182 intc_writel(intc, INTPR0 + 4 * i, intc->saved_ipr[i]);
184 return 0;
186 #else
187 #define intc_suspend NULL
188 #define intc_resume NULL
189 #endif
191 static struct sysdev_class intc_class = {
192 .name = "intc",
193 .suspend = intc_suspend,
194 .resume = intc_resume,
197 static int __init intc_init_sysdev(void)
199 int ret;
201 ret = sysdev_class_register(&intc_class);
202 if (ret)
203 return ret;
205 intc0.sysdev.id = 0;
206 intc0.sysdev.cls = &intc_class;
207 ret = sysdev_register(&intc0.sysdev);
209 return ret;
211 device_initcall(intc_init_sysdev);
213 unsigned long intc_get_pending(unsigned int group)
215 return intc_readl(&intc0, INTREQ0 + 4 * group);
217 EXPORT_SYMBOL_GPL(intc_get_pending);