blob: 08056a4bac19bf78a483ac958929595f017d8194 [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>
14#include <config.h>
Michal Simekf88a6862014-02-24 11:16:30 +010015#include <fdtdec.h>
16#include <libfdt.h>
Michal Simek185f7d92012-09-13 20:23:34 +000017#include <malloc.h>
18#include <asm/io.h>
19#include <phy.h>
20#include <miiphy.h>
21#include <watchdog.h>
David Andrey01fbf312013-04-05 17:24:24 +020022#include <asm/arch/hardware.h>
Michal Simek80243522012-10-15 14:01:23 +020023#include <asm/arch/sys_proto.h>
Michal Simek185f7d92012-09-13 20:23:34 +000024
25#if !defined(CONFIG_PHYLIB)
26# error XILINX_GEM_ETHERNET requires PHYLIB
27#endif
28
29/* Bit/mask specification */
30#define ZYNQ_GEM_PHYMNTNC_OP_MASK 0x40020000 /* operation mask bits */
31#define ZYNQ_GEM_PHYMNTNC_OP_R_MASK 0x20000000 /* read operation */
32#define ZYNQ_GEM_PHYMNTNC_OP_W_MASK 0x10000000 /* write operation */
33#define ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK 23 /* Shift bits for PHYAD */
34#define ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK 18 /* Shift bits for PHREG */
35
36#define ZYNQ_GEM_RXBUF_EOF_MASK 0x00008000 /* End of frame. */
37#define ZYNQ_GEM_RXBUF_SOF_MASK 0x00004000 /* Start of frame. */
38#define ZYNQ_GEM_RXBUF_LEN_MASK 0x00003FFF /* Mask for length field */
39
40#define ZYNQ_GEM_RXBUF_WRAP_MASK 0x00000002 /* Wrap bit, last BD */
41#define ZYNQ_GEM_RXBUF_NEW_MASK 0x00000001 /* Used bit.. */
42#define ZYNQ_GEM_RXBUF_ADD_MASK 0xFFFFFFFC /* Mask for address */
43
44/* Wrap bit, last descriptor */
45#define ZYNQ_GEM_TXBUF_WRAP_MASK 0x40000000
46#define ZYNQ_GEM_TXBUF_LAST_MASK 0x00008000 /* Last buffer */
47
Michal Simek185f7d92012-09-13 20:23:34 +000048#define ZYNQ_GEM_NWCTRL_TXEN_MASK 0x00000008 /* Enable transmit */
49#define ZYNQ_GEM_NWCTRL_RXEN_MASK 0x00000004 /* Enable receive */
50#define ZYNQ_GEM_NWCTRL_MDEN_MASK 0x00000010 /* Enable MDIO port */
51#define ZYNQ_GEM_NWCTRL_STARTTX_MASK 0x00000200 /* Start tx (tx_go) */
52
Michal Simek80243522012-10-15 14:01:23 +020053#define ZYNQ_GEM_NWCFG_SPEED100 0x000000001 /* 100 Mbps operation */
54#define ZYNQ_GEM_NWCFG_SPEED1000 0x000000400 /* 1Gbps operation */
55#define ZYNQ_GEM_NWCFG_FDEN 0x000000002 /* Full Duplex mode */
56#define ZYNQ_GEM_NWCFG_FSREM 0x000020000 /* FCS removal */
Michal Simek185f7d92012-09-13 20:23:34 +000057#define ZYNQ_GEM_NWCFG_MDCCLKDIV 0x000080000 /* Div pclk by 32, 80MHz */
Michal Simek80243522012-10-15 14:01:23 +020058#define ZYNQ_GEM_NWCFG_MDCCLKDIV2 0x0000c0000 /* Div pclk by 48, 120MHz */
Michal Simek185f7d92012-09-13 20:23:34 +000059
Michal Simek80243522012-10-15 14:01:23 +020060#define ZYNQ_GEM_NWCFG_INIT (ZYNQ_GEM_NWCFG_FDEN | \
Michal Simek185f7d92012-09-13 20:23:34 +000061 ZYNQ_GEM_NWCFG_FSREM | \
62 ZYNQ_GEM_NWCFG_MDCCLKDIV)
63
64#define ZYNQ_GEM_NWSR_MDIOIDLE_MASK 0x00000004 /* PHY management idle */
65
66#define ZYNQ_GEM_DMACR_BLENGTH 0x00000004 /* INCR4 AHB bursts */
67/* Use full configured addressable space (8 Kb) */
68#define ZYNQ_GEM_DMACR_RXSIZE 0x00000300
69/* Use full configured addressable space (4 Kb) */
70#define ZYNQ_GEM_DMACR_TXSIZE 0x00000400
71/* Set with binary 00011000 to use 1536 byte(1*max length frame/buffer) */
72#define ZYNQ_GEM_DMACR_RXBUF 0x00180000
73
74#define ZYNQ_GEM_DMACR_INIT (ZYNQ_GEM_DMACR_BLENGTH | \
75 ZYNQ_GEM_DMACR_RXSIZE | \
76 ZYNQ_GEM_DMACR_TXSIZE | \
77 ZYNQ_GEM_DMACR_RXBUF)
78
Michal Simekf97d7e82013-04-22 14:41:09 +020079/* Use MII register 1 (MII status register) to detect PHY */
80#define PHY_DETECT_REG 1
81
82/* Mask used to verify certain PHY features (or register contents)
83 * in the register above:
84 * 0x1000: 10Mbps full duplex support
85 * 0x0800: 10Mbps half duplex support
86 * 0x0008: Auto-negotiation support
87 */
88#define PHY_DETECT_MASK 0x1808
89
Srikanth Thokalaa5144232013-11-08 22:55:48 +053090/* TX BD status masks */
91#define ZYNQ_GEM_TXBUF_FRMLEN_MASK 0x000007ff
92#define ZYNQ_GEM_TXBUF_EXHAUSTED 0x08000000
93#define ZYNQ_GEM_TXBUF_UNDERRUN 0x10000000
94
Soren Brinkmann97598fc2013-11-21 13:39:01 -080095/* Clock frequencies for different speeds */
96#define ZYNQ_GEM_FREQUENCY_10 2500000UL
97#define ZYNQ_GEM_FREQUENCY_100 25000000UL
98#define ZYNQ_GEM_FREQUENCY_1000 125000000UL
99
Michal Simek185f7d92012-09-13 20:23:34 +0000100/* Device registers */
101struct zynq_gem_regs {
102 u32 nwctrl; /* Network Control reg */
103 u32 nwcfg; /* Network Config reg */
104 u32 nwsr; /* Network Status reg */
105 u32 reserved1;
106 u32 dmacr; /* DMA Control reg */
107 u32 txsr; /* TX Status reg */
108 u32 rxqbase; /* RX Q Base address reg */
109 u32 txqbase; /* TX Q Base address reg */
110 u32 rxsr; /* RX Status reg */
111 u32 reserved2[2];
112 u32 idr; /* Interrupt Disable reg */
113 u32 reserved3;
114 u32 phymntnc; /* Phy Maintaince reg */
115 u32 reserved4[18];
116 u32 hashl; /* Hash Low address reg */
117 u32 hashh; /* Hash High address reg */
118#define LADDR_LOW 0
119#define LADDR_HIGH 1
120 u32 laddr[4][LADDR_HIGH + 1]; /* Specific1 addr low/high reg */
121 u32 match[4]; /* Type ID1 Match reg */
122 u32 reserved6[18];
123 u32 stat[44]; /* Octects transmitted Low reg - stat start */
124};
125
126/* BD descriptors */
127struct emac_bd {
128 u32 addr; /* Next descriptor pointer */
129 u32 status;
130};
131
132#define RX_BUF 3
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530133/* Page table entries are set to 1MB, or multiples of 1MB
134 * (not < 1MB). driver uses less bd's so use 1MB bdspace.
135 */
136#define BD_SPACE 0x100000
137/* BD separation space */
138#define BD_SEPRN_SPACE 64
Michal Simek185f7d92012-09-13 20:23:34 +0000139
140/* Initialized, rxbd_current, rx_first_buf must be 0 after init */
141struct zynq_gem_priv {
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530142 struct emac_bd *tx_bd;
143 struct emac_bd *rx_bd;
144 char *rxbuffers;
Michal Simek185f7d92012-09-13 20:23:34 +0000145 u32 rxbd_current;
146 u32 rx_first_buf;
147 int phyaddr;
David Andrey01fbf312013-04-05 17:24:24 +0200148 u32 emio;
Michal Simek05868752013-01-24 13:04:12 +0100149 int init;
Michal Simek185f7d92012-09-13 20:23:34 +0000150 struct phy_device *phydev;
151 struct mii_dev *bus;
152};
153
154static inline int mdio_wait(struct eth_device *dev)
155{
156 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
157 u32 timeout = 200;
158
159 /* Wait till MDIO interface is ready to accept a new transaction. */
160 while (--timeout) {
161 if (readl(&regs->nwsr) & ZYNQ_GEM_NWSR_MDIOIDLE_MASK)
162 break;
163 WATCHDOG_RESET();
164 }
165
166 if (!timeout) {
167 printf("%s: Timeout\n", __func__);
168 return 1;
169 }
170
171 return 0;
172}
173
174static u32 phy_setup_op(struct eth_device *dev, u32 phy_addr, u32 regnum,
175 u32 op, u16 *data)
176{
177 u32 mgtcr;
178 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
179
180 if (mdio_wait(dev))
181 return 1;
182
183 /* Construct mgtcr mask for the operation */
184 mgtcr = ZYNQ_GEM_PHYMNTNC_OP_MASK | op |
185 (phy_addr << ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK) |
186 (regnum << ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK) | *data;
187
188 /* Write mgtcr and wait for completion */
189 writel(mgtcr, &regs->phymntnc);
190
191 if (mdio_wait(dev))
192 return 1;
193
194 if (op == ZYNQ_GEM_PHYMNTNC_OP_R_MASK)
195 *data = readl(&regs->phymntnc);
196
197 return 0;
198}
199
200static u32 phyread(struct eth_device *dev, u32 phy_addr, u32 regnum, u16 *val)
201{
202 return phy_setup_op(dev, phy_addr, regnum,
203 ZYNQ_GEM_PHYMNTNC_OP_R_MASK, val);
204}
205
206static u32 phywrite(struct eth_device *dev, u32 phy_addr, u32 regnum, u16 data)
207{
208 return phy_setup_op(dev, phy_addr, regnum,
209 ZYNQ_GEM_PHYMNTNC_OP_W_MASK, &data);
210}
211
Michal Simekf97d7e82013-04-22 14:41:09 +0200212static void phy_detection(struct eth_device *dev)
213{
214 int i;
215 u16 phyreg;
216 struct zynq_gem_priv *priv = dev->priv;
217
218 if (priv->phyaddr != -1) {
219 phyread(dev, priv->phyaddr, PHY_DETECT_REG, &phyreg);
220 if ((phyreg != 0xFFFF) &&
221 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
222 /* Found a valid PHY address */
223 debug("Default phy address %d is valid\n",
224 priv->phyaddr);
225 return;
226 } else {
227 debug("PHY address is not setup correctly %d\n",
228 priv->phyaddr);
229 priv->phyaddr = -1;
230 }
231 }
232
233 debug("detecting phy address\n");
234 if (priv->phyaddr == -1) {
235 /* detect the PHY address */
236 for (i = 31; i >= 0; i--) {
237 phyread(dev, i, PHY_DETECT_REG, &phyreg);
238 if ((phyreg != 0xFFFF) &&
239 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
240 /* Found a valid PHY address */
241 priv->phyaddr = i;
242 debug("Found valid phy address, %d\n", i);
243 return;
244 }
245 }
246 }
247 printf("PHY is not detected\n");
248}
249
Michal Simek185f7d92012-09-13 20:23:34 +0000250static int zynq_gem_setup_mac(struct eth_device *dev)
251{
252 u32 i, macaddrlow, macaddrhigh;
253 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
254
255 /* Set the MAC bits [31:0] in BOT */
256 macaddrlow = dev->enetaddr[0];
257 macaddrlow |= dev->enetaddr[1] << 8;
258 macaddrlow |= dev->enetaddr[2] << 16;
259 macaddrlow |= dev->enetaddr[3] << 24;
260
261 /* Set MAC bits [47:32] in TOP */
262 macaddrhigh = dev->enetaddr[4];
263 macaddrhigh |= dev->enetaddr[5] << 8;
264
265 for (i = 0; i < 4; i++) {
266 writel(0, &regs->laddr[i][LADDR_LOW]);
267 writel(0, &regs->laddr[i][LADDR_HIGH]);
268 /* Do not use MATCHx register */
269 writel(0, &regs->match[i]);
270 }
271
272 writel(macaddrlow, &regs->laddr[0][LADDR_LOW]);
273 writel(macaddrhigh, &regs->laddr[0][LADDR_HIGH]);
274
275 return 0;
276}
277
278static int zynq_gem_init(struct eth_device *dev, bd_t * bis)
279{
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800280 u32 i;
281 unsigned long clk_rate = 0;
Michal Simek185f7d92012-09-13 20:23:34 +0000282 struct phy_device *phydev;
283 const u32 stat_size = (sizeof(struct zynq_gem_regs) -
284 offsetof(struct zynq_gem_regs, stat)) / 4;
285 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
286 struct zynq_gem_priv *priv = dev->priv;
287 const u32 supported = SUPPORTED_10baseT_Half |
288 SUPPORTED_10baseT_Full |
289 SUPPORTED_100baseT_Half |
290 SUPPORTED_100baseT_Full |
291 SUPPORTED_1000baseT_Half |
292 SUPPORTED_1000baseT_Full;
293
Michal Simek05868752013-01-24 13:04:12 +0100294 if (!priv->init) {
295 /* Disable all interrupts */
296 writel(0xFFFFFFFF, &regs->idr);
Michal Simek185f7d92012-09-13 20:23:34 +0000297
Michal Simek05868752013-01-24 13:04:12 +0100298 /* Disable the receiver & transmitter */
299 writel(0, &regs->nwctrl);
300 writel(0, &regs->txsr);
301 writel(0, &regs->rxsr);
302 writel(0, &regs->phymntnc);
Michal Simek185f7d92012-09-13 20:23:34 +0000303
Michal Simek05868752013-01-24 13:04:12 +0100304 /* Clear the Hash registers for the mac address
305 * pointed by AddressPtr
306 */
307 writel(0x0, &regs->hashl);
308 /* Write bits [63:32] in TOP */
309 writel(0x0, &regs->hashh);
Michal Simek185f7d92012-09-13 20:23:34 +0000310
Michal Simek05868752013-01-24 13:04:12 +0100311 /* Clear all counters */
312 for (i = 0; i <= stat_size; i++)
313 readl(&regs->stat[i]);
Michal Simek185f7d92012-09-13 20:23:34 +0000314
Michal Simek05868752013-01-24 13:04:12 +0100315 /* Setup RxBD space */
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530316 memset(priv->rx_bd, 0, RX_BUF * sizeof(struct emac_bd));
Michal Simek185f7d92012-09-13 20:23:34 +0000317
Michal Simek05868752013-01-24 13:04:12 +0100318 for (i = 0; i < RX_BUF; i++) {
319 priv->rx_bd[i].status = 0xF0000000;
320 priv->rx_bd[i].addr =
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530321 ((u32)(priv->rxbuffers) +
Michal Simek185f7d92012-09-13 20:23:34 +0000322 (i * PKTSIZE_ALIGN));
Michal Simek05868752013-01-24 13:04:12 +0100323 }
324 /* WRAP bit to last BD */
325 priv->rx_bd[--i].addr |= ZYNQ_GEM_RXBUF_WRAP_MASK;
326 /* Write RxBDs to IP */
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530327 writel((u32)priv->rx_bd, &regs->rxqbase);
Michal Simek185f7d92012-09-13 20:23:34 +0000328
Michal Simek05868752013-01-24 13:04:12 +0100329 /* Setup for DMA Configuration register */
330 writel(ZYNQ_GEM_DMACR_INIT, &regs->dmacr);
Michal Simek185f7d92012-09-13 20:23:34 +0000331
Michal Simek05868752013-01-24 13:04:12 +0100332 /* Setup for Network Control register, MDIO, Rx and Tx enable */
Michal Simek80243522012-10-15 14:01:23 +0200333 setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_MDEN_MASK);
Michal Simek185f7d92012-09-13 20:23:34 +0000334
Michal Simek05868752013-01-24 13:04:12 +0100335 priv->init++;
336 }
337
Michal Simekf97d7e82013-04-22 14:41:09 +0200338 phy_detection(dev);
339
Michal Simek185f7d92012-09-13 20:23:34 +0000340 /* interface - look at tsec */
Michal Simekc1a9fa42014-02-25 10:25:38 +0100341 phydev = phy_connect(priv->bus, priv->phyaddr, dev,
342 PHY_INTERFACE_MODE_MII);
Michal Simek185f7d92012-09-13 20:23:34 +0000343
Michal Simek80243522012-10-15 14:01:23 +0200344 phydev->supported = supported | ADVERTISED_Pause |
345 ADVERTISED_Asym_Pause;
Michal Simek185f7d92012-09-13 20:23:34 +0000346 phydev->advertising = phydev->supported;
347 priv->phydev = phydev;
348 phy_config(phydev);
349 phy_startup(phydev);
350
Michal Simek4ed4aa22013-11-12 14:25:29 +0100351 if (!phydev->link) {
352 printf("%s: No link.\n", phydev->dev->name);
353 return -1;
354 }
355
Michal Simek80243522012-10-15 14:01:23 +0200356 switch (phydev->speed) {
357 case SPEED_1000:
358 writel(ZYNQ_GEM_NWCFG_INIT | ZYNQ_GEM_NWCFG_SPEED1000,
359 &regs->nwcfg);
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800360 clk_rate = ZYNQ_GEM_FREQUENCY_1000;
Michal Simek80243522012-10-15 14:01:23 +0200361 break;
362 case SPEED_100:
363 clrsetbits_le32(&regs->nwcfg, ZYNQ_GEM_NWCFG_SPEED1000,
364 ZYNQ_GEM_NWCFG_INIT | ZYNQ_GEM_NWCFG_SPEED100);
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800365 clk_rate = ZYNQ_GEM_FREQUENCY_100;
Michal Simek80243522012-10-15 14:01:23 +0200366 break;
367 case SPEED_10:
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800368 clk_rate = ZYNQ_GEM_FREQUENCY_10;
Michal Simek80243522012-10-15 14:01:23 +0200369 break;
370 }
David Andrey01fbf312013-04-05 17:24:24 +0200371
372 /* Change the rclk and clk only not using EMIO interface */
373 if (!priv->emio)
374 zynq_slcr_gem_clk_setup(dev->iobase !=
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800375 ZYNQ_GEM_BASEADDR0, clk_rate);
Michal Simek80243522012-10-15 14:01:23 +0200376
377 setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK |
378 ZYNQ_GEM_NWCTRL_TXEN_MASK);
379
Michal Simek185f7d92012-09-13 20:23:34 +0000380 return 0;
381}
382
383static int zynq_gem_send(struct eth_device *dev, void *ptr, int len)
384{
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530385 u32 addr, size;
Michal Simek185f7d92012-09-13 20:23:34 +0000386 struct zynq_gem_priv *priv = dev->priv;
387 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
Michal Simek185f7d92012-09-13 20:23:34 +0000388
389 /* setup BD */
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530390 writel((u32)priv->tx_bd, &regs->txqbase);
Michal Simek185f7d92012-09-13 20:23:34 +0000391
392 /* Setup Tx BD */
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530393 memset(priv->tx_bd, 0, sizeof(struct emac_bd));
Michal Simek185f7d92012-09-13 20:23:34 +0000394
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530395 priv->tx_bd->addr = (u32)ptr;
396 priv->tx_bd->status = (len & ZYNQ_GEM_TXBUF_FRMLEN_MASK) |
397 ZYNQ_GEM_TXBUF_LAST_MASK;
398
399 addr = (u32) ptr;
400 addr &= ~(ARCH_DMA_MINALIGN - 1);
401 size = roundup(len, ARCH_DMA_MINALIGN);
402 flush_dcache_range(addr, addr + size);
403 barrier();
Michal Simek185f7d92012-09-13 20:23:34 +0000404
405 /* Start transmit */
406 setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_STARTTX_MASK);
407
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530408 /* Read TX BD status */
409 if (priv->tx_bd->status & ZYNQ_GEM_TXBUF_UNDERRUN)
410 printf("TX underrun\n");
411 if (priv->tx_bd->status & ZYNQ_GEM_TXBUF_EXHAUSTED)
412 printf("TX buffers exhausted in mid frame\n");
Michal Simek185f7d92012-09-13 20:23:34 +0000413
Michal Simek185f7d92012-09-13 20:23:34 +0000414 return 0;
415}
416
417/* Do not check frame_recd flag in rx_status register 0x20 - just poll BD */
418static int zynq_gem_recv(struct eth_device *dev)
419{
420 int frame_len;
421 struct zynq_gem_priv *priv = dev->priv;
422 struct emac_bd *current_bd = &priv->rx_bd[priv->rxbd_current];
423 struct emac_bd *first_bd;
424
425 if (!(current_bd->addr & ZYNQ_GEM_RXBUF_NEW_MASK))
426 return 0;
427
428 if (!(current_bd->status &
429 (ZYNQ_GEM_RXBUF_SOF_MASK | ZYNQ_GEM_RXBUF_EOF_MASK))) {
430 printf("GEM: SOF or EOF not set for last buffer received!\n");
431 return 0;
432 }
433
434 frame_len = current_bd->status & ZYNQ_GEM_RXBUF_LEN_MASK;
435 if (frame_len) {
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530436 u32 addr = current_bd->addr & ZYNQ_GEM_RXBUF_ADD_MASK;
437 addr &= ~(ARCH_DMA_MINALIGN - 1);
438 u32 size = roundup(frame_len, ARCH_DMA_MINALIGN);
439 invalidate_dcache_range(addr, addr + size);
440
441 NetReceive((u8 *)addr, frame_len);
Michal Simek185f7d92012-09-13 20:23:34 +0000442
443 if (current_bd->status & ZYNQ_GEM_RXBUF_SOF_MASK)
444 priv->rx_first_buf = priv->rxbd_current;
445 else {
446 current_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK;
447 current_bd->status = 0xF0000000; /* FIXME */
448 }
449
450 if (current_bd->status & ZYNQ_GEM_RXBUF_EOF_MASK) {
451 first_bd = &priv->rx_bd[priv->rx_first_buf];
452 first_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK;
453 first_bd->status = 0xF0000000;
454 }
455
456 if ((++priv->rxbd_current) >= RX_BUF)
457 priv->rxbd_current = 0;
Michal Simek185f7d92012-09-13 20:23:34 +0000458 }
459
Michal Simek3b90d0a2013-01-25 08:24:18 +0100460 return frame_len;
Michal Simek185f7d92012-09-13 20:23:34 +0000461}
462
463static void zynq_gem_halt(struct eth_device *dev)
464{
465 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
466
Michal Simek80243522012-10-15 14:01:23 +0200467 clrsetbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK |
468 ZYNQ_GEM_NWCTRL_TXEN_MASK, 0);
Michal Simek185f7d92012-09-13 20:23:34 +0000469}
470
471static int zynq_gem_miiphyread(const char *devname, uchar addr,
472 uchar reg, ushort *val)
473{
474 struct eth_device *dev = eth_get_dev();
475 int ret;
476
477 ret = phyread(dev, addr, reg, val);
478 debug("%s 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, *val);
479 return ret;
480}
481
482static int zynq_gem_miiphy_write(const char *devname, uchar addr,
483 uchar reg, ushort val)
484{
485 struct eth_device *dev = eth_get_dev();
486
487 debug("%s 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, val);
488 return phywrite(dev, addr, reg, val);
489}
490
David Andrey01fbf312013-04-05 17:24:24 +0200491int zynq_gem_initialize(bd_t *bis, int base_addr, int phy_addr, u32 emio)
Michal Simek185f7d92012-09-13 20:23:34 +0000492{
493 struct eth_device *dev;
494 struct zynq_gem_priv *priv;
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530495 void *bd_space;
Michal Simek185f7d92012-09-13 20:23:34 +0000496
497 dev = calloc(1, sizeof(*dev));
498 if (dev == NULL)
499 return -1;
500
501 dev->priv = calloc(1, sizeof(struct zynq_gem_priv));
502 if (dev->priv == NULL) {
503 free(dev);
504 return -1;
505 }
506 priv = dev->priv;
507
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530508 /* Align rxbuffers to ARCH_DMA_MINALIGN */
509 priv->rxbuffers = memalign(ARCH_DMA_MINALIGN, RX_BUF * PKTSIZE_ALIGN);
510 memset(priv->rxbuffers, 0, RX_BUF * PKTSIZE_ALIGN);
511
512 /* Align bd_space to 1MB */
513 bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
514 mmu_set_region_dcache_behaviour((u32)bd_space, BD_SPACE, DCACHE_OFF);
515
516 /* Initialize the bd spaces for tx and rx bd's */
517 priv->tx_bd = (struct emac_bd *)bd_space;
518 priv->rx_bd = (struct emac_bd *)((u32)bd_space + BD_SEPRN_SPACE);
519
David Andrey117cd4c2013-04-04 19:13:07 +0200520 priv->phyaddr = phy_addr;
David Andrey01fbf312013-04-05 17:24:24 +0200521 priv->emio = emio;
Michal Simek185f7d92012-09-13 20:23:34 +0000522
523 sprintf(dev->name, "Gem.%x", base_addr);
524
525 dev->iobase = base_addr;
526
527 dev->init = zynq_gem_init;
528 dev->halt = zynq_gem_halt;
529 dev->send = zynq_gem_send;
530 dev->recv = zynq_gem_recv;
531 dev->write_hwaddr = zynq_gem_setup_mac;
532
533 eth_register(dev);
534
535 miiphy_register(dev->name, zynq_gem_miiphyread, zynq_gem_miiphy_write);
536 priv->bus = miiphy_get_dev_by_name(dev->name);
537
538 return 1;
539}
Michal Simekf88a6862014-02-24 11:16:30 +0100540
541#ifdef CONFIG_OF_CONTROL
542int zynq_gem_of_init(const void *blob)
543{
544 int offset = 0;
545 u32 ret = 0;
546 u32 reg, phy_reg;
547
548 debug("ZYNQ GEM: Initialization\n");
549
550 do {
551 offset = fdt_node_offset_by_compatible(blob, offset,
552 "xlnx,ps7-ethernet-1.00.a");
553 if (offset != -1) {
554 reg = fdtdec_get_addr(blob, offset, "reg");
555 if (reg != FDT_ADDR_T_NONE) {
556 offset = fdtdec_lookup_phandle(blob, offset,
557 "phy-handle");
558 if (offset != -1)
559 phy_reg = fdtdec_get_addr(blob, offset,
560 "reg");
561 else
562 phy_reg = 0;
563
564 debug("ZYNQ GEM: addr %x, phyaddr %x\n",
565 reg, phy_reg);
566
567 ret |= zynq_gem_initialize(NULL, reg,
568 phy_reg, 0);
569
570 } else {
571 debug("ZYNQ GEM: Can't get base address\n");
572 return -1;
573 }
574 }
575 } while (offset != -1);
576
577 return ret;
578}
579#endif