031-mvneta-implement-ethtool-autonegotiation-control.patch 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. From: Stas Sergeev <stsp@list.ru>
  2. Date: Wed, 2 Dec 2015 20:35:11 +0300
  3. Subject: [PATCH] mvneta: implement ethtool autonegotiation control
  4. This patch allows to do
  5. ethtool -s eth0 autoneg off
  6. ethtool -s eth0 autoneg on
  7. to disable or enable autonegotiation at run-time.
  8. Without that functionality, the only way to control the autonegotiation
  9. is to modify the device tree.
  10. This is needed if you plan to use the same kernel with
  11. different ethernet switches, the ones that support the in-band
  12. status and the ones that not.
  13. CC: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  14. CC: netdev@vger.kernel.org
  15. CC: linux-kernel@vger.kernel.org
  16. Signed-off-by: Stas Sergeev <stsp@users.sourceforge.net>
  17. Signed-off-by: David S. Miller <davem@davemloft.net>
  18. ---
  19. --- a/drivers/net/ethernet/marvell/mvneta.c
  20. +++ b/drivers/net/ethernet/marvell/mvneta.c
  21. @@ -371,7 +371,7 @@ struct mvneta_port {
  22. unsigned int duplex;
  23. unsigned int speed;
  24. unsigned int tx_csum_limit;
  25. - int use_inband_status:1;
  26. + unsigned int use_inband_status:1;
  27. u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
  28. };
  29. @@ -973,6 +973,44 @@ static void mvneta_set_other_mcast_table
  30. mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
  31. }
  32. +static void mvneta_set_autoneg(struct mvneta_port *pp, int enable)
  33. +{
  34. + u32 val;
  35. +
  36. + if (enable) {
  37. + val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  38. + val &= ~(MVNETA_GMAC_FORCE_LINK_PASS |
  39. + MVNETA_GMAC_FORCE_LINK_DOWN |
  40. + MVNETA_GMAC_AN_FLOW_CTRL_EN);
  41. + val |= MVNETA_GMAC_INBAND_AN_ENABLE |
  42. + MVNETA_GMAC_AN_SPEED_EN |
  43. + MVNETA_GMAC_AN_DUPLEX_EN;
  44. + mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  45. +
  46. + val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
  47. + val |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
  48. + mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
  49. +
  50. + val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
  51. + val |= MVNETA_GMAC2_INBAND_AN_ENABLE;
  52. + mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
  53. + } else {
  54. + val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  55. + val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE |
  56. + MVNETA_GMAC_AN_SPEED_EN |
  57. + MVNETA_GMAC_AN_DUPLEX_EN);
  58. + mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  59. +
  60. + val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
  61. + val &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
  62. + mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
  63. +
  64. + val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
  65. + val &= ~MVNETA_GMAC2_INBAND_AN_ENABLE;
  66. + mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
  67. + }
  68. +}
  69. +
  70. /* This method sets defaults to the NETA port:
  71. * Clears interrupt Cause and Mask registers.
  72. * Clears all MAC tables.
  73. @@ -1058,39 +1096,7 @@ static void mvneta_defaults_set(struct m
  74. val &= ~MVNETA_PHY_POLLING_ENABLE;
  75. mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
  76. - if (pp->use_inband_status) {
  77. - val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  78. - val &= ~(MVNETA_GMAC_FORCE_LINK_PASS |
  79. - MVNETA_GMAC_FORCE_LINK_DOWN |
  80. - MVNETA_GMAC_AN_FLOW_CTRL_EN);
  81. - val |= MVNETA_GMAC_INBAND_AN_ENABLE |
  82. - MVNETA_GMAC_AN_SPEED_EN |
  83. - MVNETA_GMAC_AN_DUPLEX_EN;
  84. - mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  85. -
  86. - val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
  87. - val |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
  88. - mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
  89. -
  90. - val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
  91. - val |= MVNETA_GMAC2_INBAND_AN_ENABLE;
  92. - mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
  93. - } else {
  94. - val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  95. - val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE |
  96. - MVNETA_GMAC_AN_SPEED_EN |
  97. - MVNETA_GMAC_AN_DUPLEX_EN);
  98. - mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  99. -
  100. - val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
  101. - val &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
  102. - mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
  103. -
  104. - val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
  105. - val &= ~MVNETA_GMAC2_INBAND_AN_ENABLE;
  106. - mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
  107. - }
  108. -
  109. + mvneta_set_autoneg(pp, pp->use_inband_status);
  110. mvneta_set_ucast_table(pp, -1);
  111. mvneta_set_special_mcast_table(pp, -1);
  112. mvneta_set_other_mcast_table(pp, -1);
  113. @@ -2956,10 +2962,43 @@ int mvneta_ethtool_get_settings(struct n
  114. int mvneta_ethtool_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  115. {
  116. struct mvneta_port *pp = netdev_priv(dev);
  117. + struct phy_device *phydev = pp->phy_dev;
  118. - if (!pp->phy_dev)
  119. + if (!phydev)
  120. return -ENODEV;
  121. + if ((cmd->autoneg == AUTONEG_ENABLE) != pp->use_inband_status) {
  122. + u32 val;
  123. +
  124. + mvneta_set_autoneg(pp, cmd->autoneg == AUTONEG_ENABLE);
  125. +
  126. + if (cmd->autoneg == AUTONEG_DISABLE) {
  127. + val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  128. + val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
  129. + MVNETA_GMAC_CONFIG_GMII_SPEED |
  130. + MVNETA_GMAC_CONFIG_FULL_DUPLEX);
  131. +
  132. + if (phydev->duplex)
  133. + val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
  134. +
  135. + if (phydev->speed == SPEED_1000)
  136. + val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
  137. + else if (phydev->speed == SPEED_100)
  138. + val |= MVNETA_GMAC_CONFIG_MII_SPEED;
  139. +
  140. + mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  141. + }
  142. +
  143. + pp->use_inband_status = (cmd->autoneg == AUTONEG_ENABLE);
  144. + netdev_info(pp->dev, "autoneg status set to %i\n",
  145. + pp->use_inband_status);
  146. +
  147. + if (netif_running(dev)) {
  148. + mvneta_port_down(pp);
  149. + mvneta_port_up(pp);
  150. + }
  151. + }
  152. +
  153. return phy_ethtool_sset(pp->phy_dev, cmd);
  154. }