blob: 94ad6bd86a00c02754d6c3ac2666cc347a2816c7 [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>
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
48struct intc_desc_int {
Magnus Damm2dcec7a2009-04-01 14:30:59 +000049 struct list_head list;
50 struct sys_device sysdev;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +000051 pm_message_t state;
Magnus Damm73505b42007-08-12 15:26:12 +090052 unsigned long *reg;
Magnus Dammf18d5332007-09-21 18:16:42 +090053#ifdef CONFIG_SMP
54 unsigned long *smp;
55#endif
Magnus Damm73505b42007-08-12 15:26:12 +090056 unsigned int nr_reg;
57 struct intc_handle_int *prio;
58 unsigned int nr_prio;
59 struct intc_handle_int *sense;
60 unsigned int nr_sense;
61 struct irq_chip chip;
62};
63
Magnus Damm2dcec7a2009-04-01 14:30:59 +000064static LIST_HEAD(intc_list);
65
Paul Mundt1ce7b032009-11-02 10:30:26 +090066/*
67 * The intc_irq_map provides a global map of bound IRQ vectors for a
68 * given platform. Allocation of IRQs are either static through the CPU
69 * vector map, or dynamic in the case of board mux vectors or MSI.
70 *
71 * As this is a central point for all IRQ controllers on the system,
72 * each of the available sources are mapped out here. This combined with
73 * sparseirq makes it quite trivial to keep the vector map tightly packed
74 * when dynamically creating IRQs, as well as tying in to otherwise
75 * unused irq_desc positions in the sparse array.
76 */
77static DECLARE_BITMAP(intc_irq_map, NR_IRQS);
78static DEFINE_SPINLOCK(vector_lock);
79
Magnus Dammf18d5332007-09-21 18:16:42 +090080#ifdef CONFIG_SMP
81#define IS_SMP(x) x.smp
82#define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
83#define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
84#else
85#define IS_SMP(x) 0
86#define INTC_REG(d, x, c) (d->reg[(x)])
87#define SMP_NR(d, x) 1
88#endif
89
Magnus Damm73505b42007-08-12 15:26:12 +090090static unsigned int intc_prio_level[NR_IRQS]; /* for now */
Magnus Dammd58876e2008-04-24 21:36:34 +090091static unsigned long ack_handle[NR_IRQS];
Magnus Damm73505b42007-08-12 15:26:12 +090092
93static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +090094{
95 struct irq_chip *chip = get_irq_chip(irq);
Stuart Menefy6000fc42009-08-24 18:27:33 +090096 return container_of(chip, struct intc_desc_int, chip);
Magnus Damm02ab3f72007-07-18 17:25:09 +090097}
98
99static inline unsigned int set_field(unsigned int value,
100 unsigned int field_value,
Magnus Damm73505b42007-08-12 15:26:12 +0900101 unsigned int handle)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900102{
Magnus Damm73505b42007-08-12 15:26:12 +0900103 unsigned int width = _INTC_WIDTH(handle);
104 unsigned int shift = _INTC_SHIFT(handle);
105
Magnus Damm02ab3f72007-07-18 17:25:09 +0900106 value &= ~(((1 << width) - 1) << shift);
107 value |= field_value << shift;
108 return value;
109}
110
Magnus Damm73505b42007-08-12 15:26:12 +0900111static void write_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900112{
Paul Mundt62429e02008-10-01 15:19:10 +0900113 __raw_writeb(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900114 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900115}
116
Magnus Damm73505b42007-08-12 15:26:12 +0900117static void write_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900118{
Paul Mundt62429e02008-10-01 15:19:10 +0900119 __raw_writew(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900120 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900121}
122
Magnus Damm73505b42007-08-12 15:26:12 +0900123static void write_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900124{
Paul Mundt62429e02008-10-01 15:19:10 +0900125 __raw_writel(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900126 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900127}
128
Magnus Damm73505b42007-08-12 15:26:12 +0900129static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900130{
Magnus Damm4370fe12008-04-24 21:53:07 +0900131 unsigned long flags;
132 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900133 __raw_writeb(set_field(__raw_readb(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900134 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900135 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900136}
137
Magnus Damm73505b42007-08-12 15:26:12 +0900138static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900139{
Magnus Damm4370fe12008-04-24 21:53:07 +0900140 unsigned long flags;
141 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900142 __raw_writew(set_field(__raw_readw(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900143 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900144 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900145}
146
Magnus Damm73505b42007-08-12 15:26:12 +0900147static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900148{
Magnus Damm4370fe12008-04-24 21:53:07 +0900149 unsigned long flags;
150 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900151 __raw_writel(set_field(__raw_readl(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900152 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900153 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900154}
155
Magnus Damm73505b42007-08-12 15:26:12 +0900156enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
Magnus Damm02ab3f72007-07-18 17:25:09 +0900157
Magnus Damm73505b42007-08-12 15:26:12 +0900158static void (*intc_reg_fns[])(unsigned long addr,
159 unsigned long h,
160 unsigned long data) = {
161 [REG_FN_WRITE_BASE + 0] = write_8,
162 [REG_FN_WRITE_BASE + 1] = write_16,
163 [REG_FN_WRITE_BASE + 3] = write_32,
164 [REG_FN_MODIFY_BASE + 0] = modify_8,
165 [REG_FN_MODIFY_BASE + 1] = modify_16,
166 [REG_FN_MODIFY_BASE + 3] = modify_32,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900167};
168
Magnus Damm73505b42007-08-12 15:26:12 +0900169enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
170 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
171 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
172 MODE_PRIO_REG, /* Priority value written to enable interrupt */
173 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
174};
175
176static void intc_mode_field(unsigned long addr,
177 unsigned long handle,
178 void (*fn)(unsigned long,
179 unsigned long,
180 unsigned long),
181 unsigned int irq)
182{
183 fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
184}
185
186static void intc_mode_zero(unsigned long addr,
187 unsigned long handle,
188 void (*fn)(unsigned long,
189 unsigned long,
190 unsigned long),
191 unsigned int irq)
192{
193 fn(addr, handle, 0);
194}
195
196static void intc_mode_prio(unsigned long addr,
197 unsigned long handle,
198 void (*fn)(unsigned long,
199 unsigned long,
200 unsigned long),
201 unsigned int irq)
202{
203 fn(addr, handle, intc_prio_level[irq]);
204}
205
206static void (*intc_enable_fns[])(unsigned long addr,
207 unsigned long handle,
208 void (*fn)(unsigned long,
209 unsigned long,
210 unsigned long),
211 unsigned int irq) = {
212 [MODE_ENABLE_REG] = intc_mode_field,
213 [MODE_MASK_REG] = intc_mode_zero,
214 [MODE_DUAL_REG] = intc_mode_field,
215 [MODE_PRIO_REG] = intc_mode_prio,
216 [MODE_PCLR_REG] = intc_mode_prio,
217};
218
219static void (*intc_disable_fns[])(unsigned long addr,
220 unsigned long handle,
221 void (*fn)(unsigned long,
222 unsigned long,
223 unsigned long),
224 unsigned int irq) = {
225 [MODE_ENABLE_REG] = intc_mode_zero,
226 [MODE_MASK_REG] = intc_mode_field,
227 [MODE_DUAL_REG] = intc_mode_field,
228 [MODE_PRIO_REG] = intc_mode_zero,
229 [MODE_PCLR_REG] = intc_mode_field,
230};
231
232static inline void _intc_enable(unsigned int irq, unsigned long handle)
233{
234 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900235 unsigned long addr;
236 unsigned int cpu;
Magnus Damm73505b42007-08-12 15:26:12 +0900237
Magnus Dammf18d5332007-09-21 18:16:42 +0900238 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
Paul Mundta8941da2010-03-08 13:33:17 +0900239#ifdef CONFIG_SMP
240 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
241 continue;
242#endif
Magnus Dammf18d5332007-09-21 18:16:42 +0900243 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
244 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
245 [_INTC_FN(handle)], irq);
246 }
Magnus Damm73505b42007-08-12 15:26:12 +0900247}
248
Magnus Damm02ab3f72007-07-18 17:25:09 +0900249static void intc_enable(unsigned int irq)
250{
Magnus Damm73505b42007-08-12 15:26:12 +0900251 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
Magnus Damm02ab3f72007-07-18 17:25:09 +0900252}
253
254static void intc_disable(unsigned int irq)
255{
Magnus Dammf18d5332007-09-21 18:16:42 +0900256 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900257 unsigned long handle = (unsigned long) get_irq_chip_data(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900258 unsigned long addr;
259 unsigned int cpu;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900260
Magnus Dammf18d5332007-09-21 18:16:42 +0900261 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
Paul Mundta8941da2010-03-08 13:33:17 +0900262#ifdef CONFIG_SMP
263 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
264 continue;
265#endif
Magnus Dammf18d5332007-09-21 18:16:42 +0900266 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
267 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
268 [_INTC_FN(handle)], irq);
269 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900270}
271
Magnus Dammd5190952010-02-09 04:29:22 +0000272static void (*intc_enable_noprio_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_field,
279 [MODE_MASK_REG] = intc_mode_zero,
280 [MODE_DUAL_REG] = intc_mode_field,
281 [MODE_PRIO_REG] = intc_mode_field,
282 [MODE_PCLR_REG] = intc_mode_field,
283};
284
285static void intc_enable_disable(struct intc_desc_int *d,
286 unsigned long handle, int do_enable)
287{
288 unsigned long addr;
289 unsigned int cpu;
290 void (*fn)(unsigned long, unsigned long,
291 void (*)(unsigned long, unsigned long, unsigned long),
292 unsigned int);
293
294 if (do_enable) {
295 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
296 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
297 fn = intc_enable_noprio_fns[_INTC_MODE(handle)];
298 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
299 }
300 } else {
301 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
302 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
303 fn = intc_disable_fns[_INTC_MODE(handle)];
304 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
305 }
306 }
307}
308
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000309static int intc_set_wake(unsigned int irq, unsigned int on)
310{
311 return 0; /* allow wakeup, but setup hardware in intc_suspend() */
312}
313
Paul Mundta8941da2010-03-08 13:33:17 +0900314#ifdef CONFIG_SMP
315/*
316 * This is held with the irq desc lock held, so we don't require any
317 * additional locking here at the intc desc level. The affinity mask is
318 * later tested in the enable/disable paths.
319 */
320static int intc_set_affinity(unsigned int irq, const struct cpumask *cpumask)
321{
322 if (!cpumask_intersects(cpumask, cpu_online_mask))
323 return -1;
324
325 cpumask_copy(irq_to_desc(irq)->affinity, cpumask);
326
327 return 0;
328}
329#endif
330
Magnus Dammd58876e2008-04-24 21:36:34 +0900331static void intc_mask_ack(unsigned int irq)
332{
333 struct intc_desc_int *d = get_intc_desc(irq);
334 unsigned long handle = ack_handle[irq];
335 unsigned long addr;
336
337 intc_disable(irq);
338
339 /* read register and write zero only to the assocaited bit */
340
341 if (handle) {
342 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900343 switch (_INTC_FN(handle)) {
344 case REG_FN_MODIFY_BASE + 0: /* 8bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900345 __raw_readb(addr);
346 __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900347 break;
348 case REG_FN_MODIFY_BASE + 1: /* 16bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900349 __raw_readw(addr);
350 __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900351 break;
352 case REG_FN_MODIFY_BASE + 3: /* 32bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900353 __raw_readl(addr);
354 __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900355 break;
356 default:
357 BUG();
358 break;
359 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900360 }
361}
Magnus Dammd58876e2008-04-24 21:36:34 +0900362
Magnus Damm73505b42007-08-12 15:26:12 +0900363static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
364 unsigned int nr_hp,
365 unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900366{
Magnus Damm73505b42007-08-12 15:26:12 +0900367 int i;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900368
Magnus Damm3d37d942007-08-17 00:50:44 +0900369 /* this doesn't scale well, but...
370 *
371 * this function should only be used for cerain uncommon
372 * operations such as intc_set_priority() and intc_set_sense()
373 * and in those rare cases performance doesn't matter that much.
374 * keeping the memory footprint low is more important.
375 *
376 * one rather simple way to speed this up and still keep the
377 * memory footprint down is to make sure the array is sorted
378 * and then perform a bisect to lookup the irq.
379 */
380
Magnus Damm73505b42007-08-12 15:26:12 +0900381 for (i = 0; i < nr_hp; i++) {
382 if ((hp + i)->irq != irq)
383 continue;
384
385 return hp + i;
386 }
387
388 return NULL;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900389}
390
Magnus Damm73505b42007-08-12 15:26:12 +0900391int intc_set_priority(unsigned int irq, unsigned int prio)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900392{
Magnus Damm73505b42007-08-12 15:26:12 +0900393 struct intc_desc_int *d = get_intc_desc(irq);
394 struct intc_handle_int *ihp;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900395
Magnus Damm73505b42007-08-12 15:26:12 +0900396 if (!intc_prio_level[irq] || prio <= 1)
397 return -EINVAL;
398
399 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
400 if (ihp) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900401 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
Magnus Damm73505b42007-08-12 15:26:12 +0900402 return -EINVAL;
403
404 intc_prio_level[irq] = prio;
405
406 /*
407 * only set secondary masking method directly
408 * primary masking method is using intc_prio_level[irq]
409 * priority level will be set during next enable()
410 */
411
Magnus Damm3d37d942007-08-17 00:50:44 +0900412 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
Magnus Damm73505b42007-08-12 15:26:12 +0900413 _intc_enable(irq, ihp->handle);
414 }
415 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900416}
417
418#define VALID(x) (x | 0x80)
419
420static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
421 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
422 [IRQ_TYPE_EDGE_RISING] = VALID(1),
423 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
Magnus Damm720be992008-04-24 21:47:15 +0900424 /* SH7706, SH7707 and SH7709 do not support high level triggered */
425#if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
426 !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
427 !defined(CONFIG_CPU_SUBTYPE_SH7709)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900428 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
Magnus Damm720be992008-04-24 21:47:15 +0900429#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900430};
431
432static int intc_set_sense(unsigned int irq, unsigned int type)
433{
Magnus Damm73505b42007-08-12 15:26:12 +0900434 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900435 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
Magnus Damm73505b42007-08-12 15:26:12 +0900436 struct intc_handle_int *ihp;
437 unsigned long addr;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900438
Magnus Damm73505b42007-08-12 15:26:12 +0900439 if (!value)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900440 return -EINVAL;
441
Magnus Damm73505b42007-08-12 15:26:12 +0900442 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
443 if (ihp) {
Magnus Dammf18d5332007-09-21 18:16:42 +0900444 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900445 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900446 }
Magnus Damm73505b42007-08-12 15:26:12 +0900447 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900448}
449
Magnus Damm73505b42007-08-12 15:26:12 +0900450static unsigned int __init intc_get_reg(struct intc_desc_int *d,
451 unsigned long address)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900452{
Magnus Damm73505b42007-08-12 15:26:12 +0900453 unsigned int k;
454
455 for (k = 0; k < d->nr_reg; k++) {
456 if (d->reg[k] == address)
457 return k;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900458 }
459
460 BUG();
Magnus Damm73505b42007-08-12 15:26:12 +0900461 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900462}
463
Magnus Damm73505b42007-08-12 15:26:12 +0900464static intc_enum __init intc_grp_id(struct intc_desc *desc,
465 intc_enum enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900466{
Magnus Damm577cd752010-02-09 04:24:46 +0000467 struct intc_group *g = desc->hw.groups;
Magnus Damm680c4592007-07-20 12:09:29 +0900468 unsigned int i, j;
469
Magnus Damm577cd752010-02-09 04:24:46 +0000470 for (i = 0; g && enum_id && i < desc->hw.nr_groups; i++) {
471 g = desc->hw.groups + i;
Magnus Damm680c4592007-07-20 12:09:29 +0900472
473 for (j = 0; g->enum_ids[j]; j++) {
474 if (g->enum_ids[j] != enum_id)
475 continue;
476
477 return g->enum_id;
478 }
479 }
480
481 return 0;
482}
483
Magnus Dammd5190952010-02-09 04:29:22 +0000484static unsigned int __init _intc_mask_data(struct intc_desc *desc,
485 struct intc_desc_int *d,
486 intc_enum enum_id,
487 unsigned int *reg_idx,
488 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900489{
Magnus Damm577cd752010-02-09 04:24:46 +0000490 struct intc_mask_reg *mr = desc->hw.mask_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000491 unsigned int fn, mode;
Magnus Damm73505b42007-08-12 15:26:12 +0900492 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900493
Magnus Dammd5190952010-02-09 04:29:22 +0000494 while (mr && enum_id && *reg_idx < desc->hw.nr_mask_regs) {
495 mr = desc->hw.mask_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900496
Magnus Dammd5190952010-02-09 04:29:22 +0000497 for (; *fld_idx < ARRAY_SIZE(mr->enum_ids); (*fld_idx)++) {
498 if (mr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900499 continue;
500
Magnus Damm73505b42007-08-12 15:26:12 +0900501 if (mr->set_reg && mr->clr_reg) {
502 fn = REG_FN_WRITE_BASE;
503 mode = MODE_DUAL_REG;
504 reg_e = mr->clr_reg;
505 reg_d = mr->set_reg;
506 } else {
507 fn = REG_FN_MODIFY_BASE;
508 if (mr->set_reg) {
509 mode = MODE_ENABLE_REG;
510 reg_e = mr->set_reg;
511 reg_d = mr->set_reg;
512 } else {
513 mode = MODE_MASK_REG;
514 reg_e = mr->clr_reg;
515 reg_d = mr->clr_reg;
516 }
Magnus Damm51da6422007-08-03 14:25:32 +0900517 }
518
Magnus Damm73505b42007-08-12 15:26:12 +0900519 fn += (mr->reg_width >> 3) - 1;
520 return _INTC_MK(fn, mode,
521 intc_get_reg(d, reg_e),
522 intc_get_reg(d, reg_d),
523 1,
Magnus Dammd5190952010-02-09 04:29:22 +0000524 (mr->reg_width - 1) - *fld_idx);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900525 }
Magnus Dammd5190952010-02-09 04:29:22 +0000526
527 *fld_idx = 0;
528 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900529 }
530
Magnus Dammd5190952010-02-09 04:29:22 +0000531 return 0;
532}
533
534static unsigned int __init intc_mask_data(struct intc_desc *desc,
535 struct intc_desc_int *d,
536 intc_enum enum_id, int do_grps)
537{
538 unsigned int i = 0;
539 unsigned int j = 0;
540 unsigned int ret;
541
542 ret = _intc_mask_data(desc, d, enum_id, &i, &j);
543 if (ret)
544 return ret;
545
Magnus Damm680c4592007-07-20 12:09:29 +0900546 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900547 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900548
Magnus Damm02ab3f72007-07-18 17:25:09 +0900549 return 0;
550}
551
Magnus Dammd5190952010-02-09 04:29:22 +0000552static unsigned int __init _intc_prio_data(struct intc_desc *desc,
553 struct intc_desc_int *d,
554 intc_enum enum_id,
555 unsigned int *reg_idx,
556 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900557{
Magnus Damm577cd752010-02-09 04:24:46 +0000558 struct intc_prio_reg *pr = desc->hw.prio_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000559 unsigned int fn, n, mode, bit;
Magnus Damm73505b42007-08-12 15:26:12 +0900560 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900561
Magnus Dammd5190952010-02-09 04:29:22 +0000562 while (pr && enum_id && *reg_idx < desc->hw.nr_prio_regs) {
563 pr = desc->hw.prio_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900564
Magnus Dammd5190952010-02-09 04:29:22 +0000565 for (; *fld_idx < ARRAY_SIZE(pr->enum_ids); (*fld_idx)++) {
566 if (pr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900567 continue;
568
Magnus Damm73505b42007-08-12 15:26:12 +0900569 if (pr->set_reg && pr->clr_reg) {
570 fn = REG_FN_WRITE_BASE;
571 mode = MODE_PCLR_REG;
572 reg_e = pr->set_reg;
573 reg_d = pr->clr_reg;
574 } else {
575 fn = REG_FN_MODIFY_BASE;
576 mode = MODE_PRIO_REG;
577 if (!pr->set_reg)
578 BUG();
579 reg_e = pr->set_reg;
580 reg_d = pr->set_reg;
581 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900582
Magnus Damm73505b42007-08-12 15:26:12 +0900583 fn += (pr->reg_width >> 3) - 1;
Magnus Dammd5190952010-02-09 04:29:22 +0000584 n = *fld_idx + 1;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900585
Magnus Dammd5190952010-02-09 04:29:22 +0000586 BUG_ON(n * pr->field_width > pr->reg_width);
roel kluinb21a9102008-09-09 23:02:43 +0200587
Magnus Dammd5190952010-02-09 04:29:22 +0000588 bit = pr->reg_width - (n * pr->field_width);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900589
Magnus Damm73505b42007-08-12 15:26:12 +0900590 return _INTC_MK(fn, mode,
591 intc_get_reg(d, reg_e),
592 intc_get_reg(d, reg_d),
593 pr->field_width, bit);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900594 }
Magnus Dammd5190952010-02-09 04:29:22 +0000595
596 *fld_idx = 0;
597 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900598 }
599
Magnus Dammd5190952010-02-09 04:29:22 +0000600 return 0;
601}
602
603static unsigned int __init intc_prio_data(struct intc_desc *desc,
604 struct intc_desc_int *d,
605 intc_enum enum_id, int do_grps)
606{
607 unsigned int i = 0;
608 unsigned int j = 0;
609 unsigned int ret;
610
611 ret = _intc_prio_data(desc, d, enum_id, &i, &j);
612 if (ret)
613 return ret;
614
Magnus Damm680c4592007-07-20 12:09:29 +0900615 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900616 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900617
Magnus Damm02ab3f72007-07-18 17:25:09 +0900618 return 0;
619}
620
Magnus Dammd5190952010-02-09 04:29:22 +0000621static void __init intc_enable_disable_enum(struct intc_desc *desc,
622 struct intc_desc_int *d,
623 intc_enum enum_id, int enable)
624{
625 unsigned int i, j, data;
626
627 /* go through and enable/disable all mask bits */
628 i = j = 0;
629 do {
630 data = _intc_mask_data(desc, d, enum_id, &i, &j);
631 if (data)
632 intc_enable_disable(d, data, enable);
633 j++;
634 } while (data);
635
636 /* go through and enable/disable all priority fields */
637 i = j = 0;
638 do {
639 data = _intc_prio_data(desc, d, enum_id, &i, &j);
640 if (data)
641 intc_enable_disable(d, data, enable);
642
643 j++;
644 } while (data);
645}
646
Magnus Dammd58876e2008-04-24 21:36:34 +0900647static unsigned int __init intc_ack_data(struct intc_desc *desc,
648 struct intc_desc_int *d,
649 intc_enum enum_id)
650{
Magnus Damm577cd752010-02-09 04:24:46 +0000651 struct intc_mask_reg *mr = desc->hw.ack_regs;
Magnus Dammd58876e2008-04-24 21:36:34 +0900652 unsigned int i, j, fn, mode;
653 unsigned long reg_e, reg_d;
654
Magnus Damm577cd752010-02-09 04:24:46 +0000655 for (i = 0; mr && enum_id && i < desc->hw.nr_ack_regs; i++) {
656 mr = desc->hw.ack_regs + i;
Magnus Dammd58876e2008-04-24 21:36:34 +0900657
658 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
659 if (mr->enum_ids[j] != enum_id)
660 continue;
661
662 fn = REG_FN_MODIFY_BASE;
663 mode = MODE_ENABLE_REG;
664 reg_e = mr->set_reg;
665 reg_d = mr->set_reg;
666
667 fn += (mr->reg_width >> 3) - 1;
668 return _INTC_MK(fn, mode,
669 intc_get_reg(d, reg_e),
670 intc_get_reg(d, reg_d),
671 1,
672 (mr->reg_width - 1) - j);
673 }
674 }
675
676 return 0;
677}
Magnus Dammd58876e2008-04-24 21:36:34 +0900678
Magnus Damm73505b42007-08-12 15:26:12 +0900679static unsigned int __init intc_sense_data(struct intc_desc *desc,
680 struct intc_desc_int *d,
681 intc_enum enum_id)
682{
Magnus Damm577cd752010-02-09 04:24:46 +0000683 struct intc_sense_reg *sr = desc->hw.sense_regs;
Magnus Damm73505b42007-08-12 15:26:12 +0900684 unsigned int i, j, fn, bit;
685
Magnus Damm577cd752010-02-09 04:24:46 +0000686 for (i = 0; sr && enum_id && i < desc->hw.nr_sense_regs; i++) {
687 sr = desc->hw.sense_regs + i;
Magnus Damm73505b42007-08-12 15:26:12 +0900688
689 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
690 if (sr->enum_ids[j] != enum_id)
691 continue;
692
693 fn = REG_FN_MODIFY_BASE;
694 fn += (sr->reg_width >> 3) - 1;
Magnus Damm73505b42007-08-12 15:26:12 +0900695
roel kluinb21a9102008-09-09 23:02:43 +0200696 BUG_ON((j + 1) * sr->field_width > sr->reg_width);
697
698 bit = sr->reg_width - ((j + 1) * sr->field_width);
Magnus Damm73505b42007-08-12 15:26:12 +0900699
700 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
701 0, sr->field_width, bit);
702 }
703 }
704
705 return 0;
706}
707
708static void __init intc_register_irq(struct intc_desc *desc,
709 struct intc_desc_int *d,
710 intc_enum enum_id,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900711 unsigned int irq)
712{
Magnus Damm3d37d942007-08-17 00:50:44 +0900713 struct intc_handle_int *hp;
Magnus Damm680c4592007-07-20 12:09:29 +0900714 unsigned int data[2], primary;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900715
Paul Mundt1ce7b032009-11-02 10:30:26 +0900716 /*
717 * Register the IRQ position with the global IRQ map
718 */
719 set_bit(irq, intc_irq_map);
720
Magnus Damm680c4592007-07-20 12:09:29 +0900721 /* Prefer single interrupt source bitmap over other combinations:
722 * 1. bitmap, single interrupt source
723 * 2. priority, single interrupt source
724 * 3. bitmap, multiple interrupt sources (groups)
725 * 4. priority, multiple interrupt sources (groups)
726 */
727
Magnus Damm73505b42007-08-12 15:26:12 +0900728 data[0] = intc_mask_data(desc, d, enum_id, 0);
729 data[1] = intc_prio_data(desc, d, enum_id, 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900730
731 primary = 0;
732 if (!data[0] && data[1])
733 primary = 1;
734
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900735 if (!data[0] && !data[1])
Paul Mundtf0335992009-03-06 17:56:58 +0900736 pr_warning("intc: missing unique irq mask for "
737 "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900738
Magnus Damm73505b42007-08-12 15:26:12 +0900739 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
740 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
Magnus Damm680c4592007-07-20 12:09:29 +0900741
742 if (!data[primary])
743 primary ^= 1;
744
745 BUG_ON(!data[primary]); /* must have primary masking method */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900746
747 disable_irq_nosync(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900748 set_irq_chip_and_handler_name(irq, &d->chip,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900749 handle_level_irq, "level");
Magnus Damm680c4592007-07-20 12:09:29 +0900750 set_irq_chip_data(irq, (void *)data[primary]);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900751
Magnus Damm7f3edee2008-01-10 14:08:55 +0900752 /* set priority level
753 * - this needs to be at least 2 for 5-bit priorities on 7780
754 */
755 intc_prio_level[irq] = 2;
Magnus Damm73505b42007-08-12 15:26:12 +0900756
Magnus Damm680c4592007-07-20 12:09:29 +0900757 /* enable secondary masking method if present */
758 if (data[!primary])
Magnus Damm73505b42007-08-12 15:26:12 +0900759 _intc_enable(irq, data[!primary]);
760
761 /* add irq to d->prio list if priority is available */
762 if (data[1]) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900763 hp = d->prio + d->nr_prio;
764 hp->irq = irq;
765 hp->handle = data[1];
766
767 if (primary) {
768 /*
769 * only secondary priority should access registers, so
770 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
771 */
772
773 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
774 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
775 }
Magnus Damm73505b42007-08-12 15:26:12 +0900776 d->nr_prio++;
777 }
778
779 /* add irq to d->sense list if sense is available */
780 data[0] = intc_sense_data(desc, d, enum_id);
781 if (data[0]) {
782 (d->sense + d->nr_sense)->irq = irq;
783 (d->sense + d->nr_sense)->handle = data[0];
784 d->nr_sense++;
785 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900786
787 /* irq should be disabled by default */
Magnus Damm73505b42007-08-12 15:26:12 +0900788 d->chip.mask(irq);
Magnus Dammd58876e2008-04-24 21:36:34 +0900789
Magnus Damm577cd752010-02-09 04:24:46 +0000790 if (desc->hw.ack_regs)
Magnus Dammd58876e2008-04-24 21:36:34 +0900791 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
Magnus Damm65a5b282010-02-05 11:15:25 +0000792
793#ifdef CONFIG_ARM
794 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
795#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900796}
797
Magnus Dammf18d5332007-09-21 18:16:42 +0900798static unsigned int __init save_reg(struct intc_desc_int *d,
799 unsigned int cnt,
800 unsigned long value,
801 unsigned int smp)
802{
803 if (value) {
804 d->reg[cnt] = value;
805#ifdef CONFIG_SMP
806 d->smp[cnt] = smp;
807#endif
808 return 1;
809 }
810
811 return 0;
812}
813
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900814static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900815{
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900816 generic_handle_irq((unsigned int)get_irq_data(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900817}
Magnus Dammf18d5332007-09-21 18:16:42 +0900818
Magnus Damm02ab3f72007-07-18 17:25:09 +0900819void __init register_intc_controller(struct intc_desc *desc)
820{
Paul Mundt54ff3282009-06-11 10:33:09 +0300821 unsigned int i, k, smp;
Magnus Damm577cd752010-02-09 04:24:46 +0000822 struct intc_hw_desc *hw = &desc->hw;
Magnus Damm73505b42007-08-12 15:26:12 +0900823 struct intc_desc_int *d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900824
Paul Mundt11b6aa92009-06-12 01:34:12 +0300825 d = kzalloc(sizeof(*d), GFP_NOWAIT);
Magnus Damm73505b42007-08-12 15:26:12 +0900826
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000827 INIT_LIST_HEAD(&d->list);
828 list_add(&d->list, &intc_list);
829
Magnus Damm577cd752010-02-09 04:24:46 +0000830 d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
831 d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
832 d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
833 d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
Paul Mundt9b798d52009-10-27 11:36:43 +0900834
Paul Mundt11b6aa92009-06-12 01:34:12 +0300835 d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
Magnus Dammf18d5332007-09-21 18:16:42 +0900836#ifdef CONFIG_SMP
Paul Mundt11b6aa92009-06-12 01:34:12 +0300837 d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
Magnus Dammf18d5332007-09-21 18:16:42 +0900838#endif
Magnus Damm73505b42007-08-12 15:26:12 +0900839 k = 0;
840
Magnus Damm577cd752010-02-09 04:24:46 +0000841 if (hw->mask_regs) {
842 for (i = 0; i < hw->nr_mask_regs; i++) {
843 smp = IS_SMP(hw->mask_regs[i]);
844 k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
845 k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900846 }
847 }
848
Magnus Damm577cd752010-02-09 04:24:46 +0000849 if (hw->prio_regs) {
850 d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
851 GFP_NOWAIT);
Magnus Damm73505b42007-08-12 15:26:12 +0900852
Magnus Damm577cd752010-02-09 04:24:46 +0000853 for (i = 0; i < hw->nr_prio_regs; i++) {
854 smp = IS_SMP(hw->prio_regs[i]);
855 k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
856 k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900857 }
858 }
859
Magnus Damm577cd752010-02-09 04:24:46 +0000860 if (hw->sense_regs) {
861 d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
862 GFP_NOWAIT);
Magnus Damm73505b42007-08-12 15:26:12 +0900863
Magnus Damm577cd752010-02-09 04:24:46 +0000864 for (i = 0; i < hw->nr_sense_regs; i++)
865 k += save_reg(d, k, hw->sense_regs[i].reg, 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900866 }
867
Magnus Damm73505b42007-08-12 15:26:12 +0900868 d->chip.name = desc->name;
869 d->chip.mask = intc_disable;
870 d->chip.unmask = intc_enable;
871 d->chip.mask_ack = intc_disable;
Magnus Dammf7dd2542009-04-01 14:20:58 +0000872 d->chip.enable = intc_enable;
873 d->chip.disable = intc_disable;
874 d->chip.shutdown = intc_disable;
Magnus Damm73505b42007-08-12 15:26:12 +0900875 d->chip.set_type = intc_set_sense;
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000876 d->chip.set_wake = intc_set_wake;
Paul Mundta8941da2010-03-08 13:33:17 +0900877#ifdef CONFIG_SMP
878 d->chip.set_affinity = intc_set_affinity;
879#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900880
Magnus Damm577cd752010-02-09 04:24:46 +0000881 if (hw->ack_regs) {
882 for (i = 0; i < hw->nr_ack_regs; i++)
883 k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
Magnus Dammd58876e2008-04-24 21:36:34 +0900884
885 d->chip.mask_ack = intc_mask_ack;
886 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900887
Magnus Dammd85429a2010-02-15 11:40:25 +0000888 /* disable bits matching force_disable before registering irqs */
889 if (desc->force_disable)
890 intc_enable_disable_enum(desc, d, desc->force_disable, 0);
Magnus Dammd5190952010-02-09 04:29:22 +0000891
892 /* disable bits matching force_enable before registering irqs */
893 if (desc->force_enable)
894 intc_enable_disable_enum(desc, d, desc->force_enable, 0);
895
Magnus Dammd58876e2008-04-24 21:36:34 +0900896 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
897
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900898 /* register the vectors one by one */
Magnus Damm577cd752010-02-09 04:24:46 +0000899 for (i = 0; i < hw->nr_vectors; i++) {
900 struct intc_vect *vect = hw->vectors + i;
Paul Mundt05ff3002009-05-22 01:28:33 +0900901 unsigned int irq = evt2irq(vect->vect);
902 struct irq_desc *irq_desc;
Paul Mundt54ff3282009-06-11 10:33:09 +0300903
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900904 if (!vect->enum_id)
905 continue;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900906
Paul Mundt54ff3282009-06-11 10:33:09 +0300907 irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
Paul Mundt05ff3002009-05-22 01:28:33 +0900908 if (unlikely(!irq_desc)) {
Paul Mundt1279b7f2009-08-31 15:15:33 +0900909 pr_info("can't get irq_desc for %d\n", irq);
Paul Mundt05ff3002009-05-22 01:28:33 +0900910 continue;
911 }
912
913 intc_register_irq(desc, d, vect->enum_id, irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900914
Magnus Damm577cd752010-02-09 04:24:46 +0000915 for (k = i + 1; k < hw->nr_vectors; k++) {
916 struct intc_vect *vect2 = hw->vectors + k;
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900917 unsigned int irq2 = evt2irq(vect2->vect);
918
919 if (vect->enum_id != vect2->enum_id)
920 continue;
921
Paul Mundt1279b7f2009-08-31 15:15:33 +0900922 /*
923 * In the case of multi-evt handling and sparse
924 * IRQ support, each vector still needs to have
925 * its own backing irq_desc.
926 */
927 irq_desc = irq_to_desc_alloc_node(irq2, numa_node_id());
928 if (unlikely(!irq_desc)) {
929 pr_info("can't get irq_desc for %d\n", irq2);
930 continue;
931 }
932
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900933 vect2->enum_id = 0;
934
935 /* redirect this interrupts to the first one */
Paul Mundt4d2185d2010-02-17 12:37:42 +0900936 set_irq_chip(irq2, &dummy_irq_chip);
Magnus Damme6f07752010-02-09 07:17:20 +0000937 set_irq_chained_handler(irq2, intc_redirect_irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900938 set_irq_data(irq2, (void *)irq);
939 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900940 }
Magnus Dammd5190952010-02-09 04:29:22 +0000941
942 /* enable bits matching force_enable after registering irqs */
943 if (desc->force_enable)
944 intc_enable_disable_enum(desc, d, desc->force_enable, 1);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900945}
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000946
947static int intc_suspend(struct sys_device *dev, pm_message_t state)
948{
949 struct intc_desc_int *d;
950 struct irq_desc *desc;
951 int irq;
952
953 /* get intc controller associated with this sysdev */
954 d = container_of(dev, struct intc_desc_int, sysdev);
955
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000956 switch (state.event) {
957 case PM_EVENT_ON:
958 if (d->state.event != PM_EVENT_FREEZE)
959 break;
960 for_each_irq_desc(irq, desc) {
Francesco VIRLINZI87a705d2009-12-04 08:57:58 +0000961 if (desc->handle_irq == intc_redirect_irq)
Paul Mundt0a753d52009-12-09 14:36:16 +0900962 continue;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000963 if (desc->chip != &d->chip)
964 continue;
965 if (desc->status & IRQ_DISABLED)
966 intc_disable(irq);
967 else
968 intc_enable(irq);
969 }
970 break;
971 case PM_EVENT_FREEZE:
972 /* nothing has to be done */
973 break;
974 case PM_EVENT_SUSPEND:
975 /* enable wakeup irqs belonging to this intc controller */
976 for_each_irq_desc(irq, desc) {
977 if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
978 intc_enable(irq);
979 }
980 break;
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000981 }
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000982 d->state = state;
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000983
984 return 0;
985}
986
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000987static int intc_resume(struct sys_device *dev)
988{
989 return intc_suspend(dev, PMSG_ON);
990}
991
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000992static struct sysdev_class intc_sysdev_class = {
993 .name = "intc",
994 .suspend = intc_suspend,
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000995 .resume = intc_resume,
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000996};
997
998/* register this intc as sysdev to allow suspend/resume */
999static int __init register_intc_sysdevs(void)
1000{
1001 struct intc_desc_int *d;
1002 int error;
1003 int id = 0;
1004
1005 error = sysdev_class_register(&intc_sysdev_class);
1006 if (!error) {
1007 list_for_each_entry(d, &intc_list, list) {
1008 d->sysdev.id = id;
1009 d->sysdev.cls = &intc_sysdev_class;
1010 error = sysdev_register(&d->sysdev);
1011 if (error)
1012 break;
1013 id++;
1014 }
1015 }
1016
1017 if (error)
1018 pr_warning("intc: sysdev registration error\n");
1019
1020 return error;
1021}
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001022device_initcall(register_intc_sysdevs);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001023
1024/*
1025 * Dynamic IRQ allocation and deallocation
1026 */
Paul Mundte9867c52010-02-02 17:35:13 +09001027unsigned int create_irq_nr(unsigned int irq_want, int node)
Paul Mundt1ce7b032009-11-02 10:30:26 +09001028{
1029 unsigned int irq = 0, new;
1030 unsigned long flags;
1031 struct irq_desc *desc;
1032
1033 spin_lock_irqsave(&vector_lock, flags);
1034
1035 /*
Paul Mundte9867c52010-02-02 17:35:13 +09001036 * First try the wanted IRQ
Paul Mundt1ce7b032009-11-02 10:30:26 +09001037 */
Paul Mundte9867c52010-02-02 17:35:13 +09001038 if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
1039 new = irq_want;
1040 } else {
1041 /* .. then fall back to scanning. */
Paul Mundt1ce7b032009-11-02 10:30:26 +09001042 new = find_first_zero_bit(intc_irq_map, nr_irqs);
1043 if (unlikely(new == nr_irqs))
1044 goto out_unlock;
1045
Paul Mundt1ce7b032009-11-02 10:30:26 +09001046 __set_bit(new, intc_irq_map);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001047 }
1048
Paul Mundte9867c52010-02-02 17:35:13 +09001049 desc = irq_to_desc_alloc_node(new, node);
1050 if (unlikely(!desc)) {
1051 pr_info("can't get irq_desc for %d\n", new);
1052 goto out_unlock;
1053 }
1054
1055 desc = move_irq_desc(desc, node);
1056 irq = new;
1057
Paul Mundt1ce7b032009-11-02 10:30:26 +09001058out_unlock:
1059 spin_unlock_irqrestore(&vector_lock, flags);
1060
Magnus Damm65a5b282010-02-05 11:15:25 +00001061 if (irq > 0) {
Paul Mundt1ce7b032009-11-02 10:30:26 +09001062 dynamic_irq_init(irq);
Magnus Damm65a5b282010-02-05 11:15:25 +00001063#ifdef CONFIG_ARM
1064 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
1065#endif
1066 }
Paul Mundt1ce7b032009-11-02 10:30:26 +09001067
1068 return irq;
1069}
1070
1071int create_irq(void)
1072{
1073 int nid = cpu_to_node(smp_processor_id());
1074 int irq;
1075
Paul Mundte9867c52010-02-02 17:35:13 +09001076 irq = create_irq_nr(NR_IRQS_LEGACY, nid);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001077 if (irq == 0)
1078 irq = -1;
1079
1080 return irq;
1081}
1082
1083void destroy_irq(unsigned int irq)
1084{
1085 unsigned long flags;
1086
1087 dynamic_irq_cleanup(irq);
1088
1089 spin_lock_irqsave(&vector_lock, flags);
1090 __clear_bit(irq, intc_irq_map);
1091 spin_unlock_irqrestore(&vector_lock, flags);
1092}
Paul Mundt45b9dea2009-11-02 15:43:20 +09001093
1094int reserve_irq_vector(unsigned int irq)
1095{
1096 unsigned long flags;
1097 int ret = 0;
1098
1099 spin_lock_irqsave(&vector_lock, flags);
1100 if (test_and_set_bit(irq, intc_irq_map))
1101 ret = -EBUSY;
1102 spin_unlock_irqrestore(&vector_lock, flags);
1103
1104 return ret;
1105}
1106
1107void reserve_irq_legacy(void)
1108{
1109 unsigned long flags;
1110 int i, j;
1111
1112 spin_lock_irqsave(&vector_lock, flags);
1113 j = find_first_bit(intc_irq_map, nr_irqs);
1114 for (i = 0; i < j; i++)
1115 __set_bit(i, intc_irq_map);
1116 spin_unlock_irqrestore(&vector_lock, flags);
1117}