ethernet-mdio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <net/dst.h>
  31. #include <asm/octeon/octeon.h>
  32. #include "ethernet-defines.h"
  33. #include "octeon-ethernet.h"
  34. #include "ethernet-mdio.h"
  35. #include "ethernet-util.h"
  36. #include "cvmx-helper-board.h"
  37. #include "cvmx-smix-defs.h"
  38. static void cvm_oct_get_drvinfo(struct net_device *dev,
  39. struct ethtool_drvinfo *info)
  40. {
  41. strcpy(info->driver, "cavium-ethernet");
  42. strcpy(info->version, OCTEON_ETHERNET_VERSION);
  43. strcpy(info->bus_info, "Builtin");
  44. }
  45. static int cvm_oct_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  46. {
  47. struct octeon_ethernet *priv = netdev_priv(dev);
  48. if (priv->phydev)
  49. return phy_ethtool_gset(priv->phydev, cmd);
  50. return -EINVAL;
  51. }
  52. static int cvm_oct_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  53. {
  54. struct octeon_ethernet *priv = netdev_priv(dev);
  55. if (!capable(CAP_NET_ADMIN))
  56. return -EPERM;
  57. if (priv->phydev)
  58. return phy_ethtool_sset(priv->phydev, cmd);
  59. return -EINVAL;
  60. }
  61. static int cvm_oct_nway_reset(struct net_device *dev)
  62. {
  63. struct octeon_ethernet *priv = netdev_priv(dev);
  64. if (!capable(CAP_NET_ADMIN))
  65. return -EPERM;
  66. if (priv->phydev)
  67. return phy_start_aneg(priv->phydev);
  68. return -EINVAL;
  69. }
  70. const struct ethtool_ops cvm_oct_ethtool_ops = {
  71. .get_drvinfo = cvm_oct_get_drvinfo,
  72. .get_settings = cvm_oct_get_settings,
  73. .set_settings = cvm_oct_set_settings,
  74. .nway_reset = cvm_oct_nway_reset,
  75. .get_link = ethtool_op_get_link,
  76. };
  77. /**
  78. * cvm_oct_ioctl - IOCTL support for PHY control
  79. * @dev: Device to change
  80. * @rq: the request
  81. * @cmd: the command
  82. *
  83. * Returns Zero on success
  84. */
  85. int cvm_oct_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  86. {
  87. struct octeon_ethernet *priv = netdev_priv(dev);
  88. if (!netif_running(dev))
  89. return -EINVAL;
  90. if (!priv->phydev)
  91. return -EINVAL;
  92. return phy_mii_ioctl(priv->phydev, rq, cmd);
  93. }
  94. static void cvm_oct_adjust_link(struct net_device *dev)
  95. {
  96. struct octeon_ethernet *priv = netdev_priv(dev);
  97. cvmx_helper_link_info_t link_info;
  98. if (priv->last_link != priv->phydev->link) {
  99. priv->last_link = priv->phydev->link;
  100. link_info.u64 = 0;
  101. link_info.s.link_up = priv->last_link ? 1 : 0;
  102. link_info.s.full_duplex = priv->phydev->duplex ? 1 : 0;
  103. link_info.s.speed = priv->phydev->speed;
  104. cvmx_helper_link_set( priv->port, link_info);
  105. if (priv->last_link) {
  106. netif_carrier_on(dev);
  107. if (priv->queue != -1)
  108. DEBUGPRINT("%s: %u Mbps %s duplex, "
  109. "port %2d, queue %2d\n",
  110. dev->name, priv->phydev->speed,
  111. priv->phydev->duplex ?
  112. "Full" : "Half",
  113. priv->port, priv->queue);
  114. else
  115. DEBUGPRINT("%s: %u Mbps %s duplex, "
  116. "port %2d, POW\n",
  117. dev->name, priv->phydev->speed,
  118. priv->phydev->duplex ?
  119. "Full" : "Half",
  120. priv->port);
  121. } else {
  122. netif_carrier_off(dev);
  123. DEBUGPRINT("%s: Link down\n", dev->name);
  124. }
  125. }
  126. }
  127. /**
  128. * cvm_oct_phy_setup_device - setup the PHY
  129. *
  130. * @dev: Device to setup
  131. *
  132. * Returns Zero on success, negative on failure
  133. */
  134. int cvm_oct_phy_setup_device(struct net_device *dev)
  135. {
  136. struct octeon_ethernet *priv = netdev_priv(dev);
  137. int phy_addr = cvmx_helper_board_get_mii_address(priv->port);
  138. if (phy_addr != -1) {
  139. char phy_id[20];
  140. snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT, "0", phy_addr);
  141. priv->phydev = phy_connect(dev, phy_id, cvm_oct_adjust_link, 0,
  142. PHY_INTERFACE_MODE_GMII);
  143. if (IS_ERR(priv->phydev)) {
  144. priv->phydev = NULL;
  145. return -1;
  146. }
  147. priv->last_link = 0;
  148. phy_start_aneg(priv->phydev);
  149. }
  150. return 0;
  151. }