phy-mvebu-sata.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * phy-mvebu-sata.c: SATA Phy driver for the Marvell mvebu SoCs.
  3. *
  4. * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/clk.h>
  14. #include <linux/phy/phy.h>
  15. #include <linux/io.h>
  16. #include <linux/platform_device.h>
  17. struct priv {
  18. struct clk *clk;
  19. void __iomem *base;
  20. };
  21. #define SATA_PHY_MODE_2 0x0330
  22. #define MODE_2_FORCE_PU_TX BIT(0)
  23. #define MODE_2_FORCE_PU_RX BIT(1)
  24. #define MODE_2_PU_PLL BIT(2)
  25. #define MODE_2_PU_IVREF BIT(3)
  26. #define SATA_IF_CTRL 0x0050
  27. #define CTRL_PHY_SHUTDOWN BIT(9)
  28. static int phy_mvebu_sata_power_on(struct phy *phy)
  29. {
  30. struct priv *priv = phy_get_drvdata(phy);
  31. u32 reg;
  32. clk_prepare_enable(priv->clk);
  33. /* Enable PLL and IVREF */
  34. reg = readl(priv->base + SATA_PHY_MODE_2);
  35. reg |= (MODE_2_FORCE_PU_TX | MODE_2_FORCE_PU_RX |
  36. MODE_2_PU_PLL | MODE_2_PU_IVREF);
  37. writel(reg , priv->base + SATA_PHY_MODE_2);
  38. /* Enable PHY */
  39. reg = readl(priv->base + SATA_IF_CTRL);
  40. reg &= ~CTRL_PHY_SHUTDOWN;
  41. writel(reg, priv->base + SATA_IF_CTRL);
  42. clk_disable_unprepare(priv->clk);
  43. return 0;
  44. }
  45. static int phy_mvebu_sata_power_off(struct phy *phy)
  46. {
  47. struct priv *priv = phy_get_drvdata(phy);
  48. u32 reg;
  49. clk_prepare_enable(priv->clk);
  50. /* Disable PLL and IVREF */
  51. reg = readl(priv->base + SATA_PHY_MODE_2);
  52. reg &= ~(MODE_2_FORCE_PU_TX | MODE_2_FORCE_PU_RX |
  53. MODE_2_PU_PLL | MODE_2_PU_IVREF);
  54. writel(reg, priv->base + SATA_PHY_MODE_2);
  55. /* Disable PHY */
  56. reg = readl(priv->base + SATA_IF_CTRL);
  57. reg |= CTRL_PHY_SHUTDOWN;
  58. writel(reg, priv->base + SATA_IF_CTRL);
  59. clk_disable_unprepare(priv->clk);
  60. return 0;
  61. }
  62. static const struct phy_ops phy_mvebu_sata_ops = {
  63. .power_on = phy_mvebu_sata_power_on,
  64. .power_off = phy_mvebu_sata_power_off,
  65. .owner = THIS_MODULE,
  66. };
  67. static int phy_mvebu_sata_probe(struct platform_device *pdev)
  68. {
  69. struct phy_provider *phy_provider;
  70. struct resource *res;
  71. struct priv *priv;
  72. struct phy *phy;
  73. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  74. if (!priv)
  75. return -ENOMEM;
  76. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  77. priv->base = devm_ioremap_resource(&pdev->dev, res);
  78. if (IS_ERR(priv->base))
  79. return PTR_ERR(priv->base);
  80. priv->clk = devm_clk_get(&pdev->dev, "sata");
  81. if (IS_ERR(priv->clk))
  82. return PTR_ERR(priv->clk);
  83. phy = devm_phy_create(&pdev->dev, NULL, &phy_mvebu_sata_ops);
  84. if (IS_ERR(phy))
  85. return PTR_ERR(phy);
  86. phy_set_drvdata(phy, priv);
  87. phy_provider = devm_of_phy_provider_register(&pdev->dev,
  88. of_phy_simple_xlate);
  89. if (IS_ERR(phy_provider))
  90. return PTR_ERR(phy_provider);
  91. /* The boot loader may of left it on. Turn it off. */
  92. phy_mvebu_sata_power_off(phy);
  93. return 0;
  94. }
  95. static const struct of_device_id phy_mvebu_sata_of_match[] = {
  96. { .compatible = "marvell,mvebu-sata-phy" },
  97. { },
  98. };
  99. MODULE_DEVICE_TABLE(of, phy_mvebu_sata_of_match);
  100. static struct platform_driver phy_mvebu_sata_driver = {
  101. .probe = phy_mvebu_sata_probe,
  102. .driver = {
  103. .name = "phy-mvebu-sata",
  104. .of_match_table = phy_mvebu_sata_of_match,
  105. }
  106. };
  107. module_platform_driver(phy_mvebu_sata_driver);
  108. MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
  109. MODULE_DESCRIPTION("Marvell MVEBU SATA PHY driver");
  110. MODULE_LICENSE("GPL v2");