blob: 6e70d03824a16147da20a53912a94f2cb28a3c29 [file] [log] [blame]
Russell Kingc41b16f2011-01-19 15:32:15 +00001/*
2 * Support for Versatile FPGA-based IRQ controllers
3 */
4#include <linux/irq.h>
5#include <linux/io.h>
Linus Walleij3108e6a2012-04-28 14:33:47 +01006#include <linux/irqdomain.h>
7#include <linux/module.h>
Russell Kingc41b16f2011-01-19 15:32:15 +00008
Linus Walleij3108e6a2012-04-28 14:33:47 +01009#include <asm/exception.h>
Russell Kingc41b16f2011-01-19 15:32:15 +000010#include <asm/mach/irq.h>
11#include <plat/fpga-irq.h>
12
13#define IRQ_STATUS 0x00
14#define IRQ_RAW_STATUS 0x04
15#define IRQ_ENABLE_SET 0x08
16#define IRQ_ENABLE_CLEAR 0x0c
17
Linus Walleij3108e6a2012-04-28 14:33:47 +010018/**
19 * struct fpga_irq_data - irq data container for the FPGA IRQ controller
20 * @base: memory offset in virtual memory
21 * @irq_start: first IRQ number handled by this instance
22 * @chip: chip container for this instance
23 * @domain: IRQ domain for this instance
24 * @valid: mask for valid IRQs on this controller
25 * @used_irqs: number of active IRQs on this controller
26 */
27struct fpga_irq_data {
28 void __iomem *base;
29 unsigned int irq_start;
30 struct irq_chip chip;
31 u32 valid;
32 struct irq_domain *domain;
33 u8 used_irqs;
34};
35
36/* we cannot allocate memory when the controllers are initially registered */
37static struct fpga_irq_data fpga_irq_devices[CONFIG_PLAT_VERSATILE_FPGA_IRQ_NR];
38static int fpga_irq_id;
39
Russell Kingc41b16f2011-01-19 15:32:15 +000040static void fpga_irq_mask(struct irq_data *d)
41{
42 struct fpga_irq_data *f = irq_data_get_irq_chip_data(d);
Linus Walleij3108e6a2012-04-28 14:33:47 +010043 u32 mask = 1 << d->hwirq;
Russell Kingc41b16f2011-01-19 15:32:15 +000044
45 writel(mask, f->base + IRQ_ENABLE_CLEAR);
46}
47
48static void fpga_irq_unmask(struct irq_data *d)
49{
50 struct fpga_irq_data *f = irq_data_get_irq_chip_data(d);
Linus Walleij3108e6a2012-04-28 14:33:47 +010051 u32 mask = 1 << d->hwirq;
Russell Kingc41b16f2011-01-19 15:32:15 +000052
53 writel(mask, f->base + IRQ_ENABLE_SET);
54}
55
56static void fpga_irq_handle(unsigned int irq, struct irq_desc *desc)
57{
Thomas Gleixner6845664a2011-03-24 13:25:22 +010058 struct fpga_irq_data *f = irq_desc_get_handler_data(desc);
Russell Kingc41b16f2011-01-19 15:32:15 +000059 u32 status = readl(f->base + IRQ_STATUS);
60
61 if (status == 0) {
62 do_bad_IRQ(irq, desc);
63 return;
64 }
65
66 do {
67 irq = ffs(status) - 1;
68 status &= ~(1 << irq);
Linus Walleij3108e6a2012-04-28 14:33:47 +010069 generic_handle_irq(irq_find_mapping(f->domain, irq));
Russell Kingc41b16f2011-01-19 15:32:15 +000070 } while (status);
71}
72
Linus Walleij3108e6a2012-04-28 14:33:47 +010073/*
74 * Handle each interrupt in a single FPGA IRQ controller. Returns non-zero
75 * if we've handled at least one interrupt. This does a single read of the
76 * status register and handles all interrupts in order from LSB first.
77 */
78static int handle_one_fpga(struct fpga_irq_data *f, struct pt_regs *regs)
Russell Kingc41b16f2011-01-19 15:32:15 +000079{
Linus Walleij3108e6a2012-04-28 14:33:47 +010080 int handled = 0;
81 int irq;
82 u32 status;
Russell Kingc41b16f2011-01-19 15:32:15 +000083
Linus Walleij3108e6a2012-04-28 14:33:47 +010084 while ((status = readl(f->base + IRQ_STATUS))) {
85 irq = ffs(status) - 1;
86 handle_IRQ(irq_find_mapping(f->domain, irq), regs);
87 handled = 1;
88 }
89
90 return handled;
91}
92
93/*
94 * Keep iterating over all registered FPGA IRQ controllers until there are
95 * no pending interrupts.
96 */
97asmlinkage void __exception_irq_entry fpga_handle_irq(struct pt_regs *regs)
98{
99 int i, handled;
100
101 do {
102 for (i = 0, handled = 0; i < fpga_irq_id; ++i)
103 handled |= handle_one_fpga(&fpga_irq_devices[i], regs);
104 } while (handled);
105}
106
107static int fpga_irqdomain_map(struct irq_domain *d, unsigned int irq,
108 irq_hw_number_t hwirq)
109{
110 struct fpga_irq_data *f = d->host_data;
111
112 /* Skip invalid IRQs, only register handlers for the real ones */
113 if (!(f->valid & (1 << hwirq)))
114 return -ENOTSUPP;
115 irq_set_chip_data(irq, f);
116 irq_set_chip_and_handler(irq, &f->chip,
117 handle_level_irq);
118 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
119 f->used_irqs++;
120 return 0;
121}
122
123static struct irq_domain_ops fpga_irqdomain_ops = {
124 .map = fpga_irqdomain_map,
125 .xlate = irq_domain_xlate_onetwocell,
126};
127
128void __init fpga_irq_init(void __iomem *base, const char *name, int irq_start,
129 int parent_irq, u32 valid, struct device_node *node)
130{
131 struct fpga_irq_data *f;
132
133 if (fpga_irq_id >= ARRAY_SIZE(fpga_irq_devices)) {
134 printk(KERN_ERR "%s: too few FPGA IRQ controllers, increase CONFIG_PLAT_VERSATILE_FPGA_IRQ_NR\n", __func__);
135 return;
136 }
137
138 f = &fpga_irq_devices[fpga_irq_id];
139 f->base = base;
140 f->irq_start = irq_start;
141 f->chip.name = name;
Russell Kingc41b16f2011-01-19 15:32:15 +0000142 f->chip.irq_ack = fpga_irq_mask;
143 f->chip.irq_mask = fpga_irq_mask;
144 f->chip.irq_unmask = fpga_irq_unmask;
Linus Walleij3108e6a2012-04-28 14:33:47 +0100145 f->valid = valid;
Russell Kingc41b16f2011-01-19 15:32:15 +0000146
147 if (parent_irq != -1) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100148 irq_set_handler_data(parent_irq, f);
149 irq_set_chained_handler(parent_irq, fpga_irq_handle);
Russell Kingc41b16f2011-01-19 15:32:15 +0000150 }
151
Linus Walleij3108e6a2012-04-28 14:33:47 +0100152 f->domain = irq_domain_add_legacy(node, fls(valid), f->irq_start, 0,
153 &fpga_irqdomain_ops, f);
154 pr_info("FPGA IRQ chip %d \"%s\" @ %p, %u irqs\n",
155 fpga_irq_id, name, base, f->used_irqs);
Russell Kingc41b16f2011-01-19 15:32:15 +0000156
Linus Walleij3108e6a2012-04-28 14:33:47 +0100157 fpga_irq_id++;
Russell Kingc41b16f2011-01-19 15:32:15 +0000158}