Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1 | /* |
| 2 | * drivers/net/phy/phy.c |
| 3 | * |
| 4 | * Framework for configuring and reading PHY devices |
| 5 | * Based on code in sungem_phy.c and gianfar_phy.c |
| 6 | * |
| 7 | * Author: Andy Fleming |
| 8 | * |
| 9 | * Copyright (c) 2004 Freescale Semiconductor, Inc. |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 10 | * Copyright (c) 2006 Maciej W. Rozycki |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License as published by the |
| 14 | * Free Software Foundation; either version 2 of the License, or (at your |
| 15 | * option) any later version. |
| 16 | * |
| 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> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/netdevice.h> |
| 27 | #include <linux/etherdevice.h> |
| 28 | #include <linux/skbuff.h> |
| 29 | #include <linux/spinlock.h> |
| 30 | #include <linux/mm.h> |
| 31 | #include <linux/module.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 32 | #include <linux/mii.h> |
| 33 | #include <linux/ethtool.h> |
| 34 | #include <linux/phy.h> |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 35 | #include <linux/timer.h> |
| 36 | #include <linux/workqueue.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 37 | |
| 38 | #include <asm/io.h> |
| 39 | #include <asm/irq.h> |
| 40 | #include <asm/uaccess.h> |
| 41 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 42 | /** |
| 43 | * phy_print_status - Convenience function to print out the current phy status |
| 44 | * @phydev: the phy_device struct |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 45 | */ |
| 46 | void phy_print_status(struct phy_device *phydev) |
| 47 | { |
Kumar Gala | a4d00f1 | 2006-01-11 11:27:33 -0800 | [diff] [blame] | 48 | pr_info("PHY: %s - Link is %s", phydev->dev.bus_id, |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 49 | phydev->link ? "Up" : "Down"); |
| 50 | if (phydev->link) |
| 51 | printk(" - %d/%s", phydev->speed, |
| 52 | DUPLEX_FULL == phydev->duplex ? |
| 53 | "Full" : "Half"); |
| 54 | |
| 55 | printk("\n"); |
| 56 | } |
| 57 | EXPORT_SYMBOL(phy_print_status); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 58 | |
| 59 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 60 | /** |
| 61 | * phy_read - Convenience function for reading a given PHY register |
| 62 | * @phydev: the phy_device struct |
| 63 | * @regnum: register number to read |
| 64 | * |
| 65 | * NOTE: MUST NOT be called from interrupt context, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 66 | * because the bus read/write functions may wait for an interrupt |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 67 | * to conclude the operation. |
| 68 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 69 | int phy_read(struct phy_device *phydev, u16 regnum) |
| 70 | { |
| 71 | int retval; |
| 72 | struct mii_bus *bus = phydev->bus; |
| 73 | |
| 74 | spin_lock_bh(&bus->mdio_lock); |
| 75 | retval = bus->read(bus, phydev->addr, regnum); |
| 76 | spin_unlock_bh(&bus->mdio_lock); |
| 77 | |
| 78 | return retval; |
| 79 | } |
| 80 | EXPORT_SYMBOL(phy_read); |
| 81 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 82 | /** |
| 83 | * phy_write - Convenience function for writing a given PHY register |
| 84 | * @phydev: the phy_device struct |
| 85 | * @regnum: register number to write |
| 86 | * @val: value to write to @regnum |
| 87 | * |
| 88 | * NOTE: MUST NOT be called from interrupt context, |
| 89 | * because the bus read/write functions may wait for an interrupt |
| 90 | * to conclude the operation. |
| 91 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 92 | int phy_write(struct phy_device *phydev, u16 regnum, u16 val) |
| 93 | { |
| 94 | int err; |
| 95 | struct mii_bus *bus = phydev->bus; |
| 96 | |
| 97 | spin_lock_bh(&bus->mdio_lock); |
| 98 | err = bus->write(bus, phydev->addr, regnum, val); |
| 99 | spin_unlock_bh(&bus->mdio_lock); |
| 100 | |
| 101 | return err; |
| 102 | } |
| 103 | EXPORT_SYMBOL(phy_write); |
| 104 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 105 | /** |
| 106 | * phy_clear_interrupt - Ack the phy device's interrupt |
| 107 | * @phydev: the phy_device struct |
| 108 | * |
| 109 | * If the @phydev driver has an ack_interrupt function, call it to |
| 110 | * ack and clear the phy device's interrupt. |
| 111 | * |
| 112 | * Returns 0 on success on < 0 on error. |
| 113 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 114 | int phy_clear_interrupt(struct phy_device *phydev) |
| 115 | { |
| 116 | int err = 0; |
| 117 | |
| 118 | if (phydev->drv->ack_interrupt) |
| 119 | err = phydev->drv->ack_interrupt(phydev); |
| 120 | |
| 121 | return err; |
| 122 | } |
| 123 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 124 | /** |
| 125 | * phy_config_interrupt - configure the PHY device for the requested interrupts |
| 126 | * @phydev: the phy_device struct |
| 127 | * @interrupts: interrupt flags to configure for this @phydev |
| 128 | * |
| 129 | * Returns 0 on success on < 0 on error. |
| 130 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 131 | int phy_config_interrupt(struct phy_device *phydev, u32 interrupts) |
| 132 | { |
| 133 | int err = 0; |
| 134 | |
| 135 | phydev->interrupts = interrupts; |
| 136 | if (phydev->drv->config_intr) |
| 137 | err = phydev->drv->config_intr(phydev); |
| 138 | |
| 139 | return err; |
| 140 | } |
| 141 | |
| 142 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 143 | /** |
| 144 | * phy_aneg_done - return auto-negotiation status |
| 145 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 146 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 147 | * Description: Reads the status register and returns 0 either if |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 148 | * auto-negotiation is incomplete, or if there was an error. |
| 149 | * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. |
| 150 | */ |
| 151 | static inline int phy_aneg_done(struct phy_device *phydev) |
| 152 | { |
| 153 | int retval; |
| 154 | |
| 155 | retval = phy_read(phydev, MII_BMSR); |
| 156 | |
| 157 | return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); |
| 158 | } |
| 159 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 160 | /* A structure for mapping a particular speed and duplex |
| 161 | * combination to a particular SUPPORTED and ADVERTISED value */ |
| 162 | struct phy_setting { |
| 163 | int speed; |
| 164 | int duplex; |
| 165 | u32 setting; |
| 166 | }; |
| 167 | |
| 168 | /* A mapping of all SUPPORTED settings to speed/duplex */ |
Arjan van de Ven | f71e130 | 2006-03-03 21:33:57 -0500 | [diff] [blame] | 169 | static const struct phy_setting settings[] = { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 170 | { |
| 171 | .speed = 10000, |
| 172 | .duplex = DUPLEX_FULL, |
| 173 | .setting = SUPPORTED_10000baseT_Full, |
| 174 | }, |
| 175 | { |
| 176 | .speed = SPEED_1000, |
| 177 | .duplex = DUPLEX_FULL, |
| 178 | .setting = SUPPORTED_1000baseT_Full, |
| 179 | }, |
| 180 | { |
| 181 | .speed = SPEED_1000, |
| 182 | .duplex = DUPLEX_HALF, |
| 183 | .setting = SUPPORTED_1000baseT_Half, |
| 184 | }, |
| 185 | { |
| 186 | .speed = SPEED_100, |
| 187 | .duplex = DUPLEX_FULL, |
| 188 | .setting = SUPPORTED_100baseT_Full, |
| 189 | }, |
| 190 | { |
| 191 | .speed = SPEED_100, |
| 192 | .duplex = DUPLEX_HALF, |
| 193 | .setting = SUPPORTED_100baseT_Half, |
| 194 | }, |
| 195 | { |
| 196 | .speed = SPEED_10, |
| 197 | .duplex = DUPLEX_FULL, |
| 198 | .setting = SUPPORTED_10baseT_Full, |
| 199 | }, |
| 200 | { |
| 201 | .speed = SPEED_10, |
| 202 | .duplex = DUPLEX_HALF, |
| 203 | .setting = SUPPORTED_10baseT_Half, |
| 204 | }, |
| 205 | }; |
| 206 | |
| 207 | #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting)) |
| 208 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 209 | /** |
| 210 | * phy_find_setting - find a PHY settings array entry that matches speed & duplex |
| 211 | * @speed: speed to match |
| 212 | * @duplex: duplex to match |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 213 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 214 | * Description: Searches the settings array for the setting which |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 215 | * matches the desired speed and duplex, and returns the index |
| 216 | * of that setting. Returns the index of the last setting if |
| 217 | * none of the others match. |
| 218 | */ |
| 219 | static inline int phy_find_setting(int speed, int duplex) |
| 220 | { |
| 221 | int idx = 0; |
| 222 | |
| 223 | while (idx < ARRAY_SIZE(settings) && |
| 224 | (settings[idx].speed != speed || |
| 225 | settings[idx].duplex != duplex)) |
| 226 | idx++; |
| 227 | |
| 228 | return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1; |
| 229 | } |
| 230 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 231 | /** |
| 232 | * phy_find_valid - find a PHY setting that matches the requested features mask |
| 233 | * @idx: The first index in settings[] to search |
| 234 | * @features: A mask of the valid settings |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 235 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 236 | * Description: Returns the index of the first valid setting less |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 237 | * than or equal to the one pointed to by idx, as determined by |
| 238 | * the mask in features. Returns the index of the last setting |
| 239 | * if nothing else matches. |
| 240 | */ |
| 241 | static inline int phy_find_valid(int idx, u32 features) |
| 242 | { |
| 243 | while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features)) |
| 244 | idx++; |
| 245 | |
| 246 | return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1; |
| 247 | } |
| 248 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 249 | /** |
| 250 | * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex |
| 251 | * @phydev: the target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 252 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 253 | * Description: Make sure the PHY is set to supported speeds and |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 254 | * duplexes. Drop down by one in this order: 1000/FULL, |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 255 | * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 256 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 257 | void phy_sanitize_settings(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 258 | { |
| 259 | u32 features = phydev->supported; |
| 260 | int idx; |
| 261 | |
| 262 | /* Sanitize settings based on PHY capabilities */ |
| 263 | if ((features & SUPPORTED_Autoneg) == 0) |
| 264 | phydev->autoneg = 0; |
| 265 | |
| 266 | idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex), |
| 267 | features); |
| 268 | |
| 269 | phydev->speed = settings[idx].speed; |
| 270 | phydev->duplex = settings[idx].duplex; |
| 271 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 272 | EXPORT_SYMBOL(phy_sanitize_settings); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 273 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 274 | /** |
| 275 | * phy_ethtool_sset - generic ethtool sset function, handles all the details |
| 276 | * @phydev: target phy_device struct |
| 277 | * @cmd: ethtool_cmd |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 278 | * |
| 279 | * A few notes about parameter checking: |
| 280 | * - We don't set port or transceiver, so we don't care what they |
| 281 | * were set to. |
| 282 | * - phy_start_aneg() will make sure forced settings are sane, and |
| 283 | * 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] | 284 | * care if ethtool tries to give us bad values. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 285 | */ |
| 286 | int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd) |
| 287 | { |
| 288 | if (cmd->phy_address != phydev->addr) |
| 289 | return -EINVAL; |
| 290 | |
| 291 | /* We make sure that we don't pass unsupported |
| 292 | * values in to the PHY */ |
| 293 | cmd->advertising &= phydev->supported; |
| 294 | |
| 295 | /* Verify the settings we care about. */ |
| 296 | if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE) |
| 297 | return -EINVAL; |
| 298 | |
| 299 | if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0) |
| 300 | return -EINVAL; |
| 301 | |
| 302 | if (cmd->autoneg == AUTONEG_DISABLE |
| 303 | && ((cmd->speed != SPEED_1000 |
| 304 | && cmd->speed != SPEED_100 |
| 305 | && cmd->speed != SPEED_10) |
| 306 | || (cmd->duplex != DUPLEX_HALF |
| 307 | && cmd->duplex != DUPLEX_FULL))) |
| 308 | return -EINVAL; |
| 309 | |
| 310 | phydev->autoneg = cmd->autoneg; |
| 311 | |
| 312 | phydev->speed = cmd->speed; |
| 313 | |
| 314 | phydev->advertising = cmd->advertising; |
| 315 | |
| 316 | if (AUTONEG_ENABLE == cmd->autoneg) |
| 317 | phydev->advertising |= ADVERTISED_Autoneg; |
| 318 | else |
| 319 | phydev->advertising &= ~ADVERTISED_Autoneg; |
| 320 | |
| 321 | phydev->duplex = cmd->duplex; |
| 322 | |
| 323 | /* Restart the PHY */ |
| 324 | phy_start_aneg(phydev); |
| 325 | |
| 326 | return 0; |
| 327 | } |
Kumar Gala | 9f6d55d | 2007-01-20 16:38:26 -0600 | [diff] [blame] | 328 | EXPORT_SYMBOL(phy_ethtool_sset); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 329 | |
| 330 | int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd) |
| 331 | { |
| 332 | cmd->supported = phydev->supported; |
| 333 | |
| 334 | cmd->advertising = phydev->advertising; |
| 335 | |
| 336 | cmd->speed = phydev->speed; |
| 337 | cmd->duplex = phydev->duplex; |
| 338 | cmd->port = PORT_MII; |
| 339 | cmd->phy_address = phydev->addr; |
| 340 | cmd->transceiver = XCVR_EXTERNAL; |
| 341 | cmd->autoneg = phydev->autoneg; |
| 342 | |
| 343 | return 0; |
| 344 | } |
Kumar Gala | 9f6d55d | 2007-01-20 16:38:26 -0600 | [diff] [blame] | 345 | EXPORT_SYMBOL(phy_ethtool_gset); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 346 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 347 | /** |
| 348 | * phy_mii_ioctl - generic PHY MII ioctl interface |
| 349 | * @phydev: the phy_device struct |
| 350 | * @mii_data: MII ioctl data |
| 351 | * @cmd: ioctl cmd to execute |
| 352 | * |
| 353 | * Note that this function is currently incompatible with the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 354 | * PHYCONTROL layer. It changes registers without regard to |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 355 | * current state. Use at own risk. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 356 | */ |
| 357 | int phy_mii_ioctl(struct phy_device *phydev, |
| 358 | struct mii_ioctl_data *mii_data, int cmd) |
| 359 | { |
| 360 | u16 val = mii_data->val_in; |
| 361 | |
| 362 | switch (cmd) { |
| 363 | case SIOCGMIIPHY: |
| 364 | mii_data->phy_id = phydev->addr; |
| 365 | break; |
| 366 | case SIOCGMIIREG: |
| 367 | mii_data->val_out = phy_read(phydev, mii_data->reg_num); |
| 368 | break; |
| 369 | |
| 370 | case SIOCSMIIREG: |
| 371 | if (!capable(CAP_NET_ADMIN)) |
| 372 | return -EPERM; |
| 373 | |
| 374 | if (mii_data->phy_id == phydev->addr) { |
| 375 | switch(mii_data->reg_num) { |
| 376 | case MII_BMCR: |
| 377 | if (val & (BMCR_RESET|BMCR_ANENABLE)) |
| 378 | phydev->autoneg = AUTONEG_DISABLE; |
| 379 | else |
| 380 | phydev->autoneg = AUTONEG_ENABLE; |
| 381 | if ((!phydev->autoneg) && (val & BMCR_FULLDPLX)) |
| 382 | phydev->duplex = DUPLEX_FULL; |
| 383 | else |
| 384 | phydev->duplex = DUPLEX_HALF; |
| 385 | break; |
| 386 | case MII_ADVERTISE: |
| 387 | phydev->advertising = val; |
| 388 | break; |
| 389 | default: |
| 390 | /* do nothing */ |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | phy_write(phydev, mii_data->reg_num, val); |
| 396 | |
| 397 | if (mii_data->reg_num == MII_BMCR |
| 398 | && val & BMCR_RESET |
| 399 | && phydev->drv->config_init) |
| 400 | phydev->drv->config_init(phydev); |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 407 | /** |
| 408 | * phy_start_aneg - start auto-negotiation for this PHY device |
| 409 | * @phydev: the phy_device struct |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 410 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 411 | * Description: Sanitizes the settings (if we're not autonegotiating |
| 412 | * them), and then calls the driver's config_aneg function. |
| 413 | * If the PHYCONTROL Layer is operating, we change the state to |
| 414 | * reflect the beginning of Auto-negotiation or forcing. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 415 | */ |
| 416 | int phy_start_aneg(struct phy_device *phydev) |
| 417 | { |
| 418 | int err; |
| 419 | |
| 420 | spin_lock(&phydev->lock); |
| 421 | |
| 422 | if (AUTONEG_DISABLE == phydev->autoneg) |
| 423 | phy_sanitize_settings(phydev); |
| 424 | |
| 425 | err = phydev->drv->config_aneg(phydev); |
| 426 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 427 | if (err < 0) |
| 428 | goto out_unlock; |
| 429 | |
| 430 | if (phydev->state != PHY_HALTED) { |
| 431 | if (AUTONEG_ENABLE == phydev->autoneg) { |
| 432 | phydev->state = PHY_AN; |
| 433 | phydev->link_timeout = PHY_AN_TIMEOUT; |
| 434 | } else { |
| 435 | phydev->state = PHY_FORCING; |
| 436 | phydev->link_timeout = PHY_FORCE_TIMEOUT; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | out_unlock: |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 441 | spin_unlock(&phydev->lock); |
| 442 | return err; |
| 443 | } |
| 444 | EXPORT_SYMBOL(phy_start_aneg); |
| 445 | |
| 446 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 447 | static void phy_change(struct work_struct *work); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 448 | static void phy_timer(unsigned long data); |
| 449 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 450 | /** |
| 451 | * phy_start_machine - start PHY state machine tracking |
| 452 | * @phydev: the phy_device struct |
| 453 | * @handler: callback function for state change notifications |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 454 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 455 | * Description: The PHY infrastructure can run a state machine |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 456 | * which tracks whether the PHY is starting up, negotiating, |
| 457 | * etc. This function starts the timer which tracks the state |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 458 | * of the PHY. If you want to be notified when the state changes, |
| 459 | * pass in the callback @handler, otherwise, pass NULL. If you |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 460 | * want to maintain your own state machine, do not call this |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 461 | * function. |
| 462 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 463 | void phy_start_machine(struct phy_device *phydev, |
| 464 | void (*handler)(struct net_device *)) |
| 465 | { |
| 466 | phydev->adjust_state = handler; |
| 467 | |
| 468 | init_timer(&phydev->phy_timer); |
| 469 | phydev->phy_timer.function = &phy_timer; |
| 470 | phydev->phy_timer.data = (unsigned long) phydev; |
| 471 | mod_timer(&phydev->phy_timer, jiffies + HZ); |
| 472 | } |
| 473 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 474 | /** |
| 475 | * phy_stop_machine - stop the PHY state machine tracking |
| 476 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 477 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 478 | * Description: Stops the state machine timer, sets the state to UP |
Sergei Shtylylov | 817acf5 | 2006-07-26 00:53:53 +0400 | [diff] [blame] | 479 | * (unless it wasn't up yet). This function must be called BEFORE |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 480 | * phy_detach. |
| 481 | */ |
| 482 | void phy_stop_machine(struct phy_device *phydev) |
| 483 | { |
| 484 | del_timer_sync(&phydev->phy_timer); |
| 485 | |
| 486 | spin_lock(&phydev->lock); |
| 487 | if (phydev->state > PHY_UP) |
| 488 | phydev->state = PHY_UP; |
| 489 | spin_unlock(&phydev->lock); |
| 490 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 491 | phydev->adjust_state = NULL; |
| 492 | } |
| 493 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 494 | /** |
| 495 | * phy_force_reduction - reduce PHY speed/duplex settings by one step |
| 496 | * @phydev: target phy_device struct |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 497 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 498 | * Description: Reduces the speed/duplex settings by one notch, |
| 499 | * in this order-- |
| 500 | * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF. |
| 501 | * The function bottoms out at 10/HALF. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 502 | */ |
| 503 | static void phy_force_reduction(struct phy_device *phydev) |
| 504 | { |
| 505 | int idx; |
| 506 | |
| 507 | idx = phy_find_setting(phydev->speed, phydev->duplex); |
| 508 | |
| 509 | idx++; |
| 510 | |
| 511 | idx = phy_find_valid(idx, phydev->supported); |
| 512 | |
| 513 | phydev->speed = settings[idx].speed; |
| 514 | phydev->duplex = settings[idx].duplex; |
| 515 | |
| 516 | pr_info("Trying %d/%s\n", phydev->speed, |
| 517 | DUPLEX_FULL == phydev->duplex ? |
| 518 | "FULL" : "HALF"); |
| 519 | } |
| 520 | |
| 521 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 522 | /** |
| 523 | * phy_error - enter HALTED state for this PHY device |
| 524 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 525 | * |
| 526 | * Moves the PHY to the HALTED state in response to a read |
| 527 | * or write error, and tells the controller the link is down. |
| 528 | * Must not be called from interrupt context, or while the |
| 529 | * phydev->lock is held. |
| 530 | */ |
| 531 | void phy_error(struct phy_device *phydev) |
| 532 | { |
| 533 | spin_lock(&phydev->lock); |
| 534 | phydev->state = PHY_HALTED; |
| 535 | spin_unlock(&phydev->lock); |
| 536 | } |
| 537 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 538 | /** |
| 539 | * phy_interrupt - PHY interrupt handler |
| 540 | * @irq: interrupt line |
| 541 | * @phy_dat: phy_device pointer |
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 | * Description: When a PHY interrupt occurs, the handler disables |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 544 | * interrupts, and schedules a work task to clear the interrupt. |
| 545 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 546 | static irqreturn_t phy_interrupt(int irq, void *phy_dat) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 547 | { |
| 548 | struct phy_device *phydev = phy_dat; |
| 549 | |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 550 | if (PHY_HALTED == phydev->state) |
| 551 | return IRQ_NONE; /* It can't be ours. */ |
| 552 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 553 | /* The MDIO bus is not allowed to be written in interrupt |
| 554 | * context, so we need to disable the irq here. A work |
| 555 | * queue will write the PHY to disable and clear the |
| 556 | * interrupt, and then reenable the irq line. */ |
| 557 | disable_irq_nosync(irq); |
| 558 | |
| 559 | schedule_work(&phydev->phy_queue); |
| 560 | |
| 561 | return IRQ_HANDLED; |
| 562 | } |
| 563 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 564 | /** |
| 565 | * phy_enable_interrupts - Enable the interrupts from the PHY side |
| 566 | * @phydev: target phy_device struct |
| 567 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 568 | int phy_enable_interrupts(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 569 | { |
| 570 | int err; |
| 571 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 572 | err = phy_clear_interrupt(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 573 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 574 | if (err < 0) |
| 575 | return err; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 576 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 577 | err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 578 | |
| 579 | return err; |
| 580 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 581 | EXPORT_SYMBOL(phy_enable_interrupts); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 582 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 583 | /** |
| 584 | * phy_disable_interrupts - Disable the PHY interrupts from the PHY side |
| 585 | * @phydev: target phy_device struct |
| 586 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 587 | int phy_disable_interrupts(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 588 | { |
| 589 | int err; |
| 590 | |
| 591 | /* Disable PHY interrupts */ |
| 592 | err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); |
| 593 | |
| 594 | if (err) |
| 595 | goto phy_err; |
| 596 | |
| 597 | /* Clear the interrupt */ |
| 598 | err = phy_clear_interrupt(phydev); |
| 599 | |
| 600 | if (err) |
| 601 | goto phy_err; |
| 602 | |
| 603 | return 0; |
| 604 | |
| 605 | phy_err: |
| 606 | phy_error(phydev); |
| 607 | |
| 608 | return err; |
| 609 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 610 | EXPORT_SYMBOL(phy_disable_interrupts); |
| 611 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 612 | /** |
| 613 | * phy_start_interrupts - request and enable interrupts for a PHY device |
| 614 | * @phydev: target phy_device struct |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 615 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 616 | * Description: Request the interrupt for the given PHY. |
| 617 | * If this fails, then we set irq to PHY_POLL. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 618 | * Otherwise, we enable the interrupts in the PHY. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 619 | * This should only be called with a valid IRQ number. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 620 | * Returns 0 on success or < 0 on error. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 621 | */ |
| 622 | int phy_start_interrupts(struct phy_device *phydev) |
| 623 | { |
| 624 | int err = 0; |
| 625 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 626 | INIT_WORK(&phydev->phy_queue, phy_change); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 627 | |
| 628 | if (request_irq(phydev->irq, phy_interrupt, |
Thomas Gleixner | 1fb9df5 | 2006-07-01 19:29:39 -0700 | [diff] [blame] | 629 | IRQF_SHARED, |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 630 | "phy_interrupt", |
| 631 | phydev) < 0) { |
| 632 | printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n", |
| 633 | phydev->bus->name, |
| 634 | phydev->irq); |
| 635 | phydev->irq = PHY_POLL; |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | err = phy_enable_interrupts(phydev); |
| 640 | |
| 641 | return err; |
| 642 | } |
| 643 | EXPORT_SYMBOL(phy_start_interrupts); |
| 644 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 645 | /** |
| 646 | * phy_stop_interrupts - disable interrupts from a PHY device |
| 647 | * @phydev: target phy_device struct |
| 648 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 649 | int phy_stop_interrupts(struct phy_device *phydev) |
| 650 | { |
| 651 | int err; |
| 652 | |
| 653 | err = phy_disable_interrupts(phydev); |
| 654 | |
| 655 | if (err) |
| 656 | phy_error(phydev); |
| 657 | |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 658 | /* |
| 659 | * Finish any pending work; we might have been scheduled |
| 660 | * to be called from keventd ourselves, though. |
| 661 | */ |
Linus Torvalds | 68380b5 | 2006-12-07 09:28:19 -0800 | [diff] [blame] | 662 | run_scheduled_work(&phydev->phy_queue); |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 663 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 664 | free_irq(phydev->irq, phydev); |
| 665 | |
| 666 | return err; |
| 667 | } |
| 668 | EXPORT_SYMBOL(phy_stop_interrupts); |
| 669 | |
| 670 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 671 | /** |
| 672 | * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes |
| 673 | * @work: work_struct that describes the work to be done |
| 674 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 675 | static void phy_change(struct work_struct *work) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 676 | { |
| 677 | int err; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 678 | struct phy_device *phydev = |
| 679 | container_of(work, struct phy_device, phy_queue); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 680 | |
| 681 | err = phy_disable_interrupts(phydev); |
| 682 | |
| 683 | if (err) |
| 684 | goto phy_err; |
| 685 | |
| 686 | spin_lock(&phydev->lock); |
| 687 | if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state)) |
| 688 | phydev->state = PHY_CHANGELINK; |
| 689 | spin_unlock(&phydev->lock); |
| 690 | |
| 691 | enable_irq(phydev->irq); |
| 692 | |
| 693 | /* Reenable interrupts */ |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 694 | if (PHY_HALTED != phydev->state) |
| 695 | err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 696 | |
| 697 | if (err) |
| 698 | goto irq_enable_err; |
| 699 | |
| 700 | return; |
| 701 | |
| 702 | irq_enable_err: |
| 703 | disable_irq(phydev->irq); |
| 704 | phy_err: |
| 705 | phy_error(phydev); |
| 706 | } |
| 707 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 708 | /** |
| 709 | * phy_stop - Bring down the PHY link, and stop checking the status |
| 710 | * @phydev: target phy_device struct |
| 711 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 712 | void phy_stop(struct phy_device *phydev) |
| 713 | { |
| 714 | spin_lock(&phydev->lock); |
| 715 | |
| 716 | if (PHY_HALTED == phydev->state) |
| 717 | goto out_unlock; |
| 718 | |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 719 | phydev->state = PHY_HALTED; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 720 | |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 721 | if (phydev->irq != PHY_POLL) { |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 722 | /* Disable PHY Interrupts */ |
| 723 | phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 724 | |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 725 | /* Clear any pending interrupts */ |
| 726 | phy_clear_interrupt(phydev); |
| 727 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 728 | |
| 729 | out_unlock: |
| 730 | spin_unlock(&phydev->lock); |
Maciej W. Rozycki | 3c3070d | 2006-10-03 16:18:35 +0100 | [diff] [blame] | 731 | |
| 732 | /* |
| 733 | * Cannot call flush_scheduled_work() here as desired because |
| 734 | * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change() |
| 735 | * will not reenable interrupts. |
| 736 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 740 | /** |
| 741 | * phy_start - start or restart a PHY device |
| 742 | * @phydev: target phy_device struct |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 743 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 744 | * Description: Indicates the attached device's readiness to |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 745 | * handle PHY-related work. Used during startup to start the |
| 746 | * PHY, and after a call to phy_stop() to resume operation. |
| 747 | * Also used to indicate the MDIO bus has cleared an error |
| 748 | * condition. |
| 749 | */ |
| 750 | void phy_start(struct phy_device *phydev) |
| 751 | { |
| 752 | spin_lock(&phydev->lock); |
| 753 | |
| 754 | switch (phydev->state) { |
| 755 | case PHY_STARTING: |
| 756 | phydev->state = PHY_PENDING; |
| 757 | break; |
| 758 | case PHY_READY: |
| 759 | phydev->state = PHY_UP; |
| 760 | break; |
| 761 | case PHY_HALTED: |
| 762 | phydev->state = PHY_RESUMING; |
| 763 | default: |
| 764 | break; |
| 765 | } |
| 766 | spin_unlock(&phydev->lock); |
| 767 | } |
| 768 | EXPORT_SYMBOL(phy_stop); |
| 769 | EXPORT_SYMBOL(phy_start); |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 770 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 771 | /* PHY timer which handles the state machine */ |
| 772 | static void phy_timer(unsigned long data) |
| 773 | { |
| 774 | struct phy_device *phydev = (struct phy_device *)data; |
| 775 | int needs_aneg = 0; |
| 776 | int err = 0; |
| 777 | |
| 778 | spin_lock(&phydev->lock); |
| 779 | |
| 780 | if (phydev->adjust_state) |
| 781 | phydev->adjust_state(phydev->attached_dev); |
| 782 | |
| 783 | switch(phydev->state) { |
| 784 | case PHY_DOWN: |
| 785 | case PHY_STARTING: |
| 786 | case PHY_READY: |
| 787 | case PHY_PENDING: |
| 788 | break; |
| 789 | case PHY_UP: |
| 790 | needs_aneg = 1; |
| 791 | |
| 792 | phydev->link_timeout = PHY_AN_TIMEOUT; |
| 793 | |
| 794 | break; |
| 795 | case PHY_AN: |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 796 | err = phy_read_status(phydev); |
| 797 | |
| 798 | if (err < 0) |
| 799 | break; |
| 800 | |
| 801 | /* If the link is down, give up on |
| 802 | * negotiation for now */ |
| 803 | if (!phydev->link) { |
| 804 | phydev->state = PHY_NOLINK; |
| 805 | netif_carrier_off(phydev->attached_dev); |
| 806 | phydev->adjust_link(phydev->attached_dev); |
| 807 | break; |
| 808 | } |
| 809 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 810 | /* Check if negotiation is done. Break |
| 811 | * if there's an error */ |
| 812 | err = phy_aneg_done(phydev); |
| 813 | if (err < 0) |
| 814 | break; |
| 815 | |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 816 | /* If AN is done, we're running */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 817 | if (err > 0) { |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 818 | phydev->state = PHY_RUNNING; |
| 819 | netif_carrier_on(phydev->attached_dev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 820 | phydev->adjust_link(phydev->attached_dev); |
| 821 | |
| 822 | } else if (0 == phydev->link_timeout--) { |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 823 | int idx; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 824 | |
| 825 | needs_aneg = 1; |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 826 | /* If we have the magic_aneg bit, |
| 827 | * we try again */ |
| 828 | if (phydev->drv->flags & PHY_HAS_MAGICANEG) |
| 829 | break; |
| 830 | |
| 831 | /* The timer expired, and we still |
| 832 | * don't have a setting, so we try |
| 833 | * forcing it until we find one that |
| 834 | * works, starting from the fastest speed, |
| 835 | * and working our way down */ |
| 836 | idx = phy_find_valid(0, phydev->supported); |
| 837 | |
| 838 | phydev->speed = settings[idx].speed; |
| 839 | phydev->duplex = settings[idx].duplex; |
| 840 | |
| 841 | phydev->autoneg = AUTONEG_DISABLE; |
| 842 | |
| 843 | pr_info("Trying %d/%s\n", phydev->speed, |
| 844 | DUPLEX_FULL == |
| 845 | phydev->duplex ? |
| 846 | "FULL" : "HALF"); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 847 | } |
| 848 | break; |
| 849 | case PHY_NOLINK: |
| 850 | err = phy_read_status(phydev); |
| 851 | |
| 852 | if (err) |
| 853 | break; |
| 854 | |
| 855 | if (phydev->link) { |
| 856 | phydev->state = PHY_RUNNING; |
| 857 | netif_carrier_on(phydev->attached_dev); |
| 858 | phydev->adjust_link(phydev->attached_dev); |
| 859 | } |
| 860 | break; |
| 861 | case PHY_FORCING: |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 862 | err = genphy_update_link(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 863 | |
| 864 | if (err) |
| 865 | break; |
| 866 | |
| 867 | if (phydev->link) { |
| 868 | phydev->state = PHY_RUNNING; |
| 869 | netif_carrier_on(phydev->attached_dev); |
| 870 | } else { |
| 871 | if (0 == phydev->link_timeout--) { |
| 872 | phy_force_reduction(phydev); |
| 873 | needs_aneg = 1; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | phydev->adjust_link(phydev->attached_dev); |
| 878 | break; |
| 879 | case PHY_RUNNING: |
| 880 | /* Only register a CHANGE if we are |
| 881 | * polling */ |
| 882 | if (PHY_POLL == phydev->irq) |
| 883 | phydev->state = PHY_CHANGELINK; |
| 884 | break; |
| 885 | case PHY_CHANGELINK: |
| 886 | err = phy_read_status(phydev); |
| 887 | |
| 888 | if (err) |
| 889 | break; |
| 890 | |
| 891 | if (phydev->link) { |
| 892 | phydev->state = PHY_RUNNING; |
| 893 | netif_carrier_on(phydev->attached_dev); |
| 894 | } else { |
| 895 | phydev->state = PHY_NOLINK; |
| 896 | netif_carrier_off(phydev->attached_dev); |
| 897 | } |
| 898 | |
| 899 | phydev->adjust_link(phydev->attached_dev); |
| 900 | |
| 901 | if (PHY_POLL != phydev->irq) |
| 902 | err = phy_config_interrupt(phydev, |
| 903 | PHY_INTERRUPT_ENABLED); |
| 904 | break; |
| 905 | case PHY_HALTED: |
| 906 | if (phydev->link) { |
| 907 | phydev->link = 0; |
| 908 | netif_carrier_off(phydev->attached_dev); |
| 909 | phydev->adjust_link(phydev->attached_dev); |
| 910 | } |
| 911 | break; |
| 912 | case PHY_RESUMING: |
| 913 | |
| 914 | err = phy_clear_interrupt(phydev); |
| 915 | |
| 916 | if (err) |
| 917 | break; |
| 918 | |
| 919 | err = phy_config_interrupt(phydev, |
| 920 | PHY_INTERRUPT_ENABLED); |
| 921 | |
| 922 | if (err) |
| 923 | break; |
| 924 | |
| 925 | if (AUTONEG_ENABLE == phydev->autoneg) { |
| 926 | err = phy_aneg_done(phydev); |
| 927 | if (err < 0) |
| 928 | break; |
| 929 | |
| 930 | /* err > 0 if AN is done. |
| 931 | * Otherwise, it's 0, and we're |
| 932 | * still waiting for AN */ |
| 933 | if (err > 0) { |
| 934 | phydev->state = PHY_RUNNING; |
| 935 | } else { |
| 936 | phydev->state = PHY_AN; |
| 937 | phydev->link_timeout = PHY_AN_TIMEOUT; |
| 938 | } |
| 939 | } else |
| 940 | phydev->state = PHY_RUNNING; |
| 941 | break; |
| 942 | } |
| 943 | |
| 944 | spin_unlock(&phydev->lock); |
| 945 | |
| 946 | if (needs_aneg) |
| 947 | err = phy_start_aneg(phydev); |
| 948 | |
| 949 | if (err < 0) |
| 950 | phy_error(phydev); |
| 951 | |
| 952 | mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ); |
| 953 | } |
| 954 | |