]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - common/miiphyutil.c
274e88a4921a16d96d8e0195bf9a77f9c5f9703d
1 // SPDX-License-Identifier: GPL-2.0+
4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
8 * This provides a bit-banged interface to the ethernet MII management
16 #include <linux/delay.h>
18 #include <asm/types.h>
19 #include <linux/list.h>
23 /* local debug macro */
28 #define debug(fmt, args...) printf(fmt, ##args)
30 #define debug(fmt, args...)
31 #endif /* MII_DEBUG */
33 static LIST_HEAD(mii_devs
);
34 static struct mii_dev
*current_mii
;
37 * Lookup the mii_dev struct by the registered device name.
39 struct mii_dev
*miiphy_get_dev_by_name(const char *devname
)
41 struct list_head
*entry
;
45 printf("NULL device name!\n");
49 list_for_each(entry
, &mii_devs
) {
50 dev
= list_entry(entry
, struct mii_dev
, link
);
51 if (strcmp(dev
->name
, devname
) == 0)
58 struct mii_dev
*mdio_alloc(void)
62 bus
= malloc(sizeof(*bus
));
66 memset(bus
, 0, sizeof(*bus
));
68 /* initialize mii_dev struct fields */
69 INIT_LIST_HEAD(&bus
->link
);
74 void mdio_free(struct mii_dev
*bus
)
79 int mdio_register(struct mii_dev
*bus
)
81 if (!bus
|| !bus
->read
|| !bus
->write
)
84 /* check if we have unique name */
85 if (miiphy_get_dev_by_name(bus
->name
)) {
86 printf("mdio_register: non unique device name '%s'\n",
91 /* add it to the list */
92 list_add_tail(&bus
->link
, &mii_devs
);
100 int mdio_register_seq(struct mii_dev
*bus
, int seq
)
104 /* Setup a unique name for each mdio bus */
105 ret
= snprintf(bus
->name
, MDIO_NAME_LEN
, "eth%d", seq
);
109 return mdio_register(bus
);
112 int mdio_unregister(struct mii_dev
*bus
)
117 /* delete it from the list */
118 list_del(&bus
->link
);
120 if (current_mii
== bus
)
126 void mdio_list_devices(void)
128 struct list_head
*entry
;
130 list_for_each(entry
, &mii_devs
) {
132 struct mii_dev
*bus
= list_entry(entry
, struct mii_dev
, link
);
134 printf("%s:\n", bus
->name
);
136 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
137 struct phy_device
*phydev
= bus
->phymap
[i
];
140 printf("%x - %s", i
, phydev
->drv
->name
);
143 printf(" <--> %s\n", phydev
->dev
->name
);
151 int miiphy_set_current_dev(const char *devname
)
155 dev
= miiphy_get_dev_by_name(devname
);
161 printf("No such device: %s\n", devname
);
166 struct mii_dev
*mdio_get_current_dev(void)
171 struct list_head
*mdio_get_list_head(void)
176 struct phy_device
*mdio_phydev_for_ethname(const char *ethname
)
178 struct list_head
*entry
;
181 list_for_each(entry
, &mii_devs
) {
183 bus
= list_entry(entry
, struct mii_dev
, link
);
185 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
186 if (!bus
->phymap
[i
] || !bus
->phymap
[i
]->dev
)
189 if (strcmp(bus
->phymap
[i
]->dev
->name
, ethname
) == 0)
190 return bus
->phymap
[i
];
194 printf("%s is not a known ethernet\n", ethname
);
198 const char *miiphy_get_current_dev(void)
201 return current_mii
->name
;
206 static struct mii_dev
*miiphy_get_active_dev(const char *devname
)
208 /* If the current mii is the one we want, return it */
210 if (strcmp(current_mii
->name
, devname
) == 0)
213 /* Otherwise, set the active one to the one we want */
214 if (miiphy_set_current_dev(devname
))
220 /*****************************************************************************
222 * Read to variable <value> from the PHY attached to device <devname>,
223 * use PHY address <addr> and register <reg>.
225 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
230 int miiphy_read(const char *devname
, unsigned char addr
, unsigned char reg
,
231 unsigned short *value
)
236 bus
= miiphy_get_active_dev(devname
);
240 ret
= bus
->read(bus
, addr
, MDIO_DEVAD_NONE
, reg
);
244 *value
= (unsigned short)ret
;
248 /*****************************************************************************
250 * Write <value> to the PHY attached to device <devname>,
251 * use PHY address <addr> and register <reg>.
253 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
258 int miiphy_write(const char *devname
, unsigned char addr
, unsigned char reg
,
259 unsigned short value
)
263 bus
= miiphy_get_active_dev(devname
);
265 return bus
->write(bus
, addr
, MDIO_DEVAD_NONE
, reg
, value
);
270 /*****************************************************************************
272 * Print out list of registered MII capable devices.
274 void miiphy_listdev(void)
276 struct list_head
*entry
;
279 puts("MII devices: ");
280 list_for_each(entry
, &mii_devs
) {
281 dev
= list_entry(entry
, struct mii_dev
, link
);
282 printf("'%s' ", dev
->name
);
287 printf("Current device: '%s'\n", current_mii
->name
);
290 /*****************************************************************************
292 * Read the OUI, manufacture's model number, and revision number.
294 * OUI: 22 bits (unsigned int)
295 * Model: 6 bits (unsigned char)
296 * Revision: 4 bits (unsigned char)
298 * This API is deprecated.
303 int miiphy_info(const char *devname
, unsigned char addr
, unsigned int *oui
,
304 unsigned char *model
, unsigned char *rev
)
306 unsigned int reg
= 0;
309 if (miiphy_read(devname
, addr
, MII_PHYSID2
, &tmp
) != 0) {
310 debug("PHY ID register 2 read failed\n");
315 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr
, reg
);
318 /* No physical device present at this address */
322 if (miiphy_read(devname
, addr
, MII_PHYSID1
, &tmp
) != 0) {
323 debug("PHY ID register 1 read failed\n");
327 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr
, reg
);
330 *model
= (unsigned char)((reg
>> 4) & 0x0000003F);
331 *rev
= (unsigned char)(reg
& 0x0000000F);
335 #ifndef CONFIG_PHYLIB
336 /*****************************************************************************
340 * This API is deprecated. Use PHYLIB.
345 int miiphy_reset(const char *devname
, unsigned char addr
)
350 if (miiphy_read(devname
, addr
, MII_BMCR
, ®
) != 0) {
351 debug("PHY status read failed\n");
354 if (miiphy_write(devname
, addr
, MII_BMCR
, reg
| BMCR_RESET
) != 0) {
355 debug("PHY reset failed\n");
358 #if CONFIG_PHY_RESET_DELAY > 0
359 udelay(CONFIG_PHY_RESET_DELAY
); /* Intel LXT971A needs this */
362 * Poll the control register for the reset bit to go to 0 (it is
363 * auto-clearing). This should happen within 0.5 seconds per the
367 while (((reg
& 0x8000) != 0) && timeout
--) {
368 if (miiphy_read(devname
, addr
, MII_BMCR
, ®
) != 0) {
369 debug("PHY status read failed\n");
374 if ((reg
& 0x8000) == 0) {
377 puts("PHY reset timed out\n");
384 /*****************************************************************************
386 * Determine the ethernet speed (10/100/1000). Return 10 on error.
388 int miiphy_speed(const char *devname
, unsigned char addr
)
390 u16 bmcr
, anlpar
, adv
;
392 #if defined(CONFIG_PHY_GIGE)
396 * Check for 1000BASE-X. If it is supported, then assume that the speed
399 if (miiphy_is_1000base_x(devname
, addr
))
403 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
405 /* Check for 1000BASE-T. */
406 if (miiphy_read(devname
, addr
, MII_STAT1000
, &btsr
)) {
407 printf("PHY 1000BT status");
408 goto miiphy_read_failed
;
410 if (btsr
!= 0xFFFF &&
411 (btsr
& (PHY_1000BTSR_1000FD
| PHY_1000BTSR_1000HD
)))
413 #endif /* CONFIG_PHY_GIGE */
415 /* Check Basic Management Control Register first. */
416 if (miiphy_read(devname
, addr
, MII_BMCR
, &bmcr
)) {
418 goto miiphy_read_failed
;
420 /* Check if auto-negotiation is on. */
421 if (bmcr
& BMCR_ANENABLE
) {
422 /* Get auto-negotiation results. */
423 if (miiphy_read(devname
, addr
, MII_LPA
, &anlpar
)) {
424 printf("PHY AN speed");
425 goto miiphy_read_failed
;
428 if (miiphy_read(devname
, addr
, MII_ADVERTISE
, &adv
)) {
429 puts("PHY AN adv speed");
430 goto miiphy_read_failed
;
432 return ((anlpar
& adv
) & LPA_100
) ? _100BASET
: _10BASET
;
434 /* Get speed from basic control settings. */
435 return (bmcr
& BMCR_SPEED100
) ? _100BASET
: _10BASET
;
438 printf(" read failed, assuming 10BASE-T\n");
442 /*****************************************************************************
444 * Determine full/half duplex. Return half on error.
446 int miiphy_duplex(const char *devname
, unsigned char addr
)
448 u16 bmcr
, anlpar
, adv
;
450 #if defined(CONFIG_PHY_GIGE)
453 /* Check for 1000BASE-X. */
454 if (miiphy_is_1000base_x(devname
, addr
)) {
456 if (miiphy_read(devname
, addr
, MII_LPA
, &anlpar
)) {
457 printf("1000BASE-X PHY AN duplex");
458 goto miiphy_read_failed
;
462 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
464 /* Check for 1000BASE-T. */
465 if (miiphy_read(devname
, addr
, MII_STAT1000
, &btsr
)) {
466 printf("PHY 1000BT status");
467 goto miiphy_read_failed
;
469 if (btsr
!= 0xFFFF) {
470 if (btsr
& PHY_1000BTSR_1000FD
) {
472 } else if (btsr
& PHY_1000BTSR_1000HD
) {
476 #endif /* CONFIG_PHY_GIGE */
478 /* Check Basic Management Control Register first. */
479 if (miiphy_read(devname
, addr
, MII_BMCR
, &bmcr
)) {
481 goto miiphy_read_failed
;
483 /* Check if auto-negotiation is on. */
484 if (bmcr
& BMCR_ANENABLE
) {
485 /* Get auto-negotiation results. */
486 if (miiphy_read(devname
, addr
, MII_LPA
, &anlpar
)) {
487 puts("PHY AN duplex");
488 goto miiphy_read_failed
;
491 if (miiphy_read(devname
, addr
, MII_ADVERTISE
, &adv
)) {
492 puts("PHY AN adv duplex");
493 goto miiphy_read_failed
;
495 return ((anlpar
& adv
) & (LPA_10FULL
| LPA_100FULL
)) ?
498 /* Get speed from basic control settings. */
499 return (bmcr
& BMCR_FULLDPLX
) ? FULL
: HALF
;
502 printf(" read failed, assuming half duplex\n");
506 /*****************************************************************************
508 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
509 * 1000BASE-T, or on error.
511 int miiphy_is_1000base_x(const char *devname
, unsigned char addr
)
513 #if defined(CONFIG_PHY_GIGE)
516 if (miiphy_read(devname
, addr
, MII_ESTATUS
, &exsr
)) {
517 printf("PHY extended status read failed, assuming no "
521 return 0 != (exsr
& (ESTATUS_1000XF
| ESTATUS_1000XH
));
527 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
528 /*****************************************************************************
530 * Determine link status
532 int miiphy_link(const char *devname
, unsigned char addr
)
536 /* dummy read; needed to latch some phys */
537 (void)miiphy_read(devname
, addr
, MII_BMSR
, ®
);
538 if (miiphy_read(devname
, addr
, MII_BMSR
, ®
)) {
539 puts("MII_BMSR read failed, assuming no link\n");
543 /* Determine if a link is active */
544 if ((reg
& BMSR_LSTATUS
) != 0) {