irq-mvebu-icu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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/jump_label.h>
  16. #include <linux/kernel.h>
  17. #include <linux/msi.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include "irq-msi-lib.h"
  22. #include <dt-bindings/interrupt-controller/mvebu-icu.h>
  23. /* ICU registers */
  24. #define ICU_SETSPI_NSR_AL 0x10
  25. #define ICU_SETSPI_NSR_AH 0x14
  26. #define ICU_CLRSPI_NSR_AL 0x18
  27. #define ICU_CLRSPI_NSR_AH 0x1c
  28. #define ICU_SET_SEI_AL 0x50
  29. #define ICU_SET_SEI_AH 0x54
  30. #define ICU_CLR_SEI_AL 0x58
  31. #define ICU_CLR_SEI_AH 0x5C
  32. #define ICU_INT_CFG(x) (0x100 + 4 * (x))
  33. #define ICU_INT_ENABLE BIT(24)
  34. #define ICU_IS_EDGE BIT(28)
  35. #define ICU_GROUP_SHIFT 29
  36. /* ICU definitions */
  37. #define ICU_MAX_IRQS 207
  38. #define ICU_SATA0_ICU_ID 109
  39. #define ICU_SATA1_ICU_ID 107
  40. struct mvebu_icu_subset_data {
  41. unsigned int icu_group;
  42. unsigned int offset_set_ah;
  43. unsigned int offset_set_al;
  44. unsigned int offset_clr_ah;
  45. unsigned int offset_clr_al;
  46. };
  47. struct mvebu_icu {
  48. void __iomem *base;
  49. struct device *dev;
  50. };
  51. struct mvebu_icu_msi_data {
  52. struct mvebu_icu *icu;
  53. atomic_t initialized;
  54. const struct mvebu_icu_subset_data *subset_data;
  55. };
  56. static DEFINE_STATIC_KEY_FALSE(legacy_bindings);
  57. static int mvebu_icu_translate(struct irq_domain *d, struct irq_fwspec *fwspec,
  58. unsigned long *hwirq, unsigned int *type)
  59. {
  60. unsigned int param_count = static_branch_unlikely(&legacy_bindings) ? 3 : 2;
  61. struct msi_domain_info *info = d->host_data;
  62. struct mvebu_icu_msi_data *msi_data = info->chip_data;
  63. struct mvebu_icu *icu = msi_data->icu;
  64. /* Check the count of the parameters in dt */
  65. if (WARN_ON(fwspec->param_count != param_count)) {
  66. dev_err(icu->dev, "wrong ICU parameter count %d\n",
  67. fwspec->param_count);
  68. return -EINVAL;
  69. }
  70. if (static_branch_unlikely(&legacy_bindings)) {
  71. *hwirq = fwspec->param[1];
  72. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  73. if (fwspec->param[0] != ICU_GRP_NSR) {
  74. dev_err(icu->dev, "wrong ICU group type %x\n",
  75. fwspec->param[0]);
  76. return -EINVAL;
  77. }
  78. } else {
  79. *hwirq = fwspec->param[0];
  80. *type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
  81. /*
  82. * The ICU receives level interrupts. While the NSR are also
  83. * level interrupts, SEI are edge interrupts. Force the type
  84. * here in this case. Please note that this makes the interrupt
  85. * handling unreliable.
  86. */
  87. if (msi_data->subset_data->icu_group == ICU_GRP_SEI)
  88. *type = IRQ_TYPE_EDGE_RISING;
  89. }
  90. if (*hwirq >= ICU_MAX_IRQS) {
  91. dev_err(icu->dev, "invalid interrupt number %ld\n", *hwirq);
  92. return -EINVAL;
  93. }
  94. return 0;
  95. }
  96. static void mvebu_icu_init(struct mvebu_icu *icu,
  97. struct mvebu_icu_msi_data *msi_data,
  98. struct msi_msg *msg)
  99. {
  100. const struct mvebu_icu_subset_data *subset = msi_data->subset_data;
  101. if (atomic_cmpxchg(&msi_data->initialized, false, true))
  102. return;
  103. /* Set 'SET' ICU SPI message address in AP */
  104. writel_relaxed(msg[0].address_hi, icu->base + subset->offset_set_ah);
  105. writel_relaxed(msg[0].address_lo, icu->base + subset->offset_set_al);
  106. if (subset->icu_group != ICU_GRP_NSR)
  107. return;
  108. /* Set 'CLEAR' ICU SPI message address in AP (level-MSI only) */
  109. writel_relaxed(msg[1].address_hi, icu->base + subset->offset_clr_ah);
  110. writel_relaxed(msg[1].address_lo, icu->base + subset->offset_clr_al);
  111. }
  112. static int mvebu_icu_msi_init(struct irq_domain *domain, struct msi_domain_info *info,
  113. unsigned int virq, irq_hw_number_t hwirq, msi_alloc_info_t *arg)
  114. {
  115. irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip, info->chip_data);
  116. return irq_set_irqchip_state(virq, IRQCHIP_STATE_PENDING, false);
  117. }
  118. static void mvebu_icu_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
  119. {
  120. arg->desc = desc;
  121. arg->hwirq = (u32)desc->data.icookie.value;
  122. }
  123. static void mvebu_icu_write_msi_msg(struct irq_data *d, struct msi_msg *msg)
  124. {
  125. struct mvebu_icu_msi_data *msi_data = d->chip_data;
  126. unsigned int icu_group = msi_data->subset_data->icu_group;
  127. struct msi_desc *desc = irq_data_get_msi_desc(d);
  128. struct mvebu_icu *icu = msi_data->icu;
  129. unsigned int type;
  130. u32 icu_int;
  131. if (msg->address_lo || msg->address_hi) {
  132. /* One off initialization per domain */
  133. mvebu_icu_init(icu, msi_data, msg);
  134. /* Configure the ICU with irq number & type */
  135. icu_int = msg->data | ICU_INT_ENABLE;
  136. type = (unsigned int)(desc->data.icookie.value >> 32);
  137. if (type & IRQ_TYPE_EDGE_RISING)
  138. icu_int |= ICU_IS_EDGE;
  139. icu_int |= icu_group << ICU_GROUP_SHIFT;
  140. } else {
  141. /* De-configure the ICU */
  142. icu_int = 0;
  143. }
  144. writel_relaxed(icu_int, icu->base + ICU_INT_CFG(d->hwirq));
  145. /*
  146. * The SATA unit has 2 ports, and a dedicated ICU entry per
  147. * port. The ahci sata driver supports only one irq interrupt
  148. * per SATA unit. To solve this conflict, we configure the 2
  149. * SATA wired interrupts in the south bridge into 1 GIC
  150. * interrupt in the north bridge. Even if only a single port
  151. * is enabled, if sata node is enabled, both interrupts are
  152. * configured (regardless of which port is actually in use).
  153. */
  154. if (d->hwirq == ICU_SATA0_ICU_ID || d->hwirq == ICU_SATA1_ICU_ID) {
  155. writel_relaxed(icu_int, icu->base + ICU_INT_CFG(ICU_SATA0_ICU_ID));
  156. writel_relaxed(icu_int, icu->base + ICU_INT_CFG(ICU_SATA1_ICU_ID));
  157. }
  158. }
  159. static const struct msi_domain_template mvebu_icu_nsr_msi_template = {
  160. .chip = {
  161. .name = "ICU-NSR",
  162. .irq_mask = irq_chip_mask_parent,
  163. .irq_unmask = irq_chip_unmask_parent,
  164. .irq_eoi = irq_chip_eoi_parent,
  165. .irq_set_type = irq_chip_set_type_parent,
  166. .irq_write_msi_msg = mvebu_icu_write_msi_msg,
  167. .flags = IRQCHIP_SUPPORTS_LEVEL_MSI,
  168. },
  169. .ops = {
  170. .msi_translate = mvebu_icu_translate,
  171. .msi_init = mvebu_icu_msi_init,
  172. .set_desc = mvebu_icu_set_desc,
  173. },
  174. .info = {
  175. .bus_token = DOMAIN_BUS_WIRED_TO_MSI,
  176. .flags = MSI_FLAG_LEVEL_CAPABLE |
  177. MSI_FLAG_USE_DEV_FWNODE,
  178. },
  179. };
  180. static const struct msi_domain_template mvebu_icu_sei_msi_template = {
  181. .chip = {
  182. .name = "ICU-SEI",
  183. .irq_mask = irq_chip_mask_parent,
  184. .irq_unmask = irq_chip_unmask_parent,
  185. .irq_ack = irq_chip_ack_parent,
  186. .irq_set_type = irq_chip_set_type_parent,
  187. .irq_write_msi_msg = mvebu_icu_write_msi_msg,
  188. .flags = IRQCHIP_SUPPORTS_LEVEL_MSI,
  189. },
  190. .ops = {
  191. .msi_translate = mvebu_icu_translate,
  192. .msi_init = mvebu_icu_msi_init,
  193. .set_desc = mvebu_icu_set_desc,
  194. },
  195. .info = {
  196. .bus_token = DOMAIN_BUS_WIRED_TO_MSI,
  197. .flags = MSI_FLAG_LEVEL_CAPABLE |
  198. MSI_FLAG_USE_DEV_FWNODE,
  199. },
  200. };
  201. static const struct mvebu_icu_subset_data mvebu_icu_nsr_subset_data = {
  202. .icu_group = ICU_GRP_NSR,
  203. .offset_set_ah = ICU_SETSPI_NSR_AH,
  204. .offset_set_al = ICU_SETSPI_NSR_AL,
  205. .offset_clr_ah = ICU_CLRSPI_NSR_AH,
  206. .offset_clr_al = ICU_CLRSPI_NSR_AL,
  207. };
  208. static const struct mvebu_icu_subset_data mvebu_icu_sei_subset_data = {
  209. .icu_group = ICU_GRP_SEI,
  210. .offset_set_ah = ICU_SET_SEI_AH,
  211. .offset_set_al = ICU_SET_SEI_AL,
  212. };
  213. static const struct of_device_id mvebu_icu_subset_of_match[] = {
  214. {
  215. .compatible = "marvell,cp110-icu-nsr",
  216. .data = &mvebu_icu_nsr_subset_data,
  217. },
  218. {
  219. .compatible = "marvell,cp110-icu-sei",
  220. .data = &mvebu_icu_sei_subset_data,
  221. },
  222. {},
  223. };
  224. static int mvebu_icu_subset_probe(struct platform_device *pdev)
  225. {
  226. const struct msi_domain_template *tmpl;
  227. struct mvebu_icu_msi_data *msi_data;
  228. struct device *dev = &pdev->dev;
  229. bool sei;
  230. msi_data = devm_kzalloc(dev, sizeof(*msi_data), GFP_KERNEL);
  231. if (!msi_data)
  232. return -ENOMEM;
  233. if (static_branch_unlikely(&legacy_bindings)) {
  234. msi_data->icu = dev_get_drvdata(dev);
  235. msi_data->subset_data = &mvebu_icu_nsr_subset_data;
  236. } else {
  237. msi_data->icu = dev_get_drvdata(dev->parent);
  238. msi_data->subset_data = of_device_get_match_data(dev);
  239. }
  240. dev->msi.domain = of_msi_get_domain(dev, dev->of_node, DOMAIN_BUS_PLATFORM_MSI);
  241. if (!dev->msi.domain)
  242. return -EPROBE_DEFER;
  243. if (!irq_domain_get_of_node(dev->msi.domain))
  244. return -ENODEV;
  245. sei = msi_data->subset_data->icu_group == ICU_GRP_SEI;
  246. tmpl = sei ? &mvebu_icu_sei_msi_template : &mvebu_icu_nsr_msi_template;
  247. if (!msi_create_device_irq_domain(dev, MSI_DEFAULT_DOMAIN, tmpl,
  248. ICU_MAX_IRQS, NULL, msi_data)) {
  249. dev_err(dev, "Failed to create ICU MSI domain\n");
  250. return -ENOMEM;
  251. }
  252. return 0;
  253. }
  254. static struct platform_driver mvebu_icu_subset_driver = {
  255. .probe = mvebu_icu_subset_probe,
  256. .driver = {
  257. .name = "mvebu-icu-subset",
  258. .of_match_table = mvebu_icu_subset_of_match,
  259. },
  260. };
  261. builtin_platform_driver(mvebu_icu_subset_driver);
  262. static int mvebu_icu_probe(struct platform_device *pdev)
  263. {
  264. struct mvebu_icu *icu;
  265. int i;
  266. icu = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_icu),
  267. GFP_KERNEL);
  268. if (!icu)
  269. return -ENOMEM;
  270. icu->dev = &pdev->dev;
  271. icu->base = devm_platform_ioremap_resource(pdev, 0);
  272. if (IS_ERR(icu->base))
  273. return PTR_ERR(icu->base);
  274. /*
  275. * Legacy bindings: ICU is one node with one MSI parent: force manually
  276. * the probe of the NSR interrupts side.
  277. * New bindings: ICU node has children, one per interrupt controller
  278. * having its own MSI parent: call platform_populate().
  279. * All ICU instances should use the same bindings.
  280. */
  281. if (!of_get_child_count(pdev->dev.of_node))
  282. static_branch_enable(&legacy_bindings);
  283. /*
  284. * Clean all ICU interrupts of type NSR and SEI, required to
  285. * avoid unpredictable SPI assignments done by firmware.
  286. */
  287. for (i = 0 ; i < ICU_MAX_IRQS ; i++) {
  288. u32 icu_int, icu_grp;
  289. icu_int = readl_relaxed(icu->base + ICU_INT_CFG(i));
  290. icu_grp = icu_int >> ICU_GROUP_SHIFT;
  291. if (icu_grp == ICU_GRP_NSR ||
  292. (icu_grp == ICU_GRP_SEI &&
  293. !static_branch_unlikely(&legacy_bindings)))
  294. writel_relaxed(0x0, icu->base + ICU_INT_CFG(i));
  295. }
  296. platform_set_drvdata(pdev, icu);
  297. if (static_branch_unlikely(&legacy_bindings))
  298. return mvebu_icu_subset_probe(pdev);
  299. else
  300. return devm_of_platform_populate(&pdev->dev);
  301. }
  302. static const struct of_device_id mvebu_icu_of_match[] = {
  303. { .compatible = "marvell,cp110-icu", },
  304. {},
  305. };
  306. static struct platform_driver mvebu_icu_driver = {
  307. .probe = mvebu_icu_probe,
  308. .driver = {
  309. .name = "mvebu-icu",
  310. .of_match_table = mvebu_icu_of_match,
  311. },
  312. };
  313. builtin_platform_driver(mvebu_icu_driver);