irq-atmel-aic5.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Atmel AT91 AIC5 (Advanced Interrupt Controller) driver
  3. *
  4. * Copyright (C) 2004 SAN People
  5. * Copyright (C) 2004 ATMEL
  6. * Copyright (C) Rick Bronson
  7. * Copyright (C) 2014 Free Electrons
  8. *
  9. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/mm.h>
  18. #include <linux/bitmap.h>
  19. #include <linux/types.h>
  20. #include <linux/irq.h>
  21. #include <linux/irqchip.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/irqdomain.h>
  26. #include <linux/err.h>
  27. #include <linux/slab.h>
  28. #include <linux/io.h>
  29. #include <asm/exception.h>
  30. #include <asm/mach/irq.h>
  31. #include "irq-atmel-aic-common.h"
  32. /* Number of irq lines managed by AIC */
  33. #define NR_AIC5_IRQS 128
  34. #define AT91_AIC5_SSR 0x0
  35. #define AT91_AIC5_INTSEL_MSK (0x7f << 0)
  36. #define AT91_AIC5_SMR 0x4
  37. #define AT91_AIC5_SVR 0x8
  38. #define AT91_AIC5_IVR 0x10
  39. #define AT91_AIC5_FVR 0x14
  40. #define AT91_AIC5_ISR 0x18
  41. #define AT91_AIC5_IPR0 0x20
  42. #define AT91_AIC5_IPR1 0x24
  43. #define AT91_AIC5_IPR2 0x28
  44. #define AT91_AIC5_IPR3 0x2c
  45. #define AT91_AIC5_IMR 0x30
  46. #define AT91_AIC5_CISR 0x34
  47. #define AT91_AIC5_IECR 0x40
  48. #define AT91_AIC5_IDCR 0x44
  49. #define AT91_AIC5_ICCR 0x48
  50. #define AT91_AIC5_ISCR 0x4c
  51. #define AT91_AIC5_EOICR 0x38
  52. #define AT91_AIC5_SPU 0x3c
  53. #define AT91_AIC5_DCR 0x6c
  54. #define AT91_AIC5_FFER 0x50
  55. #define AT91_AIC5_FFDR 0x54
  56. #define AT91_AIC5_FFSR 0x58
  57. static struct irq_domain *aic5_domain;
  58. static asmlinkage void __exception_irq_entry
  59. aic5_handle(struct pt_regs *regs)
  60. {
  61. struct irq_domain_chip_generic *dgc = aic5_domain->gc;
  62. struct irq_chip_generic *gc = dgc->gc[0];
  63. u32 irqnr;
  64. u32 irqstat;
  65. irqnr = irq_reg_readl(gc, AT91_AIC5_IVR);
  66. irqstat = irq_reg_readl(gc, AT91_AIC5_ISR);
  67. if (!irqstat)
  68. irq_reg_writel(gc, 0, AT91_AIC5_EOICR);
  69. else
  70. handle_domain_irq(aic5_domain, irqnr, regs);
  71. }
  72. static void aic5_mask(struct irq_data *d)
  73. {
  74. struct irq_domain *domain = d->domain;
  75. struct irq_domain_chip_generic *dgc = domain->gc;
  76. struct irq_chip_generic *bgc = dgc->gc[0];
  77. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  78. /*
  79. * Disable interrupt on AIC5. We always take the lock of the
  80. * first irq chip as all chips share the same registers.
  81. */
  82. irq_gc_lock(bgc);
  83. irq_reg_writel(gc, d->hwirq, AT91_AIC5_SSR);
  84. irq_reg_writel(gc, 1, AT91_AIC5_IDCR);
  85. gc->mask_cache &= ~d->mask;
  86. irq_gc_unlock(bgc);
  87. }
  88. static void aic5_unmask(struct irq_data *d)
  89. {
  90. struct irq_domain *domain = d->domain;
  91. struct irq_domain_chip_generic *dgc = domain->gc;
  92. struct irq_chip_generic *bgc = dgc->gc[0];
  93. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  94. /*
  95. * Enable interrupt on AIC5. We always take the lock of the
  96. * first irq chip as all chips share the same registers.
  97. */
  98. irq_gc_lock(bgc);
  99. irq_reg_writel(gc, d->hwirq, AT91_AIC5_SSR);
  100. irq_reg_writel(gc, 1, AT91_AIC5_IECR);
  101. gc->mask_cache |= d->mask;
  102. irq_gc_unlock(bgc);
  103. }
  104. static int aic5_retrigger(struct irq_data *d)
  105. {
  106. struct irq_domain *domain = d->domain;
  107. struct irq_domain_chip_generic *dgc = domain->gc;
  108. struct irq_chip_generic *gc = dgc->gc[0];
  109. /* Enable interrupt on AIC5 */
  110. irq_gc_lock(gc);
  111. irq_reg_writel(gc, d->hwirq, AT91_AIC5_SSR);
  112. irq_reg_writel(gc, 1, AT91_AIC5_ISCR);
  113. irq_gc_unlock(gc);
  114. return 0;
  115. }
  116. static int aic5_set_type(struct irq_data *d, unsigned type)
  117. {
  118. struct irq_domain *domain = d->domain;
  119. struct irq_domain_chip_generic *dgc = domain->gc;
  120. struct irq_chip_generic *gc = dgc->gc[0];
  121. unsigned int smr;
  122. int ret;
  123. irq_gc_lock(gc);
  124. irq_reg_writel(gc, d->hwirq, AT91_AIC5_SSR);
  125. smr = irq_reg_readl(gc, AT91_AIC5_SMR);
  126. ret = aic_common_set_type(d, type, &smr);
  127. if (!ret)
  128. irq_reg_writel(gc, smr, AT91_AIC5_SMR);
  129. irq_gc_unlock(gc);
  130. return ret;
  131. }
  132. #ifdef CONFIG_PM
  133. static void aic5_suspend(struct irq_data *d)
  134. {
  135. struct irq_domain *domain = d->domain;
  136. struct irq_domain_chip_generic *dgc = domain->gc;
  137. struct irq_chip_generic *bgc = dgc->gc[0];
  138. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  139. int i;
  140. u32 mask;
  141. irq_gc_lock(bgc);
  142. for (i = 0; i < dgc->irqs_per_chip; i++) {
  143. mask = 1 << i;
  144. if ((mask & gc->mask_cache) == (mask & gc->wake_active))
  145. continue;
  146. irq_reg_writel(bgc, i + gc->irq_base, AT91_AIC5_SSR);
  147. if (mask & gc->wake_active)
  148. irq_reg_writel(bgc, 1, AT91_AIC5_IECR);
  149. else
  150. irq_reg_writel(bgc, 1, AT91_AIC5_IDCR);
  151. }
  152. irq_gc_unlock(bgc);
  153. }
  154. static void aic5_resume(struct irq_data *d)
  155. {
  156. struct irq_domain *domain = d->domain;
  157. struct irq_domain_chip_generic *dgc = domain->gc;
  158. struct irq_chip_generic *bgc = dgc->gc[0];
  159. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  160. int i;
  161. u32 mask;
  162. irq_gc_lock(bgc);
  163. for (i = 0; i < dgc->irqs_per_chip; i++) {
  164. mask = 1 << i;
  165. if ((mask & gc->mask_cache) == (mask & gc->wake_active))
  166. continue;
  167. irq_reg_writel(bgc, i + gc->irq_base, AT91_AIC5_SSR);
  168. if (mask & gc->mask_cache)
  169. irq_reg_writel(bgc, 1, AT91_AIC5_IECR);
  170. else
  171. irq_reg_writel(bgc, 1, AT91_AIC5_IDCR);
  172. }
  173. irq_gc_unlock(bgc);
  174. }
  175. static void aic5_pm_shutdown(struct irq_data *d)
  176. {
  177. struct irq_domain *domain = d->domain;
  178. struct irq_domain_chip_generic *dgc = domain->gc;
  179. struct irq_chip_generic *bgc = dgc->gc[0];
  180. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  181. int i;
  182. irq_gc_lock(bgc);
  183. for (i = 0; i < dgc->irqs_per_chip; i++) {
  184. irq_reg_writel(bgc, i + gc->irq_base, AT91_AIC5_SSR);
  185. irq_reg_writel(bgc, 1, AT91_AIC5_IDCR);
  186. irq_reg_writel(bgc, 1, AT91_AIC5_ICCR);
  187. }
  188. irq_gc_unlock(bgc);
  189. }
  190. #else
  191. #define aic5_suspend NULL
  192. #define aic5_resume NULL
  193. #define aic5_pm_shutdown NULL
  194. #endif /* CONFIG_PM */
  195. static void __init aic5_hw_init(struct irq_domain *domain)
  196. {
  197. struct irq_chip_generic *gc = irq_get_domain_generic_chip(domain, 0);
  198. int i;
  199. /*
  200. * Perform 8 End Of Interrupt Command to make sure AIC
  201. * will not Lock out nIRQ
  202. */
  203. for (i = 0; i < 8; i++)
  204. irq_reg_writel(gc, 0, AT91_AIC5_EOICR);
  205. /*
  206. * Spurious Interrupt ID in Spurious Vector Register.
  207. * When there is no current interrupt, the IRQ Vector Register
  208. * reads the value stored in AIC_SPU
  209. */
  210. irq_reg_writel(gc, 0xffffffff, AT91_AIC5_SPU);
  211. /* No debugging in AIC: Debug (Protect) Control Register */
  212. irq_reg_writel(gc, 0, AT91_AIC5_DCR);
  213. /* Disable and clear all interrupts initially */
  214. for (i = 0; i < domain->revmap_size; i++) {
  215. irq_reg_writel(gc, i, AT91_AIC5_SSR);
  216. irq_reg_writel(gc, i, AT91_AIC5_SVR);
  217. irq_reg_writel(gc, 1, AT91_AIC5_IDCR);
  218. irq_reg_writel(gc, 1, AT91_AIC5_ICCR);
  219. }
  220. }
  221. static int aic5_irq_domain_xlate(struct irq_domain *d,
  222. struct device_node *ctrlr,
  223. const u32 *intspec, unsigned int intsize,
  224. irq_hw_number_t *out_hwirq,
  225. unsigned int *out_type)
  226. {
  227. struct irq_domain_chip_generic *dgc = d->gc;
  228. struct irq_chip_generic *gc;
  229. unsigned smr;
  230. int ret;
  231. if (!dgc)
  232. return -EINVAL;
  233. ret = aic_common_irq_domain_xlate(d, ctrlr, intspec, intsize,
  234. out_hwirq, out_type);
  235. if (ret)
  236. return ret;
  237. gc = dgc->gc[0];
  238. irq_gc_lock(gc);
  239. irq_reg_writel(gc, *out_hwirq, AT91_AIC5_SSR);
  240. smr = irq_reg_readl(gc, AT91_AIC5_SMR);
  241. ret = aic_common_set_priority(intspec[2], &smr);
  242. if (!ret)
  243. irq_reg_writel(gc, intspec[2] | smr, AT91_AIC5_SMR);
  244. irq_gc_unlock(gc);
  245. return ret;
  246. }
  247. static const struct irq_domain_ops aic5_irq_ops = {
  248. .map = irq_map_generic_chip,
  249. .xlate = aic5_irq_domain_xlate,
  250. };
  251. static void __init sama5d3_aic_irq_fixup(struct device_node *root)
  252. {
  253. aic_common_rtc_irq_fixup(root);
  254. }
  255. static const struct of_device_id aic5_irq_fixups[] __initconst = {
  256. { .compatible = "atmel,sama5d3", .data = sama5d3_aic_irq_fixup },
  257. { .compatible = "atmel,sama5d4", .data = sama5d3_aic_irq_fixup },
  258. { /* sentinel */ },
  259. };
  260. static int __init aic5_of_init(struct device_node *node,
  261. struct device_node *parent,
  262. int nirqs)
  263. {
  264. struct irq_chip_generic *gc;
  265. struct irq_domain *domain;
  266. int nchips;
  267. int i;
  268. if (nirqs > NR_AIC5_IRQS)
  269. return -EINVAL;
  270. if (aic5_domain)
  271. return -EEXIST;
  272. domain = aic_common_of_init(node, &aic5_irq_ops, "atmel-aic5",
  273. nirqs);
  274. if (IS_ERR(domain))
  275. return PTR_ERR(domain);
  276. aic_common_irq_fixup(aic5_irq_fixups);
  277. aic5_domain = domain;
  278. nchips = aic5_domain->revmap_size / 32;
  279. for (i = 0; i < nchips; i++) {
  280. gc = irq_get_domain_generic_chip(domain, i * 32);
  281. gc->chip_types[0].regs.eoi = AT91_AIC5_EOICR;
  282. gc->chip_types[0].chip.irq_mask = aic5_mask;
  283. gc->chip_types[0].chip.irq_unmask = aic5_unmask;
  284. gc->chip_types[0].chip.irq_retrigger = aic5_retrigger;
  285. gc->chip_types[0].chip.irq_set_type = aic5_set_type;
  286. gc->chip_types[0].chip.irq_suspend = aic5_suspend;
  287. gc->chip_types[0].chip.irq_resume = aic5_resume;
  288. gc->chip_types[0].chip.irq_pm_shutdown = aic5_pm_shutdown;
  289. }
  290. aic5_hw_init(domain);
  291. set_handle_irq(aic5_handle);
  292. return 0;
  293. }
  294. #define NR_SAMA5D2_IRQS 77
  295. static int __init sama5d2_aic5_of_init(struct device_node *node,
  296. struct device_node *parent)
  297. {
  298. return aic5_of_init(node, parent, NR_SAMA5D2_IRQS);
  299. }
  300. IRQCHIP_DECLARE(sama5d2_aic5, "atmel,sama5d2-aic", sama5d2_aic5_of_init);
  301. #define NR_SAMA5D3_IRQS 48
  302. static int __init sama5d3_aic5_of_init(struct device_node *node,
  303. struct device_node *parent)
  304. {
  305. return aic5_of_init(node, parent, NR_SAMA5D3_IRQS);
  306. }
  307. IRQCHIP_DECLARE(sama5d3_aic5, "atmel,sama5d3-aic", sama5d3_aic5_of_init);
  308. #define NR_SAMA5D4_IRQS 68
  309. static int __init sama5d4_aic5_of_init(struct device_node *node,
  310. struct device_node *parent)
  311. {
  312. return aic5_of_init(node, parent, NR_SAMA5D4_IRQS);
  313. }
  314. IRQCHIP_DECLARE(sama5d4_aic5, "atmel,sama5d4-aic", sama5d4_aic5_of_init);