09151986f15dff00ac1520e3483591110b7773f6
[oweals/u-boot.git] / drivers / net / xilinx_axi_emac.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2011 Michal Simek <monstr@monstr.eu>
4  * Copyright (C) 2011 PetaLogix
5  * Copyright (C) 2010 Xilinx, Inc. All rights reserved.
6  */
7
8 #include <config.h>
9 #include <common.h>
10 #include <cpu_func.h>
11 #include <dm.h>
12 #include <log.h>
13 #include <net.h>
14 #include <malloc.h>
15 #include <asm/io.h>
16 #include <phy.h>
17 #include <miiphy.h>
18 #include <wait_bit.h>
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 /* Link setup */
23 #define XAE_EMMC_LINKSPEED_MASK 0xC0000000 /* Link speed */
24 #define XAE_EMMC_LINKSPD_10     0x00000000 /* Link Speed mask for 10 Mbit */
25 #define XAE_EMMC_LINKSPD_100    0x40000000 /* Link Speed mask for 100 Mbit */
26 #define XAE_EMMC_LINKSPD_1000   0x80000000 /* Link Speed mask for 1000 Mbit */
27
28 /* Interrupt Status/Enable/Mask Registers bit definitions */
29 #define XAE_INT_RXRJECT_MASK    0x00000008 /* Rx frame rejected */
30 #define XAE_INT_MGTRDY_MASK     0x00000080 /* MGT clock Lock */
31
32 /* Receive Configuration Word 1 (RCW1) Register bit definitions */
33 #define XAE_RCW1_RX_MASK        0x10000000 /* Receiver enable */
34
35 /* Transmitter Configuration (TC) Register bit definitions */
36 #define XAE_TC_TX_MASK          0x10000000 /* Transmitter enable */
37
38 #define XAE_UAW1_UNICASTADDR_MASK       0x0000FFFF
39
40 /* MDIO Management Configuration (MC) Register bit definitions */
41 #define XAE_MDIO_MC_MDIOEN_MASK         0x00000040 /* MII management enable*/
42
43 /* MDIO Management Control Register (MCR) Register bit definitions */
44 #define XAE_MDIO_MCR_PHYAD_MASK         0x1F000000 /* Phy Address Mask */
45 #define XAE_MDIO_MCR_PHYAD_SHIFT        24         /* Phy Address Shift */
46 #define XAE_MDIO_MCR_REGAD_MASK         0x001F0000 /* Reg Address Mask */
47 #define XAE_MDIO_MCR_REGAD_SHIFT        16         /* Reg Address Shift */
48 #define XAE_MDIO_MCR_OP_READ_MASK       0x00008000 /* Op Code Read Mask */
49 #define XAE_MDIO_MCR_OP_WRITE_MASK      0x00004000 /* Op Code Write Mask */
50 #define XAE_MDIO_MCR_INITIATE_MASK      0x00000800 /* Ready Mask */
51 #define XAE_MDIO_MCR_READY_MASK         0x00000080 /* Ready Mask */
52
53 #define XAE_MDIO_DIV_DFT        29      /* Default MDIO clock divisor */
54
55 #define XAXIDMA_BD_STS_ACTUAL_LEN_MASK  0x007FFFFF /* Actual len */
56
57 /* DMA macros */
58 /* Bitmasks of XAXIDMA_CR_OFFSET register */
59 #define XAXIDMA_CR_RUNSTOP_MASK 0x00000001 /* Start/stop DMA channel */
60 #define XAXIDMA_CR_RESET_MASK   0x00000004 /* Reset DMA engine */
61
62 /* Bitmasks of XAXIDMA_SR_OFFSET register */
63 #define XAXIDMA_HALTED_MASK     0x00000001  /* DMA channel halted */
64
65 /* Bitmask for interrupts */
66 #define XAXIDMA_IRQ_IOC_MASK    0x00001000 /* Completion intr */
67 #define XAXIDMA_IRQ_DELAY_MASK  0x00002000 /* Delay interrupt */
68 #define XAXIDMA_IRQ_ALL_MASK    0x00007000 /* All interrupts */
69
70 /* Bitmasks of XAXIDMA_BD_CTRL_OFFSET register */
71 #define XAXIDMA_BD_CTRL_TXSOF_MASK      0x08000000 /* First tx packet */
72 #define XAXIDMA_BD_CTRL_TXEOF_MASK      0x04000000 /* Last tx packet */
73
74 #define DMAALIGN        128
75
76 static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN)));
77
78 /* Reflect dma offsets */
79 struct axidma_reg {
80         u32 control; /* DMACR */
81         u32 status; /* DMASR */
82         u32 current; /* CURDESC low 32 bit */
83         u32 current_hi; /* CURDESC high 32 bit */
84         u32 tail; /* TAILDESC low 32 bit */
85         u32 tail_hi; /* TAILDESC high 32 bit */
86 };
87
88 /* Private driver structures */
89 struct axidma_priv {
90         struct axidma_reg *dmatx;
91         struct axidma_reg *dmarx;
92         int phyaddr;
93         struct axi_regs *iobase;
94         phy_interface_t interface;
95         struct phy_device *phydev;
96         struct mii_dev *bus;
97         u8 eth_hasnobuf;
98         int phy_of_handle;
99 };
100
101 /* BD descriptors */
102 struct axidma_bd {
103         u32 next;       /* Next descriptor pointer */
104         u32 reserved1;
105         u32 phys;       /* Buffer address */
106         u32 reserved2;
107         u32 reserved3;
108         u32 reserved4;
109         u32 cntrl;      /* Control */
110         u32 status;     /* Status */
111         u32 app0;
112         u32 app1;       /* TX start << 16 | insert */
113         u32 app2;       /* TX csum seed */
114         u32 app3;
115         u32 app4;
116         u32 sw_id_offset;
117         u32 reserved5;
118         u32 reserved6;
119 };
120
121 /* Static BDs - driver uses only one BD */
122 static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN)));
123 static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN)));
124
125 struct axi_regs {
126         u32 reserved[3];
127         u32 is; /* 0xC: Interrupt status */
128         u32 reserved2;
129         u32 ie; /* 0x14: Interrupt enable */
130         u32 reserved3[251];
131         u32 rcw1; /* 0x404: Rx Configuration Word 1 */
132         u32 tc; /* 0x408: Tx Configuration */
133         u32 reserved4;
134         u32 emmc; /* 0x410: EMAC mode configuration */
135         u32 reserved5[59];
136         u32 mdio_mc; /* 0x500: MII Management Config */
137         u32 mdio_mcr; /* 0x504: MII Management Control */
138         u32 mdio_mwd; /* 0x508: MII Management Write Data */
139         u32 mdio_mrd; /* 0x50C: MII Management Read Data */
140         u32 reserved6[124];
141         u32 uaw0; /* 0x700: Unicast address word 0 */
142         u32 uaw1; /* 0x704: Unicast address word 1 */
143 };
144
145 /* Use MII register 1 (MII status register) to detect PHY */
146 #define PHY_DETECT_REG  1
147
148 /*
149  * Mask used to verify certain PHY features (or register contents)
150  * in the register above:
151  *  0x1000: 10Mbps full duplex support
152  *  0x0800: 10Mbps half duplex support
153  *  0x0008: Auto-negotiation support
154  */
155 #define PHY_DETECT_MASK 0x1808
156
157 static inline int mdio_wait(struct axi_regs *regs)
158 {
159         u32 timeout = 200;
160
161         /* Wait till MDIO interface is ready to accept a new transaction. */
162         while (timeout && (!(readl(&regs->mdio_mcr)
163                                                 & XAE_MDIO_MCR_READY_MASK))) {
164                 timeout--;
165                 udelay(1);
166         }
167         if (!timeout) {
168                 printf("%s: Timeout\n", __func__);
169                 return 1;
170         }
171         return 0;
172 }
173
174 /**
175  * axienet_dma_write -  Memory mapped Axi DMA register Buffer Descriptor write.
176  * @bd:         pointer to BD descriptor structure
177  * @desc:       Address offset of DMA descriptors
178  *
179  * This function writes the value into the corresponding Axi DMA register.
180  */
181 static inline void axienet_dma_write(struct axidma_bd *bd, u32 *desc)
182 {
183 #if defined(CONFIG_PHYS_64BIT)
184         writeq(bd, desc);
185 #else
186         writel((u32)bd, desc);
187 #endif
188 }
189
190 static u32 phyread(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
191                    u16 *val)
192 {
193         struct axi_regs *regs = priv->iobase;
194         u32 mdioctrlreg = 0;
195
196         if (mdio_wait(regs))
197                 return 1;
198
199         mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
200                         XAE_MDIO_MCR_PHYAD_MASK) |
201                         ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
202                         & XAE_MDIO_MCR_REGAD_MASK) |
203                         XAE_MDIO_MCR_INITIATE_MASK |
204                         XAE_MDIO_MCR_OP_READ_MASK;
205
206         writel(mdioctrlreg, &regs->mdio_mcr);
207
208         if (mdio_wait(regs))
209                 return 1;
210
211         /* Read data */
212         *val = readl(&regs->mdio_mrd);
213         return 0;
214 }
215
216 static u32 phywrite(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
217                     u32 data)
218 {
219         struct axi_regs *regs = priv->iobase;
220         u32 mdioctrlreg = 0;
221
222         if (mdio_wait(regs))
223                 return 1;
224
225         mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
226                         XAE_MDIO_MCR_PHYAD_MASK) |
227                         ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
228                         & XAE_MDIO_MCR_REGAD_MASK) |
229                         XAE_MDIO_MCR_INITIATE_MASK |
230                         XAE_MDIO_MCR_OP_WRITE_MASK;
231
232         /* Write data */
233         writel(data, &regs->mdio_mwd);
234
235         writel(mdioctrlreg, &regs->mdio_mcr);
236
237         if (mdio_wait(regs))
238                 return 1;
239
240         return 0;
241 }
242
243 static int axiemac_phy_init(struct udevice *dev)
244 {
245         u16 phyreg;
246         u32 i, ret;
247         struct axidma_priv *priv = dev_get_priv(dev);
248         struct axi_regs *regs = priv->iobase;
249         struct phy_device *phydev;
250
251         u32 supported = SUPPORTED_10baseT_Half |
252                         SUPPORTED_10baseT_Full |
253                         SUPPORTED_100baseT_Half |
254                         SUPPORTED_100baseT_Full |
255                         SUPPORTED_1000baseT_Half |
256                         SUPPORTED_1000baseT_Full;
257
258         /* Set default MDIO divisor */
259         writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, &regs->mdio_mc);
260
261         if (priv->phyaddr == -1) {
262                 /* Detect the PHY address */
263                 for (i = 31; i >= 0; i--) {
264                         ret = phyread(priv, i, PHY_DETECT_REG, &phyreg);
265                         if (!ret && (phyreg != 0xFFFF) &&
266                         ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
267                                 /* Found a valid PHY address */
268                                 priv->phyaddr = i;
269                                 debug("axiemac: Found valid phy address, %x\n",
270                                       i);
271                                 break;
272                         }
273                 }
274         }
275
276         /* Interface - look at tsec */
277         phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
278
279         phydev->supported &= supported;
280         phydev->advertising = phydev->supported;
281         priv->phydev = phydev;
282         if (priv->phy_of_handle)
283                 priv->phydev->node = offset_to_ofnode(priv->phy_of_handle);
284         phy_config(phydev);
285
286         return 0;
287 }
288
289 /* Setting axi emac and phy to proper setting */
290 static int setup_phy(struct udevice *dev)
291 {
292         u16 temp;
293         u32 speed, emmc_reg, ret;
294         struct axidma_priv *priv = dev_get_priv(dev);
295         struct axi_regs *regs = priv->iobase;
296         struct phy_device *phydev = priv->phydev;
297
298         if (priv->interface == PHY_INTERFACE_MODE_SGMII) {
299                 /*
300                  * In SGMII cases the isolate bit might set
301                  * after DMA and ethernet resets and hence
302                  * check and clear if set.
303                  */
304                 ret = phyread(priv, priv->phyaddr, MII_BMCR, &temp);
305                 if (ret)
306                         return 0;
307                 if (temp & BMCR_ISOLATE) {
308                         temp &= ~BMCR_ISOLATE;
309                         ret = phywrite(priv, priv->phyaddr, MII_BMCR, temp);
310                         if (ret)
311                                 return 0;
312                 }
313         }
314
315         if (phy_startup(phydev)) {
316                 printf("axiemac: could not initialize PHY %s\n",
317                        phydev->dev->name);
318                 return 0;
319         }
320         if (!phydev->link) {
321                 printf("%s: No link.\n", phydev->dev->name);
322                 return 0;
323         }
324
325         switch (phydev->speed) {
326         case 1000:
327                 speed = XAE_EMMC_LINKSPD_1000;
328                 break;
329         case 100:
330                 speed = XAE_EMMC_LINKSPD_100;
331                 break;
332         case 10:
333                 speed = XAE_EMMC_LINKSPD_10;
334                 break;
335         default:
336                 return 0;
337         }
338
339         /* Setup the emac for the phy speed */
340         emmc_reg = readl(&regs->emmc);
341         emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
342         emmc_reg |= speed;
343
344         /* Write new speed setting out to Axi Ethernet */
345         writel(emmc_reg, &regs->emmc);
346
347         /*
348         * Setting the operating speed of the MAC needs a delay. There
349         * doesn't seem to be register to poll, so please consider this
350         * during your application design.
351         */
352         udelay(1);
353
354         return 1;
355 }
356
357 /* STOP DMA transfers */
358 static void axiemac_stop(struct udevice *dev)
359 {
360         struct axidma_priv *priv = dev_get_priv(dev);
361         u32 temp;
362
363         /* Stop the hardware */
364         temp = readl(&priv->dmatx->control);
365         temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
366         writel(temp, &priv->dmatx->control);
367
368         temp = readl(&priv->dmarx->control);
369         temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
370         writel(temp, &priv->dmarx->control);
371
372         debug("axiemac: Halted\n");
373 }
374
375 static int axi_ethernet_init(struct axidma_priv *priv)
376 {
377         struct axi_regs *regs = priv->iobase;
378         int err;
379
380         /*
381          * Check the status of the MgtRdy bit in the interrupt status
382          * registers. This must be done to allow the MGT clock to become stable
383          * for the Sgmii and 1000BaseX PHY interfaces. No other register reads
384          * will be valid until this bit is valid.
385          * The bit is always a 1 for all other PHY interfaces.
386          * Interrupt status and enable registers are not available in non
387          * processor mode and hence bypass in this mode
388          */
389         if (!priv->eth_hasnobuf) {
390                 err = wait_for_bit_le32(&regs->is, XAE_INT_MGTRDY_MASK,
391                                         true, 200, false);
392                 if (err) {
393                         printf("%s: Timeout\n", __func__);
394                         return 1;
395                 }
396
397                 /*
398                  * Stop the device and reset HW
399                  * Disable interrupts
400                  */
401                 writel(0, &regs->ie);
402         }
403
404         /* Disable the receiver */
405         writel(readl(&regs->rcw1) & ~XAE_RCW1_RX_MASK, &regs->rcw1);
406
407         /*
408          * Stopping the receiver in mid-packet causes a dropped packet
409          * indication from HW. Clear it.
410          */
411         if (!priv->eth_hasnobuf) {
412                 /* Set the interrupt status register to clear the interrupt */
413                 writel(XAE_INT_RXRJECT_MASK, &regs->is);
414         }
415
416         /* Setup HW */
417         /* Set default MDIO divisor */
418         writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, &regs->mdio_mc);
419
420         debug("axiemac: InitHw done\n");
421         return 0;
422 }
423
424 static int axiemac_write_hwaddr(struct udevice *dev)
425 {
426         struct eth_pdata *pdata = dev_get_platdata(dev);
427         struct axidma_priv *priv = dev_get_priv(dev);
428         struct axi_regs *regs = priv->iobase;
429
430         /* Set the MAC address */
431         int val = ((pdata->enetaddr[3] << 24) | (pdata->enetaddr[2] << 16) |
432                 (pdata->enetaddr[1] << 8) | (pdata->enetaddr[0]));
433         writel(val, &regs->uaw0);
434
435         val = (pdata->enetaddr[5] << 8) | pdata->enetaddr[4];
436         val |= readl(&regs->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK;
437         writel(val, &regs->uaw1);
438         return 0;
439 }
440
441 /* Reset DMA engine */
442 static void axi_dma_init(struct axidma_priv *priv)
443 {
444         u32 timeout = 500;
445
446         /* Reset the engine so the hardware starts from a known state */
447         writel(XAXIDMA_CR_RESET_MASK, &priv->dmatx->control);
448         writel(XAXIDMA_CR_RESET_MASK, &priv->dmarx->control);
449
450         /* At the initialization time, hardware should finish reset quickly */
451         while (timeout--) {
452                 /* Check transmit/receive channel */
453                 /* Reset is done when the reset bit is low */
454                 if (!((readl(&priv->dmatx->control) |
455                                 readl(&priv->dmarx->control))
456                                                 & XAXIDMA_CR_RESET_MASK)) {
457                         break;
458                 }
459         }
460         if (!timeout)
461                 printf("%s: Timeout\n", __func__);
462 }
463
464 static int axiemac_start(struct udevice *dev)
465 {
466         struct axidma_priv *priv = dev_get_priv(dev);
467         struct axi_regs *regs = priv->iobase;
468         u32 temp;
469
470         debug("axiemac: Init started\n");
471         /*
472          * Initialize AXIDMA engine. AXIDMA engine must be initialized before
473          * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is
474          * reset, and since AXIDMA reset line is connected to AxiEthernet, this
475          * would ensure a reset of AxiEthernet.
476          */
477         axi_dma_init(priv);
478
479         /* Initialize AxiEthernet hardware. */
480         if (axi_ethernet_init(priv))
481                 return -1;
482
483         /* Disable all RX interrupts before RxBD space setup */
484         temp = readl(&priv->dmarx->control);
485         temp &= ~XAXIDMA_IRQ_ALL_MASK;
486         writel(temp, &priv->dmarx->control);
487
488         /* Start DMA RX channel. Now it's ready to receive data.*/
489         axienet_dma_write(&rx_bd, &priv->dmarx->current);
490
491         /* Setup the BD. */
492         memset(&rx_bd, 0, sizeof(rx_bd));
493         rx_bd.next = (u32)&rx_bd;
494         rx_bd.phys = (u32)&rxframe;
495         rx_bd.cntrl = sizeof(rxframe);
496         /* Flush the last BD so DMA core could see the updates */
497         flush_cache((u32)&rx_bd, sizeof(rx_bd));
498
499         /* It is necessary to flush rxframe because if you don't do it
500          * then cache can contain uninitialized data */
501         flush_cache((u32)&rxframe, sizeof(rxframe));
502
503         /* Start the hardware */
504         temp = readl(&priv->dmarx->control);
505         temp |= XAXIDMA_CR_RUNSTOP_MASK;
506         writel(temp, &priv->dmarx->control);
507
508         /* Rx BD is ready - start */
509         axienet_dma_write(&rx_bd, &priv->dmarx->tail);
510
511         /* Enable TX */
512         writel(XAE_TC_TX_MASK, &regs->tc);
513         /* Enable RX */
514         writel(XAE_RCW1_RX_MASK, &regs->rcw1);
515
516         /* PHY setup */
517         if (!setup_phy(dev)) {
518                 axiemac_stop(dev);
519                 return -1;
520         }
521
522         debug("axiemac: Init complete\n");
523         return 0;
524 }
525
526 static int axiemac_send(struct udevice *dev, void *ptr, int len)
527 {
528         struct axidma_priv *priv = dev_get_priv(dev);
529         u32 timeout;
530
531         if (len > PKTSIZE_ALIGN)
532                 len = PKTSIZE_ALIGN;
533
534         /* Flush packet to main memory to be trasfered by DMA */
535         flush_cache((u32)ptr, len);
536
537         /* Setup Tx BD */
538         memset(&tx_bd, 0, sizeof(tx_bd));
539         /* At the end of the ring, link the last BD back to the top */
540         tx_bd.next = (u32)&tx_bd;
541         tx_bd.phys = (u32)ptr;
542         /* Save len */
543         tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK |
544                                                 XAXIDMA_BD_CTRL_TXEOF_MASK;
545
546         /* Flush the last BD so DMA core could see the updates */
547         flush_cache((u32)&tx_bd, sizeof(tx_bd));
548
549         if (readl(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) {
550                 u32 temp;
551                 axienet_dma_write(&tx_bd, &priv->dmatx->current);
552                 /* Start the hardware */
553                 temp = readl(&priv->dmatx->control);
554                 temp |= XAXIDMA_CR_RUNSTOP_MASK;
555                 writel(temp, &priv->dmatx->control);
556         }
557
558         /* Start transfer */
559         axienet_dma_write(&tx_bd, &priv->dmatx->tail);
560
561         /* Wait for transmission to complete */
562         debug("axiemac: Waiting for tx to be done\n");
563         timeout = 200;
564         while (timeout && (!(readl(&priv->dmatx->status) &
565                         (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))) {
566                 timeout--;
567                 udelay(1);
568         }
569         if (!timeout) {
570                 printf("%s: Timeout\n", __func__);
571                 return 1;
572         }
573
574         debug("axiemac: Sending complete\n");
575         return 0;
576 }
577
578 static int isrxready(struct axidma_priv *priv)
579 {
580         u32 status;
581
582         /* Read pending interrupts */
583         status = readl(&priv->dmarx->status);
584
585         /* Acknowledge pending interrupts */
586         writel(status & XAXIDMA_IRQ_ALL_MASK, &priv->dmarx->status);
587
588         /*
589          * If Reception done interrupt is asserted, call RX call back function
590          * to handle the processed BDs and then raise the according flag.
591          */
592         if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))
593                 return 1;
594
595         return 0;
596 }
597
598 static int axiemac_recv(struct udevice *dev, int flags, uchar **packetp)
599 {
600         u32 length;
601         struct axidma_priv *priv = dev_get_priv(dev);
602         u32 temp;
603
604         /* Wait for an incoming packet */
605         if (!isrxready(priv))
606                 return -1;
607
608         debug("axiemac: RX data ready\n");
609
610         /* Disable IRQ for a moment till packet is handled */
611         temp = readl(&priv->dmarx->control);
612         temp &= ~XAXIDMA_IRQ_ALL_MASK;
613         writel(temp, &priv->dmarx->control);
614         if (!priv->eth_hasnobuf)
615                 length = rx_bd.app4 & 0xFFFF; /* max length mask */
616         else
617                 length = rx_bd.status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
618
619 #ifdef DEBUG
620         print_buffer(&rxframe, &rxframe[0], 1, length, 16);
621 #endif
622
623         *packetp = rxframe;
624         return length;
625 }
626
627 static int axiemac_free_pkt(struct udevice *dev, uchar *packet, int length)
628 {
629         struct axidma_priv *priv = dev_get_priv(dev);
630
631 #ifdef DEBUG
632         /* It is useful to clear buffer to be sure that it is consistent */
633         memset(rxframe, 0, sizeof(rxframe));
634 #endif
635         /* Setup RxBD */
636         /* Clear the whole buffer and setup it again - all flags are cleared */
637         memset(&rx_bd, 0, sizeof(rx_bd));
638         rx_bd.next = (u32)&rx_bd;
639         rx_bd.phys = (u32)&rxframe;
640         rx_bd.cntrl = sizeof(rxframe);
641
642         /* Write bd to HW */
643         flush_cache((u32)&rx_bd, sizeof(rx_bd));
644
645         /* It is necessary to flush rxframe because if you don't do it
646          * then cache will contain previous packet */
647         flush_cache((u32)&rxframe, sizeof(rxframe));
648
649         /* Rx BD is ready - start again */
650         axienet_dma_write(&rx_bd, &priv->dmarx->tail);
651
652         debug("axiemac: RX completed, framelength = %d\n", length);
653
654         return 0;
655 }
656
657 static int axiemac_miiphy_read(struct mii_dev *bus, int addr,
658                                int devad, int reg)
659 {
660         int ret;
661         u16 value;
662
663         ret = phyread(bus->priv, addr, reg, &value);
664         debug("axiemac: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg,
665               value, ret);
666         return value;
667 }
668
669 static int axiemac_miiphy_write(struct mii_dev *bus, int addr, int devad,
670                                 int reg, u16 value)
671 {
672         debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value);
673         return phywrite(bus->priv, addr, reg, value);
674 }
675
676 static int axi_emac_probe(struct udevice *dev)
677 {
678         struct axidma_priv *priv = dev_get_priv(dev);
679         int ret;
680
681         priv->bus = mdio_alloc();
682         priv->bus->read = axiemac_miiphy_read;
683         priv->bus->write = axiemac_miiphy_write;
684         priv->bus->priv = priv;
685
686         ret = mdio_register_seq(priv->bus, dev->seq);
687         if (ret)
688                 return ret;
689
690         axiemac_phy_init(dev);
691
692         return 0;
693 }
694
695 static int axi_emac_remove(struct udevice *dev)
696 {
697         struct axidma_priv *priv = dev_get_priv(dev);
698
699         free(priv->phydev);
700         mdio_unregister(priv->bus);
701         mdio_free(priv->bus);
702
703         return 0;
704 }
705
706 static const struct eth_ops axi_emac_ops = {
707         .start                  = axiemac_start,
708         .send                   = axiemac_send,
709         .recv                   = axiemac_recv,
710         .free_pkt               = axiemac_free_pkt,
711         .stop                   = axiemac_stop,
712         .write_hwaddr           = axiemac_write_hwaddr,
713 };
714
715 static int axi_emac_ofdata_to_platdata(struct udevice *dev)
716 {
717         struct eth_pdata *pdata = dev_get_platdata(dev);
718         struct axidma_priv *priv = dev_get_priv(dev);
719         int node = dev_of_offset(dev);
720         int offset = 0;
721         const char *phy_mode;
722
723         pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
724         priv->iobase = (struct axi_regs *)pdata->iobase;
725
726         offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
727                                        "axistream-connected");
728         if (offset <= 0) {
729                 printf("%s: axistream is not found\n", __func__);
730                 return -EINVAL;
731         }
732         priv->dmatx = (struct axidma_reg *)fdtdec_get_addr(gd->fdt_blob,
733                                                           offset, "reg");
734         if (!priv->dmatx) {
735                 printf("%s: axi_dma register space not found\n", __func__);
736                 return -EINVAL;
737         }
738         /* RX channel offset is 0x30 */
739         priv->dmarx = (struct axidma_reg *)((u32)priv->dmatx + 0x30);
740
741         priv->phyaddr = -1;
742
743         offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
744         if (offset > 0) {
745                 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
746                 priv->phy_of_handle = offset;
747         }
748
749         phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
750         if (phy_mode)
751                 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
752         if (pdata->phy_interface == -1) {
753                 printf("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
754                 return -EINVAL;
755         }
756         priv->interface = pdata->phy_interface;
757
758         priv->eth_hasnobuf = fdtdec_get_bool(gd->fdt_blob, node,
759                                              "xlnx,eth-hasnobuf");
760
761         printf("AXI EMAC: %lx, phyaddr %d, interface %s\n", (ulong)priv->iobase,
762                priv->phyaddr, phy_string_for_interface(priv->interface));
763
764         return 0;
765 }
766
767 static const struct udevice_id axi_emac_ids[] = {
768         { .compatible = "xlnx,axi-ethernet-1.00.a" },
769         { }
770 };
771
772 U_BOOT_DRIVER(axi_emac) = {
773         .name   = "axi_emac",
774         .id     = UCLASS_ETH,
775         .of_match = axi_emac_ids,
776         .ofdata_to_platdata = axi_emac_ofdata_to_platdata,
777         .probe  = axi_emac_probe,
778         .remove = axi_emac_remove,
779         .ops    = &axi_emac_ops,
780         .priv_auto_alloc_size = sizeof(struct axidma_priv),
781         .platdata_auto_alloc_size = sizeof(struct eth_pdata),
782 };