[PATCH] Added support for Xilinx Emac community driver
[platform/kernel/u-boot.git] / drivers / net / xilinx_emac.c
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
28 #include <config.h>
29 #include <common.h>
30 #include <net.h>
31 #include <asm/io.h>
32 #include <asm/asm.h>
33 #include "xilinx_emac.h"
34
35 #ifdef XILINX_EMAC
36
37 #undef DEBUG
38
39 #define ENET_MAX_MTU            PKTSIZE
40 #define ENET_ADDR_LENGTH        6
41
42 static unsigned int etherrxbuff[PKTSIZE_ALIGN/4]; /* Receive buffer */
43
44 static u8 EMACAddr[ENET_ADDR_LENGTH] = { 0x00, 0x0a, 0x35, 0x00, 0x22, 0x01 };
45
46 static XEmac Emac;
47
48 void eth_halt(void)
49 {
50         return;
51 }
52
53 int eth_init(bd_t * bis)
54 {
55         u32 HelpReg;
56 #ifdef DEBUG
57         printf("EMAC Initialization Started\n\r");
58 #endif
59         if (Emac.IsStarted) {
60                 puts("Emac is started\n");
61                 return 0;
62         }
63
64         memset (&Emac, 0, sizeof (XEmac));
65
66         Emac.BaseAddress = XILINX_EMAC_BASEADDR;
67
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);
74
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);
80
81         /* Reset the entire IPIF */
82         out_be32 (Emac.BaseAddress + XIIF_V123B_RESETR_OFFSET,
83                                         XIIF_V123B_RESET_MASK);
84
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);
89
90         if (!getenv("ethaddr")) {
91                 memcpy(bis->bi_enetaddr, EMACAddr, ENET_ADDR_LENGTH);
92         }
93
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);
100
101
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);
106
107         Emac.IsStarted = 1;
108
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);
114
115         printf("EMAC Initialization complete\n\r");
116         return 0;
117 }
118
119 int eth_send(volatile void *ptr, int len)
120 {
121         u32 IntrStatus;
122         u32 XmitStatus;
123         u32 FifoCount;
124         u32 WordCount;
125         u32 ExtraByteCount;
126         u32 *WordBuffer = (u32 *) ptr;
127
128         if (len > ENET_MAX_MTU)
129                 len = ENET_MAX_MTU;
130
131         /*
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
136          * the error.
137          */
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)) {
141 #ifdef DEBUG
142                 puts ("Transmitting overrun error\n");
143 #endif
144                 return 0;
145         } else if (IntrStatus & (XEM_EIR_XMIT_SFIFO_UNDER_MASK |
146                         XEM_EIR_XMIT_LFIFO_UNDER_MASK)) {
147 #ifdef DEBUG
148                 puts ("Transmitting underrun error\n");
149 #endif
150                 return 0;
151         } else if (in_be32 (Emac.SendFifo.RegBaseAddress +
152                         XPF_COUNT_STATUS_REG_OFFSET) & XPF_DEADLOCK_MASK) {
153 #ifdef DEBUG
154                 puts("Transmitting fifo error\n");
155 #endif
156                 return 0;
157         }
158
159         /*
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
163          * FIFOs in sync.
164          *
165          * Clear the latched LFIFO_FULL bit so next time around the most
166          * current status is represented
167          */
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);
171 #ifdef DEBUG
172                 puts ("Fifo is full\n");
173 #endif
174                 return 0;
175         }
176
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;
182
183         if (FifoCount < WordCount) {
184 #ifdef DEBUG
185                 puts ("Sending packet is larger then size of FIFO\n");
186 #endif
187                 return 0;
188         }
189
190         for (FifoCount = 0; FifoCount < WordCount; FifoCount++) {
191                 out_be32 (Emac.SendFifo.DataBaseAddress, WordBuffer[FifoCount]);
192         }
193         if (ExtraByteCount > 0) {
194                 u32 LastWord = 0;
195                 u8 *ExtraBytesBuffer = (u8 *) (WordBuffer + WordCount);
196
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;
206                 }
207                 out_be32 (Emac.SendFifo.DataBaseAddress, LastWord);
208         }
209
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);
218         }
219
220         /*
221          * Set the MAC's transmit packet length register to tell it to transmit
222          */
223         out_be32 (Emac.BaseAddress + XEM_TPLR_OFFSET, len);
224
225         /*
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.
228          */
229         do {
230                 IntrStatus = in_be32 ((Emac.BaseAddress) +
231                                                 XIIF_V123B_IISR_OFFSET);
232         }
233         while ((IntrStatus & XEM_EIR_XMIT_DONE_MASK) == 0);
234
235         XmitStatus = in_be32 (Emac.BaseAddress + XEM_TSR_OFFSET);
236
237         if (IntrStatus & (XEM_EIR_XMIT_SFIFO_OVER_MASK |
238                                         XEM_EIR_XMIT_LFIFO_OVER_MASK)) {
239 #ifdef DEBUG
240                 puts ("Transmitting overrun error\n");
241 #endif
242                 return 0;
243         } else if (IntrStatus & (XEM_EIR_XMIT_SFIFO_UNDER_MASK |
244                                         XEM_EIR_XMIT_LFIFO_UNDER_MASK)) {
245 #ifdef DEBUG
246                 puts ("Transmitting underrun error\n");
247 #endif
248                 return 0;
249         }
250
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);
254
255         /*
256          * Collision errors are stored in the transmit status register
257          * instead of the interrupt status register
258          */
259         if ((XmitStatus & XEM_TSR_EXCESS_DEFERRAL_MASK) ||
260                                 (XmitStatus & XEM_TSR_LATE_COLLISION_MASK)) {
261 #ifdef DEBUG
262                 puts ("Transmitting collision error\n");
263 #endif
264                 return 0;
265         }
266         return 1;
267 }
268
269 int eth_rx(void)
270 {
271         u32 PktLength;
272         u32 IntrStatus;
273         u32 FifoCount;
274         u32 WordCount;
275         u32 ExtraByteCount;
276         u32 LastWord;
277         u8 *ExtraBytesBuffer;
278
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);
282 #ifdef DEBUG
283                 puts ("Receiving FIFO deadlock\n");
284 #endif
285                 return 0;
286         }
287
288         /*
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.
292          */
293         IntrStatus = in_be32 ((Emac.BaseAddress) + XIIF_V123B_IISR_OFFSET);
294         /*
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
297          * empty FIFO.
298          */
299         if (!(IntrStatus & XEM_EIR_RECV_DONE_MASK)) {
300 #ifdef DEBUG
301                 /* puts("Receiving FIFO is empty\n"); */
302 #endif
303                 return 0;
304         }
305
306         /*
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)
309          */
310         PktLength = in_be32 (Emac.BaseAddress + XEM_RPLR_OFFSET);
311         if (!PktLength) {
312                 return 0;
313         }
314
315         /*
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.
322          */
323         out_be32 ((Emac.BaseAddress) + XIIF_V123B_IISR_OFFSET,
324                                                 XEM_EIR_RECV_DONE_MASK);
325
326         FifoCount = in_be32 (Emac.RecvFifo.RegBaseAddress +
327                                 XPF_COUNT_STATUS_REG_OFFSET) & XPF_COUNT_MASK;
328
329         if ((FifoCount * 4) < PktLength) {
330 #ifdef DEBUG
331                 puts ("Receiving FIFO is smaller than packet size.\n");
332 #endif
333                 return 0;
334         }
335
336         WordCount = PktLength >> 2;
337         ExtraByteCount = PktLength & 0x3;
338
339         for (FifoCount = 0; FifoCount < WordCount; FifoCount++) {
340                 etherrxbuff[FifoCount] =
341                                 in_be32 (Emac.RecvFifo.DataBaseAddress);
342         }
343
344         /*
345          * if there are extra bytes to handle, read the last word from the FIFO
346          * and insert the extra bytes into the buffer
347          */
348         if (ExtraByteCount > 0) {
349                 ExtraBytesBuffer = (u8 *) (etherrxbuff + WordCount);
350
351                 LastWord = in_be32 (Emac.RecvFifo.DataBaseAddress);
352
353                 /*
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
357                  */
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);
367                 }
368         }
369         NetReceive((uchar *)etherrxbuff, PktLength);
370         return 1;
371 }
372 #endif