irq-al-fic.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/irq.h>
  7. #include <linux/irqchip.h>
  8. #include <linux/irqchip/chained_irq.h>
  9. #include <linux/irqdomain.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. /* FIC Registers */
  15. #define AL_FIC_CAUSE 0x00
  16. #define AL_FIC_SET_CAUSE 0x08
  17. #define AL_FIC_MASK 0x10
  18. #define AL_FIC_CONTROL 0x28
  19. #define CONTROL_TRIGGER_RISING BIT(3)
  20. #define CONTROL_MASK_MSI_X BIT(5)
  21. #define NR_FIC_IRQS 32
  22. MODULE_AUTHOR("Talel Shenhar");
  23. MODULE_DESCRIPTION("Amazon's Annapurna Labs Interrupt Controller Driver");
  24. MODULE_LICENSE("GPL v2");
  25. enum al_fic_state {
  26. AL_FIC_UNCONFIGURED = 0,
  27. AL_FIC_CONFIGURED_LEVEL,
  28. AL_FIC_CONFIGURED_RISING_EDGE,
  29. };
  30. struct al_fic {
  31. void __iomem *base;
  32. struct irq_domain *domain;
  33. const char *name;
  34. unsigned int parent_irq;
  35. enum al_fic_state state;
  36. };
  37. static void al_fic_set_trigger(struct al_fic *fic,
  38. struct irq_chip_generic *gc,
  39. enum al_fic_state new_state)
  40. {
  41. irq_flow_handler_t handler;
  42. u32 control = readl_relaxed(fic->base + AL_FIC_CONTROL);
  43. if (new_state == AL_FIC_CONFIGURED_LEVEL) {
  44. handler = handle_level_irq;
  45. control &= ~CONTROL_TRIGGER_RISING;
  46. } else {
  47. handler = handle_edge_irq;
  48. control |= CONTROL_TRIGGER_RISING;
  49. }
  50. gc->chip_types->handler = handler;
  51. fic->state = new_state;
  52. writel_relaxed(control, fic->base + AL_FIC_CONTROL);
  53. }
  54. static int al_fic_irq_set_type(struct irq_data *data, unsigned int flow_type)
  55. {
  56. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
  57. struct al_fic *fic = gc->private;
  58. enum al_fic_state new_state;
  59. int ret = 0;
  60. irq_gc_lock(gc);
  61. if (((flow_type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_HIGH) &&
  62. ((flow_type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_EDGE_RISING)) {
  63. pr_debug("fic doesn't support flow type %d\n", flow_type);
  64. ret = -EINVAL;
  65. goto err;
  66. }
  67. new_state = (flow_type & IRQ_TYPE_LEVEL_HIGH) ?
  68. AL_FIC_CONFIGURED_LEVEL : AL_FIC_CONFIGURED_RISING_EDGE;
  69. /*
  70. * A given FIC instance can be either all level or all edge triggered.
  71. * This is generally fixed depending on what pieces of HW it's wired up
  72. * to.
  73. *
  74. * We configure it based on the sensitivity of the first source
  75. * being setup, and reject any subsequent attempt at configuring it in a
  76. * different way.
  77. */
  78. if (fic->state == AL_FIC_UNCONFIGURED) {
  79. al_fic_set_trigger(fic, gc, new_state);
  80. } else if (fic->state != new_state) {
  81. pr_debug("fic %s state already configured to %d\n",
  82. fic->name, fic->state);
  83. ret = -EINVAL;
  84. goto err;
  85. }
  86. err:
  87. irq_gc_unlock(gc);
  88. return ret;
  89. }
  90. static void al_fic_irq_handler(struct irq_desc *desc)
  91. {
  92. struct al_fic *fic = irq_desc_get_handler_data(desc);
  93. struct irq_domain *domain = fic->domain;
  94. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  95. struct irq_chip_generic *gc = irq_get_domain_generic_chip(domain, 0);
  96. unsigned long pending;
  97. u32 hwirq;
  98. chained_irq_enter(irqchip, desc);
  99. pending = readl_relaxed(fic->base + AL_FIC_CAUSE);
  100. pending &= ~gc->mask_cache;
  101. for_each_set_bit(hwirq, &pending, NR_FIC_IRQS)
  102. generic_handle_domain_irq(domain, hwirq);
  103. chained_irq_exit(irqchip, desc);
  104. }
  105. static int al_fic_irq_retrigger(struct irq_data *data)
  106. {
  107. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
  108. struct al_fic *fic = gc->private;
  109. writel_relaxed(BIT(data->hwirq), fic->base + AL_FIC_SET_CAUSE);
  110. return 1;
  111. }
  112. static int al_fic_register(struct device_node *node,
  113. struct al_fic *fic)
  114. {
  115. struct irq_chip_generic *gc;
  116. int ret;
  117. fic->domain = irq_domain_add_linear(node,
  118. NR_FIC_IRQS,
  119. &irq_generic_chip_ops,
  120. fic);
  121. if (!fic->domain) {
  122. pr_err("fail to add irq domain\n");
  123. return -ENOMEM;
  124. }
  125. ret = irq_alloc_domain_generic_chips(fic->domain,
  126. NR_FIC_IRQS,
  127. 1, fic->name,
  128. handle_level_irq,
  129. 0, 0, IRQ_GC_INIT_MASK_CACHE);
  130. if (ret) {
  131. pr_err("fail to allocate generic chip (%d)\n", ret);
  132. goto err_domain_remove;
  133. }
  134. gc = irq_get_domain_generic_chip(fic->domain, 0);
  135. gc->reg_base = fic->base;
  136. gc->chip_types->regs.mask = AL_FIC_MASK;
  137. gc->chip_types->regs.ack = AL_FIC_CAUSE;
  138. gc->chip_types->chip.irq_mask = irq_gc_mask_set_bit;
  139. gc->chip_types->chip.irq_unmask = irq_gc_mask_clr_bit;
  140. gc->chip_types->chip.irq_ack = irq_gc_ack_clr_bit;
  141. gc->chip_types->chip.irq_set_type = al_fic_irq_set_type;
  142. gc->chip_types->chip.irq_retrigger = al_fic_irq_retrigger;
  143. gc->chip_types->chip.flags = IRQCHIP_SKIP_SET_WAKE;
  144. gc->private = fic;
  145. irq_set_chained_handler_and_data(fic->parent_irq,
  146. al_fic_irq_handler,
  147. fic);
  148. return 0;
  149. err_domain_remove:
  150. irq_domain_remove(fic->domain);
  151. return ret;
  152. }
  153. /*
  154. * al_fic_wire_init() - initialize and configure fic in wire mode
  155. * @of_node: optional pointer to interrupt controller's device tree node.
  156. * @base: mmio to fic register
  157. * @name: name of the fic
  158. * @parent_irq: interrupt of parent
  159. *
  160. * This API will configure the fic hardware to to work in wire mode.
  161. * In wire mode, fic hardware is generating a wire ("wired") interrupt.
  162. * Interrupt can be generated based on positive edge or level - configuration is
  163. * to be determined based on connected hardware to this fic.
  164. */
  165. static struct al_fic *al_fic_wire_init(struct device_node *node,
  166. void __iomem *base,
  167. const char *name,
  168. unsigned int parent_irq)
  169. {
  170. struct al_fic *fic;
  171. int ret;
  172. u32 control = CONTROL_MASK_MSI_X;
  173. fic = kzalloc(sizeof(*fic), GFP_KERNEL);
  174. if (!fic)
  175. return ERR_PTR(-ENOMEM);
  176. fic->base = base;
  177. fic->parent_irq = parent_irq;
  178. fic->name = name;
  179. /* mask out all interrupts */
  180. writel_relaxed(0xFFFFFFFF, fic->base + AL_FIC_MASK);
  181. /* clear any pending interrupt */
  182. writel_relaxed(0, fic->base + AL_FIC_CAUSE);
  183. writel_relaxed(control, fic->base + AL_FIC_CONTROL);
  184. ret = al_fic_register(node, fic);
  185. if (ret) {
  186. pr_err("fail to register irqchip\n");
  187. goto err_free;
  188. }
  189. pr_debug("%s initialized successfully in Legacy mode (parent-irq=%u)\n",
  190. fic->name, parent_irq);
  191. return fic;
  192. err_free:
  193. kfree(fic);
  194. return ERR_PTR(ret);
  195. }
  196. static int __init al_fic_init_dt(struct device_node *node,
  197. struct device_node *parent)
  198. {
  199. int ret;
  200. void __iomem *base;
  201. unsigned int parent_irq;
  202. struct al_fic *fic;
  203. if (!parent) {
  204. pr_err("%s: unsupported - device require a parent\n",
  205. node->name);
  206. return -EINVAL;
  207. }
  208. base = of_iomap(node, 0);
  209. if (!base) {
  210. pr_err("%s: fail to map memory\n", node->name);
  211. return -ENOMEM;
  212. }
  213. parent_irq = irq_of_parse_and_map(node, 0);
  214. if (!parent_irq) {
  215. pr_err("%s: fail to map irq\n", node->name);
  216. ret = -EINVAL;
  217. goto err_unmap;
  218. }
  219. fic = al_fic_wire_init(node,
  220. base,
  221. node->name,
  222. parent_irq);
  223. if (IS_ERR(fic)) {
  224. pr_err("%s: fail to initialize irqchip (%lu)\n",
  225. node->name,
  226. PTR_ERR(fic));
  227. ret = PTR_ERR(fic);
  228. goto err_irq_dispose;
  229. }
  230. return 0;
  231. err_irq_dispose:
  232. irq_dispose_mapping(parent_irq);
  233. err_unmap:
  234. iounmap(base);
  235. return ret;
  236. }
  237. IRQCHIP_DECLARE(al_fic, "amazon,al-fic", al_fic_init_dt);