1 // SPDX-License-Identifier: GPL-2.0
3 * This file is based on code from OCTEON SDK by Cavium Networks.
5 * Copyright (c) 2003-2007 Cavium Networks
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>
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
))
33 return phy_start_aneg(dev
->phydev
);
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
,
47 * cvm_oct_ioctl - IOCTL support for PHY control
48 * @dev: Device to change
52 * Returns Zero on success
54 int cvm_oct_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
56 if (!netif_running(dev
))
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
)
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
);
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
;
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.
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
));
113 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index
, interface
), gmx_cfg
.u64
);
118 phy_disconnect(dev
->phydev
);
120 if (priv
->last_link
) {
124 cvmx_helper_link_set(priv
->port
, link_info
);
125 cvm_oct_note_carrier(priv
, link_info
);
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
;
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
);
152 phydev
= of_phy_connect(dev
, phy_node
, cvm_oct_adjust_link
, 0,
154 of_node_put(phy_node
);
157 return -EPROBE_DEFER
;
164 /* If there is no phy, assume a direct MAC connection and that
167 netif_carrier_on(dev
);