blob: 9655a574c86f9ab20065358996294911f9915d49 [file] [log] [blame]
Benjamin Gaignard8de50dc2017-11-30 09:45:00 +01001// SPDX-License-Identifier: GPL-2.0
Alexandre TORGUEe07204162016-09-20 18:00:57 +02002/*
3 * Copyright (C) Maxime Coquelin 2015
Benjamin Gaignard8de50dc2017-11-30 09:45:00 +01004 * Copyright (C) STMicroelectronics 2017
Alexandre TORGUEe07204162016-09-20 18:00:57 +02005 * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Alexandre TORGUEe07204162016-09-20 18:00:57 +02006 */
7
8#include <linux/bitops.h>
9#include <linux/interrupt.h>
10#include <linux/io.h>
11#include <linux/irq.h>
12#include <linux/irqchip.h>
13#include <linux/irqchip/chained_irq.h>
14#include <linux/irqdomain.h>
15#include <linux/of_address.h>
16#include <linux/of_irq.h>
17
Ludovic Barre6dd64ee2017-11-06 18:03:32 +010018#define IRQS_PER_BANK 32
19
20struct stm32_exti_bank {
21 u32 imr_ofst;
22 u32 emr_ofst;
23 u32 rtsr_ofst;
24 u32 ftsr_ofst;
25 u32 swier_ofst;
Ludovic Barrebe6230f2018-04-26 18:18:26 +020026 u32 rpr_ofst;
27 u32 fpr_ofst;
Ludovic Barre6dd64ee2017-11-06 18:03:32 +010028};
29
Ludovic Barrebe6230f2018-04-26 18:18:26 +020030#define UNDEF_REG ~0
31
Ludovic Barref9fc1742018-04-26 18:18:28 +020032struct stm32_exti_drv_data {
33 const struct stm32_exti_bank **exti_banks;
34 u32 bank_nr;
35};
36
Ludovic Barred9e2b19b02018-04-26 18:18:27 +020037struct stm32_exti_chip_data {
Ludovic Barref9fc1742018-04-26 18:18:28 +020038 struct stm32_exti_host_data *host_data;
Ludovic Barred9e2b19b02018-04-26 18:18:27 +020039 const struct stm32_exti_bank *reg_bank;
40 u32 rtsr_cache;
41 u32 ftsr_cache;
42};
43
Ludovic Barref9fc1742018-04-26 18:18:28 +020044struct stm32_exti_host_data {
45 void __iomem *base;
46 struct stm32_exti_chip_data *chips_data;
47 const struct stm32_exti_drv_data *drv_data;
48};
Ludovic Barred9e2b19b02018-04-26 18:18:27 +020049
Ludovic Barre6dd64ee2017-11-06 18:03:32 +010050static const struct stm32_exti_bank stm32f4xx_exti_b1 = {
51 .imr_ofst = 0x00,
52 .emr_ofst = 0x04,
53 .rtsr_ofst = 0x08,
54 .ftsr_ofst = 0x0C,
55 .swier_ofst = 0x10,
Ludovic Barrebe6230f2018-04-26 18:18:26 +020056 .rpr_ofst = 0x14,
57 .fpr_ofst = UNDEF_REG,
Ludovic Barre6dd64ee2017-11-06 18:03:32 +010058};
59
60static const struct stm32_exti_bank *stm32f4xx_exti_banks[] = {
61 &stm32f4xx_exti_b1,
62};
63
Ludovic Barref9fc1742018-04-26 18:18:28 +020064static const struct stm32_exti_drv_data stm32f4xx_drv_data = {
65 .exti_banks = stm32f4xx_exti_banks,
66 .bank_nr = ARRAY_SIZE(stm32f4xx_exti_banks),
67};
68
Ludovic Barre539c6032017-11-06 18:03:34 +010069static const struct stm32_exti_bank stm32h7xx_exti_b1 = {
70 .imr_ofst = 0x80,
71 .emr_ofst = 0x84,
72 .rtsr_ofst = 0x00,
73 .ftsr_ofst = 0x04,
74 .swier_ofst = 0x08,
Ludovic Barrebe6230f2018-04-26 18:18:26 +020075 .rpr_ofst = 0x88,
76 .fpr_ofst = UNDEF_REG,
Ludovic Barre539c6032017-11-06 18:03:34 +010077};
78
79static const struct stm32_exti_bank stm32h7xx_exti_b2 = {
80 .imr_ofst = 0x90,
81 .emr_ofst = 0x94,
82 .rtsr_ofst = 0x20,
83 .ftsr_ofst = 0x24,
84 .swier_ofst = 0x28,
Ludovic Barrebe6230f2018-04-26 18:18:26 +020085 .rpr_ofst = 0x98,
86 .fpr_ofst = UNDEF_REG,
Ludovic Barre539c6032017-11-06 18:03:34 +010087};
88
89static const struct stm32_exti_bank stm32h7xx_exti_b3 = {
90 .imr_ofst = 0xA0,
91 .emr_ofst = 0xA4,
92 .rtsr_ofst = 0x40,
93 .ftsr_ofst = 0x44,
94 .swier_ofst = 0x48,
Ludovic Barrebe6230f2018-04-26 18:18:26 +020095 .rpr_ofst = 0xA8,
96 .fpr_ofst = UNDEF_REG,
Ludovic Barre539c6032017-11-06 18:03:34 +010097};
98
99static const struct stm32_exti_bank *stm32h7xx_exti_banks[] = {
100 &stm32h7xx_exti_b1,
101 &stm32h7xx_exti_b2,
102 &stm32h7xx_exti_b3,
103};
104
Ludovic Barref9fc1742018-04-26 18:18:28 +0200105static const struct stm32_exti_drv_data stm32h7xx_drv_data = {
106 .exti_banks = stm32h7xx_exti_banks,
107 .bank_nr = ARRAY_SIZE(stm32h7xx_exti_banks),
108};
109
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100110static unsigned long stm32_exti_pending(struct irq_chip_generic *gc)
111{
Ludovic Barred9e2b19b02018-04-26 18:18:27 +0200112 struct stm32_exti_chip_data *chip_data = gc->private;
113 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
Ludovic Barrebe6230f2018-04-26 18:18:26 +0200114 unsigned long pending;
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100115
Ludovic Barrebe6230f2018-04-26 18:18:26 +0200116 pending = irq_reg_readl(gc, stm32_bank->rpr_ofst);
117 if (stm32_bank->fpr_ofst != UNDEF_REG)
118 pending |= irq_reg_readl(gc, stm32_bank->fpr_ofst);
119
120 return pending;
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100121}
122
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200123static void stm32_irq_handler(struct irq_desc *desc)
124{
125 struct irq_domain *domain = irq_desc_get_handler_data(desc);
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200126 struct irq_chip *chip = irq_desc_get_chip(desc);
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100127 unsigned int virq, nbanks = domain->gc->num_chips;
128 struct irq_chip_generic *gc;
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200129 unsigned long pending;
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100130 int n, i, irq_base = 0;
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200131
132 chained_irq_enter(chip, desc);
133
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100134 for (i = 0; i < nbanks; i++, irq_base += IRQS_PER_BANK) {
135 gc = irq_get_domain_generic_chip(domain, irq_base);
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100136
137 while ((pending = stm32_exti_pending(gc))) {
138 for_each_set_bit(n, &pending, IRQS_PER_BANK) {
139 virq = irq_find_mapping(domain, irq_base + n);
140 generic_handle_irq(virq);
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100141 }
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200142 }
143 }
144
145 chained_irq_exit(chip, desc);
146}
147
148static int stm32_irq_set_type(struct irq_data *data, unsigned int type)
149{
150 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
Ludovic Barred9e2b19b02018-04-26 18:18:27 +0200151 struct stm32_exti_chip_data *chip_data = gc->private;
152 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100153 int pin = data->hwirq % IRQS_PER_BANK;
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200154 u32 rtsr, ftsr;
155
156 irq_gc_lock(gc);
157
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100158 rtsr = irq_reg_readl(gc, stm32_bank->rtsr_ofst);
159 ftsr = irq_reg_readl(gc, stm32_bank->ftsr_ofst);
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200160
161 switch (type) {
162 case IRQ_TYPE_EDGE_RISING:
163 rtsr |= BIT(pin);
164 ftsr &= ~BIT(pin);
165 break;
166 case IRQ_TYPE_EDGE_FALLING:
167 rtsr &= ~BIT(pin);
168 ftsr |= BIT(pin);
169 break;
170 case IRQ_TYPE_EDGE_BOTH:
171 rtsr |= BIT(pin);
172 ftsr |= BIT(pin);
173 break;
174 default:
175 irq_gc_unlock(gc);
176 return -EINVAL;
177 }
178
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100179 irq_reg_writel(gc, rtsr, stm32_bank->rtsr_ofst);
180 irq_reg_writel(gc, ftsr, stm32_bank->ftsr_ofst);
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200181
182 irq_gc_unlock(gc);
183
184 return 0;
185}
186
Ludovic Barred9e2b19b02018-04-26 18:18:27 +0200187static void stm32_irq_suspend(struct irq_chip_generic *gc)
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200188{
Ludovic Barred9e2b19b02018-04-26 18:18:27 +0200189 struct stm32_exti_chip_data *chip_data = gc->private;
190 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200191
192 irq_gc_lock(gc);
193
Ludovic Barred9e2b19b02018-04-26 18:18:27 +0200194 /* save rtsr, ftsr registers */
195 chip_data->rtsr_cache = irq_reg_readl(gc, stm32_bank->rtsr_ofst);
196 chip_data->ftsr_cache = irq_reg_readl(gc, stm32_bank->ftsr_ofst);
197
198 irq_reg_writel(gc, gc->wake_active, stm32_bank->imr_ofst);
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200199
200 irq_gc_unlock(gc);
Ludovic Barred9e2b19b02018-04-26 18:18:27 +0200201}
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200202
Ludovic Barred9e2b19b02018-04-26 18:18:27 +0200203static void stm32_irq_resume(struct irq_chip_generic *gc)
204{
205 struct stm32_exti_chip_data *chip_data = gc->private;
206 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
207
208 irq_gc_lock(gc);
209
210 /* restore rtsr, ftsr registers */
211 irq_reg_writel(gc, chip_data->rtsr_cache, stm32_bank->rtsr_ofst);
212 irq_reg_writel(gc, chip_data->ftsr_cache, stm32_bank->ftsr_ofst);
213
214 irq_reg_writel(gc, gc->mask_cache, stm32_bank->imr_ofst);
215
216 irq_gc_unlock(gc);
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200217}
218
219static int stm32_exti_alloc(struct irq_domain *d, unsigned int virq,
220 unsigned int nr_irqs, void *data)
221{
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200222 struct irq_fwspec *fwspec = data;
223 irq_hw_number_t hwirq;
224
225 hwirq = fwspec->param[0];
226
227 irq_map_generic_chip(d, virq, hwirq);
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200228
229 return 0;
230}
231
232static void stm32_exti_free(struct irq_domain *d, unsigned int virq,
233 unsigned int nr_irqs)
234{
235 struct irq_data *data = irq_domain_get_irq_data(d, virq);
236
237 irq_domain_reset_irq_data(data);
238}
239
Ludovic Barreea80aa22018-04-26 18:18:25 +0200240static const struct irq_domain_ops irq_exti_domain_ops = {
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200241 .map = irq_map_generic_chip,
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200242 .alloc = stm32_exti_alloc,
243 .free = stm32_exti_free,
244};
245
Ludovic Barrebe6230f2018-04-26 18:18:26 +0200246static void stm32_irq_ack(struct irq_data *d)
247{
248 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Ludovic Barred9e2b19b02018-04-26 18:18:27 +0200249 struct stm32_exti_chip_data *chip_data = gc->private;
250 const struct stm32_exti_bank *stm32_bank = chip_data->reg_bank;
Ludovic Barrebe6230f2018-04-26 18:18:26 +0200251
252 irq_gc_lock(gc);
253
254 irq_reg_writel(gc, d->mask, stm32_bank->rpr_ofst);
255 if (stm32_bank->fpr_ofst != UNDEF_REG)
256 irq_reg_writel(gc, d->mask, stm32_bank->fpr_ofst);
257
258 irq_gc_unlock(gc);
259}
Ludovic Barref9fc1742018-04-26 18:18:28 +0200260static struct
261stm32_exti_host_data *stm32_exti_host_init(const struct stm32_exti_drv_data *dd,
262 struct device_node *node)
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200263{
Ludovic Barref9fc1742018-04-26 18:18:28 +0200264 struct stm32_exti_host_data *host_data;
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200265
Ludovic Barref9fc1742018-04-26 18:18:28 +0200266 host_data = kzalloc(sizeof(*host_data), GFP_KERNEL);
267 if (!host_data)
268 return NULL;
269
270 host_data->drv_data = dd;
271 host_data->chips_data = kcalloc(dd->bank_nr,
272 sizeof(struct stm32_exti_chip_data),
273 GFP_KERNEL);
274 if (!host_data->chips_data)
275 return NULL;
276
277 host_data->base = of_iomap(node, 0);
278 if (!host_data->base) {
Rob Herringe81f54c2017-07-18 16:43:10 -0500279 pr_err("%pOF: Unable to map registers\n", node);
Ludovic Barref9fc1742018-04-26 18:18:28 +0200280 return NULL;
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200281 }
282
Ludovic Barref9fc1742018-04-26 18:18:28 +0200283 return host_data;
284}
Ludovic Barred9e2b19b02018-04-26 18:18:27 +0200285
Ludovic Barref9fc1742018-04-26 18:18:28 +0200286static struct
287stm32_exti_chip_data *stm32_exti_chip_init(struct stm32_exti_host_data *h_data,
288 u32 bank_idx,
289 struct device_node *node)
290{
291 const struct stm32_exti_bank *stm32_bank;
292 struct stm32_exti_chip_data *chip_data;
293 void __iomem *base = h_data->base;
294 u32 irqs_mask;
295
296 stm32_bank = h_data->drv_data->exti_banks[bank_idx];
297 chip_data = &h_data->chips_data[bank_idx];
298 chip_data->host_data = h_data;
299 chip_data->reg_bank = stm32_bank;
300
301 /* Determine number of irqs supported */
302 writel_relaxed(~0UL, base + stm32_bank->rtsr_ofst);
303 irqs_mask = readl_relaxed(base + stm32_bank->rtsr_ofst);
304
305 /*
306 * This IP has no reset, so after hot reboot we should
307 * clear registers to avoid residue
308 */
309 writel_relaxed(0, base + stm32_bank->imr_ofst);
310 writel_relaxed(0, base + stm32_bank->emr_ofst);
311 writel_relaxed(0, base + stm32_bank->rtsr_ofst);
312 writel_relaxed(0, base + stm32_bank->ftsr_ofst);
313 writel_relaxed(~0UL, base + stm32_bank->rpr_ofst);
314 if (stm32_bank->fpr_ofst != UNDEF_REG)
315 writel_relaxed(~0UL, base + stm32_bank->fpr_ofst);
316
317 pr_info("%s: bank%d, External IRQs available:%#x\n",
318 node->full_name, bank_idx, irqs_mask);
319
320 return chip_data;
321}
322
323static int __init stm32_exti_init(const struct stm32_exti_drv_data *drv_data,
324 struct device_node *node)
325{
326 struct stm32_exti_host_data *host_data;
327 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
328 int nr_irqs, ret, i;
329 struct irq_chip_generic *gc;
330 struct irq_domain *domain;
331
332 host_data = stm32_exti_host_init(drv_data, node);
333 if (!host_data) {
334 ret = -ENOMEM;
335 goto out_free_mem;
336 }
337
338 domain = irq_domain_add_linear(node, drv_data->bank_nr * IRQS_PER_BANK,
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200339 &irq_exti_domain_ops, NULL);
340 if (!domain) {
341 pr_err("%s: Could not register interrupt domain.\n",
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100342 node->name);
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200343 ret = -ENOMEM;
344 goto out_unmap;
345 }
346
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100347 ret = irq_alloc_domain_generic_chips(domain, IRQS_PER_BANK, 1, "exti",
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200348 handle_edge_irq, clr, 0, 0);
349 if (ret) {
Rob Herringe81f54c2017-07-18 16:43:10 -0500350 pr_err("%pOF: Could not allocate generic interrupt chip.\n",
Ludovic Barreea80aa22018-04-26 18:18:25 +0200351 node);
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200352 goto out_free_domain;
353 }
354
Ludovic Barref9fc1742018-04-26 18:18:28 +0200355 for (i = 0; i < drv_data->bank_nr; i++) {
356 const struct stm32_exti_bank *stm32_bank;
357 struct stm32_exti_chip_data *chip_data;
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100358
Ludovic Barref9fc1742018-04-26 18:18:28 +0200359 stm32_bank = drv_data->exti_banks[i];
360 chip_data = stm32_exti_chip_init(host_data, i, node);
Ludovic Barred9e2b19b02018-04-26 18:18:27 +0200361
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100362 gc = irq_get_domain_generic_chip(domain, i * IRQS_PER_BANK);
363
Ludovic Barref9fc1742018-04-26 18:18:28 +0200364 gc->reg_base = host_data->base;
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100365 gc->chip_types->type = IRQ_TYPE_EDGE_BOTH;
Ludovic Barrebe6230f2018-04-26 18:18:26 +0200366 gc->chip_types->chip.irq_ack = stm32_irq_ack;
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100367 gc->chip_types->chip.irq_mask = irq_gc_mask_clr_bit;
368 gc->chip_types->chip.irq_unmask = irq_gc_mask_set_bit;
369 gc->chip_types->chip.irq_set_type = stm32_irq_set_type;
Ludovic Barred9e2b19b02018-04-26 18:18:27 +0200370 gc->chip_types->chip.irq_set_wake = irq_gc_set_wake;
371 gc->suspend = stm32_irq_suspend;
372 gc->resume = stm32_irq_resume;
373 gc->wake_enabled = IRQ_MSK(IRQS_PER_BANK);
374
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100375 gc->chip_types->regs.mask = stm32_bank->imr_ofst;
Ludovic Barred9e2b19b02018-04-26 18:18:27 +0200376 gc->private = (void *)chip_data;
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100377 }
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200378
379 nr_irqs = of_irq_count(node);
380 for (i = 0; i < nr_irqs; i++) {
381 unsigned int irq = irq_of_parse_and_map(node, i);
382
383 irq_set_handler_data(irq, domain);
384 irq_set_chained_handler(irq, stm32_irq_handler);
385 }
386
387 return 0;
388
389out_free_domain:
390 irq_domain_remove(domain);
391out_unmap:
Ludovic Barref9fc1742018-04-26 18:18:28 +0200392 iounmap(host_data->base);
393out_free_mem:
394 kfree(host_data->chips_data);
395 kfree(host_data);
Alexandre TORGUEe07204162016-09-20 18:00:57 +0200396 return ret;
397}
398
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100399static int __init stm32f4_exti_of_init(struct device_node *np,
400 struct device_node *parent)
401{
Ludovic Barref9fc1742018-04-26 18:18:28 +0200402 return stm32_exti_init(&stm32f4xx_drv_data, np);
Ludovic Barre6dd64ee2017-11-06 18:03:32 +0100403}
404
405IRQCHIP_DECLARE(stm32f4_exti, "st,stm32-exti", stm32f4_exti_of_init);
Ludovic Barre539c6032017-11-06 18:03:34 +0100406
407static int __init stm32h7_exti_of_init(struct device_node *np,
408 struct device_node *parent)
409{
Ludovic Barref9fc1742018-04-26 18:18:28 +0200410 return stm32_exti_init(&stm32h7xx_drv_data, np);
Ludovic Barre539c6032017-11-06 18:03:34 +0100411}
412
413IRQCHIP_DECLARE(stm32h7_exti, "st,stm32h7-exti", stm32h7_exti_of_init);