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>.
106 void miiphy_register(const char *name,
107 int (*read)(const char *devname, unsigned char addr,
108 unsigned char reg, unsigned short *value),
109 int (*write)(const char *devname, unsigned char addr,
110 unsigned char reg, unsigned short value))
112 struct mii_dev *new_dev;
113 struct legacy_mii_dev *ldev;
114 unsigned int name_len;
116 /* check if we have unique name */
117 new_dev = miiphy_get_dev_by_name(name);
119 printf("miiphy_register: non unique device name '%s'\n", name);
123 /* allocate memory */
124 name_len = strlen(name);
125 if (name_len > MDIO_NAME_LEN - 1) {
126 /* Hopefully this won't happen, but if it does, we'll know */
127 printf("miiphy_register: MDIO name was longer than %d\n",
132 new_dev = mdio_alloc();
133 ldev = malloc(sizeof(*ldev));
135 if (new_dev == NULL || ldev == NULL) {
136 printf("miiphy_register: cannot allocate memory for '%s'\n",
141 /* initalize mii_dev struct fields */
142 new_dev->read = legacy_miiphy_read;
143 new_dev->write = legacy_miiphy_write;
144 sprintf(new_dev->name, name);
147 new_dev->priv = ldev;
149 debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
150 new_dev->name, ldev->read, ldev->write);
152 /* add it to the list */
153 list_add_tail(&new_dev->link, &mii_devs);
156 current_mii = new_dev;
159 struct mii_dev *mdio_alloc(void)
163 bus = malloc(sizeof(*bus));
167 memset(bus, 0, sizeof(*bus));
169 /* initalize mii_dev struct fields */
170 INIT_LIST_HEAD(&bus->link);
175 int mdio_register(struct mii_dev *bus)
177 if (!bus || !bus->name || !bus->read || !bus->write)
180 /* check if we have unique name */
181 if (miiphy_get_dev_by_name(bus->name)) {
182 printf("mdio_register: non unique device name '%s'\n",
187 /* add it to the list */
188 list_add_tail(&bus->link, &mii_devs);
196 void mdio_list_devices(void)
198 struct list_head *entry;
200 list_for_each(entry, &mii_devs) {
202 struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
204 printf("%s:\n", bus->name);
206 for (i = 0; i < PHY_MAX_ADDR; i++) {
207 struct phy_device *phydev = bus->phymap[i];
210 printf("%d - %s", i, phydev->drv->name);
213 printf(" <--> %s\n", phydev->dev->name);
221 int miiphy_set_current_dev(const char *devname)
225 dev = miiphy_get_dev_by_name(devname);
231 printf("No such device: %s\n", devname);
236 struct mii_dev *mdio_get_current_dev(void)
241 struct phy_device *mdio_phydev_for_ethname(const char *ethname)
243 struct list_head *entry;
246 list_for_each(entry, &mii_devs) {
248 bus = list_entry(entry, struct mii_dev, link);
250 for (i = 0; i < PHY_MAX_ADDR; i++) {
251 if (!bus->phymap[i] || !bus->phymap[i]->dev)
254 if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
255 return bus->phymap[i];
259 printf("%s is not a known ethernet\n", ethname);
263 const char *miiphy_get_current_dev(void)
266 return current_mii->name;
271 static struct mii_dev *miiphy_get_active_dev(const char *devname)
273 /* If the current mii is the one we want, return it */
275 if (strcmp(current_mii->name, devname) == 0)
278 /* Otherwise, set the active one to the one we want */
279 if (miiphy_set_current_dev(devname))
285 /*****************************************************************************
287 * Read to variable <value> from the PHY attached to device <devname>,
288 * use PHY address <addr> and register <reg>.
293 int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
294 unsigned short *value)
298 bus = miiphy_get_active_dev(devname);
300 *value = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
304 return (*value < 0) ? 1 : 0;
307 /*****************************************************************************
309 * Write <value> to the PHY attached to device <devname>,
310 * use PHY address <addr> and register <reg>.
315 int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
316 unsigned short value)
320 bus = miiphy_get_active_dev(devname);
322 return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
327 /*****************************************************************************
329 * Print out list of registered MII capable devices.
331 void miiphy_listdev(void)
333 struct list_head *entry;
336 puts("MII devices: ");
337 list_for_each(entry, &mii_devs) {
338 dev = list_entry(entry, struct mii_dev, link);
339 printf("'%s' ", dev->name);
344 printf("Current device: '%s'\n", current_mii->name);
347 /*****************************************************************************
349 * Read the OUI, manufacture's model number, and revision number.
351 * OUI: 22 bits (unsigned int)
352 * Model: 6 bits (unsigned char)
353 * Revision: 4 bits (unsigned char)
358 int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
359 unsigned char *model, unsigned char *rev)
361 unsigned int reg = 0;
364 if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
365 debug("PHY ID register 2 read failed\n");
370 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
373 /* No physical device present at this address */
377 if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
378 debug("PHY ID register 1 read failed\n");
382 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
385 *model = (unsigned char)((reg >> 4) & 0x0000003F);
386 *rev = (unsigned char)(reg & 0x0000000F);
390 #ifndef CONFIG_PHYLIB
391 /*****************************************************************************
397 int miiphy_reset(const char *devname, unsigned char addr)
402 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
403 debug("PHY status read failed\n");
406 if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
407 debug("PHY reset failed\n");
410 #ifdef CONFIG_PHY_RESET_DELAY
411 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
414 * Poll the control register for the reset bit to go to 0 (it is
415 * auto-clearing). This should happen within 0.5 seconds per the
419 while (((reg & 0x8000) != 0) && timeout--) {
420 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
421 debug("PHY status read failed\n");
426 if ((reg & 0x8000) == 0) {
429 puts("PHY reset timed out\n");
436 /*****************************************************************************
438 * Determine the ethernet speed (10/100/1000). Return 10 on error.
440 int miiphy_speed(const char *devname, unsigned char addr)
444 #if defined(CONFIG_PHY_GIGE)
448 * Check for 1000BASE-X. If it is supported, then assume that the speed
451 if (miiphy_is_1000base_x(devname, addr))
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 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
465 #endif /* CONFIG_PHY_GIGE */
467 /* Check Basic Management Control Register first. */
468 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
470 goto miiphy_read_failed;
472 /* Check if auto-negotiation is on. */
473 if (bmcr & BMCR_ANENABLE) {
474 /* Get auto-negotiation results. */
475 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
476 printf("PHY AN speed");
477 goto miiphy_read_failed;
479 return (anlpar & LPA_100) ? _100BASET : _10BASET;
481 /* Get speed from basic control settings. */
482 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
485 printf(" read failed, assuming 10BASE-T\n");
489 /*****************************************************************************
491 * Determine full/half duplex. Return half on error.
493 int miiphy_duplex(const char *devname, unsigned char addr)
497 #if defined(CONFIG_PHY_GIGE)
500 /* Check for 1000BASE-X. */
501 if (miiphy_is_1000base_x(devname, addr)) {
503 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
504 printf("1000BASE-X PHY AN duplex");
505 goto miiphy_read_failed;
509 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
511 /* Check for 1000BASE-T. */
512 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
513 printf("PHY 1000BT status");
514 goto miiphy_read_failed;
516 if (btsr != 0xFFFF) {
517 if (btsr & PHY_1000BTSR_1000FD) {
519 } else if (btsr & PHY_1000BTSR_1000HD) {
523 #endif /* CONFIG_PHY_GIGE */
525 /* Check Basic Management Control Register first. */
526 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
528 goto miiphy_read_failed;
530 /* Check if auto-negotiation is on. */
531 if (bmcr & BMCR_ANENABLE) {
532 /* Get auto-negotiation results. */
533 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
534 puts("PHY AN duplex");
535 goto miiphy_read_failed;
537 return (anlpar & (LPA_10FULL | LPA_100FULL)) ?
540 /* Get speed from basic control settings. */
541 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
544 printf(" read failed, assuming half duplex\n");
548 /*****************************************************************************
550 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
551 * 1000BASE-T, or on error.
553 int miiphy_is_1000base_x(const char *devname, unsigned char addr)
555 #if defined(CONFIG_PHY_GIGE)
558 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
559 printf("PHY extended status read failed, assuming no "
563 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
569 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
570 /*****************************************************************************
572 * Determine link status
574 int miiphy_link(const char *devname, unsigned char addr)
578 /* dummy read; needed to latch some phys */
579 (void)miiphy_read(devname, addr, MII_BMSR, ®);
580 if (miiphy_read(devname, addr, MII_BMSR, ®)) {
581 puts("MII_BMSR read failed, assuming no link\n");
585 /* Determine if a link is active */
586 if ((reg & BMSR_LSTATUS) != 0) {