Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 1 | /* |
Michal Simek | 968674b | 2013-08-27 10:48:29 +0200 | [diff] [blame] | 2 | * Copyright (C) 2007-2013 Michal Simek <monstr@monstr.eu> |
| 3 | * Copyright (C) 2012-2013 Xilinx, Inc. |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 4 | * 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 Likely | 2462bac | 2012-01-26 14:10:13 -0700 | [diff] [blame] | 12 | #include <linux/irqdomain.h> |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 13 | #include <linux/irq.h> |
Joel Porquet | fd4b267 | 2015-07-07 17:13:15 -0400 | [diff] [blame] | 14 | #include <linux/irqchip.h> |
Zubair Lutfullah Kakakhel | 9689c99 | 2016-11-14 12:13:49 +0000 | [diff] [blame] | 15 | #include <linux/irqchip/chained_irq.h> |
Michal Simek | bcff661 | 2013-08-27 10:49:00 +0200 | [diff] [blame] | 16 | #include <linux/of_address.h> |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 17 | #include <linux/io.h> |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 18 | #include <linux/jump_label.h> |
John Williams | 892ee92 | 2009-07-29 22:08:40 +1000 | [diff] [blame] | 19 | #include <linux/bug.h> |
Zubair Lutfullah Kakakhel | 9689c99 | 2016-11-14 12:13:49 +0000 | [diff] [blame] | 20 | #include <linux/of_irq.h> |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 21 | |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 22 | /* 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 Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 35 | static DEFINE_STATIC_KEY_FALSE(xintc_is_be); |
Michal Simek | 1aa1243 | 2014-02-24 14:56:32 +0100 | [diff] [blame] | 36 | |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 37 | struct xintc_irq_chip { |
| 38 | void __iomem *base; |
| 39 | struct irq_domain *root_domain; |
| 40 | u32 intr_mask; |
| 41 | }; |
| 42 | |
| 43 | static struct xintc_irq_chip *xintc_irqc; |
| 44 | |
| 45 | static void xintc_write(int reg, u32 data) |
Michal Simek | 1aa1243 | 2014-02-24 14:56:32 +0100 | [diff] [blame] | 46 | { |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 47 | if (static_branch_unlikely(&xintc_is_be)) |
| 48 | iowrite32be(data, xintc_irqc->base + reg); |
| 49 | else |
| 50 | iowrite32(data, xintc_irqc->base + reg); |
Michal Simek | 1aa1243 | 2014-02-24 14:56:32 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 53 | static unsigned int xintc_read(int reg) |
Michal Simek | 1aa1243 | 2014-02-24 14:56:32 +0100 | [diff] [blame] | 54 | { |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 55 | if (static_branch_unlikely(&xintc_is_be)) |
| 56 | return ioread32be(xintc_irqc->base + reg); |
| 57 | else |
| 58 | return ioread32(xintc_irqc->base + reg); |
Michal Simek | 1aa1243 | 2014-02-24 14:56:32 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Thomas Gleixner | 6f205a4 | 2011-02-06 19:36:30 +0000 | [diff] [blame] | 61 | static void intc_enable_or_unmask(struct irq_data *d) |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 62 | { |
Michal Simek | 6c7a267 | 2011-12-09 10:45:20 +0100 | [diff] [blame] | 63 | unsigned long mask = 1 << d->hwirq; |
| 64 | |
Zubair Lutfullah Kakakhel | a5734de | 2016-11-14 12:13:46 +0000 | [diff] [blame] | 65 | pr_debug("irq-xilinx: enable_or_unmask: %ld\n", d->hwirq); |
steve@digidescorp.com | 33d9ff5 | 2009-11-17 08:43:39 -0600 | [diff] [blame] | 66 | |
| 67 | /* ack level irqs because they can't be acked during |
| 68 | * ack function since the handle_level_irq function |
| 69 | * acks the irq before calling the interrupt handler |
| 70 | */ |
Thomas Gleixner | 4adc192 | 2011-03-24 14:52:04 +0100 | [diff] [blame] | 71 | if (irqd_is_level_type(d)) |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 72 | xintc_write(IAR, mask); |
Michal Simek | 7958a68 | 2012-11-05 11:51:13 +0100 | [diff] [blame] | 73 | |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 74 | xintc_write(SIE, mask); |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Thomas Gleixner | 6f205a4 | 2011-02-06 19:36:30 +0000 | [diff] [blame] | 77 | static void intc_disable_or_mask(struct irq_data *d) |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 78 | { |
Zubair Lutfullah Kakakhel | a5734de | 2016-11-14 12:13:46 +0000 | [diff] [blame] | 79 | pr_debug("irq-xilinx: disable: %ld\n", d->hwirq); |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 80 | xintc_write(CIE, 1 << d->hwirq); |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Thomas Gleixner | 6f205a4 | 2011-02-06 19:36:30 +0000 | [diff] [blame] | 83 | static void intc_ack(struct irq_data *d) |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 84 | { |
Zubair Lutfullah Kakakhel | a5734de | 2016-11-14 12:13:46 +0000 | [diff] [blame] | 85 | pr_debug("irq-xilinx: ack: %ld\n", d->hwirq); |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 86 | xintc_write(IAR, 1 << d->hwirq); |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Thomas Gleixner | 6f205a4 | 2011-02-06 19:36:30 +0000 | [diff] [blame] | 89 | static void intc_mask_ack(struct irq_data *d) |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 90 | { |
Michal Simek | 6c7a267 | 2011-12-09 10:45:20 +0100 | [diff] [blame] | 91 | unsigned long mask = 1 << d->hwirq; |
| 92 | |
Zubair Lutfullah Kakakhel | a5734de | 2016-11-14 12:13:46 +0000 | [diff] [blame] | 93 | pr_debug("irq-xilinx: disable_and_ack: %ld\n", d->hwirq); |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 94 | xintc_write(CIE, mask); |
| 95 | xintc_write(IAR, mask); |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 98 | static struct irq_chip intc_dev = { |
| 99 | .name = "Xilinx INTC", |
Thomas Gleixner | 6f205a4 | 2011-02-06 19:36:30 +0000 | [diff] [blame] | 100 | .irq_unmask = intc_enable_or_unmask, |
| 101 | .irq_mask = intc_disable_or_mask, |
| 102 | .irq_ack = intc_ack, |
| 103 | .irq_mask_ack = intc_mask_ack, |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 104 | }; |
| 105 | |
Zubair Lutfullah Kakakhel | 2120a43 | 2016-11-14 12:13:48 +0000 | [diff] [blame] | 106 | unsigned int xintc_get_irq(void) |
Grant Likely | 2462bac | 2012-01-26 14:10:13 -0700 | [diff] [blame] | 107 | { |
| 108 | unsigned int hwirq, irq = -1; |
| 109 | |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 110 | hwirq = xintc_read(IVR); |
Grant Likely | 2462bac | 2012-01-26 14:10:13 -0700 | [diff] [blame] | 111 | if (hwirq != -1U) |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 112 | irq = irq_find_mapping(xintc_irqc->root_domain, hwirq); |
Grant Likely | 2462bac | 2012-01-26 14:10:13 -0700 | [diff] [blame] | 113 | |
Zubair Lutfullah Kakakhel | a5734de | 2016-11-14 12:13:46 +0000 | [diff] [blame] | 114 | pr_debug("irq-xilinx: hwirq=%d, irq=%d\n", hwirq, irq); |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 115 | |
| 116 | return irq; |
| 117 | } |
| 118 | |
Michal Simek | c0d997f | 2012-12-13 17:30:05 +0100 | [diff] [blame] | 119 | static int xintc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw) |
Grant Likely | 2462bac | 2012-01-26 14:10:13 -0700 | [diff] [blame] | 120 | { |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 121 | if (xintc_irqc->intr_mask & (1 << hw)) { |
Grant Likely | 2462bac | 2012-01-26 14:10:13 -0700 | [diff] [blame] | 122 | irq_set_chip_and_handler_name(irq, &intc_dev, |
| 123 | handle_edge_irq, "edge"); |
| 124 | irq_clear_status_flags(irq, IRQ_LEVEL); |
| 125 | } else { |
| 126 | irq_set_chip_and_handler_name(irq, &intc_dev, |
| 127 | handle_level_irq, "level"); |
| 128 | irq_set_status_flags(irq, IRQ_LEVEL); |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static const struct irq_domain_ops xintc_irq_domain_ops = { |
| 134 | .xlate = irq_domain_xlate_onetwocell, |
| 135 | .map = xintc_map, |
| 136 | }; |
| 137 | |
Zubair Lutfullah Kakakhel | 9689c99 | 2016-11-14 12:13:49 +0000 | [diff] [blame] | 138 | static void xil_intc_irq_handler(struct irq_desc *desc) |
| 139 | { |
| 140 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 141 | u32 pending; |
| 142 | |
| 143 | chained_irq_enter(chip, desc); |
| 144 | do { |
| 145 | pending = xintc_get_irq(); |
| 146 | if (pending == -1U) |
| 147 | break; |
| 148 | generic_handle_irq(pending); |
| 149 | } while (true); |
| 150 | chained_irq_exit(chip, desc); |
| 151 | } |
| 152 | |
Michal Simek | 8a9e90a | 2013-08-27 10:49:00 +0200 | [diff] [blame] | 153 | static int __init xilinx_intc_of_init(struct device_node *intc, |
| 154 | struct device_node *parent) |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 155 | { |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 156 | u32 nr_irq; |
Zubair Lutfullah Kakakhel | 9689c99 | 2016-11-14 12:13:49 +0000 | [diff] [blame] | 157 | int ret, irq; |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 158 | struct xintc_irq_chip *irqc; |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 159 | |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 160 | if (xintc_irqc) { |
| 161 | pr_err("irq-xilinx: Multiple instances aren't supported\n"); |
| 162 | return -EINVAL; |
| 163 | } |
| 164 | |
| 165 | irqc = kzalloc(sizeof(*irqc), GFP_KERNEL); |
| 166 | if (!irqc) |
| 167 | return -ENOMEM; |
| 168 | |
| 169 | xintc_irqc = irqc; |
| 170 | |
| 171 | irqc->base = of_iomap(intc, 0); |
| 172 | BUG_ON(!irqc->base); |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 173 | |
Michal Simek | bcff661 | 2013-08-27 10:49:00 +0200 | [diff] [blame] | 174 | ret = of_property_read_u32(intc, "xlnx,num-intr-inputs", &nr_irq); |
| 175 | if (ret < 0) { |
Zubair Lutfullah Kakakhel | a5734de | 2016-11-14 12:13:46 +0000 | [diff] [blame] | 176 | pr_err("irq-xilinx: unable to read xlnx,num-intr-inputs\n"); |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 177 | goto err_alloc; |
Michal Simek | bcff661 | 2013-08-27 10:49:00 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 180 | ret = of_property_read_u32(intc, "xlnx,kind-of-intr", &irqc->intr_mask); |
Michal Simek | bcff661 | 2013-08-27 10:49:00 +0200 | [diff] [blame] | 181 | if (ret < 0) { |
Zubair Lutfullah Kakakhel | 8a11da5 | 2016-11-14 12:13:50 +0000 | [diff] [blame] | 182 | pr_warn("irq-xilinx: unable to read xlnx,kind-of-intr\n"); |
| 183 | irqc->intr_mask = 0; |
Michal Simek | bcff661 | 2013-08-27 10:49:00 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 186 | if (irqc->intr_mask >> nr_irq) |
Zubair Lutfullah Kakakhel | a5734de | 2016-11-14 12:13:46 +0000 | [diff] [blame] | 187 | pr_warn("irq-xilinx: mismatch in kind-of-intr param\n"); |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 188 | |
Rob Herring | e81f54c | 2017-07-18 16:43:10 -0500 | [diff] [blame] | 189 | pr_info("irq-xilinx: %pOF: num_irq=%d, edge=0x%x\n", |
| 190 | intc, nr_irq, irqc->intr_mask); |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 191 | |
Michal Simek | 1aa1243 | 2014-02-24 14:56:32 +0100 | [diff] [blame] | 192 | |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 193 | /* |
| 194 | * Disable all external interrupts until they are |
| 195 | * explicity requested. |
| 196 | */ |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 197 | xintc_write(IER, 0); |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 198 | |
| 199 | /* Acknowledge any pending interrupts just in case. */ |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 200 | xintc_write(IAR, 0xffffffff); |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 201 | |
| 202 | /* Turn on the Master Enable. */ |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 203 | xintc_write(MER, MER_HIE | MER_ME); |
| 204 | if (!(xintc_read(MER) & (MER_HIE | MER_ME))) { |
| 205 | static_branch_enable(&xintc_is_be); |
| 206 | xintc_write(MER, MER_HIE | MER_ME); |
Michal Simek | 1aa1243 | 2014-02-24 14:56:32 +0100 | [diff] [blame] | 207 | } |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 208 | |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 209 | irqc->root_domain = irq_domain_add_linear(intc, nr_irq, |
| 210 | &xintc_irq_domain_ops, irqc); |
| 211 | if (!irqc->root_domain) { |
| 212 | pr_err("irq-xilinx: Unable to create IRQ domain\n"); |
| 213 | goto err_alloc; |
| 214 | } |
Dan Christensen | 7c2c851 | 2013-03-17 04:48:56 -0500 | [diff] [blame] | 215 | |
Zubair Lutfullah Kakakhel | 9689c99 | 2016-11-14 12:13:49 +0000 | [diff] [blame] | 216 | if (parent) { |
| 217 | irq = irq_of_parse_and_map(intc, 0); |
| 218 | if (irq) { |
| 219 | irq_set_chained_handler_and_data(irq, |
| 220 | xil_intc_irq_handler, |
| 221 | irqc); |
| 222 | } else { |
| 223 | pr_err("irq-xilinx: interrupts property not in DT\n"); |
| 224 | ret = -EINVAL; |
| 225 | goto err_alloc; |
| 226 | } |
| 227 | } else { |
| 228 | irq_set_default_host(irqc->root_domain); |
| 229 | } |
Michal Simek | 8a9e90a | 2013-08-27 10:49:00 +0200 | [diff] [blame] | 230 | |
| 231 | return 0; |
Zubair Lutfullah Kakakhel | 591db74 | 2016-11-14 12:13:47 +0000 | [diff] [blame] | 232 | |
| 233 | err_alloc: |
| 234 | xintc_irqc = NULL; |
| 235 | kfree(irqc); |
| 236 | return ret; |
| 237 | |
Michal Simek | eedbdab | 2009-03-27 14:25:49 +0100 | [diff] [blame] | 238 | } |
Michal Simek | 8a9e90a | 2013-08-27 10:49:00 +0200 | [diff] [blame] | 239 | |
Zubair Lutfullah Kakakhel | 8328255 | 2016-11-14 12:13:51 +0000 | [diff] [blame] | 240 | IRQCHIP_DECLARE(xilinx_intc_xps, "xlnx,xps-intc-1.00.a", xilinx_intc_of_init); |
| 241 | IRQCHIP_DECLARE(xilinx_intc_opb, "xlnx,opb-intc-1.00.c", xilinx_intc_of_init); |