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 /* Convenience function to print out the current phy status
44 void phy_print_status(struct phy_device
*phydev
)
46 pr_info("%s: Link is %s", phydev
->dev
.bus_id
,
47 phydev
->link
? "Up" : "Down");
49 printk(" - %d/%s", phydev
->speed
,
50 DUPLEX_FULL
== phydev
->duplex
?
55 EXPORT_SYMBOL(phy_print_status
);
58 /* Convenience functions for reading/writing a given PHY
59 * register. They MUST NOT be called from interrupt context,
60 * because the bus read/write functions may wait for an interrupt
61 * to conclude the operation. */
62 int phy_read(struct phy_device
*phydev
, u16 regnum
)
65 struct mii_bus
*bus
= phydev
->bus
;
67 spin_lock_bh(&bus
->mdio_lock
);
68 retval
= bus
->read(bus
, phydev
->addr
, regnum
);
69 spin_unlock_bh(&bus
->mdio_lock
);
73 EXPORT_SYMBOL(phy_read
);
75 int phy_write(struct phy_device
*phydev
, u16 regnum
, u16 val
)
78 struct mii_bus
*bus
= phydev
->bus
;
80 spin_lock_bh(&bus
->mdio_lock
);
81 err
= bus
->write(bus
, phydev
->addr
, regnum
, val
);
82 spin_unlock_bh(&bus
->mdio_lock
);
86 EXPORT_SYMBOL(phy_write
);
89 int phy_clear_interrupt(struct phy_device
*phydev
)
93 if (phydev
->drv
->ack_interrupt
)
94 err
= phydev
->drv
->ack_interrupt(phydev
);
100 int phy_config_interrupt(struct phy_device
*phydev
, u32 interrupts
)
104 phydev
->interrupts
= interrupts
;
105 if (phydev
->drv
->config_intr
)
106 err
= phydev
->drv
->config_intr(phydev
);
114 * description: Reads the status register and returns 0 either if
115 * auto-negotiation is incomplete, or if there was an error.
116 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
118 static inline int phy_aneg_done(struct phy_device
*phydev
)
122 retval
= phy_read(phydev
, MII_BMSR
);
124 return (retval
< 0) ? retval
: (retval
& BMSR_ANEGCOMPLETE
);
127 /* A structure for mapping a particular speed and duplex
128 * combination to a particular SUPPORTED and ADVERTISED value */
135 /* A mapping of all SUPPORTED settings to speed/duplex */
136 static struct phy_setting settings
[] = {
139 .duplex
= DUPLEX_FULL
,
140 .setting
= SUPPORTED_10000baseT_Full
,
144 .duplex
= DUPLEX_FULL
,
145 .setting
= SUPPORTED_1000baseT_Full
,
149 .duplex
= DUPLEX_HALF
,
150 .setting
= SUPPORTED_1000baseT_Half
,
154 .duplex
= DUPLEX_FULL
,
155 .setting
= SUPPORTED_100baseT_Full
,
159 .duplex
= DUPLEX_HALF
,
160 .setting
= SUPPORTED_100baseT_Half
,
164 .duplex
= DUPLEX_FULL
,
165 .setting
= SUPPORTED_10baseT_Full
,
169 .duplex
= DUPLEX_HALF
,
170 .setting
= SUPPORTED_10baseT_Half
,
174 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
178 * description: Searches the settings array for the setting which
179 * matches the desired speed and duplex, and returns the index
180 * of that setting. Returns the index of the last setting if
181 * none of the others match.
183 static inline int phy_find_setting(int speed
, int duplex
)
187 while (idx
< ARRAY_SIZE(settings
) &&
188 (settings
[idx
].speed
!= speed
||
189 settings
[idx
].duplex
!= duplex
))
192 return idx
< MAX_NUM_SETTINGS
? idx
: MAX_NUM_SETTINGS
- 1;
196 * idx: The first index in settings[] to search
197 * features: A mask of the valid settings
199 * description: Returns the index of the first valid setting less
200 * than or equal to the one pointed to by idx, as determined by
201 * the mask in features. Returns the index of the last setting
202 * if nothing else matches.
204 static inline int phy_find_valid(int idx
, u32 features
)
206 while (idx
< MAX_NUM_SETTINGS
&& !(settings
[idx
].setting
& features
))
209 return idx
< MAX_NUM_SETTINGS
? idx
: MAX_NUM_SETTINGS
- 1;
212 /* phy_sanitize_settings
214 * description: Make sure the PHY is set to supported speeds and
215 * duplexes. Drop down by one in this order: 1000/FULL,
216 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
218 void phy_sanitize_settings(struct phy_device
*phydev
)
220 u32 features
= phydev
->supported
;
223 /* Sanitize settings based on PHY capabilities */
224 if ((features
& SUPPORTED_Autoneg
) == 0)
227 idx
= phy_find_valid(phy_find_setting(phydev
->speed
, phydev
->duplex
),
230 phydev
->speed
= settings
[idx
].speed
;
231 phydev
->duplex
= settings
[idx
].duplex
;
233 EXPORT_SYMBOL(phy_sanitize_settings
);
236 * A generic ethtool sset function. Handles all the details
238 * A few notes about parameter checking:
239 * - We don't set port or transceiver, so we don't care what they
241 * - phy_start_aneg() will make sure forced settings are sane, and
242 * choose the next best ones from the ones selected, so we don't
243 * care if ethtool tries to give us bad values
245 * A note about the PHYCONTROL Layer. If you turn off
246 * CONFIG_PHYCONTROL, you will need to read the PHY status
247 * registers after this function completes, and update your
248 * controller manually.
250 int phy_ethtool_sset(struct phy_device
*phydev
, struct ethtool_cmd
*cmd
)
252 if (cmd
->phy_address
!= phydev
->addr
)
255 /* We make sure that we don't pass unsupported
256 * values in to the PHY */
257 cmd
->advertising
&= phydev
->supported
;
259 /* Verify the settings we care about. */
260 if (cmd
->autoneg
!= AUTONEG_ENABLE
&& cmd
->autoneg
!= AUTONEG_DISABLE
)
263 if (cmd
->autoneg
== AUTONEG_ENABLE
&& cmd
->advertising
== 0)
266 if (cmd
->autoneg
== AUTONEG_DISABLE
267 && ((cmd
->speed
!= SPEED_1000
268 && cmd
->speed
!= SPEED_100
269 && cmd
->speed
!= SPEED_10
)
270 || (cmd
->duplex
!= DUPLEX_HALF
271 && cmd
->duplex
!= DUPLEX_FULL
)))
274 phydev
->autoneg
= cmd
->autoneg
;
276 phydev
->speed
= cmd
->speed
;
278 phydev
->advertising
= cmd
->advertising
;
280 if (AUTONEG_ENABLE
== cmd
->autoneg
)
281 phydev
->advertising
|= ADVERTISED_Autoneg
;
283 phydev
->advertising
&= ~ADVERTISED_Autoneg
;
285 phydev
->duplex
= cmd
->duplex
;
287 /* Restart the PHY */
288 phy_start_aneg(phydev
);
293 int phy_ethtool_gset(struct phy_device
*phydev
, struct ethtool_cmd
*cmd
)
295 cmd
->supported
= phydev
->supported
;
297 cmd
->advertising
= phydev
->advertising
;
299 cmd
->speed
= phydev
->speed
;
300 cmd
->duplex
= phydev
->duplex
;
301 cmd
->port
= PORT_MII
;
302 cmd
->phy_address
= phydev
->addr
;
303 cmd
->transceiver
= XCVR_EXTERNAL
;
304 cmd
->autoneg
= phydev
->autoneg
;
310 /* Note that this function is currently incompatible with the
311 * PHYCONTROL layer. It changes registers without regard to
312 * current state. Use at own risk
314 int phy_mii_ioctl(struct phy_device
*phydev
,
315 struct mii_ioctl_data
*mii_data
, int cmd
)
317 u16 val
= mii_data
->val_in
;
321 mii_data
->phy_id
= phydev
->addr
;
324 mii_data
->val_out
= phy_read(phydev
, mii_data
->reg_num
);
328 if (!capable(CAP_NET_ADMIN
))
331 if (mii_data
->phy_id
== phydev
->addr
) {
332 switch(mii_data
->reg_num
) {
334 if (val
& (BMCR_RESET
|BMCR_ANENABLE
))
335 phydev
->autoneg
= AUTONEG_DISABLE
;
337 phydev
->autoneg
= AUTONEG_ENABLE
;
338 if ((!phydev
->autoneg
) && (val
& BMCR_FULLDPLX
))
339 phydev
->duplex
= DUPLEX_FULL
;
341 phydev
->duplex
= DUPLEX_HALF
;
344 phydev
->advertising
= val
;
352 phy_write(phydev
, mii_data
->reg_num
, val
);
354 if (mii_data
->reg_num
== MII_BMCR
356 && phydev
->drv
->config_init
)
357 phydev
->drv
->config_init(phydev
);
366 * description: Sanitizes the settings (if we're not
367 * autonegotiating them), and then calls the driver's
368 * config_aneg function. If the PHYCONTROL Layer is operating,
369 * we change the state to reflect the beginning of
370 * Auto-negotiation or forcing.
372 int phy_start_aneg(struct phy_device
*phydev
)
376 spin_lock(&phydev
->lock
);
378 if (AUTONEG_DISABLE
== phydev
->autoneg
)
379 phy_sanitize_settings(phydev
);
381 err
= phydev
->drv
->config_aneg(phydev
);
383 #ifdef CONFIG_PHYCONTROL
387 if (phydev
->state
!= PHY_HALTED
) {
388 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
389 phydev
->state
= PHY_AN
;
390 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
392 phydev
->state
= PHY_FORCING
;
393 phydev
->link_timeout
= PHY_FORCE_TIMEOUT
;
399 spin_unlock(&phydev
->lock
);
402 EXPORT_SYMBOL(phy_start_aneg
);
405 #ifdef CONFIG_PHYCONTROL
406 static void phy_change(void *data
);
407 static void phy_timer(unsigned long data
);
409 /* phy_start_machine:
411 * description: The PHY infrastructure can run a state machine
412 * which tracks whether the PHY is starting up, negotiating,
413 * etc. This function starts the timer which tracks the state
414 * of the PHY. If you want to be notified when the state
415 * changes, pass in the callback, otherwise, pass NULL. If you
416 * want to maintain your own state machine, do not call this
418 void phy_start_machine(struct phy_device
*phydev
,
419 void (*handler
)(struct net_device
*))
421 phydev
->adjust_state
= handler
;
423 init_timer(&phydev
->phy_timer
);
424 phydev
->phy_timer
.function
= &phy_timer
;
425 phydev
->phy_timer
.data
= (unsigned long) phydev
;
426 mod_timer(&phydev
->phy_timer
, jiffies
+ HZ
);
431 * description: Stops the state machine timer, sets the state to
432 * UP (unless it wasn't up yet), and then frees the interrupt,
433 * if it is in use. This function must be called BEFORE
436 void phy_stop_machine(struct phy_device
*phydev
)
438 del_timer_sync(&phydev
->phy_timer
);
440 spin_lock(&phydev
->lock
);
441 if (phydev
->state
> PHY_UP
)
442 phydev
->state
= PHY_UP
;
443 spin_unlock(&phydev
->lock
);
445 if (phydev
->irq
!= PHY_POLL
)
446 phy_stop_interrupts(phydev
);
448 phydev
->adjust_state
= NULL
;
451 /* phy_force_reduction
453 * description: Reduces the speed/duplex settings by
454 * one notch. The order is so:
455 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
456 * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
458 static void phy_force_reduction(struct phy_device
*phydev
)
462 idx
= phy_find_setting(phydev
->speed
, phydev
->duplex
);
466 idx
= phy_find_valid(idx
, phydev
->supported
);
468 phydev
->speed
= settings
[idx
].speed
;
469 phydev
->duplex
= settings
[idx
].duplex
;
471 pr_info("Trying %d/%s\n", phydev
->speed
,
472 DUPLEX_FULL
== phydev
->duplex
?
479 * Moves the PHY to the HALTED state in response to a read
480 * or write error, and tells the controller the link is down.
481 * Must not be called from interrupt context, or while the
482 * phydev->lock is held.
484 void phy_error(struct phy_device
*phydev
)
486 spin_lock(&phydev
->lock
);
487 phydev
->state
= PHY_HALTED
;
488 spin_unlock(&phydev
->lock
);
493 * description: When a PHY interrupt occurs, the handler disables
494 * interrupts, and schedules a work task to clear the interrupt.
496 static irqreturn_t
phy_interrupt(int irq
, void *phy_dat
, struct pt_regs
*regs
)
498 struct phy_device
*phydev
= phy_dat
;
500 /* The MDIO bus is not allowed to be written in interrupt
501 * context, so we need to disable the irq here. A work
502 * queue will write the PHY to disable and clear the
503 * interrupt, and then reenable the irq line. */
504 disable_irq_nosync(irq
);
506 schedule_work(&phydev
->phy_queue
);
511 /* Enable the interrupts from the PHY side */
512 int phy_enable_interrupts(struct phy_device
*phydev
)
516 err
= phy_clear_interrupt(phydev
);
521 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_ENABLED
);
525 EXPORT_SYMBOL(phy_enable_interrupts
);
527 /* Disable the PHY interrupts from the PHY side */
528 int phy_disable_interrupts(struct phy_device
*phydev
)
532 /* Disable PHY interrupts */
533 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_DISABLED
);
538 /* Clear the interrupt */
539 err
= phy_clear_interrupt(phydev
);
551 EXPORT_SYMBOL(phy_disable_interrupts
);
553 /* phy_start_interrupts
555 * description: Request the interrupt for the given PHY. If
556 * this fails, then we set irq to PHY_POLL.
557 * Otherwise, we enable the interrupts in the PHY.
558 * Returns 0 on success.
559 * This should only be called with a valid IRQ number.
561 int phy_start_interrupts(struct phy_device
*phydev
)
565 INIT_WORK(&phydev
->phy_queue
, phy_change
, phydev
);
567 if (request_irq(phydev
->irq
, phy_interrupt
,
571 printk(KERN_WARNING
"%s: Can't get IRQ %d (PHY)\n",
574 phydev
->irq
= PHY_POLL
;
578 err
= phy_enable_interrupts(phydev
);
582 EXPORT_SYMBOL(phy_start_interrupts
);
584 int phy_stop_interrupts(struct phy_device
*phydev
)
588 err
= phy_disable_interrupts(phydev
);
593 free_irq(phydev
->irq
, phydev
);
597 EXPORT_SYMBOL(phy_stop_interrupts
);
600 /* Scheduled by the phy_interrupt/timer to handle PHY changes */
601 static void phy_change(void *data
)
604 struct phy_device
*phydev
= data
;
606 err
= phy_disable_interrupts(phydev
);
611 spin_lock(&phydev
->lock
);
612 if ((PHY_RUNNING
== phydev
->state
) || (PHY_NOLINK
== phydev
->state
))
613 phydev
->state
= PHY_CHANGELINK
;
614 spin_unlock(&phydev
->lock
);
616 enable_irq(phydev
->irq
);
618 /* Reenable interrupts */
619 err
= phy_config_interrupt(phydev
, PHY_INTERRUPT_ENABLED
);
627 disable_irq(phydev
->irq
);
632 /* Bring down the PHY link, and stop checking the status. */
633 void phy_stop(struct phy_device
*phydev
)
635 spin_lock(&phydev
->lock
);
637 if (PHY_HALTED
== phydev
->state
)
640 if (phydev
->irq
!= PHY_POLL
) {
641 /* Clear any pending interrupts */
642 phy_clear_interrupt(phydev
);
644 /* Disable PHY Interrupts */
645 phy_config_interrupt(phydev
, PHY_INTERRUPT_DISABLED
);
648 phydev
->state
= PHY_HALTED
;
651 spin_unlock(&phydev
->lock
);
657 * description: Indicates the attached device's readiness to
658 * handle PHY-related work. Used during startup to start the
659 * PHY, and after a call to phy_stop() to resume operation.
660 * Also used to indicate the MDIO bus has cleared an error
663 void phy_start(struct phy_device
*phydev
)
665 spin_lock(&phydev
->lock
);
667 switch (phydev
->state
) {
669 phydev
->state
= PHY_PENDING
;
672 phydev
->state
= PHY_UP
;
675 phydev
->state
= PHY_RESUMING
;
679 spin_unlock(&phydev
->lock
);
681 EXPORT_SYMBOL(phy_stop
);
682 EXPORT_SYMBOL(phy_start
);
684 /* PHY timer which handles the state machine */
685 static void phy_timer(unsigned long data
)
687 struct phy_device
*phydev
= (struct phy_device
*)data
;
691 spin_lock(&phydev
->lock
);
693 if (phydev
->adjust_state
)
694 phydev
->adjust_state(phydev
->attached_dev
);
696 switch(phydev
->state
) {
705 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
709 /* Check if negotiation is done. Break
710 * if there's an error */
711 err
= phy_aneg_done(phydev
);
715 /* If auto-negotiation is done, we change to
716 * either RUNNING, or NOLINK */
718 err
= phy_read_status(phydev
);
724 phydev
->state
= PHY_RUNNING
;
725 netif_carrier_on(phydev
->attached_dev
);
727 phydev
->state
= PHY_NOLINK
;
728 netif_carrier_off(phydev
->attached_dev
);
731 phydev
->adjust_link(phydev
->attached_dev
);
733 } else if (0 == phydev
->link_timeout
--) {
734 /* The counter expired, so either we
735 * switch to forced mode, or the
736 * magic_aneg bit exists, and we try aneg
738 if (!(phydev
->drv
->flags
& PHY_HAS_MAGICANEG
)) {
741 /* We'll start from the
742 * fastest speed, and work
744 idx
= phy_find_valid(0,
747 phydev
->speed
= settings
[idx
].speed
;
748 phydev
->duplex
= settings
[idx
].duplex
;
750 phydev
->autoneg
= AUTONEG_DISABLE
;
751 phydev
->state
= PHY_FORCING
;
752 phydev
->link_timeout
=
755 pr_info("Trying %d/%s\n",
766 err
= phy_read_status(phydev
);
772 phydev
->state
= PHY_RUNNING
;
773 netif_carrier_on(phydev
->attached_dev
);
774 phydev
->adjust_link(phydev
->attached_dev
);
778 err
= phy_read_status(phydev
);
784 phydev
->state
= PHY_RUNNING
;
785 netif_carrier_on(phydev
->attached_dev
);
787 if (0 == phydev
->link_timeout
--) {
788 phy_force_reduction(phydev
);
793 phydev
->adjust_link(phydev
->attached_dev
);
796 /* Only register a CHANGE if we are
798 if (PHY_POLL
== phydev
->irq
)
799 phydev
->state
= PHY_CHANGELINK
;
802 err
= phy_read_status(phydev
);
808 phydev
->state
= PHY_RUNNING
;
809 netif_carrier_on(phydev
->attached_dev
);
811 phydev
->state
= PHY_NOLINK
;
812 netif_carrier_off(phydev
->attached_dev
);
815 phydev
->adjust_link(phydev
->attached_dev
);
817 if (PHY_POLL
!= phydev
->irq
)
818 err
= phy_config_interrupt(phydev
,
819 PHY_INTERRUPT_ENABLED
);
824 netif_carrier_off(phydev
->attached_dev
);
825 phydev
->adjust_link(phydev
->attached_dev
);
830 err
= phy_clear_interrupt(phydev
);
835 err
= phy_config_interrupt(phydev
,
836 PHY_INTERRUPT_ENABLED
);
841 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
842 err
= phy_aneg_done(phydev
);
846 /* err > 0 if AN is done.
847 * Otherwise, it's 0, and we're
848 * still waiting for AN */
850 phydev
->state
= PHY_RUNNING
;
852 phydev
->state
= PHY_AN
;
853 phydev
->link_timeout
= PHY_AN_TIMEOUT
;
856 phydev
->state
= PHY_RUNNING
;
860 spin_unlock(&phydev
->lock
);
863 err
= phy_start_aneg(phydev
);
868 mod_timer(&phydev
->phy_timer
, jiffies
+ PHY_STATE_TIME
* HZ
);
871 #endif /* CONFIG_PHYCONTROL */