]> Git Repo - J-u-boot.git/blob - drivers/net/macb.c
Merge tag 'u-boot-imx-20210125' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[J-u-boot.git] / drivers / net / macb.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2005-2006 Atmel Corporation
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  * The u-boot networking stack is a little weird.  It seems like the
14  * networking core allocates receive buffers up front without any
15  * regard to the hardware that's supposed to actually receive those
16  * packets.
17  *
18  * The MACB receives packets into 128-byte receive buffers, so the
19  * buffers allocated by the core isn't very practical to use.  We'll
20  * allocate our own, but we need one such buffer in case a packet
21  * wraps around the DMA ring so that we have to copy it.
22  *
23  * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
24  * configuration header.  This way, the core allocates one RX buffer
25  * and one TX buffer, each of which can hold a ethernet packet of
26  * maximum size.
27  *
28  * For some reason, the networking core unconditionally specifies a
29  * 32-byte packet "alignment" (which really should be called
30  * "padding").  MACB shouldn't need that, but we'll refrain from any
31  * core modifications here...
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
49 DECLARE_GLOBAL_DATA_PTR;
50
51 /*
52  * These buffer sizes must be power of 2 and divisible
53  * by RX_BUFFER_MULTIPLE
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 /* INCR4 AHB bursts */
67 #define MACB_ZYNQ_GEM_DMACR_BLENGTH             0x00000004
68 /* Use full configured addressable space (8 Kb) */
69 #define MACB_ZYNQ_GEM_DMACR_RXSIZE              0x00000300
70 /* Use full configured addressable space (4 Kb) */
71 #define MACB_ZYNQ_GEM_DMACR_TXSIZE              0x00000400
72 /* Set RXBUF with use of 128 byte */
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
81 struct macb_dma_desc {
82         u32     addr;
83         u32     ctrl;
84 };
85
86 struct macb_dma_desc_64 {
87         u32 addrh;
88         u32 unused;
89 };
90
91 #define HW_DMA_CAP_32B          0
92 #define HW_DMA_CAP_64B          1
93
94 #define DMA_DESC_SIZE           16
95 #define DMA_DESC_BYTES(n)       ((n) * DMA_DESC_SIZE)
96 #define MACB_TX_DMA_DESC_SIZE   (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
97 #define MACB_RX_DMA_DESC_SIZE   (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
98 #define MACB_TX_DUMMY_DMA_DESC_SIZE     (DMA_DESC_BYTES(1))
99
100 #define RXBUF_FRMLEN_MASK       0x00000fff
101 #define TXBUF_FRMLEN_MASK       0x000007ff
102
103 struct macb_device {
104         void                    *regs;
105
106         bool                    is_big_endian;
107
108         const struct macb_config *config;
109
110         unsigned int            rx_tail;
111         unsigned int            tx_head;
112         unsigned int            tx_tail;
113         unsigned int            next_rx_tail;
114         bool                    wrapped;
115
116         void                    *rx_buffer;
117         void                    *tx_buffer;
118         struct macb_dma_desc    *rx_ring;
119         struct macb_dma_desc    *tx_ring;
120         size_t                  rx_buffer_size;
121
122         unsigned long           rx_buffer_dma;
123         unsigned long           rx_ring_dma;
124         unsigned long           tx_ring_dma;
125
126         struct macb_dma_desc    *dummy_desc;
127         unsigned long           dummy_desc_dma;
128
129         const struct device     *dev;
130 #ifndef CONFIG_DM_ETH
131         struct eth_device       netdev;
132 #endif
133         unsigned short          phy_addr;
134         struct mii_dev          *bus;
135 #ifdef CONFIG_PHYLIB
136         struct phy_device       *phydev;
137 #endif
138
139 #ifdef CONFIG_DM_ETH
140 #ifdef CONFIG_CLK
141         unsigned long           pclk_rate;
142 #endif
143         phy_interface_t         phy_interface;
144 #endif
145 };
146
147 struct macb_usrio_cfg {
148         unsigned int            mii;
149         unsigned int            rmii;
150         unsigned int            rgmii;
151         unsigned int            clken;
152 };
153
154 struct macb_config {
155         unsigned int            dma_burst_length;
156         unsigned int            hw_dma_cap;
157         unsigned int            caps;
158
159         int                     (*clk_init)(struct udevice *dev, ulong rate);
160         const struct macb_usrio_cfg     *usrio;
161 };
162
163 #ifndef CONFIG_DM_ETH
164 #define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
165 #endif
166
167 static int macb_is_gem(struct macb_device *macb)
168 {
169         return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
170 }
171
172 #ifndef cpu_is_sama5d2
173 #define cpu_is_sama5d2() 0
174 #endif
175
176 #ifndef cpu_is_sama5d4
177 #define cpu_is_sama5d4() 0
178 #endif
179
180 static int gem_is_gigabit_capable(struct macb_device *macb)
181 {
182         /*
183          * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
184          * configured to support only 10/100.
185          */
186         return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
187 }
188
189 static void macb_mdio_write(struct macb_device *macb, u8 phy_adr, u8 reg,
190                             u16 value)
191 {
192         unsigned long netctl;
193         unsigned long netstat;
194         unsigned long frame;
195
196         netctl = macb_readl(macb, NCR);
197         netctl |= MACB_BIT(MPE);
198         macb_writel(macb, NCR, netctl);
199
200         frame = (MACB_BF(SOF, 1)
201                  | MACB_BF(RW, 1)
202                  | MACB_BF(PHYA, phy_adr)
203                  | MACB_BF(REGA, reg)
204                  | MACB_BF(CODE, 2)
205                  | MACB_BF(DATA, value));
206         macb_writel(macb, MAN, frame);
207
208         do {
209                 netstat = macb_readl(macb, NSR);
210         } while (!(netstat & MACB_BIT(IDLE)));
211
212         netctl = macb_readl(macb, NCR);
213         netctl &= ~MACB_BIT(MPE);
214         macb_writel(macb, NCR, netctl);
215 }
216
217 static u16 macb_mdio_read(struct macb_device *macb, u8 phy_adr, u8 reg)
218 {
219         unsigned long netctl;
220         unsigned long netstat;
221         unsigned long frame;
222
223         netctl = macb_readl(macb, NCR);
224         netctl |= MACB_BIT(MPE);
225         macb_writel(macb, NCR, netctl);
226
227         frame = (MACB_BF(SOF, 1)
228                  | MACB_BF(RW, 2)
229                  | MACB_BF(PHYA, phy_adr)
230                  | MACB_BF(REGA, reg)
231                  | MACB_BF(CODE, 2));
232         macb_writel(macb, MAN, frame);
233
234         do {
235                 netstat = macb_readl(macb, NSR);
236         } while (!(netstat & MACB_BIT(IDLE)));
237
238         frame = macb_readl(macb, MAN);
239
240         netctl = macb_readl(macb, NCR);
241         netctl &= ~MACB_BIT(MPE);
242         macb_writel(macb, NCR, netctl);
243
244         return MACB_BFEXT(DATA, frame);
245 }
246
247 void __weak arch_get_mdio_control(const char *name)
248 {
249         return;
250 }
251
252 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
253
254 int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
255 {
256         u16 value = 0;
257 #ifdef CONFIG_DM_ETH
258         struct udevice *dev = eth_get_dev_by_name(bus->name);
259         struct macb_device *macb = dev_get_priv(dev);
260 #else
261         struct eth_device *dev = eth_get_dev_by_name(bus->name);
262         struct macb_device *macb = to_macb(dev);
263 #endif
264
265         arch_get_mdio_control(bus->name);
266         value = macb_mdio_read(macb, phy_adr, reg);
267
268         return value;
269 }
270
271 int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
272                       u16 value)
273 {
274 #ifdef CONFIG_DM_ETH
275         struct udevice *dev = eth_get_dev_by_name(bus->name);
276         struct macb_device *macb = dev_get_priv(dev);
277 #else
278         struct eth_device *dev = eth_get_dev_by_name(bus->name);
279         struct macb_device *macb = to_macb(dev);
280 #endif
281
282         arch_get_mdio_control(bus->name);
283         macb_mdio_write(macb, phy_adr, reg, value);
284
285         return 0;
286 }
287 #endif
288
289 #define RX      1
290 #define TX      0
291 static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
292 {
293         if (rx)
294                 invalidate_dcache_range(macb->rx_ring_dma,
295                         ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
296                               PKTALIGN));
297         else
298                 invalidate_dcache_range(macb->tx_ring_dma,
299                         ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
300                               PKTALIGN));
301 }
302
303 static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
304 {
305         if (rx)
306                 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
307                                    ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
308         else
309                 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
310                                    ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
311 }
312
313 static inline void macb_flush_rx_buffer(struct macb_device *macb)
314 {
315         flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
316                            ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
317                                  PKTALIGN));
318 }
319
320 static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
321 {
322         invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
323                                 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
324                                       PKTALIGN));
325 }
326
327 #if defined(CONFIG_CMD_NET)
328
329 static struct macb_dma_desc_64 *macb_64b_desc(struct macb_dma_desc *desc)
330 {
331         return (struct macb_dma_desc_64 *)((void *)desc
332                 + sizeof(struct macb_dma_desc));
333 }
334
335 static void macb_set_addr(struct macb_device *macb, struct macb_dma_desc *desc,
336                           ulong addr)
337 {
338         struct macb_dma_desc_64 *desc_64;
339
340         if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
341                 desc_64 = macb_64b_desc(desc);
342                 desc_64->addrh = upper_32_bits(addr);
343         }
344         desc->addr = lower_32_bits(addr);
345 }
346
347 static int _macb_send(struct macb_device *macb, const char *name, void *packet,
348                       int length)
349 {
350         unsigned long paddr, ctrl;
351         unsigned int tx_head = macb->tx_head;
352         int i;
353
354         paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
355
356         ctrl = length & TXBUF_FRMLEN_MASK;
357         ctrl |= MACB_BIT(TX_LAST);
358         if (tx_head == (MACB_TX_RING_SIZE - 1)) {
359                 ctrl |= MACB_BIT(TX_WRAP);
360                 macb->tx_head = 0;
361         } else {
362                 macb->tx_head++;
363         }
364
365         if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
366                 tx_head = tx_head * 2;
367
368         macb->tx_ring[tx_head].ctrl = ctrl;
369         macb_set_addr(macb, &macb->tx_ring[tx_head], paddr);
370
371         barrier();
372         macb_flush_ring_desc(macb, TX);
373         macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
374
375         /*
376          * I guess this is necessary because the networking core may
377          * re-use the transmit buffer as soon as we return...
378          */
379         for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
380                 barrier();
381                 macb_invalidate_ring_desc(macb, TX);
382                 ctrl = macb->tx_ring[tx_head].ctrl;
383                 if (ctrl & MACB_BIT(TX_USED))
384                         break;
385                 udelay(1);
386         }
387
388         dma_unmap_single(paddr, length, DMA_TO_DEVICE);
389
390         if (i <= MACB_TX_TIMEOUT) {
391                 if (ctrl & MACB_BIT(TX_UNDERRUN))
392                         printf("%s: TX underrun\n", name);
393                 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
394                         printf("%s: TX buffers exhausted in mid frame\n", name);
395         } else {
396                 printf("%s: TX timeout\n", name);
397         }
398
399         /* No one cares anyway */
400         return 0;
401 }
402
403 static void reclaim_rx_buffers(struct macb_device *macb,
404                                unsigned int new_tail)
405 {
406         unsigned int i;
407         unsigned int count;
408
409         i = macb->rx_tail;
410
411         macb_invalidate_ring_desc(macb, RX);
412         while (i > new_tail) {
413                 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
414                         count = i * 2;
415                 else
416                         count = i;
417                 macb->rx_ring[count].addr &= ~MACB_BIT(RX_USED);
418                 i++;
419                 if (i > MACB_RX_RING_SIZE)
420                         i = 0;
421         }
422
423         while (i < new_tail) {
424                 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
425                         count = i * 2;
426                 else
427                         count = i;
428                 macb->rx_ring[count].addr &= ~MACB_BIT(RX_USED);
429                 i++;
430         }
431
432         barrier();
433         macb_flush_ring_desc(macb, RX);
434         macb->rx_tail = new_tail;
435 }
436
437 static int _macb_recv(struct macb_device *macb, uchar **packetp)
438 {
439         unsigned int next_rx_tail = macb->next_rx_tail;
440         void *buffer;
441         int length;
442         u32 status;
443         u8 flag = false;
444
445         macb->wrapped = false;
446         for (;;) {
447                 macb_invalidate_ring_desc(macb, RX);
448
449                 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
450                         next_rx_tail = next_rx_tail * 2;
451
452                 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
453                         return -EAGAIN;
454
455                 status = macb->rx_ring[next_rx_tail].ctrl;
456                 if (status & MACB_BIT(RX_SOF)) {
457                         if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
458                                 next_rx_tail = next_rx_tail / 2;
459                                 flag = true;
460                         }
461
462                         if (next_rx_tail != macb->rx_tail)
463                                 reclaim_rx_buffers(macb, next_rx_tail);
464                         macb->wrapped = false;
465                 }
466
467                 if (status & MACB_BIT(RX_EOF)) {
468                         buffer = macb->rx_buffer +
469                                 macb->rx_buffer_size * macb->rx_tail;
470                         length = status & RXBUF_FRMLEN_MASK;
471
472                         macb_invalidate_rx_buffer(macb);
473                         if (macb->wrapped) {
474                                 unsigned int headlen, taillen;
475
476                                 headlen = macb->rx_buffer_size *
477                                         (MACB_RX_RING_SIZE - macb->rx_tail);
478                                 taillen = length - headlen;
479                                 memcpy((void *)net_rx_packets[0],
480                                        buffer, headlen);
481                                 memcpy((void *)net_rx_packets[0] + headlen,
482                                        macb->rx_buffer, taillen);
483                                 *packetp = (void *)net_rx_packets[0];
484                         } else {
485                                 *packetp = buffer;
486                         }
487
488                         if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
489                                 if (!flag)
490                                         next_rx_tail = next_rx_tail / 2;
491                         }
492
493                         if (++next_rx_tail >= MACB_RX_RING_SIZE)
494                                 next_rx_tail = 0;
495                         macb->next_rx_tail = next_rx_tail;
496                         return length;
497                 } else {
498                         if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
499                                 if (!flag)
500                                         next_rx_tail = next_rx_tail / 2;
501                                 flag = false;
502                         }
503
504                         if (++next_rx_tail >= MACB_RX_RING_SIZE) {
505                                 macb->wrapped = true;
506                                 next_rx_tail = 0;
507                         }
508                 }
509                 barrier();
510         }
511 }
512
513 static void macb_phy_reset(struct macb_device *macb, const char *name)
514 {
515         int i;
516         u16 status, adv;
517
518         adv = ADVERTISE_CSMA | ADVERTISE_ALL;
519         macb_mdio_write(macb, macb->phy_addr, MII_ADVERTISE, adv);
520         printf("%s: Starting autonegotiation...\n", name);
521         macb_mdio_write(macb, macb->phy_addr, MII_BMCR, (BMCR_ANENABLE
522                                          | BMCR_ANRESTART));
523
524         for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
525                 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
526                 if (status & BMSR_ANEGCOMPLETE)
527                         break;
528                 udelay(100);
529         }
530
531         if (status & BMSR_ANEGCOMPLETE)
532                 printf("%s: Autonegotiation complete\n", name);
533         else
534                 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
535                        name, status);
536 }
537
538 static int macb_phy_find(struct macb_device *macb, const char *name)
539 {
540         int i;
541         u16 phy_id;
542
543         phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
544         if (phy_id != 0xffff) {
545                 printf("%s: PHY present at %d\n", name, macb->phy_addr);
546                 return 0;
547         }
548
549         /* Search for PHY... */
550         for (i = 0; i < 32; i++) {
551                 macb->phy_addr = i;
552                 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
553                 if (phy_id != 0xffff) {
554                         printf("%s: PHY present at %d\n", name, i);
555                         return 0;
556                 }
557         }
558
559         /* PHY isn't up to snuff */
560         printf("%s: PHY not found\n", name);
561
562         return -ENODEV;
563 }
564
565 /**
566  * macb_linkspd_cb - Linkspeed change callback function
567  * @dev/@regs:  MACB udevice (DM version) or
568  *              Base Register of MACB devices (non-DM version)
569  * @speed:      Linkspeed
570  * Returns 0 when operation success and negative errno number
571  * when operation failed.
572  */
573 #ifdef CONFIG_DM_ETH
574 static int macb_sifive_clk_init(struct udevice *dev, ulong rate)
575 {
576         fdt_addr_t addr;
577         void *gemgxl_regs;
578
579         addr = dev_read_addr_index(dev, 1);
580         if (addr == FDT_ADDR_T_NONE)
581                 return -ENODEV;
582
583         gemgxl_regs = (void __iomem *)addr;
584         if (!gemgxl_regs)
585                 return -ENODEV;
586
587         /*
588          * SiFive GEMGXL TX clock operation mode:
589          *
590          * 0 = GMII mode. Use 125 MHz gemgxlclk from PRCI in TX logic
591          *     and output clock on GMII output signal GTX_CLK
592          * 1 = MII mode. Use MII input signal TX_CLK in TX logic
593          */
594         writel(rate != 125000000, gemgxl_regs);
595         return 0;
596 }
597
598 static int macb_sama7g5_clk_init(struct udevice *dev, ulong rate)
599 {
600         struct clk clk;
601         int ret;
602
603         ret = clk_get_by_name(dev, "tx_clk", &clk);
604         if (ret)
605                 return ret;
606
607         /*
608          * This is for using GCK. Clock rate is addressed via assigned-clock
609          * property, so only clock enable is needed here. The switching to
610          * proper clock rate depending on link speed is managed by IP logic.
611          */
612         return clk_enable(&clk);
613 }
614
615 int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
616 {
617 #ifdef CONFIG_CLK
618         struct macb_device *macb = dev_get_priv(dev);
619         struct clk tx_clk;
620         ulong rate;
621         int ret;
622
623         switch (speed) {
624         case _10BASET:
625                 rate = 2500000;         /* 2.5 MHz */
626                 break;
627         case _100BASET:
628                 rate = 25000000;        /* 25 MHz */
629                 break;
630         case _1000BASET:
631                 rate = 125000000;       /* 125 MHz */
632                 break;
633         default:
634                 /* does not change anything */
635                 return 0;
636         }
637
638         if (macb->config->clk_init)
639                 return macb->config->clk_init(dev, rate);
640
641         /*
642          * "tx_clk" is an optional clock source for MACB.
643          * Ignore if it does not exist in DT.
644          */
645         ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
646         if (ret)
647                 return 0;
648
649         if (tx_clk.dev) {
650                 ret = clk_set_rate(&tx_clk, rate);
651                 if (ret < 0)
652                         return ret;
653         }
654 #endif
655
656         return 0;
657 }
658 #else
659 int __weak macb_linkspd_cb(void *regs, unsigned int speed)
660 {
661         return 0;
662 }
663 #endif
664
665 #ifdef CONFIG_DM_ETH
666 static int macb_phy_init(struct udevice *dev, const char *name)
667 #else
668 static int macb_phy_init(struct macb_device *macb, const char *name)
669 #endif
670 {
671 #ifdef CONFIG_DM_ETH
672         struct macb_device *macb = dev_get_priv(dev);
673 #endif
674         u32 ncfgr;
675         u16 phy_id, status, adv, lpa;
676         int media, speed, duplex;
677         int ret;
678         int i;
679
680         arch_get_mdio_control(name);
681         /* Auto-detect phy_addr */
682         ret = macb_phy_find(macb, name);
683         if (ret)
684                 return ret;
685
686         /* Check if the PHY is up to snuff... */
687         phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
688         if (phy_id == 0xffff) {
689                 printf("%s: No PHY present\n", name);
690                 return -ENODEV;
691         }
692
693 #ifdef CONFIG_PHYLIB
694 #ifdef CONFIG_DM_ETH
695         macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
696                              macb->phy_interface);
697 #else
698         /* need to consider other phy interface mode */
699         macb->phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
700                              PHY_INTERFACE_MODE_RGMII);
701 #endif
702         if (!macb->phydev) {
703                 printf("phy_connect failed\n");
704                 return -ENODEV;
705         }
706
707         phy_config(macb->phydev);
708 #endif
709
710         status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
711         if (!(status & BMSR_LSTATUS)) {
712                 /* Try to re-negotiate if we don't have link already. */
713                 macb_phy_reset(macb, name);
714
715                 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
716                         status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
717                         if (status & BMSR_LSTATUS) {
718                                 /*
719                                  * Delay a bit after the link is established,
720                                  * so that the next xfer does not fail
721                                  */
722                                 mdelay(10);
723                                 break;
724                         }
725                         udelay(100);
726                 }
727         }
728
729         if (!(status & BMSR_LSTATUS)) {
730                 printf("%s: link down (status: 0x%04x)\n",
731                        name, status);
732                 return -ENETDOWN;
733         }
734
735         /* First check for GMAC and that it is GiB capable */
736         if (gem_is_gigabit_capable(macb)) {
737                 lpa = macb_mdio_read(macb, macb->phy_addr, MII_STAT1000);
738
739                 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
740                                         LPA_1000XHALF)) {
741                         duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
742                                         1 : 0);
743
744                         printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
745                                name,
746                                duplex ? "full" : "half",
747                                lpa);
748
749                         ncfgr = macb_readl(macb, NCFGR);
750                         ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
751                         ncfgr |= GEM_BIT(GBE);
752
753                         if (duplex)
754                                 ncfgr |= MACB_BIT(FD);
755
756                         macb_writel(macb, NCFGR, ncfgr);
757
758 #ifdef CONFIG_DM_ETH
759                         ret = macb_linkspd_cb(dev, _1000BASET);
760 #else
761                         ret = macb_linkspd_cb(macb->regs, _1000BASET);
762 #endif
763                         if (ret)
764                                 return ret;
765
766                         return 0;
767                 }
768         }
769
770         /* fall back for EMAC checking */
771         adv = macb_mdio_read(macb, macb->phy_addr, MII_ADVERTISE);
772         lpa = macb_mdio_read(macb, macb->phy_addr, MII_LPA);
773         media = mii_nway_result(lpa & adv);
774         speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
775                  ? 1 : 0);
776         duplex = (media & ADVERTISE_FULL) ? 1 : 0;
777         printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
778                name,
779                speed ? "100" : "10",
780                duplex ? "full" : "half",
781                lpa);
782
783         ncfgr = macb_readl(macb, NCFGR);
784         ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
785         if (speed) {
786                 ncfgr |= MACB_BIT(SPD);
787 #ifdef CONFIG_DM_ETH
788                 ret = macb_linkspd_cb(dev, _100BASET);
789 #else
790                 ret = macb_linkspd_cb(macb->regs, _100BASET);
791 #endif
792         } else {
793 #ifdef CONFIG_DM_ETH
794                 ret = macb_linkspd_cb(dev, _10BASET);
795 #else
796                 ret = macb_linkspd_cb(macb->regs, _10BASET);
797 #endif
798         }
799
800         if (ret)
801                 return ret;
802
803         if (duplex)
804                 ncfgr |= MACB_BIT(FD);
805         macb_writel(macb, NCFGR, ncfgr);
806
807         return 0;
808 }
809
810 static int gmac_init_multi_queues(struct macb_device *macb)
811 {
812         int i, num_queues = 1;
813         u32 queue_mask;
814         unsigned long paddr;
815
816         /* bit 0 is never set but queue 0 always exists */
817         queue_mask = gem_readl(macb, DCFG6) & 0xff;
818         queue_mask |= 0x1;
819
820         for (i = 1; i < MACB_MAX_QUEUES; i++)
821                 if (queue_mask & (1 << i))
822                         num_queues++;
823
824         macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
825         macb->dummy_desc->addr = 0;
826         flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
827                         ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
828         paddr = macb->dummy_desc_dma;
829
830         for (i = 1; i < num_queues; i++) {
831                 gem_writel_queue_TBQP(macb, lower_32_bits(paddr), i - 1);
832                 gem_writel_queue_RBQP(macb, lower_32_bits(paddr), i - 1);
833                 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
834                         gem_writel_queue_TBQPH(macb, upper_32_bits(paddr),
835                                                i - 1);
836                         gem_writel_queue_RBQPH(macb, upper_32_bits(paddr),
837                                                i - 1);
838                 }
839         }
840         return 0;
841 }
842
843 static void gmac_configure_dma(struct macb_device *macb)
844 {
845         u32 buffer_size;
846         u32 dmacfg;
847
848         buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
849         dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
850         dmacfg |= GEM_BF(RXBS, buffer_size);
851
852         if (macb->config->dma_burst_length)
853                 dmacfg = GEM_BFINS(FBLDO,
854                                    macb->config->dma_burst_length, dmacfg);
855
856         dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
857         dmacfg &= ~GEM_BIT(ENDIA_PKT);
858
859         if (macb->is_big_endian)
860                 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
861         else
862                 dmacfg &= ~GEM_BIT(ENDIA_DESC);
863
864         dmacfg &= ~GEM_BIT(ADDR64);
865         if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
866                 dmacfg |= GEM_BIT(ADDR64);
867
868         gem_writel(macb, DMACFG, dmacfg);
869 }
870
871 #ifdef CONFIG_DM_ETH
872 static int _macb_init(struct udevice *dev, const char *name)
873 #else
874 static int _macb_init(struct macb_device *macb, const char *name)
875 #endif
876 {
877 #ifdef CONFIG_DM_ETH
878         struct macb_device *macb = dev_get_priv(dev);
879         unsigned int val = 0;
880 #endif
881         unsigned long paddr;
882         int ret;
883         int i;
884         int count;
885
886         /*
887          * macb_halt should have been called at some point before now,
888          * so we'll assume the controller is idle.
889          */
890
891         /* initialize DMA descriptors */
892         paddr = macb->rx_buffer_dma;
893         for (i = 0; i < MACB_RX_RING_SIZE; i++) {
894                 if (i == (MACB_RX_RING_SIZE - 1))
895                         paddr |= MACB_BIT(RX_WRAP);
896                 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
897                         count = i * 2;
898                 else
899                         count = i;
900                 macb->rx_ring[count].ctrl = 0;
901                 macb_set_addr(macb, &macb->rx_ring[count], paddr);
902                 paddr += macb->rx_buffer_size;
903         }
904         macb_flush_ring_desc(macb, RX);
905         macb_flush_rx_buffer(macb);
906
907         for (i = 0; i < MACB_TX_RING_SIZE; i++) {
908                 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
909                         count = i * 2;
910                 else
911                         count = i;
912                 macb_set_addr(macb, &macb->tx_ring[count], 0);
913                 if (i == (MACB_TX_RING_SIZE - 1))
914                         macb->tx_ring[count].ctrl = MACB_BIT(TX_USED) |
915                                 MACB_BIT(TX_WRAP);
916                 else
917                         macb->tx_ring[count].ctrl = MACB_BIT(TX_USED);
918         }
919         macb_flush_ring_desc(macb, TX);
920
921         macb->rx_tail = 0;
922         macb->tx_head = 0;
923         macb->tx_tail = 0;
924         macb->next_rx_tail = 0;
925
926 #ifdef CONFIG_MACB_ZYNQ
927         gem_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
928 #endif
929
930         macb_writel(macb, RBQP, lower_32_bits(macb->rx_ring_dma));
931         macb_writel(macb, TBQP, lower_32_bits(macb->tx_ring_dma));
932         if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
933                 macb_writel(macb, RBQPH, upper_32_bits(macb->rx_ring_dma));
934                 macb_writel(macb, TBQPH, upper_32_bits(macb->tx_ring_dma));
935         }
936
937         if (macb_is_gem(macb)) {
938                 /* Initialize DMA properties */
939                 gmac_configure_dma(macb);
940                 /* Check the multi queue and initialize the queue for tx */
941                 gmac_init_multi_queues(macb);
942
943                 /*
944                  * When the GMAC IP with GE feature, this bit is used to
945                  * select interface between RGMII and GMII.
946                  * When the GMAC IP without GE feature, this bit is used
947                  * to select interface between RMII and MII.
948                  */
949 #ifdef CONFIG_DM_ETH
950                 if (macb->phy_interface == PHY_INTERFACE_MODE_RGMII ||
951                     macb->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
952                     macb->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
953                     macb->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
954                         val = macb->config->usrio->rgmii;
955                 else if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
956                         val = macb->config->usrio->rmii;
957                 else if (macb->phy_interface == PHY_INTERFACE_MODE_MII)
958                         val = macb->config->usrio->mii;
959
960                 if (macb->config->caps & MACB_CAPS_USRIO_HAS_CLKEN)
961                         val |= macb->config->usrio->clken;
962
963                 gem_writel(macb, USRIO, val);
964
965                 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
966                         unsigned int ncfgr = macb_readl(macb, NCFGR);
967
968                         ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
969                         macb_writel(macb, NCFGR, ncfgr);
970                 }
971 #else
972 #if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
973                 gem_writel(macb, USRIO, macb->config->usrio->rgmii);
974 #else
975                 gem_writel(macb, USRIO, 0);
976 #endif
977 #endif
978         } else {
979         /* choose RMII or MII mode. This depends on the board */
980 #ifdef CONFIG_DM_ETH
981 #ifdef CONFIG_AT91FAMILY
982                 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
983                         macb_writel(macb, USRIO,
984                                     macb->config->usrio->rmii |
985                                     macb->config->usrio->clken);
986                 } else {
987                         macb_writel(macb, USRIO, macb->config->usrio->clken);
988                 }
989 #else
990                 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
991                         macb_writel(macb, USRIO, 0);
992                 else
993                         macb_writel(macb, USRIO, macb->config->usrio->mii);
994 #endif
995 #else
996 #ifdef CONFIG_RMII
997 #ifdef CONFIG_AT91FAMILY
998         macb_writel(macb, USRIO, macb->config->usrio->rmii |
999                     macb->config->usrio->clken);
1000 #else
1001         macb_writel(macb, USRIO, 0);
1002 #endif
1003 #else
1004 #ifdef CONFIG_AT91FAMILY
1005         macb_writel(macb, USRIO, macb->config->usrio->clken);
1006 #else
1007         macb_writel(macb, USRIO, macb->config->usrio->mii);
1008 #endif
1009 #endif /* CONFIG_RMII */
1010 #endif
1011         }
1012
1013 #ifdef CONFIG_DM_ETH
1014         ret = macb_phy_init(dev, name);
1015 #else
1016         ret = macb_phy_init(macb, name);
1017 #endif
1018         if (ret)
1019                 return ret;
1020
1021         /* Enable TX and RX */
1022         macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
1023
1024         return 0;
1025 }
1026
1027 static void _macb_halt(struct macb_device *macb)
1028 {
1029         u32 ncr, tsr;
1030
1031         /* Halt the controller and wait for any ongoing transmission to end. */
1032         ncr = macb_readl(macb, NCR);
1033         ncr |= MACB_BIT(THALT);
1034         macb_writel(macb, NCR, ncr);
1035
1036         do {
1037                 tsr = macb_readl(macb, TSR);
1038         } while (tsr & MACB_BIT(TGO));
1039
1040         /* Disable TX and RX, and clear statistics */
1041         macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
1042 }
1043
1044 static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
1045 {
1046         u32 hwaddr_bottom;
1047         u16 hwaddr_top;
1048
1049         /* set hardware address */
1050         hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
1051                         enetaddr[2] << 16 | enetaddr[3] << 24;
1052         macb_writel(macb, SA1B, hwaddr_bottom);
1053         hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
1054         macb_writel(macb, SA1T, hwaddr_top);
1055         return 0;
1056 }
1057
1058 static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
1059 {
1060         u32 config;
1061 #if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
1062         unsigned long macb_hz = macb->pclk_rate;
1063 #else
1064         unsigned long macb_hz = get_macb_pclk_rate(id);
1065 #endif
1066
1067         if (macb_hz < 20000000)
1068                 config = MACB_BF(CLK, MACB_CLK_DIV8);
1069         else if (macb_hz < 40000000)
1070                 config = MACB_BF(CLK, MACB_CLK_DIV16);
1071         else if (macb_hz < 80000000)
1072                 config = MACB_BF(CLK, MACB_CLK_DIV32);
1073         else
1074                 config = MACB_BF(CLK, MACB_CLK_DIV64);
1075
1076         return config;
1077 }
1078
1079 static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
1080 {
1081         u32 config;
1082
1083 #if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
1084         unsigned long macb_hz = macb->pclk_rate;
1085 #else
1086         unsigned long macb_hz = get_macb_pclk_rate(id);
1087 #endif
1088
1089         if (macb_hz < 20000000)
1090                 config = GEM_BF(CLK, GEM_CLK_DIV8);
1091         else if (macb_hz < 40000000)
1092                 config = GEM_BF(CLK, GEM_CLK_DIV16);
1093         else if (macb_hz < 80000000)
1094                 config = GEM_BF(CLK, GEM_CLK_DIV32);
1095         else if (macb_hz < 120000000)
1096                 config = GEM_BF(CLK, GEM_CLK_DIV48);
1097         else if (macb_hz < 160000000)
1098                 config = GEM_BF(CLK, GEM_CLK_DIV64);
1099         else if (macb_hz < 240000000)
1100                 config = GEM_BF(CLK, GEM_CLK_DIV96);
1101         else if (macb_hz < 320000000)
1102                 config = GEM_BF(CLK, GEM_CLK_DIV128);
1103         else
1104                 config = GEM_BF(CLK, GEM_CLK_DIV224);
1105
1106         return config;
1107 }
1108
1109 /*
1110  * Get the DMA bus width field of the network configuration register that we
1111  * should program. We find the width from decoding the design configuration
1112  * register to find the maximum supported data bus width.
1113  */
1114 static u32 macb_dbw(struct macb_device *macb)
1115 {
1116         switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
1117         case 4:
1118                 return GEM_BF(DBW, GEM_DBW128);
1119         case 2:
1120                 return GEM_BF(DBW, GEM_DBW64);
1121         case 1:
1122         default:
1123                 return GEM_BF(DBW, GEM_DBW32);
1124         }
1125 }
1126
1127 static void _macb_eth_initialize(struct macb_device *macb)
1128 {
1129         int id = 0;     /* This is not used by functions we call */
1130         u32 ncfgr;
1131
1132         if (macb_is_gem(macb))
1133                 macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
1134         else
1135                 macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1136
1137         /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
1138         macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
1139                                              MACB_RX_RING_SIZE,
1140                                              &macb->rx_buffer_dma);
1141         macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
1142                                            &macb->rx_ring_dma);
1143         macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
1144                                            &macb->tx_ring_dma);
1145         macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
1146                                            &macb->dummy_desc_dma);
1147
1148         /*
1149          * Do some basic initialization so that we at least can talk
1150          * to the PHY
1151          */
1152         if (macb_is_gem(macb)) {
1153                 ncfgr = gem_mdc_clk_div(id, macb);
1154                 ncfgr |= macb_dbw(macb);
1155         } else {
1156                 ncfgr = macb_mdc_clk_div(id, macb);
1157         }
1158
1159         macb_writel(macb, NCFGR, ncfgr);
1160 }
1161
1162 #ifndef CONFIG_DM_ETH
1163 static int macb_send(struct eth_device *netdev, void *packet, int length)
1164 {
1165         struct macb_device *macb = to_macb(netdev);
1166
1167         return _macb_send(macb, netdev->name, packet, length);
1168 }
1169
1170 static int macb_recv(struct eth_device *netdev)
1171 {
1172         struct macb_device *macb = to_macb(netdev);
1173         uchar *packet;
1174         int length;
1175
1176         macb->wrapped = false;
1177         for (;;) {
1178                 macb->next_rx_tail = macb->rx_tail;
1179                 length = _macb_recv(macb, &packet);
1180                 if (length >= 0) {
1181                         net_process_received_packet(packet, length);
1182                         reclaim_rx_buffers(macb, macb->next_rx_tail);
1183                 } else {
1184                         return length;
1185                 }
1186         }
1187 }
1188
1189 static int macb_init(struct eth_device *netdev, struct bd_info *bd)
1190 {
1191         struct macb_device *macb = to_macb(netdev);
1192
1193         return _macb_init(macb, netdev->name);
1194 }
1195
1196 static void macb_halt(struct eth_device *netdev)
1197 {
1198         struct macb_device *macb = to_macb(netdev);
1199
1200         return _macb_halt(macb);
1201 }
1202
1203 static int macb_write_hwaddr(struct eth_device *netdev)
1204 {
1205         struct macb_device *macb = to_macb(netdev);
1206
1207         return _macb_write_hwaddr(macb, netdev->enetaddr);
1208 }
1209
1210 int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
1211 {
1212         struct macb_device *macb;
1213         struct eth_device *netdev;
1214
1215         macb = malloc(sizeof(struct macb_device));
1216         if (!macb) {
1217                 printf("Error: Failed to allocate memory for MACB%d\n", id);
1218                 return -1;
1219         }
1220         memset(macb, 0, sizeof(struct macb_device));
1221
1222         netdev = &macb->netdev;
1223
1224         macb->regs = regs;
1225         macb->phy_addr = phy_addr;
1226
1227         if (macb_is_gem(macb))
1228                 sprintf(netdev->name, "gmac%d", id);
1229         else
1230                 sprintf(netdev->name, "macb%d", id);
1231
1232         netdev->init = macb_init;
1233         netdev->halt = macb_halt;
1234         netdev->send = macb_send;
1235         netdev->recv = macb_recv;
1236         netdev->write_hwaddr = macb_write_hwaddr;
1237
1238         _macb_eth_initialize(macb);
1239
1240         eth_register(netdev);
1241
1242 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
1243         int retval;
1244         struct mii_dev *mdiodev = mdio_alloc();
1245         if (!mdiodev)
1246                 return -ENOMEM;
1247         strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
1248         mdiodev->read = macb_miiphy_read;
1249         mdiodev->write = macb_miiphy_write;
1250
1251         retval = mdio_register(mdiodev);
1252         if (retval < 0)
1253                 return retval;
1254         macb->bus = miiphy_get_dev_by_name(netdev->name);
1255 #endif
1256         return 0;
1257 }
1258 #endif /* !CONFIG_DM_ETH */
1259
1260 #ifdef CONFIG_DM_ETH
1261
1262 static int macb_start(struct udevice *dev)
1263 {
1264         return _macb_init(dev, dev->name);
1265 }
1266
1267 static int macb_send(struct udevice *dev, void *packet, int length)
1268 {
1269         struct macb_device *macb = dev_get_priv(dev);
1270
1271         return _macb_send(macb, dev->name, packet, length);
1272 }
1273
1274 static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1275 {
1276         struct macb_device *macb = dev_get_priv(dev);
1277
1278         macb->next_rx_tail = macb->rx_tail;
1279         macb->wrapped = false;
1280
1281         return _macb_recv(macb, packetp);
1282 }
1283
1284 static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1285 {
1286         struct macb_device *macb = dev_get_priv(dev);
1287
1288         reclaim_rx_buffers(macb, macb->next_rx_tail);
1289
1290         return 0;
1291 }
1292
1293 static void macb_stop(struct udevice *dev)
1294 {
1295         struct macb_device *macb = dev_get_priv(dev);
1296
1297         _macb_halt(macb);
1298 }
1299
1300 static int macb_write_hwaddr(struct udevice *dev)
1301 {
1302         struct eth_pdata *plat = dev_get_plat(dev);
1303         struct macb_device *macb = dev_get_priv(dev);
1304
1305         return _macb_write_hwaddr(macb, plat->enetaddr);
1306 }
1307
1308 static const struct eth_ops macb_eth_ops = {
1309         .start  = macb_start,
1310         .send   = macb_send,
1311         .recv   = macb_recv,
1312         .stop   = macb_stop,
1313         .free_pkt       = macb_free_pkt,
1314         .write_hwaddr   = macb_write_hwaddr,
1315 };
1316
1317 #ifdef CONFIG_CLK
1318 static int macb_enable_clk(struct udevice *dev)
1319 {
1320         struct macb_device *macb = dev_get_priv(dev);
1321         struct clk clk;
1322         ulong clk_rate;
1323         int ret;
1324
1325         ret = clk_get_by_index(dev, 0, &clk);
1326         if (ret)
1327                 return -EINVAL;
1328
1329         /*
1330          * If clock driver didn't support enable or disable then
1331          * we get -ENOSYS from clk_enable(). To handle this, we
1332          * don't fail for ret == -ENOSYS.
1333          */
1334         ret = clk_enable(&clk);
1335         if (ret && ret != -ENOSYS)
1336                 return ret;
1337
1338         clk_rate = clk_get_rate(&clk);
1339         if (!clk_rate)
1340                 return -EINVAL;
1341
1342         macb->pclk_rate = clk_rate;
1343
1344         return 0;
1345 }
1346 #endif
1347
1348 static const struct macb_usrio_cfg macb_default_usrio = {
1349         .mii = MACB_BIT(MII),
1350         .rmii = MACB_BIT(RMII),
1351         .rgmii = GEM_BIT(RGMII),
1352         .clken = MACB_BIT(CLKEN),
1353 };
1354
1355 static const struct macb_config default_gem_config = {
1356         .dma_burst_length = 16,
1357         .hw_dma_cap = HW_DMA_CAP_32B,
1358         .clk_init = NULL,
1359         .usrio = &macb_default_usrio,
1360 };
1361
1362 static int macb_eth_probe(struct udevice *dev)
1363 {
1364         struct eth_pdata *pdata = dev_get_plat(dev);
1365         struct macb_device *macb = dev_get_priv(dev);
1366         struct ofnode_phandle_args phandle_args;
1367         const char *phy_mode;
1368         int ret;
1369
1370         phy_mode = dev_read_prop(dev, "phy-mode", NULL);
1371
1372         if (phy_mode)
1373                 macb->phy_interface = phy_get_interface_by_name(phy_mode);
1374         if (macb->phy_interface == -1) {
1375                 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1376                 return -EINVAL;
1377         }
1378
1379         /* Read phyaddr from DT */
1380         if (!dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1381                                         &phandle_args))
1382                 macb->phy_addr = ofnode_read_u32_default(phandle_args.node,
1383                                                          "reg", -1);
1384
1385         macb->regs = (void *)pdata->iobase;
1386
1387         macb->is_big_endian = (cpu_to_be32(0x12345678) == 0x12345678);
1388
1389         macb->config = (struct macb_config *)dev_get_driver_data(dev);
1390         if (!macb->config)
1391                 macb->config = &default_gem_config;
1392
1393 #ifdef CONFIG_CLK
1394         ret = macb_enable_clk(dev);
1395         if (ret)
1396                 return ret;
1397 #endif
1398
1399         _macb_eth_initialize(macb);
1400
1401 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
1402         macb->bus = mdio_alloc();
1403         if (!macb->bus)
1404                 return -ENOMEM;
1405         strncpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
1406         macb->bus->read = macb_miiphy_read;
1407         macb->bus->write = macb_miiphy_write;
1408
1409         ret = mdio_register(macb->bus);
1410         if (ret < 0)
1411                 return ret;
1412         macb->bus = miiphy_get_dev_by_name(dev->name);
1413 #endif
1414
1415         return 0;
1416 }
1417
1418 static int macb_eth_remove(struct udevice *dev)
1419 {
1420         struct macb_device *macb = dev_get_priv(dev);
1421
1422 #ifdef CONFIG_PHYLIB
1423         free(macb->phydev);
1424 #endif
1425         mdio_unregister(macb->bus);
1426         mdio_free(macb->bus);
1427
1428         return 0;
1429 }
1430
1431 /**
1432  * macb_late_eth_of_to_plat
1433  * @dev:        udevice struct
1434  * Returns 0 when operation success and negative errno number
1435  * when operation failed.
1436  */
1437 int __weak macb_late_eth_of_to_plat(struct udevice *dev)
1438 {
1439         return 0;
1440 }
1441
1442 static int macb_eth_of_to_plat(struct udevice *dev)
1443 {
1444         struct eth_pdata *pdata = dev_get_plat(dev);
1445
1446         pdata->iobase = (phys_addr_t)dev_remap_addr(dev);
1447         if (!pdata->iobase)
1448                 return -EINVAL;
1449
1450         return macb_late_eth_of_to_plat(dev);
1451 }
1452
1453 static const struct macb_usrio_cfg sama7g5_usrio = {
1454         .mii = 0,
1455         .rmii = 1,
1456         .rgmii = 2,
1457         .clken = BIT(2),
1458 };
1459
1460 static const struct macb_config microchip_config = {
1461         .dma_burst_length = 16,
1462         .hw_dma_cap = HW_DMA_CAP_64B,
1463         .clk_init = NULL,
1464         .usrio = &macb_default_usrio,
1465 };
1466
1467 static const struct macb_config sama5d4_config = {
1468         .dma_burst_length = 4,
1469         .hw_dma_cap = HW_DMA_CAP_32B,
1470         .clk_init = NULL,
1471         .usrio = &macb_default_usrio,
1472 };
1473
1474 static const struct macb_config sifive_config = {
1475         .dma_burst_length = 16,
1476         .hw_dma_cap = HW_DMA_CAP_32B,
1477         .clk_init = macb_sifive_clk_init,
1478         .usrio = &macb_default_usrio,
1479 };
1480
1481 static const struct macb_config sama7g5_gmac_config = {
1482         .dma_burst_length = 16,
1483         .hw_dma_cap = HW_DMA_CAP_32B,
1484         .clk_init = macb_sama7g5_clk_init,
1485         .usrio = &sama7g5_usrio,
1486 };
1487
1488 static const struct macb_config sama7g5_emac_config = {
1489         .caps = MACB_CAPS_USRIO_HAS_CLKEN,
1490         .dma_burst_length = 16,
1491         .hw_dma_cap = HW_DMA_CAP_32B,
1492         .usrio = &sama7g5_usrio,
1493 };
1494
1495 static const struct udevice_id macb_eth_ids[] = {
1496         { .compatible = "cdns,macb" },
1497         { .compatible = "cdns,at91sam9260-macb" },
1498         { .compatible = "cdns,sam9x60-macb" },
1499         { .compatible = "cdns,sama7g5-gem",
1500           .data = (ulong)&sama7g5_gmac_config },
1501         { .compatible = "cdns,sama7g5-emac",
1502           .data = (ulong)&sama7g5_emac_config },
1503         { .compatible = "atmel,sama5d2-gem" },
1504         { .compatible = "atmel,sama5d3-gem" },
1505         { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
1506         { .compatible = "cdns,zynq-gem" },
1507         { .compatible = "sifive,fu540-c000-gem",
1508           .data = (ulong)&sifive_config },
1509         { .compatible = "microchip,mpfs-mss-gem",
1510           .data = (ulong)&microchip_config },
1511         { }
1512 };
1513
1514 U_BOOT_DRIVER(eth_macb) = {
1515         .name   = "eth_macb",
1516         .id     = UCLASS_ETH,
1517         .of_match = macb_eth_ids,
1518         .of_to_plat = macb_eth_of_to_plat,
1519         .probe  = macb_eth_probe,
1520         .remove = macb_eth_remove,
1521         .ops    = &macb_eth_ops,
1522         .priv_auto      = sizeof(struct macb_device),
1523         .plat_auto      = sizeof(struct eth_pdata),
1524 };
1525 #endif
1526
1527 #endif
This page took 0.114991 seconds and 4 git commands to generate.