irq-tb10x.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Abilis Systems interrupt controller driver
  4. *
  5. * Copyright (C) Abilis Systems 2012
  6. *
  7. * Author: Christian Ruppert <christian.ruppert@abilis.com>
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/irqdomain.h>
  11. #include <linux/irq.h>
  12. #include <linux/irqchip.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/of_address.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/bitops.h>
  18. #define AB_IRQCTL_INT_ENABLE 0x00
  19. #define AB_IRQCTL_INT_STATUS 0x04
  20. #define AB_IRQCTL_SRC_MODE 0x08
  21. #define AB_IRQCTL_SRC_POLARITY 0x0C
  22. #define AB_IRQCTL_INT_MODE 0x10
  23. #define AB_IRQCTL_INT_POLARITY 0x14
  24. #define AB_IRQCTL_INT_FORCE 0x18
  25. #define AB_IRQCTL_MAXIRQ 32
  26. static inline void ab_irqctl_writereg(struct irq_chip_generic *gc, u32 reg,
  27. u32 val)
  28. {
  29. irq_reg_writel(gc, val, reg);
  30. }
  31. static inline u32 ab_irqctl_readreg(struct irq_chip_generic *gc, u32 reg)
  32. {
  33. return irq_reg_readl(gc, reg);
  34. }
  35. static int tb10x_irq_set_type(struct irq_data *data, unsigned int flow_type)
  36. {
  37. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
  38. uint32_t im, mod, pol;
  39. im = data->mask;
  40. irq_gc_lock(gc);
  41. mod = ab_irqctl_readreg(gc, AB_IRQCTL_SRC_MODE) | im;
  42. pol = ab_irqctl_readreg(gc, AB_IRQCTL_SRC_POLARITY) | im;
  43. switch (flow_type & IRQF_TRIGGER_MASK) {
  44. case IRQ_TYPE_EDGE_FALLING:
  45. pol ^= im;
  46. break;
  47. case IRQ_TYPE_LEVEL_HIGH:
  48. mod ^= im;
  49. break;
  50. case IRQ_TYPE_NONE:
  51. flow_type = IRQ_TYPE_LEVEL_LOW;
  52. fallthrough;
  53. case IRQ_TYPE_LEVEL_LOW:
  54. mod ^= im;
  55. pol ^= im;
  56. break;
  57. case IRQ_TYPE_EDGE_RISING:
  58. break;
  59. default:
  60. irq_gc_unlock(gc);
  61. pr_err("%s: Cannot assign multiple trigger modes to IRQ %d.\n",
  62. __func__, data->irq);
  63. return -EBADR;
  64. }
  65. irqd_set_trigger_type(data, flow_type);
  66. irq_setup_alt_chip(data, flow_type);
  67. ab_irqctl_writereg(gc, AB_IRQCTL_SRC_MODE, mod);
  68. ab_irqctl_writereg(gc, AB_IRQCTL_SRC_POLARITY, pol);
  69. ab_irqctl_writereg(gc, AB_IRQCTL_INT_STATUS, im);
  70. irq_gc_unlock(gc);
  71. return IRQ_SET_MASK_OK;
  72. }
  73. static void tb10x_irq_cascade(struct irq_desc *desc)
  74. {
  75. struct irq_domain *domain = irq_desc_get_handler_data(desc);
  76. unsigned int irq = irq_desc_get_irq(desc);
  77. generic_handle_domain_irq(domain, irq);
  78. }
  79. static int __init of_tb10x_init_irq(struct device_node *ictl,
  80. struct device_node *parent)
  81. {
  82. int i, ret, nrirqs = of_irq_count(ictl);
  83. struct resource mem;
  84. struct irq_chip_generic *gc;
  85. struct irq_domain *domain;
  86. void __iomem *reg_base;
  87. if (of_address_to_resource(ictl, 0, &mem)) {
  88. pr_err("%pOFn: No registers declared in DeviceTree.\n",
  89. ictl);
  90. return -EINVAL;
  91. }
  92. if (!request_mem_region(mem.start, resource_size(&mem),
  93. ictl->full_name)) {
  94. pr_err("%pOFn: Request mem region failed.\n", ictl);
  95. return -EBUSY;
  96. }
  97. reg_base = ioremap(mem.start, resource_size(&mem));
  98. if (!reg_base) {
  99. ret = -EBUSY;
  100. pr_err("%pOFn: ioremap failed.\n", ictl);
  101. goto ioremap_fail;
  102. }
  103. domain = irq_domain_add_linear(ictl, AB_IRQCTL_MAXIRQ,
  104. &irq_generic_chip_ops, NULL);
  105. if (!domain) {
  106. ret = -ENOMEM;
  107. pr_err("%pOFn: Could not register interrupt domain.\n",
  108. ictl);
  109. goto irq_domain_add_fail;
  110. }
  111. ret = irq_alloc_domain_generic_chips(domain, AB_IRQCTL_MAXIRQ,
  112. 2, ictl->name, handle_level_irq,
  113. IRQ_NOREQUEST, IRQ_NOPROBE,
  114. IRQ_GC_INIT_MASK_CACHE);
  115. if (ret) {
  116. pr_err("%pOFn: Could not allocate generic interrupt chip.\n",
  117. ictl);
  118. goto gc_alloc_fail;
  119. }
  120. gc = domain->gc->gc[0];
  121. gc->reg_base = reg_base;
  122. gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
  123. gc->chip_types[0].chip.irq_mask = irq_gc_mask_clr_bit;
  124. gc->chip_types[0].chip.irq_unmask = irq_gc_mask_set_bit;
  125. gc->chip_types[0].chip.irq_set_type = tb10x_irq_set_type;
  126. gc->chip_types[0].regs.mask = AB_IRQCTL_INT_ENABLE;
  127. gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
  128. gc->chip_types[1].chip.irq_ack = irq_gc_ack_set_bit;
  129. gc->chip_types[1].chip.irq_mask = irq_gc_mask_clr_bit;
  130. gc->chip_types[1].chip.irq_unmask = irq_gc_mask_set_bit;
  131. gc->chip_types[1].chip.irq_set_type = tb10x_irq_set_type;
  132. gc->chip_types[1].regs.ack = AB_IRQCTL_INT_STATUS;
  133. gc->chip_types[1].regs.mask = AB_IRQCTL_INT_ENABLE;
  134. gc->chip_types[1].handler = handle_edge_irq;
  135. for (i = 0; i < nrirqs; i++) {
  136. unsigned int irq = irq_of_parse_and_map(ictl, i);
  137. irq_set_chained_handler_and_data(irq, tb10x_irq_cascade,
  138. domain);
  139. }
  140. ab_irqctl_writereg(gc, AB_IRQCTL_INT_ENABLE, 0);
  141. ab_irqctl_writereg(gc, AB_IRQCTL_INT_MODE, 0);
  142. ab_irqctl_writereg(gc, AB_IRQCTL_INT_POLARITY, 0);
  143. ab_irqctl_writereg(gc, AB_IRQCTL_INT_STATUS, ~0UL);
  144. return 0;
  145. gc_alloc_fail:
  146. irq_domain_remove(domain);
  147. irq_domain_add_fail:
  148. iounmap(reg_base);
  149. ioremap_fail:
  150. release_mem_region(mem.start, resource_size(&mem));
  151. return ret;
  152. }
  153. IRQCHIP_DECLARE(tb10x_intc, "abilis,tb10x-ictl", of_tb10x_init_irq);