phy-spear1340-miphy.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * ST spear1340-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. /* SPEAr1340 Registers */
  23. /* Power Management Registers */
  24. #define SPEAR1340_PCM_CFG 0x100
  25. #define SPEAR1340_PCM_CFG_SATA_POWER_EN BIT(11)
  26. #define SPEAR1340_PCM_WKUP_CFG 0x104
  27. #define SPEAR1340_SWITCH_CTR 0x108
  28. #define SPEAR1340_PERIP1_SW_RST 0x318
  29. #define SPEAR1340_PERIP1_SW_RSATA BIT(12)
  30. #define SPEAR1340_PERIP2_SW_RST 0x31C
  31. #define SPEAR1340_PERIP3_SW_RST 0x320
  32. /* PCIE - SATA configuration registers */
  33. #define SPEAR1340_PCIE_SATA_CFG 0x424
  34. /* PCIE CFG MASks */
  35. #define SPEAR1340_PCIE_CFG_DEVICE_PRESENT BIT(11)
  36. #define SPEAR1340_PCIE_CFG_POWERUP_RESET BIT(10)
  37. #define SPEAR1340_PCIE_CFG_CORE_CLK_EN BIT(9)
  38. #define SPEAR1340_PCIE_CFG_AUX_CLK_EN BIT(8)
  39. #define SPEAR1340_SATA_CFG_TX_CLK_EN BIT(4)
  40. #define SPEAR1340_SATA_CFG_RX_CLK_EN BIT(3)
  41. #define SPEAR1340_SATA_CFG_POWERUP_RESET BIT(2)
  42. #define SPEAR1340_SATA_CFG_PM_CLK_EN BIT(1)
  43. #define SPEAR1340_PCIE_SATA_SEL_PCIE (0)
  44. #define SPEAR1340_PCIE_SATA_SEL_SATA (1)
  45. #define SPEAR1340_PCIE_SATA_CFG_MASK 0xF1F
  46. #define SPEAR1340_PCIE_CFG_VAL (SPEAR1340_PCIE_SATA_SEL_PCIE | \
  47. SPEAR1340_PCIE_CFG_AUX_CLK_EN | \
  48. SPEAR1340_PCIE_CFG_CORE_CLK_EN | \
  49. SPEAR1340_PCIE_CFG_POWERUP_RESET | \
  50. SPEAR1340_PCIE_CFG_DEVICE_PRESENT)
  51. #define SPEAR1340_SATA_CFG_VAL (SPEAR1340_PCIE_SATA_SEL_SATA | \
  52. SPEAR1340_SATA_CFG_PM_CLK_EN | \
  53. SPEAR1340_SATA_CFG_POWERUP_RESET | \
  54. SPEAR1340_SATA_CFG_RX_CLK_EN | \
  55. SPEAR1340_SATA_CFG_TX_CLK_EN)
  56. #define SPEAR1340_PCIE_MIPHY_CFG 0x428
  57. #define SPEAR1340_MIPHY_OSC_BYPASS_EXT BIT(31)
  58. #define SPEAR1340_MIPHY_CLK_REF_DIV2 BIT(27)
  59. #define SPEAR1340_MIPHY_CLK_REF_DIV4 (2 << 27)
  60. #define SPEAR1340_MIPHY_CLK_REF_DIV8 (3 << 27)
  61. #define SPEAR1340_MIPHY_PLL_RATIO_TOP(x) (x << 0)
  62. #define SPEAR1340_PCIE_MIPHY_CFG_MASK 0xF80000FF
  63. #define SPEAR1340_PCIE_SATA_MIPHY_CFG_SATA \
  64. (SPEAR1340_MIPHY_OSC_BYPASS_EXT | \
  65. SPEAR1340_MIPHY_CLK_REF_DIV2 | \
  66. SPEAR1340_MIPHY_PLL_RATIO_TOP(60))
  67. #define SPEAR1340_PCIE_SATA_MIPHY_CFG_SATA_25M_CRYSTAL_CLK \
  68. (SPEAR1340_MIPHY_PLL_RATIO_TOP(120))
  69. #define SPEAR1340_PCIE_SATA_MIPHY_CFG_PCIE \
  70. (SPEAR1340_MIPHY_OSC_BYPASS_EXT | \
  71. SPEAR1340_MIPHY_PLL_RATIO_TOP(25))
  72. enum spear1340_miphy_mode {
  73. SATA,
  74. PCIE,
  75. };
  76. struct spear1340_miphy_priv {
  77. /* phy mode: 0 for SATA 1 for PCIe */
  78. enum spear1340_miphy_mode mode;
  79. /* regmap for any soc specific misc registers */
  80. struct regmap *misc;
  81. /* phy struct pointer */
  82. struct phy *phy;
  83. };
  84. static int spear1340_miphy_sata_init(struct spear1340_miphy_priv *priv)
  85. {
  86. regmap_update_bits(priv->misc, SPEAR1340_PCIE_SATA_CFG,
  87. SPEAR1340_PCIE_SATA_CFG_MASK,
  88. SPEAR1340_SATA_CFG_VAL);
  89. regmap_update_bits(priv->misc, SPEAR1340_PCIE_MIPHY_CFG,
  90. SPEAR1340_PCIE_MIPHY_CFG_MASK,
  91. SPEAR1340_PCIE_SATA_MIPHY_CFG_SATA_25M_CRYSTAL_CLK);
  92. /* Switch on sata power domain */
  93. regmap_update_bits(priv->misc, SPEAR1340_PCM_CFG,
  94. SPEAR1340_PCM_CFG_SATA_POWER_EN,
  95. SPEAR1340_PCM_CFG_SATA_POWER_EN);
  96. /* Wait for SATA power domain on */
  97. msleep(20);
  98. /* Disable PCIE SATA Controller reset */
  99. regmap_update_bits(priv->misc, SPEAR1340_PERIP1_SW_RST,
  100. SPEAR1340_PERIP1_SW_RSATA, 0);
  101. /* Wait for SATA reset de-assert completion */
  102. msleep(20);
  103. return 0;
  104. }
  105. static int spear1340_miphy_sata_exit(struct spear1340_miphy_priv *priv)
  106. {
  107. regmap_update_bits(priv->misc, SPEAR1340_PCIE_SATA_CFG,
  108. SPEAR1340_PCIE_SATA_CFG_MASK, 0);
  109. regmap_update_bits(priv->misc, SPEAR1340_PCIE_MIPHY_CFG,
  110. SPEAR1340_PCIE_MIPHY_CFG_MASK, 0);
  111. /* Enable PCIE SATA Controller reset */
  112. regmap_update_bits(priv->misc, SPEAR1340_PERIP1_SW_RST,
  113. SPEAR1340_PERIP1_SW_RSATA,
  114. SPEAR1340_PERIP1_SW_RSATA);
  115. /* Wait for SATA power domain off */
  116. msleep(20);
  117. /* Switch off sata power domain */
  118. regmap_update_bits(priv->misc, SPEAR1340_PCM_CFG,
  119. SPEAR1340_PCM_CFG_SATA_POWER_EN, 0);
  120. /* Wait for SATA reset assert completion */
  121. msleep(20);
  122. return 0;
  123. }
  124. static int spear1340_miphy_pcie_init(struct spear1340_miphy_priv *priv)
  125. {
  126. regmap_update_bits(priv->misc, SPEAR1340_PCIE_MIPHY_CFG,
  127. SPEAR1340_PCIE_MIPHY_CFG_MASK,
  128. SPEAR1340_PCIE_SATA_MIPHY_CFG_PCIE);
  129. regmap_update_bits(priv->misc, SPEAR1340_PCIE_SATA_CFG,
  130. SPEAR1340_PCIE_SATA_CFG_MASK,
  131. SPEAR1340_PCIE_CFG_VAL);
  132. return 0;
  133. }
  134. static int spear1340_miphy_pcie_exit(struct spear1340_miphy_priv *priv)
  135. {
  136. regmap_update_bits(priv->misc, SPEAR1340_PCIE_MIPHY_CFG,
  137. SPEAR1340_PCIE_MIPHY_CFG_MASK, 0);
  138. regmap_update_bits(priv->misc, SPEAR1340_PCIE_SATA_CFG,
  139. SPEAR1340_PCIE_SATA_CFG_MASK, 0);
  140. return 0;
  141. }
  142. static int spear1340_miphy_init(struct phy *phy)
  143. {
  144. struct spear1340_miphy_priv *priv = phy_get_drvdata(phy);
  145. int ret = 0;
  146. if (priv->mode == SATA)
  147. ret = spear1340_miphy_sata_init(priv);
  148. else if (priv->mode == PCIE)
  149. ret = spear1340_miphy_pcie_init(priv);
  150. return ret;
  151. }
  152. static int spear1340_miphy_exit(struct phy *phy)
  153. {
  154. struct spear1340_miphy_priv *priv = phy_get_drvdata(phy);
  155. int ret = 0;
  156. if (priv->mode == SATA)
  157. ret = spear1340_miphy_sata_exit(priv);
  158. else if (priv->mode == PCIE)
  159. ret = spear1340_miphy_pcie_exit(priv);
  160. return ret;
  161. }
  162. static const struct of_device_id spear1340_miphy_of_match[] = {
  163. { .compatible = "st,spear1340-miphy" },
  164. { },
  165. };
  166. MODULE_DEVICE_TABLE(of, spear1340_miphy_of_match);
  167. static const struct phy_ops spear1340_miphy_ops = {
  168. .init = spear1340_miphy_init,
  169. .exit = spear1340_miphy_exit,
  170. .owner = THIS_MODULE,
  171. };
  172. #ifdef CONFIG_PM_SLEEP
  173. static int spear1340_miphy_suspend(struct device *dev)
  174. {
  175. struct spear1340_miphy_priv *priv = dev_get_drvdata(dev);
  176. int ret = 0;
  177. if (priv->mode == SATA)
  178. ret = spear1340_miphy_sata_exit(priv);
  179. return ret;
  180. }
  181. static int spear1340_miphy_resume(struct device *dev)
  182. {
  183. struct spear1340_miphy_priv *priv = dev_get_drvdata(dev);
  184. int ret = 0;
  185. if (priv->mode == SATA)
  186. ret = spear1340_miphy_sata_init(priv);
  187. return ret;
  188. }
  189. #endif
  190. static SIMPLE_DEV_PM_OPS(spear1340_miphy_pm_ops, spear1340_miphy_suspend,
  191. spear1340_miphy_resume);
  192. static struct phy *spear1340_miphy_xlate(struct device *dev,
  193. struct of_phandle_args *args)
  194. {
  195. struct spear1340_miphy_priv *priv = dev_get_drvdata(dev);
  196. if (args->args_count < 1) {
  197. dev_err(dev, "DT did not pass correct no of args\n");
  198. return ERR_PTR(-ENODEV);
  199. }
  200. priv->mode = args->args[0];
  201. if (priv->mode != SATA && priv->mode != PCIE) {
  202. dev_err(dev, "DT did not pass correct phy mode\n");
  203. return ERR_PTR(-ENODEV);
  204. }
  205. return priv->phy;
  206. }
  207. static int spear1340_miphy_probe(struct platform_device *pdev)
  208. {
  209. struct device *dev = &pdev->dev;
  210. struct spear1340_miphy_priv *priv;
  211. struct phy_provider *phy_provider;
  212. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  213. if (!priv)
  214. return -ENOMEM;
  215. priv->misc =
  216. syscon_regmap_lookup_by_phandle(dev->of_node, "misc");
  217. if (IS_ERR(priv->misc)) {
  218. dev_err(dev, "failed to find misc regmap\n");
  219. return PTR_ERR(priv->misc);
  220. }
  221. priv->phy = devm_phy_create(dev, NULL, &spear1340_miphy_ops);
  222. if (IS_ERR(priv->phy)) {
  223. dev_err(dev, "failed to create SATA PCIe PHY\n");
  224. return PTR_ERR(priv->phy);
  225. }
  226. dev_set_drvdata(dev, priv);
  227. phy_set_drvdata(priv->phy, priv);
  228. phy_provider =
  229. devm_of_phy_provider_register(dev, spear1340_miphy_xlate);
  230. if (IS_ERR(phy_provider)) {
  231. dev_err(dev, "failed to register phy provider\n");
  232. return PTR_ERR(phy_provider);
  233. }
  234. return 0;
  235. }
  236. static struct platform_driver spear1340_miphy_driver = {
  237. .probe = spear1340_miphy_probe,
  238. .driver = {
  239. .name = "spear1340-miphy",
  240. .pm = &spear1340_miphy_pm_ops,
  241. .of_match_table = of_match_ptr(spear1340_miphy_of_match),
  242. },
  243. };
  244. module_platform_driver(spear1340_miphy_driver);
  245. MODULE_DESCRIPTION("ST SPEAR1340-MIPHY driver");
  246. MODULE_AUTHOR("Pratyush Anand <pratyush.anand@gmail.com>");
  247. MODULE_LICENSE("GPL v2");