phy-armada38x-comphy.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Russell King, Deep Blue Solutions Ltd.
  4. *
  5. * Partly derived from CP110 comphy driver by Antoine Tenart
  6. * <[email protected]>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/module.h>
  11. #include <linux/phy/phy.h>
  12. #include <linux/phy.h>
  13. #include <linux/platform_device.h>
  14. #define MAX_A38X_COMPHY 6
  15. #define MAX_A38X_PORTS 3
  16. #define COMPHY_CFG1 0x00
  17. #define COMPHY_CFG1_GEN_TX(x) ((x) << 26)
  18. #define COMPHY_CFG1_GEN_TX_MSK COMPHY_CFG1_GEN_TX(15)
  19. #define COMPHY_CFG1_GEN_RX(x) ((x) << 22)
  20. #define COMPHY_CFG1_GEN_RX_MSK COMPHY_CFG1_GEN_RX(15)
  21. #define GEN_SGMII_1_25GBPS 6
  22. #define GEN_SGMII_3_125GBPS 8
  23. #define COMPHY_STAT1 0x18
  24. #define COMPHY_STAT1_PLL_RDY_TX BIT(3)
  25. #define COMPHY_STAT1_PLL_RDY_RX BIT(2)
  26. #define COMPHY_SELECTOR 0xfc
  27. struct a38x_comphy;
  28. struct a38x_comphy_lane {
  29. void __iomem *base;
  30. struct a38x_comphy *priv;
  31. unsigned int n;
  32. int port;
  33. };
  34. struct a38x_comphy {
  35. void __iomem *base;
  36. void __iomem *conf;
  37. struct device *dev;
  38. struct a38x_comphy_lane lane[MAX_A38X_COMPHY];
  39. };
  40. static const u8 gbe_mux[MAX_A38X_COMPHY][MAX_A38X_PORTS] = {
  41. { 0, 0, 0 },
  42. { 4, 5, 0 },
  43. { 0, 4, 0 },
  44. { 0, 0, 4 },
  45. { 0, 3, 0 },
  46. { 0, 0, 3 },
  47. };
  48. static void a38x_set_conf(struct a38x_comphy_lane *lane, bool enable)
  49. {
  50. struct a38x_comphy *priv = lane->priv;
  51. u32 conf;
  52. if (priv->conf) {
  53. conf = readl_relaxed(priv->conf);
  54. if (enable)
  55. conf |= BIT(lane->port);
  56. else
  57. conf &= ~BIT(lane->port);
  58. writel(conf, priv->conf);
  59. }
  60. }
  61. static void a38x_comphy_set_reg(struct a38x_comphy_lane *lane,
  62. unsigned int offset, u32 mask, u32 value)
  63. {
  64. u32 val;
  65. val = readl_relaxed(lane->base + offset) & ~mask;
  66. writel(val | value, lane->base + offset);
  67. }
  68. static void a38x_comphy_set_speed(struct a38x_comphy_lane *lane,
  69. unsigned int gen_tx, unsigned int gen_rx)
  70. {
  71. a38x_comphy_set_reg(lane, COMPHY_CFG1,
  72. COMPHY_CFG1_GEN_TX_MSK | COMPHY_CFG1_GEN_RX_MSK,
  73. COMPHY_CFG1_GEN_TX(gen_tx) |
  74. COMPHY_CFG1_GEN_RX(gen_rx));
  75. }
  76. static int a38x_comphy_poll(struct a38x_comphy_lane *lane,
  77. unsigned int offset, u32 mask, u32 value)
  78. {
  79. u32 val;
  80. int ret;
  81. ret = readl_relaxed_poll_timeout_atomic(lane->base + offset, val,
  82. (val & mask) == value,
  83. 1000, 150000);
  84. if (ret)
  85. dev_err(lane->priv->dev,
  86. "comphy%u: timed out waiting for status\n", lane->n);
  87. return ret;
  88. }
  89. /*
  90. * We only support changing the speed for comphys configured for GBE.
  91. * Since that is all we do, we only poll for PLL ready status.
  92. */
  93. static int a38x_comphy_set_mode(struct phy *phy, enum phy_mode mode, int sub)
  94. {
  95. struct a38x_comphy_lane *lane = phy_get_drvdata(phy);
  96. unsigned int gen;
  97. int ret;
  98. if (mode != PHY_MODE_ETHERNET)
  99. return -EINVAL;
  100. switch (sub) {
  101. case PHY_INTERFACE_MODE_SGMII:
  102. case PHY_INTERFACE_MODE_1000BASEX:
  103. gen = GEN_SGMII_1_25GBPS;
  104. break;
  105. case PHY_INTERFACE_MODE_2500BASEX:
  106. gen = GEN_SGMII_3_125GBPS;
  107. break;
  108. default:
  109. return -EINVAL;
  110. }
  111. a38x_set_conf(lane, false);
  112. a38x_comphy_set_speed(lane, gen, gen);
  113. ret = a38x_comphy_poll(lane, COMPHY_STAT1,
  114. COMPHY_STAT1_PLL_RDY_TX |
  115. COMPHY_STAT1_PLL_RDY_RX,
  116. COMPHY_STAT1_PLL_RDY_TX |
  117. COMPHY_STAT1_PLL_RDY_RX);
  118. if (ret == 0)
  119. a38x_set_conf(lane, true);
  120. return ret;
  121. }
  122. static const struct phy_ops a38x_comphy_ops = {
  123. .set_mode = a38x_comphy_set_mode,
  124. .owner = THIS_MODULE,
  125. };
  126. static struct phy *a38x_comphy_xlate(struct device *dev,
  127. struct of_phandle_args *args)
  128. {
  129. struct a38x_comphy_lane *lane;
  130. struct phy *phy;
  131. u32 val;
  132. if (WARN_ON(args->args[0] >= MAX_A38X_PORTS))
  133. return ERR_PTR(-EINVAL);
  134. phy = of_phy_simple_xlate(dev, args);
  135. if (IS_ERR(phy))
  136. return phy;
  137. lane = phy_get_drvdata(phy);
  138. if (lane->port >= 0)
  139. return ERR_PTR(-EBUSY);
  140. lane->port = args->args[0];
  141. val = readl_relaxed(lane->priv->base + COMPHY_SELECTOR);
  142. val = (val >> (4 * lane->n)) & 0xf;
  143. if (!gbe_mux[lane->n][lane->port] ||
  144. val != gbe_mux[lane->n][lane->port]) {
  145. dev_warn(lane->priv->dev,
  146. "comphy%u: not configured for GBE\n", lane->n);
  147. phy = ERR_PTR(-EINVAL);
  148. }
  149. return phy;
  150. }
  151. static int a38x_comphy_probe(struct platform_device *pdev)
  152. {
  153. struct phy_provider *provider;
  154. struct device_node *child;
  155. struct a38x_comphy *priv;
  156. struct resource *res;
  157. void __iomem *base;
  158. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  159. if (!priv)
  160. return -ENOMEM;
  161. base = devm_platform_ioremap_resource(pdev, 0);
  162. if (IS_ERR(base))
  163. return PTR_ERR(base);
  164. priv->dev = &pdev->dev;
  165. priv->base = base;
  166. /* Optional */
  167. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "conf");
  168. if (res) {
  169. priv->conf = devm_ioremap_resource(&pdev->dev, res);
  170. if (IS_ERR(priv->conf))
  171. return PTR_ERR(priv->conf);
  172. }
  173. for_each_available_child_of_node(pdev->dev.of_node, child) {
  174. struct phy *phy;
  175. int ret;
  176. u32 val;
  177. ret = of_property_read_u32(child, "reg", &val);
  178. if (ret < 0) {
  179. dev_err(&pdev->dev, "missing 'reg' property (%d)\n",
  180. ret);
  181. continue;
  182. }
  183. if (val >= MAX_A38X_COMPHY || priv->lane[val].base) {
  184. dev_err(&pdev->dev, "invalid 'reg' property\n");
  185. continue;
  186. }
  187. phy = devm_phy_create(&pdev->dev, child, &a38x_comphy_ops);
  188. if (IS_ERR(phy)) {
  189. of_node_put(child);
  190. return PTR_ERR(phy);
  191. }
  192. priv->lane[val].base = base + 0x28 * val;
  193. priv->lane[val].priv = priv;
  194. priv->lane[val].n = val;
  195. priv->lane[val].port = -1;
  196. phy_set_drvdata(phy, &priv->lane[val]);
  197. }
  198. dev_set_drvdata(&pdev->dev, priv);
  199. provider = devm_of_phy_provider_register(&pdev->dev, a38x_comphy_xlate);
  200. return PTR_ERR_OR_ZERO(provider);
  201. }
  202. static const struct of_device_id a38x_comphy_of_match_table[] = {
  203. { .compatible = "marvell,armada-380-comphy" },
  204. { },
  205. };
  206. MODULE_DEVICE_TABLE(of, a38x_comphy_of_match_table);
  207. static struct platform_driver a38x_comphy_driver = {
  208. .probe = a38x_comphy_probe,
  209. .driver = {
  210. .name = "armada-38x-comphy",
  211. .of_match_table = a38x_comphy_of_match_table,
  212. },
  213. };
  214. module_platform_driver(a38x_comphy_driver);
  215. MODULE_AUTHOR("Russell King <[email protected]>");
  216. MODULE_DESCRIPTION("Common PHY driver for Armada 38x SoCs");
  217. MODULE_LICENSE("GPL v2");