pcie-uniphier-ep.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 <[email protected]>
  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_device.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_legacy_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. enum pci_epc_irq_type type,
  205. u16 interrupt_num)
  206. {
  207. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  208. switch (type) {
  209. case PCI_EPC_IRQ_LEGACY:
  210. return uniphier_pcie_ep_raise_legacy_irq(ep);
  211. case PCI_EPC_IRQ_MSI:
  212. return uniphier_pcie_ep_raise_msi_irq(ep, func_no,
  213. interrupt_num);
  214. default:
  215. dev_err(pci->dev, "UNKNOWN IRQ type (%d)\n", type);
  216. }
  217. return 0;
  218. }
  219. static const struct pci_epc_features*
  220. uniphier_pcie_get_features(struct dw_pcie_ep *ep)
  221. {
  222. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  223. struct uniphier_pcie_ep_priv *priv = to_uniphier_pcie(pci);
  224. return &priv->data->features;
  225. }
  226. static const struct dw_pcie_ep_ops uniphier_pcie_ep_ops = {
  227. .ep_init = uniphier_pcie_ep_init,
  228. .raise_irq = uniphier_pcie_ep_raise_irq,
  229. .get_features = uniphier_pcie_get_features,
  230. };
  231. static int uniphier_pcie_ep_enable(struct uniphier_pcie_ep_priv *priv)
  232. {
  233. int ret;
  234. ret = clk_prepare_enable(priv->clk);
  235. if (ret)
  236. return ret;
  237. ret = clk_prepare_enable(priv->clk_gio);
  238. if (ret)
  239. goto out_clk_disable;
  240. ret = reset_control_deassert(priv->rst);
  241. if (ret)
  242. goto out_clk_gio_disable;
  243. ret = reset_control_deassert(priv->rst_gio);
  244. if (ret)
  245. goto out_rst_assert;
  246. if (priv->data->init)
  247. priv->data->init(priv);
  248. uniphier_pcie_phy_reset(priv, true);
  249. ret = phy_init(priv->phy);
  250. if (ret)
  251. goto out_rst_gio_assert;
  252. uniphier_pcie_phy_reset(priv, false);
  253. if (priv->data->wait) {
  254. ret = priv->data->wait(priv);
  255. if (ret)
  256. goto out_phy_exit;
  257. }
  258. return 0;
  259. out_phy_exit:
  260. phy_exit(priv->phy);
  261. out_rst_gio_assert:
  262. reset_control_assert(priv->rst_gio);
  263. out_rst_assert:
  264. reset_control_assert(priv->rst);
  265. out_clk_gio_disable:
  266. clk_disable_unprepare(priv->clk_gio);
  267. out_clk_disable:
  268. clk_disable_unprepare(priv->clk);
  269. return ret;
  270. }
  271. static const struct dw_pcie_ops dw_pcie_ops = {
  272. .start_link = uniphier_pcie_start_link,
  273. .stop_link = uniphier_pcie_stop_link,
  274. };
  275. static int uniphier_pcie_ep_probe(struct platform_device *pdev)
  276. {
  277. struct device *dev = &pdev->dev;
  278. struct uniphier_pcie_ep_priv *priv;
  279. int ret;
  280. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  281. if (!priv)
  282. return -ENOMEM;
  283. priv->data = of_device_get_match_data(dev);
  284. if (WARN_ON(!priv->data))
  285. return -EINVAL;
  286. priv->pci.dev = dev;
  287. priv->pci.ops = &dw_pcie_ops;
  288. priv->base = devm_platform_ioremap_resource_byname(pdev, "link");
  289. if (IS_ERR(priv->base))
  290. return PTR_ERR(priv->base);
  291. if (priv->data->has_gio) {
  292. priv->clk_gio = devm_clk_get(dev, "gio");
  293. if (IS_ERR(priv->clk_gio))
  294. return PTR_ERR(priv->clk_gio);
  295. priv->rst_gio = devm_reset_control_get_shared(dev, "gio");
  296. if (IS_ERR(priv->rst_gio))
  297. return PTR_ERR(priv->rst_gio);
  298. }
  299. priv->clk = devm_clk_get(dev, "link");
  300. if (IS_ERR(priv->clk))
  301. return PTR_ERR(priv->clk);
  302. priv->rst = devm_reset_control_get_shared(dev, "link");
  303. if (IS_ERR(priv->rst))
  304. return PTR_ERR(priv->rst);
  305. priv->phy = devm_phy_optional_get(dev, "pcie-phy");
  306. if (IS_ERR(priv->phy)) {
  307. ret = PTR_ERR(priv->phy);
  308. dev_err(dev, "Failed to get phy (%d)\n", ret);
  309. return ret;
  310. }
  311. platform_set_drvdata(pdev, priv);
  312. ret = uniphier_pcie_ep_enable(priv);
  313. if (ret)
  314. return ret;
  315. priv->pci.ep.ops = &uniphier_pcie_ep_ops;
  316. return dw_pcie_ep_init(&priv->pci.ep);
  317. }
  318. static const struct uniphier_pcie_ep_soc_data uniphier_pro5_data = {
  319. .has_gio = true,
  320. .init = uniphier_pcie_pro5_init_ep,
  321. .wait = NULL,
  322. .features = {
  323. .linkup_notifier = false,
  324. .msi_capable = true,
  325. .msix_capable = false,
  326. .align = 1 << 16,
  327. .bar_fixed_64bit = BIT(BAR_0) | BIT(BAR_2) | BIT(BAR_4),
  328. .reserved_bar = BIT(BAR_4),
  329. },
  330. };
  331. static const struct uniphier_pcie_ep_soc_data uniphier_nx1_data = {
  332. .has_gio = false,
  333. .init = uniphier_pcie_nx1_init_ep,
  334. .wait = uniphier_pcie_nx1_wait_ep,
  335. .features = {
  336. .linkup_notifier = false,
  337. .msi_capable = true,
  338. .msix_capable = false,
  339. .align = 1 << 12,
  340. .bar_fixed_64bit = BIT(BAR_0) | BIT(BAR_2) | BIT(BAR_4),
  341. },
  342. };
  343. static const struct of_device_id uniphier_pcie_ep_match[] = {
  344. {
  345. .compatible = "socionext,uniphier-pro5-pcie-ep",
  346. .data = &uniphier_pro5_data,
  347. },
  348. {
  349. .compatible = "socionext,uniphier-nx1-pcie-ep",
  350. .data = &uniphier_nx1_data,
  351. },
  352. { /* sentinel */ },
  353. };
  354. static struct platform_driver uniphier_pcie_ep_driver = {
  355. .probe = uniphier_pcie_ep_probe,
  356. .driver = {
  357. .name = "uniphier-pcie-ep",
  358. .of_match_table = uniphier_pcie_ep_match,
  359. .suppress_bind_attrs = true,
  360. },
  361. };
  362. builtin_platform_driver(uniphier_pcie_ep_driver);