1
2
3
4
5
6
7
8#include <config.h>
9#include <common.h>
10#include <dm.h>
11#include <net.h>
12#include <malloc.h>
13#include <asm/io.h>
14#include <phy.h>
15#include <miiphy.h>
16#include <wait_bit.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20
21#define XAE_EMMC_LINKSPEED_MASK 0xC0000000
22#define XAE_EMMC_LINKSPD_10 0x00000000
23#define XAE_EMMC_LINKSPD_100 0x40000000
24#define XAE_EMMC_LINKSPD_1000 0x80000000
25
26
27#define XAE_INT_RXRJECT_MASK 0x00000008
28#define XAE_INT_MGTRDY_MASK 0x00000080
29
30
31#define XAE_RCW1_RX_MASK 0x10000000
32
33
34#define XAE_TC_TX_MASK 0x10000000
35
36#define XAE_UAW1_UNICASTADDR_MASK 0x0000FFFF
37
38
39#define XAE_MDIO_MC_MDIOEN_MASK 0x00000040
40
41
42#define XAE_MDIO_MCR_PHYAD_MASK 0x1F000000
43#define XAE_MDIO_MCR_PHYAD_SHIFT 24
44#define XAE_MDIO_MCR_REGAD_MASK 0x001F0000
45#define XAE_MDIO_MCR_REGAD_SHIFT 16
46#define XAE_MDIO_MCR_OP_READ_MASK 0x00008000
47#define XAE_MDIO_MCR_OP_WRITE_MASK 0x00004000
48#define XAE_MDIO_MCR_INITIATE_MASK 0x00000800
49#define XAE_MDIO_MCR_READY_MASK 0x00000080
50
51#define XAE_MDIO_DIV_DFT 29
52
53#define XAXIDMA_BD_STS_ACTUAL_LEN_MASK 0x007FFFFF
54
55
56
57#define XAXIDMA_CR_RUNSTOP_MASK 0x00000001
58#define XAXIDMA_CR_RESET_MASK 0x00000004
59
60
61#define XAXIDMA_HALTED_MASK 0x00000001
62
63
64#define XAXIDMA_IRQ_IOC_MASK 0x00001000
65#define XAXIDMA_IRQ_DELAY_MASK 0x00002000
66#define XAXIDMA_IRQ_ALL_MASK 0x00007000
67
68
69#define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000
70#define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000
71
72#define DMAALIGN 128
73
74static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN)));
75
76
77struct axidma_reg {
78 u32 control;
79 u32 status;
80 u32 current;
81 u32 current_hi;
82 u32 tail;
83 u32 tail_hi;
84};
85
86
87struct axidma_priv {
88 struct axidma_reg *dmatx;
89 struct axidma_reg *dmarx;
90 int phyaddr;
91 struct axi_regs *iobase;
92 phy_interface_t interface;
93 struct phy_device *phydev;
94 struct mii_dev *bus;
95 u8 eth_hasnobuf;
96};
97
98
99struct axidma_bd {
100 u32 next;
101 u32 reserved1;
102 u32 phys;
103 u32 reserved2;
104 u32 reserved3;
105 u32 reserved4;
106 u32 cntrl;
107 u32 status;
108 u32 app0;
109 u32 app1;
110 u32 app2;
111 u32 app3;
112 u32 app4;
113 u32 sw_id_offset;
114 u32 reserved5;
115 u32 reserved6;
116};
117
118
119static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN)));
120static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN)));
121
122struct axi_regs {
123 u32 reserved[3];
124 u32 is;
125 u32 reserved2;
126 u32 ie;
127 u32 reserved3[251];
128 u32 rcw1;
129 u32 tc;
130 u32 reserved4;
131 u32 emmc;
132 u32 reserved5[59];
133 u32 mdio_mc;
134 u32 mdio_mcr;
135 u32 mdio_mwd;
136 u32 mdio_mrd;
137 u32 reserved6[124];
138 u32 uaw0;
139 u32 uaw1;
140};
141
142
143#define PHY_DETECT_REG 1
144
145
146
147
148
149
150
151
152#define PHY_DETECT_MASK 0x1808
153
154static inline int mdio_wait(struct axi_regs *regs)
155{
156 u32 timeout = 200;
157
158
159 while (timeout && (!(readl(®s->mdio_mcr)
160 & XAE_MDIO_MCR_READY_MASK))) {
161 timeout--;
162 udelay(1);
163 }
164 if (!timeout) {
165 printf("%s: Timeout\n", __func__);
166 return 1;
167 }
168 return 0;
169}
170
171
172
173
174
175
176
177
178static inline void axienet_dma_write(struct axidma_bd *bd, u32 *desc)
179{
180#if defined(CONFIG_PHYS_64BIT)
181 writeq(bd, desc);
182#else
183 writel((u32)bd, desc);
184#endif
185}
186
187static u32 phyread(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
188 u16 *val)
189{
190 struct axi_regs *regs = priv->iobase;
191 u32 mdioctrlreg = 0;
192
193 if (mdio_wait(regs))
194 return 1;
195
196 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
197 XAE_MDIO_MCR_PHYAD_MASK) |
198 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
199 & XAE_MDIO_MCR_REGAD_MASK) |
200 XAE_MDIO_MCR_INITIATE_MASK |
201 XAE_MDIO_MCR_OP_READ_MASK;
202
203 writel(mdioctrlreg, ®s->mdio_mcr);
204
205 if (mdio_wait(regs))
206 return 1;
207
208
209 *val = readl(®s->mdio_mrd);
210 return 0;
211}
212
213static u32 phywrite(struct axidma_priv *priv, u32 phyaddress, u32 registernum,
214 u32 data)
215{
216 struct axi_regs *regs = priv->iobase;
217 u32 mdioctrlreg = 0;
218
219 if (mdio_wait(regs))
220 return 1;
221
222 mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) &
223 XAE_MDIO_MCR_PHYAD_MASK) |
224 ((registernum << XAE_MDIO_MCR_REGAD_SHIFT)
225 & XAE_MDIO_MCR_REGAD_MASK) |
226 XAE_MDIO_MCR_INITIATE_MASK |
227 XAE_MDIO_MCR_OP_WRITE_MASK;
228
229
230 writel(data, ®s->mdio_mwd);
231
232 writel(mdioctrlreg, ®s->mdio_mcr);
233
234 if (mdio_wait(regs))
235 return 1;
236
237 return 0;
238}
239
240static int axiemac_phy_init(struct udevice *dev)
241{
242 u16 phyreg;
243 u32 i, ret;
244 struct axidma_priv *priv = dev_get_priv(dev);
245 struct axi_regs *regs = priv->iobase;
246 struct phy_device *phydev;
247
248 u32 supported = SUPPORTED_10baseT_Half |
249 SUPPORTED_10baseT_Full |
250 SUPPORTED_100baseT_Half |
251 SUPPORTED_100baseT_Full |
252 SUPPORTED_1000baseT_Half |
253 SUPPORTED_1000baseT_Full;
254
255
256 writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, ®s->mdio_mc);
257
258 if (priv->phyaddr == -1) {
259
260 for (i = 31; i >= 0; i--) {
261 ret = phyread(priv, i, PHY_DETECT_REG, &phyreg);
262 if (!ret && (phyreg != 0xFFFF) &&
263 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
264
265 priv->phyaddr = i;
266 debug("axiemac: Found valid phy address, %x\n",
267 i);
268 break;
269 }
270 }
271 }
272
273
274 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
275
276 phydev->supported &= supported;
277 phydev->advertising = phydev->supported;
278 priv->phydev = phydev;
279 phy_config(phydev);
280
281 return 0;
282}
283
284
285static int setup_phy(struct udevice *dev)
286{
287 u16 temp;
288 u32 speed, emmc_reg, ret;
289 struct axidma_priv *priv = dev_get_priv(dev);
290 struct axi_regs *regs = priv->iobase;
291 struct phy_device *phydev = priv->phydev;
292
293 if (priv->interface == PHY_INTERFACE_MODE_SGMII) {
294
295
296
297
298
299 ret = phyread(priv, priv->phyaddr, MII_BMCR, &temp);
300 if (ret)
301 return 0;
302 if (temp & BMCR_ISOLATE) {
303 temp &= ~BMCR_ISOLATE;
304 ret = phywrite(priv, priv->phyaddr, MII_BMCR, temp);
305 if (ret)
306 return 0;
307 }
308 }
309
310 if (phy_startup(phydev)) {
311 printf("axiemac: could not initialize PHY %s\n",
312 phydev->dev->name);
313 return 0;
314 }
315 if (!phydev->link) {
316 printf("%s: No link.\n", phydev->dev->name);
317 return 0;
318 }
319
320 switch (phydev->speed) {
321 case 1000:
322 speed = XAE_EMMC_LINKSPD_1000;
323 break;
324 case 100:
325 speed = XAE_EMMC_LINKSPD_100;
326 break;
327 case 10:
328 speed = XAE_EMMC_LINKSPD_10;
329 break;
330 default:
331 return 0;
332 }
333
334
335 emmc_reg = readl(®s->emmc);
336 emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK;
337 emmc_reg |= speed;
338
339
340 writel(emmc_reg, ®s->emmc);
341
342
343
344
345
346
347 udelay(1);
348
349 return 1;
350}
351
352
353static void axiemac_stop(struct udevice *dev)
354{
355 struct axidma_priv *priv = dev_get_priv(dev);
356 u32 temp;
357
358
359 temp = readl(&priv->dmatx->control);
360 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
361 writel(temp, &priv->dmatx->control);
362
363 temp = readl(&priv->dmarx->control);
364 temp &= ~XAXIDMA_CR_RUNSTOP_MASK;
365 writel(temp, &priv->dmarx->control);
366
367 debug("axiemac: Halted\n");
368}
369
370static int axi_ethernet_init(struct axidma_priv *priv)
371{
372 struct axi_regs *regs = priv->iobase;
373 int err;
374
375
376
377
378
379
380
381
382
383
384 if (!priv->eth_hasnobuf) {
385 err = wait_for_bit_le32(®s->is, XAE_INT_MGTRDY_MASK,
386 true, 200, false);
387 if (err) {
388 printf("%s: Timeout\n", __func__);
389 return 1;
390 }
391
392
393
394
395
396 writel(0, ®s->ie);
397 }
398
399
400 writel(readl(®s->rcw1) & ~XAE_RCW1_RX_MASK, ®s->rcw1);
401
402
403
404
405
406 if (!priv->eth_hasnobuf) {
407
408 writel(XAE_INT_RXRJECT_MASK, ®s->is);
409 }
410
411
412
413 writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, ®s->mdio_mc);
414
415 debug("axiemac: InitHw done\n");
416 return 0;
417}
418
419static int axiemac_write_hwaddr(struct udevice *dev)
420{
421 struct eth_pdata *pdata = dev_get_platdata(dev);
422 struct axidma_priv *priv = dev_get_priv(dev);
423 struct axi_regs *regs = priv->iobase;
424
425
426 int val = ((pdata->enetaddr[3] << 24) | (pdata->enetaddr[2] << 16) |
427 (pdata->enetaddr[1] << 8) | (pdata->enetaddr[0]));
428 writel(val, ®s->uaw0);
429
430 val = (pdata->enetaddr[5] << 8) | pdata->enetaddr[4];
431 val |= readl(®s->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK;
432 writel(val, ®s->uaw1);
433 return 0;
434}
435
436
437static void axi_dma_init(struct axidma_priv *priv)
438{
439 u32 timeout = 500;
440
441
442 writel(XAXIDMA_CR_RESET_MASK, &priv->dmatx->control);
443 writel(XAXIDMA_CR_RESET_MASK, &priv->dmarx->control);
444
445
446 while (timeout--) {
447
448
449 if (!((readl(&priv->dmatx->control) |
450 readl(&priv->dmarx->control))
451 & XAXIDMA_CR_RESET_MASK)) {
452 break;
453 }
454 }
455 if (!timeout)
456 printf("%s: Timeout\n", __func__);
457}
458
459static int axiemac_start(struct udevice *dev)
460{
461 struct axidma_priv *priv = dev_get_priv(dev);
462 struct axi_regs *regs = priv->iobase;
463 u32 temp;
464
465 debug("axiemac: Init started\n");
466
467
468
469
470
471
472 axi_dma_init(priv);
473
474
475 if (axi_ethernet_init(priv))
476 return -1;
477
478
479 temp = readl(&priv->dmarx->control);
480 temp &= ~XAXIDMA_IRQ_ALL_MASK;
481 writel(temp, &priv->dmarx->control);
482
483
484 axienet_dma_write(&rx_bd, &priv->dmarx->current);
485
486
487 memset(&rx_bd, 0, sizeof(rx_bd));
488 rx_bd.next = (u32)&rx_bd;
489 rx_bd.phys = (u32)&rxframe;
490 rx_bd.cntrl = sizeof(rxframe);
491
492 flush_cache((u32)&rx_bd, sizeof(rx_bd));
493
494
495
496 flush_cache((u32)&rxframe, sizeof(rxframe));
497
498
499 temp = readl(&priv->dmarx->control);
500 temp |= XAXIDMA_CR_RUNSTOP_MASK;
501 writel(temp, &priv->dmarx->control);
502
503
504 axienet_dma_write(&rx_bd, &priv->dmarx->tail);
505
506
507 writel(XAE_TC_TX_MASK, ®s->tc);
508
509 writel(XAE_RCW1_RX_MASK, ®s->rcw1);
510
511
512 if (!setup_phy(dev)) {
513 axiemac_stop(dev);
514 return -1;
515 }
516
517 debug("axiemac: Init complete\n");
518 return 0;
519}
520
521static int axiemac_send(struct udevice *dev, void *ptr, int len)
522{
523 struct axidma_priv *priv = dev_get_priv(dev);
524 u32 timeout;
525
526 if (len > PKTSIZE_ALIGN)
527 len = PKTSIZE_ALIGN;
528
529
530 flush_cache((u32)ptr, len);
531
532
533 memset(&tx_bd, 0, sizeof(tx_bd));
534
535 tx_bd.next = (u32)&tx_bd;
536 tx_bd.phys = (u32)ptr;
537
538 tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK |
539 XAXIDMA_BD_CTRL_TXEOF_MASK;
540
541
542 flush_cache((u32)&tx_bd, sizeof(tx_bd));
543
544 if (readl(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) {
545 u32 temp;
546 axienet_dma_write(&tx_bd, &priv->dmatx->current);
547
548 temp = readl(&priv->dmatx->control);
549 temp |= XAXIDMA_CR_RUNSTOP_MASK;
550 writel(temp, &priv->dmatx->control);
551 }
552
553
554 axienet_dma_write(&tx_bd, &priv->dmatx->tail);
555
556
557 debug("axiemac: Waiting for tx to be done\n");
558 timeout = 200;
559 while (timeout && (!(readl(&priv->dmatx->status) &
560 (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))) {
561 timeout--;
562 udelay(1);
563 }
564 if (!timeout) {
565 printf("%s: Timeout\n", __func__);
566 return 1;
567 }
568
569 debug("axiemac: Sending complete\n");
570 return 0;
571}
572
573static int isrxready(struct axidma_priv *priv)
574{
575 u32 status;
576
577
578 status = readl(&priv->dmarx->status);
579
580
581 writel(status & XAXIDMA_IRQ_ALL_MASK, &priv->dmarx->status);
582
583
584
585
586
587 if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))
588 return 1;
589
590 return 0;
591}
592
593static int axiemac_recv(struct udevice *dev, int flags, uchar **packetp)
594{
595 u32 length;
596 struct axidma_priv *priv = dev_get_priv(dev);
597 u32 temp;
598
599
600 if (!isrxready(priv))
601 return -1;
602
603 debug("axiemac: RX data ready\n");
604
605
606 temp = readl(&priv->dmarx->control);
607 temp &= ~XAXIDMA_IRQ_ALL_MASK;
608 writel(temp, &priv->dmarx->control);
609 if (!priv->eth_hasnobuf)
610 length = rx_bd.app4 & 0xFFFF;
611 else
612 length = rx_bd.status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK;
613
614#ifdef DEBUG
615 print_buffer(&rxframe, &rxframe[0], 1, length, 16);
616#endif
617
618 *packetp = rxframe;
619 return length;
620}
621
622static int axiemac_free_pkt(struct udevice *dev, uchar *packet, int length)
623{
624 struct axidma_priv *priv = dev_get_priv(dev);
625
626#ifdef DEBUG
627
628 memset(rxframe, 0, sizeof(rxframe));
629#endif
630
631
632 memset(&rx_bd, 0, sizeof(rx_bd));
633 rx_bd.next = (u32)&rx_bd;
634 rx_bd.phys = (u32)&rxframe;
635 rx_bd.cntrl = sizeof(rxframe);
636
637
638 flush_cache((u32)&rx_bd, sizeof(rx_bd));
639
640
641
642 flush_cache((u32)&rxframe, sizeof(rxframe));
643
644
645 axienet_dma_write(&rx_bd, &priv->dmarx->tail);
646
647 debug("axiemac: RX completed, framelength = %d\n", length);
648
649 return 0;
650}
651
652static int axiemac_miiphy_read(struct mii_dev *bus, int addr,
653 int devad, int reg)
654{
655 int ret;
656 u16 value;
657
658 ret = phyread(bus->priv, addr, reg, &value);
659 debug("axiemac: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg,
660 value, ret);
661 return value;
662}
663
664static int axiemac_miiphy_write(struct mii_dev *bus, int addr, int devad,
665 int reg, u16 value)
666{
667 debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value);
668 return phywrite(bus->priv, addr, reg, value);
669}
670
671static int axi_emac_probe(struct udevice *dev)
672{
673 struct axidma_priv *priv = dev_get_priv(dev);
674 int ret;
675
676 priv->bus = mdio_alloc();
677 priv->bus->read = axiemac_miiphy_read;
678 priv->bus->write = axiemac_miiphy_write;
679 priv->bus->priv = priv;
680
681 ret = mdio_register_seq(priv->bus, dev->seq);
682 if (ret)
683 return ret;
684
685 axiemac_phy_init(dev);
686
687 return 0;
688}
689
690static int axi_emac_remove(struct udevice *dev)
691{
692 struct axidma_priv *priv = dev_get_priv(dev);
693
694 free(priv->phydev);
695 mdio_unregister(priv->bus);
696 mdio_free(priv->bus);
697
698 return 0;
699}
700
701static const struct eth_ops axi_emac_ops = {
702 .start = axiemac_start,
703 .send = axiemac_send,
704 .recv = axiemac_recv,
705 .free_pkt = axiemac_free_pkt,
706 .stop = axiemac_stop,
707 .write_hwaddr = axiemac_write_hwaddr,
708};
709
710static int axi_emac_ofdata_to_platdata(struct udevice *dev)
711{
712 struct eth_pdata *pdata = dev_get_platdata(dev);
713 struct axidma_priv *priv = dev_get_priv(dev);
714 int node = dev_of_offset(dev);
715 int offset = 0;
716 const char *phy_mode;
717
718 pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
719 priv->iobase = (struct axi_regs *)pdata->iobase;
720
721 offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
722 "axistream-connected");
723 if (offset <= 0) {
724 printf("%s: axistream is not found\n", __func__);
725 return -EINVAL;
726 }
727 priv->dmatx = (struct axidma_reg *)fdtdec_get_addr(gd->fdt_blob,
728 offset, "reg");
729 if (!priv->dmatx) {
730 printf("%s: axi_dma register space not found\n", __func__);
731 return -EINVAL;
732 }
733
734 priv->dmarx = (struct axidma_reg *)((u32)priv->dmatx + 0x30);
735
736 priv->phyaddr = -1;
737
738 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
739 if (offset > 0)
740 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
741
742 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
743 if (phy_mode)
744 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
745 if (pdata->phy_interface == -1) {
746 printf("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
747 return -EINVAL;
748 }
749 priv->interface = pdata->phy_interface;
750
751 priv->eth_hasnobuf = fdtdec_get_bool(gd->fdt_blob, node,
752 "xlnx,eth-hasnobuf");
753
754 printf("AXI EMAC: %lx, phyaddr %d, interface %s\n", (ulong)priv->iobase,
755 priv->phyaddr, phy_string_for_interface(priv->interface));
756
757 return 0;
758}
759
760static const struct udevice_id axi_emac_ids[] = {
761 { .compatible = "xlnx,axi-ethernet-1.00.a" },
762 { }
763};
764
765U_BOOT_DRIVER(axi_emac) = {
766 .name = "axi_emac",
767 .id = UCLASS_ETH,
768 .of_match = axi_emac_ids,
769 .ofdata_to_platdata = axi_emac_ofdata_to_platdata,
770 .probe = axi_emac_probe,
771 .remove = axi_emac_remove,
772 .ops = &axi_emac_ops,
773 .priv_auto_alloc_size = sizeof(struct axidma_priv),
774 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
775};
776