irq-tb10x.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/of_irq.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/io.h>
  28. #include <linux/slab.h>
  29. #include <linux/bitops.h>
  30. #include "irqchip.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(val, gc->reg_base + reg);
  43. }
  44. static inline u32 ab_irqctl_readreg(struct irq_chip_generic *gc, u32 reg)
  45. {
  46. return irq_reg_readl(gc->reg_base + 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(unsigned int irq, struct irq_desc *desc)
  86. {
  87. struct irq_domain *domain = irq_desc_get_handler_data(desc);
  88. generic_handle_irq(irq_find_mapping(domain, irq));
  89. }
  90. static int __init of_tb10x_init_irq(struct device_node *ictl,
  91. struct device_node *parent)
  92. {
  93. int i, ret, nrirqs = of_irq_count(ictl);
  94. struct resource mem;
  95. struct irq_chip_generic *gc;
  96. struct irq_domain *domain;
  97. void __iomem *reg_base;
  98. if (of_address_to_resource(ictl, 0, &mem)) {
  99. pr_err("%s: No registers declared in DeviceTree.\n",
  100. ictl->name);
  101. return -EINVAL;
  102. }
  103. if (!request_mem_region(mem.start, resource_size(&mem),
  104. ictl->name)) {
  105. pr_err("%s: Request mem region failed.\n", ictl->name);
  106. return -EBUSY;
  107. }
  108. reg_base = ioremap(mem.start, resource_size(&mem));
  109. if (!reg_base) {
  110. ret = -EBUSY;
  111. pr_err("%s: ioremap failed.\n", ictl->name);
  112. goto ioremap_fail;
  113. }
  114. domain = irq_domain_add_linear(ictl, AB_IRQCTL_MAXIRQ,
  115. &irq_generic_chip_ops, NULL);
  116. if (!domain) {
  117. ret = -ENOMEM;
  118. pr_err("%s: Could not register interrupt domain.\n",
  119. ictl->name);
  120. goto irq_domain_add_fail;
  121. }
  122. ret = irq_alloc_domain_generic_chips(domain, AB_IRQCTL_MAXIRQ,
  123. 2, ictl->name, handle_level_irq,
  124. IRQ_NOREQUEST, IRQ_NOPROBE,
  125. IRQ_GC_INIT_MASK_CACHE);
  126. if (ret) {
  127. pr_err("%s: Could not allocate generic interrupt chip.\n",
  128. ictl->name);
  129. goto gc_alloc_fail;
  130. }
  131. gc = domain->gc->gc[0];
  132. gc->reg_base = reg_base;
  133. gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
  134. gc->chip_types[0].chip.irq_mask = irq_gc_mask_clr_bit;
  135. gc->chip_types[0].chip.irq_unmask = irq_gc_mask_set_bit;
  136. gc->chip_types[0].chip.irq_set_type = tb10x_irq_set_type;
  137. gc->chip_types[0].regs.mask = AB_IRQCTL_INT_ENABLE;
  138. gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
  139. gc->chip_types[1].chip.name = gc->chip_types[0].chip.name;
  140. gc->chip_types[1].chip.irq_ack = irq_gc_ack_set_bit;
  141. gc->chip_types[1].chip.irq_mask = irq_gc_mask_clr_bit;
  142. gc->chip_types[1].chip.irq_unmask = irq_gc_mask_set_bit;
  143. gc->chip_types[1].chip.irq_set_type = tb10x_irq_set_type;
  144. gc->chip_types[1].regs.ack = AB_IRQCTL_INT_STATUS;
  145. gc->chip_types[1].regs.mask = AB_IRQCTL_INT_ENABLE;
  146. gc->chip_types[1].handler = handle_edge_irq;
  147. for (i = 0; i < nrirqs; i++) {
  148. unsigned int irq = irq_of_parse_and_map(ictl, i);
  149. irq_set_handler_data(irq, domain);
  150. irq_set_chained_handler(irq, tb10x_irq_cascade);
  151. }
  152. ab_irqctl_writereg(gc, AB_IRQCTL_INT_ENABLE, 0);
  153. ab_irqctl_writereg(gc, AB_IRQCTL_INT_MODE, 0);
  154. ab_irqctl_writereg(gc, AB_IRQCTL_INT_POLARITY, 0);
  155. ab_irqctl_writereg(gc, AB_IRQCTL_INT_STATUS, ~0UL);
  156. return 0;
  157. gc_alloc_fail:
  158. irq_domain_remove(domain);
  159. irq_domain_add_fail:
  160. iounmap(reg_base);
  161. ioremap_fail:
  162. release_mem_region(mem.start, resource_size(&mem));
  163. return ret;
  164. }
  165. IRQCHIP_DECLARE(tb10x_intc, "abilis,tb10x-ictl", of_tb10x_init_irq);