blob: 4b49290f0e4efb36329168b946b9e8d99d2d949d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek78d19a32009-09-07 09:08:02 +02002/*
3 * (C) Copyright 2007-2009 Michal Simek
4 * (C) Copyright 2003 Xilinx Inc.
Michal Simek89c53892008-03-28 12:41:56 +01005 *
Michal Simek89c53892008-03-28 12:41:56 +01006 * Michal SIMEK <monstr@monstr.eu>
Michal Simek78d19a32009-09-07 09:08:02 +02007 */
Michal Simek89c53892008-03-28 12:41:56 +01008
9#include <common.h>
10#include <net.h>
11#include <config.h>
Michal Simekd538ee12015-12-10 17:15:52 +010012#include <dm.h>
Michal Simekd722e862015-12-10 13:33:20 +010013#include <console.h>
Michal Simek042272a2010-10-11 11:41:47 +100014#include <malloc.h>
Michal Simek89c53892008-03-28 12:41:56 +010015#include <asm/io.h>
Michal Simekd722e862015-12-10 13:33:20 +010016#include <phy.h>
17#include <miiphy.h>
Michal Simek7fd70822012-06-28 21:37:57 +000018#include <fdtdec.h>
Masahiro Yamada5d97dff2016-09-21 11:28:57 +090019#include <linux/errno.h>
Michal Simek4d2749b2015-12-10 16:31:38 +010020#include <linux/kernel.h>
Zubair Lutfullah Kakakhel39e020e2016-07-27 12:25:07 +010021#include <asm/io.h>
Michal Simek7fd70822012-06-28 21:37:57 +000022
Michal Simekd538ee12015-12-10 17:15:52 +010023DECLARE_GLOBAL_DATA_PTR;
Michal Simek89c53892008-03-28 12:41:56 +010024
Michal Simek89c53892008-03-28 12:41:56 +010025#define ENET_ADDR_LENGTH 6
Michal Simek4d2749b2015-12-10 16:31:38 +010026#define ETH_FCS_LEN 4 /* Octets in the FCS */
Michal Simek89c53892008-03-28 12:41:56 +010027
28/* Xmit complete */
29#define XEL_TSR_XMIT_BUSY_MASK 0x00000001UL
30/* Xmit interrupt enable bit */
31#define XEL_TSR_XMIT_IE_MASK 0x00000008UL
Michal Simek89c53892008-03-28 12:41:56 +010032/* Program the MAC address */
33#define XEL_TSR_PROGRAM_MASK 0x00000002UL
34/* define for programming the MAC address into the EMAC Lite */
35#define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
36
37/* Transmit packet length upper byte */
38#define XEL_TPLR_LENGTH_MASK_HI 0x0000FF00UL
39/* Transmit packet length lower byte */
40#define XEL_TPLR_LENGTH_MASK_LO 0x000000FFUL
41
42/* Recv complete */
43#define XEL_RSR_RECV_DONE_MASK 0x00000001UL
44/* Recv interrupt enable bit */
45#define XEL_RSR_RECV_IE_MASK 0x00000008UL
46
Michal Simekd722e862015-12-10 13:33:20 +010047/* MDIO Address Register Bit Masks */
48#define XEL_MDIOADDR_REGADR_MASK 0x0000001F /* Register Address */
49#define XEL_MDIOADDR_PHYADR_MASK 0x000003E0 /* PHY Address */
50#define XEL_MDIOADDR_PHYADR_SHIFT 5
51#define XEL_MDIOADDR_OP_MASK 0x00000400 /* RD/WR Operation */
52
53/* MDIO Write Data Register Bit Masks */
54#define XEL_MDIOWR_WRDATA_MASK 0x0000FFFF /* Data to be Written */
55
56/* MDIO Read Data Register Bit Masks */
57#define XEL_MDIORD_RDDATA_MASK 0x0000FFFF /* Data to be Read */
58
59/* MDIO Control Register Bit Masks */
60#define XEL_MDIOCTRL_MDIOSTS_MASK 0x00000001 /* MDIO Status Mask */
61#define XEL_MDIOCTRL_MDIOEN_MASK 0x00000008 /* MDIO Enable */
62
Michal Simek9a23c492015-12-10 14:18:15 +010063struct emaclite_regs {
64 u32 tx_ping; /* 0x0 - TX Ping buffer */
65 u32 reserved1[504];
66 u32 mdioaddr; /* 0x7e4 - MDIO Address Register */
67 u32 mdiowr; /* 0x7e8 - MDIO Write Data Register */
68 u32 mdiord;/* 0x7ec - MDIO Read Data Register */
69 u32 mdioctrl; /* 0x7f0 - MDIO Control Register */
70 u32 tx_ping_tplr; /* 0x7f4 - Tx packet length */
71 u32 global_interrupt; /* 0x7f8 - Global interrupt enable */
72 u32 tx_ping_tsr; /* 0x7fc - Tx status */
73 u32 tx_pong; /* 0x800 - TX Pong buffer */
74 u32 reserved2[508];
75 u32 tx_pong_tplr; /* 0xff4 - Tx packet length */
76 u32 reserved3; /* 0xff8 */
77 u32 tx_pong_tsr; /* 0xffc - Tx status */
78 u32 rx_ping; /* 0x1000 - Receive Buffer */
79 u32 reserved4[510];
80 u32 rx_ping_rsr; /* 0x17fc - Rx status */
81 u32 rx_pong; /* 0x1800 - Receive Buffer */
82 u32 reserved5[510];
83 u32 rx_pong_rsr; /* 0x1ffc - Rx status */
84};
85
Michal Simek773cfa82011-08-25 12:47:56 +020086struct xemaclite {
Michal Simek4d2749b2015-12-10 16:31:38 +010087 bool use_rx_pong_buffer_next; /* Next RX buffer to read from */
Michal Simek947324b2011-09-12 21:10:01 +000088 u32 txpp; /* TX ping pong buffer */
89 u32 rxpp; /* RX ping pong buffer */
Michal Simekd722e862015-12-10 13:33:20 +010090 int phyaddr;
Michal Simek9a23c492015-12-10 14:18:15 +010091 struct emaclite_regs *regs;
Michal Simekd722e862015-12-10 13:33:20 +010092 struct phy_device *phydev;
93 struct mii_dev *bus;
Michal Simek773cfa82011-08-25 12:47:56 +020094};
Michal Simek89c53892008-03-28 12:41:56 +010095
Michal Simekf412b6a2015-12-16 10:52:39 +010096static uchar etherrxbuff[PKTSIZE_ALIGN]; /* Receive buffer */
Michal Simek89c53892008-03-28 12:41:56 +010097
Michal Simek5ac83802011-09-12 21:10:05 +000098static void xemaclite_alignedread(u32 *srcptr, void *destptr, u32 bytecount)
Michal Simek89c53892008-03-28 12:41:56 +010099{
Michal Simek042272a2010-10-11 11:41:47 +1000100 u32 i;
Michal Simek89c53892008-03-28 12:41:56 +0100101 u32 alignbuffer;
102 u32 *to32ptr;
103 u32 *from32ptr;
104 u8 *to8ptr;
105 u8 *from8ptr;
106
107 from32ptr = (u32 *) srcptr;
108
109 /* Word aligned buffer, no correction needed. */
110 to32ptr = (u32 *) destptr;
111 while (bytecount > 3) {
112 *to32ptr++ = *from32ptr++;
113 bytecount -= 4;
114 }
115 to8ptr = (u8 *) to32ptr;
116
117 alignbuffer = *from32ptr++;
Michal Simek5ac83802011-09-12 21:10:05 +0000118 from8ptr = (u8 *) &alignbuffer;
Michal Simek89c53892008-03-28 12:41:56 +0100119
Michal Simek5ac83802011-09-12 21:10:05 +0000120 for (i = 0; i < bytecount; i++)
Michal Simek89c53892008-03-28 12:41:56 +0100121 *to8ptr++ = *from8ptr++;
Michal Simek89c53892008-03-28 12:41:56 +0100122}
123
Michal Simek00702512015-12-10 16:01:50 +0100124static void xemaclite_alignedwrite(void *srcptr, u32 *destptr, u32 bytecount)
Michal Simek89c53892008-03-28 12:41:56 +0100125{
Michal Simek042272a2010-10-11 11:41:47 +1000126 u32 i;
Michal Simek89c53892008-03-28 12:41:56 +0100127 u32 alignbuffer;
128 u32 *to32ptr = (u32 *) destptr;
129 u32 *from32ptr;
130 u8 *to8ptr;
131 u8 *from8ptr;
132
133 from32ptr = (u32 *) srcptr;
134 while (bytecount > 3) {
135
136 *to32ptr++ = *from32ptr++;
137 bytecount -= 4;
138 }
139
140 alignbuffer = 0;
Michal Simek5ac83802011-09-12 21:10:05 +0000141 to8ptr = (u8 *) &alignbuffer;
Michal Simek89c53892008-03-28 12:41:56 +0100142 from8ptr = (u8 *) from32ptr;
143
Michal Simek5ac83802011-09-12 21:10:05 +0000144 for (i = 0; i < bytecount; i++)
Michal Simek89c53892008-03-28 12:41:56 +0100145 *to8ptr++ = *from8ptr++;
Michal Simek89c53892008-03-28 12:41:56 +0100146
147 *to32ptr++ = alignbuffer;
148}
149
Michal Simekd722e862015-12-10 13:33:20 +0100150static 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) {
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100157 val = __raw_readl(reg);
Michal Simekd722e862015-12-10 13:33:20 +0100158
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 Simek9a23c492015-12-10 14:18:15 +0100182static int mdio_wait(struct emaclite_regs *regs)
Michal Simekd722e862015-12-10 13:33:20 +0100183{
Michal Simek9a23c492015-12-10 14:18:15 +0100184 return wait_for_bit(__func__, &regs->mdioctrl,
Michal Simekd722e862015-12-10 13:33:20 +0100185 XEL_MDIOCTRL_MDIOSTS_MASK, false, 2000);
186}
187
Michal Simek9a23c492015-12-10 14:18:15 +0100188static u32 phyread(struct xemaclite *emaclite, u32 phyaddress, u32 registernum,
Michal Simekd722e862015-12-10 13:33:20 +0100189 u16 *data)
190{
Michal Simek9a23c492015-12-10 14:18:15 +0100191 struct emaclite_regs *regs = emaclite->regs;
192
193 if (mdio_wait(regs))
Michal Simekd722e862015-12-10 13:33:20 +0100194 return 1;
195
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100196 u32 ctrl_reg = __raw_readl(&regs->mdioctrl);
197 __raw_writel(XEL_MDIOADDR_OP_MASK
198 | ((phyaddress << XEL_MDIOADDR_PHYADR_SHIFT)
199 | registernum), &regs->mdioaddr);
200 __raw_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK, &regs->mdioctrl);
Michal Simekd722e862015-12-10 13:33:20 +0100201
Michal Simek9a23c492015-12-10 14:18:15 +0100202 if (mdio_wait(regs))
Michal Simekd722e862015-12-10 13:33:20 +0100203 return 1;
204
205 /* Read data */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100206 *data = __raw_readl(&regs->mdiord);
Michal Simekd722e862015-12-10 13:33:20 +0100207 return 0;
208}
209
Michal Simek9a23c492015-12-10 14:18:15 +0100210static u32 phywrite(struct xemaclite *emaclite, u32 phyaddress, u32 registernum,
Michal Simekd722e862015-12-10 13:33:20 +0100211 u16 data)
212{
Michal Simek9a23c492015-12-10 14:18:15 +0100213 struct emaclite_regs *regs = emaclite->regs;
214
215 if (mdio_wait(regs))
Michal Simekd722e862015-12-10 13:33:20 +0100216 return 1;
217
218 /*
219 * Write the PHY address, register number and clear the OP bit in the
220 * MDIO Address register and then write the value into the MDIO Write
221 * Data register. Finally, set the Status bit in the MDIO Control
222 * register to start a MDIO write transaction.
223 */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100224 u32 ctrl_reg = __raw_readl(&regs->mdioctrl);
225 __raw_writel(~XEL_MDIOADDR_OP_MASK
226 & ((phyaddress << XEL_MDIOADDR_PHYADR_SHIFT)
227 | registernum), &regs->mdioaddr);
228 __raw_writel(data, &regs->mdiowr);
229 __raw_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK, &regs->mdioctrl);
Michal Simekd722e862015-12-10 13:33:20 +0100230
Michal Simek9a23c492015-12-10 14:18:15 +0100231 if (mdio_wait(regs))
Michal Simekd722e862015-12-10 13:33:20 +0100232 return 1;
233
234 return 0;
235}
Michal Simekd722e862015-12-10 13:33:20 +0100236
Michal Simekf03ec012015-12-16 10:40:05 +0100237static void emaclite_stop(struct udevice *dev)
Michal Simek89c53892008-03-28 12:41:56 +0100238{
Michal Simekf03ec012015-12-16 10:40:05 +0100239 debug("eth_stop\n");
Michal Simek89c53892008-03-28 12:41:56 +0100240}
241
Michal Simekd722e862015-12-10 13:33:20 +0100242/* Use MII register 1 (MII status register) to detect PHY */
243#define PHY_DETECT_REG 1
244
245/* Mask used to verify certain PHY features (or register contents)
246 * in the register above:
247 * 0x1000: 10Mbps full duplex support
248 * 0x0800: 10Mbps half duplex support
249 * 0x0008: Auto-negotiation support
250 */
251#define PHY_DETECT_MASK 0x1808
252
Michal Simekd538ee12015-12-10 17:15:52 +0100253static int setup_phy(struct udevice *dev)
Michal Simekd722e862015-12-10 13:33:20 +0100254{
Michal Simek55259e72016-05-18 12:37:22 +0200255 int i, ret;
Michal Simekd722e862015-12-10 13:33:20 +0100256 u16 phyreg;
Michal Simekd538ee12015-12-10 17:15:52 +0100257 struct xemaclite *emaclite = dev_get_priv(dev);
Michal Simekd722e862015-12-10 13:33:20 +0100258 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 Simek9a23c492015-12-10 14:18:15 +0100266 phyread(emaclite, emaclite->phyaddr, PHY_DETECT_REG, &phyreg);
Michal Simekd722e862015-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 Simek9a23c492015-12-10 14:18:15 +0100282 phyread(emaclite, i, PHY_DETECT_REG, &phyreg);
Michal Simekd722e862015-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);
Michal Simek55259e72016-05-18 12:37:22 +0200307 ret = phy_startup(phydev);
308 if (ret)
309 return ret;
Michal Simekd722e862015-12-10 13:33:20 +0100310
311 if (!phydev->link) {
312 printf("%s: No link.\n", phydev->dev->name);
313 return 0;
314 }
315
316 /* Do not setup anything */
317 return 1;
318}
Michal Simekd722e862015-12-10 13:33:20 +0100319
Michal Simekf03ec012015-12-16 10:40:05 +0100320static int emaclite_start(struct udevice *dev)
Michal Simek89c53892008-03-28 12:41:56 +0100321{
Michal Simekd538ee12015-12-10 17:15:52 +0100322 struct xemaclite *emaclite = dev_get_priv(dev);
323 struct eth_pdata *pdata = dev_get_platdata(dev);
Michal Simek9a23c492015-12-10 14:18:15 +0100324 struct emaclite_regs *regs = emaclite->regs;
325
Michal Simek5ac83802011-09-12 21:10:05 +0000326 debug("EmacLite Initialization Started\n");
Michal Simek89c53892008-03-28 12:41:56 +0100327
328/*
329 * TX - TX_PING & TX_PONG initialization
330 */
331 /* Restart PING TX */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100332 __raw_writel(0, &regs->tx_ping_tsr);
Michal Simek89c53892008-03-28 12:41:56 +0100333 /* Copy MAC address */
Michal Simekd538ee12015-12-10 17:15:52 +0100334 xemaclite_alignedwrite(pdata->enetaddr, &regs->tx_ping,
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100335 ENET_ADDR_LENGTH);
Michal Simek89c53892008-03-28 12:41:56 +0100336 /* Set the length */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100337 __raw_writel(ENET_ADDR_LENGTH, &regs->tx_ping_tplr);
Michal Simek89c53892008-03-28 12:41:56 +0100338 /* Update the MAC address in the EMAC Lite */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100339 __raw_writel(XEL_TSR_PROG_MAC_ADDR, &regs->tx_ping_tsr);
Michal Simek89c53892008-03-28 12:41:56 +0100340 /* Wait for EMAC Lite to finish with the MAC address update */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100341 while ((__raw_readl(&regs->tx_ping_tsr) &
Michal Simek8d95ddb2011-08-25 12:36:39 +0200342 XEL_TSR_PROG_MAC_ADDR) != 0)
343 ;
Michal Simek89c53892008-03-28 12:41:56 +0100344
Michal Simek947324b2011-09-12 21:10:01 +0000345 if (emaclite->txpp) {
346 /* The same operation with PONG TX */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100347 __raw_writel(0, &regs->tx_pong_tsr);
Michal Simekd538ee12015-12-10 17:15:52 +0100348 xemaclite_alignedwrite(pdata->enetaddr, &regs->tx_pong,
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100349 ENET_ADDR_LENGTH);
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100350 __raw_writel(ENET_ADDR_LENGTH, &regs->tx_pong_tplr);
351 __raw_writel(XEL_TSR_PROG_MAC_ADDR, &regs->tx_pong_tsr);
352 while ((__raw_readl(&regs->tx_pong_tsr) &
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100353 XEL_TSR_PROG_MAC_ADDR) != 0)
Michal Simek947324b2011-09-12 21:10:01 +0000354 ;
355 }
Michal Simek89c53892008-03-28 12:41:56 +0100356
357/*
358 * RX - RX_PING & RX_PONG initialization
359 */
360 /* Write out the value to flush the RX buffer */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100361 __raw_writel(XEL_RSR_RECV_IE_MASK, &regs->rx_ping_rsr);
Michal Simek947324b2011-09-12 21:10:01 +0000362
363 if (emaclite->rxpp)
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100364 __raw_writel(XEL_RSR_RECV_IE_MASK, &regs->rx_pong_rsr);
Michal Simek89c53892008-03-28 12:41:56 +0100365
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100366 __raw_writel(XEL_MDIOCTRL_MDIOEN_MASK, &regs->mdioctrl);
367 if (__raw_readl(&regs->mdioctrl) & XEL_MDIOCTRL_MDIOEN_MASK)
Michal Simekd722e862015-12-10 13:33:20 +0100368 if (!setup_phy(dev))
369 return -1;
Michal Simekd538ee12015-12-10 17:15:52 +0100370
Michal Simek5ac83802011-09-12 21:10:05 +0000371 debug("EmacLite Initialization complete\n");
Michal Simek89c53892008-03-28 12:41:56 +0100372 return 0;
373}
374
Michal Simek26c79452015-12-10 15:42:01 +0100375static int xemaclite_txbufferavailable(struct xemaclite *emaclite)
Michal Simek89c53892008-03-28 12:41:56 +0100376{
Michal Simek26c79452015-12-10 15:42:01 +0100377 u32 tmp;
378 struct emaclite_regs *regs = emaclite->regs;
Michal Simek773cfa82011-08-25 12:47:56 +0200379
Michal Simek89c53892008-03-28 12:41:56 +0100380 /*
381 * Read the other buffer register
382 * and determine if the other buffer is available
383 */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100384 tmp = ~__raw_readl(&regs->tx_ping_tsr);
Michal Simek26c79452015-12-10 15:42:01 +0100385 if (emaclite->txpp)
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100386 tmp |= ~__raw_readl(&regs->tx_pong_tsr);
Michal Simek89c53892008-03-28 12:41:56 +0100387
Michal Simek26c79452015-12-10 15:42:01 +0100388 return !(tmp & XEL_TSR_XMIT_BUSY_MASK);
Michal Simek89c53892008-03-28 12:41:56 +0100389}
390
Michal Simekd538ee12015-12-10 17:15:52 +0100391static int emaclite_send(struct udevice *dev, void *ptr, int len)
Michal Simek042272a2010-10-11 11:41:47 +1000392{
393 u32 reg;
Michal Simekd538ee12015-12-10 17:15:52 +0100394 struct xemaclite *emaclite = dev_get_priv(dev);
Michal Simek5a4baa32015-12-10 15:32:11 +0100395 struct emaclite_regs *regs = emaclite->regs;
Michal Simek89c53892008-03-28 12:41:56 +0100396
Michal Simek042272a2010-10-11 11:41:47 +1000397 u32 maxtry = 1000;
Michal Simek89c53892008-03-28 12:41:56 +0100398
Michal Simek80439252011-09-12 21:10:04 +0000399 if (len > PKTSIZE)
400 len = PKTSIZE;
Michal Simek89c53892008-03-28 12:41:56 +0100401
Michal Simek26c79452015-12-10 15:42:01 +0100402 while (xemaclite_txbufferavailable(emaclite) && maxtry) {
Michal Simek5ac83802011-09-12 21:10:05 +0000403 udelay(10);
Michal Simek89c53892008-03-28 12:41:56 +0100404 maxtry--;
405 }
406
407 if (!maxtry) {
Michal Simek5ac83802011-09-12 21:10:05 +0000408 printf("Error: Timeout waiting for ethernet TX buffer\n");
Michal Simek89c53892008-03-28 12:41:56 +0100409 /* Restart PING TX */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100410 __raw_writel(0, &regs->tx_ping_tsr);
Michal Simek947324b2011-09-12 21:10:01 +0000411 if (emaclite->txpp) {
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100412 __raw_writel(0, &regs->tx_pong_tsr);
Michal Simek947324b2011-09-12 21:10:01 +0000413 }
Michal Simek95efa792011-03-08 04:25:53 +0000414 return -1;
Michal Simek89c53892008-03-28 12:41:56 +0100415 }
416
Michal Simek89c53892008-03-28 12:41:56 +0100417 /* Determine if the expected buffer address is empty */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100418 reg = __raw_readl(&regs->tx_ping_tsr);
Michal Simek15c239c2015-12-10 16:06:07 +0100419 if ((reg & XEL_TSR_XMIT_BUSY_MASK) == 0) {
Michal Simek00702512015-12-10 16:01:50 +0100420 debug("Send packet from tx_ping buffer\n");
Michal Simek89c53892008-03-28 12:41:56 +0100421 /* Write the frame to the buffer */
Michal Simek00702512015-12-10 16:01:50 +0100422 xemaclite_alignedwrite(ptr, &regs->tx_ping, len);
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100423 __raw_writel(len
424 & (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO),
425 &regs->tx_ping_tplr);
426 reg = __raw_readl(&regs->tx_ping_tsr);
Michal Simek89c53892008-03-28 12:41:56 +0100427 reg |= XEL_TSR_XMIT_BUSY_MASK;
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100428 __raw_writel(reg, &regs->tx_ping_tsr);
Michal Simek95efa792011-03-08 04:25:53 +0000429 return 0;
Michal Simek89c53892008-03-28 12:41:56 +0100430 }
Michal Simek947324b2011-09-12 21:10:01 +0000431
432 if (emaclite->txpp) {
Michal Simek947324b2011-09-12 21:10:01 +0000433 /* Determine if the expected buffer address is empty */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100434 reg = __raw_readl(&regs->tx_pong_tsr);
Michal Simek15c239c2015-12-10 16:06:07 +0100435 if ((reg & XEL_TSR_XMIT_BUSY_MASK) == 0) {
Michal Simek00702512015-12-10 16:01:50 +0100436 debug("Send packet from tx_pong buffer\n");
Michal Simek947324b2011-09-12 21:10:01 +0000437 /* Write the frame to the buffer */
Michal Simek00702512015-12-10 16:01:50 +0100438 xemaclite_alignedwrite(ptr, &regs->tx_pong, len);
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100439 __raw_writel(len &
Michal Simek00702512015-12-10 16:01:50 +0100440 (XEL_TPLR_LENGTH_MASK_HI |
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100441 XEL_TPLR_LENGTH_MASK_LO),
442 &regs->tx_pong_tplr);
443 reg = __raw_readl(&regs->tx_pong_tsr);
Michal Simek947324b2011-09-12 21:10:01 +0000444 reg |= XEL_TSR_XMIT_BUSY_MASK;
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100445 __raw_writel(reg, &regs->tx_pong_tsr);
Michal Simek947324b2011-09-12 21:10:01 +0000446 return 0;
Michal Simek89c53892008-03-28 12:41:56 +0100447 }
Michal Simek89c53892008-03-28 12:41:56 +0100448 }
Michal Simek947324b2011-09-12 21:10:01 +0000449
Michal Simek5ac83802011-09-12 21:10:05 +0000450 puts("Error while sending frame\n");
Michal Simek95efa792011-03-08 04:25:53 +0000451 return -1;
Michal Simek89c53892008-03-28 12:41:56 +0100452}
453
Michal Simekd538ee12015-12-10 17:15:52 +0100454static int emaclite_recv(struct udevice *dev, int flags, uchar **packetp)
Michal Simek89c53892008-03-28 12:41:56 +0100455{
Michal Simek4d2749b2015-12-10 16:31:38 +0100456 u32 length, first_read, reg, attempt = 0;
457 void *addr, *ack;
Michal Simek773cfa82011-08-25 12:47:56 +0200458 struct xemaclite *emaclite = dev->priv;
Michal Simek4d2749b2015-12-10 16:31:38 +0100459 struct emaclite_regs *regs = emaclite->regs;
460 struct ethernet_hdr *eth;
461 struct ip_udp_hdr *ip;
Michal Simek89c53892008-03-28 12:41:56 +0100462
Michal Simek4d2749b2015-12-10 16:31:38 +0100463try_again:
464 if (!emaclite->use_rx_pong_buffer_next) {
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100465 reg = __raw_readl(&regs->rx_ping_rsr);
Michal Simek4d2749b2015-12-10 16:31:38 +0100466 debug("Testing data at rx_ping\n");
467 if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
468 debug("Data found in rx_ping buffer\n");
469 addr = &regs->rx_ping;
470 ack = &regs->rx_ping_rsr;
Michal Simek947324b2011-09-12 21:10:01 +0000471 } else {
Michal Simek4d2749b2015-12-10 16:31:38 +0100472 debug("Data not found in rx_ping buffer\n");
473 /* Pong buffer is not available - return immediately */
474 if (!emaclite->rxpp)
475 return -1;
476
477 /* Try pong buffer if this is first attempt */
478 if (attempt++)
479 return -1;
480 emaclite->use_rx_pong_buffer_next =
481 !emaclite->use_rx_pong_buffer_next;
482 goto try_again;
483 }
484 } else {
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100485 reg = __raw_readl(&regs->rx_pong_rsr);
Michal Simek4d2749b2015-12-10 16:31:38 +0100486 debug("Testing data at rx_pong\n");
487 if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
488 debug("Data found in rx_pong buffer\n");
489 addr = &regs->rx_pong;
490 ack = &regs->rx_pong_rsr;
491 } else {
492 debug("Data not found in rx_pong buffer\n");
493 /* Try ping buffer if this is first attempt */
494 if (attempt++)
495 return -1;
496 emaclite->use_rx_pong_buffer_next =
497 !emaclite->use_rx_pong_buffer_next;
498 goto try_again;
Michal Simek89c53892008-03-28 12:41:56 +0100499 }
Michal Simek89c53892008-03-28 12:41:56 +0100500 }
Michal Simek4d2749b2015-12-10 16:31:38 +0100501
502 /* Read all bytes for ARP packet with 32bit alignment - 48bytes */
503 first_read = ALIGN(ETHER_HDR_SIZE + ARP_HDR_SIZE + ETH_FCS_LEN, 4);
504 xemaclite_alignedread(addr, etherrxbuff, first_read);
505
506 /* Detect real packet size */
507 eth = (struct ethernet_hdr *)etherrxbuff;
508 switch (ntohs(eth->et_protlen)) {
509 case PROT_ARP:
510 length = first_read;
511 debug("ARP Packet %x\n", length);
512 break;
513 case PROT_IP:
514 ip = (struct ip_udp_hdr *)(etherrxbuff + ETHER_HDR_SIZE);
515 length = ntohs(ip->ip_len);
516 length += ETHER_HDR_SIZE + ETH_FCS_LEN;
517 debug("IP Packet %x\n", length);
518 break;
519 default:
520 debug("Other Packet\n");
521 length = PKTSIZE;
522 break;
Michal Simek89c53892008-03-28 12:41:56 +0100523 }
524
Michal Simek4d2749b2015-12-10 16:31:38 +0100525 /* Read the rest of the packet which is longer then first read */
526 if (length != first_read)
527 xemaclite_alignedread(addr + first_read,
528 etherrxbuff + first_read,
529 length - first_read);
Michal Simek89c53892008-03-28 12:41:56 +0100530
531 /* Acknowledge the frame */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100532 reg = __raw_readl(ack);
Michal Simek89c53892008-03-28 12:41:56 +0100533 reg &= ~XEL_RSR_RECV_DONE_MASK;
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100534 __raw_writel(reg, ack);
Michal Simek89c53892008-03-28 12:41:56 +0100535
Michal Simek4d2749b2015-12-10 16:31:38 +0100536 debug("Packet receive from 0x%p, length %dB\n", addr, length);
Michal Simekf412b6a2015-12-16 10:52:39 +0100537 *packetp = etherrxbuff;
538 return length;
Michal Simek89c53892008-03-28 12:41:56 +0100539}
Michal Simek042272a2010-10-11 11:41:47 +1000540
Michal Simekd538ee12015-12-10 17:15:52 +0100541static int emaclite_miiphy_read(struct mii_dev *bus, int addr,
542 int devad, int reg)
Michal Simekd722e862015-12-10 13:33:20 +0100543{
544 u32 ret;
Michal Simekd538ee12015-12-10 17:15:52 +0100545 u16 val = 0;
Michal Simekd722e862015-12-10 13:33:20 +0100546
Michal Simekd538ee12015-12-10 17:15:52 +0100547 ret = phyread(bus->priv, addr, reg, &val);
548 debug("emaclite: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg, val, ret);
549 return val;
Michal Simekd722e862015-12-10 13:33:20 +0100550}
551
Michal Simekd538ee12015-12-10 17:15:52 +0100552static int emaclite_miiphy_write(struct mii_dev *bus, int addr, int devad,
553 int reg, u16 value)
Michal Simekd722e862015-12-10 13:33:20 +0100554{
Michal Simekd538ee12015-12-10 17:15:52 +0100555 debug("emaclite: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value);
556 return phywrite(bus->priv, addr, reg, value);
Michal Simekd722e862015-12-10 13:33:20 +0100557}
Michal Simekd722e862015-12-10 13:33:20 +0100558
Michal Simekd538ee12015-12-10 17:15:52 +0100559static int emaclite_probe(struct udevice *dev)
Michal Simek042272a2010-10-11 11:41:47 +1000560{
Michal Simekd538ee12015-12-10 17:15:52 +0100561 struct xemaclite *emaclite = dev_get_priv(dev);
562 int ret;
Michal Simek042272a2010-10-11 11:41:47 +1000563
Michal Simekd538ee12015-12-10 17:15:52 +0100564 emaclite->bus = mdio_alloc();
565 emaclite->bus->read = emaclite_miiphy_read;
566 emaclite->bus->write = emaclite_miiphy_write;
567 emaclite->bus->priv = emaclite;
Michal Simek042272a2010-10-11 11:41:47 +1000568
Michal Simek6516e3f2016-12-08 10:25:44 +0100569 ret = mdio_register_seq(emaclite->bus, dev->seq);
Michal Simekd538ee12015-12-10 17:15:52 +0100570 if (ret)
571 return ret;
Michal Simek773cfa82011-08-25 12:47:56 +0200572
Michal Simekd538ee12015-12-10 17:15:52 +0100573 return 0;
574}
Michal Simek773cfa82011-08-25 12:47:56 +0200575
Michal Simekd538ee12015-12-10 17:15:52 +0100576static int emaclite_remove(struct udevice *dev)
577{
578 struct xemaclite *emaclite = dev_get_priv(dev);
Michal Simek947324b2011-09-12 21:10:01 +0000579
Michal Simekd538ee12015-12-10 17:15:52 +0100580 free(emaclite->phydev);
581 mdio_unregister(emaclite->bus);
582 mdio_free(emaclite->bus);
Michal Simek042272a2010-10-11 11:41:47 +1000583
Michal Simekd538ee12015-12-10 17:15:52 +0100584 return 0;
585}
Michal Simek042272a2010-10-11 11:41:47 +1000586
Michal Simekd538ee12015-12-10 17:15:52 +0100587static const struct eth_ops emaclite_ops = {
Michal Simekf03ec012015-12-16 10:40:05 +0100588 .start = emaclite_start,
Michal Simekd538ee12015-12-10 17:15:52 +0100589 .send = emaclite_send,
590 .recv = emaclite_recv,
Michal Simekf03ec012015-12-16 10:40:05 +0100591 .stop = emaclite_stop,
Michal Simekd538ee12015-12-10 17:15:52 +0100592};
593
594static int emaclite_ofdata_to_platdata(struct udevice *dev)
595{
596 struct eth_pdata *pdata = dev_get_platdata(dev);
597 struct xemaclite *emaclite = dev_get_priv(dev);
598 int offset = 0;
599
Simon Glassa821c4a2017-05-17 17:18:05 -0600600 pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
Zubair Lutfullah Kakakhel39e020e2016-07-27 12:25:07 +0100601 emaclite->regs = (struct emaclite_regs *)ioremap_nocache(pdata->iobase,
602 0x10000);
Michal Simekd538ee12015-12-10 17:15:52 +0100603
Michal Simekd722e862015-12-10 13:33:20 +0100604 emaclite->phyaddr = -1;
Michal Simekd722e862015-12-10 13:33:20 +0100605
Simon Glasse160f7d2017-01-17 16:52:55 -0700606 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Michal Simekd538ee12015-12-10 17:15:52 +0100607 "phy-handle");
608 if (offset > 0)
609 emaclite->phyaddr = fdtdec_get_int(gd->fdt_blob, offset,
610 "reg", -1);
Michal Simek042272a2010-10-11 11:41:47 +1000611
Simon Glasse160f7d2017-01-17 16:52:55 -0700612 emaclite->txpp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Michal Simekd538ee12015-12-10 17:15:52 +0100613 "xlnx,tx-ping-pong", 0);
Simon Glasse160f7d2017-01-17 16:52:55 -0700614 emaclite->rxpp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Michal Simekd538ee12015-12-10 17:15:52 +0100615 "xlnx,rx-ping-pong", 0);
Michal Simekd722e862015-12-10 13:33:20 +0100616
Michal Simekd538ee12015-12-10 17:15:52 +0100617 printf("EMACLITE: %lx, phyaddr %d, %d/%d\n", (ulong)emaclite->regs,
618 emaclite->phyaddr, emaclite->txpp, emaclite->rxpp);
Michal Simekd722e862015-12-10 13:33:20 +0100619
Michal Simekd538ee12015-12-10 17:15:52 +0100620 return 0;
Michal Simek042272a2010-10-11 11:41:47 +1000621}
Michal Simekd538ee12015-12-10 17:15:52 +0100622
623static const struct udevice_id emaclite_ids[] = {
624 { .compatible = "xlnx,xps-ethernetlite-1.00.a" },
625 { }
626};
627
628U_BOOT_DRIVER(emaclite) = {
629 .name = "emaclite",
630 .id = UCLASS_ETH,
631 .of_match = emaclite_ids,
632 .ofdata_to_platdata = emaclite_ofdata_to_platdata,
633 .probe = emaclite_probe,
634 .remove = emaclite_remove,
635 .ops = &emaclite_ops,
636 .priv_auto_alloc_size = sizeof(struct xemaclite),
637 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
638};