pcie-fu740.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * FU740 DesignWare PCIe Controller integration
  4. * Copyright (C) 2019-2021 SiFive, Inc.
  5. * Paul Walmsley
  6. * Greentime Hu
  7. *
  8. * Based in part on the i.MX6 PCIe host controller shim which is:
  9. *
  10. * Copyright (C) 2013 Kosagi
  11. * https://www.kosagi.com
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/gpio.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/resource.h>
  22. #include <linux/types.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/iopoll.h>
  25. #include <linux/reset.h>
  26. #include "pcie-designware.h"
  27. #define to_fu740_pcie(x) dev_get_drvdata((x)->dev)
  28. struct fu740_pcie {
  29. struct dw_pcie pci;
  30. void __iomem *mgmt_base;
  31. struct gpio_desc *reset;
  32. struct gpio_desc *pwren;
  33. struct clk *pcie_aux;
  34. struct reset_control *rst;
  35. };
  36. #define SIFIVE_DEVICESRESETREG 0x28
  37. #define PCIEX8MGMT_PERST_N 0x0
  38. #define PCIEX8MGMT_APP_LTSSM_ENABLE 0x10
  39. #define PCIEX8MGMT_APP_HOLD_PHY_RST 0x18
  40. #define PCIEX8MGMT_DEVICE_TYPE 0x708
  41. #define PCIEX8MGMT_PHY0_CR_PARA_ADDR 0x860
  42. #define PCIEX8MGMT_PHY0_CR_PARA_RD_EN 0x870
  43. #define PCIEX8MGMT_PHY0_CR_PARA_RD_DATA 0x878
  44. #define PCIEX8MGMT_PHY0_CR_PARA_SEL 0x880
  45. #define PCIEX8MGMT_PHY0_CR_PARA_WR_DATA 0x888
  46. #define PCIEX8MGMT_PHY0_CR_PARA_WR_EN 0x890
  47. #define PCIEX8MGMT_PHY0_CR_PARA_ACK 0x898
  48. #define PCIEX8MGMT_PHY1_CR_PARA_ADDR 0x8a0
  49. #define PCIEX8MGMT_PHY1_CR_PARA_RD_EN 0x8b0
  50. #define PCIEX8MGMT_PHY1_CR_PARA_RD_DATA 0x8b8
  51. #define PCIEX8MGMT_PHY1_CR_PARA_SEL 0x8c0
  52. #define PCIEX8MGMT_PHY1_CR_PARA_WR_DATA 0x8c8
  53. #define PCIEX8MGMT_PHY1_CR_PARA_WR_EN 0x8d0
  54. #define PCIEX8MGMT_PHY1_CR_PARA_ACK 0x8d8
  55. #define PCIEX8MGMT_PHY_CDR_TRACK_EN BIT(0)
  56. #define PCIEX8MGMT_PHY_LOS_THRSHLD BIT(5)
  57. #define PCIEX8MGMT_PHY_TERM_EN BIT(9)
  58. #define PCIEX8MGMT_PHY_TERM_ACDC BIT(10)
  59. #define PCIEX8MGMT_PHY_EN BIT(11)
  60. #define PCIEX8MGMT_PHY_INIT_VAL (PCIEX8MGMT_PHY_CDR_TRACK_EN|\
  61. PCIEX8MGMT_PHY_LOS_THRSHLD|\
  62. PCIEX8MGMT_PHY_TERM_EN|\
  63. PCIEX8MGMT_PHY_TERM_ACDC|\
  64. PCIEX8MGMT_PHY_EN)
  65. #define PCIEX8MGMT_PHY_LANEN_DIG_ASIC_RX_OVRD_IN_3 0x1008
  66. #define PCIEX8MGMT_PHY_LANE_OFF 0x100
  67. #define PCIEX8MGMT_PHY_LANE0_BASE (PCIEX8MGMT_PHY_LANEN_DIG_ASIC_RX_OVRD_IN_3 + 0x100 * 0)
  68. #define PCIEX8MGMT_PHY_LANE1_BASE (PCIEX8MGMT_PHY_LANEN_DIG_ASIC_RX_OVRD_IN_3 + 0x100 * 1)
  69. #define PCIEX8MGMT_PHY_LANE2_BASE (PCIEX8MGMT_PHY_LANEN_DIG_ASIC_RX_OVRD_IN_3 + 0x100 * 2)
  70. #define PCIEX8MGMT_PHY_LANE3_BASE (PCIEX8MGMT_PHY_LANEN_DIG_ASIC_RX_OVRD_IN_3 + 0x100 * 3)
  71. static void fu740_pcie_assert_reset(struct fu740_pcie *afp)
  72. {
  73. /* Assert PERST_N GPIO */
  74. gpiod_set_value_cansleep(afp->reset, 0);
  75. /* Assert controller PERST_N */
  76. writel_relaxed(0x0, afp->mgmt_base + PCIEX8MGMT_PERST_N);
  77. }
  78. static void fu740_pcie_deassert_reset(struct fu740_pcie *afp)
  79. {
  80. /* Deassert controller PERST_N */
  81. writel_relaxed(0x1, afp->mgmt_base + PCIEX8MGMT_PERST_N);
  82. /* Deassert PERST_N GPIO */
  83. gpiod_set_value_cansleep(afp->reset, 1);
  84. }
  85. static void fu740_pcie_power_on(struct fu740_pcie *afp)
  86. {
  87. gpiod_set_value_cansleep(afp->pwren, 1);
  88. /*
  89. * Ensure that PERST has been asserted for at least 100 ms.
  90. * Section 2.2 of PCI Express Card Electromechanical Specification
  91. * Revision 3.0
  92. */
  93. msleep(100);
  94. }
  95. static void fu740_pcie_drive_reset(struct fu740_pcie *afp)
  96. {
  97. fu740_pcie_assert_reset(afp);
  98. fu740_pcie_power_on(afp);
  99. fu740_pcie_deassert_reset(afp);
  100. }
  101. static void fu740_phyregwrite(const uint8_t phy, const uint16_t addr,
  102. const uint16_t wrdata, struct fu740_pcie *afp)
  103. {
  104. struct device *dev = afp->pci.dev;
  105. void __iomem *phy_cr_para_addr;
  106. void __iomem *phy_cr_para_wr_data;
  107. void __iomem *phy_cr_para_wr_en;
  108. void __iomem *phy_cr_para_ack;
  109. int ret, val;
  110. /* Setup */
  111. if (phy) {
  112. phy_cr_para_addr = afp->mgmt_base + PCIEX8MGMT_PHY1_CR_PARA_ADDR;
  113. phy_cr_para_wr_data = afp->mgmt_base + PCIEX8MGMT_PHY1_CR_PARA_WR_DATA;
  114. phy_cr_para_wr_en = afp->mgmt_base + PCIEX8MGMT_PHY1_CR_PARA_WR_EN;
  115. phy_cr_para_ack = afp->mgmt_base + PCIEX8MGMT_PHY1_CR_PARA_ACK;
  116. } else {
  117. phy_cr_para_addr = afp->mgmt_base + PCIEX8MGMT_PHY0_CR_PARA_ADDR;
  118. phy_cr_para_wr_data = afp->mgmt_base + PCIEX8MGMT_PHY0_CR_PARA_WR_DATA;
  119. phy_cr_para_wr_en = afp->mgmt_base + PCIEX8MGMT_PHY0_CR_PARA_WR_EN;
  120. phy_cr_para_ack = afp->mgmt_base + PCIEX8MGMT_PHY0_CR_PARA_ACK;
  121. }
  122. writel_relaxed(addr, phy_cr_para_addr);
  123. writel_relaxed(wrdata, phy_cr_para_wr_data);
  124. writel_relaxed(1, phy_cr_para_wr_en);
  125. /* Wait for wait_idle */
  126. ret = readl_poll_timeout(phy_cr_para_ack, val, val, 10, 5000);
  127. if (ret)
  128. dev_warn(dev, "Wait for wait_idle state failed!\n");
  129. /* Clear */
  130. writel_relaxed(0, phy_cr_para_wr_en);
  131. /* Wait for ~wait_idle */
  132. ret = readl_poll_timeout(phy_cr_para_ack, val, !val, 10, 5000);
  133. if (ret)
  134. dev_warn(dev, "Wait for !wait_idle state failed!\n");
  135. }
  136. static void fu740_pcie_init_phy(struct fu740_pcie *afp)
  137. {
  138. /* Enable phy cr_para_sel interfaces */
  139. writel_relaxed(0x1, afp->mgmt_base + PCIEX8MGMT_PHY0_CR_PARA_SEL);
  140. writel_relaxed(0x1, afp->mgmt_base + PCIEX8MGMT_PHY1_CR_PARA_SEL);
  141. /*
  142. * Wait 10 cr_para cycles to guarantee that the registers are ready
  143. * to be edited.
  144. */
  145. ndelay(10);
  146. /* Set PHY AC termination mode */
  147. fu740_phyregwrite(0, PCIEX8MGMT_PHY_LANE0_BASE, PCIEX8MGMT_PHY_INIT_VAL, afp);
  148. fu740_phyregwrite(0, PCIEX8MGMT_PHY_LANE1_BASE, PCIEX8MGMT_PHY_INIT_VAL, afp);
  149. fu740_phyregwrite(0, PCIEX8MGMT_PHY_LANE2_BASE, PCIEX8MGMT_PHY_INIT_VAL, afp);
  150. fu740_phyregwrite(0, PCIEX8MGMT_PHY_LANE3_BASE, PCIEX8MGMT_PHY_INIT_VAL, afp);
  151. fu740_phyregwrite(1, PCIEX8MGMT_PHY_LANE0_BASE, PCIEX8MGMT_PHY_INIT_VAL, afp);
  152. fu740_phyregwrite(1, PCIEX8MGMT_PHY_LANE1_BASE, PCIEX8MGMT_PHY_INIT_VAL, afp);
  153. fu740_phyregwrite(1, PCIEX8MGMT_PHY_LANE2_BASE, PCIEX8MGMT_PHY_INIT_VAL, afp);
  154. fu740_phyregwrite(1, PCIEX8MGMT_PHY_LANE3_BASE, PCIEX8MGMT_PHY_INIT_VAL, afp);
  155. }
  156. static int fu740_pcie_start_link(struct dw_pcie *pci)
  157. {
  158. struct device *dev = pci->dev;
  159. struct fu740_pcie *afp = dev_get_drvdata(dev);
  160. u8 cap_exp = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
  161. int ret;
  162. u32 orig, tmp;
  163. /*
  164. * Force 2.5GT/s when starting the link, due to some devices not
  165. * probing at higher speeds. This happens with the PCIe switch
  166. * on the Unmatched board when U-Boot has not initialised the PCIe.
  167. * The fix in U-Boot is to force 2.5GT/s, which then gets cleared
  168. * by the soft reset done by this driver.
  169. */
  170. dev_dbg(dev, "cap_exp at %x\n", cap_exp);
  171. dw_pcie_dbi_ro_wr_en(pci);
  172. tmp = dw_pcie_readl_dbi(pci, cap_exp + PCI_EXP_LNKCAP);
  173. orig = tmp & PCI_EXP_LNKCAP_SLS;
  174. tmp &= ~PCI_EXP_LNKCAP_SLS;
  175. tmp |= PCI_EXP_LNKCAP_SLS_2_5GB;
  176. dw_pcie_writel_dbi(pci, cap_exp + PCI_EXP_LNKCAP, tmp);
  177. /* Enable LTSSM */
  178. writel_relaxed(0x1, afp->mgmt_base + PCIEX8MGMT_APP_LTSSM_ENABLE);
  179. ret = dw_pcie_wait_for_link(pci);
  180. if (ret) {
  181. dev_err(dev, "error: link did not start\n");
  182. goto err;
  183. }
  184. tmp = dw_pcie_readl_dbi(pci, cap_exp + PCI_EXP_LNKCAP);
  185. if ((tmp & PCI_EXP_LNKCAP_SLS) != orig) {
  186. dev_dbg(dev, "changing speed back to original\n");
  187. tmp &= ~PCI_EXP_LNKCAP_SLS;
  188. tmp |= orig;
  189. dw_pcie_writel_dbi(pci, cap_exp + PCI_EXP_LNKCAP, tmp);
  190. tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
  191. tmp |= PORT_LOGIC_SPEED_CHANGE;
  192. dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
  193. ret = dw_pcie_wait_for_link(pci);
  194. if (ret) {
  195. dev_err(dev, "error: link did not start at new speed\n");
  196. goto err;
  197. }
  198. }
  199. ret = 0;
  200. err:
  201. WARN_ON(ret); /* we assume that errors will be very rare */
  202. dw_pcie_dbi_ro_wr_dis(pci);
  203. return ret;
  204. }
  205. static int fu740_pcie_host_init(struct dw_pcie_rp *pp)
  206. {
  207. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  208. struct fu740_pcie *afp = to_fu740_pcie(pci);
  209. struct device *dev = pci->dev;
  210. int ret;
  211. /* Power on reset */
  212. fu740_pcie_drive_reset(afp);
  213. /* Enable pcieauxclk */
  214. ret = clk_prepare_enable(afp->pcie_aux);
  215. if (ret) {
  216. dev_err(dev, "unable to enable pcie_aux clock\n");
  217. return ret;
  218. }
  219. /*
  220. * Assert hold_phy_rst (hold the controller LTSSM in reset after
  221. * power_up_rst_n for register programming with cr_para)
  222. */
  223. writel_relaxed(0x1, afp->mgmt_base + PCIEX8MGMT_APP_HOLD_PHY_RST);
  224. /* Deassert power_up_rst_n */
  225. ret = reset_control_deassert(afp->rst);
  226. if (ret) {
  227. dev_err(dev, "unable to deassert pcie_power_up_rst_n\n");
  228. return ret;
  229. }
  230. fu740_pcie_init_phy(afp);
  231. /* Disable pcieauxclk */
  232. clk_disable_unprepare(afp->pcie_aux);
  233. /* Clear hold_phy_rst */
  234. writel_relaxed(0x0, afp->mgmt_base + PCIEX8MGMT_APP_HOLD_PHY_RST);
  235. /* Enable pcieauxclk */
  236. clk_prepare_enable(afp->pcie_aux);
  237. /* Set RC mode */
  238. writel_relaxed(0x4, afp->mgmt_base + PCIEX8MGMT_DEVICE_TYPE);
  239. return 0;
  240. }
  241. static const struct dw_pcie_host_ops fu740_pcie_host_ops = {
  242. .host_init = fu740_pcie_host_init,
  243. };
  244. static const struct dw_pcie_ops dw_pcie_ops = {
  245. .start_link = fu740_pcie_start_link,
  246. };
  247. static int fu740_pcie_probe(struct platform_device *pdev)
  248. {
  249. struct device *dev = &pdev->dev;
  250. struct dw_pcie *pci;
  251. struct fu740_pcie *afp;
  252. afp = devm_kzalloc(dev, sizeof(*afp), GFP_KERNEL);
  253. if (!afp)
  254. return -ENOMEM;
  255. pci = &afp->pci;
  256. pci->dev = dev;
  257. pci->ops = &dw_pcie_ops;
  258. pci->pp.ops = &fu740_pcie_host_ops;
  259. pci->pp.num_vectors = MAX_MSI_IRQS;
  260. /* SiFive specific region: mgmt */
  261. afp->mgmt_base = devm_platform_ioremap_resource_byname(pdev, "mgmt");
  262. if (IS_ERR(afp->mgmt_base))
  263. return PTR_ERR(afp->mgmt_base);
  264. /* Fetch GPIOs */
  265. afp->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  266. if (IS_ERR(afp->reset))
  267. return dev_err_probe(dev, PTR_ERR(afp->reset), "unable to get reset-gpios\n");
  268. afp->pwren = devm_gpiod_get_optional(dev, "pwren", GPIOD_OUT_LOW);
  269. if (IS_ERR(afp->pwren))
  270. return dev_err_probe(dev, PTR_ERR(afp->pwren), "unable to get pwren-gpios\n");
  271. /* Fetch clocks */
  272. afp->pcie_aux = devm_clk_get(dev, "pcie_aux");
  273. if (IS_ERR(afp->pcie_aux))
  274. return dev_err_probe(dev, PTR_ERR(afp->pcie_aux),
  275. "pcie_aux clock source missing or invalid\n");
  276. /* Fetch reset */
  277. afp->rst = devm_reset_control_get_exclusive(dev, NULL);
  278. if (IS_ERR(afp->rst))
  279. return dev_err_probe(dev, PTR_ERR(afp->rst), "unable to get reset\n");
  280. platform_set_drvdata(pdev, afp);
  281. return dw_pcie_host_init(&pci->pp);
  282. }
  283. static void fu740_pcie_shutdown(struct platform_device *pdev)
  284. {
  285. struct fu740_pcie *afp = platform_get_drvdata(pdev);
  286. /* Bring down link, so bootloader gets clean state in case of reboot */
  287. fu740_pcie_assert_reset(afp);
  288. }
  289. static const struct of_device_id fu740_pcie_of_match[] = {
  290. { .compatible = "sifive,fu740-pcie", },
  291. {},
  292. };
  293. static struct platform_driver fu740_pcie_driver = {
  294. .driver = {
  295. .name = "fu740-pcie",
  296. .of_match_table = fu740_pcie_of_match,
  297. .suppress_bind_attrs = true,
  298. },
  299. .probe = fu740_pcie_probe,
  300. .shutdown = fu740_pcie_shutdown,
  301. };
  302. builtin_platform_driver(fu740_pcie_driver);