Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Core PHY library, taken from phy.c |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the |
| 6 | * Free Software Foundation; either version 2 of the License, or (at your |
| 7 | * option) any later version. |
| 8 | */ |
| 9 | #include <linux/export.h> |
| 10 | #include <linux/phy.h> |
| 11 | |
Russell King | da4625a | 2017-07-25 15:02:42 +0100 | [diff] [blame] | 12 | const char *phy_speed_to_str(int speed) |
| 13 | { |
| 14 | switch (speed) { |
| 15 | case SPEED_10: |
| 16 | return "10Mbps"; |
| 17 | case SPEED_100: |
| 18 | return "100Mbps"; |
| 19 | case SPEED_1000: |
| 20 | return "1Gbps"; |
| 21 | case SPEED_2500: |
| 22 | return "2.5Gbps"; |
| 23 | case SPEED_5000: |
| 24 | return "5Gbps"; |
| 25 | case SPEED_10000: |
| 26 | return "10Gbps"; |
| 27 | case SPEED_14000: |
| 28 | return "14Gbps"; |
| 29 | case SPEED_20000: |
| 30 | return "20Gbps"; |
| 31 | case SPEED_25000: |
| 32 | return "25Gbps"; |
| 33 | case SPEED_40000: |
| 34 | return "40Gbps"; |
| 35 | case SPEED_50000: |
| 36 | return "50Gbps"; |
| 37 | case SPEED_56000: |
| 38 | return "56Gbps"; |
| 39 | case SPEED_100000: |
| 40 | return "100Gbps"; |
| 41 | case SPEED_UNKNOWN: |
| 42 | return "Unknown"; |
| 43 | default: |
| 44 | return "Unsupported (update phy-core.c)"; |
| 45 | } |
| 46 | } |
| 47 | EXPORT_SYMBOL_GPL(phy_speed_to_str); |
| 48 | |
| 49 | const char *phy_duplex_to_str(unsigned int duplex) |
| 50 | { |
| 51 | if (duplex == DUPLEX_HALF) |
| 52 | return "Half"; |
| 53 | if (duplex == DUPLEX_FULL) |
| 54 | return "Full"; |
| 55 | if (duplex == DUPLEX_UNKNOWN) |
| 56 | return "Unknown"; |
| 57 | return "Unsupported (update phy-core.c)"; |
| 58 | } |
| 59 | EXPORT_SYMBOL_GPL(phy_duplex_to_str); |
| 60 | |
Russell King | 0ccb4fc | 2017-07-25 15:02:47 +0100 | [diff] [blame] | 61 | /* A mapping of all SUPPORTED settings to speed/duplex. This table |
| 62 | * must be grouped by speed and sorted in descending match priority |
| 63 | * - iow, descending speed. */ |
| 64 | static const struct phy_setting settings[] = { |
| 65 | { |
| 66 | .speed = SPEED_10000, |
| 67 | .duplex = DUPLEX_FULL, |
| 68 | .bit = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, |
| 69 | }, |
| 70 | { |
| 71 | .speed = SPEED_10000, |
| 72 | .duplex = DUPLEX_FULL, |
| 73 | .bit = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, |
| 74 | }, |
| 75 | { |
| 76 | .speed = SPEED_10000, |
| 77 | .duplex = DUPLEX_FULL, |
| 78 | .bit = ETHTOOL_LINK_MODE_10000baseT_Full_BIT, |
| 79 | }, |
| 80 | { |
| 81 | .speed = SPEED_2500, |
| 82 | .duplex = DUPLEX_FULL, |
| 83 | .bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT, |
| 84 | }, |
| 85 | { |
| 86 | .speed = SPEED_1000, |
| 87 | .duplex = DUPLEX_FULL, |
| 88 | .bit = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, |
| 89 | }, |
| 90 | { |
| 91 | .speed = SPEED_1000, |
| 92 | .duplex = DUPLEX_FULL, |
Russell King | 1f3645b | 2017-07-25 15:02:52 +0100 | [diff] [blame] | 93 | .bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT, |
| 94 | }, |
| 95 | { |
| 96 | .speed = SPEED_1000, |
| 97 | .duplex = DUPLEX_FULL, |
Russell King | 0ccb4fc | 2017-07-25 15:02:47 +0100 | [diff] [blame] | 98 | .bit = ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 99 | }, |
| 100 | { |
| 101 | .speed = SPEED_1000, |
| 102 | .duplex = DUPLEX_HALF, |
| 103 | .bit = ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 104 | }, |
| 105 | { |
| 106 | .speed = SPEED_100, |
| 107 | .duplex = DUPLEX_FULL, |
| 108 | .bit = ETHTOOL_LINK_MODE_100baseT_Full_BIT, |
| 109 | }, |
| 110 | { |
| 111 | .speed = SPEED_100, |
| 112 | .duplex = DUPLEX_HALF, |
| 113 | .bit = ETHTOOL_LINK_MODE_100baseT_Half_BIT, |
| 114 | }, |
| 115 | { |
| 116 | .speed = SPEED_10, |
| 117 | .duplex = DUPLEX_FULL, |
| 118 | .bit = ETHTOOL_LINK_MODE_10baseT_Full_BIT, |
| 119 | }, |
| 120 | { |
| 121 | .speed = SPEED_10, |
| 122 | .duplex = DUPLEX_HALF, |
| 123 | .bit = ETHTOOL_LINK_MODE_10baseT_Half_BIT, |
| 124 | }, |
| 125 | }; |
| 126 | |
| 127 | /** |
| 128 | * phy_lookup_setting - lookup a PHY setting |
| 129 | * @speed: speed to match |
| 130 | * @duplex: duplex to match |
| 131 | * @mask: allowed link modes |
| 132 | * @maxbit: bit size of link modes |
| 133 | * @exact: an exact match is required |
| 134 | * |
| 135 | * Search the settings array for a setting that matches the speed and |
| 136 | * duplex, and which is supported. |
| 137 | * |
| 138 | * If @exact is unset, either an exact match or %NULL for no match will |
| 139 | * be returned. |
| 140 | * |
| 141 | * If @exact is set, an exact match, the fastest supported setting at |
| 142 | * or below the specified speed, the slowest supported setting, or if |
| 143 | * they all fail, %NULL will be returned. |
| 144 | */ |
| 145 | const struct phy_setting * |
| 146 | phy_lookup_setting(int speed, int duplex, const unsigned long *mask, |
| 147 | size_t maxbit, bool exact) |
| 148 | { |
| 149 | const struct phy_setting *p, *match = NULL, *last = NULL; |
| 150 | int i; |
| 151 | |
| 152 | for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) { |
| 153 | if (p->bit < maxbit && test_bit(p->bit, mask)) { |
| 154 | last = p; |
| 155 | if (p->speed == speed && p->duplex == duplex) { |
| 156 | /* Exact match for speed and duplex */ |
| 157 | match = p; |
| 158 | break; |
| 159 | } else if (!exact) { |
| 160 | if (!match && p->speed <= speed) |
| 161 | /* Candidate */ |
| 162 | match = p; |
| 163 | |
| 164 | if (p->speed < speed) |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (!match && !exact) |
| 171 | match = last; |
| 172 | |
| 173 | return match; |
| 174 | } |
| 175 | EXPORT_SYMBOL_GPL(phy_lookup_setting); |
| 176 | |
| 177 | size_t phy_speeds(unsigned int *speeds, size_t size, |
| 178 | unsigned long *mask, size_t maxbit) |
| 179 | { |
| 180 | size_t count; |
| 181 | int i; |
| 182 | |
| 183 | for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++) |
| 184 | if (settings[i].bit < maxbit && |
| 185 | test_bit(settings[i].bit, mask) && |
| 186 | (count == 0 || speeds[count - 1] != settings[i].speed)) |
| 187 | speeds[count++] = settings[i].speed; |
| 188 | |
| 189 | return count; |
| 190 | } |
| 191 | |
Russell King | 8c5e850 | 2017-12-29 12:46:38 +0000 | [diff] [blame] | 192 | /** |
Colin Ian King | cc1122b | 2018-03-01 10:23:03 +0000 | [diff] [blame] | 193 | * phy_resolve_aneg_linkmode - resolve the advertisements into phy settings |
Russell King | 8c5e850 | 2017-12-29 12:46:38 +0000 | [diff] [blame] | 194 | * @phydev: The phy_device struct |
| 195 | * |
Colin Ian King | cc1122b | 2018-03-01 10:23:03 +0000 | [diff] [blame] | 196 | * Resolve our and the link partner advertisements into their corresponding |
Russell King | 8c5e850 | 2017-12-29 12:46:38 +0000 | [diff] [blame] | 197 | * speed and duplex. If full duplex was negotiated, extract the pause mode |
| 198 | * from the link partner mask. |
| 199 | */ |
| 200 | void phy_resolve_aneg_linkmode(struct phy_device *phydev) |
| 201 | { |
| 202 | u32 common = phydev->lp_advertising & phydev->advertising; |
| 203 | |
| 204 | if (common & ADVERTISED_10000baseT_Full) { |
| 205 | phydev->speed = SPEED_10000; |
| 206 | phydev->duplex = DUPLEX_FULL; |
| 207 | } else if (common & ADVERTISED_1000baseT_Full) { |
| 208 | phydev->speed = SPEED_1000; |
| 209 | phydev->duplex = DUPLEX_FULL; |
| 210 | } else if (common & ADVERTISED_1000baseT_Half) { |
| 211 | phydev->speed = SPEED_1000; |
| 212 | phydev->duplex = DUPLEX_HALF; |
| 213 | } else if (common & ADVERTISED_100baseT_Full) { |
| 214 | phydev->speed = SPEED_100; |
| 215 | phydev->duplex = DUPLEX_FULL; |
| 216 | } else if (common & ADVERTISED_100baseT_Half) { |
| 217 | phydev->speed = SPEED_100; |
| 218 | phydev->duplex = DUPLEX_HALF; |
| 219 | } else if (common & ADVERTISED_10baseT_Full) { |
| 220 | phydev->speed = SPEED_10; |
| 221 | phydev->duplex = DUPLEX_FULL; |
| 222 | } else if (common & ADVERTISED_10baseT_Half) { |
| 223 | phydev->speed = SPEED_10; |
| 224 | phydev->duplex = DUPLEX_HALF; |
| 225 | } |
| 226 | |
| 227 | if (phydev->duplex == DUPLEX_FULL) { |
| 228 | phydev->pause = !!(phydev->lp_advertising & ADVERTISED_Pause); |
| 229 | phydev->asym_pause = !!(phydev->lp_advertising & |
| 230 | ADVERTISED_Asym_Pause); |
| 231 | } |
| 232 | } |
| 233 | EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode); |
| 234 | |
Russell King | 060fbc8 | 2017-03-21 16:37:08 +0000 | [diff] [blame] | 235 | static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad, |
| 236 | u16 regnum) |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 237 | { |
| 238 | /* Write the desired MMD Devad */ |
Russell King | 1b2dea2 | 2018-01-02 10:58:32 +0000 | [diff] [blame] | 239 | __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad); |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 240 | |
| 241 | /* Write the desired MMD register address */ |
Russell King | 1b2dea2 | 2018-01-02 10:58:32 +0000 | [diff] [blame] | 242 | __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum); |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 243 | |
| 244 | /* Select the Function : DATA with no post increment */ |
Russell King | 1b2dea2 | 2018-01-02 10:58:32 +0000 | [diff] [blame] | 245 | __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, |
| 246 | devad | MII_MMD_CTRL_NOINCR); |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 250 | * phy_read_mmd - Convenience function for reading a register |
| 251 | * from an MMD on a given PHY. |
| 252 | * @phydev: The phy_device struct |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 253 | * @devad: The MMD to read from (0..31) |
| 254 | * @regnum: The register on the MMD to read (0..65535) |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 255 | * |
| 256 | * Same rules as for phy_read(); |
| 257 | */ |
| 258 | int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum) |
| 259 | { |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 260 | int val; |
| 261 | |
Russell King | 1ee6b9b | 2017-03-21 16:36:43 +0000 | [diff] [blame] | 262 | if (regnum > (u16)~0 || devad > 32) |
| 263 | return -EINVAL; |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 264 | |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 265 | if (phydev->drv->read_mmd) { |
| 266 | val = phydev->drv->read_mmd(phydev, devad, regnum); |
| 267 | } else if (phydev->is_c45) { |
Russell King | 1ee6b9b | 2017-03-21 16:36:43 +0000 | [diff] [blame] | 268 | u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff); |
Russell King | 1ee6b9b | 2017-03-21 16:36:43 +0000 | [diff] [blame] | 269 | |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 270 | val = mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr); |
| 271 | } else { |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 272 | struct mii_bus *bus = phydev->mdio.bus; |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 273 | int phy_addr = phydev->mdio.addr; |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 274 | |
| 275 | mutex_lock(&bus->mdio_lock); |
Russell King | 060fbc8 | 2017-03-21 16:37:08 +0000 | [diff] [blame] | 276 | mmd_phy_indirect(bus, phy_addr, devad, regnum); |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 277 | |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 278 | /* Read the content of the MMD's selected register */ |
Russell King | 1b2dea2 | 2018-01-02 10:58:32 +0000 | [diff] [blame] | 279 | val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA); |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 280 | mutex_unlock(&bus->mdio_lock); |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 281 | } |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 282 | return val; |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 283 | } |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 284 | EXPORT_SYMBOL(phy_read_mmd); |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 285 | |
| 286 | /** |
| 287 | * phy_write_mmd - Convenience function for writing a register |
| 288 | * on an MMD on a given PHY. |
| 289 | * @phydev: The phy_device struct |
| 290 | * @devad: The MMD to read from |
| 291 | * @regnum: The register on the MMD to read |
| 292 | * @val: value to write to @regnum |
| 293 | * |
| 294 | * Same rules as for phy_write(); |
| 295 | */ |
| 296 | int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val) |
| 297 | { |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 298 | int ret; |
| 299 | |
Russell King | 1ee6b9b | 2017-03-21 16:36:43 +0000 | [diff] [blame] | 300 | if (regnum > (u16)~0 || devad > 32) |
| 301 | return -EINVAL; |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 302 | |
Dan Carpenter | 1dbba4c | 2017-04-14 22:10:41 +0300 | [diff] [blame] | 303 | if (phydev->drv->write_mmd) { |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 304 | ret = phydev->drv->write_mmd(phydev, devad, regnum, val); |
| 305 | } else if (phydev->is_c45) { |
Russell King | 1ee6b9b | 2017-03-21 16:36:43 +0000 | [diff] [blame] | 306 | u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff); |
| 307 | |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 308 | ret = mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, |
| 309 | addr, val); |
| 310 | } else { |
| 311 | struct mii_bus *bus = phydev->mdio.bus; |
| 312 | int phy_addr = phydev->mdio.addr; |
| 313 | |
| 314 | mutex_lock(&bus->mdio_lock); |
Russell King | 060fbc8 | 2017-03-21 16:37:08 +0000 | [diff] [blame] | 315 | mmd_phy_indirect(bus, phy_addr, devad, regnum); |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 316 | |
| 317 | /* Write the data into MMD's selected register */ |
Russell King | 1b2dea2 | 2018-01-02 10:58:32 +0000 | [diff] [blame] | 318 | __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val); |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 319 | mutex_unlock(&bus->mdio_lock); |
| 320 | |
| 321 | ret = 0; |
Russell King | 1ee6b9b | 2017-03-21 16:36:43 +0000 | [diff] [blame] | 322 | } |
Russell King | 3b85d8d | 2017-03-21 16:37:03 +0000 | [diff] [blame] | 323 | return ret; |
Russell King | 9860118 | 2017-03-21 16:36:37 +0000 | [diff] [blame] | 324 | } |
| 325 | EXPORT_SYMBOL(phy_write_mmd); |
Russell King | 788f993 | 2018-01-02 10:58:37 +0000 | [diff] [blame] | 326 | |
| 327 | /** |
| 328 | * __phy_modify() - Convenience function for modifying a PHY register |
| 329 | * @phydev: a pointer to a &struct phy_device |
| 330 | * @regnum: register number |
| 331 | * @mask: bit mask of bits to clear |
| 332 | * @set: bit mask of bits to set |
| 333 | * |
| 334 | * Unlocked helper function which allows a PHY register to be modified as |
Russell King | f102852 | 2018-01-05 16:07:10 +0000 | [diff] [blame] | 335 | * new register value = (old register value & ~mask) | set |
Russell King | 788f993 | 2018-01-02 10:58:37 +0000 | [diff] [blame] | 336 | */ |
| 337 | int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set) |
| 338 | { |
Andrew Lunn | 9f239fe | 2018-01-12 15:01:36 +0100 | [diff] [blame] | 339 | int ret; |
Russell King | 788f993 | 2018-01-02 10:58:37 +0000 | [diff] [blame] | 340 | |
| 341 | ret = __phy_read(phydev, regnum); |
Andrew Lunn | 9f239fe | 2018-01-12 15:01:36 +0100 | [diff] [blame] | 342 | if (ret < 0) |
| 343 | return ret; |
Russell King | 788f993 | 2018-01-02 10:58:37 +0000 | [diff] [blame] | 344 | |
Andrew Lunn | 9f239fe | 2018-01-12 15:01:36 +0100 | [diff] [blame] | 345 | ret = __phy_write(phydev, regnum, (ret & ~mask) | set); |
| 346 | |
| 347 | return ret < 0 ? ret : 0; |
Russell King | 788f993 | 2018-01-02 10:58:37 +0000 | [diff] [blame] | 348 | } |
| 349 | EXPORT_SYMBOL_GPL(__phy_modify); |
Russell King | 78ffc4a | 2018-01-02 10:58:43 +0000 | [diff] [blame] | 350 | |
Russell King | 2b74e5b | 2018-01-02 10:58:53 +0000 | [diff] [blame] | 351 | /** |
| 352 | * phy_modify - Convenience function for modifying a given PHY register |
| 353 | * @phydev: the phy_device struct |
| 354 | * @regnum: register number to write |
| 355 | * @mask: bit mask of bits to clear |
| 356 | * @set: new value of bits set in mask to write to @regnum |
| 357 | * |
| 358 | * NOTE: MUST NOT be called from interrupt context, |
| 359 | * because the bus read/write functions may wait for an interrupt |
| 360 | * to conclude the operation. |
| 361 | */ |
| 362 | int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set) |
| 363 | { |
| 364 | int ret; |
| 365 | |
| 366 | mutex_lock(&phydev->mdio.bus->mdio_lock); |
| 367 | ret = __phy_modify(phydev, regnum, mask, set); |
| 368 | mutex_unlock(&phydev->mdio.bus->mdio_lock); |
| 369 | |
| 370 | return ret; |
| 371 | } |
| 372 | EXPORT_SYMBOL_GPL(phy_modify); |
| 373 | |
Russell King | 78ffc4a | 2018-01-02 10:58:43 +0000 | [diff] [blame] | 374 | static int __phy_read_page(struct phy_device *phydev) |
| 375 | { |
| 376 | return phydev->drv->read_page(phydev); |
| 377 | } |
| 378 | |
| 379 | static int __phy_write_page(struct phy_device *phydev, int page) |
| 380 | { |
| 381 | return phydev->drv->write_page(phydev, page); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * phy_save_page() - take the bus lock and save the current page |
| 386 | * @phydev: a pointer to a &struct phy_device |
| 387 | * |
| 388 | * Take the MDIO bus lock, and return the current page number. On error, |
| 389 | * returns a negative errno. phy_restore_page() must always be called |
| 390 | * after this, irrespective of success or failure of this call. |
| 391 | */ |
| 392 | int phy_save_page(struct phy_device *phydev) |
| 393 | { |
| 394 | mutex_lock(&phydev->mdio.bus->mdio_lock); |
| 395 | return __phy_read_page(phydev); |
| 396 | } |
| 397 | EXPORT_SYMBOL_GPL(phy_save_page); |
| 398 | |
| 399 | /** |
| 400 | * phy_select_page() - take the bus lock, save the current page, and set a page |
| 401 | * @phydev: a pointer to a &struct phy_device |
| 402 | * @page: desired page |
| 403 | * |
| 404 | * Take the MDIO bus lock to protect against concurrent access, save the |
| 405 | * current PHY page, and set the current page. On error, returns a |
| 406 | * negative errno, otherwise returns the previous page number. |
| 407 | * phy_restore_page() must always be called after this, irrespective |
| 408 | * of success or failure of this call. |
| 409 | */ |
| 410 | int phy_select_page(struct phy_device *phydev, int page) |
| 411 | { |
| 412 | int ret, oldpage; |
| 413 | |
| 414 | oldpage = ret = phy_save_page(phydev); |
| 415 | if (ret < 0) |
| 416 | return ret; |
| 417 | |
| 418 | if (oldpage != page) { |
| 419 | ret = __phy_write_page(phydev, page); |
| 420 | if (ret < 0) |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | return oldpage; |
| 425 | } |
| 426 | EXPORT_SYMBOL_GPL(phy_select_page); |
| 427 | |
| 428 | /** |
| 429 | * phy_restore_page() - restore the page register and release the bus lock |
| 430 | * @phydev: a pointer to a &struct phy_device |
| 431 | * @oldpage: the old page, return value from phy_save_page() or phy_select_page() |
| 432 | * @ret: operation's return code |
| 433 | * |
| 434 | * Release the MDIO bus lock, restoring @oldpage if it is a valid page. |
| 435 | * This function propagates the earliest error code from the group of |
| 436 | * operations. |
| 437 | * |
| 438 | * Returns: |
| 439 | * @oldpage if it was a negative value, otherwise |
| 440 | * @ret if it was a negative errno value, otherwise |
| 441 | * phy_write_page()'s negative value if it were in error, otherwise |
| 442 | * @ret. |
| 443 | */ |
| 444 | int phy_restore_page(struct phy_device *phydev, int oldpage, int ret) |
| 445 | { |
| 446 | int r; |
| 447 | |
| 448 | if (oldpage >= 0) { |
| 449 | r = __phy_write_page(phydev, oldpage); |
| 450 | |
| 451 | /* Propagate the operation return code if the page write |
| 452 | * was successful. |
| 453 | */ |
| 454 | if (ret >= 0 && r < 0) |
| 455 | ret = r; |
| 456 | } else { |
| 457 | /* Propagate the phy page selection error code */ |
| 458 | ret = oldpage; |
| 459 | } |
| 460 | |
| 461 | mutex_unlock(&phydev->mdio.bus->mdio_lock); |
| 462 | |
| 463 | return ret; |
| 464 | } |
| 465 | EXPORT_SYMBOL_GPL(phy_restore_page); |
| 466 | |
| 467 | /** |
| 468 | * phy_read_paged() - Convenience function for reading a paged register |
| 469 | * @phydev: a pointer to a &struct phy_device |
| 470 | * @page: the page for the phy |
| 471 | * @regnum: register number |
| 472 | * |
| 473 | * Same rules as for phy_read(). |
| 474 | */ |
| 475 | int phy_read_paged(struct phy_device *phydev, int page, u32 regnum) |
| 476 | { |
| 477 | int ret = 0, oldpage; |
| 478 | |
| 479 | oldpage = phy_select_page(phydev, page); |
| 480 | if (oldpage >= 0) |
| 481 | ret = __phy_read(phydev, regnum); |
| 482 | |
| 483 | return phy_restore_page(phydev, oldpage, ret); |
| 484 | } |
| 485 | EXPORT_SYMBOL(phy_read_paged); |
| 486 | |
| 487 | /** |
| 488 | * phy_write_paged() - Convenience function for writing a paged register |
| 489 | * @phydev: a pointer to a &struct phy_device |
| 490 | * @page: the page for the phy |
| 491 | * @regnum: register number |
| 492 | * @val: value to write |
| 493 | * |
| 494 | * Same rules as for phy_write(). |
| 495 | */ |
| 496 | int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val) |
| 497 | { |
| 498 | int ret = 0, oldpage; |
| 499 | |
| 500 | oldpage = phy_select_page(phydev, page); |
| 501 | if (oldpage >= 0) |
| 502 | ret = __phy_write(phydev, regnum, val); |
| 503 | |
| 504 | return phy_restore_page(phydev, oldpage, ret); |
| 505 | } |
| 506 | EXPORT_SYMBOL(phy_write_paged); |
| 507 | |
| 508 | /** |
| 509 | * phy_modify_paged() - Convenience function for modifying a paged register |
| 510 | * @phydev: a pointer to a &struct phy_device |
| 511 | * @page: the page for the phy |
| 512 | * @regnum: register number |
| 513 | * @mask: bit mask of bits to clear |
| 514 | * @set: bit mask of bits to set |
| 515 | * |
| 516 | * Same rules as for phy_read() and phy_write(). |
| 517 | */ |
| 518 | int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum, |
| 519 | u16 mask, u16 set) |
| 520 | { |
| 521 | int ret = 0, oldpage; |
| 522 | |
| 523 | oldpage = phy_select_page(phydev, page); |
| 524 | if (oldpage >= 0) |
| 525 | ret = __phy_modify(phydev, regnum, mask, set); |
| 526 | |
| 527 | return phy_restore_page(phydev, oldpage, ret); |
| 528 | } |
| 529 | EXPORT_SYMBOL(phy_modify_paged); |