phy-rcar-gen3-pcie.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas R-Car Gen3 PCIe PHY driver
  4. *
  5. * Copyright (C) 2018 Cogent Embedded, Inc.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/phy/phy.h>
  12. #include <linux/of_device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/spinlock.h>
  15. #define PHY_CTRL 0x4000 /* R8A77980 only */
  16. /* PHY control register (PHY_CTRL) */
  17. #define PHY_CTRL_PHY_PWDN BIT(2)
  18. struct rcar_gen3_phy {
  19. struct phy *phy;
  20. spinlock_t lock;
  21. void __iomem *base;
  22. };
  23. static void rcar_gen3_phy_pcie_modify_reg(struct phy *p, unsigned int reg,
  24. u32 clear, u32 set)
  25. {
  26. struct rcar_gen3_phy *phy = phy_get_drvdata(p);
  27. void __iomem *base = phy->base;
  28. unsigned long flags;
  29. u32 value;
  30. spin_lock_irqsave(&phy->lock, flags);
  31. value = readl(base + reg);
  32. value &= ~clear;
  33. value |= set;
  34. writel(value, base + reg);
  35. spin_unlock_irqrestore(&phy->lock, flags);
  36. }
  37. static int r8a77980_phy_pcie_power_on(struct phy *p)
  38. {
  39. /* Power on the PCIe PHY */
  40. rcar_gen3_phy_pcie_modify_reg(p, PHY_CTRL, PHY_CTRL_PHY_PWDN, 0);
  41. return 0;
  42. }
  43. static int r8a77980_phy_pcie_power_off(struct phy *p)
  44. {
  45. /* Power off the PCIe PHY */
  46. rcar_gen3_phy_pcie_modify_reg(p, PHY_CTRL, 0, PHY_CTRL_PHY_PWDN);
  47. return 0;
  48. }
  49. static const struct phy_ops r8a77980_phy_pcie_ops = {
  50. .power_on = r8a77980_phy_pcie_power_on,
  51. .power_off = r8a77980_phy_pcie_power_off,
  52. .owner = THIS_MODULE,
  53. };
  54. static const struct of_device_id rcar_gen3_phy_pcie_match_table[] = {
  55. { .compatible = "renesas,r8a77980-pcie-phy" },
  56. { }
  57. };
  58. MODULE_DEVICE_TABLE(of, rcar_gen3_phy_pcie_match_table);
  59. static int rcar_gen3_phy_pcie_probe(struct platform_device *pdev)
  60. {
  61. struct device *dev = &pdev->dev;
  62. struct phy_provider *provider;
  63. struct rcar_gen3_phy *phy;
  64. struct resource *res;
  65. void __iomem *base;
  66. int error;
  67. if (!dev->of_node) {
  68. dev_err(dev,
  69. "This driver must only be instantiated from the device tree\n");
  70. return -EINVAL;
  71. }
  72. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  73. base = devm_ioremap_resource(dev, res);
  74. if (IS_ERR(base))
  75. return PTR_ERR(base);
  76. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  77. if (!phy)
  78. return -ENOMEM;
  79. spin_lock_init(&phy->lock);
  80. phy->base = base;
  81. /*
  82. * devm_phy_create() will call pm_runtime_enable(&phy->dev);
  83. * And then, phy-core will manage runtime PM for this device.
  84. */
  85. pm_runtime_enable(dev);
  86. phy->phy = devm_phy_create(dev, NULL, &r8a77980_phy_pcie_ops);
  87. if (IS_ERR(phy->phy)) {
  88. dev_err(dev, "Failed to create PCIe PHY\n");
  89. error = PTR_ERR(phy->phy);
  90. goto error;
  91. }
  92. phy_set_drvdata(phy->phy, phy);
  93. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  94. if (IS_ERR(provider)) {
  95. dev_err(dev, "Failed to register PHY provider\n");
  96. error = PTR_ERR(provider);
  97. goto error;
  98. }
  99. return 0;
  100. error:
  101. pm_runtime_disable(dev);
  102. return error;
  103. }
  104. static int rcar_gen3_phy_pcie_remove(struct platform_device *pdev)
  105. {
  106. pm_runtime_disable(&pdev->dev);
  107. return 0;
  108. };
  109. static struct platform_driver rcar_gen3_phy_driver = {
  110. .driver = {
  111. .name = "phy_rcar_gen3_pcie",
  112. .of_match_table = rcar_gen3_phy_pcie_match_table,
  113. },
  114. .probe = rcar_gen3_phy_pcie_probe,
  115. .remove = rcar_gen3_phy_pcie_remove,
  116. };
  117. module_platform_driver(rcar_gen3_phy_driver);
  118. MODULE_LICENSE("GPL v2");
  119. MODULE_DESCRIPTION("Renesas R-Car Gen3 PCIe PHY");
  120. MODULE_AUTHOR("Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>");