blob: c585574b9aedcc6b5c25fc6db620959a61a9ad08 [file] [log] [blame]
Magnus Damm02ab3f72007-07-18 17:25:09 +09001/*
2 * Shared interrupt handling code for IPR and INTC2 types of IRQs.
3 *
Magnus Dammd58876e2008-04-24 21:36:34 +09004 * Copyright (C) 2007, 2008 Magnus Damm
Paul Mundta8941da2010-03-08 13:33:17 +09005 * Copyright (C) 2009, 2010 Paul Mundt
Magnus Damm02ab3f72007-07-18 17:25:09 +09006 *
7 * Based on intc2.c and ipr.c
8 *
9 * Copyright (C) 1999 Niibe Yutaka & Takeshi Yaegashi
10 * Copyright (C) 2000 Kazumoto Kojima
11 * Copyright (C) 2001 David J. Mckay (david.mckay@st.com)
12 * Copyright (C) 2003 Takashi Kusuda <kusuda-takashi@hitachi-ul.co.jp>
13 * Copyright (C) 2005, 2006 Paul Mundt
14 *
15 * This file is subject to the terms and conditions of the GNU General Public
16 * License. See the file "COPYING" in the main directory of this archive
17 * for more details.
18 */
19#include <linux/init.h>
20#include <linux/irq.h>
21#include <linux/module.h>
22#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Magnus Damm02ab3f72007-07-18 17:25:09 +090024#include <linux/interrupt.h>
Paul Mundtbbfbd8b2008-10-01 16:13:54 +090025#include <linux/sh_intc.h>
Magnus Damm2dcec7a2009-04-01 14:30:59 +000026#include <linux/sysdev.h>
27#include <linux/list.h>
Paul Mundt54ff3282009-06-11 10:33:09 +030028#include <linux/topology.h>
Paul Mundt1ce7b032009-11-02 10:30:26 +090029#include <linux/bitmap.h>
Paul Mundta8941da2010-03-08 13:33:17 +090030#include <linux/cpumask.h>
Paul Mundt43b87742010-04-13 14:43:03 +090031#include <asm/sizes.h>
Magnus Damm02ab3f72007-07-18 17:25:09 +090032
Magnus Damm73505b42007-08-12 15:26:12 +090033#define _INTC_MK(fn, mode, addr_e, addr_d, width, shift) \
34 ((shift) | ((width) << 5) | ((fn) << 9) | ((mode) << 13) | \
35 ((addr_e) << 16) | ((addr_d << 24)))
Magnus Damm02ab3f72007-07-18 17:25:09 +090036
Magnus Damm73505b42007-08-12 15:26:12 +090037#define _INTC_SHIFT(h) (h & 0x1f)
38#define _INTC_WIDTH(h) ((h >> 5) & 0xf)
39#define _INTC_FN(h) ((h >> 9) & 0xf)
40#define _INTC_MODE(h) ((h >> 13) & 0x7)
41#define _INTC_ADDR_E(h) ((h >> 16) & 0xff)
42#define _INTC_ADDR_D(h) ((h >> 24) & 0xff)
Magnus Damm02ab3f72007-07-18 17:25:09 +090043
Magnus Damm73505b42007-08-12 15:26:12 +090044struct intc_handle_int {
45 unsigned int irq;
46 unsigned long handle;
47};
48
Magnus Dammdec710b2010-03-19 16:48:01 +090049struct intc_window {
50 phys_addr_t phys;
51 void __iomem *virt;
52 unsigned long size;
53};
54
Magnus Damm73505b42007-08-12 15:26:12 +090055struct intc_desc_int {
Magnus Damm2dcec7a2009-04-01 14:30:59 +000056 struct list_head list;
57 struct sys_device sysdev;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +000058 pm_message_t state;
Magnus Damm73505b42007-08-12 15:26:12 +090059 unsigned long *reg;
Magnus Dammf18d5332007-09-21 18:16:42 +090060#ifdef CONFIG_SMP
61 unsigned long *smp;
62#endif
Magnus Damm73505b42007-08-12 15:26:12 +090063 unsigned int nr_reg;
64 struct intc_handle_int *prio;
65 unsigned int nr_prio;
66 struct intc_handle_int *sense;
67 unsigned int nr_sense;
Magnus Dammdec710b2010-03-19 16:48:01 +090068 struct intc_window *window;
69 unsigned int nr_windows;
Magnus Damm73505b42007-08-12 15:26:12 +090070 struct irq_chip chip;
71};
72
Magnus Damm2dcec7a2009-04-01 14:30:59 +000073static LIST_HEAD(intc_list);
74
Paul Mundt1ce7b032009-11-02 10:30:26 +090075/*
76 * The intc_irq_map provides a global map of bound IRQ vectors for a
77 * given platform. Allocation of IRQs are either static through the CPU
78 * vector map, or dynamic in the case of board mux vectors or MSI.
79 *
80 * As this is a central point for all IRQ controllers on the system,
81 * each of the available sources are mapped out here. This combined with
82 * sparseirq makes it quite trivial to keep the vector map tightly packed
83 * when dynamically creating IRQs, as well as tying in to otherwise
84 * unused irq_desc positions in the sparse array.
85 */
86static DECLARE_BITMAP(intc_irq_map, NR_IRQS);
87static DEFINE_SPINLOCK(vector_lock);
88
Magnus Dammf18d5332007-09-21 18:16:42 +090089#ifdef CONFIG_SMP
90#define IS_SMP(x) x.smp
91#define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
92#define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
93#else
94#define IS_SMP(x) 0
95#define INTC_REG(d, x, c) (d->reg[(x)])
96#define SMP_NR(d, x) 1
97#endif
98
Paul Mundt43b87742010-04-13 14:43:03 +090099static unsigned int intc_prio_level[NR_IRQS]; /* for now */
100static unsigned int default_prio_level = 2; /* 2 - 16 */
Magnus Dammd58876e2008-04-24 21:36:34 +0900101static unsigned long ack_handle[NR_IRQS];
Paul Mundtdc825b12010-04-15 13:13:52 +0900102#ifdef CONFIG_INTC_BALANCING
103static unsigned long dist_handle[NR_IRQS];
104#endif
Magnus Damm73505b42007-08-12 15:26:12 +0900105
106static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900107{
108 struct irq_chip *chip = get_irq_chip(irq);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900109 return container_of(chip, struct intc_desc_int, chip);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900110}
111
Paul Mundtdc825b12010-04-15 13:13:52 +0900112static unsigned long intc_phys_to_virt(struct intc_desc_int *d,
113 unsigned long address)
114{
115 struct intc_window *window;
116 int k;
117
118 /* scan through physical windows and convert address */
119 for (k = 0; k < d->nr_windows; k++) {
120 window = d->window + k;
121
122 if (address < window->phys)
123 continue;
124
125 if (address >= (window->phys + window->size))
126 continue;
127
128 address -= window->phys;
129 address += (unsigned long)window->virt;
130
131 return address;
132 }
133
134 /* no windows defined, register must be 1:1 mapped virt:phys */
135 return address;
136}
137
138static unsigned int intc_get_reg(struct intc_desc_int *d, unsigned long address)
139{
140 unsigned int k;
141
142 address = intc_phys_to_virt(d, address);
143
144 for (k = 0; k < d->nr_reg; k++) {
145 if (d->reg[k] == address)
146 return k;
147 }
148
149 BUG();
150 return 0;
151}
152
Magnus Damm02ab3f72007-07-18 17:25:09 +0900153static inline unsigned int set_field(unsigned int value,
154 unsigned int field_value,
Magnus Damm73505b42007-08-12 15:26:12 +0900155 unsigned int handle)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900156{
Magnus Damm73505b42007-08-12 15:26:12 +0900157 unsigned int width = _INTC_WIDTH(handle);
158 unsigned int shift = _INTC_SHIFT(handle);
159
Magnus Damm02ab3f72007-07-18 17:25:09 +0900160 value &= ~(((1 << width) - 1) << shift);
161 value |= field_value << shift;
162 return value;
163}
164
Magnus Damm73505b42007-08-12 15:26:12 +0900165static void write_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900166{
Paul Mundt62429e02008-10-01 15:19:10 +0900167 __raw_writeb(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900168 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900169}
170
Magnus Damm73505b42007-08-12 15:26:12 +0900171static void write_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900172{
Paul Mundt62429e02008-10-01 15:19:10 +0900173 __raw_writew(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900174 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900175}
176
Magnus Damm73505b42007-08-12 15:26:12 +0900177static void write_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900178{
Paul Mundt62429e02008-10-01 15:19:10 +0900179 __raw_writel(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900180 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900181}
182
Magnus Damm73505b42007-08-12 15:26:12 +0900183static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900184{
Magnus Damm4370fe12008-04-24 21:53:07 +0900185 unsigned long flags;
186 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900187 __raw_writeb(set_field(__raw_readb(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900188 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900189 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900190}
191
Magnus Damm73505b42007-08-12 15:26:12 +0900192static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900193{
Magnus Damm4370fe12008-04-24 21:53:07 +0900194 unsigned long flags;
195 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900196 __raw_writew(set_field(__raw_readw(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900197 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900198 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900199}
200
Magnus Damm73505b42007-08-12 15:26:12 +0900201static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900202{
Magnus Damm4370fe12008-04-24 21:53:07 +0900203 unsigned long flags;
204 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900205 __raw_writel(set_field(__raw_readl(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900206 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900207 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900208}
209
Magnus Damm73505b42007-08-12 15:26:12 +0900210enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
Magnus Damm02ab3f72007-07-18 17:25:09 +0900211
Magnus Damm73505b42007-08-12 15:26:12 +0900212static void (*intc_reg_fns[])(unsigned long addr,
213 unsigned long h,
214 unsigned long data) = {
215 [REG_FN_WRITE_BASE + 0] = write_8,
216 [REG_FN_WRITE_BASE + 1] = write_16,
217 [REG_FN_WRITE_BASE + 3] = write_32,
218 [REG_FN_MODIFY_BASE + 0] = modify_8,
219 [REG_FN_MODIFY_BASE + 1] = modify_16,
220 [REG_FN_MODIFY_BASE + 3] = modify_32,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900221};
222
Magnus Damm73505b42007-08-12 15:26:12 +0900223enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
224 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
225 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
226 MODE_PRIO_REG, /* Priority value written to enable interrupt */
227 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
228};
229
230static void intc_mode_field(unsigned long addr,
231 unsigned long handle,
232 void (*fn)(unsigned long,
233 unsigned long,
234 unsigned long),
235 unsigned int irq)
236{
237 fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
238}
239
240static void intc_mode_zero(unsigned long addr,
241 unsigned long handle,
242 void (*fn)(unsigned long,
243 unsigned long,
244 unsigned long),
245 unsigned int irq)
246{
247 fn(addr, handle, 0);
248}
249
250static void intc_mode_prio(unsigned long addr,
251 unsigned long handle,
252 void (*fn)(unsigned long,
253 unsigned long,
254 unsigned long),
255 unsigned int irq)
256{
257 fn(addr, handle, intc_prio_level[irq]);
258}
259
260static void (*intc_enable_fns[])(unsigned long addr,
261 unsigned long handle,
262 void (*fn)(unsigned long,
263 unsigned long,
264 unsigned long),
265 unsigned int irq) = {
266 [MODE_ENABLE_REG] = intc_mode_field,
267 [MODE_MASK_REG] = intc_mode_zero,
268 [MODE_DUAL_REG] = intc_mode_field,
269 [MODE_PRIO_REG] = intc_mode_prio,
270 [MODE_PCLR_REG] = intc_mode_prio,
271};
272
273static void (*intc_disable_fns[])(unsigned long addr,
274 unsigned long handle,
275 void (*fn)(unsigned long,
276 unsigned long,
277 unsigned long),
278 unsigned int irq) = {
279 [MODE_ENABLE_REG] = intc_mode_zero,
280 [MODE_MASK_REG] = intc_mode_field,
281 [MODE_DUAL_REG] = intc_mode_field,
282 [MODE_PRIO_REG] = intc_mode_zero,
283 [MODE_PCLR_REG] = intc_mode_field,
284};
285
Paul Mundtdc825b12010-04-15 13:13:52 +0900286#ifdef CONFIG_INTC_BALANCING
287static inline void intc_balancing_enable(unsigned int irq)
288{
289 struct intc_desc_int *d = get_intc_desc(irq);
290 unsigned long handle = dist_handle[irq];
291 unsigned long addr;
292
293 if (irq_balancing_disabled(irq) || !handle)
294 return;
295
296 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
297 intc_reg_fns[_INTC_FN(handle)](addr, handle, 1);
298}
299
300static inline void intc_balancing_disable(unsigned int irq)
301{
302 struct intc_desc_int *d = get_intc_desc(irq);
303 unsigned long handle = dist_handle[irq];
304 unsigned long addr;
305
306 if (irq_balancing_disabled(irq) || !handle)
307 return;
308
309 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
310 intc_reg_fns[_INTC_FN(handle)](addr, handle, 0);
311}
312
313static unsigned int intc_dist_data(struct intc_desc *desc,
314 struct intc_desc_int *d,
315 intc_enum enum_id)
316{
317 struct intc_mask_reg *mr = desc->hw.mask_regs;
318 unsigned int i, j, fn, mode;
319 unsigned long reg_e, reg_d;
320
321 for (i = 0; mr && enum_id && i < desc->hw.nr_mask_regs; i++) {
322 mr = desc->hw.mask_regs + i;
323
324 /*
325 * Skip this entry if there's no auto-distribution
326 * register associated with it.
327 */
328 if (!mr->dist_reg)
329 continue;
330
331 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
332 if (mr->enum_ids[j] != enum_id)
333 continue;
334
335 fn = REG_FN_MODIFY_BASE;
336 mode = MODE_ENABLE_REG;
337 reg_e = mr->dist_reg;
338 reg_d = mr->dist_reg;
339
340 fn += (mr->reg_width >> 3) - 1;
341 return _INTC_MK(fn, mode,
342 intc_get_reg(d, reg_e),
343 intc_get_reg(d, reg_d),
344 1,
345 (mr->reg_width - 1) - j);
346 }
347 }
348
349 /*
350 * It's possible we've gotten here with no distribution options
351 * available for the IRQ in question, so we just skip over those.
352 */
353 return 0;
354}
355#else
356static inline void intc_balancing_enable(unsigned int irq)
357{
358}
359
360static inline void intc_balancing_disable(unsigned int irq)
361{
362}
363#endif
364
Magnus Damm73505b42007-08-12 15:26:12 +0900365static inline void _intc_enable(unsigned int irq, unsigned long handle)
366{
367 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900368 unsigned long addr;
369 unsigned int cpu;
Magnus Damm73505b42007-08-12 15:26:12 +0900370
Magnus Dammf18d5332007-09-21 18:16:42 +0900371 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
Paul Mundta8941da2010-03-08 13:33:17 +0900372#ifdef CONFIG_SMP
373 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
374 continue;
375#endif
Magnus Dammf18d5332007-09-21 18:16:42 +0900376 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
377 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
378 [_INTC_FN(handle)], irq);
379 }
Paul Mundtdc825b12010-04-15 13:13:52 +0900380
381 intc_balancing_enable(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900382}
383
Magnus Damm02ab3f72007-07-18 17:25:09 +0900384static void intc_enable(unsigned int irq)
385{
Magnus Damm73505b42007-08-12 15:26:12 +0900386 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
Magnus Damm02ab3f72007-07-18 17:25:09 +0900387}
388
389static void intc_disable(unsigned int irq)
390{
Magnus Dammf18d5332007-09-21 18:16:42 +0900391 struct intc_desc_int *d = get_intc_desc(irq);
Paul Mundtdc825b12010-04-15 13:13:52 +0900392 unsigned long handle = (unsigned long)get_irq_chip_data(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900393 unsigned long addr;
394 unsigned int cpu;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900395
Paul Mundtdc825b12010-04-15 13:13:52 +0900396 intc_balancing_disable(irq);
397
Magnus Dammf18d5332007-09-21 18:16:42 +0900398 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
Paul Mundta8941da2010-03-08 13:33:17 +0900399#ifdef CONFIG_SMP
400 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
401 continue;
402#endif
Magnus Dammf18d5332007-09-21 18:16:42 +0900403 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
404 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
405 [_INTC_FN(handle)], irq);
406 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900407}
408
Magnus Dammd5190952010-02-09 04:29:22 +0000409static void (*intc_enable_noprio_fns[])(unsigned long addr,
410 unsigned long handle,
411 void (*fn)(unsigned long,
412 unsigned long,
413 unsigned long),
414 unsigned int irq) = {
415 [MODE_ENABLE_REG] = intc_mode_field,
416 [MODE_MASK_REG] = intc_mode_zero,
417 [MODE_DUAL_REG] = intc_mode_field,
418 [MODE_PRIO_REG] = intc_mode_field,
419 [MODE_PCLR_REG] = intc_mode_field,
420};
421
422static void intc_enable_disable(struct intc_desc_int *d,
423 unsigned long handle, int do_enable)
424{
425 unsigned long addr;
426 unsigned int cpu;
427 void (*fn)(unsigned long, unsigned long,
428 void (*)(unsigned long, unsigned long, unsigned long),
429 unsigned int);
430
431 if (do_enable) {
432 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
433 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
434 fn = intc_enable_noprio_fns[_INTC_MODE(handle)];
435 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
436 }
437 } else {
438 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
439 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
440 fn = intc_disable_fns[_INTC_MODE(handle)];
441 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
442 }
443 }
444}
445
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000446static int intc_set_wake(unsigned int irq, unsigned int on)
447{
448 return 0; /* allow wakeup, but setup hardware in intc_suspend() */
449}
450
Paul Mundta8941da2010-03-08 13:33:17 +0900451#ifdef CONFIG_SMP
452/*
453 * This is held with the irq desc lock held, so we don't require any
454 * additional locking here at the intc desc level. The affinity mask is
455 * later tested in the enable/disable paths.
456 */
457static int intc_set_affinity(unsigned int irq, const struct cpumask *cpumask)
458{
459 if (!cpumask_intersects(cpumask, cpu_online_mask))
460 return -1;
461
462 cpumask_copy(irq_to_desc(irq)->affinity, cpumask);
463
464 return 0;
465}
466#endif
467
Magnus Dammd58876e2008-04-24 21:36:34 +0900468static void intc_mask_ack(unsigned int irq)
469{
470 struct intc_desc_int *d = get_intc_desc(irq);
471 unsigned long handle = ack_handle[irq];
472 unsigned long addr;
473
474 intc_disable(irq);
475
Paul Mundtdc825b12010-04-15 13:13:52 +0900476 /* read register and write zero only to the associated bit */
Magnus Dammd58876e2008-04-24 21:36:34 +0900477 if (handle) {
478 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900479 switch (_INTC_FN(handle)) {
480 case REG_FN_MODIFY_BASE + 0: /* 8bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900481 __raw_readb(addr);
482 __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900483 break;
484 case REG_FN_MODIFY_BASE + 1: /* 16bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900485 __raw_readw(addr);
486 __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900487 break;
488 case REG_FN_MODIFY_BASE + 3: /* 32bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900489 __raw_readl(addr);
490 __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900491 break;
492 default:
493 BUG();
494 break;
495 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900496 }
497}
Magnus Dammd58876e2008-04-24 21:36:34 +0900498
Magnus Damm73505b42007-08-12 15:26:12 +0900499static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
500 unsigned int nr_hp,
501 unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900502{
Magnus Damm73505b42007-08-12 15:26:12 +0900503 int i;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900504
Paul Mundtdc825b12010-04-15 13:13:52 +0900505 /*
506 * this doesn't scale well, but...
Magnus Damm3d37d942007-08-17 00:50:44 +0900507 *
508 * this function should only be used for cerain uncommon
509 * operations such as intc_set_priority() and intc_set_sense()
510 * and in those rare cases performance doesn't matter that much.
511 * keeping the memory footprint low is more important.
512 *
513 * one rather simple way to speed this up and still keep the
514 * memory footprint down is to make sure the array is sorted
515 * and then perform a bisect to lookup the irq.
516 */
Magnus Damm73505b42007-08-12 15:26:12 +0900517 for (i = 0; i < nr_hp; i++) {
518 if ((hp + i)->irq != irq)
519 continue;
520
521 return hp + i;
522 }
523
524 return NULL;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900525}
526
Magnus Damm73505b42007-08-12 15:26:12 +0900527int intc_set_priority(unsigned int irq, unsigned int prio)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900528{
Magnus Damm73505b42007-08-12 15:26:12 +0900529 struct intc_desc_int *d = get_intc_desc(irq);
530 struct intc_handle_int *ihp;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900531
Magnus Damm73505b42007-08-12 15:26:12 +0900532 if (!intc_prio_level[irq] || prio <= 1)
533 return -EINVAL;
534
535 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
536 if (ihp) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900537 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
Magnus Damm73505b42007-08-12 15:26:12 +0900538 return -EINVAL;
539
540 intc_prio_level[irq] = prio;
541
542 /*
543 * only set secondary masking method directly
544 * primary masking method is using intc_prio_level[irq]
545 * priority level will be set during next enable()
546 */
Magnus Damm3d37d942007-08-17 00:50:44 +0900547 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
Magnus Damm73505b42007-08-12 15:26:12 +0900548 _intc_enable(irq, ihp->handle);
549 }
550 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900551}
552
553#define VALID(x) (x | 0x80)
554
555static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
556 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
557 [IRQ_TYPE_EDGE_RISING] = VALID(1),
558 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
Magnus Damm720be992008-04-24 21:47:15 +0900559 /* SH7706, SH7707 and SH7709 do not support high level triggered */
560#if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
561 !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
562 !defined(CONFIG_CPU_SUBTYPE_SH7709)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900563 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
Magnus Damm720be992008-04-24 21:47:15 +0900564#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900565};
566
567static int intc_set_sense(unsigned int irq, unsigned int type)
568{
Magnus Damm73505b42007-08-12 15:26:12 +0900569 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900570 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
Magnus Damm73505b42007-08-12 15:26:12 +0900571 struct intc_handle_int *ihp;
572 unsigned long addr;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900573
Magnus Damm73505b42007-08-12 15:26:12 +0900574 if (!value)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900575 return -EINVAL;
576
Magnus Damm73505b42007-08-12 15:26:12 +0900577 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
578 if (ihp) {
Magnus Dammf18d5332007-09-21 18:16:42 +0900579 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900580 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900581 }
Magnus Damm73505b42007-08-12 15:26:12 +0900582 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900583}
584
Magnus Damm73505b42007-08-12 15:26:12 +0900585static intc_enum __init intc_grp_id(struct intc_desc *desc,
586 intc_enum enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900587{
Magnus Damm577cd752010-02-09 04:24:46 +0000588 struct intc_group *g = desc->hw.groups;
Magnus Damm680c4592007-07-20 12:09:29 +0900589 unsigned int i, j;
590
Magnus Damm577cd752010-02-09 04:24:46 +0000591 for (i = 0; g && enum_id && i < desc->hw.nr_groups; i++) {
592 g = desc->hw.groups + i;
Magnus Damm680c4592007-07-20 12:09:29 +0900593
594 for (j = 0; g->enum_ids[j]; j++) {
595 if (g->enum_ids[j] != enum_id)
596 continue;
597
598 return g->enum_id;
599 }
600 }
601
602 return 0;
603}
604
Magnus Dammd5190952010-02-09 04:29:22 +0000605static unsigned int __init _intc_mask_data(struct intc_desc *desc,
606 struct intc_desc_int *d,
607 intc_enum enum_id,
608 unsigned int *reg_idx,
609 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900610{
Magnus Damm577cd752010-02-09 04:24:46 +0000611 struct intc_mask_reg *mr = desc->hw.mask_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000612 unsigned int fn, mode;
Magnus Damm73505b42007-08-12 15:26:12 +0900613 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900614
Magnus Dammd5190952010-02-09 04:29:22 +0000615 while (mr && enum_id && *reg_idx < desc->hw.nr_mask_regs) {
616 mr = desc->hw.mask_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900617
Magnus Dammd5190952010-02-09 04:29:22 +0000618 for (; *fld_idx < ARRAY_SIZE(mr->enum_ids); (*fld_idx)++) {
619 if (mr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900620 continue;
621
Magnus Damm73505b42007-08-12 15:26:12 +0900622 if (mr->set_reg && mr->clr_reg) {
623 fn = REG_FN_WRITE_BASE;
624 mode = MODE_DUAL_REG;
625 reg_e = mr->clr_reg;
626 reg_d = mr->set_reg;
627 } else {
628 fn = REG_FN_MODIFY_BASE;
629 if (mr->set_reg) {
630 mode = MODE_ENABLE_REG;
631 reg_e = mr->set_reg;
632 reg_d = mr->set_reg;
633 } else {
634 mode = MODE_MASK_REG;
635 reg_e = mr->clr_reg;
636 reg_d = mr->clr_reg;
637 }
Magnus Damm51da6422007-08-03 14:25:32 +0900638 }
639
Magnus Damm73505b42007-08-12 15:26:12 +0900640 fn += (mr->reg_width >> 3) - 1;
641 return _INTC_MK(fn, mode,
642 intc_get_reg(d, reg_e),
643 intc_get_reg(d, reg_d),
644 1,
Magnus Dammd5190952010-02-09 04:29:22 +0000645 (mr->reg_width - 1) - *fld_idx);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900646 }
Magnus Dammd5190952010-02-09 04:29:22 +0000647
648 *fld_idx = 0;
649 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900650 }
651
Magnus Dammd5190952010-02-09 04:29:22 +0000652 return 0;
653}
654
655static unsigned int __init intc_mask_data(struct intc_desc *desc,
656 struct intc_desc_int *d,
657 intc_enum enum_id, int do_grps)
658{
659 unsigned int i = 0;
660 unsigned int j = 0;
661 unsigned int ret;
662
663 ret = _intc_mask_data(desc, d, enum_id, &i, &j);
664 if (ret)
665 return ret;
666
Magnus Damm680c4592007-07-20 12:09:29 +0900667 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900668 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900669
Magnus Damm02ab3f72007-07-18 17:25:09 +0900670 return 0;
671}
672
Magnus Dammd5190952010-02-09 04:29:22 +0000673static unsigned int __init _intc_prio_data(struct intc_desc *desc,
674 struct intc_desc_int *d,
675 intc_enum enum_id,
676 unsigned int *reg_idx,
677 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900678{
Magnus Damm577cd752010-02-09 04:24:46 +0000679 struct intc_prio_reg *pr = desc->hw.prio_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000680 unsigned int fn, n, mode, bit;
Magnus Damm73505b42007-08-12 15:26:12 +0900681 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900682
Magnus Dammd5190952010-02-09 04:29:22 +0000683 while (pr && enum_id && *reg_idx < desc->hw.nr_prio_regs) {
684 pr = desc->hw.prio_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900685
Magnus Dammd5190952010-02-09 04:29:22 +0000686 for (; *fld_idx < ARRAY_SIZE(pr->enum_ids); (*fld_idx)++) {
687 if (pr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900688 continue;
689
Magnus Damm73505b42007-08-12 15:26:12 +0900690 if (pr->set_reg && pr->clr_reg) {
691 fn = REG_FN_WRITE_BASE;
692 mode = MODE_PCLR_REG;
693 reg_e = pr->set_reg;
694 reg_d = pr->clr_reg;
695 } else {
696 fn = REG_FN_MODIFY_BASE;
697 mode = MODE_PRIO_REG;
698 if (!pr->set_reg)
699 BUG();
700 reg_e = pr->set_reg;
701 reg_d = pr->set_reg;
702 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900703
Magnus Damm73505b42007-08-12 15:26:12 +0900704 fn += (pr->reg_width >> 3) - 1;
Magnus Dammd5190952010-02-09 04:29:22 +0000705 n = *fld_idx + 1;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900706
Magnus Dammd5190952010-02-09 04:29:22 +0000707 BUG_ON(n * pr->field_width > pr->reg_width);
roel kluinb21a9102008-09-09 23:02:43 +0200708
Magnus Dammd5190952010-02-09 04:29:22 +0000709 bit = pr->reg_width - (n * pr->field_width);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900710
Magnus Damm73505b42007-08-12 15:26:12 +0900711 return _INTC_MK(fn, mode,
712 intc_get_reg(d, reg_e),
713 intc_get_reg(d, reg_d),
714 pr->field_width, bit);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900715 }
Magnus Dammd5190952010-02-09 04:29:22 +0000716
717 *fld_idx = 0;
718 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900719 }
720
Magnus Dammd5190952010-02-09 04:29:22 +0000721 return 0;
722}
723
724static unsigned int __init intc_prio_data(struct intc_desc *desc,
725 struct intc_desc_int *d,
726 intc_enum enum_id, int do_grps)
727{
728 unsigned int i = 0;
729 unsigned int j = 0;
730 unsigned int ret;
731
732 ret = _intc_prio_data(desc, d, enum_id, &i, &j);
733 if (ret)
734 return ret;
735
Magnus Damm680c4592007-07-20 12:09:29 +0900736 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900737 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900738
Magnus Damm02ab3f72007-07-18 17:25:09 +0900739 return 0;
740}
741
Magnus Dammd5190952010-02-09 04:29:22 +0000742static void __init intc_enable_disable_enum(struct intc_desc *desc,
743 struct intc_desc_int *d,
744 intc_enum enum_id, int enable)
745{
746 unsigned int i, j, data;
747
748 /* go through and enable/disable all mask bits */
749 i = j = 0;
750 do {
751 data = _intc_mask_data(desc, d, enum_id, &i, &j);
752 if (data)
753 intc_enable_disable(d, data, enable);
754 j++;
755 } while (data);
756
757 /* go through and enable/disable all priority fields */
758 i = j = 0;
759 do {
760 data = _intc_prio_data(desc, d, enum_id, &i, &j);
761 if (data)
762 intc_enable_disable(d, data, enable);
763
764 j++;
765 } while (data);
766}
767
Magnus Dammd58876e2008-04-24 21:36:34 +0900768static unsigned int __init intc_ack_data(struct intc_desc *desc,
769 struct intc_desc_int *d,
770 intc_enum enum_id)
771{
Magnus Damm577cd752010-02-09 04:24:46 +0000772 struct intc_mask_reg *mr = desc->hw.ack_regs;
Magnus Dammd58876e2008-04-24 21:36:34 +0900773 unsigned int i, j, fn, mode;
774 unsigned long reg_e, reg_d;
775
Magnus Damm577cd752010-02-09 04:24:46 +0000776 for (i = 0; mr && enum_id && i < desc->hw.nr_ack_regs; i++) {
777 mr = desc->hw.ack_regs + i;
Magnus Dammd58876e2008-04-24 21:36:34 +0900778
779 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
780 if (mr->enum_ids[j] != enum_id)
781 continue;
782
783 fn = REG_FN_MODIFY_BASE;
784 mode = MODE_ENABLE_REG;
785 reg_e = mr->set_reg;
786 reg_d = mr->set_reg;
787
788 fn += (mr->reg_width >> 3) - 1;
789 return _INTC_MK(fn, mode,
790 intc_get_reg(d, reg_e),
791 intc_get_reg(d, reg_d),
792 1,
793 (mr->reg_width - 1) - j);
794 }
795 }
796
797 return 0;
798}
Magnus Dammd58876e2008-04-24 21:36:34 +0900799
Magnus Damm73505b42007-08-12 15:26:12 +0900800static unsigned int __init intc_sense_data(struct intc_desc *desc,
801 struct intc_desc_int *d,
802 intc_enum enum_id)
803{
Magnus Damm577cd752010-02-09 04:24:46 +0000804 struct intc_sense_reg *sr = desc->hw.sense_regs;
Magnus Damm73505b42007-08-12 15:26:12 +0900805 unsigned int i, j, fn, bit;
806
Magnus Damm577cd752010-02-09 04:24:46 +0000807 for (i = 0; sr && enum_id && i < desc->hw.nr_sense_regs; i++) {
808 sr = desc->hw.sense_regs + i;
Magnus Damm73505b42007-08-12 15:26:12 +0900809
810 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
811 if (sr->enum_ids[j] != enum_id)
812 continue;
813
814 fn = REG_FN_MODIFY_BASE;
815 fn += (sr->reg_width >> 3) - 1;
Magnus Damm73505b42007-08-12 15:26:12 +0900816
roel kluinb21a9102008-09-09 23:02:43 +0200817 BUG_ON((j + 1) * sr->field_width > sr->reg_width);
818
819 bit = sr->reg_width - ((j + 1) * sr->field_width);
Magnus Damm73505b42007-08-12 15:26:12 +0900820
821 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
822 0, sr->field_width, bit);
823 }
824 }
825
826 return 0;
827}
828
829static void __init intc_register_irq(struct intc_desc *desc,
830 struct intc_desc_int *d,
831 intc_enum enum_id,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900832 unsigned int irq)
833{
Magnus Damm3d37d942007-08-17 00:50:44 +0900834 struct intc_handle_int *hp;
Magnus Damm680c4592007-07-20 12:09:29 +0900835 unsigned int data[2], primary;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900836
Paul Mundt1ce7b032009-11-02 10:30:26 +0900837 /*
838 * Register the IRQ position with the global IRQ map
839 */
840 set_bit(irq, intc_irq_map);
841
Paul Mundtdc825b12010-04-15 13:13:52 +0900842 /*
843 * Prefer single interrupt source bitmap over other combinations:
844 *
Magnus Damm680c4592007-07-20 12:09:29 +0900845 * 1. bitmap, single interrupt source
846 * 2. priority, single interrupt source
847 * 3. bitmap, multiple interrupt sources (groups)
848 * 4. priority, multiple interrupt sources (groups)
849 */
Magnus Damm73505b42007-08-12 15:26:12 +0900850 data[0] = intc_mask_data(desc, d, enum_id, 0);
851 data[1] = intc_prio_data(desc, d, enum_id, 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900852
853 primary = 0;
854 if (!data[0] && data[1])
855 primary = 1;
856
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900857 if (!data[0] && !data[1])
Paul Mundtf0335992009-03-06 17:56:58 +0900858 pr_warning("intc: missing unique irq mask for "
859 "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900860
Magnus Damm73505b42007-08-12 15:26:12 +0900861 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
862 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
Magnus Damm680c4592007-07-20 12:09:29 +0900863
864 if (!data[primary])
865 primary ^= 1;
866
867 BUG_ON(!data[primary]); /* must have primary masking method */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900868
869 disable_irq_nosync(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900870 set_irq_chip_and_handler_name(irq, &d->chip,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900871 handle_level_irq, "level");
Magnus Damm680c4592007-07-20 12:09:29 +0900872 set_irq_chip_data(irq, (void *)data[primary]);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900873
Paul Mundtdc825b12010-04-15 13:13:52 +0900874 /*
875 * set priority level
Magnus Damm7f3edee2008-01-10 14:08:55 +0900876 * - this needs to be at least 2 for 5-bit priorities on 7780
877 */
Paul Mundt43b87742010-04-13 14:43:03 +0900878 intc_prio_level[irq] = default_prio_level;
Magnus Damm73505b42007-08-12 15:26:12 +0900879
Magnus Damm680c4592007-07-20 12:09:29 +0900880 /* enable secondary masking method if present */
881 if (data[!primary])
Magnus Damm73505b42007-08-12 15:26:12 +0900882 _intc_enable(irq, data[!primary]);
883
884 /* add irq to d->prio list if priority is available */
885 if (data[1]) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900886 hp = d->prio + d->nr_prio;
887 hp->irq = irq;
888 hp->handle = data[1];
889
890 if (primary) {
891 /*
892 * only secondary priority should access registers, so
893 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
894 */
Magnus Damm3d37d942007-08-17 00:50:44 +0900895 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
896 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
897 }
Magnus Damm73505b42007-08-12 15:26:12 +0900898 d->nr_prio++;
899 }
900
901 /* add irq to d->sense list if sense is available */
902 data[0] = intc_sense_data(desc, d, enum_id);
903 if (data[0]) {
904 (d->sense + d->nr_sense)->irq = irq;
905 (d->sense + d->nr_sense)->handle = data[0];
906 d->nr_sense++;
907 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900908
909 /* irq should be disabled by default */
Magnus Damm73505b42007-08-12 15:26:12 +0900910 d->chip.mask(irq);
Magnus Dammd58876e2008-04-24 21:36:34 +0900911
Magnus Damm577cd752010-02-09 04:24:46 +0000912 if (desc->hw.ack_regs)
Magnus Dammd58876e2008-04-24 21:36:34 +0900913 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
Magnus Damm65a5b282010-02-05 11:15:25 +0000914
Paul Mundtdc825b12010-04-15 13:13:52 +0900915#ifdef CONFIG_INTC_BALANCING
916 if (desc->hw.mask_regs)
917 dist_handle[irq] = intc_dist_data(desc, d, enum_id);
918#endif
919
Magnus Damm65a5b282010-02-05 11:15:25 +0000920#ifdef CONFIG_ARM
921 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
922#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900923}
924
Magnus Dammf18d5332007-09-21 18:16:42 +0900925static unsigned int __init save_reg(struct intc_desc_int *d,
926 unsigned int cnt,
927 unsigned long value,
928 unsigned int smp)
929{
930 if (value) {
Magnus Dammdec710b2010-03-19 16:48:01 +0900931 value = intc_phys_to_virt(d, value);
932
Magnus Dammf18d5332007-09-21 18:16:42 +0900933 d->reg[cnt] = value;
934#ifdef CONFIG_SMP
935 d->smp[cnt] = smp;
936#endif
937 return 1;
938 }
939
940 return 0;
941}
942
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900943static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900944{
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900945 generic_handle_irq((unsigned int)get_irq_data(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900946}
Magnus Dammf18d5332007-09-21 18:16:42 +0900947
Magnus Damm01e96512010-03-10 09:31:01 +0000948int __init register_intc_controller(struct intc_desc *desc)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900949{
Paul Mundt54ff3282009-06-11 10:33:09 +0300950 unsigned int i, k, smp;
Magnus Damm577cd752010-02-09 04:24:46 +0000951 struct intc_hw_desc *hw = &desc->hw;
Magnus Damm73505b42007-08-12 15:26:12 +0900952 struct intc_desc_int *d;
Magnus Dammdec710b2010-03-19 16:48:01 +0900953 struct resource *res;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900954
Paul Mundt12129fe2010-04-13 13:49:54 +0900955 pr_info("intc: Registered controller '%s' with %u IRQs\n",
956 desc->name, hw->nr_vectors);
957
Paul Mundt11b6aa92009-06-12 01:34:12 +0300958 d = kzalloc(sizeof(*d), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000959 if (!d)
960 goto err0;
Magnus Damm73505b42007-08-12 15:26:12 +0900961
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000962 INIT_LIST_HEAD(&d->list);
963 list_add(&d->list, &intc_list);
964
Magnus Dammdec710b2010-03-19 16:48:01 +0900965 if (desc->num_resources) {
966 d->nr_windows = desc->num_resources;
967 d->window = kzalloc(d->nr_windows * sizeof(*d->window),
968 GFP_NOWAIT);
969 if (!d->window)
970 goto err1;
971
972 for (k = 0; k < d->nr_windows; k++) {
973 res = desc->resource + k;
974 WARN_ON(resource_type(res) != IORESOURCE_MEM);
975 d->window[k].phys = res->start;
976 d->window[k].size = resource_size(res);
977 d->window[k].virt = ioremap_nocache(res->start,
978 resource_size(res));
979 if (!d->window[k].virt)
980 goto err2;
981 }
982 }
983
Magnus Damm577cd752010-02-09 04:24:46 +0000984 d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
Paul Mundtdc825b12010-04-15 13:13:52 +0900985#ifdef CONFIG_INTC_BALANCING
986 if (d->nr_reg)
987 d->nr_reg += hw->nr_mask_regs;
988#endif
Magnus Damm577cd752010-02-09 04:24:46 +0000989 d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
990 d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
991 d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
Paul Mundt9b798d52009-10-27 11:36:43 +0900992
Paul Mundt11b6aa92009-06-12 01:34:12 +0300993 d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000994 if (!d->reg)
Magnus Dammdec710b2010-03-19 16:48:01 +0900995 goto err2;
Magnus Damm01e96512010-03-10 09:31:01 +0000996
Magnus Dammf18d5332007-09-21 18:16:42 +0900997#ifdef CONFIG_SMP
Paul Mundt11b6aa92009-06-12 01:34:12 +0300998 d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000999 if (!d->smp)
Magnus Dammdec710b2010-03-19 16:48:01 +09001000 goto err3;
Magnus Dammf18d5332007-09-21 18:16:42 +09001001#endif
Magnus Damm73505b42007-08-12 15:26:12 +09001002 k = 0;
1003
Magnus Damm577cd752010-02-09 04:24:46 +00001004 if (hw->mask_regs) {
1005 for (i = 0; i < hw->nr_mask_regs; i++) {
1006 smp = IS_SMP(hw->mask_regs[i]);
1007 k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
1008 k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
Paul Mundtdc825b12010-04-15 13:13:52 +09001009#ifdef CONFIG_INTC_BALANCING
1010 k += save_reg(d, k, hw->mask_regs[i].dist_reg, 0);
1011#endif
Magnus Damm73505b42007-08-12 15:26:12 +09001012 }
1013 }
1014
Magnus Damm577cd752010-02-09 04:24:46 +00001015 if (hw->prio_regs) {
1016 d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
1017 GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +00001018 if (!d->prio)
Magnus Dammdec710b2010-03-19 16:48:01 +09001019 goto err4;
Magnus Damm73505b42007-08-12 15:26:12 +09001020
Magnus Damm577cd752010-02-09 04:24:46 +00001021 for (i = 0; i < hw->nr_prio_regs; i++) {
1022 smp = IS_SMP(hw->prio_regs[i]);
1023 k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
1024 k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +09001025 }
1026 }
1027
Magnus Damm577cd752010-02-09 04:24:46 +00001028 if (hw->sense_regs) {
1029 d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
1030 GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +00001031 if (!d->sense)
Magnus Dammdec710b2010-03-19 16:48:01 +09001032 goto err5;
Magnus Damm73505b42007-08-12 15:26:12 +09001033
Magnus Damm577cd752010-02-09 04:24:46 +00001034 for (i = 0; i < hw->nr_sense_regs; i++)
1035 k += save_reg(d, k, hw->sense_regs[i].reg, 0);
Magnus Damm73505b42007-08-12 15:26:12 +09001036 }
1037
Magnus Damm73505b42007-08-12 15:26:12 +09001038 d->chip.name = desc->name;
1039 d->chip.mask = intc_disable;
1040 d->chip.unmask = intc_enable;
1041 d->chip.mask_ack = intc_disable;
Magnus Dammf7dd2542009-04-01 14:20:58 +00001042 d->chip.enable = intc_enable;
1043 d->chip.disable = intc_disable;
1044 d->chip.shutdown = intc_disable;
Magnus Damm73505b42007-08-12 15:26:12 +09001045 d->chip.set_type = intc_set_sense;
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001046 d->chip.set_wake = intc_set_wake;
Paul Mundta8941da2010-03-08 13:33:17 +09001047#ifdef CONFIG_SMP
1048 d->chip.set_affinity = intc_set_affinity;
1049#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +09001050
Magnus Damm577cd752010-02-09 04:24:46 +00001051 if (hw->ack_regs) {
1052 for (i = 0; i < hw->nr_ack_regs; i++)
1053 k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
Magnus Dammd58876e2008-04-24 21:36:34 +09001054
1055 d->chip.mask_ack = intc_mask_ack;
1056 }
Magnus Dammd58876e2008-04-24 21:36:34 +09001057
Magnus Dammd85429a2010-02-15 11:40:25 +00001058 /* disable bits matching force_disable before registering irqs */
1059 if (desc->force_disable)
1060 intc_enable_disable_enum(desc, d, desc->force_disable, 0);
Magnus Dammd5190952010-02-09 04:29:22 +00001061
1062 /* disable bits matching force_enable before registering irqs */
1063 if (desc->force_enable)
1064 intc_enable_disable_enum(desc, d, desc->force_enable, 0);
1065
Magnus Dammd58876e2008-04-24 21:36:34 +09001066 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
1067
Magnus Dammbdaa6e82009-02-24 22:58:57 +09001068 /* register the vectors one by one */
Magnus Damm577cd752010-02-09 04:24:46 +00001069 for (i = 0; i < hw->nr_vectors; i++) {
1070 struct intc_vect *vect = hw->vectors + i;
Paul Mundt05ff3002009-05-22 01:28:33 +09001071 unsigned int irq = evt2irq(vect->vect);
1072 struct irq_desc *irq_desc;
Paul Mundt54ff3282009-06-11 10:33:09 +03001073
Magnus Dammbdaa6e82009-02-24 22:58:57 +09001074 if (!vect->enum_id)
1075 continue;
Magnus Damm02ab3f72007-07-18 17:25:09 +09001076
Paul Mundt54ff3282009-06-11 10:33:09 +03001077 irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
Paul Mundt05ff3002009-05-22 01:28:33 +09001078 if (unlikely(!irq_desc)) {
Paul Mundt12129fe2010-04-13 13:49:54 +09001079 pr_err("can't get irq_desc for %d\n", irq);
Paul Mundt05ff3002009-05-22 01:28:33 +09001080 continue;
1081 }
1082
1083 intc_register_irq(desc, d, vect->enum_id, irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +09001084
Magnus Damm577cd752010-02-09 04:24:46 +00001085 for (k = i + 1; k < hw->nr_vectors; k++) {
1086 struct intc_vect *vect2 = hw->vectors + k;
Pawel Moll05ecd5a2009-08-24 19:52:38 +09001087 unsigned int irq2 = evt2irq(vect2->vect);
1088
1089 if (vect->enum_id != vect2->enum_id)
1090 continue;
1091
Paul Mundt1279b7f2009-08-31 15:15:33 +09001092 /*
1093 * In the case of multi-evt handling and sparse
1094 * IRQ support, each vector still needs to have
1095 * its own backing irq_desc.
1096 */
1097 irq_desc = irq_to_desc_alloc_node(irq2, numa_node_id());
1098 if (unlikely(!irq_desc)) {
Paul Mundt12129fe2010-04-13 13:49:54 +09001099 pr_err("can't get irq_desc for %d\n", irq2);
Paul Mundt1279b7f2009-08-31 15:15:33 +09001100 continue;
1101 }
1102
Pawel Moll05ecd5a2009-08-24 19:52:38 +09001103 vect2->enum_id = 0;
1104
1105 /* redirect this interrupts to the first one */
Paul Mundt4d2185d2010-02-17 12:37:42 +09001106 set_irq_chip(irq2, &dummy_irq_chip);
Magnus Damme6f07752010-02-09 07:17:20 +00001107 set_irq_chained_handler(irq2, intc_redirect_irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +09001108 set_irq_data(irq2, (void *)irq);
1109 }
Magnus Damm02ab3f72007-07-18 17:25:09 +09001110 }
Magnus Dammd5190952010-02-09 04:29:22 +00001111
1112 /* enable bits matching force_enable after registering irqs */
1113 if (desc->force_enable)
1114 intc_enable_disable_enum(desc, d, desc->force_enable, 1);
Magnus Damm01e96512010-03-10 09:31:01 +00001115
1116 return 0;
Magnus Dammdec710b2010-03-19 16:48:01 +09001117err5:
Magnus Damm01e96512010-03-10 09:31:01 +00001118 kfree(d->prio);
Magnus Dammdec710b2010-03-19 16:48:01 +09001119err4:
Magnus Damm01e96512010-03-10 09:31:01 +00001120#ifdef CONFIG_SMP
1121 kfree(d->smp);
Magnus Dammdec710b2010-03-19 16:48:01 +09001122err3:
Magnus Damm01e96512010-03-10 09:31:01 +00001123#endif
1124 kfree(d->reg);
Magnus Dammdec710b2010-03-19 16:48:01 +09001125err2:
1126 for (k = 0; k < d->nr_windows; k++)
1127 if (d->window[k].virt)
1128 iounmap(d->window[k].virt);
1129
1130 kfree(d->window);
1131err1:
Magnus Damm01e96512010-03-10 09:31:01 +00001132 kfree(d);
Magnus Dammdec710b2010-03-19 16:48:01 +09001133err0:
Magnus Damm01e96512010-03-10 09:31:01 +00001134 pr_err("unable to allocate INTC memory\n");
1135
1136 return -ENOMEM;
Magnus Damm02ab3f72007-07-18 17:25:09 +09001137}
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001138
Paul Mundt43b87742010-04-13 14:43:03 +09001139#ifdef CONFIG_INTC_USERIMASK
1140static void __iomem *uimask;
1141
1142int register_intc_userimask(unsigned long addr)
1143{
1144 if (unlikely(uimask))
1145 return -EBUSY;
1146
1147 uimask = ioremap_nocache(addr, SZ_4K);
1148 if (unlikely(!uimask))
1149 return -ENOMEM;
1150
1151 pr_info("intc: userimask support registered for levels 0 -> %d\n",
1152 default_prio_level - 1);
1153
1154 return 0;
1155}
1156
1157static ssize_t
1158show_intc_userimask(struct sysdev_class *cls,
1159 struct sysdev_class_attribute *attr, char *buf)
1160{
1161 return sprintf(buf, "%d\n", (__raw_readl(uimask) >> 4) & 0xf);
1162}
1163
1164static ssize_t
1165store_intc_userimask(struct sysdev_class *cls,
1166 struct sysdev_class_attribute *attr,
1167 const char *buf, size_t count)
1168{
1169 unsigned long level;
1170
1171 level = simple_strtoul(buf, NULL, 10);
1172
1173 /*
1174 * Minimal acceptable IRQ levels are in the 2 - 16 range, but
1175 * these are chomped so as to not interfere with normal IRQs.
1176 *
1177 * Level 1 is a special case on some CPUs in that it's not
1178 * directly settable, but given that USERIMASK cuts off below a
1179 * certain level, we don't care about this limitation here.
1180 * Level 0 on the other hand equates to user masking disabled.
1181 *
1182 * We use default_prio_level as a cut off so that only special
1183 * case opt-in IRQs can be mangled.
1184 */
1185 if (level >= default_prio_level)
1186 return -EINVAL;
1187
1188 __raw_writel(0xa5 << 24 | level << 4, uimask);
1189
1190 return count;
1191}
1192
1193static SYSDEV_CLASS_ATTR(userimask, S_IRUSR | S_IWUSR,
1194 show_intc_userimask, store_intc_userimask);
1195#endif
1196
Paul Mundt0ded7542010-04-13 10:16:34 +09001197static ssize_t
1198show_intc_name(struct sys_device *dev, struct sysdev_attribute *attr, char *buf)
1199{
1200 struct intc_desc_int *d;
1201
1202 d = container_of(dev, struct intc_desc_int, sysdev);
1203
1204 return sprintf(buf, "%s\n", d->chip.name);
1205}
1206
1207static SYSDEV_ATTR(name, S_IRUGO, show_intc_name, NULL);
1208
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001209static int intc_suspend(struct sys_device *dev, pm_message_t state)
1210{
1211 struct intc_desc_int *d;
1212 struct irq_desc *desc;
1213 int irq;
1214
1215 /* get intc controller associated with this sysdev */
1216 d = container_of(dev, struct intc_desc_int, sysdev);
1217
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001218 switch (state.event) {
1219 case PM_EVENT_ON:
1220 if (d->state.event != PM_EVENT_FREEZE)
1221 break;
1222 for_each_irq_desc(irq, desc) {
Francesco VIRLINZI87a705d2009-12-04 08:57:58 +00001223 if (desc->handle_irq == intc_redirect_irq)
Paul Mundt0a753d52009-12-09 14:36:16 +09001224 continue;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001225 if (desc->chip != &d->chip)
1226 continue;
1227 if (desc->status & IRQ_DISABLED)
1228 intc_disable(irq);
1229 else
1230 intc_enable(irq);
1231 }
1232 break;
1233 case PM_EVENT_FREEZE:
1234 /* nothing has to be done */
1235 break;
1236 case PM_EVENT_SUSPEND:
1237 /* enable wakeup irqs belonging to this intc controller */
1238 for_each_irq_desc(irq, desc) {
1239 if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
1240 intc_enable(irq);
1241 }
1242 break;
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001243 }
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001244 d->state = state;
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001245
1246 return 0;
1247}
1248
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001249static int intc_resume(struct sys_device *dev)
1250{
1251 return intc_suspend(dev, PMSG_ON);
1252}
1253
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001254static struct sysdev_class intc_sysdev_class = {
1255 .name = "intc",
1256 .suspend = intc_suspend,
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001257 .resume = intc_resume,
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001258};
1259
1260/* register this intc as sysdev to allow suspend/resume */
1261static int __init register_intc_sysdevs(void)
1262{
1263 struct intc_desc_int *d;
1264 int error;
1265 int id = 0;
1266
1267 error = sysdev_class_register(&intc_sysdev_class);
Paul Mundt43b87742010-04-13 14:43:03 +09001268#ifdef CONFIG_INTC_USERIMASK
1269 if (!error && uimask)
1270 error = sysdev_class_create_file(&intc_sysdev_class,
1271 &attr_userimask);
1272#endif
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001273 if (!error) {
1274 list_for_each_entry(d, &intc_list, list) {
1275 d->sysdev.id = id;
1276 d->sysdev.cls = &intc_sysdev_class;
1277 error = sysdev_register(&d->sysdev);
Paul Mundt0ded7542010-04-13 10:16:34 +09001278 if (error == 0)
1279 error = sysdev_create_file(&d->sysdev,
1280 &attr_name);
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001281 if (error)
1282 break;
Paul Mundt0ded7542010-04-13 10:16:34 +09001283
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001284 id++;
1285 }
1286 }
1287
1288 if (error)
Paul Mundt12129fe2010-04-13 13:49:54 +09001289 pr_err("intc: sysdev registration error\n");
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001290
1291 return error;
1292}
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001293device_initcall(register_intc_sysdevs);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001294
1295/*
1296 * Dynamic IRQ allocation and deallocation
1297 */
Paul Mundte9867c52010-02-02 17:35:13 +09001298unsigned int create_irq_nr(unsigned int irq_want, int node)
Paul Mundt1ce7b032009-11-02 10:30:26 +09001299{
1300 unsigned int irq = 0, new;
1301 unsigned long flags;
1302 struct irq_desc *desc;
1303
1304 spin_lock_irqsave(&vector_lock, flags);
1305
1306 /*
Paul Mundte9867c52010-02-02 17:35:13 +09001307 * First try the wanted IRQ
Paul Mundt1ce7b032009-11-02 10:30:26 +09001308 */
Paul Mundte9867c52010-02-02 17:35:13 +09001309 if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
1310 new = irq_want;
1311 } else {
1312 /* .. then fall back to scanning. */
Paul Mundt1ce7b032009-11-02 10:30:26 +09001313 new = find_first_zero_bit(intc_irq_map, nr_irqs);
1314 if (unlikely(new == nr_irqs))
1315 goto out_unlock;
1316
Paul Mundt1ce7b032009-11-02 10:30:26 +09001317 __set_bit(new, intc_irq_map);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001318 }
1319
Paul Mundte9867c52010-02-02 17:35:13 +09001320 desc = irq_to_desc_alloc_node(new, node);
1321 if (unlikely(!desc)) {
Paul Mundt12129fe2010-04-13 13:49:54 +09001322 pr_err("can't get irq_desc for %d\n", new);
Paul Mundte9867c52010-02-02 17:35:13 +09001323 goto out_unlock;
1324 }
1325
1326 desc = move_irq_desc(desc, node);
1327 irq = new;
1328
Paul Mundt1ce7b032009-11-02 10:30:26 +09001329out_unlock:
1330 spin_unlock_irqrestore(&vector_lock, flags);
1331
Magnus Damm65a5b282010-02-05 11:15:25 +00001332 if (irq > 0) {
Paul Mundt1ce7b032009-11-02 10:30:26 +09001333 dynamic_irq_init(irq);
Magnus Damm65a5b282010-02-05 11:15:25 +00001334#ifdef CONFIG_ARM
1335 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
1336#endif
1337 }
Paul Mundt1ce7b032009-11-02 10:30:26 +09001338
1339 return irq;
1340}
1341
1342int create_irq(void)
1343{
1344 int nid = cpu_to_node(smp_processor_id());
1345 int irq;
1346
Paul Mundte9867c52010-02-02 17:35:13 +09001347 irq = create_irq_nr(NR_IRQS_LEGACY, nid);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001348 if (irq == 0)
1349 irq = -1;
1350
1351 return irq;
1352}
1353
1354void destroy_irq(unsigned int irq)
1355{
1356 unsigned long flags;
1357
1358 dynamic_irq_cleanup(irq);
1359
1360 spin_lock_irqsave(&vector_lock, flags);
1361 __clear_bit(irq, intc_irq_map);
1362 spin_unlock_irqrestore(&vector_lock, flags);
1363}
Paul Mundt45b9dea2009-11-02 15:43:20 +09001364
1365int reserve_irq_vector(unsigned int irq)
1366{
1367 unsigned long flags;
1368 int ret = 0;
1369
1370 spin_lock_irqsave(&vector_lock, flags);
1371 if (test_and_set_bit(irq, intc_irq_map))
1372 ret = -EBUSY;
1373 spin_unlock_irqrestore(&vector_lock, flags);
1374
1375 return ret;
1376}
1377
1378void reserve_irq_legacy(void)
1379{
1380 unsigned long flags;
1381 int i, j;
1382
1383 spin_lock_irqsave(&vector_lock, flags);
1384 j = find_first_bit(intc_irq_map, nr_irqs);
1385 for (i = 0; i < j; i++)
1386 __set_bit(i, intc_irq_map);
1387 spin_unlock_irqrestore(&vector_lock, flags);
1388}