xilinx_emaclite.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 u32 etherrxbuff[PKTSIZE_ALIGN/4]; /* Receive buffer */
  64. /* hardcoded MAC address for the Xilinx EMAC Core when env is nowhere*/
  65. #ifdef CONFIG_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. uchar enetaddr[6];
  120. debug ("EmacLite Initialization Started\n");
  121. memset (&emaclite, 0, sizeof (xemaclite));
  122. emaclite.baseaddress = XILINX_EMACLITE_BASEADDR;
  123. if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
  124. memcpy(enetaddr, emacaddr, ENET_ADDR_LENGTH);
  125. eth_setenv_enetaddr("ethaddr", enetaddr);
  126. }
  127. /*
  128. * TX - TX_PING & TX_PONG initialization
  129. */
  130. /* Restart PING TX */
  131. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET, 0);
  132. /* Copy MAC address */
  133. xemaclite_alignedwrite (enetaddr,
  134. emaclite.baseaddress, ENET_ADDR_LENGTH);
  135. /* Set the length */
  136. out_be32 (emaclite.baseaddress + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH);
  137. /* Update the MAC address in the EMAC Lite */
  138. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET, XEL_TSR_PROG_MAC_ADDR);
  139. /* Wait for EMAC Lite to finish with the MAC address update */
  140. while ((in_be32 (emaclite.baseaddress + XEL_TSR_OFFSET) &
  141. XEL_TSR_PROG_MAC_ADDR) != 0) ;
  142. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  143. /* The same operation with PONG TX */
  144. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET, 0);
  145. xemaclite_alignedwrite (enetaddr, emaclite.baseaddress +
  146. XEL_BUFFER_OFFSET, ENET_ADDR_LENGTH);
  147. out_be32 (emaclite.baseaddress + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH);
  148. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET,
  149. XEL_TSR_PROG_MAC_ADDR);
  150. while ((in_be32 (emaclite.baseaddress + XEL_TSR_OFFSET +
  151. XEL_BUFFER_OFFSET) & XEL_TSR_PROG_MAC_ADDR) != 0) ;
  152. #endif
  153. /*
  154. * RX - RX_PING & RX_PONG initialization
  155. */
  156. /* Write out the value to flush the RX buffer */
  157. out_be32 (emaclite.baseaddress + XEL_RSR_OFFSET, XEL_RSR_RECV_IE_MASK);
  158. #ifdef CONFIG_XILINX_EMACLITE_RX_PING_PONG
  159. out_be32 (emaclite.baseaddress + XEL_RSR_OFFSET + XEL_BUFFER_OFFSET,
  160. XEL_RSR_RECV_IE_MASK);
  161. #endif
  162. debug ("EmacLite Initialization complete\n");
  163. return 0;
  164. }
  165. int xemaclite_txbufferavailable (xemaclite * instanceptr)
  166. {
  167. u32 reg;
  168. u32 txpingbusy;
  169. u32 txpongbusy;
  170. /*
  171. * Read the other buffer register
  172. * and determine if the other buffer is available
  173. */
  174. reg = in_be32 (instanceptr->baseaddress +
  175. instanceptr->nexttxbuffertouse + 0);
  176. txpingbusy = ((reg & XEL_TSR_XMIT_BUSY_MASK) ==
  177. XEL_TSR_XMIT_BUSY_MASK);
  178. reg = in_be32 (instanceptr->baseaddress +
  179. (instanceptr->nexttxbuffertouse ^ XEL_TSR_OFFSET) + 0);
  180. txpongbusy = ((reg & XEL_TSR_XMIT_BUSY_MASK) ==
  181. XEL_TSR_XMIT_BUSY_MASK);
  182. return (!(txpingbusy && txpongbusy));
  183. }
  184. int eth_send (volatile void *ptr, int len) {
  185. unsigned int reg;
  186. unsigned int baseaddress;
  187. unsigned maxtry = 1000;
  188. if (len > ENET_MAX_MTU)
  189. len = ENET_MAX_MTU;
  190. while (!xemaclite_txbufferavailable (&emaclite) && maxtry) {
  191. udelay (10);
  192. maxtry--;
  193. }
  194. if (!maxtry) {
  195. printf ("Error: Timeout waiting for ethernet TX buffer\n");
  196. /* Restart PING TX */
  197. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET, 0);
  198. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  199. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET +
  200. XEL_BUFFER_OFFSET, 0);
  201. #endif
  202. return 0;
  203. }
  204. /* Determine the expected TX buffer address */
  205. baseaddress = (emaclite.baseaddress + emaclite.nexttxbuffertouse);
  206. /* Determine if the expected buffer address is empty */
  207. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  208. if (((reg & XEL_TSR_XMIT_BUSY_MASK) == 0)
  209. && ((in_be32 ((baseaddress) + XEL_TSR_OFFSET)
  210. & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
  211. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  212. emaclite.nexttxbuffertouse ^= XEL_BUFFER_OFFSET;
  213. #endif
  214. debug ("Send packet from 0x%x\n", baseaddress);
  215. /* Write the frame to the buffer */
  216. xemaclite_alignedwrite ((void *) ptr, baseaddress, len);
  217. out_be32 (baseaddress + XEL_TPLR_OFFSET,(len &
  218. (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO)));
  219. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  220. reg |= XEL_TSR_XMIT_BUSY_MASK;
  221. if ((reg & XEL_TSR_XMIT_IE_MASK) != 0) {
  222. reg |= XEL_TSR_XMIT_ACTIVE_MASK;
  223. }
  224. out_be32 (baseaddress + XEL_TSR_OFFSET, reg);
  225. return 1;
  226. }
  227. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  228. /* Switch to second buffer */
  229. baseaddress ^= XEL_BUFFER_OFFSET;
  230. /* Determine if the expected buffer address is empty */
  231. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  232. if (((reg & XEL_TSR_XMIT_BUSY_MASK) == 0)
  233. && ((in_be32 ((baseaddress) + XEL_TSR_OFFSET)
  234. & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
  235. debug ("Send packet from 0x%x\n", baseaddress);
  236. /* Write the frame to the buffer */
  237. xemaclite_alignedwrite ((void *) ptr, baseaddress, len);
  238. out_be32 (baseaddress + XEL_TPLR_OFFSET,(len &
  239. (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO)));
  240. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  241. reg |= XEL_TSR_XMIT_BUSY_MASK;
  242. if ((reg & XEL_TSR_XMIT_IE_MASK) != 0) {
  243. reg |= XEL_TSR_XMIT_ACTIVE_MASK;
  244. }
  245. out_be32 (baseaddress + XEL_TSR_OFFSET, reg);
  246. return 1;
  247. }
  248. #endif
  249. puts ("Error while sending frame\n");
  250. return 0;
  251. }
  252. int eth_rx (void)
  253. {
  254. unsigned int length;
  255. unsigned int reg;
  256. unsigned int baseaddress;
  257. baseaddress = emaclite.baseaddress + emaclite.nextrxbuffertouse;
  258. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  259. debug ("Testing data at address 0x%x\n", baseaddress);
  260. if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
  261. #ifdef CONFIG_XILINX_EMACLITE_RX_PING_PONG
  262. emaclite.nextrxbuffertouse ^= XEL_BUFFER_OFFSET;
  263. #endif
  264. } else {
  265. #ifndef CONFIG_XILINX_EMACLITE_RX_PING_PONG
  266. debug ("No data was available - address 0x%x\n", baseaddress);
  267. return 0;
  268. #else
  269. baseaddress ^= XEL_BUFFER_OFFSET;
  270. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  271. if ((reg & XEL_RSR_RECV_DONE_MASK) !=
  272. XEL_RSR_RECV_DONE_MASK) {
  273. debug ("No data was available - address 0x%x\n",
  274. baseaddress);
  275. return 0;
  276. }
  277. #endif
  278. }
  279. /* Get the length of the frame that arrived */
  280. switch(((in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0xC)) &
  281. 0xFFFF0000 ) >> 16) {
  282. case 0x806:
  283. length = 42 + 20; /* FIXME size of ARP */
  284. debug ("ARP Packet\n");
  285. break;
  286. case 0x800:
  287. length = 14 + 14 +
  288. (((in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0x10)) &
  289. 0xFFFF0000) >> 16); /* FIXME size of IP packet */
  290. debug ("IP Packet\n");
  291. break;
  292. default:
  293. debug ("Other Packet\n");
  294. length = ENET_MAX_MTU;
  295. break;
  296. }
  297. xemaclite_alignedread ((u32 *) (baseaddress + XEL_RXBUFF_OFFSET),
  298. etherrxbuff, length);
  299. /* Acknowledge the frame */
  300. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  301. reg &= ~XEL_RSR_RECV_DONE_MASK;
  302. out_be32 (baseaddress + XEL_RSR_OFFSET, reg);
  303. debug ("Packet receive from 0x%x, length %dB\n", baseaddress, length);
  304. NetReceive ((uchar *) etherrxbuff, length);
  305. return 1;
  306. }