ethernet-mdio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**********************************************************************
  2. * Author: Cavium Networks
  3. *
  4. * Contact: support@caviumnetworks.com
  5. * This file is part of the OCTEON SDK
  6. *
  7. * Copyright (c) 2003-2007 Cavium Networks
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this file; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. * or visit http://www.gnu.org/licenses/.
  23. *
  24. * This file may also be available under a different license from Cavium.
  25. * Contact Cavium Networks for more information
  26. **********************************************************************/
  27. #include <linux/kernel.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/phy.h>
  30. #include <linux/ratelimit.h>
  31. #include <linux/of_mdio.h>
  32. #include <net/dst.h>
  33. #include <asm/octeon/octeon.h>
  34. #include "ethernet-defines.h"
  35. #include "octeon-ethernet.h"
  36. #include "ethernet-mdio.h"
  37. #include "ethernet-util.h"
  38. #include <asm/octeon/cvmx-helper-board.h>
  39. #include <asm/octeon/cvmx-smix-defs.h>
  40. static void cvm_oct_get_drvinfo(struct net_device *dev,
  41. struct ethtool_drvinfo *info)
  42. {
  43. strlcpy(info->driver, "cavium-ethernet", sizeof(info->driver));
  44. strlcpy(info->version, OCTEON_ETHERNET_VERSION, sizeof(info->version));
  45. strlcpy(info->bus_info, "Builtin", sizeof(info->bus_info));
  46. }
  47. static int cvm_oct_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  48. {
  49. struct octeon_ethernet *priv = netdev_priv(dev);
  50. if (priv->phydev)
  51. return phy_ethtool_gset(priv->phydev, cmd);
  52. return -EINVAL;
  53. }
  54. static int cvm_oct_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  55. {
  56. struct octeon_ethernet *priv = netdev_priv(dev);
  57. if (!capable(CAP_NET_ADMIN))
  58. return -EPERM;
  59. if (priv->phydev)
  60. return phy_ethtool_sset(priv->phydev, cmd);
  61. return -EINVAL;
  62. }
  63. static int cvm_oct_nway_reset(struct net_device *dev)
  64. {
  65. struct octeon_ethernet *priv = netdev_priv(dev);
  66. if (!capable(CAP_NET_ADMIN))
  67. return -EPERM;
  68. if (priv->phydev)
  69. return phy_start_aneg(priv->phydev);
  70. return -EINVAL;
  71. }
  72. const struct ethtool_ops cvm_oct_ethtool_ops = {
  73. .get_drvinfo = cvm_oct_get_drvinfo,
  74. .get_settings = cvm_oct_get_settings,
  75. .set_settings = cvm_oct_set_settings,
  76. .nway_reset = cvm_oct_nway_reset,
  77. .get_link = ethtool_op_get_link,
  78. };
  79. /**
  80. * cvm_oct_ioctl - IOCTL support for PHY control
  81. * @dev: Device to change
  82. * @rq: the request
  83. * @cmd: the command
  84. *
  85. * Returns Zero on success
  86. */
  87. int cvm_oct_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  88. {
  89. struct octeon_ethernet *priv = netdev_priv(dev);
  90. if (!netif_running(dev))
  91. return -EINVAL;
  92. if (!priv->phydev)
  93. return -EINVAL;
  94. return phy_mii_ioctl(priv->phydev, rq, cmd);
  95. }
  96. static void cvm_oct_note_carrier(struct octeon_ethernet *priv,
  97. cvmx_helper_link_info_t li)
  98. {
  99. if (li.s.link_up) {
  100. pr_notice_ratelimited("%s: %u Mbps %s duplex, port %d\n",
  101. netdev_name(priv->netdev), li.s.speed,
  102. (li.s.full_duplex) ? "Full" : "Half",
  103. priv->port);
  104. } else {
  105. pr_notice_ratelimited("%s: Link down\n",
  106. netdev_name(priv->netdev));
  107. }
  108. }
  109. void cvm_oct_set_carrier(struct octeon_ethernet *priv,
  110. cvmx_helper_link_info_t link_info)
  111. {
  112. cvm_oct_note_carrier(priv, link_info);
  113. if (link_info.s.link_up) {
  114. if (!netif_carrier_ok(priv->netdev))
  115. netif_carrier_on(priv->netdev);
  116. } else {
  117. if (netif_carrier_ok(priv->netdev))
  118. netif_carrier_off(priv->netdev);
  119. }
  120. }
  121. void cvm_oct_adjust_link(struct net_device *dev)
  122. {
  123. struct octeon_ethernet *priv = netdev_priv(dev);
  124. cvmx_helper_link_info_t link_info;
  125. if (priv->last_link != priv->phydev->link) {
  126. priv->last_link = priv->phydev->link;
  127. link_info.u64 = 0;
  128. link_info.s.link_up = priv->last_link ? 1 : 0;
  129. link_info.s.full_duplex = priv->phydev->duplex ? 1 : 0;
  130. link_info.s.speed = priv->phydev->speed;
  131. cvmx_helper_link_set(priv->port, link_info);
  132. cvm_oct_note_carrier(priv, link_info);
  133. }
  134. }
  135. int cvm_oct_common_stop(struct net_device *dev)
  136. {
  137. struct octeon_ethernet *priv = netdev_priv(dev);
  138. cvmx_helper_link_info_t link_info;
  139. priv->poll = NULL;
  140. if (priv->phydev)
  141. phy_disconnect(priv->phydev);
  142. priv->phydev = NULL;
  143. if (priv->last_link) {
  144. link_info.u64 = 0;
  145. priv->last_link = 0;
  146. cvmx_helper_link_set(priv->port, link_info);
  147. cvm_oct_note_carrier(priv, link_info);
  148. }
  149. return 0;
  150. }
  151. /**
  152. * cvm_oct_phy_setup_device - setup the PHY
  153. *
  154. * @dev: Device to setup
  155. *
  156. * Returns Zero on success, negative on failure
  157. */
  158. int cvm_oct_phy_setup_device(struct net_device *dev)
  159. {
  160. struct octeon_ethernet *priv = netdev_priv(dev);
  161. struct device_node *phy_node;
  162. if (!priv->of_node)
  163. goto no_phy;
  164. phy_node = of_parse_phandle(priv->of_node, "phy-handle", 0);
  165. if (!phy_node)
  166. goto no_phy;
  167. priv->phydev = of_phy_connect(dev, phy_node, cvm_oct_adjust_link, 0,
  168. PHY_INTERFACE_MODE_GMII);
  169. if (priv->phydev == NULL)
  170. return -ENODEV;
  171. priv->last_link = 0;
  172. phy_start_aneg(priv->phydev);
  173. return 0;
  174. no_phy:
  175. /* If there is no phy, assume a direct MAC connection and that
  176. * the link is up.
  177. */
  178. netif_carrier_on(dev);
  179. return 0;
  180. }