blob: 36fc6e16b56986d9a99a56e7c2c459f1b65d9985 [file] [log] [blame]
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001/* Framework for configuring and reading PHY devices
Andy Fleming00db8182005-07-30 19:31:23 -04002 * Based on code in sungem_phy.c and gianfar_phy.c
3 *
4 * Author: Andy Fleming
5 *
6 * Copyright (c) 2004 Freescale Semiconductor, Inc.
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -07007 * Copyright (c) 2006, 2007 Maciej W. Rozycki
Andy Fleming00db8182005-07-30 19:31:23 -04008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
Joe Perches8d242482012-06-09 07:49:07 +000015
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Andy Fleming00db8182005-07-30 19:31:23 -040018#include <linux/kernel.h>
Andy Fleming00db8182005-07-30 19:31:23 -040019#include <linux/string.h>
20#include <linux/errno.h>
21#include <linux/unistd.h>
Andy Fleming00db8182005-07-30 19:31:23 -040022#include <linux/interrupt.h>
Andy Fleming00db8182005-07-30 19:31:23 -040023#include <linux/delay.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/skbuff.h>
Andy Fleming00db8182005-07-30 19:31:23 -040027#include <linux/mm.h>
28#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040029#include <linux/mii.h>
30#include <linux/ethtool.h>
31#include <linux/phy.h>
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +010032#include <linux/timer.h>
33#include <linux/workqueue.h>
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +000034#include <linux/mdio.h>
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030035#include <linux/io.h>
36#include <linux/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070037#include <linux/atomic.h>
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030038
Andy Fleming00db8182005-07-30 19:31:23 -040039#include <asm/irq.h>
Andy Fleming00db8182005-07-30 19:31:23 -040040
Florian Fainelli766d1d32014-02-11 17:27:35 -080041static const char *phy_speed_to_str(int speed)
42{
43 switch (speed) {
44 case SPEED_10:
45 return "10Mbps";
46 case SPEED_100:
47 return "100Mbps";
48 case SPEED_1000:
49 return "1Gbps";
50 case SPEED_2500:
51 return "2.5Gbps";
52 case SPEED_10000:
53 return "10Gbps";
54 case SPEED_UNKNOWN:
55 return "Unknown";
56 default:
57 return "Unsupported (update phy.c)";
58 }
59}
60
Randy Dunlapb3df0da2007-03-06 02:41:48 -080061/**
62 * phy_print_status - Convenience function to print out the current phy status
63 * @phydev: the phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -050064 */
65void phy_print_status(struct phy_device *phydev)
66{
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030067 if (phydev->link) {
Florian Fainellidf40cc82014-02-11 17:27:34 -080068 netdev_info(phydev->attached_dev,
Florian Fainelli766d1d32014-02-11 17:27:35 -080069 "Link is Up - %s/%s - flow control %s\n",
70 phy_speed_to_str(phydev->speed),
Florian Fainellidf40cc82014-02-11 17:27:34 -080071 DUPLEX_FULL == phydev->duplex ? "Full" : "Half",
72 phydev->pause ? "rx/tx" : "off");
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030073 } else {
Florian Fainelli43b63292014-02-11 17:27:33 -080074 netdev_info(phydev->attached_dev, "Link is Down\n");
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030075 }
Andy Fleminge1393452005-08-24 18:46:21 -050076}
77EXPORT_SYMBOL(phy_print_status);
Andy Fleming00db8182005-07-30 19:31:23 -040078
Randy Dunlapb3df0da2007-03-06 02:41:48 -080079/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -080080 * phy_clear_interrupt - Ack the phy device's interrupt
81 * @phydev: the phy_device struct
82 *
83 * If the @phydev driver has an ack_interrupt function, call it to
84 * ack and clear the phy device's interrupt.
85 *
86 * Returns 0 on success on < 0 on error.
87 */
stephen hemminger89ff05e2010-10-21 08:37:41 +000088static int phy_clear_interrupt(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -040089{
Andy Fleming00db8182005-07-30 19:31:23 -040090 if (phydev->drv->ack_interrupt)
Sergei Shtylyove62a7682014-01-05 03:21:52 +030091 return phydev->drv->ack_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -040092
Sergei Shtylyove62a7682014-01-05 03:21:52 +030093 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -040094}
95
Randy Dunlapb3df0da2007-03-06 02:41:48 -080096/**
97 * phy_config_interrupt - configure the PHY device for the requested interrupts
98 * @phydev: the phy_device struct
99 * @interrupts: interrupt flags to configure for this @phydev
100 *
101 * Returns 0 on success on < 0 on error.
102 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000103static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
Andy Fleming00db8182005-07-30 19:31:23 -0400104{
Andy Fleming00db8182005-07-30 19:31:23 -0400105 phydev->interrupts = interrupts;
106 if (phydev->drv->config_intr)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300107 return phydev->drv->config_intr(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400108
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300109 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400110}
111
112
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800113/**
114 * phy_aneg_done - return auto-negotiation status
115 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400116 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800117 * Description: Reads the status register and returns 0 either if
Andy Fleming00db8182005-07-30 19:31:23 -0400118 * auto-negotiation is incomplete, or if there was an error.
119 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
120 */
121static inline int phy_aneg_done(struct phy_device *phydev)
122{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300123 int retval = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -0400124
125 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
126}
127
Andy Fleming00db8182005-07-30 19:31:23 -0400128/* A structure for mapping a particular speed and duplex
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300129 * combination to a particular SUPPORTED and ADVERTISED value
130 */
Andy Fleming00db8182005-07-30 19:31:23 -0400131struct phy_setting {
132 int speed;
133 int duplex;
134 u32 setting;
135};
136
137/* A mapping of all SUPPORTED settings to speed/duplex */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500138static const struct phy_setting settings[] = {
Andy Fleming00db8182005-07-30 19:31:23 -0400139 {
140 .speed = 10000,
141 .duplex = DUPLEX_FULL,
142 .setting = SUPPORTED_10000baseT_Full,
143 },
144 {
145 .speed = SPEED_1000,
146 .duplex = DUPLEX_FULL,
147 .setting = SUPPORTED_1000baseT_Full,
148 },
149 {
150 .speed = SPEED_1000,
151 .duplex = DUPLEX_HALF,
152 .setting = SUPPORTED_1000baseT_Half,
153 },
154 {
155 .speed = SPEED_100,
156 .duplex = DUPLEX_FULL,
157 .setting = SUPPORTED_100baseT_Full,
158 },
159 {
160 .speed = SPEED_100,
161 .duplex = DUPLEX_HALF,
162 .setting = SUPPORTED_100baseT_Half,
163 },
164 {
165 .speed = SPEED_10,
166 .duplex = DUPLEX_FULL,
167 .setting = SUPPORTED_10baseT_Full,
168 },
169 {
170 .speed = SPEED_10,
171 .duplex = DUPLEX_HALF,
172 .setting = SUPPORTED_10baseT_Half,
173 },
174};
175
Denis Chengff8ac602007-09-02 18:30:18 +0800176#define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
Andy Fleming00db8182005-07-30 19:31:23 -0400177
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800178/**
179 * phy_find_setting - find a PHY settings array entry that matches speed & duplex
180 * @speed: speed to match
181 * @duplex: duplex to match
Andy Fleming00db8182005-07-30 19:31:23 -0400182 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800183 * Description: Searches the settings array for the setting which
Andy Fleming00db8182005-07-30 19:31:23 -0400184 * matches the desired speed and duplex, and returns the index
185 * of that setting. Returns the index of the last setting if
186 * none of the others match.
187 */
188static inline int phy_find_setting(int speed, int duplex)
189{
190 int idx = 0;
191
192 while (idx < ARRAY_SIZE(settings) &&
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300193 (settings[idx].speed != speed || settings[idx].duplex != duplex))
Andy Fleming00db8182005-07-30 19:31:23 -0400194 idx++;
195
196 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
197}
198
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800199/**
200 * phy_find_valid - find a PHY setting that matches the requested features mask
201 * @idx: The first index in settings[] to search
202 * @features: A mask of the valid settings
Andy Fleming00db8182005-07-30 19:31:23 -0400203 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800204 * Description: Returns the index of the first valid setting less
Andy Fleming00db8182005-07-30 19:31:23 -0400205 * than or equal to the one pointed to by idx, as determined by
206 * the mask in features. Returns the index of the last setting
207 * if nothing else matches.
208 */
209static inline int phy_find_valid(int idx, u32 features)
210{
211 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
212 idx++;
213
214 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
215}
216
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800217/**
218 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
219 * @phydev: the target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400220 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800221 * Description: Make sure the PHY is set to supported speeds and
Andy Fleming00db8182005-07-30 19:31:23 -0400222 * duplexes. Drop down by one in this order: 1000/FULL,
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800223 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
Andy Fleming00db8182005-07-30 19:31:23 -0400224 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000225static void phy_sanitize_settings(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400226{
227 u32 features = phydev->supported;
228 int idx;
229
230 /* Sanitize settings based on PHY capabilities */
231 if ((features & SUPPORTED_Autoneg) == 0)
Domen Puncer163642a2007-08-07 12:12:41 +0200232 phydev->autoneg = AUTONEG_DISABLE;
Andy Fleming00db8182005-07-30 19:31:23 -0400233
234 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
235 features);
236
237 phydev->speed = settings[idx].speed;
238 phydev->duplex = settings[idx].duplex;
239}
Andy Fleming00db8182005-07-30 19:31:23 -0400240
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800241/**
242 * phy_ethtool_sset - generic ethtool sset function, handles all the details
243 * @phydev: target phy_device struct
244 * @cmd: ethtool_cmd
Andy Fleming00db8182005-07-30 19:31:23 -0400245 *
246 * A few notes about parameter checking:
247 * - We don't set port or transceiver, so we don't care what they
248 * were set to.
249 * - phy_start_aneg() will make sure forced settings are sane, and
250 * choose the next best ones from the ones selected, so we don't
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800251 * care if ethtool tries to give us bad values.
Andy Fleming00db8182005-07-30 19:31:23 -0400252 */
253int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
254{
David Decotigny25db0332011-04-27 18:32:39 +0000255 u32 speed = ethtool_cmd_speed(cmd);
256
Andy Fleming00db8182005-07-30 19:31:23 -0400257 if (cmd->phy_address != phydev->addr)
258 return -EINVAL;
259
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300260 /* We make sure that we don't pass unsupported values in to the PHY */
Andy Fleming00db8182005-07-30 19:31:23 -0400261 cmd->advertising &= phydev->supported;
262
263 /* Verify the settings we care about. */
264 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
265 return -EINVAL;
266
267 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
268 return -EINVAL;
269
Joe Perches8e95a202009-12-03 07:58:21 +0000270 if (cmd->autoneg == AUTONEG_DISABLE &&
David Decotigny25db0332011-04-27 18:32:39 +0000271 ((speed != SPEED_1000 &&
272 speed != SPEED_100 &&
273 speed != SPEED_10) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000274 (cmd->duplex != DUPLEX_HALF &&
275 cmd->duplex != DUPLEX_FULL)))
Andy Fleming00db8182005-07-30 19:31:23 -0400276 return -EINVAL;
277
278 phydev->autoneg = cmd->autoneg;
279
David Decotigny25db0332011-04-27 18:32:39 +0000280 phydev->speed = speed;
Andy Fleming00db8182005-07-30 19:31:23 -0400281
282 phydev->advertising = cmd->advertising;
283
284 if (AUTONEG_ENABLE == cmd->autoneg)
285 phydev->advertising |= ADVERTISED_Autoneg;
286 else
287 phydev->advertising &= ~ADVERTISED_Autoneg;
288
289 phydev->duplex = cmd->duplex;
290
291 /* Restart the PHY */
292 phy_start_aneg(phydev);
293
294 return 0;
295}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600296EXPORT_SYMBOL(phy_ethtool_sset);
Andy Fleming00db8182005-07-30 19:31:23 -0400297
298int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
299{
300 cmd->supported = phydev->supported;
301
302 cmd->advertising = phydev->advertising;
Florian Fainelli114002b2013-12-06 13:01:30 -0800303 cmd->lp_advertising = phydev->lp_advertising;
Andy Fleming00db8182005-07-30 19:31:23 -0400304
David Decotigny70739492011-04-27 18:32:40 +0000305 ethtool_cmd_speed_set(cmd, phydev->speed);
Andy Fleming00db8182005-07-30 19:31:23 -0400306 cmd->duplex = phydev->duplex;
307 cmd->port = PORT_MII;
308 cmd->phy_address = phydev->addr;
Florian Fainelli4284b6a2013-05-23 01:11:12 +0000309 cmd->transceiver = phy_is_internal(phydev) ?
310 XCVR_INTERNAL : XCVR_EXTERNAL;
Andy Fleming00db8182005-07-30 19:31:23 -0400311 cmd->autoneg = phydev->autoneg;
312
313 return 0;
314}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600315EXPORT_SYMBOL(phy_ethtool_gset);
Andy Fleming00db8182005-07-30 19:31:23 -0400316
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800317/**
318 * phy_mii_ioctl - generic PHY MII ioctl interface
319 * @phydev: the phy_device struct
Randy Dunlap00c7d922010-08-09 13:41:59 +0000320 * @ifr: &struct ifreq for socket ioctl's
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800321 * @cmd: ioctl cmd to execute
322 *
323 * Note that this function is currently incompatible with the
Andy Fleming00db8182005-07-30 19:31:23 -0400324 * PHYCONTROL layer. It changes registers without regard to
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800325 * current state. Use at own risk.
Andy Fleming00db8182005-07-30 19:31:23 -0400326 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300327int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
Andy Fleming00db8182005-07-30 19:31:23 -0400328{
Richard Cochran28b04112010-07-17 08:48:55 +0000329 struct mii_ioctl_data *mii_data = if_mii(ifr);
Andy Fleming00db8182005-07-30 19:31:23 -0400330 u16 val = mii_data->val_in;
331
332 switch (cmd) {
333 case SIOCGMIIPHY:
334 mii_data->phy_id = phydev->addr;
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000335 /* fall through */
336
Andy Fleming00db8182005-07-30 19:31:23 -0400337 case SIOCGMIIREG:
Peter Korsgaardaf1dc132011-03-10 06:52:13 +0000338 mii_data->val_out = mdiobus_read(phydev->bus, mii_data->phy_id,
339 mii_data->reg_num);
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300340 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400341
342 case SIOCSMIIREG:
Andy Fleming00db8182005-07-30 19:31:23 -0400343 if (mii_data->phy_id == phydev->addr) {
Florian Fainellie1093742013-12-17 21:38:12 -0800344 switch (mii_data->reg_num) {
Andy Fleming00db8182005-07-30 19:31:23 -0400345 case MII_BMCR:
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300346 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0)
Andy Fleming00db8182005-07-30 19:31:23 -0400347 phydev->autoneg = AUTONEG_DISABLE;
348 else
349 phydev->autoneg = AUTONEG_ENABLE;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300350 if (!phydev->autoneg && (val & BMCR_FULLDPLX))
Andy Fleming00db8182005-07-30 19:31:23 -0400351 phydev->duplex = DUPLEX_FULL;
352 else
353 phydev->duplex = DUPLEX_HALF;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300354 if (!phydev->autoneg && (val & BMCR_SPEED1000))
Shan Lu024a0a32007-03-06 02:42:03 -0800355 phydev->speed = SPEED_1000;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300356 else if (!phydev->autoneg &&
357 (val & BMCR_SPEED100))
Shan Lu024a0a32007-03-06 02:42:03 -0800358 phydev->speed = SPEED_100;
Andy Fleming00db8182005-07-30 19:31:23 -0400359 break;
360 case MII_ADVERTISE:
361 phydev->advertising = val;
362 break;
363 default:
364 /* do nothing */
365 break;
366 }
367 }
368
Peter Korsgaardaf1dc132011-03-10 06:52:13 +0000369 mdiobus_write(phydev->bus, mii_data->phy_id,
370 mii_data->reg_num, val);
371
Joe Perches8e95a202009-12-03 07:58:21 +0000372 if (mii_data->reg_num == MII_BMCR &&
Florian Fainelli2613f952013-12-06 13:01:31 -0800373 val & BMCR_RESET)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300374 return phy_init_hw(phydev);
375 return 0;
David Woodhousedda93b42007-11-28 19:56:34 +0000376
Richard Cochranc1f19b52010-07-17 08:49:36 +0000377 case SIOCSHWTSTAMP:
378 if (phydev->drv->hwtstamp)
379 return phydev->drv->hwtstamp(phydev, ifr);
380 /* fall through */
381
David Woodhousedda93b42007-11-28 19:56:34 +0000382 default:
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000383 return -EOPNOTSUPP;
Andy Fleming00db8182005-07-30 19:31:23 -0400384 }
Andy Fleming00db8182005-07-30 19:31:23 -0400385}
Domen Puncer680e9fe2007-09-17 22:21:40 +0200386EXPORT_SYMBOL(phy_mii_ioctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400387
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800388/**
389 * phy_start_aneg - start auto-negotiation for this PHY device
390 * @phydev: the phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500391 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800392 * Description: Sanitizes the settings (if we're not autonegotiating
393 * them), and then calls the driver's config_aneg function.
394 * If the PHYCONTROL Layer is operating, we change the state to
395 * reflect the beginning of Auto-negotiation or forcing.
Andy Fleminge1393452005-08-24 18:46:21 -0500396 */
397int phy_start_aneg(struct phy_device *phydev)
398{
399 int err;
400
Nate Case35b5f6b2008-01-29 10:05:09 -0600401 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500402
403 if (AUTONEG_DISABLE == phydev->autoneg)
404 phy_sanitize_settings(phydev);
405
406 err = phydev->drv->config_aneg(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500407 if (err < 0)
408 goto out_unlock;
409
410 if (phydev->state != PHY_HALTED) {
411 if (AUTONEG_ENABLE == phydev->autoneg) {
412 phydev->state = PHY_AN;
413 phydev->link_timeout = PHY_AN_TIMEOUT;
414 } else {
415 phydev->state = PHY_FORCING;
416 phydev->link_timeout = PHY_FORCE_TIMEOUT;
417 }
418 }
419
420out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600421 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500422 return err;
423}
424EXPORT_SYMBOL(phy_start_aneg);
425
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800426/**
427 * phy_start_machine - start PHY state machine tracking
428 * @phydev: the phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400429 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800430 * Description: The PHY infrastructure can run a state machine
Andy Fleming00db8182005-07-30 19:31:23 -0400431 * which tracks whether the PHY is starting up, negotiating,
432 * etc. This function starts the timer which tracks the state
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300433 * of the PHY. If you want to maintain your own state machine,
434 * do not call this function.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800435 */
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300436void phy_start_machine(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400437{
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530438 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
Andy Fleming00db8182005-07-30 19:31:23 -0400439}
440
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800441/**
442 * phy_stop_machine - stop the PHY state machine tracking
443 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400444 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800445 * Description: Stops the state machine timer, sets the state to UP
Sergei Shtylylov817acf52006-07-26 00:53:53 +0400446 * (unless it wasn't up yet). This function must be called BEFORE
Andy Fleming00db8182005-07-30 19:31:23 -0400447 * phy_detach.
448 */
449void phy_stop_machine(struct phy_device *phydev)
450{
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700451 cancel_delayed_work_sync(&phydev->state_queue);
Andy Fleming00db8182005-07-30 19:31:23 -0400452
Nate Case35b5f6b2008-01-29 10:05:09 -0600453 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400454 if (phydev->state > PHY_UP)
455 phydev->state = PHY_UP;
Nate Case35b5f6b2008-01-29 10:05:09 -0600456 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400457}
458
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800459/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800460 * phy_error - enter HALTED state for this PHY device
461 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400462 *
463 * Moves the PHY to the HALTED state in response to a read
464 * or write error, and tells the controller the link is down.
465 * Must not be called from interrupt context, or while the
466 * phydev->lock is held.
467 */
Andy Fleming9b9a8bf2008-05-02 13:00:51 -0500468static void phy_error(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400469{
Nate Case35b5f6b2008-01-29 10:05:09 -0600470 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400471 phydev->state = PHY_HALTED;
Nate Case35b5f6b2008-01-29 10:05:09 -0600472 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400473}
474
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800475/**
476 * phy_interrupt - PHY interrupt handler
477 * @irq: interrupt line
478 * @phy_dat: phy_device pointer
Andy Fleminge1393452005-08-24 18:46:21 -0500479 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800480 * Description: When a PHY interrupt occurs, the handler disables
Andy Fleminge1393452005-08-24 18:46:21 -0500481 * interrupts, and schedules a work task to clear the interrupt.
482 */
David Howells7d12e782006-10-05 14:55:46 +0100483static irqreturn_t phy_interrupt(int irq, void *phy_dat)
Andy Fleminge1393452005-08-24 18:46:21 -0500484{
485 struct phy_device *phydev = phy_dat;
486
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100487 if (PHY_HALTED == phydev->state)
488 return IRQ_NONE; /* It can't be ours. */
489
Andy Fleminge1393452005-08-24 18:46:21 -0500490 /* The MDIO bus is not allowed to be written in interrupt
491 * context, so we need to disable the irq here. A work
492 * queue will write the PHY to disable and clear the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300493 * interrupt, and then reenable the irq line.
494 */
Andy Fleminge1393452005-08-24 18:46:21 -0500495 disable_irq_nosync(irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700496 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500497
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530498 queue_work(system_power_efficient_wq, &phydev->phy_queue);
Andy Fleminge1393452005-08-24 18:46:21 -0500499
500 return IRQ_HANDLED;
501}
502
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800503/**
504 * phy_enable_interrupts - Enable the interrupts from the PHY side
505 * @phydev: target phy_device struct
506 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000507static int phy_enable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400508{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300509 int err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400510
Andy Fleminge1393452005-08-24 18:46:21 -0500511 if (err < 0)
512 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400513
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300514 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400515}
Andy Fleming00db8182005-07-30 19:31:23 -0400516
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800517/**
518 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
519 * @phydev: target phy_device struct
520 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000521static int phy_disable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400522{
523 int err;
524
525 /* Disable PHY interrupts */
526 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400527 if (err)
528 goto phy_err;
529
530 /* Clear the interrupt */
531 err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400532 if (err)
533 goto phy_err;
534
535 return 0;
536
537phy_err:
538 phy_error(phydev);
539
540 return err;
541}
Andy Fleminge1393452005-08-24 18:46:21 -0500542
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800543/**
544 * phy_start_interrupts - request and enable interrupts for a PHY device
545 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500546 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800547 * Description: Request the interrupt for the given PHY.
548 * If this fails, then we set irq to PHY_POLL.
Andy Fleminge1393452005-08-24 18:46:21 -0500549 * Otherwise, we enable the interrupts in the PHY.
Andy Fleminge1393452005-08-24 18:46:21 -0500550 * This should only be called with a valid IRQ number.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800551 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500552 */
553int phy_start_interrupts(struct phy_device *phydev)
554{
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700555 atomic_set(&phydev->irq_disable, 0);
Sergei Shtylyov33c133c2013-12-20 22:09:04 +0300556 if (request_irq(phydev->irq, phy_interrupt, 0, "phy_interrupt",
557 phydev) < 0) {
Joe Perches8d242482012-06-09 07:49:07 +0000558 pr_warn("%s: Can't get IRQ %d (PHY)\n",
559 phydev->bus->name, phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500560 phydev->irq = PHY_POLL;
561 return 0;
562 }
563
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300564 return phy_enable_interrupts(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500565}
566EXPORT_SYMBOL(phy_start_interrupts);
567
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800568/**
569 * phy_stop_interrupts - disable interrupts from a PHY device
570 * @phydev: target phy_device struct
571 */
Andy Fleminge1393452005-08-24 18:46:21 -0500572int phy_stop_interrupts(struct phy_device *phydev)
573{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300574 int err = phy_disable_interrupts(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500575
576 if (err)
577 phy_error(phydev);
578
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700579 free_irq(phydev->irq, phydev);
580
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300581 /* Cannot call flush_scheduled_work() here as desired because
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700582 * of rtnl_lock(), but we do not really care about what would
583 * be done, except from enable_irq(), so cancel any work
584 * possibly pending and take care of the matter below.
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100585 */
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700586 cancel_work_sync(&phydev->phy_queue);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300587 /* If work indeed has been cancelled, disable_irq() will have
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700588 * been left unbalanced from phy_interrupt() and enable_irq()
589 * has to be called so that other devices on the line work.
590 */
591 while (atomic_dec_return(&phydev->irq_disable) >= 0)
592 enable_irq(phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500593
594 return err;
595}
596EXPORT_SYMBOL(phy_stop_interrupts);
597
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800598/**
599 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
600 * @work: work_struct that describes the work to be done
601 */
Florian Fainelli5ea94e72013-05-19 22:53:43 +0000602void phy_change(struct work_struct *work)
Andy Fleminge1393452005-08-24 18:46:21 -0500603{
David Howellsc4028952006-11-22 14:57:56 +0000604 struct phy_device *phydev =
605 container_of(work, struct phy_device, phy_queue);
Andy Fleminge1393452005-08-24 18:46:21 -0500606
Anatolij Gustschina8729eb2009-04-07 02:01:42 +0000607 if (phydev->drv->did_interrupt &&
608 !phydev->drv->did_interrupt(phydev))
609 goto ignore;
610
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300611 if (phy_disable_interrupts(phydev))
Andy Fleminge1393452005-08-24 18:46:21 -0500612 goto phy_err;
613
Nate Case35b5f6b2008-01-29 10:05:09 -0600614 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500615 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
616 phydev->state = PHY_CHANGELINK;
Nate Case35b5f6b2008-01-29 10:05:09 -0600617 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500618
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700619 atomic_dec(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500620 enable_irq(phydev->irq);
621
622 /* Reenable interrupts */
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300623 if (PHY_HALTED != phydev->state &&
624 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
Andy Fleminge1393452005-08-24 18:46:21 -0500625 goto irq_enable_err;
626
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700627 /* reschedule state queue work to run as soon as possible */
628 cancel_delayed_work_sync(&phydev->state_queue);
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530629 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
Andy Fleminge1393452005-08-24 18:46:21 -0500630 return;
631
Anatolij Gustschina8729eb2009-04-07 02:01:42 +0000632ignore:
633 atomic_dec(&phydev->irq_disable);
634 enable_irq(phydev->irq);
635 return;
636
Andy Fleminge1393452005-08-24 18:46:21 -0500637irq_enable_err:
638 disable_irq(phydev->irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700639 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500640phy_err:
641 phy_error(phydev);
642}
643
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800644/**
645 * phy_stop - Bring down the PHY link, and stop checking the status
646 * @phydev: target phy_device struct
647 */
Andy Fleminge1393452005-08-24 18:46:21 -0500648void phy_stop(struct phy_device *phydev)
649{
Nate Case35b5f6b2008-01-29 10:05:09 -0600650 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500651
652 if (PHY_HALTED == phydev->state)
653 goto out_unlock;
654
Florian Fainelli2c7b4922013-05-19 22:53:42 +0000655 if (phy_interrupt_is_valid(phydev)) {
Andy Fleminge1393452005-08-24 18:46:21 -0500656 /* Disable PHY Interrupts */
657 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleminge1393452005-08-24 18:46:21 -0500658
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100659 /* Clear any pending interrupts */
660 phy_clear_interrupt(phydev);
661 }
Andy Fleminge1393452005-08-24 18:46:21 -0500662
Maciej W. Rozycki6daf6532007-09-28 22:42:15 -0700663 phydev->state = PHY_HALTED;
664
Andy Fleminge1393452005-08-24 18:46:21 -0500665out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600666 mutex_unlock(&phydev->lock);
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100667
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300668 /* Cannot call flush_scheduled_work() here as desired because
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100669 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
670 * will not reenable interrupts.
671 */
Andy Fleminge1393452005-08-24 18:46:21 -0500672}
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300673EXPORT_SYMBOL(phy_stop);
Andy Fleminge1393452005-08-24 18:46:21 -0500674
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800675/**
676 * phy_start - start or restart a PHY device
677 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500678 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800679 * Description: Indicates the attached device's readiness to
Andy Fleminge1393452005-08-24 18:46:21 -0500680 * handle PHY-related work. Used during startup to start the
681 * PHY, and after a call to phy_stop() to resume operation.
682 * Also used to indicate the MDIO bus has cleared an error
683 * condition.
684 */
685void phy_start(struct phy_device *phydev)
686{
Nate Case35b5f6b2008-01-29 10:05:09 -0600687 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500688
689 switch (phydev->state) {
Florian Fainellie1093742013-12-17 21:38:12 -0800690 case PHY_STARTING:
691 phydev->state = PHY_PENDING;
692 break;
693 case PHY_READY:
694 phydev->state = PHY_UP;
695 break;
696 case PHY_HALTED:
697 phydev->state = PHY_RESUMING;
698 default:
699 break;
Andy Fleminge1393452005-08-24 18:46:21 -0500700 }
Nate Case35b5f6b2008-01-29 10:05:09 -0600701 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500702}
Andy Fleminge1393452005-08-24 18:46:21 -0500703EXPORT_SYMBOL(phy_start);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400704
Nate Case35b5f6b2008-01-29 10:05:09 -0600705/**
706 * phy_state_machine - Handle the state machine
707 * @work: work_struct that describes the work to be done
Nate Case35b5f6b2008-01-29 10:05:09 -0600708 */
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000709void phy_state_machine(struct work_struct *work)
Andy Fleming00db8182005-07-30 19:31:23 -0400710{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700711 struct delayed_work *dwork = to_delayed_work(work);
Nate Case35b5f6b2008-01-29 10:05:09 -0600712 struct phy_device *phydev =
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700713 container_of(dwork, struct phy_device, state_queue);
Sebastian Hesselbarthbe9dad12013-12-13 10:20:29 +0100714 int needs_aneg = 0, do_suspend = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400715 int err = 0;
716
Nate Case35b5f6b2008-01-29 10:05:09 -0600717 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400718
Florian Fainellie1093742013-12-17 21:38:12 -0800719 switch (phydev->state) {
720 case PHY_DOWN:
721 case PHY_STARTING:
722 case PHY_READY:
723 case PHY_PENDING:
724 break;
725 case PHY_UP:
726 needs_aneg = 1;
727
728 phydev->link_timeout = PHY_AN_TIMEOUT;
729
730 break;
731 case PHY_AN:
732 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800733 if (err < 0)
Andy Fleming00db8182005-07-30 19:31:23 -0400734 break;
Florian Fainellie1093742013-12-17 21:38:12 -0800735
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300736 /* If the link is down, give up on negotiation for now */
Florian Fainellie1093742013-12-17 21:38:12 -0800737 if (!phydev->link) {
738 phydev->state = PHY_NOLINK;
739 netif_carrier_off(phydev->attached_dev);
740 phydev->adjust_link(phydev->attached_dev);
741 break;
742 }
743
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300744 /* Check if negotiation is done. Break if there's an error */
Florian Fainellie1093742013-12-17 21:38:12 -0800745 err = phy_aneg_done(phydev);
746 if (err < 0)
747 break;
748
749 /* If AN is done, we're running */
750 if (err > 0) {
751 phydev->state = PHY_RUNNING;
752 netif_carrier_on(phydev->attached_dev);
753 phydev->adjust_link(phydev->attached_dev);
754
755 } else if (0 == phydev->link_timeout--) {
Andy Fleming00db8182005-07-30 19:31:23 -0400756 needs_aneg = 1;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300757 /* If we have the magic_aneg bit, we try again */
Florian Fainellie1093742013-12-17 21:38:12 -0800758 if (phydev->drv->flags & PHY_HAS_MAGICANEG)
759 break;
760 }
761 break;
762 case PHY_NOLINK:
763 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800764 if (err)
Andy Fleming00db8182005-07-30 19:31:23 -0400765 break;
Andy Fleming6b655522006-10-16 16:19:17 -0500766
Florian Fainellie1093742013-12-17 21:38:12 -0800767 if (phydev->link) {
768 phydev->state = PHY_RUNNING;
769 netif_carrier_on(phydev->attached_dev);
770 phydev->adjust_link(phydev->attached_dev);
771 }
772 break;
773 case PHY_FORCING:
774 err = genphy_update_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800775 if (err)
776 break;
Andy Fleming6b655522006-10-16 16:19:17 -0500777
Florian Fainellie1093742013-12-17 21:38:12 -0800778 if (phydev->link) {
779 phydev->state = PHY_RUNNING;
780 netif_carrier_on(phydev->attached_dev);
781 } else {
782 if (0 == phydev->link_timeout--)
783 needs_aneg = 1;
784 }
785
786 phydev->adjust_link(phydev->attached_dev);
787 break;
788 case PHY_RUNNING:
789 /* Only register a CHANGE if we are
790 * polling or ignoring interrupts
791 */
792 if (!phy_interrupt_is_valid(phydev))
793 phydev->state = PHY_CHANGELINK;
794 break;
795 case PHY_CHANGELINK:
796 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800797 if (err)
798 break;
799
800 if (phydev->link) {
801 phydev->state = PHY_RUNNING;
802 netif_carrier_on(phydev->attached_dev);
803 } else {
804 phydev->state = PHY_NOLINK;
805 netif_carrier_off(phydev->attached_dev);
806 }
807
808 phydev->adjust_link(phydev->attached_dev);
809
810 if (phy_interrupt_is_valid(phydev))
811 err = phy_config_interrupt(phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300812 PHY_INTERRUPT_ENABLED);
Florian Fainellie1093742013-12-17 21:38:12 -0800813 break;
814 case PHY_HALTED:
815 if (phydev->link) {
816 phydev->link = 0;
817 netif_carrier_off(phydev->attached_dev);
818 phydev->adjust_link(phydev->attached_dev);
819 do_suspend = 1;
820 }
821 break;
822 case PHY_RESUMING:
Florian Fainellie1093742013-12-17 21:38:12 -0800823 err = phy_clear_interrupt(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800824 if (err)
825 break;
826
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300827 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Florian Fainellie1093742013-12-17 21:38:12 -0800828 if (err)
829 break;
830
831 if (AUTONEG_ENABLE == phydev->autoneg) {
Andy Fleming00db8182005-07-30 19:31:23 -0400832 err = phy_aneg_done(phydev);
833 if (err < 0)
834 break;
835
Florian Fainellie1093742013-12-17 21:38:12 -0800836 /* err > 0 if AN is done.
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300837 * Otherwise, it's 0, and we're still waiting for AN
838 */
Andy Fleming00db8182005-07-30 19:31:23 -0400839 if (err > 0) {
Wade Farnsworth42caa072009-07-01 13:00:34 +0000840 err = phy_read_status(phydev);
841 if (err)
842 break;
843
844 if (phydev->link) {
845 phydev->state = PHY_RUNNING;
846 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300847 } else {
Wade Farnsworth42caa072009-07-01 13:00:34 +0000848 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300849 }
Wade Farnsworth42caa072009-07-01 13:00:34 +0000850 phydev->adjust_link(phydev->attached_dev);
Florian Fainellie1093742013-12-17 21:38:12 -0800851 } else {
852 phydev->state = PHY_AN;
853 phydev->link_timeout = PHY_AN_TIMEOUT;
Wade Farnsworth42caa072009-07-01 13:00:34 +0000854 }
Florian Fainellie1093742013-12-17 21:38:12 -0800855 } else {
856 err = phy_read_status(phydev);
857 if (err)
858 break;
859
860 if (phydev->link) {
861 phydev->state = PHY_RUNNING;
862 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300863 } else {
Florian Fainellie1093742013-12-17 21:38:12 -0800864 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300865 }
Florian Fainellie1093742013-12-17 21:38:12 -0800866 phydev->adjust_link(phydev->attached_dev);
867 }
868 break;
Andy Fleming00db8182005-07-30 19:31:23 -0400869 }
870
Nate Case35b5f6b2008-01-29 10:05:09 -0600871 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400872
873 if (needs_aneg)
874 err = phy_start_aneg(phydev);
875
Sebastian Hesselbarthbe9dad12013-12-13 10:20:29 +0100876 if (do_suspend)
877 phy_suspend(phydev);
878
Andy Fleming00db8182005-07-30 19:31:23 -0400879 if (err < 0)
880 phy_error(phydev);
881
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530882 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300883 PHY_STATE_TIME * HZ);
Nate Case35b5f6b2008-01-29 10:05:09 -0600884}
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000885
Florian Fainelli5ea94e72013-05-19 22:53:43 +0000886void phy_mac_interrupt(struct phy_device *phydev, int new_link)
887{
888 cancel_work_sync(&phydev->phy_queue);
889 phydev->link = new_link;
890 schedule_work(&phydev->phy_queue);
891}
892EXPORT_SYMBOL(phy_mac_interrupt);
893
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000894static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
895 int addr)
896{
897 /* Write the desired MMD Devad */
898 bus->write(bus, addr, MII_MMD_CTRL, devad);
899
900 /* Write the desired MMD register address */
901 bus->write(bus, addr, MII_MMD_DATA, prtad);
902
903 /* Select the Function : DATA with no post increment */
904 bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
905}
906
907/**
908 * phy_read_mmd_indirect - reads data from the MMD registers
909 * @bus: the target MII bus
910 * @prtad: MMD Address
911 * @devad: MMD DEVAD
912 * @addr: PHY address on the MII bus
913 *
914 * Description: it reads data from the MMD registers (clause 22 to access to
915 * clause 45) of the specified phy address.
916 * To read these register we have:
917 * 1) Write reg 13 // DEVAD
918 * 2) Write reg 14 // MMD Address
919 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
920 * 3) Read reg 14 // Read MMD data
921 */
922static int phy_read_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
923 int addr)
924{
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000925 mmd_phy_indirect(bus, prtad, devad, addr);
926
927 /* Read the content of the MMD's selected register */
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300928 return bus->read(bus, addr, MII_MMD_DATA);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000929}
930
931/**
932 * phy_write_mmd_indirect - writes data to the MMD registers
933 * @bus: the target MII bus
934 * @prtad: MMD Address
935 * @devad: MMD DEVAD
936 * @addr: PHY address on the MII bus
937 * @data: data to write in the MMD register
938 *
939 * Description: Write data from the MMD registers of the specified
940 * phy address.
941 * To write these register we have:
942 * 1) Write reg 13 // DEVAD
943 * 2) Write reg 14 // MMD Address
944 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
945 * 3) Write reg 14 // Write MMD data
946 */
947static void phy_write_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
948 int addr, u32 data)
949{
950 mmd_phy_indirect(bus, prtad, devad, addr);
951
952 /* Write the data into MMD's selected register */
953 bus->write(bus, addr, MII_MMD_DATA, data);
954}
955
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000956/**
957 * phy_init_eee - init and check the EEE feature
958 * @phydev: target phy_device struct
959 * @clk_stop_enable: PHY may stop the clock during LPI
960 *
961 * Description: it checks if the Energy-Efficient Ethernet (EEE)
962 * is supported by looking at the MMD registers 3.20 and 7.60/61
963 * and it programs the MMD register 3.0 setting the "Clock stop enable"
964 * bit if required.
965 */
966int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
967{
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000968 /* According to 802.3az,the EEE is supported only in full duplex-mode.
969 * Also EEE feature is active when core is operating with MII, GMII
970 * or RGMII.
971 */
972 if ((phydev->duplex == DUPLEX_FULL) &&
973 ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
974 (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
975 (phydev->interface == PHY_INTERFACE_MODE_RGMII))) {
976 int eee_lp, eee_cap, eee_adv;
977 u32 lp, cap, adv;
978 int idx, status;
979
980 /* Read phy status to properly get the right settings */
981 status = phy_read_status(phydev);
982 if (status)
983 return status;
984
985 /* First check if the EEE ability is supported */
986 eee_cap = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
987 MDIO_MMD_PCS, phydev->addr);
988 if (eee_cap < 0)
989 return eee_cap;
990
Allan, Bruce Wb32607d2012-08-20 04:55:29 +0000991 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000992 if (!cap)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300993 return -EPROTONOSUPPORT;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000994
995 /* Check which link settings negotiated and verify it in
996 * the EEE advertising registers.
997 */
998 eee_lp = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
999 MDIO_MMD_AN, phydev->addr);
1000 if (eee_lp < 0)
1001 return eee_lp;
1002
1003 eee_adv = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
1004 MDIO_MMD_AN, phydev->addr);
1005 if (eee_adv < 0)
1006 return eee_adv;
1007
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001008 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1009 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001010 idx = phy_find_setting(phydev->speed, phydev->duplex);
Giuseppe CAVALLARO9a9c56c2013-05-26 21:31:28 +00001011 if (!(lp & adv & settings[idx].setting))
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001012 return -EPROTONOSUPPORT;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001013
1014 if (clk_stop_enable) {
1015 /* Configure the PHY to stop receiving xMII
1016 * clock while it is signaling LPI.
1017 */
1018 int val = phy_read_mmd_indirect(phydev->bus, MDIO_CTRL1,
1019 MDIO_MMD_PCS,
1020 phydev->addr);
1021 if (val < 0)
1022 return val;
1023
1024 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1025 phy_write_mmd_indirect(phydev->bus, MDIO_CTRL1,
1026 MDIO_MMD_PCS, phydev->addr, val);
1027 }
1028
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001029 return 0; /* EEE supported */
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001030 }
1031
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001032 return -EPROTONOSUPPORT;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001033}
1034EXPORT_SYMBOL(phy_init_eee);
1035
1036/**
1037 * phy_get_eee_err - report the EEE wake error count
1038 * @phydev: target phy_device struct
1039 *
1040 * Description: it is to report the number of time where the PHY
1041 * failed to complete its normal wake sequence.
1042 */
1043int phy_get_eee_err(struct phy_device *phydev)
1044{
1045 return phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_WK_ERR,
1046 MDIO_MMD_PCS, phydev->addr);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001047}
1048EXPORT_SYMBOL(phy_get_eee_err);
1049
1050/**
1051 * phy_ethtool_get_eee - get EEE supported and status
1052 * @phydev: target phy_device struct
1053 * @data: ethtool_eee data
1054 *
1055 * Description: it reportes the Supported/Advertisement/LP Advertisement
1056 * capabilities.
1057 */
1058int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1059{
1060 int val;
1061
1062 /* Get Supported EEE */
1063 val = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
1064 MDIO_MMD_PCS, phydev->addr);
1065 if (val < 0)
1066 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001067 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001068
1069 /* Get advertisement EEE */
1070 val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
1071 MDIO_MMD_AN, phydev->addr);
1072 if (val < 0)
1073 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001074 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001075
1076 /* Get LP advertisement EEE */
1077 val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
1078 MDIO_MMD_AN, phydev->addr);
1079 if (val < 0)
1080 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001081 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001082
1083 return 0;
1084}
1085EXPORT_SYMBOL(phy_ethtool_get_eee);
1086
1087/**
1088 * phy_ethtool_set_eee - set EEE supported and status
1089 * @phydev: target phy_device struct
1090 * @data: ethtool_eee data
1091 *
1092 * Description: it is to program the Advertisement EEE register.
1093 */
1094int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1095{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001096 int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001097
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001098 phy_write_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV, MDIO_MMD_AN,
1099 phydev->addr, val);
1100
1101 return 0;
1102}
1103EXPORT_SYMBOL(phy_ethtool_set_eee);
Michael Stapelberg42e836e2013-03-11 13:56:44 +00001104
1105int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1106{
1107 if (phydev->drv->set_wol)
1108 return phydev->drv->set_wol(phydev, wol);
1109
1110 return -EOPNOTSUPP;
1111}
1112EXPORT_SYMBOL(phy_ethtool_set_wol);
1113
1114void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1115{
1116 if (phydev->drv->get_wol)
1117 phydev->drv->get_wol(phydev, wol);
1118}
1119EXPORT_SYMBOL(phy_ethtool_get_wol);