blob: d20a832ba86b33c4a182a5dc2c0cb866fb1254e9 [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>
Thomas Petazzoni9339d432013-04-09 23:26:15 +020028#include <asm/mach/irq.h>
29
30#include "irqchip.h"
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020031
32/* Interrupt Controller Registers Map */
33#define ARMADA_370_XP_INT_SET_MASK_OFFS (0x48)
34#define ARMADA_370_XP_INT_CLEAR_MASK_OFFS (0x4C)
35
Ben Dooksf3e16cc2012-06-04 18:50:12 +020036#define ARMADA_370_XP_INT_CONTROL (0x00)
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020037#define ARMADA_370_XP_INT_SET_ENABLE_OFFS (0x30)
38#define ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS (0x34)
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010039#define ARMADA_370_XP_INT_SOURCE_CTL(irq) (0x100 + irq*4)
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020040
41#define ARMADA_370_XP_CPU_INTACK_OFFS (0x44)
42
Gregory CLEMENT344e8732012-08-02 11:19:12 +030043#define ARMADA_370_XP_SW_TRIG_INT_OFFS (0x4)
44#define ARMADA_370_XP_IN_DRBEL_MSK_OFFS (0xc)
45#define ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS (0x8)
46
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010047#define ARMADA_370_XP_MAX_PER_CPU_IRQS (28)
48
Gregory CLEMENT7f23f622013-03-20 16:09:35 +010049#define ARMADA_370_XP_TIMER0_PER_CPU_IRQ (5)
50
Gregory CLEMENT344e8732012-08-02 11:19:12 +030051#define ACTIVE_DOORBELLS (8)
52
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010053static DEFINE_RAW_SPINLOCK(irq_controller_lock);
54
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020055static void __iomem *per_cpu_int_base;
56static void __iomem *main_int_base;
57static struct irq_domain *armada_370_xp_mpic_domain;
58
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010059/*
60 * In SMP mode:
61 * For shared global interrupts, mask/unmask global enable bit
62 * For CPU interrtups, mask/unmask the calling CPU's bit
63 */
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020064static void armada_370_xp_irq_mask(struct irq_data *d)
65{
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010066#ifdef CONFIG_SMP
67 irq_hw_number_t hwirq = irqd_to_hwirq(d);
68
Gregory CLEMENT7f23f622013-03-20 16:09:35 +010069 if (hwirq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010070 writel(hwirq, main_int_base +
71 ARMADA_370_XP_INT_CLEAR_ENABLE_OFFS);
72 else
73 writel(hwirq, per_cpu_int_base +
74 ARMADA_370_XP_INT_SET_MASK_OFFS);
75#else
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020076 writel(irqd_to_hwirq(d),
77 per_cpu_int_base + ARMADA_370_XP_INT_SET_MASK_OFFS);
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010078#endif
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020079}
80
81static void armada_370_xp_irq_unmask(struct irq_data *d)
82{
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010083#ifdef CONFIG_SMP
84 irq_hw_number_t hwirq = irqd_to_hwirq(d);
85
Gregory CLEMENT7f23f622013-03-20 16:09:35 +010086 if (hwirq != ARMADA_370_XP_TIMER0_PER_CPU_IRQ)
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010087 writel(hwirq, main_int_base +
88 ARMADA_370_XP_INT_SET_ENABLE_OFFS);
89 else
90 writel(hwirq, per_cpu_int_base +
91 ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
92#else
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020093 writel(irqd_to_hwirq(d),
94 per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
Gregory CLEMENT3202bf02012-12-05 21:43:23 +010095#endif
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +020096}
97
Gregory CLEMENT344e8732012-08-02 11:19:12 +030098#ifdef CONFIG_SMP
99static int armada_xp_set_affinity(struct irq_data *d,
100 const struct cpumask *mask_val, bool force)
101{
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100102 unsigned long reg;
103 unsigned long new_mask = 0;
104 unsigned long online_mask = 0;
105 unsigned long count = 0;
106 irq_hw_number_t hwirq = irqd_to_hwirq(d);
107 int cpu;
108
109 for_each_cpu(cpu, mask_val) {
110 new_mask |= 1 << cpu_logical_map(cpu);
111 count++;
112 }
113
114 /*
115 * Forbid mutlicore interrupt affinity
116 * This is required since the MPIC HW doesn't limit
117 * several CPUs from acknowledging the same interrupt.
118 */
119 if (count > 1)
120 return -EINVAL;
121
122 for_each_cpu(cpu, cpu_online_mask)
123 online_mask |= 1 << cpu_logical_map(cpu);
124
125 raw_spin_lock(&irq_controller_lock);
126
127 reg = readl(main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
128 reg = (reg & (~online_mask)) | new_mask;
129 writel(reg, main_int_base + ARMADA_370_XP_INT_SOURCE_CTL(hwirq));
130
131 raw_spin_unlock(&irq_controller_lock);
132
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300133 return 0;
134}
135#endif
136
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200137static struct irq_chip armada_370_xp_irq_chip = {
138 .name = "armada_370_xp_irq",
139 .irq_mask = armada_370_xp_irq_mask,
140 .irq_mask_ack = armada_370_xp_irq_mask,
141 .irq_unmask = armada_370_xp_irq_unmask,
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300142#ifdef CONFIG_SMP
143 .irq_set_affinity = armada_xp_set_affinity,
144#endif
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200145};
146
147static int armada_370_xp_mpic_irq_map(struct irq_domain *h,
148 unsigned int virq, irq_hw_number_t hw)
149{
150 armada_370_xp_irq_mask(irq_get_irq_data(virq));
151 writel(hw, main_int_base + ARMADA_370_XP_INT_SET_ENABLE_OFFS);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200152 irq_set_status_flags(virq, IRQ_LEVEL);
Gregory CLEMENT3a6f08a2013-01-25 18:32:41 +0100153
Gregory CLEMENT7f23f622013-03-20 16:09:35 +0100154 if (hw == ARMADA_370_XP_TIMER0_PER_CPU_IRQ) {
Gregory CLEMENT3a6f08a2013-01-25 18:32:41 +0100155 irq_set_percpu_devid(virq);
156 irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
157 handle_percpu_devid_irq);
158
159 } else {
160 irq_set_chip_and_handler(virq, &armada_370_xp_irq_chip,
161 handle_level_irq);
162 }
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200163 set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
164
165 return 0;
166}
167
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300168#ifdef CONFIG_SMP
169void armada_mpic_send_doorbell(const struct cpumask *mask, unsigned int irq)
170{
171 int cpu;
172 unsigned long map = 0;
173
174 /* Convert our logical CPU mask into a physical one. */
175 for_each_cpu(cpu, mask)
176 map |= 1 << cpu_logical_map(cpu);
177
178 /*
179 * Ensure that stores to Normal memory are visible to the
180 * other CPUs before issuing the IPI.
181 */
182 dsb();
183
184 /* submit softirq */
185 writel((map << 8) | irq, main_int_base +
186 ARMADA_370_XP_SW_TRIG_INT_OFFS);
187}
188
189void armada_xp_mpic_smp_cpu_init(void)
190{
191 /* Clear pending IPIs */
192 writel(0, per_cpu_int_base + ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
193
194 /* Enable first 8 IPIs */
195 writel((1 << ACTIVE_DOORBELLS) - 1, per_cpu_int_base +
196 ARMADA_370_XP_IN_DRBEL_MSK_OFFS);
197
198 /* Unmask IPI interrupt */
199 writel(0, per_cpu_int_base + ARMADA_370_XP_INT_CLEAR_MASK_OFFS);
200}
201#endif /* CONFIG_SMP */
202
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200203static struct irq_domain_ops armada_370_xp_mpic_irq_ops = {
204 .map = armada_370_xp_mpic_irq_map,
205 .xlate = irq_domain_xlate_onecell,
206};
207
Thomas Petazzoni9339d432013-04-09 23:26:15 +0200208static asmlinkage void __exception_irq_entry
209armada_370_xp_handle_irq(struct pt_regs *regs);
210
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200211static int __init armada_370_xp_mpic_of_init(struct device_node *node,
212 struct device_node *parent)
213{
Ben Dooksf3e16cc2012-06-04 18:50:12 +0200214 u32 control;
215
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200216 main_int_base = of_iomap(node, 0);
217 per_cpu_int_base = of_iomap(node, 1);
218
219 BUG_ON(!main_int_base);
220 BUG_ON(!per_cpu_int_base);
221
Ben Dooksf3e16cc2012-06-04 18:50:12 +0200222 control = readl(main_int_base + ARMADA_370_XP_INT_CONTROL);
223
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200224 armada_370_xp_mpic_domain =
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300225 irq_domain_add_linear(node, (control >> 2) & 0x3ff,
226 &armada_370_xp_mpic_irq_ops, NULL);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200227
228 if (!armada_370_xp_mpic_domain)
229 panic("Unable to add Armada_370_Xp MPIC irq domain (DT)\n");
230
231 irq_set_default_host(armada_370_xp_mpic_domain);
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300232
233#ifdef CONFIG_SMP
234 armada_xp_mpic_smp_cpu_init();
Gregory CLEMENT3202bf02012-12-05 21:43:23 +0100235
236 /*
237 * Set the default affinity from all CPUs to the boot cpu.
238 * This is required since the MPIC doesn't limit several CPUs
239 * from acknowledging the same interrupt.
240 */
241 cpumask_clear(irq_default_affinity);
242 cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
243
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300244#endif
245
Thomas Petazzoni9339d432013-04-09 23:26:15 +0200246 set_handle_irq(armada_370_xp_handle_irq);
247
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200248 return 0;
249}
250
Thomas Petazzoni9339d432013-04-09 23:26:15 +0200251static asmlinkage void __exception_irq_entry
252armada_370_xp_handle_irq(struct pt_regs *regs)
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200253{
254 u32 irqstat, irqnr;
255
256 do {
257 irqstat = readl_relaxed(per_cpu_int_base +
258 ARMADA_370_XP_CPU_INTACK_OFFS);
259 irqnr = irqstat & 0x3FF;
260
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300261 if (irqnr > 1022)
262 break;
263
Gregory CLEMENT3a6f08a2013-01-25 18:32:41 +0100264 if (irqnr > 0) {
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300265 irqnr = irq_find_mapping(armada_370_xp_mpic_domain,
266 irqnr);
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200267 handle_IRQ(irqnr, regs);
268 continue;
269 }
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300270#ifdef CONFIG_SMP
271 /* IPI Handling */
272 if (irqnr == 0) {
273 u32 ipimask, ipinr;
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200274
Gregory CLEMENT344e8732012-08-02 11:19:12 +0300275 ipimask = readl_relaxed(per_cpu_int_base +
276 ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS)
277 & 0xFF;
278
279 writel(0x0, per_cpu_int_base +
280 ARMADA_370_XP_IN_DRBEL_CAUSE_OFFS);
281
282 /* Handle all pending doorbells */
283 for (ipinr = 0; ipinr < ACTIVE_DOORBELLS; ipinr++) {
284 if (ipimask & (0x1 << ipinr))
285 handle_IPI(ipinr, regs);
286 }
287 continue;
288 }
289#endif
290
Thomas Petazzoni9ae6f742012-06-13 19:01:28 +0200291 } while (1);
292}
293
Thomas Petazzoni9339d432013-04-09 23:26:15 +0200294IRQCHIP_DECLARE(armada_370_xp_mpic, "marvell,mpic", armada_370_xp_mpic_of_init);