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
;
467 cmd
->base
.phy_address
= phydev
->mdio
.addr
;
468 cmd
->base
.autoneg
= phydev
->autoneg
;
469 cmd
->base
.eth_tp_mdix_ctrl
= phydev
->mdix
;
473 EXPORT_SYMBOL(phy_ethtool_ksettings_get
);
476 * phy_mii_ioctl - generic PHY MII ioctl interface
477 * @phydev: the phy_device struct
478 * @ifr: &struct ifreq for socket ioctl's
479 * @cmd: ioctl cmd to execute
481 * Note that this function is currently incompatible with the
482 * PHYCONTROL layer. It changes registers without regard to
483 * current state. Use at own risk.
485 int phy_mii_ioctl(struct phy_device
*phydev
, struct ifreq
*ifr
, int cmd
)
487 struct mii_ioctl_data
*mii_data
= if_mii(ifr
);
488 u16 val
= mii_data
->val_in
;
489 bool change_autoneg
= false;
493 mii_data
->phy_id
= phydev
->mdio
.addr
;
497 mii_data
->val_out
= mdiobus_read(phydev
->mdio
.bus
,
503 if (mii_data
->phy_id
== phydev
->mdio
.addr
) {
504 switch (mii_data
->reg_num
) {
506 if ((val
& (BMCR_RESET
| BMCR_ANENABLE
)) == 0) {
507 if (phydev
->autoneg
== AUTONEG_ENABLE
)
508 change_autoneg
= true;
509 phydev
->autoneg
= AUTONEG_DISABLE
;
510 if (val
& BMCR_FULLDPLX
)
511 phydev
->duplex
= DUPLEX_FULL
;
513 phydev
->duplex
= DUPLEX_HALF
;
514 if (val
& BMCR_SPEED1000
)
515 phydev
->speed
= SPEED_1000
;
516 else if (val
& BMCR_SPEED100
)
517 phydev
->speed
= SPEED_100
;
518 else phydev
->speed
= SPEED_10
;
521 if (phydev
->autoneg
== AUTONEG_DISABLE
)
522 change_autoneg
= true;
523 phydev
->autoneg
= AUTONEG_ENABLE
;
527 phydev
->advertising
= mii_adv_to_ethtool_adv_t(val
);
528 change_autoneg
= true;
536 mdiobus_write(phydev
->mdio
.bus
, mii_data
->phy_id
,
537 mii_data
->reg_num
, val
);
539 if (mii_data
->phy_id
== phydev
->mdio
.addr
&&
540 mii_data
->reg_num
== MII_BMCR
&&
542 return phy_init_hw(phydev
);
545 return phy_start_aneg(phydev
);
550 if (phydev
->drv
->hwtstamp
)
551 return phydev
->drv
->hwtstamp(phydev
, ifr
);
558 EXPORT_SYMBOL(phy_mii_ioctl
);
561 * phy_start_aneg_priv - start auto-negotiation for this PHY device
562 * @phydev: the phy_device struct
563 * @sync: indicate whether we should wait for the workqueue cancelation
565 * Description: Sanitizes the settings (if we're not autonegotiating
566 * them), and then calls the driver's config_aneg function.
567 * If the PHYCONTROL Layer is operating, we change the state to
568 * reflect the beginning of Auto-negotiation or forcing.
570 static int phy_start_aneg_priv(struct phy_device
*phydev
, bool sync
)
575 mutex_lock(&phydev
->lock
);
577 if (AUTONEG_DISABLE
== phydev
->autoneg
)
578 phy_sanitize_settings(phydev
);
580 /* Invalidate LP advertising flags */
581 phydev
->lp_advertising
= 0;
583 err
= phydev
->drv
->config_aneg(phydev
);
587 if (phydev
->state
!= PHY_HALTED
) {
588 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
589 phydev
->state
= PHY_AN
;
590 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
592 phydev
->state
= PHY_FORCING
;
593 phydev
->link_timeout
= PHY_FORCE_TIMEOUT
;
597 /* Re-schedule a PHY state machine to check PHY status because
598 * negotiation may already be done and aneg interrupt may not be
601 if (phydev
->irq
!= PHY_POLL
&& phydev
->state
== PHY_AN
) {
602 err
= phy_aneg_done(phydev
);
610 mutex_unlock(&phydev
->lock
);
613 phy_trigger_machine(phydev
, sync
);
619 * phy_start_aneg - start auto-negotiation for this PHY device
620 * @phydev: the phy_device struct
622 * Description: Sanitizes the settings (if we're not autonegotiating
623 * them), and then calls the driver's config_aneg function.
624 * If the PHYCONTROL Layer is operating, we change the state to
625 * reflect the beginning of Auto-negotiation or forcing.
627 int phy_start_aneg(struct phy_device
*phydev
)
629 return phy_start_aneg_priv(phydev
, true);
631 EXPORT_SYMBOL(phy_start_aneg
);
634 * phy_start_machine - start PHY state machine tracking
635 * @phydev: the phy_device struct
637 * Description: The PHY infrastructure can run a state machine
638 * which tracks whether the PHY is starting up, negotiating,
639 * etc. This function starts the timer which tracks the state
640 * of the PHY. If you want to maintain your own state machine,
641 * do not call this function.
643 void phy_start_machine(struct phy_device
*phydev
)
645 queue_delayed_work(system_power_efficient_wq
, &phydev
->state_queue
, HZ
);
649 * phy_trigger_machine - trigger the state machine to run
651 * @phydev: the phy_device struct
652 * @sync: indicate whether we should wait for the workqueue cancelation
654 * Description: There has been a change in state which requires that the
655 * state machine runs.
658 void phy_trigger_machine(struct phy_device
*phydev
, bool sync
)
661 cancel_delayed_work_sync(&phydev
->state_queue
);
663 cancel_delayed_work(&phydev
->state_queue
);
664 queue_delayed_work(system_power_efficient_wq
, &phydev
->state_queue
, 0);
668 * phy_stop_machine - stop the PHY state machine tracking
669 * @phydev: target phy_device struct
671 * Description: Stops the state machine timer, sets the state to UP
672 * (unless it wasn't up yet). This function must be called BEFORE
675 void phy_stop_machine(struct phy_device
*phydev
)
677 cancel_delayed_work_sync(&phydev
->state_queue
);
679 mutex_lock(&phydev
->lock
);
680 if (phydev
->state
> PHY_UP
&& phydev
->state
!= PHY_HALTED
)
681 phydev
->state
= PHY_UP
;
682 mutex_unlock(&phydev
->lock
);
686 * phy_error - enter HALTED state for this PHY device
687 * @phydev: target phy_device struct
689 * Moves the PHY to the HALTED state in response to a read
690 * or write error, and tells the controller the link is down.
691 * Must not be called from interrupt context, or while the
692 * phydev->lock is held.
694 static void phy_error(struct phy_device
*phydev
)
696 mutex_lock(&phydev
->lock
);
697 phydev
->state
= PHY_HALTED
;
698 mutex_unlock(&phydev
->lock
);
700 phy_trigger_machine(phydev
, false);
704 * phy_interrupt - PHY interrupt handler
705 * @irq: interrupt line
706 * @phy_dat: phy_device pointer
708 * Description: When a PHY interrupt occurs, the handler disables
709 * interrupts, and schedules a work task to clear the interrupt.
711 static irqreturn_t
phy_interrupt(int irq
, void *phy_dat
)
713 struct phy_device
*phydev
= phy_dat
;
715 if (PHY_HALTED
== phydev
->state
)
716 return IRQ_NONE
; /* It can't be ours. */
718 /* The MDIO bus is not allowed to be written in interrupt
719 * context, so we need to disable the irq here. A work
720 * queue will write the PHY to disable and clear the
721 * interrupt, and then reenable the irq line.
723 disable_irq_nosync(irq
);
724 atomic_inc(&phydev
->irq_disable
);
726 queue_work(system_power_efficient_wq
, &phydev
->phy_queue
);
732 * phy_enable_interrupts - Enable the interrupts from the PHY side
733 * @phydev: target phy_device struct
735 static int phy_enable_interrupts(struct phy_device
*phydev
)
737 int err
= phy_clear_interrupt(phydev
);
742 return phy_config_interrupt(phydev
, PHY_INTERRUPT_ENABLED
);
746 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
747 * @phydev: target phy_device struct
749 static int phy_disable_interrupts(struct phy_device
*phydev
)
753 /* Disable PHY interrupts */
754 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_DISABLED
);
758 /* Clear the interrupt */
759 err
= phy_clear_interrupt(phydev
);
772 * phy_start_interrupts - request and enable interrupts for a PHY device
773 * @phydev: target phy_device struct
775 * Description: Request the interrupt for the given PHY.
776 * If this fails, then we set irq to PHY_POLL.
777 * Otherwise, we enable the interrupts in the PHY.
778 * This should only be called with a valid IRQ number.
779 * Returns 0 on success or < 0 on error.
781 int phy_start_interrupts(struct phy_device
*phydev
)
783 atomic_set(&phydev
->irq_disable
, 0);
784 if (request_irq(phydev
->irq
, phy_interrupt
,
788 pr_warn("%s: Can't get IRQ %d (PHY)\n",
789 phydev
->mdio
.bus
->name
, phydev
->irq
);
790 phydev
->irq
= PHY_POLL
;
794 return phy_enable_interrupts(phydev
);
796 EXPORT_SYMBOL(phy_start_interrupts
);
799 * phy_stop_interrupts - disable interrupts from a PHY device
800 * @phydev: target phy_device struct
802 int phy_stop_interrupts(struct phy_device
*phydev
)
804 int err
= phy_disable_interrupts(phydev
);
809 free_irq(phydev
->irq
, phydev
);
811 /* Cannot call flush_scheduled_work() here as desired because
812 * of rtnl_lock(), but we do not really care about what would
813 * be done, except from enable_irq(), so cancel any work
814 * possibly pending and take care of the matter below.
816 cancel_work_sync(&phydev
->phy_queue
);
817 /* If work indeed has been cancelled, disable_irq() will have
818 * been left unbalanced from phy_interrupt() and enable_irq()
819 * has to be called so that other devices on the line work.
821 while (atomic_dec_return(&phydev
->irq_disable
) >= 0)
822 enable_irq(phydev
->irq
);
826 EXPORT_SYMBOL(phy_stop_interrupts
);
829 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
830 * @work: work_struct that describes the work to be done
832 void phy_change(struct work_struct
*work
)
834 struct phy_device
*phydev
=
835 container_of(work
, struct phy_device
, phy_queue
);
837 if (phy_interrupt_is_valid(phydev
)) {
838 if (phydev
->drv
->did_interrupt
&&
839 !phydev
->drv
->did_interrupt(phydev
))
842 if (phy_disable_interrupts(phydev
))
846 mutex_lock(&phydev
->lock
);
847 if ((PHY_RUNNING
== phydev
->state
) || (PHY_NOLINK
== phydev
->state
))
848 phydev
->state
= PHY_CHANGELINK
;
849 mutex_unlock(&phydev
->lock
);
851 if (phy_interrupt_is_valid(phydev
)) {
852 atomic_dec(&phydev
->irq_disable
);
853 enable_irq(phydev
->irq
);
855 /* Reenable interrupts */
856 if (PHY_HALTED
!= phydev
->state
&&
857 phy_config_interrupt(phydev
, PHY_INTERRUPT_ENABLED
))
861 /* reschedule state queue work to run as soon as possible */
862 phy_trigger_machine(phydev
, true);
866 atomic_dec(&phydev
->irq_disable
);
867 enable_irq(phydev
->irq
);
871 disable_irq(phydev
->irq
);
872 atomic_inc(&phydev
->irq_disable
);
878 * phy_stop - Bring down the PHY link, and stop checking the status
879 * @phydev: target phy_device struct
881 void phy_stop(struct phy_device
*phydev
)
883 mutex_lock(&phydev
->lock
);
885 if (PHY_HALTED
== phydev
->state
)
888 if (phy_interrupt_is_valid(phydev
)) {
889 /* Disable PHY Interrupts */
890 phy_config_interrupt(phydev
, PHY_INTERRUPT_DISABLED
);
892 /* Clear any pending interrupts */
893 phy_clear_interrupt(phydev
);
896 phydev
->state
= PHY_HALTED
;
899 mutex_unlock(&phydev
->lock
);
901 /* Cannot call flush_scheduled_work() here as desired because
902 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
903 * will not reenable interrupts.
906 EXPORT_SYMBOL(phy_stop
);
909 * phy_start - start or restart a PHY device
910 * @phydev: target phy_device struct
912 * Description: Indicates the attached device's readiness to
913 * handle PHY-related work. Used during startup to start the
914 * PHY, and after a call to phy_stop() to resume operation.
915 * Also used to indicate the MDIO bus has cleared an error
918 void phy_start(struct phy_device
*phydev
)
920 bool do_resume
= false;
923 mutex_lock(&phydev
->lock
);
925 switch (phydev
->state
) {
927 phydev
->state
= PHY_PENDING
;
930 phydev
->state
= PHY_UP
;
933 /* make sure interrupts are re-enabled for the PHY */
934 if (phy_interrupt_is_valid(phydev
)) {
935 err
= phy_enable_interrupts(phydev
);
940 phydev
->state
= PHY_RESUMING
;
946 mutex_unlock(&phydev
->lock
);
948 /* if phy was suspended, bring the physical link up again */
952 phy_trigger_machine(phydev
, true);
954 EXPORT_SYMBOL(phy_start
);
957 * phy_state_machine - Handle the state machine
958 * @work: work_struct that describes the work to be done
960 void phy_state_machine(struct work_struct
*work
)
962 struct delayed_work
*dwork
= to_delayed_work(work
);
963 struct phy_device
*phydev
=
964 container_of(dwork
, struct phy_device
, state_queue
);
965 bool needs_aneg
= false, do_suspend
= false;
966 enum phy_state old_state
;
970 mutex_lock(&phydev
->lock
);
972 old_state
= phydev
->state
;
974 if (phydev
->drv
->link_change_notify
)
975 phydev
->drv
->link_change_notify(phydev
);
977 switch (phydev
->state
) {
986 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
990 err
= phy_read_status(phydev
);
994 /* If the link is down, give up on negotiation for now */
996 phydev
->state
= PHY_NOLINK
;
997 netif_carrier_off(phydev
->attached_dev
);
998 phydev
->adjust_link(phydev
->attached_dev
);
1002 /* Check if negotiation is done. Break if there's an error */
1003 err
= phy_aneg_done(phydev
);
1007 /* If AN is done, we're running */
1009 phydev
->state
= PHY_RUNNING
;
1010 netif_carrier_on(phydev
->attached_dev
);
1011 phydev
->adjust_link(phydev
->attached_dev
);
1013 } else if (0 == phydev
->link_timeout
--)
1017 if (phy_interrupt_is_valid(phydev
))
1020 err
= phy_read_status(phydev
);
1025 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
1026 err
= phy_aneg_done(phydev
);
1031 phydev
->state
= PHY_AN
;
1032 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
1036 phydev
->state
= PHY_RUNNING
;
1037 netif_carrier_on(phydev
->attached_dev
);
1038 phydev
->adjust_link(phydev
->attached_dev
);
1042 err
= genphy_update_link(phydev
);
1047 phydev
->state
= PHY_RUNNING
;
1048 netif_carrier_on(phydev
->attached_dev
);
1050 if (0 == phydev
->link_timeout
--)
1054 phydev
->adjust_link(phydev
->attached_dev
);
1057 /* Only register a CHANGE if we are polling and link changed
1058 * since latest checking.
1060 if (phydev
->irq
== PHY_POLL
) {
1061 old_link
= phydev
->link
;
1062 err
= phy_read_status(phydev
);
1066 if (old_link
!= phydev
->link
)
1067 phydev
->state
= PHY_CHANGELINK
;
1070 * Failsafe: check that nobody set phydev->link=0 between two
1071 * poll cycles, otherwise we won't leave RUNNING state as long
1072 * as link remains down.
1074 if (!phydev
->link
&& phydev
->state
== PHY_RUNNING
) {
1075 phydev
->state
= PHY_CHANGELINK
;
1076 phydev_err(phydev
, "no link in PHY_RUNNING\n");
1079 case PHY_CHANGELINK
:
1080 err
= phy_read_status(phydev
);
1085 phydev
->state
= PHY_RUNNING
;
1086 netif_carrier_on(phydev
->attached_dev
);
1088 phydev
->state
= PHY_NOLINK
;
1089 netif_carrier_off(phydev
->attached_dev
);
1092 phydev
->adjust_link(phydev
->attached_dev
);
1094 if (phy_interrupt_is_valid(phydev
))
1095 err
= phy_config_interrupt(phydev
,
1096 PHY_INTERRUPT_ENABLED
);
1101 netif_carrier_off(phydev
->attached_dev
);
1102 phydev
->adjust_link(phydev
->attached_dev
);
1107 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
1108 err
= phy_aneg_done(phydev
);
1112 /* err > 0 if AN is done.
1113 * Otherwise, it's 0, and we're still waiting for AN
1116 err
= phy_read_status(phydev
);
1121 phydev
->state
= PHY_RUNNING
;
1122 netif_carrier_on(phydev
->attached_dev
);
1124 phydev
->state
= PHY_NOLINK
;
1126 phydev
->adjust_link(phydev
->attached_dev
);
1128 phydev
->state
= PHY_AN
;
1129 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
1132 err
= phy_read_status(phydev
);
1137 phydev
->state
= PHY_RUNNING
;
1138 netif_carrier_on(phydev
->attached_dev
);
1140 phydev
->state
= PHY_NOLINK
;
1142 phydev
->adjust_link(phydev
->attached_dev
);
1147 mutex_unlock(&phydev
->lock
);
1150 err
= phy_start_aneg_priv(phydev
, false);
1151 else if (do_suspend
)
1152 phy_suspend(phydev
);
1157 phydev_dbg(phydev
, "PHY state change %s -> %s\n",
1158 phy_state_to_str(old_state
),
1159 phy_state_to_str(phydev
->state
));
1161 /* Only re-schedule a PHY state machine change if we are polling the
1162 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1163 * between states from phy_mac_interrupt()
1165 if (phydev
->irq
== PHY_POLL
)
1166 queue_delayed_work(system_power_efficient_wq
, &phydev
->state_queue
,
1167 PHY_STATE_TIME
* HZ
);
1170 void phy_mac_interrupt(struct phy_device
*phydev
, int new_link
)
1172 phydev
->link
= new_link
;
1174 /* Trigger a state machine change */
1175 queue_work(system_power_efficient_wq
, &phydev
->phy_queue
);
1177 EXPORT_SYMBOL(phy_mac_interrupt
);
1179 static inline void mmd_phy_indirect(struct mii_bus
*bus
, int prtad
, int devad
,
1182 /* Write the desired MMD Devad */
1183 bus
->write(bus
, addr
, MII_MMD_CTRL
, devad
);
1185 /* Write the desired MMD register address */
1186 bus
->write(bus
, addr
, MII_MMD_DATA
, prtad
);
1188 /* Select the Function : DATA with no post increment */
1189 bus
->write(bus
, addr
, MII_MMD_CTRL
, (devad
| MII_MMD_CTRL_NOINCR
));
1193 * phy_read_mmd_indirect - reads data from the MMD registers
1194 * @phydev: The PHY device bus
1195 * @prtad: MMD Address
1198 * Description: it reads data from the MMD registers (clause 22 to access to
1199 * clause 45) of the specified phy address.
1200 * To read these register we have:
1201 * 1) Write reg 13 // DEVAD
1202 * 2) Write reg 14 // MMD Address
1203 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1204 * 3) Read reg 14 // Read MMD data
1206 int phy_read_mmd_indirect(struct phy_device
*phydev
, int prtad
, int devad
)
1208 struct phy_driver
*phydrv
= phydev
->drv
;
1209 int addr
= phydev
->mdio
.addr
;
1212 if (!phydrv
->read_mmd_indirect
) {
1213 struct mii_bus
*bus
= phydev
->mdio
.bus
;
1215 mutex_lock(&bus
->mdio_lock
);
1216 mmd_phy_indirect(bus
, prtad
, devad
, addr
);
1218 /* Read the content of the MMD's selected register */
1219 value
= bus
->read(bus
, addr
, MII_MMD_DATA
);
1220 mutex_unlock(&bus
->mdio_lock
);
1222 value
= phydrv
->read_mmd_indirect(phydev
, prtad
, devad
, addr
);
1226 EXPORT_SYMBOL(phy_read_mmd_indirect
);
1229 * phy_write_mmd_indirect - writes data to the MMD registers
1230 * @phydev: The PHY device
1231 * @prtad: MMD Address
1233 * @data: data to write in the MMD register
1235 * Description: Write data from the MMD registers of the specified
1237 * To write these register we have:
1238 * 1) Write reg 13 // DEVAD
1239 * 2) Write reg 14 // MMD Address
1240 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1241 * 3) Write reg 14 // Write MMD data
1243 void phy_write_mmd_indirect(struct phy_device
*phydev
, int prtad
,
1244 int devad
, u32 data
)
1246 struct phy_driver
*phydrv
= phydev
->drv
;
1247 int addr
= phydev
->mdio
.addr
;
1249 if (!phydrv
->write_mmd_indirect
) {
1250 struct mii_bus
*bus
= phydev
->mdio
.bus
;
1252 mutex_lock(&bus
->mdio_lock
);
1253 mmd_phy_indirect(bus
, prtad
, devad
, addr
);
1255 /* Write the data into MMD's selected register */
1256 bus
->write(bus
, addr
, MII_MMD_DATA
, data
);
1257 mutex_unlock(&bus
->mdio_lock
);
1259 phydrv
->write_mmd_indirect(phydev
, prtad
, devad
, addr
, data
);
1262 EXPORT_SYMBOL(phy_write_mmd_indirect
);
1265 * phy_init_eee - init and check the EEE feature
1266 * @phydev: target phy_device struct
1267 * @clk_stop_enable: PHY may stop the clock during LPI
1269 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1270 * is supported by looking at the MMD registers 3.20 and 7.60/61
1271 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1274 int phy_init_eee(struct phy_device
*phydev
, bool clk_stop_enable
)
1276 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1277 * Also EEE feature is active when core is operating with MII, GMII
1278 * or RGMII (all kinds). Internal PHYs are also allowed to proceed and
1279 * should return an error if they do not support EEE.
1281 if ((phydev
->duplex
== DUPLEX_FULL
) &&
1282 ((phydev
->interface
== PHY_INTERFACE_MODE_MII
) ||
1283 (phydev
->interface
== PHY_INTERFACE_MODE_GMII
) ||
1284 phy_interface_is_rgmii(phydev
) ||
1285 phy_is_internal(phydev
))) {
1286 int eee_lp
, eee_cap
, eee_adv
;
1290 /* Read phy status to properly get the right settings */
1291 status
= phy_read_status(phydev
);
1295 /* First check if the EEE ability is supported */
1296 eee_cap
= phy_read_mmd_indirect(phydev
, MDIO_PCS_EEE_ABLE
,
1301 cap
= mmd_eee_cap_to_ethtool_sup_t(eee_cap
);
1305 /* Check which link settings negotiated and verify it in
1306 * the EEE advertising registers.
1308 eee_lp
= phy_read_mmd_indirect(phydev
, MDIO_AN_EEE_LPABLE
,
1313 eee_adv
= phy_read_mmd_indirect(phydev
, MDIO_AN_EEE_ADV
,
1318 adv
= mmd_eee_adv_to_ethtool_adv_t(eee_adv
);
1319 lp
= mmd_eee_adv_to_ethtool_adv_t(eee_lp
);
1320 if (!phy_check_valid(phydev
->speed
, phydev
->duplex
, lp
& adv
))
1323 if (clk_stop_enable
) {
1324 /* Configure the PHY to stop receiving xMII
1325 * clock while it is signaling LPI.
1327 int val
= phy_read_mmd_indirect(phydev
, MDIO_CTRL1
,
1332 val
|= MDIO_PCS_CTRL1_CLKSTOP_EN
;
1333 phy_write_mmd_indirect(phydev
, MDIO_CTRL1
,
1337 return 0; /* EEE supported */
1340 return -EPROTONOSUPPORT
;
1342 EXPORT_SYMBOL(phy_init_eee
);
1345 * phy_get_eee_err - report the EEE wake error count
1346 * @phydev: target phy_device struct
1348 * Description: it is to report the number of time where the PHY
1349 * failed to complete its normal wake sequence.
1351 int phy_get_eee_err(struct phy_device
*phydev
)
1353 return phy_read_mmd_indirect(phydev
, MDIO_PCS_EEE_WK_ERR
, MDIO_MMD_PCS
);
1355 EXPORT_SYMBOL(phy_get_eee_err
);
1358 * phy_ethtool_get_eee - get EEE supported and status
1359 * @phydev: target phy_device struct
1360 * @data: ethtool_eee data
1362 * Description: it reportes the Supported/Advertisement/LP Advertisement
1365 int phy_ethtool_get_eee(struct phy_device
*phydev
, struct ethtool_eee
*data
)
1369 /* Get Supported EEE */
1370 val
= phy_read_mmd_indirect(phydev
, MDIO_PCS_EEE_ABLE
, MDIO_MMD_PCS
);
1373 data
->supported
= mmd_eee_cap_to_ethtool_sup_t(val
);
1375 /* Get advertisement EEE */
1376 val
= phy_read_mmd_indirect(phydev
, MDIO_AN_EEE_ADV
, MDIO_MMD_AN
);
1379 data
->advertised
= mmd_eee_adv_to_ethtool_adv_t(val
);
1381 /* Get LP advertisement EEE */
1382 val
= phy_read_mmd_indirect(phydev
, MDIO_AN_EEE_LPABLE
, MDIO_MMD_AN
);
1385 data
->lp_advertised
= mmd_eee_adv_to_ethtool_adv_t(val
);
1389 EXPORT_SYMBOL(phy_ethtool_get_eee
);
1392 * phy_ethtool_set_eee - set EEE supported and status
1393 * @phydev: target phy_device struct
1394 * @data: ethtool_eee data
1396 * Description: it is to program the Advertisement EEE register.
1398 int phy_ethtool_set_eee(struct phy_device
*phydev
, struct ethtool_eee
*data
)
1400 int val
= ethtool_adv_to_mmd_eee_adv_t(data
->advertised
);
1402 /* Mask prohibited EEE modes */
1403 val
&= ~phydev
->eee_broken_modes
;
1405 phy_write_mmd_indirect(phydev
, MDIO_AN_EEE_ADV
, MDIO_MMD_AN
, val
);
1409 EXPORT_SYMBOL(phy_ethtool_set_eee
);
1411 int phy_ethtool_set_wol(struct phy_device
*phydev
, struct ethtool_wolinfo
*wol
)
1413 if (phydev
->drv
->set_wol
)
1414 return phydev
->drv
->set_wol(phydev
, wol
);
1418 EXPORT_SYMBOL(phy_ethtool_set_wol
);
1420 void phy_ethtool_get_wol(struct phy_device
*phydev
, struct ethtool_wolinfo
*wol
)
1422 if (phydev
->drv
->get_wol
)
1423 phydev
->drv
->get_wol(phydev
, wol
);
1425 EXPORT_SYMBOL(phy_ethtool_get_wol
);
1427 int phy_ethtool_get_link_ksettings(struct net_device
*ndev
,
1428 struct ethtool_link_ksettings
*cmd
)
1430 struct phy_device
*phydev
= ndev
->phydev
;
1435 return phy_ethtool_ksettings_get(phydev
, cmd
);
1437 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings
);
1439 int phy_ethtool_set_link_ksettings(struct net_device
*ndev
,
1440 const struct ethtool_link_ksettings
*cmd
)
1442 struct phy_device
*phydev
= ndev
->phydev
;
1447 return phy_ethtool_ksettings_set(phydev
, cmd
);
1449 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings
);