irq-dw-apb-ictl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Synopsys DW APB ICTL irqchip driver.
  3. *
  4. * Sebastian Hesselbarth <[email protected]>
  5. *
  6. * based on GPL'ed 2.6 kernel sources
  7. * (c) Marvell International Ltd.
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/io.h>
  14. #include <linux/irq.h>
  15. #include <linux/irqchip.h>
  16. #include <linux/irqchip/chained_irq.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/interrupt.h>
  20. #define APB_INT_ENABLE_L 0x00
  21. #define APB_INT_ENABLE_H 0x04
  22. #define APB_INT_MASK_L 0x08
  23. #define APB_INT_MASK_H 0x0c
  24. #define APB_INT_FINALSTATUS_L 0x30
  25. #define APB_INT_FINALSTATUS_H 0x34
  26. #define APB_INT_BASE_OFFSET 0x04
  27. /* irq domain of the primary interrupt controller. */
  28. static struct irq_domain *dw_apb_ictl_irq_domain;
  29. static void __irq_entry dw_apb_ictl_handle_irq(struct pt_regs *regs)
  30. {
  31. struct irq_domain *d = dw_apb_ictl_irq_domain;
  32. int n;
  33. for (n = 0; n < d->revmap_size; n += 32) {
  34. struct irq_chip_generic *gc = irq_get_domain_generic_chip(d, n);
  35. u32 stat = readl_relaxed(gc->reg_base + APB_INT_FINALSTATUS_L);
  36. while (stat) {
  37. u32 hwirq = ffs(stat) - 1;
  38. generic_handle_domain_irq(d, hwirq);
  39. stat &= ~BIT(hwirq);
  40. }
  41. }
  42. }
  43. static void dw_apb_ictl_handle_irq_cascaded(struct irq_desc *desc)
  44. {
  45. struct irq_domain *d = irq_desc_get_handler_data(desc);
  46. struct irq_chip *chip = irq_desc_get_chip(desc);
  47. int n;
  48. chained_irq_enter(chip, desc);
  49. for (n = 0; n < d->revmap_size; n += 32) {
  50. struct irq_chip_generic *gc = irq_get_domain_generic_chip(d, n);
  51. u32 stat = readl_relaxed(gc->reg_base + APB_INT_FINALSTATUS_L);
  52. while (stat) {
  53. u32 hwirq = ffs(stat) - 1;
  54. generic_handle_domain_irq(d, gc->irq_base + hwirq);
  55. stat &= ~BIT(hwirq);
  56. }
  57. }
  58. chained_irq_exit(chip, desc);
  59. }
  60. static int dw_apb_ictl_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  61. unsigned int nr_irqs, void *arg)
  62. {
  63. int i, ret;
  64. irq_hw_number_t hwirq;
  65. unsigned int type = IRQ_TYPE_NONE;
  66. struct irq_fwspec *fwspec = arg;
  67. ret = irq_domain_translate_onecell(domain, fwspec, &hwirq, &type);
  68. if (ret)
  69. return ret;
  70. for (i = 0; i < nr_irqs; i++)
  71. irq_map_generic_chip(domain, virq + i, hwirq + i);
  72. return 0;
  73. }
  74. static const struct irq_domain_ops dw_apb_ictl_irq_domain_ops = {
  75. .translate = irq_domain_translate_onecell,
  76. .alloc = dw_apb_ictl_irq_domain_alloc,
  77. .free = irq_domain_free_irqs_top,
  78. };
  79. #ifdef CONFIG_PM
  80. static void dw_apb_ictl_resume(struct irq_data *d)
  81. {
  82. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  83. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  84. irq_gc_lock(gc);
  85. writel_relaxed(~0, gc->reg_base + ct->regs.enable);
  86. writel_relaxed(*ct->mask_cache, gc->reg_base + ct->regs.mask);
  87. irq_gc_unlock(gc);
  88. }
  89. #else
  90. #define dw_apb_ictl_resume NULL
  91. #endif /* CONFIG_PM */
  92. static int __init dw_apb_ictl_init(struct device_node *np,
  93. struct device_node *parent)
  94. {
  95. const struct irq_domain_ops *domain_ops;
  96. unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  97. struct resource r;
  98. struct irq_domain *domain;
  99. struct irq_chip_generic *gc;
  100. void __iomem *iobase;
  101. int ret, nrirqs, parent_irq, i;
  102. u32 reg;
  103. if (!parent) {
  104. /* Used as the primary interrupt controller */
  105. parent_irq = 0;
  106. domain_ops = &dw_apb_ictl_irq_domain_ops;
  107. } else {
  108. /* Map the parent interrupt for the chained handler */
  109. parent_irq = irq_of_parse_and_map(np, 0);
  110. if (parent_irq <= 0) {
  111. pr_err("%pOF: unable to parse irq\n", np);
  112. return -EINVAL;
  113. }
  114. domain_ops = &irq_generic_chip_ops;
  115. }
  116. ret = of_address_to_resource(np, 0, &r);
  117. if (ret) {
  118. pr_err("%pOF: unable to get resource\n", np);
  119. return ret;
  120. }
  121. if (!request_mem_region(r.start, resource_size(&r), np->full_name)) {
  122. pr_err("%pOF: unable to request mem region\n", np);
  123. return -ENOMEM;
  124. }
  125. iobase = ioremap(r.start, resource_size(&r));
  126. if (!iobase) {
  127. pr_err("%pOF: unable to map resource\n", np);
  128. ret = -ENOMEM;
  129. goto err_release;
  130. }
  131. /*
  132. * DW IP can be configured to allow 2-64 irqs. We can determine
  133. * the number of irqs supported by writing into enable register
  134. * and look for bits not set, as corresponding flip-flops will
  135. * have been removed by synthesis tool.
  136. */
  137. /* mask and enable all interrupts */
  138. writel_relaxed(~0, iobase + APB_INT_MASK_L);
  139. writel_relaxed(~0, iobase + APB_INT_MASK_H);
  140. writel_relaxed(~0, iobase + APB_INT_ENABLE_L);
  141. writel_relaxed(~0, iobase + APB_INT_ENABLE_H);
  142. reg = readl_relaxed(iobase + APB_INT_ENABLE_H);
  143. if (reg)
  144. nrirqs = 32 + fls(reg);
  145. else
  146. nrirqs = fls(readl_relaxed(iobase + APB_INT_ENABLE_L));
  147. domain = irq_domain_add_linear(np, nrirqs, domain_ops, NULL);
  148. if (!domain) {
  149. pr_err("%pOF: unable to add irq domain\n", np);
  150. ret = -ENOMEM;
  151. goto err_unmap;
  152. }
  153. ret = irq_alloc_domain_generic_chips(domain, 32, 1, np->name,
  154. handle_level_irq, clr, 0,
  155. IRQ_GC_INIT_MASK_CACHE);
  156. if (ret) {
  157. pr_err("%pOF: unable to alloc irq domain gc\n", np);
  158. goto err_unmap;
  159. }
  160. for (i = 0; i < DIV_ROUND_UP(nrirqs, 32); i++) {
  161. gc = irq_get_domain_generic_chip(domain, i * 32);
  162. gc->reg_base = iobase + i * APB_INT_BASE_OFFSET;
  163. gc->chip_types[0].regs.mask = APB_INT_MASK_L;
  164. gc->chip_types[0].regs.enable = APB_INT_ENABLE_L;
  165. gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
  166. gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
  167. gc->chip_types[0].chip.irq_resume = dw_apb_ictl_resume;
  168. }
  169. if (parent_irq) {
  170. irq_set_chained_handler_and_data(parent_irq,
  171. dw_apb_ictl_handle_irq_cascaded, domain);
  172. } else {
  173. dw_apb_ictl_irq_domain = domain;
  174. set_handle_irq(dw_apb_ictl_handle_irq);
  175. }
  176. return 0;
  177. err_unmap:
  178. iounmap(iobase);
  179. err_release:
  180. release_mem_region(r.start, resource_size(&r));
  181. return ret;
  182. }
  183. IRQCHIP_DECLARE(dw_apb_ictl,
  184. "snps,dw-apb-ictl", dw_apb_ictl_init);