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 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_unregister(struct mii_dev *bus)
115 /* delete it from the list */
116 list_del(&bus->link);
118 if (current_mii == bus)
124 void mdio_list_devices(void)
126 struct list_head *entry;
128 list_for_each(entry, &mii_devs) {
130 struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
132 printf("%s:\n", bus->name);
134 for (i = 0; i < PHY_MAX_ADDR; i++) {
135 struct phy_device *phydev = bus->phymap[i];
138 printf("%d - %s", i, phydev->drv->name);
141 printf(" <--> %s\n", phydev->dev->name);
149 int miiphy_set_current_dev(const char *devname)
153 dev = miiphy_get_dev_by_name(devname);
159 printf("No such device: %s\n", devname);
164 struct mii_dev *mdio_get_current_dev(void)
169 struct phy_device *mdio_phydev_for_ethname(const char *ethname)
171 struct list_head *entry;
174 list_for_each(entry, &mii_devs) {
176 bus = list_entry(entry, struct mii_dev, link);
178 for (i = 0; i < PHY_MAX_ADDR; i++) {
179 if (!bus->phymap[i] || !bus->phymap[i]->dev)
182 if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
183 return bus->phymap[i];
187 printf("%s is not a known ethernet\n", ethname);
191 const char *miiphy_get_current_dev(void)
194 return current_mii->name;
199 static struct mii_dev *miiphy_get_active_dev(const char *devname)
201 /* If the current mii is the one we want, return it */
203 if (strcmp(current_mii->name, devname) == 0)
206 /* Otherwise, set the active one to the one we want */
207 if (miiphy_set_current_dev(devname))
213 /*****************************************************************************
215 * Read to variable <value> from the PHY attached to device <devname>,
216 * use PHY address <addr> and register <reg>.
218 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
223 int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
224 unsigned short *value)
229 bus = miiphy_get_active_dev(devname);
233 ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
237 *value = (unsigned short)ret;
241 /*****************************************************************************
243 * Write <value> to the PHY attached to device <devname>,
244 * use PHY address <addr> and register <reg>.
246 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
251 int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
252 unsigned short value)
256 bus = miiphy_get_active_dev(devname);
258 return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
263 /*****************************************************************************
265 * Print out list of registered MII capable devices.
267 void miiphy_listdev(void)
269 struct list_head *entry;
272 puts("MII devices: ");
273 list_for_each(entry, &mii_devs) {
274 dev = list_entry(entry, struct mii_dev, link);
275 printf("'%s' ", dev->name);
280 printf("Current device: '%s'\n", current_mii->name);
283 /*****************************************************************************
285 * Read the OUI, manufacture's model number, and revision number.
287 * OUI: 22 bits (unsigned int)
288 * Model: 6 bits (unsigned char)
289 * Revision: 4 bits (unsigned char)
291 * This API is deprecated.
296 int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
297 unsigned char *model, unsigned char *rev)
299 unsigned int reg = 0;
302 if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
303 debug("PHY ID register 2 read failed\n");
308 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
311 /* No physical device present at this address */
315 if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
316 debug("PHY ID register 1 read failed\n");
320 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
323 *model = (unsigned char)((reg >> 4) & 0x0000003F);
324 *rev = (unsigned char)(reg & 0x0000000F);
328 #ifndef CONFIG_PHYLIB
329 /*****************************************************************************
333 * This API is deprecated. Use PHYLIB.
338 int miiphy_reset(const char *devname, unsigned char addr)
343 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
344 debug("PHY status read failed\n");
347 if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
348 debug("PHY reset failed\n");
351 #ifdef CONFIG_PHY_RESET_DELAY
352 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
355 * Poll the control register for the reset bit to go to 0 (it is
356 * auto-clearing). This should happen within 0.5 seconds per the
360 while (((reg & 0x8000) != 0) && timeout--) {
361 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
362 debug("PHY status read failed\n");
367 if ((reg & 0x8000) == 0) {
370 puts("PHY reset timed out\n");
377 /*****************************************************************************
379 * Determine the ethernet speed (10/100/1000). Return 10 on error.
381 int miiphy_speed(const char *devname, unsigned char addr)
383 u16 bmcr, anlpar, adv;
385 #if defined(CONFIG_PHY_GIGE)
389 * Check for 1000BASE-X. If it is supported, then assume that the speed
392 if (miiphy_is_1000base_x(devname, addr))
396 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
398 /* Check for 1000BASE-T. */
399 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
400 printf("PHY 1000BT status");
401 goto miiphy_read_failed;
403 if (btsr != 0xFFFF &&
404 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
406 #endif /* CONFIG_PHY_GIGE */
408 /* Check Basic Management Control Register first. */
409 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
411 goto miiphy_read_failed;
413 /* Check if auto-negotiation is on. */
414 if (bmcr & BMCR_ANENABLE) {
415 /* Get auto-negotiation results. */
416 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
417 printf("PHY AN speed");
418 goto miiphy_read_failed;
421 if (miiphy_read(devname, addr, MII_ADVERTISE, &adv)) {
422 puts("PHY AN adv speed");
423 goto miiphy_read_failed;
425 return ((anlpar & adv) & LPA_100) ? _100BASET : _10BASET;
427 /* Get speed from basic control settings. */
428 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
431 printf(" read failed, assuming 10BASE-T\n");
435 /*****************************************************************************
437 * Determine full/half duplex. Return half on error.
439 int miiphy_duplex(const char *devname, unsigned char addr)
441 u16 bmcr, anlpar, adv;
443 #if defined(CONFIG_PHY_GIGE)
446 /* Check for 1000BASE-X. */
447 if (miiphy_is_1000base_x(devname, addr)) {
449 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
450 printf("1000BASE-X PHY AN duplex");
451 goto miiphy_read_failed;
455 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
457 /* Check for 1000BASE-T. */
458 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
459 printf("PHY 1000BT status");
460 goto miiphy_read_failed;
462 if (btsr != 0xFFFF) {
463 if (btsr & PHY_1000BTSR_1000FD) {
465 } else if (btsr & PHY_1000BTSR_1000HD) {
469 #endif /* CONFIG_PHY_GIGE */
471 /* Check Basic Management Control Register first. */
472 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
474 goto miiphy_read_failed;
476 /* Check if auto-negotiation is on. */
477 if (bmcr & BMCR_ANENABLE) {
478 /* Get auto-negotiation results. */
479 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
480 puts("PHY AN duplex");
481 goto miiphy_read_failed;
484 if (miiphy_read(devname, addr, MII_ADVERTISE, &adv)) {
485 puts("PHY AN adv duplex");
486 goto miiphy_read_failed;
488 return ((anlpar & adv) & (LPA_10FULL | LPA_100FULL)) ?
491 /* Get speed from basic control settings. */
492 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
495 printf(" read failed, assuming half duplex\n");
499 /*****************************************************************************
501 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
502 * 1000BASE-T, or on error.
504 int miiphy_is_1000base_x(const char *devname, unsigned char addr)
506 #if defined(CONFIG_PHY_GIGE)
509 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
510 printf("PHY extended status read failed, assuming no "
514 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
520 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
521 /*****************************************************************************
523 * Determine link status
525 int miiphy_link(const char *devname, unsigned char addr)
529 /* dummy read; needed to latch some phys */
530 (void)miiphy_read(devname, addr, MII_BMSR, ®);
531 if (miiphy_read(devname, addr, MII_BMSR, ®)) {
532 puts("MII_BMSR read failed, assuming no link\n");
536 /* Determine if a link is active */
537 if ((reg & BMSR_LSTATUS) != 0) {