irq-mst-intc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. /*
  3. * Copyright (c) 2020 MediaTek Inc.
  4. * Author Mark-PK Tsai <[email protected]>
  5. */
  6. #include <linux/interrupt.h>
  7. #include <linux/io.h>
  8. #include <linux/irq.h>
  9. #include <linux/irqchip.h>
  10. #include <linux/irqdomain.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/syscore_ops.h>
  17. #define MST_INTC_MAX_IRQS 64
  18. #define INTC_MASK 0x0
  19. #define INTC_REV_POLARITY 0x10
  20. #define INTC_EOI 0x20
  21. #ifdef CONFIG_PM_SLEEP
  22. static LIST_HEAD(mst_intc_list);
  23. #endif
  24. struct mst_intc_chip_data {
  25. raw_spinlock_t lock;
  26. unsigned int irq_start, nr_irqs;
  27. void __iomem *base;
  28. bool no_eoi;
  29. #ifdef CONFIG_PM_SLEEP
  30. struct list_head entry;
  31. u16 saved_polarity_conf[DIV_ROUND_UP(MST_INTC_MAX_IRQS, 16)];
  32. #endif
  33. };
  34. static void mst_set_irq(struct irq_data *d, u32 offset)
  35. {
  36. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  37. struct mst_intc_chip_data *cd = irq_data_get_irq_chip_data(d);
  38. u16 val, mask;
  39. unsigned long flags;
  40. mask = 1 << (hwirq % 16);
  41. offset += (hwirq / 16) * 4;
  42. raw_spin_lock_irqsave(&cd->lock, flags);
  43. val = readw_relaxed(cd->base + offset) | mask;
  44. writew_relaxed(val, cd->base + offset);
  45. raw_spin_unlock_irqrestore(&cd->lock, flags);
  46. }
  47. static void mst_clear_irq(struct irq_data *d, u32 offset)
  48. {
  49. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  50. struct mst_intc_chip_data *cd = irq_data_get_irq_chip_data(d);
  51. u16 val, mask;
  52. unsigned long flags;
  53. mask = 1 << (hwirq % 16);
  54. offset += (hwirq / 16) * 4;
  55. raw_spin_lock_irqsave(&cd->lock, flags);
  56. val = readw_relaxed(cd->base + offset) & ~mask;
  57. writew_relaxed(val, cd->base + offset);
  58. raw_spin_unlock_irqrestore(&cd->lock, flags);
  59. }
  60. static void mst_intc_mask_irq(struct irq_data *d)
  61. {
  62. mst_set_irq(d, INTC_MASK);
  63. irq_chip_mask_parent(d);
  64. }
  65. static void mst_intc_unmask_irq(struct irq_data *d)
  66. {
  67. mst_clear_irq(d, INTC_MASK);
  68. irq_chip_unmask_parent(d);
  69. }
  70. static void mst_intc_eoi_irq(struct irq_data *d)
  71. {
  72. struct mst_intc_chip_data *cd = irq_data_get_irq_chip_data(d);
  73. if (!cd->no_eoi)
  74. mst_set_irq(d, INTC_EOI);
  75. irq_chip_eoi_parent(d);
  76. }
  77. static int mst_irq_chip_set_type(struct irq_data *data, unsigned int type)
  78. {
  79. switch (type) {
  80. case IRQ_TYPE_LEVEL_LOW:
  81. case IRQ_TYPE_EDGE_FALLING:
  82. mst_set_irq(data, INTC_REV_POLARITY);
  83. break;
  84. case IRQ_TYPE_LEVEL_HIGH:
  85. case IRQ_TYPE_EDGE_RISING:
  86. mst_clear_irq(data, INTC_REV_POLARITY);
  87. break;
  88. default:
  89. return -EINVAL;
  90. }
  91. return irq_chip_set_type_parent(data, IRQ_TYPE_LEVEL_HIGH);
  92. }
  93. static struct irq_chip mst_intc_chip = {
  94. .name = "mst-intc",
  95. .irq_mask = mst_intc_mask_irq,
  96. .irq_unmask = mst_intc_unmask_irq,
  97. .irq_eoi = mst_intc_eoi_irq,
  98. .irq_get_irqchip_state = irq_chip_get_parent_state,
  99. .irq_set_irqchip_state = irq_chip_set_parent_state,
  100. .irq_set_affinity = irq_chip_set_affinity_parent,
  101. .irq_set_vcpu_affinity = irq_chip_set_vcpu_affinity_parent,
  102. .irq_set_type = mst_irq_chip_set_type,
  103. .irq_retrigger = irq_chip_retrigger_hierarchy,
  104. .flags = IRQCHIP_SET_TYPE_MASKED |
  105. IRQCHIP_SKIP_SET_WAKE |
  106. IRQCHIP_MASK_ON_SUSPEND,
  107. };
  108. #ifdef CONFIG_PM_SLEEP
  109. static void mst_intc_polarity_save(struct mst_intc_chip_data *cd)
  110. {
  111. int i;
  112. void __iomem *addr = cd->base + INTC_REV_POLARITY;
  113. for (i = 0; i < DIV_ROUND_UP(cd->nr_irqs, 16); i++)
  114. cd->saved_polarity_conf[i] = readw_relaxed(addr + i * 4);
  115. }
  116. static void mst_intc_polarity_restore(struct mst_intc_chip_data *cd)
  117. {
  118. int i;
  119. void __iomem *addr = cd->base + INTC_REV_POLARITY;
  120. for (i = 0; i < DIV_ROUND_UP(cd->nr_irqs, 16); i++)
  121. writew_relaxed(cd->saved_polarity_conf[i], addr + i * 4);
  122. }
  123. static void mst_irq_resume(void)
  124. {
  125. struct mst_intc_chip_data *cd;
  126. list_for_each_entry(cd, &mst_intc_list, entry)
  127. mst_intc_polarity_restore(cd);
  128. }
  129. static int mst_irq_suspend(void)
  130. {
  131. struct mst_intc_chip_data *cd;
  132. list_for_each_entry(cd, &mst_intc_list, entry)
  133. mst_intc_polarity_save(cd);
  134. return 0;
  135. }
  136. static struct syscore_ops mst_irq_syscore_ops = {
  137. .suspend = mst_irq_suspend,
  138. .resume = mst_irq_resume,
  139. };
  140. static int __init mst_irq_pm_init(void)
  141. {
  142. register_syscore_ops(&mst_irq_syscore_ops);
  143. return 0;
  144. }
  145. late_initcall(mst_irq_pm_init);
  146. #endif
  147. static int mst_intc_domain_translate(struct irq_domain *d,
  148. struct irq_fwspec *fwspec,
  149. unsigned long *hwirq,
  150. unsigned int *type)
  151. {
  152. struct mst_intc_chip_data *cd = d->host_data;
  153. if (is_of_node(fwspec->fwnode)) {
  154. if (fwspec->param_count != 3)
  155. return -EINVAL;
  156. /* No PPI should point to this domain */
  157. if (fwspec->param[0] != 0)
  158. return -EINVAL;
  159. if (fwspec->param[1] >= cd->nr_irqs)
  160. return -EINVAL;
  161. *hwirq = fwspec->param[1];
  162. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  163. return 0;
  164. }
  165. return -EINVAL;
  166. }
  167. static int mst_intc_domain_alloc(struct irq_domain *domain, unsigned int virq,
  168. unsigned int nr_irqs, void *data)
  169. {
  170. int i;
  171. irq_hw_number_t hwirq;
  172. struct irq_fwspec parent_fwspec, *fwspec = data;
  173. struct mst_intc_chip_data *cd = domain->host_data;
  174. /* Not GIC compliant */
  175. if (fwspec->param_count != 3)
  176. return -EINVAL;
  177. /* No PPI should point to this domain */
  178. if (fwspec->param[0])
  179. return -EINVAL;
  180. hwirq = fwspec->param[1];
  181. for (i = 0; i < nr_irqs; i++)
  182. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  183. &mst_intc_chip,
  184. domain->host_data);
  185. parent_fwspec = *fwspec;
  186. parent_fwspec.fwnode = domain->parent->fwnode;
  187. parent_fwspec.param[1] = cd->irq_start + hwirq;
  188. /*
  189. * mst-intc latch the interrupt request if it's edge triggered,
  190. * so the output signal to parent GIC is always level sensitive.
  191. * And if the irq signal is active low, configure it to active high
  192. * to meet GIC SPI spec in mst_irq_chip_set_type via REV_POLARITY bit.
  193. */
  194. parent_fwspec.param[2] = IRQ_TYPE_LEVEL_HIGH;
  195. return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, &parent_fwspec);
  196. }
  197. static const struct irq_domain_ops mst_intc_domain_ops = {
  198. .translate = mst_intc_domain_translate,
  199. .alloc = mst_intc_domain_alloc,
  200. .free = irq_domain_free_irqs_common,
  201. };
  202. static int __init mst_intc_of_init(struct device_node *dn,
  203. struct device_node *parent)
  204. {
  205. struct irq_domain *domain, *domain_parent;
  206. struct mst_intc_chip_data *cd;
  207. u32 irq_start, irq_end;
  208. domain_parent = irq_find_host(parent);
  209. if (!domain_parent) {
  210. pr_err("mst-intc: interrupt-parent not found\n");
  211. return -EINVAL;
  212. }
  213. if (of_property_read_u32_index(dn, "mstar,irqs-map-range", 0, &irq_start) ||
  214. of_property_read_u32_index(dn, "mstar,irqs-map-range", 1, &irq_end))
  215. return -EINVAL;
  216. cd = kzalloc(sizeof(*cd), GFP_KERNEL);
  217. if (!cd)
  218. return -ENOMEM;
  219. cd->base = of_iomap(dn, 0);
  220. if (!cd->base) {
  221. kfree(cd);
  222. return -ENOMEM;
  223. }
  224. cd->no_eoi = of_property_read_bool(dn, "mstar,intc-no-eoi");
  225. raw_spin_lock_init(&cd->lock);
  226. cd->irq_start = irq_start;
  227. cd->nr_irqs = irq_end - irq_start + 1;
  228. domain = irq_domain_add_hierarchy(domain_parent, 0, cd->nr_irqs, dn,
  229. &mst_intc_domain_ops, cd);
  230. if (!domain) {
  231. iounmap(cd->base);
  232. kfree(cd);
  233. return -ENOMEM;
  234. }
  235. #ifdef CONFIG_PM_SLEEP
  236. INIT_LIST_HEAD(&cd->entry);
  237. list_add_tail(&cd->entry, &mst_intc_list);
  238. #endif
  239. return 0;
  240. }
  241. IRQCHIP_DECLARE(mst_intc, "mstar,mst-intc", mst_intc_of_init);