blob: 5fde8e335f13980a647695ffb574ea92500682bb [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
Russell Kinge9399672017-06-05 12:22:55 +0100151 /* Avoid genphy_aneg_done() if the Clause 45 PHY does not
152 * implement Clause 22 registers
153 */
154 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
155 return -EINVAL;
156
Florian Fainellia9fa6e62014-02-11 17:27:36 -0800157 return genphy_aneg_done(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400158}
159
Andy Fleming00db8182005-07-30 19:31:23 -0400160/* A structure for mapping a particular speed and duplex
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300161 * combination to a particular SUPPORTED and ADVERTISED value
162 */
Andy Fleming00db8182005-07-30 19:31:23 -0400163struct phy_setting {
164 int speed;
165 int duplex;
166 u32 setting;
167};
168
169/* A mapping of all SUPPORTED settings to speed/duplex */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500170static const struct phy_setting settings[] = {
Andy Fleming00db8182005-07-30 19:31:23 -0400171 {
Lendacky, Thomas3e707702014-07-14 14:05:46 -0500172 .speed = SPEED_10000,
173 .duplex = DUPLEX_FULL,
174 .setting = SUPPORTED_10000baseKR_Full,
175 },
176 {
177 .speed = SPEED_10000,
178 .duplex = DUPLEX_FULL,
179 .setting = SUPPORTED_10000baseKX4_Full,
180 },
181 {
182 .speed = SPEED_10000,
Andy Fleming00db8182005-07-30 19:31:23 -0400183 .duplex = DUPLEX_FULL,
184 .setting = SUPPORTED_10000baseT_Full,
185 },
186 {
Lendacky, Thomas3e707702014-07-14 14:05:46 -0500187 .speed = SPEED_2500,
188 .duplex = DUPLEX_FULL,
189 .setting = SUPPORTED_2500baseX_Full,
190 },
191 {
192 .speed = SPEED_1000,
193 .duplex = DUPLEX_FULL,
194 .setting = SUPPORTED_1000baseKX_Full,
195 },
196 {
Andy Fleming00db8182005-07-30 19:31:23 -0400197 .speed = SPEED_1000,
198 .duplex = DUPLEX_FULL,
199 .setting = SUPPORTED_1000baseT_Full,
200 },
201 {
202 .speed = SPEED_1000,
203 .duplex = DUPLEX_HALF,
204 .setting = SUPPORTED_1000baseT_Half,
205 },
206 {
207 .speed = SPEED_100,
208 .duplex = DUPLEX_FULL,
209 .setting = SUPPORTED_100baseT_Full,
210 },
211 {
212 .speed = SPEED_100,
213 .duplex = DUPLEX_HALF,
214 .setting = SUPPORTED_100baseT_Half,
215 },
216 {
217 .speed = SPEED_10,
218 .duplex = DUPLEX_FULL,
219 .setting = SUPPORTED_10baseT_Full,
220 },
221 {
222 .speed = SPEED_10,
223 .duplex = DUPLEX_HALF,
224 .setting = SUPPORTED_10baseT_Half,
225 },
226};
227
Denis Chengff8ac602007-09-02 18:30:18 +0800228#define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
Andy Fleming00db8182005-07-30 19:31:23 -0400229
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800230/**
231 * phy_find_setting - find a PHY settings array entry that matches speed & duplex
232 * @speed: speed to match
233 * @duplex: duplex to match
Andy Fleming00db8182005-07-30 19:31:23 -0400234 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800235 * Description: Searches the settings array for the setting which
Andy Fleming00db8182005-07-30 19:31:23 -0400236 * matches the desired speed and duplex, and returns the index
237 * of that setting. Returns the index of the last setting if
238 * none of the others match.
239 */
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -0700240static inline unsigned int phy_find_setting(int speed, int duplex)
Andy Fleming00db8182005-07-30 19:31:23 -0400241{
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -0700242 unsigned int idx = 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400243
244 while (idx < ARRAY_SIZE(settings) &&
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300245 (settings[idx].speed != speed || settings[idx].duplex != duplex))
Andy Fleming00db8182005-07-30 19:31:23 -0400246 idx++;
247
248 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
249}
250
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800251/**
252 * phy_find_valid - find a PHY setting that matches the requested features mask
253 * @idx: The first index in settings[] to search
254 * @features: A mask of the valid settings
Andy Fleming00db8182005-07-30 19:31:23 -0400255 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800256 * Description: Returns the index of the first valid setting less
Andy Fleming00db8182005-07-30 19:31:23 -0400257 * than or equal to the one pointed to by idx, as determined by
258 * the mask in features. Returns the index of the last setting
259 * if nothing else matches.
260 */
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -0700261static inline unsigned int phy_find_valid(unsigned int idx, u32 features)
Andy Fleming00db8182005-07-30 19:31:23 -0400262{
263 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
264 idx++;
265
266 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
267}
268
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800269/**
Guenter Roeck54da5a82015-02-17 09:36:22 -0800270 * phy_check_valid - check if there is a valid PHY setting which matches
271 * speed, duplex, and feature mask
272 * @speed: speed to match
273 * @duplex: duplex to match
274 * @features: A mask of the valid settings
275 *
276 * Description: Returns true if there is a valid setting, false otherwise.
277 */
278static inline bool phy_check_valid(int speed, int duplex, u32 features)
279{
280 unsigned int idx;
281
282 idx = phy_find_valid(phy_find_setting(speed, duplex), features);
283
284 return settings[idx].speed == speed && settings[idx].duplex == duplex &&
285 (settings[idx].setting & features);
286}
287
288/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800289 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
290 * @phydev: the target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400291 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800292 * Description: Make sure the PHY is set to supported speeds and
Andy Fleming00db8182005-07-30 19:31:23 -0400293 * duplexes. Drop down by one in this order: 1000/FULL,
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800294 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
Andy Fleming00db8182005-07-30 19:31:23 -0400295 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000296static void phy_sanitize_settings(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400297{
298 u32 features = phydev->supported;
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -0700299 unsigned int idx;
Andy Fleming00db8182005-07-30 19:31:23 -0400300
301 /* Sanitize settings based on PHY capabilities */
302 if ((features & SUPPORTED_Autoneg) == 0)
Domen Puncer163642a2007-08-07 12:12:41 +0200303 phydev->autoneg = AUTONEG_DISABLE;
Andy Fleming00db8182005-07-30 19:31:23 -0400304
305 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
306 features);
307
308 phydev->speed = settings[idx].speed;
309 phydev->duplex = settings[idx].duplex;
310}
Andy Fleming00db8182005-07-30 19:31:23 -0400311
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800312/**
313 * phy_ethtool_sset - generic ethtool sset function, handles all the details
314 * @phydev: target phy_device struct
315 * @cmd: ethtool_cmd
Andy Fleming00db8182005-07-30 19:31:23 -0400316 *
317 * A few notes about parameter checking:
318 * - We don't set port or transceiver, so we don't care what they
319 * were set to.
320 * - phy_start_aneg() will make sure forced settings are sane, and
321 * choose the next best ones from the ones selected, so we don't
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800322 * care if ethtool tries to give us bad values.
Andy Fleming00db8182005-07-30 19:31:23 -0400323 */
324int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
325{
David Decotigny25db0332011-04-27 18:32:39 +0000326 u32 speed = ethtool_cmd_speed(cmd);
327
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100328 if (cmd->phy_address != phydev->mdio.addr)
Andy Fleming00db8182005-07-30 19:31:23 -0400329 return -EINVAL;
330
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300331 /* We make sure that we don't pass unsupported values in to the PHY */
Andy Fleming00db8182005-07-30 19:31:23 -0400332 cmd->advertising &= phydev->supported;
333
334 /* Verify the settings we care about. */
335 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
336 return -EINVAL;
337
338 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
339 return -EINVAL;
340
Joe Perches8e95a202009-12-03 07:58:21 +0000341 if (cmd->autoneg == AUTONEG_DISABLE &&
David Decotigny25db0332011-04-27 18:32:39 +0000342 ((speed != SPEED_1000 &&
343 speed != SPEED_100 &&
344 speed != SPEED_10) ||
Joe Perches8e95a202009-12-03 07:58:21 +0000345 (cmd->duplex != DUPLEX_HALF &&
346 cmd->duplex != DUPLEX_FULL)))
Andy Fleming00db8182005-07-30 19:31:23 -0400347 return -EINVAL;
348
349 phydev->autoneg = cmd->autoneg;
350
David Decotigny25db0332011-04-27 18:32:39 +0000351 phydev->speed = speed;
Andy Fleming00db8182005-07-30 19:31:23 -0400352
353 phydev->advertising = cmd->advertising;
354
355 if (AUTONEG_ENABLE == cmd->autoneg)
356 phydev->advertising |= ADVERTISED_Autoneg;
357 else
358 phydev->advertising &= ~ADVERTISED_Autoneg;
359
360 phydev->duplex = cmd->duplex;
361
David Thomson634ec362015-07-10 13:56:54 +1200362 phydev->mdix = cmd->eth_tp_mdix_ctrl;
363
Andy Fleming00db8182005-07-30 19:31:23 -0400364 /* Restart the PHY */
365 phy_start_aneg(phydev);
366
367 return 0;
368}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600369EXPORT_SYMBOL(phy_ethtool_sset);
Andy Fleming00db8182005-07-30 19:31:23 -0400370
Philippe Reynes2d551732016-04-15 00:35:00 +0200371int phy_ethtool_ksettings_set(struct phy_device *phydev,
372 const struct ethtool_link_ksettings *cmd)
373{
374 u8 autoneg = cmd->base.autoneg;
375 u8 duplex = cmd->base.duplex;
376 u32 speed = cmd->base.speed;
377 u32 advertising;
378
379 if (cmd->base.phy_address != phydev->mdio.addr)
380 return -EINVAL;
381
382 ethtool_convert_link_mode_to_legacy_u32(&advertising,
383 cmd->link_modes.advertising);
384
385 /* We make sure that we don't pass unsupported values in to the PHY */
386 advertising &= phydev->supported;
387
388 /* Verify the settings we care about. */
389 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
390 return -EINVAL;
391
392 if (autoneg == AUTONEG_ENABLE && advertising == 0)
393 return -EINVAL;
394
395 if (autoneg == AUTONEG_DISABLE &&
396 ((speed != SPEED_1000 &&
397 speed != SPEED_100 &&
398 speed != SPEED_10) ||
399 (duplex != DUPLEX_HALF &&
400 duplex != DUPLEX_FULL)))
401 return -EINVAL;
402
403 phydev->autoneg = autoneg;
404
405 phydev->speed = speed;
406
407 phydev->advertising = advertising;
408
409 if (autoneg == AUTONEG_ENABLE)
410 phydev->advertising |= ADVERTISED_Autoneg;
411 else
412 phydev->advertising &= ~ADVERTISED_Autoneg;
413
414 phydev->duplex = duplex;
415
416 phydev->mdix = cmd->base.eth_tp_mdix_ctrl;
417
418 /* Restart the PHY */
419 phy_start_aneg(phydev);
420
421 return 0;
422}
423EXPORT_SYMBOL(phy_ethtool_ksettings_set);
424
Andy Fleming00db8182005-07-30 19:31:23 -0400425int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
426{
427 cmd->supported = phydev->supported;
428
429 cmd->advertising = phydev->advertising;
Florian Fainelli114002b2013-12-06 13:01:30 -0800430 cmd->lp_advertising = phydev->lp_advertising;
Andy Fleming00db8182005-07-30 19:31:23 -0400431
David Decotigny70739492011-04-27 18:32:40 +0000432 ethtool_cmd_speed_set(cmd, phydev->speed);
Andy Fleming00db8182005-07-30 19:31:23 -0400433 cmd->duplex = phydev->duplex;
Florian Fainellic88838c2014-02-13 16:08:43 -0800434 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
435 cmd->port = PORT_BNC;
436 else
437 cmd->port = PORT_MII;
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100438 cmd->phy_address = phydev->mdio.addr;
Florian Fainelli4284b6a2013-05-23 01:11:12 +0000439 cmd->transceiver = phy_is_internal(phydev) ?
440 XCVR_INTERNAL : XCVR_EXTERNAL;
Andy Fleming00db8182005-07-30 19:31:23 -0400441 cmd->autoneg = phydev->autoneg;
David Thomson239aa552015-07-10 16:28:25 +1200442 cmd->eth_tp_mdix_ctrl = phydev->mdix;
Andy Fleming00db8182005-07-30 19:31:23 -0400443
444 return 0;
445}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600446EXPORT_SYMBOL(phy_ethtool_gset);
Andy Fleming00db8182005-07-30 19:31:23 -0400447
Philippe Reynes2d551732016-04-15 00:35:00 +0200448int phy_ethtool_ksettings_get(struct phy_device *phydev,
449 struct ethtool_link_ksettings *cmd)
450{
451 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
452 phydev->supported);
453
454 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
455 phydev->advertising);
456
457 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
458 phydev->lp_advertising);
459
460 cmd->base.speed = phydev->speed;
461 cmd->base.duplex = phydev->duplex;
462 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
463 cmd->base.port = PORT_BNC;
464 else
465 cmd->base.port = PORT_MII;
466
467 cmd->base.phy_address = phydev->mdio.addr;
468 cmd->base.autoneg = phydev->autoneg;
469 cmd->base.eth_tp_mdix_ctrl = phydev->mdix;
470
471 return 0;
472}
473EXPORT_SYMBOL(phy_ethtool_ksettings_get);
474
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800475/**
476 * phy_mii_ioctl - generic PHY MII ioctl interface
477 * @phydev: the phy_device struct
Randy Dunlap00c7d922010-08-09 13:41:59 +0000478 * @ifr: &struct ifreq for socket ioctl's
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800479 * @cmd: ioctl cmd to execute
480 *
481 * Note that this function is currently incompatible with the
Andy Fleming00db8182005-07-30 19:31:23 -0400482 * PHYCONTROL layer. It changes registers without regard to
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800483 * current state. Use at own risk.
Andy Fleming00db8182005-07-30 19:31:23 -0400484 */
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300485int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
Andy Fleming00db8182005-07-30 19:31:23 -0400486{
Richard Cochran28b04112010-07-17 08:48:55 +0000487 struct mii_ioctl_data *mii_data = if_mii(ifr);
Andy Fleming00db8182005-07-30 19:31:23 -0400488 u16 val = mii_data->val_in;
Brian Hill79ce0472014-11-11 13:39:39 -0700489 bool change_autoneg = false;
Andy Fleming00db8182005-07-30 19:31:23 -0400490
491 switch (cmd) {
492 case SIOCGMIIPHY:
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100493 mii_data->phy_id = phydev->mdio.addr;
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000494 /* fall through */
495
Andy Fleming00db8182005-07-30 19:31:23 -0400496 case SIOCGMIIREG:
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100497 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
498 mii_data->phy_id,
Peter Korsgaardaf1dc132011-03-10 06:52:13 +0000499 mii_data->reg_num);
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300500 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400501
502 case SIOCSMIIREG:
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100503 if (mii_data->phy_id == phydev->mdio.addr) {
Florian Fainellie1093742013-12-17 21:38:12 -0800504 switch (mii_data->reg_num) {
Andy Fleming00db8182005-07-30 19:31:23 -0400505 case MII_BMCR:
Brian Hill79ce0472014-11-11 13:39:39 -0700506 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
507 if (phydev->autoneg == AUTONEG_ENABLE)
508 change_autoneg = true;
Andy Fleming00db8182005-07-30 19:31:23 -0400509 phydev->autoneg = AUTONEG_DISABLE;
Brian Hill79ce0472014-11-11 13:39:39 -0700510 if (val & BMCR_FULLDPLX)
511 phydev->duplex = DUPLEX_FULL;
512 else
513 phydev->duplex = DUPLEX_HALF;
514 if (val & BMCR_SPEED1000)
515 phydev->speed = SPEED_1000;
516 else if (val & BMCR_SPEED100)
517 phydev->speed = SPEED_100;
518 else phydev->speed = SPEED_10;
519 }
520 else {
521 if (phydev->autoneg == AUTONEG_DISABLE)
522 change_autoneg = true;
Andy Fleming00db8182005-07-30 19:31:23 -0400523 phydev->autoneg = AUTONEG_ENABLE;
Brian Hill79ce0472014-11-11 13:39:39 -0700524 }
Andy Fleming00db8182005-07-30 19:31:23 -0400525 break;
526 case MII_ADVERTISE:
Brian Hill79ce0472014-11-11 13:39:39 -0700527 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
528 change_autoneg = true;
Andy Fleming00db8182005-07-30 19:31:23 -0400529 break;
530 default:
531 /* do nothing */
532 break;
533 }
534 }
535
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100536 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
Peter Korsgaardaf1dc132011-03-10 06:52:13 +0000537 mii_data->reg_num, val);
538
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100539 if (mii_data->phy_id == phydev->mdio.addr &&
Jérôme Pouillercf18b772015-12-03 10:02:35 +0100540 mii_data->reg_num == MII_BMCR &&
Florian Fainelli2613f952013-12-06 13:01:31 -0800541 val & BMCR_RESET)
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300542 return phy_init_hw(phydev);
Brian Hill79ce0472014-11-11 13:39:39 -0700543
544 if (change_autoneg)
545 return phy_start_aneg(phydev);
546
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300547 return 0;
David Woodhousedda93b42007-11-28 19:56:34 +0000548
Richard Cochranc1f19b52010-07-17 08:49:36 +0000549 case SIOCSHWTSTAMP:
550 if (phydev->drv->hwtstamp)
551 return phydev->drv->hwtstamp(phydev, ifr);
552 /* fall through */
553
David Woodhousedda93b42007-11-28 19:56:34 +0000554 default:
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000555 return -EOPNOTSUPP;
Andy Fleming00db8182005-07-30 19:31:23 -0400556 }
Andy Fleming00db8182005-07-30 19:31:23 -0400557}
Domen Puncer680e9fe2007-09-17 22:21:40 +0200558EXPORT_SYMBOL(phy_mii_ioctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400559
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800560/**
Alexander Kochetkovae6a7622017-04-20 14:00:04 +0300561 * phy_start_aneg_priv - start auto-negotiation for this PHY device
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800562 * @phydev: the phy_device struct
Alexander Kochetkovae6a7622017-04-20 14:00:04 +0300563 * @sync: indicate whether we should wait for the workqueue cancelation
Andy Fleminge1393452005-08-24 18:46:21 -0500564 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800565 * Description: Sanitizes the settings (if we're not autonegotiating
566 * them), and then calls the driver's config_aneg function.
567 * If the PHYCONTROL Layer is operating, we change the state to
568 * reflect the beginning of Auto-negotiation or forcing.
Andy Fleminge1393452005-08-24 18:46:21 -0500569 */
Alexander Kochetkovae6a7622017-04-20 14:00:04 +0300570static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
Andy Fleminge1393452005-08-24 18:46:21 -0500571{
Alexander Kochetkovae6a7622017-04-20 14:00:04 +0300572 bool trigger = 0;
Andy Fleminge1393452005-08-24 18:46:21 -0500573 int err;
574
Nate Case35b5f6b2008-01-29 10:05:09 -0600575 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500576
577 if (AUTONEG_DISABLE == phydev->autoneg)
578 phy_sanitize_settings(phydev);
579
Ben Hutchings9b3320e2015-01-27 00:58:15 +0000580 /* Invalidate LP advertising flags */
581 phydev->lp_advertising = 0;
582
Andy Fleminge1393452005-08-24 18:46:21 -0500583 err = phydev->drv->config_aneg(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500584 if (err < 0)
585 goto out_unlock;
586
587 if (phydev->state != PHY_HALTED) {
588 if (AUTONEG_ENABLE == phydev->autoneg) {
589 phydev->state = PHY_AN;
590 phydev->link_timeout = PHY_AN_TIMEOUT;
591 } else {
592 phydev->state = PHY_FORCING;
593 phydev->link_timeout = PHY_FORCE_TIMEOUT;
594 }
595 }
596
Alexander Kochetkovae6a7622017-04-20 14:00:04 +0300597 /* Re-schedule a PHY state machine to check PHY status because
598 * negotiation may already be done and aneg interrupt may not be
599 * generated.
600 */
Heiner Kallweitc6ac36b2018-07-19 08:15:16 +0200601 if (phydev->irq != PHY_POLL && phydev->state == PHY_AN) {
Alexander Kochetkovae6a7622017-04-20 14:00:04 +0300602 err = phy_aneg_done(phydev);
603 if (err > 0) {
604 trigger = true;
605 err = 0;
606 }
607 }
608
Andy Fleminge1393452005-08-24 18:46:21 -0500609out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600610 mutex_unlock(&phydev->lock);
Alexander Kochetkovae6a7622017-04-20 14:00:04 +0300611
612 if (trigger)
613 phy_trigger_machine(phydev, sync);
614
Andy Fleminge1393452005-08-24 18:46:21 -0500615 return err;
616}
Alexander Kochetkovae6a7622017-04-20 14:00:04 +0300617
618/**
619 * phy_start_aneg - start auto-negotiation for this PHY device
620 * @phydev: the phy_device struct
621 *
622 * Description: Sanitizes the settings (if we're not autonegotiating
623 * them), and then calls the driver's config_aneg function.
624 * If the PHYCONTROL Layer is operating, we change the state to
625 * reflect the beginning of Auto-negotiation or forcing.
626 */
627int phy_start_aneg(struct phy_device *phydev)
628{
629 return phy_start_aneg_priv(phydev, true);
630}
Andy Fleminge1393452005-08-24 18:46:21 -0500631EXPORT_SYMBOL(phy_start_aneg);
632
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800633/**
634 * phy_start_machine - start PHY state machine tracking
635 * @phydev: the phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400636 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800637 * Description: The PHY infrastructure can run a state machine
Andy Fleming00db8182005-07-30 19:31:23 -0400638 * which tracks whether the PHY is starting up, negotiating,
639 * etc. This function starts the timer which tracks the state
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300640 * of the PHY. If you want to maintain your own state machine,
641 * do not call this function.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800642 */
Sergei Shtylyov29935ae2014-01-05 03:27:17 +0300643void phy_start_machine(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400644{
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530645 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
Andy Fleming00db8182005-07-30 19:31:23 -0400646}
647
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800648/**
Andrew Lunn3c293f42016-10-12 22:14:53 +0200649 * phy_trigger_machine - trigger the state machine to run
650 *
651 * @phydev: the phy_device struct
Florian Fainellifeaa5ba2017-01-20 15:31:52 -0800652 * @sync: indicate whether we should wait for the workqueue cancelation
Andrew Lunn3c293f42016-10-12 22:14:53 +0200653 *
654 * Description: There has been a change in state which requires that the
655 * state machine runs.
656 */
657
Alexander Kochetkovae6a7622017-04-20 14:00:04 +0300658void phy_trigger_machine(struct phy_device *phydev, bool sync)
Andrew Lunn3c293f42016-10-12 22:14:53 +0200659{
Florian Fainellifeaa5ba2017-01-20 15:31:52 -0800660 if (sync)
661 cancel_delayed_work_sync(&phydev->state_queue);
662 else
663 cancel_delayed_work(&phydev->state_queue);
Andrew Lunn3c293f42016-10-12 22:14:53 +0200664 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
665}
666
667/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800668 * phy_stop_machine - stop the PHY state machine tracking
669 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400670 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800671 * Description: Stops the state machine timer, sets the state to UP
Sergei Shtylylov817acf52006-07-26 00:53:53 +0400672 * (unless it wasn't up yet). This function must be called BEFORE
Andy Fleming00db8182005-07-30 19:31:23 -0400673 * phy_detach.
674 */
675void phy_stop_machine(struct phy_device *phydev)
676{
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700677 cancel_delayed_work_sync(&phydev->state_queue);
Andy Fleming00db8182005-07-30 19:31:23 -0400678
Nate Case35b5f6b2008-01-29 10:05:09 -0600679 mutex_lock(&phydev->lock);
Nathan Sullivanc63d6182017-03-22 15:27:01 -0500680 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
Andy Fleming00db8182005-07-30 19:31:23 -0400681 phydev->state = PHY_UP;
Nate Case35b5f6b2008-01-29 10:05:09 -0600682 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400683}
684
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800685/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800686 * phy_error - enter HALTED state for this PHY device
687 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400688 *
689 * Moves the PHY to the HALTED state in response to a read
690 * or write error, and tells the controller the link is down.
691 * Must not be called from interrupt context, or while the
692 * phydev->lock is held.
693 */
Andy Fleming9b9a8bf2008-05-02 13:00:51 -0500694static void phy_error(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400695{
Nate Case35b5f6b2008-01-29 10:05:09 -0600696 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400697 phydev->state = PHY_HALTED;
Nate Case35b5f6b2008-01-29 10:05:09 -0600698 mutex_unlock(&phydev->lock);
Andrew Lunn3c293f42016-10-12 22:14:53 +0200699
Florian Fainellifeaa5ba2017-01-20 15:31:52 -0800700 phy_trigger_machine(phydev, false);
Andy Fleming00db8182005-07-30 19:31:23 -0400701}
702
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800703/**
704 * phy_interrupt - PHY interrupt handler
705 * @irq: interrupt line
706 * @phy_dat: phy_device pointer
Andy Fleminge1393452005-08-24 18:46:21 -0500707 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800708 * Description: When a PHY interrupt occurs, the handler disables
Andy Fleminge1393452005-08-24 18:46:21 -0500709 * interrupts, and schedules a work task to clear the interrupt.
710 */
David Howells7d12e782006-10-05 14:55:46 +0100711static irqreturn_t phy_interrupt(int irq, void *phy_dat)
Andy Fleminge1393452005-08-24 18:46:21 -0500712{
713 struct phy_device *phydev = phy_dat;
714
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100715 if (PHY_HALTED == phydev->state)
716 return IRQ_NONE; /* It can't be ours. */
717
Andy Fleminge1393452005-08-24 18:46:21 -0500718 /* The MDIO bus is not allowed to be written in interrupt
719 * context, so we need to disable the irq here. A work
720 * queue will write the PHY to disable and clear the
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300721 * interrupt, and then reenable the irq line.
722 */
Andy Fleminge1393452005-08-24 18:46:21 -0500723 disable_irq_nosync(irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700724 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500725
Viresh Kumarbbb47bd2013-04-24 17:12:55 +0530726 queue_work(system_power_efficient_wq, &phydev->phy_queue);
Andy Fleminge1393452005-08-24 18:46:21 -0500727
728 return IRQ_HANDLED;
729}
730
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800731/**
732 * phy_enable_interrupts - Enable the interrupts from the PHY side
733 * @phydev: target phy_device struct
734 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000735static int phy_enable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400736{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300737 int err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400738
Andy Fleminge1393452005-08-24 18:46:21 -0500739 if (err < 0)
740 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400741
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300742 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400743}
Andy Fleming00db8182005-07-30 19:31:23 -0400744
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800745/**
746 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
747 * @phydev: target phy_device struct
748 */
stephen hemminger89ff05e2010-10-21 08:37:41 +0000749static int phy_disable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400750{
751 int err;
752
753 /* Disable PHY interrupts */
754 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400755 if (err)
756 goto phy_err;
757
758 /* Clear the interrupt */
759 err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400760 if (err)
761 goto phy_err;
762
763 return 0;
764
765phy_err:
766 phy_error(phydev);
767
768 return err;
769}
Andy Fleminge1393452005-08-24 18:46:21 -0500770
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800771/**
772 * phy_start_interrupts - request and enable interrupts for a PHY device
773 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500774 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800775 * Description: Request the interrupt for the given PHY.
776 * If this fails, then we set irq to PHY_POLL.
Andy Fleminge1393452005-08-24 18:46:21 -0500777 * Otherwise, we enable the interrupts in the PHY.
Andy Fleminge1393452005-08-24 18:46:21 -0500778 * This should only be called with a valid IRQ number.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800779 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500780 */
781int phy_start_interrupts(struct phy_device *phydev)
782{
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700783 atomic_set(&phydev->irq_disable, 0);
Xander Huffc3e70ed2016-08-24 16:47:53 -0500784 if (request_irq(phydev->irq, phy_interrupt,
785 IRQF_SHARED,
786 "phy_interrupt",
787 phydev) < 0) {
Joe Perches8d242482012-06-09 07:49:07 +0000788 pr_warn("%s: Can't get IRQ %d (PHY)\n",
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100789 phydev->mdio.bus->name, phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500790 phydev->irq = PHY_POLL;
791 return 0;
792 }
793
Sergei Shtylyove62a7682014-01-05 03:21:52 +0300794 return phy_enable_interrupts(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500795}
796EXPORT_SYMBOL(phy_start_interrupts);
797
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800798/**
799 * phy_stop_interrupts - disable interrupts from a PHY device
800 * @phydev: target phy_device struct
801 */
Andy Fleminge1393452005-08-24 18:46:21 -0500802int phy_stop_interrupts(struct phy_device *phydev)
803{
Sergei Shtylyov553fe922014-01-05 03:23:19 +0300804 int err = phy_disable_interrupts(phydev);
Andy Fleminge1393452005-08-24 18:46:21 -0500805
806 if (err)
807 phy_error(phydev);
808
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700809 free_irq(phydev->irq, phydev);
810
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300811 /* Cannot call flush_scheduled_work() here as desired because
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700812 * of rtnl_lock(), but we do not really care about what would
813 * be done, except from enable_irq(), so cancel any work
814 * possibly pending and take care of the matter below.
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100815 */
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700816 cancel_work_sync(&phydev->phy_queue);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300817 /* If work indeed has been cancelled, disable_irq() will have
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700818 * been left unbalanced from phy_interrupt() and enable_irq()
819 * has to be called so that other devices on the line work.
820 */
821 while (atomic_dec_return(&phydev->irq_disable) >= 0)
822 enable_irq(phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500823
824 return err;
825}
826EXPORT_SYMBOL(phy_stop_interrupts);
827
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800828/**
829 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
830 * @work: work_struct that describes the work to be done
831 */
Florian Fainelli5ea94e72013-05-19 22:53:43 +0000832void phy_change(struct work_struct *work)
Andy Fleminge1393452005-08-24 18:46:21 -0500833{
David Howellsc4028952006-11-22 14:57:56 +0000834 struct phy_device *phydev =
835 container_of(work, struct phy_device, phy_queue);
Andy Fleminge1393452005-08-24 18:46:21 -0500836
Florian Fainellideccd16f92016-01-18 19:33:07 -0800837 if (phy_interrupt_is_valid(phydev)) {
838 if (phydev->drv->did_interrupt &&
839 !phydev->drv->did_interrupt(phydev))
840 goto ignore;
Anatolij Gustschina8729eb2009-04-07 02:01:42 +0000841
Florian Fainellideccd16f92016-01-18 19:33:07 -0800842 if (phy_disable_interrupts(phydev))
843 goto phy_err;
844 }
Andy Fleminge1393452005-08-24 18:46:21 -0500845
Nate Case35b5f6b2008-01-29 10:05:09 -0600846 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500847 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
848 phydev->state = PHY_CHANGELINK;
Nate Case35b5f6b2008-01-29 10:05:09 -0600849 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500850
Florian Fainellideccd16f92016-01-18 19:33:07 -0800851 if (phy_interrupt_is_valid(phydev)) {
852 atomic_dec(&phydev->irq_disable);
853 enable_irq(phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500854
Florian Fainellideccd16f92016-01-18 19:33:07 -0800855 /* Reenable interrupts */
856 if (PHY_HALTED != phydev->state &&
857 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
858 goto irq_enable_err;
859 }
Andy Fleminge1393452005-08-24 18:46:21 -0500860
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700861 /* reschedule state queue work to run as soon as possible */
Florian Fainellifeaa5ba2017-01-20 15:31:52 -0800862 phy_trigger_machine(phydev, true);
Andy Fleminge1393452005-08-24 18:46:21 -0500863 return;
864
Anatolij Gustschina8729eb2009-04-07 02:01:42 +0000865ignore:
866 atomic_dec(&phydev->irq_disable);
867 enable_irq(phydev->irq);
868 return;
869
Andy Fleminge1393452005-08-24 18:46:21 -0500870irq_enable_err:
871 disable_irq(phydev->irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700872 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500873phy_err:
874 phy_error(phydev);
875}
876
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800877/**
878 * phy_stop - Bring down the PHY link, and stop checking the status
879 * @phydev: target phy_device struct
880 */
Andy Fleminge1393452005-08-24 18:46:21 -0500881void phy_stop(struct phy_device *phydev)
882{
Nate Case35b5f6b2008-01-29 10:05:09 -0600883 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500884
885 if (PHY_HALTED == phydev->state)
886 goto out_unlock;
887
Florian Fainelli2c7b4922013-05-19 22:53:42 +0000888 if (phy_interrupt_is_valid(phydev)) {
Andy Fleminge1393452005-08-24 18:46:21 -0500889 /* Disable PHY Interrupts */
890 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleminge1393452005-08-24 18:46:21 -0500891
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100892 /* Clear any pending interrupts */
893 phy_clear_interrupt(phydev);
894 }
Andy Fleminge1393452005-08-24 18:46:21 -0500895
Maciej W. Rozycki6daf6532007-09-28 22:42:15 -0700896 phydev->state = PHY_HALTED;
897
Andy Fleminge1393452005-08-24 18:46:21 -0500898out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600899 mutex_unlock(&phydev->lock);
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100900
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300901 /* Cannot call flush_scheduled_work() here as desired because
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100902 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
903 * will not reenable interrupts.
904 */
Andy Fleminge1393452005-08-24 18:46:21 -0500905}
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300906EXPORT_SYMBOL(phy_stop);
Andy Fleminge1393452005-08-24 18:46:21 -0500907
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800908/**
909 * phy_start - start or restart a PHY device
910 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500911 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800912 * Description: Indicates the attached device's readiness to
Andy Fleminge1393452005-08-24 18:46:21 -0500913 * handle PHY-related work. Used during startup to start the
914 * PHY, and after a call to phy_stop() to resume operation.
915 * Also used to indicate the MDIO bus has cleared an error
916 * condition.
917 */
918void phy_start(struct phy_device *phydev)
919{
Tim Bealec15e10e2015-05-18 15:38:38 +1200920 bool do_resume = false;
921 int err = 0;
922
Nate Case35b5f6b2008-01-29 10:05:09 -0600923 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500924
925 switch (phydev->state) {
Florian Fainellie1093742013-12-17 21:38:12 -0800926 case PHY_STARTING:
927 phydev->state = PHY_PENDING;
928 break;
929 case PHY_READY:
930 phydev->state = PHY_UP;
931 break;
932 case PHY_HALTED:
Tim Bealec15e10e2015-05-18 15:38:38 +1200933 /* make sure interrupts are re-enabled for the PHY */
Heiner Kallweit19d32132018-02-08 21:01:48 +0100934 if (phy_interrupt_is_valid(phydev)) {
Shaohui Xie84a527a2016-05-10 17:42:26 +0800935 err = phy_enable_interrupts(phydev);
936 if (err < 0)
937 break;
938 }
Tim Bealec15e10e2015-05-18 15:38:38 +1200939
Florian Fainellie1093742013-12-17 21:38:12 -0800940 phydev->state = PHY_RESUMING;
Tim Bealec15e10e2015-05-18 15:38:38 +1200941 do_resume = true;
942 break;
Florian Fainellie1093742013-12-17 21:38:12 -0800943 default:
944 break;
Andy Fleminge1393452005-08-24 18:46:21 -0500945 }
Nate Case35b5f6b2008-01-29 10:05:09 -0600946 mutex_unlock(&phydev->lock);
Tim Bealec15e10e2015-05-18 15:38:38 +1200947
948 /* if phy was suspended, bring the physical link up again */
949 if (do_resume)
950 phy_resume(phydev);
Andrew Lunn3c293f42016-10-12 22:14:53 +0200951
Florian Fainellifeaa5ba2017-01-20 15:31:52 -0800952 phy_trigger_machine(phydev, true);
Andy Fleminge1393452005-08-24 18:46:21 -0500953}
Andy Fleminge1393452005-08-24 18:46:21 -0500954EXPORT_SYMBOL(phy_start);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400955
Nate Case35b5f6b2008-01-29 10:05:09 -0600956/**
957 * phy_state_machine - Handle the state machine
958 * @work: work_struct that describes the work to be done
Nate Case35b5f6b2008-01-29 10:05:09 -0600959 */
Anton Vorontsov4f9c85a2010-01-18 05:37:16 +0000960void phy_state_machine(struct work_struct *work)
Andy Fleming00db8182005-07-30 19:31:23 -0400961{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700962 struct delayed_work *dwork = to_delayed_work(work);
Nate Case35b5f6b2008-01-29 10:05:09 -0600963 struct phy_device *phydev =
Marcin Slusarza390d1f2009-03-13 15:41:19 -0700964 container_of(dwork, struct phy_device, state_queue);
Tim Bealec15e10e2015-05-18 15:38:38 +1200965 bool needs_aneg = false, do_suspend = false;
Florian Fainelli3e2186e2015-05-16 10:17:56 -0700966 enum phy_state old_state;
Andy Fleming00db8182005-07-30 19:31:23 -0400967 int err = 0;
Shaohui Xie11e122c2015-08-14 12:23:40 +0800968 int old_link;
Andy Fleming00db8182005-07-30 19:31:23 -0400969
Nate Case35b5f6b2008-01-29 10:05:09 -0600970 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400971
Florian Fainelli3e2186e2015-05-16 10:17:56 -0700972 old_state = phydev->state;
973
Daniel Mack2b8f2a22014-06-18 11:01:41 +0200974 if (phydev->drv->link_change_notify)
975 phydev->drv->link_change_notify(phydev);
976
Florian Fainellie1093742013-12-17 21:38:12 -0800977 switch (phydev->state) {
978 case PHY_DOWN:
979 case PHY_STARTING:
980 case PHY_READY:
981 case PHY_PENDING:
982 break;
983 case PHY_UP:
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +0800984 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -0800985
986 phydev->link_timeout = PHY_AN_TIMEOUT;
987
988 break;
989 case PHY_AN:
990 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -0800991 if (err < 0)
Andy Fleming00db8182005-07-30 19:31:23 -0400992 break;
Florian Fainellie1093742013-12-17 21:38:12 -0800993
Sergei Shtylyov2f53e902014-01-05 03:17:06 +0300994 /* If the link is down, give up on negotiation for now */
Florian Fainellie1093742013-12-17 21:38:12 -0800995 if (!phydev->link) {
996 phydev->state = PHY_NOLINK;
997 netif_carrier_off(phydev->attached_dev);
998 phydev->adjust_link(phydev->attached_dev);
999 break;
1000 }
1001
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001002 /* Check if negotiation is done. Break if there's an error */
Florian Fainellie1093742013-12-17 21:38:12 -08001003 err = phy_aneg_done(phydev);
1004 if (err < 0)
1005 break;
1006
1007 /* If AN is done, we're running */
1008 if (err > 0) {
1009 phydev->state = PHY_RUNNING;
1010 netif_carrier_on(phydev->attached_dev);
1011 phydev->adjust_link(phydev->attached_dev);
1012
Balakumaran Kannanfa8cdda2014-04-09 09:03:45 +05301013 } else if (0 == phydev->link_timeout--)
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001014 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001015 break;
1016 case PHY_NOLINK:
Andrew Lunn321beec2015-11-16 23:36:46 +01001017 if (phy_interrupt_is_valid(phydev))
1018 break;
1019
Florian Fainellie1093742013-12-17 21:38:12 -08001020 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001021 if (err)
Andy Fleming00db8182005-07-30 19:31:23 -04001022 break;
Andy Fleming6b655522006-10-16 16:19:17 -05001023
Florian Fainellie1093742013-12-17 21:38:12 -08001024 if (phydev->link) {
Balakumaran Kannane46e08b2014-04-24 08:22:47 +05301025 if (AUTONEG_ENABLE == phydev->autoneg) {
1026 err = phy_aneg_done(phydev);
1027 if (err < 0)
1028 break;
1029
1030 if (!err) {
1031 phydev->state = PHY_AN;
1032 phydev->link_timeout = PHY_AN_TIMEOUT;
1033 break;
1034 }
1035 }
Florian Fainellie1093742013-12-17 21:38:12 -08001036 phydev->state = PHY_RUNNING;
1037 netif_carrier_on(phydev->attached_dev);
1038 phydev->adjust_link(phydev->attached_dev);
1039 }
1040 break;
1041 case PHY_FORCING:
1042 err = genphy_update_link(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001043 if (err)
1044 break;
Andy Fleming6b655522006-10-16 16:19:17 -05001045
Florian Fainellie1093742013-12-17 21:38:12 -08001046 if (phydev->link) {
1047 phydev->state = PHY_RUNNING;
1048 netif_carrier_on(phydev->attached_dev);
1049 } else {
1050 if (0 == phydev->link_timeout--)
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001051 needs_aneg = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001052 }
1053
1054 phydev->adjust_link(phydev->attached_dev);
1055 break;
1056 case PHY_RUNNING:
Florian Fainellid5c3d842016-01-18 19:33:06 -08001057 /* Only register a CHANGE if we are polling and link changed
1058 * since latest checking.
Florian Fainellie1093742013-12-17 21:38:12 -08001059 */
Florian Fainellid5c3d842016-01-18 19:33:06 -08001060 if (phydev->irq == PHY_POLL) {
Shaohui Xie11e122c2015-08-14 12:23:40 +08001061 old_link = phydev->link;
1062 err = phy_read_status(phydev);
1063 if (err)
1064 break;
1065
1066 if (old_link != phydev->link)
1067 phydev->state = PHY_CHANGELINK;
1068 }
Zefir Kurtisi0bbbbae2017-01-06 12:14:48 +01001069 /*
1070 * Failsafe: check that nobody set phydev->link=0 between two
1071 * poll cycles, otherwise we won't leave RUNNING state as long
1072 * as link remains down.
1073 */
1074 if (!phydev->link && phydev->state == PHY_RUNNING) {
1075 phydev->state = PHY_CHANGELINK;
1076 phydev_err(phydev, "no link in PHY_RUNNING\n");
1077 }
Florian Fainellie1093742013-12-17 21:38:12 -08001078 break;
1079 case PHY_CHANGELINK:
1080 err = phy_read_status(phydev);
Florian Fainellie1093742013-12-17 21:38:12 -08001081 if (err)
1082 break;
1083
1084 if (phydev->link) {
1085 phydev->state = PHY_RUNNING;
1086 netif_carrier_on(phydev->attached_dev);
1087 } else {
1088 phydev->state = PHY_NOLINK;
1089 netif_carrier_off(phydev->attached_dev);
1090 }
1091
1092 phydev->adjust_link(phydev->attached_dev);
1093
1094 if (phy_interrupt_is_valid(phydev))
1095 err = phy_config_interrupt(phydev,
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001096 PHY_INTERRUPT_ENABLED);
Florian Fainellie1093742013-12-17 21:38:12 -08001097 break;
1098 case PHY_HALTED:
1099 if (phydev->link) {
1100 phydev->link = 0;
1101 netif_carrier_off(phydev->attached_dev);
1102 phydev->adjust_link(phydev->attached_dev);
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001103 do_suspend = true;
Florian Fainellie1093742013-12-17 21:38:12 -08001104 }
1105 break;
1106 case PHY_RESUMING:
Florian Fainellie1093742013-12-17 21:38:12 -08001107 if (AUTONEG_ENABLE == phydev->autoneg) {
Andy Fleming00db8182005-07-30 19:31:23 -04001108 err = phy_aneg_done(phydev);
1109 if (err < 0)
1110 break;
1111
Florian Fainellie1093742013-12-17 21:38:12 -08001112 /* err > 0 if AN is done.
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001113 * Otherwise, it's 0, and we're still waiting for AN
1114 */
Andy Fleming00db8182005-07-30 19:31:23 -04001115 if (err > 0) {
Wade Farnsworth42caa072009-07-01 13:00:34 +00001116 err = phy_read_status(phydev);
1117 if (err)
1118 break;
1119
1120 if (phydev->link) {
1121 phydev->state = PHY_RUNNING;
1122 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001123 } else {
Wade Farnsworth42caa072009-07-01 13:00:34 +00001124 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001125 }
Wade Farnsworth42caa072009-07-01 13:00:34 +00001126 phydev->adjust_link(phydev->attached_dev);
Florian Fainellie1093742013-12-17 21:38:12 -08001127 } else {
1128 phydev->state = PHY_AN;
1129 phydev->link_timeout = PHY_AN_TIMEOUT;
Wade Farnsworth42caa072009-07-01 13:00:34 +00001130 }
Florian Fainellie1093742013-12-17 21:38:12 -08001131 } else {
1132 err = phy_read_status(phydev);
1133 if (err)
1134 break;
1135
1136 if (phydev->link) {
1137 phydev->state = PHY_RUNNING;
1138 netif_carrier_on(phydev->attached_dev);
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001139 } else {
Florian Fainellie1093742013-12-17 21:38:12 -08001140 phydev->state = PHY_NOLINK;
Sergei Shtylyov2f53e902014-01-05 03:17:06 +03001141 }
Florian Fainellie1093742013-12-17 21:38:12 -08001142 phydev->adjust_link(phydev->attached_dev);
1143 }
1144 break;
Andy Fleming00db8182005-07-30 19:31:23 -04001145 }
1146
Nate Case35b5f6b2008-01-29 10:05:09 -06001147 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -04001148
1149 if (needs_aneg)
Alexander Kochetkovae6a7622017-04-20 14:00:04 +03001150 err = phy_start_aneg_priv(phydev, false);
Zhangfei Gao6e14a5ee2014-05-15 13:35:34 +08001151 else if (do_suspend)
Sebastian Hesselbarthbe9dad12013-12-13 10:20:29 +01001152 phy_suspend(phydev);
1153
Andy Fleming00db8182005-07-30 19:31:23 -04001154 if (err < 0)
1155 phy_error(phydev);
1156
Andrew Lunn72ba48b2016-01-06 20:11:09 +01001157 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1158 phy_state_to_str(old_state),
1159 phy_state_to_str(phydev->state));
Florian Fainelli3e2186e2015-05-16 10:17:56 -07001160
Florian Fainellid5c3d842016-01-18 19:33:06 -08001161 /* Only re-schedule a PHY state machine change if we are polling the
1162 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1163 * between states from phy_mac_interrupt()
1164 */
1165 if (phydev->irq == PHY_POLL)
1166 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1167 PHY_STATE_TIME * HZ);
Nate Case35b5f6b2008-01-29 10:05:09 -06001168}
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001169
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001170void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1171{
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001172 phydev->link = new_link;
Florian Fainellideccd16f92016-01-18 19:33:07 -08001173
1174 /* Trigger a state machine change */
1175 queue_work(system_power_efficient_wq, &phydev->phy_queue);
Florian Fainelli5ea94e72013-05-19 22:53:43 +00001176}
1177EXPORT_SYMBOL(phy_mac_interrupt);
1178
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001179static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
1180 int addr)
1181{
1182 /* Write the desired MMD Devad */
1183 bus->write(bus, addr, MII_MMD_CTRL, devad);
1184
1185 /* Write the desired MMD register address */
1186 bus->write(bus, addr, MII_MMD_DATA, prtad);
1187
1188 /* Select the Function : DATA with no post increment */
1189 bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
1190}
1191
1192/**
1193 * phy_read_mmd_indirect - reads data from the MMD registers
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001194 * @phydev: The PHY device bus
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001195 * @prtad: MMD Address
1196 * @devad: MMD DEVAD
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001197 *
1198 * Description: it reads data from the MMD registers (clause 22 to access to
1199 * clause 45) of the specified phy address.
1200 * To read these register we have:
1201 * 1) Write reg 13 // DEVAD
1202 * 2) Write reg 14 // MMD Address
1203 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1204 * 3) Read reg 14 // Read MMD data
1205 */
Andrew Lunn053e7e12016-01-06 20:11:12 +01001206int phy_read_mmd_indirect(struct phy_device *phydev, int prtad, int devad)
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001207{
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001208 struct phy_driver *phydrv = phydev->drv;
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001209 int addr = phydev->mdio.addr;
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001210 int value = -1;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001211
Sergei Shtylyovef899c02015-08-28 21:35:14 +03001212 if (!phydrv->read_mmd_indirect) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001213 struct mii_bus *bus = phydev->mdio.bus;
Russell King05a7f582015-08-25 09:49:47 +01001214
1215 mutex_lock(&bus->mdio_lock);
1216 mmd_phy_indirect(bus, prtad, devad, addr);
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001217
1218 /* Read the content of the MMD's selected register */
Russell King05a7f582015-08-25 09:49:47 +01001219 value = bus->read(bus, addr, MII_MMD_DATA);
1220 mutex_unlock(&bus->mdio_lock);
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001221 } else {
1222 value = phydrv->read_mmd_indirect(phydev, prtad, devad, addr);
1223 }
1224 return value;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001225}
Florian Fainelli66ce7fb2014-08-22 18:55:43 -07001226EXPORT_SYMBOL(phy_read_mmd_indirect);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001227
1228/**
1229 * phy_write_mmd_indirect - writes data to the MMD registers
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001230 * @phydev: The PHY device
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001231 * @prtad: MMD Address
1232 * @devad: MMD DEVAD
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001233 * @data: data to write in the MMD register
1234 *
1235 * Description: Write data from the MMD registers of the specified
1236 * phy address.
1237 * To write these register we have:
1238 * 1) Write reg 13 // DEVAD
1239 * 2) Write reg 14 // MMD Address
1240 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1241 * 3) Write reg 14 // Write MMD data
1242 */
Florian Fainelli66ce7fb2014-08-22 18:55:43 -07001243void phy_write_mmd_indirect(struct phy_device *phydev, int prtad,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001244 int devad, u32 data)
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001245{
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001246 struct phy_driver *phydrv = phydev->drv;
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001247 int addr = phydev->mdio.addr;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001248
Sergei Shtylyovef899c02015-08-28 21:35:14 +03001249 if (!phydrv->write_mmd_indirect) {
Andrew Lunne5a03bf2016-01-06 20:11:16 +01001250 struct mii_bus *bus = phydev->mdio.bus;
Russell King05a7f582015-08-25 09:49:47 +01001251
1252 mutex_lock(&bus->mdio_lock);
1253 mmd_phy_indirect(bus, prtad, devad, addr);
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001254
1255 /* Write the data into MMD's selected register */
Russell King05a7f582015-08-25 09:49:47 +01001256 bus->write(bus, addr, MII_MMD_DATA, data);
1257 mutex_unlock(&bus->mdio_lock);
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001258 } else {
1259 phydrv->write_mmd_indirect(phydev, prtad, devad, addr, data);
1260 }
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001261}
Florian Fainelli66ce7fb2014-08-22 18:55:43 -07001262EXPORT_SYMBOL(phy_write_mmd_indirect);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001263
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001264/**
1265 * phy_init_eee - init and check the EEE feature
1266 * @phydev: target phy_device struct
1267 * @clk_stop_enable: PHY may stop the clock during LPI
1268 *
1269 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1270 * is supported by looking at the MMD registers 3.20 and 7.60/61
1271 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1272 * bit if required.
1273 */
1274int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1275{
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001276 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1277 * Also EEE feature is active when core is operating with MII, GMII
Florian Fainelli7e140692015-05-15 16:30:41 -07001278 * or RGMII (all kinds). Internal PHYs are also allowed to proceed and
1279 * should return an error if they do not support EEE.
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001280 */
1281 if ((phydev->duplex == DUPLEX_FULL) &&
1282 ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
1283 (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
Florian Fainelli32a64162015-05-26 12:19:59 -07001284 phy_interface_is_rgmii(phydev) ||
Florian Fainellia9f63092014-08-22 18:55:44 -07001285 phy_is_internal(phydev))) {
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001286 int eee_lp, eee_cap, eee_adv;
1287 u32 lp, cap, adv;
Bjorn Helgaas4ae6e502014-03-04 17:35:44 -07001288 int status;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001289
1290 /* Read phy status to properly get the right settings */
1291 status = phy_read_status(phydev);
1292 if (status)
1293 return status;
1294
1295 /* First check if the EEE ability is supported */
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001296 eee_cap = phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_ABLE,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001297 MDIO_MMD_PCS);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001298 if (eee_cap <= 0)
1299 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001300
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001301 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001302 if (!cap)
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001303 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001304
1305 /* Check which link settings negotiated and verify it in
1306 * the EEE advertising registers.
1307 */
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001308 eee_lp = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_LPABLE,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001309 MDIO_MMD_AN);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001310 if (eee_lp <= 0)
1311 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001312
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001313 eee_adv = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_ADV,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001314 MDIO_MMD_AN);
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001315 if (eee_adv <= 0)
1316 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001317
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001318 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1319 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
Guenter Roeck54da5a82015-02-17 09:36:22 -08001320 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001321 goto eee_exit_err;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001322
1323 if (clk_stop_enable) {
1324 /* Configure the PHY to stop receiving xMII
1325 * clock while it is signaling LPI.
1326 */
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001327 int val = phy_read_mmd_indirect(phydev, MDIO_CTRL1,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001328 MDIO_MMD_PCS);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001329 if (val < 0)
1330 return val;
1331
1332 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
Vince Bridgers0c1d77d2014-07-29 15:19:57 -05001333 phy_write_mmd_indirect(phydev, MDIO_CTRL1,
Andrew Lunn053e7e12016-01-06 20:11:12 +01001334 MDIO_MMD_PCS, val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001335 }
1336
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001337 return 0; /* EEE supported */
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001338 }
Giuseppe CAVALLARO7a4cecf2014-08-26 09:26:52 +02001339eee_exit_err:
Sergei Shtylyove62a7682014-01-05 03:21:52 +03001340 return -EPROTONOSUPPORT;
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001341}
1342EXPORT_SYMBOL(phy_init_eee);
1343
1344/**
1345 * phy_get_eee_err - report the EEE wake error count
1346 * @phydev: target phy_device struct
1347 *
1348 * Description: it is to report the number of time where the PHY
1349 * failed to complete its normal wake sequence.
1350 */
1351int phy_get_eee_err(struct phy_device *phydev)
1352{
Andrew Lunn053e7e12016-01-06 20:11:12 +01001353 return phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_WK_ERR, MDIO_MMD_PCS);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001354}
1355EXPORT_SYMBOL(phy_get_eee_err);
1356
1357/**
1358 * phy_ethtool_get_eee - get EEE supported and status
1359 * @phydev: target phy_device struct
1360 * @data: ethtool_eee data
1361 *
1362 * Description: it reportes the Supported/Advertisement/LP Advertisement
1363 * capabilities.
1364 */
1365int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1366{
1367 int val;
1368
1369 /* Get Supported EEE */
Andrew Lunn053e7e12016-01-06 20:11:12 +01001370 val = phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_ABLE, MDIO_MMD_PCS);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001371 if (val < 0)
1372 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001373 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001374
1375 /* Get advertisement EEE */
Andrew Lunn053e7e12016-01-06 20:11:12 +01001376 val = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_ADV, MDIO_MMD_AN);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001377 if (val < 0)
1378 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001379 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001380
1381 /* Get LP advertisement EEE */
Andrew Lunn053e7e12016-01-06 20:11:12 +01001382 val = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_LPABLE, MDIO_MMD_AN);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001383 if (val < 0)
1384 return val;
Allan, Bruce Wb32607d2012-08-20 04:55:29 +00001385 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001386
1387 return 0;
1388}
1389EXPORT_SYMBOL(phy_ethtool_get_eee);
1390
1391/**
1392 * phy_ethtool_set_eee - set EEE supported and status
1393 * @phydev: target phy_device struct
1394 * @data: ethtool_eee data
1395 *
1396 * Description: it is to program the Advertisement EEE register.
1397 */
1398int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1399{
Sergei Shtylyov553fe922014-01-05 03:23:19 +03001400 int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001401
jbrunet97ace182016-11-28 10:46:46 +01001402 /* Mask prohibited EEE modes */
1403 val &= ~phydev->eee_broken_modes;
1404
Andrew Lunn053e7e12016-01-06 20:11:12 +01001405 phy_write_mmd_indirect(phydev, MDIO_AN_EEE_ADV, MDIO_MMD_AN, val);
Giuseppe CAVALLAROa59a4d12012-06-27 21:14:38 +00001406
1407 return 0;
1408}
1409EXPORT_SYMBOL(phy_ethtool_set_eee);
Michael Stapelberg42e836e2013-03-11 13:56:44 +00001410
1411int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1412{
1413 if (phydev->drv->set_wol)
1414 return phydev->drv->set_wol(phydev, wol);
1415
1416 return -EOPNOTSUPP;
1417}
1418EXPORT_SYMBOL(phy_ethtool_set_wol);
1419
1420void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1421{
1422 if (phydev->drv->get_wol)
1423 phydev->drv->get_wol(phydev, wol);
1424}
1425EXPORT_SYMBOL(phy_ethtool_get_wol);
Philippe Reynes9d9a77c2016-05-10 00:19:41 +02001426
1427int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1428 struct ethtool_link_ksettings *cmd)
1429{
1430 struct phy_device *phydev = ndev->phydev;
1431
1432 if (!phydev)
1433 return -ENODEV;
1434
1435 return phy_ethtool_ksettings_get(phydev, cmd);
1436}
1437EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1438
1439int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1440 const struct ethtool_link_ksettings *cmd)
1441{
1442 struct phy_device *phydev = ndev->phydev;
1443
1444 if (!phydev)
1445 return -ENODEV;
1446
1447 return phy_ethtool_ksettings_set(phydev, cmd);
1448}
1449EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);