irq-aspeed-scu-ic.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Aspeed AST24XX, AST25XX, and AST26XX SCU Interrupt Controller
  4. * Copyright 2019 IBM Corporation
  5. *
  6. * Eddie James <[email protected]>
  7. */
  8. #include <linux/bitops.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/mfd/syscon.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/regmap.h>
  16. #define ASPEED_SCU_IC_REG 0x018
  17. #define ASPEED_SCU_IC_SHIFT 0
  18. #define ASPEED_SCU_IC_ENABLE GENMASK(6, ASPEED_SCU_IC_SHIFT)
  19. #define ASPEED_SCU_IC_NUM_IRQS 7
  20. #define ASPEED_SCU_IC_STATUS_SHIFT 16
  21. #define ASPEED_AST2600_SCU_IC0_REG 0x560
  22. #define ASPEED_AST2600_SCU_IC0_SHIFT 0
  23. #define ASPEED_AST2600_SCU_IC0_ENABLE \
  24. GENMASK(5, ASPEED_AST2600_SCU_IC0_SHIFT)
  25. #define ASPEED_AST2600_SCU_IC0_NUM_IRQS 6
  26. #define ASPEED_AST2600_SCU_IC1_REG 0x570
  27. #define ASPEED_AST2600_SCU_IC1_SHIFT 4
  28. #define ASPEED_AST2600_SCU_IC1_ENABLE \
  29. GENMASK(5, ASPEED_AST2600_SCU_IC1_SHIFT)
  30. #define ASPEED_AST2600_SCU_IC1_NUM_IRQS 2
  31. struct aspeed_scu_ic {
  32. unsigned long irq_enable;
  33. unsigned long irq_shift;
  34. unsigned int num_irqs;
  35. unsigned int reg;
  36. struct regmap *scu;
  37. struct irq_domain *irq_domain;
  38. };
  39. static void aspeed_scu_ic_irq_handler(struct irq_desc *desc)
  40. {
  41. unsigned int sts;
  42. unsigned long bit;
  43. unsigned long enabled;
  44. unsigned long max;
  45. unsigned long status;
  46. struct aspeed_scu_ic *scu_ic = irq_desc_get_handler_data(desc);
  47. struct irq_chip *chip = irq_desc_get_chip(desc);
  48. unsigned int mask = scu_ic->irq_enable << ASPEED_SCU_IC_STATUS_SHIFT;
  49. chained_irq_enter(chip, desc);
  50. /*
  51. * The SCU IC has just one register to control its operation and read
  52. * status. The interrupt enable bits occupy the lower 16 bits of the
  53. * register, while the interrupt status bits occupy the upper 16 bits.
  54. * The status bit for a given interrupt is always 16 bits shifted from
  55. * the enable bit for the same interrupt.
  56. * Therefore, perform the IRQ operations in the enable bit space by
  57. * shifting the status down to get the mapping and then back up to
  58. * clear the bit.
  59. */
  60. regmap_read(scu_ic->scu, scu_ic->reg, &sts);
  61. enabled = sts & scu_ic->irq_enable;
  62. status = (sts >> ASPEED_SCU_IC_STATUS_SHIFT) & enabled;
  63. bit = scu_ic->irq_shift;
  64. max = scu_ic->num_irqs + bit;
  65. for_each_set_bit_from(bit, &status, max) {
  66. generic_handle_domain_irq(scu_ic->irq_domain,
  67. bit - scu_ic->irq_shift);
  68. regmap_write_bits(scu_ic->scu, scu_ic->reg, mask,
  69. BIT(bit + ASPEED_SCU_IC_STATUS_SHIFT));
  70. }
  71. chained_irq_exit(chip, desc);
  72. }
  73. static void aspeed_scu_ic_irq_mask(struct irq_data *data)
  74. {
  75. struct aspeed_scu_ic *scu_ic = irq_data_get_irq_chip_data(data);
  76. unsigned int mask = BIT(data->hwirq + scu_ic->irq_shift) |
  77. (scu_ic->irq_enable << ASPEED_SCU_IC_STATUS_SHIFT);
  78. /*
  79. * Status bits are cleared by writing 1. In order to prevent the mask
  80. * operation from clearing the status bits, they should be under the
  81. * mask and written with 0.
  82. */
  83. regmap_update_bits(scu_ic->scu, scu_ic->reg, mask, 0);
  84. }
  85. static void aspeed_scu_ic_irq_unmask(struct irq_data *data)
  86. {
  87. struct aspeed_scu_ic *scu_ic = irq_data_get_irq_chip_data(data);
  88. unsigned int bit = BIT(data->hwirq + scu_ic->irq_shift);
  89. unsigned int mask = bit |
  90. (scu_ic->irq_enable << ASPEED_SCU_IC_STATUS_SHIFT);
  91. /*
  92. * Status bits are cleared by writing 1. In order to prevent the unmask
  93. * operation from clearing the status bits, they should be under the
  94. * mask and written with 0.
  95. */
  96. regmap_update_bits(scu_ic->scu, scu_ic->reg, mask, bit);
  97. }
  98. static int aspeed_scu_ic_irq_set_affinity(struct irq_data *data,
  99. const struct cpumask *dest,
  100. bool force)
  101. {
  102. return -EINVAL;
  103. }
  104. static struct irq_chip aspeed_scu_ic_chip = {
  105. .name = "aspeed-scu-ic",
  106. .irq_mask = aspeed_scu_ic_irq_mask,
  107. .irq_unmask = aspeed_scu_ic_irq_unmask,
  108. .irq_set_affinity = aspeed_scu_ic_irq_set_affinity,
  109. };
  110. static int aspeed_scu_ic_map(struct irq_domain *domain, unsigned int irq,
  111. irq_hw_number_t hwirq)
  112. {
  113. irq_set_chip_and_handler(irq, &aspeed_scu_ic_chip, handle_level_irq);
  114. irq_set_chip_data(irq, domain->host_data);
  115. return 0;
  116. }
  117. static const struct irq_domain_ops aspeed_scu_ic_domain_ops = {
  118. .map = aspeed_scu_ic_map,
  119. };
  120. static int aspeed_scu_ic_of_init_common(struct aspeed_scu_ic *scu_ic,
  121. struct device_node *node)
  122. {
  123. int irq;
  124. int rc = 0;
  125. if (!node->parent) {
  126. rc = -ENODEV;
  127. goto err;
  128. }
  129. scu_ic->scu = syscon_node_to_regmap(node->parent);
  130. if (IS_ERR(scu_ic->scu)) {
  131. rc = PTR_ERR(scu_ic->scu);
  132. goto err;
  133. }
  134. irq = irq_of_parse_and_map(node, 0);
  135. if (!irq) {
  136. rc = -EINVAL;
  137. goto err;
  138. }
  139. scu_ic->irq_domain = irq_domain_add_linear(node, scu_ic->num_irqs,
  140. &aspeed_scu_ic_domain_ops,
  141. scu_ic);
  142. if (!scu_ic->irq_domain) {
  143. rc = -ENOMEM;
  144. goto err;
  145. }
  146. irq_set_chained_handler_and_data(irq, aspeed_scu_ic_irq_handler,
  147. scu_ic);
  148. return 0;
  149. err:
  150. kfree(scu_ic);
  151. return rc;
  152. }
  153. static int __init aspeed_scu_ic_of_init(struct device_node *node,
  154. struct device_node *parent)
  155. {
  156. struct aspeed_scu_ic *scu_ic = kzalloc(sizeof(*scu_ic), GFP_KERNEL);
  157. if (!scu_ic)
  158. return -ENOMEM;
  159. scu_ic->irq_enable = ASPEED_SCU_IC_ENABLE;
  160. scu_ic->irq_shift = ASPEED_SCU_IC_SHIFT;
  161. scu_ic->num_irqs = ASPEED_SCU_IC_NUM_IRQS;
  162. scu_ic->reg = ASPEED_SCU_IC_REG;
  163. return aspeed_scu_ic_of_init_common(scu_ic, node);
  164. }
  165. static int __init aspeed_ast2600_scu_ic0_of_init(struct device_node *node,
  166. struct device_node *parent)
  167. {
  168. struct aspeed_scu_ic *scu_ic = kzalloc(sizeof(*scu_ic), GFP_KERNEL);
  169. if (!scu_ic)
  170. return -ENOMEM;
  171. scu_ic->irq_enable = ASPEED_AST2600_SCU_IC0_ENABLE;
  172. scu_ic->irq_shift = ASPEED_AST2600_SCU_IC0_SHIFT;
  173. scu_ic->num_irqs = ASPEED_AST2600_SCU_IC0_NUM_IRQS;
  174. scu_ic->reg = ASPEED_AST2600_SCU_IC0_REG;
  175. return aspeed_scu_ic_of_init_common(scu_ic, node);
  176. }
  177. static int __init aspeed_ast2600_scu_ic1_of_init(struct device_node *node,
  178. struct device_node *parent)
  179. {
  180. struct aspeed_scu_ic *scu_ic = kzalloc(sizeof(*scu_ic), GFP_KERNEL);
  181. if (!scu_ic)
  182. return -ENOMEM;
  183. scu_ic->irq_enable = ASPEED_AST2600_SCU_IC1_ENABLE;
  184. scu_ic->irq_shift = ASPEED_AST2600_SCU_IC1_SHIFT;
  185. scu_ic->num_irqs = ASPEED_AST2600_SCU_IC1_NUM_IRQS;
  186. scu_ic->reg = ASPEED_AST2600_SCU_IC1_REG;
  187. return aspeed_scu_ic_of_init_common(scu_ic, node);
  188. }
  189. IRQCHIP_DECLARE(ast2400_scu_ic, "aspeed,ast2400-scu-ic", aspeed_scu_ic_of_init);
  190. IRQCHIP_DECLARE(ast2500_scu_ic, "aspeed,ast2500-scu-ic", aspeed_scu_ic_of_init);
  191. IRQCHIP_DECLARE(ast2600_scu_ic0, "aspeed,ast2600-scu-ic0",
  192. aspeed_ast2600_scu_ic0_of_init);
  193. IRQCHIP_DECLARE(ast2600_scu_ic1, "aspeed,ast2600-scu-ic1",
  194. aspeed_ast2600_scu_ic1_of_init);