1 /* Framework for configuring and reading PHY devices
2 * Based on code in sungem_phy.c and gianfar_phy.c
6 * Copyright (c) 2004 Freescale Semiconductor, Inc.
7 * Copyright (c) 2006, 2007 Maciej W. Rozycki
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/unistd.h>
22 #include <linux/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/skbuff.h>
28 #include <linux/module.h>
29 #include <linux/mii.h>
30 #include <linux/ethtool.h>
31 #include <linux/phy.h>
32 #include <linux/phy_led_triggers.h>
33 #include <linux/workqueue.h>
34 #include <linux/mdio.h>
36 #include <linux/uaccess.h>
37 #include <linux/atomic.h>
41 static const char *phy_speed_to_str(int speed)
73 return "Unsupported (update phy.c)";
77 #define PHY_STATE_STR(_state) \
79 return __stringify(_state); \
81 static const char *phy_state_to_str(enum phy_state st)
85 PHY_STATE_STR(STARTING)
87 PHY_STATE_STR(PENDING)
90 PHY_STATE_STR(RUNNING)
92 PHY_STATE_STR(FORCING)
93 PHY_STATE_STR(CHANGELINK)
95 PHY_STATE_STR(RESUMING)
103 * phy_print_status - Convenience function to print out the current phy status
104 * @phydev: the phy_device struct
106 void phy_print_status(struct phy_device *phydev)
109 netdev_info(phydev->attached_dev,
110 "Link is Up - %s/%s - flow control %s\n",
111 phy_speed_to_str(phydev->speed),
112 DUPLEX_FULL == phydev->duplex ? "Full" : "Half",
113 phydev->pause ? "rx/tx" : "off");
115 netdev_info(phydev->attached_dev, "Link is Down\n");
118 EXPORT_SYMBOL(phy_print_status);
121 * phy_clear_interrupt - Ack the phy device's interrupt
122 * @phydev: the phy_device struct
124 * If the @phydev driver has an ack_interrupt function, call it to
125 * ack and clear the phy device's interrupt.
127 * Returns 0 on success or < 0 on error.
129 static int phy_clear_interrupt(struct phy_device *phydev)
131 if (phydev->drv->ack_interrupt)
132 return phydev->drv->ack_interrupt(phydev);
138 * phy_config_interrupt - configure the PHY device for the requested interrupts
139 * @phydev: the phy_device struct
140 * @interrupts: interrupt flags to configure for this @phydev
142 * Returns 0 on success or < 0 on error.
144 static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
146 phydev->interrupts = interrupts;
147 if (phydev->drv->config_intr)
148 return phydev->drv->config_intr(phydev);
154 * phy_restart_aneg - restart auto-negotiation
155 * @phydev: target phy_device struct
157 * Restart the autonegotiation on @phydev. Returns >= 0 on success or
158 * negative errno on error.
160 int phy_restart_aneg(struct phy_device *phydev)
164 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
165 ret = genphy_c45_restart_aneg(phydev);
167 ret = genphy_restart_aneg(phydev);
171 EXPORT_SYMBOL_GPL(phy_restart_aneg);
174 * phy_aneg_done - return auto-negotiation status
175 * @phydev: target phy_device struct
177 * Description: Return the auto-negotiation status from this @phydev
178 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
181 int phy_aneg_done(struct phy_device *phydev)
183 if (phydev->drv && phydev->drv->aneg_done)
184 return phydev->drv->aneg_done(phydev);
186 /* Avoid genphy_aneg_done() if the Clause 45 PHY does not
187 * implement Clause 22 registers
189 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
192 return genphy_aneg_done(phydev);
194 EXPORT_SYMBOL(phy_aneg_done);
196 /* A structure for mapping a particular speed and duplex
197 * combination to a particular SUPPORTED and ADVERTISED value
205 /* A mapping of all SUPPORTED settings to speed/duplex. This table
206 * must be grouped by speed and sorted in descending match priority
207 * - iow, descending speed. */
208 static const struct phy_setting settings[] = {
210 .speed = SPEED_10000,
211 .duplex = DUPLEX_FULL,
212 .bit = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
215 .speed = SPEED_10000,
216 .duplex = DUPLEX_FULL,
217 .bit = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
220 .speed = SPEED_10000,
221 .duplex = DUPLEX_FULL,
222 .bit = ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
226 .duplex = DUPLEX_FULL,
227 .bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
231 .duplex = DUPLEX_FULL,
232 .bit = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
236 .duplex = DUPLEX_FULL,
237 .bit = ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
241 .duplex = DUPLEX_HALF,
242 .bit = ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
246 .duplex = DUPLEX_FULL,
247 .bit = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
251 .duplex = DUPLEX_HALF,
252 .bit = ETHTOOL_LINK_MODE_100baseT_Half_BIT,
256 .duplex = DUPLEX_FULL,
257 .bit = ETHTOOL_LINK_MODE_10baseT_Full_BIT,
261 .duplex = DUPLEX_HALF,
262 .bit = ETHTOOL_LINK_MODE_10baseT_Half_BIT,
267 * phy_lookup_setting - lookup a PHY setting
268 * @speed: speed to match
269 * @duplex: duplex to match
270 * @mask: allowed link modes
271 * @maxbit: bit size of link modes
272 * @exact: an exact match is required
274 * Search the settings array for a setting that matches the speed and
275 * duplex, and which is supported.
277 * If @exact is unset, either an exact match or %NULL for no match will
280 * If @exact is set, an exact match, the fastest supported setting at
281 * or below the specified speed, the slowest supported setting, or if
282 * they all fail, %NULL will be returned.
284 static const struct phy_setting *
285 phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
286 size_t maxbit, bool exact)
288 const struct phy_setting *p, *match = NULL, *last = NULL;
291 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
292 if (p->bit < maxbit && test_bit(p->bit, mask)) {
294 if (p->speed == speed && p->duplex == duplex) {
295 /* Exact match for speed and duplex */
299 if (!match && p->speed <= speed)
303 if (p->speed < speed)
309 if (!match && !exact)
316 * phy_find_valid - find a PHY setting that matches the requested parameters
317 * @speed: desired speed
318 * @duplex: desired duplex
319 * @supported: mask of supported link modes
321 * Locate a supported phy setting that is, in priority order:
322 * - an exact match for the specified speed and duplex mode
323 * - a match for the specified speed, or slower speed
324 * - the slowest supported speed
325 * Returns the matched phy_setting entry, or %NULL if no supported phy
326 * settings were found.
328 static const struct phy_setting *
329 phy_find_valid(int speed, int duplex, u32 supported)
331 unsigned long mask = supported;
333 return phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, false);
337 * phy_supported_speeds - return all speeds currently supported by a phy device
338 * @phy: The phy device to return supported speeds of.
339 * @speeds: buffer to store supported speeds in.
340 * @size: size of speeds buffer.
342 * Description: Returns the number of supported speeds, and fills the speeds
343 * buffer with the supported speeds. If speeds buffer is too small to contain
344 * all currently supported speeds, will return as many speeds as can fit.
346 unsigned int phy_supported_speeds(struct phy_device *phy,
347 unsigned int *speeds,
350 unsigned long supported = phy->supported;
351 unsigned int count = 0;
352 unsigned int idx = 0;
354 for (idx = 0; idx < ARRAY_SIZE(settings) && count < size; idx++)
355 /* Assumes settings are grouped by speed */
356 if (settings[idx].bit < BITS_PER_LONG &&
357 !test_bit(settings[idx].bit, &supported) &&
358 (count == 0 || speeds[count - 1] != settings[idx].speed))
359 speeds[count++] = settings[idx].speed;
365 * phy_check_valid - check if there is a valid PHY setting which matches
366 * speed, duplex, and feature mask
367 * @speed: speed to match
368 * @duplex: duplex to match
369 * @features: A mask of the valid settings
371 * Description: Returns true if there is a valid setting, false otherwise.
373 static inline bool phy_check_valid(int speed, int duplex, u32 features)
375 unsigned long mask = features;
377 return !!phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, true);
381 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
382 * @phydev: the target phy_device struct
384 * Description: Make sure the PHY is set to supported speeds and
385 * duplexes. Drop down by one in this order: 1000/FULL,
386 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
388 static void phy_sanitize_settings(struct phy_device *phydev)
390 const struct phy_setting *setting;
391 u32 features = phydev->supported;
393 /* Sanitize settings based on PHY capabilities */
394 if ((features & SUPPORTED_Autoneg) == 0)
395 phydev->autoneg = AUTONEG_DISABLE;
397 setting = phy_find_valid(phydev->speed, phydev->duplex, features);
399 phydev->speed = setting->speed;
400 phydev->duplex = setting->duplex;
402 /* We failed to find anything (no supported speeds?) */
403 phydev->speed = SPEED_UNKNOWN;
404 phydev->duplex = DUPLEX_UNKNOWN;
409 * phy_ethtool_sset - generic ethtool sset function, handles all the details
410 * @phydev: target phy_device struct
413 * A few notes about parameter checking:
415 * - We don't set port or transceiver, so we don't care what they
417 * - phy_start_aneg() will make sure forced settings are sane, and
418 * choose the next best ones from the ones selected, so we don't
419 * care if ethtool tries to give us bad values.
421 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
423 u32 speed = ethtool_cmd_speed(cmd);
425 if (cmd->phy_address != phydev->mdio.addr)
428 /* We make sure that we don't pass unsupported values in to the PHY */
429 cmd->advertising &= phydev->supported;
431 /* Verify the settings we care about. */
432 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
435 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
438 if (cmd->autoneg == AUTONEG_DISABLE &&
439 ((speed != SPEED_1000 &&
440 speed != SPEED_100 &&
441 speed != SPEED_10) ||
442 (cmd->duplex != DUPLEX_HALF &&
443 cmd->duplex != DUPLEX_FULL)))
446 phydev->autoneg = cmd->autoneg;
448 phydev->speed = speed;
450 phydev->advertising = cmd->advertising;
452 if (AUTONEG_ENABLE == cmd->autoneg)
453 phydev->advertising |= ADVERTISED_Autoneg;
455 phydev->advertising &= ~ADVERTISED_Autoneg;
457 phydev->duplex = cmd->duplex;
459 phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
461 /* Restart the PHY */
462 phy_start_aneg(phydev);
466 EXPORT_SYMBOL(phy_ethtool_sset);
468 int phy_ethtool_ksettings_set(struct phy_device *phydev,
469 const struct ethtool_link_ksettings *cmd)
471 u8 autoneg = cmd->base.autoneg;
472 u8 duplex = cmd->base.duplex;
473 u32 speed = cmd->base.speed;
476 if (cmd->base.phy_address != phydev->mdio.addr)
479 ethtool_convert_link_mode_to_legacy_u32(&advertising,
480 cmd->link_modes.advertising);
482 /* We make sure that we don't pass unsupported values in to the PHY */
483 advertising &= phydev->supported;
485 /* Verify the settings we care about. */
486 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
489 if (autoneg == AUTONEG_ENABLE && advertising == 0)
492 if (autoneg == AUTONEG_DISABLE &&
493 ((speed != SPEED_1000 &&
494 speed != SPEED_100 &&
495 speed != SPEED_10) ||
496 (duplex != DUPLEX_HALF &&
497 duplex != DUPLEX_FULL)))
500 phydev->autoneg = autoneg;
502 phydev->speed = speed;
504 phydev->advertising = advertising;
506 if (autoneg == AUTONEG_ENABLE)
507 phydev->advertising |= ADVERTISED_Autoneg;
509 phydev->advertising &= ~ADVERTISED_Autoneg;
511 phydev->duplex = duplex;
513 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
515 /* Restart the PHY */
516 phy_start_aneg(phydev);
520 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
522 void phy_ethtool_ksettings_get(struct phy_device *phydev,
523 struct ethtool_link_ksettings *cmd)
525 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
528 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
529 phydev->advertising);
531 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
532 phydev->lp_advertising);
534 cmd->base.speed = phydev->speed;
535 cmd->base.duplex = phydev->duplex;
536 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
537 cmd->base.port = PORT_BNC;
539 cmd->base.port = PORT_MII;
541 cmd->base.phy_address = phydev->mdio.addr;
542 cmd->base.autoneg = phydev->autoneg;
543 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
544 cmd->base.eth_tp_mdix = phydev->mdix;
546 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
549 * phy_mii_ioctl - generic PHY MII ioctl interface
550 * @phydev: the phy_device struct
551 * @ifr: &struct ifreq for socket ioctl's
552 * @cmd: ioctl cmd to execute
554 * Note that this function is currently incompatible with the
555 * PHYCONTROL layer. It changes registers without regard to
556 * current state. Use at own risk.
558 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
560 struct mii_ioctl_data *mii_data = if_mii(ifr);
561 u16 val = mii_data->val_in;
562 bool change_autoneg = false;
566 mii_data->phy_id = phydev->mdio.addr;
570 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
576 if (mii_data->phy_id == phydev->mdio.addr) {
577 switch (mii_data->reg_num) {
579 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
580 if (phydev->autoneg == AUTONEG_ENABLE)
581 change_autoneg = true;
582 phydev->autoneg = AUTONEG_DISABLE;
583 if (val & BMCR_FULLDPLX)
584 phydev->duplex = DUPLEX_FULL;
586 phydev->duplex = DUPLEX_HALF;
587 if (val & BMCR_SPEED1000)
588 phydev->speed = SPEED_1000;
589 else if (val & BMCR_SPEED100)
590 phydev->speed = SPEED_100;
591 else phydev->speed = SPEED_10;
594 if (phydev->autoneg == AUTONEG_DISABLE)
595 change_autoneg = true;
596 phydev->autoneg = AUTONEG_ENABLE;
600 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
601 change_autoneg = true;
609 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
610 mii_data->reg_num, val);
612 if (mii_data->phy_id == phydev->mdio.addr &&
613 mii_data->reg_num == MII_BMCR &&
615 return phy_init_hw(phydev);
618 return phy_start_aneg(phydev);
623 if (phydev->drv && phydev->drv->hwtstamp)
624 return phydev->drv->hwtstamp(phydev, ifr);
631 EXPORT_SYMBOL(phy_mii_ioctl);
634 * phy_start_aneg_priv - start auto-negotiation for this PHY device
635 * @phydev: the phy_device struct
636 * @sync: indicate whether we should wait for the workqueue cancelation
638 * Description: Sanitizes the settings (if we're not autonegotiating
639 * them), and then calls the driver's config_aneg function.
640 * If the PHYCONTROL Layer is operating, we change the state to
641 * reflect the beginning of Auto-negotiation or forcing.
643 static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
651 mutex_lock(&phydev->lock);
653 if (AUTONEG_DISABLE == phydev->autoneg)
654 phy_sanitize_settings(phydev);
656 /* Invalidate LP advertising flags */
657 phydev->lp_advertising = 0;
659 err = phydev->drv->config_aneg(phydev);
663 if (phydev->state != PHY_HALTED) {
664 if (AUTONEG_ENABLE == phydev->autoneg) {
665 phydev->state = PHY_AN;
666 phydev->link_timeout = PHY_AN_TIMEOUT;
668 phydev->state = PHY_FORCING;
669 phydev->link_timeout = PHY_FORCE_TIMEOUT;
673 /* Re-schedule a PHY state machine to check PHY status because
674 * negotiation may already be done and aneg interrupt may not be
677 if (phy_interrupt_is_valid(phydev) && (phydev->state == PHY_AN)) {
678 err = phy_aneg_done(phydev);
686 mutex_unlock(&phydev->lock);
689 phy_trigger_machine(phydev, sync);
695 * phy_start_aneg - start auto-negotiation for this PHY device
696 * @phydev: the phy_device struct
698 * Description: Sanitizes the settings (if we're not autonegotiating
699 * them), and then calls the driver's config_aneg function.
700 * If the PHYCONTROL Layer is operating, we change the state to
701 * reflect the beginning of Auto-negotiation or forcing.
703 int phy_start_aneg(struct phy_device *phydev)
705 return phy_start_aneg_priv(phydev, true);
707 EXPORT_SYMBOL(phy_start_aneg);
710 * phy_start_machine - start PHY state machine tracking
711 * @phydev: the phy_device struct
713 * Description: The PHY infrastructure can run a state machine
714 * which tracks whether the PHY is starting up, negotiating,
715 * etc. This function starts the delayed workqueue which tracks
716 * the state of the PHY. If you want to maintain your own state machine,
717 * do not call this function.
719 void phy_start_machine(struct phy_device *phydev)
721 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
725 * phy_trigger_machine - trigger the state machine to run
727 * @phydev: the phy_device struct
728 * @sync: indicate whether we should wait for the workqueue cancelation
730 * Description: There has been a change in state which requires that the
731 * state machine runs.
734 void phy_trigger_machine(struct phy_device *phydev, bool sync)
737 cancel_delayed_work_sync(&phydev->state_queue);
739 cancel_delayed_work(&phydev->state_queue);
740 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
744 * phy_stop_machine - stop the PHY state machine tracking
745 * @phydev: target phy_device struct
747 * Description: Stops the state machine delayed workqueue, sets the
748 * state to UP (unless it wasn't up yet). This function must be
749 * called BEFORE phy_detach.
751 void phy_stop_machine(struct phy_device *phydev)
753 cancel_delayed_work_sync(&phydev->state_queue);
755 mutex_lock(&phydev->lock);
756 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
757 phydev->state = PHY_UP;
758 mutex_unlock(&phydev->lock);
760 /* Now we can run the state machine synchronously */
761 phy_state_machine(&phydev->state_queue.work);
765 * phy_error - enter HALTED state for this PHY device
766 * @phydev: target phy_device struct
768 * Moves the PHY to the HALTED state in response to a read
769 * or write error, and tells the controller the link is down.
770 * Must not be called from interrupt context, or while the
771 * phydev->lock is held.
773 static void phy_error(struct phy_device *phydev)
775 mutex_lock(&phydev->lock);
776 phydev->state = PHY_HALTED;
777 mutex_unlock(&phydev->lock);
779 phy_trigger_machine(phydev, false);
783 * phy_interrupt - PHY interrupt handler
784 * @irq: interrupt line
785 * @phy_dat: phy_device pointer
787 * Description: When a PHY interrupt occurs, the handler disables
788 * interrupts, and uses phy_change to handle the interrupt.
790 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
792 struct phy_device *phydev = phy_dat;
794 if (PHY_HALTED == phydev->state)
795 return IRQ_NONE; /* It can't be ours. */
797 disable_irq_nosync(irq);
798 atomic_inc(&phydev->irq_disable);
806 * phy_enable_interrupts - Enable the interrupts from the PHY side
807 * @phydev: target phy_device struct
809 static int phy_enable_interrupts(struct phy_device *phydev)
811 int err = phy_clear_interrupt(phydev);
816 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
820 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
821 * @phydev: target phy_device struct
823 static int phy_disable_interrupts(struct phy_device *phydev)
827 /* Disable PHY interrupts */
828 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
832 /* Clear the interrupt */
833 err = phy_clear_interrupt(phydev);
846 * phy_start_interrupts - request and enable interrupts for a PHY device
847 * @phydev: target phy_device struct
849 * Description: Request the interrupt for the given PHY.
850 * If this fails, then we set irq to PHY_POLL.
851 * Otherwise, we enable the interrupts in the PHY.
852 * This should only be called with a valid IRQ number.
853 * Returns 0 on success or < 0 on error.
855 int phy_start_interrupts(struct phy_device *phydev)
857 atomic_set(&phydev->irq_disable, 0);
858 if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
859 IRQF_ONESHOT | IRQF_SHARED,
860 phydev_name(phydev), phydev) < 0) {
861 pr_warn("%s: Can't get IRQ %d (PHY)\n",
862 phydev->mdio.bus->name, phydev->irq);
863 phydev->irq = PHY_POLL;
867 return phy_enable_interrupts(phydev);
869 EXPORT_SYMBOL(phy_start_interrupts);
872 * phy_stop_interrupts - disable interrupts from a PHY device
873 * @phydev: target phy_device struct
875 int phy_stop_interrupts(struct phy_device *phydev)
877 int err = phy_disable_interrupts(phydev);
882 free_irq(phydev->irq, phydev);
884 /* If work indeed has been cancelled, disable_irq() will have
885 * been left unbalanced from phy_interrupt() and enable_irq()
886 * has to be called so that other devices on the line work.
888 while (atomic_dec_return(&phydev->irq_disable) >= 0)
889 enable_irq(phydev->irq);
893 EXPORT_SYMBOL(phy_stop_interrupts);
896 * phy_change - Called by the phy_interrupt to handle PHY changes
897 * @phydev: phy_device struct that interrupted
899 void phy_change(struct phy_device *phydev)
901 if (phy_interrupt_is_valid(phydev)) {
902 if (phydev->drv->did_interrupt &&
903 !phydev->drv->did_interrupt(phydev))
906 if (phy_disable_interrupts(phydev))
910 mutex_lock(&phydev->lock);
911 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
912 phydev->state = PHY_CHANGELINK;
913 mutex_unlock(&phydev->lock);
915 if (phy_interrupt_is_valid(phydev)) {
916 atomic_dec(&phydev->irq_disable);
917 enable_irq(phydev->irq);
919 /* Reenable interrupts */
920 if (PHY_HALTED != phydev->state &&
921 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
925 /* reschedule state queue work to run as soon as possible */
926 phy_trigger_machine(phydev, true);
930 atomic_dec(&phydev->irq_disable);
931 enable_irq(phydev->irq);
935 disable_irq(phydev->irq);
936 atomic_inc(&phydev->irq_disable);
942 * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
943 * @work: work_struct that describes the work to be done
945 void phy_change_work(struct work_struct *work)
947 struct phy_device *phydev =
948 container_of(work, struct phy_device, phy_queue);
954 * phy_stop - Bring down the PHY link, and stop checking the status
955 * @phydev: target phy_device struct
957 void phy_stop(struct phy_device *phydev)
959 mutex_lock(&phydev->lock);
961 if (PHY_HALTED == phydev->state)
964 if (phy_interrupt_is_valid(phydev)) {
965 /* Disable PHY Interrupts */
966 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
968 /* Clear any pending interrupts */
969 phy_clear_interrupt(phydev);
972 phydev->state = PHY_HALTED;
975 mutex_unlock(&phydev->lock);
977 /* Cannot call flush_scheduled_work() here as desired because
978 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
979 * will not reenable interrupts.
982 EXPORT_SYMBOL(phy_stop);
985 * phy_start - start or restart a PHY device
986 * @phydev: target phy_device struct
988 * Description: Indicates the attached device's readiness to
989 * handle PHY-related work. Used during startup to start the
990 * PHY, and after a call to phy_stop() to resume operation.
991 * Also used to indicate the MDIO bus has cleared an error
994 void phy_start(struct phy_device *phydev)
996 bool do_resume = false;
999 mutex_lock(&phydev->lock);
1001 switch (phydev->state) {
1003 phydev->state = PHY_PENDING;
1006 phydev->state = PHY_UP;
1009 /* make sure interrupts are re-enabled for the PHY */
1010 if (phydev->irq != PHY_POLL) {
1011 err = phy_enable_interrupts(phydev);
1016 phydev->state = PHY_RESUMING;
1022 mutex_unlock(&phydev->lock);
1024 /* if phy was suspended, bring the physical link up again */
1028 phy_trigger_machine(phydev, true);
1030 EXPORT_SYMBOL(phy_start);
1032 static void phy_adjust_link(struct phy_device *phydev)
1034 phydev->adjust_link(phydev->attached_dev);
1035 phy_led_trigger_change_speed(phydev);
1039 * phy_state_machine - Handle the state machine
1040 * @work: work_struct that describes the work to be done
1042 void phy_state_machine(struct work_struct *work)
1044 struct delayed_work *dwork = to_delayed_work(work);
1045 struct phy_device *phydev =
1046 container_of(dwork, struct phy_device, state_queue);
1047 bool needs_aneg = false, do_suspend = false;
1048 enum phy_state old_state;
1052 mutex_lock(&phydev->lock);
1054 old_state = phydev->state;
1056 if (phydev->drv && phydev->drv->link_change_notify)
1057 phydev->drv->link_change_notify(phydev);
1059 switch (phydev->state) {
1068 phydev->link_timeout = PHY_AN_TIMEOUT;
1072 err = phy_read_status(phydev);
1076 /* If the link is down, give up on negotiation for now */
1077 if (!phydev->link) {
1078 phydev->state = PHY_NOLINK;
1079 netif_carrier_off(phydev->attached_dev);
1080 phy_adjust_link(phydev);
1084 /* Check if negotiation is done. Break if there's an error */
1085 err = phy_aneg_done(phydev);
1089 /* If AN is done, we're running */
1091 phydev->state = PHY_RUNNING;
1092 netif_carrier_on(phydev->attached_dev);
1093 phy_adjust_link(phydev);
1095 } else if (0 == phydev->link_timeout--)
1099 if (phy_interrupt_is_valid(phydev))
1102 err = phy_read_status(phydev);
1107 if (AUTONEG_ENABLE == phydev->autoneg) {
1108 err = phy_aneg_done(phydev);
1113 phydev->state = PHY_AN;
1114 phydev->link_timeout = PHY_AN_TIMEOUT;
1118 phydev->state = PHY_RUNNING;
1119 netif_carrier_on(phydev->attached_dev);
1120 phy_adjust_link(phydev);
1124 err = genphy_update_link(phydev);
1129 phydev->state = PHY_RUNNING;
1130 netif_carrier_on(phydev->attached_dev);
1132 if (0 == phydev->link_timeout--)
1136 phy_adjust_link(phydev);
1139 /* Only register a CHANGE if we are polling and link changed
1140 * since latest checking.
1142 if (phydev->irq == PHY_POLL) {
1143 old_link = phydev->link;
1144 err = phy_read_status(phydev);
1148 if (old_link != phydev->link)
1149 phydev->state = PHY_CHANGELINK;
1152 * Failsafe: check that nobody set phydev->link=0 between two
1153 * poll cycles, otherwise we won't leave RUNNING state as long
1154 * as link remains down.
1156 if (!phydev->link && phydev->state == PHY_RUNNING) {
1157 phydev->state = PHY_CHANGELINK;
1158 phydev_err(phydev, "no link in PHY_RUNNING\n");
1161 case PHY_CHANGELINK:
1162 err = phy_read_status(phydev);
1167 phydev->state = PHY_RUNNING;
1168 netif_carrier_on(phydev->attached_dev);
1170 phydev->state = PHY_NOLINK;
1171 netif_carrier_off(phydev->attached_dev);
1174 phy_adjust_link(phydev);
1176 if (phy_interrupt_is_valid(phydev))
1177 err = phy_config_interrupt(phydev,
1178 PHY_INTERRUPT_ENABLED);
1183 netif_carrier_off(phydev->attached_dev);
1184 phy_adjust_link(phydev);
1189 if (AUTONEG_ENABLE == phydev->autoneg) {
1190 err = phy_aneg_done(phydev);
1194 /* err > 0 if AN is done.
1195 * Otherwise, it's 0, and we're still waiting for AN
1198 err = phy_read_status(phydev);
1203 phydev->state = PHY_RUNNING;
1204 netif_carrier_on(phydev->attached_dev);
1206 phydev->state = PHY_NOLINK;
1208 phy_adjust_link(phydev);
1210 phydev->state = PHY_AN;
1211 phydev->link_timeout = PHY_AN_TIMEOUT;
1214 err = phy_read_status(phydev);
1219 phydev->state = PHY_RUNNING;
1220 netif_carrier_on(phydev->attached_dev);
1222 phydev->state = PHY_NOLINK;
1224 phy_adjust_link(phydev);
1229 mutex_unlock(&phydev->lock);
1232 err = phy_start_aneg_priv(phydev, false);
1233 else if (do_suspend)
1234 phy_suspend(phydev);
1239 if (old_state != phydev->state)
1240 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1241 phy_state_to_str(old_state),
1242 phy_state_to_str(phydev->state));
1244 /* Only re-schedule a PHY state machine change if we are polling the
1245 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1246 * between states from phy_mac_interrupt()
1248 if (phydev->irq == PHY_POLL)
1249 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1250 PHY_STATE_TIME * HZ);
1254 * phy_mac_interrupt - MAC says the link has changed
1255 * @phydev: phy_device struct with changed link
1256 * @new_link: Link is Up/Down.
1258 * Description: The MAC layer is able indicate there has been a change
1259 * in the PHY link status. Set the new link status, and trigger the
1260 * state machine, work a work queue.
1262 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1264 phydev->link = new_link;
1266 /* Trigger a state machine change */
1267 queue_work(system_power_efficient_wq, &phydev->phy_queue);
1269 EXPORT_SYMBOL(phy_mac_interrupt);
1272 * phy_init_eee - init and check the EEE feature
1273 * @phydev: target phy_device struct
1274 * @clk_stop_enable: PHY may stop the clock during LPI
1276 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1277 * is supported by looking at the MMD registers 3.20 and 7.60/61
1278 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1281 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1286 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1288 if (phydev->duplex == DUPLEX_FULL) {
1289 int eee_lp, eee_cap, eee_adv;
1293 /* Read phy status to properly get the right settings */
1294 status = phy_read_status(phydev);
1298 /* First check if the EEE ability is supported */
1299 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1303 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1307 /* Check which link settings negotiated and verify it in
1308 * the EEE advertising registers.
1310 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1314 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1318 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1319 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1320 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
1323 if (clk_stop_enable) {
1324 /* Configure the PHY to stop receiving xMII
1325 * clock while it is signaling LPI.
1327 int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1331 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1332 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
1335 return 0; /* EEE supported */
1338 return -EPROTONOSUPPORT;
1340 EXPORT_SYMBOL(phy_init_eee);
1343 * phy_get_eee_err - report the EEE wake error count
1344 * @phydev: target phy_device struct
1346 * Description: it is to report the number of time where the PHY
1347 * failed to complete its normal wake sequence.
1349 int phy_get_eee_err(struct phy_device *phydev)
1354 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1356 EXPORT_SYMBOL(phy_get_eee_err);
1359 * phy_ethtool_get_eee - get EEE supported and status
1360 * @phydev: target phy_device struct
1361 * @data: ethtool_eee data
1363 * Description: it reportes the Supported/Advertisement/LP Advertisement
1366 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1373 /* Get Supported EEE */
1374 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1377 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1379 /* Get advertisement EEE */
1380 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1383 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1385 /* Get LP advertisement EEE */
1386 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1389 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1393 EXPORT_SYMBOL(phy_ethtool_get_eee);
1396 * phy_ethtool_set_eee - set EEE supported and status
1397 * @phydev: target phy_device struct
1398 * @data: ethtool_eee data
1400 * Description: it is to program the Advertisement EEE register.
1402 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1404 int cap, old_adv, adv, ret;
1409 /* Get Supported EEE */
1410 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1414 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1418 adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1420 /* Mask prohibited EEE modes */
1421 adv &= ~phydev->eee_broken_modes;
1423 if (old_adv != adv) {
1424 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1428 /* Restart autonegotiation so the new modes get sent to the
1431 ret = phy_restart_aneg(phydev);
1438 EXPORT_SYMBOL(phy_ethtool_set_eee);
1440 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1442 if (phydev->drv && phydev->drv->set_wol)
1443 return phydev->drv->set_wol(phydev, wol);
1447 EXPORT_SYMBOL(phy_ethtool_set_wol);
1449 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1451 if (phydev->drv && phydev->drv->get_wol)
1452 phydev->drv->get_wol(phydev, wol);
1454 EXPORT_SYMBOL(phy_ethtool_get_wol);
1456 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1457 struct ethtool_link_ksettings *cmd)
1459 struct phy_device *phydev = ndev->phydev;
1464 phy_ethtool_ksettings_get(phydev, cmd);
1468 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1470 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1471 const struct ethtool_link_ksettings *cmd)
1473 struct phy_device *phydev = ndev->phydev;
1478 return phy_ethtool_ksettings_set(phydev, cmd);
1480 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1482 int phy_ethtool_nway_reset(struct net_device *ndev)
1484 struct phy_device *phydev = ndev->phydev;
1492 return phy_restart_aneg(phydev);
1494 EXPORT_SYMBOL(phy_ethtool_nway_reset);