intc.c 21.0 KB
Newer Older
1 2 3
/*
 * Shared interrupt handling code for IPR and INTC2 types of IRQs.
 *
M
Magnus Damm 已提交
4
 * Copyright (C) 2007, 2008 Magnus Damm
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Based on intc2.c and ipr.c
 *
 * Copyright (C) 1999  Niibe Yutaka & Takeshi Yaegashi
 * Copyright (C) 2000  Kazumoto Kojima
 * Copyright (C) 2001  David J. Mckay (david.mckay@st.com)
 * Copyright (C) 2003  Takashi Kusuda <kusuda-takashi@hitachi-ul.co.jp>
 * Copyright (C) 2005, 2006  Paul Mundt
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/interrupt.h>
M
Magnus Damm 已提交
23
#include <linux/bootmem.h>
24
#include <linux/sh_intc.h>
M
Magnus Damm 已提交
25 26
#include <linux/sysdev.h>
#include <linux/list.h>
P
Paul Mundt 已提交
27
#include <linux/topology.h>
M
Magnus Damm 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

#define _INTC_MK(fn, mode, addr_e, addr_d, width, shift) \
	((shift) | ((width) << 5) | ((fn) << 9) | ((mode) << 13) | \
	 ((addr_e) << 16) | ((addr_d << 24)))

#define _INTC_SHIFT(h) (h & 0x1f)
#define _INTC_WIDTH(h) ((h >> 5) & 0xf)
#define _INTC_FN(h) ((h >> 9) & 0xf)
#define _INTC_MODE(h) ((h >> 13) & 0x7)
#define _INTC_ADDR_E(h) ((h >> 16) & 0xff)
#define _INTC_ADDR_D(h) ((h >> 24) & 0xff)

struct intc_handle_int {
	unsigned int irq;
	unsigned long handle;
};
44

M
Magnus Damm 已提交
45
struct intc_desc_int {
M
Magnus Damm 已提交
46 47
	struct list_head list;
	struct sys_device sysdev;
48
	pm_message_t state;
M
Magnus Damm 已提交
49
	unsigned long *reg;
M
Magnus Damm 已提交
50 51 52
#ifdef CONFIG_SMP
	unsigned long *smp;
#endif
M
Magnus Damm 已提交
53 54 55 56 57 58 59
	unsigned int nr_reg;
	struct intc_handle_int *prio;
	unsigned int nr_prio;
	struct intc_handle_int *sense;
	unsigned int nr_sense;
	struct irq_chip chip;
};
60

M
Magnus Damm 已提交
61 62
static LIST_HEAD(intc_list);

M
Magnus Damm 已提交
63 64 65 66 67 68 69 70 71 72
#ifdef CONFIG_SMP
#define IS_SMP(x) x.smp
#define INTC_REG(d, x, c) (d->reg[(x)] + ((d->smp[(x)] & 0xff) * c))
#define SMP_NR(d, x) ((d->smp[(x)] >> 8) ? (d->smp[(x)] >> 8) : 1)
#else
#define IS_SMP(x) 0
#define INTC_REG(d, x, c) (d->reg[(x)])
#define SMP_NR(d, x) 1
#endif

M
Magnus Damm 已提交
73
static unsigned int intc_prio_level[NR_IRQS]; /* for now */
74
#if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
M
Magnus Damm 已提交
75 76
static unsigned long ack_handle[NR_IRQS];
#endif
77

M
Magnus Damm 已提交
78
static inline struct intc_desc_int *get_intc_desc(unsigned int irq)
79 80
{
	struct irq_chip *chip = get_irq_chip(irq);
M
Magnus Damm 已提交
81
	return (void *)((char *)chip - offsetof(struct intc_desc_int, chip));
82 83 84 85
}

static inline unsigned int set_field(unsigned int value,
				     unsigned int field_value,
M
Magnus Damm 已提交
86
				     unsigned int handle)
87
{
M
Magnus Damm 已提交
88 89 90
	unsigned int width = _INTC_WIDTH(handle);
	unsigned int shift = _INTC_SHIFT(handle);

91 92 93 94 95
	value &= ~(((1 << width) - 1) << shift);
	value |= field_value << shift;
	return value;
}

M
Magnus Damm 已提交
96
static void write_8(unsigned long addr, unsigned long h, unsigned long data)
97
{
98
	__raw_writeb(set_field(0, data, h), addr);
99 100
}

M
Magnus Damm 已提交
101
static void write_16(unsigned long addr, unsigned long h, unsigned long data)
102
{
103
	__raw_writew(set_field(0, data, h), addr);
104 105
}

M
Magnus Damm 已提交
106
static void write_32(unsigned long addr, unsigned long h, unsigned long data)
107
{
108
	__raw_writel(set_field(0, data, h), addr);
109 110
}

M
Magnus Damm 已提交
111
static void modify_8(unsigned long addr, unsigned long h, unsigned long data)
112
{
M
Magnus Damm 已提交
113 114
	unsigned long flags;
	local_irq_save(flags);
115
	__raw_writeb(set_field(__raw_readb(addr), data, h), addr);
M
Magnus Damm 已提交
116
	local_irq_restore(flags);
117 118
}

M
Magnus Damm 已提交
119
static void modify_16(unsigned long addr, unsigned long h, unsigned long data)
120
{
M
Magnus Damm 已提交
121 122
	unsigned long flags;
	local_irq_save(flags);
123
	__raw_writew(set_field(__raw_readw(addr), data, h), addr);
M
Magnus Damm 已提交
124
	local_irq_restore(flags);
125 126
}

M
Magnus Damm 已提交
127
static void modify_32(unsigned long addr, unsigned long h, unsigned long data)
128
{
M
Magnus Damm 已提交
129 130
	unsigned long flags;
	local_irq_save(flags);
131
	__raw_writel(set_field(__raw_readl(addr), data, h), addr);
M
Magnus Damm 已提交
132
	local_irq_restore(flags);
133 134
}

M
Magnus Damm 已提交
135 136 137 138 139 140 141 142 143 144 145 146
enum {	REG_FN_ERR = 0, REG_FN_WRITE_BASE = 1, REG_FN_MODIFY_BASE = 5 };

static void (*intc_reg_fns[])(unsigned long addr,
			      unsigned long h,
			      unsigned long data) = {
	[REG_FN_WRITE_BASE + 0] = write_8,
	[REG_FN_WRITE_BASE + 1] = write_16,
	[REG_FN_WRITE_BASE + 3] = write_32,
	[REG_FN_MODIFY_BASE + 0] = modify_8,
	[REG_FN_MODIFY_BASE + 1] = modify_16,
	[REG_FN_MODIFY_BASE + 3] = modify_32,
};
147

M
Magnus Damm 已提交
148 149 150 151 152 153
enum {	MODE_ENABLE_REG = 0, /* Bit(s) set -> interrupt enabled */
	MODE_MASK_REG,       /* Bit(s) set -> interrupt disabled */
	MODE_DUAL_REG,       /* Two registers, set bit to enable / disable */
	MODE_PRIO_REG,       /* Priority value written to enable interrupt */
	MODE_PCLR_REG,       /* Above plus all bits set to disable interrupt */
};
154

M
Magnus Damm 已提交
155 156 157 158 159 160
static void intc_mode_field(unsigned long addr,
			    unsigned long handle,
			    void (*fn)(unsigned long,
				       unsigned long,
				       unsigned long),
			    unsigned int irq)
161
{
M
Magnus Damm 已提交
162
	fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
163 164
}

M
Magnus Damm 已提交
165 166 167 168 169 170
static void intc_mode_zero(unsigned long addr,
			   unsigned long handle,
			   void (*fn)(unsigned long,
				       unsigned long,
				       unsigned long),
			   unsigned int irq)
171
{
M
Magnus Damm 已提交
172
	fn(addr, handle, 0);
173 174
}

M
Magnus Damm 已提交
175 176 177 178 179 180
static void intc_mode_prio(unsigned long addr,
			   unsigned long handle,
			   void (*fn)(unsigned long,
				       unsigned long,
				       unsigned long),
			   unsigned int irq)
181
{
M
Magnus Damm 已提交
182
	fn(addr, handle, intc_prio_level[irq]);
183 184
}

M
Magnus Damm 已提交
185 186 187 188 189 190 191 192 193 194 195 196
static void (*intc_enable_fns[])(unsigned long addr,
				 unsigned long handle,
				 void (*fn)(unsigned long,
					    unsigned long,
					    unsigned long),
				 unsigned int irq) = {
	[MODE_ENABLE_REG] = intc_mode_field,
	[MODE_MASK_REG] = intc_mode_zero,
	[MODE_DUAL_REG] = intc_mode_field,
	[MODE_PRIO_REG] = intc_mode_prio,
	[MODE_PCLR_REG] = intc_mode_prio,
};
197

M
Magnus Damm 已提交
198 199 200 201 202 203 204 205 206 207 208 209
static void (*intc_disable_fns[])(unsigned long addr,
				  unsigned long handle,
				  void (*fn)(unsigned long,
					     unsigned long,
					     unsigned long),
				  unsigned int irq) = {
	[MODE_ENABLE_REG] = intc_mode_zero,
	[MODE_MASK_REG] = intc_mode_field,
	[MODE_DUAL_REG] = intc_mode_field,
	[MODE_PRIO_REG] = intc_mode_zero,
	[MODE_PCLR_REG] = intc_mode_field,
};
210

M
Magnus Damm 已提交
211
static inline void _intc_enable(unsigned int irq, unsigned long handle)
212
{
M
Magnus Damm 已提交
213
	struct intc_desc_int *d = get_intc_desc(irq);
M
Magnus Damm 已提交
214 215
	unsigned long addr;
	unsigned int cpu;
216

M
Magnus Damm 已提交
217 218 219 220 221
	for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_E(handle)); cpu++) {
		addr = INTC_REG(d, _INTC_ADDR_E(handle), cpu);
		intc_enable_fns[_INTC_MODE(handle)](addr, handle, intc_reg_fns\
						    [_INTC_FN(handle)], irq);
	}
222 223
}

224 225
static void intc_enable(unsigned int irq)
{
M
Magnus Damm 已提交
226
	_intc_enable(irq, (unsigned long)get_irq_chip_data(irq));
227 228 229 230
}

static void intc_disable(unsigned int irq)
{
M
Magnus Damm 已提交
231
	struct intc_desc_int *d = get_intc_desc(irq);
M
Magnus Damm 已提交
232
	unsigned long handle = (unsigned long) get_irq_chip_data(irq);
M
Magnus Damm 已提交
233 234
	unsigned long addr;
	unsigned int cpu;
235

M
Magnus Damm 已提交
236 237 238 239 240
	for (cpu = 0; cpu < SMP_NR(d, _INTC_ADDR_D(handle)); cpu++) {
		addr = INTC_REG(d, _INTC_ADDR_D(handle), cpu);
		intc_disable_fns[_INTC_MODE(handle)](addr, handle,intc_reg_fns\
						     [_INTC_FN(handle)], irq);
	}
241 242
}

M
Magnus Damm 已提交
243 244 245 246 247
static int intc_set_wake(unsigned int irq, unsigned int on)
{
	return 0; /* allow wakeup, but setup hardware in intc_suspend() */
}

248
#if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
M
Magnus Damm 已提交
249 250 251 252 253 254 255 256 257 258 259 260
static void intc_mask_ack(unsigned int irq)
{
	struct intc_desc_int *d = get_intc_desc(irq);
	unsigned long handle = ack_handle[irq];
	unsigned long addr;

	intc_disable(irq);

	/* read register and write zero only to the assocaited bit */

	if (handle) {
		addr = INTC_REG(d, _INTC_ADDR_D(handle), 0);
261 262
		switch (_INTC_FN(handle)) {
		case REG_FN_MODIFY_BASE + 0:	/* 8bit */
263 264
			__raw_readb(addr);
			__raw_writeb(0xff ^ set_field(0, 1, handle), addr);
265 266
			break;
		case REG_FN_MODIFY_BASE + 1:	/* 16bit */
267 268
			__raw_readw(addr);
			__raw_writew(0xffff ^ set_field(0, 1, handle), addr);
269 270
			break;
		case REG_FN_MODIFY_BASE + 3:	/* 32bit */
271 272
			__raw_readl(addr);
			__raw_writel(0xffffffff ^ set_field(0, 1, handle), addr);
273 274 275 276 277
			break;
		default:
			BUG();
			break;
		}
M
Magnus Damm 已提交
278 279 280 281
	}
}
#endif

M
Magnus Damm 已提交
282 283 284
static struct intc_handle_int *intc_find_irq(struct intc_handle_int *hp,
					     unsigned int nr_hp,
					     unsigned int irq)
285
{
M
Magnus Damm 已提交
286 287
	int i;

288 289 290 291 292 293 294 295 296 297 298 299
	/* this doesn't scale well, but...
	 *
	 * this function should only be used for cerain uncommon
	 * operations such as intc_set_priority() and intc_set_sense()
	 * and in those rare cases performance doesn't matter that much.
	 * keeping the memory footprint low is more important.
	 *
	 * one rather simple way to speed this up and still keep the
	 * memory footprint down is to make sure the array is sorted
	 * and then perform a bisect to lookup the irq.
	 */

M
Magnus Damm 已提交
300 301 302 303 304 305
	for (i = 0; i < nr_hp; i++) {
		if ((hp + i)->irq != irq)
			continue;

		return hp + i;
	}
306

M
Magnus Damm 已提交
307
	return NULL;
308 309
}

M
Magnus Damm 已提交
310
int intc_set_priority(unsigned int irq, unsigned int prio)
311
{
M
Magnus Damm 已提交
312 313 314 315 316 317 318 319
	struct intc_desc_int *d = get_intc_desc(irq);
	struct intc_handle_int *ihp;

	if (!intc_prio_level[irq] || prio <= 1)
		return -EINVAL;

	ihp = intc_find_irq(d->prio, d->nr_prio, irq);
	if (ihp) {
320
		if (prio >= (1 << _INTC_WIDTH(ihp->handle)))
M
Magnus Damm 已提交
321
			return -EINVAL;
322

M
Magnus Damm 已提交
323 324 325 326 327 328 329 330
		intc_prio_level[irq] = prio;

		/*
		 * only set secondary masking method directly
		 * primary masking method is using intc_prio_level[irq]
		 * priority level will be set during next enable()
		 */

331
		if (_INTC_FN(ihp->handle) != REG_FN_ERR)
M
Magnus Damm 已提交
332 333 334
			_intc_enable(irq, ihp->handle);
	}
	return 0;
335 336 337 338 339 340 341 342
}

#define VALID(x) (x | 0x80)

static unsigned char intc_irq_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
	[IRQ_TYPE_EDGE_FALLING] = VALID(0),
	[IRQ_TYPE_EDGE_RISING] = VALID(1),
	[IRQ_TYPE_LEVEL_LOW] = VALID(2),
343 344 345 346
	/* SH7706, SH7707 and SH7709 do not support high level triggered */
#if !defined(CONFIG_CPU_SUBTYPE_SH7706) && \
    !defined(CONFIG_CPU_SUBTYPE_SH7707) && \
    !defined(CONFIG_CPU_SUBTYPE_SH7709)
347
	[IRQ_TYPE_LEVEL_HIGH] = VALID(3),
348
#endif
349 350 351 352
};

static int intc_set_sense(unsigned int irq, unsigned int type)
{
M
Magnus Damm 已提交
353
	struct intc_desc_int *d = get_intc_desc(irq);
354
	unsigned char value = intc_irq_sense_table[type & IRQ_TYPE_SENSE_MASK];
M
Magnus Damm 已提交
355 356
	struct intc_handle_int *ihp;
	unsigned long addr;
357

M
Magnus Damm 已提交
358
	if (!value)
359 360
		return -EINVAL;

M
Magnus Damm 已提交
361 362
	ihp = intc_find_irq(d->sense, d->nr_sense, irq);
	if (ihp) {
M
Magnus Damm 已提交
363
		addr = INTC_REG(d, _INTC_ADDR_E(ihp->handle), 0);
M
Magnus Damm 已提交
364
		intc_reg_fns[_INTC_FN(ihp->handle)](addr, ihp->handle, value);
365
	}
M
Magnus Damm 已提交
366
	return 0;
367 368
}

M
Magnus Damm 已提交
369 370
static unsigned int __init intc_get_reg(struct intc_desc_int *d,
				 unsigned long address)
371
{
M
Magnus Damm 已提交
372
	unsigned int k;
373

M
Magnus Damm 已提交
374 375 376
	for (k = 0; k < d->nr_reg; k++) {
		if (d->reg[k] == address)
			return k;
377 378 379
	}

	BUG();
M
Magnus Damm 已提交
380
	return 0;
381 382
}

M
Magnus Damm 已提交
383 384
static intc_enum __init intc_grp_id(struct intc_desc *desc,
				    intc_enum enum_id)
M
Magnus Damm 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
{
	struct intc_group *g = desc->groups;
	unsigned int i, j;

	for (i = 0; g && enum_id && i < desc->nr_groups; i++) {
		g = desc->groups + i;

		for (j = 0; g->enum_ids[j]; j++) {
			if (g->enum_ids[j] != enum_id)
				continue;

			return g->enum_id;
		}
	}

	return 0;
}

403
static unsigned int __init intc_mask_data(struct intc_desc *desc,
M
Magnus Damm 已提交
404
					  struct intc_desc_int *d,
M
Magnus Damm 已提交
405
					  intc_enum enum_id, int do_grps)
406
{
M
Magnus Damm 已提交
407
	struct intc_mask_reg *mr = desc->mask_regs;
M
Magnus Damm 已提交
408 409
	unsigned int i, j, fn, mode;
	unsigned long reg_e, reg_d;
410

M
Magnus Damm 已提交
411 412
	for (i = 0; mr && enum_id && i < desc->nr_mask_regs; i++) {
		mr = desc->mask_regs + i;
413 414 415 416 417

		for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
			if (mr->enum_ids[j] != enum_id)
				continue;

M
Magnus Damm 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
			if (mr->set_reg && mr->clr_reg) {
				fn = REG_FN_WRITE_BASE;
				mode = MODE_DUAL_REG;
				reg_e = mr->clr_reg;
				reg_d = mr->set_reg;
			} else {
				fn = REG_FN_MODIFY_BASE;
				if (mr->set_reg) {
					mode = MODE_ENABLE_REG;
					reg_e = mr->set_reg;
					reg_d = mr->set_reg;
				} else {
					mode = MODE_MASK_REG;
					reg_e = mr->clr_reg;
					reg_d = mr->clr_reg;
				}
434 435
			}

M
Magnus Damm 已提交
436 437 438 439 440 441
			fn += (mr->reg_width >> 3) - 1;
			return _INTC_MK(fn, mode,
					intc_get_reg(d, reg_e),
					intc_get_reg(d, reg_d),
					1,
					(mr->reg_width - 1) - j);
442 443 444
		}
	}

M
Magnus Damm 已提交
445
	if (do_grps)
M
Magnus Damm 已提交
446
		return intc_mask_data(desc, d, intc_grp_id(desc, enum_id), 0);
M
Magnus Damm 已提交
447

448 449 450 451
	return 0;
}

static unsigned int __init intc_prio_data(struct intc_desc *desc,
M
Magnus Damm 已提交
452
					  struct intc_desc_int *d,
M
Magnus Damm 已提交
453
					  intc_enum enum_id, int do_grps)
454
{
M
Magnus Damm 已提交
455
	struct intc_prio_reg *pr = desc->prio_regs;
M
Magnus Damm 已提交
456 457
	unsigned int i, j, fn, mode, bit;
	unsigned long reg_e, reg_d;
458

M
Magnus Damm 已提交
459 460
	for (i = 0; pr && enum_id && i < desc->nr_prio_regs; i++) {
		pr = desc->prio_regs + i;
461 462 463 464 465

		for (j = 0; j < ARRAY_SIZE(pr->enum_ids); j++) {
			if (pr->enum_ids[j] != enum_id)
				continue;

M
Magnus Damm 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478
			if (pr->set_reg && pr->clr_reg) {
				fn = REG_FN_WRITE_BASE;
				mode = MODE_PCLR_REG;
				reg_e = pr->set_reg;
				reg_d = pr->clr_reg;
			} else {
				fn = REG_FN_MODIFY_BASE;
				mode = MODE_PRIO_REG;
				if (!pr->set_reg)
					BUG();
				reg_e = pr->set_reg;
				reg_d = pr->set_reg;
			}
479

M
Magnus Damm 已提交
480
			fn += (pr->reg_width >> 3) - 1;
481

482 483 484
			BUG_ON((j + 1) * pr->field_width > pr->reg_width);

			bit = pr->reg_width - ((j + 1) * pr->field_width);
485

M
Magnus Damm 已提交
486 487 488 489
			return _INTC_MK(fn, mode,
					intc_get_reg(d, reg_e),
					intc_get_reg(d, reg_d),
					pr->field_width, bit);
490 491 492
		}
	}

M
Magnus Damm 已提交
493
	if (do_grps)
M
Magnus Damm 已提交
494 495 496 497 498
		return intc_prio_data(desc, d, intc_grp_id(desc, enum_id), 0);

	return 0;
}

499
#if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
M
Magnus Damm 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
static unsigned int __init intc_ack_data(struct intc_desc *desc,
					  struct intc_desc_int *d,
					  intc_enum enum_id)
{
	struct intc_mask_reg *mr = desc->ack_regs;
	unsigned int i, j, fn, mode;
	unsigned long reg_e, reg_d;

	for (i = 0; mr && enum_id && i < desc->nr_ack_regs; i++) {
		mr = desc->ack_regs + i;

		for (j = 0; j < ARRAY_SIZE(mr->enum_ids); j++) {
			if (mr->enum_ids[j] != enum_id)
				continue;

			fn = REG_FN_MODIFY_BASE;
			mode = MODE_ENABLE_REG;
			reg_e = mr->set_reg;
			reg_d = mr->set_reg;

			fn += (mr->reg_width >> 3) - 1;
			return _INTC_MK(fn, mode,
					intc_get_reg(d, reg_e),
					intc_get_reg(d, reg_d),
					1,
					(mr->reg_width - 1) - j);
		}
	}

	return 0;
}
#endif

M
Magnus Damm 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
static unsigned int __init intc_sense_data(struct intc_desc *desc,
					   struct intc_desc_int *d,
					   intc_enum enum_id)
{
	struct intc_sense_reg *sr = desc->sense_regs;
	unsigned int i, j, fn, bit;

	for (i = 0; sr && enum_id && i < desc->nr_sense_regs; i++) {
		sr = desc->sense_regs + i;

		for (j = 0; j < ARRAY_SIZE(sr->enum_ids); j++) {
			if (sr->enum_ids[j] != enum_id)
				continue;

			fn = REG_FN_MODIFY_BASE;
			fn += (sr->reg_width >> 3) - 1;

550 551 552
			BUG_ON((j + 1) * sr->field_width > sr->reg_width);

			bit = sr->reg_width - ((j + 1) * sr->field_width);
M
Magnus Damm 已提交
553 554 555 556 557

			return _INTC_MK(fn, 0, intc_get_reg(d, sr->reg),
					0, sr->field_width, bit);
		}
	}
M
Magnus Damm 已提交
558

559 560 561
	return 0;
}

M
Magnus Damm 已提交
562 563 564
static void __init intc_register_irq(struct intc_desc *desc,
				     struct intc_desc_int *d,
				     intc_enum enum_id,
565 566
				     unsigned int irq)
{
567
	struct intc_handle_int *hp;
M
Magnus Damm 已提交
568 569 570 571 572 573 574 575
	unsigned int data[2], primary;

	/* Prefer single interrupt source bitmap over other combinations:
	 * 1. bitmap, single interrupt source
	 * 2. priority, single interrupt source
	 * 3. bitmap, multiple interrupt sources (groups)
	 * 4. priority, multiple interrupt sources (groups)
	 */
576

M
Magnus Damm 已提交
577 578
	data[0] = intc_mask_data(desc, d, enum_id, 0);
	data[1] = intc_prio_data(desc, d, enum_id, 0);
M
Magnus Damm 已提交
579 580 581 582 583

	primary = 0;
	if (!data[0] && data[1])
		primary = 1;

584
	if (!data[0] && !data[1])
585 586
		pr_warning("intc: missing unique irq mask for "
			   "irq %d (vect 0x%04x)\n", irq, irq2evt(irq));
587

M
Magnus Damm 已提交
588 589
	data[0] = data[0] ? data[0] : intc_mask_data(desc, d, enum_id, 1);
	data[1] = data[1] ? data[1] : intc_prio_data(desc, d, enum_id, 1);
M
Magnus Damm 已提交
590 591 592 593 594

	if (!data[primary])
		primary ^= 1;

	BUG_ON(!data[primary]); /* must have primary masking method */
595 596

	disable_irq_nosync(irq);
M
Magnus Damm 已提交
597
	set_irq_chip_and_handler_name(irq, &d->chip,
598
				      handle_level_irq, "level");
M
Magnus Damm 已提交
599
	set_irq_chip_data(irq, (void *)data[primary]);
600

601 602 603 604
	/* set priority level
	 * - this needs to be at least 2 for 5-bit priorities on 7780
	 */
	intc_prio_level[irq] = 2;
M
Magnus Damm 已提交
605

M
Magnus Damm 已提交
606 607
	/* enable secondary masking method if present */
	if (data[!primary])
M
Magnus Damm 已提交
608 609 610 611
		_intc_enable(irq, data[!primary]);

	/* add irq to d->prio list if priority is available */
	if (data[1]) {
612 613 614 615 616 617 618 619 620 621 622 623 624
		hp = d->prio + d->nr_prio;
		hp->irq = irq;
		hp->handle = data[1];

		if (primary) {
			/*
			 * only secondary priority should access registers, so
			 * set _INTC_FN(h) = REG_FN_ERR for intc_set_priority()
			 */

			hp->handle &= ~_INTC_MK(0x0f, 0, 0, 0, 0, 0);
			hp->handle |= _INTC_MK(REG_FN_ERR, 0, 0, 0, 0, 0);
		}
M
Magnus Damm 已提交
625 626 627 628 629 630 631 632 633 634
		d->nr_prio++;
	}

	/* add irq to d->sense list if sense is available */
	data[0] = intc_sense_data(desc, d, enum_id);
	if (data[0]) {
		(d->sense + d->nr_sense)->irq = irq;
		(d->sense + d->nr_sense)->handle = data[0];
		d->nr_sense++;
	}
635 636

	/* irq should be disabled by default */
M
Magnus Damm 已提交
637
	d->chip.mask(irq);
M
Magnus Damm 已提交
638

639
#if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
M
Magnus Damm 已提交
640 641 642
	if (desc->ack_regs)
		ack_handle[irq] = intc_ack_data(desc, d, enum_id);
#endif
643 644
}

M
Magnus Damm 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
static unsigned int __init save_reg(struct intc_desc_int *d,
				    unsigned int cnt,
				    unsigned long value,
				    unsigned int smp)
{
	if (value) {
		d->reg[cnt] = value;
#ifdef CONFIG_SMP
		d->smp[cnt] = smp;
#endif
		return 1;
	}

	return 0;
}

661 662 663 664 665 666 667 668 669 670 671
static unsigned char *intc_evt2irq_table;

unsigned int intc_evt2irq(unsigned int vector)
{
	unsigned int irq = evt2irq(vector);

	if (intc_evt2irq_table && intc_evt2irq_table[irq])
		irq = intc_evt2irq_table[irq];

	return irq;
}
M
Magnus Damm 已提交
672

673 674
void __init register_intc_controller(struct intc_desc *desc)
{
P
Paul Mundt 已提交
675
	unsigned int i, k, smp;
M
Magnus Damm 已提交
676 677 678 679
	struct intc_desc_int *d;

	d = alloc_bootmem(sizeof(*d));

M
Magnus Damm 已提交
680 681 682
	INIT_LIST_HEAD(&d->list);
	list_add(&d->list, &intc_list);

M
Magnus Damm 已提交
683 684 685 686
	d->nr_reg = desc->mask_regs ? desc->nr_mask_regs * 2 : 0;
	d->nr_reg += desc->prio_regs ? desc->nr_prio_regs * 2 : 0;
	d->nr_reg += desc->sense_regs ? desc->nr_sense_regs : 0;

687
#if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
M
Magnus Damm 已提交
688 689
	d->nr_reg += desc->ack_regs ? desc->nr_ack_regs : 0;
#endif
M
Magnus Damm 已提交
690
	d->reg = alloc_bootmem(d->nr_reg * sizeof(*d->reg));
M
Magnus Damm 已提交
691 692 693
#ifdef CONFIG_SMP
	d->smp = alloc_bootmem(d->nr_reg * sizeof(*d->smp));
#endif
M
Magnus Damm 已提交
694 695 696 697
	k = 0;

	if (desc->mask_regs) {
		for (i = 0; i < desc->nr_mask_regs; i++) {
M
Magnus Damm 已提交
698 699 700
			smp = IS_SMP(desc->mask_regs[i]);
			k += save_reg(d, k, desc->mask_regs[i].set_reg, smp);
			k += save_reg(d, k, desc->mask_regs[i].clr_reg, smp);
M
Magnus Damm 已提交
701 702 703 704 705 706 707
		}
	}

	if (desc->prio_regs) {
		d->prio = alloc_bootmem(desc->nr_vectors * sizeof(*d->prio));

		for (i = 0; i < desc->nr_prio_regs; i++) {
M
Magnus Damm 已提交
708 709 710
			smp = IS_SMP(desc->prio_regs[i]);
			k += save_reg(d, k, desc->prio_regs[i].set_reg, smp);
			k += save_reg(d, k, desc->prio_regs[i].clr_reg, smp);
M
Magnus Damm 已提交
711 712 713 714 715 716 717
		}
	}

	if (desc->sense_regs) {
		d->sense = alloc_bootmem(desc->nr_vectors * sizeof(*d->sense));

		for (i = 0; i < desc->nr_sense_regs; i++) {
M
Magnus Damm 已提交
718
			k += save_reg(d, k, desc->sense_regs[i].reg, 0);
M
Magnus Damm 已提交
719 720 721 722 723 724 725
		}
	}

	d->chip.name = desc->name;
	d->chip.mask = intc_disable;
	d->chip.unmask = intc_enable;
	d->chip.mask_ack = intc_disable;
726 727 728
	d->chip.enable = intc_enable;
	d->chip.disable = intc_disable;
	d->chip.shutdown = intc_disable;
M
Magnus Damm 已提交
729
	d->chip.set_type = intc_set_sense;
M
Magnus Damm 已提交
730
	d->chip.set_wake = intc_set_wake;
731

732
#if defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4A)
M
Magnus Damm 已提交
733 734 735 736 737 738 739 740 741 742
	if (desc->ack_regs) {
		for (i = 0; i < desc->nr_ack_regs; i++)
			k += save_reg(d, k, desc->ack_regs[i].set_reg, 0);

		d->chip.mask_ack = intc_mask_ack;
	}
#endif

	BUG_ON(k > 256); /* _INTC_ADDR_E() and _INTC_ADDR_D() are 8 bits */

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
	/* keep the first vector only if same enum is used multiple times */
	for (i = 0; i < desc->nr_vectors; i++) {
		struct intc_vect *vect = desc->vectors + i;
		int first_irq = evt2irq(vect->vect);

		if (!vect->enum_id)
			continue;

		for (k = i + 1; k < desc->nr_vectors; k++) {
			struct intc_vect *vect2 = desc->vectors + k;

			if (vect->enum_id != vect2->enum_id)
				continue;

			vect2->enum_id = 0;

			if (!intc_evt2irq_table)
				intc_evt2irq_table = alloc_bootmem(NR_IRQS);

			if (!intc_evt2irq_table) {
				pr_warning("intc: cannot allocate evt2irq!\n");
				continue;
			}

			intc_evt2irq_table[evt2irq(vect2->vect)] = first_irq;
		}
	}

	/* register the vectors one by one */
772 773
	for (i = 0; i < desc->nr_vectors; i++) {
		struct intc_vect *vect = desc->vectors + i;
774 775
		unsigned int irq = evt2irq(vect->vect);
		struct irq_desc *irq_desc;
P
Paul Mundt 已提交
776

777 778 779
		if (!vect->enum_id)
			continue;

P
Paul Mundt 已提交
780
		irq_desc = irq_to_desc_alloc_node(irq, numa_node_id());
781 782 783 784 785 786
		if (unlikely(!irq_desc)) {
			printk(KERN_INFO "can not get irq_desc for %d\n", irq);
			continue;
		}

		intc_register_irq(desc, d, vect->enum_id, irq);
787 788
	}
}
M
Magnus Damm 已提交
789 790 791 792 793 794 795 796 797 798

static int intc_suspend(struct sys_device *dev, pm_message_t state)
{
	struct intc_desc_int *d;
	struct irq_desc *desc;
	int irq;

	/* get intc controller associated with this sysdev */
	d = container_of(dev, struct intc_desc_int, sysdev);

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
	switch (state.event) {
	case PM_EVENT_ON:
		if (d->state.event != PM_EVENT_FREEZE)
			break;
		for_each_irq_desc(irq, desc) {
			if (desc->chip != &d->chip)
				continue;
			if (desc->status & IRQ_DISABLED)
				intc_disable(irq);
			else
				intc_enable(irq);
		}
		break;
	case PM_EVENT_FREEZE:
		/* nothing has to be done */
		break;
	case PM_EVENT_SUSPEND:
		/* enable wakeup irqs belonging to this intc controller */
		for_each_irq_desc(irq, desc) {
			if ((desc->status & IRQ_WAKEUP) && (desc->chip == &d->chip))
				intc_enable(irq);
		}
		break;
M
Magnus Damm 已提交
822
	}
823
	d->state = state;
M
Magnus Damm 已提交
824 825 826 827

	return 0;
}

828 829 830 831 832
static int intc_resume(struct sys_device *dev)
{
	return intc_suspend(dev, PMSG_ON);
}

M
Magnus Damm 已提交
833 834 835
static struct sysdev_class intc_sysdev_class = {
	.name = "intc",
	.suspend = intc_suspend,
836
	.resume = intc_resume,
M
Magnus Damm 已提交
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
};

/* register this intc as sysdev to allow suspend/resume */
static int __init register_intc_sysdevs(void)
{
	struct intc_desc_int *d;
	int error;
	int id = 0;

	error = sysdev_class_register(&intc_sysdev_class);
	if (!error) {
		list_for_each_entry(d, &intc_list, list) {
			d->sysdev.id = id;
			d->sysdev.cls = &intc_sysdev_class;
			error = sysdev_register(&d->sysdev);
			if (error)
				break;
			id++;
		}
	}

	if (error)
		pr_warning("intc: sysdev registration error\n");

	return error;
}

device_initcall(register_intc_sysdevs);