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
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 struct mii_dev *mdio_alloc(void)
72 bus = malloc(sizeof(*bus));
76 memset(bus, 0, sizeof(*bus));
78 /* initalize mii_dev struct fields */
79 INIT_LIST_HEAD(&bus->link);
84 void mdio_free(struct mii_dev *bus)
89 int mdio_register(struct mii_dev *bus)
91 if (!bus || !bus->read || !bus->write)
94 /* check if we have unique name */
95 if (miiphy_get_dev_by_name(bus->name)) {
96 printf("mdio_register: non unique device name '%s'\n",
101 /* add it to the list */
102 list_add_tail(&bus->link, &mii_devs);
110 int mdio_register_seq(struct mii_dev *bus, int seq)
114 /* Setup a unique name for each mdio bus */
115 ret = snprintf(bus->name, MDIO_NAME_LEN, "eth%d", seq);
119 return mdio_register(bus);
122 int mdio_unregister(struct mii_dev *bus)
127 /* delete it from the list */
128 list_del(&bus->link);
130 if (current_mii == bus)
136 void mdio_list_devices(void)
138 struct list_head *entry;
140 list_for_each(entry, &mii_devs) {
142 struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
144 printf("%s:\n", bus->name);
146 for (i = 0; i < PHY_MAX_ADDR; i++) {
147 struct phy_device *phydev = bus->phymap[i];
150 printf("%x - %s", i, phydev->drv->name);
153 printf(" <--> %s\n", phydev->dev->name);
161 int miiphy_set_current_dev(const char *devname)
165 dev = miiphy_get_dev_by_name(devname);
171 printf("No such device: %s\n", devname);
176 struct mii_dev *mdio_get_current_dev(void)
181 struct list_head *mdio_get_list_head(void)
186 struct phy_device *mdio_phydev_for_ethname(const char *ethname)
188 struct list_head *entry;
191 list_for_each(entry, &mii_devs) {
193 bus = list_entry(entry, struct mii_dev, link);
195 for (i = 0; i < PHY_MAX_ADDR; i++) {
196 if (!bus->phymap[i] || !bus->phymap[i]->dev)
199 if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
200 return bus->phymap[i];
204 printf("%s is not a known ethernet\n", ethname);
208 const char *miiphy_get_current_dev(void)
211 return current_mii->name;
216 static struct mii_dev *miiphy_get_active_dev(const char *devname)
218 /* If the current mii is the one we want, return it */
220 if (strcmp(current_mii->name, devname) == 0)
223 /* Otherwise, set the active one to the one we want */
224 if (miiphy_set_current_dev(devname))
230 /*****************************************************************************
232 * Read to variable <value> from the PHY attached to device <devname>,
233 * use PHY address <addr> and register <reg>.
235 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
240 int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
241 unsigned short *value)
246 bus = miiphy_get_active_dev(devname);
250 ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
254 *value = (unsigned short)ret;
258 /*****************************************************************************
260 * Write <value> to the PHY attached to device <devname>,
261 * use PHY address <addr> and register <reg>.
263 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
268 int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
269 unsigned short value)
273 bus = miiphy_get_active_dev(devname);
275 return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
280 /*****************************************************************************
282 * Print out list of registered MII capable devices.
284 void miiphy_listdev(void)
286 struct list_head *entry;
289 puts("MII devices: ");
290 list_for_each(entry, &mii_devs) {
291 dev = list_entry(entry, struct mii_dev, link);
292 printf("'%s' ", dev->name);
297 printf("Current device: '%s'\n", current_mii->name);
300 /*****************************************************************************
302 * Read the OUI, manufacture's model number, and revision number.
304 * OUI: 22 bits (unsigned int)
305 * Model: 6 bits (unsigned char)
306 * Revision: 4 bits (unsigned char)
308 * This API is deprecated.
313 int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
314 unsigned char *model, unsigned char *rev)
316 unsigned int reg = 0;
319 if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
320 debug("PHY ID register 2 read failed\n");
325 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
328 /* No physical device present at this address */
332 if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
333 debug("PHY ID register 1 read failed\n");
337 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
340 *model = (unsigned char)((reg >> 4) & 0x0000003F);
341 *rev = (unsigned char)(reg & 0x0000000F);
345 #ifndef CONFIG_PHYLIB
346 /*****************************************************************************
350 * This API is deprecated. Use PHYLIB.
355 int miiphy_reset(const char *devname, unsigned char addr)
360 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
361 debug("PHY status read failed\n");
364 if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
365 debug("PHY reset failed\n");
368 #ifdef CONFIG_PHY_RESET_DELAY
369 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
372 * Poll the control register for the reset bit to go to 0 (it is
373 * auto-clearing). This should happen within 0.5 seconds per the
377 while (((reg & 0x8000) != 0) && timeout--) {
378 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
379 debug("PHY status read failed\n");
384 if ((reg & 0x8000) == 0) {
387 puts("PHY reset timed out\n");
394 /*****************************************************************************
396 * Determine the ethernet speed (10/100/1000). Return 10 on error.
398 int miiphy_speed(const char *devname, unsigned char addr)
400 u16 bmcr, anlpar, adv;
402 #if defined(CONFIG_PHY_GIGE)
406 * Check for 1000BASE-X. If it is supported, then assume that the speed
409 if (miiphy_is_1000base_x(devname, addr))
413 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
415 /* Check for 1000BASE-T. */
416 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
417 printf("PHY 1000BT status");
418 goto miiphy_read_failed;
420 if (btsr != 0xFFFF &&
421 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
423 #endif /* CONFIG_PHY_GIGE */
425 /* Check Basic Management Control Register first. */
426 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
428 goto miiphy_read_failed;
430 /* Check if auto-negotiation is on. */
431 if (bmcr & BMCR_ANENABLE) {
432 /* Get auto-negotiation results. */
433 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
434 printf("PHY AN speed");
435 goto miiphy_read_failed;
438 if (miiphy_read(devname, addr, MII_ADVERTISE, &adv)) {
439 puts("PHY AN adv speed");
440 goto miiphy_read_failed;
442 return ((anlpar & adv) & LPA_100) ? _100BASET : _10BASET;
444 /* Get speed from basic control settings. */
445 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
448 printf(" read failed, assuming 10BASE-T\n");
452 /*****************************************************************************
454 * Determine full/half duplex. Return half on error.
456 int miiphy_duplex(const char *devname, unsigned char addr)
458 u16 bmcr, anlpar, adv;
460 #if defined(CONFIG_PHY_GIGE)
463 /* Check for 1000BASE-X. */
464 if (miiphy_is_1000base_x(devname, addr)) {
466 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
467 printf("1000BASE-X PHY AN duplex");
468 goto miiphy_read_failed;
472 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
474 /* Check for 1000BASE-T. */
475 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
476 printf("PHY 1000BT status");
477 goto miiphy_read_failed;
479 if (btsr != 0xFFFF) {
480 if (btsr & PHY_1000BTSR_1000FD) {
482 } else if (btsr & PHY_1000BTSR_1000HD) {
486 #endif /* CONFIG_PHY_GIGE */
488 /* Check Basic Management Control Register first. */
489 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
491 goto miiphy_read_failed;
493 /* Check if auto-negotiation is on. */
494 if (bmcr & BMCR_ANENABLE) {
495 /* Get auto-negotiation results. */
496 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
497 puts("PHY AN duplex");
498 goto miiphy_read_failed;
501 if (miiphy_read(devname, addr, MII_ADVERTISE, &adv)) {
502 puts("PHY AN adv duplex");
503 goto miiphy_read_failed;
505 return ((anlpar & adv) & (LPA_10FULL | LPA_100FULL)) ?
508 /* Get speed from basic control settings. */
509 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
512 printf(" read failed, assuming half duplex\n");
516 /*****************************************************************************
518 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
519 * 1000BASE-T, or on error.
521 int miiphy_is_1000base_x(const char *devname, unsigned char addr)
523 #if defined(CONFIG_PHY_GIGE)
526 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
527 printf("PHY extended status read failed, assuming no "
531 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
537 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
538 /*****************************************************************************
540 * Determine link status
542 int miiphy_link(const char *devname, unsigned char addr)
546 /* dummy read; needed to latch some phys */
547 (void)miiphy_read(devname, addr, MII_BMSR, ®);
548 if (miiphy_read(devname, addr, MII_BMSR, ®)) {
549 puts("MII_BMSR read failed, assuming no link\n");
553 /* Determine if a link is active */
554 if ((reg & BMSR_LSTATUS) != 0) {