irq-dw-apb-ictl.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Synopsys DW APB ICTL irqchip driver.
  3. *
  4. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  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/chained_irq.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include "irqchip.h"
  19. #define APB_INT_ENABLE_L 0x00
  20. #define APB_INT_ENABLE_H 0x04
  21. #define APB_INT_MASK_L 0x08
  22. #define APB_INT_MASK_H 0x0c
  23. #define APB_INT_FINALSTATUS_L 0x30
  24. #define APB_INT_FINALSTATUS_H 0x34
  25. static void dw_apb_ictl_handler(unsigned int irq, struct irq_desc *desc)
  26. {
  27. struct irq_chip *chip = irq_get_chip(irq);
  28. struct irq_chip_generic *gc = irq_get_handler_data(irq);
  29. struct irq_domain *d = gc->private;
  30. u32 stat;
  31. int n;
  32. chained_irq_enter(chip, desc);
  33. for (n = 0; n < gc->num_ct; n++) {
  34. stat = readl_relaxed(gc->reg_base +
  35. APB_INT_FINALSTATUS_L + 4 * n);
  36. while (stat) {
  37. u32 hwirq = ffs(stat) - 1;
  38. generic_handle_irq(irq_find_mapping(d,
  39. gc->irq_base + hwirq + 32 * n));
  40. stat &= ~(1 << hwirq);
  41. }
  42. }
  43. chained_irq_exit(chip, desc);
  44. }
  45. #ifdef CONFIG_PM
  46. static void dw_apb_ictl_resume(struct irq_data *d)
  47. {
  48. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  49. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  50. irq_gc_lock(gc);
  51. writel_relaxed(~0, gc->reg_base + ct->regs.enable);
  52. writel_relaxed(*ct->mask_cache, gc->reg_base + ct->regs.mask);
  53. irq_gc_unlock(gc);
  54. }
  55. #else
  56. #define dw_apb_ictl_resume NULL
  57. #endif /* CONFIG_PM */
  58. static int __init dw_apb_ictl_init(struct device_node *np,
  59. struct device_node *parent)
  60. {
  61. unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  62. struct resource r;
  63. struct irq_domain *domain;
  64. struct irq_chip_generic *gc;
  65. void __iomem *iobase;
  66. int ret, nrirqs, irq;
  67. u32 reg;
  68. /* Map the parent interrupt for the chained handler */
  69. irq = irq_of_parse_and_map(np, 0);
  70. if (irq <= 0) {
  71. pr_err("%s: unable to parse irq\n", np->full_name);
  72. return -EINVAL;
  73. }
  74. ret = of_address_to_resource(np, 0, &r);
  75. if (ret) {
  76. pr_err("%s: unable to get resource\n", np->full_name);
  77. return ret;
  78. }
  79. if (!request_mem_region(r.start, resource_size(&r), np->full_name)) {
  80. pr_err("%s: unable to request mem region\n", np->full_name);
  81. return -ENOMEM;
  82. }
  83. iobase = ioremap(r.start, resource_size(&r));
  84. if (!iobase) {
  85. pr_err("%s: unable to map resource\n", np->full_name);
  86. ret = -ENOMEM;
  87. goto err_release;
  88. }
  89. /*
  90. * DW IP can be configured to allow 2-64 irqs. We can determine
  91. * the number of irqs supported by writing into enable register
  92. * and look for bits not set, as corresponding flip-flops will
  93. * have been removed by sythesis tool.
  94. */
  95. /* mask and enable all interrupts */
  96. writel_relaxed(~0, iobase + APB_INT_MASK_L);
  97. writel_relaxed(~0, iobase + APB_INT_MASK_H);
  98. writel_relaxed(~0, iobase + APB_INT_ENABLE_L);
  99. writel_relaxed(~0, iobase + APB_INT_ENABLE_H);
  100. reg = readl_relaxed(iobase + APB_INT_ENABLE_H);
  101. if (reg)
  102. nrirqs = 32 + fls(reg);
  103. else
  104. nrirqs = fls(readl_relaxed(iobase + APB_INT_ENABLE_L));
  105. domain = irq_domain_add_linear(np, nrirqs,
  106. &irq_generic_chip_ops, NULL);
  107. if (!domain) {
  108. pr_err("%s: unable to add irq domain\n", np->full_name);
  109. ret = -ENOMEM;
  110. goto err_unmap;
  111. }
  112. ret = irq_alloc_domain_generic_chips(domain, 32, (nrirqs > 32) ? 2 : 1,
  113. np->name, handle_level_irq, clr, 0,
  114. IRQ_GC_MASK_CACHE_PER_TYPE |
  115. IRQ_GC_INIT_MASK_CACHE);
  116. if (ret) {
  117. pr_err("%s: unable to alloc irq domain gc\n", np->full_name);
  118. goto err_unmap;
  119. }
  120. gc = irq_get_domain_generic_chip(domain, 0);
  121. gc->private = domain;
  122. gc->reg_base = iobase;
  123. gc->chip_types[0].regs.mask = APB_INT_MASK_L;
  124. gc->chip_types[0].regs.enable = APB_INT_ENABLE_L;
  125. gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
  126. gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
  127. gc->chip_types[0].chip.irq_resume = dw_apb_ictl_resume;
  128. if (nrirqs > 32) {
  129. gc->chip_types[1].regs.mask = APB_INT_MASK_H;
  130. gc->chip_types[1].regs.enable = APB_INT_ENABLE_H;
  131. gc->chip_types[1].chip.irq_mask = irq_gc_mask_set_bit;
  132. gc->chip_types[1].chip.irq_unmask = irq_gc_mask_clr_bit;
  133. gc->chip_types[1].chip.irq_resume = dw_apb_ictl_resume;
  134. }
  135. irq_set_handler_data(irq, gc);
  136. irq_set_chained_handler(irq, dw_apb_ictl_handler);
  137. return 0;
  138. err_unmap:
  139. iounmap(iobase);
  140. err_release:
  141. release_mem_region(r.start, resource_size(&r));
  142. return ret;
  143. }
  144. IRQCHIP_DECLARE(dw_apb_ictl,
  145. "snps,dw-apb-ictl", dw_apb_ictl_init);