fpga-irq.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <asm/exception.h>
  9. #include <asm/mach/irq.h>
  10. #include <plat/fpga-irq.h>
  11. #define IRQ_STATUS 0x00
  12. #define IRQ_RAW_STATUS 0x04
  13. #define IRQ_ENABLE_SET 0x08
  14. #define IRQ_ENABLE_CLEAR 0x0c
  15. /**
  16. * struct fpga_irq_data - irq data container for the FPGA IRQ controller
  17. * @base: memory offset in virtual memory
  18. * @irq_start: first IRQ number handled by this instance
  19. * @chip: chip container for this instance
  20. * @domain: IRQ domain for this instance
  21. * @valid: mask for valid IRQs on this controller
  22. * @used_irqs: number of active IRQs on this controller
  23. */
  24. struct fpga_irq_data {
  25. void __iomem *base;
  26. unsigned int irq_start;
  27. struct irq_chip chip;
  28. u32 valid;
  29. struct irq_domain *domain;
  30. u8 used_irqs;
  31. };
  32. /* we cannot allocate memory when the controllers are initially registered */
  33. static struct fpga_irq_data fpga_irq_devices[CONFIG_PLAT_VERSATILE_FPGA_IRQ_NR];
  34. static int fpga_irq_id;
  35. static void fpga_irq_mask(struct irq_data *d)
  36. {
  37. struct fpga_irq_data *f = irq_data_get_irq_chip_data(d);
  38. u32 mask = 1 << d->hwirq;
  39. writel(mask, f->base + IRQ_ENABLE_CLEAR);
  40. }
  41. static void fpga_irq_unmask(struct irq_data *d)
  42. {
  43. struct fpga_irq_data *f = irq_data_get_irq_chip_data(d);
  44. u32 mask = 1 << d->hwirq;
  45. writel(mask, f->base + IRQ_ENABLE_SET);
  46. }
  47. static void fpga_irq_handle(unsigned int irq, struct irq_desc *desc)
  48. {
  49. struct fpga_irq_data *f = irq_desc_get_handler_data(desc);
  50. u32 status = readl(f->base + IRQ_STATUS);
  51. if (status == 0) {
  52. do_bad_IRQ(irq, desc);
  53. return;
  54. }
  55. do {
  56. irq = ffs(status) - 1;
  57. status &= ~(1 << irq);
  58. generic_handle_irq(irq_find_mapping(f->domain, irq));
  59. } while (status);
  60. }
  61. /*
  62. * Handle each interrupt in a single FPGA IRQ controller. Returns non-zero
  63. * if we've handled at least one interrupt. This does a single read of the
  64. * status register and handles all interrupts in order from LSB first.
  65. */
  66. static int handle_one_fpga(struct fpga_irq_data *f, struct pt_regs *regs)
  67. {
  68. int handled = 0;
  69. int irq;
  70. u32 status;
  71. while ((status = readl(f->base + IRQ_STATUS))) {
  72. irq = ffs(status) - 1;
  73. handle_IRQ(irq_find_mapping(f->domain, irq), regs);
  74. handled = 1;
  75. }
  76. return handled;
  77. }
  78. /*
  79. * Keep iterating over all registered FPGA IRQ controllers until there are
  80. * no pending interrupts.
  81. */
  82. asmlinkage void __exception_irq_entry fpga_handle_irq(struct pt_regs *regs)
  83. {
  84. int i, handled;
  85. do {
  86. for (i = 0, handled = 0; i < fpga_irq_id; ++i)
  87. handled |= handle_one_fpga(&fpga_irq_devices[i], regs);
  88. } while (handled);
  89. }
  90. static int fpga_irqdomain_map(struct irq_domain *d, unsigned int irq,
  91. irq_hw_number_t hwirq)
  92. {
  93. struct fpga_irq_data *f = d->host_data;
  94. /* Skip invalid IRQs, only register handlers for the real ones */
  95. if (!(f->valid & (1 << hwirq)))
  96. return -ENOTSUPP;
  97. irq_set_chip_data(irq, f);
  98. irq_set_chip_and_handler(irq, &f->chip,
  99. handle_level_irq);
  100. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  101. f->used_irqs++;
  102. return 0;
  103. }
  104. static struct irq_domain_ops fpga_irqdomain_ops = {
  105. .map = fpga_irqdomain_map,
  106. .xlate = irq_domain_xlate_onetwocell,
  107. };
  108. void __init fpga_irq_init(void __iomem *base, const char *name, int irq_start,
  109. int parent_irq, u32 valid, struct device_node *node)
  110. {
  111. struct fpga_irq_data *f;
  112. if (fpga_irq_id >= ARRAY_SIZE(fpga_irq_devices)) {
  113. printk(KERN_ERR "%s: too few FPGA IRQ controllers, increase CONFIG_PLAT_VERSATILE_FPGA_IRQ_NR\n", __func__);
  114. return;
  115. }
  116. f = &fpga_irq_devices[fpga_irq_id];
  117. f->base = base;
  118. f->irq_start = irq_start;
  119. f->chip.name = name;
  120. f->chip.irq_ack = fpga_irq_mask;
  121. f->chip.irq_mask = fpga_irq_mask;
  122. f->chip.irq_unmask = fpga_irq_unmask;
  123. f->valid = valid;
  124. if (parent_irq != -1) {
  125. irq_set_handler_data(parent_irq, f);
  126. irq_set_chained_handler(parent_irq, fpga_irq_handle);
  127. }
  128. f->domain = irq_domain_add_legacy(node, fls(valid), f->irq_start, 0,
  129. &fpga_irqdomain_ops, f);
  130. pr_info("FPGA IRQ chip %d \"%s\" @ %p, %u irqs\n",
  131. fpga_irq_id, name, base, f->used_irqs);
  132. fpga_irq_id++;
  133. }