pcie-nxp-s32g.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for NXP S32G SoCs
  4. *
  5. * Copyright 2019-2025 NXP
  6. */
  7. #include <linux/interrupt.h>
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/of_device.h>
  11. #include <linux/of_address.h>
  12. #include <linux/pci.h>
  13. #include <linux/phy/phy.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/sizes.h>
  17. #include <linux/types.h>
  18. #include "pcie-designware.h"
  19. /* PCIe controller Sub-System */
  20. /* PCIe controller 0 General Control 1 */
  21. #define PCIE_S32G_PE0_GEN_CTRL_1 0x50
  22. #define DEVICE_TYPE_MASK GENMASK(3, 0)
  23. #define SRIS_MODE BIT(8)
  24. /* PCIe controller 0 General Control 3 */
  25. #define PCIE_S32G_PE0_GEN_CTRL_3 0x58
  26. #define LTSSM_EN BIT(0)
  27. /* PCIe Controller 0 Interrupt Status */
  28. #define PCIE_S32G_PE0_INT_STS 0xE8
  29. #define HP_INT_STS BIT(6)
  30. /* Boundary between peripheral space and physical memory space */
  31. #define S32G_MEMORY_BOUNDARY_ADDR 0x80000000
  32. struct s32g_pcie_port {
  33. struct list_head list;
  34. struct phy *phy;
  35. };
  36. struct s32g_pcie {
  37. struct dw_pcie pci;
  38. void __iomem *ctrl_base;
  39. struct list_head ports;
  40. };
  41. #define to_s32g_from_dw_pcie(x) \
  42. container_of(x, struct s32g_pcie, pci)
  43. static void s32g_pcie_writel_ctrl(struct s32g_pcie *s32g_pp, u32 reg, u32 val)
  44. {
  45. writel(val, s32g_pp->ctrl_base + reg);
  46. }
  47. static u32 s32g_pcie_readl_ctrl(struct s32g_pcie *s32g_pp, u32 reg)
  48. {
  49. return readl(s32g_pp->ctrl_base + reg);
  50. }
  51. static void s32g_pcie_enable_ltssm(struct s32g_pcie *s32g_pp)
  52. {
  53. u32 reg;
  54. reg = s32g_pcie_readl_ctrl(s32g_pp, PCIE_S32G_PE0_GEN_CTRL_3);
  55. reg |= LTSSM_EN;
  56. s32g_pcie_writel_ctrl(s32g_pp, PCIE_S32G_PE0_GEN_CTRL_3, reg);
  57. }
  58. static void s32g_pcie_disable_ltssm(struct s32g_pcie *s32g_pp)
  59. {
  60. u32 reg;
  61. reg = s32g_pcie_readl_ctrl(s32g_pp, PCIE_S32G_PE0_GEN_CTRL_3);
  62. reg &= ~LTSSM_EN;
  63. s32g_pcie_writel_ctrl(s32g_pp, PCIE_S32G_PE0_GEN_CTRL_3, reg);
  64. }
  65. static int s32g_pcie_start_link(struct dw_pcie *pci)
  66. {
  67. struct s32g_pcie *s32g_pp = to_s32g_from_dw_pcie(pci);
  68. s32g_pcie_enable_ltssm(s32g_pp);
  69. return 0;
  70. }
  71. static void s32g_pcie_stop_link(struct dw_pcie *pci)
  72. {
  73. struct s32g_pcie *s32g_pp = to_s32g_from_dw_pcie(pci);
  74. s32g_pcie_disable_ltssm(s32g_pp);
  75. }
  76. static struct dw_pcie_ops s32g_pcie_ops = {
  77. .start_link = s32g_pcie_start_link,
  78. .stop_link = s32g_pcie_stop_link,
  79. };
  80. /* Configure the AMBA AXI Coherency Extensions (ACE) interface */
  81. static void s32g_pcie_reset_mstr_ace(struct dw_pcie *pci)
  82. {
  83. u32 ddr_base_low = lower_32_bits(S32G_MEMORY_BOUNDARY_ADDR);
  84. u32 ddr_base_high = upper_32_bits(S32G_MEMORY_BOUNDARY_ADDR);
  85. dw_pcie_dbi_ro_wr_en(pci);
  86. dw_pcie_writel_dbi(pci, COHERENCY_CONTROL_3_OFF, 0x0);
  87. /*
  88. * Ncore is a cache-coherent interconnect module that enables the
  89. * integration of heterogeneous coherent and non-coherent agents in
  90. * the chip. Ncore transactions to peripheral should be non-coherent
  91. * or it might drop them.
  92. *
  93. * One example where this is needed are PCIe MSIs, which use NoSnoop=0
  94. * and might end up routed to Ncore. PCIe coherent traffic (e.g. MSIs)
  95. * that targets peripheral space will be dropped by Ncore because
  96. * peripherals on S32G are not coherent as slaves. We add a hard
  97. * boundary in the PCIe controller coherency control registers to
  98. * separate physical memory space from peripheral space.
  99. *
  100. * Define the start of DDR as seen by Linux as this boundary between
  101. * "memory" and "peripherals", with peripherals being below.
  102. */
  103. dw_pcie_writel_dbi(pci, COHERENCY_CONTROL_1_OFF,
  104. (ddr_base_low & CFG_MEMTYPE_BOUNDARY_LOW_ADDR_MASK));
  105. dw_pcie_writel_dbi(pci, COHERENCY_CONTROL_2_OFF, ddr_base_high);
  106. dw_pcie_dbi_ro_wr_dis(pci);
  107. }
  108. static int s32g_init_pcie_controller(struct dw_pcie_rp *pp)
  109. {
  110. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  111. struct s32g_pcie *s32g_pp = to_s32g_from_dw_pcie(pci);
  112. u32 val;
  113. /* Set RP mode */
  114. val = s32g_pcie_readl_ctrl(s32g_pp, PCIE_S32G_PE0_GEN_CTRL_1);
  115. val &= ~DEVICE_TYPE_MASK;
  116. val |= FIELD_PREP(DEVICE_TYPE_MASK, PCI_EXP_TYPE_ROOT_PORT);
  117. /* Use default CRNS */
  118. val &= ~SRIS_MODE;
  119. s32g_pcie_writel_ctrl(s32g_pp, PCIE_S32G_PE0_GEN_CTRL_1, val);
  120. /*
  121. * Make sure we use the coherency defaults (just in case the settings
  122. * have been changed from their reset values)
  123. */
  124. s32g_pcie_reset_mstr_ace(pci);
  125. dw_pcie_dbi_ro_wr_en(pci);
  126. val = dw_pcie_readl_dbi(pci, PCIE_PORT_FORCE);
  127. val |= PORT_FORCE_DO_DESKEW_FOR_SRIS;
  128. dw_pcie_writel_dbi(pci, PCIE_PORT_FORCE, val);
  129. val = dw_pcie_readl_dbi(pci, GEN3_RELATED_OFF);
  130. val |= GEN3_RELATED_OFF_EQ_PHASE_2_3;
  131. dw_pcie_writel_dbi(pci, GEN3_RELATED_OFF, val);
  132. dw_pcie_dbi_ro_wr_dis(pci);
  133. return 0;
  134. }
  135. static const struct dw_pcie_host_ops s32g_pcie_host_ops = {
  136. .init = s32g_init_pcie_controller,
  137. };
  138. static int s32g_init_pcie_phy(struct s32g_pcie *s32g_pp)
  139. {
  140. struct dw_pcie *pci = &s32g_pp->pci;
  141. struct device *dev = pci->dev;
  142. struct s32g_pcie_port *port, *tmp;
  143. int ret;
  144. list_for_each_entry(port, &s32g_pp->ports, list) {
  145. ret = phy_init(port->phy);
  146. if (ret) {
  147. dev_err(dev, "Failed to init serdes PHY\n");
  148. goto err_phy_revert;
  149. }
  150. ret = phy_set_mode_ext(port->phy, PHY_MODE_PCIE, 0);
  151. if (ret) {
  152. dev_err(dev, "Failed to set mode on serdes PHY\n");
  153. goto err_phy_exit;
  154. }
  155. ret = phy_power_on(port->phy);
  156. if (ret) {
  157. dev_err(dev, "Failed to power on serdes PHY\n");
  158. goto err_phy_exit;
  159. }
  160. }
  161. return 0;
  162. err_phy_exit:
  163. phy_exit(port->phy);
  164. err_phy_revert:
  165. list_for_each_entry_continue_reverse(port, &s32g_pp->ports, list) {
  166. phy_power_off(port->phy);
  167. phy_exit(port->phy);
  168. }
  169. list_for_each_entry_safe(port, tmp, &s32g_pp->ports, list)
  170. list_del(&port->list);
  171. return ret;
  172. }
  173. static void s32g_deinit_pcie_phy(struct s32g_pcie *s32g_pp)
  174. {
  175. struct s32g_pcie_port *port, *tmp;
  176. list_for_each_entry_safe(port, tmp, &s32g_pp->ports, list) {
  177. phy_power_off(port->phy);
  178. phy_exit(port->phy);
  179. list_del(&port->list);
  180. }
  181. }
  182. static int s32g_pcie_init(struct device *dev, struct s32g_pcie *s32g_pp)
  183. {
  184. s32g_pcie_disable_ltssm(s32g_pp);
  185. return s32g_init_pcie_phy(s32g_pp);
  186. }
  187. static void s32g_pcie_deinit(struct s32g_pcie *s32g_pp)
  188. {
  189. s32g_pcie_disable_ltssm(s32g_pp);
  190. s32g_deinit_pcie_phy(s32g_pp);
  191. }
  192. static int s32g_pcie_parse_port(struct s32g_pcie *s32g_pp, struct device_node *node)
  193. {
  194. struct device *dev = s32g_pp->pci.dev;
  195. struct s32g_pcie_port *port;
  196. int num_lanes;
  197. port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  198. if (!port)
  199. return -ENOMEM;
  200. port->phy = devm_of_phy_get(dev, node, NULL);
  201. if (IS_ERR(port->phy))
  202. return dev_err_probe(dev, PTR_ERR(port->phy),
  203. "Failed to get serdes PHY\n");
  204. INIT_LIST_HEAD(&port->list);
  205. list_add_tail(&port->list, &s32g_pp->ports);
  206. /*
  207. * The DWC core initialization code cannot yet parse the num-lanes
  208. * attribute in the Root Port node. The S32G only supports one Root
  209. * Port for now so its driver can parse the node and set the num_lanes
  210. * field of struct dwc_pcie before calling dw_pcie_host_init().
  211. */
  212. if (!of_property_read_u32(node, "num-lanes", &num_lanes))
  213. s32g_pp->pci.num_lanes = num_lanes;
  214. return 0;
  215. }
  216. static int s32g_pcie_parse_ports(struct device *dev, struct s32g_pcie *s32g_pp)
  217. {
  218. struct s32g_pcie_port *port, *tmp;
  219. int ret = -ENOENT;
  220. for_each_available_child_of_node_scoped(dev->of_node, of_port) {
  221. if (!of_node_is_type(of_port, "pci"))
  222. continue;
  223. ret = s32g_pcie_parse_port(s32g_pp, of_port);
  224. if (ret)
  225. break;
  226. }
  227. if (ret)
  228. list_for_each_entry_safe(port, tmp, &s32g_pp->ports, list)
  229. list_del(&port->list);
  230. return ret;
  231. }
  232. static int s32g_pcie_get_resources(struct platform_device *pdev,
  233. struct s32g_pcie *s32g_pp)
  234. {
  235. struct dw_pcie *pci = &s32g_pp->pci;
  236. struct device *dev = &pdev->dev;
  237. int ret;
  238. pci->dev = dev;
  239. pci->ops = &s32g_pcie_ops;
  240. s32g_pp->ctrl_base = devm_platform_ioremap_resource_byname(pdev, "ctrl");
  241. if (IS_ERR(s32g_pp->ctrl_base))
  242. return PTR_ERR(s32g_pp->ctrl_base);
  243. INIT_LIST_HEAD(&s32g_pp->ports);
  244. ret = s32g_pcie_parse_ports(dev, s32g_pp);
  245. if (ret)
  246. return dev_err_probe(dev, ret,
  247. "Failed to parse Root Port: %d\n", ret);
  248. platform_set_drvdata(pdev, s32g_pp);
  249. return 0;
  250. }
  251. static int s32g_pcie_probe(struct platform_device *pdev)
  252. {
  253. struct device *dev = &pdev->dev;
  254. struct s32g_pcie *s32g_pp;
  255. struct dw_pcie_rp *pp;
  256. int ret;
  257. s32g_pp = devm_kzalloc(dev, sizeof(*s32g_pp), GFP_KERNEL);
  258. if (!s32g_pp)
  259. return -ENOMEM;
  260. ret = s32g_pcie_get_resources(pdev, s32g_pp);
  261. if (ret)
  262. return ret;
  263. pm_runtime_no_callbacks(dev);
  264. devm_pm_runtime_enable(dev);
  265. ret = pm_runtime_get_sync(dev);
  266. if (ret < 0)
  267. goto err_pm_runtime_put;
  268. ret = s32g_pcie_init(dev, s32g_pp);
  269. if (ret)
  270. goto err_pm_runtime_put;
  271. pp = &s32g_pp->pci.pp;
  272. pp->ops = &s32g_pcie_host_ops;
  273. pp->use_atu_msg = true;
  274. ret = dw_pcie_host_init(pp);
  275. if (ret)
  276. goto err_pcie_deinit;
  277. return 0;
  278. err_pcie_deinit:
  279. s32g_pcie_deinit(s32g_pp);
  280. err_pm_runtime_put:
  281. pm_runtime_put(dev);
  282. return ret;
  283. }
  284. static int s32g_pcie_suspend_noirq(struct device *dev)
  285. {
  286. struct s32g_pcie *s32g_pp = dev_get_drvdata(dev);
  287. struct dw_pcie *pci = &s32g_pp->pci;
  288. return dw_pcie_suspend_noirq(pci);
  289. }
  290. static int s32g_pcie_resume_noirq(struct device *dev)
  291. {
  292. struct s32g_pcie *s32g_pp = dev_get_drvdata(dev);
  293. struct dw_pcie *pci = &s32g_pp->pci;
  294. return dw_pcie_resume_noirq(pci);
  295. }
  296. static const struct dev_pm_ops s32g_pcie_pm_ops = {
  297. NOIRQ_SYSTEM_SLEEP_PM_OPS(s32g_pcie_suspend_noirq,
  298. s32g_pcie_resume_noirq)
  299. };
  300. static const struct of_device_id s32g_pcie_of_match[] = {
  301. { .compatible = "nxp,s32g2-pcie" },
  302. { /* sentinel */ },
  303. };
  304. MODULE_DEVICE_TABLE(of, s32g_pcie_of_match);
  305. static struct platform_driver s32g_pcie_driver = {
  306. .driver = {
  307. .name = "s32g-pcie",
  308. .of_match_table = s32g_pcie_of_match,
  309. .suppress_bind_attrs = true,
  310. .pm = pm_sleep_ptr(&s32g_pcie_pm_ops),
  311. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  312. },
  313. .probe = s32g_pcie_probe,
  314. };
  315. builtin_platform_driver(s32g_pcie_driver);
  316. MODULE_AUTHOR("Ionut Vicovan <Ionut.Vicovan@nxp.com>");
  317. MODULE_DESCRIPTION("NXP S32G PCIe Host controller driver");
  318. MODULE_LICENSE("GPL");