irq.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * linux/arch/arm/mach-at91/irq.c
  3. *
  4. * Copyright (C) 2004 SAN People
  5. * Copyright (C) 2004 ATMEL
  6. * Copyright (C) Rick Bronson
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/mm.h>
  25. #include <linux/types.h>
  26. #include <linux/irq.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/irqdomain.h>
  31. #include <linux/err.h>
  32. #include <mach/hardware.h>
  33. #include <asm/irq.h>
  34. #include <asm/setup.h>
  35. #include <asm/mach/arch.h>
  36. #include <asm/mach/irq.h>
  37. #include <asm/mach/map.h>
  38. void __iomem *at91_aic_base;
  39. static struct irq_domain *at91_aic_domain;
  40. static struct device_node *at91_aic_np;
  41. static void at91_aic_mask_irq(struct irq_data *d)
  42. {
  43. /* Disable interrupt on AIC */
  44. at91_aic_write(AT91_AIC_IDCR, 1 << d->hwirq);
  45. }
  46. static void at91_aic_unmask_irq(struct irq_data *d)
  47. {
  48. /* Enable interrupt on AIC */
  49. at91_aic_write(AT91_AIC_IECR, 1 << d->hwirq);
  50. }
  51. static void at91_aic_eoi(struct irq_data *d)
  52. {
  53. /*
  54. * Mark end-of-interrupt on AIC, the controller doesn't care about
  55. * the value written. Moreover it's a write-only register.
  56. */
  57. at91_aic_write(AT91_AIC_EOICR, 0);
  58. }
  59. unsigned int at91_extern_irq;
  60. #define is_extern_irq(hwirq) ((1 << (hwirq)) & at91_extern_irq)
  61. static int at91_aic_set_type(struct irq_data *d, unsigned type)
  62. {
  63. unsigned int smr, srctype;
  64. switch (type) {
  65. case IRQ_TYPE_LEVEL_HIGH:
  66. srctype = AT91_AIC_SRCTYPE_HIGH;
  67. break;
  68. case IRQ_TYPE_EDGE_RISING:
  69. srctype = AT91_AIC_SRCTYPE_RISING;
  70. break;
  71. case IRQ_TYPE_LEVEL_LOW:
  72. if ((d->hwirq == AT91_ID_FIQ) || is_extern_irq(d->hwirq)) /* only supported on external interrupts */
  73. srctype = AT91_AIC_SRCTYPE_LOW;
  74. else
  75. return -EINVAL;
  76. break;
  77. case IRQ_TYPE_EDGE_FALLING:
  78. if ((d->hwirq == AT91_ID_FIQ) || is_extern_irq(d->hwirq)) /* only supported on external interrupts */
  79. srctype = AT91_AIC_SRCTYPE_FALLING;
  80. else
  81. return -EINVAL;
  82. break;
  83. default:
  84. return -EINVAL;
  85. }
  86. smr = at91_aic_read(AT91_AIC_SMR(d->hwirq)) & ~AT91_AIC_SRCTYPE;
  87. at91_aic_write(AT91_AIC_SMR(d->hwirq), smr | srctype);
  88. return 0;
  89. }
  90. #ifdef CONFIG_PM
  91. static u32 wakeups;
  92. static u32 backups;
  93. static int at91_aic_set_wake(struct irq_data *d, unsigned value)
  94. {
  95. if (unlikely(d->hwirq >= NR_AIC_IRQS))
  96. return -EINVAL;
  97. if (value)
  98. wakeups |= (1 << d->hwirq);
  99. else
  100. wakeups &= ~(1 << d->hwirq);
  101. return 0;
  102. }
  103. void at91_irq_suspend(void)
  104. {
  105. backups = at91_aic_read(AT91_AIC_IMR);
  106. at91_aic_write(AT91_AIC_IDCR, backups);
  107. at91_aic_write(AT91_AIC_IECR, wakeups);
  108. }
  109. void at91_irq_resume(void)
  110. {
  111. at91_aic_write(AT91_AIC_IDCR, wakeups);
  112. at91_aic_write(AT91_AIC_IECR, backups);
  113. }
  114. #else
  115. #define at91_aic_set_wake NULL
  116. #endif
  117. static struct irq_chip at91_aic_chip = {
  118. .name = "AIC",
  119. .irq_mask = at91_aic_mask_irq,
  120. .irq_unmask = at91_aic_unmask_irq,
  121. .irq_set_type = at91_aic_set_type,
  122. .irq_set_wake = at91_aic_set_wake,
  123. .irq_eoi = at91_aic_eoi,
  124. };
  125. static void __init at91_aic_hw_init(unsigned int spu_vector)
  126. {
  127. int i;
  128. /*
  129. * Perform 8 End Of Interrupt Command to make sure AIC
  130. * will not Lock out nIRQ
  131. */
  132. for (i = 0; i < 8; i++)
  133. at91_aic_write(AT91_AIC_EOICR, 0);
  134. /*
  135. * Spurious Interrupt ID in Spurious Vector Register.
  136. * When there is no current interrupt, the IRQ Vector Register
  137. * reads the value stored in AIC_SPU
  138. */
  139. at91_aic_write(AT91_AIC_SPU, spu_vector);
  140. /* No debugging in AIC: Debug (Protect) Control Register */
  141. at91_aic_write(AT91_AIC_DCR, 0);
  142. /* Disable and clear all interrupts initially */
  143. at91_aic_write(AT91_AIC_IDCR, 0xFFFFFFFF);
  144. at91_aic_write(AT91_AIC_ICCR, 0xFFFFFFFF);
  145. }
  146. #if defined(CONFIG_OF)
  147. static int at91_aic_irq_map(struct irq_domain *h, unsigned int virq,
  148. irq_hw_number_t hw)
  149. {
  150. /* Put virq number in Source Vector Register */
  151. at91_aic_write(AT91_AIC_SVR(hw), virq);
  152. /* Active Low interrupt, without priority */
  153. at91_aic_write(AT91_AIC_SMR(hw), AT91_AIC_SRCTYPE_LOW);
  154. irq_set_chip_and_handler(virq, &at91_aic_chip, handle_fasteoi_irq);
  155. set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
  156. return 0;
  157. }
  158. static struct irq_domain_ops at91_aic_irq_ops = {
  159. .map = at91_aic_irq_map,
  160. .xlate = irq_domain_xlate_twocell,
  161. };
  162. int __init at91_aic_of_init(struct device_node *node,
  163. struct device_node *parent)
  164. {
  165. struct property *prop;
  166. const __be32 *p;
  167. u32 val;
  168. at91_aic_base = of_iomap(node, 0);
  169. at91_aic_np = node;
  170. at91_aic_domain = irq_domain_add_linear(at91_aic_np, NR_AIC_IRQS,
  171. &at91_aic_irq_ops, NULL);
  172. if (!at91_aic_domain)
  173. panic("Unable to add AIC irq domain (DT)\n");
  174. at91_extern_irq = 0;
  175. of_property_for_each_u32(node, "atmel,external-irqs", prop, p, val) {
  176. if (val > 31)
  177. pr_warn("AIC: external irq %d > 31 skip it\n", val);
  178. else
  179. at91_extern_irq |= (1 << val);
  180. }
  181. irq_set_default_host(at91_aic_domain);
  182. at91_aic_hw_init(NR_AIC_IRQS);
  183. return 0;
  184. }
  185. #endif
  186. /*
  187. * Initialize the AIC interrupt controller.
  188. */
  189. void __init at91_aic_init(unsigned int priority[NR_AIC_IRQS])
  190. {
  191. unsigned int i;
  192. int irq_base;
  193. at91_aic_base = ioremap(AT91_AIC, 512);
  194. if (!at91_aic_base)
  195. panic("Unable to ioremap AIC registers\n");
  196. /* Add irq domain for AIC */
  197. irq_base = irq_alloc_descs(-1, 0, NR_AIC_IRQS, 0);
  198. if (irq_base < 0) {
  199. WARN(1, "Cannot allocate irq_descs, assuming pre-allocated\n");
  200. irq_base = 0;
  201. }
  202. at91_aic_domain = irq_domain_add_legacy(at91_aic_np, NR_AIC_IRQS,
  203. irq_base, 0,
  204. &irq_domain_simple_ops, NULL);
  205. if (!at91_aic_domain)
  206. panic("Unable to add AIC irq domain\n");
  207. irq_set_default_host(at91_aic_domain);
  208. /*
  209. * The IVR is used by macro get_irqnr_and_base to read and verify.
  210. * The irq number is NR_AIC_IRQS when a spurious interrupt has occurred.
  211. */
  212. for (i = 0; i < NR_AIC_IRQS; i++) {
  213. /* Put hardware irq number in Source Vector Register: */
  214. at91_aic_write(AT91_AIC_SVR(i), i);
  215. /* Active Low interrupt, with the specified priority */
  216. at91_aic_write(AT91_AIC_SMR(i), AT91_AIC_SRCTYPE_LOW | priority[i]);
  217. irq_set_chip_and_handler(i, &at91_aic_chip, handle_fasteoi_irq);
  218. set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
  219. }
  220. at91_aic_hw_init(NR_AIC_IRQS);
  221. }