miiphyutil.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * (C) Copyright 2001
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  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.
  12. *
  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.
  17. *
  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,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * This provides a bit-banged interface to the ethernet MII management
  25. * channel.
  26. */
  27. #include <common.h>
  28. #include <miiphy.h>
  29. #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
  30. /*****************************************************************************
  31. *
  32. * Read the OUI, manufacture's model number, and revision number.
  33. *
  34. * OUI: 22 bits (unsigned int)
  35. * Model: 6 bits (unsigned char)
  36. * Revision: 4 bits (unsigned char)
  37. *
  38. * Returns:
  39. * 0 on success
  40. */
  41. int miiphy_info (unsigned char addr,
  42. unsigned int *oui,
  43. unsigned char *model, unsigned char *rev)
  44. {
  45. unsigned int reg = 0;
  46. /*
  47. * Trick: we are reading two 16 registers into a 32 bit variable
  48. * so we do a 16 read into the high order bits of the variable (big
  49. * endian, you know), shift it down 16 bits, and the read the rest.
  50. */
  51. if (miiphy_read (addr, PHY_PHYIDR2, (unsigned short *) &reg) != 0) {
  52. #ifdef DEBUG
  53. printf ("PHY ID register 2 read failed\n");
  54. #endif
  55. return (-1);
  56. }
  57. reg >>= 16;
  58. #ifdef DEBUG
  59. printf ("PHY_PHYIDR2 @ 0x%x = 0x%04x\n", addr, reg);
  60. #endif
  61. if (reg == 0xFFFF) {
  62. /* No physical device present at this address */
  63. return (-1);
  64. }
  65. if (miiphy_read (addr, PHY_PHYIDR1, (unsigned short *) &reg) != 0) {
  66. #ifdef DEBUG
  67. printf ("PHY ID register 1 read failed\n");
  68. #endif
  69. return (-1);
  70. }
  71. #ifdef DEBUG
  72. printf ("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
  73. #endif
  74. *oui = ( reg >> 10);
  75. *model = (unsigned char) ((reg >> 4) & 0x0000003F);
  76. *rev = (unsigned char) ( reg & 0x0000000F);
  77. return (0);
  78. }
  79. /*****************************************************************************
  80. *
  81. * Reset the PHY.
  82. * Returns:
  83. * 0 on success
  84. */
  85. int miiphy_reset (unsigned char addr)
  86. {
  87. unsigned short reg;
  88. int loop_cnt;
  89. if (miiphy_write (addr, PHY_BMCR, 0x8000) != 0) {
  90. #ifdef DEBUG
  91. printf ("PHY reset failed\n");
  92. #endif
  93. return (-1);
  94. }
  95. /*
  96. * Poll the control register for the reset bit to go to 0 (it is
  97. * auto-clearing). This should happen within 0.5 seconds per the
  98. * IEEE spec.
  99. */
  100. loop_cnt = 0;
  101. reg = 0x8000;
  102. while (((reg & 0x8000) != 0) && (loop_cnt++ < 1000000)) {
  103. if (miiphy_read (addr, PHY_BMCR, &reg) != 0) {
  104. # ifdef DEBUG
  105. printf ("PHY status read failed\n");
  106. # endif
  107. return (-1);
  108. }
  109. }
  110. if ((reg & 0x8000) == 0) {
  111. return (0);
  112. } else {
  113. printf ("PHY reset timed out\n");
  114. return (-1);
  115. }
  116. return (0);
  117. }
  118. /*****************************************************************************
  119. *
  120. * Determine the ethernet speed (10/100).
  121. */
  122. int miiphy_speed (unsigned char addr)
  123. {
  124. unsigned short reg;
  125. if (miiphy_read (addr, PHY_ANLPAR, &reg)) {
  126. printf ("PHY speed1 read failed, assuming 10bT\n");
  127. return (_10BASET);
  128. }
  129. if ((reg & PHY_ANLPAR_100) != 0) {
  130. return (_100BASET);
  131. } else {
  132. return (_10BASET);
  133. }
  134. }
  135. /*****************************************************************************
  136. *
  137. * Determine full/half duplex.
  138. */
  139. int miiphy_duplex (unsigned char addr)
  140. {
  141. unsigned short reg;
  142. if (miiphy_read (addr, PHY_ANLPAR, &reg)) {
  143. printf ("PHY duplex read failed, assuming half duplex\n");
  144. return (HALF);
  145. }
  146. if ((reg & (PHY_ANLPAR_10FD | PHY_ANLPAR_TXFD)) != 0) {
  147. return (FULL);
  148. } else {
  149. return (HALF);
  150. }
  151. }
  152. #endif /* CONFIG_MII || (CONFIG_COMMANDS & CFG_CMD_MII) */