miiphybb.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <ioports.h>
  29. #include <ppc_asm.tmpl>
  30. #ifdef CONFIG_BITBANGMII
  31. /*****************************************************************************
  32. *
  33. * Utility to send the preamble, address, and register (common to read
  34. * and write).
  35. */
  36. static void miiphy_pre(char read,
  37. unsigned char addr,
  38. unsigned char reg)
  39. {
  40. int j; /* counter */
  41. volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, MDIO_PORT);
  42. /*
  43. * Send a 32 bit preamble ('1's) with an extra '1' bit for good measure.
  44. * The IEEE spec says this is a PHY optional requirement. The AMD
  45. * 79C874 requires one after power up and one after a MII communications
  46. * error. This means that we are doing more preambles than we need,
  47. * but it is safer and will be much more robust.
  48. */
  49. MDIO_ACTIVE;
  50. MDIO(1);
  51. for(j = 0; j < 32; j++)
  52. {
  53. MDC(0);
  54. MIIDELAY;
  55. MDC(1);
  56. MIIDELAY;
  57. }
  58. /* send the start bit (01) and the read opcode (10) or write (10) */
  59. MDC(0); MDIO(0); MIIDELAY; MDC(1); MIIDELAY;
  60. MDC(0); MDIO(1); MIIDELAY; MDC(1); MIIDELAY;
  61. MDC(0); MDIO(read); MIIDELAY; MDC(1); MIIDELAY;
  62. MDC(0); MDIO(!read); MIIDELAY; MDC(1); MIIDELAY;
  63. /* send the PHY address */
  64. for(j = 0; j < 5; j++)
  65. {
  66. MDC(0);
  67. if((addr & 0x10) == 0)
  68. {
  69. MDIO(0);
  70. }
  71. else
  72. {
  73. MDIO(1);
  74. }
  75. MIIDELAY;
  76. MDC(1);
  77. MIIDELAY;
  78. addr <<= 1;
  79. }
  80. /* send the register address */
  81. for(j = 0; j < 5; j++)
  82. {
  83. MDC(0);
  84. if((reg & 0x10) == 0)
  85. {
  86. MDIO(0);
  87. }
  88. else
  89. {
  90. MDIO(1);
  91. }
  92. MIIDELAY;
  93. MDC(1);
  94. MIIDELAY;
  95. reg <<= 1;
  96. }
  97. }
  98. /*****************************************************************************
  99. *
  100. * Read a MII PHY register.
  101. *
  102. * Returns:
  103. * 0 on success
  104. */
  105. int miiphy_read(unsigned char addr,
  106. unsigned char reg,
  107. unsigned short *value)
  108. {
  109. short rdreg; /* register working value */
  110. int j; /* counter */
  111. volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, MDIO_PORT);
  112. miiphy_pre(1, addr, reg);
  113. /* tri-state our MDIO I/O pin so we can read */
  114. MDC(0);
  115. MDIO_TRISTATE;
  116. MIIDELAY;
  117. MDC(1);
  118. MIIDELAY;
  119. /* check the turnaround bit: the PHY should be driving it to zero */
  120. if(MDIO_READ != 0)
  121. {
  122. /* puts ("PHY didn't drive TA low\n"); */
  123. for(j = 0; j < 32; j++)
  124. {
  125. MDC(0);
  126. MIIDELAY;
  127. MDC(1);
  128. MIIDELAY;
  129. }
  130. return(-1);
  131. }
  132. MDC(0);
  133. MIIDELAY;
  134. /* read 16 bits of register data, MSB first */
  135. rdreg = 0;
  136. for(j = 0; j < 16; j++)
  137. {
  138. MDC(1);
  139. MIIDELAY;
  140. rdreg <<= 1;
  141. rdreg |= MDIO_READ;
  142. MDC(0);
  143. MIIDELAY;
  144. }
  145. MDC(1);
  146. MIIDELAY;
  147. MDC(0);
  148. MIIDELAY;
  149. MDC(1);
  150. MIIDELAY;
  151. *value = rdreg;
  152. #ifdef DEBUG
  153. printf ("miiphy_read(0x%x) @ 0x%x = 0x%04x\n", reg, addr, *value);
  154. #endif
  155. return 0;
  156. }
  157. /*****************************************************************************
  158. *
  159. * Write a MII PHY register.
  160. *
  161. * Returns:
  162. * 0 on success
  163. */
  164. int miiphy_write(unsigned char addr,
  165. unsigned char reg,
  166. unsigned short value)
  167. {
  168. int j; /* counter */
  169. volatile ioport_t *iop = ioport_addr((immap_t *)CFG_IMMR, MDIO_PORT);
  170. miiphy_pre(0, addr, reg);
  171. /* send the turnaround (10) */
  172. MDC(0); MDIO(1); MIIDELAY; MDC(1); MIIDELAY;
  173. MDC(0); MDIO(0); MIIDELAY; MDC(1); MIIDELAY;
  174. /* write 16 bits of register data, MSB first */
  175. for(j = 0; j < 16; j++)
  176. {
  177. MDC(0);
  178. if((value & 0x00008000) == 0)
  179. {
  180. MDIO(0);
  181. }
  182. else
  183. {
  184. MDIO(1);
  185. }
  186. MIIDELAY;
  187. MDC(1);
  188. MIIDELAY;
  189. value <<= 1;
  190. }
  191. /*
  192. * Tri-state the MDIO line.
  193. */
  194. MDIO_TRISTATE;
  195. MDC(0);
  196. MIIDELAY;
  197. MDC(1);
  198. MIIDELAY;
  199. return 0;
  200. }
  201. #endif /* CONFIG_BITBANGMII */