nvmet-fc: hold reference on hostport match
[linux-2.6-block.git] / drivers / irqchip / spear-shirq.c
CommitLineData
4c18e77f 1/*
4c18e77f 2 * SPEAr platform shared irq layer source file
3 *
df1590d9 4 * Copyright (C) 2009-2012 ST Microelectronics
da89947b 5 * Viresh Kumar <vireshk@kernel.org>
4c18e77f 6 *
df1590d9 7 * Copyright (C) 2012 ST Microelectronics
9cc23682 8 * Shiraz Hashim <shiraz.linux.kernel@gmail.com>
df1590d9 9 *
4c18e77f 10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
13 */
80515a5a 14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4c18e77f 15
16#include <linux/err.h>
80515a5a
SH
17#include <linux/export.h>
18#include <linux/interrupt.h>
4c18e77f 19#include <linux/io.h>
20#include <linux/irq.h>
41a83e06 21#include <linux/irqchip.h>
80515a5a
SH
22#include <linux/irqdomain.h>
23#include <linux/of.h>
24#include <linux/of_address.h>
25#include <linux/of_irq.h>
4c18e77f 26#include <linux/spinlock.h>
4c18e77f 27
078bc005
TG
28/*
29 * struct spear_shirq: shared irq structure
30 *
c5d1d857 31 * base: Base register address
1b0a76c1
TG
32 * status_reg: Status register offset for chained interrupt handler
33 * mask_reg: Mask register offset for irq chip
4ecc832f 34 * mask: Mask to apply to the status register
c5d1d857
TG
35 * virq_base: Base virtual interrupt number
36 * nr_irqs: Number of interrupts handled by this block
37 * offset: Bit offset of the first interrupt
f07e42f9
TG
38 * irq_chip: Interrupt controller chip used for this instance,
39 * if NULL group is disabled, but accounted
078bc005
TG
40 */
41struct spear_shirq {
c5d1d857 42 void __iomem *base;
1b0a76c1
TG
43 u32 status_reg;
44 u32 mask_reg;
4ecc832f 45 u32 mask;
c5d1d857
TG
46 u32 virq_base;
47 u32 nr_irqs;
48 u32 offset;
f07e42f9 49 struct irq_chip *irq_chip;
078bc005
TG
50};
51
80515a5a
SH
52/* spear300 shared irq registers offsets and masks */
53#define SPEAR300_INT_ENB_MASK_REG 0x54
54#define SPEAR300_INT_STS_MASK_REG 0x58
55
f07e42f9
TG
56static DEFINE_RAW_SPINLOCK(shirq_lock);
57
58static void shirq_irq_mask(struct irq_data *d)
59{
60 struct spear_shirq *shirq = irq_data_get_irq_chip_data(d);
61 u32 val, shift = d->irq - shirq->virq_base + shirq->offset;
1b0a76c1 62 u32 __iomem *reg = shirq->base + shirq->mask_reg;
f07e42f9
TG
63
64 raw_spin_lock(&shirq_lock);
65 val = readl(reg) & ~(0x1 << shift);
66 writel(val, reg);
67 raw_spin_unlock(&shirq_lock);
68}
69
70static void shirq_irq_unmask(struct irq_data *d)
71{
72 struct spear_shirq *shirq = irq_data_get_irq_chip_data(d);
73 u32 val, shift = d->irq - shirq->virq_base + shirq->offset;
1b0a76c1 74 u32 __iomem *reg = shirq->base + shirq->mask_reg;
f07e42f9
TG
75
76 raw_spin_lock(&shirq_lock);
77 val = readl(reg) | (0x1 << shift);
78 writel(val, reg);
79 raw_spin_unlock(&shirq_lock);
80}
81
82static struct irq_chip shirq_chip = {
83 .name = "spear-shirq",
84 .irq_mask = shirq_irq_mask,
85 .irq_unmask = shirq_irq_unmask,
86};
87
80515a5a 88static struct spear_shirq spear300_shirq_ras1 = {
c5d1d857
TG
89 .offset = 0,
90 .nr_irqs = 9,
4ecc832f 91 .mask = ((0x1 << 9) - 1) << 0,
f07e42f9 92 .irq_chip = &shirq_chip,
1b0a76c1
TG
93 .status_reg = SPEAR300_INT_STS_MASK_REG,
94 .mask_reg = SPEAR300_INT_ENB_MASK_REG,
80515a5a
SH
95};
96
97static struct spear_shirq *spear300_shirq_blocks[] = {
98 &spear300_shirq_ras1,
99};
100
101/* spear310 shared irq registers offsets and masks */
102#define SPEAR310_INT_STS_MASK_REG 0x04
103
104static struct spear_shirq spear310_shirq_ras1 = {
c5d1d857
TG
105 .offset = 0,
106 .nr_irqs = 8,
4ecc832f 107 .mask = ((0x1 << 8) - 1) << 0,
f07e42f9 108 .irq_chip = &dummy_irq_chip,
1b0a76c1 109 .status_reg = SPEAR310_INT_STS_MASK_REG,
80515a5a
SH
110};
111
112static struct spear_shirq spear310_shirq_ras2 = {
c5d1d857
TG
113 .offset = 8,
114 .nr_irqs = 5,
4ecc832f 115 .mask = ((0x1 << 5) - 1) << 8,
f07e42f9 116 .irq_chip = &dummy_irq_chip,
1b0a76c1 117 .status_reg = SPEAR310_INT_STS_MASK_REG,
80515a5a
SH
118};
119
120static struct spear_shirq spear310_shirq_ras3 = {
c5d1d857
TG
121 .offset = 13,
122 .nr_irqs = 1,
4ecc832f 123 .mask = ((0x1 << 1) - 1) << 13,
f07e42f9 124 .irq_chip = &dummy_irq_chip,
1b0a76c1 125 .status_reg = SPEAR310_INT_STS_MASK_REG,
80515a5a
SH
126};
127
128static struct spear_shirq spear310_shirq_intrcomm_ras = {
c5d1d857
TG
129 .offset = 14,
130 .nr_irqs = 3,
4ecc832f 131 .mask = ((0x1 << 3) - 1) << 14,
f07e42f9 132 .irq_chip = &dummy_irq_chip,
1b0a76c1 133 .status_reg = SPEAR310_INT_STS_MASK_REG,
80515a5a
SH
134};
135
136static struct spear_shirq *spear310_shirq_blocks[] = {
137 &spear310_shirq_ras1,
138 &spear310_shirq_ras2,
139 &spear310_shirq_ras3,
140 &spear310_shirq_intrcomm_ras,
141};
142
143/* spear320 shared irq registers offsets and masks */
144#define SPEAR320_INT_STS_MASK_REG 0x04
145#define SPEAR320_INT_CLR_MASK_REG 0x04
146#define SPEAR320_INT_ENB_MASK_REG 0x08
147
03319a1a
TG
148static struct spear_shirq spear320_shirq_ras3 = {
149 .offset = 0,
150 .nr_irqs = 7,
4ecc832f 151 .mask = ((0x1 << 7) - 1) << 0,
f63c8625
HC
152 .irq_chip = &dummy_irq_chip,
153 .status_reg = SPEAR320_INT_STS_MASK_REG,
80515a5a
SH
154};
155
03319a1a
TG
156static struct spear_shirq spear320_shirq_ras1 = {
157 .offset = 7,
158 .nr_irqs = 3,
4ecc832f 159 .mask = ((0x1 << 3) - 1) << 7,
f07e42f9 160 .irq_chip = &dummy_irq_chip,
1b0a76c1 161 .status_reg = SPEAR320_INT_STS_MASK_REG,
80515a5a
SH
162};
163
03319a1a
TG
164static struct spear_shirq spear320_shirq_ras2 = {
165 .offset = 10,
166 .nr_irqs = 1,
4ecc832f 167 .mask = ((0x1 << 1) - 1) << 10,
f07e42f9 168 .irq_chip = &dummy_irq_chip,
1b0a76c1 169 .status_reg = SPEAR320_INT_STS_MASK_REG,
80515a5a
SH
170};
171
172static struct spear_shirq spear320_shirq_intrcomm_ras = {
c5d1d857
TG
173 .offset = 11,
174 .nr_irqs = 11,
4ecc832f 175 .mask = ((0x1 << 11) - 1) << 11,
f07e42f9 176 .irq_chip = &dummy_irq_chip,
1b0a76c1 177 .status_reg = SPEAR320_INT_STS_MASK_REG,
80515a5a
SH
178};
179
180static struct spear_shirq *spear320_shirq_blocks[] = {
181 &spear320_shirq_ras3,
182 &spear320_shirq_ras1,
183 &spear320_shirq_ras2,
184 &spear320_shirq_intrcomm_ras,
185};
186
bd0b9ac4 187static void shirq_handler(struct irq_desc *desc)
4c18e77f 188{
5b29264c 189 struct spear_shirq *shirq = irq_desc_get_handler_data(desc);
25dc49e3 190 u32 pend;
4c18e77f 191
1b0a76c1 192 pend = readl(shirq->base + shirq->status_reg) & shirq->mask;
25dc49e3 193 pend >>= shirq->offset;
80515a5a 194
25dc49e3
TG
195 while (pend) {
196 int irq = __ffs(pend);
80515a5a 197
25dc49e3
TG
198 pend &= ~(0x1 << irq);
199 generic_handle_irq(shirq->virq_base + irq);
4c18e77f 200 }
4c18e77f 201}
202
f37ecbce
TG
203static void __init spear_shirq_register(struct spear_shirq *shirq,
204 int parent_irq)
4c18e77f 205{
206 int i;
207
f07e42f9 208 if (!shirq->irq_chip)
80515a5a 209 return;
4c18e77f 210
2aedd0fd 211 irq_set_chained_handler_and_data(parent_irq, shirq_handler, shirq);
f37ecbce 212
c5d1d857
TG
213 for (i = 0; i < shirq->nr_irqs; i++) {
214 irq_set_chip_and_handler(shirq->virq_base + i,
f07e42f9 215 shirq->irq_chip, handle_simple_irq);
c5d1d857 216 irq_set_chip_data(shirq->virq_base + i, shirq);
4c18e77f 217 }
80515a5a
SH
218}
219
220static int __init shirq_init(struct spear_shirq **shirq_blocks, int block_nr,
221 struct device_node *np)
222{
c5d1d857 223 int i, parent_irq, virq_base, hwirq = 0, nr_irqs = 0;
a26c06f9 224 struct irq_domain *shirq_domain;
80515a5a
SH
225 void __iomem *base;
226
227 base = of_iomap(np, 0);
228 if (!base) {
229 pr_err("%s: failed to map shirq registers\n", __func__);
230 return -ENXIO;
231 }
232
233 for (i = 0; i < block_nr; i++)
c5d1d857 234 nr_irqs += shirq_blocks[i]->nr_irqs;
80515a5a 235
c5d1d857 236 virq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
287980e4 237 if (virq_base < 0) {
80515a5a
SH
238 pr_err("%s: irq desc alloc failed\n", __func__);
239 goto err_unmap;
240 }
241
c5d1d857 242 shirq_domain = irq_domain_add_legacy(np, nr_irqs, virq_base, 0,
80515a5a
SH
243 &irq_domain_simple_ops, NULL);
244 if (WARN_ON(!shirq_domain)) {
245 pr_warn("%s: irq domain init failed\n", __func__);
246 goto err_free_desc;
247 }
248
249 for (i = 0; i < block_nr; i++) {
250 shirq_blocks[i]->base = base;
c5d1d857 251 shirq_blocks[i]->virq_base = irq_find_mapping(shirq_domain,
80515a5a 252 hwirq);
80515a5a 253
f37ecbce
TG
254 parent_irq = irq_of_parse_and_map(np, i);
255 spear_shirq_register(shirq_blocks[i], parent_irq);
c5d1d857 256 hwirq += shirq_blocks[i]->nr_irqs;
80515a5a
SH
257 }
258
4c18e77f 259 return 0;
80515a5a
SH
260
261err_free_desc:
c5d1d857 262 irq_free_descs(virq_base, nr_irqs);
80515a5a
SH
263err_unmap:
264 iounmap(base);
265 return -ENXIO;
266}
267
078bc005
TG
268static int __init spear300_shirq_of_init(struct device_node *np,
269 struct device_node *parent)
80515a5a
SH
270{
271 return shirq_init(spear300_shirq_blocks,
272 ARRAY_SIZE(spear300_shirq_blocks), np);
273}
e9c51558 274IRQCHIP_DECLARE(spear300_shirq, "st,spear300-shirq", spear300_shirq_of_init);
80515a5a 275
078bc005
TG
276static int __init spear310_shirq_of_init(struct device_node *np,
277 struct device_node *parent)
80515a5a
SH
278{
279 return shirq_init(spear310_shirq_blocks,
280 ARRAY_SIZE(spear310_shirq_blocks), np);
281}
e9c51558 282IRQCHIP_DECLARE(spear310_shirq, "st,spear310-shirq", spear310_shirq_of_init);
80515a5a 283
078bc005
TG
284static int __init spear320_shirq_of_init(struct device_node *np,
285 struct device_node *parent)
80515a5a
SH
286{
287 return shirq_init(spear320_shirq_blocks,
288 ARRAY_SIZE(spear320_shirq_blocks), np);
4c18e77f 289}
e9c51558 290IRQCHIP_DECLARE(spear320_shirq, "st,spear320-shirq", spear320_shirq_of_init);