drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / arch / mips / lantiq / irq.c
blob8f208007b8e84c7b89d678b9bacc5b8410fb93b0
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
4 * Copyright (C) 2010 John Crispin <john@phrozen.org>
5 * Copyright (C) 2010 Thomas Langer <thomas.langer@lantiq.com>
6 */
8 #include <linux/interrupt.h>
9 #include <linux/ioport.h>
10 #include <linux/sched.h>
11 #include <linux/irqchip.h>
12 #include <linux/irqdomain.h>
13 #include <linux/of.h>
14 #include <linux/of_address.h>
15 #include <linux/of_irq.h>
17 #include <asm/bootinfo.h>
18 #include <asm/irq_cpu.h>
20 #include <lantiq_soc.h>
21 #include <irq.h>
23 /* register definitions - internal irqs */
24 #define LTQ_ICU_ISR 0x0000
25 #define LTQ_ICU_IER 0x0008
26 #define LTQ_ICU_IOSR 0x0010
27 #define LTQ_ICU_IRSR 0x0018
28 #define LTQ_ICU_IMR 0x0020
30 #define LTQ_ICU_IM_SIZE 0x28
32 /* register definitions - external irqs */
33 #define LTQ_EIU_EXIN_C 0x0000
34 #define LTQ_EIU_EXIN_INIC 0x0004
35 #define LTQ_EIU_EXIN_INC 0x0008
36 #define LTQ_EIU_EXIN_INEN 0x000C
38 /* number of external interrupts */
39 #define MAX_EIU 6
41 /* the performance counter */
42 #define LTQ_PERF_IRQ (INT_NUM_IM4_IRL0 + 31)
45 * irqs generated by devices attached to the EBU need to be acked in
46 * a special manner
48 #define LTQ_ICU_EBU_IRQ 22
50 #define ltq_icu_w32(vpe, m, x, y) \
51 ltq_w32((x), ltq_icu_membase[vpe] + m*LTQ_ICU_IM_SIZE + (y))
53 #define ltq_icu_r32(vpe, m, x) \
54 ltq_r32(ltq_icu_membase[vpe] + m*LTQ_ICU_IM_SIZE + (x))
56 #define ltq_eiu_w32(x, y) ltq_w32((x), ltq_eiu_membase + (y))
57 #define ltq_eiu_r32(x) ltq_r32(ltq_eiu_membase + (x))
59 /* we have a cascade of 8 irqs */
60 #define MIPS_CPU_IRQ_CASCADE 8
62 static int exin_avail;
63 static u32 ltq_eiu_irq[MAX_EIU];
64 static void __iomem *ltq_icu_membase[NR_CPUS];
65 static void __iomem *ltq_eiu_membase;
66 static struct irq_domain *ltq_domain;
67 static DEFINE_SPINLOCK(ltq_eiu_lock);
68 static DEFINE_RAW_SPINLOCK(ltq_icu_lock);
69 static int ltq_perfcount_irq;
71 int ltq_eiu_get_irq(int exin)
73 if (exin < exin_avail)
74 return ltq_eiu_irq[exin];
75 return -1;
78 void ltq_disable_irq(struct irq_data *d)
80 unsigned long offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
81 unsigned long im = offset / INT_NUM_IM_OFFSET;
82 unsigned long flags;
83 int vpe;
85 offset %= INT_NUM_IM_OFFSET;
87 raw_spin_lock_irqsave(&ltq_icu_lock, flags);
88 for_each_present_cpu(vpe) {
89 ltq_icu_w32(vpe, im,
90 ltq_icu_r32(vpe, im, LTQ_ICU_IER) & ~BIT(offset),
91 LTQ_ICU_IER);
93 raw_spin_unlock_irqrestore(&ltq_icu_lock, flags);
96 void ltq_mask_and_ack_irq(struct irq_data *d)
98 unsigned long offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
99 unsigned long im = offset / INT_NUM_IM_OFFSET;
100 unsigned long flags;
101 int vpe;
103 offset %= INT_NUM_IM_OFFSET;
105 raw_spin_lock_irqsave(&ltq_icu_lock, flags);
106 for_each_present_cpu(vpe) {
107 ltq_icu_w32(vpe, im,
108 ltq_icu_r32(vpe, im, LTQ_ICU_IER) & ~BIT(offset),
109 LTQ_ICU_IER);
110 ltq_icu_w32(vpe, im, BIT(offset), LTQ_ICU_ISR);
112 raw_spin_unlock_irqrestore(&ltq_icu_lock, flags);
115 static void ltq_ack_irq(struct irq_data *d)
117 unsigned long offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
118 unsigned long im = offset / INT_NUM_IM_OFFSET;
119 unsigned long flags;
120 int vpe;
122 offset %= INT_NUM_IM_OFFSET;
124 raw_spin_lock_irqsave(&ltq_icu_lock, flags);
125 for_each_present_cpu(vpe) {
126 ltq_icu_w32(vpe, im, BIT(offset), LTQ_ICU_ISR);
128 raw_spin_unlock_irqrestore(&ltq_icu_lock, flags);
131 void ltq_enable_irq(struct irq_data *d)
133 unsigned long offset = d->hwirq - MIPS_CPU_IRQ_CASCADE;
134 unsigned long im = offset / INT_NUM_IM_OFFSET;
135 unsigned long flags;
136 int vpe;
138 offset %= INT_NUM_IM_OFFSET;
140 vpe = cpumask_first(irq_data_get_effective_affinity_mask(d));
142 /* This shouldn't be even possible, maybe during CPU hotplug spam */
143 if (unlikely(vpe >= nr_cpu_ids))
144 vpe = smp_processor_id();
146 raw_spin_lock_irqsave(&ltq_icu_lock, flags);
148 ltq_icu_w32(vpe, im, ltq_icu_r32(vpe, im, LTQ_ICU_IER) | BIT(offset),
149 LTQ_ICU_IER);
151 raw_spin_unlock_irqrestore(&ltq_icu_lock, flags);
154 static int ltq_eiu_settype(struct irq_data *d, unsigned int type)
156 int i;
157 unsigned long flags;
159 for (i = 0; i < exin_avail; i++) {
160 if (d->hwirq == ltq_eiu_irq[i]) {
161 int val = 0;
162 int edge = 0;
164 switch (type) {
165 case IRQF_TRIGGER_NONE:
166 break;
167 case IRQF_TRIGGER_RISING:
168 val = 1;
169 edge = 1;
170 break;
171 case IRQF_TRIGGER_FALLING:
172 val = 2;
173 edge = 1;
174 break;
175 case IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING:
176 val = 3;
177 edge = 1;
178 break;
179 case IRQF_TRIGGER_HIGH:
180 val = 5;
181 break;
182 case IRQF_TRIGGER_LOW:
183 val = 6;
184 break;
185 default:
186 pr_err("invalid type %d for irq %ld\n",
187 type, d->hwirq);
188 return -EINVAL;
191 if (edge)
192 irq_set_handler(d->hwirq, handle_edge_irq);
194 spin_lock_irqsave(&ltq_eiu_lock, flags);
195 ltq_eiu_w32((ltq_eiu_r32(LTQ_EIU_EXIN_C) &
196 (~(7 << (i * 4)))) | (val << (i * 4)),
197 LTQ_EIU_EXIN_C);
198 spin_unlock_irqrestore(&ltq_eiu_lock, flags);
202 return 0;
205 static unsigned int ltq_startup_eiu_irq(struct irq_data *d)
207 int i;
209 ltq_enable_irq(d);
210 for (i = 0; i < exin_avail; i++) {
211 if (d->hwirq == ltq_eiu_irq[i]) {
212 /* by default we are low level triggered */
213 ltq_eiu_settype(d, IRQF_TRIGGER_LOW);
214 /* clear all pending */
215 ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INC) & ~BIT(i),
216 LTQ_EIU_EXIN_INC);
217 /* enable */
218 ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INEN) | BIT(i),
219 LTQ_EIU_EXIN_INEN);
220 break;
224 return 0;
227 static void ltq_shutdown_eiu_irq(struct irq_data *d)
229 int i;
231 ltq_disable_irq(d);
232 for (i = 0; i < exin_avail; i++) {
233 if (d->hwirq == ltq_eiu_irq[i]) {
234 /* disable */
235 ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INEN) & ~BIT(i),
236 LTQ_EIU_EXIN_INEN);
237 break;
242 #if defined(CONFIG_SMP)
243 static int ltq_icu_irq_set_affinity(struct irq_data *d,
244 const struct cpumask *cpumask, bool force)
246 struct cpumask tmask;
248 if (!cpumask_and(&tmask, cpumask, cpu_online_mask))
249 return -EINVAL;
251 irq_data_update_effective_affinity(d, &tmask);
253 return IRQ_SET_MASK_OK;
255 #endif
257 static struct irq_chip ltq_irq_type = {
258 .name = "icu",
259 .irq_enable = ltq_enable_irq,
260 .irq_disable = ltq_disable_irq,
261 .irq_unmask = ltq_enable_irq,
262 .irq_ack = ltq_ack_irq,
263 .irq_mask = ltq_disable_irq,
264 .irq_mask_ack = ltq_mask_and_ack_irq,
265 #if defined(CONFIG_SMP)
266 .irq_set_affinity = ltq_icu_irq_set_affinity,
267 #endif
270 static struct irq_chip ltq_eiu_type = {
271 .name = "eiu",
272 .irq_startup = ltq_startup_eiu_irq,
273 .irq_shutdown = ltq_shutdown_eiu_irq,
274 .irq_enable = ltq_enable_irq,
275 .irq_disable = ltq_disable_irq,
276 .irq_unmask = ltq_enable_irq,
277 .irq_ack = ltq_ack_irq,
278 .irq_mask = ltq_disable_irq,
279 .irq_mask_ack = ltq_mask_and_ack_irq,
280 .irq_set_type = ltq_eiu_settype,
281 #if defined(CONFIG_SMP)
282 .irq_set_affinity = ltq_icu_irq_set_affinity,
283 #endif
286 static void ltq_hw_irq_handler(struct irq_desc *desc)
288 unsigned int module = irq_desc_get_irq(desc) - 2;
289 u32 irq;
290 irq_hw_number_t hwirq;
291 int vpe = smp_processor_id();
293 irq = ltq_icu_r32(vpe, module, LTQ_ICU_IOSR);
294 if (irq == 0)
295 return;
298 * silicon bug causes only the msb set to 1 to be valid. all
299 * other bits might be bogus
301 irq = __fls(irq);
302 hwirq = irq + MIPS_CPU_IRQ_CASCADE + (INT_NUM_IM_OFFSET * module);
303 generic_handle_domain_irq(ltq_domain, hwirq);
305 /* if this is a EBU irq, we need to ack it or get a deadlock */
306 if (irq == LTQ_ICU_EBU_IRQ && !module && LTQ_EBU_PCC_ISTAT != 0)
307 ltq_ebu_w32(ltq_ebu_r32(LTQ_EBU_PCC_ISTAT) | 0x10,
308 LTQ_EBU_PCC_ISTAT);
311 static int icu_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
313 struct irq_chip *chip = &ltq_irq_type;
314 struct irq_data *data;
315 int i;
317 if (hw < MIPS_CPU_IRQ_CASCADE)
318 return 0;
320 for (i = 0; i < exin_avail; i++)
321 if (hw == ltq_eiu_irq[i])
322 chip = &ltq_eiu_type;
324 data = irq_get_irq_data(irq);
326 irq_data_update_effective_affinity(data, cpumask_of(0));
328 irq_set_chip_and_handler(irq, chip, handle_level_irq);
330 return 0;
333 static const struct irq_domain_ops irq_domain_ops = {
334 .xlate = irq_domain_xlate_onetwocell,
335 .map = icu_map,
338 int __init icu_of_init(struct device_node *node, struct device_node *parent)
340 struct device_node *eiu_node;
341 struct resource res;
342 int i, ret, vpe;
344 /* load register regions of available ICUs */
345 for_each_possible_cpu(vpe) {
346 if (of_address_to_resource(node, vpe, &res))
347 panic("Failed to get icu%i memory range", vpe);
349 if (!request_mem_region(res.start, resource_size(&res),
350 res.name))
351 pr_err("Failed to request icu%i memory\n", vpe);
353 ltq_icu_membase[vpe] = ioremap(res.start,
354 resource_size(&res));
356 if (!ltq_icu_membase[vpe])
357 panic("Failed to remap icu%i memory", vpe);
360 /* turn off all irqs by default */
361 for_each_possible_cpu(vpe) {
362 for (i = 0; i < MAX_IM; i++) {
363 /* make sure all irqs are turned off by default */
364 ltq_icu_w32(vpe, i, 0, LTQ_ICU_IER);
366 /* clear all possibly pending interrupts */
367 ltq_icu_w32(vpe, i, ~0, LTQ_ICU_ISR);
368 ltq_icu_w32(vpe, i, ~0, LTQ_ICU_IMR);
370 /* clear resend */
371 ltq_icu_w32(vpe, i, 0, LTQ_ICU_IRSR);
375 mips_cpu_irq_init();
377 for (i = 0; i < MAX_IM; i++)
378 irq_set_chained_handler(i + 2, ltq_hw_irq_handler);
380 ltq_domain = irq_domain_add_linear(node,
381 (MAX_IM * INT_NUM_IM_OFFSET) + MIPS_CPU_IRQ_CASCADE,
382 &irq_domain_ops, 0);
384 /* tell oprofile which irq to use */
385 ltq_perfcount_irq = irq_create_mapping(ltq_domain, LTQ_PERF_IRQ);
387 /* the external interrupts are optional and xway only */
388 eiu_node = of_find_compatible_node(NULL, NULL, "lantiq,eiu-xway");
389 if (eiu_node && !of_address_to_resource(eiu_node, 0, &res)) {
390 /* find out how many external irq sources we have */
391 exin_avail = of_property_count_u32_elems(eiu_node,
392 "lantiq,eiu-irqs");
394 if (exin_avail > MAX_EIU)
395 exin_avail = MAX_EIU;
397 ret = of_property_read_u32_array(eiu_node, "lantiq,eiu-irqs",
398 ltq_eiu_irq, exin_avail);
399 if (ret)
400 panic("failed to load external irq resources");
402 if (!request_mem_region(res.start, resource_size(&res),
403 res.name))
404 pr_err("Failed to request eiu memory");
406 ltq_eiu_membase = ioremap(res.start,
407 resource_size(&res));
408 if (!ltq_eiu_membase)
409 panic("Failed to remap eiu memory");
411 of_node_put(eiu_node);
413 return 0;
416 int get_c0_perfcount_int(void)
418 return ltq_perfcount_irq;
420 EXPORT_SYMBOL_GPL(get_c0_perfcount_int);
422 unsigned int get_c0_compare_int(void)
424 return CP0_LEGACY_COMPARE_IRQ;
427 IRQCHIP_DECLARE(lantiq_icu, "lantiq,icu", icu_of_init);
429 void __init arch_init_irq(void)
431 irqchip_init();