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
32 #include <asm/types.h>
33 #include <linux/list.h>
37 /* local debug macro */
42 #define debug(fmt,args...) printf (fmt ,##args)
44 #define debug(fmt,args...)
45 #endif /* MII_DEBUG */
48 struct list_head link;
50 int (*read) (const char *devname, unsigned char addr,
51 unsigned char reg, unsigned short *value);
52 int (*write) (const char *devname, unsigned char addr,
53 unsigned char reg, unsigned short value);
56 static struct list_head mii_devs;
57 static struct mii_dev *current_mii;
60 * Lookup the mii_dev struct by the registered device name.
62 static struct mii_dev *miiphy_get_dev_by_name(const char *devname, int quiet)
64 struct list_head *entry;
68 printf("NULL device name!\n");
72 list_for_each(entry, &mii_devs) {
73 dev = list_entry(entry, struct mii_dev, link);
74 if (strcmp(dev->name, devname) == 0)
79 printf("No such device: %s\n", devname);
83 /*****************************************************************************
85 * Initialize global data. Need to be called before any other miiphy routine.
87 void miiphy_init(void)
89 INIT_LIST_HEAD (&mii_devs);
93 /*****************************************************************************
95 * Register read and write MII access routines for the device <name>.
97 void miiphy_register(const char *name,
98 int (*read) (const char *devname, unsigned char addr,
99 unsigned char reg, unsigned short *value),
100 int (*write) (const char *devname, unsigned char addr,
101 unsigned char reg, unsigned short value))
103 struct mii_dev *new_dev;
104 unsigned int name_len;
107 /* check if we have unique name */
108 new_dev = miiphy_get_dev_by_name(name, 1);
110 printf("miiphy_register: non unique device name '%s'\n", name);
114 /* allocate memory */
115 name_len = strlen (name);
117 (struct mii_dev *)malloc (sizeof (struct mii_dev) + name_len + 1);
119 if (new_dev == NULL) {
120 printf ("miiphy_register: cannot allocate memory for '%s'\n",
124 memset (new_dev, 0, sizeof (struct mii_dev) + name_len);
126 /* initalize mii_dev struct fields */
127 INIT_LIST_HEAD (&new_dev->link);
128 new_dev->read = read;
129 new_dev->write = write;
130 new_dev->name = new_name = (char *)(new_dev + 1);
131 strncpy (new_name, name, name_len);
132 new_name[name_len] = '\0';
134 debug ("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
135 new_dev->name, new_dev->read, new_dev->write);
137 /* add it to the list */
138 list_add_tail (&new_dev->link, &mii_devs);
141 current_mii = new_dev;
144 int miiphy_set_current_dev(const char *devname)
148 dev = miiphy_get_dev_by_name(devname, 0);
157 const char *miiphy_get_current_dev(void)
160 return current_mii->name;
165 static struct mii_dev *miiphy_get_active_dev(const char *devname)
167 /* If the current mii is the one we want, return it */
169 if (strcmp(current_mii->name, devname) == 0)
172 /* Otherwise, set the active one to the one we want */
173 if (miiphy_set_current_dev(devname))
179 /*****************************************************************************
181 * Read to variable <value> from the PHY attached to device <devname>,
182 * use PHY address <addr> and register <reg>.
187 int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
188 unsigned short *value)
192 dev = miiphy_get_active_dev(devname);
194 return dev->read(devname, addr, reg, value);
199 /*****************************************************************************
201 * Write <value> to the PHY attached to device <devname>,
202 * use PHY address <addr> and register <reg>.
207 int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
208 unsigned short value)
212 dev = miiphy_get_active_dev(devname);
214 return dev->write(devname, addr, reg, value);
219 /*****************************************************************************
221 * Print out list of registered MII capable devices.
223 void miiphy_listdev (void)
225 struct list_head *entry;
228 puts ("MII devices: ");
229 list_for_each (entry, &mii_devs) {
230 dev = list_entry (entry, struct mii_dev, link);
231 printf ("'%s' ", dev->name);
236 printf ("Current device: '%s'\n", current_mii->name);
239 /*****************************************************************************
241 * Read the OUI, manufacture's model number, and revision number.
243 * OUI: 22 bits (unsigned int)
244 * Model: 6 bits (unsigned char)
245 * Revision: 4 bits (unsigned char)
250 int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
251 unsigned char *model, unsigned char *rev)
253 unsigned int reg = 0;
256 if (miiphy_read (devname, addr, PHY_PHYIDR2, &tmp) != 0) {
257 debug ("PHY ID register 2 read failed\n");
262 debug ("PHY_PHYIDR2 @ 0x%x = 0x%04x\n", addr, reg);
265 /* No physical device present at this address */
269 if (miiphy_read (devname, addr, PHY_PHYIDR1, &tmp) != 0) {
270 debug ("PHY ID register 1 read failed\n");
274 debug ("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
277 *model = (unsigned char)((reg >> 4) & 0x0000003F);
278 *rev = (unsigned char)(reg & 0x0000000F);
282 /*****************************************************************************
288 int miiphy_reset(const char *devname, unsigned char addr)
293 if (miiphy_read (devname, addr, PHY_BMCR, ®) != 0) {
294 debug ("PHY status read failed\n");
297 if (miiphy_write (devname, addr, PHY_BMCR, reg | PHY_BMCR_RESET) != 0) {
298 debug ("PHY reset failed\n");
301 #ifdef CONFIG_PHY_RESET_DELAY
302 udelay (CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
305 * Poll the control register for the reset bit to go to 0 (it is
306 * auto-clearing). This should happen within 0.5 seconds per the
310 while (((reg & 0x8000) != 0) && timeout--) {
311 if (miiphy_read(devname, addr, PHY_BMCR, ®) != 0) {
312 debug("PHY status read failed\n");
317 if ((reg & 0x8000) == 0) {
320 puts ("PHY reset timed out\n");
326 /*****************************************************************************
328 * Determine the ethernet speed (10/100/1000). Return 10 on error.
330 int miiphy_speed(const char *devname, unsigned char addr)
334 #if defined(CONFIG_PHY_GIGE)
338 * Check for 1000BASE-X. If it is supported, then assume that the speed
341 if (miiphy_is_1000base_x (devname, addr)) {
345 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
347 /* Check for 1000BASE-T. */
348 if (miiphy_read (devname, addr, PHY_1000BTSR, &btsr)) {
349 printf ("PHY 1000BT status");
350 goto miiphy_read_failed;
352 if (btsr != 0xFFFF &&
353 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD))) {
356 #endif /* CONFIG_PHY_GIGE */
358 /* Check Basic Management Control Register first. */
359 if (miiphy_read (devname, addr, PHY_BMCR, &bmcr)) {
360 printf ("PHY speed");
361 goto miiphy_read_failed;
363 /* Check if auto-negotiation is on. */
364 if (bmcr & PHY_BMCR_AUTON) {
365 /* Get auto-negotiation results. */
366 if (miiphy_read (devname, addr, PHY_ANLPAR, &anlpar)) {
367 printf ("PHY AN speed");
368 goto miiphy_read_failed;
370 return (anlpar & PHY_ANLPAR_100) ? _100BASET : _10BASET;
372 /* Get speed from basic control settings. */
373 return (bmcr & PHY_BMCR_100MB) ? _100BASET : _10BASET;
376 printf (" read failed, assuming 10BASE-T\n");
380 /*****************************************************************************
382 * Determine full/half duplex. Return half on error.
384 int miiphy_duplex(const char *devname, unsigned char addr)
388 #if defined(CONFIG_PHY_GIGE)
391 /* Check for 1000BASE-X. */
392 if (miiphy_is_1000base_x (devname, addr)) {
394 if (miiphy_read (devname, addr, PHY_ANLPAR, &anlpar)) {
395 printf ("1000BASE-X PHY AN duplex");
396 goto miiphy_read_failed;
400 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
402 /* Check for 1000BASE-T. */
403 if (miiphy_read (devname, addr, PHY_1000BTSR, &btsr)) {
404 printf ("PHY 1000BT status");
405 goto miiphy_read_failed;
407 if (btsr != 0xFFFF) {
408 if (btsr & PHY_1000BTSR_1000FD) {
410 } else if (btsr & PHY_1000BTSR_1000HD) {
414 #endif /* CONFIG_PHY_GIGE */
416 /* Check Basic Management Control Register first. */
417 if (miiphy_read (devname, addr, PHY_BMCR, &bmcr)) {
419 goto miiphy_read_failed;
421 /* Check if auto-negotiation is on. */
422 if (bmcr & PHY_BMCR_AUTON) {
423 /* Get auto-negotiation results. */
424 if (miiphy_read (devname, addr, PHY_ANLPAR, &anlpar)) {
425 puts ("PHY AN duplex");
426 goto miiphy_read_failed;
428 return (anlpar & (PHY_ANLPAR_10FD | PHY_ANLPAR_TXFD)) ?
431 /* Get speed from basic control settings. */
432 return (bmcr & PHY_BMCR_DPLX) ? FULL : HALF;
435 printf (" read failed, assuming half duplex\n");
439 /*****************************************************************************
441 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
442 * 1000BASE-T, or on error.
444 int miiphy_is_1000base_x(const char *devname, unsigned char addr)
446 #if defined(CONFIG_PHY_GIGE)
449 if (miiphy_read (devname, addr, PHY_EXSR, &exsr)) {
450 printf ("PHY extended status read failed, assuming no "
454 return 0 != (exsr & (PHY_EXSR_1000XF | PHY_EXSR_1000XH));
460 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
461 /*****************************************************************************
463 * Determine link status
465 int miiphy_link(const char *devname, unsigned char addr)
469 /* dummy read; needed to latch some phys */
470 (void)miiphy_read (devname, addr, PHY_BMSR, ®);
471 if (miiphy_read (devname, addr, PHY_BMSR, ®)) {
472 puts ("PHY_BMSR read failed, assuming no link\n");
476 /* Determine if a link is active */
477 if ((reg & PHY_BMSR_LS) != 0) {