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 "
97 name_len
= strlen (name
);
99 (struct mii_dev
*)malloc (sizeof (struct mii_dev
) + name_len
+ 1);
101 if (new_dev
== NULL
) {
102 printf ("miiphy_register: cannot allocate memory for '%s'\n",
106 memset (new_dev
, 0, sizeof (struct mii_dev
) + name_len
);
108 /* initalize mii_dev struct fields */
109 INIT_LIST_HEAD (&new_dev
->link
);
110 new_dev
->read
= read
;
111 new_dev
->write
= write
;
112 new_dev
->name
= (char *)(new_dev
+ 1);
113 strncpy (new_dev
->name
, name
, name_len
);
114 new_dev
->name
[name_len
] = '\0';
116 debug ("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
117 new_dev
->name
, new_dev
->read
, new_dev
->write
);
119 /* add it to the list */
120 list_add_tail (&new_dev
->link
, &mii_devs
);
123 current_mii
= new_dev
;
126 int miiphy_set_current_dev (char *devname
)
128 struct list_head
*entry
;
131 list_for_each (entry
, &mii_devs
) {
132 dev
= list_entry (entry
, struct mii_dev
, link
);
134 if (strcmp (devname
, dev
->name
) == 0) {
140 printf ("No such device: %s\n", devname
);
144 char *miiphy_get_current_dev ()
147 return current_mii
->name
;
152 /*****************************************************************************
154 * Read to variable <value> from the PHY attached to device <devname>,
155 * use PHY address <addr> and register <reg>.
160 int miiphy_read (char *devname
, unsigned char addr
, unsigned char reg
,
161 unsigned short *value
)
163 struct list_head
*entry
;
169 printf ("NULL device name!\n");
173 list_for_each (entry
, &mii_devs
) {
174 dev
= list_entry (entry
, struct mii_dev
, link
);
176 if (strcmp (devname
, dev
->name
) == 0) {
178 read_ret
= dev
->read (devname
, addr
, reg
, value
);
184 printf ("No such device: %s\n", devname
);
186 return ((found_dev
) ? read_ret
: 1);
189 /*****************************************************************************
191 * Write <value> to the PHY attached to device <devname>,
192 * use PHY address <addr> and register <reg>.
197 int miiphy_write (char *devname
, unsigned char addr
, unsigned char reg
,
198 unsigned short value
)
200 struct list_head
*entry
;
206 printf ("NULL device name!\n");
210 list_for_each (entry
, &mii_devs
) {
211 dev
= list_entry (entry
, struct mii_dev
, link
);
213 if (strcmp (devname
, dev
->name
) == 0) {
215 write_ret
= dev
->write (devname
, addr
, reg
, value
);
221 printf ("No such device: %s\n", devname
);
223 return ((found_dev
) ? write_ret
: 1);
226 /*****************************************************************************
228 * Print out list of registered MII capable devices.
230 void miiphy_listdev (void)
232 struct list_head
*entry
;
235 puts ("MII devices: ");
236 list_for_each (entry
, &mii_devs
) {
237 dev
= list_entry (entry
, struct mii_dev
, link
);
238 printf ("'%s' ", dev
->name
);
243 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
, unsigned char addr
, unsigned int *oui
,
258 unsigned char *model
, unsigned char *rev
)
260 unsigned int reg
= 0;
263 if (miiphy_read (devname
, addr
, PHY_PHYIDR2
, &tmp
) != 0) {
265 puts ("PHY ID register 2 read failed\n");
272 printf ("PHY_PHYIDR2 @ 0x%x = 0x%04x\n", addr
, reg
);
275 /* No physical device present at this address */
279 if (miiphy_read (devname
, addr
, PHY_PHYIDR1
, &tmp
) != 0) {
281 puts ("PHY ID register 1 read failed\n");
287 printf ("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr
, reg
);
290 *model
= (unsigned char)((reg
>> 4) & 0x0000003F);
291 *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");
345 /*****************************************************************************
347 * Determine the ethernet speed (10/100/1000). Return 10 on error.
349 int miiphy_speed (char *devname
, unsigned char addr
)
353 #if defined(CONFIG_PHY_GIGE)
357 * Check for 1000BASE-X. If it is supported, then assume that the speed
360 if (miiphy_is_1000base_x (devname
, addr
)) {
364 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
366 /* Check for 1000BASE-T. */
367 if (miiphy_read (devname
, addr
, PHY_1000BTSR
, &btsr
)) {
368 printf ("PHY 1000BT status");
369 goto miiphy_read_failed
;
371 if (btsr
!= 0xFFFF &&
372 (btsr
& (PHY_1000BTSR_1000FD
| PHY_1000BTSR_1000HD
))) {
375 #endif /* CONFIG_PHY_GIGE */
377 /* Check Basic Management Control Register first. */
378 if (miiphy_read (devname
, addr
, PHY_BMCR
, &bmcr
)) {
379 printf ("PHY speed");
380 goto miiphy_read_failed
;
382 /* Check if auto-negotiation is on. */
383 if (bmcr
& PHY_BMCR_AUTON
) {
384 /* Get auto-negotiation results. */
385 if (miiphy_read (devname
, addr
, PHY_ANLPAR
, &anlpar
)) {
386 printf ("PHY AN speed");
387 goto miiphy_read_failed
;
389 return (anlpar
& PHY_ANLPAR_100
) ? _100BASET
: _10BASET
;
391 /* Get speed from basic control settings. */
392 return (bmcr
& PHY_BMCR_100MB
) ? _100BASET
: _10BASET
;
395 printf (" read failed, assuming 10BASE-T\n");
399 /*****************************************************************************
401 * Determine full/half duplex. Return half on error.
403 int miiphy_duplex (char *devname
, unsigned char addr
)
407 #if defined(CONFIG_PHY_GIGE)
410 /* Check for 1000BASE-X. */
411 if (miiphy_is_1000base_x (devname
, addr
)) {
413 if (miiphy_read (devname
, addr
, PHY_ANLPAR
, &anlpar
)) {
414 printf ("1000BASE-X PHY AN duplex");
415 goto miiphy_read_failed
;
419 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
421 /* Check for 1000BASE-T. */
422 if (miiphy_read (devname
, addr
, PHY_1000BTSR
, &btsr
)) {
423 printf ("PHY 1000BT status");
424 goto miiphy_read_failed
;
426 if (btsr
!= 0xFFFF) {
427 if (btsr
& PHY_1000BTSR_1000FD
) {
429 } else if (btsr
& PHY_1000BTSR_1000HD
) {
433 #endif /* CONFIG_PHY_GIGE */
435 /* Check Basic Management Control Register first. */
436 if (miiphy_read (devname
, addr
, PHY_BMCR
, &bmcr
)) {
438 goto miiphy_read_failed
;
440 /* Check if auto-negotiation is on. */
441 if (bmcr
& PHY_BMCR_AUTON
) {
442 /* Get auto-negotiation results. */
443 if (miiphy_read (devname
, addr
, PHY_ANLPAR
, &anlpar
)) {
444 puts ("PHY AN duplex");
445 goto miiphy_read_failed
;
447 return (anlpar
& (PHY_ANLPAR_10FD
| PHY_ANLPAR_TXFD
)) ?
450 /* Get speed from basic control settings. */
451 return (bmcr
& PHY_BMCR_DPLX
) ? FULL
: HALF
;
454 printf (" read failed, assuming half duplex\n");
458 /*****************************************************************************
460 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
461 * 1000BASE-T, or on error.
463 int miiphy_is_1000base_x (char *devname
, unsigned char addr
)
465 #if defined(CONFIG_PHY_GIGE)
468 if (miiphy_read (devname
, addr
, PHY_EXSR
, &exsr
)) {
469 printf ("PHY extended status read failed, assuming no "
473 return 0 != (exsr
& (PHY_EXSR_1000XF
| PHY_EXSR_1000XH
));
479 #ifdef CFG_FAULT_ECHO_LINK_DOWN
480 /*****************************************************************************
482 * Determine link status
484 int miiphy_link (char *devname
, unsigned char addr
)
488 /* dummy read; needed to latch some phys */
489 (void)miiphy_read (devname
, addr
, PHY_BMSR
, ®
);
490 if (miiphy_read (devname
, addr
, PHY_BMSR
, ®
)) {
491 puts ("PHY_BMSR read failed, assuming no link\n");
495 /* Determine if a link is active */
496 if ((reg
& PHY_BMSR_LS
) != 0) {
503 #endif /* CONFIG_MII */