irq-dw-apb-ictl.c 4.3 KB

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