phy-uniphier-ahci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * phy-uniphier-ahci.c - PHY driver for UniPhier AHCI controller
  4. * Copyright 2016-2020, Socionext Inc.
  5. * Author: Kunihiko Hayashi <[email protected]>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/phy/phy.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/reset.h>
  17. struct uniphier_ahciphy_priv {
  18. struct device *dev;
  19. void __iomem *base;
  20. struct clk *clk, *clk_parent, *clk_parent_gio;
  21. struct reset_control *rst, *rst_parent, *rst_parent_gio;
  22. struct reset_control *rst_pm, *rst_tx, *rst_rx;
  23. const struct uniphier_ahciphy_soc_data *data;
  24. };
  25. struct uniphier_ahciphy_soc_data {
  26. int (*init)(struct uniphier_ahciphy_priv *priv);
  27. int (*power_on)(struct uniphier_ahciphy_priv *priv);
  28. int (*power_off)(struct uniphier_ahciphy_priv *priv);
  29. bool is_legacy;
  30. bool is_ready_high;
  31. bool is_phy_clk;
  32. };
  33. /* for Pro4 */
  34. #define CKCTRL0 0x0
  35. #define CKCTRL0_CK_OFF BIT(9)
  36. #define CKCTRL0_NCY_MASK GENMASK(8, 4)
  37. #define CKCTRL0_NCY5_MASK GENMASK(3, 2)
  38. #define CKCTRL0_PRESCALE_MASK GENMASK(1, 0)
  39. #define CKCTRL1 0x4
  40. #define CKCTRL1_LOS_LVL_MASK GENMASK(20, 16)
  41. #define CKCTRL1_TX_LVL_MASK GENMASK(12, 8)
  42. #define RXTXCTRL 0x8
  43. #define RXTXCTRL_RX_EQ_VALL_MASK GENMASK(31, 29)
  44. #define RXTXCTRL_RX_DPLL_MODE_MASK GENMASK(28, 26)
  45. #define RXTXCTRL_TX_ATTEN_MASK GENMASK(14, 12)
  46. #define RXTXCTRL_TX_BOOST_MASK GENMASK(11, 8)
  47. #define RXTXCTRL_TX_EDGERATE_MASK GENMASK(3, 2)
  48. #define RXTXCTRL_TX_CKO_EN BIT(0)
  49. #define RSTPWR 0x30
  50. #define RSTPWR_RX_EN_VAL BIT(18)
  51. /* for PXs2/PXs3 */
  52. #define CKCTRL 0x0
  53. #define CKCTRL_P0_READY BIT(15)
  54. #define CKCTRL_P0_RESET BIT(10)
  55. #define CKCTRL_REF_SSP_EN BIT(9)
  56. #define TXCTRL0 0x4
  57. #define TXCTRL0_AMP_G3_MASK GENMASK(22, 16)
  58. #define TXCTRL0_AMP_G2_MASK GENMASK(14, 8)
  59. #define TXCTRL0_AMP_G1_MASK GENMASK(6, 0)
  60. #define TXCTRL1 0x8
  61. #define TXCTRL1_DEEMPH_G3_MASK GENMASK(21, 16)
  62. #define TXCTRL1_DEEMPH_G2_MASK GENMASK(13, 8)
  63. #define TXCTRL1_DEEMPH_G1_MASK GENMASK(5, 0)
  64. #define RXCTRL 0xc
  65. #define RXCTRL_LOS_LVL_MASK GENMASK(20, 16)
  66. #define RXCTRL_LOS_BIAS_MASK GENMASK(10, 8)
  67. #define RXCTRL_RX_EQ_MASK GENMASK(2, 0)
  68. static int uniphier_ahciphy_pro4_init(struct uniphier_ahciphy_priv *priv)
  69. {
  70. u32 val;
  71. /* set phy MPLL parameters */
  72. val = readl(priv->base + CKCTRL0);
  73. val &= ~CKCTRL0_NCY_MASK;
  74. val |= FIELD_PREP(CKCTRL0_NCY_MASK, 0x6);
  75. val &= ~CKCTRL0_NCY5_MASK;
  76. val |= FIELD_PREP(CKCTRL0_NCY5_MASK, 0x2);
  77. val &= ~CKCTRL0_PRESCALE_MASK;
  78. val |= FIELD_PREP(CKCTRL0_PRESCALE_MASK, 0x1);
  79. writel(val, priv->base + CKCTRL0);
  80. /* setup phy control parameters */
  81. val = readl(priv->base + CKCTRL1);
  82. val &= ~CKCTRL1_LOS_LVL_MASK;
  83. val |= FIELD_PREP(CKCTRL1_LOS_LVL_MASK, 0x10);
  84. val &= ~CKCTRL1_TX_LVL_MASK;
  85. val |= FIELD_PREP(CKCTRL1_TX_LVL_MASK, 0x06);
  86. writel(val, priv->base + CKCTRL1);
  87. val = readl(priv->base + RXTXCTRL);
  88. val &= ~RXTXCTRL_RX_EQ_VALL_MASK;
  89. val |= FIELD_PREP(RXTXCTRL_RX_EQ_VALL_MASK, 0x6);
  90. val &= ~RXTXCTRL_RX_DPLL_MODE_MASK;
  91. val |= FIELD_PREP(RXTXCTRL_RX_DPLL_MODE_MASK, 0x3);
  92. val &= ~RXTXCTRL_TX_ATTEN_MASK;
  93. val |= FIELD_PREP(RXTXCTRL_TX_ATTEN_MASK, 0x3);
  94. val &= ~RXTXCTRL_TX_BOOST_MASK;
  95. val |= FIELD_PREP(RXTXCTRL_TX_BOOST_MASK, 0x5);
  96. val &= ~RXTXCTRL_TX_EDGERATE_MASK;
  97. val |= FIELD_PREP(RXTXCTRL_TX_EDGERATE_MASK, 0x0);
  98. writel(val, priv->base + RXTXCTRL);
  99. return 0;
  100. }
  101. static int uniphier_ahciphy_pro4_power_on(struct uniphier_ahciphy_priv *priv)
  102. {
  103. u32 val;
  104. int ret;
  105. /* enable reference clock for phy */
  106. val = readl(priv->base + CKCTRL0);
  107. val &= ~CKCTRL0_CK_OFF;
  108. writel(val, priv->base + CKCTRL0);
  109. /* enable TX clock */
  110. val = readl(priv->base + RXTXCTRL);
  111. val |= RXTXCTRL_TX_CKO_EN;
  112. writel(val, priv->base + RXTXCTRL);
  113. /* wait until RX is ready */
  114. ret = readl_poll_timeout(priv->base + RSTPWR, val,
  115. !(val & RSTPWR_RX_EN_VAL), 200, 2000);
  116. if (ret) {
  117. dev_err(priv->dev, "Failed to check whether Rx is ready\n");
  118. goto out_disable_clock;
  119. }
  120. /* release all reset */
  121. ret = reset_control_deassert(priv->rst_pm);
  122. if (ret) {
  123. dev_err(priv->dev, "Failed to release PM reset\n");
  124. goto out_disable_clock;
  125. }
  126. ret = reset_control_deassert(priv->rst_tx);
  127. if (ret) {
  128. dev_err(priv->dev, "Failed to release Tx reset\n");
  129. goto out_reset_pm_assert;
  130. }
  131. ret = reset_control_deassert(priv->rst_rx);
  132. if (ret) {
  133. dev_err(priv->dev, "Failed to release Rx reset\n");
  134. goto out_reset_tx_assert;
  135. }
  136. return 0;
  137. out_reset_tx_assert:
  138. reset_control_assert(priv->rst_tx);
  139. out_reset_pm_assert:
  140. reset_control_assert(priv->rst_pm);
  141. out_disable_clock:
  142. /* disable TX clock */
  143. val = readl(priv->base + RXTXCTRL);
  144. val &= ~RXTXCTRL_TX_CKO_EN;
  145. writel(val, priv->base + RXTXCTRL);
  146. /* disable reference clock for phy */
  147. val = readl(priv->base + CKCTRL0);
  148. val |= CKCTRL0_CK_OFF;
  149. writel(val, priv->base + CKCTRL0);
  150. return ret;
  151. }
  152. static int uniphier_ahciphy_pro4_power_off(struct uniphier_ahciphy_priv *priv)
  153. {
  154. u32 val;
  155. reset_control_assert(priv->rst_rx);
  156. reset_control_assert(priv->rst_tx);
  157. reset_control_assert(priv->rst_pm);
  158. /* disable TX clock */
  159. val = readl(priv->base + RXTXCTRL);
  160. val &= ~RXTXCTRL_TX_CKO_EN;
  161. writel(val, priv->base + RXTXCTRL);
  162. /* disable reference clock for phy */
  163. val = readl(priv->base + CKCTRL0);
  164. val |= CKCTRL0_CK_OFF;
  165. writel(val, priv->base + CKCTRL0);
  166. return 0;
  167. }
  168. static void uniphier_ahciphy_pxs2_enable(struct uniphier_ahciphy_priv *priv,
  169. bool enable)
  170. {
  171. u32 val;
  172. val = readl(priv->base + CKCTRL);
  173. if (enable) {
  174. val |= CKCTRL_REF_SSP_EN;
  175. writel(val, priv->base + CKCTRL);
  176. val &= ~CKCTRL_P0_RESET;
  177. writel(val, priv->base + CKCTRL);
  178. } else {
  179. val |= CKCTRL_P0_RESET;
  180. writel(val, priv->base + CKCTRL);
  181. val &= ~CKCTRL_REF_SSP_EN;
  182. writel(val, priv->base + CKCTRL);
  183. }
  184. }
  185. static int uniphier_ahciphy_pxs2_power_on(struct uniphier_ahciphy_priv *priv)
  186. {
  187. int ret;
  188. u32 val;
  189. uniphier_ahciphy_pxs2_enable(priv, true);
  190. /* wait until PLL is ready */
  191. if (priv->data->is_ready_high)
  192. ret = readl_poll_timeout(priv->base + CKCTRL, val,
  193. (val & CKCTRL_P0_READY), 200, 400);
  194. else
  195. ret = readl_poll_timeout(priv->base + CKCTRL, val,
  196. !(val & CKCTRL_P0_READY), 200, 400);
  197. if (ret) {
  198. dev_err(priv->dev, "Failed to check whether PHY PLL is ready\n");
  199. uniphier_ahciphy_pxs2_enable(priv, false);
  200. }
  201. return ret;
  202. }
  203. static int uniphier_ahciphy_pxs2_power_off(struct uniphier_ahciphy_priv *priv)
  204. {
  205. uniphier_ahciphy_pxs2_enable(priv, false);
  206. return 0;
  207. }
  208. static int uniphier_ahciphy_pxs3_init(struct uniphier_ahciphy_priv *priv)
  209. {
  210. int i;
  211. u32 val;
  212. /* setup port parameter */
  213. val = readl(priv->base + TXCTRL0);
  214. val &= ~TXCTRL0_AMP_G3_MASK;
  215. val |= FIELD_PREP(TXCTRL0_AMP_G3_MASK, 0x73);
  216. val &= ~TXCTRL0_AMP_G2_MASK;
  217. val |= FIELD_PREP(TXCTRL0_AMP_G2_MASK, 0x46);
  218. val &= ~TXCTRL0_AMP_G1_MASK;
  219. val |= FIELD_PREP(TXCTRL0_AMP_G1_MASK, 0x42);
  220. writel(val, priv->base + TXCTRL0);
  221. val = readl(priv->base + TXCTRL1);
  222. val &= ~TXCTRL1_DEEMPH_G3_MASK;
  223. val |= FIELD_PREP(TXCTRL1_DEEMPH_G3_MASK, 0x23);
  224. val &= ~TXCTRL1_DEEMPH_G2_MASK;
  225. val |= FIELD_PREP(TXCTRL1_DEEMPH_G2_MASK, 0x05);
  226. val &= ~TXCTRL1_DEEMPH_G1_MASK;
  227. val |= FIELD_PREP(TXCTRL1_DEEMPH_G1_MASK, 0x05);
  228. val = readl(priv->base + RXCTRL);
  229. val &= ~RXCTRL_LOS_LVL_MASK;
  230. val |= FIELD_PREP(RXCTRL_LOS_LVL_MASK, 0x9);
  231. val &= ~RXCTRL_LOS_BIAS_MASK;
  232. val |= FIELD_PREP(RXCTRL_LOS_BIAS_MASK, 0x2);
  233. val &= ~RXCTRL_RX_EQ_MASK;
  234. val |= FIELD_PREP(RXCTRL_RX_EQ_MASK, 0x1);
  235. /* dummy read 25 times to make a wait time for the phy to stabilize */
  236. for (i = 0; i < 25; i++)
  237. readl(priv->base + CKCTRL);
  238. return 0;
  239. }
  240. static int uniphier_ahciphy_init(struct phy *phy)
  241. {
  242. struct uniphier_ahciphy_priv *priv = phy_get_drvdata(phy);
  243. int ret;
  244. ret = clk_prepare_enable(priv->clk_parent_gio);
  245. if (ret)
  246. return ret;
  247. ret = clk_prepare_enable(priv->clk_parent);
  248. if (ret)
  249. goto out_clk_gio_disable;
  250. ret = reset_control_deassert(priv->rst_parent_gio);
  251. if (ret)
  252. goto out_clk_disable;
  253. ret = reset_control_deassert(priv->rst_parent);
  254. if (ret)
  255. goto out_rst_gio_assert;
  256. if (priv->data->init) {
  257. ret = priv->data->init(priv);
  258. if (ret)
  259. goto out_rst_assert;
  260. }
  261. return 0;
  262. out_rst_assert:
  263. reset_control_assert(priv->rst_parent);
  264. out_rst_gio_assert:
  265. reset_control_assert(priv->rst_parent_gio);
  266. out_clk_disable:
  267. clk_disable_unprepare(priv->clk_parent);
  268. out_clk_gio_disable:
  269. clk_disable_unprepare(priv->clk_parent_gio);
  270. return ret;
  271. }
  272. static int uniphier_ahciphy_exit(struct phy *phy)
  273. {
  274. struct uniphier_ahciphy_priv *priv = phy_get_drvdata(phy);
  275. reset_control_assert(priv->rst_parent);
  276. reset_control_assert(priv->rst_parent_gio);
  277. clk_disable_unprepare(priv->clk_parent);
  278. clk_disable_unprepare(priv->clk_parent_gio);
  279. return 0;
  280. }
  281. static int uniphier_ahciphy_power_on(struct phy *phy)
  282. {
  283. struct uniphier_ahciphy_priv *priv = phy_get_drvdata(phy);
  284. int ret = 0;
  285. ret = clk_prepare_enable(priv->clk);
  286. if (ret)
  287. return ret;
  288. ret = reset_control_deassert(priv->rst);
  289. if (ret)
  290. goto out_clk_disable;
  291. if (priv->data->power_on) {
  292. ret = priv->data->power_on(priv);
  293. if (ret)
  294. goto out_reset_assert;
  295. }
  296. return 0;
  297. out_reset_assert:
  298. reset_control_assert(priv->rst);
  299. out_clk_disable:
  300. clk_disable_unprepare(priv->clk);
  301. return ret;
  302. }
  303. static int uniphier_ahciphy_power_off(struct phy *phy)
  304. {
  305. struct uniphier_ahciphy_priv *priv = phy_get_drvdata(phy);
  306. int ret = 0;
  307. if (priv->data->power_off)
  308. ret = priv->data->power_off(priv);
  309. reset_control_assert(priv->rst);
  310. clk_disable_unprepare(priv->clk);
  311. return ret;
  312. }
  313. static const struct phy_ops uniphier_ahciphy_ops = {
  314. .init = uniphier_ahciphy_init,
  315. .exit = uniphier_ahciphy_exit,
  316. .power_on = uniphier_ahciphy_power_on,
  317. .power_off = uniphier_ahciphy_power_off,
  318. .owner = THIS_MODULE,
  319. };
  320. static int uniphier_ahciphy_probe(struct platform_device *pdev)
  321. {
  322. struct device *dev = &pdev->dev;
  323. struct uniphier_ahciphy_priv *priv;
  324. struct phy *phy;
  325. struct phy_provider *phy_provider;
  326. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  327. if (!priv)
  328. return -ENOMEM;
  329. priv->dev = dev;
  330. priv->data = of_device_get_match_data(dev);
  331. if (WARN_ON(!priv->data))
  332. return -EINVAL;
  333. priv->base = devm_platform_ioremap_resource(pdev, 0);
  334. if (IS_ERR(priv->base))
  335. return PTR_ERR(priv->base);
  336. priv->clk_parent = devm_clk_get(dev, "link");
  337. if (IS_ERR(priv->clk_parent))
  338. return PTR_ERR(priv->clk_parent);
  339. if (priv->data->is_phy_clk) {
  340. priv->clk = devm_clk_get(dev, "phy");
  341. if (IS_ERR(priv->clk))
  342. return PTR_ERR(priv->clk);
  343. }
  344. priv->rst_parent = devm_reset_control_get_shared(dev, "link");
  345. if (IS_ERR(priv->rst_parent))
  346. return PTR_ERR(priv->rst_parent);
  347. priv->rst = devm_reset_control_get_shared(dev, "phy");
  348. if (IS_ERR(priv->rst))
  349. return PTR_ERR(priv->rst);
  350. if (priv->data->is_legacy) {
  351. priv->clk_parent_gio = devm_clk_get(dev, "gio");
  352. if (IS_ERR(priv->clk_parent_gio))
  353. return PTR_ERR(priv->clk_parent_gio);
  354. priv->rst_parent_gio =
  355. devm_reset_control_get_shared(dev, "gio");
  356. if (IS_ERR(priv->rst_parent_gio))
  357. return PTR_ERR(priv->rst_parent_gio);
  358. priv->rst_pm = devm_reset_control_get_shared(dev, "pm");
  359. if (IS_ERR(priv->rst_pm))
  360. return PTR_ERR(priv->rst_pm);
  361. priv->rst_tx = devm_reset_control_get_shared(dev, "tx");
  362. if (IS_ERR(priv->rst_tx))
  363. return PTR_ERR(priv->rst_tx);
  364. priv->rst_rx = devm_reset_control_get_shared(dev, "rx");
  365. if (IS_ERR(priv->rst_rx))
  366. return PTR_ERR(priv->rst_rx);
  367. }
  368. phy = devm_phy_create(dev, dev->of_node, &uniphier_ahciphy_ops);
  369. if (IS_ERR(phy)) {
  370. dev_err(dev, "failed to create phy\n");
  371. return PTR_ERR(phy);
  372. }
  373. phy_set_drvdata(phy, priv);
  374. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  375. if (IS_ERR(phy_provider))
  376. return PTR_ERR(phy_provider);
  377. return 0;
  378. }
  379. static const struct uniphier_ahciphy_soc_data uniphier_pro4_data = {
  380. .init = uniphier_ahciphy_pro4_init,
  381. .power_on = uniphier_ahciphy_pro4_power_on,
  382. .power_off = uniphier_ahciphy_pro4_power_off,
  383. .is_legacy = true,
  384. .is_phy_clk = false,
  385. };
  386. static const struct uniphier_ahciphy_soc_data uniphier_pxs2_data = {
  387. .power_on = uniphier_ahciphy_pxs2_power_on,
  388. .power_off = uniphier_ahciphy_pxs2_power_off,
  389. .is_legacy = false,
  390. .is_ready_high = false,
  391. .is_phy_clk = false,
  392. };
  393. static const struct uniphier_ahciphy_soc_data uniphier_pxs3_data = {
  394. .init = uniphier_ahciphy_pxs3_init,
  395. .power_on = uniphier_ahciphy_pxs2_power_on,
  396. .power_off = uniphier_ahciphy_pxs2_power_off,
  397. .is_legacy = false,
  398. .is_ready_high = true,
  399. .is_phy_clk = true,
  400. };
  401. static const struct of_device_id uniphier_ahciphy_match[] = {
  402. {
  403. .compatible = "socionext,uniphier-pro4-ahci-phy",
  404. .data = &uniphier_pro4_data,
  405. },
  406. {
  407. .compatible = "socionext,uniphier-pxs2-ahci-phy",
  408. .data = &uniphier_pxs2_data,
  409. },
  410. {
  411. .compatible = "socionext,uniphier-pxs3-ahci-phy",
  412. .data = &uniphier_pxs3_data,
  413. },
  414. { /* Sentinel */ },
  415. };
  416. MODULE_DEVICE_TABLE(of, uniphier_ahciphy_match);
  417. static struct platform_driver uniphier_ahciphy_driver = {
  418. .probe = uniphier_ahciphy_probe,
  419. .driver = {
  420. .name = "uniphier-ahci-phy",
  421. .of_match_table = uniphier_ahciphy_match,
  422. },
  423. };
  424. module_platform_driver(uniphier_ahciphy_driver);
  425. MODULE_AUTHOR("Kunihiko Hayashi <[email protected]>");
  426. MODULE_DESCRIPTION("UniPhier PHY driver for AHCI controller");
  427. MODULE_LICENSE("GPL v2");