irq-loongson-pch-lpc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Loongson LPC Interrupt Controller support
  4. *
  5. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  6. */
  7. #define pr_fmt(fmt) "lpc: " fmt
  8. #include <linux/interrupt.h>
  9. #include <linux/irq.h>
  10. #include <linux/irqchip.h>
  11. #include <linux/irqchip/chained_irq.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/kernel.h>
  14. /* Registers */
  15. #define LPC_INT_CTL 0x00
  16. #define LPC_INT_ENA 0x04
  17. #define LPC_INT_STS 0x08
  18. #define LPC_INT_CLR 0x0c
  19. #define LPC_INT_POL 0x10
  20. #define LPC_COUNT 16
  21. /* LPC_INT_CTL */
  22. #define LPC_INT_CTL_EN BIT(31)
  23. struct pch_lpc {
  24. void __iomem *base;
  25. struct irq_domain *lpc_domain;
  26. raw_spinlock_t lpc_lock;
  27. u32 saved_reg_ctl;
  28. u32 saved_reg_ena;
  29. u32 saved_reg_pol;
  30. };
  31. struct fwnode_handle *pch_lpc_handle;
  32. static void lpc_irq_ack(struct irq_data *d)
  33. {
  34. unsigned long flags;
  35. struct pch_lpc *priv = d->domain->host_data;
  36. raw_spin_lock_irqsave(&priv->lpc_lock, flags);
  37. writel(0x1 << d->hwirq, priv->base + LPC_INT_CLR);
  38. raw_spin_unlock_irqrestore(&priv->lpc_lock, flags);
  39. }
  40. static void lpc_irq_mask(struct irq_data *d)
  41. {
  42. unsigned long flags;
  43. struct pch_lpc *priv = d->domain->host_data;
  44. raw_spin_lock_irqsave(&priv->lpc_lock, flags);
  45. writel(readl(priv->base + LPC_INT_ENA) & (~(0x1 << (d->hwirq))),
  46. priv->base + LPC_INT_ENA);
  47. raw_spin_unlock_irqrestore(&priv->lpc_lock, flags);
  48. }
  49. static void lpc_irq_unmask(struct irq_data *d)
  50. {
  51. unsigned long flags;
  52. struct pch_lpc *priv = d->domain->host_data;
  53. raw_spin_lock_irqsave(&priv->lpc_lock, flags);
  54. writel(readl(priv->base + LPC_INT_ENA) | (0x1 << (d->hwirq)),
  55. priv->base + LPC_INT_ENA);
  56. raw_spin_unlock_irqrestore(&priv->lpc_lock, flags);
  57. }
  58. static int lpc_irq_set_type(struct irq_data *d, unsigned int type)
  59. {
  60. u32 val;
  61. u32 mask = 0x1 << (d->hwirq);
  62. struct pch_lpc *priv = d->domain->host_data;
  63. if (!(type & IRQ_TYPE_LEVEL_MASK))
  64. return 0;
  65. val = readl(priv->base + LPC_INT_POL);
  66. if (type == IRQ_TYPE_LEVEL_HIGH)
  67. val |= mask;
  68. else
  69. val &= ~mask;
  70. writel(val, priv->base + LPC_INT_POL);
  71. return 0;
  72. }
  73. static const struct irq_chip pch_lpc_irq_chip = {
  74. .name = "PCH LPC",
  75. .irq_mask = lpc_irq_mask,
  76. .irq_unmask = lpc_irq_unmask,
  77. .irq_ack = lpc_irq_ack,
  78. .irq_set_type = lpc_irq_set_type,
  79. .flags = IRQCHIP_SKIP_SET_WAKE,
  80. };
  81. static void lpc_irq_dispatch(struct irq_desc *desc)
  82. {
  83. u32 pending, bit;
  84. struct irq_chip *chip = irq_desc_get_chip(desc);
  85. struct pch_lpc *priv = irq_desc_get_handler_data(desc);
  86. chained_irq_enter(chip, desc);
  87. pending = readl(priv->base + LPC_INT_ENA);
  88. pending &= readl(priv->base + LPC_INT_STS);
  89. if (!pending)
  90. spurious_interrupt();
  91. while (pending) {
  92. bit = __ffs(pending);
  93. generic_handle_domain_irq(priv->lpc_domain, bit);
  94. pending &= ~BIT(bit);
  95. }
  96. chained_irq_exit(chip, desc);
  97. }
  98. static int pch_lpc_map(struct irq_domain *d, unsigned int irq,
  99. irq_hw_number_t hw)
  100. {
  101. irq_set_chip_and_handler(irq, &pch_lpc_irq_chip, handle_level_irq);
  102. return 0;
  103. }
  104. static const struct irq_domain_ops pch_lpc_domain_ops = {
  105. .map = pch_lpc_map,
  106. .translate = irq_domain_translate_twocell,
  107. };
  108. static void pch_lpc_reset(struct pch_lpc *priv)
  109. {
  110. /* Enable the LPC interrupt, bit31: en bit30: edge */
  111. writel(LPC_INT_CTL_EN, priv->base + LPC_INT_CTL);
  112. writel(0, priv->base + LPC_INT_ENA);
  113. /* Clear all 18-bit interrpt bit */
  114. writel(GENMASK(17, 0), priv->base + LPC_INT_CLR);
  115. }
  116. static int pch_lpc_disabled(struct pch_lpc *priv)
  117. {
  118. return (readl(priv->base + LPC_INT_ENA) == 0xffffffff) &&
  119. (readl(priv->base + LPC_INT_STS) == 0xffffffff);
  120. }
  121. int __init pch_lpc_acpi_init(struct irq_domain *parent,
  122. struct acpi_madt_lpc_pic *acpi_pchlpc)
  123. {
  124. int parent_irq;
  125. struct pch_lpc *priv;
  126. struct irq_fwspec fwspec;
  127. struct fwnode_handle *irq_handle;
  128. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  129. if (!priv)
  130. return -ENOMEM;
  131. raw_spin_lock_init(&priv->lpc_lock);
  132. priv->base = ioremap(acpi_pchlpc->address, acpi_pchlpc->size);
  133. if (!priv->base)
  134. goto free_priv;
  135. if (pch_lpc_disabled(priv)) {
  136. pr_err("Failed to get LPC status\n");
  137. goto iounmap_base;
  138. }
  139. irq_handle = irq_domain_alloc_named_fwnode("lpcintc");
  140. if (!irq_handle) {
  141. pr_err("Unable to allocate domain handle\n");
  142. goto iounmap_base;
  143. }
  144. priv->lpc_domain = irq_domain_create_linear(irq_handle, LPC_COUNT,
  145. &pch_lpc_domain_ops, priv);
  146. if (!priv->lpc_domain) {
  147. pr_err("Failed to create IRQ domain\n");
  148. goto free_irq_handle;
  149. }
  150. pch_lpc_reset(priv);
  151. fwspec.fwnode = parent->fwnode;
  152. fwspec.param[0] = acpi_pchlpc->cascade + GSI_MIN_PCH_IRQ;
  153. fwspec.param[1] = IRQ_TYPE_LEVEL_HIGH;
  154. fwspec.param_count = 2;
  155. parent_irq = irq_create_fwspec_mapping(&fwspec);
  156. irq_set_chained_handler_and_data(parent_irq, lpc_irq_dispatch, priv);
  157. pch_lpc_handle = irq_handle;
  158. return 0;
  159. free_irq_handle:
  160. irq_domain_free_fwnode(irq_handle);
  161. iounmap_base:
  162. iounmap(priv->base);
  163. free_priv:
  164. kfree(priv);
  165. return -ENOMEM;
  166. }