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 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 int mdio_register(struct mii_dev *bus)
157 if (!bus || !bus->name || !bus->read || !bus->write)
160 /* check if we have unique name */
161 if (miiphy_get_dev_by_name(bus->name)) {
162 printf("mdio_register: non unique device name '%s'\n",
167 /* add it to the list */
168 list_add_tail(&bus->link, &mii_devs);
176 void mdio_list_devices(void)
178 struct list_head *entry;
180 list_for_each(entry, &mii_devs) {
182 struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
184 printf("%s:\n", bus->name);
186 for (i = 0; i < PHY_MAX_ADDR; i++) {
187 struct phy_device *phydev = bus->phymap[i];
190 printf("%d - %s", i, phydev->drv->name);
193 printf(" <--> %s\n", phydev->dev->name);
201 int miiphy_set_current_dev(const char *devname)
205 dev = miiphy_get_dev_by_name(devname);
211 printf("No such device: %s\n", devname);
216 struct mii_dev *mdio_get_current_dev(void)
221 struct phy_device *mdio_phydev_for_ethname(const char *ethname)
223 struct list_head *entry;
226 list_for_each(entry, &mii_devs) {
228 bus = list_entry(entry, struct mii_dev, link);
230 for (i = 0; i < PHY_MAX_ADDR; i++) {
231 if (!bus->phymap[i] || !bus->phymap[i]->dev)
234 if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
235 return bus->phymap[i];
239 printf("%s is not a known ethernet\n", ethname);
243 const char *miiphy_get_current_dev(void)
246 return current_mii->name;
251 static struct mii_dev *miiphy_get_active_dev(const char *devname)
253 /* If the current mii is the one we want, return it */
255 if (strcmp(current_mii->name, devname) == 0)
258 /* Otherwise, set the active one to the one we want */
259 if (miiphy_set_current_dev(devname))
265 /*****************************************************************************
267 * Read to variable <value> from the PHY attached to device <devname>,
268 * use PHY address <addr> and register <reg>.
270 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
275 int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
276 unsigned short *value)
281 bus = miiphy_get_active_dev(devname);
285 ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
289 *value = (unsigned short)ret;
293 /*****************************************************************************
295 * Write <value> to the PHY attached to device <devname>,
296 * use PHY address <addr> and register <reg>.
298 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
303 int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
304 unsigned short value)
308 bus = miiphy_get_active_dev(devname);
310 return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
315 /*****************************************************************************
317 * Print out list of registered MII capable devices.
319 void miiphy_listdev(void)
321 struct list_head *entry;
324 puts("MII devices: ");
325 list_for_each(entry, &mii_devs) {
326 dev = list_entry(entry, struct mii_dev, link);
327 printf("'%s' ", dev->name);
332 printf("Current device: '%s'\n", current_mii->name);
335 /*****************************************************************************
337 * Read the OUI, manufacture's model number, and revision number.
339 * OUI: 22 bits (unsigned int)
340 * Model: 6 bits (unsigned char)
341 * Revision: 4 bits (unsigned char)
343 * This API is deprecated.
348 int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
349 unsigned char *model, unsigned char *rev)
351 unsigned int reg = 0;
354 if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
355 debug("PHY ID register 2 read failed\n");
360 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
363 /* No physical device present at this address */
367 if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
368 debug("PHY ID register 1 read failed\n");
372 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
375 *model = (unsigned char)((reg >> 4) & 0x0000003F);
376 *rev = (unsigned char)(reg & 0x0000000F);
380 #ifndef CONFIG_PHYLIB
381 /*****************************************************************************
385 * This API is deprecated. Use PHYLIB.
390 int miiphy_reset(const char *devname, unsigned char addr)
395 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
396 debug("PHY status read failed\n");
399 if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
400 debug("PHY reset failed\n");
403 #ifdef CONFIG_PHY_RESET_DELAY
404 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
407 * Poll the control register for the reset bit to go to 0 (it is
408 * auto-clearing). This should happen within 0.5 seconds per the
412 while (((reg & 0x8000) != 0) && timeout--) {
413 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
414 debug("PHY status read failed\n");
419 if ((reg & 0x8000) == 0) {
422 puts("PHY reset timed out\n");
429 /*****************************************************************************
431 * Determine the ethernet speed (10/100/1000). Return 10 on error.
433 int miiphy_speed(const char *devname, unsigned char addr)
437 #if defined(CONFIG_PHY_GIGE)
441 * Check for 1000BASE-X. If it is supported, then assume that the speed
444 if (miiphy_is_1000base_x(devname, addr))
448 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
450 /* Check for 1000BASE-T. */
451 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
452 printf("PHY 1000BT status");
453 goto miiphy_read_failed;
455 if (btsr != 0xFFFF &&
456 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
458 #endif /* CONFIG_PHY_GIGE */
460 /* Check Basic Management Control Register first. */
461 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
463 goto miiphy_read_failed;
465 /* Check if auto-negotiation is on. */
466 if (bmcr & BMCR_ANENABLE) {
467 /* Get auto-negotiation results. */
468 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
469 printf("PHY AN speed");
470 goto miiphy_read_failed;
472 return (anlpar & LPA_100) ? _100BASET : _10BASET;
474 /* Get speed from basic control settings. */
475 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
478 printf(" read failed, assuming 10BASE-T\n");
482 /*****************************************************************************
484 * Determine full/half duplex. Return half on error.
486 int miiphy_duplex(const char *devname, unsigned char addr)
490 #if defined(CONFIG_PHY_GIGE)
493 /* Check for 1000BASE-X. */
494 if (miiphy_is_1000base_x(devname, addr)) {
496 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
497 printf("1000BASE-X PHY AN duplex");
498 goto miiphy_read_failed;
502 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
504 /* Check for 1000BASE-T. */
505 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
506 printf("PHY 1000BT status");
507 goto miiphy_read_failed;
509 if (btsr != 0xFFFF) {
510 if (btsr & PHY_1000BTSR_1000FD) {
512 } else if (btsr & PHY_1000BTSR_1000HD) {
516 #endif /* CONFIG_PHY_GIGE */
518 /* Check Basic Management Control Register first. */
519 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
521 goto miiphy_read_failed;
523 /* Check if auto-negotiation is on. */
524 if (bmcr & BMCR_ANENABLE) {
525 /* Get auto-negotiation results. */
526 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
527 puts("PHY AN duplex");
528 goto miiphy_read_failed;
530 return (anlpar & (LPA_10FULL | LPA_100FULL)) ?
533 /* Get speed from basic control settings. */
534 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
537 printf(" read failed, assuming half duplex\n");
541 /*****************************************************************************
543 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
544 * 1000BASE-T, or on error.
546 int miiphy_is_1000base_x(const char *devname, unsigned char addr)
548 #if defined(CONFIG_PHY_GIGE)
551 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
552 printf("PHY extended status read failed, assuming no "
556 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
562 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
563 /*****************************************************************************
565 * Determine link status
567 int miiphy_link(const char *devname, unsigned char addr)
571 /* dummy read; needed to latch some phys */
572 (void)miiphy_read(devname, addr, MII_BMSR, ®);
573 if (miiphy_read(devname, addr, MII_BMSR, ®)) {
574 puts("MII_BMSR read failed, assuming no link\n");
578 /* Determine if a link is active */
579 if ((reg & BMSR_LSTATUS) != 0) {