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/timer.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)
57 return "Unsupported (update phy.c)";
61 #define PHY_STATE_STR(_state) \
63 return __stringify(_state); \
65 static const char *phy_state_to_str(enum phy_state st)
69 PHY_STATE_STR(STARTING)
71 PHY_STATE_STR(PENDING)
74 PHY_STATE_STR(RUNNING)
76 PHY_STATE_STR(FORCING)
77 PHY_STATE_STR(CHANGELINK)
79 PHY_STATE_STR(RESUMING)
87 * phy_print_status - Convenience function to print out the current phy status
88 * @phydev: the phy_device struct
90 void phy_print_status(struct phy_device *phydev)
93 netdev_info(phydev->attached_dev,
94 "Link is Up - %s/%s - flow control %s\n",
95 phy_speed_to_str(phydev->speed),
96 DUPLEX_FULL == phydev->duplex ? "Full" : "Half",
97 phydev->pause ? "rx/tx" : "off");
99 netdev_info(phydev->attached_dev, "Link is Down\n");
102 EXPORT_SYMBOL(phy_print_status);
105 * phy_clear_interrupt - Ack the phy device's interrupt
106 * @phydev: the phy_device struct
108 * If the @phydev driver has an ack_interrupt function, call it to
109 * ack and clear the phy device's interrupt.
111 * Returns 0 on success or < 0 on error.
113 static int phy_clear_interrupt(struct phy_device *phydev)
115 if (phydev->drv->ack_interrupt)
116 return phydev->drv->ack_interrupt(phydev);
122 * phy_config_interrupt - configure the PHY device for the requested interrupts
123 * @phydev: the phy_device struct
124 * @interrupts: interrupt flags to configure for this @phydev
126 * Returns 0 on success or < 0 on error.
128 static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
130 phydev->interrupts = interrupts;
131 if (phydev->drv->config_intr)
132 return phydev->drv->config_intr(phydev);
139 * phy_aneg_done - return auto-negotiation status
140 * @phydev: target phy_device struct
142 * Description: Return the auto-negotiation status from this @phydev
143 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
146 static inline int phy_aneg_done(struct phy_device *phydev)
148 if (phydev->drv->aneg_done)
149 return phydev->drv->aneg_done(phydev);
151 /* Avoid genphy_aneg_done() if the Clause 45 PHY does not
152 * implement Clause 22 registers
154 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
157 return genphy_aneg_done(phydev);
160 /* A structure for mapping a particular speed and duplex
161 * combination to a particular SUPPORTED and ADVERTISED value
169 /* A mapping of all SUPPORTED settings to speed/duplex */
170 static const struct phy_setting settings[] = {
172 .speed = SPEED_10000,
173 .duplex = DUPLEX_FULL,
174 .setting = SUPPORTED_10000baseKR_Full,
177 .speed = SPEED_10000,
178 .duplex = DUPLEX_FULL,
179 .setting = SUPPORTED_10000baseKX4_Full,
182 .speed = SPEED_10000,
183 .duplex = DUPLEX_FULL,
184 .setting = SUPPORTED_10000baseT_Full,
188 .duplex = DUPLEX_FULL,
189 .setting = SUPPORTED_2500baseX_Full,
193 .duplex = DUPLEX_FULL,
194 .setting = SUPPORTED_1000baseKX_Full,
198 .duplex = DUPLEX_FULL,
199 .setting = SUPPORTED_1000baseT_Full,
203 .duplex = DUPLEX_HALF,
204 .setting = SUPPORTED_1000baseT_Half,
208 .duplex = DUPLEX_FULL,
209 .setting = SUPPORTED_100baseT_Full,
213 .duplex = DUPLEX_HALF,
214 .setting = SUPPORTED_100baseT_Half,
218 .duplex = DUPLEX_FULL,
219 .setting = SUPPORTED_10baseT_Full,
223 .duplex = DUPLEX_HALF,
224 .setting = SUPPORTED_10baseT_Half,
228 #define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
231 * phy_find_setting - find a PHY settings array entry that matches speed & duplex
232 * @speed: speed to match
233 * @duplex: duplex to match
235 * Description: Searches the settings array for the setting which
236 * matches the desired speed and duplex, and returns the index
237 * of that setting. Returns the index of the last setting if
238 * none of the others match.
240 static inline unsigned int phy_find_setting(int speed, int duplex)
242 unsigned int idx = 0;
244 while (idx < ARRAY_SIZE(settings) &&
245 (settings[idx].speed != speed || settings[idx].duplex != duplex))
248 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
252 * phy_find_valid - find a PHY setting that matches the requested features mask
253 * @idx: The first index in settings[] to search
254 * @features: A mask of the valid settings
256 * Description: Returns the index of the first valid setting less
257 * than or equal to the one pointed to by idx, as determined by
258 * the mask in features. Returns the index of the last setting
259 * if nothing else matches.
261 static inline unsigned int phy_find_valid(unsigned int idx, u32 features)
263 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
266 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
270 * phy_check_valid - check if there is a valid PHY setting which matches
271 * speed, duplex, and feature mask
272 * @speed: speed to match
273 * @duplex: duplex to match
274 * @features: A mask of the valid settings
276 * Description: Returns true if there is a valid setting, false otherwise.
278 static inline bool phy_check_valid(int speed, int duplex, u32 features)
282 idx = phy_find_valid(phy_find_setting(speed, duplex), features);
284 return settings[idx].speed == speed && settings[idx].duplex == duplex &&
285 (settings[idx].setting & features);
289 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
290 * @phydev: the target phy_device struct
292 * Description: Make sure the PHY is set to supported speeds and
293 * duplexes. Drop down by one in this order: 1000/FULL,
294 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
296 static void phy_sanitize_settings(struct phy_device *phydev)
298 u32 features = phydev->supported;
301 /* Sanitize settings based on PHY capabilities */
302 if ((features & SUPPORTED_Autoneg) == 0)
303 phydev->autoneg = AUTONEG_DISABLE;
305 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
308 phydev->speed = settings[idx].speed;
309 phydev->duplex = settings[idx].duplex;
313 * phy_ethtool_sset - generic ethtool sset function, handles all the details
314 * @phydev: target phy_device struct
317 * A few notes about parameter checking:
318 * - We don't set port or transceiver, so we don't care what they
320 * - phy_start_aneg() will make sure forced settings are sane, and
321 * choose the next best ones from the ones selected, so we don't
322 * care if ethtool tries to give us bad values.
324 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
326 u32 speed = ethtool_cmd_speed(cmd);
328 if (cmd->phy_address != phydev->mdio.addr)
331 /* We make sure that we don't pass unsupported values in to the PHY */
332 cmd->advertising &= phydev->supported;
334 /* Verify the settings we care about. */
335 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
338 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
341 if (cmd->autoneg == AUTONEG_DISABLE &&
342 ((speed != SPEED_1000 &&
343 speed != SPEED_100 &&
344 speed != SPEED_10) ||
345 (cmd->duplex != DUPLEX_HALF &&
346 cmd->duplex != DUPLEX_FULL)))
349 phydev->autoneg = cmd->autoneg;
351 phydev->speed = speed;
353 phydev->advertising = cmd->advertising;
355 if (AUTONEG_ENABLE == cmd->autoneg)
356 phydev->advertising |= ADVERTISED_Autoneg;
358 phydev->advertising &= ~ADVERTISED_Autoneg;
360 phydev->duplex = cmd->duplex;
362 phydev->mdix = cmd->eth_tp_mdix_ctrl;
364 /* Restart the PHY */
365 phy_start_aneg(phydev);
369 EXPORT_SYMBOL(phy_ethtool_sset);
371 int phy_ethtool_ksettings_set(struct phy_device *phydev,
372 const struct ethtool_link_ksettings *cmd)
374 u8 autoneg = cmd->base.autoneg;
375 u8 duplex = cmd->base.duplex;
376 u32 speed = cmd->base.speed;
379 if (cmd->base.phy_address != phydev->mdio.addr)
382 ethtool_convert_link_mode_to_legacy_u32(&advertising,
383 cmd->link_modes.advertising);
385 /* We make sure that we don't pass unsupported values in to the PHY */
386 advertising &= phydev->supported;
388 /* Verify the settings we care about. */
389 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
392 if (autoneg == AUTONEG_ENABLE && advertising == 0)
395 if (autoneg == AUTONEG_DISABLE &&
396 ((speed != SPEED_1000 &&
397 speed != SPEED_100 &&
398 speed != SPEED_10) ||
399 (duplex != DUPLEX_HALF &&
400 duplex != DUPLEX_FULL)))
403 phydev->autoneg = autoneg;
405 phydev->speed = speed;
407 phydev->advertising = advertising;
409 if (autoneg == AUTONEG_ENABLE)
410 phydev->advertising |= ADVERTISED_Autoneg;
412 phydev->advertising &= ~ADVERTISED_Autoneg;
414 phydev->duplex = duplex;
416 phydev->mdix = cmd->base.eth_tp_mdix_ctrl;
418 /* Restart the PHY */
419 phy_start_aneg(phydev);
423 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
425 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
427 cmd->supported = phydev->supported;
429 cmd->advertising = phydev->advertising;
430 cmd->lp_advertising = phydev->lp_advertising;
432 ethtool_cmd_speed_set(cmd, phydev->speed);
433 cmd->duplex = phydev->duplex;
434 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
435 cmd->port = PORT_BNC;
437 cmd->port = PORT_MII;
438 cmd->phy_address = phydev->mdio.addr;
439 cmd->transceiver = phy_is_internal(phydev) ?
440 XCVR_INTERNAL : XCVR_EXTERNAL;
441 cmd->autoneg = phydev->autoneg;
442 cmd->eth_tp_mdix_ctrl = phydev->mdix;
446 EXPORT_SYMBOL(phy_ethtool_gset);
448 int phy_ethtool_ksettings_get(struct phy_device *phydev,
449 struct ethtool_link_ksettings *cmd)
451 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
454 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
455 phydev->advertising);
457 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
458 phydev->lp_advertising);
460 cmd->base.speed = phydev->speed;
461 cmd->base.duplex = phydev->duplex;
462 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
463 cmd->base.port = PORT_BNC;
465 cmd->base.port = PORT_MII;
466 cmd->base.transceiver = phy_is_internal(phydev) ?
467 XCVR_INTERNAL : XCVR_EXTERNAL;
468 cmd->base.phy_address = phydev->mdio.addr;
469 cmd->base.autoneg = phydev->autoneg;
470 cmd->base.eth_tp_mdix_ctrl = phydev->mdix;
474 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
477 * phy_mii_ioctl - generic PHY MII ioctl interface
478 * @phydev: the phy_device struct
479 * @ifr: &struct ifreq for socket ioctl's
480 * @cmd: ioctl cmd to execute
482 * Note that this function is currently incompatible with the
483 * PHYCONTROL layer. It changes registers without regard to
484 * current state. Use at own risk.
486 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
488 struct mii_ioctl_data *mii_data = if_mii(ifr);
489 u16 val = mii_data->val_in;
490 bool change_autoneg = false;
494 mii_data->phy_id = phydev->mdio.addr;
498 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
504 if (mii_data->phy_id == phydev->mdio.addr) {
505 switch (mii_data->reg_num) {
507 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
508 if (phydev->autoneg == AUTONEG_ENABLE)
509 change_autoneg = true;
510 phydev->autoneg = AUTONEG_DISABLE;
511 if (val & BMCR_FULLDPLX)
512 phydev->duplex = DUPLEX_FULL;
514 phydev->duplex = DUPLEX_HALF;
515 if (val & BMCR_SPEED1000)
516 phydev->speed = SPEED_1000;
517 else if (val & BMCR_SPEED100)
518 phydev->speed = SPEED_100;
519 else phydev->speed = SPEED_10;
522 if (phydev->autoneg == AUTONEG_DISABLE)
523 change_autoneg = true;
524 phydev->autoneg = AUTONEG_ENABLE;
528 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
529 change_autoneg = true;
537 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
538 mii_data->reg_num, val);
540 if (mii_data->phy_id == phydev->mdio.addr &&
541 mii_data->reg_num == MII_BMCR &&
543 return phy_init_hw(phydev);
546 return phy_start_aneg(phydev);
551 if (phydev->drv->hwtstamp)
552 return phydev->drv->hwtstamp(phydev, ifr);
559 EXPORT_SYMBOL(phy_mii_ioctl);
562 * phy_start_aneg_priv - start auto-negotiation for this PHY device
563 * @phydev: the phy_device struct
564 * @sync: indicate whether we should wait for the workqueue cancelation
566 * Description: Sanitizes the settings (if we're not autonegotiating
567 * them), and then calls the driver's config_aneg function.
568 * If the PHYCONTROL Layer is operating, we change the state to
569 * reflect the beginning of Auto-negotiation or forcing.
571 static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
576 mutex_lock(&phydev->lock);
578 if (AUTONEG_DISABLE == phydev->autoneg)
579 phy_sanitize_settings(phydev);
581 /* Invalidate LP advertising flags */
582 phydev->lp_advertising = 0;
584 err = phydev->drv->config_aneg(phydev);
588 if (phydev->state != PHY_HALTED) {
589 if (AUTONEG_ENABLE == phydev->autoneg) {
590 phydev->state = PHY_AN;
591 phydev->link_timeout = PHY_AN_TIMEOUT;
593 phydev->state = PHY_FORCING;
594 phydev->link_timeout = PHY_FORCE_TIMEOUT;
598 /* Re-schedule a PHY state machine to check PHY status because
599 * negotiation may already be done and aneg interrupt may not be
602 if (phydev->irq != PHY_POLL && phydev->state == PHY_AN) {
603 err = phy_aneg_done(phydev);
611 mutex_unlock(&phydev->lock);
614 phy_trigger_machine(phydev, sync);
620 * phy_start_aneg - start auto-negotiation for this PHY device
621 * @phydev: the phy_device struct
623 * Description: Sanitizes the settings (if we're not autonegotiating
624 * them), and then calls the driver's config_aneg function.
625 * If the PHYCONTROL Layer is operating, we change the state to
626 * reflect the beginning of Auto-negotiation or forcing.
628 int phy_start_aneg(struct phy_device *phydev)
630 return phy_start_aneg_priv(phydev, true);
632 EXPORT_SYMBOL(phy_start_aneg);
635 * phy_start_machine - start PHY state machine tracking
636 * @phydev: the phy_device struct
638 * Description: The PHY infrastructure can run a state machine
639 * which tracks whether the PHY is starting up, negotiating,
640 * etc. This function starts the timer which tracks the state
641 * of the PHY. If you want to maintain your own state machine,
642 * do not call this function.
644 void phy_start_machine(struct phy_device *phydev)
646 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
650 * phy_trigger_machine - trigger the state machine to run
652 * @phydev: the phy_device struct
653 * @sync: indicate whether we should wait for the workqueue cancelation
655 * Description: There has been a change in state which requires that the
656 * state machine runs.
659 void phy_trigger_machine(struct phy_device *phydev, bool sync)
662 cancel_delayed_work_sync(&phydev->state_queue);
664 cancel_delayed_work(&phydev->state_queue);
665 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
669 * phy_stop_machine - stop the PHY state machine tracking
670 * @phydev: target phy_device struct
672 * Description: Stops the state machine timer, sets the state to UP
673 * (unless it wasn't up yet). This function must be called BEFORE
676 void phy_stop_machine(struct phy_device *phydev)
678 cancel_delayed_work_sync(&phydev->state_queue);
680 mutex_lock(&phydev->lock);
681 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
682 phydev->state = PHY_UP;
683 mutex_unlock(&phydev->lock);
687 * phy_error - enter HALTED state for this PHY device
688 * @phydev: target phy_device struct
690 * Moves the PHY to the HALTED state in response to a read
691 * or write error, and tells the controller the link is down.
692 * Must not be called from interrupt context, or while the
693 * phydev->lock is held.
695 static void phy_error(struct phy_device *phydev)
697 mutex_lock(&phydev->lock);
698 phydev->state = PHY_HALTED;
699 mutex_unlock(&phydev->lock);
701 phy_trigger_machine(phydev, false);
705 * phy_interrupt - PHY interrupt handler
706 * @irq: interrupt line
707 * @phy_dat: phy_device pointer
709 * Description: When a PHY interrupt occurs, the handler disables
710 * interrupts, and schedules a work task to clear the interrupt.
712 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
714 struct phy_device *phydev = phy_dat;
716 if (PHY_HALTED == phydev->state)
717 return IRQ_NONE; /* It can't be ours. */
719 /* The MDIO bus is not allowed to be written in interrupt
720 * context, so we need to disable the irq here. A work
721 * queue will write the PHY to disable and clear the
722 * interrupt, and then reenable the irq line.
724 disable_irq_nosync(irq);
725 atomic_inc(&phydev->irq_disable);
727 queue_work(system_power_efficient_wq, &phydev->phy_queue);
733 * phy_enable_interrupts - Enable the interrupts from the PHY side
734 * @phydev: target phy_device struct
736 static int phy_enable_interrupts(struct phy_device *phydev)
738 int err = phy_clear_interrupt(phydev);
743 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
747 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
748 * @phydev: target phy_device struct
750 static int phy_disable_interrupts(struct phy_device *phydev)
754 /* Disable PHY interrupts */
755 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
759 /* Clear the interrupt */
760 err = phy_clear_interrupt(phydev);
773 * phy_start_interrupts - request and enable interrupts for a PHY device
774 * @phydev: target phy_device struct
776 * Description: Request the interrupt for the given PHY.
777 * If this fails, then we set irq to PHY_POLL.
778 * Otherwise, we enable the interrupts in the PHY.
779 * This should only be called with a valid IRQ number.
780 * Returns 0 on success or < 0 on error.
782 int phy_start_interrupts(struct phy_device *phydev)
784 atomic_set(&phydev->irq_disable, 0);
785 if (request_irq(phydev->irq, phy_interrupt,
789 pr_warn("%s: Can't get IRQ %d (PHY)\n",
790 phydev->mdio.bus->name, phydev->irq);
791 phydev->irq = PHY_POLL;
795 return phy_enable_interrupts(phydev);
797 EXPORT_SYMBOL(phy_start_interrupts);
800 * phy_stop_interrupts - disable interrupts from a PHY device
801 * @phydev: target phy_device struct
803 int phy_stop_interrupts(struct phy_device *phydev)
805 int err = phy_disable_interrupts(phydev);
810 free_irq(phydev->irq, phydev);
812 /* Cannot call flush_scheduled_work() here as desired because
813 * of rtnl_lock(), but we do not really care about what would
814 * be done, except from enable_irq(), so cancel any work
815 * possibly pending and take care of the matter below.
817 cancel_work_sync(&phydev->phy_queue);
818 /* If work indeed has been cancelled, disable_irq() will have
819 * been left unbalanced from phy_interrupt() and enable_irq()
820 * has to be called so that other devices on the line work.
822 while (atomic_dec_return(&phydev->irq_disable) >= 0)
823 enable_irq(phydev->irq);
827 EXPORT_SYMBOL(phy_stop_interrupts);
830 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
831 * @work: work_struct that describes the work to be done
833 void phy_change(struct work_struct *work)
835 struct phy_device *phydev =
836 container_of(work, struct phy_device, phy_queue);
838 if (phy_interrupt_is_valid(phydev)) {
839 if (phydev->drv->did_interrupt &&
840 !phydev->drv->did_interrupt(phydev))
843 if (phy_disable_interrupts(phydev))
847 mutex_lock(&phydev->lock);
848 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
849 phydev->state = PHY_CHANGELINK;
850 mutex_unlock(&phydev->lock);
852 if (phy_interrupt_is_valid(phydev)) {
853 atomic_dec(&phydev->irq_disable);
854 enable_irq(phydev->irq);
856 /* Reenable interrupts */
857 if (PHY_HALTED != phydev->state &&
858 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
862 /* reschedule state queue work to run as soon as possible */
863 phy_trigger_machine(phydev, true);
867 atomic_dec(&phydev->irq_disable);
868 enable_irq(phydev->irq);
872 disable_irq(phydev->irq);
873 atomic_inc(&phydev->irq_disable);
879 * phy_stop - Bring down the PHY link, and stop checking the status
880 * @phydev: target phy_device struct
882 void phy_stop(struct phy_device *phydev)
884 mutex_lock(&phydev->lock);
886 if (PHY_HALTED == phydev->state)
889 if (phy_interrupt_is_valid(phydev)) {
890 /* Disable PHY Interrupts */
891 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
893 /* Clear any pending interrupts */
894 phy_clear_interrupt(phydev);
897 phydev->state = PHY_HALTED;
900 mutex_unlock(&phydev->lock);
902 /* Cannot call flush_scheduled_work() here as desired because
903 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
904 * will not reenable interrupts.
907 EXPORT_SYMBOL(phy_stop);
910 * phy_start - start or restart a PHY device
911 * @phydev: target phy_device struct
913 * Description: Indicates the attached device's readiness to
914 * handle PHY-related work. Used during startup to start the
915 * PHY, and after a call to phy_stop() to resume operation.
916 * Also used to indicate the MDIO bus has cleared an error
919 void phy_start(struct phy_device *phydev)
921 bool do_resume = false;
924 mutex_lock(&phydev->lock);
926 switch (phydev->state) {
928 phydev->state = PHY_PENDING;
931 phydev->state = PHY_UP;
934 /* make sure interrupts are re-enabled for the PHY */
935 if (phy_interrupt_is_valid(phydev)) {
936 err = phy_enable_interrupts(phydev);
941 phydev->state = PHY_RESUMING;
947 mutex_unlock(&phydev->lock);
949 /* if phy was suspended, bring the physical link up again */
953 phy_trigger_machine(phydev, true);
955 EXPORT_SYMBOL(phy_start);
958 * phy_state_machine - Handle the state machine
959 * @work: work_struct that describes the work to be done
961 void phy_state_machine(struct work_struct *work)
963 struct delayed_work *dwork = to_delayed_work(work);
964 struct phy_device *phydev =
965 container_of(dwork, struct phy_device, state_queue);
966 bool needs_aneg = false, do_suspend = false;
967 enum phy_state old_state;
971 mutex_lock(&phydev->lock);
973 old_state = phydev->state;
975 if (phydev->drv->link_change_notify)
976 phydev->drv->link_change_notify(phydev);
978 switch (phydev->state) {
987 phydev->link_timeout = PHY_AN_TIMEOUT;
991 err = phy_read_status(phydev);
995 /* If the link is down, give up on negotiation for now */
997 phydev->state = PHY_NOLINK;
998 netif_carrier_off(phydev->attached_dev);
999 phydev->adjust_link(phydev->attached_dev);
1003 /* Check if negotiation is done. Break if there's an error */
1004 err = phy_aneg_done(phydev);
1008 /* If AN is done, we're running */
1010 phydev->state = PHY_RUNNING;
1011 netif_carrier_on(phydev->attached_dev);
1012 phydev->adjust_link(phydev->attached_dev);
1014 } else if (0 == phydev->link_timeout--)
1018 if (phy_interrupt_is_valid(phydev))
1021 err = phy_read_status(phydev);
1026 if (AUTONEG_ENABLE == phydev->autoneg) {
1027 err = phy_aneg_done(phydev);
1032 phydev->state = PHY_AN;
1033 phydev->link_timeout = PHY_AN_TIMEOUT;
1037 phydev->state = PHY_RUNNING;
1038 netif_carrier_on(phydev->attached_dev);
1039 phydev->adjust_link(phydev->attached_dev);
1043 err = genphy_update_link(phydev);
1048 phydev->state = PHY_RUNNING;
1049 netif_carrier_on(phydev->attached_dev);
1051 if (0 == phydev->link_timeout--)
1055 phydev->adjust_link(phydev->attached_dev);
1058 /* Only register a CHANGE if we are polling and link changed
1059 * since latest checking.
1061 if (phydev->irq == PHY_POLL) {
1062 old_link = phydev->link;
1063 err = phy_read_status(phydev);
1067 if (old_link != phydev->link)
1068 phydev->state = PHY_CHANGELINK;
1071 * Failsafe: check that nobody set phydev->link=0 between two
1072 * poll cycles, otherwise we won't leave RUNNING state as long
1073 * as link remains down.
1075 if (!phydev->link && phydev->state == PHY_RUNNING) {
1076 phydev->state = PHY_CHANGELINK;
1077 phydev_err(phydev, "no link in PHY_RUNNING\n");
1080 case PHY_CHANGELINK:
1081 err = phy_read_status(phydev);
1086 phydev->state = PHY_RUNNING;
1087 netif_carrier_on(phydev->attached_dev);
1089 phydev->state = PHY_NOLINK;
1090 netif_carrier_off(phydev->attached_dev);
1093 phydev->adjust_link(phydev->attached_dev);
1095 if (phy_interrupt_is_valid(phydev))
1096 err = phy_config_interrupt(phydev,
1097 PHY_INTERRUPT_ENABLED);
1102 netif_carrier_off(phydev->attached_dev);
1103 phydev->adjust_link(phydev->attached_dev);
1108 if (AUTONEG_ENABLE == phydev->autoneg) {
1109 err = phy_aneg_done(phydev);
1113 /* err > 0 if AN is done.
1114 * Otherwise, it's 0, and we're still waiting for AN
1117 err = phy_read_status(phydev);
1122 phydev->state = PHY_RUNNING;
1123 netif_carrier_on(phydev->attached_dev);
1125 phydev->state = PHY_NOLINK;
1127 phydev->adjust_link(phydev->attached_dev);
1129 phydev->state = PHY_AN;
1130 phydev->link_timeout = PHY_AN_TIMEOUT;
1133 err = phy_read_status(phydev);
1138 phydev->state = PHY_RUNNING;
1139 netif_carrier_on(phydev->attached_dev);
1141 phydev->state = PHY_NOLINK;
1143 phydev->adjust_link(phydev->attached_dev);
1148 mutex_unlock(&phydev->lock);
1151 err = phy_start_aneg_priv(phydev, false);
1152 else if (do_suspend)
1153 phy_suspend(phydev);
1158 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1159 phy_state_to_str(old_state),
1160 phy_state_to_str(phydev->state));
1162 /* Only re-schedule a PHY state machine change if we are polling the
1163 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1164 * between states from phy_mac_interrupt()
1166 if (phydev->irq == PHY_POLL)
1167 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1168 PHY_STATE_TIME * HZ);
1171 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1173 phydev->link = new_link;
1175 /* Trigger a state machine change */
1176 queue_work(system_power_efficient_wq, &phydev->phy_queue);
1178 EXPORT_SYMBOL(phy_mac_interrupt);
1180 static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
1183 /* Write the desired MMD Devad */
1184 bus->write(bus, addr, MII_MMD_CTRL, devad);
1186 /* Write the desired MMD register address */
1187 bus->write(bus, addr, MII_MMD_DATA, prtad);
1189 /* Select the Function : DATA with no post increment */
1190 bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
1194 * phy_read_mmd_indirect - reads data from the MMD registers
1195 * @phydev: The PHY device bus
1196 * @prtad: MMD Address
1199 * Description: it reads data from the MMD registers (clause 22 to access to
1200 * clause 45) of the specified phy address.
1201 * To read these register we have:
1202 * 1) Write reg 13 // DEVAD
1203 * 2) Write reg 14 // MMD Address
1204 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1205 * 3) Read reg 14 // Read MMD data
1207 int phy_read_mmd_indirect(struct phy_device *phydev, int prtad, int devad)
1209 struct phy_driver *phydrv = phydev->drv;
1210 int addr = phydev->mdio.addr;
1213 if (!phydrv->read_mmd_indirect) {
1214 struct mii_bus *bus = phydev->mdio.bus;
1216 mutex_lock(&bus->mdio_lock);
1217 mmd_phy_indirect(bus, prtad, devad, addr);
1219 /* Read the content of the MMD's selected register */
1220 value = bus->read(bus, addr, MII_MMD_DATA);
1221 mutex_unlock(&bus->mdio_lock);
1223 value = phydrv->read_mmd_indirect(phydev, prtad, devad, addr);
1227 EXPORT_SYMBOL(phy_read_mmd_indirect);
1230 * phy_write_mmd_indirect - writes data to the MMD registers
1231 * @phydev: The PHY device
1232 * @prtad: MMD Address
1234 * @data: data to write in the MMD register
1236 * Description: Write data from the MMD registers of the specified
1238 * To write these register we have:
1239 * 1) Write reg 13 // DEVAD
1240 * 2) Write reg 14 // MMD Address
1241 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1242 * 3) Write reg 14 // Write MMD data
1244 void phy_write_mmd_indirect(struct phy_device *phydev, int prtad,
1245 int devad, u32 data)
1247 struct phy_driver *phydrv = phydev->drv;
1248 int addr = phydev->mdio.addr;
1250 if (!phydrv->write_mmd_indirect) {
1251 struct mii_bus *bus = phydev->mdio.bus;
1253 mutex_lock(&bus->mdio_lock);
1254 mmd_phy_indirect(bus, prtad, devad, addr);
1256 /* Write the data into MMD's selected register */
1257 bus->write(bus, addr, MII_MMD_DATA, data);
1258 mutex_unlock(&bus->mdio_lock);
1260 phydrv->write_mmd_indirect(phydev, prtad, devad, addr, data);
1263 EXPORT_SYMBOL(phy_write_mmd_indirect);
1266 * phy_init_eee - init and check the EEE feature
1267 * @phydev: target phy_device struct
1268 * @clk_stop_enable: PHY may stop the clock during LPI
1270 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1271 * is supported by looking at the MMD registers 3.20 and 7.60/61
1272 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1275 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1277 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1278 * Also EEE feature is active when core is operating with MII, GMII
1279 * or RGMII (all kinds). Internal PHYs are also allowed to proceed and
1280 * should return an error if they do not support EEE.
1282 if ((phydev->duplex == DUPLEX_FULL) &&
1283 ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
1284 (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
1285 phy_interface_is_rgmii(phydev) ||
1286 phy_is_internal(phydev))) {
1287 int eee_lp, eee_cap, eee_adv;
1291 /* Read phy status to properly get the right settings */
1292 status = phy_read_status(phydev);
1296 /* First check if the EEE ability is supported */
1297 eee_cap = phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_ABLE,
1302 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1306 /* Check which link settings negotiated and verify it in
1307 * the EEE advertising registers.
1309 eee_lp = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_LPABLE,
1314 eee_adv = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_ADV,
1319 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1320 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1321 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
1324 if (clk_stop_enable) {
1325 /* Configure the PHY to stop receiving xMII
1326 * clock while it is signaling LPI.
1328 int val = phy_read_mmd_indirect(phydev, MDIO_CTRL1,
1333 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1334 phy_write_mmd_indirect(phydev, MDIO_CTRL1,
1338 return 0; /* EEE supported */
1341 return -EPROTONOSUPPORT;
1343 EXPORT_SYMBOL(phy_init_eee);
1346 * phy_get_eee_err - report the EEE wake error count
1347 * @phydev: target phy_device struct
1349 * Description: it is to report the number of time where the PHY
1350 * failed to complete its normal wake sequence.
1352 int phy_get_eee_err(struct phy_device *phydev)
1354 return phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_WK_ERR, MDIO_MMD_PCS);
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)
1370 /* Get Supported EEE */
1371 val = phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_ABLE, MDIO_MMD_PCS);
1374 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1376 /* Get advertisement EEE */
1377 val = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_ADV, MDIO_MMD_AN);
1380 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1382 /* Get LP advertisement EEE */
1383 val = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_LPABLE, MDIO_MMD_AN);
1386 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1390 EXPORT_SYMBOL(phy_ethtool_get_eee);
1393 * phy_ethtool_set_eee - set EEE supported and status
1394 * @phydev: target phy_device struct
1395 * @data: ethtool_eee data
1397 * Description: it is to program the Advertisement EEE register.
1399 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1401 int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
1403 /* Mask prohibited EEE modes */
1404 val &= ~phydev->eee_broken_modes;
1406 phy_write_mmd_indirect(phydev, MDIO_AN_EEE_ADV, MDIO_MMD_AN, val);
1410 EXPORT_SYMBOL(phy_ethtool_set_eee);
1412 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1414 if (phydev->drv->set_wol)
1415 return phydev->drv->set_wol(phydev, wol);
1419 EXPORT_SYMBOL(phy_ethtool_set_wol);
1421 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1423 if (phydev->drv->get_wol)
1424 phydev->drv->get_wol(phydev, wol);
1426 EXPORT_SYMBOL(phy_ethtool_get_wol);
1428 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1429 struct ethtool_link_ksettings *cmd)
1431 struct phy_device *phydev = ndev->phydev;
1436 return phy_ethtool_ksettings_get(phydev, cmd);
1438 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1440 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1441 const struct ethtool_link_ksettings *cmd)
1443 struct phy_device *phydev = ndev->phydev;
1448 return phy_ethtool_ksettings_set(phydev, cmd);
1450 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);