blob: fae61a0fa07d47e326054cdbd79a1a4083aa0c2f [file] [log] [blame]
Michal Simek185f7d92012-09-13 20:23:34 +00001/*
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 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02009 * SPDX-License-Identifier: GPL-2.0+
Michal Simek185f7d92012-09-13 20:23:34 +000010 */
11
12#include <common.h>
13#include <net.h>
Michal Simek2fd24892014-04-25 14:17:38 +020014#include <netdev.h>
Michal Simek185f7d92012-09-13 20:23:34 +000015#include <config.h>
Michal Simekf88a6862014-02-24 11:16:30 +010016#include <fdtdec.h>
17#include <libfdt.h>
Michal Simek185f7d92012-09-13 20:23:34 +000018#include <malloc.h>
19#include <asm/io.h>
20#include <phy.h>
21#include <miiphy.h>
22#include <watchdog.h>
Siva Durga Prasad Paladugu96f4f142014-12-06 12:57:53 +053023#include <asm/system.h>
David Andrey01fbf312013-04-05 17:24:24 +020024#include <asm/arch/hardware.h>
Michal Simek80243522012-10-15 14:01:23 +020025#include <asm/arch/sys_proto.h>
Michal Simek185f7d92012-09-13 20:23:34 +000026
27#if !defined(CONFIG_PHYLIB)
28# error XILINX_GEM_ETHERNET requires PHYLIB
29#endif
30
31/* Bit/mask specification */
32#define ZYNQ_GEM_PHYMNTNC_OP_MASK 0x40020000 /* operation mask bits */
33#define ZYNQ_GEM_PHYMNTNC_OP_R_MASK 0x20000000 /* read operation */
34#define ZYNQ_GEM_PHYMNTNC_OP_W_MASK 0x10000000 /* write operation */
35#define ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK 23 /* Shift bits for PHYAD */
36#define ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK 18 /* Shift bits for PHREG */
37
38#define ZYNQ_GEM_RXBUF_EOF_MASK 0x00008000 /* End of frame. */
39#define ZYNQ_GEM_RXBUF_SOF_MASK 0x00004000 /* Start of frame. */
40#define ZYNQ_GEM_RXBUF_LEN_MASK 0x00003FFF /* Mask for length field */
41
42#define ZYNQ_GEM_RXBUF_WRAP_MASK 0x00000002 /* Wrap bit, last BD */
43#define ZYNQ_GEM_RXBUF_NEW_MASK 0x00000001 /* Used bit.. */
44#define ZYNQ_GEM_RXBUF_ADD_MASK 0xFFFFFFFC /* Mask for address */
45
46/* Wrap bit, last descriptor */
47#define ZYNQ_GEM_TXBUF_WRAP_MASK 0x40000000
48#define ZYNQ_GEM_TXBUF_LAST_MASK 0x00008000 /* Last buffer */
49
Michal Simek185f7d92012-09-13 20:23:34 +000050#define ZYNQ_GEM_NWCTRL_TXEN_MASK 0x00000008 /* Enable transmit */
51#define ZYNQ_GEM_NWCTRL_RXEN_MASK 0x00000004 /* Enable receive */
52#define ZYNQ_GEM_NWCTRL_MDEN_MASK 0x00000010 /* Enable MDIO port */
53#define ZYNQ_GEM_NWCTRL_STARTTX_MASK 0x00000200 /* Start tx (tx_go) */
54
Michal Simek80243522012-10-15 14:01:23 +020055#define ZYNQ_GEM_NWCFG_SPEED100 0x000000001 /* 100 Mbps operation */
56#define ZYNQ_GEM_NWCFG_SPEED1000 0x000000400 /* 1Gbps operation */
57#define ZYNQ_GEM_NWCFG_FDEN 0x000000002 /* Full Duplex mode */
58#define ZYNQ_GEM_NWCFG_FSREM 0x000020000 /* FCS removal */
Michal Simek185f7d92012-09-13 20:23:34 +000059#define ZYNQ_GEM_NWCFG_MDCCLKDIV 0x000080000 /* Div pclk by 32, 80MHz */
Michal Simek80243522012-10-15 14:01:23 +020060#define ZYNQ_GEM_NWCFG_MDCCLKDIV2 0x0000c0000 /* Div pclk by 48, 120MHz */
Michal Simek185f7d92012-09-13 20:23:34 +000061
Siva Durga Prasad Paladugu8a584c82014-07-08 15:31:03 +053062#ifdef CONFIG_ARM64
63# define ZYNQ_GEM_DBUS_WIDTH (1 << 21) /* 64 bit bus */
64#else
65# define ZYNQ_GEM_DBUS_WIDTH (0 << 21) /* 32 bit bus */
66#endif
67
68#define ZYNQ_GEM_NWCFG_INIT (ZYNQ_GEM_DBUS_WIDTH | \
69 ZYNQ_GEM_NWCFG_FDEN | \
Michal Simek185f7d92012-09-13 20:23:34 +000070 ZYNQ_GEM_NWCFG_FSREM | \
71 ZYNQ_GEM_NWCFG_MDCCLKDIV)
72
73#define ZYNQ_GEM_NWSR_MDIOIDLE_MASK 0x00000004 /* PHY management idle */
74
75#define ZYNQ_GEM_DMACR_BLENGTH 0x00000004 /* INCR4 AHB bursts */
76/* Use full configured addressable space (8 Kb) */
77#define ZYNQ_GEM_DMACR_RXSIZE 0x00000300
78/* Use full configured addressable space (4 Kb) */
79#define ZYNQ_GEM_DMACR_TXSIZE 0x00000400
80/* Set with binary 00011000 to use 1536 byte(1*max length frame/buffer) */
81#define ZYNQ_GEM_DMACR_RXBUF 0x00180000
82
83#define ZYNQ_GEM_DMACR_INIT (ZYNQ_GEM_DMACR_BLENGTH | \
84 ZYNQ_GEM_DMACR_RXSIZE | \
85 ZYNQ_GEM_DMACR_TXSIZE | \
86 ZYNQ_GEM_DMACR_RXBUF)
87
Michal Simekf97d7e82013-04-22 14:41:09 +020088/* Use MII register 1 (MII status register) to detect PHY */
89#define PHY_DETECT_REG 1
90
91/* Mask used to verify certain PHY features (or register contents)
92 * in the register above:
93 * 0x1000: 10Mbps full duplex support
94 * 0x0800: 10Mbps half duplex support
95 * 0x0008: Auto-negotiation support
96 */
97#define PHY_DETECT_MASK 0x1808
98
Srikanth Thokalaa5144232013-11-08 22:55:48 +053099/* TX BD status masks */
100#define ZYNQ_GEM_TXBUF_FRMLEN_MASK 0x000007ff
101#define ZYNQ_GEM_TXBUF_EXHAUSTED 0x08000000
102#define ZYNQ_GEM_TXBUF_UNDERRUN 0x10000000
103
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800104/* Clock frequencies for different speeds */
105#define ZYNQ_GEM_FREQUENCY_10 2500000UL
106#define ZYNQ_GEM_FREQUENCY_100 25000000UL
107#define ZYNQ_GEM_FREQUENCY_1000 125000000UL
108
Michal Simek185f7d92012-09-13 20:23:34 +0000109/* Device registers */
110struct zynq_gem_regs {
Michal Simek97a51a02015-10-05 11:49:43 +0200111 u32 nwctrl; /* 0x0 - Network Control reg */
112 u32 nwcfg; /* 0x4 - Network Config reg */
113 u32 nwsr; /* 0x8 - Network Status reg */
Michal Simek185f7d92012-09-13 20:23:34 +0000114 u32 reserved1;
Michal Simek97a51a02015-10-05 11:49:43 +0200115 u32 dmacr; /* 0x10 - DMA Control reg */
116 u32 txsr; /* 0x14 - TX Status reg */
117 u32 rxqbase; /* 0x18 - RX Q Base address reg */
118 u32 txqbase; /* 0x1c - TX Q Base address reg */
119 u32 rxsr; /* 0x20 - RX Status reg */
Michal Simek185f7d92012-09-13 20:23:34 +0000120 u32 reserved2[2];
Michal Simek97a51a02015-10-05 11:49:43 +0200121 u32 idr; /* 0x2c - Interrupt Disable reg */
Michal Simek185f7d92012-09-13 20:23:34 +0000122 u32 reserved3;
Michal Simek97a51a02015-10-05 11:49:43 +0200123 u32 phymntnc; /* 0x34 - Phy Maintaince reg */
Michal Simek185f7d92012-09-13 20:23:34 +0000124 u32 reserved4[18];
Michal Simek97a51a02015-10-05 11:49:43 +0200125 u32 hashl; /* 0x80 - Hash Low address reg */
126 u32 hashh; /* 0x84 - Hash High address reg */
Michal Simek185f7d92012-09-13 20:23:34 +0000127#define LADDR_LOW 0
128#define LADDR_HIGH 1
Michal Simek97a51a02015-10-05 11:49:43 +0200129 u32 laddr[4][LADDR_HIGH + 1]; /* 0x8c - Specific1 addr low/high reg */
130 u32 match[4]; /* 0xa8 - Type ID1 Match reg */
Michal Simek185f7d92012-09-13 20:23:34 +0000131 u32 reserved6[18];
Michal Simek97a51a02015-10-05 11:49:43 +0200132 u32 stat[44]; /* 0x100 - Octects transmitted Low reg - stat start */
Michal Simek185f7d92012-09-13 20:23:34 +0000133};
134
135/* BD descriptors */
136struct emac_bd {
137 u32 addr; /* Next descriptor pointer */
138 u32 status;
139};
140
Siva Durga Prasad Paladugueda9d302015-04-15 12:15:01 +0530141#define RX_BUF 32
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530142/* Page table entries are set to 1MB, or multiples of 1MB
143 * (not < 1MB). driver uses less bd's so use 1MB bdspace.
144 */
145#define BD_SPACE 0x100000
146/* BD separation space */
147#define BD_SEPRN_SPACE 64
Michal Simek185f7d92012-09-13 20:23:34 +0000148
149/* Initialized, rxbd_current, rx_first_buf must be 0 after init */
150struct zynq_gem_priv {
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530151 struct emac_bd *tx_bd;
152 struct emac_bd *rx_bd;
153 char *rxbuffers;
Michal Simek185f7d92012-09-13 20:23:34 +0000154 u32 rxbd_current;
155 u32 rx_first_buf;
156 int phyaddr;
David Andrey01fbf312013-04-05 17:24:24 +0200157 u32 emio;
Michal Simek05868752013-01-24 13:04:12 +0100158 int init;
Michal Simek16ce6de2015-10-07 16:42:56 +0200159 phy_interface_t interface;
Michal Simek185f7d92012-09-13 20:23:34 +0000160 struct phy_device *phydev;
161 struct mii_dev *bus;
162};
163
164static inline int mdio_wait(struct eth_device *dev)
165{
166 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
Michal Simek4c8b7bf2012-10-16 17:37:11 +0200167 u32 timeout = 20000;
Michal Simek185f7d92012-09-13 20:23:34 +0000168
169 /* Wait till MDIO interface is ready to accept a new transaction. */
170 while (--timeout) {
171 if (readl(&regs->nwsr) & ZYNQ_GEM_NWSR_MDIOIDLE_MASK)
172 break;
173 WATCHDOG_RESET();
174 }
175
176 if (!timeout) {
177 printf("%s: Timeout\n", __func__);
178 return 1;
179 }
180
181 return 0;
182}
183
184static u32 phy_setup_op(struct eth_device *dev, u32 phy_addr, u32 regnum,
185 u32 op, u16 *data)
186{
187 u32 mgtcr;
188 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
189
190 if (mdio_wait(dev))
191 return 1;
192
193 /* Construct mgtcr mask for the operation */
194 mgtcr = ZYNQ_GEM_PHYMNTNC_OP_MASK | op |
195 (phy_addr << ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK) |
196 (regnum << ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK) | *data;
197
198 /* Write mgtcr and wait for completion */
199 writel(mgtcr, &regs->phymntnc);
200
201 if (mdio_wait(dev))
202 return 1;
203
204 if (op == ZYNQ_GEM_PHYMNTNC_OP_R_MASK)
205 *data = readl(&regs->phymntnc);
206
207 return 0;
208}
209
210static u32 phyread(struct eth_device *dev, u32 phy_addr, u32 regnum, u16 *val)
211{
Michal Simek198e9a42015-10-07 16:34:51 +0200212 u32 ret;
213
214 ret = phy_setup_op(dev, phy_addr, regnum,
Michal Simek185f7d92012-09-13 20:23:34 +0000215 ZYNQ_GEM_PHYMNTNC_OP_R_MASK, val);
Michal Simek198e9a42015-10-07 16:34:51 +0200216
217 if (!ret)
218 debug("%s: phy_addr %d, regnum 0x%x, val 0x%x\n", __func__,
219 phy_addr, regnum, *val);
220
221 return ret;
Michal Simek185f7d92012-09-13 20:23:34 +0000222}
223
224static u32 phywrite(struct eth_device *dev, u32 phy_addr, u32 regnum, u16 data)
225{
Michal Simek198e9a42015-10-07 16:34:51 +0200226 debug("%s: phy_addr %d, regnum 0x%x, data 0x%x\n", __func__, phy_addr,
227 regnum, data);
228
Michal Simek185f7d92012-09-13 20:23:34 +0000229 return phy_setup_op(dev, phy_addr, regnum,
230 ZYNQ_GEM_PHYMNTNC_OP_W_MASK, &data);
231}
232
Michal Simekf97d7e82013-04-22 14:41:09 +0200233static void phy_detection(struct eth_device *dev)
234{
235 int i;
236 u16 phyreg;
237 struct zynq_gem_priv *priv = dev->priv;
238
239 if (priv->phyaddr != -1) {
240 phyread(dev, priv->phyaddr, PHY_DETECT_REG, &phyreg);
241 if ((phyreg != 0xFFFF) &&
242 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
243 /* Found a valid PHY address */
244 debug("Default phy address %d is valid\n",
245 priv->phyaddr);
246 return;
247 } else {
248 debug("PHY address is not setup correctly %d\n",
249 priv->phyaddr);
250 priv->phyaddr = -1;
251 }
252 }
253
254 debug("detecting phy address\n");
255 if (priv->phyaddr == -1) {
256 /* detect the PHY address */
257 for (i = 31; i >= 0; i--) {
258 phyread(dev, i, PHY_DETECT_REG, &phyreg);
259 if ((phyreg != 0xFFFF) &&
260 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
261 /* Found a valid PHY address */
262 priv->phyaddr = i;
263 debug("Found valid phy address, %d\n", i);
264 return;
265 }
266 }
267 }
268 printf("PHY is not detected\n");
269}
270
Michal Simek185f7d92012-09-13 20:23:34 +0000271static int zynq_gem_setup_mac(struct eth_device *dev)
272{
273 u32 i, macaddrlow, macaddrhigh;
274 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
275
276 /* Set the MAC bits [31:0] in BOT */
277 macaddrlow = dev->enetaddr[0];
278 macaddrlow |= dev->enetaddr[1] << 8;
279 macaddrlow |= dev->enetaddr[2] << 16;
280 macaddrlow |= dev->enetaddr[3] << 24;
281
282 /* Set MAC bits [47:32] in TOP */
283 macaddrhigh = dev->enetaddr[4];
284 macaddrhigh |= dev->enetaddr[5] << 8;
285
286 for (i = 0; i < 4; i++) {
287 writel(0, &regs->laddr[i][LADDR_LOW]);
288 writel(0, &regs->laddr[i][LADDR_HIGH]);
289 /* Do not use MATCHx register */
290 writel(0, &regs->match[i]);
291 }
292
293 writel(macaddrlow, &regs->laddr[0][LADDR_LOW]);
294 writel(macaddrhigh, &regs->laddr[0][LADDR_HIGH]);
295
296 return 0;
297}
298
299static int zynq_gem_init(struct eth_device *dev, bd_t * bis)
300{
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800301 u32 i;
302 unsigned long clk_rate = 0;
Michal Simek185f7d92012-09-13 20:23:34 +0000303 struct phy_device *phydev;
304 const u32 stat_size = (sizeof(struct zynq_gem_regs) -
305 offsetof(struct zynq_gem_regs, stat)) / 4;
306 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
307 struct zynq_gem_priv *priv = dev->priv;
308 const u32 supported = SUPPORTED_10baseT_Half |
309 SUPPORTED_10baseT_Full |
310 SUPPORTED_100baseT_Half |
311 SUPPORTED_100baseT_Full |
312 SUPPORTED_1000baseT_Half |
313 SUPPORTED_1000baseT_Full;
314
Michal Simek05868752013-01-24 13:04:12 +0100315 if (!priv->init) {
316 /* Disable all interrupts */
317 writel(0xFFFFFFFF, &regs->idr);
Michal Simek185f7d92012-09-13 20:23:34 +0000318
Michal Simek05868752013-01-24 13:04:12 +0100319 /* Disable the receiver & transmitter */
320 writel(0, &regs->nwctrl);
321 writel(0, &regs->txsr);
322 writel(0, &regs->rxsr);
323 writel(0, &regs->phymntnc);
Michal Simek185f7d92012-09-13 20:23:34 +0000324
Michal Simek05868752013-01-24 13:04:12 +0100325 /* Clear the Hash registers for the mac address
326 * pointed by AddressPtr
327 */
328 writel(0x0, &regs->hashl);
329 /* Write bits [63:32] in TOP */
330 writel(0x0, &regs->hashh);
Michal Simek185f7d92012-09-13 20:23:34 +0000331
Michal Simek05868752013-01-24 13:04:12 +0100332 /* Clear all counters */
333 for (i = 0; i <= stat_size; i++)
334 readl(&regs->stat[i]);
Michal Simek185f7d92012-09-13 20:23:34 +0000335
Michal Simek05868752013-01-24 13:04:12 +0100336 /* Setup RxBD space */
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530337 memset(priv->rx_bd, 0, RX_BUF * sizeof(struct emac_bd));
Michal Simek185f7d92012-09-13 20:23:34 +0000338
Michal Simek05868752013-01-24 13:04:12 +0100339 for (i = 0; i < RX_BUF; i++) {
340 priv->rx_bd[i].status = 0xF0000000;
341 priv->rx_bd[i].addr =
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530342 ((ulong)(priv->rxbuffers) +
Michal Simek185f7d92012-09-13 20:23:34 +0000343 (i * PKTSIZE_ALIGN));
Michal Simek05868752013-01-24 13:04:12 +0100344 }
345 /* WRAP bit to last BD */
346 priv->rx_bd[--i].addr |= ZYNQ_GEM_RXBUF_WRAP_MASK;
347 /* Write RxBDs to IP */
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530348 writel((ulong)priv->rx_bd, &regs->rxqbase);
Michal Simek185f7d92012-09-13 20:23:34 +0000349
Michal Simek05868752013-01-24 13:04:12 +0100350 /* Setup for DMA Configuration register */
351 writel(ZYNQ_GEM_DMACR_INIT, &regs->dmacr);
Michal Simek185f7d92012-09-13 20:23:34 +0000352
Michal Simek05868752013-01-24 13:04:12 +0100353 /* Setup for Network Control register, MDIO, Rx and Tx enable */
Michal Simek80243522012-10-15 14:01:23 +0200354 setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_MDEN_MASK);
Michal Simek185f7d92012-09-13 20:23:34 +0000355
Michal Simek05868752013-01-24 13:04:12 +0100356 priv->init++;
357 }
358
Michal Simekf97d7e82013-04-22 14:41:09 +0200359 phy_detection(dev);
360
Michal Simek185f7d92012-09-13 20:23:34 +0000361 /* interface - look at tsec */
Michal Simekc1a9fa42014-02-25 10:25:38 +0100362 phydev = phy_connect(priv->bus, priv->phyaddr, dev,
Michal Simek16ce6de2015-10-07 16:42:56 +0200363 priv->interface);
Michal Simek185f7d92012-09-13 20:23:34 +0000364
Michal Simek80243522012-10-15 14:01:23 +0200365 phydev->supported = supported | ADVERTISED_Pause |
366 ADVERTISED_Asym_Pause;
Michal Simek185f7d92012-09-13 20:23:34 +0000367 phydev->advertising = phydev->supported;
368 priv->phydev = phydev;
369 phy_config(phydev);
370 phy_startup(phydev);
371
Michal Simek4ed4aa22013-11-12 14:25:29 +0100372 if (!phydev->link) {
373 printf("%s: No link.\n", phydev->dev->name);
374 return -1;
375 }
376
Michal Simek80243522012-10-15 14:01:23 +0200377 switch (phydev->speed) {
378 case SPEED_1000:
379 writel(ZYNQ_GEM_NWCFG_INIT | ZYNQ_GEM_NWCFG_SPEED1000,
380 &regs->nwcfg);
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800381 clk_rate = ZYNQ_GEM_FREQUENCY_1000;
Michal Simek80243522012-10-15 14:01:23 +0200382 break;
383 case SPEED_100:
384 clrsetbits_le32(&regs->nwcfg, ZYNQ_GEM_NWCFG_SPEED1000,
385 ZYNQ_GEM_NWCFG_INIT | ZYNQ_GEM_NWCFG_SPEED100);
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800386 clk_rate = ZYNQ_GEM_FREQUENCY_100;
Michal Simek80243522012-10-15 14:01:23 +0200387 break;
388 case SPEED_10:
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800389 clk_rate = ZYNQ_GEM_FREQUENCY_10;
Michal Simek80243522012-10-15 14:01:23 +0200390 break;
391 }
David Andrey01fbf312013-04-05 17:24:24 +0200392
393 /* Change the rclk and clk only not using EMIO interface */
394 if (!priv->emio)
395 zynq_slcr_gem_clk_setup(dev->iobase !=
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800396 ZYNQ_GEM_BASEADDR0, clk_rate);
Michal Simek80243522012-10-15 14:01:23 +0200397
398 setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK |
399 ZYNQ_GEM_NWCTRL_TXEN_MASK);
400
Michal Simek185f7d92012-09-13 20:23:34 +0000401 return 0;
402}
403
404static int zynq_gem_send(struct eth_device *dev, void *ptr, int len)
405{
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530406 u32 addr, size;
Michal Simek185f7d92012-09-13 20:23:34 +0000407 struct zynq_gem_priv *priv = dev->priv;
408 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
Michal Simek185f7d92012-09-13 20:23:34 +0000409
410 /* setup BD */
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530411 writel((ulong)priv->tx_bd, &regs->txqbase);
Michal Simek185f7d92012-09-13 20:23:34 +0000412
413 /* Setup Tx BD */
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530414 memset(priv->tx_bd, 0, sizeof(struct emac_bd));
Michal Simek185f7d92012-09-13 20:23:34 +0000415
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530416 priv->tx_bd->addr = (ulong)ptr;
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530417 priv->tx_bd->status = (len & ZYNQ_GEM_TXBUF_FRMLEN_MASK) |
Michal Simeke65d33c2015-05-26 12:01:12 +0200418 ZYNQ_GEM_TXBUF_LAST_MASK |
419 ZYNQ_GEM_TXBUF_WRAP_MASK;
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530420
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530421 addr = (ulong) ptr;
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530422 addr &= ~(ARCH_DMA_MINALIGN - 1);
423 size = roundup(len, ARCH_DMA_MINALIGN);
424 flush_dcache_range(addr, addr + size);
Siva Durga Prasad Paladugu96f4f142014-12-06 12:57:53 +0530425
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530426 addr = (ulong)priv->rxbuffers;
Siva Durga Prasad Paladugu96f4f142014-12-06 12:57:53 +0530427 addr &= ~(ARCH_DMA_MINALIGN - 1);
428 size = roundup((RX_BUF * PKTSIZE_ALIGN), ARCH_DMA_MINALIGN);
429 flush_dcache_range(addr, addr + size);
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530430 barrier();
Michal Simek185f7d92012-09-13 20:23:34 +0000431
432 /* Start transmit */
433 setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_STARTTX_MASK);
434
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530435 /* Read TX BD status */
436 if (priv->tx_bd->status & ZYNQ_GEM_TXBUF_UNDERRUN)
437 printf("TX underrun\n");
438 if (priv->tx_bd->status & ZYNQ_GEM_TXBUF_EXHAUSTED)
439 printf("TX buffers exhausted in mid frame\n");
Michal Simek185f7d92012-09-13 20:23:34 +0000440
Michal Simek185f7d92012-09-13 20:23:34 +0000441 return 0;
442}
443
444/* Do not check frame_recd flag in rx_status register 0x20 - just poll BD */
445static int zynq_gem_recv(struct eth_device *dev)
446{
447 int frame_len;
448 struct zynq_gem_priv *priv = dev->priv;
449 struct emac_bd *current_bd = &priv->rx_bd[priv->rxbd_current];
450 struct emac_bd *first_bd;
451
452 if (!(current_bd->addr & ZYNQ_GEM_RXBUF_NEW_MASK))
453 return 0;
454
455 if (!(current_bd->status &
456 (ZYNQ_GEM_RXBUF_SOF_MASK | ZYNQ_GEM_RXBUF_EOF_MASK))) {
457 printf("GEM: SOF or EOF not set for last buffer received!\n");
458 return 0;
459 }
460
461 frame_len = current_bd->status & ZYNQ_GEM_RXBUF_LEN_MASK;
462 if (frame_len) {
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530463 u32 addr = current_bd->addr & ZYNQ_GEM_RXBUF_ADD_MASK;
464 addr &= ~(ARCH_DMA_MINALIGN - 1);
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530465
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530466 net_process_received_packet((u8 *)(ulong)addr, frame_len);
Michal Simek185f7d92012-09-13 20:23:34 +0000467
468 if (current_bd->status & ZYNQ_GEM_RXBUF_SOF_MASK)
469 priv->rx_first_buf = priv->rxbd_current;
470 else {
471 current_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK;
472 current_bd->status = 0xF0000000; /* FIXME */
473 }
474
475 if (current_bd->status & ZYNQ_GEM_RXBUF_EOF_MASK) {
476 first_bd = &priv->rx_bd[priv->rx_first_buf];
477 first_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK;
478 first_bd->status = 0xF0000000;
479 }
480
481 if ((++priv->rxbd_current) >= RX_BUF)
482 priv->rxbd_current = 0;
Michal Simek185f7d92012-09-13 20:23:34 +0000483 }
484
Michal Simek3b90d0a2013-01-25 08:24:18 +0100485 return frame_len;
Michal Simek185f7d92012-09-13 20:23:34 +0000486}
487
488static void zynq_gem_halt(struct eth_device *dev)
489{
490 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
491
Michal Simek80243522012-10-15 14:01:23 +0200492 clrsetbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK |
493 ZYNQ_GEM_NWCTRL_TXEN_MASK, 0);
Michal Simek185f7d92012-09-13 20:23:34 +0000494}
495
496static int zynq_gem_miiphyread(const char *devname, uchar addr,
497 uchar reg, ushort *val)
498{
499 struct eth_device *dev = eth_get_dev();
500 int ret;
501
502 ret = phyread(dev, addr, reg, val);
503 debug("%s 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, *val);
504 return ret;
505}
506
507static int zynq_gem_miiphy_write(const char *devname, uchar addr,
508 uchar reg, ushort val)
509{
510 struct eth_device *dev = eth_get_dev();
511
512 debug("%s 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, val);
513 return phywrite(dev, addr, reg, val);
514}
515
Michal Simek58405372015-01-14 15:44:21 +0100516int zynq_gem_initialize(bd_t *bis, phys_addr_t base_addr,
517 int phy_addr, u32 emio)
Michal Simek185f7d92012-09-13 20:23:34 +0000518{
519 struct eth_device *dev;
520 struct zynq_gem_priv *priv;
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530521 void *bd_space;
Michal Simek185f7d92012-09-13 20:23:34 +0000522
523 dev = calloc(1, sizeof(*dev));
524 if (dev == NULL)
525 return -1;
526
527 dev->priv = calloc(1, sizeof(struct zynq_gem_priv));
528 if (dev->priv == NULL) {
529 free(dev);
530 return -1;
531 }
532 priv = dev->priv;
533
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530534 /* Align rxbuffers to ARCH_DMA_MINALIGN */
535 priv->rxbuffers = memalign(ARCH_DMA_MINALIGN, RX_BUF * PKTSIZE_ALIGN);
536 memset(priv->rxbuffers, 0, RX_BUF * PKTSIZE_ALIGN);
537
Siva Durga Prasad Paladugu96f4f142014-12-06 12:57:53 +0530538 /* Align bd_space to MMU_SECTION_SHIFT */
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530539 bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
Michal Simek9ce1edc2015-04-15 13:31:28 +0200540 mmu_set_region_dcache_behaviour((phys_addr_t)bd_space,
541 BD_SPACE, DCACHE_OFF);
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530542
543 /* Initialize the bd spaces for tx and rx bd's */
544 priv->tx_bd = (struct emac_bd *)bd_space;
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530545 priv->rx_bd = (struct emac_bd *)((ulong)bd_space + BD_SEPRN_SPACE);
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530546
David Andrey117cd4c2013-04-04 19:13:07 +0200547 priv->phyaddr = phy_addr;
David Andrey01fbf312013-04-05 17:24:24 +0200548 priv->emio = emio;
Michal Simek185f7d92012-09-13 20:23:34 +0000549
Michal Simek16ce6de2015-10-07 16:42:56 +0200550#ifndef CONFIG_ZYNQ_GEM_INTERFACE
551 priv->interface = PHY_INTERFACE_MODE_MII;
552#else
553 priv->interface = CONFIG_ZYNQ_GEM_INTERFACE;
554#endif
555
Michal Simek58405372015-01-14 15:44:21 +0100556 sprintf(dev->name, "Gem.%lx", base_addr);
Michal Simek185f7d92012-09-13 20:23:34 +0000557
558 dev->iobase = base_addr;
559
560 dev->init = zynq_gem_init;
561 dev->halt = zynq_gem_halt;
562 dev->send = zynq_gem_send;
563 dev->recv = zynq_gem_recv;
564 dev->write_hwaddr = zynq_gem_setup_mac;
565
566 eth_register(dev);
567
568 miiphy_register(dev->name, zynq_gem_miiphyread, zynq_gem_miiphy_write);
569 priv->bus = miiphy_get_dev_by_name(dev->name);
570
571 return 1;
572}
Michal Simekf88a6862014-02-24 11:16:30 +0100573
Masahiro Yamada0f925822015-08-12 07:31:55 +0900574#if CONFIG_IS_ENABLED(OF_CONTROL)
Michal Simekf88a6862014-02-24 11:16:30 +0100575int zynq_gem_of_init(const void *blob)
576{
577 int offset = 0;
578 u32 ret = 0;
579 u32 reg, phy_reg;
580
581 debug("ZYNQ GEM: Initialization\n");
582
583 do {
584 offset = fdt_node_offset_by_compatible(blob, offset,
585 "xlnx,ps7-ethernet-1.00.a");
586 if (offset != -1) {
587 reg = fdtdec_get_addr(blob, offset, "reg");
588 if (reg != FDT_ADDR_T_NONE) {
589 offset = fdtdec_lookup_phandle(blob, offset,
590 "phy-handle");
591 if (offset != -1)
592 phy_reg = fdtdec_get_addr(blob, offset,
593 "reg");
594 else
595 phy_reg = 0;
596
597 debug("ZYNQ GEM: addr %x, phyaddr %x\n",
598 reg, phy_reg);
599
600 ret |= zynq_gem_initialize(NULL, reg,
601 phy_reg, 0);
602
603 } else {
604 debug("ZYNQ GEM: Can't get base address\n");
605 return -1;
606 }
607 }
608 } while (offset != -1);
609
610 return ret;
611}
612#endif