Linux 6.13-rc4
[linux.git] / drivers / staging / octeon / ethernet-mdio.c
blob211423059e303f26d43b08ab06158b5fe331c283
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * This file is based on code from OCTEON SDK by Cavium Networks.
5 * Copyright (c) 2003-2007 Cavium Networks
6 */
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 <net/dst.h>
15 #include "octeon-ethernet.h"
16 #include "ethernet-defines.h"
17 #include "ethernet-mdio.h"
18 #include "ethernet-util.h"
20 static void cvm_oct_get_drvinfo(struct net_device *dev,
21 struct ethtool_drvinfo *info)
23 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
24 strscpy(info->bus_info, "Builtin", sizeof(info->bus_info));
27 static int cvm_oct_nway_reset(struct net_device *dev)
29 if (!capable(CAP_NET_ADMIN))
30 return -EPERM;
32 if (dev->phydev)
33 return phy_start_aneg(dev->phydev);
35 return -EINVAL;
38 const struct ethtool_ops cvm_oct_ethtool_ops = {
39 .get_drvinfo = cvm_oct_get_drvinfo,
40 .nway_reset = cvm_oct_nway_reset,
41 .get_link = ethtool_op_get_link,
42 .get_link_ksettings = phy_ethtool_get_link_ksettings,
43 .set_link_ksettings = phy_ethtool_set_link_ksettings,
46 /**
47 * cvm_oct_ioctl - IOCTL support for PHY control
48 * @dev: Device to change
49 * @rq: the request
50 * @cmd: the command
52 * Returns Zero on success
54 int cvm_oct_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
56 if (!netif_running(dev))
57 return -EINVAL;
59 if (!dev->phydev)
60 return -EINVAL;
62 return phy_mii_ioctl(dev->phydev, rq, cmd);
65 void cvm_oct_note_carrier(struct octeon_ethernet *priv,
66 union cvmx_helper_link_info li)
68 if (li.s.link_up) {
69 pr_notice_ratelimited("%s: %u Mbps %s duplex, port %d, queue %d\n",
70 netdev_name(priv->netdev), li.s.speed,
71 (li.s.full_duplex) ? "Full" : "Half",
72 priv->port, priv->queue);
73 } else {
74 pr_notice_ratelimited("%s: Link down\n",
75 netdev_name(priv->netdev));
79 void cvm_oct_adjust_link(struct net_device *dev)
81 struct octeon_ethernet *priv = netdev_priv(dev);
82 union cvmx_helper_link_info link_info;
84 link_info.u64 = 0;
85 link_info.s.link_up = dev->phydev->link ? 1 : 0;
86 link_info.s.full_duplex = dev->phydev->duplex ? 1 : 0;
87 link_info.s.speed = dev->phydev->speed;
88 priv->link_info = link_info.u64;
91 * The polling task need to know about link status changes.
93 if (priv->poll)
94 priv->poll(dev);
96 if (priv->last_link != dev->phydev->link) {
97 priv->last_link = dev->phydev->link;
98 cvmx_helper_link_set(priv->port, link_info);
99 cvm_oct_note_carrier(priv, link_info);
103 int cvm_oct_common_stop(struct net_device *dev)
105 struct octeon_ethernet *priv = netdev_priv(dev);
106 int interface = INTERFACE(priv->port);
107 union cvmx_helper_link_info link_info;
108 union cvmx_gmxx_prtx_cfg gmx_cfg;
109 int index = INDEX(priv->port);
111 gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
112 gmx_cfg.s.en = 0;
113 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
115 priv->poll = NULL;
117 if (dev->phydev)
118 phy_disconnect(dev->phydev);
120 if (priv->last_link) {
121 link_info.u64 = 0;
122 priv->last_link = 0;
124 cvmx_helper_link_set(priv->port, link_info);
125 cvm_oct_note_carrier(priv, link_info);
127 return 0;
131 * cvm_oct_phy_setup_device - setup the PHY
133 * @dev: Device to setup
135 * Returns Zero on success, negative on failure
137 int cvm_oct_phy_setup_device(struct net_device *dev)
139 struct octeon_ethernet *priv = netdev_priv(dev);
140 struct device_node *phy_node;
141 struct phy_device *phydev = NULL;
143 if (!priv->of_node)
144 goto no_phy;
146 phy_node = of_parse_phandle(priv->of_node, "phy-handle", 0);
147 if (!phy_node && of_phy_is_fixed_link(priv->of_node))
148 phy_node = of_node_get(priv->of_node);
149 if (!phy_node)
150 goto no_phy;
152 phydev = of_phy_connect(dev, phy_node, cvm_oct_adjust_link, 0,
153 priv->phy_mode);
154 of_node_put(phy_node);
156 if (!phydev)
157 return -EPROBE_DEFER;
159 priv->last_link = 0;
160 phy_start(phydev);
162 return 0;
163 no_phy:
164 /* If there is no phy, assume a direct MAC connection and that
165 * the link is up.
167 netif_carrier_on(dev);
168 return 0;