blob: df3695406d803382caf582829faf26a512339db9 [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
Magnus Damm02ab3f72007-07-18 17:25:09 +09005 *
6 * Based on intc2.c and ipr.c
7 *
8 * Copyright (C) 1999 Niibe Yutaka & Takeshi Yaegashi
9 * Copyright (C) 2000 Kazumoto Kojima
10 * Copyright (C) 2001 David J. Mckay (david.mckay@st.com)
11 * Copyright (C) 2003 Takashi Kusuda <kusuda-takashi@hitachi-ul.co.jp>
12 * Copyright (C) 2005, 2006 Paul Mundt
13 *
14 * This file is subject to the terms and conditions of the GNU General Public
15 * License. See the file "COPYING" in the main directory of this archive
16 * for more details.
17 */
18#include <linux/init.h>
19#include <linux/irq.h>
20#include <linux/module.h>
21#include <linux/io.h>
22#include <linux/interrupt.h>
Magnus Damm73505b42007-08-12 15:26:12 +090023#include <linux/bootmem.h>
Magnus Damm02ab3f72007-07-18 17:25:09 +090024
Magnus Damm73505b42007-08-12 15:26:12 +090025#define _INTC_MK(fn, mode, addr_e, addr_d, width, shift) \
26 ((shift) | ((width) << 5) | ((fn) << 9) | ((mode) << 13) | \
27 ((addr_e) << 16) | ((addr_d << 24)))
Magnus Damm02ab3f72007-07-18 17:25:09 +090028
Magnus Damm73505b42007-08-12 15:26:12 +090029#define _INTC_SHIFT(h) (h & 0x1f)
30#define _INTC_WIDTH(h) ((h >> 5) & 0xf)
31#define _INTC_FN(h) ((h >> 9) & 0xf)
32#define _INTC_MODE(h) ((h >> 13) & 0x7)
33#define _INTC_ADDR_E(h) ((h >> 16) & 0xff)
34#define _INTC_ADDR_D(h) ((h >> 24) & 0xff)
Magnus Damm02ab3f72007-07-18 17:25:09 +090035
Magnus Damm73505b42007-08-12 15:26:12 +090036struct intc_handle_int {
37 unsigned int irq;
38 unsigned long handle;
39};
40
41struct intc_desc_int {
42 unsigned long *reg;
Magnus Dammf18d5332007-09-21 18:16:42 +090043#ifdef CONFIG_SMP
44 unsigned long *smp;
45#endif
Magnus Damm73505b42007-08-12 15:26:12 +090046 unsigned int nr_reg;
47 struct intc_handle_int *prio;
48 unsigned int nr_prio;
49 struct intc_handle_int *sense;
50 unsigned int nr_sense;
51 struct irq_chip chip;
52};
53
Magnus Dammf18d5332007-09-21 18:16:42 +090054#ifdef CONFIG_SMP
55#define IS_SMP(x) x.smp
56#define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
57#define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
58#else
59#define IS_SMP(x) 0
60#define INTC_REG(d, x, c) (d->reg[(x)])
61#define SMP_NR(d, x) 1
62#endif
63
Magnus Damm73505b42007-08-12 15:26:12 +090064static unsigned int intc_prio_level[NR_IRQS]; /* for now */
Magnus Dammd58876e2008-04-24 21:36:34 +090065#ifdef CONFIG_CPU_SH3
66static unsigned long ack_handle[NR_IRQS];
67#endif
Magnus Damm73505b42007-08-12 15:26:12 +090068
69static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +090070{
71 struct irq_chip *chip = get_irq_chip(irq);
Magnus Damm73505b42007-08-12 15:26:12 +090072 return (void *)((char *)chip - offsetof(struct intc_desc_int, chip));
Magnus Damm02ab3f72007-07-18 17:25:09 +090073}
74
75static inline unsigned int set_field(unsigned int value,
76 unsigned int field_value,
Magnus Damm73505b42007-08-12 15:26:12 +090077 unsigned int handle)
Magnus Damm02ab3f72007-07-18 17:25:09 +090078{
Magnus Damm73505b42007-08-12 15:26:12 +090079 unsigned int width = _INTC_WIDTH(handle);
80 unsigned int shift = _INTC_SHIFT(handle);
81
Magnus Damm02ab3f72007-07-18 17:25:09 +090082 value &= ~(((1 << width) - 1) << shift);
83 value |= field_value << shift;
84 return value;
85}
86
Magnus Damm73505b42007-08-12 15:26:12 +090087static void write_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +090088{
Magnus Damm73505b42007-08-12 15:26:12 +090089 ctrl_outb(set_field(0, data, h), addr);
Magnus Damm02ab3f72007-07-18 17:25:09 +090090}
91
Magnus Damm73505b42007-08-12 15:26:12 +090092static void write_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +090093{
Magnus Damm73505b42007-08-12 15:26:12 +090094 ctrl_outw(set_field(0, data, h), addr);
Magnus Damm02ab3f72007-07-18 17:25:09 +090095}
96
Magnus Damm73505b42007-08-12 15:26:12 +090097static void write_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +090098{
Magnus Damm73505b42007-08-12 15:26:12 +090099 ctrl_outl(set_field(0, data, h), addr);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900100}
101
Magnus Damm73505b42007-08-12 15:26:12 +0900102static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900103{
Magnus Damm73505b42007-08-12 15:26:12 +0900104 ctrl_outb(set_field(ctrl_inb(addr), data, h), addr);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900105}
106
Magnus Damm73505b42007-08-12 15:26:12 +0900107static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900108{
Magnus Damm73505b42007-08-12 15:26:12 +0900109 ctrl_outw(set_field(ctrl_inw(addr), data, h), addr);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900110}
111
Magnus Damm73505b42007-08-12 15:26:12 +0900112static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900113{
Magnus Damm73505b42007-08-12 15:26:12 +0900114 ctrl_outl(set_field(ctrl_inl(addr), data, h), addr);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900115}
116
Magnus Damm73505b42007-08-12 15:26:12 +0900117enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
Magnus Damm02ab3f72007-07-18 17:25:09 +0900118
Magnus Damm73505b42007-08-12 15:26:12 +0900119static void (*intc_reg_fns[])(unsigned long addr,
120 unsigned long h,
121 unsigned long data) = {
122 [REG_FN_WRITE_BASE + 0] = write_8,
123 [REG_FN_WRITE_BASE + 1] = write_16,
124 [REG_FN_WRITE_BASE + 3] = write_32,
125 [REG_FN_MODIFY_BASE + 0] = modify_8,
126 [REG_FN_MODIFY_BASE + 1] = modify_16,
127 [REG_FN_MODIFY_BASE + 3] = modify_32,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900128};
129
Magnus Damm73505b42007-08-12 15:26:12 +0900130enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
131 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
132 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
133 MODE_PRIO_REG, /* Priority value written to enable interrupt */
134 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
135};
136
137static void intc_mode_field(unsigned long addr,
138 unsigned long handle,
139 void (*fn)(unsigned long,
140 unsigned long,
141 unsigned long),
142 unsigned int irq)
143{
144 fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
145}
146
147static void intc_mode_zero(unsigned long addr,
148 unsigned long handle,
149 void (*fn)(unsigned long,
150 unsigned long,
151 unsigned long),
152 unsigned int irq)
153{
154 fn(addr, handle, 0);
155}
156
157static void intc_mode_prio(unsigned long addr,
158 unsigned long handle,
159 void (*fn)(unsigned long,
160 unsigned long,
161 unsigned long),
162 unsigned int irq)
163{
164 fn(addr, handle, intc_prio_level[irq]);
165}
166
167static void (*intc_enable_fns[])(unsigned long addr,
168 unsigned long handle,
169 void (*fn)(unsigned long,
170 unsigned long,
171 unsigned long),
172 unsigned int irq) = {
173 [MODE_ENABLE_REG] = intc_mode_field,
174 [MODE_MASK_REG] = intc_mode_zero,
175 [MODE_DUAL_REG] = intc_mode_field,
176 [MODE_PRIO_REG] = intc_mode_prio,
177 [MODE_PCLR_REG] = intc_mode_prio,
178};
179
180static void (*intc_disable_fns[])(unsigned long addr,
181 unsigned long handle,
182 void (*fn)(unsigned long,
183 unsigned long,
184 unsigned long),
185 unsigned int irq) = {
186 [MODE_ENABLE_REG] = intc_mode_zero,
187 [MODE_MASK_REG] = intc_mode_field,
188 [MODE_DUAL_REG] = intc_mode_field,
189 [MODE_PRIO_REG] = intc_mode_zero,
190 [MODE_PCLR_REG] = intc_mode_field,
191};
192
193static inline void _intc_enable(unsigned int irq, unsigned long handle)
194{
195 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900196 unsigned long addr;
197 unsigned int cpu;
Magnus Damm73505b42007-08-12 15:26:12 +0900198
Magnus Dammf18d5332007-09-21 18:16:42 +0900199 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
200 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
201 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
202 [_INTC_FN(handle)], irq);
203 }
Magnus Damm73505b42007-08-12 15:26:12 +0900204}
205
Magnus Damm02ab3f72007-07-18 17:25:09 +0900206static void intc_enable(unsigned int irq)
207{
Magnus Damm73505b42007-08-12 15:26:12 +0900208 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
Magnus Damm02ab3f72007-07-18 17:25:09 +0900209}
210
211static void intc_disable(unsigned int irq)
212{
Magnus Dammf18d5332007-09-21 18:16:42 +0900213 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900214 unsigned long handle = (unsigned long) get_irq_chip_data(irq);
Magnus Dammf18d5332007-09-21 18:16:42 +0900215 unsigned long addr;
216 unsigned int cpu;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900217
Magnus Dammf18d5332007-09-21 18:16:42 +0900218 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
219 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
220 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
221 [_INTC_FN(handle)], irq);
222 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900223}
224
Magnus Dammd58876e2008-04-24 21:36:34 +0900225#ifdef CONFIG_CPU_SH3
226static void intc_mask_ack(unsigned int irq)
227{
228 struct intc_desc_int *d = get_intc_desc(irq);
229 unsigned long handle = ack_handle[irq];
230 unsigned long addr;
231
232 intc_disable(irq);
233
234 /* read register and write zero only to the assocaited bit */
235
236 if (handle) {
237 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
238 ctrl_inb(addr);
239 ctrl_outb(0x3f ^ set_field(0, 1, handle), addr);
240 }
241}
242#endif
243
Magnus Damm73505b42007-08-12 15:26:12 +0900244static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
245 unsigned int nr_hp,
246 unsigned int irq)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900247{
Magnus Damm73505b42007-08-12 15:26:12 +0900248 int i;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900249
Magnus Damm3d37d942007-08-17 00:50:44 +0900250 /* this doesn't scale well, but...
251 *
252 * this function should only be used for cerain uncommon
253 * operations such as intc_set_priority() and intc_set_sense()
254 * and in those rare cases performance doesn't matter that much.
255 * keeping the memory footprint low is more important.
256 *
257 * one rather simple way to speed this up and still keep the
258 * memory footprint down is to make sure the array is sorted
259 * and then perform a bisect to lookup the irq.
260 */
261
Magnus Damm73505b42007-08-12 15:26:12 +0900262 for (i = 0; i < nr_hp; i++) {
263 if ((hp + i)->irq != irq)
264 continue;
265
266 return hp + i;
267 }
268
269 return NULL;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900270}
271
Magnus Damm73505b42007-08-12 15:26:12 +0900272int intc_set_priority(unsigned int irq, unsigned int prio)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900273{
Magnus Damm73505b42007-08-12 15:26:12 +0900274 struct intc_desc_int *d = get_intc_desc(irq);
275 struct intc_handle_int *ihp;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900276
Magnus Damm73505b42007-08-12 15:26:12 +0900277 if (!intc_prio_level[irq] || prio <= 1)
278 return -EINVAL;
279
280 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
281 if (ihp) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900282 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
Magnus Damm73505b42007-08-12 15:26:12 +0900283 return -EINVAL;
284
285 intc_prio_level[irq] = prio;
286
287 /*
288 * only set secondary masking method directly
289 * primary masking method is using intc_prio_level[irq]
290 * priority level will be set during next enable()
291 */
292
Magnus Damm3d37d942007-08-17 00:50:44 +0900293 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
Magnus Damm73505b42007-08-12 15:26:12 +0900294 _intc_enable(irq, ihp->handle);
295 }
296 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900297}
298
299#define VALID(x) (x | 0x80)
300
301static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
302 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
303 [IRQ_TYPE_EDGE_RISING] = VALID(1),
304 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
305 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
306};
307
308static int intc_set_sense(unsigned int irq, unsigned int type)
309{
Magnus Damm73505b42007-08-12 15:26:12 +0900310 struct intc_desc_int *d = get_intc_desc(irq);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900311 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
Magnus Damm73505b42007-08-12 15:26:12 +0900312 struct intc_handle_int *ihp;
313 unsigned long addr;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900314
Magnus Damm73505b42007-08-12 15:26:12 +0900315 if (!value)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900316 return -EINVAL;
317
Magnus Damm73505b42007-08-12 15:26:12 +0900318 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
319 if (ihp) {
Magnus Dammf18d5332007-09-21 18:16:42 +0900320 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900321 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900322 }
Magnus Damm73505b42007-08-12 15:26:12 +0900323 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900324}
325
Magnus Damm73505b42007-08-12 15:26:12 +0900326static unsigned int __init intc_get_reg(struct intc_desc_int *d,
327 unsigned long address)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900328{
Magnus Damm73505b42007-08-12 15:26:12 +0900329 unsigned int k;
330
331 for (k = 0; k < d->nr_reg; k++) {
332 if (d->reg[k] == address)
333 return k;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900334 }
335
336 BUG();
Magnus Damm73505b42007-08-12 15:26:12 +0900337 return 0;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900338}
339
Magnus Damm73505b42007-08-12 15:26:12 +0900340static intc_enum __init intc_grp_id(struct intc_desc *desc,
341 intc_enum enum_id)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900342{
Magnus Damm680c4592007-07-20 12:09:29 +0900343 struct intc_group *g = desc->groups;
344 unsigned int i, j;
345
346 for (i = 0; g && enum_id && i < desc->nr_groups; i++) {
347 g = desc->groups + i;
348
349 for (j = 0; g->enum_ids[j]; j++) {
350 if (g->enum_ids[j] != enum_id)
351 continue;
352
353 return g->enum_id;
354 }
355 }
356
357 return 0;
358}
359
Magnus Damm02ab3f72007-07-18 17:25:09 +0900360static unsigned int __init intc_mask_data(struct intc_desc *desc,
Magnus Damm73505b42007-08-12 15:26:12 +0900361 struct intc_desc_int *d,
Magnus Damm680c4592007-07-20 12:09:29 +0900362 intc_enum enum_id, int do_grps)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900363{
Magnus Damm680c4592007-07-20 12:09:29 +0900364 struct intc_mask_reg *mr = desc->mask_regs;
Magnus Damm73505b42007-08-12 15:26:12 +0900365 unsigned int i, j, fn, mode;
366 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900367
Magnus Damm680c4592007-07-20 12:09:29 +0900368 for (i = 0; mr && enum_id && i < desc->nr_mask_regs; i++) {
369 mr = desc->mask_regs + i;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900370
371 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
372 if (mr->enum_ids[j] != enum_id)
373 continue;
374
Magnus Damm73505b42007-08-12 15:26:12 +0900375 if (mr->set_reg && mr->clr_reg) {
376 fn = REG_FN_WRITE_BASE;
377 mode = MODE_DUAL_REG;
378 reg_e = mr->clr_reg;
379 reg_d = mr->set_reg;
380 } else {
381 fn = REG_FN_MODIFY_BASE;
382 if (mr->set_reg) {
383 mode = MODE_ENABLE_REG;
384 reg_e = mr->set_reg;
385 reg_d = mr->set_reg;
386 } else {
387 mode = MODE_MASK_REG;
388 reg_e = mr->clr_reg;
389 reg_d = mr->clr_reg;
390 }
Magnus Damm51da6422007-08-03 14:25:32 +0900391 }
392
Magnus Damm73505b42007-08-12 15:26:12 +0900393 fn += (mr->reg_width >> 3) - 1;
394 return _INTC_MK(fn, mode,
395 intc_get_reg(d, reg_e),
396 intc_get_reg(d, reg_d),
397 1,
398 (mr->reg_width - 1) - j);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900399 }
400 }
401
Magnus Damm680c4592007-07-20 12:09:29 +0900402 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900403 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900404
Magnus Damm02ab3f72007-07-18 17:25:09 +0900405 return 0;
406}
407
408static unsigned int __init intc_prio_data(struct intc_desc *desc,
Magnus Damm73505b42007-08-12 15:26:12 +0900409 struct intc_desc_int *d,
Magnus Damm680c4592007-07-20 12:09:29 +0900410 intc_enum enum_id, int do_grps)
Magnus Damm02ab3f72007-07-18 17:25:09 +0900411{
Magnus Damm680c4592007-07-20 12:09:29 +0900412 struct intc_prio_reg *pr = desc->prio_regs;
Magnus Damm73505b42007-08-12 15:26:12 +0900413 unsigned int i, j, fn, mode, bit;
414 unsigned long reg_e, reg_d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900415
Magnus Damm680c4592007-07-20 12:09:29 +0900416 for (i = 0; pr && enum_id && i < desc->nr_prio_regs; i++) {
417 pr = desc->prio_regs + i;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900418
419 for (j = 0; j < ARRAY_SIZE(pr->enum_ids); j++) {
420 if (pr->enum_ids[j] != enum_id)
421 continue;
422
Magnus Damm73505b42007-08-12 15:26:12 +0900423 if (pr->set_reg && pr->clr_reg) {
424 fn = REG_FN_WRITE_BASE;
425 mode = MODE_PCLR_REG;
426 reg_e = pr->set_reg;
427 reg_d = pr->clr_reg;
428 } else {
429 fn = REG_FN_MODIFY_BASE;
430 mode = MODE_PRIO_REG;
431 if (!pr->set_reg)
432 BUG();
433 reg_e = pr->set_reg;
434 reg_d = pr->set_reg;
435 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900436
Magnus Damm73505b42007-08-12 15:26:12 +0900437 fn += (pr->reg_width >> 3) - 1;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900438 bit = pr->reg_width - ((j + 1) * pr->field_width);
439
440 BUG_ON(bit < 0);
441
Magnus Damm73505b42007-08-12 15:26:12 +0900442 return _INTC_MK(fn, mode,
443 intc_get_reg(d, reg_e),
444 intc_get_reg(d, reg_d),
445 pr->field_width, bit);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900446 }
447 }
448
Magnus Damm680c4592007-07-20 12:09:29 +0900449 if (do_grps)
Magnus Damm73505b42007-08-12 15:26:12 +0900450 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900451
Magnus Damm02ab3f72007-07-18 17:25:09 +0900452 return 0;
453}
454
Magnus Dammd58876e2008-04-24 21:36:34 +0900455#ifdef CONFIG_CPU_SH3
456static unsigned int __init intc_ack_data(struct intc_desc *desc,
457 struct intc_desc_int *d,
458 intc_enum enum_id)
459{
460 struct intc_mask_reg *mr = desc->ack_regs;
461 unsigned int i, j, fn, mode;
462 unsigned long reg_e, reg_d;
463
464 for (i = 0; mr && enum_id && i < desc->nr_ack_regs; i++) {
465 mr = desc->ack_regs + i;
466
467 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
468 if (mr->enum_ids[j] != enum_id)
469 continue;
470
471 fn = REG_FN_MODIFY_BASE;
472 mode = MODE_ENABLE_REG;
473 reg_e = mr->set_reg;
474 reg_d = mr->set_reg;
475
476 fn += (mr->reg_width >> 3) - 1;
477 return _INTC_MK(fn, mode,
478 intc_get_reg(d, reg_e),
479 intc_get_reg(d, reg_d),
480 1,
481 (mr->reg_width - 1) - j);
482 }
483 }
484
485 return 0;
486}
487#endif
488
Magnus Damm73505b42007-08-12 15:26:12 +0900489static unsigned int __init intc_sense_data(struct intc_desc *desc,
490 struct intc_desc_int *d,
491 intc_enum enum_id)
492{
493 struct intc_sense_reg *sr = desc->sense_regs;
494 unsigned int i, j, fn, bit;
495
496 for (i = 0; sr && enum_id && i < desc->nr_sense_regs; i++) {
497 sr = desc->sense_regs + i;
498
499 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
500 if (sr->enum_ids[j] != enum_id)
501 continue;
502
503 fn = REG_FN_MODIFY_BASE;
504 fn += (sr->reg_width >> 3) - 1;
505 bit = sr->reg_width - ((j + 1) * sr->field_width);
506
507 BUG_ON(bit < 0);
508
509 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
510 0, sr->field_width, bit);
511 }
512 }
513
514 return 0;
515}
516
517static void __init intc_register_irq(struct intc_desc *desc,
518 struct intc_desc_int *d,
519 intc_enum enum_id,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900520 unsigned int irq)
521{
Magnus Damm3d37d942007-08-17 00:50:44 +0900522 struct intc_handle_int *hp;
Magnus Damm680c4592007-07-20 12:09:29 +0900523 unsigned int data[2], primary;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900524
Magnus Damm680c4592007-07-20 12:09:29 +0900525 /* Prefer single interrupt source bitmap over other combinations:
526 * 1. bitmap, single interrupt source
527 * 2. priority, single interrupt source
528 * 3. bitmap, multiple interrupt sources (groups)
529 * 4. priority, multiple interrupt sources (groups)
530 */
531
Magnus Damm73505b42007-08-12 15:26:12 +0900532 data[0] = intc_mask_data(desc, d, enum_id, 0);
533 data[1] = intc_prio_data(desc, d, enum_id, 0);
Magnus Damm680c4592007-07-20 12:09:29 +0900534
535 primary = 0;
536 if (!data[0] && data[1])
537 primary = 1;
538
Magnus Damm73505b42007-08-12 15:26:12 +0900539 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
540 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
Magnus Damm680c4592007-07-20 12:09:29 +0900541
542 if (!data[primary])
543 primary ^= 1;
544
545 BUG_ON(!data[primary]); /* must have primary masking method */
Magnus Damm02ab3f72007-07-18 17:25:09 +0900546
547 disable_irq_nosync(irq);
Magnus Damm73505b42007-08-12 15:26:12 +0900548 set_irq_chip_and_handler_name(irq, &d->chip,
Magnus Damm02ab3f72007-07-18 17:25:09 +0900549 handle_level_irq, "level");
Magnus Damm680c4592007-07-20 12:09:29 +0900550 set_irq_chip_data(irq, (void *)data[primary]);
Magnus Damm02ab3f72007-07-18 17:25:09 +0900551
Magnus Damm7f3edee2008-01-10 14:08:55 +0900552 /* set priority level
553 * - this needs to be at least 2 for 5-bit priorities on 7780
554 */
555 intc_prio_level[irq] = 2;
Magnus Damm73505b42007-08-12 15:26:12 +0900556
Magnus Damm680c4592007-07-20 12:09:29 +0900557 /* enable secondary masking method if present */
558 if (data[!primary])
Magnus Damm73505b42007-08-12 15:26:12 +0900559 _intc_enable(irq, data[!primary]);
560
561 /* add irq to d->prio list if priority is available */
562 if (data[1]) {
Magnus Damm3d37d942007-08-17 00:50:44 +0900563 hp = d->prio + d->nr_prio;
564 hp->irq = irq;
565 hp->handle = data[1];
566
567 if (primary) {
568 /*
569 * only secondary priority should access registers, so
570 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
571 */
572
573 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
574 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
575 }
Magnus Damm73505b42007-08-12 15:26:12 +0900576 d->nr_prio++;
577 }
578
579 /* add irq to d->sense list if sense is available */
580 data[0] = intc_sense_data(desc, d, enum_id);
581 if (data[0]) {
582 (d->sense + d->nr_sense)->irq = irq;
583 (d->sense + d->nr_sense)->handle = data[0];
584 d->nr_sense++;
585 }
Magnus Damm02ab3f72007-07-18 17:25:09 +0900586
587 /* irq should be disabled by default */
Magnus Damm73505b42007-08-12 15:26:12 +0900588 d->chip.mask(irq);
Magnus Dammd58876e2008-04-24 21:36:34 +0900589
590#ifdef CONFIG_CPU_SH3
591 if (desc->ack_regs)
592 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
593#endif
Magnus Damm02ab3f72007-07-18 17:25:09 +0900594}
595
Magnus Dammf18d5332007-09-21 18:16:42 +0900596static unsigned int __init save_reg(struct intc_desc_int *d,
597 unsigned int cnt,
598 unsigned long value,
599 unsigned int smp)
600{
601 if (value) {
602 d->reg[cnt] = value;
603#ifdef CONFIG_SMP
604 d->smp[cnt] = smp;
605#endif
606 return 1;
607 }
608
609 return 0;
610}
611
612
Magnus Damm02ab3f72007-07-18 17:25:09 +0900613void __init register_intc_controller(struct intc_desc *desc)
614{
Magnus Dammf18d5332007-09-21 18:16:42 +0900615 unsigned int i, k, smp;
Magnus Damm73505b42007-08-12 15:26:12 +0900616 struct intc_desc_int *d;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900617
Magnus Damm73505b42007-08-12 15:26:12 +0900618 d = alloc_bootmem(sizeof(*d));
619
620 d->nr_reg = desc->mask_regs ? desc->nr_mask_regs * 2 : 0;
621 d->nr_reg += desc->prio_regs ? desc->nr_prio_regs * 2 : 0;
622 d->nr_reg += desc->sense_regs ? desc->nr_sense_regs : 0;
623
Magnus Dammd58876e2008-04-24 21:36:34 +0900624#ifdef CONFIG_CPU_SH3
625 d->nr_reg += desc->ack_regs ? desc->nr_ack_regs : 0;
626#endif
Magnus Damm73505b42007-08-12 15:26:12 +0900627 d->reg = alloc_bootmem(d->nr_reg * sizeof(*d->reg));
Magnus Dammf18d5332007-09-21 18:16:42 +0900628#ifdef CONFIG_SMP
629 d->smp = alloc_bootmem(d->nr_reg * sizeof(*d->smp));
630#endif
Magnus Damm73505b42007-08-12 15:26:12 +0900631 k = 0;
632
633 if (desc->mask_regs) {
634 for (i = 0; i < desc->nr_mask_regs; i++) {
Magnus Dammf18d5332007-09-21 18:16:42 +0900635 smp = IS_SMP(desc->mask_regs[i]);
636 k += save_reg(d, k, desc->mask_regs[i].set_reg, smp);
637 k += save_reg(d, k, desc->mask_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900638 }
639 }
640
641 if (desc->prio_regs) {
642 d->prio = alloc_bootmem(desc->nr_vectors * sizeof(*d->prio));
643
644 for (i = 0; i < desc->nr_prio_regs; i++) {
Magnus Dammf18d5332007-09-21 18:16:42 +0900645 smp = IS_SMP(desc->prio_regs[i]);
646 k += save_reg(d, k, desc->prio_regs[i].set_reg, smp);
647 k += save_reg(d, k, desc->prio_regs[i].clr_reg, smp);
Magnus Damm73505b42007-08-12 15:26:12 +0900648 }
649 }
650
651 if (desc->sense_regs) {
652 d->sense = alloc_bootmem(desc->nr_vectors * sizeof(*d->sense));
653
654 for (i = 0; i < desc->nr_sense_regs; i++) {
Magnus Dammf18d5332007-09-21 18:16:42 +0900655 k += save_reg(d, k, desc->sense_regs[i].reg, 0);
Magnus Damm73505b42007-08-12 15:26:12 +0900656 }
657 }
658
Magnus Damm73505b42007-08-12 15:26:12 +0900659 d->chip.name = desc->name;
660 d->chip.mask = intc_disable;
661 d->chip.unmask = intc_enable;
662 d->chip.mask_ack = intc_disable;
663 d->chip.set_type = intc_set_sense;
Magnus Damm02ab3f72007-07-18 17:25:09 +0900664
Magnus Dammd58876e2008-04-24 21:36:34 +0900665#ifdef CONFIG_CPU_SH3
666 if (desc->ack_regs) {
667 for (i = 0; i < desc->nr_ack_regs; i++)
668 k += save_reg(d, k, desc->ack_regs[i].set_reg, 0);
669
670 d->chip.mask_ack = intc_mask_ack;
671 }
672#endif
673
674 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
675
Magnus Damm02ab3f72007-07-18 17:25:09 +0900676 for (i = 0; i < desc->nr_vectors; i++) {
677 struct intc_vect *vect = desc->vectors + i;
678
Magnus Damm73505b42007-08-12 15:26:12 +0900679 intc_register_irq(desc, d, vect->enum_id, evt2irq(vect->vect));
Magnus Damm02ab3f72007-07-18 17:25:09 +0900680 }
681}