2 * Shared interrupt handling code for IPR and INTC2 types of IRQs.
4 * Copyright (C) 2007, 2008 Magnus Damm
6 * Based on intc2.c and ipr.c
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
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
18 #include <linux/init.h>
19 #include <linux/irq.h>
20 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/bootmem.h>
24 #include <linux/sh_intc.h>
25 #include <linux/sysdev.h>
26 #include <linux/list.h>
28 #define _INTC_MK(fn, mode, addr_e, addr_d, width, shift) \
29 ((shift) | ((width) << 5) | ((fn) << 9) | ((mode) << 13) | \
30 ((addr_e) << 16) | ((addr_d << 24)))
32 #define _INTC_SHIFT(h) (h & 0x1f)
33 #define _INTC_WIDTH(h) ((h >> 5) & 0xf)
34 #define _INTC_FN(h) ((h >> 9) & 0xf)
35 #define _INTC_MODE(h) ((h >> 13) & 0x7)
36 #define _INTC_ADDR_E(h) ((h >> 16) & 0xff)
37 #define _INTC_ADDR_D(h) ((h >> 24) & 0xff)
39 struct intc_handle_int {
44 struct intc_desc_int {
45 struct list_head list;
46 struct sys_device sysdev;
52 struct intc_handle_int *prio;
54 struct intc_handle_int *sense;
55 unsigned int nr_sense;
59 static LIST_HEAD(intc_list);
62 #define IS_SMP(x) x.smp
63 #define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
64 #define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
67 #define INTC_REG(d, x, c) (d->reg[(x)])
68 #define SMP_NR(d, x) 1
71 static unsigned int intc_prio_level[NR_IRQS]; /* for now */
72 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
73 static unsigned long ack_handle[NR_IRQS];
76 static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
78 struct irq_chip *chip = get_irq_chip(irq);
79 return (void *)((char *)chip - offsetof(struct intc_desc_int, chip));
82 static inline unsigned int set_field(unsigned int value,
83 unsigned int field_value,
86 unsigned int width = _INTC_WIDTH(handle);
87 unsigned int shift = _INTC_SHIFT(handle);
89 value &= ~(((1 << width) - 1) << shift);
90 value |= field_value << shift;
94 static void write_8(unsigned long addr, unsigned long h, unsigned long data)
96 __raw_writeb(set_field(0, data, h), addr);
99 static void write_16(unsigned long addr, unsigned long h, unsigned long data)
101 __raw_writew(set_field(0, data, h), addr);
104 static void write_32(unsigned long addr, unsigned long h, unsigned long data)
106 __raw_writel(set_field(0, data, h), addr);
109 static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
112 local_irq_save(flags);
113 __raw_writeb(set_field(__raw_readb(addr), data, h), addr);
114 local_irq_restore(flags);
117 static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
120 local_irq_save(flags);
121 __raw_writew(set_field(__raw_readw(addr), data, h), addr);
122 local_irq_restore(flags);
125 static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
128 local_irq_save(flags);
129 __raw_writel(set_field(__raw_readl(addr), data, h), addr);
130 local_irq_restore(flags);
133 enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
135 static void (*intc_reg_fns[])(unsigned long addr,
137 unsigned long data) = {
138 [REG_FN_WRITE_BASE + 0] = write_8,
139 [REG_FN_WRITE_BASE + 1] = write_16,
140 [REG_FN_WRITE_BASE + 3] = write_32,
141 [REG_FN_MODIFY_BASE + 0] = modify_8,
142 [REG_FN_MODIFY_BASE + 1] = modify_16,
143 [REG_FN_MODIFY_BASE + 3] = modify_32,
146 enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
147 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
148 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
149 MODE_PRIO_REG, /* Priority value written to enable interrupt */
150 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
153 static void intc_mode_field(unsigned long addr,
154 unsigned long handle,
155 void (*fn)(unsigned long,
160 fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
163 static void intc_mode_zero(unsigned long addr,
164 unsigned long handle,
165 void (*fn)(unsigned long,
173 static void intc_mode_prio(unsigned long addr,
174 unsigned long handle,
175 void (*fn)(unsigned long,
180 fn(addr, handle, intc_prio_level[irq]);
183 static void (*intc_enable_fns[])(unsigned long addr,
184 unsigned long handle,
185 void (*fn)(unsigned long,
188 unsigned int irq) = {
189 [MODE_ENABLE_REG] = intc_mode_field,
190 [MODE_MASK_REG] = intc_mode_zero,
191 [MODE_DUAL_REG] = intc_mode_field,
192 [MODE_PRIO_REG] = intc_mode_prio,
193 [MODE_PCLR_REG] = intc_mode_prio,
196 static void (*intc_disable_fns[])(unsigned long addr,
197 unsigned long handle,
198 void (*fn)(unsigned long,
201 unsigned int irq) = {
202 [MODE_ENABLE_REG] = intc_mode_zero,
203 [MODE_MASK_REG] = intc_mode_field,
204 [MODE_DUAL_REG] = intc_mode_field,
205 [MODE_PRIO_REG] = intc_mode_zero,
206 [MODE_PCLR_REG] = intc_mode_field,
209 static inline void _intc_enable(unsigned int irq, unsigned long handle)
211 struct intc_desc_int *d = get_intc_desc(irq);
215 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
216 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
217 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
218 [_INTC_FN(handle)], irq);
222 static void intc_enable(unsigned int irq)
224 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
227 static void intc_disable(unsigned int irq)
229 struct intc_desc_int *d = get_intc_desc(irq);
230 unsigned long handle = (unsigned long) get_irq_chip_data(irq);
234 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
235 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
236 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
237 [_INTC_FN(handle)], irq);
241 static int intc_set_wake(unsigned int irq, unsigned int on)
243 return 0; /* allow wakeup, but setup hardware in intc_suspend() */
246 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
247 static void intc_mask_ack(unsigned int irq)
249 struct intc_desc_int *d = get_intc_desc(irq);
250 unsigned long handle = ack_handle[irq];
255 /* read register and write zero only to the assocaited bit */
258 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
259 switch (_INTC_FN(handle)) {
260 case REG_FN_MODIFY_BASE + 0: /* 8bit */
262 __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
264 case REG_FN_MODIFY_BASE + 1: /* 16bit */
266 __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
268 case REG_FN_MODIFY_BASE + 3: /* 32bit */
270 __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
280 static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
286 /* this doesn't scale well, but...
288 * this function should only be used for cerain uncommon
289 * operations such as intc_set_priority() and intc_set_sense()
290 * and in those rare cases performance doesn't matter that much.
291 * keeping the memory footprint low is more important.
293 * one rather simple way to speed this up and still keep the
294 * memory footprint down is to make sure the array is sorted
295 * and then perform a bisect to lookup the irq.
298 for (i = 0; i < nr_hp; i++) {
299 if ((hp + i)->irq != irq)
308 int intc_set_priority(unsigned int irq, unsigned int prio)
310 struct intc_desc_int *d = get_intc_desc(irq);
311 struct intc_handle_int *ihp;
313 if (!intc_prio_level[irq] || prio <= 1)
316 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
318 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
321 intc_prio_level[irq] = prio;
324 * only set secondary masking method directly
325 * primary masking method is using intc_prio_level[irq]
326 * priority level will be set during next enable()
329 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
330 _intc_enable(irq, ihp->handle);
335 #define VALID(x) (x | 0x80)
337 static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
338 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
339 [IRQ_TYPE_EDGE_RISING] = VALID(1),
340 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
341 /* SH7706, SH7707 and SH7709 do not support high level triggered */
342 #if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
343 !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
344 !defined(CONFIG_CPU_SUBTYPE_SH7709)
345 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
349 static int intc_set_sense(unsigned int irq, unsigned int type)
351 struct intc_desc_int *d = get_intc_desc(irq);
352 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
353 struct intc_handle_int *ihp;
359 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
361 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
362 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
367 static unsigned int __init intc_get_reg(struct intc_desc_int *d,
368 unsigned long address)
372 for (k = 0; k < d->nr_reg; k++) {
373 if (d->reg[k] == address)
381 static intc_enum __init intc_grp_id(struct intc_desc *desc,
384 struct intc_group *g = desc->groups;
387 for (i = 0; g && enum_id && i < desc->nr_groups; i++) {
388 g = desc->groups + i;
390 for (j = 0; g->enum_ids[j]; j++) {
391 if (g->enum_ids[j] != enum_id)
401 static unsigned int __init intc_mask_data(struct intc_desc *desc,
402 struct intc_desc_int *d,
403 intc_enum enum_id, int do_grps)
405 struct intc_mask_reg *mr = desc->mask_regs;
406 unsigned int i, j, fn, mode;
407 unsigned long reg_e, reg_d;
409 for (i = 0; mr && enum_id && i < desc->nr_mask_regs; i++) {
410 mr = desc->mask_regs + i;
412 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
413 if (mr->enum_ids[j] != enum_id)
416 if (mr->set_reg && mr->clr_reg) {
417 fn = REG_FN_WRITE_BASE;
418 mode = MODE_DUAL_REG;
422 fn = REG_FN_MODIFY_BASE;
424 mode = MODE_ENABLE_REG;
428 mode = MODE_MASK_REG;
434 fn += (mr->reg_width >> 3) - 1;
435 return _INTC_MK(fn, mode,
436 intc_get_reg(d, reg_e),
437 intc_get_reg(d, reg_d),
439 (mr->reg_width - 1) - j);
444 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
449 static unsigned int __init intc_prio_data(struct intc_desc *desc,
450 struct intc_desc_int *d,
451 intc_enum enum_id, int do_grps)
453 struct intc_prio_reg *pr = desc->prio_regs;
454 unsigned int i, j, fn, mode, bit;
455 unsigned long reg_e, reg_d;
457 for (i = 0; pr && enum_id && i < desc->nr_prio_regs; i++) {
458 pr = desc->prio_regs + i;
460 for (j = 0; j < ARRAY_SIZE(pr->enum_ids); j++) {
461 if (pr->enum_ids[j] != enum_id)
464 if (pr->set_reg && pr->clr_reg) {
465 fn = REG_FN_WRITE_BASE;
466 mode = MODE_PCLR_REG;
470 fn = REG_FN_MODIFY_BASE;
471 mode = MODE_PRIO_REG;
478 fn += (pr->reg_width >> 3) - 1;
480 BUG_ON((j + 1) * pr->field_width > pr->reg_width);
482 bit = pr->reg_width - ((j + 1) * pr->field_width);
484 return _INTC_MK(fn, mode,
485 intc_get_reg(d, reg_e),
486 intc_get_reg(d, reg_d),
487 pr->field_width, bit);
492 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
497 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
498 static unsigned int __init intc_ack_data(struct intc_desc *desc,
499 struct intc_desc_int *d,
502 struct intc_mask_reg *mr = desc->ack_regs;
503 unsigned int i, j, fn, mode;
504 unsigned long reg_e, reg_d;
506 for (i = 0; mr && enum_id && i < desc->nr_ack_regs; i++) {
507 mr = desc->ack_regs + i;
509 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
510 if (mr->enum_ids[j] != enum_id)
513 fn = REG_FN_MODIFY_BASE;
514 mode = MODE_ENABLE_REG;
518 fn += (mr->reg_width >> 3) - 1;
519 return _INTC_MK(fn, mode,
520 intc_get_reg(d, reg_e),
521 intc_get_reg(d, reg_d),
523 (mr->reg_width - 1) - j);
531 static unsigned int __init intc_sense_data(struct intc_desc *desc,
532 struct intc_desc_int *d,
535 struct intc_sense_reg *sr = desc->sense_regs;
536 unsigned int i, j, fn, bit;
538 for (i = 0; sr && enum_id && i < desc->nr_sense_regs; i++) {
539 sr = desc->sense_regs + i;
541 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
542 if (sr->enum_ids[j] != enum_id)
545 fn = REG_FN_MODIFY_BASE;
546 fn += (sr->reg_width >> 3) - 1;
548 BUG_ON((j + 1) * sr->field_width > sr->reg_width);
550 bit = sr->reg_width - ((j + 1) * sr->field_width);
552 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
553 0, sr->field_width, bit);
560 static void __init intc_register_irq(struct intc_desc *desc,
561 struct intc_desc_int *d,
565 struct intc_handle_int *hp;
566 unsigned int data[2], primary;
568 /* Prefer single interrupt source bitmap over other combinations:
569 * 1. bitmap, single interrupt source
570 * 2. priority, single interrupt source
571 * 3. bitmap, multiple interrupt sources (groups)
572 * 4. priority, multiple interrupt sources (groups)
575 data[0] = intc_mask_data(desc, d, enum_id, 0);
576 data[1] = intc_prio_data(desc, d, enum_id, 0);
579 if (!data[0] && data[1])
582 if (!data[0] && !data[1])
583 pr_warning("intc: missing unique irq mask for "
584 "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
586 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
587 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
592 BUG_ON(!data[primary]); /* must have primary masking method */
594 disable_irq_nosync(irq);
595 set_irq_chip_and_handler_name(irq, &d->chip,
596 handle_level_irq, "level");
597 set_irq_chip_data(irq, (void *)data[primary]);
599 /* set priority level
600 * - this needs to be at least 2 for 5-bit priorities on 7780
602 intc_prio_level[irq] = 2;
604 /* enable secondary masking method if present */
606 _intc_enable(irq, data[!primary]);
608 /* add irq to d->prio list if priority is available */
610 hp = d->prio + d->nr_prio;
612 hp->handle = data[1];
616 * only secondary priority should access registers, so
617 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
620 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
621 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
626 /* add irq to d->sense list if sense is available */
627 data[0] = intc_sense_data(desc, d, enum_id);
629 (d->sense + d->nr_sense)->irq = irq;
630 (d->sense + d->nr_sense)->handle = data[0];
634 /* irq should be disabled by default */
637 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
639 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
643 static unsigned int __init save_reg(struct intc_desc_int *d,
659 static unsigned char *intc_evt2irq_table;
661 unsigned int intc_evt2irq(unsigned int vector)
663 unsigned int irq = evt2irq(vector);
665 if (intc_evt2irq_table && intc_evt2irq_table[irq])
666 irq = intc_evt2irq_table[irq];
671 void __init register_intc_controller(struct intc_desc *desc)
673 unsigned int i, k, smp;
674 struct intc_desc_int *d;
676 d = alloc_bootmem(sizeof(*d));
678 INIT_LIST_HEAD(&d->list);
679 list_add(&d->list, &intc_list);
681 d->nr_reg = desc->mask_regs ? desc->nr_mask_regs * 2 : 0;
682 d->nr_reg += desc->prio_regs ? desc->nr_prio_regs * 2 : 0;
683 d->nr_reg += desc->sense_regs ? desc->nr_sense_regs : 0;
685 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
686 d->nr_reg += desc->ack_regs ? desc->nr_ack_regs : 0;
688 d->reg = alloc_bootmem(d->nr_reg * sizeof(*d->reg));
690 d->smp = alloc_bootmem(d->nr_reg * sizeof(*d->smp));
694 if (desc->mask_regs) {
695 for (i = 0; i < desc->nr_mask_regs; i++) {
696 smp = IS_SMP(desc->mask_regs[i]);
697 k += save_reg(d, k, desc->mask_regs[i].set_reg, smp);
698 k += save_reg(d, k, desc->mask_regs[i].clr_reg, smp);
702 if (desc->prio_regs) {
703 d->prio = alloc_bootmem(desc->nr_vectors * sizeof(*d->prio));
705 for (i = 0; i < desc->nr_prio_regs; i++) {
706 smp = IS_SMP(desc->prio_regs[i]);
707 k += save_reg(d, k, desc->prio_regs[i].set_reg, smp);
708 k += save_reg(d, k, desc->prio_regs[i].clr_reg, smp);
712 if (desc->sense_regs) {
713 d->sense = alloc_bootmem(desc->nr_vectors * sizeof(*d->sense));
715 for (i = 0; i < desc->nr_sense_regs; i++) {
716 k += save_reg(d, k, desc->sense_regs[i].reg, 0);
720 d->chip.name = desc->name;
721 d->chip.mask = intc_disable;
722 d->chip.unmask = intc_enable;
723 d->chip.mask_ack = intc_disable;
724 d->chip.enable = intc_enable;
725 d->chip.disable = intc_disable;
726 d->chip.shutdown = intc_disable;
727 d->chip.set_type = intc_set_sense;
728 d->chip.set_wake = intc_set_wake;
730 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
731 if (desc->ack_regs) {
732 for (i = 0; i < desc->nr_ack_regs; i++)
733 k += save_reg(d, k, desc->ack_regs[i].set_reg, 0);
735 d->chip.mask_ack = intc_mask_ack;
739 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
741 /* keep the first vector only if same enum is used multiple times */
742 for (i = 0; i < desc->nr_vectors; i++) {
743 struct intc_vect *vect = desc->vectors + i;
744 int first_irq = evt2irq(vect->vect);
749 for (k = i + 1; k < desc->nr_vectors; k++) {
750 struct intc_vect *vect2 = desc->vectors + k;
752 if (vect->enum_id != vect2->enum_id)
757 if (!intc_evt2irq_table)
758 intc_evt2irq_table = alloc_bootmem(NR_IRQS);
760 if (!intc_evt2irq_table) {
761 pr_warning("intc: cannot allocate evt2irq!\n");
765 intc_evt2irq_table[evt2irq(vect2->vect)] = first_irq;
769 /* register the vectors one by one */
770 for (i = 0; i < desc->nr_vectors; i++) {
771 struct intc_vect *vect = desc->vectors + i;
776 intc_register_irq(desc, d, vect->enum_id, evt2irq(vect->vect));
780 static int intc_suspend(struct sys_device *dev, pm_message_t state)
782 struct intc_desc_int *d;
783 struct irq_desc *desc;
786 /* get intc controller associated with this sysdev */
787 d = container_of(dev, struct intc_desc_int, sysdev);
789 /* enable wakeup irqs belonging to this intc controller */
790 for_each_irq_desc(irq, desc) {
791 if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
798 static struct sysdev_class intc_sysdev_class = {
800 .suspend = intc_suspend,
803 /* register this intc as sysdev to allow suspend/resume */
804 static int __init register_intc_sysdevs(void)
806 struct intc_desc_int *d;
810 error = sysdev_class_register(&intc_sysdev_class);
812 list_for_each_entry(d, &intc_list, list) {
814 d->sysdev.cls = &intc_sysdev_class;
815 error = sysdev_register(&d->sysdev);
823 pr_warning("intc: sysdev registration error\n");
828 device_initcall(register_intc_sysdevs);