blob: 00280d4eeb563099ae1ab4d177f62c27347751b3 [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>
Geert Uytterhoevend6f8cfa2017-01-25 11:39:49 +010032#include <linux/phy_led_triggers.h>
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +010033#include <linux/timer.h>
34#include <linux/workqueue.h>
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +000035#include <linux/mdio.h>
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030036#include <linux/io.h>
37#include <linux/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070038#include <linux/atomic.h>
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030039
Andy Fleming00db8182005-07-30 19:31:23 -040040#include <asm/irq.h>
Andy Fleming00db8182005-07-30 19:31:23 -040041
Florian Fainelli766d1d32014-02-11 17:27:35 -080042static const char *phy_speed_to_str(int speed)
43{
44 switch (speed) {
45 case SPEED_10:
46 return "10Mbps";
47 case SPEED_100:
48 return "100Mbps";
49 case SPEED_1000:
50 return "1Gbps";
51 case SPEED_2500:
52 return "2.5Gbps";
Joe Perches1f37b172017-04-02 14:30:06 -070053 case SPEED_5000:
54 return "5Gbps";
Florian Fainelli766d1d32014-02-11 17:27:35 -080055 case SPEED_10000:
56 return "10Gbps";
Joe Perches1f37b172017-04-02 14:30:06 -070057 case SPEED_20000:
58 return "20Gbps";
59 case SPEED_25000:
60 return "25Gbps";
61 case SPEED_40000:
62 return "40Gbps";
63 case SPEED_50000:
64 return "50Gbps";
65 case SPEED_56000:
66 return "56Gbps";
67 case SPEED_100000:
68 return "100Gbps";
Florian Fainelli766d1d32014-02-11 17:27:35 -080069 case SPEED_UNKNOWN:
70 return "Unknown";
71 default:
72 return "Unsupported (update phy.c)";
73 }
74}
75
Florian Fainelli3e2186e2015-05-16 10:17:56 -070076#define PHY_STATE_STR(_state) \
77 case PHY_##_state: \
78 return __stringify(_state); \
79
80static const char *phy_state_to_str(enum phy_state st)
81{
82 switch (st) {
83 PHY_STATE_STR(DOWN)
84 PHY_STATE_STR(STARTING)
85 PHY_STATE_STR(READY)
86 PHY_STATE_STR(PENDING)
87 PHY_STATE_STR(UP)
88 PHY_STATE_STR(AN)
89 PHY_STATE_STR(RUNNING)
90 PHY_STATE_STR(NOLINK)
91 PHY_STATE_STR(FORCING)
92 PHY_STATE_STR(CHANGELINK)
93 PHY_STATE_STR(HALTED)
94 PHY_STATE_STR(RESUMING)
95 }
96
97 return NULL;
98}
99
100
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800101/**
102 * phy_print_status - Convenience function to print out the current phy status
103 * @phydev: the phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500104 */
105void phy_print_status(struct phy_device *phydev)
106{
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300107 if (phydev->link) {
Florian Fainellidf40cc82014-02-11 17:27:34 -0800108 netdev_info(phydev->attached_dev,
Florian Fainelli766d1d32014-02-11 17:27:35 -0800109 "Link is Up - %s/%s - flow control %s\n",
110 phy_speed_to_str(phydev->speed),
Florian Fainellidf40cc82014-02-11 17:27:34 -0800111 DUPLEX_FULL == phydev->duplex ? "Full" : "Half",
112 phydev->pause ? "rx/tx" : "off");
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300113 } else {
Florian Fainelli43b63292014-02-11 17:27:33 -0800114 netdev_info(phydev->attached_dev, "Link is Down\n");
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300115 }
Andy Fleminge1393452005-08-24 18:46:21 -0500116}
117EXPORT_SYMBOL(phy_print_status);
Andy Fleming00db8182005-07-30 19:31:23 -0400118
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800119/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800120 * phy_clear_interrupt - Ack the phy device's interrupt
121 * @phydev: the phy_device struct
122 *
123 * If the @phydev driver has an ack_interrupt function, call it to
124 * ack and clear the phy device's interrupt.
125 *
Florian Fainelliad033502014-02-11 17:27:38 -0800126 * Returns 0 on success or < 0 on error.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800127 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000128static int phy_clear_interrupt(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400129{
Andy Fleming00db8182005-07-30 19:31:23 -0400130 if (phydev->drv->ack_interrupt)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300131 return phydev->drv->ack_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400132
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300133 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400134}
135
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800136/**
137 * phy_config_interrupt - configure the PHY device for the requested interrupts
138 * @phydev: the phy_device struct
139 * @interrupts: interrupt flags to configure for this @phydev
140 *
Florian Fainelliad033502014-02-11 17:27:38 -0800141 * Returns 0 on success or < 0 on error.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800142 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000143static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
Andy Fleming00db8182005-07-30 19:31:23 -0400144{
Andy Fleming00db8182005-07-30 19:31:23 -0400145 phydev->interrupts = interrupts;
146 if (phydev->drv->config_intr)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300147 return phydev->drv->config_intr(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400148
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300149 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400150}
151
152
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800153/**
154 * phy_aneg_done - return auto-negotiation status
155 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400156 *
Florian Fainelli76a423a2014-02-11 17:27:37 -0800157 * Description: Return the auto-negotiation status from this @phydev
158 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
159 * is still pending.
Andy Fleming00db8182005-07-30 19:31:23 -0400160 */
Lendacky, Thomas372788f2016-11-10 17:10:46 -0600161int phy_aneg_done(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400162{
Florian Fainelli65f27672017-02-23 14:22:19 -0800163 if (phydev->drv && phydev->drv->aneg_done)
Florian Fainelli76a423a2014-02-11 17:27:37 -0800164 return phydev->drv->aneg_done(phydev);
165
Florian Fainellia9fa6e62014-02-11 17:27:36 -0800166 return genphy_aneg_done(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400167}
Lendacky, Thomas372788f2016-11-10 17:10:46 -0600168EXPORT_SYMBOL(phy_aneg_done);
Andy Fleming00db8182005-07-30 19:31:23 -0400169
Andy Fleming00db8182005-07-30 19:31:23 -0400170/* A structure for mapping a particular speed and duplex
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300171 * combination to a particular SUPPORTED and ADVERTISED value
172 */
Andy Fleming00db8182005-07-30 19:31:23 -0400173struct phy_setting {
174 int speed;
175 int duplex;
176 u32 setting;
177};
178
Russell Kingd0613032017-04-13 16:49:15 +0100179/* A mapping of all SUPPORTED settings to speed/duplex. This table
180 * must be grouped by speed and sorted in descending match priority
181 * - iow, descending speed. */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500182static const struct phy_setting settings[] = {
Andy Fleming00db8182005-07-30 19:31:23 -0400183 {
Lendacky, Thomas3e707702014-07-14 14:05:46 -0500184 .speed = SPEED_10000,
185 .duplex = DUPLEX_FULL,
186 .setting = SUPPORTED_10000baseKR_Full,
187 },
188 {
189 .speed = SPEED_10000,
190 .duplex = DUPLEX_FULL,
191 .setting = SUPPORTED_10000baseKX4_Full,
192 },
193 {
194 .speed = SPEED_10000,
Andy Fleming00db8182005-07-30 19:31:23 -0400195 .duplex = DUPLEX_FULL,
196 .setting = SUPPORTED_10000baseT_Full,
197 },
198 {
Lendacky, Thomas3e707702014-07-14 14:05:46 -0500199 .speed = SPEED_2500,
200 .duplex = DUPLEX_FULL,
201 .setting = SUPPORTED_2500baseX_Full,
202 },
203 {
204 .speed = SPEED_1000,
205 .duplex = DUPLEX_FULL,
206 .setting = SUPPORTED_1000baseKX_Full,
207 },
208 {
Andy Fleming00db8182005-07-30 19:31:23 -0400209 .speed = SPEED_1000,
210 .duplex = DUPLEX_FULL,
211 .setting = SUPPORTED_1000baseT_Full,
212 },
213 {
214 .speed = SPEED_1000,
215 .duplex = DUPLEX_HALF,
216 .setting = SUPPORTED_1000baseT_Half,
217 },
218 {
219 .speed = SPEED_100,
220 .duplex = DUPLEX_FULL,
221 .setting = SUPPORTED_100baseT_Full,
222 },
223 {
224 .speed = SPEED_100,
225 .duplex = DUPLEX_HALF,
226 .setting = SUPPORTED_100baseT_Half,
227 },
228 {
229 .speed = SPEED_10,
230 .duplex = DUPLEX_FULL,
231 .setting = SUPPORTED_10baseT_Full,
232 },
233 {
234 .speed = SPEED_10,
235 .duplex = DUPLEX_HALF,
236 .setting = SUPPORTED_10baseT_Half,
237 },
238};
239
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800240/**
Russell Kingd0613032017-04-13 16:49:15 +0100241 * phy_lookup_setting - lookup a PHY setting
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800242 * @speed: speed to match
243 * @duplex: duplex to match
Russell Kingd0613032017-04-13 16:49:15 +0100244 * @feature: allowed link modes
245 * @exact: an exact match is required
Andy Fleming00db8182005-07-30 19:31:23 -0400246 *
Russell Kingd0613032017-04-13 16:49:15 +0100247 * Search the settings array for a setting that matches the speed and
248 * duplex, and which is supported.
249 *
250 * If @exact is unset, either an exact match or %NULL for no match will
251 * be returned.
252 *
253 * If @exact is set, an exact match, the fastest supported setting at
254 * or below the specified speed, the slowest supported setting, or if
255 * they all fail, %NULL will be returned.
Andy Fleming00db8182005-07-30 19:31:23 -0400256 */
Russell Kingd0613032017-04-13 16:49:15 +0100257static const struct phy_setting *
258phy_lookup_setting(int speed, int duplex, u32 features, bool exact)
Andy Fleming00db8182005-07-30 19:31:23 -0400259{
Russell Kingd0613032017-04-13 16:49:15 +0100260 const struct phy_setting *p, *match = NULL, *last = NULL;
261 int i;
Andy Fleming00db8182005-07-30 19:31:23 -0400262
Russell Kingd0613032017-04-13 16:49:15 +0100263 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
264 if (p->setting & features) {
265 last = p;
266 if (p->speed == speed && p->duplex == duplex) {
267 /* Exact match for speed and duplex */
268 match = p;
269 break;
270 } else if (!exact) {
271 if (!match && p->speed <= speed)
272 /* Candidate */
273 match = p;
Andy Fleming00db8182005-07-30 19:31:23 -0400274
Russell Kingd0613032017-04-13 16:49:15 +0100275 if (p->speed < speed)
276 break;
277 }
278 }
279 }
280
281 if (!match && !exact)
282 match = last;
283
284 return match;
Andy Fleming00db8182005-07-30 19:31:23 -0400285}
286
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800287/**
Russell Kingd0613032017-04-13 16:49:15 +0100288 * phy_find_valid - find a PHY setting that matches the requested parameters
289 * @speed: desired speed
290 * @duplex: desired duplex
291 * @supported: mask of supported link modes
Andy Fleming00db8182005-07-30 19:31:23 -0400292 *
Russell Kingd0613032017-04-13 16:49:15 +0100293 * Locate a supported phy setting that is, in priority order:
294 * - an exact match for the specified speed and duplex mode
295 * - a match for the specified speed, or slower speed
296 * - the slowest supported speed
297 * Returns the matched phy_setting entry, or %NULL if no supported phy
298 * settings were found.
Andy Fleming00db8182005-07-30 19:31:23 -0400299 */
Russell Kingd0613032017-04-13 16:49:15 +0100300static const struct phy_setting *
301phy_find_valid(int speed, int duplex, u32 supported)
Andy Fleming00db8182005-07-30 19:31:23 -0400302{
Russell Kingd0613032017-04-13 16:49:15 +0100303 return phy_lookup_setting(speed, duplex, supported, false);
Andy Fleming00db8182005-07-30 19:31:23 -0400304}
305
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800306/**
Zach Brown1f9127c2016-10-17 10:49:54 -0500307 * phy_supported_speeds - return all speeds currently supported by a phy device
308 * @phy: The phy device to return supported speeds of.
309 * @speeds: buffer to store supported speeds in.
310 * @size: size of speeds buffer.
311 *
312 * Description: Returns the number of supported speeds, and fills the speeds
313 * buffer with the supported speeds. If speeds buffer is too small to contain
314 * all currently supported speeds, will return as many speeds as can fit.
315 */
316unsigned int phy_supported_speeds(struct phy_device *phy,
317 unsigned int *speeds,
318 unsigned int size)
319{
320 unsigned int count = 0;
321 unsigned int idx = 0;
322
Russell Kingd0613032017-04-13 16:49:15 +0100323 for (idx = 0; idx < ARRAY_SIZE(settings) && count < size; idx++) {
Zach Brown1f9127c2016-10-17 10:49:54 -0500324 if (!(settings[idx].setting & phy->supported))
Russell Kingd0613032017-04-13 16:49:15 +0100325 continue;
Zach Brown1f9127c2016-10-17 10:49:54 -0500326
327 /* Assumes settings are grouped by speed */
328 if ((count == 0) ||
329 (speeds[count - 1] != settings[idx].speed)) {
330 speeds[count] = settings[idx].speed;
331 count++;
332 }
Zach Brown1f9127c2016-10-17 10:49:54 -0500333 }
334
335 return count;
336}
337
338/**
Guenter Roeck54da5a82015-02-17 09:36:22 -0800339 * phy_check_valid - check if there is a valid PHY setting which matches
340 * speed, duplex, and feature mask
341 * @speed: speed to match
342 * @duplex: duplex to match
343 * @features: A mask of the valid settings
344 *
345 * Description: Returns true if there is a valid setting, false otherwise.
346 */
347static inline bool phy_check_valid(int speed, int duplex, u32 features)
348{
Russell Kingd0613032017-04-13 16:49:15 +0100349 return !!phy_lookup_setting(speed, duplex, features, true);
Guenter Roeck54da5a82015-02-17 09:36:22 -0800350}
351
352/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800353 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
354 * @phydev: the target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400355 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800356 * Description: Make sure the PHY is set to supported speeds and
Andy Fleming00db8182005-07-30 19:31:23 -0400357 * duplexes. Drop down by one in this order: 1000/FULL,
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800358 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
Andy Fleming00db8182005-07-30 19:31:23 -0400359 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000360static void phy_sanitize_settings(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400361{
Russell Kingd0613032017-04-13 16:49:15 +0100362 const struct phy_setting *setting;
Andy Fleming00db8182005-07-30 19:31:23 -0400363 u32 features = phydev->supported;
Andy Fleming00db8182005-07-30 19:31:23 -0400364
365 /* Sanitize settings based on PHY capabilities */
366 if ((features & SUPPORTED_Autoneg) == 0)
Domen Puncer163642a2007-08-07 12:12:41 +0200367 phydev->autoneg = AUTONEG_DISABLE;
Andy Fleming00db8182005-07-30 19:31:23 -0400368
Russell Kingd0613032017-04-13 16:49:15 +0100369 setting = phy_find_valid(phydev->speed, phydev->duplex, features);
370 if (setting) {
371 phydev->speed = setting->speed;
372 phydev->duplex = setting->duplex;
373 } else {
374 /* We failed to find anything (no supported speeds?) */
375 phydev->speed = SPEED_UNKNOWN;
376 phydev->duplex = DUPLEX_UNKNOWN;
377 }
Andy Fleming00db8182005-07-30 19:31:23 -0400378}
Andy Fleming00db8182005-07-30 19:31:23 -0400379
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800380/**
381 * phy_ethtool_sset - generic ethtool sset function, handles all the details
382 * @phydev: target phy_device struct
383 * @cmd: ethtool_cmd
Andy Fleming00db8182005-07-30 19:31:23 -0400384 *
385 * A few notes about parameter checking:
386 * - We don't set port or transceiver, so we don't care what they
387 * were set to.
388 * - phy_start_aneg() will make sure forced settings are sane, and
389 * choose the next best ones from the ones selected, so we don't
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800390 * care if ethtool tries to give us bad values.
Andy Fleming00db8182005-07-30 19:31:23 -0400391 */
392int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
393{
David Decotigny25db0332011-04-27 18:32:39 +0000394 u32 speed = ethtool_cmd_speed(cmd);
395
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100396 if (cmd->phy_address != phydev->mdio.addr)
Andy Fleming00db8182005-07-30 19:31:23 -0400397 return -EINVAL;
398
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300399 /* We make sure that we don't pass unsupported values in to the PHY */
Andy Fleming00db8182005-07-30 19:31:23 -0400400 cmd->advertising &= phydev->supported;
401
402 /* Verify the settings we care about. */
403 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
404 return -EINVAL;
405
406 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
407 return -EINVAL;
408
Joe Perches8e95a202009-12-03 07:58:21 +0000409 if (cmd->autoneg == AUTONEG_DISABLE &&
David Decotigny25db0332011-04-27 18:32:39 +0000410 ((speed != SPEED_1000 &&
411 speed != SPEED_100 &&
412 speed != SPEED_10) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000413 (cmd->duplex != DUPLEX_HALF &&
414 cmd->duplex != DUPLEX_FULL)))
Andy Fleming00db8182005-07-30 19:31:23 -0400415 return -EINVAL;
416
417 phydev->autoneg = cmd->autoneg;
418
David Decotigny25db0332011-04-27 18:32:39 +0000419 phydev->speed = speed;
Andy Fleming00db8182005-07-30 19:31:23 -0400420
421 phydev->advertising = cmd->advertising;
422
423 if (AUTONEG_ENABLE == cmd->autoneg)
424 phydev->advertising |= ADVERTISED_Autoneg;
425 else
426 phydev->advertising &= ~ADVERTISED_Autoneg;
427
428 phydev->duplex = cmd->duplex;
429
Raju Lakkaraju1004ee62016-11-29 15:16:47 +0530430 phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
David Thomson634ec362015-07-10 13:56:54 +1200431
Andy Fleming00db8182005-07-30 19:31:23 -0400432 /* Restart the PHY */
433 phy_start_aneg(phydev);
434
435 return 0;
436}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600437EXPORT_SYMBOL(phy_ethtool_sset);
Andy Fleming00db8182005-07-30 19:31:23 -0400438
Philippe Reynes2d551732016-04-15 00:35:00 +0200439int phy_ethtool_ksettings_set(struct phy_device *phydev,
440 const struct ethtool_link_ksettings *cmd)
441{
442 u8 autoneg = cmd->base.autoneg;
443 u8 duplex = cmd->base.duplex;
444 u32 speed = cmd->base.speed;
445 u32 advertising;
446
447 if (cmd->base.phy_address != phydev->mdio.addr)
448 return -EINVAL;
449
450 ethtool_convert_link_mode_to_legacy_u32(&advertising,
451 cmd->link_modes.advertising);
452
453 /* We make sure that we don't pass unsupported values in to the PHY */
454 advertising &= phydev->supported;
455
456 /* Verify the settings we care about. */
457 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
458 return -EINVAL;
459
460 if (autoneg == AUTONEG_ENABLE && advertising == 0)
461 return -EINVAL;
462
463 if (autoneg == AUTONEG_DISABLE &&
464 ((speed != SPEED_1000 &&
465 speed != SPEED_100 &&
466 speed != SPEED_10) ||
467 (duplex != DUPLEX_HALF &&
468 duplex != DUPLEX_FULL)))
469 return -EINVAL;
470
471 phydev->autoneg = autoneg;
472
473 phydev->speed = speed;
474
475 phydev->advertising = advertising;
476
477 if (autoneg == AUTONEG_ENABLE)
478 phydev->advertising |= ADVERTISED_Autoneg;
479 else
480 phydev->advertising &= ~ADVERTISED_Autoneg;
481
482 phydev->duplex = duplex;
483
Raju Lakkaraju1004ee62016-11-29 15:16:47 +0530484 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
Philippe Reynes2d551732016-04-15 00:35:00 +0200485
486 /* Restart the PHY */
487 phy_start_aneg(phydev);
488
489 return 0;
490}
491EXPORT_SYMBOL(phy_ethtool_ksettings_set);
492
Andy Fleming00db8182005-07-30 19:31:23 -0400493int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
494{
495 cmd->supported = phydev->supported;
496
497 cmd->advertising = phydev->advertising;
Florian Fainelli114002b2013-12-06 13:01:30 -0800498 cmd->lp_advertising = phydev->lp_advertising;
Andy Fleming00db8182005-07-30 19:31:23 -0400499
David Decotigny70739492011-04-27 18:32:40 +0000500 ethtool_cmd_speed_set(cmd, phydev->speed);
Andy Fleming00db8182005-07-30 19:31:23 -0400501 cmd->duplex = phydev->duplex;
Florian Fainellic88838c2014-02-13 16:08:43 -0800502 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
503 cmd->port = PORT_BNC;
504 else
505 cmd->port = PORT_MII;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100506 cmd->phy_address = phydev->mdio.addr;
Florian Fainelli4284b6a2013-05-23 01:11:12 +0000507 cmd->transceiver = phy_is_internal(phydev) ?
508 XCVR_INTERNAL : XCVR_EXTERNAL;
Andy Fleming00db8182005-07-30 19:31:23 -0400509 cmd->autoneg = phydev->autoneg;
Raju Lakkaraju1004ee62016-11-29 15:16:47 +0530510 cmd->eth_tp_mdix_ctrl = phydev->mdix_ctrl;
511 cmd->eth_tp_mdix = phydev->mdix;
Andy Fleming00db8182005-07-30 19:31:23 -0400512
513 return 0;
514}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600515EXPORT_SYMBOL(phy_ethtool_gset);
Andy Fleming00db8182005-07-30 19:31:23 -0400516
Philippe Reynes2d551732016-04-15 00:35:00 +0200517int phy_ethtool_ksettings_get(struct phy_device *phydev,
518 struct ethtool_link_ksettings *cmd)
519{
520 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
521 phydev->supported);
522
523 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
524 phydev->advertising);
525
526 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
527 phydev->lp_advertising);
528
529 cmd->base.speed = phydev->speed;
530 cmd->base.duplex = phydev->duplex;
531 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
532 cmd->base.port = PORT_BNC;
533 else
534 cmd->base.port = PORT_MII;
535
536 cmd->base.phy_address = phydev->mdio.addr;
537 cmd->base.autoneg = phydev->autoneg;
Raju Lakkaraju1004ee62016-11-29 15:16:47 +0530538 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
539 cmd->base.eth_tp_mdix = phydev->mdix;
Philippe Reynes2d551732016-04-15 00:35:00 +0200540
541 return 0;
542}
543EXPORT_SYMBOL(phy_ethtool_ksettings_get);
544
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800545/**
546 * phy_mii_ioctl - generic PHY MII ioctl interface
547 * @phydev: the phy_device struct
Randy Dunlap00c7d922010-08-09 13:41:59 +0000548 * @ifr: &struct ifreq for socket ioctl's
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800549 * @cmd: ioctl cmd to execute
550 *
551 * Note that this function is currently incompatible with the
Andy Fleming00db8182005-07-30 19:31:23 -0400552 * PHYCONTROL layer. It changes registers without regard to
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800553 * current state. Use at own risk.
Andy Fleming00db8182005-07-30 19:31:23 -0400554 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300555int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
Andy Fleming00db8182005-07-30 19:31:23 -0400556{
Richard Cochran28b04112010-07-17 08:48:55 +0000557 struct mii_ioctl_data *mii_data = if_mii(ifr);
Andy Fleming00db8182005-07-30 19:31:23 -0400558 u16 val = mii_data->val_in;
Brian Hill79ce0472014-11-11 13:39:39 -0700559 bool change_autoneg = false;
Andy Fleming00db8182005-07-30 19:31:23 -0400560
561 switch (cmd) {
562 case SIOCGMIIPHY:
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100563 mii_data->phy_id = phydev->mdio.addr;
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000564 /* fall through */
565
Andy Fleming00db8182005-07-30 19:31:23 -0400566 case SIOCGMIIREG:
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100567 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
568 mii_data->phy_id,
Peter Korsgaardaf1dc132011-03-10 06:52:13 +0000569 mii_data->reg_num);
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300570 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400571
572 case SIOCSMIIREG:
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100573 if (mii_data->phy_id == phydev->mdio.addr) {
Florian Fainellie1093742013-12-17 21:38:12 -0800574 switch (mii_data->reg_num) {
Andy Fleming00db8182005-07-30 19:31:23 -0400575 case MII_BMCR:
Brian Hill79ce0472014-11-11 13:39:39 -0700576 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
577 if (phydev->autoneg == AUTONEG_ENABLE)
578 change_autoneg = true;
Andy Fleming00db8182005-07-30 19:31:23 -0400579 phydev->autoneg = AUTONEG_DISABLE;
Brian Hill79ce0472014-11-11 13:39:39 -0700580 if (val & BMCR_FULLDPLX)
581 phydev->duplex = DUPLEX_FULL;
582 else
583 phydev->duplex = DUPLEX_HALF;
584 if (val & BMCR_SPEED1000)
585 phydev->speed = SPEED_1000;
586 else if (val & BMCR_SPEED100)
587 phydev->speed = SPEED_100;
588 else phydev->speed = SPEED_10;
589 }
590 else {
591 if (phydev->autoneg == AUTONEG_DISABLE)
592 change_autoneg = true;
Andy Fleming00db8182005-07-30 19:31:23 -0400593 phydev->autoneg = AUTONEG_ENABLE;
Brian Hill79ce0472014-11-11 13:39:39 -0700594 }
Andy Fleming00db8182005-07-30 19:31:23 -0400595 break;
596 case MII_ADVERTISE:
Brian Hill79ce0472014-11-11 13:39:39 -0700597 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
598 change_autoneg = true;
Andy Fleming00db8182005-07-30 19:31:23 -0400599 break;
600 default:
601 /* do nothing */
602 break;
603 }
604 }
605
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100606 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
Peter Korsgaardaf1dc132011-03-10 06:52:13 +0000607 mii_data->reg_num, val);
608
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100609 if (mii_data->phy_id == phydev->mdio.addr &&
Jérôme Pouillercf18b772015-12-03 10:02:35 +0100610 mii_data->reg_num == MII_BMCR &&
Florian Fainelli2613f952013-12-06 13:01:31 -0800611 val & BMCR_RESET)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300612 return phy_init_hw(phydev);
Brian Hill79ce0472014-11-11 13:39:39 -0700613
614 if (change_autoneg)
615 return phy_start_aneg(phydev);
616
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300617 return 0;
David Woodhousedda93b42007-11-28 19:56:34 +0000618
Richard Cochranc1f19b52010-07-17 08:49:36 +0000619 case SIOCSHWTSTAMP:
Florian Fainelli25149ef2017-02-17 16:07:34 -0800620 if (phydev->drv && phydev->drv->hwtstamp)
Richard Cochranc1f19b52010-07-17 08:49:36 +0000621 return phydev->drv->hwtstamp(phydev, ifr);
622 /* fall through */
623
David Woodhousedda93b42007-11-28 19:56:34 +0000624 default:
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000625 return -EOPNOTSUPP;
Andy Fleming00db8182005-07-30 19:31:23 -0400626 }
Andy Fleming00db8182005-07-30 19:31:23 -0400627}
Domen Puncer680e9fe2007-09-17 22:21:40 +0200628EXPORT_SYMBOL(phy_mii_ioctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400629
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800630/**
631 * phy_start_aneg - start auto-negotiation for this PHY device
632 * @phydev: the phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500633 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800634 * Description: Sanitizes the settings (if we're not autonegotiating
635 * them), and then calls the driver's config_aneg function.
636 * If the PHYCONTROL Layer is operating, we change the state to
637 * reflect the beginning of Auto-negotiation or forcing.
Andy Fleminge1393452005-08-24 18:46:21 -0500638 */
639int phy_start_aneg(struct phy_device *phydev)
640{
641 int err;
642
Florian Fainelli25149ef2017-02-17 16:07:34 -0800643 if (!phydev->drv)
644 return -EIO;
645
Nate Case35b5f6b2008-01-29 10:05:09 -0600646 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500647
648 if (AUTONEG_DISABLE == phydev->autoneg)
649 phy_sanitize_settings(phydev);
650
Ben Hutchings9b3320e2015-01-27 00:58:15 +0000651 /* Invalidate LP advertising flags */
652 phydev->lp_advertising = 0;
653
Andy Fleminge1393452005-08-24 18:46:21 -0500654 err = phydev->drv->config_aneg(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500655 if (err < 0)
656 goto out_unlock;
657
658 if (phydev->state != PHY_HALTED) {
659 if (AUTONEG_ENABLE == phydev->autoneg) {
660 phydev->state = PHY_AN;
661 phydev->link_timeout = PHY_AN_TIMEOUT;
662 } else {
663 phydev->state = PHY_FORCING;
664 phydev->link_timeout = PHY_FORCE_TIMEOUT;
665 }
666 }
667
668out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600669 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500670 return err;
671}
672EXPORT_SYMBOL(phy_start_aneg);
673
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800674/**
675 * phy_start_machine - start PHY state machine tracking
676 * @phydev: the phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400677 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800678 * Description: The PHY infrastructure can run a state machine
Andy Fleming00db8182005-07-30 19:31:23 -0400679 * which tracks whether the PHY is starting up, negotiating,
680 * etc. This function starts the timer which tracks the state
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300681 * of the PHY. If you want to maintain your own state machine,
682 * do not call this function.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800683 */
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300684void phy_start_machine(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400685{
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530686 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
Andy Fleming00db8182005-07-30 19:31:23 -0400687}
688
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800689/**
Andrew Lunn3c293f42016-10-12 22:14:53 +0200690 * phy_trigger_machine - trigger the state machine to run
691 *
692 * @phydev: the phy_device struct
Florian Fainellieab12772017-01-20 15:31:52 -0800693 * @sync: indicate whether we should wait for the workqueue cancelation
Andrew Lunn3c293f42016-10-12 22:14:53 +0200694 *
695 * Description: There has been a change in state which requires that the
696 * state machine runs.
697 */
698
Florian Fainellieab12772017-01-20 15:31:52 -0800699static void phy_trigger_machine(struct phy_device *phydev, bool sync)
Andrew Lunn3c293f42016-10-12 22:14:53 +0200700{
Florian Fainellieab12772017-01-20 15:31:52 -0800701 if (sync)
702 cancel_delayed_work_sync(&phydev->state_queue);
703 else
704 cancel_delayed_work(&phydev->state_queue);
Andrew Lunn3c293f42016-10-12 22:14:53 +0200705 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
706}
707
708/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800709 * phy_stop_machine - stop the PHY state machine tracking
710 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400711 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800712 * Description: Stops the state machine timer, sets the state to UP
Sergei Shtylylov817acf52006-07-26 00:53:53 +0400713 * (unless it wasn't up yet). This function must be called BEFORE
Andy Fleming00db8182005-07-30 19:31:23 -0400714 * phy_detach.
715 */
716void phy_stop_machine(struct phy_device *phydev)
717{
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700718 cancel_delayed_work_sync(&phydev->state_queue);
Andy Fleming00db8182005-07-30 19:31:23 -0400719
Nate Case35b5f6b2008-01-29 10:05:09 -0600720 mutex_lock(&phydev->lock);
Nathan Sullivan49d52e82017-03-22 15:27:01 -0500721 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
Andy Fleming00db8182005-07-30 19:31:23 -0400722 phydev->state = PHY_UP;
Nate Case35b5f6b2008-01-29 10:05:09 -0600723 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400724}
725
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800726/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800727 * phy_error - enter HALTED state for this PHY device
728 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400729 *
730 * Moves the PHY to the HALTED state in response to a read
731 * or write error, and tells the controller the link is down.
732 * Must not be called from interrupt context, or while the
733 * phydev->lock is held.
734 */
Andy Fleming9b9a8bf2008-05-02 13:00:51 -0500735static void phy_error(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400736{
Nate Case35b5f6b2008-01-29 10:05:09 -0600737 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400738 phydev->state = PHY_HALTED;
Nate Case35b5f6b2008-01-29 10:05:09 -0600739 mutex_unlock(&phydev->lock);
Andrew Lunn3c293f42016-10-12 22:14:53 +0200740
Florian Fainellieab12772017-01-20 15:31:52 -0800741 phy_trigger_machine(phydev, false);
Andy Fleming00db8182005-07-30 19:31:23 -0400742}
743
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800744/**
745 * phy_interrupt - PHY interrupt handler
746 * @irq: interrupt line
747 * @phy_dat: phy_device pointer
Andy Fleminge1393452005-08-24 18:46:21 -0500748 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800749 * Description: When a PHY interrupt occurs, the handler disables
Andrew Lunn664fcf12016-10-16 19:56:51 +0200750 * interrupts, and uses phy_change to handle the interrupt.
Andy Fleminge1393452005-08-24 18:46:21 -0500751 */
David Howells7d12e782006-10-05 14:55:46 +0100752static irqreturn_t phy_interrupt(int irq, void *phy_dat)
Andy Fleminge1393452005-08-24 18:46:21 -0500753{
754 struct phy_device *phydev = phy_dat;
755
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100756 if (PHY_HALTED == phydev->state)
757 return IRQ_NONE; /* It can't be ours. */
758
Andy Fleminge1393452005-08-24 18:46:21 -0500759 disable_irq_nosync(irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700760 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500761
Andrew Lunn664fcf12016-10-16 19:56:51 +0200762 phy_change(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500763
764 return IRQ_HANDLED;
765}
766
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800767/**
768 * phy_enable_interrupts - Enable the interrupts from the PHY side
769 * @phydev: target phy_device struct
770 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000771static int phy_enable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400772{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300773 int err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400774
Andy Fleminge1393452005-08-24 18:46:21 -0500775 if (err < 0)
776 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400777
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300778 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400779}
Andy Fleming00db8182005-07-30 19:31:23 -0400780
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800781/**
782 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
783 * @phydev: target phy_device struct
784 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000785static int phy_disable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400786{
787 int err;
788
789 /* Disable PHY interrupts */
790 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400791 if (err)
792 goto phy_err;
793
794 /* Clear the interrupt */
795 err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400796 if (err)
797 goto phy_err;
798
799 return 0;
800
801phy_err:
802 phy_error(phydev);
803
804 return err;
805}
Andy Fleminge1393452005-08-24 18:46:21 -0500806
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800807/**
808 * phy_start_interrupts - request and enable interrupts for a PHY device
809 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500810 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800811 * Description: Request the interrupt for the given PHY.
812 * If this fails, then we set irq to PHY_POLL.
Andy Fleminge1393452005-08-24 18:46:21 -0500813 * Otherwise, we enable the interrupts in the PHY.
Andy Fleminge1393452005-08-24 18:46:21 -0500814 * This should only be called with a valid IRQ number.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800815 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500816 */
817int phy_start_interrupts(struct phy_device *phydev)
818{
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700819 atomic_set(&phydev->irq_disable, 0);
Andrew Lunnc974bdb2016-10-16 19:56:50 +0200820 if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
821 IRQF_ONESHOT | IRQF_SHARED,
Andrew Lunnae0219c2016-10-16 19:56:52 +0200822 phydev_name(phydev), phydev) < 0) {
Joe Perches8d242482012-06-09 07:49:07 +0000823 pr_warn("%s: Can't get IRQ %d (PHY)\n",
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100824 phydev->mdio.bus->name, phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500825 phydev->irq = PHY_POLL;
826 return 0;
827 }
828
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300829 return phy_enable_interrupts(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500830}
831EXPORT_SYMBOL(phy_start_interrupts);
832
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800833/**
834 * phy_stop_interrupts - disable interrupts from a PHY device
835 * @phydev: target phy_device struct
836 */
Andy Fleminge1393452005-08-24 18:46:21 -0500837int phy_stop_interrupts(struct phy_device *phydev)
838{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300839 int err = phy_disable_interrupts(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500840
841 if (err)
842 phy_error(phydev);
843
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700844 free_irq(phydev->irq, phydev);
845
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300846 /* If work indeed has been cancelled, disable_irq() will have
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700847 * been left unbalanced from phy_interrupt() and enable_irq()
848 * has to be called so that other devices on the line work.
849 */
850 while (atomic_dec_return(&phydev->irq_disable) >= 0)
851 enable_irq(phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500852
853 return err;
854}
855EXPORT_SYMBOL(phy_stop_interrupts);
856
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800857/**
Andrew Lunn664fcf12016-10-16 19:56:51 +0200858 * phy_change - Called by the phy_interrupt to handle PHY changes
859 * @phydev: phy_device struct that interrupted
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800860 */
Andrew Lunn664fcf12016-10-16 19:56:51 +0200861void phy_change(struct phy_device *phydev)
Andy Fleminge1393452005-08-24 18:46:21 -0500862{
Florian Fainellideccd16f92016-01-18 19:33:07 -0800863 if (phy_interrupt_is_valid(phydev)) {
864 if (phydev->drv->did_interrupt &&
865 !phydev->drv->did_interrupt(phydev))
866 goto ignore;
Anatolij Gustschina8729eb2009-04-07 02:01:42 +0000867
Florian Fainellideccd16f92016-01-18 19:33:07 -0800868 if (phy_disable_interrupts(phydev))
869 goto phy_err;
870 }
Andy Fleminge1393452005-08-24 18:46:21 -0500871
Nate Case35b5f6b2008-01-29 10:05:09 -0600872 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500873 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
874 phydev->state = PHY_CHANGELINK;
Nate Case35b5f6b2008-01-29 10:05:09 -0600875 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500876
Florian Fainellideccd16f92016-01-18 19:33:07 -0800877 if (phy_interrupt_is_valid(phydev)) {
878 atomic_dec(&phydev->irq_disable);
879 enable_irq(phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500880
Florian Fainellideccd16f92016-01-18 19:33:07 -0800881 /* Reenable interrupts */
882 if (PHY_HALTED != phydev->state &&
883 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
884 goto irq_enable_err;
885 }
Andy Fleminge1393452005-08-24 18:46:21 -0500886
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700887 /* reschedule state queue work to run as soon as possible */
Florian Fainellieab12772017-01-20 15:31:52 -0800888 phy_trigger_machine(phydev, true);
Andy Fleminge1393452005-08-24 18:46:21 -0500889 return;
890
Anatolij Gustschina8729eb2009-04-07 02:01:42 +0000891ignore:
892 atomic_dec(&phydev->irq_disable);
893 enable_irq(phydev->irq);
894 return;
895
Andy Fleminge1393452005-08-24 18:46:21 -0500896irq_enable_err:
897 disable_irq(phydev->irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700898 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500899phy_err:
900 phy_error(phydev);
901}
902
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800903/**
Andrew Lunn664fcf12016-10-16 19:56:51 +0200904 * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
905 * @work: work_struct that describes the work to be done
906 */
907void phy_change_work(struct work_struct *work)
908{
909 struct phy_device *phydev =
910 container_of(work, struct phy_device, phy_queue);
911
912 phy_change(phydev);
913}
914
915/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800916 * phy_stop - Bring down the PHY link, and stop checking the status
917 * @phydev: target phy_device struct
918 */
Andy Fleminge1393452005-08-24 18:46:21 -0500919void phy_stop(struct phy_device *phydev)
920{
Nate Case35b5f6b2008-01-29 10:05:09 -0600921 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500922
923 if (PHY_HALTED == phydev->state)
924 goto out_unlock;
925
Florian Fainelli2c7b4922013-05-19 22:53:42 +0000926 if (phy_interrupt_is_valid(phydev)) {
Andy Fleminge1393452005-08-24 18:46:21 -0500927 /* Disable PHY Interrupts */
928 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleminge1393452005-08-24 18:46:21 -0500929
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100930 /* Clear any pending interrupts */
931 phy_clear_interrupt(phydev);
932 }
Andy Fleminge1393452005-08-24 18:46:21 -0500933
Maciej W. Rozycki6daf6532007-09-28 22:42:15 -0700934 phydev->state = PHY_HALTED;
935
Andy Fleminge1393452005-08-24 18:46:21 -0500936out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600937 mutex_unlock(&phydev->lock);
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100938
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300939 /* Cannot call flush_scheduled_work() here as desired because
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100940 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
941 * will not reenable interrupts.
942 */
Andy Fleminge1393452005-08-24 18:46:21 -0500943}
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300944EXPORT_SYMBOL(phy_stop);
Andy Fleminge1393452005-08-24 18:46:21 -0500945
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800946/**
947 * phy_start - start or restart a PHY device
948 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500949 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800950 * Description: Indicates the attached device's readiness to
Andy Fleminge1393452005-08-24 18:46:21 -0500951 * handle PHY-related work. Used during startup to start the
952 * PHY, and after a call to phy_stop() to resume operation.
953 * Also used to indicate the MDIO bus has cleared an error
954 * condition.
955 */
956void phy_start(struct phy_device *phydev)
957{
Tim Bealec15e10e2015-05-18 15:38:38 +1200958 bool do_resume = false;
959 int err = 0;
960
Nate Case35b5f6b2008-01-29 10:05:09 -0600961 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500962
963 switch (phydev->state) {
Florian Fainellie1093742013-12-17 21:38:12 -0800964 case PHY_STARTING:
965 phydev->state = PHY_PENDING;
966 break;
967 case PHY_READY:
968 phydev->state = PHY_UP;
969 break;
970 case PHY_HALTED:
Tim Bealec15e10e2015-05-18 15:38:38 +1200971 /* make sure interrupts are re-enabled for the PHY */
Shaohui Xie84a527a2016-05-10 17:42:26 +0800972 if (phydev->irq != PHY_POLL) {
973 err = phy_enable_interrupts(phydev);
974 if (err < 0)
975 break;
976 }
Tim Bealec15e10e2015-05-18 15:38:38 +1200977
Florian Fainellie1093742013-12-17 21:38:12 -0800978 phydev->state = PHY_RESUMING;
Tim Bealec15e10e2015-05-18 15:38:38 +1200979 do_resume = true;
980 break;
Florian Fainellie1093742013-12-17 21:38:12 -0800981 default:
982 break;
Andy Fleminge1393452005-08-24 18:46:21 -0500983 }
Nate Case35b5f6b2008-01-29 10:05:09 -0600984 mutex_unlock(&phydev->lock);
Tim Bealec15e10e2015-05-18 15:38:38 +1200985
986 /* if phy was suspended, bring the physical link up again */
987 if (do_resume)
988 phy_resume(phydev);
Andrew Lunn3c293f42016-10-12 22:14:53 +0200989
Florian Fainellieab12772017-01-20 15:31:52 -0800990 phy_trigger_machine(phydev, true);
Andy Fleminge1393452005-08-24 18:46:21 -0500991}
Andy Fleminge1393452005-08-24 18:46:21 -0500992EXPORT_SYMBOL(phy_start);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400993
Zach Brown61a17962016-10-17 10:49:53 -0500994static void phy_adjust_link(struct phy_device *phydev)
995{
996 phydev->adjust_link(phydev->attached_dev);
Zach Brown2e0bc452016-10-17 10:49:55 -0500997 phy_led_trigger_change_speed(phydev);
Zach Brown61a17962016-10-17 10:49:53 -0500998}
999
Nate Case35b5f6b2008-01-29 10:05:09 -06001000/**
1001 * phy_state_machine - Handle the state machine
1002 * @work: work_struct that describes the work to be done
Nate Case35b5f6b2008-01-29 10:05:09 -06001003 */
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +00001004void phy_state_machine(struct work_struct *work)
Andy Fleming00db8182005-07-30 19:31:23 -04001005{
Jean Delvarebf6aede2009-04-02 16:56:54 -07001006 struct delayed_work *dwork = to_delayed_work(work);
Nate Case35b5f6b2008-01-29 10:05:09 -06001007 struct phy_device *phydev =
Marcin Slusarza390d1f2009-03-13 15:41:19 -07001008 container_of(dwork, struct phy_device, state_queue);
Tim Bealec15e10e2015-05-18 15:38:38 +12001009 bool needs_aneg = false, do_suspend = false;
Florian Fainelli3e2186e2015-05-16 10:17:56 -07001010 enum phy_state old_state;
Andy Fleming00db8182005-07-30 19:31:23 -04001011 int err = 0;
Shaohui Xie11e122c2015-08-14 12:23:40 +08001012 int old_link;
Andy Fleming00db8182005-07-30 19:31:23 -04001013
Nate Case35b5f6b2008-01-29 10:05:09 -06001014 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001015
Florian Fainelli3e2186e2015-05-16 10:17:56 -07001016 old_state = phydev->state;
1017
Florian Fainelli25149ef2017-02-17 16:07:34 -08001018 if (phydev->drv && phydev->drv->link_change_notify)
Daniel Mack2b8f2a22014-06-18 11:01:41 +02001019 phydev->drv->link_change_notify(phydev);
1020
Florian Fainellie1093742013-12-17 21:38:12 -08001021 switch (phydev->state) {
1022 case PHY_DOWN:
1023 case PHY_STARTING:
1024 case PHY_READY:
1025 case PHY_PENDING:
1026 break;
1027 case PHY_UP:
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001028 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001029
1030 phydev->link_timeout = PHY_AN_TIMEOUT;
1031
1032 break;
1033 case PHY_AN:
1034 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001035 if (err < 0)
Andy Fleming00db8182005-07-30 19:31:23 -04001036 break;
Florian Fainellie1093742013-12-17 21:38:12 -08001037
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001038 /* If the link is down, give up on negotiation for now */
Florian Fainellie1093742013-12-17 21:38:12 -08001039 if (!phydev->link) {
1040 phydev->state = PHY_NOLINK;
1041 netif_carrier_off(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001042 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001043 break;
1044 }
1045
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001046 /* Check if negotiation is done. Break if there's an error */
Florian Fainellie1093742013-12-17 21:38:12 -08001047 err = phy_aneg_done(phydev);
1048 if (err < 0)
1049 break;
1050
1051 /* If AN is done, we're running */
1052 if (err > 0) {
1053 phydev->state = PHY_RUNNING;
1054 netif_carrier_on(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001055 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001056
Balakumaran Kannanfa8cdda2014-04-09 09:03:45 +05301057 } else if (0 == phydev->link_timeout--)
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001058 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001059 break;
1060 case PHY_NOLINK:
Andrew Lunn321beec2015-11-16 23:36:46 +01001061 if (phy_interrupt_is_valid(phydev))
1062 break;
1063
Florian Fainellie1093742013-12-17 21:38:12 -08001064 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001065 if (err)
Andy Fleming00db8182005-07-30 19:31:23 -04001066 break;
Andy Fleming6b655522006-10-16 16:19:17 -05001067
Florian Fainellie1093742013-12-17 21:38:12 -08001068 if (phydev->link) {
Balakumaran Kannane46e08b2014-04-24 08:22:47 +05301069 if (AUTONEG_ENABLE == phydev->autoneg) {
1070 err = phy_aneg_done(phydev);
1071 if (err < 0)
1072 break;
1073
1074 if (!err) {
1075 phydev->state = PHY_AN;
1076 phydev->link_timeout = PHY_AN_TIMEOUT;
1077 break;
1078 }
1079 }
Florian Fainellie1093742013-12-17 21:38:12 -08001080 phydev->state = PHY_RUNNING;
1081 netif_carrier_on(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001082 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001083 }
1084 break;
1085 case PHY_FORCING:
1086 err = genphy_update_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001087 if (err)
1088 break;
Andy Fleming6b655522006-10-16 16:19:17 -05001089
Florian Fainellie1093742013-12-17 21:38:12 -08001090 if (phydev->link) {
1091 phydev->state = PHY_RUNNING;
1092 netif_carrier_on(phydev->attached_dev);
1093 } else {
1094 if (0 == phydev->link_timeout--)
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001095 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001096 }
1097
Zach Brown61a17962016-10-17 10:49:53 -05001098 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001099 break;
1100 case PHY_RUNNING:
Florian Fainellid5c3d842016-01-18 19:33:06 -08001101 /* Only register a CHANGE if we are polling and link changed
1102 * since latest checking.
Florian Fainellie1093742013-12-17 21:38:12 -08001103 */
Florian Fainellid5c3d842016-01-18 19:33:06 -08001104 if (phydev->irq == PHY_POLL) {
Shaohui Xie11e122c2015-08-14 12:23:40 +08001105 old_link = phydev->link;
1106 err = phy_read_status(phydev);
1107 if (err)
1108 break;
1109
1110 if (old_link != phydev->link)
1111 phydev->state = PHY_CHANGELINK;
1112 }
Zefir Kurtisi811a9192017-01-06 12:14:48 +01001113 /*
1114 * Failsafe: check that nobody set phydev->link=0 between two
1115 * poll cycles, otherwise we won't leave RUNNING state as long
1116 * as link remains down.
1117 */
1118 if (!phydev->link && phydev->state == PHY_RUNNING) {
1119 phydev->state = PHY_CHANGELINK;
1120 phydev_err(phydev, "no link in PHY_RUNNING\n");
1121 }
Florian Fainellie1093742013-12-17 21:38:12 -08001122 break;
1123 case PHY_CHANGELINK:
1124 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001125 if (err)
1126 break;
1127
1128 if (phydev->link) {
1129 phydev->state = PHY_RUNNING;
1130 netif_carrier_on(phydev->attached_dev);
1131 } else {
1132 phydev->state = PHY_NOLINK;
1133 netif_carrier_off(phydev->attached_dev);
1134 }
1135
Zach Brown61a17962016-10-17 10:49:53 -05001136 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001137
1138 if (phy_interrupt_is_valid(phydev))
1139 err = phy_config_interrupt(phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001140 PHY_INTERRUPT_ENABLED);
Florian Fainellie1093742013-12-17 21:38:12 -08001141 break;
1142 case PHY_HALTED:
1143 if (phydev->link) {
1144 phydev->link = 0;
1145 netif_carrier_off(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001146 phy_adjust_link(phydev);
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001147 do_suspend = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001148 }
1149 break;
1150 case PHY_RESUMING:
Florian Fainellie1093742013-12-17 21:38:12 -08001151 if (AUTONEG_ENABLE == phydev->autoneg) {
Andy Fleming00db8182005-07-30 19:31:23 -04001152 err = phy_aneg_done(phydev);
1153 if (err < 0)
1154 break;
1155
Florian Fainellie1093742013-12-17 21:38:12 -08001156 /* err > 0 if AN is done.
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001157 * Otherwise, it's 0, and we're still waiting for AN
1158 */
Andy Fleming00db8182005-07-30 19:31:23 -04001159 if (err > 0) {
Wade Farnsworth42caa072009-07-01 13:00:34 +00001160 err = phy_read_status(phydev);
1161 if (err)
1162 break;
1163
1164 if (phydev->link) {
1165 phydev->state = PHY_RUNNING;
1166 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001167 } else {
Wade Farnsworth42caa072009-07-01 13:00:34 +00001168 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001169 }
Zach Brown61a17962016-10-17 10:49:53 -05001170 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001171 } else {
1172 phydev->state = PHY_AN;
1173 phydev->link_timeout = PHY_AN_TIMEOUT;
Wade Farnsworth42caa072009-07-01 13:00:34 +00001174 }
Florian Fainellie1093742013-12-17 21:38:12 -08001175 } else {
1176 err = phy_read_status(phydev);
1177 if (err)
1178 break;
1179
1180 if (phydev->link) {
1181 phydev->state = PHY_RUNNING;
1182 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001183 } else {
Florian Fainellie1093742013-12-17 21:38:12 -08001184 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001185 }
Zach Brown61a17962016-10-17 10:49:53 -05001186 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001187 }
1188 break;
Andy Fleming00db8182005-07-30 19:31:23 -04001189 }
1190
Nate Case35b5f6b2008-01-29 10:05:09 -06001191 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001192
1193 if (needs_aneg)
1194 err = phy_start_aneg(phydev);
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001195 else if (do_suspend)
Sebastian Hesselbarthbe9dad12013-12-13 10:20:29 +01001196 phy_suspend(phydev);
1197
Andy Fleming00db8182005-07-30 19:31:23 -04001198 if (err < 0)
1199 phy_error(phydev);
1200
Andrew Lunn72ba48b2016-01-06 20:11:09 +01001201 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1202 phy_state_to_str(old_state),
1203 phy_state_to_str(phydev->state));
Florian Fainelli3e2186e2015-05-16 10:17:56 -07001204
Florian Fainellid5c3d842016-01-18 19:33:06 -08001205 /* Only re-schedule a PHY state machine change if we are polling the
1206 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1207 * between states from phy_mac_interrupt()
1208 */
1209 if (phydev->irq == PHY_POLL)
1210 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1211 PHY_STATE_TIME * HZ);
Nate Case35b5f6b2008-01-29 10:05:09 -06001212}
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001213
Andrew Lunn664fcf12016-10-16 19:56:51 +02001214/**
1215 * phy_mac_interrupt - MAC says the link has changed
1216 * @phydev: phy_device struct with changed link
1217 * @new_link: Link is Up/Down.
1218 *
1219 * Description: The MAC layer is able indicate there has been a change
1220 * in the PHY link status. Set the new link status, and trigger the
1221 * state machine, work a work queue.
1222 */
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001223void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1224{
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001225 phydev->link = new_link;
Florian Fainellideccd16f92016-01-18 19:33:07 -08001226
1227 /* Trigger a state machine change */
1228 queue_work(system_power_efficient_wq, &phydev->phy_queue);
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001229}
1230EXPORT_SYMBOL(phy_mac_interrupt);
1231
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001232/**
1233 * phy_init_eee - init and check the EEE feature
1234 * @phydev: target phy_device struct
1235 * @clk_stop_enable: PHY may stop the clock during LPI
1236 *
1237 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1238 * is supported by looking at the MMD registers 3.20 and 7.60/61
1239 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1240 * bit if required.
1241 */
1242int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1243{
Florian Fainelli25149ef2017-02-17 16:07:34 -08001244 if (!phydev->drv)
1245 return -EIO;
1246
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001247 /* According to 802.3az,the EEE is supported only in full duplex-mode.
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001248 */
Russell King32d75142017-03-31 10:37:18 +01001249 if (phydev->duplex == DUPLEX_FULL) {
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001250 int eee_lp, eee_cap, eee_adv;
1251 u32 lp, cap, adv;
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -07001252 int status;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001253
1254 /* Read phy status to properly get the right settings */
1255 status = phy_read_status(phydev);
1256 if (status)
1257 return status;
1258
1259 /* First check if the EEE ability is supported */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001260 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001261 if (eee_cap <= 0)
1262 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001263
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001264 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001265 if (!cap)
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001266 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001267
1268 /* Check which link settings negotiated and verify it in
1269 * the EEE advertising registers.
1270 */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001271 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001272 if (eee_lp <= 0)
1273 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001274
Russell Kinga6d99fc2017-03-21 16:36:53 +00001275 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001276 if (eee_adv <= 0)
1277 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001278
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001279 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1280 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
Guenter Roeck54da5a82015-02-17 09:36:22 -08001281 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001282 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001283
1284 if (clk_stop_enable) {
1285 /* Configure the PHY to stop receiving xMII
1286 * clock while it is signaling LPI.
1287 */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001288 int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001289 if (val < 0)
1290 return val;
1291
1292 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
Russell Kinga6d99fc2017-03-21 16:36:53 +00001293 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001294 }
1295
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001296 return 0; /* EEE supported */
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001297 }
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001298eee_exit_err:
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001299 return -EPROTONOSUPPORT;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001300}
1301EXPORT_SYMBOL(phy_init_eee);
1302
1303/**
1304 * phy_get_eee_err - report the EEE wake error count
1305 * @phydev: target phy_device struct
1306 *
1307 * Description: it is to report the number of time where the PHY
1308 * failed to complete its normal wake sequence.
1309 */
1310int phy_get_eee_err(struct phy_device *phydev)
1311{
Florian Fainelli25149ef2017-02-17 16:07:34 -08001312 if (!phydev->drv)
1313 return -EIO;
1314
Russell Kinga6d99fc2017-03-21 16:36:53 +00001315 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001316}
1317EXPORT_SYMBOL(phy_get_eee_err);
1318
1319/**
1320 * phy_ethtool_get_eee - get EEE supported and status
1321 * @phydev: target phy_device struct
1322 * @data: ethtool_eee data
1323 *
1324 * Description: it reportes the Supported/Advertisement/LP Advertisement
1325 * capabilities.
1326 */
1327int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1328{
1329 int val;
1330
Florian Fainelli25149ef2017-02-17 16:07:34 -08001331 if (!phydev->drv)
1332 return -EIO;
1333
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001334 /* Get Supported EEE */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001335 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001336 if (val < 0)
1337 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001338 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001339
1340 /* Get advertisement EEE */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001341 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001342 if (val < 0)
1343 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001344 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001345
1346 /* Get LP advertisement EEE */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001347 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001348 if (val < 0)
1349 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001350 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001351
1352 return 0;
1353}
1354EXPORT_SYMBOL(phy_ethtool_get_eee);
1355
1356/**
1357 * phy_ethtool_set_eee - set EEE supported and status
1358 * @phydev: target phy_device struct
1359 * @data: ethtool_eee data
1360 *
1361 * Description: it is to program the Advertisement EEE register.
1362 */
1363int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1364{
Russell Kingf75abeb2017-03-31 10:37:12 +01001365 int cap, old_adv, adv, ret;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001366
Florian Fainelli25149ef2017-02-17 16:07:34 -08001367 if (!phydev->drv)
1368 return -EIO;
1369
Russell King83ea0672017-03-31 10:37:07 +01001370 /* Get Supported EEE */
1371 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1372 if (cap < 0)
1373 return cap;
1374
Russell Kingf75abeb2017-03-31 10:37:12 +01001375 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1376 if (old_adv < 0)
1377 return old_adv;
1378
Russell King83ea0672017-03-31 10:37:07 +01001379 adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1380
jbrunetd853d142016-11-28 10:46:46 +01001381 /* Mask prohibited EEE modes */
Russell King83ea0672017-03-31 10:37:07 +01001382 adv &= ~phydev->eee_broken_modes;
jbrunetd853d142016-11-28 10:46:46 +01001383
Russell Kingf75abeb2017-03-31 10:37:12 +01001384 if (old_adv != adv) {
1385 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1386 if (ret < 0)
1387 return ret;
1388
1389 /* Restart autonegotiation so the new modes get sent to the
1390 * link partner.
1391 */
1392 ret = genphy_restart_aneg(phydev);
1393 if (ret < 0)
1394 return ret;
1395 }
1396
1397 return 0;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001398}
1399EXPORT_SYMBOL(phy_ethtool_set_eee);
Michael Stapelberg42e836e2013-03-11 13:56:44 +00001400
1401int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1402{
Florian Fainelli25149ef2017-02-17 16:07:34 -08001403 if (phydev->drv && phydev->drv->set_wol)
Michael Stapelberg42e836e2013-03-11 13:56:44 +00001404 return phydev->drv->set_wol(phydev, wol);
1405
1406 return -EOPNOTSUPP;
1407}
1408EXPORT_SYMBOL(phy_ethtool_set_wol);
1409
1410void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1411{
Florian Fainelli25149ef2017-02-17 16:07:34 -08001412 if (phydev->drv && phydev->drv->get_wol)
Michael Stapelberg42e836e2013-03-11 13:56:44 +00001413 phydev->drv->get_wol(phydev, wol);
1414}
1415EXPORT_SYMBOL(phy_ethtool_get_wol);
Philippe Reynes9d9a77c2016-05-10 00:19:41 +02001416
1417int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1418 struct ethtool_link_ksettings *cmd)
1419{
1420 struct phy_device *phydev = ndev->phydev;
1421
1422 if (!phydev)
1423 return -ENODEV;
1424
1425 return phy_ethtool_ksettings_get(phydev, cmd);
1426}
1427EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1428
1429int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1430 const struct ethtool_link_ksettings *cmd)
1431{
1432 struct phy_device *phydev = ndev->phydev;
1433
1434 if (!phydev)
1435 return -ENODEV;
1436
1437 return phy_ethtool_ksettings_set(phydev, cmd);
1438}
1439EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
Florian Fainellie86a8982016-11-15 10:06:30 -08001440
1441int phy_ethtool_nway_reset(struct net_device *ndev)
1442{
1443 struct phy_device *phydev = ndev->phydev;
1444
1445 if (!phydev)
1446 return -ENODEV;
1447
Florian Fainelli25149ef2017-02-17 16:07:34 -08001448 if (!phydev->drv)
1449 return -EIO;
1450
Florian Fainellie86a8982016-11-15 10:06:30 -08001451 return genphy_restart_aneg(phydev);
1452}
1453EXPORT_SYMBOL(phy_ethtool_nway_reset);