ethernet-mdio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * This file is based on code from OCTEON SDK by Cavium Networks.
  3. *
  4. * Copyright (c) 2003-2007 Cavium Networks
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, Version 2, as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/ethtool.h>
  12. #include <linux/phy.h>
  13. #include <linux/ratelimit.h>
  14. #include <linux/of_mdio.h>
  15. #include <generated/utsrelease.h>
  16. #include <net/dst.h>
  17. #include <asm/octeon/octeon.h>
  18. #include "ethernet-defines.h"
  19. #include "octeon-ethernet.h"
  20. #include "ethernet-mdio.h"
  21. #include "ethernet-util.h"
  22. #include <asm/octeon/cvmx-gmxx-defs.h>
  23. #include <asm/octeon/cvmx-smix-defs.h>
  24. static void cvm_oct_get_drvinfo(struct net_device *dev,
  25. struct ethtool_drvinfo *info)
  26. {
  27. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  28. strlcpy(info->version, UTS_RELEASE, sizeof(info->version));
  29. strlcpy(info->bus_info, "Builtin", sizeof(info->bus_info));
  30. }
  31. static int cvm_oct_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  32. {
  33. struct octeon_ethernet *priv = netdev_priv(dev);
  34. if (priv->phydev)
  35. return phy_ethtool_gset(priv->phydev, cmd);
  36. return -EINVAL;
  37. }
  38. static int cvm_oct_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  39. {
  40. struct octeon_ethernet *priv = netdev_priv(dev);
  41. if (!capable(CAP_NET_ADMIN))
  42. return -EPERM;
  43. if (priv->phydev)
  44. return phy_ethtool_sset(priv->phydev, cmd);
  45. return -EINVAL;
  46. }
  47. static int cvm_oct_nway_reset(struct net_device *dev)
  48. {
  49. struct octeon_ethernet *priv = netdev_priv(dev);
  50. if (!capable(CAP_NET_ADMIN))
  51. return -EPERM;
  52. if (priv->phydev)
  53. return phy_start_aneg(priv->phydev);
  54. return -EINVAL;
  55. }
  56. const struct ethtool_ops cvm_oct_ethtool_ops = {
  57. .get_drvinfo = cvm_oct_get_drvinfo,
  58. .get_settings = cvm_oct_get_settings,
  59. .set_settings = cvm_oct_set_settings,
  60. .nway_reset = cvm_oct_nway_reset,
  61. .get_link = ethtool_op_get_link,
  62. };
  63. /**
  64. * cvm_oct_ioctl - IOCTL support for PHY control
  65. * @dev: Device to change
  66. * @rq: the request
  67. * @cmd: the command
  68. *
  69. * Returns Zero on success
  70. */
  71. int cvm_oct_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  72. {
  73. struct octeon_ethernet *priv = netdev_priv(dev);
  74. if (!netif_running(dev))
  75. return -EINVAL;
  76. if (!priv->phydev)
  77. return -EINVAL;
  78. return phy_mii_ioctl(priv->phydev, rq, cmd);
  79. }
  80. void cvm_oct_note_carrier(struct octeon_ethernet *priv,
  81. cvmx_helper_link_info_t li)
  82. {
  83. if (li.s.link_up) {
  84. pr_notice_ratelimited("%s: %u Mbps %s duplex, port %d, queue %d\n",
  85. netdev_name(priv->netdev), li.s.speed,
  86. (li.s.full_duplex) ? "Full" : "Half",
  87. priv->port, priv->queue);
  88. } else {
  89. pr_notice_ratelimited("%s: Link down\n",
  90. netdev_name(priv->netdev));
  91. }
  92. }
  93. void cvm_oct_adjust_link(struct net_device *dev)
  94. {
  95. struct octeon_ethernet *priv = netdev_priv(dev);
  96. cvmx_helper_link_info_t link_info;
  97. if (priv->last_link != priv->phydev->link) {
  98. priv->last_link = priv->phydev->link;
  99. link_info.u64 = 0;
  100. link_info.s.link_up = priv->last_link ? 1 : 0;
  101. link_info.s.full_duplex = priv->phydev->duplex ? 1 : 0;
  102. link_info.s.speed = priv->phydev->speed;
  103. cvmx_helper_link_set(priv->port, link_info);
  104. cvm_oct_note_carrier(priv, link_info);
  105. }
  106. }
  107. int cvm_oct_common_stop(struct net_device *dev)
  108. {
  109. struct octeon_ethernet *priv = netdev_priv(dev);
  110. int interface = INTERFACE(priv->port);
  111. cvmx_helper_link_info_t link_info;
  112. union cvmx_gmxx_prtx_cfg gmx_cfg;
  113. int index = INDEX(priv->port);
  114. gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
  115. gmx_cfg.s.en = 0;
  116. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
  117. priv->poll = NULL;
  118. if (priv->phydev)
  119. phy_disconnect(priv->phydev);
  120. priv->phydev = NULL;
  121. if (priv->last_link) {
  122. link_info.u64 = 0;
  123. priv->last_link = 0;
  124. cvmx_helper_link_set(priv->port, link_info);
  125. cvm_oct_note_carrier(priv, link_info);
  126. }
  127. return 0;
  128. }
  129. /**
  130. * cvm_oct_phy_setup_device - setup the PHY
  131. *
  132. * @dev: Device to setup
  133. *
  134. * Returns Zero on success, negative on failure
  135. */
  136. int cvm_oct_phy_setup_device(struct net_device *dev)
  137. {
  138. struct octeon_ethernet *priv = netdev_priv(dev);
  139. struct device_node *phy_node;
  140. if (!priv->of_node)
  141. goto no_phy;
  142. phy_node = of_parse_phandle(priv->of_node, "phy-handle", 0);
  143. if (!phy_node)
  144. goto no_phy;
  145. priv->phydev = of_phy_connect(dev, phy_node, cvm_oct_adjust_link, 0,
  146. PHY_INTERFACE_MODE_GMII);
  147. if (priv->phydev == NULL)
  148. return -ENODEV;
  149. priv->last_link = 0;
  150. phy_start_aneg(priv->phydev);
  151. return 0;
  152. no_phy:
  153. /* If there is no phy, assume a direct MAC connection and that
  154. * the link is up.
  155. */
  156. netif_carrier_on(dev);
  157. return 0;
  158. }