phy-qcom-ipq806x-sata.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/of_address.h>
  18. #include <linux/time.h>
  19. #include <linux/delay.h>
  20. #include <linux/clk.h>
  21. #include <linux/slab.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/phy/phy.h>
  24. struct qcom_ipq806x_sata_phy {
  25. void __iomem *mmio;
  26. struct clk *cfg_clk;
  27. struct device *dev;
  28. };
  29. #define __set(v, a, b) (((v) << (b)) & GENMASK(a, b))
  30. #define SATA_PHY_P0_PARAM0 0x200
  31. #define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3(x) __set(x, 17, 12)
  32. #define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3_MASK GENMASK(17, 12)
  33. #define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN2(x) __set(x, 11, 6)
  34. #define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN2_MASK GENMASK(11, 6)
  35. #define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN1(x) __set(x, 5, 0)
  36. #define SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN1_MASK GENMASK(5, 0)
  37. #define SATA_PHY_P0_PARAM1 0x204
  38. #define SATA_PHY_P0_PARAM1_RESERVED_BITS31_21(x) __set(x, 31, 21)
  39. #define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3(x) __set(x, 20, 14)
  40. #define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3_MASK GENMASK(20, 14)
  41. #define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2(x) __set(x, 13, 7)
  42. #define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2_MASK GENMASK(13, 7)
  43. #define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1(x) __set(x, 6, 0)
  44. #define SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1_MASK GENMASK(6, 0)
  45. #define SATA_PHY_P0_PARAM2 0x208
  46. #define SATA_PHY_P0_PARAM2_RX_EQ(x) __set(x, 20, 18)
  47. #define SATA_PHY_P0_PARAM2_RX_EQ_MASK GENMASK(20, 18)
  48. #define SATA_PHY_P0_PARAM3 0x20C
  49. #define SATA_PHY_SSC_EN 0x8
  50. #define SATA_PHY_P0_PARAM4 0x210
  51. #define SATA_PHY_REF_SSP_EN 0x2
  52. #define SATA_PHY_RESET 0x1
  53. static int qcom_ipq806x_sata_phy_init(struct phy *generic_phy)
  54. {
  55. struct qcom_ipq806x_sata_phy *phy = phy_get_drvdata(generic_phy);
  56. u32 reg;
  57. /* Setting SSC_EN to 1 */
  58. reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM3);
  59. reg = reg | SATA_PHY_SSC_EN;
  60. writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM3);
  61. reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM0) &
  62. ~(SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3_MASK |
  63. SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN2_MASK |
  64. SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN1_MASK);
  65. reg |= SATA_PHY_P0_PARAM0_P0_TX_PREEMPH_GEN3(0xf);
  66. writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM0);
  67. reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM1) &
  68. ~(SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3_MASK |
  69. SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2_MASK |
  70. SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1_MASK);
  71. reg |= SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN3(0x55) |
  72. SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN2(0x55) |
  73. SATA_PHY_P0_PARAM1_P0_TX_AMPLITUDE_GEN1(0x55);
  74. writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM1);
  75. reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM2) &
  76. ~SATA_PHY_P0_PARAM2_RX_EQ_MASK;
  77. reg |= SATA_PHY_P0_PARAM2_RX_EQ(0x3);
  78. writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM2);
  79. /* Setting PHY_RESET to 1 */
  80. reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
  81. reg = reg | SATA_PHY_RESET;
  82. writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
  83. /* Setting REF_SSP_EN to 1 */
  84. reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
  85. reg = reg | SATA_PHY_REF_SSP_EN | SATA_PHY_RESET;
  86. writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
  87. /* make sure all changes complete before we let the PHY out of reset */
  88. mb();
  89. /* sleep for max. 50us more to combine processor wakeups */
  90. usleep_range(20, 20 + 50);
  91. /* Clearing PHY_RESET to 0 */
  92. reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
  93. reg = reg & ~SATA_PHY_RESET;
  94. writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
  95. return 0;
  96. }
  97. static int qcom_ipq806x_sata_phy_exit(struct phy *generic_phy)
  98. {
  99. struct qcom_ipq806x_sata_phy *phy = phy_get_drvdata(generic_phy);
  100. u32 reg;
  101. /* Setting PHY_RESET to 1 */
  102. reg = readl_relaxed(phy->mmio + SATA_PHY_P0_PARAM4);
  103. reg = reg | SATA_PHY_RESET;
  104. writel_relaxed(reg, phy->mmio + SATA_PHY_P0_PARAM4);
  105. return 0;
  106. }
  107. static const struct phy_ops qcom_ipq806x_sata_phy_ops = {
  108. .init = qcom_ipq806x_sata_phy_init,
  109. .exit = qcom_ipq806x_sata_phy_exit,
  110. .owner = THIS_MODULE,
  111. };
  112. static int qcom_ipq806x_sata_phy_probe(struct platform_device *pdev)
  113. {
  114. struct qcom_ipq806x_sata_phy *phy;
  115. struct device *dev = &pdev->dev;
  116. struct resource *res;
  117. struct phy_provider *phy_provider;
  118. struct phy *generic_phy;
  119. int ret;
  120. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  121. if (!phy)
  122. return -ENOMEM;
  123. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  124. phy->mmio = devm_ioremap_resource(dev, res);
  125. if (IS_ERR(phy->mmio))
  126. return PTR_ERR(phy->mmio);
  127. generic_phy = devm_phy_create(dev, NULL, &qcom_ipq806x_sata_phy_ops);
  128. if (IS_ERR(generic_phy)) {
  129. dev_err(dev, "%s: failed to create phy\n", __func__);
  130. return PTR_ERR(generic_phy);
  131. }
  132. phy->dev = dev;
  133. phy_set_drvdata(generic_phy, phy);
  134. platform_set_drvdata(pdev, phy);
  135. phy->cfg_clk = devm_clk_get(dev, "cfg");
  136. if (IS_ERR(phy->cfg_clk)) {
  137. dev_err(dev, "Failed to get sata cfg clock\n");
  138. return PTR_ERR(phy->cfg_clk);
  139. }
  140. ret = clk_prepare_enable(phy->cfg_clk);
  141. if (ret)
  142. return ret;
  143. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  144. if (IS_ERR(phy_provider)) {
  145. clk_disable_unprepare(phy->cfg_clk);
  146. dev_err(dev, "%s: failed to register phy\n", __func__);
  147. return PTR_ERR(phy_provider);
  148. }
  149. return 0;
  150. }
  151. static int qcom_ipq806x_sata_phy_remove(struct platform_device *pdev)
  152. {
  153. struct qcom_ipq806x_sata_phy *phy = platform_get_drvdata(pdev);
  154. clk_disable_unprepare(phy->cfg_clk);
  155. return 0;
  156. }
  157. static const struct of_device_id qcom_ipq806x_sata_phy_of_match[] = {
  158. { .compatible = "qcom,ipq806x-sata-phy" },
  159. { },
  160. };
  161. MODULE_DEVICE_TABLE(of, qcom_ipq806x_sata_phy_of_match);
  162. static struct platform_driver qcom_ipq806x_sata_phy_driver = {
  163. .probe = qcom_ipq806x_sata_phy_probe,
  164. .remove = qcom_ipq806x_sata_phy_remove,
  165. .driver = {
  166. .name = "qcom-ipq806x-sata-phy",
  167. .of_match_table = qcom_ipq806x_sata_phy_of_match,
  168. }
  169. };
  170. module_platform_driver(qcom_ipq806x_sata_phy_driver);
  171. MODULE_DESCRIPTION("QCOM IPQ806x SATA PHY driver");
  172. MODULE_LICENSE("GPL v2");