irq-versatile-fpga.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Support for Versatile FPGA-based IRQ controllers
  4. */
  5. #include <linux/bitops.h>
  6. #include <linux/irq.h>
  7. #include <linux/io.h>
  8. #include <linux/irqchip.h>
  9. #include <linux/irqchip/chained_irq.h>
  10. #include <linux/irqdomain.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/seq_file.h>
  16. #include <asm/exception.h>
  17. #include <asm/mach/irq.h>
  18. #define IRQ_STATUS 0x00
  19. #define IRQ_RAW_STATUS 0x04
  20. #define IRQ_ENABLE_SET 0x08
  21. #define IRQ_ENABLE_CLEAR 0x0c
  22. #define INT_SOFT_SET 0x10
  23. #define INT_SOFT_CLEAR 0x14
  24. #define FIQ_STATUS 0x20
  25. #define FIQ_RAW_STATUS 0x24
  26. #define FIQ_ENABLE 0x28
  27. #define FIQ_ENABLE_SET 0x28
  28. #define FIQ_ENABLE_CLEAR 0x2C
  29. #define PIC_ENABLES 0x20 /* set interrupt pass through bits */
  30. /**
  31. * struct fpga_irq_data - irq data container for the FPGA IRQ controller
  32. * @base: memory offset in virtual memory
  33. * @domain: IRQ domain for this instance
  34. * @valid: mask for valid IRQs on this controller
  35. * @used_irqs: number of active IRQs on this controller
  36. */
  37. struct fpga_irq_data {
  38. void __iomem *base;
  39. u32 valid;
  40. struct irq_domain *domain;
  41. u8 used_irqs;
  42. };
  43. /* we cannot allocate memory when the controllers are initially registered */
  44. static struct fpga_irq_data fpga_irq_devices[CONFIG_VERSATILE_FPGA_IRQ_NR];
  45. static int fpga_irq_id;
  46. static void fpga_irq_mask(struct irq_data *d)
  47. {
  48. struct fpga_irq_data *f = irq_data_get_irq_chip_data(d);
  49. u32 mask = 1 << d->hwirq;
  50. writel(mask, f->base + IRQ_ENABLE_CLEAR);
  51. }
  52. static void fpga_irq_unmask(struct irq_data *d)
  53. {
  54. struct fpga_irq_data *f = irq_data_get_irq_chip_data(d);
  55. u32 mask = 1 << d->hwirq;
  56. writel(mask, f->base + IRQ_ENABLE_SET);
  57. }
  58. static void fpga_irq_print_chip(struct irq_data *d, struct seq_file *p)
  59. {
  60. struct fpga_irq_data *f = irq_data_get_irq_chip_data(d);
  61. seq_printf(p, irq_domain_get_of_node(f->domain)->name);
  62. }
  63. static const struct irq_chip fpga_chip = {
  64. .irq_ack = fpga_irq_mask,
  65. .irq_mask = fpga_irq_mask,
  66. .irq_unmask = fpga_irq_unmask,
  67. .irq_print_chip = fpga_irq_print_chip,
  68. };
  69. static void fpga_irq_handle(struct irq_desc *desc)
  70. {
  71. struct irq_chip *chip = irq_desc_get_chip(desc);
  72. struct fpga_irq_data *f = irq_desc_get_handler_data(desc);
  73. u32 status;
  74. chained_irq_enter(chip, desc);
  75. status = readl(f->base + IRQ_STATUS);
  76. if (status == 0) {
  77. do_bad_IRQ(desc);
  78. goto out;
  79. }
  80. do {
  81. unsigned int irq = ffs(status) - 1;
  82. status &= ~(1 << irq);
  83. generic_handle_domain_irq(f->domain, irq);
  84. } while (status);
  85. out:
  86. chained_irq_exit(chip, desc);
  87. }
  88. /*
  89. * Handle each interrupt in a single FPGA IRQ controller. Returns non-zero
  90. * if we've handled at least one interrupt. This does a single read of the
  91. * status register and handles all interrupts in order from LSB first.
  92. */
  93. static int handle_one_fpga(struct fpga_irq_data *f, struct pt_regs *regs)
  94. {
  95. int handled = 0;
  96. int irq;
  97. u32 status;
  98. while ((status = readl(f->base + IRQ_STATUS))) {
  99. irq = ffs(status) - 1;
  100. generic_handle_domain_irq(f->domain, irq);
  101. handled = 1;
  102. }
  103. return handled;
  104. }
  105. /*
  106. * Keep iterating over all registered FPGA IRQ controllers until there are
  107. * no pending interrupts.
  108. */
  109. static asmlinkage void __exception_irq_entry fpga_handle_irq(struct pt_regs *regs)
  110. {
  111. int i, handled;
  112. do {
  113. for (i = 0, handled = 0; i < fpga_irq_id; ++i)
  114. handled |= handle_one_fpga(&fpga_irq_devices[i], regs);
  115. } while (handled);
  116. }
  117. static int fpga_irqdomain_map(struct irq_domain *d, unsigned int irq,
  118. irq_hw_number_t hwirq)
  119. {
  120. struct fpga_irq_data *f = d->host_data;
  121. /* Skip invalid IRQs, only register handlers for the real ones */
  122. if (!(f->valid & BIT(hwirq)))
  123. return -EPERM;
  124. irq_set_chip_data(irq, f);
  125. irq_set_chip_and_handler(irq, &fpga_chip, handle_level_irq);
  126. irq_set_probe(irq);
  127. return 0;
  128. }
  129. static const struct irq_domain_ops fpga_irqdomain_ops = {
  130. .map = fpga_irqdomain_map,
  131. .xlate = irq_domain_xlate_onetwocell,
  132. };
  133. static void __init fpga_irq_init(void __iomem *base, int parent_irq,
  134. u32 valid, struct device_node *node)
  135. {
  136. struct fpga_irq_data *f;
  137. int i;
  138. if (fpga_irq_id >= ARRAY_SIZE(fpga_irq_devices)) {
  139. pr_err("%s: too few FPGA IRQ controllers, increase CONFIG_VERSATILE_FPGA_IRQ_NR\n", __func__);
  140. return;
  141. }
  142. f = &fpga_irq_devices[fpga_irq_id];
  143. f->base = base;
  144. f->valid = valid;
  145. if (parent_irq != -1) {
  146. irq_set_chained_handler_and_data(parent_irq, fpga_irq_handle,
  147. f);
  148. }
  149. f->domain = irq_domain_add_linear(node, fls(valid),
  150. &fpga_irqdomain_ops, f);
  151. /* This will allocate all valid descriptors in the linear case */
  152. for (i = 0; i < fls(valid); i++)
  153. if (valid & BIT(i)) {
  154. /* Is this still required? */
  155. irq_create_mapping(f->domain, i);
  156. f->used_irqs++;
  157. }
  158. pr_info("FPGA IRQ chip %d \"%s\" @ %p, %u irqs",
  159. fpga_irq_id, node->name, base, f->used_irqs);
  160. if (parent_irq != -1)
  161. pr_cont(", parent IRQ: %d\n", parent_irq);
  162. else
  163. pr_cont("\n");
  164. fpga_irq_id++;
  165. }
  166. #ifdef CONFIG_OF
  167. static int __init fpga_irq_of_init(struct device_node *node,
  168. struct device_node *parent)
  169. {
  170. void __iomem *base;
  171. u32 clear_mask;
  172. u32 valid_mask;
  173. int parent_irq;
  174. if (WARN_ON(!node))
  175. return -ENODEV;
  176. base = of_iomap(node, 0);
  177. WARN(!base, "unable to map fpga irq registers\n");
  178. if (of_property_read_u32(node, "clear-mask", &clear_mask))
  179. clear_mask = 0;
  180. if (of_property_read_u32(node, "valid-mask", &valid_mask))
  181. valid_mask = 0;
  182. writel(clear_mask, base + IRQ_ENABLE_CLEAR);
  183. writel(clear_mask, base + FIQ_ENABLE_CLEAR);
  184. /* Some chips are cascaded from a parent IRQ */
  185. parent_irq = irq_of_parse_and_map(node, 0);
  186. if (!parent_irq) {
  187. set_handle_irq(fpga_handle_irq);
  188. parent_irq = -1;
  189. }
  190. fpga_irq_init(base, parent_irq, valid_mask, node);
  191. /*
  192. * On Versatile AB/PB, some secondary interrupts have a direct
  193. * pass-thru to the primary controller for IRQs 20 and 22-31 which need
  194. * to be enabled. See section 3.10 of the Versatile AB user guide.
  195. */
  196. if (of_device_is_compatible(node, "arm,versatile-sic"))
  197. writel(0xffd00000, base + PIC_ENABLES);
  198. return 0;
  199. }
  200. IRQCHIP_DECLARE(arm_fpga, "arm,versatile-fpga-irq", fpga_irq_of_init);
  201. IRQCHIP_DECLARE(arm_fpga_sic, "arm,versatile-sic", fpga_irq_of_init);
  202. IRQCHIP_DECLARE(ox810se_rps, "oxsemi,ox810se-rps-irq", fpga_irq_of_init);
  203. #endif