blob: b605f7e478d6728491d3fe383cf17451e5c9a9cf [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
Magnus Dammdec710b2010-03-19 16:48:01 +090047struct intc_window {
48 phys_addr_t phys;
49 void __iomem *virt;
50 unsigned long size;
51};
52
Magnus Damm73505b42007-08-12 15:26:12 +090053struct intc_desc_int {
Magnus Damm2dcec7a2009-04-01 14:30:59 +000054 struct list_head list;
55 struct sys_device sysdev;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +000056 pm_message_t state;
Magnus Damm73505b42007-08-12 15:26:12 +090057 unsigned long *reg;
Magnus Dammf18d5332007-09-21 18:16:42 +090058#ifdef CONFIG_SMP
59 unsigned long *smp;
60#endif
Magnus Damm73505b42007-08-12 15:26:12 +090061 unsigned int nr_reg;
62 struct intc_handle_int *prio;
63 unsigned int nr_prio;
64 struct intc_handle_int *sense;
65 unsigned int nr_sense;
Magnus Dammdec710b2010-03-19 16:48:01 +090066 struct intc_window *window;
67 unsigned int nr_windows;
Magnus Damm73505b42007-08-12 15:26:12 +090068 struct irq_chip chip;
69};
70
Magnus Damm2dcec7a2009-04-01 14:30:59 +000071static LIST_HEAD(intc_list);
72
Paul Mundt1ce7b032009-11-02 10:30:26 +090073/*
74 * The intc_irq_map provides a global map of bound IRQ vectors for a
75 * given platform. Allocation of IRQs are either static through the CPU
76 * vector map, or dynamic in the case of board mux vectors or MSI.
77 *
78 * As this is a central point for all IRQ controllers on the system,
79 * each of the available sources are mapped out here. This combined with
80 * sparseirq makes it quite trivial to keep the vector map tightly packed
81 * when dynamically creating IRQs, as well as tying in to otherwise
82 * unused irq_desc positions in the sparse array.
83 */
84static DECLARE_BITMAP(intc_irq_map, NR_IRQS);
85static DEFINE_SPINLOCK(vector_lock);
86
Magnus Dammf18d5332007-09-21 18:16:42 +090087#ifdef CONFIG_SMP
88#define IS_SMP(x) x.smp
89#define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
90#define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
91#else
92#define IS_SMP(x) 0
93#define INTC_REG(d, x, c) (d->reg[(x)])
94#define SMP_NR(d, x) 1
95#endif
96
Magnus Damm73505b42007-08-12 15:26:12 +090097static unsigned int intc_prio_level[NR_IRQS]; /* for now */
Magnus Dammd58876e2008-04-24 21:36:34 +090098static unsigned long ack_handle[NR_IRQS];
Magnus Damm73505b42007-08-12 15:26:12 +090099
100static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900101{
102 struct irq_chip *chip = get_irq_chip(irq);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900103 return container_of(chip, struct intc_desc_int, chip);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900104}
105
106static inline unsigned int set_field(unsigned int value,
107 unsigned int field_value,
Magnus Damm73505b42007-08-12 15:26:12 +0900108 unsigned int handle)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900109{
Magnus Damm73505b42007-08-12 15:26:12 +0900110 unsigned int width = _INTC_WIDTH(handle);
111 unsigned int shift = _INTC_SHIFT(handle);
112
Magnus Damm02ab3f72007-07-18 17:25:09 +0900113 value &= ~(((1 << width) - 1) << shift);
114 value |= field_value << shift;
115 return value;
116}
117
Magnus Damm73505b42007-08-12 15:26:12 +0900118static void write_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900119{
Paul Mundt62429e02008-10-01 15:19:10 +0900120 __raw_writeb(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900121 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900122}
123
Magnus Damm73505b42007-08-12 15:26:12 +0900124static void write_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900125{
Paul Mundt62429e02008-10-01 15:19:10 +0900126 __raw_writew(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900127 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900128}
129
Magnus Damm73505b42007-08-12 15:26:12 +0900130static void write_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900131{
Paul Mundt62429e02008-10-01 15:19:10 +0900132 __raw_writel(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900133 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900134}
135
Magnus Damm73505b42007-08-12 15:26:12 +0900136static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900137{
Magnus Damm4370fe12008-04-24 21:53:07 +0900138 unsigned long flags;
139 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900140 __raw_writeb(set_field(__raw_readb(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900141 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900142 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900143}
144
Magnus Damm73505b42007-08-12 15:26:12 +0900145static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900146{
Magnus Damm4370fe12008-04-24 21:53:07 +0900147 unsigned long flags;
148 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900149 __raw_writew(set_field(__raw_readw(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900150 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900151 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900152}
153
Magnus Damm73505b42007-08-12 15:26:12 +0900154static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900155{
Magnus Damm4370fe12008-04-24 21:53:07 +0900156 unsigned long flags;
157 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900158 __raw_writel(set_field(__raw_readl(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900159 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900160 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900161}
162
Magnus Damm73505b42007-08-12 15:26:12 +0900163enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
Magnus Damm02ab3f72007-07-18 17:25:09 +0900164
Magnus Damm73505b42007-08-12 15:26:12 +0900165static void (*intc_reg_fns[])(unsigned long addr,
166 unsigned long h,
167 unsigned long data) = {
168 [REG_FN_WRITE_BASE + 0] = write_8,
169 [REG_FN_WRITE_BASE + 1] = write_16,
170 [REG_FN_WRITE_BASE + 3] = write_32,
171 [REG_FN_MODIFY_BASE + 0] = modify_8,
172 [REG_FN_MODIFY_BASE + 1] = modify_16,
173 [REG_FN_MODIFY_BASE + 3] = modify_32,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900174};
175
Magnus Damm73505b42007-08-12 15:26:12 +0900176enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
177 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
178 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
179 MODE_PRIO_REG, /* Priority value written to enable interrupt */
180 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
181};
182
183static void intc_mode_field(unsigned long addr,
184 unsigned long handle,
185 void (*fn)(unsigned long,
186 unsigned long,
187 unsigned long),
188 unsigned int irq)
189{
190 fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
191}
192
193static void intc_mode_zero(unsigned long addr,
194 unsigned long handle,
195 void (*fn)(unsigned long,
196 unsigned long,
197 unsigned long),
198 unsigned int irq)
199{
200 fn(addr, handle, 0);
201}
202
203static void intc_mode_prio(unsigned long addr,
204 unsigned long handle,
205 void (*fn)(unsigned long,
206 unsigned long,
207 unsigned long),
208 unsigned int irq)
209{
210 fn(addr, handle, intc_prio_level[irq]);
211}
212
213static void (*intc_enable_fns[])(unsigned long addr,
214 unsigned long handle,
215 void (*fn)(unsigned long,
216 unsigned long,
217 unsigned long),
218 unsigned int irq) = {
219 [MODE_ENABLE_REG] = intc_mode_field,
220 [MODE_MASK_REG] = intc_mode_zero,
221 [MODE_DUAL_REG] = intc_mode_field,
222 [MODE_PRIO_REG] = intc_mode_prio,
223 [MODE_PCLR_REG] = intc_mode_prio,
224};
225
226static void (*intc_disable_fns[])(unsigned long addr,
227 unsigned long handle,
228 void (*fn)(unsigned long,
229 unsigned long,
230 unsigned long),
231 unsigned int irq) = {
232 [MODE_ENABLE_REG] = intc_mode_zero,
233 [MODE_MASK_REG] = intc_mode_field,
234 [MODE_DUAL_REG] = intc_mode_field,
235 [MODE_PRIO_REG] = intc_mode_zero,
236 [MODE_PCLR_REG] = intc_mode_field,
237};
238
239static inline void _intc_enable(unsigned int irq, unsigned long handle)
240{
241 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900242 unsigned long addr;
243 unsigned int cpu;
Magnus Damm73505b42007-08-12 15:26:12 +0900244
Magnus Dammf18d5332007-09-21 18:16:42 +0900245 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
Paul Mundta8941da2010-03-08 13:33:17 +0900246#ifdef CONFIG_SMP
247 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
248 continue;
249#endif
Magnus Dammf18d5332007-09-21 18:16:42 +0900250 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
251 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
252 [_INTC_FN(handle)], irq);
253 }
Magnus Damm73505b42007-08-12 15:26:12 +0900254}
255
Magnus Damm02ab3f72007-07-18 17:25:09 +0900256static void intc_enable(unsigned int irq)
257{
Magnus Damm73505b42007-08-12 15:26:12 +0900258 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
Magnus Damm02ab3f72007-07-18 17:25:09 +0900259}
260
261static void intc_disable(unsigned int irq)
262{
Magnus Dammf18d5332007-09-21 18:16:42 +0900263 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900264 unsigned long handle = (unsigned long) get_irq_chip_data(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900265 unsigned long addr;
266 unsigned int cpu;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900267
Magnus Dammf18d5332007-09-21 18:16:42 +0900268 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
Paul Mundta8941da2010-03-08 13:33:17 +0900269#ifdef CONFIG_SMP
270 if (!cpumask_test_cpu(cpu, irq_to_desc(irq)->affinity))
271 continue;
272#endif
Magnus Dammf18d5332007-09-21 18:16:42 +0900273 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
274 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
275 [_INTC_FN(handle)], irq);
276 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900277}
278
Magnus Dammd5190952010-02-09 04:29:22 +0000279static void (*intc_enable_noprio_fns[])(unsigned long addr,
280 unsigned long handle,
281 void (*fn)(unsigned long,
282 unsigned long,
283 unsigned long),
284 unsigned int irq) = {
285 [MODE_ENABLE_REG] = intc_mode_field,
286 [MODE_MASK_REG] = intc_mode_zero,
287 [MODE_DUAL_REG] = intc_mode_field,
288 [MODE_PRIO_REG] = intc_mode_field,
289 [MODE_PCLR_REG] = intc_mode_field,
290};
291
292static void intc_enable_disable(struct intc_desc_int *d,
293 unsigned long handle, int do_enable)
294{
295 unsigned long addr;
296 unsigned int cpu;
297 void (*fn)(unsigned long, unsigned long,
298 void (*)(unsigned long, unsigned long, unsigned long),
299 unsigned int);
300
301 if (do_enable) {
302 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
303 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
304 fn = intc_enable_noprio_fns[_INTC_MODE(handle)];
305 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
306 }
307 } else {
308 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
309 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
310 fn = intc_disable_fns[_INTC_MODE(handle)];
311 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
312 }
313 }
314}
315
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000316static int intc_set_wake(unsigned int irq, unsigned int on)
317{
318 return 0; /* allow wakeup, but setup hardware in intc_suspend() */
319}
320
Paul Mundta8941da2010-03-08 13:33:17 +0900321#ifdef CONFIG_SMP
322/*
323 * This is held with the irq desc lock held, so we don't require any
324 * additional locking here at the intc desc level. The affinity mask is
325 * later tested in the enable/disable paths.
326 */
327static int intc_set_affinity(unsigned int irq, const struct cpumask *cpumask)
328{
329 if (!cpumask_intersects(cpumask, cpu_online_mask))
330 return -1;
331
332 cpumask_copy(irq_to_desc(irq)->affinity, cpumask);
333
334 return 0;
335}
336#endif
337
Magnus Dammd58876e2008-04-24 21:36:34 +0900338static void intc_mask_ack(unsigned int irq)
339{
340 struct intc_desc_int *d = get_intc_desc(irq);
341 unsigned long handle = ack_handle[irq];
342 unsigned long addr;
343
344 intc_disable(irq);
345
346 /* read register and write zero only to the assocaited bit */
347
348 if (handle) {
349 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900350 switch (_INTC_FN(handle)) {
351 case REG_FN_MODIFY_BASE + 0: /* 8bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900352 __raw_readb(addr);
353 __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900354 break;
355 case REG_FN_MODIFY_BASE + 1: /* 16bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900356 __raw_readw(addr);
357 __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900358 break;
359 case REG_FN_MODIFY_BASE + 3: /* 32bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900360 __raw_readl(addr);
361 __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900362 break;
363 default:
364 BUG();
365 break;
366 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900367 }
368}
Magnus Dammd58876e2008-04-24 21:36:34 +0900369
Magnus Damm73505b42007-08-12 15:26:12 +0900370static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
371 unsigned int nr_hp,
372 unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900373{
Magnus Damm73505b42007-08-12 15:26:12 +0900374 int i;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900375
Magnus Damm3d37d942007-08-17 00:50:44 +0900376 /* this doesn't scale well, but...
377 *
378 * this function should only be used for cerain uncommon
379 * operations such as intc_set_priority() and intc_set_sense()
380 * and in those rare cases performance doesn't matter that much.
381 * keeping the memory footprint low is more important.
382 *
383 * one rather simple way to speed this up and still keep the
384 * memory footprint down is to make sure the array is sorted
385 * and then perform a bisect to lookup the irq.
386 */
387
Magnus Damm73505b42007-08-12 15:26:12 +0900388 for (i = 0; i < nr_hp; i++) {
389 if ((hp + i)->irq != irq)
390 continue;
391
392 return hp + i;
393 }
394
395 return NULL;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900396}
397
Magnus Damm73505b42007-08-12 15:26:12 +0900398int intc_set_priority(unsigned int irq, unsigned int prio)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900399{
Magnus Damm73505b42007-08-12 15:26:12 +0900400 struct intc_desc_int *d = get_intc_desc(irq);
401 struct intc_handle_int *ihp;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900402
Magnus Damm73505b42007-08-12 15:26:12 +0900403 if (!intc_prio_level[irq] || prio <= 1)
404 return -EINVAL;
405
406 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
407 if (ihp) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900408 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
Magnus Damm73505b42007-08-12 15:26:12 +0900409 return -EINVAL;
410
411 intc_prio_level[irq] = prio;
412
413 /*
414 * only set secondary masking method directly
415 * primary masking method is using intc_prio_level[irq]
416 * priority level will be set during next enable()
417 */
418
Magnus Damm3d37d942007-08-17 00:50:44 +0900419 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
Magnus Damm73505b42007-08-12 15:26:12 +0900420 _intc_enable(irq, ihp->handle);
421 }
422 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900423}
424
425#define VALID(x) (x | 0x80)
426
427static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
428 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
429 [IRQ_TYPE_EDGE_RISING] = VALID(1),
430 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
Magnus Damm720be992008-04-24 21:47:15 +0900431 /* SH7706, SH7707 and SH7709 do not support high level triggered */
432#if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
433 !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
434 !defined(CONFIG_CPU_SUBTYPE_SH7709)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900435 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
Magnus Damm720be992008-04-24 21:47:15 +0900436#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900437};
438
439static int intc_set_sense(unsigned int irq, unsigned int type)
440{
Magnus Damm73505b42007-08-12 15:26:12 +0900441 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900442 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
Magnus Damm73505b42007-08-12 15:26:12 +0900443 struct intc_handle_int *ihp;
444 unsigned long addr;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900445
Magnus Damm73505b42007-08-12 15:26:12 +0900446 if (!value)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900447 return -EINVAL;
448
Magnus Damm73505b42007-08-12 15:26:12 +0900449 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
450 if (ihp) {
Magnus Dammf18d5332007-09-21 18:16:42 +0900451 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900452 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900453 }
Magnus Damm73505b42007-08-12 15:26:12 +0900454 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900455}
456
Magnus Dammdec710b2010-03-19 16:48:01 +0900457static unsigned long intc_phys_to_virt(struct intc_desc_int *d,
458 unsigned long address)
459{
460 struct intc_window *window;
461 int k;
462
463 /* scan through physical windows and convert address */
464 for (k = 0; k < d->nr_windows; k++) {
465 window = d->window + k;
466
467 if (address < window->phys)
468 continue;
469
470 if (address >= (window->phys + window->size))
471 continue;
472
473 address -= window->phys;
474 address += (unsigned long)window->virt;
475
476 return address;
477 }
478
479 /* no windows defined, register must be 1:1 mapped virt:phys */
480 return address;
481}
482
Magnus Damm73505b42007-08-12 15:26:12 +0900483static unsigned int __init intc_get_reg(struct intc_desc_int *d,
Magnus Dammdec710b2010-03-19 16:48:01 +0900484 unsigned long address)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900485{
Magnus Damm73505b42007-08-12 15:26:12 +0900486 unsigned int k;
487
Magnus Dammdec710b2010-03-19 16:48:01 +0900488 address = intc_phys_to_virt(d, address);
489
Magnus Damm73505b42007-08-12 15:26:12 +0900490 for (k = 0; k < d->nr_reg; k++) {
491 if (d->reg[k] == address)
492 return k;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900493 }
494
495 BUG();
Magnus Damm73505b42007-08-12 15:26:12 +0900496 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900497}
498
Magnus Damm73505b42007-08-12 15:26:12 +0900499static intc_enum __init intc_grp_id(struct intc_desc *desc,
500 intc_enum enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900501{
Magnus Damm577cd752010-02-09 04:24:46 +0000502 struct intc_group *g = desc->hw.groups;
Magnus Damm680c4592007-07-20 12:09:29 +0900503 unsigned int i, j;
504
Magnus Damm577cd752010-02-09 04:24:46 +0000505 for (i = 0; g && enum_id && i < desc->hw.nr_groups; i++) {
506 g = desc->hw.groups + i;
Magnus Damm680c4592007-07-20 12:09:29 +0900507
508 for (j = 0; g->enum_ids[j]; j++) {
509 if (g->enum_ids[j] != enum_id)
510 continue;
511
512 return g->enum_id;
513 }
514 }
515
516 return 0;
517}
518
Magnus Dammd5190952010-02-09 04:29:22 +0000519static unsigned int __init _intc_mask_data(struct intc_desc *desc,
520 struct intc_desc_int *d,
521 intc_enum enum_id,
522 unsigned int *reg_idx,
523 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900524{
Magnus Damm577cd752010-02-09 04:24:46 +0000525 struct intc_mask_reg *mr = desc->hw.mask_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000526 unsigned int fn, mode;
Magnus Damm73505b42007-08-12 15:26:12 +0900527 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900528
Magnus Dammd5190952010-02-09 04:29:22 +0000529 while (mr && enum_id && *reg_idx < desc->hw.nr_mask_regs) {
530 mr = desc->hw.mask_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900531
Magnus Dammd5190952010-02-09 04:29:22 +0000532 for (; *fld_idx < ARRAY_SIZE(mr->enum_ids); (*fld_idx)++) {
533 if (mr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900534 continue;
535
Magnus Damm73505b42007-08-12 15:26:12 +0900536 if (mr->set_reg && mr->clr_reg) {
537 fn = REG_FN_WRITE_BASE;
538 mode = MODE_DUAL_REG;
539 reg_e = mr->clr_reg;
540 reg_d = mr->set_reg;
541 } else {
542 fn = REG_FN_MODIFY_BASE;
543 if (mr->set_reg) {
544 mode = MODE_ENABLE_REG;
545 reg_e = mr->set_reg;
546 reg_d = mr->set_reg;
547 } else {
548 mode = MODE_MASK_REG;
549 reg_e = mr->clr_reg;
550 reg_d = mr->clr_reg;
551 }
Magnus Damm51da6422007-08-03 14:25:32 +0900552 }
553
Magnus Damm73505b42007-08-12 15:26:12 +0900554 fn += (mr->reg_width >> 3) - 1;
555 return _INTC_MK(fn, mode,
556 intc_get_reg(d, reg_e),
557 intc_get_reg(d, reg_d),
558 1,
Magnus Dammd5190952010-02-09 04:29:22 +0000559 (mr->reg_width - 1) - *fld_idx);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900560 }
Magnus Dammd5190952010-02-09 04:29:22 +0000561
562 *fld_idx = 0;
563 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900564 }
565
Magnus Dammd5190952010-02-09 04:29:22 +0000566 return 0;
567}
568
569static unsigned int __init intc_mask_data(struct intc_desc *desc,
570 struct intc_desc_int *d,
571 intc_enum enum_id, int do_grps)
572{
573 unsigned int i = 0;
574 unsigned int j = 0;
575 unsigned int ret;
576
577 ret = _intc_mask_data(desc, d, enum_id, &i, &j);
578 if (ret)
579 return ret;
580
Magnus Damm680c4592007-07-20 12:09:29 +0900581 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900582 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900583
Magnus Damm02ab3f72007-07-18 17:25:09 +0900584 return 0;
585}
586
Magnus Dammd5190952010-02-09 04:29:22 +0000587static unsigned int __init _intc_prio_data(struct intc_desc *desc,
588 struct intc_desc_int *d,
589 intc_enum enum_id,
590 unsigned int *reg_idx,
591 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900592{
Magnus Damm577cd752010-02-09 04:24:46 +0000593 struct intc_prio_reg *pr = desc->hw.prio_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000594 unsigned int fn, n, mode, bit;
Magnus Damm73505b42007-08-12 15:26:12 +0900595 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900596
Magnus Dammd5190952010-02-09 04:29:22 +0000597 while (pr && enum_id && *reg_idx < desc->hw.nr_prio_regs) {
598 pr = desc->hw.prio_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900599
Magnus Dammd5190952010-02-09 04:29:22 +0000600 for (; *fld_idx < ARRAY_SIZE(pr->enum_ids); (*fld_idx)++) {
601 if (pr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900602 continue;
603
Magnus Damm73505b42007-08-12 15:26:12 +0900604 if (pr->set_reg && pr->clr_reg) {
605 fn = REG_FN_WRITE_BASE;
606 mode = MODE_PCLR_REG;
607 reg_e = pr->set_reg;
608 reg_d = pr->clr_reg;
609 } else {
610 fn = REG_FN_MODIFY_BASE;
611 mode = MODE_PRIO_REG;
612 if (!pr->set_reg)
613 BUG();
614 reg_e = pr->set_reg;
615 reg_d = pr->set_reg;
616 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900617
Magnus Damm73505b42007-08-12 15:26:12 +0900618 fn += (pr->reg_width >> 3) - 1;
Magnus Dammd5190952010-02-09 04:29:22 +0000619 n = *fld_idx + 1;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900620
Magnus Dammd5190952010-02-09 04:29:22 +0000621 BUG_ON(n * pr->field_width > pr->reg_width);
roel kluinb21a9102008-09-09 23:02:43 +0200622
Magnus Dammd5190952010-02-09 04:29:22 +0000623 bit = pr->reg_width - (n * pr->field_width);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900624
Magnus Damm73505b42007-08-12 15:26:12 +0900625 return _INTC_MK(fn, mode,
626 intc_get_reg(d, reg_e),
627 intc_get_reg(d, reg_d),
628 pr->field_width, bit);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900629 }
Magnus Dammd5190952010-02-09 04:29:22 +0000630
631 *fld_idx = 0;
632 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900633 }
634
Magnus Dammd5190952010-02-09 04:29:22 +0000635 return 0;
636}
637
638static unsigned int __init intc_prio_data(struct intc_desc *desc,
639 struct intc_desc_int *d,
640 intc_enum enum_id, int do_grps)
641{
642 unsigned int i = 0;
643 unsigned int j = 0;
644 unsigned int ret;
645
646 ret = _intc_prio_data(desc, d, enum_id, &i, &j);
647 if (ret)
648 return ret;
649
Magnus Damm680c4592007-07-20 12:09:29 +0900650 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900651 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900652
Magnus Damm02ab3f72007-07-18 17:25:09 +0900653 return 0;
654}
655
Magnus Dammd5190952010-02-09 04:29:22 +0000656static void __init intc_enable_disable_enum(struct intc_desc *desc,
657 struct intc_desc_int *d,
658 intc_enum enum_id, int enable)
659{
660 unsigned int i, j, data;
661
662 /* go through and enable/disable all mask bits */
663 i = j = 0;
664 do {
665 data = _intc_mask_data(desc, d, enum_id, &i, &j);
666 if (data)
667 intc_enable_disable(d, data, enable);
668 j++;
669 } while (data);
670
671 /* go through and enable/disable all priority fields */
672 i = j = 0;
673 do {
674 data = _intc_prio_data(desc, d, enum_id, &i, &j);
675 if (data)
676 intc_enable_disable(d, data, enable);
677
678 j++;
679 } while (data);
680}
681
Magnus Dammd58876e2008-04-24 21:36:34 +0900682static unsigned int __init intc_ack_data(struct intc_desc *desc,
683 struct intc_desc_int *d,
684 intc_enum enum_id)
685{
Magnus Damm577cd752010-02-09 04:24:46 +0000686 struct intc_mask_reg *mr = desc->hw.ack_regs;
Magnus Dammd58876e2008-04-24 21:36:34 +0900687 unsigned int i, j, fn, mode;
688 unsigned long reg_e, reg_d;
689
Magnus Damm577cd752010-02-09 04:24:46 +0000690 for (i = 0; mr && enum_id && i < desc->hw.nr_ack_regs; i++) {
691 mr = desc->hw.ack_regs + i;
Magnus Dammd58876e2008-04-24 21:36:34 +0900692
693 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
694 if (mr->enum_ids[j] != enum_id)
695 continue;
696
697 fn = REG_FN_MODIFY_BASE;
698 mode = MODE_ENABLE_REG;
699 reg_e = mr->set_reg;
700 reg_d = mr->set_reg;
701
702 fn += (mr->reg_width >> 3) - 1;
703 return _INTC_MK(fn, mode,
704 intc_get_reg(d, reg_e),
705 intc_get_reg(d, reg_d),
706 1,
707 (mr->reg_width - 1) - j);
708 }
709 }
710
711 return 0;
712}
Magnus Dammd58876e2008-04-24 21:36:34 +0900713
Magnus Damm73505b42007-08-12 15:26:12 +0900714static unsigned int __init intc_sense_data(struct intc_desc *desc,
715 struct intc_desc_int *d,
716 intc_enum enum_id)
717{
Magnus Damm577cd752010-02-09 04:24:46 +0000718 struct intc_sense_reg *sr = desc->hw.sense_regs;
Magnus Damm73505b42007-08-12 15:26:12 +0900719 unsigned int i, j, fn, bit;
720
Magnus Damm577cd752010-02-09 04:24:46 +0000721 for (i = 0; sr && enum_id && i < desc->hw.nr_sense_regs; i++) {
722 sr = desc->hw.sense_regs + i;
Magnus Damm73505b42007-08-12 15:26:12 +0900723
724 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
725 if (sr->enum_ids[j] != enum_id)
726 continue;
727
728 fn = REG_FN_MODIFY_BASE;
729 fn += (sr->reg_width >> 3) - 1;
Magnus Damm73505b42007-08-12 15:26:12 +0900730
roel kluinb21a9102008-09-09 23:02:43 +0200731 BUG_ON((j + 1) * sr->field_width > sr->reg_width);
732
733 bit = sr->reg_width - ((j + 1) * sr->field_width);
Magnus Damm73505b42007-08-12 15:26:12 +0900734
735 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
736 0, sr->field_width, bit);
737 }
738 }
739
740 return 0;
741}
742
743static void __init intc_register_irq(struct intc_desc *desc,
744 struct intc_desc_int *d,
745 intc_enum enum_id,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900746 unsigned int irq)
747{
Magnus Damm3d37d942007-08-17 00:50:44 +0900748 struct intc_handle_int *hp;
Magnus Damm680c4592007-07-20 12:09:29 +0900749 unsigned int data[2], primary;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900750
Paul Mundt1ce7b032009-11-02 10:30:26 +0900751 /*
752 * Register the IRQ position with the global IRQ map
753 */
754 set_bit(irq, intc_irq_map);
755
Magnus Damm680c4592007-07-20 12:09:29 +0900756 /* Prefer single interrupt source bitmap over other combinations:
757 * 1. bitmap, single interrupt source
758 * 2. priority, single interrupt source
759 * 3. bitmap, multiple interrupt sources (groups)
760 * 4. priority, multiple interrupt sources (groups)
761 */
762
Magnus Damm73505b42007-08-12 15:26:12 +0900763 data[0] = intc_mask_data(desc, d, enum_id, 0);
764 data[1] = intc_prio_data(desc, d, enum_id, 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900765
766 primary = 0;
767 if (!data[0] && data[1])
768 primary = 1;
769
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900770 if (!data[0] && !data[1])
Paul Mundtf0335992009-03-06 17:56:58 +0900771 pr_warning("intc: missing unique irq mask for "
772 "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900773
Magnus Damm73505b42007-08-12 15:26:12 +0900774 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
775 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
Magnus Damm680c4592007-07-20 12:09:29 +0900776
777 if (!data[primary])
778 primary ^= 1;
779
780 BUG_ON(!data[primary]); /* must have primary masking method */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900781
782 disable_irq_nosync(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900783 set_irq_chip_and_handler_name(irq, &d->chip,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900784 handle_level_irq, "level");
Magnus Damm680c4592007-07-20 12:09:29 +0900785 set_irq_chip_data(irq, (void *)data[primary]);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900786
Magnus Damm7f3edee2008-01-10 14:08:55 +0900787 /* set priority level
788 * - this needs to be at least 2 for 5-bit priorities on 7780
789 */
790 intc_prio_level[irq] = 2;
Magnus Damm73505b42007-08-12 15:26:12 +0900791
Magnus Damm680c4592007-07-20 12:09:29 +0900792 /* enable secondary masking method if present */
793 if (data[!primary])
Magnus Damm73505b42007-08-12 15:26:12 +0900794 _intc_enable(irq, data[!primary]);
795
796 /* add irq to d->prio list if priority is available */
797 if (data[1]) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900798 hp = d->prio + d->nr_prio;
799 hp->irq = irq;
800 hp->handle = data[1];
801
802 if (primary) {
803 /*
804 * only secondary priority should access registers, so
805 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
806 */
807
808 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
809 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
810 }
Magnus Damm73505b42007-08-12 15:26:12 +0900811 d->nr_prio++;
812 }
813
814 /* add irq to d->sense list if sense is available */
815 data[0] = intc_sense_data(desc, d, enum_id);
816 if (data[0]) {
817 (d->sense + d->nr_sense)->irq = irq;
818 (d->sense + d->nr_sense)->handle = data[0];
819 d->nr_sense++;
820 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900821
822 /* irq should be disabled by default */
Magnus Damm73505b42007-08-12 15:26:12 +0900823 d->chip.mask(irq);
Magnus Dammd58876e2008-04-24 21:36:34 +0900824
Magnus Damm577cd752010-02-09 04:24:46 +0000825 if (desc->hw.ack_regs)
Magnus Dammd58876e2008-04-24 21:36:34 +0900826 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
Magnus Damm65a5b282010-02-05 11:15:25 +0000827
828#ifdef CONFIG_ARM
829 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
830#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900831}
832
Magnus Dammf18d5332007-09-21 18:16:42 +0900833static unsigned int __init save_reg(struct intc_desc_int *d,
834 unsigned int cnt,
835 unsigned long value,
836 unsigned int smp)
837{
838 if (value) {
Magnus Dammdec710b2010-03-19 16:48:01 +0900839 value = intc_phys_to_virt(d, value);
840
Magnus Dammf18d5332007-09-21 18:16:42 +0900841 d->reg[cnt] = value;
842#ifdef CONFIG_SMP
843 d->smp[cnt] = smp;
844#endif
845 return 1;
846 }
847
848 return 0;
849}
850
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900851static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900852{
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900853 generic_handle_irq((unsigned int)get_irq_data(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900854}
Magnus Dammf18d5332007-09-21 18:16:42 +0900855
Magnus Damm01e96512010-03-10 09:31:01 +0000856int __init register_intc_controller(struct intc_desc *desc)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900857{
Paul Mundt54ff3282009-06-11 10:33:09 +0300858 unsigned int i, k, smp;
Magnus Damm577cd752010-02-09 04:24:46 +0000859 struct intc_hw_desc *hw = &desc->hw;
Magnus Damm73505b42007-08-12 15:26:12 +0900860 struct intc_desc_int *d;
Magnus Dammdec710b2010-03-19 16:48:01 +0900861 struct resource *res;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900862
Paul Mundt11b6aa92009-06-12 01:34:12 +0300863 d = kzalloc(sizeof(*d), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000864 if (!d)
865 goto err0;
Magnus Damm73505b42007-08-12 15:26:12 +0900866
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000867 INIT_LIST_HEAD(&d->list);
868 list_add(&d->list, &intc_list);
869
Magnus Dammdec710b2010-03-19 16:48:01 +0900870 if (desc->num_resources) {
871 d->nr_windows = desc->num_resources;
872 d->window = kzalloc(d->nr_windows * sizeof(*d->window),
873 GFP_NOWAIT);
874 if (!d->window)
875 goto err1;
876
877 for (k = 0; k < d->nr_windows; k++) {
878 res = desc->resource + k;
879 WARN_ON(resource_type(res) != IORESOURCE_MEM);
880 d->window[k].phys = res->start;
881 d->window[k].size = resource_size(res);
882 d->window[k].virt = ioremap_nocache(res->start,
883 resource_size(res));
884 if (!d->window[k].virt)
885 goto err2;
886 }
887 }
888
Magnus Damm577cd752010-02-09 04:24:46 +0000889 d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
890 d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
891 d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
892 d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
Paul Mundt9b798d52009-10-27 11:36:43 +0900893
Paul Mundt11b6aa92009-06-12 01:34:12 +0300894 d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000895 if (!d->reg)
Magnus Dammdec710b2010-03-19 16:48:01 +0900896 goto err2;
Magnus Damm01e96512010-03-10 09:31:01 +0000897
Magnus Dammf18d5332007-09-21 18:16:42 +0900898#ifdef CONFIG_SMP
Paul Mundt11b6aa92009-06-12 01:34:12 +0300899 d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000900 if (!d->smp)
Magnus Dammdec710b2010-03-19 16:48:01 +0900901 goto err3;
Magnus Dammf18d5332007-09-21 18:16:42 +0900902#endif
Magnus Damm73505b42007-08-12 15:26:12 +0900903 k = 0;
904
Magnus Damm577cd752010-02-09 04:24:46 +0000905 if (hw->mask_regs) {
906 for (i = 0; i < hw->nr_mask_regs; i++) {
907 smp = IS_SMP(hw->mask_regs[i]);
908 k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
909 k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900910 }
911 }
912
Magnus Damm577cd752010-02-09 04:24:46 +0000913 if (hw->prio_regs) {
914 d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
915 GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000916 if (!d->prio)
Magnus Dammdec710b2010-03-19 16:48:01 +0900917 goto err4;
Magnus Damm73505b42007-08-12 15:26:12 +0900918
Magnus Damm577cd752010-02-09 04:24:46 +0000919 for (i = 0; i < hw->nr_prio_regs; i++) {
920 smp = IS_SMP(hw->prio_regs[i]);
921 k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
922 k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900923 }
924 }
925
Magnus Damm577cd752010-02-09 04:24:46 +0000926 if (hw->sense_regs) {
927 d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
928 GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000929 if (!d->sense)
Magnus Dammdec710b2010-03-19 16:48:01 +0900930 goto err5;
Magnus Damm73505b42007-08-12 15:26:12 +0900931
Magnus Damm577cd752010-02-09 04:24:46 +0000932 for (i = 0; i < hw->nr_sense_regs; i++)
933 k += save_reg(d, k, hw->sense_regs[i].reg, 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900934 }
935
Magnus Damm73505b42007-08-12 15:26:12 +0900936 d->chip.name = desc->name;
937 d->chip.mask = intc_disable;
938 d->chip.unmask = intc_enable;
939 d->chip.mask_ack = intc_disable;
Magnus Dammf7dd2542009-04-01 14:20:58 +0000940 d->chip.enable = intc_enable;
941 d->chip.disable = intc_disable;
942 d->chip.shutdown = intc_disable;
Magnus Damm73505b42007-08-12 15:26:12 +0900943 d->chip.set_type = intc_set_sense;
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000944 d->chip.set_wake = intc_set_wake;
Paul Mundta8941da2010-03-08 13:33:17 +0900945#ifdef CONFIG_SMP
946 d->chip.set_affinity = intc_set_affinity;
947#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900948
Magnus Damm577cd752010-02-09 04:24:46 +0000949 if (hw->ack_regs) {
950 for (i = 0; i < hw->nr_ack_regs; i++)
951 k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
Magnus Dammd58876e2008-04-24 21:36:34 +0900952
953 d->chip.mask_ack = intc_mask_ack;
954 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900955
Magnus Dammd85429a2010-02-15 11:40:25 +0000956 /* disable bits matching force_disable before registering irqs */
957 if (desc->force_disable)
958 intc_enable_disable_enum(desc, d, desc->force_disable, 0);
Magnus Dammd5190952010-02-09 04:29:22 +0000959
960 /* disable bits matching force_enable before registering irqs */
961 if (desc->force_enable)
962 intc_enable_disable_enum(desc, d, desc->force_enable, 0);
963
Magnus Dammd58876e2008-04-24 21:36:34 +0900964 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
965
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900966 /* register the vectors one by one */
Magnus Damm577cd752010-02-09 04:24:46 +0000967 for (i = 0; i < hw->nr_vectors; i++) {
968 struct intc_vect *vect = hw->vectors + i;
Paul Mundt05ff3002009-05-22 01:28:33 +0900969 unsigned int irq = evt2irq(vect->vect);
970 struct irq_desc *irq_desc;
Paul Mundt54ff3282009-06-11 10:33:09 +0300971
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900972 if (!vect->enum_id)
973 continue;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900974
Paul Mundt54ff3282009-06-11 10:33:09 +0300975 irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
Paul Mundt05ff3002009-05-22 01:28:33 +0900976 if (unlikely(!irq_desc)) {
Paul Mundt1279b7f2009-08-31 15:15:33 +0900977 pr_info("can't get irq_desc for %d\n", irq);
Paul Mundt05ff3002009-05-22 01:28:33 +0900978 continue;
979 }
980
981 intc_register_irq(desc, d, vect->enum_id, irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900982
Magnus Damm577cd752010-02-09 04:24:46 +0000983 for (k = i + 1; k < hw->nr_vectors; k++) {
984 struct intc_vect *vect2 = hw->vectors + k;
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900985 unsigned int irq2 = evt2irq(vect2->vect);
986
987 if (vect->enum_id != vect2->enum_id)
988 continue;
989
Paul Mundt1279b7f2009-08-31 15:15:33 +0900990 /*
991 * In the case of multi-evt handling and sparse
992 * IRQ support, each vector still needs to have
993 * its own backing irq_desc.
994 */
995 irq_desc = irq_to_desc_alloc_node(irq2, numa_node_id());
996 if (unlikely(!irq_desc)) {
997 pr_info("can't get irq_desc for %d\n", irq2);
998 continue;
999 }
1000
Pawel Moll05ecd5a2009-08-24 19:52:38 +09001001 vect2->enum_id = 0;
1002
1003 /* redirect this interrupts to the first one */
Paul Mundt4d2185d2010-02-17 12:37:42 +09001004 set_irq_chip(irq2, &dummy_irq_chip);
Magnus Damme6f07752010-02-09 07:17:20 +00001005 set_irq_chained_handler(irq2, intc_redirect_irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +09001006 set_irq_data(irq2, (void *)irq);
1007 }
Magnus Damm02ab3f72007-07-18 17:25:09 +09001008 }
Magnus Dammd5190952010-02-09 04:29:22 +00001009
1010 /* enable bits matching force_enable after registering irqs */
1011 if (desc->force_enable)
1012 intc_enable_disable_enum(desc, d, desc->force_enable, 1);
Magnus Damm01e96512010-03-10 09:31:01 +00001013
1014 return 0;
Magnus Dammdec710b2010-03-19 16:48:01 +09001015err5:
Magnus Damm01e96512010-03-10 09:31:01 +00001016 kfree(d->prio);
Magnus Dammdec710b2010-03-19 16:48:01 +09001017err4:
Magnus Damm01e96512010-03-10 09:31:01 +00001018#ifdef CONFIG_SMP
1019 kfree(d->smp);
Magnus Dammdec710b2010-03-19 16:48:01 +09001020err3:
Magnus Damm01e96512010-03-10 09:31:01 +00001021#endif
1022 kfree(d->reg);
Magnus Dammdec710b2010-03-19 16:48:01 +09001023err2:
1024 for (k = 0; k < d->nr_windows; k++)
1025 if (d->window[k].virt)
1026 iounmap(d->window[k].virt);
1027
1028 kfree(d->window);
1029err1:
Magnus Damm01e96512010-03-10 09:31:01 +00001030 kfree(d);
Magnus Dammdec710b2010-03-19 16:48:01 +09001031err0:
Magnus Damm01e96512010-03-10 09:31:01 +00001032 pr_err("unable to allocate INTC memory\n");
1033
1034 return -ENOMEM;
Magnus Damm02ab3f72007-07-18 17:25:09 +09001035}
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001036
Paul Mundt0ded7542010-04-13 10:16:34 +09001037static ssize_t
1038show_intc_name(struct sys_device *dev, struct sysdev_attribute *attr, char *buf)
1039{
1040 struct intc_desc_int *d;
1041
1042 d = container_of(dev, struct intc_desc_int, sysdev);
1043
1044 return sprintf(buf, "%s\n", d->chip.name);
1045}
1046
1047static SYSDEV_ATTR(name, S_IRUGO, show_intc_name, NULL);
1048
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001049static int intc_suspend(struct sys_device *dev, pm_message_t state)
1050{
1051 struct intc_desc_int *d;
1052 struct irq_desc *desc;
1053 int irq;
1054
1055 /* get intc controller associated with this sysdev */
1056 d = container_of(dev, struct intc_desc_int, sysdev);
1057
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001058 switch (state.event) {
1059 case PM_EVENT_ON:
1060 if (d->state.event != PM_EVENT_FREEZE)
1061 break;
1062 for_each_irq_desc(irq, desc) {
Francesco VIRLINZI87a705d2009-12-04 08:57:58 +00001063 if (desc->handle_irq == intc_redirect_irq)
Paul Mundt0a753d52009-12-09 14:36:16 +09001064 continue;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001065 if (desc->chip != &d->chip)
1066 continue;
1067 if (desc->status & IRQ_DISABLED)
1068 intc_disable(irq);
1069 else
1070 intc_enable(irq);
1071 }
1072 break;
1073 case PM_EVENT_FREEZE:
1074 /* nothing has to be done */
1075 break;
1076 case PM_EVENT_SUSPEND:
1077 /* enable wakeup irqs belonging to this intc controller */
1078 for_each_irq_desc(irq, desc) {
1079 if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
1080 intc_enable(irq);
1081 }
1082 break;
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001083 }
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001084 d->state = state;
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001085
1086 return 0;
1087}
1088
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001089static int intc_resume(struct sys_device *dev)
1090{
1091 return intc_suspend(dev, PMSG_ON);
1092}
1093
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001094static struct sysdev_class intc_sysdev_class = {
1095 .name = "intc",
1096 .suspend = intc_suspend,
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001097 .resume = intc_resume,
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001098};
1099
1100/* register this intc as sysdev to allow suspend/resume */
1101static int __init register_intc_sysdevs(void)
1102{
1103 struct intc_desc_int *d;
1104 int error;
1105 int id = 0;
1106
1107 error = sysdev_class_register(&intc_sysdev_class);
1108 if (!error) {
1109 list_for_each_entry(d, &intc_list, list) {
1110 d->sysdev.id = id;
1111 d->sysdev.cls = &intc_sysdev_class;
1112 error = sysdev_register(&d->sysdev);
Paul Mundt0ded7542010-04-13 10:16:34 +09001113 if (error == 0)
1114 error = sysdev_create_file(&d->sysdev,
1115 &attr_name);
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001116 if (error)
1117 break;
Paul Mundt0ded7542010-04-13 10:16:34 +09001118
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001119 id++;
1120 }
1121 }
1122
1123 if (error)
1124 pr_warning("intc: sysdev registration error\n");
1125
1126 return error;
1127}
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001128device_initcall(register_intc_sysdevs);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001129
1130/*
1131 * Dynamic IRQ allocation and deallocation
1132 */
Paul Mundte9867c52010-02-02 17:35:13 +09001133unsigned int create_irq_nr(unsigned int irq_want, int node)
Paul Mundt1ce7b032009-11-02 10:30:26 +09001134{
1135 unsigned int irq = 0, new;
1136 unsigned long flags;
1137 struct irq_desc *desc;
1138
1139 spin_lock_irqsave(&vector_lock, flags);
1140
1141 /*
Paul Mundte9867c52010-02-02 17:35:13 +09001142 * First try the wanted IRQ
Paul Mundt1ce7b032009-11-02 10:30:26 +09001143 */
Paul Mundte9867c52010-02-02 17:35:13 +09001144 if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
1145 new = irq_want;
1146 } else {
1147 /* .. then fall back to scanning. */
Paul Mundt1ce7b032009-11-02 10:30:26 +09001148 new = find_first_zero_bit(intc_irq_map, nr_irqs);
1149 if (unlikely(new == nr_irqs))
1150 goto out_unlock;
1151
Paul Mundt1ce7b032009-11-02 10:30:26 +09001152 __set_bit(new, intc_irq_map);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001153 }
1154
Paul Mundte9867c52010-02-02 17:35:13 +09001155 desc = irq_to_desc_alloc_node(new, node);
1156 if (unlikely(!desc)) {
1157 pr_info("can't get irq_desc for %d\n", new);
1158 goto out_unlock;
1159 }
1160
1161 desc = move_irq_desc(desc, node);
1162 irq = new;
1163
Paul Mundt1ce7b032009-11-02 10:30:26 +09001164out_unlock:
1165 spin_unlock_irqrestore(&vector_lock, flags);
1166
Magnus Damm65a5b282010-02-05 11:15:25 +00001167 if (irq > 0) {
Paul Mundt1ce7b032009-11-02 10:30:26 +09001168 dynamic_irq_init(irq);
Magnus Damm65a5b282010-02-05 11:15:25 +00001169#ifdef CONFIG_ARM
1170 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
1171#endif
1172 }
Paul Mundt1ce7b032009-11-02 10:30:26 +09001173
1174 return irq;
1175}
1176
1177int create_irq(void)
1178{
1179 int nid = cpu_to_node(smp_processor_id());
1180 int irq;
1181
Paul Mundte9867c52010-02-02 17:35:13 +09001182 irq = create_irq_nr(NR_IRQS_LEGACY, nid);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001183 if (irq == 0)
1184 irq = -1;
1185
1186 return irq;
1187}
1188
1189void destroy_irq(unsigned int irq)
1190{
1191 unsigned long flags;
1192
1193 dynamic_irq_cleanup(irq);
1194
1195 spin_lock_irqsave(&vector_lock, flags);
1196 __clear_bit(irq, intc_irq_map);
1197 spin_unlock_irqrestore(&vector_lock, flags);
1198}
Paul Mundt45b9dea2009-11-02 15:43:20 +09001199
1200int reserve_irq_vector(unsigned int irq)
1201{
1202 unsigned long flags;
1203 int ret = 0;
1204
1205 spin_lock_irqsave(&vector_lock, flags);
1206 if (test_and_set_bit(irq, intc_irq_map))
1207 ret = -EBUSY;
1208 spin_unlock_irqrestore(&vector_lock, flags);
1209
1210 return ret;
1211}
1212
1213void reserve_irq_legacy(void)
1214{
1215 unsigned long flags;
1216 int i, j;
1217
1218 spin_lock_irqsave(&vector_lock, flags);
1219 j = find_first_bit(intc_irq_map, nr_irqs);
1220 for (i = 0; i < j; i++)
1221 __set_bit(i, intc_irq_map);
1222 spin_unlock_irqrestore(&vector_lock, flags);
1223}