1 /* Framework for configuring and reading PHY devices
2 * Based on code in sungem_phy.c and gianfar_phy.c
6 * Copyright (c) 2004 Freescale Semiconductor, Inc.
7 * Copyright (c) 2006, 2007 Maciej W. Rozycki
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.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/unistd.h>
22 #include <linux/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/skbuff.h>
28 #include <linux/module.h>
29 #include <linux/mii.h>
30 #include <linux/ethtool.h>
31 #include <linux/phy.h>
32 #include <linux/phy_led_triggers.h>
33 #include <linux/workqueue.h>
34 #include <linux/mdio.h>
36 #include <linux/uaccess.h>
37 #include <linux/atomic.h>
41 #define PHY_STATE_STR(_state) \
43 return __stringify(_state); \
45 static const char *phy_state_to_str(enum phy_state st)
49 PHY_STATE_STR(STARTING)
51 PHY_STATE_STR(PENDING)
53 PHY_STATE_STR(RUNNING)
55 PHY_STATE_STR(FORCING)
56 PHY_STATE_STR(CHANGELINK)
58 PHY_STATE_STR(RESUMING)
64 static void phy_link_up(struct phy_device *phydev)
66 phydev->phy_link_change(phydev, true, true);
67 phy_led_trigger_change_speed(phydev);
70 static void phy_link_down(struct phy_device *phydev, bool do_carrier)
72 phydev->phy_link_change(phydev, false, do_carrier);
73 phy_led_trigger_change_speed(phydev);
77 * phy_print_status - Convenience function to print out the current phy status
78 * @phydev: the phy_device struct
80 void phy_print_status(struct phy_device *phydev)
83 netdev_info(phydev->attached_dev,
84 "Link is Up - %s/%s - flow control %s\n",
85 phy_speed_to_str(phydev->speed),
86 phy_duplex_to_str(phydev->duplex),
87 phydev->pause ? "rx/tx" : "off");
89 netdev_info(phydev->attached_dev, "Link is Down\n");
92 EXPORT_SYMBOL(phy_print_status);
95 * phy_clear_interrupt - Ack the phy device's interrupt
96 * @phydev: the phy_device struct
98 * If the @phydev driver has an ack_interrupt function, call it to
99 * ack and clear the phy device's interrupt.
101 * Returns 0 on success or < 0 on error.
103 static int phy_clear_interrupt(struct phy_device *phydev)
105 if (phydev->drv->ack_interrupt)
106 return phydev->drv->ack_interrupt(phydev);
112 * phy_config_interrupt - configure the PHY device for the requested interrupts
113 * @phydev: the phy_device struct
114 * @interrupts: interrupt flags to configure for this @phydev
116 * Returns 0 on success or < 0 on error.
118 static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
120 phydev->interrupts = interrupts ? 1 : 0;
121 if (phydev->drv->config_intr)
122 return phydev->drv->config_intr(phydev);
128 * phy_restart_aneg - restart auto-negotiation
129 * @phydev: target phy_device struct
131 * Restart the autonegotiation on @phydev. Returns >= 0 on success or
132 * negative errno on error.
134 int phy_restart_aneg(struct phy_device *phydev)
138 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
139 ret = genphy_c45_restart_aneg(phydev);
141 ret = genphy_restart_aneg(phydev);
145 EXPORT_SYMBOL_GPL(phy_restart_aneg);
148 * phy_aneg_done - return auto-negotiation status
149 * @phydev: target phy_device struct
151 * Description: Return the auto-negotiation status from this @phydev
152 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
155 int phy_aneg_done(struct phy_device *phydev)
157 if (phydev->drv && phydev->drv->aneg_done)
158 return phydev->drv->aneg_done(phydev);
160 /* Avoid genphy_aneg_done() if the Clause 45 PHY does not
161 * implement Clause 22 registers
163 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
166 return genphy_aneg_done(phydev);
168 EXPORT_SYMBOL(phy_aneg_done);
171 * phy_find_valid - find a PHY setting that matches the requested parameters
172 * @speed: desired speed
173 * @duplex: desired duplex
174 * @supported: mask of supported link modes
176 * Locate a supported phy setting that is, in priority order:
177 * - an exact match for the specified speed and duplex mode
178 * - a match for the specified speed, or slower speed
179 * - the slowest supported speed
180 * Returns the matched phy_setting entry, or %NULL if no supported phy
181 * settings were found.
183 static const struct phy_setting *
184 phy_find_valid(int speed, int duplex, u32 supported)
186 unsigned long mask = supported;
188 return phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, false);
192 * phy_supported_speeds - return all speeds currently supported by a phy device
193 * @phy: The phy device to return supported speeds of.
194 * @speeds: buffer to store supported speeds in.
195 * @size: size of speeds buffer.
197 * Description: Returns the number of supported speeds, and fills the speeds
198 * buffer with the supported speeds. If speeds buffer is too small to contain
199 * all currently supported speeds, will return as many speeds as can fit.
201 unsigned int phy_supported_speeds(struct phy_device *phy,
202 unsigned int *speeds,
205 unsigned long supported = phy->supported;
207 return phy_speeds(speeds, size, &supported, BITS_PER_LONG);
211 * phy_check_valid - check if there is a valid PHY setting which matches
212 * speed, duplex, and feature mask
213 * @speed: speed to match
214 * @duplex: duplex to match
215 * @features: A mask of the valid settings
217 * Description: Returns true if there is a valid setting, false otherwise.
219 static inline bool phy_check_valid(int speed, int duplex, u32 features)
221 unsigned long mask = features;
223 return !!phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, true);
227 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
228 * @phydev: the target phy_device struct
230 * Description: Make sure the PHY is set to supported speeds and
231 * duplexes. Drop down by one in this order: 1000/FULL,
232 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
234 static void phy_sanitize_settings(struct phy_device *phydev)
236 const struct phy_setting *setting;
237 u32 features = phydev->supported;
239 /* Sanitize settings based on PHY capabilities */
240 if ((features & SUPPORTED_Autoneg) == 0)
241 phydev->autoneg = AUTONEG_DISABLE;
243 setting = phy_find_valid(phydev->speed, phydev->duplex, features);
245 phydev->speed = setting->speed;
246 phydev->duplex = setting->duplex;
248 /* We failed to find anything (no supported speeds?) */
249 phydev->speed = SPEED_UNKNOWN;
250 phydev->duplex = DUPLEX_UNKNOWN;
255 * phy_ethtool_sset - generic ethtool sset function, handles all the details
256 * @phydev: target phy_device struct
259 * A few notes about parameter checking:
261 * - We don't set port or transceiver, so we don't care what they
263 * - phy_start_aneg() will make sure forced settings are sane, and
264 * choose the next best ones from the ones selected, so we don't
265 * care if ethtool tries to give us bad values.
267 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
269 u32 speed = ethtool_cmd_speed(cmd);
271 if (cmd->phy_address != phydev->mdio.addr)
274 /* We make sure that we don't pass unsupported values in to the PHY */
275 cmd->advertising &= phydev->supported;
277 /* Verify the settings we care about. */
278 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
281 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
284 if (cmd->autoneg == AUTONEG_DISABLE &&
285 ((speed != SPEED_1000 &&
286 speed != SPEED_100 &&
287 speed != SPEED_10) ||
288 (cmd->duplex != DUPLEX_HALF &&
289 cmd->duplex != DUPLEX_FULL)))
292 phydev->autoneg = cmd->autoneg;
294 phydev->speed = speed;
296 phydev->advertising = cmd->advertising;
298 if (AUTONEG_ENABLE == cmd->autoneg)
299 phydev->advertising |= ADVERTISED_Autoneg;
301 phydev->advertising &= ~ADVERTISED_Autoneg;
303 phydev->duplex = cmd->duplex;
305 phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
307 /* Restart the PHY */
308 phy_start_aneg(phydev);
312 EXPORT_SYMBOL(phy_ethtool_sset);
314 int phy_ethtool_ksettings_set(struct phy_device *phydev,
315 const struct ethtool_link_ksettings *cmd)
317 u8 autoneg = cmd->base.autoneg;
318 u8 duplex = cmd->base.duplex;
319 u32 speed = cmd->base.speed;
322 if (cmd->base.phy_address != phydev->mdio.addr)
325 ethtool_convert_link_mode_to_legacy_u32(&advertising,
326 cmd->link_modes.advertising);
328 /* We make sure that we don't pass unsupported values in to the PHY */
329 advertising &= phydev->supported;
331 /* Verify the settings we care about. */
332 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
335 if (autoneg == AUTONEG_ENABLE && advertising == 0)
338 if (autoneg == AUTONEG_DISABLE &&
339 ((speed != SPEED_1000 &&
340 speed != SPEED_100 &&
341 speed != SPEED_10) ||
342 (duplex != DUPLEX_HALF &&
343 duplex != DUPLEX_FULL)))
346 phydev->autoneg = autoneg;
348 phydev->speed = speed;
350 phydev->advertising = advertising;
352 if (autoneg == AUTONEG_ENABLE)
353 phydev->advertising |= ADVERTISED_Autoneg;
355 phydev->advertising &= ~ADVERTISED_Autoneg;
357 phydev->duplex = duplex;
359 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
361 /* Restart the PHY */
362 phy_start_aneg(phydev);
366 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
368 void phy_ethtool_ksettings_get(struct phy_device *phydev,
369 struct ethtool_link_ksettings *cmd)
371 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
374 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
375 phydev->advertising);
377 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
378 phydev->lp_advertising);
380 cmd->base.speed = phydev->speed;
381 cmd->base.duplex = phydev->duplex;
382 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
383 cmd->base.port = PORT_BNC;
385 cmd->base.port = PORT_MII;
386 cmd->base.transceiver = phy_is_internal(phydev) ?
387 XCVR_INTERNAL : XCVR_EXTERNAL;
388 cmd->base.phy_address = phydev->mdio.addr;
389 cmd->base.autoneg = phydev->autoneg;
390 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
391 cmd->base.eth_tp_mdix = phydev->mdix;
393 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
396 * phy_mii_ioctl - generic PHY MII ioctl interface
397 * @phydev: the phy_device struct
398 * @ifr: &struct ifreq for socket ioctl's
399 * @cmd: ioctl cmd to execute
401 * Note that this function is currently incompatible with the
402 * PHYCONTROL layer. It changes registers without regard to
403 * current state. Use at own risk.
405 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
407 struct mii_ioctl_data *mii_data = if_mii(ifr);
408 u16 val = mii_data->val_in;
409 bool change_autoneg = false;
413 mii_data->phy_id = phydev->mdio.addr;
417 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
423 if (mii_data->phy_id == phydev->mdio.addr) {
424 switch (mii_data->reg_num) {
426 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
427 if (phydev->autoneg == AUTONEG_ENABLE)
428 change_autoneg = true;
429 phydev->autoneg = AUTONEG_DISABLE;
430 if (val & BMCR_FULLDPLX)
431 phydev->duplex = DUPLEX_FULL;
433 phydev->duplex = DUPLEX_HALF;
434 if (val & BMCR_SPEED1000)
435 phydev->speed = SPEED_1000;
436 else if (val & BMCR_SPEED100)
437 phydev->speed = SPEED_100;
438 else phydev->speed = SPEED_10;
441 if (phydev->autoneg == AUTONEG_DISABLE)
442 change_autoneg = true;
443 phydev->autoneg = AUTONEG_ENABLE;
447 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
448 change_autoneg = true;
456 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
457 mii_data->reg_num, val);
459 if (mii_data->phy_id == phydev->mdio.addr &&
460 mii_data->reg_num == MII_BMCR &&
462 return phy_init_hw(phydev);
465 return phy_start_aneg(phydev);
470 if (phydev->drv && phydev->drv->hwtstamp)
471 return phydev->drv->hwtstamp(phydev, ifr);
478 EXPORT_SYMBOL(phy_mii_ioctl);
480 static void phy_queue_state_machine(struct phy_device *phydev,
483 mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
487 static void phy_trigger_machine(struct phy_device *phydev)
489 phy_queue_state_machine(phydev, 0);
492 static int phy_config_aneg(struct phy_device *phydev)
494 if (phydev->drv->config_aneg)
495 return phydev->drv->config_aneg(phydev);
497 /* Clause 45 PHYs that don't implement Clause 22 registers are not
498 * allowed to call genphy_config_aneg()
500 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
503 return genphy_config_aneg(phydev);
507 * phy_check_link_status - check link status and set state accordingly
508 * @phydev: the phy_device struct
510 * Description: Check for link and whether autoneg was triggered / is running
511 * and set state accordingly
513 static int phy_check_link_status(struct phy_device *phydev)
517 WARN_ON(!mutex_is_locked(&phydev->lock));
519 err = phy_read_status(phydev);
523 if (phydev->link && phydev->state != PHY_RUNNING) {
524 phydev->state = PHY_RUNNING;
526 } else if (!phydev->link && phydev->state != PHY_NOLINK) {
527 phydev->state = PHY_NOLINK;
528 phy_link_down(phydev, true);
535 * phy_start_aneg - start auto-negotiation for this PHY device
536 * @phydev: the phy_device struct
538 * Description: Sanitizes the settings (if we're not autonegotiating
539 * them), and then calls the driver's config_aneg function.
540 * If the PHYCONTROL Layer is operating, we change the state to
541 * reflect the beginning of Auto-negotiation or forcing.
543 int phy_start_aneg(struct phy_device *phydev)
550 mutex_lock(&phydev->lock);
552 if (AUTONEG_DISABLE == phydev->autoneg)
553 phy_sanitize_settings(phydev);
555 /* Invalidate LP advertising flags */
556 phydev->lp_advertising = 0;
558 err = phy_config_aneg(phydev);
562 if (phydev->state != PHY_HALTED) {
563 if (AUTONEG_ENABLE == phydev->autoneg) {
564 err = phy_check_link_status(phydev);
566 phydev->state = PHY_FORCING;
567 phydev->link_timeout = PHY_FORCE_TIMEOUT;
572 mutex_unlock(&phydev->lock);
576 EXPORT_SYMBOL(phy_start_aneg);
578 static int phy_poll_aneg_done(struct phy_device *phydev)
580 unsigned int retries = 100;
585 ret = phy_aneg_done(phydev);
586 } while (!ret && --retries);
591 return ret < 0 ? ret : 0;
595 * phy_speed_down - set speed to lowest speed supported by both link partners
596 * @phydev: the phy_device struct
597 * @sync: perform action synchronously
599 * Description: Typically used to save energy when waiting for a WoL packet
601 * WARNING: Setting sync to false may cause the system being unable to suspend
602 * in case the PHY generates an interrupt when finishing the autonegotiation.
603 * This interrupt may wake up the system immediately after suspend.
604 * Therefore use sync = false only if you're sure it's safe with the respective
607 int phy_speed_down(struct phy_device *phydev, bool sync)
609 u32 adv = phydev->lp_advertising & phydev->supported;
610 u32 adv_old = phydev->advertising;
613 if (phydev->autoneg != AUTONEG_ENABLE)
616 if (adv & PHY_10BT_FEATURES)
617 phydev->advertising &= ~(PHY_100BT_FEATURES |
618 PHY_1000BT_FEATURES);
619 else if (adv & PHY_100BT_FEATURES)
620 phydev->advertising &= ~PHY_1000BT_FEATURES;
622 if (phydev->advertising == adv_old)
625 ret = phy_config_aneg(phydev);
629 return sync ? phy_poll_aneg_done(phydev) : 0;
631 EXPORT_SYMBOL_GPL(phy_speed_down);
634 * phy_speed_up - (re)set advertised speeds to all supported speeds
635 * @phydev: the phy_device struct
637 * Description: Used to revert the effect of phy_speed_down
639 int phy_speed_up(struct phy_device *phydev)
641 u32 mask = PHY_10BT_FEATURES | PHY_100BT_FEATURES | PHY_1000BT_FEATURES;
642 u32 adv_old = phydev->advertising;
644 if (phydev->autoneg != AUTONEG_ENABLE)
647 phydev->advertising = (adv_old & ~mask) | (phydev->supported & mask);
649 if (phydev->advertising == adv_old)
652 return phy_config_aneg(phydev);
654 EXPORT_SYMBOL_GPL(phy_speed_up);
657 * phy_start_machine - start PHY state machine tracking
658 * @phydev: the phy_device struct
660 * Description: The PHY infrastructure can run a state machine
661 * which tracks whether the PHY is starting up, negotiating,
662 * etc. This function starts the delayed workqueue which tracks
663 * the state of the PHY. If you want to maintain your own state machine,
664 * do not call this function.
666 void phy_start_machine(struct phy_device *phydev)
668 phy_trigger_machine(phydev);
670 EXPORT_SYMBOL_GPL(phy_start_machine);
673 * phy_stop_machine - stop the PHY state machine tracking
674 * @phydev: target phy_device struct
676 * Description: Stops the state machine delayed workqueue, sets the
677 * state to UP (unless it wasn't up yet). This function must be
678 * called BEFORE phy_detach.
680 void phy_stop_machine(struct phy_device *phydev)
682 cancel_delayed_work_sync(&phydev->state_queue);
684 mutex_lock(&phydev->lock);
685 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
686 phydev->state = PHY_UP;
687 mutex_unlock(&phydev->lock);
691 * phy_error - enter HALTED state for this PHY device
692 * @phydev: target phy_device struct
694 * Moves the PHY to the HALTED state in response to a read
695 * or write error, and tells the controller the link is down.
696 * Must not be called from interrupt context, or while the
697 * phydev->lock is held.
699 static void phy_error(struct phy_device *phydev)
701 mutex_lock(&phydev->lock);
702 phydev->state = PHY_HALTED;
703 mutex_unlock(&phydev->lock);
705 phy_trigger_machine(phydev);
709 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
710 * @phydev: target phy_device struct
712 static int phy_disable_interrupts(struct phy_device *phydev)
716 /* Disable PHY interrupts */
717 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
721 /* Clear the interrupt */
722 return phy_clear_interrupt(phydev);
726 * phy_change - Called by the phy_interrupt to handle PHY changes
727 * @phydev: phy_device struct that interrupted
729 static irqreturn_t phy_change(struct phy_device *phydev)
731 if (phy_interrupt_is_valid(phydev)) {
732 if (phydev->drv->did_interrupt &&
733 !phydev->drv->did_interrupt(phydev))
736 if (phydev->state == PHY_HALTED)
737 if (phy_disable_interrupts(phydev))
741 mutex_lock(&phydev->lock);
742 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
743 phydev->state = PHY_CHANGELINK;
744 mutex_unlock(&phydev->lock);
746 /* reschedule state queue work to run as soon as possible */
747 phy_trigger_machine(phydev);
749 if (phy_interrupt_is_valid(phydev) && phy_clear_interrupt(phydev))
759 * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
760 * @work: work_struct that describes the work to be done
762 void phy_change_work(struct work_struct *work)
764 struct phy_device *phydev =
765 container_of(work, struct phy_device, phy_queue);
771 * phy_interrupt - PHY interrupt handler
772 * @irq: interrupt line
773 * @phy_dat: phy_device pointer
775 * Description: When a PHY interrupt occurs, the handler disables
776 * interrupts, and uses phy_change to handle the interrupt.
778 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
780 struct phy_device *phydev = phy_dat;
782 if (PHY_HALTED == phydev->state)
783 return IRQ_NONE; /* It can't be ours. */
785 return phy_change(phydev);
789 * phy_enable_interrupts - Enable the interrupts from the PHY side
790 * @phydev: target phy_device struct
792 static int phy_enable_interrupts(struct phy_device *phydev)
794 int err = phy_clear_interrupt(phydev);
799 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
803 * phy_start_interrupts - request and enable interrupts for a PHY device
804 * @phydev: target phy_device struct
806 * Description: Request the interrupt for the given PHY.
807 * If this fails, then we set irq to PHY_POLL.
808 * Otherwise, we enable the interrupts in the PHY.
809 * This should only be called with a valid IRQ number.
810 * Returns 0 on success or < 0 on error.
812 int phy_start_interrupts(struct phy_device *phydev)
814 if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
815 IRQF_ONESHOT | IRQF_SHARED,
816 phydev_name(phydev), phydev) < 0) {
817 pr_warn("%s: Can't get IRQ %d (PHY)\n",
818 phydev->mdio.bus->name, phydev->irq);
819 phydev->irq = PHY_POLL;
823 return phy_enable_interrupts(phydev);
825 EXPORT_SYMBOL(phy_start_interrupts);
828 * phy_stop_interrupts - disable interrupts from a PHY device
829 * @phydev: target phy_device struct
831 int phy_stop_interrupts(struct phy_device *phydev)
833 int err = phy_disable_interrupts(phydev);
838 free_irq(phydev->irq, phydev);
842 EXPORT_SYMBOL(phy_stop_interrupts);
845 * phy_stop - Bring down the PHY link, and stop checking the status
846 * @phydev: target phy_device struct
848 void phy_stop(struct phy_device *phydev)
850 mutex_lock(&phydev->lock);
852 if (PHY_HALTED == phydev->state)
855 if (phy_interrupt_is_valid(phydev))
856 phy_disable_interrupts(phydev);
858 phydev->state = PHY_HALTED;
861 mutex_unlock(&phydev->lock);
863 phy_state_machine(&phydev->state_queue.work);
865 /* Cannot call flush_scheduled_work() here as desired because
866 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
867 * will not reenable interrupts.
870 EXPORT_SYMBOL(phy_stop);
873 * phy_start - start or restart a PHY device
874 * @phydev: target phy_device struct
876 * Description: Indicates the attached device's readiness to
877 * handle PHY-related work. Used during startup to start the
878 * PHY, and after a call to phy_stop() to resume operation.
879 * Also used to indicate the MDIO bus has cleared an error
882 void phy_start(struct phy_device *phydev)
886 mutex_lock(&phydev->lock);
888 switch (phydev->state) {
890 phydev->state = PHY_PENDING;
893 phydev->state = PHY_UP;
896 /* if phy was suspended, bring the physical link up again */
897 __phy_resume(phydev);
899 /* make sure interrupts are re-enabled for the PHY */
900 if (phy_interrupt_is_valid(phydev)) {
901 err = phy_enable_interrupts(phydev);
906 phydev->state = PHY_RESUMING;
911 mutex_unlock(&phydev->lock);
913 phy_trigger_machine(phydev);
915 EXPORT_SYMBOL(phy_start);
918 * phy_state_machine - Handle the state machine
919 * @work: work_struct that describes the work to be done
921 void phy_state_machine(struct work_struct *work)
923 struct delayed_work *dwork = to_delayed_work(work);
924 struct phy_device *phydev =
925 container_of(dwork, struct phy_device, state_queue);
926 bool needs_aneg = false, do_suspend = false;
927 enum phy_state old_state;
930 mutex_lock(&phydev->lock);
932 old_state = phydev->state;
934 if (phydev->drv && phydev->drv->link_change_notify)
935 phydev->drv->link_change_notify(phydev);
937 switch (phydev->state) {
949 if (!phy_polling_mode(phydev))
954 err = phy_check_link_status(phydev);
957 err = genphy_update_link(phydev);
962 phydev->state = PHY_RUNNING;
965 if (0 == phydev->link_timeout--)
967 phy_link_down(phydev, false);
973 phy_link_down(phydev, true);
979 mutex_unlock(&phydev->lock);
982 err = phy_start_aneg(phydev);
989 if (old_state != phydev->state)
990 phydev_dbg(phydev, "PHY state change %s -> %s\n",
991 phy_state_to_str(old_state),
992 phy_state_to_str(phydev->state));
994 /* Only re-schedule a PHY state machine change if we are polling the
995 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
996 * between states from phy_mac_interrupt().
998 * In state PHY_HALTED the PHY gets suspended, so rescheduling the
999 * state machine would be pointless and possibly error prone when
1000 * called from phy_disconnect() synchronously.
1002 if (phy_polling_mode(phydev) && old_state != PHY_HALTED)
1003 phy_queue_state_machine(phydev, PHY_STATE_TIME);
1007 * phy_mac_interrupt - MAC says the link has changed
1008 * @phydev: phy_device struct with changed link
1010 * The MAC layer is able to indicate there has been a change in the PHY link
1011 * status. Trigger the state machine and work a work queue.
1013 void phy_mac_interrupt(struct phy_device *phydev)
1015 /* Trigger a state machine change */
1016 queue_work(system_power_efficient_wq, &phydev->phy_queue);
1018 EXPORT_SYMBOL(phy_mac_interrupt);
1021 * phy_init_eee - init and check the EEE feature
1022 * @phydev: target phy_device struct
1023 * @clk_stop_enable: PHY may stop the clock during LPI
1025 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1026 * is supported by looking at the MMD registers 3.20 and 7.60/61
1027 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1030 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1035 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1037 if (phydev->duplex == DUPLEX_FULL) {
1038 int eee_lp, eee_cap, eee_adv;
1042 /* Read phy status to properly get the right settings */
1043 status = phy_read_status(phydev);
1047 /* First check if the EEE ability is supported */
1048 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1052 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1056 /* Check which link settings negotiated and verify it in
1057 * the EEE advertising registers.
1059 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1063 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1067 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1068 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1069 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
1072 if (clk_stop_enable) {
1073 /* Configure the PHY to stop receiving xMII
1074 * clock while it is signaling LPI.
1076 int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1080 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1081 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
1084 return 0; /* EEE supported */
1087 return -EPROTONOSUPPORT;
1089 EXPORT_SYMBOL(phy_init_eee);
1092 * phy_get_eee_err - report the EEE wake error count
1093 * @phydev: target phy_device struct
1095 * Description: it is to report the number of time where the PHY
1096 * failed to complete its normal wake sequence.
1098 int phy_get_eee_err(struct phy_device *phydev)
1103 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1105 EXPORT_SYMBOL(phy_get_eee_err);
1108 * phy_ethtool_get_eee - get EEE supported and status
1109 * @phydev: target phy_device struct
1110 * @data: ethtool_eee data
1112 * Description: it reportes the Supported/Advertisement/LP Advertisement
1115 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1122 /* Get Supported EEE */
1123 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1126 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1128 /* Get advertisement EEE */
1129 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1132 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1134 /* Get LP advertisement EEE */
1135 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1138 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1142 EXPORT_SYMBOL(phy_ethtool_get_eee);
1145 * phy_ethtool_set_eee - set EEE supported and status
1146 * @phydev: target phy_device struct
1147 * @data: ethtool_eee data
1149 * Description: it is to program the Advertisement EEE register.
1151 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1153 int cap, old_adv, adv, ret;
1158 /* Get Supported EEE */
1159 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1163 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1167 adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1169 /* Mask prohibited EEE modes */
1170 adv &= ~phydev->eee_broken_modes;
1172 if (old_adv != adv) {
1173 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1177 /* Restart autonegotiation so the new modes get sent to the
1180 ret = phy_restart_aneg(phydev);
1187 EXPORT_SYMBOL(phy_ethtool_set_eee);
1189 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1191 if (phydev->drv && phydev->drv->set_wol)
1192 return phydev->drv->set_wol(phydev, wol);
1196 EXPORT_SYMBOL(phy_ethtool_set_wol);
1198 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1200 if (phydev->drv && phydev->drv->get_wol)
1201 phydev->drv->get_wol(phydev, wol);
1203 EXPORT_SYMBOL(phy_ethtool_get_wol);
1205 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1206 struct ethtool_link_ksettings *cmd)
1208 struct phy_device *phydev = ndev->phydev;
1213 phy_ethtool_ksettings_get(phydev, cmd);
1217 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1219 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1220 const struct ethtool_link_ksettings *cmd)
1222 struct phy_device *phydev = ndev->phydev;
1227 return phy_ethtool_ksettings_set(phydev, cmd);
1229 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1231 int phy_ethtool_nway_reset(struct net_device *ndev)
1233 struct phy_device *phydev = ndev->phydev;
1241 return phy_restart_aneg(phydev);
1243 EXPORT_SYMBOL(phy_ethtool_nway_reset);