xilinx_emaclite.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /******************************************************************************
  2. *
  3. * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
  4. * AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND
  5. * SOLUTIONS FOR XILINX DEVICES. BY PROVIDING THIS DESIGN, CODE,
  6. * OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,
  7. * APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION
  8. * THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,
  9. * AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE
  10. * FOR YOUR IMPLEMENTATION. XILINX EXPRESSLY DISCLAIMS ANY
  11. * WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE
  12. * IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR
  13. * REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF
  14. * INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  15. * FOR A PARTICULAR PURPOSE.
  16. *
  17. * (C) Copyright 2007-2008 Michal Simek
  18. * Michal SIMEK <monstr@monstr.eu>
  19. *
  20. * (c) Copyright 2003 Xilinx Inc.
  21. * All rights reserved.
  22. *
  23. ******************************************************************************/
  24. #include <common.h>
  25. #include <net.h>
  26. #include <config.h>
  27. #include <asm/io.h>
  28. #undef DEBUG
  29. #define ENET_MAX_MTU PKTSIZE
  30. #define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
  31. #define ENET_ADDR_LENGTH 6
  32. /* EmacLite constants */
  33. #define XEL_BUFFER_OFFSET 0x0800 /* Next buffer's offset */
  34. #define XEL_TPLR_OFFSET 0x07F4 /* Tx packet length */
  35. #define XEL_TSR_OFFSET 0x07FC /* Tx status */
  36. #define XEL_RSR_OFFSET 0x17FC /* Rx status */
  37. #define XEL_RXBUFF_OFFSET 0x1000 /* Receive Buffer */
  38. /* Xmit complete */
  39. #define XEL_TSR_XMIT_BUSY_MASK 0x00000001UL
  40. /* Xmit interrupt enable bit */
  41. #define XEL_TSR_XMIT_IE_MASK 0x00000008UL
  42. /* Buffer is active, SW bit only */
  43. #define XEL_TSR_XMIT_ACTIVE_MASK 0x80000000UL
  44. /* Program the MAC address */
  45. #define XEL_TSR_PROGRAM_MASK 0x00000002UL
  46. /* define for programming the MAC address into the EMAC Lite */
  47. #define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
  48. /* Transmit packet length upper byte */
  49. #define XEL_TPLR_LENGTH_MASK_HI 0x0000FF00UL
  50. /* Transmit packet length lower byte */
  51. #define XEL_TPLR_LENGTH_MASK_LO 0x000000FFUL
  52. /* Recv complete */
  53. #define XEL_RSR_RECV_DONE_MASK 0x00000001UL
  54. /* Recv interrupt enable bit */
  55. #define XEL_RSR_RECV_IE_MASK 0x00000008UL
  56. typedef struct {
  57. unsigned int baseaddress; /* Base address for device (IPIF) */
  58. unsigned int nexttxbuffertouse; /* Next TX buffer to write to */
  59. unsigned int nextrxbuffertouse; /* Next RX buffer to read from */
  60. unsigned char deviceid; /* Unique ID of device - for future */
  61. } xemaclite;
  62. static xemaclite emaclite;
  63. static char etherrxbuff[PKTSIZE_ALIGN/4]; /* Receive buffer */
  64. /* hardcoded MAC address for the Xilinx EMAC Core when env is nowhere*/
  65. #ifdef CFG_ENV_IS_NOWHERE
  66. static u8 emacaddr[ENET_ADDR_LENGTH] = { 0x00, 0x0a, 0x35, 0x00, 0x22, 0x01 };
  67. #else
  68. static u8 emacaddr[ENET_ADDR_LENGTH];
  69. #endif
  70. void xemaclite_alignedread (u32 * srcptr, void *destptr, unsigned bytecount)
  71. {
  72. unsigned int i;
  73. u32 alignbuffer;
  74. u32 *to32ptr;
  75. u32 *from32ptr;
  76. u8 *to8ptr;
  77. u8 *from8ptr;
  78. from32ptr = (u32 *) srcptr;
  79. /* Word aligned buffer, no correction needed. */
  80. to32ptr = (u32 *) destptr;
  81. while (bytecount > 3) {
  82. *to32ptr++ = *from32ptr++;
  83. bytecount -= 4;
  84. }
  85. to8ptr = (u8 *) to32ptr;
  86. alignbuffer = *from32ptr++;
  87. from8ptr = (u8 *) & alignbuffer;
  88. for (i = 0; i < bytecount; i++) {
  89. *to8ptr++ = *from8ptr++;
  90. }
  91. }
  92. void xemaclite_alignedwrite (void *srcptr, u32 destptr, unsigned bytecount)
  93. {
  94. unsigned i;
  95. u32 alignbuffer;
  96. u32 *to32ptr = (u32 *) destptr;
  97. u32 *from32ptr;
  98. u8 *to8ptr;
  99. u8 *from8ptr;
  100. from32ptr = (u32 *) srcptr;
  101. while (bytecount > 3) {
  102. *to32ptr++ = *from32ptr++;
  103. bytecount -= 4;
  104. }
  105. alignbuffer = 0;
  106. to8ptr = (u8 *) & alignbuffer;
  107. from8ptr = (u8 *) from32ptr;
  108. for (i = 0; i < bytecount; i++) {
  109. *to8ptr++ = *from8ptr++;
  110. }
  111. *to32ptr++ = alignbuffer;
  112. }
  113. void eth_halt (void)
  114. {
  115. debug ("eth_halt\n");
  116. }
  117. int eth_init (bd_t * bis)
  118. {
  119. debug ("EmacLite Initialization Started\n");
  120. memset (&emaclite, 0, sizeof (xemaclite));
  121. emaclite.baseaddress = XILINX_EMACLITE_BASEADDR;
  122. if (!getenv("ethaddr")) {
  123. memcpy(bis->bi_enetaddr, emacaddr, ENET_ADDR_LENGTH);
  124. }
  125. /*
  126. * TX - TX_PING & TX_PONG initialization
  127. */
  128. /* Restart PING TX */
  129. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET, 0);
  130. /* Copy MAC address */
  131. xemaclite_alignedwrite (bis->bi_enetaddr,
  132. emaclite.baseaddress, ENET_ADDR_LENGTH);
  133. /* Set the length */
  134. out_be32 (emaclite.baseaddress + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH);
  135. /* Update the MAC address in the EMAC Lite */
  136. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET, XEL_TSR_PROG_MAC_ADDR);
  137. /* Wait for EMAC Lite to finish with the MAC address update */
  138. while ((in_be32 (emaclite.baseaddress + XEL_TSR_OFFSET) &
  139. XEL_TSR_PROG_MAC_ADDR) != 0) ;
  140. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  141. /* The same operation with PONG TX */
  142. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET, 0);
  143. xemaclite_alignedwrite (bis->bi_enetaddr, emaclite.baseaddress +
  144. XEL_BUFFER_OFFSET, ENET_ADDR_LENGTH);
  145. out_be32 (emaclite.baseaddress + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH);
  146. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET,
  147. XEL_TSR_PROG_MAC_ADDR);
  148. while ((in_be32 (emaclite.baseaddress + XEL_TSR_OFFSET +
  149. XEL_BUFFER_OFFSET) & XEL_TSR_PROG_MAC_ADDR) != 0) ;
  150. #endif
  151. /*
  152. * RX - RX_PING & RX_PONG initialization
  153. */
  154. /* Write out the value to flush the RX buffer */
  155. out_be32 (emaclite.baseaddress + XEL_RSR_OFFSET, XEL_RSR_RECV_IE_MASK);
  156. #ifdef CONFIG_XILINX_EMACLITE_RX_PING_PONG
  157. out_be32 (emaclite.baseaddress + XEL_RSR_OFFSET + XEL_BUFFER_OFFSET,
  158. XEL_RSR_RECV_IE_MASK);
  159. #endif
  160. debug ("EmacLite Initialization complete\n");
  161. return 0;
  162. }
  163. int xemaclite_txbufferavailable (xemaclite * instanceptr)
  164. {
  165. u32 reg;
  166. u32 txpingbusy;
  167. u32 txpongbusy;
  168. /*
  169. * Read the other buffer register
  170. * and determine if the other buffer is available
  171. */
  172. reg = in_be32 (instanceptr->baseaddress +
  173. instanceptr->nexttxbuffertouse + 0);
  174. txpingbusy = ((reg & XEL_TSR_XMIT_BUSY_MASK) ==
  175. XEL_TSR_XMIT_BUSY_MASK);
  176. reg = in_be32 (instanceptr->baseaddress +
  177. (instanceptr->nexttxbuffertouse ^ XEL_TSR_OFFSET) + 0);
  178. txpongbusy = ((reg & XEL_TSR_XMIT_BUSY_MASK) ==
  179. XEL_TSR_XMIT_BUSY_MASK);
  180. return (!(txpingbusy && txpongbusy));
  181. }
  182. int eth_send (volatile void *ptr, int len) {
  183. unsigned int reg;
  184. unsigned int baseaddress;
  185. unsigned maxtry = 1000;
  186. if (len > ENET_MAX_MTU)
  187. len = ENET_MAX_MTU;
  188. while (!xemaclite_txbufferavailable (&emaclite) && maxtry) {
  189. udelay (10);
  190. maxtry--;
  191. }
  192. if (!maxtry) {
  193. printf ("Error: Timeout waiting for ethernet TX buffer\n");
  194. /* Restart PING TX */
  195. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET, 0);
  196. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  197. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET +
  198. XEL_BUFFER_OFFSET, 0);
  199. #endif
  200. return 0;
  201. }
  202. /* Determine the expected TX buffer address */
  203. baseaddress = (emaclite.baseaddress + emaclite.nexttxbuffertouse);
  204. /* Determine if the expected buffer address is empty */
  205. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  206. if (((reg & XEL_TSR_XMIT_BUSY_MASK) == 0)
  207. && ((in_be32 ((baseaddress) + XEL_TSR_OFFSET)
  208. & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
  209. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  210. emaclite.nexttxbuffertouse ^= XEL_BUFFER_OFFSET;
  211. #endif
  212. debug ("Send packet from 0x%x\n", baseaddress);
  213. /* Write the frame to the buffer */
  214. xemaclite_alignedwrite ((void *) ptr, baseaddress, len);
  215. out_be32 (baseaddress + XEL_TPLR_OFFSET,(len &
  216. (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO)));
  217. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  218. reg |= XEL_TSR_XMIT_BUSY_MASK;
  219. if ((reg & XEL_TSR_XMIT_IE_MASK) != 0) {
  220. reg |= XEL_TSR_XMIT_ACTIVE_MASK;
  221. }
  222. out_be32 (baseaddress + XEL_TSR_OFFSET, reg);
  223. return 1;
  224. }
  225. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  226. /* Switch to second buffer */
  227. baseaddress ^= XEL_BUFFER_OFFSET;
  228. /* Determine if the expected buffer address is empty */
  229. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  230. if (((reg & XEL_TSR_XMIT_BUSY_MASK) == 0)
  231. && ((in_be32 ((baseaddress) + XEL_TSR_OFFSET)
  232. & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
  233. debug ("Send packet from 0x%x\n", baseaddress);
  234. /* Write the frame to the buffer */
  235. xemaclite_alignedwrite ((void *) ptr, baseaddress, len);
  236. out_be32 (baseaddress + XEL_TPLR_OFFSET,(len &
  237. (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO)));
  238. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  239. reg |= XEL_TSR_XMIT_BUSY_MASK;
  240. if ((reg & XEL_TSR_XMIT_IE_MASK) != 0) {
  241. reg |= XEL_TSR_XMIT_ACTIVE_MASK;
  242. }
  243. out_be32 (baseaddress + XEL_TSR_OFFSET, reg);
  244. return 1;
  245. }
  246. #endif
  247. puts ("Error while sending frame\n");
  248. return 0;
  249. }
  250. int eth_rx (void)
  251. {
  252. unsigned int length;
  253. unsigned int reg;
  254. unsigned int baseaddress;
  255. baseaddress = emaclite.baseaddress + emaclite.nextrxbuffertouse;
  256. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  257. debug ("Testing data at address 0x%x\n", baseaddress);
  258. if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
  259. #ifdef CONFIG_XILINX_EMACLITE_RX_PING_PONG
  260. emaclite.nextrxbuffertouse ^= XEL_BUFFER_OFFSET;
  261. #endif
  262. } else {
  263. #ifndef CONFIG_XILINX_EMACLITE_RX_PING_PONG
  264. debug ("No data was available - address 0x%x\n", baseaddress);
  265. return 0;
  266. #else
  267. baseaddress ^= XEL_BUFFER_OFFSET;
  268. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  269. if ((reg & XEL_RSR_RECV_DONE_MASK) !=
  270. XEL_RSR_RECV_DONE_MASK) {
  271. debug ("No data was available - address 0x%x\n",
  272. baseaddress);
  273. return 0;
  274. }
  275. #endif
  276. }
  277. /* Get the length of the frame that arrived */
  278. switch(((in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0xC)) &
  279. 0xFFFF0000 ) >> 16) {
  280. case 0x806:
  281. length = 42 + 20; /* FIXME size of ARP */
  282. debug ("ARP Packet\n");
  283. break;
  284. case 0x800:
  285. length = 14 + 14 +
  286. (((in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0x10)) &
  287. 0xFFFF0000) >> 16); /* FIXME size of IP packet */
  288. debug ("IP Packet\n");
  289. break;
  290. default:
  291. debug ("Other Packet\n");
  292. length = ENET_MAX_MTU;
  293. break;
  294. }
  295. xemaclite_alignedread ((u32 *) (baseaddress + XEL_RXBUFF_OFFSET),
  296. etherrxbuff, length);
  297. /* Acknowledge the frame */
  298. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  299. reg &= ~XEL_RSR_RECV_DONE_MASK;
  300. out_be32 (baseaddress + XEL_RSR_OFFSET, reg);
  301. debug ("Packet receive from 0x%x, length %dB\n", baseaddress, length);
  302. NetReceive ((uchar *) etherrxbuff, length);
  303. return 1;
  304. }