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