irq-versatile-fpga.c 5.9 KB

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