cp_intc.c 3.8 KB

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