staging: octeon-ethernet: consolidate carrier notifications
[deliverable/linux.git] / drivers / staging / octeon / ethernet-mdio.c
1 /**********************************************************************
2 * Author: Cavium Networks
3 *
4 * Contact: support@caviumnetworks.com
5 * This file is part of the OCTEON SDK
6 *
7 * Copyright (c) 2003-2007 Cavium Networks
8 *
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
12 *
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this file; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * or visit http://www.gnu.org/licenses/.
23 *
24 * This file may also be available under a different license from Cavium.
25 * Contact Cavium Networks for more information
26 **********************************************************************/
27 #include <linux/kernel.h>
28 #include <linux/ethtool.h>
29 #include <linux/phy.h>
30 #include <linux/ratelimit.h>
31 #include <linux/of_mdio.h>
32
33 #include <net/dst.h>
34
35 #include <asm/octeon/octeon.h>
36
37 #include "ethernet-defines.h"
38 #include "octeon-ethernet.h"
39 #include "ethernet-mdio.h"
40 #include "ethernet-util.h"
41
42 #include <asm/octeon/cvmx-gmxx-defs.h>
43 #include <asm/octeon/cvmx-smix-defs.h>
44
45 static void cvm_oct_get_drvinfo(struct net_device *dev,
46 struct ethtool_drvinfo *info)
47 {
48 strlcpy(info->driver, "cavium-ethernet", sizeof(info->driver));
49 strlcpy(info->version, OCTEON_ETHERNET_VERSION, sizeof(info->version));
50 strlcpy(info->bus_info, "Builtin", sizeof(info->bus_info));
51 }
52
53 static int cvm_oct_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
54 {
55 struct octeon_ethernet *priv = netdev_priv(dev);
56
57 if (priv->phydev)
58 return phy_ethtool_gset(priv->phydev, cmd);
59
60 return -EINVAL;
61 }
62
63 static int cvm_oct_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
64 {
65 struct octeon_ethernet *priv = netdev_priv(dev);
66
67 if (!capable(CAP_NET_ADMIN))
68 return -EPERM;
69
70 if (priv->phydev)
71 return phy_ethtool_sset(priv->phydev, cmd);
72
73 return -EINVAL;
74 }
75
76 static int cvm_oct_nway_reset(struct net_device *dev)
77 {
78 struct octeon_ethernet *priv = netdev_priv(dev);
79
80 if (!capable(CAP_NET_ADMIN))
81 return -EPERM;
82
83 if (priv->phydev)
84 return phy_start_aneg(priv->phydev);
85
86 return -EINVAL;
87 }
88
89 const struct ethtool_ops cvm_oct_ethtool_ops = {
90 .get_drvinfo = cvm_oct_get_drvinfo,
91 .get_settings = cvm_oct_get_settings,
92 .set_settings = cvm_oct_set_settings,
93 .nway_reset = cvm_oct_nway_reset,
94 .get_link = ethtool_op_get_link,
95 };
96
97 /**
98 * cvm_oct_ioctl - IOCTL support for PHY control
99 * @dev: Device to change
100 * @rq: the request
101 * @cmd: the command
102 *
103 * Returns Zero on success
104 */
105 int cvm_oct_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
106 {
107 struct octeon_ethernet *priv = netdev_priv(dev);
108
109 if (!netif_running(dev))
110 return -EINVAL;
111
112 if (!priv->phydev)
113 return -EINVAL;
114
115 return phy_mii_ioctl(priv->phydev, rq, cmd);
116 }
117
118 void cvm_oct_note_carrier(struct octeon_ethernet *priv,
119 cvmx_helper_link_info_t li)
120 {
121 if (li.s.link_up) {
122 pr_notice_ratelimited("%s: %u Mbps %s duplex, port %d, queue %d\n",
123 netdev_name(priv->netdev), li.s.speed,
124 (li.s.full_duplex) ? "Full" : "Half",
125 priv->port, priv->queue);
126 } else {
127 pr_notice_ratelimited("%s: Link down\n",
128 netdev_name(priv->netdev));
129 }
130 }
131
132 void cvm_oct_adjust_link(struct net_device *dev)
133 {
134 struct octeon_ethernet *priv = netdev_priv(dev);
135 cvmx_helper_link_info_t link_info;
136
137 if (priv->last_link != priv->phydev->link) {
138 priv->last_link = priv->phydev->link;
139 link_info.u64 = 0;
140 link_info.s.link_up = priv->last_link ? 1 : 0;
141 link_info.s.full_duplex = priv->phydev->duplex ? 1 : 0;
142 link_info.s.speed = priv->phydev->speed;
143
144 cvmx_helper_link_set(priv->port, link_info);
145 cvm_oct_note_carrier(priv, link_info);
146 }
147 }
148
149 int cvm_oct_common_stop(struct net_device *dev)
150 {
151 struct octeon_ethernet *priv = netdev_priv(dev);
152 int interface = INTERFACE(priv->port);
153 cvmx_helper_link_info_t link_info;
154 union cvmx_gmxx_prtx_cfg gmx_cfg;
155 int index = INDEX(priv->port);
156
157 gmx_cfg.u64 = cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
158 gmx_cfg.s.en = 0;
159 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
160
161 priv->poll = NULL;
162
163 if (priv->phydev)
164 phy_disconnect(priv->phydev);
165 priv->phydev = NULL;
166
167 if (priv->last_link) {
168 link_info.u64 = 0;
169 priv->last_link = 0;
170
171 cvmx_helper_link_set(priv->port, link_info);
172 cvm_oct_note_carrier(priv, link_info);
173 }
174 return 0;
175 }
176
177 /**
178 * cvm_oct_phy_setup_device - setup the PHY
179 *
180 * @dev: Device to setup
181 *
182 * Returns Zero on success, negative on failure
183 */
184 int cvm_oct_phy_setup_device(struct net_device *dev)
185 {
186 struct octeon_ethernet *priv = netdev_priv(dev);
187 struct device_node *phy_node;
188
189 if (!priv->of_node)
190 goto no_phy;
191
192 phy_node = of_parse_phandle(priv->of_node, "phy-handle", 0);
193 if (!phy_node)
194 goto no_phy;
195
196 priv->phydev = of_phy_connect(dev, phy_node, cvm_oct_adjust_link, 0,
197 PHY_INTERFACE_MODE_GMII);
198
199 if (priv->phydev == NULL)
200 return -ENODEV;
201
202 priv->last_link = 0;
203 phy_start_aneg(priv->phydev);
204
205 return 0;
206 no_phy:
207 /* If there is no phy, assume a direct MAC connection and that
208 * the link is up.
209 */
210 netif_carrier_on(dev);
211 return 0;
212 }
This page took 0.055381 seconds and 5 git commands to generate.