irq-atmel-aic-common.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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(7, 6)
  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 = domain->of_node;
  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(root, NULL, "atmel,at91rm9200-rtc");
  124. if (!np)
  125. np = of_find_compatible_node(root, 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. void __init aic_common_irq_fixup(const struct of_device_id *matches)
  137. {
  138. struct device_node *root = of_find_node_by_path("/");
  139. const struct of_device_id *match;
  140. if (!root)
  141. return;
  142. match = of_match_node(matches, root);
  143. of_node_put(root);
  144. if (match) {
  145. void (*fixup)(struct device_node *) = match->data;
  146. fixup(root);
  147. }
  148. of_node_put(root);
  149. }
  150. struct irq_domain *__init aic_common_of_init(struct device_node *node,
  151. const struct irq_domain_ops *ops,
  152. const char *name, int nirqs)
  153. {
  154. struct irq_chip_generic *gc;
  155. struct irq_domain *domain;
  156. struct aic_chip_data *aic;
  157. void __iomem *reg_base;
  158. int nchips;
  159. int ret;
  160. int i;
  161. nchips = DIV_ROUND_UP(nirqs, 32);
  162. reg_base = of_iomap(node, 0);
  163. if (!reg_base)
  164. return ERR_PTR(-ENOMEM);
  165. aic = kcalloc(nchips, sizeof(*aic), GFP_KERNEL);
  166. if (!aic) {
  167. ret = -ENOMEM;
  168. goto err_iounmap;
  169. }
  170. domain = irq_domain_add_linear(node, nchips * 32, ops, aic);
  171. if (!domain) {
  172. ret = -ENOMEM;
  173. goto err_free_aic;
  174. }
  175. ret = irq_alloc_domain_generic_chips(domain, 32, 1, name,
  176. handle_level_irq, 0, 0,
  177. IRQCHIP_SKIP_SET_WAKE);
  178. if (ret)
  179. goto err_domain_remove;
  180. for (i = 0; i < nchips; i++) {
  181. gc = irq_get_domain_generic_chip(domain, i * 32);
  182. gc->reg_base = reg_base;
  183. gc->unused = 0;
  184. gc->wake_enabled = ~0;
  185. gc->chip_types[0].type = IRQ_TYPE_SENSE_MASK;
  186. gc->chip_types[0].handler = handle_fasteoi_irq;
  187. gc->chip_types[0].chip.irq_eoi = irq_gc_eoi;
  188. gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
  189. gc->chip_types[0].chip.irq_shutdown = aic_common_shutdown;
  190. gc->private = &aic[i];
  191. }
  192. aic_common_ext_irq_of_init(domain);
  193. return domain;
  194. err_domain_remove:
  195. irq_domain_remove(domain);
  196. err_free_aic:
  197. kfree(aic);
  198. err_iounmap:
  199. iounmap(reg_base);
  200. return ERR_PTR(ret);
  201. }