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->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_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
373 cmd->supported = phydev->supported;
375 cmd->advertising = phydev->advertising;
376 cmd->lp_advertising = phydev->lp_advertising;
378 ethtool_cmd_speed_set(cmd, phydev->speed);
379 cmd->duplex = phydev->duplex;
380 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
381 cmd->port = PORT_BNC;
383 cmd->port = PORT_MII;
384 cmd->phy_address = phydev->addr;
385 cmd->transceiver = phy_is_internal(phydev) ?
386 XCVR_INTERNAL : XCVR_EXTERNAL;
387 cmd->autoneg = phydev->autoneg;
388 cmd->eth_tp_mdix_ctrl = phydev->mdix;
392 EXPORT_SYMBOL(phy_ethtool_gset);
395 * phy_mii_ioctl - generic PHY MII ioctl interface
396 * @phydev: the phy_device struct
397 * @ifr: &struct ifreq for socket ioctl's
398 * @cmd: ioctl cmd to execute
400 * Note that this function is currently incompatible with the
401 * PHYCONTROL layer. It changes registers without regard to
402 * current state. Use at own risk.
404 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
406 struct mii_ioctl_data *mii_data = if_mii(ifr);
407 u16 val = mii_data->val_in;
408 bool change_autoneg = false;
412 mii_data->phy_id = phydev->addr;
416 mii_data->val_out = mdiobus_read(phydev->bus, mii_data->phy_id,
421 if (mii_data->phy_id == phydev->addr) {
422 switch (mii_data->reg_num) {
424 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
425 if (phydev->autoneg == AUTONEG_ENABLE)
426 change_autoneg = true;
427 phydev->autoneg = AUTONEG_DISABLE;
428 if (val & BMCR_FULLDPLX)
429 phydev->duplex = DUPLEX_FULL;
431 phydev->duplex = DUPLEX_HALF;
432 if (val & BMCR_SPEED1000)
433 phydev->speed = SPEED_1000;
434 else if (val & BMCR_SPEED100)
435 phydev->speed = SPEED_100;
436 else phydev->speed = SPEED_10;
439 if (phydev->autoneg == AUTONEG_DISABLE)
440 change_autoneg = true;
441 phydev->autoneg = AUTONEG_ENABLE;
445 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
446 change_autoneg = true;
454 mdiobus_write(phydev->bus, mii_data->phy_id,
455 mii_data->reg_num, val);
457 if (mii_data->phy_id == phydev->addr &&
458 mii_data->reg_num == MII_BMCR &&
460 return phy_init_hw(phydev);
463 return phy_start_aneg(phydev);
468 if (phydev->drv->hwtstamp)
469 return phydev->drv->hwtstamp(phydev, ifr);
476 EXPORT_SYMBOL(phy_mii_ioctl);
479 * phy_start_aneg - start auto-negotiation for this PHY device
480 * @phydev: the phy_device struct
482 * Description: Sanitizes the settings (if we're not autonegotiating
483 * them), and then calls the driver's config_aneg function.
484 * If the PHYCONTROL Layer is operating, we change the state to
485 * reflect the beginning of Auto-negotiation or forcing.
487 int phy_start_aneg(struct phy_device *phydev)
491 mutex_lock(&phydev->lock);
493 if (AUTONEG_DISABLE == phydev->autoneg)
494 phy_sanitize_settings(phydev);
496 /* Invalidate LP advertising flags */
497 phydev->lp_advertising = 0;
499 err = phydev->drv->config_aneg(phydev);
503 if (phydev->state != PHY_HALTED) {
504 if (AUTONEG_ENABLE == phydev->autoneg) {
505 phydev->state = PHY_AN;
506 phydev->link_timeout = PHY_AN_TIMEOUT;
508 phydev->state = PHY_FORCING;
509 phydev->link_timeout = PHY_FORCE_TIMEOUT;
514 mutex_unlock(&phydev->lock);
517 EXPORT_SYMBOL(phy_start_aneg);
520 * phy_start_machine - start PHY state machine tracking
521 * @phydev: the phy_device struct
523 * Description: The PHY infrastructure can run a state machine
524 * which tracks whether the PHY is starting up, negotiating,
525 * etc. This function starts the timer which tracks the state
526 * of the PHY. If you want to maintain your own state machine,
527 * do not call this function.
529 void phy_start_machine(struct phy_device *phydev)
531 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
535 * phy_stop_machine - stop the PHY state machine tracking
536 * @phydev: target phy_device struct
538 * Description: Stops the state machine timer, sets the state to UP
539 * (unless it wasn't up yet). This function must be called BEFORE
542 void phy_stop_machine(struct phy_device *phydev)
544 cancel_delayed_work_sync(&phydev->state_queue);
546 mutex_lock(&phydev->lock);
547 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
548 phydev->state = PHY_UP;
549 mutex_unlock(&phydev->lock);
553 * phy_error - enter HALTED state for this PHY device
554 * @phydev: target phy_device struct
556 * Moves the PHY to the HALTED state in response to a read
557 * or write error, and tells the controller the link is down.
558 * Must not be called from interrupt context, or while the
559 * phydev->lock is held.
561 static void phy_error(struct phy_device *phydev)
563 mutex_lock(&phydev->lock);
564 phydev->state = PHY_HALTED;
565 mutex_unlock(&phydev->lock);
569 * phy_interrupt - PHY interrupt handler
570 * @irq: interrupt line
571 * @phy_dat: phy_device pointer
573 * Description: When a PHY interrupt occurs, the handler disables
574 * interrupts, and schedules a work task to clear the interrupt.
576 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
578 struct phy_device *phydev = phy_dat;
580 if (PHY_HALTED == phydev->state)
581 return IRQ_NONE; /* It can't be ours. */
583 /* The MDIO bus is not allowed to be written in interrupt
584 * context, so we need to disable the irq here. A work
585 * queue will write the PHY to disable and clear the
586 * interrupt, and then reenable the irq line.
588 disable_irq_nosync(irq);
589 atomic_inc(&phydev->irq_disable);
591 queue_work(system_power_efficient_wq, &phydev->phy_queue);
597 * phy_enable_interrupts - Enable the interrupts from the PHY side
598 * @phydev: target phy_device struct
600 static int phy_enable_interrupts(struct phy_device *phydev)
602 int err = phy_clear_interrupt(phydev);
607 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
611 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
612 * @phydev: target phy_device struct
614 static int phy_disable_interrupts(struct phy_device *phydev)
618 /* Disable PHY interrupts */
619 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
623 /* Clear the interrupt */
624 err = phy_clear_interrupt(phydev);
637 * phy_start_interrupts - request and enable interrupts for a PHY device
638 * @phydev: target phy_device struct
640 * Description: Request the interrupt for the given PHY.
641 * If this fails, then we set irq to PHY_POLL.
642 * Otherwise, we enable the interrupts in the PHY.
643 * This should only be called with a valid IRQ number.
644 * Returns 0 on success or < 0 on error.
646 int phy_start_interrupts(struct phy_device *phydev)
648 atomic_set(&phydev->irq_disable, 0);
649 if (request_irq(phydev->irq, phy_interrupt,
653 pr_warn("%s: Can't get IRQ %d (PHY)\n",
654 phydev->bus->name, phydev->irq);
655 phydev->irq = PHY_POLL;
659 return phy_enable_interrupts(phydev);
661 EXPORT_SYMBOL(phy_start_interrupts);
664 * phy_stop_interrupts - disable interrupts from a PHY device
665 * @phydev: target phy_device struct
667 int phy_stop_interrupts(struct phy_device *phydev)
669 int err = phy_disable_interrupts(phydev);
674 free_irq(phydev->irq, phydev);
676 /* Cannot call flush_scheduled_work() here as desired because
677 * of rtnl_lock(), but we do not really care about what would
678 * be done, except from enable_irq(), so cancel any work
679 * possibly pending and take care of the matter below.
681 cancel_work_sync(&phydev->phy_queue);
682 /* If work indeed has been cancelled, disable_irq() will have
683 * been left unbalanced from phy_interrupt() and enable_irq()
684 * has to be called so that other devices on the line work.
686 while (atomic_dec_return(&phydev->irq_disable) >= 0)
687 enable_irq(phydev->irq);
691 EXPORT_SYMBOL(phy_stop_interrupts);
694 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
695 * @work: work_struct that describes the work to be done
697 void phy_change(struct work_struct *work)
699 struct phy_device *phydev =
700 container_of(work, struct phy_device, phy_queue);
702 if (phy_interrupt_is_valid(phydev)) {
703 if (phydev->drv->did_interrupt &&
704 !phydev->drv->did_interrupt(phydev))
707 if (phy_disable_interrupts(phydev))
711 mutex_lock(&phydev->lock);
712 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
713 phydev->state = PHY_CHANGELINK;
714 mutex_unlock(&phydev->lock);
716 if (phy_interrupt_is_valid(phydev)) {
717 atomic_dec(&phydev->irq_disable);
718 enable_irq(phydev->irq);
720 /* Reenable interrupts */
721 if (PHY_HALTED != phydev->state &&
722 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
726 /* reschedule state queue work to run as soon as possible */
727 cancel_delayed_work_sync(&phydev->state_queue);
728 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
732 atomic_dec(&phydev->irq_disable);
733 enable_irq(phydev->irq);
737 disable_irq(phydev->irq);
738 atomic_inc(&phydev->irq_disable);
744 * phy_stop - Bring down the PHY link, and stop checking the status
745 * @phydev: target phy_device struct
747 void phy_stop(struct phy_device *phydev)
749 mutex_lock(&phydev->lock);
751 if (PHY_HALTED == phydev->state)
754 if (phy_interrupt_is_valid(phydev)) {
755 /* Disable PHY Interrupts */
756 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
758 /* Clear any pending interrupts */
759 phy_clear_interrupt(phydev);
762 phydev->state = PHY_HALTED;
765 mutex_unlock(&phydev->lock);
767 /* Cannot call flush_scheduled_work() here as desired because
768 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
769 * will not reenable interrupts.
772 EXPORT_SYMBOL(phy_stop);
775 * phy_start - start or restart a PHY device
776 * @phydev: target phy_device struct
778 * Description: Indicates the attached device's readiness to
779 * handle PHY-related work. Used during startup to start the
780 * PHY, and after a call to phy_stop() to resume operation.
781 * Also used to indicate the MDIO bus has cleared an error
784 void phy_start(struct phy_device *phydev)
786 bool do_resume = false;
789 mutex_lock(&phydev->lock);
791 switch (phydev->state) {
793 phydev->state = PHY_PENDING;
796 phydev->state = PHY_UP;
799 /* make sure interrupts are re-enabled for the PHY */
800 err = phy_enable_interrupts(phydev);
804 phydev->state = PHY_RESUMING;
810 mutex_unlock(&phydev->lock);
812 /* if phy was suspended, bring the physical link up again */
816 EXPORT_SYMBOL(phy_start);
819 * phy_state_machine - Handle the state machine
820 * @work: work_struct that describes the work to be done
822 void phy_state_machine(struct work_struct *work)
824 struct delayed_work *dwork = to_delayed_work(work);
825 struct phy_device *phydev =
826 container_of(dwork, struct phy_device, state_queue);
827 bool needs_aneg = false, do_suspend = false;
828 enum phy_state old_state;
832 mutex_lock(&phydev->lock);
834 old_state = phydev->state;
836 if (phydev->drv->link_change_notify)
837 phydev->drv->link_change_notify(phydev);
839 switch (phydev->state) {
848 phydev->link_timeout = PHY_AN_TIMEOUT;
852 err = phy_read_status(phydev);
856 /* If the link is down, give up on negotiation for now */
858 phydev->state = PHY_NOLINK;
859 netif_carrier_off(phydev->attached_dev);
860 phydev->adjust_link(phydev->attached_dev);
864 /* Check if negotiation is done. Break if there's an error */
865 err = phy_aneg_done(phydev);
869 /* If AN is done, we're running */
871 phydev->state = PHY_RUNNING;
872 netif_carrier_on(phydev->attached_dev);
873 phydev->adjust_link(phydev->attached_dev);
875 } else if (0 == phydev->link_timeout--)
879 if (phy_interrupt_is_valid(phydev))
882 err = phy_read_status(phydev);
887 if (AUTONEG_ENABLE == phydev->autoneg) {
888 err = phy_aneg_done(phydev);
893 phydev->state = PHY_AN;
894 phydev->link_timeout = PHY_AN_TIMEOUT;
898 phydev->state = PHY_RUNNING;
899 netif_carrier_on(phydev->attached_dev);
900 phydev->adjust_link(phydev->attached_dev);
904 err = genphy_update_link(phydev);
909 phydev->state = PHY_RUNNING;
910 netif_carrier_on(phydev->attached_dev);
912 if (0 == phydev->link_timeout--)
916 phydev->adjust_link(phydev->attached_dev);
919 /* Only register a CHANGE if we are polling or ignoring
920 * interrupts and link changed since latest checking.
922 if (!phy_interrupt_is_valid(phydev)) {
923 old_link = phydev->link;
924 err = phy_read_status(phydev);
928 if (old_link != phydev->link)
929 phydev->state = PHY_CHANGELINK;
932 * Failsafe: check that nobody set phydev->link=0 between two
933 * poll cycles, otherwise we won't leave RUNNING state as long
934 * as link remains down.
936 if (!phydev->link && phydev->state == PHY_RUNNING) {
937 phydev->state = PHY_CHANGELINK;
938 dev_err(&phydev->dev, "no link in PHY_RUNNING\n");
942 err = phy_read_status(phydev);
947 phydev->state = PHY_RUNNING;
948 netif_carrier_on(phydev->attached_dev);
950 phydev->state = PHY_NOLINK;
951 netif_carrier_off(phydev->attached_dev);
954 phydev->adjust_link(phydev->attached_dev);
956 if (phy_interrupt_is_valid(phydev))
957 err = phy_config_interrupt(phydev,
958 PHY_INTERRUPT_ENABLED);
963 netif_carrier_off(phydev->attached_dev);
964 phydev->adjust_link(phydev->attached_dev);
969 if (AUTONEG_ENABLE == phydev->autoneg) {
970 err = phy_aneg_done(phydev);
974 /* err > 0 if AN is done.
975 * Otherwise, it's 0, and we're still waiting for AN
978 err = phy_read_status(phydev);
983 phydev->state = PHY_RUNNING;
984 netif_carrier_on(phydev->attached_dev);
986 phydev->state = PHY_NOLINK;
988 phydev->adjust_link(phydev->attached_dev);
990 phydev->state = PHY_AN;
991 phydev->link_timeout = PHY_AN_TIMEOUT;
994 err = phy_read_status(phydev);
999 phydev->state = PHY_RUNNING;
1000 netif_carrier_on(phydev->attached_dev);
1002 phydev->state = PHY_NOLINK;
1004 phydev->adjust_link(phydev->attached_dev);
1009 mutex_unlock(&phydev->lock);
1012 err = phy_start_aneg(phydev);
1013 else if (do_suspend)
1014 phy_suspend(phydev);
1019 dev_dbg(&phydev->dev, "PHY state change %s -> %s\n",
1020 phy_state_to_str(old_state), phy_state_to_str(phydev->state));
1022 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1023 PHY_STATE_TIME * HZ);
1026 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1028 phydev->link = new_link;
1030 /* Trigger a state machine change */
1031 queue_work(system_power_efficient_wq, &phydev->phy_queue);
1033 EXPORT_SYMBOL(phy_mac_interrupt);
1035 static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
1038 /* Write the desired MMD Devad */
1039 bus->write(bus, addr, MII_MMD_CTRL, devad);
1041 /* Write the desired MMD register address */
1042 bus->write(bus, addr, MII_MMD_DATA, prtad);
1044 /* Select the Function : DATA with no post increment */
1045 bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
1049 * phy_read_mmd_indirect - reads data from the MMD registers
1050 * @phydev: The PHY device bus
1051 * @prtad: MMD Address
1053 * @addr: PHY address on the MII bus
1055 * Description: it reads data from the MMD registers (clause 22 to access to
1056 * clause 45) of the specified phy address.
1057 * To read these register we have:
1058 * 1) Write reg 13 // DEVAD
1059 * 2) Write reg 14 // MMD Address
1060 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1061 * 3) Read reg 14 // Read MMD data
1063 int phy_read_mmd_indirect(struct phy_device *phydev, int prtad,
1064 int devad, int addr)
1066 struct phy_driver *phydrv = phydev->drv;
1069 if (!phydrv->read_mmd_indirect) {
1070 struct mii_bus *bus = phydev->bus;
1072 mutex_lock(&bus->mdio_lock);
1073 mmd_phy_indirect(bus, prtad, devad, addr);
1075 /* Read the content of the MMD's selected register */
1076 value = bus->read(bus, addr, MII_MMD_DATA);
1077 mutex_unlock(&bus->mdio_lock);
1079 value = phydrv->read_mmd_indirect(phydev, prtad, devad, addr);
1083 EXPORT_SYMBOL(phy_read_mmd_indirect);
1086 * phy_write_mmd_indirect - writes data to the MMD registers
1087 * @phydev: The PHY device
1088 * @prtad: MMD Address
1090 * @addr: PHY address on the MII bus
1091 * @data: data to write in the MMD register
1093 * Description: Write data from the MMD registers of the specified
1095 * To write these register we have:
1096 * 1) Write reg 13 // DEVAD
1097 * 2) Write reg 14 // MMD Address
1098 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1099 * 3) Write reg 14 // Write MMD data
1101 void phy_write_mmd_indirect(struct phy_device *phydev, int prtad,
1102 int devad, int addr, u32 data)
1104 struct phy_driver *phydrv = phydev->drv;
1106 if (!phydrv->write_mmd_indirect) {
1107 struct mii_bus *bus = phydev->bus;
1109 mutex_lock(&bus->mdio_lock);
1110 mmd_phy_indirect(bus, prtad, devad, addr);
1112 /* Write the data into MMD's selected register */
1113 bus->write(bus, addr, MII_MMD_DATA, data);
1114 mutex_unlock(&bus->mdio_lock);
1116 phydrv->write_mmd_indirect(phydev, prtad, devad, addr, data);
1119 EXPORT_SYMBOL(phy_write_mmd_indirect);
1122 * phy_init_eee - init and check the EEE feature
1123 * @phydev: target phy_device struct
1124 * @clk_stop_enable: PHY may stop the clock during LPI
1126 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1127 * is supported by looking at the MMD registers 3.20 and 7.60/61
1128 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1131 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1133 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1134 * Also EEE feature is active when core is operating with MII, GMII
1135 * or RGMII (all kinds). Internal PHYs are also allowed to proceed and
1136 * should return an error if they do not support EEE.
1138 if ((phydev->duplex == DUPLEX_FULL) &&
1139 ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
1140 (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
1141 phy_interface_is_rgmii(phydev) ||
1142 phy_is_internal(phydev))) {
1143 int eee_lp, eee_cap, eee_adv;
1147 /* Read phy status to properly get the right settings */
1148 status = phy_read_status(phydev);
1152 /* First check if the EEE ability is supported */
1153 eee_cap = phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_ABLE,
1154 MDIO_MMD_PCS, phydev->addr);
1158 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1162 /* Check which link settings negotiated and verify it in
1163 * the EEE advertising registers.
1165 eee_lp = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_LPABLE,
1166 MDIO_MMD_AN, phydev->addr);
1170 eee_adv = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_ADV,
1171 MDIO_MMD_AN, phydev->addr);
1175 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1176 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1177 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
1180 if (clk_stop_enable) {
1181 /* Configure the PHY to stop receiving xMII
1182 * clock while it is signaling LPI.
1184 int val = phy_read_mmd_indirect(phydev, MDIO_CTRL1,
1190 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1191 phy_write_mmd_indirect(phydev, MDIO_CTRL1,
1192 MDIO_MMD_PCS, phydev->addr,
1196 return 0; /* EEE supported */
1199 return -EPROTONOSUPPORT;
1201 EXPORT_SYMBOL(phy_init_eee);
1204 * phy_get_eee_err - report the EEE wake error count
1205 * @phydev: target phy_device struct
1207 * Description: it is to report the number of time where the PHY
1208 * failed to complete its normal wake sequence.
1210 int phy_get_eee_err(struct phy_device *phydev)
1212 return phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_WK_ERR,
1213 MDIO_MMD_PCS, phydev->addr);
1215 EXPORT_SYMBOL(phy_get_eee_err);
1218 * phy_ethtool_get_eee - get EEE supported and status
1219 * @phydev: target phy_device struct
1220 * @data: ethtool_eee data
1222 * Description: it reportes the Supported/Advertisement/LP Advertisement
1225 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1229 /* Get Supported EEE */
1230 val = phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_ABLE,
1231 MDIO_MMD_PCS, phydev->addr);
1234 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1236 /* Get advertisement EEE */
1237 val = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_ADV,
1238 MDIO_MMD_AN, phydev->addr);
1241 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1243 /* Get LP advertisement EEE */
1244 val = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_LPABLE,
1245 MDIO_MMD_AN, phydev->addr);
1248 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1252 EXPORT_SYMBOL(phy_ethtool_get_eee);
1255 * phy_ethtool_set_eee - set EEE supported and status
1256 * @phydev: target phy_device struct
1257 * @data: ethtool_eee data
1259 * Description: it is to program the Advertisement EEE register.
1261 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1263 int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
1265 phy_write_mmd_indirect(phydev, MDIO_AN_EEE_ADV, MDIO_MMD_AN,
1270 EXPORT_SYMBOL(phy_ethtool_set_eee);
1272 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1274 if (phydev->drv->set_wol)
1275 return phydev->drv->set_wol(phydev, wol);
1279 EXPORT_SYMBOL(phy_ethtool_set_wol);
1281 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1283 if (phydev->drv->get_wol)
1284 phydev->drv->get_wol(phydev, wol);
1286 EXPORT_SYMBOL(phy_ethtool_get_wol);