fpga-irq.c 5.2 KB

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