blob: 77d10acf1884942e5682892442a55272f38f1843 [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];
Magnus Damm73505b42007-08-12 15:26:12 +0900101
102static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900103{
104 struct irq_chip *chip = get_irq_chip(irq);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900105 return container_of(chip, struct intc_desc_int, chip);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900106}
107
108static inline unsigned int set_field(unsigned int value,
109 unsigned int field_value,
Magnus Damm73505b42007-08-12 15:26:12 +0900110 unsigned int handle)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900111{
Magnus Damm73505b42007-08-12 15:26:12 +0900112 unsigned int width = _INTC_WIDTH(handle);
113 unsigned int shift = _INTC_SHIFT(handle);
114
Magnus Damm02ab3f72007-07-18 17:25:09 +0900115 value &= ~(((1 << width) - 1) << shift);
116 value |= field_value << shift;
117 return value;
118}
119
Magnus Damm73505b42007-08-12 15:26:12 +0900120static void write_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900121{
Paul Mundt62429e02008-10-01 15:19:10 +0900122 __raw_writeb(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900123 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900124}
125
Magnus Damm73505b42007-08-12 15:26:12 +0900126static void write_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900127{
Paul Mundt62429e02008-10-01 15:19:10 +0900128 __raw_writew(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900129 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900130}
131
Magnus Damm73505b42007-08-12 15:26:12 +0900132static void write_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900133{
Paul Mundt62429e02008-10-01 15:19:10 +0900134 __raw_writel(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900135 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900136}
137
Magnus Damm73505b42007-08-12 15:26:12 +0900138static void modify_8(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_writeb(set_field(__raw_readb(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900143 (void)__raw_readb(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_16(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_writew(set_field(__raw_readw(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900152 (void)__raw_readw(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 +0900156static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900157{
Magnus Damm4370fe12008-04-24 21:53:07 +0900158 unsigned long flags;
159 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900160 __raw_writel(set_field(__raw_readl(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900161 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900162 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900163}
164
Magnus Damm73505b42007-08-12 15:26:12 +0900165enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
Magnus Damm02ab3f72007-07-18 17:25:09 +0900166
Magnus Damm73505b42007-08-12 15:26:12 +0900167static void (*intc_reg_fns[])(unsigned long addr,
168 unsigned long h,
169 unsigned long data) = {
170 [REG_FN_WRITE_BASE + 0] = write_8,
171 [REG_FN_WRITE_BASE + 1] = write_16,
172 [REG_FN_WRITE_BASE + 3] = write_32,
173 [REG_FN_MODIFY_BASE + 0] = modify_8,
174 [REG_FN_MODIFY_BASE + 1] = modify_16,
175 [REG_FN_MODIFY_BASE + 3] = modify_32,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900176};
177
Magnus Damm73505b42007-08-12 15:26:12 +0900178enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
179 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
180 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
181 MODE_PRIO_REG, /* Priority value written to enable interrupt */
182 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
183};
184
185static void intc_mode_field(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, ((1 << _INTC_WIDTH(handle)) - 1));
193}
194
195static void intc_mode_zero(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, 0);
203}
204
205static void intc_mode_prio(unsigned long addr,
206 unsigned long handle,
207 void (*fn)(unsigned long,
208 unsigned long,
209 unsigned long),
210 unsigned int irq)
211{
212 fn(addr, handle, intc_prio_level[irq]);
213}
214
215static void (*intc_enable_fns[])(unsigned long addr,
216 unsigned long handle,
217 void (*fn)(unsigned long,
218 unsigned long,
219 unsigned long),
220 unsigned int irq) = {
221 [MODE_ENABLE_REG] = intc_mode_field,
222 [MODE_MASK_REG] = intc_mode_zero,
223 [MODE_DUAL_REG] = intc_mode_field,
224 [MODE_PRIO_REG] = intc_mode_prio,
225 [MODE_PCLR_REG] = intc_mode_prio,
226};
227
228static void (*intc_disable_fns[])(unsigned long addr,
229 unsigned long handle,
230 void (*fn)(unsigned long,
231 unsigned long,
232 unsigned long),
233 unsigned int irq) = {
234 [MODE_ENABLE_REG] = intc_mode_zero,
235 [MODE_MASK_REG] = intc_mode_field,
236 [MODE_DUAL_REG] = intc_mode_field,
237 [MODE_PRIO_REG] = intc_mode_zero,
238 [MODE_PCLR_REG] = intc_mode_field,
239};
240
241static inline void _intc_enable(unsigned int irq, unsigned long handle)
242{
243 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900244 unsigned long addr;
245 unsigned int cpu;
Magnus Damm73505b42007-08-12 15:26:12 +0900246
Magnus Dammf18d5332007-09-21 18:16:42 +0900247 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
Paul Mundta8941da2010-03-08 13:33:17 +0900248#ifdef CONFIG_SMP
249 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
250 continue;
251#endif
Magnus Dammf18d5332007-09-21 18:16:42 +0900252 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
253 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
254 [_INTC_FN(handle)], irq);
255 }
Magnus Damm73505b42007-08-12 15:26:12 +0900256}
257
Magnus Damm02ab3f72007-07-18 17:25:09 +0900258static void intc_enable(unsigned int irq)
259{
Magnus Damm73505b42007-08-12 15:26:12 +0900260 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
Magnus Damm02ab3f72007-07-18 17:25:09 +0900261}
262
263static void intc_disable(unsigned int irq)
264{
Magnus Dammf18d5332007-09-21 18:16:42 +0900265 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900266 unsigned long handle = (unsigned long) get_irq_chip_data(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900267 unsigned long addr;
268 unsigned int cpu;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900269
Magnus Dammf18d5332007-09-21 18:16:42 +0900270 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
Paul Mundta8941da2010-03-08 13:33:17 +0900271#ifdef CONFIG_SMP
272 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
273 continue;
274#endif
Magnus Dammf18d5332007-09-21 18:16:42 +0900275 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
276 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
277 [_INTC_FN(handle)], irq);
278 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900279}
280
Magnus Dammd5190952010-02-09 04:29:22 +0000281static void (*intc_enable_noprio_fns[])(unsigned long addr,
282 unsigned long handle,
283 void (*fn)(unsigned long,
284 unsigned long,
285 unsigned long),
286 unsigned int irq) = {
287 [MODE_ENABLE_REG] = intc_mode_field,
288 [MODE_MASK_REG] = intc_mode_zero,
289 [MODE_DUAL_REG] = intc_mode_field,
290 [MODE_PRIO_REG] = intc_mode_field,
291 [MODE_PCLR_REG] = intc_mode_field,
292};
293
294static void intc_enable_disable(struct intc_desc_int *d,
295 unsigned long handle, int do_enable)
296{
297 unsigned long addr;
298 unsigned int cpu;
299 void (*fn)(unsigned long, unsigned long,
300 void (*)(unsigned long, unsigned long, unsigned long),
301 unsigned int);
302
303 if (do_enable) {
304 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
305 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
306 fn = intc_enable_noprio_fns[_INTC_MODE(handle)];
307 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
308 }
309 } else {
310 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
311 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
312 fn = intc_disable_fns[_INTC_MODE(handle)];
313 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
314 }
315 }
316}
317
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000318static int intc_set_wake(unsigned int irq, unsigned int on)
319{
320 return 0; /* allow wakeup, but setup hardware in intc_suspend() */
321}
322
Paul Mundta8941da2010-03-08 13:33:17 +0900323#ifdef CONFIG_SMP
324/*
325 * This is held with the irq desc lock held, so we don't require any
326 * additional locking here at the intc desc level. The affinity mask is
327 * later tested in the enable/disable paths.
328 */
329static int intc_set_affinity(unsigned int irq, const struct cpumask *cpumask)
330{
331 if (!cpumask_intersects(cpumask, cpu_online_mask))
332 return -1;
333
334 cpumask_copy(irq_to_desc(irq)->affinity, cpumask);
335
336 return 0;
337}
338#endif
339
Magnus Dammd58876e2008-04-24 21:36:34 +0900340static void intc_mask_ack(unsigned int irq)
341{
342 struct intc_desc_int *d = get_intc_desc(irq);
343 unsigned long handle = ack_handle[irq];
344 unsigned long addr;
345
346 intc_disable(irq);
347
348 /* read register and write zero only to the assocaited bit */
349
350 if (handle) {
351 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900352 switch (_INTC_FN(handle)) {
353 case REG_FN_MODIFY_BASE + 0: /* 8bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900354 __raw_readb(addr);
355 __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900356 break;
357 case REG_FN_MODIFY_BASE + 1: /* 16bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900358 __raw_readw(addr);
359 __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900360 break;
361 case REG_FN_MODIFY_BASE + 3: /* 32bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900362 __raw_readl(addr);
363 __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900364 break;
365 default:
366 BUG();
367 break;
368 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900369 }
370}
Magnus Dammd58876e2008-04-24 21:36:34 +0900371
Magnus Damm73505b42007-08-12 15:26:12 +0900372static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
373 unsigned int nr_hp,
374 unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900375{
Magnus Damm73505b42007-08-12 15:26:12 +0900376 int i;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900377
Magnus Damm3d37d942007-08-17 00:50:44 +0900378 /* this doesn't scale well, but...
379 *
380 * this function should only be used for cerain uncommon
381 * operations such as intc_set_priority() and intc_set_sense()
382 * and in those rare cases performance doesn't matter that much.
383 * keeping the memory footprint low is more important.
384 *
385 * one rather simple way to speed this up and still keep the
386 * memory footprint down is to make sure the array is sorted
387 * and then perform a bisect to lookup the irq.
388 */
389
Magnus Damm73505b42007-08-12 15:26:12 +0900390 for (i = 0; i < nr_hp; i++) {
391 if ((hp + i)->irq != irq)
392 continue;
393
394 return hp + i;
395 }
396
397 return NULL;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900398}
399
Magnus Damm73505b42007-08-12 15:26:12 +0900400int intc_set_priority(unsigned int irq, unsigned int prio)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900401{
Magnus Damm73505b42007-08-12 15:26:12 +0900402 struct intc_desc_int *d = get_intc_desc(irq);
403 struct intc_handle_int *ihp;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900404
Magnus Damm73505b42007-08-12 15:26:12 +0900405 if (!intc_prio_level[irq] || prio <= 1)
406 return -EINVAL;
407
408 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
409 if (ihp) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900410 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
Magnus Damm73505b42007-08-12 15:26:12 +0900411 return -EINVAL;
412
413 intc_prio_level[irq] = prio;
414
415 /*
416 * only set secondary masking method directly
417 * primary masking method is using intc_prio_level[irq]
418 * priority level will be set during next enable()
419 */
420
Magnus Damm3d37d942007-08-17 00:50:44 +0900421 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
Magnus Damm73505b42007-08-12 15:26:12 +0900422 _intc_enable(irq, ihp->handle);
423 }
424 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900425}
426
427#define VALID(x) (x | 0x80)
428
429static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
430 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
431 [IRQ_TYPE_EDGE_RISING] = VALID(1),
432 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
Magnus Damm720be992008-04-24 21:47:15 +0900433 /* SH7706, SH7707 and SH7709 do not support high level triggered */
434#if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
435 !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
436 !defined(CONFIG_CPU_SUBTYPE_SH7709)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900437 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
Magnus Damm720be992008-04-24 21:47:15 +0900438#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900439};
440
441static int intc_set_sense(unsigned int irq, unsigned int type)
442{
Magnus Damm73505b42007-08-12 15:26:12 +0900443 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900444 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
Magnus Damm73505b42007-08-12 15:26:12 +0900445 struct intc_handle_int *ihp;
446 unsigned long addr;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900447
Magnus Damm73505b42007-08-12 15:26:12 +0900448 if (!value)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900449 return -EINVAL;
450
Magnus Damm73505b42007-08-12 15:26:12 +0900451 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
452 if (ihp) {
Magnus Dammf18d5332007-09-21 18:16:42 +0900453 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900454 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900455 }
Magnus Damm73505b42007-08-12 15:26:12 +0900456 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900457}
458
Magnus Dammdec710b2010-03-19 16:48:01 +0900459static unsigned long intc_phys_to_virt(struct intc_desc_int *d,
460 unsigned long address)
461{
462 struct intc_window *window;
463 int k;
464
465 /* scan through physical windows and convert address */
466 for (k = 0; k < d->nr_windows; k++) {
467 window = d->window + k;
468
469 if (address < window->phys)
470 continue;
471
472 if (address >= (window->phys + window->size))
473 continue;
474
475 address -= window->phys;
476 address += (unsigned long)window->virt;
477
478 return address;
479 }
480
481 /* no windows defined, register must be 1:1 mapped virt:phys */
482 return address;
483}
484
Magnus Damm73505b42007-08-12 15:26:12 +0900485static unsigned int __init intc_get_reg(struct intc_desc_int *d,
Magnus Dammdec710b2010-03-19 16:48:01 +0900486 unsigned long address)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900487{
Magnus Damm73505b42007-08-12 15:26:12 +0900488 unsigned int k;
489
Magnus Dammdec710b2010-03-19 16:48:01 +0900490 address = intc_phys_to_virt(d, address);
491
Magnus Damm73505b42007-08-12 15:26:12 +0900492 for (k = 0; k < d->nr_reg; k++) {
493 if (d->reg[k] == address)
494 return k;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900495 }
496
497 BUG();
Magnus Damm73505b42007-08-12 15:26:12 +0900498 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900499}
500
Magnus Damm73505b42007-08-12 15:26:12 +0900501static intc_enum __init intc_grp_id(struct intc_desc *desc,
502 intc_enum enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900503{
Magnus Damm577cd752010-02-09 04:24:46 +0000504 struct intc_group *g = desc->hw.groups;
Magnus Damm680c4592007-07-20 12:09:29 +0900505 unsigned int i, j;
506
Magnus Damm577cd752010-02-09 04:24:46 +0000507 for (i = 0; g && enum_id && i < desc->hw.nr_groups; i++) {
508 g = desc->hw.groups + i;
Magnus Damm680c4592007-07-20 12:09:29 +0900509
510 for (j = 0; g->enum_ids[j]; j++) {
511 if (g->enum_ids[j] != enum_id)
512 continue;
513
514 return g->enum_id;
515 }
516 }
517
518 return 0;
519}
520
Magnus Dammd5190952010-02-09 04:29:22 +0000521static unsigned int __init _intc_mask_data(struct intc_desc *desc,
522 struct intc_desc_int *d,
523 intc_enum enum_id,
524 unsigned int *reg_idx,
525 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900526{
Magnus Damm577cd752010-02-09 04:24:46 +0000527 struct intc_mask_reg *mr = desc->hw.mask_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000528 unsigned int fn, mode;
Magnus Damm73505b42007-08-12 15:26:12 +0900529 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900530
Magnus Dammd5190952010-02-09 04:29:22 +0000531 while (mr && enum_id && *reg_idx < desc->hw.nr_mask_regs) {
532 mr = desc->hw.mask_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900533
Magnus Dammd5190952010-02-09 04:29:22 +0000534 for (; *fld_idx < ARRAY_SIZE(mr->enum_ids); (*fld_idx)++) {
535 if (mr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900536 continue;
537
Magnus Damm73505b42007-08-12 15:26:12 +0900538 if (mr->set_reg && mr->clr_reg) {
539 fn = REG_FN_WRITE_BASE;
540 mode = MODE_DUAL_REG;
541 reg_e = mr->clr_reg;
542 reg_d = mr->set_reg;
543 } else {
544 fn = REG_FN_MODIFY_BASE;
545 if (mr->set_reg) {
546 mode = MODE_ENABLE_REG;
547 reg_e = mr->set_reg;
548 reg_d = mr->set_reg;
549 } else {
550 mode = MODE_MASK_REG;
551 reg_e = mr->clr_reg;
552 reg_d = mr->clr_reg;
553 }
Magnus Damm51da6422007-08-03 14:25:32 +0900554 }
555
Magnus Damm73505b42007-08-12 15:26:12 +0900556 fn += (mr->reg_width >> 3) - 1;
557 return _INTC_MK(fn, mode,
558 intc_get_reg(d, reg_e),
559 intc_get_reg(d, reg_d),
560 1,
Magnus Dammd5190952010-02-09 04:29:22 +0000561 (mr->reg_width - 1) - *fld_idx);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900562 }
Magnus Dammd5190952010-02-09 04:29:22 +0000563
564 *fld_idx = 0;
565 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900566 }
567
Magnus Dammd5190952010-02-09 04:29:22 +0000568 return 0;
569}
570
571static unsigned int __init intc_mask_data(struct intc_desc *desc,
572 struct intc_desc_int *d,
573 intc_enum enum_id, int do_grps)
574{
575 unsigned int i = 0;
576 unsigned int j = 0;
577 unsigned int ret;
578
579 ret = _intc_mask_data(desc, d, enum_id, &i, &j);
580 if (ret)
581 return ret;
582
Magnus Damm680c4592007-07-20 12:09:29 +0900583 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900584 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900585
Magnus Damm02ab3f72007-07-18 17:25:09 +0900586 return 0;
587}
588
Magnus Dammd5190952010-02-09 04:29:22 +0000589static unsigned int __init _intc_prio_data(struct intc_desc *desc,
590 struct intc_desc_int *d,
591 intc_enum enum_id,
592 unsigned int *reg_idx,
593 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900594{
Magnus Damm577cd752010-02-09 04:24:46 +0000595 struct intc_prio_reg *pr = desc->hw.prio_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000596 unsigned int fn, n, mode, bit;
Magnus Damm73505b42007-08-12 15:26:12 +0900597 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900598
Magnus Dammd5190952010-02-09 04:29:22 +0000599 while (pr && enum_id && *reg_idx < desc->hw.nr_prio_regs) {
600 pr = desc->hw.prio_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900601
Magnus Dammd5190952010-02-09 04:29:22 +0000602 for (; *fld_idx < ARRAY_SIZE(pr->enum_ids); (*fld_idx)++) {
603 if (pr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900604 continue;
605
Magnus Damm73505b42007-08-12 15:26:12 +0900606 if (pr->set_reg && pr->clr_reg) {
607 fn = REG_FN_WRITE_BASE;
608 mode = MODE_PCLR_REG;
609 reg_e = pr->set_reg;
610 reg_d = pr->clr_reg;
611 } else {
612 fn = REG_FN_MODIFY_BASE;
613 mode = MODE_PRIO_REG;
614 if (!pr->set_reg)
615 BUG();
616 reg_e = pr->set_reg;
617 reg_d = pr->set_reg;
618 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900619
Magnus Damm73505b42007-08-12 15:26:12 +0900620 fn += (pr->reg_width >> 3) - 1;
Magnus Dammd5190952010-02-09 04:29:22 +0000621 n = *fld_idx + 1;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900622
Magnus Dammd5190952010-02-09 04:29:22 +0000623 BUG_ON(n * pr->field_width > pr->reg_width);
roel kluinb21a9102008-09-09 23:02:43 +0200624
Magnus Dammd5190952010-02-09 04:29:22 +0000625 bit = pr->reg_width - (n * pr->field_width);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900626
Magnus Damm73505b42007-08-12 15:26:12 +0900627 return _INTC_MK(fn, mode,
628 intc_get_reg(d, reg_e),
629 intc_get_reg(d, reg_d),
630 pr->field_width, bit);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900631 }
Magnus Dammd5190952010-02-09 04:29:22 +0000632
633 *fld_idx = 0;
634 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900635 }
636
Magnus Dammd5190952010-02-09 04:29:22 +0000637 return 0;
638}
639
640static unsigned int __init intc_prio_data(struct intc_desc *desc,
641 struct intc_desc_int *d,
642 intc_enum enum_id, int do_grps)
643{
644 unsigned int i = 0;
645 unsigned int j = 0;
646 unsigned int ret;
647
648 ret = _intc_prio_data(desc, d, enum_id, &i, &j);
649 if (ret)
650 return ret;
651
Magnus Damm680c4592007-07-20 12:09:29 +0900652 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900653 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900654
Magnus Damm02ab3f72007-07-18 17:25:09 +0900655 return 0;
656}
657
Magnus Dammd5190952010-02-09 04:29:22 +0000658static void __init intc_enable_disable_enum(struct intc_desc *desc,
659 struct intc_desc_int *d,
660 intc_enum enum_id, int enable)
661{
662 unsigned int i, j, data;
663
664 /* go through and enable/disable all mask bits */
665 i = j = 0;
666 do {
667 data = _intc_mask_data(desc, d, enum_id, &i, &j);
668 if (data)
669 intc_enable_disable(d, data, enable);
670 j++;
671 } while (data);
672
673 /* go through and enable/disable all priority fields */
674 i = j = 0;
675 do {
676 data = _intc_prio_data(desc, d, enum_id, &i, &j);
677 if (data)
678 intc_enable_disable(d, data, enable);
679
680 j++;
681 } while (data);
682}
683
Magnus Dammd58876e2008-04-24 21:36:34 +0900684static unsigned int __init intc_ack_data(struct intc_desc *desc,
685 struct intc_desc_int *d,
686 intc_enum enum_id)
687{
Magnus Damm577cd752010-02-09 04:24:46 +0000688 struct intc_mask_reg *mr = desc->hw.ack_regs;
Magnus Dammd58876e2008-04-24 21:36:34 +0900689 unsigned int i, j, fn, mode;
690 unsigned long reg_e, reg_d;
691
Magnus Damm577cd752010-02-09 04:24:46 +0000692 for (i = 0; mr && enum_id && i < desc->hw.nr_ack_regs; i++) {
693 mr = desc->hw.ack_regs + i;
Magnus Dammd58876e2008-04-24 21:36:34 +0900694
695 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
696 if (mr->enum_ids[j] != enum_id)
697 continue;
698
699 fn = REG_FN_MODIFY_BASE;
700 mode = MODE_ENABLE_REG;
701 reg_e = mr->set_reg;
702 reg_d = mr->set_reg;
703
704 fn += (mr->reg_width >> 3) - 1;
705 return _INTC_MK(fn, mode,
706 intc_get_reg(d, reg_e),
707 intc_get_reg(d, reg_d),
708 1,
709 (mr->reg_width - 1) - j);
710 }
711 }
712
713 return 0;
714}
Magnus Dammd58876e2008-04-24 21:36:34 +0900715
Magnus Damm73505b42007-08-12 15:26:12 +0900716static unsigned int __init intc_sense_data(struct intc_desc *desc,
717 struct intc_desc_int *d,
718 intc_enum enum_id)
719{
Magnus Damm577cd752010-02-09 04:24:46 +0000720 struct intc_sense_reg *sr = desc->hw.sense_regs;
Magnus Damm73505b42007-08-12 15:26:12 +0900721 unsigned int i, j, fn, bit;
722
Magnus Damm577cd752010-02-09 04:24:46 +0000723 for (i = 0; sr && enum_id && i < desc->hw.nr_sense_regs; i++) {
724 sr = desc->hw.sense_regs + i;
Magnus Damm73505b42007-08-12 15:26:12 +0900725
726 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
727 if (sr->enum_ids[j] != enum_id)
728 continue;
729
730 fn = REG_FN_MODIFY_BASE;
731 fn += (sr->reg_width >> 3) - 1;
Magnus Damm73505b42007-08-12 15:26:12 +0900732
roel kluinb21a9102008-09-09 23:02:43 +0200733 BUG_ON((j + 1) * sr->field_width > sr->reg_width);
734
735 bit = sr->reg_width - ((j + 1) * sr->field_width);
Magnus Damm73505b42007-08-12 15:26:12 +0900736
737 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
738 0, sr->field_width, bit);
739 }
740 }
741
742 return 0;
743}
744
745static void __init intc_register_irq(struct intc_desc *desc,
746 struct intc_desc_int *d,
747 intc_enum enum_id,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900748 unsigned int irq)
749{
Magnus Damm3d37d942007-08-17 00:50:44 +0900750 struct intc_handle_int *hp;
Magnus Damm680c4592007-07-20 12:09:29 +0900751 unsigned int data[2], primary;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900752
Paul Mundt1ce7b032009-11-02 10:30:26 +0900753 /*
754 * Register the IRQ position with the global IRQ map
755 */
756 set_bit(irq, intc_irq_map);
757
Magnus Damm680c4592007-07-20 12:09:29 +0900758 /* Prefer single interrupt source bitmap over other combinations:
759 * 1. bitmap, single interrupt source
760 * 2. priority, single interrupt source
761 * 3. bitmap, multiple interrupt sources (groups)
762 * 4. priority, multiple interrupt sources (groups)
763 */
764
Magnus Damm73505b42007-08-12 15:26:12 +0900765 data[0] = intc_mask_data(desc, d, enum_id, 0);
766 data[1] = intc_prio_data(desc, d, enum_id, 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900767
768 primary = 0;
769 if (!data[0] && data[1])
770 primary = 1;
771
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900772 if (!data[0] && !data[1])
Paul Mundtf0335992009-03-06 17:56:58 +0900773 pr_warning("intc: missing unique irq mask for "
774 "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900775
Magnus Damm73505b42007-08-12 15:26:12 +0900776 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
777 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
Magnus Damm680c4592007-07-20 12:09:29 +0900778
779 if (!data[primary])
780 primary ^= 1;
781
782 BUG_ON(!data[primary]); /* must have primary masking method */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900783
784 disable_irq_nosync(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900785 set_irq_chip_and_handler_name(irq, &d->chip,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900786 handle_level_irq, "level");
Magnus Damm680c4592007-07-20 12:09:29 +0900787 set_irq_chip_data(irq, (void *)data[primary]);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900788
Magnus Damm7f3edee2008-01-10 14:08:55 +0900789 /* set priority level
790 * - this needs to be at least 2 for 5-bit priorities on 7780
791 */
Paul Mundt43b87742010-04-13 14:43:03 +0900792 intc_prio_level[irq] = default_prio_level;
Magnus Damm73505b42007-08-12 15:26:12 +0900793
Magnus Damm680c4592007-07-20 12:09:29 +0900794 /* enable secondary masking method if present */
795 if (data[!primary])
Magnus Damm73505b42007-08-12 15:26:12 +0900796 _intc_enable(irq, data[!primary]);
797
798 /* add irq to d->prio list if priority is available */
799 if (data[1]) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900800 hp = d->prio + d->nr_prio;
801 hp->irq = irq;
802 hp->handle = data[1];
803
804 if (primary) {
805 /*
806 * only secondary priority should access registers, so
807 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
808 */
809
810 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
811 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
812 }
Magnus Damm73505b42007-08-12 15:26:12 +0900813 d->nr_prio++;
814 }
815
816 /* add irq to d->sense list if sense is available */
817 data[0] = intc_sense_data(desc, d, enum_id);
818 if (data[0]) {
819 (d->sense + d->nr_sense)->irq = irq;
820 (d->sense + d->nr_sense)->handle = data[0];
821 d->nr_sense++;
822 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900823
824 /* irq should be disabled by default */
Magnus Damm73505b42007-08-12 15:26:12 +0900825 d->chip.mask(irq);
Magnus Dammd58876e2008-04-24 21:36:34 +0900826
Magnus Damm577cd752010-02-09 04:24:46 +0000827 if (desc->hw.ack_regs)
Magnus Dammd58876e2008-04-24 21:36:34 +0900828 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
Magnus Damm65a5b282010-02-05 11:15:25 +0000829
830#ifdef CONFIG_ARM
831 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
832#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900833}
834
Magnus Dammf18d5332007-09-21 18:16:42 +0900835static unsigned int __init save_reg(struct intc_desc_int *d,
836 unsigned int cnt,
837 unsigned long value,
838 unsigned int smp)
839{
840 if (value) {
Magnus Dammdec710b2010-03-19 16:48:01 +0900841 value = intc_phys_to_virt(d, value);
842
Magnus Dammf18d5332007-09-21 18:16:42 +0900843 d->reg[cnt] = value;
844#ifdef CONFIG_SMP
845 d->smp[cnt] = smp;
846#endif
847 return 1;
848 }
849
850 return 0;
851}
852
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900853static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900854{
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900855 generic_handle_irq((unsigned int)get_irq_data(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900856}
Magnus Dammf18d5332007-09-21 18:16:42 +0900857
Magnus Damm01e96512010-03-10 09:31:01 +0000858int __init register_intc_controller(struct intc_desc *desc)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900859{
Paul Mundt54ff3282009-06-11 10:33:09 +0300860 unsigned int i, k, smp;
Magnus Damm577cd752010-02-09 04:24:46 +0000861 struct intc_hw_desc *hw = &desc->hw;
Magnus Damm73505b42007-08-12 15:26:12 +0900862 struct intc_desc_int *d;
Magnus Dammdec710b2010-03-19 16:48:01 +0900863 struct resource *res;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900864
Paul Mundt12129fe2010-04-13 13:49:54 +0900865 pr_info("intc: Registered controller '%s' with %u IRQs\n",
866 desc->name, hw->nr_vectors);
867
Paul Mundt11b6aa92009-06-12 01:34:12 +0300868 d = kzalloc(sizeof(*d), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000869 if (!d)
870 goto err0;
Magnus Damm73505b42007-08-12 15:26:12 +0900871
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000872 INIT_LIST_HEAD(&d->list);
873 list_add(&d->list, &intc_list);
874
Magnus Dammdec710b2010-03-19 16:48:01 +0900875 if (desc->num_resources) {
876 d->nr_windows = desc->num_resources;
877 d->window = kzalloc(d->nr_windows * sizeof(*d->window),
878 GFP_NOWAIT);
879 if (!d->window)
880 goto err1;
881
882 for (k = 0; k < d->nr_windows; k++) {
883 res = desc->resource + k;
884 WARN_ON(resource_type(res) != IORESOURCE_MEM);
885 d->window[k].phys = res->start;
886 d->window[k].size = resource_size(res);
887 d->window[k].virt = ioremap_nocache(res->start,
888 resource_size(res));
889 if (!d->window[k].virt)
890 goto err2;
891 }
892 }
893
Magnus Damm577cd752010-02-09 04:24:46 +0000894 d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
895 d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
896 d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
897 d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
Paul Mundt9b798d52009-10-27 11:36:43 +0900898
Paul Mundt11b6aa92009-06-12 01:34:12 +0300899 d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000900 if (!d->reg)
Magnus Dammdec710b2010-03-19 16:48:01 +0900901 goto err2;
Magnus Damm01e96512010-03-10 09:31:01 +0000902
Magnus Dammf18d5332007-09-21 18:16:42 +0900903#ifdef CONFIG_SMP
Paul Mundt11b6aa92009-06-12 01:34:12 +0300904 d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000905 if (!d->smp)
Magnus Dammdec710b2010-03-19 16:48:01 +0900906 goto err3;
Magnus Dammf18d5332007-09-21 18:16:42 +0900907#endif
Magnus Damm73505b42007-08-12 15:26:12 +0900908 k = 0;
909
Magnus Damm577cd752010-02-09 04:24:46 +0000910 if (hw->mask_regs) {
911 for (i = 0; i < hw->nr_mask_regs; i++) {
912 smp = IS_SMP(hw->mask_regs[i]);
913 k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
914 k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900915 }
916 }
917
Magnus Damm577cd752010-02-09 04:24:46 +0000918 if (hw->prio_regs) {
919 d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
920 GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000921 if (!d->prio)
Magnus Dammdec710b2010-03-19 16:48:01 +0900922 goto err4;
Magnus Damm73505b42007-08-12 15:26:12 +0900923
Magnus Damm577cd752010-02-09 04:24:46 +0000924 for (i = 0; i < hw->nr_prio_regs; i++) {
925 smp = IS_SMP(hw->prio_regs[i]);
926 k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
927 k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900928 }
929 }
930
Magnus Damm577cd752010-02-09 04:24:46 +0000931 if (hw->sense_regs) {
932 d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
933 GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000934 if (!d->sense)
Magnus Dammdec710b2010-03-19 16:48:01 +0900935 goto err5;
Magnus Damm73505b42007-08-12 15:26:12 +0900936
Magnus Damm577cd752010-02-09 04:24:46 +0000937 for (i = 0; i < hw->nr_sense_regs; i++)
938 k += save_reg(d, k, hw->sense_regs[i].reg, 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900939 }
940
Magnus Damm73505b42007-08-12 15:26:12 +0900941 d->chip.name = desc->name;
942 d->chip.mask = intc_disable;
943 d->chip.unmask = intc_enable;
944 d->chip.mask_ack = intc_disable;
Magnus Dammf7dd2542009-04-01 14:20:58 +0000945 d->chip.enable = intc_enable;
946 d->chip.disable = intc_disable;
947 d->chip.shutdown = intc_disable;
Magnus Damm73505b42007-08-12 15:26:12 +0900948 d->chip.set_type = intc_set_sense;
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000949 d->chip.set_wake = intc_set_wake;
Paul Mundta8941da2010-03-08 13:33:17 +0900950#ifdef CONFIG_SMP
951 d->chip.set_affinity = intc_set_affinity;
952#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900953
Magnus Damm577cd752010-02-09 04:24:46 +0000954 if (hw->ack_regs) {
955 for (i = 0; i < hw->nr_ack_regs; i++)
956 k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
Magnus Dammd58876e2008-04-24 21:36:34 +0900957
958 d->chip.mask_ack = intc_mask_ack;
959 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900960
Magnus Dammd85429a2010-02-15 11:40:25 +0000961 /* disable bits matching force_disable before registering irqs */
962 if (desc->force_disable)
963 intc_enable_disable_enum(desc, d, desc->force_disable, 0);
Magnus Dammd5190952010-02-09 04:29:22 +0000964
965 /* disable bits matching force_enable before registering irqs */
966 if (desc->force_enable)
967 intc_enable_disable_enum(desc, d, desc->force_enable, 0);
968
Magnus Dammd58876e2008-04-24 21:36:34 +0900969 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
970
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900971 /* register the vectors one by one */
Magnus Damm577cd752010-02-09 04:24:46 +0000972 for (i = 0; i < hw->nr_vectors; i++) {
973 struct intc_vect *vect = hw->vectors + i;
Paul Mundt05ff3002009-05-22 01:28:33 +0900974 unsigned int irq = evt2irq(vect->vect);
975 struct irq_desc *irq_desc;
Paul Mundt54ff3282009-06-11 10:33:09 +0300976
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900977 if (!vect->enum_id)
978 continue;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900979
Paul Mundt54ff3282009-06-11 10:33:09 +0300980 irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
Paul Mundt05ff3002009-05-22 01:28:33 +0900981 if (unlikely(!irq_desc)) {
Paul Mundt12129fe2010-04-13 13:49:54 +0900982 pr_err("can't get irq_desc for %d\n", irq);
Paul Mundt05ff3002009-05-22 01:28:33 +0900983 continue;
984 }
985
986 intc_register_irq(desc, d, vect->enum_id, irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900987
Magnus Damm577cd752010-02-09 04:24:46 +0000988 for (k = i + 1; k < hw->nr_vectors; k++) {
989 struct intc_vect *vect2 = hw->vectors + k;
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900990 unsigned int irq2 = evt2irq(vect2->vect);
991
992 if (vect->enum_id != vect2->enum_id)
993 continue;
994
Paul Mundt1279b7f2009-08-31 15:15:33 +0900995 /*
996 * In the case of multi-evt handling and sparse
997 * IRQ support, each vector still needs to have
998 * its own backing irq_desc.
999 */
1000 irq_desc = irq_to_desc_alloc_node(irq2, numa_node_id());
1001 if (unlikely(!irq_desc)) {
Paul Mundt12129fe2010-04-13 13:49:54 +09001002 pr_err("can't get irq_desc for %d\n", irq2);
Paul Mundt1279b7f2009-08-31 15:15:33 +09001003 continue;
1004 }
1005
Pawel Moll05ecd5a2009-08-24 19:52:38 +09001006 vect2->enum_id = 0;
1007
1008 /* redirect this interrupts to the first one */
Paul Mundt4d2185d2010-02-17 12:37:42 +09001009 set_irq_chip(irq2, &dummy_irq_chip);
Magnus Damme6f07752010-02-09 07:17:20 +00001010 set_irq_chained_handler(irq2, intc_redirect_irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +09001011 set_irq_data(irq2, (void *)irq);
1012 }
Magnus Damm02ab3f72007-07-18 17:25:09 +09001013 }
Magnus Dammd5190952010-02-09 04:29:22 +00001014
1015 /* enable bits matching force_enable after registering irqs */
1016 if (desc->force_enable)
1017 intc_enable_disable_enum(desc, d, desc->force_enable, 1);
Magnus Damm01e96512010-03-10 09:31:01 +00001018
1019 return 0;
Magnus Dammdec710b2010-03-19 16:48:01 +09001020err5:
Magnus Damm01e96512010-03-10 09:31:01 +00001021 kfree(d->prio);
Magnus Dammdec710b2010-03-19 16:48:01 +09001022err4:
Magnus Damm01e96512010-03-10 09:31:01 +00001023#ifdef CONFIG_SMP
1024 kfree(d->smp);
Magnus Dammdec710b2010-03-19 16:48:01 +09001025err3:
Magnus Damm01e96512010-03-10 09:31:01 +00001026#endif
1027 kfree(d->reg);
Magnus Dammdec710b2010-03-19 16:48:01 +09001028err2:
1029 for (k = 0; k < d->nr_windows; k++)
1030 if (d->window[k].virt)
1031 iounmap(d->window[k].virt);
1032
1033 kfree(d->window);
1034err1:
Magnus Damm01e96512010-03-10 09:31:01 +00001035 kfree(d);
Magnus Dammdec710b2010-03-19 16:48:01 +09001036err0:
Magnus Damm01e96512010-03-10 09:31:01 +00001037 pr_err("unable to allocate INTC memory\n");
1038
1039 return -ENOMEM;
Magnus Damm02ab3f72007-07-18 17:25:09 +09001040}
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001041
Paul Mundt43b87742010-04-13 14:43:03 +09001042#ifdef CONFIG_INTC_USERIMASK
1043static void __iomem *uimask;
1044
1045int register_intc_userimask(unsigned long addr)
1046{
1047 if (unlikely(uimask))
1048 return -EBUSY;
1049
1050 uimask = ioremap_nocache(addr, SZ_4K);
1051 if (unlikely(!uimask))
1052 return -ENOMEM;
1053
1054 pr_info("intc: userimask support registered for levels 0 -> %d\n",
1055 default_prio_level - 1);
1056
1057 return 0;
1058}
1059
1060static ssize_t
1061show_intc_userimask(struct sysdev_class *cls,
1062 struct sysdev_class_attribute *attr, char *buf)
1063{
1064 return sprintf(buf, "%d\n", (__raw_readl(uimask) >> 4) & 0xf);
1065}
1066
1067static ssize_t
1068store_intc_userimask(struct sysdev_class *cls,
1069 struct sysdev_class_attribute *attr,
1070 const char *buf, size_t count)
1071{
1072 unsigned long level;
1073
1074 level = simple_strtoul(buf, NULL, 10);
1075
1076 /*
1077 * Minimal acceptable IRQ levels are in the 2 - 16 range, but
1078 * these are chomped so as to not interfere with normal IRQs.
1079 *
1080 * Level 1 is a special case on some CPUs in that it's not
1081 * directly settable, but given that USERIMASK cuts off below a
1082 * certain level, we don't care about this limitation here.
1083 * Level 0 on the other hand equates to user masking disabled.
1084 *
1085 * We use default_prio_level as a cut off so that only special
1086 * case opt-in IRQs can be mangled.
1087 */
1088 if (level >= default_prio_level)
1089 return -EINVAL;
1090
1091 __raw_writel(0xa5 << 24 | level << 4, uimask);
1092
1093 return count;
1094}
1095
1096static SYSDEV_CLASS_ATTR(userimask, S_IRUSR | S_IWUSR,
1097 show_intc_userimask, store_intc_userimask);
1098#endif
1099
Paul Mundt0ded7542010-04-13 10:16:34 +09001100static ssize_t
1101show_intc_name(struct sys_device *dev, struct sysdev_attribute *attr, char *buf)
1102{
1103 struct intc_desc_int *d;
1104
1105 d = container_of(dev, struct intc_desc_int, sysdev);
1106
1107 return sprintf(buf, "%s\n", d->chip.name);
1108}
1109
1110static SYSDEV_ATTR(name, S_IRUGO, show_intc_name, NULL);
1111
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001112static int intc_suspend(struct sys_device *dev, pm_message_t state)
1113{
1114 struct intc_desc_int *d;
1115 struct irq_desc *desc;
1116 int irq;
1117
1118 /* get intc controller associated with this sysdev */
1119 d = container_of(dev, struct intc_desc_int, sysdev);
1120
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001121 switch (state.event) {
1122 case PM_EVENT_ON:
1123 if (d->state.event != PM_EVENT_FREEZE)
1124 break;
1125 for_each_irq_desc(irq, desc) {
Francesco VIRLINZI87a705d2009-12-04 08:57:58 +00001126 if (desc->handle_irq == intc_redirect_irq)
Paul Mundt0a753d52009-12-09 14:36:16 +09001127 continue;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001128 if (desc->chip != &d->chip)
1129 continue;
1130 if (desc->status & IRQ_DISABLED)
1131 intc_disable(irq);
1132 else
1133 intc_enable(irq);
1134 }
1135 break;
1136 case PM_EVENT_FREEZE:
1137 /* nothing has to be done */
1138 break;
1139 case PM_EVENT_SUSPEND:
1140 /* enable wakeup irqs belonging to this intc controller */
1141 for_each_irq_desc(irq, desc) {
1142 if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
1143 intc_enable(irq);
1144 }
1145 break;
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001146 }
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001147 d->state = state;
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001148
1149 return 0;
1150}
1151
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001152static int intc_resume(struct sys_device *dev)
1153{
1154 return intc_suspend(dev, PMSG_ON);
1155}
1156
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001157static struct sysdev_class intc_sysdev_class = {
1158 .name = "intc",
1159 .suspend = intc_suspend,
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001160 .resume = intc_resume,
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001161};
1162
1163/* register this intc as sysdev to allow suspend/resume */
1164static int __init register_intc_sysdevs(void)
1165{
1166 struct intc_desc_int *d;
1167 int error;
1168 int id = 0;
1169
1170 error = sysdev_class_register(&intc_sysdev_class);
Paul Mundt43b87742010-04-13 14:43:03 +09001171#ifdef CONFIG_INTC_USERIMASK
1172 if (!error && uimask)
1173 error = sysdev_class_create_file(&intc_sysdev_class,
1174 &attr_userimask);
1175#endif
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001176 if (!error) {
1177 list_for_each_entry(d, &intc_list, list) {
1178 d->sysdev.id = id;
1179 d->sysdev.cls = &intc_sysdev_class;
1180 error = sysdev_register(&d->sysdev);
Paul Mundt0ded7542010-04-13 10:16:34 +09001181 if (error == 0)
1182 error = sysdev_create_file(&d->sysdev,
1183 &attr_name);
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001184 if (error)
1185 break;
Paul Mundt0ded7542010-04-13 10:16:34 +09001186
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001187 id++;
1188 }
1189 }
1190
1191 if (error)
Paul Mundt12129fe2010-04-13 13:49:54 +09001192 pr_err("intc: sysdev registration error\n");
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001193
1194 return error;
1195}
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001196device_initcall(register_intc_sysdevs);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001197
1198/*
1199 * Dynamic IRQ allocation and deallocation
1200 */
Paul Mundte9867c52010-02-02 17:35:13 +09001201unsigned int create_irq_nr(unsigned int irq_want, int node)
Paul Mundt1ce7b032009-11-02 10:30:26 +09001202{
1203 unsigned int irq = 0, new;
1204 unsigned long flags;
1205 struct irq_desc *desc;
1206
1207 spin_lock_irqsave(&vector_lock, flags);
1208
1209 /*
Paul Mundte9867c52010-02-02 17:35:13 +09001210 * First try the wanted IRQ
Paul Mundt1ce7b032009-11-02 10:30:26 +09001211 */
Paul Mundte9867c52010-02-02 17:35:13 +09001212 if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
1213 new = irq_want;
1214 } else {
1215 /* .. then fall back to scanning. */
Paul Mundt1ce7b032009-11-02 10:30:26 +09001216 new = find_first_zero_bit(intc_irq_map, nr_irqs);
1217 if (unlikely(new == nr_irqs))
1218 goto out_unlock;
1219
Paul Mundt1ce7b032009-11-02 10:30:26 +09001220 __set_bit(new, intc_irq_map);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001221 }
1222
Paul Mundte9867c52010-02-02 17:35:13 +09001223 desc = irq_to_desc_alloc_node(new, node);
1224 if (unlikely(!desc)) {
Paul Mundt12129fe2010-04-13 13:49:54 +09001225 pr_err("can't get irq_desc for %d\n", new);
Paul Mundte9867c52010-02-02 17:35:13 +09001226 goto out_unlock;
1227 }
1228
1229 desc = move_irq_desc(desc, node);
1230 irq = new;
1231
Paul Mundt1ce7b032009-11-02 10:30:26 +09001232out_unlock:
1233 spin_unlock_irqrestore(&vector_lock, flags);
1234
Magnus Damm65a5b282010-02-05 11:15:25 +00001235 if (irq > 0) {
Paul Mundt1ce7b032009-11-02 10:30:26 +09001236 dynamic_irq_init(irq);
Magnus Damm65a5b282010-02-05 11:15:25 +00001237#ifdef CONFIG_ARM
1238 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
1239#endif
1240 }
Paul Mundt1ce7b032009-11-02 10:30:26 +09001241
1242 return irq;
1243}
1244
1245int create_irq(void)
1246{
1247 int nid = cpu_to_node(smp_processor_id());
1248 int irq;
1249
Paul Mundte9867c52010-02-02 17:35:13 +09001250 irq = create_irq_nr(NR_IRQS_LEGACY, nid);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001251 if (irq == 0)
1252 irq = -1;
1253
1254 return irq;
1255}
1256
1257void destroy_irq(unsigned int irq)
1258{
1259 unsigned long flags;
1260
1261 dynamic_irq_cleanup(irq);
1262
1263 spin_lock_irqsave(&vector_lock, flags);
1264 __clear_bit(irq, intc_irq_map);
1265 spin_unlock_irqrestore(&vector_lock, flags);
1266}
Paul Mundt45b9dea2009-11-02 15:43:20 +09001267
1268int reserve_irq_vector(unsigned int irq)
1269{
1270 unsigned long flags;
1271 int ret = 0;
1272
1273 spin_lock_irqsave(&vector_lock, flags);
1274 if (test_and_set_bit(irq, intc_irq_map))
1275 ret = -EBUSY;
1276 spin_unlock_irqrestore(&vector_lock, flags);
1277
1278 return ret;
1279}
1280
1281void reserve_irq_legacy(void)
1282{
1283 unsigned long flags;
1284 int i, j;
1285
1286 spin_lock_irqsave(&vector_lock, flags);
1287 j = find_first_bit(intc_irq_map, nr_irqs);
1288 for (i = 0; i < j; i++)
1289 __set_bit(i, intc_irq_map);
1290 spin_unlock_irqrestore(&vector_lock, flags);
1291}