xilinx_emaclite.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * (C) Copyright 2007 Michal Simek
  3. *
  4. * Michal SIMEK <monstr@monstr.eu>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <net.h>
  26. #include <config.h>
  27. #include <asm/io.h>
  28. #ifdef XILINX_EMACLITE_BASEADDR
  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 char etherrxbuff[PKTSIZE_ALIGN/4]; /* Receive buffer */
  65. /* hardcoded MAC address for the Xilinx EMAC Core when env is nowhere*/
  66. #ifdef CFG_ENV_IS_NOWHERE
  67. static u8 emacaddr[ENET_ADDR_LENGTH] = { 0x00, 0x0a, 0x35, 0x00, 0x22, 0x01 };
  68. #endif
  69. void xemaclite_alignedread (u32 * srcptr, void *destptr, unsigned bytecount)
  70. {
  71. unsigned int i;
  72. u32 alignbuffer;
  73. u32 *to32ptr;
  74. u32 *from32ptr;
  75. u8 *to8ptr;
  76. u8 *from8ptr;
  77. from32ptr = (u32 *) srcptr;
  78. /* Word aligned buffer, no correction needed. */
  79. to32ptr = (u32 *) destptr;
  80. while (bytecount > 3) {
  81. *to32ptr++ = *from32ptr++;
  82. bytecount -= 4;
  83. }
  84. to8ptr = (u8 *) to32ptr;
  85. alignbuffer = *from32ptr++;
  86. from8ptr = (u8 *) & alignbuffer;
  87. for (i = 0; i < bytecount; i++) {
  88. *to8ptr++ = *from8ptr++;
  89. }
  90. }
  91. void xemaclite_alignedwrite (void *srcptr, u32 destptr, unsigned bytecount)
  92. {
  93. unsigned i;
  94. u32 alignbuffer;
  95. u32 *to32ptr = (u32 *) destptr;
  96. u32 *from32ptr;
  97. u8 *to8ptr;
  98. u8 *from8ptr;
  99. from32ptr = (u32 *) srcptr;
  100. while (bytecount > 3) {
  101. *to32ptr++ = *from32ptr++;
  102. bytecount -= 4;
  103. }
  104. alignbuffer = 0;
  105. to8ptr = (u8 *) & alignbuffer;
  106. from8ptr = (u8 *) from32ptr;
  107. for (i = 0; i < bytecount; i++) {
  108. *to8ptr++ = *from8ptr++;
  109. }
  110. *to32ptr++ = alignbuffer;
  111. }
  112. void eth_halt (void)
  113. {
  114. #ifdef DEBUG
  115. puts ("eth_halt\n");
  116. #endif
  117. }
  118. int eth_init (bd_t * bis)
  119. {
  120. #ifdef DEBUG
  121. puts ("EmacLite Initialization Started\n");
  122. #endif
  123. memset (&emaclite, 0, sizeof (xemaclite));
  124. emaclite.baseaddress = XILINX_EMACLITE_BASEADDR;
  125. if (!getenv("ethaddr")) {
  126. memcpy(bis->bi_enetaddr, emacaddr, ENET_ADDR_LENGTH);
  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 (bis->bi_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 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 (bis->bi_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 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. #ifdef DEBUG
  164. puts ("EmacLite Initialization complete\n");
  165. #endif
  166. return 0;
  167. }
  168. int xemaclite_txbufferavailable (xemaclite * instanceptr)
  169. {
  170. u32 reg;
  171. u32 txpingbusy;
  172. u32 txpongbusy;
  173. /*
  174. * Read the other buffer register
  175. * and determine if the other buffer is available
  176. */
  177. reg = in_be32 (instanceptr->baseaddress +
  178. instanceptr->nexttxbuffertouse + 0);
  179. txpingbusy = ((reg & XEL_TSR_XMIT_BUSY_MASK) ==
  180. XEL_TSR_XMIT_BUSY_MASK);
  181. reg = in_be32 (instanceptr->baseaddress +
  182. (instanceptr->nexttxbuffertouse ^ XEL_TSR_OFFSET) + 0);
  183. txpongbusy = ((reg & XEL_TSR_XMIT_BUSY_MASK) ==
  184. XEL_TSR_XMIT_BUSY_MASK);
  185. return (!(txpingbusy && txpongbusy));
  186. }
  187. int eth_send (volatile void *ptr, int len) {
  188. unsigned int reg;
  189. unsigned int baseaddress;
  190. unsigned maxtry = 1000;
  191. if (len > ENET_MAX_MTU)
  192. len = ENET_MAX_MTU;
  193. while (!xemaclite_txbufferavailable (&emaclite) && maxtry) {
  194. udelay (10);
  195. maxtry--;
  196. }
  197. if (!maxtry) {
  198. printf ("Error: Timeout waiting for ethernet TX buffer\n");
  199. /* Restart PING TX */
  200. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET, 0);
  201. #ifdef XILINX_EMACLITE_TX_PING_PONG
  202. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET +
  203. XEL_BUFFER_OFFSET, 0);
  204. #endif
  205. return 0;
  206. }
  207. /* Determine the expected TX buffer address */
  208. baseaddress = (emaclite.baseaddress + emaclite.nexttxbuffertouse);
  209. /* Determine if the expected buffer address is empty */
  210. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  211. if (((reg & XEL_TSR_XMIT_BUSY_MASK) == 0)
  212. && ((in_be32 ((baseaddress) + XEL_TSR_OFFSET)
  213. & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
  214. #ifdef XILINX_EMACLITE_TX_PING_PONG
  215. emaclite.nexttxbuffertouse ^= XEL_BUFFER_OFFSET;
  216. #endif
  217. #ifdef DEBUG
  218. printf ("Send packet from 0x%x\n", baseaddress);
  219. #endif
  220. /* Write the frame to the buffer */
  221. xemaclite_alignedwrite ((void *) ptr, baseaddress, len);
  222. out_be32 (baseaddress + XEL_TPLR_OFFSET,(len &
  223. (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO)));
  224. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  225. reg |= XEL_TSR_XMIT_BUSY_MASK;
  226. if ((reg & XEL_TSR_XMIT_IE_MASK) != 0) {
  227. reg |= XEL_TSR_XMIT_ACTIVE_MASK;
  228. }
  229. out_be32 (baseaddress + XEL_TSR_OFFSET, reg);
  230. return 1;
  231. }
  232. #ifdef XILINX_EMACLITE_TX_PING_PONG
  233. /* Switch to second buffer */
  234. baseaddress ^= XEL_BUFFER_OFFSET;
  235. /* Determine if the expected buffer address is empty */
  236. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  237. if (((reg & XEL_TSR_XMIT_BUSY_MASK) == 0)
  238. && ((in_be32 ((baseaddress) + XEL_TSR_OFFSET)
  239. & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
  240. #ifdef DEBUG
  241. printf ("Send packet from 0x%x\n", baseaddress);
  242. #endif
  243. /* Write the frame to the buffer */
  244. xemaclite_alignedwrite ((void *) ptr, baseaddress, len);
  245. out_be32 (baseaddress + XEL_TPLR_OFFSET,(len &
  246. (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO)));
  247. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  248. reg |= XEL_TSR_XMIT_BUSY_MASK;
  249. if ((reg & XEL_TSR_XMIT_IE_MASK) != 0) {
  250. reg |= XEL_TSR_XMIT_ACTIVE_MASK;
  251. }
  252. out_be32 (baseaddress + XEL_TSR_OFFSET, reg);
  253. return 1;
  254. }
  255. #endif
  256. puts ("Error while sending frame\n");
  257. return 0;
  258. }
  259. int eth_rx (void)
  260. {
  261. unsigned int length;
  262. unsigned int reg;
  263. unsigned int baseaddress;
  264. baseaddress = emaclite.baseaddress + emaclite.nextrxbuffertouse;
  265. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  266. #ifdef DEBUG
  267. printf ("Testing data at address 0x%x\n", baseaddress);
  268. #endif
  269. if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
  270. #ifdef XILINX_EMACLITE_RX_PING_PONG
  271. emaclite.nextrxbuffertouse ^= XEL_BUFFER_OFFSET;
  272. #endif
  273. } else {
  274. #ifndef XILINX_EMACLITE_RX_PING_PONG
  275. #ifdef DEBUG
  276. printf ("No data was available - address 0x%x\n", baseaddress);
  277. #endif
  278. return 0;
  279. #else
  280. baseaddress ^= XEL_BUFFER_OFFSET;
  281. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  282. if ((reg & XEL_RSR_RECV_DONE_MASK) !=
  283. XEL_RSR_RECV_DONE_MASK) {
  284. #ifdef DEBUG
  285. printf ("No data was available - address 0x%x\n",
  286. baseaddress);
  287. #endif
  288. return 0;
  289. }
  290. #endif
  291. }
  292. /* Get the length of the frame that arrived */
  293. switch(((in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0xC)) &
  294. 0xFFFF0000 ) >> 16) {
  295. case 0x806:
  296. length = 42 + 20; /* FIXME size of ARP */
  297. #ifdef DEBUG
  298. puts ("ARP Packet\n");
  299. #endif
  300. break;
  301. case 0x800:
  302. length = 14 + 14 +
  303. (((in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0x10)) &
  304. 0xFFFF0000) >> 16); /* FIXME size of IP packet */
  305. #ifdef DEBUG
  306. puts("IP Packet\n");
  307. #endif
  308. break;
  309. default:
  310. #ifdef DEBUG
  311. puts("Other Packet\n");
  312. #endif
  313. length = ENET_MAX_MTU;
  314. break;
  315. }
  316. xemaclite_alignedread ((u32 *) (baseaddress + XEL_RXBUFF_OFFSET),
  317. etherrxbuff, length);
  318. /* Acknowledge the frame */
  319. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  320. reg &= ~XEL_RSR_RECV_DONE_MASK;
  321. out_be32 (baseaddress + XEL_RSR_OFFSET, reg);
  322. #ifdef DEBUG
  323. printf ("Packet receive from 0x%x, length %dB\n", baseaddress, length);
  324. #endif
  325. NetReceive ((uchar *) etherrxbuff, length);
  326. return 1;
  327. }
  328. #endif