2 * (C) Copyright 2007 Michal Simek
4 * Michal SIMEK <monstr@monstr.eu>
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
24 * Based on Xilinx drivers
33 #include "xilinx_emac.h"
39 #define ENET_MAX_MTU PKTSIZE
40 #define ENET_ADDR_LENGTH 6
42 static unsigned int etherrxbuff[PKTSIZE_ALIGN/4]; /* Receive buffer */
44 static u8 EMACAddr[ENET_ADDR_LENGTH] = { 0x00, 0x0a, 0x35, 0x00, 0x22, 0x01 };
53 int eth_init(bd_t * bis)
57 printf("EMAC Initialization Started\n\r");
60 puts("Emac is started\n");
64 memset (&Emac, 0, sizeof (XEmac));
66 Emac.BaseAddress = XILINX_EMAC_BASEADDR;
68 /* Setting up FIFOs */
69 Emac.RecvFifo.RegBaseAddress = Emac.BaseAddress +
70 XEM_PFIFO_RXREG_OFFSET;
71 Emac.RecvFifo.DataBaseAddress = Emac.BaseAddress +
72 XEM_PFIFO_RXDATA_OFFSET;
73 out_be32 (Emac.RecvFifo.RegBaseAddress, XPF_RESET_FIFO_MASK);
75 Emac.SendFifo.RegBaseAddress = Emac.BaseAddress +
76 XEM_PFIFO_TXREG_OFFSET;
77 Emac.SendFifo.DataBaseAddress = Emac.BaseAddress +
78 XEM_PFIFO_TXDATA_OFFSET;
79 out_be32 (Emac.SendFifo.RegBaseAddress, XPF_RESET_FIFO_MASK);
81 /* Reset the entire IPIF */
82 out_be32 (Emac.BaseAddress + XIIF_V123B_RESETR_OFFSET,
83 XIIF_V123B_RESET_MASK);
85 /* Stopping EMAC for setting up MAC */
86 HelpReg = in_be32 (Emac.BaseAddress + XEM_ECR_OFFSET);
87 HelpReg &= ~(XEM_ECR_XMIT_ENABLE_MASK | XEM_ECR_RECV_ENABLE_MASK);
88 out_be32 (Emac.BaseAddress + XEM_ECR_OFFSET, HelpReg);
90 if (!getenv("ethaddr")) {
91 memcpy(bis->bi_enetaddr, EMACAddr, ENET_ADDR_LENGTH);
94 /* Set the device station address high and low registers */
95 HelpReg = (bis->bi_enetaddr[0] << 8) | bis->bi_enetaddr[1];
96 out_be32 (Emac.BaseAddress + XEM_SAH_OFFSET, HelpReg);
97 HelpReg = (bis->bi_enetaddr[2] << 24) | (bis->bi_enetaddr[3] << 16) |
98 (bis->bi_enetaddr[4] << 8) | bis->bi_enetaddr[5];
99 out_be32 (Emac.BaseAddress + XEM_SAL_OFFSET, HelpReg);
102 HelpReg = XEM_ECR_UNICAST_ENABLE_MASK | XEM_ECR_BROAD_ENABLE_MASK |
103 XEM_ECR_FULL_DUPLEX_MASK | XEM_ECR_XMIT_FCS_ENABLE_MASK |
104 XEM_ECR_XMIT_PAD_ENABLE_MASK | XEM_ECR_PHY_ENABLE_MASK;
105 out_be32 (Emac.BaseAddress + XEM_ECR_OFFSET, HelpReg);
109 /* Enable the transmitter, and receiver */
110 HelpReg = in_be32 (Emac.BaseAddress + XEM_ECR_OFFSET);
111 HelpReg &= ~(XEM_ECR_XMIT_RESET_MASK | XEM_ECR_RECV_RESET_MASK);
112 HelpReg |= (XEM_ECR_XMIT_ENABLE_MASK | XEM_ECR_RECV_ENABLE_MASK);
113 out_be32 (Emac.BaseAddress + XEM_ECR_OFFSET, HelpReg);
115 printf("EMAC Initialization complete\n\r");
119 int eth_send(volatile void *ptr, int len)
126 u32 *WordBuffer = (u32 *) ptr;
128 if (len > ENET_MAX_MTU)
132 * Check for overruns and underruns for the transmit status and length
133 * FIFOs and make sure the send packet FIFO is not deadlocked.
134 * Any of these conditions is bad enough that we do not want to
135 * continue. The upper layer software should reset the device to resolve
138 IntrStatus = in_be32 ((Emac.BaseAddress) + XIIF_V123B_IISR_OFFSET);
139 if (IntrStatus & (XEM_EIR_XMIT_SFIFO_OVER_MASK |
140 XEM_EIR_XMIT_LFIFO_OVER_MASK)) {
142 puts ("Transmitting overrun error\n");
145 } else if (IntrStatus & (XEM_EIR_XMIT_SFIFO_UNDER_MASK |
146 XEM_EIR_XMIT_LFIFO_UNDER_MASK)) {
148 puts ("Transmitting underrun error\n");
151 } else if (in_be32 (Emac.SendFifo.RegBaseAddress +
152 XPF_COUNT_STATUS_REG_OFFSET) & XPF_DEADLOCK_MASK) {
154 puts("Transmitting fifo error\n");
160 * Before writing to the data FIFO, make sure the length FIFO is not
161 * full. The data FIFO might not be full yet even though the length FIFO
162 * is. This avoids an overrun condition on the length FIFO and keeps the
165 * Clear the latched LFIFO_FULL bit so next time around the most
166 * current status is represented
168 if (IntrStatus & XEM_EIR_XMIT_LFIFO_FULL_MASK) {
169 out_be32 ((Emac.BaseAddress) + XIIF_V123B_IISR_OFFSET, IntrStatus
170 & XEM_EIR_XMIT_LFIFO_FULL_MASK);
172 puts ("Fifo is full\n");
177 /* get the count of how many words may be inserted into the FIFO */
178 FifoCount = in_be32 (Emac.SendFifo.RegBaseAddress +
179 XPF_COUNT_STATUS_REG_OFFSET) & XPF_COUNT_MASK;
180 WordCount = len >> 2;
181 ExtraByteCount = len & 0x3;
183 if (FifoCount < WordCount) {
185 puts ("Sending packet is larger then size of FIFO\n");
190 for (FifoCount = 0; FifoCount < WordCount; FifoCount++) {
191 out_be32 (Emac.SendFifo.DataBaseAddress, WordBuffer[FifoCount]);
193 if (ExtraByteCount > 0) {
195 u8 *ExtraBytesBuffer = (u8 *) (WordBuffer + WordCount);
197 if (ExtraByteCount == 1) {
198 LastWord = ExtraBytesBuffer[0] << 24;
199 } else if (ExtraByteCount == 2) {
200 LastWord = ExtraBytesBuffer[0] << 24 |
201 ExtraBytesBuffer[1] << 16;
202 } else if (ExtraByteCount == 3) {
203 LastWord = ExtraBytesBuffer[0] << 24 |
204 ExtraBytesBuffer[1] << 16 |
205 ExtraBytesBuffer[2] << 8;
207 out_be32 (Emac.SendFifo.DataBaseAddress, LastWord);
210 /* Loop on the MAC's status to wait for any pause to complete */
211 IntrStatus = in_be32 ((Emac.BaseAddress) + XIIF_V123B_IISR_OFFSET);
212 while ((IntrStatus & XEM_EIR_XMIT_PAUSE_MASK) != 0) {
213 IntrStatus = in_be32 ((Emac.BaseAddress) +
214 XIIF_V123B_IISR_OFFSET);
215 /* Clear the pause status from the transmit status register */
216 out_be32 ((Emac.BaseAddress) + XIIF_V123B_IISR_OFFSET,
217 IntrStatus & XEM_EIR_XMIT_PAUSE_MASK);
221 * Set the MAC's transmit packet length register to tell it to transmit
223 out_be32 (Emac.BaseAddress + XEM_TPLR_OFFSET, len);
226 * Loop on the MAC's status to wait for the transmit to complete.
227 * The transmit status is in the FIFO when the XMIT_DONE bit is set.
230 IntrStatus = in_be32 ((Emac.BaseAddress) +
231 XIIF_V123B_IISR_OFFSET);
233 while ((IntrStatus & XEM_EIR_XMIT_DONE_MASK) == 0);
235 XmitStatus = in_be32 (Emac.BaseAddress + XEM_TSR_OFFSET);
237 if (IntrStatus & (XEM_EIR_XMIT_SFIFO_OVER_MASK |
238 XEM_EIR_XMIT_LFIFO_OVER_MASK)) {
240 puts ("Transmitting overrun error\n");
243 } else if (IntrStatus & (XEM_EIR_XMIT_SFIFO_UNDER_MASK |
244 XEM_EIR_XMIT_LFIFO_UNDER_MASK)) {
246 puts ("Transmitting underrun error\n");
251 /* Clear the interrupt status register of transmit statuses */
252 out_be32 ((Emac.BaseAddress) + XIIF_V123B_IISR_OFFSET,
253 IntrStatus & XEM_EIR_XMIT_ALL_MASK);
256 * Collision errors are stored in the transmit status register
257 * instead of the interrupt status register
259 if ((XmitStatus & XEM_TSR_EXCESS_DEFERRAL_MASK) ||
260 (XmitStatus & XEM_TSR_LATE_COLLISION_MASK)) {
262 puts ("Transmitting collision error\n");
277 u8 *ExtraBytesBuffer;
279 if (in_be32 (Emac.RecvFifo.RegBaseAddress + XPF_COUNT_STATUS_REG_OFFSET)
280 & XPF_DEADLOCK_MASK) {
281 out_be32 (Emac.RecvFifo.RegBaseAddress, XPF_RESET_FIFO_MASK);
283 puts ("Receiving FIFO deadlock\n");
289 * Get the interrupt status to know what happened (whether an error occurred
290 * and/or whether frames have been received successfully). When clearing the
291 * intr status register, clear only statuses that pertain to receive.
293 IntrStatus = in_be32 ((Emac.BaseAddress) + XIIF_V123B_IISR_OFFSET);
295 * Before reading from the length FIFO, make sure the length FIFO is not
296 * empty. We could cause an underrun error if we try to read from an
299 if (!(IntrStatus & XEM_EIR_RECV_DONE_MASK)) {
301 /* puts("Receiving FIFO is empty\n"); */
307 * Determine, from the MAC, the length of the next packet available
308 * in the data FIFO (there should be a non-zero length here)
310 PktLength = in_be32 (Emac.BaseAddress + XEM_RPLR_OFFSET);
316 * Write the RECV_DONE bit in the status register to clear it. This bit
317 * indicates the RPLR is non-empty, and we know it's set at this point.
318 * We clear it so that subsequent entry into this routine will reflect
319 * the current status. This is done because the non-empty bit is latched
320 * in the IPIF, which means it may indicate a non-empty condition even
321 * though there is something in the FIFO.
323 out_be32 ((Emac.BaseAddress) + XIIF_V123B_IISR_OFFSET,
324 XEM_EIR_RECV_DONE_MASK);
326 FifoCount = in_be32 (Emac.RecvFifo.RegBaseAddress +
327 XPF_COUNT_STATUS_REG_OFFSET) & XPF_COUNT_MASK;
329 if ((FifoCount * 4) < PktLength) {
331 puts ("Receiving FIFO is smaller than packet size.\n");
336 WordCount = PktLength >> 2;
337 ExtraByteCount = PktLength & 0x3;
339 for (FifoCount = 0; FifoCount < WordCount; FifoCount++) {
340 etherrxbuff[FifoCount] =
341 in_be32 (Emac.RecvFifo.DataBaseAddress);
345 * if there are extra bytes to handle, read the last word from the FIFO
346 * and insert the extra bytes into the buffer
348 if (ExtraByteCount > 0) {
349 ExtraBytesBuffer = (u8 *) (etherrxbuff + WordCount);
351 LastWord = in_be32 (Emac.RecvFifo.DataBaseAddress);
354 * one extra byte in the last word, put the byte into the next
355 * location of the buffer, bytes in a word of the FIFO are
356 * ordered from most significant byte to least
358 if (ExtraByteCount == 1) {
359 ExtraBytesBuffer[0] = (u8) (LastWord >> 24);
360 } else if (ExtraByteCount == 2) {
361 ExtraBytesBuffer[0] = (u8) (LastWord >> 24);
362 ExtraBytesBuffer[1] = (u8) (LastWord >> 16);
363 } else if (ExtraByteCount == 3) {
364 ExtraBytesBuffer[0] = (u8) (LastWord >> 24);
365 ExtraBytesBuffer[1] = (u8) (LastWord >> 16);
366 ExtraBytesBuffer[2] = (u8) (LastWord >> 8);
369 NetReceive((uchar *)etherrxbuff, PktLength);