irq-wpcm450-aic.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright 2021 Jonathan Neuschäfer
  3. #include <linux/irqchip.h>
  4. #include <linux/of_address.h>
  5. #include <linux/of_irq.h>
  6. #include <linux/printk.h>
  7. #include <asm/exception.h>
  8. #define AIC_SCR(x) ((x)*4) /* Source control registers */
  9. #define AIC_GEN 0x84 /* Interrupt group enable control register */
  10. #define AIC_GRSR 0x88 /* Interrupt group raw status register */
  11. #define AIC_IRSR 0x100 /* Interrupt raw status register */
  12. #define AIC_IASR 0x104 /* Interrupt active status register */
  13. #define AIC_ISR 0x108 /* Interrupt status register */
  14. #define AIC_IPER 0x10c /* Interrupt priority encoding register */
  15. #define AIC_ISNR 0x110 /* Interrupt source number register */
  16. #define AIC_IMR 0x114 /* Interrupt mask register */
  17. #define AIC_OISR 0x118 /* Output interrupt status register */
  18. #define AIC_MECR 0x120 /* Mask enable command register */
  19. #define AIC_MDCR 0x124 /* Mask disable command register */
  20. #define AIC_SSCR 0x128 /* Source set command register */
  21. #define AIC_SCCR 0x12c /* Source clear command register */
  22. #define AIC_EOSCR 0x130 /* End of service command register */
  23. #define AIC_SCR_SRCTYPE_LOW_LEVEL (0 << 6)
  24. #define AIC_SCR_SRCTYPE_HIGH_LEVEL (1 << 6)
  25. #define AIC_SCR_SRCTYPE_NEG_EDGE (2 << 6)
  26. #define AIC_SCR_SRCTYPE_POS_EDGE (3 << 6)
  27. #define AIC_SCR_PRIORITY(x) (x)
  28. #define AIC_SCR_PRIORITY_MASK 0x7
  29. #define AIC_NUM_IRQS 32
  30. struct wpcm450_aic {
  31. void __iomem *regs;
  32. struct irq_domain *domain;
  33. };
  34. static struct wpcm450_aic *aic;
  35. static void wpcm450_aic_init_hw(void)
  36. {
  37. int i;
  38. /* Disable (mask) all interrupts */
  39. writel(0xffffffff, aic->regs + AIC_MDCR);
  40. /*
  41. * Make sure the interrupt controller is ready to serve new interrupts.
  42. * Reading from IPER indicates that the nIRQ signal may be deasserted,
  43. * and writing to EOSCR indicates that interrupt handling has finished.
  44. */
  45. readl(aic->regs + AIC_IPER);
  46. writel(0, aic->regs + AIC_EOSCR);
  47. /* Initialize trigger mode and priority of each interrupt source */
  48. for (i = 0; i < AIC_NUM_IRQS; i++)
  49. writel(AIC_SCR_SRCTYPE_HIGH_LEVEL | AIC_SCR_PRIORITY(7),
  50. aic->regs + AIC_SCR(i));
  51. }
  52. static void __exception_irq_entry wpcm450_aic_handle_irq(struct pt_regs *regs)
  53. {
  54. int hwirq;
  55. /* Determine the interrupt source */
  56. /* Read IPER to signal that nIRQ can be de-asserted */
  57. hwirq = readl(aic->regs + AIC_IPER) / 4;
  58. generic_handle_domain_irq(aic->domain, hwirq);
  59. }
  60. static void wpcm450_aic_eoi(struct irq_data *d)
  61. {
  62. /* Signal end-of-service */
  63. writel(0, aic->regs + AIC_EOSCR);
  64. }
  65. static void wpcm450_aic_mask(struct irq_data *d)
  66. {
  67. unsigned int mask = BIT(d->hwirq);
  68. /* Disable (mask) the interrupt */
  69. writel(mask, aic->regs + AIC_MDCR);
  70. }
  71. static void wpcm450_aic_unmask(struct irq_data *d)
  72. {
  73. unsigned int mask = BIT(d->hwirq);
  74. /* Enable (unmask) the interrupt */
  75. writel(mask, aic->regs + AIC_MECR);
  76. }
  77. static int wpcm450_aic_set_type(struct irq_data *d, unsigned int flow_type)
  78. {
  79. /*
  80. * The hardware supports high/low level, as well as rising/falling edge
  81. * modes, and the DT binding accommodates for that, but as long as
  82. * other modes than high level mode are not used and can't be tested,
  83. * they are rejected in this driver.
  84. */
  85. if ((flow_type & IRQ_TYPE_SENSE_MASK) != IRQ_TYPE_LEVEL_HIGH)
  86. return -EINVAL;
  87. return 0;
  88. }
  89. static struct irq_chip wpcm450_aic_chip = {
  90. .name = "wpcm450-aic",
  91. .irq_eoi = wpcm450_aic_eoi,
  92. .irq_mask = wpcm450_aic_mask,
  93. .irq_unmask = wpcm450_aic_unmask,
  94. .irq_set_type = wpcm450_aic_set_type,
  95. };
  96. static int wpcm450_aic_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hwirq)
  97. {
  98. if (hwirq >= AIC_NUM_IRQS)
  99. return -EPERM;
  100. irq_set_chip_and_handler(irq, &wpcm450_aic_chip, handle_fasteoi_irq);
  101. irq_set_chip_data(irq, aic);
  102. irq_set_probe(irq);
  103. return 0;
  104. }
  105. static const struct irq_domain_ops wpcm450_aic_ops = {
  106. .map = wpcm450_aic_map,
  107. .xlate = irq_domain_xlate_twocell,
  108. };
  109. static int __init wpcm450_aic_of_init(struct device_node *node,
  110. struct device_node *parent)
  111. {
  112. if (parent)
  113. return -EINVAL;
  114. aic = kzalloc(sizeof(*aic), GFP_KERNEL);
  115. if (!aic)
  116. return -ENOMEM;
  117. aic->regs = of_iomap(node, 0);
  118. if (!aic->regs) {
  119. pr_err("Failed to map WPCM450 AIC registers\n");
  120. kfree(aic);
  121. return -ENOMEM;
  122. }
  123. wpcm450_aic_init_hw();
  124. set_handle_irq(wpcm450_aic_handle_irq);
  125. aic->domain = irq_domain_add_linear(node, AIC_NUM_IRQS, &wpcm450_aic_ops, aic);
  126. return 0;
  127. }
  128. IRQCHIP_DECLARE(wpcm450_aic, "nuvoton,wpcm450-aic", wpcm450_aic_of_init);