irq-tb10x.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Abilis Systems interrupt controller driver
  3. *
  4. * Copyright (C) Abilis Systems 2012
  5. *
  6. * Author: Christian Ruppert <christian.ruppert@abilis.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/interrupt.h>
  22. #include <linux/irqdomain.h>
  23. #include <linux/irq.h>
  24. #include <linux/irqchip.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/of_address.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/io.h>
  29. #include <linux/slab.h>
  30. #include <linux/bitops.h>
  31. #define AB_IRQCTL_INT_ENABLE 0x00
  32. #define AB_IRQCTL_INT_STATUS 0x04
  33. #define AB_IRQCTL_SRC_MODE 0x08
  34. #define AB_IRQCTL_SRC_POLARITY 0x0C
  35. #define AB_IRQCTL_INT_MODE 0x10
  36. #define AB_IRQCTL_INT_POLARITY 0x14
  37. #define AB_IRQCTL_INT_FORCE 0x18
  38. #define AB_IRQCTL_MAXIRQ 32
  39. static inline void ab_irqctl_writereg(struct irq_chip_generic *gc, u32 reg,
  40. u32 val)
  41. {
  42. irq_reg_writel(gc, val, reg);
  43. }
  44. static inline u32 ab_irqctl_readreg(struct irq_chip_generic *gc, u32 reg)
  45. {
  46. return irq_reg_readl(gc, reg);
  47. }
  48. static int tb10x_irq_set_type(struct irq_data *data, unsigned int flow_type)
  49. {
  50. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
  51. uint32_t im, mod, pol;
  52. im = data->mask;
  53. irq_gc_lock(gc);
  54. mod = ab_irqctl_readreg(gc, AB_IRQCTL_SRC_MODE) | im;
  55. pol = ab_irqctl_readreg(gc, AB_IRQCTL_SRC_POLARITY) | im;
  56. switch (flow_type & IRQF_TRIGGER_MASK) {
  57. case IRQ_TYPE_EDGE_FALLING:
  58. pol ^= im;
  59. break;
  60. case IRQ_TYPE_LEVEL_HIGH:
  61. mod ^= im;
  62. break;
  63. case IRQ_TYPE_NONE:
  64. flow_type = IRQ_TYPE_LEVEL_LOW;
  65. case IRQ_TYPE_LEVEL_LOW:
  66. mod ^= im;
  67. pol ^= im;
  68. break;
  69. case IRQ_TYPE_EDGE_RISING:
  70. break;
  71. default:
  72. irq_gc_unlock(gc);
  73. pr_err("%s: Cannot assign multiple trigger modes to IRQ %d.\n",
  74. __func__, data->irq);
  75. return -EBADR;
  76. }
  77. irqd_set_trigger_type(data, flow_type);
  78. irq_setup_alt_chip(data, flow_type);
  79. ab_irqctl_writereg(gc, AB_IRQCTL_SRC_MODE, mod);
  80. ab_irqctl_writereg(gc, AB_IRQCTL_SRC_POLARITY, pol);
  81. ab_irqctl_writereg(gc, AB_IRQCTL_INT_STATUS, im);
  82. irq_gc_unlock(gc);
  83. return IRQ_SET_MASK_OK;
  84. }
  85. static void tb10x_irq_cascade(struct irq_desc *desc)
  86. {
  87. struct irq_domain *domain = irq_desc_get_handler_data(desc);
  88. unsigned int irq = irq_desc_get_irq(desc);
  89. generic_handle_irq(irq_find_mapping(domain, irq));
  90. }
  91. static int __init of_tb10x_init_irq(struct device_node *ictl,
  92. struct device_node *parent)
  93. {
  94. int i, ret, nrirqs = of_irq_count(ictl);
  95. struct resource mem;
  96. struct irq_chip_generic *gc;
  97. struct irq_domain *domain;
  98. void __iomem *reg_base;
  99. if (of_address_to_resource(ictl, 0, &mem)) {
  100. pr_err("%s: No registers declared in DeviceTree.\n",
  101. ictl->name);
  102. return -EINVAL;
  103. }
  104. if (!request_mem_region(mem.start, resource_size(&mem),
  105. ictl->name)) {
  106. pr_err("%s: Request mem region failed.\n", ictl->name);
  107. return -EBUSY;
  108. }
  109. reg_base = ioremap(mem.start, resource_size(&mem));
  110. if (!reg_base) {
  111. ret = -EBUSY;
  112. pr_err("%s: ioremap failed.\n", ictl->name);
  113. goto ioremap_fail;
  114. }
  115. domain = irq_domain_add_linear(ictl, AB_IRQCTL_MAXIRQ,
  116. &irq_generic_chip_ops, NULL);
  117. if (!domain) {
  118. ret = -ENOMEM;
  119. pr_err("%s: Could not register interrupt domain.\n",
  120. ictl->name);
  121. goto irq_domain_add_fail;
  122. }
  123. ret = irq_alloc_domain_generic_chips(domain, AB_IRQCTL_MAXIRQ,
  124. 2, ictl->name, handle_level_irq,
  125. IRQ_NOREQUEST, IRQ_NOPROBE,
  126. IRQ_GC_INIT_MASK_CACHE);
  127. if (ret) {
  128. pr_err("%s: Could not allocate generic interrupt chip.\n",
  129. ictl->name);
  130. goto gc_alloc_fail;
  131. }
  132. gc = domain->gc->gc[0];
  133. gc->reg_base = reg_base;
  134. gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
  135. gc->chip_types[0].chip.irq_mask = irq_gc_mask_clr_bit;
  136. gc->chip_types[0].chip.irq_unmask = irq_gc_mask_set_bit;
  137. gc->chip_types[0].chip.irq_set_type = tb10x_irq_set_type;
  138. gc->chip_types[0].regs.mask = AB_IRQCTL_INT_ENABLE;
  139. gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
  140. gc->chip_types[1].chip.name = gc->chip_types[0].chip.name;
  141. gc->chip_types[1].chip.irq_ack = irq_gc_ack_set_bit;
  142. gc->chip_types[1].chip.irq_mask = irq_gc_mask_clr_bit;
  143. gc->chip_types[1].chip.irq_unmask = irq_gc_mask_set_bit;
  144. gc->chip_types[1].chip.irq_set_type = tb10x_irq_set_type;
  145. gc->chip_types[1].regs.ack = AB_IRQCTL_INT_STATUS;
  146. gc->chip_types[1].regs.mask = AB_IRQCTL_INT_ENABLE;
  147. gc->chip_types[1].handler = handle_edge_irq;
  148. for (i = 0; i < nrirqs; i++) {
  149. unsigned int irq = irq_of_parse_and_map(ictl, i);
  150. irq_set_chained_handler_and_data(irq, tb10x_irq_cascade,
  151. domain);
  152. }
  153. ab_irqctl_writereg(gc, AB_IRQCTL_INT_ENABLE, 0);
  154. ab_irqctl_writereg(gc, AB_IRQCTL_INT_MODE, 0);
  155. ab_irqctl_writereg(gc, AB_IRQCTL_INT_POLARITY, 0);
  156. ab_irqctl_writereg(gc, AB_IRQCTL_INT_STATUS, ~0UL);
  157. return 0;
  158. gc_alloc_fail:
  159. irq_domain_remove(domain);
  160. irq_domain_add_fail:
  161. iounmap(reg_base);
  162. ioremap_fail:
  163. release_mem_region(mem.start, resource_size(&mem));
  164. return ret;
  165. }
  166. IRQCHIP_DECLARE(tb10x_intc, "abilis,tb10x-ictl", of_tb10x_init_irq);