1
2
3
4
5#include <common.h>
6#include <clk.h>
7#include <cpu_func.h>
8#include <dm.h>
9#include <log.h>
10#include <linux/delay.h>
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <net.h>
35#ifndef CONFIG_DM_ETH
36#include <netdev.h>
37#endif
38#include <malloc.h>
39#include <miiphy.h>
40
41#include <linux/mii.h>
42#include <asm/io.h>
43#include <linux/dma-mapping.h>
44#include <asm/arch/clk.h>
45#include <linux/errno.h>
46
47#include "macb.h"
48
49DECLARE_GLOBAL_DATA_PTR;
50
51
52
53
54
55#define MACB_RX_BUFFER_SIZE 128
56#define GEM_RX_BUFFER_SIZE 2048
57#define RX_BUFFER_MULTIPLE 64
58
59#define MACB_RX_RING_SIZE 32
60#define MACB_TX_RING_SIZE 16
61
62#define MACB_TX_TIMEOUT 1000
63#define MACB_AUTONEG_TIMEOUT 5000000
64
65#ifdef CONFIG_MACB_ZYNQ
66
67#define MACB_ZYNQ_GEM_DMACR_BLENGTH 0x00000004
68
69#define MACB_ZYNQ_GEM_DMACR_RXSIZE 0x00000300
70
71#define MACB_ZYNQ_GEM_DMACR_TXSIZE 0x00000400
72
73#define MACB_ZYNQ_GEM_DMACR_RXBUF 0x00020000
74#define MACB_ZYNQ_GEM_DMACR_INIT \
75 (MACB_ZYNQ_GEM_DMACR_BLENGTH | \
76 MACB_ZYNQ_GEM_DMACR_RXSIZE | \
77 MACB_ZYNQ_GEM_DMACR_TXSIZE | \
78 MACB_ZYNQ_GEM_DMACR_RXBUF)
79#endif
80
81struct macb_dma_desc {
82 u32 addr;
83 u32 ctrl;
84};
85
86#define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
87#define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
88#define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
89#define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
90
91#define RXBUF_FRMLEN_MASK 0x00000fff
92#define TXBUF_FRMLEN_MASK 0x000007ff
93
94struct macb_device {
95 void *regs;
96
97 bool is_big_endian;
98
99 const struct macb_config *config;
100
101 unsigned int rx_tail;
102 unsigned int tx_head;
103 unsigned int tx_tail;
104 unsigned int next_rx_tail;
105 bool wrapped;
106
107 void *rx_buffer;
108 void *tx_buffer;
109 struct macb_dma_desc *rx_ring;
110 struct macb_dma_desc *tx_ring;
111 size_t rx_buffer_size;
112
113 unsigned long rx_buffer_dma;
114 unsigned long rx_ring_dma;
115 unsigned long tx_ring_dma;
116
117 struct macb_dma_desc *dummy_desc;
118 unsigned long dummy_desc_dma;
119
120 const struct device *dev;
121#ifndef CONFIG_DM_ETH
122 struct eth_device netdev;
123#endif
124 unsigned short phy_addr;
125 struct mii_dev *bus;
126#ifdef CONFIG_PHYLIB
127 struct phy_device *phydev;
128#endif
129
130#ifdef CONFIG_DM_ETH
131#ifdef CONFIG_CLK
132 unsigned long pclk_rate;
133#endif
134 phy_interface_t phy_interface;
135#endif
136};
137
138struct macb_config {
139 unsigned int dma_burst_length;
140
141 int (*clk_init)(struct udevice *dev, ulong rate);
142};
143
144#ifndef CONFIG_DM_ETH
145#define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
146#endif
147
148static int macb_is_gem(struct macb_device *macb)
149{
150 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
151}
152
153#ifndef cpu_is_sama5d2
154#define cpu_is_sama5d2() 0
155#endif
156
157#ifndef cpu_is_sama5d4
158#define cpu_is_sama5d4() 0
159#endif
160
161static int gem_is_gigabit_capable(struct macb_device *macb)
162{
163
164
165
166
167 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
168}
169
170static void macb_mdio_write(struct macb_device *macb, u8 phy_adr, u8 reg,
171 u16 value)
172{
173 unsigned long netctl;
174 unsigned long netstat;
175 unsigned long frame;
176
177 netctl = macb_readl(macb, NCR);
178 netctl |= MACB_BIT(MPE);
179 macb_writel(macb, NCR, netctl);
180
181 frame = (MACB_BF(SOF, 1)
182 | MACB_BF(RW, 1)
183 | MACB_BF(PHYA, phy_adr)
184 | MACB_BF(REGA, reg)
185 | MACB_BF(CODE, 2)
186 | MACB_BF(DATA, value));
187 macb_writel(macb, MAN, frame);
188
189 do {
190 netstat = macb_readl(macb, NSR);
191 } while (!(netstat & MACB_BIT(IDLE)));
192
193 netctl = macb_readl(macb, NCR);
194 netctl &= ~MACB_BIT(MPE);
195 macb_writel(macb, NCR, netctl);
196}
197
198static u16 macb_mdio_read(struct macb_device *macb, u8 phy_adr, u8 reg)
199{
200 unsigned long netctl;
201 unsigned long netstat;
202 unsigned long frame;
203
204 netctl = macb_readl(macb, NCR);
205 netctl |= MACB_BIT(MPE);
206 macb_writel(macb, NCR, netctl);
207
208 frame = (MACB_BF(SOF, 1)
209 | MACB_BF(RW, 2)
210 | MACB_BF(PHYA, phy_adr)
211 | MACB_BF(REGA, reg)
212 | MACB_BF(CODE, 2));
213 macb_writel(macb, MAN, frame);
214
215 do {
216 netstat = macb_readl(macb, NSR);
217 } while (!(netstat & MACB_BIT(IDLE)));
218
219 frame = macb_readl(macb, MAN);
220
221 netctl = macb_readl(macb, NCR);
222 netctl &= ~MACB_BIT(MPE);
223 macb_writel(macb, NCR, netctl);
224
225 return MACB_BFEXT(DATA, frame);
226}
227
228void __weak arch_get_mdio_control(const char *name)
229{
230 return;
231}
232
233#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
234
235int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
236{
237 u16 value = 0;
238#ifdef CONFIG_DM_ETH
239 struct udevice *dev = eth_get_dev_by_name(bus->name);
240 struct macb_device *macb = dev_get_priv(dev);
241#else
242 struct eth_device *dev = eth_get_dev_by_name(bus->name);
243 struct macb_device *macb = to_macb(dev);
244#endif
245
246 arch_get_mdio_control(bus->name);
247 value = macb_mdio_read(macb, phy_adr, reg);
248
249 return value;
250}
251
252int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
253 u16 value)
254{
255#ifdef CONFIG_DM_ETH
256 struct udevice *dev = eth_get_dev_by_name(bus->name);
257 struct macb_device *macb = dev_get_priv(dev);
258#else
259 struct eth_device *dev = eth_get_dev_by_name(bus->name);
260 struct macb_device *macb = to_macb(dev);
261#endif
262
263 arch_get_mdio_control(bus->name);
264 macb_mdio_write(macb, phy_adr, reg, value);
265
266 return 0;
267}
268#endif
269
270#define RX 1
271#define TX 0
272static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
273{
274 if (rx)
275 invalidate_dcache_range(macb->rx_ring_dma,
276 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
277 PKTALIGN));
278 else
279 invalidate_dcache_range(macb->tx_ring_dma,
280 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
281 PKTALIGN));
282}
283
284static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
285{
286 if (rx)
287 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
288 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
289 else
290 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
291 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
292}
293
294static inline void macb_flush_rx_buffer(struct macb_device *macb)
295{
296 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
297 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
298 PKTALIGN));
299}
300
301static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
302{
303 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
304 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
305 PKTALIGN));
306}
307
308#if defined(CONFIG_CMD_NET)
309
310static int _macb_send(struct macb_device *macb, const char *name, void *packet,
311 int length)
312{
313 unsigned long paddr, ctrl;
314 unsigned int tx_head = macb->tx_head;
315 int i;
316
317 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
318
319 ctrl = length & TXBUF_FRMLEN_MASK;
320 ctrl |= MACB_BIT(TX_LAST);
321 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
322 ctrl |= MACB_BIT(TX_WRAP);
323 macb->tx_head = 0;
324 } else {
325 macb->tx_head++;
326 }
327
328 macb->tx_ring[tx_head].ctrl = ctrl;
329 macb->tx_ring[tx_head].addr = paddr;
330 barrier();
331 macb_flush_ring_desc(macb, TX);
332 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
333
334
335
336
337
338 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
339 barrier();
340 macb_invalidate_ring_desc(macb, TX);
341 ctrl = macb->tx_ring[tx_head].ctrl;
342 if (ctrl & MACB_BIT(TX_USED))
343 break;
344 udelay(1);
345 }
346
347 dma_unmap_single(paddr, length, DMA_TO_DEVICE);
348
349 if (i <= MACB_TX_TIMEOUT) {
350 if (ctrl & MACB_BIT(TX_UNDERRUN))
351 printf("%s: TX underrun\n", name);
352 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
353 printf("%s: TX buffers exhausted in mid frame\n", name);
354 } else {
355 printf("%s: TX timeout\n", name);
356 }
357
358
359 return 0;
360}
361
362static void reclaim_rx_buffers(struct macb_device *macb,
363 unsigned int new_tail)
364{
365 unsigned int i;
366
367 i = macb->rx_tail;
368
369 macb_invalidate_ring_desc(macb, RX);
370 while (i > new_tail) {
371 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
372 i++;
373 if (i > MACB_RX_RING_SIZE)
374 i = 0;
375 }
376
377 while (i < new_tail) {
378 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
379 i++;
380 }
381
382 barrier();
383 macb_flush_ring_desc(macb, RX);
384 macb->rx_tail = new_tail;
385}
386
387static int _macb_recv(struct macb_device *macb, uchar **packetp)
388{
389 unsigned int next_rx_tail = macb->next_rx_tail;
390 void *buffer;
391 int length;
392 u32 status;
393
394 macb->wrapped = false;
395 for (;;) {
396 macb_invalidate_ring_desc(macb, RX);
397
398 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
399 return -EAGAIN;
400
401 status = macb->rx_ring[next_rx_tail].ctrl;
402 if (status & MACB_BIT(RX_SOF)) {
403 if (next_rx_tail != macb->rx_tail)
404 reclaim_rx_buffers(macb, next_rx_tail);
405 macb->wrapped = false;
406 }
407
408 if (status & MACB_BIT(RX_EOF)) {
409 buffer = macb->rx_buffer +
410 macb->rx_buffer_size * macb->rx_tail;
411 length = status & RXBUF_FRMLEN_MASK;
412
413 macb_invalidate_rx_buffer(macb);
414 if (macb->wrapped) {
415 unsigned int headlen, taillen;
416
417 headlen = macb->rx_buffer_size *
418 (MACB_RX_RING_SIZE - macb->rx_tail);
419 taillen = length - headlen;
420 memcpy((void *)net_rx_packets[0],
421 buffer, headlen);
422 memcpy((void *)net_rx_packets[0] + headlen,
423 macb->rx_buffer, taillen);
424 *packetp = (void *)net_rx_packets[0];
425 } else {
426 *packetp = buffer;
427 }
428
429 if (++next_rx_tail >= MACB_RX_RING_SIZE)
430 next_rx_tail = 0;
431 macb->next_rx_tail = next_rx_tail;
432 return length;
433 } else {
434 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
435 macb->wrapped = true;
436 next_rx_tail = 0;
437 }
438 }
439 barrier();
440 }
441}
442
443static void macb_phy_reset(struct macb_device *macb, const char *name)
444{
445 int i;
446 u16 status, adv;
447
448 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
449 macb_mdio_write(macb, macb->phy_addr, MII_ADVERTISE, adv);
450 printf("%s: Starting autonegotiation...\n", name);
451 macb_mdio_write(macb, macb->phy_addr, MII_BMCR, (BMCR_ANENABLE
452 | BMCR_ANRESTART));
453
454 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
455 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
456 if (status & BMSR_ANEGCOMPLETE)
457 break;
458 udelay(100);
459 }
460
461 if (status & BMSR_ANEGCOMPLETE)
462 printf("%s: Autonegotiation complete\n", name);
463 else
464 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
465 name, status);
466}
467
468static int macb_phy_find(struct macb_device *macb, const char *name)
469{
470 int i;
471 u16 phy_id;
472
473
474 for (i = 0; i < 32; i++) {
475 macb->phy_addr = i;
476 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
477 if (phy_id != 0xffff) {
478 printf("%s: PHY present at %d\n", name, i);
479 return 0;
480 }
481 }
482
483
484 printf("%s: PHY not found\n", name);
485
486 return -ENODEV;
487}
488
489
490
491
492
493
494
495
496
497#ifdef CONFIG_DM_ETH
498static int macb_sifive_clk_init(struct udevice *dev, ulong rate)
499{
500 fdt_addr_t addr;
501 void *gemgxl_regs;
502
503 addr = dev_read_addr_index(dev, 1);
504 if (addr == FDT_ADDR_T_NONE)
505 return -ENODEV;
506
507 gemgxl_regs = (void __iomem *)addr;
508 if (!gemgxl_regs)
509 return -ENODEV;
510
511
512
513
514
515
516
517
518 writel(rate != 125000000, gemgxl_regs);
519 return 0;
520}
521
522int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
523{
524#ifdef CONFIG_CLK
525 struct macb_device *macb = dev_get_priv(dev);
526 struct clk tx_clk;
527 ulong rate;
528 int ret;
529
530 switch (speed) {
531 case _10BASET:
532 rate = 2500000;
533 break;
534 case _100BASET:
535 rate = 25000000;
536 break;
537 case _1000BASET:
538 rate = 125000000;
539 break;
540 default:
541
542 return 0;
543 }
544
545 if (macb->config->clk_init)
546 return macb->config->clk_init(dev, rate);
547
548
549
550
551
552 ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
553 if (ret)
554 return 0;
555
556 if (tx_clk.dev) {
557 ret = clk_set_rate(&tx_clk, rate);
558 if (ret)
559 return ret;
560 }
561#endif
562
563 return 0;
564}
565#else
566int __weak macb_linkspd_cb(void *regs, unsigned int speed)
567{
568 return 0;
569}
570#endif
571
572#ifdef CONFIG_DM_ETH
573static int macb_phy_init(struct udevice *dev, const char *name)
574#else
575static int macb_phy_init(struct macb_device *macb, const char *name)
576#endif
577{
578#ifdef CONFIG_DM_ETH
579 struct macb_device *macb = dev_get_priv(dev);
580#endif
581 u32 ncfgr;
582 u16 phy_id, status, adv, lpa;
583 int media, speed, duplex;
584 int ret;
585 int i;
586
587 arch_get_mdio_control(name);
588
589 ret = macb_phy_find(macb, name);
590 if (ret)
591 return ret;
592
593
594 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
595 if (phy_id == 0xffff) {
596 printf("%s: No PHY present\n", name);
597 return -ENODEV;
598 }
599
600#ifdef CONFIG_PHYLIB
601#ifdef CONFIG_DM_ETH
602 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
603 macb->phy_interface);
604#else
605
606 macb->phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
607 PHY_INTERFACE_MODE_RGMII);
608#endif
609 if (!macb->phydev) {
610 printf("phy_connect failed\n");
611 return -ENODEV;
612 }
613
614 phy_config(macb->phydev);
615#endif
616
617 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
618 if (!(status & BMSR_LSTATUS)) {
619
620 macb_phy_reset(macb, name);
621
622 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
623 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
624 if (status & BMSR_LSTATUS) {
625
626
627
628
629 mdelay(10);
630 break;
631 }
632 udelay(100);
633 }
634 }
635
636 if (!(status & BMSR_LSTATUS)) {
637 printf("%s: link down (status: 0x%04x)\n",
638 name, status);
639 return -ENETDOWN;
640 }
641
642
643 if (gem_is_gigabit_capable(macb)) {
644 lpa = macb_mdio_read(macb, macb->phy_addr, MII_STAT1000);
645
646 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
647 LPA_1000XHALF)) {
648 duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
649 1 : 0);
650
651 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
652 name,
653 duplex ? "full" : "half",
654 lpa);
655
656 ncfgr = macb_readl(macb, NCFGR);
657 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
658 ncfgr |= GEM_BIT(GBE);
659
660 if (duplex)
661 ncfgr |= MACB_BIT(FD);
662
663 macb_writel(macb, NCFGR, ncfgr);
664
665#ifdef CONFIG_DM_ETH
666 ret = macb_linkspd_cb(dev, _1000BASET);
667#else
668 ret = macb_linkspd_cb(macb->regs, _1000BASET);
669#endif
670 if (ret)
671 return ret;
672
673 return 0;
674 }
675 }
676
677
678 adv = macb_mdio_read(macb, macb->phy_addr, MII_ADVERTISE);
679 lpa = macb_mdio_read(macb, macb->phy_addr, MII_LPA);
680 media = mii_nway_result(lpa & adv);
681 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
682 ? 1 : 0);
683 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
684 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
685 name,
686 speed ? "100" : "10",
687 duplex ? "full" : "half",
688 lpa);
689
690 ncfgr = macb_readl(macb, NCFGR);
691 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
692 if (speed) {
693 ncfgr |= MACB_BIT(SPD);
694#ifdef CONFIG_DM_ETH
695 ret = macb_linkspd_cb(dev, _100BASET);
696#else
697 ret = macb_linkspd_cb(macb->regs, _100BASET);
698#endif
699 } else {
700#ifdef CONFIG_DM_ETH
701 ret = macb_linkspd_cb(dev, _10BASET);
702#else
703 ret = macb_linkspd_cb(macb->regs, _10BASET);
704#endif
705 }
706
707 if (ret)
708 return ret;
709
710 if (duplex)
711 ncfgr |= MACB_BIT(FD);
712 macb_writel(macb, NCFGR, ncfgr);
713
714 return 0;
715}
716
717static int gmac_init_multi_queues(struct macb_device *macb)
718{
719 int i, num_queues = 1;
720 u32 queue_mask;
721
722
723 queue_mask = gem_readl(macb, DCFG6) & 0xff;
724 queue_mask |= 0x1;
725
726 for (i = 1; i < MACB_MAX_QUEUES; i++)
727 if (queue_mask & (1 << i))
728 num_queues++;
729
730 macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
731 macb->dummy_desc->addr = 0;
732 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
733 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
734
735 for (i = 1; i < num_queues; i++)
736 gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
737
738 return 0;
739}
740
741static void gmac_configure_dma(struct macb_device *macb)
742{
743 u32 buffer_size;
744 u32 dmacfg;
745
746 buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
747 dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
748 dmacfg |= GEM_BF(RXBS, buffer_size);
749
750 if (macb->config->dma_burst_length)
751 dmacfg = GEM_BFINS(FBLDO,
752 macb->config->dma_burst_length, dmacfg);
753
754 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
755 dmacfg &= ~GEM_BIT(ENDIA_PKT);
756
757 if (macb->is_big_endian)
758 dmacfg |= GEM_BIT(ENDIA_DESC);
759 else
760 dmacfg &= ~GEM_BIT(ENDIA_DESC);
761
762 dmacfg &= ~GEM_BIT(ADDR64);
763 gem_writel(macb, DMACFG, dmacfg);
764}
765
766#ifdef CONFIG_DM_ETH
767static int _macb_init(struct udevice *dev, const char *name)
768#else
769static int _macb_init(struct macb_device *macb, const char *name)
770#endif
771{
772#ifdef CONFIG_DM_ETH
773 struct macb_device *macb = dev_get_priv(dev);
774#endif
775 unsigned long paddr;
776 int ret;
777 int i;
778
779
780
781
782
783
784
785 paddr = macb->rx_buffer_dma;
786 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
787 if (i == (MACB_RX_RING_SIZE - 1))
788 paddr |= MACB_BIT(RX_WRAP);
789 macb->rx_ring[i].addr = paddr;
790 macb->rx_ring[i].ctrl = 0;
791 paddr += macb->rx_buffer_size;
792 }
793 macb_flush_ring_desc(macb, RX);
794 macb_flush_rx_buffer(macb);
795
796 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
797 macb->tx_ring[i].addr = 0;
798 if (i == (MACB_TX_RING_SIZE - 1))
799 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED) |
800 MACB_BIT(TX_WRAP);
801 else
802 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED);
803 }
804 macb_flush_ring_desc(macb, TX);
805
806 macb->rx_tail = 0;
807 macb->tx_head = 0;
808 macb->tx_tail = 0;
809 macb->next_rx_tail = 0;
810
811#ifdef CONFIG_MACB_ZYNQ
812 gem_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
813#endif
814
815 macb_writel(macb, RBQP, macb->rx_ring_dma);
816 macb_writel(macb, TBQP, macb->tx_ring_dma);
817
818 if (macb_is_gem(macb)) {
819
820 gmac_configure_dma(macb);
821
822 gmac_init_multi_queues(macb);
823
824
825
826
827
828
829
830#ifdef CONFIG_DM_ETH
831 if ((macb->phy_interface == PHY_INTERFACE_MODE_RMII) ||
832 (macb->phy_interface == PHY_INTERFACE_MODE_RGMII))
833 gem_writel(macb, USRIO, GEM_BIT(RGMII));
834 else
835 gem_writel(macb, USRIO, 0);
836
837 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
838 unsigned int ncfgr = macb_readl(macb, NCFGR);
839
840 ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
841 macb_writel(macb, NCFGR, ncfgr);
842 }
843#else
844#if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
845 gem_writel(macb, USRIO, GEM_BIT(RGMII));
846#else
847 gem_writel(macb, USRIO, 0);
848#endif
849#endif
850 } else {
851
852#ifdef CONFIG_DM_ETH
853#ifdef CONFIG_AT91FAMILY
854 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
855 macb_writel(macb, USRIO,
856 MACB_BIT(RMII) | MACB_BIT(CLKEN));
857 } else {
858 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
859 }
860#else
861 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
862 macb_writel(macb, USRIO, 0);
863 else
864 macb_writel(macb, USRIO, MACB_BIT(MII));
865#endif
866#else
867#ifdef CONFIG_RMII
868#ifdef CONFIG_AT91FAMILY
869 macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
870#else
871 macb_writel(macb, USRIO, 0);
872#endif
873#else
874#ifdef CONFIG_AT91FAMILY
875 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
876#else
877 macb_writel(macb, USRIO, MACB_BIT(MII));
878#endif
879#endif
880#endif
881 }
882
883#ifdef CONFIG_DM_ETH
884 ret = macb_phy_init(dev, name);
885#else
886 ret = macb_phy_init(macb, name);
887#endif
888 if (ret)
889 return ret;
890
891
892 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
893
894 return 0;
895}
896
897static void _macb_halt(struct macb_device *macb)
898{
899 u32 ncr, tsr;
900
901
902 ncr = macb_readl(macb, NCR);
903 ncr |= MACB_BIT(THALT);
904 macb_writel(macb, NCR, ncr);
905
906 do {
907 tsr = macb_readl(macb, TSR);
908 } while (tsr & MACB_BIT(TGO));
909
910
911 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
912}
913
914static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
915{
916 u32 hwaddr_bottom;
917 u16 hwaddr_top;
918
919
920 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
921 enetaddr[2] << 16 | enetaddr[3] << 24;
922 macb_writel(macb, SA1B, hwaddr_bottom);
923 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
924 macb_writel(macb, SA1T, hwaddr_top);
925 return 0;
926}
927
928static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
929{
930 u32 config;
931#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
932 unsigned long macb_hz = macb->pclk_rate;
933#else
934 unsigned long macb_hz = get_macb_pclk_rate(id);
935#endif
936
937 if (macb_hz < 20000000)
938 config = MACB_BF(CLK, MACB_CLK_DIV8);
939 else if (macb_hz < 40000000)
940 config = MACB_BF(CLK, MACB_CLK_DIV16);
941 else if (macb_hz < 80000000)
942 config = MACB_BF(CLK, MACB_CLK_DIV32);
943 else
944 config = MACB_BF(CLK, MACB_CLK_DIV64);
945
946 return config;
947}
948
949static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
950{
951 u32 config;
952
953#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
954 unsigned long macb_hz = macb->pclk_rate;
955#else
956 unsigned long macb_hz = get_macb_pclk_rate(id);
957#endif
958
959 if (macb_hz < 20000000)
960 config = GEM_BF(CLK, GEM_CLK_DIV8);
961 else if (macb_hz < 40000000)
962 config = GEM_BF(CLK, GEM_CLK_DIV16);
963 else if (macb_hz < 80000000)
964 config = GEM_BF(CLK, GEM_CLK_DIV32);
965 else if (macb_hz < 120000000)
966 config = GEM_BF(CLK, GEM_CLK_DIV48);
967 else if (macb_hz < 160000000)
968 config = GEM_BF(CLK, GEM_CLK_DIV64);
969 else if (macb_hz < 240000000)
970 config = GEM_BF(CLK, GEM_CLK_DIV96);
971 else if (macb_hz < 320000000)
972 config = GEM_BF(CLK, GEM_CLK_DIV128);
973 else
974 config = GEM_BF(CLK, GEM_CLK_DIV224);
975
976 return config;
977}
978
979
980
981
982
983
984static u32 macb_dbw(struct macb_device *macb)
985{
986 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
987 case 4:
988 return GEM_BF(DBW, GEM_DBW128);
989 case 2:
990 return GEM_BF(DBW, GEM_DBW64);
991 case 1:
992 default:
993 return GEM_BF(DBW, GEM_DBW32);
994 }
995}
996
997static void _macb_eth_initialize(struct macb_device *macb)
998{
999 int id = 0;
1000 u32 ncfgr;
1001
1002 if (macb_is_gem(macb))
1003 macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
1004 else
1005 macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1006
1007
1008 macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
1009 MACB_RX_RING_SIZE,
1010 &macb->rx_buffer_dma);
1011 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
1012 &macb->rx_ring_dma);
1013 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
1014 &macb->tx_ring_dma);
1015 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
1016 &macb->dummy_desc_dma);
1017
1018
1019
1020
1021
1022 if (macb_is_gem(macb)) {
1023 ncfgr = gem_mdc_clk_div(id, macb);
1024 ncfgr |= macb_dbw(macb);
1025 } else {
1026 ncfgr = macb_mdc_clk_div(id, macb);
1027 }
1028
1029 macb_writel(macb, NCFGR, ncfgr);
1030}
1031
1032#ifndef CONFIG_DM_ETH
1033static int macb_send(struct eth_device *netdev, void *packet, int length)
1034{
1035 struct macb_device *macb = to_macb(netdev);
1036
1037 return _macb_send(macb, netdev->name, packet, length);
1038}
1039
1040static int macb_recv(struct eth_device *netdev)
1041{
1042 struct macb_device *macb = to_macb(netdev);
1043 uchar *packet;
1044 int length;
1045
1046 macb->wrapped = false;
1047 for (;;) {
1048 macb->next_rx_tail = macb->rx_tail;
1049 length = _macb_recv(macb, &packet);
1050 if (length >= 0) {
1051 net_process_received_packet(packet, length);
1052 reclaim_rx_buffers(macb, macb->next_rx_tail);
1053 } else {
1054 return length;
1055 }
1056 }
1057}
1058
1059static int macb_init(struct eth_device *netdev, struct bd_info *bd)
1060{
1061 struct macb_device *macb = to_macb(netdev);
1062
1063 return _macb_init(macb, netdev->name);
1064}
1065
1066static void macb_halt(struct eth_device *netdev)
1067{
1068 struct macb_device *macb = to_macb(netdev);
1069
1070 return _macb_halt(macb);
1071}
1072
1073static int macb_write_hwaddr(struct eth_device *netdev)
1074{
1075 struct macb_device *macb = to_macb(netdev);
1076
1077 return _macb_write_hwaddr(macb, netdev->enetaddr);
1078}
1079
1080int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
1081{
1082 struct macb_device *macb;
1083 struct eth_device *netdev;
1084
1085 macb = malloc(sizeof(struct macb_device));
1086 if (!macb) {
1087 printf("Error: Failed to allocate memory for MACB%d\n", id);
1088 return -1;
1089 }
1090 memset(macb, 0, sizeof(struct macb_device));
1091
1092 netdev = &macb->netdev;
1093
1094 macb->regs = regs;
1095 macb->phy_addr = phy_addr;
1096
1097 if (macb_is_gem(macb))
1098 sprintf(netdev->name, "gmac%d", id);
1099 else
1100 sprintf(netdev->name, "macb%d", id);
1101
1102 netdev->init = macb_init;
1103 netdev->halt = macb_halt;
1104 netdev->send = macb_send;
1105 netdev->recv = macb_recv;
1106 netdev->write_hwaddr = macb_write_hwaddr;
1107
1108 _macb_eth_initialize(macb);
1109
1110 eth_register(netdev);
1111
1112#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
1113 int retval;
1114 struct mii_dev *mdiodev = mdio_alloc();
1115 if (!mdiodev)
1116 return -ENOMEM;
1117 strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
1118 mdiodev->read = macb_miiphy_read;
1119 mdiodev->write = macb_miiphy_write;
1120
1121 retval = mdio_register(mdiodev);
1122 if (retval < 0)
1123 return retval;
1124 macb->bus = miiphy_get_dev_by_name(netdev->name);
1125#endif
1126 return 0;
1127}
1128#endif
1129
1130#ifdef CONFIG_DM_ETH
1131
1132static int macb_start(struct udevice *dev)
1133{
1134 return _macb_init(dev, dev->name);
1135}
1136
1137static int macb_send(struct udevice *dev, void *packet, int length)
1138{
1139 struct macb_device *macb = dev_get_priv(dev);
1140
1141 return _macb_send(macb, dev->name, packet, length);
1142}
1143
1144static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1145{
1146 struct macb_device *macb = dev_get_priv(dev);
1147
1148 macb->next_rx_tail = macb->rx_tail;
1149 macb->wrapped = false;
1150
1151 return _macb_recv(macb, packetp);
1152}
1153
1154static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1155{
1156 struct macb_device *macb = dev_get_priv(dev);
1157
1158 reclaim_rx_buffers(macb, macb->next_rx_tail);
1159
1160 return 0;
1161}
1162
1163static void macb_stop(struct udevice *dev)
1164{
1165 struct macb_device *macb = dev_get_priv(dev);
1166
1167 _macb_halt(macb);
1168}
1169
1170static int macb_write_hwaddr(struct udevice *dev)
1171{
1172 struct eth_pdata *plat = dev_get_platdata(dev);
1173 struct macb_device *macb = dev_get_priv(dev);
1174
1175 return _macb_write_hwaddr(macb, plat->enetaddr);
1176}
1177
1178static const struct eth_ops macb_eth_ops = {
1179 .start = macb_start,
1180 .send = macb_send,
1181 .recv = macb_recv,
1182 .stop = macb_stop,
1183 .free_pkt = macb_free_pkt,
1184 .write_hwaddr = macb_write_hwaddr,
1185};
1186
1187#ifdef CONFIG_CLK
1188static int macb_enable_clk(struct udevice *dev)
1189{
1190 struct macb_device *macb = dev_get_priv(dev);
1191 struct clk clk;
1192 ulong clk_rate;
1193 int ret;
1194
1195 ret = clk_get_by_index(dev, 0, &clk);
1196 if (ret)
1197 return -EINVAL;
1198
1199
1200
1201
1202
1203
1204 ret = clk_enable(&clk);
1205 if (ret && ret != -ENOSYS)
1206 return ret;
1207
1208 clk_rate = clk_get_rate(&clk);
1209 if (!clk_rate)
1210 return -EINVAL;
1211
1212 macb->pclk_rate = clk_rate;
1213
1214 return 0;
1215}
1216#endif
1217
1218static const struct macb_config default_gem_config = {
1219 .dma_burst_length = 16,
1220 .clk_init = NULL,
1221};
1222
1223static int macb_eth_probe(struct udevice *dev)
1224{
1225 struct eth_pdata *pdata = dev_get_platdata(dev);
1226 struct macb_device *macb = dev_get_priv(dev);
1227 const char *phy_mode;
1228 int ret;
1229
1230 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1231 NULL);
1232 if (phy_mode)
1233 macb->phy_interface = phy_get_interface_by_name(phy_mode);
1234 if (macb->phy_interface == -1) {
1235 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1236 return -EINVAL;
1237 }
1238
1239 macb->regs = (void *)pdata->iobase;
1240
1241 macb->is_big_endian = (cpu_to_be32(0x12345678) == 0x12345678);
1242
1243 macb->config = (struct macb_config *)dev_get_driver_data(dev);
1244 if (!macb->config)
1245 macb->config = &default_gem_config;
1246
1247#ifdef CONFIG_CLK
1248 ret = macb_enable_clk(dev);
1249 if (ret)
1250 return ret;
1251#endif
1252
1253 _macb_eth_initialize(macb);
1254
1255#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
1256 macb->bus = mdio_alloc();
1257 if (!macb->bus)
1258 return -ENOMEM;
1259 strncpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
1260 macb->bus->read = macb_miiphy_read;
1261 macb->bus->write = macb_miiphy_write;
1262
1263 ret = mdio_register(macb->bus);
1264 if (ret < 0)
1265 return ret;
1266 macb->bus = miiphy_get_dev_by_name(dev->name);
1267#endif
1268
1269 return 0;
1270}
1271
1272static int macb_eth_remove(struct udevice *dev)
1273{
1274 struct macb_device *macb = dev_get_priv(dev);
1275
1276#ifdef CONFIG_PHYLIB
1277 free(macb->phydev);
1278#endif
1279 mdio_unregister(macb->bus);
1280 mdio_free(macb->bus);
1281
1282 return 0;
1283}
1284
1285
1286
1287
1288
1289
1290
1291int __weak macb_late_eth_ofdata_to_platdata(struct udevice *dev)
1292{
1293 return 0;
1294}
1295
1296static int macb_eth_ofdata_to_platdata(struct udevice *dev)
1297{
1298 struct eth_pdata *pdata = dev_get_platdata(dev);
1299
1300 pdata->iobase = (phys_addr_t)dev_remap_addr(dev);
1301 if (!pdata->iobase)
1302 return -EINVAL;
1303
1304 return macb_late_eth_ofdata_to_platdata(dev);
1305}
1306
1307static const struct macb_config sama5d4_config = {
1308 .dma_burst_length = 4,
1309 .clk_init = NULL,
1310};
1311
1312static const struct macb_config sifive_config = {
1313 .dma_burst_length = 16,
1314 .clk_init = macb_sifive_clk_init,
1315};
1316
1317static const struct udevice_id macb_eth_ids[] = {
1318 { .compatible = "cdns,macb" },
1319 { .compatible = "cdns,at91sam9260-macb" },
1320 { .compatible = "cdns,sam9x60-macb" },
1321 { .compatible = "atmel,sama5d2-gem" },
1322 { .compatible = "atmel,sama5d3-gem" },
1323 { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
1324 { .compatible = "cdns,zynq-gem" },
1325 { .compatible = "sifive,fu540-c000-gem",
1326 .data = (ulong)&sifive_config },
1327 { }
1328};
1329
1330U_BOOT_DRIVER(eth_macb) = {
1331 .name = "eth_macb",
1332 .id = UCLASS_ETH,
1333 .of_match = macb_eth_ids,
1334 .ofdata_to_platdata = macb_eth_ofdata_to_platdata,
1335 .probe = macb_eth_probe,
1336 .remove = macb_eth_remove,
1337 .ops = &macb_eth_ops,
1338 .priv_auto_alloc_size = sizeof(struct macb_device),
1339 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1340};
1341#endif
1342
1343#endif
1344