Michal Simek | 14b4c70 | 2009-09-07 09:08:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007-2009 Michal Simek |
| 3 | * (C) Copyright 2003 Xilinx Inc. |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 4 | * |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 5 | * Michal SIMEK <monstr@monstr.eu> |
| 6 | * |
Michal Simek | 14b4c70 | 2009-09-07 09:08:02 +0200 | [diff] [blame] | 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 19 | * |
Michal Simek | 14b4c70 | 2009-09-07 09:08:02 +0200 | [diff] [blame] | 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 25 | |
| 26 | #include <common.h> |
| 27 | #include <net.h> |
| 28 | #include <config.h> |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 29 | #include <malloc.h> |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 30 | #include <asm/io.h> |
| 31 | |
| 32 | #undef DEBUG |
| 33 | |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 34 | #define ENET_ADDR_LENGTH 6 |
| 35 | |
| 36 | /* EmacLite constants */ |
| 37 | #define XEL_BUFFER_OFFSET 0x0800 /* Next buffer's offset */ |
| 38 | #define XEL_TPLR_OFFSET 0x07F4 /* Tx packet length */ |
| 39 | #define XEL_TSR_OFFSET 0x07FC /* Tx status */ |
| 40 | #define XEL_RSR_OFFSET 0x17FC /* Rx status */ |
| 41 | #define XEL_RXBUFF_OFFSET 0x1000 /* Receive Buffer */ |
| 42 | |
| 43 | /* Xmit complete */ |
| 44 | #define XEL_TSR_XMIT_BUSY_MASK 0x00000001UL |
| 45 | /* Xmit interrupt enable bit */ |
| 46 | #define XEL_TSR_XMIT_IE_MASK 0x00000008UL |
| 47 | /* Buffer is active, SW bit only */ |
| 48 | #define XEL_TSR_XMIT_ACTIVE_MASK 0x80000000UL |
| 49 | /* Program the MAC address */ |
| 50 | #define XEL_TSR_PROGRAM_MASK 0x00000002UL |
| 51 | /* define for programming the MAC address into the EMAC Lite */ |
| 52 | #define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK) |
| 53 | |
| 54 | /* Transmit packet length upper byte */ |
| 55 | #define XEL_TPLR_LENGTH_MASK_HI 0x0000FF00UL |
| 56 | /* Transmit packet length lower byte */ |
| 57 | #define XEL_TPLR_LENGTH_MASK_LO 0x000000FFUL |
| 58 | |
| 59 | /* Recv complete */ |
| 60 | #define XEL_RSR_RECV_DONE_MASK 0x00000001UL |
| 61 | /* Recv interrupt enable bit */ |
| 62 | #define XEL_RSR_RECV_IE_MASK 0x00000008UL |
| 63 | |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 64 | struct xemaclite { |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 65 | u32 nexttxbuffertouse; /* Next TX buffer to write to */ |
| 66 | u32 nextrxbuffertouse; /* Next RX buffer to read from */ |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 67 | u32 txpp; /* TX ping pong buffer */ |
| 68 | u32 rxpp; /* RX ping pong buffer */ |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 69 | }; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 70 | |
Clive Stubbings | 0d50191 | 2008-10-27 15:05:00 +0000 | [diff] [blame] | 71 | static u32 etherrxbuff[PKTSIZE_ALIGN/4]; /* Receive buffer */ |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 72 | |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 73 | static void xemaclite_alignedread (u32 *srcptr, void *destptr, u32 bytecount) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 74 | { |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 75 | u32 i; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 76 | u32 alignbuffer; |
| 77 | u32 *to32ptr; |
| 78 | u32 *from32ptr; |
| 79 | u8 *to8ptr; |
| 80 | u8 *from8ptr; |
| 81 | |
| 82 | from32ptr = (u32 *) srcptr; |
| 83 | |
| 84 | /* Word aligned buffer, no correction needed. */ |
| 85 | to32ptr = (u32 *) destptr; |
| 86 | while (bytecount > 3) { |
| 87 | *to32ptr++ = *from32ptr++; |
| 88 | bytecount -= 4; |
| 89 | } |
| 90 | to8ptr = (u8 *) to32ptr; |
| 91 | |
| 92 | alignbuffer = *from32ptr++; |
| 93 | from8ptr = (u8 *) & alignbuffer; |
| 94 | |
| 95 | for (i = 0; i < bytecount; i++) { |
| 96 | *to8ptr++ = *from8ptr++; |
| 97 | } |
| 98 | } |
| 99 | |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 100 | static void xemaclite_alignedwrite (void *srcptr, u32 destptr, u32 bytecount) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 101 | { |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 102 | u32 i; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 103 | u32 alignbuffer; |
| 104 | u32 *to32ptr = (u32 *) destptr; |
| 105 | u32 *from32ptr; |
| 106 | u8 *to8ptr; |
| 107 | u8 *from8ptr; |
| 108 | |
| 109 | from32ptr = (u32 *) srcptr; |
| 110 | while (bytecount > 3) { |
| 111 | |
| 112 | *to32ptr++ = *from32ptr++; |
| 113 | bytecount -= 4; |
| 114 | } |
| 115 | |
| 116 | alignbuffer = 0; |
| 117 | to8ptr = (u8 *) & alignbuffer; |
| 118 | from8ptr = (u8 *) from32ptr; |
| 119 | |
| 120 | for (i = 0; i < bytecount; i++) { |
| 121 | *to8ptr++ = *from8ptr++; |
| 122 | } |
| 123 | |
| 124 | *to32ptr++ = alignbuffer; |
| 125 | } |
| 126 | |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 127 | static void emaclite_halt(struct eth_device *dev) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 128 | { |
| 129 | debug ("eth_halt\n"); |
| 130 | } |
| 131 | |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 132 | static int emaclite_init(struct eth_device *dev, bd_t *bis) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 133 | { |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 134 | struct xemaclite *emaclite = dev->priv; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 135 | debug ("EmacLite Initialization Started\n"); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * TX - TX_PING & TX_PONG initialization |
| 139 | */ |
| 140 | /* Restart PING TX */ |
Michal Simek | ac357ac | 2011-08-25 12:36:39 +0200 | [diff] [blame] | 141 | out_be32 (dev->iobase + XEL_TSR_OFFSET, 0); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 142 | /* Copy MAC address */ |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 143 | xemaclite_alignedwrite (dev->enetaddr, |
Michal Simek | ac357ac | 2011-08-25 12:36:39 +0200 | [diff] [blame] | 144 | dev->iobase, ENET_ADDR_LENGTH); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 145 | /* Set the length */ |
Michal Simek | ac357ac | 2011-08-25 12:36:39 +0200 | [diff] [blame] | 146 | out_be32 (dev->iobase + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 147 | /* Update the MAC address in the EMAC Lite */ |
Michal Simek | ac357ac | 2011-08-25 12:36:39 +0200 | [diff] [blame] | 148 | out_be32 (dev->iobase + XEL_TSR_OFFSET, XEL_TSR_PROG_MAC_ADDR); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 149 | /* Wait for EMAC Lite to finish with the MAC address update */ |
Michal Simek | ac357ac | 2011-08-25 12:36:39 +0200 | [diff] [blame] | 150 | while ((in_be32 (dev->iobase + XEL_TSR_OFFSET) & |
| 151 | XEL_TSR_PROG_MAC_ADDR) != 0) |
| 152 | ; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 153 | |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 154 | if (emaclite->txpp) { |
| 155 | /* The same operation with PONG TX */ |
| 156 | out_be32 (dev->iobase + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET, 0); |
| 157 | xemaclite_alignedwrite(dev->enetaddr, dev->iobase + |
| 158 | XEL_BUFFER_OFFSET, ENET_ADDR_LENGTH); |
| 159 | out_be32 (dev->iobase + XEL_TPLR_OFFSET, ENET_ADDR_LENGTH); |
| 160 | out_be32 (dev->iobase + XEL_TSR_OFFSET + XEL_BUFFER_OFFSET, |
| 161 | XEL_TSR_PROG_MAC_ADDR); |
| 162 | while ((in_be32 (dev->iobase + XEL_TSR_OFFSET + |
| 163 | XEL_BUFFER_OFFSET) & XEL_TSR_PROG_MAC_ADDR) != 0) |
| 164 | ; |
| 165 | } |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * RX - RX_PING & RX_PONG initialization |
| 169 | */ |
| 170 | /* Write out the value to flush the RX buffer */ |
Michal Simek | ac357ac | 2011-08-25 12:36:39 +0200 | [diff] [blame] | 171 | out_be32 (dev->iobase + XEL_RSR_OFFSET, XEL_RSR_RECV_IE_MASK); |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 172 | |
| 173 | if (emaclite->rxpp) |
| 174 | out_be32 (dev->iobase + XEL_RSR_OFFSET + XEL_BUFFER_OFFSET, |
| 175 | XEL_RSR_RECV_IE_MASK); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 176 | |
| 177 | debug ("EmacLite Initialization complete\n"); |
| 178 | return 0; |
| 179 | } |
| 180 | |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 181 | static int xemaclite_txbufferavailable(struct eth_device *dev) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 182 | { |
| 183 | u32 reg; |
| 184 | u32 txpingbusy; |
| 185 | u32 txpongbusy; |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 186 | struct xemaclite *emaclite = dev->priv; |
| 187 | |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 188 | /* |
| 189 | * Read the other buffer register |
| 190 | * and determine if the other buffer is available |
| 191 | */ |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 192 | reg = in_be32 (dev->iobase + |
| 193 | emaclite->nexttxbuffertouse + 0); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 194 | txpingbusy = ((reg & XEL_TSR_XMIT_BUSY_MASK) == |
| 195 | XEL_TSR_XMIT_BUSY_MASK); |
| 196 | |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 197 | reg = in_be32 (dev->iobase + |
| 198 | (emaclite->nexttxbuffertouse ^ XEL_TSR_OFFSET) + 0); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 199 | txpongbusy = ((reg & XEL_TSR_XMIT_BUSY_MASK) == |
| 200 | XEL_TSR_XMIT_BUSY_MASK); |
| 201 | |
| 202 | return (!(txpingbusy && txpongbusy)); |
| 203 | } |
| 204 | |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 205 | static int emaclite_send (struct eth_device *dev, volatile void *ptr, int len) |
| 206 | { |
| 207 | u32 reg; |
| 208 | u32 baseaddress; |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 209 | struct xemaclite *emaclite = dev->priv; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 210 | |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 211 | u32 maxtry = 1000; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 212 | |
Michal Simek | 3aa96f8 | 2011-09-12 21:10:04 +0000 | [diff] [blame^] | 213 | if (len > PKTSIZE) |
| 214 | len = PKTSIZE; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 215 | |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 216 | while (!xemaclite_txbufferavailable(dev) && maxtry) { |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 217 | udelay (10); |
| 218 | maxtry--; |
| 219 | } |
| 220 | |
| 221 | if (!maxtry) { |
| 222 | printf ("Error: Timeout waiting for ethernet TX buffer\n"); |
| 223 | /* Restart PING TX */ |
Michal Simek | ac357ac | 2011-08-25 12:36:39 +0200 | [diff] [blame] | 224 | out_be32 (dev->iobase + XEL_TSR_OFFSET, 0); |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 225 | if (emaclite->txpp) { |
| 226 | out_be32 (dev->iobase + XEL_TSR_OFFSET + |
| 227 | XEL_BUFFER_OFFSET, 0); |
| 228 | } |
Michal Simek | 2986921 | 2011-03-08 04:25:53 +0000 | [diff] [blame] | 229 | return -1; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* Determine the expected TX buffer address */ |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 233 | baseaddress = (dev->iobase + emaclite->nexttxbuffertouse); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 234 | |
| 235 | /* Determine if the expected buffer address is empty */ |
| 236 | reg = in_be32 (baseaddress + XEL_TSR_OFFSET); |
| 237 | if (((reg & XEL_TSR_XMIT_BUSY_MASK) == 0) |
| 238 | && ((in_be32 ((baseaddress) + XEL_TSR_OFFSET) |
| 239 | & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) { |
| 240 | |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 241 | if (emaclite->txpp) |
| 242 | emaclite->nexttxbuffertouse ^= XEL_BUFFER_OFFSET; |
| 243 | |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 244 | debug ("Send packet from 0x%x\n", baseaddress); |
| 245 | /* Write the frame to the buffer */ |
| 246 | xemaclite_alignedwrite ((void *) ptr, baseaddress, len); |
| 247 | out_be32 (baseaddress + XEL_TPLR_OFFSET,(len & |
| 248 | (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO))); |
| 249 | reg = in_be32 (baseaddress + XEL_TSR_OFFSET); |
| 250 | reg |= XEL_TSR_XMIT_BUSY_MASK; |
| 251 | if ((reg & XEL_TSR_XMIT_IE_MASK) != 0) { |
| 252 | reg |= XEL_TSR_XMIT_ACTIVE_MASK; |
| 253 | } |
| 254 | out_be32 (baseaddress + XEL_TSR_OFFSET, reg); |
Michal Simek | 2986921 | 2011-03-08 04:25:53 +0000 | [diff] [blame] | 255 | return 0; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 256 | } |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 257 | |
| 258 | if (emaclite->txpp) { |
| 259 | /* Switch to second buffer */ |
| 260 | baseaddress ^= XEL_BUFFER_OFFSET; |
| 261 | /* Determine if the expected buffer address is empty */ |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 262 | reg = in_be32 (baseaddress + XEL_TSR_OFFSET); |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 263 | if (((reg & XEL_TSR_XMIT_BUSY_MASK) == 0) |
| 264 | && ((in_be32 ((baseaddress) + XEL_TSR_OFFSET) |
| 265 | & XEL_TSR_XMIT_ACTIVE_MASK) == 0)) { |
| 266 | debug("Send packet from 0x%x\n", baseaddress); |
| 267 | /* Write the frame to the buffer */ |
| 268 | xemaclite_alignedwrite((void *) ptr, baseaddress, len); |
| 269 | out_be32 (baseaddress + XEL_TPLR_OFFSET, (len & |
| 270 | (XEL_TPLR_LENGTH_MASK_HI | |
| 271 | XEL_TPLR_LENGTH_MASK_LO))); |
| 272 | reg = in_be32 (baseaddress + XEL_TSR_OFFSET); |
| 273 | reg |= XEL_TSR_XMIT_BUSY_MASK; |
| 274 | if ((reg & XEL_TSR_XMIT_IE_MASK) != 0) |
| 275 | reg |= XEL_TSR_XMIT_ACTIVE_MASK; |
| 276 | out_be32 (baseaddress + XEL_TSR_OFFSET, reg); |
| 277 | return 0; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 278 | } |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 279 | } |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 280 | |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 281 | puts ("Error while sending frame\n"); |
Michal Simek | 2986921 | 2011-03-08 04:25:53 +0000 | [diff] [blame] | 282 | return -1; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 285 | static int emaclite_recv(struct eth_device *dev) |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 286 | { |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 287 | u32 length; |
| 288 | u32 reg; |
| 289 | u32 baseaddress; |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 290 | struct xemaclite *emaclite = dev->priv; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 291 | |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 292 | baseaddress = dev->iobase + emaclite->nextrxbuffertouse; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 293 | reg = in_be32 (baseaddress + XEL_RSR_OFFSET); |
| 294 | debug ("Testing data at address 0x%x\n", baseaddress); |
| 295 | if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) { |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 296 | if (emaclite->rxpp) |
| 297 | emaclite->nextrxbuffertouse ^= XEL_BUFFER_OFFSET; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 298 | } else { |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 299 | |
| 300 | if (!emaclite->rxpp) { |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 301 | debug ("No data was available - address 0x%x\n", |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 302 | baseaddress); |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 303 | return 0; |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 304 | } else { |
| 305 | baseaddress ^= XEL_BUFFER_OFFSET; |
| 306 | reg = in_be32 (baseaddress + XEL_RSR_OFFSET); |
| 307 | if ((reg & XEL_RSR_RECV_DONE_MASK) != |
| 308 | XEL_RSR_RECV_DONE_MASK) { |
| 309 | debug("No data was available - address 0x%x\n", |
| 310 | baseaddress); |
| 311 | return 0; |
| 312 | } |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 313 | } |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 314 | } |
| 315 | /* Get the length of the frame that arrived */ |
Michal Simek | 1b9ecc9 | 2010-10-11 11:41:46 +1000 | [diff] [blame] | 316 | switch(((ntohl(in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0xC))) & |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 317 | 0xFFFF0000 ) >> 16) { |
| 318 | case 0x806: |
| 319 | length = 42 + 20; /* FIXME size of ARP */ |
| 320 | debug ("ARP Packet\n"); |
| 321 | break; |
| 322 | case 0x800: |
| 323 | length = 14 + 14 + |
Michal Simek | 1b9ecc9 | 2010-10-11 11:41:46 +1000 | [diff] [blame] | 324 | (((ntohl(in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0x10))) & |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 325 | 0xFFFF0000) >> 16); /* FIXME size of IP packet */ |
| 326 | debug ("IP Packet\n"); |
| 327 | break; |
| 328 | default: |
Michal Simek | 3aa96f8 | 2011-09-12 21:10:04 +0000 | [diff] [blame^] | 329 | debug("Other Packet\n"); |
| 330 | length = PKTSIZE; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | |
| 334 | xemaclite_alignedread ((u32 *) (baseaddress + XEL_RXBUFF_OFFSET), |
| 335 | etherrxbuff, length); |
| 336 | |
| 337 | /* Acknowledge the frame */ |
| 338 | reg = in_be32 (baseaddress + XEL_RSR_OFFSET); |
| 339 | reg &= ~XEL_RSR_RECV_DONE_MASK; |
| 340 | out_be32 (baseaddress + XEL_RSR_OFFSET, reg); |
| 341 | |
| 342 | debug ("Packet receive from 0x%x, length %dB\n", baseaddress, length); |
| 343 | NetReceive ((uchar *) etherrxbuff, length); |
Michal Simek | 2986921 | 2011-03-08 04:25:53 +0000 | [diff] [blame] | 344 | return length; |
Michal Simek | 4514b37 | 2008-03-28 12:41:56 +0100 | [diff] [blame] | 345 | |
| 346 | } |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 347 | |
| 348 | int xilinx_emaclite_initialize (bd_t *bis, int base_addr) |
| 349 | { |
| 350 | struct eth_device *dev; |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 351 | struct xemaclite *emaclite; |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 352 | |
Michal Simek | 8f2bf36 | 2011-08-25 12:28:47 +0200 | [diff] [blame] | 353 | dev = calloc(1, sizeof(*dev)); |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 354 | if (dev == NULL) |
Michal Simek | 2986921 | 2011-03-08 04:25:53 +0000 | [diff] [blame] | 355 | return -1; |
Michal Simek | f35b7cd | 2011-08-25 12:47:56 +0200 | [diff] [blame] | 356 | |
| 357 | emaclite = calloc(1, sizeof(struct xemaclite)); |
| 358 | if (emaclite == NULL) { |
| 359 | free(dev); |
| 360 | return -1; |
| 361 | } |
| 362 | |
| 363 | dev->priv = emaclite; |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 364 | |
Michal Simek | df40ead | 2011-09-12 21:10:01 +0000 | [diff] [blame] | 365 | #ifdef CONFIG_XILINX_EMACLITE_TX_PING_PONG |
| 366 | emaclite->txpp = 1; |
| 367 | #endif |
| 368 | #ifdef CONFIG_XILINX_EMACLITE_RX_PING_PONG |
| 369 | emaclite->rxpp = 1; |
| 370 | #endif |
| 371 | |
Michal Simek | 0c7fa44 | 2011-08-25 12:25:14 +0200 | [diff] [blame] | 372 | sprintf(dev->name, "Xelite.%x", base_addr); |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 373 | |
| 374 | dev->iobase = base_addr; |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 375 | dev->init = emaclite_init; |
| 376 | dev->halt = emaclite_halt; |
| 377 | dev->send = emaclite_send; |
| 378 | dev->recv = emaclite_recv; |
| 379 | |
| 380 | eth_register(dev); |
| 381 | |
Michal Simek | 2986921 | 2011-03-08 04:25:53 +0000 | [diff] [blame] | 382 | return 1; |
Michal Simek | b4a1d08 | 2010-10-11 11:41:47 +1000 | [diff] [blame] | 383 | } |