pcie-uniphier-ep.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe endpoint controller driver for UniPhier SoCs
  4. * Copyright 2018 Socionext Inc.
  5. * Author: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/init.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/of.h>
  14. #include <linux/pci.h>
  15. #include <linux/phy/phy.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/reset.h>
  18. #include "pcie-designware.h"
  19. /* Link Glue registers */
  20. #define PCL_RSTCTRL0 0x0010
  21. #define PCL_RSTCTRL_AXI_REG BIT(3)
  22. #define PCL_RSTCTRL_AXI_SLAVE BIT(2)
  23. #define PCL_RSTCTRL_AXI_MASTER BIT(1)
  24. #define PCL_RSTCTRL_PIPE3 BIT(0)
  25. #define PCL_RSTCTRL1 0x0020
  26. #define PCL_RSTCTRL_PERST BIT(0)
  27. #define PCL_RSTCTRL2 0x0024
  28. #define PCL_RSTCTRL_PHY_RESET BIT(0)
  29. #define PCL_PINCTRL0 0x002c
  30. #define PCL_PERST_PLDN_REGEN BIT(12)
  31. #define PCL_PERST_NOE_REGEN BIT(11)
  32. #define PCL_PERST_OUT_REGEN BIT(8)
  33. #define PCL_PERST_PLDN_REGVAL BIT(4)
  34. #define PCL_PERST_NOE_REGVAL BIT(3)
  35. #define PCL_PERST_OUT_REGVAL BIT(0)
  36. #define PCL_PIPEMON 0x0044
  37. #define PCL_PCLK_ALIVE BIT(15)
  38. #define PCL_MODE 0x8000
  39. #define PCL_MODE_REGEN BIT(8)
  40. #define PCL_MODE_REGVAL BIT(0)
  41. #define PCL_APP_CLK_CTRL 0x8004
  42. #define PCL_APP_CLK_REQ BIT(0)
  43. #define PCL_APP_READY_CTRL 0x8008
  44. #define PCL_APP_LTSSM_ENABLE BIT(0)
  45. #define PCL_APP_MSI0 0x8040
  46. #define PCL_APP_VEN_MSI_TC_MASK GENMASK(10, 8)
  47. #define PCL_APP_VEN_MSI_VECTOR_MASK GENMASK(4, 0)
  48. #define PCL_APP_MSI1 0x8044
  49. #define PCL_APP_MSI_REQ BIT(0)
  50. #define PCL_APP_INTX 0x8074
  51. #define PCL_APP_INTX_SYS_INT BIT(0)
  52. #define PCL_APP_PM0 0x8078
  53. #define PCL_SYS_AUX_PWR_DET BIT(8)
  54. /* assertion time of INTx in usec */
  55. #define PCL_INTX_WIDTH_USEC 30
  56. struct uniphier_pcie_ep_priv {
  57. void __iomem *base;
  58. struct dw_pcie pci;
  59. struct clk *clk, *clk_gio;
  60. struct reset_control *rst, *rst_gio;
  61. struct phy *phy;
  62. const struct uniphier_pcie_ep_soc_data *data;
  63. };
  64. struct uniphier_pcie_ep_soc_data {
  65. bool has_gio;
  66. void (*init)(struct uniphier_pcie_ep_priv *priv);
  67. int (*wait)(struct uniphier_pcie_ep_priv *priv);
  68. const struct pci_epc_features features;
  69. };
  70. #define to_uniphier_pcie(x) dev_get_drvdata((x)->dev)
  71. static void uniphier_pcie_ltssm_enable(struct uniphier_pcie_ep_priv *priv,
  72. bool enable)
  73. {
  74. u32 val;
  75. val = readl(priv->base + PCL_APP_READY_CTRL);
  76. if (enable)
  77. val |= PCL_APP_LTSSM_ENABLE;
  78. else
  79. val &= ~PCL_APP_LTSSM_ENABLE;
  80. writel(val, priv->base + PCL_APP_READY_CTRL);
  81. }
  82. static void uniphier_pcie_phy_reset(struct uniphier_pcie_ep_priv *priv,
  83. bool assert)
  84. {
  85. u32 val;
  86. val = readl(priv->base + PCL_RSTCTRL2);
  87. if (assert)
  88. val |= PCL_RSTCTRL_PHY_RESET;
  89. else
  90. val &= ~PCL_RSTCTRL_PHY_RESET;
  91. writel(val, priv->base + PCL_RSTCTRL2);
  92. }
  93. static void uniphier_pcie_pro5_init_ep(struct uniphier_pcie_ep_priv *priv)
  94. {
  95. u32 val;
  96. /* set EP mode */
  97. val = readl(priv->base + PCL_MODE);
  98. val |= PCL_MODE_REGEN | PCL_MODE_REGVAL;
  99. writel(val, priv->base + PCL_MODE);
  100. /* clock request */
  101. val = readl(priv->base + PCL_APP_CLK_CTRL);
  102. val &= ~PCL_APP_CLK_REQ;
  103. writel(val, priv->base + PCL_APP_CLK_CTRL);
  104. /* deassert PIPE3 and AXI reset */
  105. val = readl(priv->base + PCL_RSTCTRL0);
  106. val |= PCL_RSTCTRL_AXI_REG | PCL_RSTCTRL_AXI_SLAVE
  107. | PCL_RSTCTRL_AXI_MASTER | PCL_RSTCTRL_PIPE3;
  108. writel(val, priv->base + PCL_RSTCTRL0);
  109. uniphier_pcie_ltssm_enable(priv, false);
  110. msleep(100);
  111. }
  112. static void uniphier_pcie_nx1_init_ep(struct uniphier_pcie_ep_priv *priv)
  113. {
  114. u32 val;
  115. /* set EP mode */
  116. val = readl(priv->base + PCL_MODE);
  117. val |= PCL_MODE_REGEN | PCL_MODE_REGVAL;
  118. writel(val, priv->base + PCL_MODE);
  119. /* use auxiliary power detection */
  120. val = readl(priv->base + PCL_APP_PM0);
  121. val |= PCL_SYS_AUX_PWR_DET;
  122. writel(val, priv->base + PCL_APP_PM0);
  123. /* assert PERST# */
  124. val = readl(priv->base + PCL_PINCTRL0);
  125. val &= ~(PCL_PERST_NOE_REGVAL | PCL_PERST_OUT_REGVAL
  126. | PCL_PERST_PLDN_REGVAL);
  127. val |= PCL_PERST_NOE_REGEN | PCL_PERST_OUT_REGEN
  128. | PCL_PERST_PLDN_REGEN;
  129. writel(val, priv->base + PCL_PINCTRL0);
  130. uniphier_pcie_ltssm_enable(priv, false);
  131. usleep_range(100000, 200000);
  132. /* deassert PERST# */
  133. val = readl(priv->base + PCL_PINCTRL0);
  134. val |= PCL_PERST_OUT_REGVAL | PCL_PERST_OUT_REGEN;
  135. writel(val, priv->base + PCL_PINCTRL0);
  136. }
  137. static int uniphier_pcie_nx1_wait_ep(struct uniphier_pcie_ep_priv *priv)
  138. {
  139. u32 status;
  140. int ret;
  141. /* wait PIPE clock */
  142. ret = readl_poll_timeout(priv->base + PCL_PIPEMON, status,
  143. status & PCL_PCLK_ALIVE, 100000, 1000000);
  144. if (ret) {
  145. dev_err(priv->pci.dev,
  146. "Failed to initialize controller in EP mode\n");
  147. return ret;
  148. }
  149. return 0;
  150. }
  151. static int uniphier_pcie_start_link(struct dw_pcie *pci)
  152. {
  153. struct uniphier_pcie_ep_priv *priv = to_uniphier_pcie(pci);
  154. uniphier_pcie_ltssm_enable(priv, true);
  155. return 0;
  156. }
  157. static void uniphier_pcie_stop_link(struct dw_pcie *pci)
  158. {
  159. struct uniphier_pcie_ep_priv *priv = to_uniphier_pcie(pci);
  160. uniphier_pcie_ltssm_enable(priv, false);
  161. }
  162. static void uniphier_pcie_ep_init(struct dw_pcie_ep *ep)
  163. {
  164. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  165. enum pci_barno bar;
  166. for (bar = BAR_0; bar <= BAR_5; bar++)
  167. dw_pcie_ep_reset_bar(pci, bar);
  168. }
  169. static int uniphier_pcie_ep_raise_intx_irq(struct dw_pcie_ep *ep)
  170. {
  171. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  172. struct uniphier_pcie_ep_priv *priv = to_uniphier_pcie(pci);
  173. u32 val;
  174. /*
  175. * This makes pulse signal to send INTx to the RC, so this should
  176. * be cleared as soon as possible. This sequence is covered with
  177. * mutex in pci_epc_raise_irq().
  178. */
  179. /* assert INTx */
  180. val = readl(priv->base + PCL_APP_INTX);
  181. val |= PCL_APP_INTX_SYS_INT;
  182. writel(val, priv->base + PCL_APP_INTX);
  183. udelay(PCL_INTX_WIDTH_USEC);
  184. /* deassert INTx */
  185. val &= ~PCL_APP_INTX_SYS_INT;
  186. writel(val, priv->base + PCL_APP_INTX);
  187. return 0;
  188. }
  189. static int uniphier_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep,
  190. u8 func_no, u16 interrupt_num)
  191. {
  192. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  193. struct uniphier_pcie_ep_priv *priv = to_uniphier_pcie(pci);
  194. u32 val;
  195. val = FIELD_PREP(PCL_APP_VEN_MSI_TC_MASK, func_no)
  196. | FIELD_PREP(PCL_APP_VEN_MSI_VECTOR_MASK, interrupt_num - 1);
  197. writel(val, priv->base + PCL_APP_MSI0);
  198. val = readl(priv->base + PCL_APP_MSI1);
  199. val |= PCL_APP_MSI_REQ;
  200. writel(val, priv->base + PCL_APP_MSI1);
  201. return 0;
  202. }
  203. static int uniphier_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
  204. unsigned int type, u16 interrupt_num)
  205. {
  206. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  207. switch (type) {
  208. case PCI_IRQ_INTX:
  209. return uniphier_pcie_ep_raise_intx_irq(ep);
  210. case PCI_IRQ_MSI:
  211. return uniphier_pcie_ep_raise_msi_irq(ep, func_no,
  212. interrupt_num);
  213. default:
  214. dev_err(pci->dev, "UNKNOWN IRQ type (%d)\n", type);
  215. }
  216. return 0;
  217. }
  218. static const struct pci_epc_features*
  219. uniphier_pcie_get_features(struct dw_pcie_ep *ep)
  220. {
  221. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  222. struct uniphier_pcie_ep_priv *priv = to_uniphier_pcie(pci);
  223. return &priv->data->features;
  224. }
  225. static const struct dw_pcie_ep_ops uniphier_pcie_ep_ops = {
  226. .init = uniphier_pcie_ep_init,
  227. .raise_irq = uniphier_pcie_ep_raise_irq,
  228. .get_features = uniphier_pcie_get_features,
  229. };
  230. static int uniphier_pcie_ep_enable(struct uniphier_pcie_ep_priv *priv)
  231. {
  232. int ret;
  233. ret = clk_prepare_enable(priv->clk);
  234. if (ret)
  235. return ret;
  236. ret = clk_prepare_enable(priv->clk_gio);
  237. if (ret)
  238. goto out_clk_disable;
  239. ret = reset_control_deassert(priv->rst);
  240. if (ret)
  241. goto out_clk_gio_disable;
  242. ret = reset_control_deassert(priv->rst_gio);
  243. if (ret)
  244. goto out_rst_assert;
  245. if (priv->data->init)
  246. priv->data->init(priv);
  247. uniphier_pcie_phy_reset(priv, true);
  248. ret = phy_init(priv->phy);
  249. if (ret)
  250. goto out_rst_gio_assert;
  251. uniphier_pcie_phy_reset(priv, false);
  252. if (priv->data->wait) {
  253. ret = priv->data->wait(priv);
  254. if (ret)
  255. goto out_phy_exit;
  256. }
  257. return 0;
  258. out_phy_exit:
  259. phy_exit(priv->phy);
  260. out_rst_gio_assert:
  261. reset_control_assert(priv->rst_gio);
  262. out_rst_assert:
  263. reset_control_assert(priv->rst);
  264. out_clk_gio_disable:
  265. clk_disable_unprepare(priv->clk_gio);
  266. out_clk_disable:
  267. clk_disable_unprepare(priv->clk);
  268. return ret;
  269. }
  270. static const struct dw_pcie_ops dw_pcie_ops = {
  271. .start_link = uniphier_pcie_start_link,
  272. .stop_link = uniphier_pcie_stop_link,
  273. };
  274. static int uniphier_pcie_ep_probe(struct platform_device *pdev)
  275. {
  276. struct device *dev = &pdev->dev;
  277. struct uniphier_pcie_ep_priv *priv;
  278. int ret;
  279. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  280. if (!priv)
  281. return -ENOMEM;
  282. priv->data = of_device_get_match_data(dev);
  283. if (WARN_ON(!priv->data))
  284. return -EINVAL;
  285. priv->pci.dev = dev;
  286. priv->pci.ops = &dw_pcie_ops;
  287. priv->base = devm_platform_ioremap_resource_byname(pdev, "link");
  288. if (IS_ERR(priv->base))
  289. return PTR_ERR(priv->base);
  290. if (priv->data->has_gio) {
  291. priv->clk_gio = devm_clk_get(dev, "gio");
  292. if (IS_ERR(priv->clk_gio))
  293. return PTR_ERR(priv->clk_gio);
  294. priv->rst_gio = devm_reset_control_get_shared(dev, "gio");
  295. if (IS_ERR(priv->rst_gio))
  296. return PTR_ERR(priv->rst_gio);
  297. }
  298. priv->clk = devm_clk_get(dev, "link");
  299. if (IS_ERR(priv->clk))
  300. return PTR_ERR(priv->clk);
  301. priv->rst = devm_reset_control_get_shared(dev, "link");
  302. if (IS_ERR(priv->rst))
  303. return PTR_ERR(priv->rst);
  304. priv->phy = devm_phy_optional_get(dev, "pcie-phy");
  305. if (IS_ERR(priv->phy)) {
  306. ret = PTR_ERR(priv->phy);
  307. dev_err(dev, "Failed to get phy (%d)\n", ret);
  308. return ret;
  309. }
  310. platform_set_drvdata(pdev, priv);
  311. ret = uniphier_pcie_ep_enable(priv);
  312. if (ret)
  313. return ret;
  314. priv->pci.ep.ops = &uniphier_pcie_ep_ops;
  315. ret = dw_pcie_ep_init(&priv->pci.ep);
  316. if (ret)
  317. return ret;
  318. ret = dw_pcie_ep_init_registers(&priv->pci.ep);
  319. if (ret) {
  320. dev_err(dev, "Failed to initialize DWC endpoint registers\n");
  321. dw_pcie_ep_deinit(&priv->pci.ep);
  322. return ret;
  323. }
  324. pci_epc_init_notify(priv->pci.ep.epc);
  325. return 0;
  326. }
  327. static const struct uniphier_pcie_ep_soc_data uniphier_pro5_data = {
  328. .has_gio = true,
  329. .init = uniphier_pcie_pro5_init_ep,
  330. .wait = NULL,
  331. .features = {
  332. DWC_EPC_COMMON_FEATURES,
  333. .linkup_notifier = false,
  334. .msi_capable = true,
  335. .msix_capable = false,
  336. .align = 1 << 16,
  337. .bar[BAR_0] = { .only_64bit = true, },
  338. .bar[BAR_1] = { .type = BAR_RESERVED, },
  339. .bar[BAR_2] = { .only_64bit = true, },
  340. .bar[BAR_3] = { .type = BAR_RESERVED, },
  341. .bar[BAR_4] = { .type = BAR_RESERVED, },
  342. .bar[BAR_5] = { .type = BAR_RESERVED, },
  343. },
  344. };
  345. static const struct uniphier_pcie_ep_soc_data uniphier_nx1_data = {
  346. .has_gio = false,
  347. .init = uniphier_pcie_nx1_init_ep,
  348. .wait = uniphier_pcie_nx1_wait_ep,
  349. .features = {
  350. DWC_EPC_COMMON_FEATURES,
  351. .linkup_notifier = false,
  352. .msi_capable = true,
  353. .msix_capable = false,
  354. .align = 1 << 12,
  355. .bar[BAR_0] = { .only_64bit = true, },
  356. .bar[BAR_1] = { .type = BAR_RESERVED, },
  357. .bar[BAR_2] = { .only_64bit = true, },
  358. .bar[BAR_3] = { .type = BAR_RESERVED, },
  359. .bar[BAR_4] = { .only_64bit = true, },
  360. .bar[BAR_5] = { .type = BAR_RESERVED, },
  361. },
  362. };
  363. static const struct of_device_id uniphier_pcie_ep_match[] = {
  364. {
  365. .compatible = "socionext,uniphier-pro5-pcie-ep",
  366. .data = &uniphier_pro5_data,
  367. },
  368. {
  369. .compatible = "socionext,uniphier-nx1-pcie-ep",
  370. .data = &uniphier_nx1_data,
  371. },
  372. { /* sentinel */ },
  373. };
  374. static struct platform_driver uniphier_pcie_ep_driver = {
  375. .probe = uniphier_pcie_ep_probe,
  376. .driver = {
  377. .name = "uniphier-pcie-ep",
  378. .of_match_table = uniphier_pcie_ep_match,
  379. .suppress_bind_attrs = true,
  380. },
  381. };
  382. builtin_platform_driver(uniphier_pcie_ep_driver);