blob: 2f94c60d49394ecc743b13660a261979c460c15b [file] [log] [blame]
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001/* Framework for configuring and reading PHY devices
Andy Fleming00db8182005-07-30 19:31:23 -04002 * Based on code in sungem_phy.c and gianfar_phy.c
3 *
4 * Author: Andy Fleming
5 *
6 * Copyright (c) 2004 Freescale Semiconductor, Inc.
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -07007 * Copyright (c) 2006, 2007 Maciej W. Rozycki
Andy Fleming00db8182005-07-30 19:31:23 -04008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
Joe Perches8d242482012-06-09 07:49:07 +000015
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Andy Fleming00db8182005-07-30 19:31:23 -040018#include <linux/kernel.h>
Andy Fleming00db8182005-07-30 19:31:23 -040019#include <linux/string.h>
20#include <linux/errno.h>
21#include <linux/unistd.h>
Andy Fleming00db8182005-07-30 19:31:23 -040022#include <linux/interrupt.h>
Andy Fleming00db8182005-07-30 19:31:23 -040023#include <linux/delay.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/skbuff.h>
Andy Fleming00db8182005-07-30 19:31:23 -040027#include <linux/mm.h>
28#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040029#include <linux/mii.h>
30#include <linux/ethtool.h>
31#include <linux/phy.h>
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +010032#include <linux/timer.h>
33#include <linux/workqueue.h>
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +000034#include <linux/mdio.h>
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030035#include <linux/io.h>
36#include <linux/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070037#include <linux/atomic.h>
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030038
Andy Fleming00db8182005-07-30 19:31:23 -040039#include <asm/irq.h>
Andy Fleming00db8182005-07-30 19:31:23 -040040
Florian Fainelli766d1d32014-02-11 17:27:35 -080041static const char *phy_speed_to_str(int speed)
42{
43 switch (speed) {
44 case SPEED_10:
45 return "10Mbps";
46 case SPEED_100:
47 return "100Mbps";
48 case SPEED_1000:
49 return "1Gbps";
50 case SPEED_2500:
51 return "2.5Gbps";
52 case SPEED_10000:
53 return "10Gbps";
54 case SPEED_UNKNOWN:
55 return "Unknown";
56 default:
57 return "Unsupported (update phy.c)";
58 }
59}
60
Florian Fainelli3e2186e2015-05-16 10:17:56 -070061#define PHY_STATE_STR(_state) \
62 case PHY_##_state: \
63 return __stringify(_state); \
64
65static const char *phy_state_to_str(enum phy_state st)
66{
67 switch (st) {
68 PHY_STATE_STR(DOWN)
69 PHY_STATE_STR(STARTING)
70 PHY_STATE_STR(READY)
71 PHY_STATE_STR(PENDING)
72 PHY_STATE_STR(UP)
73 PHY_STATE_STR(AN)
74 PHY_STATE_STR(RUNNING)
75 PHY_STATE_STR(NOLINK)
76 PHY_STATE_STR(FORCING)
77 PHY_STATE_STR(CHANGELINK)
78 PHY_STATE_STR(HALTED)
79 PHY_STATE_STR(RESUMING)
80 }
81
82 return NULL;
83}
84
85
Randy Dunlapb3df0da2007-03-06 02:41:48 -080086/**
87 * phy_print_status - Convenience function to print out the current phy status
88 * @phydev: the phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -050089 */
90void phy_print_status(struct phy_device *phydev)
91{
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030092 if (phydev->link) {
Florian Fainellidf40cc82014-02-11 17:27:34 -080093 netdev_info(phydev->attached_dev,
Florian Fainelli766d1d32014-02-11 17:27:35 -080094 "Link is Up - %s/%s - flow control %s\n",
95 phy_speed_to_str(phydev->speed),
Florian Fainellidf40cc82014-02-11 17:27:34 -080096 DUPLEX_FULL == phydev->duplex ? "Full" : "Half",
97 phydev->pause ? "rx/tx" : "off");
Sergei Shtylyov2f53e902014-01-05 03:17:06 +030098 } else {
Florian Fainelli43b63292014-02-11 17:27:33 -080099 netdev_info(phydev->attached_dev, "Link is Down\n");
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300100 }
Andy Fleminge1393452005-08-24 18:46:21 -0500101}
102EXPORT_SYMBOL(phy_print_status);
Andy Fleming00db8182005-07-30 19:31:23 -0400103
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800104/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800105 * phy_clear_interrupt - Ack the phy device's interrupt
106 * @phydev: the phy_device struct
107 *
108 * If the @phydev driver has an ack_interrupt function, call it to
109 * ack and clear the phy device's interrupt.
110 *
Florian Fainelliad033502014-02-11 17:27:38 -0800111 * Returns 0 on success or < 0 on error.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800112 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000113static int phy_clear_interrupt(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400114{
Andy Fleming00db8182005-07-30 19:31:23 -0400115 if (phydev->drv->ack_interrupt)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300116 return phydev->drv->ack_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400117
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300118 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400119}
120
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800121/**
122 * phy_config_interrupt - configure the PHY device for the requested interrupts
123 * @phydev: the phy_device struct
124 * @interrupts: interrupt flags to configure for this @phydev
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_config_interrupt(struct phy_device *phydev, u32 interrupts)
Andy Fleming00db8182005-07-30 19:31:23 -0400129{
Andy Fleming00db8182005-07-30 19:31:23 -0400130 phydev->interrupts = interrupts;
131 if (phydev->drv->config_intr)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300132 return phydev->drv->config_intr(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400133
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300134 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400135}
136
137
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800138/**
139 * phy_aneg_done - return auto-negotiation status
140 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400141 *
Florian Fainelli76a423a2014-02-11 17:27:37 -0800142 * Description: Return the auto-negotiation status from this @phydev
143 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
144 * is still pending.
Andy Fleming00db8182005-07-30 19:31:23 -0400145 */
146static inline int phy_aneg_done(struct phy_device *phydev)
147{
Florian Fainelli76a423a2014-02-11 17:27:37 -0800148 if (phydev->drv->aneg_done)
149 return phydev->drv->aneg_done(phydev);
150
Florian Fainellia9fa6e62014-02-11 17:27:36 -0800151 return genphy_aneg_done(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400152}
153
Andy Fleming00db8182005-07-30 19:31:23 -0400154/* A structure for mapping a particular speed and duplex
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300155 * combination to a particular SUPPORTED and ADVERTISED value
156 */
Andy Fleming00db8182005-07-30 19:31:23 -0400157struct phy_setting {
158 int speed;
159 int duplex;
160 u32 setting;
161};
162
163/* A mapping of all SUPPORTED settings to speed/duplex */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500164static const struct phy_setting settings[] = {
Andy Fleming00db8182005-07-30 19:31:23 -0400165 {
Lendacky, Thomas3e707702014-07-14 14:05:46 -0500166 .speed = SPEED_10000,
167 .duplex = DUPLEX_FULL,
168 .setting = SUPPORTED_10000baseKR_Full,
169 },
170 {
171 .speed = SPEED_10000,
172 .duplex = DUPLEX_FULL,
173 .setting = SUPPORTED_10000baseKX4_Full,
174 },
175 {
176 .speed = SPEED_10000,
Andy Fleming00db8182005-07-30 19:31:23 -0400177 .duplex = DUPLEX_FULL,
178 .setting = SUPPORTED_10000baseT_Full,
179 },
180 {
Lendacky, Thomas3e707702014-07-14 14:05:46 -0500181 .speed = SPEED_2500,
182 .duplex = DUPLEX_FULL,
183 .setting = SUPPORTED_2500baseX_Full,
184 },
185 {
186 .speed = SPEED_1000,
187 .duplex = DUPLEX_FULL,
188 .setting = SUPPORTED_1000baseKX_Full,
189 },
190 {
Andy Fleming00db8182005-07-30 19:31:23 -0400191 .speed = SPEED_1000,
192 .duplex = DUPLEX_FULL,
193 .setting = SUPPORTED_1000baseT_Full,
194 },
195 {
196 .speed = SPEED_1000,
197 .duplex = DUPLEX_HALF,
198 .setting = SUPPORTED_1000baseT_Half,
199 },
200 {
201 .speed = SPEED_100,
202 .duplex = DUPLEX_FULL,
203 .setting = SUPPORTED_100baseT_Full,
204 },
205 {
206 .speed = SPEED_100,
207 .duplex = DUPLEX_HALF,
208 .setting = SUPPORTED_100baseT_Half,
209 },
210 {
211 .speed = SPEED_10,
212 .duplex = DUPLEX_FULL,
213 .setting = SUPPORTED_10baseT_Full,
214 },
215 {
216 .speed = SPEED_10,
217 .duplex = DUPLEX_HALF,
218 .setting = SUPPORTED_10baseT_Half,
219 },
220};
221
Denis Chengff8ac602007-09-02 18:30:18 +0800222#define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
Andy Fleming00db8182005-07-30 19:31:23 -0400223
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800224/**
225 * phy_find_setting - find a PHY settings array entry that matches speed & duplex
226 * @speed: speed to match
227 * @duplex: duplex to match
Andy Fleming00db8182005-07-30 19:31:23 -0400228 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800229 * Description: Searches the settings array for the setting which
Andy Fleming00db8182005-07-30 19:31:23 -0400230 * matches the desired speed and duplex, and returns the index
231 * of that setting. Returns the index of the last setting if
232 * none of the others match.
233 */
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -0700234static inline unsigned int phy_find_setting(int speed, int duplex)
Andy Fleming00db8182005-07-30 19:31:23 -0400235{
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -0700236 unsigned int idx = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400237
238 while (idx < ARRAY_SIZE(settings) &&
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300239 (settings[idx].speed != speed || settings[idx].duplex != duplex))
Andy Fleming00db8182005-07-30 19:31:23 -0400240 idx++;
241
242 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
243}
244
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800245/**
246 * phy_find_valid - find a PHY setting that matches the requested features mask
247 * @idx: The first index in settings[] to search
248 * @features: A mask of the valid settings
Andy Fleming00db8182005-07-30 19:31:23 -0400249 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800250 * Description: Returns the index of the first valid setting less
Andy Fleming00db8182005-07-30 19:31:23 -0400251 * than or equal to the one pointed to by idx, as determined by
252 * the mask in features. Returns the index of the last setting
253 * if nothing else matches.
254 */
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -0700255static inline unsigned int phy_find_valid(unsigned int idx, u32 features)
Andy Fleming00db8182005-07-30 19:31:23 -0400256{
257 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
258 idx++;
259
260 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
261}
262
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800263/**
Zach Brown1f9127c2016-10-17 10:49:54 -0500264 * phy_supported_speeds - return all speeds currently supported by a phy device
265 * @phy: The phy device to return supported speeds of.
266 * @speeds: buffer to store supported speeds in.
267 * @size: size of speeds buffer.
268 *
269 * Description: Returns the number of supported speeds, and fills the speeds
270 * buffer with the supported speeds. If speeds buffer is too small to contain
271 * all currently supported speeds, will return as many speeds as can fit.
272 */
273unsigned int phy_supported_speeds(struct phy_device *phy,
274 unsigned int *speeds,
275 unsigned int size)
276{
277 unsigned int count = 0;
278 unsigned int idx = 0;
279
280 while (idx < MAX_NUM_SETTINGS && count < size) {
281 idx = phy_find_valid(idx, phy->supported);
282
283 if (!(settings[idx].setting & phy->supported))
284 break;
285
286 /* Assumes settings are grouped by speed */
287 if ((count == 0) ||
288 (speeds[count - 1] != settings[idx].speed)) {
289 speeds[count] = settings[idx].speed;
290 count++;
291 }
292 idx++;
293 }
294
295 return count;
296}
297
298/**
Guenter Roeck54da5a82015-02-17 09:36:22 -0800299 * phy_check_valid - check if there is a valid PHY setting which matches
300 * speed, duplex, and feature mask
301 * @speed: speed to match
302 * @duplex: duplex to match
303 * @features: A mask of the valid settings
304 *
305 * Description: Returns true if there is a valid setting, false otherwise.
306 */
307static inline bool phy_check_valid(int speed, int duplex, u32 features)
308{
309 unsigned int idx;
310
311 idx = phy_find_valid(phy_find_setting(speed, duplex), features);
312
313 return settings[idx].speed == speed && settings[idx].duplex == duplex &&
314 (settings[idx].setting & features);
315}
316
317/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800318 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
319 * @phydev: the target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400320 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800321 * Description: Make sure the PHY is set to supported speeds and
Andy Fleming00db8182005-07-30 19:31:23 -0400322 * duplexes. Drop down by one in this order: 1000/FULL,
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800323 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
Andy Fleming00db8182005-07-30 19:31:23 -0400324 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000325static void phy_sanitize_settings(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400326{
327 u32 features = phydev->supported;
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -0700328 unsigned int idx;
Andy Fleming00db8182005-07-30 19:31:23 -0400329
330 /* Sanitize settings based on PHY capabilities */
331 if ((features & SUPPORTED_Autoneg) == 0)
Domen Puncer163642a2007-08-07 12:12:41 +0200332 phydev->autoneg = AUTONEG_DISABLE;
Andy Fleming00db8182005-07-30 19:31:23 -0400333
334 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
335 features);
336
337 phydev->speed = settings[idx].speed;
338 phydev->duplex = settings[idx].duplex;
339}
Andy Fleming00db8182005-07-30 19:31:23 -0400340
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800341/**
342 * phy_ethtool_sset - generic ethtool sset function, handles all the details
343 * @phydev: target phy_device struct
344 * @cmd: ethtool_cmd
Andy Fleming00db8182005-07-30 19:31:23 -0400345 *
346 * A few notes about parameter checking:
347 * - We don't set port or transceiver, so we don't care what they
348 * were set to.
349 * - phy_start_aneg() will make sure forced settings are sane, and
350 * choose the next best ones from the ones selected, so we don't
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800351 * care if ethtool tries to give us bad values.
Andy Fleming00db8182005-07-30 19:31:23 -0400352 */
353int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
354{
David Decotigny25db0332011-04-27 18:32:39 +0000355 u32 speed = ethtool_cmd_speed(cmd);
356
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100357 if (cmd->phy_address != phydev->mdio.addr)
Andy Fleming00db8182005-07-30 19:31:23 -0400358 return -EINVAL;
359
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300360 /* We make sure that we don't pass unsupported values in to the PHY */
Andy Fleming00db8182005-07-30 19:31:23 -0400361 cmd->advertising &= phydev->supported;
362
363 /* Verify the settings we care about. */
364 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
365 return -EINVAL;
366
367 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
368 return -EINVAL;
369
Joe Perches8e95a202009-12-03 07:58:21 +0000370 if (cmd->autoneg == AUTONEG_DISABLE &&
David Decotigny25db0332011-04-27 18:32:39 +0000371 ((speed != SPEED_1000 &&
372 speed != SPEED_100 &&
373 speed != SPEED_10) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000374 (cmd->duplex != DUPLEX_HALF &&
375 cmd->duplex != DUPLEX_FULL)))
Andy Fleming00db8182005-07-30 19:31:23 -0400376 return -EINVAL;
377
378 phydev->autoneg = cmd->autoneg;
379
David Decotigny25db0332011-04-27 18:32:39 +0000380 phydev->speed = speed;
Andy Fleming00db8182005-07-30 19:31:23 -0400381
382 phydev->advertising = cmd->advertising;
383
384 if (AUTONEG_ENABLE == cmd->autoneg)
385 phydev->advertising |= ADVERTISED_Autoneg;
386 else
387 phydev->advertising &= ~ADVERTISED_Autoneg;
388
389 phydev->duplex = cmd->duplex;
390
David Thomson634ec362015-07-10 13:56:54 +1200391 phydev->mdix = cmd->eth_tp_mdix_ctrl;
392
Andy Fleming00db8182005-07-30 19:31:23 -0400393 /* Restart the PHY */
394 phy_start_aneg(phydev);
395
396 return 0;
397}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600398EXPORT_SYMBOL(phy_ethtool_sset);
Andy Fleming00db8182005-07-30 19:31:23 -0400399
Philippe Reynes2d551732016-04-15 00:35:00 +0200400int phy_ethtool_ksettings_set(struct phy_device *phydev,
401 const struct ethtool_link_ksettings *cmd)
402{
403 u8 autoneg = cmd->base.autoneg;
404 u8 duplex = cmd->base.duplex;
405 u32 speed = cmd->base.speed;
406 u32 advertising;
407
408 if (cmd->base.phy_address != phydev->mdio.addr)
409 return -EINVAL;
410
411 ethtool_convert_link_mode_to_legacy_u32(&advertising,
412 cmd->link_modes.advertising);
413
414 /* We make sure that we don't pass unsupported values in to the PHY */
415 advertising &= phydev->supported;
416
417 /* Verify the settings we care about. */
418 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
419 return -EINVAL;
420
421 if (autoneg == AUTONEG_ENABLE && advertising == 0)
422 return -EINVAL;
423
424 if (autoneg == AUTONEG_DISABLE &&
425 ((speed != SPEED_1000 &&
426 speed != SPEED_100 &&
427 speed != SPEED_10) ||
428 (duplex != DUPLEX_HALF &&
429 duplex != DUPLEX_FULL)))
430 return -EINVAL;
431
432 phydev->autoneg = autoneg;
433
434 phydev->speed = speed;
435
436 phydev->advertising = advertising;
437
438 if (autoneg == AUTONEG_ENABLE)
439 phydev->advertising |= ADVERTISED_Autoneg;
440 else
441 phydev->advertising &= ~ADVERTISED_Autoneg;
442
443 phydev->duplex = duplex;
444
445 phydev->mdix = cmd->base.eth_tp_mdix_ctrl;
446
447 /* Restart the PHY */
448 phy_start_aneg(phydev);
449
450 return 0;
451}
452EXPORT_SYMBOL(phy_ethtool_ksettings_set);
453
Andy Fleming00db8182005-07-30 19:31:23 -0400454int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
455{
456 cmd->supported = phydev->supported;
457
458 cmd->advertising = phydev->advertising;
Florian Fainelli114002b2013-12-06 13:01:30 -0800459 cmd->lp_advertising = phydev->lp_advertising;
Andy Fleming00db8182005-07-30 19:31:23 -0400460
David Decotigny70739492011-04-27 18:32:40 +0000461 ethtool_cmd_speed_set(cmd, phydev->speed);
Andy Fleming00db8182005-07-30 19:31:23 -0400462 cmd->duplex = phydev->duplex;
Florian Fainellic88838c2014-02-13 16:08:43 -0800463 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
464 cmd->port = PORT_BNC;
465 else
466 cmd->port = PORT_MII;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100467 cmd->phy_address = phydev->mdio.addr;
Florian Fainelli4284b6a2013-05-23 01:11:12 +0000468 cmd->transceiver = phy_is_internal(phydev) ?
469 XCVR_INTERNAL : XCVR_EXTERNAL;
Andy Fleming00db8182005-07-30 19:31:23 -0400470 cmd->autoneg = phydev->autoneg;
David Thomson239aa552015-07-10 16:28:25 +1200471 cmd->eth_tp_mdix_ctrl = phydev->mdix;
Andy Fleming00db8182005-07-30 19:31:23 -0400472
473 return 0;
474}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600475EXPORT_SYMBOL(phy_ethtool_gset);
Andy Fleming00db8182005-07-30 19:31:23 -0400476
Philippe Reynes2d551732016-04-15 00:35:00 +0200477int phy_ethtool_ksettings_get(struct phy_device *phydev,
478 struct ethtool_link_ksettings *cmd)
479{
480 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
481 phydev->supported);
482
483 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
484 phydev->advertising);
485
486 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
487 phydev->lp_advertising);
488
489 cmd->base.speed = phydev->speed;
490 cmd->base.duplex = phydev->duplex;
491 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
492 cmd->base.port = PORT_BNC;
493 else
494 cmd->base.port = PORT_MII;
495
496 cmd->base.phy_address = phydev->mdio.addr;
497 cmd->base.autoneg = phydev->autoneg;
498 cmd->base.eth_tp_mdix_ctrl = phydev->mdix;
499
500 return 0;
501}
502EXPORT_SYMBOL(phy_ethtool_ksettings_get);
503
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800504/**
505 * phy_mii_ioctl - generic PHY MII ioctl interface
506 * @phydev: the phy_device struct
Randy Dunlap00c7d922010-08-09 13:41:59 +0000507 * @ifr: &struct ifreq for socket ioctl's
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800508 * @cmd: ioctl cmd to execute
509 *
510 * Note that this function is currently incompatible with the
Andy Fleming00db8182005-07-30 19:31:23 -0400511 * PHYCONTROL layer. It changes registers without regard to
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800512 * current state. Use at own risk.
Andy Fleming00db8182005-07-30 19:31:23 -0400513 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300514int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
Andy Fleming00db8182005-07-30 19:31:23 -0400515{
Richard Cochran28b04112010-07-17 08:48:55 +0000516 struct mii_ioctl_data *mii_data = if_mii(ifr);
Andy Fleming00db8182005-07-30 19:31:23 -0400517 u16 val = mii_data->val_in;
Brian Hill79ce0472014-11-11 13:39:39 -0700518 bool change_autoneg = false;
Andy Fleming00db8182005-07-30 19:31:23 -0400519
520 switch (cmd) {
521 case SIOCGMIIPHY:
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100522 mii_data->phy_id = phydev->mdio.addr;
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000523 /* fall through */
524
Andy Fleming00db8182005-07-30 19:31:23 -0400525 case SIOCGMIIREG:
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100526 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
527 mii_data->phy_id,
Peter Korsgaardaf1dc132011-03-10 06:52:13 +0000528 mii_data->reg_num);
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300529 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400530
531 case SIOCSMIIREG:
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100532 if (mii_data->phy_id == phydev->mdio.addr) {
Florian Fainellie1093742013-12-17 21:38:12 -0800533 switch (mii_data->reg_num) {
Andy Fleming00db8182005-07-30 19:31:23 -0400534 case MII_BMCR:
Brian Hill79ce0472014-11-11 13:39:39 -0700535 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
536 if (phydev->autoneg == AUTONEG_ENABLE)
537 change_autoneg = true;
Andy Fleming00db8182005-07-30 19:31:23 -0400538 phydev->autoneg = AUTONEG_DISABLE;
Brian Hill79ce0472014-11-11 13:39:39 -0700539 if (val & BMCR_FULLDPLX)
540 phydev->duplex = DUPLEX_FULL;
541 else
542 phydev->duplex = DUPLEX_HALF;
543 if (val & BMCR_SPEED1000)
544 phydev->speed = SPEED_1000;
545 else if (val & BMCR_SPEED100)
546 phydev->speed = SPEED_100;
547 else phydev->speed = SPEED_10;
548 }
549 else {
550 if (phydev->autoneg == AUTONEG_DISABLE)
551 change_autoneg = true;
Andy Fleming00db8182005-07-30 19:31:23 -0400552 phydev->autoneg = AUTONEG_ENABLE;
Brian Hill79ce0472014-11-11 13:39:39 -0700553 }
Andy Fleming00db8182005-07-30 19:31:23 -0400554 break;
555 case MII_ADVERTISE:
Brian Hill79ce0472014-11-11 13:39:39 -0700556 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
557 change_autoneg = true;
Andy Fleming00db8182005-07-30 19:31:23 -0400558 break;
559 default:
560 /* do nothing */
561 break;
562 }
563 }
564
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100565 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
Peter Korsgaardaf1dc132011-03-10 06:52:13 +0000566 mii_data->reg_num, val);
567
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100568 if (mii_data->phy_id == phydev->mdio.addr &&
Jérôme Pouillercf18b772015-12-03 10:02:35 +0100569 mii_data->reg_num == MII_BMCR &&
Florian Fainelli2613f952013-12-06 13:01:31 -0800570 val & BMCR_RESET)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300571 return phy_init_hw(phydev);
Brian Hill79ce0472014-11-11 13:39:39 -0700572
573 if (change_autoneg)
574 return phy_start_aneg(phydev);
575
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300576 return 0;
David Woodhousedda93b482007-11-28 19:56:34 +0000577
Richard Cochranc1f19b52010-07-17 08:49:36 +0000578 case SIOCSHWTSTAMP:
579 if (phydev->drv->hwtstamp)
580 return phydev->drv->hwtstamp(phydev, ifr);
581 /* fall through */
582
David Woodhousedda93b482007-11-28 19:56:34 +0000583 default:
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000584 return -EOPNOTSUPP;
Andy Fleming00db8182005-07-30 19:31:23 -0400585 }
Andy Fleming00db8182005-07-30 19:31:23 -0400586}
Domen Puncer680e9fe2007-09-17 22:21:40 +0200587EXPORT_SYMBOL(phy_mii_ioctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400588
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800589/**
590 * phy_start_aneg - start auto-negotiation for this PHY device
591 * @phydev: the phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500592 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800593 * Description: Sanitizes the settings (if we're not autonegotiating
594 * them), and then calls the driver's config_aneg function.
595 * If the PHYCONTROL Layer is operating, we change the state to
596 * reflect the beginning of Auto-negotiation or forcing.
Andy Fleminge1393452005-08-24 18:46:21 -0500597 */
598int phy_start_aneg(struct phy_device *phydev)
599{
600 int err;
601
Nate Case35b5f6b2008-01-29 10:05:09 -0600602 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500603
604 if (AUTONEG_DISABLE == phydev->autoneg)
605 phy_sanitize_settings(phydev);
606
Ben Hutchings9b3320e2015-01-27 00:58:15 +0000607 /* Invalidate LP advertising flags */
608 phydev->lp_advertising = 0;
609
Andy Fleminge1393452005-08-24 18:46:21 -0500610 err = phydev->drv->config_aneg(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500611 if (err < 0)
612 goto out_unlock;
613
614 if (phydev->state != PHY_HALTED) {
615 if (AUTONEG_ENABLE == phydev->autoneg) {
616 phydev->state = PHY_AN;
617 phydev->link_timeout = PHY_AN_TIMEOUT;
618 } else {
619 phydev->state = PHY_FORCING;
620 phydev->link_timeout = PHY_FORCE_TIMEOUT;
621 }
622 }
623
624out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600625 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500626 return err;
627}
628EXPORT_SYMBOL(phy_start_aneg);
629
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800630/**
631 * phy_start_machine - start PHY state machine tracking
632 * @phydev: the phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400633 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800634 * Description: The PHY infrastructure can run a state machine
Andy Fleming00db8182005-07-30 19:31:23 -0400635 * which tracks whether the PHY is starting up, negotiating,
636 * etc. This function starts the timer which tracks the state
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300637 * of the PHY. If you want to maintain your own state machine,
638 * do not call this function.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800639 */
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300640void phy_start_machine(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400641{
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530642 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
Andy Fleming00db8182005-07-30 19:31:23 -0400643}
644
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800645/**
Andrew Lunn3c293f42016-10-12 22:14:53 +0200646 * phy_trigger_machine - trigger the state machine to run
647 *
648 * @phydev: the phy_device struct
649 *
650 * Description: There has been a change in state which requires that the
651 * state machine runs.
652 */
653
654static void phy_trigger_machine(struct phy_device *phydev)
655{
656 cancel_delayed_work_sync(&phydev->state_queue);
657 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
658}
659
660/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800661 * phy_stop_machine - stop the PHY state machine tracking
662 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400663 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800664 * Description: Stops the state machine timer, sets the state to UP
Sergei Shtylylov817acf52006-07-26 00:53:53 +0400665 * (unless it wasn't up yet). This function must be called BEFORE
Andy Fleming00db8182005-07-30 19:31:23 -0400666 * phy_detach.
667 */
668void phy_stop_machine(struct phy_device *phydev)
669{
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700670 cancel_delayed_work_sync(&phydev->state_queue);
Andy Fleming00db8182005-07-30 19:31:23 -0400671
Nate Case35b5f6b2008-01-29 10:05:09 -0600672 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400673 if (phydev->state > PHY_UP)
674 phydev->state = PHY_UP;
Nate Case35b5f6b2008-01-29 10:05:09 -0600675 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400676}
677
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800678/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800679 * phy_error - enter HALTED state for this PHY device
680 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400681 *
682 * Moves the PHY to the HALTED state in response to a read
683 * or write error, and tells the controller the link is down.
684 * Must not be called from interrupt context, or while the
685 * phydev->lock is held.
686 */
Andy Fleming9b9a8bf2008-05-02 13:00:51 -0500687static void phy_error(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400688{
Nate Case35b5f6b2008-01-29 10:05:09 -0600689 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400690 phydev->state = PHY_HALTED;
Nate Case35b5f6b2008-01-29 10:05:09 -0600691 mutex_unlock(&phydev->lock);
Andrew Lunn3c293f42016-10-12 22:14:53 +0200692
693 phy_trigger_machine(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400694}
695
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800696/**
697 * phy_interrupt - PHY interrupt handler
698 * @irq: interrupt line
699 * @phy_dat: phy_device pointer
Andy Fleminge1393452005-08-24 18:46:21 -0500700 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800701 * Description: When a PHY interrupt occurs, the handler disables
Andrew Lunn664fcf12016-10-16 19:56:51 +0200702 * interrupts, and uses phy_change to handle the interrupt.
Andy Fleminge1393452005-08-24 18:46:21 -0500703 */
David Howells7d12e782006-10-05 14:55:46 +0100704static irqreturn_t phy_interrupt(int irq, void *phy_dat)
Andy Fleminge1393452005-08-24 18:46:21 -0500705{
706 struct phy_device *phydev = phy_dat;
707
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100708 if (PHY_HALTED == phydev->state)
709 return IRQ_NONE; /* It can't be ours. */
710
Andy Fleminge1393452005-08-24 18:46:21 -0500711 disable_irq_nosync(irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700712 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500713
Andrew Lunn664fcf12016-10-16 19:56:51 +0200714 phy_change(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500715
716 return IRQ_HANDLED;
717}
718
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800719/**
720 * phy_enable_interrupts - Enable the interrupts from the PHY side
721 * @phydev: target phy_device struct
722 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000723static int phy_enable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400724{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300725 int err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400726
Andy Fleminge1393452005-08-24 18:46:21 -0500727 if (err < 0)
728 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400729
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300730 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400731}
Andy Fleming00db8182005-07-30 19:31:23 -0400732
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800733/**
734 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
735 * @phydev: target phy_device struct
736 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000737static int phy_disable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400738{
739 int err;
740
741 /* Disable PHY interrupts */
742 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400743 if (err)
744 goto phy_err;
745
746 /* Clear the interrupt */
747 err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400748 if (err)
749 goto phy_err;
750
751 return 0;
752
753phy_err:
754 phy_error(phydev);
755
756 return err;
757}
Andy Fleminge1393452005-08-24 18:46:21 -0500758
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800759/**
760 * phy_start_interrupts - request and enable interrupts for a PHY device
761 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500762 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800763 * Description: Request the interrupt for the given PHY.
764 * If this fails, then we set irq to PHY_POLL.
Andy Fleminge1393452005-08-24 18:46:21 -0500765 * Otherwise, we enable the interrupts in the PHY.
Andy Fleminge1393452005-08-24 18:46:21 -0500766 * This should only be called with a valid IRQ number.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800767 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500768 */
769int phy_start_interrupts(struct phy_device *phydev)
770{
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700771 atomic_set(&phydev->irq_disable, 0);
Andrew Lunnc974bdb2016-10-16 19:56:50 +0200772 if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
773 IRQF_ONESHOT | IRQF_SHARED,
Andrew Lunnae0219c2016-10-16 19:56:52 +0200774 phydev_name(phydev), phydev) < 0) {
Joe Perches8d242482012-06-09 07:49:07 +0000775 pr_warn("%s: Can't get IRQ %d (PHY)\n",
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100776 phydev->mdio.bus->name, phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500777 phydev->irq = PHY_POLL;
778 return 0;
779 }
780
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300781 return phy_enable_interrupts(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500782}
783EXPORT_SYMBOL(phy_start_interrupts);
784
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800785/**
786 * phy_stop_interrupts - disable interrupts from a PHY device
787 * @phydev: target phy_device struct
788 */
Andy Fleminge1393452005-08-24 18:46:21 -0500789int phy_stop_interrupts(struct phy_device *phydev)
790{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300791 int err = phy_disable_interrupts(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500792
793 if (err)
794 phy_error(phydev);
795
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700796 free_irq(phydev->irq, phydev);
797
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300798 /* If work indeed has been cancelled, disable_irq() will have
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700799 * been left unbalanced from phy_interrupt() and enable_irq()
800 * has to be called so that other devices on the line work.
801 */
802 while (atomic_dec_return(&phydev->irq_disable) >= 0)
803 enable_irq(phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500804
805 return err;
806}
807EXPORT_SYMBOL(phy_stop_interrupts);
808
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800809/**
Andrew Lunn664fcf12016-10-16 19:56:51 +0200810 * phy_change - Called by the phy_interrupt to handle PHY changes
811 * @phydev: phy_device struct that interrupted
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800812 */
Andrew Lunn664fcf12016-10-16 19:56:51 +0200813void phy_change(struct phy_device *phydev)
Andy Fleminge1393452005-08-24 18:46:21 -0500814{
Florian Fainellideccd16f92016-01-18 19:33:07 -0800815 if (phy_interrupt_is_valid(phydev)) {
816 if (phydev->drv->did_interrupt &&
817 !phydev->drv->did_interrupt(phydev))
818 goto ignore;
Anatolij Gustschina8729eb2009-04-07 02:01:42 +0000819
Florian Fainellideccd16f92016-01-18 19:33:07 -0800820 if (phy_disable_interrupts(phydev))
821 goto phy_err;
822 }
Andy Fleminge1393452005-08-24 18:46:21 -0500823
Nate Case35b5f6b2008-01-29 10:05:09 -0600824 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500825 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
826 phydev->state = PHY_CHANGELINK;
Nate Case35b5f6b2008-01-29 10:05:09 -0600827 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500828
Florian Fainellideccd16f92016-01-18 19:33:07 -0800829 if (phy_interrupt_is_valid(phydev)) {
830 atomic_dec(&phydev->irq_disable);
831 enable_irq(phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500832
Florian Fainellideccd16f92016-01-18 19:33:07 -0800833 /* Reenable interrupts */
834 if (PHY_HALTED != phydev->state &&
835 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
836 goto irq_enable_err;
837 }
Andy Fleminge1393452005-08-24 18:46:21 -0500838
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700839 /* reschedule state queue work to run as soon as possible */
Andrew Lunn3c293f42016-10-12 22:14:53 +0200840 phy_trigger_machine(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500841 return;
842
Anatolij Gustschina8729eb2009-04-07 02:01:42 +0000843ignore:
844 atomic_dec(&phydev->irq_disable);
845 enable_irq(phydev->irq);
846 return;
847
Andy Fleminge1393452005-08-24 18:46:21 -0500848irq_enable_err:
849 disable_irq(phydev->irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700850 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500851phy_err:
852 phy_error(phydev);
853}
854
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800855/**
Andrew Lunn664fcf12016-10-16 19:56:51 +0200856 * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
857 * @work: work_struct that describes the work to be done
858 */
859void phy_change_work(struct work_struct *work)
860{
861 struct phy_device *phydev =
862 container_of(work, struct phy_device, phy_queue);
863
864 phy_change(phydev);
865}
866
867/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800868 * phy_stop - Bring down the PHY link, and stop checking the status
869 * @phydev: target phy_device struct
870 */
Andy Fleminge1393452005-08-24 18:46:21 -0500871void phy_stop(struct phy_device *phydev)
872{
Nate Case35b5f6b2008-01-29 10:05:09 -0600873 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500874
875 if (PHY_HALTED == phydev->state)
876 goto out_unlock;
877
Florian Fainelli2c7b4922013-05-19 22:53:42 +0000878 if (phy_interrupt_is_valid(phydev)) {
Andy Fleminge1393452005-08-24 18:46:21 -0500879 /* Disable PHY Interrupts */
880 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleminge1393452005-08-24 18:46:21 -0500881
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100882 /* Clear any pending interrupts */
883 phy_clear_interrupt(phydev);
884 }
Andy Fleminge1393452005-08-24 18:46:21 -0500885
Maciej W. Rozycki6daf6532007-09-28 22:42:15 -0700886 phydev->state = PHY_HALTED;
887
Andy Fleminge1393452005-08-24 18:46:21 -0500888out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600889 mutex_unlock(&phydev->lock);
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100890
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300891 /* Cannot call flush_scheduled_work() here as desired because
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100892 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
893 * will not reenable interrupts.
894 */
Andy Fleminge1393452005-08-24 18:46:21 -0500895}
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300896EXPORT_SYMBOL(phy_stop);
Andy Fleminge1393452005-08-24 18:46:21 -0500897
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800898/**
899 * phy_start - start or restart a PHY device
900 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500901 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800902 * Description: Indicates the attached device's readiness to
Andy Fleminge1393452005-08-24 18:46:21 -0500903 * handle PHY-related work. Used during startup to start the
904 * PHY, and after a call to phy_stop() to resume operation.
905 * Also used to indicate the MDIO bus has cleared an error
906 * condition.
907 */
908void phy_start(struct phy_device *phydev)
909{
Tim Bealec15e10e2015-05-18 15:38:38 +1200910 bool do_resume = false;
911 int err = 0;
912
Nate Case35b5f6b2008-01-29 10:05:09 -0600913 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500914
915 switch (phydev->state) {
Florian Fainellie1093742013-12-17 21:38:12 -0800916 case PHY_STARTING:
917 phydev->state = PHY_PENDING;
918 break;
919 case PHY_READY:
920 phydev->state = PHY_UP;
921 break;
922 case PHY_HALTED:
Tim Bealec15e10e2015-05-18 15:38:38 +1200923 /* make sure interrupts are re-enabled for the PHY */
Shaohui Xie84a527a2016-05-10 17:42:26 +0800924 if (phydev->irq != PHY_POLL) {
925 err = phy_enable_interrupts(phydev);
926 if (err < 0)
927 break;
928 }
Tim Bealec15e10e2015-05-18 15:38:38 +1200929
Florian Fainellie1093742013-12-17 21:38:12 -0800930 phydev->state = PHY_RESUMING;
Tim Bealec15e10e2015-05-18 15:38:38 +1200931 do_resume = true;
932 break;
Florian Fainellie1093742013-12-17 21:38:12 -0800933 default:
934 break;
Andy Fleminge1393452005-08-24 18:46:21 -0500935 }
Nate Case35b5f6b2008-01-29 10:05:09 -0600936 mutex_unlock(&phydev->lock);
Tim Bealec15e10e2015-05-18 15:38:38 +1200937
938 /* if phy was suspended, bring the physical link up again */
939 if (do_resume)
940 phy_resume(phydev);
Andrew Lunn3c293f42016-10-12 22:14:53 +0200941
942 phy_trigger_machine(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500943}
Andy Fleminge1393452005-08-24 18:46:21 -0500944EXPORT_SYMBOL(phy_start);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400945
Zach Brown61a17962016-10-17 10:49:53 -0500946static void phy_adjust_link(struct phy_device *phydev)
947{
948 phydev->adjust_link(phydev->attached_dev);
Zach Brown2e0bc452016-10-17 10:49:55 -0500949 phy_led_trigger_change_speed(phydev);
Zach Brown61a17962016-10-17 10:49:53 -0500950}
951
Nate Case35b5f6b2008-01-29 10:05:09 -0600952/**
953 * phy_state_machine - Handle the state machine
954 * @work: work_struct that describes the work to be done
Nate Case35b5f6b2008-01-29 10:05:09 -0600955 */
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000956void phy_state_machine(struct work_struct *work)
Andy Fleming00db8182005-07-30 19:31:23 -0400957{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700958 struct delayed_work *dwork = to_delayed_work(work);
Nate Case35b5f6b2008-01-29 10:05:09 -0600959 struct phy_device *phydev =
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700960 container_of(dwork, struct phy_device, state_queue);
Tim Bealec15e10e2015-05-18 15:38:38 +1200961 bool needs_aneg = false, do_suspend = false;
Florian Fainelli3e2186e2015-05-16 10:17:56 -0700962 enum phy_state old_state;
Andy Fleming00db8182005-07-30 19:31:23 -0400963 int err = 0;
Shaohui Xie11e122c2015-08-14 12:23:40 +0800964 int old_link;
Andy Fleming00db8182005-07-30 19:31:23 -0400965
Nate Case35b5f6b2008-01-29 10:05:09 -0600966 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400967
Florian Fainelli3e2186e2015-05-16 10:17:56 -0700968 old_state = phydev->state;
969
Daniel Mack2b8f2a22014-06-18 11:01:41 +0200970 if (phydev->drv->link_change_notify)
971 phydev->drv->link_change_notify(phydev);
972
Florian Fainellie1093742013-12-17 21:38:12 -0800973 switch (phydev->state) {
974 case PHY_DOWN:
975 case PHY_STARTING:
976 case PHY_READY:
977 case PHY_PENDING:
978 break;
979 case PHY_UP:
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +0800980 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -0800981
982 phydev->link_timeout = PHY_AN_TIMEOUT;
983
984 break;
985 case PHY_AN:
986 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800987 if (err < 0)
Andy Fleming00db8182005-07-30 19:31:23 -0400988 break;
Florian Fainellie1093742013-12-17 21:38:12 -0800989
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300990 /* If the link is down, give up on negotiation for now */
Florian Fainellie1093742013-12-17 21:38:12 -0800991 if (!phydev->link) {
992 phydev->state = PHY_NOLINK;
993 netif_carrier_off(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -0500994 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800995 break;
996 }
997
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300998 /* Check if negotiation is done. Break if there's an error */
Florian Fainellie1093742013-12-17 21:38:12 -0800999 err = phy_aneg_done(phydev);
1000 if (err < 0)
1001 break;
1002
1003 /* If AN is done, we're running */
1004 if (err > 0) {
1005 phydev->state = PHY_RUNNING;
1006 netif_carrier_on(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001007 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001008
Balakumaran Kannanfa8cdda2014-04-09 09:03:45 +05301009 } else if (0 == phydev->link_timeout--)
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001010 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001011 break;
1012 case PHY_NOLINK:
Andrew Lunn321beec2015-11-16 23:36:46 +01001013 if (phy_interrupt_is_valid(phydev))
1014 break;
1015
Florian Fainellie1093742013-12-17 21:38:12 -08001016 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001017 if (err)
Andy Fleming00db8182005-07-30 19:31:23 -04001018 break;
Andy Fleming6b655522006-10-16 16:19:17 -05001019
Florian Fainellie1093742013-12-17 21:38:12 -08001020 if (phydev->link) {
Balakumaran Kannane46e08b2014-04-24 08:22:47 +05301021 if (AUTONEG_ENABLE == phydev->autoneg) {
1022 err = phy_aneg_done(phydev);
1023 if (err < 0)
1024 break;
1025
1026 if (!err) {
1027 phydev->state = PHY_AN;
1028 phydev->link_timeout = PHY_AN_TIMEOUT;
1029 break;
1030 }
1031 }
Florian Fainellie1093742013-12-17 21:38:12 -08001032 phydev->state = PHY_RUNNING;
1033 netif_carrier_on(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001034 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001035 }
1036 break;
1037 case PHY_FORCING:
1038 err = genphy_update_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001039 if (err)
1040 break;
Andy Fleming6b655522006-10-16 16:19:17 -05001041
Florian Fainellie1093742013-12-17 21:38:12 -08001042 if (phydev->link) {
1043 phydev->state = PHY_RUNNING;
1044 netif_carrier_on(phydev->attached_dev);
1045 } else {
1046 if (0 == phydev->link_timeout--)
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001047 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001048 }
1049
Zach Brown61a17962016-10-17 10:49:53 -05001050 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001051 break;
1052 case PHY_RUNNING:
Florian Fainellid5c3d842016-01-18 19:33:06 -08001053 /* Only register a CHANGE if we are polling and link changed
1054 * since latest checking.
Florian Fainellie1093742013-12-17 21:38:12 -08001055 */
Florian Fainellid5c3d842016-01-18 19:33:06 -08001056 if (phydev->irq == PHY_POLL) {
Shaohui Xie11e122c2015-08-14 12:23:40 +08001057 old_link = phydev->link;
1058 err = phy_read_status(phydev);
1059 if (err)
1060 break;
1061
1062 if (old_link != phydev->link)
1063 phydev->state = PHY_CHANGELINK;
1064 }
Florian Fainellie1093742013-12-17 21:38:12 -08001065 break;
1066 case PHY_CHANGELINK:
1067 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001068 if (err)
1069 break;
1070
1071 if (phydev->link) {
1072 phydev->state = PHY_RUNNING;
1073 netif_carrier_on(phydev->attached_dev);
1074 } else {
1075 phydev->state = PHY_NOLINK;
1076 netif_carrier_off(phydev->attached_dev);
1077 }
1078
Zach Brown61a17962016-10-17 10:49:53 -05001079 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001080
1081 if (phy_interrupt_is_valid(phydev))
1082 err = phy_config_interrupt(phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001083 PHY_INTERRUPT_ENABLED);
Florian Fainellie1093742013-12-17 21:38:12 -08001084 break;
1085 case PHY_HALTED:
1086 if (phydev->link) {
1087 phydev->link = 0;
1088 netif_carrier_off(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001089 phy_adjust_link(phydev);
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001090 do_suspend = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001091 }
1092 break;
1093 case PHY_RESUMING:
Florian Fainellie1093742013-12-17 21:38:12 -08001094 if (AUTONEG_ENABLE == phydev->autoneg) {
Andy Fleming00db8182005-07-30 19:31:23 -04001095 err = phy_aneg_done(phydev);
1096 if (err < 0)
1097 break;
1098
Florian Fainellie1093742013-12-17 21:38:12 -08001099 /* err > 0 if AN is done.
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001100 * Otherwise, it's 0, and we're still waiting for AN
1101 */
Andy Fleming00db8182005-07-30 19:31:23 -04001102 if (err > 0) {
Wade Farnsworth42caa072009-07-01 13:00:34 +00001103 err = phy_read_status(phydev);
1104 if (err)
1105 break;
1106
1107 if (phydev->link) {
1108 phydev->state = PHY_RUNNING;
1109 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001110 } else {
Wade Farnsworth42caa072009-07-01 13:00:34 +00001111 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001112 }
Zach Brown61a17962016-10-17 10:49:53 -05001113 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001114 } else {
1115 phydev->state = PHY_AN;
1116 phydev->link_timeout = PHY_AN_TIMEOUT;
Wade Farnsworth42caa072009-07-01 13:00:34 +00001117 }
Florian Fainellie1093742013-12-17 21:38:12 -08001118 } else {
1119 err = phy_read_status(phydev);
1120 if (err)
1121 break;
1122
1123 if (phydev->link) {
1124 phydev->state = PHY_RUNNING;
1125 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001126 } else {
Florian Fainellie1093742013-12-17 21:38:12 -08001127 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001128 }
Zach Brown61a17962016-10-17 10:49:53 -05001129 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001130 }
1131 break;
Andy Fleming00db8182005-07-30 19:31:23 -04001132 }
1133
Nate Case35b5f6b2008-01-29 10:05:09 -06001134 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001135
1136 if (needs_aneg)
1137 err = phy_start_aneg(phydev);
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001138 else if (do_suspend)
Sebastian Hesselbarthbe9dad12013-12-13 10:20:29 +01001139 phy_suspend(phydev);
1140
Andy Fleming00db8182005-07-30 19:31:23 -04001141 if (err < 0)
1142 phy_error(phydev);
1143
Andrew Lunn72ba48b2016-01-06 20:11:09 +01001144 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1145 phy_state_to_str(old_state),
1146 phy_state_to_str(phydev->state));
Florian Fainelli3e2186e2015-05-16 10:17:56 -07001147
Florian Fainellid5c3d842016-01-18 19:33:06 -08001148 /* Only re-schedule a PHY state machine change if we are polling the
1149 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1150 * between states from phy_mac_interrupt()
1151 */
1152 if (phydev->irq == PHY_POLL)
1153 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1154 PHY_STATE_TIME * HZ);
Nate Case35b5f6b2008-01-29 10:05:09 -06001155}
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001156
Andrew Lunn664fcf12016-10-16 19:56:51 +02001157/**
1158 * phy_mac_interrupt - MAC says the link has changed
1159 * @phydev: phy_device struct with changed link
1160 * @new_link: Link is Up/Down.
1161 *
1162 * Description: The MAC layer is able indicate there has been a change
1163 * in the PHY link status. Set the new link status, and trigger the
1164 * state machine, work a work queue.
1165 */
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001166void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1167{
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001168 phydev->link = new_link;
Florian Fainellideccd16f92016-01-18 19:33:07 -08001169
1170 /* Trigger a state machine change */
1171 queue_work(system_power_efficient_wq, &phydev->phy_queue);
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001172}
1173EXPORT_SYMBOL(phy_mac_interrupt);
1174
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001175static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
1176 int addr)
1177{
1178 /* Write the desired MMD Devad */
1179 bus->write(bus, addr, MII_MMD_CTRL, devad);
1180
1181 /* Write the desired MMD register address */
1182 bus->write(bus, addr, MII_MMD_DATA, prtad);
1183
1184 /* Select the Function : DATA with no post increment */
1185 bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
1186}
1187
1188/**
1189 * phy_read_mmd_indirect - reads data from the MMD registers
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001190 * @phydev: The PHY device bus
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001191 * @prtad: MMD Address
1192 * @devad: MMD DEVAD
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001193 *
1194 * Description: it reads data from the MMD registers (clause 22 to access to
1195 * clause 45) of the specified phy address.
1196 * To read these register we have:
1197 * 1) Write reg 13 // DEVAD
1198 * 2) Write reg 14 // MMD Address
1199 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1200 * 3) Read reg 14 // Read MMD data
1201 */
Andrew Lunn053e7e12016-01-06 20:11:12 +01001202int phy_read_mmd_indirect(struct phy_device *phydev, int prtad, int devad)
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001203{
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001204 struct phy_driver *phydrv = phydev->drv;
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001205 int addr = phydev->mdio.addr;
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001206 int value = -1;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001207
Sergei Shtylyovef899c02015-08-28 21:35:14 +03001208 if (!phydrv->read_mmd_indirect) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001209 struct mii_bus *bus = phydev->mdio.bus;
Russell King05a7f582015-08-25 09:49:47 +01001210
1211 mutex_lock(&bus->mdio_lock);
1212 mmd_phy_indirect(bus, prtad, devad, addr);
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001213
1214 /* Read the content of the MMD's selected register */
Russell King05a7f582015-08-25 09:49:47 +01001215 value = bus->read(bus, addr, MII_MMD_DATA);
1216 mutex_unlock(&bus->mdio_lock);
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001217 } else {
1218 value = phydrv->read_mmd_indirect(phydev, prtad, devad, addr);
1219 }
1220 return value;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001221}
Florian Fainelli66ce7fb2014-08-22 18:55:43 -07001222EXPORT_SYMBOL(phy_read_mmd_indirect);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001223
1224/**
1225 * phy_write_mmd_indirect - writes data to the MMD registers
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001226 * @phydev: The PHY device
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001227 * @prtad: MMD Address
1228 * @devad: MMD DEVAD
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001229 * @data: data to write in the MMD register
1230 *
1231 * Description: Write data from the MMD registers of the specified
1232 * phy address.
1233 * To write these register we have:
1234 * 1) Write reg 13 // DEVAD
1235 * 2) Write reg 14 // MMD Address
1236 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1237 * 3) Write reg 14 // Write MMD data
1238 */
Florian Fainelli66ce7fb2014-08-22 18:55:43 -07001239void phy_write_mmd_indirect(struct phy_device *phydev, int prtad,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001240 int devad, u32 data)
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001241{
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001242 struct phy_driver *phydrv = phydev->drv;
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001243 int addr = phydev->mdio.addr;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001244
Sergei Shtylyovef899c02015-08-28 21:35:14 +03001245 if (!phydrv->write_mmd_indirect) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001246 struct mii_bus *bus = phydev->mdio.bus;
Russell King05a7f582015-08-25 09:49:47 +01001247
1248 mutex_lock(&bus->mdio_lock);
1249 mmd_phy_indirect(bus, prtad, devad, addr);
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001250
1251 /* Write the data into MMD's selected register */
Russell King05a7f582015-08-25 09:49:47 +01001252 bus->write(bus, addr, MII_MMD_DATA, data);
1253 mutex_unlock(&bus->mdio_lock);
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001254 } else {
1255 phydrv->write_mmd_indirect(phydev, prtad, devad, addr, data);
1256 }
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001257}
Florian Fainelli66ce7fb2014-08-22 18:55:43 -07001258EXPORT_SYMBOL(phy_write_mmd_indirect);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001259
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001260/**
1261 * phy_init_eee - init and check the EEE feature
1262 * @phydev: target phy_device struct
1263 * @clk_stop_enable: PHY may stop the clock during LPI
1264 *
1265 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1266 * is supported by looking at the MMD registers 3.20 and 7.60/61
1267 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1268 * bit if required.
1269 */
1270int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1271{
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001272 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1273 * Also EEE feature is active when core is operating with MII, GMII
Florian Fainelli7e140692015-05-15 16:30:41 -07001274 * or RGMII (all kinds). Internal PHYs are also allowed to proceed and
1275 * should return an error if they do not support EEE.
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001276 */
1277 if ((phydev->duplex == DUPLEX_FULL) &&
1278 ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
1279 (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
Florian Fainelli32a64162015-05-26 12:19:59 -07001280 phy_interface_is_rgmii(phydev) ||
Florian Fainellia9f63092014-08-22 18:55:44 -07001281 phy_is_internal(phydev))) {
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001282 int eee_lp, eee_cap, eee_adv;
1283 u32 lp, cap, adv;
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -07001284 int status;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001285
1286 /* Read phy status to properly get the right settings */
1287 status = phy_read_status(phydev);
1288 if (status)
1289 return status;
1290
1291 /* First check if the EEE ability is supported */
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001292 eee_cap = phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_ABLE,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001293 MDIO_MMD_PCS);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001294 if (eee_cap <= 0)
1295 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001296
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001297 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001298 if (!cap)
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001299 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001300
1301 /* Check which link settings negotiated and verify it in
1302 * the EEE advertising registers.
1303 */
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001304 eee_lp = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_LPABLE,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001305 MDIO_MMD_AN);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001306 if (eee_lp <= 0)
1307 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001308
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001309 eee_adv = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_ADV,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001310 MDIO_MMD_AN);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001311 if (eee_adv <= 0)
1312 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001313
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001314 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1315 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
Guenter Roeck54da5a82015-02-17 09:36:22 -08001316 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001317 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001318
1319 if (clk_stop_enable) {
1320 /* Configure the PHY to stop receiving xMII
1321 * clock while it is signaling LPI.
1322 */
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001323 int val = phy_read_mmd_indirect(phydev, MDIO_CTRL1,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001324 MDIO_MMD_PCS);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001325 if (val < 0)
1326 return val;
1327
1328 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001329 phy_write_mmd_indirect(phydev, MDIO_CTRL1,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001330 MDIO_MMD_PCS, val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001331 }
1332
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001333 return 0; /* EEE supported */
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001334 }
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001335eee_exit_err:
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001336 return -EPROTONOSUPPORT;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001337}
1338EXPORT_SYMBOL(phy_init_eee);
1339
1340/**
1341 * phy_get_eee_err - report the EEE wake error count
1342 * @phydev: target phy_device struct
1343 *
1344 * Description: it is to report the number of time where the PHY
1345 * failed to complete its normal wake sequence.
1346 */
1347int phy_get_eee_err(struct phy_device *phydev)
1348{
Andrew Lunn053e7e12016-01-06 20:11:12 +01001349 return phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_WK_ERR, MDIO_MMD_PCS);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001350}
1351EXPORT_SYMBOL(phy_get_eee_err);
1352
1353/**
1354 * phy_ethtool_get_eee - get EEE supported and status
1355 * @phydev: target phy_device struct
1356 * @data: ethtool_eee data
1357 *
1358 * Description: it reportes the Supported/Advertisement/LP Advertisement
1359 * capabilities.
1360 */
1361int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1362{
1363 int val;
1364
1365 /* Get Supported EEE */
Andrew Lunn053e7e12016-01-06 20:11:12 +01001366 val = phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_ABLE, MDIO_MMD_PCS);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001367 if (val < 0)
1368 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001369 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001370
1371 /* Get advertisement EEE */
Andrew Lunn053e7e12016-01-06 20:11:12 +01001372 val = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_ADV, MDIO_MMD_AN);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001373 if (val < 0)
1374 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001375 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001376
1377 /* Get LP advertisement EEE */
Andrew Lunn053e7e12016-01-06 20:11:12 +01001378 val = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_LPABLE, MDIO_MMD_AN);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001379 if (val < 0)
1380 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001381 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001382
1383 return 0;
1384}
1385EXPORT_SYMBOL(phy_ethtool_get_eee);
1386
1387/**
1388 * phy_ethtool_set_eee - set EEE supported and status
1389 * @phydev: target phy_device struct
1390 * @data: ethtool_eee data
1391 *
1392 * Description: it is to program the Advertisement EEE register.
1393 */
1394int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1395{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001396 int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001397
Andrew Lunn053e7e12016-01-06 20:11:12 +01001398 phy_write_mmd_indirect(phydev, MDIO_AN_EEE_ADV, MDIO_MMD_AN, val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001399
1400 return 0;
1401}
1402EXPORT_SYMBOL(phy_ethtool_set_eee);
Michael Stapelberg42e836e2013-03-11 13:56:44 +00001403
1404int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1405{
1406 if (phydev->drv->set_wol)
1407 return phydev->drv->set_wol(phydev, wol);
1408
1409 return -EOPNOTSUPP;
1410}
1411EXPORT_SYMBOL(phy_ethtool_set_wol);
1412
1413void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1414{
1415 if (phydev->drv->get_wol)
1416 phydev->drv->get_wol(phydev, wol);
1417}
1418EXPORT_SYMBOL(phy_ethtool_get_wol);
Philippe Reynes9d9a77c2016-05-10 00:19:41 +02001419
1420int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1421 struct ethtool_link_ksettings *cmd)
1422{
1423 struct phy_device *phydev = ndev->phydev;
1424
1425 if (!phydev)
1426 return -ENODEV;
1427
1428 return phy_ethtool_ksettings_get(phydev, cmd);
1429}
1430EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1431
1432int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1433 const struct ethtool_link_ksettings *cmd)
1434{
1435 struct phy_device *phydev = ndev->phydev;
1436
1437 if (!phydev)
1438 return -ENODEV;
1439
1440 return phy_ethtool_ksettings_set(phydev, cmd);
1441}
1442EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);