irq-dw-apb-ictl.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. static int __init dw_apb_ictl_init(struct device_node *np,
  46. struct device_node *parent)
  47. {
  48. unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  49. struct resource r;
  50. struct irq_domain *domain;
  51. struct irq_chip_generic *gc;
  52. void __iomem *iobase;
  53. int ret, nrirqs, irq;
  54. u32 reg;
  55. /* Map the parent interrupt for the chained handler */
  56. irq = irq_of_parse_and_map(np, 0);
  57. if (irq <= 0) {
  58. pr_err("%s: unable to parse irq\n", np->full_name);
  59. return -EINVAL;
  60. }
  61. ret = of_address_to_resource(np, 0, &r);
  62. if (ret) {
  63. pr_err("%s: unable to get resource\n", np->full_name);
  64. return ret;
  65. }
  66. if (!request_mem_region(r.start, resource_size(&r), np->full_name)) {
  67. pr_err("%s: unable to request mem region\n", np->full_name);
  68. return -ENOMEM;
  69. }
  70. iobase = ioremap(r.start, resource_size(&r));
  71. if (!iobase) {
  72. pr_err("%s: unable to map resource\n", np->full_name);
  73. ret = -ENOMEM;
  74. goto err_release;
  75. }
  76. /*
  77. * DW IP can be configured to allow 2-64 irqs. We can determine
  78. * the number of irqs supported by writing into enable register
  79. * and look for bits not set, as corresponding flip-flops will
  80. * have been removed by sythesis tool.
  81. */
  82. /* mask and enable all interrupts */
  83. writel(~0, iobase + APB_INT_MASK_L);
  84. writel(~0, iobase + APB_INT_MASK_H);
  85. writel(~0, iobase + APB_INT_ENABLE_L);
  86. writel(~0, iobase + APB_INT_ENABLE_H);
  87. reg = readl(iobase + APB_INT_ENABLE_H);
  88. if (reg)
  89. nrirqs = 32 + fls(reg);
  90. else
  91. nrirqs = fls(readl(iobase + APB_INT_ENABLE_L));
  92. domain = irq_domain_add_linear(np, nrirqs,
  93. &irq_generic_chip_ops, NULL);
  94. if (!domain) {
  95. pr_err("%s: unable to add irq domain\n", np->full_name);
  96. ret = -ENOMEM;
  97. goto err_unmap;
  98. }
  99. ret = irq_alloc_domain_generic_chips(domain, 32, (nrirqs > 32) ? 2 : 1,
  100. np->name, handle_level_irq, clr, 0,
  101. IRQ_GC_INIT_MASK_CACHE);
  102. if (ret) {
  103. pr_err("%s: unable to alloc irq domain gc\n", np->full_name);
  104. goto err_unmap;
  105. }
  106. gc = irq_get_domain_generic_chip(domain, 0);
  107. gc->private = domain;
  108. gc->reg_base = iobase;
  109. gc->chip_types[0].regs.mask = APB_INT_MASK_L;
  110. gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
  111. gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
  112. if (nrirqs > 32) {
  113. gc->chip_types[1].regs.mask = APB_INT_MASK_H;
  114. gc->chip_types[1].chip.irq_mask = irq_gc_mask_set_bit;
  115. gc->chip_types[1].chip.irq_unmask = irq_gc_mask_clr_bit;
  116. }
  117. irq_set_handler_data(irq, gc);
  118. irq_set_chained_handler(irq, dw_apb_ictl_handler);
  119. return 0;
  120. err_unmap:
  121. iounmap(iobase);
  122. err_release:
  123. release_mem_region(r.start, resource_size(&r));
  124. return ret;
  125. }
  126. IRQCHIP_DECLARE(dw_apb_ictl,
  127. "snps,dw-apb-ictl", dw_apb_ictl_init);