xilinx_emaclite.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. struct xemaclite {
  59. u32 nexttxbuffertouse; /* Next TX buffer to write to */
  60. u32 nextrxbuffertouse; /* Next RX buffer to read from */
  61. u32 txpp; /* TX ping pong buffer */
  62. u32 rxpp; /* RX ping pong buffer */
  63. };
  64. static u32 etherrxbuff[PKTSIZE_ALIGN/4]; /* Receive buffer */
  65. static void xemaclite_alignedread (u32 *srcptr, void *destptr, u32 bytecount)
  66. {
  67. u32 i;
  68. u32 alignbuffer;
  69. u32 *to32ptr;
  70. u32 *from32ptr;
  71. u8 *to8ptr;
  72. u8 *from8ptr;
  73. from32ptr = (u32 *) srcptr;
  74. /* Word aligned buffer, no correction needed. */
  75. to32ptr = (u32 *) destptr;
  76. while (bytecount > 3) {
  77. *to32ptr++ = *from32ptr++;
  78. bytecount -= 4;
  79. }
  80. to8ptr = (u8 *) to32ptr;
  81. alignbuffer = *from32ptr++;
  82. from8ptr = (u8 *) & alignbuffer;
  83. for (i = 0; i < bytecount; i++) {
  84. *to8ptr++ = *from8ptr++;
  85. }
  86. }
  87. static void xemaclite_alignedwrite (void *srcptr, u32 destptr, u32 bytecount)
  88. {
  89. u32 i;
  90. u32 alignbuffer;
  91. u32 *to32ptr = (u32 *) destptr;
  92. u32 *from32ptr;
  93. u8 *to8ptr;
  94. u8 *from8ptr;
  95. from32ptr = (u32 *) srcptr;
  96. while (bytecount > 3) {
  97. *to32ptr++ = *from32ptr++;
  98. bytecount -= 4;
  99. }
  100. alignbuffer = 0;
  101. to8ptr = (u8 *) & alignbuffer;
  102. from8ptr = (u8 *) from32ptr;
  103. for (i = 0; i < bytecount; i++) {
  104. *to8ptr++ = *from8ptr++;
  105. }
  106. *to32ptr++ = alignbuffer;
  107. }
  108. static void emaclite_halt(struct eth_device *dev)
  109. {
  110. debug ("eth_halt\n");
  111. }
  112. static int emaclite_init(struct eth_device *dev, bd_t *bis)
  113. {
  114. struct xemaclite *emaclite = dev->priv;
  115. debug ("EmacLite Initialization Started\n");
  116. /*
  117. * TX - TX_PING & TX_PONG initialization
  118. */
  119. /* Restart PING TX */
  120. out_be32 (dev->iobase + XEL_TSR_OFFSET, 0);
  121. /* Copy MAC address */
  122. xemaclite_alignedwrite (dev->enetaddr,
  123. dev->iobase, ENET_ADDR_LENGTH);
  124. /* Set the length */
  125. out_be32 (dev->iobase + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH);
  126. /* Update the MAC address in the EMAC Lite */
  127. out_be32 (dev->iobase + XEL_TSR_OFFSET, XEL_TSR_PROG_MAC_ADDR);
  128. /* Wait for EMAC Lite to finish with the MAC address update */
  129. while ((in_be32 (dev->iobase + XEL_TSR_OFFSET) &
  130. XEL_TSR_PROG_MAC_ADDR) != 0)
  131. ;
  132. if (emaclite->txpp) {
  133. /* The same operation with PONG TX */
  134. out_be32 (dev->iobase + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET, 0);
  135. xemaclite_alignedwrite(dev->enetaddr, dev->iobase +
  136. XEL_BUFFER_OFFSET, ENET_ADDR_LENGTH);
  137. out_be32 (dev->iobase + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH);
  138. out_be32 (dev->iobase + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET,
  139. XEL_TSR_PROG_MAC_ADDR);
  140. while ((in_be32 (dev->iobase + XEL_TSR_OFFSET +
  141. XEL_BUFFER_OFFSET) & XEL_TSR_PROG_MAC_ADDR) != 0)
  142. ;
  143. }
  144. /*
  145. * RX - RX_PING & RX_PONG initialization
  146. */
  147. /* Write out the value to flush the RX buffer */
  148. out_be32 (dev->iobase + XEL_RSR_OFFSET, XEL_RSR_RECV_IE_MASK);
  149. if (emaclite->rxpp)
  150. out_be32 (dev->iobase + XEL_RSR_OFFSET + XEL_BUFFER_OFFSET,
  151. XEL_RSR_RECV_IE_MASK);
  152. debug ("EmacLite Initialization complete\n");
  153. return 0;
  154. }
  155. static int xemaclite_txbufferavailable(struct eth_device *dev)
  156. {
  157. u32 reg;
  158. u32 txpingbusy;
  159. u32 txpongbusy;
  160. struct xemaclite *emaclite = dev->priv;
  161. /*
  162. * Read the other buffer register
  163. * and determine if the other buffer is available
  164. */
  165. reg = in_be32 (dev->iobase +
  166. emaclite->nexttxbuffertouse + 0);
  167. txpingbusy = ((reg & XEL_TSR_XMIT_BUSY_MASK) ==
  168. XEL_TSR_XMIT_BUSY_MASK);
  169. reg = in_be32 (dev->iobase +
  170. (emaclite->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. struct xemaclite *emaclite = dev->priv;
  180. u32 maxtry = 1000;
  181. if (len > ENET_MAX_MTU)
  182. len = ENET_MAX_MTU;
  183. while (!xemaclite_txbufferavailable(dev) && maxtry) {
  184. udelay (10);
  185. maxtry--;
  186. }
  187. if (!maxtry) {
  188. printf ("Error: Timeout waiting for ethernet TX buffer\n");
  189. /* Restart PING TX */
  190. out_be32 (dev->iobase + XEL_TSR_OFFSET, 0);
  191. if (emaclite->txpp) {
  192. out_be32 (dev->iobase + XEL_TSR_OFFSET +
  193. XEL_BUFFER_OFFSET, 0);
  194. }
  195. return -1;
  196. }
  197. /* Determine the expected TX buffer address */
  198. baseaddress = (dev->iobase + emaclite->nexttxbuffertouse);
  199. /* Determine if the expected buffer address is empty */
  200. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  201. if (((reg & XEL_TSR_XMIT_BUSY_MASK) == 0)
  202. && ((in_be32 ((baseaddress) + XEL_TSR_OFFSET)
  203. & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
  204. if (emaclite->txpp)
  205. emaclite->nexttxbuffertouse ^= XEL_BUFFER_OFFSET;
  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. if (emaclite->txpp) {
  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 |
  232. XEL_TPLR_LENGTH_MASK_LO)));
  233. reg = in_be32 (baseaddress + XEL_TSR_OFFSET);
  234. reg |= XEL_TSR_XMIT_BUSY_MASK;
  235. if ((reg & XEL_TSR_XMIT_IE_MASK) != 0)
  236. reg |= XEL_TSR_XMIT_ACTIVE_MASK;
  237. out_be32 (baseaddress + XEL_TSR_OFFSET, reg);
  238. return 0;
  239. }
  240. }
  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. struct xemaclite *emaclite = dev->priv;
  250. baseaddress = dev->iobase + emaclite->nextrxbuffertouse;
  251. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  252. debug ("Testing data at address 0x%x\n", baseaddress);
  253. if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
  254. if (emaclite->rxpp)
  255. emaclite->nextrxbuffertouse ^= XEL_BUFFER_OFFSET;
  256. } else {
  257. if (!emaclite->rxpp) {
  258. debug ("No data was available - address 0x%x\n",
  259. baseaddress);
  260. return 0;
  261. } else {
  262. baseaddress ^= XEL_BUFFER_OFFSET;
  263. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  264. if ((reg & XEL_RSR_RECV_DONE_MASK) !=
  265. XEL_RSR_RECV_DONE_MASK) {
  266. debug("No data was available - address 0x%x\n",
  267. baseaddress);
  268. return 0;
  269. }
  270. }
  271. }
  272. /* Get the length of the frame that arrived */
  273. switch(((ntohl(in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0xC))) &
  274. 0xFFFF0000 ) >> 16) {
  275. case 0x806:
  276. length = 42 + 20; /* FIXME size of ARP */
  277. debug ("ARP Packet\n");
  278. break;
  279. case 0x800:
  280. length = 14 + 14 +
  281. (((ntohl(in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0x10))) &
  282. 0xFFFF0000) >> 16); /* FIXME size of IP packet */
  283. debug ("IP Packet\n");
  284. break;
  285. default:
  286. debug ("Other Packet\n");
  287. length = ENET_MAX_MTU;
  288. break;
  289. }
  290. xemaclite_alignedread ((u32 *) (baseaddress + XEL_RXBUFF_OFFSET),
  291. etherrxbuff, length);
  292. /* Acknowledge the frame */
  293. reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
  294. reg &= ~XEL_RSR_RECV_DONE_MASK;
  295. out_be32 (baseaddress + XEL_RSR_OFFSET, reg);
  296. debug ("Packet receive from 0x%x, length %dB\n", baseaddress, length);
  297. NetReceive ((uchar *) etherrxbuff, length);
  298. return length;
  299. }
  300. int xilinx_emaclite_initialize (bd_t *bis, int base_addr)
  301. {
  302. struct eth_device *dev;
  303. struct xemaclite *emaclite;
  304. dev = calloc(1, sizeof(*dev));
  305. if (dev == NULL)
  306. return -1;
  307. emaclite = calloc(1, sizeof(struct xemaclite));
  308. if (emaclite == NULL) {
  309. free(dev);
  310. return -1;
  311. }
  312. dev->priv = emaclite;
  313. #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG
  314. emaclite->txpp = 1;
  315. #endif
  316. #ifdef CONFIG_XILINX_EMACLITE_RX_PING_PONG
  317. emaclite->rxpp = 1;
  318. #endif
  319. sprintf(dev->name, "Xelite.%x", base_addr);
  320. dev->iobase = base_addr;
  321. dev->init = emaclite_init;
  322. dev->halt = emaclite_halt;
  323. dev->send = emaclite_send;
  324. dev->recv = emaclite_recv;
  325. eth_register(dev);
  326. return 1;
  327. }