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/init.h>
24 #include <linux/delay.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/skbuff.h>
29 #include <linux/module.h>
30 #include <linux/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/phy.h>
33 #include <linux/timer.h>
34 #include <linux/workqueue.h>
35 #include <linux/mdio.h>
37 #include <linux/uaccess.h>
38 #include <linux/atomic.h>
43 * phy_print_status - Convenience function to print out the current phy status
44 * @phydev: the phy_device struct
46 void phy_print_status(struct phy_device
*phydev
)
49 pr_info("%s - Link is Up - %d/%s\n",
50 dev_name(&phydev
->dev
),
52 DUPLEX_FULL
== phydev
->duplex
? "Full" : "Half");
54 pr_info("%s - Link is Down\n", dev_name(&phydev
->dev
));
57 EXPORT_SYMBOL(phy_print_status
);
60 * phy_clear_interrupt - Ack the phy device's interrupt
61 * @phydev: the phy_device struct
63 * If the @phydev driver has an ack_interrupt function, call it to
64 * ack and clear the phy device's interrupt.
66 * Returns 0 on success on < 0 on error.
68 static int phy_clear_interrupt(struct phy_device
*phydev
)
70 if (phydev
->drv
->ack_interrupt
)
71 return phydev
->drv
->ack_interrupt(phydev
);
77 * phy_config_interrupt - configure the PHY device for the requested interrupts
78 * @phydev: the phy_device struct
79 * @interrupts: interrupt flags to configure for this @phydev
81 * Returns 0 on success on < 0 on error.
83 static int phy_config_interrupt(struct phy_device
*phydev
, u32 interrupts
)
85 phydev
->interrupts
= interrupts
;
86 if (phydev
->drv
->config_intr
)
87 return phydev
->drv
->config_intr(phydev
);
94 * phy_aneg_done - return auto-negotiation status
95 * @phydev: target phy_device struct
97 * Description: Reads the status register and returns 0 either if
98 * auto-negotiation is incomplete, or if there was an error.
99 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
101 static inline int phy_aneg_done(struct phy_device
*phydev
)
103 int retval
= phy_read(phydev
, MII_BMSR
);
105 return (retval
< 0) ? retval
: (retval
& BMSR_ANEGCOMPLETE
);
108 /* A structure for mapping a particular speed and duplex
109 * combination to a particular SUPPORTED and ADVERTISED value
117 /* A mapping of all SUPPORTED settings to speed/duplex */
118 static const struct phy_setting settings
[] = {
121 .duplex
= DUPLEX_FULL
,
122 .setting
= SUPPORTED_10000baseT_Full
,
126 .duplex
= DUPLEX_FULL
,
127 .setting
= SUPPORTED_1000baseT_Full
,
131 .duplex
= DUPLEX_HALF
,
132 .setting
= SUPPORTED_1000baseT_Half
,
136 .duplex
= DUPLEX_FULL
,
137 .setting
= SUPPORTED_100baseT_Full
,
141 .duplex
= DUPLEX_HALF
,
142 .setting
= SUPPORTED_100baseT_Half
,
146 .duplex
= DUPLEX_FULL
,
147 .setting
= SUPPORTED_10baseT_Full
,
151 .duplex
= DUPLEX_HALF
,
152 .setting
= SUPPORTED_10baseT_Half
,
156 #define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
159 * phy_find_setting - find a PHY settings array entry that matches speed & duplex
160 * @speed: speed to match
161 * @duplex: duplex to match
163 * Description: Searches the settings array for the setting which
164 * matches the desired speed and duplex, and returns the index
165 * of that setting. Returns the index of the last setting if
166 * none of the others match.
168 static inline int phy_find_setting(int speed
, int duplex
)
172 while (idx
< ARRAY_SIZE(settings
) &&
173 (settings
[idx
].speed
!= speed
|| settings
[idx
].duplex
!= duplex
))
176 return idx
< MAX_NUM_SETTINGS
? idx
: MAX_NUM_SETTINGS
- 1;
180 * phy_find_valid - find a PHY setting that matches the requested features mask
181 * @idx: The first index in settings[] to search
182 * @features: A mask of the valid settings
184 * Description: Returns the index of the first valid setting less
185 * than or equal to the one pointed to by idx, as determined by
186 * the mask in features. Returns the index of the last setting
187 * if nothing else matches.
189 static inline int phy_find_valid(int idx
, u32 features
)
191 while (idx
< MAX_NUM_SETTINGS
&& !(settings
[idx
].setting
& features
))
194 return idx
< MAX_NUM_SETTINGS
? idx
: MAX_NUM_SETTINGS
- 1;
198 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
199 * @phydev: the target phy_device struct
201 * Description: Make sure the PHY is set to supported speeds and
202 * duplexes. Drop down by one in this order: 1000/FULL,
203 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
205 static void phy_sanitize_settings(struct phy_device
*phydev
)
207 u32 features
= phydev
->supported
;
210 /* Sanitize settings based on PHY capabilities */
211 if ((features
& SUPPORTED_Autoneg
) == 0)
212 phydev
->autoneg
= AUTONEG_DISABLE
;
214 idx
= phy_find_valid(phy_find_setting(phydev
->speed
, phydev
->duplex
),
217 phydev
->speed
= settings
[idx
].speed
;
218 phydev
->duplex
= settings
[idx
].duplex
;
222 * phy_ethtool_sset - generic ethtool sset function, handles all the details
223 * @phydev: target phy_device struct
226 * A few notes about parameter checking:
227 * - We don't set port or transceiver, so we don't care what they
229 * - phy_start_aneg() will make sure forced settings are sane, and
230 * choose the next best ones from the ones selected, so we don't
231 * care if ethtool tries to give us bad values.
233 int phy_ethtool_sset(struct phy_device
*phydev
, struct ethtool_cmd
*cmd
)
235 u32 speed
= ethtool_cmd_speed(cmd
);
237 if (cmd
->phy_address
!= phydev
->addr
)
240 /* We make sure that we don't pass unsupported values in to the PHY */
241 cmd
->advertising
&= phydev
->supported
;
243 /* Verify the settings we care about. */
244 if (cmd
->autoneg
!= AUTONEG_ENABLE
&& cmd
->autoneg
!= AUTONEG_DISABLE
)
247 if (cmd
->autoneg
== AUTONEG_ENABLE
&& cmd
->advertising
== 0)
250 if (cmd
->autoneg
== AUTONEG_DISABLE
&&
251 ((speed
!= SPEED_1000
&&
252 speed
!= SPEED_100
&&
253 speed
!= SPEED_10
) ||
254 (cmd
->duplex
!= DUPLEX_HALF
&&
255 cmd
->duplex
!= DUPLEX_FULL
)))
258 phydev
->autoneg
= cmd
->autoneg
;
260 phydev
->speed
= speed
;
262 phydev
->advertising
= cmd
->advertising
;
264 if (AUTONEG_ENABLE
== cmd
->autoneg
)
265 phydev
->advertising
|= ADVERTISED_Autoneg
;
267 phydev
->advertising
&= ~ADVERTISED_Autoneg
;
269 phydev
->duplex
= cmd
->duplex
;
271 /* Restart the PHY */
272 phy_start_aneg(phydev
);
276 EXPORT_SYMBOL(phy_ethtool_sset
);
278 int phy_ethtool_gset(struct phy_device
*phydev
, struct ethtool_cmd
*cmd
)
280 cmd
->supported
= phydev
->supported
;
282 cmd
->advertising
= phydev
->advertising
;
283 cmd
->lp_advertising
= phydev
->lp_advertising
;
285 ethtool_cmd_speed_set(cmd
, phydev
->speed
);
286 cmd
->duplex
= phydev
->duplex
;
287 cmd
->port
= PORT_MII
;
288 cmd
->phy_address
= phydev
->addr
;
289 cmd
->transceiver
= phy_is_internal(phydev
) ?
290 XCVR_INTERNAL
: XCVR_EXTERNAL
;
291 cmd
->autoneg
= phydev
->autoneg
;
295 EXPORT_SYMBOL(phy_ethtool_gset
);
298 * phy_mii_ioctl - generic PHY MII ioctl interface
299 * @phydev: the phy_device struct
300 * @ifr: &struct ifreq for socket ioctl's
301 * @cmd: ioctl cmd to execute
303 * Note that this function is currently incompatible with the
304 * PHYCONTROL layer. It changes registers without regard to
305 * current state. Use at own risk.
307 int phy_mii_ioctl(struct phy_device
*phydev
, struct ifreq
*ifr
, int cmd
)
309 struct mii_ioctl_data
*mii_data
= if_mii(ifr
);
310 u16 val
= mii_data
->val_in
;
314 mii_data
->phy_id
= phydev
->addr
;
318 mii_data
->val_out
= mdiobus_read(phydev
->bus
, mii_data
->phy_id
,
323 if (mii_data
->phy_id
== phydev
->addr
) {
324 switch (mii_data
->reg_num
) {
326 if ((val
& (BMCR_RESET
| BMCR_ANENABLE
)) == 0)
327 phydev
->autoneg
= AUTONEG_DISABLE
;
329 phydev
->autoneg
= AUTONEG_ENABLE
;
330 if (!phydev
->autoneg
&& (val
& BMCR_FULLDPLX
))
331 phydev
->duplex
= DUPLEX_FULL
;
333 phydev
->duplex
= DUPLEX_HALF
;
334 if (!phydev
->autoneg
&& (val
& BMCR_SPEED1000
))
335 phydev
->speed
= SPEED_1000
;
336 else if (!phydev
->autoneg
&&
337 (val
& BMCR_SPEED100
))
338 phydev
->speed
= SPEED_100
;
341 phydev
->advertising
= val
;
349 mdiobus_write(phydev
->bus
, mii_data
->phy_id
,
350 mii_data
->reg_num
, val
);
352 if (mii_data
->reg_num
== MII_BMCR
&&
354 return phy_init_hw(phydev
);
358 if (phydev
->drv
->hwtstamp
)
359 return phydev
->drv
->hwtstamp(phydev
, ifr
);
366 EXPORT_SYMBOL(phy_mii_ioctl
);
369 * phy_start_aneg - start auto-negotiation for this PHY device
370 * @phydev: the phy_device struct
372 * Description: Sanitizes the settings (if we're not autonegotiating
373 * them), and then calls the driver's config_aneg function.
374 * If the PHYCONTROL Layer is operating, we change the state to
375 * reflect the beginning of Auto-negotiation or forcing.
377 int phy_start_aneg(struct phy_device
*phydev
)
381 mutex_lock(&phydev
->lock
);
383 if (AUTONEG_DISABLE
== phydev
->autoneg
)
384 phy_sanitize_settings(phydev
);
386 err
= phydev
->drv
->config_aneg(phydev
);
390 if (phydev
->state
!= PHY_HALTED
) {
391 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
392 phydev
->state
= PHY_AN
;
393 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
395 phydev
->state
= PHY_FORCING
;
396 phydev
->link_timeout
= PHY_FORCE_TIMEOUT
;
401 mutex_unlock(&phydev
->lock
);
404 EXPORT_SYMBOL(phy_start_aneg
);
407 * phy_start_machine - start PHY state machine tracking
408 * @phydev: the phy_device struct
410 * Description: The PHY infrastructure can run a state machine
411 * which tracks whether the PHY is starting up, negotiating,
412 * etc. This function starts the timer which tracks the state
413 * of the PHY. If you want to maintain your own state machine,
414 * do not call this function.
416 void phy_start_machine(struct phy_device
*phydev
)
418 queue_delayed_work(system_power_efficient_wq
, &phydev
->state_queue
, HZ
);
422 * phy_stop_machine - stop the PHY state machine tracking
423 * @phydev: target phy_device struct
425 * Description: Stops the state machine timer, sets the state to UP
426 * (unless it wasn't up yet). This function must be called BEFORE
429 void phy_stop_machine(struct phy_device
*phydev
)
431 cancel_delayed_work_sync(&phydev
->state_queue
);
433 mutex_lock(&phydev
->lock
);
434 if (phydev
->state
> PHY_UP
)
435 phydev
->state
= PHY_UP
;
436 mutex_unlock(&phydev
->lock
);
440 * phy_error - enter HALTED state for this PHY device
441 * @phydev: target phy_device struct
443 * Moves the PHY to the HALTED state in response to a read
444 * or write error, and tells the controller the link is down.
445 * Must not be called from interrupt context, or while the
446 * phydev->lock is held.
448 static void phy_error(struct phy_device
*phydev
)
450 mutex_lock(&phydev
->lock
);
451 phydev
->state
= PHY_HALTED
;
452 mutex_unlock(&phydev
->lock
);
456 * phy_interrupt - PHY interrupt handler
457 * @irq: interrupt line
458 * @phy_dat: phy_device pointer
460 * Description: When a PHY interrupt occurs, the handler disables
461 * interrupts, and schedules a work task to clear the interrupt.
463 static irqreturn_t
phy_interrupt(int irq
, void *phy_dat
)
465 struct phy_device
*phydev
= phy_dat
;
467 if (PHY_HALTED
== phydev
->state
)
468 return IRQ_NONE
; /* It can't be ours. */
470 /* The MDIO bus is not allowed to be written in interrupt
471 * context, so we need to disable the irq here. A work
472 * queue will write the PHY to disable and clear the
473 * interrupt, and then reenable the irq line.
475 disable_irq_nosync(irq
);
476 atomic_inc(&phydev
->irq_disable
);
478 queue_work(system_power_efficient_wq
, &phydev
->phy_queue
);
484 * phy_enable_interrupts - Enable the interrupts from the PHY side
485 * @phydev: target phy_device struct
487 static int phy_enable_interrupts(struct phy_device
*phydev
)
489 int err
= phy_clear_interrupt(phydev
);
494 return phy_config_interrupt(phydev
, PHY_INTERRUPT_ENABLED
);
498 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
499 * @phydev: target phy_device struct
501 static int phy_disable_interrupts(struct phy_device
*phydev
)
505 /* Disable PHY interrupts */
506 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_DISABLED
);
510 /* Clear the interrupt */
511 err
= phy_clear_interrupt(phydev
);
524 * phy_start_interrupts - request and enable interrupts for a PHY device
525 * @phydev: target phy_device struct
527 * Description: Request the interrupt for the given PHY.
528 * If this fails, then we set irq to PHY_POLL.
529 * Otherwise, we enable the interrupts in the PHY.
530 * This should only be called with a valid IRQ number.
531 * Returns 0 on success or < 0 on error.
533 int phy_start_interrupts(struct phy_device
*phydev
)
535 atomic_set(&phydev
->irq_disable
, 0);
536 if (request_irq(phydev
->irq
, phy_interrupt
,
540 pr_warn("%s: Can't get IRQ %d (PHY)\n",
541 phydev
->bus
->name
, phydev
->irq
);
542 phydev
->irq
= PHY_POLL
;
546 return phy_enable_interrupts(phydev
);
548 EXPORT_SYMBOL(phy_start_interrupts
);
551 * phy_stop_interrupts - disable interrupts from a PHY device
552 * @phydev: target phy_device struct
554 int phy_stop_interrupts(struct phy_device
*phydev
)
556 int err
= phy_disable_interrupts(phydev
);
561 free_irq(phydev
->irq
, phydev
);
563 /* Cannot call flush_scheduled_work() here as desired because
564 * of rtnl_lock(), but we do not really care about what would
565 * be done, except from enable_irq(), so cancel any work
566 * possibly pending and take care of the matter below.
568 cancel_work_sync(&phydev
->phy_queue
);
569 /* If work indeed has been cancelled, disable_irq() will have
570 * been left unbalanced from phy_interrupt() and enable_irq()
571 * has to be called so that other devices on the line work.
573 while (atomic_dec_return(&phydev
->irq_disable
) >= 0)
574 enable_irq(phydev
->irq
);
578 EXPORT_SYMBOL(phy_stop_interrupts
);
581 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
582 * @work: work_struct that describes the work to be done
584 void phy_change(struct work_struct
*work
)
586 struct phy_device
*phydev
=
587 container_of(work
, struct phy_device
, phy_queue
);
589 if (phydev
->drv
->did_interrupt
&&
590 !phydev
->drv
->did_interrupt(phydev
))
593 if (phy_disable_interrupts(phydev
))
596 mutex_lock(&phydev
->lock
);
597 if ((PHY_RUNNING
== phydev
->state
) || (PHY_NOLINK
== phydev
->state
))
598 phydev
->state
= PHY_CHANGELINK
;
599 mutex_unlock(&phydev
->lock
);
601 atomic_dec(&phydev
->irq_disable
);
602 enable_irq(phydev
->irq
);
604 /* Reenable interrupts */
605 if (PHY_HALTED
!= phydev
->state
&&
606 phy_config_interrupt(phydev
, PHY_INTERRUPT_ENABLED
))
609 /* reschedule state queue work to run as soon as possible */
610 cancel_delayed_work_sync(&phydev
->state_queue
);
611 queue_delayed_work(system_power_efficient_wq
, &phydev
->state_queue
, 0);
615 atomic_dec(&phydev
->irq_disable
);
616 enable_irq(phydev
->irq
);
620 disable_irq(phydev
->irq
);
621 atomic_inc(&phydev
->irq_disable
);
627 * phy_stop - Bring down the PHY link, and stop checking the status
628 * @phydev: target phy_device struct
630 void phy_stop(struct phy_device
*phydev
)
632 mutex_lock(&phydev
->lock
);
634 if (PHY_HALTED
== phydev
->state
)
637 if (phy_interrupt_is_valid(phydev
)) {
638 /* Disable PHY Interrupts */
639 phy_config_interrupt(phydev
, PHY_INTERRUPT_DISABLED
);
641 /* Clear any pending interrupts */
642 phy_clear_interrupt(phydev
);
645 phydev
->state
= PHY_HALTED
;
648 mutex_unlock(&phydev
->lock
);
650 /* Cannot call flush_scheduled_work() here as desired because
651 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
652 * will not reenable interrupts.
655 EXPORT_SYMBOL(phy_stop
);
658 * phy_start - start or restart a PHY device
659 * @phydev: target phy_device struct
661 * Description: Indicates the attached device's readiness to
662 * handle PHY-related work. Used during startup to start the
663 * PHY, and after a call to phy_stop() to resume operation.
664 * Also used to indicate the MDIO bus has cleared an error
667 void phy_start(struct phy_device
*phydev
)
669 mutex_lock(&phydev
->lock
);
671 switch (phydev
->state
) {
673 phydev
->state
= PHY_PENDING
;
676 phydev
->state
= PHY_UP
;
679 phydev
->state
= PHY_RESUMING
;
683 mutex_unlock(&phydev
->lock
);
685 EXPORT_SYMBOL(phy_start
);
688 * phy_state_machine - Handle the state machine
689 * @work: work_struct that describes the work to be done
691 void phy_state_machine(struct work_struct
*work
)
693 struct delayed_work
*dwork
= to_delayed_work(work
);
694 struct phy_device
*phydev
=
695 container_of(dwork
, struct phy_device
, state_queue
);
696 int needs_aneg
= 0, do_suspend
= 0;
699 mutex_lock(&phydev
->lock
);
701 switch (phydev
->state
) {
710 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
714 err
= phy_read_status(phydev
);
718 /* If the link is down, give up on negotiation for now */
720 phydev
->state
= PHY_NOLINK
;
721 netif_carrier_off(phydev
->attached_dev
);
722 phydev
->adjust_link(phydev
->attached_dev
);
726 /* Check if negotiation is done. Break if there's an error */
727 err
= phy_aneg_done(phydev
);
731 /* If AN is done, we're running */
733 phydev
->state
= PHY_RUNNING
;
734 netif_carrier_on(phydev
->attached_dev
);
735 phydev
->adjust_link(phydev
->attached_dev
);
737 } else if (0 == phydev
->link_timeout
--) {
739 /* If we have the magic_aneg bit, we try again */
740 if (phydev
->drv
->flags
& PHY_HAS_MAGICANEG
)
745 err
= phy_read_status(phydev
);
750 phydev
->state
= PHY_RUNNING
;
751 netif_carrier_on(phydev
->attached_dev
);
752 phydev
->adjust_link(phydev
->attached_dev
);
756 err
= genphy_update_link(phydev
);
761 phydev
->state
= PHY_RUNNING
;
762 netif_carrier_on(phydev
->attached_dev
);
764 if (0 == phydev
->link_timeout
--)
768 phydev
->adjust_link(phydev
->attached_dev
);
771 /* Only register a CHANGE if we are
772 * polling or ignoring interrupts
774 if (!phy_interrupt_is_valid(phydev
))
775 phydev
->state
= PHY_CHANGELINK
;
778 err
= phy_read_status(phydev
);
783 phydev
->state
= PHY_RUNNING
;
784 netif_carrier_on(phydev
->attached_dev
);
786 phydev
->state
= PHY_NOLINK
;
787 netif_carrier_off(phydev
->attached_dev
);
790 phydev
->adjust_link(phydev
->attached_dev
);
792 if (phy_interrupt_is_valid(phydev
))
793 err
= phy_config_interrupt(phydev
,
794 PHY_INTERRUPT_ENABLED
);
799 netif_carrier_off(phydev
->attached_dev
);
800 phydev
->adjust_link(phydev
->attached_dev
);
805 err
= phy_clear_interrupt(phydev
);
809 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_ENABLED
);
813 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
814 err
= phy_aneg_done(phydev
);
818 /* err > 0 if AN is done.
819 * Otherwise, it's 0, and we're still waiting for AN
822 err
= phy_read_status(phydev
);
827 phydev
->state
= PHY_RUNNING
;
828 netif_carrier_on(phydev
->attached_dev
);
830 phydev
->state
= PHY_NOLINK
;
832 phydev
->adjust_link(phydev
->attached_dev
);
834 phydev
->state
= PHY_AN
;
835 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
838 err
= phy_read_status(phydev
);
843 phydev
->state
= PHY_RUNNING
;
844 netif_carrier_on(phydev
->attached_dev
);
846 phydev
->state
= PHY_NOLINK
;
848 phydev
->adjust_link(phydev
->attached_dev
);
853 mutex_unlock(&phydev
->lock
);
856 err
= phy_start_aneg(phydev
);
864 queue_delayed_work(system_power_efficient_wq
, &phydev
->state_queue
,
865 PHY_STATE_TIME
* HZ
);
868 void phy_mac_interrupt(struct phy_device
*phydev
, int new_link
)
870 cancel_work_sync(&phydev
->phy_queue
);
871 phydev
->link
= new_link
;
872 schedule_work(&phydev
->phy_queue
);
874 EXPORT_SYMBOL(phy_mac_interrupt
);
876 static inline void mmd_phy_indirect(struct mii_bus
*bus
, int prtad
, int devad
,
879 /* Write the desired MMD Devad */
880 bus
->write(bus
, addr
, MII_MMD_CTRL
, devad
);
882 /* Write the desired MMD register address */
883 bus
->write(bus
, addr
, MII_MMD_DATA
, prtad
);
885 /* Select the Function : DATA with no post increment */
886 bus
->write(bus
, addr
, MII_MMD_CTRL
, (devad
| MII_MMD_CTRL_NOINCR
));
890 * phy_read_mmd_indirect - reads data from the MMD registers
891 * @bus: the target MII bus
892 * @prtad: MMD Address
894 * @addr: PHY address on the MII bus
896 * Description: it reads data from the MMD registers (clause 22 to access to
897 * clause 45) of the specified phy address.
898 * To read these register we have:
899 * 1) Write reg 13 // DEVAD
900 * 2) Write reg 14 // MMD Address
901 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
902 * 3) Read reg 14 // Read MMD data
904 static int phy_read_mmd_indirect(struct mii_bus
*bus
, int prtad
, int devad
,
907 mmd_phy_indirect(bus
, prtad
, devad
, addr
);
909 /* Read the content of the MMD's selected register */
910 return bus
->read(bus
, addr
, MII_MMD_DATA
);
914 * phy_write_mmd_indirect - writes data to the MMD registers
915 * @bus: the target MII bus
916 * @prtad: MMD Address
918 * @addr: PHY address on the MII bus
919 * @data: data to write in the MMD register
921 * Description: Write data from the MMD registers of the specified
923 * To write these register we have:
924 * 1) Write reg 13 // DEVAD
925 * 2) Write reg 14 // MMD Address
926 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
927 * 3) Write reg 14 // Write MMD data
929 static void phy_write_mmd_indirect(struct mii_bus
*bus
, int prtad
, int devad
,
932 mmd_phy_indirect(bus
, prtad
, devad
, addr
);
934 /* Write the data into MMD's selected register */
935 bus
->write(bus
, addr
, MII_MMD_DATA
, data
);
939 * phy_init_eee - init and check the EEE feature
940 * @phydev: target phy_device struct
941 * @clk_stop_enable: PHY may stop the clock during LPI
943 * Description: it checks if the Energy-Efficient Ethernet (EEE)
944 * is supported by looking at the MMD registers 3.20 and 7.60/61
945 * and it programs the MMD register 3.0 setting the "Clock stop enable"
948 int phy_init_eee(struct phy_device
*phydev
, bool clk_stop_enable
)
950 /* According to 802.3az,the EEE is supported only in full duplex-mode.
951 * Also EEE feature is active when core is operating with MII, GMII
954 if ((phydev
->duplex
== DUPLEX_FULL
) &&
955 ((phydev
->interface
== PHY_INTERFACE_MODE_MII
) ||
956 (phydev
->interface
== PHY_INTERFACE_MODE_GMII
) ||
957 (phydev
->interface
== PHY_INTERFACE_MODE_RGMII
))) {
958 int eee_lp
, eee_cap
, eee_adv
;
962 /* Read phy status to properly get the right settings */
963 status
= phy_read_status(phydev
);
967 /* First check if the EEE ability is supported */
968 eee_cap
= phy_read_mmd_indirect(phydev
->bus
, MDIO_PCS_EEE_ABLE
,
969 MDIO_MMD_PCS
, phydev
->addr
);
973 cap
= mmd_eee_cap_to_ethtool_sup_t(eee_cap
);
975 return -EPROTONOSUPPORT
;
977 /* Check which link settings negotiated and verify it in
978 * the EEE advertising registers.
980 eee_lp
= phy_read_mmd_indirect(phydev
->bus
, MDIO_AN_EEE_LPABLE
,
981 MDIO_MMD_AN
, phydev
->addr
);
985 eee_adv
= phy_read_mmd_indirect(phydev
->bus
, MDIO_AN_EEE_ADV
,
986 MDIO_MMD_AN
, phydev
->addr
);
990 adv
= mmd_eee_adv_to_ethtool_adv_t(eee_adv
);
991 lp
= mmd_eee_adv_to_ethtool_adv_t(eee_lp
);
992 idx
= phy_find_setting(phydev
->speed
, phydev
->duplex
);
993 if (!(lp
& adv
& settings
[idx
].setting
))
994 return -EPROTONOSUPPORT
;
996 if (clk_stop_enable
) {
997 /* Configure the PHY to stop receiving xMII
998 * clock while it is signaling LPI.
1000 int val
= phy_read_mmd_indirect(phydev
->bus
, MDIO_CTRL1
,
1006 val
|= MDIO_PCS_CTRL1_CLKSTOP_EN
;
1007 phy_write_mmd_indirect(phydev
->bus
, MDIO_CTRL1
,
1008 MDIO_MMD_PCS
, phydev
->addr
, val
);
1011 return 0; /* EEE supported */
1014 return -EPROTONOSUPPORT
;
1016 EXPORT_SYMBOL(phy_init_eee
);
1019 * phy_get_eee_err - report the EEE wake error count
1020 * @phydev: target phy_device struct
1022 * Description: it is to report the number of time where the PHY
1023 * failed to complete its normal wake sequence.
1025 int phy_get_eee_err(struct phy_device
*phydev
)
1027 return phy_read_mmd_indirect(phydev
->bus
, MDIO_PCS_EEE_WK_ERR
,
1028 MDIO_MMD_PCS
, phydev
->addr
);
1030 EXPORT_SYMBOL(phy_get_eee_err
);
1033 * phy_ethtool_get_eee - get EEE supported and status
1034 * @phydev: target phy_device struct
1035 * @data: ethtool_eee data
1037 * Description: it reportes the Supported/Advertisement/LP Advertisement
1040 int phy_ethtool_get_eee(struct phy_device
*phydev
, struct ethtool_eee
*data
)
1044 /* Get Supported EEE */
1045 val
= phy_read_mmd_indirect(phydev
->bus
, MDIO_PCS_EEE_ABLE
,
1046 MDIO_MMD_PCS
, phydev
->addr
);
1049 data
->supported
= mmd_eee_cap_to_ethtool_sup_t(val
);
1051 /* Get advertisement EEE */
1052 val
= phy_read_mmd_indirect(phydev
->bus
, MDIO_AN_EEE_ADV
,
1053 MDIO_MMD_AN
, phydev
->addr
);
1056 data
->advertised
= mmd_eee_adv_to_ethtool_adv_t(val
);
1058 /* Get LP advertisement EEE */
1059 val
= phy_read_mmd_indirect(phydev
->bus
, MDIO_AN_EEE_LPABLE
,
1060 MDIO_MMD_AN
, phydev
->addr
);
1063 data
->lp_advertised
= mmd_eee_adv_to_ethtool_adv_t(val
);
1067 EXPORT_SYMBOL(phy_ethtool_get_eee
);
1070 * phy_ethtool_set_eee - set EEE supported and status
1071 * @phydev: target phy_device struct
1072 * @data: ethtool_eee data
1074 * Description: it is to program the Advertisement EEE register.
1076 int phy_ethtool_set_eee(struct phy_device
*phydev
, struct ethtool_eee
*data
)
1078 int val
= ethtool_adv_to_mmd_eee_adv_t(data
->advertised
);
1080 phy_write_mmd_indirect(phydev
->bus
, MDIO_AN_EEE_ADV
, MDIO_MMD_AN
,
1085 EXPORT_SYMBOL(phy_ethtool_set_eee
);
1087 int phy_ethtool_set_wol(struct phy_device
*phydev
, struct ethtool_wolinfo
*wol
)
1089 if (phydev
->drv
->set_wol
)
1090 return phydev
->drv
->set_wol(phydev
, wol
);
1094 EXPORT_SYMBOL(phy_ethtool_set_wol
);
1096 void phy_ethtool_get_wol(struct phy_device
*phydev
, struct ethtool_wolinfo
*wol
)
1098 if (phydev
->drv
->get_wol
)
1099 phydev
->drv
->get_wol(phydev
, wol
);
1101 EXPORT_SYMBOL(phy_ethtool_get_wol
);