phy-bcm-cygnus-pcie.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2015 Broadcom Corporation
  3. #include <linux/delay.h>
  4. #include <linux/io.h>
  5. #include <linux/module.h>
  6. #include <linux/of.h>
  7. #include <linux/phy/phy.h>
  8. #include <linux/platform_device.h>
  9. #define PCIE_CFG_OFFSET 0x00
  10. #define PCIE1_PHY_IDDQ_SHIFT 10
  11. #define PCIE0_PHY_IDDQ_SHIFT 2
  12. enum cygnus_pcie_phy_id {
  13. CYGNUS_PHY_PCIE0 = 0,
  14. CYGNUS_PHY_PCIE1,
  15. MAX_NUM_PHYS,
  16. };
  17. struct cygnus_pcie_phy_core;
  18. /**
  19. * struct cygnus_pcie_phy - Cygnus PCIe PHY device
  20. * @core: pointer to the Cygnus PCIe PHY core control
  21. * @id: internal ID to identify the Cygnus PCIe PHY
  22. * @phy: pointer to the kernel PHY device
  23. */
  24. struct cygnus_pcie_phy {
  25. struct cygnus_pcie_phy_core *core;
  26. enum cygnus_pcie_phy_id id;
  27. struct phy *phy;
  28. };
  29. /**
  30. * struct cygnus_pcie_phy_core - Cygnus PCIe PHY core control
  31. * @dev: pointer to device
  32. * @base: base register
  33. * @lock: mutex to protect access to individual PHYs
  34. * @phys: pointer to Cygnus PHY device
  35. */
  36. struct cygnus_pcie_phy_core {
  37. struct device *dev;
  38. void __iomem *base;
  39. struct mutex lock;
  40. struct cygnus_pcie_phy phys[MAX_NUM_PHYS];
  41. };
  42. static int cygnus_pcie_power_config(struct cygnus_pcie_phy *phy, bool enable)
  43. {
  44. struct cygnus_pcie_phy_core *core = phy->core;
  45. unsigned shift;
  46. u32 val;
  47. mutex_lock(&core->lock);
  48. switch (phy->id) {
  49. case CYGNUS_PHY_PCIE0:
  50. shift = PCIE0_PHY_IDDQ_SHIFT;
  51. break;
  52. case CYGNUS_PHY_PCIE1:
  53. shift = PCIE1_PHY_IDDQ_SHIFT;
  54. break;
  55. default:
  56. mutex_unlock(&core->lock);
  57. dev_err(core->dev, "PCIe PHY %d invalid\n", phy->id);
  58. return -EINVAL;
  59. }
  60. if (enable) {
  61. val = readl(core->base + PCIE_CFG_OFFSET);
  62. val &= ~BIT(shift);
  63. writel(val, core->base + PCIE_CFG_OFFSET);
  64. /*
  65. * Wait 50 ms for the PCIe Serdes to stabilize after the analog
  66. * front end is brought up
  67. */
  68. msleep(50);
  69. } else {
  70. val = readl(core->base + PCIE_CFG_OFFSET);
  71. val |= BIT(shift);
  72. writel(val, core->base + PCIE_CFG_OFFSET);
  73. }
  74. mutex_unlock(&core->lock);
  75. dev_dbg(core->dev, "PCIe PHY %d %s\n", phy->id,
  76. enable ? "enabled" : "disabled");
  77. return 0;
  78. }
  79. static int cygnus_pcie_phy_power_on(struct phy *p)
  80. {
  81. struct cygnus_pcie_phy *phy = phy_get_drvdata(p);
  82. return cygnus_pcie_power_config(phy, true);
  83. }
  84. static int cygnus_pcie_phy_power_off(struct phy *p)
  85. {
  86. struct cygnus_pcie_phy *phy = phy_get_drvdata(p);
  87. return cygnus_pcie_power_config(phy, false);
  88. }
  89. static const struct phy_ops cygnus_pcie_phy_ops = {
  90. .power_on = cygnus_pcie_phy_power_on,
  91. .power_off = cygnus_pcie_phy_power_off,
  92. .owner = THIS_MODULE,
  93. };
  94. static int cygnus_pcie_phy_probe(struct platform_device *pdev)
  95. {
  96. struct device *dev = &pdev->dev;
  97. struct device_node *node = dev->of_node;
  98. struct cygnus_pcie_phy_core *core;
  99. struct phy_provider *provider;
  100. unsigned cnt = 0;
  101. if (of_get_child_count(node) == 0) {
  102. dev_err(dev, "PHY no child node\n");
  103. return -ENODEV;
  104. }
  105. core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
  106. if (!core)
  107. return -ENOMEM;
  108. core->dev = dev;
  109. core->base = devm_platform_ioremap_resource(pdev, 0);
  110. if (IS_ERR(core->base))
  111. return PTR_ERR(core->base);
  112. mutex_init(&core->lock);
  113. for_each_available_child_of_node_scoped(node, child) {
  114. unsigned int id;
  115. struct cygnus_pcie_phy *p;
  116. if (of_property_read_u32(child, "reg", &id)) {
  117. dev_err(dev, "missing reg property for %pOFn\n",
  118. child);
  119. return -EINVAL;
  120. }
  121. if (id >= MAX_NUM_PHYS) {
  122. dev_err(dev, "invalid PHY id: %u\n", id);
  123. return -EINVAL;
  124. }
  125. if (core->phys[id].phy) {
  126. dev_err(dev, "duplicated PHY id: %u\n", id);
  127. return -EINVAL;
  128. }
  129. p = &core->phys[id];
  130. p->phy = devm_phy_create(dev, child, &cygnus_pcie_phy_ops);
  131. if (IS_ERR(p->phy)) {
  132. dev_err(dev, "failed to create PHY\n");
  133. return PTR_ERR(p->phy);
  134. }
  135. p->core = core;
  136. p->id = id;
  137. phy_set_drvdata(p->phy, p);
  138. cnt++;
  139. }
  140. dev_set_drvdata(dev, core);
  141. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  142. if (IS_ERR(provider)) {
  143. dev_err(dev, "failed to register PHY provider\n");
  144. return PTR_ERR(provider);
  145. }
  146. dev_dbg(dev, "registered %u PCIe PHY(s)\n", cnt);
  147. return 0;
  148. }
  149. static const struct of_device_id cygnus_pcie_phy_match_table[] = {
  150. { .compatible = "brcm,cygnus-pcie-phy" },
  151. { /* sentinel */ }
  152. };
  153. MODULE_DEVICE_TABLE(of, cygnus_pcie_phy_match_table);
  154. static struct platform_driver cygnus_pcie_phy_driver = {
  155. .driver = {
  156. .name = "cygnus-pcie-phy",
  157. .of_match_table = cygnus_pcie_phy_match_table,
  158. },
  159. .probe = cygnus_pcie_phy_probe,
  160. };
  161. module_platform_driver(cygnus_pcie_phy_driver);
  162. MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
  163. MODULE_DESCRIPTION("Broadcom Cygnus PCIe PHY driver");
  164. MODULE_LICENSE("GPL v2");