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) (char *devname, unsigned char addr,
51 unsigned char reg, unsigned short *value);
52 int (*write) (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;
59 /*****************************************************************************
61 * Initialize global data. Need to be called before any other miiphy routine.
65 INIT_LIST_HEAD (&mii_devs);
69 /*****************************************************************************
71 * Register read and write MII access routines for the device <name>.
73 void miiphy_register (char *name,
74 int (*read) (char *devname, unsigned char addr,
75 unsigned char reg, unsigned short *value),
76 int (*write) (char *devname, unsigned char addr,
77 unsigned char reg, unsigned short value))
79 struct list_head *entry;
80 struct mii_dev *new_dev;
81 struct mii_dev *miidev;
82 unsigned int name_len;
84 /* check if we have unique name */
85 list_for_each (entry, &mii_devs) {
86 miidev = list_entry (entry, struct mii_dev, link);
87 if (strcmp (miidev->name, name) == 0) {
88 printf ("miiphy_register: non unique device name "
95 name_len = strlen (name);
97 (struct mii_dev *)malloc (sizeof (struct mii_dev) + name_len + 1);
99 if (new_dev == NULL) {
100 printf ("miiphy_register: cannot allocate memory for '%s'\n",
104 memset (new_dev, 0, sizeof (struct mii_dev) + name_len);
106 /* initalize mii_dev struct fields */
107 INIT_LIST_HEAD (&new_dev->link);
108 new_dev->read = read;
109 new_dev->write = write;
110 new_dev->name = (char *)(new_dev + 1);
111 strncpy (new_dev->name, name, name_len);
112 new_dev->name[name_len] = '\0';
114 debug ("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
115 new_dev->name, new_dev->read, new_dev->write);
117 /* add it to the list */
118 list_add_tail (&new_dev->link, &mii_devs);
121 current_mii = new_dev;
124 int miiphy_set_current_dev (char *devname)
126 struct list_head *entry;
129 list_for_each (entry, &mii_devs) {
130 dev = list_entry (entry, struct mii_dev, link);
132 if (strcmp (devname, dev->name) == 0) {
138 printf ("No such device: %s\n", devname);
142 char *miiphy_get_current_dev ()
145 return current_mii->name;
150 /*****************************************************************************
152 * Read to variable <value> from the PHY attached to device <devname>,
153 * use PHY address <addr> and register <reg>.
158 int miiphy_read (char *devname, unsigned char addr, unsigned char reg,
159 unsigned short *value)
161 struct list_head *entry;
167 printf ("NULL device name!\n");
171 list_for_each (entry, &mii_devs) {
172 dev = list_entry (entry, struct mii_dev, link);
174 if (strcmp (devname, dev->name) == 0) {
176 read_ret = dev->read (devname, addr, reg, value);
182 printf ("No such device: %s\n", devname);
184 return ((found_dev) ? read_ret : 1);
187 /*****************************************************************************
189 * Write <value> to the PHY attached to device <devname>,
190 * use PHY address <addr> and register <reg>.
195 int miiphy_write (char *devname, unsigned char addr, unsigned char reg,
196 unsigned short value)
198 struct list_head *entry;
204 printf ("NULL device name!\n");
208 list_for_each (entry, &mii_devs) {
209 dev = list_entry (entry, struct mii_dev, link);
211 if (strcmp (devname, dev->name) == 0) {
213 write_ret = dev->write (devname, addr, reg, value);
219 printf ("No such device: %s\n", devname);
221 return ((found_dev) ? write_ret : 1);
224 /*****************************************************************************
226 * Print out list of registered MII capable devices.
228 void miiphy_listdev (void)
230 struct list_head *entry;
233 puts ("MII devices: ");
234 list_for_each (entry, &mii_devs) {
235 dev = list_entry (entry, struct mii_dev, link);
236 printf ("'%s' ", dev->name);
241 printf ("Current device: '%s'\n", current_mii->name);
244 /*****************************************************************************
246 * Read the OUI, manufacture's model number, and revision number.
248 * OUI: 22 bits (unsigned int)
249 * Model: 6 bits (unsigned char)
250 * Revision: 4 bits (unsigned char)
255 int miiphy_info (char *devname, unsigned char addr, unsigned int *oui,
256 unsigned char *model, unsigned char *rev)
258 unsigned int reg = 0;
261 if (miiphy_read (devname, addr, PHY_PHYIDR2, &tmp) != 0) {
262 debug ("PHY ID register 2 read failed\n");
267 debug ("PHY_PHYIDR2 @ 0x%x = 0x%04x\n", addr, reg);
270 /* No physical device present at this address */
274 if (miiphy_read (devname, addr, PHY_PHYIDR1, &tmp) != 0) {
275 debug ("PHY ID register 1 read failed\n");
279 debug ("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
282 *model = (unsigned char)((reg >> 4) & 0x0000003F);
283 *rev = (unsigned char)(reg & 0x0000000F);
287 /*****************************************************************************
293 int miiphy_reset (char *devname, unsigned char addr)
298 if (miiphy_read (devname, addr, PHY_BMCR, ®) != 0) {
299 debug ("PHY status read failed\n");
302 if (miiphy_write (devname, addr, PHY_BMCR, reg | 0x8000) != 0) {
303 debug ("PHY reset failed\n");
306 #ifdef CONFIG_PHY_RESET_DELAY
307 udelay (CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
310 * Poll the control register for the reset bit to go to 0 (it is
311 * auto-clearing). This should happen within 0.5 seconds per the
316 while (((reg & 0x8000) != 0) && (loop_cnt++ < 1000000)) {
317 if (miiphy_read (devname, addr, PHY_BMCR, ®) != 0) {
318 debug ("PHY status read failed\n");
322 if ((reg & 0x8000) == 0) {
325 puts ("PHY reset timed out\n");
331 /*****************************************************************************
333 * Determine the ethernet speed (10/100/1000). Return 10 on error.
335 int miiphy_speed (char *devname, unsigned char addr)
339 #if defined(CONFIG_PHY_GIGE)
343 * Check for 1000BASE-X. If it is supported, then assume that the speed
346 if (miiphy_is_1000base_x (devname, addr)) {
350 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
352 /* Check for 1000BASE-T. */
353 if (miiphy_read (devname, addr, PHY_1000BTSR, &btsr)) {
354 printf ("PHY 1000BT status");
355 goto miiphy_read_failed;
357 if (btsr != 0xFFFF &&
358 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD))) {
361 #endif /* CONFIG_PHY_GIGE */
363 /* Check Basic Management Control Register first. */
364 if (miiphy_read (devname, addr, PHY_BMCR, &bmcr)) {
365 printf ("PHY speed");
366 goto miiphy_read_failed;
368 /* Check if auto-negotiation is on. */
369 if (bmcr & PHY_BMCR_AUTON) {
370 /* Get auto-negotiation results. */
371 if (miiphy_read (devname, addr, PHY_ANLPAR, &anlpar)) {
372 printf ("PHY AN speed");
373 goto miiphy_read_failed;
375 return (anlpar & PHY_ANLPAR_100) ? _100BASET : _10BASET;
377 /* Get speed from basic control settings. */
378 return (bmcr & PHY_BMCR_100MB) ? _100BASET : _10BASET;
381 printf (" read failed, assuming 10BASE-T\n");
385 /*****************************************************************************
387 * Determine full/half duplex. Return half on error.
389 int miiphy_duplex (char *devname, unsigned char addr)
393 #if defined(CONFIG_PHY_GIGE)
396 /* Check for 1000BASE-X. */
397 if (miiphy_is_1000base_x (devname, addr)) {
399 if (miiphy_read (devname, addr, PHY_ANLPAR, &anlpar)) {
400 printf ("1000BASE-X PHY AN duplex");
401 goto miiphy_read_failed;
405 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
407 /* Check for 1000BASE-T. */
408 if (miiphy_read (devname, addr, PHY_1000BTSR, &btsr)) {
409 printf ("PHY 1000BT status");
410 goto miiphy_read_failed;
412 if (btsr != 0xFFFF) {
413 if (btsr & PHY_1000BTSR_1000FD) {
415 } else if (btsr & PHY_1000BTSR_1000HD) {
419 #endif /* CONFIG_PHY_GIGE */
421 /* Check Basic Management Control Register first. */
422 if (miiphy_read (devname, addr, PHY_BMCR, &bmcr)) {
424 goto miiphy_read_failed;
426 /* Check if auto-negotiation is on. */
427 if (bmcr & PHY_BMCR_AUTON) {
428 /* Get auto-negotiation results. */
429 if (miiphy_read (devname, addr, PHY_ANLPAR, &anlpar)) {
430 puts ("PHY AN duplex");
431 goto miiphy_read_failed;
433 return (anlpar & (PHY_ANLPAR_10FD | PHY_ANLPAR_TXFD)) ?
436 /* Get speed from basic control settings. */
437 return (bmcr & PHY_BMCR_DPLX) ? FULL : HALF;
440 printf (" read failed, assuming half duplex\n");
444 /*****************************************************************************
446 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
447 * 1000BASE-T, or on error.
449 int miiphy_is_1000base_x (char *devname, unsigned char addr)
451 #if defined(CONFIG_PHY_GIGE)
454 if (miiphy_read (devname, addr, PHY_EXSR, &exsr)) {
455 printf ("PHY extended status read failed, assuming no "
459 return 0 != (exsr & (PHY_EXSR_1000XF | PHY_EXSR_1000XH));
465 #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
466 /*****************************************************************************
468 * Determine link status
470 int miiphy_link (char *devname, unsigned char addr)
474 /* dummy read; needed to latch some phys */
475 (void)miiphy_read (devname, addr, PHY_BMSR, ®);
476 if (miiphy_read (devname, addr, PHY_BMSR, ®)) {
477 puts ("PHY_BMSR read failed, assuming no link\n");
481 /* Determine if a link is active */
482 if ((reg & PHY_BMSR_LS) != 0) {