phy-qcom-ufs-qmp-20nm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 2013-2015, 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. */
  14. #include "phy-qcom-ufs-qmp-20nm.h"
  15. #define UFS_PHY_NAME "ufs_phy_qmp_20nm"
  16. static
  17. int ufs_qcom_phy_qmp_20nm_phy_calibrate(struct ufs_qcom_phy *ufs_qcom_phy,
  18. bool is_rate_B)
  19. {
  20. struct ufs_qcom_phy_calibration *tbl_A, *tbl_B;
  21. int tbl_size_A, tbl_size_B;
  22. u8 major = ufs_qcom_phy->host_ctrl_rev_major;
  23. u16 minor = ufs_qcom_phy->host_ctrl_rev_minor;
  24. u16 step = ufs_qcom_phy->host_ctrl_rev_step;
  25. int err;
  26. if ((major == 0x1) && (minor == 0x002) && (step == 0x0000)) {
  27. tbl_size_A = ARRAY_SIZE(phy_cal_table_rate_A_1_2_0);
  28. tbl_A = phy_cal_table_rate_A_1_2_0;
  29. } else if ((major == 0x1) && (minor == 0x003) && (step == 0x0000)) {
  30. tbl_size_A = ARRAY_SIZE(phy_cal_table_rate_A_1_3_0);
  31. tbl_A = phy_cal_table_rate_A_1_3_0;
  32. } else {
  33. dev_err(ufs_qcom_phy->dev, "%s: Unknown UFS-PHY version, no calibration values\n",
  34. __func__);
  35. err = -ENODEV;
  36. goto out;
  37. }
  38. tbl_size_B = ARRAY_SIZE(phy_cal_table_rate_B);
  39. tbl_B = phy_cal_table_rate_B;
  40. err = ufs_qcom_phy_calibrate(ufs_qcom_phy, tbl_A, tbl_size_A,
  41. tbl_B, tbl_size_B, is_rate_B);
  42. if (err)
  43. dev_err(ufs_qcom_phy->dev, "%s: ufs_qcom_phy_calibrate() failed %d\n",
  44. __func__, err);
  45. out:
  46. return err;
  47. }
  48. static
  49. void ufs_qcom_phy_qmp_20nm_advertise_quirks(struct ufs_qcom_phy *phy_common)
  50. {
  51. phy_common->quirks =
  52. UFS_QCOM_PHY_QUIRK_HIBERN8_EXIT_AFTER_PHY_PWR_COLLAPSE;
  53. }
  54. static int ufs_qcom_phy_qmp_20nm_init(struct phy *generic_phy)
  55. {
  56. struct ufs_qcom_phy *phy_common = get_ufs_qcom_phy(generic_phy);
  57. bool is_rate_B = false;
  58. int ret;
  59. if (phy_common->mode == PHY_MODE_UFS_HS_B)
  60. is_rate_B = true;
  61. ret = ufs_qcom_phy_qmp_20nm_phy_calibrate(phy_common, is_rate_B);
  62. if (!ret)
  63. /* phy calibrated, but yet to be started */
  64. phy_common->is_started = false;
  65. return ret;
  66. }
  67. static int ufs_qcom_phy_qmp_20nm_exit(struct phy *generic_phy)
  68. {
  69. return 0;
  70. }
  71. static
  72. int ufs_qcom_phy_qmp_20nm_set_mode(struct phy *generic_phy, enum phy_mode mode)
  73. {
  74. struct ufs_qcom_phy *phy_common = get_ufs_qcom_phy(generic_phy);
  75. phy_common->mode = PHY_MODE_INVALID;
  76. if (mode > 0)
  77. phy_common->mode = mode;
  78. return 0;
  79. }
  80. static
  81. void ufs_qcom_phy_qmp_20nm_power_control(struct ufs_qcom_phy *phy, bool val)
  82. {
  83. bool hibern8_exit_after_pwr_collapse = phy->quirks &
  84. UFS_QCOM_PHY_QUIRK_HIBERN8_EXIT_AFTER_PHY_PWR_COLLAPSE;
  85. if (val) {
  86. writel_relaxed(0x1, phy->mmio + UFS_PHY_POWER_DOWN_CONTROL);
  87. /*
  88. * Before any transactions involving PHY, ensure PHY knows
  89. * that it's analog rail is powered ON.
  90. */
  91. mb();
  92. if (hibern8_exit_after_pwr_collapse) {
  93. /*
  94. * Give atleast 1us delay after restoring PHY analog
  95. * power.
  96. */
  97. usleep_range(1, 2);
  98. writel_relaxed(0x0A, phy->mmio +
  99. QSERDES_COM_SYSCLK_EN_SEL_TXBAND);
  100. writel_relaxed(0x08, phy->mmio +
  101. QSERDES_COM_SYSCLK_EN_SEL_TXBAND);
  102. /*
  103. * Make sure workaround is deactivated before proceeding
  104. * with normal PHY operations.
  105. */
  106. mb();
  107. }
  108. } else {
  109. if (hibern8_exit_after_pwr_collapse) {
  110. writel_relaxed(0x0A, phy->mmio +
  111. QSERDES_COM_SYSCLK_EN_SEL_TXBAND);
  112. writel_relaxed(0x02, phy->mmio +
  113. QSERDES_COM_SYSCLK_EN_SEL_TXBAND);
  114. /*
  115. * Make sure that above workaround is activated before
  116. * PHY analog power collapse.
  117. */
  118. mb();
  119. }
  120. writel_relaxed(0x0, phy->mmio + UFS_PHY_POWER_DOWN_CONTROL);
  121. /*
  122. * ensure that PHY knows its PHY analog rail is going
  123. * to be powered down
  124. */
  125. mb();
  126. }
  127. }
  128. static
  129. void ufs_qcom_phy_qmp_20nm_set_tx_lane_enable(struct ufs_qcom_phy *phy, u32 val)
  130. {
  131. writel_relaxed(val & UFS_PHY_TX_LANE_ENABLE_MASK,
  132. phy->mmio + UFS_PHY_TX_LANE_ENABLE);
  133. mb();
  134. }
  135. static inline void ufs_qcom_phy_qmp_20nm_start_serdes(struct ufs_qcom_phy *phy)
  136. {
  137. u32 tmp;
  138. tmp = readl_relaxed(phy->mmio + UFS_PHY_PHY_START);
  139. tmp &= ~MASK_SERDES_START;
  140. tmp |= (1 << OFFSET_SERDES_START);
  141. writel_relaxed(tmp, phy->mmio + UFS_PHY_PHY_START);
  142. mb();
  143. }
  144. static int ufs_qcom_phy_qmp_20nm_is_pcs_ready(struct ufs_qcom_phy *phy_common)
  145. {
  146. int err = 0;
  147. u32 val;
  148. err = readl_poll_timeout(phy_common->mmio + UFS_PHY_PCS_READY_STATUS,
  149. val, (val & MASK_PCS_READY), 10, 1000000);
  150. if (err)
  151. dev_err(phy_common->dev, "%s: poll for pcs failed err = %d\n",
  152. __func__, err);
  153. return err;
  154. }
  155. static const struct phy_ops ufs_qcom_phy_qmp_20nm_phy_ops = {
  156. .init = ufs_qcom_phy_qmp_20nm_init,
  157. .exit = ufs_qcom_phy_qmp_20nm_exit,
  158. .power_on = ufs_qcom_phy_power_on,
  159. .power_off = ufs_qcom_phy_power_off,
  160. .set_mode = ufs_qcom_phy_qmp_20nm_set_mode,
  161. .owner = THIS_MODULE,
  162. };
  163. static struct ufs_qcom_phy_specific_ops phy_20nm_ops = {
  164. .start_serdes = ufs_qcom_phy_qmp_20nm_start_serdes,
  165. .is_physical_coding_sublayer_ready = ufs_qcom_phy_qmp_20nm_is_pcs_ready,
  166. .set_tx_lane_enable = ufs_qcom_phy_qmp_20nm_set_tx_lane_enable,
  167. .power_control = ufs_qcom_phy_qmp_20nm_power_control,
  168. };
  169. static int ufs_qcom_phy_qmp_20nm_probe(struct platform_device *pdev)
  170. {
  171. struct device *dev = &pdev->dev;
  172. struct phy *generic_phy;
  173. struct ufs_qcom_phy_qmp_20nm *phy;
  174. struct ufs_qcom_phy *phy_common;
  175. int err = 0;
  176. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  177. if (!phy) {
  178. err = -ENOMEM;
  179. goto out;
  180. }
  181. phy_common = &phy->common_cfg;
  182. generic_phy = ufs_qcom_phy_generic_probe(pdev, phy_common,
  183. &ufs_qcom_phy_qmp_20nm_phy_ops, &phy_20nm_ops);
  184. if (!generic_phy) {
  185. err = -EIO;
  186. goto out;
  187. }
  188. err = ufs_qcom_phy_init_clks(phy_common);
  189. if (err)
  190. goto out;
  191. err = ufs_qcom_phy_init_vregulators(phy_common);
  192. if (err)
  193. goto out;
  194. ufs_qcom_phy_qmp_20nm_advertise_quirks(phy_common);
  195. phy_set_drvdata(generic_phy, phy);
  196. strlcpy(phy_common->name, UFS_PHY_NAME, sizeof(phy_common->name));
  197. out:
  198. return err;
  199. }
  200. static const struct of_device_id ufs_qcom_phy_qmp_20nm_of_match[] = {
  201. {.compatible = "qcom,ufs-phy-qmp-20nm"},
  202. {},
  203. };
  204. MODULE_DEVICE_TABLE(of, ufs_qcom_phy_qmp_20nm_of_match);
  205. static struct platform_driver ufs_qcom_phy_qmp_20nm_driver = {
  206. .probe = ufs_qcom_phy_qmp_20nm_probe,
  207. .driver = {
  208. .of_match_table = ufs_qcom_phy_qmp_20nm_of_match,
  209. .name = "ufs_qcom_phy_qmp_20nm",
  210. },
  211. };
  212. module_platform_driver(ufs_qcom_phy_qmp_20nm_driver);
  213. MODULE_DESCRIPTION("Universal Flash Storage (UFS) QCOM PHY QMP 20nm");
  214. MODULE_LICENSE("GPL v2");