blob: d5970f5a1e8d5ea8010c8f243853ec3a28d6bb30 [file] [log] [blame]
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +02001/*
2 * Marvell Armada 370 and Armada XP SoC IRQ handling
3 *
4 * Copyright (C) 2012 Marvell
5 *
6 * Lior Amsalem <alior@marvell.com>
7 * Gregory CLEMENT <gregory.clement@free-electrons.com>
8 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
9 * Ben Dooks <ben.dooks@codethink.co.uk>
10 *
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/irq.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/of_address.h>
23#include <linux/of_irq.h>
24#include <linux/irqdomain.h>
25#include <asm/mach/arch.h>
26#include <asm/exception.h>
Gregory CLEMENT344e8732012-08-02 11:19:12 +030027#include <asm/smp_plat.h>
Gregory CLEMENTd792b1e2012-09-26 18:02:48 +020028#include <asm/hardware/cache-l2x0.h>
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020029
30/* Interrupt Controller Registers Map */
31#define ARMADA_370_XP_INT_SET_MASK_OFFS (0x48)
32#define ARMADA_370_XP_INT_CLEAR_MASK_OFFS (0x4C)
33
Ben Dooksf3e16cc2012-06-04 18:50:12 +020034#define ARMADA_370_XP_INT_CONTROL (0x00)
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020035#define ARMADA_370_XP_INT_SET_ENABLE_OFFS (0x30)
36#define ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS (0x34)
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010037#define ARMADA_370_XP_INT_SOURCE_CTL(irq) (0x100 + irq*4)
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020038
39#define ARMADA_370_XP_CPU_INTACK_OFFS (0x44)
40
Gregory CLEMENT344e8732012-08-02 11:19:12 +030041#define ARMADA_370_XP_SW_TRIG_INT_OFFS (0x4)
42#define ARMADA_370_XP_IN_DRBEL_MSK_OFFS (0xc)
43#define ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS (0x8)
44
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010045#define ARMADA_370_XP_MAX_PER_CPU_IRQS (28)
46
Gregory CLEMENT7f23f622013-03-20 16:09:35 +010047#define ARMADA_370_XP_TIMER0_PER_CPU_IRQ (5)
48
Gregory CLEMENT344e8732012-08-02 11:19:12 +030049#define ACTIVE_DOORBELLS (8)
50
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010051static DEFINE_RAW_SPINLOCK(irq_controller_lock);
52
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020053static void __iomem *per_cpu_int_base;
54static void __iomem *main_int_base;
55static struct irq_domain *armada_370_xp_mpic_domain;
56
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010057/*
58 * In SMP mode:
59 * For shared global interrupts, mask/unmask global enable bit
60 * For CPU interrtups, mask/unmask the calling CPU's bit
61 */
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020062static void armada_370_xp_irq_mask(struct irq_data *d)
63{
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010064 irq_hw_number_t hwirq = irqd_to_hwirq(d);
65
Gregory CLEMENT7f23f622013-03-20 16:09:35 +010066 if (hwirq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010067 writel(hwirq, main_int_base +
68 ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS);
69 else
70 writel(hwirq, per_cpu_int_base +
71 ARMADA_370_XP_INT_SET_MASK_OFFS);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020072}
73
74static void armada_370_xp_irq_unmask(struct irq_data *d)
75{
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010076 irq_hw_number_t hwirq = irqd_to_hwirq(d);
77
Gregory CLEMENT7f23f622013-03-20 16:09:35 +010078 if (hwirq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010079 writel(hwirq, main_int_base +
80 ARMADA_370_XP_INT_SET_ENABLE_OFFS);
81 else
82 writel(hwirq, per_cpu_int_base +
83 ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020084}
85
Gregory CLEMENT344e8732012-08-02 11:19:12 +030086#ifdef CONFIG_SMP
87static int armada_xp_set_affinity(struct irq_data *d,
88 const struct cpumask *mask_val, bool force)
89{
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010090 unsigned long reg;
91 unsigned long new_mask = 0;
92 unsigned long online_mask = 0;
93 unsigned long count = 0;
94 irq_hw_number_t hwirq = irqd_to_hwirq(d);
95 int cpu;
96
97 for_each_cpu(cpu, mask_val) {
98 new_mask |= 1 << cpu_logical_map(cpu);
99 count++;
100 }
101
102 /*
103 * Forbid mutlicore interrupt affinity
104 * This is required since the MPIC HW doesn't limit
105 * several CPUs from acknowledging the same interrupt.
106 */
107 if (count > 1)
108 return -EINVAL;
109
110 for_each_cpu(cpu, cpu_online_mask)
111 online_mask |= 1 << cpu_logical_map(cpu);
112
113 raw_spin_lock(&irq_controller_lock);
114
115 reg = readl(main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
116 reg = (reg & (~online_mask)) | new_mask;
117 writel(reg, main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
118
119 raw_spin_unlock(&irq_controller_lock);
120
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300121 return 0;
122}
123#endif
124
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200125static struct irq_chip armada_370_xp_irq_chip = {
126 .name = "armada_370_xp_irq",
127 .irq_mask = armada_370_xp_irq_mask,
128 .irq_mask_ack = armada_370_xp_irq_mask,
129 .irq_unmask = armada_370_xp_irq_unmask,
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300130#ifdef CONFIG_SMP
131 .irq_set_affinity = armada_xp_set_affinity,
132#endif
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200133};
134
135static int armada_370_xp_mpic_irq_map(struct irq_domain *h,
136 unsigned int virq, irq_hw_number_t hw)
137{
138 armada_370_xp_irq_mask(irq_get_irq_data(virq));
Gregory CLEMENT600468d2013-04-05 14:32:52 +0200139 if (hw != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
140 writel(hw, per_cpu_int_base +
141 ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
142 else
143 writel(hw, main_int_base + ARMADA_370_XP_INT_SET_ENABLE_OFFS);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200144 irq_set_status_flags(virq, IRQ_LEVEL);
Gregory CLEMENT3a6f08a2013-01-25 18:32:41 +0100145
Gregory CLEMENT7f23f622013-03-20 16:09:35 +0100146 if (hw == ARMADA_370_XP_TIMER0_PER_CPU_IRQ) {
Gregory CLEMENT3a6f08a2013-01-25 18:32:41 +0100147 irq_set_percpu_devid(virq);
148 irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
149 handle_percpu_devid_irq);
150
151 } else {
152 irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
153 handle_level_irq);
154 }
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200155 set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
156
157 return 0;
158}
159
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300160#ifdef CONFIG_SMP
161void armada_mpic_send_doorbell(const struct cpumask *mask, unsigned int irq)
162{
163 int cpu;
164 unsigned long map = 0;
165
166 /* Convert our logical CPU mask into a physical one. */
167 for_each_cpu(cpu, mask)
168 map |= 1 << cpu_logical_map(cpu);
169
170 /*
171 * Ensure that stores to Normal memory are visible to the
172 * other CPUs before issuing the IPI.
173 */
174 dsb();
175
176 /* submit softirq */
177 writel((map << 8) | irq, main_int_base +
178 ARMADA_370_XP_SW_TRIG_INT_OFFS);
179}
180
181void armada_xp_mpic_smp_cpu_init(void)
182{
183 /* Clear pending IPIs */
184 writel(0, per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
185
186 /* Enable first 8 IPIs */
187 writel((1 << ACTIVE_DOORBELLS) - 1, per_cpu_int_base +
188 ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
189
190 /* Unmask IPI interrupt */
191 writel(0, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
192}
193#endif /* CONFIG_SMP */
194
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200195static struct irq_domain_ops armada_370_xp_mpic_irq_ops = {
196 .map = armada_370_xp_mpic_irq_map,
197 .xlate = irq_domain_xlate_onecell,
198};
199
200static int __init armada_370_xp_mpic_of_init(struct device_node *node,
201 struct device_node *parent)
202{
Ben Dooksf3e16cc2012-06-04 18:50:12 +0200203 u32 control;
204
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200205 main_int_base = of_iomap(node, 0);
206 per_cpu_int_base = of_iomap(node, 1);
207
208 BUG_ON(!main_int_base);
209 BUG_ON(!per_cpu_int_base);
210
Ben Dooksf3e16cc2012-06-04 18:50:12 +0200211 control = readl(main_int_base + ARMADA_370_XP_INT_CONTROL);
212
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200213 armada_370_xp_mpic_domain =
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300214 irq_domain_add_linear(node, (control >> 2) & 0x3ff,
215 &armada_370_xp_mpic_irq_ops, NULL);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200216
217 if (!armada_370_xp_mpic_domain)
218 panic("Unable to add Armada_370_Xp MPIC irq domain (DT)\n");
219
220 irq_set_default_host(armada_370_xp_mpic_domain);
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300221
222#ifdef CONFIG_SMP
223 armada_xp_mpic_smp_cpu_init();
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100224
225 /*
226 * Set the default affinity from all CPUs to the boot cpu.
227 * This is required since the MPIC doesn't limit several CPUs
228 * from acknowledging the same interrupt.
229 */
230 cpumask_clear(irq_default_affinity);
231 cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
232
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300233#endif
234
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200235 return 0;
236}
237
238asmlinkage void __exception_irq_entry armada_370_xp_handle_irq(struct pt_regs
239 *regs)
240{
241 u32 irqstat, irqnr;
242
243 do {
244 irqstat = readl_relaxed(per_cpu_int_base +
245 ARMADA_370_XP_CPU_INTACK_OFFS);
246 irqnr = irqstat & 0x3FF;
247
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300248 if (irqnr > 1022)
249 break;
250
Gregory CLEMENT3a6f08a2013-01-25 18:32:41 +0100251 if (irqnr > 0) {
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300252 irqnr = irq_find_mapping(armada_370_xp_mpic_domain,
253 irqnr);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200254 handle_IRQ(irqnr, regs);
255 continue;
256 }
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300257#ifdef CONFIG_SMP
258 /* IPI Handling */
259 if (irqnr == 0) {
260 u32 ipimask, ipinr;
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200261
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300262 ipimask = readl_relaxed(per_cpu_int_base +
263 ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS)
264 & 0xFF;
265
266 writel(0x0, per_cpu_int_base +
267 ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
268
269 /* Handle all pending doorbells */
270 for (ipinr = 0; ipinr < ACTIVE_DOORBELLS; ipinr++) {
271 if (ipimask & (0x1 << ipinr))
272 handle_IPI(ipinr, regs);
273 }
274 continue;
275 }
276#endif
277
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200278 } while (1);
279}
280
281static const struct of_device_id mpic_of_match[] __initconst = {
282 {.compatible = "marvell,mpic", .data = armada_370_xp_mpic_of_init},
283 {},
284};
285
286void __init armada_370_xp_init_irq(void)
287{
288 of_irq_init(mpic_of_match);
Gregory CLEMENTd792b1e2012-09-26 18:02:48 +0200289#ifdef CONFIG_CACHE_L2X0
290 l2x0_of_init(0, ~0UL);
291#endif
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200292}