blob: 01ae73088578f57ed03f3f265728946f1ca9a90e [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>
Michal Simekbcff6612013-08-27 10:49:00 +020014#include <linux/of_address.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010015#include <linux/io.h>
John Williams892ee922009-07-29 22:08:40 +100016#include <linux/bug.h>
Michal Simekeedbdab2009-03-27 14:25:49 +010017
Michal Simek8a9e90a2013-08-27 10:49:00 +020018#include "../../drivers/irqchip/irqchip.h"
Michal Simekeedbdab2009-03-27 14:25:49 +010019
Michal Simekbcff6612013-08-27 10:49:00 +020020static void __iomem *intc_baseaddr;
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
Michal Simek1aa12432014-02-24 14:56:32 +010035static unsigned int (*read_fn)(void __iomem *);
36static void (*write_fn)(u32, void __iomem *);
37
38static void intc_write32(u32 val, void __iomem *addr)
39{
40 iowrite32(val, addr);
41}
42
43static unsigned int intc_read32(void __iomem *addr)
44{
45 return ioread32(addr);
46}
47
48static void intc_write32_be(u32 val, void __iomem *addr)
49{
50 iowrite32be(val, addr);
51}
52
53static unsigned int intc_read32_be(void __iomem *addr)
54{
55 return ioread32be(addr);
56}
57
Thomas Gleixner6f205a42011-02-06 19:36:30 +000058static void intc_enable_or_unmask(struct irq_data *d)
Michal Simekeedbdab2009-03-27 14:25:49 +010059{
Michal Simek6c7a2672011-12-09 10:45:20 +010060 unsigned long mask = 1 << d->hwirq;
61
62 pr_debug("enable_or_unmask: %ld\n", d->hwirq);
steve@digidescorp.com33d9ff52009-11-17 08:43:39 -060063
64 /* ack level irqs because they can't be acked during
65 * ack function since the handle_level_irq function
66 * acks the irq before calling the interrupt handler
67 */
Thomas Gleixner4adc1922011-03-24 14:52:04 +010068 if (irqd_is_level_type(d))
Michal Simek1aa12432014-02-24 14:56:32 +010069 write_fn(mask, intc_baseaddr + IAR);
Michal Simek7958a682012-11-05 11:51:13 +010070
Michal Simek1aa12432014-02-24 14:56:32 +010071 write_fn(mask, intc_baseaddr + SIE);
Michal Simekeedbdab2009-03-27 14:25:49 +010072}
73
Thomas Gleixner6f205a42011-02-06 19:36:30 +000074static void intc_disable_or_mask(struct irq_data *d)
Michal Simekeedbdab2009-03-27 14:25:49 +010075{
Michal Simek6c7a2672011-12-09 10:45:20 +010076 pr_debug("disable: %ld\n", d->hwirq);
Michal Simek1aa12432014-02-24 14:56:32 +010077 write_fn(1 << d->hwirq, intc_baseaddr + CIE);
Michal Simekeedbdab2009-03-27 14:25:49 +010078}
79
Thomas Gleixner6f205a42011-02-06 19:36:30 +000080static void intc_ack(struct irq_data *d)
Michal Simekeedbdab2009-03-27 14:25:49 +010081{
Michal Simek6c7a2672011-12-09 10:45:20 +010082 pr_debug("ack: %ld\n", d->hwirq);
Michal Simek1aa12432014-02-24 14:56:32 +010083 write_fn(1 << d->hwirq, intc_baseaddr + IAR);
Michal Simekeedbdab2009-03-27 14:25:49 +010084}
85
Thomas Gleixner6f205a42011-02-06 19:36:30 +000086static void intc_mask_ack(struct irq_data *d)
Michal Simekeedbdab2009-03-27 14:25:49 +010087{
Michal Simek6c7a2672011-12-09 10:45:20 +010088 unsigned long mask = 1 << d->hwirq;
89
90 pr_debug("disable_and_ack: %ld\n", d->hwirq);
Michal Simek1aa12432014-02-24 14:56:32 +010091 write_fn(mask, intc_baseaddr + CIE);
92 write_fn(mask, intc_baseaddr + IAR);
Michal Simekeedbdab2009-03-27 14:25:49 +010093}
94
Michal Simekeedbdab2009-03-27 14:25:49 +010095static struct irq_chip intc_dev = {
96 .name = "Xilinx INTC",
Thomas Gleixner6f205a42011-02-06 19:36:30 +000097 .irq_unmask = intc_enable_or_unmask,
98 .irq_mask = intc_disable_or_mask,
99 .irq_ack = intc_ack,
100 .irq_mask_ack = intc_mask_ack,
Michal Simekeedbdab2009-03-27 14:25:49 +0100101};
102
Grant Likely2462bac2012-01-26 14:10:13 -0700103static struct irq_domain *root_domain;
Michal Simekeedbdab2009-03-27 14:25:49 +0100104
Grant Likely2462bac2012-01-26 14:10:13 -0700105unsigned int get_irq(void)
106{
107 unsigned int hwirq, irq = -1;
108
Michal Simek1aa12432014-02-24 14:56:32 +0100109 hwirq = read_fn(intc_baseaddr + IVR);
Grant Likely2462bac2012-01-26 14:10:13 -0700110 if (hwirq != -1U)
111 irq = irq_find_mapping(root_domain, hwirq);
112
113 pr_debug("get_irq: hwirq=%d, irq=%d\n", hwirq, irq);
Michal Simekeedbdab2009-03-27 14:25:49 +0100114
115 return irq;
116}
117
Michal Simekc0d997f2012-12-13 17:30:05 +0100118static int xintc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
Grant Likely2462bac2012-01-26 14:10:13 -0700119{
120 u32 intr_mask = (u32)d->host_data;
121
122 if (intr_mask & (1 << hw)) {
123 irq_set_chip_and_handler_name(irq, &intc_dev,
124 handle_edge_irq, "edge");
125 irq_clear_status_flags(irq, IRQ_LEVEL);
126 } else {
127 irq_set_chip_and_handler_name(irq, &intc_dev,
128 handle_level_irq, "level");
129 irq_set_status_flags(irq, IRQ_LEVEL);
130 }
131 return 0;
132}
133
134static const struct irq_domain_ops xintc_irq_domain_ops = {
135 .xlate = irq_domain_xlate_onetwocell,
136 .map = xintc_map,
137};
138
Michal Simek8a9e90a2013-08-27 10:49:00 +0200139static int __init xilinx_intc_of_init(struct device_node *intc,
140 struct device_node *parent)
Michal Simekeedbdab2009-03-27 14:25:49 +0100141{
Grant Likely2462bac2012-01-26 14:10:13 -0700142 u32 nr_irq, intr_mask;
Michal Simekbcff6612013-08-27 10:49:00 +0200143 int ret;
Michal Simekeedbdab2009-03-27 14:25:49 +0100144
Michal Simekbcff6612013-08-27 10:49:00 +0200145 intc_baseaddr = of_iomap(intc, 0);
146 BUG_ON(!intc_baseaddr);
Michal Simekeedbdab2009-03-27 14:25:49 +0100147
Michal Simekbcff6612013-08-27 10:49:00 +0200148 ret = of_property_read_u32(intc, "xlnx,num-intr-inputs", &nr_irq);
149 if (ret < 0) {
150 pr_err("%s: unable to read xlnx,num-intr-inputs\n", __func__);
Soren Brinkmann2c80a072014-12-19 10:21:04 -0800151 return ret;
Michal Simekbcff6612013-08-27 10:49:00 +0200152 }
153
154 ret = of_property_read_u32(intc, "xlnx,kind-of-intr", &intr_mask);
155 if (ret < 0) {
156 pr_err("%s: unable to read xlnx,kind-of-intr\n", __func__);
Soren Brinkmann2c80a072014-12-19 10:21:04 -0800157 return ret;
Michal Simekbcff6612013-08-27 10:49:00 +0200158 }
159
Michal Simek2ecb8992011-12-09 12:26:55 +0100160 if (intr_mask > (u32)((1ULL << nr_irq) - 1))
Michal Simek6bd55f02012-12-27 10:40:38 +0100161 pr_info(" ERROR: Mismatch in kind-of-intr param\n");
Michal Simekeedbdab2009-03-27 14:25:49 +0100162
Michal Simekbcff6612013-08-27 10:49:00 +0200163 pr_info("%s: num_irq=%d, edge=0x%x\n",
164 intc->full_name, nr_irq, intr_mask);
Michal Simekeedbdab2009-03-27 14:25:49 +0100165
Michal Simek1aa12432014-02-24 14:56:32 +0100166 write_fn = intc_write32;
167 read_fn = intc_read32;
168
Michal Simekeedbdab2009-03-27 14:25:49 +0100169 /*
170 * Disable all external interrupts until they are
171 * explicity requested.
172 */
Michal Simek1aa12432014-02-24 14:56:32 +0100173 write_fn(0, intc_baseaddr + IER);
Michal Simekeedbdab2009-03-27 14:25:49 +0100174
175 /* Acknowledge any pending interrupts just in case. */
Michal Simek1aa12432014-02-24 14:56:32 +0100176 write_fn(0xffffffff, intc_baseaddr + IAR);
Michal Simekeedbdab2009-03-27 14:25:49 +0100177
178 /* Turn on the Master Enable. */
Michal Simek1aa12432014-02-24 14:56:32 +0100179 write_fn(MER_HIE | MER_ME, intc_baseaddr + MER);
180 if (!(read_fn(intc_baseaddr + MER) & (MER_HIE | MER_ME))) {
181 write_fn = intc_write32_be;
182 read_fn = intc_read32_be;
183 write_fn(MER_HIE | MER_ME, intc_baseaddr + MER);
184 }
Michal Simekeedbdab2009-03-27 14:25:49 +0100185
Grant Likely2462bac2012-01-26 14:10:13 -0700186 /* Yeah, okay, casting the intr_mask to a void* is butt-ugly, but I'm
187 * lazy and Michal can clean it up to something nicer when he tests
188 * and commits this patch. ~~gcl */
189 root_domain = irq_domain_add_linear(intc, nr_irq, &xintc_irq_domain_ops,
190 (void *)intr_mask);
Dan Christensen7c2c8512013-03-17 04:48:56 -0500191
192 irq_set_default_host(root_domain);
Michal Simek8a9e90a2013-08-27 10:49:00 +0200193
194 return 0;
Michal Simekeedbdab2009-03-27 14:25:49 +0100195}
Michal Simek8a9e90a2013-08-27 10:49:00 +0200196
197IRQCHIP_DECLARE(xilinx_intc, "xlnx,xps-intc-1.00.a", xilinx_intc_of_init);