blob: c232ee04754bde2f3cbd094e5ea8024fe3cf7f7b [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
Russell King41408ad2017-06-05 12:22:55 +0100166 /* Avoid genphy_aneg_done() if the Clause 45 PHY does not
167 * implement Clause 22 registers
168 */
169 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
170 return -EINVAL;
171
Florian Fainellia9fa6e62014-02-11 17:27:36 -0800172 return genphy_aneg_done(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400173}
Lendacky, Thomas372788f2016-11-10 17:10:46 -0600174EXPORT_SYMBOL(phy_aneg_done);
Andy Fleming00db8182005-07-30 19:31:23 -0400175
Andy Fleming00db8182005-07-30 19:31:23 -0400176/* A structure for mapping a particular speed and duplex
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300177 * combination to a particular SUPPORTED and ADVERTISED value
178 */
Andy Fleming00db8182005-07-30 19:31:23 -0400179struct phy_setting {
180 int speed;
181 int duplex;
182 u32 setting;
183};
184
Russell Kingd0613032017-04-13 16:49:15 +0100185/* A mapping of all SUPPORTED settings to speed/duplex. This table
186 * must be grouped by speed and sorted in descending match priority
187 * - iow, descending speed. */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500188static const struct phy_setting settings[] = {
Andy Fleming00db8182005-07-30 19:31:23 -0400189 {
Lendacky, Thomas3e707702014-07-14 14:05:46 -0500190 .speed = SPEED_10000,
191 .duplex = DUPLEX_FULL,
192 .setting = SUPPORTED_10000baseKR_Full,
193 },
194 {
195 .speed = SPEED_10000,
196 .duplex = DUPLEX_FULL,
197 .setting = SUPPORTED_10000baseKX4_Full,
198 },
199 {
200 .speed = SPEED_10000,
Andy Fleming00db8182005-07-30 19:31:23 -0400201 .duplex = DUPLEX_FULL,
202 .setting = SUPPORTED_10000baseT_Full,
203 },
204 {
Lendacky, Thomas3e707702014-07-14 14:05:46 -0500205 .speed = SPEED_2500,
206 .duplex = DUPLEX_FULL,
207 .setting = SUPPORTED_2500baseX_Full,
208 },
209 {
210 .speed = SPEED_1000,
211 .duplex = DUPLEX_FULL,
212 .setting = SUPPORTED_1000baseKX_Full,
213 },
214 {
Andy Fleming00db8182005-07-30 19:31:23 -0400215 .speed = SPEED_1000,
216 .duplex = DUPLEX_FULL,
217 .setting = SUPPORTED_1000baseT_Full,
218 },
219 {
220 .speed = SPEED_1000,
221 .duplex = DUPLEX_HALF,
222 .setting = SUPPORTED_1000baseT_Half,
223 },
224 {
225 .speed = SPEED_100,
226 .duplex = DUPLEX_FULL,
227 .setting = SUPPORTED_100baseT_Full,
228 },
229 {
230 .speed = SPEED_100,
231 .duplex = DUPLEX_HALF,
232 .setting = SUPPORTED_100baseT_Half,
233 },
234 {
235 .speed = SPEED_10,
236 .duplex = DUPLEX_FULL,
237 .setting = SUPPORTED_10baseT_Full,
238 },
239 {
240 .speed = SPEED_10,
241 .duplex = DUPLEX_HALF,
242 .setting = SUPPORTED_10baseT_Half,
243 },
244};
245
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800246/**
Russell Kingd0613032017-04-13 16:49:15 +0100247 * phy_lookup_setting - lookup a PHY setting
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800248 * @speed: speed to match
249 * @duplex: duplex to match
Russell Kingd0613032017-04-13 16:49:15 +0100250 * @feature: allowed link modes
251 * @exact: an exact match is required
Andy Fleming00db8182005-07-30 19:31:23 -0400252 *
Russell Kingd0613032017-04-13 16:49:15 +0100253 * Search the settings array for a setting that matches the speed and
254 * duplex, and which is supported.
255 *
256 * If @exact is unset, either an exact match or %NULL for no match will
257 * be returned.
258 *
259 * If @exact is set, an exact match, the fastest supported setting at
260 * or below the specified speed, the slowest supported setting, or if
261 * they all fail, %NULL will be returned.
Andy Fleming00db8182005-07-30 19:31:23 -0400262 */
Russell Kingd0613032017-04-13 16:49:15 +0100263static const struct phy_setting *
264phy_lookup_setting(int speed, int duplex, u32 features, bool exact)
Andy Fleming00db8182005-07-30 19:31:23 -0400265{
Russell Kingd0613032017-04-13 16:49:15 +0100266 const struct phy_setting *p, *match = NULL, *last = NULL;
267 int i;
Andy Fleming00db8182005-07-30 19:31:23 -0400268
Russell Kingd0613032017-04-13 16:49:15 +0100269 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
270 if (p->setting & features) {
271 last = p;
272 if (p->speed == speed && p->duplex == duplex) {
273 /* Exact match for speed and duplex */
274 match = p;
275 break;
276 } else if (!exact) {
277 if (!match && p->speed <= speed)
278 /* Candidate */
279 match = p;
Andy Fleming00db8182005-07-30 19:31:23 -0400280
Russell Kingd0613032017-04-13 16:49:15 +0100281 if (p->speed < speed)
282 break;
283 }
284 }
285 }
286
287 if (!match && !exact)
288 match = last;
289
290 return match;
Andy Fleming00db8182005-07-30 19:31:23 -0400291}
292
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800293/**
Russell Kingd0613032017-04-13 16:49:15 +0100294 * phy_find_valid - find a PHY setting that matches the requested parameters
295 * @speed: desired speed
296 * @duplex: desired duplex
297 * @supported: mask of supported link modes
Andy Fleming00db8182005-07-30 19:31:23 -0400298 *
Russell Kingd0613032017-04-13 16:49:15 +0100299 * Locate a supported phy setting that is, in priority order:
300 * - an exact match for the specified speed and duplex mode
301 * - a match for the specified speed, or slower speed
302 * - the slowest supported speed
303 * Returns the matched phy_setting entry, or %NULL if no supported phy
304 * settings were found.
Andy Fleming00db8182005-07-30 19:31:23 -0400305 */
Russell Kingd0613032017-04-13 16:49:15 +0100306static const struct phy_setting *
307phy_find_valid(int speed, int duplex, u32 supported)
Andy Fleming00db8182005-07-30 19:31:23 -0400308{
Russell Kingd0613032017-04-13 16:49:15 +0100309 return phy_lookup_setting(speed, duplex, supported, false);
Andy Fleming00db8182005-07-30 19:31:23 -0400310}
311
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800312/**
Zach Brown1f9127c2016-10-17 10:49:54 -0500313 * phy_supported_speeds - return all speeds currently supported by a phy device
314 * @phy: The phy device to return supported speeds of.
315 * @speeds: buffer to store supported speeds in.
316 * @size: size of speeds buffer.
317 *
318 * Description: Returns the number of supported speeds, and fills the speeds
319 * buffer with the supported speeds. If speeds buffer is too small to contain
320 * all currently supported speeds, will return as many speeds as can fit.
321 */
322unsigned int phy_supported_speeds(struct phy_device *phy,
323 unsigned int *speeds,
324 unsigned int size)
325{
326 unsigned int count = 0;
327 unsigned int idx = 0;
328
Russell King786df9c2017-04-13 16:49:20 +0100329 for (idx = 0; idx < ARRAY_SIZE(settings) && count < size; idx++)
Zach Brown1f9127c2016-10-17 10:49:54 -0500330 /* Assumes settings are grouped by speed */
Russell King786df9c2017-04-13 16:49:20 +0100331 if ((settings[idx].setting & phy->supported) &&
332 (count == 0 || speeds[count - 1] != settings[idx].speed))
333 speeds[count++] = settings[idx].speed;
Zach Brown1f9127c2016-10-17 10:49:54 -0500334
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
Philippe Reynes2d551732016-04-15 00:35:00 +0200493int phy_ethtool_ksettings_get(struct phy_device *phydev,
494 struct ethtool_link_ksettings *cmd)
495{
496 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
497 phydev->supported);
498
499 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
500 phydev->advertising);
501
502 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
503 phydev->lp_advertising);
504
505 cmd->base.speed = phydev->speed;
506 cmd->base.duplex = phydev->duplex;
507 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
508 cmd->base.port = PORT_BNC;
509 else
510 cmd->base.port = PORT_MII;
511
512 cmd->base.phy_address = phydev->mdio.addr;
513 cmd->base.autoneg = phydev->autoneg;
Raju Lakkaraju1004ee62016-11-29 15:16:47 +0530514 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
515 cmd->base.eth_tp_mdix = phydev->mdix;
Philippe Reynes2d551732016-04-15 00:35:00 +0200516
517 return 0;
518}
519EXPORT_SYMBOL(phy_ethtool_ksettings_get);
520
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800521/**
522 * phy_mii_ioctl - generic PHY MII ioctl interface
523 * @phydev: the phy_device struct
Randy Dunlap00c7d922010-08-09 13:41:59 +0000524 * @ifr: &struct ifreq for socket ioctl's
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800525 * @cmd: ioctl cmd to execute
526 *
527 * Note that this function is currently incompatible with the
Andy Fleming00db8182005-07-30 19:31:23 -0400528 * PHYCONTROL layer. It changes registers without regard to
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800529 * current state. Use at own risk.
Andy Fleming00db8182005-07-30 19:31:23 -0400530 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300531int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
Andy Fleming00db8182005-07-30 19:31:23 -0400532{
Richard Cochran28b04112010-07-17 08:48:55 +0000533 struct mii_ioctl_data *mii_data = if_mii(ifr);
Andy Fleming00db8182005-07-30 19:31:23 -0400534 u16 val = mii_data->val_in;
Brian Hill79ce0472014-11-11 13:39:39 -0700535 bool change_autoneg = false;
Andy Fleming00db8182005-07-30 19:31:23 -0400536
537 switch (cmd) {
538 case SIOCGMIIPHY:
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100539 mii_data->phy_id = phydev->mdio.addr;
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000540 /* fall through */
541
Andy Fleming00db8182005-07-30 19:31:23 -0400542 case SIOCGMIIREG:
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100543 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
544 mii_data->phy_id,
Peter Korsgaardaf1dc132011-03-10 06:52:13 +0000545 mii_data->reg_num);
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300546 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400547
548 case SIOCSMIIREG:
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100549 if (mii_data->phy_id == phydev->mdio.addr) {
Florian Fainellie1093742013-12-17 21:38:12 -0800550 switch (mii_data->reg_num) {
Andy Fleming00db8182005-07-30 19:31:23 -0400551 case MII_BMCR:
Brian Hill79ce0472014-11-11 13:39:39 -0700552 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
553 if (phydev->autoneg == AUTONEG_ENABLE)
554 change_autoneg = true;
Andy Fleming00db8182005-07-30 19:31:23 -0400555 phydev->autoneg = AUTONEG_DISABLE;
Brian Hill79ce0472014-11-11 13:39:39 -0700556 if (val & BMCR_FULLDPLX)
557 phydev->duplex = DUPLEX_FULL;
558 else
559 phydev->duplex = DUPLEX_HALF;
560 if (val & BMCR_SPEED1000)
561 phydev->speed = SPEED_1000;
562 else if (val & BMCR_SPEED100)
563 phydev->speed = SPEED_100;
564 else phydev->speed = SPEED_10;
565 }
566 else {
567 if (phydev->autoneg == AUTONEG_DISABLE)
568 change_autoneg = true;
Andy Fleming00db8182005-07-30 19:31:23 -0400569 phydev->autoneg = AUTONEG_ENABLE;
Brian Hill79ce0472014-11-11 13:39:39 -0700570 }
Andy Fleming00db8182005-07-30 19:31:23 -0400571 break;
572 case MII_ADVERTISE:
Brian Hill79ce0472014-11-11 13:39:39 -0700573 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
574 change_autoneg = true;
Andy Fleming00db8182005-07-30 19:31:23 -0400575 break;
576 default:
577 /* do nothing */
578 break;
579 }
580 }
581
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100582 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
Peter Korsgaardaf1dc132011-03-10 06:52:13 +0000583 mii_data->reg_num, val);
584
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100585 if (mii_data->phy_id == phydev->mdio.addr &&
Jérôme Pouillercf18b772015-12-03 10:02:35 +0100586 mii_data->reg_num == MII_BMCR &&
Florian Fainelli2613f952013-12-06 13:01:31 -0800587 val & BMCR_RESET)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300588 return phy_init_hw(phydev);
Brian Hill79ce0472014-11-11 13:39:39 -0700589
590 if (change_autoneg)
591 return phy_start_aneg(phydev);
592
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300593 return 0;
David Woodhousedda93b482007-11-28 19:56:34 +0000594
Richard Cochranc1f19b52010-07-17 08:49:36 +0000595 case SIOCSHWTSTAMP:
Florian Fainelli25149ef2017-02-17 16:07:34 -0800596 if (phydev->drv && phydev->drv->hwtstamp)
Richard Cochranc1f19b52010-07-17 08:49:36 +0000597 return phydev->drv->hwtstamp(phydev, ifr);
598 /* fall through */
599
David Woodhousedda93b482007-11-28 19:56:34 +0000600 default:
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000601 return -EOPNOTSUPP;
Andy Fleming00db8182005-07-30 19:31:23 -0400602 }
Andy Fleming00db8182005-07-30 19:31:23 -0400603}
Domen Puncer680e9fe2007-09-17 22:21:40 +0200604EXPORT_SYMBOL(phy_mii_ioctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400605
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800606/**
Alexander Kochetkovf555f342017-04-20 14:00:04 +0300607 * phy_start_aneg_priv - start auto-negotiation for this PHY device
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800608 * @phydev: the phy_device struct
Alexander Kochetkovf555f342017-04-20 14:00:04 +0300609 * @sync: indicate whether we should wait for the workqueue cancelation
Andy Fleminge1393452005-08-24 18:46:21 -0500610 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800611 * Description: Sanitizes the settings (if we're not autonegotiating
612 * them), and then calls the driver's config_aneg function.
613 * If the PHYCONTROL Layer is operating, we change the state to
614 * reflect the beginning of Auto-negotiation or forcing.
Andy Fleminge1393452005-08-24 18:46:21 -0500615 */
Alexander Kochetkovf555f342017-04-20 14:00:04 +0300616static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
Andy Fleminge1393452005-08-24 18:46:21 -0500617{
Alexander Kochetkovf555f342017-04-20 14:00:04 +0300618 bool trigger = 0;
Andy Fleminge1393452005-08-24 18:46:21 -0500619 int err;
620
Florian Fainelli25149ef2017-02-17 16:07:34 -0800621 if (!phydev->drv)
622 return -EIO;
623
Nate Case35b5f6b2008-01-29 10:05:09 -0600624 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500625
626 if (AUTONEG_DISABLE == phydev->autoneg)
627 phy_sanitize_settings(phydev);
628
Ben Hutchings9b3320e2015-01-27 00:58:15 +0000629 /* Invalidate LP advertising flags */
630 phydev->lp_advertising = 0;
631
Andy Fleminge1393452005-08-24 18:46:21 -0500632 err = phydev->drv->config_aneg(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500633 if (err < 0)
634 goto out_unlock;
635
636 if (phydev->state != PHY_HALTED) {
637 if (AUTONEG_ENABLE == phydev->autoneg) {
638 phydev->state = PHY_AN;
639 phydev->link_timeout = PHY_AN_TIMEOUT;
640 } else {
641 phydev->state = PHY_FORCING;
642 phydev->link_timeout = PHY_FORCE_TIMEOUT;
643 }
644 }
645
Alexander Kochetkovf555f342017-04-20 14:00:04 +0300646 /* Re-schedule a PHY state machine to check PHY status because
647 * negotiation may already be done and aneg interrupt may not be
648 * generated.
649 */
650 if (phy_interrupt_is_valid(phydev) && (phydev->state == PHY_AN)) {
651 err = phy_aneg_done(phydev);
652 if (err > 0) {
653 trigger = true;
654 err = 0;
655 }
656 }
657
Andy Fleminge1393452005-08-24 18:46:21 -0500658out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600659 mutex_unlock(&phydev->lock);
Alexander Kochetkovf555f342017-04-20 14:00:04 +0300660
661 if (trigger)
662 phy_trigger_machine(phydev, sync);
663
Andy Fleminge1393452005-08-24 18:46:21 -0500664 return err;
665}
Alexander Kochetkovf555f342017-04-20 14:00:04 +0300666
667/**
668 * phy_start_aneg - start auto-negotiation for this PHY device
669 * @phydev: the phy_device struct
670 *
671 * Description: Sanitizes the settings (if we're not autonegotiating
672 * them), and then calls the driver's config_aneg function.
673 * If the PHYCONTROL Layer is operating, we change the state to
674 * reflect the beginning of Auto-negotiation or forcing.
675 */
676int phy_start_aneg(struct phy_device *phydev)
677{
678 return phy_start_aneg_priv(phydev, true);
679}
Andy Fleminge1393452005-08-24 18:46:21 -0500680EXPORT_SYMBOL(phy_start_aneg);
681
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800682/**
683 * phy_start_machine - start PHY state machine tracking
684 * @phydev: the phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400685 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800686 * Description: The PHY infrastructure can run a state machine
Andy Fleming00db8182005-07-30 19:31:23 -0400687 * which tracks whether the PHY is starting up, negotiating,
688 * etc. This function starts the timer which tracks the state
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300689 * of the PHY. If you want to maintain your own state machine,
690 * do not call this function.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800691 */
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300692void phy_start_machine(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400693{
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530694 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
Andy Fleming00db8182005-07-30 19:31:23 -0400695}
696
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800697/**
Andrew Lunn3c293f42016-10-12 22:14:53 +0200698 * phy_trigger_machine - trigger the state machine to run
699 *
700 * @phydev: the phy_device struct
Florian Fainellieab12772017-01-20 15:31:52 -0800701 * @sync: indicate whether we should wait for the workqueue cancelation
Andrew Lunn3c293f42016-10-12 22:14:53 +0200702 *
703 * Description: There has been a change in state which requires that the
704 * state machine runs.
705 */
706
Alexander Kochetkovf555f342017-04-20 14:00:04 +0300707void phy_trigger_machine(struct phy_device *phydev, bool sync)
Andrew Lunn3c293f42016-10-12 22:14:53 +0200708{
Florian Fainellieab12772017-01-20 15:31:52 -0800709 if (sync)
710 cancel_delayed_work_sync(&phydev->state_queue);
711 else
712 cancel_delayed_work(&phydev->state_queue);
Andrew Lunn3c293f42016-10-12 22:14:53 +0200713 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
714}
715
716/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800717 * phy_stop_machine - stop the PHY state machine tracking
718 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400719 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800720 * Description: Stops the state machine timer, sets the state to UP
Sergei Shtylylov817acf52006-07-26 00:53:53 +0400721 * (unless it wasn't up yet). This function must be called BEFORE
Andy Fleming00db8182005-07-30 19:31:23 -0400722 * phy_detach.
723 */
724void phy_stop_machine(struct phy_device *phydev)
725{
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700726 cancel_delayed_work_sync(&phydev->state_queue);
Andy Fleming00db8182005-07-30 19:31:23 -0400727
Nate Case35b5f6b2008-01-29 10:05:09 -0600728 mutex_lock(&phydev->lock);
Nathan Sullivan49d52e82017-03-22 15:27:01 -0500729 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
Andy Fleming00db8182005-07-30 19:31:23 -0400730 phydev->state = PHY_UP;
Nate Case35b5f6b2008-01-29 10:05:09 -0600731 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400732}
733
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800734/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800735 * phy_error - enter HALTED state for this PHY device
736 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400737 *
738 * Moves the PHY to the HALTED state in response to a read
739 * or write error, and tells the controller the link is down.
740 * Must not be called from interrupt context, or while the
741 * phydev->lock is held.
742 */
Andy Fleming9b9a8bf2008-05-02 13:00:51 -0500743static void phy_error(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400744{
Nate Case35b5f6b2008-01-29 10:05:09 -0600745 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400746 phydev->state = PHY_HALTED;
Nate Case35b5f6b2008-01-29 10:05:09 -0600747 mutex_unlock(&phydev->lock);
Andrew Lunn3c293f42016-10-12 22:14:53 +0200748
Florian Fainellieab12772017-01-20 15:31:52 -0800749 phy_trigger_machine(phydev, false);
Andy Fleming00db8182005-07-30 19:31:23 -0400750}
751
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800752/**
753 * phy_interrupt - PHY interrupt handler
754 * @irq: interrupt line
755 * @phy_dat: phy_device pointer
Andy Fleminge1393452005-08-24 18:46:21 -0500756 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800757 * Description: When a PHY interrupt occurs, the handler disables
Andrew Lunn664fcf12016-10-16 19:56:51 +0200758 * interrupts, and uses phy_change to handle the interrupt.
Andy Fleminge1393452005-08-24 18:46:21 -0500759 */
David Howells7d12e782006-10-05 14:55:46 +0100760static irqreturn_t phy_interrupt(int irq, void *phy_dat)
Andy Fleminge1393452005-08-24 18:46:21 -0500761{
762 struct phy_device *phydev = phy_dat;
763
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100764 if (PHY_HALTED == phydev->state)
765 return IRQ_NONE; /* It can't be ours. */
766
Andy Fleminge1393452005-08-24 18:46:21 -0500767 disable_irq_nosync(irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700768 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500769
Andrew Lunn664fcf12016-10-16 19:56:51 +0200770 phy_change(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500771
772 return IRQ_HANDLED;
773}
774
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800775/**
776 * phy_enable_interrupts - Enable the interrupts from the PHY side
777 * @phydev: target phy_device struct
778 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000779static int phy_enable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400780{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300781 int err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400782
Andy Fleminge1393452005-08-24 18:46:21 -0500783 if (err < 0)
784 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400785
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300786 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400787}
Andy Fleming00db8182005-07-30 19:31:23 -0400788
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800789/**
790 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
791 * @phydev: target phy_device struct
792 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000793static int phy_disable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400794{
795 int err;
796
797 /* Disable PHY interrupts */
798 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400799 if (err)
800 goto phy_err;
801
802 /* Clear the interrupt */
803 err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400804 if (err)
805 goto phy_err;
806
807 return 0;
808
809phy_err:
810 phy_error(phydev);
811
812 return err;
813}
Andy Fleminge1393452005-08-24 18:46:21 -0500814
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800815/**
816 * phy_start_interrupts - request and enable interrupts for a PHY device
817 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500818 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800819 * Description: Request the interrupt for the given PHY.
820 * If this fails, then we set irq to PHY_POLL.
Andy Fleminge1393452005-08-24 18:46:21 -0500821 * Otherwise, we enable the interrupts in the PHY.
Andy Fleminge1393452005-08-24 18:46:21 -0500822 * This should only be called with a valid IRQ number.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800823 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500824 */
825int phy_start_interrupts(struct phy_device *phydev)
826{
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700827 atomic_set(&phydev->irq_disable, 0);
Andrew Lunnc974bdb2016-10-16 19:56:50 +0200828 if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
829 IRQF_ONESHOT | IRQF_SHARED,
Andrew Lunnae0219c2016-10-16 19:56:52 +0200830 phydev_name(phydev), phydev) < 0) {
Joe Perches8d242482012-06-09 07:49:07 +0000831 pr_warn("%s: Can't get IRQ %d (PHY)\n",
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100832 phydev->mdio.bus->name, phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500833 phydev->irq = PHY_POLL;
834 return 0;
835 }
836
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300837 return phy_enable_interrupts(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500838}
839EXPORT_SYMBOL(phy_start_interrupts);
840
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800841/**
842 * phy_stop_interrupts - disable interrupts from a PHY device
843 * @phydev: target phy_device struct
844 */
Andy Fleminge1393452005-08-24 18:46:21 -0500845int phy_stop_interrupts(struct phy_device *phydev)
846{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300847 int err = phy_disable_interrupts(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500848
849 if (err)
850 phy_error(phydev);
851
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700852 free_irq(phydev->irq, phydev);
853
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300854 /* If work indeed has been cancelled, disable_irq() will have
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700855 * been left unbalanced from phy_interrupt() and enable_irq()
856 * has to be called so that other devices on the line work.
857 */
858 while (atomic_dec_return(&phydev->irq_disable) >= 0)
859 enable_irq(phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500860
861 return err;
862}
863EXPORT_SYMBOL(phy_stop_interrupts);
864
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800865/**
Andrew Lunn664fcf12016-10-16 19:56:51 +0200866 * phy_change - Called by the phy_interrupt to handle PHY changes
867 * @phydev: phy_device struct that interrupted
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800868 */
Andrew Lunn664fcf12016-10-16 19:56:51 +0200869void phy_change(struct phy_device *phydev)
Andy Fleminge1393452005-08-24 18:46:21 -0500870{
Florian Fainellideccd16f92016-01-18 19:33:07 -0800871 if (phy_interrupt_is_valid(phydev)) {
872 if (phydev->drv->did_interrupt &&
873 !phydev->drv->did_interrupt(phydev))
874 goto ignore;
Anatolij Gustschina8729eb2009-04-07 02:01:42 +0000875
Florian Fainellideccd16f92016-01-18 19:33:07 -0800876 if (phy_disable_interrupts(phydev))
877 goto phy_err;
878 }
Andy Fleminge1393452005-08-24 18:46:21 -0500879
Nate Case35b5f6b2008-01-29 10:05:09 -0600880 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500881 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
882 phydev->state = PHY_CHANGELINK;
Nate Case35b5f6b2008-01-29 10:05:09 -0600883 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500884
Florian Fainellideccd16f92016-01-18 19:33:07 -0800885 if (phy_interrupt_is_valid(phydev)) {
886 atomic_dec(&phydev->irq_disable);
887 enable_irq(phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500888
Florian Fainellideccd16f92016-01-18 19:33:07 -0800889 /* Reenable interrupts */
890 if (PHY_HALTED != phydev->state &&
891 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
892 goto irq_enable_err;
893 }
Andy Fleminge1393452005-08-24 18:46:21 -0500894
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700895 /* reschedule state queue work to run as soon as possible */
Florian Fainellieab12772017-01-20 15:31:52 -0800896 phy_trigger_machine(phydev, true);
Andy Fleminge1393452005-08-24 18:46:21 -0500897 return;
898
Anatolij Gustschina8729eb2009-04-07 02:01:42 +0000899ignore:
900 atomic_dec(&phydev->irq_disable);
901 enable_irq(phydev->irq);
902 return;
903
Andy Fleminge1393452005-08-24 18:46:21 -0500904irq_enable_err:
905 disable_irq(phydev->irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700906 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500907phy_err:
908 phy_error(phydev);
909}
910
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800911/**
Andrew Lunn664fcf12016-10-16 19:56:51 +0200912 * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
913 * @work: work_struct that describes the work to be done
914 */
915void phy_change_work(struct work_struct *work)
916{
917 struct phy_device *phydev =
918 container_of(work, struct phy_device, phy_queue);
919
920 phy_change(phydev);
921}
922
923/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800924 * phy_stop - Bring down the PHY link, and stop checking the status
925 * @phydev: target phy_device struct
926 */
Andy Fleminge1393452005-08-24 18:46:21 -0500927void phy_stop(struct phy_device *phydev)
928{
Nate Case35b5f6b2008-01-29 10:05:09 -0600929 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500930
931 if (PHY_HALTED == phydev->state)
932 goto out_unlock;
933
Florian Fainelli2c7b4922013-05-19 22:53:42 +0000934 if (phy_interrupt_is_valid(phydev)) {
Andy Fleminge1393452005-08-24 18:46:21 -0500935 /* Disable PHY Interrupts */
936 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleminge1393452005-08-24 18:46:21 -0500937
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100938 /* Clear any pending interrupts */
939 phy_clear_interrupt(phydev);
940 }
Andy Fleminge1393452005-08-24 18:46:21 -0500941
Maciej W. Rozycki6daf6532007-09-28 22:42:15 -0700942 phydev->state = PHY_HALTED;
943
Andy Fleminge1393452005-08-24 18:46:21 -0500944out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600945 mutex_unlock(&phydev->lock);
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100946
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300947 /* Cannot call flush_scheduled_work() here as desired because
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100948 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
949 * will not reenable interrupts.
950 */
Andy Fleminge1393452005-08-24 18:46:21 -0500951}
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300952EXPORT_SYMBOL(phy_stop);
Andy Fleminge1393452005-08-24 18:46:21 -0500953
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800954/**
955 * phy_start - start or restart a PHY device
956 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500957 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800958 * Description: Indicates the attached device's readiness to
Andy Fleminge1393452005-08-24 18:46:21 -0500959 * handle PHY-related work. Used during startup to start the
960 * PHY, and after a call to phy_stop() to resume operation.
961 * Also used to indicate the MDIO bus has cleared an error
962 * condition.
963 */
964void phy_start(struct phy_device *phydev)
965{
Tim Bealec15e10e2015-05-18 15:38:38 +1200966 bool do_resume = false;
967 int err = 0;
968
Nate Case35b5f6b2008-01-29 10:05:09 -0600969 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500970
971 switch (phydev->state) {
Florian Fainellie1093742013-12-17 21:38:12 -0800972 case PHY_STARTING:
973 phydev->state = PHY_PENDING;
974 break;
975 case PHY_READY:
976 phydev->state = PHY_UP;
977 break;
978 case PHY_HALTED:
Tim Bealec15e10e2015-05-18 15:38:38 +1200979 /* make sure interrupts are re-enabled for the PHY */
Shaohui Xie84a527a2016-05-10 17:42:26 +0800980 if (phydev->irq != PHY_POLL) {
981 err = phy_enable_interrupts(phydev);
982 if (err < 0)
983 break;
984 }
Tim Bealec15e10e2015-05-18 15:38:38 +1200985
Florian Fainellie1093742013-12-17 21:38:12 -0800986 phydev->state = PHY_RESUMING;
Tim Bealec15e10e2015-05-18 15:38:38 +1200987 do_resume = true;
988 break;
Florian Fainellie1093742013-12-17 21:38:12 -0800989 default:
990 break;
Andy Fleminge1393452005-08-24 18:46:21 -0500991 }
Nate Case35b5f6b2008-01-29 10:05:09 -0600992 mutex_unlock(&phydev->lock);
Tim Bealec15e10e2015-05-18 15:38:38 +1200993
994 /* if phy was suspended, bring the physical link up again */
995 if (do_resume)
996 phy_resume(phydev);
Andrew Lunn3c293f42016-10-12 22:14:53 +0200997
Florian Fainellieab12772017-01-20 15:31:52 -0800998 phy_trigger_machine(phydev, true);
Andy Fleminge1393452005-08-24 18:46:21 -0500999}
Andy Fleminge1393452005-08-24 18:46:21 -05001000EXPORT_SYMBOL(phy_start);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -04001001
Zach Brown61a17962016-10-17 10:49:53 -05001002static void phy_adjust_link(struct phy_device *phydev)
1003{
1004 phydev->adjust_link(phydev->attached_dev);
Zach Brown2e0bc452016-10-17 10:49:55 -05001005 phy_led_trigger_change_speed(phydev);
Zach Brown61a17962016-10-17 10:49:53 -05001006}
1007
Nate Case35b5f6b2008-01-29 10:05:09 -06001008/**
1009 * phy_state_machine - Handle the state machine
1010 * @work: work_struct that describes the work to be done
Nate Case35b5f6b2008-01-29 10:05:09 -06001011 */
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +00001012void phy_state_machine(struct work_struct *work)
Andy Fleming00db8182005-07-30 19:31:23 -04001013{
Jean Delvarebf6aede2009-04-02 16:56:54 -07001014 struct delayed_work *dwork = to_delayed_work(work);
Nate Case35b5f6b2008-01-29 10:05:09 -06001015 struct phy_device *phydev =
Marcin Slusarza390d1f2009-03-13 15:41:19 -07001016 container_of(dwork, struct phy_device, state_queue);
Tim Bealec15e10e2015-05-18 15:38:38 +12001017 bool needs_aneg = false, do_suspend = false;
Florian Fainelli3e2186e2015-05-16 10:17:56 -07001018 enum phy_state old_state;
Andy Fleming00db8182005-07-30 19:31:23 -04001019 int err = 0;
Shaohui Xie11e122c2015-08-14 12:23:40 +08001020 int old_link;
Andy Fleming00db8182005-07-30 19:31:23 -04001021
Nate Case35b5f6b2008-01-29 10:05:09 -06001022 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001023
Florian Fainelli3e2186e2015-05-16 10:17:56 -07001024 old_state = phydev->state;
1025
Florian Fainelli25149ef2017-02-17 16:07:34 -08001026 if (phydev->drv && phydev->drv->link_change_notify)
Daniel Mack2b8f2a22014-06-18 11:01:41 +02001027 phydev->drv->link_change_notify(phydev);
1028
Florian Fainellie1093742013-12-17 21:38:12 -08001029 switch (phydev->state) {
1030 case PHY_DOWN:
1031 case PHY_STARTING:
1032 case PHY_READY:
1033 case PHY_PENDING:
1034 break;
1035 case PHY_UP:
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001036 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001037
1038 phydev->link_timeout = PHY_AN_TIMEOUT;
1039
1040 break;
1041 case PHY_AN:
1042 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001043 if (err < 0)
Andy Fleming00db8182005-07-30 19:31:23 -04001044 break;
Florian Fainellie1093742013-12-17 21:38:12 -08001045
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001046 /* If the link is down, give up on negotiation for now */
Florian Fainellie1093742013-12-17 21:38:12 -08001047 if (!phydev->link) {
1048 phydev->state = PHY_NOLINK;
1049 netif_carrier_off(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001050 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001051 break;
1052 }
1053
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001054 /* Check if negotiation is done. Break if there's an error */
Florian Fainellie1093742013-12-17 21:38:12 -08001055 err = phy_aneg_done(phydev);
1056 if (err < 0)
1057 break;
1058
1059 /* If AN is done, we're running */
1060 if (err > 0) {
1061 phydev->state = PHY_RUNNING;
1062 netif_carrier_on(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001063 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001064
Balakumaran Kannanfa8cdda2014-04-09 09:03:45 +05301065 } else if (0 == phydev->link_timeout--)
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001066 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001067 break;
1068 case PHY_NOLINK:
Andrew Lunn321beec2015-11-16 23:36:46 +01001069 if (phy_interrupt_is_valid(phydev))
1070 break;
1071
Florian Fainellie1093742013-12-17 21:38:12 -08001072 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001073 if (err)
Andy Fleming00db8182005-07-30 19:31:23 -04001074 break;
Andy Fleming6b655522006-10-16 16:19:17 -05001075
Florian Fainellie1093742013-12-17 21:38:12 -08001076 if (phydev->link) {
Balakumaran Kannane46e08b2014-04-24 08:22:47 +05301077 if (AUTONEG_ENABLE == phydev->autoneg) {
1078 err = phy_aneg_done(phydev);
1079 if (err < 0)
1080 break;
1081
1082 if (!err) {
1083 phydev->state = PHY_AN;
1084 phydev->link_timeout = PHY_AN_TIMEOUT;
1085 break;
1086 }
1087 }
Florian Fainellie1093742013-12-17 21:38:12 -08001088 phydev->state = PHY_RUNNING;
1089 netif_carrier_on(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001090 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001091 }
1092 break;
1093 case PHY_FORCING:
1094 err = genphy_update_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001095 if (err)
1096 break;
Andy Fleming6b655522006-10-16 16:19:17 -05001097
Florian Fainellie1093742013-12-17 21:38:12 -08001098 if (phydev->link) {
1099 phydev->state = PHY_RUNNING;
1100 netif_carrier_on(phydev->attached_dev);
1101 } else {
1102 if (0 == phydev->link_timeout--)
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001103 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001104 }
1105
Zach Brown61a17962016-10-17 10:49:53 -05001106 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001107 break;
1108 case PHY_RUNNING:
Florian Fainellid5c3d842016-01-18 19:33:06 -08001109 /* Only register a CHANGE if we are polling and link changed
1110 * since latest checking.
Florian Fainellie1093742013-12-17 21:38:12 -08001111 */
Florian Fainellid5c3d842016-01-18 19:33:06 -08001112 if (phydev->irq == PHY_POLL) {
Shaohui Xie11e122c2015-08-14 12:23:40 +08001113 old_link = phydev->link;
1114 err = phy_read_status(phydev);
1115 if (err)
1116 break;
1117
1118 if (old_link != phydev->link)
1119 phydev->state = PHY_CHANGELINK;
1120 }
Zefir Kurtisi811a9192017-01-06 12:14:48 +01001121 /*
1122 * Failsafe: check that nobody set phydev->link=0 between two
1123 * poll cycles, otherwise we won't leave RUNNING state as long
1124 * as link remains down.
1125 */
1126 if (!phydev->link && phydev->state == PHY_RUNNING) {
1127 phydev->state = PHY_CHANGELINK;
1128 phydev_err(phydev, "no link in PHY_RUNNING\n");
1129 }
Florian Fainellie1093742013-12-17 21:38:12 -08001130 break;
1131 case PHY_CHANGELINK:
1132 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001133 if (err)
1134 break;
1135
1136 if (phydev->link) {
1137 phydev->state = PHY_RUNNING;
1138 netif_carrier_on(phydev->attached_dev);
1139 } else {
1140 phydev->state = PHY_NOLINK;
1141 netif_carrier_off(phydev->attached_dev);
1142 }
1143
Zach Brown61a17962016-10-17 10:49:53 -05001144 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001145
1146 if (phy_interrupt_is_valid(phydev))
1147 err = phy_config_interrupt(phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001148 PHY_INTERRUPT_ENABLED);
Florian Fainellie1093742013-12-17 21:38:12 -08001149 break;
1150 case PHY_HALTED:
1151 if (phydev->link) {
1152 phydev->link = 0;
1153 netif_carrier_off(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001154 phy_adjust_link(phydev);
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001155 do_suspend = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001156 }
1157 break;
1158 case PHY_RESUMING:
Florian Fainellie1093742013-12-17 21:38:12 -08001159 if (AUTONEG_ENABLE == phydev->autoneg) {
Andy Fleming00db8182005-07-30 19:31:23 -04001160 err = phy_aneg_done(phydev);
1161 if (err < 0)
1162 break;
1163
Florian Fainellie1093742013-12-17 21:38:12 -08001164 /* err > 0 if AN is done.
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001165 * Otherwise, it's 0, and we're still waiting for AN
1166 */
Andy Fleming00db8182005-07-30 19:31:23 -04001167 if (err > 0) {
Wade Farnsworth42caa072009-07-01 13:00:34 +00001168 err = phy_read_status(phydev);
1169 if (err)
1170 break;
1171
1172 if (phydev->link) {
1173 phydev->state = PHY_RUNNING;
1174 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001175 } else {
Wade Farnsworth42caa072009-07-01 13:00:34 +00001176 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001177 }
Zach Brown61a17962016-10-17 10:49:53 -05001178 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001179 } else {
1180 phydev->state = PHY_AN;
1181 phydev->link_timeout = PHY_AN_TIMEOUT;
Wade Farnsworth42caa072009-07-01 13:00:34 +00001182 }
Florian Fainellie1093742013-12-17 21:38:12 -08001183 } else {
1184 err = phy_read_status(phydev);
1185 if (err)
1186 break;
1187
1188 if (phydev->link) {
1189 phydev->state = PHY_RUNNING;
1190 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001191 } else {
Florian Fainellie1093742013-12-17 21:38:12 -08001192 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001193 }
Zach Brown61a17962016-10-17 10:49:53 -05001194 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001195 }
1196 break;
Andy Fleming00db8182005-07-30 19:31:23 -04001197 }
1198
Nate Case35b5f6b2008-01-29 10:05:09 -06001199 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001200
1201 if (needs_aneg)
Alexander Kochetkovf555f342017-04-20 14:00:04 +03001202 err = phy_start_aneg_priv(phydev, false);
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001203 else if (do_suspend)
Sebastian Hesselbarthbe9dad12013-12-13 10:20:29 +01001204 phy_suspend(phydev);
1205
Andy Fleming00db8182005-07-30 19:31:23 -04001206 if (err < 0)
1207 phy_error(phydev);
1208
Andrew Lunn72ba48b2016-01-06 20:11:09 +01001209 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1210 phy_state_to_str(old_state),
1211 phy_state_to_str(phydev->state));
Florian Fainelli3e2186e2015-05-16 10:17:56 -07001212
Florian Fainellid5c3d842016-01-18 19:33:06 -08001213 /* Only re-schedule a PHY state machine change if we are polling the
1214 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1215 * between states from phy_mac_interrupt()
1216 */
1217 if (phydev->irq == PHY_POLL)
1218 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1219 PHY_STATE_TIME * HZ);
Nate Case35b5f6b2008-01-29 10:05:09 -06001220}
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001221
Andrew Lunn664fcf12016-10-16 19:56:51 +02001222/**
1223 * phy_mac_interrupt - MAC says the link has changed
1224 * @phydev: phy_device struct with changed link
1225 * @new_link: Link is Up/Down.
1226 *
1227 * Description: The MAC layer is able indicate there has been a change
1228 * in the PHY link status. Set the new link status, and trigger the
1229 * state machine, work a work queue.
1230 */
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001231void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1232{
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001233 phydev->link = new_link;
Florian Fainellideccd16f92016-01-18 19:33:07 -08001234
1235 /* Trigger a state machine change */
1236 queue_work(system_power_efficient_wq, &phydev->phy_queue);
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001237}
1238EXPORT_SYMBOL(phy_mac_interrupt);
1239
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001240/**
1241 * phy_init_eee - init and check the EEE feature
1242 * @phydev: target phy_device struct
1243 * @clk_stop_enable: PHY may stop the clock during LPI
1244 *
1245 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1246 * is supported by looking at the MMD registers 3.20 and 7.60/61
1247 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1248 * bit if required.
1249 */
1250int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1251{
Florian Fainelli25149ef2017-02-17 16:07:34 -08001252 if (!phydev->drv)
1253 return -EIO;
1254
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001255 /* According to 802.3az,the EEE is supported only in full duplex-mode.
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001256 */
Russell King32d75142017-03-31 10:37:18 +01001257 if (phydev->duplex == DUPLEX_FULL) {
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001258 int eee_lp, eee_cap, eee_adv;
1259 u32 lp, cap, adv;
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -07001260 int status;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001261
1262 /* Read phy status to properly get the right settings */
1263 status = phy_read_status(phydev);
1264 if (status)
1265 return status;
1266
1267 /* First check if the EEE ability is supported */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001268 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001269 if (eee_cap <= 0)
1270 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001271
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001272 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001273 if (!cap)
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001274 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001275
1276 /* Check which link settings negotiated and verify it in
1277 * the EEE advertising registers.
1278 */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001279 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001280 if (eee_lp <= 0)
1281 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001282
Russell Kinga6d99fc2017-03-21 16:36:53 +00001283 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001284 if (eee_adv <= 0)
1285 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001286
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001287 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1288 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
Guenter Roeck54da5a82015-02-17 09:36:22 -08001289 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001290 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001291
1292 if (clk_stop_enable) {
1293 /* Configure the PHY to stop receiving xMII
1294 * clock while it is signaling LPI.
1295 */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001296 int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001297 if (val < 0)
1298 return val;
1299
1300 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
Russell Kinga6d99fc2017-03-21 16:36:53 +00001301 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001302 }
1303
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001304 return 0; /* EEE supported */
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001305 }
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001306eee_exit_err:
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001307 return -EPROTONOSUPPORT;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001308}
1309EXPORT_SYMBOL(phy_init_eee);
1310
1311/**
1312 * phy_get_eee_err - report the EEE wake error count
1313 * @phydev: target phy_device struct
1314 *
1315 * Description: it is to report the number of time where the PHY
1316 * failed to complete its normal wake sequence.
1317 */
1318int phy_get_eee_err(struct phy_device *phydev)
1319{
Florian Fainelli25149ef2017-02-17 16:07:34 -08001320 if (!phydev->drv)
1321 return -EIO;
1322
Russell Kinga6d99fc2017-03-21 16:36:53 +00001323 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001324}
1325EXPORT_SYMBOL(phy_get_eee_err);
1326
1327/**
1328 * phy_ethtool_get_eee - get EEE supported and status
1329 * @phydev: target phy_device struct
1330 * @data: ethtool_eee data
1331 *
1332 * Description: it reportes the Supported/Advertisement/LP Advertisement
1333 * capabilities.
1334 */
1335int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1336{
1337 int val;
1338
Florian Fainelli25149ef2017-02-17 16:07:34 -08001339 if (!phydev->drv)
1340 return -EIO;
1341
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001342 /* Get Supported EEE */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001343 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001344 if (val < 0)
1345 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001346 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001347
1348 /* Get advertisement EEE */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001349 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001350 if (val < 0)
1351 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001352 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001353
1354 /* Get LP advertisement EEE */
Russell Kinga6d99fc2017-03-21 16:36:53 +00001355 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001356 if (val < 0)
1357 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001358 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001359
1360 return 0;
1361}
1362EXPORT_SYMBOL(phy_ethtool_get_eee);
1363
1364/**
1365 * phy_ethtool_set_eee - set EEE supported and status
1366 * @phydev: target phy_device struct
1367 * @data: ethtool_eee data
1368 *
1369 * Description: it is to program the Advertisement EEE register.
1370 */
1371int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1372{
Russell Kingf75abeb2017-03-31 10:37:12 +01001373 int cap, old_adv, adv, ret;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001374
Florian Fainelli25149ef2017-02-17 16:07:34 -08001375 if (!phydev->drv)
1376 return -EIO;
1377
Russell King83ea0672017-03-31 10:37:07 +01001378 /* Get Supported EEE */
1379 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1380 if (cap < 0)
1381 return cap;
1382
Russell Kingf75abeb2017-03-31 10:37:12 +01001383 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1384 if (old_adv < 0)
1385 return old_adv;
1386
Russell King83ea0672017-03-31 10:37:07 +01001387 adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1388
jbrunetd853d142016-11-28 10:46:46 +01001389 /* Mask prohibited EEE modes */
Russell King83ea0672017-03-31 10:37:07 +01001390 adv &= ~phydev->eee_broken_modes;
jbrunetd853d142016-11-28 10:46:46 +01001391
Russell Kingf75abeb2017-03-31 10:37:12 +01001392 if (old_adv != adv) {
1393 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1394 if (ret < 0)
1395 return ret;
1396
1397 /* Restart autonegotiation so the new modes get sent to the
1398 * link partner.
1399 */
1400 ret = genphy_restart_aneg(phydev);
1401 if (ret < 0)
1402 return ret;
1403 }
1404
1405 return 0;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001406}
1407EXPORT_SYMBOL(phy_ethtool_set_eee);
Michael Stapelberg42e836e2013-03-11 13:56:44 +00001408
1409int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1410{
Florian Fainelli25149ef2017-02-17 16:07:34 -08001411 if (phydev->drv && phydev->drv->set_wol)
Michael Stapelberg42e836e2013-03-11 13:56:44 +00001412 return phydev->drv->set_wol(phydev, wol);
1413
1414 return -EOPNOTSUPP;
1415}
1416EXPORT_SYMBOL(phy_ethtool_set_wol);
1417
1418void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1419{
Florian Fainelli25149ef2017-02-17 16:07:34 -08001420 if (phydev->drv && phydev->drv->get_wol)
Michael Stapelberg42e836e2013-03-11 13:56:44 +00001421 phydev->drv->get_wol(phydev, wol);
1422}
1423EXPORT_SYMBOL(phy_ethtool_get_wol);
Philippe Reynes9d9a77c2016-05-10 00:19:41 +02001424
1425int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1426 struct ethtool_link_ksettings *cmd)
1427{
1428 struct phy_device *phydev = ndev->phydev;
1429
1430 if (!phydev)
1431 return -ENODEV;
1432
1433 return phy_ethtool_ksettings_get(phydev, cmd);
1434}
1435EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1436
1437int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1438 const struct ethtool_link_ksettings *cmd)
1439{
1440 struct phy_device *phydev = ndev->phydev;
1441
1442 if (!phydev)
1443 return -ENODEV;
1444
1445 return phy_ethtool_ksettings_set(phydev, cmd);
1446}
1447EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
Florian Fainellie86a8982016-11-15 10:06:30 -08001448
1449int phy_ethtool_nway_reset(struct net_device *ndev)
1450{
1451 struct phy_device *phydev = ndev->phydev;
1452
1453 if (!phydev)
1454 return -ENODEV;
1455
Florian Fainelli25149ef2017-02-17 16:07:34 -08001456 if (!phydev->drv)
1457 return -EIO;
1458
Florian Fainellie86a8982016-11-15 10:06:30 -08001459 return genphy_restart_aneg(phydev);
1460}
1461EXPORT_SYMBOL(phy_ethtool_nway_reset);