pcie-stm32-ep.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * STMicroelectronics STM32MP25 PCIe endpoint driver.
  4. *
  5. * Copyright (C) 2025 STMicroelectronics
  6. * Author: Christian Bruel <christian.bruel@foss.st.com>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/phy/phy.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/regmap.h>
  16. #include <linux/reset.h>
  17. #include "pcie-designware.h"
  18. #include "pcie-stm32.h"
  19. struct stm32_pcie {
  20. struct dw_pcie pci;
  21. struct regmap *regmap;
  22. struct reset_control *rst;
  23. struct phy *phy;
  24. struct clk *clk;
  25. struct gpio_desc *perst_gpio;
  26. unsigned int perst_irq;
  27. };
  28. static void stm32_pcie_ep_init(struct dw_pcie_ep *ep)
  29. {
  30. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  31. enum pci_barno bar;
  32. for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
  33. dw_pcie_ep_reset_bar(pci, bar);
  34. }
  35. static int stm32_pcie_start_link(struct dw_pcie *pci)
  36. {
  37. struct stm32_pcie *stm32_pcie = to_stm32_pcie(pci);
  38. enable_irq(stm32_pcie->perst_irq);
  39. return 0;
  40. }
  41. static void stm32_pcie_stop_link(struct dw_pcie *pci)
  42. {
  43. struct stm32_pcie *stm32_pcie = to_stm32_pcie(pci);
  44. disable_irq(stm32_pcie->perst_irq);
  45. }
  46. static int stm32_pcie_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
  47. unsigned int type, u16 interrupt_num)
  48. {
  49. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  50. switch (type) {
  51. case PCI_IRQ_INTX:
  52. return dw_pcie_ep_raise_intx_irq(ep, func_no);
  53. case PCI_IRQ_MSI:
  54. return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num);
  55. default:
  56. dev_err(pci->dev, "UNKNOWN IRQ type\n");
  57. return -EINVAL;
  58. }
  59. }
  60. static const struct pci_epc_features stm32_pcie_epc_features = {
  61. DWC_EPC_COMMON_FEATURES,
  62. .msi_capable = true,
  63. .align = SZ_64K,
  64. };
  65. static const struct pci_epc_features*
  66. stm32_pcie_get_features(struct dw_pcie_ep *ep)
  67. {
  68. return &stm32_pcie_epc_features;
  69. }
  70. static const struct dw_pcie_ep_ops stm32_pcie_ep_ops = {
  71. .init = stm32_pcie_ep_init,
  72. .raise_irq = stm32_pcie_raise_irq,
  73. .get_features = stm32_pcie_get_features,
  74. };
  75. static const struct dw_pcie_ops dw_pcie_ops = {
  76. .start_link = stm32_pcie_start_link,
  77. .stop_link = stm32_pcie_stop_link,
  78. };
  79. static int stm32_pcie_enable_resources(struct stm32_pcie *stm32_pcie)
  80. {
  81. int ret;
  82. ret = phy_init(stm32_pcie->phy);
  83. if (ret)
  84. return ret;
  85. ret = clk_prepare_enable(stm32_pcie->clk);
  86. if (ret)
  87. phy_exit(stm32_pcie->phy);
  88. return ret;
  89. }
  90. static void stm32_pcie_disable_resources(struct stm32_pcie *stm32_pcie)
  91. {
  92. clk_disable_unprepare(stm32_pcie->clk);
  93. phy_exit(stm32_pcie->phy);
  94. }
  95. static void stm32_pcie_perst_assert(struct dw_pcie *pci)
  96. {
  97. struct stm32_pcie *stm32_pcie = to_stm32_pcie(pci);
  98. struct dw_pcie_ep *ep = &stm32_pcie->pci.ep;
  99. struct device *dev = pci->dev;
  100. dev_dbg(dev, "PERST asserted by host\n");
  101. regmap_update_bits(stm32_pcie->regmap, SYSCFG_PCIECR,
  102. STM32MP25_PCIECR_LTSSM_EN, 0);
  103. pci_epc_deinit_notify(ep->epc);
  104. stm32_pcie_disable_resources(stm32_pcie);
  105. pm_runtime_put_sync(dev);
  106. }
  107. static void stm32_pcie_perst_deassert(struct dw_pcie *pci)
  108. {
  109. struct stm32_pcie *stm32_pcie = to_stm32_pcie(pci);
  110. struct device *dev = pci->dev;
  111. struct dw_pcie_ep *ep = &pci->ep;
  112. int ret;
  113. dev_dbg(dev, "PERST de-asserted by host\n");
  114. ret = pm_runtime_resume_and_get(dev);
  115. if (ret < 0) {
  116. dev_err(dev, "Failed to resume runtime PM: %d\n", ret);
  117. return;
  118. }
  119. ret = stm32_pcie_enable_resources(stm32_pcie);
  120. if (ret) {
  121. dev_err(dev, "Failed to enable resources: %d\n", ret);
  122. goto err_pm_put_sync;
  123. }
  124. /*
  125. * Reprogram the configuration space registers here because the DBI
  126. * registers were reset by the PHY RCC during phy_init().
  127. */
  128. ret = dw_pcie_ep_init_registers(ep);
  129. if (ret) {
  130. dev_err(dev, "Failed to complete initialization: %d\n", ret);
  131. goto err_disable_resources;
  132. }
  133. pci_epc_init_notify(ep->epc);
  134. /* Enable link training */
  135. regmap_update_bits(stm32_pcie->regmap, SYSCFG_PCIECR,
  136. STM32MP25_PCIECR_LTSSM_EN,
  137. STM32MP25_PCIECR_LTSSM_EN);
  138. return;
  139. err_disable_resources:
  140. stm32_pcie_disable_resources(stm32_pcie);
  141. err_pm_put_sync:
  142. pm_runtime_put_sync(dev);
  143. }
  144. static irqreturn_t stm32_pcie_ep_perst_irq_thread(int irq, void *data)
  145. {
  146. struct stm32_pcie *stm32_pcie = data;
  147. struct dw_pcie *pci = &stm32_pcie->pci;
  148. u32 perst;
  149. perst = gpiod_get_value(stm32_pcie->perst_gpio);
  150. if (perst)
  151. stm32_pcie_perst_assert(pci);
  152. else
  153. stm32_pcie_perst_deassert(pci);
  154. irq_set_irq_type(gpiod_to_irq(stm32_pcie->perst_gpio),
  155. (perst ? IRQF_TRIGGER_HIGH : IRQF_TRIGGER_LOW));
  156. return IRQ_HANDLED;
  157. }
  158. static int stm32_add_pcie_ep(struct stm32_pcie *stm32_pcie,
  159. struct platform_device *pdev)
  160. {
  161. struct dw_pcie_ep *ep = &stm32_pcie->pci.ep;
  162. struct device *dev = &pdev->dev;
  163. int ret;
  164. ret = regmap_update_bits(stm32_pcie->regmap, SYSCFG_PCIECR,
  165. STM32MP25_PCIECR_TYPE_MASK,
  166. STM32MP25_PCIECR_EP);
  167. if (ret)
  168. return ret;
  169. reset_control_assert(stm32_pcie->rst);
  170. reset_control_deassert(stm32_pcie->rst);
  171. ep->ops = &stm32_pcie_ep_ops;
  172. ep->page_size = stm32_pcie_epc_features.align;
  173. ret = dw_pcie_ep_init(ep);
  174. if (ret) {
  175. dev_err(dev, "Failed to initialize ep: %d\n", ret);
  176. return ret;
  177. }
  178. ret = stm32_pcie_enable_resources(stm32_pcie);
  179. if (ret) {
  180. dev_err(dev, "Failed to enable resources: %d\n", ret);
  181. dw_pcie_ep_deinit(ep);
  182. return ret;
  183. }
  184. return 0;
  185. }
  186. static int stm32_pcie_probe(struct platform_device *pdev)
  187. {
  188. struct stm32_pcie *stm32_pcie;
  189. struct device *dev = &pdev->dev;
  190. int ret;
  191. stm32_pcie = devm_kzalloc(dev, sizeof(*stm32_pcie), GFP_KERNEL);
  192. if (!stm32_pcie)
  193. return -ENOMEM;
  194. stm32_pcie->pci.dev = dev;
  195. stm32_pcie->pci.ops = &dw_pcie_ops;
  196. stm32_pcie->regmap = syscon_regmap_lookup_by_compatible("st,stm32mp25-syscfg");
  197. if (IS_ERR(stm32_pcie->regmap))
  198. return dev_err_probe(dev, PTR_ERR(stm32_pcie->regmap),
  199. "No syscfg specified\n");
  200. stm32_pcie->phy = devm_phy_get(dev, NULL);
  201. if (IS_ERR(stm32_pcie->phy))
  202. return dev_err_probe(dev, PTR_ERR(stm32_pcie->phy),
  203. "failed to get pcie-phy\n");
  204. stm32_pcie->clk = devm_clk_get(dev, NULL);
  205. if (IS_ERR(stm32_pcie->clk))
  206. return dev_err_probe(dev, PTR_ERR(stm32_pcie->clk),
  207. "Failed to get PCIe clock source\n");
  208. stm32_pcie->rst = devm_reset_control_get_exclusive(dev, NULL);
  209. if (IS_ERR(stm32_pcie->rst))
  210. return dev_err_probe(dev, PTR_ERR(stm32_pcie->rst),
  211. "Failed to get PCIe reset\n");
  212. stm32_pcie->perst_gpio = devm_gpiod_get(dev, "reset", GPIOD_IN);
  213. if (IS_ERR(stm32_pcie->perst_gpio))
  214. return dev_err_probe(dev, PTR_ERR(stm32_pcie->perst_gpio),
  215. "Failed to get reset GPIO\n");
  216. ret = phy_set_mode(stm32_pcie->phy, PHY_MODE_PCIE);
  217. if (ret)
  218. return ret;
  219. platform_set_drvdata(pdev, stm32_pcie);
  220. pm_runtime_get_noresume(dev);
  221. ret = devm_pm_runtime_enable(dev);
  222. if (ret < 0) {
  223. pm_runtime_put_noidle(&pdev->dev);
  224. return dev_err_probe(dev, ret, "Failed to enable runtime PM\n");
  225. }
  226. stm32_pcie->perst_irq = gpiod_to_irq(stm32_pcie->perst_gpio);
  227. /* Will be enabled in start_link when device is initialized. */
  228. irq_set_status_flags(stm32_pcie->perst_irq, IRQ_NOAUTOEN);
  229. ret = devm_request_threaded_irq(dev, stm32_pcie->perst_irq, NULL,
  230. stm32_pcie_ep_perst_irq_thread,
  231. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  232. "perst_irq", stm32_pcie);
  233. if (ret) {
  234. pm_runtime_put_noidle(&pdev->dev);
  235. return dev_err_probe(dev, ret, "Failed to request PERST IRQ\n");
  236. }
  237. ret = stm32_add_pcie_ep(stm32_pcie, pdev);
  238. if (ret)
  239. pm_runtime_put_noidle(&pdev->dev);
  240. return ret;
  241. }
  242. static void stm32_pcie_remove(struct platform_device *pdev)
  243. {
  244. struct stm32_pcie *stm32_pcie = platform_get_drvdata(pdev);
  245. struct dw_pcie *pci = &stm32_pcie->pci;
  246. struct dw_pcie_ep *ep = &pci->ep;
  247. dw_pcie_stop_link(pci);
  248. pci_epc_deinit_notify(ep->epc);
  249. dw_pcie_ep_deinit(ep);
  250. stm32_pcie_disable_resources(stm32_pcie);
  251. pm_runtime_put_sync(&pdev->dev);
  252. }
  253. static const struct of_device_id stm32_pcie_ep_of_match[] = {
  254. { .compatible = "st,stm32mp25-pcie-ep" },
  255. {},
  256. };
  257. static struct platform_driver stm32_pcie_ep_driver = {
  258. .probe = stm32_pcie_probe,
  259. .remove = stm32_pcie_remove,
  260. .driver = {
  261. .name = "stm32-ep-pcie",
  262. .of_match_table = stm32_pcie_ep_of_match,
  263. },
  264. };
  265. module_platform_driver(stm32_pcie_ep_driver);
  266. MODULE_AUTHOR("Christian Bruel <christian.bruel@foss.st.com>");
  267. MODULE_DESCRIPTION("STM32MP25 PCIe Endpoint Controller driver");
  268. MODULE_LICENSE("GPL");
  269. MODULE_DEVICE_TABLE(of, stm32_pcie_ep_of_match);