12be6bb0cSPaul Mundt /*
22be6bb0cSPaul Mundt * Shared interrupt handling code for IPR and INTC2 types of IRQs.
32be6bb0cSPaul Mundt *
42be6bb0cSPaul Mundt * Copyright (C) 2007, 2008 Magnus Damm
52be6bb0cSPaul Mundt * Copyright (C) 2009, 2010 Paul Mundt
62be6bb0cSPaul Mundt *
72be6bb0cSPaul Mundt * This file is subject to the terms and conditions of the GNU General Public
82be6bb0cSPaul Mundt * License. See the file "COPYING" in the main directory of this archive
92be6bb0cSPaul Mundt * for more details.
102be6bb0cSPaul Mundt */
112be6bb0cSPaul Mundt #include <linux/init.h>
122be6bb0cSPaul Mundt #include <linux/irq.h>
132be6bb0cSPaul Mundt #include <linux/spinlock.h>
142be6bb0cSPaul Mundt #include "internals.h"
152be6bb0cSPaul Mundt
160f552393SRob Herring static unsigned long ack_handle[INTC_NR_IRQS];
172be6bb0cSPaul Mundt
intc_grp_id(struct intc_desc * desc,intc_enum enum_id)182be6bb0cSPaul Mundt static intc_enum __init intc_grp_id(struct intc_desc *desc,
192be6bb0cSPaul Mundt intc_enum enum_id)
202be6bb0cSPaul Mundt {
212be6bb0cSPaul Mundt struct intc_group *g = desc->hw.groups;
222be6bb0cSPaul Mundt unsigned int i, j;
232be6bb0cSPaul Mundt
242be6bb0cSPaul Mundt for (i = 0; g && enum_id && i < desc->hw.nr_groups; i++) {
252be6bb0cSPaul Mundt g = desc->hw.groups + i;
262be6bb0cSPaul Mundt
272be6bb0cSPaul Mundt for (j = 0; g->enum_ids[j]; j++) {
282be6bb0cSPaul Mundt if (g->enum_ids[j] != enum_id)
292be6bb0cSPaul Mundt continue;
302be6bb0cSPaul Mundt
312be6bb0cSPaul Mundt return g->enum_id;
322be6bb0cSPaul Mundt }
332be6bb0cSPaul Mundt }
342be6bb0cSPaul Mundt
352be6bb0cSPaul Mundt return 0;
362be6bb0cSPaul Mundt }
372be6bb0cSPaul Mundt
_intc_mask_data(struct intc_desc * desc,struct intc_desc_int * d,intc_enum enum_id,unsigned int * reg_idx,unsigned int * fld_idx)382be6bb0cSPaul Mundt static unsigned int __init _intc_mask_data(struct intc_desc *desc,
392be6bb0cSPaul Mundt struct intc_desc_int *d,
402be6bb0cSPaul Mundt intc_enum enum_id,
412be6bb0cSPaul Mundt unsigned int *reg_idx,
422be6bb0cSPaul Mundt unsigned int *fld_idx)
432be6bb0cSPaul Mundt {
442be6bb0cSPaul Mundt struct intc_mask_reg *mr = desc->hw.mask_regs;
452be6bb0cSPaul Mundt unsigned int fn, mode;
462be6bb0cSPaul Mundt unsigned long reg_e, reg_d;
472be6bb0cSPaul Mundt
482be6bb0cSPaul Mundt while (mr && enum_id && *reg_idx < desc->hw.nr_mask_regs) {
492be6bb0cSPaul Mundt mr = desc->hw.mask_regs + *reg_idx;
502be6bb0cSPaul Mundt
512be6bb0cSPaul Mundt for (; *fld_idx < ARRAY_SIZE(mr->enum_ids); (*fld_idx)++) {
522be6bb0cSPaul Mundt if (mr->enum_ids[*fld_idx] != enum_id)
532be6bb0cSPaul Mundt continue;
542be6bb0cSPaul Mundt
552be6bb0cSPaul Mundt if (mr->set_reg && mr->clr_reg) {
562be6bb0cSPaul Mundt fn = REG_FN_WRITE_BASE;
572be6bb0cSPaul Mundt mode = MODE_DUAL_REG;
582be6bb0cSPaul Mundt reg_e = mr->clr_reg;
592be6bb0cSPaul Mundt reg_d = mr->set_reg;
602be6bb0cSPaul Mundt } else {
612be6bb0cSPaul Mundt fn = REG_FN_MODIFY_BASE;
622be6bb0cSPaul Mundt if (mr->set_reg) {
632be6bb0cSPaul Mundt mode = MODE_ENABLE_REG;
642be6bb0cSPaul Mundt reg_e = mr->set_reg;
652be6bb0cSPaul Mundt reg_d = mr->set_reg;
662be6bb0cSPaul Mundt } else {
672be6bb0cSPaul Mundt mode = MODE_MASK_REG;
682be6bb0cSPaul Mundt reg_e = mr->clr_reg;
692be6bb0cSPaul Mundt reg_d = mr->clr_reg;
702be6bb0cSPaul Mundt }
712be6bb0cSPaul Mundt }
722be6bb0cSPaul Mundt
732be6bb0cSPaul Mundt fn += (mr->reg_width >> 3) - 1;
742be6bb0cSPaul Mundt return _INTC_MK(fn, mode,
752be6bb0cSPaul Mundt intc_get_reg(d, reg_e),
762be6bb0cSPaul Mundt intc_get_reg(d, reg_d),
772be6bb0cSPaul Mundt 1,
782be6bb0cSPaul Mundt (mr->reg_width - 1) - *fld_idx);
792be6bb0cSPaul Mundt }
802be6bb0cSPaul Mundt
812be6bb0cSPaul Mundt *fld_idx = 0;
822be6bb0cSPaul Mundt (*reg_idx)++;
832be6bb0cSPaul Mundt }
842be6bb0cSPaul Mundt
852be6bb0cSPaul Mundt return 0;
862be6bb0cSPaul Mundt }
872be6bb0cSPaul Mundt
882be6bb0cSPaul Mundt unsigned int __init
intc_get_mask_handle(struct intc_desc * desc,struct intc_desc_int * d,intc_enum enum_id,int do_grps)892be6bb0cSPaul Mundt intc_get_mask_handle(struct intc_desc *desc, struct intc_desc_int *d,
902be6bb0cSPaul Mundt intc_enum enum_id, int do_grps)
912be6bb0cSPaul Mundt {
922be6bb0cSPaul Mundt unsigned int i = 0;
932be6bb0cSPaul Mundt unsigned int j = 0;
942be6bb0cSPaul Mundt unsigned int ret;
952be6bb0cSPaul Mundt
962be6bb0cSPaul Mundt ret = _intc_mask_data(desc, d, enum_id, &i, &j);
972be6bb0cSPaul Mundt if (ret)
982be6bb0cSPaul Mundt return ret;
992be6bb0cSPaul Mundt
1002be6bb0cSPaul Mundt if (do_grps)
1012be6bb0cSPaul Mundt return intc_get_mask_handle(desc, d, intc_grp_id(desc, enum_id), 0);
1022be6bb0cSPaul Mundt
1032be6bb0cSPaul Mundt return 0;
1042be6bb0cSPaul Mundt }
1052be6bb0cSPaul Mundt
_intc_prio_data(struct intc_desc * desc,struct intc_desc_int * d,intc_enum enum_id,unsigned int * reg_idx,unsigned int * fld_idx)1062be6bb0cSPaul Mundt static unsigned int __init _intc_prio_data(struct intc_desc *desc,
1072be6bb0cSPaul Mundt struct intc_desc_int *d,
1082be6bb0cSPaul Mundt intc_enum enum_id,
1092be6bb0cSPaul Mundt unsigned int *reg_idx,
1102be6bb0cSPaul Mundt unsigned int *fld_idx)
1112be6bb0cSPaul Mundt {
1122be6bb0cSPaul Mundt struct intc_prio_reg *pr = desc->hw.prio_regs;
1132be6bb0cSPaul Mundt unsigned int fn, n, mode, bit;
1142be6bb0cSPaul Mundt unsigned long reg_e, reg_d;
1152be6bb0cSPaul Mundt
1162be6bb0cSPaul Mundt while (pr && enum_id && *reg_idx < desc->hw.nr_prio_regs) {
1172be6bb0cSPaul Mundt pr = desc->hw.prio_regs + *reg_idx;
1182be6bb0cSPaul Mundt
1192be6bb0cSPaul Mundt for (; *fld_idx < ARRAY_SIZE(pr->enum_ids); (*fld_idx)++) {
1202be6bb0cSPaul Mundt if (pr->enum_ids[*fld_idx] != enum_id)
1212be6bb0cSPaul Mundt continue;
1222be6bb0cSPaul Mundt
1232be6bb0cSPaul Mundt if (pr->set_reg && pr->clr_reg) {
1242be6bb0cSPaul Mundt fn = REG_FN_WRITE_BASE;
1252be6bb0cSPaul Mundt mode = MODE_PCLR_REG;
1262be6bb0cSPaul Mundt reg_e = pr->set_reg;
1272be6bb0cSPaul Mundt reg_d = pr->clr_reg;
1282be6bb0cSPaul Mundt } else {
1292be6bb0cSPaul Mundt fn = REG_FN_MODIFY_BASE;
1302be6bb0cSPaul Mundt mode = MODE_PRIO_REG;
1312be6bb0cSPaul Mundt if (!pr->set_reg)
1322be6bb0cSPaul Mundt BUG();
1332be6bb0cSPaul Mundt reg_e = pr->set_reg;
1342be6bb0cSPaul Mundt reg_d = pr->set_reg;
1352be6bb0cSPaul Mundt }
1362be6bb0cSPaul Mundt
1372be6bb0cSPaul Mundt fn += (pr->reg_width >> 3) - 1;
1382be6bb0cSPaul Mundt n = *fld_idx + 1;
1392be6bb0cSPaul Mundt
1402be6bb0cSPaul Mundt BUG_ON(n * pr->field_width > pr->reg_width);
1412be6bb0cSPaul Mundt
1422be6bb0cSPaul Mundt bit = pr->reg_width - (n * pr->field_width);
1432be6bb0cSPaul Mundt
1442be6bb0cSPaul Mundt return _INTC_MK(fn, mode,
1452be6bb0cSPaul Mundt intc_get_reg(d, reg_e),
1462be6bb0cSPaul Mundt intc_get_reg(d, reg_d),
1472be6bb0cSPaul Mundt pr->field_width, bit);
1482be6bb0cSPaul Mundt }
1492be6bb0cSPaul Mundt
1502be6bb0cSPaul Mundt *fld_idx = 0;
1512be6bb0cSPaul Mundt (*reg_idx)++;
1522be6bb0cSPaul Mundt }
1532be6bb0cSPaul Mundt
1542be6bb0cSPaul Mundt return 0;
1552be6bb0cSPaul Mundt }
1562be6bb0cSPaul Mundt
1572be6bb0cSPaul Mundt unsigned int __init
intc_get_prio_handle(struct intc_desc * desc,struct intc_desc_int * d,intc_enum enum_id,int do_grps)1582be6bb0cSPaul Mundt intc_get_prio_handle(struct intc_desc *desc, struct intc_desc_int *d,
1592be6bb0cSPaul Mundt intc_enum enum_id, int do_grps)
1602be6bb0cSPaul Mundt {
1612be6bb0cSPaul Mundt unsigned int i = 0;
1622be6bb0cSPaul Mundt unsigned int j = 0;
1632be6bb0cSPaul Mundt unsigned int ret;
1642be6bb0cSPaul Mundt
1652be6bb0cSPaul Mundt ret = _intc_prio_data(desc, d, enum_id, &i, &j);
1662be6bb0cSPaul Mundt if (ret)
1672be6bb0cSPaul Mundt return ret;
1682be6bb0cSPaul Mundt
1692be6bb0cSPaul Mundt if (do_grps)
1702be6bb0cSPaul Mundt return intc_get_prio_handle(desc, d, intc_grp_id(desc, enum_id), 0);
1712be6bb0cSPaul Mundt
1722be6bb0cSPaul Mundt return 0;
1732be6bb0cSPaul Mundt }
1742be6bb0cSPaul Mundt
intc_ack_data(struct intc_desc * desc,struct intc_desc_int * d,intc_enum enum_id)175*b448d6adSPaul Mundt static unsigned int intc_ack_data(struct intc_desc *desc,
176*b448d6adSPaul Mundt struct intc_desc_int *d, intc_enum enum_id)
1772be6bb0cSPaul Mundt {
1782be6bb0cSPaul Mundt struct intc_mask_reg *mr = desc->hw.ack_regs;
1792be6bb0cSPaul Mundt unsigned int i, j, fn, mode;
1802be6bb0cSPaul Mundt unsigned long reg_e, reg_d;
1812be6bb0cSPaul Mundt
1822be6bb0cSPaul Mundt for (i = 0; mr && enum_id && i < desc->hw.nr_ack_regs; i++) {
1832be6bb0cSPaul Mundt mr = desc->hw.ack_regs + i;
1842be6bb0cSPaul Mundt
1852be6bb0cSPaul Mundt for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
1862be6bb0cSPaul Mundt if (mr->enum_ids[j] != enum_id)
1872be6bb0cSPaul Mundt continue;
1882be6bb0cSPaul Mundt
1892be6bb0cSPaul Mundt fn = REG_FN_MODIFY_BASE;
1902be6bb0cSPaul Mundt mode = MODE_ENABLE_REG;
1912be6bb0cSPaul Mundt reg_e = mr->set_reg;
1922be6bb0cSPaul Mundt reg_d = mr->set_reg;
1932be6bb0cSPaul Mundt
1942be6bb0cSPaul Mundt fn += (mr->reg_width >> 3) - 1;
1952be6bb0cSPaul Mundt return _INTC_MK(fn, mode,
1962be6bb0cSPaul Mundt intc_get_reg(d, reg_e),
1972be6bb0cSPaul Mundt intc_get_reg(d, reg_d),
1982be6bb0cSPaul Mundt 1,
1992be6bb0cSPaul Mundt (mr->reg_width - 1) - j);
2002be6bb0cSPaul Mundt }
2012be6bb0cSPaul Mundt }
2022be6bb0cSPaul Mundt
2032be6bb0cSPaul Mundt return 0;
2042be6bb0cSPaul Mundt }
2052be6bb0cSPaul Mundt
intc_enable_disable(struct intc_desc_int * d,unsigned long handle,int do_enable)2062be6bb0cSPaul Mundt static void intc_enable_disable(struct intc_desc_int *d,
2072be6bb0cSPaul Mundt unsigned long handle, int do_enable)
2082be6bb0cSPaul Mundt {
2092be6bb0cSPaul Mundt unsigned long addr;
2102be6bb0cSPaul Mundt unsigned int cpu;
2112be6bb0cSPaul Mundt unsigned long (*fn)(unsigned long, unsigned long,
2122be6bb0cSPaul Mundt unsigned long (*)(unsigned long, unsigned long,
2132be6bb0cSPaul Mundt unsigned long),
2142be6bb0cSPaul Mundt unsigned int);
2152be6bb0cSPaul Mundt
2162be6bb0cSPaul Mundt if (do_enable) {
2172be6bb0cSPaul Mundt for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
2182be6bb0cSPaul Mundt addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
2192be6bb0cSPaul Mundt fn = intc_enable_noprio_fns[_INTC_MODE(handle)];
2202be6bb0cSPaul Mundt fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
2212be6bb0cSPaul Mundt }
2222be6bb0cSPaul Mundt } else {
2232be6bb0cSPaul Mundt for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
2242be6bb0cSPaul Mundt addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
2252be6bb0cSPaul Mundt fn = intc_disable_fns[_INTC_MODE(handle)];
2262be6bb0cSPaul Mundt fn(addr, handle, intc_reg_fns[_INTC_FN(handle)], 0);
2272be6bb0cSPaul Mundt }
2282be6bb0cSPaul Mundt }
2292be6bb0cSPaul Mundt }
2302be6bb0cSPaul Mundt
intc_enable_disable_enum(struct intc_desc * desc,struct intc_desc_int * d,intc_enum enum_id,int enable)2312be6bb0cSPaul Mundt void __init intc_enable_disable_enum(struct intc_desc *desc,
2322be6bb0cSPaul Mundt struct intc_desc_int *d,
2332be6bb0cSPaul Mundt intc_enum enum_id, int enable)
2342be6bb0cSPaul Mundt {
2352be6bb0cSPaul Mundt unsigned int i, j, data;
2362be6bb0cSPaul Mundt
2372be6bb0cSPaul Mundt /* go through and enable/disable all mask bits */
2382be6bb0cSPaul Mundt i = j = 0;
2392be6bb0cSPaul Mundt do {
2402be6bb0cSPaul Mundt data = _intc_mask_data(desc, d, enum_id, &i, &j);
2412be6bb0cSPaul Mundt if (data)
2422be6bb0cSPaul Mundt intc_enable_disable(d, data, enable);
2432be6bb0cSPaul Mundt j++;
2442be6bb0cSPaul Mundt } while (data);
2452be6bb0cSPaul Mundt
2462be6bb0cSPaul Mundt /* go through and enable/disable all priority fields */
2472be6bb0cSPaul Mundt i = j = 0;
2482be6bb0cSPaul Mundt do {
2492be6bb0cSPaul Mundt data = _intc_prio_data(desc, d, enum_id, &i, &j);
2502be6bb0cSPaul Mundt if (data)
2512be6bb0cSPaul Mundt intc_enable_disable(d, data, enable);
2522be6bb0cSPaul Mundt
2532be6bb0cSPaul Mundt j++;
2542be6bb0cSPaul Mundt } while (data);
2552be6bb0cSPaul Mundt }
2562be6bb0cSPaul Mundt
2572be6bb0cSPaul Mundt unsigned int __init
intc_get_sense_handle(struct intc_desc * desc,struct intc_desc_int * d,intc_enum enum_id)2582be6bb0cSPaul Mundt intc_get_sense_handle(struct intc_desc *desc, struct intc_desc_int *d,
2592be6bb0cSPaul Mundt intc_enum enum_id)
2602be6bb0cSPaul Mundt {
2612be6bb0cSPaul Mundt struct intc_sense_reg *sr = desc->hw.sense_regs;
2622be6bb0cSPaul Mundt unsigned int i, j, fn, bit;
2632be6bb0cSPaul Mundt
2642be6bb0cSPaul Mundt for (i = 0; sr && enum_id && i < desc->hw.nr_sense_regs; i++) {
2652be6bb0cSPaul Mundt sr = desc->hw.sense_regs + i;
2662be6bb0cSPaul Mundt
2672be6bb0cSPaul Mundt for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
2682be6bb0cSPaul Mundt if (sr->enum_ids[j] != enum_id)
2692be6bb0cSPaul Mundt continue;
2702be6bb0cSPaul Mundt
2712be6bb0cSPaul Mundt fn = REG_FN_MODIFY_BASE;
2722be6bb0cSPaul Mundt fn += (sr->reg_width >> 3) - 1;
2732be6bb0cSPaul Mundt
2742be6bb0cSPaul Mundt BUG_ON((j + 1) * sr->field_width > sr->reg_width);
2752be6bb0cSPaul Mundt
2762be6bb0cSPaul Mundt bit = sr->reg_width - ((j + 1) * sr->field_width);
2772be6bb0cSPaul Mundt
2782be6bb0cSPaul Mundt return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
2792be6bb0cSPaul Mundt 0, sr->field_width, bit);
2802be6bb0cSPaul Mundt }
2812be6bb0cSPaul Mundt }
2822be6bb0cSPaul Mundt
2832be6bb0cSPaul Mundt return 0;
2842be6bb0cSPaul Mundt }
2852be6bb0cSPaul Mundt
2862be6bb0cSPaul Mundt
intc_set_ack_handle(unsigned int irq,struct intc_desc * desc,struct intc_desc_int * d,intc_enum id)2872be6bb0cSPaul Mundt void intc_set_ack_handle(unsigned int irq, struct intc_desc *desc,
2882be6bb0cSPaul Mundt struct intc_desc_int *d, intc_enum id)
2892be6bb0cSPaul Mundt {
2902be6bb0cSPaul Mundt unsigned long flags;
2912be6bb0cSPaul Mundt
2922be6bb0cSPaul Mundt /*
2932be6bb0cSPaul Mundt * Nothing to do for this IRQ.
2942be6bb0cSPaul Mundt */
2952be6bb0cSPaul Mundt if (!desc->hw.ack_regs)
2962be6bb0cSPaul Mundt return;
2972be6bb0cSPaul Mundt
2982be6bb0cSPaul Mundt raw_spin_lock_irqsave(&intc_big_lock, flags);
2992be6bb0cSPaul Mundt ack_handle[irq] = intc_ack_data(desc, d, id);
3002be6bb0cSPaul Mundt raw_spin_unlock_irqrestore(&intc_big_lock, flags);
3012be6bb0cSPaul Mundt }
3022be6bb0cSPaul Mundt
intc_get_ack_handle(unsigned int irq)3032be6bb0cSPaul Mundt unsigned long intc_get_ack_handle(unsigned int irq)
3042be6bb0cSPaul Mundt {
3052be6bb0cSPaul Mundt return ack_handle[irq];
3062be6bb0cSPaul Mundt }
307