blob: a3d8677af6a52dd1db005fcb13d3fc12ede75c16 [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>
Magnus Damm02ab3f72007-07-18 17:25:09 +090030
Magnus Damm73505b42007-08-12 15:26:12 +090031#define _INTC_MK(fn, mode, addr_e, addr_d, width, shift) \
32 ((shift) | ((width) << 5) | ((fn) << 9) | ((mode) << 13) | \
33 ((addr_e) << 16) | ((addr_d << 24)))
Magnus Damm02ab3f72007-07-18 17:25:09 +090034
Magnus Damm73505b42007-08-12 15:26:12 +090035#define _INTC_SHIFT(h) (h & 0x1f)
36#define _INTC_WIDTH(h) ((h >> 5) & 0xf)
37#define _INTC_FN(h) ((h >> 9) & 0xf)
38#define _INTC_MODE(h) ((h >> 13) & 0x7)
39#define _INTC_ADDR_E(h) ((h >> 16) & 0xff)
40#define _INTC_ADDR_D(h) ((h >> 24) & 0xff)
Magnus Damm02ab3f72007-07-18 17:25:09 +090041
Magnus Damm73505b42007-08-12 15:26:12 +090042struct intc_handle_int {
43 unsigned int irq;
44 unsigned long handle;
45};
46
47struct intc_desc_int {
Magnus Damm2dcec7a2009-04-01 14:30:59 +000048 struct list_head list;
49 struct sys_device sysdev;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +000050 pm_message_t state;
Magnus Damm73505b42007-08-12 15:26:12 +090051 unsigned long *reg;
Magnus Dammf18d5332007-09-21 18:16:42 +090052#ifdef CONFIG_SMP
53 unsigned long *smp;
54#endif
Magnus Damm73505b42007-08-12 15:26:12 +090055 unsigned int nr_reg;
56 struct intc_handle_int *prio;
57 unsigned int nr_prio;
58 struct intc_handle_int *sense;
59 unsigned int nr_sense;
60 struct irq_chip chip;
61};
62
Magnus Damm2dcec7a2009-04-01 14:30:59 +000063static LIST_HEAD(intc_list);
64
Paul Mundt1ce7b032009-11-02 10:30:26 +090065/*
66 * The intc_irq_map provides a global map of bound IRQ vectors for a
67 * given platform. Allocation of IRQs are either static through the CPU
68 * vector map, or dynamic in the case of board mux vectors or MSI.
69 *
70 * As this is a central point for all IRQ controllers on the system,
71 * each of the available sources are mapped out here. This combined with
72 * sparseirq makes it quite trivial to keep the vector map tightly packed
73 * when dynamically creating IRQs, as well as tying in to otherwise
74 * unused irq_desc positions in the sparse array.
75 */
76static DECLARE_BITMAP(intc_irq_map, NR_IRQS);
77static DEFINE_SPINLOCK(vector_lock);
78
Magnus Dammf18d5332007-09-21 18:16:42 +090079#ifdef CONFIG_SMP
80#define IS_SMP(x) x.smp
81#define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
82#define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
83#else
84#define IS_SMP(x) 0
85#define INTC_REG(d, x, c) (d->reg[(x)])
86#define SMP_NR(d, x) 1
87#endif
88
Magnus Damm73505b42007-08-12 15:26:12 +090089static unsigned int intc_prio_level[NR_IRQS]; /* for now */
Magnus Dammd58876e2008-04-24 21:36:34 +090090static unsigned long ack_handle[NR_IRQS];
Magnus Damm73505b42007-08-12 15:26:12 +090091
92static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +090093{
94 struct irq_chip *chip = get_irq_chip(irq);
Stuart Menefy6000fc42009-08-24 18:27:33 +090095 return container_of(chip, struct intc_desc_int, chip);
Magnus Damm02ab3f72007-07-18 17:25:09 +090096}
97
98static inline unsigned int set_field(unsigned int value,
99 unsigned int field_value,
Magnus Damm73505b42007-08-12 15:26:12 +0900100 unsigned int handle)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900101{
Magnus Damm73505b42007-08-12 15:26:12 +0900102 unsigned int width = _INTC_WIDTH(handle);
103 unsigned int shift = _INTC_SHIFT(handle);
104
Magnus Damm02ab3f72007-07-18 17:25:09 +0900105 value &= ~(((1 << width) - 1) << shift);
106 value |= field_value << shift;
107 return value;
108}
109
Magnus Damm73505b42007-08-12 15:26:12 +0900110static void write_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900111{
Paul Mundt62429e02008-10-01 15:19:10 +0900112 __raw_writeb(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900113 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900114}
115
Magnus Damm73505b42007-08-12 15:26:12 +0900116static void write_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900117{
Paul Mundt62429e02008-10-01 15:19:10 +0900118 __raw_writew(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900119 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900120}
121
Magnus Damm73505b42007-08-12 15:26:12 +0900122static void write_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900123{
Paul Mundt62429e02008-10-01 15:19:10 +0900124 __raw_writel(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900125 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900126}
127
Magnus Damm73505b42007-08-12 15:26:12 +0900128static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900129{
Magnus Damm4370fe12008-04-24 21:53:07 +0900130 unsigned long flags;
131 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900132 __raw_writeb(set_field(__raw_readb(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900133 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900134 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900135}
136
Magnus Damm73505b42007-08-12 15:26:12 +0900137static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900138{
Magnus Damm4370fe12008-04-24 21:53:07 +0900139 unsigned long flags;
140 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900141 __raw_writew(set_field(__raw_readw(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900142 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900143 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900144}
145
Magnus Damm73505b42007-08-12 15:26:12 +0900146static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900147{
Magnus Damm4370fe12008-04-24 21:53:07 +0900148 unsigned long flags;
149 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900150 __raw_writel(set_field(__raw_readl(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900151 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900152 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900153}
154
Magnus Damm73505b42007-08-12 15:26:12 +0900155enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
Magnus Damm02ab3f72007-07-18 17:25:09 +0900156
Magnus Damm73505b42007-08-12 15:26:12 +0900157static void (*intc_reg_fns[])(unsigned long addr,
158 unsigned long h,
159 unsigned long data) = {
160 [REG_FN_WRITE_BASE + 0] = write_8,
161 [REG_FN_WRITE_BASE + 1] = write_16,
162 [REG_FN_WRITE_BASE + 3] = write_32,
163 [REG_FN_MODIFY_BASE + 0] = modify_8,
164 [REG_FN_MODIFY_BASE + 1] = modify_16,
165 [REG_FN_MODIFY_BASE + 3] = modify_32,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900166};
167
Magnus Damm73505b42007-08-12 15:26:12 +0900168enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
169 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
170 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
171 MODE_PRIO_REG, /* Priority value written to enable interrupt */
172 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
173};
174
175static void intc_mode_field(unsigned long addr,
176 unsigned long handle,
177 void (*fn)(unsigned long,
178 unsigned long,
179 unsigned long),
180 unsigned int irq)
181{
182 fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
183}
184
185static void intc_mode_zero(unsigned long addr,
186 unsigned long handle,
187 void (*fn)(unsigned long,
188 unsigned long,
189 unsigned long),
190 unsigned int irq)
191{
192 fn(addr, handle, 0);
193}
194
195static void intc_mode_prio(unsigned long addr,
196 unsigned long handle,
197 void (*fn)(unsigned long,
198 unsigned long,
199 unsigned long),
200 unsigned int irq)
201{
202 fn(addr, handle, intc_prio_level[irq]);
203}
204
205static void (*intc_enable_fns[])(unsigned long addr,
206 unsigned long handle,
207 void (*fn)(unsigned long,
208 unsigned long,
209 unsigned long),
210 unsigned int irq) = {
211 [MODE_ENABLE_REG] = intc_mode_field,
212 [MODE_MASK_REG] = intc_mode_zero,
213 [MODE_DUAL_REG] = intc_mode_field,
214 [MODE_PRIO_REG] = intc_mode_prio,
215 [MODE_PCLR_REG] = intc_mode_prio,
216};
217
218static void (*intc_disable_fns[])(unsigned long addr,
219 unsigned long handle,
220 void (*fn)(unsigned long,
221 unsigned long,
222 unsigned long),
223 unsigned int irq) = {
224 [MODE_ENABLE_REG] = intc_mode_zero,
225 [MODE_MASK_REG] = intc_mode_field,
226 [MODE_DUAL_REG] = intc_mode_field,
227 [MODE_PRIO_REG] = intc_mode_zero,
228 [MODE_PCLR_REG] = intc_mode_field,
229};
230
231static inline void _intc_enable(unsigned int irq, unsigned long handle)
232{
233 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900234 unsigned long addr;
235 unsigned int cpu;
Magnus Damm73505b42007-08-12 15:26:12 +0900236
Magnus Dammf18d5332007-09-21 18:16:42 +0900237 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
Paul Mundta8941da2010-03-08 13:33:17 +0900238#ifdef CONFIG_SMP
239 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
240 continue;
241#endif
Magnus Dammf18d5332007-09-21 18:16:42 +0900242 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
243 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
244 [_INTC_FN(handle)], irq);
245 }
Magnus Damm73505b42007-08-12 15:26:12 +0900246}
247
Magnus Damm02ab3f72007-07-18 17:25:09 +0900248static void intc_enable(unsigned int irq)
249{
Magnus Damm73505b42007-08-12 15:26:12 +0900250 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
Magnus Damm02ab3f72007-07-18 17:25:09 +0900251}
252
253static void intc_disable(unsigned int irq)
254{
Magnus Dammf18d5332007-09-21 18:16:42 +0900255 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900256 unsigned long handle = (unsigned long) get_irq_chip_data(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900257 unsigned long addr;
258 unsigned int cpu;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900259
Magnus Dammf18d5332007-09-21 18:16:42 +0900260 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
Paul Mundta8941da2010-03-08 13:33:17 +0900261#ifdef CONFIG_SMP
262 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
263 continue;
264#endif
Magnus Dammf18d5332007-09-21 18:16:42 +0900265 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
266 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
267 [_INTC_FN(handle)], irq);
268 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900269}
270
Magnus Dammd5190952010-02-09 04:29:22 +0000271static void (*intc_enable_noprio_fns[])(unsigned long addr,
272 unsigned long handle,
273 void (*fn)(unsigned long,
274 unsigned long,
275 unsigned long),
276 unsigned int irq) = {
277 [MODE_ENABLE_REG] = intc_mode_field,
278 [MODE_MASK_REG] = intc_mode_zero,
279 [MODE_DUAL_REG] = intc_mode_field,
280 [MODE_PRIO_REG] = intc_mode_field,
281 [MODE_PCLR_REG] = intc_mode_field,
282};
283
284static void intc_enable_disable(struct intc_desc_int *d,
285 unsigned long handle, int do_enable)
286{
287 unsigned long addr;
288 unsigned int cpu;
289 void (*fn)(unsigned long, unsigned long,
290 void (*)(unsigned long, unsigned long, unsigned long),
291 unsigned int);
292
293 if (do_enable) {
294 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
295 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
296 fn = intc_enable_noprio_fns[_INTC_MODE(handle)];
297 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
298 }
299 } else {
300 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
301 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
302 fn = intc_disable_fns[_INTC_MODE(handle)];
303 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
304 }
305 }
306}
307
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000308static int intc_set_wake(unsigned int irq, unsigned int on)
309{
310 return 0; /* allow wakeup, but setup hardware in intc_suspend() */
311}
312
Paul Mundta8941da2010-03-08 13:33:17 +0900313#ifdef CONFIG_SMP
314/*
315 * This is held with the irq desc lock held, so we don't require any
316 * additional locking here at the intc desc level. The affinity mask is
317 * later tested in the enable/disable paths.
318 */
319static int intc_set_affinity(unsigned int irq, const struct cpumask *cpumask)
320{
321 if (!cpumask_intersects(cpumask, cpu_online_mask))
322 return -1;
323
324 cpumask_copy(irq_to_desc(irq)->affinity, cpumask);
325
326 return 0;
327}
328#endif
329
Magnus Dammd58876e2008-04-24 21:36:34 +0900330static void intc_mask_ack(unsigned int irq)
331{
332 struct intc_desc_int *d = get_intc_desc(irq);
333 unsigned long handle = ack_handle[irq];
334 unsigned long addr;
335
336 intc_disable(irq);
337
338 /* read register and write zero only to the assocaited bit */
339
340 if (handle) {
341 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900342 switch (_INTC_FN(handle)) {
343 case REG_FN_MODIFY_BASE + 0: /* 8bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900344 __raw_readb(addr);
345 __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900346 break;
347 case REG_FN_MODIFY_BASE + 1: /* 16bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900348 __raw_readw(addr);
349 __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900350 break;
351 case REG_FN_MODIFY_BASE + 3: /* 32bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900352 __raw_readl(addr);
353 __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900354 break;
355 default:
356 BUG();
357 break;
358 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900359 }
360}
Magnus Dammd58876e2008-04-24 21:36:34 +0900361
Magnus Damm73505b42007-08-12 15:26:12 +0900362static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
363 unsigned int nr_hp,
364 unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900365{
Magnus Damm73505b42007-08-12 15:26:12 +0900366 int i;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900367
Magnus Damm3d37d942007-08-17 00:50:44 +0900368 /* this doesn't scale well, but...
369 *
370 * this function should only be used for cerain uncommon
371 * operations such as intc_set_priority() and intc_set_sense()
372 * and in those rare cases performance doesn't matter that much.
373 * keeping the memory footprint low is more important.
374 *
375 * one rather simple way to speed this up and still keep the
376 * memory footprint down is to make sure the array is sorted
377 * and then perform a bisect to lookup the irq.
378 */
379
Magnus Damm73505b42007-08-12 15:26:12 +0900380 for (i = 0; i < nr_hp; i++) {
381 if ((hp + i)->irq != irq)
382 continue;
383
384 return hp + i;
385 }
386
387 return NULL;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900388}
389
Magnus Damm73505b42007-08-12 15:26:12 +0900390int intc_set_priority(unsigned int irq, unsigned int prio)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900391{
Magnus Damm73505b42007-08-12 15:26:12 +0900392 struct intc_desc_int *d = get_intc_desc(irq);
393 struct intc_handle_int *ihp;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900394
Magnus Damm73505b42007-08-12 15:26:12 +0900395 if (!intc_prio_level[irq] || prio <= 1)
396 return -EINVAL;
397
398 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
399 if (ihp) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900400 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
Magnus Damm73505b42007-08-12 15:26:12 +0900401 return -EINVAL;
402
403 intc_prio_level[irq] = prio;
404
405 /*
406 * only set secondary masking method directly
407 * primary masking method is using intc_prio_level[irq]
408 * priority level will be set during next enable()
409 */
410
Magnus Damm3d37d942007-08-17 00:50:44 +0900411 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
Magnus Damm73505b42007-08-12 15:26:12 +0900412 _intc_enable(irq, ihp->handle);
413 }
414 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900415}
416
417#define VALID(x) (x | 0x80)
418
419static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
420 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
421 [IRQ_TYPE_EDGE_RISING] = VALID(1),
422 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
Magnus Damm720be992008-04-24 21:47:15 +0900423 /* SH7706, SH7707 and SH7709 do not support high level triggered */
424#if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
425 !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
426 !defined(CONFIG_CPU_SUBTYPE_SH7709)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900427 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
Magnus Damm720be992008-04-24 21:47:15 +0900428#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900429};
430
431static int intc_set_sense(unsigned int irq, unsigned int type)
432{
Magnus Damm73505b42007-08-12 15:26:12 +0900433 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900434 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
Magnus Damm73505b42007-08-12 15:26:12 +0900435 struct intc_handle_int *ihp;
436 unsigned long addr;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900437
Magnus Damm73505b42007-08-12 15:26:12 +0900438 if (!value)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900439 return -EINVAL;
440
Magnus Damm73505b42007-08-12 15:26:12 +0900441 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
442 if (ihp) {
Magnus Dammf18d5332007-09-21 18:16:42 +0900443 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900444 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900445 }
Magnus Damm73505b42007-08-12 15:26:12 +0900446 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900447}
448
Magnus Damm73505b42007-08-12 15:26:12 +0900449static unsigned int __init intc_get_reg(struct intc_desc_int *d,
450 unsigned long address)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900451{
Magnus Damm73505b42007-08-12 15:26:12 +0900452 unsigned int k;
453
454 for (k = 0; k < d->nr_reg; k++) {
455 if (d->reg[k] == address)
456 return k;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900457 }
458
459 BUG();
Magnus Damm73505b42007-08-12 15:26:12 +0900460 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900461}
462
Magnus Damm73505b42007-08-12 15:26:12 +0900463static intc_enum __init intc_grp_id(struct intc_desc *desc,
464 intc_enum enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900465{
Magnus Damm577cd752010-02-09 04:24:46 +0000466 struct intc_group *g = desc->hw.groups;
Magnus Damm680c4592007-07-20 12:09:29 +0900467 unsigned int i, j;
468
Magnus Damm577cd752010-02-09 04:24:46 +0000469 for (i = 0; g && enum_id && i < desc->hw.nr_groups; i++) {
470 g = desc->hw.groups + i;
Magnus Damm680c4592007-07-20 12:09:29 +0900471
472 for (j = 0; g->enum_ids[j]; j++) {
473 if (g->enum_ids[j] != enum_id)
474 continue;
475
476 return g->enum_id;
477 }
478 }
479
480 return 0;
481}
482
Magnus Dammd5190952010-02-09 04:29:22 +0000483static unsigned int __init _intc_mask_data(struct intc_desc *desc,
484 struct intc_desc_int *d,
485 intc_enum enum_id,
486 unsigned int *reg_idx,
487 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900488{
Magnus Damm577cd752010-02-09 04:24:46 +0000489 struct intc_mask_reg *mr = desc->hw.mask_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000490 unsigned int fn, mode;
Magnus Damm73505b42007-08-12 15:26:12 +0900491 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900492
Magnus Dammd5190952010-02-09 04:29:22 +0000493 while (mr && enum_id && *reg_idx < desc->hw.nr_mask_regs) {
494 mr = desc->hw.mask_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900495
Magnus Dammd5190952010-02-09 04:29:22 +0000496 for (; *fld_idx < ARRAY_SIZE(mr->enum_ids); (*fld_idx)++) {
497 if (mr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900498 continue;
499
Magnus Damm73505b42007-08-12 15:26:12 +0900500 if (mr->set_reg && mr->clr_reg) {
501 fn = REG_FN_WRITE_BASE;
502 mode = MODE_DUAL_REG;
503 reg_e = mr->clr_reg;
504 reg_d = mr->set_reg;
505 } else {
506 fn = REG_FN_MODIFY_BASE;
507 if (mr->set_reg) {
508 mode = MODE_ENABLE_REG;
509 reg_e = mr->set_reg;
510 reg_d = mr->set_reg;
511 } else {
512 mode = MODE_MASK_REG;
513 reg_e = mr->clr_reg;
514 reg_d = mr->clr_reg;
515 }
Magnus Damm51da6422007-08-03 14:25:32 +0900516 }
517
Magnus Damm73505b42007-08-12 15:26:12 +0900518 fn += (mr->reg_width >> 3) - 1;
519 return _INTC_MK(fn, mode,
520 intc_get_reg(d, reg_e),
521 intc_get_reg(d, reg_d),
522 1,
Magnus Dammd5190952010-02-09 04:29:22 +0000523 (mr->reg_width - 1) - *fld_idx);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900524 }
Magnus Dammd5190952010-02-09 04:29:22 +0000525
526 *fld_idx = 0;
527 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900528 }
529
Magnus Dammd5190952010-02-09 04:29:22 +0000530 return 0;
531}
532
533static unsigned int __init intc_mask_data(struct intc_desc *desc,
534 struct intc_desc_int *d,
535 intc_enum enum_id, int do_grps)
536{
537 unsigned int i = 0;
538 unsigned int j = 0;
539 unsigned int ret;
540
541 ret = _intc_mask_data(desc, d, enum_id, &i, &j);
542 if (ret)
543 return ret;
544
Magnus Damm680c4592007-07-20 12:09:29 +0900545 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900546 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900547
Magnus Damm02ab3f72007-07-18 17:25:09 +0900548 return 0;
549}
550
Magnus Dammd5190952010-02-09 04:29:22 +0000551static unsigned int __init _intc_prio_data(struct intc_desc *desc,
552 struct intc_desc_int *d,
553 intc_enum enum_id,
554 unsigned int *reg_idx,
555 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900556{
Magnus Damm577cd752010-02-09 04:24:46 +0000557 struct intc_prio_reg *pr = desc->hw.prio_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000558 unsigned int fn, n, mode, bit;
Magnus Damm73505b42007-08-12 15:26:12 +0900559 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900560
Magnus Dammd5190952010-02-09 04:29:22 +0000561 while (pr && enum_id && *reg_idx < desc->hw.nr_prio_regs) {
562 pr = desc->hw.prio_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900563
Magnus Dammd5190952010-02-09 04:29:22 +0000564 for (; *fld_idx < ARRAY_SIZE(pr->enum_ids); (*fld_idx)++) {
565 if (pr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900566 continue;
567
Magnus Damm73505b42007-08-12 15:26:12 +0900568 if (pr->set_reg && pr->clr_reg) {
569 fn = REG_FN_WRITE_BASE;
570 mode = MODE_PCLR_REG;
571 reg_e = pr->set_reg;
572 reg_d = pr->clr_reg;
573 } else {
574 fn = REG_FN_MODIFY_BASE;
575 mode = MODE_PRIO_REG;
576 if (!pr->set_reg)
577 BUG();
578 reg_e = pr->set_reg;
579 reg_d = pr->set_reg;
580 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900581
Magnus Damm73505b42007-08-12 15:26:12 +0900582 fn += (pr->reg_width >> 3) - 1;
Magnus Dammd5190952010-02-09 04:29:22 +0000583 n = *fld_idx + 1;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900584
Magnus Dammd5190952010-02-09 04:29:22 +0000585 BUG_ON(n * pr->field_width > pr->reg_width);
roel kluinb21a9102008-09-09 23:02:43 +0200586
Magnus Dammd5190952010-02-09 04:29:22 +0000587 bit = pr->reg_width - (n * pr->field_width);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900588
Magnus Damm73505b42007-08-12 15:26:12 +0900589 return _INTC_MK(fn, mode,
590 intc_get_reg(d, reg_e),
591 intc_get_reg(d, reg_d),
592 pr->field_width, bit);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900593 }
Magnus Dammd5190952010-02-09 04:29:22 +0000594
595 *fld_idx = 0;
596 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900597 }
598
Magnus Dammd5190952010-02-09 04:29:22 +0000599 return 0;
600}
601
602static unsigned int __init intc_prio_data(struct intc_desc *desc,
603 struct intc_desc_int *d,
604 intc_enum enum_id, int do_grps)
605{
606 unsigned int i = 0;
607 unsigned int j = 0;
608 unsigned int ret;
609
610 ret = _intc_prio_data(desc, d, enum_id, &i, &j);
611 if (ret)
612 return ret;
613
Magnus Damm680c4592007-07-20 12:09:29 +0900614 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900615 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900616
Magnus Damm02ab3f72007-07-18 17:25:09 +0900617 return 0;
618}
619
Magnus Dammd5190952010-02-09 04:29:22 +0000620static void __init intc_enable_disable_enum(struct intc_desc *desc,
621 struct intc_desc_int *d,
622 intc_enum enum_id, int enable)
623{
624 unsigned int i, j, data;
625
626 /* go through and enable/disable all mask bits */
627 i = j = 0;
628 do {
629 data = _intc_mask_data(desc, d, enum_id, &i, &j);
630 if (data)
631 intc_enable_disable(d, data, enable);
632 j++;
633 } while (data);
634
635 /* go through and enable/disable all priority fields */
636 i = j = 0;
637 do {
638 data = _intc_prio_data(desc, d, enum_id, &i, &j);
639 if (data)
640 intc_enable_disable(d, data, enable);
641
642 j++;
643 } while (data);
644}
645
Magnus Dammd58876e2008-04-24 21:36:34 +0900646static unsigned int __init intc_ack_data(struct intc_desc *desc,
647 struct intc_desc_int *d,
648 intc_enum enum_id)
649{
Magnus Damm577cd752010-02-09 04:24:46 +0000650 struct intc_mask_reg *mr = desc->hw.ack_regs;
Magnus Dammd58876e2008-04-24 21:36:34 +0900651 unsigned int i, j, fn, mode;
652 unsigned long reg_e, reg_d;
653
Magnus Damm577cd752010-02-09 04:24:46 +0000654 for (i = 0; mr && enum_id && i < desc->hw.nr_ack_regs; i++) {
655 mr = desc->hw.ack_regs + i;
Magnus Dammd58876e2008-04-24 21:36:34 +0900656
657 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
658 if (mr->enum_ids[j] != enum_id)
659 continue;
660
661 fn = REG_FN_MODIFY_BASE;
662 mode = MODE_ENABLE_REG;
663 reg_e = mr->set_reg;
664 reg_d = mr->set_reg;
665
666 fn += (mr->reg_width >> 3) - 1;
667 return _INTC_MK(fn, mode,
668 intc_get_reg(d, reg_e),
669 intc_get_reg(d, reg_d),
670 1,
671 (mr->reg_width - 1) - j);
672 }
673 }
674
675 return 0;
676}
Magnus Dammd58876e2008-04-24 21:36:34 +0900677
Magnus Damm73505b42007-08-12 15:26:12 +0900678static unsigned int __init intc_sense_data(struct intc_desc *desc,
679 struct intc_desc_int *d,
680 intc_enum enum_id)
681{
Magnus Damm577cd752010-02-09 04:24:46 +0000682 struct intc_sense_reg *sr = desc->hw.sense_regs;
Magnus Damm73505b42007-08-12 15:26:12 +0900683 unsigned int i, j, fn, bit;
684
Magnus Damm577cd752010-02-09 04:24:46 +0000685 for (i = 0; sr && enum_id && i < desc->hw.nr_sense_regs; i++) {
686 sr = desc->hw.sense_regs + i;
Magnus Damm73505b42007-08-12 15:26:12 +0900687
688 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
689 if (sr->enum_ids[j] != enum_id)
690 continue;
691
692 fn = REG_FN_MODIFY_BASE;
693 fn += (sr->reg_width >> 3) - 1;
Magnus Damm73505b42007-08-12 15:26:12 +0900694
roel kluinb21a9102008-09-09 23:02:43 +0200695 BUG_ON((j + 1) * sr->field_width > sr->reg_width);
696
697 bit = sr->reg_width - ((j + 1) * sr->field_width);
Magnus Damm73505b42007-08-12 15:26:12 +0900698
699 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
700 0, sr->field_width, bit);
701 }
702 }
703
704 return 0;
705}
706
707static void __init intc_register_irq(struct intc_desc *desc,
708 struct intc_desc_int *d,
709 intc_enum enum_id,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900710 unsigned int irq)
711{
Magnus Damm3d37d942007-08-17 00:50:44 +0900712 struct intc_handle_int *hp;
Magnus Damm680c4592007-07-20 12:09:29 +0900713 unsigned int data[2], primary;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900714
Paul Mundt1ce7b032009-11-02 10:30:26 +0900715 /*
716 * Register the IRQ position with the global IRQ map
717 */
718 set_bit(irq, intc_irq_map);
719
Magnus Damm680c4592007-07-20 12:09:29 +0900720 /* Prefer single interrupt source bitmap over other combinations:
721 * 1. bitmap, single interrupt source
722 * 2. priority, single interrupt source
723 * 3. bitmap, multiple interrupt sources (groups)
724 * 4. priority, multiple interrupt sources (groups)
725 */
726
Magnus Damm73505b42007-08-12 15:26:12 +0900727 data[0] = intc_mask_data(desc, d, enum_id, 0);
728 data[1] = intc_prio_data(desc, d, enum_id, 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900729
730 primary = 0;
731 if (!data[0] && data[1])
732 primary = 1;
733
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900734 if (!data[0] && !data[1])
Paul Mundtf0335992009-03-06 17:56:58 +0900735 pr_warning("intc: missing unique irq mask for "
736 "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900737
Magnus Damm73505b42007-08-12 15:26:12 +0900738 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
739 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
Magnus Damm680c4592007-07-20 12:09:29 +0900740
741 if (!data[primary])
742 primary ^= 1;
743
744 BUG_ON(!data[primary]); /* must have primary masking method */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900745
746 disable_irq_nosync(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900747 set_irq_chip_and_handler_name(irq, &d->chip,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900748 handle_level_irq, "level");
Magnus Damm680c4592007-07-20 12:09:29 +0900749 set_irq_chip_data(irq, (void *)data[primary]);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900750
Magnus Damm7f3edee2008-01-10 14:08:55 +0900751 /* set priority level
752 * - this needs to be at least 2 for 5-bit priorities on 7780
753 */
754 intc_prio_level[irq] = 2;
Magnus Damm73505b42007-08-12 15:26:12 +0900755
Magnus Damm680c4592007-07-20 12:09:29 +0900756 /* enable secondary masking method if present */
757 if (data[!primary])
Magnus Damm73505b42007-08-12 15:26:12 +0900758 _intc_enable(irq, data[!primary]);
759
760 /* add irq to d->prio list if priority is available */
761 if (data[1]) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900762 hp = d->prio + d->nr_prio;
763 hp->irq = irq;
764 hp->handle = data[1];
765
766 if (primary) {
767 /*
768 * only secondary priority should access registers, so
769 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
770 */
771
772 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
773 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
774 }
Magnus Damm73505b42007-08-12 15:26:12 +0900775 d->nr_prio++;
776 }
777
778 /* add irq to d->sense list if sense is available */
779 data[0] = intc_sense_data(desc, d, enum_id);
780 if (data[0]) {
781 (d->sense + d->nr_sense)->irq = irq;
782 (d->sense + d->nr_sense)->handle = data[0];
783 d->nr_sense++;
784 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900785
786 /* irq should be disabled by default */
Magnus Damm73505b42007-08-12 15:26:12 +0900787 d->chip.mask(irq);
Magnus Dammd58876e2008-04-24 21:36:34 +0900788
Magnus Damm577cd752010-02-09 04:24:46 +0000789 if (desc->hw.ack_regs)
Magnus Dammd58876e2008-04-24 21:36:34 +0900790 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
Magnus Damm65a5b282010-02-05 11:15:25 +0000791
792#ifdef CONFIG_ARM
793 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
794#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900795}
796
Magnus Dammf18d5332007-09-21 18:16:42 +0900797static unsigned int __init save_reg(struct intc_desc_int *d,
798 unsigned int cnt,
799 unsigned long value,
800 unsigned int smp)
801{
802 if (value) {
803 d->reg[cnt] = value;
804#ifdef CONFIG_SMP
805 d->smp[cnt] = smp;
806#endif
807 return 1;
808 }
809
810 return 0;
811}
812
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900813static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900814{
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900815 generic_handle_irq((unsigned int)get_irq_data(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900816}
Magnus Dammf18d5332007-09-21 18:16:42 +0900817
Magnus Damm02ab3f72007-07-18 17:25:09 +0900818void __init register_intc_controller(struct intc_desc *desc)
819{
Paul Mundt54ff3282009-06-11 10:33:09 +0300820 unsigned int i, k, smp;
Magnus Damm577cd752010-02-09 04:24:46 +0000821 struct intc_hw_desc *hw = &desc->hw;
Magnus Damm73505b42007-08-12 15:26:12 +0900822 struct intc_desc_int *d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900823
Paul Mundt11b6aa92009-06-12 01:34:12 +0300824 d = kzalloc(sizeof(*d), GFP_NOWAIT);
Magnus Damm73505b42007-08-12 15:26:12 +0900825
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000826 INIT_LIST_HEAD(&d->list);
827 list_add(&d->list, &intc_list);
828
Magnus Damm577cd752010-02-09 04:24:46 +0000829 d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
830 d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
831 d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
832 d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
Paul Mundt9b798d52009-10-27 11:36:43 +0900833
Paul Mundt11b6aa92009-06-12 01:34:12 +0300834 d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
Magnus Dammf18d5332007-09-21 18:16:42 +0900835#ifdef CONFIG_SMP
Paul Mundt11b6aa92009-06-12 01:34:12 +0300836 d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
Magnus Dammf18d5332007-09-21 18:16:42 +0900837#endif
Magnus Damm73505b42007-08-12 15:26:12 +0900838 k = 0;
839
Magnus Damm577cd752010-02-09 04:24:46 +0000840 if (hw->mask_regs) {
841 for (i = 0; i < hw->nr_mask_regs; i++) {
842 smp = IS_SMP(hw->mask_regs[i]);
843 k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
844 k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900845 }
846 }
847
Magnus Damm577cd752010-02-09 04:24:46 +0000848 if (hw->prio_regs) {
849 d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
850 GFP_NOWAIT);
Magnus Damm73505b42007-08-12 15:26:12 +0900851
Magnus Damm577cd752010-02-09 04:24:46 +0000852 for (i = 0; i < hw->nr_prio_regs; i++) {
853 smp = IS_SMP(hw->prio_regs[i]);
854 k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
855 k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900856 }
857 }
858
Magnus Damm577cd752010-02-09 04:24:46 +0000859 if (hw->sense_regs) {
860 d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
861 GFP_NOWAIT);
Magnus Damm73505b42007-08-12 15:26:12 +0900862
Magnus Damm577cd752010-02-09 04:24:46 +0000863 for (i = 0; i < hw->nr_sense_regs; i++)
864 k += save_reg(d, k, hw->sense_regs[i].reg, 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900865 }
866
Magnus Damm73505b42007-08-12 15:26:12 +0900867 d->chip.name = desc->name;
868 d->chip.mask = intc_disable;
869 d->chip.unmask = intc_enable;
870 d->chip.mask_ack = intc_disable;
Magnus Dammf7dd2542009-04-01 14:20:58 +0000871 d->chip.enable = intc_enable;
872 d->chip.disable = intc_disable;
873 d->chip.shutdown = intc_disable;
Magnus Damm73505b42007-08-12 15:26:12 +0900874 d->chip.set_type = intc_set_sense;
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000875 d->chip.set_wake = intc_set_wake;
Paul Mundta8941da2010-03-08 13:33:17 +0900876#ifdef CONFIG_SMP
877 d->chip.set_affinity = intc_set_affinity;
878#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900879
Magnus Damm577cd752010-02-09 04:24:46 +0000880 if (hw->ack_regs) {
881 for (i = 0; i < hw->nr_ack_regs; i++)
882 k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
Magnus Dammd58876e2008-04-24 21:36:34 +0900883
884 d->chip.mask_ack = intc_mask_ack;
885 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900886
Magnus Dammd85429a2010-02-15 11:40:25 +0000887 /* disable bits matching force_disable before registering irqs */
888 if (desc->force_disable)
889 intc_enable_disable_enum(desc, d, desc->force_disable, 0);
Magnus Dammd5190952010-02-09 04:29:22 +0000890
891 /* disable bits matching force_enable before registering irqs */
892 if (desc->force_enable)
893 intc_enable_disable_enum(desc, d, desc->force_enable, 0);
894
Magnus Dammd58876e2008-04-24 21:36:34 +0900895 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
896
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900897 /* register the vectors one by one */
Magnus Damm577cd752010-02-09 04:24:46 +0000898 for (i = 0; i < hw->nr_vectors; i++) {
899 struct intc_vect *vect = hw->vectors + i;
Paul Mundt05ff3002009-05-22 01:28:33 +0900900 unsigned int irq = evt2irq(vect->vect);
901 struct irq_desc *irq_desc;
Paul Mundt54ff3282009-06-11 10:33:09 +0300902
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900903 if (!vect->enum_id)
904 continue;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900905
Paul Mundt54ff3282009-06-11 10:33:09 +0300906 irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
Paul Mundt05ff3002009-05-22 01:28:33 +0900907 if (unlikely(!irq_desc)) {
Paul Mundt1279b7f2009-08-31 15:15:33 +0900908 pr_info("can't get irq_desc for %d\n", irq);
Paul Mundt05ff3002009-05-22 01:28:33 +0900909 continue;
910 }
911
912 intc_register_irq(desc, d, vect->enum_id, irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900913
Magnus Damm577cd752010-02-09 04:24:46 +0000914 for (k = i + 1; k < hw->nr_vectors; k++) {
915 struct intc_vect *vect2 = hw->vectors + k;
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900916 unsigned int irq2 = evt2irq(vect2->vect);
917
918 if (vect->enum_id != vect2->enum_id)
919 continue;
920
Paul Mundt1279b7f2009-08-31 15:15:33 +0900921 /*
922 * In the case of multi-evt handling and sparse
923 * IRQ support, each vector still needs to have
924 * its own backing irq_desc.
925 */
926 irq_desc = irq_to_desc_alloc_node(irq2, numa_node_id());
927 if (unlikely(!irq_desc)) {
928 pr_info("can't get irq_desc for %d\n", irq2);
929 continue;
930 }
931
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900932 vect2->enum_id = 0;
933
934 /* redirect this interrupts to the first one */
Paul Mundt4d2185d2010-02-17 12:37:42 +0900935 set_irq_chip(irq2, &dummy_irq_chip);
Magnus Damme6f07752010-02-09 07:17:20 +0000936 set_irq_chained_handler(irq2, intc_redirect_irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900937 set_irq_data(irq2, (void *)irq);
938 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900939 }
Magnus Dammd5190952010-02-09 04:29:22 +0000940
941 /* enable bits matching force_enable after registering irqs */
942 if (desc->force_enable)
943 intc_enable_disable_enum(desc, d, desc->force_enable, 1);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900944}
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000945
946static int intc_suspend(struct sys_device *dev, pm_message_t state)
947{
948 struct intc_desc_int *d;
949 struct irq_desc *desc;
950 int irq;
951
952 /* get intc controller associated with this sysdev */
953 d = container_of(dev, struct intc_desc_int, sysdev);
954
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000955 switch (state.event) {
956 case PM_EVENT_ON:
957 if (d->state.event != PM_EVENT_FREEZE)
958 break;
959 for_each_irq_desc(irq, desc) {
Francesco VIRLINZI87a705d2009-12-04 08:57:58 +0000960 if (desc->handle_irq == intc_redirect_irq)
Paul Mundt0a753d52009-12-09 14:36:16 +0900961 continue;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000962 if (desc->chip != &d->chip)
963 continue;
964 if (desc->status & IRQ_DISABLED)
965 intc_disable(irq);
966 else
967 intc_enable(irq);
968 }
969 break;
970 case PM_EVENT_FREEZE:
971 /* nothing has to be done */
972 break;
973 case PM_EVENT_SUSPEND:
974 /* enable wakeup irqs belonging to this intc controller */
975 for_each_irq_desc(irq, desc) {
976 if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
977 intc_enable(irq);
978 }
979 break;
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000980 }
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000981 d->state = state;
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000982
983 return 0;
984}
985
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000986static int intc_resume(struct sys_device *dev)
987{
988 return intc_suspend(dev, PMSG_ON);
989}
990
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000991static struct sysdev_class intc_sysdev_class = {
992 .name = "intc",
993 .suspend = intc_suspend,
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000994 .resume = intc_resume,
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000995};
996
997/* register this intc as sysdev to allow suspend/resume */
998static int __init register_intc_sysdevs(void)
999{
1000 struct intc_desc_int *d;
1001 int error;
1002 int id = 0;
1003
1004 error = sysdev_class_register(&intc_sysdev_class);
1005 if (!error) {
1006 list_for_each_entry(d, &intc_list, list) {
1007 d->sysdev.id = id;
1008 d->sysdev.cls = &intc_sysdev_class;
1009 error = sysdev_register(&d->sysdev);
1010 if (error)
1011 break;
1012 id++;
1013 }
1014 }
1015
1016 if (error)
1017 pr_warning("intc: sysdev registration error\n");
1018
1019 return error;
1020}
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001021device_initcall(register_intc_sysdevs);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001022
1023/*
1024 * Dynamic IRQ allocation and deallocation
1025 */
Paul Mundte9867c52010-02-02 17:35:13 +09001026unsigned int create_irq_nr(unsigned int irq_want, int node)
Paul Mundt1ce7b032009-11-02 10:30:26 +09001027{
1028 unsigned int irq = 0, new;
1029 unsigned long flags;
1030 struct irq_desc *desc;
1031
1032 spin_lock_irqsave(&vector_lock, flags);
1033
1034 /*
Paul Mundte9867c52010-02-02 17:35:13 +09001035 * First try the wanted IRQ
Paul Mundt1ce7b032009-11-02 10:30:26 +09001036 */
Paul Mundte9867c52010-02-02 17:35:13 +09001037 if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
1038 new = irq_want;
1039 } else {
1040 /* .. then fall back to scanning. */
Paul Mundt1ce7b032009-11-02 10:30:26 +09001041 new = find_first_zero_bit(intc_irq_map, nr_irqs);
1042 if (unlikely(new == nr_irqs))
1043 goto out_unlock;
1044
Paul Mundt1ce7b032009-11-02 10:30:26 +09001045 __set_bit(new, intc_irq_map);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001046 }
1047
Paul Mundte9867c52010-02-02 17:35:13 +09001048 desc = irq_to_desc_alloc_node(new, node);
1049 if (unlikely(!desc)) {
1050 pr_info("can't get irq_desc for %d\n", new);
1051 goto out_unlock;
1052 }
1053
1054 desc = move_irq_desc(desc, node);
1055 irq = new;
1056
Paul Mundt1ce7b032009-11-02 10:30:26 +09001057out_unlock:
1058 spin_unlock_irqrestore(&vector_lock, flags);
1059
Magnus Damm65a5b282010-02-05 11:15:25 +00001060 if (irq > 0) {
Paul Mundt1ce7b032009-11-02 10:30:26 +09001061 dynamic_irq_init(irq);
Magnus Damm65a5b282010-02-05 11:15:25 +00001062#ifdef CONFIG_ARM
1063 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
1064#endif
1065 }
Paul Mundt1ce7b032009-11-02 10:30:26 +09001066
1067 return irq;
1068}
1069
1070int create_irq(void)
1071{
1072 int nid = cpu_to_node(smp_processor_id());
1073 int irq;
1074
Paul Mundte9867c52010-02-02 17:35:13 +09001075 irq = create_irq_nr(NR_IRQS_LEGACY, nid);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001076 if (irq == 0)
1077 irq = -1;
1078
1079 return irq;
1080}
1081
1082void destroy_irq(unsigned int irq)
1083{
1084 unsigned long flags;
1085
1086 dynamic_irq_cleanup(irq);
1087
1088 spin_lock_irqsave(&vector_lock, flags);
1089 __clear_bit(irq, intc_irq_map);
1090 spin_unlock_irqrestore(&vector_lock, flags);
1091}
Paul Mundt45b9dea2009-11-02 15:43:20 +09001092
1093int reserve_irq_vector(unsigned int irq)
1094{
1095 unsigned long flags;
1096 int ret = 0;
1097
1098 spin_lock_irqsave(&vector_lock, flags);
1099 if (test_and_set_bit(irq, intc_irq_map))
1100 ret = -EBUSY;
1101 spin_unlock_irqrestore(&vector_lock, flags);
1102
1103 return ret;
1104}
1105
1106void reserve_irq_legacy(void)
1107{
1108 unsigned long flags;
1109 int i, j;
1110
1111 spin_lock_irqsave(&vector_lock, flags);
1112 j = find_first_bit(intc_irq_map, nr_irqs);
1113 for (i = 0; i < j; i++)
1114 __set_bit(i, intc_irq_map);
1115 spin_unlock_irqrestore(&vector_lock, flags);
1116}