blob: 800345bba489534b68e8ca29e6e799505d4c81cf [file] [log] [blame]
Michal Simek14b4c702009-09-07 09:08:02 +02001/*
2 * (C) Copyright 2007-2009 Michal Simek
3 * (C) Copyright 2003 Xilinx Inc.
Michal Simek4514b372008-03-28 12:41:56 +01004 *
Michal Simek4514b372008-03-28 12:41:56 +01005 * Michal SIMEK <monstr@monstr.eu>
6 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02007 * SPDX-License-Identifier: GPL-2.0+
Michal Simek14b4c702009-09-07 09:08:02 +02008 */
Michal Simek4514b372008-03-28 12:41:56 +01009
10#include <common.h>
11#include <net.h>
12#include <config.h>
Michal Simek912145b2015-12-10 13:33:20 +010013#include <console.h>
Michal Simekb4a1d082010-10-11 11:41:47 +100014#include <malloc.h>
Michal Simek4514b372008-03-28 12:41:56 +010015#include <asm/io.h>
Michal Simek912145b2015-12-10 13:33:20 +010016#include <phy.h>
17#include <miiphy.h>
Michal Simekbb8b27b2012-06-28 21:37:57 +000018#include <fdtdec.h>
Michal Simek912145b2015-12-10 13:33:20 +010019#include <asm-generic/errno.h>
Michal Simek36f7a412015-12-10 16:31:38 +010020#include <linux/kernel.h>
Michal Simekbb8b27b2012-06-28 21:37:57 +000021
Michal Simek4514b372008-03-28 12:41:56 +010022#undef DEBUG
23
Michal Simek4514b372008-03-28 12:41:56 +010024#define ENET_ADDR_LENGTH 6
Michal Simek36f7a412015-12-10 16:31:38 +010025#define ETH_FCS_LEN 4 /* Octets in the FCS */
Michal Simek4514b372008-03-28 12:41:56 +010026
27/* Xmit complete */
28#define XEL_TSR_XMIT_BUSY_MASK 0x00000001UL
29/* Xmit interrupt enable bit */
30#define XEL_TSR_XMIT_IE_MASK 0x00000008UL
Michal Simek4514b372008-03-28 12:41:56 +010031/* Program the MAC address */
32#define XEL_TSR_PROGRAM_MASK 0x00000002UL
33/* define for programming the MAC address into the EMAC Lite */
34#define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
35
36/* Transmit packet length upper byte */
37#define XEL_TPLR_LENGTH_MASK_HI 0x0000FF00UL
38/* Transmit packet length lower byte */
39#define XEL_TPLR_LENGTH_MASK_LO 0x000000FFUL
40
41/* Recv complete */
42#define XEL_RSR_RECV_DONE_MASK 0x00000001UL
43/* Recv interrupt enable bit */
44#define XEL_RSR_RECV_IE_MASK 0x00000008UL
45
Michal Simek912145b2015-12-10 13:33:20 +010046/* MDIO Address Register Bit Masks */
47#define XEL_MDIOADDR_REGADR_MASK 0x0000001F /* Register Address */
48#define XEL_MDIOADDR_PHYADR_MASK 0x000003E0 /* PHY Address */
49#define XEL_MDIOADDR_PHYADR_SHIFT 5
50#define XEL_MDIOADDR_OP_MASK 0x00000400 /* RD/WR Operation */
51
52/* MDIO Write Data Register Bit Masks */
53#define XEL_MDIOWR_WRDATA_MASK 0x0000FFFF /* Data to be Written */
54
55/* MDIO Read Data Register Bit Masks */
56#define XEL_MDIORD_RDDATA_MASK 0x0000FFFF /* Data to be Read */
57
58/* MDIO Control Register Bit Masks */
59#define XEL_MDIOCTRL_MDIOSTS_MASK 0x00000001 /* MDIO Status Mask */
60#define XEL_MDIOCTRL_MDIOEN_MASK 0x00000008 /* MDIO Enable */
61
Michal Simek905f0982015-12-10 14:18:15 +010062struct emaclite_regs {
63 u32 tx_ping; /* 0x0 - TX Ping buffer */
64 u32 reserved1[504];
65 u32 mdioaddr; /* 0x7e4 - MDIO Address Register */
66 u32 mdiowr; /* 0x7e8 - MDIO Write Data Register */
67 u32 mdiord;/* 0x7ec - MDIO Read Data Register */
68 u32 mdioctrl; /* 0x7f0 - MDIO Control Register */
69 u32 tx_ping_tplr; /* 0x7f4 - Tx packet length */
70 u32 global_interrupt; /* 0x7f8 - Global interrupt enable */
71 u32 tx_ping_tsr; /* 0x7fc - Tx status */
72 u32 tx_pong; /* 0x800 - TX Pong buffer */
73 u32 reserved2[508];
74 u32 tx_pong_tplr; /* 0xff4 - Tx packet length */
75 u32 reserved3; /* 0xff8 */
76 u32 tx_pong_tsr; /* 0xffc - Tx status */
77 u32 rx_ping; /* 0x1000 - Receive Buffer */
78 u32 reserved4[510];
79 u32 rx_ping_rsr; /* 0x17fc - Rx status */
80 u32 rx_pong; /* 0x1800 - Receive Buffer */
81 u32 reserved5[510];
82 u32 rx_pong_rsr; /* 0x1ffc - Rx status */
83};
84
Michal Simekf35b7cd2011-08-25 12:47:56 +020085struct xemaclite {
Michal Simek36f7a412015-12-10 16:31:38 +010086 bool use_rx_pong_buffer_next; /* Next RX buffer to read from */
Michal Simekdf40ead2011-09-12 21:10:01 +000087 u32 txpp; /* TX ping pong buffer */
88 u32 rxpp; /* RX ping pong buffer */
Michal Simek912145b2015-12-10 13:33:20 +010089 int phyaddr;
Michal Simek905f0982015-12-10 14:18:15 +010090 struct emaclite_regs *regs;
Michal Simek912145b2015-12-10 13:33:20 +010091 struct phy_device *phydev;
92 struct mii_dev *bus;
Michal Simekf35b7cd2011-08-25 12:47:56 +020093};
Michal Simek4514b372008-03-28 12:41:56 +010094
Clive Stubbings0d501912008-10-27 15:05:00 +000095static u32 etherrxbuff[PKTSIZE_ALIGN/4]; /* Receive buffer */
Michal Simek4514b372008-03-28 12:41:56 +010096
Michal Simek5d1cf6c2011-09-12 21:10:05 +000097static void xemaclite_alignedread(u32 *srcptr, void *destptr, u32 bytecount)
Michal Simek4514b372008-03-28 12:41:56 +010098{
Michal Simekb4a1d082010-10-11 11:41:47 +100099 u32 i;
Michal Simek4514b372008-03-28 12:41:56 +0100100 u32 alignbuffer;
101 u32 *to32ptr;
102 u32 *from32ptr;
103 u8 *to8ptr;
104 u8 *from8ptr;
105
106 from32ptr = (u32 *) srcptr;
107
108 /* Word aligned buffer, no correction needed. */
109 to32ptr = (u32 *) destptr;
110 while (bytecount > 3) {
111 *to32ptr++ = *from32ptr++;
112 bytecount -= 4;
113 }
114 to8ptr = (u8 *) to32ptr;
115
116 alignbuffer = *from32ptr++;
Michal Simek5d1cf6c2011-09-12 21:10:05 +0000117 from8ptr = (u8 *) &alignbuffer;
Michal Simek4514b372008-03-28 12:41:56 +0100118
Michal Simek5d1cf6c2011-09-12 21:10:05 +0000119 for (i = 0; i < bytecount; i++)
Michal Simek4514b372008-03-28 12:41:56 +0100120 *to8ptr++ = *from8ptr++;
Michal Simek4514b372008-03-28 12:41:56 +0100121}
122
Michal Simek90e89bf2015-12-10 16:01:50 +0100123static void xemaclite_alignedwrite(void *srcptr, u32 *destptr, u32 bytecount)
Michal Simek4514b372008-03-28 12:41:56 +0100124{
Michal Simekb4a1d082010-10-11 11:41:47 +1000125 u32 i;
Michal Simek4514b372008-03-28 12:41:56 +0100126 u32 alignbuffer;
127 u32 *to32ptr = (u32 *) destptr;
128 u32 *from32ptr;
129 u8 *to8ptr;
130 u8 *from8ptr;
131
132 from32ptr = (u32 *) srcptr;
133 while (bytecount > 3) {
134
135 *to32ptr++ = *from32ptr++;
136 bytecount -= 4;
137 }
138
139 alignbuffer = 0;
Michal Simek5d1cf6c2011-09-12 21:10:05 +0000140 to8ptr = (u8 *) &alignbuffer;
Michal Simek4514b372008-03-28 12:41:56 +0100141 from8ptr = (u8 *) from32ptr;
142
Michal Simek5d1cf6c2011-09-12 21:10:05 +0000143 for (i = 0; i < bytecount; i++)
Michal Simek4514b372008-03-28 12:41:56 +0100144 *to8ptr++ = *from8ptr++;
Michal Simek4514b372008-03-28 12:41:56 +0100145
146 *to32ptr++ = alignbuffer;
147}
148
Michal Simek912145b2015-12-10 13:33:20 +0100149#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
150static int wait_for_bit(const char *func, u32 *reg, const u32 mask,
151 bool set, unsigned int timeout)
152{
153 u32 val;
154 unsigned long start = get_timer(0);
155
156 while (1) {
157 val = readl(reg);
158
159 if (!set)
160 val = ~val;
161
162 if ((val & mask) == mask)
163 return 0;
164
165 if (get_timer(start) > timeout)
166 break;
167
168 if (ctrlc()) {
169 puts("Abort\n");
170 return -EINTR;
171 }
172
173 udelay(1);
174 }
175
176 debug("%s: Timeout (reg=%p mask=%08x wait_set=%i)\n",
177 func, reg, mask, set);
178
179 return -ETIMEDOUT;
180}
181
Michal Simek905f0982015-12-10 14:18:15 +0100182static int mdio_wait(struct emaclite_regs *regs)
Michal Simek912145b2015-12-10 13:33:20 +0100183{
Michal Simek905f0982015-12-10 14:18:15 +0100184 return wait_for_bit(__func__, &regs->mdioctrl,
Michal Simek912145b2015-12-10 13:33:20 +0100185 XEL_MDIOCTRL_MDIOSTS_MASK, false, 2000);
186}
187
Michal Simek905f0982015-12-10 14:18:15 +0100188static u32 phyread(struct xemaclite *emaclite, u32 phyaddress, u32 registernum,
Michal Simek912145b2015-12-10 13:33:20 +0100189 u16 *data)
190{
Michal Simek905f0982015-12-10 14:18:15 +0100191 struct emaclite_regs *regs = emaclite->regs;
192
193 if (mdio_wait(regs))
Michal Simek912145b2015-12-10 13:33:20 +0100194 return 1;
195
Michal Simek905f0982015-12-10 14:18:15 +0100196 u32 ctrl_reg = in_be32(&regs->mdioctrl);
197 out_be32(&regs->mdioaddr, XEL_MDIOADDR_OP_MASK |
Michal Simek912145b2015-12-10 13:33:20 +0100198 ((phyaddress << XEL_MDIOADDR_PHYADR_SHIFT) | registernum));
Michal Simek905f0982015-12-10 14:18:15 +0100199 out_be32(&regs->mdioctrl, ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK);
Michal Simek912145b2015-12-10 13:33:20 +0100200
Michal Simek905f0982015-12-10 14:18:15 +0100201 if (mdio_wait(regs))
Michal Simek912145b2015-12-10 13:33:20 +0100202 return 1;
203
204 /* Read data */
Michal Simek905f0982015-12-10 14:18:15 +0100205 *data = in_be32(&regs->mdiord);
Michal Simek912145b2015-12-10 13:33:20 +0100206 return 0;
207}
208
Michal Simek905f0982015-12-10 14:18:15 +0100209static u32 phywrite(struct xemaclite *emaclite, u32 phyaddress, u32 registernum,
Michal Simek912145b2015-12-10 13:33:20 +0100210 u16 data)
211{
Michal Simek905f0982015-12-10 14:18:15 +0100212 struct emaclite_regs *regs = emaclite->regs;
213
214 if (mdio_wait(regs))
Michal Simek912145b2015-12-10 13:33:20 +0100215 return 1;
216
217 /*
218 * Write the PHY address, register number and clear the OP bit in the
219 * MDIO Address register and then write the value into the MDIO Write
220 * Data register. Finally, set the Status bit in the MDIO Control
221 * register to start a MDIO write transaction.
222 */
Michal Simek905f0982015-12-10 14:18:15 +0100223 u32 ctrl_reg = in_be32(&regs->mdioctrl);
224 out_be32(&regs->mdioaddr, ~XEL_MDIOADDR_OP_MASK &
Michal Simek912145b2015-12-10 13:33:20 +0100225 ((phyaddress << XEL_MDIOADDR_PHYADR_SHIFT) | registernum));
Michal Simek905f0982015-12-10 14:18:15 +0100226 out_be32(&regs->mdiowr, data);
227 out_be32(&regs->mdioctrl, ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK);
Michal Simek912145b2015-12-10 13:33:20 +0100228
Michal Simek905f0982015-12-10 14:18:15 +0100229 if (mdio_wait(regs))
Michal Simek912145b2015-12-10 13:33:20 +0100230 return 1;
231
232 return 0;
233}
234#endif
235
Michal Simekb4a1d082010-10-11 11:41:47 +1000236static void emaclite_halt(struct eth_device *dev)
Michal Simek4514b372008-03-28 12:41:56 +0100237{
Michal Simek5d1cf6c2011-09-12 21:10:05 +0000238 debug("eth_halt\n");
Michal Simek4514b372008-03-28 12:41:56 +0100239}
Michal Simek912145b2015-12-10 13:33:20 +0100240
241/* Use MII register 1 (MII status register) to detect PHY */
242#define PHY_DETECT_REG 1
243
244/* Mask used to verify certain PHY features (or register contents)
245 * in the register above:
246 * 0x1000: 10Mbps full duplex support
247 * 0x0800: 10Mbps half duplex support
248 * 0x0008: Auto-negotiation support
249 */
250#define PHY_DETECT_MASK 0x1808
251
252#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
253static int setup_phy(struct eth_device *dev)
254{
255 int i;
256 u16 phyreg;
257 struct xemaclite *emaclite = dev->priv;
258 struct phy_device *phydev;
259
260 u32 supported = SUPPORTED_10baseT_Half |
261 SUPPORTED_10baseT_Full |
262 SUPPORTED_100baseT_Half |
263 SUPPORTED_100baseT_Full;
264
265 if (emaclite->phyaddr != -1) {
Michal Simek905f0982015-12-10 14:18:15 +0100266 phyread(emaclite, emaclite->phyaddr, PHY_DETECT_REG, &phyreg);
Michal Simek912145b2015-12-10 13:33:20 +0100267 if ((phyreg != 0xFFFF) &&
268 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
269 /* Found a valid PHY address */
270 debug("Default phy address %d is valid\n",
271 emaclite->phyaddr);
272 } else {
273 debug("PHY address is not setup correctly %d\n",
274 emaclite->phyaddr);
275 emaclite->phyaddr = -1;
276 }
277 }
278
279 if (emaclite->phyaddr == -1) {
280 /* detect the PHY address */
281 for (i = 31; i >= 0; i--) {
Michal Simek905f0982015-12-10 14:18:15 +0100282 phyread(emaclite, i, PHY_DETECT_REG, &phyreg);
Michal Simek912145b2015-12-10 13:33:20 +0100283 if ((phyreg != 0xFFFF) &&
284 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
285 /* Found a valid PHY address */
286 emaclite->phyaddr = i;
287 debug("emaclite: Found valid phy address, %d\n",
288 i);
289 break;
290 }
291 }
292 }
293
294 /* interface - look at tsec */
295 phydev = phy_connect(emaclite->bus, emaclite->phyaddr, dev,
296 PHY_INTERFACE_MODE_MII);
297 /*
298 * Phy can support 1000baseT but device NOT that's why phydev->supported
299 * must be setup for 1000baseT. phydev->advertising setups what speeds
300 * will be used for autonegotiation where 1000baseT must be disabled.
301 */
302 phydev->supported = supported | SUPPORTED_1000baseT_Half |
303 SUPPORTED_1000baseT_Full;
304 phydev->advertising = supported;
305 emaclite->phydev = phydev;
306 phy_config(phydev);
307 phy_startup(phydev);
308
309 if (!phydev->link) {
310 printf("%s: No link.\n", phydev->dev->name);
311 return 0;
312 }
313
314 /* Do not setup anything */
315 return 1;
316}
317#endif
Michal Simek4514b372008-03-28 12:41:56 +0100318
Michal Simekb4a1d082010-10-11 11:41:47 +1000319static int emaclite_init(struct eth_device *dev, bd_t *bis)
Michal Simek4514b372008-03-28 12:41:56 +0100320{
Michal Simekdf40ead2011-09-12 21:10:01 +0000321 struct xemaclite *emaclite = dev->priv;
Michal Simek905f0982015-12-10 14:18:15 +0100322 struct emaclite_regs *regs = emaclite->regs;
323
Michal Simek5d1cf6c2011-09-12 21:10:05 +0000324 debug("EmacLite Initialization Started\n");
Michal Simek4514b372008-03-28 12:41:56 +0100325
326/*
327 * TX - TX_PING & TX_PONG initialization
328 */
329 /* Restart PING TX */
Michal Simek34240c42015-12-10 15:22:21 +0100330 out_be32(&regs->tx_ping_tsr, 0);
Michal Simek4514b372008-03-28 12:41:56 +0100331 /* Copy MAC address */
Michal Simek90e89bf2015-12-10 16:01:50 +0100332 xemaclite_alignedwrite(dev->enetaddr, &regs->tx_ping,
Michal Simek34240c42015-12-10 15:22:21 +0100333 ENET_ADDR_LENGTH);
Michal Simek4514b372008-03-28 12:41:56 +0100334 /* Set the length */
Michal Simek34240c42015-12-10 15:22:21 +0100335 out_be32(&regs->tx_ping_tplr, ENET_ADDR_LENGTH);
Michal Simek4514b372008-03-28 12:41:56 +0100336 /* Update the MAC address in the EMAC Lite */
Michal Simek34240c42015-12-10 15:22:21 +0100337 out_be32(&regs->tx_ping_tsr, XEL_TSR_PROG_MAC_ADDR);
Michal Simek4514b372008-03-28 12:41:56 +0100338 /* Wait for EMAC Lite to finish with the MAC address update */
Michal Simek34240c42015-12-10 15:22:21 +0100339 while ((in_be32 (&regs->tx_ping_tsr) &
Michal Simekac357ac2011-08-25 12:36:39 +0200340 XEL_TSR_PROG_MAC_ADDR) != 0)
341 ;
Michal Simek4514b372008-03-28 12:41:56 +0100342
Michal Simekdf40ead2011-09-12 21:10:01 +0000343 if (emaclite->txpp) {
344 /* The same operation with PONG TX */
Michal Simek34240c42015-12-10 15:22:21 +0100345 out_be32(&regs->tx_pong_tsr, 0);
Michal Simek90e89bf2015-12-10 16:01:50 +0100346 xemaclite_alignedwrite(dev->enetaddr, &regs->tx_pong,
Michal Simek34240c42015-12-10 15:22:21 +0100347 ENET_ADDR_LENGTH);
348 out_be32(&regs->tx_pong_tplr, ENET_ADDR_LENGTH);
349 out_be32(&regs->tx_pong_tsr, XEL_TSR_PROG_MAC_ADDR);
350 while ((in_be32(&regs->tx_pong_tsr) &
351 XEL_TSR_PROG_MAC_ADDR) != 0)
Michal Simekdf40ead2011-09-12 21:10:01 +0000352 ;
353 }
Michal Simek4514b372008-03-28 12:41:56 +0100354
355/*
356 * RX - RX_PING & RX_PONG initialization
357 */
358 /* Write out the value to flush the RX buffer */
Michal Simek9066cdb2015-12-10 15:24:23 +0100359 out_be32(&regs->rx_ping_rsr, XEL_RSR_RECV_IE_MASK);
Michal Simekdf40ead2011-09-12 21:10:01 +0000360
361 if (emaclite->rxpp)
Michal Simek9066cdb2015-12-10 15:24:23 +0100362 out_be32(&regs->rx_pong_rsr, XEL_RSR_RECV_IE_MASK);
Michal Simek4514b372008-03-28 12:41:56 +0100363
Michal Simek912145b2015-12-10 13:33:20 +0100364#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Michal Simek905f0982015-12-10 14:18:15 +0100365 out_be32(&regs->mdioctrl, XEL_MDIOCTRL_MDIOEN_MASK);
366 if (in_be32(&regs->mdioctrl) & XEL_MDIOCTRL_MDIOEN_MASK)
Michal Simek912145b2015-12-10 13:33:20 +0100367 if (!setup_phy(dev))
368 return -1;
369#endif
Michal Simek5d1cf6c2011-09-12 21:10:05 +0000370 debug("EmacLite Initialization complete\n");
Michal Simek4514b372008-03-28 12:41:56 +0100371 return 0;
372}
373
Michal Simek1edc6572015-12-10 15:42:01 +0100374static int xemaclite_txbufferavailable(struct xemaclite *emaclite)
Michal Simek4514b372008-03-28 12:41:56 +0100375{
Michal Simek1edc6572015-12-10 15:42:01 +0100376 u32 tmp;
377 struct emaclite_regs *regs = emaclite->regs;
Michal Simekf35b7cd2011-08-25 12:47:56 +0200378
Michal Simek4514b372008-03-28 12:41:56 +0100379 /*
380 * Read the other buffer register
381 * and determine if the other buffer is available
382 */
Michal Simek1edc6572015-12-10 15:42:01 +0100383 tmp = ~in_be32(&regs->tx_ping_tsr);
384 if (emaclite->txpp)
385 tmp |= ~in_be32(&regs->tx_pong_tsr);
Michal Simek4514b372008-03-28 12:41:56 +0100386
Michal Simek1edc6572015-12-10 15:42:01 +0100387 return !(tmp & XEL_TSR_XMIT_BUSY_MASK);
Michal Simek4514b372008-03-28 12:41:56 +0100388}
389
Stephan Linz76aeeb92012-05-22 12:18:10 +0000390static int emaclite_send(struct eth_device *dev, void *ptr, int len)
Michal Simekb4a1d082010-10-11 11:41:47 +1000391{
392 u32 reg;
Michal Simekf35b7cd2011-08-25 12:47:56 +0200393 struct xemaclite *emaclite = dev->priv;
Michal Simek9b9423b2015-12-10 15:32:11 +0100394 struct emaclite_regs *regs = emaclite->regs;
Michal Simek4514b372008-03-28 12:41:56 +0100395
Michal Simekb4a1d082010-10-11 11:41:47 +1000396 u32 maxtry = 1000;
Michal Simek4514b372008-03-28 12:41:56 +0100397
Michal Simek3aa96f82011-09-12 21:10:04 +0000398 if (len > PKTSIZE)
399 len = PKTSIZE;
Michal Simek4514b372008-03-28 12:41:56 +0100400
Michal Simek1edc6572015-12-10 15:42:01 +0100401 while (xemaclite_txbufferavailable(emaclite) && maxtry) {
Michal Simek5d1cf6c2011-09-12 21:10:05 +0000402 udelay(10);
Michal Simek4514b372008-03-28 12:41:56 +0100403 maxtry--;
404 }
405
406 if (!maxtry) {
Michal Simek5d1cf6c2011-09-12 21:10:05 +0000407 printf("Error: Timeout waiting for ethernet TX buffer\n");
Michal Simek4514b372008-03-28 12:41:56 +0100408 /* Restart PING TX */
Michal Simek9b9423b2015-12-10 15:32:11 +0100409 out_be32(&regs->tx_ping_tsr, 0);
Michal Simekdf40ead2011-09-12 21:10:01 +0000410 if (emaclite->txpp) {
Michal Simek9b9423b2015-12-10 15:32:11 +0100411 out_be32(&regs->tx_pong_tsr, 0);
Michal Simekdf40ead2011-09-12 21:10:01 +0000412 }
Michal Simek29869212011-03-08 04:25:53 +0000413 return -1;
Michal Simek4514b372008-03-28 12:41:56 +0100414 }
415
Michal Simek4514b372008-03-28 12:41:56 +0100416 /* Determine if the expected buffer address is empty */
Michal Simek90e89bf2015-12-10 16:01:50 +0100417 reg = in_be32(&regs->tx_ping_tsr);
Michal Simekd92cef42015-12-10 16:06:07 +0100418 if ((reg & XEL_TSR_XMIT_BUSY_MASK) == 0) {
Michal Simek90e89bf2015-12-10 16:01:50 +0100419 debug("Send packet from tx_ping buffer\n");
Michal Simek4514b372008-03-28 12:41:56 +0100420 /* Write the frame to the buffer */
Michal Simek90e89bf2015-12-10 16:01:50 +0100421 xemaclite_alignedwrite(ptr, &regs->tx_ping, len);
422 out_be32(&regs->tx_ping_tplr, len &
423 (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO));
424 reg = in_be32(&regs->tx_ping_tsr);
Michal Simek4514b372008-03-28 12:41:56 +0100425 reg |= XEL_TSR_XMIT_BUSY_MASK;
Michal Simek90e89bf2015-12-10 16:01:50 +0100426 out_be32(&regs->tx_ping_tsr, reg);
Michal Simek29869212011-03-08 04:25:53 +0000427 return 0;
Michal Simek4514b372008-03-28 12:41:56 +0100428 }
Michal Simekdf40ead2011-09-12 21:10:01 +0000429
430 if (emaclite->txpp) {
Michal Simekdf40ead2011-09-12 21:10:01 +0000431 /* Determine if the expected buffer address is empty */
Michal Simek90e89bf2015-12-10 16:01:50 +0100432 reg = in_be32(&regs->tx_pong_tsr);
Michal Simekd92cef42015-12-10 16:06:07 +0100433 if ((reg & XEL_TSR_XMIT_BUSY_MASK) == 0) {
Michal Simek90e89bf2015-12-10 16:01:50 +0100434 debug("Send packet from tx_pong buffer\n");
Michal Simekdf40ead2011-09-12 21:10:01 +0000435 /* Write the frame to the buffer */
Michal Simek90e89bf2015-12-10 16:01:50 +0100436 xemaclite_alignedwrite(ptr, &regs->tx_pong, len);
437 out_be32(&regs->tx_pong_tplr, len &
438 (XEL_TPLR_LENGTH_MASK_HI |
439 XEL_TPLR_LENGTH_MASK_LO));
440 reg = in_be32(&regs->tx_pong_tsr);
Michal Simekdf40ead2011-09-12 21:10:01 +0000441 reg |= XEL_TSR_XMIT_BUSY_MASK;
Michal Simek90e89bf2015-12-10 16:01:50 +0100442 out_be32(&regs->tx_pong_tsr, reg);
Michal Simekdf40ead2011-09-12 21:10:01 +0000443 return 0;
Michal Simek4514b372008-03-28 12:41:56 +0100444 }
Michal Simek4514b372008-03-28 12:41:56 +0100445 }
Michal Simekdf40ead2011-09-12 21:10:01 +0000446
Michal Simek5d1cf6c2011-09-12 21:10:05 +0000447 puts("Error while sending frame\n");
Michal Simek29869212011-03-08 04:25:53 +0000448 return -1;
Michal Simek4514b372008-03-28 12:41:56 +0100449}
450
Michal Simekb4a1d082010-10-11 11:41:47 +1000451static int emaclite_recv(struct eth_device *dev)
Michal Simek4514b372008-03-28 12:41:56 +0100452{
Michal Simek36f7a412015-12-10 16:31:38 +0100453 u32 length, first_read, reg, attempt = 0;
454 void *addr, *ack;
Michal Simekf35b7cd2011-08-25 12:47:56 +0200455 struct xemaclite *emaclite = dev->priv;
Michal Simek36f7a412015-12-10 16:31:38 +0100456 struct emaclite_regs *regs = emaclite->regs;
457 struct ethernet_hdr *eth;
458 struct ip_udp_hdr *ip;
Michal Simek4514b372008-03-28 12:41:56 +0100459
Michal Simek36f7a412015-12-10 16:31:38 +0100460try_again:
461 if (!emaclite->use_rx_pong_buffer_next) {
462 reg = in_be32(&regs->rx_ping_rsr);
463 debug("Testing data at rx_ping\n");
464 if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
465 debug("Data found in rx_ping buffer\n");
466 addr = &regs->rx_ping;
467 ack = &regs->rx_ping_rsr;
468 } else {
469 debug("Data not found in rx_ping buffer\n");
470 /* Pong buffer is not available - return immediately */
471 if (!emaclite->rxpp)
472 return -1;
Michal Simekdf40ead2011-09-12 21:10:01 +0000473
Michal Simek36f7a412015-12-10 16:31:38 +0100474 /* Try pong buffer if this is first attempt */
475 if (attempt++)
476 return -1;
477 emaclite->use_rx_pong_buffer_next =
478 !emaclite->use_rx_pong_buffer_next;
479 goto try_again;
480 }
481 } else {
482 reg = in_be32(&regs->rx_pong_rsr);
483 debug("Testing data at rx_pong\n");
484 if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
485 debug("Data found in rx_pong buffer\n");
486 addr = &regs->rx_pong;
487 ack = &regs->rx_pong_rsr;
Michal Simekdf40ead2011-09-12 21:10:01 +0000488 } else {
Michal Simek36f7a412015-12-10 16:31:38 +0100489 debug("Data not found in rx_pong buffer\n");
490 /* Try ping buffer if this is first attempt */
491 if (attempt++)
492 return -1;
493 emaclite->use_rx_pong_buffer_next =
494 !emaclite->use_rx_pong_buffer_next;
495 goto try_again;
Michal Simek4514b372008-03-28 12:41:56 +0100496 }
Michal Simek4514b372008-03-28 12:41:56 +0100497 }
Michal Simek36f7a412015-12-10 16:31:38 +0100498
499 /* Read all bytes for ARP packet with 32bit alignment - 48bytes */
500 first_read = ALIGN(ETHER_HDR_SIZE + ARP_HDR_SIZE + ETH_FCS_LEN, 4);
501 xemaclite_alignedread(addr, etherrxbuff, first_read);
502
503 /* Detect real packet size */
504 eth = (struct ethernet_hdr *)etherrxbuff;
505 switch (ntohs(eth->et_protlen)) {
506 case PROT_ARP:
507 length = first_read;
508 debug("ARP Packet %x\n", length);
509 break;
510 case PROT_IP:
511 ip = (struct ip_udp_hdr *)(etherrxbuff + ETHER_HDR_SIZE);
512 length = ntohs(ip->ip_len);
513 length += ETHER_HDR_SIZE + ETH_FCS_LEN;
514 debug("IP Packet %x\n", length);
515 break;
516 default:
517 debug("Other Packet\n");
518 length = PKTSIZE;
519 break;
Michal Simek4514b372008-03-28 12:41:56 +0100520 }
521
Michal Simek36f7a412015-12-10 16:31:38 +0100522 /* Read the rest of the packet which is longer then first read */
523 if (length != first_read)
524 xemaclite_alignedread(addr + first_read,
525 etherrxbuff + first_read,
526 length - first_read);
Michal Simek4514b372008-03-28 12:41:56 +0100527
528 /* Acknowledge the frame */
Michal Simek36f7a412015-12-10 16:31:38 +0100529 reg = in_be32(ack);
Michal Simek4514b372008-03-28 12:41:56 +0100530 reg &= ~XEL_RSR_RECV_DONE_MASK;
Michal Simek36f7a412015-12-10 16:31:38 +0100531 out_be32(ack, reg);
Michal Simek4514b372008-03-28 12:41:56 +0100532
Michal Simek36f7a412015-12-10 16:31:38 +0100533 debug("Packet receive from 0x%p, length %dB\n", addr, length);
Joe Hershberger9f09a362015-04-08 01:41:06 -0500534 net_process_received_packet((uchar *)etherrxbuff, length);
Michal Simek29869212011-03-08 04:25:53 +0000535 return length;
Michal Simek4514b372008-03-28 12:41:56 +0100536
Michal Simek912145b2015-12-10 13:33:20 +0100537}
538
539#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
540static int emaclite_miiphy_read(const char *devname, uchar addr,
541 uchar reg, ushort *val)
542{
543 u32 ret;
544 struct eth_device *dev = eth_get_dev();
545
Michal Simek905f0982015-12-10 14:18:15 +0100546 ret = phyread(dev->priv, addr, reg, val);
Michal Simek912145b2015-12-10 13:33:20 +0100547 debug("emaclite: Read MII 0x%x, 0x%x, 0x%x\n", addr, reg, *val);
548 return ret;
Michal Simek4514b372008-03-28 12:41:56 +0100549}
Michal Simekb4a1d082010-10-11 11:41:47 +1000550
Michal Simek912145b2015-12-10 13:33:20 +0100551static int emaclite_miiphy_write(const char *devname, uchar addr,
552 uchar reg, ushort val)
553{
554 struct eth_device *dev = eth_get_dev();
555
556 debug("emaclite: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, val);
Michal Simek905f0982015-12-10 14:18:15 +0100557 return phywrite(dev->priv, addr, reg, val);
Michal Simek912145b2015-12-10 13:33:20 +0100558}
559#endif
560
Michal Simeka6745b82011-10-12 23:23:22 +0000561int xilinx_emaclite_initialize(bd_t *bis, unsigned long base_addr,
562 int txpp, int rxpp)
Michal Simekb4a1d082010-10-11 11:41:47 +1000563{
564 struct eth_device *dev;
Michal Simekf35b7cd2011-08-25 12:47:56 +0200565 struct xemaclite *emaclite;
Michal Simek905f0982015-12-10 14:18:15 +0100566 struct emaclite_regs *regs;
Michal Simekb4a1d082010-10-11 11:41:47 +1000567
Michal Simek8f2bf362011-08-25 12:28:47 +0200568 dev = calloc(1, sizeof(*dev));
Michal Simekb4a1d082010-10-11 11:41:47 +1000569 if (dev == NULL)
Michal Simek29869212011-03-08 04:25:53 +0000570 return -1;
Michal Simekf35b7cd2011-08-25 12:47:56 +0200571
572 emaclite = calloc(1, sizeof(struct xemaclite));
573 if (emaclite == NULL) {
574 free(dev);
575 return -1;
576 }
577
578 dev->priv = emaclite;
Michal Simekb4a1d082010-10-11 11:41:47 +1000579
Michal Simeka6745b82011-10-12 23:23:22 +0000580 emaclite->txpp = txpp;
581 emaclite->rxpp = rxpp;
Michal Simekdf40ead2011-09-12 21:10:01 +0000582
Michal Simekc4336552011-10-12 23:23:21 +0000583 sprintf(dev->name, "Xelite.%lx", base_addr);
Michal Simekb4a1d082010-10-11 11:41:47 +1000584
Michal Simek905f0982015-12-10 14:18:15 +0100585 emaclite->regs = (struct emaclite_regs *)base_addr;
586 regs = emaclite->regs;
Michal Simekb4a1d082010-10-11 11:41:47 +1000587 dev->iobase = base_addr;
Michal Simekb4a1d082010-10-11 11:41:47 +1000588 dev->init = emaclite_init;
589 dev->halt = emaclite_halt;
590 dev->send = emaclite_send;
591 dev->recv = emaclite_recv;
592
Michal Simek912145b2015-12-10 13:33:20 +0100593#ifdef CONFIG_PHY_ADDR
594 emaclite->phyaddr = CONFIG_PHY_ADDR;
595#else
596 emaclite->phyaddr = -1;
597#endif
598
Michal Simekb4a1d082010-10-11 11:41:47 +1000599 eth_register(dev);
600
Michal Simek912145b2015-12-10 13:33:20 +0100601#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
602 miiphy_register(dev->name, emaclite_miiphy_read, emaclite_miiphy_write);
603 emaclite->bus = miiphy_get_dev_by_name(dev->name);
604
Michal Simek905f0982015-12-10 14:18:15 +0100605 out_be32(&regs->mdioctrl, XEL_MDIOCTRL_MDIOEN_MASK);
Michal Simek912145b2015-12-10 13:33:20 +0100606#endif
607
Michal Simek29869212011-03-08 04:25:53 +0000608 return 1;
Michal Simekb4a1d082010-10-11 11:41:47 +1000609}