Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1 | /* Framework for configuring and reading PHY devices |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2 | * 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. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 7 | * Copyright (c) 2006, 2007 Maciej W. Rozycki |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 8 | * |
| 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 Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 15 | |
| 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 17 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 19 | #include <linux/string.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/unistd.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 22 | #include <linux/interrupt.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 23 | #include <linux/delay.h> |
| 24 | #include <linux/netdevice.h> |
| 25 | #include <linux/etherdevice.h> |
| 26 | #include <linux/skbuff.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 27 | #include <linux/mm.h> |
| 28 | #include <linux/module.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 29 | #include <linux/mii.h> |
| 30 | #include <linux/ethtool.h> |
| 31 | #include <linux/phy.h> |
Geert Uytterhoeven | d6f8cfa | 2017-01-25 11:39:49 +0100 | [diff] [blame] | 32 | #include <linux/phy_led_triggers.h> |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 33 | #include <linux/timer.h> |
| 34 | #include <linux/workqueue.h> |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 35 | #include <linux/mdio.h> |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 36 | #include <linux/io.h> |
| 37 | #include <linux/uaccess.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 38 | #include <linux/atomic.h> |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 39 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 40 | #include <asm/irq.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 41 | |
Florian Fainelli | 766d1d3 | 2014-02-11 17:27:35 -0800 | [diff] [blame] | 42 | static const char *phy_speed_to_str(int speed) |
| 43 | { |
| 44 | switch (speed) { |
| 45 | case SPEED_10: |
| 46 | return "10Mbps"; |
| 47 | case SPEED_100: |
| 48 | return "100Mbps"; |
| 49 | case SPEED_1000: |
| 50 | return "1Gbps"; |
| 51 | case SPEED_2500: |
| 52 | return "2.5Gbps"; |
Joe Perches | 1f37b17 | 2017-04-02 14:30:06 -0700 | [diff] [blame] | 53 | case SPEED_5000: |
| 54 | return "5Gbps"; |
Florian Fainelli | 766d1d3 | 2014-02-11 17:27:35 -0800 | [diff] [blame] | 55 | case SPEED_10000: |
| 56 | return "10Gbps"; |
Joe Perches | 1f37b17 | 2017-04-02 14:30:06 -0700 | [diff] [blame] | 57 | case SPEED_20000: |
| 58 | return "20Gbps"; |
| 59 | case SPEED_25000: |
| 60 | return "25Gbps"; |
| 61 | case SPEED_40000: |
| 62 | return "40Gbps"; |
| 63 | case SPEED_50000: |
| 64 | return "50Gbps"; |
| 65 | case SPEED_56000: |
| 66 | return "56Gbps"; |
| 67 | case SPEED_100000: |
| 68 | return "100Gbps"; |
Florian Fainelli | 766d1d3 | 2014-02-11 17:27:35 -0800 | [diff] [blame] | 69 | case SPEED_UNKNOWN: |
| 70 | return "Unknown"; |
| 71 | default: |
| 72 | return "Unsupported (update phy.c)"; |
| 73 | } |
| 74 | } |
| 75 | |
Florian Fainelli | 3e2186e | 2015-05-16 10:17:56 -0700 | [diff] [blame] | 76 | #define PHY_STATE_STR(_state) \ |
| 77 | case PHY_##_state: \ |
| 78 | return __stringify(_state); \ |
| 79 | |
| 80 | static const char *phy_state_to_str(enum phy_state st) |
| 81 | { |
| 82 | switch (st) { |
| 83 | PHY_STATE_STR(DOWN) |
| 84 | PHY_STATE_STR(STARTING) |
| 85 | PHY_STATE_STR(READY) |
| 86 | PHY_STATE_STR(PENDING) |
| 87 | PHY_STATE_STR(UP) |
| 88 | PHY_STATE_STR(AN) |
| 89 | PHY_STATE_STR(RUNNING) |
| 90 | PHY_STATE_STR(NOLINK) |
| 91 | PHY_STATE_STR(FORCING) |
| 92 | PHY_STATE_STR(CHANGELINK) |
| 93 | PHY_STATE_STR(HALTED) |
| 94 | PHY_STATE_STR(RESUMING) |
| 95 | } |
| 96 | |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 101 | /** |
| 102 | * phy_print_status - Convenience function to print out the current phy status |
| 103 | * @phydev: the phy_device struct |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 104 | */ |
| 105 | void phy_print_status(struct phy_device *phydev) |
| 106 | { |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 107 | if (phydev->link) { |
Florian Fainelli | df40cc8 | 2014-02-11 17:27:34 -0800 | [diff] [blame] | 108 | netdev_info(phydev->attached_dev, |
Florian Fainelli | 766d1d3 | 2014-02-11 17:27:35 -0800 | [diff] [blame] | 109 | "Link is Up - %s/%s - flow control %s\n", |
| 110 | phy_speed_to_str(phydev->speed), |
Florian Fainelli | df40cc8 | 2014-02-11 17:27:34 -0800 | [diff] [blame] | 111 | DUPLEX_FULL == phydev->duplex ? "Full" : "Half", |
| 112 | phydev->pause ? "rx/tx" : "off"); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 113 | } else { |
Florian Fainelli | 43b6329 | 2014-02-11 17:27:33 -0800 | [diff] [blame] | 114 | netdev_info(phydev->attached_dev, "Link is Down\n"); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 115 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 116 | } |
| 117 | EXPORT_SYMBOL(phy_print_status); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 118 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 119 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 120 | * phy_clear_interrupt - Ack the phy device's interrupt |
| 121 | * @phydev: the phy_device struct |
| 122 | * |
| 123 | * If the @phydev driver has an ack_interrupt function, call it to |
| 124 | * ack and clear the phy device's interrupt. |
| 125 | * |
Florian Fainelli | ad03350 | 2014-02-11 17:27:38 -0800 | [diff] [blame] | 126 | * Returns 0 on success or < 0 on error. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 127 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 128 | static int phy_clear_interrupt(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 129 | { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 130 | if (phydev->drv->ack_interrupt) |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 131 | return phydev->drv->ack_interrupt(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 132 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 133 | return 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 136 | /** |
| 137 | * phy_config_interrupt - configure the PHY device for the requested interrupts |
| 138 | * @phydev: the phy_device struct |
| 139 | * @interrupts: interrupt flags to configure for this @phydev |
| 140 | * |
Florian Fainelli | ad03350 | 2014-02-11 17:27:38 -0800 | [diff] [blame] | 141 | * Returns 0 on success or < 0 on error. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 142 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 143 | static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 144 | { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 145 | phydev->interrupts = interrupts; |
| 146 | if (phydev->drv->config_intr) |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 147 | return phydev->drv->config_intr(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 148 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 149 | return 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 153 | /** |
| 154 | * phy_aneg_done - return auto-negotiation status |
| 155 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 156 | * |
Florian Fainelli | 76a423a | 2014-02-11 17:27:37 -0800 | [diff] [blame] | 157 | * Description: Return the auto-negotiation status from this @phydev |
| 158 | * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation |
| 159 | * is still pending. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 160 | */ |
Lendacky, Thomas | 372788f | 2016-11-10 17:10:46 -0600 | [diff] [blame] | 161 | int phy_aneg_done(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 162 | { |
Florian Fainelli | 65f2767 | 2017-02-23 14:22:19 -0800 | [diff] [blame] | 163 | if (phydev->drv && phydev->drv->aneg_done) |
Florian Fainelli | 76a423a | 2014-02-11 17:27:37 -0800 | [diff] [blame] | 164 | return phydev->drv->aneg_done(phydev); |
| 165 | |
Florian Fainelli | a9fa6e6 | 2014-02-11 17:27:36 -0800 | [diff] [blame] | 166 | return genphy_aneg_done(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 167 | } |
Lendacky, Thomas | 372788f | 2016-11-10 17:10:46 -0600 | [diff] [blame] | 168 | EXPORT_SYMBOL(phy_aneg_done); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 169 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 170 | /* A structure for mapping a particular speed and duplex |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 171 | * combination to a particular SUPPORTED and ADVERTISED value |
| 172 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 173 | struct phy_setting { |
| 174 | int speed; |
| 175 | int duplex; |
| 176 | u32 setting; |
| 177 | }; |
| 178 | |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 179 | /* A mapping of all SUPPORTED settings to speed/duplex. This table |
| 180 | * must be grouped by speed and sorted in descending match priority |
| 181 | * - iow, descending speed. */ |
Arjan van de Ven | f71e130 | 2006-03-03 21:33:57 -0500 | [diff] [blame] | 182 | static const struct phy_setting settings[] = { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 183 | { |
Lendacky, Thomas | 3e70770 | 2014-07-14 14:05:46 -0500 | [diff] [blame] | 184 | .speed = SPEED_10000, |
| 185 | .duplex = DUPLEX_FULL, |
| 186 | .setting = SUPPORTED_10000baseKR_Full, |
| 187 | }, |
| 188 | { |
| 189 | .speed = SPEED_10000, |
| 190 | .duplex = DUPLEX_FULL, |
| 191 | .setting = SUPPORTED_10000baseKX4_Full, |
| 192 | }, |
| 193 | { |
| 194 | .speed = SPEED_10000, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 195 | .duplex = DUPLEX_FULL, |
| 196 | .setting = SUPPORTED_10000baseT_Full, |
| 197 | }, |
| 198 | { |
Lendacky, Thomas | 3e70770 | 2014-07-14 14:05:46 -0500 | [diff] [blame] | 199 | .speed = SPEED_2500, |
| 200 | .duplex = DUPLEX_FULL, |
| 201 | .setting = SUPPORTED_2500baseX_Full, |
| 202 | }, |
| 203 | { |
| 204 | .speed = SPEED_1000, |
| 205 | .duplex = DUPLEX_FULL, |
| 206 | .setting = SUPPORTED_1000baseKX_Full, |
| 207 | }, |
| 208 | { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 209 | .speed = SPEED_1000, |
| 210 | .duplex = DUPLEX_FULL, |
| 211 | .setting = SUPPORTED_1000baseT_Full, |
| 212 | }, |
| 213 | { |
| 214 | .speed = SPEED_1000, |
| 215 | .duplex = DUPLEX_HALF, |
| 216 | .setting = SUPPORTED_1000baseT_Half, |
| 217 | }, |
| 218 | { |
| 219 | .speed = SPEED_100, |
| 220 | .duplex = DUPLEX_FULL, |
| 221 | .setting = SUPPORTED_100baseT_Full, |
| 222 | }, |
| 223 | { |
| 224 | .speed = SPEED_100, |
| 225 | .duplex = DUPLEX_HALF, |
| 226 | .setting = SUPPORTED_100baseT_Half, |
| 227 | }, |
| 228 | { |
| 229 | .speed = SPEED_10, |
| 230 | .duplex = DUPLEX_FULL, |
| 231 | .setting = SUPPORTED_10baseT_Full, |
| 232 | }, |
| 233 | { |
| 234 | .speed = SPEED_10, |
| 235 | .duplex = DUPLEX_HALF, |
| 236 | .setting = SUPPORTED_10baseT_Half, |
| 237 | }, |
| 238 | }; |
| 239 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 240 | /** |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 241 | * phy_lookup_setting - lookup a PHY setting |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 242 | * @speed: speed to match |
| 243 | * @duplex: duplex to match |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 244 | * @feature: allowed link modes |
| 245 | * @exact: an exact match is required |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 246 | * |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 247 | * Search the settings array for a setting that matches the speed and |
| 248 | * duplex, and which is supported. |
| 249 | * |
| 250 | * If @exact is unset, either an exact match or %NULL for no match will |
| 251 | * be returned. |
| 252 | * |
| 253 | * If @exact is set, an exact match, the fastest supported setting at |
| 254 | * or below the specified speed, the slowest supported setting, or if |
| 255 | * they all fail, %NULL will be returned. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 256 | */ |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 257 | static const struct phy_setting * |
| 258 | phy_lookup_setting(int speed, int duplex, u32 features, bool exact) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 259 | { |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 260 | const struct phy_setting *p, *match = NULL, *last = NULL; |
| 261 | int i; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 262 | |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 263 | for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) { |
| 264 | if (p->setting & features) { |
| 265 | last = p; |
| 266 | if (p->speed == speed && p->duplex == duplex) { |
| 267 | /* Exact match for speed and duplex */ |
| 268 | match = p; |
| 269 | break; |
| 270 | } else if (!exact) { |
| 271 | if (!match && p->speed <= speed) |
| 272 | /* Candidate */ |
| 273 | match = p; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 274 | |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 275 | if (p->speed < speed) |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if (!match && !exact) |
| 282 | match = last; |
| 283 | |
| 284 | return match; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 287 | /** |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 288 | * phy_find_valid - find a PHY setting that matches the requested parameters |
| 289 | * @speed: desired speed |
| 290 | * @duplex: desired duplex |
| 291 | * @supported: mask of supported link modes |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 292 | * |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 293 | * Locate a supported phy setting that is, in priority order: |
| 294 | * - an exact match for the specified speed and duplex mode |
| 295 | * - a match for the specified speed, or slower speed |
| 296 | * - the slowest supported speed |
| 297 | * Returns the matched phy_setting entry, or %NULL if no supported phy |
| 298 | * settings were found. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 299 | */ |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 300 | static const struct phy_setting * |
| 301 | phy_find_valid(int speed, int duplex, u32 supported) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 302 | { |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 303 | return phy_lookup_setting(speed, duplex, supported, false); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 304 | } |
| 305 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 306 | /** |
Zach Brown | 1f9127c | 2016-10-17 10:49:54 -0500 | [diff] [blame] | 307 | * phy_supported_speeds - return all speeds currently supported by a phy device |
| 308 | * @phy: The phy device to return supported speeds of. |
| 309 | * @speeds: buffer to store supported speeds in. |
| 310 | * @size: size of speeds buffer. |
| 311 | * |
| 312 | * Description: Returns the number of supported speeds, and fills the speeds |
| 313 | * buffer with the supported speeds. If speeds buffer is too small to contain |
| 314 | * all currently supported speeds, will return as many speeds as can fit. |
| 315 | */ |
| 316 | unsigned int phy_supported_speeds(struct phy_device *phy, |
| 317 | unsigned int *speeds, |
| 318 | unsigned int size) |
| 319 | { |
| 320 | unsigned int count = 0; |
| 321 | unsigned int idx = 0; |
| 322 | |
Russell King | 786df9c | 2017-04-13 16:49:20 +0100 | [diff] [blame] | 323 | for (idx = 0; idx < ARRAY_SIZE(settings) && count < size; idx++) |
Zach Brown | 1f9127c | 2016-10-17 10:49:54 -0500 | [diff] [blame] | 324 | /* Assumes settings are grouped by speed */ |
Russell King | 786df9c | 2017-04-13 16:49:20 +0100 | [diff] [blame] | 325 | if ((settings[idx].setting & phy->supported) && |
| 326 | (count == 0 || speeds[count - 1] != settings[idx].speed)) |
| 327 | speeds[count++] = settings[idx].speed; |
Zach Brown | 1f9127c | 2016-10-17 10:49:54 -0500 | [diff] [blame] | 328 | |
| 329 | return count; |
| 330 | } |
| 331 | |
| 332 | /** |
Guenter Roeck | 54da5a8 | 2015-02-17 09:36:22 -0800 | [diff] [blame] | 333 | * phy_check_valid - check if there is a valid PHY setting which matches |
| 334 | * speed, duplex, and feature mask |
| 335 | * @speed: speed to match |
| 336 | * @duplex: duplex to match |
| 337 | * @features: A mask of the valid settings |
| 338 | * |
| 339 | * Description: Returns true if there is a valid setting, false otherwise. |
| 340 | */ |
| 341 | static inline bool phy_check_valid(int speed, int duplex, u32 features) |
| 342 | { |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 343 | return !!phy_lookup_setting(speed, duplex, features, true); |
Guenter Roeck | 54da5a8 | 2015-02-17 09:36:22 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 347 | * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex |
| 348 | * @phydev: the target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 349 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 350 | * Description: Make sure the PHY is set to supported speeds and |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 351 | * duplexes. Drop down by one in this order: 1000/FULL, |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 352 | * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 353 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 354 | static void phy_sanitize_settings(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 355 | { |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 356 | const struct phy_setting *setting; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 357 | u32 features = phydev->supported; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 358 | |
| 359 | /* Sanitize settings based on PHY capabilities */ |
| 360 | if ((features & SUPPORTED_Autoneg) == 0) |
Domen Puncer | 163642a | 2007-08-07 12:12:41 +0200 | [diff] [blame] | 361 | phydev->autoneg = AUTONEG_DISABLE; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 362 | |
Russell King | d061303 | 2017-04-13 16:49:15 +0100 | [diff] [blame] | 363 | setting = phy_find_valid(phydev->speed, phydev->duplex, features); |
| 364 | if (setting) { |
| 365 | phydev->speed = setting->speed; |
| 366 | phydev->duplex = setting->duplex; |
| 367 | } else { |
| 368 | /* We failed to find anything (no supported speeds?) */ |
| 369 | phydev->speed = SPEED_UNKNOWN; |
| 370 | phydev->duplex = DUPLEX_UNKNOWN; |
| 371 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 372 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 373 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 374 | /** |
| 375 | * phy_ethtool_sset - generic ethtool sset function, handles all the details |
| 376 | * @phydev: target phy_device struct |
| 377 | * @cmd: ethtool_cmd |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 378 | * |
| 379 | * A few notes about parameter checking: |
| 380 | * - We don't set port or transceiver, so we don't care what they |
| 381 | * were set to. |
| 382 | * - phy_start_aneg() will make sure forced settings are sane, and |
| 383 | * choose the next best ones from the ones selected, so we don't |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 384 | * care if ethtool tries to give us bad values. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 385 | */ |
| 386 | int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd) |
| 387 | { |
David Decotigny | 25db033 | 2011-04-27 18:32:39 +0000 | [diff] [blame] | 388 | u32 speed = ethtool_cmd_speed(cmd); |
| 389 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 390 | if (cmd->phy_address != phydev->mdio.addr) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 391 | return -EINVAL; |
| 392 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 393 | /* We make sure that we don't pass unsupported values in to the PHY */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 394 | cmd->advertising &= phydev->supported; |
| 395 | |
| 396 | /* Verify the settings we care about. */ |
| 397 | if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE) |
| 398 | return -EINVAL; |
| 399 | |
| 400 | if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0) |
| 401 | return -EINVAL; |
| 402 | |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 403 | if (cmd->autoneg == AUTONEG_DISABLE && |
David Decotigny | 25db033 | 2011-04-27 18:32:39 +0000 | [diff] [blame] | 404 | ((speed != SPEED_1000 && |
| 405 | speed != SPEED_100 && |
| 406 | speed != SPEED_10) || |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 407 | (cmd->duplex != DUPLEX_HALF && |
| 408 | cmd->duplex != DUPLEX_FULL))) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 409 | return -EINVAL; |
| 410 | |
| 411 | phydev->autoneg = cmd->autoneg; |
| 412 | |
David Decotigny | 25db033 | 2011-04-27 18:32:39 +0000 | [diff] [blame] | 413 | phydev->speed = speed; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 414 | |
| 415 | phydev->advertising = cmd->advertising; |
| 416 | |
| 417 | if (AUTONEG_ENABLE == cmd->autoneg) |
| 418 | phydev->advertising |= ADVERTISED_Autoneg; |
| 419 | else |
| 420 | phydev->advertising &= ~ADVERTISED_Autoneg; |
| 421 | |
| 422 | phydev->duplex = cmd->duplex; |
| 423 | |
Raju Lakkaraju | 1004ee6 | 2016-11-29 15:16:47 +0530 | [diff] [blame] | 424 | phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl; |
David Thomson | 634ec36 | 2015-07-10 13:56:54 +1200 | [diff] [blame] | 425 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 426 | /* Restart the PHY */ |
| 427 | phy_start_aneg(phydev); |
| 428 | |
| 429 | return 0; |
| 430 | } |
Kumar Gala | 9f6d55d | 2007-01-20 16:38:26 -0600 | [diff] [blame] | 431 | EXPORT_SYMBOL(phy_ethtool_sset); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 432 | |
Philippe Reynes | 2d55173 | 2016-04-15 00:35:00 +0200 | [diff] [blame] | 433 | int phy_ethtool_ksettings_set(struct phy_device *phydev, |
| 434 | const struct ethtool_link_ksettings *cmd) |
| 435 | { |
| 436 | u8 autoneg = cmd->base.autoneg; |
| 437 | u8 duplex = cmd->base.duplex; |
| 438 | u32 speed = cmd->base.speed; |
| 439 | u32 advertising; |
| 440 | |
| 441 | if (cmd->base.phy_address != phydev->mdio.addr) |
| 442 | return -EINVAL; |
| 443 | |
| 444 | ethtool_convert_link_mode_to_legacy_u32(&advertising, |
| 445 | cmd->link_modes.advertising); |
| 446 | |
| 447 | /* We make sure that we don't pass unsupported values in to the PHY */ |
| 448 | advertising &= phydev->supported; |
| 449 | |
| 450 | /* Verify the settings we care about. */ |
| 451 | if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE) |
| 452 | return -EINVAL; |
| 453 | |
| 454 | if (autoneg == AUTONEG_ENABLE && advertising == 0) |
| 455 | return -EINVAL; |
| 456 | |
| 457 | if (autoneg == AUTONEG_DISABLE && |
| 458 | ((speed != SPEED_1000 && |
| 459 | speed != SPEED_100 && |
| 460 | speed != SPEED_10) || |
| 461 | (duplex != DUPLEX_HALF && |
| 462 | duplex != DUPLEX_FULL))) |
| 463 | return -EINVAL; |
| 464 | |
| 465 | phydev->autoneg = autoneg; |
| 466 | |
| 467 | phydev->speed = speed; |
| 468 | |
| 469 | phydev->advertising = advertising; |
| 470 | |
| 471 | if (autoneg == AUTONEG_ENABLE) |
| 472 | phydev->advertising |= ADVERTISED_Autoneg; |
| 473 | else |
| 474 | phydev->advertising &= ~ADVERTISED_Autoneg; |
| 475 | |
| 476 | phydev->duplex = duplex; |
| 477 | |
Raju Lakkaraju | 1004ee6 | 2016-11-29 15:16:47 +0530 | [diff] [blame] | 478 | phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl; |
Philippe Reynes | 2d55173 | 2016-04-15 00:35:00 +0200 | [diff] [blame] | 479 | |
| 480 | /* Restart the PHY */ |
| 481 | phy_start_aneg(phydev); |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | EXPORT_SYMBOL(phy_ethtool_ksettings_set); |
| 486 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 487 | int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd) |
| 488 | { |
| 489 | cmd->supported = phydev->supported; |
| 490 | |
| 491 | cmd->advertising = phydev->advertising; |
Florian Fainelli | 114002b | 2013-12-06 13:01:30 -0800 | [diff] [blame] | 492 | cmd->lp_advertising = phydev->lp_advertising; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 493 | |
David Decotigny | 7073949 | 2011-04-27 18:32:40 +0000 | [diff] [blame] | 494 | ethtool_cmd_speed_set(cmd, phydev->speed); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 495 | cmd->duplex = phydev->duplex; |
Florian Fainelli | c88838c | 2014-02-13 16:08:43 -0800 | [diff] [blame] | 496 | if (phydev->interface == PHY_INTERFACE_MODE_MOCA) |
| 497 | cmd->port = PORT_BNC; |
| 498 | else |
| 499 | cmd->port = PORT_MII; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 500 | cmd->phy_address = phydev->mdio.addr; |
Florian Fainelli | 4284b6a | 2013-05-23 01:11:12 +0000 | [diff] [blame] | 501 | cmd->transceiver = phy_is_internal(phydev) ? |
| 502 | XCVR_INTERNAL : XCVR_EXTERNAL; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 503 | cmd->autoneg = phydev->autoneg; |
Raju Lakkaraju | 1004ee6 | 2016-11-29 15:16:47 +0530 | [diff] [blame] | 504 | cmd->eth_tp_mdix_ctrl = phydev->mdix_ctrl; |
| 505 | cmd->eth_tp_mdix = phydev->mdix; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 506 | |
| 507 | return 0; |
| 508 | } |
Kumar Gala | 9f6d55d | 2007-01-20 16:38:26 -0600 | [diff] [blame] | 509 | EXPORT_SYMBOL(phy_ethtool_gset); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 510 | |
Philippe Reynes | 2d55173 | 2016-04-15 00:35:00 +0200 | [diff] [blame] | 511 | int phy_ethtool_ksettings_get(struct phy_device *phydev, |
| 512 | struct ethtool_link_ksettings *cmd) |
| 513 | { |
| 514 | ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, |
| 515 | phydev->supported); |
| 516 | |
| 517 | ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, |
| 518 | phydev->advertising); |
| 519 | |
| 520 | ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising, |
| 521 | phydev->lp_advertising); |
| 522 | |
| 523 | cmd->base.speed = phydev->speed; |
| 524 | cmd->base.duplex = phydev->duplex; |
| 525 | if (phydev->interface == PHY_INTERFACE_MODE_MOCA) |
| 526 | cmd->base.port = PORT_BNC; |
| 527 | else |
| 528 | cmd->base.port = PORT_MII; |
| 529 | |
| 530 | cmd->base.phy_address = phydev->mdio.addr; |
| 531 | cmd->base.autoneg = phydev->autoneg; |
Raju Lakkaraju | 1004ee6 | 2016-11-29 15:16:47 +0530 | [diff] [blame] | 532 | cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl; |
| 533 | cmd->base.eth_tp_mdix = phydev->mdix; |
Philippe Reynes | 2d55173 | 2016-04-15 00:35:00 +0200 | [diff] [blame] | 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | EXPORT_SYMBOL(phy_ethtool_ksettings_get); |
| 538 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 539 | /** |
| 540 | * phy_mii_ioctl - generic PHY MII ioctl interface |
| 541 | * @phydev: the phy_device struct |
Randy Dunlap | 00c7d92 | 2010-08-09 13:41:59 +0000 | [diff] [blame] | 542 | * @ifr: &struct ifreq for socket ioctl's |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 543 | * @cmd: ioctl cmd to execute |
| 544 | * |
| 545 | * Note that this function is currently incompatible with the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 546 | * PHYCONTROL layer. It changes registers without regard to |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 547 | * current state. Use at own risk. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 548 | */ |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 549 | int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 550 | { |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 551 | struct mii_ioctl_data *mii_data = if_mii(ifr); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 552 | u16 val = mii_data->val_in; |
Brian Hill | 79ce047 | 2014-11-11 13:39:39 -0700 | [diff] [blame] | 553 | bool change_autoneg = false; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 554 | |
| 555 | switch (cmd) { |
| 556 | case SIOCGMIIPHY: |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 557 | mii_data->phy_id = phydev->mdio.addr; |
Lennert Buytenhek | c6d6a51 | 2008-09-18 03:06:52 +0000 | [diff] [blame] | 558 | /* fall through */ |
| 559 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 560 | case SIOCGMIIREG: |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 561 | mii_data->val_out = mdiobus_read(phydev->mdio.bus, |
| 562 | mii_data->phy_id, |
Peter Korsgaard | af1dc13 | 2011-03-10 06:52:13 +0000 | [diff] [blame] | 563 | mii_data->reg_num); |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 564 | return 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 565 | |
| 566 | case SIOCSMIIREG: |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 567 | if (mii_data->phy_id == phydev->mdio.addr) { |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 568 | switch (mii_data->reg_num) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 569 | case MII_BMCR: |
Brian Hill | 79ce047 | 2014-11-11 13:39:39 -0700 | [diff] [blame] | 570 | if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) { |
| 571 | if (phydev->autoneg == AUTONEG_ENABLE) |
| 572 | change_autoneg = true; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 573 | phydev->autoneg = AUTONEG_DISABLE; |
Brian Hill | 79ce047 | 2014-11-11 13:39:39 -0700 | [diff] [blame] | 574 | if (val & BMCR_FULLDPLX) |
| 575 | phydev->duplex = DUPLEX_FULL; |
| 576 | else |
| 577 | phydev->duplex = DUPLEX_HALF; |
| 578 | if (val & BMCR_SPEED1000) |
| 579 | phydev->speed = SPEED_1000; |
| 580 | else if (val & BMCR_SPEED100) |
| 581 | phydev->speed = SPEED_100; |
| 582 | else phydev->speed = SPEED_10; |
| 583 | } |
| 584 | else { |
| 585 | if (phydev->autoneg == AUTONEG_DISABLE) |
| 586 | change_autoneg = true; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 587 | phydev->autoneg = AUTONEG_ENABLE; |
Brian Hill | 79ce047 | 2014-11-11 13:39:39 -0700 | [diff] [blame] | 588 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 589 | break; |
| 590 | case MII_ADVERTISE: |
Brian Hill | 79ce047 | 2014-11-11 13:39:39 -0700 | [diff] [blame] | 591 | phydev->advertising = mii_adv_to_ethtool_adv_t(val); |
| 592 | change_autoneg = true; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 593 | break; |
| 594 | default: |
| 595 | /* do nothing */ |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 600 | mdiobus_write(phydev->mdio.bus, mii_data->phy_id, |
Peter Korsgaard | af1dc13 | 2011-03-10 06:52:13 +0000 | [diff] [blame] | 601 | mii_data->reg_num, val); |
| 602 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 603 | if (mii_data->phy_id == phydev->mdio.addr && |
Jérôme Pouiller | cf18b77 | 2015-12-03 10:02:35 +0100 | [diff] [blame] | 604 | mii_data->reg_num == MII_BMCR && |
Florian Fainelli | 2613f95 | 2013-12-06 13:01:31 -0800 | [diff] [blame] | 605 | val & BMCR_RESET) |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 606 | return phy_init_hw(phydev); |
Brian Hill | 79ce047 | 2014-11-11 13:39:39 -0700 | [diff] [blame] | 607 | |
| 608 | if (change_autoneg) |
| 609 | return phy_start_aneg(phydev); |
| 610 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 611 | return 0; |
David Woodhouse | dda93b4 | 2007-11-28 19:56:34 +0000 | [diff] [blame] | 612 | |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 613 | case SIOCSHWTSTAMP: |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 614 | if (phydev->drv && phydev->drv->hwtstamp) |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 615 | return phydev->drv->hwtstamp(phydev, ifr); |
| 616 | /* fall through */ |
| 617 | |
David Woodhouse | dda93b4 | 2007-11-28 19:56:34 +0000 | [diff] [blame] | 618 | default: |
Lennert Buytenhek | c6d6a51 | 2008-09-18 03:06:52 +0000 | [diff] [blame] | 619 | return -EOPNOTSUPP; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 620 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 621 | } |
Domen Puncer | 680e9fe | 2007-09-17 22:21:40 +0200 | [diff] [blame] | 622 | EXPORT_SYMBOL(phy_mii_ioctl); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 623 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 624 | /** |
Alexander Kochetkov | f555f34 | 2017-04-20 14:00:04 +0300 | [diff] [blame] | 625 | * phy_start_aneg_priv - start auto-negotiation for this PHY device |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 626 | * @phydev: the phy_device struct |
Alexander Kochetkov | f555f34 | 2017-04-20 14:00:04 +0300 | [diff] [blame] | 627 | * @sync: indicate whether we should wait for the workqueue cancelation |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 628 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 629 | * Description: Sanitizes the settings (if we're not autonegotiating |
| 630 | * them), and then calls the driver's config_aneg function. |
| 631 | * If the PHYCONTROL Layer is operating, we change the state to |
| 632 | * reflect the beginning of Auto-negotiation or forcing. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 633 | */ |
Alexander Kochetkov | f555f34 | 2017-04-20 14:00:04 +0300 | [diff] [blame] | 634 | static int phy_start_aneg_priv(struct phy_device *phydev, bool sync) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 635 | { |
Alexander Kochetkov | f555f34 | 2017-04-20 14:00:04 +0300 | [diff] [blame] | 636 | bool trigger = 0; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 637 | int err; |
| 638 | |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 639 | if (!phydev->drv) |
| 640 | return -EIO; |
| 641 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 642 | mutex_lock(&phydev->lock); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 643 | |
| 644 | if (AUTONEG_DISABLE == phydev->autoneg) |
| 645 | phy_sanitize_settings(phydev); |
| 646 | |
Ben Hutchings | 9b3320e | 2015-01-27 00:58:15 +0000 | [diff] [blame] | 647 | /* Invalidate LP advertising flags */ |
| 648 | phydev->lp_advertising = 0; |
| 649 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 650 | err = phydev->drv->config_aneg(phydev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 651 | if (err < 0) |
| 652 | goto out_unlock; |
| 653 | |
| 654 | if (phydev->state != PHY_HALTED) { |
| 655 | if (AUTONEG_ENABLE == phydev->autoneg) { |
| 656 | phydev->state = PHY_AN; |
| 657 | phydev->link_timeout = PHY_AN_TIMEOUT; |
| 658 | } else { |
| 659 | phydev->state = PHY_FORCING; |
| 660 | phydev->link_timeout = PHY_FORCE_TIMEOUT; |
| 661 | } |
| 662 | } |
| 663 | |
Alexander Kochetkov | f555f34 | 2017-04-20 14:00:04 +0300 | [diff] [blame] | 664 | /* Re-schedule a PHY state machine to check PHY status because |
| 665 | * negotiation may already be done and aneg interrupt may not be |
| 666 | * generated. |
| 667 | */ |
| 668 | if (phy_interrupt_is_valid(phydev) && (phydev->state == PHY_AN)) { |
| 669 | err = phy_aneg_done(phydev); |
| 670 | if (err > 0) { |
| 671 | trigger = true; |
| 672 | err = 0; |
| 673 | } |
| 674 | } |
| 675 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 676 | out_unlock: |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 677 | mutex_unlock(&phydev->lock); |
Alexander Kochetkov | f555f34 | 2017-04-20 14:00:04 +0300 | [diff] [blame] | 678 | |
| 679 | if (trigger) |
| 680 | phy_trigger_machine(phydev, sync); |
| 681 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 682 | return err; |
| 683 | } |
Alexander Kochetkov | f555f34 | 2017-04-20 14:00:04 +0300 | [diff] [blame] | 684 | |
| 685 | /** |
| 686 | * phy_start_aneg - start auto-negotiation for this PHY device |
| 687 | * @phydev: the phy_device struct |
| 688 | * |
| 689 | * Description: Sanitizes the settings (if we're not autonegotiating |
| 690 | * them), and then calls the driver's config_aneg function. |
| 691 | * If the PHYCONTROL Layer is operating, we change the state to |
| 692 | * reflect the beginning of Auto-negotiation or forcing. |
| 693 | */ |
| 694 | int phy_start_aneg(struct phy_device *phydev) |
| 695 | { |
| 696 | return phy_start_aneg_priv(phydev, true); |
| 697 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 698 | EXPORT_SYMBOL(phy_start_aneg); |
| 699 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 700 | /** |
| 701 | * phy_start_machine - start PHY state machine tracking |
| 702 | * @phydev: the phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 703 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 704 | * Description: The PHY infrastructure can run a state machine |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 705 | * which tracks whether the PHY is starting up, negotiating, |
| 706 | * etc. This function starts the timer which tracks the state |
Sergei Shtylyov | 29935ae | 2014-01-05 03:27:17 +0300 | [diff] [blame] | 707 | * of the PHY. If you want to maintain your own state machine, |
| 708 | * do not call this function. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 709 | */ |
Sergei Shtylyov | 29935ae | 2014-01-05 03:27:17 +0300 | [diff] [blame] | 710 | void phy_start_machine(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 711 | { |
Viresh Kumar | bbb47bd | 2013-04-24 17:12:55 +0530 | [diff] [blame] | 712 | queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 713 | } |
| 714 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 715 | /** |
Andrew Lunn | 3c293f4 | 2016-10-12 22:14:53 +0200 | [diff] [blame] | 716 | * phy_trigger_machine - trigger the state machine to run |
| 717 | * |
| 718 | * @phydev: the phy_device struct |
Florian Fainelli | eab1277 | 2017-01-20 15:31:52 -0800 | [diff] [blame] | 719 | * @sync: indicate whether we should wait for the workqueue cancelation |
Andrew Lunn | 3c293f4 | 2016-10-12 22:14:53 +0200 | [diff] [blame] | 720 | * |
| 721 | * Description: There has been a change in state which requires that the |
| 722 | * state machine runs. |
| 723 | */ |
| 724 | |
Alexander Kochetkov | f555f34 | 2017-04-20 14:00:04 +0300 | [diff] [blame] | 725 | void phy_trigger_machine(struct phy_device *phydev, bool sync) |
Andrew Lunn | 3c293f4 | 2016-10-12 22:14:53 +0200 | [diff] [blame] | 726 | { |
Florian Fainelli | eab1277 | 2017-01-20 15:31:52 -0800 | [diff] [blame] | 727 | if (sync) |
| 728 | cancel_delayed_work_sync(&phydev->state_queue); |
| 729 | else |
| 730 | cancel_delayed_work(&phydev->state_queue); |
Andrew Lunn | 3c293f4 | 2016-10-12 22:14:53 +0200 | [diff] [blame] | 731 | queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0); |
| 732 | } |
| 733 | |
| 734 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 735 | * phy_stop_machine - stop the PHY state machine tracking |
| 736 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 737 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 738 | * Description: Stops the state machine timer, sets the state to UP |
Sergei Shtylylov | 817acf5 | 2006-07-26 00:53:53 +0400 | [diff] [blame] | 739 | * (unless it wasn't up yet). This function must be called BEFORE |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 740 | * phy_detach. |
| 741 | */ |
| 742 | void phy_stop_machine(struct phy_device *phydev) |
| 743 | { |
Marcin Slusarz | a390d1f | 2009-03-13 15:41:19 -0700 | [diff] [blame] | 744 | cancel_delayed_work_sync(&phydev->state_queue); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 745 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 746 | mutex_lock(&phydev->lock); |
Nathan Sullivan | 49d52e8 | 2017-03-22 15:27:01 -0500 | [diff] [blame] | 747 | if (phydev->state > PHY_UP && phydev->state != PHY_HALTED) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 748 | phydev->state = PHY_UP; |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 749 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 750 | } |
| 751 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 752 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 753 | * phy_error - enter HALTED state for this PHY device |
| 754 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 755 | * |
| 756 | * Moves the PHY to the HALTED state in response to a read |
| 757 | * or write error, and tells the controller the link is down. |
| 758 | * Must not be called from interrupt context, or while the |
| 759 | * phydev->lock is held. |
| 760 | */ |
Andy Fleming | 9b9a8bf | 2008-05-02 13:00:51 -0500 | [diff] [blame] | 761 | static void phy_error(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 762 | { |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 763 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 764 | phydev->state = PHY_HALTED; |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 765 | mutex_unlock(&phydev->lock); |
Andrew Lunn | 3c293f4 | 2016-10-12 22:14:53 +0200 | [diff] [blame] | 766 | |
Florian Fainelli | eab1277 | 2017-01-20 15:31:52 -0800 | [diff] [blame] | 767 | phy_trigger_machine(phydev, false); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 768 | } |
| 769 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 770 | /** |
| 771 | * phy_interrupt - PHY interrupt handler |
| 772 | * @irq: interrupt line |
| 773 | * @phy_dat: phy_device pointer |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 774 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 775 | * Description: When a PHY interrupt occurs, the handler disables |
Andrew Lunn | 664fcf1 | 2016-10-16 19:56:51 +0200 | [diff] [blame] | 776 | * interrupts, and uses phy_change to handle the interrupt. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 777 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 778 | static irqreturn_t phy_interrupt(int irq, void *phy_dat) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 779 | { |
| 780 | struct phy_device *phydev = phy_dat; |
| 781 | |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 782 | if (PHY_HALTED == phydev->state) |
| 783 | return IRQ_NONE; /* It can't be ours. */ |
| 784 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 785 | disable_irq_nosync(irq); |
Maciej W. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 786 | atomic_inc(&phydev->irq_disable); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 787 | |
Andrew Lunn | 664fcf1 | 2016-10-16 19:56:51 +0200 | [diff] [blame] | 788 | phy_change(phydev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 789 | |
| 790 | return IRQ_HANDLED; |
| 791 | } |
| 792 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 793 | /** |
| 794 | * phy_enable_interrupts - Enable the interrupts from the PHY side |
| 795 | * @phydev: target phy_device struct |
| 796 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 797 | static int phy_enable_interrupts(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 798 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 799 | int err = phy_clear_interrupt(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 800 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 801 | if (err < 0) |
| 802 | return err; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 803 | |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 804 | return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 805 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 806 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 807 | /** |
| 808 | * phy_disable_interrupts - Disable the PHY interrupts from the PHY side |
| 809 | * @phydev: target phy_device struct |
| 810 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 811 | static int phy_disable_interrupts(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 812 | { |
| 813 | int err; |
| 814 | |
| 815 | /* Disable PHY interrupts */ |
| 816 | err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 817 | if (err) |
| 818 | goto phy_err; |
| 819 | |
| 820 | /* Clear the interrupt */ |
| 821 | err = phy_clear_interrupt(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 822 | if (err) |
| 823 | goto phy_err; |
| 824 | |
| 825 | return 0; |
| 826 | |
| 827 | phy_err: |
| 828 | phy_error(phydev); |
| 829 | |
| 830 | return err; |
| 831 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 832 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 833 | /** |
| 834 | * phy_start_interrupts - request and enable interrupts for a PHY device |
| 835 | * @phydev: target phy_device struct |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 836 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 837 | * Description: Request the interrupt for the given PHY. |
| 838 | * If this fails, then we set irq to PHY_POLL. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 839 | * Otherwise, we enable the interrupts in the PHY. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 840 | * This should only be called with a valid IRQ number. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 841 | * Returns 0 on success or < 0 on error. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 842 | */ |
| 843 | int phy_start_interrupts(struct phy_device *phydev) |
| 844 | { |
Maciej W. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 845 | atomic_set(&phydev->irq_disable, 0); |
Andrew Lunn | c974bdb | 2016-10-16 19:56:50 +0200 | [diff] [blame] | 846 | if (request_threaded_irq(phydev->irq, NULL, phy_interrupt, |
| 847 | IRQF_ONESHOT | IRQF_SHARED, |
Andrew Lunn | ae0219c | 2016-10-16 19:56:52 +0200 | [diff] [blame] | 848 | phydev_name(phydev), phydev) < 0) { |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 849 | pr_warn("%s: Can't get IRQ %d (PHY)\n", |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 850 | phydev->mdio.bus->name, phydev->irq); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 851 | phydev->irq = PHY_POLL; |
| 852 | return 0; |
| 853 | } |
| 854 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 855 | return phy_enable_interrupts(phydev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 856 | } |
| 857 | EXPORT_SYMBOL(phy_start_interrupts); |
| 858 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 859 | /** |
| 860 | * phy_stop_interrupts - disable interrupts from a PHY device |
| 861 | * @phydev: target phy_device struct |
| 862 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 863 | int phy_stop_interrupts(struct phy_device *phydev) |
| 864 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 865 | int err = phy_disable_interrupts(phydev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 866 | |
| 867 | if (err) |
| 868 | phy_error(phydev); |
| 869 | |
Maciej W. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 870 | free_irq(phydev->irq, phydev); |
| 871 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 872 | /* If work indeed has been cancelled, disable_irq() will have |
Maciej W. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 873 | * been left unbalanced from phy_interrupt() and enable_irq() |
| 874 | * has to be called so that other devices on the line work. |
| 875 | */ |
| 876 | while (atomic_dec_return(&phydev->irq_disable) >= 0) |
| 877 | enable_irq(phydev->irq); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 878 | |
| 879 | return err; |
| 880 | } |
| 881 | EXPORT_SYMBOL(phy_stop_interrupts); |
| 882 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 883 | /** |
Andrew Lunn | 664fcf1 | 2016-10-16 19:56:51 +0200 | [diff] [blame] | 884 | * phy_change - Called by the phy_interrupt to handle PHY changes |
| 885 | * @phydev: phy_device struct that interrupted |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 886 | */ |
Andrew Lunn | 664fcf1 | 2016-10-16 19:56:51 +0200 | [diff] [blame] | 887 | void phy_change(struct phy_device *phydev) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 888 | { |
Florian Fainelli | deccd16f9 | 2016-01-18 19:33:07 -0800 | [diff] [blame] | 889 | if (phy_interrupt_is_valid(phydev)) { |
| 890 | if (phydev->drv->did_interrupt && |
| 891 | !phydev->drv->did_interrupt(phydev)) |
| 892 | goto ignore; |
Anatolij Gustschin | a8729eb | 2009-04-07 02:01:42 +0000 | [diff] [blame] | 893 | |
Florian Fainelli | deccd16f9 | 2016-01-18 19:33:07 -0800 | [diff] [blame] | 894 | if (phy_disable_interrupts(phydev)) |
| 895 | goto phy_err; |
| 896 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 897 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 898 | mutex_lock(&phydev->lock); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 899 | if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state)) |
| 900 | phydev->state = PHY_CHANGELINK; |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 901 | mutex_unlock(&phydev->lock); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 902 | |
Florian Fainelli | deccd16f9 | 2016-01-18 19:33:07 -0800 | [diff] [blame] | 903 | if (phy_interrupt_is_valid(phydev)) { |
| 904 | atomic_dec(&phydev->irq_disable); |
| 905 | enable_irq(phydev->irq); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 906 | |
Florian Fainelli | deccd16f9 | 2016-01-18 19:33:07 -0800 | [diff] [blame] | 907 | /* Reenable interrupts */ |
| 908 | if (PHY_HALTED != phydev->state && |
| 909 | phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED)) |
| 910 | goto irq_enable_err; |
| 911 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 912 | |
Marcin Slusarz | a390d1f | 2009-03-13 15:41:19 -0700 | [diff] [blame] | 913 | /* reschedule state queue work to run as soon as possible */ |
Florian Fainelli | eab1277 | 2017-01-20 15:31:52 -0800 | [diff] [blame] | 914 | phy_trigger_machine(phydev, true); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 915 | return; |
| 916 | |
Anatolij Gustschin | a8729eb | 2009-04-07 02:01:42 +0000 | [diff] [blame] | 917 | ignore: |
| 918 | atomic_dec(&phydev->irq_disable); |
| 919 | enable_irq(phydev->irq); |
| 920 | return; |
| 921 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 922 | irq_enable_err: |
| 923 | disable_irq(phydev->irq); |
Maciej W. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 924 | atomic_inc(&phydev->irq_disable); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 925 | phy_err: |
| 926 | phy_error(phydev); |
| 927 | } |
| 928 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 929 | /** |
Andrew Lunn | 664fcf1 | 2016-10-16 19:56:51 +0200 | [diff] [blame] | 930 | * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes |
| 931 | * @work: work_struct that describes the work to be done |
| 932 | */ |
| 933 | void phy_change_work(struct work_struct *work) |
| 934 | { |
| 935 | struct phy_device *phydev = |
| 936 | container_of(work, struct phy_device, phy_queue); |
| 937 | |
| 938 | phy_change(phydev); |
| 939 | } |
| 940 | |
| 941 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 942 | * phy_stop - Bring down the PHY link, and stop checking the status |
| 943 | * @phydev: target phy_device struct |
| 944 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 945 | void phy_stop(struct phy_device *phydev) |
| 946 | { |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 947 | mutex_lock(&phydev->lock); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 948 | |
| 949 | if (PHY_HALTED == phydev->state) |
| 950 | goto out_unlock; |
| 951 | |
Florian Fainelli | 2c7b492 | 2013-05-19 22:53:42 +0000 | [diff] [blame] | 952 | if (phy_interrupt_is_valid(phydev)) { |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 953 | /* Disable PHY Interrupts */ |
| 954 | phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 955 | |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 956 | /* Clear any pending interrupts */ |
| 957 | phy_clear_interrupt(phydev); |
| 958 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 959 | |
Maciej W. Rozycki | 6daf653 | 2007-09-28 22:42:15 -0700 | [diff] [blame] | 960 | phydev->state = PHY_HALTED; |
| 961 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 962 | out_unlock: |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 963 | mutex_unlock(&phydev->lock); |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 964 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 965 | /* Cannot call flush_scheduled_work() here as desired because |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 966 | * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change() |
| 967 | * will not reenable interrupts. |
| 968 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 969 | } |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 970 | EXPORT_SYMBOL(phy_stop); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 971 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 972 | /** |
| 973 | * phy_start - start or restart a PHY device |
| 974 | * @phydev: target phy_device struct |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 975 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 976 | * Description: Indicates the attached device's readiness to |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 977 | * handle PHY-related work. Used during startup to start the |
| 978 | * PHY, and after a call to phy_stop() to resume operation. |
| 979 | * Also used to indicate the MDIO bus has cleared an error |
| 980 | * condition. |
| 981 | */ |
| 982 | void phy_start(struct phy_device *phydev) |
| 983 | { |
Tim Beale | c15e10e | 2015-05-18 15:38:38 +1200 | [diff] [blame] | 984 | bool do_resume = false; |
| 985 | int err = 0; |
| 986 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 987 | mutex_lock(&phydev->lock); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 988 | |
| 989 | switch (phydev->state) { |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 990 | case PHY_STARTING: |
| 991 | phydev->state = PHY_PENDING; |
| 992 | break; |
| 993 | case PHY_READY: |
| 994 | phydev->state = PHY_UP; |
| 995 | break; |
| 996 | case PHY_HALTED: |
Tim Beale | c15e10e | 2015-05-18 15:38:38 +1200 | [diff] [blame] | 997 | /* make sure interrupts are re-enabled for the PHY */ |
Shaohui Xie | 84a527a | 2016-05-10 17:42:26 +0800 | [diff] [blame] | 998 | if (phydev->irq != PHY_POLL) { |
| 999 | err = phy_enable_interrupts(phydev); |
| 1000 | if (err < 0) |
| 1001 | break; |
| 1002 | } |
Tim Beale | c15e10e | 2015-05-18 15:38:38 +1200 | [diff] [blame] | 1003 | |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1004 | phydev->state = PHY_RESUMING; |
Tim Beale | c15e10e | 2015-05-18 15:38:38 +1200 | [diff] [blame] | 1005 | do_resume = true; |
| 1006 | break; |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1007 | default: |
| 1008 | break; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1009 | } |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 1010 | mutex_unlock(&phydev->lock); |
Tim Beale | c15e10e | 2015-05-18 15:38:38 +1200 | [diff] [blame] | 1011 | |
| 1012 | /* if phy was suspended, bring the physical link up again */ |
| 1013 | if (do_resume) |
| 1014 | phy_resume(phydev); |
Andrew Lunn | 3c293f4 | 2016-10-12 22:14:53 +0200 | [diff] [blame] | 1015 | |
Florian Fainelli | eab1277 | 2017-01-20 15:31:52 -0800 | [diff] [blame] | 1016 | phy_trigger_machine(phydev, true); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1017 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1018 | EXPORT_SYMBOL(phy_start); |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1019 | |
Zach Brown | 61a1796 | 2016-10-17 10:49:53 -0500 | [diff] [blame] | 1020 | static void phy_adjust_link(struct phy_device *phydev) |
| 1021 | { |
| 1022 | phydev->adjust_link(phydev->attached_dev); |
Zach Brown | 2e0bc45 | 2016-10-17 10:49:55 -0500 | [diff] [blame] | 1023 | phy_led_trigger_change_speed(phydev); |
Zach Brown | 61a1796 | 2016-10-17 10:49:53 -0500 | [diff] [blame] | 1024 | } |
| 1025 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 1026 | /** |
| 1027 | * phy_state_machine - Handle the state machine |
| 1028 | * @work: work_struct that describes the work to be done |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 1029 | */ |
Anton Vorontsov | 4f9c85a | 2010-01-18 05:37:16 +0000 | [diff] [blame] | 1030 | void phy_state_machine(struct work_struct *work) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1031 | { |
Jean Delvare | bf6aede | 2009-04-02 16:56:54 -0700 | [diff] [blame] | 1032 | struct delayed_work *dwork = to_delayed_work(work); |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 1033 | struct phy_device *phydev = |
Marcin Slusarz | a390d1f | 2009-03-13 15:41:19 -0700 | [diff] [blame] | 1034 | container_of(dwork, struct phy_device, state_queue); |
Tim Beale | c15e10e | 2015-05-18 15:38:38 +1200 | [diff] [blame] | 1035 | bool needs_aneg = false, do_suspend = false; |
Florian Fainelli | 3e2186e | 2015-05-16 10:17:56 -0700 | [diff] [blame] | 1036 | enum phy_state old_state; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1037 | int err = 0; |
Shaohui Xie | 11e122c | 2015-08-14 12:23:40 +0800 | [diff] [blame] | 1038 | int old_link; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1039 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 1040 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1041 | |
Florian Fainelli | 3e2186e | 2015-05-16 10:17:56 -0700 | [diff] [blame] | 1042 | old_state = phydev->state; |
| 1043 | |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 1044 | if (phydev->drv && phydev->drv->link_change_notify) |
Daniel Mack | 2b8f2a2 | 2014-06-18 11:01:41 +0200 | [diff] [blame] | 1045 | phydev->drv->link_change_notify(phydev); |
| 1046 | |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1047 | switch (phydev->state) { |
| 1048 | case PHY_DOWN: |
| 1049 | case PHY_STARTING: |
| 1050 | case PHY_READY: |
| 1051 | case PHY_PENDING: |
| 1052 | break; |
| 1053 | case PHY_UP: |
Zhangfei Gao | 6e14a5ee | 2014-05-15 13:35:34 +0800 | [diff] [blame] | 1054 | needs_aneg = true; |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1055 | |
| 1056 | phydev->link_timeout = PHY_AN_TIMEOUT; |
| 1057 | |
| 1058 | break; |
| 1059 | case PHY_AN: |
| 1060 | err = phy_read_status(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1061 | if (err < 0) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1062 | break; |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1063 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1064 | /* If the link is down, give up on negotiation for now */ |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1065 | if (!phydev->link) { |
| 1066 | phydev->state = PHY_NOLINK; |
| 1067 | netif_carrier_off(phydev->attached_dev); |
Zach Brown | 61a1796 | 2016-10-17 10:49:53 -0500 | [diff] [blame] | 1068 | phy_adjust_link(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1069 | break; |
| 1070 | } |
| 1071 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1072 | /* Check if negotiation is done. Break if there's an error */ |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1073 | err = phy_aneg_done(phydev); |
| 1074 | if (err < 0) |
| 1075 | break; |
| 1076 | |
| 1077 | /* If AN is done, we're running */ |
| 1078 | if (err > 0) { |
| 1079 | phydev->state = PHY_RUNNING; |
| 1080 | netif_carrier_on(phydev->attached_dev); |
Zach Brown | 61a1796 | 2016-10-17 10:49:53 -0500 | [diff] [blame] | 1081 | phy_adjust_link(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1082 | |
Balakumaran Kannan | fa8cdda | 2014-04-09 09:03:45 +0530 | [diff] [blame] | 1083 | } else if (0 == phydev->link_timeout--) |
Zhangfei Gao | 6e14a5ee | 2014-05-15 13:35:34 +0800 | [diff] [blame] | 1084 | needs_aneg = true; |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1085 | break; |
| 1086 | case PHY_NOLINK: |
Andrew Lunn | 321beec | 2015-11-16 23:36:46 +0100 | [diff] [blame] | 1087 | if (phy_interrupt_is_valid(phydev)) |
| 1088 | break; |
| 1089 | |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1090 | err = phy_read_status(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1091 | if (err) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1092 | break; |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 1093 | |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1094 | if (phydev->link) { |
Balakumaran Kannan | e46e08b | 2014-04-24 08:22:47 +0530 | [diff] [blame] | 1095 | if (AUTONEG_ENABLE == phydev->autoneg) { |
| 1096 | err = phy_aneg_done(phydev); |
| 1097 | if (err < 0) |
| 1098 | break; |
| 1099 | |
| 1100 | if (!err) { |
| 1101 | phydev->state = PHY_AN; |
| 1102 | phydev->link_timeout = PHY_AN_TIMEOUT; |
| 1103 | break; |
| 1104 | } |
| 1105 | } |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1106 | phydev->state = PHY_RUNNING; |
| 1107 | netif_carrier_on(phydev->attached_dev); |
Zach Brown | 61a1796 | 2016-10-17 10:49:53 -0500 | [diff] [blame] | 1108 | phy_adjust_link(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1109 | } |
| 1110 | break; |
| 1111 | case PHY_FORCING: |
| 1112 | err = genphy_update_link(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1113 | if (err) |
| 1114 | break; |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 1115 | |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1116 | if (phydev->link) { |
| 1117 | phydev->state = PHY_RUNNING; |
| 1118 | netif_carrier_on(phydev->attached_dev); |
| 1119 | } else { |
| 1120 | if (0 == phydev->link_timeout--) |
Zhangfei Gao | 6e14a5ee | 2014-05-15 13:35:34 +0800 | [diff] [blame] | 1121 | needs_aneg = true; |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Zach Brown | 61a1796 | 2016-10-17 10:49:53 -0500 | [diff] [blame] | 1124 | phy_adjust_link(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1125 | break; |
| 1126 | case PHY_RUNNING: |
Florian Fainelli | d5c3d84 | 2016-01-18 19:33:06 -0800 | [diff] [blame] | 1127 | /* Only register a CHANGE if we are polling and link changed |
| 1128 | * since latest checking. |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1129 | */ |
Florian Fainelli | d5c3d84 | 2016-01-18 19:33:06 -0800 | [diff] [blame] | 1130 | if (phydev->irq == PHY_POLL) { |
Shaohui Xie | 11e122c | 2015-08-14 12:23:40 +0800 | [diff] [blame] | 1131 | old_link = phydev->link; |
| 1132 | err = phy_read_status(phydev); |
| 1133 | if (err) |
| 1134 | break; |
| 1135 | |
| 1136 | if (old_link != phydev->link) |
| 1137 | phydev->state = PHY_CHANGELINK; |
| 1138 | } |
Zefir Kurtisi | 811a919 | 2017-01-06 12:14:48 +0100 | [diff] [blame] | 1139 | /* |
| 1140 | * Failsafe: check that nobody set phydev->link=0 between two |
| 1141 | * poll cycles, otherwise we won't leave RUNNING state as long |
| 1142 | * as link remains down. |
| 1143 | */ |
| 1144 | if (!phydev->link && phydev->state == PHY_RUNNING) { |
| 1145 | phydev->state = PHY_CHANGELINK; |
| 1146 | phydev_err(phydev, "no link in PHY_RUNNING\n"); |
| 1147 | } |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1148 | break; |
| 1149 | case PHY_CHANGELINK: |
| 1150 | err = phy_read_status(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1151 | if (err) |
| 1152 | break; |
| 1153 | |
| 1154 | if (phydev->link) { |
| 1155 | phydev->state = PHY_RUNNING; |
| 1156 | netif_carrier_on(phydev->attached_dev); |
| 1157 | } else { |
| 1158 | phydev->state = PHY_NOLINK; |
| 1159 | netif_carrier_off(phydev->attached_dev); |
| 1160 | } |
| 1161 | |
Zach Brown | 61a1796 | 2016-10-17 10:49:53 -0500 | [diff] [blame] | 1162 | phy_adjust_link(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1163 | |
| 1164 | if (phy_interrupt_is_valid(phydev)) |
| 1165 | err = phy_config_interrupt(phydev, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1166 | PHY_INTERRUPT_ENABLED); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1167 | break; |
| 1168 | case PHY_HALTED: |
| 1169 | if (phydev->link) { |
| 1170 | phydev->link = 0; |
| 1171 | netif_carrier_off(phydev->attached_dev); |
Zach Brown | 61a1796 | 2016-10-17 10:49:53 -0500 | [diff] [blame] | 1172 | phy_adjust_link(phydev); |
Zhangfei Gao | 6e14a5ee | 2014-05-15 13:35:34 +0800 | [diff] [blame] | 1173 | do_suspend = true; |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1174 | } |
| 1175 | break; |
| 1176 | case PHY_RESUMING: |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1177 | if (AUTONEG_ENABLE == phydev->autoneg) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1178 | err = phy_aneg_done(phydev); |
| 1179 | if (err < 0) |
| 1180 | break; |
| 1181 | |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1182 | /* err > 0 if AN is done. |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1183 | * Otherwise, it's 0, and we're still waiting for AN |
| 1184 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1185 | if (err > 0) { |
Wade Farnsworth | 42caa07 | 2009-07-01 13:00:34 +0000 | [diff] [blame] | 1186 | err = phy_read_status(phydev); |
| 1187 | if (err) |
| 1188 | break; |
| 1189 | |
| 1190 | if (phydev->link) { |
| 1191 | phydev->state = PHY_RUNNING; |
| 1192 | netif_carrier_on(phydev->attached_dev); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1193 | } else { |
Wade Farnsworth | 42caa07 | 2009-07-01 13:00:34 +0000 | [diff] [blame] | 1194 | phydev->state = PHY_NOLINK; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1195 | } |
Zach Brown | 61a1796 | 2016-10-17 10:49:53 -0500 | [diff] [blame] | 1196 | phy_adjust_link(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1197 | } else { |
| 1198 | phydev->state = PHY_AN; |
| 1199 | phydev->link_timeout = PHY_AN_TIMEOUT; |
Wade Farnsworth | 42caa07 | 2009-07-01 13:00:34 +0000 | [diff] [blame] | 1200 | } |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1201 | } else { |
| 1202 | err = phy_read_status(phydev); |
| 1203 | if (err) |
| 1204 | break; |
| 1205 | |
| 1206 | if (phydev->link) { |
| 1207 | phydev->state = PHY_RUNNING; |
| 1208 | netif_carrier_on(phydev->attached_dev); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1209 | } else { |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1210 | phydev->state = PHY_NOLINK; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1211 | } |
Zach Brown | 61a1796 | 2016-10-17 10:49:53 -0500 | [diff] [blame] | 1212 | phy_adjust_link(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1213 | } |
| 1214 | break; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1215 | } |
| 1216 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 1217 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1218 | |
| 1219 | if (needs_aneg) |
Alexander Kochetkov | f555f34 | 2017-04-20 14:00:04 +0300 | [diff] [blame] | 1220 | err = phy_start_aneg_priv(phydev, false); |
Zhangfei Gao | 6e14a5ee | 2014-05-15 13:35:34 +0800 | [diff] [blame] | 1221 | else if (do_suspend) |
Sebastian Hesselbarth | be9dad1 | 2013-12-13 10:20:29 +0100 | [diff] [blame] | 1222 | phy_suspend(phydev); |
| 1223 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1224 | if (err < 0) |
| 1225 | phy_error(phydev); |
| 1226 | |
Andrew Lunn | 72ba48b | 2016-01-06 20:11:09 +0100 | [diff] [blame] | 1227 | phydev_dbg(phydev, "PHY state change %s -> %s\n", |
| 1228 | phy_state_to_str(old_state), |
| 1229 | phy_state_to_str(phydev->state)); |
Florian Fainelli | 3e2186e | 2015-05-16 10:17:56 -0700 | [diff] [blame] | 1230 | |
Florian Fainelli | d5c3d84 | 2016-01-18 19:33:06 -0800 | [diff] [blame] | 1231 | /* Only re-schedule a PHY state machine change if we are polling the |
| 1232 | * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving |
| 1233 | * between states from phy_mac_interrupt() |
| 1234 | */ |
| 1235 | if (phydev->irq == PHY_POLL) |
| 1236 | queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, |
| 1237 | PHY_STATE_TIME * HZ); |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 1238 | } |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1239 | |
Andrew Lunn | 664fcf1 | 2016-10-16 19:56:51 +0200 | [diff] [blame] | 1240 | /** |
| 1241 | * phy_mac_interrupt - MAC says the link has changed |
| 1242 | * @phydev: phy_device struct with changed link |
| 1243 | * @new_link: Link is Up/Down. |
| 1244 | * |
| 1245 | * Description: The MAC layer is able indicate there has been a change |
| 1246 | * in the PHY link status. Set the new link status, and trigger the |
| 1247 | * state machine, work a work queue. |
| 1248 | */ |
Florian Fainelli | 5ea94e7 | 2013-05-19 22:53:43 +0000 | [diff] [blame] | 1249 | void phy_mac_interrupt(struct phy_device *phydev, int new_link) |
| 1250 | { |
Florian Fainelli | 5ea94e7 | 2013-05-19 22:53:43 +0000 | [diff] [blame] | 1251 | phydev->link = new_link; |
Florian Fainelli | deccd16f9 | 2016-01-18 19:33:07 -0800 | [diff] [blame] | 1252 | |
| 1253 | /* Trigger a state machine change */ |
| 1254 | queue_work(system_power_efficient_wq, &phydev->phy_queue); |
Florian Fainelli | 5ea94e7 | 2013-05-19 22:53:43 +0000 | [diff] [blame] | 1255 | } |
| 1256 | EXPORT_SYMBOL(phy_mac_interrupt); |
| 1257 | |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1258 | /** |
| 1259 | * phy_init_eee - init and check the EEE feature |
| 1260 | * @phydev: target phy_device struct |
| 1261 | * @clk_stop_enable: PHY may stop the clock during LPI |
| 1262 | * |
| 1263 | * Description: it checks if the Energy-Efficient Ethernet (EEE) |
| 1264 | * is supported by looking at the MMD registers 3.20 and 7.60/61 |
| 1265 | * and it programs the MMD register 3.0 setting the "Clock stop enable" |
| 1266 | * bit if required. |
| 1267 | */ |
| 1268 | int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable) |
| 1269 | { |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 1270 | if (!phydev->drv) |
| 1271 | return -EIO; |
| 1272 | |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1273 | /* According to 802.3az,the EEE is supported only in full duplex-mode. |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1274 | */ |
Russell King | 32d7514 | 2017-03-31 10:37:18 +0100 | [diff] [blame] | 1275 | if (phydev->duplex == DUPLEX_FULL) { |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1276 | int eee_lp, eee_cap, eee_adv; |
| 1277 | u32 lp, cap, adv; |
Bjorn Helgaas | 4ae6e50 | 2014-03-04 17:35:44 -0700 | [diff] [blame] | 1278 | int status; |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1279 | |
| 1280 | /* Read phy status to properly get the right settings */ |
| 1281 | status = phy_read_status(phydev); |
| 1282 | if (status) |
| 1283 | return status; |
| 1284 | |
| 1285 | /* First check if the EEE ability is supported */ |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 1286 | eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); |
Giuseppe CAVALLARO | 7a4cecf | 2014-08-26 09:26:52 +0200 | [diff] [blame] | 1287 | if (eee_cap <= 0) |
| 1288 | goto eee_exit_err; |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1289 | |
Allan, Bruce W | b32607d | 2012-08-20 04:55:29 +0000 | [diff] [blame] | 1290 | cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1291 | if (!cap) |
Giuseppe CAVALLARO | 7a4cecf | 2014-08-26 09:26:52 +0200 | [diff] [blame] | 1292 | goto eee_exit_err; |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1293 | |
| 1294 | /* Check which link settings negotiated and verify it in |
| 1295 | * the EEE advertising registers. |
| 1296 | */ |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 1297 | eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); |
Giuseppe CAVALLARO | 7a4cecf | 2014-08-26 09:26:52 +0200 | [diff] [blame] | 1298 | if (eee_lp <= 0) |
| 1299 | goto eee_exit_err; |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1300 | |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 1301 | eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV); |
Giuseppe CAVALLARO | 7a4cecf | 2014-08-26 09:26:52 +0200 | [diff] [blame] | 1302 | if (eee_adv <= 0) |
| 1303 | goto eee_exit_err; |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1304 | |
Allan, Bruce W | b32607d | 2012-08-20 04:55:29 +0000 | [diff] [blame] | 1305 | adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv); |
| 1306 | lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp); |
Guenter Roeck | 54da5a8 | 2015-02-17 09:36:22 -0800 | [diff] [blame] | 1307 | if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv)) |
Giuseppe CAVALLARO | 7a4cecf | 2014-08-26 09:26:52 +0200 | [diff] [blame] | 1308 | goto eee_exit_err; |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1309 | |
| 1310 | if (clk_stop_enable) { |
| 1311 | /* Configure the PHY to stop receiving xMII |
| 1312 | * clock while it is signaling LPI. |
| 1313 | */ |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 1314 | int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1315 | if (val < 0) |
| 1316 | return val; |
| 1317 | |
| 1318 | val |= MDIO_PCS_CTRL1_CLKSTOP_EN; |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 1319 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 1322 | return 0; /* EEE supported */ |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1323 | } |
Giuseppe CAVALLARO | 7a4cecf | 2014-08-26 09:26:52 +0200 | [diff] [blame] | 1324 | eee_exit_err: |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 1325 | return -EPROTONOSUPPORT; |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1326 | } |
| 1327 | EXPORT_SYMBOL(phy_init_eee); |
| 1328 | |
| 1329 | /** |
| 1330 | * phy_get_eee_err - report the EEE wake error count |
| 1331 | * @phydev: target phy_device struct |
| 1332 | * |
| 1333 | * Description: it is to report the number of time where the PHY |
| 1334 | * failed to complete its normal wake sequence. |
| 1335 | */ |
| 1336 | int phy_get_eee_err(struct phy_device *phydev) |
| 1337 | { |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 1338 | if (!phydev->drv) |
| 1339 | return -EIO; |
| 1340 | |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 1341 | return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1342 | } |
| 1343 | EXPORT_SYMBOL(phy_get_eee_err); |
| 1344 | |
| 1345 | /** |
| 1346 | * phy_ethtool_get_eee - get EEE supported and status |
| 1347 | * @phydev: target phy_device struct |
| 1348 | * @data: ethtool_eee data |
| 1349 | * |
| 1350 | * Description: it reportes the Supported/Advertisement/LP Advertisement |
| 1351 | * capabilities. |
| 1352 | */ |
| 1353 | int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data) |
| 1354 | { |
| 1355 | int val; |
| 1356 | |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 1357 | if (!phydev->drv) |
| 1358 | return -EIO; |
| 1359 | |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1360 | /* Get Supported EEE */ |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 1361 | val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1362 | if (val < 0) |
| 1363 | return val; |
Allan, Bruce W | b32607d | 2012-08-20 04:55:29 +0000 | [diff] [blame] | 1364 | data->supported = mmd_eee_cap_to_ethtool_sup_t(val); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1365 | |
| 1366 | /* Get advertisement EEE */ |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 1367 | val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1368 | if (val < 0) |
| 1369 | return val; |
Allan, Bruce W | b32607d | 2012-08-20 04:55:29 +0000 | [diff] [blame] | 1370 | data->advertised = mmd_eee_adv_to_ethtool_adv_t(val); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1371 | |
| 1372 | /* Get LP advertisement EEE */ |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 1373 | val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1374 | if (val < 0) |
| 1375 | return val; |
Allan, Bruce W | b32607d | 2012-08-20 04:55:29 +0000 | [diff] [blame] | 1376 | data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1377 | |
| 1378 | return 0; |
| 1379 | } |
| 1380 | EXPORT_SYMBOL(phy_ethtool_get_eee); |
| 1381 | |
| 1382 | /** |
| 1383 | * phy_ethtool_set_eee - set EEE supported and status |
| 1384 | * @phydev: target phy_device struct |
| 1385 | * @data: ethtool_eee data |
| 1386 | * |
| 1387 | * Description: it is to program the Advertisement EEE register. |
| 1388 | */ |
| 1389 | int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data) |
| 1390 | { |
Russell King | f75abeb | 2017-03-31 10:37:12 +0100 | [diff] [blame] | 1391 | int cap, old_adv, adv, ret; |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1392 | |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 1393 | if (!phydev->drv) |
| 1394 | return -EIO; |
| 1395 | |
Russell King | 83ea067 | 2017-03-31 10:37:07 +0100 | [diff] [blame] | 1396 | /* Get Supported EEE */ |
| 1397 | cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); |
| 1398 | if (cap < 0) |
| 1399 | return cap; |
| 1400 | |
Russell King | f75abeb | 2017-03-31 10:37:12 +0100 | [diff] [blame] | 1401 | old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV); |
| 1402 | if (old_adv < 0) |
| 1403 | return old_adv; |
| 1404 | |
Russell King | 83ea067 | 2017-03-31 10:37:07 +0100 | [diff] [blame] | 1405 | adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap; |
| 1406 | |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1407 | /* Mask prohibited EEE modes */ |
Russell King | 83ea067 | 2017-03-31 10:37:07 +0100 | [diff] [blame] | 1408 | adv &= ~phydev->eee_broken_modes; |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1409 | |
Russell King | f75abeb | 2017-03-31 10:37:12 +0100 | [diff] [blame] | 1410 | if (old_adv != adv) { |
| 1411 | ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv); |
| 1412 | if (ret < 0) |
| 1413 | return ret; |
| 1414 | |
| 1415 | /* Restart autonegotiation so the new modes get sent to the |
| 1416 | * link partner. |
| 1417 | */ |
| 1418 | ret = genphy_restart_aneg(phydev); |
| 1419 | if (ret < 0) |
| 1420 | return ret; |
| 1421 | } |
| 1422 | |
| 1423 | return 0; |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1424 | } |
| 1425 | EXPORT_SYMBOL(phy_ethtool_set_eee); |
Michael Stapelberg | 42e836e | 2013-03-11 13:56:44 +0000 | [diff] [blame] | 1426 | |
| 1427 | int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol) |
| 1428 | { |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 1429 | if (phydev->drv && phydev->drv->set_wol) |
Michael Stapelberg | 42e836e | 2013-03-11 13:56:44 +0000 | [diff] [blame] | 1430 | return phydev->drv->set_wol(phydev, wol); |
| 1431 | |
| 1432 | return -EOPNOTSUPP; |
| 1433 | } |
| 1434 | EXPORT_SYMBOL(phy_ethtool_set_wol); |
| 1435 | |
| 1436 | void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol) |
| 1437 | { |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 1438 | if (phydev->drv && phydev->drv->get_wol) |
Michael Stapelberg | 42e836e | 2013-03-11 13:56:44 +0000 | [diff] [blame] | 1439 | phydev->drv->get_wol(phydev, wol); |
| 1440 | } |
| 1441 | EXPORT_SYMBOL(phy_ethtool_get_wol); |
Philippe Reynes | 9d9a77c | 2016-05-10 00:19:41 +0200 | [diff] [blame] | 1442 | |
| 1443 | int phy_ethtool_get_link_ksettings(struct net_device *ndev, |
| 1444 | struct ethtool_link_ksettings *cmd) |
| 1445 | { |
| 1446 | struct phy_device *phydev = ndev->phydev; |
| 1447 | |
| 1448 | if (!phydev) |
| 1449 | return -ENODEV; |
| 1450 | |
| 1451 | return phy_ethtool_ksettings_get(phydev, cmd); |
| 1452 | } |
| 1453 | EXPORT_SYMBOL(phy_ethtool_get_link_ksettings); |
| 1454 | |
| 1455 | int phy_ethtool_set_link_ksettings(struct net_device *ndev, |
| 1456 | const struct ethtool_link_ksettings *cmd) |
| 1457 | { |
| 1458 | struct phy_device *phydev = ndev->phydev; |
| 1459 | |
| 1460 | if (!phydev) |
| 1461 | return -ENODEV; |
| 1462 | |
| 1463 | return phy_ethtool_ksettings_set(phydev, cmd); |
| 1464 | } |
| 1465 | EXPORT_SYMBOL(phy_ethtool_set_link_ksettings); |
Florian Fainelli | e86a898 | 2016-11-15 10:06:30 -0800 | [diff] [blame] | 1466 | |
| 1467 | int phy_ethtool_nway_reset(struct net_device *ndev) |
| 1468 | { |
| 1469 | struct phy_device *phydev = ndev->phydev; |
| 1470 | |
| 1471 | if (!phydev) |
| 1472 | return -ENODEV; |
| 1473 | |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 1474 | if (!phydev->drv) |
| 1475 | return -EIO; |
| 1476 | |
Florian Fainelli | e86a898 | 2016-11-15 10:06:30 -0800 | [diff] [blame] | 1477 | return genphy_restart_aneg(phydev); |
| 1478 | } |
| 1479 | EXPORT_SYMBOL(phy_ethtool_nway_reset); |