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>
27 #include <linux/topology.h>
29 #define _INTC_MK(fn, mode, addr_e, addr_d, width, shift) \
30 ((shift) | ((width) << 5) | ((fn) << 9) | ((mode) << 13) | \
31 ((addr_e) << 16) | ((addr_d << 24)))
33 #define _INTC_SHIFT(h) (h & 0x1f)
34 #define _INTC_WIDTH(h) ((h >> 5) & 0xf)
35 #define _INTC_FN(h) ((h >> 9) & 0xf)
36 #define _INTC_MODE(h) ((h >> 13) & 0x7)
37 #define _INTC_ADDR_E(h) ((h >> 16) & 0xff)
38 #define _INTC_ADDR_D(h) ((h >> 24) & 0xff)
40 struct intc_handle_int {
45 struct intc_desc_int {
46 struct list_head list;
47 struct sys_device sysdev;
54 struct intc_handle_int *prio;
56 struct intc_handle_int *sense;
57 unsigned int nr_sense;
61 static LIST_HEAD(intc_list);
64 #define IS_SMP(x) x.smp
65 #define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
66 #define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
69 #define INTC_REG(d, x, c) (d->reg[(x)])
70 #define SMP_NR(d, x) 1
73 static unsigned int intc_prio_level[NR_IRQS]; /* for now */
74 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
75 static unsigned long ack_handle[NR_IRQS];
78 static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
80 struct irq_chip *chip = get_irq_chip(irq);
81 return (void *)((char *)chip - offsetof(struct intc_desc_int, chip));
84 static inline unsigned int set_field(unsigned int value,
85 unsigned int field_value,
88 unsigned int width = _INTC_WIDTH(handle);
89 unsigned int shift = _INTC_SHIFT(handle);
91 value &= ~(((1 << width) - 1) << shift);
92 value |= field_value << shift;
96 static void write_8(unsigned long addr, unsigned long h, unsigned long data)
98 __raw_writeb(set_field(0, data, h), addr);
101 static void write_16(unsigned long addr, unsigned long h, unsigned long data)
103 __raw_writew(set_field(0, data, h), addr);
106 static void write_32(unsigned long addr, unsigned long h, unsigned long data)
108 __raw_writel(set_field(0, data, h), addr);
111 static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
114 local_irq_save(flags);
115 __raw_writeb(set_field(__raw_readb(addr), data, h), addr);
116 local_irq_restore(flags);
119 static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
122 local_irq_save(flags);
123 __raw_writew(set_field(__raw_readw(addr), data, h), addr);
124 local_irq_restore(flags);
127 static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
130 local_irq_save(flags);
131 __raw_writel(set_field(__raw_readl(addr), data, h), addr);
132 local_irq_restore(flags);
135 enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
137 static void (*intc_reg_fns[])(unsigned long addr,
139 unsigned long data) = {
140 [REG_FN_WRITE_BASE + 0] = write_8,
141 [REG_FN_WRITE_BASE + 1] = write_16,
142 [REG_FN_WRITE_BASE + 3] = write_32,
143 [REG_FN_MODIFY_BASE + 0] = modify_8,
144 [REG_FN_MODIFY_BASE + 1] = modify_16,
145 [REG_FN_MODIFY_BASE + 3] = modify_32,
148 enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
149 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
150 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
151 MODE_PRIO_REG, /* Priority value written to enable interrupt */
152 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
155 static void intc_mode_field(unsigned long addr,
156 unsigned long handle,
157 void (*fn)(unsigned long,
162 fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
165 static void intc_mode_zero(unsigned long addr,
166 unsigned long handle,
167 void (*fn)(unsigned long,
175 static void intc_mode_prio(unsigned long addr,
176 unsigned long handle,
177 void (*fn)(unsigned long,
182 fn(addr, handle, intc_prio_level[irq]);
185 static void (*intc_enable_fns[])(unsigned long addr,
186 unsigned long handle,
187 void (*fn)(unsigned long,
190 unsigned int irq) = {
191 [MODE_ENABLE_REG] = intc_mode_field,
192 [MODE_MASK_REG] = intc_mode_zero,
193 [MODE_DUAL_REG] = intc_mode_field,
194 [MODE_PRIO_REG] = intc_mode_prio,
195 [MODE_PCLR_REG] = intc_mode_prio,
198 static void (*intc_disable_fns[])(unsigned long addr,
199 unsigned long handle,
200 void (*fn)(unsigned long,
203 unsigned int irq) = {
204 [MODE_ENABLE_REG] = intc_mode_zero,
205 [MODE_MASK_REG] = intc_mode_field,
206 [MODE_DUAL_REG] = intc_mode_field,
207 [MODE_PRIO_REG] = intc_mode_zero,
208 [MODE_PCLR_REG] = intc_mode_field,
211 static inline void _intc_enable(unsigned int irq, unsigned long handle)
213 struct intc_desc_int *d = get_intc_desc(irq);
217 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
218 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
219 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
220 [_INTC_FN(handle)], irq);
224 static void intc_enable(unsigned int irq)
226 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
229 static void intc_disable(unsigned int irq)
231 struct intc_desc_int *d = get_intc_desc(irq);
232 unsigned long handle = (unsigned long) get_irq_chip_data(irq);
236 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
237 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
238 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
239 [_INTC_FN(handle)], irq);
243 static int intc_set_wake(unsigned int irq, unsigned int on)
245 return 0; /* allow wakeup, but setup hardware in intc_suspend() */
248 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
249 static void intc_mask_ack(unsigned int irq)
251 struct intc_desc_int *d = get_intc_desc(irq);
252 unsigned long handle = ack_handle[irq];
257 /* read register and write zero only to the assocaited bit */
260 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
261 switch (_INTC_FN(handle)) {
262 case REG_FN_MODIFY_BASE + 0: /* 8bit */
264 __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
266 case REG_FN_MODIFY_BASE + 1: /* 16bit */
268 __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
270 case REG_FN_MODIFY_BASE + 3: /* 32bit */
272 __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
282 static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
288 /* this doesn't scale well, but...
290 * this function should only be used for cerain uncommon
291 * operations such as intc_set_priority() and intc_set_sense()
292 * and in those rare cases performance doesn't matter that much.
293 * keeping the memory footprint low is more important.
295 * one rather simple way to speed this up and still keep the
296 * memory footprint down is to make sure the array is sorted
297 * and then perform a bisect to lookup the irq.
300 for (i = 0; i < nr_hp; i++) {
301 if ((hp + i)->irq != irq)
310 int intc_set_priority(unsigned int irq, unsigned int prio)
312 struct intc_desc_int *d = get_intc_desc(irq);
313 struct intc_handle_int *ihp;
315 if (!intc_prio_level[irq] || prio <= 1)
318 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
320 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
323 intc_prio_level[irq] = prio;
326 * only set secondary masking method directly
327 * primary masking method is using intc_prio_level[irq]
328 * priority level will be set during next enable()
331 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
332 _intc_enable(irq, ihp->handle);
337 #define VALID(x) (x | 0x80)
339 static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
340 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
341 [IRQ_TYPE_EDGE_RISING] = VALID(1),
342 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
343 /* SH7706, SH7707 and SH7709 do not support high level triggered */
344 #if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
345 !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
346 !defined(CONFIG_CPU_SUBTYPE_SH7709)
347 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
351 static int intc_set_sense(unsigned int irq, unsigned int type)
353 struct intc_desc_int *d = get_intc_desc(irq);
354 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
355 struct intc_handle_int *ihp;
361 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
363 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
364 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
369 static unsigned int __init intc_get_reg(struct intc_desc_int *d,
370 unsigned long address)
374 for (k = 0; k < d->nr_reg; k++) {
375 if (d->reg[k] == address)
383 static intc_enum __init intc_grp_id(struct intc_desc *desc,
386 struct intc_group *g = desc->groups;
389 for (i = 0; g && enum_id && i < desc->nr_groups; i++) {
390 g = desc->groups + i;
392 for (j = 0; g->enum_ids[j]; j++) {
393 if (g->enum_ids[j] != enum_id)
403 static unsigned int __init intc_mask_data(struct intc_desc *desc,
404 struct intc_desc_int *d,
405 intc_enum enum_id, int do_grps)
407 struct intc_mask_reg *mr = desc->mask_regs;
408 unsigned int i, j, fn, mode;
409 unsigned long reg_e, reg_d;
411 for (i = 0; mr && enum_id && i < desc->nr_mask_regs; i++) {
412 mr = desc->mask_regs + i;
414 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
415 if (mr->enum_ids[j] != enum_id)
418 if (mr->set_reg && mr->clr_reg) {
419 fn = REG_FN_WRITE_BASE;
420 mode = MODE_DUAL_REG;
424 fn = REG_FN_MODIFY_BASE;
426 mode = MODE_ENABLE_REG;
430 mode = MODE_MASK_REG;
436 fn += (mr->reg_width >> 3) - 1;
437 return _INTC_MK(fn, mode,
438 intc_get_reg(d, reg_e),
439 intc_get_reg(d, reg_d),
441 (mr->reg_width - 1) - j);
446 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
451 static unsigned int __init intc_prio_data(struct intc_desc *desc,
452 struct intc_desc_int *d,
453 intc_enum enum_id, int do_grps)
455 struct intc_prio_reg *pr = desc->prio_regs;
456 unsigned int i, j, fn, mode, bit;
457 unsigned long reg_e, reg_d;
459 for (i = 0; pr && enum_id && i < desc->nr_prio_regs; i++) {
460 pr = desc->prio_regs + i;
462 for (j = 0; j < ARRAY_SIZE(pr->enum_ids); j++) {
463 if (pr->enum_ids[j] != enum_id)
466 if (pr->set_reg && pr->clr_reg) {
467 fn = REG_FN_WRITE_BASE;
468 mode = MODE_PCLR_REG;
472 fn = REG_FN_MODIFY_BASE;
473 mode = MODE_PRIO_REG;
480 fn += (pr->reg_width >> 3) - 1;
482 BUG_ON((j + 1) * pr->field_width > pr->reg_width);
484 bit = pr->reg_width - ((j + 1) * pr->field_width);
486 return _INTC_MK(fn, mode,
487 intc_get_reg(d, reg_e),
488 intc_get_reg(d, reg_d),
489 pr->field_width, bit);
494 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
499 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
500 static unsigned int __init intc_ack_data(struct intc_desc *desc,
501 struct intc_desc_int *d,
504 struct intc_mask_reg *mr = desc->ack_regs;
505 unsigned int i, j, fn, mode;
506 unsigned long reg_e, reg_d;
508 for (i = 0; mr && enum_id && i < desc->nr_ack_regs; i++) {
509 mr = desc->ack_regs + i;
511 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
512 if (mr->enum_ids[j] != enum_id)
515 fn = REG_FN_MODIFY_BASE;
516 mode = MODE_ENABLE_REG;
520 fn += (mr->reg_width >> 3) - 1;
521 return _INTC_MK(fn, mode,
522 intc_get_reg(d, reg_e),
523 intc_get_reg(d, reg_d),
525 (mr->reg_width - 1) - j);
533 static unsigned int __init intc_sense_data(struct intc_desc *desc,
534 struct intc_desc_int *d,
537 struct intc_sense_reg *sr = desc->sense_regs;
538 unsigned int i, j, fn, bit;
540 for (i = 0; sr && enum_id && i < desc->nr_sense_regs; i++) {
541 sr = desc->sense_regs + i;
543 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
544 if (sr->enum_ids[j] != enum_id)
547 fn = REG_FN_MODIFY_BASE;
548 fn += (sr->reg_width >> 3) - 1;
550 BUG_ON((j + 1) * sr->field_width > sr->reg_width);
552 bit = sr->reg_width - ((j + 1) * sr->field_width);
554 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
555 0, sr->field_width, bit);
562 static void __init intc_register_irq(struct intc_desc *desc,
563 struct intc_desc_int *d,
567 struct intc_handle_int *hp;
568 unsigned int data[2], primary;
570 /* Prefer single interrupt source bitmap over other combinations:
571 * 1. bitmap, single interrupt source
572 * 2. priority, single interrupt source
573 * 3. bitmap, multiple interrupt sources (groups)
574 * 4. priority, multiple interrupt sources (groups)
577 data[0] = intc_mask_data(desc, d, enum_id, 0);
578 data[1] = intc_prio_data(desc, d, enum_id, 0);
581 if (!data[0] && data[1])
584 if (!data[0] && !data[1])
585 pr_warning("intc: missing unique irq mask for "
586 "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
588 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
589 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
594 BUG_ON(!data[primary]); /* must have primary masking method */
596 disable_irq_nosync(irq);
597 set_irq_chip_and_handler_name(irq, &d->chip,
598 handle_level_irq, "level");
599 set_irq_chip_data(irq, (void *)data[primary]);
601 /* set priority level
602 * - this needs to be at least 2 for 5-bit priorities on 7780
604 intc_prio_level[irq] = 2;
606 /* enable secondary masking method if present */
608 _intc_enable(irq, data[!primary]);
610 /* add irq to d->prio list if priority is available */
612 hp = d->prio + d->nr_prio;
614 hp->handle = data[1];
618 * only secondary priority should access registers, so
619 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
622 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
623 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
628 /* add irq to d->sense list if sense is available */
629 data[0] = intc_sense_data(desc, d, enum_id);
631 (d->sense + d->nr_sense)->irq = irq;
632 (d->sense + d->nr_sense)->handle = data[0];
636 /* irq should be disabled by default */
639 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
641 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
645 static unsigned int __init save_reg(struct intc_desc_int *d,
661 static unsigned char *intc_evt2irq_table;
663 unsigned int intc_evt2irq(unsigned int vector)
665 unsigned int irq = evt2irq(vector);
667 if (intc_evt2irq_table && intc_evt2irq_table[irq])
668 irq = intc_evt2irq_table[irq];
673 void __init register_intc_controller(struct intc_desc *desc)
675 unsigned int i, k, smp;
676 struct intc_desc_int *d;
678 d = alloc_bootmem(sizeof(*d));
680 INIT_LIST_HEAD(&d->list);
681 list_add(&d->list, &intc_list);
683 d->nr_reg = desc->mask_regs ? desc->nr_mask_regs * 2 : 0;
684 d->nr_reg += desc->prio_regs ? desc->nr_prio_regs * 2 : 0;
685 d->nr_reg += desc->sense_regs ? desc->nr_sense_regs : 0;
687 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
688 d->nr_reg += desc->ack_regs ? desc->nr_ack_regs : 0;
690 d->reg = alloc_bootmem(d->nr_reg * sizeof(*d->reg));
692 d->smp = alloc_bootmem(d->nr_reg * sizeof(*d->smp));
696 if (desc->mask_regs) {
697 for (i = 0; i < desc->nr_mask_regs; i++) {
698 smp = IS_SMP(desc->mask_regs[i]);
699 k += save_reg(d, k, desc->mask_regs[i].set_reg, smp);
700 k += save_reg(d, k, desc->mask_regs[i].clr_reg, smp);
704 if (desc->prio_regs) {
705 d->prio = alloc_bootmem(desc->nr_vectors * sizeof(*d->prio));
707 for (i = 0; i < desc->nr_prio_regs; i++) {
708 smp = IS_SMP(desc->prio_regs[i]);
709 k += save_reg(d, k, desc->prio_regs[i].set_reg, smp);
710 k += save_reg(d, k, desc->prio_regs[i].clr_reg, smp);
714 if (desc->sense_regs) {
715 d->sense = alloc_bootmem(desc->nr_vectors * sizeof(*d->sense));
717 for (i = 0; i < desc->nr_sense_regs; i++) {
718 k += save_reg(d, k, desc->sense_regs[i].reg, 0);
722 d->chip.name = desc->name;
723 d->chip.mask = intc_disable;
724 d->chip.unmask = intc_enable;
725 d->chip.mask_ack = intc_disable;
726 d->chip.enable = intc_enable;
727 d->chip.disable = intc_disable;
728 d->chip.shutdown = intc_disable;
729 d->chip.set_type = intc_set_sense;
730 d->chip.set_wake = intc_set_wake;
732 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
733 if (desc->ack_regs) {
734 for (i = 0; i < desc->nr_ack_regs; i++)
735 k += save_reg(d, k, desc->ack_regs[i].set_reg, 0);
737 d->chip.mask_ack = intc_mask_ack;
741 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
743 /* keep the first vector only if same enum is used multiple times */
744 for (i = 0; i < desc->nr_vectors; i++) {
745 struct intc_vect *vect = desc->vectors + i;
746 int first_irq = evt2irq(vect->vect);
751 for (k = i + 1; k < desc->nr_vectors; k++) {
752 struct intc_vect *vect2 = desc->vectors + k;
754 if (vect->enum_id != vect2->enum_id)
759 if (!intc_evt2irq_table)
760 intc_evt2irq_table = alloc_bootmem(NR_IRQS);
762 if (!intc_evt2irq_table) {
763 pr_warning("intc: cannot allocate evt2irq!\n");
767 intc_evt2irq_table[evt2irq(vect2->vect)] = first_irq;
771 /* register the vectors one by one */
772 for (i = 0; i < desc->nr_vectors; i++) {
773 struct intc_vect *vect = desc->vectors + i;
774 unsigned int irq = evt2irq(vect->vect);
775 struct irq_desc *irq_desc;
780 irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
781 if (unlikely(!irq_desc)) {
782 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
786 intc_register_irq(desc, d, vect->enum_id, irq);
790 static int intc_suspend(struct sys_device *dev, pm_message_t state)
792 struct intc_desc_int *d;
793 struct irq_desc *desc;
796 /* get intc controller associated with this sysdev */
797 d = container_of(dev, struct intc_desc_int, sysdev);
799 switch (state.event) {
801 if (d->state.event != PM_EVENT_FREEZE)
803 for_each_irq_desc(irq, desc) {
804 if (desc->chip != &d->chip)
806 if (desc->status & IRQ_DISABLED)
812 case PM_EVENT_FREEZE:
813 /* nothing has to be done */
815 case PM_EVENT_SUSPEND:
816 /* enable wakeup irqs belonging to this intc controller */
817 for_each_irq_desc(irq, desc) {
818 if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
828 static int intc_resume(struct sys_device *dev)
830 return intc_suspend(dev, PMSG_ON);
833 static struct sysdev_class intc_sysdev_class = {
835 .suspend = intc_suspend,
836 .resume = intc_resume,
839 /* register this intc as sysdev to allow suspend/resume */
840 static int __init register_intc_sysdevs(void)
842 struct intc_desc_int *d;
846 error = sysdev_class_register(&intc_sysdev_class);
848 list_for_each_entry(d, &intc_list, list) {
850 d->sysdev.cls = &intc_sysdev_class;
851 error = sysdev_register(&d->sysdev);
859 pr_warning("intc: sysdev registration error\n");
864 device_initcall(register_intc_sysdevs);