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;
53 struct intc_handle_int *prio;
55 struct intc_handle_int *sense;
56 unsigned int nr_sense;
60 static LIST_HEAD(intc_list);
63 #define IS_SMP(x) x.smp
64 #define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
65 #define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
68 #define INTC_REG(d, x, c) (d->reg[(x)])
69 #define SMP_NR(d, x) 1
72 static unsigned int intc_prio_level[NR_IRQS]; /* for now */
73 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
74 static unsigned long ack_handle[NR_IRQS];
77 static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
79 struct irq_chip *chip = get_irq_chip(irq);
80 return (void *)((char *)chip - offsetof(struct intc_desc_int, chip));
83 static inline unsigned int set_field(unsigned int value,
84 unsigned int field_value,
87 unsigned int width = _INTC_WIDTH(handle);
88 unsigned int shift = _INTC_SHIFT(handle);
90 value &= ~(((1 << width) - 1) << shift);
91 value |= field_value << shift;
95 static void write_8(unsigned long addr, unsigned long h, unsigned long data)
97 __raw_writeb(set_field(0, data, h), addr);
100 static void write_16(unsigned long addr, unsigned long h, unsigned long data)
102 __raw_writew(set_field(0, data, h), addr);
105 static void write_32(unsigned long addr, unsigned long h, unsigned long data)
107 __raw_writel(set_field(0, data, h), addr);
110 static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
113 local_irq_save(flags);
114 __raw_writeb(set_field(__raw_readb(addr), data, h), addr);
115 local_irq_restore(flags);
118 static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
121 local_irq_save(flags);
122 __raw_writew(set_field(__raw_readw(addr), data, h), addr);
123 local_irq_restore(flags);
126 static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
129 local_irq_save(flags);
130 __raw_writel(set_field(__raw_readl(addr), data, h), addr);
131 local_irq_restore(flags);
134 enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };
136 static void (*intc_reg_fns[])(unsigned long addr,
138 unsigned long data) = {
139 [REG_FN_WRITE_BASE + 0] = write_8,
140 [REG_FN_WRITE_BASE + 1] = write_16,
141 [REG_FN_WRITE_BASE + 3] = write_32,
142 [REG_FN_MODIFY_BASE + 0] = modify_8,
143 [REG_FN_MODIFY_BASE + 1] = modify_16,
144 [REG_FN_MODIFY_BASE + 3] = modify_32,
147 enum { MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
148 MODE_MASK_REG, /* Bit(s) set -> interrupt disabled */
149 MODE_DUAL_REG, /* Two registers, set bit to enable / disable */
150 MODE_PRIO_REG, /* Priority value written to enable interrupt */
151 MODE_PCLR_REG, /* Above plus all bits set to disable interrupt */
154 static void intc_mode_field(unsigned long addr,
155 unsigned long handle,
156 void (*fn)(unsigned long,
161 fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
164 static void intc_mode_zero(unsigned long addr,
165 unsigned long handle,
166 void (*fn)(unsigned long,
174 static void intc_mode_prio(unsigned long addr,
175 unsigned long handle,
176 void (*fn)(unsigned long,
181 fn(addr, handle, intc_prio_level[irq]);
184 static void (*intc_enable_fns[])(unsigned long addr,
185 unsigned long handle,
186 void (*fn)(unsigned long,
189 unsigned int irq) = {
190 [MODE_ENABLE_REG] = intc_mode_field,
191 [MODE_MASK_REG] = intc_mode_zero,
192 [MODE_DUAL_REG] = intc_mode_field,
193 [MODE_PRIO_REG] = intc_mode_prio,
194 [MODE_PCLR_REG] = intc_mode_prio,
197 static void (*intc_disable_fns[])(unsigned long addr,
198 unsigned long handle,
199 void (*fn)(unsigned long,
202 unsigned int irq) = {
203 [MODE_ENABLE_REG] = intc_mode_zero,
204 [MODE_MASK_REG] = intc_mode_field,
205 [MODE_DUAL_REG] = intc_mode_field,
206 [MODE_PRIO_REG] = intc_mode_zero,
207 [MODE_PCLR_REG] = intc_mode_field,
210 static inline void _intc_enable(unsigned int irq, unsigned long handle)
212 struct intc_desc_int *d = get_intc_desc(irq);
216 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
217 addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
218 intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
219 [_INTC_FN(handle)], irq);
223 static void intc_enable(unsigned int irq)
225 _intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
228 static void intc_disable(unsigned int irq)
230 struct intc_desc_int *d = get_intc_desc(irq);
231 unsigned long handle = (unsigned long) get_irq_chip_data(irq);
235 for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
236 addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
237 intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
238 [_INTC_FN(handle)], irq);
242 static int intc_set_wake(unsigned int irq, unsigned int on)
244 return 0; /* allow wakeup, but setup hardware in intc_suspend() */
247 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
248 static void intc_mask_ack(unsigned int irq)
250 struct intc_desc_int *d = get_intc_desc(irq);
251 unsigned long handle = ack_handle[irq];
256 /* read register and write zero only to the assocaited bit */
259 addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
260 switch (_INTC_FN(handle)) {
261 case REG_FN_MODIFY_BASE + 0: /* 8bit */
263 __raw_writeb(0xff ^ set_field(0, 1, handle), addr);
265 case REG_FN_MODIFY_BASE + 1: /* 16bit */
267 __raw_writew(0xffff ^ set_field(0, 1, handle), addr);
269 case REG_FN_MODIFY_BASE + 3: /* 32bit */
271 __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
281 static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
287 /* this doesn't scale well, but...
289 * this function should only be used for cerain uncommon
290 * operations such as intc_set_priority() and intc_set_sense()
291 * and in those rare cases performance doesn't matter that much.
292 * keeping the memory footprint low is more important.
294 * one rather simple way to speed this up and still keep the
295 * memory footprint down is to make sure the array is sorted
296 * and then perform a bisect to lookup the irq.
299 for (i = 0; i < nr_hp; i++) {
300 if ((hp + i)->irq != irq)
309 int intc_set_priority(unsigned int irq, unsigned int prio)
311 struct intc_desc_int *d = get_intc_desc(irq);
312 struct intc_handle_int *ihp;
314 if (!intc_prio_level[irq] || prio <= 1)
317 ihp = intc_find_irq(d->prio, d->nr_prio, irq);
319 if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
322 intc_prio_level[irq] = prio;
325 * only set secondary masking method directly
326 * primary masking method is using intc_prio_level[irq]
327 * priority level will be set during next enable()
330 if (_INTC_FN(ihp->handle) != REG_FN_ERR)
331 _intc_enable(irq, ihp->handle);
336 #define VALID(x) (x | 0x80)
338 static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
339 [IRQ_TYPE_EDGE_FALLING] = VALID(0),
340 [IRQ_TYPE_EDGE_RISING] = VALID(1),
341 [IRQ_TYPE_LEVEL_LOW] = VALID(2),
342 /* SH7706, SH7707 and SH7709 do not support high level triggered */
343 #if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
344 !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
345 !defined(CONFIG_CPU_SUBTYPE_SH7709)
346 [IRQ_TYPE_LEVEL_HIGH] = VALID(3),
350 static int intc_set_sense(unsigned int irq, unsigned int type)
352 struct intc_desc_int *d = get_intc_desc(irq);
353 unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
354 struct intc_handle_int *ihp;
360 ihp = intc_find_irq(d->sense, d->nr_sense, irq);
362 addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
363 intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
368 static unsigned int __init intc_get_reg(struct intc_desc_int *d,
369 unsigned long address)
373 for (k = 0; k < d->nr_reg; k++) {
374 if (d->reg[k] == address)
382 static intc_enum __init intc_grp_id(struct intc_desc *desc,
385 struct intc_group *g = desc->groups;
388 for (i = 0; g && enum_id && i < desc->nr_groups; i++) {
389 g = desc->groups + i;
391 for (j = 0; g->enum_ids[j]; j++) {
392 if (g->enum_ids[j] != enum_id)
402 static unsigned int __init intc_mask_data(struct intc_desc *desc,
403 struct intc_desc_int *d,
404 intc_enum enum_id, int do_grps)
406 struct intc_mask_reg *mr = desc->mask_regs;
407 unsigned int i, j, fn, mode;
408 unsigned long reg_e, reg_d;
410 for (i = 0; mr && enum_id && i < desc->nr_mask_regs; i++) {
411 mr = desc->mask_regs + i;
413 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
414 if (mr->enum_ids[j] != enum_id)
417 if (mr->set_reg && mr->clr_reg) {
418 fn = REG_FN_WRITE_BASE;
419 mode = MODE_DUAL_REG;
423 fn = REG_FN_MODIFY_BASE;
425 mode = MODE_ENABLE_REG;
429 mode = MODE_MASK_REG;
435 fn += (mr->reg_width >> 3) - 1;
436 return _INTC_MK(fn, mode,
437 intc_get_reg(d, reg_e),
438 intc_get_reg(d, reg_d),
440 (mr->reg_width - 1) - j);
445 return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
450 static unsigned int __init intc_prio_data(struct intc_desc *desc,
451 struct intc_desc_int *d,
452 intc_enum enum_id, int do_grps)
454 struct intc_prio_reg *pr = desc->prio_regs;
455 unsigned int i, j, fn, mode, bit;
456 unsigned long reg_e, reg_d;
458 for (i = 0; pr && enum_id && i < desc->nr_prio_regs; i++) {
459 pr = desc->prio_regs + i;
461 for (j = 0; j < ARRAY_SIZE(pr->enum_ids); j++) {
462 if (pr->enum_ids[j] != enum_id)
465 if (pr->set_reg && pr->clr_reg) {
466 fn = REG_FN_WRITE_BASE;
467 mode = MODE_PCLR_REG;
471 fn = REG_FN_MODIFY_BASE;
472 mode = MODE_PRIO_REG;
479 fn += (pr->reg_width >> 3) - 1;
481 BUG_ON((j + 1) * pr->field_width > pr->reg_width);
483 bit = pr->reg_width - ((j + 1) * pr->field_width);
485 return _INTC_MK(fn, mode,
486 intc_get_reg(d, reg_e),
487 intc_get_reg(d, reg_d),
488 pr->field_width, bit);
493 return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);
498 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
499 static unsigned int __init intc_ack_data(struct intc_desc *desc,
500 struct intc_desc_int *d,
503 struct intc_mask_reg *mr = desc->ack_regs;
504 unsigned int i, j, fn, mode;
505 unsigned long reg_e, reg_d;
507 for (i = 0; mr && enum_id && i < desc->nr_ack_regs; i++) {
508 mr = desc->ack_regs + i;
510 for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
511 if (mr->enum_ids[j] != enum_id)
514 fn = REG_FN_MODIFY_BASE;
515 mode = MODE_ENABLE_REG;
519 fn += (mr->reg_width >> 3) - 1;
520 return _INTC_MK(fn, mode,
521 intc_get_reg(d, reg_e),
522 intc_get_reg(d, reg_d),
524 (mr->reg_width - 1) - j);
532 static unsigned int __init intc_sense_data(struct intc_desc *desc,
533 struct intc_desc_int *d,
536 struct intc_sense_reg *sr = desc->sense_regs;
537 unsigned int i, j, fn, bit;
539 for (i = 0; sr && enum_id && i < desc->nr_sense_regs; i++) {
540 sr = desc->sense_regs + i;
542 for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
543 if (sr->enum_ids[j] != enum_id)
546 fn = REG_FN_MODIFY_BASE;
547 fn += (sr->reg_width >> 3) - 1;
549 BUG_ON((j + 1) * sr->field_width > sr->reg_width);
551 bit = sr->reg_width - ((j + 1) * sr->field_width);
553 return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
554 0, sr->field_width, bit);
561 static void __init intc_register_irq(struct intc_desc *desc,
562 struct intc_desc_int *d,
566 struct intc_handle_int *hp;
567 unsigned int data[2], primary;
569 /* Prefer single interrupt source bitmap over other combinations:
570 * 1. bitmap, single interrupt source
571 * 2. priority, single interrupt source
572 * 3. bitmap, multiple interrupt sources (groups)
573 * 4. priority, multiple interrupt sources (groups)
576 data[0] = intc_mask_data(desc, d, enum_id, 0);
577 data[1] = intc_prio_data(desc, d, enum_id, 0);
580 if (!data[0] && data[1])
583 if (!data[0] && !data[1])
584 pr_warning("intc: missing unique irq mask for "
585 "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
587 data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
588 data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
593 BUG_ON(!data[primary]); /* must have primary masking method */
595 disable_irq_nosync(irq);
596 set_irq_chip_and_handler_name(irq, &d->chip,
597 handle_level_irq, "level");
598 set_irq_chip_data(irq, (void *)data[primary]);
600 /* set priority level
601 * - this needs to be at least 2 for 5-bit priorities on 7780
603 intc_prio_level[irq] = 2;
605 /* enable secondary masking method if present */
607 _intc_enable(irq, data[!primary]);
609 /* add irq to d->prio list if priority is available */
611 hp = d->prio + d->nr_prio;
613 hp->handle = data[1];
617 * only secondary priority should access registers, so
618 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
621 hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
622 hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
627 /* add irq to d->sense list if sense is available */
628 data[0] = intc_sense_data(desc, d, enum_id);
630 (d->sense + d->nr_sense)->irq = irq;
631 (d->sense + d->nr_sense)->handle = data[0];
635 /* irq should be disabled by default */
638 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
640 ack_handle[irq] = intc_ack_data(desc, d, enum_id);
644 static unsigned int __init save_reg(struct intc_desc_int *d,
660 static unsigned char *intc_evt2irq_table;
662 unsigned int intc_evt2irq(unsigned int vector)
664 unsigned int irq = evt2irq(vector);
666 if (intc_evt2irq_table && intc_evt2irq_table[irq])
667 irq = intc_evt2irq_table[irq];
672 void __init register_intc_controller(struct intc_desc *desc)
674 unsigned int i, k, smp;
675 struct intc_desc_int *d;
677 d = alloc_bootmem(sizeof(*d));
679 INIT_LIST_HEAD(&d->list);
680 list_add(&d->list, &intc_list);
682 d->nr_reg = desc->mask_regs ? desc->nr_mask_regs * 2 : 0;
683 d->nr_reg += desc->prio_regs ? desc->nr_prio_regs * 2 : 0;
684 d->nr_reg += desc->sense_regs ? desc->nr_sense_regs : 0;
686 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
687 d->nr_reg += desc->ack_regs ? desc->nr_ack_regs : 0;
689 d->reg = alloc_bootmem(d->nr_reg * sizeof(*d->reg));
691 d->smp = alloc_bootmem(d->nr_reg * sizeof(*d->smp));
695 if (desc->mask_regs) {
696 for (i = 0; i < desc->nr_mask_regs; i++) {
697 smp = IS_SMP(desc->mask_regs[i]);
698 k += save_reg(d, k, desc->mask_regs[i].set_reg, smp);
699 k += save_reg(d, k, desc->mask_regs[i].clr_reg, smp);
703 if (desc->prio_regs) {
704 d->prio = alloc_bootmem(desc->nr_vectors * sizeof(*d->prio));
706 for (i = 0; i < desc->nr_prio_regs; i++) {
707 smp = IS_SMP(desc->prio_regs[i]);
708 k += save_reg(d, k, desc->prio_regs[i].set_reg, smp);
709 k += save_reg(d, k, desc->prio_regs[i].clr_reg, smp);
713 if (desc->sense_regs) {
714 d->sense = alloc_bootmem(desc->nr_vectors * sizeof(*d->sense));
716 for (i = 0; i < desc->nr_sense_regs; i++) {
717 k += save_reg(d, k, desc->sense_regs[i].reg, 0);
721 d->chip.name = desc->name;
722 d->chip.mask = intc_disable;
723 d->chip.unmask = intc_enable;
724 d->chip.mask_ack = intc_disable;
725 d->chip.enable = intc_enable;
726 d->chip.disable = intc_disable;
727 d->chip.shutdown = intc_disable;
728 d->chip.set_type = intc_set_sense;
729 d->chip.set_wake = intc_set_wake;
731 #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
732 if (desc->ack_regs) {
733 for (i = 0; i < desc->nr_ack_regs; i++)
734 k += save_reg(d, k, desc->ack_regs[i].set_reg, 0);
736 d->chip.mask_ack = intc_mask_ack;
740 BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */
742 /* keep the first vector only if same enum is used multiple times */
743 for (i = 0; i < desc->nr_vectors; i++) {
744 struct intc_vect *vect = desc->vectors + i;
745 int first_irq = evt2irq(vect->vect);
750 for (k = i + 1; k < desc->nr_vectors; k++) {
751 struct intc_vect *vect2 = desc->vectors + k;
753 if (vect->enum_id != vect2->enum_id)
758 if (!intc_evt2irq_table)
759 intc_evt2irq_table = alloc_bootmem(NR_IRQS);
761 if (!intc_evt2irq_table) {
762 pr_warning("intc: cannot allocate evt2irq!\n");
766 intc_evt2irq_table[evt2irq(vect2->vect)] = first_irq;
770 /* register the vectors one by one */
771 for (i = 0; i < desc->nr_vectors; i++) {
772 struct intc_vect *vect = desc->vectors + i;
777 intc_register_irq(desc, d, vect->enum_id, evt2irq(vect->vect));
781 static int intc_suspend(struct sys_device *dev, pm_message_t state)
783 struct intc_desc_int *d;
784 struct irq_desc *desc;
787 /* get intc controller associated with this sysdev */
788 d = container_of(dev, struct intc_desc_int, sysdev);
790 switch (state.event) {
792 if (d->state.event != PM_EVENT_FREEZE)
794 for_each_irq_desc(irq, desc) {
795 if (desc->chip != &d->chip)
797 if (desc->status & IRQ_DISABLED)
803 case PM_EVENT_FREEZE:
804 /* nothing has to be done */
806 case PM_EVENT_SUSPEND:
807 /* enable wakeup irqs belonging to this intc controller */
808 for_each_irq_desc(irq, desc) {
809 if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
819 static int intc_resume(struct sys_device *dev)
821 return intc_suspend(dev, PMSG_ON);
824 static struct sysdev_class intc_sysdev_class = {
826 .suspend = intc_suspend,
827 .resume = intc_resume,
830 /* register this intc as sysdev to allow suspend/resume */
831 static int __init register_intc_sysdevs(void)
833 struct intc_desc_int *d;
837 error = sysdev_class_register(&intc_sysdev_class);
839 list_for_each_entry(d, &intc_list, list) {
841 d->sysdev.cls = &intc_sysdev_class;
842 error = sysdev_register(&d->sysdev);
850 pr_warning("intc: sysdev registration error\n");
855 device_initcall(register_intc_sysdevs);