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_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 phy_device *mdio_phydev_for_ethname(const char *ethname)
183 struct list_head *entry;
186 list_for_each(entry, &mii_devs) {
188 bus = list_entry(entry, struct mii_dev, link);
190 for (i = 0; i < PHY_MAX_ADDR; i++) {
191 if (!bus->phymap[i] || !bus->phymap[i]->dev)
194 if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
195 return bus->phymap[i];
199 printf("%s is not a known ethernet\n", ethname);
203 const char *miiphy_get_current_dev(void)
206 return current_mii->name;
211 static struct mii_dev *miiphy_get_active_dev(const char *devname)
213 /* If the current mii is the one we want, return it */
215 if (strcmp(current_mii->name, devname) == 0)
218 /* Otherwise, set the active one to the one we want */
219 if (miiphy_set_current_dev(devname))
225 /*****************************************************************************
227 * Read to variable <value> from the PHY attached to device <devname>,
228 * use PHY address <addr> and register <reg>.
230 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
235 int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
236 unsigned short *value)
241 bus = miiphy_get_active_dev(devname);
245 ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
249 *value = (unsigned short)ret;
253 /*****************************************************************************
255 * Write <value> to the PHY attached to device <devname>,
256 * use PHY address <addr> and register <reg>.
258 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
263 int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
264 unsigned short value)
268 bus = miiphy_get_active_dev(devname);
270 return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
275 /*****************************************************************************
277 * Print out list of registered MII capable devices.
279 void miiphy_listdev(void)
281 struct list_head *entry;
284 puts("MII devices: ");
285 list_for_each(entry, &mii_devs) {
286 dev = list_entry(entry, struct mii_dev, link);
287 printf("'%s' ", dev->name);
292 printf("Current device: '%s'\n", current_mii->name);
295 /*****************************************************************************
297 * Read the OUI, manufacture's model number, and revision number.
299 * OUI: 22 bits (unsigned int)
300 * Model: 6 bits (unsigned char)
301 * Revision: 4 bits (unsigned char)
303 * This API is deprecated.
308 int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
309 unsigned char *model, unsigned char *rev)
311 unsigned int reg = 0;
314 if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
315 debug("PHY ID register 2 read failed\n");
320 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
323 /* No physical device present at this address */
327 if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
328 debug("PHY ID register 1 read failed\n");
332 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
335 *model = (unsigned char)((reg >> 4) & 0x0000003F);
336 *rev = (unsigned char)(reg & 0x0000000F);
340 #ifndef CONFIG_PHYLIB
341 /*****************************************************************************
345 * This API is deprecated. Use PHYLIB.
350 int miiphy_reset(const char *devname, unsigned char addr)
355 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
356 debug("PHY status read failed\n");
359 if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
360 debug("PHY reset failed\n");
363 #ifdef CONFIG_PHY_RESET_DELAY
364 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
367 * Poll the control register for the reset bit to go to 0 (it is
368 * auto-clearing). This should happen within 0.5 seconds per the
372 while (((reg & 0x8000) != 0) && timeout--) {
373 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
374 debug("PHY status read failed\n");
379 if ((reg & 0x8000) == 0) {
382 puts("PHY reset timed out\n");
389 /*****************************************************************************
391 * Determine the ethernet speed (10/100/1000). Return 10 on error.
393 int miiphy_speed(const char *devname, unsigned char addr)
395 u16 bmcr, anlpar, adv;
397 #if defined(CONFIG_PHY_GIGE)
401 * Check for 1000BASE-X. If it is supported, then assume that the speed
404 if (miiphy_is_1000base_x(devname, addr))
408 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
410 /* Check for 1000BASE-T. */
411 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
412 printf("PHY 1000BT status");
413 goto miiphy_read_failed;
415 if (btsr != 0xFFFF &&
416 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
418 #endif /* CONFIG_PHY_GIGE */
420 /* Check Basic Management Control Register first. */
421 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
423 goto miiphy_read_failed;
425 /* Check if auto-negotiation is on. */
426 if (bmcr & BMCR_ANENABLE) {
427 /* Get auto-negotiation results. */
428 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
429 printf("PHY AN speed");
430 goto miiphy_read_failed;
433 if (miiphy_read(devname, addr, MII_ADVERTISE, &adv)) {
434 puts("PHY AN adv speed");
435 goto miiphy_read_failed;
437 return ((anlpar & adv) & LPA_100) ? _100BASET : _10BASET;
439 /* Get speed from basic control settings. */
440 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
443 printf(" read failed, assuming 10BASE-T\n");
447 /*****************************************************************************
449 * Determine full/half duplex. Return half on error.
451 int miiphy_duplex(const char *devname, unsigned char addr)
453 u16 bmcr, anlpar, adv;
455 #if defined(CONFIG_PHY_GIGE)
458 /* Check for 1000BASE-X. */
459 if (miiphy_is_1000base_x(devname, addr)) {
461 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
462 printf("1000BASE-X PHY AN duplex");
463 goto miiphy_read_failed;
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 if (btsr & PHY_1000BTSR_1000FD) {
477 } else if (btsr & PHY_1000BTSR_1000HD) {
481 #endif /* CONFIG_PHY_GIGE */
483 /* Check Basic Management Control Register first. */
484 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
486 goto miiphy_read_failed;
488 /* Check if auto-negotiation is on. */
489 if (bmcr & BMCR_ANENABLE) {
490 /* Get auto-negotiation results. */
491 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
492 puts("PHY AN duplex");
493 goto miiphy_read_failed;
496 if (miiphy_read(devname, addr, MII_ADVERTISE, &adv)) {
497 puts("PHY AN adv duplex");
498 goto miiphy_read_failed;
500 return ((anlpar & adv) & (LPA_10FULL | LPA_100FULL)) ?
503 /* Get speed from basic control settings. */
504 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
507 printf(" read failed, assuming half duplex\n");
511 /*****************************************************************************
513 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
514 * 1000BASE-T, or on error.
516 int miiphy_is_1000base_x(const char *devname, unsigned char addr)
518 #if defined(CONFIG_PHY_GIGE)
521 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
522 printf("PHY extended status read failed, assuming no "
526 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
532 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
533 /*****************************************************************************
535 * Determine link status
537 int miiphy_link(const char *devname, unsigned char addr)
541 /* dummy read; needed to latch some phys */
542 (void)miiphy_read(devname, addr, MII_BMSR, ®);
543 if (miiphy_read(devname, addr, MII_BMSR, ®)) {
544 puts("MII_BMSR read failed, assuming no link\n");
548 /* Determine if a link is active */
549 if ((reg & BMSR_LSTATUS) != 0) {