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> |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 32 | #include <linux/timer.h> |
| 33 | #include <linux/workqueue.h> |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 34 | #include <linux/mdio.h> |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 35 | #include <linux/io.h> |
| 36 | #include <linux/uaccess.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 37 | #include <linux/atomic.h> |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 38 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 39 | #include <asm/irq.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 40 | |
Florian Fainelli | 766d1d3 | 2014-02-11 17:27:35 -0800 | [diff] [blame^] | 41 | static 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 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 61 | /** |
| 62 | * phy_print_status - Convenience function to print out the current phy status |
| 63 | * @phydev: the phy_device struct |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 64 | */ |
| 65 | void phy_print_status(struct phy_device *phydev) |
| 66 | { |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 67 | if (phydev->link) { |
Florian Fainelli | df40cc8 | 2014-02-11 17:27:34 -0800 | [diff] [blame] | 68 | netdev_info(phydev->attached_dev, |
Florian Fainelli | 766d1d3 | 2014-02-11 17:27:35 -0800 | [diff] [blame^] | 69 | "Link is Up - %s/%s - flow control %s\n", |
| 70 | phy_speed_to_str(phydev->speed), |
Florian Fainelli | df40cc8 | 2014-02-11 17:27:34 -0800 | [diff] [blame] | 71 | DUPLEX_FULL == phydev->duplex ? "Full" : "Half", |
| 72 | phydev->pause ? "rx/tx" : "off"); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 73 | } else { |
Florian Fainelli | 43b6329 | 2014-02-11 17:27:33 -0800 | [diff] [blame] | 74 | netdev_info(phydev->attached_dev, "Link is Down\n"); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 75 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 76 | } |
| 77 | EXPORT_SYMBOL(phy_print_status); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 78 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 79 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 80 | * phy_clear_interrupt - Ack the phy device's interrupt |
| 81 | * @phydev: the phy_device struct |
| 82 | * |
| 83 | * If the @phydev driver has an ack_interrupt function, call it to |
| 84 | * ack and clear the phy device's interrupt. |
| 85 | * |
| 86 | * Returns 0 on success on < 0 on error. |
| 87 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 88 | static int phy_clear_interrupt(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 89 | { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 90 | if (phydev->drv->ack_interrupt) |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 91 | return phydev->drv->ack_interrupt(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 92 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 93 | return 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 96 | /** |
| 97 | * phy_config_interrupt - configure the PHY device for the requested interrupts |
| 98 | * @phydev: the phy_device struct |
| 99 | * @interrupts: interrupt flags to configure for this @phydev |
| 100 | * |
| 101 | * Returns 0 on success on < 0 on error. |
| 102 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 103 | static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 104 | { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 105 | phydev->interrupts = interrupts; |
| 106 | if (phydev->drv->config_intr) |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 107 | return phydev->drv->config_intr(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 108 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 109 | return 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 113 | /** |
| 114 | * phy_aneg_done - return auto-negotiation status |
| 115 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 116 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 117 | * Description: Reads the status register and returns 0 either if |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 118 | * auto-negotiation is incomplete, or if there was an error. |
| 119 | * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. |
| 120 | */ |
| 121 | static inline int phy_aneg_done(struct phy_device *phydev) |
| 122 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 123 | int retval = phy_read(phydev, MII_BMSR); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 124 | |
| 125 | return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); |
| 126 | } |
| 127 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 128 | /* A structure for mapping a particular speed and duplex |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 129 | * combination to a particular SUPPORTED and ADVERTISED value |
| 130 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 131 | struct phy_setting { |
| 132 | int speed; |
| 133 | int duplex; |
| 134 | u32 setting; |
| 135 | }; |
| 136 | |
| 137 | /* A mapping of all SUPPORTED settings to speed/duplex */ |
Arjan van de Ven | f71e130 | 2006-03-03 21:33:57 -0500 | [diff] [blame] | 138 | static const struct phy_setting settings[] = { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 139 | { |
| 140 | .speed = 10000, |
| 141 | .duplex = DUPLEX_FULL, |
| 142 | .setting = SUPPORTED_10000baseT_Full, |
| 143 | }, |
| 144 | { |
| 145 | .speed = SPEED_1000, |
| 146 | .duplex = DUPLEX_FULL, |
| 147 | .setting = SUPPORTED_1000baseT_Full, |
| 148 | }, |
| 149 | { |
| 150 | .speed = SPEED_1000, |
| 151 | .duplex = DUPLEX_HALF, |
| 152 | .setting = SUPPORTED_1000baseT_Half, |
| 153 | }, |
| 154 | { |
| 155 | .speed = SPEED_100, |
| 156 | .duplex = DUPLEX_FULL, |
| 157 | .setting = SUPPORTED_100baseT_Full, |
| 158 | }, |
| 159 | { |
| 160 | .speed = SPEED_100, |
| 161 | .duplex = DUPLEX_HALF, |
| 162 | .setting = SUPPORTED_100baseT_Half, |
| 163 | }, |
| 164 | { |
| 165 | .speed = SPEED_10, |
| 166 | .duplex = DUPLEX_FULL, |
| 167 | .setting = SUPPORTED_10baseT_Full, |
| 168 | }, |
| 169 | { |
| 170 | .speed = SPEED_10, |
| 171 | .duplex = DUPLEX_HALF, |
| 172 | .setting = SUPPORTED_10baseT_Half, |
| 173 | }, |
| 174 | }; |
| 175 | |
Denis Cheng | ff8ac60 | 2007-09-02 18:30:18 +0800 | [diff] [blame] | 176 | #define MAX_NUM_SETTINGS ARRAY_SIZE(settings) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 177 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 178 | /** |
| 179 | * phy_find_setting - find a PHY settings array entry that matches speed & duplex |
| 180 | * @speed: speed to match |
| 181 | * @duplex: duplex to match |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 182 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 183 | * Description: Searches the settings array for the setting which |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 184 | * matches the desired speed and duplex, and returns the index |
| 185 | * of that setting. Returns the index of the last setting if |
| 186 | * none of the others match. |
| 187 | */ |
| 188 | static inline int phy_find_setting(int speed, int duplex) |
| 189 | { |
| 190 | int idx = 0; |
| 191 | |
| 192 | while (idx < ARRAY_SIZE(settings) && |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 193 | (settings[idx].speed != speed || settings[idx].duplex != duplex)) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 194 | idx++; |
| 195 | |
| 196 | return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1; |
| 197 | } |
| 198 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 199 | /** |
| 200 | * phy_find_valid - find a PHY setting that matches the requested features mask |
| 201 | * @idx: The first index in settings[] to search |
| 202 | * @features: A mask of the valid settings |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 203 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 204 | * Description: Returns the index of the first valid setting less |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 205 | * than or equal to the one pointed to by idx, as determined by |
| 206 | * the mask in features. Returns the index of the last setting |
| 207 | * if nothing else matches. |
| 208 | */ |
| 209 | static inline int phy_find_valid(int idx, u32 features) |
| 210 | { |
| 211 | while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features)) |
| 212 | idx++; |
| 213 | |
| 214 | return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1; |
| 215 | } |
| 216 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 217 | /** |
| 218 | * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex |
| 219 | * @phydev: the target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 220 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 221 | * Description: Make sure the PHY is set to supported speeds and |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 222 | * duplexes. Drop down by one in this order: 1000/FULL, |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 223 | * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 224 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 225 | static void phy_sanitize_settings(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 226 | { |
| 227 | u32 features = phydev->supported; |
| 228 | int idx; |
| 229 | |
| 230 | /* Sanitize settings based on PHY capabilities */ |
| 231 | if ((features & SUPPORTED_Autoneg) == 0) |
Domen Puncer | 163642a | 2007-08-07 12:12:41 +0200 | [diff] [blame] | 232 | phydev->autoneg = AUTONEG_DISABLE; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 233 | |
| 234 | idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex), |
| 235 | features); |
| 236 | |
| 237 | phydev->speed = settings[idx].speed; |
| 238 | phydev->duplex = settings[idx].duplex; |
| 239 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 240 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 241 | /** |
| 242 | * phy_ethtool_sset - generic ethtool sset function, handles all the details |
| 243 | * @phydev: target phy_device struct |
| 244 | * @cmd: ethtool_cmd |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 245 | * |
| 246 | * A few notes about parameter checking: |
| 247 | * - We don't set port or transceiver, so we don't care what they |
| 248 | * were set to. |
| 249 | * - phy_start_aneg() will make sure forced settings are sane, and |
| 250 | * 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] | 251 | * care if ethtool tries to give us bad values. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 252 | */ |
| 253 | int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd) |
| 254 | { |
David Decotigny | 25db033 | 2011-04-27 18:32:39 +0000 | [diff] [blame] | 255 | u32 speed = ethtool_cmd_speed(cmd); |
| 256 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 257 | if (cmd->phy_address != phydev->addr) |
| 258 | return -EINVAL; |
| 259 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 260 | /* 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] | 261 | cmd->advertising &= phydev->supported; |
| 262 | |
| 263 | /* Verify the settings we care about. */ |
| 264 | if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE) |
| 265 | return -EINVAL; |
| 266 | |
| 267 | if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0) |
| 268 | return -EINVAL; |
| 269 | |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 270 | if (cmd->autoneg == AUTONEG_DISABLE && |
David Decotigny | 25db033 | 2011-04-27 18:32:39 +0000 | [diff] [blame] | 271 | ((speed != SPEED_1000 && |
| 272 | speed != SPEED_100 && |
| 273 | speed != SPEED_10) || |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 274 | (cmd->duplex != DUPLEX_HALF && |
| 275 | cmd->duplex != DUPLEX_FULL))) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 276 | return -EINVAL; |
| 277 | |
| 278 | phydev->autoneg = cmd->autoneg; |
| 279 | |
David Decotigny | 25db033 | 2011-04-27 18:32:39 +0000 | [diff] [blame] | 280 | phydev->speed = speed; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 281 | |
| 282 | phydev->advertising = cmd->advertising; |
| 283 | |
| 284 | if (AUTONEG_ENABLE == cmd->autoneg) |
| 285 | phydev->advertising |= ADVERTISED_Autoneg; |
| 286 | else |
| 287 | phydev->advertising &= ~ADVERTISED_Autoneg; |
| 288 | |
| 289 | phydev->duplex = cmd->duplex; |
| 290 | |
| 291 | /* Restart the PHY */ |
| 292 | phy_start_aneg(phydev); |
| 293 | |
| 294 | return 0; |
| 295 | } |
Kumar Gala | 9f6d55d | 2007-01-20 16:38:26 -0600 | [diff] [blame] | 296 | EXPORT_SYMBOL(phy_ethtool_sset); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 297 | |
| 298 | int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd) |
| 299 | { |
| 300 | cmd->supported = phydev->supported; |
| 301 | |
| 302 | cmd->advertising = phydev->advertising; |
Florian Fainelli | 114002b | 2013-12-06 13:01:30 -0800 | [diff] [blame] | 303 | cmd->lp_advertising = phydev->lp_advertising; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 304 | |
David Decotigny | 7073949 | 2011-04-27 18:32:40 +0000 | [diff] [blame] | 305 | ethtool_cmd_speed_set(cmd, phydev->speed); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 306 | cmd->duplex = phydev->duplex; |
| 307 | cmd->port = PORT_MII; |
| 308 | cmd->phy_address = phydev->addr; |
Florian Fainelli | 4284b6a | 2013-05-23 01:11:12 +0000 | [diff] [blame] | 309 | cmd->transceiver = phy_is_internal(phydev) ? |
| 310 | XCVR_INTERNAL : XCVR_EXTERNAL; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 311 | cmd->autoneg = phydev->autoneg; |
| 312 | |
| 313 | return 0; |
| 314 | } |
Kumar Gala | 9f6d55d | 2007-01-20 16:38:26 -0600 | [diff] [blame] | 315 | EXPORT_SYMBOL(phy_ethtool_gset); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 316 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 317 | /** |
| 318 | * phy_mii_ioctl - generic PHY MII ioctl interface |
| 319 | * @phydev: the phy_device struct |
Randy Dunlap | 00c7d92 | 2010-08-09 13:41:59 +0000 | [diff] [blame] | 320 | * @ifr: &struct ifreq for socket ioctl's |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 321 | * @cmd: ioctl cmd to execute |
| 322 | * |
| 323 | * Note that this function is currently incompatible with the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 324 | * PHYCONTROL layer. It changes registers without regard to |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 325 | * current state. Use at own risk. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 326 | */ |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 327 | 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] | 328 | { |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 329 | struct mii_ioctl_data *mii_data = if_mii(ifr); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 330 | u16 val = mii_data->val_in; |
| 331 | |
| 332 | switch (cmd) { |
| 333 | case SIOCGMIIPHY: |
| 334 | mii_data->phy_id = phydev->addr; |
Lennert Buytenhek | c6d6a51 | 2008-09-18 03:06:52 +0000 | [diff] [blame] | 335 | /* fall through */ |
| 336 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 337 | case SIOCGMIIREG: |
Peter Korsgaard | af1dc13 | 2011-03-10 06:52:13 +0000 | [diff] [blame] | 338 | mii_data->val_out = mdiobus_read(phydev->bus, mii_data->phy_id, |
| 339 | mii_data->reg_num); |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 340 | return 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 341 | |
| 342 | case SIOCSMIIREG: |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 343 | if (mii_data->phy_id == phydev->addr) { |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 344 | switch (mii_data->reg_num) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 345 | case MII_BMCR: |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 346 | if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 347 | phydev->autoneg = AUTONEG_DISABLE; |
| 348 | else |
| 349 | phydev->autoneg = AUTONEG_ENABLE; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 350 | if (!phydev->autoneg && (val & BMCR_FULLDPLX)) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 351 | phydev->duplex = DUPLEX_FULL; |
| 352 | else |
| 353 | phydev->duplex = DUPLEX_HALF; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 354 | if (!phydev->autoneg && (val & BMCR_SPEED1000)) |
Shan Lu | 024a0a3 | 2007-03-06 02:42:03 -0800 | [diff] [blame] | 355 | phydev->speed = SPEED_1000; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 356 | else if (!phydev->autoneg && |
| 357 | (val & BMCR_SPEED100)) |
Shan Lu | 024a0a3 | 2007-03-06 02:42:03 -0800 | [diff] [blame] | 358 | phydev->speed = SPEED_100; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 359 | break; |
| 360 | case MII_ADVERTISE: |
| 361 | phydev->advertising = val; |
| 362 | break; |
| 363 | default: |
| 364 | /* do nothing */ |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
Peter Korsgaard | af1dc13 | 2011-03-10 06:52:13 +0000 | [diff] [blame] | 369 | mdiobus_write(phydev->bus, mii_data->phy_id, |
| 370 | mii_data->reg_num, val); |
| 371 | |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 372 | if (mii_data->reg_num == MII_BMCR && |
Florian Fainelli | 2613f95 | 2013-12-06 13:01:31 -0800 | [diff] [blame] | 373 | val & BMCR_RESET) |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 374 | return phy_init_hw(phydev); |
| 375 | return 0; |
David Woodhouse | dda93b4 | 2007-11-28 19:56:34 +0000 | [diff] [blame] | 376 | |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 377 | case SIOCSHWTSTAMP: |
| 378 | if (phydev->drv->hwtstamp) |
| 379 | return phydev->drv->hwtstamp(phydev, ifr); |
| 380 | /* fall through */ |
| 381 | |
David Woodhouse | dda93b4 | 2007-11-28 19:56:34 +0000 | [diff] [blame] | 382 | default: |
Lennert Buytenhek | c6d6a51 | 2008-09-18 03:06:52 +0000 | [diff] [blame] | 383 | return -EOPNOTSUPP; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 384 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 385 | } |
Domen Puncer | 680e9fe | 2007-09-17 22:21:40 +0200 | [diff] [blame] | 386 | EXPORT_SYMBOL(phy_mii_ioctl); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 387 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 388 | /** |
| 389 | * phy_start_aneg - start auto-negotiation for this PHY device |
| 390 | * @phydev: the phy_device struct |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 391 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 392 | * Description: Sanitizes the settings (if we're not autonegotiating |
| 393 | * them), and then calls the driver's config_aneg function. |
| 394 | * If the PHYCONTROL Layer is operating, we change the state to |
| 395 | * reflect the beginning of Auto-negotiation or forcing. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 396 | */ |
| 397 | int phy_start_aneg(struct phy_device *phydev) |
| 398 | { |
| 399 | int err; |
| 400 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 401 | mutex_lock(&phydev->lock); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 402 | |
| 403 | if (AUTONEG_DISABLE == phydev->autoneg) |
| 404 | phy_sanitize_settings(phydev); |
| 405 | |
| 406 | err = phydev->drv->config_aneg(phydev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 407 | if (err < 0) |
| 408 | goto out_unlock; |
| 409 | |
| 410 | if (phydev->state != PHY_HALTED) { |
| 411 | if (AUTONEG_ENABLE == phydev->autoneg) { |
| 412 | phydev->state = PHY_AN; |
| 413 | phydev->link_timeout = PHY_AN_TIMEOUT; |
| 414 | } else { |
| 415 | phydev->state = PHY_FORCING; |
| 416 | phydev->link_timeout = PHY_FORCE_TIMEOUT; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | out_unlock: |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 421 | mutex_unlock(&phydev->lock); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 422 | return err; |
| 423 | } |
| 424 | EXPORT_SYMBOL(phy_start_aneg); |
| 425 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 426 | /** |
| 427 | * phy_start_machine - start PHY state machine tracking |
| 428 | * @phydev: the phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 429 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 430 | * Description: The PHY infrastructure can run a state machine |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 431 | * which tracks whether the PHY is starting up, negotiating, |
| 432 | * etc. This function starts the timer which tracks the state |
Sergei Shtylyov | 29935ae | 2014-01-05 03:27:17 +0300 | [diff] [blame] | 433 | * of the PHY. If you want to maintain your own state machine, |
| 434 | * do not call this function. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 435 | */ |
Sergei Shtylyov | 29935ae | 2014-01-05 03:27:17 +0300 | [diff] [blame] | 436 | void phy_start_machine(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 437 | { |
Viresh Kumar | bbb47bd | 2013-04-24 17:12:55 +0530 | [diff] [blame] | 438 | queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 439 | } |
| 440 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 441 | /** |
| 442 | * phy_stop_machine - stop the PHY state machine tracking |
| 443 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 444 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 445 | * Description: Stops the state machine timer, sets the state to UP |
Sergei Shtylylov | 817acf5 | 2006-07-26 00:53:53 +0400 | [diff] [blame] | 446 | * (unless it wasn't up yet). This function must be called BEFORE |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 447 | * phy_detach. |
| 448 | */ |
| 449 | void phy_stop_machine(struct phy_device *phydev) |
| 450 | { |
Marcin Slusarz | a390d1f | 2009-03-13 15:41:19 -0700 | [diff] [blame] | 451 | cancel_delayed_work_sync(&phydev->state_queue); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 452 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 453 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 454 | if (phydev->state > PHY_UP) |
| 455 | phydev->state = PHY_UP; |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 456 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 457 | } |
| 458 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 459 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 460 | * phy_error - enter HALTED state for this PHY device |
| 461 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 462 | * |
| 463 | * Moves the PHY to the HALTED state in response to a read |
| 464 | * or write error, and tells the controller the link is down. |
| 465 | * Must not be called from interrupt context, or while the |
| 466 | * phydev->lock is held. |
| 467 | */ |
Andy Fleming | 9b9a8bf | 2008-05-02 13:00:51 -0500 | [diff] [blame] | 468 | static void phy_error(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 469 | { |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 470 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 471 | phydev->state = PHY_HALTED; |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 472 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 473 | } |
| 474 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 475 | /** |
| 476 | * phy_interrupt - PHY interrupt handler |
| 477 | * @irq: interrupt line |
| 478 | * @phy_dat: phy_device pointer |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 479 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 480 | * Description: When a PHY interrupt occurs, the handler disables |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 481 | * interrupts, and schedules a work task to clear the interrupt. |
| 482 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 483 | static irqreturn_t phy_interrupt(int irq, void *phy_dat) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 484 | { |
| 485 | struct phy_device *phydev = phy_dat; |
| 486 | |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 487 | if (PHY_HALTED == phydev->state) |
| 488 | return IRQ_NONE; /* It can't be ours. */ |
| 489 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 490 | /* The MDIO bus is not allowed to be written in interrupt |
| 491 | * context, so we need to disable the irq here. A work |
| 492 | * queue will write the PHY to disable and clear the |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 493 | * interrupt, and then reenable the irq line. |
| 494 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 495 | disable_irq_nosync(irq); |
Maciej W. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 496 | atomic_inc(&phydev->irq_disable); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 497 | |
Viresh Kumar | bbb47bd | 2013-04-24 17:12:55 +0530 | [diff] [blame] | 498 | queue_work(system_power_efficient_wq, &phydev->phy_queue); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 499 | |
| 500 | return IRQ_HANDLED; |
| 501 | } |
| 502 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 503 | /** |
| 504 | * phy_enable_interrupts - Enable the interrupts from the PHY side |
| 505 | * @phydev: target phy_device struct |
| 506 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 507 | static int phy_enable_interrupts(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 508 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 509 | int err = phy_clear_interrupt(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 510 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 511 | if (err < 0) |
| 512 | return err; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 513 | |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 514 | return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 515 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 516 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 517 | /** |
| 518 | * phy_disable_interrupts - Disable the PHY interrupts from the PHY side |
| 519 | * @phydev: target phy_device struct |
| 520 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 521 | static int phy_disable_interrupts(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 522 | { |
| 523 | int err; |
| 524 | |
| 525 | /* Disable PHY interrupts */ |
| 526 | err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 527 | if (err) |
| 528 | goto phy_err; |
| 529 | |
| 530 | /* Clear the interrupt */ |
| 531 | err = phy_clear_interrupt(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 532 | if (err) |
| 533 | goto phy_err; |
| 534 | |
| 535 | return 0; |
| 536 | |
| 537 | phy_err: |
| 538 | phy_error(phydev); |
| 539 | |
| 540 | return err; |
| 541 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 542 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 543 | /** |
| 544 | * phy_start_interrupts - request and enable interrupts for a PHY device |
| 545 | * @phydev: target phy_device struct |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 546 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 547 | * Description: Request the interrupt for the given PHY. |
| 548 | * If this fails, then we set irq to PHY_POLL. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 549 | * Otherwise, we enable the interrupts in the PHY. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 550 | * This should only be called with a valid IRQ number. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 551 | * Returns 0 on success or < 0 on error. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 552 | */ |
| 553 | int phy_start_interrupts(struct phy_device *phydev) |
| 554 | { |
Maciej W. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 555 | atomic_set(&phydev->irq_disable, 0); |
Sergei Shtylyov | 33c133c | 2013-12-20 22:09:04 +0300 | [diff] [blame] | 556 | if (request_irq(phydev->irq, phy_interrupt, 0, "phy_interrupt", |
| 557 | phydev) < 0) { |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 558 | pr_warn("%s: Can't get IRQ %d (PHY)\n", |
| 559 | phydev->bus->name, phydev->irq); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 560 | phydev->irq = PHY_POLL; |
| 561 | return 0; |
| 562 | } |
| 563 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 564 | return phy_enable_interrupts(phydev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 565 | } |
| 566 | EXPORT_SYMBOL(phy_start_interrupts); |
| 567 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 568 | /** |
| 569 | * phy_stop_interrupts - disable interrupts from a PHY device |
| 570 | * @phydev: target phy_device struct |
| 571 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 572 | int phy_stop_interrupts(struct phy_device *phydev) |
| 573 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 574 | int err = phy_disable_interrupts(phydev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 575 | |
| 576 | if (err) |
| 577 | phy_error(phydev); |
| 578 | |
Maciej W. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 579 | free_irq(phydev->irq, phydev); |
| 580 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 581 | /* Cannot call flush_scheduled_work() here as desired because |
Maciej W. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 582 | * of rtnl_lock(), but we do not really care about what would |
| 583 | * be done, except from enable_irq(), so cancel any work |
| 584 | * possibly pending and take care of the matter below. |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 585 | */ |
Oleg Nesterov | 28e53bd | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 586 | cancel_work_sync(&phydev->phy_queue); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 587 | /* If work indeed has been cancelled, disable_irq() will have |
Maciej W. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 588 | * been left unbalanced from phy_interrupt() and enable_irq() |
| 589 | * has to be called so that other devices on the line work. |
| 590 | */ |
| 591 | while (atomic_dec_return(&phydev->irq_disable) >= 0) |
| 592 | enable_irq(phydev->irq); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 593 | |
| 594 | return err; |
| 595 | } |
| 596 | EXPORT_SYMBOL(phy_stop_interrupts); |
| 597 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 598 | /** |
| 599 | * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes |
| 600 | * @work: work_struct that describes the work to be done |
| 601 | */ |
Florian Fainelli | 5ea94e7 | 2013-05-19 22:53:43 +0000 | [diff] [blame] | 602 | void phy_change(struct work_struct *work) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 603 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 604 | struct phy_device *phydev = |
| 605 | container_of(work, struct phy_device, phy_queue); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 606 | |
Anatolij Gustschin | a8729eb | 2009-04-07 02:01:42 +0000 | [diff] [blame] | 607 | if (phydev->drv->did_interrupt && |
| 608 | !phydev->drv->did_interrupt(phydev)) |
| 609 | goto ignore; |
| 610 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 611 | if (phy_disable_interrupts(phydev)) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 612 | goto phy_err; |
| 613 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 614 | mutex_lock(&phydev->lock); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 615 | if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state)) |
| 616 | phydev->state = PHY_CHANGELINK; |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 617 | mutex_unlock(&phydev->lock); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 618 | |
Maciej W. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 619 | atomic_dec(&phydev->irq_disable); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 620 | enable_irq(phydev->irq); |
| 621 | |
| 622 | /* Reenable interrupts */ |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 623 | if (PHY_HALTED != phydev->state && |
| 624 | phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED)) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 625 | goto irq_enable_err; |
| 626 | |
Marcin Slusarz | a390d1f | 2009-03-13 15:41:19 -0700 | [diff] [blame] | 627 | /* reschedule state queue work to run as soon as possible */ |
| 628 | cancel_delayed_work_sync(&phydev->state_queue); |
Viresh Kumar | bbb47bd | 2013-04-24 17:12:55 +0530 | [diff] [blame] | 629 | queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 630 | return; |
| 631 | |
Anatolij Gustschin | a8729eb | 2009-04-07 02:01:42 +0000 | [diff] [blame] | 632 | ignore: |
| 633 | atomic_dec(&phydev->irq_disable); |
| 634 | enable_irq(phydev->irq); |
| 635 | return; |
| 636 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 637 | irq_enable_err: |
| 638 | disable_irq(phydev->irq); |
Maciej W. Rozycki | 0ac4952 | 2007-09-28 22:42:14 -0700 | [diff] [blame] | 639 | atomic_inc(&phydev->irq_disable); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 640 | phy_err: |
| 641 | phy_error(phydev); |
| 642 | } |
| 643 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 644 | /** |
| 645 | * phy_stop - Bring down the PHY link, and stop checking the status |
| 646 | * @phydev: target phy_device struct |
| 647 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 648 | void phy_stop(struct phy_device *phydev) |
| 649 | { |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 650 | mutex_lock(&phydev->lock); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 651 | |
| 652 | if (PHY_HALTED == phydev->state) |
| 653 | goto out_unlock; |
| 654 | |
Florian Fainelli | 2c7b492 | 2013-05-19 22:53:42 +0000 | [diff] [blame] | 655 | if (phy_interrupt_is_valid(phydev)) { |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 656 | /* Disable PHY Interrupts */ |
| 657 | phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 658 | |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 659 | /* Clear any pending interrupts */ |
| 660 | phy_clear_interrupt(phydev); |
| 661 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 662 | |
Maciej W. Rozycki | 6daf653 | 2007-09-28 22:42:15 -0700 | [diff] [blame] | 663 | phydev->state = PHY_HALTED; |
| 664 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 665 | out_unlock: |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 666 | mutex_unlock(&phydev->lock); |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 667 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 668 | /* Cannot call flush_scheduled_work() here as desired because |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 669 | * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change() |
| 670 | * will not reenable interrupts. |
| 671 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 672 | } |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 673 | EXPORT_SYMBOL(phy_stop); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 674 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 675 | /** |
| 676 | * phy_start - start or restart a PHY device |
| 677 | * @phydev: target phy_device struct |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 678 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 679 | * Description: Indicates the attached device's readiness to |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 680 | * handle PHY-related work. Used during startup to start the |
| 681 | * PHY, and after a call to phy_stop() to resume operation. |
| 682 | * Also used to indicate the MDIO bus has cleared an error |
| 683 | * condition. |
| 684 | */ |
| 685 | void phy_start(struct phy_device *phydev) |
| 686 | { |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 687 | mutex_lock(&phydev->lock); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 688 | |
| 689 | switch (phydev->state) { |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 690 | case PHY_STARTING: |
| 691 | phydev->state = PHY_PENDING; |
| 692 | break; |
| 693 | case PHY_READY: |
| 694 | phydev->state = PHY_UP; |
| 695 | break; |
| 696 | case PHY_HALTED: |
| 697 | phydev->state = PHY_RESUMING; |
| 698 | default: |
| 699 | break; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 700 | } |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 701 | mutex_unlock(&phydev->lock); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 702 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 703 | EXPORT_SYMBOL(phy_start); |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 704 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 705 | /** |
| 706 | * phy_state_machine - Handle the state machine |
| 707 | * @work: work_struct that describes the work to be done |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 708 | */ |
Anton Vorontsov | 4f9c85a | 2010-01-18 05:37:16 +0000 | [diff] [blame] | 709 | void phy_state_machine(struct work_struct *work) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 710 | { |
Jean Delvare | bf6aede | 2009-04-02 16:56:54 -0700 | [diff] [blame] | 711 | struct delayed_work *dwork = to_delayed_work(work); |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 712 | struct phy_device *phydev = |
Marcin Slusarz | a390d1f | 2009-03-13 15:41:19 -0700 | [diff] [blame] | 713 | container_of(dwork, struct phy_device, state_queue); |
Sebastian Hesselbarth | be9dad1 | 2013-12-13 10:20:29 +0100 | [diff] [blame] | 714 | int needs_aneg = 0, do_suspend = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 715 | int err = 0; |
| 716 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 717 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 718 | |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 719 | switch (phydev->state) { |
| 720 | case PHY_DOWN: |
| 721 | case PHY_STARTING: |
| 722 | case PHY_READY: |
| 723 | case PHY_PENDING: |
| 724 | break; |
| 725 | case PHY_UP: |
| 726 | needs_aneg = 1; |
| 727 | |
| 728 | phydev->link_timeout = PHY_AN_TIMEOUT; |
| 729 | |
| 730 | break; |
| 731 | case PHY_AN: |
| 732 | err = phy_read_status(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 733 | if (err < 0) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 734 | break; |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 735 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 736 | /* If the link is down, give up on negotiation for now */ |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 737 | if (!phydev->link) { |
| 738 | phydev->state = PHY_NOLINK; |
| 739 | netif_carrier_off(phydev->attached_dev); |
| 740 | phydev->adjust_link(phydev->attached_dev); |
| 741 | break; |
| 742 | } |
| 743 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 744 | /* Check if negotiation is done. Break if there's an error */ |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 745 | err = phy_aneg_done(phydev); |
| 746 | if (err < 0) |
| 747 | break; |
| 748 | |
| 749 | /* If AN is done, we're running */ |
| 750 | if (err > 0) { |
| 751 | phydev->state = PHY_RUNNING; |
| 752 | netif_carrier_on(phydev->attached_dev); |
| 753 | phydev->adjust_link(phydev->attached_dev); |
| 754 | |
| 755 | } else if (0 == phydev->link_timeout--) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 756 | needs_aneg = 1; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 757 | /* If we have the magic_aneg bit, we try again */ |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 758 | if (phydev->drv->flags & PHY_HAS_MAGICANEG) |
| 759 | break; |
| 760 | } |
| 761 | break; |
| 762 | case PHY_NOLINK: |
| 763 | err = phy_read_status(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 764 | if (err) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 765 | break; |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 766 | |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 767 | if (phydev->link) { |
| 768 | phydev->state = PHY_RUNNING; |
| 769 | netif_carrier_on(phydev->attached_dev); |
| 770 | phydev->adjust_link(phydev->attached_dev); |
| 771 | } |
| 772 | break; |
| 773 | case PHY_FORCING: |
| 774 | err = genphy_update_link(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 775 | if (err) |
| 776 | break; |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 777 | |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 778 | if (phydev->link) { |
| 779 | phydev->state = PHY_RUNNING; |
| 780 | netif_carrier_on(phydev->attached_dev); |
| 781 | } else { |
| 782 | if (0 == phydev->link_timeout--) |
| 783 | needs_aneg = 1; |
| 784 | } |
| 785 | |
| 786 | phydev->adjust_link(phydev->attached_dev); |
| 787 | break; |
| 788 | case PHY_RUNNING: |
| 789 | /* Only register a CHANGE if we are |
| 790 | * polling or ignoring interrupts |
| 791 | */ |
| 792 | if (!phy_interrupt_is_valid(phydev)) |
| 793 | phydev->state = PHY_CHANGELINK; |
| 794 | break; |
| 795 | case PHY_CHANGELINK: |
| 796 | err = phy_read_status(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 797 | if (err) |
| 798 | break; |
| 799 | |
| 800 | if (phydev->link) { |
| 801 | phydev->state = PHY_RUNNING; |
| 802 | netif_carrier_on(phydev->attached_dev); |
| 803 | } else { |
| 804 | phydev->state = PHY_NOLINK; |
| 805 | netif_carrier_off(phydev->attached_dev); |
| 806 | } |
| 807 | |
| 808 | phydev->adjust_link(phydev->attached_dev); |
| 809 | |
| 810 | if (phy_interrupt_is_valid(phydev)) |
| 811 | err = phy_config_interrupt(phydev, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 812 | PHY_INTERRUPT_ENABLED); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 813 | break; |
| 814 | case PHY_HALTED: |
| 815 | if (phydev->link) { |
| 816 | phydev->link = 0; |
| 817 | netif_carrier_off(phydev->attached_dev); |
| 818 | phydev->adjust_link(phydev->attached_dev); |
| 819 | do_suspend = 1; |
| 820 | } |
| 821 | break; |
| 822 | case PHY_RESUMING: |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 823 | err = phy_clear_interrupt(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 824 | if (err) |
| 825 | break; |
| 826 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 827 | err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 828 | if (err) |
| 829 | break; |
| 830 | |
| 831 | if (AUTONEG_ENABLE == phydev->autoneg) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 832 | err = phy_aneg_done(phydev); |
| 833 | if (err < 0) |
| 834 | break; |
| 835 | |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 836 | /* err > 0 if AN is done. |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 837 | * Otherwise, it's 0, and we're still waiting for AN |
| 838 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 839 | if (err > 0) { |
Wade Farnsworth | 42caa07 | 2009-07-01 13:00:34 +0000 | [diff] [blame] | 840 | err = phy_read_status(phydev); |
| 841 | if (err) |
| 842 | break; |
| 843 | |
| 844 | if (phydev->link) { |
| 845 | phydev->state = PHY_RUNNING; |
| 846 | netif_carrier_on(phydev->attached_dev); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 847 | } else { |
Wade Farnsworth | 42caa07 | 2009-07-01 13:00:34 +0000 | [diff] [blame] | 848 | phydev->state = PHY_NOLINK; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 849 | } |
Wade Farnsworth | 42caa07 | 2009-07-01 13:00:34 +0000 | [diff] [blame] | 850 | phydev->adjust_link(phydev->attached_dev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 851 | } else { |
| 852 | phydev->state = PHY_AN; |
| 853 | phydev->link_timeout = PHY_AN_TIMEOUT; |
Wade Farnsworth | 42caa07 | 2009-07-01 13:00:34 +0000 | [diff] [blame] | 854 | } |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 855 | } else { |
| 856 | err = phy_read_status(phydev); |
| 857 | if (err) |
| 858 | break; |
| 859 | |
| 860 | if (phydev->link) { |
| 861 | phydev->state = PHY_RUNNING; |
| 862 | netif_carrier_on(phydev->attached_dev); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 863 | } else { |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 864 | phydev->state = PHY_NOLINK; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 865 | } |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 866 | phydev->adjust_link(phydev->attached_dev); |
| 867 | } |
| 868 | break; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 869 | } |
| 870 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 871 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 872 | |
| 873 | if (needs_aneg) |
| 874 | err = phy_start_aneg(phydev); |
| 875 | |
Sebastian Hesselbarth | be9dad1 | 2013-12-13 10:20:29 +0100 | [diff] [blame] | 876 | if (do_suspend) |
| 877 | phy_suspend(phydev); |
| 878 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 879 | if (err < 0) |
| 880 | phy_error(phydev); |
| 881 | |
Viresh Kumar | bbb47bd | 2013-04-24 17:12:55 +0530 | [diff] [blame] | 882 | queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 883 | PHY_STATE_TIME * HZ); |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 884 | } |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 885 | |
Florian Fainelli | 5ea94e7 | 2013-05-19 22:53:43 +0000 | [diff] [blame] | 886 | void phy_mac_interrupt(struct phy_device *phydev, int new_link) |
| 887 | { |
| 888 | cancel_work_sync(&phydev->phy_queue); |
| 889 | phydev->link = new_link; |
| 890 | schedule_work(&phydev->phy_queue); |
| 891 | } |
| 892 | EXPORT_SYMBOL(phy_mac_interrupt); |
| 893 | |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 894 | static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad, |
| 895 | int addr) |
| 896 | { |
| 897 | /* Write the desired MMD Devad */ |
| 898 | bus->write(bus, addr, MII_MMD_CTRL, devad); |
| 899 | |
| 900 | /* Write the desired MMD register address */ |
| 901 | bus->write(bus, addr, MII_MMD_DATA, prtad); |
| 902 | |
| 903 | /* Select the Function : DATA with no post increment */ |
| 904 | bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR)); |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * phy_read_mmd_indirect - reads data from the MMD registers |
| 909 | * @bus: the target MII bus |
| 910 | * @prtad: MMD Address |
| 911 | * @devad: MMD DEVAD |
| 912 | * @addr: PHY address on the MII bus |
| 913 | * |
| 914 | * Description: it reads data from the MMD registers (clause 22 to access to |
| 915 | * clause 45) of the specified phy address. |
| 916 | * To read these register we have: |
| 917 | * 1) Write reg 13 // DEVAD |
| 918 | * 2) Write reg 14 // MMD Address |
| 919 | * 3) Write reg 13 // MMD Data Command for MMD DEVAD |
| 920 | * 3) Read reg 14 // Read MMD data |
| 921 | */ |
| 922 | static int phy_read_mmd_indirect(struct mii_bus *bus, int prtad, int devad, |
| 923 | int addr) |
| 924 | { |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 925 | mmd_phy_indirect(bus, prtad, devad, addr); |
| 926 | |
| 927 | /* Read the content of the MMD's selected register */ |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 928 | return bus->read(bus, addr, MII_MMD_DATA); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /** |
| 932 | * phy_write_mmd_indirect - writes data to the MMD registers |
| 933 | * @bus: the target MII bus |
| 934 | * @prtad: MMD Address |
| 935 | * @devad: MMD DEVAD |
| 936 | * @addr: PHY address on the MII bus |
| 937 | * @data: data to write in the MMD register |
| 938 | * |
| 939 | * Description: Write data from the MMD registers of the specified |
| 940 | * phy address. |
| 941 | * To write these register we have: |
| 942 | * 1) Write reg 13 // DEVAD |
| 943 | * 2) Write reg 14 // MMD Address |
| 944 | * 3) Write reg 13 // MMD Data Command for MMD DEVAD |
| 945 | * 3) Write reg 14 // Write MMD data |
| 946 | */ |
| 947 | static void phy_write_mmd_indirect(struct mii_bus *bus, int prtad, int devad, |
| 948 | int addr, u32 data) |
| 949 | { |
| 950 | mmd_phy_indirect(bus, prtad, devad, addr); |
| 951 | |
| 952 | /* Write the data into MMD's selected register */ |
| 953 | bus->write(bus, addr, MII_MMD_DATA, data); |
| 954 | } |
| 955 | |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 956 | /** |
| 957 | * phy_init_eee - init and check the EEE feature |
| 958 | * @phydev: target phy_device struct |
| 959 | * @clk_stop_enable: PHY may stop the clock during LPI |
| 960 | * |
| 961 | * Description: it checks if the Energy-Efficient Ethernet (EEE) |
| 962 | * is supported by looking at the MMD registers 3.20 and 7.60/61 |
| 963 | * and it programs the MMD register 3.0 setting the "Clock stop enable" |
| 964 | * bit if required. |
| 965 | */ |
| 966 | int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable) |
| 967 | { |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 968 | /* According to 802.3az,the EEE is supported only in full duplex-mode. |
| 969 | * Also EEE feature is active when core is operating with MII, GMII |
| 970 | * or RGMII. |
| 971 | */ |
| 972 | if ((phydev->duplex == DUPLEX_FULL) && |
| 973 | ((phydev->interface == PHY_INTERFACE_MODE_MII) || |
| 974 | (phydev->interface == PHY_INTERFACE_MODE_GMII) || |
| 975 | (phydev->interface == PHY_INTERFACE_MODE_RGMII))) { |
| 976 | int eee_lp, eee_cap, eee_adv; |
| 977 | u32 lp, cap, adv; |
| 978 | int idx, status; |
| 979 | |
| 980 | /* Read phy status to properly get the right settings */ |
| 981 | status = phy_read_status(phydev); |
| 982 | if (status) |
| 983 | return status; |
| 984 | |
| 985 | /* First check if the EEE ability is supported */ |
| 986 | eee_cap = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE, |
| 987 | MDIO_MMD_PCS, phydev->addr); |
| 988 | if (eee_cap < 0) |
| 989 | return eee_cap; |
| 990 | |
Allan, Bruce W | b32607d | 2012-08-20 04:55:29 +0000 | [diff] [blame] | 991 | cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 992 | if (!cap) |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 993 | return -EPROTONOSUPPORT; |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 994 | |
| 995 | /* Check which link settings negotiated and verify it in |
| 996 | * the EEE advertising registers. |
| 997 | */ |
| 998 | eee_lp = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE, |
| 999 | MDIO_MMD_AN, phydev->addr); |
| 1000 | if (eee_lp < 0) |
| 1001 | return eee_lp; |
| 1002 | |
| 1003 | eee_adv = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV, |
| 1004 | MDIO_MMD_AN, phydev->addr); |
| 1005 | if (eee_adv < 0) |
| 1006 | return eee_adv; |
| 1007 | |
Allan, Bruce W | b32607d | 2012-08-20 04:55:29 +0000 | [diff] [blame] | 1008 | adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv); |
| 1009 | lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1010 | idx = phy_find_setting(phydev->speed, phydev->duplex); |
Giuseppe CAVALLARO | 9a9c56c | 2013-05-26 21:31:28 +0000 | [diff] [blame] | 1011 | if (!(lp & adv & settings[idx].setting)) |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 1012 | return -EPROTONOSUPPORT; |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1013 | |
| 1014 | if (clk_stop_enable) { |
| 1015 | /* Configure the PHY to stop receiving xMII |
| 1016 | * clock while it is signaling LPI. |
| 1017 | */ |
| 1018 | int val = phy_read_mmd_indirect(phydev->bus, MDIO_CTRL1, |
| 1019 | MDIO_MMD_PCS, |
| 1020 | phydev->addr); |
| 1021 | if (val < 0) |
| 1022 | return val; |
| 1023 | |
| 1024 | val |= MDIO_PCS_CTRL1_CLKSTOP_EN; |
| 1025 | phy_write_mmd_indirect(phydev->bus, MDIO_CTRL1, |
| 1026 | MDIO_MMD_PCS, phydev->addr, val); |
| 1027 | } |
| 1028 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 1029 | return 0; /* EEE supported */ |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 1032 | return -EPROTONOSUPPORT; |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1033 | } |
| 1034 | EXPORT_SYMBOL(phy_init_eee); |
| 1035 | |
| 1036 | /** |
| 1037 | * phy_get_eee_err - report the EEE wake error count |
| 1038 | * @phydev: target phy_device struct |
| 1039 | * |
| 1040 | * Description: it is to report the number of time where the PHY |
| 1041 | * failed to complete its normal wake sequence. |
| 1042 | */ |
| 1043 | int phy_get_eee_err(struct phy_device *phydev) |
| 1044 | { |
| 1045 | return phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_WK_ERR, |
| 1046 | MDIO_MMD_PCS, phydev->addr); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1047 | } |
| 1048 | EXPORT_SYMBOL(phy_get_eee_err); |
| 1049 | |
| 1050 | /** |
| 1051 | * phy_ethtool_get_eee - get EEE supported and status |
| 1052 | * @phydev: target phy_device struct |
| 1053 | * @data: ethtool_eee data |
| 1054 | * |
| 1055 | * Description: it reportes the Supported/Advertisement/LP Advertisement |
| 1056 | * capabilities. |
| 1057 | */ |
| 1058 | int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data) |
| 1059 | { |
| 1060 | int val; |
| 1061 | |
| 1062 | /* Get Supported EEE */ |
| 1063 | val = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE, |
| 1064 | MDIO_MMD_PCS, phydev->addr); |
| 1065 | if (val < 0) |
| 1066 | return val; |
Allan, Bruce W | b32607d | 2012-08-20 04:55:29 +0000 | [diff] [blame] | 1067 | data->supported = mmd_eee_cap_to_ethtool_sup_t(val); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1068 | |
| 1069 | /* Get advertisement EEE */ |
| 1070 | val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV, |
| 1071 | MDIO_MMD_AN, phydev->addr); |
| 1072 | if (val < 0) |
| 1073 | return val; |
Allan, Bruce W | b32607d | 2012-08-20 04:55:29 +0000 | [diff] [blame] | 1074 | data->advertised = mmd_eee_adv_to_ethtool_adv_t(val); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1075 | |
| 1076 | /* Get LP advertisement EEE */ |
| 1077 | val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE, |
| 1078 | MDIO_MMD_AN, phydev->addr); |
| 1079 | if (val < 0) |
| 1080 | return val; |
Allan, Bruce W | b32607d | 2012-08-20 04:55:29 +0000 | [diff] [blame] | 1081 | data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1082 | |
| 1083 | return 0; |
| 1084 | } |
| 1085 | EXPORT_SYMBOL(phy_ethtool_get_eee); |
| 1086 | |
| 1087 | /** |
| 1088 | * phy_ethtool_set_eee - set EEE supported and status |
| 1089 | * @phydev: target phy_device struct |
| 1090 | * @data: ethtool_eee data |
| 1091 | * |
| 1092 | * Description: it is to program the Advertisement EEE register. |
| 1093 | */ |
| 1094 | int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data) |
| 1095 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 1096 | int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised); |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1097 | |
Giuseppe CAVALLARO | a59a4d1 | 2012-06-27 21:14:38 +0000 | [diff] [blame] | 1098 | phy_write_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV, MDIO_MMD_AN, |
| 1099 | phydev->addr, val); |
| 1100 | |
| 1101 | return 0; |
| 1102 | } |
| 1103 | EXPORT_SYMBOL(phy_ethtool_set_eee); |
Michael Stapelberg | 42e836e | 2013-03-11 13:56:44 +0000 | [diff] [blame] | 1104 | |
| 1105 | int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol) |
| 1106 | { |
| 1107 | if (phydev->drv->set_wol) |
| 1108 | return phydev->drv->set_wol(phydev, wol); |
| 1109 | |
| 1110 | return -EOPNOTSUPP; |
| 1111 | } |
| 1112 | EXPORT_SYMBOL(phy_ethtool_set_wol); |
| 1113 | |
| 1114 | void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol) |
| 1115 | { |
| 1116 | if (phydev->drv->get_wol) |
| 1117 | phydev->drv->get_wol(phydev, wol); |
| 1118 | } |
| 1119 | EXPORT_SYMBOL(phy_ethtool_get_wol); |