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);
154 * phy_aneg_done - return auto-negotiation status
155 * @phydev: target phy_device struct
157 * Description: Return the auto-negotiation status from this @phydev
158 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
161 int phy_aneg_done(struct phy_device *phydev)
163 if (phydev->drv && phydev->drv->aneg_done)
164 return phydev->drv->aneg_done(phydev);
166 return genphy_aneg_done(phydev);
168 EXPORT_SYMBOL(phy_aneg_done);
170 /* A structure for mapping a particular speed and duplex
171 * combination to a particular SUPPORTED and ADVERTISED value
179 /* A mapping of all SUPPORTED settings to speed/duplex. This table
180 * must be grouped by speed and sorted in descending match priority
181 * - iow, descending speed. */
182 static const struct phy_setting settings[] = {
184 .speed = SPEED_10000,
185 .duplex = DUPLEX_FULL,
186 .setting = SUPPORTED_10000baseKR_Full,
189 .speed = SPEED_10000,
190 .duplex = DUPLEX_FULL,
191 .setting = SUPPORTED_10000baseKX4_Full,
194 .speed = SPEED_10000,
195 .duplex = DUPLEX_FULL,
196 .setting = SUPPORTED_10000baseT_Full,
200 .duplex = DUPLEX_FULL,
201 .setting = SUPPORTED_2500baseX_Full,
205 .duplex = DUPLEX_FULL,
206 .setting = SUPPORTED_1000baseKX_Full,
210 .duplex = DUPLEX_FULL,
211 .setting = SUPPORTED_1000baseT_Full,
215 .duplex = DUPLEX_HALF,
216 .setting = SUPPORTED_1000baseT_Half,
220 .duplex = DUPLEX_FULL,
221 .setting = SUPPORTED_100baseT_Full,
225 .duplex = DUPLEX_HALF,
226 .setting = SUPPORTED_100baseT_Half,
230 .duplex = DUPLEX_FULL,
231 .setting = SUPPORTED_10baseT_Full,
235 .duplex = DUPLEX_HALF,
236 .setting = SUPPORTED_10baseT_Half,
241 * phy_lookup_setting - lookup a PHY setting
242 * @speed: speed to match
243 * @duplex: duplex to match
244 * @feature: allowed link modes
245 * @exact: an exact match is required
247 * Search the settings array for a setting that matches the speed and
248 * duplex, and which is supported.
250 * If @exact is unset, either an exact match or %NULL for no match will
253 * If @exact is set, an exact match, the fastest supported setting at
254 * or below the specified speed, the slowest supported setting, or if
255 * they all fail, %NULL will be returned.
257 static const struct phy_setting *
258 phy_lookup_setting(int speed, int duplex, u32 features, bool exact)
260 const struct phy_setting *p, *match = NULL, *last = NULL;
263 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
264 if (p->setting & features) {
266 if (p->speed == speed && p->duplex == duplex) {
267 /* Exact match for speed and duplex */
271 if (!match && p->speed <= speed)
275 if (p->speed < speed)
281 if (!match && !exact)
288 * phy_find_valid - find a PHY setting that matches the requested parameters
289 * @speed: desired speed
290 * @duplex: desired duplex
291 * @supported: mask of supported link modes
293 * Locate a supported phy setting that is, in priority order:
294 * - an exact match for the specified speed and duplex mode
295 * - a match for the specified speed, or slower speed
296 * - the slowest supported speed
297 * Returns the matched phy_setting entry, or %NULL if no supported phy
298 * settings were found.
300 static const struct phy_setting *
301 phy_find_valid(int speed, int duplex, u32 supported)
303 return phy_lookup_setting(speed, duplex, supported, false);
307 * phy_supported_speeds - return all speeds currently supported by a phy device
308 * @phy: The phy device to return supported speeds of.
309 * @speeds: buffer to store supported speeds in.
310 * @size: size of speeds buffer.
312 * Description: Returns the number of supported speeds, and fills the speeds
313 * buffer with the supported speeds. If speeds buffer is too small to contain
314 * all currently supported speeds, will return as many speeds as can fit.
316 unsigned int phy_supported_speeds(struct phy_device *phy,
317 unsigned int *speeds,
320 unsigned int count = 0;
321 unsigned int idx = 0;
323 for (idx = 0; idx < ARRAY_SIZE(settings) && count < size; idx++)
324 /* Assumes settings are grouped by speed */
325 if ((settings[idx].setting & phy->supported) &&
326 (count == 0 || speeds[count - 1] != settings[idx].speed))
327 speeds[count++] = settings[idx].speed;
333 * phy_check_valid - check if there is a valid PHY setting which matches
334 * speed, duplex, and feature mask
335 * @speed: speed to match
336 * @duplex: duplex to match
337 * @features: A mask of the valid settings
339 * Description: Returns true if there is a valid setting, false otherwise.
341 static inline bool phy_check_valid(int speed, int duplex, u32 features)
343 return !!phy_lookup_setting(speed, duplex, features, true);
347 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
348 * @phydev: the target phy_device struct
350 * Description: Make sure the PHY is set to supported speeds and
351 * duplexes. Drop down by one in this order: 1000/FULL,
352 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
354 static void phy_sanitize_settings(struct phy_device *phydev)
356 const struct phy_setting *setting;
357 u32 features = phydev->supported;
359 /* Sanitize settings based on PHY capabilities */
360 if ((features & SUPPORTED_Autoneg) == 0)
361 phydev->autoneg = AUTONEG_DISABLE;
363 setting = phy_find_valid(phydev->speed, phydev->duplex, features);
365 phydev->speed = setting->speed;
366 phydev->duplex = setting->duplex;
368 /* We failed to find anything (no supported speeds?) */
369 phydev->speed = SPEED_UNKNOWN;
370 phydev->duplex = DUPLEX_UNKNOWN;
375 * phy_ethtool_sset - generic ethtool sset function, handles all the details
376 * @phydev: target phy_device struct
379 * A few notes about parameter checking:
380 * - We don't set port or transceiver, so we don't care what they
382 * - phy_start_aneg() will make sure forced settings are sane, and
383 * choose the next best ones from the ones selected, so we don't
384 * care if ethtool tries to give us bad values.
386 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
388 u32 speed = ethtool_cmd_speed(cmd);
390 if (cmd->phy_address != phydev->mdio.addr)
393 /* We make sure that we don't pass unsupported values in to the PHY */
394 cmd->advertising &= phydev->supported;
396 /* Verify the settings we care about. */
397 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
400 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
403 if (cmd->autoneg == AUTONEG_DISABLE &&
404 ((speed != SPEED_1000 &&
405 speed != SPEED_100 &&
406 speed != SPEED_10) ||
407 (cmd->duplex != DUPLEX_HALF &&
408 cmd->duplex != DUPLEX_FULL)))
411 phydev->autoneg = cmd->autoneg;
413 phydev->speed = speed;
415 phydev->advertising = cmd->advertising;
417 if (AUTONEG_ENABLE == cmd->autoneg)
418 phydev->advertising |= ADVERTISED_Autoneg;
420 phydev->advertising &= ~ADVERTISED_Autoneg;
422 phydev->duplex = cmd->duplex;
424 phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
426 /* Restart the PHY */
427 phy_start_aneg(phydev);
431 EXPORT_SYMBOL(phy_ethtool_sset);
433 int phy_ethtool_ksettings_set(struct phy_device *phydev,
434 const struct ethtool_link_ksettings *cmd)
436 u8 autoneg = cmd->base.autoneg;
437 u8 duplex = cmd->base.duplex;
438 u32 speed = cmd->base.speed;
441 if (cmd->base.phy_address != phydev->mdio.addr)
444 ethtool_convert_link_mode_to_legacy_u32(&advertising,
445 cmd->link_modes.advertising);
447 /* We make sure that we don't pass unsupported values in to the PHY */
448 advertising &= phydev->supported;
450 /* Verify the settings we care about. */
451 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
454 if (autoneg == AUTONEG_ENABLE && advertising == 0)
457 if (autoneg == AUTONEG_DISABLE &&
458 ((speed != SPEED_1000 &&
459 speed != SPEED_100 &&
460 speed != SPEED_10) ||
461 (duplex != DUPLEX_HALF &&
462 duplex != DUPLEX_FULL)))
465 phydev->autoneg = autoneg;
467 phydev->speed = speed;
469 phydev->advertising = advertising;
471 if (autoneg == AUTONEG_ENABLE)
472 phydev->advertising |= ADVERTISED_Autoneg;
474 phydev->advertising &= ~ADVERTISED_Autoneg;
476 phydev->duplex = duplex;
478 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
480 /* Restart the PHY */
481 phy_start_aneg(phydev);
485 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
487 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
489 cmd->supported = phydev->supported;
491 cmd->advertising = phydev->advertising;
492 cmd->lp_advertising = phydev->lp_advertising;
494 ethtool_cmd_speed_set(cmd, phydev->speed);
495 cmd->duplex = phydev->duplex;
496 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
497 cmd->port = PORT_BNC;
499 cmd->port = PORT_MII;
500 cmd->phy_address = phydev->mdio.addr;
501 cmd->transceiver = phy_is_internal(phydev) ?
502 XCVR_INTERNAL : XCVR_EXTERNAL;
503 cmd->autoneg = phydev->autoneg;
504 cmd->eth_tp_mdix_ctrl = phydev->mdix_ctrl;
505 cmd->eth_tp_mdix = phydev->mdix;
509 EXPORT_SYMBOL(phy_ethtool_gset);
511 int phy_ethtool_ksettings_get(struct phy_device *phydev,
512 struct ethtool_link_ksettings *cmd)
514 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
517 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
518 phydev->advertising);
520 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
521 phydev->lp_advertising);
523 cmd->base.speed = phydev->speed;
524 cmd->base.duplex = phydev->duplex;
525 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
526 cmd->base.port = PORT_BNC;
528 cmd->base.port = PORT_MII;
530 cmd->base.phy_address = phydev->mdio.addr;
531 cmd->base.autoneg = phydev->autoneg;
532 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
533 cmd->base.eth_tp_mdix = phydev->mdix;
537 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
540 * phy_mii_ioctl - generic PHY MII ioctl interface
541 * @phydev: the phy_device struct
542 * @ifr: &struct ifreq for socket ioctl's
543 * @cmd: ioctl cmd to execute
545 * Note that this function is currently incompatible with the
546 * PHYCONTROL layer. It changes registers without regard to
547 * current state. Use at own risk.
549 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
551 struct mii_ioctl_data *mii_data = if_mii(ifr);
552 u16 val = mii_data->val_in;
553 bool change_autoneg = false;
557 mii_data->phy_id = phydev->mdio.addr;
561 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
567 if (mii_data->phy_id == phydev->mdio.addr) {
568 switch (mii_data->reg_num) {
570 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
571 if (phydev->autoneg == AUTONEG_ENABLE)
572 change_autoneg = true;
573 phydev->autoneg = AUTONEG_DISABLE;
574 if (val & BMCR_FULLDPLX)
575 phydev->duplex = DUPLEX_FULL;
577 phydev->duplex = DUPLEX_HALF;
578 if (val & BMCR_SPEED1000)
579 phydev->speed = SPEED_1000;
580 else if (val & BMCR_SPEED100)
581 phydev->speed = SPEED_100;
582 else phydev->speed = SPEED_10;
585 if (phydev->autoneg == AUTONEG_DISABLE)
586 change_autoneg = true;
587 phydev->autoneg = AUTONEG_ENABLE;
591 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
592 change_autoneg = true;
600 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
601 mii_data->reg_num, val);
603 if (mii_data->phy_id == phydev->mdio.addr &&
604 mii_data->reg_num == MII_BMCR &&
606 return phy_init_hw(phydev);
609 return phy_start_aneg(phydev);
614 if (phydev->drv && phydev->drv->hwtstamp)
615 return phydev->drv->hwtstamp(phydev, ifr);
622 EXPORT_SYMBOL(phy_mii_ioctl);
625 * phy_start_aneg_priv - start auto-negotiation for this PHY device
626 * @phydev: the phy_device struct
627 * @sync: indicate whether we should wait for the workqueue cancelation
629 * Description: Sanitizes the settings (if we're not autonegotiating
630 * them), and then calls the driver's config_aneg function.
631 * If the PHYCONTROL Layer is operating, we change the state to
632 * reflect the beginning of Auto-negotiation or forcing.
634 static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
642 mutex_lock(&phydev->lock);
644 if (AUTONEG_DISABLE == phydev->autoneg)
645 phy_sanitize_settings(phydev);
647 /* Invalidate LP advertising flags */
648 phydev->lp_advertising = 0;
650 err = phydev->drv->config_aneg(phydev);
654 if (phydev->state != PHY_HALTED) {
655 if (AUTONEG_ENABLE == phydev->autoneg) {
656 phydev->state = PHY_AN;
657 phydev->link_timeout = PHY_AN_TIMEOUT;
659 phydev->state = PHY_FORCING;
660 phydev->link_timeout = PHY_FORCE_TIMEOUT;
664 /* Re-schedule a PHY state machine to check PHY status because
665 * negotiation may already be done and aneg interrupt may not be
668 if (phy_interrupt_is_valid(phydev) && (phydev->state == PHY_AN)) {
669 err = phy_aneg_done(phydev);
677 mutex_unlock(&phydev->lock);
680 phy_trigger_machine(phydev, sync);
686 * phy_start_aneg - start auto-negotiation for this PHY device
687 * @phydev: the phy_device struct
689 * Description: Sanitizes the settings (if we're not autonegotiating
690 * them), and then calls the driver's config_aneg function.
691 * If the PHYCONTROL Layer is operating, we change the state to
692 * reflect the beginning of Auto-negotiation or forcing.
694 int phy_start_aneg(struct phy_device *phydev)
696 return phy_start_aneg_priv(phydev, true);
698 EXPORT_SYMBOL(phy_start_aneg);
701 * phy_start_machine - start PHY state machine tracking
702 * @phydev: the phy_device struct
704 * Description: The PHY infrastructure can run a state machine
705 * which tracks whether the PHY is starting up, negotiating,
706 * etc. This function starts the timer which tracks the state
707 * of the PHY. If you want to maintain your own state machine,
708 * do not call this function.
710 void phy_start_machine(struct phy_device *phydev)
712 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
716 * phy_trigger_machine - trigger the state machine to run
718 * @phydev: the phy_device struct
719 * @sync: indicate whether we should wait for the workqueue cancelation
721 * Description: There has been a change in state which requires that the
722 * state machine runs.
725 void phy_trigger_machine(struct phy_device *phydev, bool sync)
728 cancel_delayed_work_sync(&phydev->state_queue);
730 cancel_delayed_work(&phydev->state_queue);
731 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
735 * phy_stop_machine - stop the PHY state machine tracking
736 * @phydev: target phy_device struct
738 * Description: Stops the state machine timer, sets the state to UP
739 * (unless it wasn't up yet). This function must be called BEFORE
742 void phy_stop_machine(struct phy_device *phydev)
744 cancel_delayed_work_sync(&phydev->state_queue);
746 mutex_lock(&phydev->lock);
747 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
748 phydev->state = PHY_UP;
749 mutex_unlock(&phydev->lock);
753 * phy_error - enter HALTED state for this PHY device
754 * @phydev: target phy_device struct
756 * Moves the PHY to the HALTED state in response to a read
757 * or write error, and tells the controller the link is down.
758 * Must not be called from interrupt context, or while the
759 * phydev->lock is held.
761 static void phy_error(struct phy_device *phydev)
763 mutex_lock(&phydev->lock);
764 phydev->state = PHY_HALTED;
765 mutex_unlock(&phydev->lock);
767 phy_trigger_machine(phydev, false);
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 disable_irq_nosync(irq);
786 atomic_inc(&phydev->irq_disable);
794 * phy_enable_interrupts - Enable the interrupts from the PHY side
795 * @phydev: target phy_device struct
797 static int phy_enable_interrupts(struct phy_device *phydev)
799 int err = phy_clear_interrupt(phydev);
804 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
808 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
809 * @phydev: target phy_device struct
811 static int phy_disable_interrupts(struct phy_device *phydev)
815 /* Disable PHY interrupts */
816 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
820 /* Clear the interrupt */
821 err = phy_clear_interrupt(phydev);
834 * phy_start_interrupts - request and enable interrupts for a PHY device
835 * @phydev: target phy_device struct
837 * Description: Request the interrupt for the given PHY.
838 * If this fails, then we set irq to PHY_POLL.
839 * Otherwise, we enable the interrupts in the PHY.
840 * This should only be called with a valid IRQ number.
841 * Returns 0 on success or < 0 on error.
843 int phy_start_interrupts(struct phy_device *phydev)
845 atomic_set(&phydev->irq_disable, 0);
846 if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
847 IRQF_ONESHOT | IRQF_SHARED,
848 phydev_name(phydev), phydev) < 0) {
849 pr_warn("%s: Can't get IRQ %d (PHY)\n",
850 phydev->mdio.bus->name, phydev->irq);
851 phydev->irq = PHY_POLL;
855 return phy_enable_interrupts(phydev);
857 EXPORT_SYMBOL(phy_start_interrupts);
860 * phy_stop_interrupts - disable interrupts from a PHY device
861 * @phydev: target phy_device struct
863 int phy_stop_interrupts(struct phy_device *phydev)
865 int err = phy_disable_interrupts(phydev);
870 free_irq(phydev->irq, phydev);
872 /* If work indeed has been cancelled, disable_irq() will have
873 * been left unbalanced from phy_interrupt() and enable_irq()
874 * has to be called so that other devices on the line work.
876 while (atomic_dec_return(&phydev->irq_disable) >= 0)
877 enable_irq(phydev->irq);
881 EXPORT_SYMBOL(phy_stop_interrupts);
884 * phy_change - Called by the phy_interrupt to handle PHY changes
885 * @phydev: phy_device struct that interrupted
887 void phy_change(struct phy_device *phydev)
889 if (phy_interrupt_is_valid(phydev)) {
890 if (phydev->drv->did_interrupt &&
891 !phydev->drv->did_interrupt(phydev))
894 if (phy_disable_interrupts(phydev))
898 mutex_lock(&phydev->lock);
899 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
900 phydev->state = PHY_CHANGELINK;
901 mutex_unlock(&phydev->lock);
903 if (phy_interrupt_is_valid(phydev)) {
904 atomic_dec(&phydev->irq_disable);
905 enable_irq(phydev->irq);
907 /* Reenable interrupts */
908 if (PHY_HALTED != phydev->state &&
909 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
913 /* reschedule state queue work to run as soon as possible */
914 phy_trigger_machine(phydev, true);
918 atomic_dec(&phydev->irq_disable);
919 enable_irq(phydev->irq);
923 disable_irq(phydev->irq);
924 atomic_inc(&phydev->irq_disable);
930 * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
931 * @work: work_struct that describes the work to be done
933 void phy_change_work(struct work_struct *work)
935 struct phy_device *phydev =
936 container_of(work, struct phy_device, phy_queue);
942 * phy_stop - Bring down the PHY link, and stop checking the status
943 * @phydev: target phy_device struct
945 void phy_stop(struct phy_device *phydev)
947 mutex_lock(&phydev->lock);
949 if (PHY_HALTED == phydev->state)
952 if (phy_interrupt_is_valid(phydev)) {
953 /* Disable PHY Interrupts */
954 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
956 /* Clear any pending interrupts */
957 phy_clear_interrupt(phydev);
960 phydev->state = PHY_HALTED;
963 mutex_unlock(&phydev->lock);
965 /* Cannot call flush_scheduled_work() here as desired because
966 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
967 * will not reenable interrupts.
970 EXPORT_SYMBOL(phy_stop);
973 * phy_start - start or restart a PHY device
974 * @phydev: target phy_device struct
976 * Description: Indicates the attached device's readiness to
977 * handle PHY-related work. Used during startup to start the
978 * PHY, and after a call to phy_stop() to resume operation.
979 * Also used to indicate the MDIO bus has cleared an error
982 void phy_start(struct phy_device *phydev)
984 bool do_resume = false;
987 mutex_lock(&phydev->lock);
989 switch (phydev->state) {
991 phydev->state = PHY_PENDING;
994 phydev->state = PHY_UP;
997 /* make sure interrupts are re-enabled for the PHY */
998 if (phydev->irq != PHY_POLL) {
999 err = phy_enable_interrupts(phydev);
1004 phydev->state = PHY_RESUMING;
1010 mutex_unlock(&phydev->lock);
1012 /* if phy was suspended, bring the physical link up again */
1016 phy_trigger_machine(phydev, true);
1018 EXPORT_SYMBOL(phy_start);
1020 static void phy_adjust_link(struct phy_device *phydev)
1022 phydev->adjust_link(phydev->attached_dev);
1023 phy_led_trigger_change_speed(phydev);
1027 * phy_state_machine - Handle the state machine
1028 * @work: work_struct that describes the work to be done
1030 void phy_state_machine(struct work_struct *work)
1032 struct delayed_work *dwork = to_delayed_work(work);
1033 struct phy_device *phydev =
1034 container_of(dwork, struct phy_device, state_queue);
1035 bool needs_aneg = false, do_suspend = false;
1036 enum phy_state old_state;
1040 mutex_lock(&phydev->lock);
1042 old_state = phydev->state;
1044 if (phydev->drv && phydev->drv->link_change_notify)
1045 phydev->drv->link_change_notify(phydev);
1047 switch (phydev->state) {
1056 phydev->link_timeout = PHY_AN_TIMEOUT;
1060 err = phy_read_status(phydev);
1064 /* If the link is down, give up on negotiation for now */
1065 if (!phydev->link) {
1066 phydev->state = PHY_NOLINK;
1067 netif_carrier_off(phydev->attached_dev);
1068 phy_adjust_link(phydev);
1072 /* Check if negotiation is done. Break if there's an error */
1073 err = phy_aneg_done(phydev);
1077 /* If AN is done, we're running */
1079 phydev->state = PHY_RUNNING;
1080 netif_carrier_on(phydev->attached_dev);
1081 phy_adjust_link(phydev);
1083 } else if (0 == phydev->link_timeout--)
1087 if (phy_interrupt_is_valid(phydev))
1090 err = phy_read_status(phydev);
1095 if (AUTONEG_ENABLE == phydev->autoneg) {
1096 err = phy_aneg_done(phydev);
1101 phydev->state = PHY_AN;
1102 phydev->link_timeout = PHY_AN_TIMEOUT;
1106 phydev->state = PHY_RUNNING;
1107 netif_carrier_on(phydev->attached_dev);
1108 phy_adjust_link(phydev);
1112 err = genphy_update_link(phydev);
1117 phydev->state = PHY_RUNNING;
1118 netif_carrier_on(phydev->attached_dev);
1120 if (0 == phydev->link_timeout--)
1124 phy_adjust_link(phydev);
1127 /* Only register a CHANGE if we are polling and link changed
1128 * since latest checking.
1130 if (phydev->irq == PHY_POLL) {
1131 old_link = phydev->link;
1132 err = phy_read_status(phydev);
1136 if (old_link != phydev->link)
1137 phydev->state = PHY_CHANGELINK;
1140 * Failsafe: check that nobody set phydev->link=0 between two
1141 * poll cycles, otherwise we won't leave RUNNING state as long
1142 * as link remains down.
1144 if (!phydev->link && phydev->state == PHY_RUNNING) {
1145 phydev->state = PHY_CHANGELINK;
1146 phydev_err(phydev, "no link in PHY_RUNNING\n");
1149 case PHY_CHANGELINK:
1150 err = phy_read_status(phydev);
1155 phydev->state = PHY_RUNNING;
1156 netif_carrier_on(phydev->attached_dev);
1158 phydev->state = PHY_NOLINK;
1159 netif_carrier_off(phydev->attached_dev);
1162 phy_adjust_link(phydev);
1164 if (phy_interrupt_is_valid(phydev))
1165 err = phy_config_interrupt(phydev,
1166 PHY_INTERRUPT_ENABLED);
1171 netif_carrier_off(phydev->attached_dev);
1172 phy_adjust_link(phydev);
1177 if (AUTONEG_ENABLE == phydev->autoneg) {
1178 err = phy_aneg_done(phydev);
1182 /* err > 0 if AN is done.
1183 * Otherwise, it's 0, and we're still waiting for AN
1186 err = phy_read_status(phydev);
1191 phydev->state = PHY_RUNNING;
1192 netif_carrier_on(phydev->attached_dev);
1194 phydev->state = PHY_NOLINK;
1196 phy_adjust_link(phydev);
1198 phydev->state = PHY_AN;
1199 phydev->link_timeout = PHY_AN_TIMEOUT;
1202 err = phy_read_status(phydev);
1207 phydev->state = PHY_RUNNING;
1208 netif_carrier_on(phydev->attached_dev);
1210 phydev->state = PHY_NOLINK;
1212 phy_adjust_link(phydev);
1217 mutex_unlock(&phydev->lock);
1220 err = phy_start_aneg_priv(phydev, false);
1221 else if (do_suspend)
1222 phy_suspend(phydev);
1227 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1228 phy_state_to_str(old_state),
1229 phy_state_to_str(phydev->state));
1231 /* Only re-schedule a PHY state machine change if we are polling the
1232 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1233 * between states from phy_mac_interrupt()
1235 if (phydev->irq == PHY_POLL)
1236 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1237 PHY_STATE_TIME * HZ);
1241 * phy_mac_interrupt - MAC says the link has changed
1242 * @phydev: phy_device struct with changed link
1243 * @new_link: Link is Up/Down.
1245 * Description: The MAC layer is able indicate there has been a change
1246 * in the PHY link status. Set the new link status, and trigger the
1247 * state machine, work a work queue.
1249 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1251 phydev->link = new_link;
1253 /* Trigger a state machine change */
1254 queue_work(system_power_efficient_wq, &phydev->phy_queue);
1256 EXPORT_SYMBOL(phy_mac_interrupt);
1259 * phy_init_eee - init and check the EEE feature
1260 * @phydev: target phy_device struct
1261 * @clk_stop_enable: PHY may stop the clock during LPI
1263 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1264 * is supported by looking at the MMD registers 3.20 and 7.60/61
1265 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1268 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1273 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1275 if (phydev->duplex == DUPLEX_FULL) {
1276 int eee_lp, eee_cap, eee_adv;
1280 /* Read phy status to properly get the right settings */
1281 status = phy_read_status(phydev);
1285 /* First check if the EEE ability is supported */
1286 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1290 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1294 /* Check which link settings negotiated and verify it in
1295 * the EEE advertising registers.
1297 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1301 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1305 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1306 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1307 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
1310 if (clk_stop_enable) {
1311 /* Configure the PHY to stop receiving xMII
1312 * clock while it is signaling LPI.
1314 int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1318 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1319 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
1322 return 0; /* EEE supported */
1325 return -EPROTONOSUPPORT;
1327 EXPORT_SYMBOL(phy_init_eee);
1330 * phy_get_eee_err - report the EEE wake error count
1331 * @phydev: target phy_device struct
1333 * Description: it is to report the number of time where the PHY
1334 * failed to complete its normal wake sequence.
1336 int phy_get_eee_err(struct phy_device *phydev)
1341 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1343 EXPORT_SYMBOL(phy_get_eee_err);
1346 * phy_ethtool_get_eee - get EEE supported and status
1347 * @phydev: target phy_device struct
1348 * @data: ethtool_eee data
1350 * Description: it reportes the Supported/Advertisement/LP Advertisement
1353 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1360 /* Get Supported EEE */
1361 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1364 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1366 /* Get advertisement EEE */
1367 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1370 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1372 /* Get LP advertisement EEE */
1373 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1376 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1380 EXPORT_SYMBOL(phy_ethtool_get_eee);
1383 * phy_ethtool_set_eee - set EEE supported and status
1384 * @phydev: target phy_device struct
1385 * @data: ethtool_eee data
1387 * Description: it is to program the Advertisement EEE register.
1389 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1391 int cap, old_adv, adv, ret;
1396 /* Get Supported EEE */
1397 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1401 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1405 adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1407 /* Mask prohibited EEE modes */
1408 adv &= ~phydev->eee_broken_modes;
1410 if (old_adv != adv) {
1411 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1415 /* Restart autonegotiation so the new modes get sent to the
1418 ret = genphy_restart_aneg(phydev);
1425 EXPORT_SYMBOL(phy_ethtool_set_eee);
1427 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1429 if (phydev->drv && phydev->drv->set_wol)
1430 return phydev->drv->set_wol(phydev, wol);
1434 EXPORT_SYMBOL(phy_ethtool_set_wol);
1436 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1438 if (phydev->drv && phydev->drv->get_wol)
1439 phydev->drv->get_wol(phydev, wol);
1441 EXPORT_SYMBOL(phy_ethtool_get_wol);
1443 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1444 struct ethtool_link_ksettings *cmd)
1446 struct phy_device *phydev = ndev->phydev;
1451 return phy_ethtool_ksettings_get(phydev, cmd);
1453 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1455 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1456 const struct ethtool_link_ksettings *cmd)
1458 struct phy_device *phydev = ndev->phydev;
1463 return phy_ethtool_ksettings_set(phydev, cmd);
1465 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1467 int phy_ethtool_nway_reset(struct net_device *ndev)
1469 struct phy_device *phydev = ndev->phydev;
1477 return genphy_restart_aneg(phydev);
1479 EXPORT_SYMBOL(phy_ethtool_nway_reset);