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 if (!(settings[idx].setting & phy->supported))
327 /* Assumes settings are grouped by speed */
329 (speeds[count - 1] != settings[idx].speed)) {
330 speeds[count] = settings[idx].speed;
339 * phy_check_valid - check if there is a valid PHY setting which matches
340 * speed, duplex, and feature mask
341 * @speed: speed to match
342 * @duplex: duplex to match
343 * @features: A mask of the valid settings
345 * Description: Returns true if there is a valid setting, false otherwise.
347 static inline bool phy_check_valid(int speed, int duplex, u32 features)
349 return !!phy_lookup_setting(speed, duplex, features, true);
353 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
354 * @phydev: the target phy_device struct
356 * Description: Make sure the PHY is set to supported speeds and
357 * duplexes. Drop down by one in this order: 1000/FULL,
358 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
360 static void phy_sanitize_settings(struct phy_device *phydev)
362 const struct phy_setting *setting;
363 u32 features = phydev->supported;
365 /* Sanitize settings based on PHY capabilities */
366 if ((features & SUPPORTED_Autoneg) == 0)
367 phydev->autoneg = AUTONEG_DISABLE;
369 setting = phy_find_valid(phydev->speed, phydev->duplex, features);
371 phydev->speed = setting->speed;
372 phydev->duplex = setting->duplex;
374 /* We failed to find anything (no supported speeds?) */
375 phydev->speed = SPEED_UNKNOWN;
376 phydev->duplex = DUPLEX_UNKNOWN;
381 * phy_ethtool_sset - generic ethtool sset function, handles all the details
382 * @phydev: target phy_device struct
385 * A few notes about parameter checking:
386 * - We don't set port or transceiver, so we don't care what they
388 * - phy_start_aneg() will make sure forced settings are sane, and
389 * choose the next best ones from the ones selected, so we don't
390 * care if ethtool tries to give us bad values.
392 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
394 u32 speed = ethtool_cmd_speed(cmd);
396 if (cmd->phy_address != phydev->mdio.addr)
399 /* We make sure that we don't pass unsupported values in to the PHY */
400 cmd->advertising &= phydev->supported;
402 /* Verify the settings we care about. */
403 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
406 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
409 if (cmd->autoneg == AUTONEG_DISABLE &&
410 ((speed != SPEED_1000 &&
411 speed != SPEED_100 &&
412 speed != SPEED_10) ||
413 (cmd->duplex != DUPLEX_HALF &&
414 cmd->duplex != DUPLEX_FULL)))
417 phydev->autoneg = cmd->autoneg;
419 phydev->speed = speed;
421 phydev->advertising = cmd->advertising;
423 if (AUTONEG_ENABLE == cmd->autoneg)
424 phydev->advertising |= ADVERTISED_Autoneg;
426 phydev->advertising &= ~ADVERTISED_Autoneg;
428 phydev->duplex = cmd->duplex;
430 phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
432 /* Restart the PHY */
433 phy_start_aneg(phydev);
437 EXPORT_SYMBOL(phy_ethtool_sset);
439 int phy_ethtool_ksettings_set(struct phy_device *phydev,
440 const struct ethtool_link_ksettings *cmd)
442 u8 autoneg = cmd->base.autoneg;
443 u8 duplex = cmd->base.duplex;
444 u32 speed = cmd->base.speed;
447 if (cmd->base.phy_address != phydev->mdio.addr)
450 ethtool_convert_link_mode_to_legacy_u32(&advertising,
451 cmd->link_modes.advertising);
453 /* We make sure that we don't pass unsupported values in to the PHY */
454 advertising &= phydev->supported;
456 /* Verify the settings we care about. */
457 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
460 if (autoneg == AUTONEG_ENABLE && advertising == 0)
463 if (autoneg == AUTONEG_DISABLE &&
464 ((speed != SPEED_1000 &&
465 speed != SPEED_100 &&
466 speed != SPEED_10) ||
467 (duplex != DUPLEX_HALF &&
468 duplex != DUPLEX_FULL)))
471 phydev->autoneg = autoneg;
473 phydev->speed = speed;
475 phydev->advertising = advertising;
477 if (autoneg == AUTONEG_ENABLE)
478 phydev->advertising |= ADVERTISED_Autoneg;
480 phydev->advertising &= ~ADVERTISED_Autoneg;
482 phydev->duplex = duplex;
484 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
486 /* Restart the PHY */
487 phy_start_aneg(phydev);
491 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
493 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
495 cmd->supported = phydev->supported;
497 cmd->advertising = phydev->advertising;
498 cmd->lp_advertising = phydev->lp_advertising;
500 ethtool_cmd_speed_set(cmd, phydev->speed);
501 cmd->duplex = phydev->duplex;
502 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
503 cmd->port = PORT_BNC;
505 cmd->port = PORT_MII;
506 cmd->phy_address = phydev->mdio.addr;
507 cmd->transceiver = phy_is_internal(phydev) ?
508 XCVR_INTERNAL : XCVR_EXTERNAL;
509 cmd->autoneg = phydev->autoneg;
510 cmd->eth_tp_mdix_ctrl = phydev->mdix_ctrl;
511 cmd->eth_tp_mdix = phydev->mdix;
515 EXPORT_SYMBOL(phy_ethtool_gset);
517 int phy_ethtool_ksettings_get(struct phy_device *phydev,
518 struct ethtool_link_ksettings *cmd)
520 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
523 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
524 phydev->advertising);
526 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
527 phydev->lp_advertising);
529 cmd->base.speed = phydev->speed;
530 cmd->base.duplex = phydev->duplex;
531 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
532 cmd->base.port = PORT_BNC;
534 cmd->base.port = PORT_MII;
536 cmd->base.phy_address = phydev->mdio.addr;
537 cmd->base.autoneg = phydev->autoneg;
538 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
539 cmd->base.eth_tp_mdix = phydev->mdix;
543 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
546 * phy_mii_ioctl - generic PHY MII ioctl interface
547 * @phydev: the phy_device struct
548 * @ifr: &struct ifreq for socket ioctl's
549 * @cmd: ioctl cmd to execute
551 * Note that this function is currently incompatible with the
552 * PHYCONTROL layer. It changes registers without regard to
553 * current state. Use at own risk.
555 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
557 struct mii_ioctl_data *mii_data = if_mii(ifr);
558 u16 val = mii_data->val_in;
559 bool change_autoneg = false;
563 mii_data->phy_id = phydev->mdio.addr;
567 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
573 if (mii_data->phy_id == phydev->mdio.addr) {
574 switch (mii_data->reg_num) {
576 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
577 if (phydev->autoneg == AUTONEG_ENABLE)
578 change_autoneg = true;
579 phydev->autoneg = AUTONEG_DISABLE;
580 if (val & BMCR_FULLDPLX)
581 phydev->duplex = DUPLEX_FULL;
583 phydev->duplex = DUPLEX_HALF;
584 if (val & BMCR_SPEED1000)
585 phydev->speed = SPEED_1000;
586 else if (val & BMCR_SPEED100)
587 phydev->speed = SPEED_100;
588 else phydev->speed = SPEED_10;
591 if (phydev->autoneg == AUTONEG_DISABLE)
592 change_autoneg = true;
593 phydev->autoneg = AUTONEG_ENABLE;
597 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
598 change_autoneg = true;
606 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
607 mii_data->reg_num, val);
609 if (mii_data->phy_id == phydev->mdio.addr &&
610 mii_data->reg_num == MII_BMCR &&
612 return phy_init_hw(phydev);
615 return phy_start_aneg(phydev);
620 if (phydev->drv && phydev->drv->hwtstamp)
621 return phydev->drv->hwtstamp(phydev, ifr);
628 EXPORT_SYMBOL(phy_mii_ioctl);
631 * phy_start_aneg - start auto-negotiation for this PHY device
632 * @phydev: the phy_device struct
634 * Description: Sanitizes the settings (if we're not autonegotiating
635 * them), and then calls the driver's config_aneg function.
636 * If the PHYCONTROL Layer is operating, we change the state to
637 * reflect the beginning of Auto-negotiation or forcing.
639 int phy_start_aneg(struct phy_device *phydev)
646 mutex_lock(&phydev->lock);
648 if (AUTONEG_DISABLE == phydev->autoneg)
649 phy_sanitize_settings(phydev);
651 /* Invalidate LP advertising flags */
652 phydev->lp_advertising = 0;
654 err = phydev->drv->config_aneg(phydev);
658 if (phydev->state != PHY_HALTED) {
659 if (AUTONEG_ENABLE == phydev->autoneg) {
660 phydev->state = PHY_AN;
661 phydev->link_timeout = PHY_AN_TIMEOUT;
663 phydev->state = PHY_FORCING;
664 phydev->link_timeout = PHY_FORCE_TIMEOUT;
669 mutex_unlock(&phydev->lock);
672 EXPORT_SYMBOL(phy_start_aneg);
675 * phy_start_machine - start PHY state machine tracking
676 * @phydev: the phy_device struct
678 * Description: The PHY infrastructure can run a state machine
679 * which tracks whether the PHY is starting up, negotiating,
680 * etc. This function starts the timer which tracks the state
681 * of the PHY. If you want to maintain your own state machine,
682 * do not call this function.
684 void phy_start_machine(struct phy_device *phydev)
686 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
690 * phy_trigger_machine - trigger the state machine to run
692 * @phydev: the phy_device struct
693 * @sync: indicate whether we should wait for the workqueue cancelation
695 * Description: There has been a change in state which requires that the
696 * state machine runs.
699 static void phy_trigger_machine(struct phy_device *phydev, bool sync)
702 cancel_delayed_work_sync(&phydev->state_queue);
704 cancel_delayed_work(&phydev->state_queue);
705 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
709 * phy_stop_machine - stop the PHY state machine tracking
710 * @phydev: target phy_device struct
712 * Description: Stops the state machine timer, sets the state to UP
713 * (unless it wasn't up yet). This function must be called BEFORE
716 void phy_stop_machine(struct phy_device *phydev)
718 cancel_delayed_work_sync(&phydev->state_queue);
720 mutex_lock(&phydev->lock);
721 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
722 phydev->state = PHY_UP;
723 mutex_unlock(&phydev->lock);
727 * phy_error - enter HALTED state for this PHY device
728 * @phydev: target phy_device struct
730 * Moves the PHY to the HALTED state in response to a read
731 * or write error, and tells the controller the link is down.
732 * Must not be called from interrupt context, or while the
733 * phydev->lock is held.
735 static void phy_error(struct phy_device *phydev)
737 mutex_lock(&phydev->lock);
738 phydev->state = PHY_HALTED;
739 mutex_unlock(&phydev->lock);
741 phy_trigger_machine(phydev, false);
745 * phy_interrupt - PHY interrupt handler
746 * @irq: interrupt line
747 * @phy_dat: phy_device pointer
749 * Description: When a PHY interrupt occurs, the handler disables
750 * interrupts, and uses phy_change to handle the interrupt.
752 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
754 struct phy_device *phydev = phy_dat;
756 if (PHY_HALTED == phydev->state)
757 return IRQ_NONE; /* It can't be ours. */
759 disable_irq_nosync(irq);
760 atomic_inc(&phydev->irq_disable);
768 * phy_enable_interrupts - Enable the interrupts from the PHY side
769 * @phydev: target phy_device struct
771 static int phy_enable_interrupts(struct phy_device *phydev)
773 int err = phy_clear_interrupt(phydev);
778 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
782 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
783 * @phydev: target phy_device struct
785 static int phy_disable_interrupts(struct phy_device *phydev)
789 /* Disable PHY interrupts */
790 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
794 /* Clear the interrupt */
795 err = phy_clear_interrupt(phydev);
808 * phy_start_interrupts - request and enable interrupts for a PHY device
809 * @phydev: target phy_device struct
811 * Description: Request the interrupt for the given PHY.
812 * If this fails, then we set irq to PHY_POLL.
813 * Otherwise, we enable the interrupts in the PHY.
814 * This should only be called with a valid IRQ number.
815 * Returns 0 on success or < 0 on error.
817 int phy_start_interrupts(struct phy_device *phydev)
819 atomic_set(&phydev->irq_disable, 0);
820 if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
821 IRQF_ONESHOT | IRQF_SHARED,
822 phydev_name(phydev), phydev) < 0) {
823 pr_warn("%s: Can't get IRQ %d (PHY)\n",
824 phydev->mdio.bus->name, phydev->irq);
825 phydev->irq = PHY_POLL;
829 return phy_enable_interrupts(phydev);
831 EXPORT_SYMBOL(phy_start_interrupts);
834 * phy_stop_interrupts - disable interrupts from a PHY device
835 * @phydev: target phy_device struct
837 int phy_stop_interrupts(struct phy_device *phydev)
839 int err = phy_disable_interrupts(phydev);
844 free_irq(phydev->irq, phydev);
846 /* If work indeed has been cancelled, disable_irq() will have
847 * been left unbalanced from phy_interrupt() and enable_irq()
848 * has to be called so that other devices on the line work.
850 while (atomic_dec_return(&phydev->irq_disable) >= 0)
851 enable_irq(phydev->irq);
855 EXPORT_SYMBOL(phy_stop_interrupts);
858 * phy_change - Called by the phy_interrupt to handle PHY changes
859 * @phydev: phy_device struct that interrupted
861 void phy_change(struct phy_device *phydev)
863 if (phy_interrupt_is_valid(phydev)) {
864 if (phydev->drv->did_interrupt &&
865 !phydev->drv->did_interrupt(phydev))
868 if (phy_disable_interrupts(phydev))
872 mutex_lock(&phydev->lock);
873 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
874 phydev->state = PHY_CHANGELINK;
875 mutex_unlock(&phydev->lock);
877 if (phy_interrupt_is_valid(phydev)) {
878 atomic_dec(&phydev->irq_disable);
879 enable_irq(phydev->irq);
881 /* Reenable interrupts */
882 if (PHY_HALTED != phydev->state &&
883 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
887 /* reschedule state queue work to run as soon as possible */
888 phy_trigger_machine(phydev, true);
892 atomic_dec(&phydev->irq_disable);
893 enable_irq(phydev->irq);
897 disable_irq(phydev->irq);
898 atomic_inc(&phydev->irq_disable);
904 * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
905 * @work: work_struct that describes the work to be done
907 void phy_change_work(struct work_struct *work)
909 struct phy_device *phydev =
910 container_of(work, struct phy_device, phy_queue);
916 * phy_stop - Bring down the PHY link, and stop checking the status
917 * @phydev: target phy_device struct
919 void phy_stop(struct phy_device *phydev)
921 mutex_lock(&phydev->lock);
923 if (PHY_HALTED == phydev->state)
926 if (phy_interrupt_is_valid(phydev)) {
927 /* Disable PHY Interrupts */
928 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
930 /* Clear any pending interrupts */
931 phy_clear_interrupt(phydev);
934 phydev->state = PHY_HALTED;
937 mutex_unlock(&phydev->lock);
939 /* Cannot call flush_scheduled_work() here as desired because
940 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
941 * will not reenable interrupts.
944 EXPORT_SYMBOL(phy_stop);
947 * phy_start - start or restart a PHY device
948 * @phydev: target phy_device struct
950 * Description: Indicates the attached device's readiness to
951 * handle PHY-related work. Used during startup to start the
952 * PHY, and after a call to phy_stop() to resume operation.
953 * Also used to indicate the MDIO bus has cleared an error
956 void phy_start(struct phy_device *phydev)
958 bool do_resume = false;
961 mutex_lock(&phydev->lock);
963 switch (phydev->state) {
965 phydev->state = PHY_PENDING;
968 phydev->state = PHY_UP;
971 /* make sure interrupts are re-enabled for the PHY */
972 if (phydev->irq != PHY_POLL) {
973 err = phy_enable_interrupts(phydev);
978 phydev->state = PHY_RESUMING;
984 mutex_unlock(&phydev->lock);
986 /* if phy was suspended, bring the physical link up again */
990 phy_trigger_machine(phydev, true);
992 EXPORT_SYMBOL(phy_start);
994 static void phy_adjust_link(struct phy_device *phydev)
996 phydev->adjust_link(phydev->attached_dev);
997 phy_led_trigger_change_speed(phydev);
1001 * phy_state_machine - Handle the state machine
1002 * @work: work_struct that describes the work to be done
1004 void phy_state_machine(struct work_struct *work)
1006 struct delayed_work *dwork = to_delayed_work(work);
1007 struct phy_device *phydev =
1008 container_of(dwork, struct phy_device, state_queue);
1009 bool needs_aneg = false, do_suspend = false;
1010 enum phy_state old_state;
1014 mutex_lock(&phydev->lock);
1016 old_state = phydev->state;
1018 if (phydev->drv && phydev->drv->link_change_notify)
1019 phydev->drv->link_change_notify(phydev);
1021 switch (phydev->state) {
1030 phydev->link_timeout = PHY_AN_TIMEOUT;
1034 err = phy_read_status(phydev);
1038 /* If the link is down, give up on negotiation for now */
1039 if (!phydev->link) {
1040 phydev->state = PHY_NOLINK;
1041 netif_carrier_off(phydev->attached_dev);
1042 phy_adjust_link(phydev);
1046 /* Check if negotiation is done. Break if there's an error */
1047 err = phy_aneg_done(phydev);
1051 /* If AN is done, we're running */
1053 phydev->state = PHY_RUNNING;
1054 netif_carrier_on(phydev->attached_dev);
1055 phy_adjust_link(phydev);
1057 } else if (0 == phydev->link_timeout--)
1061 if (phy_interrupt_is_valid(phydev))
1064 err = phy_read_status(phydev);
1069 if (AUTONEG_ENABLE == phydev->autoneg) {
1070 err = phy_aneg_done(phydev);
1075 phydev->state = PHY_AN;
1076 phydev->link_timeout = PHY_AN_TIMEOUT;
1080 phydev->state = PHY_RUNNING;
1081 netif_carrier_on(phydev->attached_dev);
1082 phy_adjust_link(phydev);
1086 err = genphy_update_link(phydev);
1091 phydev->state = PHY_RUNNING;
1092 netif_carrier_on(phydev->attached_dev);
1094 if (0 == phydev->link_timeout--)
1098 phy_adjust_link(phydev);
1101 /* Only register a CHANGE if we are polling and link changed
1102 * since latest checking.
1104 if (phydev->irq == PHY_POLL) {
1105 old_link = phydev->link;
1106 err = phy_read_status(phydev);
1110 if (old_link != phydev->link)
1111 phydev->state = PHY_CHANGELINK;
1114 * Failsafe: check that nobody set phydev->link=0 between two
1115 * poll cycles, otherwise we won't leave RUNNING state as long
1116 * as link remains down.
1118 if (!phydev->link && phydev->state == PHY_RUNNING) {
1119 phydev->state = PHY_CHANGELINK;
1120 phydev_err(phydev, "no link in PHY_RUNNING\n");
1123 case PHY_CHANGELINK:
1124 err = phy_read_status(phydev);
1129 phydev->state = PHY_RUNNING;
1130 netif_carrier_on(phydev->attached_dev);
1132 phydev->state = PHY_NOLINK;
1133 netif_carrier_off(phydev->attached_dev);
1136 phy_adjust_link(phydev);
1138 if (phy_interrupt_is_valid(phydev))
1139 err = phy_config_interrupt(phydev,
1140 PHY_INTERRUPT_ENABLED);
1145 netif_carrier_off(phydev->attached_dev);
1146 phy_adjust_link(phydev);
1151 if (AUTONEG_ENABLE == phydev->autoneg) {
1152 err = phy_aneg_done(phydev);
1156 /* err > 0 if AN is done.
1157 * Otherwise, it's 0, and we're still waiting for AN
1160 err = phy_read_status(phydev);
1165 phydev->state = PHY_RUNNING;
1166 netif_carrier_on(phydev->attached_dev);
1168 phydev->state = PHY_NOLINK;
1170 phy_adjust_link(phydev);
1172 phydev->state = PHY_AN;
1173 phydev->link_timeout = PHY_AN_TIMEOUT;
1176 err = phy_read_status(phydev);
1181 phydev->state = PHY_RUNNING;
1182 netif_carrier_on(phydev->attached_dev);
1184 phydev->state = PHY_NOLINK;
1186 phy_adjust_link(phydev);
1191 mutex_unlock(&phydev->lock);
1194 err = phy_start_aneg(phydev);
1195 else if (do_suspend)
1196 phy_suspend(phydev);
1201 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1202 phy_state_to_str(old_state),
1203 phy_state_to_str(phydev->state));
1205 /* Only re-schedule a PHY state machine change if we are polling the
1206 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1207 * between states from phy_mac_interrupt()
1209 if (phydev->irq == PHY_POLL)
1210 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1211 PHY_STATE_TIME * HZ);
1215 * phy_mac_interrupt - MAC says the link has changed
1216 * @phydev: phy_device struct with changed link
1217 * @new_link: Link is Up/Down.
1219 * Description: The MAC layer is able indicate there has been a change
1220 * in the PHY link status. Set the new link status, and trigger the
1221 * state machine, work a work queue.
1223 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1225 phydev->link = new_link;
1227 /* Trigger a state machine change */
1228 queue_work(system_power_efficient_wq, &phydev->phy_queue);
1230 EXPORT_SYMBOL(phy_mac_interrupt);
1233 * phy_init_eee - init and check the EEE feature
1234 * @phydev: target phy_device struct
1235 * @clk_stop_enable: PHY may stop the clock during LPI
1237 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1238 * is supported by looking at the MMD registers 3.20 and 7.60/61
1239 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1242 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1247 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1249 if (phydev->duplex == DUPLEX_FULL) {
1250 int eee_lp, eee_cap, eee_adv;
1254 /* Read phy status to properly get the right settings */
1255 status = phy_read_status(phydev);
1259 /* First check if the EEE ability is supported */
1260 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1264 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1268 /* Check which link settings negotiated and verify it in
1269 * the EEE advertising registers.
1271 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1275 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1279 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1280 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1281 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
1284 if (clk_stop_enable) {
1285 /* Configure the PHY to stop receiving xMII
1286 * clock while it is signaling LPI.
1288 int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1292 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1293 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
1296 return 0; /* EEE supported */
1299 return -EPROTONOSUPPORT;
1301 EXPORT_SYMBOL(phy_init_eee);
1304 * phy_get_eee_err - report the EEE wake error count
1305 * @phydev: target phy_device struct
1307 * Description: it is to report the number of time where the PHY
1308 * failed to complete its normal wake sequence.
1310 int phy_get_eee_err(struct phy_device *phydev)
1315 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1317 EXPORT_SYMBOL(phy_get_eee_err);
1320 * phy_ethtool_get_eee - get EEE supported and status
1321 * @phydev: target phy_device struct
1322 * @data: ethtool_eee data
1324 * Description: it reportes the Supported/Advertisement/LP Advertisement
1327 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1334 /* Get Supported EEE */
1335 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1338 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1340 /* Get advertisement EEE */
1341 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1344 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1346 /* Get LP advertisement EEE */
1347 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1350 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1354 EXPORT_SYMBOL(phy_ethtool_get_eee);
1357 * phy_ethtool_set_eee - set EEE supported and status
1358 * @phydev: target phy_device struct
1359 * @data: ethtool_eee data
1361 * Description: it is to program the Advertisement EEE register.
1363 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1365 int cap, old_adv, adv, ret;
1370 /* Get Supported EEE */
1371 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1375 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1379 adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1381 /* Mask prohibited EEE modes */
1382 adv &= ~phydev->eee_broken_modes;
1384 if (old_adv != adv) {
1385 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1389 /* Restart autonegotiation so the new modes get sent to the
1392 ret = genphy_restart_aneg(phydev);
1399 EXPORT_SYMBOL(phy_ethtool_set_eee);
1401 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1403 if (phydev->drv && phydev->drv->set_wol)
1404 return phydev->drv->set_wol(phydev, wol);
1408 EXPORT_SYMBOL(phy_ethtool_set_wol);
1410 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1412 if (phydev->drv && phydev->drv->get_wol)
1413 phydev->drv->get_wol(phydev, wol);
1415 EXPORT_SYMBOL(phy_ethtool_get_wol);
1417 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1418 struct ethtool_link_ksettings *cmd)
1420 struct phy_device *phydev = ndev->phydev;
1425 return phy_ethtool_ksettings_get(phydev, cmd);
1427 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1429 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1430 const struct ethtool_link_ksettings *cmd)
1432 struct phy_device *phydev = ndev->phydev;
1437 return phy_ethtool_ksettings_set(phydev, cmd);
1439 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1441 int phy_ethtool_nway_reset(struct net_device *ndev)
1443 struct phy_device *phydev = ndev->phydev;
1451 return genphy_restart_aneg(phydev);
1453 EXPORT_SYMBOL(phy_ethtool_nway_reset);