blob: 1d3d273309bd3a561510298093f9d3b67703d62a [file] [log] [blame]
Michal Simekeedbdab2009-03-27 14:25:49 +01001/*
Michal Simek968674b2013-08-27 10:48:29 +02002 * Copyright (C) 2007-2013 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2012-2013 Xilinx, Inc.
Michal Simekeedbdab2009-03-27 14:25:49 +01004 * Copyright (C) 2007-2009 PetaLogix
5 * Copyright (C) 2006 Atmark Techno, Inc.
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details.
10 */
11
Grant Likely2462bac2012-01-26 14:10:13 -070012#include <linux/irqdomain.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010013#include <linux/irq.h>
Joel Porquetfd4b2672015-07-07 17:13:15 -040014#include <linux/irqchip.h>
Zubair Lutfullah Kakakhel9689c992016-11-14 12:13:49 +000015#include <linux/irqchip/chained_irq.h>
Michal Simekbcff6612013-08-27 10:49:00 +020016#include <linux/of_address.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010017#include <linux/io.h>
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +000018#include <linux/jump_label.h>
John Williams892ee922009-07-29 22:08:40 +100019#include <linux/bug.h>
Zubair Lutfullah Kakakhel9689c992016-11-14 12:13:49 +000020#include <linux/of_irq.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010021
Michal Simekeedbdab2009-03-27 14:25:49 +010022/* No one else should require these constants, so define them locally here. */
23#define ISR 0x00 /* Interrupt Status Register */
24#define IPR 0x04 /* Interrupt Pending Register */
25#define IER 0x08 /* Interrupt Enable Register */
26#define IAR 0x0c /* Interrupt Acknowledge Register */
27#define SIE 0x10 /* Set Interrupt Enable bits */
28#define CIE 0x14 /* Clear Interrupt Enable bits */
29#define IVR 0x18 /* Interrupt Vector Register */
30#define MER 0x1c /* Master Enable Register */
31
32#define MER_ME (1<<0)
33#define MER_HIE (1<<1)
34
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +000035static DEFINE_STATIC_KEY_FALSE(xintc_is_be);
Michal Simek1aa12432014-02-24 14:56:32 +010036
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +000037struct xintc_irq_chip {
38 void __iomem *base;
39 struct irq_domain *root_domain;
40 u32 intr_mask;
Mubin Sayyed67862a32020-03-17 18:25:57 +053041 u32 nr_irq;
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +000042};
43
Mubin Sayyed67862a32020-03-17 18:25:57 +053044static struct xintc_irq_chip *primary_intc;
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +000045
Mubin Sayyed67862a32020-03-17 18:25:57 +053046static void xintc_write(struct xintc_irq_chip *irqc, int reg, u32 data)
Michal Simek1aa12432014-02-24 14:56:32 +010047{
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +000048 if (static_branch_unlikely(&xintc_is_be))
Mubin Sayyed67862a32020-03-17 18:25:57 +053049 iowrite32be(data, irqc->base + reg);
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +000050 else
Mubin Sayyed67862a32020-03-17 18:25:57 +053051 iowrite32(data, irqc->base + reg);
Michal Simek1aa12432014-02-24 14:56:32 +010052}
53
Mubin Sayyed67862a32020-03-17 18:25:57 +053054static u32 xintc_read(struct xintc_irq_chip *irqc, int reg)
Michal Simek1aa12432014-02-24 14:56:32 +010055{
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +000056 if (static_branch_unlikely(&xintc_is_be))
Mubin Sayyed67862a32020-03-17 18:25:57 +053057 return ioread32be(irqc->base + reg);
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +000058 else
Mubin Sayyed67862a32020-03-17 18:25:57 +053059 return ioread32(irqc->base + reg);
Michal Simek1aa12432014-02-24 14:56:32 +010060}
61
Thomas Gleixner6f205a42011-02-06 19:36:30 +000062static void intc_enable_or_unmask(struct irq_data *d)
Michal Simekeedbdab2009-03-27 14:25:49 +010063{
Mubin Sayyed67862a32020-03-17 18:25:57 +053064 struct xintc_irq_chip *irqc = irq_data_get_irq_chip_data(d);
65 unsigned long mask = BIT(d->hwirq);
Michal Simek6c7a2672011-12-09 10:45:20 +010066
Zubair Lutfullah Kakakhela5734de2016-11-14 12:13:46 +000067 pr_debug("irq-xilinx: enable_or_unmask: %ld\n", d->hwirq);
steve@digidescorp.com33d9ff52009-11-17 08:43:39 -060068
69 /* ack level irqs because they can't be acked during
70 * ack function since the handle_level_irq function
71 * acks the irq before calling the interrupt handler
72 */
Thomas Gleixner4adc1922011-03-24 14:52:04 +010073 if (irqd_is_level_type(d))
Mubin Sayyed67862a32020-03-17 18:25:57 +053074 xintc_write(irqc, IAR, mask);
Michal Simek7958a682012-11-05 11:51:13 +010075
Mubin Sayyed67862a32020-03-17 18:25:57 +053076 xintc_write(irqc, SIE, mask);
Michal Simekeedbdab2009-03-27 14:25:49 +010077}
78
Thomas Gleixner6f205a42011-02-06 19:36:30 +000079static void intc_disable_or_mask(struct irq_data *d)
Michal Simekeedbdab2009-03-27 14:25:49 +010080{
Mubin Sayyed67862a32020-03-17 18:25:57 +053081 struct xintc_irq_chip *irqc = irq_data_get_irq_chip_data(d);
82
Zubair Lutfullah Kakakhela5734de2016-11-14 12:13:46 +000083 pr_debug("irq-xilinx: disable: %ld\n", d->hwirq);
Mubin Sayyed67862a32020-03-17 18:25:57 +053084 xintc_write(irqc, CIE, BIT(d->hwirq));
Michal Simekeedbdab2009-03-27 14:25:49 +010085}
86
Thomas Gleixner6f205a42011-02-06 19:36:30 +000087static void intc_ack(struct irq_data *d)
Michal Simekeedbdab2009-03-27 14:25:49 +010088{
Mubin Sayyed67862a32020-03-17 18:25:57 +053089 struct xintc_irq_chip *irqc = irq_data_get_irq_chip_data(d);
90
Zubair Lutfullah Kakakhela5734de2016-11-14 12:13:46 +000091 pr_debug("irq-xilinx: ack: %ld\n", d->hwirq);
Mubin Sayyed67862a32020-03-17 18:25:57 +053092 xintc_write(irqc, IAR, BIT(d->hwirq));
Michal Simekeedbdab2009-03-27 14:25:49 +010093}
94
Thomas Gleixner6f205a42011-02-06 19:36:30 +000095static void intc_mask_ack(struct irq_data *d)
Michal Simekeedbdab2009-03-27 14:25:49 +010096{
Mubin Sayyed67862a32020-03-17 18:25:57 +053097 struct xintc_irq_chip *irqc = irq_data_get_irq_chip_data(d);
98 unsigned long mask = BIT(d->hwirq);
Michal Simek6c7a2672011-12-09 10:45:20 +010099
Zubair Lutfullah Kakakhela5734de2016-11-14 12:13:46 +0000100 pr_debug("irq-xilinx: disable_and_ack: %ld\n", d->hwirq);
Mubin Sayyed67862a32020-03-17 18:25:57 +0530101 xintc_write(irqc, CIE, mask);
102 xintc_write(irqc, IAR, mask);
Michal Simekeedbdab2009-03-27 14:25:49 +0100103}
104
Michal Simekeedbdab2009-03-27 14:25:49 +0100105static struct irq_chip intc_dev = {
106 .name = "Xilinx INTC",
Thomas Gleixner6f205a42011-02-06 19:36:30 +0000107 .irq_unmask = intc_enable_or_unmask,
108 .irq_mask = intc_disable_or_mask,
109 .irq_ack = intc_ack,
110 .irq_mask_ack = intc_mask_ack,
Michal Simekeedbdab2009-03-27 14:25:49 +0100111};
112
Mubin Sayyed67862a32020-03-17 18:25:57 +0530113static unsigned int xintc_get_irq_local(struct xintc_irq_chip *irqc)
114{
115 unsigned int irq = 0;
116 u32 hwirq;
117
118 hwirq = xintc_read(irqc, IVR);
119 if (hwirq != -1U)
120 irq = irq_find_mapping(irqc->root_domain, hwirq);
121
122 pr_debug("irq-xilinx: hwirq=%d, irq=%d\n", hwirq, irq);
123
124 return irq;
125}
126
Marc Zyngier4cea7492020-03-30 10:43:59 +0100127unsigned int xintc_get_irq(void)
128{
129 unsigned int irq = -1;
130 u32 hwirq;
131
132 hwirq = xintc_read(primary_intc, IVR);
133 if (hwirq != -1U)
134 irq = irq_find_mapping(primary_intc->root_domain, hwirq);
135
136 pr_debug("irq-xilinx: hwirq=%d, irq=%d\n", hwirq, irq);
137
138 return irq;
139}
140
Michal Simekc0d997f2012-12-13 17:30:05 +0100141static int xintc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
Grant Likely2462bac2012-01-26 14:10:13 -0700142{
Mubin Sayyed67862a32020-03-17 18:25:57 +0530143 struct xintc_irq_chip *irqc = d->host_data;
144
145 if (irqc->intr_mask & BIT(hw)) {
Grant Likely2462bac2012-01-26 14:10:13 -0700146 irq_set_chip_and_handler_name(irq, &intc_dev,
Mubin Sayyed67862a32020-03-17 18:25:57 +0530147 handle_edge_irq, "edge");
Grant Likely2462bac2012-01-26 14:10:13 -0700148 irq_clear_status_flags(irq, IRQ_LEVEL);
149 } else {
150 irq_set_chip_and_handler_name(irq, &intc_dev,
Mubin Sayyed67862a32020-03-17 18:25:57 +0530151 handle_level_irq, "level");
Grant Likely2462bac2012-01-26 14:10:13 -0700152 irq_set_status_flags(irq, IRQ_LEVEL);
153 }
Mubin Sayyed67862a32020-03-17 18:25:57 +0530154 irq_set_chip_data(irq, irqc);
Grant Likely2462bac2012-01-26 14:10:13 -0700155 return 0;
156}
157
158static const struct irq_domain_ops xintc_irq_domain_ops = {
159 .xlate = irq_domain_xlate_onetwocell,
160 .map = xintc_map,
161};
162
Zubair Lutfullah Kakakhel9689c992016-11-14 12:13:49 +0000163static void xil_intc_irq_handler(struct irq_desc *desc)
164{
165 struct irq_chip *chip = irq_desc_get_chip(desc);
Mubin Sayyed67862a32020-03-17 18:25:57 +0530166 struct xintc_irq_chip *irqc;
Zubair Lutfullah Kakakhel9689c992016-11-14 12:13:49 +0000167 u32 pending;
168
Mubin Sayyed67862a32020-03-17 18:25:57 +0530169 irqc = irq_data_get_irq_handler_data(&desc->irq_data);
Zubair Lutfullah Kakakhel9689c992016-11-14 12:13:49 +0000170 chained_irq_enter(chip, desc);
171 do {
Mubin Sayyed67862a32020-03-17 18:25:57 +0530172 pending = xintc_get_irq_local(irqc);
173 if (pending == 0)
Zubair Lutfullah Kakakhel9689c992016-11-14 12:13:49 +0000174 break;
175 generic_handle_irq(pending);
176 } while (true);
177 chained_irq_exit(chip, desc);
178}
179
Michal Simek8a9e90a2013-08-27 10:49:00 +0200180static int __init xilinx_intc_of_init(struct device_node *intc,
181 struct device_node *parent)
Michal Simekeedbdab2009-03-27 14:25:49 +0100182{
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +0000183 struct xintc_irq_chip *irqc;
Mubin Sayyed67862a32020-03-17 18:25:57 +0530184 int ret, irq;
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +0000185
186 irqc = kzalloc(sizeof(*irqc), GFP_KERNEL);
187 if (!irqc)
188 return -ENOMEM;
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +0000189 irqc->base = of_iomap(intc, 0);
190 BUG_ON(!irqc->base);
Michal Simekeedbdab2009-03-27 14:25:49 +0100191
Mubin Sayyed67862a32020-03-17 18:25:57 +0530192 ret = of_property_read_u32(intc, "xlnx,num-intr-inputs", &irqc->nr_irq);
Michal Simekbcff6612013-08-27 10:49:00 +0200193 if (ret < 0) {
Zubair Lutfullah Kakakhela5734de2016-11-14 12:13:46 +0000194 pr_err("irq-xilinx: unable to read xlnx,num-intr-inputs\n");
Mubin Sayyed67862a32020-03-17 18:25:57 +0530195 goto error;
Michal Simekbcff6612013-08-27 10:49:00 +0200196 }
197
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +0000198 ret = of_property_read_u32(intc, "xlnx,kind-of-intr", &irqc->intr_mask);
Michal Simekbcff6612013-08-27 10:49:00 +0200199 if (ret < 0) {
Zubair Lutfullah Kakakhel8a11da52016-11-14 12:13:50 +0000200 pr_warn("irq-xilinx: unable to read xlnx,kind-of-intr\n");
201 irqc->intr_mask = 0;
Michal Simekbcff6612013-08-27 10:49:00 +0200202 }
203
Mubin Sayyed67862a32020-03-17 18:25:57 +0530204 if (irqc->intr_mask >> irqc->nr_irq)
Zubair Lutfullah Kakakhela5734de2016-11-14 12:13:46 +0000205 pr_warn("irq-xilinx: mismatch in kind-of-intr param\n");
Michal Simekeedbdab2009-03-27 14:25:49 +0100206
Rob Herringe81f54c2017-07-18 16:43:10 -0500207 pr_info("irq-xilinx: %pOF: num_irq=%d, edge=0x%x\n",
Mubin Sayyed67862a32020-03-17 18:25:57 +0530208 intc, irqc->nr_irq, irqc->intr_mask);
Michal Simekeedbdab2009-03-27 14:25:49 +0100209
Michal Simek1aa12432014-02-24 14:56:32 +0100210
Michal Simekeedbdab2009-03-27 14:25:49 +0100211 /*
212 * Disable all external interrupts until they are
213 * explicity requested.
214 */
Mubin Sayyed67862a32020-03-17 18:25:57 +0530215 xintc_write(irqc, IER, 0);
Michal Simekeedbdab2009-03-27 14:25:49 +0100216
217 /* Acknowledge any pending interrupts just in case. */
Mubin Sayyed67862a32020-03-17 18:25:57 +0530218 xintc_write(irqc, IAR, 0xffffffff);
Michal Simekeedbdab2009-03-27 14:25:49 +0100219
220 /* Turn on the Master Enable. */
Mubin Sayyed67862a32020-03-17 18:25:57 +0530221 xintc_write(irqc, MER, MER_HIE | MER_ME);
222 if (xintc_read(irqc, MER) != (MER_HIE | MER_ME)) {
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +0000223 static_branch_enable(&xintc_is_be);
Mubin Sayyed67862a32020-03-17 18:25:57 +0530224 xintc_write(irqc, MER, MER_HIE | MER_ME);
Michal Simek1aa12432014-02-24 14:56:32 +0100225 }
Michal Simekeedbdab2009-03-27 14:25:49 +0100226
Mubin Sayyed67862a32020-03-17 18:25:57 +0530227 irqc->root_domain = irq_domain_add_linear(intc, irqc->nr_irq,
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +0000228 &xintc_irq_domain_ops, irqc);
229 if (!irqc->root_domain) {
230 pr_err("irq-xilinx: Unable to create IRQ domain\n");
Michal Simekc74038b2020-03-17 18:25:58 +0530231 ret = -EINVAL;
Mubin Sayyed67862a32020-03-17 18:25:57 +0530232 goto error;
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +0000233 }
Dan Christensen7c2c8512013-03-17 04:48:56 -0500234
Zubair Lutfullah Kakakhel9689c992016-11-14 12:13:49 +0000235 if (parent) {
236 irq = irq_of_parse_and_map(intc, 0);
237 if (irq) {
238 irq_set_chained_handler_and_data(irq,
239 xil_intc_irq_handler,
240 irqc);
241 } else {
242 pr_err("irq-xilinx: interrupts property not in DT\n");
243 ret = -EINVAL;
Mubin Sayyed67862a32020-03-17 18:25:57 +0530244 goto error;
Zubair Lutfullah Kakakhel9689c992016-11-14 12:13:49 +0000245 }
246 } else {
Mubin Sayyed67862a32020-03-17 18:25:57 +0530247 primary_intc = irqc;
Marc Zyngiere02f6c02020-03-30 10:41:58 +0100248 irq_set_default_host(primary_intc->root_domain);
Zubair Lutfullah Kakakhel9689c992016-11-14 12:13:49 +0000249 }
Michal Simek8a9e90a2013-08-27 10:49:00 +0200250
251 return 0;
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +0000252
Mubin Sayyed67862a32020-03-17 18:25:57 +0530253error:
254 iounmap(irqc->base);
Zubair Lutfullah Kakakhel591db742016-11-14 12:13:47 +0000255 kfree(irqc);
256 return ret;
257
Michal Simekeedbdab2009-03-27 14:25:49 +0100258}
Michal Simek8a9e90a2013-08-27 10:49:00 +0200259
Zubair Lutfullah Kakakhel83282552016-11-14 12:13:51 +0000260IRQCHIP_DECLARE(xilinx_intc_xps, "xlnx,xps-intc-1.00.a", xilinx_intc_of_init);
261IRQCHIP_DECLARE(xilinx_intc_opb, "xlnx,opb-intc-1.00.c", xilinx_intc_of_init);