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",
120 /* initalize mii_dev struct fields */
121 new_dev->read = legacy_miiphy_read;
122 new_dev->write = legacy_miiphy_write;
123 strncpy(new_dev->name, name, MDIO_NAME_LEN);
124 new_dev->name[MDIO_NAME_LEN - 1] = 0;
127 new_dev->priv = ldev;
129 debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
130 new_dev->name, ldev->read, ldev->write);
132 /* add it to the list */
133 list_add_tail(&new_dev->link, &mii_devs);
136 current_mii = new_dev;
139 struct mii_dev *mdio_alloc(void)
143 bus = malloc(sizeof(*bus));
147 memset(bus, 0, sizeof(*bus));
149 /* initalize mii_dev struct fields */
150 INIT_LIST_HEAD(&bus->link);
155 void mdio_free(struct mii_dev *bus)
160 int mdio_register(struct mii_dev *bus)
162 if (!bus || !bus->name || !bus->read || !bus->write)
165 /* check if we have unique name */
166 if (miiphy_get_dev_by_name(bus->name)) {
167 printf("mdio_register: non unique device name '%s'\n",
172 /* add it to the list */
173 list_add_tail(&bus->link, &mii_devs);
181 int mdio_unregister(struct mii_dev *bus)
186 /* delete it from the list */
187 list_del(&bus->link);
189 if (current_mii == bus)
195 void mdio_list_devices(void)
197 struct list_head *entry;
199 list_for_each(entry, &mii_devs) {
201 struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
203 printf("%s:\n", bus->name);
205 for (i = 0; i < PHY_MAX_ADDR; i++) {
206 struct phy_device *phydev = bus->phymap[i];
209 printf("%d - %s", i, phydev->drv->name);
212 printf(" <--> %s\n", phydev->dev->name);
220 int miiphy_set_current_dev(const char *devname)
224 dev = miiphy_get_dev_by_name(devname);
230 printf("No such device: %s\n", devname);
235 struct mii_dev *mdio_get_current_dev(void)
240 struct phy_device *mdio_phydev_for_ethname(const char *ethname)
242 struct list_head *entry;
245 list_for_each(entry, &mii_devs) {
247 bus = list_entry(entry, struct mii_dev, link);
249 for (i = 0; i < PHY_MAX_ADDR; i++) {
250 if (!bus->phymap[i] || !bus->phymap[i]->dev)
253 if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
254 return bus->phymap[i];
258 printf("%s is not a known ethernet\n", ethname);
262 const char *miiphy_get_current_dev(void)
265 return current_mii->name;
270 static struct mii_dev *miiphy_get_active_dev(const char *devname)
272 /* If the current mii is the one we want, return it */
274 if (strcmp(current_mii->name, devname) == 0)
277 /* Otherwise, set the active one to the one we want */
278 if (miiphy_set_current_dev(devname))
284 /*****************************************************************************
286 * Read to variable <value> from the PHY attached to device <devname>,
287 * use PHY address <addr> and register <reg>.
289 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
294 int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
295 unsigned short *value)
300 bus = miiphy_get_active_dev(devname);
304 ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
308 *value = (unsigned short)ret;
312 /*****************************************************************************
314 * Write <value> to the PHY attached to device <devname>,
315 * use PHY address <addr> and register <reg>.
317 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
322 int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
323 unsigned short value)
327 bus = miiphy_get_active_dev(devname);
329 return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
334 /*****************************************************************************
336 * Print out list of registered MII capable devices.
338 void miiphy_listdev(void)
340 struct list_head *entry;
343 puts("MII devices: ");
344 list_for_each(entry, &mii_devs) {
345 dev = list_entry(entry, struct mii_dev, link);
346 printf("'%s' ", dev->name);
351 printf("Current device: '%s'\n", current_mii->name);
354 /*****************************************************************************
356 * Read the OUI, manufacture's model number, and revision number.
358 * OUI: 22 bits (unsigned int)
359 * Model: 6 bits (unsigned char)
360 * Revision: 4 bits (unsigned char)
362 * This API is deprecated.
367 int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
368 unsigned char *model, unsigned char *rev)
370 unsigned int reg = 0;
373 if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
374 debug("PHY ID register 2 read failed\n");
379 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
382 /* No physical device present at this address */
386 if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
387 debug("PHY ID register 1 read failed\n");
391 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
394 *model = (unsigned char)((reg >> 4) & 0x0000003F);
395 *rev = (unsigned char)(reg & 0x0000000F);
399 #ifndef CONFIG_PHYLIB
400 /*****************************************************************************
404 * This API is deprecated. Use PHYLIB.
409 int miiphy_reset(const char *devname, unsigned char addr)
414 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
415 debug("PHY status read failed\n");
418 if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
419 debug("PHY reset failed\n");
422 #ifdef CONFIG_PHY_RESET_DELAY
423 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
426 * Poll the control register for the reset bit to go to 0 (it is
427 * auto-clearing). This should happen within 0.5 seconds per the
431 while (((reg & 0x8000) != 0) && timeout--) {
432 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
433 debug("PHY status read failed\n");
438 if ((reg & 0x8000) == 0) {
441 puts("PHY reset timed out\n");
448 /*****************************************************************************
450 * Determine the ethernet speed (10/100/1000). Return 10 on error.
452 int miiphy_speed(const char *devname, unsigned char addr)
456 #if defined(CONFIG_PHY_GIGE)
460 * Check for 1000BASE-X. If it is supported, then assume that the speed
463 if (miiphy_is_1000base_x(devname, addr))
467 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
469 /* Check for 1000BASE-T. */
470 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
471 printf("PHY 1000BT status");
472 goto miiphy_read_failed;
474 if (btsr != 0xFFFF &&
475 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
477 #endif /* CONFIG_PHY_GIGE */
479 /* Check Basic Management Control Register first. */
480 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
482 goto miiphy_read_failed;
484 /* Check if auto-negotiation is on. */
485 if (bmcr & BMCR_ANENABLE) {
486 /* Get auto-negotiation results. */
487 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
488 printf("PHY AN speed");
489 goto miiphy_read_failed;
491 return (anlpar & LPA_100) ? _100BASET : _10BASET;
493 /* Get speed from basic control settings. */
494 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
497 printf(" read failed, assuming 10BASE-T\n");
501 /*****************************************************************************
503 * Determine full/half duplex. Return half on error.
505 int miiphy_duplex(const char *devname, unsigned char addr)
509 #if defined(CONFIG_PHY_GIGE)
512 /* Check for 1000BASE-X. */
513 if (miiphy_is_1000base_x(devname, addr)) {
515 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
516 printf("1000BASE-X PHY AN duplex");
517 goto miiphy_read_failed;
521 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
523 /* Check for 1000BASE-T. */
524 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
525 printf("PHY 1000BT status");
526 goto miiphy_read_failed;
528 if (btsr != 0xFFFF) {
529 if (btsr & PHY_1000BTSR_1000FD) {
531 } else if (btsr & PHY_1000BTSR_1000HD) {
535 #endif /* CONFIG_PHY_GIGE */
537 /* Check Basic Management Control Register first. */
538 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
540 goto miiphy_read_failed;
542 /* Check if auto-negotiation is on. */
543 if (bmcr & BMCR_ANENABLE) {
544 /* Get auto-negotiation results. */
545 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
546 puts("PHY AN duplex");
547 goto miiphy_read_failed;
549 return (anlpar & (LPA_10FULL | LPA_100FULL)) ?
552 /* Get speed from basic control settings. */
553 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
556 printf(" read failed, assuming half duplex\n");
560 /*****************************************************************************
562 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
563 * 1000BASE-T, or on error.
565 int miiphy_is_1000base_x(const char *devname, unsigned char addr)
567 #if defined(CONFIG_PHY_GIGE)
570 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
571 printf("PHY extended status read failed, assuming no "
575 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
581 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
582 /*****************************************************************************
584 * Determine link status
586 int miiphy_link(const char *devname, unsigned char addr)
590 /* dummy read; needed to latch some phys */
591 (void)miiphy_read(devname, addr, MII_BMSR, ®);
592 if (miiphy_read(devname, addr, MII_BMSR, ®)) {
593 puts("MII_BMSR read failed, assuming no link\n");
597 /* Determine if a link is active */
598 if ((reg & BMSR_LSTATUS) != 0) {