blob: 43fc36dc6a82b34a22c2d3e635b90a75ae76d5ed [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Michal Simek89c53892008-03-28 12:41:56 +010011#include <net.h>
12#include <config.h>
Michal Simekd538ee12015-12-10 17:15:52 +010013#include <dm.h>
Michal Simekd722e862015-12-10 13:33:20 +010014#include <console.h>
Michal Simek042272a2010-10-11 11:41:47 +100015#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060016#include <asm/global_data.h>
Michal Simek89c53892008-03-28 12:41:56 +010017#include <asm/io.h>
Michal Simekd722e862015-12-10 13:33:20 +010018#include <phy.h>
19#include <miiphy.h>
Michal Simek7fd70822012-06-28 21:37:57 +000020#include <fdtdec.h>
Simon Glassc05ed002020-05-10 11:40:11 -060021#include <linux/delay.h>
Masahiro Yamada5d97dff2016-09-21 11:28:57 +090022#include <linux/errno.h>
Michal Simek4d2749b2015-12-10 16:31:38 +010023#include <linux/kernel.h>
Zubair Lutfullah Kakakhel39e020e2016-07-27 12:25:07 +010024#include <asm/io.h>
Michal Simek7fd70822012-06-28 21:37:57 +000025
Michal Simekd538ee12015-12-10 17:15:52 +010026DECLARE_GLOBAL_DATA_PTR;
Michal Simek89c53892008-03-28 12:41:56 +010027
Michal Simek89c53892008-03-28 12:41:56 +010028#define ENET_ADDR_LENGTH 6
Michal Simek4d2749b2015-12-10 16:31:38 +010029#define ETH_FCS_LEN 4 /* Octets in the FCS */
Michal Simek89c53892008-03-28 12:41:56 +010030
31/* Xmit complete */
32#define XEL_TSR_XMIT_BUSY_MASK 0x00000001UL
33/* Xmit interrupt enable bit */
34#define XEL_TSR_XMIT_IE_MASK 0x00000008UL
Michal Simek89c53892008-03-28 12:41:56 +010035/* Program the MAC address */
36#define XEL_TSR_PROGRAM_MASK 0x00000002UL
37/* define for programming the MAC address into the EMAC Lite */
38#define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
39
40/* Transmit packet length upper byte */
41#define XEL_TPLR_LENGTH_MASK_HI 0x0000FF00UL
42/* Transmit packet length lower byte */
43#define XEL_TPLR_LENGTH_MASK_LO 0x000000FFUL
44
45/* Recv complete */
46#define XEL_RSR_RECV_DONE_MASK 0x00000001UL
47/* Recv interrupt enable bit */
48#define XEL_RSR_RECV_IE_MASK 0x00000008UL
49
Michal Simekd722e862015-12-10 13:33:20 +010050/* MDIO Address Register Bit Masks */
51#define XEL_MDIOADDR_REGADR_MASK 0x0000001F /* Register Address */
52#define XEL_MDIOADDR_PHYADR_MASK 0x000003E0 /* PHY Address */
53#define XEL_MDIOADDR_PHYADR_SHIFT 5
54#define XEL_MDIOADDR_OP_MASK 0x00000400 /* RD/WR Operation */
55
56/* MDIO Write Data Register Bit Masks */
57#define XEL_MDIOWR_WRDATA_MASK 0x0000FFFF /* Data to be Written */
58
59/* MDIO Read Data Register Bit Masks */
60#define XEL_MDIORD_RDDATA_MASK 0x0000FFFF /* Data to be Read */
61
62/* MDIO Control Register Bit Masks */
63#define XEL_MDIOCTRL_MDIOSTS_MASK 0x00000001 /* MDIO Status Mask */
64#define XEL_MDIOCTRL_MDIOEN_MASK 0x00000008 /* MDIO Enable */
65
Michal Simek9a23c492015-12-10 14:18:15 +010066struct emaclite_regs {
67 u32 tx_ping; /* 0x0 - TX Ping buffer */
68 u32 reserved1[504];
69 u32 mdioaddr; /* 0x7e4 - MDIO Address Register */
70 u32 mdiowr; /* 0x7e8 - MDIO Write Data Register */
71 u32 mdiord;/* 0x7ec - MDIO Read Data Register */
72 u32 mdioctrl; /* 0x7f0 - MDIO Control Register */
73 u32 tx_ping_tplr; /* 0x7f4 - Tx packet length */
74 u32 global_interrupt; /* 0x7f8 - Global interrupt enable */
75 u32 tx_ping_tsr; /* 0x7fc - Tx status */
76 u32 tx_pong; /* 0x800 - TX Pong buffer */
77 u32 reserved2[508];
78 u32 tx_pong_tplr; /* 0xff4 - Tx packet length */
79 u32 reserved3; /* 0xff8 */
80 u32 tx_pong_tsr; /* 0xffc - Tx status */
81 u32 rx_ping; /* 0x1000 - Receive Buffer */
82 u32 reserved4[510];
83 u32 rx_ping_rsr; /* 0x17fc - Rx status */
84 u32 rx_pong; /* 0x1800 - Receive Buffer */
85 u32 reserved5[510];
86 u32 rx_pong_rsr; /* 0x1ffc - Rx status */
87};
88
Michal Simek773cfa82011-08-25 12:47:56 +020089struct xemaclite {
Michal Simek4d2749b2015-12-10 16:31:38 +010090 bool use_rx_pong_buffer_next; /* Next RX buffer to read from */
Michal Simek947324b2011-09-12 21:10:01 +000091 u32 txpp; /* TX ping pong buffer */
92 u32 rxpp; /* RX ping pong buffer */
Michal Simekd722e862015-12-10 13:33:20 +010093 int phyaddr;
Michal Simek9a23c492015-12-10 14:18:15 +010094 struct emaclite_regs *regs;
Michal Simekd722e862015-12-10 13:33:20 +010095 struct phy_device *phydev;
96 struct mii_dev *bus;
Michal Simek773cfa82011-08-25 12:47:56 +020097};
Michal Simek89c53892008-03-28 12:41:56 +010098
Michal Simekf412b6a2015-12-16 10:52:39 +010099static uchar etherrxbuff[PKTSIZE_ALIGN]; /* Receive buffer */
Michal Simek89c53892008-03-28 12:41:56 +0100100
Michal Simek5ac83802011-09-12 21:10:05 +0000101static void xemaclite_alignedread(u32 *srcptr, void *destptr, u32 bytecount)
Michal Simek89c53892008-03-28 12:41:56 +0100102{
Michal Simek042272a2010-10-11 11:41:47 +1000103 u32 i;
Michal Simek89c53892008-03-28 12:41:56 +0100104 u32 alignbuffer;
105 u32 *to32ptr;
106 u32 *from32ptr;
107 u8 *to8ptr;
108 u8 *from8ptr;
109
110 from32ptr = (u32 *) srcptr;
111
112 /* Word aligned buffer, no correction needed. */
113 to32ptr = (u32 *) destptr;
114 while (bytecount > 3) {
115 *to32ptr++ = *from32ptr++;
116 bytecount -= 4;
117 }
118 to8ptr = (u8 *) to32ptr;
119
120 alignbuffer = *from32ptr++;
Michal Simek5ac83802011-09-12 21:10:05 +0000121 from8ptr = (u8 *) &alignbuffer;
Michal Simek89c53892008-03-28 12:41:56 +0100122
Michal Simek5ac83802011-09-12 21:10:05 +0000123 for (i = 0; i < bytecount; i++)
Michal Simek89c53892008-03-28 12:41:56 +0100124 *to8ptr++ = *from8ptr++;
Michal Simek89c53892008-03-28 12:41:56 +0100125}
126
Michal Simek00702512015-12-10 16:01:50 +0100127static void xemaclite_alignedwrite(void *srcptr, u32 *destptr, u32 bytecount)
Michal Simek89c53892008-03-28 12:41:56 +0100128{
Michal Simek042272a2010-10-11 11:41:47 +1000129 u32 i;
Michal Simek89c53892008-03-28 12:41:56 +0100130 u32 alignbuffer;
131 u32 *to32ptr = (u32 *) destptr;
132 u32 *from32ptr;
133 u8 *to8ptr;
134 u8 *from8ptr;
135
136 from32ptr = (u32 *) srcptr;
137 while (bytecount > 3) {
138
139 *to32ptr++ = *from32ptr++;
140 bytecount -= 4;
141 }
142
143 alignbuffer = 0;
Michal Simek5ac83802011-09-12 21:10:05 +0000144 to8ptr = (u8 *) &alignbuffer;
Michal Simek89c53892008-03-28 12:41:56 +0100145 from8ptr = (u8 *) from32ptr;
146
Michal Simek5ac83802011-09-12 21:10:05 +0000147 for (i = 0; i < bytecount; i++)
Michal Simek89c53892008-03-28 12:41:56 +0100148 *to8ptr++ = *from8ptr++;
Michal Simek89c53892008-03-28 12:41:56 +0100149
150 *to32ptr++ = alignbuffer;
151}
152
Michal Simekd722e862015-12-10 13:33:20 +0100153static int wait_for_bit(const char *func, u32 *reg, const u32 mask,
154 bool set, unsigned int timeout)
155{
156 u32 val;
157 unsigned long start = get_timer(0);
158
159 while (1) {
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100160 val = __raw_readl(reg);
Michal Simekd722e862015-12-10 13:33:20 +0100161
162 if (!set)
163 val = ~val;
164
165 if ((val & mask) == mask)
166 return 0;
167
168 if (get_timer(start) > timeout)
169 break;
170
171 if (ctrlc()) {
172 puts("Abort\n");
173 return -EINTR;
174 }
175
176 udelay(1);
177 }
178
179 debug("%s: Timeout (reg=%p mask=%08x wait_set=%i)\n",
180 func, reg, mask, set);
181
182 return -ETIMEDOUT;
183}
184
Michal Simek9a23c492015-12-10 14:18:15 +0100185static int mdio_wait(struct emaclite_regs *regs)
Michal Simekd722e862015-12-10 13:33:20 +0100186{
Michal Simek9a23c492015-12-10 14:18:15 +0100187 return wait_for_bit(__func__, &regs->mdioctrl,
Michal Simekd722e862015-12-10 13:33:20 +0100188 XEL_MDIOCTRL_MDIOSTS_MASK, false, 2000);
189}
190
Michal Simek9a23c492015-12-10 14:18:15 +0100191static u32 phyread(struct xemaclite *emaclite, u32 phyaddress, u32 registernum,
Michal Simekd722e862015-12-10 13:33:20 +0100192 u16 *data)
193{
Michal Simek9a23c492015-12-10 14:18:15 +0100194 struct emaclite_regs *regs = emaclite->regs;
195
196 if (mdio_wait(regs))
Michal Simekd722e862015-12-10 13:33:20 +0100197 return 1;
198
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100199 u32 ctrl_reg = __raw_readl(&regs->mdioctrl);
200 __raw_writel(XEL_MDIOADDR_OP_MASK
201 | ((phyaddress << XEL_MDIOADDR_PHYADR_SHIFT)
202 | registernum), &regs->mdioaddr);
203 __raw_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK, &regs->mdioctrl);
Michal Simekd722e862015-12-10 13:33:20 +0100204
Michal Simek9a23c492015-12-10 14:18:15 +0100205 if (mdio_wait(regs))
Michal Simekd722e862015-12-10 13:33:20 +0100206 return 1;
207
208 /* Read data */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100209 *data = __raw_readl(&regs->mdiord);
Michal Simekd722e862015-12-10 13:33:20 +0100210 return 0;
211}
212
Michal Simek9a23c492015-12-10 14:18:15 +0100213static u32 phywrite(struct xemaclite *emaclite, u32 phyaddress, u32 registernum,
Michal Simekd722e862015-12-10 13:33:20 +0100214 u16 data)
215{
Michal Simek9a23c492015-12-10 14:18:15 +0100216 struct emaclite_regs *regs = emaclite->regs;
217
218 if (mdio_wait(regs))
Michal Simekd722e862015-12-10 13:33:20 +0100219 return 1;
220
221 /*
222 * Write the PHY address, register number and clear the OP bit in the
223 * MDIO Address register and then write the value into the MDIO Write
224 * Data register. Finally, set the Status bit in the MDIO Control
225 * register to start a MDIO write transaction.
226 */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100227 u32 ctrl_reg = __raw_readl(&regs->mdioctrl);
228 __raw_writel(~XEL_MDIOADDR_OP_MASK
229 & ((phyaddress << XEL_MDIOADDR_PHYADR_SHIFT)
230 | registernum), &regs->mdioaddr);
231 __raw_writel(data, &regs->mdiowr);
232 __raw_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK, &regs->mdioctrl);
Michal Simekd722e862015-12-10 13:33:20 +0100233
Michal Simek9a23c492015-12-10 14:18:15 +0100234 if (mdio_wait(regs))
Michal Simekd722e862015-12-10 13:33:20 +0100235 return 1;
236
237 return 0;
238}
Michal Simekd722e862015-12-10 13:33:20 +0100239
Michal Simekf03ec012015-12-16 10:40:05 +0100240static void emaclite_stop(struct udevice *dev)
Michal Simek89c53892008-03-28 12:41:56 +0100241{
Michal Simekf03ec012015-12-16 10:40:05 +0100242 debug("eth_stop\n");
Michal Simek89c53892008-03-28 12:41:56 +0100243}
244
Michal Simekd722e862015-12-10 13:33:20 +0100245/* Use MII register 1 (MII status register) to detect PHY */
246#define PHY_DETECT_REG 1
247
248/* Mask used to verify certain PHY features (or register contents)
249 * in the register above:
250 * 0x1000: 10Mbps full duplex support
251 * 0x0800: 10Mbps half duplex support
252 * 0x0008: Auto-negotiation support
253 */
254#define PHY_DETECT_MASK 0x1808
255
Michal Simekd538ee12015-12-10 17:15:52 +0100256static int setup_phy(struct udevice *dev)
Michal Simekd722e862015-12-10 13:33:20 +0100257{
Michal Simek55259e72016-05-18 12:37:22 +0200258 int i, ret;
Michal Simekd722e862015-12-10 13:33:20 +0100259 u16 phyreg;
Michal Simekd538ee12015-12-10 17:15:52 +0100260 struct xemaclite *emaclite = dev_get_priv(dev);
Michal Simekd722e862015-12-10 13:33:20 +0100261 struct phy_device *phydev;
262
263 u32 supported = SUPPORTED_10baseT_Half |
264 SUPPORTED_10baseT_Full |
265 SUPPORTED_100baseT_Half |
266 SUPPORTED_100baseT_Full;
267
268 if (emaclite->phyaddr != -1) {
Michal Simek9a23c492015-12-10 14:18:15 +0100269 phyread(emaclite, emaclite->phyaddr, PHY_DETECT_REG, &phyreg);
Michal Simekd722e862015-12-10 13:33:20 +0100270 if ((phyreg != 0xFFFF) &&
271 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
272 /* Found a valid PHY address */
273 debug("Default phy address %d is valid\n",
274 emaclite->phyaddr);
275 } else {
276 debug("PHY address is not setup correctly %d\n",
277 emaclite->phyaddr);
278 emaclite->phyaddr = -1;
279 }
280 }
281
282 if (emaclite->phyaddr == -1) {
283 /* detect the PHY address */
284 for (i = 31; i >= 0; i--) {
Michal Simek9a23c492015-12-10 14:18:15 +0100285 phyread(emaclite, i, PHY_DETECT_REG, &phyreg);
Michal Simekd722e862015-12-10 13:33:20 +0100286 if ((phyreg != 0xFFFF) &&
287 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
288 /* Found a valid PHY address */
289 emaclite->phyaddr = i;
290 debug("emaclite: Found valid phy address, %d\n",
291 i);
292 break;
293 }
294 }
295 }
296
297 /* interface - look at tsec */
298 phydev = phy_connect(emaclite->bus, emaclite->phyaddr, dev,
299 PHY_INTERFACE_MODE_MII);
300 /*
301 * Phy can support 1000baseT but device NOT that's why phydev->supported
302 * must be setup for 1000baseT. phydev->advertising setups what speeds
303 * will be used for autonegotiation where 1000baseT must be disabled.
304 */
305 phydev->supported = supported | SUPPORTED_1000baseT_Half |
306 SUPPORTED_1000baseT_Full;
307 phydev->advertising = supported;
308 emaclite->phydev = phydev;
309 phy_config(phydev);
Michal Simek55259e72016-05-18 12:37:22 +0200310 ret = phy_startup(phydev);
311 if (ret)
312 return ret;
Michal Simekd722e862015-12-10 13:33:20 +0100313
314 if (!phydev->link) {
315 printf("%s: No link.\n", phydev->dev->name);
316 return 0;
317 }
318
319 /* Do not setup anything */
320 return 1;
321}
Michal Simekd722e862015-12-10 13:33:20 +0100322
Michal Simekf03ec012015-12-16 10:40:05 +0100323static int emaclite_start(struct udevice *dev)
Michal Simek89c53892008-03-28 12:41:56 +0100324{
Michal Simekd538ee12015-12-10 17:15:52 +0100325 struct xemaclite *emaclite = dev_get_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700326 struct eth_pdata *pdata = dev_get_plat(dev);
Michal Simek9a23c492015-12-10 14:18:15 +0100327 struct emaclite_regs *regs = emaclite->regs;
328
Michal Simek5ac83802011-09-12 21:10:05 +0000329 debug("EmacLite Initialization Started\n");
Michal Simek89c53892008-03-28 12:41:56 +0100330
331/*
332 * TX - TX_PING & TX_PONG initialization
333 */
334 /* Restart PING TX */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100335 __raw_writel(0, &regs->tx_ping_tsr);
Michal Simek89c53892008-03-28 12:41:56 +0100336 /* Copy MAC address */
Michal Simekd538ee12015-12-10 17:15:52 +0100337 xemaclite_alignedwrite(pdata->enetaddr, &regs->tx_ping,
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100338 ENET_ADDR_LENGTH);
Michal Simek89c53892008-03-28 12:41:56 +0100339 /* Set the length */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100340 __raw_writel(ENET_ADDR_LENGTH, &regs->tx_ping_tplr);
Michal Simek89c53892008-03-28 12:41:56 +0100341 /* Update the MAC address in the EMAC Lite */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100342 __raw_writel(XEL_TSR_PROG_MAC_ADDR, &regs->tx_ping_tsr);
Michal Simek89c53892008-03-28 12:41:56 +0100343 /* Wait for EMAC Lite to finish with the MAC address update */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100344 while ((__raw_readl(&regs->tx_ping_tsr) &
Michal Simek8d95ddb2011-08-25 12:36:39 +0200345 XEL_TSR_PROG_MAC_ADDR) != 0)
346 ;
Michal Simek89c53892008-03-28 12:41:56 +0100347
Michal Simek947324b2011-09-12 21:10:01 +0000348 if (emaclite->txpp) {
349 /* The same operation with PONG TX */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100350 __raw_writel(0, &regs->tx_pong_tsr);
Michal Simekd538ee12015-12-10 17:15:52 +0100351 xemaclite_alignedwrite(pdata->enetaddr, &regs->tx_pong,
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100352 ENET_ADDR_LENGTH);
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100353 __raw_writel(ENET_ADDR_LENGTH, &regs->tx_pong_tplr);
354 __raw_writel(XEL_TSR_PROG_MAC_ADDR, &regs->tx_pong_tsr);
355 while ((__raw_readl(&regs->tx_pong_tsr) &
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100356 XEL_TSR_PROG_MAC_ADDR) != 0)
Michal Simek947324b2011-09-12 21:10:01 +0000357 ;
358 }
Michal Simek89c53892008-03-28 12:41:56 +0100359
360/*
361 * RX - RX_PING & RX_PONG initialization
362 */
363 /* Write out the value to flush the RX buffer */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100364 __raw_writel(XEL_RSR_RECV_IE_MASK, &regs->rx_ping_rsr);
Michal Simek947324b2011-09-12 21:10:01 +0000365
366 if (emaclite->rxpp)
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100367 __raw_writel(XEL_RSR_RECV_IE_MASK, &regs->rx_pong_rsr);
Michal Simek89c53892008-03-28 12:41:56 +0100368
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100369 __raw_writel(XEL_MDIOCTRL_MDIOEN_MASK, &regs->mdioctrl);
370 if (__raw_readl(&regs->mdioctrl) & XEL_MDIOCTRL_MDIOEN_MASK)
Michal Simekd722e862015-12-10 13:33:20 +0100371 if (!setup_phy(dev))
372 return -1;
Michal Simekd538ee12015-12-10 17:15:52 +0100373
Michal Simek5ac83802011-09-12 21:10:05 +0000374 debug("EmacLite Initialization complete\n");
Michal Simek89c53892008-03-28 12:41:56 +0100375 return 0;
376}
377
Michal Simek26c79452015-12-10 15:42:01 +0100378static int xemaclite_txbufferavailable(struct xemaclite *emaclite)
Michal Simek89c53892008-03-28 12:41:56 +0100379{
Michal Simek26c79452015-12-10 15:42:01 +0100380 u32 tmp;
381 struct emaclite_regs *regs = emaclite->regs;
Michal Simek773cfa82011-08-25 12:47:56 +0200382
Michal Simek89c53892008-03-28 12:41:56 +0100383 /*
384 * Read the other buffer register
385 * and determine if the other buffer is available
386 */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100387 tmp = ~__raw_readl(&regs->tx_ping_tsr);
Michal Simek26c79452015-12-10 15:42:01 +0100388 if (emaclite->txpp)
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100389 tmp |= ~__raw_readl(&regs->tx_pong_tsr);
Michal Simek89c53892008-03-28 12:41:56 +0100390
Michal Simek26c79452015-12-10 15:42:01 +0100391 return !(tmp & XEL_TSR_XMIT_BUSY_MASK);
Michal Simek89c53892008-03-28 12:41:56 +0100392}
393
Michal Simekd538ee12015-12-10 17:15:52 +0100394static int emaclite_send(struct udevice *dev, void *ptr, int len)
Michal Simek042272a2010-10-11 11:41:47 +1000395{
396 u32 reg;
Michal Simekd538ee12015-12-10 17:15:52 +0100397 struct xemaclite *emaclite = dev_get_priv(dev);
Michal Simek5a4baa32015-12-10 15:32:11 +0100398 struct emaclite_regs *regs = emaclite->regs;
Michal Simek89c53892008-03-28 12:41:56 +0100399
Michal Simek042272a2010-10-11 11:41:47 +1000400 u32 maxtry = 1000;
Michal Simek89c53892008-03-28 12:41:56 +0100401
Michal Simek80439252011-09-12 21:10:04 +0000402 if (len > PKTSIZE)
403 len = PKTSIZE;
Michal Simek89c53892008-03-28 12:41:56 +0100404
Michal Simek26c79452015-12-10 15:42:01 +0100405 while (xemaclite_txbufferavailable(emaclite) && maxtry) {
Michal Simek5ac83802011-09-12 21:10:05 +0000406 udelay(10);
Michal Simek89c53892008-03-28 12:41:56 +0100407 maxtry--;
408 }
409
410 if (!maxtry) {
Michal Simek5ac83802011-09-12 21:10:05 +0000411 printf("Error: Timeout waiting for ethernet TX buffer\n");
Michal Simek89c53892008-03-28 12:41:56 +0100412 /* Restart PING TX */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100413 __raw_writel(0, &regs->tx_ping_tsr);
Michal Simek947324b2011-09-12 21:10:01 +0000414 if (emaclite->txpp) {
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100415 __raw_writel(0, &regs->tx_pong_tsr);
Michal Simek947324b2011-09-12 21:10:01 +0000416 }
Michal Simek95efa792011-03-08 04:25:53 +0000417 return -1;
Michal Simek89c53892008-03-28 12:41:56 +0100418 }
419
Michal Simek89c53892008-03-28 12:41:56 +0100420 /* Determine if the expected buffer address is empty */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100421 reg = __raw_readl(&regs->tx_ping_tsr);
Michal Simek15c239c2015-12-10 16:06:07 +0100422 if ((reg & XEL_TSR_XMIT_BUSY_MASK) == 0) {
Michal Simek00702512015-12-10 16:01:50 +0100423 debug("Send packet from tx_ping buffer\n");
Michal Simek89c53892008-03-28 12:41:56 +0100424 /* Write the frame to the buffer */
Michal Simek00702512015-12-10 16:01:50 +0100425 xemaclite_alignedwrite(ptr, &regs->tx_ping, len);
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100426 __raw_writel(len
427 & (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO),
428 &regs->tx_ping_tplr);
429 reg = __raw_readl(&regs->tx_ping_tsr);
Michal Simek89c53892008-03-28 12:41:56 +0100430 reg |= XEL_TSR_XMIT_BUSY_MASK;
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100431 __raw_writel(reg, &regs->tx_ping_tsr);
Michal Simek95efa792011-03-08 04:25:53 +0000432 return 0;
Michal Simek89c53892008-03-28 12:41:56 +0100433 }
Michal Simek947324b2011-09-12 21:10:01 +0000434
435 if (emaclite->txpp) {
Michal Simek947324b2011-09-12 21:10:01 +0000436 /* Determine if the expected buffer address is empty */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100437 reg = __raw_readl(&regs->tx_pong_tsr);
Michal Simek15c239c2015-12-10 16:06:07 +0100438 if ((reg & XEL_TSR_XMIT_BUSY_MASK) == 0) {
Michal Simek00702512015-12-10 16:01:50 +0100439 debug("Send packet from tx_pong buffer\n");
Michal Simek947324b2011-09-12 21:10:01 +0000440 /* Write the frame to the buffer */
Michal Simek00702512015-12-10 16:01:50 +0100441 xemaclite_alignedwrite(ptr, &regs->tx_pong, len);
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100442 __raw_writel(len &
Michal Simek00702512015-12-10 16:01:50 +0100443 (XEL_TPLR_LENGTH_MASK_HI |
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100444 XEL_TPLR_LENGTH_MASK_LO),
445 &regs->tx_pong_tplr);
446 reg = __raw_readl(&regs->tx_pong_tsr);
Michal Simek947324b2011-09-12 21:10:01 +0000447 reg |= XEL_TSR_XMIT_BUSY_MASK;
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100448 __raw_writel(reg, &regs->tx_pong_tsr);
Michal Simek947324b2011-09-12 21:10:01 +0000449 return 0;
Michal Simek89c53892008-03-28 12:41:56 +0100450 }
Michal Simek89c53892008-03-28 12:41:56 +0100451 }
Michal Simek947324b2011-09-12 21:10:01 +0000452
Michal Simek5ac83802011-09-12 21:10:05 +0000453 puts("Error while sending frame\n");
Michal Simek95efa792011-03-08 04:25:53 +0000454 return -1;
Michal Simek89c53892008-03-28 12:41:56 +0100455}
456
Michal Simekd538ee12015-12-10 17:15:52 +0100457static int emaclite_recv(struct udevice *dev, int flags, uchar **packetp)
Michal Simek89c53892008-03-28 12:41:56 +0100458{
Michal Simek4d2749b2015-12-10 16:31:38 +0100459 u32 length, first_read, reg, attempt = 0;
460 void *addr, *ack;
Simon Glass0fd3d912020-12-22 19:30:28 -0700461 struct xemaclite *emaclite = dev_get_priv(dev);
Michal Simek4d2749b2015-12-10 16:31:38 +0100462 struct emaclite_regs *regs = emaclite->regs;
463 struct ethernet_hdr *eth;
464 struct ip_udp_hdr *ip;
Michal Simek89c53892008-03-28 12:41:56 +0100465
Michal Simek4d2749b2015-12-10 16:31:38 +0100466try_again:
467 if (!emaclite->use_rx_pong_buffer_next) {
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100468 reg = __raw_readl(&regs->rx_ping_rsr);
Michal Simek4d2749b2015-12-10 16:31:38 +0100469 debug("Testing data at rx_ping\n");
470 if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
471 debug("Data found in rx_ping buffer\n");
472 addr = &regs->rx_ping;
473 ack = &regs->rx_ping_rsr;
Michal Simek947324b2011-09-12 21:10:01 +0000474 } else {
Michal Simek4d2749b2015-12-10 16:31:38 +0100475 debug("Data not found in rx_ping buffer\n");
476 /* Pong buffer is not available - return immediately */
477 if (!emaclite->rxpp)
478 return -1;
479
480 /* Try pong buffer if this is first attempt */
481 if (attempt++)
482 return -1;
483 emaclite->use_rx_pong_buffer_next =
484 !emaclite->use_rx_pong_buffer_next;
485 goto try_again;
486 }
487 } else {
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100488 reg = __raw_readl(&regs->rx_pong_rsr);
Michal Simek4d2749b2015-12-10 16:31:38 +0100489 debug("Testing data at rx_pong\n");
490 if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
491 debug("Data found in rx_pong buffer\n");
492 addr = &regs->rx_pong;
493 ack = &regs->rx_pong_rsr;
494 } else {
495 debug("Data not found in rx_pong buffer\n");
496 /* Try ping buffer if this is first attempt */
497 if (attempt++)
498 return -1;
499 emaclite->use_rx_pong_buffer_next =
500 !emaclite->use_rx_pong_buffer_next;
501 goto try_again;
Michal Simek89c53892008-03-28 12:41:56 +0100502 }
Michal Simek89c53892008-03-28 12:41:56 +0100503 }
Michal Simek4d2749b2015-12-10 16:31:38 +0100504
505 /* Read all bytes for ARP packet with 32bit alignment - 48bytes */
506 first_read = ALIGN(ETHER_HDR_SIZE + ARP_HDR_SIZE + ETH_FCS_LEN, 4);
507 xemaclite_alignedread(addr, etherrxbuff, first_read);
508
509 /* Detect real packet size */
510 eth = (struct ethernet_hdr *)etherrxbuff;
511 switch (ntohs(eth->et_protlen)) {
512 case PROT_ARP:
513 length = first_read;
514 debug("ARP Packet %x\n", length);
515 break;
516 case PROT_IP:
517 ip = (struct ip_udp_hdr *)(etherrxbuff + ETHER_HDR_SIZE);
518 length = ntohs(ip->ip_len);
519 length += ETHER_HDR_SIZE + ETH_FCS_LEN;
520 debug("IP Packet %x\n", length);
521 break;
522 default:
523 debug("Other Packet\n");
524 length = PKTSIZE;
525 break;
Michal Simek89c53892008-03-28 12:41:56 +0100526 }
527
Michal Simek4d2749b2015-12-10 16:31:38 +0100528 /* Read the rest of the packet which is longer then first read */
529 if (length != first_read)
530 xemaclite_alignedread(addr + first_read,
531 etherrxbuff + first_read,
532 length - first_read);
Michal Simek89c53892008-03-28 12:41:56 +0100533
534 /* Acknowledge the frame */
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100535 reg = __raw_readl(ack);
Michal Simek89c53892008-03-28 12:41:56 +0100536 reg &= ~XEL_RSR_RECV_DONE_MASK;
Zubair Lutfullah Kakakhel611fe0b2016-07-27 12:25:08 +0100537 __raw_writel(reg, ack);
Michal Simek89c53892008-03-28 12:41:56 +0100538
Michal Simek4d2749b2015-12-10 16:31:38 +0100539 debug("Packet receive from 0x%p, length %dB\n", addr, length);
Michal Simekf412b6a2015-12-16 10:52:39 +0100540 *packetp = etherrxbuff;
541 return length;
Michal Simek89c53892008-03-28 12:41:56 +0100542}
Michal Simek042272a2010-10-11 11:41:47 +1000543
Michal Simekd538ee12015-12-10 17:15:52 +0100544static int emaclite_miiphy_read(struct mii_dev *bus, int addr,
545 int devad, int reg)
Michal Simekd722e862015-12-10 13:33:20 +0100546{
547 u32 ret;
Michal Simekd538ee12015-12-10 17:15:52 +0100548 u16 val = 0;
Michal Simekd722e862015-12-10 13:33:20 +0100549
Michal Simekd538ee12015-12-10 17:15:52 +0100550 ret = phyread(bus->priv, addr, reg, &val);
551 debug("emaclite: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg, val, ret);
552 return val;
Michal Simekd722e862015-12-10 13:33:20 +0100553}
554
Michal Simekd538ee12015-12-10 17:15:52 +0100555static int emaclite_miiphy_write(struct mii_dev *bus, int addr, int devad,
556 int reg, u16 value)
Michal Simekd722e862015-12-10 13:33:20 +0100557{
Michal Simekd538ee12015-12-10 17:15:52 +0100558 debug("emaclite: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value);
559 return phywrite(bus->priv, addr, reg, value);
Michal Simekd722e862015-12-10 13:33:20 +0100560}
Michal Simekd722e862015-12-10 13:33:20 +0100561
Michal Simekd538ee12015-12-10 17:15:52 +0100562static int emaclite_probe(struct udevice *dev)
Michal Simek042272a2010-10-11 11:41:47 +1000563{
Michal Simekd538ee12015-12-10 17:15:52 +0100564 struct xemaclite *emaclite = dev_get_priv(dev);
565 int ret;
Michal Simek042272a2010-10-11 11:41:47 +1000566
Michal Simekd538ee12015-12-10 17:15:52 +0100567 emaclite->bus = mdio_alloc();
568 emaclite->bus->read = emaclite_miiphy_read;
569 emaclite->bus->write = emaclite_miiphy_write;
570 emaclite->bus->priv = emaclite;
Michal Simek042272a2010-10-11 11:41:47 +1000571
Simon Glass8b85dfc2020-12-16 21:20:07 -0700572 ret = mdio_register_seq(emaclite->bus, dev_seq(dev));
Michal Simekd538ee12015-12-10 17:15:52 +0100573 if (ret)
574 return ret;
Michal Simek773cfa82011-08-25 12:47:56 +0200575
Michal Simekd538ee12015-12-10 17:15:52 +0100576 return 0;
577}
Michal Simek773cfa82011-08-25 12:47:56 +0200578
Michal Simekd538ee12015-12-10 17:15:52 +0100579static int emaclite_remove(struct udevice *dev)
580{
581 struct xemaclite *emaclite = dev_get_priv(dev);
Michal Simek947324b2011-09-12 21:10:01 +0000582
Michal Simekd538ee12015-12-10 17:15:52 +0100583 free(emaclite->phydev);
584 mdio_unregister(emaclite->bus);
585 mdio_free(emaclite->bus);
Michal Simek042272a2010-10-11 11:41:47 +1000586
Michal Simekd538ee12015-12-10 17:15:52 +0100587 return 0;
588}
Michal Simek042272a2010-10-11 11:41:47 +1000589
Michal Simekd538ee12015-12-10 17:15:52 +0100590static const struct eth_ops emaclite_ops = {
Michal Simekf03ec012015-12-16 10:40:05 +0100591 .start = emaclite_start,
Michal Simekd538ee12015-12-10 17:15:52 +0100592 .send = emaclite_send,
593 .recv = emaclite_recv,
Michal Simekf03ec012015-12-16 10:40:05 +0100594 .stop = emaclite_stop,
Michal Simekd538ee12015-12-10 17:15:52 +0100595};
596
Simon Glassd1998a92020-12-03 16:55:21 -0700597static int emaclite_of_to_plat(struct udevice *dev)
Michal Simekd538ee12015-12-10 17:15:52 +0100598{
Simon Glassc69cda22020-12-03 16:55:20 -0700599 struct eth_pdata *pdata = dev_get_plat(dev);
Michal Simekd538ee12015-12-10 17:15:52 +0100600 struct xemaclite *emaclite = dev_get_priv(dev);
601 int offset = 0;
602
Masahiro Yamada25484932020-07-17 14:36:48 +0900603 pdata->iobase = dev_read_addr(dev);
Zubair Lutfullah Kakakhel39e020e2016-07-27 12:25:07 +0100604 emaclite->regs = (struct emaclite_regs *)ioremap_nocache(pdata->iobase,
605 0x10000);
Michal Simekd538ee12015-12-10 17:15:52 +0100606
Michal Simekd722e862015-12-10 13:33:20 +0100607 emaclite->phyaddr = -1;
Michal Simekd722e862015-12-10 13:33:20 +0100608
Simon Glasse160f7d2017-01-17 16:52:55 -0700609 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Michal Simekd538ee12015-12-10 17:15:52 +0100610 "phy-handle");
611 if (offset > 0)
612 emaclite->phyaddr = fdtdec_get_int(gd->fdt_blob, offset,
613 "reg", -1);
Michal Simek042272a2010-10-11 11:41:47 +1000614
Simon Glasse160f7d2017-01-17 16:52:55 -0700615 emaclite->txpp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Michal Simekd538ee12015-12-10 17:15:52 +0100616 "xlnx,tx-ping-pong", 0);
Simon Glasse160f7d2017-01-17 16:52:55 -0700617 emaclite->rxpp = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Michal Simekd538ee12015-12-10 17:15:52 +0100618 "xlnx,rx-ping-pong", 0);
Michal Simekd722e862015-12-10 13:33:20 +0100619
Michal Simekd538ee12015-12-10 17:15:52 +0100620 printf("EMACLITE: %lx, phyaddr %d, %d/%d\n", (ulong)emaclite->regs,
621 emaclite->phyaddr, emaclite->txpp, emaclite->rxpp);
Michal Simekd722e862015-12-10 13:33:20 +0100622
Michal Simekd538ee12015-12-10 17:15:52 +0100623 return 0;
Michal Simek042272a2010-10-11 11:41:47 +1000624}
Michal Simekd538ee12015-12-10 17:15:52 +0100625
626static const struct udevice_id emaclite_ids[] = {
627 { .compatible = "xlnx,xps-ethernetlite-1.00.a" },
628 { }
629};
630
631U_BOOT_DRIVER(emaclite) = {
632 .name = "emaclite",
633 .id = UCLASS_ETH,
634 .of_match = emaclite_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700635 .of_to_plat = emaclite_of_to_plat,
Michal Simekd538ee12015-12-10 17:15:52 +0100636 .probe = emaclite_probe,
637 .remove = emaclite_remove,
638 .ops = &emaclite_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700639 .priv_auto = sizeof(struct xemaclite),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700640 .plat_auto = sizeof(struct eth_pdata),
Michal Simekd538ee12015-12-10 17:15:52 +0100641};