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