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.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/config.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/unistd.h>
23 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
32 #include <linux/module.h>
33 #include <linux/version.h>
34 #include <linux/mii.h>
35 #include <linux/ethtool.h>
36 #include <linux/phy.h>
40 #include <asm/uaccess.h>
42 static void phy_timer(unsigned long data
);
44 /* Convenience function to print out the current phy status
46 void phy_print_status(struct phy_device
*phydev
)
48 pr_info("%s: Link is %s", phydev
->dev
.bus_id
,
49 phydev
->link
? "Up" : "Down");
51 printk(" - %d/%s", phydev
->speed
,
52 DUPLEX_FULL
== phydev
->duplex
?
57 EXPORT_SYMBOL(phy_print_status
);
60 /* Convenience functions for reading/writing a given PHY
61 * register. They MUST NOT be called from interrupt context,
62 * because the bus read/write functions may wait for an interrupt
63 * to conclude the operation. */
64 int phy_read(struct phy_device
*phydev
, u16 regnum
)
67 struct mii_bus
*bus
= phydev
->bus
;
69 spin_lock_bh(&bus
->mdio_lock
);
70 retval
= bus
->read(bus
, phydev
->addr
, regnum
);
71 spin_unlock_bh(&bus
->mdio_lock
);
75 EXPORT_SYMBOL(phy_read
);
77 int phy_write(struct phy_device
*phydev
, u16 regnum
, u16 val
)
80 struct mii_bus
*bus
= phydev
->bus
;
82 spin_lock_bh(&bus
->mdio_lock
);
83 err
= bus
->write(bus
, phydev
->addr
, regnum
, val
);
84 spin_unlock_bh(&bus
->mdio_lock
);
88 EXPORT_SYMBOL(phy_write
);
91 int phy_clear_interrupt(struct phy_device
*phydev
)
95 if (phydev
->drv
->ack_interrupt
)
96 err
= phydev
->drv
->ack_interrupt(phydev
);
102 int phy_config_interrupt(struct phy_device
*phydev
, u32 interrupts
)
106 phydev
->interrupts
= interrupts
;
107 if (phydev
->drv
->config_intr
)
108 err
= phydev
->drv
->config_intr(phydev
);
116 * description: Reads the status register and returns 0 either if
117 * auto-negotiation is incomplete, or if there was an error.
118 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
120 static inline int phy_aneg_done(struct phy_device
*phydev
)
124 retval
= phy_read(phydev
, MII_BMSR
);
126 return (retval
< 0) ? retval
: (retval
& BMSR_ANEGCOMPLETE
);
131 * description: Calls the PHY driver's config_aneg, and then
132 * sets the PHY state to PHY_AN if auto-negotiation is enabled,
133 * and to PHY_FORCING if auto-negotiation is disabled. Unless
134 * the PHY is currently HALTED.
136 int phy_start_aneg(struct phy_device
*phydev
)
140 spin_lock(&phydev
->lock
);
142 if (AUTONEG_DISABLE
== phydev
->autoneg
)
143 phy_sanitize_settings(phydev
);
145 err
= phydev
->drv
->config_aneg(phydev
);
150 if (phydev
->state
!= PHY_HALTED
) {
151 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
152 phydev
->state
= PHY_AN
;
153 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
155 phydev
->state
= PHY_FORCING
;
156 phydev
->link_timeout
= PHY_FORCE_TIMEOUT
;
161 spin_unlock(&phydev
->lock
);
164 EXPORT_SYMBOL(phy_start_aneg
);
167 /* A structure for mapping a particular speed and duplex
168 * combination to a particular SUPPORTED and ADVERTISED value */
175 /* A mapping of all SUPPORTED settings to speed/duplex */
176 static struct phy_setting settings
[] = {
179 .duplex
= DUPLEX_FULL
,
180 .setting
= SUPPORTED_10000baseT_Full
,
184 .duplex
= DUPLEX_FULL
,
185 .setting
= SUPPORTED_1000baseT_Full
,
189 .duplex
= DUPLEX_HALF
,
190 .setting
= SUPPORTED_1000baseT_Half
,
194 .duplex
= DUPLEX_FULL
,
195 .setting
= SUPPORTED_100baseT_Full
,
199 .duplex
= DUPLEX_HALF
,
200 .setting
= SUPPORTED_100baseT_Half
,
204 .duplex
= DUPLEX_FULL
,
205 .setting
= SUPPORTED_10baseT_Full
,
209 .duplex
= DUPLEX_HALF
,
210 .setting
= SUPPORTED_10baseT_Half
,
214 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
218 * description: Searches the settings array for the setting which
219 * matches the desired speed and duplex, and returns the index
220 * of that setting. Returns the index of the last setting if
221 * none of the others match.
223 static inline int phy_find_setting(int speed
, int duplex
)
227 while (idx
< ARRAY_SIZE(settings
) &&
228 (settings
[idx
].speed
!= speed
||
229 settings
[idx
].duplex
!= duplex
))
232 return idx
< MAX_NUM_SETTINGS
? idx
: MAX_NUM_SETTINGS
- 1;
236 * idx: The first index in settings[] to search
237 * features: A mask of the valid settings
239 * description: Returns the index of the first valid setting less
240 * than or equal to the one pointed to by idx, as determined by
241 * the mask in features. Returns the index of the last setting
242 * if nothing else matches.
244 static inline int phy_find_valid(int idx
, u32 features
)
246 while (idx
< MAX_NUM_SETTINGS
&& !(settings
[idx
].setting
& features
))
249 return idx
< MAX_NUM_SETTINGS
? idx
: MAX_NUM_SETTINGS
- 1;
252 /* phy_sanitize_settings
254 * description: Make sure the PHY is set to supported speeds and
255 * duplexes. Drop down by one in this order: 1000/FULL,
256 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
258 void phy_sanitize_settings(struct phy_device
*phydev
)
260 u32 features
= phydev
->supported
;
263 /* Sanitize settings based on PHY capabilities */
264 if ((features
& SUPPORTED_Autoneg
) == 0)
267 idx
= phy_find_valid(phy_find_setting(phydev
->speed
, phydev
->duplex
),
270 phydev
->speed
= settings
[idx
].speed
;
271 phydev
->duplex
= settings
[idx
].duplex
;
273 EXPORT_SYMBOL(phy_sanitize_settings
);
275 /* phy_force_reduction
277 * description: Reduces the speed/duplex settings by
278 * one notch. The order is so:
279 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
280 * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
282 static void phy_force_reduction(struct phy_device
*phydev
)
286 idx
= phy_find_setting(phydev
->speed
, phydev
->duplex
);
290 idx
= phy_find_valid(idx
, phydev
->supported
);
292 phydev
->speed
= settings
[idx
].speed
;
293 phydev
->duplex
= settings
[idx
].duplex
;
295 pr_info("Trying %d/%s\n", phydev
->speed
,
296 DUPLEX_FULL
== phydev
->duplex
?
301 * A generic ethtool sset function. Handles all the details
303 * A few notes about parameter checking:
304 * - We don't set port or transceiver, so we don't care what they
306 * - phy_start_aneg() will make sure forced settings are sane, and
307 * choose the next best ones from the ones selected, so we don't
308 * care if ethtool tries to give us bad values
310 int phy_ethtool_sset(struct phy_device
*phydev
, struct ethtool_cmd
*cmd
)
312 if (cmd
->phy_address
!= phydev
->addr
)
315 /* We make sure that we don't pass unsupported
316 * values in to the PHY */
317 cmd
->advertising
&= phydev
->supported
;
319 /* Verify the settings we care about. */
320 if (cmd
->autoneg
!= AUTONEG_ENABLE
&& cmd
->autoneg
!= AUTONEG_DISABLE
)
323 if (cmd
->autoneg
== AUTONEG_ENABLE
&& cmd
->advertising
== 0)
326 if (cmd
->autoneg
== AUTONEG_DISABLE
327 && ((cmd
->speed
!= SPEED_1000
328 && cmd
->speed
!= SPEED_100
329 && cmd
->speed
!= SPEED_10
)
330 || (cmd
->duplex
!= DUPLEX_HALF
331 && cmd
->duplex
!= DUPLEX_FULL
)))
334 phydev
->autoneg
= cmd
->autoneg
;
336 phydev
->speed
= cmd
->speed
;
338 phydev
->advertising
= cmd
->advertising
;
340 if (AUTONEG_ENABLE
== cmd
->autoneg
)
341 phydev
->advertising
|= ADVERTISED_Autoneg
;
343 phydev
->advertising
&= ~ADVERTISED_Autoneg
;
345 phydev
->duplex
= cmd
->duplex
;
347 /* Restart the PHY */
348 phy_start_aneg(phydev
);
353 int phy_ethtool_gset(struct phy_device
*phydev
, struct ethtool_cmd
*cmd
)
355 cmd
->supported
= phydev
->supported
;
357 cmd
->advertising
= phydev
->advertising
;
359 cmd
->speed
= phydev
->speed
;
360 cmd
->duplex
= phydev
->duplex
;
361 cmd
->port
= PORT_MII
;
362 cmd
->phy_address
= phydev
->addr
;
363 cmd
->transceiver
= XCVR_EXTERNAL
;
364 cmd
->autoneg
= phydev
->autoneg
;
370 /* Note that this function is currently incompatible with the
371 * PHYCONTROL layer. It changes registers without regard to
372 * current state. Use at own risk
374 int phy_mii_ioctl(struct phy_device
*phydev
,
375 struct mii_ioctl_data
*mii_data
, int cmd
)
377 u16 val
= mii_data
->val_in
;
381 mii_data
->phy_id
= phydev
->addr
;
384 mii_data
->val_out
= phy_read(phydev
, mii_data
->reg_num
);
388 if (!capable(CAP_NET_ADMIN
))
391 if (mii_data
->phy_id
== phydev
->addr
) {
392 switch(mii_data
->reg_num
) {
394 if (val
& (BMCR_RESET
|BMCR_ANENABLE
))
395 phydev
->autoneg
= AUTONEG_DISABLE
;
397 phydev
->autoneg
= AUTONEG_ENABLE
;
398 if ((!phydev
->autoneg
) && (val
& BMCR_FULLDPLX
))
399 phydev
->duplex
= DUPLEX_FULL
;
401 phydev
->duplex
= DUPLEX_HALF
;
404 phydev
->advertising
= val
;
412 phy_write(phydev
, mii_data
->reg_num
, val
);
414 if (mii_data
->reg_num
== MII_BMCR
416 && phydev
->drv
->config_init
)
417 phydev
->drv
->config_init(phydev
);
424 /* phy_start_machine:
426 * description: The PHY infrastructure can run a state machine
427 * which tracks whether the PHY is starting up, negotiating,
428 * etc. This function starts the timer which tracks the state
429 * of the PHY. If you want to be notified when the state
430 * changes, pass in the callback, otherwise, pass NULL. If you
431 * want to maintain your own state machine, do not call this
433 void phy_start_machine(struct phy_device
*phydev
,
434 void (*handler
)(struct net_device
*))
436 phydev
->adjust_state
= handler
;
438 init_timer(&phydev
->phy_timer
);
439 phydev
->phy_timer
.function
= &phy_timer
;
440 phydev
->phy_timer
.data
= (unsigned long) phydev
;
441 mod_timer(&phydev
->phy_timer
, jiffies
+ HZ
);
446 * description: Stops the state machine timer, sets the state to
447 * UP (unless it wasn't up yet), and then frees the interrupt,
448 * if it is in use. This function must be called BEFORE
451 void phy_stop_machine(struct phy_device
*phydev
)
453 del_timer_sync(&phydev
->phy_timer
);
455 spin_lock(&phydev
->lock
);
456 if (phydev
->state
> PHY_UP
)
457 phydev
->state
= PHY_UP
;
458 spin_unlock(&phydev
->lock
);
460 if (phydev
->irq
!= PHY_POLL
)
461 phy_stop_interrupts(phydev
);
463 phydev
->adjust_state
= NULL
;
468 * Moves the PHY to the HALTED state in response to a read
469 * or write error, and tells the controller the link is down.
470 * Must not be called from interrupt context, or while the
471 * phydev->lock is held.
473 void phy_error(struct phy_device
*phydev
)
475 spin_lock(&phydev
->lock
);
476 phydev
->state
= PHY_HALTED
;
477 spin_unlock(&phydev
->lock
);
480 #ifdef CONFIG_PHYCONTROL
482 static void phy_change(void *data
);
486 * description: When a PHY interrupt occurs, the handler disables
487 * interrupts, and schedules a work task to clear the interrupt.
489 static irqreturn_t
phy_interrupt(int irq
, void *phy_dat
, struct pt_regs
*regs
)
491 struct phy_device
*phydev
= phy_dat
;
493 /* The MDIO bus is not allowed to be written in interrupt
494 * context, so we need to disable the irq here. A work
495 * queue will write the PHY to disable and clear the
496 * interrupt, and then reenable the irq line. */
497 disable_irq_nosync(irq
);
499 schedule_work(&phydev
->phy_queue
);
504 /* Enable the interrupts from the PHY side */
505 int phy_enable_interrupts(struct phy_device
*phydev
)
509 err
= phy_clear_interrupt(phydev
);
514 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_ENABLED
);
518 EXPORT_SYMBOL(phy_enable_interrupts
);
520 /* Disable the PHY interrupts from the PHY side */
521 int phy_disable_interrupts(struct phy_device
*phydev
)
525 /* Disable PHY interrupts */
526 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_DISABLED
);
531 /* Clear the interrupt */
532 err
= phy_clear_interrupt(phydev
);
544 EXPORT_SYMBOL(phy_disable_interrupts
);
546 /* phy_start_interrupts
548 * description: Request the interrupt for the given PHY. If
549 * this fails, then we set irq to PHY_POLL.
550 * Otherwise, we enable the interrupts in the PHY.
551 * Returns 0 on success.
552 * This should only be called with a valid IRQ number.
554 int phy_start_interrupts(struct phy_device
*phydev
)
558 INIT_WORK(&phydev
->phy_queue
, phy_change
, phydev
);
560 if (request_irq(phydev
->irq
, phy_interrupt
,
564 printk(KERN_WARNING
"%s: Can't get IRQ %d (PHY)\n",
567 phydev
->irq
= PHY_POLL
;
571 err
= phy_enable_interrupts(phydev
);
575 EXPORT_SYMBOL(phy_start_interrupts
);
577 int phy_stop_interrupts(struct phy_device
*phydev
)
581 err
= phy_disable_interrupts(phydev
);
586 free_irq(phydev
->irq
, phydev
);
590 EXPORT_SYMBOL(phy_stop_interrupts
);
593 /* Scheduled by the phy_interrupt/timer to handle PHY changes */
594 static void phy_change(void *data
)
597 struct phy_device
*phydev
= data
;
599 err
= phy_disable_interrupts(phydev
);
604 spin_lock(&phydev
->lock
);
605 if ((PHY_RUNNING
== phydev
->state
) || (PHY_NOLINK
== phydev
->state
))
606 phydev
->state
= PHY_CHANGELINK
;
607 spin_unlock(&phydev
->lock
);
609 enable_irq(phydev
->irq
);
611 /* Reenable interrupts */
612 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_ENABLED
);
620 disable_irq(phydev
->irq
);
625 /* Bring down the PHY link, and stop checking the status. */
626 void phy_stop(struct phy_device
*phydev
)
628 spin_lock(&phydev
->lock
);
630 if (PHY_HALTED
== phydev
->state
)
633 if (phydev
->irq
!= PHY_POLL
) {
634 /* Clear any pending interrupts */
635 phy_clear_interrupt(phydev
);
637 /* Disable PHY Interrupts */
638 phy_config_interrupt(phydev
, PHY_INTERRUPT_DISABLED
);
641 phydev
->state
= PHY_HALTED
;
644 spin_unlock(&phydev
->lock
);
650 * description: Indicates the attached device's readiness to
651 * handle PHY-related work. Used during startup to start the
652 * PHY, and after a call to phy_stop() to resume operation.
653 * Also used to indicate the MDIO bus has cleared an error
656 void phy_start(struct phy_device
*phydev
)
658 spin_lock(&phydev
->lock
);
660 switch (phydev
->state
) {
662 phydev
->state
= PHY_PENDING
;
665 phydev
->state
= PHY_UP
;
668 phydev
->state
= PHY_RESUMING
;
672 spin_unlock(&phydev
->lock
);
674 EXPORT_SYMBOL(phy_stop
);
675 EXPORT_SYMBOL(phy_start
);
677 #endif /* CONFIG_PHYCONTROL */
679 /* PHY timer which handles the state machine */
680 static void phy_timer(unsigned long data
)
682 struct phy_device
*phydev
= (struct phy_device
*)data
;
686 spin_lock(&phydev
->lock
);
688 if (phydev
->adjust_state
)
689 phydev
->adjust_state(phydev
->attached_dev
);
691 switch(phydev
->state
) {
700 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
704 /* Check if negotiation is done. Break
705 * if there's an error */
706 err
= phy_aneg_done(phydev
);
710 /* If auto-negotiation is done, we change to
711 * either RUNNING, or NOLINK */
713 err
= phy_read_status(phydev
);
719 phydev
->state
= PHY_RUNNING
;
720 netif_carrier_on(phydev
->attached_dev
);
722 phydev
->state
= PHY_NOLINK
;
723 netif_carrier_off(phydev
->attached_dev
);
726 phydev
->adjust_link(phydev
->attached_dev
);
728 } else if (0 == phydev
->link_timeout
--) {
729 /* The counter expired, so either we
730 * switch to forced mode, or the
731 * magic_aneg bit exists, and we try aneg
733 if (!(phydev
->drv
->flags
& PHY_HAS_MAGICANEG
)) {
736 /* We'll start from the
737 * fastest speed, and work
739 idx
= phy_find_valid(0,
742 phydev
->speed
= settings
[idx
].speed
;
743 phydev
->duplex
= settings
[idx
].duplex
;
745 phydev
->autoneg
= AUTONEG_DISABLE
;
746 phydev
->state
= PHY_FORCING
;
747 phydev
->link_timeout
=
750 pr_info("Trying %d/%s\n",
761 err
= phy_read_status(phydev
);
767 phydev
->state
= PHY_RUNNING
;
768 netif_carrier_on(phydev
->attached_dev
);
769 phydev
->adjust_link(phydev
->attached_dev
);
773 err
= phy_read_status(phydev
);
779 phydev
->state
= PHY_RUNNING
;
780 netif_carrier_on(phydev
->attached_dev
);
782 if (0 == phydev
->link_timeout
--) {
783 phy_force_reduction(phydev
);
788 phydev
->adjust_link(phydev
->attached_dev
);
791 /* Only register a CHANGE if we are
793 if (PHY_POLL
== phydev
->irq
)
794 phydev
->state
= PHY_CHANGELINK
;
797 err
= phy_read_status(phydev
);
803 phydev
->state
= PHY_RUNNING
;
804 netif_carrier_on(phydev
->attached_dev
);
806 phydev
->state
= PHY_NOLINK
;
807 netif_carrier_off(phydev
->attached_dev
);
810 phydev
->adjust_link(phydev
->attached_dev
);
812 if (PHY_POLL
!= phydev
->irq
)
813 err
= phy_config_interrupt(phydev
,
814 PHY_INTERRUPT_ENABLED
);
819 netif_carrier_off(phydev
->attached_dev
);
820 phydev
->adjust_link(phydev
->attached_dev
);
825 err
= phy_clear_interrupt(phydev
);
830 err
= phy_config_interrupt(phydev
,
831 PHY_INTERRUPT_ENABLED
);
836 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
837 err
= phy_aneg_done(phydev
);
841 /* err > 0 if AN is done.
842 * Otherwise, it's 0, and we're
843 * still waiting for AN */
845 phydev
->state
= PHY_RUNNING
;
847 phydev
->state
= PHY_AN
;
848 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
851 phydev
->state
= PHY_RUNNING
;
855 spin_unlock(&phydev
->lock
);
858 err
= phy_start_aneg(phydev
);
863 mod_timer(&phydev
->phy_timer
, jiffies
+ PHY_STATE_TIME
* HZ
);