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 */
43 #define debug(fmt,args...) printf (fmt ,##args)
45 #define debug(fmt,args...)
46 #endif /* MII_DEBUG */
49 struct list_head link
;
51 int (*read
) (char *devname
, unsigned char addr
,
52 unsigned char reg
, unsigned short *value
);
53 int (*write
) (char *devname
, unsigned char addr
,
54 unsigned char reg
, unsigned short value
);
57 static struct list_head mii_devs
;
58 static struct mii_dev
*current_mii
;
60 /*****************************************************************************
62 * Initialize global data. Need to be called before any other miiphy routine.
66 INIT_LIST_HEAD (&mii_devs
);
70 /*****************************************************************************
72 * Register read and write MII access routines for the device <name>.
74 void miiphy_register (char *name
,
75 int (*read
) (char *devname
, unsigned char addr
,
76 unsigned char reg
, unsigned short *value
),
77 int (*write
) (char *devname
, unsigned char addr
,
78 unsigned char reg
, unsigned short value
))
80 struct list_head
*entry
;
81 struct mii_dev
*new_dev
;
82 struct mii_dev
*miidev
;
83 unsigned int name_len
;
85 /* check if we have unique name */
86 list_for_each (entry
, &mii_devs
) {
87 miidev
= list_entry (entry
, struct mii_dev
, link
);
88 if (strcmp (miidev
->name
, name
) == 0) {
89 printf ("miiphy_register: non unique device name "
96 name_len
= strlen (name
);
98 (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
);
245 /*****************************************************************************
247 * Read the OUI, manufacture's model number, and revision number.
249 * OUI: 22 bits (unsigned int)
250 * Model: 6 bits (unsigned char)
251 * Revision: 4 bits (unsigned char)
256 int miiphy_info (char *devname
, unsigned char addr
, unsigned int *oui
,
257 unsigned char *model
, unsigned char *rev
)
259 unsigned int reg
= 0;
262 if (miiphy_read (devname
, addr
, PHY_PHYIDR2
, &tmp
) != 0) {
263 debug ("PHY ID register 2 read failed\n");
268 debug ("PHY_PHYIDR2 @ 0x%x = 0x%04x\n", addr
, reg
);
271 /* No physical device present at this address */
275 if (miiphy_read (devname
, addr
, PHY_PHYIDR1
, &tmp
) != 0) {
276 debug ("PHY ID register 1 read failed\n");
280 debug ("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr
, reg
);
283 *model
= (unsigned char)((reg
>> 4) & 0x0000003F);
284 *rev
= (unsigned char)(reg
& 0x0000000F);
288 /*****************************************************************************
294 int miiphy_reset (char *devname
, unsigned char addr
)
299 if (miiphy_read (devname
, addr
, PHY_BMCR
, ®
) != 0) {
300 debug ("PHY status read failed\n");
303 if (miiphy_write (devname
, addr
, PHY_BMCR
, reg
| 0x8000) != 0) {
304 debug ("PHY reset failed\n");
307 #ifdef CONFIG_PHY_RESET_DELAY
308 udelay (CONFIG_PHY_RESET_DELAY
); /* Intel LXT971A needs this */
311 * Poll the control register for the reset bit to go to 0 (it is
312 * auto-clearing). This should happen within 0.5 seconds per the
317 while (((reg
& 0x8000) != 0) && (loop_cnt
++ < 1000000)) {
318 if (miiphy_read (devname
, addr
, PHY_BMCR
, ®
) != 0) {
319 debug ("PHY status read failed\n");
323 if ((reg
& 0x8000) == 0) {
326 puts ("PHY reset timed out\n");
332 /*****************************************************************************
334 * Determine the ethernet speed (10/100/1000). Return 10 on error.
336 int miiphy_speed (char *devname
, unsigned char addr
)
340 #if defined(CONFIG_PHY_GIGE)
344 * Check for 1000BASE-X. If it is supported, then assume that the speed
347 if (miiphy_is_1000base_x (devname
, addr
)) {
351 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
353 /* Check for 1000BASE-T. */
354 if (miiphy_read (devname
, addr
, PHY_1000BTSR
, &btsr
)) {
355 printf ("PHY 1000BT status");
356 goto miiphy_read_failed
;
358 if (btsr
!= 0xFFFF &&
359 (btsr
& (PHY_1000BTSR_1000FD
| PHY_1000BTSR_1000HD
))) {
362 #endif /* CONFIG_PHY_GIGE */
364 /* Check Basic Management Control Register first. */
365 if (miiphy_read (devname
, addr
, PHY_BMCR
, &bmcr
)) {
366 printf ("PHY speed");
367 goto miiphy_read_failed
;
369 /* Check if auto-negotiation is on. */
370 if (bmcr
& PHY_BMCR_AUTON
) {
371 /* Get auto-negotiation results. */
372 if (miiphy_read (devname
, addr
, PHY_ANLPAR
, &anlpar
)) {
373 printf ("PHY AN speed");
374 goto miiphy_read_failed
;
376 return (anlpar
& PHY_ANLPAR_100
) ? _100BASET
: _10BASET
;
378 /* Get speed from basic control settings. */
379 return (bmcr
& PHY_BMCR_100MB
) ? _100BASET
: _10BASET
;
382 printf (" read failed, assuming 10BASE-T\n");
386 /*****************************************************************************
388 * Determine full/half duplex. Return half on error.
390 int miiphy_duplex (char *devname
, unsigned char addr
)
394 #if defined(CONFIG_PHY_GIGE)
397 /* Check for 1000BASE-X. */
398 if (miiphy_is_1000base_x (devname
, addr
)) {
400 if (miiphy_read (devname
, addr
, PHY_ANLPAR
, &anlpar
)) {
401 printf ("1000BASE-X PHY AN duplex");
402 goto miiphy_read_failed
;
406 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
408 /* Check for 1000BASE-T. */
409 if (miiphy_read (devname
, addr
, PHY_1000BTSR
, &btsr
)) {
410 printf ("PHY 1000BT status");
411 goto miiphy_read_failed
;
413 if (btsr
!= 0xFFFF) {
414 if (btsr
& PHY_1000BTSR_1000FD
) {
416 } else if (btsr
& PHY_1000BTSR_1000HD
) {
420 #endif /* CONFIG_PHY_GIGE */
422 /* Check Basic Management Control Register first. */
423 if (miiphy_read (devname
, addr
, PHY_BMCR
, &bmcr
)) {
425 goto miiphy_read_failed
;
427 /* Check if auto-negotiation is on. */
428 if (bmcr
& PHY_BMCR_AUTON
) {
429 /* Get auto-negotiation results. */
430 if (miiphy_read (devname
, addr
, PHY_ANLPAR
, &anlpar
)) {
431 puts ("PHY AN duplex");
432 goto miiphy_read_failed
;
434 return (anlpar
& (PHY_ANLPAR_10FD
| PHY_ANLPAR_TXFD
)) ?
437 /* Get speed from basic control settings. */
438 return (bmcr
& PHY_BMCR_DPLX
) ? FULL
: HALF
;
441 printf (" read failed, assuming half duplex\n");
445 /*****************************************************************************
447 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
448 * 1000BASE-T, or on error.
450 int miiphy_is_1000base_x (char *devname
, unsigned char addr
)
452 #if defined(CONFIG_PHY_GIGE)
455 if (miiphy_read (devname
, addr
, PHY_EXSR
, &exsr
)) {
456 printf ("PHY extended status read failed, assuming no "
460 return 0 != (exsr
& (PHY_EXSR_1000XF
| PHY_EXSR_1000XH
));
466 #ifdef CFG_FAULT_ECHO_LINK_DOWN
467 /*****************************************************************************
469 * Determine link status
471 int miiphy_link (char *devname
, unsigned char addr
)
475 /* dummy read; needed to latch some phys */
476 (void)miiphy_read (devname
, addr
, PHY_BMSR
, ®
);
477 if (miiphy_read (devname
, addr
, PHY_BMSR
, ®
)) {
478 puts ("PHY_BMSR read failed, assuming no link\n");
482 /* Determine if a link is active */
483 if ((reg
& PHY_BMSR_LS
) != 0) {
490 #endif /* CONFIG_MII */