xilinx_emac.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. * Based on Xilinx drivers
  25. *
  26. */
  27. #include <config.h>
  28. #include <common.h>
  29. #include <net.h>
  30. #include <asm/io.h>
  31. #include "xilinx_emac.h"
  32. #ifdef XILINX_EMAC
  33. #undef DEBUG
  34. #define ENET_MAX_MTU PKTSIZE
  35. #define ENET_ADDR_LENGTH 6
  36. static unsigned int etherrxbuff[PKTSIZE_ALIGN/4]; /* Receive buffer */
  37. static u8 emacaddr[ENET_ADDR_LENGTH] = { 0x00, 0x0a, 0x35, 0x00, 0x22, 0x01 };
  38. static xemac emac;
  39. void eth_halt(void)
  40. {
  41. #ifdef DEBUG
  42. puts ("eth_halt\n");
  43. #endif
  44. }
  45. int eth_init(bd_t * bis)
  46. {
  47. u32 helpreg;
  48. #ifdef DEBUG
  49. printf("EMAC Initialization Started\n\r");
  50. #endif
  51. if (emac.isstarted) {
  52. puts("Emac is started\n");
  53. return 0;
  54. }
  55. memset (&emac, 0, sizeof (xemac));
  56. emac.baseaddress = XILINX_EMAC_BASEADDR;
  57. /* Setting up FIFOs */
  58. emac.recvfifo.regbaseaddress = emac.baseaddress +
  59. XEM_PFIFO_RXREG_OFFSET;
  60. emac.recvfifo.databaseaddress = emac.baseaddress +
  61. XEM_PFIFO_RXDATA_OFFSET;
  62. out_be32 (emac.recvfifo.regbaseaddress, XPF_RESET_FIFO_MASK);
  63. emac.sendfifo.regbaseaddress = emac.baseaddress +
  64. XEM_PFIFO_TXREG_OFFSET;
  65. emac.sendfifo.databaseaddress = emac.baseaddress +
  66. XEM_PFIFO_TXDATA_OFFSET;
  67. out_be32 (emac.sendfifo.regbaseaddress, XPF_RESET_FIFO_MASK);
  68. /* Reset the entire IPIF */
  69. out_be32 (emac.baseaddress + XIIF_V123B_RESETR_OFFSET,
  70. XIIF_V123B_RESET_MASK);
  71. /* Stopping EMAC for setting up MAC */
  72. helpreg = in_be32 (emac.baseaddress + XEM_ECR_OFFSET);
  73. helpreg &= ~(XEM_ECR_XMIT_ENABLE_MASK | XEM_ECR_RECV_ENABLE_MASK);
  74. out_be32 (emac.baseaddress + XEM_ECR_OFFSET, helpreg);
  75. if (!getenv("ethaddr")) {
  76. memcpy(bis->bi_enetaddr, emacaddr, ENET_ADDR_LENGTH);
  77. }
  78. /* Set the device station address high and low registers */
  79. helpreg = (bis->bi_enetaddr[0] << 8) | bis->bi_enetaddr[1];
  80. out_be32 (emac.baseaddress + XEM_SAH_OFFSET, helpreg);
  81. helpreg = (bis->bi_enetaddr[2] << 24) | (bis->bi_enetaddr[3] << 16) |
  82. (bis->bi_enetaddr[4] << 8) | bis->bi_enetaddr[5];
  83. out_be32 (emac.baseaddress + XEM_SAL_OFFSET, helpreg);
  84. helpreg = XEM_ECR_UNICAST_ENABLE_MASK | XEM_ECR_BROAD_ENABLE_MASK |
  85. XEM_ECR_FULL_DUPLEX_MASK | XEM_ECR_XMIT_FCS_ENABLE_MASK |
  86. XEM_ECR_XMIT_PAD_ENABLE_MASK | XEM_ECR_PHY_ENABLE_MASK;
  87. out_be32 (emac.baseaddress + XEM_ECR_OFFSET, helpreg);
  88. emac.isstarted = 1;
  89. /* Enable the transmitter, and receiver */
  90. helpreg = in_be32 (emac.baseaddress + XEM_ECR_OFFSET);
  91. helpreg &= ~(XEM_ECR_XMIT_RESET_MASK | XEM_ECR_RECV_RESET_MASK);
  92. helpreg |= (XEM_ECR_XMIT_ENABLE_MASK | XEM_ECR_RECV_ENABLE_MASK);
  93. out_be32 (emac.baseaddress + XEM_ECR_OFFSET, helpreg);
  94. printf("EMAC Initialization complete\n\r");
  95. return 0;
  96. }
  97. int eth_send(volatile void *ptr, int len)
  98. {
  99. u32 intrstatus;
  100. u32 xmitstatus;
  101. u32 fifocount;
  102. u32 wordcount;
  103. u32 extrabytecount;
  104. u32 *wordbuffer = (u32 *) ptr;
  105. if (len > ENET_MAX_MTU)
  106. len = ENET_MAX_MTU;
  107. /*
  108. * Check for overruns and underruns for the transmit status and length
  109. * FIFOs and make sure the send packet FIFO is not deadlocked.
  110. * Any of these conditions is bad enough that we do not want to
  111. * continue. The upper layer software should reset the device to resolve
  112. * the error.
  113. */
  114. intrstatus = in_be32 ((emac.baseaddress) + XIIF_V123B_IISR_OFFSET);
  115. if (intrstatus & (XEM_EIR_XMIT_SFIFO_OVER_MASK |
  116. XEM_EIR_XMIT_LFIFO_OVER_MASK)) {
  117. #ifdef DEBUG
  118. puts ("Transmitting overrun error\n");
  119. #endif
  120. return 0;
  121. } else if (intrstatus & (XEM_EIR_XMIT_SFIFO_UNDER_MASK |
  122. XEM_EIR_XMIT_LFIFO_UNDER_MASK)) {
  123. #ifdef DEBUG
  124. puts ("Transmitting underrun error\n");
  125. #endif
  126. return 0;
  127. } else if (in_be32 (emac.sendfifo.regbaseaddress +
  128. XPF_COUNT_STATUS_REG_OFFSET) & XPF_DEADLOCK_MASK) {
  129. #ifdef DEBUG
  130. puts("Transmitting fifo error\n");
  131. #endif
  132. return 0;
  133. }
  134. /*
  135. * Before writing to the data FIFO, make sure the length FIFO is not
  136. * full. The data FIFO might not be full yet even though the length FIFO
  137. * is. This avoids an overrun condition on the length FIFO and keeps the
  138. * FIFOs in sync.
  139. *
  140. * Clear the latched LFIFO_FULL bit so next time around the most
  141. * current status is represented
  142. */
  143. if (intrstatus & XEM_EIR_XMIT_LFIFO_FULL_MASK) {
  144. out_be32 ((emac.baseaddress) + XIIF_V123B_IISR_OFFSET,
  145. intrstatus & XEM_EIR_XMIT_LFIFO_FULL_MASK);
  146. #ifdef DEBUG
  147. puts ("Fifo is full\n");
  148. #endif
  149. return 0;
  150. }
  151. /* get the count of how many words may be inserted into the FIFO */
  152. fifocount = in_be32 (emac.sendfifo.regbaseaddress +
  153. XPF_COUNT_STATUS_REG_OFFSET) & XPF_COUNT_MASK;
  154. wordcount = len >> 2;
  155. extrabytecount = len & 0x3;
  156. if (fifocount < wordcount) {
  157. #ifdef DEBUG
  158. puts ("Sending packet is larger then size of FIFO\n");
  159. #endif
  160. return 0;
  161. }
  162. for (fifocount = 0; fifocount < wordcount; fifocount++) {
  163. out_be32 (emac.sendfifo.databaseaddress, wordbuffer[fifocount]);
  164. }
  165. if (extrabytecount > 0) {
  166. u32 lastword = 0;
  167. u8 *extrabytesbuffer = (u8 *) (wordbuffer + wordcount);
  168. if (extrabytecount == 1) {
  169. lastword = extrabytesbuffer[0] << 24;
  170. } else if (extrabytecount == 2) {
  171. lastword = extrabytesbuffer[0] << 24 |
  172. extrabytesbuffer[1] << 16;
  173. } else if (extrabytecount == 3) {
  174. lastword = extrabytesbuffer[0] << 24 |
  175. extrabytesbuffer[1] << 16 |
  176. extrabytesbuffer[2] << 8;
  177. }
  178. out_be32 (emac.sendfifo.databaseaddress, lastword);
  179. }
  180. /* Loop on the MAC's status to wait for any pause to complete */
  181. intrstatus = in_be32 ((emac.baseaddress) + XIIF_V123B_IISR_OFFSET);
  182. while ((intrstatus & XEM_EIR_XMIT_PAUSE_MASK) != 0) {
  183. intrstatus = in_be32 ((emac.baseaddress) +
  184. XIIF_V123B_IISR_OFFSET);
  185. /* Clear the pause status from the transmit status register */
  186. out_be32 ((emac.baseaddress) + XIIF_V123B_IISR_OFFSET,
  187. intrstatus & XEM_EIR_XMIT_PAUSE_MASK);
  188. }
  189. /*
  190. * Set the MAC's transmit packet length register to tell it to transmit
  191. */
  192. out_be32 (emac.baseaddress + XEM_TPLR_OFFSET, len);
  193. /*
  194. * Loop on the MAC's status to wait for the transmit to complete.
  195. * The transmit status is in the FIFO when the XMIT_DONE bit is set.
  196. */
  197. do {
  198. intrstatus = in_be32 ((emac.baseaddress) +
  199. XIIF_V123B_IISR_OFFSET);
  200. }
  201. while ((intrstatus & XEM_EIR_XMIT_DONE_MASK) == 0);
  202. xmitstatus = in_be32 (emac.baseaddress + XEM_TSR_OFFSET);
  203. if (intrstatus & (XEM_EIR_XMIT_SFIFO_OVER_MASK |
  204. XEM_EIR_XMIT_LFIFO_OVER_MASK)) {
  205. #ifdef DEBUG
  206. puts ("Transmitting overrun error\n");
  207. #endif
  208. return 0;
  209. } else if (intrstatus & (XEM_EIR_XMIT_SFIFO_UNDER_MASK |
  210. XEM_EIR_XMIT_LFIFO_UNDER_MASK)) {
  211. #ifdef DEBUG
  212. puts ("Transmitting underrun error\n");
  213. #endif
  214. return 0;
  215. }
  216. /* Clear the interrupt status register of transmit statuses */
  217. out_be32 ((emac.baseaddress) + XIIF_V123B_IISR_OFFSET,
  218. intrstatus & XEM_EIR_XMIT_ALL_MASK);
  219. /*
  220. * Collision errors are stored in the transmit status register
  221. * instead of the interrupt status register
  222. */
  223. if ((xmitstatus & XEM_TSR_EXCESS_DEFERRAL_MASK) ||
  224. (xmitstatus & XEM_TSR_LATE_COLLISION_MASK)) {
  225. #ifdef DEBUG
  226. puts ("Transmitting collision error\n");
  227. #endif
  228. return 0;
  229. }
  230. return 1;
  231. }
  232. int eth_rx(void)
  233. {
  234. u32 pktlength;
  235. u32 intrstatus;
  236. u32 fifocount;
  237. u32 wordcount;
  238. u32 extrabytecount;
  239. u32 lastword;
  240. u8 *extrabytesbuffer;
  241. if (in_be32 (emac.recvfifo.regbaseaddress + XPF_COUNT_STATUS_REG_OFFSET)
  242. & XPF_DEADLOCK_MASK) {
  243. out_be32 (emac.recvfifo.regbaseaddress, XPF_RESET_FIFO_MASK);
  244. #ifdef DEBUG
  245. puts ("Receiving FIFO deadlock\n");
  246. #endif
  247. return 0;
  248. }
  249. /*
  250. * Get the interrupt status to know what happened (whether an error
  251. * occurred and/or whether frames have been received successfully).
  252. * When clearing the intr status register, clear only statuses that
  253. * pertain to receive.
  254. */
  255. intrstatus = in_be32 ((emac.baseaddress) + XIIF_V123B_IISR_OFFSET);
  256. /*
  257. * Before reading from the length FIFO, make sure the length FIFO is not
  258. * empty. We could cause an underrun error if we try to read from an
  259. * empty FIFO.
  260. */
  261. if (!(intrstatus & XEM_EIR_RECV_DONE_MASK)) {
  262. #ifdef DEBUG
  263. /* puts("Receiving FIFO is empty\n"); */
  264. #endif
  265. return 0;
  266. }
  267. /*
  268. * Determine, from the MAC, the length of the next packet available
  269. * in the data FIFO (there should be a non-zero length here)
  270. */
  271. pktlength = in_be32 (emac.baseaddress + XEM_RPLR_OFFSET);
  272. if (!pktlength) {
  273. return 0;
  274. }
  275. /*
  276. * Write the RECV_DONE bit in the status register to clear it. This bit
  277. * indicates the RPLR is non-empty, and we know it's set at this point.
  278. * We clear it so that subsequent entry into this routine will reflect
  279. * the current status. This is done because the non-empty bit is latched
  280. * in the IPIF, which means it may indicate a non-empty condition even
  281. * though there is something in the FIFO.
  282. */
  283. out_be32 ((emac.baseaddress) + XIIF_V123B_IISR_OFFSET,
  284. XEM_EIR_RECV_DONE_MASK);
  285. fifocount = in_be32 (emac.recvfifo.regbaseaddress +
  286. XPF_COUNT_STATUS_REG_OFFSET) & XPF_COUNT_MASK;
  287. if ((fifocount * 4) < pktlength) {
  288. #ifdef DEBUG
  289. puts ("Receiving FIFO is smaller than packet size.\n");
  290. #endif
  291. return 0;
  292. }
  293. wordcount = pktlength >> 2;
  294. extrabytecount = pktlength & 0x3;
  295. for (fifocount = 0; fifocount < wordcount; fifocount++) {
  296. etherrxbuff[fifocount] =
  297. in_be32 (emac.recvfifo.databaseaddress);
  298. }
  299. /*
  300. * if there are extra bytes to handle, read the last word from the FIFO
  301. * and insert the extra bytes into the buffer
  302. */
  303. if (extrabytecount > 0) {
  304. extrabytesbuffer = (u8 *) (etherrxbuff + wordcount);
  305. lastword = in_be32 (emac.recvfifo.databaseaddress);
  306. /*
  307. * one extra byte in the last word, put the byte into the next
  308. * location of the buffer, bytes in a word of the FIFO are
  309. * ordered from most significant byte to least
  310. */
  311. if (extrabytecount == 1) {
  312. extrabytesbuffer[0] = (u8) (lastword >> 24);
  313. } else if (extrabytecount == 2) {
  314. extrabytesbuffer[0] = (u8) (lastword >> 24);
  315. extrabytesbuffer[1] = (u8) (lastword >> 16);
  316. } else if (extrabytecount == 3) {
  317. extrabytesbuffer[0] = (u8) (lastword >> 24);
  318. extrabytesbuffer[1] = (u8) (lastword >> 16);
  319. extrabytesbuffer[2] = (u8) (lastword >> 8);
  320. }
  321. }
  322. NetReceive((uchar *)etherrxbuff, pktlength);
  323. return 1;
  324. }
  325. #endif