pcie-intel-gw.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Intel Gateway SoCs
  4. *
  5. * Copyright (c) 2019 Intel Corporation.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/clk.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/pci_regs.h>
  13. #include <linux/phy/phy.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/property.h>
  16. #include <linux/reset.h>
  17. #include "../../pci.h"
  18. #include "pcie-designware.h"
  19. #define PORT_AFR_N_FTS_GEN12_DFT (SZ_128 - 1)
  20. #define PORT_AFR_N_FTS_GEN3 180
  21. #define PORT_AFR_N_FTS_GEN4 196
  22. /* PCIe Application logic Registers */
  23. #define PCIE_APP_CCR 0x10
  24. #define PCIE_APP_CCR_LTSSM_ENABLE BIT(0)
  25. #define PCIE_APP_MSG_CR 0x30
  26. #define PCIE_APP_MSG_XMT_PM_TURNOFF BIT(0)
  27. #define PCIE_APP_PMC 0x44
  28. #define PCIE_APP_PMC_IN_L2 BIT(20)
  29. #define PCIE_APP_IRNEN 0xF4
  30. #define PCIE_APP_IRNCR 0xF8
  31. #define PCIE_APP_IRN_AER_REPORT BIT(0)
  32. #define PCIE_APP_IRN_PME BIT(2)
  33. #define PCIE_APP_IRN_RX_VDM_MSG BIT(4)
  34. #define PCIE_APP_IRN_PM_TO_ACK BIT(9)
  35. #define PCIE_APP_IRN_LINK_AUTO_BW_STAT BIT(11)
  36. #define PCIE_APP_IRN_BW_MGT BIT(12)
  37. #define PCIE_APP_IRN_INTA BIT(13)
  38. #define PCIE_APP_IRN_INTB BIT(14)
  39. #define PCIE_APP_IRN_INTC BIT(15)
  40. #define PCIE_APP_IRN_INTD BIT(16)
  41. #define PCIE_APP_IRN_MSG_LTR BIT(18)
  42. #define PCIE_APP_IRN_SYS_ERR_RC BIT(29)
  43. #define PCIE_APP_INTX_OFST 12
  44. #define PCIE_APP_IRN_INT \
  45. (PCIE_APP_IRN_AER_REPORT | PCIE_APP_IRN_PME | \
  46. PCIE_APP_IRN_RX_VDM_MSG | PCIE_APP_IRN_SYS_ERR_RC | \
  47. PCIE_APP_IRN_PM_TO_ACK | PCIE_APP_IRN_MSG_LTR | \
  48. PCIE_APP_IRN_BW_MGT | PCIE_APP_IRN_LINK_AUTO_BW_STAT | \
  49. PCIE_APP_IRN_INTA | PCIE_APP_IRN_INTB | \
  50. PCIE_APP_IRN_INTC | PCIE_APP_IRN_INTD)
  51. #define RESET_INTERVAL_MS 100
  52. struct intel_pcie {
  53. struct dw_pcie pci;
  54. void __iomem *app_base;
  55. struct gpio_desc *reset_gpio;
  56. u32 rst_intrvl;
  57. struct clk *core_clk;
  58. struct reset_control *core_rst;
  59. struct phy *phy;
  60. };
  61. static void pcie_update_bits(void __iomem *base, u32 ofs, u32 mask, u32 val)
  62. {
  63. u32 old;
  64. old = readl(base + ofs);
  65. val = (old & ~mask) | (val & mask);
  66. if (val != old)
  67. writel(val, base + ofs);
  68. }
  69. static inline void pcie_app_wr(struct intel_pcie *pcie, u32 ofs, u32 val)
  70. {
  71. writel(val, pcie->app_base + ofs);
  72. }
  73. static void pcie_app_wr_mask(struct intel_pcie *pcie, u32 ofs,
  74. u32 mask, u32 val)
  75. {
  76. pcie_update_bits(pcie->app_base, ofs, mask, val);
  77. }
  78. static inline u32 pcie_rc_cfg_rd(struct intel_pcie *pcie, u32 ofs)
  79. {
  80. return dw_pcie_readl_dbi(&pcie->pci, ofs);
  81. }
  82. static inline void pcie_rc_cfg_wr(struct intel_pcie *pcie, u32 ofs, u32 val)
  83. {
  84. dw_pcie_writel_dbi(&pcie->pci, ofs, val);
  85. }
  86. static void pcie_rc_cfg_wr_mask(struct intel_pcie *pcie, u32 ofs,
  87. u32 mask, u32 val)
  88. {
  89. pcie_update_bits(pcie->pci.dbi_base, ofs, mask, val);
  90. }
  91. static void intel_pcie_ltssm_enable(struct intel_pcie *pcie)
  92. {
  93. pcie_app_wr_mask(pcie, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE,
  94. PCIE_APP_CCR_LTSSM_ENABLE);
  95. }
  96. static void intel_pcie_ltssm_disable(struct intel_pcie *pcie)
  97. {
  98. pcie_app_wr_mask(pcie, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE, 0);
  99. }
  100. static void intel_pcie_link_setup(struct intel_pcie *pcie)
  101. {
  102. u32 val;
  103. u8 offset = dw_pcie_find_capability(&pcie->pci, PCI_CAP_ID_EXP);
  104. val = pcie_rc_cfg_rd(pcie, offset + PCI_EXP_LNKCTL);
  105. val &= ~(PCI_EXP_LNKCTL_LD | PCI_EXP_LNKCTL_ASPMC);
  106. pcie_rc_cfg_wr(pcie, offset + PCI_EXP_LNKCTL, val);
  107. }
  108. static void intel_pcie_init_n_fts(struct dw_pcie *pci)
  109. {
  110. switch (pci->max_link_speed) {
  111. case 3:
  112. pci->n_fts[1] = PORT_AFR_N_FTS_GEN3;
  113. break;
  114. case 4:
  115. pci->n_fts[1] = PORT_AFR_N_FTS_GEN4;
  116. break;
  117. default:
  118. pci->n_fts[1] = PORT_AFR_N_FTS_GEN12_DFT;
  119. break;
  120. }
  121. pci->n_fts[0] = PORT_AFR_N_FTS_GEN12_DFT;
  122. }
  123. static int intel_pcie_ep_rst_init(struct intel_pcie *pcie)
  124. {
  125. struct device *dev = pcie->pci.dev;
  126. int ret;
  127. pcie->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
  128. if (IS_ERR(pcie->reset_gpio)) {
  129. ret = PTR_ERR(pcie->reset_gpio);
  130. if (ret != -EPROBE_DEFER)
  131. dev_err(dev, "Failed to request PCIe GPIO: %d\n", ret);
  132. return ret;
  133. }
  134. /* Make initial reset last for 100us */
  135. usleep_range(100, 200);
  136. return 0;
  137. }
  138. static void intel_pcie_core_rst_assert(struct intel_pcie *pcie)
  139. {
  140. reset_control_assert(pcie->core_rst);
  141. }
  142. static void intel_pcie_core_rst_deassert(struct intel_pcie *pcie)
  143. {
  144. /*
  145. * One micro-second delay to make sure the reset pulse
  146. * wide enough so that core reset is clean.
  147. */
  148. udelay(1);
  149. reset_control_deassert(pcie->core_rst);
  150. /*
  151. * Some SoC core reset also reset PHY, more delay needed
  152. * to make sure the reset process is done.
  153. */
  154. usleep_range(1000, 2000);
  155. }
  156. static void intel_pcie_device_rst_assert(struct intel_pcie *pcie)
  157. {
  158. gpiod_set_value_cansleep(pcie->reset_gpio, 1);
  159. }
  160. static void intel_pcie_device_rst_deassert(struct intel_pcie *pcie)
  161. {
  162. msleep(pcie->rst_intrvl);
  163. gpiod_set_value_cansleep(pcie->reset_gpio, 0);
  164. }
  165. static void intel_pcie_core_irq_disable(struct intel_pcie *pcie)
  166. {
  167. pcie_app_wr(pcie, PCIE_APP_IRNEN, 0);
  168. pcie_app_wr(pcie, PCIE_APP_IRNCR, PCIE_APP_IRN_INT);
  169. }
  170. static int intel_pcie_get_resources(struct platform_device *pdev)
  171. {
  172. struct intel_pcie *pcie = platform_get_drvdata(pdev);
  173. struct dw_pcie *pci = &pcie->pci;
  174. struct device *dev = pci->dev;
  175. int ret;
  176. pcie->core_clk = devm_clk_get(dev, NULL);
  177. if (IS_ERR(pcie->core_clk)) {
  178. ret = PTR_ERR(pcie->core_clk);
  179. if (ret != -EPROBE_DEFER)
  180. dev_err(dev, "Failed to get clks: %d\n", ret);
  181. return ret;
  182. }
  183. pcie->core_rst = devm_reset_control_get(dev, NULL);
  184. if (IS_ERR(pcie->core_rst)) {
  185. ret = PTR_ERR(pcie->core_rst);
  186. if (ret != -EPROBE_DEFER)
  187. dev_err(dev, "Failed to get resets: %d\n", ret);
  188. return ret;
  189. }
  190. ret = device_property_read_u32(dev, "reset-assert-ms",
  191. &pcie->rst_intrvl);
  192. if (ret)
  193. pcie->rst_intrvl = RESET_INTERVAL_MS;
  194. pcie->app_base = devm_platform_ioremap_resource_byname(pdev, "app");
  195. if (IS_ERR(pcie->app_base))
  196. return PTR_ERR(pcie->app_base);
  197. pcie->phy = devm_phy_get(dev, "pcie");
  198. if (IS_ERR(pcie->phy)) {
  199. ret = PTR_ERR(pcie->phy);
  200. if (ret != -EPROBE_DEFER)
  201. dev_err(dev, "Couldn't get pcie-phy: %d\n", ret);
  202. return ret;
  203. }
  204. return 0;
  205. }
  206. static int intel_pcie_wait_l2(struct intel_pcie *pcie)
  207. {
  208. u32 value;
  209. int ret;
  210. struct dw_pcie *pci = &pcie->pci;
  211. if (pci->max_link_speed < 3)
  212. return 0;
  213. /* Send PME_TURN_OFF message */
  214. pcie_app_wr_mask(pcie, PCIE_APP_MSG_CR, PCIE_APP_MSG_XMT_PM_TURNOFF,
  215. PCIE_APP_MSG_XMT_PM_TURNOFF);
  216. /* Read PMC status and wait for falling into L2 link state */
  217. ret = readl_poll_timeout(pcie->app_base + PCIE_APP_PMC, value,
  218. value & PCIE_APP_PMC_IN_L2, 20,
  219. jiffies_to_usecs(5 * HZ));
  220. if (ret)
  221. dev_err(pcie->pci.dev, "PCIe link enter L2 timeout!\n");
  222. return ret;
  223. }
  224. static void intel_pcie_turn_off(struct intel_pcie *pcie)
  225. {
  226. if (dw_pcie_link_up(&pcie->pci))
  227. intel_pcie_wait_l2(pcie);
  228. /* Put endpoint device in reset state */
  229. intel_pcie_device_rst_assert(pcie);
  230. pcie_rc_cfg_wr_mask(pcie, PCI_COMMAND, PCI_COMMAND_MEMORY, 0);
  231. }
  232. static int intel_pcie_host_setup(struct intel_pcie *pcie)
  233. {
  234. int ret;
  235. struct dw_pcie *pci = &pcie->pci;
  236. intel_pcie_core_rst_assert(pcie);
  237. intel_pcie_device_rst_assert(pcie);
  238. ret = phy_init(pcie->phy);
  239. if (ret)
  240. return ret;
  241. intel_pcie_core_rst_deassert(pcie);
  242. ret = clk_prepare_enable(pcie->core_clk);
  243. if (ret) {
  244. dev_err(pcie->pci.dev, "Core clock enable failed: %d\n", ret);
  245. goto clk_err;
  246. }
  247. pci->atu_base = pci->dbi_base + 0xC0000;
  248. intel_pcie_ltssm_disable(pcie);
  249. intel_pcie_link_setup(pcie);
  250. intel_pcie_init_n_fts(pci);
  251. ret = dw_pcie_setup_rc(&pci->pp);
  252. if (ret)
  253. goto app_init_err;
  254. dw_pcie_upconfig_setup(pci);
  255. intel_pcie_device_rst_deassert(pcie);
  256. intel_pcie_ltssm_enable(pcie);
  257. ret = dw_pcie_wait_for_link(pci);
  258. if (ret)
  259. goto app_init_err;
  260. /* Enable integrated interrupts */
  261. pcie_app_wr_mask(pcie, PCIE_APP_IRNEN, PCIE_APP_IRN_INT,
  262. PCIE_APP_IRN_INT);
  263. return 0;
  264. app_init_err:
  265. clk_disable_unprepare(pcie->core_clk);
  266. clk_err:
  267. intel_pcie_core_rst_assert(pcie);
  268. phy_exit(pcie->phy);
  269. return ret;
  270. }
  271. static void __intel_pcie_remove(struct intel_pcie *pcie)
  272. {
  273. intel_pcie_core_irq_disable(pcie);
  274. intel_pcie_turn_off(pcie);
  275. clk_disable_unprepare(pcie->core_clk);
  276. intel_pcie_core_rst_assert(pcie);
  277. phy_exit(pcie->phy);
  278. }
  279. static void intel_pcie_remove(struct platform_device *pdev)
  280. {
  281. struct intel_pcie *pcie = platform_get_drvdata(pdev);
  282. struct dw_pcie_rp *pp = &pcie->pci.pp;
  283. dw_pcie_host_deinit(pp);
  284. __intel_pcie_remove(pcie);
  285. }
  286. static int intel_pcie_suspend_noirq(struct device *dev)
  287. {
  288. struct intel_pcie *pcie = dev_get_drvdata(dev);
  289. int ret;
  290. intel_pcie_core_irq_disable(pcie);
  291. ret = intel_pcie_wait_l2(pcie);
  292. if (ret)
  293. return ret;
  294. phy_exit(pcie->phy);
  295. clk_disable_unprepare(pcie->core_clk);
  296. return ret;
  297. }
  298. static int intel_pcie_resume_noirq(struct device *dev)
  299. {
  300. struct intel_pcie *pcie = dev_get_drvdata(dev);
  301. return intel_pcie_host_setup(pcie);
  302. }
  303. static int intel_pcie_rc_init(struct dw_pcie_rp *pp)
  304. {
  305. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  306. struct intel_pcie *pcie = dev_get_drvdata(pci->dev);
  307. return intel_pcie_host_setup(pcie);
  308. }
  309. static const struct dw_pcie_ops intel_pcie_ops = {
  310. };
  311. static const struct dw_pcie_host_ops intel_pcie_dw_ops = {
  312. .init = intel_pcie_rc_init,
  313. };
  314. static int intel_pcie_probe(struct platform_device *pdev)
  315. {
  316. struct device *dev = &pdev->dev;
  317. struct intel_pcie *pcie;
  318. struct dw_pcie_rp *pp;
  319. struct dw_pcie *pci;
  320. int ret;
  321. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  322. if (!pcie)
  323. return -ENOMEM;
  324. platform_set_drvdata(pdev, pcie);
  325. pci = &pcie->pci;
  326. pci->dev = dev;
  327. pci->use_parent_dt_ranges = true;
  328. pp = &pci->pp;
  329. ret = intel_pcie_get_resources(pdev);
  330. if (ret)
  331. return ret;
  332. ret = intel_pcie_ep_rst_init(pcie);
  333. if (ret)
  334. return ret;
  335. pci->ops = &intel_pcie_ops;
  336. pp->ops = &intel_pcie_dw_ops;
  337. ret = dw_pcie_host_init(pp);
  338. if (ret) {
  339. dev_err(dev, "Cannot initialize host\n");
  340. return ret;
  341. }
  342. return 0;
  343. }
  344. static const struct dev_pm_ops intel_pcie_pm_ops = {
  345. NOIRQ_SYSTEM_SLEEP_PM_OPS(intel_pcie_suspend_noirq,
  346. intel_pcie_resume_noirq)
  347. };
  348. static const struct of_device_id of_intel_pcie_match[] = {
  349. { .compatible = "intel,lgm-pcie" },
  350. {}
  351. };
  352. static struct platform_driver intel_pcie_driver = {
  353. .probe = intel_pcie_probe,
  354. .remove = intel_pcie_remove,
  355. .driver = {
  356. .name = "intel-gw-pcie",
  357. .of_match_table = of_intel_pcie_match,
  358. .pm = &intel_pcie_pm_ops,
  359. },
  360. };
  361. builtin_platform_driver(intel_pcie_driver);