irq-atmel-aic5.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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_chip_generic *bgc = irq_get_domain_generic_chip(aic5_domain, 0);
  62. u32 irqnr;
  63. u32 irqstat;
  64. irqnr = irq_reg_readl(bgc, AT91_AIC5_IVR);
  65. irqstat = irq_reg_readl(bgc, AT91_AIC5_ISR);
  66. if (!irqstat)
  67. irq_reg_writel(bgc, 0, AT91_AIC5_EOICR);
  68. else
  69. handle_domain_irq(aic5_domain, irqnr, regs);
  70. }
  71. static void aic5_mask(struct irq_data *d)
  72. {
  73. struct irq_domain *domain = d->domain;
  74. struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
  75. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  76. /*
  77. * Disable interrupt on AIC5. We always take the lock of the
  78. * first irq chip as all chips share the same registers.
  79. */
  80. irq_gc_lock(bgc);
  81. irq_reg_writel(gc, d->hwirq, AT91_AIC5_SSR);
  82. irq_reg_writel(gc, 1, AT91_AIC5_IDCR);
  83. gc->mask_cache &= ~d->mask;
  84. irq_gc_unlock(bgc);
  85. }
  86. static void aic5_unmask(struct irq_data *d)
  87. {
  88. struct irq_domain *domain = d->domain;
  89. struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
  90. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  91. /*
  92. * Enable interrupt on AIC5. We always take the lock of the
  93. * first irq chip as all chips share the same registers.
  94. */
  95. irq_gc_lock(bgc);
  96. irq_reg_writel(gc, d->hwirq, AT91_AIC5_SSR);
  97. irq_reg_writel(gc, 1, AT91_AIC5_IECR);
  98. gc->mask_cache |= d->mask;
  99. irq_gc_unlock(bgc);
  100. }
  101. static int aic5_retrigger(struct irq_data *d)
  102. {
  103. struct irq_domain *domain = d->domain;
  104. struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
  105. /* Enable interrupt on AIC5 */
  106. irq_gc_lock(bgc);
  107. irq_reg_writel(bgc, d->hwirq, AT91_AIC5_SSR);
  108. irq_reg_writel(bgc, 1, AT91_AIC5_ISCR);
  109. irq_gc_unlock(bgc);
  110. return 0;
  111. }
  112. static int aic5_set_type(struct irq_data *d, unsigned type)
  113. {
  114. struct irq_domain *domain = d->domain;
  115. struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
  116. unsigned int smr;
  117. int ret;
  118. irq_gc_lock(bgc);
  119. irq_reg_writel(bgc, d->hwirq, AT91_AIC5_SSR);
  120. smr = irq_reg_readl(bgc, AT91_AIC5_SMR);
  121. ret = aic_common_set_type(d, type, &smr);
  122. if (!ret)
  123. irq_reg_writel(bgc, smr, AT91_AIC5_SMR);
  124. irq_gc_unlock(bgc);
  125. return ret;
  126. }
  127. #ifdef CONFIG_PM
  128. static void aic5_suspend(struct irq_data *d)
  129. {
  130. struct irq_domain *domain = d->domain;
  131. struct irq_domain_chip_generic *dgc = domain->gc;
  132. struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
  133. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  134. int i;
  135. u32 mask;
  136. irq_gc_lock(bgc);
  137. for (i = 0; i < dgc->irqs_per_chip; i++) {
  138. mask = 1 << i;
  139. if ((mask & gc->mask_cache) == (mask & gc->wake_active))
  140. continue;
  141. irq_reg_writel(bgc, i + gc->irq_base, AT91_AIC5_SSR);
  142. if (mask & gc->wake_active)
  143. irq_reg_writel(bgc, 1, AT91_AIC5_IECR);
  144. else
  145. irq_reg_writel(bgc, 1, AT91_AIC5_IDCR);
  146. }
  147. irq_gc_unlock(bgc);
  148. }
  149. static void aic5_resume(struct irq_data *d)
  150. {
  151. struct irq_domain *domain = d->domain;
  152. struct irq_domain_chip_generic *dgc = domain->gc;
  153. struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
  154. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  155. int i;
  156. u32 mask;
  157. irq_gc_lock(bgc);
  158. for (i = 0; i < dgc->irqs_per_chip; i++) {
  159. mask = 1 << i;
  160. if ((mask & gc->mask_cache) == (mask & gc->wake_active))
  161. continue;
  162. irq_reg_writel(bgc, i + gc->irq_base, AT91_AIC5_SSR);
  163. if (mask & gc->mask_cache)
  164. irq_reg_writel(bgc, 1, AT91_AIC5_IECR);
  165. else
  166. irq_reg_writel(bgc, 1, AT91_AIC5_IDCR);
  167. }
  168. irq_gc_unlock(bgc);
  169. }
  170. static void aic5_pm_shutdown(struct irq_data *d)
  171. {
  172. struct irq_domain *domain = d->domain;
  173. struct irq_domain_chip_generic *dgc = domain->gc;
  174. struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
  175. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  176. int i;
  177. irq_gc_lock(bgc);
  178. for (i = 0; i < dgc->irqs_per_chip; i++) {
  179. irq_reg_writel(bgc, i + gc->irq_base, AT91_AIC5_SSR);
  180. irq_reg_writel(bgc, 1, AT91_AIC5_IDCR);
  181. irq_reg_writel(bgc, 1, AT91_AIC5_ICCR);
  182. }
  183. irq_gc_unlock(bgc);
  184. }
  185. #else
  186. #define aic5_suspend NULL
  187. #define aic5_resume NULL
  188. #define aic5_pm_shutdown NULL
  189. #endif /* CONFIG_PM */
  190. static void __init aic5_hw_init(struct irq_domain *domain)
  191. {
  192. struct irq_chip_generic *gc = irq_get_domain_generic_chip(domain, 0);
  193. int i;
  194. /*
  195. * Perform 8 End Of Interrupt Command to make sure AIC
  196. * will not Lock out nIRQ
  197. */
  198. for (i = 0; i < 8; i++)
  199. irq_reg_writel(gc, 0, AT91_AIC5_EOICR);
  200. /*
  201. * Spurious Interrupt ID in Spurious Vector Register.
  202. * When there is no current interrupt, the IRQ Vector Register
  203. * reads the value stored in AIC_SPU
  204. */
  205. irq_reg_writel(gc, 0xffffffff, AT91_AIC5_SPU);
  206. /* No debugging in AIC: Debug (Protect) Control Register */
  207. irq_reg_writel(gc, 0, AT91_AIC5_DCR);
  208. /* Disable and clear all interrupts initially */
  209. for (i = 0; i < domain->revmap_size; i++) {
  210. irq_reg_writel(gc, i, AT91_AIC5_SSR);
  211. irq_reg_writel(gc, i, AT91_AIC5_SVR);
  212. irq_reg_writel(gc, 1, AT91_AIC5_IDCR);
  213. irq_reg_writel(gc, 1, AT91_AIC5_ICCR);
  214. }
  215. }
  216. static int aic5_irq_domain_xlate(struct irq_domain *d,
  217. struct device_node *ctrlr,
  218. const u32 *intspec, unsigned int intsize,
  219. irq_hw_number_t *out_hwirq,
  220. unsigned int *out_type)
  221. {
  222. struct irq_chip_generic *bgc = irq_get_domain_generic_chip(d, 0);
  223. unsigned long flags;
  224. unsigned smr;
  225. int ret;
  226. if (!bgc)
  227. return -EINVAL;
  228. ret = aic_common_irq_domain_xlate(d, ctrlr, intspec, intsize,
  229. out_hwirq, out_type);
  230. if (ret)
  231. return ret;
  232. irq_gc_lock_irqsave(bgc, flags);
  233. irq_reg_writel(bgc, *out_hwirq, AT91_AIC5_SSR);
  234. smr = irq_reg_readl(bgc, AT91_AIC5_SMR);
  235. ret = aic_common_set_priority(intspec[2], &smr);
  236. if (!ret)
  237. irq_reg_writel(bgc, intspec[2] | smr, AT91_AIC5_SMR);
  238. irq_gc_unlock_irqrestore(bgc, flags);
  239. return ret;
  240. }
  241. static const struct irq_domain_ops aic5_irq_ops = {
  242. .map = irq_map_generic_chip,
  243. .xlate = aic5_irq_domain_xlate,
  244. };
  245. static void __init sama5d3_aic_irq_fixup(struct device_node *root)
  246. {
  247. aic_common_rtc_irq_fixup(root);
  248. }
  249. static const struct of_device_id aic5_irq_fixups[] __initconst = {
  250. { .compatible = "atmel,sama5d3", .data = sama5d3_aic_irq_fixup },
  251. { .compatible = "atmel,sama5d4", .data = sama5d3_aic_irq_fixup },
  252. { /* sentinel */ },
  253. };
  254. static int __init aic5_of_init(struct device_node *node,
  255. struct device_node *parent,
  256. int nirqs)
  257. {
  258. struct irq_chip_generic *gc;
  259. struct irq_domain *domain;
  260. int nchips;
  261. int i;
  262. if (nirqs > NR_AIC5_IRQS)
  263. return -EINVAL;
  264. if (aic5_domain)
  265. return -EEXIST;
  266. domain = aic_common_of_init(node, &aic5_irq_ops, "atmel-aic5",
  267. nirqs);
  268. if (IS_ERR(domain))
  269. return PTR_ERR(domain);
  270. aic_common_irq_fixup(aic5_irq_fixups);
  271. aic5_domain = domain;
  272. nchips = aic5_domain->revmap_size / 32;
  273. for (i = 0; i < nchips; i++) {
  274. gc = irq_get_domain_generic_chip(domain, i * 32);
  275. gc->chip_types[0].regs.eoi = AT91_AIC5_EOICR;
  276. gc->chip_types[0].chip.irq_mask = aic5_mask;
  277. gc->chip_types[0].chip.irq_unmask = aic5_unmask;
  278. gc->chip_types[0].chip.irq_retrigger = aic5_retrigger;
  279. gc->chip_types[0].chip.irq_set_type = aic5_set_type;
  280. gc->chip_types[0].chip.irq_suspend = aic5_suspend;
  281. gc->chip_types[0].chip.irq_resume = aic5_resume;
  282. gc->chip_types[0].chip.irq_pm_shutdown = aic5_pm_shutdown;
  283. }
  284. aic5_hw_init(domain);
  285. set_handle_irq(aic5_handle);
  286. return 0;
  287. }
  288. #define NR_SAMA5D2_IRQS 77
  289. static int __init sama5d2_aic5_of_init(struct device_node *node,
  290. struct device_node *parent)
  291. {
  292. return aic5_of_init(node, parent, NR_SAMA5D2_IRQS);
  293. }
  294. IRQCHIP_DECLARE(sama5d2_aic5, "atmel,sama5d2-aic", sama5d2_aic5_of_init);
  295. #define NR_SAMA5D3_IRQS 48
  296. static int __init sama5d3_aic5_of_init(struct device_node *node,
  297. struct device_node *parent)
  298. {
  299. return aic5_of_init(node, parent, NR_SAMA5D3_IRQS);
  300. }
  301. IRQCHIP_DECLARE(sama5d3_aic5, "atmel,sama5d3-aic", sama5d3_aic5_of_init);
  302. #define NR_SAMA5D4_IRQS 68
  303. static int __init sama5d4_aic5_of_init(struct device_node *node,
  304. struct device_node *parent)
  305. {
  306. return aic5_of_init(node, parent, NR_SAMA5D4_IRQS);
  307. }
  308. IRQCHIP_DECLARE(sama5d4_aic5, "atmel,sama5d4-aic", sama5d4_aic5_of_init);