blob: ee3c793124c7649eb75fd91537ed980dfae1bb64 [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);
949}
950
Nate Case35b5f6b2008-01-29 10:05:09 -0600951/**
952 * phy_state_machine - Handle the state machine
953 * @work: work_struct that describes the work to be done
Nate Case35b5f6b2008-01-29 10:05:09 -0600954 */
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000955void phy_state_machine(struct work_struct *work)
Andy Fleming00db8182005-07-30 19:31:23 -0400956{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700957 struct delayed_work *dwork = to_delayed_work(work);
Nate Case35b5f6b2008-01-29 10:05:09 -0600958 struct phy_device *phydev =
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700959 container_of(dwork, struct phy_device, state_queue);
Tim Bealec15e10e2015-05-18 15:38:38 +1200960 bool needs_aneg = false, do_suspend = false;
Florian Fainelli3e2186e2015-05-16 10:17:56 -0700961 enum phy_state old_state;
Andy Fleming00db8182005-07-30 19:31:23 -0400962 int err = 0;
Shaohui Xie11e122c2015-08-14 12:23:40 +0800963 int old_link;
Andy Fleming00db8182005-07-30 19:31:23 -0400964
Nate Case35b5f6b2008-01-29 10:05:09 -0600965 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400966
Florian Fainelli3e2186e2015-05-16 10:17:56 -0700967 old_state = phydev->state;
968
Daniel Mack2b8f2a22014-06-18 11:01:41 +0200969 if (phydev->drv->link_change_notify)
970 phydev->drv->link_change_notify(phydev);
971
Florian Fainellie1093742013-12-17 21:38:12 -0800972 switch (phydev->state) {
973 case PHY_DOWN:
974 case PHY_STARTING:
975 case PHY_READY:
976 case PHY_PENDING:
977 break;
978 case PHY_UP:
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +0800979 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -0800980
981 phydev->link_timeout = PHY_AN_TIMEOUT;
982
983 break;
984 case PHY_AN:
985 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800986 if (err < 0)
Andy Fleming00db8182005-07-30 19:31:23 -0400987 break;
Florian Fainellie1093742013-12-17 21:38:12 -0800988
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300989 /* If the link is down, give up on negotiation for now */
Florian Fainellie1093742013-12-17 21:38:12 -0800990 if (!phydev->link) {
991 phydev->state = PHY_NOLINK;
992 netif_carrier_off(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -0500993 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800994 break;
995 }
996
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300997 /* Check if negotiation is done. Break if there's an error */
Florian Fainellie1093742013-12-17 21:38:12 -0800998 err = phy_aneg_done(phydev);
999 if (err < 0)
1000 break;
1001
1002 /* If AN is done, we're running */
1003 if (err > 0) {
1004 phydev->state = PHY_RUNNING;
1005 netif_carrier_on(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001006 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001007
Balakumaran Kannanfa8cdda2014-04-09 09:03:45 +05301008 } else if (0 == phydev->link_timeout--)
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001009 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001010 break;
1011 case PHY_NOLINK:
Andrew Lunn321beec2015-11-16 23:36:46 +01001012 if (phy_interrupt_is_valid(phydev))
1013 break;
1014
Florian Fainellie1093742013-12-17 21:38:12 -08001015 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001016 if (err)
Andy Fleming00db8182005-07-30 19:31:23 -04001017 break;
Andy Fleming6b655522006-10-16 16:19:17 -05001018
Florian Fainellie1093742013-12-17 21:38:12 -08001019 if (phydev->link) {
Balakumaran Kannane46e08b2014-04-24 08:22:47 +05301020 if (AUTONEG_ENABLE == phydev->autoneg) {
1021 err = phy_aneg_done(phydev);
1022 if (err < 0)
1023 break;
1024
1025 if (!err) {
1026 phydev->state = PHY_AN;
1027 phydev->link_timeout = PHY_AN_TIMEOUT;
1028 break;
1029 }
1030 }
Florian Fainellie1093742013-12-17 21:38:12 -08001031 phydev->state = PHY_RUNNING;
1032 netif_carrier_on(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001033 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001034 }
1035 break;
1036 case PHY_FORCING:
1037 err = genphy_update_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001038 if (err)
1039 break;
Andy Fleming6b655522006-10-16 16:19:17 -05001040
Florian Fainellie1093742013-12-17 21:38:12 -08001041 if (phydev->link) {
1042 phydev->state = PHY_RUNNING;
1043 netif_carrier_on(phydev->attached_dev);
1044 } else {
1045 if (0 == phydev->link_timeout--)
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001046 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001047 }
1048
Zach Brown61a17962016-10-17 10:49:53 -05001049 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001050 break;
1051 case PHY_RUNNING:
Florian Fainellid5c3d842016-01-18 19:33:06 -08001052 /* Only register a CHANGE if we are polling and link changed
1053 * since latest checking.
Florian Fainellie1093742013-12-17 21:38:12 -08001054 */
Florian Fainellid5c3d842016-01-18 19:33:06 -08001055 if (phydev->irq == PHY_POLL) {
Shaohui Xie11e122c2015-08-14 12:23:40 +08001056 old_link = phydev->link;
1057 err = phy_read_status(phydev);
1058 if (err)
1059 break;
1060
1061 if (old_link != phydev->link)
1062 phydev->state = PHY_CHANGELINK;
1063 }
Florian Fainellie1093742013-12-17 21:38:12 -08001064 break;
1065 case PHY_CHANGELINK:
1066 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001067 if (err)
1068 break;
1069
1070 if (phydev->link) {
1071 phydev->state = PHY_RUNNING;
1072 netif_carrier_on(phydev->attached_dev);
1073 } else {
1074 phydev->state = PHY_NOLINK;
1075 netif_carrier_off(phydev->attached_dev);
1076 }
1077
Zach Brown61a17962016-10-17 10:49:53 -05001078 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001079
1080 if (phy_interrupt_is_valid(phydev))
1081 err = phy_config_interrupt(phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001082 PHY_INTERRUPT_ENABLED);
Florian Fainellie1093742013-12-17 21:38:12 -08001083 break;
1084 case PHY_HALTED:
1085 if (phydev->link) {
1086 phydev->link = 0;
1087 netif_carrier_off(phydev->attached_dev);
Zach Brown61a17962016-10-17 10:49:53 -05001088 phy_adjust_link(phydev);
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001089 do_suspend = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001090 }
1091 break;
1092 case PHY_RESUMING:
Florian Fainellie1093742013-12-17 21:38:12 -08001093 if (AUTONEG_ENABLE == phydev->autoneg) {
Andy Fleming00db8182005-07-30 19:31:23 -04001094 err = phy_aneg_done(phydev);
1095 if (err < 0)
1096 break;
1097
Florian Fainellie1093742013-12-17 21:38:12 -08001098 /* err > 0 if AN is done.
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001099 * Otherwise, it's 0, and we're still waiting for AN
1100 */
Andy Fleming00db8182005-07-30 19:31:23 -04001101 if (err > 0) {
Wade Farnsworth42caa072009-07-01 13:00:34 +00001102 err = phy_read_status(phydev);
1103 if (err)
1104 break;
1105
1106 if (phydev->link) {
1107 phydev->state = PHY_RUNNING;
1108 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001109 } else {
Wade Farnsworth42caa072009-07-01 13:00:34 +00001110 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001111 }
Zach Brown61a17962016-10-17 10:49:53 -05001112 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001113 } else {
1114 phydev->state = PHY_AN;
1115 phydev->link_timeout = PHY_AN_TIMEOUT;
Wade Farnsworth42caa072009-07-01 13:00:34 +00001116 }
Florian Fainellie1093742013-12-17 21:38:12 -08001117 } else {
1118 err = phy_read_status(phydev);
1119 if (err)
1120 break;
1121
1122 if (phydev->link) {
1123 phydev->state = PHY_RUNNING;
1124 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001125 } else {
Florian Fainellie1093742013-12-17 21:38:12 -08001126 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001127 }
Zach Brown61a17962016-10-17 10:49:53 -05001128 phy_adjust_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001129 }
1130 break;
Andy Fleming00db8182005-07-30 19:31:23 -04001131 }
1132
Nate Case35b5f6b2008-01-29 10:05:09 -06001133 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001134
1135 if (needs_aneg)
1136 err = phy_start_aneg(phydev);
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001137 else if (do_suspend)
Sebastian Hesselbarthbe9dad12013-12-13 10:20:29 +01001138 phy_suspend(phydev);
1139
Andy Fleming00db8182005-07-30 19:31:23 -04001140 if (err < 0)
1141 phy_error(phydev);
1142
Andrew Lunn72ba48b2016-01-06 20:11:09 +01001143 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1144 phy_state_to_str(old_state),
1145 phy_state_to_str(phydev->state));
Florian Fainelli3e2186e2015-05-16 10:17:56 -07001146
Florian Fainellid5c3d842016-01-18 19:33:06 -08001147 /* Only re-schedule a PHY state machine change if we are polling the
1148 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1149 * between states from phy_mac_interrupt()
1150 */
1151 if (phydev->irq == PHY_POLL)
1152 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1153 PHY_STATE_TIME * HZ);
Nate Case35b5f6b2008-01-29 10:05:09 -06001154}
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001155
Andrew Lunn664fcf12016-10-16 19:56:51 +02001156/**
1157 * phy_mac_interrupt - MAC says the link has changed
1158 * @phydev: phy_device struct with changed link
1159 * @new_link: Link is Up/Down.
1160 *
1161 * Description: The MAC layer is able indicate there has been a change
1162 * in the PHY link status. Set the new link status, and trigger the
1163 * state machine, work a work queue.
1164 */
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001165void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1166{
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001167 phydev->link = new_link;
Florian Fainellideccd16f92016-01-18 19:33:07 -08001168
1169 /* Trigger a state machine change */
1170 queue_work(system_power_efficient_wq, &phydev->phy_queue);
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001171}
1172EXPORT_SYMBOL(phy_mac_interrupt);
1173
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001174static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
1175 int addr)
1176{
1177 /* Write the desired MMD Devad */
1178 bus->write(bus, addr, MII_MMD_CTRL, devad);
1179
1180 /* Write the desired MMD register address */
1181 bus->write(bus, addr, MII_MMD_DATA, prtad);
1182
1183 /* Select the Function : DATA with no post increment */
1184 bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
1185}
1186
1187/**
1188 * phy_read_mmd_indirect - reads data from the MMD registers
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001189 * @phydev: The PHY device bus
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001190 * @prtad: MMD Address
1191 * @devad: MMD DEVAD
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001192 *
1193 * Description: it reads data from the MMD registers (clause 22 to access to
1194 * clause 45) of the specified phy address.
1195 * To read these register we have:
1196 * 1) Write reg 13 // DEVAD
1197 * 2) Write reg 14 // MMD Address
1198 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1199 * 3) Read reg 14 // Read MMD data
1200 */
Andrew Lunn053e7e12016-01-06 20:11:12 +01001201int phy_read_mmd_indirect(struct phy_device *phydev, int prtad, int devad)
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001202{
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001203 struct phy_driver *phydrv = phydev->drv;
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001204 int addr = phydev->mdio.addr;
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001205 int value = -1;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001206
Sergei Shtylyovef899c02015-08-28 21:35:14 +03001207 if (!phydrv->read_mmd_indirect) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001208 struct mii_bus *bus = phydev->mdio.bus;
Russell King05a7f582015-08-25 09:49:47 +01001209
1210 mutex_lock(&bus->mdio_lock);
1211 mmd_phy_indirect(bus, prtad, devad, addr);
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001212
1213 /* Read the content of the MMD's selected register */
Russell King05a7f582015-08-25 09:49:47 +01001214 value = bus->read(bus, addr, MII_MMD_DATA);
1215 mutex_unlock(&bus->mdio_lock);
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001216 } else {
1217 value = phydrv->read_mmd_indirect(phydev, prtad, devad, addr);
1218 }
1219 return value;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001220}
Florian Fainelli66ce7fb2014-08-22 18:55:43 -07001221EXPORT_SYMBOL(phy_read_mmd_indirect);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001222
1223/**
1224 * phy_write_mmd_indirect - writes data to the MMD registers
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001225 * @phydev: The PHY device
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001226 * @prtad: MMD Address
1227 * @devad: MMD DEVAD
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001228 * @data: data to write in the MMD register
1229 *
1230 * Description: Write data from the MMD registers of the specified
1231 * phy address.
1232 * To write these register we have:
1233 * 1) Write reg 13 // DEVAD
1234 * 2) Write reg 14 // MMD Address
1235 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1236 * 3) Write reg 14 // Write MMD data
1237 */
Florian Fainelli66ce7fb2014-08-22 18:55:43 -07001238void phy_write_mmd_indirect(struct phy_device *phydev, int prtad,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001239 int devad, u32 data)
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001240{
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001241 struct phy_driver *phydrv = phydev->drv;
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001242 int addr = phydev->mdio.addr;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001243
Sergei Shtylyovef899c02015-08-28 21:35:14 +03001244 if (!phydrv->write_mmd_indirect) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001245 struct mii_bus *bus = phydev->mdio.bus;
Russell King05a7f582015-08-25 09:49:47 +01001246
1247 mutex_lock(&bus->mdio_lock);
1248 mmd_phy_indirect(bus, prtad, devad, addr);
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001249
1250 /* Write the data into MMD's selected register */
Russell King05a7f582015-08-25 09:49:47 +01001251 bus->write(bus, addr, MII_MMD_DATA, data);
1252 mutex_unlock(&bus->mdio_lock);
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001253 } else {
1254 phydrv->write_mmd_indirect(phydev, prtad, devad, addr, data);
1255 }
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001256}
Florian Fainelli66ce7fb2014-08-22 18:55:43 -07001257EXPORT_SYMBOL(phy_write_mmd_indirect);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001258
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001259/**
1260 * phy_init_eee - init and check the EEE feature
1261 * @phydev: target phy_device struct
1262 * @clk_stop_enable: PHY may stop the clock during LPI
1263 *
1264 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1265 * is supported by looking at the MMD registers 3.20 and 7.60/61
1266 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1267 * bit if required.
1268 */
1269int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1270{
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001271 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1272 * Also EEE feature is active when core is operating with MII, GMII
Florian Fainelli7e140692015-05-15 16:30:41 -07001273 * or RGMII (all kinds). Internal PHYs are also allowed to proceed and
1274 * should return an error if they do not support EEE.
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001275 */
1276 if ((phydev->duplex == DUPLEX_FULL) &&
1277 ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
1278 (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
Florian Fainelli32a64162015-05-26 12:19:59 -07001279 phy_interface_is_rgmii(phydev) ||
Florian Fainellia9f63092014-08-22 18:55:44 -07001280 phy_is_internal(phydev))) {
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001281 int eee_lp, eee_cap, eee_adv;
1282 u32 lp, cap, adv;
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -07001283 int status;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001284
1285 /* Read phy status to properly get the right settings */
1286 status = phy_read_status(phydev);
1287 if (status)
1288 return status;
1289
1290 /* First check if the EEE ability is supported */
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001291 eee_cap = phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_ABLE,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001292 MDIO_MMD_PCS);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001293 if (eee_cap <= 0)
1294 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001295
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001296 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001297 if (!cap)
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001298 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001299
1300 /* Check which link settings negotiated and verify it in
1301 * the EEE advertising registers.
1302 */
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001303 eee_lp = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_LPABLE,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001304 MDIO_MMD_AN);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001305 if (eee_lp <= 0)
1306 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001307
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001308 eee_adv = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_ADV,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001309 MDIO_MMD_AN);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001310 if (eee_adv <= 0)
1311 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001312
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001313 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1314 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
Guenter Roeck54da5a82015-02-17 09:36:22 -08001315 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001316 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001317
1318 if (clk_stop_enable) {
1319 /* Configure the PHY to stop receiving xMII
1320 * clock while it is signaling LPI.
1321 */
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001322 int val = phy_read_mmd_indirect(phydev, MDIO_CTRL1,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001323 MDIO_MMD_PCS);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001324 if (val < 0)
1325 return val;
1326
1327 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001328 phy_write_mmd_indirect(phydev, MDIO_CTRL1,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001329 MDIO_MMD_PCS, val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001330 }
1331
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001332 return 0; /* EEE supported */
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001333 }
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001334eee_exit_err:
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001335 return -EPROTONOSUPPORT;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001336}
1337EXPORT_SYMBOL(phy_init_eee);
1338
1339/**
1340 * phy_get_eee_err - report the EEE wake error count
1341 * @phydev: target phy_device struct
1342 *
1343 * Description: it is to report the number of time where the PHY
1344 * failed to complete its normal wake sequence.
1345 */
1346int phy_get_eee_err(struct phy_device *phydev)
1347{
Andrew Lunn053e7e12016-01-06 20:11:12 +01001348 return phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_WK_ERR, MDIO_MMD_PCS);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001349}
1350EXPORT_SYMBOL(phy_get_eee_err);
1351
1352/**
1353 * phy_ethtool_get_eee - get EEE supported and status
1354 * @phydev: target phy_device struct
1355 * @data: ethtool_eee data
1356 *
1357 * Description: it reportes the Supported/Advertisement/LP Advertisement
1358 * capabilities.
1359 */
1360int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1361{
1362 int val;
1363
1364 /* Get Supported EEE */
Andrew Lunn053e7e12016-01-06 20:11:12 +01001365 val = phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_ABLE, MDIO_MMD_PCS);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001366 if (val < 0)
1367 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001368 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001369
1370 /* Get advertisement EEE */
Andrew Lunn053e7e12016-01-06 20:11:12 +01001371 val = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_ADV, MDIO_MMD_AN);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001372 if (val < 0)
1373 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001374 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001375
1376 /* Get LP advertisement EEE */
Andrew Lunn053e7e12016-01-06 20:11:12 +01001377 val = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_LPABLE, MDIO_MMD_AN);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001378 if (val < 0)
1379 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001380 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001381
1382 return 0;
1383}
1384EXPORT_SYMBOL(phy_ethtool_get_eee);
1385
1386/**
1387 * phy_ethtool_set_eee - set EEE supported and status
1388 * @phydev: target phy_device struct
1389 * @data: ethtool_eee data
1390 *
1391 * Description: it is to program the Advertisement EEE register.
1392 */
1393int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1394{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001395 int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001396
Andrew Lunn053e7e12016-01-06 20:11:12 +01001397 phy_write_mmd_indirect(phydev, MDIO_AN_EEE_ADV, MDIO_MMD_AN, val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001398
1399 return 0;
1400}
1401EXPORT_SYMBOL(phy_ethtool_set_eee);
Michael Stapelberg42e836e2013-03-11 13:56:44 +00001402
1403int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1404{
1405 if (phydev->drv->set_wol)
1406 return phydev->drv->set_wol(phydev, wol);
1407
1408 return -EOPNOTSUPP;
1409}
1410EXPORT_SYMBOL(phy_ethtool_set_wol);
1411
1412void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1413{
1414 if (phydev->drv->get_wol)
1415 phydev->drv->get_wol(phydev, wol);
1416}
1417EXPORT_SYMBOL(phy_ethtool_get_wol);
Philippe Reynes9d9a77c2016-05-10 00:19:41 +02001418
1419int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1420 struct ethtool_link_ksettings *cmd)
1421{
1422 struct phy_device *phydev = ndev->phydev;
1423
1424 if (!phydev)
1425 return -ENODEV;
1426
1427 return phy_ethtool_ksettings_get(phydev, cmd);
1428}
1429EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1430
1431int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1432 const struct ethtool_link_ksettings *cmd)
1433{
1434 struct phy_device *phydev = ndev->phydev;
1435
1436 if (!phydev)
1437 return -ENODEV;
1438
1439 return phy_ethtool_ksettings_set(phydev, cmd);
1440}
1441EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);