ethernet-mdio.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_nway_reset(struct net_device *dev)
  32. {
  33. if (!capable(CAP_NET_ADMIN))
  34. return -EPERM;
  35. if (dev->phydev)
  36. return phy_start_aneg(dev->phydev);
  37. return -EINVAL;
  38. }
  39. const struct ethtool_ops cvm_oct_ethtool_ops = {
  40. .get_drvinfo = cvm_oct_get_drvinfo,
  41. .nway_reset = cvm_oct_nway_reset,
  42. .get_link = ethtool_op_get_link,
  43. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  44. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  45. };
  46. /**
  47. * cvm_oct_ioctl - IOCTL support for PHY control
  48. * @dev: Device to change
  49. * @rq: the request
  50. * @cmd: the command
  51. *
  52. * Returns Zero on success
  53. */
  54. int cvm_oct_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  55. {
  56. if (!netif_running(dev))
  57. return -EINVAL;
  58. if (!dev->phydev)
  59. return -EINVAL;
  60. return phy_mii_ioctl(dev->phydev, rq, cmd);
  61. }
  62. void cvm_oct_note_carrier(struct octeon_ethernet *priv,
  63. cvmx_helper_link_info_t li)
  64. {
  65. if (li.s.link_up) {
  66. pr_notice_ratelimited("%s: %u Mbps %s duplex, port %d, queue %d\n",
  67. netdev_name(priv->netdev), li.s.speed,
  68. (li.s.full_duplex) ? "Full" : "Half",
  69. priv->port, priv->queue);
  70. } else {
  71. pr_notice_ratelimited("%s: Link down\n",
  72. netdev_name(priv->netdev));
  73. }
  74. }
  75. void cvm_oct_adjust_link(struct net_device *dev)
  76. {
  77. struct octeon_ethernet *priv = netdev_priv(dev);
  78. cvmx_helper_link_info_t link_info;
  79. link_info.u64 = 0;
  80. link_info.s.link_up = dev->phydev->link ? 1 : 0;
  81. link_info.s.full_duplex = dev->phydev->duplex ? 1 : 0;
  82. link_info.s.speed = dev->phydev->speed;
  83. priv->link_info = link_info.u64;
  84. /*
  85. * The polling task need to know about link status changes.
  86. */
  87. if (priv->poll)
  88. priv->poll(dev);
  89. if (priv->last_link != dev->phydev->link) {
  90. priv->last_link = dev->phydev->link;
  91. cvmx_helper_link_set(priv->port, link_info);
  92. cvm_oct_note_carrier(priv, link_info);
  93. }
  94. }
  95. int cvm_oct_common_stop(struct net_device *dev)
  96. {
  97. struct octeon_ethernet *priv = netdev_priv(dev);
  98. int interface = INTERFACE(priv->port);
  99. cvmx_helper_link_info_t link_info;
  100. union cvmx_gmxx_prtx_cfg gmx_cfg;
  101. int index = INDEX(priv->port);
  102. gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
  103. gmx_cfg.s.en = 0;
  104. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
  105. priv->poll = NULL;
  106. if (dev->phydev)
  107. phy_disconnect(dev->phydev);
  108. if (priv->last_link) {
  109. link_info.u64 = 0;
  110. priv->last_link = 0;
  111. cvmx_helper_link_set(priv->port, link_info);
  112. cvm_oct_note_carrier(priv, link_info);
  113. }
  114. return 0;
  115. }
  116. /**
  117. * cvm_oct_phy_setup_device - setup the PHY
  118. *
  119. * @dev: Device to setup
  120. *
  121. * Returns Zero on success, negative on failure
  122. */
  123. int cvm_oct_phy_setup_device(struct net_device *dev)
  124. {
  125. struct octeon_ethernet *priv = netdev_priv(dev);
  126. struct device_node *phy_node;
  127. struct phy_device *phydev = NULL;
  128. if (!priv->of_node)
  129. goto no_phy;
  130. phy_node = of_parse_phandle(priv->of_node, "phy-handle", 0);
  131. if (!phy_node && of_phy_is_fixed_link(priv->of_node)) {
  132. int rc;
  133. rc = of_phy_register_fixed_link(priv->of_node);
  134. if (rc)
  135. return rc;
  136. phy_node = of_node_get(priv->of_node);
  137. }
  138. if (!phy_node)
  139. goto no_phy;
  140. phydev = of_phy_connect(dev, phy_node, cvm_oct_adjust_link, 0,
  141. PHY_INTERFACE_MODE_GMII);
  142. of_node_put(phy_node);
  143. if (!phydev)
  144. return -ENODEV;
  145. priv->last_link = 0;
  146. phy_start_aneg(phydev);
  147. return 0;
  148. no_phy:
  149. /* If there is no phy, assume a direct MAC connection and that
  150. * the link is up.
  151. */
  152. netif_carrier_on(dev);
  153. return 0;
  154. }