pci-layerscape.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Freescale Layerscape SoCs
  4. *
  5. * Copyright (C) 2014 Freescale Semiconductor.
  6. * Copyright 2021 NXP
  7. *
  8. * Author: Minghuan Lian <Minghuan.Lian@freescale.com>
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/kernel.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/init.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/of_pci.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/of_address.h>
  18. #include <linux/pci.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/resource.h>
  21. #include <linux/mfd/syscon.h>
  22. #include <linux/regmap.h>
  23. #include "../../pci.h"
  24. #include "pcie-designware.h"
  25. /* PEX Internal Configuration Registers */
  26. #define PCIE_STRFMR1 0x71c /* Symbol Timer & Filter Mask Register1 */
  27. #define PCIE_ABSERR 0x8d0 /* Bridge Slave Error Response Register */
  28. #define PCIE_ABSERR_SETTING 0x9401 /* Forward error of non-posted request */
  29. /* PF Message Command Register */
  30. #define LS_PCIE_PF_MCR 0x2c
  31. #define PF_MCR_PTOMR BIT(0)
  32. #define PF_MCR_EXL2S BIT(1)
  33. /* LS1021A PEXn PM Write Control Register */
  34. #define SCFG_PEXPMWRCR(idx) (0x5c + (idx) * 0x64)
  35. #define PMXMTTURNOFF BIT(31)
  36. #define SCFG_PEXSFTRSTCR 0x190
  37. #define PEXSR(idx) BIT(idx)
  38. /* LS1043A PEX PME control register */
  39. #define SCFG_PEXPMECR 0x144
  40. #define PEXPME(idx) BIT(31 - (idx) * 4)
  41. /* LS1043A PEX LUT debug register */
  42. #define LS_PCIE_LDBG 0x7fc
  43. #define LDBG_SR BIT(30)
  44. #define LDBG_WE BIT(31)
  45. #define PCIE_IATU_NUM 6
  46. struct ls_pcie_drvdata {
  47. const u32 pf_lut_off;
  48. const struct dw_pcie_host_ops *ops;
  49. int (*exit_from_l2)(struct dw_pcie_rp *pp);
  50. bool scfg_support;
  51. bool pm_support;
  52. };
  53. struct ls_pcie {
  54. struct dw_pcie *pci;
  55. const struct ls_pcie_drvdata *drvdata;
  56. void __iomem *pf_lut_base;
  57. struct regmap *scfg;
  58. int index;
  59. bool big_endian;
  60. };
  61. #define ls_pcie_pf_lut_readl_addr(addr) ls_pcie_pf_lut_readl(pcie, addr)
  62. #define to_ls_pcie(x) dev_get_drvdata((x)->dev)
  63. static bool ls_pcie_is_bridge(struct ls_pcie *pcie)
  64. {
  65. struct dw_pcie *pci = pcie->pci;
  66. u32 header_type;
  67. header_type = ioread8(pci->dbi_base + PCI_HEADER_TYPE);
  68. header_type &= PCI_HEADER_TYPE_MASK;
  69. return header_type == PCI_HEADER_TYPE_BRIDGE;
  70. }
  71. /* Clear multi-function bit */
  72. static void ls_pcie_clear_multifunction(struct ls_pcie *pcie)
  73. {
  74. struct dw_pcie *pci = pcie->pci;
  75. iowrite8(PCI_HEADER_TYPE_BRIDGE, pci->dbi_base + PCI_HEADER_TYPE);
  76. }
  77. /* Drop MSG TLP except for Vendor MSG */
  78. static void ls_pcie_drop_msg_tlp(struct ls_pcie *pcie)
  79. {
  80. u32 val;
  81. struct dw_pcie *pci = pcie->pci;
  82. val = ioread32(pci->dbi_base + PCIE_STRFMR1);
  83. val &= 0xDFFFFFFF;
  84. iowrite32(val, pci->dbi_base + PCIE_STRFMR1);
  85. }
  86. /* Forward error response of outbound non-posted requests */
  87. static void ls_pcie_fix_error_response(struct ls_pcie *pcie)
  88. {
  89. struct dw_pcie *pci = pcie->pci;
  90. iowrite32(PCIE_ABSERR_SETTING, pci->dbi_base + PCIE_ABSERR);
  91. }
  92. static u32 ls_pcie_pf_lut_readl(struct ls_pcie *pcie, u32 off)
  93. {
  94. if (pcie->big_endian)
  95. return ioread32be(pcie->pf_lut_base + off);
  96. return ioread32(pcie->pf_lut_base + off);
  97. }
  98. static void ls_pcie_pf_lut_writel(struct ls_pcie *pcie, u32 off, u32 val)
  99. {
  100. if (pcie->big_endian)
  101. iowrite32be(val, pcie->pf_lut_base + off);
  102. else
  103. iowrite32(val, pcie->pf_lut_base + off);
  104. }
  105. static void ls_pcie_send_turnoff_msg(struct dw_pcie_rp *pp)
  106. {
  107. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  108. struct ls_pcie *pcie = to_ls_pcie(pci);
  109. u32 val;
  110. int ret;
  111. val = ls_pcie_pf_lut_readl(pcie, LS_PCIE_PF_MCR);
  112. val |= PF_MCR_PTOMR;
  113. ls_pcie_pf_lut_writel(pcie, LS_PCIE_PF_MCR, val);
  114. ret = readx_poll_timeout(ls_pcie_pf_lut_readl_addr, LS_PCIE_PF_MCR,
  115. val, !(val & PF_MCR_PTOMR),
  116. PCIE_PME_TO_L2_TIMEOUT_US/10,
  117. PCIE_PME_TO_L2_TIMEOUT_US);
  118. if (ret)
  119. dev_err(pcie->pci->dev, "PME_Turn_off timeout\n");
  120. }
  121. static int ls_pcie_exit_from_l2(struct dw_pcie_rp *pp)
  122. {
  123. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  124. struct ls_pcie *pcie = to_ls_pcie(pci);
  125. u32 val;
  126. int ret;
  127. /*
  128. * Set PF_MCR_EXL2S bit in LS_PCIE_PF_MCR register for the link
  129. * to exit L2 state.
  130. */
  131. val = ls_pcie_pf_lut_readl(pcie, LS_PCIE_PF_MCR);
  132. val |= PF_MCR_EXL2S;
  133. ls_pcie_pf_lut_writel(pcie, LS_PCIE_PF_MCR, val);
  134. /*
  135. * L2 exit timeout of 10ms is not defined in the specifications,
  136. * it was chosen based on empirical observations.
  137. */
  138. ret = readx_poll_timeout(ls_pcie_pf_lut_readl_addr, LS_PCIE_PF_MCR,
  139. val, !(val & PF_MCR_EXL2S),
  140. 1000,
  141. 10000);
  142. if (ret)
  143. dev_err(pcie->pci->dev, "L2 exit timeout\n");
  144. return ret;
  145. }
  146. static int ls_pcie_host_init(struct dw_pcie_rp *pp)
  147. {
  148. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  149. struct ls_pcie *pcie = to_ls_pcie(pci);
  150. ls_pcie_fix_error_response(pcie);
  151. dw_pcie_dbi_ro_wr_en(pci);
  152. ls_pcie_clear_multifunction(pcie);
  153. dw_pcie_dbi_ro_wr_dis(pci);
  154. ls_pcie_drop_msg_tlp(pcie);
  155. return 0;
  156. }
  157. static void scfg_pcie_send_turnoff_msg(struct regmap *scfg, u32 reg, u32 mask)
  158. {
  159. /* Send PME_Turn_Off message */
  160. regmap_write_bits(scfg, reg, mask, mask);
  161. /*
  162. * There is no specific register to check for PME_To_Ack from endpoint.
  163. * So on the safe side, wait for PCIE_PME_TO_L2_TIMEOUT_US.
  164. */
  165. mdelay(PCIE_PME_TO_L2_TIMEOUT_US/1000);
  166. /*
  167. * Layerscape hardware reference manual recommends clearing the PMXMTTURNOFF bit
  168. * to complete the PME_Turn_Off handshake.
  169. */
  170. regmap_write_bits(scfg, reg, mask, 0);
  171. }
  172. static void ls1021a_pcie_send_turnoff_msg(struct dw_pcie_rp *pp)
  173. {
  174. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  175. struct ls_pcie *pcie = to_ls_pcie(pci);
  176. scfg_pcie_send_turnoff_msg(pcie->scfg, SCFG_PEXPMWRCR(pcie->index), PMXMTTURNOFF);
  177. }
  178. static int scfg_pcie_exit_from_l2(struct regmap *scfg, u32 reg, u32 mask)
  179. {
  180. /* Reset the PEX wrapper to bring the link out of L2 */
  181. regmap_write_bits(scfg, reg, mask, mask);
  182. regmap_write_bits(scfg, reg, mask, 0);
  183. return 0;
  184. }
  185. static int ls1021a_pcie_exit_from_l2(struct dw_pcie_rp *pp)
  186. {
  187. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  188. struct ls_pcie *pcie = to_ls_pcie(pci);
  189. return scfg_pcie_exit_from_l2(pcie->scfg, SCFG_PEXSFTRSTCR, PEXSR(pcie->index));
  190. }
  191. static void ls1043a_pcie_send_turnoff_msg(struct dw_pcie_rp *pp)
  192. {
  193. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  194. struct ls_pcie *pcie = to_ls_pcie(pci);
  195. scfg_pcie_send_turnoff_msg(pcie->scfg, SCFG_PEXPMECR, PEXPME(pcie->index));
  196. }
  197. static int ls1043a_pcie_exit_from_l2(struct dw_pcie_rp *pp)
  198. {
  199. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  200. struct ls_pcie *pcie = to_ls_pcie(pci);
  201. u32 val;
  202. /*
  203. * Reset the PEX wrapper to bring the link out of L2.
  204. * LDBG_WE: allows the user to have write access to the PEXDBG[SR] for both setting and
  205. * clearing the soft reset on the PEX module.
  206. * LDBG_SR: When SR is set to 1, the PEX module enters soft reset.
  207. */
  208. val = ls_pcie_pf_lut_readl(pcie, LS_PCIE_LDBG);
  209. val |= LDBG_WE;
  210. ls_pcie_pf_lut_writel(pcie, LS_PCIE_LDBG, val);
  211. val = ls_pcie_pf_lut_readl(pcie, LS_PCIE_LDBG);
  212. val |= LDBG_SR;
  213. ls_pcie_pf_lut_writel(pcie, LS_PCIE_LDBG, val);
  214. val = ls_pcie_pf_lut_readl(pcie, LS_PCIE_LDBG);
  215. val &= ~LDBG_SR;
  216. ls_pcie_pf_lut_writel(pcie, LS_PCIE_LDBG, val);
  217. val = ls_pcie_pf_lut_readl(pcie, LS_PCIE_LDBG);
  218. val &= ~LDBG_WE;
  219. ls_pcie_pf_lut_writel(pcie, LS_PCIE_LDBG, val);
  220. return 0;
  221. }
  222. static const struct dw_pcie_host_ops ls_pcie_host_ops = {
  223. .init = ls_pcie_host_init,
  224. .pme_turn_off = ls_pcie_send_turnoff_msg,
  225. };
  226. static const struct dw_pcie_host_ops ls1021a_pcie_host_ops = {
  227. .init = ls_pcie_host_init,
  228. .pme_turn_off = ls1021a_pcie_send_turnoff_msg,
  229. };
  230. static const struct ls_pcie_drvdata ls1021a_drvdata = {
  231. .pm_support = true,
  232. .scfg_support = true,
  233. .ops = &ls1021a_pcie_host_ops,
  234. .exit_from_l2 = ls1021a_pcie_exit_from_l2,
  235. };
  236. static const struct dw_pcie_host_ops ls1043a_pcie_host_ops = {
  237. .init = ls_pcie_host_init,
  238. .pme_turn_off = ls1043a_pcie_send_turnoff_msg,
  239. };
  240. static const struct ls_pcie_drvdata ls1043a_drvdata = {
  241. .pf_lut_off = 0x10000,
  242. .pm_support = true,
  243. .scfg_support = true,
  244. .ops = &ls1043a_pcie_host_ops,
  245. .exit_from_l2 = ls1043a_pcie_exit_from_l2,
  246. };
  247. static const struct ls_pcie_drvdata layerscape_drvdata = {
  248. .pf_lut_off = 0xc0000,
  249. .pm_support = true,
  250. .ops = &ls_pcie_host_ops,
  251. .exit_from_l2 = ls_pcie_exit_from_l2,
  252. };
  253. static const struct of_device_id ls_pcie_of_match[] = {
  254. { .compatible = "fsl,ls1012a-pcie", .data = &layerscape_drvdata },
  255. { .compatible = "fsl,ls1021a-pcie", .data = &ls1021a_drvdata },
  256. { .compatible = "fsl,ls1028a-pcie", .data = &layerscape_drvdata },
  257. { .compatible = "fsl,ls1043a-pcie", .data = &ls1043a_drvdata },
  258. { .compatible = "fsl,ls1046a-pcie", .data = &layerscape_drvdata },
  259. { .compatible = "fsl,ls2080a-pcie", .data = &layerscape_drvdata },
  260. { .compatible = "fsl,ls2085a-pcie", .data = &layerscape_drvdata },
  261. { .compatible = "fsl,ls2088a-pcie", .data = &layerscape_drvdata },
  262. { .compatible = "fsl,ls1088a-pcie", .data = &layerscape_drvdata },
  263. { },
  264. };
  265. static int ls_pcie_probe(struct platform_device *pdev)
  266. {
  267. struct device *dev = &pdev->dev;
  268. struct dw_pcie *pci;
  269. struct ls_pcie *pcie;
  270. struct resource *dbi_base;
  271. u32 index[2];
  272. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  273. if (!pcie)
  274. return -ENOMEM;
  275. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  276. if (!pci)
  277. return -ENOMEM;
  278. pcie->drvdata = of_device_get_match_data(dev);
  279. pci->dev = dev;
  280. pcie->pci = pci;
  281. pci->pp.ops = pcie->drvdata->ops;
  282. dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  283. pci->dbi_base = devm_pci_remap_cfg_resource(dev, dbi_base);
  284. if (IS_ERR(pci->dbi_base))
  285. return PTR_ERR(pci->dbi_base);
  286. pcie->big_endian = of_property_read_bool(dev->of_node, "big-endian");
  287. pcie->pf_lut_base = pci->dbi_base + pcie->drvdata->pf_lut_off;
  288. if (pcie->drvdata->scfg_support) {
  289. pcie->scfg =
  290. syscon_regmap_lookup_by_phandle_args(dev->of_node,
  291. "fsl,pcie-scfg", 1,
  292. index);
  293. if (IS_ERR(pcie->scfg)) {
  294. dev_err(dev, "No syscfg phandle specified\n");
  295. return PTR_ERR(pcie->scfg);
  296. }
  297. pcie->index = index[1];
  298. }
  299. if (!ls_pcie_is_bridge(pcie))
  300. return -ENODEV;
  301. platform_set_drvdata(pdev, pcie);
  302. return dw_pcie_host_init(&pci->pp);
  303. }
  304. static int ls_pcie_suspend_noirq(struct device *dev)
  305. {
  306. struct ls_pcie *pcie = dev_get_drvdata(dev);
  307. if (!pcie->drvdata->pm_support)
  308. return 0;
  309. return dw_pcie_suspend_noirq(pcie->pci);
  310. }
  311. static int ls_pcie_resume_noirq(struct device *dev)
  312. {
  313. struct ls_pcie *pcie = dev_get_drvdata(dev);
  314. int ret;
  315. if (!pcie->drvdata->pm_support)
  316. return 0;
  317. ret = pcie->drvdata->exit_from_l2(&pcie->pci->pp);
  318. if (ret)
  319. return ret;
  320. return dw_pcie_resume_noirq(pcie->pci);
  321. }
  322. static const struct dev_pm_ops ls_pcie_pm_ops = {
  323. NOIRQ_SYSTEM_SLEEP_PM_OPS(ls_pcie_suspend_noirq, ls_pcie_resume_noirq)
  324. };
  325. static struct platform_driver ls_pcie_driver = {
  326. .probe = ls_pcie_probe,
  327. .driver = {
  328. .name = "layerscape-pcie",
  329. .of_match_table = ls_pcie_of_match,
  330. .suppress_bind_attrs = true,
  331. .pm = &ls_pcie_pm_ops,
  332. },
  333. };
  334. builtin_platform_driver(ls_pcie_driver);