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;
115 BUG_ON(strlen(name) >= MDIO_NAME_LEN);
117 /* check if we have unique name */
118 new_dev = miiphy_get_dev_by_name(name);
120 printf("miiphy_register: non unique device name '%s'\n", name);
124 /* allocate memory */
125 new_dev = mdio_alloc();
126 ldev = malloc(sizeof(*ldev));
128 if (new_dev == NULL || ldev == NULL) {
129 printf("miiphy_register: cannot allocate memory for '%s'\n",
134 /* initalize mii_dev struct fields */
135 new_dev->read = legacy_miiphy_read;
136 new_dev->write = legacy_miiphy_write;
137 strncpy(new_dev->name, name, MDIO_NAME_LEN);
138 new_dev->name[MDIO_NAME_LEN - 1] = 0;
141 new_dev->priv = ldev;
143 debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
144 new_dev->name, ldev->read, ldev->write);
146 /* add it to the list */
147 list_add_tail(&new_dev->link, &mii_devs);
150 current_mii = new_dev;
153 struct mii_dev *mdio_alloc(void)
157 bus = malloc(sizeof(*bus));
161 memset(bus, 0, sizeof(*bus));
163 /* initalize mii_dev struct fields */
164 INIT_LIST_HEAD(&bus->link);
169 int mdio_register(struct mii_dev *bus)
171 if (!bus || !bus->name || !bus->read || !bus->write)
174 /* check if we have unique name */
175 if (miiphy_get_dev_by_name(bus->name)) {
176 printf("mdio_register: non unique device name '%s'\n",
181 /* add it to the list */
182 list_add_tail(&bus->link, &mii_devs);
190 void mdio_list_devices(void)
192 struct list_head *entry;
194 list_for_each(entry, &mii_devs) {
196 struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
198 printf("%s:\n", bus->name);
200 for (i = 0; i < PHY_MAX_ADDR; i++) {
201 struct phy_device *phydev = bus->phymap[i];
204 printf("%d - %s", i, phydev->drv->name);
207 printf(" <--> %s\n", phydev->dev->name);
215 int miiphy_set_current_dev(const char *devname)
219 dev = miiphy_get_dev_by_name(devname);
225 printf("No such device: %s\n", devname);
230 struct mii_dev *mdio_get_current_dev(void)
235 struct phy_device *mdio_phydev_for_ethname(const char *ethname)
237 struct list_head *entry;
240 list_for_each(entry, &mii_devs) {
242 bus = list_entry(entry, struct mii_dev, link);
244 for (i = 0; i < PHY_MAX_ADDR; i++) {
245 if (!bus->phymap[i] || !bus->phymap[i]->dev)
248 if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
249 return bus->phymap[i];
253 printf("%s is not a known ethernet\n", ethname);
257 const char *miiphy_get_current_dev(void)
260 return current_mii->name;
265 static struct mii_dev *miiphy_get_active_dev(const char *devname)
267 /* If the current mii is the one we want, return it */
269 if (strcmp(current_mii->name, devname) == 0)
272 /* Otherwise, set the active one to the one we want */
273 if (miiphy_set_current_dev(devname))
279 /*****************************************************************************
281 * Read to variable <value> from the PHY attached to device <devname>,
282 * use PHY address <addr> and register <reg>.
287 int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
288 unsigned short *value)
293 bus = miiphy_get_active_dev(devname);
297 ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
301 *value = (unsigned short)ret;
305 /*****************************************************************************
307 * Write <value> to the PHY attached to device <devname>,
308 * use PHY address <addr> and register <reg>.
313 int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
314 unsigned short value)
318 bus = miiphy_get_active_dev(devname);
320 return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
325 /*****************************************************************************
327 * Print out list of registered MII capable devices.
329 void miiphy_listdev(void)
331 struct list_head *entry;
334 puts("MII devices: ");
335 list_for_each(entry, &mii_devs) {
336 dev = list_entry(entry, struct mii_dev, link);
337 printf("'%s' ", dev->name);
342 printf("Current device: '%s'\n", current_mii->name);
345 /*****************************************************************************
347 * Read the OUI, manufacture's model number, and revision number.
349 * OUI: 22 bits (unsigned int)
350 * Model: 6 bits (unsigned char)
351 * Revision: 4 bits (unsigned char)
356 int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
357 unsigned char *model, unsigned char *rev)
359 unsigned int reg = 0;
362 if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
363 debug("PHY ID register 2 read failed\n");
368 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
371 /* No physical device present at this address */
375 if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
376 debug("PHY ID register 1 read failed\n");
380 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
383 *model = (unsigned char)((reg >> 4) & 0x0000003F);
384 *rev = (unsigned char)(reg & 0x0000000F);
388 #ifndef CONFIG_PHYLIB
389 /*****************************************************************************
395 int miiphy_reset(const char *devname, unsigned char addr)
400 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
401 debug("PHY status read failed\n");
404 if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
405 debug("PHY reset failed\n");
408 #ifdef CONFIG_PHY_RESET_DELAY
409 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
412 * Poll the control register for the reset bit to go to 0 (it is
413 * auto-clearing). This should happen within 0.5 seconds per the
417 while (((reg & 0x8000) != 0) && timeout--) {
418 if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) {
419 debug("PHY status read failed\n");
424 if ((reg & 0x8000) == 0) {
427 puts("PHY reset timed out\n");
434 /*****************************************************************************
436 * Determine the ethernet speed (10/100/1000). Return 10 on error.
438 int miiphy_speed(const char *devname, unsigned char addr)
442 #if defined(CONFIG_PHY_GIGE)
446 * Check for 1000BASE-X. If it is supported, then assume that the speed
449 if (miiphy_is_1000base_x(devname, addr))
453 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
455 /* Check for 1000BASE-T. */
456 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
457 printf("PHY 1000BT status");
458 goto miiphy_read_failed;
460 if (btsr != 0xFFFF &&
461 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
463 #endif /* CONFIG_PHY_GIGE */
465 /* Check Basic Management Control Register first. */
466 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
468 goto miiphy_read_failed;
470 /* Check if auto-negotiation is on. */
471 if (bmcr & BMCR_ANENABLE) {
472 /* Get auto-negotiation results. */
473 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
474 printf("PHY AN speed");
475 goto miiphy_read_failed;
477 return (anlpar & LPA_100) ? _100BASET : _10BASET;
479 /* Get speed from basic control settings. */
480 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
483 printf(" read failed, assuming 10BASE-T\n");
487 /*****************************************************************************
489 * Determine full/half duplex. Return half on error.
491 int miiphy_duplex(const char *devname, unsigned char addr)
495 #if defined(CONFIG_PHY_GIGE)
498 /* Check for 1000BASE-X. */
499 if (miiphy_is_1000base_x(devname, addr)) {
501 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
502 printf("1000BASE-X PHY AN duplex");
503 goto miiphy_read_failed;
507 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
509 /* Check for 1000BASE-T. */
510 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
511 printf("PHY 1000BT status");
512 goto miiphy_read_failed;
514 if (btsr != 0xFFFF) {
515 if (btsr & PHY_1000BTSR_1000FD) {
517 } else if (btsr & PHY_1000BTSR_1000HD) {
521 #endif /* CONFIG_PHY_GIGE */
523 /* Check Basic Management Control Register first. */
524 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
526 goto miiphy_read_failed;
528 /* Check if auto-negotiation is on. */
529 if (bmcr & BMCR_ANENABLE) {
530 /* Get auto-negotiation results. */
531 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
532 puts("PHY AN duplex");
533 goto miiphy_read_failed;
535 return (anlpar & (LPA_10FULL | LPA_100FULL)) ?
538 /* Get speed from basic control settings. */
539 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
542 printf(" read failed, assuming half duplex\n");
546 /*****************************************************************************
548 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
549 * 1000BASE-T, or on error.
551 int miiphy_is_1000base_x(const char *devname, unsigned char addr)
553 #if defined(CONFIG_PHY_GIGE)
556 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
557 printf("PHY extended status read failed, assuming no "
561 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
567 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
568 /*****************************************************************************
570 * Determine link status
572 int miiphy_link(const char *devname, unsigned char addr)
576 /* dummy read; needed to latch some phys */
577 (void)miiphy_read(devname, addr, MII_BMSR, ®);
578 if (miiphy_read(devname, addr, MII_BMSR, ®)) {
579 puts("MII_BMSR read failed, assuming no link\n");
583 /* Determine if a link is active */
584 if ((reg & BMSR_LSTATUS) != 0) {