irq-aspeed-scu-ic.c 6.7 KB

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