pcie-intel-gw.c 10 KB

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