blob: 806ba0ef33c0a375d7fa6162c732d78afbf50caf [file] [log] [blame]
Guo Rend8a5f5f2018-09-16 15:57:14 +08001// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3
4#include <linux/kernel.h>
5#include <linux/init.h>
6#include <linux/of.h>
7#include <linux/of_address.h>
8#include <linux/module.h>
9#include <linux/irqdomain.h>
10#include <linux/irqchip.h>
11#include <linux/irq.h>
12#include <linux/interrupt.h>
13#include <linux/smp.h>
14#include <linux/io.h>
15#include <asm/irq.h>
16#include <asm/traps.h>
17#include <asm/reg_ops.h>
18
19static struct irq_domain *root_domain;
20static void __iomem *INTCG_base;
21static void __iomem *INTCL_base;
22
23#define IPI_IRQ 15
24#define INTC_IRQS 256
25#define COMM_IRQ_BASE 32
26
27#define INTCG_SIZE 0x8000
28#define INTCL_SIZE 0x1000
29
30#define INTCG_ICTLR 0x0
31#define INTCG_CICFGR 0x100
32#define INTCG_CIDSTR 0x1000
33
34#define INTCL_PICTLR 0x0
Guo Ren648f8352019-06-06 15:37:31 +080035#define INTCL_CFGR 0x14
Guo Rend8a5f5f2018-09-16 15:57:14 +080036#define INTCL_SIGR 0x60
Guo Rend8a5f5f2018-09-16 15:57:14 +080037#define INTCL_RDYIR 0x6c
38#define INTCL_SENR 0xa0
39#define INTCL_CENR 0xa4
40#define INTCL_CACR 0xb4
41
42static DEFINE_PER_CPU(void __iomem *, intcl_reg);
43
Guo Ren648f8352019-06-06 15:37:31 +080044static unsigned long *__trigger;
45
46#define IRQ_OFFSET(irq) ((irq < COMM_IRQ_BASE) ? irq : (irq - COMM_IRQ_BASE))
47
48#define TRIG_BYTE_OFFSET(i) ((((i) * 2) / 32) * 4)
49#define TRIG_BIT_OFFSET(i) (((i) * 2) % 32)
50
51#define TRIG_VAL(trigger, irq) (trigger << TRIG_BIT_OFFSET(IRQ_OFFSET(irq)))
52#define TRIG_VAL_MSK(irq) (~(3 << TRIG_BIT_OFFSET(IRQ_OFFSET(irq))))
53
54#define TRIG_BASE(irq) \
55 (TRIG_BYTE_OFFSET(IRQ_OFFSET(irq)) + ((irq < COMM_IRQ_BASE) ? \
56 (this_cpu_read(intcl_reg) + INTCL_CFGR) : (INTCG_base + INTCG_CICFGR)))
57
58static DEFINE_SPINLOCK(setup_lock);
59static void setup_trigger(unsigned long irq, unsigned long trigger)
60{
61 unsigned int tmp;
62
63 spin_lock(&setup_lock);
64
65 /* setup trigger */
66 tmp = readl_relaxed(TRIG_BASE(irq)) & TRIG_VAL_MSK(irq);
67
68 writel_relaxed(tmp | TRIG_VAL(trigger, irq), TRIG_BASE(irq));
69
70 spin_unlock(&setup_lock);
71}
72
Guo Rend8a5f5f2018-09-16 15:57:14 +080073static void csky_mpintc_handler(struct pt_regs *regs)
74{
75 void __iomem *reg_base = this_cpu_read(intcl_reg);
76
Guo Rene85c9c92019-06-06 15:37:33 +080077 handle_domain_irq(root_domain,
78 readl_relaxed(reg_base + INTCL_RDYIR), regs);
Guo Rend8a5f5f2018-09-16 15:57:14 +080079}
80
81static void csky_mpintc_enable(struct irq_data *d)
82{
83 void __iomem *reg_base = this_cpu_read(intcl_reg);
84
Guo Ren648f8352019-06-06 15:37:31 +080085 setup_trigger(d->hwirq, __trigger[d->hwirq]);
86
Guo Rend8a5f5f2018-09-16 15:57:14 +080087 writel_relaxed(d->hwirq, reg_base + INTCL_SENR);
88}
89
90static void csky_mpintc_disable(struct irq_data *d)
91{
92 void __iomem *reg_base = this_cpu_read(intcl_reg);
93
94 writel_relaxed(d->hwirq, reg_base + INTCL_CENR);
95}
96
97static void csky_mpintc_eoi(struct irq_data *d)
98{
99 void __iomem *reg_base = this_cpu_read(intcl_reg);
100
101 writel_relaxed(d->hwirq, reg_base + INTCL_CACR);
102}
103
Guo Ren648f8352019-06-06 15:37:31 +0800104static int csky_mpintc_set_type(struct irq_data *d, unsigned int type)
105{
106 switch (type & IRQ_TYPE_SENSE_MASK) {
107 case IRQ_TYPE_LEVEL_HIGH:
108 __trigger[d->hwirq] = 0;
109 break;
110 case IRQ_TYPE_LEVEL_LOW:
111 __trigger[d->hwirq] = 1;
112 break;
113 case IRQ_TYPE_EDGE_RISING:
114 __trigger[d->hwirq] = 2;
115 break;
116 case IRQ_TYPE_EDGE_FALLING:
117 __trigger[d->hwirq] = 3;
118 break;
119 default:
120 return -EINVAL;
121 }
122
123 return 0;
124}
125
Guo Rend8a5f5f2018-09-16 15:57:14 +0800126#ifdef CONFIG_SMP
127static int csky_irq_set_affinity(struct irq_data *d,
128 const struct cpumask *mask_val,
129 bool force)
130{
131 unsigned int cpu;
132 unsigned int offset = 4 * (d->hwirq - COMM_IRQ_BASE);
133
134 if (!force)
135 cpu = cpumask_any_and(mask_val, cpu_online_mask);
136 else
137 cpu = cpumask_first(mask_val);
138
139 if (cpu >= nr_cpu_ids)
140 return -EINVAL;
141
142 /* Enable interrupt destination */
143 cpu |= BIT(31);
144
145 writel_relaxed(cpu, INTCG_base + INTCG_CIDSTR + offset);
146
147 irq_data_update_effective_affinity(d, cpumask_of(cpu));
148
149 return IRQ_SET_MASK_OK_DONE;
150}
151#endif
152
153static struct irq_chip csky_irq_chip = {
154 .name = "C-SKY SMP Intc",
155 .irq_eoi = csky_mpintc_eoi,
156 .irq_enable = csky_mpintc_enable,
157 .irq_disable = csky_mpintc_disable,
Guo Ren648f8352019-06-06 15:37:31 +0800158 .irq_set_type = csky_mpintc_set_type,
Guo Rend8a5f5f2018-09-16 15:57:14 +0800159#ifdef CONFIG_SMP
160 .irq_set_affinity = csky_irq_set_affinity,
161#endif
162};
163
164static int csky_irqdomain_map(struct irq_domain *d, unsigned int irq,
165 irq_hw_number_t hwirq)
166{
167 if (hwirq < COMM_IRQ_BASE) {
168 irq_set_percpu_devid(irq);
169 irq_set_chip_and_handler(irq, &csky_irq_chip,
170 handle_percpu_irq);
171 } else {
172 irq_set_chip_and_handler(irq, &csky_irq_chip,
173 handle_fasteoi_irq);
174 }
175
176 return 0;
177}
178
Guo Ren648f8352019-06-06 15:37:31 +0800179static int csky_irq_domain_xlate_cells(struct irq_domain *d,
180 struct device_node *ctrlr, const u32 *intspec,
181 unsigned int intsize, unsigned long *out_hwirq,
182 unsigned int *out_type)
183{
184 if (WARN_ON(intsize < 1))
185 return -EINVAL;
186
187 *out_hwirq = intspec[0];
188 if (intsize > 1)
189 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
190 else
191 *out_type = IRQ_TYPE_LEVEL_HIGH;
192
193 return 0;
194}
195
Guo Rend8a5f5f2018-09-16 15:57:14 +0800196static const struct irq_domain_ops csky_irqdomain_ops = {
197 .map = csky_irqdomain_map,
Guo Ren648f8352019-06-06 15:37:31 +0800198 .xlate = csky_irq_domain_xlate_cells,
Guo Rend8a5f5f2018-09-16 15:57:14 +0800199};
200
201#ifdef CONFIG_SMP
202static void csky_mpintc_send_ipi(const struct cpumask *mask)
203{
204 void __iomem *reg_base = this_cpu_read(intcl_reg);
205
206 /*
207 * INTCL_SIGR[3:0] INTID
208 * INTCL_SIGR[8:15] CPUMASK
209 */
210 writel_relaxed((*cpumask_bits(mask)) << 8 | IPI_IRQ,
211 reg_base + INTCL_SIGR);
212}
213#endif
214
215/* C-SKY multi processor interrupt controller */
216static int __init
217csky_mpintc_init(struct device_node *node, struct device_node *parent)
218{
219 int ret;
220 unsigned int cpu, nr_irq;
221#ifdef CONFIG_SMP
222 unsigned int ipi_irq;
223#endif
224
225 if (parent)
226 return 0;
227
228 ret = of_property_read_u32(node, "csky,num-irqs", &nr_irq);
229 if (ret < 0)
230 nr_irq = INTC_IRQS;
231
Guo Ren648f8352019-06-06 15:37:31 +0800232 __trigger = kcalloc(nr_irq, sizeof(unsigned long), GFP_KERNEL);
233 if (__trigger == NULL)
234 return -ENXIO;
235
Guo Rend8a5f5f2018-09-16 15:57:14 +0800236 if (INTCG_base == NULL) {
237 INTCG_base = ioremap(mfcr("cr<31, 14>"),
238 INTCL_SIZE*nr_cpu_ids + INTCG_SIZE);
239 if (INTCG_base == NULL)
240 return -EIO;
241
242 INTCL_base = INTCG_base + INTCG_SIZE;
243
244 writel_relaxed(BIT(0), INTCG_base + INTCG_ICTLR);
245 }
246
247 root_domain = irq_domain_add_linear(node, nr_irq, &csky_irqdomain_ops,
248 NULL);
249 if (!root_domain)
250 return -ENXIO;
251
252 /* for every cpu */
253 for_each_present_cpu(cpu) {
254 per_cpu(intcl_reg, cpu) = INTCL_base + (INTCL_SIZE * cpu);
255 writel_relaxed(BIT(0), per_cpu(intcl_reg, cpu) + INTCL_PICTLR);
256 }
257
258 set_handle_irq(&csky_mpintc_handler);
259
260#ifdef CONFIG_SMP
261 ipi_irq = irq_create_mapping(root_domain, IPI_IRQ);
262 if (!ipi_irq)
263 return -EIO;
264
265 set_send_ipi(&csky_mpintc_send_ipi, ipi_irq);
266#endif
267
268 return 0;
269}
270IRQCHIP_DECLARE(csky_mpintc, "csky,mpintc", csky_mpintc_init);