irq-versatile-fpga.c 5.9 KB

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