]>
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
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 struct 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 /*****************************************************************************
60 * Initialize global data. Need to be called before any other miiphy routine.
62 void miiphy_init(void)
64 INIT_LIST_HEAD(&mii_devs
);
68 static int legacy_miiphy_read(struct mii_dev
*bus
, int addr
, int devad
, int reg
)
72 struct legacy_mii_dev
*ldev
= bus
->priv
;
74 ret
= ldev
->read(bus
->name
, addr
, reg
, &val
);
76 return ret
? -1 : (int)val
;
79 static int legacy_miiphy_write(struct mii_dev
*bus
, int addr
, int devad
,
82 struct legacy_mii_dev
*ldev
= bus
->priv
;
84 return ldev
->write(bus
->name
, addr
, reg
, val
);
87 /*****************************************************************************
89 * Register read and write MII access routines for the device <name>.
90 * This API is now deprecated. Please use mdio_alloc and mdio_register, instead.
92 void miiphy_register(const char *name
,
93 int (*read
)(const char *devname
, unsigned char addr
,
94 unsigned char reg
, unsigned short *value
),
95 int (*write
)(const char *devname
, unsigned char addr
,
96 unsigned char reg
, unsigned short value
))
98 struct mii_dev
*new_dev
;
99 struct legacy_mii_dev
*ldev
;
101 BUG_ON(strlen(name
) >= MDIO_NAME_LEN
);
103 /* check if we have unique name */
104 new_dev
= miiphy_get_dev_by_name(name
);
106 printf("miiphy_register: non unique device name '%s'\n", name
);
110 /* allocate memory */
111 new_dev
= mdio_alloc();
112 ldev
= malloc(sizeof(*ldev
));
114 if (new_dev
== NULL
|| ldev
== NULL
) {
115 printf("miiphy_register: cannot allocate memory for '%s'\n",
122 /* initalize mii_dev struct fields */
123 new_dev
->read
= legacy_miiphy_read
;
124 new_dev
->write
= legacy_miiphy_write
;
125 strncpy(new_dev
->name
, name
, MDIO_NAME_LEN
);
126 new_dev
->name
[MDIO_NAME_LEN
- 1] = 0;
129 new_dev
->priv
= ldev
;
131 debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
132 new_dev
->name
, ldev
->read
, ldev
->write
);
134 /* add it to the list */
135 list_add_tail(&new_dev
->link
, &mii_devs
);
138 current_mii
= new_dev
;
141 struct mii_dev
*mdio_alloc(void)
145 bus
= malloc(sizeof(*bus
));
149 memset(bus
, 0, sizeof(*bus
));
151 /* initalize mii_dev struct fields */
152 INIT_LIST_HEAD(&bus
->link
);
157 void mdio_free(struct mii_dev
*bus
)
162 int mdio_register(struct mii_dev
*bus
)
164 if (!bus
|| !bus
->read
|| !bus
->write
)
167 /* check if we have unique name */
168 if (miiphy_get_dev_by_name(bus
->name
)) {
169 printf("mdio_register: non unique device name '%s'\n",
174 /* add it to the list */
175 list_add_tail(&bus
->link
, &mii_devs
);
183 int mdio_unregister(struct mii_dev
*bus
)
188 /* delete it from the list */
189 list_del(&bus
->link
);
191 if (current_mii
== bus
)
197 void mdio_list_devices(void)
199 struct list_head
*entry
;
201 list_for_each(entry
, &mii_devs
) {
203 struct mii_dev
*bus
= list_entry(entry
, struct mii_dev
, link
);
205 printf("%s:\n", bus
->name
);
207 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
208 struct phy_device
*phydev
= bus
->phymap
[i
];
211 printf("%d - %s", i
, phydev
->drv
->name
);
214 printf(" <--> %s\n", phydev
->dev
->name
);
222 int miiphy_set_current_dev(const char *devname
)
226 dev
= miiphy_get_dev_by_name(devname
);
232 printf("No such device: %s\n", devname
);
237 struct mii_dev
*mdio_get_current_dev(void)
242 struct phy_device
*mdio_phydev_for_ethname(const char *ethname
)
244 struct list_head
*entry
;
247 list_for_each(entry
, &mii_devs
) {
249 bus
= list_entry(entry
, struct mii_dev
, link
);
251 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
252 if (!bus
->phymap
[i
] || !bus
->phymap
[i
]->dev
)
255 if (strcmp(bus
->phymap
[i
]->dev
->name
, ethname
) == 0)
256 return bus
->phymap
[i
];
260 printf("%s is not a known ethernet\n", ethname
);
264 const char *miiphy_get_current_dev(void)
267 return current_mii
->name
;
272 static struct mii_dev
*miiphy_get_active_dev(const char *devname
)
274 /* If the current mii is the one we want, return it */
276 if (strcmp(current_mii
->name
, devname
) == 0)
279 /* Otherwise, set the active one to the one we want */
280 if (miiphy_set_current_dev(devname
))
286 /*****************************************************************************
288 * Read to variable <value> from the PHY attached to device <devname>,
289 * use PHY address <addr> and register <reg>.
291 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
296 int miiphy_read(const char *devname
, unsigned char addr
, unsigned char reg
,
297 unsigned short *value
)
302 bus
= miiphy_get_active_dev(devname
);
306 ret
= bus
->read(bus
, addr
, MDIO_DEVAD_NONE
, reg
);
310 *value
= (unsigned short)ret
;
314 /*****************************************************************************
316 * Write <value> to the PHY attached to device <devname>,
317 * use PHY address <addr> and register <reg>.
319 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
324 int miiphy_write(const char *devname
, unsigned char addr
, unsigned char reg
,
325 unsigned short value
)
329 bus
= miiphy_get_active_dev(devname
);
331 return bus
->write(bus
, addr
, MDIO_DEVAD_NONE
, reg
, value
);
336 /*****************************************************************************
338 * Print out list of registered MII capable devices.
340 void miiphy_listdev(void)
342 struct list_head
*entry
;
345 puts("MII devices: ");
346 list_for_each(entry
, &mii_devs
) {
347 dev
= list_entry(entry
, struct mii_dev
, link
);
348 printf("'%s' ", dev
->name
);
353 printf("Current device: '%s'\n", current_mii
->name
);
356 /*****************************************************************************
358 * Read the OUI, manufacture's model number, and revision number.
360 * OUI: 22 bits (unsigned int)
361 * Model: 6 bits (unsigned char)
362 * Revision: 4 bits (unsigned char)
364 * This API is deprecated.
369 int miiphy_info(const char *devname
, unsigned char addr
, unsigned int *oui
,
370 unsigned char *model
, unsigned char *rev
)
372 unsigned int reg
= 0;
375 if (miiphy_read(devname
, addr
, MII_PHYSID2
, &tmp
) != 0) {
376 debug("PHY ID register 2 read failed\n");
381 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr
, reg
);
384 /* No physical device present at this address */
388 if (miiphy_read(devname
, addr
, MII_PHYSID1
, &tmp
) != 0) {
389 debug("PHY ID register 1 read failed\n");
393 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr
, reg
);
396 *model
= (unsigned char)((reg
>> 4) & 0x0000003F);
397 *rev
= (unsigned char)(reg
& 0x0000000F);
401 #ifndef CONFIG_PHYLIB
402 /*****************************************************************************
406 * This API is deprecated. Use PHYLIB.
411 int miiphy_reset(const char *devname
, unsigned char addr
)
416 if (miiphy_read(devname
, addr
, MII_BMCR
, ®
) != 0) {
417 debug("PHY status read failed\n");
420 if (miiphy_write(devname
, addr
, MII_BMCR
, reg
| BMCR_RESET
) != 0) {
421 debug("PHY reset failed\n");
424 #ifdef CONFIG_PHY_RESET_DELAY
425 udelay(CONFIG_PHY_RESET_DELAY
); /* Intel LXT971A needs this */
428 * Poll the control register for the reset bit to go to 0 (it is
429 * auto-clearing). This should happen within 0.5 seconds per the
433 while (((reg
& 0x8000) != 0) && timeout
--) {
434 if (miiphy_read(devname
, addr
, MII_BMCR
, ®
) != 0) {
435 debug("PHY status read failed\n");
440 if ((reg
& 0x8000) == 0) {
443 puts("PHY reset timed out\n");
450 /*****************************************************************************
452 * Determine the ethernet speed (10/100/1000). Return 10 on error.
454 int miiphy_speed(const char *devname
, unsigned char addr
)
458 #if defined(CONFIG_PHY_GIGE)
462 * Check for 1000BASE-X. If it is supported, then assume that the speed
465 if (miiphy_is_1000base_x(devname
, addr
))
469 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
471 /* Check for 1000BASE-T. */
472 if (miiphy_read(devname
, addr
, MII_STAT1000
, &btsr
)) {
473 printf("PHY 1000BT status");
474 goto miiphy_read_failed
;
476 if (btsr
!= 0xFFFF &&
477 (btsr
& (PHY_1000BTSR_1000FD
| PHY_1000BTSR_1000HD
)))
479 #endif /* CONFIG_PHY_GIGE */
481 /* Check Basic Management Control Register first. */
482 if (miiphy_read(devname
, addr
, MII_BMCR
, &bmcr
)) {
484 goto miiphy_read_failed
;
486 /* Check if auto-negotiation is on. */
487 if (bmcr
& BMCR_ANENABLE
) {
488 /* Get auto-negotiation results. */
489 if (miiphy_read(devname
, addr
, MII_LPA
, &anlpar
)) {
490 printf("PHY AN speed");
491 goto miiphy_read_failed
;
493 return (anlpar
& LPA_100
) ? _100BASET
: _10BASET
;
495 /* Get speed from basic control settings. */
496 return (bmcr
& BMCR_SPEED100
) ? _100BASET
: _10BASET
;
499 printf(" read failed, assuming 10BASE-T\n");
503 /*****************************************************************************
505 * Determine full/half duplex. Return half on error.
507 int miiphy_duplex(const char *devname
, unsigned char addr
)
511 #if defined(CONFIG_PHY_GIGE)
514 /* Check for 1000BASE-X. */
515 if (miiphy_is_1000base_x(devname
, addr
)) {
517 if (miiphy_read(devname
, addr
, MII_LPA
, &anlpar
)) {
518 printf("1000BASE-X PHY AN duplex");
519 goto miiphy_read_failed
;
523 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
525 /* Check for 1000BASE-T. */
526 if (miiphy_read(devname
, addr
, MII_STAT1000
, &btsr
)) {
527 printf("PHY 1000BT status");
528 goto miiphy_read_failed
;
530 if (btsr
!= 0xFFFF) {
531 if (btsr
& PHY_1000BTSR_1000FD
) {
533 } else if (btsr
& PHY_1000BTSR_1000HD
) {
537 #endif /* CONFIG_PHY_GIGE */
539 /* Check Basic Management Control Register first. */
540 if (miiphy_read(devname
, addr
, MII_BMCR
, &bmcr
)) {
542 goto miiphy_read_failed
;
544 /* Check if auto-negotiation is on. */
545 if (bmcr
& BMCR_ANENABLE
) {
546 /* Get auto-negotiation results. */
547 if (miiphy_read(devname
, addr
, MII_LPA
, &anlpar
)) {
548 puts("PHY AN duplex");
549 goto miiphy_read_failed
;
551 return (anlpar
& (LPA_10FULL
| LPA_100FULL
)) ?
554 /* Get speed from basic control settings. */
555 return (bmcr
& BMCR_FULLDPLX
) ? FULL
: HALF
;
558 printf(" read failed, assuming half duplex\n");
562 /*****************************************************************************
564 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
565 * 1000BASE-T, or on error.
567 int miiphy_is_1000base_x(const char *devname
, unsigned char addr
)
569 #if defined(CONFIG_PHY_GIGE)
572 if (miiphy_read(devname
, addr
, MII_ESTATUS
, &exsr
)) {
573 printf("PHY extended status read failed, assuming no "
577 return 0 != (exsr
& (ESTATUS_1000XF
| ESTATUS_1000XH
));
583 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
584 /*****************************************************************************
586 * Determine link status
588 int miiphy_link(const char *devname
, unsigned char addr
)
592 /* dummy read; needed to latch some phys */
593 (void)miiphy_read(devname
, addr
, MII_BMSR
, ®
);
594 if (miiphy_read(devname
, addr
, MII_BMSR
, ®
)) {
595 puts("MII_BMSR read failed, assuming no link\n");
599 /* Determine if a link is active */
600 if ((reg
& BMSR_LSTATUS
) != 0) {