xilinx_emaclite.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 <malloc.h>
  29. #include <asm/io.h>
  30. #undef DEBUG
  31. #define ENET_MAX_MTU PKTSIZE
  32. #define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
  33. #define ENET_ADDR_LENGTH 6
  34. /* EmacLite constants */
  35. #define XEL_BUFFER_OFFSET 0x0800 /* Next buffer's offset */
  36. #define XEL_TPLR_OFFSET 0x07F4 /* Tx packet length */
  37. #define XEL_TSR_OFFSET 0x07FC /* Tx status */
  38. #define XEL_RSR_OFFSET 0x17FC /* Rx status */
  39. #define XEL_RXBUFF_OFFSET 0x1000 /* Receive Buffer */
  40. /* Xmit complete */
  41. #define XEL_TSR_XMIT_BUSY_MASK 0x00000001UL
  42. /* Xmit interrupt enable bit */
  43. #define XEL_TSR_XMIT_IE_MASK 0x00000008UL
  44. /* Buffer is active, SW bit only */
  45. #define XEL_TSR_XMIT_ACTIVE_MASK 0x80000000UL
  46. /* Program the MAC address */
  47. #define XEL_TSR_PROGRAM_MASK 0x00000002UL
  48. /* define for programming the MAC address into the EMAC Lite */
  49. #define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
  50. /* Transmit packet length upper byte */
  51. #define XEL_TPLR_LENGTH_MASK_HI 0x0000FF00UL
  52. /* Transmit packet length lower byte */
  53. #define XEL_TPLR_LENGTH_MASK_LO 0x000000FFUL
  54. /* Recv complete */
  55. #define XEL_RSR_RECV_DONE_MASK 0x00000001UL
  56. /* Recv interrupt enable bit */
  57. #define XEL_RSR_RECV_IE_MASK 0x00000008UL
  58. typedef struct {
  59. u32 baseaddress; /* Base address for device (IPIF) */
  60. u32 nexttxbuffertouse; /* Next TX buffer to write to */
  61. u32 nextrxbuffertouse; /* Next RX buffer to read from */
  62. uchar deviceid; /* Unique ID of device - for future */
  63. } xemaclite;
  64. static xemaclite emaclite;
  65. static u32 etherrxbuff[PKTSIZE_ALIGN/4]; /* Receive buffer */
  66. static void xemaclite_alignedread (u32 *srcptr, void *destptr, u32 bytecount)
  67. {
  68. u32 i;
  69. u32 alignbuffer;
  70. u32 *to32ptr;
  71. u32 *from32ptr;
  72. u8 *to8ptr;
  73. u8 *from8ptr;
  74. from32ptr = (u32 *) srcptr;
  75. /* Word aligned buffer, no correction needed. */
  76. to32ptr = (u32 *) destptr;
  77. while (bytecount > 3) {
  78. *to32ptr++ = *from32ptr++;
  79. bytecount -= 4;
  80. }
  81. to8ptr = (u8 *) to32ptr;
  82. alignbuffer = *from32ptr++;
  83. from8ptr = (u8 *) & alignbuffer;
  84. for (i = 0; i < bytecount; i++) {
  85. *to8ptr++ = *from8ptr++;
  86. }
  87. }
  88. static void xemaclite_alignedwrite (void *srcptr, u32 destptr, u32 bytecount)
  89. {
  90. u32 i;
  91. u32 alignbuffer;
  92. u32 *to32ptr = (u32 *) destptr;
  93. u32 *from32ptr;
  94. u8 *to8ptr;
  95. u8 *from8ptr;
  96. from32ptr = (u32 *) srcptr;
  97. while (bytecount > 3) {
  98. *to32ptr++ = *from32ptr++;
  99. bytecount -= 4;
  100. }
  101. alignbuffer = 0;
  102. to8ptr = (u8 *) & alignbuffer;
  103. from8ptr = (u8 *) from32ptr;
  104. for (i = 0; i < bytecount; i++) {
  105. *to8ptr++ = *from8ptr++;
  106. }
  107. *to32ptr++ = alignbuffer;
  108. }
  109. static void emaclite_halt(struct eth_device *dev)
  110. {
  111. debug ("eth_halt\n");
  112. }
  113. static int emaclite_init(struct eth_device *dev, bd_t *bis)
  114. {
  115. debug ("EmacLite Initialization Started\n");
  116. memset (&emaclite, 0, sizeof (xemaclite));
  117. emaclite.baseaddress = dev->iobase;
  118. /*
  119. * TX - TX_PING & TX_PONG initialization
  120. */
  121. /* Restart PING TX */
  122. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET, 0);
  123. /* Copy MAC address */
  124. xemaclite_alignedwrite (dev->enetaddr,
  125. emaclite.baseaddress, ENET_ADDR_LENGTH);
  126. /* Set the length */
  127. out_be32 (emaclite.baseaddress + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH);
  128. /* Update the MAC address in the EMAC Lite */
  129. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET, XEL_TSR_PROG_MAC_ADDR);
  130. /* Wait for EMAC Lite to finish with the MAC address update */
  131. while ((in_be32 (emaclite.baseaddress + XEL_TSR_OFFSET) &
  132. XEL_TSR_PROG_MAC_ADDR) != 0) ;
  133. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  134. /* The same operation with PONG TX */
  135. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET, 0);
  136. xemaclite_alignedwrite (dev->enetaddr, emaclite.baseaddress +
  137. XEL_BUFFER_OFFSET, ENET_ADDR_LENGTH);
  138. out_be32 (emaclite.baseaddress + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH);
  139. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET,
  140. XEL_TSR_PROG_MAC_ADDR);
  141. while ((in_be32 (emaclite.baseaddress + XEL_TSR_OFFSET +
  142. XEL_BUFFER_OFFSET) & XEL_TSR_PROG_MAC_ADDR) != 0) ;
  143. #endif
  144. /*
  145. * RX - RX_PING & RX_PONG initialization
  146. */
  147. /* Write out the value to flush the RX buffer */
  148. out_be32 (emaclite.baseaddress + XEL_RSR_OFFSET, XEL_RSR_RECV_IE_MASK);
  149. #ifdef CONFIG_XILINX_EMACLITE_RX_PING_PONG
  150. out_be32 (emaclite.baseaddress + XEL_RSR_OFFSET + XEL_BUFFER_OFFSET,
  151. XEL_RSR_RECV_IE_MASK);
  152. #endif
  153. debug ("EmacLite Initialization complete\n");
  154. return 0;
  155. }
  156. static int xemaclite_txbufferavailable (xemaclite *instanceptr)
  157. {
  158. u32 reg;
  159. u32 txpingbusy;
  160. u32 txpongbusy;
  161. /*
  162. * Read the other buffer register
  163. * and determine if the other buffer is available
  164. */
  165. reg = in_be32 (instanceptr->baseaddress +
  166. instanceptr->nexttxbuffertouse + 0);
  167. txpingbusy = ((reg & XEL_TSR_XMIT_BUSY_MASK) ==
  168. XEL_TSR_XMIT_BUSY_MASK);
  169. reg = in_be32 (instanceptr->baseaddress +
  170. (instanceptr->nexttxbuffertouse ^ XEL_TSR_OFFSET) + 0);
  171. txpongbusy = ((reg & XEL_TSR_XMIT_BUSY_MASK) ==
  172. XEL_TSR_XMIT_BUSY_MASK);
  173. return (!(txpingbusy && txpongbusy));
  174. }
  175. static int emaclite_send (struct eth_device *dev, volatile void *ptr, int len)
  176. {
  177. u32 reg;
  178. u32 baseaddress;
  179. u32 maxtry = 1000;
  180. if (len > ENET_MAX_MTU)
  181. len = ENET_MAX_MTU;
  182. while (!xemaclite_txbufferavailable (&emaclite) && maxtry) {
  183. udelay (10);
  184. maxtry--;
  185. }
  186. if (!maxtry) {
  187. printf ("Error: Timeout waiting for ethernet TX buffer\n");
  188. /* Restart PING TX */
  189. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET, 0);
  190. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  191. out_be32 (emaclite.baseaddress + XEL_TSR_OFFSET +
  192. XEL_BUFFER_OFFSET, 0);
  193. #endif
  194. return -1;
  195. }
  196. /* Determine the expected TX buffer address */
  197. baseaddress = (emaclite.baseaddress + emaclite.nexttxbuffertouse);
  198. /* Determine if the expected buffer address is empty */
  199. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  200. if (((reg & XEL_TSR_XMIT_BUSY_MASK) == 0)
  201. && ((in_be32 ((baseaddress) + XEL_TSR_OFFSET)
  202. & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
  203. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  204. emaclite.nexttxbuffertouse ^= XEL_BUFFER_OFFSET;
  205. #endif
  206. debug ("Send packet from 0x%x\n", baseaddress);
  207. /* Write the frame to the buffer */
  208. xemaclite_alignedwrite ((void *) ptr, baseaddress, len);
  209. out_be32 (baseaddress + XEL_TPLR_OFFSET,(len &
  210. (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO)));
  211. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  212. reg |= XEL_TSR_XMIT_BUSY_MASK;
  213. if ((reg & XEL_TSR_XMIT_IE_MASK) != 0) {
  214. reg |= XEL_TSR_XMIT_ACTIVE_MASK;
  215. }
  216. out_be32 (baseaddress + XEL_TSR_OFFSET, reg);
  217. return 0;
  218. }
  219. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  220. /* Switch to second buffer */
  221. baseaddress ^= XEL_BUFFER_OFFSET;
  222. /* Determine if the expected buffer address is empty */
  223. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  224. if (((reg & XEL_TSR_XMIT_BUSY_MASK) == 0)
  225. && ((in_be32 ((baseaddress) + XEL_TSR_OFFSET)
  226. & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
  227. debug ("Send packet from 0x%x\n", baseaddress);
  228. /* Write the frame to the buffer */
  229. xemaclite_alignedwrite ((void *) ptr, baseaddress, len);
  230. out_be32 (baseaddress + XEL_TPLR_OFFSET,(len &
  231. (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO)));
  232. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  233. reg |= XEL_TSR_XMIT_BUSY_MASK;
  234. if ((reg & XEL_TSR_XMIT_IE_MASK) != 0) {
  235. reg |= XEL_TSR_XMIT_ACTIVE_MASK;
  236. }
  237. out_be32 (baseaddress + XEL_TSR_OFFSET, reg);
  238. return 0;
  239. }
  240. #endif
  241. puts ("Error while sending frame\n");
  242. return -1;
  243. }
  244. static int emaclite_recv(struct eth_device *dev)
  245. {
  246. u32 length;
  247. u32 reg;
  248. u32 baseaddress;
  249. baseaddress = emaclite.baseaddress + emaclite.nextrxbuffertouse;
  250. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  251. debug ("Testing data at address 0x%x\n", baseaddress);
  252. if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
  253. #ifdef CONFIG_XILINX_EMACLITE_RX_PING_PONG
  254. emaclite.nextrxbuffertouse ^= XEL_BUFFER_OFFSET;
  255. #endif
  256. } else {
  257. #ifndef CONFIG_XILINX_EMACLITE_RX_PING_PONG
  258. debug ("No data was available - address 0x%x\n", baseaddress);
  259. return 0;
  260. #else
  261. baseaddress ^= XEL_BUFFER_OFFSET;
  262. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  263. if ((reg & XEL_RSR_RECV_DONE_MASK) !=
  264. XEL_RSR_RECV_DONE_MASK) {
  265. debug ("No data was available - address 0x%x\n",
  266. baseaddress);
  267. return 0;
  268. }
  269. #endif
  270. }
  271. /* Get the length of the frame that arrived */
  272. switch(((ntohl(in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0xC))) &
  273. 0xFFFF0000 ) >> 16) {
  274. case 0x806:
  275. length = 42 + 20; /* FIXME size of ARP */
  276. debug ("ARP Packet\n");
  277. break;
  278. case 0x800:
  279. length = 14 + 14 +
  280. (((ntohl(in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0x10))) &
  281. 0xFFFF0000) >> 16); /* FIXME size of IP packet */
  282. debug ("IP Packet\n");
  283. break;
  284. default:
  285. debug ("Other Packet\n");
  286. length = ENET_MAX_MTU;
  287. break;
  288. }
  289. xemaclite_alignedread ((u32 *) (baseaddress + XEL_RXBUFF_OFFSET),
  290. etherrxbuff, length);
  291. /* Acknowledge the frame */
  292. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  293. reg &= ~XEL_RSR_RECV_DONE_MASK;
  294. out_be32 (baseaddress + XEL_RSR_OFFSET, reg);
  295. debug ("Packet receive from 0x%x, length %dB\n", baseaddress, length);
  296. NetReceive ((uchar *) etherrxbuff, length);
  297. return length;
  298. }
  299. int xilinx_emaclite_initialize (bd_t *bis, int base_addr)
  300. {
  301. struct eth_device *dev;
  302. dev = malloc(sizeof(*dev));
  303. if (dev == NULL)
  304. return -1;
  305. memset(dev, 0, sizeof(*dev));
  306. sprintf(dev->name, "Xelite.%x", base_addr);
  307. dev->iobase = base_addr;
  308. dev->priv = 0;
  309. dev->init = emaclite_init;
  310. dev->halt = emaclite_halt;
  311. dev->send = emaclite_send;
  312. dev->recv = emaclite_recv;
  313. eth_register(dev);
  314. return 1;
  315. }