Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Shared interrupt handling code for IPR and INTC2 types of IRQs. |
| 3 | * |
Magnus Damm | d58876e | 2008-04-24 21:36:34 +0900 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008 Magnus Damm |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 5 | * |
| 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> |
Paul Mundt | bbfbd8b | 2008-10-01 16:13:54 +0900 | [diff] [blame] | 23 | #include <linux/sh_intc.h> |
Magnus Damm | 2dcec7a | 2009-04-01 14:30:59 +0000 | [diff] [blame] | 24 | #include <linux/sysdev.h> |
| 25 | #include <linux/list.h> |
Paul Mundt | 54ff328 | 2009-06-11 10:33:09 +0300 | [diff] [blame] | 26 | #include <linux/topology.h> |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 27 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 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))) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 31 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 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) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 38 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 39 | struct intc_handle_int { |
| 40 | unsigned int irq; |
| 41 | unsigned long handle; |
| 42 | }; |
| 43 | |
| 44 | struct intc_desc_int { |
Magnus Damm | 2dcec7a | 2009-04-01 14:30:59 +0000 | [diff] [blame] | 45 | struct list_head list; |
| 46 | struct sys_device sysdev; |
Francesco VIRLINZI | 7fd87b3 | 2009-04-06 07:17:04 +0000 | [diff] [blame] | 47 | pm_message_t state; |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 48 | unsigned long *reg; |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 49 | #ifdef CONFIG_SMP |
| 50 | unsigned long *smp; |
| 51 | #endif |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 52 | unsigned int nr_reg; |
| 53 | struct intc_handle_int *prio; |
| 54 | unsigned int nr_prio; |
| 55 | struct intc_handle_int *sense; |
| 56 | unsigned int nr_sense; |
| 57 | struct irq_chip chip; |
| 58 | }; |
| 59 | |
Magnus Damm | 2dcec7a | 2009-04-01 14:30:59 +0000 | [diff] [blame] | 60 | static LIST_HEAD(intc_list); |
| 61 | |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 62 | #ifdef CONFIG_SMP |
| 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) |
| 66 | #else |
| 67 | #define IS_SMP(x) 0 |
| 68 | #define INTC_REG(d, x, c) (d->reg[(x)]) |
| 69 | #define SMP_NR(d, x) 1 |
| 70 | #endif |
| 71 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 72 | static unsigned int intc_prio_level[NR_IRQS]; /* for now */ |
Yoshihiro Shimoda | 6bdfb22 | 2008-07-04 12:37:12 +0900 | [diff] [blame] | 73 | #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A) |
Magnus Damm | d58876e | 2008-04-24 21:36:34 +0900 | [diff] [blame] | 74 | static unsigned long ack_handle[NR_IRQS]; |
| 75 | #endif |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 76 | |
| 77 | static inline struct intc_desc_int *get_intc_desc(unsigned int irq) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 78 | { |
| 79 | struct irq_chip *chip = get_irq_chip(irq); |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 80 | return (void *)((char *)chip - offsetof(struct intc_desc_int, chip)); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static inline unsigned int set_field(unsigned int value, |
| 84 | unsigned int field_value, |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 85 | unsigned int handle) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 86 | { |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 87 | unsigned int width = _INTC_WIDTH(handle); |
| 88 | unsigned int shift = _INTC_SHIFT(handle); |
| 89 | |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 90 | value &= ~(((1 << width) - 1) << shift); |
| 91 | value |= field_value << shift; |
| 92 | return value; |
| 93 | } |
| 94 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 95 | static void write_8(unsigned long addr, unsigned long h, unsigned long data) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 96 | { |
Paul Mundt | 62429e0 | 2008-10-01 15:19:10 +0900 | [diff] [blame] | 97 | __raw_writeb(set_field(0, data, h), addr); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 98 | } |
| 99 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 100 | static void write_16(unsigned long addr, unsigned long h, unsigned long data) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 101 | { |
Paul Mundt | 62429e0 | 2008-10-01 15:19:10 +0900 | [diff] [blame] | 102 | __raw_writew(set_field(0, data, h), addr); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 103 | } |
| 104 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 105 | static void write_32(unsigned long addr, unsigned long h, unsigned long data) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 106 | { |
Paul Mundt | 62429e0 | 2008-10-01 15:19:10 +0900 | [diff] [blame] | 107 | __raw_writel(set_field(0, data, h), addr); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 108 | } |
| 109 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 110 | static void modify_8(unsigned long addr, unsigned long h, unsigned long data) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 111 | { |
Magnus Damm | 4370fe1 | 2008-04-24 21:53:07 +0900 | [diff] [blame] | 112 | unsigned long flags; |
| 113 | local_irq_save(flags); |
Paul Mundt | 62429e0 | 2008-10-01 15:19:10 +0900 | [diff] [blame] | 114 | __raw_writeb(set_field(__raw_readb(addr), data, h), addr); |
Magnus Damm | 4370fe1 | 2008-04-24 21:53:07 +0900 | [diff] [blame] | 115 | local_irq_restore(flags); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 116 | } |
| 117 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 118 | static void modify_16(unsigned long addr, unsigned long h, unsigned long data) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 119 | { |
Magnus Damm | 4370fe1 | 2008-04-24 21:53:07 +0900 | [diff] [blame] | 120 | unsigned long flags; |
| 121 | local_irq_save(flags); |
Paul Mundt | 62429e0 | 2008-10-01 15:19:10 +0900 | [diff] [blame] | 122 | __raw_writew(set_field(__raw_readw(addr), data, h), addr); |
Magnus Damm | 4370fe1 | 2008-04-24 21:53:07 +0900 | [diff] [blame] | 123 | local_irq_restore(flags); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 124 | } |
| 125 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 126 | static void modify_32(unsigned long addr, unsigned long h, unsigned long data) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 127 | { |
Magnus Damm | 4370fe1 | 2008-04-24 21:53:07 +0900 | [diff] [blame] | 128 | unsigned long flags; |
| 129 | local_irq_save(flags); |
Paul Mundt | 62429e0 | 2008-10-01 15:19:10 +0900 | [diff] [blame] | 130 | __raw_writel(set_field(__raw_readl(addr), data, h), addr); |
Magnus Damm | 4370fe1 | 2008-04-24 21:53:07 +0900 | [diff] [blame] | 131 | local_irq_restore(flags); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 132 | } |
| 133 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 134 | enum { REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 }; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 135 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 136 | static void (*intc_reg_fns[])(unsigned long addr, |
| 137 | unsigned long h, |
| 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, |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 145 | }; |
| 146 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 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 */ |
| 152 | }; |
| 153 | |
| 154 | static void intc_mode_field(unsigned long addr, |
| 155 | unsigned long handle, |
| 156 | void (*fn)(unsigned long, |
| 157 | unsigned long, |
| 158 | unsigned long), |
| 159 | unsigned int irq) |
| 160 | { |
| 161 | fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1)); |
| 162 | } |
| 163 | |
| 164 | static void intc_mode_zero(unsigned long addr, |
| 165 | unsigned long handle, |
| 166 | void (*fn)(unsigned long, |
| 167 | unsigned long, |
| 168 | unsigned long), |
| 169 | unsigned int irq) |
| 170 | { |
| 171 | fn(addr, handle, 0); |
| 172 | } |
| 173 | |
| 174 | static void intc_mode_prio(unsigned long addr, |
| 175 | unsigned long handle, |
| 176 | void (*fn)(unsigned long, |
| 177 | unsigned long, |
| 178 | unsigned long), |
| 179 | unsigned int irq) |
| 180 | { |
| 181 | fn(addr, handle, intc_prio_level[irq]); |
| 182 | } |
| 183 | |
| 184 | static void (*intc_enable_fns[])(unsigned long addr, |
| 185 | unsigned long handle, |
| 186 | void (*fn)(unsigned long, |
| 187 | unsigned long, |
| 188 | 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, |
| 195 | }; |
| 196 | |
| 197 | static void (*intc_disable_fns[])(unsigned long addr, |
| 198 | unsigned long handle, |
| 199 | void (*fn)(unsigned long, |
| 200 | unsigned long, |
| 201 | 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, |
| 208 | }; |
| 209 | |
| 210 | static inline void _intc_enable(unsigned int irq, unsigned long handle) |
| 211 | { |
| 212 | struct intc_desc_int *d = get_intc_desc(irq); |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 213 | unsigned long addr; |
| 214 | unsigned int cpu; |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 215 | |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 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); |
| 220 | } |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 221 | } |
| 222 | |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 223 | static void intc_enable(unsigned int irq) |
| 224 | { |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 225 | _intc_enable(irq, (unsigned long)get_irq_chip_data(irq)); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void intc_disable(unsigned int irq) |
| 229 | { |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 230 | struct intc_desc_int *d = get_intc_desc(irq); |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 231 | unsigned long handle = (unsigned long) get_irq_chip_data(irq); |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 232 | unsigned long addr; |
| 233 | unsigned int cpu; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 234 | |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 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); |
| 239 | } |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 240 | } |
| 241 | |
Magnus Damm | 2dcec7a | 2009-04-01 14:30:59 +0000 | [diff] [blame] | 242 | static int intc_set_wake(unsigned int irq, unsigned int on) |
| 243 | { |
| 244 | return 0; /* allow wakeup, but setup hardware in intc_suspend() */ |
| 245 | } |
| 246 | |
Yoshihiro Shimoda | 6bdfb22 | 2008-07-04 12:37:12 +0900 | [diff] [blame] | 247 | #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A) |
Magnus Damm | d58876e | 2008-04-24 21:36:34 +0900 | [diff] [blame] | 248 | static void intc_mask_ack(unsigned int irq) |
| 249 | { |
| 250 | struct intc_desc_int *d = get_intc_desc(irq); |
| 251 | unsigned long handle = ack_handle[irq]; |
| 252 | unsigned long addr; |
| 253 | |
| 254 | intc_disable(irq); |
| 255 | |
| 256 | /* read register and write zero only to the assocaited bit */ |
| 257 | |
| 258 | if (handle) { |
| 259 | addr = INTC_REG(d, _INTC_ADDR_D(handle), 0); |
Yoshihiro Shimoda | 6bdfb22 | 2008-07-04 12:37:12 +0900 | [diff] [blame] | 260 | switch (_INTC_FN(handle)) { |
| 261 | case REG_FN_MODIFY_BASE + 0: /* 8bit */ |
Paul Mundt | 62429e0 | 2008-10-01 15:19:10 +0900 | [diff] [blame] | 262 | __raw_readb(addr); |
| 263 | __raw_writeb(0xff ^ set_field(0, 1, handle), addr); |
Yoshihiro Shimoda | 6bdfb22 | 2008-07-04 12:37:12 +0900 | [diff] [blame] | 264 | break; |
| 265 | case REG_FN_MODIFY_BASE + 1: /* 16bit */ |
Paul Mundt | 62429e0 | 2008-10-01 15:19:10 +0900 | [diff] [blame] | 266 | __raw_readw(addr); |
| 267 | __raw_writew(0xffff ^ set_field(0, 1, handle), addr); |
Yoshihiro Shimoda | 6bdfb22 | 2008-07-04 12:37:12 +0900 | [diff] [blame] | 268 | break; |
| 269 | case REG_FN_MODIFY_BASE + 3: /* 32bit */ |
Paul Mundt | 62429e0 | 2008-10-01 15:19:10 +0900 | [diff] [blame] | 270 | __raw_readl(addr); |
| 271 | __raw_writel(0xffffffff ^ set_field(0, 1, handle), addr); |
Yoshihiro Shimoda | 6bdfb22 | 2008-07-04 12:37:12 +0900 | [diff] [blame] | 272 | break; |
| 273 | default: |
| 274 | BUG(); |
| 275 | break; |
| 276 | } |
Magnus Damm | d58876e | 2008-04-24 21:36:34 +0900 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | #endif |
| 280 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 281 | static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp, |
| 282 | unsigned int nr_hp, |
| 283 | unsigned int irq) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 284 | { |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 285 | int i; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 286 | |
Magnus Damm | 3d37d94 | 2007-08-17 00:50:44 +0900 | [diff] [blame] | 287 | /* this doesn't scale well, but... |
| 288 | * |
| 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. |
| 293 | * |
| 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. |
| 297 | */ |
| 298 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 299 | for (i = 0; i < nr_hp; i++) { |
| 300 | if ((hp + i)->irq != irq) |
| 301 | continue; |
| 302 | |
| 303 | return hp + i; |
| 304 | } |
| 305 | |
| 306 | return NULL; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 307 | } |
| 308 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 309 | int intc_set_priority(unsigned int irq, unsigned int prio) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 310 | { |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 311 | struct intc_desc_int *d = get_intc_desc(irq); |
| 312 | struct intc_handle_int *ihp; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 313 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 314 | if (!intc_prio_level[irq] || prio <= 1) |
| 315 | return -EINVAL; |
| 316 | |
| 317 | ihp = intc_find_irq(d->prio, d->nr_prio, irq); |
| 318 | if (ihp) { |
Magnus Damm | 3d37d94 | 2007-08-17 00:50:44 +0900 | [diff] [blame] | 319 | if (prio >= (1 << _INTC_WIDTH(ihp->handle))) |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 320 | return -EINVAL; |
| 321 | |
| 322 | intc_prio_level[irq] = prio; |
| 323 | |
| 324 | /* |
| 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() |
| 328 | */ |
| 329 | |
Magnus Damm | 3d37d94 | 2007-08-17 00:50:44 +0900 | [diff] [blame] | 330 | if (_INTC_FN(ihp->handle) != REG_FN_ERR) |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 331 | _intc_enable(irq, ihp->handle); |
| 332 | } |
| 333 | return 0; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | #define VALID(x) (x | 0x80) |
| 337 | |
| 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), |
Magnus Damm | 720be99 | 2008-04-24 21:47:15 +0900 | [diff] [blame] | 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) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 346 | [IRQ_TYPE_LEVEL_HIGH] = VALID(3), |
Magnus Damm | 720be99 | 2008-04-24 21:47:15 +0900 | [diff] [blame] | 347 | #endif |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 348 | }; |
| 349 | |
| 350 | static int intc_set_sense(unsigned int irq, unsigned int type) |
| 351 | { |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 352 | struct intc_desc_int *d = get_intc_desc(irq); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 353 | unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK]; |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 354 | struct intc_handle_int *ihp; |
| 355 | unsigned long addr; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 356 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 357 | if (!value) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 358 | return -EINVAL; |
| 359 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 360 | ihp = intc_find_irq(d->sense, d->nr_sense, irq); |
| 361 | if (ihp) { |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 362 | addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0); |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 363 | intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 364 | } |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 365 | return 0; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 366 | } |
| 367 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 368 | static unsigned int __init intc_get_reg(struct intc_desc_int *d, |
| 369 | unsigned long address) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 370 | { |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 371 | unsigned int k; |
| 372 | |
| 373 | for (k = 0; k < d->nr_reg; k++) { |
| 374 | if (d->reg[k] == address) |
| 375 | return k; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | BUG(); |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 379 | return 0; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 380 | } |
| 381 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 382 | static intc_enum __init intc_grp_id(struct intc_desc *desc, |
| 383 | intc_enum enum_id) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 384 | { |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 385 | struct intc_group *g = desc->groups; |
| 386 | unsigned int i, j; |
| 387 | |
| 388 | for (i = 0; g && enum_id && i < desc->nr_groups; i++) { |
| 389 | g = desc->groups + i; |
| 390 | |
| 391 | for (j = 0; g->enum_ids[j]; j++) { |
| 392 | if (g->enum_ids[j] != enum_id) |
| 393 | continue; |
| 394 | |
| 395 | return g->enum_id; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 402 | static unsigned int __init intc_mask_data(struct intc_desc *desc, |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 403 | struct intc_desc_int *d, |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 404 | intc_enum enum_id, int do_grps) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 405 | { |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 406 | struct intc_mask_reg *mr = desc->mask_regs; |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 407 | unsigned int i, j, fn, mode; |
| 408 | unsigned long reg_e, reg_d; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 409 | |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 410 | for (i = 0; mr && enum_id && i < desc->nr_mask_regs; i++) { |
| 411 | mr = desc->mask_regs + i; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 412 | |
| 413 | for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) { |
| 414 | if (mr->enum_ids[j] != enum_id) |
| 415 | continue; |
| 416 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 417 | if (mr->set_reg && mr->clr_reg) { |
| 418 | fn = REG_FN_WRITE_BASE; |
| 419 | mode = MODE_DUAL_REG; |
| 420 | reg_e = mr->clr_reg; |
| 421 | reg_d = mr->set_reg; |
| 422 | } else { |
| 423 | fn = REG_FN_MODIFY_BASE; |
| 424 | if (mr->set_reg) { |
| 425 | mode = MODE_ENABLE_REG; |
| 426 | reg_e = mr->set_reg; |
| 427 | reg_d = mr->set_reg; |
| 428 | } else { |
| 429 | mode = MODE_MASK_REG; |
| 430 | reg_e = mr->clr_reg; |
| 431 | reg_d = mr->clr_reg; |
| 432 | } |
Magnus Damm | 51da642 | 2007-08-03 14:25:32 +0900 | [diff] [blame] | 433 | } |
| 434 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 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), |
| 439 | 1, |
| 440 | (mr->reg_width - 1) - j); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 444 | if (do_grps) |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 445 | return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0); |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 446 | |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static unsigned int __init intc_prio_data(struct intc_desc *desc, |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 451 | struct intc_desc_int *d, |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 452 | intc_enum enum_id, int do_grps) |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 453 | { |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 454 | struct intc_prio_reg *pr = desc->prio_regs; |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 455 | unsigned int i, j, fn, mode, bit; |
| 456 | unsigned long reg_e, reg_d; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 457 | |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 458 | for (i = 0; pr && enum_id && i < desc->nr_prio_regs; i++) { |
| 459 | pr = desc->prio_regs + i; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 460 | |
| 461 | for (j = 0; j < ARRAY_SIZE(pr->enum_ids); j++) { |
| 462 | if (pr->enum_ids[j] != enum_id) |
| 463 | continue; |
| 464 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 465 | if (pr->set_reg && pr->clr_reg) { |
| 466 | fn = REG_FN_WRITE_BASE; |
| 467 | mode = MODE_PCLR_REG; |
| 468 | reg_e = pr->set_reg; |
| 469 | reg_d = pr->clr_reg; |
| 470 | } else { |
| 471 | fn = REG_FN_MODIFY_BASE; |
| 472 | mode = MODE_PRIO_REG; |
| 473 | if (!pr->set_reg) |
| 474 | BUG(); |
| 475 | reg_e = pr->set_reg; |
| 476 | reg_d = pr->set_reg; |
| 477 | } |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 478 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 479 | fn += (pr->reg_width >> 3) - 1; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 480 | |
roel kluin | b21a910 | 2008-09-09 23:02:43 +0200 | [diff] [blame] | 481 | BUG_ON((j + 1) * pr->field_width > pr->reg_width); |
| 482 | |
| 483 | bit = pr->reg_width - ((j + 1) * pr->field_width); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 484 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 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); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 492 | if (do_grps) |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 493 | return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0); |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 494 | |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
Yoshihiro Shimoda | 6bdfb22 | 2008-07-04 12:37:12 +0900 | [diff] [blame] | 498 | #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A) |
Magnus Damm | d58876e | 2008-04-24 21:36:34 +0900 | [diff] [blame] | 499 | static unsigned int __init intc_ack_data(struct intc_desc *desc, |
| 500 | struct intc_desc_int *d, |
| 501 | intc_enum enum_id) |
| 502 | { |
| 503 | struct intc_mask_reg *mr = desc->ack_regs; |
| 504 | unsigned int i, j, fn, mode; |
| 505 | unsigned long reg_e, reg_d; |
| 506 | |
| 507 | for (i = 0; mr && enum_id && i < desc->nr_ack_regs; i++) { |
| 508 | mr = desc->ack_regs + i; |
| 509 | |
| 510 | for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) { |
| 511 | if (mr->enum_ids[j] != enum_id) |
| 512 | continue; |
| 513 | |
| 514 | fn = REG_FN_MODIFY_BASE; |
| 515 | mode = MODE_ENABLE_REG; |
| 516 | reg_e = mr->set_reg; |
| 517 | reg_d = mr->set_reg; |
| 518 | |
| 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), |
| 523 | 1, |
| 524 | (mr->reg_width - 1) - j); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | #endif |
| 531 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 532 | static unsigned int __init intc_sense_data(struct intc_desc *desc, |
| 533 | struct intc_desc_int *d, |
| 534 | intc_enum enum_id) |
| 535 | { |
| 536 | struct intc_sense_reg *sr = desc->sense_regs; |
| 537 | unsigned int i, j, fn, bit; |
| 538 | |
| 539 | for (i = 0; sr && enum_id && i < desc->nr_sense_regs; i++) { |
| 540 | sr = desc->sense_regs + i; |
| 541 | |
| 542 | for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) { |
| 543 | if (sr->enum_ids[j] != enum_id) |
| 544 | continue; |
| 545 | |
| 546 | fn = REG_FN_MODIFY_BASE; |
| 547 | fn += (sr->reg_width >> 3) - 1; |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 548 | |
roel kluin | b21a910 | 2008-09-09 23:02:43 +0200 | [diff] [blame] | 549 | BUG_ON((j + 1) * sr->field_width > sr->reg_width); |
| 550 | |
| 551 | bit = sr->reg_width - ((j + 1) * sr->field_width); |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 552 | |
| 553 | return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg), |
| 554 | 0, sr->field_width, bit); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static void __init intc_register_irq(struct intc_desc *desc, |
| 562 | struct intc_desc_int *d, |
| 563 | intc_enum enum_id, |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 564 | unsigned int irq) |
| 565 | { |
Magnus Damm | 3d37d94 | 2007-08-17 00:50:44 +0900 | [diff] [blame] | 566 | struct intc_handle_int *hp; |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 567 | unsigned int data[2], primary; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 568 | |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 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) |
| 574 | */ |
| 575 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 576 | data[0] = intc_mask_data(desc, d, enum_id, 0); |
| 577 | data[1] = intc_prio_data(desc, d, enum_id, 0); |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 578 | |
| 579 | primary = 0; |
| 580 | if (!data[0] && data[1]) |
| 581 | primary = 1; |
| 582 | |
Magnus Damm | bdaa6e8 | 2009-02-24 22:58:57 +0900 | [diff] [blame] | 583 | if (!data[0] && !data[1]) |
Paul Mundt | f033599 | 2009-03-06 17:56:58 +0900 | [diff] [blame] | 584 | pr_warning("intc: missing unique irq mask for " |
| 585 | "irq %d (vect 0x%04x)\n", irq, irq2evt(irq)); |
Magnus Damm | bdaa6e8 | 2009-02-24 22:58:57 +0900 | [diff] [blame] | 586 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 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); |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 589 | |
| 590 | if (!data[primary]) |
| 591 | primary ^= 1; |
| 592 | |
| 593 | BUG_ON(!data[primary]); /* must have primary masking method */ |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 594 | |
| 595 | disable_irq_nosync(irq); |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 596 | set_irq_chip_and_handler_name(irq, &d->chip, |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 597 | handle_level_irq, "level"); |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 598 | set_irq_chip_data(irq, (void *)data[primary]); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 599 | |
Magnus Damm | 7f3edee | 2008-01-10 14:08:55 +0900 | [diff] [blame] | 600 | /* set priority level |
| 601 | * - this needs to be at least 2 for 5-bit priorities on 7780 |
| 602 | */ |
| 603 | intc_prio_level[irq] = 2; |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 604 | |
Magnus Damm | 680c459 | 2007-07-20 12:09:29 +0900 | [diff] [blame] | 605 | /* enable secondary masking method if present */ |
| 606 | if (data[!primary]) |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 607 | _intc_enable(irq, data[!primary]); |
| 608 | |
| 609 | /* add irq to d->prio list if priority is available */ |
| 610 | if (data[1]) { |
Magnus Damm | 3d37d94 | 2007-08-17 00:50:44 +0900 | [diff] [blame] | 611 | hp = d->prio + d->nr_prio; |
| 612 | hp->irq = irq; |
| 613 | hp->handle = data[1]; |
| 614 | |
| 615 | if (primary) { |
| 616 | /* |
| 617 | * only secondary priority should access registers, so |
| 618 | * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority() |
| 619 | */ |
| 620 | |
| 621 | hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0); |
| 622 | hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0); |
| 623 | } |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 624 | d->nr_prio++; |
| 625 | } |
| 626 | |
| 627 | /* add irq to d->sense list if sense is available */ |
| 628 | data[0] = intc_sense_data(desc, d, enum_id); |
| 629 | if (data[0]) { |
| 630 | (d->sense + d->nr_sense)->irq = irq; |
| 631 | (d->sense + d->nr_sense)->handle = data[0]; |
| 632 | d->nr_sense++; |
| 633 | } |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 634 | |
| 635 | /* irq should be disabled by default */ |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 636 | d->chip.mask(irq); |
Magnus Damm | d58876e | 2008-04-24 21:36:34 +0900 | [diff] [blame] | 637 | |
Yoshihiro Shimoda | 6bdfb22 | 2008-07-04 12:37:12 +0900 | [diff] [blame] | 638 | #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A) |
Magnus Damm | d58876e | 2008-04-24 21:36:34 +0900 | [diff] [blame] | 639 | if (desc->ack_regs) |
| 640 | ack_handle[irq] = intc_ack_data(desc, d, enum_id); |
| 641 | #endif |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 642 | } |
| 643 | |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 644 | static unsigned int __init save_reg(struct intc_desc_int *d, |
| 645 | unsigned int cnt, |
| 646 | unsigned long value, |
| 647 | unsigned int smp) |
| 648 | { |
| 649 | if (value) { |
| 650 | d->reg[cnt] = value; |
| 651 | #ifdef CONFIG_SMP |
| 652 | d->smp[cnt] = smp; |
| 653 | #endif |
| 654 | return 1; |
| 655 | } |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
Magnus Damm | bdaa6e8 | 2009-02-24 22:58:57 +0900 | [diff] [blame] | 660 | static unsigned char *intc_evt2irq_table; |
| 661 | |
| 662 | unsigned int intc_evt2irq(unsigned int vector) |
| 663 | { |
| 664 | unsigned int irq = evt2irq(vector); |
| 665 | |
| 666 | if (intc_evt2irq_table && intc_evt2irq_table[irq]) |
| 667 | irq = intc_evt2irq_table[irq]; |
| 668 | |
| 669 | return irq; |
| 670 | } |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 671 | |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 672 | void __init register_intc_controller(struct intc_desc *desc) |
| 673 | { |
Paul Mundt | 54ff328 | 2009-06-11 10:33:09 +0300 | [diff] [blame] | 674 | unsigned int i, k, smp; |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 675 | struct intc_desc_int *d; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 676 | |
Paul Mundt | 11b6aa9 | 2009-06-12 01:34:12 +0300 | [diff] [blame] | 677 | d = kzalloc(sizeof(*d), GFP_NOWAIT); |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 678 | |
Magnus Damm | 2dcec7a | 2009-04-01 14:30:59 +0000 | [diff] [blame] | 679 | INIT_LIST_HEAD(&d->list); |
| 680 | list_add(&d->list, &intc_list); |
| 681 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 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; |
| 685 | |
Yoshihiro Shimoda | 6bdfb22 | 2008-07-04 12:37:12 +0900 | [diff] [blame] | 686 | #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A) |
Magnus Damm | d58876e | 2008-04-24 21:36:34 +0900 | [diff] [blame] | 687 | d->nr_reg += desc->ack_regs ? desc->nr_ack_regs : 0; |
| 688 | #endif |
Paul Mundt | 11b6aa9 | 2009-06-12 01:34:12 +0300 | [diff] [blame] | 689 | d->reg = kzalloc(d->nr_reg * sizeof(*d->reg), GFP_NOWAIT); |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 690 | #ifdef CONFIG_SMP |
Paul Mundt | 11b6aa9 | 2009-06-12 01:34:12 +0300 | [diff] [blame] | 691 | d->smp = kzalloc(d->nr_reg * sizeof(*d->smp), GFP_NOWAIT); |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 692 | #endif |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 693 | k = 0; |
| 694 | |
| 695 | if (desc->mask_regs) { |
| 696 | for (i = 0; i < desc->nr_mask_regs; i++) { |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 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); |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
| 703 | if (desc->prio_regs) { |
Paul Mundt | 11b6aa9 | 2009-06-12 01:34:12 +0300 | [diff] [blame] | 704 | d->prio = kzalloc(desc->nr_vectors * sizeof(*d->prio), GFP_NOWAIT); |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 705 | |
| 706 | for (i = 0; i < desc->nr_prio_regs; i++) { |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 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); |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
| 713 | if (desc->sense_regs) { |
Paul Mundt | 11b6aa9 | 2009-06-12 01:34:12 +0300 | [diff] [blame] | 714 | d->sense = kzalloc(desc->nr_vectors * sizeof(*d->sense), GFP_NOWAIT); |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 715 | |
| 716 | for (i = 0; i < desc->nr_sense_regs; i++) { |
Magnus Damm | f18d533 | 2007-09-21 18:16:42 +0900 | [diff] [blame] | 717 | k += save_reg(d, k, desc->sense_regs[i].reg, 0); |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 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; |
Magnus Damm | f7dd254 | 2009-04-01 14:20:58 +0000 | [diff] [blame] | 725 | d->chip.enable = intc_enable; |
| 726 | d->chip.disable = intc_disable; |
| 727 | d->chip.shutdown = intc_disable; |
Magnus Damm | 73505b4 | 2007-08-12 15:26:12 +0900 | [diff] [blame] | 728 | d->chip.set_type = intc_set_sense; |
Magnus Damm | 2dcec7a | 2009-04-01 14:30:59 +0000 | [diff] [blame] | 729 | d->chip.set_wake = intc_set_wake; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 730 | |
Yoshihiro Shimoda | 6bdfb22 | 2008-07-04 12:37:12 +0900 | [diff] [blame] | 731 | #if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A) |
Magnus Damm | d58876e | 2008-04-24 21:36:34 +0900 | [diff] [blame] | 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); |
| 735 | |
| 736 | d->chip.mask_ack = intc_mask_ack; |
| 737 | } |
| 738 | #endif |
| 739 | |
| 740 | BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */ |
| 741 | |
Magnus Damm | bdaa6e8 | 2009-02-24 22:58:57 +0900 | [diff] [blame] | 742 | /* keep the first vector only if same enum is used multiple times */ |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 743 | for (i = 0; i < desc->nr_vectors; i++) { |
| 744 | struct intc_vect *vect = desc->vectors + i; |
Magnus Damm | bdaa6e8 | 2009-02-24 22:58:57 +0900 | [diff] [blame] | 745 | int first_irq = evt2irq(vect->vect); |
| 746 | |
| 747 | if (!vect->enum_id) |
| 748 | continue; |
| 749 | |
| 750 | for (k = i + 1; k < desc->nr_vectors; k++) { |
| 751 | struct intc_vect *vect2 = desc->vectors + k; |
| 752 | |
| 753 | if (vect->enum_id != vect2->enum_id) |
| 754 | continue; |
| 755 | |
| 756 | vect2->enum_id = 0; |
| 757 | |
| 758 | if (!intc_evt2irq_table) |
Paul Mundt | 11b6aa9 | 2009-06-12 01:34:12 +0300 | [diff] [blame] | 759 | intc_evt2irq_table = kzalloc(NR_IRQS, GFP_NOWAIT); |
Magnus Damm | bdaa6e8 | 2009-02-24 22:58:57 +0900 | [diff] [blame] | 760 | |
| 761 | if (!intc_evt2irq_table) { |
| 762 | pr_warning("intc: cannot allocate evt2irq!\n"); |
| 763 | continue; |
| 764 | } |
| 765 | |
| 766 | intc_evt2irq_table[evt2irq(vect2->vect)] = first_irq; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /* register the vectors one by one */ |
| 771 | for (i = 0; i < desc->nr_vectors; i++) { |
| 772 | struct intc_vect *vect = desc->vectors + i; |
Paul Mundt | 05ff300 | 2009-05-22 01:28:33 +0900 | [diff] [blame] | 773 | unsigned int irq = evt2irq(vect->vect); |
| 774 | struct irq_desc *irq_desc; |
Paul Mundt | 54ff328 | 2009-06-11 10:33:09 +0300 | [diff] [blame] | 775 | |
Magnus Damm | bdaa6e8 | 2009-02-24 22:58:57 +0900 | [diff] [blame] | 776 | if (!vect->enum_id) |
| 777 | continue; |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 778 | |
Paul Mundt | 54ff328 | 2009-06-11 10:33:09 +0300 | [diff] [blame] | 779 | irq_desc = irq_to_desc_alloc_node(irq, numa_node_id()); |
Paul Mundt | 05ff300 | 2009-05-22 01:28:33 +0900 | [diff] [blame] | 780 | if (unlikely(!irq_desc)) { |
| 781 | printk(KERN_INFO "can not get irq_desc for %d\n", irq); |
| 782 | continue; |
| 783 | } |
| 784 | |
| 785 | intc_register_irq(desc, d, vect->enum_id, irq); |
Magnus Damm | 02ab3f7 | 2007-07-18 17:25:09 +0900 | [diff] [blame] | 786 | } |
| 787 | } |
Magnus Damm | 2dcec7a | 2009-04-01 14:30:59 +0000 | [diff] [blame] | 788 | |
| 789 | static int intc_suspend(struct sys_device *dev, pm_message_t state) |
| 790 | { |
| 791 | struct intc_desc_int *d; |
| 792 | struct irq_desc *desc; |
| 793 | int irq; |
| 794 | |
| 795 | /* get intc controller associated with this sysdev */ |
| 796 | d = container_of(dev, struct intc_desc_int, sysdev); |
| 797 | |
Francesco VIRLINZI | 7fd87b3 | 2009-04-06 07:17:04 +0000 | [diff] [blame] | 798 | switch (state.event) { |
| 799 | case PM_EVENT_ON: |
| 800 | if (d->state.event != PM_EVENT_FREEZE) |
| 801 | break; |
| 802 | for_each_irq_desc(irq, desc) { |
| 803 | if (desc->chip != &d->chip) |
| 804 | continue; |
| 805 | if (desc->status & IRQ_DISABLED) |
| 806 | intc_disable(irq); |
| 807 | else |
| 808 | intc_enable(irq); |
| 809 | } |
| 810 | break; |
| 811 | case PM_EVENT_FREEZE: |
| 812 | /* nothing has to be done */ |
| 813 | break; |
| 814 | case PM_EVENT_SUSPEND: |
| 815 | /* enable wakeup irqs belonging to this intc controller */ |
| 816 | for_each_irq_desc(irq, desc) { |
| 817 | if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip)) |
| 818 | intc_enable(irq); |
| 819 | } |
| 820 | break; |
Magnus Damm | 2dcec7a | 2009-04-01 14:30:59 +0000 | [diff] [blame] | 821 | } |
Francesco VIRLINZI | 7fd87b3 | 2009-04-06 07:17:04 +0000 | [diff] [blame] | 822 | d->state = state; |
Magnus Damm | 2dcec7a | 2009-04-01 14:30:59 +0000 | [diff] [blame] | 823 | |
| 824 | return 0; |
| 825 | } |
| 826 | |
Francesco VIRLINZI | 7fd87b3 | 2009-04-06 07:17:04 +0000 | [diff] [blame] | 827 | static int intc_resume(struct sys_device *dev) |
| 828 | { |
| 829 | return intc_suspend(dev, PMSG_ON); |
| 830 | } |
| 831 | |
Magnus Damm | 2dcec7a | 2009-04-01 14:30:59 +0000 | [diff] [blame] | 832 | static struct sysdev_class intc_sysdev_class = { |
| 833 | .name = "intc", |
| 834 | .suspend = intc_suspend, |
Francesco VIRLINZI | 7fd87b3 | 2009-04-06 07:17:04 +0000 | [diff] [blame] | 835 | .resume = intc_resume, |
Magnus Damm | 2dcec7a | 2009-04-01 14:30:59 +0000 | [diff] [blame] | 836 | }; |
| 837 | |
| 838 | /* register this intc as sysdev to allow suspend/resume */ |
| 839 | static int __init register_intc_sysdevs(void) |
| 840 | { |
| 841 | struct intc_desc_int *d; |
| 842 | int error; |
| 843 | int id = 0; |
| 844 | |
| 845 | error = sysdev_class_register(&intc_sysdev_class); |
| 846 | if (!error) { |
| 847 | list_for_each_entry(d, &intc_list, list) { |
| 848 | d->sysdev.id = id; |
| 849 | d->sysdev.cls = &intc_sysdev_class; |
| 850 | error = sysdev_register(&d->sysdev); |
| 851 | if (error) |
| 852 | break; |
| 853 | id++; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | if (error) |
| 858 | pr_warning("intc: sysdev registration error\n"); |
| 859 | |
| 860 | return error; |
| 861 | } |
| 862 | |
| 863 | device_initcall(register_intc_sysdevs); |