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) || (CONFIG_COMMANDS & CFG_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 * Register read and write MII access routines for the device <name>.
65 void miiphy_register(char *name,
66 int (* read)(char *devname, unsigned char addr,
67 unsigned char reg, unsigned short *value),
68 int (* write)(char *devname, unsigned char addr,
69 unsigned char reg, unsigned short value))
71 struct list_head *entry;
72 struct mii_dev *new_dev;
73 struct mii_dev *miidev;
74 static int head_initialized = 0;
75 unsigned int name_len;
77 if (head_initialized == 0) {
78 INIT_LIST_HEAD(&mii_devs);
83 /* check if we have unique name */
84 list_for_each(entry, &mii_devs) {
85 miidev = list_entry(entry, struct mii_dev, link);
86 if (strcmp(miidev->name, name) == 0) {
87 printf("miiphy_register: non unique device name '%s'\n",
94 name_len = strlen(name);
95 new_dev = (struct mii_dev *)malloc(sizeof(struct mii_dev) + name_len + 1);
98 printf("miiphy_register: cannot allocate memory for '%s'\n",
102 memset(new_dev, 0, sizeof(struct mii_dev) + name_len);
104 /* initalize mii_dev struct fields */
105 INIT_LIST_HEAD(&new_dev->link);
106 new_dev->read = read;
107 new_dev->write = write;
108 new_dev->name = (char *)(new_dev + 1);
109 strncpy(new_dev->name, name, name_len);
110 new_dev->name[name_len] = '\0';
112 debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
113 new_dev->name, new_dev->read, new_dev->write);
115 /* add it to the list */
116 list_add_tail(&new_dev->link, &mii_devs);
119 current_mii = new_dev;
122 int miiphy_set_current_dev(char *devname)
124 struct list_head *entry;
127 list_for_each(entry, &mii_devs) {
128 dev = list_entry(entry, struct mii_dev, link);
130 if (strcmp(devname, dev->name) == 0) {
136 printf("No such device: %s\n", devname);
140 char *miiphy_get_current_dev()
143 return current_mii->name;
148 /*****************************************************************************
150 * Read to variable <value> from the PHY attached to device <devname>,
151 * use PHY address <addr> and register <reg>.
156 int miiphy_read(char *devname, unsigned char addr, unsigned char reg,
157 unsigned short *value)
159 struct list_head *entry;
165 printf("NULL device name!\n");
169 list_for_each(entry, &mii_devs) {
170 dev = list_entry(entry, struct mii_dev, link);
172 if (strcmp(devname, dev->name) == 0) {
174 read_ret = dev->read(devname, addr, reg, value);
180 printf("No such device: %s\n", devname);
182 return ((found_dev) ? read_ret : 1);
185 /*****************************************************************************
187 * Write <value> to the PHY attached to device <devname>,
188 * use PHY address <addr> and register <reg>.
193 int miiphy_write(char *devname, unsigned char addr, unsigned char reg,
194 unsigned short value)
196 struct list_head *entry;
202 printf("NULL device name!\n");
206 list_for_each(entry, &mii_devs) {
207 dev = list_entry(entry, struct mii_dev, link);
209 if (strcmp(devname, dev->name) == 0) {
211 write_ret = dev->write(devname, addr, reg, value);
217 printf("No such device: %s\n", devname);
219 return ((found_dev) ? write_ret : 1);
222 /*****************************************************************************
224 * Print out list of registered MII capable devices.
226 void miiphy_listdev(void)
228 struct list_head *entry;
231 puts("MII devices: ");
232 list_for_each(entry, &mii_devs) {
233 dev = list_entry(entry, struct mii_dev, link);
234 printf("'%s' ", dev->name);
239 printf("Current device: '%s'\n", current_mii->name);
243 /*****************************************************************************
245 * Read the OUI, manufacture's model number, and revision number.
247 * OUI: 22 bits (unsigned int)
248 * Model: 6 bits (unsigned char)
249 * Revision: 4 bits (unsigned char)
254 int miiphy_info (char *devname,
257 unsigned char *model, unsigned char *rev)
259 unsigned int reg = 0;
262 if (miiphy_read (devname, addr, PHY_PHYIDR2, &tmp) != 0) {
264 puts ("PHY ID register 2 read failed\n");
271 printf ("PHY_PHYIDR2 @ 0x%x = 0x%04x\n", addr, reg);
274 /* No physical device present at this address */
278 if (miiphy_read (devname, addr, PHY_PHYIDR1, &tmp) != 0) {
280 puts ("PHY ID register 1 read failed\n");
286 printf ("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
289 *model = (unsigned char) ((reg >> 4) & 0x0000003F);
290 *rev = (unsigned char) ( reg & 0x0000000F);
295 /*****************************************************************************
301 int miiphy_reset (char *devname, unsigned char addr)
306 if (miiphy_read (devname, addr, PHY_BMCR, ®) != 0) {
308 printf ("PHY status read failed\n");
312 if (miiphy_write (devname, addr, PHY_BMCR, reg | 0x8000) != 0) {
314 puts ("PHY reset failed\n");
318 #ifdef CONFIG_PHY_RESET_DELAY
319 udelay (CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
322 * Poll the control register for the reset bit to go to 0 (it is
323 * auto-clearing). This should happen within 0.5 seconds per the
328 while (((reg & 0x8000) != 0) && (loop_cnt++ < 1000000)) {
329 if (miiphy_read (devname, addr, PHY_BMCR, ®) != 0) {
331 puts ("PHY status read failed\n");
336 if ((reg & 0x8000) == 0) {
339 puts ("PHY reset timed out\n");
346 /*****************************************************************************
348 * Determine the ethernet speed (10/100).
350 int miiphy_speed (char *devname, unsigned char addr)
354 #if defined(CONFIG_PHY_GIGE)
355 if (miiphy_read (devname, addr, PHY_1000BTSR, ®)) {
356 printf ("PHY 1000BT Status read failed\n");
359 if ((reg & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) !=0) {
364 #endif /* CONFIG_PHY_GIGE */
366 /* Check Basic Management Control Register first. */
367 if (miiphy_read (devname, addr, PHY_BMCR, ®)) {
368 puts ("PHY speed read failed, assuming 10bT\n");
371 /* Check if auto-negotiation is on. */
372 if ((reg & PHY_BMCR_AUTON) != 0) {
373 /* Get auto-negotiation results. */
374 if (miiphy_read (devname, addr, PHY_ANLPAR, ®)) {
375 puts ("PHY AN speed read failed, assuming 10bT\n");
378 if ((reg & PHY_ANLPAR_100) != 0) {
384 /* Get speed from basic control settings. */
385 else if (reg & PHY_BMCR_100MB) {
394 /*****************************************************************************
396 * Determine full/half duplex.
398 int miiphy_duplex (char *devname, unsigned char addr)
402 #if defined(CONFIG_PHY_GIGE)
403 if (miiphy_read (devname, addr, PHY_1000BTSR, ®)) {
404 printf ("PHY 1000BT Status read failed\n");
406 if ( (reg != 0xFFFF) &&
407 (reg & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) ) {
408 if ((reg & PHY_1000BTSR_1000FD) !=0) {
415 #endif /* CONFIG_PHY_GIGE */
417 /* Check Basic Management Control Register first. */
418 if (miiphy_read (devname, addr, PHY_BMCR, ®)) {
419 puts ("PHY duplex read failed, assuming half duplex\n");
422 /* Check if auto-negotiation is on. */
423 if ((reg & PHY_BMCR_AUTON) != 0) {
424 /* Get auto-negotiation results. */
425 if (miiphy_read (devname, addr, PHY_ANLPAR, ®)) {
426 puts ("PHY AN duplex read failed, assuming half duplex\n");
430 if ((reg & (PHY_ANLPAR_10FD | PHY_ANLPAR_TXFD)) != 0) {
436 /* Get speed from basic control settings. */
437 else if (reg & PHY_BMCR_DPLX) {
445 #ifdef CFG_FAULT_ECHO_LINK_DOWN
446 /*****************************************************************************
448 * Determine link status
450 int miiphy_link (char *devname, unsigned char addr)
454 /* dummy read; needed to latch some phys */
455 (void)miiphy_read(devname, addr, PHY_BMSR, ®);
456 if (miiphy_read (devname, addr, PHY_BMSR, ®)) {
457 puts ("PHY_BMSR read failed, assuming no link\n");
461 /* Determine if a link is active */
462 if ((reg & PHY_BMSR_LS) != 0) {
470 #endif /* CONFIG_MII || (CONFIG_COMMANDS & CFG_CMD_MII) */