irq-atmel-aic-common.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Atmel AT91 common AIC (Advanced Interrupt Controller) code shared by
  3. * irq-atmel-aic and irq-atmel-aic5 drivers
  4. *
  5. * Copyright (C) 2004 SAN People
  6. * Copyright (C) 2004 ATMEL
  7. * Copyright (C) Rick Bronson
  8. * Copyright (C) 2014 Free Electrons
  9. *
  10. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  11. *
  12. * This file is licensed under the terms of the GNU General Public
  13. * License version 2. This program is licensed "as is" without any
  14. * warranty of any kind, whether express or implied.
  15. */
  16. #include <linux/errno.h>
  17. #include <linux/io.h>
  18. #include <linux/irq.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/slab.h>
  23. #include "irq-atmel-aic-common.h"
  24. #define AT91_AIC_PRIOR GENMASK(2, 0)
  25. #define AT91_AIC_IRQ_MIN_PRIORITY 0
  26. #define AT91_AIC_IRQ_MAX_PRIORITY 7
  27. #define AT91_AIC_SRCTYPE GENMASK(6, 5)
  28. #define AT91_AIC_SRCTYPE_LOW (0 << 5)
  29. #define AT91_AIC_SRCTYPE_FALLING (1 << 5)
  30. #define AT91_AIC_SRCTYPE_HIGH (2 << 5)
  31. #define AT91_AIC_SRCTYPE_RISING (3 << 5)
  32. struct aic_chip_data {
  33. u32 ext_irqs;
  34. };
  35. static void aic_common_shutdown(struct irq_data *d)
  36. {
  37. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  38. ct->chip.irq_mask(d);
  39. }
  40. int aic_common_set_type(struct irq_data *d, unsigned type, unsigned *val)
  41. {
  42. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  43. struct aic_chip_data *aic = gc->private;
  44. unsigned aic_type;
  45. switch (type) {
  46. case IRQ_TYPE_LEVEL_HIGH:
  47. aic_type = AT91_AIC_SRCTYPE_HIGH;
  48. break;
  49. case IRQ_TYPE_EDGE_RISING:
  50. aic_type = AT91_AIC_SRCTYPE_RISING;
  51. break;
  52. case IRQ_TYPE_LEVEL_LOW:
  53. if (!(d->mask & aic->ext_irqs))
  54. return -EINVAL;
  55. aic_type = AT91_AIC_SRCTYPE_LOW;
  56. break;
  57. case IRQ_TYPE_EDGE_FALLING:
  58. if (!(d->mask & aic->ext_irqs))
  59. return -EINVAL;
  60. aic_type = AT91_AIC_SRCTYPE_FALLING;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. *val &= ~AT91_AIC_SRCTYPE;
  66. *val |= aic_type;
  67. return 0;
  68. }
  69. int aic_common_set_priority(int priority, unsigned *val)
  70. {
  71. if (priority < AT91_AIC_IRQ_MIN_PRIORITY ||
  72. priority > AT91_AIC_IRQ_MAX_PRIORITY)
  73. return -EINVAL;
  74. *val &= ~AT91_AIC_PRIOR;
  75. *val |= priority;
  76. return 0;
  77. }
  78. int aic_common_irq_domain_xlate(struct irq_domain *d,
  79. struct device_node *ctrlr,
  80. const u32 *intspec,
  81. unsigned int intsize,
  82. irq_hw_number_t *out_hwirq,
  83. unsigned int *out_type)
  84. {
  85. if (WARN_ON(intsize < 3))
  86. return -EINVAL;
  87. if (WARN_ON((intspec[2] < AT91_AIC_IRQ_MIN_PRIORITY) ||
  88. (intspec[2] > AT91_AIC_IRQ_MAX_PRIORITY)))
  89. return -EINVAL;
  90. *out_hwirq = intspec[0];
  91. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  92. return 0;
  93. }
  94. static void __init aic_common_ext_irq_of_init(struct irq_domain *domain)
  95. {
  96. struct device_node *node = irq_domain_get_of_node(domain);
  97. struct irq_chip_generic *gc;
  98. struct aic_chip_data *aic;
  99. struct property *prop;
  100. const __be32 *p;
  101. u32 hwirq;
  102. gc = irq_get_domain_generic_chip(domain, 0);
  103. aic = gc->private;
  104. aic->ext_irqs |= 1;
  105. of_property_for_each_u32(node, "atmel,external-irqs", prop, p, hwirq) {
  106. gc = irq_get_domain_generic_chip(domain, hwirq);
  107. if (!gc) {
  108. pr_warn("AIC: external irq %d >= %d skip it\n",
  109. hwirq, domain->revmap_size);
  110. continue;
  111. }
  112. aic = gc->private;
  113. aic->ext_irqs |= (1 << (hwirq % 32));
  114. }
  115. }
  116. #define AT91_RTC_IDR 0x24
  117. #define AT91_RTC_IMR 0x28
  118. #define AT91_RTC_IRQ_MASK 0x1f
  119. void __init aic_common_rtc_irq_fixup(struct device_node *root)
  120. {
  121. struct device_node *np;
  122. void __iomem *regs;
  123. np = of_find_compatible_node(NULL, NULL, "atmel,at91rm9200-rtc");
  124. if (!np)
  125. np = of_find_compatible_node(NULL, NULL,
  126. "atmel,at91sam9x5-rtc");
  127. if (!np)
  128. return;
  129. regs = of_iomap(np, 0);
  130. of_node_put(np);
  131. if (!regs)
  132. return;
  133. writel(AT91_RTC_IRQ_MASK, regs + AT91_RTC_IDR);
  134. iounmap(regs);
  135. }
  136. #define AT91_RTT_MR 0x00 /* Real-time Mode Register */
  137. #define AT91_RTT_ALMIEN (1 << 16) /* Alarm Interrupt Enable */
  138. #define AT91_RTT_RTTINCIEN (1 << 17) /* Real Time Timer Increment Interrupt Enable */
  139. void __init aic_common_rtt_irq_fixup(struct device_node *root)
  140. {
  141. struct device_node *np;
  142. void __iomem *regs;
  143. /*
  144. * The at91sam9263 SoC has 2 instances of the RTT block, hence we
  145. * iterate over the DT to find each occurrence.
  146. */
  147. for_each_compatible_node(np, NULL, "atmel,at91sam9260-rtt") {
  148. regs = of_iomap(np, 0);
  149. if (!regs)
  150. continue;
  151. writel(readl(regs + AT91_RTT_MR) &
  152. ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN),
  153. regs + AT91_RTT_MR);
  154. iounmap(regs);
  155. }
  156. }
  157. void __init aic_common_irq_fixup(const struct of_device_id *matches)
  158. {
  159. struct device_node *root = of_find_node_by_path("/");
  160. const struct of_device_id *match;
  161. if (!root)
  162. return;
  163. match = of_match_node(matches, root);
  164. if (match) {
  165. void (*fixup)(struct device_node *) = match->data;
  166. fixup(root);
  167. }
  168. of_node_put(root);
  169. }
  170. struct irq_domain *__init aic_common_of_init(struct device_node *node,
  171. const struct irq_domain_ops *ops,
  172. const char *name, int nirqs)
  173. {
  174. struct irq_chip_generic *gc;
  175. struct irq_domain *domain;
  176. struct aic_chip_data *aic;
  177. void __iomem *reg_base;
  178. int nchips;
  179. int ret;
  180. int i;
  181. nchips = DIV_ROUND_UP(nirqs, 32);
  182. reg_base = of_iomap(node, 0);
  183. if (!reg_base)
  184. return ERR_PTR(-ENOMEM);
  185. aic = kcalloc(nchips, sizeof(*aic), GFP_KERNEL);
  186. if (!aic) {
  187. ret = -ENOMEM;
  188. goto err_iounmap;
  189. }
  190. domain = irq_domain_add_linear(node, nchips * 32, ops, aic);
  191. if (!domain) {
  192. ret = -ENOMEM;
  193. goto err_free_aic;
  194. }
  195. ret = irq_alloc_domain_generic_chips(domain, 32, 1, name,
  196. handle_fasteoi_irq,
  197. IRQ_NOREQUEST | IRQ_NOPROBE |
  198. IRQ_NOAUTOEN, 0, 0);
  199. if (ret)
  200. goto err_domain_remove;
  201. for (i = 0; i < nchips; i++) {
  202. gc = irq_get_domain_generic_chip(domain, i * 32);
  203. gc->reg_base = reg_base;
  204. gc->unused = 0;
  205. gc->wake_enabled = ~0;
  206. gc->chip_types[0].type = IRQ_TYPE_SENSE_MASK;
  207. gc->chip_types[0].chip.irq_eoi = irq_gc_eoi;
  208. gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
  209. gc->chip_types[0].chip.irq_shutdown = aic_common_shutdown;
  210. gc->private = &aic[i];
  211. }
  212. aic_common_ext_irq_of_init(domain);
  213. return domain;
  214. err_domain_remove:
  215. irq_domain_remove(domain);
  216. err_free_aic:
  217. kfree(aic);
  218. err_iounmap:
  219. iounmap(reg_base);
  220. return ERR_PTR(ret);
  221. }