cp_intc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * TI Common Platform Interrupt Controller (cp_intc) driver
  3. *
  4. * Author: Steve Chen <schen@mvista.com>
  5. * Copyright (C) 2008-2009, MontaVista Software, Inc. <source@mvista.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public License
  8. * version 2. This program is licensed "as is" without any warranty of any
  9. * kind, whether express or implied.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/sched.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel.h>
  15. #include <linux/irq.h>
  16. #include <linux/io.h>
  17. #include <mach/cp_intc.h>
  18. static void __iomem *cp_intc_base;
  19. static inline unsigned int cp_intc_read(unsigned offset)
  20. {
  21. return __raw_readl(cp_intc_base + offset);
  22. }
  23. static inline void cp_intc_write(unsigned long value, unsigned offset)
  24. {
  25. __raw_writel(value, cp_intc_base + offset);
  26. }
  27. static void cp_intc_ack_irq(unsigned int irq)
  28. {
  29. cp_intc_write(irq, CP_INTC_SYS_STAT_IDX_CLR);
  30. }
  31. /* Disable interrupt */
  32. static void cp_intc_mask_irq(unsigned int irq)
  33. {
  34. /* XXX don't know why we need to disable nIRQ here... */
  35. cp_intc_write(1, CP_INTC_HOST_ENABLE_IDX_CLR);
  36. cp_intc_write(irq, CP_INTC_SYS_ENABLE_IDX_CLR);
  37. cp_intc_write(1, CP_INTC_HOST_ENABLE_IDX_SET);
  38. }
  39. /* Enable interrupt */
  40. static void cp_intc_unmask_irq(unsigned int irq)
  41. {
  42. cp_intc_write(irq, CP_INTC_SYS_ENABLE_IDX_SET);
  43. }
  44. static int cp_intc_set_irq_type(unsigned int irq, unsigned int flow_type)
  45. {
  46. unsigned reg = BIT_WORD(irq);
  47. unsigned mask = BIT_MASK(irq);
  48. unsigned polarity = cp_intc_read(CP_INTC_SYS_POLARITY(reg));
  49. unsigned type = cp_intc_read(CP_INTC_SYS_TYPE(reg));
  50. switch (flow_type) {
  51. case IRQ_TYPE_EDGE_RISING:
  52. polarity |= mask;
  53. type |= mask;
  54. break;
  55. case IRQ_TYPE_EDGE_FALLING:
  56. polarity &= ~mask;
  57. type |= mask;
  58. break;
  59. case IRQ_TYPE_LEVEL_HIGH:
  60. polarity |= mask;
  61. type &= ~mask;
  62. break;
  63. case IRQ_TYPE_LEVEL_LOW:
  64. polarity &= ~mask;
  65. type &= ~mask;
  66. break;
  67. default:
  68. return -EINVAL;
  69. }
  70. cp_intc_write(polarity, CP_INTC_SYS_POLARITY(reg));
  71. cp_intc_write(type, CP_INTC_SYS_TYPE(reg));
  72. return 0;
  73. }
  74. static struct irq_chip cp_intc_irq_chip = {
  75. .name = "cp_intc",
  76. .ack = cp_intc_ack_irq,
  77. .mask = cp_intc_mask_irq,
  78. .unmask = cp_intc_unmask_irq,
  79. .set_type = cp_intc_set_irq_type,
  80. };
  81. void __init cp_intc_init(void __iomem *base, unsigned short num_irq,
  82. u8 *irq_prio)
  83. {
  84. unsigned num_reg = BITS_TO_LONGS(num_irq);
  85. int i;
  86. cp_intc_base = base;
  87. cp_intc_write(0, CP_INTC_GLOBAL_ENABLE);
  88. /* Disable all host interrupts */
  89. cp_intc_write(0, CP_INTC_HOST_ENABLE(0));
  90. /* Disable system interrupts */
  91. for (i = 0; i < num_reg; i++)
  92. cp_intc_write(~0, CP_INTC_SYS_ENABLE_CLR(i));
  93. /* Set to normal mode, no nesting, no priority hold */
  94. cp_intc_write(0, CP_INTC_CTRL);
  95. cp_intc_write(0, CP_INTC_HOST_CTRL);
  96. /* Clear system interrupt status */
  97. for (i = 0; i < num_reg; i++)
  98. cp_intc_write(~0, CP_INTC_SYS_STAT_CLR(i));
  99. /* Enable nIRQ (what about nFIQ?) */
  100. cp_intc_write(1, CP_INTC_HOST_ENABLE_IDX_SET);
  101. /*
  102. * Priority is determined by host channel: lower channel number has
  103. * higher priority i.e. channel 0 has highest priority and channel 31
  104. * had the lowest priority.
  105. */
  106. num_reg = (num_irq + 3) >> 2; /* 4 channels per register */
  107. if (irq_prio) {
  108. unsigned j, k;
  109. u32 val;
  110. for (k = i = 0; i < num_reg; i++) {
  111. for (val = j = 0; j < 4; j++, k++) {
  112. val >>= 8;
  113. if (k < num_irq)
  114. val |= irq_prio[k] << 24;
  115. }
  116. cp_intc_write(val, CP_INTC_CHAN_MAP(i));
  117. }
  118. } else {
  119. /*
  120. * Default everything to channel 15 if priority not specified.
  121. * Note that channel 0-1 are mapped to nFIQ and channels 2-31
  122. * are mapped to nIRQ.
  123. */
  124. for (i = 0; i < num_reg; i++)
  125. cp_intc_write(0x0f0f0f0f, CP_INTC_CHAN_MAP(i));
  126. }
  127. /* Set up genirq dispatching for cp_intc */
  128. for (i = 0; i < num_irq; i++) {
  129. set_irq_chip(i, &cp_intc_irq_chip);
  130. set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
  131. set_irq_handler(i, handle_edge_irq);
  132. }
  133. /* Enable global interrupt */
  134. cp_intc_write(1, CP_INTC_GLOBAL_ENABLE);
  135. }