irq-mvebu-icu.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (C) 2017 Marvell
  3. *
  4. * Hanna Hawa <hannah@marvell.com>
  5. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/irqchip.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/kernel.h>
  16. #include <linux/msi.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #include <dt-bindings/interrupt-controller/mvebu-icu.h>
  21. /* ICU registers */
  22. #define ICU_SETSPI_NSR_AL 0x10
  23. #define ICU_SETSPI_NSR_AH 0x14
  24. #define ICU_CLRSPI_NSR_AL 0x18
  25. #define ICU_CLRSPI_NSR_AH 0x1c
  26. #define ICU_INT_CFG(x) (0x100 + 4 * (x))
  27. #define ICU_INT_ENABLE BIT(24)
  28. #define ICU_IS_EDGE BIT(28)
  29. #define ICU_GROUP_SHIFT 29
  30. /* ICU definitions */
  31. #define ICU_MAX_IRQS 207
  32. #define ICU_SATA0_ICU_ID 109
  33. #define ICU_SATA1_ICU_ID 107
  34. struct mvebu_icu {
  35. struct irq_chip irq_chip;
  36. void __iomem *base;
  37. struct irq_domain *domain;
  38. struct device *dev;
  39. atomic_t initialized;
  40. };
  41. struct mvebu_icu_irq_data {
  42. struct mvebu_icu *icu;
  43. unsigned int icu_group;
  44. unsigned int type;
  45. };
  46. static void mvebu_icu_init(struct mvebu_icu *icu, struct msi_msg *msg)
  47. {
  48. if (atomic_cmpxchg(&icu->initialized, false, true))
  49. return;
  50. /* Set Clear/Set ICU SPI message address in AP */
  51. writel_relaxed(msg[0].address_hi, icu->base + ICU_SETSPI_NSR_AH);
  52. writel_relaxed(msg[0].address_lo, icu->base + ICU_SETSPI_NSR_AL);
  53. writel_relaxed(msg[1].address_hi, icu->base + ICU_CLRSPI_NSR_AH);
  54. writel_relaxed(msg[1].address_lo, icu->base + ICU_CLRSPI_NSR_AL);
  55. }
  56. static void mvebu_icu_write_msg(struct msi_desc *desc, struct msi_msg *msg)
  57. {
  58. struct irq_data *d = irq_get_irq_data(desc->irq);
  59. struct mvebu_icu_irq_data *icu_irqd = d->chip_data;
  60. struct mvebu_icu *icu = icu_irqd->icu;
  61. unsigned int icu_int;
  62. if (msg->address_lo || msg->address_hi) {
  63. /* One off initialization */
  64. mvebu_icu_init(icu, msg);
  65. /* Configure the ICU with irq number & type */
  66. icu_int = msg->data | ICU_INT_ENABLE;
  67. if (icu_irqd->type & IRQ_TYPE_EDGE_RISING)
  68. icu_int |= ICU_IS_EDGE;
  69. icu_int |= icu_irqd->icu_group << ICU_GROUP_SHIFT;
  70. } else {
  71. /* De-configure the ICU */
  72. icu_int = 0;
  73. }
  74. writel_relaxed(icu_int, icu->base + ICU_INT_CFG(d->hwirq));
  75. /*
  76. * The SATA unit has 2 ports, and a dedicated ICU entry per
  77. * port. The ahci sata driver supports only one irq interrupt
  78. * per SATA unit. To solve this conflict, we configure the 2
  79. * SATA wired interrupts in the south bridge into 1 GIC
  80. * interrupt in the north bridge. Even if only a single port
  81. * is enabled, if sata node is enabled, both interrupts are
  82. * configured (regardless of which port is actually in use).
  83. */
  84. if (d->hwirq == ICU_SATA0_ICU_ID || d->hwirq == ICU_SATA1_ICU_ID) {
  85. writel_relaxed(icu_int,
  86. icu->base + ICU_INT_CFG(ICU_SATA0_ICU_ID));
  87. writel_relaxed(icu_int,
  88. icu->base + ICU_INT_CFG(ICU_SATA1_ICU_ID));
  89. }
  90. }
  91. static int
  92. mvebu_icu_irq_domain_translate(struct irq_domain *d, struct irq_fwspec *fwspec,
  93. unsigned long *hwirq, unsigned int *type)
  94. {
  95. struct mvebu_icu *icu = platform_msi_get_host_data(d);
  96. unsigned int icu_group;
  97. /* Check the count of the parameters in dt */
  98. if (WARN_ON(fwspec->param_count < 3)) {
  99. dev_err(icu->dev, "wrong ICU parameter count %d\n",
  100. fwspec->param_count);
  101. return -EINVAL;
  102. }
  103. /* Only ICU group type is handled */
  104. icu_group = fwspec->param[0];
  105. if (icu_group != ICU_GRP_NSR && icu_group != ICU_GRP_SR &&
  106. icu_group != ICU_GRP_SEI && icu_group != ICU_GRP_REI) {
  107. dev_err(icu->dev, "wrong ICU group type %x\n", icu_group);
  108. return -EINVAL;
  109. }
  110. *hwirq = fwspec->param[1];
  111. if (*hwirq >= ICU_MAX_IRQS) {
  112. dev_err(icu->dev, "invalid interrupt number %ld\n", *hwirq);
  113. return -EINVAL;
  114. }
  115. /* Mask the type to prevent wrong DT configuration */
  116. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  117. return 0;
  118. }
  119. static int
  120. mvebu_icu_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  121. unsigned int nr_irqs, void *args)
  122. {
  123. int err;
  124. unsigned long hwirq;
  125. struct irq_fwspec *fwspec = args;
  126. struct mvebu_icu *icu = platform_msi_get_host_data(domain);
  127. struct mvebu_icu_irq_data *icu_irqd;
  128. icu_irqd = kmalloc(sizeof(*icu_irqd), GFP_KERNEL);
  129. if (!icu_irqd)
  130. return -ENOMEM;
  131. err = mvebu_icu_irq_domain_translate(domain, fwspec, &hwirq,
  132. &icu_irqd->type);
  133. if (err) {
  134. dev_err(icu->dev, "failed to translate ICU parameters\n");
  135. goto free_irqd;
  136. }
  137. icu_irqd->icu_group = fwspec->param[0];
  138. icu_irqd->icu = icu;
  139. err = platform_msi_domain_alloc(domain, virq, nr_irqs);
  140. if (err) {
  141. dev_err(icu->dev, "failed to allocate ICU interrupt in parent domain\n");
  142. goto free_irqd;
  143. }
  144. /* Make sure there is no interrupt left pending by the firmware */
  145. err = irq_set_irqchip_state(virq, IRQCHIP_STATE_PENDING, false);
  146. if (err)
  147. goto free_msi;
  148. err = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  149. &icu->irq_chip, icu_irqd);
  150. if (err) {
  151. dev_err(icu->dev, "failed to set the data to IRQ domain\n");
  152. goto free_msi;
  153. }
  154. return 0;
  155. free_msi:
  156. platform_msi_domain_free(domain, virq, nr_irqs);
  157. free_irqd:
  158. kfree(icu_irqd);
  159. return err;
  160. }
  161. static void
  162. mvebu_icu_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  163. unsigned int nr_irqs)
  164. {
  165. struct irq_data *d = irq_get_irq_data(virq);
  166. struct mvebu_icu_irq_data *icu_irqd = d->chip_data;
  167. kfree(icu_irqd);
  168. platform_msi_domain_free(domain, virq, nr_irqs);
  169. }
  170. static const struct irq_domain_ops mvebu_icu_domain_ops = {
  171. .translate = mvebu_icu_irq_domain_translate,
  172. .alloc = mvebu_icu_irq_domain_alloc,
  173. .free = mvebu_icu_irq_domain_free,
  174. };
  175. static int mvebu_icu_probe(struct platform_device *pdev)
  176. {
  177. struct mvebu_icu *icu;
  178. struct device_node *node = pdev->dev.of_node;
  179. struct device_node *gicp_dn;
  180. struct resource *res;
  181. int i;
  182. icu = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_icu),
  183. GFP_KERNEL);
  184. if (!icu)
  185. return -ENOMEM;
  186. icu->dev = &pdev->dev;
  187. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  188. icu->base = devm_ioremap_resource(&pdev->dev, res);
  189. if (IS_ERR(icu->base)) {
  190. dev_err(&pdev->dev, "Failed to map icu base address.\n");
  191. return PTR_ERR(icu->base);
  192. }
  193. icu->irq_chip.name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  194. "ICU.%x",
  195. (unsigned int)res->start);
  196. if (!icu->irq_chip.name)
  197. return -ENOMEM;
  198. icu->irq_chip.irq_mask = irq_chip_mask_parent;
  199. icu->irq_chip.irq_unmask = irq_chip_unmask_parent;
  200. icu->irq_chip.irq_eoi = irq_chip_eoi_parent;
  201. icu->irq_chip.irq_set_type = irq_chip_set_type_parent;
  202. #ifdef CONFIG_SMP
  203. icu->irq_chip.irq_set_affinity = irq_chip_set_affinity_parent;
  204. #endif
  205. /*
  206. * We're probed after MSI domains have been resolved, so force
  207. * resolution here.
  208. */
  209. pdev->dev.msi_domain = of_msi_get_domain(&pdev->dev, node,
  210. DOMAIN_BUS_PLATFORM_MSI);
  211. if (!pdev->dev.msi_domain)
  212. return -EPROBE_DEFER;
  213. gicp_dn = irq_domain_get_of_node(pdev->dev.msi_domain);
  214. if (!gicp_dn)
  215. return -ENODEV;
  216. /*
  217. * Clean all ICU interrupts with type SPI_NSR, required to
  218. * avoid unpredictable SPI assignments done by firmware.
  219. */
  220. for (i = 0 ; i < ICU_MAX_IRQS ; i++) {
  221. u32 icu_int = readl_relaxed(icu->base + ICU_INT_CFG(i));
  222. if ((icu_int >> ICU_GROUP_SHIFT) == ICU_GRP_NSR)
  223. writel_relaxed(0x0, icu->base + ICU_INT_CFG(i));
  224. }
  225. icu->domain =
  226. platform_msi_create_device_domain(&pdev->dev, ICU_MAX_IRQS,
  227. mvebu_icu_write_msg,
  228. &mvebu_icu_domain_ops, icu);
  229. if (!icu->domain) {
  230. dev_err(&pdev->dev, "Failed to create ICU domain\n");
  231. return -ENOMEM;
  232. }
  233. return 0;
  234. }
  235. static const struct of_device_id mvebu_icu_of_match[] = {
  236. { .compatible = "marvell,cp110-icu", },
  237. {},
  238. };
  239. static struct platform_driver mvebu_icu_driver = {
  240. .probe = mvebu_icu_probe,
  241. .driver = {
  242. .name = "mvebu-icu",
  243. .of_match_table = mvebu_icu_of_match,
  244. },
  245. };
  246. builtin_platform_driver(mvebu_icu_driver);