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
35 /* Generic PHY support and helper functions */
38 * genphy_config_advert - sanitize and advertise auto-negotation parameters
39 * @phydev: target phy_device struct
41 * Description: Writes MII_ADVERTISE with the appropriate values,
42 * after sanitizing the values to make sure we only advertise
43 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
44 * hasn't changed, and > 0 if it has changed.
46 int genphy_config_advert(struct phy_device
*phydev
)
52 /* Only allow advertising what
53 * this PHY supports */
54 phydev
->advertising
&= phydev
->supported
;
55 advertise
= phydev
->advertising
;
57 /* Setup standard advertisement */
58 oldadv
= adv
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_ADVERTISE
);
63 adv
&= ~(ADVERTISE_ALL
| ADVERTISE_100BASE4
| ADVERTISE_PAUSE_CAP
|
64 ADVERTISE_PAUSE_ASYM
);
65 if (advertise
& ADVERTISED_10baseT_Half
)
66 adv
|= ADVERTISE_10HALF
;
67 if (advertise
& ADVERTISED_10baseT_Full
)
68 adv
|= ADVERTISE_10FULL
;
69 if (advertise
& ADVERTISED_100baseT_Half
)
70 adv
|= ADVERTISE_100HALF
;
71 if (advertise
& ADVERTISED_100baseT_Full
)
72 adv
|= ADVERTISE_100FULL
;
73 if (advertise
& ADVERTISED_Pause
)
74 adv
|= ADVERTISE_PAUSE_CAP
;
75 if (advertise
& ADVERTISED_Asym_Pause
)
76 adv
|= ADVERTISE_PAUSE_ASYM
;
79 err
= phy_write(phydev
, MDIO_DEVAD_NONE
, MII_ADVERTISE
, adv
);
86 /* Configure gigabit if it's supported */
87 if (phydev
->supported
& (SUPPORTED_1000baseT_Half
|
88 SUPPORTED_1000baseT_Full
)) {
89 oldadv
= adv
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_CTRL1000
);
94 adv
&= ~(ADVERTISE_1000FULL
| ADVERTISE_1000HALF
);
95 if (advertise
& SUPPORTED_1000baseT_Half
)
96 adv
|= ADVERTISE_1000HALF
;
97 if (advertise
& SUPPORTED_1000baseT_Full
)
98 adv
|= ADVERTISE_1000FULL
;
101 err
= phy_write(phydev
, MDIO_DEVAD_NONE
, MII_CTRL1000
,
115 * genphy_setup_forced - configures/forces speed/duplex from @phydev
116 * @phydev: target phy_device struct
118 * Description: Configures MII_BMCR to force speed/duplex
119 * to the values in phydev. Assumes that the values are valid.
121 int genphy_setup_forced(struct phy_device
*phydev
)
126 phydev
->pause
= phydev
->asym_pause
= 0;
128 if (SPEED_1000
== phydev
->speed
)
129 ctl
|= BMCR_SPEED1000
;
130 else if (SPEED_100
== phydev
->speed
)
131 ctl
|= BMCR_SPEED100
;
133 if (DUPLEX_FULL
== phydev
->duplex
)
134 ctl
|= BMCR_FULLDPLX
;
136 err
= phy_write(phydev
, MDIO_DEVAD_NONE
, MII_BMCR
, ctl
);
143 * genphy_restart_aneg - Enable and Restart Autonegotiation
144 * @phydev: target phy_device struct
146 int genphy_restart_aneg(struct phy_device
*phydev
)
150 ctl
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMCR
);
155 ctl
|= (BMCR_ANENABLE
| BMCR_ANRESTART
);
157 /* Don't isolate the PHY if we're negotiating */
158 ctl
&= ~(BMCR_ISOLATE
);
160 ctl
= phy_write(phydev
, MDIO_DEVAD_NONE
, MII_BMCR
, ctl
);
167 * genphy_config_aneg - restart auto-negotiation or write BMCR
168 * @phydev: target phy_device struct
170 * Description: If auto-negotiation is enabled, we configure the
171 * advertising, and then restart auto-negotiation. If it is not
172 * enabled, then we write the BMCR.
174 int genphy_config_aneg(struct phy_device
*phydev
)
178 if (AUTONEG_ENABLE
!= phydev
->autoneg
)
179 return genphy_setup_forced(phydev
);
181 result
= genphy_config_advert(phydev
);
183 if (result
< 0) /* error */
187 /* Advertisment hasn't changed, but maybe aneg was never on to
188 * begin with? Or maybe phy was isolated? */
189 int ctl
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMCR
);
194 if (!(ctl
& BMCR_ANENABLE
) || (ctl
& BMCR_ISOLATE
))
195 result
= 1; /* do restart aneg */
198 /* Only restart aneg if we are advertising something different
199 * than we were before. */
201 result
= genphy_restart_aneg(phydev
);
207 * genphy_update_link - update link status in @phydev
208 * @phydev: target phy_device struct
210 * Description: Update the value in phydev->link to reflect the
211 * current link value. In order to do this, we need to read
212 * the status register twice, keeping the second value.
214 int genphy_update_link(struct phy_device
*phydev
)
216 unsigned int mii_reg
;
219 * Wait if the link is up, and autonegotiation is in progress
220 * (ie - we're capable and it's not done)
222 mii_reg
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMSR
);
225 * If we already saw the link up, and it hasn't gone down, then
226 * we don't need to wait for autoneg again
228 if (phydev
->link
&& mii_reg
& BMSR_LSTATUS
)
231 if ((mii_reg
& BMSR_ANEGCAPABLE
) && !(mii_reg
& BMSR_ANEGCOMPLETE
)) {
234 printf("%s Waiting for PHY auto negotiation to complete",
236 while (!(mii_reg
& BMSR_ANEGCOMPLETE
)) {
240 if (i
> PHY_ANEG_TIMEOUT
) {
241 printf(" TIMEOUT !\n");
247 puts("user interrupt!\n");
252 if ((i
++ % 500) == 0)
255 udelay(1000); /* 1 ms */
256 mii_reg
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMSR
);
261 /* Read the link a second time to clear the latched state */
262 mii_reg
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMSR
);
264 if (mii_reg
& BMSR_LSTATUS
)
274 * Generic function which updates the speed and duplex. If
275 * autonegotiation is enabled, it uses the AND of the link
276 * partner's advertised capabilities and our advertised
277 * capabilities. If autonegotiation is disabled, we use the
278 * appropriate bits in the control register.
280 * Stolen from Linux's mii.c and phy_device.c
282 static int genphy_parse_link(struct phy_device
*phydev
)
284 int mii_reg
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMSR
);
286 /* We're using autonegotiation */
287 if (mii_reg
& BMSR_ANEGCAPABLE
) {
291 /* Check for gigabit capability */
292 if (mii_reg
& BMSR_ERCAP
) {
293 /* We want a list of states supported by
294 * both PHYs in the link
296 gblpa
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_STAT1000
);
297 gblpa
&= phy_read(phydev
,
298 MDIO_DEVAD_NONE
, MII_CTRL1000
) << 2;
301 /* Set the baseline so we only have to set them
302 * if they're different
304 phydev
->speed
= SPEED_10
;
305 phydev
->duplex
= DUPLEX_HALF
;
307 /* Check the gigabit fields */
308 if (gblpa
& (PHY_1000BTSR_1000FD
| PHY_1000BTSR_1000HD
)) {
309 phydev
->speed
= SPEED_1000
;
311 if (gblpa
& PHY_1000BTSR_1000FD
)
312 phydev
->duplex
= DUPLEX_FULL
;
318 lpa
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_ADVERTISE
);
319 lpa
&= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_LPA
);
321 if (lpa
& (LPA_100FULL
| LPA_100HALF
)) {
322 phydev
->speed
= SPEED_100
;
324 if (lpa
& LPA_100FULL
)
325 phydev
->duplex
= DUPLEX_FULL
;
327 } else if (lpa
& LPA_10FULL
)
328 phydev
->duplex
= DUPLEX_FULL
;
330 u32 bmcr
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMCR
);
332 phydev
->speed
= SPEED_10
;
333 phydev
->duplex
= DUPLEX_HALF
;
335 if (bmcr
& BMCR_FULLDPLX
)
336 phydev
->duplex
= DUPLEX_FULL
;
338 if (bmcr
& BMCR_SPEED1000
)
339 phydev
->speed
= SPEED_1000
;
340 else if (bmcr
& BMCR_SPEED100
)
341 phydev
->speed
= SPEED_100
;
347 int genphy_config(struct phy_device
*phydev
)
352 /* For now, I'll claim that the generic driver supports
353 * all possible port types */
354 features
= (SUPPORTED_TP
| SUPPORTED_MII
355 | SUPPORTED_AUI
| SUPPORTED_FIBRE
|
358 /* Do we support autonegotiation? */
359 val
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_BMSR
);
364 if (val
& BMSR_ANEGCAPABLE
)
365 features
|= SUPPORTED_Autoneg
;
367 if (val
& BMSR_100FULL
)
368 features
|= SUPPORTED_100baseT_Full
;
369 if (val
& BMSR_100HALF
)
370 features
|= SUPPORTED_100baseT_Half
;
371 if (val
& BMSR_10FULL
)
372 features
|= SUPPORTED_10baseT_Full
;
373 if (val
& BMSR_10HALF
)
374 features
|= SUPPORTED_10baseT_Half
;
376 if (val
& BMSR_ESTATEN
) {
377 val
= phy_read(phydev
, MDIO_DEVAD_NONE
, MII_ESTATUS
);
382 if (val
& ESTATUS_1000_TFULL
)
383 features
|= SUPPORTED_1000baseT_Full
;
384 if (val
& ESTATUS_1000_THALF
)
385 features
|= SUPPORTED_1000baseT_Half
;
388 phydev
->supported
= features
;
389 phydev
->advertising
= features
;
391 genphy_config_aneg(phydev
);
396 int genphy_startup(struct phy_device
*phydev
)
398 genphy_update_link(phydev
);
399 genphy_parse_link(phydev
);
404 int genphy_shutdown(struct phy_device
*phydev
)
409 static struct phy_driver genphy_driver
= {
412 .name
= "Generic PHY",
414 .config
= genphy_config
,
415 .startup
= genphy_startup
,
416 .shutdown
= genphy_shutdown
,
419 static LIST_HEAD(phy_drivers
);
426 int phy_register(struct phy_driver
*drv
)
428 INIT_LIST_HEAD(&drv
->list
);
429 list_add_tail(&drv
->list
, &phy_drivers
);
434 int phy_probe(struct phy_device
*phydev
)
438 phydev
->advertising
= phydev
->supported
= phydev
->drv
->features
;
439 phydev
->mmds
= phydev
->drv
->mmds
;
441 if (phydev
->drv
->probe
)
442 err
= phydev
->drv
->probe(phydev
);
447 static struct phy_driver
*generic_for_interface(phy_interface_t interface
)
449 #ifdef CONFIG_PHYLIB_10G
450 if (is_10g_interface(interface
))
451 return &gen10g_driver
;
454 return &genphy_driver
;
457 struct phy_driver
*get_phy_driver(struct phy_device
*phydev
,
458 phy_interface_t interface
)
460 struct list_head
*entry
;
461 int phy_id
= phydev
->phy_id
;
462 struct phy_driver
*drv
= NULL
;
464 list_for_each(entry
, &phy_drivers
) {
465 drv
= list_entry(entry
, struct phy_driver
, list
);
466 if ((drv
->uid
& drv
->mask
) == (phy_id
& drv
->mask
))
470 /* If we made it here, there's no driver for this PHY */
471 return generic_for_interface(interface
);
474 struct phy_device
*phy_device_create(struct mii_dev
*bus
, int addr
, int phy_id
,
475 phy_interface_t interface
)
477 struct phy_device
*dev
;
479 /* We allocate the device, and initialize the
481 dev
= malloc(sizeof(*dev
));
483 printf("Failed to allocate PHY device for %s:%d\n",
488 memset(dev
, 0, sizeof(*dev
));
492 dev
->interface
= interface
;
494 dev
->autoneg
= AUTONEG_ENABLE
;
497 dev
->phy_id
= phy_id
;
500 dev
->drv
= get_phy_driver(dev
, interface
);
504 bus
->phymap
[addr
] = dev
;
510 * get_phy_id - reads the specified addr for its ID.
511 * @bus: the target MII bus
512 * @addr: PHY address on the MII bus
513 * @phy_id: where to store the ID retrieved.
515 * Description: Reads the ID registers of the PHY at @addr on the
516 * @bus, stores it in @phy_id and returns zero on success.
518 int get_phy_id(struct mii_dev
*bus
, int addr
, int devad
, u32
*phy_id
)
522 /* Grab the bits from PHYIR1, and put them
523 * in the upper half */
524 phy_reg
= bus
->read(bus
, addr
, devad
, MII_PHYSID1
);
529 *phy_id
= (phy_reg
& 0xffff) << 16;
531 /* Grab the bits from PHYIR2, and put them in the lower half */
532 phy_reg
= bus
->read(bus
, addr
, devad
, MII_PHYSID2
);
537 *phy_id
|= (phy_reg
& 0xffff);
543 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
544 * @bus: the target MII bus
545 * @addr: PHY address on the MII bus
547 * Description: Reads the ID registers of the PHY at @addr on the
548 * @bus, then allocates and returns the phy_device to represent it.
550 struct phy_device
*get_phy_device(struct mii_dev
*bus
, int addr
,
551 phy_interface_t interface
)
553 u32 phy_id
= 0x1fffffff;
557 /* If we have one, return the existing device, with new interface */
558 if (bus
->phymap
[addr
]) {
559 bus
->phymap
[addr
]->interface
= interface
;
561 return bus
->phymap
[addr
];
564 /* Try Standard (ie Clause 22) access */
565 r
= get_phy_id(bus
, addr
, MDIO_DEVAD_NONE
, &phy_id
);
569 /* If the PHY ID is mostly f's, we didn't find anything */
570 if ((phy_id
& 0x1fffffff) != 0x1fffffff)
571 return phy_device_create(bus
, addr
, phy_id
, interface
);
573 /* Otherwise we have to try Clause 45 */
574 for (i
= 1; i
< 5; i
++) {
575 r
= get_phy_id(bus
, addr
, i
, &phy_id
);
579 /* If the phy_id is mostly Fs, there is no device there */
580 if ((phy_id
& 0x1fffffff) != 0x1fffffff)
584 return phy_device_create(bus
, addr
, phy_id
, interface
);
587 int phy_reset(struct phy_device
*phydev
)
591 int devad
= MDIO_DEVAD_NONE
;
593 #ifdef CONFIG_PHYLIB_10G
594 /* If it's 10G, we need to issue reset through one of the MMDs */
595 if (is_10g_interface(phydev
->interface
)) {
597 gen10g_discover_mmds(phydev
);
599 devad
= ffs(phydev
->mmds
) - 1;
603 reg
= phy_read(phydev
, devad
, MII_BMCR
);
605 debug("PHY status read failed\n");
611 if (phy_write(phydev
, devad
, MII_BMCR
, reg
) < 0) {
612 debug("PHY reset failed\n");
616 #ifdef CONFIG_PHY_RESET_DELAY
617 udelay(CONFIG_PHY_RESET_DELAY
); /* Intel LXT971A needs this */
620 * Poll the control register for the reset bit to go to 0 (it is
621 * auto-clearing). This should happen within 0.5 seconds per the
624 while ((reg
& BMCR_RESET
) && timeout
--) {
625 reg
= phy_read(phydev
, devad
, MII_BMCR
);
628 debug("PHY status read failed\n");
634 if (reg
& BMCR_RESET
) {
635 puts("PHY reset timed out\n");
642 int miiphy_reset(const char *devname
, unsigned char addr
)
644 struct mii_dev
*bus
= miiphy_get_dev_by_name(devname
);
645 struct phy_device
*phydev
;
648 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
649 * If later code tries to connect with the right interface, this will
650 * be corrected by get_phy_device in phy_connect()
652 phydev
= get_phy_device(bus
, addr
, PHY_INTERFACE_MODE_MII
);
654 return phy_reset(phydev
);
657 struct phy_device
*phy_connect(struct mii_dev
*bus
, int addr
,
658 struct eth_device
*dev
,
659 phy_interface_t interface
)
661 struct phy_device
*phydev
;
666 /* Wait 15ms to make sure the PHY has come out of hard reset */
669 phydev
= get_phy_device(bus
, addr
, interface
);
672 printf("Could not get PHY for %s:%d\n", bus
->name
, addr
);
677 /* Soft Reset the PHY */
681 printf("%s:%d is connected to %s. Reconnecting to %s\n",
682 bus
->name
, addr
, phydev
->dev
->name
, dev
->name
);
686 printf("%s connected to %s\n", dev
->name
, phydev
->drv
->name
);
691 int phy_startup(struct phy_device
*phydev
)
693 if (phydev
->drv
->startup
)
694 phydev
->drv
->startup(phydev
);
699 static int __board_phy_config(struct phy_device
*phydev
)
704 int board_phy_config(struct phy_device
*phydev
)
705 __attribute__((weak
, alias("__board_phy_config")));
707 int phy_config(struct phy_device
*phydev
)
709 if (phydev
->drv
->config
)
710 phydev
->drv
->config(phydev
);
712 /* Invoke an optional board-specific helper */
713 board_phy_config(phydev
);
718 int phy_shutdown(struct phy_device
*phydev
)
720 if (phydev
->drv
->shutdown
)
721 phydev
->drv
->shutdown(phydev
);