irq-xilinx-intc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2007-2013 Michal Simek <[email protected]>
  3. * Copyright (C) 2012-2013 Xilinx, Inc.
  4. * Copyright (C) 2007-2009 PetaLogix
  5. * Copyright (C) 2006 Atmark Techno, Inc.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/irqdomain.h>
  12. #include <linux/irq.h>
  13. #include <linux/irqchip.h>
  14. #include <linux/irqchip/chained_irq.h>
  15. #include <linux/of_address.h>
  16. #include <linux/io.h>
  17. #include <linux/jump_label.h>
  18. #include <linux/bug.h>
  19. #include <linux/of_irq.h>
  20. /* No one else should require these constants, so define them locally here. */
  21. #define ISR 0x00 /* Interrupt Status Register */
  22. #define IPR 0x04 /* Interrupt Pending Register */
  23. #define IER 0x08 /* Interrupt Enable Register */
  24. #define IAR 0x0c /* Interrupt Acknowledge Register */
  25. #define SIE 0x10 /* Set Interrupt Enable bits */
  26. #define CIE 0x14 /* Clear Interrupt Enable bits */
  27. #define IVR 0x18 /* Interrupt Vector Register */
  28. #define MER 0x1c /* Master Enable Register */
  29. #define MER_ME (1<<0)
  30. #define MER_HIE (1<<1)
  31. #define SPURIOUS_IRQ (-1U)
  32. static DEFINE_STATIC_KEY_FALSE(xintc_is_be);
  33. struct xintc_irq_chip {
  34. void __iomem *base;
  35. struct irq_domain *root_domain;
  36. u32 intr_mask;
  37. u32 nr_irq;
  38. };
  39. static struct xintc_irq_chip *primary_intc;
  40. static void xintc_write(struct xintc_irq_chip *irqc, int reg, u32 data)
  41. {
  42. if (static_branch_unlikely(&xintc_is_be))
  43. iowrite32be(data, irqc->base + reg);
  44. else
  45. iowrite32(data, irqc->base + reg);
  46. }
  47. static u32 xintc_read(struct xintc_irq_chip *irqc, int reg)
  48. {
  49. if (static_branch_unlikely(&xintc_is_be))
  50. return ioread32be(irqc->base + reg);
  51. else
  52. return ioread32(irqc->base + reg);
  53. }
  54. static void intc_enable_or_unmask(struct irq_data *d)
  55. {
  56. struct xintc_irq_chip *irqc = irq_data_get_irq_chip_data(d);
  57. unsigned long mask = BIT(d->hwirq);
  58. pr_debug("irq-xilinx: enable_or_unmask: %ld\n", d->hwirq);
  59. /* ack level irqs because they can't be acked during
  60. * ack function since the handle_level_irq function
  61. * acks the irq before calling the interrupt handler
  62. */
  63. if (irqd_is_level_type(d))
  64. xintc_write(irqc, IAR, mask);
  65. xintc_write(irqc, SIE, mask);
  66. }
  67. static void intc_disable_or_mask(struct irq_data *d)
  68. {
  69. struct xintc_irq_chip *irqc = irq_data_get_irq_chip_data(d);
  70. pr_debug("irq-xilinx: disable: %ld\n", d->hwirq);
  71. xintc_write(irqc, CIE, BIT(d->hwirq));
  72. }
  73. static void intc_ack(struct irq_data *d)
  74. {
  75. struct xintc_irq_chip *irqc = irq_data_get_irq_chip_data(d);
  76. pr_debug("irq-xilinx: ack: %ld\n", d->hwirq);
  77. xintc_write(irqc, IAR, BIT(d->hwirq));
  78. }
  79. static void intc_mask_ack(struct irq_data *d)
  80. {
  81. struct xintc_irq_chip *irqc = irq_data_get_irq_chip_data(d);
  82. unsigned long mask = BIT(d->hwirq);
  83. pr_debug("irq-xilinx: disable_and_ack: %ld\n", d->hwirq);
  84. xintc_write(irqc, CIE, mask);
  85. xintc_write(irqc, IAR, mask);
  86. }
  87. static struct irq_chip intc_dev = {
  88. .name = "Xilinx INTC",
  89. .irq_unmask = intc_enable_or_unmask,
  90. .irq_mask = intc_disable_or_mask,
  91. .irq_ack = intc_ack,
  92. .irq_mask_ack = intc_mask_ack,
  93. };
  94. static int xintc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
  95. {
  96. struct xintc_irq_chip *irqc = d->host_data;
  97. if (irqc->intr_mask & BIT(hw)) {
  98. irq_set_chip_and_handler_name(irq, &intc_dev,
  99. handle_edge_irq, "edge");
  100. irq_clear_status_flags(irq, IRQ_LEVEL);
  101. } else {
  102. irq_set_chip_and_handler_name(irq, &intc_dev,
  103. handle_level_irq, "level");
  104. irq_set_status_flags(irq, IRQ_LEVEL);
  105. }
  106. irq_set_chip_data(irq, irqc);
  107. return 0;
  108. }
  109. static const struct irq_domain_ops xintc_irq_domain_ops = {
  110. .xlate = irq_domain_xlate_onetwocell,
  111. .map = xintc_map,
  112. };
  113. static void xil_intc_irq_handler(struct irq_desc *desc)
  114. {
  115. struct irq_chip *chip = irq_desc_get_chip(desc);
  116. struct xintc_irq_chip *irqc;
  117. irqc = irq_data_get_irq_handler_data(&desc->irq_data);
  118. chained_irq_enter(chip, desc);
  119. do {
  120. u32 hwirq = xintc_read(irqc, IVR);
  121. if (hwirq == -1U)
  122. break;
  123. generic_handle_domain_irq(irqc->root_domain, hwirq);
  124. } while (true);
  125. chained_irq_exit(chip, desc);
  126. }
  127. static void xil_intc_handle_irq(struct pt_regs *regs)
  128. {
  129. u32 hwirq;
  130. do {
  131. hwirq = xintc_read(primary_intc, IVR);
  132. if (unlikely(hwirq == SPURIOUS_IRQ))
  133. break;
  134. generic_handle_domain_irq(primary_intc->root_domain, hwirq);
  135. } while (true);
  136. }
  137. static int __init xilinx_intc_of_init(struct device_node *intc,
  138. struct device_node *parent)
  139. {
  140. struct xintc_irq_chip *irqc;
  141. int ret, irq;
  142. irqc = kzalloc(sizeof(*irqc), GFP_KERNEL);
  143. if (!irqc)
  144. return -ENOMEM;
  145. irqc->base = of_iomap(intc, 0);
  146. BUG_ON(!irqc->base);
  147. ret = of_property_read_u32(intc, "xlnx,num-intr-inputs", &irqc->nr_irq);
  148. if (ret < 0) {
  149. pr_err("irq-xilinx: unable to read xlnx,num-intr-inputs\n");
  150. goto error;
  151. }
  152. ret = of_property_read_u32(intc, "xlnx,kind-of-intr", &irqc->intr_mask);
  153. if (ret < 0) {
  154. pr_warn("irq-xilinx: unable to read xlnx,kind-of-intr\n");
  155. irqc->intr_mask = 0;
  156. }
  157. if (irqc->intr_mask >> irqc->nr_irq)
  158. pr_warn("irq-xilinx: mismatch in kind-of-intr param\n");
  159. pr_info("irq-xilinx: %pOF: num_irq=%d, edge=0x%x\n",
  160. intc, irqc->nr_irq, irqc->intr_mask);
  161. /*
  162. * Disable all external interrupts until they are
  163. * explicitly requested.
  164. */
  165. xintc_write(irqc, IER, 0);
  166. /* Acknowledge any pending interrupts just in case. */
  167. xintc_write(irqc, IAR, 0xffffffff);
  168. /* Turn on the Master Enable. */
  169. xintc_write(irqc, MER, MER_HIE | MER_ME);
  170. if (xintc_read(irqc, MER) != (MER_HIE | MER_ME)) {
  171. static_branch_enable(&xintc_is_be);
  172. xintc_write(irqc, MER, MER_HIE | MER_ME);
  173. }
  174. irqc->root_domain = irq_domain_add_linear(intc, irqc->nr_irq,
  175. &xintc_irq_domain_ops, irqc);
  176. if (!irqc->root_domain) {
  177. pr_err("irq-xilinx: Unable to create IRQ domain\n");
  178. ret = -EINVAL;
  179. goto error;
  180. }
  181. if (parent) {
  182. irq = irq_of_parse_and_map(intc, 0);
  183. if (irq) {
  184. irq_set_chained_handler_and_data(irq,
  185. xil_intc_irq_handler,
  186. irqc);
  187. } else {
  188. pr_err("irq-xilinx: interrupts property not in DT\n");
  189. ret = -EINVAL;
  190. goto error;
  191. }
  192. } else {
  193. primary_intc = irqc;
  194. irq_set_default_host(primary_intc->root_domain);
  195. set_handle_irq(xil_intc_handle_irq);
  196. }
  197. return 0;
  198. error:
  199. iounmap(irqc->base);
  200. kfree(irqc);
  201. return ret;
  202. }
  203. IRQCHIP_DECLARE(xilinx_intc_xps, "xlnx,xps-intc-1.00.a", xilinx_intc_of_init);
  204. IRQCHIP_DECLARE(xilinx_intc_opb, "xlnx,opb-intc-1.00.c", xilinx_intc_of_init);