blob: 961fb7261243faf87baf47ce6ca420fc19b30d02 [file] [log] [blame]
viresh kumar4c18e772010-05-03 09:24:30 +01001/*
2 * arch/arm/plat-spear/shirq.c
3 *
4 * SPEAr platform shared irq layer source file
5 *
6 * Copyright (C) 2009 ST Microelectronics
7 * Viresh Kumar<viresh.kumar@st.com>
8 *
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
12 */
13
14#include <linux/err.h>
15#include <linux/io.h>
16#include <linux/irq.h>
17#include <linux/spinlock.h>
18#include <plat/shirq.h>
19
20struct spear_shirq *shirq;
21static DEFINE_SPINLOCK(lock);
22
Lennert Buytenhek0e60e112010-11-29 11:22:33 +010023static void shirq_irq_mask(struct irq_data *d)
viresh kumar4c18e772010-05-03 09:24:30 +010024{
Lennert Buytenhek0e60e112010-11-29 11:22:33 +010025 struct spear_shirq *shirq = irq_data_get_irq_chip_data(d);
26 u32 val, id = d->irq - shirq->dev_config[0].virq;
viresh kumar4c18e772010-05-03 09:24:30 +010027 unsigned long flags;
28
29 if ((shirq->regs.enb_reg == -1) || shirq->dev_config[id].enb_mask == -1)
30 return;
31
32 spin_lock_irqsave(&lock, flags);
33 val = readl(shirq->regs.base + shirq->regs.enb_reg);
34 if (shirq->regs.reset_to_enb)
35 val |= shirq->dev_config[id].enb_mask;
36 else
37 val &= ~(shirq->dev_config[id].enb_mask);
38 writel(val, shirq->regs.base + shirq->regs.enb_reg);
39 spin_unlock_irqrestore(&lock, flags);
40}
41
Lennert Buytenhek0e60e112010-11-29 11:22:33 +010042static void shirq_irq_unmask(struct irq_data *d)
viresh kumar4c18e772010-05-03 09:24:30 +010043{
Lennert Buytenhek0e60e112010-11-29 11:22:33 +010044 struct spear_shirq *shirq = irq_data_get_irq_chip_data(d);
45 u32 val, id = d->irq - shirq->dev_config[0].virq;
viresh kumar4c18e772010-05-03 09:24:30 +010046 unsigned long flags;
47
48 if ((shirq->regs.enb_reg == -1) || shirq->dev_config[id].enb_mask == -1)
49 return;
50
51 spin_lock_irqsave(&lock, flags);
52 val = readl(shirq->regs.base + shirq->regs.enb_reg);
53 if (shirq->regs.reset_to_enb)
54 val &= ~(shirq->dev_config[id].enb_mask);
55 else
56 val |= shirq->dev_config[id].enb_mask;
57 writel(val, shirq->regs.base + shirq->regs.enb_reg);
58 spin_unlock_irqrestore(&lock, flags);
59}
60
61static struct irq_chip shirq_chip = {
62 .name = "spear_shirq",
Lennert Buytenhek0e60e112010-11-29 11:22:33 +010063 .irq_ack = shirq_irq_mask,
64 .irq_mask = shirq_irq_mask,
65 .irq_unmask = shirq_irq_unmask,
viresh kumar4c18e772010-05-03 09:24:30 +010066};
67
68static void shirq_handler(unsigned irq, struct irq_desc *desc)
69{
70 u32 i, val, mask;
Thomas Gleixner6845664a2011-03-24 13:25:22 +010071 struct spear_shirq *shirq = irq_get_handler_data(irq);
viresh kumar4c18e772010-05-03 09:24:30 +010072
Lennert Buytenhek0e60e112010-11-29 11:22:33 +010073 desc->irq_data.chip->irq_ack(&desc->irq_data);
viresh kumar4c18e772010-05-03 09:24:30 +010074 while ((val = readl(shirq->regs.base + shirq->regs.status_reg) &
75 shirq->regs.status_reg_mask)) {
76 for (i = 0; (i < shirq->dev_count) && val; i++) {
77 if (!(shirq->dev_config[i].status_mask & val))
78 continue;
79
80 generic_handle_irq(shirq->dev_config[i].virq);
81
82 /* clear interrupt */
83 val &= ~shirq->dev_config[i].status_mask;
84 if ((shirq->regs.clear_reg == -1) ||
85 shirq->dev_config[i].clear_mask == -1)
86 continue;
87 mask = readl(shirq->regs.base + shirq->regs.clear_reg);
88 if (shirq->regs.reset_to_clear)
89 mask &= ~shirq->dev_config[i].clear_mask;
90 else
91 mask |= shirq->dev_config[i].clear_mask;
92 writel(mask, shirq->regs.base + shirq->regs.clear_reg);
93 }
94 }
Lennert Buytenhek0e60e112010-11-29 11:22:33 +010095 desc->irq_data.chip->irq_unmask(&desc->irq_data);
viresh kumar4c18e772010-05-03 09:24:30 +010096}
97
98int spear_shirq_register(struct spear_shirq *shirq)
99{
100 int i;
101
102 if (!shirq || !shirq->dev_config || !shirq->regs.base)
103 return -EFAULT;
104
105 if (!shirq->dev_count)
106 return -EINVAL;
107
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100108 irq_set_chained_handler(shirq->irq, shirq_handler);
viresh kumar4c18e772010-05-03 09:24:30 +0100109 for (i = 0; i < shirq->dev_count; i++) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100110 irq_set_chip_and_handler(shirq->dev_config[i].virq,
111 &shirq_chip, handle_simple_irq);
viresh kumar4c18e772010-05-03 09:24:30 +0100112 set_irq_flags(shirq->dev_config[i].virq, IRQF_VALID);
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100113 irq_set_chip_data(shirq->dev_config[i].virq, shirq);
viresh kumar4c18e772010-05-03 09:24:30 +0100114 }
115
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100116 irq_set_handler_data(shirq->irq, shirq);
viresh kumar4c18e772010-05-03 09:24:30 +0100117 return 0;
118}