Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[pandora-kernel.git] / arch / blackfin / mach-common / ints-priority.c
1 /*
2  * Set up the interrupt priorities
3  *
4  * Copyright  2004-2009 Analog Devices Inc.
5  *                 2003 Bas Vermeulen <bas@buyways.nl>
6  *                 2002 Arcturus Networks Inc. MaTed <mated@sympatico.ca>
7  *            2000-2001 Lineo, Inc. D. Jefff Dionne <jeff@lineo.ca>
8  *                 1999 D. Jeff Dionne <jeff@uclinux.org>
9  *                 1996 Roman Zippel
10  *
11  * Licensed under the GPL-2
12  */
13
14 #include <linux/module.h>
15 #include <linux/kernel_stat.h>
16 #include <linux/seq_file.h>
17 #include <linux/irq.h>
18 #include <linux/sched.h>
19 #ifdef CONFIG_IPIPE
20 #include <linux/ipipe.h>
21 #endif
22 #ifdef CONFIG_KGDB
23 #include <linux/kgdb.h>
24 #endif
25 #include <asm/traps.h>
26 #include <asm/blackfin.h>
27 #include <asm/gpio.h>
28 #include <asm/irq_handler.h>
29 #include <asm/dpmc.h>
30 #include <asm/bfin5xx_spi.h>
31 #include <asm/bfin_sport.h>
32 #include <asm/bfin_can.h>
33
34 #define SIC_SYSIRQ(irq) (irq - (IRQ_CORETMR + 1))
35
36 #ifdef BF537_FAMILY
37 # define BF537_GENERIC_ERROR_INT_DEMUX
38 # define SPI_ERR_MASK   (BIT_STAT_TXCOL | BIT_STAT_RBSY | BIT_STAT_MODF | BIT_STAT_TXE) /* SPI_STAT */
39 # define SPORT_ERR_MASK (ROVF | RUVF | TOVF | TUVF)     /* SPORT_STAT */
40 # define PPI_ERR_MASK   (0xFFFF & ~FLD) /* PPI_STATUS */
41 # define EMAC_ERR_MASK  (PHYINT | MMCINT | RXFSINT | TXFSINT | WAKEDET | RXDMAERR | TXDMAERR | STMDONE) /* EMAC_SYSTAT */
42 # define UART_ERR_MASK  (0x6)   /* UART_IIR */
43 # define CAN_ERR_MASK   (EWTIF | EWRIF | EPIF | BOIF | WUIF | UIAIF | AAIF | RMLIF | UCEIF | EXTIF | ADIF)      /* CAN_GIF */
44 #else
45 # undef BF537_GENERIC_ERROR_INT_DEMUX
46 #endif
47
48 /*
49  * NOTES:
50  * - we have separated the physical Hardware interrupt from the
51  * levels that the LINUX kernel sees (see the description in irq.h)
52  * -
53  */
54
55 #ifndef CONFIG_SMP
56 /* Initialize this to an actual value to force it into the .data
57  * section so that we know it is properly initialized at entry into
58  * the kernel but before bss is initialized to zero (which is where
59  * it would live otherwise).  The 0x1f magic represents the IRQs we
60  * cannot actually mask out in hardware.
61  */
62 unsigned long bfin_irq_flags = 0x1f;
63 EXPORT_SYMBOL(bfin_irq_flags);
64 #endif
65
66 /* The number of spurious interrupts */
67 atomic_t num_spurious;
68
69 #ifdef CONFIG_PM
70 unsigned long bfin_sic_iwr[3];  /* Up to 3 SIC_IWRx registers */
71 unsigned vr_wakeup;
72 #endif
73
74 struct ivgx {
75         /* irq number for request_irq, available in mach-bf5xx/irq.h */
76         unsigned int irqno;
77         /* corresponding bit in the SIC_ISR register */
78         unsigned int isrflag;
79 } ivg_table[NR_PERI_INTS];
80
81 struct ivg_slice {
82         /* position of first irq in ivg_table for given ivg */
83         struct ivgx *ifirst;
84         struct ivgx *istop;
85 } ivg7_13[IVG13 - IVG7 + 1];
86
87
88 /*
89  * Search SIC_IAR and fill tables with the irqvalues
90  * and their positions in the SIC_ISR register.
91  */
92 static void __init search_IAR(void)
93 {
94         unsigned ivg, irq_pos = 0;
95         for (ivg = 0; ivg <= IVG13 - IVG7; ivg++) {
96                 int irqN;
97
98                 ivg7_13[ivg].istop = ivg7_13[ivg].ifirst = &ivg_table[irq_pos];
99
100                 for (irqN = 0; irqN < NR_PERI_INTS; irqN += 4) {
101                         int irqn;
102                         u32 iar = bfin_read32((unsigned long *)SIC_IAR0 +
103 #if defined(CONFIG_BF51x) || defined(CONFIG_BF52x) || \
104         defined(CONFIG_BF538) || defined(CONFIG_BF539)
105                                 ((irqN % 32) >> 3) + ((irqN / 32) * ((SIC_IAR4 - SIC_IAR0) / 4))
106 #else
107                                 (irqN >> 3)
108 #endif
109                                 );
110
111                         for (irqn = irqN; irqn < irqN + 4; ++irqn) {
112                                 int iar_shift = (irqn & 7) * 4;
113                                 if (ivg == (0xf & (iar >> iar_shift))) {
114                                         ivg_table[irq_pos].irqno = IVG7 + irqn;
115                                         ivg_table[irq_pos].isrflag = 1 << (irqn % 32);
116                                         ivg7_13[ivg].istop++;
117                                         irq_pos++;
118                                 }
119                         }
120                 }
121         }
122 }
123
124 /*
125  * This is for core internal IRQs
126  */
127
128 static void bfin_ack_noop(struct irq_data *d)
129 {
130         /* Dummy function.  */
131 }
132
133 static void bfin_core_mask_irq(struct irq_data *d)
134 {
135         bfin_irq_flags &= ~(1 << d->irq);
136         if (!hard_irqs_disabled())
137                 hard_local_irq_enable();
138 }
139
140 static void bfin_core_unmask_irq(struct irq_data *d)
141 {
142         bfin_irq_flags |= 1 << d->irq;
143         /*
144          * If interrupts are enabled, IMASK must contain the same value
145          * as bfin_irq_flags.  Make sure that invariant holds.  If interrupts
146          * are currently disabled we need not do anything; one of the
147          * callers will take care of setting IMASK to the proper value
148          * when reenabling interrupts.
149          * local_irq_enable just does "STI bfin_irq_flags", so it's exactly
150          * what we need.
151          */
152         if (!hard_irqs_disabled())
153                 hard_local_irq_enable();
154         return;
155 }
156
157 static void bfin_internal_mask_irq(unsigned int irq)
158 {
159         unsigned long flags;
160
161 #ifdef CONFIG_BF53x
162         flags = hard_local_irq_save();
163         bfin_write_SIC_IMASK(bfin_read_SIC_IMASK() &
164                              ~(1 << SIC_SYSIRQ(irq)));
165 #else
166         unsigned mask_bank, mask_bit;
167         flags = hard_local_irq_save();
168         mask_bank = SIC_SYSIRQ(irq) / 32;
169         mask_bit = SIC_SYSIRQ(irq) % 32;
170         bfin_write_SIC_IMASK(mask_bank, bfin_read_SIC_IMASK(mask_bank) &
171                              ~(1 << mask_bit));
172 #ifdef CONFIG_SMP
173         bfin_write_SICB_IMASK(mask_bank, bfin_read_SICB_IMASK(mask_bank) &
174                              ~(1 << mask_bit));
175 #endif
176 #endif
177         hard_local_irq_restore(flags);
178 }
179
180 static void bfin_internal_mask_irq_chip(struct irq_data *d)
181 {
182         bfin_internal_mask_irq(d->irq);
183 }
184
185 #ifdef CONFIG_SMP
186 static void bfin_internal_unmask_irq_affinity(unsigned int irq,
187                 const struct cpumask *affinity)
188 #else
189 static void bfin_internal_unmask_irq(unsigned int irq)
190 #endif
191 {
192         unsigned long flags;
193
194 #ifdef CONFIG_BF53x
195         flags = hard_local_irq_save();
196         bfin_write_SIC_IMASK(bfin_read_SIC_IMASK() |
197                              (1 << SIC_SYSIRQ(irq)));
198 #else
199         unsigned mask_bank, mask_bit;
200         flags = hard_local_irq_save();
201         mask_bank = SIC_SYSIRQ(irq) / 32;
202         mask_bit = SIC_SYSIRQ(irq) % 32;
203 #ifdef CONFIG_SMP
204         if (cpumask_test_cpu(0, affinity))
205 #endif
206                 bfin_write_SIC_IMASK(mask_bank,
207                         bfin_read_SIC_IMASK(mask_bank) |
208                         (1 << mask_bit));
209 #ifdef CONFIG_SMP
210         if (cpumask_test_cpu(1, affinity))
211                 bfin_write_SICB_IMASK(mask_bank,
212                         bfin_read_SICB_IMASK(mask_bank) |
213                         (1 << mask_bit));
214 #endif
215 #endif
216         hard_local_irq_restore(flags);
217 }
218
219 #ifdef CONFIG_SMP
220 static void bfin_internal_unmask_irq_chip(struct irq_data *d)
221 {
222         bfin_internal_unmask_irq_affinity(d->irq, d->affinity);
223 }
224
225 static int bfin_internal_set_affinity(struct irq_data *d,
226                                       const struct cpumask *mask, bool force)
227 {
228         bfin_internal_mask_irq(d->irq);
229         bfin_internal_unmask_irq_affinity(d->irq, mask);
230
231         return 0;
232 }
233 #else
234 static void bfin_internal_unmask_irq_chip(struct irq_data *d)
235 {
236         bfin_internal_unmask_irq(d->irq);
237 }
238 #endif
239
240 #ifdef CONFIG_PM
241 int bfin_internal_set_wake(unsigned int irq, unsigned int state)
242 {
243         u32 bank, bit, wakeup = 0;
244         unsigned long flags;
245         bank = SIC_SYSIRQ(irq) / 32;
246         bit = SIC_SYSIRQ(irq) % 32;
247
248         switch (irq) {
249 #ifdef IRQ_RTC
250         case IRQ_RTC:
251         wakeup |= WAKE;
252         break;
253 #endif
254 #ifdef IRQ_CAN0_RX
255         case IRQ_CAN0_RX:
256         wakeup |= CANWE;
257         break;
258 #endif
259 #ifdef IRQ_CAN1_RX
260         case IRQ_CAN1_RX:
261         wakeup |= CANWE;
262         break;
263 #endif
264 #ifdef IRQ_USB_INT0
265         case IRQ_USB_INT0:
266         wakeup |= USBWE;
267         break;
268 #endif
269 #ifdef CONFIG_BF54x
270         case IRQ_CNT:
271         wakeup |= ROTWE;
272         break;
273 #endif
274         default:
275         break;
276         }
277
278         flags = hard_local_irq_save();
279
280         if (state) {
281                 bfin_sic_iwr[bank] |= (1 << bit);
282                 vr_wakeup  |= wakeup;
283
284         } else {
285                 bfin_sic_iwr[bank] &= ~(1 << bit);
286                 vr_wakeup  &= ~wakeup;
287         }
288
289         hard_local_irq_restore(flags);
290
291         return 0;
292 }
293
294 static int bfin_internal_set_wake_chip(struct irq_data *d, unsigned int state)
295 {
296         return bfin_internal_set_wake(d->irq, state);
297 }
298 #endif
299
300 static struct irq_chip bfin_core_irqchip = {
301         .name = "CORE",
302         .irq_ack = bfin_ack_noop,
303         .irq_mask = bfin_core_mask_irq,
304         .irq_unmask = bfin_core_unmask_irq,
305 };
306
307 static struct irq_chip bfin_internal_irqchip = {
308         .name = "INTN",
309         .irq_ack = bfin_ack_noop,
310         .irq_mask = bfin_internal_mask_irq_chip,
311         .irq_unmask = bfin_internal_unmask_irq_chip,
312         .irq_mask_ack = bfin_internal_mask_irq_chip,
313         .irq_disable = bfin_internal_mask_irq_chip,
314         .irq_enable = bfin_internal_unmask_irq_chip,
315 #ifdef CONFIG_SMP
316         .irq_set_affinity = bfin_internal_set_affinity,
317 #endif
318 #ifdef CONFIG_PM
319         .irq_set_wake = bfin_internal_set_wake_chip,
320 #endif
321 };
322
323 static void bfin_handle_irq(unsigned irq)
324 {
325 #ifdef CONFIG_IPIPE
326         struct pt_regs regs;    /* Contents not used. */
327         ipipe_trace_irq_entry(irq);
328         __ipipe_handle_irq(irq, &regs);
329         ipipe_trace_irq_exit(irq);
330 #else /* !CONFIG_IPIPE */
331         generic_handle_irq(irq);
332 #endif  /* !CONFIG_IPIPE */
333 }
334
335 #ifdef BF537_GENERIC_ERROR_INT_DEMUX
336 static int error_int_mask;
337
338 static void bfin_generic_error_mask_irq(struct irq_data *d)
339 {
340         error_int_mask &= ~(1L << (d->irq - IRQ_PPI_ERROR));
341         if (!error_int_mask)
342                 bfin_internal_mask_irq(IRQ_GENERIC_ERROR);
343 }
344
345 static void bfin_generic_error_unmask_irq(struct irq_data *d)
346 {
347         bfin_internal_unmask_irq(IRQ_GENERIC_ERROR);
348         error_int_mask |= 1L << (d->irq - IRQ_PPI_ERROR);
349 }
350
351 static struct irq_chip bfin_generic_error_irqchip = {
352         .name = "ERROR",
353         .irq_ack = bfin_ack_noop,
354         .irq_mask_ack = bfin_generic_error_mask_irq,
355         .irq_mask = bfin_generic_error_mask_irq,
356         .irq_unmask = bfin_generic_error_unmask_irq,
357 };
358
359 static void bfin_demux_error_irq(unsigned int int_err_irq,
360                                  struct irq_desc *inta_desc)
361 {
362         int irq = 0;
363
364 #if (defined(CONFIG_BF537) || defined(CONFIG_BF536))
365         if (bfin_read_EMAC_SYSTAT() & EMAC_ERR_MASK)
366                 irq = IRQ_MAC_ERROR;
367         else
368 #endif
369         if (bfin_read_SPORT0_STAT() & SPORT_ERR_MASK)
370                 irq = IRQ_SPORT0_ERROR;
371         else if (bfin_read_SPORT1_STAT() & SPORT_ERR_MASK)
372                 irq = IRQ_SPORT1_ERROR;
373         else if (bfin_read_PPI_STATUS() & PPI_ERR_MASK)
374                 irq = IRQ_PPI_ERROR;
375         else if (bfin_read_CAN_GIF() & CAN_ERR_MASK)
376                 irq = IRQ_CAN_ERROR;
377         else if (bfin_read_SPI_STAT() & SPI_ERR_MASK)
378                 irq = IRQ_SPI_ERROR;
379         else if ((bfin_read_UART0_IIR() & UART_ERR_MASK) == UART_ERR_MASK)
380                 irq = IRQ_UART0_ERROR;
381         else if ((bfin_read_UART1_IIR() & UART_ERR_MASK) == UART_ERR_MASK)
382                 irq = IRQ_UART1_ERROR;
383
384         if (irq) {
385                 if (error_int_mask & (1L << (irq - IRQ_PPI_ERROR)))
386                         bfin_handle_irq(irq);
387                 else {
388
389                         switch (irq) {
390                         case IRQ_PPI_ERROR:
391                                 bfin_write_PPI_STATUS(PPI_ERR_MASK);
392                                 break;
393 #if (defined(CONFIG_BF537) || defined(CONFIG_BF536))
394                         case IRQ_MAC_ERROR:
395                                 bfin_write_EMAC_SYSTAT(EMAC_ERR_MASK);
396                                 break;
397 #endif
398                         case IRQ_SPORT0_ERROR:
399                                 bfin_write_SPORT0_STAT(SPORT_ERR_MASK);
400                                 break;
401
402                         case IRQ_SPORT1_ERROR:
403                                 bfin_write_SPORT1_STAT(SPORT_ERR_MASK);
404                                 break;
405
406                         case IRQ_CAN_ERROR:
407                                 bfin_write_CAN_GIS(CAN_ERR_MASK);
408                                 break;
409
410                         case IRQ_SPI_ERROR:
411                                 bfin_write_SPI_STAT(SPI_ERR_MASK);
412                                 break;
413
414                         default:
415                                 break;
416                         }
417
418                         pr_debug("IRQ %d:"
419                                  " MASKED PERIPHERAL ERROR INTERRUPT ASSERTED\n",
420                                  irq);
421                 }
422         } else
423                 printk(KERN_ERR
424                        "%s : %s : LINE %d :\nIRQ ?: PERIPHERAL ERROR"
425                        " INTERRUPT ASSERTED BUT NO SOURCE FOUND\n",
426                        __func__, __FILE__, __LINE__);
427
428 }
429 #endif                          /* BF537_GENERIC_ERROR_INT_DEMUX */
430
431 #if defined(CONFIG_BFIN_MAC) || defined(CONFIG_BFIN_MAC_MODULE)
432 static int mac_stat_int_mask;
433
434 static void bfin_mac_status_ack_irq(unsigned int irq)
435 {
436         switch (irq) {
437         case IRQ_MAC_MMCINT:
438                 bfin_write_EMAC_MMC_TIRQS(
439                         bfin_read_EMAC_MMC_TIRQE() &
440                         bfin_read_EMAC_MMC_TIRQS());
441                 bfin_write_EMAC_MMC_RIRQS(
442                         bfin_read_EMAC_MMC_RIRQE() &
443                         bfin_read_EMAC_MMC_RIRQS());
444                 break;
445         case IRQ_MAC_RXFSINT:
446                 bfin_write_EMAC_RX_STKY(
447                         bfin_read_EMAC_RX_IRQE() &
448                         bfin_read_EMAC_RX_STKY());
449                 break;
450         case IRQ_MAC_TXFSINT:
451                 bfin_write_EMAC_TX_STKY(
452                         bfin_read_EMAC_TX_IRQE() &
453                         bfin_read_EMAC_TX_STKY());
454                 break;
455         case IRQ_MAC_WAKEDET:
456                  bfin_write_EMAC_WKUP_CTL(
457                         bfin_read_EMAC_WKUP_CTL() | MPKS | RWKS);
458                 break;
459         default:
460                 /* These bits are W1C */
461                 bfin_write_EMAC_SYSTAT(1L << (irq - IRQ_MAC_PHYINT));
462                 break;
463         }
464 }
465
466 static void bfin_mac_status_mask_irq(struct irq_data *d)
467 {
468         unsigned int irq = d->irq;
469
470         mac_stat_int_mask &= ~(1L << (irq - IRQ_MAC_PHYINT));
471 #ifdef BF537_GENERIC_ERROR_INT_DEMUX
472         switch (irq) {
473         case IRQ_MAC_PHYINT:
474                 bfin_write_EMAC_SYSCTL(bfin_read_EMAC_SYSCTL() & ~PHYIE);
475                 break;
476         default:
477                 break;
478         }
479 #else
480         if (!mac_stat_int_mask)
481                 bfin_internal_mask_irq(IRQ_MAC_ERROR);
482 #endif
483         bfin_mac_status_ack_irq(irq);
484 }
485
486 static void bfin_mac_status_unmask_irq(struct irq_data *d)
487 {
488         unsigned int irq = d->irq;
489
490 #ifdef BF537_GENERIC_ERROR_INT_DEMUX
491         switch (irq) {
492         case IRQ_MAC_PHYINT:
493                 bfin_write_EMAC_SYSCTL(bfin_read_EMAC_SYSCTL() | PHYIE);
494                 break;
495         default:
496                 break;
497         }
498 #else
499         if (!mac_stat_int_mask)
500                 bfin_internal_unmask_irq(IRQ_MAC_ERROR);
501 #endif
502         mac_stat_int_mask |= 1L << (irq - IRQ_MAC_PHYINT);
503 }
504
505 #ifdef CONFIG_PM
506 int bfin_mac_status_set_wake(struct irq_data *d, unsigned int state)
507 {
508 #ifdef BF537_GENERIC_ERROR_INT_DEMUX
509         return bfin_internal_set_wake(IRQ_GENERIC_ERROR, state);
510 #else
511         return bfin_internal_set_wake(IRQ_MAC_ERROR, state);
512 #endif
513 }
514 #endif
515
516 static struct irq_chip bfin_mac_status_irqchip = {
517         .name = "MACST",
518         .irq_ack = bfin_ack_noop,
519         .irq_mask_ack = bfin_mac_status_mask_irq,
520         .irq_mask = bfin_mac_status_mask_irq,
521         .irq_unmask = bfin_mac_status_unmask_irq,
522 #ifdef CONFIG_PM
523         .irq_set_wake = bfin_mac_status_set_wake,
524 #endif
525 };
526
527 static void bfin_demux_mac_status_irq(unsigned int int_err_irq,
528                                  struct irq_desc *inta_desc)
529 {
530         int i, irq = 0;
531         u32 status = bfin_read_EMAC_SYSTAT();
532
533         for (i = 0; i <= (IRQ_MAC_STMDONE - IRQ_MAC_PHYINT); i++)
534                 if (status & (1L << i)) {
535                         irq = IRQ_MAC_PHYINT + i;
536                         break;
537                 }
538
539         if (irq) {
540                 if (mac_stat_int_mask & (1L << (irq - IRQ_MAC_PHYINT))) {
541                         bfin_handle_irq(irq);
542                 } else {
543                         bfin_mac_status_ack_irq(irq);
544                         pr_debug("IRQ %d:"
545                                  " MASKED MAC ERROR INTERRUPT ASSERTED\n",
546                                  irq);
547                 }
548         } else
549                 printk(KERN_ERR
550                        "%s : %s : LINE %d :\nIRQ ?: MAC ERROR"
551                        " INTERRUPT ASSERTED BUT NO SOURCE FOUND"
552                        "(EMAC_SYSTAT=0x%X)\n",
553                        __func__, __FILE__, __LINE__, status);
554 }
555 #endif
556
557 static inline void bfin_set_irq_handler(unsigned irq, irq_flow_handler_t handle)
558 {
559 #ifdef CONFIG_IPIPE
560         handle = handle_level_irq;
561 #endif
562         __set_irq_handler_unlocked(irq, handle);
563 }
564
565 static DECLARE_BITMAP(gpio_enabled, MAX_BLACKFIN_GPIOS);
566 extern void bfin_gpio_irq_prepare(unsigned gpio);
567
568 #if !defined(CONFIG_BF54x)
569
570 static void bfin_gpio_ack_irq(struct irq_data *d)
571 {
572         /* AFAIK ack_irq in case mask_ack is provided
573          * get's only called for edge sense irqs
574          */
575         set_gpio_data(irq_to_gpio(d->irq), 0);
576 }
577
578 static void bfin_gpio_mask_ack_irq(struct irq_data *d)
579 {
580         unsigned int irq = d->irq;
581         struct irq_desc *desc = irq_to_desc(irq);
582         u32 gpionr = irq_to_gpio(irq);
583
584         if (desc->handle_irq == handle_edge_irq)
585                 set_gpio_data(gpionr, 0);
586
587         set_gpio_maska(gpionr, 0);
588 }
589
590 static void bfin_gpio_mask_irq(struct irq_data *d)
591 {
592         set_gpio_maska(irq_to_gpio(d->irq), 0);
593 }
594
595 static void bfin_gpio_unmask_irq(struct irq_data *d)
596 {
597         set_gpio_maska(irq_to_gpio(d->irq), 1);
598 }
599
600 static unsigned int bfin_gpio_irq_startup(struct irq_data *d)
601 {
602         u32 gpionr = irq_to_gpio(d->irq);
603
604         if (__test_and_set_bit(gpionr, gpio_enabled))
605                 bfin_gpio_irq_prepare(gpionr);
606
607         bfin_gpio_unmask_irq(d);
608
609         return 0;
610 }
611
612 static void bfin_gpio_irq_shutdown(struct irq_data *d)
613 {
614         u32 gpionr = irq_to_gpio(d->irq);
615
616         bfin_gpio_mask_irq(d);
617         __clear_bit(gpionr, gpio_enabled);
618         bfin_gpio_irq_free(gpionr);
619 }
620
621 static int bfin_gpio_irq_type(struct irq_data *d, unsigned int type)
622 {
623         unsigned int irq = d->irq;
624         int ret;
625         char buf[16];
626         u32 gpionr = irq_to_gpio(irq);
627
628         if (type == IRQ_TYPE_PROBE) {
629                 /* only probe unenabled GPIO interrupt lines */
630                 if (test_bit(gpionr, gpio_enabled))
631                         return 0;
632                 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
633         }
634
635         if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
636                     IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
637
638                 snprintf(buf, 16, "gpio-irq%d", irq);
639                 ret = bfin_gpio_irq_request(gpionr, buf);
640                 if (ret)
641                         return ret;
642
643                 if (__test_and_set_bit(gpionr, gpio_enabled))
644                         bfin_gpio_irq_prepare(gpionr);
645
646         } else {
647                 __clear_bit(gpionr, gpio_enabled);
648                 return 0;
649         }
650
651         set_gpio_inen(gpionr, 0);
652         set_gpio_dir(gpionr, 0);
653
654         if ((type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
655             == (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
656                 set_gpio_both(gpionr, 1);
657         else
658                 set_gpio_both(gpionr, 0);
659
660         if ((type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW)))
661                 set_gpio_polar(gpionr, 1);      /* low or falling edge denoted by one */
662         else
663                 set_gpio_polar(gpionr, 0);      /* high or rising edge denoted by zero */
664
665         if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
666                 set_gpio_edge(gpionr, 1);
667                 set_gpio_inen(gpionr, 1);
668                 set_gpio_data(gpionr, 0);
669
670         } else {
671                 set_gpio_edge(gpionr, 0);
672                 set_gpio_inen(gpionr, 1);
673         }
674
675         if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
676                 bfin_set_irq_handler(irq, handle_edge_irq);
677         else
678                 bfin_set_irq_handler(irq, handle_level_irq);
679
680         return 0;
681 }
682
683 #ifdef CONFIG_PM
684 int bfin_gpio_set_wake(struct irq_data *d, unsigned int state)
685 {
686         return gpio_pm_wakeup_ctrl(irq_to_gpio(d->irq), state);
687 }
688 #endif
689
690 static void bfin_demux_gpio_irq(unsigned int inta_irq,
691                                 struct irq_desc *desc)
692 {
693         unsigned int i, gpio, mask, irq, search = 0;
694
695         switch (inta_irq) {
696 #if defined(CONFIG_BF53x)
697         case IRQ_PROG_INTA:
698                 irq = IRQ_PF0;
699                 search = 1;
700                 break;
701 # if defined(BF537_FAMILY) && !(defined(CONFIG_BFIN_MAC) || defined(CONFIG_BFIN_MAC_MODULE))
702         case IRQ_MAC_RX:
703                 irq = IRQ_PH0;
704                 break;
705 # endif
706 #elif defined(CONFIG_BF538) || defined(CONFIG_BF539)
707         case IRQ_PORTF_INTA:
708                 irq = IRQ_PF0;
709                 break;
710 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
711         case IRQ_PORTF_INTA:
712                 irq = IRQ_PF0;
713                 break;
714         case IRQ_PORTG_INTA:
715                 irq = IRQ_PG0;
716                 break;
717         case IRQ_PORTH_INTA:
718                 irq = IRQ_PH0;
719                 break;
720 #elif defined(CONFIG_BF561)
721         case IRQ_PROG0_INTA:
722                 irq = IRQ_PF0;
723                 break;
724         case IRQ_PROG1_INTA:
725                 irq = IRQ_PF16;
726                 break;
727         case IRQ_PROG2_INTA:
728                 irq = IRQ_PF32;
729                 break;
730 #endif
731         default:
732                 BUG();
733                 return;
734         }
735
736         if (search) {
737                 for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
738                         irq += i;
739
740                         mask = get_gpiop_data(i) & get_gpiop_maska(i);
741
742                         while (mask) {
743                                 if (mask & 1)
744                                         bfin_handle_irq(irq);
745                                 irq++;
746                                 mask >>= 1;
747                         }
748                 }
749         } else {
750                         gpio = irq_to_gpio(irq);
751                         mask = get_gpiop_data(gpio) & get_gpiop_maska(gpio);
752
753                         do {
754                                 if (mask & 1)
755                                         bfin_handle_irq(irq);
756                                 irq++;
757                                 mask >>= 1;
758                         } while (mask);
759         }
760
761 }
762
763 #else                           /* CONFIG_BF54x */
764
765 #define NR_PINT_SYS_IRQS        4
766 #define NR_PINT_BITS            32
767 #define NR_PINTS                160
768 #define IRQ_NOT_AVAIL           0xFF
769
770 #define PINT_2_BANK(x)          ((x) >> 5)
771 #define PINT_2_BIT(x)           ((x) & 0x1F)
772 #define PINT_BIT(x)             (1 << (PINT_2_BIT(x)))
773
774 static unsigned char irq2pint_lut[NR_PINTS];
775 static unsigned char pint2irq_lut[NR_PINT_SYS_IRQS * NR_PINT_BITS];
776
777 struct pin_int_t {
778         unsigned int mask_set;
779         unsigned int mask_clear;
780         unsigned int request;
781         unsigned int assign;
782         unsigned int edge_set;
783         unsigned int edge_clear;
784         unsigned int invert_set;
785         unsigned int invert_clear;
786         unsigned int pinstate;
787         unsigned int latch;
788 };
789
790 static struct pin_int_t *pint[NR_PINT_SYS_IRQS] = {
791         (struct pin_int_t *)PINT0_MASK_SET,
792         (struct pin_int_t *)PINT1_MASK_SET,
793         (struct pin_int_t *)PINT2_MASK_SET,
794         (struct pin_int_t *)PINT3_MASK_SET,
795 };
796
797 inline unsigned int get_irq_base(u32 bank, u8 bmap)
798 {
799         unsigned int irq_base;
800
801         if (bank < 2) {         /*PA-PB */
802                 irq_base = IRQ_PA0 + bmap * 16;
803         } else {                /*PC-PJ */
804                 irq_base = IRQ_PC0 + bmap * 16;
805         }
806
807         return irq_base;
808 }
809
810         /* Whenever PINTx_ASSIGN is altered init_pint_lut() must be executed! */
811 void init_pint_lut(void)
812 {
813         u16 bank, bit, irq_base, bit_pos;
814         u32 pint_assign;
815         u8 bmap;
816
817         memset(irq2pint_lut, IRQ_NOT_AVAIL, sizeof(irq2pint_lut));
818
819         for (bank = 0; bank < NR_PINT_SYS_IRQS; bank++) {
820
821                 pint_assign = pint[bank]->assign;
822
823                 for (bit = 0; bit < NR_PINT_BITS; bit++) {
824
825                         bmap = (pint_assign >> ((bit / 8) * 8)) & 0xFF;
826
827                         irq_base = get_irq_base(bank, bmap);
828
829                         irq_base += (bit % 8) + ((bit / 8) & 1 ? 8 : 0);
830                         bit_pos = bit + bank * NR_PINT_BITS;
831
832                         pint2irq_lut[bit_pos] = irq_base - SYS_IRQS;
833                         irq2pint_lut[irq_base - SYS_IRQS] = bit_pos;
834                 }
835         }
836 }
837
838 static void bfin_gpio_ack_irq(struct irq_data *d)
839 {
840         struct irq_desc *desc = irq_to_desc(d->irq);
841         u32 pint_val = irq2pint_lut[d->irq - SYS_IRQS];
842         u32 pintbit = PINT_BIT(pint_val);
843         u32 bank = PINT_2_BANK(pint_val);
844
845         if ((desc->status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
846                 if (pint[bank]->invert_set & pintbit)
847                         pint[bank]->invert_clear = pintbit;
848                 else
849                         pint[bank]->invert_set = pintbit;
850         }
851         pint[bank]->request = pintbit;
852
853 }
854
855 static void bfin_gpio_mask_ack_irq(struct irq_data *d)
856 {
857         struct irq_desc *desc = irq_to_desc(d->irq);
858         u32 pint_val = irq2pint_lut[d->irq - SYS_IRQS];
859         u32 pintbit = PINT_BIT(pint_val);
860         u32 bank = PINT_2_BANK(pint_val);
861
862         if ((desc->status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
863                 if (pint[bank]->invert_set & pintbit)
864                         pint[bank]->invert_clear = pintbit;
865                 else
866                         pint[bank]->invert_set = pintbit;
867         }
868
869         pint[bank]->request = pintbit;
870         pint[bank]->mask_clear = pintbit;
871 }
872
873 static void bfin_gpio_mask_irq(struct irq_data *d)
874 {
875         u32 pint_val = irq2pint_lut[d->irq - SYS_IRQS];
876
877         pint[PINT_2_BANK(pint_val)]->mask_clear = PINT_BIT(pint_val);
878 }
879
880 static void bfin_gpio_unmask_irq(struct irq_data *d)
881 {
882         u32 pint_val = irq2pint_lut[d->irq - SYS_IRQS];
883         u32 pintbit = PINT_BIT(pint_val);
884         u32 bank = PINT_2_BANK(pint_val);
885
886         pint[bank]->mask_set = pintbit;
887 }
888
889 static unsigned int bfin_gpio_irq_startup(struct irq_data *d)
890 {
891         unsigned int irq = d->irq;
892         u32 gpionr = irq_to_gpio(irq);
893         u32 pint_val = irq2pint_lut[irq - SYS_IRQS];
894
895         if (pint_val == IRQ_NOT_AVAIL) {
896                 printk(KERN_ERR
897                 "GPIO IRQ %d :Not in PINT Assign table "
898                 "Reconfigure Interrupt to Port Assignemt\n", irq);
899                 return -ENODEV;
900         }
901
902         if (__test_and_set_bit(gpionr, gpio_enabled))
903                 bfin_gpio_irq_prepare(gpionr);
904
905         bfin_gpio_unmask_irq(d);
906
907         return 0;
908 }
909
910 static void bfin_gpio_irq_shutdown(struct irq_data *d)
911 {
912         u32 gpionr = irq_to_gpio(d->irq);
913
914         bfin_gpio_mask_irq(d);
915         __clear_bit(gpionr, gpio_enabled);
916         bfin_gpio_irq_free(gpionr);
917 }
918
919 static int bfin_gpio_irq_type(struct irq_data *d, unsigned int type)
920 {
921         unsigned int irq = d->irq;
922         int ret;
923         char buf[16];
924         u32 gpionr = irq_to_gpio(irq);
925         u32 pint_val = irq2pint_lut[irq - SYS_IRQS];
926         u32 pintbit = PINT_BIT(pint_val);
927         u32 bank = PINT_2_BANK(pint_val);
928
929         if (pint_val == IRQ_NOT_AVAIL)
930                 return -ENODEV;
931
932         if (type == IRQ_TYPE_PROBE) {
933                 /* only probe unenabled GPIO interrupt lines */
934                 if (test_bit(gpionr, gpio_enabled))
935                         return 0;
936                 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
937         }
938
939         if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
940                     IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
941
942                 snprintf(buf, 16, "gpio-irq%d", irq);
943                 ret = bfin_gpio_irq_request(gpionr, buf);
944                 if (ret)
945                         return ret;
946
947                 if (__test_and_set_bit(gpionr, gpio_enabled))
948                         bfin_gpio_irq_prepare(gpionr);
949
950         } else {
951                 __clear_bit(gpionr, gpio_enabled);
952                 return 0;
953         }
954
955         if ((type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW)))
956                 pint[bank]->invert_set = pintbit;       /* low or falling edge denoted by one */
957         else
958                 pint[bank]->invert_clear = pintbit;     /* high or rising edge denoted by zero */
959
960         if ((type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING))
961             == (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
962                 if (gpio_get_value(gpionr))
963                         pint[bank]->invert_set = pintbit;
964                 else
965                         pint[bank]->invert_clear = pintbit;
966         }
967
968         if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
969                 pint[bank]->edge_set = pintbit;
970                 bfin_set_irq_handler(irq, handle_edge_irq);
971         } else {
972                 pint[bank]->edge_clear = pintbit;
973                 bfin_set_irq_handler(irq, handle_level_irq);
974         }
975
976         return 0;
977 }
978
979 #ifdef CONFIG_PM
980 u32 pint_saved_masks[NR_PINT_SYS_IRQS];
981 u32 pint_wakeup_masks[NR_PINT_SYS_IRQS];
982
983 int bfin_gpio_set_wake(struct irq_data *d, unsigned int state)
984 {
985         u32 pint_irq;
986         u32 pint_val = irq2pint_lut[d->irq - SYS_IRQS];
987         u32 bank = PINT_2_BANK(pint_val);
988         u32 pintbit = PINT_BIT(pint_val);
989
990         switch (bank) {
991         case 0:
992                 pint_irq = IRQ_PINT0;
993                 break;
994         case 2:
995                 pint_irq = IRQ_PINT2;
996                 break;
997         case 3:
998                 pint_irq = IRQ_PINT3;
999                 break;
1000         case 1:
1001                 pint_irq = IRQ_PINT1;
1002                 break;
1003         default:
1004                 return -EINVAL;
1005         }
1006
1007         bfin_internal_set_wake(pint_irq, state);
1008
1009         if (state)
1010                 pint_wakeup_masks[bank] |= pintbit;
1011         else
1012                 pint_wakeup_masks[bank] &= ~pintbit;
1013
1014         return 0;
1015 }
1016
1017 u32 bfin_pm_setup(void)
1018 {
1019         u32 val, i;
1020
1021         for (i = 0; i < NR_PINT_SYS_IRQS; i++) {
1022                 val = pint[i]->mask_clear;
1023                 pint_saved_masks[i] = val;
1024                 if (val ^ pint_wakeup_masks[i]) {
1025                         pint[i]->mask_clear = val;
1026                         pint[i]->mask_set = pint_wakeup_masks[i];
1027                 }
1028         }
1029
1030         return 0;
1031 }
1032
1033 void bfin_pm_restore(void)
1034 {
1035         u32 i, val;
1036
1037         for (i = 0; i < NR_PINT_SYS_IRQS; i++) {
1038                 val = pint_saved_masks[i];
1039                 if (val ^ pint_wakeup_masks[i]) {
1040                         pint[i]->mask_clear = pint[i]->mask_clear;
1041                         pint[i]->mask_set = val;
1042                 }
1043         }
1044 }
1045 #endif
1046
1047 static void bfin_demux_gpio_irq(unsigned int inta_irq,
1048                                 struct irq_desc *desc)
1049 {
1050         u32 bank, pint_val;
1051         u32 request, irq;
1052
1053         switch (inta_irq) {
1054         case IRQ_PINT0:
1055                 bank = 0;
1056                 break;
1057         case IRQ_PINT2:
1058                 bank = 2;
1059                 break;
1060         case IRQ_PINT3:
1061                 bank = 3;
1062                 break;
1063         case IRQ_PINT1:
1064                 bank = 1;
1065                 break;
1066         default:
1067                 return;
1068         }
1069
1070         pint_val = bank * NR_PINT_BITS;
1071
1072         request = pint[bank]->request;
1073
1074         while (request) {
1075                 if (request & 1) {
1076                         irq = pint2irq_lut[pint_val] + SYS_IRQS;
1077                         bfin_handle_irq(irq);
1078                 }
1079                 pint_val++;
1080                 request >>= 1;
1081         }
1082
1083 }
1084 #endif
1085
1086 static struct irq_chip bfin_gpio_irqchip = {
1087         .name = "GPIO",
1088         .irq_ack = bfin_gpio_ack_irq,
1089         .irq_mask = bfin_gpio_mask_irq,
1090         .irq_mask_ack = bfin_gpio_mask_ack_irq,
1091         .irq_unmask = bfin_gpio_unmask_irq,
1092         .irq_disable = bfin_gpio_mask_irq,
1093         .irq_enable = bfin_gpio_unmask_irq,
1094         .irq_set_type = bfin_gpio_irq_type,
1095         .irq_startup = bfin_gpio_irq_startup,
1096         .irq_shutdown = bfin_gpio_irq_shutdown,
1097 #ifdef CONFIG_PM
1098         .irq_set_wake = bfin_gpio_set_wake,
1099 #endif
1100 };
1101
1102 void __cpuinit init_exception_vectors(void)
1103 {
1104         /* cannot program in software:
1105          * evt0 - emulation (jtag)
1106          * evt1 - reset
1107          */
1108         bfin_write_EVT2(evt_nmi);
1109         bfin_write_EVT3(trap);
1110         bfin_write_EVT5(evt_ivhw);
1111         bfin_write_EVT6(evt_timer);
1112         bfin_write_EVT7(evt_evt7);
1113         bfin_write_EVT8(evt_evt8);
1114         bfin_write_EVT9(evt_evt9);
1115         bfin_write_EVT10(evt_evt10);
1116         bfin_write_EVT11(evt_evt11);
1117         bfin_write_EVT12(evt_evt12);
1118         bfin_write_EVT13(evt_evt13);
1119         bfin_write_EVT14(evt_evt14);
1120         bfin_write_EVT15(evt_system_call);
1121         CSYNC();
1122 }
1123
1124 /*
1125  * This function should be called during kernel startup to initialize
1126  * the BFin IRQ handling routines.
1127  */
1128
1129 int __init init_arch_irq(void)
1130 {
1131         int irq;
1132         unsigned long ilat = 0;
1133         /*  Disable all the peripheral intrs  - page 4-29 HW Ref manual */
1134 #if defined(CONFIG_BF54x) || defined(CONFIG_BF52x) || defined(CONFIG_BF561) \
1135         || defined(BF538_FAMILY) || defined(CONFIG_BF51x)
1136         bfin_write_SIC_IMASK0(SIC_UNMASK_ALL);
1137         bfin_write_SIC_IMASK1(SIC_UNMASK_ALL);
1138 # ifdef CONFIG_BF54x
1139         bfin_write_SIC_IMASK2(SIC_UNMASK_ALL);
1140 # endif
1141 # ifdef CONFIG_SMP
1142         bfin_write_SICB_IMASK0(SIC_UNMASK_ALL);
1143         bfin_write_SICB_IMASK1(SIC_UNMASK_ALL);
1144 # endif
1145 #else
1146         bfin_write_SIC_IMASK(SIC_UNMASK_ALL);
1147 #endif
1148
1149         local_irq_disable();
1150
1151 #if (defined(CONFIG_BF537) || defined(CONFIG_BF536))
1152         /* Clear EMAC Interrupt Status bits so we can demux it later */
1153         bfin_write_EMAC_SYSTAT(-1);
1154 #endif
1155
1156 #ifdef CONFIG_BF54x
1157 # ifdef CONFIG_PINTx_REASSIGN
1158         pint[0]->assign = CONFIG_PINT0_ASSIGN;
1159         pint[1]->assign = CONFIG_PINT1_ASSIGN;
1160         pint[2]->assign = CONFIG_PINT2_ASSIGN;
1161         pint[3]->assign = CONFIG_PINT3_ASSIGN;
1162 # endif
1163         /* Whenever PINTx_ASSIGN is altered init_pint_lut() must be executed! */
1164         init_pint_lut();
1165 #endif
1166
1167         for (irq = 0; irq <= SYS_IRQS; irq++) {
1168                 if (irq <= IRQ_CORETMR)
1169                         set_irq_chip(irq, &bfin_core_irqchip);
1170                 else
1171                         set_irq_chip(irq, &bfin_internal_irqchip);
1172
1173                 switch (irq) {
1174 #if defined(CONFIG_BF53x)
1175                 case IRQ_PROG_INTA:
1176 # if defined(BF537_FAMILY) && !(defined(CONFIG_BFIN_MAC) || defined(CONFIG_BFIN_MAC_MODULE))
1177                 case IRQ_MAC_RX:
1178 # endif
1179 #elif defined(CONFIG_BF54x)
1180                 case IRQ_PINT0:
1181                 case IRQ_PINT1:
1182                 case IRQ_PINT2:
1183                 case IRQ_PINT3:
1184 #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
1185                 case IRQ_PORTF_INTA:
1186                 case IRQ_PORTG_INTA:
1187                 case IRQ_PORTH_INTA:
1188 #elif defined(CONFIG_BF561)
1189                 case IRQ_PROG0_INTA:
1190                 case IRQ_PROG1_INTA:
1191                 case IRQ_PROG2_INTA:
1192 #elif defined(CONFIG_BF538) || defined(CONFIG_BF539)
1193                 case IRQ_PORTF_INTA:
1194 #endif
1195                         set_irq_chained_handler(irq,
1196                                                 bfin_demux_gpio_irq);
1197                         break;
1198 #ifdef BF537_GENERIC_ERROR_INT_DEMUX
1199                 case IRQ_GENERIC_ERROR:
1200                         set_irq_chained_handler(irq, bfin_demux_error_irq);
1201                         break;
1202 #endif
1203 #if defined(CONFIG_BFIN_MAC) || defined(CONFIG_BFIN_MAC_MODULE)
1204                 case IRQ_MAC_ERROR:
1205                         set_irq_chained_handler(irq, bfin_demux_mac_status_irq);
1206                         break;
1207 #endif
1208 #ifdef CONFIG_SMP
1209                 case IRQ_SUPPLE_0:
1210                 case IRQ_SUPPLE_1:
1211                         set_irq_handler(irq, handle_percpu_irq);
1212                         break;
1213 #endif
1214
1215 #ifdef CONFIG_TICKSOURCE_CORETMR
1216                 case IRQ_CORETMR:
1217 # ifdef CONFIG_SMP
1218                         set_irq_handler(irq, handle_percpu_irq);
1219                         break;
1220 # else
1221                         set_irq_handler(irq, handle_simple_irq);
1222                         break;
1223 # endif
1224 #endif
1225
1226 #ifdef CONFIG_TICKSOURCE_GPTMR0
1227                 case IRQ_TIMER0:
1228                         set_irq_handler(irq, handle_simple_irq);
1229                         break;
1230 #endif
1231
1232 #ifdef CONFIG_IPIPE
1233                 default:
1234                         set_irq_handler(irq, handle_level_irq);
1235                         break;
1236 #else /* !CONFIG_IPIPE */
1237                 default:
1238                         set_irq_handler(irq, handle_simple_irq);
1239                         break;
1240 #endif /* !CONFIG_IPIPE */
1241                 }
1242         }
1243
1244 #ifdef BF537_GENERIC_ERROR_INT_DEMUX
1245         for (irq = IRQ_PPI_ERROR; irq <= IRQ_UART1_ERROR; irq++)
1246                 set_irq_chip_and_handler(irq, &bfin_generic_error_irqchip,
1247                                          handle_level_irq);
1248 #if defined(CONFIG_BFIN_MAC) || defined(CONFIG_BFIN_MAC_MODULE)
1249         set_irq_chained_handler(IRQ_MAC_ERROR, bfin_demux_mac_status_irq);
1250 #endif
1251 #endif
1252
1253 #if defined(CONFIG_BFIN_MAC) || defined(CONFIG_BFIN_MAC_MODULE)
1254         for (irq = IRQ_MAC_PHYINT; irq <= IRQ_MAC_STMDONE; irq++)
1255                 set_irq_chip_and_handler(irq, &bfin_mac_status_irqchip,
1256                                          handle_level_irq);
1257 #endif
1258         /* if configured as edge, then will be changed to do_edge_IRQ */
1259         for (irq = GPIO_IRQ_BASE;
1260                 irq < (GPIO_IRQ_BASE + MAX_BLACKFIN_GPIOS); irq++)
1261                 set_irq_chip_and_handler(irq, &bfin_gpio_irqchip,
1262                                          handle_level_irq);
1263
1264         bfin_write_IMASK(0);
1265         CSYNC();
1266         ilat = bfin_read_ILAT();
1267         CSYNC();
1268         bfin_write_ILAT(ilat);
1269         CSYNC();
1270
1271         printk(KERN_INFO "Configuring Blackfin Priority Driven Interrupts\n");
1272         /* IMASK=xxx is equivalent to STI xx or bfin_irq_flags=xx,
1273          * local_irq_enable()
1274          */
1275         program_IAR();
1276         /* Therefore it's better to setup IARs before interrupts enabled */
1277         search_IAR();
1278
1279         /* Enable interrupts IVG7-15 */
1280         bfin_irq_flags |= IMASK_IVG15 |
1281             IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
1282             IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;
1283
1284         /* This implicitly covers ANOMALY_05000171
1285          * Boot-ROM code modifies SICA_IWRx wakeup registers
1286          */
1287 #ifdef SIC_IWR0
1288         bfin_write_SIC_IWR0(IWR_DISABLE_ALL);
1289 # ifdef SIC_IWR1
1290         /* BF52x/BF51x system reset does not properly reset SIC_IWR1 which
1291          * will screw up the bootrom as it relies on MDMA0/1 waking it
1292          * up from IDLE instructions.  See this report for more info:
1293          * http://blackfin.uclinux.org/gf/tracker/4323
1294          */
1295         if (ANOMALY_05000435)
1296                 bfin_write_SIC_IWR1(IWR_ENABLE(10) | IWR_ENABLE(11));
1297         else
1298                 bfin_write_SIC_IWR1(IWR_DISABLE_ALL);
1299 # endif
1300 # ifdef SIC_IWR2
1301         bfin_write_SIC_IWR2(IWR_DISABLE_ALL);
1302 # endif
1303 #else
1304         bfin_write_SIC_IWR(IWR_DISABLE_ALL);
1305 #endif
1306
1307         return 0;
1308 }
1309
1310 #ifdef CONFIG_DO_IRQ_L1
1311 __attribute__((l1_text))
1312 #endif
1313 void do_irq(int vec, struct pt_regs *fp)
1314 {
1315         if (vec == EVT_IVTMR_P) {
1316                 vec = IRQ_CORETMR;
1317         } else {
1318                 struct ivgx *ivg = ivg7_13[vec - IVG7].ifirst;
1319                 struct ivgx *ivg_stop = ivg7_13[vec - IVG7].istop;
1320 #if defined(SIC_ISR0)
1321                 unsigned long sic_status[3];
1322
1323                 if (smp_processor_id()) {
1324 # ifdef SICB_ISR0
1325                         /* This will be optimized out in UP mode. */
1326                         sic_status[0] = bfin_read_SICB_ISR0() & bfin_read_SICB_IMASK0();
1327                         sic_status[1] = bfin_read_SICB_ISR1() & bfin_read_SICB_IMASK1();
1328 # endif
1329                 } else {
1330                         sic_status[0] = bfin_read_SIC_ISR0() & bfin_read_SIC_IMASK0();
1331                         sic_status[1] = bfin_read_SIC_ISR1() & bfin_read_SIC_IMASK1();
1332                 }
1333 # ifdef SIC_ISR2
1334                 sic_status[2] = bfin_read_SIC_ISR2() & bfin_read_SIC_IMASK2();
1335 # endif
1336                 for (;; ivg++) {
1337                         if (ivg >= ivg_stop) {
1338                                 atomic_inc(&num_spurious);
1339                                 return;
1340                         }
1341                         if (sic_status[(ivg->irqno - IVG7) / 32] & ivg->isrflag)
1342                                 break;
1343                 }
1344 #else
1345                 unsigned long sic_status;
1346
1347                 sic_status = bfin_read_SIC_IMASK() & bfin_read_SIC_ISR();
1348
1349                 for (;; ivg++) {
1350                         if (ivg >= ivg_stop) {
1351                                 atomic_inc(&num_spurious);
1352                                 return;
1353                         } else if (sic_status & ivg->isrflag)
1354                                 break;
1355                 }
1356 #endif
1357                 vec = ivg->irqno;
1358         }
1359         asm_do_IRQ(vec, fp);
1360 }
1361
1362 #ifdef CONFIG_IPIPE
1363
1364 int __ipipe_get_irq_priority(unsigned irq)
1365 {
1366         int ient, prio;
1367
1368         if (irq <= IRQ_CORETMR)
1369                 return irq;
1370
1371         for (ient = 0; ient < NR_PERI_INTS; ient++) {
1372                 struct ivgx *ivg = ivg_table + ient;
1373                 if (ivg->irqno == irq) {
1374                         for (prio = 0; prio <= IVG13-IVG7; prio++) {
1375                                 if (ivg7_13[prio].ifirst <= ivg &&
1376                                     ivg7_13[prio].istop > ivg)
1377                                         return IVG7 + prio;
1378                         }
1379                 }
1380         }
1381
1382         return IVG15;
1383 }
1384
1385 /* Hw interrupts are disabled on entry (check SAVE_CONTEXT). */
1386 #ifdef CONFIG_DO_IRQ_L1
1387 __attribute__((l1_text))
1388 #endif
1389 asmlinkage int __ipipe_grab_irq(int vec, struct pt_regs *regs)
1390 {
1391         struct ipipe_percpu_domain_data *p = ipipe_root_cpudom_ptr();
1392         struct ipipe_domain *this_domain = __ipipe_current_domain;
1393         struct ivgx *ivg_stop = ivg7_13[vec-IVG7].istop;
1394         struct ivgx *ivg = ivg7_13[vec-IVG7].ifirst;
1395         int irq, s = 0;
1396
1397         if (likely(vec == EVT_IVTMR_P))
1398                 irq = IRQ_CORETMR;
1399         else {
1400 #if defined(SIC_ISR0)
1401                 unsigned long sic_status[3];
1402
1403                 sic_status[0] = bfin_read_SIC_ISR0() & bfin_read_SIC_IMASK0();
1404                 sic_status[1] = bfin_read_SIC_ISR1() & bfin_read_SIC_IMASK1();
1405 # ifdef SIC_ISR2
1406                 sic_status[2] = bfin_read_SIC_ISR2() & bfin_read_SIC_IMASK2();
1407 # endif
1408                 for (;; ivg++) {
1409                         if (ivg >= ivg_stop) {
1410                                 atomic_inc(&num_spurious);
1411                                 return 0;
1412                         }
1413                         if (sic_status[(ivg->irqno - IVG7) / 32] & ivg->isrflag)
1414                                 break;
1415                 }
1416 #else
1417                 unsigned long sic_status;
1418
1419                 sic_status = bfin_read_SIC_IMASK() & bfin_read_SIC_ISR();
1420
1421                 for (;; ivg++) {
1422                         if (ivg >= ivg_stop) {
1423                                 atomic_inc(&num_spurious);
1424                                 return 0;
1425                         } else if (sic_status & ivg->isrflag)
1426                                 break;
1427                 }
1428 #endif
1429                 irq = ivg->irqno;
1430         }
1431
1432         if (irq == IRQ_SYSTMR) {
1433 #if !defined(CONFIG_GENERIC_CLOCKEVENTS) || defined(CONFIG_TICKSOURCE_GPTMR0)
1434                 bfin_write_TIMER_STATUS(1); /* Latch TIMIL0 */
1435 #endif
1436                 /* This is basically what we need from the register frame. */
1437                 __raw_get_cpu_var(__ipipe_tick_regs).ipend = regs->ipend;
1438                 __raw_get_cpu_var(__ipipe_tick_regs).pc = regs->pc;
1439                 if (this_domain != ipipe_root_domain)
1440                         __raw_get_cpu_var(__ipipe_tick_regs).ipend &= ~0x10;
1441                 else
1442                         __raw_get_cpu_var(__ipipe_tick_regs).ipend |= 0x10;
1443         }
1444
1445         /*
1446          * We don't want Linux interrupt handlers to run at the
1447          * current core priority level (i.e. < EVT15), since this
1448          * might delay other interrupts handled by a high priority
1449          * domain. Here is what we do instead:
1450          *
1451          * - we raise the SYNCDEFER bit to prevent
1452          * __ipipe_handle_irq() to sync the pipeline for the root
1453          * stage for the incoming interrupt. Upon return, that IRQ is
1454          * pending in the interrupt log.
1455          *
1456          * - we raise the TIF_IRQ_SYNC bit for the current thread, so
1457          * that _schedule_and_signal_from_int will eventually sync the
1458          * pipeline from EVT15.
1459          */
1460         if (this_domain == ipipe_root_domain) {
1461                 s = __test_and_set_bit(IPIPE_SYNCDEFER_FLAG, &p->status);
1462                 barrier();
1463         }
1464
1465         ipipe_trace_irq_entry(irq);
1466         __ipipe_handle_irq(irq, regs);
1467         ipipe_trace_irq_exit(irq);
1468
1469         if (user_mode(regs) &&
1470             !ipipe_test_foreign_stack() &&
1471             (current->ipipe_flags & PF_EVTRET) != 0) {
1472                 /*
1473                  * Testing for user_regs() does NOT fully eliminate
1474                  * foreign stack contexts, because of the forged
1475                  * interrupt returns we do through
1476                  * __ipipe_call_irqtail. In that case, we might have
1477                  * preempted a foreign stack context in a high
1478                  * priority domain, with a single interrupt level now
1479                  * pending after the irqtail unwinding is done. In
1480                  * which case user_mode() is now true, and the event
1481                  * gets dispatched spuriously.
1482                  */
1483                 current->ipipe_flags &= ~PF_EVTRET;
1484                 __ipipe_dispatch_event(IPIPE_EVENT_RETURN, regs);
1485         }
1486
1487         if (this_domain == ipipe_root_domain) {
1488                 set_thread_flag(TIF_IRQ_SYNC);
1489                 if (!s) {
1490                         __clear_bit(IPIPE_SYNCDEFER_FLAG, &p->status);
1491                         return !test_bit(IPIPE_STALL_FLAG, &p->status);
1492                 }
1493         }
1494
1495         return 0;
1496 }
1497
1498 #endif /* CONFIG_IPIPE */