blob: a700dfec8dc36d3d980732856605a5707928a9df [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
Magnus Dammdec710b2010-03-19 16:48:01 +090046struct intc_window {
47 phys_addr_t phys;
48 void __iomem *virt;
49 unsigned long size;
50};
51
Magnus Damm73505b42007-08-12 15:26:12 +090052struct intc_desc_int {
Magnus Damm2dcec7a2009-04-01 14:30:59 +000053 struct list_head list;
54 struct sys_device sysdev;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +000055 pm_message_t state;
Magnus Damm73505b42007-08-12 15:26:12 +090056 unsigned long *reg;
Magnus Dammf18d5332007-09-21 18:16:42 +090057#ifdef CONFIG_SMP
58 unsigned long *smp;
59#endif
Magnus Damm73505b42007-08-12 15:26:12 +090060 unsigned int nr_reg;
61 struct intc_handle_int *prio;
62 unsigned int nr_prio;
63 struct intc_handle_int *sense;
64 unsigned int nr_sense;
Magnus Dammdec710b2010-03-19 16:48:01 +090065 struct intc_window *window;
66 unsigned int nr_windows;
Magnus Damm73505b42007-08-12 15:26:12 +090067 struct irq_chip chip;
68};
69
Magnus Damm2dcec7a2009-04-01 14:30:59 +000070static LIST_HEAD(intc_list);
71
Paul Mundt1ce7b032009-11-02 10:30:26 +090072/*
73 * The intc_irq_map provides a global map of bound IRQ vectors for a
74 * given platform. Allocation of IRQs are either static through the CPU
75 * vector map, or dynamic in the case of board mux vectors or MSI.
76 *
77 * As this is a central point for all IRQ controllers on the system,
78 * each of the available sources are mapped out here. This combined with
79 * sparseirq makes it quite trivial to keep the vector map tightly packed
80 * when dynamically creating IRQs, as well as tying in to otherwise
81 * unused irq_desc positions in the sparse array.
82 */
83static DECLARE_BITMAP(intc_irq_map, NR_IRQS);
84static DEFINE_SPINLOCK(vector_lock);
85
Magnus Dammf18d5332007-09-21 18:16:42 +090086#ifdef CONFIG_SMP
87#define IS_SMP(x) x.smp
88#define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
89#define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
90#else
91#define IS_SMP(x) 0
92#define INTC_REG(d, x, c) (d->reg[(x)])
93#define SMP_NR(d, x) 1
94#endif
95
Magnus Damm73505b42007-08-12 15:26:12 +090096static unsigned int intc_prio_level[NR_IRQS]; /* for now */
Magnus Dammd58876e2008-04-24 21:36:34 +090097static unsigned long ack_handle[NR_IRQS];
Magnus Damm73505b42007-08-12 15:26:12 +090098
99static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900100{
101 struct irq_chip *chip = get_irq_chip(irq);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900102 return container_of(chip, struct intc_desc_int, chip);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900103}
104
105static inline unsigned int set_field(unsigned int value,
106 unsigned int field_value,
Magnus Damm73505b42007-08-12 15:26:12 +0900107 unsigned int handle)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900108{
Magnus Damm73505b42007-08-12 15:26:12 +0900109 unsigned int width = _INTC_WIDTH(handle);
110 unsigned int shift = _INTC_SHIFT(handle);
111
Magnus Damm02ab3f72007-07-18 17:25:09 +0900112 value &= ~(((1 << width) - 1) << shift);
113 value |= field_value << shift;
114 return value;
115}
116
Magnus Damm73505b42007-08-12 15:26:12 +0900117static void write_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900118{
Paul Mundt62429e02008-10-01 15:19:10 +0900119 __raw_writeb(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900120 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900121}
122
Magnus Damm73505b42007-08-12 15:26:12 +0900123static void write_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900124{
Paul Mundt62429e02008-10-01 15:19:10 +0900125 __raw_writew(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900126 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900127}
128
Magnus Damm73505b42007-08-12 15:26:12 +0900129static void write_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900130{
Paul Mundt62429e02008-10-01 15:19:10 +0900131 __raw_writel(set_field(0, data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900132 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900133}
134
Magnus Damm73505b42007-08-12 15:26:12 +0900135static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900136{
Magnus Damm4370fe12008-04-24 21:53:07 +0900137 unsigned long flags;
138 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900139 __raw_writeb(set_field(__raw_readb(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900140 (void)__raw_readb(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900141 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900142}
143
Magnus Damm73505b42007-08-12 15:26:12 +0900144static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900145{
Magnus Damm4370fe12008-04-24 21:53:07 +0900146 unsigned long flags;
147 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900148 __raw_writew(set_field(__raw_readw(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900149 (void)__raw_readw(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900150 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900151}
152
Magnus Damm73505b42007-08-12 15:26:12 +0900153static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900154{
Magnus Damm4370fe12008-04-24 21:53:07 +0900155 unsigned long flags;
156 local_irq_save(flags);
Paul Mundt62429e02008-10-01 15:19:10 +0900157 __raw_writel(set_field(__raw_readl(addr), data, h), addr);
Stuart Menefy6000fc42009-08-24 18:27:33 +0900158 (void)__raw_readl(addr); /* Defeat write posting */
Magnus Damm4370fe12008-04-24 21:53:07 +0900159 local_irq_restore(flags);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900160}
161
Magnus Damm73505b42007-08-12 15:26:12 +0900162enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
Magnus Damm02ab3f72007-07-18 17:25:09 +0900163
Magnus Damm73505b42007-08-12 15:26:12 +0900164static void (*intc_reg_fns[])(unsigned long addr,
165 unsigned long h,
166 unsigned long data) = {
167 [REG_FN_WRITE_BASE + 0] = write_8,
168 [REG_FN_WRITE_BASE + 1] = write_16,
169 [REG_FN_WRITE_BASE + 3] = write_32,
170 [REG_FN_MODIFY_BASE + 0] = modify_8,
171 [REG_FN_MODIFY_BASE + 1] = modify_16,
172 [REG_FN_MODIFY_BASE + 3] = modify_32,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900173};
174
Magnus Damm73505b42007-08-12 15:26:12 +0900175enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
176 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
177 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
178 MODE_PRIO_REG, /* Priority value written to enable interrupt */
179 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
180};
181
182static void intc_mode_field(unsigned long addr,
183 unsigned long handle,
184 void (*fn)(unsigned long,
185 unsigned long,
186 unsigned long),
187 unsigned int irq)
188{
189 fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
190}
191
192static void intc_mode_zero(unsigned long addr,
193 unsigned long handle,
194 void (*fn)(unsigned long,
195 unsigned long,
196 unsigned long),
197 unsigned int irq)
198{
199 fn(addr, handle, 0);
200}
201
202static void intc_mode_prio(unsigned long addr,
203 unsigned long handle,
204 void (*fn)(unsigned long,
205 unsigned long,
206 unsigned long),
207 unsigned int irq)
208{
209 fn(addr, handle, intc_prio_level[irq]);
210}
211
212static void (*intc_enable_fns[])(unsigned long addr,
213 unsigned long handle,
214 void (*fn)(unsigned long,
215 unsigned long,
216 unsigned long),
217 unsigned int irq) = {
218 [MODE_ENABLE_REG] = intc_mode_field,
219 [MODE_MASK_REG] = intc_mode_zero,
220 [MODE_DUAL_REG] = intc_mode_field,
221 [MODE_PRIO_REG] = intc_mode_prio,
222 [MODE_PCLR_REG] = intc_mode_prio,
223};
224
225static void (*intc_disable_fns[])(unsigned long addr,
226 unsigned long handle,
227 void (*fn)(unsigned long,
228 unsigned long,
229 unsigned long),
230 unsigned int irq) = {
231 [MODE_ENABLE_REG] = intc_mode_zero,
232 [MODE_MASK_REG] = intc_mode_field,
233 [MODE_DUAL_REG] = intc_mode_field,
234 [MODE_PRIO_REG] = intc_mode_zero,
235 [MODE_PCLR_REG] = intc_mode_field,
236};
237
238static inline void _intc_enable(unsigned int irq, unsigned long handle)
239{
240 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900241 unsigned long addr;
242 unsigned int cpu;
Magnus Damm73505b42007-08-12 15:26:12 +0900243
Magnus Dammf18d5332007-09-21 18:16:42 +0900244 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
245 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
246 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
247 [_INTC_FN(handle)], irq);
248 }
Magnus Damm73505b42007-08-12 15:26:12 +0900249}
250
Magnus Damm02ab3f72007-07-18 17:25:09 +0900251static void intc_enable(unsigned int irq)
252{
Magnus Damm73505b42007-08-12 15:26:12 +0900253 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
Magnus Damm02ab3f72007-07-18 17:25:09 +0900254}
255
256static void intc_disable(unsigned int irq)
257{
Magnus Dammf18d5332007-09-21 18:16:42 +0900258 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900259 unsigned long handle = (unsigned long) get_irq_chip_data(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900260 unsigned long addr;
261 unsigned int cpu;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900262
Magnus Dammf18d5332007-09-21 18:16:42 +0900263 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
264 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
265 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
266 [_INTC_FN(handle)], irq);
267 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900268}
269
Magnus Dammd5190952010-02-09 04:29:22 +0000270static void (*intc_enable_noprio_fns[])(unsigned long addr,
271 unsigned long handle,
272 void (*fn)(unsigned long,
273 unsigned long,
274 unsigned long),
275 unsigned int irq) = {
276 [MODE_ENABLE_REG] = intc_mode_field,
277 [MODE_MASK_REG] = intc_mode_zero,
278 [MODE_DUAL_REG] = intc_mode_field,
279 [MODE_PRIO_REG] = intc_mode_field,
280 [MODE_PCLR_REG] = intc_mode_field,
281};
282
283static void intc_enable_disable(struct intc_desc_int *d,
284 unsigned long handle, int do_enable)
285{
286 unsigned long addr;
287 unsigned int cpu;
288 void (*fn)(unsigned long, unsigned long,
289 void (*)(unsigned long, unsigned long, unsigned long),
290 unsigned int);
291
292 if (do_enable) {
293 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
294 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
295 fn = intc_enable_noprio_fns[_INTC_MODE(handle)];
296 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
297 }
298 } else {
299 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
300 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
301 fn = intc_disable_fns[_INTC_MODE(handle)];
302 fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
303 }
304 }
305}
306
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000307static int intc_set_wake(unsigned int irq, unsigned int on)
308{
309 return 0; /* allow wakeup, but setup hardware in intc_suspend() */
310}
311
Magnus Dammd58876e2008-04-24 21:36:34 +0900312static void intc_mask_ack(unsigned int irq)
313{
314 struct intc_desc_int *d = get_intc_desc(irq);
315 unsigned long handle = ack_handle[irq];
316 unsigned long addr;
317
318 intc_disable(irq);
319
320 /* read register and write zero only to the assocaited bit */
321
322 if (handle) {
323 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900324 switch (_INTC_FN(handle)) {
325 case REG_FN_MODIFY_BASE + 0: /* 8bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900326 __raw_readb(addr);
327 __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900328 break;
329 case REG_FN_MODIFY_BASE + 1: /* 16bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900330 __raw_readw(addr);
331 __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900332 break;
333 case REG_FN_MODIFY_BASE + 3: /* 32bit */
Paul Mundt62429e02008-10-01 15:19:10 +0900334 __raw_readl(addr);
335 __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
Yoshihiro Shimoda6bdfb222008-07-04 12:37:12 +0900336 break;
337 default:
338 BUG();
339 break;
340 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900341 }
342}
Magnus Dammd58876e2008-04-24 21:36:34 +0900343
Magnus Damm73505b42007-08-12 15:26:12 +0900344static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
345 unsigned int nr_hp,
346 unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900347{
Magnus Damm73505b42007-08-12 15:26:12 +0900348 int i;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900349
Magnus Damm3d37d942007-08-17 00:50:44 +0900350 /* this doesn't scale well, but...
351 *
352 * this function should only be used for cerain uncommon
353 * operations such as intc_set_priority() and intc_set_sense()
354 * and in those rare cases performance doesn't matter that much.
355 * keeping the memory footprint low is more important.
356 *
357 * one rather simple way to speed this up and still keep the
358 * memory footprint down is to make sure the array is sorted
359 * and then perform a bisect to lookup the irq.
360 */
361
Magnus Damm73505b42007-08-12 15:26:12 +0900362 for (i = 0; i < nr_hp; i++) {
363 if ((hp + i)->irq != irq)
364 continue;
365
366 return hp + i;
367 }
368
369 return NULL;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900370}
371
Magnus Damm73505b42007-08-12 15:26:12 +0900372int intc_set_priority(unsigned int irq, unsigned int prio)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900373{
Magnus Damm73505b42007-08-12 15:26:12 +0900374 struct intc_desc_int *d = get_intc_desc(irq);
375 struct intc_handle_int *ihp;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900376
Magnus Damm73505b42007-08-12 15:26:12 +0900377 if (!intc_prio_level[irq] || prio <= 1)
378 return -EINVAL;
379
380 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
381 if (ihp) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900382 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
Magnus Damm73505b42007-08-12 15:26:12 +0900383 return -EINVAL;
384
385 intc_prio_level[irq] = prio;
386
387 /*
388 * only set secondary masking method directly
389 * primary masking method is using intc_prio_level[irq]
390 * priority level will be set during next enable()
391 */
392
Magnus Damm3d37d942007-08-17 00:50:44 +0900393 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
Magnus Damm73505b42007-08-12 15:26:12 +0900394 _intc_enable(irq, ihp->handle);
395 }
396 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900397}
398
399#define VALID(x) (x | 0x80)
400
401static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
402 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
403 [IRQ_TYPE_EDGE_RISING] = VALID(1),
404 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
Magnus Damm720be992008-04-24 21:47:15 +0900405 /* SH7706, SH7707 and SH7709 do not support high level triggered */
406#if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
407 !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
408 !defined(CONFIG_CPU_SUBTYPE_SH7709)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900409 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
Magnus Damm720be992008-04-24 21:47:15 +0900410#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900411};
412
413static int intc_set_sense(unsigned int irq, unsigned int type)
414{
Magnus Damm73505b42007-08-12 15:26:12 +0900415 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900416 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
Magnus Damm73505b42007-08-12 15:26:12 +0900417 struct intc_handle_int *ihp;
418 unsigned long addr;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900419
Magnus Damm73505b42007-08-12 15:26:12 +0900420 if (!value)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900421 return -EINVAL;
422
Magnus Damm73505b42007-08-12 15:26:12 +0900423 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
424 if (ihp) {
Magnus Dammf18d5332007-09-21 18:16:42 +0900425 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900426 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900427 }
Magnus Damm73505b42007-08-12 15:26:12 +0900428 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900429}
430
Magnus Dammdec710b2010-03-19 16:48:01 +0900431static unsigned long intc_phys_to_virt(struct intc_desc_int *d,
432 unsigned long address)
433{
434 struct intc_window *window;
435 int k;
436
437 /* scan through physical windows and convert address */
438 for (k = 0; k < d->nr_windows; k++) {
439 window = d->window + k;
440
441 if (address < window->phys)
442 continue;
443
444 if (address >= (window->phys + window->size))
445 continue;
446
447 address -= window->phys;
448 address += (unsigned long)window->virt;
449
450 return address;
451 }
452
453 /* no windows defined, register must be 1:1 mapped virt:phys */
454 return address;
455}
456
Magnus Damm73505b42007-08-12 15:26:12 +0900457static unsigned int __init intc_get_reg(struct intc_desc_int *d,
Magnus Dammdec710b2010-03-19 16:48:01 +0900458 unsigned long address)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900459{
Magnus Damm73505b42007-08-12 15:26:12 +0900460 unsigned int k;
461
Magnus Dammdec710b2010-03-19 16:48:01 +0900462 address = intc_phys_to_virt(d, address);
463
Magnus Damm73505b42007-08-12 15:26:12 +0900464 for (k = 0; k < d->nr_reg; k++) {
465 if (d->reg[k] == address)
466 return k;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900467 }
468
469 BUG();
Magnus Damm73505b42007-08-12 15:26:12 +0900470 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900471}
472
Magnus Damm73505b42007-08-12 15:26:12 +0900473static intc_enum __init intc_grp_id(struct intc_desc *desc,
474 intc_enum enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900475{
Magnus Damm577cd752010-02-09 04:24:46 +0000476 struct intc_group *g = desc->hw.groups;
Magnus Damm680c4592007-07-20 12:09:29 +0900477 unsigned int i, j;
478
Magnus Damm577cd752010-02-09 04:24:46 +0000479 for (i = 0; g && enum_id && i < desc->hw.nr_groups; i++) {
480 g = desc->hw.groups + i;
Magnus Damm680c4592007-07-20 12:09:29 +0900481
482 for (j = 0; g->enum_ids[j]; j++) {
483 if (g->enum_ids[j] != enum_id)
484 continue;
485
486 return g->enum_id;
487 }
488 }
489
490 return 0;
491}
492
Magnus Dammd5190952010-02-09 04:29:22 +0000493static unsigned int __init _intc_mask_data(struct intc_desc *desc,
494 struct intc_desc_int *d,
495 intc_enum enum_id,
496 unsigned int *reg_idx,
497 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900498{
Magnus Damm577cd752010-02-09 04:24:46 +0000499 struct intc_mask_reg *mr = desc->hw.mask_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000500 unsigned int fn, mode;
Magnus Damm73505b42007-08-12 15:26:12 +0900501 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900502
Magnus Dammd5190952010-02-09 04:29:22 +0000503 while (mr && enum_id && *reg_idx < desc->hw.nr_mask_regs) {
504 mr = desc->hw.mask_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900505
Magnus Dammd5190952010-02-09 04:29:22 +0000506 for (; *fld_idx < ARRAY_SIZE(mr->enum_ids); (*fld_idx)++) {
507 if (mr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900508 continue;
509
Magnus Damm73505b42007-08-12 15:26:12 +0900510 if (mr->set_reg && mr->clr_reg) {
511 fn = REG_FN_WRITE_BASE;
512 mode = MODE_DUAL_REG;
513 reg_e = mr->clr_reg;
514 reg_d = mr->set_reg;
515 } else {
516 fn = REG_FN_MODIFY_BASE;
517 if (mr->set_reg) {
518 mode = MODE_ENABLE_REG;
519 reg_e = mr->set_reg;
520 reg_d = mr->set_reg;
521 } else {
522 mode = MODE_MASK_REG;
523 reg_e = mr->clr_reg;
524 reg_d = mr->clr_reg;
525 }
Magnus Damm51da6422007-08-03 14:25:32 +0900526 }
527
Magnus Damm73505b42007-08-12 15:26:12 +0900528 fn += (mr->reg_width >> 3) - 1;
529 return _INTC_MK(fn, mode,
530 intc_get_reg(d, reg_e),
531 intc_get_reg(d, reg_d),
532 1,
Magnus Dammd5190952010-02-09 04:29:22 +0000533 (mr->reg_width - 1) - *fld_idx);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900534 }
Magnus Dammd5190952010-02-09 04:29:22 +0000535
536 *fld_idx = 0;
537 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900538 }
539
Magnus Dammd5190952010-02-09 04:29:22 +0000540 return 0;
541}
542
543static unsigned int __init intc_mask_data(struct intc_desc *desc,
544 struct intc_desc_int *d,
545 intc_enum enum_id, int do_grps)
546{
547 unsigned int i = 0;
548 unsigned int j = 0;
549 unsigned int ret;
550
551 ret = _intc_mask_data(desc, d, enum_id, &i, &j);
552 if (ret)
553 return ret;
554
Magnus Damm680c4592007-07-20 12:09:29 +0900555 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900556 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900557
Magnus Damm02ab3f72007-07-18 17:25:09 +0900558 return 0;
559}
560
Magnus Dammd5190952010-02-09 04:29:22 +0000561static unsigned int __init _intc_prio_data(struct intc_desc *desc,
562 struct intc_desc_int *d,
563 intc_enum enum_id,
564 unsigned int *reg_idx,
565 unsigned int *fld_idx)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900566{
Magnus Damm577cd752010-02-09 04:24:46 +0000567 struct intc_prio_reg *pr = desc->hw.prio_regs;
Magnus Dammd5190952010-02-09 04:29:22 +0000568 unsigned int fn, n, mode, bit;
Magnus Damm73505b42007-08-12 15:26:12 +0900569 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900570
Magnus Dammd5190952010-02-09 04:29:22 +0000571 while (pr && enum_id && *reg_idx < desc->hw.nr_prio_regs) {
572 pr = desc->hw.prio_regs + *reg_idx;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900573
Magnus Dammd5190952010-02-09 04:29:22 +0000574 for (; *fld_idx < ARRAY_SIZE(pr->enum_ids); (*fld_idx)++) {
575 if (pr->enum_ids[*fld_idx] != enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900576 continue;
577
Magnus Damm73505b42007-08-12 15:26:12 +0900578 if (pr->set_reg && pr->clr_reg) {
579 fn = REG_FN_WRITE_BASE;
580 mode = MODE_PCLR_REG;
581 reg_e = pr->set_reg;
582 reg_d = pr->clr_reg;
583 } else {
584 fn = REG_FN_MODIFY_BASE;
585 mode = MODE_PRIO_REG;
586 if (!pr->set_reg)
587 BUG();
588 reg_e = pr->set_reg;
589 reg_d = pr->set_reg;
590 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900591
Magnus Damm73505b42007-08-12 15:26:12 +0900592 fn += (pr->reg_width >> 3) - 1;
Magnus Dammd5190952010-02-09 04:29:22 +0000593 n = *fld_idx + 1;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900594
Magnus Dammd5190952010-02-09 04:29:22 +0000595 BUG_ON(n * pr->field_width > pr->reg_width);
roel kluinb21a9102008-09-09 23:02:43 +0200596
Magnus Dammd5190952010-02-09 04:29:22 +0000597 bit = pr->reg_width - (n * pr->field_width);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900598
Magnus Damm73505b42007-08-12 15:26:12 +0900599 return _INTC_MK(fn, mode,
600 intc_get_reg(d, reg_e),
601 intc_get_reg(d, reg_d),
602 pr->field_width, bit);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900603 }
Magnus Dammd5190952010-02-09 04:29:22 +0000604
605 *fld_idx = 0;
606 (*reg_idx)++;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900607 }
608
Magnus Dammd5190952010-02-09 04:29:22 +0000609 return 0;
610}
611
612static unsigned int __init intc_prio_data(struct intc_desc *desc,
613 struct intc_desc_int *d,
614 intc_enum enum_id, int do_grps)
615{
616 unsigned int i = 0;
617 unsigned int j = 0;
618 unsigned int ret;
619
620 ret = _intc_prio_data(desc, d, enum_id, &i, &j);
621 if (ret)
622 return ret;
623
Magnus Damm680c4592007-07-20 12:09:29 +0900624 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900625 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900626
Magnus Damm02ab3f72007-07-18 17:25:09 +0900627 return 0;
628}
629
Magnus Dammd5190952010-02-09 04:29:22 +0000630static void __init intc_enable_disable_enum(struct intc_desc *desc,
631 struct intc_desc_int *d,
632 intc_enum enum_id, int enable)
633{
634 unsigned int i, j, data;
635
636 /* go through and enable/disable all mask bits */
637 i = j = 0;
638 do {
639 data = _intc_mask_data(desc, d, enum_id, &i, &j);
640 if (data)
641 intc_enable_disable(d, data, enable);
642 j++;
643 } while (data);
644
645 /* go through and enable/disable all priority fields */
646 i = j = 0;
647 do {
648 data = _intc_prio_data(desc, d, enum_id, &i, &j);
649 if (data)
650 intc_enable_disable(d, data, enable);
651
652 j++;
653 } while (data);
654}
655
Magnus Dammd58876e2008-04-24 21:36:34 +0900656static unsigned int __init intc_ack_data(struct intc_desc *desc,
657 struct intc_desc_int *d,
658 intc_enum enum_id)
659{
Magnus Damm577cd752010-02-09 04:24:46 +0000660 struct intc_mask_reg *mr = desc->hw.ack_regs;
Magnus Dammd58876e2008-04-24 21:36:34 +0900661 unsigned int i, j, fn, mode;
662 unsigned long reg_e, reg_d;
663
Magnus Damm577cd752010-02-09 04:24:46 +0000664 for (i = 0; mr && enum_id && i < desc->hw.nr_ack_regs; i++) {
665 mr = desc->hw.ack_regs + i;
Magnus Dammd58876e2008-04-24 21:36:34 +0900666
667 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
668 if (mr->enum_ids[j] != enum_id)
669 continue;
670
671 fn = REG_FN_MODIFY_BASE;
672 mode = MODE_ENABLE_REG;
673 reg_e = mr->set_reg;
674 reg_d = mr->set_reg;
675
676 fn += (mr->reg_width >> 3) - 1;
677 return _INTC_MK(fn, mode,
678 intc_get_reg(d, reg_e),
679 intc_get_reg(d, reg_d),
680 1,
681 (mr->reg_width - 1) - j);
682 }
683 }
684
685 return 0;
686}
Magnus Dammd58876e2008-04-24 21:36:34 +0900687
Magnus Damm73505b42007-08-12 15:26:12 +0900688static unsigned int __init intc_sense_data(struct intc_desc *desc,
689 struct intc_desc_int *d,
690 intc_enum enum_id)
691{
Magnus Damm577cd752010-02-09 04:24:46 +0000692 struct intc_sense_reg *sr = desc->hw.sense_regs;
Magnus Damm73505b42007-08-12 15:26:12 +0900693 unsigned int i, j, fn, bit;
694
Magnus Damm577cd752010-02-09 04:24:46 +0000695 for (i = 0; sr && enum_id && i < desc->hw.nr_sense_regs; i++) {
696 sr = desc->hw.sense_regs + i;
Magnus Damm73505b42007-08-12 15:26:12 +0900697
698 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
699 if (sr->enum_ids[j] != enum_id)
700 continue;
701
702 fn = REG_FN_MODIFY_BASE;
703 fn += (sr->reg_width >> 3) - 1;
Magnus Damm73505b42007-08-12 15:26:12 +0900704
roel kluinb21a9102008-09-09 23:02:43 +0200705 BUG_ON((j + 1) * sr->field_width > sr->reg_width);
706
707 bit = sr->reg_width - ((j + 1) * sr->field_width);
Magnus Damm73505b42007-08-12 15:26:12 +0900708
709 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
710 0, sr->field_width, bit);
711 }
712 }
713
714 return 0;
715}
716
717static void __init intc_register_irq(struct intc_desc *desc,
718 struct intc_desc_int *d,
719 intc_enum enum_id,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900720 unsigned int irq)
721{
Magnus Damm3d37d942007-08-17 00:50:44 +0900722 struct intc_handle_int *hp;
Magnus Damm680c4592007-07-20 12:09:29 +0900723 unsigned int data[2], primary;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900724
Paul Mundt1ce7b032009-11-02 10:30:26 +0900725 /*
726 * Register the IRQ position with the global IRQ map
727 */
728 set_bit(irq, intc_irq_map);
729
Magnus Damm680c4592007-07-20 12:09:29 +0900730 /* Prefer single interrupt source bitmap over other combinations:
731 * 1. bitmap, single interrupt source
732 * 2. priority, single interrupt source
733 * 3. bitmap, multiple interrupt sources (groups)
734 * 4. priority, multiple interrupt sources (groups)
735 */
736
Magnus Damm73505b42007-08-12 15:26:12 +0900737 data[0] = intc_mask_data(desc, d, enum_id, 0);
738 data[1] = intc_prio_data(desc, d, enum_id, 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900739
740 primary = 0;
741 if (!data[0] && data[1])
742 primary = 1;
743
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900744 if (!data[0] && !data[1])
Paul Mundtf0335992009-03-06 17:56:58 +0900745 pr_warning("intc: missing unique irq mask for "
746 "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900747
Magnus Damm73505b42007-08-12 15:26:12 +0900748 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
749 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
Magnus Damm680c4592007-07-20 12:09:29 +0900750
751 if (!data[primary])
752 primary ^= 1;
753
754 BUG_ON(!data[primary]); /* must have primary masking method */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900755
756 disable_irq_nosync(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900757 set_irq_chip_and_handler_name(irq, &d->chip,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900758 handle_level_irq, "level");
Magnus Damm680c4592007-07-20 12:09:29 +0900759 set_irq_chip_data(irq, (void *)data[primary]);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900760
Magnus Damm7f3edee2008-01-10 14:08:55 +0900761 /* set priority level
762 * - this needs to be at least 2 for 5-bit priorities on 7780
763 */
764 intc_prio_level[irq] = 2;
Magnus Damm73505b42007-08-12 15:26:12 +0900765
Magnus Damm680c4592007-07-20 12:09:29 +0900766 /* enable secondary masking method if present */
767 if (data[!primary])
Magnus Damm73505b42007-08-12 15:26:12 +0900768 _intc_enable(irq, data[!primary]);
769
770 /* add irq to d->prio list if priority is available */
771 if (data[1]) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900772 hp = d->prio + d->nr_prio;
773 hp->irq = irq;
774 hp->handle = data[1];
775
776 if (primary) {
777 /*
778 * only secondary priority should access registers, so
779 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
780 */
781
782 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
783 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
784 }
Magnus Damm73505b42007-08-12 15:26:12 +0900785 d->nr_prio++;
786 }
787
788 /* add irq to d->sense list if sense is available */
789 data[0] = intc_sense_data(desc, d, enum_id);
790 if (data[0]) {
791 (d->sense + d->nr_sense)->irq = irq;
792 (d->sense + d->nr_sense)->handle = data[0];
793 d->nr_sense++;
794 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900795
796 /* irq should be disabled by default */
Magnus Damm73505b42007-08-12 15:26:12 +0900797 d->chip.mask(irq);
Magnus Dammd58876e2008-04-24 21:36:34 +0900798
Magnus Damm577cd752010-02-09 04:24:46 +0000799 if (desc->hw.ack_regs)
Magnus Dammd58876e2008-04-24 21:36:34 +0900800 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
Magnus Damm65a5b282010-02-05 11:15:25 +0000801
802#ifdef CONFIG_ARM
803 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
804#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900805}
806
Magnus Dammf18d5332007-09-21 18:16:42 +0900807static unsigned int __init save_reg(struct intc_desc_int *d,
808 unsigned int cnt,
809 unsigned long value,
810 unsigned int smp)
811{
812 if (value) {
Magnus Dammdec710b2010-03-19 16:48:01 +0900813 value = intc_phys_to_virt(d, value);
814
Magnus Dammf18d5332007-09-21 18:16:42 +0900815 d->reg[cnt] = value;
816#ifdef CONFIG_SMP
817 d->smp[cnt] = smp;
818#endif
819 return 1;
820 }
821
822 return 0;
823}
824
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900825static void intc_redirect_irq(unsigned int irq, struct irq_desc *desc)
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900826{
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900827 generic_handle_irq((unsigned int)get_irq_data(irq));
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900828}
Magnus Dammf18d5332007-09-21 18:16:42 +0900829
Magnus Damm01e96512010-03-10 09:31:01 +0000830int __init register_intc_controller(struct intc_desc *desc)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900831{
Paul Mundt54ff3282009-06-11 10:33:09 +0300832 unsigned int i, k, smp;
Magnus Damm577cd752010-02-09 04:24:46 +0000833 struct intc_hw_desc *hw = &desc->hw;
Magnus Damm73505b42007-08-12 15:26:12 +0900834 struct intc_desc_int *d;
Magnus Dammdec710b2010-03-19 16:48:01 +0900835 struct resource *res;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900836
Paul Mundt11b6aa92009-06-12 01:34:12 +0300837 d = kzalloc(sizeof(*d), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000838 if (!d)
839 goto err0;
Magnus Damm73505b42007-08-12 15:26:12 +0900840
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000841 INIT_LIST_HEAD(&d->list);
842 list_add(&d->list, &intc_list);
843
Magnus Dammdec710b2010-03-19 16:48:01 +0900844 if (desc->num_resources) {
845 d->nr_windows = desc->num_resources;
846 d->window = kzalloc(d->nr_windows * sizeof(*d->window),
847 GFP_NOWAIT);
848 if (!d->window)
849 goto err1;
850
851 for (k = 0; k < d->nr_windows; k++) {
852 res = desc->resource + k;
853 WARN_ON(resource_type(res) != IORESOURCE_MEM);
854 d->window[k].phys = res->start;
855 d->window[k].size = resource_size(res);
856 d->window[k].virt = ioremap_nocache(res->start,
857 resource_size(res));
858 if (!d->window[k].virt)
859 goto err2;
860 }
861 }
862
Magnus Damm577cd752010-02-09 04:24:46 +0000863 d->nr_reg = hw->mask_regs ? hw->nr_mask_regs * 2 : 0;
864 d->nr_reg += hw->prio_regs ? hw->nr_prio_regs * 2 : 0;
865 d->nr_reg += hw->sense_regs ? hw->nr_sense_regs : 0;
866 d->nr_reg += hw->ack_regs ? hw->nr_ack_regs : 0;
Paul Mundt9b798d52009-10-27 11:36:43 +0900867
Paul Mundt11b6aa92009-06-12 01:34:12 +0300868 d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000869 if (!d->reg)
Magnus Dammdec710b2010-03-19 16:48:01 +0900870 goto err2;
Magnus Damm01e96512010-03-10 09:31:01 +0000871
Magnus Dammf18d5332007-09-21 18:16:42 +0900872#ifdef CONFIG_SMP
Paul Mundt11b6aa92009-06-12 01:34:12 +0300873 d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000874 if (!d->smp)
Magnus Dammdec710b2010-03-19 16:48:01 +0900875 goto err3;
Magnus Dammf18d5332007-09-21 18:16:42 +0900876#endif
Magnus Damm73505b42007-08-12 15:26:12 +0900877 k = 0;
878
Magnus Damm577cd752010-02-09 04:24:46 +0000879 if (hw->mask_regs) {
880 for (i = 0; i < hw->nr_mask_regs; i++) {
881 smp = IS_SMP(hw->mask_regs[i]);
882 k += save_reg(d, k, hw->mask_regs[i].set_reg, smp);
883 k += save_reg(d, k, hw->mask_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900884 }
885 }
886
Magnus Damm577cd752010-02-09 04:24:46 +0000887 if (hw->prio_regs) {
888 d->prio = kzalloc(hw->nr_vectors * sizeof(*d->prio),
889 GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000890 if (!d->prio)
Magnus Dammdec710b2010-03-19 16:48:01 +0900891 goto err4;
Magnus Damm73505b42007-08-12 15:26:12 +0900892
Magnus Damm577cd752010-02-09 04:24:46 +0000893 for (i = 0; i < hw->nr_prio_regs; i++) {
894 smp = IS_SMP(hw->prio_regs[i]);
895 k += save_reg(d, k, hw->prio_regs[i].set_reg, smp);
896 k += save_reg(d, k, hw->prio_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900897 }
898 }
899
Magnus Damm577cd752010-02-09 04:24:46 +0000900 if (hw->sense_regs) {
901 d->sense = kzalloc(hw->nr_vectors * sizeof(*d->sense),
902 GFP_NOWAIT);
Magnus Damm01e96512010-03-10 09:31:01 +0000903 if (!d->sense)
Magnus Dammdec710b2010-03-19 16:48:01 +0900904 goto err5;
Magnus Damm73505b42007-08-12 15:26:12 +0900905
Magnus Damm577cd752010-02-09 04:24:46 +0000906 for (i = 0; i < hw->nr_sense_regs; i++)
907 k += save_reg(d, k, hw->sense_regs[i].reg, 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900908 }
909
Magnus Damm73505b42007-08-12 15:26:12 +0900910 d->chip.name = desc->name;
911 d->chip.mask = intc_disable;
912 d->chip.unmask = intc_enable;
913 d->chip.mask_ack = intc_disable;
Magnus Dammf7dd2542009-04-01 14:20:58 +0000914 d->chip.enable = intc_enable;
915 d->chip.disable = intc_disable;
916 d->chip.shutdown = intc_disable;
Magnus Damm73505b42007-08-12 15:26:12 +0900917 d->chip.set_type = intc_set_sense;
Magnus Damm2dcec7a2009-04-01 14:30:59 +0000918 d->chip.set_wake = intc_set_wake;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900919
Magnus Damm577cd752010-02-09 04:24:46 +0000920 if (hw->ack_regs) {
921 for (i = 0; i < hw->nr_ack_regs; i++)
922 k += save_reg(d, k, hw->ack_regs[i].set_reg, 0);
Magnus Dammd58876e2008-04-24 21:36:34 +0900923
924 d->chip.mask_ack = intc_mask_ack;
925 }
Magnus Dammd58876e2008-04-24 21:36:34 +0900926
Magnus Dammd85429a2010-02-15 11:40:25 +0000927 /* disable bits matching force_disable before registering irqs */
928 if (desc->force_disable)
929 intc_enable_disable_enum(desc, d, desc->force_disable, 0);
Magnus Dammd5190952010-02-09 04:29:22 +0000930
931 /* disable bits matching force_enable before registering irqs */
932 if (desc->force_enable)
933 intc_enable_disable_enum(desc, d, desc->force_enable, 0);
934
Magnus Dammd58876e2008-04-24 21:36:34 +0900935 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
936
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900937 /* register the vectors one by one */
Magnus Damm577cd752010-02-09 04:24:46 +0000938 for (i = 0; i < hw->nr_vectors; i++) {
939 struct intc_vect *vect = hw->vectors + i;
Paul Mundt05ff3002009-05-22 01:28:33 +0900940 unsigned int irq = evt2irq(vect->vect);
941 struct irq_desc *irq_desc;
Paul Mundt54ff3282009-06-11 10:33:09 +0300942
Magnus Dammbdaa6e82009-02-24 22:58:57 +0900943 if (!vect->enum_id)
944 continue;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900945
Paul Mundt54ff3282009-06-11 10:33:09 +0300946 irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
Paul Mundt05ff3002009-05-22 01:28:33 +0900947 if (unlikely(!irq_desc)) {
Paul Mundt1279b7f2009-08-31 15:15:33 +0900948 pr_info("can't get irq_desc for %d\n", irq);
Paul Mundt05ff3002009-05-22 01:28:33 +0900949 continue;
950 }
951
952 intc_register_irq(desc, d, vect->enum_id, irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900953
Magnus Damm577cd752010-02-09 04:24:46 +0000954 for (k = i + 1; k < hw->nr_vectors; k++) {
955 struct intc_vect *vect2 = hw->vectors + k;
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900956 unsigned int irq2 = evt2irq(vect2->vect);
957
958 if (vect->enum_id != vect2->enum_id)
959 continue;
960
Paul Mundt1279b7f2009-08-31 15:15:33 +0900961 /*
962 * In the case of multi-evt handling and sparse
963 * IRQ support, each vector still needs to have
964 * its own backing irq_desc.
965 */
966 irq_desc = irq_to_desc_alloc_node(irq2, numa_node_id());
967 if (unlikely(!irq_desc)) {
968 pr_info("can't get irq_desc for %d\n", irq2);
969 continue;
970 }
971
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900972 vect2->enum_id = 0;
973
974 /* redirect this interrupts to the first one */
Paul Mundt4d2185d2010-02-17 12:37:42 +0900975 set_irq_chip(irq2, &dummy_irq_chip);
Magnus Damme6f07752010-02-09 07:17:20 +0000976 set_irq_chained_handler(irq2, intc_redirect_irq);
Pawel Moll05ecd5a2009-08-24 19:52:38 +0900977 set_irq_data(irq2, (void *)irq);
978 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900979 }
Magnus Dammd5190952010-02-09 04:29:22 +0000980
981 /* enable bits matching force_enable after registering irqs */
982 if (desc->force_enable)
983 intc_enable_disable_enum(desc, d, desc->force_enable, 1);
Magnus Damm01e96512010-03-10 09:31:01 +0000984
985 return 0;
Magnus Dammdec710b2010-03-19 16:48:01 +0900986err5:
Magnus Damm01e96512010-03-10 09:31:01 +0000987 kfree(d->prio);
Magnus Dammdec710b2010-03-19 16:48:01 +0900988err4:
Magnus Damm01e96512010-03-10 09:31:01 +0000989#ifdef CONFIG_SMP
990 kfree(d->smp);
Magnus Dammdec710b2010-03-19 16:48:01 +0900991err3:
Magnus Damm01e96512010-03-10 09:31:01 +0000992#endif
993 kfree(d->reg);
Magnus Dammdec710b2010-03-19 16:48:01 +0900994err2:
995 for (k = 0; k < d->nr_windows; k++)
996 if (d->window[k].virt)
997 iounmap(d->window[k].virt);
998
999 kfree(d->window);
1000err1:
Magnus Damm01e96512010-03-10 09:31:01 +00001001 kfree(d);
Magnus Dammdec710b2010-03-19 16:48:01 +09001002err0:
Magnus Damm01e96512010-03-10 09:31:01 +00001003 pr_err("unable to allocate INTC memory\n");
1004
1005 return -ENOMEM;
Magnus Damm02ab3f72007-07-18 17:25:09 +09001006}
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001007
1008static int intc_suspend(struct sys_device *dev, pm_message_t state)
1009{
1010 struct intc_desc_int *d;
1011 struct irq_desc *desc;
1012 int irq;
1013
1014 /* get intc controller associated with this sysdev */
1015 d = container_of(dev, struct intc_desc_int, sysdev);
1016
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001017 switch (state.event) {
1018 case PM_EVENT_ON:
1019 if (d->state.event != PM_EVENT_FREEZE)
1020 break;
1021 for_each_irq_desc(irq, desc) {
Francesco VIRLINZI87a705d2009-12-04 08:57:58 +00001022 if (desc->handle_irq == intc_redirect_irq)
Paul Mundt0a753d52009-12-09 14:36:16 +09001023 continue;
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001024 if (desc->chip != &d->chip)
1025 continue;
1026 if (desc->status & IRQ_DISABLED)
1027 intc_disable(irq);
1028 else
1029 intc_enable(irq);
1030 }
1031 break;
1032 case PM_EVENT_FREEZE:
1033 /* nothing has to be done */
1034 break;
1035 case PM_EVENT_SUSPEND:
1036 /* enable wakeup irqs belonging to this intc controller */
1037 for_each_irq_desc(irq, desc) {
1038 if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
1039 intc_enable(irq);
1040 }
1041 break;
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001042 }
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001043 d->state = state;
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001044
1045 return 0;
1046}
1047
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001048static int intc_resume(struct sys_device *dev)
1049{
1050 return intc_suspend(dev, PMSG_ON);
1051}
1052
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001053static struct sysdev_class intc_sysdev_class = {
1054 .name = "intc",
1055 .suspend = intc_suspend,
Francesco VIRLINZI7fd87b32009-04-06 07:17:04 +00001056 .resume = intc_resume,
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001057};
1058
1059/* register this intc as sysdev to allow suspend/resume */
1060static int __init register_intc_sysdevs(void)
1061{
1062 struct intc_desc_int *d;
1063 int error;
1064 int id = 0;
1065
1066 error = sysdev_class_register(&intc_sysdev_class);
1067 if (!error) {
1068 list_for_each_entry(d, &intc_list, list) {
1069 d->sysdev.id = id;
1070 d->sysdev.cls = &intc_sysdev_class;
1071 error = sysdev_register(&d->sysdev);
1072 if (error)
1073 break;
1074 id++;
1075 }
1076 }
1077
1078 if (error)
1079 pr_warning("intc: sysdev registration error\n");
1080
1081 return error;
1082}
Magnus Damm2dcec7a2009-04-01 14:30:59 +00001083device_initcall(register_intc_sysdevs);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001084
1085/*
1086 * Dynamic IRQ allocation and deallocation
1087 */
Paul Mundte9867c52010-02-02 17:35:13 +09001088unsigned int create_irq_nr(unsigned int irq_want, int node)
Paul Mundt1ce7b032009-11-02 10:30:26 +09001089{
1090 unsigned int irq = 0, new;
1091 unsigned long flags;
1092 struct irq_desc *desc;
1093
1094 spin_lock_irqsave(&vector_lock, flags);
1095
1096 /*
Paul Mundte9867c52010-02-02 17:35:13 +09001097 * First try the wanted IRQ
Paul Mundt1ce7b032009-11-02 10:30:26 +09001098 */
Paul Mundte9867c52010-02-02 17:35:13 +09001099 if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
1100 new = irq_want;
1101 } else {
1102 /* .. then fall back to scanning. */
Paul Mundt1ce7b032009-11-02 10:30:26 +09001103 new = find_first_zero_bit(intc_irq_map, nr_irqs);
1104 if (unlikely(new == nr_irqs))
1105 goto out_unlock;
1106
Paul Mundt1ce7b032009-11-02 10:30:26 +09001107 __set_bit(new, intc_irq_map);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001108 }
1109
Paul Mundte9867c52010-02-02 17:35:13 +09001110 desc = irq_to_desc_alloc_node(new, node);
1111 if (unlikely(!desc)) {
1112 pr_info("can't get irq_desc for %d\n", new);
1113 goto out_unlock;
1114 }
1115
1116 desc = move_irq_desc(desc, node);
1117 irq = new;
1118
Paul Mundt1ce7b032009-11-02 10:30:26 +09001119out_unlock:
1120 spin_unlock_irqrestore(&vector_lock, flags);
1121
Magnus Damm65a5b282010-02-05 11:15:25 +00001122 if (irq > 0) {
Paul Mundt1ce7b032009-11-02 10:30:26 +09001123 dynamic_irq_init(irq);
Magnus Damm65a5b282010-02-05 11:15:25 +00001124#ifdef CONFIG_ARM
1125 set_irq_flags(irq, IRQF_VALID); /* Enable IRQ on ARM systems */
1126#endif
1127 }
Paul Mundt1ce7b032009-11-02 10:30:26 +09001128
1129 return irq;
1130}
1131
1132int create_irq(void)
1133{
1134 int nid = cpu_to_node(smp_processor_id());
1135 int irq;
1136
Paul Mundte9867c52010-02-02 17:35:13 +09001137 irq = create_irq_nr(NR_IRQS_LEGACY, nid);
Paul Mundt1ce7b032009-11-02 10:30:26 +09001138 if (irq == 0)
1139 irq = -1;
1140
1141 return irq;
1142}
1143
1144void destroy_irq(unsigned int irq)
1145{
1146 unsigned long flags;
1147
1148 dynamic_irq_cleanup(irq);
1149
1150 spin_lock_irqsave(&vector_lock, flags);
1151 __clear_bit(irq, intc_irq_map);
1152 spin_unlock_irqrestore(&vector_lock, flags);
1153}
Paul Mundt45b9dea2009-11-02 15:43:20 +09001154
1155int reserve_irq_vector(unsigned int irq)
1156{
1157 unsigned long flags;
1158 int ret = 0;
1159
1160 spin_lock_irqsave(&vector_lock, flags);
1161 if (test_and_set_bit(irq, intc_irq_map))
1162 ret = -EBUSY;
1163 spin_unlock_irqrestore(&vector_lock, flags);
1164
1165 return ret;
1166}
1167
1168void reserve_irq_legacy(void)
1169{
1170 unsigned long flags;
1171 int i, j;
1172
1173 spin_lock_irqsave(&vector_lock, flags);
1174 j = find_first_bit(intc_irq_map, nr_irqs);
1175 for (i = 0; i < j; i++)
1176 __set_bit(i, intc_irq_map);
1177 spin_unlock_irqrestore(&vector_lock, flags);
1178}