phy-exynos5250-sata.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Samsung SATA SerDes(PHY) driver
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  5. * Authors: Girish K S <ks.giri@samsung.com>
  6. * Yuvaraj Kumar C D <yuvaraj.cd@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/io.h>
  15. #include <linux/i2c.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/phy/phy.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/mfd/syscon.h>
  25. #define SATAPHY_CONTROL_OFFSET 0x0724
  26. #define EXYNOS5_SATAPHY_PMU_ENABLE BIT(0)
  27. #define EXYNOS5_SATA_RESET 0x4
  28. #define RESET_GLOBAL_RST_N BIT(0)
  29. #define RESET_CMN_RST_N BIT(1)
  30. #define RESET_CMN_BLOCK_RST_N BIT(2)
  31. #define RESET_CMN_I2C_RST_N BIT(3)
  32. #define RESET_TX_RX_PIPE_RST_N BIT(4)
  33. #define RESET_TX_RX_BLOCK_RST_N BIT(5)
  34. #define RESET_TX_RX_I2C_RST_N (BIT(6) | BIT(7))
  35. #define LINK_RESET 0xf0000
  36. #define EXYNOS5_SATA_MODE0 0x10
  37. #define SATA_SPD_GEN3 BIT(1)
  38. #define EXYNOS5_SATA_CTRL0 0x14
  39. #define CTRL0_P0_PHY_CALIBRATED_SEL BIT(9)
  40. #define CTRL0_P0_PHY_CALIBRATED BIT(8)
  41. #define EXYNOS5_SATA_PHSATA_CTRLM 0xe0
  42. #define PHCTRLM_REF_RATE BIT(1)
  43. #define PHCTRLM_HIGH_SPEED BIT(0)
  44. #define EXYNOS5_SATA_PHSATA_STATM 0xf0
  45. #define PHSTATM_PLL_LOCKED BIT(0)
  46. #define PHY_PLL_TIMEOUT (usecs_to_jiffies(1000))
  47. struct exynos_sata_phy {
  48. struct phy *phy;
  49. struct clk *phyclk;
  50. void __iomem *regs;
  51. struct regmap *pmureg;
  52. struct i2c_client *client;
  53. };
  54. static int wait_for_reg_status(void __iomem *base, u32 reg, u32 checkbit,
  55. u32 status)
  56. {
  57. unsigned long timeout = jiffies + PHY_PLL_TIMEOUT;
  58. while (time_before(jiffies, timeout)) {
  59. if ((readl(base + reg) & checkbit) == status)
  60. return 0;
  61. }
  62. return -EFAULT;
  63. }
  64. static int exynos_sata_phy_power_on(struct phy *phy)
  65. {
  66. struct exynos_sata_phy *sata_phy = phy_get_drvdata(phy);
  67. return regmap_update_bits(sata_phy->pmureg, SATAPHY_CONTROL_OFFSET,
  68. EXYNOS5_SATAPHY_PMU_ENABLE, true);
  69. }
  70. static int exynos_sata_phy_power_off(struct phy *phy)
  71. {
  72. struct exynos_sata_phy *sata_phy = phy_get_drvdata(phy);
  73. return regmap_update_bits(sata_phy->pmureg, SATAPHY_CONTROL_OFFSET,
  74. EXYNOS5_SATAPHY_PMU_ENABLE, false);
  75. }
  76. static int exynos_sata_phy_init(struct phy *phy)
  77. {
  78. u32 val = 0;
  79. int ret = 0;
  80. u8 buf[] = { 0x3a, 0x0b };
  81. struct exynos_sata_phy *sata_phy = phy_get_drvdata(phy);
  82. ret = regmap_update_bits(sata_phy->pmureg, SATAPHY_CONTROL_OFFSET,
  83. EXYNOS5_SATAPHY_PMU_ENABLE, true);
  84. if (ret != 0)
  85. dev_err(&sata_phy->phy->dev, "phy init failed\n");
  86. writel(val, sata_phy->regs + EXYNOS5_SATA_RESET);
  87. val = readl(sata_phy->regs + EXYNOS5_SATA_RESET);
  88. val |= RESET_GLOBAL_RST_N | RESET_CMN_RST_N | RESET_CMN_BLOCK_RST_N
  89. | RESET_CMN_I2C_RST_N | RESET_TX_RX_PIPE_RST_N
  90. | RESET_TX_RX_BLOCK_RST_N | RESET_TX_RX_I2C_RST_N;
  91. writel(val, sata_phy->regs + EXYNOS5_SATA_RESET);
  92. val = readl(sata_phy->regs + EXYNOS5_SATA_RESET);
  93. val |= LINK_RESET;
  94. writel(val, sata_phy->regs + EXYNOS5_SATA_RESET);
  95. val = readl(sata_phy->regs + EXYNOS5_SATA_RESET);
  96. val |= RESET_CMN_RST_N;
  97. writel(val, sata_phy->regs + EXYNOS5_SATA_RESET);
  98. val = readl(sata_phy->regs + EXYNOS5_SATA_PHSATA_CTRLM);
  99. val &= ~PHCTRLM_REF_RATE;
  100. writel(val, sata_phy->regs + EXYNOS5_SATA_PHSATA_CTRLM);
  101. /* High speed enable for Gen3 */
  102. val = readl(sata_phy->regs + EXYNOS5_SATA_PHSATA_CTRLM);
  103. val |= PHCTRLM_HIGH_SPEED;
  104. writel(val, sata_phy->regs + EXYNOS5_SATA_PHSATA_CTRLM);
  105. val = readl(sata_phy->regs + EXYNOS5_SATA_CTRL0);
  106. val |= CTRL0_P0_PHY_CALIBRATED_SEL | CTRL0_P0_PHY_CALIBRATED;
  107. writel(val, sata_phy->regs + EXYNOS5_SATA_CTRL0);
  108. val = readl(sata_phy->regs + EXYNOS5_SATA_MODE0);
  109. val |= SATA_SPD_GEN3;
  110. writel(val, sata_phy->regs + EXYNOS5_SATA_MODE0);
  111. ret = i2c_master_send(sata_phy->client, buf, sizeof(buf));
  112. if (ret < 0)
  113. return ret;
  114. /* release cmu reset */
  115. val = readl(sata_phy->regs + EXYNOS5_SATA_RESET);
  116. val &= ~RESET_CMN_RST_N;
  117. writel(val, sata_phy->regs + EXYNOS5_SATA_RESET);
  118. val = readl(sata_phy->regs + EXYNOS5_SATA_RESET);
  119. val |= RESET_CMN_RST_N;
  120. writel(val, sata_phy->regs + EXYNOS5_SATA_RESET);
  121. ret = wait_for_reg_status(sata_phy->regs,
  122. EXYNOS5_SATA_PHSATA_STATM,
  123. PHSTATM_PLL_LOCKED, 1);
  124. if (ret < 0)
  125. dev_err(&sata_phy->phy->dev,
  126. "PHY PLL locking failed\n");
  127. return ret;
  128. }
  129. static const struct phy_ops exynos_sata_phy_ops = {
  130. .init = exynos_sata_phy_init,
  131. .power_on = exynos_sata_phy_power_on,
  132. .power_off = exynos_sata_phy_power_off,
  133. .owner = THIS_MODULE,
  134. };
  135. static int exynos_sata_phy_probe(struct platform_device *pdev)
  136. {
  137. struct exynos_sata_phy *sata_phy;
  138. struct device *dev = &pdev->dev;
  139. struct resource *res;
  140. struct phy_provider *phy_provider;
  141. struct device_node *node;
  142. int ret = 0;
  143. sata_phy = devm_kzalloc(dev, sizeof(*sata_phy), GFP_KERNEL);
  144. if (!sata_phy)
  145. return -ENOMEM;
  146. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  147. sata_phy->regs = devm_ioremap_resource(dev, res);
  148. if (IS_ERR(sata_phy->regs))
  149. return PTR_ERR(sata_phy->regs);
  150. sata_phy->pmureg = syscon_regmap_lookup_by_phandle(dev->of_node,
  151. "samsung,syscon-phandle");
  152. if (IS_ERR(sata_phy->pmureg)) {
  153. dev_err(dev, "syscon regmap lookup failed.\n");
  154. return PTR_ERR(sata_phy->pmureg);
  155. }
  156. node = of_parse_phandle(dev->of_node,
  157. "samsung,exynos-sataphy-i2c-phandle", 0);
  158. if (!node)
  159. return -EINVAL;
  160. sata_phy->client = of_find_i2c_device_by_node(node);
  161. if (!sata_phy->client)
  162. return -EPROBE_DEFER;
  163. dev_set_drvdata(dev, sata_phy);
  164. sata_phy->phyclk = devm_clk_get(dev, "sata_phyctrl");
  165. if (IS_ERR(sata_phy->phyclk)) {
  166. dev_err(dev, "failed to get clk for PHY\n");
  167. return PTR_ERR(sata_phy->phyclk);
  168. }
  169. ret = clk_prepare_enable(sata_phy->phyclk);
  170. if (ret < 0) {
  171. dev_err(dev, "failed to enable source clk\n");
  172. return ret;
  173. }
  174. sata_phy->phy = devm_phy_create(dev, NULL, &exynos_sata_phy_ops);
  175. if (IS_ERR(sata_phy->phy)) {
  176. clk_disable_unprepare(sata_phy->phyclk);
  177. dev_err(dev, "failed to create PHY\n");
  178. return PTR_ERR(sata_phy->phy);
  179. }
  180. phy_set_drvdata(sata_phy->phy, sata_phy);
  181. phy_provider = devm_of_phy_provider_register(dev,
  182. of_phy_simple_xlate);
  183. if (IS_ERR(phy_provider)) {
  184. clk_disable_unprepare(sata_phy->phyclk);
  185. return PTR_ERR(phy_provider);
  186. }
  187. return 0;
  188. }
  189. static const struct of_device_id exynos_sata_phy_of_match[] = {
  190. { .compatible = "samsung,exynos5250-sata-phy" },
  191. { },
  192. };
  193. MODULE_DEVICE_TABLE(of, exynos_sata_phy_of_match);
  194. static struct platform_driver exynos_sata_phy_driver = {
  195. .probe = exynos_sata_phy_probe,
  196. .driver = {
  197. .of_match_table = exynos_sata_phy_of_match,
  198. .name = "samsung,sata-phy",
  199. }
  200. };
  201. module_platform_driver(exynos_sata_phy_driver);
  202. MODULE_DESCRIPTION("Samsung SerDes PHY driver");
  203. MODULE_LICENSE("GPL v2");
  204. MODULE_AUTHOR("Girish K S <ks.giri@samsung.com>");
  205. MODULE_AUTHOR("Yuvaraj C D <yuvaraj.cd@samsung.com>");