blob: 3a5a17db94744d73d1086b0bc3e89ef6bdb62e52 [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 Mundt1ce7b032009-11-02 10:30:26 +09005 * Copyright (C) 2009 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>
Magnus Damm02ab3f72007-07-18 17:25:09 +090029
Magnus Damm73505b42007-08-12 15:26:12 +090030#define _INTC_MK(fn, mode, addr_e, addr_d, width, shift) \
31 ((shift) | ((width) << 5) | ((fn) << 9) | ((mode) << 13) | \
32 ((addr_e) << 16) | ((addr_d << 24)))
Magnus Damm02ab3f72007-07-18 17:25:09 +090033
Magnus Damm73505b42007-08-12 15:26:12 +090034#define _INTC_SHIFT(h) (h & 0x1f)
35#define _INTC_WIDTH(h) ((h >> 5) & 0xf)
36#define _INTC_FN(h) ((h >> 9) & 0xf)
37#define _INTC_MODE(h) ((h >> 13) & 0x7)
38#define _INTC_ADDR_E(h) ((h >> 16) & 0xff)
39#define _INTC_ADDR_D(h) ((h >> 24) & 0xff)
Magnus Damm02ab3f72007-07-18 17:25:09 +090040
Magnus Damm73505b42007-08-12 15:26:12 +090041struct intc_handle_int {
42 unsigned int irq;
43 unsigned long handle;
44};
45
46struct intc_desc_int {
Magnus Damm2dcec7a2009-04-01 14:30:59 +000047 struct list_head list;
48 struct sys_device sysdev;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +000049 pm_message_t state;
Magnus Damm73505b42007-08-12 15:26:12 +090050 unsigned long *reg;
Magnus Dammf18d5332007-09-21 18:16:42 +090051#ifdef CONFIG_SMP
52 unsigned long *smp;
53#endif
Magnus Damm73505b42007-08-12 15:26:12 +090054 unsigned int nr_reg;
55 struct intc_handle_int *prio;
56 unsigned int nr_prio;
57 struct intc_handle_int *sense;
58 unsigned int nr_sense;
59 struct irq_chip chip;
60};
61
Magnus Damm2dcec7a2009-04-01 14:30:59 +000062static LIST_HEAD(intc_list);
63
Paul Mundt1ce7b032009-11-02 10:30:26 +090064/*
65 * The intc_irq_map provides a global map of bound IRQ vectors for a
66 * given platform. Allocation of IRQs are either static through the CPU
67 * vector map, or dynamic in the case of board mux vectors or MSI.
68 *
69 * As this is a central point for all IRQ controllers on the system,
70 * each of the available sources are mapped out here. This combined with
71 * sparseirq makes it quite trivial to keep the vector map tightly packed
72 * when dynamically creating IRQs, as well as tying in to otherwise
73 * unused irq_desc positions in the sparse array.
74 */
75static DECLARE_BITMAP(intc_irq_map, NR_IRQS);
76static DEFINE_SPINLOCK(vector_lock);
77
Magnus Dammf18d5332007-09-21 18:16:42 +090078#ifdef CONFIG_SMP
79#define IS_SMP(x) x.smp
80#define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
81#define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
82#else
83#define IS_SMP(x) 0
84#define INTC_REG(d, x, c) (d->reg[(x)])
85#define SMP_NR(d, x) 1
86#endif
87
Magnus Damm73505b42007-08-12 15:26:12 +090088static unsigned int intc_prio_level[NR_IRQS]; /* for now */
Magnus Dammd58876e2008-04-24 21:36:34 +090089static unsigned long ack_handle[NR_IRQS];
Magnus Damm73505b42007-08-12 15:26:12 +090090
91static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +090092{
93 struct irq_chip *chip = get_irq_chip(irq);
Stuart Menefy6000fc42009-08-24 18:27:33 +090094 return container_of(chip, struct intc_desc_int, chip);
Magnus Damm02ab3f72007-07-18 17:25:09 +090095}
96
97static inline unsigned int set_field(unsigned int value,
98 unsigned int field_value,
Magnus Damm73505b42007-08-12 15:26:12 +090099 unsigned int handle)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900100{
Magnus Damm73505b42007-08-12 15:26:12 +0900101 unsigned int width = _INTC_WIDTH(handle);
102 unsigned int shift = _INTC_SHIFT(handle);
103
Magnus Damm02ab3f72007-07-18 17:25:09 +0900104 value &= ~(((1 << width) - 1) << shift);
105 value |= field_value << shift;
106 return value;
107}
108
Magnus Damm73505b42007-08-12 15:26:12 +0900109static void write_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900110{
Paul Mundt62429e02008-10-01 15:19:10 +0900111 __raw_writeb(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900112 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900113}
114
Magnus Damm73505b42007-08-12 15:26:12 +0900115static void write_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900116{
Paul Mundt62429e02008-10-01 15:19:10 +0900117 __raw_writew(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900118 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900119}
120
Magnus Damm73505b42007-08-12 15:26:12 +0900121static void write_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900122{
Paul Mundt62429e02008-10-01 15:19:10 +0900123 __raw_writel(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900124 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900125}
126
Magnus Damm73505b42007-08-12 15:26:12 +0900127static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900128{
Magnus Damm4370fe12008-04-24 21:53:07 +0900129 unsigned long flags;
130 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900131 __raw_writeb(set_field(__raw_readb(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900132 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900133 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900134}
135
Magnus Damm73505b42007-08-12 15:26:12 +0900136static void modify_16(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_writew(set_field(__raw_readw(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900141 (void)__raw_readw(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_32(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_writel(set_field(__raw_readl(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900150 (void)__raw_readl(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 +0900154enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
Magnus Damm02ab3f72007-07-18 17:25:09 +0900155
Magnus Damm73505b42007-08-12 15:26:12 +0900156static void (*intc_reg_fns[])(unsigned long addr,
157 unsigned long h,
158 unsigned long data) = {
159 [REG_FN_WRITE_BASE + 0] = write_8,
160 [REG_FN_WRITE_BASE + 1] = write_16,
161 [REG_FN_WRITE_BASE + 3] = write_32,
162 [REG_FN_MODIFY_BASE + 0] = modify_8,
163 [REG_FN_MODIFY_BASE + 1] = modify_16,
164 [REG_FN_MODIFY_BASE + 3] = modify_32,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900165};
166
Magnus Damm73505b42007-08-12 15:26:12 +0900167enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
168 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
169 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
170 MODE_PRIO_REG, /* Priority value written to enable interrupt */
171 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
172};
173
174static void intc_mode_field(unsigned long addr,
175 unsigned long handle,
176 void (*fn)(unsigned long,
177 unsigned long,
178 unsigned long),
179 unsigned int irq)
180{
181 fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
182}
183
184static void intc_mode_zero(unsigned long addr,
185 unsigned long handle,
186 void (*fn)(unsigned long,
187 unsigned long,
188 unsigned long),
189 unsigned int irq)
190{
191 fn(addr, handle, 0);
192}
193
194static void intc_mode_prio(unsigned long addr,
195 unsigned long handle,
196 void (*fn)(unsigned long,
197 unsigned long,
198 unsigned long),
199 unsigned int irq)
200{
201 fn(addr, handle, intc_prio_level[irq]);
202}
203
204static void (*intc_enable_fns[])(unsigned long addr,
205 unsigned long handle,
206 void (*fn)(unsigned long,
207 unsigned long,
208 unsigned long),
209 unsigned int irq) = {
210 [MODE_ENABLE_REG] = intc_mode_field,
211 [MODE_MASK_REG] = intc_mode_zero,
212 [MODE_DUAL_REG] = intc_mode_field,
213 [MODE_PRIO_REG] = intc_mode_prio,
214 [MODE_PCLR_REG] = intc_mode_prio,
215};
216
217static void (*intc_disable_fns[])(unsigned long addr,
218 unsigned long handle,
219 void (*fn)(unsigned long,
220 unsigned long,
221 unsigned long),
222 unsigned int irq) = {
223 [MODE_ENABLE_REG] = intc_mode_zero,
224 [MODE_MASK_REG] = intc_mode_field,
225 [MODE_DUAL_REG] = intc_mode_field,
226 [MODE_PRIO_REG] = intc_mode_zero,
227 [MODE_PCLR_REG] = intc_mode_field,
228};
229
230static inline void _intc_enable(unsigned int irq, unsigned long handle)
231{
232 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900233 unsigned long addr;
234 unsigned int cpu;
Magnus Damm73505b42007-08-12 15:26:12 +0900235
Magnus Dammf18d5332007-09-21 18:16:42 +0900236 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
237 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
238 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
239 [_INTC_FN(handle)], irq);
240 }
Magnus Damm73505b42007-08-12 15:26:12 +0900241}
242
Magnus Damm02ab3f72007-07-18 17:25:09 +0900243static void intc_enable(unsigned int irq)
244{
Magnus Damm73505b42007-08-12 15:26:12 +0900245 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
Magnus Damm02ab3f72007-07-18 17:25:09 +0900246}
247
248static void intc_disable(unsigned int irq)
249{
Magnus Dammf18d5332007-09-21 18:16:42 +0900250 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900251 unsigned long handle = (unsigned long) get_irq_chip_data(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900252 unsigned long addr;
253 unsigned int cpu;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900254
Magnus Dammf18d5332007-09-21 18:16:42 +0900255 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
256 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
257 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
258 [_INTC_FN(handle)], irq);
259 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900260}
261
Magnus Dammd5190952010-02-09 04:29:22 +0000262static void (*intc_enable_noprio_fns[])(unsigned long addr,
263 unsigned long handle,
264 void (*fn)(unsigned long,
265 unsigned long,
266 unsigned long),
267 unsigned int irq) = {
268 [MODE_ENABLE_REG] = intc_mode_field,
269 [MODE_MASK_REG] = intc_mode_zero,
270 [MODE_DUAL_REG] = intc_mode_field,
271 [MODE_PRIO_REG] = intc_mode_field,
272 [MODE_PCLR_REG] = intc_mode_field,
273};
274
275static void intc_enable_disable(struct intc_desc_int *d,
276 unsigned long handle, int do_enable)
277{
278 unsigned long addr;
279 unsigned int cpu;
280 void (*fn)(unsigned long, unsigned long,
281 void (*)(unsigned long, unsigned long, unsigned long),
282 unsigned int);
283
284 if (do_enable) {
285 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
286 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
287 fn = intc_enable_noprio_fns[_INTC_MODE(handle)];
288 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
289 }
290 } else {
291 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
292 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
293 fn = intc_disable_fns[_INTC_MODE(handle)];
294 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
295 }
296 }
297}
298
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000299static int intc_set_wake(unsigned int irq, unsigned int on)
300{
301 return 0; /* allow wakeup, but setup hardware in intc_suspend() */
302}
303
Magnus Dammd58876e2008-04-24 21:36:34 +0900304static void intc_mask_ack(unsigned int irq)
305{
306 struct intc_desc_int *d = get_intc_desc(irq);
307 unsigned long handle = ack_handle[irq];
308 unsigned long addr;
309
310 intc_disable(irq);
311
312 /* read register and write zero only to the assocaited bit */
313
314 if (handle) {
315 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900316 switch (_INTC_FN(handle)) {
317 case REG_FN_MODIFY_BASE + 0: /* 8bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900318 __raw_readb(addr);
319 __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900320 break;
321 case REG_FN_MODIFY_BASE + 1: /* 16bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900322 __raw_readw(addr);
323 __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900324 break;
325 case REG_FN_MODIFY_BASE + 3: /* 32bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900326 __raw_readl(addr);
327 __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900328 break;
329 default:
330 BUG();
331 break;
332 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900333 }
334}
Magnus Dammd58876e2008-04-24 21:36:34 +0900335
Magnus Damm73505b42007-08-12 15:26:12 +0900336static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
337 unsigned int nr_hp,
338 unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900339{
Magnus Damm73505b42007-08-12 15:26:12 +0900340 int i;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900341
Magnus Damm3d37d942007-08-17 00:50:44 +0900342 /* this doesn't scale well, but...
343 *
344 * this function should only be used for cerain uncommon
345 * operations such as intc_set_priority() and intc_set_sense()
346 * and in those rare cases performance doesn't matter that much.
347 * keeping the memory footprint low is more important.
348 *
349 * one rather simple way to speed this up and still keep the
350 * memory footprint down is to make sure the array is sorted
351 * and then perform a bisect to lookup the irq.
352 */
353
Magnus Damm73505b42007-08-12 15:26:12 +0900354 for (i = 0; i < nr_hp; i++) {
355 if ((hp + i)->irq != irq)
356 continue;
357
358 return hp + i;
359 }
360
361 return NULL;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900362}
363
Magnus Damm73505b42007-08-12 15:26:12 +0900364int intc_set_priority(unsigned int irq, unsigned int prio)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900365{
Magnus Damm73505b42007-08-12 15:26:12 +0900366 struct intc_desc_int *d = get_intc_desc(irq);
367 struct intc_handle_int *ihp;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900368
Magnus Damm73505b42007-08-12 15:26:12 +0900369 if (!intc_prio_level[irq] || prio <= 1)
370 return -EINVAL;
371
372 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
373 if (ihp) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900374 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
Magnus Damm73505b42007-08-12 15:26:12 +0900375 return -EINVAL;
376
377 intc_prio_level[irq] = prio;
378
379 /*
380 * only set secondary masking method directly
381 * primary masking method is using intc_prio_level[irq]
382 * priority level will be set during next enable()
383 */
384
Magnus Damm3d37d942007-08-17 00:50:44 +0900385 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
Magnus Damm73505b42007-08-12 15:26:12 +0900386 _intc_enable(irq, ihp->handle);
387 }
388 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900389}
390
391#define VALID(x) (x | 0x80)
392
393static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
394 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
395 [IRQ_TYPE_EDGE_RISING] = VALID(1),
396 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
Magnus Damm720be992008-04-24 21:47:15 +0900397 /* SH7706, SH7707 and SH7709 do not support high level triggered */
398#if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
399 !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
400 !defined(CONFIG_CPU_SUBTYPE_SH7709)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900401 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
Magnus Damm720be992008-04-24 21:47:15 +0900402#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900403};
404
405static int intc_set_sense(unsigned int irq, unsigned int type)
406{
Magnus Damm73505b42007-08-12 15:26:12 +0900407 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900408 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
Magnus Damm73505b42007-08-12 15:26:12 +0900409 struct intc_handle_int *ihp;
410 unsigned long addr;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900411
Magnus Damm73505b42007-08-12 15:26:12 +0900412 if (!value)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900413 return -EINVAL;
414
Magnus Damm73505b42007-08-12 15:26:12 +0900415 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
416 if (ihp) {
Magnus Dammf18d5332007-09-21 18:16:42 +0900417 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900418 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900419 }
Magnus Damm73505b42007-08-12 15:26:12 +0900420 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900421}
422
Magnus Damm73505b42007-08-12 15:26:12 +0900423static unsigned int __init intc_get_reg(struct intc_desc_int *d,
424 unsigned long address)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900425{
Magnus Damm73505b42007-08-12 15:26:12 +0900426 unsigned int k;
427
428 for (k = 0; k < d->nr_reg; k++) {
429 if (d->reg[k] == address)
430 return k;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900431 }
432
433 BUG();
Magnus Damm73505b42007-08-12 15:26:12 +0900434 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900435}
436
Magnus Damm73505b42007-08-12 15:26:12 +0900437static intc_enum __init intc_grp_id(struct intc_desc *desc,
438 intc_enum enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900439{
Magnus Damm577cd752010-02-09 04:24:46 +0000440 struct intc_group *g = desc->hw.groups;
Magnus Damm680c4592007-07-20 12:09:29 +0900441 unsigned int i, j;
442
Magnus Damm577cd752010-02-09 04:24:46 +0000443 for (i = 0; g && enum_id && i < desc->hw.nr_groups; i++) {
444 g = desc->hw.groups + i;
Magnus Damm680c4592007-07-20 12:09:29 +0900445
446 for (j = 0; g->enum_ids[j]; j++) {
447 if (g->enum_ids[j] != enum_id)
448 continue;
449
450 return g->enum_id;
451 }
452 }
453
454 return 0;
455}
456
Magnus Dammd5190952010-02-09 04:29:22 +0000457static unsigned int __init _intc_mask_data(struct intc_desc *desc,
458 struct intc_desc_int *d,
459 intc_enum enum_id,
460 unsigned int *reg_idx,
461 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900462{
Magnus Damm577cd752010-02-09 04:24:46 +0000463 struct intc_mask_reg *mr = desc->hw.mask_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000464 unsigned int fn, mode;
Magnus Damm73505b42007-08-12 15:26:12 +0900465 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900466
Magnus Dammd5190952010-02-09 04:29:22 +0000467 while (mr && enum_id && *reg_idx < desc->hw.nr_mask_regs) {
468 mr = desc->hw.mask_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900469
Magnus Dammd5190952010-02-09 04:29:22 +0000470 for (; *fld_idx < ARRAY_SIZE(mr->enum_ids); (*fld_idx)++) {
471 if (mr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900472 continue;
473
Magnus Damm73505b42007-08-12 15:26:12 +0900474 if (mr->set_reg && mr->clr_reg) {
475 fn = REG_FN_WRITE_BASE;
476 mode = MODE_DUAL_REG;
477 reg_e = mr->clr_reg;
478 reg_d = mr->set_reg;
479 } else {
480 fn = REG_FN_MODIFY_BASE;
481 if (mr->set_reg) {
482 mode = MODE_ENABLE_REG;
483 reg_e = mr->set_reg;
484 reg_d = mr->set_reg;
485 } else {
486 mode = MODE_MASK_REG;
487 reg_e = mr->clr_reg;
488 reg_d = mr->clr_reg;
489 }
Magnus Damm51da6422007-08-03 14:25:32 +0900490 }
491
Magnus Damm73505b42007-08-12 15:26:12 +0900492 fn += (mr->reg_width >> 3) - 1;
493 return _INTC_MK(fn, mode,
494 intc_get_reg(d, reg_e),
495 intc_get_reg(d, reg_d),
496 1,
Magnus Dammd5190952010-02-09 04:29:22 +0000497 (mr->reg_width - 1) - *fld_idx);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900498 }
Magnus Dammd5190952010-02-09 04:29:22 +0000499
500 *fld_idx = 0;
501 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900502 }
503
Magnus Dammd5190952010-02-09 04:29:22 +0000504 return 0;
505}
506
507static unsigned int __init intc_mask_data(struct intc_desc *desc,
508 struct intc_desc_int *d,
509 intc_enum enum_id, int do_grps)
510{
511 unsigned int i = 0;
512 unsigned int j = 0;
513 unsigned int ret;
514
515 ret = _intc_mask_data(desc, d, enum_id, &i, &j);
516 if (ret)
517 return ret;
518
Magnus Damm680c4592007-07-20 12:09:29 +0900519 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900520 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900521
Magnus Damm02ab3f72007-07-18 17:25:09 +0900522 return 0;
523}
524
Magnus Dammd5190952010-02-09 04:29:22 +0000525static unsigned int __init _intc_prio_data(struct intc_desc *desc,
526 struct intc_desc_int *d,
527 intc_enum enum_id,
528 unsigned int *reg_idx,
529 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900530{
Magnus Damm577cd752010-02-09 04:24:46 +0000531 struct intc_prio_reg *pr = desc->hw.prio_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000532 unsigned int fn, n, mode, bit;
Magnus Damm73505b42007-08-12 15:26:12 +0900533 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900534
Magnus Dammd5190952010-02-09 04:29:22 +0000535 while (pr && enum_id && *reg_idx < desc->hw.nr_prio_regs) {
536 pr = desc->hw.prio_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900537
Magnus Dammd5190952010-02-09 04:29:22 +0000538 for (; *fld_idx < ARRAY_SIZE(pr->enum_ids); (*fld_idx)++) {
539 if (pr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900540 continue;
541
Magnus Damm73505b42007-08-12 15:26:12 +0900542 if (pr->set_reg && pr->clr_reg) {
543 fn = REG_FN_WRITE_BASE;
544 mode = MODE_PCLR_REG;
545 reg_e = pr->set_reg;
546 reg_d = pr->clr_reg;
547 } else {
548 fn = REG_FN_MODIFY_BASE;
549 mode = MODE_PRIO_REG;
550 if (!pr->set_reg)
551 BUG();
552 reg_e = pr->set_reg;
553 reg_d = pr->set_reg;
554 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900555
Magnus Damm73505b42007-08-12 15:26:12 +0900556 fn += (pr->reg_width >> 3) - 1;
Magnus Dammd5190952010-02-09 04:29:22 +0000557 n = *fld_idx + 1;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900558
Magnus Dammd5190952010-02-09 04:29:22 +0000559 BUG_ON(n * pr->field_width > pr->reg_width);
roel kluinb21a9102008-09-09 23:02:43 +0200560
Magnus Dammd5190952010-02-09 04:29:22 +0000561 bit = pr->reg_width - (n * pr->field_width);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900562
Magnus Damm73505b42007-08-12 15:26:12 +0900563 return _INTC_MK(fn, mode,
564 intc_get_reg(d, reg_e),
565 intc_get_reg(d, reg_d),
566 pr->field_width, bit);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900567 }
Magnus Dammd5190952010-02-09 04:29:22 +0000568
569 *fld_idx = 0;
570 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900571 }
572
Magnus Dammd5190952010-02-09 04:29:22 +0000573 return 0;
574}
575
576static unsigned int __init intc_prio_data(struct intc_desc *desc,
577 struct intc_desc_int *d,
578 intc_enum enum_id, int do_grps)
579{
580 unsigned int i = 0;
581 unsigned int j = 0;
582 unsigned int ret;
583
584 ret = _intc_prio_data(desc, d, enum_id, &i, &j);
585 if (ret)
586 return ret;
587
Magnus Damm680c4592007-07-20 12:09:29 +0900588 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900589 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900590
Magnus Damm02ab3f72007-07-18 17:25:09 +0900591 return 0;
592}
593
Magnus Dammd5190952010-02-09 04:29:22 +0000594static void __init intc_enable_disable_enum(struct intc_desc *desc,
595 struct intc_desc_int *d,
596 intc_enum enum_id, int enable)
597{
598 unsigned int i, j, data;
599
600 /* go through and enable/disable all mask bits */
601 i = j = 0;
602 do {
603 data = _intc_mask_data(desc, d, enum_id, &i, &j);
604 if (data)
605 intc_enable_disable(d, data, enable);
606 j++;
607 } while (data);
608
609 /* go through and enable/disable all priority fields */
610 i = j = 0;
611 do {
612 data = _intc_prio_data(desc, d, enum_id, &i, &j);
613 if (data)
614 intc_enable_disable(d, data, enable);
615
616 j++;
617 } while (data);
618}
619
Magnus Dammd58876e2008-04-24 21:36:34 +0900620static unsigned int __init intc_ack_data(struct intc_desc *desc,
621 struct intc_desc_int *d,
622 intc_enum enum_id)
623{
Magnus Damm577cd752010-02-09 04:24:46 +0000624 struct intc_mask_reg *mr = desc->hw.ack_regs;
Magnus Dammd58876e2008-04-24 21:36:34 +0900625 unsigned int i, j, fn, mode;
626 unsigned long reg_e, reg_d;
627
Magnus Damm577cd752010-02-09 04:24:46 +0000628 for (i = 0; mr && enum_id && i < desc->hw.nr_ack_regs; i++) {
629 mr = desc->hw.ack_regs + i;
Magnus Dammd58876e2008-04-24 21:36:34 +0900630
631 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
632 if (mr->enum_ids[j] != enum_id)
633 continue;
634
635 fn = REG_FN_MODIFY_BASE;
636 mode = MODE_ENABLE_REG;
637 reg_e = mr->set_reg;
638 reg_d = mr->set_reg;
639
640 fn += (mr->reg_width >> 3) - 1;
641 return _INTC_MK(fn, mode,
642 intc_get_reg(d, reg_e),
643 intc_get_reg(d, reg_d),
644 1,
645 (mr->reg_width - 1) - j);
646 }
647 }
648
649 return 0;
650}
Magnus Dammd58876e2008-04-24 21:36:34 +0900651
Magnus Damm73505b42007-08-12 15:26:12 +0900652static unsigned int __init intc_sense_data(struct intc_desc *desc,
653 struct intc_desc_int *d,
654 intc_enum enum_id)
655{
Magnus Damm577cd752010-02-09 04:24:46 +0000656 struct intc_sense_reg *sr = desc->hw.sense_regs;
Magnus Damm73505b42007-08-12 15:26:12 +0900657 unsigned int i, j, fn, bit;
658
Magnus Damm577cd752010-02-09 04:24:46 +0000659 for (i = 0; sr && enum_id && i < desc->hw.nr_sense_regs; i++) {
660 sr = desc->hw.sense_regs + i;
Magnus Damm73505b42007-08-12 15:26:12 +0900661
662 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
663 if (sr->enum_ids[j] != enum_id)
664 continue;
665
666 fn = REG_FN_MODIFY_BASE;
667 fn += (sr->reg_width >> 3) - 1;
Magnus Damm73505b42007-08-12 15:26:12 +0900668
roel kluinb21a9102008-09-09 23:02:43 +0200669 BUG_ON((j + 1) * sr->field_width > sr->reg_width);
670
671 bit = sr->reg_width - ((j + 1) * sr->field_width);
Magnus Damm73505b42007-08-12 15:26:12 +0900672
673 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
674 0, sr->field_width, bit);
675 }
676 }
677
678 return 0;
679}
680
681static void __init intc_register_irq(struct intc_desc *desc,
682 struct intc_desc_int *d,
683 intc_enum enum_id,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900684 unsigned int irq)
685{
Magnus Damm3d37d942007-08-17 00:50:44 +0900686 struct intc_handle_int *hp;
Magnus Damm680c4592007-07-20 12:09:29 +0900687 unsigned int data[2], primary;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900688
Paul Mundt1ce7b032009-11-02 10:30:26 +0900689 /*
690 * Register the IRQ position with the global IRQ map
691 */
692 set_bit(irq, intc_irq_map);
693
Magnus Damm680c4592007-07-20 12:09:29 +0900694 /* Prefer single interrupt source bitmap over other combinations:
695 * 1. bitmap, single interrupt source
696 * 2. priority, single interrupt source
697 * 3. bitmap, multiple interrupt sources (groups)
698 * 4. priority, multiple interrupt sources (groups)
699 */
700
Magnus Damm73505b42007-08-12 15:26:12 +0900701 data[0] = intc_mask_data(desc, d, enum_id, 0);
702 data[1] = intc_prio_data(desc, d, enum_id, 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900703
704 primary = 0;
705 if (!data[0] && data[1])
706 primary = 1;
707
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900708 if (!data[0] && !data[1])
Paul Mundtf0335992009-03-06 17:56:58 +0900709 pr_warning("intc: missing unique irq mask for "
710 "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900711
Magnus Damm73505b42007-08-12 15:26:12 +0900712 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
713 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
Magnus Damm680c4592007-07-20 12:09:29 +0900714
715 if (!data[primary])
716 primary ^= 1;
717
718 BUG_ON(!data[primary]); /* must have primary masking method */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900719
720 disable_irq_nosync(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900721 set_irq_chip_and_handler_name(irq, &d->chip,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900722 handle_level_irq, "level");
Magnus Damm680c4592007-07-20 12:09:29 +0900723 set_irq_chip_data(irq, (void *)data[primary]);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900724
Magnus Damm7f3edee2008-01-10 14:08:55 +0900725 /* set priority level
726 * - this needs to be at least 2 for 5-bit priorities on 7780
727 */
728 intc_prio_level[irq] = 2;
Magnus Damm73505b42007-08-12 15:26:12 +0900729
Magnus Damm680c4592007-07-20 12:09:29 +0900730 /* enable secondary masking method if present */
731 if (data[!primary])
Magnus Damm73505b42007-08-12 15:26:12 +0900732 _intc_enable(irq, data[!primary]);
733
734 /* add irq to d->prio list if priority is available */
735 if (data[1]) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900736 hp = d->prio + d->nr_prio;
737 hp->irq = irq;
738 hp->handle = data[1];
739
740 if (primary) {
741 /*
742 * only secondary priority should access registers, so
743 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
744 */
745
746 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
747 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
748 }
Magnus Damm73505b42007-08-12 15:26:12 +0900749 d->nr_prio++;
750 }
751
752 /* add irq to d->sense list if sense is available */
753 data[0] = intc_sense_data(desc, d, enum_id);
754 if (data[0]) {
755 (d->sense + d->nr_sense)->irq = irq;
756 (d->sense + d->nr_sense)->handle = data[0];
757 d->nr_sense++;
758 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900759
760 /* irq should be disabled by default */
Magnus Damm73505b42007-08-12 15:26:12 +0900761 d->chip.mask(irq);
Magnus Dammd58876e2008-04-24 21:36:34 +0900762
Magnus Damm577cd752010-02-09 04:24:46 +0000763 if (desc->hw.ack_regs)
Magnus Dammd58876e2008-04-24 21:36:34 +0900764 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900765}
766
Magnus Dammf18d5332007-09-21 18:16:42 +0900767static unsigned int __init save_reg(struct intc_desc_int *d,
768 unsigned int cnt,
769 unsigned long value,
770 unsigned int smp)
771{
772 if (value) {
773 d->reg[cnt] = value;
774#ifdef CONFIG_SMP
775 d->smp[cnt] = smp;
776#endif
777 return 1;
778 }
779
780 return 0;
781}
782
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900783static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900784{
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900785 generic_handle_irq((unsigned int)get_irq_data(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900786}
Magnus Dammf18d5332007-09-21 18:16:42 +0900787
Magnus Damm02ab3f72007-07-18 17:25:09 +0900788void __init register_intc_controller(struct intc_desc *desc)
789{
Paul Mundt54ff3282009-06-11 10:33:09 +0300790 unsigned int i, k, smp;
Magnus Damm577cd752010-02-09 04:24:46 +0000791 struct intc_hw_desc *hw = &desc->hw;
Magnus Damm73505b42007-08-12 15:26:12 +0900792 struct intc_desc_int *d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900793
Paul Mundt11b6aa92009-06-12 01:34:12 +0300794 d = kzalloc(sizeof(*d), GFP_NOWAIT);
Magnus Damm73505b42007-08-12 15:26:12 +0900795
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000796 INIT_LIST_HEAD(&d->list);
797 list_add(&d->list, &intc_list);
798
Magnus Damm577cd752010-02-09 04:24:46 +0000799 d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
800 d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
801 d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
802 d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
Paul Mundt9b798d52009-10-27 11:36:43 +0900803
Paul Mundt11b6aa92009-06-12 01:34:12 +0300804 d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
Magnus Dammf18d5332007-09-21 18:16:42 +0900805#ifdef CONFIG_SMP
Paul Mundt11b6aa92009-06-12 01:34:12 +0300806 d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
Magnus Dammf18d5332007-09-21 18:16:42 +0900807#endif
Magnus Damm73505b42007-08-12 15:26:12 +0900808 k = 0;
809
Magnus Damm577cd752010-02-09 04:24:46 +0000810 if (hw->mask_regs) {
811 for (i = 0; i < hw->nr_mask_regs; i++) {
812 smp = IS_SMP(hw->mask_regs[i]);
813 k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
814 k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900815 }
816 }
817
Magnus Damm577cd752010-02-09 04:24:46 +0000818 if (hw->prio_regs) {
819 d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
820 GFP_NOWAIT);
Magnus Damm73505b42007-08-12 15:26:12 +0900821
Magnus Damm577cd752010-02-09 04:24:46 +0000822 for (i = 0; i < hw->nr_prio_regs; i++) {
823 smp = IS_SMP(hw->prio_regs[i]);
824 k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
825 k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900826 }
827 }
828
Magnus Damm577cd752010-02-09 04:24:46 +0000829 if (hw->sense_regs) {
830 d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
831 GFP_NOWAIT);
Magnus Damm73505b42007-08-12 15:26:12 +0900832
Magnus Damm577cd752010-02-09 04:24:46 +0000833 for (i = 0; i < hw->nr_sense_regs; i++)
834 k += save_reg(d, k, hw->sense_regs[i].reg, 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900835 }
836
Magnus Damm73505b42007-08-12 15:26:12 +0900837 d->chip.name = desc->name;
838 d->chip.mask = intc_disable;
839 d->chip.unmask = intc_enable;
840 d->chip.mask_ack = intc_disable;
Magnus Dammf7dd2542009-04-01 14:20:58 +0000841 d->chip.enable = intc_enable;
842 d->chip.disable = intc_disable;
843 d->chip.shutdown = intc_disable;
Magnus Damm73505b42007-08-12 15:26:12 +0900844 d->chip.set_type = intc_set_sense;
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000845 d->chip.set_wake = intc_set_wake;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900846
Magnus Damm577cd752010-02-09 04:24:46 +0000847 if (hw->ack_regs) {
848 for (i = 0; i < hw->nr_ack_regs; i++)
849 k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
Magnus Dammd58876e2008-04-24 21:36:34 +0900850
851 d->chip.mask_ack = intc_mask_ack;
852 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900853
Magnus Dammd85429a2010-02-15 11:40:25 +0000854 /* disable bits matching force_disable before registering irqs */
855 if (desc->force_disable)
856 intc_enable_disable_enum(desc, d, desc->force_disable, 0);
Magnus Dammd5190952010-02-09 04:29:22 +0000857
858 /* disable bits matching force_enable before registering irqs */
859 if (desc->force_enable)
860 intc_enable_disable_enum(desc, d, desc->force_enable, 0);
861
Magnus Dammd58876e2008-04-24 21:36:34 +0900862 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
863
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900864 /* register the vectors one by one */
Magnus Damm577cd752010-02-09 04:24:46 +0000865 for (i = 0; i < hw->nr_vectors; i++) {
866 struct intc_vect *vect = hw->vectors + i;
Paul Mundt05ff3002009-05-22 01:28:33 +0900867 unsigned int irq = evt2irq(vect->vect);
868 struct irq_desc *irq_desc;
Paul Mundt54ff3282009-06-11 10:33:09 +0300869
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900870 if (!vect->enum_id)
871 continue;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900872
Paul Mundt54ff3282009-06-11 10:33:09 +0300873 irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
Paul Mundt05ff3002009-05-22 01:28:33 +0900874 if (unlikely(!irq_desc)) {
Paul Mundt1279b7f2009-08-31 15:15:33 +0900875 pr_info("can't get irq_desc for %d\n", irq);
Paul Mundt05ff3002009-05-22 01:28:33 +0900876 continue;
877 }
878
879 intc_register_irq(desc, d, vect->enum_id, irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900880
Magnus Damm577cd752010-02-09 04:24:46 +0000881 for (k = i + 1; k < hw->nr_vectors; k++) {
882 struct intc_vect *vect2 = hw->vectors + k;
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900883 unsigned int irq2 = evt2irq(vect2->vect);
884
885 if (vect->enum_id != vect2->enum_id)
886 continue;
887
Paul Mundt1279b7f2009-08-31 15:15:33 +0900888 /*
889 * In the case of multi-evt handling and sparse
890 * IRQ support, each vector still needs to have
891 * its own backing irq_desc.
892 */
893 irq_desc = irq_to_desc_alloc_node(irq2, numa_node_id());
894 if (unlikely(!irq_desc)) {
895 pr_info("can't get irq_desc for %d\n", irq2);
896 continue;
897 }
898
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900899 vect2->enum_id = 0;
900
901 /* redirect this interrupts to the first one */
Paul Mundt4d2185d2010-02-17 12:37:42 +0900902 set_irq_chip(irq2, &dummy_irq_chip);
Magnus Damme6f07752010-02-09 07:17:20 +0000903 set_irq_chained_handler(irq2, intc_redirect_irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900904 set_irq_data(irq2, (void *)irq);
905 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900906 }
Magnus Dammd5190952010-02-09 04:29:22 +0000907
908 /* enable bits matching force_enable after registering irqs */
909 if (desc->force_enable)
910 intc_enable_disable_enum(desc, d, desc->force_enable, 1);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900911}
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000912
913static int intc_suspend(struct sys_device *dev, pm_message_t state)
914{
915 struct intc_desc_int *d;
916 struct irq_desc *desc;
917 int irq;
918
919 /* get intc controller associated with this sysdev */
920 d = container_of(dev, struct intc_desc_int, sysdev);
921
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000922 switch (state.event) {
923 case PM_EVENT_ON:
924 if (d->state.event != PM_EVENT_FREEZE)
925 break;
926 for_each_irq_desc(irq, desc) {
Francesco VIRLINZI87a705d2009-12-04 08:57:58 +0000927 if (desc->handle_irq == intc_redirect_irq)
Paul Mundt0a753d52009-12-09 14:36:16 +0900928 continue;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000929 if (desc->chip != &d->chip)
930 continue;
931 if (desc->status & IRQ_DISABLED)
932 intc_disable(irq);
933 else
934 intc_enable(irq);
935 }
936 break;
937 case PM_EVENT_FREEZE:
938 /* nothing has to be done */
939 break;
940 case PM_EVENT_SUSPEND:
941 /* enable wakeup irqs belonging to this intc controller */
942 for_each_irq_desc(irq, desc) {
943 if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
944 intc_enable(irq);
945 }
946 break;
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000947 }
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000948 d->state = state;
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000949
950 return 0;
951}
952
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000953static int intc_resume(struct sys_device *dev)
954{
955 return intc_suspend(dev, PMSG_ON);
956}
957
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000958static struct sysdev_class intc_sysdev_class = {
959 .name = "intc",
960 .suspend = intc_suspend,
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +0000961 .resume = intc_resume,
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000962};
963
964/* register this intc as sysdev to allow suspend/resume */
965static int __init register_intc_sysdevs(void)
966{
967 struct intc_desc_int *d;
968 int error;
969 int id = 0;
970
971 error = sysdev_class_register(&intc_sysdev_class);
972 if (!error) {
973 list_for_each_entry(d, &intc_list, list) {
974 d->sysdev.id = id;
975 d->sysdev.cls = &intc_sysdev_class;
976 error = sysdev_register(&d->sysdev);
977 if (error)
978 break;
979 id++;
980 }
981 }
982
983 if (error)
984 pr_warning("intc: sysdev registration error\n");
985
986 return error;
987}
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000988device_initcall(register_intc_sysdevs);
Paul Mundt1ce7b032009-11-02 10:30:26 +0900989
990/*
991 * Dynamic IRQ allocation and deallocation
992 */
Paul Mundte9867c52010-02-02 17:35:13 +0900993unsigned int create_irq_nr(unsigned int irq_want, int node)
Paul Mundt1ce7b032009-11-02 10:30:26 +0900994{
995 unsigned int irq = 0, new;
996 unsigned long flags;
997 struct irq_desc *desc;
998
999 spin_lock_irqsave(&vector_lock, flags);
1000
1001 /*
Paul Mundte9867c52010-02-02 17:35:13 +09001002 * First try the wanted IRQ
Paul Mundt1ce7b032009-11-02 10:30:26 +09001003 */
Paul Mundte9867c52010-02-02 17:35:13 +09001004 if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
1005 new = irq_want;
1006 } else {
1007 /* .. then fall back to scanning. */
Paul Mundt1ce7b032009-11-02 10:30:26 +09001008 new = find_first_zero_bit(intc_irq_map, nr_irqs);
1009 if (unlikely(new == nr_irqs))
1010 goto out_unlock;
1011
Paul Mundt1ce7b032009-11-02 10:30:26 +09001012 __set_bit(new, intc_irq_map);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001013 }
1014
Paul Mundte9867c52010-02-02 17:35:13 +09001015 desc = irq_to_desc_alloc_node(new, node);
1016 if (unlikely(!desc)) {
1017 pr_info("can't get irq_desc for %d\n", new);
1018 goto out_unlock;
1019 }
1020
1021 desc = move_irq_desc(desc, node);
1022 irq = new;
1023
Paul Mundt1ce7b032009-11-02 10:30:26 +09001024out_unlock:
1025 spin_unlock_irqrestore(&vector_lock, flags);
1026
1027 if (irq > 0)
1028 dynamic_irq_init(irq);
1029
1030 return irq;
1031}
1032
1033int create_irq(void)
1034{
1035 int nid = cpu_to_node(smp_processor_id());
1036 int irq;
1037
Paul Mundte9867c52010-02-02 17:35:13 +09001038 irq = create_irq_nr(NR_IRQS_LEGACY, nid);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001039 if (irq == 0)
1040 irq = -1;
1041
1042 return irq;
1043}
1044
1045void destroy_irq(unsigned int irq)
1046{
1047 unsigned long flags;
1048
1049 dynamic_irq_cleanup(irq);
1050
1051 spin_lock_irqsave(&vector_lock, flags);
1052 __clear_bit(irq, intc_irq_map);
1053 spin_unlock_irqrestore(&vector_lock, flags);
1054}
Paul Mundt45b9dea2009-11-02 15:43:20 +09001055
1056int reserve_irq_vector(unsigned int irq)
1057{
1058 unsigned long flags;
1059 int ret = 0;
1060
1061 spin_lock_irqsave(&vector_lock, flags);
1062 if (test_and_set_bit(irq, intc_irq_map))
1063 ret = -EBUSY;
1064 spin_unlock_irqrestore(&vector_lock, flags);
1065
1066 return ret;
1067}
1068
1069void reserve_irq_legacy(void)
1070{
1071 unsigned long flags;
1072 int i, j;
1073
1074 spin_lock_irqsave(&vector_lock, flags);
1075 j = find_first_bit(intc_irq_map, nr_irqs);
1076 for (i = 0; i < j; i++)
1077 __set_bit(i, intc_irq_map);
1078 spin_unlock_irqrestore(&vector_lock, flags);
1079}