blob: dcb4c833820bd92db4fe23bba82c45bd42103ce2 [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>
23#include <linux/interrupt.h>
Paul Mundtbbfbd8b2008-10-01 16:13:54 +090024#include <linux/sh_intc.h>
Magnus Damm2dcec7a2009-04-01 14:30:59 +000025#include <linux/sysdev.h>
26#include <linux/list.h>
Paul Mundt54ff3282009-06-11 10:33:09 +030027#include <linux/topology.h>
Paul Mundt1ce7b032009-11-02 10:30:26 +090028#include <linux/bitmap.h>
Paul Mundta8941da2010-03-08 13:33:17 +090029#include <linux/cpumask.h>
Paul Mundt43b87742010-04-13 14:43:03 +090030#include <asm/sizes.h>
Magnus Damm02ab3f72007-07-18 17:25:09 +090031
Magnus Damm73505b42007-08-12 15:26:12 +090032#define _INTC_MK(fn, mode, addr_e, addr_d, width, shift) \
33 ((shift) | ((width) << 5) | ((fn) << 9) | ((mode) << 13) | \
34 ((addr_e) << 16) | ((addr_d << 24)))
Magnus Damm02ab3f72007-07-18 17:25:09 +090035
Magnus Damm73505b42007-08-12 15:26:12 +090036#define _INTC_SHIFT(h) (h & 0x1f)
37#define _INTC_WIDTH(h) ((h >> 5) & 0xf)
38#define _INTC_FN(h) ((h >> 9) & 0xf)
39#define _INTC_MODE(h) ((h >> 13) & 0x7)
40#define _INTC_ADDR_E(h) ((h >> 16) & 0xff)
41#define _INTC_ADDR_D(h) ((h >> 24) & 0xff)
Magnus Damm02ab3f72007-07-18 17:25:09 +090042
Magnus Damm73505b42007-08-12 15:26:12 +090043struct intc_handle_int {
44 unsigned int irq;
45 unsigned long handle;
46};
47
Magnus Dammdec710b2010-03-19 16:48:01 +090048struct intc_window {
49 phys_addr_t phys;
50 void __iomem *virt;
51 unsigned long size;
52};
53
Magnus Damm73505b42007-08-12 15:26:12 +090054struct intc_desc_int {
Magnus Damm2dcec7a2009-04-01 14:30:59 +000055 struct list_head list;
56 struct sys_device sysdev;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +000057 pm_message_t state;
Magnus Damm73505b42007-08-12 15:26:12 +090058 unsigned long *reg;
Magnus Dammf18d5332007-09-21 18:16:42 +090059#ifdef CONFIG_SMP
60 unsigned long *smp;
61#endif
Magnus Damm73505b42007-08-12 15:26:12 +090062 unsigned int nr_reg;
63 struct intc_handle_int *prio;
64 unsigned int nr_prio;
65 struct intc_handle_int *sense;
66 unsigned int nr_sense;
Magnus Dammdec710b2010-03-19 16:48:01 +090067 struct intc_window *window;
68 unsigned int nr_windows;
Magnus Damm73505b42007-08-12 15:26:12 +090069 struct irq_chip chip;
70};
71
Magnus Damm2dcec7a2009-04-01 14:30:59 +000072static LIST_HEAD(intc_list);
73
Paul Mundt1ce7b032009-11-02 10:30:26 +090074/*
75 * The intc_irq_map provides a global map of bound IRQ vectors for a
76 * given platform. Allocation of IRQs are either static through the CPU
77 * vector map, or dynamic in the case of board mux vectors or MSI.
78 *
79 * As this is a central point for all IRQ controllers on the system,
80 * each of the available sources are mapped out here. This combined with
81 * sparseirq makes it quite trivial to keep the vector map tightly packed
82 * when dynamically creating IRQs, as well as tying in to otherwise
83 * unused irq_desc positions in the sparse array.
84 */
85static DECLARE_BITMAP(intc_irq_map, NR_IRQS);
86static DEFINE_SPINLOCK(vector_lock);
87
Magnus Dammf18d5332007-09-21 18:16:42 +090088#ifdef CONFIG_SMP
89#define IS_SMP(x) x.smp
90#define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
91#define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
92#else
93#define IS_SMP(x) 0
94#define INTC_REG(d, x, c) (d->reg[(x)])
95#define SMP_NR(d, x) 1
96#endif
97
Paul Mundt43b87742010-04-13 14:43:03 +090098static unsigned int intc_prio_level[NR_IRQS]; /* for now */
99static unsigned int default_prio_level = 2; /* 2 - 16 */
Magnus Dammd58876e2008-04-24 21:36:34 +0900100static unsigned long ack_handle[NR_IRQS];
Paul Mundtdc825b12010-04-15 13:13:52 +0900101#ifdef CONFIG_INTC_BALANCING
102static unsigned long dist_handle[NR_IRQS];
103#endif
Magnus Damm73505b42007-08-12 15:26:12 +0900104
105static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900106{
107 struct irq_chip *chip = get_irq_chip(irq);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900108 return container_of(chip, struct intc_desc_int, chip);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900109}
110
Paul Mundtdc825b12010-04-15 13:13:52 +0900111static unsigned long intc_phys_to_virt(struct intc_desc_int *d,
112 unsigned long address)
113{
114 struct intc_window *window;
115 int k;
116
117 /* scan through physical windows and convert address */
118 for (k = 0; k < d->nr_windows; k++) {
119 window = d->window + k;
120
121 if (address < window->phys)
122 continue;
123
124 if (address >= (window->phys + window->size))
125 continue;
126
127 address -= window->phys;
128 address += (unsigned long)window->virt;
129
130 return address;
131 }
132
133 /* no windows defined, register must be 1:1 mapped virt:phys */
134 return address;
135}
136
137static unsigned int intc_get_reg(struct intc_desc_int *d, unsigned long address)
138{
139 unsigned int k;
140
141 address = intc_phys_to_virt(d, address);
142
143 for (k = 0; k < d->nr_reg; k++) {
144 if (d->reg[k] == address)
145 return k;
146 }
147
148 BUG();
149 return 0;
150}
151
Magnus Damm02ab3f72007-07-18 17:25:09 +0900152static inline unsigned int set_field(unsigned int value,
153 unsigned int field_value,
Magnus Damm73505b42007-08-12 15:26:12 +0900154 unsigned int handle)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900155{
Magnus Damm73505b42007-08-12 15:26:12 +0900156 unsigned int width = _INTC_WIDTH(handle);
157 unsigned int shift = _INTC_SHIFT(handle);
158
Magnus Damm02ab3f72007-07-18 17:25:09 +0900159 value &= ~(((1 << width) - 1) << shift);
160 value |= field_value << shift;
161 return value;
162}
163
Magnus Damm73505b42007-08-12 15:26:12 +0900164static void write_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900165{
Paul Mundt62429e02008-10-01 15:19:10 +0900166 __raw_writeb(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900167 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900168}
169
Magnus Damm73505b42007-08-12 15:26:12 +0900170static void write_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900171{
Paul Mundt62429e02008-10-01 15:19:10 +0900172 __raw_writew(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900173 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900174}
175
Magnus Damm73505b42007-08-12 15:26:12 +0900176static void write_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900177{
Paul Mundt62429e02008-10-01 15:19:10 +0900178 __raw_writel(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900179 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900180}
181
Magnus Damm73505b42007-08-12 15:26:12 +0900182static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900183{
Magnus Damm4370fe12008-04-24 21:53:07 +0900184 unsigned long flags;
185 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900186 __raw_writeb(set_field(__raw_readb(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900187 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900188 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900189}
190
Magnus Damm73505b42007-08-12 15:26:12 +0900191static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900192{
Magnus Damm4370fe12008-04-24 21:53:07 +0900193 unsigned long flags;
194 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900195 __raw_writew(set_field(__raw_readw(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900196 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900197 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900198}
199
Magnus Damm73505b42007-08-12 15:26:12 +0900200static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900201{
Magnus Damm4370fe12008-04-24 21:53:07 +0900202 unsigned long flags;
203 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900204 __raw_writel(set_field(__raw_readl(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900205 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900206 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900207}
208
Magnus Damm73505b42007-08-12 15:26:12 +0900209enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
Magnus Damm02ab3f72007-07-18 17:25:09 +0900210
Magnus Damm73505b42007-08-12 15:26:12 +0900211static void (*intc_reg_fns[])(unsigned long addr,
212 unsigned long h,
213 unsigned long data) = {
214 [REG_FN_WRITE_BASE + 0] = write_8,
215 [REG_FN_WRITE_BASE + 1] = write_16,
216 [REG_FN_WRITE_BASE + 3] = write_32,
217 [REG_FN_MODIFY_BASE + 0] = modify_8,
218 [REG_FN_MODIFY_BASE + 1] = modify_16,
219 [REG_FN_MODIFY_BASE + 3] = modify_32,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900220};
221
Magnus Damm73505b42007-08-12 15:26:12 +0900222enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
223 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
224 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
225 MODE_PRIO_REG, /* Priority value written to enable interrupt */
226 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
227};
228
229static void intc_mode_field(unsigned long addr,
230 unsigned long handle,
231 void (*fn)(unsigned long,
232 unsigned long,
233 unsigned long),
234 unsigned int irq)
235{
236 fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
237}
238
239static void intc_mode_zero(unsigned long addr,
240 unsigned long handle,
241 void (*fn)(unsigned long,
242 unsigned long,
243 unsigned long),
244 unsigned int irq)
245{
246 fn(addr, handle, 0);
247}
248
249static void intc_mode_prio(unsigned long addr,
250 unsigned long handle,
251 void (*fn)(unsigned long,
252 unsigned long,
253 unsigned long),
254 unsigned int irq)
255{
256 fn(addr, handle, intc_prio_level[irq]);
257}
258
259static void (*intc_enable_fns[])(unsigned long addr,
260 unsigned long handle,
261 void (*fn)(unsigned long,
262 unsigned long,
263 unsigned long),
264 unsigned int irq) = {
265 [MODE_ENABLE_REG] = intc_mode_field,
266 [MODE_MASK_REG] = intc_mode_zero,
267 [MODE_DUAL_REG] = intc_mode_field,
268 [MODE_PRIO_REG] = intc_mode_prio,
269 [MODE_PCLR_REG] = intc_mode_prio,
270};
271
272static void (*intc_disable_fns[])(unsigned long addr,
273 unsigned long handle,
274 void (*fn)(unsigned long,
275 unsigned long,
276 unsigned long),
277 unsigned int irq) = {
278 [MODE_ENABLE_REG] = intc_mode_zero,
279 [MODE_MASK_REG] = intc_mode_field,
280 [MODE_DUAL_REG] = intc_mode_field,
281 [MODE_PRIO_REG] = intc_mode_zero,
282 [MODE_PCLR_REG] = intc_mode_field,
283};
284
Paul Mundtdc825b12010-04-15 13:13:52 +0900285#ifdef CONFIG_INTC_BALANCING
286static inline void intc_balancing_enable(unsigned int irq)
287{
288 struct intc_desc_int *d = get_intc_desc(irq);
289 unsigned long handle = dist_handle[irq];
290 unsigned long addr;
291
292 if (irq_balancing_disabled(irq) || !handle)
293 return;
294
295 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
296 intc_reg_fns[_INTC_FN(handle)](addr, handle, 1);
297}
298
299static inline void intc_balancing_disable(unsigned int irq)
300{
301 struct intc_desc_int *d = get_intc_desc(irq);
302 unsigned long handle = dist_handle[irq];
303 unsigned long addr;
304
305 if (irq_balancing_disabled(irq) || !handle)
306 return;
307
308 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
309 intc_reg_fns[_INTC_FN(handle)](addr, handle, 0);
310}
311
312static unsigned int intc_dist_data(struct intc_desc *desc,
313 struct intc_desc_int *d,
314 intc_enum enum_id)
315{
316 struct intc_mask_reg *mr = desc->hw.mask_regs;
317 unsigned int i, j, fn, mode;
318 unsigned long reg_e, reg_d;
319
320 for (i = 0; mr && enum_id && i < desc->hw.nr_mask_regs; i++) {
321 mr = desc->hw.mask_regs + i;
322
323 /*
324 * Skip this entry if there's no auto-distribution
325 * register associated with it.
326 */
327 if (!mr->dist_reg)
328 continue;
329
330 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
331 if (mr->enum_ids[j] != enum_id)
332 continue;
333
334 fn = REG_FN_MODIFY_BASE;
335 mode = MODE_ENABLE_REG;
336 reg_e = mr->dist_reg;
337 reg_d = mr->dist_reg;
338
339 fn += (mr->reg_width >> 3) - 1;
340 return _INTC_MK(fn, mode,
341 intc_get_reg(d, reg_e),
342 intc_get_reg(d, reg_d),
343 1,
344 (mr->reg_width - 1) - j);
345 }
346 }
347
348 /*
349 * It's possible we've gotten here with no distribution options
350 * available for the IRQ in question, so we just skip over those.
351 */
352 return 0;
353}
354#else
355static inline void intc_balancing_enable(unsigned int irq)
356{
357}
358
359static inline void intc_balancing_disable(unsigned int irq)
360{
361}
362#endif
363
Magnus Damm73505b42007-08-12 15:26:12 +0900364static inline void _intc_enable(unsigned int irq, unsigned long handle)
365{
366 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900367 unsigned long addr;
368 unsigned int cpu;
Magnus Damm73505b42007-08-12 15:26:12 +0900369
Magnus Dammf18d5332007-09-21 18:16:42 +0900370 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
Paul Mundta8941da2010-03-08 13:33:17 +0900371#ifdef CONFIG_SMP
372 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
373 continue;
374#endif
Magnus Dammf18d5332007-09-21 18:16:42 +0900375 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
376 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
377 [_INTC_FN(handle)], irq);
378 }
Paul Mundtdc825b12010-04-15 13:13:52 +0900379
380 intc_balancing_enable(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900381}
382
Magnus Damm02ab3f72007-07-18 17:25:09 +0900383static void intc_enable(unsigned int irq)
384{
Magnus Damm73505b42007-08-12 15:26:12 +0900385 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
Magnus Damm02ab3f72007-07-18 17:25:09 +0900386}
387
388static void intc_disable(unsigned int irq)
389{
Magnus Dammf18d5332007-09-21 18:16:42 +0900390 struct intc_desc_int *d = get_intc_desc(irq);
Paul Mundtdc825b12010-04-15 13:13:52 +0900391 unsigned long handle = (unsigned long)get_irq_chip_data(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900392 unsigned long addr;
393 unsigned int cpu;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900394
Paul Mundtdc825b12010-04-15 13:13:52 +0900395 intc_balancing_disable(irq);
396
Magnus Dammf18d5332007-09-21 18:16:42 +0900397 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
Paul Mundta8941da2010-03-08 13:33:17 +0900398#ifdef CONFIG_SMP
399 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
400 continue;
401#endif
Magnus Dammf18d5332007-09-21 18:16:42 +0900402 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
403 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
404 [_INTC_FN(handle)], irq);
405 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900406}
407
Magnus Dammd5190952010-02-09 04:29:22 +0000408static void (*intc_enable_noprio_fns[])(unsigned long addr,
409 unsigned long handle,
410 void (*fn)(unsigned long,
411 unsigned long,
412 unsigned long),
413 unsigned int irq) = {
414 [MODE_ENABLE_REG] = intc_mode_field,
415 [MODE_MASK_REG] = intc_mode_zero,
416 [MODE_DUAL_REG] = intc_mode_field,
417 [MODE_PRIO_REG] = intc_mode_field,
418 [MODE_PCLR_REG] = intc_mode_field,
419};
420
421static void intc_enable_disable(struct intc_desc_int *d,
422 unsigned long handle, int do_enable)
423{
424 unsigned long addr;
425 unsigned int cpu;
426 void (*fn)(unsigned long, unsigned long,
427 void (*)(unsigned long, unsigned long, unsigned long),
428 unsigned int);
429
430 if (do_enable) {
431 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
432 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
433 fn = intc_enable_noprio_fns[_INTC_MODE(handle)];
434 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
435 }
436 } else {
437 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
438 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
439 fn = intc_disable_fns[_INTC_MODE(handle)];
440 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
441 }
442 }
443}
444
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000445static int intc_set_wake(unsigned int irq, unsigned int on)
446{
447 return 0; /* allow wakeup, but setup hardware in intc_suspend() */
448}
449
Paul Mundta8941da2010-03-08 13:33:17 +0900450#ifdef CONFIG_SMP
451/*
452 * This is held with the irq desc lock held, so we don't require any
453 * additional locking here at the intc desc level. The affinity mask is
454 * later tested in the enable/disable paths.
455 */
456static int intc_set_affinity(unsigned int irq, const struct cpumask *cpumask)
457{
458 if (!cpumask_intersects(cpumask, cpu_online_mask))
459 return -1;
460
461 cpumask_copy(irq_to_desc(irq)->affinity, cpumask);
462
463 return 0;
464}
465#endif
466
Magnus Dammd58876e2008-04-24 21:36:34 +0900467static void intc_mask_ack(unsigned int irq)
468{
469 struct intc_desc_int *d = get_intc_desc(irq);
470 unsigned long handle = ack_handle[irq];
471 unsigned long addr;
472
473 intc_disable(irq);
474
Paul Mundtdc825b12010-04-15 13:13:52 +0900475 /* read register and write zero only to the associated bit */
Magnus Dammd58876e2008-04-24 21:36:34 +0900476 if (handle) {
477 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900478 switch (_INTC_FN(handle)) {
479 case REG_FN_MODIFY_BASE + 0: /* 8bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900480 __raw_readb(addr);
481 __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900482 break;
483 case REG_FN_MODIFY_BASE + 1: /* 16bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900484 __raw_readw(addr);
485 __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900486 break;
487 case REG_FN_MODIFY_BASE + 3: /* 32bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900488 __raw_readl(addr);
489 __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900490 break;
491 default:
492 BUG();
493 break;
494 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900495 }
496}
Magnus Dammd58876e2008-04-24 21:36:34 +0900497
Magnus Damm73505b42007-08-12 15:26:12 +0900498static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
499 unsigned int nr_hp,
500 unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900501{
Magnus Damm73505b42007-08-12 15:26:12 +0900502 int i;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900503
Paul Mundtdc825b12010-04-15 13:13:52 +0900504 /*
505 * this doesn't scale well, but...
Magnus Damm3d37d942007-08-17 00:50:44 +0900506 *
507 * this function should only be used for cerain uncommon
508 * operations such as intc_set_priority() and intc_set_sense()
509 * and in those rare cases performance doesn't matter that much.
510 * keeping the memory footprint low is more important.
511 *
512 * one rather simple way to speed this up and still keep the
513 * memory footprint down is to make sure the array is sorted
514 * and then perform a bisect to lookup the irq.
515 */
Magnus Damm73505b42007-08-12 15:26:12 +0900516 for (i = 0; i < nr_hp; i++) {
517 if ((hp + i)->irq != irq)
518 continue;
519
520 return hp + i;
521 }
522
523 return NULL;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900524}
525
Magnus Damm73505b42007-08-12 15:26:12 +0900526int intc_set_priority(unsigned int irq, unsigned int prio)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900527{
Magnus Damm73505b42007-08-12 15:26:12 +0900528 struct intc_desc_int *d = get_intc_desc(irq);
529 struct intc_handle_int *ihp;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900530
Magnus Damm73505b42007-08-12 15:26:12 +0900531 if (!intc_prio_level[irq] || prio <= 1)
532 return -EINVAL;
533
534 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
535 if (ihp) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900536 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
Magnus Damm73505b42007-08-12 15:26:12 +0900537 return -EINVAL;
538
539 intc_prio_level[irq] = prio;
540
541 /*
542 * only set secondary masking method directly
543 * primary masking method is using intc_prio_level[irq]
544 * priority level will be set during next enable()
545 */
Magnus Damm3d37d942007-08-17 00:50:44 +0900546 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
Magnus Damm73505b42007-08-12 15:26:12 +0900547 _intc_enable(irq, ihp->handle);
548 }
549 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900550}
551
552#define VALID(x) (x | 0x80)
553
554static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
555 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
556 [IRQ_TYPE_EDGE_RISING] = VALID(1),
557 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
Magnus Damm720be992008-04-24 21:47:15 +0900558 /* SH7706, SH7707 and SH7709 do not support high level triggered */
559#if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
560 !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
561 !defined(CONFIG_CPU_SUBTYPE_SH7709)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900562 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
Magnus Damm720be992008-04-24 21:47:15 +0900563#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900564};
565
566static int intc_set_sense(unsigned int irq, unsigned int type)
567{
Magnus Damm73505b42007-08-12 15:26:12 +0900568 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900569 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
Magnus Damm73505b42007-08-12 15:26:12 +0900570 struct intc_handle_int *ihp;
571 unsigned long addr;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900572
Magnus Damm73505b42007-08-12 15:26:12 +0900573 if (!value)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900574 return -EINVAL;
575
Magnus Damm73505b42007-08-12 15:26:12 +0900576 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
577 if (ihp) {
Magnus Dammf18d5332007-09-21 18:16:42 +0900578 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900579 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900580 }
Magnus Damm73505b42007-08-12 15:26:12 +0900581 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900582}
583
Magnus Damm73505b42007-08-12 15:26:12 +0900584static intc_enum __init intc_grp_id(struct intc_desc *desc,
585 intc_enum enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900586{
Magnus Damm577cd752010-02-09 04:24:46 +0000587 struct intc_group *g = desc->hw.groups;
Magnus Damm680c4592007-07-20 12:09:29 +0900588 unsigned int i, j;
589
Magnus Damm577cd752010-02-09 04:24:46 +0000590 for (i = 0; g && enum_id && i < desc->hw.nr_groups; i++) {
591 g = desc->hw.groups + i;
Magnus Damm680c4592007-07-20 12:09:29 +0900592
593 for (j = 0; g->enum_ids[j]; j++) {
594 if (g->enum_ids[j] != enum_id)
595 continue;
596
597 return g->enum_id;
598 }
599 }
600
601 return 0;
602}
603
Magnus Dammd5190952010-02-09 04:29:22 +0000604static unsigned int __init _intc_mask_data(struct intc_desc *desc,
605 struct intc_desc_int *d,
606 intc_enum enum_id,
607 unsigned int *reg_idx,
608 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900609{
Magnus Damm577cd752010-02-09 04:24:46 +0000610 struct intc_mask_reg *mr = desc->hw.mask_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000611 unsigned int fn, mode;
Magnus Damm73505b42007-08-12 15:26:12 +0900612 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900613
Magnus Dammd5190952010-02-09 04:29:22 +0000614 while (mr && enum_id && *reg_idx < desc->hw.nr_mask_regs) {
615 mr = desc->hw.mask_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900616
Magnus Dammd5190952010-02-09 04:29:22 +0000617 for (; *fld_idx < ARRAY_SIZE(mr->enum_ids); (*fld_idx)++) {
618 if (mr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900619 continue;
620
Magnus Damm73505b42007-08-12 15:26:12 +0900621 if (mr->set_reg && mr->clr_reg) {
622 fn = REG_FN_WRITE_BASE;
623 mode = MODE_DUAL_REG;
624 reg_e = mr->clr_reg;
625 reg_d = mr->set_reg;
626 } else {
627 fn = REG_FN_MODIFY_BASE;
628 if (mr->set_reg) {
629 mode = MODE_ENABLE_REG;
630 reg_e = mr->set_reg;
631 reg_d = mr->set_reg;
632 } else {
633 mode = MODE_MASK_REG;
634 reg_e = mr->clr_reg;
635 reg_d = mr->clr_reg;
636 }
Magnus Damm51da6422007-08-03 14:25:32 +0900637 }
638
Magnus Damm73505b42007-08-12 15:26:12 +0900639 fn += (mr->reg_width >> 3) - 1;
640 return _INTC_MK(fn, mode,
641 intc_get_reg(d, reg_e),
642 intc_get_reg(d, reg_d),
643 1,
Magnus Dammd5190952010-02-09 04:29:22 +0000644 (mr->reg_width - 1) - *fld_idx);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900645 }
Magnus Dammd5190952010-02-09 04:29:22 +0000646
647 *fld_idx = 0;
648 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900649 }
650
Magnus Dammd5190952010-02-09 04:29:22 +0000651 return 0;
652}
653
654static unsigned int __init intc_mask_data(struct intc_desc *desc,
655 struct intc_desc_int *d,
656 intc_enum enum_id, int do_grps)
657{
658 unsigned int i = 0;
659 unsigned int j = 0;
660 unsigned int ret;
661
662 ret = _intc_mask_data(desc, d, enum_id, &i, &j);
663 if (ret)
664 return ret;
665
Magnus Damm680c4592007-07-20 12:09:29 +0900666 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900667 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900668
Magnus Damm02ab3f72007-07-18 17:25:09 +0900669 return 0;
670}
671
Magnus Dammd5190952010-02-09 04:29:22 +0000672static unsigned int __init _intc_prio_data(struct intc_desc *desc,
673 struct intc_desc_int *d,
674 intc_enum enum_id,
675 unsigned int *reg_idx,
676 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900677{
Magnus Damm577cd752010-02-09 04:24:46 +0000678 struct intc_prio_reg *pr = desc->hw.prio_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000679 unsigned int fn, n, mode, bit;
Magnus Damm73505b42007-08-12 15:26:12 +0900680 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900681
Magnus Dammd5190952010-02-09 04:29:22 +0000682 while (pr && enum_id && *reg_idx < desc->hw.nr_prio_regs) {
683 pr = desc->hw.prio_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900684
Magnus Dammd5190952010-02-09 04:29:22 +0000685 for (; *fld_idx < ARRAY_SIZE(pr->enum_ids); (*fld_idx)++) {
686 if (pr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900687 continue;
688
Magnus Damm73505b42007-08-12 15:26:12 +0900689 if (pr->set_reg && pr->clr_reg) {
690 fn = REG_FN_WRITE_BASE;
691 mode = MODE_PCLR_REG;
692 reg_e = pr->set_reg;
693 reg_d = pr->clr_reg;
694 } else {
695 fn = REG_FN_MODIFY_BASE;
696 mode = MODE_PRIO_REG;
697 if (!pr->set_reg)
698 BUG();
699 reg_e = pr->set_reg;
700 reg_d = pr->set_reg;
701 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900702
Magnus Damm73505b42007-08-12 15:26:12 +0900703 fn += (pr->reg_width >> 3) - 1;
Magnus Dammd5190952010-02-09 04:29:22 +0000704 n = *fld_idx + 1;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900705
Magnus Dammd5190952010-02-09 04:29:22 +0000706 BUG_ON(n * pr->field_width > pr->reg_width);
roel kluinb21a9102008-09-09 23:02:43 +0200707
Magnus Dammd5190952010-02-09 04:29:22 +0000708 bit = pr->reg_width - (n * pr->field_width);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900709
Magnus Damm73505b42007-08-12 15:26:12 +0900710 return _INTC_MK(fn, mode,
711 intc_get_reg(d, reg_e),
712 intc_get_reg(d, reg_d),
713 pr->field_width, bit);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900714 }
Magnus Dammd5190952010-02-09 04:29:22 +0000715
716 *fld_idx = 0;
717 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900718 }
719
Magnus Dammd5190952010-02-09 04:29:22 +0000720 return 0;
721}
722
723static unsigned int __init intc_prio_data(struct intc_desc *desc,
724 struct intc_desc_int *d,
725 intc_enum enum_id, int do_grps)
726{
727 unsigned int i = 0;
728 unsigned int j = 0;
729 unsigned int ret;
730
731 ret = _intc_prio_data(desc, d, enum_id, &i, &j);
732 if (ret)
733 return ret;
734
Magnus Damm680c4592007-07-20 12:09:29 +0900735 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900736 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900737
Magnus Damm02ab3f72007-07-18 17:25:09 +0900738 return 0;
739}
740
Magnus Dammd5190952010-02-09 04:29:22 +0000741static void __init intc_enable_disable_enum(struct intc_desc *desc,
742 struct intc_desc_int *d,
743 intc_enum enum_id, int enable)
744{
745 unsigned int i, j, data;
746
747 /* go through and enable/disable all mask bits */
748 i = j = 0;
749 do {
750 data = _intc_mask_data(desc, d, enum_id, &i, &j);
751 if (data)
752 intc_enable_disable(d, data, enable);
753 j++;
754 } while (data);
755
756 /* go through and enable/disable all priority fields */
757 i = j = 0;
758 do {
759 data = _intc_prio_data(desc, d, enum_id, &i, &j);
760 if (data)
761 intc_enable_disable(d, data, enable);
762
763 j++;
764 } while (data);
765}
766
Magnus Dammd58876e2008-04-24 21:36:34 +0900767static unsigned int __init intc_ack_data(struct intc_desc *desc,
768 struct intc_desc_int *d,
769 intc_enum enum_id)
770{
Magnus Damm577cd752010-02-09 04:24:46 +0000771 struct intc_mask_reg *mr = desc->hw.ack_regs;
Magnus Dammd58876e2008-04-24 21:36:34 +0900772 unsigned int i, j, fn, mode;
773 unsigned long reg_e, reg_d;
774
Magnus Damm577cd752010-02-09 04:24:46 +0000775 for (i = 0; mr && enum_id && i < desc->hw.nr_ack_regs; i++) {
776 mr = desc->hw.ack_regs + i;
Magnus Dammd58876e2008-04-24 21:36:34 +0900777
778 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
779 if (mr->enum_ids[j] != enum_id)
780 continue;
781
782 fn = REG_FN_MODIFY_BASE;
783 mode = MODE_ENABLE_REG;
784 reg_e = mr->set_reg;
785 reg_d = mr->set_reg;
786
787 fn += (mr->reg_width >> 3) - 1;
788 return _INTC_MK(fn, mode,
789 intc_get_reg(d, reg_e),
790 intc_get_reg(d, reg_d),
791 1,
792 (mr->reg_width - 1) - j);
793 }
794 }
795
796 return 0;
797}
Magnus Dammd58876e2008-04-24 21:36:34 +0900798
Magnus Damm73505b42007-08-12 15:26:12 +0900799static unsigned int __init intc_sense_data(struct intc_desc *desc,
800 struct intc_desc_int *d,
801 intc_enum enum_id)
802{
Magnus Damm577cd752010-02-09 04:24:46 +0000803 struct intc_sense_reg *sr = desc->hw.sense_regs;
Magnus Damm73505b42007-08-12 15:26:12 +0900804 unsigned int i, j, fn, bit;
805
Magnus Damm577cd752010-02-09 04:24:46 +0000806 for (i = 0; sr && enum_id && i < desc->hw.nr_sense_regs; i++) {
807 sr = desc->hw.sense_regs + i;
Magnus Damm73505b42007-08-12 15:26:12 +0900808
809 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
810 if (sr->enum_ids[j] != enum_id)
811 continue;
812
813 fn = REG_FN_MODIFY_BASE;
814 fn += (sr->reg_width >> 3) - 1;
Magnus Damm73505b42007-08-12 15:26:12 +0900815
roel kluinb21a9102008-09-09 23:02:43 +0200816 BUG_ON((j + 1) * sr->field_width > sr->reg_width);
817
818 bit = sr->reg_width - ((j + 1) * sr->field_width);
Magnus Damm73505b42007-08-12 15:26:12 +0900819
820 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
821 0, sr->field_width, bit);
822 }
823 }
824
825 return 0;
826}
827
828static void __init intc_register_irq(struct intc_desc *desc,
829 struct intc_desc_int *d,
830 intc_enum enum_id,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900831 unsigned int irq)
832{
Magnus Damm3d37d942007-08-17 00:50:44 +0900833 struct intc_handle_int *hp;
Magnus Damm680c4592007-07-20 12:09:29 +0900834 unsigned int data[2], primary;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900835
Paul Mundt1ce7b032009-11-02 10:30:26 +0900836 /*
837 * Register the IRQ position with the global IRQ map
838 */
839 set_bit(irq, intc_irq_map);
840
Paul Mundtdc825b12010-04-15 13:13:52 +0900841 /*
842 * Prefer single interrupt source bitmap over other combinations:
843 *
Magnus Damm680c4592007-07-20 12:09:29 +0900844 * 1. bitmap, single interrupt source
845 * 2. priority, single interrupt source
846 * 3. bitmap, multiple interrupt sources (groups)
847 * 4. priority, multiple interrupt sources (groups)
848 */
Magnus Damm73505b42007-08-12 15:26:12 +0900849 data[0] = intc_mask_data(desc, d, enum_id, 0);
850 data[1] = intc_prio_data(desc, d, enum_id, 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900851
852 primary = 0;
853 if (!data[0] && data[1])
854 primary = 1;
855
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900856 if (!data[0] && !data[1])
Paul Mundtf0335992009-03-06 17:56:58 +0900857 pr_warning("intc: missing unique irq mask for "
858 "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900859
Magnus Damm73505b42007-08-12 15:26:12 +0900860 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
861 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
Magnus Damm680c4592007-07-20 12:09:29 +0900862
863 if (!data[primary])
864 primary ^= 1;
865
866 BUG_ON(!data[primary]); /* must have primary masking method */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900867
868 disable_irq_nosync(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900869 set_irq_chip_and_handler_name(irq, &d->chip,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900870 handle_level_irq, "level");
Magnus Damm680c4592007-07-20 12:09:29 +0900871 set_irq_chip_data(irq, (void *)data[primary]);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900872
Paul Mundtdc825b12010-04-15 13:13:52 +0900873 /*
874 * set priority level
Magnus Damm7f3edee2008-01-10 14:08:55 +0900875 * - this needs to be at least 2 for 5-bit priorities on 7780
876 */
Paul Mundt43b87742010-04-13 14:43:03 +0900877 intc_prio_level[irq] = default_prio_level;
Magnus Damm73505b42007-08-12 15:26:12 +0900878
Magnus Damm680c4592007-07-20 12:09:29 +0900879 /* enable secondary masking method if present */
880 if (data[!primary])
Magnus Damm73505b42007-08-12 15:26:12 +0900881 _intc_enable(irq, data[!primary]);
882
883 /* add irq to d->prio list if priority is available */
884 if (data[1]) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900885 hp = d->prio + d->nr_prio;
886 hp->irq = irq;
887 hp->handle = data[1];
888
889 if (primary) {
890 /*
891 * only secondary priority should access registers, so
892 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
893 */
Magnus Damm3d37d942007-08-17 00:50:44 +0900894 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
895 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
896 }
Magnus Damm73505b42007-08-12 15:26:12 +0900897 d->nr_prio++;
898 }
899
900 /* add irq to d->sense list if sense is available */
901 data[0] = intc_sense_data(desc, d, enum_id);
902 if (data[0]) {
903 (d->sense + d->nr_sense)->irq = irq;
904 (d->sense + d->nr_sense)->handle = data[0];
905 d->nr_sense++;
906 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900907
908 /* irq should be disabled by default */
Magnus Damm73505b42007-08-12 15:26:12 +0900909 d->chip.mask(irq);
Magnus Dammd58876e2008-04-24 21:36:34 +0900910
Magnus Damm577cd752010-02-09 04:24:46 +0000911 if (desc->hw.ack_regs)
Magnus Dammd58876e2008-04-24 21:36:34 +0900912 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
Magnus Damm65a5b282010-02-05 11:15:25 +0000913
Paul Mundtdc825b12010-04-15 13:13:52 +0900914#ifdef CONFIG_INTC_BALANCING
915 if (desc->hw.mask_regs)
916 dist_handle[irq] = intc_dist_data(desc, d, enum_id);
917#endif
918
Magnus Damm65a5b282010-02-05 11:15:25 +0000919#ifdef CONFIG_ARM
920 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
921#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900922}
923
Magnus Dammf18d5332007-09-21 18:16:42 +0900924static unsigned int __init save_reg(struct intc_desc_int *d,
925 unsigned int cnt,
926 unsigned long value,
927 unsigned int smp)
928{
929 if (value) {
Magnus Dammdec710b2010-03-19 16:48:01 +0900930 value = intc_phys_to_virt(d, value);
931
Magnus Dammf18d5332007-09-21 18:16:42 +0900932 d->reg[cnt] = value;
933#ifdef CONFIG_SMP
934 d->smp[cnt] = smp;
935#endif
936 return 1;
937 }
938
939 return 0;
940}
941
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900942static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900943{
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900944 generic_handle_irq((unsigned int)get_irq_data(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900945}
Magnus Dammf18d5332007-09-21 18:16:42 +0900946
Magnus Damm01e96512010-03-10 09:31:01 +0000947int __init register_intc_controller(struct intc_desc *desc)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900948{
Paul Mundt54ff3282009-06-11 10:33:09 +0300949 unsigned int i, k, smp;
Magnus Damm577cd752010-02-09 04:24:46 +0000950 struct intc_hw_desc *hw = &desc->hw;
Magnus Damm73505b42007-08-12 15:26:12 +0900951 struct intc_desc_int *d;
Magnus Dammdec710b2010-03-19 16:48:01 +0900952 struct resource *res;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900953
Paul Mundt12129fe2010-04-13 13:49:54 +0900954 pr_info("intc: Registered controller '%s' with %u IRQs\n",
955 desc->name, hw->nr_vectors);
956
Paul Mundt11b6aa92009-06-12 01:34:12 +0300957 d = kzalloc(sizeof(*d), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000958 if (!d)
959 goto err0;
Magnus Damm73505b42007-08-12 15:26:12 +0900960
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000961 INIT_LIST_HEAD(&d->list);
962 list_add(&d->list, &intc_list);
963
Magnus Dammdec710b2010-03-19 16:48:01 +0900964 if (desc->num_resources) {
965 d->nr_windows = desc->num_resources;
966 d->window = kzalloc(d->nr_windows * sizeof(*d->window),
967 GFP_NOWAIT);
968 if (!d->window)
969 goto err1;
970
971 for (k = 0; k < d->nr_windows; k++) {
972 res = desc->resource + k;
973 WARN_ON(resource_type(res) != IORESOURCE_MEM);
974 d->window[k].phys = res->start;
975 d->window[k].size = resource_size(res);
976 d->window[k].virt = ioremap_nocache(res->start,
977 resource_size(res));
978 if (!d->window[k].virt)
979 goto err2;
980 }
981 }
982
Magnus Damm577cd752010-02-09 04:24:46 +0000983 d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
Paul Mundtdc825b12010-04-15 13:13:52 +0900984#ifdef CONFIG_INTC_BALANCING
985 if (d->nr_reg)
986 d->nr_reg += hw->nr_mask_regs;
987#endif
Magnus Damm577cd752010-02-09 04:24:46 +0000988 d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
989 d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
990 d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
Paul Mundt9b798d52009-10-27 11:36:43 +0900991
Paul Mundt11b6aa92009-06-12 01:34:12 +0300992 d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000993 if (!d->reg)
Magnus Dammdec710b2010-03-19 16:48:01 +0900994 goto err2;
Magnus Damm01e96512010-03-10 09:31:01 +0000995
Magnus Dammf18d5332007-09-21 18:16:42 +0900996#ifdef CONFIG_SMP
Paul Mundt11b6aa92009-06-12 01:34:12 +0300997 d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000998 if (!d->smp)
Magnus Dammdec710b2010-03-19 16:48:01 +0900999 goto err3;
Magnus Dammf18d5332007-09-21 18:16:42 +09001000#endif
Magnus Damm73505b42007-08-12 15:26:12 +09001001 k = 0;
1002
Magnus Damm577cd752010-02-09 04:24:46 +00001003 if (hw->mask_regs) {
1004 for (i = 0; i < hw->nr_mask_regs; i++) {
1005 smp = IS_SMP(hw->mask_regs[i]);
1006 k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
1007 k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
Paul Mundtdc825b12010-04-15 13:13:52 +09001008#ifdef CONFIG_INTC_BALANCING
1009 k += save_reg(d, k, hw->mask_regs[i].dist_reg, 0);
1010#endif
Magnus Damm73505b42007-08-12 15:26:12 +09001011 }
1012 }
1013
Magnus Damm577cd752010-02-09 04:24:46 +00001014 if (hw->prio_regs) {
1015 d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
1016 GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +00001017 if (!d->prio)
Magnus Dammdec710b2010-03-19 16:48:01 +09001018 goto err4;
Magnus Damm73505b42007-08-12 15:26:12 +09001019
Magnus Damm577cd752010-02-09 04:24:46 +00001020 for (i = 0; i < hw->nr_prio_regs; i++) {
1021 smp = IS_SMP(hw->prio_regs[i]);
1022 k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
1023 k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +09001024 }
1025 }
1026
Magnus Damm577cd752010-02-09 04:24:46 +00001027 if (hw->sense_regs) {
1028 d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
1029 GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +00001030 if (!d->sense)
Magnus Dammdec710b2010-03-19 16:48:01 +09001031 goto err5;
Magnus Damm73505b42007-08-12 15:26:12 +09001032
Magnus Damm577cd752010-02-09 04:24:46 +00001033 for (i = 0; i < hw->nr_sense_regs; i++)
1034 k += save_reg(d, k, hw->sense_regs[i].reg, 0);
Magnus Damm73505b42007-08-12 15:26:12 +09001035 }
1036
Magnus Damm73505b42007-08-12 15:26:12 +09001037 d->chip.name = desc->name;
1038 d->chip.mask = intc_disable;
1039 d->chip.unmask = intc_enable;
1040 d->chip.mask_ack = intc_disable;
Magnus Dammf7dd2542009-04-01 14:20:58 +00001041 d->chip.enable = intc_enable;
1042 d->chip.disable = intc_disable;
1043 d->chip.shutdown = intc_disable;
Magnus Damm73505b42007-08-12 15:26:12 +09001044 d->chip.set_type = intc_set_sense;
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001045 d->chip.set_wake = intc_set_wake;
Paul Mundta8941da2010-03-08 13:33:17 +09001046#ifdef CONFIG_SMP
1047 d->chip.set_affinity = intc_set_affinity;
1048#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +09001049
Magnus Damm577cd752010-02-09 04:24:46 +00001050 if (hw->ack_regs) {
1051 for (i = 0; i < hw->nr_ack_regs; i++)
1052 k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
Magnus Dammd58876e2008-04-24 21:36:34 +09001053
1054 d->chip.mask_ack = intc_mask_ack;
1055 }
Magnus Dammd58876e2008-04-24 21:36:34 +09001056
Magnus Dammd85429a2010-02-15 11:40:25 +00001057 /* disable bits matching force_disable before registering irqs */
1058 if (desc->force_disable)
1059 intc_enable_disable_enum(desc, d, desc->force_disable, 0);
Magnus Dammd5190952010-02-09 04:29:22 +00001060
1061 /* disable bits matching force_enable before registering irqs */
1062 if (desc->force_enable)
1063 intc_enable_disable_enum(desc, d, desc->force_enable, 0);
1064
Magnus Dammd58876e2008-04-24 21:36:34 +09001065 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
1066
Magnus Dammbdaa6e82009-02-24 22:58:57 +09001067 /* register the vectors one by one */
Magnus Damm577cd752010-02-09 04:24:46 +00001068 for (i = 0; i < hw->nr_vectors; i++) {
1069 struct intc_vect *vect = hw->vectors + i;
Paul Mundt05ff3002009-05-22 01:28:33 +09001070 unsigned int irq = evt2irq(vect->vect);
1071 struct irq_desc *irq_desc;
Paul Mundt54ff3282009-06-11 10:33:09 +03001072
Magnus Dammbdaa6e82009-02-24 22:58:57 +09001073 if (!vect->enum_id)
1074 continue;
Magnus Damm02ab3f72007-07-18 17:25:09 +09001075
Paul Mundt54ff3282009-06-11 10:33:09 +03001076 irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
Paul Mundt05ff3002009-05-22 01:28:33 +09001077 if (unlikely(!irq_desc)) {
Paul Mundt12129fe2010-04-13 13:49:54 +09001078 pr_err("can't get irq_desc for %d\n", irq);
Paul Mundt05ff3002009-05-22 01:28:33 +09001079 continue;
1080 }
1081
1082 intc_register_irq(desc, d, vect->enum_id, irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +09001083
Magnus Damm577cd752010-02-09 04:24:46 +00001084 for (k = i + 1; k < hw->nr_vectors; k++) {
1085 struct intc_vect *vect2 = hw->vectors + k;
Pawel Moll05ecd5a2009-08-24 19:52:38 +09001086 unsigned int irq2 = evt2irq(vect2->vect);
1087
1088 if (vect->enum_id != vect2->enum_id)
1089 continue;
1090
Paul Mundt1279b7f2009-08-31 15:15:33 +09001091 /*
1092 * In the case of multi-evt handling and sparse
1093 * IRQ support, each vector still needs to have
1094 * its own backing irq_desc.
1095 */
1096 irq_desc = irq_to_desc_alloc_node(irq2, numa_node_id());
1097 if (unlikely(!irq_desc)) {
Paul Mundt12129fe2010-04-13 13:49:54 +09001098 pr_err("can't get irq_desc for %d\n", irq2);
Paul Mundt1279b7f2009-08-31 15:15:33 +09001099 continue;
1100 }
1101
Pawel Moll05ecd5a2009-08-24 19:52:38 +09001102 vect2->enum_id = 0;
1103
1104 /* redirect this interrupts to the first one */
Paul Mundt4d2185d2010-02-17 12:37:42 +09001105 set_irq_chip(irq2, &dummy_irq_chip);
Magnus Damme6f07752010-02-09 07:17:20 +00001106 set_irq_chained_handler(irq2, intc_redirect_irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +09001107 set_irq_data(irq2, (void *)irq);
1108 }
Magnus Damm02ab3f72007-07-18 17:25:09 +09001109 }
Magnus Dammd5190952010-02-09 04:29:22 +00001110
1111 /* enable bits matching force_enable after registering irqs */
1112 if (desc->force_enable)
1113 intc_enable_disable_enum(desc, d, desc->force_enable, 1);
Magnus Damm01e96512010-03-10 09:31:01 +00001114
1115 return 0;
Magnus Dammdec710b2010-03-19 16:48:01 +09001116err5:
Magnus Damm01e96512010-03-10 09:31:01 +00001117 kfree(d->prio);
Magnus Dammdec710b2010-03-19 16:48:01 +09001118err4:
Magnus Damm01e96512010-03-10 09:31:01 +00001119#ifdef CONFIG_SMP
1120 kfree(d->smp);
Magnus Dammdec710b2010-03-19 16:48:01 +09001121err3:
Magnus Damm01e96512010-03-10 09:31:01 +00001122#endif
1123 kfree(d->reg);
Magnus Dammdec710b2010-03-19 16:48:01 +09001124err2:
1125 for (k = 0; k < d->nr_windows; k++)
1126 if (d->window[k].virt)
1127 iounmap(d->window[k].virt);
1128
1129 kfree(d->window);
1130err1:
Magnus Damm01e96512010-03-10 09:31:01 +00001131 kfree(d);
Magnus Dammdec710b2010-03-19 16:48:01 +09001132err0:
Magnus Damm01e96512010-03-10 09:31:01 +00001133 pr_err("unable to allocate INTC memory\n");
1134
1135 return -ENOMEM;
Magnus Damm02ab3f72007-07-18 17:25:09 +09001136}
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001137
Paul Mundt43b87742010-04-13 14:43:03 +09001138#ifdef CONFIG_INTC_USERIMASK
1139static void __iomem *uimask;
1140
1141int register_intc_userimask(unsigned long addr)
1142{
1143 if (unlikely(uimask))
1144 return -EBUSY;
1145
1146 uimask = ioremap_nocache(addr, SZ_4K);
1147 if (unlikely(!uimask))
1148 return -ENOMEM;
1149
1150 pr_info("intc: userimask support registered for levels 0 -> %d\n",
1151 default_prio_level - 1);
1152
1153 return 0;
1154}
1155
1156static ssize_t
1157show_intc_userimask(struct sysdev_class *cls,
1158 struct sysdev_class_attribute *attr, char *buf)
1159{
1160 return sprintf(buf, "%d\n", (__raw_readl(uimask) >> 4) & 0xf);
1161}
1162
1163static ssize_t
1164store_intc_userimask(struct sysdev_class *cls,
1165 struct sysdev_class_attribute *attr,
1166 const char *buf, size_t count)
1167{
1168 unsigned long level;
1169
1170 level = simple_strtoul(buf, NULL, 10);
1171
1172 /*
1173 * Minimal acceptable IRQ levels are in the 2 - 16 range, but
1174 * these are chomped so as to not interfere with normal IRQs.
1175 *
1176 * Level 1 is a special case on some CPUs in that it's not
1177 * directly settable, but given that USERIMASK cuts off below a
1178 * certain level, we don't care about this limitation here.
1179 * Level 0 on the other hand equates to user masking disabled.
1180 *
1181 * We use default_prio_level as a cut off so that only special
1182 * case opt-in IRQs can be mangled.
1183 */
1184 if (level >= default_prio_level)
1185 return -EINVAL;
1186
1187 __raw_writel(0xa5 << 24 | level << 4, uimask);
1188
1189 return count;
1190}
1191
1192static SYSDEV_CLASS_ATTR(userimask, S_IRUSR | S_IWUSR,
1193 show_intc_userimask, store_intc_userimask);
1194#endif
1195
Paul Mundt0ded7542010-04-13 10:16:34 +09001196static ssize_t
1197show_intc_name(struct sys_device *dev, struct sysdev_attribute *attr, char *buf)
1198{
1199 struct intc_desc_int *d;
1200
1201 d = container_of(dev, struct intc_desc_int, sysdev);
1202
1203 return sprintf(buf, "%s\n", d->chip.name);
1204}
1205
1206static SYSDEV_ATTR(name, S_IRUGO, show_intc_name, NULL);
1207
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001208static int intc_suspend(struct sys_device *dev, pm_message_t state)
1209{
1210 struct intc_desc_int *d;
1211 struct irq_desc *desc;
1212 int irq;
1213
1214 /* get intc controller associated with this sysdev */
1215 d = container_of(dev, struct intc_desc_int, sysdev);
1216
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001217 switch (state.event) {
1218 case PM_EVENT_ON:
1219 if (d->state.event != PM_EVENT_FREEZE)
1220 break;
1221 for_each_irq_desc(irq, desc) {
Francesco VIRLINZI87a705d2009-12-04 08:57:58 +00001222 if (desc->handle_irq == intc_redirect_irq)
Paul Mundt0a753d52009-12-09 14:36:16 +09001223 continue;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001224 if (desc->chip != &d->chip)
1225 continue;
1226 if (desc->status & IRQ_DISABLED)
1227 intc_disable(irq);
1228 else
1229 intc_enable(irq);
1230 }
1231 break;
1232 case PM_EVENT_FREEZE:
1233 /* nothing has to be done */
1234 break;
1235 case PM_EVENT_SUSPEND:
1236 /* enable wakeup irqs belonging to this intc controller */
1237 for_each_irq_desc(irq, desc) {
1238 if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
1239 intc_enable(irq);
1240 }
1241 break;
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001242 }
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001243 d->state = state;
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001244
1245 return 0;
1246}
1247
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001248static int intc_resume(struct sys_device *dev)
1249{
1250 return intc_suspend(dev, PMSG_ON);
1251}
1252
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001253static struct sysdev_class intc_sysdev_class = {
1254 .name = "intc",
1255 .suspend = intc_suspend,
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001256 .resume = intc_resume,
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001257};
1258
1259/* register this intc as sysdev to allow suspend/resume */
1260static int __init register_intc_sysdevs(void)
1261{
1262 struct intc_desc_int *d;
1263 int error;
1264 int id = 0;
1265
1266 error = sysdev_class_register(&intc_sysdev_class);
Paul Mundt43b87742010-04-13 14:43:03 +09001267#ifdef CONFIG_INTC_USERIMASK
1268 if (!error && uimask)
1269 error = sysdev_class_create_file(&intc_sysdev_class,
1270 &attr_userimask);
1271#endif
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001272 if (!error) {
1273 list_for_each_entry(d, &intc_list, list) {
1274 d->sysdev.id = id;
1275 d->sysdev.cls = &intc_sysdev_class;
1276 error = sysdev_register(&d->sysdev);
Paul Mundt0ded7542010-04-13 10:16:34 +09001277 if (error == 0)
1278 error = sysdev_create_file(&d->sysdev,
1279 &attr_name);
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001280 if (error)
1281 break;
Paul Mundt0ded7542010-04-13 10:16:34 +09001282
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001283 id++;
1284 }
1285 }
1286
1287 if (error)
Paul Mundt12129fe2010-04-13 13:49:54 +09001288 pr_err("intc: sysdev registration error\n");
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001289
1290 return error;
1291}
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001292device_initcall(register_intc_sysdevs);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001293
1294/*
1295 * Dynamic IRQ allocation and deallocation
1296 */
Paul Mundte9867c52010-02-02 17:35:13 +09001297unsigned int create_irq_nr(unsigned int irq_want, int node)
Paul Mundt1ce7b032009-11-02 10:30:26 +09001298{
1299 unsigned int irq = 0, new;
1300 unsigned long flags;
1301 struct irq_desc *desc;
1302
1303 spin_lock_irqsave(&vector_lock, flags);
1304
1305 /*
Paul Mundte9867c52010-02-02 17:35:13 +09001306 * First try the wanted IRQ
Paul Mundt1ce7b032009-11-02 10:30:26 +09001307 */
Paul Mundte9867c52010-02-02 17:35:13 +09001308 if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
1309 new = irq_want;
1310 } else {
1311 /* .. then fall back to scanning. */
Paul Mundt1ce7b032009-11-02 10:30:26 +09001312 new = find_first_zero_bit(intc_irq_map, nr_irqs);
1313 if (unlikely(new == nr_irqs))
1314 goto out_unlock;
1315
Paul Mundt1ce7b032009-11-02 10:30:26 +09001316 __set_bit(new, intc_irq_map);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001317 }
1318
Paul Mundte9867c52010-02-02 17:35:13 +09001319 desc = irq_to_desc_alloc_node(new, node);
1320 if (unlikely(!desc)) {
Paul Mundt12129fe2010-04-13 13:49:54 +09001321 pr_err("can't get irq_desc for %d\n", new);
Paul Mundte9867c52010-02-02 17:35:13 +09001322 goto out_unlock;
1323 }
1324
1325 desc = move_irq_desc(desc, node);
1326 irq = new;
1327
Paul Mundt1ce7b032009-11-02 10:30:26 +09001328out_unlock:
1329 spin_unlock_irqrestore(&vector_lock, flags);
1330
Magnus Damm65a5b282010-02-05 11:15:25 +00001331 if (irq > 0) {
Paul Mundt1ce7b032009-11-02 10:30:26 +09001332 dynamic_irq_init(irq);
Magnus Damm65a5b282010-02-05 11:15:25 +00001333#ifdef CONFIG_ARM
1334 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
1335#endif
1336 }
Paul Mundt1ce7b032009-11-02 10:30:26 +09001337
1338 return irq;
1339}
1340
1341int create_irq(void)
1342{
1343 int nid = cpu_to_node(smp_processor_id());
1344 int irq;
1345
Paul Mundte9867c52010-02-02 17:35:13 +09001346 irq = create_irq_nr(NR_IRQS_LEGACY, nid);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001347 if (irq == 0)
1348 irq = -1;
1349
1350 return irq;
1351}
1352
1353void destroy_irq(unsigned int irq)
1354{
1355 unsigned long flags;
1356
1357 dynamic_irq_cleanup(irq);
1358
1359 spin_lock_irqsave(&vector_lock, flags);
1360 __clear_bit(irq, intc_irq_map);
1361 spin_unlock_irqrestore(&vector_lock, flags);
1362}
Paul Mundt45b9dea2009-11-02 15:43:20 +09001363
1364int reserve_irq_vector(unsigned int irq)
1365{
1366 unsigned long flags;
1367 int ret = 0;
1368
1369 spin_lock_irqsave(&vector_lock, flags);
1370 if (test_and_set_bit(irq, intc_irq_map))
1371 ret = -EBUSY;
1372 spin_unlock_irqrestore(&vector_lock, flags);
1373
1374 return ret;
1375}
1376
1377void reserve_irq_legacy(void)
1378{
1379 unsigned long flags;
1380 int i, j;
1381
1382 spin_lock_irqsave(&vector_lock, flags);
1383 j = find_first_bit(intc_irq_map, nr_irqs);
1384 for (i = 0; i < j; i++)
1385 __set_bit(i, intc_irq_map);
1386 spin_unlock_irqrestore(&vector_lock, flags);
1387}