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