xref: /openbmc/u-boot/drivers/net/zynq_gem.c (revision 48263504)
1 /*
2  * (C) Copyright 2011 Michal Simek
3  *
4  * Michal SIMEK <monstr@monstr.eu>
5  *
6  * Based on Xilinx gmac driver:
7  * (C) Copyright 2011 Xilinx
8  *
9  * SPDX-License-Identifier:	GPL-2.0+
10  */
11 
12 #include <clk.h>
13 #include <common.h>
14 #include <dm.h>
15 #include <net.h>
16 #include <netdev.h>
17 #include <config.h>
18 #include <console.h>
19 #include <malloc.h>
20 #include <asm/io.h>
21 #include <phy.h>
22 #include <miiphy.h>
23 #include <wait_bit.h>
24 #include <watchdog.h>
25 #include <asm/system.h>
26 #include <asm/arch/hardware.h>
27 #include <asm/arch/sys_proto.h>
28 #include <linux/errno.h>
29 
30 DECLARE_GLOBAL_DATA_PTR;
31 
32 /* Bit/mask specification */
33 #define ZYNQ_GEM_PHYMNTNC_OP_MASK	0x40020000 /* operation mask bits */
34 #define ZYNQ_GEM_PHYMNTNC_OP_R_MASK	0x20000000 /* read operation */
35 #define ZYNQ_GEM_PHYMNTNC_OP_W_MASK	0x10000000 /* write operation */
36 #define ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK	23 /* Shift bits for PHYAD */
37 #define ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK	18 /* Shift bits for PHREG */
38 
39 #define ZYNQ_GEM_RXBUF_EOF_MASK		0x00008000 /* End of frame. */
40 #define ZYNQ_GEM_RXBUF_SOF_MASK		0x00004000 /* Start of frame. */
41 #define ZYNQ_GEM_RXBUF_LEN_MASK		0x00003FFF /* Mask for length field */
42 
43 #define ZYNQ_GEM_RXBUF_WRAP_MASK	0x00000002 /* Wrap bit, last BD */
44 #define ZYNQ_GEM_RXBUF_NEW_MASK		0x00000001 /* Used bit.. */
45 #define ZYNQ_GEM_RXBUF_ADD_MASK		0xFFFFFFFC /* Mask for address */
46 
47 /* Wrap bit, last descriptor */
48 #define ZYNQ_GEM_TXBUF_WRAP_MASK	0x40000000
49 #define ZYNQ_GEM_TXBUF_LAST_MASK	0x00008000 /* Last buffer */
50 #define ZYNQ_GEM_TXBUF_USED_MASK	0x80000000 /* Used by Hw */
51 
52 #define ZYNQ_GEM_NWCTRL_TXEN_MASK	0x00000008 /* Enable transmit */
53 #define ZYNQ_GEM_NWCTRL_RXEN_MASK	0x00000004 /* Enable receive */
54 #define ZYNQ_GEM_NWCTRL_MDEN_MASK	0x00000010 /* Enable MDIO port */
55 #define ZYNQ_GEM_NWCTRL_STARTTX_MASK	0x00000200 /* Start tx (tx_go) */
56 
57 #define ZYNQ_GEM_NWCFG_SPEED100		0x00000001 /* 100 Mbps operation */
58 #define ZYNQ_GEM_NWCFG_SPEED1000	0x00000400 /* 1Gbps operation */
59 #define ZYNQ_GEM_NWCFG_FDEN		0x00000002 /* Full Duplex mode */
60 #define ZYNQ_GEM_NWCFG_FSREM		0x00020000 /* FCS removal */
61 #define ZYNQ_GEM_NWCFG_SGMII_ENBL	0x08000000 /* SGMII Enable */
62 #define ZYNQ_GEM_NWCFG_PCS_SEL		0x00000800 /* PCS select */
63 #ifdef CONFIG_ARM64
64 #define ZYNQ_GEM_NWCFG_MDCCLKDIV	0x00100000 /* Div pclk by 64, max 160MHz */
65 #else
66 #define ZYNQ_GEM_NWCFG_MDCCLKDIV	0x000c0000 /* Div pclk by 48, max 120MHz */
67 #endif
68 
69 #ifdef CONFIG_ARM64
70 # define ZYNQ_GEM_DBUS_WIDTH	(1 << 21) /* 64 bit bus */
71 #else
72 # define ZYNQ_GEM_DBUS_WIDTH	(0 << 21) /* 32 bit bus */
73 #endif
74 
75 #define ZYNQ_GEM_NWCFG_INIT		(ZYNQ_GEM_DBUS_WIDTH | \
76 					ZYNQ_GEM_NWCFG_FDEN | \
77 					ZYNQ_GEM_NWCFG_FSREM | \
78 					ZYNQ_GEM_NWCFG_MDCCLKDIV)
79 
80 #define ZYNQ_GEM_NWSR_MDIOIDLE_MASK	0x00000004 /* PHY management idle */
81 
82 #define ZYNQ_GEM_DMACR_BLENGTH		0x00000004 /* INCR4 AHB bursts */
83 /* Use full configured addressable space (8 Kb) */
84 #define ZYNQ_GEM_DMACR_RXSIZE		0x00000300
85 /* Use full configured addressable space (4 Kb) */
86 #define ZYNQ_GEM_DMACR_TXSIZE		0x00000400
87 /* Set with binary 00011000 to use 1536 byte(1*max length frame/buffer) */
88 #define ZYNQ_GEM_DMACR_RXBUF		0x00180000
89 
90 #define ZYNQ_GEM_DMACR_INIT		(ZYNQ_GEM_DMACR_BLENGTH | \
91 					ZYNQ_GEM_DMACR_RXSIZE | \
92 					ZYNQ_GEM_DMACR_TXSIZE | \
93 					ZYNQ_GEM_DMACR_RXBUF)
94 
95 #define ZYNQ_GEM_TSR_DONE		0x00000020 /* Tx done mask */
96 
97 #define ZYNQ_GEM_PCS_CTL_ANEG_ENBL	0x1000
98 
99 /* Use MII register 1 (MII status register) to detect PHY */
100 #define PHY_DETECT_REG  1
101 
102 /* Mask used to verify certain PHY features (or register contents)
103  * in the register above:
104  *  0x1000: 10Mbps full duplex support
105  *  0x0800: 10Mbps half duplex support
106  *  0x0008: Auto-negotiation support
107  */
108 #define PHY_DETECT_MASK 0x1808
109 
110 /* TX BD status masks */
111 #define ZYNQ_GEM_TXBUF_FRMLEN_MASK	0x000007ff
112 #define ZYNQ_GEM_TXBUF_EXHAUSTED	0x08000000
113 #define ZYNQ_GEM_TXBUF_UNDERRUN		0x10000000
114 
115 /* Clock frequencies for different speeds */
116 #define ZYNQ_GEM_FREQUENCY_10	2500000UL
117 #define ZYNQ_GEM_FREQUENCY_100	25000000UL
118 #define ZYNQ_GEM_FREQUENCY_1000	125000000UL
119 
120 /* Device registers */
121 struct zynq_gem_regs {
122 	u32 nwctrl; /* 0x0 - Network Control reg */
123 	u32 nwcfg; /* 0x4 - Network Config reg */
124 	u32 nwsr; /* 0x8 - Network Status reg */
125 	u32 reserved1;
126 	u32 dmacr; /* 0x10 - DMA Control reg */
127 	u32 txsr; /* 0x14 - TX Status reg */
128 	u32 rxqbase; /* 0x18 - RX Q Base address reg */
129 	u32 txqbase; /* 0x1c - TX Q Base address reg */
130 	u32 rxsr; /* 0x20 - RX Status reg */
131 	u32 reserved2[2];
132 	u32 idr; /* 0x2c - Interrupt Disable reg */
133 	u32 reserved3;
134 	u32 phymntnc; /* 0x34 - Phy Maintaince reg */
135 	u32 reserved4[18];
136 	u32 hashl; /* 0x80 - Hash Low address reg */
137 	u32 hashh; /* 0x84 - Hash High address reg */
138 #define LADDR_LOW	0
139 #define LADDR_HIGH	1
140 	u32 laddr[4][LADDR_HIGH + 1]; /* 0x8c - Specific1 addr low/high reg */
141 	u32 match[4]; /* 0xa8 - Type ID1 Match reg */
142 	u32 reserved6[18];
143 #define STAT_SIZE	44
144 	u32 stat[STAT_SIZE]; /* 0x100 - Octects transmitted Low reg */
145 	u32 reserved9[20];
146 	u32 pcscntrl;
147 	u32 reserved7[143];
148 	u32 transmit_q1_ptr; /* 0x440 - Transmit priority queue 1 */
149 	u32 reserved8[15];
150 	u32 receive_q1_ptr; /* 0x480 - Receive priority queue 1 */
151 };
152 
153 /* BD descriptors */
154 struct emac_bd {
155 	u32 addr; /* Next descriptor pointer */
156 	u32 status;
157 };
158 
159 #define RX_BUF 32
160 /* Page table entries are set to 1MB, or multiples of 1MB
161  * (not < 1MB). driver uses less bd's so use 1MB bdspace.
162  */
163 #define BD_SPACE	0x100000
164 /* BD separation space */
165 #define BD_SEPRN_SPACE	(RX_BUF * sizeof(struct emac_bd))
166 
167 /* Setup the first free TX descriptor */
168 #define TX_FREE_DESC	2
169 
170 /* Initialized, rxbd_current, rx_first_buf must be 0 after init */
171 struct zynq_gem_priv {
172 	struct emac_bd *tx_bd;
173 	struct emac_bd *rx_bd;
174 	char *rxbuffers;
175 	u32 rxbd_current;
176 	u32 rx_first_buf;
177 	int phyaddr;
178 	int init;
179 	struct zynq_gem_regs *iobase;
180 	phy_interface_t interface;
181 	struct phy_device *phydev;
182 	int phy_of_handle;
183 	struct mii_dev *bus;
184 	struct clk clk;
185 	bool int_pcs;
186 };
187 
188 static u32 phy_setup_op(struct zynq_gem_priv *priv, u32 phy_addr, u32 regnum,
189 			u32 op, u16 *data)
190 {
191 	u32 mgtcr;
192 	struct zynq_gem_regs *regs = priv->iobase;
193 	int err;
194 
195 	err = wait_for_bit_le32(&regs->nwsr, ZYNQ_GEM_NWSR_MDIOIDLE_MASK,
196 				true, 20000, false);
197 	if (err)
198 		return err;
199 
200 	/* Construct mgtcr mask for the operation */
201 	mgtcr = ZYNQ_GEM_PHYMNTNC_OP_MASK | op |
202 		(phy_addr << ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK) |
203 		(regnum << ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK) | *data;
204 
205 	/* Write mgtcr and wait for completion */
206 	writel(mgtcr, &regs->phymntnc);
207 
208 	err = wait_for_bit_le32(&regs->nwsr, ZYNQ_GEM_NWSR_MDIOIDLE_MASK,
209 				true, 20000, false);
210 	if (err)
211 		return err;
212 
213 	if (op == ZYNQ_GEM_PHYMNTNC_OP_R_MASK)
214 		*data = readl(&regs->phymntnc);
215 
216 	return 0;
217 }
218 
219 static u32 phyread(struct zynq_gem_priv *priv, u32 phy_addr,
220 		   u32 regnum, u16 *val)
221 {
222 	u32 ret;
223 
224 	ret = phy_setup_op(priv, phy_addr, regnum,
225 			   ZYNQ_GEM_PHYMNTNC_OP_R_MASK, val);
226 
227 	if (!ret)
228 		debug("%s: phy_addr %d, regnum 0x%x, val 0x%x\n", __func__,
229 		      phy_addr, regnum, *val);
230 
231 	return ret;
232 }
233 
234 static u32 phywrite(struct zynq_gem_priv *priv, u32 phy_addr,
235 		    u32 regnum, u16 data)
236 {
237 	debug("%s: phy_addr %d, regnum 0x%x, data 0x%x\n", __func__, phy_addr,
238 	      regnum, data);
239 
240 	return phy_setup_op(priv, phy_addr, regnum,
241 			    ZYNQ_GEM_PHYMNTNC_OP_W_MASK, &data);
242 }
243 
244 static int phy_detection(struct udevice *dev)
245 {
246 	int i;
247 	u16 phyreg;
248 	struct zynq_gem_priv *priv = dev->priv;
249 
250 	if (priv->phyaddr != -1) {
251 		phyread(priv, priv->phyaddr, PHY_DETECT_REG, &phyreg);
252 		if ((phyreg != 0xFFFF) &&
253 		    ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
254 			/* Found a valid PHY address */
255 			debug("Default phy address %d is valid\n",
256 			      priv->phyaddr);
257 			return 0;
258 		} else {
259 			debug("PHY address is not setup correctly %d\n",
260 			      priv->phyaddr);
261 			priv->phyaddr = -1;
262 		}
263 	}
264 
265 	debug("detecting phy address\n");
266 	if (priv->phyaddr == -1) {
267 		/* detect the PHY address */
268 		for (i = 31; i >= 0; i--) {
269 			phyread(priv, i, PHY_DETECT_REG, &phyreg);
270 			if ((phyreg != 0xFFFF) &&
271 			    ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
272 				/* Found a valid PHY address */
273 				priv->phyaddr = i;
274 				debug("Found valid phy address, %d\n", i);
275 				return 0;
276 			}
277 		}
278 	}
279 	printf("PHY is not detected\n");
280 	return -1;
281 }
282 
283 static int zynq_gem_setup_mac(struct udevice *dev)
284 {
285 	u32 i, macaddrlow, macaddrhigh;
286 	struct eth_pdata *pdata = dev_get_platdata(dev);
287 	struct zynq_gem_priv *priv = dev_get_priv(dev);
288 	struct zynq_gem_regs *regs = priv->iobase;
289 
290 	/* Set the MAC bits [31:0] in BOT */
291 	macaddrlow = pdata->enetaddr[0];
292 	macaddrlow |= pdata->enetaddr[1] << 8;
293 	macaddrlow |= pdata->enetaddr[2] << 16;
294 	macaddrlow |= pdata->enetaddr[3] << 24;
295 
296 	/* Set MAC bits [47:32] in TOP */
297 	macaddrhigh = pdata->enetaddr[4];
298 	macaddrhigh |= pdata->enetaddr[5] << 8;
299 
300 	for (i = 0; i < 4; i++) {
301 		writel(0, &regs->laddr[i][LADDR_LOW]);
302 		writel(0, &regs->laddr[i][LADDR_HIGH]);
303 		/* Do not use MATCHx register */
304 		writel(0, &regs->match[i]);
305 	}
306 
307 	writel(macaddrlow, &regs->laddr[0][LADDR_LOW]);
308 	writel(macaddrhigh, &regs->laddr[0][LADDR_HIGH]);
309 
310 	return 0;
311 }
312 
313 static int zynq_phy_init(struct udevice *dev)
314 {
315 	int ret;
316 	struct zynq_gem_priv *priv = dev_get_priv(dev);
317 	struct zynq_gem_regs *regs = priv->iobase;
318 	const u32 supported = SUPPORTED_10baseT_Half |
319 			SUPPORTED_10baseT_Full |
320 			SUPPORTED_100baseT_Half |
321 			SUPPORTED_100baseT_Full |
322 			SUPPORTED_1000baseT_Half |
323 			SUPPORTED_1000baseT_Full;
324 
325 	/* Enable only MDIO bus */
326 	writel(ZYNQ_GEM_NWCTRL_MDEN_MASK, &regs->nwctrl);
327 
328 	if (priv->interface != PHY_INTERFACE_MODE_SGMII) {
329 		ret = phy_detection(dev);
330 		if (ret) {
331 			printf("GEM PHY init failed\n");
332 			return ret;
333 		}
334 	}
335 
336 	priv->phydev = phy_connect(priv->bus, priv->phyaddr, dev,
337 				   priv->interface);
338 	if (!priv->phydev)
339 		return -ENODEV;
340 
341 	priv->phydev->supported &= supported | ADVERTISED_Pause |
342 				  ADVERTISED_Asym_Pause;
343 	priv->phydev->advertising = priv->phydev->supported;
344 
345 	if (priv->phy_of_handle > 0)
346 		dev_set_of_offset(priv->phydev->dev, priv->phy_of_handle);
347 
348 	return phy_config(priv->phydev);
349 }
350 
351 static int zynq_gem_init(struct udevice *dev)
352 {
353 	u32 i, nwconfig;
354 	int ret;
355 	unsigned long clk_rate = 0;
356 	struct zynq_gem_priv *priv = dev_get_priv(dev);
357 	struct zynq_gem_regs *regs = priv->iobase;
358 	struct emac_bd *dummy_tx_bd = &priv->tx_bd[TX_FREE_DESC];
359 	struct emac_bd *dummy_rx_bd = &priv->tx_bd[TX_FREE_DESC + 2];
360 
361 	if (!priv->init) {
362 		/* Disable all interrupts */
363 		writel(0xFFFFFFFF, &regs->idr);
364 
365 		/* Disable the receiver & transmitter */
366 		writel(0, &regs->nwctrl);
367 		writel(0, &regs->txsr);
368 		writel(0, &regs->rxsr);
369 		writel(0, &regs->phymntnc);
370 
371 		/* Clear the Hash registers for the mac address
372 		 * pointed by AddressPtr
373 		 */
374 		writel(0x0, &regs->hashl);
375 		/* Write bits [63:32] in TOP */
376 		writel(0x0, &regs->hashh);
377 
378 		/* Clear all counters */
379 		for (i = 0; i < STAT_SIZE; i++)
380 			readl(&regs->stat[i]);
381 
382 		/* Setup RxBD space */
383 		memset(priv->rx_bd, 0, RX_BUF * sizeof(struct emac_bd));
384 
385 		for (i = 0; i < RX_BUF; i++) {
386 			priv->rx_bd[i].status = 0xF0000000;
387 			priv->rx_bd[i].addr =
388 					((ulong)(priv->rxbuffers) +
389 							(i * PKTSIZE_ALIGN));
390 		}
391 		/* WRAP bit to last BD */
392 		priv->rx_bd[--i].addr |= ZYNQ_GEM_RXBUF_WRAP_MASK;
393 		/* Write RxBDs to IP */
394 		writel((ulong)priv->rx_bd, &regs->rxqbase);
395 
396 		/* Setup for DMA Configuration register */
397 		writel(ZYNQ_GEM_DMACR_INIT, &regs->dmacr);
398 
399 		/* Setup for Network Control register, MDIO, Rx and Tx enable */
400 		setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_MDEN_MASK);
401 
402 		/* Disable the second priority queue */
403 		dummy_tx_bd->addr = 0;
404 		dummy_tx_bd->status = ZYNQ_GEM_TXBUF_WRAP_MASK |
405 				ZYNQ_GEM_TXBUF_LAST_MASK|
406 				ZYNQ_GEM_TXBUF_USED_MASK;
407 
408 		dummy_rx_bd->addr = ZYNQ_GEM_RXBUF_WRAP_MASK |
409 				ZYNQ_GEM_RXBUF_NEW_MASK;
410 		dummy_rx_bd->status = 0;
411 
412 		writel((ulong)dummy_tx_bd, &regs->transmit_q1_ptr);
413 		writel((ulong)dummy_rx_bd, &regs->receive_q1_ptr);
414 
415 		priv->init++;
416 	}
417 
418 	ret = phy_startup(priv->phydev);
419 	if (ret)
420 		return ret;
421 
422 	if (!priv->phydev->link) {
423 		printf("%s: No link.\n", priv->phydev->dev->name);
424 		return -1;
425 	}
426 
427 	nwconfig = ZYNQ_GEM_NWCFG_INIT;
428 
429 	/*
430 	 * Set SGMII enable PCS selection only if internal PCS/PMA
431 	 * core is used and interface is SGMII.
432 	 */
433 	if (priv->interface == PHY_INTERFACE_MODE_SGMII &&
434 	    priv->int_pcs) {
435 		nwconfig |= ZYNQ_GEM_NWCFG_SGMII_ENBL |
436 			    ZYNQ_GEM_NWCFG_PCS_SEL;
437 #ifdef CONFIG_ARM64
438 		writel(readl(&regs->pcscntrl) | ZYNQ_GEM_PCS_CTL_ANEG_ENBL,
439 		       &regs->pcscntrl);
440 #endif
441 	}
442 
443 	switch (priv->phydev->speed) {
444 	case SPEED_1000:
445 		writel(nwconfig | ZYNQ_GEM_NWCFG_SPEED1000,
446 		       &regs->nwcfg);
447 		clk_rate = ZYNQ_GEM_FREQUENCY_1000;
448 		break;
449 	case SPEED_100:
450 		writel(nwconfig | ZYNQ_GEM_NWCFG_SPEED100,
451 		       &regs->nwcfg);
452 		clk_rate = ZYNQ_GEM_FREQUENCY_100;
453 		break;
454 	case SPEED_10:
455 		clk_rate = ZYNQ_GEM_FREQUENCY_10;
456 		break;
457 	}
458 
459 	ret = clk_set_rate(&priv->clk, clk_rate);
460 	if (IS_ERR_VALUE(ret) && ret != (unsigned long)-ENOSYS) {
461 		dev_err(dev, "failed to set tx clock rate\n");
462 		return ret;
463 	}
464 
465 	ret = clk_enable(&priv->clk);
466 	if (ret && ret != -ENOSYS) {
467 		dev_err(dev, "failed to enable tx clock\n");
468 		return ret;
469 	}
470 
471 	setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK |
472 					ZYNQ_GEM_NWCTRL_TXEN_MASK);
473 
474 	return 0;
475 }
476 
477 static int zynq_gem_send(struct udevice *dev, void *ptr, int len)
478 {
479 	u32 addr, size;
480 	struct zynq_gem_priv *priv = dev_get_priv(dev);
481 	struct zynq_gem_regs *regs = priv->iobase;
482 	struct emac_bd *current_bd = &priv->tx_bd[1];
483 
484 	/* Setup Tx BD */
485 	memset(priv->tx_bd, 0, sizeof(struct emac_bd));
486 
487 	priv->tx_bd->addr = (ulong)ptr;
488 	priv->tx_bd->status = (len & ZYNQ_GEM_TXBUF_FRMLEN_MASK) |
489 			       ZYNQ_GEM_TXBUF_LAST_MASK;
490 	/* Dummy descriptor to mark it as the last in descriptor chain */
491 	current_bd->addr = 0x0;
492 	current_bd->status = ZYNQ_GEM_TXBUF_WRAP_MASK |
493 			     ZYNQ_GEM_TXBUF_LAST_MASK|
494 			     ZYNQ_GEM_TXBUF_USED_MASK;
495 
496 	/* setup BD */
497 	writel((ulong)priv->tx_bd, &regs->txqbase);
498 
499 	addr = (ulong) ptr;
500 	addr &= ~(ARCH_DMA_MINALIGN - 1);
501 	size = roundup(len, ARCH_DMA_MINALIGN);
502 	flush_dcache_range(addr, addr + size);
503 
504 	addr = (ulong)priv->rxbuffers;
505 	addr &= ~(ARCH_DMA_MINALIGN - 1);
506 	size = roundup((RX_BUF * PKTSIZE_ALIGN), ARCH_DMA_MINALIGN);
507 	flush_dcache_range(addr, addr + size);
508 	barrier();
509 
510 	/* Start transmit */
511 	setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_STARTTX_MASK);
512 
513 	/* Read TX BD status */
514 	if (priv->tx_bd->status & ZYNQ_GEM_TXBUF_EXHAUSTED)
515 		printf("TX buffers exhausted in mid frame\n");
516 
517 	return wait_for_bit_le32(&regs->txsr, ZYNQ_GEM_TSR_DONE,
518 				 true, 20000, true);
519 }
520 
521 /* Do not check frame_recd flag in rx_status register 0x20 - just poll BD */
522 static int zynq_gem_recv(struct udevice *dev, int flags, uchar **packetp)
523 {
524 	int frame_len;
525 	u32 addr;
526 	struct zynq_gem_priv *priv = dev_get_priv(dev);
527 	struct emac_bd *current_bd = &priv->rx_bd[priv->rxbd_current];
528 
529 	if (!(current_bd->addr & ZYNQ_GEM_RXBUF_NEW_MASK))
530 		return -1;
531 
532 	if (!(current_bd->status &
533 			(ZYNQ_GEM_RXBUF_SOF_MASK | ZYNQ_GEM_RXBUF_EOF_MASK))) {
534 		printf("GEM: SOF or EOF not set for last buffer received!\n");
535 		return -1;
536 	}
537 
538 	frame_len = current_bd->status & ZYNQ_GEM_RXBUF_LEN_MASK;
539 	if (!frame_len) {
540 		printf("%s: Zero size packet?\n", __func__);
541 		return -1;
542 	}
543 
544 	addr = current_bd->addr & ZYNQ_GEM_RXBUF_ADD_MASK;
545 	addr &= ~(ARCH_DMA_MINALIGN - 1);
546 	*packetp = (uchar *)(uintptr_t)addr;
547 
548 	return frame_len;
549 }
550 
551 static int zynq_gem_free_pkt(struct udevice *dev, uchar *packet, int length)
552 {
553 	struct zynq_gem_priv *priv = dev_get_priv(dev);
554 	struct emac_bd *current_bd = &priv->rx_bd[priv->rxbd_current];
555 	struct emac_bd *first_bd;
556 
557 	if (current_bd->status & ZYNQ_GEM_RXBUF_SOF_MASK) {
558 		priv->rx_first_buf = priv->rxbd_current;
559 	} else {
560 		current_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK;
561 		current_bd->status = 0xF0000000; /* FIXME */
562 	}
563 
564 	if (current_bd->status & ZYNQ_GEM_RXBUF_EOF_MASK) {
565 		first_bd = &priv->rx_bd[priv->rx_first_buf];
566 		first_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK;
567 		first_bd->status = 0xF0000000;
568 	}
569 
570 	if ((++priv->rxbd_current) >= RX_BUF)
571 		priv->rxbd_current = 0;
572 
573 	return 0;
574 }
575 
576 static void zynq_gem_halt(struct udevice *dev)
577 {
578 	struct zynq_gem_priv *priv = dev_get_priv(dev);
579 	struct zynq_gem_regs *regs = priv->iobase;
580 
581 	clrsetbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK |
582 						ZYNQ_GEM_NWCTRL_TXEN_MASK, 0);
583 }
584 
585 __weak int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
586 {
587 	return -ENOSYS;
588 }
589 
590 static int zynq_gem_read_rom_mac(struct udevice *dev)
591 {
592 	struct eth_pdata *pdata = dev_get_platdata(dev);
593 
594 	if (!pdata)
595 		return -ENOSYS;
596 
597 	return zynq_board_read_rom_ethaddr(pdata->enetaddr);
598 }
599 
600 static int zynq_gem_miiphy_read(struct mii_dev *bus, int addr,
601 				int devad, int reg)
602 {
603 	struct zynq_gem_priv *priv = bus->priv;
604 	int ret;
605 	u16 val;
606 
607 	ret = phyread(priv, addr, reg, &val);
608 	debug("%s 0x%x, 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, val, ret);
609 	return val;
610 }
611 
612 static int zynq_gem_miiphy_write(struct mii_dev *bus, int addr, int devad,
613 				 int reg, u16 value)
614 {
615 	struct zynq_gem_priv *priv = bus->priv;
616 
617 	debug("%s 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, value);
618 	return phywrite(priv, addr, reg, value);
619 }
620 
621 static int zynq_gem_probe(struct udevice *dev)
622 {
623 	void *bd_space;
624 	struct zynq_gem_priv *priv = dev_get_priv(dev);
625 	int ret;
626 
627 	/* Align rxbuffers to ARCH_DMA_MINALIGN */
628 	priv->rxbuffers = memalign(ARCH_DMA_MINALIGN, RX_BUF * PKTSIZE_ALIGN);
629 	memset(priv->rxbuffers, 0, RX_BUF * PKTSIZE_ALIGN);
630 
631 	/* Align bd_space to MMU_SECTION_SHIFT */
632 	bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
633 	mmu_set_region_dcache_behaviour((phys_addr_t)bd_space,
634 					BD_SPACE, DCACHE_OFF);
635 
636 	/* Initialize the bd spaces for tx and rx bd's */
637 	priv->tx_bd = (struct emac_bd *)bd_space;
638 	priv->rx_bd = (struct emac_bd *)((ulong)bd_space + BD_SEPRN_SPACE);
639 
640 	ret = clk_get_by_name(dev, "tx_clk", &priv->clk);
641 	if (ret < 0) {
642 		dev_err(dev, "failed to get clock\n");
643 		return -EINVAL;
644 	}
645 
646 	priv->bus = mdio_alloc();
647 	priv->bus->read = zynq_gem_miiphy_read;
648 	priv->bus->write = zynq_gem_miiphy_write;
649 	priv->bus->priv = priv;
650 
651 	ret = mdio_register_seq(priv->bus, dev->seq);
652 	if (ret)
653 		return ret;
654 
655 	return zynq_phy_init(dev);
656 }
657 
658 static int zynq_gem_remove(struct udevice *dev)
659 {
660 	struct zynq_gem_priv *priv = dev_get_priv(dev);
661 
662 	free(priv->phydev);
663 	mdio_unregister(priv->bus);
664 	mdio_free(priv->bus);
665 
666 	return 0;
667 }
668 
669 static const struct eth_ops zynq_gem_ops = {
670 	.start			= zynq_gem_init,
671 	.send			= zynq_gem_send,
672 	.recv			= zynq_gem_recv,
673 	.free_pkt		= zynq_gem_free_pkt,
674 	.stop			= zynq_gem_halt,
675 	.write_hwaddr		= zynq_gem_setup_mac,
676 	.read_rom_hwaddr	= zynq_gem_read_rom_mac,
677 };
678 
679 static int zynq_gem_ofdata_to_platdata(struct udevice *dev)
680 {
681 	struct eth_pdata *pdata = dev_get_platdata(dev);
682 	struct zynq_gem_priv *priv = dev_get_priv(dev);
683 	int node = dev_of_offset(dev);
684 	const char *phy_mode;
685 
686 	pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
687 	priv->iobase = (struct zynq_gem_regs *)pdata->iobase;
688 	/* Hardcode for now */
689 	priv->phyaddr = -1;
690 
691 	priv->phy_of_handle = fdtdec_lookup_phandle(gd->fdt_blob, node,
692 						    "phy-handle");
693 	if (priv->phy_of_handle > 0)
694 		priv->phyaddr = fdtdec_get_int(gd->fdt_blob,
695 					priv->phy_of_handle, "reg", -1);
696 
697 	phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
698 	if (phy_mode)
699 		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
700 	if (pdata->phy_interface == -1) {
701 		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
702 		return -EINVAL;
703 	}
704 	priv->interface = pdata->phy_interface;
705 
706 	priv->int_pcs = fdtdec_get_bool(gd->fdt_blob, node,
707 					"is-internal-pcspma");
708 
709 	printf("ZYNQ GEM: %lx, phyaddr %x, interface %s\n", (ulong)priv->iobase,
710 	       priv->phyaddr, phy_string_for_interface(priv->interface));
711 
712 	return 0;
713 }
714 
715 static const struct udevice_id zynq_gem_ids[] = {
716 	{ .compatible = "cdns,zynqmp-gem" },
717 	{ .compatible = "cdns,zynq-gem" },
718 	{ .compatible = "cdns,gem" },
719 	{ }
720 };
721 
722 U_BOOT_DRIVER(zynq_gem) = {
723 	.name	= "zynq_gem",
724 	.id	= UCLASS_ETH,
725 	.of_match = zynq_gem_ids,
726 	.ofdata_to_platdata = zynq_gem_ofdata_to_platdata,
727 	.probe	= zynq_gem_probe,
728 	.remove	= zynq_gem_remove,
729 	.ops	= &zynq_gem_ops,
730 	.priv_auto_alloc_size = sizeof(struct zynq_gem_priv),
731 	.platdata_auto_alloc_size = sizeof(struct eth_pdata),
732 };
733