miiphy.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*-----------------------------------------------------------------------------+
  2. |
  3. | This source code has been made available to you by IBM on an AS-IS
  4. | basis. Anyone receiving this source is licensed under IBM
  5. | copyrights to use it in any way he or she deems fit, including
  6. | copying it, modifying it, compiling it, and redistributing it either
  7. | with or without modifications. No license under IBM patents or
  8. | patent applications is to be implied by the copyright license.
  9. |
  10. | Any user of this software should understand that IBM cannot provide
  11. | technical support for this software and will not be responsible for
  12. | any consequences resulting from the use of this software.
  13. |
  14. | Any person who transfers this source code or any derivative work
  15. | must include the IBM copyright notice, this paragraph, and the
  16. | preceding two paragraphs in the transferred software.
  17. |
  18. | COPYRIGHT I B M CORPORATION 1995
  19. | LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
  20. +-----------------------------------------------------------------------------*/
  21. /*-----------------------------------------------------------------------------+
  22. |
  23. | File Name: miiphy.c
  24. |
  25. | Function: This module has utilities for accessing the MII PHY through
  26. | the EMAC3 macro.
  27. |
  28. | Author: Mark Wisner
  29. |
  30. | Change Activity-
  31. |
  32. | Date Description of Change BY
  33. | --------- --------------------- ---
  34. | 05-May-99 Created MKW
  35. | 01-Jul-99 Changed clock setting of sta_reg from 66Mhz to 50Mhz to
  36. | better match OPB speed. Also modified delay times. JWB
  37. | 29-Jul-99 Added Full duplex support MKW
  38. | 24-Aug-99 Removed printf from dp83843_duplex() JWB
  39. | 19-Jul-00 Ported to esd cpci405 sr
  40. |
  41. +-----------------------------------------------------------------------------*/
  42. #include <common.h>
  43. #include <asm/processor.h>
  44. #include <ppc_asm.tmpl>
  45. #include <commproc.h>
  46. #include <405gp_enet.h>
  47. #include <405_mal.h>
  48. #include <miiphy.h>
  49. #if (defined(CONFIG_405GP) || defined(CONFIG_405EP) || defined(CONFIG_440)) \
  50. && !defined (CONFIG_NET_MULTI)
  51. /***********************************************************/
  52. /* Dump out to the screen PHY regs */
  53. /***********************************************************/
  54. void miiphy_dump (unsigned char addr)
  55. {
  56. unsigned long i;
  57. unsigned short data;
  58. for (i = 0; i < 0x1A; i++) {
  59. if (miiphy_read (addr, i, &data)) {
  60. printf ("read error for reg %lx\n", i);
  61. return;
  62. }
  63. printf ("Phy reg %lx ==> %4x\n", i, data);
  64. /* jump to the next set of regs */
  65. if (i == 0x07)
  66. i = 0x0f;
  67. } /* end for loop */
  68. } /* end dump */
  69. /***********************************************************/
  70. /* read a phy reg and return the value with a rc */
  71. /***********************************************************/
  72. int miiphy_read (unsigned char addr, unsigned char reg,
  73. unsigned short *value)
  74. {
  75. unsigned long sta_reg; /* STA scratch area */
  76. unsigned long i;
  77. /* see if it is ready for 1000 nsec */
  78. i = 0;
  79. /* see if it is ready for sec */
  80. while ((in32 (EMAC_STACR) & EMAC_STACR_OC) == 0) {
  81. udelay (7);
  82. if (i > 5) {
  83. #if 0 /* test-only */
  84. printf ("read err 1\n");
  85. #endif
  86. return -1;
  87. }
  88. i++;
  89. }
  90. sta_reg = reg; /* reg address */
  91. /* set clock (50Mhz) and read flags */
  92. sta_reg = (sta_reg | EMAC_STACR_READ) & ~EMAC_STACR_CLK_100MHZ;
  93. #ifdef CONFIG_PHY_CLK_FREQ
  94. sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;
  95. #endif
  96. sta_reg = sta_reg | (addr << 5); /* Phy address */
  97. out32 (EMAC_STACR, sta_reg);
  98. #if 0 /* test-only */
  99. printf ("a2: write: EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
  100. #endif
  101. sta_reg = in32 (EMAC_STACR);
  102. i = 0;
  103. while ((sta_reg & EMAC_STACR_OC) == 0) {
  104. udelay (7);
  105. if (i > 5) {
  106. #if 0 /* test-only */
  107. printf ("read err 2\n");
  108. #endif
  109. return -1;
  110. }
  111. i++;
  112. sta_reg = in32 (EMAC_STACR);
  113. }
  114. if ((sta_reg & EMAC_STACR_PHYE) != 0) {
  115. #if 0 /* test-only */
  116. printf ("read err 3\n");
  117. printf ("a2: read: EMAC_STACR=0x%0lx, i=%d\n",
  118. sta_reg, (int) i); /* test-only */
  119. #endif
  120. return -1;
  121. }
  122. *value = *(short *) (&sta_reg);
  123. return 0;
  124. } /* phy_read */
  125. /***********************************************************/
  126. /* write a phy reg and return the value with a rc */
  127. /***********************************************************/
  128. int miiphy_write (unsigned char addr, unsigned char reg,
  129. unsigned short value)
  130. {
  131. unsigned long sta_reg; /* STA scratch area */
  132. unsigned long i;
  133. /* see if it is ready for 1000 nsec */
  134. i = 0;
  135. while ((in32 (EMAC_STACR) & EMAC_STACR_OC) == 0) {
  136. if (i > 5)
  137. return -1;
  138. udelay (7);
  139. i++;
  140. }
  141. sta_reg = 0;
  142. sta_reg = reg; /* reg address */
  143. /* set clock (50Mhz) and read flags */
  144. sta_reg = (sta_reg | EMAC_STACR_WRITE) & ~EMAC_STACR_CLK_100MHZ;
  145. #ifdef CONFIG_PHY_CLK_FREQ
  146. sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ; /* Set clock frequency (PLB freq. dependend) */
  147. #endif
  148. sta_reg = sta_reg | ((unsigned long) addr << 5); /* Phy address */
  149. memcpy (&sta_reg, &value, 2); /* put in data */
  150. out32 (EMAC_STACR, sta_reg);
  151. /* wait for completion */
  152. i = 0;
  153. sta_reg = in32 (EMAC_STACR);
  154. while ((sta_reg & EMAC_STACR_OC) == 0) {
  155. udelay (7);
  156. if (i > 5)
  157. return -1;
  158. i++;
  159. sta_reg = in32 (EMAC_STACR);
  160. }
  161. if ((sta_reg & EMAC_STACR_PHYE) != 0)
  162. return -1;
  163. return 0;
  164. } /* phy_read */
  165. #endif /* CONFIG_405GP */