phy-armada38x-comphy.c 6.2 KB

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