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/timer.h>
34 #include <linux/workqueue.h>
35 #include <linux/mdio.h>
37 #include <linux/uaccess.h>
38 #include <linux/atomic.h>
42 static const char *phy_speed_to_str(int speed)
72 return "Unsupported (update phy.c)";
76 #define PHY_STATE_STR(_state) \
78 return __stringify(_state); \
80 static const char *phy_state_to_str(enum phy_state st)
84 PHY_STATE_STR(STARTING)
86 PHY_STATE_STR(PENDING)
89 PHY_STATE_STR(RUNNING)
91 PHY_STATE_STR(FORCING)
92 PHY_STATE_STR(CHANGELINK)
94 PHY_STATE_STR(RESUMING)
102 * phy_print_status - Convenience function to print out the current phy status
103 * @phydev: the phy_device struct
105 void phy_print_status(struct phy_device *phydev)
108 netdev_info(phydev->attached_dev,
109 "Link is Up - %s/%s - flow control %s\n",
110 phy_speed_to_str(phydev->speed),
111 DUPLEX_FULL == phydev->duplex ? "Full" : "Half",
112 phydev->pause ? "rx/tx" : "off");
114 netdev_info(phydev->attached_dev, "Link is Down\n");
117 EXPORT_SYMBOL(phy_print_status);
120 * phy_clear_interrupt - Ack the phy device's interrupt
121 * @phydev: the phy_device struct
123 * If the @phydev driver has an ack_interrupt function, call it to
124 * ack and clear the phy device's interrupt.
126 * Returns 0 on success or < 0 on error.
128 static int phy_clear_interrupt(struct phy_device *phydev)
130 if (phydev->drv->ack_interrupt)
131 return phydev->drv->ack_interrupt(phydev);
137 * phy_config_interrupt - configure the PHY device for the requested interrupts
138 * @phydev: the phy_device struct
139 * @interrupts: interrupt flags to configure for this @phydev
141 * Returns 0 on success or < 0 on error.
143 static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
145 phydev->interrupts = interrupts;
146 if (phydev->drv->config_intr)
147 return phydev->drv->config_intr(phydev);
153 * phy_restart_aneg - restart auto-negotiation
154 * @phydev: target phy_device struct
156 * Restart the autonegotiation on @phydev. Returns >= 0 on success or
157 * negative errno on error.
159 int phy_restart_aneg(struct phy_device *phydev)
163 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
164 ret = genphy_c45_restart_aneg(phydev);
166 ret = genphy_restart_aneg(phydev);
170 EXPORT_SYMBOL_GPL(phy_restart_aneg);
173 * phy_aneg_done - return auto-negotiation status
174 * @phydev: target phy_device struct
176 * Description: Return the auto-negotiation status from this @phydev
177 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
180 int phy_aneg_done(struct phy_device *phydev)
182 if (phydev->drv && phydev->drv->aneg_done)
183 return phydev->drv->aneg_done(phydev);
185 /* Avoid genphy_aneg_done() if the Clause 45 PHY does not
186 * implement Clause 22 registers
188 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
191 return genphy_aneg_done(phydev);
193 EXPORT_SYMBOL(phy_aneg_done);
195 /* A structure for mapping a particular speed and duplex
196 * combination to a particular SUPPORTED and ADVERTISED value
204 /* A mapping of all SUPPORTED settings to speed/duplex. This table
205 * must be grouped by speed and sorted in descending match priority
206 * - iow, descending speed. */
207 static const struct phy_setting settings[] = {
209 .speed = SPEED_10000,
210 .duplex = DUPLEX_FULL,
211 .setting = SUPPORTED_10000baseKR_Full,
214 .speed = SPEED_10000,
215 .duplex = DUPLEX_FULL,
216 .setting = SUPPORTED_10000baseKX4_Full,
219 .speed = SPEED_10000,
220 .duplex = DUPLEX_FULL,
221 .setting = SUPPORTED_10000baseT_Full,
225 .duplex = DUPLEX_FULL,
226 .setting = SUPPORTED_2500baseX_Full,
230 .duplex = DUPLEX_FULL,
231 .setting = SUPPORTED_1000baseKX_Full,
235 .duplex = DUPLEX_FULL,
236 .setting = SUPPORTED_1000baseT_Full,
240 .duplex = DUPLEX_HALF,
241 .setting = SUPPORTED_1000baseT_Half,
245 .duplex = DUPLEX_FULL,
246 .setting = SUPPORTED_100baseT_Full,
250 .duplex = DUPLEX_HALF,
251 .setting = SUPPORTED_100baseT_Half,
255 .duplex = DUPLEX_FULL,
256 .setting = SUPPORTED_10baseT_Full,
260 .duplex = DUPLEX_HALF,
261 .setting = SUPPORTED_10baseT_Half,
266 * phy_lookup_setting - lookup a PHY setting
267 * @speed: speed to match
268 * @duplex: duplex to match
269 * @features: allowed link modes
270 * @exact: an exact match is required
272 * Search the settings array for a setting that matches the speed and
273 * duplex, and which is supported.
275 * If @exact is unset, either an exact match or %NULL for no match will
278 * If @exact is set, an exact match, the fastest supported setting at
279 * or below the specified speed, the slowest supported setting, or if
280 * they all fail, %NULL will be returned.
282 static const struct phy_setting *
283 phy_lookup_setting(int speed, int duplex, u32 features, bool exact)
285 const struct phy_setting *p, *match = NULL, *last = NULL;
288 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
289 if (p->setting & features) {
291 if (p->speed == speed && p->duplex == duplex) {
292 /* Exact match for speed and duplex */
296 if (!match && p->speed <= speed)
300 if (p->speed < speed)
306 if (!match && !exact)
313 * phy_find_valid - find a PHY setting that matches the requested parameters
314 * @speed: desired speed
315 * @duplex: desired duplex
316 * @supported: mask of supported link modes
318 * Locate a supported phy setting that is, in priority order:
319 * - an exact match for the specified speed and duplex mode
320 * - a match for the specified speed, or slower speed
321 * - the slowest supported speed
322 * Returns the matched phy_setting entry, or %NULL if no supported phy
323 * settings were found.
325 static const struct phy_setting *
326 phy_find_valid(int speed, int duplex, u32 supported)
328 return phy_lookup_setting(speed, duplex, supported, false);
332 * phy_supported_speeds - return all speeds currently supported by a phy device
333 * @phy: The phy device to return supported speeds of.
334 * @speeds: buffer to store supported speeds in.
335 * @size: size of speeds buffer.
337 * Description: Returns the number of supported speeds, and fills the speeds
338 * buffer with the supported speeds. If speeds buffer is too small to contain
339 * all currently supported speeds, will return as many speeds as can fit.
341 unsigned int phy_supported_speeds(struct phy_device *phy,
342 unsigned int *speeds,
345 unsigned int count = 0;
346 unsigned int idx = 0;
348 for (idx = 0; idx < ARRAY_SIZE(settings) && count < size; idx++)
349 /* Assumes settings are grouped by speed */
350 if ((settings[idx].setting & phy->supported) &&
351 (count == 0 || speeds[count - 1] != settings[idx].speed))
352 speeds[count++] = settings[idx].speed;
358 * phy_check_valid - check if there is a valid PHY setting which matches
359 * speed, duplex, and feature mask
360 * @speed: speed to match
361 * @duplex: duplex to match
362 * @features: A mask of the valid settings
364 * Description: Returns true if there is a valid setting, false otherwise.
366 static inline bool phy_check_valid(int speed, int duplex, u32 features)
368 return !!phy_lookup_setting(speed, duplex, features, true);
372 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
373 * @phydev: the target phy_device struct
375 * Description: Make sure the PHY is set to supported speeds and
376 * duplexes. Drop down by one in this order: 1000/FULL,
377 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
379 static void phy_sanitize_settings(struct phy_device *phydev)
381 const struct phy_setting *setting;
382 u32 features = phydev->supported;
384 /* Sanitize settings based on PHY capabilities */
385 if ((features & SUPPORTED_Autoneg) == 0)
386 phydev->autoneg = AUTONEG_DISABLE;
388 setting = phy_find_valid(phydev->speed, phydev->duplex, features);
390 phydev->speed = setting->speed;
391 phydev->duplex = setting->duplex;
393 /* We failed to find anything (no supported speeds?) */
394 phydev->speed = SPEED_UNKNOWN;
395 phydev->duplex = DUPLEX_UNKNOWN;
400 * phy_ethtool_sset - generic ethtool sset function, handles all the details
401 * @phydev: target phy_device struct
404 * A few notes about parameter checking:
405 * - We don't set port or transceiver, so we don't care what they
407 * - phy_start_aneg() will make sure forced settings are sane, and
408 * choose the next best ones from the ones selected, so we don't
409 * care if ethtool tries to give us bad values.
411 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
413 u32 speed = ethtool_cmd_speed(cmd);
415 if (cmd->phy_address != phydev->mdio.addr)
418 /* We make sure that we don't pass unsupported values in to the PHY */
419 cmd->advertising &= phydev->supported;
421 /* Verify the settings we care about. */
422 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
425 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
428 if (cmd->autoneg == AUTONEG_DISABLE &&
429 ((speed != SPEED_1000 &&
430 speed != SPEED_100 &&
431 speed != SPEED_10) ||
432 (cmd->duplex != DUPLEX_HALF &&
433 cmd->duplex != DUPLEX_FULL)))
436 phydev->autoneg = cmd->autoneg;
438 phydev->speed = speed;
440 phydev->advertising = cmd->advertising;
442 if (AUTONEG_ENABLE == cmd->autoneg)
443 phydev->advertising |= ADVERTISED_Autoneg;
445 phydev->advertising &= ~ADVERTISED_Autoneg;
447 phydev->duplex = cmd->duplex;
449 phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
451 /* Restart the PHY */
452 phy_start_aneg(phydev);
456 EXPORT_SYMBOL(phy_ethtool_sset);
458 int phy_ethtool_ksettings_set(struct phy_device *phydev,
459 const struct ethtool_link_ksettings *cmd)
461 u8 autoneg = cmd->base.autoneg;
462 u8 duplex = cmd->base.duplex;
463 u32 speed = cmd->base.speed;
466 if (cmd->base.phy_address != phydev->mdio.addr)
469 ethtool_convert_link_mode_to_legacy_u32(&advertising,
470 cmd->link_modes.advertising);
472 /* We make sure that we don't pass unsupported values in to the PHY */
473 advertising &= phydev->supported;
475 /* Verify the settings we care about. */
476 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
479 if (autoneg == AUTONEG_ENABLE && advertising == 0)
482 if (autoneg == AUTONEG_DISABLE &&
483 ((speed != SPEED_1000 &&
484 speed != SPEED_100 &&
485 speed != SPEED_10) ||
486 (duplex != DUPLEX_HALF &&
487 duplex != DUPLEX_FULL)))
490 phydev->autoneg = autoneg;
492 phydev->speed = speed;
494 phydev->advertising = advertising;
496 if (autoneg == AUTONEG_ENABLE)
497 phydev->advertising |= ADVERTISED_Autoneg;
499 phydev->advertising &= ~ADVERTISED_Autoneg;
501 phydev->duplex = duplex;
503 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
505 /* Restart the PHY */
506 phy_start_aneg(phydev);
510 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
512 int phy_ethtool_ksettings_get(struct phy_device *phydev,
513 struct ethtool_link_ksettings *cmd)
515 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
518 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
519 phydev->advertising);
521 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
522 phydev->lp_advertising);
524 cmd->base.speed = phydev->speed;
525 cmd->base.duplex = phydev->duplex;
526 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
527 cmd->base.port = PORT_BNC;
529 cmd->base.port = PORT_MII;
531 cmd->base.phy_address = phydev->mdio.addr;
532 cmd->base.autoneg = phydev->autoneg;
533 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
534 cmd->base.eth_tp_mdix = phydev->mdix;
538 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
541 * phy_mii_ioctl - generic PHY MII ioctl interface
542 * @phydev: the phy_device struct
543 * @ifr: &struct ifreq for socket ioctl's
544 * @cmd: ioctl cmd to execute
546 * Note that this function is currently incompatible with the
547 * PHYCONTROL layer. It changes registers without regard to
548 * current state. Use at own risk.
550 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
552 struct mii_ioctl_data *mii_data = if_mii(ifr);
553 u16 val = mii_data->val_in;
554 bool change_autoneg = false;
558 mii_data->phy_id = phydev->mdio.addr;
562 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
568 if (mii_data->phy_id == phydev->mdio.addr) {
569 switch (mii_data->reg_num) {
571 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
572 if (phydev->autoneg == AUTONEG_ENABLE)
573 change_autoneg = true;
574 phydev->autoneg = AUTONEG_DISABLE;
575 if (val & BMCR_FULLDPLX)
576 phydev->duplex = DUPLEX_FULL;
578 phydev->duplex = DUPLEX_HALF;
579 if (val & BMCR_SPEED1000)
580 phydev->speed = SPEED_1000;
581 else if (val & BMCR_SPEED100)
582 phydev->speed = SPEED_100;
583 else phydev->speed = SPEED_10;
586 if (phydev->autoneg == AUTONEG_DISABLE)
587 change_autoneg = true;
588 phydev->autoneg = AUTONEG_ENABLE;
592 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
593 change_autoneg = true;
601 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
602 mii_data->reg_num, val);
604 if (mii_data->phy_id == phydev->mdio.addr &&
605 mii_data->reg_num == MII_BMCR &&
607 return phy_init_hw(phydev);
610 return phy_start_aneg(phydev);
615 if (phydev->drv && phydev->drv->hwtstamp)
616 return phydev->drv->hwtstamp(phydev, ifr);
623 EXPORT_SYMBOL(phy_mii_ioctl);
626 * phy_start_aneg_priv - start auto-negotiation for this PHY device
627 * @phydev: the phy_device struct
628 * @sync: indicate whether we should wait for the workqueue cancelation
630 * Description: Sanitizes the settings (if we're not autonegotiating
631 * them), and then calls the driver's config_aneg function.
632 * If the PHYCONTROL Layer is operating, we change the state to
633 * reflect the beginning of Auto-negotiation or forcing.
635 static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
643 mutex_lock(&phydev->lock);
645 if (AUTONEG_DISABLE == phydev->autoneg)
646 phy_sanitize_settings(phydev);
648 /* Invalidate LP advertising flags */
649 phydev->lp_advertising = 0;
651 err = phydev->drv->config_aneg(phydev);
655 if (phydev->state != PHY_HALTED) {
656 if (AUTONEG_ENABLE == phydev->autoneg) {
657 phydev->state = PHY_AN;
658 phydev->link_timeout = PHY_AN_TIMEOUT;
660 phydev->state = PHY_FORCING;
661 phydev->link_timeout = PHY_FORCE_TIMEOUT;
665 /* Re-schedule a PHY state machine to check PHY status because
666 * negotiation may already be done and aneg interrupt may not be
669 if (phy_interrupt_is_valid(phydev) && (phydev->state == PHY_AN)) {
670 err = phy_aneg_done(phydev);
678 mutex_unlock(&phydev->lock);
681 phy_trigger_machine(phydev, sync);
687 * phy_start_aneg - start auto-negotiation for this PHY device
688 * @phydev: the phy_device struct
690 * Description: Sanitizes the settings (if we're not autonegotiating
691 * them), and then calls the driver's config_aneg function.
692 * If the PHYCONTROL Layer is operating, we change the state to
693 * reflect the beginning of Auto-negotiation or forcing.
695 int phy_start_aneg(struct phy_device *phydev)
697 return phy_start_aneg_priv(phydev, true);
699 EXPORT_SYMBOL(phy_start_aneg);
702 * phy_start_machine - start PHY state machine tracking
703 * @phydev: the phy_device struct
705 * Description: The PHY infrastructure can run a state machine
706 * which tracks whether the PHY is starting up, negotiating,
707 * etc. This function starts the timer which tracks the state
708 * of the PHY. If you want to maintain your own state machine,
709 * do not call this function.
711 void phy_start_machine(struct phy_device *phydev)
713 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
717 * phy_trigger_machine - trigger the state machine to run
719 * @phydev: the phy_device struct
720 * @sync: indicate whether we should wait for the workqueue cancelation
722 * Description: There has been a change in state which requires that the
723 * state machine runs.
726 void phy_trigger_machine(struct phy_device *phydev, bool sync)
729 cancel_delayed_work_sync(&phydev->state_queue);
731 cancel_delayed_work(&phydev->state_queue);
732 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
736 * phy_stop_machine - stop the PHY state machine tracking
737 * @phydev: target phy_device struct
739 * Description: Stops the state machine timer, sets the state to UP
740 * (unless it wasn't up yet). This function must be called BEFORE
743 void phy_stop_machine(struct phy_device *phydev)
745 cancel_delayed_work_sync(&phydev->state_queue);
747 mutex_lock(&phydev->lock);
748 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
749 phydev->state = PHY_UP;
750 mutex_unlock(&phydev->lock);
754 * phy_error - enter HALTED state for this PHY device
755 * @phydev: target phy_device struct
757 * Moves the PHY to the HALTED state in response to a read
758 * or write error, and tells the controller the link is down.
759 * Must not be called from interrupt context, or while the
760 * phydev->lock is held.
762 static void phy_error(struct phy_device *phydev)
764 mutex_lock(&phydev->lock);
765 phydev->state = PHY_HALTED;
766 mutex_unlock(&phydev->lock);
768 phy_trigger_machine(phydev, false);
772 * phy_interrupt - PHY interrupt handler
773 * @irq: interrupt line
774 * @phy_dat: phy_device pointer
776 * Description: When a PHY interrupt occurs, the handler disables
777 * interrupts, and uses phy_change to handle the interrupt.
779 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
781 struct phy_device *phydev = phy_dat;
783 if (PHY_HALTED == phydev->state)
784 return IRQ_NONE; /* It can't be ours. */
786 disable_irq_nosync(irq);
787 atomic_inc(&phydev->irq_disable);
795 * phy_enable_interrupts - Enable the interrupts from the PHY side
796 * @phydev: target phy_device struct
798 static int phy_enable_interrupts(struct phy_device *phydev)
800 int err = phy_clear_interrupt(phydev);
805 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
809 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
810 * @phydev: target phy_device struct
812 static int phy_disable_interrupts(struct phy_device *phydev)
816 /* Disable PHY interrupts */
817 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
821 /* Clear the interrupt */
822 err = phy_clear_interrupt(phydev);
835 * phy_start_interrupts - request and enable interrupts for a PHY device
836 * @phydev: target phy_device struct
838 * Description: Request the interrupt for the given PHY.
839 * If this fails, then we set irq to PHY_POLL.
840 * Otherwise, we enable the interrupts in the PHY.
841 * This should only be called with a valid IRQ number.
842 * Returns 0 on success or < 0 on error.
844 int phy_start_interrupts(struct phy_device *phydev)
846 atomic_set(&phydev->irq_disable, 0);
847 if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
848 IRQF_ONESHOT | IRQF_SHARED,
849 phydev_name(phydev), phydev) < 0) {
850 pr_warn("%s: Can't get IRQ %d (PHY)\n",
851 phydev->mdio.bus->name, phydev->irq);
852 phydev->irq = PHY_POLL;
856 return phy_enable_interrupts(phydev);
858 EXPORT_SYMBOL(phy_start_interrupts);
861 * phy_stop_interrupts - disable interrupts from a PHY device
862 * @phydev: target phy_device struct
864 int phy_stop_interrupts(struct phy_device *phydev)
866 int err = phy_disable_interrupts(phydev);
871 free_irq(phydev->irq, phydev);
873 /* If work indeed has been cancelled, disable_irq() will have
874 * been left unbalanced from phy_interrupt() and enable_irq()
875 * has to be called so that other devices on the line work.
877 while (atomic_dec_return(&phydev->irq_disable) >= 0)
878 enable_irq(phydev->irq);
882 EXPORT_SYMBOL(phy_stop_interrupts);
885 * phy_change - Called by the phy_interrupt to handle PHY changes
886 * @phydev: phy_device struct that interrupted
888 void phy_change(struct phy_device *phydev)
890 if (phy_interrupt_is_valid(phydev)) {
891 if (phydev->drv->did_interrupt &&
892 !phydev->drv->did_interrupt(phydev))
895 if (phy_disable_interrupts(phydev))
899 mutex_lock(&phydev->lock);
900 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
901 phydev->state = PHY_CHANGELINK;
902 mutex_unlock(&phydev->lock);
904 if (phy_interrupt_is_valid(phydev)) {
905 atomic_dec(&phydev->irq_disable);
906 enable_irq(phydev->irq);
908 /* Reenable interrupts */
909 if (PHY_HALTED != phydev->state &&
910 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
914 /* reschedule state queue work to run as soon as possible */
915 phy_trigger_machine(phydev, true);
919 atomic_dec(&phydev->irq_disable);
920 enable_irq(phydev->irq);
924 disable_irq(phydev->irq);
925 atomic_inc(&phydev->irq_disable);
931 * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
932 * @work: work_struct that describes the work to be done
934 void phy_change_work(struct work_struct *work)
936 struct phy_device *phydev =
937 container_of(work, struct phy_device, phy_queue);
943 * phy_stop - Bring down the PHY link, and stop checking the status
944 * @phydev: target phy_device struct
946 void phy_stop(struct phy_device *phydev)
948 mutex_lock(&phydev->lock);
950 if (PHY_HALTED == phydev->state)
953 if (phy_interrupt_is_valid(phydev)) {
954 /* Disable PHY Interrupts */
955 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
957 /* Clear any pending interrupts */
958 phy_clear_interrupt(phydev);
961 phydev->state = PHY_HALTED;
964 mutex_unlock(&phydev->lock);
966 /* Cannot call flush_scheduled_work() here as desired because
967 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
968 * will not reenable interrupts.
971 EXPORT_SYMBOL(phy_stop);
974 * phy_start - start or restart a PHY device
975 * @phydev: target phy_device struct
977 * Description: Indicates the attached device's readiness to
978 * handle PHY-related work. Used during startup to start the
979 * PHY, and after a call to phy_stop() to resume operation.
980 * Also used to indicate the MDIO bus has cleared an error
983 void phy_start(struct phy_device *phydev)
985 bool do_resume = false;
988 mutex_lock(&phydev->lock);
990 switch (phydev->state) {
992 phydev->state = PHY_PENDING;
995 phydev->state = PHY_UP;
998 /* make sure interrupts are re-enabled for the PHY */
999 if (phydev->irq != PHY_POLL) {
1000 err = phy_enable_interrupts(phydev);
1005 phydev->state = PHY_RESUMING;
1011 mutex_unlock(&phydev->lock);
1013 /* if phy was suspended, bring the physical link up again */
1017 phy_trigger_machine(phydev, true);
1019 EXPORT_SYMBOL(phy_start);
1021 static void phy_adjust_link(struct phy_device *phydev)
1023 phydev->adjust_link(phydev->attached_dev);
1024 phy_led_trigger_change_speed(phydev);
1028 * phy_state_machine - Handle the state machine
1029 * @work: work_struct that describes the work to be done
1031 void phy_state_machine(struct work_struct *work)
1033 struct delayed_work *dwork = to_delayed_work(work);
1034 struct phy_device *phydev =
1035 container_of(dwork, struct phy_device, state_queue);
1036 bool needs_aneg = false, do_suspend = false;
1037 enum phy_state old_state;
1041 mutex_lock(&phydev->lock);
1043 old_state = phydev->state;
1045 if (phydev->drv && phydev->drv->link_change_notify)
1046 phydev->drv->link_change_notify(phydev);
1048 switch (phydev->state) {
1057 phydev->link_timeout = PHY_AN_TIMEOUT;
1061 err = phy_read_status(phydev);
1065 /* If the link is down, give up on negotiation for now */
1066 if (!phydev->link) {
1067 phydev->state = PHY_NOLINK;
1068 netif_carrier_off(phydev->attached_dev);
1069 phy_adjust_link(phydev);
1073 /* Check if negotiation is done. Break if there's an error */
1074 err = phy_aneg_done(phydev);
1078 /* If AN is done, we're running */
1080 phydev->state = PHY_RUNNING;
1081 netif_carrier_on(phydev->attached_dev);
1082 phy_adjust_link(phydev);
1084 } else if (0 == phydev->link_timeout--)
1088 if (phy_interrupt_is_valid(phydev))
1091 err = phy_read_status(phydev);
1096 if (AUTONEG_ENABLE == phydev->autoneg) {
1097 err = phy_aneg_done(phydev);
1102 phydev->state = PHY_AN;
1103 phydev->link_timeout = PHY_AN_TIMEOUT;
1107 phydev->state = PHY_RUNNING;
1108 netif_carrier_on(phydev->attached_dev);
1109 phy_adjust_link(phydev);
1113 err = genphy_update_link(phydev);
1118 phydev->state = PHY_RUNNING;
1119 netif_carrier_on(phydev->attached_dev);
1121 if (0 == phydev->link_timeout--)
1125 phy_adjust_link(phydev);
1128 /* Only register a CHANGE if we are polling and link changed
1129 * since latest checking.
1131 if (phydev->irq == PHY_POLL) {
1132 old_link = phydev->link;
1133 err = phy_read_status(phydev);
1137 if (old_link != phydev->link)
1138 phydev->state = PHY_CHANGELINK;
1141 * Failsafe: check that nobody set phydev->link=0 between two
1142 * poll cycles, otherwise we won't leave RUNNING state as long
1143 * as link remains down.
1145 if (!phydev->link && phydev->state == PHY_RUNNING) {
1146 phydev->state = PHY_CHANGELINK;
1147 phydev_err(phydev, "no link in PHY_RUNNING\n");
1150 case PHY_CHANGELINK:
1151 err = phy_read_status(phydev);
1156 phydev->state = PHY_RUNNING;
1157 netif_carrier_on(phydev->attached_dev);
1159 phydev->state = PHY_NOLINK;
1160 netif_carrier_off(phydev->attached_dev);
1163 phy_adjust_link(phydev);
1165 if (phy_interrupt_is_valid(phydev))
1166 err = phy_config_interrupt(phydev,
1167 PHY_INTERRUPT_ENABLED);
1172 netif_carrier_off(phydev->attached_dev);
1173 phy_adjust_link(phydev);
1178 if (AUTONEG_ENABLE == phydev->autoneg) {
1179 err = phy_aneg_done(phydev);
1183 /* err > 0 if AN is done.
1184 * Otherwise, it's 0, and we're still waiting for AN
1187 err = phy_read_status(phydev);
1192 phydev->state = PHY_RUNNING;
1193 netif_carrier_on(phydev->attached_dev);
1195 phydev->state = PHY_NOLINK;
1197 phy_adjust_link(phydev);
1199 phydev->state = PHY_AN;
1200 phydev->link_timeout = PHY_AN_TIMEOUT;
1203 err = phy_read_status(phydev);
1208 phydev->state = PHY_RUNNING;
1209 netif_carrier_on(phydev->attached_dev);
1211 phydev->state = PHY_NOLINK;
1213 phy_adjust_link(phydev);
1218 mutex_unlock(&phydev->lock);
1221 err = phy_start_aneg_priv(phydev, false);
1222 else if (do_suspend)
1223 phy_suspend(phydev);
1228 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1229 phy_state_to_str(old_state),
1230 phy_state_to_str(phydev->state));
1232 /* Only re-schedule a PHY state machine change if we are polling the
1233 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1234 * between states from phy_mac_interrupt()
1236 if (phydev->irq == PHY_POLL)
1237 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1238 PHY_STATE_TIME * HZ);
1242 * phy_mac_interrupt - MAC says the link has changed
1243 * @phydev: phy_device struct with changed link
1244 * @new_link: Link is Up/Down.
1246 * Description: The MAC layer is able indicate there has been a change
1247 * in the PHY link status. Set the new link status, and trigger the
1248 * state machine, work a work queue.
1250 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1252 phydev->link = new_link;
1254 /* Trigger a state machine change */
1255 queue_work(system_power_efficient_wq, &phydev->phy_queue);
1257 EXPORT_SYMBOL(phy_mac_interrupt);
1260 * phy_init_eee - init and check the EEE feature
1261 * @phydev: target phy_device struct
1262 * @clk_stop_enable: PHY may stop the clock during LPI
1264 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1265 * is supported by looking at the MMD registers 3.20 and 7.60/61
1266 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1269 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1274 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1276 if (phydev->duplex == DUPLEX_FULL) {
1277 int eee_lp, eee_cap, eee_adv;
1281 /* Read phy status to properly get the right settings */
1282 status = phy_read_status(phydev);
1286 /* First check if the EEE ability is supported */
1287 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1291 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1295 /* Check which link settings negotiated and verify it in
1296 * the EEE advertising registers.
1298 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1302 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1306 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1307 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1308 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
1311 if (clk_stop_enable) {
1312 /* Configure the PHY to stop receiving xMII
1313 * clock while it is signaling LPI.
1315 int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1319 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1320 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
1323 return 0; /* EEE supported */
1326 return -EPROTONOSUPPORT;
1328 EXPORT_SYMBOL(phy_init_eee);
1331 * phy_get_eee_err - report the EEE wake error count
1332 * @phydev: target phy_device struct
1334 * Description: it is to report the number of time where the PHY
1335 * failed to complete its normal wake sequence.
1337 int phy_get_eee_err(struct phy_device *phydev)
1342 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1344 EXPORT_SYMBOL(phy_get_eee_err);
1347 * phy_ethtool_get_eee - get EEE supported and status
1348 * @phydev: target phy_device struct
1349 * @data: ethtool_eee data
1351 * Description: it reportes the Supported/Advertisement/LP Advertisement
1354 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1361 /* Get Supported EEE */
1362 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1365 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1367 /* Get advertisement EEE */
1368 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1371 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1373 /* Get LP advertisement EEE */
1374 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1377 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1381 EXPORT_SYMBOL(phy_ethtool_get_eee);
1384 * phy_ethtool_set_eee - set EEE supported and status
1385 * @phydev: target phy_device struct
1386 * @data: ethtool_eee data
1388 * Description: it is to program the Advertisement EEE register.
1390 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1392 int cap, old_adv, adv, ret;
1397 /* Get Supported EEE */
1398 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1402 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1406 adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1408 /* Mask prohibited EEE modes */
1409 adv &= ~phydev->eee_broken_modes;
1411 if (old_adv != adv) {
1412 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1416 /* Restart autonegotiation so the new modes get sent to the
1419 ret = phy_restart_aneg(phydev);
1426 EXPORT_SYMBOL(phy_ethtool_set_eee);
1428 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1430 if (phydev->drv && phydev->drv->set_wol)
1431 return phydev->drv->set_wol(phydev, wol);
1435 EXPORT_SYMBOL(phy_ethtool_set_wol);
1437 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1439 if (phydev->drv && phydev->drv->get_wol)
1440 phydev->drv->get_wol(phydev, wol);
1442 EXPORT_SYMBOL(phy_ethtool_get_wol);
1444 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1445 struct ethtool_link_ksettings *cmd)
1447 struct phy_device *phydev = ndev->phydev;
1452 return phy_ethtool_ksettings_get(phydev, cmd);
1454 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1456 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1457 const struct ethtool_link_ksettings *cmd)
1459 struct phy_device *phydev = ndev->phydev;
1464 return phy_ethtool_ksettings_set(phydev, cmd);
1466 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1468 int phy_ethtool_nway_reset(struct net_device *ndev)
1470 struct phy_device *phydev = ndev->phydev;
1478 return phy_restart_aneg(phydev);
1480 EXPORT_SYMBOL(phy_ethtool_nway_reset);