irq-versatile-fpga.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/versatile-fpga.h>
  8. #include <linux/irqdomain.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <asm/exception.h>
  13. #include <asm/mach/irq.h>
  14. #define IRQ_STATUS 0x00
  15. #define IRQ_RAW_STATUS 0x04
  16. #define IRQ_ENABLE_SET 0x08
  17. #define IRQ_ENABLE_CLEAR 0x0c
  18. #define INT_SOFT_SET 0x10
  19. #define INT_SOFT_CLEAR 0x14
  20. #define FIQ_STATUS 0x20
  21. #define FIQ_RAW_STATUS 0x24
  22. #define FIQ_ENABLE 0x28
  23. #define FIQ_ENABLE_SET 0x28
  24. #define FIQ_ENABLE_CLEAR 0x2C
  25. /**
  26. * struct fpga_irq_data - irq data container for the FPGA IRQ controller
  27. * @base: memory offset in virtual memory
  28. * @chip: chip container for this instance
  29. * @domain: IRQ domain for this instance
  30. * @valid: mask for valid IRQs on this controller
  31. * @used_irqs: number of active IRQs on this controller
  32. */
  33. struct fpga_irq_data {
  34. void __iomem *base;
  35. struct irq_chip chip;
  36. u32 valid;
  37. struct irq_domain *domain;
  38. u8 used_irqs;
  39. };
  40. /* we cannot allocate memory when the controllers are initially registered */
  41. static struct fpga_irq_data fpga_irq_devices[CONFIG_VERSATILE_FPGA_IRQ_NR];
  42. static int fpga_irq_id;
  43. static void fpga_irq_mask(struct irq_data *d)
  44. {
  45. struct fpga_irq_data *f = irq_data_get_irq_chip_data(d);
  46. u32 mask = 1 << d->hwirq;
  47. writel(mask, f->base + IRQ_ENABLE_CLEAR);
  48. }
  49. static void fpga_irq_unmask(struct irq_data *d)
  50. {
  51. struct fpga_irq_data *f = irq_data_get_irq_chip_data(d);
  52. u32 mask = 1 << d->hwirq;
  53. writel(mask, f->base + IRQ_ENABLE_SET);
  54. }
  55. static void fpga_irq_handle(unsigned int irq, struct irq_desc *desc)
  56. {
  57. struct fpga_irq_data *f = irq_desc_get_handler_data(desc);
  58. u32 status = readl(f->base + IRQ_STATUS);
  59. if (status == 0) {
  60. do_bad_IRQ(irq, desc);
  61. return;
  62. }
  63. do {
  64. irq = ffs(status) - 1;
  65. status &= ~(1 << irq);
  66. generic_handle_irq(irq_find_mapping(f->domain, irq));
  67. } while (status);
  68. }
  69. /*
  70. * Handle each interrupt in a single FPGA IRQ controller. Returns non-zero
  71. * if we've handled at least one interrupt. This does a single read of the
  72. * status register and handles all interrupts in order from LSB first.
  73. */
  74. static int handle_one_fpga(struct fpga_irq_data *f, struct pt_regs *regs)
  75. {
  76. int handled = 0;
  77. int irq;
  78. u32 status;
  79. while ((status = readl(f->base + IRQ_STATUS))) {
  80. irq = ffs(status) - 1;
  81. handle_IRQ(irq_find_mapping(f->domain, irq), regs);
  82. handled = 1;
  83. }
  84. return handled;
  85. }
  86. /*
  87. * Keep iterating over all registered FPGA IRQ controllers until there are
  88. * no pending interrupts.
  89. */
  90. asmlinkage void __exception_irq_entry fpga_handle_irq(struct pt_regs *regs)
  91. {
  92. int i, handled;
  93. do {
  94. for (i = 0, handled = 0; i < fpga_irq_id; ++i)
  95. handled |= handle_one_fpga(&fpga_irq_devices[i], regs);
  96. } while (handled);
  97. }
  98. static int fpga_irqdomain_map(struct irq_domain *d, unsigned int irq,
  99. irq_hw_number_t hwirq)
  100. {
  101. struct fpga_irq_data *f = d->host_data;
  102. /* Skip invalid IRQs, only register handlers for the real ones */
  103. if (!(f->valid & BIT(hwirq)))
  104. return -ENOTSUPP;
  105. irq_set_chip_data(irq, f);
  106. irq_set_chip_and_handler(irq, &f->chip,
  107. handle_level_irq);
  108. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  109. return 0;
  110. }
  111. static struct irq_domain_ops fpga_irqdomain_ops = {
  112. .map = fpga_irqdomain_map,
  113. .xlate = irq_domain_xlate_onetwocell,
  114. };
  115. void __init fpga_irq_init(void __iomem *base, const char *name, int irq_start,
  116. int parent_irq, u32 valid, struct device_node *node)
  117. {
  118. struct fpga_irq_data *f;
  119. int i;
  120. if (fpga_irq_id >= ARRAY_SIZE(fpga_irq_devices)) {
  121. pr_err("%s: too few FPGA IRQ controllers, increase CONFIG_PLAT_VERSATILE_FPGA_IRQ_NR\n", __func__);
  122. return;
  123. }
  124. f = &fpga_irq_devices[fpga_irq_id];
  125. f->base = base;
  126. f->chip.name = name;
  127. f->chip.irq_ack = fpga_irq_mask;
  128. f->chip.irq_mask = fpga_irq_mask;
  129. f->chip.irq_unmask = fpga_irq_unmask;
  130. f->valid = valid;
  131. if (parent_irq != -1) {
  132. irq_set_handler_data(parent_irq, f);
  133. irq_set_chained_handler(parent_irq, fpga_irq_handle);
  134. }
  135. /* This will also allocate irq descriptors */
  136. f->domain = irq_domain_add_simple(node, fls(valid), irq_start,
  137. &fpga_irqdomain_ops, f);
  138. /* This will allocate all valid descriptors in the linear case */
  139. for (i = 0; i < fls(valid); i++)
  140. if (valid & BIT(i)) {
  141. if (!irq_start)
  142. irq_create_mapping(f->domain, i);
  143. f->used_irqs++;
  144. }
  145. pr_info("FPGA IRQ chip %d \"%s\" @ %p, %u irqs\n",
  146. fpga_irq_id, name, base, f->used_irqs);
  147. fpga_irq_id++;
  148. }
  149. #ifdef CONFIG_OF
  150. int __init fpga_irq_of_init(struct device_node *node,
  151. struct device_node *parent)
  152. {
  153. void __iomem *base;
  154. u32 clear_mask;
  155. u32 valid_mask;
  156. if (WARN_ON(!node))
  157. return -ENODEV;
  158. base = of_iomap(node, 0);
  159. WARN(!base, "unable to map fpga irq registers\n");
  160. if (of_property_read_u32(node, "clear-mask", &clear_mask))
  161. clear_mask = 0;
  162. if (of_property_read_u32(node, "valid-mask", &valid_mask))
  163. valid_mask = 0;
  164. fpga_irq_init(base, node->name, 0, -1, valid_mask, node);
  165. writel(clear_mask, base + IRQ_ENABLE_CLEAR);
  166. writel(clear_mask, base + FIQ_ENABLE_CLEAR);
  167. return 0;
  168. }
  169. #endif