xilinx_emaclite.c 10 KB

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