miiphy.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. | 23-Dec-03 Ported from miiphy.c to 440GX Travis Sawyer TBS
  41. | <travis.sawyer@sandburst.com>
  42. |
  43. +-----------------------------------------------------------------------------*/
  44. #include <common.h>
  45. #include <asm/processor.h>
  46. #include <ppc_asm.tmpl>
  47. #include <commproc.h>
  48. #include <ppc4xx_enet.h>
  49. #include <405_mal.h>
  50. #include <miiphy.h>
  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. /* (Re)start autonegotiation */
  71. /***********************************************************/
  72. int phy_setup_aneg (unsigned char addr)
  73. {
  74. unsigned short ctl, adv;
  75. /* Setup standard advertise */
  76. miiphy_read (addr, PHY_ANAR, &adv);
  77. adv |= (PHY_ANLPAR_ACK | PHY_ANLPAR_RF | PHY_ANLPAR_T4 |
  78. PHY_ANLPAR_TXFD | PHY_ANLPAR_TX | PHY_ANLPAR_10FD |
  79. PHY_ANLPAR_10);
  80. miiphy_write (addr, PHY_ANAR, adv);
  81. /* Start/Restart aneg */
  82. miiphy_read (addr, PHY_BMCR, &ctl);
  83. ctl |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
  84. miiphy_write (addr, PHY_BMCR, ctl);
  85. return 0;
  86. }
  87. /***********************************************************/
  88. /* read a phy reg and return the value with a rc */
  89. /***********************************************************/
  90. unsigned int miiphy_getemac_offset (void)
  91. {
  92. #if defined(CONFIG_440) && defined(CONFIG_NET_MULTI)
  93. unsigned long zmii;
  94. unsigned long eoffset;
  95. /* Need to find out which mdi port we're using */
  96. zmii = in32 (ZMII_FER);
  97. if (zmii & (ZMII_FER_MDI << ZMII_FER_V (0))) {
  98. /* using port 0 */
  99. eoffset = 0;
  100. } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (1))) {
  101. /* using port 1 */
  102. eoffset = 0x100;
  103. } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (2))) {
  104. /* using port 2 */
  105. eoffset = 0x400;
  106. } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (3))) {
  107. /* using port 3 */
  108. eoffset = 0x600;
  109. } else {
  110. /* None of the mdi ports are enabled! */
  111. /* enable port 0 */
  112. zmii |= ZMII_FER_MDI << ZMII_FER_V (0);
  113. out32 (ZMII_FER, zmii);
  114. eoffset = 0;
  115. /* need to soft reset port 0 */
  116. zmii = in32 (EMAC_M0);
  117. zmii |= EMAC_M0_SRST;
  118. out32 (EMAC_M0, zmii);
  119. }
  120. return (eoffset);
  121. #else
  122. return 0;
  123. #endif
  124. }
  125. int miiphy_read (unsigned char addr, unsigned char reg, unsigned short *value)
  126. {
  127. unsigned long sta_reg; /* STA scratch area */
  128. unsigned long i;
  129. unsigned long emac_reg;
  130. emac_reg = miiphy_getemac_offset ();
  131. /* see if it is ready for 1000 nsec */
  132. i = 0;
  133. /* see if it is ready for sec */
  134. while ((in32 (EMAC_STACR + emac_reg) & EMAC_STACR_OC) == 0) {
  135. udelay (7);
  136. if (i > 5) {
  137. #if 0
  138. printf ("read err 1\n");
  139. #endif
  140. return -1;
  141. }
  142. i++;
  143. }
  144. sta_reg = reg; /* reg address */
  145. /* set clock (50Mhz) and read flags */
  146. #if defined(CONFIG_440GX)
  147. sta_reg |= EMAC_STACR_READ;
  148. #else
  149. sta_reg = (sta_reg | EMAC_STACR_READ) & ~EMAC_STACR_CLK_100MHZ;
  150. #endif
  151. #if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX)
  152. sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;
  153. #endif
  154. sta_reg = sta_reg | (addr << 5); /* Phy address */
  155. out32 (EMAC_STACR + emac_reg, sta_reg);
  156. #if 0 /* test-only */
  157. printf ("a2: write: EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
  158. #endif
  159. sta_reg = in32 (EMAC_STACR + emac_reg);
  160. i = 0;
  161. while ((sta_reg & EMAC_STACR_OC) == 0) {
  162. udelay (7);
  163. if (i > 5) {
  164. return -1;
  165. }
  166. i++;
  167. sta_reg = in32 (EMAC_STACR + emac_reg);
  168. }
  169. if ((sta_reg & EMAC_STACR_PHYE) != 0) {
  170. return -1;
  171. }
  172. *value = *(short *) (&sta_reg);
  173. return 0;
  174. } /* phy_read */
  175. /***********************************************************/
  176. /* write a phy reg and return the value with a rc */
  177. /***********************************************************/
  178. int miiphy_write (unsigned char addr, unsigned char reg, unsigned short value)
  179. {
  180. unsigned long sta_reg; /* STA scratch area */
  181. unsigned long i;
  182. unsigned long emac_reg;
  183. emac_reg = miiphy_getemac_offset ();
  184. /* see if it is ready for 1000 nsec */
  185. i = 0;
  186. while ((in32 (EMAC_STACR + emac_reg) & EMAC_STACR_OC) == 0) {
  187. if (i > 5)
  188. return -1;
  189. udelay (7);
  190. i++;
  191. }
  192. sta_reg = 0;
  193. sta_reg = reg; /* reg address */
  194. /* set clock (50Mhz) and read flags */
  195. #if defined(CONFIG_440GX)
  196. sta_reg |= EMAC_STACR_WRITE;
  197. #else
  198. sta_reg = (sta_reg | EMAC_STACR_WRITE) & ~EMAC_STACR_CLK_100MHZ;
  199. #endif
  200. #if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX)
  201. sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ; /* Set clock frequency (PLB freq. dependend) */
  202. #endif
  203. sta_reg = sta_reg | ((unsigned long) addr << 5); /* Phy address */
  204. memcpy (&sta_reg, &value, 2); /* put in data */
  205. out32 (EMAC_STACR + emac_reg, sta_reg);
  206. /* wait for completion */
  207. i = 0;
  208. sta_reg = in32 (EMAC_STACR + emac_reg);
  209. while ((sta_reg & EMAC_STACR_OC) == 0) {
  210. udelay (7);
  211. if (i > 5)
  212. return -1;
  213. i++;
  214. sta_reg = in32 (EMAC_STACR + emac_reg);
  215. }
  216. if ((sta_reg & EMAC_STACR_PHYE) != 0)
  217. return -1;
  218. return 0;
  219. } /* phy_write */