phy-hix5hd2-sata.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2014 Linaro Ltd.
  3. * Copyright (c) 2014 Hisilicon Limited.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/io.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/phy/phy.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #define SATA_PHY0_CTLL 0xa0
  18. #define MPLL_MULTIPLIER_SHIFT 1
  19. #define MPLL_MULTIPLIER_MASK 0xfe
  20. #define MPLL_MULTIPLIER_50M 0x3c
  21. #define MPLL_MULTIPLIER_100M 0x1e
  22. #define PHY_RESET BIT(0)
  23. #define REF_SSP_EN BIT(9)
  24. #define SSC_EN BIT(10)
  25. #define REF_USE_PAD BIT(23)
  26. #define SATA_PORT_PHYCTL 0x174
  27. #define SPEED_MODE_MASK 0x6f0000
  28. #define HALF_RATE_SHIFT 16
  29. #define PHY_CONFIG_SHIFT 18
  30. #define GEN2_EN_SHIFT 21
  31. #define SPEED_CTRL BIT(20)
  32. #define SATA_PORT_PHYCTL1 0x148
  33. #define AMPLITUDE_MASK 0x3ffffe
  34. #define AMPLITUDE_GEN3 0x68
  35. #define AMPLITUDE_GEN3_SHIFT 15
  36. #define AMPLITUDE_GEN2 0x56
  37. #define AMPLITUDE_GEN2_SHIFT 8
  38. #define AMPLITUDE_GEN1 0x56
  39. #define AMPLITUDE_GEN1_SHIFT 1
  40. #define SATA_PORT_PHYCTL2 0x14c
  41. #define PREEMPH_MASK 0x3ffff
  42. #define PREEMPH_GEN3 0x20
  43. #define PREEMPH_GEN3_SHIFT 12
  44. #define PREEMPH_GEN2 0x15
  45. #define PREEMPH_GEN2_SHIFT 6
  46. #define PREEMPH_GEN1 0x5
  47. #define PREEMPH_GEN1_SHIFT 0
  48. struct hix5hd2_priv {
  49. void __iomem *base;
  50. struct regmap *peri_ctrl;
  51. };
  52. enum phy_speed_mode {
  53. SPEED_MODE_GEN1 = 0,
  54. SPEED_MODE_GEN2 = 1,
  55. SPEED_MODE_GEN3 = 2,
  56. };
  57. static int hix5hd2_sata_phy_init(struct phy *phy)
  58. {
  59. struct hix5hd2_priv *priv = phy_get_drvdata(phy);
  60. u32 val, data[2];
  61. int ret;
  62. if (priv->peri_ctrl) {
  63. ret = of_property_read_u32_array(phy->dev.of_node,
  64. "hisilicon,power-reg",
  65. &data[0], 2);
  66. if (ret) {
  67. dev_err(&phy->dev, "Fail read hisilicon,power-reg\n");
  68. return ret;
  69. }
  70. regmap_update_bits(priv->peri_ctrl, data[0],
  71. BIT(data[1]), BIT(data[1]));
  72. }
  73. /* reset phy */
  74. val = readl_relaxed(priv->base + SATA_PHY0_CTLL);
  75. val &= ~(MPLL_MULTIPLIER_MASK | REF_USE_PAD);
  76. val |= MPLL_MULTIPLIER_50M << MPLL_MULTIPLIER_SHIFT |
  77. REF_SSP_EN | PHY_RESET;
  78. writel_relaxed(val, priv->base + SATA_PHY0_CTLL);
  79. msleep(20);
  80. val &= ~PHY_RESET;
  81. writel_relaxed(val, priv->base + SATA_PHY0_CTLL);
  82. val = readl_relaxed(priv->base + SATA_PORT_PHYCTL1);
  83. val &= ~AMPLITUDE_MASK;
  84. val |= AMPLITUDE_GEN3 << AMPLITUDE_GEN3_SHIFT |
  85. AMPLITUDE_GEN2 << AMPLITUDE_GEN2_SHIFT |
  86. AMPLITUDE_GEN1 << AMPLITUDE_GEN1_SHIFT;
  87. writel_relaxed(val, priv->base + SATA_PORT_PHYCTL1);
  88. val = readl_relaxed(priv->base + SATA_PORT_PHYCTL2);
  89. val &= ~PREEMPH_MASK;
  90. val |= PREEMPH_GEN3 << PREEMPH_GEN3_SHIFT |
  91. PREEMPH_GEN2 << PREEMPH_GEN2_SHIFT |
  92. PREEMPH_GEN1 << PREEMPH_GEN1_SHIFT;
  93. writel_relaxed(val, priv->base + SATA_PORT_PHYCTL2);
  94. /* ensure PHYCTRL setting takes effect */
  95. val = readl_relaxed(priv->base + SATA_PORT_PHYCTL);
  96. val &= ~SPEED_MODE_MASK;
  97. val |= SPEED_MODE_GEN1 << HALF_RATE_SHIFT |
  98. SPEED_MODE_GEN1 << PHY_CONFIG_SHIFT |
  99. SPEED_MODE_GEN1 << GEN2_EN_SHIFT | SPEED_CTRL;
  100. writel_relaxed(val, priv->base + SATA_PORT_PHYCTL);
  101. msleep(20);
  102. val &= ~SPEED_MODE_MASK;
  103. val |= SPEED_MODE_GEN3 << HALF_RATE_SHIFT |
  104. SPEED_MODE_GEN3 << PHY_CONFIG_SHIFT |
  105. SPEED_MODE_GEN3 << GEN2_EN_SHIFT | SPEED_CTRL;
  106. writel_relaxed(val, priv->base + SATA_PORT_PHYCTL);
  107. val &= ~(SPEED_MODE_MASK | SPEED_CTRL);
  108. val |= SPEED_MODE_GEN2 << HALF_RATE_SHIFT |
  109. SPEED_MODE_GEN2 << PHY_CONFIG_SHIFT |
  110. SPEED_MODE_GEN2 << GEN2_EN_SHIFT;
  111. writel_relaxed(val, priv->base + SATA_PORT_PHYCTL);
  112. return 0;
  113. }
  114. static const struct phy_ops hix5hd2_sata_phy_ops = {
  115. .init = hix5hd2_sata_phy_init,
  116. .owner = THIS_MODULE,
  117. };
  118. static int hix5hd2_sata_phy_probe(struct platform_device *pdev)
  119. {
  120. struct phy_provider *phy_provider;
  121. struct device *dev = &pdev->dev;
  122. struct resource *res;
  123. struct phy *phy;
  124. struct hix5hd2_priv *priv;
  125. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  126. if (!priv)
  127. return -ENOMEM;
  128. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  129. if (!res)
  130. return -EINVAL;
  131. priv->base = devm_ioremap(dev, res->start, resource_size(res));
  132. if (!priv->base)
  133. return -ENOMEM;
  134. priv->peri_ctrl = syscon_regmap_lookup_by_phandle(dev->of_node,
  135. "hisilicon,peripheral-syscon");
  136. if (IS_ERR(priv->peri_ctrl))
  137. priv->peri_ctrl = NULL;
  138. phy = devm_phy_create(dev, NULL, &hix5hd2_sata_phy_ops);
  139. if (IS_ERR(phy)) {
  140. dev_err(dev, "failed to create PHY\n");
  141. return PTR_ERR(phy);
  142. }
  143. phy_set_drvdata(phy, priv);
  144. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  145. return PTR_ERR_OR_ZERO(phy_provider);
  146. }
  147. static const struct of_device_id hix5hd2_sata_phy_of_match[] = {
  148. {.compatible = "hisilicon,hix5hd2-sata-phy",},
  149. { },
  150. };
  151. MODULE_DEVICE_TABLE(of, hix5hd2_sata_phy_of_match);
  152. static struct platform_driver hix5hd2_sata_phy_driver = {
  153. .probe = hix5hd2_sata_phy_probe,
  154. .driver = {
  155. .name = "hix5hd2-sata-phy",
  156. .of_match_table = hix5hd2_sata_phy_of_match,
  157. }
  158. };
  159. module_platform_driver(hix5hd2_sata_phy_driver);
  160. MODULE_AUTHOR("Jiancheng Xue <xuejiancheng@huawei.com>");
  161. MODULE_DESCRIPTION("HISILICON HIX5HD2 SATA PHY driver");
  162. MODULE_ALIAS("platform:hix5hd2-sata-phy");
  163. MODULE_LICENSE("GPL v2");