irq-versatile-fpga.c 5.3 KB

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