phy-bcm-cygnus-pcie.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, *child;
  98. struct cygnus_pcie_phy_core *core;
  99. struct phy_provider *provider;
  100. unsigned cnt = 0;
  101. int ret;
  102. if (of_get_child_count(node) == 0) {
  103. dev_err(dev, "PHY no child node\n");
  104. return -ENODEV;
  105. }
  106. core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
  107. if (!core)
  108. return -ENOMEM;
  109. core->dev = dev;
  110. core->base = devm_platform_ioremap_resource(pdev, 0);
  111. if (IS_ERR(core->base))
  112. return PTR_ERR(core->base);
  113. mutex_init(&core->lock);
  114. for_each_available_child_of_node(node, child) {
  115. unsigned int id;
  116. struct cygnus_pcie_phy *p;
  117. if (of_property_read_u32(child, "reg", &id)) {
  118. dev_err(dev, "missing reg property for %pOFn\n",
  119. child);
  120. ret = -EINVAL;
  121. goto put_child;
  122. }
  123. if (id >= MAX_NUM_PHYS) {
  124. dev_err(dev, "invalid PHY id: %u\n", id);
  125. ret = -EINVAL;
  126. goto put_child;
  127. }
  128. if (core->phys[id].phy) {
  129. dev_err(dev, "duplicated PHY id: %u\n", id);
  130. ret = -EINVAL;
  131. goto put_child;
  132. }
  133. p = &core->phys[id];
  134. p->phy = devm_phy_create(dev, child, &cygnus_pcie_phy_ops);
  135. if (IS_ERR(p->phy)) {
  136. dev_err(dev, "failed to create PHY\n");
  137. ret = PTR_ERR(p->phy);
  138. goto put_child;
  139. }
  140. p->core = core;
  141. p->id = id;
  142. phy_set_drvdata(p->phy, p);
  143. cnt++;
  144. }
  145. dev_set_drvdata(dev, core);
  146. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  147. if (IS_ERR(provider)) {
  148. dev_err(dev, "failed to register PHY provider\n");
  149. return PTR_ERR(provider);
  150. }
  151. dev_dbg(dev, "registered %u PCIe PHY(s)\n", cnt);
  152. return 0;
  153. put_child:
  154. of_node_put(child);
  155. return ret;
  156. }
  157. static const struct of_device_id cygnus_pcie_phy_match_table[] = {
  158. { .compatible = "brcm,cygnus-pcie-phy" },
  159. { /* sentinel */ }
  160. };
  161. MODULE_DEVICE_TABLE(of, cygnus_pcie_phy_match_table);
  162. static struct platform_driver cygnus_pcie_phy_driver = {
  163. .driver = {
  164. .name = "cygnus-pcie-phy",
  165. .of_match_table = cygnus_pcie_phy_match_table,
  166. },
  167. .probe = cygnus_pcie_phy_probe,
  168. };
  169. module_platform_driver(cygnus_pcie_phy_driver);
  170. MODULE_AUTHOR("Ray Jui <[email protected]>");
  171. MODULE_DESCRIPTION("Broadcom Cygnus PCIe PHY driver");
  172. MODULE_LICENSE("GPL v2");