ints-priority.c 39.8 KB
Newer Older
B
Bryan Wu 已提交
1
/*
2
 * Set up the interrupt priorities
B
Bryan Wu 已提交
3
 *
4 5 6 7 8 9
 * Copyright  2004-2009 Analog Devices Inc.
 *                 2003 Bas Vermeulen <bas@buyways.nl>
 *                 2002 Arcturus Networks Inc. MaTed <mated@sympatico.ca>
 *            2000-2001 Lineo, Inc. D. Jefff Dionne <jeff@lineo.ca>
 *                 1999 D. Jeff Dionne <jeff@uclinux.org>
 *                 1996 Roman Zippel
B
Bryan Wu 已提交
10
 *
11
 * Licensed under the GPL-2
B
Bryan Wu 已提交
12 13 14 15 16 17
 */

#include <linux/module.h>
#include <linux/kernel_stat.h>
#include <linux/seq_file.h>
#include <linux/irq.h>
18
#include <linux/sched.h>
19 20
#include <linux/syscore_ops.h>
#include <asm/delay.h>
21 22 23
#ifdef CONFIG_IPIPE
#include <linux/ipipe.h>
#endif
B
Bryan Wu 已提交
24 25 26 27
#include <asm/traps.h>
#include <asm/blackfin.h>
#include <asm/gpio.h>
#include <asm/irq_handler.h>
28
#include <asm/dpmc.h>
29
#include <asm/traps.h>
B
Bryan Wu 已提交
30

31
#ifndef SEC_GCTL
32 33 34 35
# define SIC_SYSIRQ(irq)	(irq - (IRQ_CORETMR + 1))
#else
# define SIC_SYSIRQ(irq)	((irq) - IVG15)
#endif
36

B
Bryan Wu 已提交
37 38 39 40 41 42 43
/*
 * NOTES:
 * - we have separated the physical Hardware interrupt from the
 * levels that the LINUX kernel sees (see the description in irq.h)
 * -
 */

44
#ifndef CONFIG_SMP
45 46 47 48 49 50
/* Initialize this to an actual value to force it into the .data
 * section so that we know it is properly initialized at entry into
 * the kernel but before bss is initialized to zero (which is where
 * it would live otherwise).  The 0x1f magic represents the IRQs we
 * cannot actually mask out in hardware.
 */
51 52
unsigned long bfin_irq_flags = 0x1f;
EXPORT_SYMBOL(bfin_irq_flags);
53
#endif
B
Bryan Wu 已提交
54

55 56
#ifdef CONFIG_PM
unsigned long bfin_sic_iwr[3];	/* Up to 3 SIC_IWRx registers */
57
unsigned vr_wakeup;
58 59
#endif

60
#ifndef SEC_GCTL
61
static struct ivgx {
62
	/* irq number for request_irq, available in mach-bf5xx/irq.h */
63
	unsigned int irqno;
B
Bryan Wu 已提交
64
	/* corresponding bit in the SIC_ISR register */
65
	unsigned int isrflag;
B
Bryan Wu 已提交
66 67
} ivg_table[NR_PERI_INTS];

68
static struct ivg_slice {
B
Bryan Wu 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82
	/* position of first irq in ivg_table for given ivg */
	struct ivgx *ifirst;
	struct ivgx *istop;
} ivg7_13[IVG13 - IVG7 + 1];


/*
 * Search SIC_IAR and fill tables with the irqvalues
 * and their positions in the SIC_ISR register.
 */
static void __init search_IAR(void)
{
	unsigned ivg, irq_pos = 0;
	for (ivg = 0; ivg <= IVG13 - IVG7; ivg++) {
83
		int irqN;
B
Bryan Wu 已提交
84

85
		ivg7_13[ivg].istop = ivg7_13[ivg].ifirst = &ivg_table[irq_pos];
B
Bryan Wu 已提交
86

87 88
		for (irqN = 0; irqN < NR_PERI_INTS; irqN += 4) {
			int irqn;
89 90
			u32 iar =
				bfin_read32((unsigned long *)SIC_IAR0 +
91 92 93
#if defined(CONFIG_BF51x) || defined(CONFIG_BF52x) || \
	defined(CONFIG_BF538) || defined(CONFIG_BF539)
				((irqN % 32) >> 3) + ((irqN / 32) * ((SIC_IAR4 - SIC_IAR0) / 4))
94
#else
95
				(irqN >> 3)
96
#endif
97 98 99 100 101 102 103 104 105
				);
			for (irqn = irqN; irqn < irqN + 4; ++irqn) {
				int iar_shift = (irqn & 7) * 4;
				if (ivg == (0xf & (iar >> iar_shift))) {
					ivg_table[irq_pos].irqno = IVG7 + irqn;
					ivg_table[irq_pos].isrflag = 1 << (irqn % 32);
					ivg7_13[ivg].istop++;
					irq_pos++;
				}
B
Bryan Wu 已提交
106 107 108 109
			}
		}
	}
}
110
#endif
B
Bryan Wu 已提交
111 112

/*
113
 * This is for core internal IRQs
B
Bryan Wu 已提交
114
 */
115
void bfin_ack_noop(struct irq_data *d)
B
Bryan Wu 已提交
116 117 118 119
{
	/* Dummy function.  */
}

120
static void bfin_core_mask_irq(struct irq_data *d)
B
Bryan Wu 已提交
121
{
122
	bfin_irq_flags &= ~(1 << d->irq);
123 124
	if (!hard_irqs_disabled())
		hard_local_irq_enable();
B
Bryan Wu 已提交
125 126
}

127
static void bfin_core_unmask_irq(struct irq_data *d)
B
Bryan Wu 已提交
128
{
129
	bfin_irq_flags |= 1 << d->irq;
B
Bryan Wu 已提交
130 131
	/*
	 * If interrupts are enabled, IMASK must contain the same value
132
	 * as bfin_irq_flags.  Make sure that invariant holds.  If interrupts
B
Bryan Wu 已提交
133 134 135
	 * are currently disabled we need not do anything; one of the
	 * callers will take care of setting IMASK to the proper value
	 * when reenabling interrupts.
136
	 * local_irq_enable just does "STI bfin_irq_flags", so it's exactly
B
Bryan Wu 已提交
137 138
	 * what we need.
	 */
139 140
	if (!hard_irqs_disabled())
		hard_local_irq_enable();
B
Bryan Wu 已提交
141 142 143
	return;
}

144
void bfin_internal_mask_irq(unsigned int irq)
B
Bryan Wu 已提交
145
{
146
	unsigned long flags = hard_local_irq_save();
147
#ifndef SEC_GCTL
148 149 150
#ifdef SIC_IMASK0
	unsigned mask_bank = SIC_SYSIRQ(irq) / 32;
	unsigned mask_bit = SIC_SYSIRQ(irq) % 32;
151
	bfin_write_SIC_IMASK(mask_bank, bfin_read_SIC_IMASK(mask_bank) &
152 153
			~(1 << mask_bit));
# if defined(CONFIG_SMP) || defined(CONFIG_ICC)
154
	bfin_write_SICB_IMASK(mask_bank, bfin_read_SICB_IMASK(mask_bank) &
155
			~(1 << mask_bit));
156 157 158
# endif
#else
	bfin_write_SIC_IMASK(bfin_read_SIC_IMASK() &
159 160
			~(1 << SIC_SYSIRQ(irq)));
#endif /* end of SIC_IMASK0 */
161
#endif
162
	hard_local_irq_restore(flags);
B
Bryan Wu 已提交
163 164
}

165 166 167 168 169
static void bfin_internal_mask_irq_chip(struct irq_data *d)
{
	bfin_internal_mask_irq(d->irq);
}

170
#ifdef CONFIG_SMP
171
void bfin_internal_unmask_irq_affinity(unsigned int irq,
172 173
		const struct cpumask *affinity)
#else
174
void bfin_internal_unmask_irq(unsigned int irq)
175
#endif
B
Bryan Wu 已提交
176
{
177
	unsigned long flags = hard_local_irq_save();
178

179
#ifndef SEC_GCTL
180 181 182 183
#ifdef SIC_IMASK0
	unsigned mask_bank = SIC_SYSIRQ(irq) / 32;
	unsigned mask_bit = SIC_SYSIRQ(irq) % 32;
# ifdef CONFIG_SMP
184
	if (cpumask_test_cpu(0, affinity))
185
# endif
186
		bfin_write_SIC_IMASK(mask_bank,
187 188
				bfin_read_SIC_IMASK(mask_bank) |
				(1 << mask_bit));
189
# ifdef CONFIG_SMP
190 191
	if (cpumask_test_cpu(1, affinity))
		bfin_write_SICB_IMASK(mask_bank,
192 193
				bfin_read_SICB_IMASK(mask_bank) |
				(1 << mask_bit));
194 195 196
# endif
#else
	bfin_write_SIC_IMASK(bfin_read_SIC_IMASK() |
197 198
			(1 << SIC_SYSIRQ(irq)));
#endif
199
#endif
200 201 202
	hard_local_irq_restore(flags);
}

203
#ifdef SEC_GCTL
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
static void bfin_sec_preflow_handler(struct irq_data *d)
{
	unsigned long flags = hard_local_irq_save();
	unsigned int sid = SIC_SYSIRQ(d->irq);

	bfin_write_SEC_SCI(0, SEC_CSID, sid);

	hard_local_irq_restore(flags);
}

static void bfin_sec_mask_ack_irq(struct irq_data *d)
{
	unsigned long flags = hard_local_irq_save();
	unsigned int sid = SIC_SYSIRQ(d->irq);

	bfin_write_SEC_SCI(0, SEC_CSID, sid);

	hard_local_irq_restore(flags);
}

static void bfin_sec_unmask_irq(struct irq_data *d)
{
	unsigned long flags = hard_local_irq_save();
	unsigned int sid = SIC_SYSIRQ(d->irq);

	bfin_write32(SEC_END, sid);

	hard_local_irq_restore(flags);
}

static void bfin_sec_enable_ssi(unsigned int sid)
{
	unsigned long flags = hard_local_irq_save();
	uint32_t reg_sctl = bfin_read_SEC_SCTL(sid);

	reg_sctl |= SEC_SCTL_SRC_EN;
	bfin_write_SEC_SCTL(sid, reg_sctl);

	hard_local_irq_restore(flags);
}

static void bfin_sec_disable_ssi(unsigned int sid)
{
	unsigned long flags = hard_local_irq_save();
	uint32_t reg_sctl = bfin_read_SEC_SCTL(sid);

	reg_sctl &= ((uint32_t)~SEC_SCTL_SRC_EN);
	bfin_write_SEC_SCTL(sid, reg_sctl);

	hard_local_irq_restore(flags);
}

static void bfin_sec_set_ssi_coreid(unsigned int sid, unsigned int coreid)
{
	unsigned long flags = hard_local_irq_save();
	uint32_t reg_sctl = bfin_read_SEC_SCTL(sid);

	reg_sctl &= ((uint32_t)~SEC_SCTL_CTG);
	bfin_write_SEC_SCTL(sid, reg_sctl | ((coreid << 20) & SEC_SCTL_CTG));

	hard_local_irq_restore(flags);
}

static void bfin_sec_enable_sci(unsigned int sid)
{
	unsigned long flags = hard_local_irq_save();
	uint32_t reg_sctl = bfin_read_SEC_SCTL(sid);

	if (sid == SIC_SYSIRQ(IRQ_WATCH0))
		reg_sctl |= SEC_SCTL_FAULT_EN;
	else
		reg_sctl |= SEC_SCTL_INT_EN;
	bfin_write_SEC_SCTL(sid, reg_sctl);

	hard_local_irq_restore(flags);
}

static void bfin_sec_disable_sci(unsigned int sid)
{
	unsigned long flags = hard_local_irq_save();
	uint32_t reg_sctl = bfin_read_SEC_SCTL(sid);

	reg_sctl &= ((uint32_t)~SEC_SCTL_INT_EN);
	bfin_write_SEC_SCTL(sid, reg_sctl);

	hard_local_irq_restore(flags);
}

static void bfin_sec_enable(struct irq_data *d)
{
	unsigned long flags = hard_local_irq_save();
	unsigned int sid = SIC_SYSIRQ(d->irq);

	bfin_sec_enable_sci(sid);
	bfin_sec_enable_ssi(sid);
299

300
	hard_local_irq_restore(flags);
B
Bryan Wu 已提交
301 302
}

303 304 305 306 307 308 309 310 311 312 313
static void bfin_sec_disable(struct irq_data *d)
{
	unsigned long flags = hard_local_irq_save();
	unsigned int sid = SIC_SYSIRQ(d->irq);

	bfin_sec_disable_sci(sid);
	bfin_sec_disable_ssi(sid);

	hard_local_irq_restore(flags);
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
static void bfin_sec_set_priority(unsigned int sec_int_levels, u8 *sec_int_priority)
{
	unsigned long flags = hard_local_irq_save();
	uint32_t reg_sctl;
	int i;

	bfin_write_SEC_SCI(0, SEC_CPLVL, sec_int_levels);

	for (i = 0; i < SYS_IRQS - BFIN_IRQ(0); i++) {
		reg_sctl = bfin_read_SEC_SCTL(i) & ~SEC_SCTL_PRIO;
		reg_sctl |= sec_int_priority[i] << SEC_SCTL_PRIO_OFFSET;
		bfin_write_SEC_SCTL(i, reg_sctl);
	}

	hard_local_irq_restore(flags);
}

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
static void bfin_sec_raise_irq(unsigned int sid)
{
	unsigned long flags = hard_local_irq_save();

	bfin_write32(SEC_RAISE, sid);

	hard_local_irq_restore(flags);
}

static void init_software_driven_irq(void)
{
	bfin_sec_set_ssi_coreid(34, 0);
	bfin_sec_set_ssi_coreid(35, 1);
	bfin_sec_set_ssi_coreid(36, 0);
	bfin_sec_set_ssi_coreid(37, 1);
}

void bfin_sec_resume(void)
{
	bfin_write_SEC_SCI(0, SEC_CCTL, SEC_CCTL_RESET);
	udelay(100);
	bfin_write_SEC_GCTL(SEC_GCTL_EN);
	bfin_write_SEC_SCI(0, SEC_CCTL, SEC_CCTL_EN | SEC_CCTL_NMI_EN);
}

void handle_sec_sfi_fault(uint32_t gstat)
{

}

void handle_sec_sci_fault(uint32_t gstat)
{
	uint32_t core_id;
	uint32_t cstat;

	core_id = gstat & SEC_GSTAT_SCI;
	cstat = bfin_read_SEC_SCI(core_id, SEC_CSTAT);
	if (cstat & SEC_CSTAT_ERR) {
		switch (cstat & SEC_CSTAT_ERRC) {
		case SEC_CSTAT_ACKERR:
			printk(KERN_DEBUG "sec ack err\n");
			break;
		default:
			printk(KERN_DEBUG "sec sci unknow err\n");
		}
	}

}

void handle_sec_ssi_fault(uint32_t gstat)
{
	uint32_t sid;
	uint32_t sstat;

	sid = gstat & SEC_GSTAT_SID;
	sstat = bfin_read_SEC_SSTAT(sid);

}

void handle_sec_fault(unsigned int irq, struct irq_desc *desc)
{
	uint32_t sec_gstat;

	raw_spin_lock(&desc->lock);

	sec_gstat = bfin_read32(SEC_GSTAT);
	if (sec_gstat & SEC_GSTAT_ERR) {

		switch (sec_gstat & SEC_GSTAT_ERRC) {
		case 0:
			handle_sec_sfi_fault(sec_gstat);
			break;
		case SEC_GSTAT_SCIERR:
			handle_sec_sci_fault(sec_gstat);
			break;
		case SEC_GSTAT_SSIERR:
			handle_sec_ssi_fault(sec_gstat);
			break;
		}


	}

	raw_spin_unlock(&desc->lock);
}

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
void handle_core_fault(unsigned int irq, struct irq_desc *desc)
{
	struct pt_regs *fp = get_irq_regs();

	raw_spin_lock(&desc->lock);

	switch (irq) {
	case IRQ_C0_DBL_FAULT:
		double_fault_c(fp);
		break;
	case IRQ_C0_HW_ERR:
		dump_bfin_process(fp);
		dump_bfin_mem(fp);
		show_regs(fp);
		printk(KERN_NOTICE "Kernel Stack\n");
		show_stack(current, NULL);
		print_modules();
		panic("Kernel core hardware error");
		break;
	case IRQ_C0_NMI_L1_PARITY_ERR:
		panic("NMI %d occurs unexpectedly");
		break;
	default:
		panic("Core 1 fault %d occurs unexpectedly");
	}

	raw_spin_unlock(&desc->lock);
}
445 446
#endif

447
#ifdef CONFIG_SMP
448
static void bfin_internal_unmask_irq_chip(struct irq_data *d)
449
{
450
	bfin_internal_unmask_irq_affinity(d->irq, d->affinity);
451 452
}

453 454
static int bfin_internal_set_affinity(struct irq_data *d,
				      const struct cpumask *mask, bool force)
455
{
456 457
	bfin_internal_mask_irq(d->irq);
	bfin_internal_unmask_irq_affinity(d->irq, mask);
458 459 460

	return 0;
}
461 462 463 464 465
#else
static void bfin_internal_unmask_irq_chip(struct irq_data *d)
{
	bfin_internal_unmask_irq(d->irq);
}
466 467
#endif

468
#if defined(CONFIG_PM) && !defined(SEC_GCTL)
469 470
int bfin_internal_set_wake(unsigned int irq, unsigned int state)
{
471
	u32 bank, bit, wakeup = 0;
472
	unsigned long flags;
473 474
	bank = SIC_SYSIRQ(irq) / 32;
	bit = SIC_SYSIRQ(irq) % 32;
475

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	switch (irq) {
#ifdef IRQ_RTC
	case IRQ_RTC:
	wakeup |= WAKE;
	break;
#endif
#ifdef IRQ_CAN0_RX
	case IRQ_CAN0_RX:
	wakeup |= CANWE;
	break;
#endif
#ifdef IRQ_CAN1_RX
	case IRQ_CAN1_RX:
	wakeup |= CANWE;
	break;
#endif
#ifdef IRQ_USB_INT0
	case IRQ_USB_INT0:
	wakeup |= USBWE;
	break;
#endif
497
#ifdef CONFIG_BF54x
498 499 500 501 502 503 504 505
	case IRQ_CNT:
	wakeup |= ROTWE;
	break;
#endif
	default:
	break;
	}

506
	flags = hard_local_irq_save();
507

508
	if (state) {
509
		bfin_sic_iwr[bank] |= (1 << bit);
510 511 512
		vr_wakeup  |= wakeup;

	} else {
513
		bfin_sic_iwr[bank] &= ~(1 << bit);
514 515
		vr_wakeup  &= ~wakeup;
	}
516

517
	hard_local_irq_restore(flags);
518 519 520

	return 0;
}
521 522 523 524 525

static int bfin_internal_set_wake_chip(struct irq_data *d, unsigned int state)
{
	return bfin_internal_set_wake(d->irq, state);
}
526
#else
527 528 529 530
inline int bfin_internal_set_wake(unsigned int irq, unsigned int state)
{
	return 0;
}
531
# define bfin_internal_set_wake_chip NULL
532 533
#endif

B
Bryan Wu 已提交
534
static struct irq_chip bfin_core_irqchip = {
535
	.name = "CORE",
536 537
	.irq_mask = bfin_core_mask_irq,
	.irq_unmask = bfin_core_unmask_irq,
B
Bryan Wu 已提交
538 539 540
};

static struct irq_chip bfin_internal_irqchip = {
541
	.name = "INTN",
542 543 544 545
	.irq_mask = bfin_internal_mask_irq_chip,
	.irq_unmask = bfin_internal_unmask_irq_chip,
	.irq_disable = bfin_internal_mask_irq_chip,
	.irq_enable = bfin_internal_unmask_irq_chip,
546
#ifdef CONFIG_SMP
547
	.irq_set_affinity = bfin_internal_set_affinity,
548
#endif
549
	.irq_set_wake = bfin_internal_set_wake_chip,
B
Bryan Wu 已提交
550 551
};

552
#ifdef SEC_GCTL
553 554 555 556 557 558 559 560 561 562 563
static struct irq_chip bfin_sec_irqchip = {
	.name = "SEC",
	.irq_mask_ack = bfin_sec_mask_ack_irq,
	.irq_mask = bfin_sec_mask_ack_irq,
	.irq_unmask = bfin_sec_unmask_irq,
	.irq_eoi = bfin_sec_unmask_irq,
	.irq_disable = bfin_sec_disable,
	.irq_enable = bfin_sec_enable,
};
#endif

564
void bfin_handle_irq(unsigned irq)
565 566 567 568 569 570 571
{
#ifdef CONFIG_IPIPE
	struct pt_regs regs;    /* Contents not used. */
	ipipe_trace_irq_entry(irq);
	__ipipe_handle_irq(irq, &regs);
	ipipe_trace_irq_exit(irq);
#else /* !CONFIG_IPIPE */
572
	generic_handle_irq(irq);
573 574 575
#endif  /* !CONFIG_IPIPE */
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
#if defined(CONFIG_BFIN_MAC) || defined(CONFIG_BFIN_MAC_MODULE)
static int mac_stat_int_mask;

static void bfin_mac_status_ack_irq(unsigned int irq)
{
	switch (irq) {
	case IRQ_MAC_MMCINT:
		bfin_write_EMAC_MMC_TIRQS(
			bfin_read_EMAC_MMC_TIRQE() &
			bfin_read_EMAC_MMC_TIRQS());
		bfin_write_EMAC_MMC_RIRQS(
			bfin_read_EMAC_MMC_RIRQE() &
			bfin_read_EMAC_MMC_RIRQS());
		break;
	case IRQ_MAC_RXFSINT:
		bfin_write_EMAC_RX_STKY(
			bfin_read_EMAC_RX_IRQE() &
			bfin_read_EMAC_RX_STKY());
		break;
	case IRQ_MAC_TXFSINT:
		bfin_write_EMAC_TX_STKY(
			bfin_read_EMAC_TX_IRQE() &
			bfin_read_EMAC_TX_STKY());
		break;
	case IRQ_MAC_WAKEDET:
		 bfin_write_EMAC_WKUP_CTL(
			bfin_read_EMAC_WKUP_CTL() | MPKS | RWKS);
		break;
	default:
		/* These bits are W1C */
		bfin_write_EMAC_SYSTAT(1L << (irq - IRQ_MAC_PHYINT));
		break;
	}
}

611
static void bfin_mac_status_mask_irq(struct irq_data *d)
612
{
613 614
	unsigned int irq = d->irq;

615
	mac_stat_int_mask &= ~(1L << (irq - IRQ_MAC_PHYINT));
616
#ifdef BF537_FAMILY
617 618 619 620 621 622 623 624 625 626 627 628 629 630
	switch (irq) {
	case IRQ_MAC_PHYINT:
		bfin_write_EMAC_SYSCTL(bfin_read_EMAC_SYSCTL() & ~PHYIE);
		break;
	default:
		break;
	}
#else
	if (!mac_stat_int_mask)
		bfin_internal_mask_irq(IRQ_MAC_ERROR);
#endif
	bfin_mac_status_ack_irq(irq);
}

631
static void bfin_mac_status_unmask_irq(struct irq_data *d)
632
{
633 634
	unsigned int irq = d->irq;

635
#ifdef BF537_FAMILY
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
	switch (irq) {
	case IRQ_MAC_PHYINT:
		bfin_write_EMAC_SYSCTL(bfin_read_EMAC_SYSCTL() | PHYIE);
		break;
	default:
		break;
	}
#else
	if (!mac_stat_int_mask)
		bfin_internal_unmask_irq(IRQ_MAC_ERROR);
#endif
	mac_stat_int_mask |= 1L << (irq - IRQ_MAC_PHYINT);
}

#ifdef CONFIG_PM
651
int bfin_mac_status_set_wake(struct irq_data *d, unsigned int state)
652
{
653
#ifdef BF537_FAMILY
654 655 656 657 658
	return bfin_internal_set_wake(IRQ_GENERIC_ERROR, state);
#else
	return bfin_internal_set_wake(IRQ_MAC_ERROR, state);
#endif
}
659 660
#else
# define bfin_mac_status_set_wake NULL
661 662 663 664
#endif

static struct irq_chip bfin_mac_status_irqchip = {
	.name = "MACST",
665 666 667
	.irq_mask = bfin_mac_status_mask_irq,
	.irq_unmask = bfin_mac_status_unmask_irq,
	.irq_set_wake = bfin_mac_status_set_wake,
668 669
};

670 671
void bfin_demux_mac_status_irq(unsigned int int_err_irq,
			       struct irq_desc *inta_desc)
672 673 674 675
{
	int i, irq = 0;
	u32 status = bfin_read_EMAC_SYSTAT();

676
	for (i = 0; i <= (IRQ_MAC_STMDONE - IRQ_MAC_PHYINT); i++)
677 678 679 680 681 682 683 684 685 686 687
		if (status & (1L << i)) {
			irq = IRQ_MAC_PHYINT + i;
			break;
		}

	if (irq) {
		if (mac_stat_int_mask & (1L << (irq - IRQ_MAC_PHYINT))) {
			bfin_handle_irq(irq);
		} else {
			bfin_mac_status_ack_irq(irq);
			pr_debug("IRQ %d:"
688 689
					" MASKED MAC ERROR INTERRUPT ASSERTED\n",
					irq);
690 691 692
		}
	} else
		printk(KERN_ERR
693 694 695 696
				"%s : %s : LINE %d :\nIRQ ?: MAC ERROR"
				" INTERRUPT ASSERTED BUT NO SOURCE FOUND"
				"(EMAC_SYSTAT=0x%X)\n",
				__func__, __FILE__, __LINE__, status);
697 698 699
}
#endif

700 701
static inline void bfin_set_irq_handler(unsigned irq, irq_flow_handler_t handle)
{
702
#ifdef CONFIG_IPIPE
703
	handle = handle_level_irq;
704
#endif
T
Thomas Gleixner 已提交
705
	__irq_set_handler_locked(irq, handle);
706 707
}

708
static DECLARE_BITMAP(gpio_enabled, MAX_BLACKFIN_GPIOS);
709
extern void bfin_gpio_irq_prepare(unsigned gpio);
710

711
#if !BFIN_GPIO_PINT
712

713
static void bfin_gpio_ack_irq(struct irq_data *d)
B
Bryan Wu 已提交
714
{
715 716 717
	/* AFAIK ack_irq in case mask_ack is provided
	 * get's only called for edge sense irqs
	 */
718
	set_gpio_data(irq_to_gpio(d->irq), 0);
B
Bryan Wu 已提交
719 720
}

721
static void bfin_gpio_mask_ack_irq(struct irq_data *d)
B
Bryan Wu 已提交
722
{
723
	unsigned int irq = d->irq;
724
	u32 gpionr = irq_to_gpio(irq);
B
Bryan Wu 已提交
725

T
Thomas Gleixner 已提交
726
	if (!irqd_is_level_type(d))
B
Bryan Wu 已提交
727 728 729 730 731
		set_gpio_data(gpionr, 0);

	set_gpio_maska(gpionr, 0);
}

732
static void bfin_gpio_mask_irq(struct irq_data *d)
B
Bryan Wu 已提交
733
{
734
	set_gpio_maska(irq_to_gpio(d->irq), 0);
B
Bryan Wu 已提交
735 736
}

737
static void bfin_gpio_unmask_irq(struct irq_data *d)
B
Bryan Wu 已提交
738
{
739
	set_gpio_maska(irq_to_gpio(d->irq), 1);
B
Bryan Wu 已提交
740 741
}

742
static unsigned int bfin_gpio_irq_startup(struct irq_data *d)
B
Bryan Wu 已提交
743
{
744
	u32 gpionr = irq_to_gpio(d->irq);
B
Bryan Wu 已提交
745

746
	if (__test_and_set_bit(gpionr, gpio_enabled))
747
		bfin_gpio_irq_prepare(gpionr);
B
Bryan Wu 已提交
748

749
	bfin_gpio_unmask_irq(d);
B
Bryan Wu 已提交
750

751
	return 0;
B
Bryan Wu 已提交
752 753
}

754
static void bfin_gpio_irq_shutdown(struct irq_data *d)
B
Bryan Wu 已提交
755
{
756
	u32 gpionr = irq_to_gpio(d->irq);
757

758
	bfin_gpio_mask_irq(d);
759
	__clear_bit(gpionr, gpio_enabled);
760
	bfin_gpio_irq_free(gpionr);
B
Bryan Wu 已提交
761 762
}

763
static int bfin_gpio_irq_type(struct irq_data *d, unsigned int type)
B
Bryan Wu 已提交
764
{
765
	unsigned int irq = d->irq;
766 767
	int ret;
	char buf[16];
768
	u32 gpionr = irq_to_gpio(irq);
B
Bryan Wu 已提交
769 770 771

	if (type == IRQ_TYPE_PROBE) {
		/* only probe unenabled GPIO interrupt lines */
772
		if (test_bit(gpionr, gpio_enabled))
B
Bryan Wu 已提交
773 774 775 776 777
			return 0;
		type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
	}

	if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
778
		    IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
779

780 781 782 783 784
		snprintf(buf, 16, "gpio-irq%d", irq);
		ret = bfin_gpio_irq_request(gpionr, buf);
		if (ret)
			return ret;

785
		if (__test_and_set_bit(gpionr, gpio_enabled))
786
			bfin_gpio_irq_prepare(gpionr);
B
Bryan Wu 已提交
787 788

	} else {
789
		__clear_bit(gpionr, gpio_enabled);
B
Bryan Wu 已提交
790 791 792
		return 0;
	}

793
	set_gpio_inen(gpionr, 0);
B
Bryan Wu 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806
	set_gpio_dir(gpionr, 0);

	if ((type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
	    == (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
		set_gpio_both(gpionr, 1);
	else
		set_gpio_both(gpionr, 0);

	if ((type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW)))
		set_gpio_polar(gpionr, 1);	/* low or falling edge denoted by one */
	else
		set_gpio_polar(gpionr, 0);	/* high or rising edge denoted by zero */

807 808 809 810 811 812 813 814 815 816
	if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
		set_gpio_edge(gpionr, 1);
		set_gpio_inen(gpionr, 1);
		set_gpio_data(gpionr, 0);

	} else {
		set_gpio_edge(gpionr, 0);
		set_gpio_inen(gpionr, 1);
	}

B
Bryan Wu 已提交
817
	if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
818
		bfin_set_irq_handler(irq, handle_edge_irq);
B
Bryan Wu 已提交
819
	else
820
		bfin_set_irq_handler(irq, handle_level_irq);
B
Bryan Wu 已提交
821 822 823 824

	return 0;
}

825
#ifdef CONFIG_PM
826
static int bfin_gpio_set_wake(struct irq_data *d, unsigned int state)
827
{
828
	return gpio_pm_wakeup_ctrl(irq_to_gpio(d->irq), state);
829
}
830 831
#else
# define bfin_gpio_set_wake NULL
832 833
#endif

834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
static void bfin_demux_gpio_block(unsigned int irq)
{
	unsigned int gpio, mask;

	gpio = irq_to_gpio(irq);
	mask = get_gpiop_data(gpio) & get_gpiop_maska(gpio);

	while (mask) {
		if (mask & 1)
			bfin_handle_irq(irq);
		irq++;
		mask >>= 1;
	}
}

849
void bfin_demux_gpio_irq(unsigned int inta_irq,
850
			struct irq_desc *desc)
B
Bryan Wu 已提交
851
{
852
	unsigned int irq;
853 854

	switch (inta_irq) {
855
#if defined(BF537_FAMILY)
856
	case IRQ_PF_INTA_PG_INTA:
857 858
		bfin_demux_gpio_block(IRQ_PF0);
		irq = IRQ_PG0;
859
		break;
860
	case IRQ_PH_INTA_MAC_RX:
861 862
		irq = IRQ_PH0;
		break;
863 864 865 866
#elif defined(BF533_FAMILY)
	case IRQ_PROG_INTA:
		irq = IRQ_PF0;
		break;
867
#elif defined(BF538_FAMILY)
868 869 870
	case IRQ_PORTF_INTA:
		irq = IRQ_PF0;
		break;
871
#elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
	case IRQ_PORTF_INTA:
		irq = IRQ_PF0;
		break;
	case IRQ_PORTG_INTA:
		irq = IRQ_PG0;
		break;
	case IRQ_PORTH_INTA:
		irq = IRQ_PH0;
		break;
#elif defined(CONFIG_BF561)
	case IRQ_PROG0_INTA:
		irq = IRQ_PF0;
		break;
	case IRQ_PROG1_INTA:
		irq = IRQ_PF16;
		break;
	case IRQ_PROG2_INTA:
		irq = IRQ_PF32;
		break;
#endif
	default:
		BUG();
		return;
	}

897
	bfin_demux_gpio_block(irq);
B
Bryan Wu 已提交
898 899
}

900
#else
901

902
#define NR_PINT_BITS		32
903 904 905 906 907 908 909
#define IRQ_NOT_AVAIL		0xFF

#define PINT_2_BANK(x)		((x) >> 5)
#define PINT_2_BIT(x)		((x) & 0x1F)
#define PINT_BIT(x)		(1 << (PINT_2_BIT(x)))

static unsigned char irq2pint_lut[NR_PINTS];
910
static unsigned char pint2irq_lut[NR_PINT_SYS_IRQS * NR_PINT_BITS];
911

912 913 914 915 916
static struct bfin_pint_regs * const pint[NR_PINT_SYS_IRQS] = {
	(struct bfin_pint_regs *)PINT0_MASK_SET,
	(struct bfin_pint_regs *)PINT1_MASK_SET,
	(struct bfin_pint_regs *)PINT2_MASK_SET,
	(struct bfin_pint_regs *)PINT3_MASK_SET,
917 918 919 920
#ifdef CONFIG_BF60x
	(struct bfin_pint_regs *)PINT4_MASK_SET,
	(struct bfin_pint_regs *)PINT5_MASK_SET,
#endif
921 922
};

923
inline unsigned int get_irq_base(u32 bank, u8 bmap)
924
{
925
	unsigned int irq_base;
926

927
#ifndef CONFIG_BF60x
928 929 930 931 932
	if (bank < 2) {		/*PA-PB */
		irq_base = IRQ_PA0 + bmap * 16;
	} else {		/*PC-PJ */
		irq_base = IRQ_PC0 + bmap * 16;
	}
933 934
#else
	irq_base = IRQ_PA0 + bank * 16 + bmap * 16;
935
#endif
936 937
	return irq_base;
}
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960

	/* Whenever PINTx_ASSIGN is altered init_pint_lut() must be executed! */
void init_pint_lut(void)
{
	u16 bank, bit, irq_base, bit_pos;
	u32 pint_assign;
	u8 bmap;

	memset(irq2pint_lut, IRQ_NOT_AVAIL, sizeof(irq2pint_lut));

	for (bank = 0; bank < NR_PINT_SYS_IRQS; bank++) {

		pint_assign = pint[bank]->assign;

		for (bit = 0; bit < NR_PINT_BITS; bit++) {

			bmap = (pint_assign >> ((bit / 8) * 8)) & 0xFF;

			irq_base = get_irq_base(bank, bmap);

			irq_base += (bit % 8) + ((bit / 8) & 1 ? 8 : 0);
			bit_pos = bit + bank * NR_PINT_BITS;

961
			pint2irq_lut[bit_pos] = irq_base - SYS_IRQS;
962 963 964 965 966
			irq2pint_lut[irq_base - SYS_IRQS] = bit_pos;
		}
	}
}

967
static void bfin_gpio_ack_irq(struct irq_data *d)
968
{
969
	u32 pint_val = irq2pint_lut[d->irq - SYS_IRQS];
970
	u32 pintbit = PINT_BIT(pint_val);
971
	u32 bank = PINT_2_BANK(pint_val);
972

T
Thomas Gleixner 已提交
973
	if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH) {
974 975 976 977 978 979
		if (pint[bank]->invert_set & pintbit)
			pint[bank]->invert_clear = pintbit;
		else
			pint[bank]->invert_set = pintbit;
	}
	pint[bank]->request = pintbit;
980 981 982

}

983
static void bfin_gpio_mask_ack_irq(struct irq_data *d)
984
{
985
	u32 pint_val = irq2pint_lut[d->irq - SYS_IRQS];
986
	u32 pintbit = PINT_BIT(pint_val);
987
	u32 bank = PINT_2_BANK(pint_val);
988

T
Thomas Gleixner 已提交
989
	if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH) {
990 991 992 993 994 995
		if (pint[bank]->invert_set & pintbit)
			pint[bank]->invert_clear = pintbit;
		else
			pint[bank]->invert_set = pintbit;
	}

996 997
	pint[bank]->request = pintbit;
	pint[bank]->mask_clear = pintbit;
998 999
}

1000
static void bfin_gpio_mask_irq(struct irq_data *d)
1001
{
1002
	u32 pint_val = irq2pint_lut[d->irq - SYS_IRQS];
1003 1004 1005 1006

	pint[PINT_2_BANK(pint_val)]->mask_clear = PINT_BIT(pint_val);
}

1007
static void bfin_gpio_unmask_irq(struct irq_data *d)
1008
{
1009
	u32 pint_val = irq2pint_lut[d->irq - SYS_IRQS];
1010
	u32 pintbit = PINT_BIT(pint_val);
1011
	u32 bank = PINT_2_BANK(pint_val);
1012

1013
	pint[bank]->mask_set = pintbit;
1014 1015
}

1016
static unsigned int bfin_gpio_irq_startup(struct irq_data *d)
1017
{
1018
	unsigned int irq = d->irq;
1019 1020
	u32 gpionr = irq_to_gpio(irq);
	u32 pint_val = irq2pint_lut[irq - SYS_IRQS];
1021

1022 1023 1024 1025
	if (pint_val == IRQ_NOT_AVAIL) {
		printk(KERN_ERR
		"GPIO IRQ %d :Not in PINT Assign table "
		"Reconfigure Interrupt to Port Assignemt\n", irq);
1026
		return -ENODEV;
1027
	}
1028

1029
	if (__test_and_set_bit(gpionr, gpio_enabled))
1030
		bfin_gpio_irq_prepare(gpionr);
1031

1032
	bfin_gpio_unmask_irq(d);
1033

1034
	return 0;
1035 1036
}

1037
static void bfin_gpio_irq_shutdown(struct irq_data *d)
1038
{
1039
	u32 gpionr = irq_to_gpio(d->irq);
1040

1041
	bfin_gpio_mask_irq(d);
1042
	__clear_bit(gpionr, gpio_enabled);
1043
	bfin_gpio_irq_free(gpionr);
1044 1045
}

1046
static int bfin_gpio_irq_type(struct irq_data *d, unsigned int type)
1047
{
1048
	unsigned int irq = d->irq;
1049 1050
	int ret;
	char buf[16];
1051 1052
	u32 gpionr = irq_to_gpio(irq);
	u32 pint_val = irq2pint_lut[irq - SYS_IRQS];
1053
	u32 pintbit = PINT_BIT(pint_val);
1054
	u32 bank = PINT_2_BANK(pint_val);
1055 1056 1057 1058 1059 1060

	if (pint_val == IRQ_NOT_AVAIL)
		return -ENODEV;

	if (type == IRQ_TYPE_PROBE) {
		/* only probe unenabled GPIO interrupt lines */
1061
		if (test_bit(gpionr, gpio_enabled))
1062 1063 1064 1065 1066 1067
			return 0;
		type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
	}

	if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
		    IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
1068 1069 1070 1071 1072 1073

		snprintf(buf, 16, "gpio-irq%d", irq);
		ret = bfin_gpio_irq_request(gpionr, buf);
		if (ret)
			return ret;

1074
		if (__test_and_set_bit(gpionr, gpio_enabled))
1075
			bfin_gpio_irq_prepare(gpionr);
1076 1077

	} else {
1078
		__clear_bit(gpionr, gpio_enabled);
1079 1080 1081 1082
		return 0;
	}

	if ((type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW)))
1083
		pint[bank]->invert_set = pintbit;	/* low or falling edge denoted by one */
1084
	else
1085
		pint[bank]->invert_clear = pintbit;	/* high or rising edge denoted by zero */
1086

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	if ((type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
	    == (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
		if (gpio_get_value(gpionr))
			pint[bank]->invert_set = pintbit;
		else
			pint[bank]->invert_clear = pintbit;
	}

	if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
		pint[bank]->edge_set = pintbit;
1097
		bfin_set_irq_handler(irq, handle_edge_irq);
1098 1099
	} else {
		pint[bank]->edge_clear = pintbit;
1100
		bfin_set_irq_handler(irq, handle_level_irq);
1101 1102
	}

1103 1104 1105
	return 0;
}

1106
#ifdef CONFIG_PM
1107 1108 1109
static struct bfin_pm_pint_save save_pint_reg[NR_PINT_SYS_IRQS];
static u32 save_pint_sec_ctl[NR_PINT_SYS_IRQS];

1110
static int bfin_gpio_set_wake(struct irq_data *d, unsigned int state)
1111 1112
{
	u32 pint_irq;
1113
	u32 pint_val = irq2pint_lut[d->irq - SYS_IRQS];
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
	u32 bank = PINT_2_BANK(pint_val);

	switch (bank) {
	case 0:
		pint_irq = IRQ_PINT0;
		break;
	case 2:
		pint_irq = IRQ_PINT2;
		break;
	case 3:
		pint_irq = IRQ_PINT3;
		break;
	case 1:
		pint_irq = IRQ_PINT1;
		break;
1129
#ifdef CONFIG_BF60x
1130 1131 1132 1133 1134 1135
	case 4:
		pint_irq = IRQ_PINT4;
		break;
	case 5:
		pint_irq = IRQ_PINT5;
		break;
1136
#endif
1137 1138 1139 1140 1141 1142 1143 1144
	default:
		return -EINVAL;
	}

	bfin_internal_set_wake(pint_irq, state);

	return 0;
}
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

void bfin_pint_suspend(void)
{
	u32 bank;

	for (bank = 0; bank < NR_PINT_SYS_IRQS; bank++) {
		save_pint_reg[bank].mask_set = pint[bank]->mask_set;
		save_pint_reg[bank].assign = pint[bank]->assign;
		save_pint_reg[bank].edge_set = pint[bank]->edge_set;
		save_pint_reg[bank].invert_set = pint[bank]->invert_set;
	}
}

void bfin_pint_resume(void)
{
	u32 bank;

	for (bank = 0; bank < NR_PINT_SYS_IRQS; bank++) {
		pint[bank]->mask_set = save_pint_reg[bank].mask_set;
		pint[bank]->assign = save_pint_reg[bank].assign;
		pint[bank]->edge_set = save_pint_reg[bank].edge_set;
		pint[bank]->invert_set = save_pint_reg[bank].invert_set;
	}
}

1170
#ifdef SEC_GCTL
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
static int sec_suspend(void)
{
	u32 bank;

	for (bank = 0; bank < NR_PINT_SYS_IRQS; bank++)
		save_pint_sec_ctl[bank] = bfin_read_SEC_SCTL(bank + SIC_SYSIRQ(IRQ_PINT0));
	return 0;
}

static void sec_resume(void)
{
	u32 bank;

	bfin_write_SEC_SCI(0, SEC_CCTL, SEC_CCTL_RESET);
	udelay(100);
	bfin_write_SEC_GCTL(SEC_GCTL_EN);
	bfin_write_SEC_SCI(0, SEC_CCTL, SEC_CCTL_EN | SEC_CCTL_NMI_EN);

	for (bank = 0; bank < NR_PINT_SYS_IRQS; bank++)
		bfin_write_SEC_SCTL(bank + SIC_SYSIRQ(IRQ_PINT0), save_pint_sec_ctl[bank]);
}

static struct syscore_ops sec_pm_syscore_ops = {
	.suspend = sec_suspend,
	.resume = sec_resume,
};
1197
#endif
1198 1199
#else
# define bfin_gpio_set_wake NULL
1200 1201
#endif

1202
void bfin_demux_gpio_irq(unsigned int inta_irq,
1203
			struct irq_desc *desc)
1204
{
1205
	u32 bank, pint_val;
1206
	u32 request, irq;
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
	u32 level_mask;
	int umask = 0;
	struct irq_chip *chip = irq_desc_get_chip(desc);

	if (chip->irq_mask_ack) {
		chip->irq_mask_ack(&desc->irq_data);
	} else {
		chip->irq_mask(&desc->irq_data);
		if (chip->irq_ack)
			chip->irq_ack(&desc->irq_data);
	}
1218

1219
	switch (inta_irq) {
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
	case IRQ_PINT0:
		bank = 0;
		break;
	case IRQ_PINT2:
		bank = 2;
		break;
	case IRQ_PINT3:
		bank = 3;
		break;
	case IRQ_PINT1:
		bank = 1;
		break;
1232 1233 1234 1235 1236 1237 1238 1239
#ifdef CONFIG_BF60x
	case IRQ_PINT4:
		bank = 4;
		break;
	case IRQ_PINT5:
		bank = 5;
		break;
#endif
1240 1241
	default:
		return;
1242 1243 1244 1245 1246 1247
	}

	pint_val = bank * NR_PINT_BITS;

	request = pint[bank]->request;

1248 1249
	level_mask = pint[bank]->edge_set & request;

1250 1251
	while (request) {
		if (request & 1) {
1252
			irq = pint2irq_lut[pint_val] + SYS_IRQS;
1253 1254 1255 1256
			if (level_mask & PINT_BIT(pint_val)) {
				umask = 1;
				chip->irq_unmask(&desc->irq_data);
			}
1257
			bfin_handle_irq(irq);
1258 1259 1260 1261 1262
		}
		pint_val++;
		request >>= 1;
	}

1263 1264
	if (!umask)
		chip->irq_unmask(&desc->irq_data);
1265
}
1266
#endif
B
Bryan Wu 已提交
1267

1268 1269
static struct irq_chip bfin_gpio_irqchip = {
	.name = "GPIO",
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
	.irq_ack = bfin_gpio_ack_irq,
	.irq_mask = bfin_gpio_mask_irq,
	.irq_mask_ack = bfin_gpio_mask_ack_irq,
	.irq_unmask = bfin_gpio_unmask_irq,
	.irq_disable = bfin_gpio_mask_irq,
	.irq_enable = bfin_gpio_unmask_irq,
	.irq_set_type = bfin_gpio_irq_type,
	.irq_startup = bfin_gpio_irq_startup,
	.irq_shutdown = bfin_gpio_irq_shutdown,
	.irq_set_wake = bfin_gpio_set_wake,
1280 1281
};

1282
void __cpuinit init_exception_vectors(void)
1283
{
1284 1285 1286 1287 1288
	/* cannot program in software:
	 * evt0 - emulation (jtag)
	 * evt1 - reset
	 */
	bfin_write_EVT2(evt_nmi);
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
	bfin_write_EVT3(trap);
	bfin_write_EVT5(evt_ivhw);
	bfin_write_EVT6(evt_timer);
	bfin_write_EVT7(evt_evt7);
	bfin_write_EVT8(evt_evt8);
	bfin_write_EVT9(evt_evt9);
	bfin_write_EVT10(evt_evt10);
	bfin_write_EVT11(evt_evt11);
	bfin_write_EVT12(evt_evt12);
	bfin_write_EVT13(evt_evt13);
1299
	bfin_write_EVT14(evt_evt14);
1300 1301 1302 1303
	bfin_write_EVT15(evt_system_call);
	CSYNC();
}

1304
#ifndef SEC_GCTL
B
Bryan Wu 已提交
1305 1306 1307 1308
/*
 * This function should be called during kernel startup to initialize
 * the BFin IRQ handling routines.
 */
1309

B
Bryan Wu 已提交
1310 1311 1312 1313
int __init init_arch_irq(void)
{
	int irq;
	unsigned long ilat = 0;
1314

B
Bryan Wu 已提交
1315
	/*  Disable all the peripheral intrs  - page 4-29 HW Ref manual */
1316
#ifdef SIC_IMASK0
1317 1318
	bfin_write_SIC_IMASK0(SIC_UNMASK_ALL);
	bfin_write_SIC_IMASK1(SIC_UNMASK_ALL);
1319
# ifdef SIC_IMASK2
1320
	bfin_write_SIC_IMASK2(SIC_UNMASK_ALL);
1321
# endif
1322
# if defined(CONFIG_SMP) || defined(CONFIG_ICC)
1323 1324 1325
	bfin_write_SICB_IMASK0(SIC_UNMASK_ALL);
	bfin_write_SICB_IMASK1(SIC_UNMASK_ALL);
# endif
1326
#else
B
Bryan Wu 已提交
1327
	bfin_write_SIC_IMASK(SIC_UNMASK_ALL);
1328
#endif
B
Bryan Wu 已提交
1329 1330 1331

	local_irq_disable();

1332
#if BFIN_GPIO_PINT
1333
# ifdef CONFIG_PINTx_REASSIGN
1334 1335 1336 1337
	pint[0]->assign = CONFIG_PINT0_ASSIGN;
	pint[1]->assign = CONFIG_PINT1_ASSIGN;
	pint[2]->assign = CONFIG_PINT2_ASSIGN;
	pint[3]->assign = CONFIG_PINT3_ASSIGN;
1338
# endif
1339 1340 1341 1342 1343
	/* Whenever PINTx_ASSIGN is altered init_pint_lut() must be executed! */
	init_pint_lut();
#endif

	for (irq = 0; irq <= SYS_IRQS; irq++) {
B
Bryan Wu 已提交
1344
		if (irq <= IRQ_CORETMR)
T
Thomas Gleixner 已提交
1345
			irq_set_chip(irq, &bfin_core_irqchip);
B
Bryan Wu 已提交
1346
		else
T
Thomas Gleixner 已提交
1347
			irq_set_chip(irq, &bfin_internal_irqchip);
B
Bryan Wu 已提交
1348

1349
		switch (irq) {
1350
#if BFIN_GPIO_PINT
1351 1352 1353 1354
		case IRQ_PINT0:
		case IRQ_PINT1:
		case IRQ_PINT2:
		case IRQ_PINT3:
1355 1356 1357 1358 1359
#elif defined(BF537_FAMILY)
		case IRQ_PH_INTA_MAC_RX:
		case IRQ_PF_INTA_PG_INTA:
#elif defined(BF533_FAMILY)
		case IRQ_PROG_INTA:
1360
#elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
1361 1362 1363
		case IRQ_PORTF_INTA:
		case IRQ_PORTG_INTA:
		case IRQ_PORTH_INTA:
1364
#elif defined(CONFIG_BF561)
1365 1366 1367
		case IRQ_PROG0_INTA:
		case IRQ_PROG1_INTA:
		case IRQ_PROG2_INTA:
1368
#elif defined(BF538_FAMILY)
1369
		case IRQ_PORTF_INTA:
B
Bryan Wu 已提交
1370
#endif
T
Thomas Gleixner 已提交
1371
			irq_set_chained_handler(irq, bfin_demux_gpio_irq);
1372
			break;
1373 1374
#if defined(CONFIG_BFIN_MAC) || defined(CONFIG_BFIN_MAC_MODULE)
		case IRQ_MAC_ERROR:
T
Thomas Gleixner 已提交
1375 1376
			irq_set_chained_handler(irq,
						bfin_demux_mac_status_irq);
1377 1378
			break;
#endif
1379
#if defined(CONFIG_SMP) || defined(CONFIG_ICC)
1380 1381
		case IRQ_SUPPLE_0:
		case IRQ_SUPPLE_1:
T
Thomas Gleixner 已提交
1382
			irq_set_handler(irq, handle_percpu_irq);
1383 1384
			break;
#endif
1385

1386 1387 1388
#ifdef CONFIG_TICKSOURCE_CORETMR
		case IRQ_CORETMR:
# ifdef CONFIG_SMP
T
Thomas Gleixner 已提交
1389
			irq_set_handler(irq, handle_percpu_irq);
1390
# else
T
Thomas Gleixner 已提交
1391
			irq_set_handler(irq, handle_simple_irq);
1392
# endif
1393
			break;
1394
#endif
1395 1396 1397

#ifdef CONFIG_TICKSOURCE_GPTMR0
		case IRQ_TIMER0:
T
Thomas Gleixner 已提交
1398
			irq_set_handler(irq, handle_simple_irq);
1399
			break;
1400 1401
#endif

1402
		default:
1403
#ifdef CONFIG_IPIPE
T
Thomas Gleixner 已提交
1404
			irq_set_handler(irq, handle_level_irq);
1405
#else
T
Thomas Gleixner 已提交
1406
			irq_set_handler(irq, handle_simple_irq);
1407
#endif
1408 1409
			break;
		}
B
Bryan Wu 已提交
1410
	}
1411

1412
	init_mach_irq();
B
Bryan Wu 已提交
1413

1414
#if (defined(CONFIG_BFIN_MAC) || defined(CONFIG_BFIN_MAC_MODULE))
1415
	for (irq = IRQ_MAC_PHYINT; irq <= IRQ_MAC_STMDONE; irq++)
T
Thomas Gleixner 已提交
1416
		irq_set_chip_and_handler(irq, &bfin_mac_status_irqchip,
1417 1418
					 handle_level_irq);
#endif
1419
	/* if configured as edge, then will be changed to do_edge_IRQ */
1420 1421
	for (irq = GPIO_IRQ_BASE;
		irq < (GPIO_IRQ_BASE + MAX_BLACKFIN_GPIOS); irq++)
T
Thomas Gleixner 已提交
1422
		irq_set_chip_and_handler(irq, &bfin_gpio_irqchip,
1423
					 handle_level_irq);
B
Bryan Wu 已提交
1424 1425 1426 1427 1428 1429 1430
	bfin_write_IMASK(0);
	CSYNC();
	ilat = bfin_read_ILAT();
	CSYNC();
	bfin_write_ILAT(ilat);
	CSYNC();

1431
	printk(KERN_INFO "Configuring Blackfin Priority Driven Interrupts\n");
1432
	/* IMASK=xxx is equivalent to STI xx or bfin_irq_flags=xx,
B
Bryan Wu 已提交
1433 1434 1435 1436 1437 1438 1439
	 * local_irq_enable()
	 */
	program_IAR();
	/* Therefore it's better to setup IARs before interrupts enabled */
	search_IAR();

	/* Enable interrupts IVG7-15 */
1440
	bfin_irq_flags |= IMASK_IVG15 |
1441 1442 1443 1444
		IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
		IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;

	bfin_sti(bfin_irq_flags);
B
Bryan Wu 已提交
1445

1446 1447 1448
	/* This implicitly covers ANOMALY_05000171
	 * Boot-ROM code modifies SICA_IWRx wakeup registers
	 */
1449
#ifdef SIC_IWR0
1450
	bfin_write_SIC_IWR0(IWR_DISABLE_ALL);
1451
# ifdef SIC_IWR1
1452
	/* BF52x/BF51x system reset does not properly reset SIC_IWR1 which
1453 1454 1455 1456
	 * will screw up the bootrom as it relies on MDMA0/1 waking it
	 * up from IDLE instructions.  See this report for more info:
	 * http://blackfin.uclinux.org/gf/tracker/4323
	 */
1457 1458 1459 1460
	if (ANOMALY_05000435)
		bfin_write_SIC_IWR1(IWR_ENABLE(10) | IWR_ENABLE(11));
	else
		bfin_write_SIC_IWR1(IWR_DISABLE_ALL);
1461 1462
# endif
# ifdef SIC_IWR2
1463
	bfin_write_SIC_IWR2(IWR_DISABLE_ALL);
1464 1465
# endif
#else
1466
	bfin_write_SIC_IWR(IWR_DISABLE_ALL);
1467
#endif
B
Bryan Wu 已提交
1468 1469 1470 1471
	return 0;
}

#ifdef CONFIG_DO_IRQ_L1
1472
__attribute__((l1_text))
B
Bryan Wu 已提交
1473
#endif
1474
static int vec_to_irq(int vec)
B
Bryan Wu 已提交
1475
{
1476 1477 1478 1479 1480 1481 1482 1483 1484
	struct ivgx *ivg = ivg7_13[vec - IVG7].ifirst;
	struct ivgx *ivg_stop = ivg7_13[vec - IVG7].istop;
	unsigned long sic_status[3];
	if (likely(vec == EVT_IVTMR_P))
		return IRQ_CORETMR;
#ifdef SIC_ISR
	sic_status[0] = bfin_read_SIC_IMASK() & bfin_read_SIC_ISR();
#else
	if (smp_processor_id()) {
1485
# ifdef SICB_ISR0
1486 1487 1488
		/* This will be optimized out in UP mode. */
		sic_status[0] = bfin_read_SICB_ISR0() & bfin_read_SICB_IMASK0();
		sic_status[1] = bfin_read_SICB_ISR1() & bfin_read_SICB_IMASK1();
1489
# endif
1490 1491 1492 1493 1494 1495 1496 1497
	} else {
		sic_status[0] = bfin_read_SIC_ISR0() & bfin_read_SIC_IMASK0();
		sic_status[1] = bfin_read_SIC_ISR1() & bfin_read_SIC_IMASK1();
	}
#endif
#ifdef SIC_ISR2
	sic_status[2] = bfin_read_SIC_ISR2() & bfin_read_SIC_IMASK2();
#endif
B
Bryan Wu 已提交
1498

1499 1500 1501 1502 1503 1504 1505
	for (;; ivg++) {
		if (ivg >= ivg_stop)
			return -1;
#ifdef SIC_ISR
		if (sic_status[0] & ivg->isrflag)
#else
		if (sic_status[(ivg->irqno - IVG7) / 32] & ivg->isrflag)
1506
#endif
1507
			return ivg->irqno;
B
Bryan Wu 已提交
1508
	}
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
}

#else /* SEC_GCTL */

/*
 * This function should be called during kernel startup to initialize
 * the BFin IRQ handling routines.
 */

int __init init_arch_irq(void)
{
	int irq;
	unsigned long ilat = 0;

	bfin_write_SEC_GCTL(SEC_GCTL_RESET);

	local_irq_disable();

#if BFIN_GPIO_PINT
# ifdef CONFIG_PINTx_REASSIGN
	pint[0]->assign = CONFIG_PINT0_ASSIGN;
	pint[1]->assign = CONFIG_PINT1_ASSIGN;
	pint[2]->assign = CONFIG_PINT2_ASSIGN;
	pint[3]->assign = CONFIG_PINT3_ASSIGN;
	pint[4]->assign = CONFIG_PINT4_ASSIGN;
	pint[5]->assign = CONFIG_PINT5_ASSIGN;
# endif
	/* Whenever PINTx_ASSIGN is altered init_pint_lut() must be executed! */
	init_pint_lut();
#endif

	for (irq = 0; irq <= SYS_IRQS; irq++) {
		if (irq <= IRQ_CORETMR) {
			irq_set_chip(irq, &bfin_core_irqchip);
#ifdef CONFIG_TICKSOURCE_CORETMR
			if (irq == IRQ_CORETMR)
# ifdef CONFIG_SMP
				irq_set_handler(irq, handle_percpu_irq);
# else
				irq_set_handler(irq, handle_simple_irq);
# endif
#endif
		} else if (irq < BFIN_IRQ(0)) {
			irq_set_chip_and_handler(irq, &bfin_internal_irqchip,
					handle_simple_irq);
1554
		} else if (irq == IRQ_SEC_ERR) {
1555 1556
			irq_set_chip_and_handler(irq, &bfin_sec_irqchip,
					handle_sec_fault);
1557 1558 1559
		} else if (irq < CORE_IRQS && irq >= IRQ_C0_DBL_FAULT) {
			irq_set_chip_and_handler(irq, &bfin_sec_irqchip,
					handle_core_fault);
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
		} else if (irq >= BFIN_IRQ(21) && irq <= BFIN_IRQ(26)) {
			irq_set_chip(irq, &bfin_sec_irqchip);
			irq_set_chained_handler(irq, bfin_demux_gpio_irq);
		} else if (irq >= BFIN_IRQ(34) && irq <= BFIN_IRQ(37)) {
			irq_set_chip(irq, &bfin_sec_irqchip);
			irq_set_handler(irq, handle_percpu_irq);
		} else {
			irq_set_chip_and_handler(irq, &bfin_sec_irqchip,
					handle_fasteoi_irq);
			__irq_set_preflow_handler(irq, bfin_sec_preflow_handler);
		}
	}
	for (irq = GPIO_IRQ_BASE;
		irq < (GPIO_IRQ_BASE + MAX_BLACKFIN_GPIOS); irq++)
		irq_set_chip_and_handler(irq, &bfin_gpio_irqchip,
					handle_level_irq);

	bfin_write_IMASK(0);
	CSYNC();
	ilat = bfin_read_ILAT();
	CSYNC();
	bfin_write_ILAT(ilat);
	CSYNC();

	printk(KERN_INFO "Configuring Blackfin Priority Driven Interrupts\n");

1586 1587 1588 1589
	bfin_sec_set_priority(CONFIG_SEC_IRQ_PRIORITY_LEVELS, sec_int_priority);

	bfin_sec_set_priority(CONFIG_SEC_IRQ_PRIORITY_LEVELS, sec_int_priority);

1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
	/* Enable interrupts IVG7-15 */
	bfin_irq_flags |= IMASK_IVG15 |
	    IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
	    IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;


	bfin_write_SEC_FCTL(SEC_FCTL_EN | SEC_FCTL_SYSRST_EN | SEC_FCTL_FLTIN_EN);
	bfin_sec_enable_sci(SIC_SYSIRQ(IRQ_WATCH0));
	bfin_sec_enable_ssi(SIC_SYSIRQ(IRQ_WATCH0));
	bfin_write_SEC_SCI(0, SEC_CCTL, SEC_CCTL_RESET);
	udelay(100);
	bfin_write_SEC_GCTL(SEC_GCTL_EN);
	bfin_write_SEC_SCI(0, SEC_CCTL, SEC_CCTL_EN | SEC_CCTL_NMI_EN);
	bfin_write_SEC_SCI(1, SEC_CCTL, SEC_CCTL_EN | SEC_CCTL_NMI_EN);

	init_software_driven_irq();
	register_syscore_ops(&sec_pm_syscore_ops);

	return 0;
}

#ifdef CONFIG_DO_IRQ_L1
__attribute__((l1_text))
#endif
static int vec_to_irq(int vec)
{
	if (likely(vec == EVT_IVTMR_P))
		return IRQ_CORETMR;

1619
	return BFIN_IRQ(bfin_read_SEC_SCI(0, SEC_CSID));
1620
}
1621
#endif  /* SEC_GCTL */
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631

#ifdef CONFIG_DO_IRQ_L1
__attribute__((l1_text))
#endif
void do_irq(int vec, struct pt_regs *fp)
{
	int irq = vec_to_irq(vec);
	if (irq == -1)
		return;
	asm_do_IRQ(irq, fp);
B
Bryan Wu 已提交
1632
}
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642

#ifdef CONFIG_IPIPE

int __ipipe_get_irq_priority(unsigned irq)
{
	int ient, prio;

	if (irq <= IRQ_CORETMR)
		return irq;

1643 1644 1645 1646
#ifdef SEC_GCTL
	if (irq >= BFIN_IRQ(0))
		return IVG11;
#else
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
	for (ient = 0; ient < NR_PERI_INTS; ient++) {
		struct ivgx *ivg = ivg_table + ient;
		if (ivg->irqno == irq) {
			for (prio = 0; prio <= IVG13-IVG7; prio++) {
				if (ivg7_13[prio].ifirst <= ivg &&
				    ivg7_13[prio].istop > ivg)
					return IVG7 + prio;
			}
		}
	}
1657
#endif
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667

	return IVG15;
}

/* Hw interrupts are disabled on entry (check SAVE_CONTEXT). */
#ifdef CONFIG_DO_IRQ_L1
__attribute__((l1_text))
#endif
asmlinkage int __ipipe_grab_irq(int vec, struct pt_regs *regs)
{
1668
	struct ipipe_percpu_domain_data *p = ipipe_root_cpudom_ptr();
1669
	struct ipipe_domain *this_domain = __ipipe_current_domain;
1670
	int irq, s = 0;
1671

1672 1673 1674
	irq = vec_to_irq(vec);
	if (irq == -1)
		return 0;
1675 1676

	if (irq == IRQ_SYSTMR) {
1677
#if !defined(CONFIG_GENERIC_CLOCKEVENTS) || defined(CONFIG_TICKSOURCE_GPTMR0)
1678
		bfin_write_TIMER_STATUS(1); /* Latch TIMIL0 */
1679
#endif
1680 1681 1682
		/* This is basically what we need from the register frame. */
		__raw_get_cpu_var(__ipipe_tick_regs).ipend = regs->ipend;
		__raw_get_cpu_var(__ipipe_tick_regs).pc = regs->pc;
1683
		if (this_domain != ipipe_root_domain)
1684
			__raw_get_cpu_var(__ipipe_tick_regs).ipend &= ~0x10;
1685 1686
		else
			__raw_get_cpu_var(__ipipe_tick_regs).ipend |= 0x10;
1687 1688
	}

1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
	/*
	 * We don't want Linux interrupt handlers to run at the
	 * current core priority level (i.e. < EVT15), since this
	 * might delay other interrupts handled by a high priority
	 * domain. Here is what we do instead:
	 *
	 * - we raise the SYNCDEFER bit to prevent
	 * __ipipe_handle_irq() to sync the pipeline for the root
	 * stage for the incoming interrupt. Upon return, that IRQ is
	 * pending in the interrupt log.
	 *
	 * - we raise the TIF_IRQ_SYNC bit for the current thread, so
	 * that _schedule_and_signal_from_int will eventually sync the
	 * pipeline from EVT15.
	 */
1704 1705 1706 1707
	if (this_domain == ipipe_root_domain) {
		s = __test_and_set_bit(IPIPE_SYNCDEFER_FLAG, &p->status);
		barrier();
	}
1708 1709 1710

	ipipe_trace_irq_entry(irq);
	__ipipe_handle_irq(irq, regs);
1711
	ipipe_trace_irq_exit(irq);
1712

1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
	if (user_mode(regs) &&
	    !ipipe_test_foreign_stack() &&
	    (current->ipipe_flags & PF_EVTRET) != 0) {
		/*
		 * Testing for user_regs() does NOT fully eliminate
		 * foreign stack contexts, because of the forged
		 * interrupt returns we do through
		 * __ipipe_call_irqtail. In that case, we might have
		 * preempted a foreign stack context in a high
		 * priority domain, with a single interrupt level now
		 * pending after the irqtail unwinding is done. In
		 * which case user_mode() is now true, and the event
		 * gets dispatched spuriously.
		 */
		current->ipipe_flags &= ~PF_EVTRET;
		__ipipe_dispatch_event(IPIPE_EVENT_RETURN, regs);
	}

1731 1732 1733 1734 1735 1736 1737
	if (this_domain == ipipe_root_domain) {
		set_thread_flag(TIF_IRQ_SYNC);
		if (!s) {
			__clear_bit(IPIPE_SYNCDEFER_FLAG, &p->status);
			return !test_bit(IPIPE_STALL_FLAG, &p->status);
		}
	}
1738

1739
	return 0;
1740 1741 1742
}

#endif /* CONFIG_IPIPE */