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 <linux/delay.h>
19 #include <asm/types.h>
20 #include <linux/list.h>
24 /* local debug macro */
29 #define debug(fmt, args...) printf(fmt, ##args)
31 #define debug(fmt, args...)
32 #endif /* MII_DEBUG */
34 static struct list_head mii_devs;
35 static struct mii_dev *current_mii;
38 * Lookup the mii_dev struct by the registered device name.
40 struct mii_dev *miiphy_get_dev_by_name(const char *devname)
42 struct list_head *entry;
46 printf("NULL device name!\n");
50 list_for_each(entry, &mii_devs) {
51 dev = list_entry(entry, struct mii_dev, link);
52 if (strcmp(dev->name, devname) == 0)
59 /*****************************************************************************
61 * Initialize global data. Need to be called before any other miiphy routine.
63 void miiphy_init(void)
65 INIT_LIST_HEAD(&mii_devs);
69 struct mii_dev *mdio_alloc(void)
73 bus = malloc(sizeof(*bus));
77 memset(bus, 0, sizeof(*bus));
79 /* initalize mii_dev struct fields */
80 INIT_LIST_HEAD(&bus->link);
85 void mdio_free(struct mii_dev *bus)
90 int mdio_register(struct mii_dev *bus)
92 if (!bus || !bus->read || !bus->write)
95 /* check if we have unique name */
96 if (miiphy_get_dev_by_name(bus->name)) {
97 printf("mdio_register: non unique device name '%s'\n",
102 /* add it to the list */
103 list_add_tail(&bus->link, &mii_devs);
111 int mdio_register_seq(struct mii_dev *bus, int seq)
115 /* Setup a unique name for each mdio bus */
116 ret = snprintf(bus->name, MDIO_NAME_LEN, "eth%d", seq);
120 return mdio_register(bus);
123 int mdio_unregister(struct mii_dev *bus)
128 /* delete it from the list */
129 list_del(&bus->link);
131 if (current_mii == bus)
137 void mdio_list_devices(void)
139 struct list_head *entry;
141 list_for_each(entry, &mii_devs) {
143 struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
145 printf("%s:\n", bus->name);
147 for (i = 0; i < PHY_MAX_ADDR; i++) {
148 struct phy_device *phydev = bus->phymap[i];
151 printf("%x - %s", i, phydev->drv->name);
154 printf(" <--> %s\n", phydev->dev->name);
162 int miiphy_set_current_dev(const char *devname)
166 dev = miiphy_get_dev_by_name(devname);
172 printf("No such device: %s\n", devname);
177 struct mii_dev *mdio_get_current_dev(void)
182 struct list_head *mdio_get_list_head(void)
187 struct phy_device *mdio_phydev_for_ethname(const char *ethname)
189 struct list_head *entry;
192 list_for_each(entry, &mii_devs) {
194 bus = list_entry(entry, struct mii_dev, link);
196 for (i = 0; i < PHY_MAX_ADDR; i++) {
197 if (!bus->phymap[i] || !bus->phymap[i]->dev)
200 if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
201 return bus->phymap[i];
205 printf("%s is not a known ethernet\n", ethname);
209 const char *miiphy_get_current_dev(void)
212 return current_mii->name;
217 static struct mii_dev *miiphy_get_active_dev(const char *devname)
219 /* If the current mii is the one we want, return it */
221 if (strcmp(current_mii->name, devname) == 0)
224 /* Otherwise, set the active one to the one we want */
225 if (miiphy_set_current_dev(devname))
231 /*****************************************************************************
233 * Read to variable <value> from the PHY attached to device <devname>,
234 * use PHY address <addr> and register <reg>.
236 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
241 int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
242 unsigned short *value)
247 bus = miiphy_get_active_dev(devname);
251 ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
255 *value = (unsigned short)ret;
259 /*****************************************************************************
261 * Write <value> to the PHY attached to device <devname>,
262 * use PHY address <addr> and register <reg>.
264 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
269 int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
270 unsigned short value)
274 bus = miiphy_get_active_dev(devname);
276 return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
281 /*****************************************************************************
283 * Print out list of registered MII capable devices.
285 void miiphy_listdev(void)
287 struct list_head *entry;
290 puts("MII devices: ");
291 list_for_each(entry, &mii_devs) {
292 dev = list_entry(entry, struct mii_dev, link);
293 printf("'%s' ", dev->name);
298 printf("Current device: '%s'\n", current_mii->name);
301 /*****************************************************************************
303 * Read the OUI, manufacture's model number, and revision number.
305 * OUI: 22 bits (unsigned int)
306 * Model: 6 bits (unsigned char)
307 * Revision: 4 bits (unsigned char)
309 * This API is deprecated.
314 int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
315 unsigned char *model, unsigned char *rev)
317 unsigned int reg = 0;
320 if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
321 debug("PHY ID register 2 read failed\n");
326 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
329 /* No physical device present at this address */
333 if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
334 debug("PHY ID register 1 read failed\n");
338 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
341 *model = (unsigned char)((reg >> 4) & 0x0000003F);
342 *rev = (unsigned char)(reg & 0x0000000F);
346 #ifndef CONFIG_PHYLIB
347 /*****************************************************************************
351 * This API is deprecated. Use PHYLIB.
356 int miiphy_reset(const char *devname, unsigned char addr)
361 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
362 debug("PHY status read failed\n");
365 if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
366 debug("PHY reset failed\n");
369 #ifdef CONFIG_PHY_RESET_DELAY
370 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
373 * Poll the control register for the reset bit to go to 0 (it is
374 * auto-clearing). This should happen within 0.5 seconds per the
378 while (((reg & 0x8000) != 0) && timeout--) {
379 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
380 debug("PHY status read failed\n");
385 if ((reg & 0x8000) == 0) {
388 puts("PHY reset timed out\n");
395 /*****************************************************************************
397 * Determine the ethernet speed (10/100/1000). Return 10 on error.
399 int miiphy_speed(const char *devname, unsigned char addr)
401 u16 bmcr, anlpar, adv;
403 #if defined(CONFIG_PHY_GIGE)
407 * Check for 1000BASE-X. If it is supported, then assume that the speed
410 if (miiphy_is_1000base_x(devname, addr))
414 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
416 /* Check for 1000BASE-T. */
417 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
418 printf("PHY 1000BT status");
419 goto miiphy_read_failed;
421 if (btsr != 0xFFFF &&
422 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
424 #endif /* CONFIG_PHY_GIGE */
426 /* Check Basic Management Control Register first. */
427 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
429 goto miiphy_read_failed;
431 /* Check if auto-negotiation is on. */
432 if (bmcr & BMCR_ANENABLE) {
433 /* Get auto-negotiation results. */
434 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
435 printf("PHY AN speed");
436 goto miiphy_read_failed;
439 if (miiphy_read(devname, addr, MII_ADVERTISE, &adv)) {
440 puts("PHY AN adv speed");
441 goto miiphy_read_failed;
443 return ((anlpar & adv) & LPA_100) ? _100BASET : _10BASET;
445 /* Get speed from basic control settings. */
446 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
449 printf(" read failed, assuming 10BASE-T\n");
453 /*****************************************************************************
455 * Determine full/half duplex. Return half on error.
457 int miiphy_duplex(const char *devname, unsigned char addr)
459 u16 bmcr, anlpar, adv;
461 #if defined(CONFIG_PHY_GIGE)
464 /* Check for 1000BASE-X. */
465 if (miiphy_is_1000base_x(devname, addr)) {
467 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
468 printf("1000BASE-X PHY AN duplex");
469 goto miiphy_read_failed;
473 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
475 /* Check for 1000BASE-T. */
476 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
477 printf("PHY 1000BT status");
478 goto miiphy_read_failed;
480 if (btsr != 0xFFFF) {
481 if (btsr & PHY_1000BTSR_1000FD) {
483 } else if (btsr & PHY_1000BTSR_1000HD) {
487 #endif /* CONFIG_PHY_GIGE */
489 /* Check Basic Management Control Register first. */
490 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
492 goto miiphy_read_failed;
494 /* Check if auto-negotiation is on. */
495 if (bmcr & BMCR_ANENABLE) {
496 /* Get auto-negotiation results. */
497 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
498 puts("PHY AN duplex");
499 goto miiphy_read_failed;
502 if (miiphy_read(devname, addr, MII_ADVERTISE, &adv)) {
503 puts("PHY AN adv duplex");
504 goto miiphy_read_failed;
506 return ((anlpar & adv) & (LPA_10FULL | LPA_100FULL)) ?
509 /* Get speed from basic control settings. */
510 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
513 printf(" read failed, assuming half duplex\n");
517 /*****************************************************************************
519 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
520 * 1000BASE-T, or on error.
522 int miiphy_is_1000base_x(const char *devname, unsigned char addr)
524 #if defined(CONFIG_PHY_GIGE)
527 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
528 printf("PHY extended status read failed, assuming no "
532 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
538 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
539 /*****************************************************************************
541 * Determine link status
543 int miiphy_link(const char *devname, unsigned char addr)
547 /* dummy read; needed to latch some phys */
548 (void)miiphy_read(devname, addr, MII_BMSR, ®);
549 if (miiphy_read(devname, addr, MII_BMSR, ®)) {
550 puts("MII_BMSR read failed, assuming no link\n");
554 /* Determine if a link is active */
555 if ((reg & BMSR_LSTATUS) != 0) {