phy-spear1310-miphy.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * ST SPEAr1310-miphy driver
  3. *
  4. * Copyright (C) 2014 ST Microelectronics
  5. * Pratyush Anand <pratyush.anand@gmail.com>
  6. * Mohit Kumar <mohit.kumar.dhaka@gmail.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. */
  13. #include <linux/bitops.h>
  14. #include <linux/delay.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/module.h>
  19. #include <linux/of_device.h>
  20. #include <linux/phy/phy.h>
  21. #include <linux/regmap.h>
  22. /* SPEAr1310 Registers */
  23. #define SPEAR1310_PCIE_SATA_CFG 0x3A4
  24. #define SPEAR1310_PCIE_SATA2_SEL_PCIE (0 << 31)
  25. #define SPEAR1310_PCIE_SATA1_SEL_PCIE (0 << 30)
  26. #define SPEAR1310_PCIE_SATA0_SEL_PCIE (0 << 29)
  27. #define SPEAR1310_PCIE_SATA2_SEL_SATA BIT(31)
  28. #define SPEAR1310_PCIE_SATA1_SEL_SATA BIT(30)
  29. #define SPEAR1310_PCIE_SATA0_SEL_SATA BIT(29)
  30. #define SPEAR1310_SATA2_CFG_TX_CLK_EN BIT(27)
  31. #define SPEAR1310_SATA2_CFG_RX_CLK_EN BIT(26)
  32. #define SPEAR1310_SATA2_CFG_POWERUP_RESET BIT(25)
  33. #define SPEAR1310_SATA2_CFG_PM_CLK_EN BIT(24)
  34. #define SPEAR1310_SATA1_CFG_TX_CLK_EN BIT(23)
  35. #define SPEAR1310_SATA1_CFG_RX_CLK_EN BIT(22)
  36. #define SPEAR1310_SATA1_CFG_POWERUP_RESET BIT(21)
  37. #define SPEAR1310_SATA1_CFG_PM_CLK_EN BIT(20)
  38. #define SPEAR1310_SATA0_CFG_TX_CLK_EN BIT(19)
  39. #define SPEAR1310_SATA0_CFG_RX_CLK_EN BIT(18)
  40. #define SPEAR1310_SATA0_CFG_POWERUP_RESET BIT(17)
  41. #define SPEAR1310_SATA0_CFG_PM_CLK_EN BIT(16)
  42. #define SPEAR1310_PCIE2_CFG_DEVICE_PRESENT BIT(11)
  43. #define SPEAR1310_PCIE2_CFG_POWERUP_RESET BIT(10)
  44. #define SPEAR1310_PCIE2_CFG_CORE_CLK_EN BIT(9)
  45. #define SPEAR1310_PCIE2_CFG_AUX_CLK_EN BIT(8)
  46. #define SPEAR1310_PCIE1_CFG_DEVICE_PRESENT BIT(7)
  47. #define SPEAR1310_PCIE1_CFG_POWERUP_RESET BIT(6)
  48. #define SPEAR1310_PCIE1_CFG_CORE_CLK_EN BIT(5)
  49. #define SPEAR1310_PCIE1_CFG_AUX_CLK_EN BIT(4)
  50. #define SPEAR1310_PCIE0_CFG_DEVICE_PRESENT BIT(3)
  51. #define SPEAR1310_PCIE0_CFG_POWERUP_RESET BIT(2)
  52. #define SPEAR1310_PCIE0_CFG_CORE_CLK_EN BIT(1)
  53. #define SPEAR1310_PCIE0_CFG_AUX_CLK_EN BIT(0)
  54. #define SPEAR1310_PCIE_CFG_MASK(x) ((0xF << (x * 4)) | BIT((x + 29)))
  55. #define SPEAR1310_SATA_CFG_MASK(x) ((0xF << (x * 4 + 16)) | \
  56. BIT((x + 29)))
  57. #define SPEAR1310_PCIE_CFG_VAL(x) \
  58. (SPEAR1310_PCIE_SATA##x##_SEL_PCIE | \
  59. SPEAR1310_PCIE##x##_CFG_AUX_CLK_EN | \
  60. SPEAR1310_PCIE##x##_CFG_CORE_CLK_EN | \
  61. SPEAR1310_PCIE##x##_CFG_POWERUP_RESET | \
  62. SPEAR1310_PCIE##x##_CFG_DEVICE_PRESENT)
  63. #define SPEAR1310_SATA_CFG_VAL(x) \
  64. (SPEAR1310_PCIE_SATA##x##_SEL_SATA | \
  65. SPEAR1310_SATA##x##_CFG_PM_CLK_EN | \
  66. SPEAR1310_SATA##x##_CFG_POWERUP_RESET | \
  67. SPEAR1310_SATA##x##_CFG_RX_CLK_EN | \
  68. SPEAR1310_SATA##x##_CFG_TX_CLK_EN)
  69. #define SPEAR1310_PCIE_MIPHY_CFG_1 0x3A8
  70. #define SPEAR1310_MIPHY_DUAL_OSC_BYPASS_EXT BIT(31)
  71. #define SPEAR1310_MIPHY_DUAL_CLK_REF_DIV2 BIT(28)
  72. #define SPEAR1310_MIPHY_DUAL_PLL_RATIO_TOP(x) (x << 16)
  73. #define SPEAR1310_MIPHY_SINGLE_OSC_BYPASS_EXT BIT(15)
  74. #define SPEAR1310_MIPHY_SINGLE_CLK_REF_DIV2 BIT(12)
  75. #define SPEAR1310_MIPHY_SINGLE_PLL_RATIO_TOP(x) (x << 0)
  76. #define SPEAR1310_PCIE_SATA_MIPHY_CFG_SATA_MASK (0xFFFF)
  77. #define SPEAR1310_PCIE_SATA_MIPHY_CFG_PCIE_MASK (0xFFFF << 16)
  78. #define SPEAR1310_PCIE_SATA_MIPHY_CFG_SATA \
  79. (SPEAR1310_MIPHY_DUAL_OSC_BYPASS_EXT | \
  80. SPEAR1310_MIPHY_DUAL_CLK_REF_DIV2 | \
  81. SPEAR1310_MIPHY_DUAL_PLL_RATIO_TOP(60) | \
  82. SPEAR1310_MIPHY_SINGLE_OSC_BYPASS_EXT | \
  83. SPEAR1310_MIPHY_SINGLE_CLK_REF_DIV2 | \
  84. SPEAR1310_MIPHY_SINGLE_PLL_RATIO_TOP(60))
  85. #define SPEAR1310_PCIE_SATA_MIPHY_CFG_SATA_25M_CRYSTAL_CLK \
  86. (SPEAR1310_MIPHY_SINGLE_PLL_RATIO_TOP(120))
  87. #define SPEAR1310_PCIE_SATA_MIPHY_CFG_PCIE \
  88. (SPEAR1310_MIPHY_DUAL_OSC_BYPASS_EXT | \
  89. SPEAR1310_MIPHY_DUAL_PLL_RATIO_TOP(25) | \
  90. SPEAR1310_MIPHY_SINGLE_OSC_BYPASS_EXT | \
  91. SPEAR1310_MIPHY_SINGLE_PLL_RATIO_TOP(25))
  92. #define SPEAR1310_PCIE_MIPHY_CFG_2 0x3AC
  93. enum spear1310_miphy_mode {
  94. SATA,
  95. PCIE,
  96. };
  97. struct spear1310_miphy_priv {
  98. /* instance id of this phy */
  99. u32 id;
  100. /* phy mode: 0 for SATA 1 for PCIe */
  101. enum spear1310_miphy_mode mode;
  102. /* regmap for any soc specific misc registers */
  103. struct regmap *misc;
  104. /* phy struct pointer */
  105. struct phy *phy;
  106. };
  107. static int spear1310_miphy_pcie_init(struct spear1310_miphy_priv *priv)
  108. {
  109. u32 val;
  110. regmap_update_bits(priv->misc, SPEAR1310_PCIE_MIPHY_CFG_1,
  111. SPEAR1310_PCIE_SATA_MIPHY_CFG_PCIE_MASK,
  112. SPEAR1310_PCIE_SATA_MIPHY_CFG_PCIE);
  113. switch (priv->id) {
  114. case 0:
  115. val = SPEAR1310_PCIE_CFG_VAL(0);
  116. break;
  117. case 1:
  118. val = SPEAR1310_PCIE_CFG_VAL(1);
  119. break;
  120. case 2:
  121. val = SPEAR1310_PCIE_CFG_VAL(2);
  122. break;
  123. default:
  124. return -EINVAL;
  125. }
  126. regmap_update_bits(priv->misc, SPEAR1310_PCIE_SATA_CFG,
  127. SPEAR1310_PCIE_CFG_MASK(priv->id), val);
  128. return 0;
  129. }
  130. static int spear1310_miphy_pcie_exit(struct spear1310_miphy_priv *priv)
  131. {
  132. regmap_update_bits(priv->misc, SPEAR1310_PCIE_SATA_CFG,
  133. SPEAR1310_PCIE_CFG_MASK(priv->id), 0);
  134. regmap_update_bits(priv->misc, SPEAR1310_PCIE_MIPHY_CFG_1,
  135. SPEAR1310_PCIE_SATA_MIPHY_CFG_PCIE_MASK, 0);
  136. return 0;
  137. }
  138. static int spear1310_miphy_init(struct phy *phy)
  139. {
  140. struct spear1310_miphy_priv *priv = phy_get_drvdata(phy);
  141. int ret = 0;
  142. if (priv->mode == PCIE)
  143. ret = spear1310_miphy_pcie_init(priv);
  144. return ret;
  145. }
  146. static int spear1310_miphy_exit(struct phy *phy)
  147. {
  148. struct spear1310_miphy_priv *priv = phy_get_drvdata(phy);
  149. int ret = 0;
  150. if (priv->mode == PCIE)
  151. ret = spear1310_miphy_pcie_exit(priv);
  152. return ret;
  153. }
  154. static const struct of_device_id spear1310_miphy_of_match[] = {
  155. { .compatible = "st,spear1310-miphy" },
  156. { },
  157. };
  158. MODULE_DEVICE_TABLE(of, spear1310_miphy_of_match);
  159. static const struct phy_ops spear1310_miphy_ops = {
  160. .init = spear1310_miphy_init,
  161. .exit = spear1310_miphy_exit,
  162. .owner = THIS_MODULE,
  163. };
  164. static struct phy *spear1310_miphy_xlate(struct device *dev,
  165. struct of_phandle_args *args)
  166. {
  167. struct spear1310_miphy_priv *priv = dev_get_drvdata(dev);
  168. if (args->args_count < 1) {
  169. dev_err(dev, "DT did not pass correct no of args\n");
  170. return ERR_PTR(-ENODEV);
  171. }
  172. priv->mode = args->args[0];
  173. if (priv->mode != SATA && priv->mode != PCIE) {
  174. dev_err(dev, "DT did not pass correct phy mode\n");
  175. return ERR_PTR(-ENODEV);
  176. }
  177. return priv->phy;
  178. }
  179. static int spear1310_miphy_probe(struct platform_device *pdev)
  180. {
  181. struct device *dev = &pdev->dev;
  182. struct spear1310_miphy_priv *priv;
  183. struct phy_provider *phy_provider;
  184. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  185. if (!priv)
  186. return -ENOMEM;
  187. priv->misc =
  188. syscon_regmap_lookup_by_phandle(dev->of_node, "misc");
  189. if (IS_ERR(priv->misc)) {
  190. dev_err(dev, "failed to find misc regmap\n");
  191. return PTR_ERR(priv->misc);
  192. }
  193. if (of_property_read_u32(dev->of_node, "phy-id", &priv->id)) {
  194. dev_err(dev, "failed to find phy id\n");
  195. return -EINVAL;
  196. }
  197. priv->phy = devm_phy_create(dev, NULL, &spear1310_miphy_ops);
  198. if (IS_ERR(priv->phy)) {
  199. dev_err(dev, "failed to create SATA PCIe PHY\n");
  200. return PTR_ERR(priv->phy);
  201. }
  202. dev_set_drvdata(dev, priv);
  203. phy_set_drvdata(priv->phy, priv);
  204. phy_provider =
  205. devm_of_phy_provider_register(dev, spear1310_miphy_xlate);
  206. if (IS_ERR(phy_provider)) {
  207. dev_err(dev, "failed to register phy provider\n");
  208. return PTR_ERR(phy_provider);
  209. }
  210. return 0;
  211. }
  212. static struct platform_driver spear1310_miphy_driver = {
  213. .probe = spear1310_miphy_probe,
  214. .driver = {
  215. .name = "spear1310-miphy",
  216. .of_match_table = of_match_ptr(spear1310_miphy_of_match),
  217. },
  218. };
  219. module_platform_driver(spear1310_miphy_driver);
  220. MODULE_DESCRIPTION("ST SPEAR1310-MIPHY driver");
  221. MODULE_AUTHOR("Pratyush Anand <pratyush.anand@gmail.com>");
  222. MODULE_LICENSE("GPL v2");