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>
42 * phy_print_status - Convenience function to print out the current phy status
43 * @phydev: the phy_device struct
45 void phy_print_status(struct phy_device *phydev)
48 pr_info("%s - Link is Up - %d/%s\n",
49 dev_name(&phydev->dev),
51 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
53 pr_info("%s - Link is Down\n", dev_name(&phydev->dev));
56 EXPORT_SYMBOL(phy_print_status);
59 * phy_clear_interrupt - Ack the phy device's interrupt
60 * @phydev: the phy_device struct
62 * If the @phydev driver has an ack_interrupt function, call it to
63 * ack and clear the phy device's interrupt.
65 * Returns 0 on success on < 0 on error.
67 static int phy_clear_interrupt(struct phy_device *phydev)
69 if (phydev->drv->ack_interrupt)
70 return phydev->drv->ack_interrupt(phydev);
76 * phy_config_interrupt - configure the PHY device for the requested interrupts
77 * @phydev: the phy_device struct
78 * @interrupts: interrupt flags to configure for this @phydev
80 * Returns 0 on success on < 0 on error.
82 static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
84 phydev->interrupts = interrupts;
85 if (phydev->drv->config_intr)
86 return phydev->drv->config_intr(phydev);
93 * phy_aneg_done - return auto-negotiation status
94 * @phydev: target phy_device struct
96 * Description: Reads the status register and returns 0 either if
97 * auto-negotiation is incomplete, or if there was an error.
98 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
100 static inline int phy_aneg_done(struct phy_device *phydev)
102 int retval = phy_read(phydev, MII_BMSR);
104 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
107 /* A structure for mapping a particular speed and duplex
108 * combination to a particular SUPPORTED and ADVERTISED value
116 /* A mapping of all SUPPORTED settings to speed/duplex */
117 static const struct phy_setting settings[] = {
120 .duplex = DUPLEX_FULL,
121 .setting = SUPPORTED_10000baseT_Full,
125 .duplex = DUPLEX_FULL,
126 .setting = SUPPORTED_1000baseT_Full,
130 .duplex = DUPLEX_HALF,
131 .setting = SUPPORTED_1000baseT_Half,
135 .duplex = DUPLEX_FULL,
136 .setting = SUPPORTED_100baseT_Full,
140 .duplex = DUPLEX_HALF,
141 .setting = SUPPORTED_100baseT_Half,
145 .duplex = DUPLEX_FULL,
146 .setting = SUPPORTED_10baseT_Full,
150 .duplex = DUPLEX_HALF,
151 .setting = SUPPORTED_10baseT_Half,
155 #define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
158 * phy_find_setting - find a PHY settings array entry that matches speed & duplex
159 * @speed: speed to match
160 * @duplex: duplex to match
162 * Description: Searches the settings array for the setting which
163 * matches the desired speed and duplex, and returns the index
164 * of that setting. Returns the index of the last setting if
165 * none of the others match.
167 static inline int phy_find_setting(int speed, int duplex)
171 while (idx < ARRAY_SIZE(settings) &&
172 (settings[idx].speed != speed || settings[idx].duplex != duplex))
175 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
179 * phy_find_valid - find a PHY setting that matches the requested features mask
180 * @idx: The first index in settings[] to search
181 * @features: A mask of the valid settings
183 * Description: Returns the index of the first valid setting less
184 * than or equal to the one pointed to by idx, as determined by
185 * the mask in features. Returns the index of the last setting
186 * if nothing else matches.
188 static inline int phy_find_valid(int idx, u32 features)
190 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
193 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
197 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
198 * @phydev: the target phy_device struct
200 * Description: Make sure the PHY is set to supported speeds and
201 * duplexes. Drop down by one in this order: 1000/FULL,
202 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
204 static void phy_sanitize_settings(struct phy_device *phydev)
206 u32 features = phydev->supported;
209 /* Sanitize settings based on PHY capabilities */
210 if ((features & SUPPORTED_Autoneg) == 0)
211 phydev->autoneg = AUTONEG_DISABLE;
213 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
216 phydev->speed = settings[idx].speed;
217 phydev->duplex = settings[idx].duplex;
221 * phy_ethtool_sset - generic ethtool sset function, handles all the details
222 * @phydev: target phy_device struct
225 * A few notes about parameter checking:
226 * - We don't set port or transceiver, so we don't care what they
228 * - phy_start_aneg() will make sure forced settings are sane, and
229 * choose the next best ones from the ones selected, so we don't
230 * care if ethtool tries to give us bad values.
232 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
234 u32 speed = ethtool_cmd_speed(cmd);
236 if (cmd->phy_address != phydev->addr)
239 /* We make sure that we don't pass unsupported values in to the PHY */
240 cmd->advertising &= phydev->supported;
242 /* Verify the settings we care about. */
243 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
246 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
249 if (cmd->autoneg == AUTONEG_DISABLE &&
250 ((speed != SPEED_1000 &&
251 speed != SPEED_100 &&
252 speed != SPEED_10) ||
253 (cmd->duplex != DUPLEX_HALF &&
254 cmd->duplex != DUPLEX_FULL)))
257 phydev->autoneg = cmd->autoneg;
259 phydev->speed = speed;
261 phydev->advertising = cmd->advertising;
263 if (AUTONEG_ENABLE == cmd->autoneg)
264 phydev->advertising |= ADVERTISED_Autoneg;
266 phydev->advertising &= ~ADVERTISED_Autoneg;
268 phydev->duplex = cmd->duplex;
270 /* Restart the PHY */
271 phy_start_aneg(phydev);
275 EXPORT_SYMBOL(phy_ethtool_sset);
277 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
279 cmd->supported = phydev->supported;
281 cmd->advertising = phydev->advertising;
282 cmd->lp_advertising = phydev->lp_advertising;
284 ethtool_cmd_speed_set(cmd, phydev->speed);
285 cmd->duplex = phydev->duplex;
286 cmd->port = PORT_MII;
287 cmd->phy_address = phydev->addr;
288 cmd->transceiver = phy_is_internal(phydev) ?
289 XCVR_INTERNAL : XCVR_EXTERNAL;
290 cmd->autoneg = phydev->autoneg;
294 EXPORT_SYMBOL(phy_ethtool_gset);
297 * phy_mii_ioctl - generic PHY MII ioctl interface
298 * @phydev: the phy_device struct
299 * @ifr: &struct ifreq for socket ioctl's
300 * @cmd: ioctl cmd to execute
302 * Note that this function is currently incompatible with the
303 * PHYCONTROL layer. It changes registers without regard to
304 * current state. Use at own risk.
306 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
308 struct mii_ioctl_data *mii_data = if_mii(ifr);
309 u16 val = mii_data->val_in;
313 mii_data->phy_id = phydev->addr;
317 mii_data->val_out = mdiobus_read(phydev->bus, mii_data->phy_id,
322 if (mii_data->phy_id == phydev->addr) {
323 switch (mii_data->reg_num) {
325 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0)
326 phydev->autoneg = AUTONEG_DISABLE;
328 phydev->autoneg = AUTONEG_ENABLE;
329 if (!phydev->autoneg && (val & BMCR_FULLDPLX))
330 phydev->duplex = DUPLEX_FULL;
332 phydev->duplex = DUPLEX_HALF;
333 if (!phydev->autoneg && (val & BMCR_SPEED1000))
334 phydev->speed = SPEED_1000;
335 else if (!phydev->autoneg &&
336 (val & BMCR_SPEED100))
337 phydev->speed = SPEED_100;
340 phydev->advertising = val;
348 mdiobus_write(phydev->bus, mii_data->phy_id,
349 mii_data->reg_num, val);
351 if (mii_data->reg_num == MII_BMCR &&
353 return phy_init_hw(phydev);
357 if (phydev->drv->hwtstamp)
358 return phydev->drv->hwtstamp(phydev, ifr);
365 EXPORT_SYMBOL(phy_mii_ioctl);
368 * phy_start_aneg - start auto-negotiation for this PHY device
369 * @phydev: the phy_device struct
371 * Description: Sanitizes the settings (if we're not autonegotiating
372 * them), and then calls the driver's config_aneg function.
373 * If the PHYCONTROL Layer is operating, we change the state to
374 * reflect the beginning of Auto-negotiation or forcing.
376 int phy_start_aneg(struct phy_device *phydev)
380 mutex_lock(&phydev->lock);
382 if (AUTONEG_DISABLE == phydev->autoneg)
383 phy_sanitize_settings(phydev);
385 err = phydev->drv->config_aneg(phydev);
389 if (phydev->state != PHY_HALTED) {
390 if (AUTONEG_ENABLE == phydev->autoneg) {
391 phydev->state = PHY_AN;
392 phydev->link_timeout = PHY_AN_TIMEOUT;
394 phydev->state = PHY_FORCING;
395 phydev->link_timeout = PHY_FORCE_TIMEOUT;
400 mutex_unlock(&phydev->lock);
403 EXPORT_SYMBOL(phy_start_aneg);
406 * phy_start_machine - start PHY state machine tracking
407 * @phydev: the phy_device struct
409 * Description: The PHY infrastructure can run a state machine
410 * which tracks whether the PHY is starting up, negotiating,
411 * etc. This function starts the timer which tracks the state
412 * of the PHY. If you want to maintain your own state machine,
413 * do not call this function.
415 void phy_start_machine(struct phy_device *phydev)
417 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
421 * phy_stop_machine - stop the PHY state machine tracking
422 * @phydev: target phy_device struct
424 * Description: Stops the state machine timer, sets the state to UP
425 * (unless it wasn't up yet). This function must be called BEFORE
428 void phy_stop_machine(struct phy_device *phydev)
430 cancel_delayed_work_sync(&phydev->state_queue);
432 mutex_lock(&phydev->lock);
433 if (phydev->state > PHY_UP)
434 phydev->state = PHY_UP;
435 mutex_unlock(&phydev->lock);
439 * phy_error - enter HALTED state for this PHY device
440 * @phydev: target phy_device struct
442 * Moves the PHY to the HALTED state in response to a read
443 * or write error, and tells the controller the link is down.
444 * Must not be called from interrupt context, or while the
445 * phydev->lock is held.
447 static void phy_error(struct phy_device *phydev)
449 mutex_lock(&phydev->lock);
450 phydev->state = PHY_HALTED;
451 mutex_unlock(&phydev->lock);
455 * phy_interrupt - PHY interrupt handler
456 * @irq: interrupt line
457 * @phy_dat: phy_device pointer
459 * Description: When a PHY interrupt occurs, the handler disables
460 * interrupts, and schedules a work task to clear the interrupt.
462 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
464 struct phy_device *phydev = phy_dat;
466 if (PHY_HALTED == phydev->state)
467 return IRQ_NONE; /* It can't be ours. */
469 /* The MDIO bus is not allowed to be written in interrupt
470 * context, so we need to disable the irq here. A work
471 * queue will write the PHY to disable and clear the
472 * interrupt, and then reenable the irq line.
474 disable_irq_nosync(irq);
475 atomic_inc(&phydev->irq_disable);
477 queue_work(system_power_efficient_wq, &phydev->phy_queue);
483 * phy_enable_interrupts - Enable the interrupts from the PHY side
484 * @phydev: target phy_device struct
486 static int phy_enable_interrupts(struct phy_device *phydev)
488 int err = phy_clear_interrupt(phydev);
493 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
497 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
498 * @phydev: target phy_device struct
500 static int phy_disable_interrupts(struct phy_device *phydev)
504 /* Disable PHY interrupts */
505 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
509 /* Clear the interrupt */
510 err = phy_clear_interrupt(phydev);
523 * phy_start_interrupts - request and enable interrupts for a PHY device
524 * @phydev: target phy_device struct
526 * Description: Request the interrupt for the given PHY.
527 * If this fails, then we set irq to PHY_POLL.
528 * Otherwise, we enable the interrupts in the PHY.
529 * This should only be called with a valid IRQ number.
530 * Returns 0 on success or < 0 on error.
532 int phy_start_interrupts(struct phy_device *phydev)
534 atomic_set(&phydev->irq_disable, 0);
535 if (request_irq(phydev->irq, phy_interrupt, 0, "phy_interrupt",
537 pr_warn("%s: Can't get IRQ %d (PHY)\n",
538 phydev->bus->name, phydev->irq);
539 phydev->irq = PHY_POLL;
543 return phy_enable_interrupts(phydev);
545 EXPORT_SYMBOL(phy_start_interrupts);
548 * phy_stop_interrupts - disable interrupts from a PHY device
549 * @phydev: target phy_device struct
551 int phy_stop_interrupts(struct phy_device *phydev)
553 int err = phy_disable_interrupts(phydev);
558 free_irq(phydev->irq, phydev);
560 /* Cannot call flush_scheduled_work() here as desired because
561 * of rtnl_lock(), but we do not really care about what would
562 * be done, except from enable_irq(), so cancel any work
563 * possibly pending and take care of the matter below.
565 cancel_work_sync(&phydev->phy_queue);
566 /* If work indeed has been cancelled, disable_irq() will have
567 * been left unbalanced from phy_interrupt() and enable_irq()
568 * has to be called so that other devices on the line work.
570 while (atomic_dec_return(&phydev->irq_disable) >= 0)
571 enable_irq(phydev->irq);
575 EXPORT_SYMBOL(phy_stop_interrupts);
578 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
579 * @work: work_struct that describes the work to be done
581 void phy_change(struct work_struct *work)
583 struct phy_device *phydev =
584 container_of(work, struct phy_device, phy_queue);
586 if (phydev->drv->did_interrupt &&
587 !phydev->drv->did_interrupt(phydev))
590 if (phy_disable_interrupts(phydev))
593 mutex_lock(&phydev->lock);
594 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
595 phydev->state = PHY_CHANGELINK;
596 mutex_unlock(&phydev->lock);
598 atomic_dec(&phydev->irq_disable);
599 enable_irq(phydev->irq);
601 /* Reenable interrupts */
602 if (PHY_HALTED != phydev->state &&
603 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
606 /* reschedule state queue work to run as soon as possible */
607 cancel_delayed_work_sync(&phydev->state_queue);
608 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
612 atomic_dec(&phydev->irq_disable);
613 enable_irq(phydev->irq);
617 disable_irq(phydev->irq);
618 atomic_inc(&phydev->irq_disable);
624 * phy_stop - Bring down the PHY link, and stop checking the status
625 * @phydev: target phy_device struct
627 void phy_stop(struct phy_device *phydev)
629 mutex_lock(&phydev->lock);
631 if (PHY_HALTED == phydev->state)
634 if (phy_interrupt_is_valid(phydev)) {
635 /* Disable PHY Interrupts */
636 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
638 /* Clear any pending interrupts */
639 phy_clear_interrupt(phydev);
642 phydev->state = PHY_HALTED;
645 mutex_unlock(&phydev->lock);
647 /* Cannot call flush_scheduled_work() here as desired because
648 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
649 * will not reenable interrupts.
652 EXPORT_SYMBOL(phy_stop);
655 * phy_start - start or restart a PHY device
656 * @phydev: target phy_device struct
658 * Description: Indicates the attached device's readiness to
659 * handle PHY-related work. Used during startup to start the
660 * PHY, and after a call to phy_stop() to resume operation.
661 * Also used to indicate the MDIO bus has cleared an error
664 void phy_start(struct phy_device *phydev)
666 mutex_lock(&phydev->lock);
668 switch (phydev->state) {
670 phydev->state = PHY_PENDING;
673 phydev->state = PHY_UP;
676 phydev->state = PHY_RESUMING;
680 mutex_unlock(&phydev->lock);
682 EXPORT_SYMBOL(phy_start);
685 * phy_state_machine - Handle the state machine
686 * @work: work_struct that describes the work to be done
688 void phy_state_machine(struct work_struct *work)
690 struct delayed_work *dwork = to_delayed_work(work);
691 struct phy_device *phydev =
692 container_of(dwork, struct phy_device, state_queue);
693 int needs_aneg = 0, do_suspend = 0;
696 mutex_lock(&phydev->lock);
698 switch (phydev->state) {
707 phydev->link_timeout = PHY_AN_TIMEOUT;
711 err = phy_read_status(phydev);
715 /* If the link is down, give up on negotiation for now */
717 phydev->state = PHY_NOLINK;
718 netif_carrier_off(phydev->attached_dev);
719 phydev->adjust_link(phydev->attached_dev);
723 /* Check if negotiation is done. Break if there's an error */
724 err = phy_aneg_done(phydev);
728 /* If AN is done, we're running */
730 phydev->state = PHY_RUNNING;
731 netif_carrier_on(phydev->attached_dev);
732 phydev->adjust_link(phydev->attached_dev);
734 } else if (0 == phydev->link_timeout--) {
736 /* If we have the magic_aneg bit, we try again */
737 if (phydev->drv->flags & PHY_HAS_MAGICANEG)
742 err = phy_read_status(phydev);
747 phydev->state = PHY_RUNNING;
748 netif_carrier_on(phydev->attached_dev);
749 phydev->adjust_link(phydev->attached_dev);
753 err = genphy_update_link(phydev);
758 phydev->state = PHY_RUNNING;
759 netif_carrier_on(phydev->attached_dev);
761 if (0 == phydev->link_timeout--)
765 phydev->adjust_link(phydev->attached_dev);
768 /* Only register a CHANGE if we are
769 * polling or ignoring interrupts
771 if (!phy_interrupt_is_valid(phydev))
772 phydev->state = PHY_CHANGELINK;
775 err = phy_read_status(phydev);
780 phydev->state = PHY_RUNNING;
781 netif_carrier_on(phydev->attached_dev);
783 phydev->state = PHY_NOLINK;
784 netif_carrier_off(phydev->attached_dev);
787 phydev->adjust_link(phydev->attached_dev);
789 if (phy_interrupt_is_valid(phydev))
790 err = phy_config_interrupt(phydev,
791 PHY_INTERRUPT_ENABLED);
796 netif_carrier_off(phydev->attached_dev);
797 phydev->adjust_link(phydev->attached_dev);
802 err = phy_clear_interrupt(phydev);
806 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
810 if (AUTONEG_ENABLE == phydev->autoneg) {
811 err = phy_aneg_done(phydev);
815 /* err > 0 if AN is done.
816 * Otherwise, it's 0, and we're still waiting for AN
819 err = phy_read_status(phydev);
824 phydev->state = PHY_RUNNING;
825 netif_carrier_on(phydev->attached_dev);
827 phydev->state = PHY_NOLINK;
829 phydev->adjust_link(phydev->attached_dev);
831 phydev->state = PHY_AN;
832 phydev->link_timeout = PHY_AN_TIMEOUT;
835 err = phy_read_status(phydev);
840 phydev->state = PHY_RUNNING;
841 netif_carrier_on(phydev->attached_dev);
843 phydev->state = PHY_NOLINK;
845 phydev->adjust_link(phydev->attached_dev);
850 mutex_unlock(&phydev->lock);
853 err = phy_start_aneg(phydev);
861 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
862 PHY_STATE_TIME * HZ);
865 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
867 cancel_work_sync(&phydev->phy_queue);
868 phydev->link = new_link;
869 schedule_work(&phydev->phy_queue);
871 EXPORT_SYMBOL(phy_mac_interrupt);
873 static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
876 /* Write the desired MMD Devad */
877 bus->write(bus, addr, MII_MMD_CTRL, devad);
879 /* Write the desired MMD register address */
880 bus->write(bus, addr, MII_MMD_DATA, prtad);
882 /* Select the Function : DATA with no post increment */
883 bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
887 * phy_read_mmd_indirect - reads data from the MMD registers
888 * @bus: the target MII bus
889 * @prtad: MMD Address
891 * @addr: PHY address on the MII bus
893 * Description: it reads data from the MMD registers (clause 22 to access to
894 * clause 45) of the specified phy address.
895 * To read these register we have:
896 * 1) Write reg 13 // DEVAD
897 * 2) Write reg 14 // MMD Address
898 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
899 * 3) Read reg 14 // Read MMD data
901 static int phy_read_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
904 mmd_phy_indirect(bus, prtad, devad, addr);
906 /* Read the content of the MMD's selected register */
907 return bus->read(bus, addr, MII_MMD_DATA);
911 * phy_write_mmd_indirect - writes data to the MMD registers
912 * @bus: the target MII bus
913 * @prtad: MMD Address
915 * @addr: PHY address on the MII bus
916 * @data: data to write in the MMD register
918 * Description: Write data from the MMD registers of the specified
920 * To write these register we have:
921 * 1) Write reg 13 // DEVAD
922 * 2) Write reg 14 // MMD Address
923 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
924 * 3) Write reg 14 // Write MMD data
926 static void phy_write_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
929 mmd_phy_indirect(bus, prtad, devad, addr);
931 /* Write the data into MMD's selected register */
932 bus->write(bus, addr, MII_MMD_DATA, data);
936 * phy_init_eee - init and check the EEE feature
937 * @phydev: target phy_device struct
938 * @clk_stop_enable: PHY may stop the clock during LPI
940 * Description: it checks if the Energy-Efficient Ethernet (EEE)
941 * is supported by looking at the MMD registers 3.20 and 7.60/61
942 * and it programs the MMD register 3.0 setting the "Clock stop enable"
945 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
947 /* According to 802.3az,the EEE is supported only in full duplex-mode.
948 * Also EEE feature is active when core is operating with MII, GMII
951 if ((phydev->duplex == DUPLEX_FULL) &&
952 ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
953 (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
954 (phydev->interface == PHY_INTERFACE_MODE_RGMII))) {
955 int eee_lp, eee_cap, eee_adv;
959 /* Read phy status to properly get the right settings */
960 status = phy_read_status(phydev);
964 /* First check if the EEE ability is supported */
965 eee_cap = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
966 MDIO_MMD_PCS, phydev->addr);
970 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
972 return -EPROTONOSUPPORT;
974 /* Check which link settings negotiated and verify it in
975 * the EEE advertising registers.
977 eee_lp = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
978 MDIO_MMD_AN, phydev->addr);
982 eee_adv = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
983 MDIO_MMD_AN, phydev->addr);
987 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
988 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
989 idx = phy_find_setting(phydev->speed, phydev->duplex);
990 if (!(lp & adv & settings[idx].setting))
991 return -EPROTONOSUPPORT;
993 if (clk_stop_enable) {
994 /* Configure the PHY to stop receiving xMII
995 * clock while it is signaling LPI.
997 int val = phy_read_mmd_indirect(phydev->bus, MDIO_CTRL1,
1003 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1004 phy_write_mmd_indirect(phydev->bus, MDIO_CTRL1,
1005 MDIO_MMD_PCS, phydev->addr, val);
1008 return 0; /* EEE supported */
1011 return -EPROTONOSUPPORT;
1013 EXPORT_SYMBOL(phy_init_eee);
1016 * phy_get_eee_err - report the EEE wake error count
1017 * @phydev: target phy_device struct
1019 * Description: it is to report the number of time where the PHY
1020 * failed to complete its normal wake sequence.
1022 int phy_get_eee_err(struct phy_device *phydev)
1024 return phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_WK_ERR,
1025 MDIO_MMD_PCS, phydev->addr);
1027 EXPORT_SYMBOL(phy_get_eee_err);
1030 * phy_ethtool_get_eee - get EEE supported and status
1031 * @phydev: target phy_device struct
1032 * @data: ethtool_eee data
1034 * Description: it reportes the Supported/Advertisement/LP Advertisement
1037 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1041 /* Get Supported EEE */
1042 val = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
1043 MDIO_MMD_PCS, phydev->addr);
1046 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1048 /* Get advertisement EEE */
1049 val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
1050 MDIO_MMD_AN, phydev->addr);
1053 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1055 /* Get LP advertisement EEE */
1056 val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
1057 MDIO_MMD_AN, phydev->addr);
1060 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1064 EXPORT_SYMBOL(phy_ethtool_get_eee);
1067 * phy_ethtool_set_eee - set EEE supported and status
1068 * @phydev: target phy_device struct
1069 * @data: ethtool_eee data
1071 * Description: it is to program the Advertisement EEE register.
1073 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1075 int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
1077 phy_write_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV, MDIO_MMD_AN,
1082 EXPORT_SYMBOL(phy_ethtool_set_eee);
1084 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1086 if (phydev->drv->set_wol)
1087 return phydev->drv->set_wol(phydev, wol);
1091 EXPORT_SYMBOL(phy_ethtool_set_wol);
1093 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1095 if (phydev->drv->get_wol)
1096 phydev->drv->get_wol(phydev, wol);
1098 EXPORT_SYMBOL(phy_ethtool_get_wol);