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 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
33 #include <asm/types.h>
34 #include <linux/list.h>
38 /* local debug macro */
44 #define debug(fmt,args...) printf (fmt ,##args)
46 #define debug(fmt,args...)
47 #endif /* MII_DEBUG */
50 struct list_head link;
52 int (* read)(char *devname, unsigned char addr,
53 unsigned char reg, unsigned short *value);
54 int (* write)(char *devname, unsigned char addr,
55 unsigned char reg, unsigned short value);
58 static struct list_head mii_devs;
59 static struct mii_dev *current_mii;
61 /*****************************************************************************
63 * Initialize global data. Need to be called before any other miiphy routine.
67 INIT_LIST_HEAD(&mii_devs);
71 /*****************************************************************************
73 * Register read and write MII access routines for the device <name>.
75 void miiphy_register(char *name,
76 int (* read)(char *devname, unsigned char addr,
77 unsigned char reg, unsigned short *value),
78 int (* write)(char *devname, unsigned char addr,
79 unsigned char reg, unsigned short value))
81 struct list_head *entry;
82 struct mii_dev *new_dev;
83 struct mii_dev *miidev;
84 unsigned int name_len;
86 /* check if we have unique name */
87 list_for_each(entry, &mii_devs) {
88 miidev = list_entry(entry, struct mii_dev, link);
89 if (strcmp(miidev->name, name) == 0) {
90 printf("miiphy_register: non unique device name '%s'\n",
97 name_len = strlen(name);
98 new_dev = (struct mii_dev *)malloc(sizeof(struct mii_dev) + name_len + 1);
100 if(new_dev == NULL) {
101 printf("miiphy_register: cannot allocate memory for '%s'\n",
105 memset(new_dev, 0, sizeof(struct mii_dev) + name_len);
107 /* initalize mii_dev struct fields */
108 INIT_LIST_HEAD(&new_dev->link);
109 new_dev->read = read;
110 new_dev->write = write;
111 new_dev->name = (char *)(new_dev + 1);
112 strncpy(new_dev->name, name, name_len);
113 new_dev->name[name_len] = '\0';
115 debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
116 new_dev->name, new_dev->read, new_dev->write);
118 /* add it to the list */
119 list_add_tail(&new_dev->link, &mii_devs);
122 current_mii = new_dev;
125 int miiphy_set_current_dev(char *devname)
127 struct list_head *entry;
130 list_for_each(entry, &mii_devs) {
131 dev = list_entry(entry, struct mii_dev, link);
133 if (strcmp(devname, dev->name) == 0) {
139 printf("No such device: %s\n", devname);
143 char *miiphy_get_current_dev()
146 return current_mii->name;
151 /*****************************************************************************
153 * Read to variable <value> from the PHY attached to device <devname>,
154 * use PHY address <addr> and register <reg>.
159 int miiphy_read(char *devname, unsigned char addr, unsigned char reg,
160 unsigned short *value)
162 struct list_head *entry;
168 printf("NULL device name!\n");
172 list_for_each(entry, &mii_devs) {
173 dev = list_entry(entry, struct mii_dev, link);
175 if (strcmp(devname, dev->name) == 0) {
177 read_ret = dev->read(devname, addr, reg, value);
183 printf("No such device: %s\n", devname);
185 return ((found_dev) ? read_ret : 1);
188 /*****************************************************************************
190 * Write <value> to the PHY attached to device <devname>,
191 * use PHY address <addr> and register <reg>.
196 int miiphy_write(char *devname, unsigned char addr, unsigned char reg,
197 unsigned short value)
199 struct list_head *entry;
205 printf("NULL device name!\n");
209 list_for_each(entry, &mii_devs) {
210 dev = list_entry(entry, struct mii_dev, link);
212 if (strcmp(devname, dev->name) == 0) {
214 write_ret = dev->write(devname, addr, reg, value);
220 printf("No such device: %s\n", devname);
222 return ((found_dev) ? write_ret : 1);
225 /*****************************************************************************
227 * Print out list of registered MII capable devices.
229 void miiphy_listdev(void)
231 struct list_head *entry;
234 puts("MII devices: ");
235 list_for_each(entry, &mii_devs) {
236 dev = list_entry(entry, struct mii_dev, link);
237 printf("'%s' ", dev->name);
242 printf("Current device: '%s'\n", current_mii->name);
246 /*****************************************************************************
248 * Read the OUI, manufacture's model number, and revision number.
250 * OUI: 22 bits (unsigned int)
251 * Model: 6 bits (unsigned char)
252 * Revision: 4 bits (unsigned char)
257 int miiphy_info (char *devname,
260 unsigned char *model, unsigned char *rev)
262 unsigned int reg = 0;
265 if (miiphy_read (devname, addr, PHY_PHYIDR2, &tmp) != 0) {
267 puts ("PHY ID register 2 read failed\n");
274 printf ("PHY_PHYIDR2 @ 0x%x = 0x%04x\n", addr, reg);
277 /* No physical device present at this address */
281 if (miiphy_read (devname, addr, PHY_PHYIDR1, &tmp) != 0) {
283 puts ("PHY ID register 1 read failed\n");
289 printf ("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
292 *model = (unsigned char) ((reg >> 4) & 0x0000003F);
293 *rev = (unsigned char) ( reg & 0x0000000F);
298 /*****************************************************************************
304 int miiphy_reset (char *devname, unsigned char addr)
309 if (miiphy_read (devname, addr, PHY_BMCR, ®) != 0) {
311 printf ("PHY status read failed\n");
315 if (miiphy_write (devname, addr, PHY_BMCR, reg | 0x8000) != 0) {
317 puts ("PHY reset failed\n");
321 #ifdef CONFIG_PHY_RESET_DELAY
322 udelay (CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
325 * Poll the control register for the reset bit to go to 0 (it is
326 * auto-clearing). This should happen within 0.5 seconds per the
331 while (((reg & 0x8000) != 0) && (loop_cnt++ < 1000000)) {
332 if (miiphy_read (devname, addr, PHY_BMCR, ®) != 0) {
334 puts ("PHY status read failed\n");
339 if ((reg & 0x8000) == 0) {
342 puts ("PHY reset timed out\n");
349 /*****************************************************************************
351 * Determine the ethernet speed (10/100).
353 int miiphy_speed (char *devname, unsigned char addr)
357 #if defined(CONFIG_PHY_GIGE)
358 if (miiphy_read (devname, addr, PHY_1000BTSR, ®)) {
359 printf ("PHY 1000BT Status read failed\n");
362 if ((reg & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) !=0) {
367 #endif /* CONFIG_PHY_GIGE */
369 /* Check Basic Management Control Register first. */
370 if (miiphy_read (devname, addr, PHY_BMCR, ®)) {
371 puts ("PHY speed read failed, assuming 10bT\n");
374 /* Check if auto-negotiation is on. */
375 if ((reg & PHY_BMCR_AUTON) != 0) {
376 /* Get auto-negotiation results. */
377 if (miiphy_read (devname, addr, PHY_ANLPAR, ®)) {
378 puts ("PHY AN speed read failed, assuming 10bT\n");
381 if ((reg & PHY_ANLPAR_100) != 0) {
387 /* Get speed from basic control settings. */
388 else if (reg & PHY_BMCR_100MB) {
397 /*****************************************************************************
399 * Determine full/half duplex.
401 int miiphy_duplex (char *devname, unsigned char addr)
405 #if defined(CONFIG_PHY_GIGE)
406 if (miiphy_read (devname, addr, PHY_1000BTSR, ®)) {
407 printf ("PHY 1000BT Status read failed\n");
409 if ( (reg != 0xFFFF) &&
410 (reg & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) ) {
411 if ((reg & PHY_1000BTSR_1000FD) !=0) {
418 #endif /* CONFIG_PHY_GIGE */
420 /* Check Basic Management Control Register first. */
421 if (miiphy_read (devname, addr, PHY_BMCR, ®)) {
422 puts ("PHY duplex read failed, assuming half duplex\n");
425 /* Check if auto-negotiation is on. */
426 if ((reg & PHY_BMCR_AUTON) != 0) {
427 /* Get auto-negotiation results. */
428 if (miiphy_read (devname, addr, PHY_ANLPAR, ®)) {
429 puts ("PHY AN duplex read failed, assuming half duplex\n");
433 if ((reg & (PHY_ANLPAR_10FD | PHY_ANLPAR_TXFD)) != 0) {
439 /* Get speed from basic control settings. */
440 else if (reg & PHY_BMCR_DPLX) {
448 #ifdef CFG_FAULT_ECHO_LINK_DOWN
449 /*****************************************************************************
451 * Determine link status
453 int miiphy_link (char *devname, unsigned char addr)
457 /* dummy read; needed to latch some phys */
458 (void)miiphy_read(devname, addr, PHY_BMSR, ®);
459 if (miiphy_read (devname, addr, PHY_BMSR, ®)) {
460 puts ("PHY_BMSR read failed, assuming no link\n");
464 /* Determine if a link is active */
465 if ((reg & PHY_BMSR_LS) != 0) {
473 #endif /* CONFIG_MII */