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