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,
30 #ifdef XILINX_EMACLITE_BASEADDR
34 #define ENET_MAX_MTU PKTSIZE
35 #define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
36 #define ENET_ADDR_LENGTH 6
38 /* EmacLite constants */
39 #define XEL_BUFFER_OFFSET 0x0800 /* Next buffer's offset */
40 #define XEL_TPLR_OFFSET 0x07F4 /* Tx packet length */
41 #define XEL_TSR_OFFSET 0x07FC /* Tx status */
42 #define XEL_RSR_OFFSET 0x17FC /* Rx status */
43 #define XEL_RXBUFF_OFFSET 0x1000 /* Receive Buffer */
46 #define XEL_TSR_XMIT_BUSY_MASK 0x00000001UL
47 /* Xmit interrupt enable bit */
48 #define XEL_TSR_XMIT_IE_MASK 0x00000008UL
49 /* Buffer is active, SW bit only */
50 #define XEL_TSR_XMIT_ACTIVE_MASK 0x80000000UL
51 /* Program the MAC address */
52 #define XEL_TSR_PROGRAM_MASK 0x00000002UL
53 /* define for programming the MAC address into the EMAC Lite */
54 #define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
56 /* Transmit packet length upper byte */
57 #define XEL_TPLR_LENGTH_MASK_HI 0x0000FF00UL
58 /* Transmit packet length lower byte */
59 #define XEL_TPLR_LENGTH_MASK_LO 0x000000FFUL
62 #define XEL_RSR_RECV_DONE_MASK 0x00000001UL
63 /* Recv interrupt enable bit */
64 #define XEL_RSR_RECV_IE_MASK 0x00000008UL
67 unsigned int BaseAddress; /* Base address for device (IPIF) */
68 unsigned int NextTxBufferToUse; /* Next TX buffer to write to */
69 unsigned int NextRxBufferToUse; /* Next RX buffer to read from */
70 unsigned char DeviceId; /* Unique ID of device - for future */
73 static XEmacLite EmacLite;
75 static char etherrxbuff[PKTSIZE_ALIGN]; /* Receive buffer */
77 /* hardcoded MAC address for the Xilinx EMAC Core when env is nowhere*/
78 #ifdef CFG_ENV_IS_NOWHERE
79 static u8 EMACAddr[ENET_ADDR_LENGTH] = { 0x00, 0x0a, 0x35, 0x00, 0x22, 0x01 };
82 void XEmacLite_AlignedRead (u32 * SrcPtr, void *DestPtr, unsigned ByteCount)
85 unsigned Length = ByteCount;
92 From32Ptr = (u32 *) SrcPtr;
94 /* Word aligned buffer, no correction needed. */
95 To32Ptr = (u32 *) DestPtr;
97 *To32Ptr++ = *From32Ptr++;
100 To8Ptr = (u8 *) To32Ptr;
102 AlignBuffer = *From32Ptr++;
103 From8Ptr = (u8 *) & AlignBuffer;
105 for (i = 0; i < Length; i++) {
106 *To8Ptr++ = *From8Ptr++;
110 void XEmacLite_AlignedWrite (void *SrcPtr, u32 * DestPtr, unsigned ByteCount)
113 unsigned Length = ByteCount;
121 From32Ptr = (u32 *) SrcPtr;
124 *To32Ptr++ = *From32Ptr++;
129 To8Ptr = (u8 *) & AlignBuffer;
130 From8Ptr = (u8 *) From32Ptr;
132 for (i = 0; i < Length; i++) {
133 *To8Ptr++ = *From8Ptr++;
136 *To32Ptr++ = AlignBuffer;
146 int eth_init (bd_t * bis)
149 puts ("EmacLite Initialization Started\n");
151 memset (&EmacLite, 0, sizeof (XEmacLite));
152 EmacLite.BaseAddress = XILINX_EMACLITE_BASEADDR;
154 #ifdef CFG_ENV_IS_NOWHERE
155 memcpy (bis->bi_enetaddr, EMACAddr, ENET_ADDR_LENGTH);
158 * TX - TX_PING & TX_PONG initialization
160 /* Restart PING TX */
161 out_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET, 0);
162 /* Copy MAC address */
163 XEmacLite_AlignedWrite (bis->bi_enetaddr,
164 EmacLite.BaseAddress, ENET_ADDR_LENGTH);
166 out_be32 (EmacLite.BaseAddress + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH);
167 /* Update the MAC address in the EMAC Lite */
168 out_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET, XEL_TSR_PROG_MAC_ADDR);
169 /* Wait for EMAC Lite to finish with the MAC address update */
170 while ((in_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET) &
171 XEL_TSR_PROG_MAC_ADDR) != 0) ;
173 #ifdef XILINX_EMACLITE_TX_PING_PONG
174 /* The same operation with PONG TX */
175 out_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET, 0);
176 XEmacLite_AlignedWrite (bis->bi_enetaddr,
177 EmacLite.BaseAddress + XEL_BUFFER_OFFSET, ENET_ADDR_LENGTH);
178 out_be32 (EmacLite.BaseAddress + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH);
179 out_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET,
180 XEL_TSR_PROG_MAC_ADDR);
181 while ((in_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET +
182 XEL_BUFFER_OFFSET) & XEL_TSR_PROG_MAC_ADDR) != 0) ;
186 * RX - RX_PING & RX_PONG initialization
188 /* Write out the value to flush the RX buffer */
189 out_be32 (EmacLite.BaseAddress + XEL_RSR_OFFSET, XEL_RSR_RECV_IE_MASK);
190 #ifdef XILINX_EMACLITE_RX_PING_PONG
191 out_be32 (EmacLite.BaseAddress + XEL_RSR_OFFSET + XEL_BUFFER_OFFSET,
192 XEL_RSR_RECV_IE_MASK);
196 puts ("EmacLite Initialization complete\n");
201 int XEmacLite_TxBufferAvailable (XEmacLite * InstancePtr)
207 * Read the other buffer register
208 * and determine if the other buffer is available
210 Register = in_be32 (InstancePtr->BaseAddress +
211 InstancePtr->NextTxBufferToUse + 0);
212 TxPingBusy = ((Register & XEL_TSR_XMIT_BUSY_MASK) ==
213 XEL_TSR_XMIT_BUSY_MASK);
215 Register = in_be32 (InstancePtr->BaseAddress +
216 (InstancePtr->NextTxBufferToUse ^ XEL_TSR_OFFSET) + 0);
217 TxPongBusy = ((Register & XEL_TSR_XMIT_BUSY_MASK) ==
218 XEL_TSR_XMIT_BUSY_MASK);
220 return (!(TxPingBusy && TxPongBusy));
223 int eth_send (volatile void *ptr, int len) {
225 unsigned int Register;
226 unsigned int BaseAddress;
228 unsigned maxtry = 1000;
230 if (len > ENET_MAX_MTU)
233 while (!XEmacLite_TxBufferAvailable (&EmacLite) && maxtry) {
239 printf ("Error: Timeout waiting for ethernet TX buffer\n");
240 /* Restart PING TX */
241 out_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET, 0);
242 #ifdef XILINX_EMACLITE_TX_PING_PONG
243 out_be32 (EmacLite.BaseAddress + XEL_TSR_OFFSET +
244 XEL_BUFFER_OFFSET, 0);
249 /* Determine the expected TX buffer address */
250 BaseAddress = (EmacLite.BaseAddress + EmacLite.NextTxBufferToUse);
252 /* Determine if the expected buffer address is empty */
253 Register = in_be32 (BaseAddress + XEL_TSR_OFFSET);
254 if (((Register & XEL_TSR_XMIT_BUSY_MASK) == 0)
255 && ((in_be32 ((BaseAddress) + XEL_TSR_OFFSET)
256 & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
258 #ifdef XILINX_EMACLITE_TX_PING_PONG
259 EmacLite.NextTxBufferToUse ^= XEL_BUFFER_OFFSET;
262 printf ("Send packet from 0x%x\n", BaseAddress);
264 /* Write the frame to the buffer */
265 XEmacLite_AlignedWrite (ptr, (u32 *) BaseAddress, len);
266 out_be32 (BaseAddress + XEL_TPLR_OFFSET,(len &
267 (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO)));
268 Register = in_be32 (BaseAddress + XEL_TSR_OFFSET);
269 Register |= XEL_TSR_XMIT_BUSY_MASK;
270 if ((Register & XEL_TSR_XMIT_IE_MASK) != 0) {
271 Register |= XEL_TSR_XMIT_ACTIVE_MASK;
273 out_be32 (BaseAddress + XEL_TSR_OFFSET, Register);
276 #ifdef XILINX_EMACLITE_TX_PING_PONG
277 /* Switch to second buffer */
278 BaseAddress ^= XEL_BUFFER_OFFSET;
279 /* Determine if the expected buffer address is empty */
280 Register = in_be32 (BaseAddress + XEL_TSR_OFFSET);
281 if (((Register & XEL_TSR_XMIT_BUSY_MASK) == 0)
282 && ((in_be32 ((BaseAddress) + XEL_TSR_OFFSET)
283 & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) {
285 printf ("Send packet from 0x%x\n", BaseAddress);
287 /* Write the frame to the buffer */
288 XEmacLite_AlignedWrite (ptr, (u32 *) BaseAddress, len);
289 out_be32 (BaseAddress + XEL_TPLR_OFFSET,(len &
290 (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO)));
291 Register = in_be32 (BaseAddress + XEL_TSR_OFFSET);
292 Register |= XEL_TSR_XMIT_BUSY_MASK;
293 if ((Register & XEL_TSR_XMIT_IE_MASK) != 0) {
294 Register |= XEL_TSR_XMIT_ACTIVE_MASK;
296 out_be32 (BaseAddress + XEL_TSR_OFFSET, Register);
300 puts ("Error while sending frame\n");
307 unsigned int Register;
308 unsigned int BaseAddress;
310 BaseAddress = EmacLite.BaseAddress + EmacLite.NextRxBufferToUse;
311 Register = in_be32 (BaseAddress + XEL_RSR_OFFSET);
313 // printf ("Testing data at address 0x%x\n", BaseAddress);
315 if ((Register & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
316 #ifdef XILINX_EMACLITE_RX_PING_PONG
317 EmacLite.NextRxBufferToUse ^= XEL_BUFFER_OFFSET;
320 #ifndef XILINX_EMACLITE_RX_PING_PONG
322 // printf ("No data was available - address 0x%x\n", BaseAddress);
326 BaseAddress ^= XEL_BUFFER_OFFSET;
327 Register = in_be32 (BaseAddress + XEL_RSR_OFFSET);
328 if ((Register & XEL_RSR_RECV_DONE_MASK) !=
329 XEL_RSR_RECV_DONE_MASK) {
331 // printf ("No data was available - address 0x%x\n",
338 /* Get the length of the frame that arrived */
339 switch(((in_be32(BaseAddress + XEL_RXBUFF_OFFSET + 0xC)) &
340 0xFFFF0000 ) >> 16) {
342 Length = 42 + 20; /* FIXME size of ARP */
344 puts ("ARP Packet\n");
349 (((in_be32(BaseAddress + XEL_RXBUFF_OFFSET + 0x10)) &
350 0xFFFF0000) >> 16); /* FIXME size of IP packet */
357 puts("Other Packet\n");
359 Length = ENET_MAX_MTU;
363 XEmacLite_AlignedRead ((BaseAddress + XEL_RXBUFF_OFFSET),
364 etherrxbuff, Length);
366 /* Acknowledge the frame */
367 Register = in_be32 (BaseAddress + XEL_RSR_OFFSET);
368 Register &= ~XEL_RSR_RECV_DONE_MASK;
369 out_be32 (BaseAddress + XEL_RSR_OFFSET, Register);
372 printf ("Packet receive from 0x%x, length %dB\n", BaseAddress, Length);
374 NetReceive ((uchar *) etherrxbuff, Length);