blob: e97ce2ce31f3487f888389158c09f4d473993022 [file] [log] [blame]
Michal Simek78d19a32009-09-07 09:08:02 +02001/*
2 * (C) Copyright 2007-2009 Michal Simek
3 * (C) Copyright 2003 Xilinx Inc.
Michal Simek89c53892008-03-28 12:41:56 +01004 *
Michal Simek89c53892008-03-28 12:41:56 +01005 * Michal SIMEK <monstr@monstr.eu>
6 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02007 * SPDX-License-Identifier: GPL-2.0+
Michal Simek78d19a32009-09-07 09:08:02 +02008 */
Michal Simek89c53892008-03-28 12:41:56 +01009
10#include <common.h>
11#include <net.h>
12#include <config.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>
Michal Simekd722e862015-12-10 13:33:20 +010019#include <asm-generic/errno.h>
Michal Simek7fd70822012-06-28 21:37:57 +000020
Michal Simek89c53892008-03-28 12:41:56 +010021#undef DEBUG
22
Michal Simek89c53892008-03-28 12:41:56 +010023#define ENET_ADDR_LENGTH 6
24
25/* EmacLite constants */
26#define XEL_BUFFER_OFFSET 0x0800 /* Next buffer's offset */
Michal Simek89c53892008-03-28 12:41:56 +010027#define XEL_RSR_OFFSET 0x17FC /* Rx status */
28#define XEL_RXBUFF_OFFSET 0x1000 /* Receive Buffer */
29
30/* Xmit complete */
31#define XEL_TSR_XMIT_BUSY_MASK 0x00000001UL
32/* Xmit interrupt enable bit */
33#define XEL_TSR_XMIT_IE_MASK 0x00000008UL
Michal Simek89c53892008-03-28 12:41:56 +010034/* 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 Simekd722e862015-12-10 13:33:20 +010049/* MDIO Address Register Bit Masks */
50#define XEL_MDIOADDR_REGADR_MASK 0x0000001F /* Register Address */
51#define XEL_MDIOADDR_PHYADR_MASK 0x000003E0 /* PHY Address */
52#define XEL_MDIOADDR_PHYADR_SHIFT 5
53#define XEL_MDIOADDR_OP_MASK 0x00000400 /* RD/WR Operation */
54
55/* MDIO Write Data Register Bit Masks */
56#define XEL_MDIOWR_WRDATA_MASK 0x0000FFFF /* Data to be Written */
57
58/* MDIO Read Data Register Bit Masks */
59#define XEL_MDIORD_RDDATA_MASK 0x0000FFFF /* Data to be Read */
60
61/* MDIO Control Register Bit Masks */
62#define XEL_MDIOCTRL_MDIOSTS_MASK 0x00000001 /* MDIO Status Mask */
63#define XEL_MDIOCTRL_MDIOEN_MASK 0x00000008 /* MDIO Enable */
64
Michal Simek9a23c492015-12-10 14:18:15 +010065struct emaclite_regs {
66 u32 tx_ping; /* 0x0 - TX Ping buffer */
67 u32 reserved1[504];
68 u32 mdioaddr; /* 0x7e4 - MDIO Address Register */
69 u32 mdiowr; /* 0x7e8 - MDIO Write Data Register */
70 u32 mdiord;/* 0x7ec - MDIO Read Data Register */
71 u32 mdioctrl; /* 0x7f0 - MDIO Control Register */
72 u32 tx_ping_tplr; /* 0x7f4 - Tx packet length */
73 u32 global_interrupt; /* 0x7f8 - Global interrupt enable */
74 u32 tx_ping_tsr; /* 0x7fc - Tx status */
75 u32 tx_pong; /* 0x800 - TX Pong buffer */
76 u32 reserved2[508];
77 u32 tx_pong_tplr; /* 0xff4 - Tx packet length */
78 u32 reserved3; /* 0xff8 */
79 u32 tx_pong_tsr; /* 0xffc - Tx status */
80 u32 rx_ping; /* 0x1000 - Receive Buffer */
81 u32 reserved4[510];
82 u32 rx_ping_rsr; /* 0x17fc - Rx status */
83 u32 rx_pong; /* 0x1800 - Receive Buffer */
84 u32 reserved5[510];
85 u32 rx_pong_rsr; /* 0x1ffc - Rx status */
86};
87
Michal Simek773cfa82011-08-25 12:47:56 +020088struct xemaclite {
Michal Simek042272a2010-10-11 11:41:47 +100089 u32 nextrxbuffertouse; /* Next RX buffer to read from */
Michal Simek947324b2011-09-12 21:10:01 +000090 u32 txpp; /* TX ping pong buffer */
91 u32 rxpp; /* RX ping pong buffer */
Michal Simekd722e862015-12-10 13:33:20 +010092 int phyaddr;
Michal Simek9a23c492015-12-10 14:18:15 +010093 struct emaclite_regs *regs;
Michal Simekd722e862015-12-10 13:33:20 +010094 struct phy_device *phydev;
95 struct mii_dev *bus;
Michal Simek773cfa82011-08-25 12:47:56 +020096};
Michal Simek89c53892008-03-28 12:41:56 +010097
Clive Stubbingsf2a7806f2008-10-27 15:05:00 +000098static u32 etherrxbuff[PKTSIZE_ALIGN/4]; /* Receive buffer */
Michal Simek89c53892008-03-28 12:41:56 +010099
Michal Simek5ac83802011-09-12 21:10:05 +0000100static void xemaclite_alignedread(u32 *srcptr, void *destptr, u32 bytecount)
Michal Simek89c53892008-03-28 12:41:56 +0100101{
Michal Simek042272a2010-10-11 11:41:47 +1000102 u32 i;
Michal Simek89c53892008-03-28 12:41:56 +0100103 u32 alignbuffer;
104 u32 *to32ptr;
105 u32 *from32ptr;
106 u8 *to8ptr;
107 u8 *from8ptr;
108
109 from32ptr = (u32 *) srcptr;
110
111 /* Word aligned buffer, no correction needed. */
112 to32ptr = (u32 *) destptr;
113 while (bytecount > 3) {
114 *to32ptr++ = *from32ptr++;
115 bytecount -= 4;
116 }
117 to8ptr = (u8 *) to32ptr;
118
119 alignbuffer = *from32ptr++;
Michal Simek5ac83802011-09-12 21:10:05 +0000120 from8ptr = (u8 *) &alignbuffer;
Michal Simek89c53892008-03-28 12:41:56 +0100121
Michal Simek5ac83802011-09-12 21:10:05 +0000122 for (i = 0; i < bytecount; i++)
Michal Simek89c53892008-03-28 12:41:56 +0100123 *to8ptr++ = *from8ptr++;
Michal Simek89c53892008-03-28 12:41:56 +0100124}
125
Michal Simek00702512015-12-10 16:01:50 +0100126static void xemaclite_alignedwrite(void *srcptr, u32 *destptr, u32 bytecount)
Michal Simek89c53892008-03-28 12:41:56 +0100127{
Michal Simek042272a2010-10-11 11:41:47 +1000128 u32 i;
Michal Simek89c53892008-03-28 12:41:56 +0100129 u32 alignbuffer;
130 u32 *to32ptr = (u32 *) destptr;
131 u32 *from32ptr;
132 u8 *to8ptr;
133 u8 *from8ptr;
134
135 from32ptr = (u32 *) srcptr;
136 while (bytecount > 3) {
137
138 *to32ptr++ = *from32ptr++;
139 bytecount -= 4;
140 }
141
142 alignbuffer = 0;
Michal Simek5ac83802011-09-12 21:10:05 +0000143 to8ptr = (u8 *) &alignbuffer;
Michal Simek89c53892008-03-28 12:41:56 +0100144 from8ptr = (u8 *) from32ptr;
145
Michal Simek5ac83802011-09-12 21:10:05 +0000146 for (i = 0; i < bytecount; i++)
Michal Simek89c53892008-03-28 12:41:56 +0100147 *to8ptr++ = *from8ptr++;
Michal Simek89c53892008-03-28 12:41:56 +0100148
149 *to32ptr++ = alignbuffer;
150}
151
Michal Simekd722e862015-12-10 13:33:20 +0100152#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
153static 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) {
160 val = readl(reg);
161
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
Michal Simek9a23c492015-12-10 14:18:15 +0100199 u32 ctrl_reg = in_be32(&regs->mdioctrl);
200 out_be32(&regs->mdioaddr, XEL_MDIOADDR_OP_MASK |
Michal Simekd722e862015-12-10 13:33:20 +0100201 ((phyaddress << XEL_MDIOADDR_PHYADR_SHIFT) | registernum));
Michal Simek9a23c492015-12-10 14:18:15 +0100202 out_be32(&regs->mdioctrl, ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK);
Michal Simekd722e862015-12-10 13:33:20 +0100203
Michal Simek9a23c492015-12-10 14:18:15 +0100204 if (mdio_wait(regs))
Michal Simekd722e862015-12-10 13:33:20 +0100205 return 1;
206
207 /* Read data */
Michal Simek9a23c492015-12-10 14:18:15 +0100208 *data = in_be32(&regs->mdiord);
Michal Simekd722e862015-12-10 13:33:20 +0100209 return 0;
210}
211
Michal Simek9a23c492015-12-10 14:18:15 +0100212static u32 phywrite(struct xemaclite *emaclite, u32 phyaddress, u32 registernum,
Michal Simekd722e862015-12-10 13:33:20 +0100213 u16 data)
214{
Michal Simek9a23c492015-12-10 14:18:15 +0100215 struct emaclite_regs *regs = emaclite->regs;
216
217 if (mdio_wait(regs))
Michal Simekd722e862015-12-10 13:33:20 +0100218 return 1;
219
220 /*
221 * Write the PHY address, register number and clear the OP bit in the
222 * MDIO Address register and then write the value into the MDIO Write
223 * Data register. Finally, set the Status bit in the MDIO Control
224 * register to start a MDIO write transaction.
225 */
Michal Simek9a23c492015-12-10 14:18:15 +0100226 u32 ctrl_reg = in_be32(&regs->mdioctrl);
227 out_be32(&regs->mdioaddr, ~XEL_MDIOADDR_OP_MASK &
Michal Simekd722e862015-12-10 13:33:20 +0100228 ((phyaddress << XEL_MDIOADDR_PHYADR_SHIFT) | registernum));
Michal Simek9a23c492015-12-10 14:18:15 +0100229 out_be32(&regs->mdiowr, data);
230 out_be32(&regs->mdioctrl, ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK);
Michal Simekd722e862015-12-10 13:33:20 +0100231
Michal Simek9a23c492015-12-10 14:18:15 +0100232 if (mdio_wait(regs))
Michal Simekd722e862015-12-10 13:33:20 +0100233 return 1;
234
235 return 0;
236}
237#endif
238
Michal Simek042272a2010-10-11 11:41:47 +1000239static void emaclite_halt(struct eth_device *dev)
Michal Simek89c53892008-03-28 12:41:56 +0100240{
Michal Simek5ac83802011-09-12 21:10:05 +0000241 debug("eth_halt\n");
Michal Simek89c53892008-03-28 12:41:56 +0100242}
243
Michal Simekd722e862015-12-10 13:33:20 +0100244/* Use MII register 1 (MII status register) to detect PHY */
245#define PHY_DETECT_REG 1
246
247/* Mask used to verify certain PHY features (or register contents)
248 * in the register above:
249 * 0x1000: 10Mbps full duplex support
250 * 0x0800: 10Mbps half duplex support
251 * 0x0008: Auto-negotiation support
252 */
253#define PHY_DETECT_MASK 0x1808
254
255#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
256static int setup_phy(struct eth_device *dev)
257{
258 int i;
259 u16 phyreg;
260 struct xemaclite *emaclite = dev->priv;
261 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);
310 phy_startup(phydev);
311
312 if (!phydev->link) {
313 printf("%s: No link.\n", phydev->dev->name);
314 return 0;
315 }
316
317 /* Do not setup anything */
318 return 1;
319}
320#endif
321
Michal Simek042272a2010-10-11 11:41:47 +1000322static int emaclite_init(struct eth_device *dev, bd_t *bis)
Michal Simek89c53892008-03-28 12:41:56 +0100323{
Michal Simek947324b2011-09-12 21:10:01 +0000324 struct xemaclite *emaclite = dev->priv;
Michal Simek9a23c492015-12-10 14:18:15 +0100325 struct emaclite_regs *regs = emaclite->regs;
326
Michal Simek5ac83802011-09-12 21:10:05 +0000327 debug("EmacLite Initialization Started\n");
Michal Simek89c53892008-03-28 12:41:56 +0100328
329/*
330 * TX - TX_PING & TX_PONG initialization
331 */
332 /* Restart PING TX */
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100333 out_be32(&regs->tx_ping_tsr, 0);
Michal Simek89c53892008-03-28 12:41:56 +0100334 /* Copy MAC address */
Michal Simek00702512015-12-10 16:01:50 +0100335 xemaclite_alignedwrite(dev->enetaddr, &regs->tx_ping,
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100336 ENET_ADDR_LENGTH);
Michal Simek89c53892008-03-28 12:41:56 +0100337 /* Set the length */
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100338 out_be32(&regs->tx_ping_tplr, ENET_ADDR_LENGTH);
Michal Simek89c53892008-03-28 12:41:56 +0100339 /* Update the MAC address in the EMAC Lite */
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100340 out_be32(&regs->tx_ping_tsr, XEL_TSR_PROG_MAC_ADDR);
Michal Simek89c53892008-03-28 12:41:56 +0100341 /* Wait for EMAC Lite to finish with the MAC address update */
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100342 while ((in_be32 (&regs->tx_ping_tsr) &
Michal Simek8d95ddb2011-08-25 12:36:39 +0200343 XEL_TSR_PROG_MAC_ADDR) != 0)
344 ;
Michal Simek89c53892008-03-28 12:41:56 +0100345
Michal Simek947324b2011-09-12 21:10:01 +0000346 if (emaclite->txpp) {
347 /* The same operation with PONG TX */
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100348 out_be32(&regs->tx_pong_tsr, 0);
Michal Simek00702512015-12-10 16:01:50 +0100349 xemaclite_alignedwrite(dev->enetaddr, &regs->tx_pong,
Michal Simeka0b2bfb2015-12-10 15:22:21 +0100350 ENET_ADDR_LENGTH);
351 out_be32(&regs->tx_pong_tplr, ENET_ADDR_LENGTH);
352 out_be32(&regs->tx_pong_tsr, XEL_TSR_PROG_MAC_ADDR);
353 while ((in_be32(&regs->tx_pong_tsr) &
354 XEL_TSR_PROG_MAC_ADDR) != 0)
Michal Simek947324b2011-09-12 21:10:01 +0000355 ;
356 }
Michal Simek89c53892008-03-28 12:41:56 +0100357
358/*
359 * RX - RX_PING & RX_PONG initialization
360 */
361 /* Write out the value to flush the RX buffer */
Michal Simek3af70902015-12-10 15:24:23 +0100362 out_be32(&regs->rx_ping_rsr, XEL_RSR_RECV_IE_MASK);
Michal Simek947324b2011-09-12 21:10:01 +0000363
364 if (emaclite->rxpp)
Michal Simek3af70902015-12-10 15:24:23 +0100365 out_be32(&regs->rx_pong_rsr, XEL_RSR_RECV_IE_MASK);
Michal Simek89c53892008-03-28 12:41:56 +0100366
Michal Simekd722e862015-12-10 13:33:20 +0100367#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Michal Simek9a23c492015-12-10 14:18:15 +0100368 out_be32(&regs->mdioctrl, XEL_MDIOCTRL_MDIOEN_MASK);
369 if (in_be32(&regs->mdioctrl) & XEL_MDIOCTRL_MDIOEN_MASK)
Michal Simekd722e862015-12-10 13:33:20 +0100370 if (!setup_phy(dev))
371 return -1;
372#endif
Michal Simek5ac83802011-09-12 21:10:05 +0000373 debug("EmacLite Initialization complete\n");
Michal Simek89c53892008-03-28 12:41:56 +0100374 return 0;
375}
376
Michal Simek26c79452015-12-10 15:42:01 +0100377static int xemaclite_txbufferavailable(struct xemaclite *emaclite)
Michal Simek89c53892008-03-28 12:41:56 +0100378{
Michal Simek26c79452015-12-10 15:42:01 +0100379 u32 tmp;
380 struct emaclite_regs *regs = emaclite->regs;
Michal Simek773cfa82011-08-25 12:47:56 +0200381
Michal Simek89c53892008-03-28 12:41:56 +0100382 /*
383 * Read the other buffer register
384 * and determine if the other buffer is available
385 */
Michal Simek26c79452015-12-10 15:42:01 +0100386 tmp = ~in_be32(&regs->tx_ping_tsr);
387 if (emaclite->txpp)
388 tmp |= ~in_be32(&regs->tx_pong_tsr);
Michal Simek89c53892008-03-28 12:41:56 +0100389
Michal Simek26c79452015-12-10 15:42:01 +0100390 return !(tmp & XEL_TSR_XMIT_BUSY_MASK);
Michal Simek89c53892008-03-28 12:41:56 +0100391}
392
Stephan Linz1ae6b9c2012-05-22 12:18:10 +0000393static int emaclite_send(struct eth_device *dev, void *ptr, int len)
Michal Simek042272a2010-10-11 11:41:47 +1000394{
395 u32 reg;
Michal Simek773cfa82011-08-25 12:47:56 +0200396 struct xemaclite *emaclite = dev->priv;
Michal Simek5a4baa32015-12-10 15:32:11 +0100397 struct emaclite_regs *regs = emaclite->regs;
Michal Simek89c53892008-03-28 12:41:56 +0100398
Michal Simek042272a2010-10-11 11:41:47 +1000399 u32 maxtry = 1000;
Michal Simek89c53892008-03-28 12:41:56 +0100400
Michal Simek80439252011-09-12 21:10:04 +0000401 if (len > PKTSIZE)
402 len = PKTSIZE;
Michal Simek89c53892008-03-28 12:41:56 +0100403
Michal Simek26c79452015-12-10 15:42:01 +0100404 while (xemaclite_txbufferavailable(emaclite) && maxtry) {
Michal Simek5ac83802011-09-12 21:10:05 +0000405 udelay(10);
Michal Simek89c53892008-03-28 12:41:56 +0100406 maxtry--;
407 }
408
409 if (!maxtry) {
Michal Simek5ac83802011-09-12 21:10:05 +0000410 printf("Error: Timeout waiting for ethernet TX buffer\n");
Michal Simek89c53892008-03-28 12:41:56 +0100411 /* Restart PING TX */
Michal Simek5a4baa32015-12-10 15:32:11 +0100412 out_be32(&regs->tx_ping_tsr, 0);
Michal Simek947324b2011-09-12 21:10:01 +0000413 if (emaclite->txpp) {
Michal Simek5a4baa32015-12-10 15:32:11 +0100414 out_be32(&regs->tx_pong_tsr, 0);
Michal Simek947324b2011-09-12 21:10:01 +0000415 }
Michal Simek95efa792011-03-08 04:25:53 +0000416 return -1;
Michal Simek89c53892008-03-28 12:41:56 +0100417 }
418
Michal Simek89c53892008-03-28 12:41:56 +0100419 /* Determine if the expected buffer address is empty */
Michal Simek00702512015-12-10 16:01:50 +0100420 reg = in_be32(&regs->tx_ping_tsr);
Michal Simek15c239c2015-12-10 16:06:07 +0100421 if ((reg & XEL_TSR_XMIT_BUSY_MASK) == 0) {
Michal Simek00702512015-12-10 16:01:50 +0100422 debug("Send packet from tx_ping buffer\n");
Michal Simek89c53892008-03-28 12:41:56 +0100423 /* Write the frame to the buffer */
Michal Simek00702512015-12-10 16:01:50 +0100424 xemaclite_alignedwrite(ptr, &regs->tx_ping, len);
425 out_be32(&regs->tx_ping_tplr, len &
426 (XEL_TPLR_LENGTH_MASK_HI | XEL_TPLR_LENGTH_MASK_LO));
427 reg = in_be32(&regs->tx_ping_tsr);
Michal Simek89c53892008-03-28 12:41:56 +0100428 reg |= XEL_TSR_XMIT_BUSY_MASK;
Michal Simek00702512015-12-10 16:01:50 +0100429 out_be32(&regs->tx_ping_tsr, reg);
Michal Simek95efa792011-03-08 04:25:53 +0000430 return 0;
Michal Simek89c53892008-03-28 12:41:56 +0100431 }
Michal Simek947324b2011-09-12 21:10:01 +0000432
433 if (emaclite->txpp) {
Michal Simek947324b2011-09-12 21:10:01 +0000434 /* Determine if the expected buffer address is empty */
Michal Simek00702512015-12-10 16:01:50 +0100435 reg = in_be32(&regs->tx_pong_tsr);
Michal Simek15c239c2015-12-10 16:06:07 +0100436 if ((reg & XEL_TSR_XMIT_BUSY_MASK) == 0) {
Michal Simek00702512015-12-10 16:01:50 +0100437 debug("Send packet from tx_pong buffer\n");
Michal Simek947324b2011-09-12 21:10:01 +0000438 /* Write the frame to the buffer */
Michal Simek00702512015-12-10 16:01:50 +0100439 xemaclite_alignedwrite(ptr, &regs->tx_pong, len);
440 out_be32(&regs->tx_pong_tplr, len &
441 (XEL_TPLR_LENGTH_MASK_HI |
442 XEL_TPLR_LENGTH_MASK_LO));
443 reg = in_be32(&regs->tx_pong_tsr);
Michal Simek947324b2011-09-12 21:10:01 +0000444 reg |= XEL_TSR_XMIT_BUSY_MASK;
Michal Simek00702512015-12-10 16:01:50 +0100445 out_be32(&regs->tx_pong_tsr, reg);
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 Simek042272a2010-10-11 11:41:47 +1000454static int emaclite_recv(struct eth_device *dev)
Michal Simek89c53892008-03-28 12:41:56 +0100455{
Michal Simek042272a2010-10-11 11:41:47 +1000456 u32 length;
457 u32 reg;
458 u32 baseaddress;
Michal Simek773cfa82011-08-25 12:47:56 +0200459 struct xemaclite *emaclite = dev->priv;
Michal Simek89c53892008-03-28 12:41:56 +0100460
Michal Simek773cfa82011-08-25 12:47:56 +0200461 baseaddress = dev->iobase + emaclite->nextrxbuffertouse;
Michal Simek89c53892008-03-28 12:41:56 +0100462 reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
Michal Simek5ac83802011-09-12 21:10:05 +0000463 debug("Testing data at address 0x%x\n", baseaddress);
Michal Simek89c53892008-03-28 12:41:56 +0100464 if ((reg & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
Michal Simek947324b2011-09-12 21:10:01 +0000465 if (emaclite->rxpp)
466 emaclite->nextrxbuffertouse ^= XEL_BUFFER_OFFSET;
Michal Simek89c53892008-03-28 12:41:56 +0100467 } else {
Michal Simek947324b2011-09-12 21:10:01 +0000468
469 if (!emaclite->rxpp) {
Michal Simek5ac83802011-09-12 21:10:05 +0000470 debug("No data was available - address 0x%x\n",
Michal Simek947324b2011-09-12 21:10:01 +0000471 baseaddress);
Michal Simek89c53892008-03-28 12:41:56 +0100472 return 0;
Michal Simek947324b2011-09-12 21:10:01 +0000473 } else {
474 baseaddress ^= XEL_BUFFER_OFFSET;
475 reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
476 if ((reg & XEL_RSR_RECV_DONE_MASK) !=
477 XEL_RSR_RECV_DONE_MASK) {
478 debug("No data was available - address 0x%x\n",
479 baseaddress);
480 return 0;
481 }
Michal Simek89c53892008-03-28 12:41:56 +0100482 }
Michal Simek89c53892008-03-28 12:41:56 +0100483 }
484 /* Get the length of the frame that arrived */
Michal Simek3f91ec02010-10-11 11:41:46 +1000485 switch(((ntohl(in_be32 (baseaddress + XEL_RXBUFF_OFFSET + 0xC))) &
Michal Simek89c53892008-03-28 12:41:56 +0100486 0xFFFF0000 ) >> 16) {
487 case 0x806:
488 length = 42 + 20; /* FIXME size of ARP */
Michal Simek5ac83802011-09-12 21:10:05 +0000489 debug("ARP Packet\n");
Michal Simek89c53892008-03-28 12:41:56 +0100490 break;
491 case 0x800:
492 length = 14 + 14 +
Michal Simek5ac83802011-09-12 21:10:05 +0000493 (((ntohl(in_be32 (baseaddress + XEL_RXBUFF_OFFSET +
494 0x10))) & 0xFFFF0000) >> 16);
495 /* FIXME size of IP packet */
Michal Simek89c53892008-03-28 12:41:56 +0100496 debug ("IP Packet\n");
497 break;
498 default:
Michal Simek80439252011-09-12 21:10:04 +0000499 debug("Other Packet\n");
500 length = PKTSIZE;
Michal Simek89c53892008-03-28 12:41:56 +0100501 break;
502 }
503
Michal Simek5ac83802011-09-12 21:10:05 +0000504 xemaclite_alignedread((u32 *) (baseaddress + XEL_RXBUFF_OFFSET),
Michal Simek89c53892008-03-28 12:41:56 +0100505 etherrxbuff, length);
506
507 /* Acknowledge the frame */
508 reg = in_be32 (baseaddress + XEL_RSR_OFFSET);
509 reg &= ~XEL_RSR_RECV_DONE_MASK;
510 out_be32 (baseaddress + XEL_RSR_OFFSET, reg);
511
Michal Simek5ac83802011-09-12 21:10:05 +0000512 debug("Packet receive from 0x%x, length %dB\n", baseaddress, length);
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500513 net_process_received_packet((uchar *)etherrxbuff, length);
Michal Simek95efa792011-03-08 04:25:53 +0000514 return length;
Michal Simek89c53892008-03-28 12:41:56 +0100515
516}
Michal Simek042272a2010-10-11 11:41:47 +1000517
Michal Simekd722e862015-12-10 13:33:20 +0100518#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
519static int emaclite_miiphy_read(const char *devname, uchar addr,
520 uchar reg, ushort *val)
521{
522 u32 ret;
523 struct eth_device *dev = eth_get_dev();
524
Michal Simek9a23c492015-12-10 14:18:15 +0100525 ret = phyread(dev->priv, addr, reg, val);
Michal Simekd722e862015-12-10 13:33:20 +0100526 debug("emaclite: Read MII 0x%x, 0x%x, 0x%x\n", addr, reg, *val);
527 return ret;
528}
529
530static int emaclite_miiphy_write(const char *devname, uchar addr,
531 uchar reg, ushort val)
532{
533 struct eth_device *dev = eth_get_dev();
534
535 debug("emaclite: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, val);
Michal Simek9a23c492015-12-10 14:18:15 +0100536 return phywrite(dev->priv, addr, reg, val);
Michal Simekd722e862015-12-10 13:33:20 +0100537}
538#endif
539
Michal Simekc1044a12011-10-12 23:23:22 +0000540int xilinx_emaclite_initialize(bd_t *bis, unsigned long base_addr,
541 int txpp, int rxpp)
Michal Simek042272a2010-10-11 11:41:47 +1000542{
543 struct eth_device *dev;
Michal Simek773cfa82011-08-25 12:47:56 +0200544 struct xemaclite *emaclite;
Michal Simek9a23c492015-12-10 14:18:15 +0100545 struct emaclite_regs *regs;
Michal Simek042272a2010-10-11 11:41:47 +1000546
Michal Simek28ae02e2011-08-25 12:28:47 +0200547 dev = calloc(1, sizeof(*dev));
Michal Simek042272a2010-10-11 11:41:47 +1000548 if (dev == NULL)
Michal Simek95efa792011-03-08 04:25:53 +0000549 return -1;
Michal Simek042272a2010-10-11 11:41:47 +1000550
Michal Simek773cfa82011-08-25 12:47:56 +0200551 emaclite = calloc(1, sizeof(struct xemaclite));
552 if (emaclite == NULL) {
553 free(dev);
554 return -1;
555 }
556
557 dev->priv = emaclite;
558
Michal Simekc1044a12011-10-12 23:23:22 +0000559 emaclite->txpp = txpp;
560 emaclite->rxpp = rxpp;
Michal Simek947324b2011-09-12 21:10:01 +0000561
Michal Simek9b947552011-10-12 23:23:21 +0000562 sprintf(dev->name, "Xelite.%lx", base_addr);
Michal Simek042272a2010-10-11 11:41:47 +1000563
Michal Simek9a23c492015-12-10 14:18:15 +0100564 emaclite->regs = (struct emaclite_regs *)base_addr;
565 regs = emaclite->regs;
Michal Simek042272a2010-10-11 11:41:47 +1000566 dev->iobase = base_addr;
Michal Simek042272a2010-10-11 11:41:47 +1000567 dev->init = emaclite_init;
568 dev->halt = emaclite_halt;
569 dev->send = emaclite_send;
570 dev->recv = emaclite_recv;
571
Michal Simekd722e862015-12-10 13:33:20 +0100572#ifdef CONFIG_PHY_ADDR
573 emaclite->phyaddr = CONFIG_PHY_ADDR;
574#else
575 emaclite->phyaddr = -1;
576#endif
577
Michal Simek042272a2010-10-11 11:41:47 +1000578 eth_register(dev);
579
Michal Simekd722e862015-12-10 13:33:20 +0100580#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
581 miiphy_register(dev->name, emaclite_miiphy_read, emaclite_miiphy_write);
582 emaclite->bus = miiphy_get_dev_by_name(dev->name);
583
Michal Simek9a23c492015-12-10 14:18:15 +0100584 out_be32(&regs->mdioctrl, XEL_MDIOCTRL_MDIOEN_MASK);
Michal Simekd722e862015-12-10 13:33:20 +0100585#endif
586
Michal Simek95efa792011-03-08 04:25:53 +0000587 return 1;
Michal Simek042272a2010-10-11 11:41:47 +1000588}