2 * drivers/net/phy/phy.c
4 * Framework for configuring and reading PHY devices
5 * Based on code in sungem_phy.c and gianfar_phy.c
9 * Copyright (c) 2004 Freescale Semiconductor, Inc.
10 * Copyright (c) 2006, 2007 Maciej W. Rozycki
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/unistd.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
32 #include <linux/module.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/phy.h>
36 #include <linux/timer.h>
37 #include <linux/workqueue.h>
38 #include <linux/mdio.h>
40 #include <linux/atomic.h>
43 #include <asm/uaccess.h>
46 * phy_print_status - Convenience function to print out the current phy status
47 * @phydev: the phy_device struct
49 void phy_print_status(struct phy_device
*phydev
)
52 pr_info("%s - Link is Up - %d/%s\n",
53 dev_name(&phydev
->dev
),
55 DUPLEX_FULL
== phydev
->duplex
? "Full" : "Half");
57 pr_info("%s - Link is Down\n", dev_name(&phydev
->dev
));
59 EXPORT_SYMBOL(phy_print_status
);
62 * phy_clear_interrupt - Ack the phy device's interrupt
63 * @phydev: the phy_device struct
65 * If the @phydev driver has an ack_interrupt function, call it to
66 * ack and clear the phy device's interrupt.
68 * Returns 0 on success on < 0 on error.
70 static int phy_clear_interrupt(struct phy_device
*phydev
)
74 if (phydev
->drv
->ack_interrupt
)
75 err
= phydev
->drv
->ack_interrupt(phydev
);
81 * phy_config_interrupt - configure the PHY device for the requested interrupts
82 * @phydev: the phy_device struct
83 * @interrupts: interrupt flags to configure for this @phydev
85 * Returns 0 on success on < 0 on error.
87 static int phy_config_interrupt(struct phy_device
*phydev
, u32 interrupts
)
91 phydev
->interrupts
= interrupts
;
92 if (phydev
->drv
->config_intr
)
93 err
= phydev
->drv
->config_intr(phydev
);
100 * phy_aneg_done - return auto-negotiation status
101 * @phydev: target phy_device struct
103 * Description: Reads the status register and returns 0 either if
104 * auto-negotiation is incomplete, or if there was an error.
105 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
107 static inline int phy_aneg_done(struct phy_device
*phydev
)
111 retval
= phy_read(phydev
, MII_BMSR
);
113 return (retval
< 0) ? retval
: (retval
& BMSR_ANEGCOMPLETE
);
116 /* A structure for mapping a particular speed and duplex
117 * combination to a particular SUPPORTED and ADVERTISED value */
124 /* A mapping of all SUPPORTED settings to speed/duplex */
125 static const struct phy_setting settings
[] = {
128 .duplex
= DUPLEX_FULL
,
129 .setting
= SUPPORTED_10000baseT_Full
,
133 .duplex
= DUPLEX_FULL
,
134 .setting
= SUPPORTED_1000baseT_Full
,
138 .duplex
= DUPLEX_HALF
,
139 .setting
= SUPPORTED_1000baseT_Half
,
143 .duplex
= DUPLEX_FULL
,
144 .setting
= SUPPORTED_100baseT_Full
,
148 .duplex
= DUPLEX_HALF
,
149 .setting
= SUPPORTED_100baseT_Half
,
153 .duplex
= DUPLEX_FULL
,
154 .setting
= SUPPORTED_10baseT_Full
,
158 .duplex
= DUPLEX_HALF
,
159 .setting
= SUPPORTED_10baseT_Half
,
163 #define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
166 * phy_find_setting - find a PHY settings array entry that matches speed & duplex
167 * @speed: speed to match
168 * @duplex: duplex to match
170 * Description: Searches the settings array for the setting which
171 * matches the desired speed and duplex, and returns the index
172 * of that setting. Returns the index of the last setting if
173 * none of the others match.
175 static inline int phy_find_setting(int speed
, int duplex
)
179 while (idx
< ARRAY_SIZE(settings
) &&
180 (settings
[idx
].speed
!= speed
||
181 settings
[idx
].duplex
!= duplex
))
184 return idx
< MAX_NUM_SETTINGS
? idx
: MAX_NUM_SETTINGS
- 1;
188 * phy_find_valid - find a PHY setting that matches the requested features mask
189 * @idx: The first index in settings[] to search
190 * @features: A mask of the valid settings
192 * Description: Returns the index of the first valid setting less
193 * than or equal to the one pointed to by idx, as determined by
194 * the mask in features. Returns the index of the last setting
195 * if nothing else matches.
197 static inline int phy_find_valid(int idx
, u32 features
)
199 while (idx
< MAX_NUM_SETTINGS
&& !(settings
[idx
].setting
& features
))
202 return idx
< MAX_NUM_SETTINGS
? idx
: MAX_NUM_SETTINGS
- 1;
206 * phy_check_valid - check if there is a valid PHY setting which matches
207 * speed, duplex, and feature mask
208 * @speed: speed to match
209 * @duplex: duplex to match
210 * @features: A mask of the valid settings
212 * Description: Returns true if there is a valid setting, false otherwise.
214 static inline bool phy_check_valid(int speed
, int duplex
, u32 features
)
218 idx
= phy_find_valid(phy_find_setting(speed
, duplex
), features
);
220 return settings
[idx
].speed
== speed
&& settings
[idx
].duplex
== duplex
&&
221 (settings
[idx
].setting
& features
);
225 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
226 * @phydev: the target phy_device struct
228 * Description: Make sure the PHY is set to supported speeds and
229 * duplexes. Drop down by one in this order: 1000/FULL,
230 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
232 static void phy_sanitize_settings(struct phy_device
*phydev
)
234 u32 features
= phydev
->supported
;
237 /* Sanitize settings based on PHY capabilities */
238 if ((features
& SUPPORTED_Autoneg
) == 0)
239 phydev
->autoneg
= AUTONEG_DISABLE
;
241 idx
= phy_find_valid(phy_find_setting(phydev
->speed
, phydev
->duplex
),
244 phydev
->speed
= settings
[idx
].speed
;
245 phydev
->duplex
= settings
[idx
].duplex
;
249 * phy_ethtool_sset - generic ethtool sset function, handles all the details
250 * @phydev: target phy_device struct
253 * A few notes about parameter checking:
254 * - We don't set port or transceiver, so we don't care what they
256 * - phy_start_aneg() will make sure forced settings are sane, and
257 * choose the next best ones from the ones selected, so we don't
258 * care if ethtool tries to give us bad values.
260 int phy_ethtool_sset(struct phy_device
*phydev
, struct ethtool_cmd
*cmd
)
262 u32 speed
= ethtool_cmd_speed(cmd
);
264 if (cmd
->phy_address
!= phydev
->addr
)
267 /* We make sure that we don't pass unsupported
268 * values in to the PHY */
269 cmd
->advertising
&= phydev
->supported
;
271 /* Verify the settings we care about. */
272 if (cmd
->autoneg
!= AUTONEG_ENABLE
&& cmd
->autoneg
!= AUTONEG_DISABLE
)
275 if (cmd
->autoneg
== AUTONEG_ENABLE
&& cmd
->advertising
== 0)
278 if (cmd
->autoneg
== AUTONEG_DISABLE
&&
279 ((speed
!= SPEED_1000
&&
280 speed
!= SPEED_100
&&
281 speed
!= SPEED_10
) ||
282 (cmd
->duplex
!= DUPLEX_HALF
&&
283 cmd
->duplex
!= DUPLEX_FULL
)))
286 phydev
->autoneg
= cmd
->autoneg
;
288 phydev
->speed
= speed
;
290 phydev
->advertising
= cmd
->advertising
;
292 if (AUTONEG_ENABLE
== cmd
->autoneg
)
293 phydev
->advertising
|= ADVERTISED_Autoneg
;
295 phydev
->advertising
&= ~ADVERTISED_Autoneg
;
297 phydev
->duplex
= cmd
->duplex
;
299 /* Restart the PHY */
300 phy_start_aneg(phydev
);
304 EXPORT_SYMBOL(phy_ethtool_sset
);
306 int phy_ethtool_gset(struct phy_device
*phydev
, struct ethtool_cmd
*cmd
)
308 cmd
->supported
= phydev
->supported
;
310 cmd
->advertising
= phydev
->advertising
;
312 ethtool_cmd_speed_set(cmd
, phydev
->speed
);
313 cmd
->duplex
= phydev
->duplex
;
314 cmd
->port
= PORT_MII
;
315 cmd
->phy_address
= phydev
->addr
;
316 cmd
->transceiver
= phy_is_internal(phydev
) ?
317 XCVR_INTERNAL
: XCVR_EXTERNAL
;
318 cmd
->autoneg
= phydev
->autoneg
;
322 EXPORT_SYMBOL(phy_ethtool_gset
);
325 * phy_mii_ioctl - generic PHY MII ioctl interface
326 * @phydev: the phy_device struct
327 * @ifr: &struct ifreq for socket ioctl's
328 * @cmd: ioctl cmd to execute
330 * Note that this function is currently incompatible with the
331 * PHYCONTROL layer. It changes registers without regard to
332 * current state. Use at own risk.
334 int phy_mii_ioctl(struct phy_device
*phydev
,
335 struct ifreq
*ifr
, int cmd
)
337 struct mii_ioctl_data
*mii_data
= if_mii(ifr
);
338 u16 val
= mii_data
->val_in
;
342 mii_data
->phy_id
= phydev
->addr
;
346 mii_data
->val_out
= mdiobus_read(phydev
->bus
, mii_data
->phy_id
,
351 if (mii_data
->phy_id
== phydev
->addr
) {
352 switch(mii_data
->reg_num
) {
354 if ((val
& (BMCR_RESET
|BMCR_ANENABLE
)) == 0)
355 phydev
->autoneg
= AUTONEG_DISABLE
;
357 phydev
->autoneg
= AUTONEG_ENABLE
;
358 if ((!phydev
->autoneg
) && (val
& BMCR_FULLDPLX
))
359 phydev
->duplex
= DUPLEX_FULL
;
361 phydev
->duplex
= DUPLEX_HALF
;
362 if ((!phydev
->autoneg
) &&
363 (val
& BMCR_SPEED1000
))
364 phydev
->speed
= SPEED_1000
;
365 else if ((!phydev
->autoneg
) &&
366 (val
& BMCR_SPEED100
))
367 phydev
->speed
= SPEED_100
;
370 phydev
->advertising
= val
;
378 mdiobus_write(phydev
->bus
, mii_data
->phy_id
,
379 mii_data
->reg_num
, val
);
381 if (mii_data
->reg_num
== MII_BMCR
&&
383 phydev
->drv
->config_init
) {
384 phy_scan_fixups(phydev
);
385 phydev
->drv
->config_init(phydev
);
390 if (phydev
->drv
->hwtstamp
)
391 return phydev
->drv
->hwtstamp(phydev
, ifr
);
400 EXPORT_SYMBOL(phy_mii_ioctl
);
403 * phy_start_aneg - start auto-negotiation for this PHY device
404 * @phydev: the phy_device struct
406 * Description: Sanitizes the settings (if we're not autonegotiating
407 * them), and then calls the driver's config_aneg function.
408 * If the PHYCONTROL Layer is operating, we change the state to
409 * reflect the beginning of Auto-negotiation or forcing.
411 int phy_start_aneg(struct phy_device
*phydev
)
415 mutex_lock(&phydev
->lock
);
417 if (AUTONEG_DISABLE
== phydev
->autoneg
)
418 phy_sanitize_settings(phydev
);
420 err
= phydev
->drv
->config_aneg(phydev
);
425 if (phydev
->state
!= PHY_HALTED
) {
426 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
427 phydev
->state
= PHY_AN
;
428 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
430 phydev
->state
= PHY_FORCING
;
431 phydev
->link_timeout
= PHY_FORCE_TIMEOUT
;
436 mutex_unlock(&phydev
->lock
);
439 EXPORT_SYMBOL(phy_start_aneg
);
443 * phy_start_machine - start PHY state machine tracking
444 * @phydev: the phy_device struct
445 * @handler: callback function for state change notifications
447 * Description: The PHY infrastructure can run a state machine
448 * which tracks whether the PHY is starting up, negotiating,
449 * etc. This function starts the timer which tracks the state
450 * of the PHY. If you want to be notified when the state changes,
451 * pass in the callback @handler, otherwise, pass NULL. If you
452 * want to maintain your own state machine, do not call this
455 void phy_start_machine(struct phy_device
*phydev
,
456 void (*handler
)(struct net_device
*))
458 phydev
->adjust_state
= handler
;
460 queue_delayed_work(system_power_efficient_wq
, &phydev
->state_queue
, HZ
);
464 * phy_stop_machine - stop the PHY state machine tracking
465 * @phydev: target phy_device struct
467 * Description: Stops the state machine timer, sets the state to UP
468 * (unless it wasn't up yet). This function must be called BEFORE
471 void phy_stop_machine(struct phy_device
*phydev
)
473 cancel_delayed_work_sync(&phydev
->state_queue
);
475 mutex_lock(&phydev
->lock
);
476 if (phydev
->state
> PHY_UP
)
477 phydev
->state
= PHY_UP
;
478 mutex_unlock(&phydev
->lock
);
480 phydev
->adjust_state
= NULL
;
484 * phy_error - enter HALTED state for this PHY device
485 * @phydev: target phy_device struct
487 * Moves the PHY to the HALTED state in response to a read
488 * or write error, and tells the controller the link is down.
489 * Must not be called from interrupt context, or while the
490 * phydev->lock is held.
492 static void phy_error(struct phy_device
*phydev
)
494 mutex_lock(&phydev
->lock
);
495 phydev
->state
= PHY_HALTED
;
496 mutex_unlock(&phydev
->lock
);
500 * phy_interrupt - PHY interrupt handler
501 * @irq: interrupt line
502 * @phy_dat: phy_device pointer
504 * Description: When a PHY interrupt occurs, the handler disables
505 * interrupts, and schedules a work task to clear the interrupt.
507 static irqreturn_t
phy_interrupt(int irq
, void *phy_dat
)
509 struct phy_device
*phydev
= phy_dat
;
511 if (PHY_HALTED
== phydev
->state
)
512 return IRQ_NONE
; /* It can't be ours. */
514 /* The MDIO bus is not allowed to be written in interrupt
515 * context, so we need to disable the irq here. A work
516 * queue will write the PHY to disable and clear the
517 * interrupt, and then reenable the irq line. */
518 disable_irq_nosync(irq
);
519 atomic_inc(&phydev
->irq_disable
);
521 queue_work(system_power_efficient_wq
, &phydev
->phy_queue
);
527 * phy_enable_interrupts - Enable the interrupts from the PHY side
528 * @phydev: target phy_device struct
530 static int phy_enable_interrupts(struct phy_device
*phydev
)
534 err
= phy_clear_interrupt(phydev
);
539 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_ENABLED
);
545 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
546 * @phydev: target phy_device struct
548 static int phy_disable_interrupts(struct phy_device
*phydev
)
552 /* Disable PHY interrupts */
553 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_DISABLED
);
558 /* Clear the interrupt */
559 err
= phy_clear_interrupt(phydev
);
573 * phy_start_interrupts - request and enable interrupts for a PHY device
574 * @phydev: target phy_device struct
576 * Description: Request the interrupt for the given PHY.
577 * If this fails, then we set irq to PHY_POLL.
578 * Otherwise, we enable the interrupts in the PHY.
579 * This should only be called with a valid IRQ number.
580 * Returns 0 on success or < 0 on error.
582 int phy_start_interrupts(struct phy_device
*phydev
)
586 atomic_set(&phydev
->irq_disable
, 0);
587 if (request_irq(phydev
->irq
, phy_interrupt
,
591 pr_warn("%s: Can't get IRQ %d (PHY)\n",
592 phydev
->bus
->name
, phydev
->irq
);
593 phydev
->irq
= PHY_POLL
;
597 err
= phy_enable_interrupts(phydev
);
601 EXPORT_SYMBOL(phy_start_interrupts
);
604 * phy_stop_interrupts - disable interrupts from a PHY device
605 * @phydev: target phy_device struct
607 int phy_stop_interrupts(struct phy_device
*phydev
)
611 err
= phy_disable_interrupts(phydev
);
616 free_irq(phydev
->irq
, phydev
);
619 * Cannot call flush_scheduled_work() here as desired because
620 * of rtnl_lock(), but we do not really care about what would
621 * be done, except from enable_irq(), so cancel any work
622 * possibly pending and take care of the matter below.
624 cancel_work_sync(&phydev
->phy_queue
);
626 * If work indeed has been cancelled, disable_irq() will have
627 * been left unbalanced from phy_interrupt() and enable_irq()
628 * has to be called so that other devices on the line work.
630 while (atomic_dec_return(&phydev
->irq_disable
) >= 0)
631 enable_irq(phydev
->irq
);
635 EXPORT_SYMBOL(phy_stop_interrupts
);
639 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
640 * @work: work_struct that describes the work to be done
642 void phy_change(struct work_struct
*work
)
645 struct phy_device
*phydev
=
646 container_of(work
, struct phy_device
, phy_queue
);
648 if (phydev
->drv
->did_interrupt
&&
649 !phydev
->drv
->did_interrupt(phydev
))
652 err
= phy_disable_interrupts(phydev
);
657 mutex_lock(&phydev
->lock
);
658 if ((PHY_RUNNING
== phydev
->state
) || (PHY_NOLINK
== phydev
->state
))
659 phydev
->state
= PHY_CHANGELINK
;
660 mutex_unlock(&phydev
->lock
);
662 atomic_dec(&phydev
->irq_disable
);
663 enable_irq(phydev
->irq
);
665 /* Reenable interrupts */
666 if (PHY_HALTED
!= phydev
->state
)
667 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_ENABLED
);
672 /* reschedule state queue work to run as soon as possible */
673 cancel_delayed_work_sync(&phydev
->state_queue
);
674 queue_delayed_work(system_power_efficient_wq
, &phydev
->state_queue
, 0);
679 atomic_dec(&phydev
->irq_disable
);
680 enable_irq(phydev
->irq
);
684 disable_irq(phydev
->irq
);
685 atomic_inc(&phydev
->irq_disable
);
691 * phy_stop - Bring down the PHY link, and stop checking the status
692 * @phydev: target phy_device struct
694 void phy_stop(struct phy_device
*phydev
)
696 mutex_lock(&phydev
->lock
);
698 if (PHY_HALTED
== phydev
->state
)
701 if (phy_interrupt_is_valid(phydev
)) {
702 /* Disable PHY Interrupts */
703 phy_config_interrupt(phydev
, PHY_INTERRUPT_DISABLED
);
705 /* Clear any pending interrupts */
706 phy_clear_interrupt(phydev
);
709 phydev
->state
= PHY_HALTED
;
712 mutex_unlock(&phydev
->lock
);
715 * Cannot call flush_scheduled_work() here as desired because
716 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
717 * will not reenable interrupts.
723 * phy_start - start or restart a PHY device
724 * @phydev: target phy_device struct
726 * Description: Indicates the attached device's readiness to
727 * handle PHY-related work. Used during startup to start the
728 * PHY, and after a call to phy_stop() to resume operation.
729 * Also used to indicate the MDIO bus has cleared an error
732 void phy_start(struct phy_device
*phydev
)
734 mutex_lock(&phydev
->lock
);
736 switch (phydev
->state
) {
738 phydev
->state
= PHY_PENDING
;
741 phydev
->state
= PHY_UP
;
744 phydev
->state
= PHY_RESUMING
;
748 mutex_unlock(&phydev
->lock
);
750 EXPORT_SYMBOL(phy_stop
);
751 EXPORT_SYMBOL(phy_start
);
754 * phy_state_machine - Handle the state machine
755 * @work: work_struct that describes the work to be done
757 void phy_state_machine(struct work_struct
*work
)
759 struct delayed_work
*dwork
= to_delayed_work(work
);
760 struct phy_device
*phydev
=
761 container_of(dwork
, struct phy_device
, state_queue
);
765 mutex_lock(&phydev
->lock
);
767 if (phydev
->adjust_state
)
768 phydev
->adjust_state(phydev
->attached_dev
);
770 switch(phydev
->state
) {
779 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
783 err
= phy_read_status(phydev
);
788 /* If the link is down, give up on
789 * negotiation for now */
791 phydev
->state
= PHY_NOLINK
;
792 netif_carrier_off(phydev
->attached_dev
);
793 phydev
->adjust_link(phydev
->attached_dev
);
797 /* Check if negotiation is done. Break
798 * if there's an error */
799 err
= phy_aneg_done(phydev
);
803 /* If AN is done, we're running */
805 phydev
->state
= PHY_RUNNING
;
806 netif_carrier_on(phydev
->attached_dev
);
807 phydev
->adjust_link(phydev
->attached_dev
);
809 } else if (0 == phydev
->link_timeout
--) {
811 /* If we have the magic_aneg bit,
813 if (phydev
->drv
->flags
& PHY_HAS_MAGICANEG
)
818 err
= phy_read_status(phydev
);
824 phydev
->state
= PHY_RUNNING
;
825 netif_carrier_on(phydev
->attached_dev
);
826 phydev
->adjust_link(phydev
->attached_dev
);
830 err
= genphy_update_link(phydev
);
836 phydev
->state
= PHY_RUNNING
;
837 netif_carrier_on(phydev
->attached_dev
);
839 if (0 == phydev
->link_timeout
--)
843 phydev
->adjust_link(phydev
->attached_dev
);
846 /* Only register a CHANGE if we are
847 * polling or ignoring interrupts
849 if (!phy_interrupt_is_valid(phydev
))
850 phydev
->state
= PHY_CHANGELINK
;
853 err
= phy_read_status(phydev
);
859 phydev
->state
= PHY_RUNNING
;
860 netif_carrier_on(phydev
->attached_dev
);
862 phydev
->state
= PHY_NOLINK
;
863 netif_carrier_off(phydev
->attached_dev
);
866 phydev
->adjust_link(phydev
->attached_dev
);
868 if (phy_interrupt_is_valid(phydev
))
869 err
= phy_config_interrupt(phydev
,
870 PHY_INTERRUPT_ENABLED
);
875 netif_carrier_off(phydev
->attached_dev
);
876 phydev
->adjust_link(phydev
->attached_dev
);
881 err
= phy_clear_interrupt(phydev
);
886 err
= phy_config_interrupt(phydev
,
887 PHY_INTERRUPT_ENABLED
);
892 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
893 err
= phy_aneg_done(phydev
);
897 /* err > 0 if AN is done.
898 * Otherwise, it's 0, and we're
899 * still waiting for AN */
901 err
= phy_read_status(phydev
);
906 phydev
->state
= PHY_RUNNING
;
907 netif_carrier_on(phydev
->attached_dev
);
909 phydev
->state
= PHY_NOLINK
;
910 phydev
->adjust_link(phydev
->attached_dev
);
912 phydev
->state
= PHY_AN
;
913 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
916 err
= phy_read_status(phydev
);
921 phydev
->state
= PHY_RUNNING
;
922 netif_carrier_on(phydev
->attached_dev
);
924 phydev
->state
= PHY_NOLINK
;
925 phydev
->adjust_link(phydev
->attached_dev
);
930 mutex_unlock(&phydev
->lock
);
933 err
= phy_start_aneg(phydev
);
938 queue_delayed_work(system_power_efficient_wq
, &phydev
->state_queue
,
939 PHY_STATE_TIME
* HZ
);
942 void phy_mac_interrupt(struct phy_device
*phydev
, int new_link
)
944 cancel_work_sync(&phydev
->phy_queue
);
945 phydev
->link
= new_link
;
946 schedule_work(&phydev
->phy_queue
);
948 EXPORT_SYMBOL(phy_mac_interrupt
);
950 static inline void mmd_phy_indirect(struct mii_bus
*bus
, int prtad
, int devad
,
953 /* Write the desired MMD Devad */
954 bus
->write(bus
, addr
, MII_MMD_CTRL
, devad
);
956 /* Write the desired MMD register address */
957 bus
->write(bus
, addr
, MII_MMD_DATA
, prtad
);
959 /* Select the Function : DATA with no post increment */
960 bus
->write(bus
, addr
, MII_MMD_CTRL
, (devad
| MII_MMD_CTRL_NOINCR
));
964 * phy_read_mmd_indirect - reads data from the MMD registers
965 * @bus: the target MII bus
966 * @prtad: MMD Address
968 * @addr: PHY address on the MII bus
970 * Description: it reads data from the MMD registers (clause 22 to access to
971 * clause 45) of the specified phy address.
972 * To read these register we have:
973 * 1) Write reg 13 // DEVAD
974 * 2) Write reg 14 // MMD Address
975 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
976 * 3) Read reg 14 // Read MMD data
978 static int phy_read_mmd_indirect(struct mii_bus
*bus
, int prtad
, int devad
,
983 mmd_phy_indirect(bus
, prtad
, devad
, addr
);
985 /* Read the content of the MMD's selected register */
986 ret
= bus
->read(bus
, addr
, MII_MMD_DATA
);
992 * phy_write_mmd_indirect - writes data to the MMD registers
993 * @bus: the target MII bus
994 * @prtad: MMD Address
996 * @addr: PHY address on the MII bus
997 * @data: data to write in the MMD register
999 * Description: Write data from the MMD registers of the specified
1001 * To write these register we have:
1002 * 1) Write reg 13 // DEVAD
1003 * 2) Write reg 14 // MMD Address
1004 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1005 * 3) Write reg 14 // Write MMD data
1007 static void phy_write_mmd_indirect(struct mii_bus
*bus
, int prtad
, int devad
,
1010 mmd_phy_indirect(bus
, prtad
, devad
, addr
);
1012 /* Write the data into MMD's selected register */
1013 bus
->write(bus
, addr
, MII_MMD_DATA
, data
);
1017 * phy_init_eee - init and check the EEE feature
1018 * @phydev: target phy_device struct
1019 * @clk_stop_enable: PHY may stop the clock during LPI
1021 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1022 * is supported by looking at the MMD registers 3.20 and 7.60/61
1023 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1026 int phy_init_eee(struct phy_device
*phydev
, bool clk_stop_enable
)
1028 int ret
= -EPROTONOSUPPORT
;
1030 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1031 * Also EEE feature is active when core is operating with MII, GMII
1034 if ((phydev
->duplex
== DUPLEX_FULL
) &&
1035 ((phydev
->interface
== PHY_INTERFACE_MODE_MII
) ||
1036 (phydev
->interface
== PHY_INTERFACE_MODE_GMII
) ||
1037 (phydev
->interface
== PHY_INTERFACE_MODE_RGMII
))) {
1038 int eee_lp
, eee_cap
, eee_adv
;
1042 /* Read phy status to properly get the right settings */
1043 status
= phy_read_status(phydev
);
1047 /* First check if the EEE ability is supported */
1048 eee_cap
= phy_read_mmd_indirect(phydev
->bus
, MDIO_PCS_EEE_ABLE
,
1049 MDIO_MMD_PCS
, phydev
->addr
);
1053 cap
= mmd_eee_cap_to_ethtool_sup_t(eee_cap
);
1057 /* Check which link settings negotiated and verify it in
1058 * the EEE advertising registers.
1060 eee_lp
= phy_read_mmd_indirect(phydev
->bus
, MDIO_AN_EEE_LPABLE
,
1061 MDIO_MMD_AN
, phydev
->addr
);
1065 eee_adv
= phy_read_mmd_indirect(phydev
->bus
, MDIO_AN_EEE_ADV
,
1066 MDIO_MMD_AN
, phydev
->addr
);
1070 adv
= mmd_eee_adv_to_ethtool_adv_t(eee_adv
);
1071 lp
= mmd_eee_adv_to_ethtool_adv_t(eee_lp
);
1072 if (!phy_check_valid(phydev
->speed
, phydev
->duplex
, lp
& adv
))
1075 if (clk_stop_enable
) {
1076 /* Configure the PHY to stop receiving xMII
1077 * clock while it is signaling LPI.
1079 int val
= phy_read_mmd_indirect(phydev
->bus
, MDIO_CTRL1
,
1085 val
|= MDIO_PCS_CTRL1_CLKSTOP_EN
;
1086 phy_write_mmd_indirect(phydev
->bus
, MDIO_CTRL1
,
1087 MDIO_MMD_PCS
, phydev
->addr
, val
);
1090 ret
= 0; /* EEE supported */
1096 EXPORT_SYMBOL(phy_init_eee
);
1099 * phy_get_eee_err - report the EEE wake error count
1100 * @phydev: target phy_device struct
1102 * Description: it is to report the number of time where the PHY
1103 * failed to complete its normal wake sequence.
1105 int phy_get_eee_err(struct phy_device
*phydev
)
1107 return phy_read_mmd_indirect(phydev
->bus
, MDIO_PCS_EEE_WK_ERR
,
1108 MDIO_MMD_PCS
, phydev
->addr
);
1111 EXPORT_SYMBOL(phy_get_eee_err
);
1114 * phy_ethtool_get_eee - get EEE supported and status
1115 * @phydev: target phy_device struct
1116 * @data: ethtool_eee data
1118 * Description: it reportes the Supported/Advertisement/LP Advertisement
1121 int phy_ethtool_get_eee(struct phy_device
*phydev
, struct ethtool_eee
*data
)
1125 /* Get Supported EEE */
1126 val
= phy_read_mmd_indirect(phydev
->bus
, MDIO_PCS_EEE_ABLE
,
1127 MDIO_MMD_PCS
, phydev
->addr
);
1130 data
->supported
= mmd_eee_cap_to_ethtool_sup_t(val
);
1132 /* Get advertisement EEE */
1133 val
= phy_read_mmd_indirect(phydev
->bus
, MDIO_AN_EEE_ADV
,
1134 MDIO_MMD_AN
, phydev
->addr
);
1137 data
->advertised
= mmd_eee_adv_to_ethtool_adv_t(val
);
1139 /* Get LP advertisement EEE */
1140 val
= phy_read_mmd_indirect(phydev
->bus
, MDIO_AN_EEE_LPABLE
,
1141 MDIO_MMD_AN
, phydev
->addr
);
1144 data
->lp_advertised
= mmd_eee_adv_to_ethtool_adv_t(val
);
1148 EXPORT_SYMBOL(phy_ethtool_get_eee
);
1151 * phy_ethtool_set_eee - set EEE supported and status
1152 * @phydev: target phy_device struct
1153 * @data: ethtool_eee data
1155 * Description: it is to program the Advertisement EEE register.
1157 int phy_ethtool_set_eee(struct phy_device
*phydev
, struct ethtool_eee
*data
)
1161 val
= ethtool_adv_to_mmd_eee_adv_t(data
->advertised
);
1162 phy_write_mmd_indirect(phydev
->bus
, MDIO_AN_EEE_ADV
, MDIO_MMD_AN
,
1167 EXPORT_SYMBOL(phy_ethtool_set_eee
);
1169 int phy_ethtool_set_wol(struct phy_device
*phydev
, struct ethtool_wolinfo
*wol
)
1171 if (phydev
->drv
->set_wol
)
1172 return phydev
->drv
->set_wol(phydev
, wol
);
1176 EXPORT_SYMBOL(phy_ethtool_set_wol
);
1178 void phy_ethtool_get_wol(struct phy_device
*phydev
, struct ethtool_wolinfo
*wol
)
1180 if (phydev
->drv
->get_wol
)
1181 phydev
->drv
->get_wol(phydev
, wol
);
1183 EXPORT_SYMBOL(phy_ethtool_get_wol
);