miiphy.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_440) || defined(CONFIG_405EP)
  50. /***********************************************************/
  51. /* Dump out to the screen PHY regs */
  52. /***********************************************************/
  53. void miiphy_dump (unsigned char addr)
  54. {
  55. unsigned long i;
  56. unsigned short data;
  57. for (i = 0; i < 0x1A; i++) {
  58. if (miiphy_read (addr, i, &data)) {
  59. printf ("read error for reg %lx\n", i);
  60. return;
  61. }
  62. printf ("Phy reg %lx ==> %4x\n", i, data);
  63. /* jump to the next set of regs */
  64. if (i == 0x07)
  65. i = 0x0f;
  66. } /* end for loop */
  67. } /* end dump */
  68. /***********************************************************/
  69. /* read a phy reg and return the value with a rc */
  70. /***********************************************************/
  71. int miiphy_read (unsigned char addr, unsigned char reg,
  72. unsigned short *value)
  73. {
  74. unsigned long sta_reg; /* STA scratch area */
  75. unsigned long i;
  76. /* see if it is ready for 1000 nsec */
  77. i = 0;
  78. /* see if it is ready for sec */
  79. while ((in32 (EMAC_STACR) & EMAC_STACR_OC) == 0) {
  80. udelay (7);
  81. if (i > 5) {
  82. printf ("read err 1\n");
  83. return -1;
  84. }
  85. i++;
  86. }
  87. sta_reg = reg; /* reg address */
  88. /* set clock (50Mhz) and read flags */
  89. sta_reg = (sta_reg | EMAC_STACR_READ) & ~EMAC_STACR_CLK_100MHZ;
  90. sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;
  91. sta_reg = sta_reg | (addr << 5); /* Phy address */
  92. out32 (EMAC_STACR, sta_reg);
  93. #if 0 /* test-only */
  94. printf ("a2: write: EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
  95. #endif
  96. sta_reg = in32 (EMAC_STACR);
  97. i = 0;
  98. while ((sta_reg & EMAC_STACR_OC) == 0) {
  99. udelay (7);
  100. if (i > 5) {
  101. printf ("read err 2\n");
  102. return -1;
  103. }
  104. i++;
  105. sta_reg = in32 (EMAC_STACR);
  106. }
  107. if ((sta_reg & EMAC_STACR_PHYE) != 0) {
  108. printf ("read err 3\n");
  109. printf ("a2: read: EMAC_STACR=0x%0lx, i=%d\n",
  110. sta_reg, (int) i); /* test-only */
  111. return -1;
  112. }
  113. *value = *(short *) (&sta_reg);
  114. return 0;
  115. } /* phy_read */
  116. /***********************************************************/
  117. /* write a phy reg and return the value with a rc */
  118. /***********************************************************/
  119. int miiphy_write (unsigned char addr, unsigned char reg,
  120. unsigned short value)
  121. {
  122. unsigned long sta_reg; /* STA scratch area */
  123. unsigned long i;
  124. /* see if it is ready for 1000 nsec */
  125. i = 0;
  126. while ((in32 (EMAC_STACR) & EMAC_STACR_OC) == 0) {
  127. if (i > 5)
  128. return -1;
  129. udelay (7);
  130. i++;
  131. }
  132. sta_reg = 0;
  133. sta_reg = reg; /* reg address */
  134. /* set clock (50Mhz) and read flags */
  135. sta_reg = (sta_reg | EMAC_STACR_WRITE) & ~EMAC_STACR_CLK_100MHZ;
  136. sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ; /* Set clock frequency (PLB freq. dependend) */
  137. sta_reg = sta_reg | ((unsigned long) addr << 5); /* Phy address */
  138. memcpy (&sta_reg, &value, 2); /* put in data */
  139. out32 (EMAC_STACR, sta_reg);
  140. /* wait for completion */
  141. i = 0;
  142. sta_reg = in32 (EMAC_STACR);
  143. while ((sta_reg & EMAC_STACR_OC) == 0) {
  144. udelay (7);
  145. if (i > 5)
  146. return -1;
  147. i++;
  148. sta_reg = in32 (EMAC_STACR);
  149. }
  150. if ((sta_reg & EMAC_STACR_PHYE) != 0)
  151. return -1;
  152. return 0;
  153. } /* phy_read */
  154. #endif /* CONFIG_405GP */