fec.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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. #include <common.h>
  24. #include <commproc.h>
  25. #include <net.h>
  26. #include <command.h>
  27. /**************************************************************
  28. *
  29. * FEC Ethernet Initialization Routine
  30. *
  31. *************************************************************/
  32. #define FEC_ECNTRL_ETHER_EN 0x00000002
  33. #define FEC_ECNTRL_RESET 0x00000001
  34. #define FEC_RCNTRL_BC_REJ 0x00000010
  35. #define FEC_RCNTRL_PROM 0x00000008
  36. #define FEC_RCNTRL_MII_MODE 0x00000004
  37. #define FEC_RCNTRL_DRT 0x00000002
  38. #define FEC_RCNTRL_LOOP 0x00000001
  39. #define FEC_TCNTRL_FDEN 0x00000004
  40. #define FEC_TCNTRL_HBC 0x00000002
  41. #define FEC_TCNTRL_GTS 0x00000001
  42. #define FEC_RESET_DELAY 50000
  43. /* Ethernet Transmit and Receive Buffers */
  44. #define DBUF_LENGTH 1520
  45. #define TX_BUF_CNT 2
  46. #define TOUT_LOOP 100
  47. #define PKT_MAXBUF_SIZE 1518
  48. #define PKT_MINBUF_SIZE 64
  49. #define PKT_MAXBLR_SIZE 1520
  50. #ifdef CONFIG_M5272
  51. #define FEC_ADDR 0x10000840
  52. #endif
  53. #ifdef CONFIG_M5282
  54. #define FEC_ADDR 0x40001000
  55. #endif
  56. #undef ET_DEBUG
  57. #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(FEC_ENET)
  58. static char txbuf[DBUF_LENGTH];
  59. static uint rxIdx; /* index of the current RX buffer */
  60. static uint txIdx; /* index of the current TX buffer */
  61. /*
  62. * FEC Ethernet Tx and Rx buffer descriptors allocated at the
  63. * immr->udata_bd address on Dual-Port RAM
  64. * Provide for Double Buffering
  65. */
  66. typedef volatile struct CommonBufferDescriptor {
  67. cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
  68. cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
  69. } RTXBD;
  70. static RTXBD *rtx = 0x380000;
  71. int eth_send (volatile void *packet, int length)
  72. {
  73. int j, rc;
  74. volatile fec_t *fecp = FEC_ADDR;
  75. /* section 16.9.23.3
  76. * Wait for ready
  77. */
  78. j = 0;
  79. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY)
  80. && (j < TOUT_LOOP)) {
  81. udelay (1);
  82. j++;
  83. }
  84. if (j >= TOUT_LOOP) {
  85. printf ("TX not ready\n");
  86. }
  87. rtx->txbd[txIdx].cbd_bufaddr = (uint) packet;
  88. rtx->txbd[txIdx].cbd_datlen = length;
  89. rtx->txbd[txIdx].cbd_sc |= BD_ENET_TX_READY | BD_ENET_TX_LAST;
  90. /* Activate transmit Buffer Descriptor polling */
  91. fecp->fec_x_des_active = 0x01000000; /* Descriptor polling active */
  92. j = 0;
  93. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY)
  94. && (j < TOUT_LOOP)) {
  95. udelay (1);
  96. j++;
  97. }
  98. if (j >= TOUT_LOOP) {
  99. printf ("TX timeout\n");
  100. }
  101. #ifdef ET_DEBUG
  102. printf ("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
  103. __FILE__, __LINE__, __FUNCTION__, j, rtx->txbd[txIdx].cbd_sc,
  104. (rtx->txbd[txIdx].cbd_sc & 0x003C) >> 2);
  105. #endif
  106. /* return only status bits */ ;
  107. rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS);
  108. txIdx = (txIdx + 1) % TX_BUF_CNT;
  109. return rc;
  110. }
  111. int eth_rx (void)
  112. {
  113. int length;
  114. volatile fec_t *fecp = FEC_ADDR;
  115. for (;;) {
  116. /* section 16.9.23.2 */
  117. if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  118. length = -1;
  119. break; /* nothing received - leave for() loop */
  120. }
  121. length = rtx->rxbd[rxIdx].cbd_datlen;
  122. if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) {
  123. #ifdef ET_DEBUG
  124. printf ("%s[%d] err: %x\n",
  125. __FUNCTION__, __LINE__,
  126. rtx->rxbd[rxIdx].cbd_sc);
  127. #endif
  128. } else {
  129. /* Pass the packet up to the protocol layers. */
  130. NetReceive (NetRxPackets[rxIdx], length - 4);
  131. }
  132. /* Give the buffer back to the FEC. */
  133. rtx->rxbd[rxIdx].cbd_datlen = 0;
  134. /* wrap around buffer index when necessary */
  135. if ((rxIdx + 1) >= PKTBUFSRX) {
  136. rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
  137. (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  138. rxIdx = 0;
  139. } else {
  140. rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  141. rxIdx++;
  142. }
  143. /* Try to fill Buffer Descriptors */
  144. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  145. }
  146. return length;
  147. }
  148. int eth_init (bd_t * bd)
  149. {
  150. int i;
  151. volatile fec_t *fecp = FEC_ADDR;
  152. /* Whack a reset.
  153. * A delay is required between a reset of the FEC block and
  154. * initialization of other FEC registers because the reset takes
  155. * some time to complete. If you don't delay, subsequent writes
  156. * to FEC registers might get killed by the reset routine which is
  157. * still in progress.
  158. */
  159. fecp->fec_ecntrl = FEC_ECNTRL_RESET;
  160. for (i = 0;
  161. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  162. ++i) {
  163. udelay (1);
  164. }
  165. if (i == FEC_RESET_DELAY) {
  166. printf ("FEC_RESET_DELAY timeout\n");
  167. return 0;
  168. }
  169. /* We use strictly polling mode only
  170. */
  171. fecp->fec_imask = 0;
  172. /* Clear any pending interrupt */
  173. fecp->fec_ievent = 0xffffffff;
  174. /* Set station address */
  175. #define ea bd->bi_enetaddr
  176. fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) |
  177. (ea[2] << 8) | (ea[3]);
  178. fecp->fec_addr_high = (ea[4] << 24) | (ea[5] << 16);
  179. #undef ea
  180. /* Clear multicast address hash table
  181. */
  182. fecp->fec_hash_table_high = 0;
  183. fecp->fec_hash_table_low = 0;
  184. /* Set maximum receive buffer size.
  185. */
  186. fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
  187. /*
  188. * Setup Buffers and Buffer Desriptors
  189. */
  190. rxIdx = 0;
  191. txIdx = 0;
  192. /*
  193. * Setup Receiver Buffer Descriptors (13.14.24.18)
  194. * Settings:
  195. * Empty, Wrap
  196. */
  197. for (i = 0; i < PKTBUFSRX; i++) {
  198. rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  199. rtx->rxbd[i].cbd_datlen = 0; /* Reset */
  200. rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
  201. }
  202. rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  203. /*
  204. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  205. * Settings:
  206. * Last, Tx CRC
  207. */
  208. for (i = 0; i < TX_BUF_CNT; i++) {
  209. rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
  210. rtx->txbd[i].cbd_datlen = 0; /* Reset */
  211. rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
  212. }
  213. rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  214. /* Set receive and transmit descriptor base
  215. */
  216. fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
  217. fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
  218. /* Enable MII mode
  219. */
  220. /* Half duplex mode */
  221. fecp->fec_r_cntrl = (PKT_MAXBUF_SIZE << 16) | FEC_RCNTRL_MII_MODE;
  222. fecp->fec_r_cntrl = (PKT_MAXBUF_SIZE << 16) | FEC_RCNTRL_MII_MODE;
  223. fecp->fec_x_cntrl = 0;
  224. fecp->fec_mii_speed = 0;
  225. /* Now enable the transmit and receive processing
  226. */
  227. fecp->fec_ecntrl = FEC_ECNTRL_ETHER_EN;
  228. /* And last, try to fill Rx Buffer Descriptors */
  229. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  230. return 1;
  231. }
  232. void eth_halt (void)
  233. {
  234. volatile fec_t *fecp = FEC_ADDR;
  235. fecp->fec_ecntrl = 0;
  236. }
  237. #endif