3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * This provides a bit-banged interface to the ethernet MII management
33 #include <asm/types.h>
34 #include <linux/list.h>
38 /* local debug macro */
43 #define debug(fmt, args...) printf(fmt, ##args)
45 #define debug(fmt, args...)
46 #endif /* MII_DEBUG */
48 static struct list_head mii_devs;
49 static struct mii_dev *current_mii;
52 * Lookup the mii_dev struct by the registered device name.
54 struct mii_dev *miiphy_get_dev_by_name(const char *devname)
56 struct list_head *entry;
60 printf("NULL device name!\n");
64 list_for_each(entry, &mii_devs) {
65 dev = list_entry(entry, struct mii_dev, link);
66 if (strcmp(dev->name, devname) == 0)
73 /*****************************************************************************
75 * Initialize global data. Need to be called before any other miiphy routine.
77 void miiphy_init(void)
79 INIT_LIST_HEAD(&mii_devs);
83 static int legacy_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
87 struct legacy_mii_dev *ldev = bus->priv;
89 ret = ldev->read(bus->name, addr, reg, &val);
91 return ret ? -1 : (int)val;
94 static int legacy_miiphy_write(struct mii_dev *bus, int addr, int devad,
97 struct legacy_mii_dev *ldev = bus->priv;
99 return ldev->write(bus->name, addr, reg, val);
102 /*****************************************************************************
104 * Register read and write MII access routines for the device <name>.
105 * This API is now deprecated. Please use mdio_alloc and mdio_register, instead.
107 void miiphy_register(const char *name,
108 int (*read)(const char *devname, unsigned char addr,
109 unsigned short reg, unsigned short *value),
110 int (*write)(const char *devname, unsigned char addr,
111 unsigned short reg, unsigned short value))
113 struct mii_dev *new_dev;
114 struct legacy_mii_dev *ldev;
116 BUG_ON(strlen(name) >= MDIO_NAME_LEN);
118 /* check if we have unique name */
119 new_dev = miiphy_get_dev_by_name(name);
121 printf("miiphy_register: non unique device name '%s'\n", name);
125 /* allocate memory */
126 new_dev = mdio_alloc();
127 ldev = malloc(sizeof(*ldev));
129 if (new_dev == NULL || ldev == NULL) {
130 printf("miiphy_register: cannot allocate memory for '%s'\n",
135 /* initalize mii_dev struct fields */
136 new_dev->read = legacy_miiphy_read;
137 new_dev->write = legacy_miiphy_write;
138 strncpy(new_dev->name, name, MDIO_NAME_LEN);
139 new_dev->name[MDIO_NAME_LEN - 1] = 0;
142 new_dev->priv = ldev;
144 debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
145 new_dev->name, ldev->read, ldev->write);
147 /* add it to the list */
148 list_add_tail(&new_dev->link, &mii_devs);
151 current_mii = new_dev;
154 struct mii_dev *mdio_alloc(void)
158 bus = malloc(sizeof(*bus));
162 memset(bus, 0, sizeof(*bus));
164 /* initalize mii_dev struct fields */
165 INIT_LIST_HEAD(&bus->link);
170 int mdio_register(struct mii_dev *bus)
172 if (!bus || !bus->name || !bus->read || !bus->write)
175 /* check if we have unique name */
176 if (miiphy_get_dev_by_name(bus->name)) {
177 printf("mdio_register: non unique device name '%s'\n",
182 /* add it to the list */
183 list_add_tail(&bus->link, &mii_devs);
191 void mdio_list_devices(void)
193 struct list_head *entry;
195 list_for_each(entry, &mii_devs) {
197 struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
199 printf("%s:\n", bus->name);
201 for (i = 0; i < PHY_MAX_ADDR; i++) {
202 struct phy_device *phydev = bus->phymap[i];
205 printf("%d - %s", i, phydev->drv->name);
208 printf(" <--> %s\n", phydev->dev->name);
216 int miiphy_set_current_dev(const char *devname)
220 dev = miiphy_get_dev_by_name(devname);
226 printf("No such device: %s\n", devname);
231 struct mii_dev *mdio_get_current_dev(void)
236 struct phy_device *mdio_phydev_for_ethname(const char *ethname)
238 struct list_head *entry;
241 list_for_each(entry, &mii_devs) {
243 bus = list_entry(entry, struct mii_dev, link);
245 for (i = 0; i < PHY_MAX_ADDR; i++) {
246 if (!bus->phymap[i] || !bus->phymap[i]->dev)
249 if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
250 return bus->phymap[i];
254 printf("%s is not a known ethernet\n", ethname);
258 const char *miiphy_get_current_dev(void)
261 return current_mii->name;
266 static struct mii_dev *miiphy_get_active_dev(const char *devname)
268 /* If the current mii is the one we want, return it */
270 if (strcmp(current_mii->name, devname) == 0)
273 /* Otherwise, set the active one to the one we want */
274 if (miiphy_set_current_dev(devname))
280 /*****************************************************************************
282 * Read to variable <value> from the PHY attached to device <devname>,
283 * use PHY address <addr> and register <reg>.
285 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
290 int miiphy_read(const char *devname, unsigned char addr, unsigned short reg,
291 unsigned short *value)
296 bus = miiphy_get_active_dev(devname);
300 ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
304 *value = (unsigned short)ret;
308 /*****************************************************************************
310 * Write <value> to the PHY attached to device <devname>,
311 * use PHY address <addr> and register <reg>.
313 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
318 int miiphy_write(const char *devname, unsigned char addr, unsigned short reg,
319 unsigned short value)
323 bus = miiphy_get_active_dev(devname);
325 return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
330 /*****************************************************************************
332 * Print out list of registered MII capable devices.
334 void miiphy_listdev(void)
336 struct list_head *entry;
339 puts("MII devices: ");
340 list_for_each(entry, &mii_devs) {
341 dev = list_entry(entry, struct mii_dev, link);
342 printf("'%s' ", dev->name);
347 printf("Current device: '%s'\n", current_mii->name);
350 /*****************************************************************************
352 * Read the OUI, manufacture's model number, and revision number.
354 * OUI: 22 bits (unsigned int)
355 * Model: 6 bits (unsigned char)
356 * Revision: 4 bits (unsigned char)
358 * This API is deprecated.
363 int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
364 unsigned char *model, unsigned char *rev)
366 unsigned int reg = 0;
369 if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
370 debug("PHY ID register 2 read failed\n");
375 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
378 /* No physical device present at this address */
382 if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
383 debug("PHY ID register 1 read failed\n");
387 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
390 *model = (unsigned char)((reg >> 4) & 0x0000003F);
391 *rev = (unsigned char)(reg & 0x0000000F);
395 #ifndef CONFIG_PHYLIB
396 /*****************************************************************************
400 * This API is deprecated. Use PHYLIB.
405 int miiphy_reset(const char *devname, unsigned char addr)
410 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
411 debug("PHY status read failed\n");
414 if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
415 debug("PHY reset failed\n");
418 #ifdef CONFIG_PHY_RESET_DELAY
419 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
422 * Poll the control register for the reset bit to go to 0 (it is
423 * auto-clearing). This should happen within 0.5 seconds per the
427 while (((reg & 0x8000) != 0) && timeout--) {
428 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
429 debug("PHY status read failed\n");
434 if ((reg & 0x8000) == 0) {
437 puts("PHY reset timed out\n");
444 /*****************************************************************************
446 * Determine the ethernet speed (10/100/1000). Return 10 on error.
448 int miiphy_speed(const char *devname, unsigned char addr)
452 #if defined(CONFIG_PHY_GIGE)
456 * Check for 1000BASE-X. If it is supported, then assume that the speed
459 if (miiphy_is_1000base_x(devname, addr))
463 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
465 /* Check for 1000BASE-T. */
466 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
467 printf("PHY 1000BT status");
468 goto miiphy_read_failed;
470 if (btsr != 0xFFFF &&
471 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
473 #endif /* CONFIG_PHY_GIGE */
475 /* Check Basic Management Control Register first. */
476 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
478 goto miiphy_read_failed;
480 /* Check if auto-negotiation is on. */
481 if (bmcr & BMCR_ANENABLE) {
482 /* Get auto-negotiation results. */
483 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
484 printf("PHY AN speed");
485 goto miiphy_read_failed;
487 return (anlpar & LPA_100) ? _100BASET : _10BASET;
489 /* Get speed from basic control settings. */
490 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
493 printf(" read failed, assuming 10BASE-T\n");
497 /*****************************************************************************
499 * Determine full/half duplex. Return half on error.
501 int miiphy_duplex(const char *devname, unsigned char addr)
505 #if defined(CONFIG_PHY_GIGE)
508 /* Check for 1000BASE-X. */
509 if (miiphy_is_1000base_x(devname, addr)) {
511 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
512 printf("1000BASE-X PHY AN duplex");
513 goto miiphy_read_failed;
517 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
519 /* Check for 1000BASE-T. */
520 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
521 printf("PHY 1000BT status");
522 goto miiphy_read_failed;
524 if (btsr != 0xFFFF) {
525 if (btsr & PHY_1000BTSR_1000FD) {
527 } else if (btsr & PHY_1000BTSR_1000HD) {
531 #endif /* CONFIG_PHY_GIGE */
533 /* Check Basic Management Control Register first. */
534 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
536 goto miiphy_read_failed;
538 /* Check if auto-negotiation is on. */
539 if (bmcr & BMCR_ANENABLE) {
540 /* Get auto-negotiation results. */
541 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
542 puts("PHY AN duplex");
543 goto miiphy_read_failed;
545 return (anlpar & (LPA_10FULL | LPA_100FULL)) ?
548 /* Get speed from basic control settings. */
549 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
552 printf(" read failed, assuming half duplex\n");
556 /*****************************************************************************
558 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
559 * 1000BASE-T, or on error.
561 int miiphy_is_1000base_x(const char *devname, unsigned char addr)
563 #if defined(CONFIG_PHY_GIGE)
566 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
567 printf("PHY extended status read failed, assuming no "
571 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
577 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
578 /*****************************************************************************
580 * Determine link status
582 int miiphy_link(const char *devname, unsigned char addr)
586 /* dummy read; needed to latch some phys */
587 (void)miiphy_read(devname, addr, MII_BMSR, ®);
588 if (miiphy_read(devname, addr, MII_BMSR, ®)) {
589 puts("MII_BMSR read failed, assuming no link\n");
593 /* Determine if a link is active */
594 if ((reg & BMSR_LSTATUS) != 0) {