xilinx_emaclite.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. //#define 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]; /* 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 i;
  72. unsigned Length = ByteCount;
  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 (Length > 3) {
  82. *To32Ptr++ = *From32Ptr++;
  83. Length -= 4;
  84. }
  85. To8Ptr = (u8 *) To32Ptr;
  86. AlignBuffer = *From32Ptr++;
  87. From8Ptr = (u8 *) & AlignBuffer;
  88. for (i = 0; i < Length; i++) {
  89. *To8Ptr++ = *From8Ptr++;
  90. }
  91. }
  92. void XEmacLite_AlignedWrite (void *SrcPtr, u32 * DestPtr, unsigned ByteCount)
  93. {
  94. unsigned i;
  95. unsigned Length = ByteCount;
  96. u32 AlignBuffer;
  97. u32 *To32Ptr;
  98. u32 *From32Ptr;
  99. u8 *To8Ptr;
  100. u8 *From8Ptr;
  101. To32Ptr = DestPtr;
  102. From32Ptr = (u32 *) SrcPtr;
  103. while (Length > 3) {
  104. *To32Ptr++ = *From32Ptr++;
  105. Length -= 4;
  106. }
  107. AlignBuffer = 0;
  108. To8Ptr = (u8 *) & AlignBuffer;
  109. From8Ptr = (u8 *) From32Ptr;
  110. for (i = 0; i < Length; i++) {
  111. *To8Ptr++ = *From8Ptr++;
  112. }
  113. *To32Ptr++ = AlignBuffer;
  114. }
  115. void eth_halt (void)
  116. {
  117. #ifdef DEBUG
  118. puts ("eth_halt\n");
  119. #endif
  120. }
  121. int eth_init (bd_t * bis)
  122. {
  123. #ifdef DEBUG
  124. puts ("EmacLite Initialization Started\n");
  125. #endif
  126. memset (&EmacLite, 0, sizeof (XEmacLite));
  127. EmacLite.BaseAddress = XILINX_EMACLITE_BASEADDR;
  128. #ifdef CFG_ENV_IS_NOWHERE
  129. memcpy (bis->bi_enetaddr, EMACAddr, ENET_ADDR_LENGTH);
  130. #endif
  131. /*
  132. * TX - TX_PING & TX_PONG initialization
  133. */
  134. /* Restart PING TX */
  135. out_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET, 0);
  136. /* Copy MAC address */
  137. XEmacLite_AlignedWrite (bis->bi_enetaddr,
  138. EmacLite.BaseAddress, ENET_ADDR_LENGTH);
  139. /* Set the length */
  140. out_be32 (EmacLite.BaseAddress + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH);
  141. /* Update the MAC address in the EMAC Lite */
  142. out_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET, XEL_TSR_PROG_MAC_ADDR);
  143. /* Wait for EMAC Lite to finish with the MAC address update */
  144. while ((in_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET) &
  145. XEL_TSR_PROG_MAC_ADDR) != 0) ;
  146. #ifdef XILINX_EMACLITE_TX_PING_PONG
  147. /* The same operation with PONG TX */
  148. out_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET, 0);
  149. XEmacLite_AlignedWrite (bis->bi_enetaddr,
  150. EmacLite.BaseAddress + XEL_BUFFER_OFFSET, ENET_ADDR_LENGTH);
  151. out_be32 (EmacLite.BaseAddress + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH);
  152. out_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET,
  153. XEL_TSR_PROG_MAC_ADDR);
  154. while ((in_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET +
  155. XEL_BUFFER_OFFSET) & XEL_TSR_PROG_MAC_ADDR) != 0) ;
  156. #endif
  157. /*
  158. * RX - RX_PING & RX_PONG initialization
  159. */
  160. /* Write out the value to flush the RX buffer */
  161. out_be32 (EmacLite.BaseAddress + XEL_RSR_OFFSET, XEL_RSR_RECV_IE_MASK);
  162. #ifdef XILINX_EMACLITE_RX_PING_PONG
  163. out_be32 (EmacLite.BaseAddress + XEL_RSR_OFFSET + XEL_BUFFER_OFFSET,
  164. XEL_RSR_RECV_IE_MASK);
  165. #endif
  166. #ifdef DEBUG
  167. puts ("EmacLite Initialization complete\n");
  168. #endif
  169. return 0;
  170. }
  171. int XEmacLite_TxBufferAvailable (XEmacLite * InstancePtr)
  172. {
  173. u32 Register;
  174. u32 TxPingBusy;
  175. u32 TxPongBusy;
  176. /*
  177. * Read the other buffer register
  178. * and determine if the other buffer is available
  179. */
  180. Register = in_be32 (InstancePtr->BaseAddress +
  181. InstancePtr->NextTxBufferToUse + 0);
  182. TxPingBusy = ((Register & XEL_TSR_XMIT_BUSY_MASK) ==
  183. XEL_TSR_XMIT_BUSY_MASK);
  184. Register = in_be32 (InstancePtr->BaseAddress +
  185. (InstancePtr->NextTxBufferToUse ^ XEL_TSR_OFFSET) + 0);
  186. TxPongBusy = ((Register & XEL_TSR_XMIT_BUSY_MASK) ==
  187. XEL_TSR_XMIT_BUSY_MASK);
  188. return (!(TxPingBusy && TxPongBusy));
  189. }
  190. int eth_send (volatile void *ptr, int len) {
  191. unsigned int Register;
  192. unsigned int BaseAddress;
  193. unsigned maxtry = 1000;
  194. if (len > ENET_MAX_MTU)
  195. len = ENET_MAX_MTU;
  196. while (!XEmacLite_TxBufferAvailable (&EmacLite) && maxtry) {
  197. udelay (10);
  198. maxtry--;
  199. }
  200. if (!maxtry) {
  201. printf ("Error: Timeout waiting for ethernet TX buffer\n");
  202. /* Restart PING TX */
  203. out_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET, 0);
  204. #ifdef XILINX_EMACLITE_TX_PING_PONG
  205. out_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET +
  206. XEL_BUFFER_OFFSET, 0);
  207. #endif
  208. return 0;
  209. }
  210. /* Determine the expected TX buffer address */
  211. BaseAddress = (EmacLite.BaseAddress + EmacLite.NextTxBufferToUse);
  212. /* Determine if the expected buffer address is empty */
  213. Register = in_be32 (BaseAddress + XEL_TSR_OFFSET);
  214. if (((Register & XEL_TSR_XMIT_BUSY_MASK) == 0)
  215. && ((in_be32 ((BaseAddress) + XEL_TSR_OFFSET)
  216. & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
  217. #ifdef XILINX_EMACLITE_TX_PING_PONG
  218. EmacLite.NextTxBufferToUse ^= XEL_BUFFER_OFFSET;
  219. #endif
  220. #ifdef DEBUG
  221. printf ("Send packet from 0x%x\n", BaseAddress);
  222. #endif
  223. /* Write the frame to the buffer */
  224. XEmacLite_AlignedWrite (ptr, (u32 *) BaseAddress, len);
  225. out_be32 (BaseAddress + XEL_TPLR_OFFSET,(len &
  226. (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO)));
  227. Register = in_be32 (BaseAddress + XEL_TSR_OFFSET);
  228. Register |= XEL_TSR_XMIT_BUSY_MASK;
  229. if ((Register & XEL_TSR_XMIT_IE_MASK) != 0) {
  230. Register |= XEL_TSR_XMIT_ACTIVE_MASK;
  231. }
  232. out_be32 (BaseAddress + XEL_TSR_OFFSET, Register);
  233. return 1;
  234. }
  235. #ifdef XILINX_EMACLITE_TX_PING_PONG
  236. /* Switch to second buffer */
  237. BaseAddress ^= XEL_BUFFER_OFFSET;
  238. /* Determine if the expected buffer address is empty */
  239. Register = in_be32 (BaseAddress + XEL_TSR_OFFSET);
  240. if (((Register & XEL_TSR_XMIT_BUSY_MASK) == 0)
  241. && ((in_be32 ((BaseAddress) + XEL_TSR_OFFSET)
  242. & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
  243. #ifdef DEBUG
  244. printf ("Send packet from 0x%x\n", BaseAddress);
  245. #endif
  246. /* Write the frame to the buffer */
  247. XEmacLite_AlignedWrite (ptr, (u32 *) BaseAddress, len);
  248. out_be32 (BaseAddress + XEL_TPLR_OFFSET,(len &
  249. (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO)));
  250. Register = in_be32 (BaseAddress + XEL_TSR_OFFSET);
  251. Register |= XEL_TSR_XMIT_BUSY_MASK;
  252. if ((Register & XEL_TSR_XMIT_IE_MASK) != 0) {
  253. Register |= XEL_TSR_XMIT_ACTIVE_MASK;
  254. }
  255. out_be32 (BaseAddress + XEL_TSR_OFFSET, Register);
  256. return 1;
  257. }
  258. #endif
  259. puts ("Error while sending frame\n");
  260. return 0;
  261. }
  262. int eth_rx (void)
  263. {
  264. unsigned int Length;
  265. unsigned int Register;
  266. unsigned int BaseAddress;
  267. BaseAddress = EmacLite.BaseAddress + EmacLite.NextRxBufferToUse;
  268. Register = in_be32 (BaseAddress + XEL_RSR_OFFSET);
  269. #ifdef DEBUG
  270. // printf ("Testing data at address 0x%x\n", BaseAddress);
  271. #endif
  272. if ((Register & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
  273. #ifdef XILINX_EMACLITE_RX_PING_PONG
  274. EmacLite.NextRxBufferToUse ^= XEL_BUFFER_OFFSET;
  275. #endif
  276. } else {
  277. #ifndef XILINX_EMACLITE_RX_PING_PONG
  278. #ifdef DEBUG
  279. // printf ("No data was available - address 0x%x\n", BaseAddress);
  280. #endif
  281. return 0;
  282. #else
  283. BaseAddress ^= XEL_BUFFER_OFFSET;
  284. Register = in_be32 (BaseAddress + XEL_RSR_OFFSET);
  285. if ((Register & XEL_RSR_RECV_DONE_MASK) !=
  286. XEL_RSR_RECV_DONE_MASK) {
  287. #ifdef DEBUG
  288. // printf ("No data was available - address 0x%x\n",
  289. // BaseAddress);
  290. #endif
  291. return 0;
  292. }
  293. #endif
  294. }
  295. /* Get the length of the frame that arrived */
  296. switch(((in_be32(BaseAddress + XEL_RXBUFF_OFFSET + 0xC)) &
  297. 0xFFFF0000 ) >> 16) {
  298. case 0x806:
  299. Length = 42 + 20; /* FIXME size of ARP */
  300. #ifdef DEBUG
  301. puts ("ARP Packet\n");
  302. #endif
  303. break;
  304. case 0x800:
  305. Length = 14 + 14 +
  306. (((in_be32(BaseAddress + XEL_RXBUFF_OFFSET + 0x10)) &
  307. 0xFFFF0000) >> 16); /* FIXME size of IP packet */
  308. #ifdef DEBUG
  309. puts("IP Packet\n");
  310. #endif
  311. break;
  312. default:
  313. #ifdef DEBUG
  314. puts("Other Packet\n");
  315. #endif
  316. Length = ENET_MAX_MTU;
  317. break;
  318. }
  319. XEmacLite_AlignedRead ((BaseAddress + XEL_RXBUFF_OFFSET),
  320. etherrxbuff, Length);
  321. /* Acknowledge the frame */
  322. Register = in_be32 (BaseAddress + XEL_RSR_OFFSET);
  323. Register &= ~XEL_RSR_RECV_DONE_MASK;
  324. out_be32 (BaseAddress + XEL_RSR_OFFSET, Register);
  325. #ifdef DEBUG
  326. printf ("Packet receive from 0x%x, length %dB\n", BaseAddress, Length);
  327. #endif
  328. NetReceive ((uchar *) etherrxbuff, Length);
  329. return 1;
  330. }
  331. #endif