blob: 90bec7d71f8562165a2c0cd9f758ac2b78d40599 [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>
Michal Simekbcff6612013-08-27 10:49:00 +020015#include <linux/of_address.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010016#include <linux/io.h>
John Williams892ee922009-07-29 22:08:40 +100017#include <linux/bug.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010018
Michal Simekbcff6612013-08-27 10:49:00 +020019static void __iomem *intc_baseaddr;
Michal Simekeedbdab2009-03-27 14:25:49 +010020
Michal Simekeedbdab2009-03-27 14:25:49 +010021/* No one else should require these constants, so define them locally here. */
22#define ISR 0x00 /* Interrupt Status Register */
23#define IPR 0x04 /* Interrupt Pending Register */
24#define IER 0x08 /* Interrupt Enable Register */
25#define IAR 0x0c /* Interrupt Acknowledge Register */
26#define SIE 0x10 /* Set Interrupt Enable bits */
27#define CIE 0x14 /* Clear Interrupt Enable bits */
28#define IVR 0x18 /* Interrupt Vector Register */
29#define MER 0x1c /* Master Enable Register */
30
31#define MER_ME (1<<0)
32#define MER_HIE (1<<1)
33
Michal Simek1aa12432014-02-24 14:56:32 +010034static unsigned int (*read_fn)(void __iomem *);
35static void (*write_fn)(u32, void __iomem *);
36
37static void intc_write32(u32 val, void __iomem *addr)
38{
39 iowrite32(val, addr);
40}
41
42static unsigned int intc_read32(void __iomem *addr)
43{
44 return ioread32(addr);
45}
46
47static void intc_write32_be(u32 val, void __iomem *addr)
48{
49 iowrite32be(val, addr);
50}
51
52static unsigned int intc_read32_be(void __iomem *addr)
53{
54 return ioread32be(addr);
55}
56
Thomas Gleixner6f205a42011-02-06 19:36:30 +000057static void intc_enable_or_unmask(struct irq_data *d)
Michal Simekeedbdab2009-03-27 14:25:49 +010058{
Michal Simek6c7a2672011-12-09 10:45:20 +010059 unsigned long mask = 1 << d->hwirq;
60
61 pr_debug("enable_or_unmask: %ld\n", d->hwirq);
steve@digidescorp.com33d9ff52009-11-17 08:43:39 -060062
63 /* ack level irqs because they can't be acked during
64 * ack function since the handle_level_irq function
65 * acks the irq before calling the interrupt handler
66 */
Thomas Gleixner4adc1922011-03-24 14:52:04 +010067 if (irqd_is_level_type(d))
Michal Simek1aa12432014-02-24 14:56:32 +010068 write_fn(mask, intc_baseaddr + IAR);
Michal Simek7958a682012-11-05 11:51:13 +010069
Michal Simek1aa12432014-02-24 14:56:32 +010070 write_fn(mask, intc_baseaddr + SIE);
Michal Simekeedbdab2009-03-27 14:25:49 +010071}
72
Thomas Gleixner6f205a42011-02-06 19:36:30 +000073static void intc_disable_or_mask(struct irq_data *d)
Michal Simekeedbdab2009-03-27 14:25:49 +010074{
Michal Simek6c7a2672011-12-09 10:45:20 +010075 pr_debug("disable: %ld\n", d->hwirq);
Michal Simek1aa12432014-02-24 14:56:32 +010076 write_fn(1 << d->hwirq, intc_baseaddr + CIE);
Michal Simekeedbdab2009-03-27 14:25:49 +010077}
78
Thomas Gleixner6f205a42011-02-06 19:36:30 +000079static void intc_ack(struct irq_data *d)
Michal Simekeedbdab2009-03-27 14:25:49 +010080{
Michal Simek6c7a2672011-12-09 10:45:20 +010081 pr_debug("ack: %ld\n", d->hwirq);
Michal Simek1aa12432014-02-24 14:56:32 +010082 write_fn(1 << d->hwirq, intc_baseaddr + IAR);
Michal Simekeedbdab2009-03-27 14:25:49 +010083}
84
Thomas Gleixner6f205a42011-02-06 19:36:30 +000085static void intc_mask_ack(struct irq_data *d)
Michal Simekeedbdab2009-03-27 14:25:49 +010086{
Michal Simek6c7a2672011-12-09 10:45:20 +010087 unsigned long mask = 1 << d->hwirq;
88
89 pr_debug("disable_and_ack: %ld\n", d->hwirq);
Michal Simek1aa12432014-02-24 14:56:32 +010090 write_fn(mask, intc_baseaddr + CIE);
91 write_fn(mask, intc_baseaddr + IAR);
Michal Simekeedbdab2009-03-27 14:25:49 +010092}
93
Michal Simekeedbdab2009-03-27 14:25:49 +010094static struct irq_chip intc_dev = {
95 .name = "Xilinx INTC",
Thomas Gleixner6f205a42011-02-06 19:36:30 +000096 .irq_unmask = intc_enable_or_unmask,
97 .irq_mask = intc_disable_or_mask,
98 .irq_ack = intc_ack,
99 .irq_mask_ack = intc_mask_ack,
Michal Simekeedbdab2009-03-27 14:25:49 +0100100};
101
Grant Likely2462bac2012-01-26 14:10:13 -0700102static struct irq_domain *root_domain;
Michal Simekeedbdab2009-03-27 14:25:49 +0100103
Grant Likely2462bac2012-01-26 14:10:13 -0700104unsigned int get_irq(void)
105{
106 unsigned int hwirq, irq = -1;
107
Michal Simek1aa12432014-02-24 14:56:32 +0100108 hwirq = read_fn(intc_baseaddr + IVR);
Grant Likely2462bac2012-01-26 14:10:13 -0700109 if (hwirq != -1U)
110 irq = irq_find_mapping(root_domain, hwirq);
111
112 pr_debug("get_irq: hwirq=%d, irq=%d\n", hwirq, irq);
Michal Simekeedbdab2009-03-27 14:25:49 +0100113
114 return irq;
115}
116
Michal Simekc0d997f2012-12-13 17:30:05 +0100117static int xintc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
Grant Likely2462bac2012-01-26 14:10:13 -0700118{
119 u32 intr_mask = (u32)d->host_data;
120
121 if (intr_mask & (1 << hw)) {
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
133static const struct irq_domain_ops xintc_irq_domain_ops = {
134 .xlate = irq_domain_xlate_onetwocell,
135 .map = xintc_map,
136};
137
Michal Simek8a9e90a2013-08-27 10:49:00 +0200138static int __init xilinx_intc_of_init(struct device_node *intc,
139 struct device_node *parent)
Michal Simekeedbdab2009-03-27 14:25:49 +0100140{
Grant Likely2462bac2012-01-26 14:10:13 -0700141 u32 nr_irq, intr_mask;
Michal Simekbcff6612013-08-27 10:49:00 +0200142 int ret;
Michal Simekeedbdab2009-03-27 14:25:49 +0100143
Michal Simekbcff6612013-08-27 10:49:00 +0200144 intc_baseaddr = of_iomap(intc, 0);
145 BUG_ON(!intc_baseaddr);
Michal Simekeedbdab2009-03-27 14:25:49 +0100146
Michal Simekbcff6612013-08-27 10:49:00 +0200147 ret = of_property_read_u32(intc, "xlnx,num-intr-inputs", &nr_irq);
148 if (ret < 0) {
149 pr_err("%s: unable to read xlnx,num-intr-inputs\n", __func__);
Soren Brinkmann2c80a072014-12-19 10:21:04 -0800150 return ret;
Michal Simekbcff6612013-08-27 10:49:00 +0200151 }
152
153 ret = of_property_read_u32(intc, "xlnx,kind-of-intr", &intr_mask);
154 if (ret < 0) {
155 pr_err("%s: unable to read xlnx,kind-of-intr\n", __func__);
Soren Brinkmann2c80a072014-12-19 10:21:04 -0800156 return ret;
Michal Simekbcff6612013-08-27 10:49:00 +0200157 }
158
Soren Brinkmannd50466c2014-12-19 10:21:05 -0800159 if (intr_mask >> nr_irq)
Soren Brinkmann231856a2014-12-19 10:21:06 -0800160 pr_warn("%s: mismatch in kind-of-intr param\n", __func__);
Michal Simekeedbdab2009-03-27 14:25:49 +0100161
Michal Simekbcff6612013-08-27 10:49:00 +0200162 pr_info("%s: num_irq=%d, edge=0x%x\n",
163 intc->full_name, nr_irq, intr_mask);
Michal Simekeedbdab2009-03-27 14:25:49 +0100164
Michal Simek1aa12432014-02-24 14:56:32 +0100165 write_fn = intc_write32;
166 read_fn = intc_read32;
167
Michal Simekeedbdab2009-03-27 14:25:49 +0100168 /*
169 * Disable all external interrupts until they are
170 * explicity requested.
171 */
Michal Simek1aa12432014-02-24 14:56:32 +0100172 write_fn(0, intc_baseaddr + IER);
Michal Simekeedbdab2009-03-27 14:25:49 +0100173
174 /* Acknowledge any pending interrupts just in case. */
Michal Simek1aa12432014-02-24 14:56:32 +0100175 write_fn(0xffffffff, intc_baseaddr + IAR);
Michal Simekeedbdab2009-03-27 14:25:49 +0100176
177 /* Turn on the Master Enable. */
Michal Simek1aa12432014-02-24 14:56:32 +0100178 write_fn(MER_HIE | MER_ME, intc_baseaddr + MER);
179 if (!(read_fn(intc_baseaddr + MER) & (MER_HIE | MER_ME))) {
180 write_fn = intc_write32_be;
181 read_fn = intc_read32_be;
182 write_fn(MER_HIE | MER_ME, intc_baseaddr + MER);
183 }
Michal Simekeedbdab2009-03-27 14:25:49 +0100184
Grant Likely2462bac2012-01-26 14:10:13 -0700185 /* Yeah, okay, casting the intr_mask to a void* is butt-ugly, but I'm
186 * lazy and Michal can clean it up to something nicer when he tests
187 * and commits this patch. ~~gcl */
188 root_domain = irq_domain_add_linear(intc, nr_irq, &xintc_irq_domain_ops,
189 (void *)intr_mask);
Dan Christensen7c2c8512013-03-17 04:48:56 -0500190
191 irq_set_default_host(root_domain);
Michal Simek8a9e90a2013-08-27 10:49:00 +0200192
193 return 0;
Michal Simekeedbdab2009-03-27 14:25:49 +0100194}
Michal Simek8a9e90a2013-08-27 10:49:00 +0200195
196IRQCHIP_DECLARE(xilinx_intc, "xlnx,xps-intc-1.00.a", xilinx_intc_of_init);