irq-mvebu-icu.c 7.8 KB

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