blob: 19c9eca0ef2638165dc9dc087d87aee934d28348 [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
Randy Dunlapb3df0da2007-03-06 02:41:48 -080041/**
42 * phy_print_status - Convenience function to print out the current phy status
43 * @phydev: the phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -050044 */
45void phy_print_status(struct phy_device *phydev)
46{
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030047 if (phydev->link) {
Joe Perches8d242482012-06-09 07:49:07 +000048 pr_info("%s - Link is Up - %d/%s\n",
49 dev_name(&phydev->dev),
50 phydev->speed,
51 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030052 } else {
Joe Perches8d242482012-06-09 07:49:07 +000053 pr_info("%s - Link is Down\n", dev_name(&phydev->dev));
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030054 }
Andy Fleminge1393452005-08-24 18:46:21 -050055}
56EXPORT_SYMBOL(phy_print_status);
Andy Fleming00db8182005-07-30 19:31:23 -040057
Randy Dunlapb3df0da2007-03-06 02:41:48 -080058/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -080059 * phy_clear_interrupt - Ack the phy device's interrupt
60 * @phydev: the phy_device struct
61 *
62 * If the @phydev driver has an ack_interrupt function, call it to
63 * ack and clear the phy device's interrupt.
64 *
65 * Returns 0 on success on < 0 on error.
66 */
stephen hemminger89ff05e2010-10-21 08:37:41 +000067static int phy_clear_interrupt(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -040068{
Andy Fleming00db8182005-07-30 19:31:23 -040069 if (phydev->drv->ack_interrupt)
Sergei Shtylyove62a7682014-01-05 03:21:52 +030070 return phydev->drv->ack_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -040071
Sergei Shtylyove62a7682014-01-05 03:21:52 +030072 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -040073}
74
Randy Dunlapb3df0da2007-03-06 02:41:48 -080075/**
76 * phy_config_interrupt - configure the PHY device for the requested interrupts
77 * @phydev: the phy_device struct
78 * @interrupts: interrupt flags to configure for this @phydev
79 *
80 * Returns 0 on success on < 0 on error.
81 */
stephen hemminger89ff05e2010-10-21 08:37:41 +000082static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
Andy Fleming00db8182005-07-30 19:31:23 -040083{
Andy Fleming00db8182005-07-30 19:31:23 -040084 phydev->interrupts = interrupts;
85 if (phydev->drv->config_intr)
Sergei Shtylyove62a7682014-01-05 03:21:52 +030086 return phydev->drv->config_intr(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -040087
Sergei Shtylyove62a7682014-01-05 03:21:52 +030088 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -040089}
90
91
Randy Dunlapb3df0da2007-03-06 02:41:48 -080092/**
93 * phy_aneg_done - return auto-negotiation status
94 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -040095 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -080096 * Description: Reads the status register and returns 0 either if
Andy Fleming00db8182005-07-30 19:31:23 -040097 * auto-negotiation is incomplete, or if there was an error.
98 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
99 */
100static inline int phy_aneg_done(struct phy_device *phydev)
101{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300102 int retval = phy_read(phydev, MII_BMSR);
Andy Fleming00db8182005-07-30 19:31:23 -0400103
104 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
105}
106
Andy Fleming00db8182005-07-30 19:31:23 -0400107/* A structure for mapping a particular speed and duplex
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300108 * combination to a particular SUPPORTED and ADVERTISED value
109 */
Andy Fleming00db8182005-07-30 19:31:23 -0400110struct phy_setting {
111 int speed;
112 int duplex;
113 u32 setting;
114};
115
116/* A mapping of all SUPPORTED settings to speed/duplex */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500117static const struct phy_setting settings[] = {
Andy Fleming00db8182005-07-30 19:31:23 -0400118 {
119 .speed = 10000,
120 .duplex = DUPLEX_FULL,
121 .setting = SUPPORTED_10000baseT_Full,
122 },
123 {
124 .speed = SPEED_1000,
125 .duplex = DUPLEX_FULL,
126 .setting = SUPPORTED_1000baseT_Full,
127 },
128 {
129 .speed = SPEED_1000,
130 .duplex = DUPLEX_HALF,
131 .setting = SUPPORTED_1000baseT_Half,
132 },
133 {
134 .speed = SPEED_100,
135 .duplex = DUPLEX_FULL,
136 .setting = SUPPORTED_100baseT_Full,
137 },
138 {
139 .speed = SPEED_100,
140 .duplex = DUPLEX_HALF,
141 .setting = SUPPORTED_100baseT_Half,
142 },
143 {
144 .speed = SPEED_10,
145 .duplex = DUPLEX_FULL,
146 .setting = SUPPORTED_10baseT_Full,
147 },
148 {
149 .speed = SPEED_10,
150 .duplex = DUPLEX_HALF,
151 .setting = SUPPORTED_10baseT_Half,
152 },
153};
154
Denis Chengff8ac602007-09-02 18:30:18 +0800155#define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
Andy Fleming00db8182005-07-30 19:31:23 -0400156
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800157/**
158 * phy_find_setting - find a PHY settings array entry that matches speed & duplex
159 * @speed: speed to match
160 * @duplex: duplex to match
Andy Fleming00db8182005-07-30 19:31:23 -0400161 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800162 * Description: Searches the settings array for the setting which
Andy Fleming00db8182005-07-30 19:31:23 -0400163 * matches the desired speed and duplex, and returns the index
164 * of that setting. Returns the index of the last setting if
165 * none of the others match.
166 */
167static inline int phy_find_setting(int speed, int duplex)
168{
169 int idx = 0;
170
171 while (idx < ARRAY_SIZE(settings) &&
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300172 (settings[idx].speed != speed || settings[idx].duplex != duplex))
Andy Fleming00db8182005-07-30 19:31:23 -0400173 idx++;
174
175 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
176}
177
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800178/**
179 * phy_find_valid - find a PHY setting that matches the requested features mask
180 * @idx: The first index in settings[] to search
181 * @features: A mask of the valid settings
Andy Fleming00db8182005-07-30 19:31:23 -0400182 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800183 * Description: Returns the index of the first valid setting less
Andy Fleming00db8182005-07-30 19:31:23 -0400184 * than or equal to the one pointed to by idx, as determined by
185 * the mask in features. Returns the index of the last setting
186 * if nothing else matches.
187 */
188static inline int phy_find_valid(int idx, u32 features)
189{
190 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
191 idx++;
192
193 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
194}
195
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800196/**
197 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
198 * @phydev: the target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400199 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800200 * Description: Make sure the PHY is set to supported speeds and
Andy Fleming00db8182005-07-30 19:31:23 -0400201 * duplexes. Drop down by one in this order: 1000/FULL,
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800202 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
Andy Fleming00db8182005-07-30 19:31:23 -0400203 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000204static void phy_sanitize_settings(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400205{
206 u32 features = phydev->supported;
207 int idx;
208
209 /* Sanitize settings based on PHY capabilities */
210 if ((features & SUPPORTED_Autoneg) == 0)
Domen Puncer163642a2007-08-07 12:12:41 +0200211 phydev->autoneg = AUTONEG_DISABLE;
Andy Fleming00db8182005-07-30 19:31:23 -0400212
213 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
214 features);
215
216 phydev->speed = settings[idx].speed;
217 phydev->duplex = settings[idx].duplex;
218}
Andy Fleming00db8182005-07-30 19:31:23 -0400219
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800220/**
221 * phy_ethtool_sset - generic ethtool sset function, handles all the details
222 * @phydev: target phy_device struct
223 * @cmd: ethtool_cmd
Andy Fleming00db8182005-07-30 19:31:23 -0400224 *
225 * A few notes about parameter checking:
226 * - We don't set port or transceiver, so we don't care what they
227 * were set to.
228 * - phy_start_aneg() will make sure forced settings are sane, and
229 * choose the next best ones from the ones selected, so we don't
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800230 * care if ethtool tries to give us bad values.
Andy Fleming00db8182005-07-30 19:31:23 -0400231 */
232int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
233{
David Decotigny25db0332011-04-27 18:32:39 +0000234 u32 speed = ethtool_cmd_speed(cmd);
235
Andy Fleming00db8182005-07-30 19:31:23 -0400236 if (cmd->phy_address != phydev->addr)
237 return -EINVAL;
238
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300239 /* We make sure that we don't pass unsupported values in to the PHY */
Andy Fleming00db8182005-07-30 19:31:23 -0400240 cmd->advertising &= phydev->supported;
241
242 /* Verify the settings we care about. */
243 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
244 return -EINVAL;
245
246 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
247 return -EINVAL;
248
Joe Perches8e95a202009-12-03 07:58:21 +0000249 if (cmd->autoneg == AUTONEG_DISABLE &&
David Decotigny25db0332011-04-27 18:32:39 +0000250 ((speed != SPEED_1000 &&
251 speed != SPEED_100 &&
252 speed != SPEED_10) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000253 (cmd->duplex != DUPLEX_HALF &&
254 cmd->duplex != DUPLEX_FULL)))
Andy Fleming00db8182005-07-30 19:31:23 -0400255 return -EINVAL;
256
257 phydev->autoneg = cmd->autoneg;
258
David Decotigny25db0332011-04-27 18:32:39 +0000259 phydev->speed = speed;
Andy Fleming00db8182005-07-30 19:31:23 -0400260
261 phydev->advertising = cmd->advertising;
262
263 if (AUTONEG_ENABLE == cmd->autoneg)
264 phydev->advertising |= ADVERTISED_Autoneg;
265 else
266 phydev->advertising &= ~ADVERTISED_Autoneg;
267
268 phydev->duplex = cmd->duplex;
269
270 /* Restart the PHY */
271 phy_start_aneg(phydev);
272
273 return 0;
274}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600275EXPORT_SYMBOL(phy_ethtool_sset);
Andy Fleming00db8182005-07-30 19:31:23 -0400276
277int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
278{
279 cmd->supported = phydev->supported;
280
281 cmd->advertising = phydev->advertising;
Florian Fainelli114002b2013-12-06 13:01:30 -0800282 cmd->lp_advertising = phydev->lp_advertising;
Andy Fleming00db8182005-07-30 19:31:23 -0400283
David Decotigny70739492011-04-27 18:32:40 +0000284 ethtool_cmd_speed_set(cmd, phydev->speed);
Andy Fleming00db8182005-07-30 19:31:23 -0400285 cmd->duplex = phydev->duplex;
286 cmd->port = PORT_MII;
287 cmd->phy_address = phydev->addr;
Florian Fainelli4284b6a2013-05-23 01:11:12 +0000288 cmd->transceiver = phy_is_internal(phydev) ?
289 XCVR_INTERNAL : XCVR_EXTERNAL;
Andy Fleming00db8182005-07-30 19:31:23 -0400290 cmd->autoneg = phydev->autoneg;
291
292 return 0;
293}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600294EXPORT_SYMBOL(phy_ethtool_gset);
Andy Fleming00db8182005-07-30 19:31:23 -0400295
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800296/**
297 * phy_mii_ioctl - generic PHY MII ioctl interface
298 * @phydev: the phy_device struct
Randy Dunlap00c7d922010-08-09 13:41:59 +0000299 * @ifr: &struct ifreq for socket ioctl's
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800300 * @cmd: ioctl cmd to execute
301 *
302 * Note that this function is currently incompatible with the
Andy Fleming00db8182005-07-30 19:31:23 -0400303 * PHYCONTROL layer. It changes registers without regard to
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800304 * current state. Use at own risk.
Andy Fleming00db8182005-07-30 19:31:23 -0400305 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300306int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
Andy Fleming00db8182005-07-30 19:31:23 -0400307{
Richard Cochran28b04112010-07-17 08:48:55 +0000308 struct mii_ioctl_data *mii_data = if_mii(ifr);
Andy Fleming00db8182005-07-30 19:31:23 -0400309 u16 val = mii_data->val_in;
310
311 switch (cmd) {
312 case SIOCGMIIPHY:
313 mii_data->phy_id = phydev->addr;
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000314 /* fall through */
315
Andy Fleming00db8182005-07-30 19:31:23 -0400316 case SIOCGMIIREG:
Peter Korsgaardaf1dc132011-03-10 06:52:13 +0000317 mii_data->val_out = mdiobus_read(phydev->bus, mii_data->phy_id,
318 mii_data->reg_num);
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300319 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400320
321 case SIOCSMIIREG:
Andy Fleming00db8182005-07-30 19:31:23 -0400322 if (mii_data->phy_id == phydev->addr) {
Florian Fainellie1093742013-12-17 21:38:12 -0800323 switch (mii_data->reg_num) {
Andy Fleming00db8182005-07-30 19:31:23 -0400324 case MII_BMCR:
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300325 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0)
Andy Fleming00db8182005-07-30 19:31:23 -0400326 phydev->autoneg = AUTONEG_DISABLE;
327 else
328 phydev->autoneg = AUTONEG_ENABLE;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300329 if (!phydev->autoneg && (val & BMCR_FULLDPLX))
Andy Fleming00db8182005-07-30 19:31:23 -0400330 phydev->duplex = DUPLEX_FULL;
331 else
332 phydev->duplex = DUPLEX_HALF;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300333 if (!phydev->autoneg && (val & BMCR_SPEED1000))
Shan Lu024a0a32007-03-06 02:42:03 -0800334 phydev->speed = SPEED_1000;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300335 else if (!phydev->autoneg &&
336 (val & BMCR_SPEED100))
Shan Lu024a0a32007-03-06 02:42:03 -0800337 phydev->speed = SPEED_100;
Andy Fleming00db8182005-07-30 19:31:23 -0400338 break;
339 case MII_ADVERTISE:
340 phydev->advertising = val;
341 break;
342 default:
343 /* do nothing */
344 break;
345 }
346 }
347
Peter Korsgaardaf1dc132011-03-10 06:52:13 +0000348 mdiobus_write(phydev->bus, mii_data->phy_id,
349 mii_data->reg_num, val);
350
Joe Perches8e95a202009-12-03 07:58:21 +0000351 if (mii_data->reg_num == MII_BMCR &&
Florian Fainelli2613f952013-12-06 13:01:31 -0800352 val & BMCR_RESET)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300353 return phy_init_hw(phydev);
354 return 0;
David Woodhousedda93b42007-11-28 19:56:34 +0000355
Richard Cochranc1f19b52010-07-17 08:49:36 +0000356 case SIOCSHWTSTAMP:
357 if (phydev->drv->hwtstamp)
358 return phydev->drv->hwtstamp(phydev, ifr);
359 /* fall through */
360
David Woodhousedda93b42007-11-28 19:56:34 +0000361 default:
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000362 return -EOPNOTSUPP;
Andy Fleming00db8182005-07-30 19:31:23 -0400363 }
Andy Fleming00db8182005-07-30 19:31:23 -0400364}
Domen Puncer680e9fe2007-09-17 22:21:40 +0200365EXPORT_SYMBOL(phy_mii_ioctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400366
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800367/**
368 * phy_start_aneg - start auto-negotiation for this PHY device
369 * @phydev: the phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500370 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800371 * Description: Sanitizes the settings (if we're not autonegotiating
372 * them), and then calls the driver's config_aneg function.
373 * If the PHYCONTROL Layer is operating, we change the state to
374 * reflect the beginning of Auto-negotiation or forcing.
Andy Fleminge1393452005-08-24 18:46:21 -0500375 */
376int phy_start_aneg(struct phy_device *phydev)
377{
378 int err;
379
Nate Case35b5f6b2008-01-29 10:05:09 -0600380 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500381
382 if (AUTONEG_DISABLE == phydev->autoneg)
383 phy_sanitize_settings(phydev);
384
385 err = phydev->drv->config_aneg(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500386 if (err < 0)
387 goto out_unlock;
388
389 if (phydev->state != PHY_HALTED) {
390 if (AUTONEG_ENABLE == phydev->autoneg) {
391 phydev->state = PHY_AN;
392 phydev->link_timeout = PHY_AN_TIMEOUT;
393 } else {
394 phydev->state = PHY_FORCING;
395 phydev->link_timeout = PHY_FORCE_TIMEOUT;
396 }
397 }
398
399out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600400 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500401 return err;
402}
403EXPORT_SYMBOL(phy_start_aneg);
404
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800405/**
406 * phy_start_machine - start PHY state machine tracking
407 * @phydev: the phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400408 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800409 * Description: The PHY infrastructure can run a state machine
Andy Fleming00db8182005-07-30 19:31:23 -0400410 * which tracks whether the PHY is starting up, negotiating,
411 * etc. This function starts the timer which tracks the state
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300412 * of the PHY. If you want to maintain your own state machine,
413 * do not call this function.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800414 */
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300415void phy_start_machine(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400416{
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530417 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
Andy Fleming00db8182005-07-30 19:31:23 -0400418}
419
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800420/**
421 * phy_stop_machine - stop the PHY state machine tracking
422 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400423 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800424 * Description: Stops the state machine timer, sets the state to UP
Sergei Shtylylov817acf52006-07-26 00:53:53 +0400425 * (unless it wasn't up yet). This function must be called BEFORE
Andy Fleming00db8182005-07-30 19:31:23 -0400426 * phy_detach.
427 */
428void phy_stop_machine(struct phy_device *phydev)
429{
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700430 cancel_delayed_work_sync(&phydev->state_queue);
Andy Fleming00db8182005-07-30 19:31:23 -0400431
Nate Case35b5f6b2008-01-29 10:05:09 -0600432 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400433 if (phydev->state > PHY_UP)
434 phydev->state = PHY_UP;
Nate Case35b5f6b2008-01-29 10:05:09 -0600435 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400436}
437
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800438/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800439 * phy_error - enter HALTED state for this PHY device
440 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400441 *
442 * Moves the PHY to the HALTED state in response to a read
443 * or write error, and tells the controller the link is down.
444 * Must not be called from interrupt context, or while the
445 * phydev->lock is held.
446 */
Andy Fleming9b9a8bf2008-05-02 13:00:51 -0500447static void phy_error(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400448{
Nate Case35b5f6b2008-01-29 10:05:09 -0600449 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400450 phydev->state = PHY_HALTED;
Nate Case35b5f6b2008-01-29 10:05:09 -0600451 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400452}
453
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800454/**
455 * phy_interrupt - PHY interrupt handler
456 * @irq: interrupt line
457 * @phy_dat: phy_device pointer
Andy Fleminge1393452005-08-24 18:46:21 -0500458 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800459 * Description: When a PHY interrupt occurs, the handler disables
Andy Fleminge1393452005-08-24 18:46:21 -0500460 * interrupts, and schedules a work task to clear the interrupt.
461 */
David Howells7d12e782006-10-05 14:55:46 +0100462static irqreturn_t phy_interrupt(int irq, void *phy_dat)
Andy Fleminge1393452005-08-24 18:46:21 -0500463{
464 struct phy_device *phydev = phy_dat;
465
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100466 if (PHY_HALTED == phydev->state)
467 return IRQ_NONE; /* It can't be ours. */
468
Andy Fleminge1393452005-08-24 18:46:21 -0500469 /* The MDIO bus is not allowed to be written in interrupt
470 * context, so we need to disable the irq here. A work
471 * queue will write the PHY to disable and clear the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300472 * interrupt, and then reenable the irq line.
473 */
Andy Fleminge1393452005-08-24 18:46:21 -0500474 disable_irq_nosync(irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700475 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500476
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530477 queue_work(system_power_efficient_wq, &phydev->phy_queue);
Andy Fleminge1393452005-08-24 18:46:21 -0500478
479 return IRQ_HANDLED;
480}
481
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800482/**
483 * phy_enable_interrupts - Enable the interrupts from the PHY side
484 * @phydev: target phy_device struct
485 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000486static int phy_enable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400487{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300488 int err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400489
Andy Fleminge1393452005-08-24 18:46:21 -0500490 if (err < 0)
491 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400492
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300493 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400494}
Andy Fleming00db8182005-07-30 19:31:23 -0400495
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800496/**
497 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
498 * @phydev: target phy_device struct
499 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000500static int phy_disable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400501{
502 int err;
503
504 /* Disable PHY interrupts */
505 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400506 if (err)
507 goto phy_err;
508
509 /* Clear the interrupt */
510 err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400511 if (err)
512 goto phy_err;
513
514 return 0;
515
516phy_err:
517 phy_error(phydev);
518
519 return err;
520}
Andy Fleminge1393452005-08-24 18:46:21 -0500521
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800522/**
523 * phy_start_interrupts - request and enable interrupts for a PHY device
524 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500525 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800526 * Description: Request the interrupt for the given PHY.
527 * If this fails, then we set irq to PHY_POLL.
Andy Fleminge1393452005-08-24 18:46:21 -0500528 * Otherwise, we enable the interrupts in the PHY.
Andy Fleminge1393452005-08-24 18:46:21 -0500529 * This should only be called with a valid IRQ number.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800530 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500531 */
532int phy_start_interrupts(struct phy_device *phydev)
533{
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700534 atomic_set(&phydev->irq_disable, 0);
Sergei Shtylyov33c133c2013-12-20 22:09:04 +0300535 if (request_irq(phydev->irq, phy_interrupt, 0, "phy_interrupt",
536 phydev) < 0) {
Joe Perches8d242482012-06-09 07:49:07 +0000537 pr_warn("%s: Can't get IRQ %d (PHY)\n",
538 phydev->bus->name, phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500539 phydev->irq = PHY_POLL;
540 return 0;
541 }
542
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300543 return phy_enable_interrupts(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500544}
545EXPORT_SYMBOL(phy_start_interrupts);
546
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800547/**
548 * phy_stop_interrupts - disable interrupts from a PHY device
549 * @phydev: target phy_device struct
550 */
Andy Fleminge1393452005-08-24 18:46:21 -0500551int phy_stop_interrupts(struct phy_device *phydev)
552{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300553 int err = phy_disable_interrupts(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500554
555 if (err)
556 phy_error(phydev);
557
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700558 free_irq(phydev->irq, phydev);
559
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300560 /* Cannot call flush_scheduled_work() here as desired because
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700561 * of rtnl_lock(), but we do not really care about what would
562 * be done, except from enable_irq(), so cancel any work
563 * possibly pending and take care of the matter below.
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100564 */
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700565 cancel_work_sync(&phydev->phy_queue);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300566 /* If work indeed has been cancelled, disable_irq() will have
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700567 * been left unbalanced from phy_interrupt() and enable_irq()
568 * has to be called so that other devices on the line work.
569 */
570 while (atomic_dec_return(&phydev->irq_disable) >= 0)
571 enable_irq(phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500572
573 return err;
574}
575EXPORT_SYMBOL(phy_stop_interrupts);
576
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800577/**
578 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
579 * @work: work_struct that describes the work to be done
580 */
Florian Fainelli5ea94e72013-05-19 22:53:43 +0000581void phy_change(struct work_struct *work)
Andy Fleminge1393452005-08-24 18:46:21 -0500582{
David Howellsc4028952006-11-22 14:57:56 +0000583 struct phy_device *phydev =
584 container_of(work, struct phy_device, phy_queue);
Andy Fleminge1393452005-08-24 18:46:21 -0500585
Anatolij Gustschina8729eb2009-04-07 02:01:42 +0000586 if (phydev->drv->did_interrupt &&
587 !phydev->drv->did_interrupt(phydev))
588 goto ignore;
589
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300590 if (phy_disable_interrupts(phydev))
Andy Fleminge1393452005-08-24 18:46:21 -0500591 goto phy_err;
592
Nate Case35b5f6b2008-01-29 10:05:09 -0600593 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500594 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
595 phydev->state = PHY_CHANGELINK;
Nate Case35b5f6b2008-01-29 10:05:09 -0600596 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500597
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700598 atomic_dec(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500599 enable_irq(phydev->irq);
600
601 /* Reenable interrupts */
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300602 if (PHY_HALTED != phydev->state &&
603 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
Andy Fleminge1393452005-08-24 18:46:21 -0500604 goto irq_enable_err;
605
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700606 /* reschedule state queue work to run as soon as possible */
607 cancel_delayed_work_sync(&phydev->state_queue);
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530608 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
Andy Fleminge1393452005-08-24 18:46:21 -0500609 return;
610
Anatolij Gustschina8729eb2009-04-07 02:01:42 +0000611ignore:
612 atomic_dec(&phydev->irq_disable);
613 enable_irq(phydev->irq);
614 return;
615
Andy Fleminge1393452005-08-24 18:46:21 -0500616irq_enable_err:
617 disable_irq(phydev->irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700618 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500619phy_err:
620 phy_error(phydev);
621}
622
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800623/**
624 * phy_stop - Bring down the PHY link, and stop checking the status
625 * @phydev: target phy_device struct
626 */
Andy Fleminge1393452005-08-24 18:46:21 -0500627void phy_stop(struct phy_device *phydev)
628{
Nate Case35b5f6b2008-01-29 10:05:09 -0600629 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500630
631 if (PHY_HALTED == phydev->state)
632 goto out_unlock;
633
Florian Fainelli2c7b4922013-05-19 22:53:42 +0000634 if (phy_interrupt_is_valid(phydev)) {
Andy Fleminge1393452005-08-24 18:46:21 -0500635 /* Disable PHY Interrupts */
636 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleminge1393452005-08-24 18:46:21 -0500637
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100638 /* Clear any pending interrupts */
639 phy_clear_interrupt(phydev);
640 }
Andy Fleminge1393452005-08-24 18:46:21 -0500641
Maciej W. Rozycki6daf6532007-09-28 22:42:15 -0700642 phydev->state = PHY_HALTED;
643
Andy Fleminge1393452005-08-24 18:46:21 -0500644out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600645 mutex_unlock(&phydev->lock);
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100646
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300647 /* Cannot call flush_scheduled_work() here as desired because
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100648 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
649 * will not reenable interrupts.
650 */
Andy Fleminge1393452005-08-24 18:46:21 -0500651}
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300652EXPORT_SYMBOL(phy_stop);
Andy Fleminge1393452005-08-24 18:46:21 -0500653
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800654/**
655 * phy_start - start or restart a PHY device
656 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500657 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800658 * Description: Indicates the attached device's readiness to
Andy Fleminge1393452005-08-24 18:46:21 -0500659 * handle PHY-related work. Used during startup to start the
660 * PHY, and after a call to phy_stop() to resume operation.
661 * Also used to indicate the MDIO bus has cleared an error
662 * condition.
663 */
664void phy_start(struct phy_device *phydev)
665{
Nate Case35b5f6b2008-01-29 10:05:09 -0600666 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500667
668 switch (phydev->state) {
Florian Fainellie1093742013-12-17 21:38:12 -0800669 case PHY_STARTING:
670 phydev->state = PHY_PENDING;
671 break;
672 case PHY_READY:
673 phydev->state = PHY_UP;
674 break;
675 case PHY_HALTED:
676 phydev->state = PHY_RESUMING;
677 default:
678 break;
Andy Fleminge1393452005-08-24 18:46:21 -0500679 }
Nate Case35b5f6b2008-01-29 10:05:09 -0600680 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500681}
Andy Fleminge1393452005-08-24 18:46:21 -0500682EXPORT_SYMBOL(phy_start);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400683
Nate Case35b5f6b2008-01-29 10:05:09 -0600684/**
685 * phy_state_machine - Handle the state machine
686 * @work: work_struct that describes the work to be done
Nate Case35b5f6b2008-01-29 10:05:09 -0600687 */
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000688void phy_state_machine(struct work_struct *work)
Andy Fleming00db8182005-07-30 19:31:23 -0400689{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700690 struct delayed_work *dwork = to_delayed_work(work);
Nate Case35b5f6b2008-01-29 10:05:09 -0600691 struct phy_device *phydev =
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700692 container_of(dwork, struct phy_device, state_queue);
Sebastian Hesselbarthbe9dad12013-12-13 10:20:29 +0100693 int needs_aneg = 0, do_suspend = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400694 int err = 0;
695
Nate Case35b5f6b2008-01-29 10:05:09 -0600696 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400697
Florian Fainellie1093742013-12-17 21:38:12 -0800698 switch (phydev->state) {
699 case PHY_DOWN:
700 case PHY_STARTING:
701 case PHY_READY:
702 case PHY_PENDING:
703 break;
704 case PHY_UP:
705 needs_aneg = 1;
706
707 phydev->link_timeout = PHY_AN_TIMEOUT;
708
709 break;
710 case PHY_AN:
711 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800712 if (err < 0)
Andy Fleming00db8182005-07-30 19:31:23 -0400713 break;
Florian Fainellie1093742013-12-17 21:38:12 -0800714
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300715 /* If the link is down, give up on negotiation for now */
Florian Fainellie1093742013-12-17 21:38:12 -0800716 if (!phydev->link) {
717 phydev->state = PHY_NOLINK;
718 netif_carrier_off(phydev->attached_dev);
719 phydev->adjust_link(phydev->attached_dev);
720 break;
721 }
722
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300723 /* Check if negotiation is done. Break if there's an error */
Florian Fainellie1093742013-12-17 21:38:12 -0800724 err = phy_aneg_done(phydev);
725 if (err < 0)
726 break;
727
728 /* If AN is done, we're running */
729 if (err > 0) {
730 phydev->state = PHY_RUNNING;
731 netif_carrier_on(phydev->attached_dev);
732 phydev->adjust_link(phydev->attached_dev);
733
734 } else if (0 == phydev->link_timeout--) {
Andy Fleming00db8182005-07-30 19:31:23 -0400735 needs_aneg = 1;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300736 /* If we have the magic_aneg bit, we try again */
Florian Fainellie1093742013-12-17 21:38:12 -0800737 if (phydev->drv->flags & PHY_HAS_MAGICANEG)
738 break;
739 }
740 break;
741 case PHY_NOLINK:
742 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800743 if (err)
Andy Fleming00db8182005-07-30 19:31:23 -0400744 break;
Andy Fleming6b655522006-10-16 16:19:17 -0500745
Florian Fainellie1093742013-12-17 21:38:12 -0800746 if (phydev->link) {
747 phydev->state = PHY_RUNNING;
748 netif_carrier_on(phydev->attached_dev);
749 phydev->adjust_link(phydev->attached_dev);
750 }
751 break;
752 case PHY_FORCING:
753 err = genphy_update_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800754 if (err)
755 break;
Andy Fleming6b655522006-10-16 16:19:17 -0500756
Florian Fainellie1093742013-12-17 21:38:12 -0800757 if (phydev->link) {
758 phydev->state = PHY_RUNNING;
759 netif_carrier_on(phydev->attached_dev);
760 } else {
761 if (0 == phydev->link_timeout--)
762 needs_aneg = 1;
763 }
764
765 phydev->adjust_link(phydev->attached_dev);
766 break;
767 case PHY_RUNNING:
768 /* Only register a CHANGE if we are
769 * polling or ignoring interrupts
770 */
771 if (!phy_interrupt_is_valid(phydev))
772 phydev->state = PHY_CHANGELINK;
773 break;
774 case PHY_CHANGELINK:
775 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800776 if (err)
777 break;
778
779 if (phydev->link) {
780 phydev->state = PHY_RUNNING;
781 netif_carrier_on(phydev->attached_dev);
782 } else {
783 phydev->state = PHY_NOLINK;
784 netif_carrier_off(phydev->attached_dev);
785 }
786
787 phydev->adjust_link(phydev->attached_dev);
788
789 if (phy_interrupt_is_valid(phydev))
790 err = phy_config_interrupt(phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300791 PHY_INTERRUPT_ENABLED);
Florian Fainellie1093742013-12-17 21:38:12 -0800792 break;
793 case PHY_HALTED:
794 if (phydev->link) {
795 phydev->link = 0;
796 netif_carrier_off(phydev->attached_dev);
797 phydev->adjust_link(phydev->attached_dev);
798 do_suspend = 1;
799 }
800 break;
801 case PHY_RESUMING:
Florian Fainellie1093742013-12-17 21:38:12 -0800802 err = phy_clear_interrupt(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800803 if (err)
804 break;
805
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300806 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Florian Fainellie1093742013-12-17 21:38:12 -0800807 if (err)
808 break;
809
810 if (AUTONEG_ENABLE == phydev->autoneg) {
Andy Fleming00db8182005-07-30 19:31:23 -0400811 err = phy_aneg_done(phydev);
812 if (err < 0)
813 break;
814
Florian Fainellie1093742013-12-17 21:38:12 -0800815 /* err > 0 if AN is done.
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300816 * Otherwise, it's 0, and we're still waiting for AN
817 */
Andy Fleming00db8182005-07-30 19:31:23 -0400818 if (err > 0) {
Wade Farnsworth42caa072009-07-01 13:00:34 +0000819 err = phy_read_status(phydev);
820 if (err)
821 break;
822
823 if (phydev->link) {
824 phydev->state = PHY_RUNNING;
825 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300826 } else {
Wade Farnsworth42caa072009-07-01 13:00:34 +0000827 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300828 }
Wade Farnsworth42caa072009-07-01 13:00:34 +0000829 phydev->adjust_link(phydev->attached_dev);
Florian Fainellie1093742013-12-17 21:38:12 -0800830 } else {
831 phydev->state = PHY_AN;
832 phydev->link_timeout = PHY_AN_TIMEOUT;
Wade Farnsworth42caa072009-07-01 13:00:34 +0000833 }
Florian Fainellie1093742013-12-17 21:38:12 -0800834 } else {
835 err = phy_read_status(phydev);
836 if (err)
837 break;
838
839 if (phydev->link) {
840 phydev->state = PHY_RUNNING;
841 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300842 } else {
Florian Fainellie1093742013-12-17 21:38:12 -0800843 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300844 }
Florian Fainellie1093742013-12-17 21:38:12 -0800845 phydev->adjust_link(phydev->attached_dev);
846 }
847 break;
Andy Fleming00db8182005-07-30 19:31:23 -0400848 }
849
Nate Case35b5f6b2008-01-29 10:05:09 -0600850 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400851
852 if (needs_aneg)
853 err = phy_start_aneg(phydev);
854
Sebastian Hesselbarthbe9dad12013-12-13 10:20:29 +0100855 if (do_suspend)
856 phy_suspend(phydev);
857
Andy Fleming00db8182005-07-30 19:31:23 -0400858 if (err < 0)
859 phy_error(phydev);
860
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530861 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300862 PHY_STATE_TIME * HZ);
Nate Case35b5f6b2008-01-29 10:05:09 -0600863}
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000864
Florian Fainelli5ea94e72013-05-19 22:53:43 +0000865void phy_mac_interrupt(struct phy_device *phydev, int new_link)
866{
867 cancel_work_sync(&phydev->phy_queue);
868 phydev->link = new_link;
869 schedule_work(&phydev->phy_queue);
870}
871EXPORT_SYMBOL(phy_mac_interrupt);
872
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000873static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
874 int addr)
875{
876 /* Write the desired MMD Devad */
877 bus->write(bus, addr, MII_MMD_CTRL, devad);
878
879 /* Write the desired MMD register address */
880 bus->write(bus, addr, MII_MMD_DATA, prtad);
881
882 /* Select the Function : DATA with no post increment */
883 bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
884}
885
886/**
887 * phy_read_mmd_indirect - reads data from the MMD registers
888 * @bus: the target MII bus
889 * @prtad: MMD Address
890 * @devad: MMD DEVAD
891 * @addr: PHY address on the MII bus
892 *
893 * Description: it reads data from the MMD registers (clause 22 to access to
894 * clause 45) of the specified phy address.
895 * To read these register we have:
896 * 1) Write reg 13 // DEVAD
897 * 2) Write reg 14 // MMD Address
898 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
899 * 3) Read reg 14 // Read MMD data
900 */
901static int phy_read_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
902 int addr)
903{
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000904 mmd_phy_indirect(bus, prtad, devad, addr);
905
906 /* Read the content of the MMD's selected register */
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300907 return bus->read(bus, addr, MII_MMD_DATA);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000908}
909
910/**
911 * phy_write_mmd_indirect - writes data to the MMD registers
912 * @bus: the target MII bus
913 * @prtad: MMD Address
914 * @devad: MMD DEVAD
915 * @addr: PHY address on the MII bus
916 * @data: data to write in the MMD register
917 *
918 * Description: Write data from the MMD registers of the specified
919 * phy address.
920 * To write these register we have:
921 * 1) Write reg 13 // DEVAD
922 * 2) Write reg 14 // MMD Address
923 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
924 * 3) Write reg 14 // Write MMD data
925 */
926static void phy_write_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
927 int addr, u32 data)
928{
929 mmd_phy_indirect(bus, prtad, devad, addr);
930
931 /* Write the data into MMD's selected register */
932 bus->write(bus, addr, MII_MMD_DATA, data);
933}
934
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000935/**
936 * phy_init_eee - init and check the EEE feature
937 * @phydev: target phy_device struct
938 * @clk_stop_enable: PHY may stop the clock during LPI
939 *
940 * Description: it checks if the Energy-Efficient Ethernet (EEE)
941 * is supported by looking at the MMD registers 3.20 and 7.60/61
942 * and it programs the MMD register 3.0 setting the "Clock stop enable"
943 * bit if required.
944 */
945int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
946{
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000947 /* According to 802.3az,the EEE is supported only in full duplex-mode.
948 * Also EEE feature is active when core is operating with MII, GMII
949 * or RGMII.
950 */
951 if ((phydev->duplex == DUPLEX_FULL) &&
952 ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
953 (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
954 (phydev->interface == PHY_INTERFACE_MODE_RGMII))) {
955 int eee_lp, eee_cap, eee_adv;
956 u32 lp, cap, adv;
957 int idx, status;
958
959 /* Read phy status to properly get the right settings */
960 status = phy_read_status(phydev);
961 if (status)
962 return status;
963
964 /* First check if the EEE ability is supported */
965 eee_cap = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
966 MDIO_MMD_PCS, phydev->addr);
967 if (eee_cap < 0)
968 return eee_cap;
969
Allan, Bruce Wb32607d2012-08-20 04:55:29 +0000970 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000971 if (!cap)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300972 return -EPROTONOSUPPORT;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000973
974 /* Check which link settings negotiated and verify it in
975 * the EEE advertising registers.
976 */
977 eee_lp = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
978 MDIO_MMD_AN, phydev->addr);
979 if (eee_lp < 0)
980 return eee_lp;
981
982 eee_adv = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
983 MDIO_MMD_AN, phydev->addr);
984 if (eee_adv < 0)
985 return eee_adv;
986
Allan, Bruce Wb32607d2012-08-20 04:55:29 +0000987 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
988 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000989 idx = phy_find_setting(phydev->speed, phydev->duplex);
Giuseppe CAVALLARO9a9c56c2013-05-26 21:31:28 +0000990 if (!(lp & adv & settings[idx].setting))
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300991 return -EPROTONOSUPPORT;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +0000992
993 if (clk_stop_enable) {
994 /* Configure the PHY to stop receiving xMII
995 * clock while it is signaling LPI.
996 */
997 int val = phy_read_mmd_indirect(phydev->bus, MDIO_CTRL1,
998 MDIO_MMD_PCS,
999 phydev->addr);
1000 if (val < 0)
1001 return val;
1002
1003 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1004 phy_write_mmd_indirect(phydev->bus, MDIO_CTRL1,
1005 MDIO_MMD_PCS, phydev->addr, val);
1006 }
1007
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001008 return 0; /* EEE supported */
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001009 }
1010
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001011 return -EPROTONOSUPPORT;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001012}
1013EXPORT_SYMBOL(phy_init_eee);
1014
1015/**
1016 * phy_get_eee_err - report the EEE wake error count
1017 * @phydev: target phy_device struct
1018 *
1019 * Description: it is to report the number of time where the PHY
1020 * failed to complete its normal wake sequence.
1021 */
1022int phy_get_eee_err(struct phy_device *phydev)
1023{
1024 return phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_WK_ERR,
1025 MDIO_MMD_PCS, phydev->addr);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001026}
1027EXPORT_SYMBOL(phy_get_eee_err);
1028
1029/**
1030 * phy_ethtool_get_eee - get EEE supported and status
1031 * @phydev: target phy_device struct
1032 * @data: ethtool_eee data
1033 *
1034 * Description: it reportes the Supported/Advertisement/LP Advertisement
1035 * capabilities.
1036 */
1037int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1038{
1039 int val;
1040
1041 /* Get Supported EEE */
1042 val = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
1043 MDIO_MMD_PCS, phydev->addr);
1044 if (val < 0)
1045 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001046 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001047
1048 /* Get advertisement EEE */
1049 val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
1050 MDIO_MMD_AN, phydev->addr);
1051 if (val < 0)
1052 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001053 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001054
1055 /* Get LP advertisement EEE */
1056 val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
1057 MDIO_MMD_AN, phydev->addr);
1058 if (val < 0)
1059 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001060 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001061
1062 return 0;
1063}
1064EXPORT_SYMBOL(phy_ethtool_get_eee);
1065
1066/**
1067 * phy_ethtool_set_eee - set EEE supported and status
1068 * @phydev: target phy_device struct
1069 * @data: ethtool_eee data
1070 *
1071 * Description: it is to program the Advertisement EEE register.
1072 */
1073int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1074{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001075 int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001076
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001077 phy_write_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV, MDIO_MMD_AN,
1078 phydev->addr, val);
1079
1080 return 0;
1081}
1082EXPORT_SYMBOL(phy_ethtool_set_eee);
Michael Stapelberg42e836e2013-03-11 13:56:44 +00001083
1084int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1085{
1086 if (phydev->drv->set_wol)
1087 return phydev->drv->set_wol(phydev, wol);
1088
1089 return -EOPNOTSUPP;
1090}
1091EXPORT_SYMBOL(phy_ethtool_set_wol);
1092
1093void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1094{
1095 if (phydev->drv->get_wol)
1096 phydev->drv->get_wol(phydev, wol);
1097}
1098EXPORT_SYMBOL(phy_ethtool_get_wol);