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
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 struct mii_dev *mdio_alloc(void)
71 bus = malloc(sizeof(*bus));
75 memset(bus, 0, sizeof(*bus));
77 /* initalize mii_dev struct fields */
78 INIT_LIST_HEAD(&bus->link);
83 void mdio_free(struct mii_dev *bus)
88 int mdio_register(struct mii_dev *bus)
90 if (!bus || !bus->read || !bus->write)
93 /* check if we have unique name */
94 if (miiphy_get_dev_by_name(bus->name)) {
95 printf("mdio_register: non unique device name '%s'\n",
100 /* add it to the list */
101 list_add_tail(&bus->link, &mii_devs);
109 int mdio_register_seq(struct mii_dev *bus, int seq)
113 /* Setup a unique name for each mdio bus */
114 ret = snprintf(bus->name, MDIO_NAME_LEN, "eth%d", seq);
118 return mdio_register(bus);
121 int mdio_unregister(struct mii_dev *bus)
126 /* delete it from the list */
127 list_del(&bus->link);
129 if (current_mii == bus)
135 void mdio_list_devices(void)
137 struct list_head *entry;
139 list_for_each(entry, &mii_devs) {
141 struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
143 printf("%s:\n", bus->name);
145 for (i = 0; i < PHY_MAX_ADDR; i++) {
146 struct phy_device *phydev = bus->phymap[i];
149 printf("%x - %s", i, phydev->drv->name);
152 printf(" <--> %s\n", phydev->dev->name);
160 int miiphy_set_current_dev(const char *devname)
164 dev = miiphy_get_dev_by_name(devname);
170 printf("No such device: %s\n", devname);
175 struct mii_dev *mdio_get_current_dev(void)
180 struct phy_device *mdio_phydev_for_ethname(const char *ethname)
182 struct list_head *entry;
185 list_for_each(entry, &mii_devs) {
187 bus = list_entry(entry, struct mii_dev, link);
189 for (i = 0; i < PHY_MAX_ADDR; i++) {
190 if (!bus->phymap[i] || !bus->phymap[i]->dev)
193 if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
194 return bus->phymap[i];
198 printf("%s is not a known ethernet\n", ethname);
202 const char *miiphy_get_current_dev(void)
205 return current_mii->name;
210 static struct mii_dev *miiphy_get_active_dev(const char *devname)
212 /* If the current mii is the one we want, return it */
214 if (strcmp(current_mii->name, devname) == 0)
217 /* Otherwise, set the active one to the one we want */
218 if (miiphy_set_current_dev(devname))
224 /*****************************************************************************
226 * Read to variable <value> from the PHY attached to device <devname>,
227 * use PHY address <addr> and register <reg>.
229 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
234 int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
235 unsigned short *value)
240 bus = miiphy_get_active_dev(devname);
244 ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
248 *value = (unsigned short)ret;
252 /*****************************************************************************
254 * Write <value> to the PHY attached to device <devname>,
255 * use PHY address <addr> and register <reg>.
257 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
262 int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
263 unsigned short value)
267 bus = miiphy_get_active_dev(devname);
269 return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
274 /*****************************************************************************
276 * Print out list of registered MII capable devices.
278 void miiphy_listdev(void)
280 struct list_head *entry;
283 puts("MII devices: ");
284 list_for_each(entry, &mii_devs) {
285 dev = list_entry(entry, struct mii_dev, link);
286 printf("'%s' ", dev->name);
291 printf("Current device: '%s'\n", current_mii->name);
294 /*****************************************************************************
296 * Read the OUI, manufacture's model number, and revision number.
298 * OUI: 22 bits (unsigned int)
299 * Model: 6 bits (unsigned char)
300 * Revision: 4 bits (unsigned char)
302 * This API is deprecated.
307 int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
308 unsigned char *model, unsigned char *rev)
310 unsigned int reg = 0;
313 if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
314 debug("PHY ID register 2 read failed\n");
319 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
322 /* No physical device present at this address */
326 if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
327 debug("PHY ID register 1 read failed\n");
331 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
334 *model = (unsigned char)((reg >> 4) & 0x0000003F);
335 *rev = (unsigned char)(reg & 0x0000000F);
339 #ifndef CONFIG_PHYLIB
340 /*****************************************************************************
344 * This API is deprecated. Use PHYLIB.
349 int miiphy_reset(const char *devname, unsigned char addr)
354 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
355 debug("PHY status read failed\n");
358 if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
359 debug("PHY reset failed\n");
362 #ifdef CONFIG_PHY_RESET_DELAY
363 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
366 * Poll the control register for the reset bit to go to 0 (it is
367 * auto-clearing). This should happen within 0.5 seconds per the
371 while (((reg & 0x8000) != 0) && timeout--) {
372 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
373 debug("PHY status read failed\n");
378 if ((reg & 0x8000) == 0) {
381 puts("PHY reset timed out\n");
388 /*****************************************************************************
390 * Determine the ethernet speed (10/100/1000). Return 10 on error.
392 int miiphy_speed(const char *devname, unsigned char addr)
394 u16 bmcr, anlpar, adv;
396 #if defined(CONFIG_PHY_GIGE)
400 * Check for 1000BASE-X. If it is supported, then assume that the speed
403 if (miiphy_is_1000base_x(devname, addr))
407 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
409 /* Check for 1000BASE-T. */
410 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
411 printf("PHY 1000BT status");
412 goto miiphy_read_failed;
414 if (btsr != 0xFFFF &&
415 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
417 #endif /* CONFIG_PHY_GIGE */
419 /* Check Basic Management Control Register first. */
420 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
422 goto miiphy_read_failed;
424 /* Check if auto-negotiation is on. */
425 if (bmcr & BMCR_ANENABLE) {
426 /* Get auto-negotiation results. */
427 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
428 printf("PHY AN speed");
429 goto miiphy_read_failed;
432 if (miiphy_read(devname, addr, MII_ADVERTISE, &adv)) {
433 puts("PHY AN adv speed");
434 goto miiphy_read_failed;
436 return ((anlpar & adv) & LPA_100) ? _100BASET : _10BASET;
438 /* Get speed from basic control settings. */
439 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
442 printf(" read failed, assuming 10BASE-T\n");
446 /*****************************************************************************
448 * Determine full/half duplex. Return half on error.
450 int miiphy_duplex(const char *devname, unsigned char addr)
452 u16 bmcr, anlpar, adv;
454 #if defined(CONFIG_PHY_GIGE)
457 /* Check for 1000BASE-X. */
458 if (miiphy_is_1000base_x(devname, addr)) {
460 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
461 printf("1000BASE-X PHY AN duplex");
462 goto miiphy_read_failed;
466 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
468 /* Check for 1000BASE-T. */
469 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
470 printf("PHY 1000BT status");
471 goto miiphy_read_failed;
473 if (btsr != 0xFFFF) {
474 if (btsr & PHY_1000BTSR_1000FD) {
476 } else if (btsr & PHY_1000BTSR_1000HD) {
480 #endif /* CONFIG_PHY_GIGE */
482 /* Check Basic Management Control Register first. */
483 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
485 goto miiphy_read_failed;
487 /* Check if auto-negotiation is on. */
488 if (bmcr & BMCR_ANENABLE) {
489 /* Get auto-negotiation results. */
490 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
491 puts("PHY AN duplex");
492 goto miiphy_read_failed;
495 if (miiphy_read(devname, addr, MII_ADVERTISE, &adv)) {
496 puts("PHY AN adv duplex");
497 goto miiphy_read_failed;
499 return ((anlpar & adv) & (LPA_10FULL | LPA_100FULL)) ?
502 /* Get speed from basic control settings. */
503 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
506 printf(" read failed, assuming half duplex\n");
510 /*****************************************************************************
512 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
513 * 1000BASE-T, or on error.
515 int miiphy_is_1000base_x(const char *devname, unsigned char addr)
517 #if defined(CONFIG_PHY_GIGE)
520 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
521 printf("PHY extended status read failed, assuming no "
525 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
531 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
532 /*****************************************************************************
534 * Determine link status
536 int miiphy_link(const char *devname, unsigned char addr)
540 /* dummy read; needed to latch some phys */
541 (void)miiphy_read(devname, addr, MII_BMSR, ®);
542 if (miiphy_read(devname, addr, MII_BMSR, ®)) {
543 puts("MII_BMSR read failed, assuming no link\n");
547 /* Determine if a link is active */
548 if ((reg & BMSR_LSTATUS) != 0) {