irq.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * arch/arm/mach-orion/irq.c
  3. *
  4. * Core IRQ functions for Marvell Orion System On Chip
  5. *
  6. * Maintainer: Tzachi Perelstein <tzachi@marvell.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/irq.h>
  15. #include <asm/gpio.h>
  16. #include <asm/arch/orion.h>
  17. #include "common.h"
  18. /*****************************************************************************
  19. * Orion GPIO IRQ
  20. *
  21. * GPIO_IN_POL register controlls whether GPIO_DATA_IN will hold the same
  22. * value of the line or the opposite value.
  23. *
  24. * Level IRQ handlers: DATA_IN is used directly as cause register.
  25. * Interrupt are masked by LEVEL_MASK registers.
  26. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  27. * Interrupt are masked by EDGE_MASK registers.
  28. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  29. * the polarity to catch the next line transaction.
  30. * This is a race condition that might not perfectly
  31. * work on some use cases.
  32. *
  33. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  34. * cause register.
  35. *
  36. * EDGE cause mask
  37. * data-in /--------| |-----| |----\
  38. * -----| |----- ---- to main cause reg
  39. * X \----------------| |----/
  40. * polarity LEVEL mask
  41. *
  42. ****************************************************************************/
  43. static void orion_gpio_irq_ack(u32 irq)
  44. {
  45. int pin = irq_to_gpio(irq);
  46. if (irq_desc[irq].status & IRQ_LEVEL)
  47. /*
  48. * Mask bit for level interrupt
  49. */
  50. orion_clrbits(GPIO_LEVEL_MASK, 1 << pin);
  51. else
  52. /*
  53. * Clear casue bit for egde interrupt
  54. */
  55. orion_clrbits(GPIO_EDGE_CAUSE, 1 << pin);
  56. }
  57. static void orion_gpio_irq_mask(u32 irq)
  58. {
  59. int pin = irq_to_gpio(irq);
  60. if (irq_desc[irq].status & IRQ_LEVEL)
  61. orion_clrbits(GPIO_LEVEL_MASK, 1 << pin);
  62. else
  63. orion_clrbits(GPIO_EDGE_MASK, 1 << pin);
  64. }
  65. static void orion_gpio_irq_unmask(u32 irq)
  66. {
  67. int pin = irq_to_gpio(irq);
  68. if (irq_desc[irq].status & IRQ_LEVEL)
  69. orion_setbits(GPIO_LEVEL_MASK, 1 << pin);
  70. else
  71. orion_setbits(GPIO_EDGE_MASK, 1 << pin);
  72. }
  73. static int orion_gpio_set_irq_type(u32 irq, u32 type)
  74. {
  75. int pin = irq_to_gpio(irq);
  76. struct irq_desc *desc;
  77. if ((orion_read(GPIO_IO_CONF) & (1 << pin)) == 0) {
  78. printk(KERN_ERR "orion_gpio_set_irq_type failed "
  79. "(irq %d, pin %d).\n", irq, pin);
  80. return -EINVAL;
  81. }
  82. desc = irq_desc + irq;
  83. switch (type) {
  84. case IRQT_HIGH:
  85. desc->handle_irq = handle_level_irq;
  86. desc->status |= IRQ_LEVEL;
  87. orion_clrbits(GPIO_IN_POL, (1 << pin));
  88. break;
  89. case IRQT_LOW:
  90. desc->handle_irq = handle_level_irq;
  91. desc->status |= IRQ_LEVEL;
  92. orion_setbits(GPIO_IN_POL, (1 << pin));
  93. break;
  94. case IRQT_RISING:
  95. desc->handle_irq = handle_edge_irq;
  96. desc->status &= ~IRQ_LEVEL;
  97. orion_clrbits(GPIO_IN_POL, (1 << pin));
  98. break;
  99. case IRQT_FALLING:
  100. desc->handle_irq = handle_edge_irq;
  101. desc->status &= ~IRQ_LEVEL;
  102. orion_setbits(GPIO_IN_POL, (1 << pin));
  103. break;
  104. case IRQT_BOTHEDGE:
  105. desc->handle_irq = handle_edge_irq;
  106. desc->status &= ~IRQ_LEVEL;
  107. /*
  108. * set initial polarity based on current input level
  109. */
  110. if ((orion_read(GPIO_IN_POL) ^ orion_read(GPIO_DATA_IN))
  111. & (1 << pin))
  112. orion_setbits(GPIO_IN_POL, (1 << pin)); /* falling */
  113. else
  114. orion_clrbits(GPIO_IN_POL, (1 << pin)); /* rising */
  115. break;
  116. default:
  117. printk(KERN_ERR "failed to set irq=%d (type=%d)\n", irq, type);
  118. return -EINVAL;
  119. }
  120. desc->status &= ~IRQ_TYPE_SENSE_MASK;
  121. desc->status |= type & IRQ_TYPE_SENSE_MASK;
  122. return 0;
  123. }
  124. static struct irq_chip orion_gpio_irq_chip = {
  125. .name = "Orion-IRQ-GPIO",
  126. .ack = orion_gpio_irq_ack,
  127. .mask = orion_gpio_irq_mask,
  128. .unmask = orion_gpio_irq_unmask,
  129. .set_type = orion_gpio_set_irq_type,
  130. };
  131. static void orion_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  132. {
  133. u32 cause, offs, pin;
  134. BUG_ON(irq < IRQ_ORION_GPIO_0_7 || irq > IRQ_ORION_GPIO_24_31);
  135. offs = (irq - IRQ_ORION_GPIO_0_7) * 8;
  136. cause = (orion_read(GPIO_DATA_IN) & orion_read(GPIO_LEVEL_MASK)) |
  137. (orion_read(GPIO_EDGE_CAUSE) & orion_read(GPIO_EDGE_MASK));
  138. for (pin = offs; pin < offs + 8; pin++) {
  139. if (cause & (1 << pin)) {
  140. irq = gpio_to_irq(pin);
  141. desc = irq_desc + irq;
  142. if ((desc->status & IRQ_TYPE_SENSE_MASK) == IRQT_BOTHEDGE) {
  143. /* Swap polarity (race with GPIO line) */
  144. u32 polarity = orion_read(GPIO_IN_POL);
  145. polarity ^= 1 << pin;
  146. orion_write(GPIO_IN_POL, polarity);
  147. }
  148. desc_handle_irq(irq, desc);
  149. }
  150. }
  151. }
  152. static void __init orion_init_gpio_irq(void)
  153. {
  154. int i;
  155. struct irq_desc *desc;
  156. /*
  157. * Mask and clear GPIO IRQ interrupts
  158. */
  159. orion_write(GPIO_LEVEL_MASK, 0x0);
  160. orion_write(GPIO_EDGE_MASK, 0x0);
  161. orion_write(GPIO_EDGE_CAUSE, 0x0);
  162. /*
  163. * Register chained level handlers for GPIO IRQs by default.
  164. * User can use set_type() if he wants to use edge types handlers.
  165. */
  166. for (i = IRQ_ORION_GPIO_START; i < NR_IRQS; i++) {
  167. set_irq_chip(i, &orion_gpio_irq_chip);
  168. set_irq_handler(i, handle_level_irq);
  169. desc = irq_desc + i;
  170. desc->status |= IRQ_LEVEL;
  171. set_irq_flags(i, IRQF_VALID);
  172. }
  173. set_irq_chained_handler(IRQ_ORION_GPIO_0_7, orion_gpio_irq_handler);
  174. set_irq_chained_handler(IRQ_ORION_GPIO_8_15, orion_gpio_irq_handler);
  175. set_irq_chained_handler(IRQ_ORION_GPIO_16_23, orion_gpio_irq_handler);
  176. set_irq_chained_handler(IRQ_ORION_GPIO_24_31, orion_gpio_irq_handler);
  177. }
  178. /*****************************************************************************
  179. * Orion Main IRQ
  180. ****************************************************************************/
  181. static void orion_main_irq_mask(u32 irq)
  182. {
  183. orion_clrbits(MAIN_IRQ_MASK, 1 << irq);
  184. }
  185. static void orion_main_irq_unmask(u32 irq)
  186. {
  187. orion_setbits(MAIN_IRQ_MASK, 1 << irq);
  188. }
  189. static struct irq_chip orion_main_irq_chip = {
  190. .name = "Orion-IRQ-Main",
  191. .ack = orion_main_irq_mask,
  192. .mask = orion_main_irq_mask,
  193. .unmask = orion_main_irq_unmask,
  194. };
  195. static void __init orion_init_main_irq(void)
  196. {
  197. int i;
  198. /*
  199. * Mask and clear Main IRQ interrupts
  200. */
  201. orion_write(MAIN_IRQ_MASK, 0x0);
  202. orion_write(MAIN_IRQ_CAUSE, 0x0);
  203. /*
  204. * Register level handler for Main IRQs
  205. */
  206. for (i = 0; i < IRQ_ORION_GPIO_START; i++) {
  207. set_irq_chip(i, &orion_main_irq_chip);
  208. set_irq_handler(i, handle_level_irq);
  209. set_irq_flags(i, IRQF_VALID);
  210. }
  211. }
  212. void __init orion_init_irq(void)
  213. {
  214. orion_init_main_irq();
  215. orion_init_gpio_irq();
  216. }