phy-qcom-apq8064-sata.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/time.h>
  18. #include <linux/delay.h>
  19. #include <linux/clk.h>
  20. #include <linux/slab.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/phy/phy.h>
  23. /* PHY registers */
  24. #define UNIPHY_PLL_REFCLK_CFG 0x000
  25. #define UNIPHY_PLL_PWRGEN_CFG 0x014
  26. #define UNIPHY_PLL_GLB_CFG 0x020
  27. #define UNIPHY_PLL_SDM_CFG0 0x038
  28. #define UNIPHY_PLL_SDM_CFG1 0x03C
  29. #define UNIPHY_PLL_SDM_CFG2 0x040
  30. #define UNIPHY_PLL_SDM_CFG3 0x044
  31. #define UNIPHY_PLL_SDM_CFG4 0x048
  32. #define UNIPHY_PLL_SSC_CFG0 0x04C
  33. #define UNIPHY_PLL_SSC_CFG1 0x050
  34. #define UNIPHY_PLL_SSC_CFG2 0x054
  35. #define UNIPHY_PLL_SSC_CFG3 0x058
  36. #define UNIPHY_PLL_LKDET_CFG0 0x05C
  37. #define UNIPHY_PLL_LKDET_CFG1 0x060
  38. #define UNIPHY_PLL_LKDET_CFG2 0x064
  39. #define UNIPHY_PLL_CAL_CFG0 0x06C
  40. #define UNIPHY_PLL_CAL_CFG8 0x08C
  41. #define UNIPHY_PLL_CAL_CFG9 0x090
  42. #define UNIPHY_PLL_CAL_CFG10 0x094
  43. #define UNIPHY_PLL_CAL_CFG11 0x098
  44. #define UNIPHY_PLL_STATUS 0x0C0
  45. #define SATA_PHY_SER_CTRL 0x100
  46. #define SATA_PHY_TX_DRIV_CTRL0 0x104
  47. #define SATA_PHY_TX_DRIV_CTRL1 0x108
  48. #define SATA_PHY_TX_IMCAL0 0x11C
  49. #define SATA_PHY_TX_IMCAL2 0x124
  50. #define SATA_PHY_RX_IMCAL0 0x128
  51. #define SATA_PHY_EQUAL 0x13C
  52. #define SATA_PHY_OOB_TERM 0x144
  53. #define SATA_PHY_CDR_CTRL0 0x148
  54. #define SATA_PHY_CDR_CTRL1 0x14C
  55. #define SATA_PHY_CDR_CTRL2 0x150
  56. #define SATA_PHY_CDR_CTRL3 0x154
  57. #define SATA_PHY_PI_CTRL0 0x168
  58. #define SATA_PHY_POW_DWN_CTRL0 0x180
  59. #define SATA_PHY_POW_DWN_CTRL1 0x184
  60. #define SATA_PHY_TX_DATA_CTRL 0x188
  61. #define SATA_PHY_ALIGNP 0x1A4
  62. #define SATA_PHY_TX_IMCAL_STAT 0x1E4
  63. #define SATA_PHY_RX_IMCAL_STAT 0x1E8
  64. #define UNIPHY_PLL_LOCK BIT(0)
  65. #define SATA_PHY_TX_CAL BIT(0)
  66. #define SATA_PHY_RX_CAL BIT(0)
  67. /* default timeout set to 1 sec */
  68. #define TIMEOUT_MS 10000
  69. #define DELAY_INTERVAL_US 100
  70. struct qcom_apq8064_sata_phy {
  71. void __iomem *mmio;
  72. struct clk *cfg_clk;
  73. struct device *dev;
  74. };
  75. /* Helper function to do poll and timeout */
  76. static int read_poll_timeout(void __iomem *addr, u32 mask)
  77. {
  78. unsigned long timeout = jiffies + msecs_to_jiffies(TIMEOUT_MS);
  79. do {
  80. if (readl_relaxed(addr) & mask)
  81. return 0;
  82. usleep_range(DELAY_INTERVAL_US, DELAY_INTERVAL_US + 50);
  83. } while (!time_after(jiffies, timeout));
  84. return (readl_relaxed(addr) & mask) ? 0 : -ETIMEDOUT;
  85. }
  86. static int qcom_apq8064_sata_phy_init(struct phy *generic_phy)
  87. {
  88. struct qcom_apq8064_sata_phy *phy = phy_get_drvdata(generic_phy);
  89. void __iomem *base = phy->mmio;
  90. int ret = 0;
  91. /* SATA phy initialization */
  92. writel_relaxed(0x01, base + SATA_PHY_SER_CTRL);
  93. writel_relaxed(0xB1, base + SATA_PHY_POW_DWN_CTRL0);
  94. /* Make sure the power down happens before power up */
  95. mb();
  96. usleep_range(10, 60);
  97. writel_relaxed(0x01, base + SATA_PHY_POW_DWN_CTRL0);
  98. writel_relaxed(0x3E, base + SATA_PHY_POW_DWN_CTRL1);
  99. writel_relaxed(0x01, base + SATA_PHY_RX_IMCAL0);
  100. writel_relaxed(0x01, base + SATA_PHY_TX_IMCAL0);
  101. writel_relaxed(0x02, base + SATA_PHY_TX_IMCAL2);
  102. /* Write UNIPHYPLL registers to configure PLL */
  103. writel_relaxed(0x04, base + UNIPHY_PLL_REFCLK_CFG);
  104. writel_relaxed(0x00, base + UNIPHY_PLL_PWRGEN_CFG);
  105. writel_relaxed(0x0A, base + UNIPHY_PLL_CAL_CFG0);
  106. writel_relaxed(0xF3, base + UNIPHY_PLL_CAL_CFG8);
  107. writel_relaxed(0x01, base + UNIPHY_PLL_CAL_CFG9);
  108. writel_relaxed(0xED, base + UNIPHY_PLL_CAL_CFG10);
  109. writel_relaxed(0x02, base + UNIPHY_PLL_CAL_CFG11);
  110. writel_relaxed(0x36, base + UNIPHY_PLL_SDM_CFG0);
  111. writel_relaxed(0x0D, base + UNIPHY_PLL_SDM_CFG1);
  112. writel_relaxed(0xA3, base + UNIPHY_PLL_SDM_CFG2);
  113. writel_relaxed(0xF0, base + UNIPHY_PLL_SDM_CFG3);
  114. writel_relaxed(0x00, base + UNIPHY_PLL_SDM_CFG4);
  115. writel_relaxed(0x19, base + UNIPHY_PLL_SSC_CFG0);
  116. writel_relaxed(0xE1, base + UNIPHY_PLL_SSC_CFG1);
  117. writel_relaxed(0x00, base + UNIPHY_PLL_SSC_CFG2);
  118. writel_relaxed(0x11, base + UNIPHY_PLL_SSC_CFG3);
  119. writel_relaxed(0x04, base + UNIPHY_PLL_LKDET_CFG0);
  120. writel_relaxed(0xFF, base + UNIPHY_PLL_LKDET_CFG1);
  121. writel_relaxed(0x02, base + UNIPHY_PLL_GLB_CFG);
  122. /* make sure global config LDO power down happens before power up */
  123. mb();
  124. writel_relaxed(0x03, base + UNIPHY_PLL_GLB_CFG);
  125. writel_relaxed(0x05, base + UNIPHY_PLL_LKDET_CFG2);
  126. /* PLL Lock wait */
  127. ret = read_poll_timeout(base + UNIPHY_PLL_STATUS, UNIPHY_PLL_LOCK);
  128. if (ret) {
  129. dev_err(phy->dev, "poll timeout UNIPHY_PLL_STATUS\n");
  130. return ret;
  131. }
  132. /* TX Calibration */
  133. ret = read_poll_timeout(base + SATA_PHY_TX_IMCAL_STAT, SATA_PHY_TX_CAL);
  134. if (ret) {
  135. dev_err(phy->dev, "poll timeout SATA_PHY_TX_IMCAL_STAT\n");
  136. return ret;
  137. }
  138. /* RX Calibration */
  139. ret = read_poll_timeout(base + SATA_PHY_RX_IMCAL_STAT, SATA_PHY_RX_CAL);
  140. if (ret) {
  141. dev_err(phy->dev, "poll timeout SATA_PHY_RX_IMCAL_STAT\n");
  142. return ret;
  143. }
  144. /* SATA phy calibrated succesfully, power up to functional mode */
  145. writel_relaxed(0x3E, base + SATA_PHY_POW_DWN_CTRL1);
  146. writel_relaxed(0x01, base + SATA_PHY_RX_IMCAL0);
  147. writel_relaxed(0x01, base + SATA_PHY_TX_IMCAL0);
  148. writel_relaxed(0x00, base + SATA_PHY_POW_DWN_CTRL1);
  149. writel_relaxed(0x59, base + SATA_PHY_CDR_CTRL0);
  150. writel_relaxed(0x04, base + SATA_PHY_CDR_CTRL1);
  151. writel_relaxed(0x00, base + SATA_PHY_CDR_CTRL2);
  152. writel_relaxed(0x00, base + SATA_PHY_PI_CTRL0);
  153. writel_relaxed(0x00, base + SATA_PHY_CDR_CTRL3);
  154. writel_relaxed(0x01, base + SATA_PHY_POW_DWN_CTRL0);
  155. writel_relaxed(0x11, base + SATA_PHY_TX_DATA_CTRL);
  156. writel_relaxed(0x43, base + SATA_PHY_ALIGNP);
  157. writel_relaxed(0x04, base + SATA_PHY_OOB_TERM);
  158. writel_relaxed(0x01, base + SATA_PHY_EQUAL);
  159. writel_relaxed(0x09, base + SATA_PHY_TX_DRIV_CTRL0);
  160. writel_relaxed(0x09, base + SATA_PHY_TX_DRIV_CTRL1);
  161. return 0;
  162. }
  163. static int qcom_apq8064_sata_phy_exit(struct phy *generic_phy)
  164. {
  165. struct qcom_apq8064_sata_phy *phy = phy_get_drvdata(generic_phy);
  166. void __iomem *base = phy->mmio;
  167. /* Power down PHY */
  168. writel_relaxed(0xF8, base + SATA_PHY_POW_DWN_CTRL0);
  169. writel_relaxed(0xFE, base + SATA_PHY_POW_DWN_CTRL1);
  170. /* Power down PLL block */
  171. writel_relaxed(0x00, base + UNIPHY_PLL_GLB_CFG);
  172. return 0;
  173. }
  174. static const struct phy_ops qcom_apq8064_sata_phy_ops = {
  175. .init = qcom_apq8064_sata_phy_init,
  176. .exit = qcom_apq8064_sata_phy_exit,
  177. .owner = THIS_MODULE,
  178. };
  179. static int qcom_apq8064_sata_phy_probe(struct platform_device *pdev)
  180. {
  181. struct qcom_apq8064_sata_phy *phy;
  182. struct device *dev = &pdev->dev;
  183. struct resource *res;
  184. struct phy_provider *phy_provider;
  185. struct phy *generic_phy;
  186. int ret;
  187. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  188. if (!phy)
  189. return -ENOMEM;
  190. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  191. phy->mmio = devm_ioremap_resource(dev, res);
  192. if (IS_ERR(phy->mmio))
  193. return PTR_ERR(phy->mmio);
  194. generic_phy = devm_phy_create(dev, NULL, &qcom_apq8064_sata_phy_ops);
  195. if (IS_ERR(generic_phy)) {
  196. dev_err(dev, "%s: failed to create phy\n", __func__);
  197. return PTR_ERR(generic_phy);
  198. }
  199. phy->dev = dev;
  200. phy_set_drvdata(generic_phy, phy);
  201. platform_set_drvdata(pdev, phy);
  202. phy->cfg_clk = devm_clk_get(dev, "cfg");
  203. if (IS_ERR(phy->cfg_clk)) {
  204. dev_err(dev, "Failed to get sata cfg clock\n");
  205. return PTR_ERR(phy->cfg_clk);
  206. }
  207. ret = clk_prepare_enable(phy->cfg_clk);
  208. if (ret)
  209. return ret;
  210. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  211. if (IS_ERR(phy_provider)) {
  212. clk_disable_unprepare(phy->cfg_clk);
  213. dev_err(dev, "%s: failed to register phy\n", __func__);
  214. return PTR_ERR(phy_provider);
  215. }
  216. return 0;
  217. }
  218. static int qcom_apq8064_sata_phy_remove(struct platform_device *pdev)
  219. {
  220. struct qcom_apq8064_sata_phy *phy = platform_get_drvdata(pdev);
  221. clk_disable_unprepare(phy->cfg_clk);
  222. return 0;
  223. }
  224. static const struct of_device_id qcom_apq8064_sata_phy_of_match[] = {
  225. { .compatible = "qcom,apq8064-sata-phy" },
  226. { },
  227. };
  228. MODULE_DEVICE_TABLE(of, qcom_apq8064_sata_phy_of_match);
  229. static struct platform_driver qcom_apq8064_sata_phy_driver = {
  230. .probe = qcom_apq8064_sata_phy_probe,
  231. .remove = qcom_apq8064_sata_phy_remove,
  232. .driver = {
  233. .name = "qcom-apq8064-sata-phy",
  234. .of_match_table = qcom_apq8064_sata_phy_of_match,
  235. }
  236. };
  237. module_platform_driver(qcom_apq8064_sata_phy_driver);
  238. MODULE_DESCRIPTION("QCOM apq8064 SATA PHY driver");
  239. MODULE_LICENSE("GPL v2");