]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/miiphyutil.c
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
5 * SPDX-License-Identifier: GPL-2.0+
9 * This provides a bit-banged interface to the ethernet MII management
17 #include <asm/types.h>
18 #include <linux/list.h>
22 /* local debug macro */
27 #define debug(fmt, args...) printf(fmt, ##args)
29 #define debug(fmt, args...)
30 #endif /* MII_DEBUG */
32 static struct list_head mii_devs
;
33 static struct mii_dev
*current_mii
;
36 * Lookup the mii_dev struct by the registered device name.
38 struct mii_dev
*miiphy_get_dev_by_name(const char *devname
)
40 struct list_head
*entry
;
44 printf("NULL device name!\n");
48 list_for_each(entry
, &mii_devs
) {
49 dev
= list_entry(entry
, struct mii_dev
, link
);
50 if (strcmp(dev
->name
, devname
) == 0)
57 /*****************************************************************************
59 * Initialize global data. Need to be called before any other miiphy routine.
61 void miiphy_init(void)
63 INIT_LIST_HEAD(&mii_devs
);
67 static int legacy_miiphy_read(struct mii_dev
*bus
, int addr
, int devad
, int reg
)
71 struct legacy_mii_dev
*ldev
= bus
->priv
;
73 ret
= ldev
->read(bus
->name
, addr
, reg
, &val
);
75 return ret
? -1 : (int)val
;
78 static int legacy_miiphy_write(struct mii_dev
*bus
, int addr
, int devad
,
81 struct legacy_mii_dev
*ldev
= bus
->priv
;
83 return ldev
->write(bus
->name
, addr
, reg
, val
);
86 /*****************************************************************************
88 * Register read and write MII access routines for the device <name>.
89 * This API is now deprecated. Please use mdio_alloc and mdio_register, instead.
91 void miiphy_register(const char *name
,
92 int (*read
)(const char *devname
, unsigned char addr
,
93 unsigned char reg
, unsigned short *value
),
94 int (*write
)(const char *devname
, unsigned char addr
,
95 unsigned char reg
, unsigned short value
))
97 struct mii_dev
*new_dev
;
98 struct legacy_mii_dev
*ldev
;
100 BUG_ON(strlen(name
) >= MDIO_NAME_LEN
);
102 /* check if we have unique name */
103 new_dev
= miiphy_get_dev_by_name(name
);
105 printf("miiphy_register: non unique device name '%s'\n", name
);
109 /* allocate memory */
110 new_dev
= mdio_alloc();
111 ldev
= malloc(sizeof(*ldev
));
113 if (new_dev
== NULL
|| ldev
== NULL
) {
114 printf("miiphy_register: cannot allocate memory for '%s'\n",
119 /* initalize mii_dev struct fields */
120 new_dev
->read
= legacy_miiphy_read
;
121 new_dev
->write
= legacy_miiphy_write
;
122 strncpy(new_dev
->name
, name
, MDIO_NAME_LEN
);
123 new_dev
->name
[MDIO_NAME_LEN
- 1] = 0;
126 new_dev
->priv
= ldev
;
128 debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
129 new_dev
->name
, ldev
->read
, ldev
->write
);
131 /* add it to the list */
132 list_add_tail(&new_dev
->link
, &mii_devs
);
135 current_mii
= new_dev
;
138 struct mii_dev
*mdio_alloc(void)
142 bus
= malloc(sizeof(*bus
));
146 memset(bus
, 0, sizeof(*bus
));
148 /* initalize mii_dev struct fields */
149 INIT_LIST_HEAD(&bus
->link
);
154 int mdio_register(struct mii_dev
*bus
)
156 if (!bus
|| !bus
->name
|| !bus
->read
|| !bus
->write
)
159 /* check if we have unique name */
160 if (miiphy_get_dev_by_name(bus
->name
)) {
161 printf("mdio_register: non unique device name '%s'\n",
166 /* add it to the list */
167 list_add_tail(&bus
->link
, &mii_devs
);
175 void mdio_list_devices(void)
177 struct list_head
*entry
;
179 list_for_each(entry
, &mii_devs
) {
181 struct mii_dev
*bus
= list_entry(entry
, struct mii_dev
, link
);
183 printf("%s:\n", bus
->name
);
185 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
186 struct phy_device
*phydev
= bus
->phymap
[i
];
189 printf("%d - %s", i
, phydev
->drv
->name
);
192 printf(" <--> %s\n", phydev
->dev
->name
);
200 int miiphy_set_current_dev(const char *devname
)
204 dev
= miiphy_get_dev_by_name(devname
);
210 printf("No such device: %s\n", devname
);
215 struct mii_dev
*mdio_get_current_dev(void)
220 struct phy_device
*mdio_phydev_for_ethname(const char *ethname
)
222 struct list_head
*entry
;
225 list_for_each(entry
, &mii_devs
) {
227 bus
= list_entry(entry
, struct mii_dev
, link
);
229 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
230 if (!bus
->phymap
[i
] || !bus
->phymap
[i
]->dev
)
233 if (strcmp(bus
->phymap
[i
]->dev
->name
, ethname
) == 0)
234 return bus
->phymap
[i
];
238 printf("%s is not a known ethernet\n", ethname
);
242 const char *miiphy_get_current_dev(void)
245 return current_mii
->name
;
250 static struct mii_dev
*miiphy_get_active_dev(const char *devname
)
252 /* If the current mii is the one we want, return it */
254 if (strcmp(current_mii
->name
, devname
) == 0)
257 /* Otherwise, set the active one to the one we want */
258 if (miiphy_set_current_dev(devname
))
264 /*****************************************************************************
266 * Read to variable <value> from the PHY attached to device <devname>,
267 * use PHY address <addr> and register <reg>.
269 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
274 int miiphy_read(const char *devname
, unsigned char addr
, unsigned char reg
,
275 unsigned short *value
)
280 bus
= miiphy_get_active_dev(devname
);
284 ret
= bus
->read(bus
, addr
, MDIO_DEVAD_NONE
, reg
);
288 *value
= (unsigned short)ret
;
292 /*****************************************************************************
294 * Write <value> to the PHY attached to device <devname>,
295 * use PHY address <addr> and register <reg>.
297 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
302 int miiphy_write(const char *devname
, unsigned char addr
, unsigned char reg
,
303 unsigned short value
)
307 bus
= miiphy_get_active_dev(devname
);
309 return bus
->write(bus
, addr
, MDIO_DEVAD_NONE
, reg
, value
);
314 /*****************************************************************************
316 * Print out list of registered MII capable devices.
318 void miiphy_listdev(void)
320 struct list_head
*entry
;
323 puts("MII devices: ");
324 list_for_each(entry
, &mii_devs
) {
325 dev
= list_entry(entry
, struct mii_dev
, link
);
326 printf("'%s' ", dev
->name
);
331 printf("Current device: '%s'\n", current_mii
->name
);
334 /*****************************************************************************
336 * Read the OUI, manufacture's model number, and revision number.
338 * OUI: 22 bits (unsigned int)
339 * Model: 6 bits (unsigned char)
340 * Revision: 4 bits (unsigned char)
342 * This API is deprecated.
347 int miiphy_info(const char *devname
, unsigned char addr
, unsigned int *oui
,
348 unsigned char *model
, unsigned char *rev
)
350 unsigned int reg
= 0;
353 if (miiphy_read(devname
, addr
, MII_PHYSID2
, &tmp
) != 0) {
354 debug("PHY ID register 2 read failed\n");
359 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr
, reg
);
362 /* No physical device present at this address */
366 if (miiphy_read(devname
, addr
, MII_PHYSID1
, &tmp
) != 0) {
367 debug("PHY ID register 1 read failed\n");
371 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr
, reg
);
374 *model
= (unsigned char)((reg
>> 4) & 0x0000003F);
375 *rev
= (unsigned char)(reg
& 0x0000000F);
379 #ifndef CONFIG_PHYLIB
380 /*****************************************************************************
384 * This API is deprecated. Use PHYLIB.
389 int miiphy_reset(const char *devname
, unsigned char addr
)
394 if (miiphy_read(devname
, addr
, MII_BMCR
, ®
) != 0) {
395 debug("PHY status read failed\n");
398 if (miiphy_write(devname
, addr
, MII_BMCR
, reg
| BMCR_RESET
) != 0) {
399 debug("PHY reset failed\n");
402 #ifdef CONFIG_PHY_RESET_DELAY
403 udelay(CONFIG_PHY_RESET_DELAY
); /* Intel LXT971A needs this */
406 * Poll the control register for the reset bit to go to 0 (it is
407 * auto-clearing). This should happen within 0.5 seconds per the
411 while (((reg
& 0x8000) != 0) && timeout
--) {
412 if (miiphy_read(devname
, addr
, MII_BMCR
, ®
) != 0) {
413 debug("PHY status read failed\n");
418 if ((reg
& 0x8000) == 0) {
421 puts("PHY reset timed out\n");
428 /*****************************************************************************
430 * Determine the ethernet speed (10/100/1000). Return 10 on error.
432 int miiphy_speed(const char *devname
, unsigned char addr
)
436 #if defined(CONFIG_PHY_GIGE)
440 * Check for 1000BASE-X. If it is supported, then assume that the speed
443 if (miiphy_is_1000base_x(devname
, addr
))
447 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
449 /* Check for 1000BASE-T. */
450 if (miiphy_read(devname
, addr
, MII_STAT1000
, &btsr
)) {
451 printf("PHY 1000BT status");
452 goto miiphy_read_failed
;
454 if (btsr
!= 0xFFFF &&
455 (btsr
& (PHY_1000BTSR_1000FD
| PHY_1000BTSR_1000HD
)))
457 #endif /* CONFIG_PHY_GIGE */
459 /* Check Basic Management Control Register first. */
460 if (miiphy_read(devname
, addr
, MII_BMCR
, &bmcr
)) {
462 goto miiphy_read_failed
;
464 /* Check if auto-negotiation is on. */
465 if (bmcr
& BMCR_ANENABLE
) {
466 /* Get auto-negotiation results. */
467 if (miiphy_read(devname
, addr
, MII_LPA
, &anlpar
)) {
468 printf("PHY AN speed");
469 goto miiphy_read_failed
;
471 return (anlpar
& LPA_100
) ? _100BASET
: _10BASET
;
473 /* Get speed from basic control settings. */
474 return (bmcr
& BMCR_SPEED100
) ? _100BASET
: _10BASET
;
477 printf(" read failed, assuming 10BASE-T\n");
481 /*****************************************************************************
483 * Determine full/half duplex. Return half on error.
485 int miiphy_duplex(const char *devname
, unsigned char addr
)
489 #if defined(CONFIG_PHY_GIGE)
492 /* Check for 1000BASE-X. */
493 if (miiphy_is_1000base_x(devname
, addr
)) {
495 if (miiphy_read(devname
, addr
, MII_LPA
, &anlpar
)) {
496 printf("1000BASE-X PHY AN duplex");
497 goto miiphy_read_failed
;
501 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
503 /* Check for 1000BASE-T. */
504 if (miiphy_read(devname
, addr
, MII_STAT1000
, &btsr
)) {
505 printf("PHY 1000BT status");
506 goto miiphy_read_failed
;
508 if (btsr
!= 0xFFFF) {
509 if (btsr
& PHY_1000BTSR_1000FD
) {
511 } else if (btsr
& PHY_1000BTSR_1000HD
) {
515 #endif /* CONFIG_PHY_GIGE */
517 /* Check Basic Management Control Register first. */
518 if (miiphy_read(devname
, addr
, MII_BMCR
, &bmcr
)) {
520 goto miiphy_read_failed
;
522 /* Check if auto-negotiation is on. */
523 if (bmcr
& BMCR_ANENABLE
) {
524 /* Get auto-negotiation results. */
525 if (miiphy_read(devname
, addr
, MII_LPA
, &anlpar
)) {
526 puts("PHY AN duplex");
527 goto miiphy_read_failed
;
529 return (anlpar
& (LPA_10FULL
| LPA_100FULL
)) ?
532 /* Get speed from basic control settings. */
533 return (bmcr
& BMCR_FULLDPLX
) ? FULL
: HALF
;
536 printf(" read failed, assuming half duplex\n");
540 /*****************************************************************************
542 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
543 * 1000BASE-T, or on error.
545 int miiphy_is_1000base_x(const char *devname
, unsigned char addr
)
547 #if defined(CONFIG_PHY_GIGE)
550 if (miiphy_read(devname
, addr
, MII_ESTATUS
, &exsr
)) {
551 printf("PHY extended status read failed, assuming no "
555 return 0 != (exsr
& (ESTATUS_1000XF
| ESTATUS_1000XH
));
561 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
562 /*****************************************************************************
564 * Determine link status
566 int miiphy_link(const char *devname
, unsigned char addr
)
570 /* dummy read; needed to latch some phys */
571 (void)miiphy_read(devname
, addr
, MII_BMSR
, ®
);
572 if (miiphy_read(devname
, addr
, MII_BMSR
, ®
)) {
573 puts("MII_BMSR read failed, assuming no link\n");
577 /* Determine if a link is active */
578 if ((reg
& BMSR_LSTATUS
) != 0) {