irq.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/bitmap.h>
  26. #include <linux/types.h>
  27. #include <linux/irq.h>
  28. #include <linux/of.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/irqdomain.h>
  32. #include <linux/err.h>
  33. #include <linux/slab.h>
  34. #include <mach/hardware.h>
  35. #include <asm/irq.h>
  36. #include <asm/setup.h>
  37. #include <asm/exception.h>
  38. #include <asm/mach/arch.h>
  39. #include <asm/mach/irq.h>
  40. #include <asm/mach/map.h>
  41. #include "at91_aic.h"
  42. void __iomem *at91_aic_base;
  43. static struct irq_domain *at91_aic_domain;
  44. static struct device_node *at91_aic_np;
  45. static unsigned int n_irqs = NR_AIC_IRQS;
  46. #ifdef CONFIG_PM
  47. static unsigned long *wakeups;
  48. static unsigned long *backups;
  49. #define set_backup(bit) set_bit(bit, backups)
  50. #define clear_backup(bit) clear_bit(bit, backups)
  51. static int at91_aic_pm_init(void)
  52. {
  53. backups = kzalloc(BITS_TO_LONGS(n_irqs) * sizeof(*backups), GFP_KERNEL);
  54. if (!backups)
  55. return -ENOMEM;
  56. wakeups = kzalloc(BITS_TO_LONGS(n_irqs) * sizeof(*backups), GFP_KERNEL);
  57. if (!wakeups) {
  58. kfree(backups);
  59. return -ENOMEM;
  60. }
  61. return 0;
  62. }
  63. static int at91_aic_set_wake(struct irq_data *d, unsigned value)
  64. {
  65. if (unlikely(d->hwirq >= n_irqs))
  66. return -EINVAL;
  67. if (value)
  68. set_bit(d->hwirq, wakeups);
  69. else
  70. clear_bit(d->hwirq, wakeups);
  71. return 0;
  72. }
  73. void at91_irq_suspend(void)
  74. {
  75. at91_aic_write(AT91_AIC_IDCR, *backups);
  76. at91_aic_write(AT91_AIC_IECR, *wakeups);
  77. }
  78. void at91_irq_resume(void)
  79. {
  80. at91_aic_write(AT91_AIC_IDCR, *wakeups);
  81. at91_aic_write(AT91_AIC_IECR, *backups);
  82. }
  83. #else
  84. static inline int at91_aic_pm_init(void)
  85. {
  86. return 0;
  87. }
  88. #define set_backup(bit)
  89. #define clear_backup(bit)
  90. #define at91_aic_set_wake NULL
  91. #endif /* CONFIG_PM */
  92. asmlinkage void __exception_irq_entry
  93. at91_aic_handle_irq(struct pt_regs *regs)
  94. {
  95. u32 irqnr;
  96. u32 irqstat;
  97. irqnr = at91_aic_read(AT91_AIC_IVR);
  98. irqstat = at91_aic_read(AT91_AIC_ISR);
  99. /*
  100. * ISR value is 0 when there is no current interrupt or when there is
  101. * a spurious interrupt
  102. */
  103. if (!irqstat)
  104. at91_aic_write(AT91_AIC_EOICR, 0);
  105. else
  106. handle_IRQ(irqnr, regs);
  107. }
  108. static void at91_aic_mask_irq(struct irq_data *d)
  109. {
  110. /* Disable interrupt on AIC */
  111. at91_aic_write(AT91_AIC_IDCR, 1 << d->hwirq);
  112. /* Update ISR cache */
  113. clear_backup(d->hwirq);
  114. }
  115. static void at91_aic_unmask_irq(struct irq_data *d)
  116. {
  117. /* Enable interrupt on AIC */
  118. at91_aic_write(AT91_AIC_IECR, 1 << d->hwirq);
  119. /* Update ISR cache */
  120. set_backup(d->hwirq);
  121. }
  122. static void at91_aic_eoi(struct irq_data *d)
  123. {
  124. /*
  125. * Mark end-of-interrupt on AIC, the controller doesn't care about
  126. * the value written. Moreover it's a write-only register.
  127. */
  128. at91_aic_write(AT91_AIC_EOICR, 0);
  129. }
  130. static unsigned long *at91_extern_irq;
  131. u32 at91_get_extern_irq(void)
  132. {
  133. if (!at91_extern_irq)
  134. return 0;
  135. return *at91_extern_irq;
  136. }
  137. #define is_extern_irq(hwirq) test_bit(hwirq, at91_extern_irq)
  138. static int at91_aic_compute_srctype(struct irq_data *d, unsigned type)
  139. {
  140. int srctype;
  141. switch (type) {
  142. case IRQ_TYPE_LEVEL_HIGH:
  143. srctype = AT91_AIC_SRCTYPE_HIGH;
  144. break;
  145. case IRQ_TYPE_EDGE_RISING:
  146. srctype = AT91_AIC_SRCTYPE_RISING;
  147. break;
  148. case IRQ_TYPE_LEVEL_LOW:
  149. if ((d->hwirq == AT91_ID_FIQ) || is_extern_irq(d->hwirq)) /* only supported on external interrupts */
  150. srctype = AT91_AIC_SRCTYPE_LOW;
  151. else
  152. srctype = -EINVAL;
  153. break;
  154. case IRQ_TYPE_EDGE_FALLING:
  155. if ((d->hwirq == AT91_ID_FIQ) || is_extern_irq(d->hwirq)) /* only supported on external interrupts */
  156. srctype = AT91_AIC_SRCTYPE_FALLING;
  157. else
  158. srctype = -EINVAL;
  159. break;
  160. default:
  161. srctype = -EINVAL;
  162. }
  163. return srctype;
  164. }
  165. static int at91_aic_set_type(struct irq_data *d, unsigned type)
  166. {
  167. unsigned int smr;
  168. int srctype;
  169. srctype = at91_aic_compute_srctype(d, type);
  170. if (srctype < 0)
  171. return srctype;
  172. smr = at91_aic_read(AT91_AIC_SMR(d->hwirq)) & ~AT91_AIC_SRCTYPE;
  173. at91_aic_write(AT91_AIC_SMR(d->hwirq), smr | srctype);
  174. return 0;
  175. }
  176. static struct irq_chip at91_aic_chip = {
  177. .name = "AIC",
  178. .irq_mask = at91_aic_mask_irq,
  179. .irq_unmask = at91_aic_unmask_irq,
  180. .irq_set_type = at91_aic_set_type,
  181. .irq_set_wake = at91_aic_set_wake,
  182. .irq_eoi = at91_aic_eoi,
  183. };
  184. static void __init at91_aic_hw_init(unsigned int spu_vector)
  185. {
  186. int i;
  187. /*
  188. * Perform 8 End Of Interrupt Command to make sure AIC
  189. * will not Lock out nIRQ
  190. */
  191. for (i = 0; i < 8; i++)
  192. at91_aic_write(AT91_AIC_EOICR, 0);
  193. /*
  194. * Spurious Interrupt ID in Spurious Vector Register.
  195. * When there is no current interrupt, the IRQ Vector Register
  196. * reads the value stored in AIC_SPU
  197. */
  198. at91_aic_write(AT91_AIC_SPU, spu_vector);
  199. /* No debugging in AIC: Debug (Protect) Control Register */
  200. at91_aic_write(AT91_AIC_DCR, 0);
  201. /* Disable and clear all interrupts initially */
  202. at91_aic_write(AT91_AIC_IDCR, 0xFFFFFFFF);
  203. at91_aic_write(AT91_AIC_ICCR, 0xFFFFFFFF);
  204. }
  205. /*
  206. * Initialize the AIC interrupt controller.
  207. */
  208. void __init at91_aic_init(unsigned int *priority, unsigned int ext_irq_mask)
  209. {
  210. unsigned int i;
  211. int irq_base;
  212. at91_extern_irq = kzalloc(BITS_TO_LONGS(n_irqs)
  213. * sizeof(*at91_extern_irq), GFP_KERNEL);
  214. if (at91_aic_pm_init() || at91_extern_irq == NULL)
  215. panic("Unable to allocate bit maps\n");
  216. *at91_extern_irq = ext_irq_mask;
  217. at91_aic_base = ioremap(AT91_AIC, 512);
  218. if (!at91_aic_base)
  219. panic("Unable to ioremap AIC registers\n");
  220. /* Add irq domain for AIC */
  221. irq_base = irq_alloc_descs(-1, 0, n_irqs, 0);
  222. if (irq_base < 0) {
  223. WARN(1, "Cannot allocate irq_descs, assuming pre-allocated\n");
  224. irq_base = 0;
  225. }
  226. at91_aic_domain = irq_domain_add_legacy(at91_aic_np, n_irqs,
  227. irq_base, 0,
  228. &irq_domain_simple_ops, NULL);
  229. if (!at91_aic_domain)
  230. panic("Unable to add AIC irq domain\n");
  231. irq_set_default_host(at91_aic_domain);
  232. /*
  233. * The IVR is used by macro get_irqnr_and_base to read and verify.
  234. * The irq number is NR_AIC_IRQS when a spurious interrupt has occurred.
  235. */
  236. for (i = 0; i < n_irqs; i++) {
  237. /* Put hardware irq number in Source Vector Register: */
  238. at91_aic_write(AT91_AIC_SVR(i), NR_IRQS_LEGACY + i);
  239. /* Active Low interrupt, with the specified priority */
  240. at91_aic_write(AT91_AIC_SMR(i), AT91_AIC_SRCTYPE_LOW | priority[i]);
  241. irq_set_chip_and_handler(NR_IRQS_LEGACY + i, &at91_aic_chip, handle_fasteoi_irq);
  242. set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
  243. }
  244. at91_aic_hw_init(n_irqs);
  245. }