miiphy.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #undef ET_DEBUG
  52. /***********************************************************/
  53. /* Dump out to the screen PHY regs */
  54. /***********************************************************/
  55. void miiphy_dump (char *devname, unsigned char addr)
  56. {
  57. unsigned long i;
  58. unsigned short data;
  59. for (i = 0; i < 0x1A; i++) {
  60. if (miiphy_read (devname, addr, i, &data)) {
  61. printf ("read error for reg %lx\n", i);
  62. return;
  63. }
  64. printf ("Phy reg %lx ==> %4x\n", i, data);
  65. /* jump to the next set of regs */
  66. if (i == 0x07)
  67. i = 0x0f;
  68. } /* end for loop */
  69. } /* end dump */
  70. /***********************************************************/
  71. /* (Re)start autonegotiation */
  72. /***********************************************************/
  73. int phy_setup_aneg (char *devname, unsigned char addr)
  74. {
  75. unsigned short ctl, adv;
  76. /* Setup standard advertise */
  77. miiphy_read (devname, addr, PHY_ANAR, &adv);
  78. adv |= (PHY_ANLPAR_ACK | PHY_ANLPAR_RF | PHY_ANLPAR_T4 |
  79. PHY_ANLPAR_TXFD | PHY_ANLPAR_TX | PHY_ANLPAR_10FD |
  80. PHY_ANLPAR_10);
  81. miiphy_write (devname, addr, PHY_ANAR, adv);
  82. miiphy_read (devname, addr, PHY_1000BTCR, &adv);
  83. adv |= (0x0300);
  84. miiphy_write (devname, addr, PHY_1000BTCR, adv);
  85. /* Start/Restart aneg */
  86. miiphy_read (devname, addr, PHY_BMCR, &ctl);
  87. ctl |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
  88. miiphy_write (devname, addr, PHY_BMCR, ctl);
  89. return 0;
  90. }
  91. /***********************************************************/
  92. /* read a phy reg and return the value with a rc */
  93. /***********************************************************/
  94. unsigned int miiphy_getemac_offset (void)
  95. {
  96. #if (defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)) && defined(CONFIG_NET_MULTI)
  97. unsigned long zmii;
  98. unsigned long eoffset;
  99. /* Need to find out which mdi port we're using */
  100. zmii = in32 (ZMII_FER);
  101. if (zmii & (ZMII_FER_MDI << ZMII_FER_V (0))) {
  102. /* using port 0 */
  103. eoffset = 0;
  104. } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (1))) {
  105. /* using port 1 */
  106. eoffset = 0x100;
  107. } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (2))) {
  108. /* using port 2 */
  109. eoffset = 0x400;
  110. } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (3))) {
  111. /* using port 3 */
  112. eoffset = 0x600;
  113. } else {
  114. /* None of the mdi ports are enabled! */
  115. /* enable port 0 */
  116. zmii |= ZMII_FER_MDI << ZMII_FER_V (0);
  117. out32 (ZMII_FER, zmii);
  118. eoffset = 0;
  119. /* need to soft reset port 0 */
  120. zmii = in32 (EMAC_M0);
  121. zmii |= EMAC_M0_SRST;
  122. out32 (EMAC_M0, zmii);
  123. }
  124. return (eoffset);
  125. #else
  126. #if defined(CONFIG_NET_MULTI) && defined(CONFIG_405EX)
  127. unsigned long rgmii;
  128. int devnum = 1;
  129. rgmii = in32(RGMII_FER);
  130. if (rgmii & (1 << (19 - devnum)))
  131. return 0x100;
  132. #endif
  133. return 0;
  134. #endif
  135. }
  136. int emac4xx_miiphy_read (char *devname, unsigned char addr,
  137. unsigned char reg, unsigned short *value)
  138. {
  139. unsigned long sta_reg; /* STA scratch area */
  140. unsigned long i;
  141. unsigned long emac_reg;
  142. emac_reg = miiphy_getemac_offset ();
  143. /* see if it is ready for 1000 nsec */
  144. i = 0;
  145. /* see if it is ready for sec */
  146. while ((in32 (EMAC_STACR + emac_reg) & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
  147. udelay (7);
  148. if (i > 5) {
  149. #ifdef ET_DEBUG
  150. sta_reg = in32 (EMAC_STACR + emac_reg);
  151. printf ("read : EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
  152. printf ("read err 1\n");
  153. #endif
  154. return -1;
  155. }
  156. i++;
  157. }
  158. sta_reg = reg; /* reg address */
  159. /* set clock (50Mhz) and read flags */
  160. #if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
  161. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  162. defined(CONFIG_405EX)
  163. #if defined(CONFIG_IBM_EMAC4_V4) /* EMAC4 V4 changed bit setting */
  164. sta_reg = (sta_reg & ~EMAC_STACR_OP_MASK) | EMAC_STACR_READ;
  165. #else
  166. sta_reg |= EMAC_STACR_READ;
  167. #endif
  168. #else
  169. sta_reg = (sta_reg | EMAC_STACR_READ) & ~EMAC_STACR_CLK_100MHZ;
  170. #endif
  171. #if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX) && \
  172. !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
  173. !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX) && \
  174. !defined(CONFIG_405EX)
  175. sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;
  176. #endif
  177. sta_reg = sta_reg | (addr << 5); /* Phy address */
  178. sta_reg = sta_reg | EMAC_STACR_OC_MASK; /* new IBM emac v4 */
  179. out32 (EMAC_STACR + emac_reg, sta_reg);
  180. #ifdef ET_DEBUG
  181. printf ("a2: write: EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
  182. #endif
  183. sta_reg = in32 (EMAC_STACR + emac_reg);
  184. #ifdef ET_DEBUG
  185. printf ("a21: read : EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
  186. #endif
  187. i = 0;
  188. while ((sta_reg & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
  189. udelay (7);
  190. if (i > 5) {
  191. return -1;
  192. }
  193. i++;
  194. sta_reg = in32 (EMAC_STACR + emac_reg);
  195. #ifdef ET_DEBUG
  196. printf ("a22: read : EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
  197. #endif
  198. }
  199. if ((sta_reg & EMAC_STACR_PHYE) != 0) {
  200. return -1;
  201. }
  202. *value = *(short *) (&sta_reg);
  203. return 0;
  204. } /* phy_read */
  205. /***********************************************************/
  206. /* write a phy reg and return the value with a rc */
  207. /***********************************************************/
  208. int emac4xx_miiphy_write (char *devname, unsigned char addr,
  209. unsigned char reg, unsigned short value)
  210. {
  211. unsigned long sta_reg; /* STA scratch area */
  212. unsigned long i;
  213. unsigned long emac_reg;
  214. emac_reg = miiphy_getemac_offset ();
  215. /* see if it is ready for 1000 nsec */
  216. i = 0;
  217. while ((in32 (EMAC_STACR + emac_reg) & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
  218. if (i > 5)
  219. return -1;
  220. udelay (7);
  221. i++;
  222. }
  223. sta_reg = 0;
  224. sta_reg = reg; /* reg address */
  225. /* set clock (50Mhz) and read flags */
  226. #if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
  227. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  228. defined(CONFIG_405EX)
  229. #if defined(CONFIG_IBM_EMAC4_V4) /* EMAC4 V4 changed bit setting */
  230. sta_reg = (sta_reg & ~EMAC_STACR_OP_MASK) | EMAC_STACR_WRITE;
  231. #else
  232. sta_reg |= EMAC_STACR_WRITE;
  233. #endif
  234. #else
  235. sta_reg = (sta_reg | EMAC_STACR_WRITE) & ~EMAC_STACR_CLK_100MHZ;
  236. #endif
  237. #if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX) && \
  238. !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
  239. !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX) && \
  240. !defined(CONFIG_405EX)
  241. sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ; /* Set clock frequency (PLB freq. dependend) */
  242. #endif
  243. sta_reg = sta_reg | ((unsigned long) addr << 5);/* Phy address */
  244. sta_reg = sta_reg | EMAC_STACR_OC_MASK; /* new IBM emac v4 */
  245. memcpy (&sta_reg, &value, 2); /* put in data */
  246. out32 (EMAC_STACR + emac_reg, sta_reg);
  247. /* wait for completion */
  248. i = 0;
  249. sta_reg = in32 (EMAC_STACR + emac_reg);
  250. #ifdef ET_DEBUG
  251. printf ("a31: read : EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
  252. #endif
  253. while ((sta_reg & EMAC_STACR_OC) == EMAC_STACR_OC_MASK) {
  254. udelay (7);
  255. if (i > 5)
  256. return -1;
  257. i++;
  258. sta_reg = in32 (EMAC_STACR + emac_reg);
  259. #ifdef ET_DEBUG
  260. printf ("a32: read : EMAC_STACR=0x%0x\n", sta_reg); /* test-only */
  261. #endif
  262. }
  263. if ((sta_reg & EMAC_STACR_PHYE) != 0)
  264. return -1;
  265. return 0;
  266. } /* phy_write */