2 * Generic PHY Management code
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * Copyright 2011 Freescale Semiconductor, Inc.
23 * Based loosely off of Linux's PHY Lib
34 #include <linux/err.h>
36 /* Generic PHY support and helper functions */
39 * genphy_config_advert - sanitize and advertise auto-negotation parameters
40 * @phydev: target phy_device struct
42 * Description: Writes MII_ADVERTISE with the appropriate values,
43 * after sanitizing the values to make sure we only advertise
44 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
45 * hasn't changed, and > 0 if it has changed.
47 static int genphy_config_advert(struct phy_device
*phydev
)
53 /* Only allow advertising what
54 * this PHY supports */
55 phydev
->advertising
&= phydev
->supported
;
56 advertise
= phydev
->advertising
;
58 /* Setup standard advertisement */
59 oldadv
= adv
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_ADVERTISE
);
64 adv
&= ~(ADVERTISE_ALL
| ADVERTISE_100BASE4
| ADVERTISE_PAUSE_CAP
|
65 ADVERTISE_PAUSE_ASYM
);
66 if (advertise
& ADVERTISED_10baseT_Half
)
67 adv
|= ADVERTISE_10HALF
;
68 if (advertise
& ADVERTISED_10baseT_Full
)
69 adv
|= ADVERTISE_10FULL
;
70 if (advertise
& ADVERTISED_100baseT_Half
)
71 adv
|= ADVERTISE_100HALF
;
72 if (advertise
& ADVERTISED_100baseT_Full
)
73 adv
|= ADVERTISE_100FULL
;
74 if (advertise
& ADVERTISED_Pause
)
75 adv
|= ADVERTISE_PAUSE_CAP
;
76 if (advertise
& ADVERTISED_Asym_Pause
)
77 adv
|= ADVERTISE_PAUSE_ASYM
;
78 if (advertise
& ADVERTISED_1000baseX_Half
)
79 adv
|= ADVERTISE_1000XHALF
;
80 if (advertise
& ADVERTISED_1000baseX_Full
)
81 adv
|= ADVERTISE_1000XFULL
;
84 err
= phy_write(phydev
, MDIO_DEVAD_NONE
, MII_ADVERTISE
, adv
);
91 /* Configure gigabit if it's supported */
92 if (phydev
->supported
& (SUPPORTED_1000baseT_Half
|
93 SUPPORTED_1000baseT_Full
)) {
94 oldadv
= adv
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_CTRL1000
);
99 adv
&= ~(ADVERTISE_1000FULL
| ADVERTISE_1000HALF
);
100 if (advertise
& SUPPORTED_1000baseT_Half
)
101 adv
|= ADVERTISE_1000HALF
;
102 if (advertise
& SUPPORTED_1000baseT_Full
)
103 adv
|= ADVERTISE_1000FULL
;
106 err
= phy_write(phydev
, MDIO_DEVAD_NONE
, MII_CTRL1000
,
120 * genphy_setup_forced - configures/forces speed/duplex from @phydev
121 * @phydev: target phy_device struct
123 * Description: Configures MII_BMCR to force speed/duplex
124 * to the values in phydev. Assumes that the values are valid.
126 static int genphy_setup_forced(struct phy_device
*phydev
)
131 phydev
->pause
= phydev
->asym_pause
= 0;
133 if (SPEED_1000
== phydev
->speed
)
134 ctl
|= BMCR_SPEED1000
;
135 else if (SPEED_100
== phydev
->speed
)
136 ctl
|= BMCR_SPEED100
;
138 if (DUPLEX_FULL
== phydev
->duplex
)
139 ctl
|= BMCR_FULLDPLX
;
141 err
= phy_write(phydev
, MDIO_DEVAD_NONE
, MII_BMCR
, ctl
);
148 * genphy_restart_aneg - Enable and Restart Autonegotiation
149 * @phydev: target phy_device struct
151 int genphy_restart_aneg(struct phy_device
*phydev
)
155 ctl
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMCR
);
160 ctl
|= (BMCR_ANENABLE
| BMCR_ANRESTART
);
162 /* Don't isolate the PHY if we're negotiating */
163 ctl
&= ~(BMCR_ISOLATE
);
165 ctl
= phy_write(phydev
, MDIO_DEVAD_NONE
, MII_BMCR
, ctl
);
172 * genphy_config_aneg - restart auto-negotiation or write BMCR
173 * @phydev: target phy_device struct
175 * Description: If auto-negotiation is enabled, we configure the
176 * advertising, and then restart auto-negotiation. If it is not
177 * enabled, then we write the BMCR.
179 int genphy_config_aneg(struct phy_device
*phydev
)
183 if (AUTONEG_ENABLE
!= phydev
->autoneg
)
184 return genphy_setup_forced(phydev
);
186 result
= genphy_config_advert(phydev
);
188 if (result
< 0) /* error */
192 /* Advertisment hasn't changed, but maybe aneg was never on to
193 * begin with? Or maybe phy was isolated? */
194 int ctl
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMCR
);
199 if (!(ctl
& BMCR_ANENABLE
) || (ctl
& BMCR_ISOLATE
))
200 result
= 1; /* do restart aneg */
203 /* Only restart aneg if we are advertising something different
204 * than we were before. */
206 result
= genphy_restart_aneg(phydev
);
212 * genphy_update_link - update link status in @phydev
213 * @phydev: target phy_device struct
215 * Description: Update the value in phydev->link to reflect the
216 * current link value. In order to do this, we need to read
217 * the status register twice, keeping the second value.
219 int genphy_update_link(struct phy_device
*phydev
)
221 unsigned int mii_reg
;
224 * Wait if the link is up, and autonegotiation is in progress
225 * (ie - we're capable and it's not done)
227 mii_reg
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMSR
);
230 * If we already saw the link up, and it hasn't gone down, then
231 * we don't need to wait for autoneg again
233 if (phydev
->link
&& mii_reg
& BMSR_LSTATUS
)
236 if ((mii_reg
& BMSR_ANEGCAPABLE
) && !(mii_reg
& BMSR_ANEGCOMPLETE
)) {
239 printf("%s Waiting for PHY auto negotiation to complete",
241 while (!(mii_reg
& BMSR_ANEGCOMPLETE
)) {
245 if (i
> PHY_ANEG_TIMEOUT
) {
246 printf(" TIMEOUT !\n");
252 puts("user interrupt!\n");
257 if ((i
++ % 500) == 0)
260 udelay(1000); /* 1 ms */
261 mii_reg
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMSR
);
266 /* Read the link a second time to clear the latched state */
267 mii_reg
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMSR
);
269 if (mii_reg
& BMSR_LSTATUS
)
279 * Generic function which updates the speed and duplex. If
280 * autonegotiation is enabled, it uses the AND of the link
281 * partner's advertised capabilities and our advertised
282 * capabilities. If autonegotiation is disabled, we use the
283 * appropriate bits in the control register.
285 * Stolen from Linux's mii.c and phy_device.c
287 int genphy_parse_link(struct phy_device
*phydev
)
289 int mii_reg
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMSR
);
291 /* We're using autonegotiation */
292 if (mii_reg
& BMSR_ANEGCAPABLE
) {
297 /* Check for gigabit capability */
298 if (mii_reg
& BMSR_ERCAP
) {
299 /* We want a list of states supported by
300 * both PHYs in the link
302 gblpa
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_STAT1000
);
303 gblpa
&= phy_read(phydev
,
304 MDIO_DEVAD_NONE
, MII_CTRL1000
) << 2;
307 /* Set the baseline so we only have to set them
308 * if they're different
310 phydev
->speed
= SPEED_10
;
311 phydev
->duplex
= DUPLEX_HALF
;
313 /* Check the gigabit fields */
314 if (gblpa
& (PHY_1000BTSR_1000FD
| PHY_1000BTSR_1000HD
)) {
315 phydev
->speed
= SPEED_1000
;
317 if (gblpa
& PHY_1000BTSR_1000FD
)
318 phydev
->duplex
= DUPLEX_FULL
;
324 lpa
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_ADVERTISE
);
325 lpa
&= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_LPA
);
327 if (lpa
& (LPA_100FULL
| LPA_100HALF
)) {
328 phydev
->speed
= SPEED_100
;
330 if (lpa
& LPA_100FULL
)
331 phydev
->duplex
= DUPLEX_FULL
;
333 } else if (lpa
& LPA_10FULL
)
334 phydev
->duplex
= DUPLEX_FULL
;
337 * Extended status may indicate that the PHY supports
338 * 1000BASE-T/X even though the 1000BASE-T registers
339 * are missing. In this case we can't tell whether the
340 * peer also supports it, so we only check extended
341 * status if the 1000BASE-T registers are actually
344 if ((mii_reg
& BMSR_ESTATEN
) && !(mii_reg
& BMSR_ERCAP
))
345 estatus
= phy_read(phydev
, MDIO_DEVAD_NONE
,
348 if (estatus
& (ESTATUS_1000_XFULL
| ESTATUS_1000_XHALF
|
349 ESTATUS_1000_TFULL
| ESTATUS_1000_THALF
)) {
350 phydev
->speed
= SPEED_1000
;
351 if (estatus
& (ESTATUS_1000_XFULL
| ESTATUS_1000_TFULL
))
352 phydev
->duplex
= DUPLEX_FULL
;
356 u32 bmcr
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMCR
);
358 phydev
->speed
= SPEED_10
;
359 phydev
->duplex
= DUPLEX_HALF
;
361 if (bmcr
& BMCR_FULLDPLX
)
362 phydev
->duplex
= DUPLEX_FULL
;
364 if (bmcr
& BMCR_SPEED1000
)
365 phydev
->speed
= SPEED_1000
;
366 else if (bmcr
& BMCR_SPEED100
)
367 phydev
->speed
= SPEED_100
;
373 int genphy_config(struct phy_device
*phydev
)
378 /* For now, I'll claim that the generic driver supports
379 * all possible port types */
380 features
= (SUPPORTED_TP
| SUPPORTED_MII
381 | SUPPORTED_AUI
| SUPPORTED_FIBRE
|
384 /* Do we support autonegotiation? */
385 val
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMSR
);
390 if (val
& BMSR_ANEGCAPABLE
)
391 features
|= SUPPORTED_Autoneg
;
393 if (val
& BMSR_100FULL
)
394 features
|= SUPPORTED_100baseT_Full
;
395 if (val
& BMSR_100HALF
)
396 features
|= SUPPORTED_100baseT_Half
;
397 if (val
& BMSR_10FULL
)
398 features
|= SUPPORTED_10baseT_Full
;
399 if (val
& BMSR_10HALF
)
400 features
|= SUPPORTED_10baseT_Half
;
402 if (val
& BMSR_ESTATEN
) {
403 val
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_ESTATUS
);
408 if (val
& ESTATUS_1000_TFULL
)
409 features
|= SUPPORTED_1000baseT_Full
;
410 if (val
& ESTATUS_1000_THALF
)
411 features
|= SUPPORTED_1000baseT_Half
;
412 if (val
& ESTATUS_1000_XFULL
)
413 features
|= SUPPORTED_1000baseX_Full
;
414 if (val
& ESTATUS_1000_XHALF
)
415 features
|= SUPPORTED_1000baseX_Full
;
418 phydev
->supported
= features
;
419 phydev
->advertising
= features
;
421 genphy_config_aneg(phydev
);
426 int genphy_startup(struct phy_device
*phydev
)
428 genphy_update_link(phydev
);
429 genphy_parse_link(phydev
);
434 int genphy_shutdown(struct phy_device
*phydev
)
439 static struct phy_driver genphy_driver
= {
442 .name
= "Generic PHY",
444 .config
= genphy_config
,
445 .startup
= genphy_startup
,
446 .shutdown
= genphy_shutdown
,
449 static LIST_HEAD(phy_drivers
);
453 #ifdef CONFIG_PHY_ATHEROS
456 #ifdef CONFIG_PHY_BROADCOM
459 #ifdef CONFIG_PHY_DAVICOM
462 #ifdef CONFIG_PHY_ET1011C
465 #ifdef CONFIG_PHY_ICPLUS
468 #ifdef CONFIG_PHY_LXT
471 #ifdef CONFIG_PHY_MARVELL
474 #ifdef CONFIG_PHY_MICREL
477 #ifdef CONFIG_PHY_NATSEMI
480 #ifdef CONFIG_PHY_REALTEK
483 #ifdef CONFIG_PHY_SMSC
486 #ifdef CONFIG_PHY_TERANETICS
487 phy_teranetics_init();
489 #ifdef CONFIG_PHY_VITESSE
496 int phy_register(struct phy_driver
*drv
)
498 INIT_LIST_HEAD(&drv
->list
);
499 list_add_tail(&drv
->list
, &phy_drivers
);
504 static int phy_probe(struct phy_device
*phydev
)
508 phydev
->advertising
= phydev
->supported
= phydev
->drv
->features
;
509 phydev
->mmds
= phydev
->drv
->mmds
;
511 if (phydev
->drv
->probe
)
512 err
= phydev
->drv
->probe(phydev
);
517 static struct phy_driver
*generic_for_interface(phy_interface_t interface
)
519 #ifdef CONFIG_PHYLIB_10G
520 if (is_10g_interface(interface
))
521 return &gen10g_driver
;
524 return &genphy_driver
;
527 static struct phy_driver
*get_phy_driver(struct phy_device
*phydev
,
528 phy_interface_t interface
)
530 struct list_head
*entry
;
531 int phy_id
= phydev
->phy_id
;
532 struct phy_driver
*drv
= NULL
;
534 list_for_each(entry
, &phy_drivers
) {
535 drv
= list_entry(entry
, struct phy_driver
, list
);
536 if ((drv
->uid
& drv
->mask
) == (phy_id
& drv
->mask
))
540 /* If we made it here, there's no driver for this PHY */
541 return generic_for_interface(interface
);
544 static struct phy_device
*phy_device_create(struct mii_dev
*bus
, int addr
,
546 phy_interface_t interface
)
548 struct phy_device
*dev
;
550 /* We allocate the device, and initialize the
552 dev
= malloc(sizeof(*dev
));
554 printf("Failed to allocate PHY device for %s:%d\n",
559 memset(dev
, 0, sizeof(*dev
));
563 dev
->interface
= interface
;
565 dev
->autoneg
= AUTONEG_ENABLE
;
568 dev
->phy_id
= phy_id
;
571 dev
->drv
= get_phy_driver(dev
, interface
);
575 bus
->phymap
[addr
] = dev
;
581 * get_phy_id - reads the specified addr for its ID.
582 * @bus: the target MII bus
583 * @addr: PHY address on the MII bus
584 * @phy_id: where to store the ID retrieved.
586 * Description: Reads the ID registers of the PHY at @addr on the
587 * @bus, stores it in @phy_id and returns zero on success.
589 static int get_phy_id(struct mii_dev
*bus
, int addr
, int devad
, u32
*phy_id
)
593 /* Grab the bits from PHYIR1, and put them
594 * in the upper half */
595 phy_reg
= bus
->read(bus
, addr
, devad
, MII_PHYSID1
);
600 *phy_id
= (phy_reg
& 0xffff) << 16;
602 /* Grab the bits from PHYIR2, and put them in the lower half */
603 phy_reg
= bus
->read(bus
, addr
, devad
, MII_PHYSID2
);
608 *phy_id
|= (phy_reg
& 0xffff);
613 static struct phy_device
*create_phy_by_mask(struct mii_dev
*bus
,
614 unsigned phy_mask
, int devad
, phy_interface_t interface
)
616 u32 phy_id
= 0xffffffff;
618 int addr
= ffs(phy_mask
) - 1;
619 int r
= get_phy_id(bus
, addr
, devad
, &phy_id
);
622 /* If the PHY ID is mostly f's, we didn't find anything */
623 if ((phy_id
& 0x1fffffff) != 0x1fffffff)
624 return phy_device_create(bus
, addr
, phy_id
, interface
);
625 phy_mask
&= ~(1 << addr
);
630 static struct phy_device
*search_for_existing_phy(struct mii_dev
*bus
,
631 unsigned phy_mask
, phy_interface_t interface
)
633 /* If we have one, return the existing device, with new interface */
635 int addr
= ffs(phy_mask
) - 1;
636 if (bus
->phymap
[addr
]) {
637 bus
->phymap
[addr
]->interface
= interface
;
638 return bus
->phymap
[addr
];
640 phy_mask
&= ~(1 << addr
);
645 static struct phy_device
*get_phy_device_by_mask(struct mii_dev
*bus
,
646 unsigned phy_mask
, phy_interface_t interface
)
649 struct phy_device
*phydev
;
651 phydev
= search_for_existing_phy(bus
, phy_mask
, interface
);
654 /* Try Standard (ie Clause 22) access */
655 /* Otherwise we have to try Clause 45 */
656 for (i
= 0; i
< 5; i
++) {
657 phydev
= create_phy_by_mask(bus
, phy_mask
,
658 i
? i
: MDIO_DEVAD_NONE
, interface
);
664 printf("Phy not found\n");
665 return phy_device_create(bus
, ffs(phy_mask
) - 1, 0xffffffff, interface
);
669 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
670 * @bus: the target MII bus
671 * @addr: PHY address on the MII bus
673 * Description: Reads the ID registers of the PHY at @addr on the
674 * @bus, then allocates and returns the phy_device to represent it.
676 static struct phy_device
*get_phy_device(struct mii_dev
*bus
, int addr
,
677 phy_interface_t interface
)
679 return get_phy_device_by_mask(bus
, 1 << addr
, interface
);
682 int phy_reset(struct phy_device
*phydev
)
686 int devad
= MDIO_DEVAD_NONE
;
688 #ifdef CONFIG_PHYLIB_10G
689 /* If it's 10G, we need to issue reset through one of the MMDs */
690 if (is_10g_interface(phydev
->interface
)) {
692 gen10g_discover_mmds(phydev
);
694 devad
= ffs(phydev
->mmds
) - 1;
698 reg
= phy_read(phydev
, devad
, MII_BMCR
);
700 debug("PHY status read failed\n");
706 if (phy_write(phydev
, devad
, MII_BMCR
, reg
) < 0) {
707 debug("PHY reset failed\n");
711 #ifdef CONFIG_PHY_RESET_DELAY
712 udelay(CONFIG_PHY_RESET_DELAY
); /* Intel LXT971A needs this */
715 * Poll the control register for the reset bit to go to 0 (it is
716 * auto-clearing). This should happen within 0.5 seconds per the
719 while ((reg
& BMCR_RESET
) && timeout
--) {
720 reg
= phy_read(phydev
, devad
, MII_BMCR
);
723 debug("PHY status read failed\n");
729 if (reg
& BMCR_RESET
) {
730 puts("PHY reset timed out\n");
737 int miiphy_reset(const char *devname
, unsigned char addr
)
739 struct mii_dev
*bus
= miiphy_get_dev_by_name(devname
);
740 struct phy_device
*phydev
;
743 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
744 * If later code tries to connect with the right interface, this will
745 * be corrected by get_phy_device in phy_connect()
747 phydev
= get_phy_device(bus
, addr
, PHY_INTERFACE_MODE_MII
);
749 return phy_reset(phydev
);
752 struct phy_device
*phy_find_by_mask(struct mii_dev
*bus
, unsigned phy_mask
,
753 phy_interface_t interface
)
759 /* Wait 15ms to make sure the PHY has come out of hard reset */
761 return get_phy_device_by_mask(bus
, phy_mask
, interface
);
764 void phy_connect_dev(struct phy_device
*phydev
, struct eth_device
*dev
)
766 /* Soft Reset the PHY */
769 printf("%s:%d is connected to %s. Reconnecting to %s\n",
770 phydev
->bus
->name
, phydev
->addr
,
771 phydev
->dev
->name
, dev
->name
);
774 debug("%s connected to %s\n", dev
->name
, phydev
->drv
->name
);
777 struct phy_device
*phy_connect(struct mii_dev
*bus
, int addr
,
778 struct eth_device
*dev
, phy_interface_t interface
)
780 struct phy_device
*phydev
;
782 phydev
= phy_find_by_mask(bus
, 1 << addr
, interface
);
784 phy_connect_dev(phydev
, dev
);
786 printf("Could not get PHY for %s: addr %d\n", bus
->name
, addr
);
791 * Start the PHY. Returns 0 on success, or a negative error code.
793 int phy_startup(struct phy_device
*phydev
)
795 if (phydev
->drv
->startup
)
796 return phydev
->drv
->startup(phydev
);
801 static int __board_phy_config(struct phy_device
*phydev
)
803 if (phydev
->drv
->config
)
804 return phydev
->drv
->config(phydev
);
808 int board_phy_config(struct phy_device
*phydev
)
809 __attribute__((weak
, alias("__board_phy_config")));
811 int phy_config(struct phy_device
*phydev
)
813 /* Invoke an optional board-specific helper */
814 board_phy_config(phydev
);
819 int phy_shutdown(struct phy_device
*phydev
)
821 if (phydev
->drv
->shutdown
)
822 phydev
->drv
->shutdown(phydev
);