blob: c99cf663a4162d3fdfa50f1b6ec13a30e1b2aedc [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01002/*
3 * Copyright (C) 2005-2006 Atmel Corporation
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01004 */
5#include <common.h>
Wenyou Yang577aa3b2016-11-02 10:06:56 +08006#include <clk.h>
Simon Glassf1dcc192016-05-05 07:28:11 -06007#include <dm.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01008
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01009/*
10 * The u-boot networking stack is a little weird. It seems like the
11 * networking core allocates receive buffers up front without any
12 * regard to the hardware that's supposed to actually receive those
13 * packets.
14 *
15 * The MACB receives packets into 128-byte receive buffers, so the
16 * buffers allocated by the core isn't very practical to use. We'll
17 * allocate our own, but we need one such buffer in case a packet
18 * wraps around the DMA ring so that we have to copy it.
19 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020020 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010021 * configuration header. This way, the core allocates one RX buffer
22 * and one TX buffer, each of which can hold a ethernet packet of
23 * maximum size.
24 *
25 * For some reason, the networking core unconditionally specifies a
26 * 32-byte packet "alignment" (which really should be called
27 * "padding"). MACB shouldn't need that, but we'll refrain from any
28 * core modifications here...
29 */
30
31#include <net.h>
Simon Glassf1dcc192016-05-05 07:28:11 -060032#ifndef CONFIG_DM_ETH
Ben Warren89973f82008-08-31 22:22:04 -070033#include <netdev.h>
Simon Glassf1dcc192016-05-05 07:28:11 -060034#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010035#include <malloc.h>
Semih Hazar0f751d62009-12-17 15:07:15 +020036#include <miiphy.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010037
38#include <linux/mii.h>
39#include <asm/io.h>
40#include <asm/dma-mapping.h>
41#include <asm/arch/clk.h>
Masahiro Yamada5d97dff2016-09-21 11:28:57 +090042#include <linux/errno.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010043
44#include "macb.h"
45
Wenyou Yanga212b662016-05-17 13:11:35 +080046DECLARE_GLOBAL_DATA_PTR;
47
Ramon Friedc6d07bf2019-07-14 18:25:14 +030048/*
49 * These buffer sizes must be power of 2 and divisible
50 * by RX_BUFFER_MULTIPLE
51 */
52#define MACB_RX_BUFFER_SIZE 128
53#define GEM_RX_BUFFER_SIZE 2048
Ramon Fried9c295802019-07-16 22:04:36 +030054#define RX_BUFFER_MULTIPLE 64
Ramon Friedc6d07bf2019-07-14 18:25:14 +030055
56#define MACB_RX_RING_SIZE 32
Andreas Bießmannceef9832014-05-26 22:55:18 +020057#define MACB_TX_RING_SIZE 16
Ramon Friedc6d07bf2019-07-14 18:25:14 +030058
Andreas Bießmannceef9832014-05-26 22:55:18 +020059#define MACB_TX_TIMEOUT 1000
60#define MACB_AUTONEG_TIMEOUT 5000000
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010061
Wilson Lee4bf56912017-08-22 20:25:07 -070062#ifdef CONFIG_MACB_ZYNQ
63/* INCR4 AHB bursts */
64#define MACB_ZYNQ_GEM_DMACR_BLENGTH 0x00000004
65/* Use full configured addressable space (8 Kb) */
66#define MACB_ZYNQ_GEM_DMACR_RXSIZE 0x00000300
67/* Use full configured addressable space (4 Kb) */
68#define MACB_ZYNQ_GEM_DMACR_TXSIZE 0x00000400
69/* Set RXBUF with use of 128 byte */
70#define MACB_ZYNQ_GEM_DMACR_RXBUF 0x00020000
71#define MACB_ZYNQ_GEM_DMACR_INIT \
72 (MACB_ZYNQ_GEM_DMACR_BLENGTH | \
73 MACB_ZYNQ_GEM_DMACR_RXSIZE | \
74 MACB_ZYNQ_GEM_DMACR_TXSIZE | \
75 MACB_ZYNQ_GEM_DMACR_RXBUF)
76#endif
77
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010078struct macb_dma_desc {
79 u32 addr;
80 u32 ctrl;
81};
82
Wu, Josh5ae0e382014-05-27 16:31:05 +080083#define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
84#define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
85#define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
Wu, Joshade4ea42015-06-03 16:45:44 +080086#define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
Wu, Josh5ae0e382014-05-27 16:31:05 +080087
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010088#define RXBUF_FRMLEN_MASK 0x00000fff
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010089#define TXBUF_FRMLEN_MASK 0x000007ff
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010090
91struct macb_device {
92 void *regs;
Anup Pateld0a04db2019-07-24 04:09:32 +000093
Anup Pateleff0e0c2019-07-24 04:09:37 +000094 bool is_big_endian;
95
Anup Pateld0a04db2019-07-24 04:09:32 +000096 const struct macb_config *config;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010097
98 unsigned int rx_tail;
99 unsigned int tx_head;
100 unsigned int tx_tail;
Simon Glassd5555b72016-05-05 07:28:09 -0600101 unsigned int next_rx_tail;
102 bool wrapped;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100103
104 void *rx_buffer;
105 void *tx_buffer;
106 struct macb_dma_desc *rx_ring;
107 struct macb_dma_desc *tx_ring;
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300108 size_t rx_buffer_size;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100109
110 unsigned long rx_buffer_dma;
111 unsigned long rx_ring_dma;
112 unsigned long tx_ring_dma;
113
Wu, Joshade4ea42015-06-03 16:45:44 +0800114 struct macb_dma_desc *dummy_desc;
115 unsigned long dummy_desc_dma;
116
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100117 const struct device *dev;
Simon Glassf1dcc192016-05-05 07:28:11 -0600118#ifndef CONFIG_DM_ETH
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100119 struct eth_device netdev;
Simon Glassf1dcc192016-05-05 07:28:11 -0600120#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100121 unsigned short phy_addr;
Bo Shenb1a00062013-04-24 15:59:27 +0800122 struct mii_dev *bus;
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800123#ifdef CONFIG_PHYLIB
124 struct phy_device *phydev;
125#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800126
127#ifdef CONFIG_DM_ETH
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800128#ifdef CONFIG_CLK
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800129 unsigned long pclk_rate;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800130#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800131 phy_interface_t phy_interface;
132#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100133};
Ramon Frieded3c64f2019-07-16 22:04:35 +0300134
135struct macb_config {
136 unsigned int dma_burst_length;
Anup Pateld0a04db2019-07-24 04:09:32 +0000137
138 int (*clk_init)(struct udevice *dev, ulong rate);
Ramon Frieded3c64f2019-07-16 22:04:35 +0300139};
140
Simon Glassf1dcc192016-05-05 07:28:11 -0600141#ifndef CONFIG_DM_ETH
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100142#define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
Simon Glassf1dcc192016-05-05 07:28:11 -0600143#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100144
Bo Shend256be22013-04-24 15:59:28 +0800145static int macb_is_gem(struct macb_device *macb)
146{
Atish Patrafbcaa262019-02-25 08:14:42 +0000147 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
Bo Shend256be22013-04-24 15:59:28 +0800148}
149
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100150#ifndef cpu_is_sama5d2
151#define cpu_is_sama5d2() 0
152#endif
153
154#ifndef cpu_is_sama5d4
155#define cpu_is_sama5d4() 0
156#endif
157
158static int gem_is_gigabit_capable(struct macb_device *macb)
159{
160 /*
Robert P. J. Day1cc0a9f2016-05-04 04:47:31 -0400161 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100162 * configured to support only 10/100.
163 */
164 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
165}
166
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100167static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
168{
169 unsigned long netctl;
170 unsigned long netstat;
171 unsigned long frame;
172
173 netctl = macb_readl(macb, NCR);
174 netctl |= MACB_BIT(MPE);
175 macb_writel(macb, NCR, netctl);
176
177 frame = (MACB_BF(SOF, 1)
178 | MACB_BF(RW, 1)
179 | MACB_BF(PHYA, macb->phy_addr)
180 | MACB_BF(REGA, reg)
181 | MACB_BF(CODE, 2)
182 | MACB_BF(DATA, value));
183 macb_writel(macb, MAN, frame);
184
185 do {
186 netstat = macb_readl(macb, NSR);
187 } while (!(netstat & MACB_BIT(IDLE)));
188
189 netctl = macb_readl(macb, NCR);
190 netctl &= ~MACB_BIT(MPE);
191 macb_writel(macb, NCR, netctl);
192}
193
194static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
195{
196 unsigned long netctl;
197 unsigned long netstat;
198 unsigned long frame;
199
200 netctl = macb_readl(macb, NCR);
201 netctl |= MACB_BIT(MPE);
202 macb_writel(macb, NCR, netctl);
203
204 frame = (MACB_BF(SOF, 1)
205 | MACB_BF(RW, 2)
206 | MACB_BF(PHYA, macb->phy_addr)
207 | MACB_BF(REGA, reg)
208 | MACB_BF(CODE, 2));
209 macb_writel(macb, MAN, frame);
210
211 do {
212 netstat = macb_readl(macb, NSR);
213 } while (!(netstat & MACB_BIT(IDLE)));
214
215 frame = macb_readl(macb, MAN);
216
217 netctl = macb_readl(macb, NCR);
218 netctl &= ~MACB_BIT(MPE);
219 macb_writel(macb, NCR, netctl);
220
221 return MACB_BFEXT(DATA, frame);
222}
223
Joe Hershberger1b8c18b2013-06-24 19:06:38 -0500224void __weak arch_get_mdio_control(const char *name)
Shiraz Hashim416ce622012-12-13 17:22:52 +0530225{
226 return;
227}
228
Bo Shenb1a00062013-04-24 15:59:27 +0800229#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Semih Hazar0f751d62009-12-17 15:07:15 +0200230
Joe Hershberger5a49f172016-08-08 11:28:38 -0500231int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
Semih Hazar0f751d62009-12-17 15:07:15 +0200232{
Joe Hershberger5a49f172016-08-08 11:28:38 -0500233 u16 value = 0;
Simon Glassf1dcc192016-05-05 07:28:11 -0600234#ifdef CONFIG_DM_ETH
Joe Hershberger5a49f172016-08-08 11:28:38 -0500235 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600236 struct macb_device *macb = dev_get_priv(dev);
237#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500238 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200239 struct macb_device *macb = to_macb(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -0600240#endif
Semih Hazar0f751d62009-12-17 15:07:15 +0200241
Andreas Bießmannceef9832014-05-26 22:55:18 +0200242 if (macb->phy_addr != phy_adr)
Semih Hazar0f751d62009-12-17 15:07:15 +0200243 return -1;
244
Joe Hershberger5a49f172016-08-08 11:28:38 -0500245 arch_get_mdio_control(bus->name);
246 value = macb_mdio_read(macb, reg);
Semih Hazar0f751d62009-12-17 15:07:15 +0200247
Joe Hershberger5a49f172016-08-08 11:28:38 -0500248 return value;
Semih Hazar0f751d62009-12-17 15:07:15 +0200249}
250
Joe Hershberger5a49f172016-08-08 11:28:38 -0500251int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
252 u16 value)
Semih Hazar0f751d62009-12-17 15:07:15 +0200253{
Simon Glassf1dcc192016-05-05 07:28:11 -0600254#ifdef CONFIG_DM_ETH
Joe Hershberger5a49f172016-08-08 11:28:38 -0500255 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600256 struct macb_device *macb = dev_get_priv(dev);
257#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500258 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200259 struct macb_device *macb = to_macb(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -0600260#endif
Semih Hazar0f751d62009-12-17 15:07:15 +0200261
Andreas Bießmannceef9832014-05-26 22:55:18 +0200262 if (macb->phy_addr != phy_adr)
Semih Hazar0f751d62009-12-17 15:07:15 +0200263 return -1;
264
Joe Hershberger5a49f172016-08-08 11:28:38 -0500265 arch_get_mdio_control(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200266 macb_mdio_write(macb, reg, value);
267
268 return 0;
269}
270#endif
271
Wu, Josh5ae0e382014-05-27 16:31:05 +0800272#define RX 1
273#define TX 0
274static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
275{
276 if (rx)
Heiko Schocher592a7492016-08-29 07:46:11 +0200277 invalidate_dcache_range(macb->rx_ring_dma,
278 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
279 PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800280 else
Heiko Schocher592a7492016-08-29 07:46:11 +0200281 invalidate_dcache_range(macb->tx_ring_dma,
282 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
283 PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800284}
285
286static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
287{
288 if (rx)
289 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200290 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800291 else
292 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200293 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800294}
295
296static inline void macb_flush_rx_buffer(struct macb_device *macb)
297{
298 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200299 ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800300}
301
302static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
303{
304 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200305 ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800306}
Semih Hazar0f751d62009-12-17 15:07:15 +0200307
Jon Loeliger07d38a12007-07-09 17:30:01 -0500308#if defined(CONFIG_CMD_NET)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100309
Simon Glassd5555b72016-05-05 07:28:09 -0600310static int _macb_send(struct macb_device *macb, const char *name, void *packet,
311 int length)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100312{
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100313 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;
Ramon Fried0a2827e2019-07-16 22:04:33 +0300320 ctrl |= MACB_BIT(TX_LAST);
Andreas Bießmannceef9832014-05-26 22:55:18 +0200321 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300322 ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100323 macb->tx_head = 0;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200324 } else {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100325 macb->tx_head++;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200326 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100327
328 macb->tx_ring[tx_head].ctrl = ctrl;
329 macb->tx_ring[tx_head].addr = paddr;
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200330 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800331 macb_flush_ring_desc(macb, TX);
332 /* Do we need check paddr and length is dcache line aligned? */
Simon Glassf589f8c2016-05-05 07:28:10 -0600333 flush_dcache_range(paddr, paddr + ALIGN(length, ARCH_DMA_MINALIGN));
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100334 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
335
336 /*
337 * I guess this is necessary because the networking core may
338 * re-use the transmit buffer as soon as we return...
339 */
Andreas Bießmannceef9832014-05-26 22:55:18 +0200340 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200341 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800342 macb_invalidate_ring_desc(macb, TX);
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200343 ctrl = macb->tx_ring[tx_head].ctrl;
Ramon Fried0a2827e2019-07-16 22:04:33 +0300344 if (ctrl & MACB_BIT(TX_USED))
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100345 break;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100346 udelay(1);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100347 }
348
349 dma_unmap_single(packet, length, paddr);
350
Andreas Bießmannceef9832014-05-26 22:55:18 +0200351 if (i <= MACB_TX_TIMEOUT) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300352 if (ctrl & MACB_BIT(TX_UNDERRUN))
Simon Glassd5555b72016-05-05 07:28:09 -0600353 printf("%s: TX underrun\n", name);
Ramon Fried0a2827e2019-07-16 22:04:33 +0300354 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
Simon Glassd5555b72016-05-05 07:28:09 -0600355 printf("%s: TX buffers exhausted in mid frame\n", name);
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200356 } else {
Simon Glassd5555b72016-05-05 07:28:09 -0600357 printf("%s: TX timeout\n", name);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100358 }
359
360 /* No one cares anyway */
361 return 0;
362}
363
364static void reclaim_rx_buffers(struct macb_device *macb,
365 unsigned int new_tail)
366{
367 unsigned int i;
368
369 i = macb->rx_tail;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800370
371 macb_invalidate_ring_desc(macb, RX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100372 while (i > new_tail) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300373 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100374 i++;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200375 if (i > MACB_RX_RING_SIZE)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100376 i = 0;
377 }
378
379 while (i < new_tail) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300380 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100381 i++;
382 }
383
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200384 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800385 macb_flush_ring_desc(macb, RX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100386 macb->rx_tail = new_tail;
387}
388
Simon Glassd5555b72016-05-05 07:28:09 -0600389static int _macb_recv(struct macb_device *macb, uchar **packetp)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100390{
Simon Glassd5555b72016-05-05 07:28:09 -0600391 unsigned int next_rx_tail = macb->next_rx_tail;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100392 void *buffer;
393 int length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100394 u32 status;
395
Simon Glassd5555b72016-05-05 07:28:09 -0600396 macb->wrapped = false;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100397 for (;;) {
Wu, Josh5ae0e382014-05-27 16:31:05 +0800398 macb_invalidate_ring_desc(macb, RX);
399
Ramon Fried0a2827e2019-07-16 22:04:33 +0300400 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
Simon Glassd5555b72016-05-05 07:28:09 -0600401 return -EAGAIN;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100402
Simon Glassd5555b72016-05-05 07:28:09 -0600403 status = macb->rx_ring[next_rx_tail].ctrl;
Ramon Fried0a2827e2019-07-16 22:04:33 +0300404 if (status & MACB_BIT(RX_SOF)) {
Simon Glassd5555b72016-05-05 07:28:09 -0600405 if (next_rx_tail != macb->rx_tail)
406 reclaim_rx_buffers(macb, next_rx_tail);
407 macb->wrapped = false;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100408 }
409
Ramon Fried0a2827e2019-07-16 22:04:33 +0300410 if (status & MACB_BIT(RX_EOF)) {
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300411 buffer = macb->rx_buffer +
412 macb->rx_buffer_size * macb->rx_tail;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100413 length = status & RXBUF_FRMLEN_MASK;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800414
415 macb_invalidate_rx_buffer(macb);
Simon Glassd5555b72016-05-05 07:28:09 -0600416 if (macb->wrapped) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100417 unsigned int headlen, taillen;
418
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300419 headlen = macb->rx_buffer_size *
420 (MACB_RX_RING_SIZE - macb->rx_tail);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100421 taillen = length - headlen;
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500422 memcpy((void *)net_rx_packets[0],
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100423 buffer, headlen);
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500424 memcpy((void *)net_rx_packets[0] + headlen,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100425 macb->rx_buffer, taillen);
Simon Glassd5555b72016-05-05 07:28:09 -0600426 *packetp = (void *)net_rx_packets[0];
427 } else {
428 *packetp = buffer;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100429 }
430
Simon Glassd5555b72016-05-05 07:28:09 -0600431 if (++next_rx_tail >= MACB_RX_RING_SIZE)
432 next_rx_tail = 0;
433 macb->next_rx_tail = next_rx_tail;
434 return length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100435 } else {
Simon Glassd5555b72016-05-05 07:28:09 -0600436 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
437 macb->wrapped = true;
438 next_rx_tail = 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100439 }
440 }
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200441 barrier();
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100442 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100443}
444
Simon Glassd5555b72016-05-05 07:28:09 -0600445static void macb_phy_reset(struct macb_device *macb, const char *name)
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200446{
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200447 int i;
448 u16 status, adv;
449
450 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
451 macb_mdio_write(macb, MII_ADVERTISE, adv);
Simon Glassd5555b72016-05-05 07:28:09 -0600452 printf("%s: Starting autonegotiation...\n", name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200453 macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
454 | BMCR_ANRESTART));
455
Andreas Bießmannceef9832014-05-26 22:55:18 +0200456 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200457 status = macb_mdio_read(macb, MII_BMSR);
458 if (status & BMSR_ANEGCOMPLETE)
459 break;
460 udelay(100);
461 }
462
463 if (status & BMSR_ANEGCOMPLETE)
Simon Glassd5555b72016-05-05 07:28:09 -0600464 printf("%s: Autonegotiation complete\n", name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200465 else
466 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600467 name, status);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200468}
469
Wenyou Yanga212b662016-05-17 13:11:35 +0800470static int macb_phy_find(struct macb_device *macb, const char *name)
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100471{
472 int i;
473 u16 phy_id;
474
475 /* Search for PHY... */
476 for (i = 0; i < 32; i++) {
477 macb->phy_addr = i;
478 phy_id = macb_mdio_read(macb, MII_PHYSID1);
479 if (phy_id != 0xffff) {
Wenyou Yanga212b662016-05-17 13:11:35 +0800480 printf("%s: PHY present at %d\n", name, i);
Wilson Lee4bf56912017-08-22 20:25:07 -0700481 return 0;
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100482 }
483 }
484
485 /* PHY isn't up to snuff */
Wenyou Yanga212b662016-05-17 13:11:35 +0800486 printf("%s: PHY not found\n", name);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100487
Wilson Lee4bf56912017-08-22 20:25:07 -0700488 return -ENODEV;
489}
490
491/**
492 * macb_linkspd_cb - Linkspeed change callback function
Bin Menga5e3d232019-05-22 00:09:45 -0700493 * @dev/@regs: MACB udevice (DM version) or
494 * Base Register of MACB devices (non-DM version)
Wilson Lee4bf56912017-08-22 20:25:07 -0700495 * @speed: Linkspeed
496 * Returns 0 when operation success and negative errno number
497 * when operation failed.
498 */
Bin Menga5e3d232019-05-22 00:09:45 -0700499#ifdef CONFIG_DM_ETH
Anup Pateld0a04db2019-07-24 04:09:32 +0000500static int macb_sifive_clk_init(struct udevice *dev, ulong rate)
501{
502 fdt_addr_t addr;
503 void *gemgxl_regs;
504
505 addr = dev_read_addr_index(dev, 1);
506 if (addr == FDT_ADDR_T_NONE)
507 return -ENODEV;
508
509 gemgxl_regs = (void __iomem *)addr;
510 if (!gemgxl_regs)
511 return -ENODEV;
512
513 /*
514 * SiFive GEMGXL TX clock operation mode:
515 *
516 * 0 = GMII mode. Use 125 MHz gemgxlclk from PRCI in TX logic
517 * and output clock on GMII output signal GTX_CLK
518 * 1 = MII mode. Use MII input signal TX_CLK in TX logic
519 */
520 writel(rate != 125000000, gemgxl_regs);
521 return 0;
522}
523
Bin Menga5e3d232019-05-22 00:09:45 -0700524int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
525{
Bin Meng3ef64442019-05-22 00:09:46 -0700526#ifdef CONFIG_CLK
Anup Pateld0a04db2019-07-24 04:09:32 +0000527 struct macb_device *macb = dev_get_priv(dev);
Bin Meng3ef64442019-05-22 00:09:46 -0700528 struct clk tx_clk;
529 ulong rate;
530 int ret;
531
Bin Meng3ef64442019-05-22 00:09:46 -0700532 switch (speed) {
533 case _10BASET:
534 rate = 2500000; /* 2.5 MHz */
535 break;
536 case _100BASET:
537 rate = 25000000; /* 25 MHz */
538 break;
539 case _1000BASET:
540 rate = 125000000; /* 125 MHz */
541 break;
542 default:
543 /* does not change anything */
544 return 0;
545 }
546
Anup Pateld0a04db2019-07-24 04:09:32 +0000547 if (macb->config->clk_init)
548 return macb->config->clk_init(dev, rate);
549
550 /*
551 * "tx_clk" is an optional clock source for MACB.
552 * Ignore if it does not exist in DT.
553 */
554 ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
555 if (ret)
556 return 0;
557
Bin Meng3ef64442019-05-22 00:09:46 -0700558 if (tx_clk.dev) {
559 ret = clk_set_rate(&tx_clk, rate);
560 if (ret)
561 return ret;
562 }
563#endif
564
Bin Menga5e3d232019-05-22 00:09:45 -0700565 return 0;
566}
567#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700568int __weak macb_linkspd_cb(void *regs, unsigned int speed)
569{
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100570 return 0;
571}
Bin Menga5e3d232019-05-22 00:09:45 -0700572#endif
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100573
Wenyou Yanga212b662016-05-17 13:11:35 +0800574#ifdef CONFIG_DM_ETH
575static int macb_phy_init(struct udevice *dev, const char *name)
576#else
Simon Glassd5555b72016-05-05 07:28:09 -0600577static int macb_phy_init(struct macb_device *macb, const char *name)
Wenyou Yanga212b662016-05-17 13:11:35 +0800578#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100579{
Wenyou Yanga212b662016-05-17 13:11:35 +0800580#ifdef CONFIG_DM_ETH
581 struct macb_device *macb = dev_get_priv(dev);
582#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100583 u32 ncfgr;
584 u16 phy_id, status, adv, lpa;
585 int media, speed, duplex;
Wilson Lee4bf56912017-08-22 20:25:07 -0700586 int ret;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100587 int i;
588
Simon Glassd5555b72016-05-05 07:28:09 -0600589 arch_get_mdio_control(name);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100590 /* Auto-detect phy_addr */
Wilson Lee4bf56912017-08-22 20:25:07 -0700591 ret = macb_phy_find(macb, name);
592 if (ret)
593 return ret;
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100594
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100595 /* Check if the PHY is up to snuff... */
596 phy_id = macb_mdio_read(macb, MII_PHYSID1);
597 if (phy_id == 0xffff) {
Simon Glassd5555b72016-05-05 07:28:09 -0600598 printf("%s: No PHY present\n", name);
Wilson Lee4bf56912017-08-22 20:25:07 -0700599 return -ENODEV;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100600 }
601
Bo Shenb1a00062013-04-24 15:59:27 +0800602#ifdef CONFIG_PHYLIB
Wenyou Yanga212b662016-05-17 13:11:35 +0800603#ifdef CONFIG_DM_ETH
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800604 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
Wenyou Yanga212b662016-05-17 13:11:35 +0800605 macb->phy_interface);
606#else
Bo Shen8314ccd2013-08-19 10:35:47 +0800607 /* need to consider other phy interface mode */
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800608 macb->phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
Bo Shen8314ccd2013-08-19 10:35:47 +0800609 PHY_INTERFACE_MODE_RGMII);
Wenyou Yanga212b662016-05-17 13:11:35 +0800610#endif
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800611 if (!macb->phydev) {
Bo Shen8314ccd2013-08-19 10:35:47 +0800612 printf("phy_connect failed\n");
613 return -ENODEV;
614 }
615
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800616 phy_config(macb->phydev);
Bo Shenb1a00062013-04-24 15:59:27 +0800617#endif
618
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200619 status = macb_mdio_read(macb, MII_BMSR);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100620 if (!(status & BMSR_LSTATUS)) {
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200621 /* Try to re-negotiate if we don't have link already. */
Simon Glassd5555b72016-05-05 07:28:09 -0600622 macb_phy_reset(macb, name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200623
Andreas Bießmannceef9832014-05-26 22:55:18 +0200624 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100625 status = macb_mdio_read(macb, MII_BMSR);
Stefan Roese7bf9bca2019-03-27 11:20:19 +0100626 if (status & BMSR_LSTATUS) {
627 /*
628 * Delay a bit after the link is established,
629 * so that the next xfer does not fail
630 */
631 mdelay(10);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100632 break;
Stefan Roese7bf9bca2019-03-27 11:20:19 +0100633 }
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200634 udelay(100);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100635 }
636 }
637
638 if (!(status & BMSR_LSTATUS)) {
639 printf("%s: link down (status: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600640 name, status);
Wilson Lee4bf56912017-08-22 20:25:07 -0700641 return -ENETDOWN;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100642 }
Bo Shend256be22013-04-24 15:59:28 +0800643
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100644 /* First check for GMAC and that it is GiB capable */
645 if (gem_is_gigabit_capable(macb)) {
Radu Pirea1b0c9912019-06-07 14:18:35 +0300646 lpa = macb_mdio_read(macb, MII_LPA);
Bo Shend256be22013-04-24 15:59:28 +0800647
Radu Pirea0dc97fc2019-06-07 14:18:36 +0300648 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
649 LPA_1000XHALF)) {
650 duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
651 1 : 0);
Andreas Bießmann47609572014-09-18 23:46:48 +0200652
653 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600654 name,
Bo Shend256be22013-04-24 15:59:28 +0800655 duplex ? "full" : "half",
656 lpa);
657
658 ncfgr = macb_readl(macb, NCFGR);
Andreas Bießmann47609572014-09-18 23:46:48 +0200659 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
660 ncfgr |= GEM_BIT(GBE);
661
Bo Shend256be22013-04-24 15:59:28 +0800662 if (duplex)
663 ncfgr |= MACB_BIT(FD);
Andreas Bießmann47609572014-09-18 23:46:48 +0200664
Bo Shend256be22013-04-24 15:59:28 +0800665 macb_writel(macb, NCFGR, ncfgr);
666
Bin Menga5e3d232019-05-22 00:09:45 -0700667#ifdef CONFIG_DM_ETH
668 ret = macb_linkspd_cb(dev, _1000BASET);
669#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700670 ret = macb_linkspd_cb(macb->regs, _1000BASET);
Bin Menga5e3d232019-05-22 00:09:45 -0700671#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700672 if (ret)
673 return ret;
674
675 return 0;
Bo Shend256be22013-04-24 15:59:28 +0800676 }
677 }
678
679 /* fall back for EMAC checking */
680 adv = macb_mdio_read(macb, MII_ADVERTISE);
681 lpa = macb_mdio_read(macb, MII_LPA);
682 media = mii_nway_result(lpa & adv);
683 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
684 ? 1 : 0);
685 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
686 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600687 name,
Bo Shend256be22013-04-24 15:59:28 +0800688 speed ? "100" : "10",
689 duplex ? "full" : "half",
690 lpa);
691
692 ncfgr = macb_readl(macb, NCFGR);
Bo Shenc83cb5f2015-03-04 13:35:16 +0800693 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
Wilson Lee4bf56912017-08-22 20:25:07 -0700694 if (speed) {
Bo Shend256be22013-04-24 15:59:28 +0800695 ncfgr |= MACB_BIT(SPD);
Bin Menga5e3d232019-05-22 00:09:45 -0700696#ifdef CONFIG_DM_ETH
697 ret = macb_linkspd_cb(dev, _100BASET);
698#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700699 ret = macb_linkspd_cb(macb->regs, _100BASET);
Bin Menga5e3d232019-05-22 00:09:45 -0700700#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700701 } else {
Bin Menga5e3d232019-05-22 00:09:45 -0700702#ifdef CONFIG_DM_ETH
703 ret = macb_linkspd_cb(dev, _10BASET);
704#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700705 ret = macb_linkspd_cb(macb->regs, _10BASET);
Bin Menga5e3d232019-05-22 00:09:45 -0700706#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700707 }
708
709 if (ret)
710 return ret;
711
Bo Shend256be22013-04-24 15:59:28 +0800712 if (duplex)
713 ncfgr |= MACB_BIT(FD);
714 macb_writel(macb, NCFGR, ncfgr);
715
Wilson Lee4bf56912017-08-22 20:25:07 -0700716 return 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100717}
718
Wu, Joshade4ea42015-06-03 16:45:44 +0800719static int gmac_init_multi_queues(struct macb_device *macb)
720{
721 int i, num_queues = 1;
722 u32 queue_mask;
723
724 /* bit 0 is never set but queue 0 always exists */
725 queue_mask = gem_readl(macb, DCFG6) & 0xff;
726 queue_mask |= 0x1;
727
728 for (i = 1; i < MACB_MAX_QUEUES; i++)
729 if (queue_mask & (1 << i))
730 num_queues++;
731
Ramon Fried0a2827e2019-07-16 22:04:33 +0300732 macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
Wu, Joshade4ea42015-06-03 16:45:44 +0800733 macb->dummy_desc->addr = 0;
734 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200735 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
Wu, Joshade4ea42015-06-03 16:45:44 +0800736
737 for (i = 1; i < num_queues; i++)
738 gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
739
740 return 0;
741}
742
Ramon Fried9c295802019-07-16 22:04:36 +0300743static void gmac_configure_dma(struct macb_device *macb)
744{
745 u32 buffer_size;
746 u32 dmacfg;
747
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300748 buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
Ramon Fried9c295802019-07-16 22:04:36 +0300749 dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
750 dmacfg |= GEM_BF(RXBS, buffer_size);
751
Anup Pateld0a04db2019-07-24 04:09:32 +0000752 if (macb->config->dma_burst_length)
753 dmacfg = GEM_BFINS(FBLDO,
754 macb->config->dma_burst_length, dmacfg);
Ramon Fried9c295802019-07-16 22:04:36 +0300755
756 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
757 dmacfg &= ~GEM_BIT(ENDIA_PKT);
758
Anup Pateleff0e0c2019-07-24 04:09:37 +0000759 if (macb->is_big_endian)
Ramon Fried9c295802019-07-16 22:04:36 +0300760 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
Anup Pateleff0e0c2019-07-24 04:09:37 +0000761 else
762 dmacfg &= ~GEM_BIT(ENDIA_DESC);
Ramon Fried9c295802019-07-16 22:04:36 +0300763
764 dmacfg &= ~GEM_BIT(ADDR64);
765 gem_writel(macb, DMACFG, dmacfg);
766}
767
Wenyou Yanga212b662016-05-17 13:11:35 +0800768#ifdef CONFIG_DM_ETH
769static int _macb_init(struct udevice *dev, const char *name)
770#else
Simon Glassd5555b72016-05-05 07:28:09 -0600771static int _macb_init(struct macb_device *macb, const char *name)
Wenyou Yanga212b662016-05-17 13:11:35 +0800772#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100773{
Wenyou Yanga212b662016-05-17 13:11:35 +0800774#ifdef CONFIG_DM_ETH
775 struct macb_device *macb = dev_get_priv(dev);
776#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100777 unsigned long paddr;
Wilson Lee4bf56912017-08-22 20:25:07 -0700778 int ret;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100779 int i;
780
781 /*
782 * macb_halt should have been called at some point before now,
783 * so we'll assume the controller is idle.
784 */
785
786 /* initialize DMA descriptors */
787 paddr = macb->rx_buffer_dma;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200788 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
789 if (i == (MACB_RX_RING_SIZE - 1))
Ramon Fried0a2827e2019-07-16 22:04:33 +0300790 paddr |= MACB_BIT(RX_WRAP);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100791 macb->rx_ring[i].addr = paddr;
792 macb->rx_ring[i].ctrl = 0;
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300793 paddr += macb->rx_buffer_size;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100794 }
Wu, Josh5ae0e382014-05-27 16:31:05 +0800795 macb_flush_ring_desc(macb, RX);
796 macb_flush_rx_buffer(macb);
797
Andreas Bießmannceef9832014-05-26 22:55:18 +0200798 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100799 macb->tx_ring[i].addr = 0;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200800 if (i == (MACB_TX_RING_SIZE - 1))
Ramon Fried0a2827e2019-07-16 22:04:33 +0300801 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED) |
802 MACB_BIT(TX_WRAP);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100803 else
Ramon Fried0a2827e2019-07-16 22:04:33 +0300804 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100805 }
Wu, Josh5ae0e382014-05-27 16:31:05 +0800806 macb_flush_ring_desc(macb, TX);
807
Andreas Bießmannceef9832014-05-26 22:55:18 +0200808 macb->rx_tail = 0;
809 macb->tx_head = 0;
810 macb->tx_tail = 0;
Simon Glassd5555b72016-05-05 07:28:09 -0600811 macb->next_rx_tail = 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100812
Wilson Lee4bf56912017-08-22 20:25:07 -0700813#ifdef CONFIG_MACB_ZYNQ
814 macb_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
815#endif
816
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100817 macb_writel(macb, RBQP, macb->rx_ring_dma);
818 macb_writel(macb, TBQP, macb->tx_ring_dma);
819
Bo Shend256be22013-04-24 15:59:28 +0800820 if (macb_is_gem(macb)) {
Ramon Fried9c295802019-07-16 22:04:36 +0300821 /* Initialize DMA properties */
822 gmac_configure_dma(macb);
Wu, Joshade4ea42015-06-03 16:45:44 +0800823 /* Check the multi queue and initialize the queue for tx */
824 gmac_init_multi_queues(macb);
825
Bo Shencabf61c2014-11-10 15:24:01 +0800826 /*
827 * When the GMAC IP with GE feature, this bit is used to
828 * select interface between RGMII and GMII.
829 * When the GMAC IP without GE feature, this bit is used
830 * to select interface between RMII and MII.
831 */
Wenyou Yanga212b662016-05-17 13:11:35 +0800832#ifdef CONFIG_DM_ETH
Wenyou Yang6de046e2017-04-20 11:13:13 +0800833 if ((macb->phy_interface == PHY_INTERFACE_MODE_RMII) ||
834 (macb->phy_interface == PHY_INTERFACE_MODE_RGMII))
Ramon Fried6c636512019-07-16 22:03:00 +0300835 gem_writel(macb, USRIO, GEM_BIT(RGMII));
Wenyou Yanga212b662016-05-17 13:11:35 +0800836 else
Ramon Fried6c636512019-07-16 22:03:00 +0300837 gem_writel(macb, USRIO, 0);
Ramon Fried5a1899f2019-07-16 22:04:34 +0300838
839 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
840 unsigned int ncfgr = macb_readl(macb, NCFGR);
841
842 ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
843 macb_writel(macb, NCFGR, ncfgr);
844 }
Wenyou Yanga212b662016-05-17 13:11:35 +0800845#else
Bo Shencabf61c2014-11-10 15:24:01 +0800846#if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
Ramon Fried6c636512019-07-16 22:03:00 +0300847 gem_writel(macb, USRIO, GEM_BIT(RGMII));
Bo Shend256be22013-04-24 15:59:28 +0800848#else
Ramon Fried6c636512019-07-16 22:03:00 +0300849 gem_writel(macb, USRIO, 0);
Bo Shend256be22013-04-24 15:59:28 +0800850#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800851#endif
Bo Shend256be22013-04-24 15:59:28 +0800852 } else {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100853 /* choose RMII or MII mode. This depends on the board */
Wenyou Yanga212b662016-05-17 13:11:35 +0800854#ifdef CONFIG_DM_ETH
855#ifdef CONFIG_AT91FAMILY
856 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
857 macb_writel(macb, USRIO,
858 MACB_BIT(RMII) | MACB_BIT(CLKEN));
859 } else {
860 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
861 }
862#else
863 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
864 macb_writel(macb, USRIO, 0);
865 else
866 macb_writel(macb, USRIO, MACB_BIT(MII));
867#endif
868#else
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100869#ifdef CONFIG_RMII
Bo Shend8f64b42013-04-24 15:59:26 +0800870#ifdef CONFIG_AT91FAMILY
Stelian Pop7263ef12008-01-03 21:15:56 +0000871 macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
872#else
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100873 macb_writel(macb, USRIO, 0);
Stelian Pop7263ef12008-01-03 21:15:56 +0000874#endif
875#else
Bo Shend8f64b42013-04-24 15:59:26 +0800876#ifdef CONFIG_AT91FAMILY
Stelian Pop7263ef12008-01-03 21:15:56 +0000877 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100878#else
879 macb_writel(macb, USRIO, MACB_BIT(MII));
880#endif
Stelian Pop7263ef12008-01-03 21:15:56 +0000881#endif /* CONFIG_RMII */
Wenyou Yanga212b662016-05-17 13:11:35 +0800882#endif
Bo Shend256be22013-04-24 15:59:28 +0800883 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100884
Wenyou Yanga212b662016-05-17 13:11:35 +0800885#ifdef CONFIG_DM_ETH
Wilson Lee4bf56912017-08-22 20:25:07 -0700886 ret = macb_phy_init(dev, name);
Wenyou Yanga212b662016-05-17 13:11:35 +0800887#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700888 ret = macb_phy_init(macb, name);
Wenyou Yanga212b662016-05-17 13:11:35 +0800889#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700890 if (ret)
891 return ret;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100892
893 /* Enable TX and RX */
894 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
895
Ben Warren422b1a02008-01-09 18:15:53 -0500896 return 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100897}
898
Simon Glassd5555b72016-05-05 07:28:09 -0600899static void _macb_halt(struct macb_device *macb)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100900{
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100901 u32 ncr, tsr;
902
903 /* Halt the controller and wait for any ongoing transmission to end. */
904 ncr = macb_readl(macb, NCR);
905 ncr |= MACB_BIT(THALT);
906 macb_writel(macb, NCR, ncr);
907
908 do {
909 tsr = macb_readl(macb, TSR);
910 } while (tsr & MACB_BIT(TGO));
911
912 /* Disable TX and RX, and clear statistics */
913 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
914}
915
Simon Glassd5555b72016-05-05 07:28:09 -0600916static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
Ben Warren6bb46792010-06-01 11:55:42 -0700917{
Ben Warren6bb46792010-06-01 11:55:42 -0700918 u32 hwaddr_bottom;
919 u16 hwaddr_top;
920
921 /* set hardware address */
Simon Glassd5555b72016-05-05 07:28:09 -0600922 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
923 enetaddr[2] << 16 | enetaddr[3] << 24;
Ben Warren6bb46792010-06-01 11:55:42 -0700924 macb_writel(macb, SA1B, hwaddr_bottom);
Simon Glassd5555b72016-05-05 07:28:09 -0600925 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
Ben Warren6bb46792010-06-01 11:55:42 -0700926 macb_writel(macb, SA1T, hwaddr_top);
927 return 0;
928}
929
Bo Shend256be22013-04-24 15:59:28 +0800930static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
931{
932 u32 config;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800933#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800934 unsigned long macb_hz = macb->pclk_rate;
935#else
Bo Shend256be22013-04-24 15:59:28 +0800936 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800937#endif
Bo Shend256be22013-04-24 15:59:28 +0800938
939 if (macb_hz < 20000000)
940 config = MACB_BF(CLK, MACB_CLK_DIV8);
941 else if (macb_hz < 40000000)
942 config = MACB_BF(CLK, MACB_CLK_DIV16);
943 else if (macb_hz < 80000000)
944 config = MACB_BF(CLK, MACB_CLK_DIV32);
945 else
946 config = MACB_BF(CLK, MACB_CLK_DIV64);
947
948 return config;
949}
950
951static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
952{
953 u32 config;
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800954
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800955#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800956 unsigned long macb_hz = macb->pclk_rate;
957#else
Bo Shend256be22013-04-24 15:59:28 +0800958 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800959#endif
Bo Shend256be22013-04-24 15:59:28 +0800960
961 if (macb_hz < 20000000)
962 config = GEM_BF(CLK, GEM_CLK_DIV8);
963 else if (macb_hz < 40000000)
964 config = GEM_BF(CLK, GEM_CLK_DIV16);
965 else if (macb_hz < 80000000)
966 config = GEM_BF(CLK, GEM_CLK_DIV32);
967 else if (macb_hz < 120000000)
968 config = GEM_BF(CLK, GEM_CLK_DIV48);
969 else if (macb_hz < 160000000)
970 config = GEM_BF(CLK, GEM_CLK_DIV64);
Ramon Fried9e65f802019-07-16 22:04:32 +0300971 else if (macb_hz < 240000000)
Bo Shend256be22013-04-24 15:59:28 +0800972 config = GEM_BF(CLK, GEM_CLK_DIV96);
Ramon Fried9e65f802019-07-16 22:04:32 +0300973 else if (macb_hz < 320000000)
974 config = GEM_BF(CLK, GEM_CLK_DIV128);
975 else
976 config = GEM_BF(CLK, GEM_CLK_DIV224);
Bo Shend256be22013-04-24 15:59:28 +0800977
978 return config;
979}
980
Bo Shen32e4f6b2013-09-18 15:07:44 +0800981/*
982 * Get the DMA bus width field of the network configuration register that we
983 * should program. We find the width from decoding the design configuration
984 * register to find the maximum supported data bus width.
985 */
986static u32 macb_dbw(struct macb_device *macb)
987{
988 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
989 case 4:
990 return GEM_BF(DBW, GEM_DBW128);
991 case 2:
992 return GEM_BF(DBW, GEM_DBW64);
993 case 1:
994 default:
995 return GEM_BF(DBW, GEM_DBW32);
996 }
997}
998
Simon Glassd5555b72016-05-05 07:28:09 -0600999static void _macb_eth_initialize(struct macb_device *macb)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001000{
Simon Glassd5555b72016-05-05 07:28:09 -06001001 int id = 0; /* This is not used by functions we call */
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001002 u32 ncfgr;
1003
Ramon Friedc6d07bf2019-07-14 18:25:14 +03001004 if (macb_is_gem(macb))
1005 macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
1006 else
1007 macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1008
Simon Glassd5555b72016-05-05 07:28:09 -06001009 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
Ramon Friedc6d07bf2019-07-14 18:25:14 +03001010 macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
1011 MACB_RX_RING_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001012 &macb->rx_buffer_dma);
Wu, Josh5ae0e382014-05-27 16:31:05 +08001013 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001014 &macb->rx_ring_dma);
Wu, Josh5ae0e382014-05-27 16:31:05 +08001015 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001016 &macb->tx_ring_dma);
Wu, Joshade4ea42015-06-03 16:45:44 +08001017 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
1018 &macb->dummy_desc_dma);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001019
Simon Glassd5555b72016-05-05 07:28:09 -06001020 /*
1021 * Do some basic initialization so that we at least can talk
1022 * to the PHY
1023 */
1024 if (macb_is_gem(macb)) {
1025 ncfgr = gem_mdc_clk_div(id, macb);
1026 ncfgr |= macb_dbw(macb);
1027 } else {
1028 ncfgr = macb_mdc_clk_div(id, macb);
1029 }
1030
1031 macb_writel(macb, NCFGR, ncfgr);
1032}
1033
Simon Glassf1dcc192016-05-05 07:28:11 -06001034#ifndef CONFIG_DM_ETH
Simon Glassd5555b72016-05-05 07:28:09 -06001035static int macb_send(struct eth_device *netdev, void *packet, int length)
1036{
1037 struct macb_device *macb = to_macb(netdev);
1038
1039 return _macb_send(macb, netdev->name, packet, length);
1040}
1041
1042static int macb_recv(struct eth_device *netdev)
1043{
1044 struct macb_device *macb = to_macb(netdev);
1045 uchar *packet;
1046 int length;
1047
1048 macb->wrapped = false;
1049 for (;;) {
1050 macb->next_rx_tail = macb->rx_tail;
1051 length = _macb_recv(macb, &packet);
1052 if (length >= 0) {
1053 net_process_received_packet(packet, length);
1054 reclaim_rx_buffers(macb, macb->next_rx_tail);
Heinrich Schuchardt6cdf0722018-03-18 11:32:53 +01001055 } else {
Simon Glassd5555b72016-05-05 07:28:09 -06001056 return length;
1057 }
1058 }
1059}
1060
1061static int macb_init(struct eth_device *netdev, bd_t *bd)
1062{
1063 struct macb_device *macb = to_macb(netdev);
1064
1065 return _macb_init(macb, netdev->name);
1066}
1067
1068static void macb_halt(struct eth_device *netdev)
1069{
1070 struct macb_device *macb = to_macb(netdev);
1071
1072 return _macb_halt(macb);
1073}
1074
1075static int macb_write_hwaddr(struct eth_device *netdev)
1076{
1077 struct macb_device *macb = to_macb(netdev);
1078
1079 return _macb_write_hwaddr(macb, netdev->enetaddr);
1080}
1081
1082int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
1083{
1084 struct macb_device *macb;
1085 struct eth_device *netdev;
1086
1087 macb = malloc(sizeof(struct macb_device));
1088 if (!macb) {
1089 printf("Error: Failed to allocate memory for MACB%d\n", id);
1090 return -1;
1091 }
1092 memset(macb, 0, sizeof(struct macb_device));
1093
1094 netdev = &macb->netdev;
Wu, Josh5ae0e382014-05-27 16:31:05 +08001095
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001096 macb->regs = regs;
1097 macb->phy_addr = phy_addr;
1098
Bo Shend256be22013-04-24 15:59:28 +08001099 if (macb_is_gem(macb))
1100 sprintf(netdev->name, "gmac%d", id);
1101 else
1102 sprintf(netdev->name, "macb%d", id);
1103
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001104 netdev->init = macb_init;
1105 netdev->halt = macb_halt;
1106 netdev->send = macb_send;
1107 netdev->recv = macb_recv;
Ben Warren6bb46792010-06-01 11:55:42 -07001108 netdev->write_hwaddr = macb_write_hwaddr;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001109
Simon Glassd5555b72016-05-05 07:28:09 -06001110 _macb_eth_initialize(macb);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001111
1112 eth_register(netdev);
1113
Bo Shenb1a00062013-04-24 15:59:27 +08001114#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Joe Hershberger5a49f172016-08-08 11:28:38 -05001115 int retval;
1116 struct mii_dev *mdiodev = mdio_alloc();
1117 if (!mdiodev)
1118 return -ENOMEM;
1119 strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
1120 mdiodev->read = macb_miiphy_read;
1121 mdiodev->write = macb_miiphy_write;
1122
1123 retval = mdio_register(mdiodev);
1124 if (retval < 0)
1125 return retval;
Bo Shenb1a00062013-04-24 15:59:27 +08001126 macb->bus = miiphy_get_dev_by_name(netdev->name);
Semih Hazar0f751d62009-12-17 15:07:15 +02001127#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001128 return 0;
1129}
Simon Glassf1dcc192016-05-05 07:28:11 -06001130#endif /* !CONFIG_DM_ETH */
1131
1132#ifdef CONFIG_DM_ETH
1133
1134static int macb_start(struct udevice *dev)
1135{
Wenyou Yanga212b662016-05-17 13:11:35 +08001136 return _macb_init(dev, dev->name);
Simon Glassf1dcc192016-05-05 07:28:11 -06001137}
1138
1139static int macb_send(struct udevice *dev, void *packet, int length)
1140{
1141 struct macb_device *macb = dev_get_priv(dev);
1142
1143 return _macb_send(macb, dev->name, packet, length);
1144}
1145
1146static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1147{
1148 struct macb_device *macb = dev_get_priv(dev);
1149
1150 macb->next_rx_tail = macb->rx_tail;
1151 macb->wrapped = false;
1152
1153 return _macb_recv(macb, packetp);
1154}
1155
1156static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1157{
1158 struct macb_device *macb = dev_get_priv(dev);
1159
1160 reclaim_rx_buffers(macb, macb->next_rx_tail);
1161
1162 return 0;
1163}
1164
1165static void macb_stop(struct udevice *dev)
1166{
1167 struct macb_device *macb = dev_get_priv(dev);
1168
1169 _macb_halt(macb);
1170}
1171
1172static int macb_write_hwaddr(struct udevice *dev)
1173{
1174 struct eth_pdata *plat = dev_get_platdata(dev);
1175 struct macb_device *macb = dev_get_priv(dev);
1176
1177 return _macb_write_hwaddr(macb, plat->enetaddr);
1178}
1179
1180static const struct eth_ops macb_eth_ops = {
1181 .start = macb_start,
1182 .send = macb_send,
1183 .recv = macb_recv,
1184 .stop = macb_stop,
1185 .free_pkt = macb_free_pkt,
1186 .write_hwaddr = macb_write_hwaddr,
1187};
1188
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001189#ifdef CONFIG_CLK
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001190static int macb_enable_clk(struct udevice *dev)
1191{
1192 struct macb_device *macb = dev_get_priv(dev);
1193 struct clk clk;
1194 ulong clk_rate;
1195 int ret;
1196
1197 ret = clk_get_by_index(dev, 0, &clk);
1198 if (ret)
1199 return -EINVAL;
1200
Wilson Lee4bf56912017-08-22 20:25:07 -07001201 /*
Anup Patel2e242f52019-02-25 08:14:36 +00001202 * If clock driver didn't support enable or disable then
1203 * we get -ENOSYS from clk_enable(). To handle this, we
1204 * don't fail for ret == -ENOSYS.
Wilson Lee4bf56912017-08-22 20:25:07 -07001205 */
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001206 ret = clk_enable(&clk);
Anup Patel2e242f52019-02-25 08:14:36 +00001207 if (ret && ret != -ENOSYS)
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001208 return ret;
1209
1210 clk_rate = clk_get_rate(&clk);
1211 if (!clk_rate)
1212 return -EINVAL;
1213
1214 macb->pclk_rate = clk_rate;
1215
1216 return 0;
1217}
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001218#endif
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001219
Ramon Frieded3c64f2019-07-16 22:04:35 +03001220static const struct macb_config default_gem_config = {
1221 .dma_burst_length = 16,
Anup Pateld0a04db2019-07-24 04:09:32 +00001222 .clk_init = NULL,
Ramon Frieded3c64f2019-07-16 22:04:35 +03001223};
1224
Simon Glassf1dcc192016-05-05 07:28:11 -06001225static int macb_eth_probe(struct udevice *dev)
1226{
1227 struct eth_pdata *pdata = dev_get_platdata(dev);
1228 struct macb_device *macb = dev_get_priv(dev);
Wenyou Yanga212b662016-05-17 13:11:35 +08001229 const char *phy_mode;
Anup Pateld0a04db2019-07-24 04:09:32 +00001230 int ret;
Wenyou Yanga212b662016-05-17 13:11:35 +08001231
Simon Glasse160f7d2017-01-17 16:52:55 -07001232 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1233 NULL);
Wenyou Yanga212b662016-05-17 13:11:35 +08001234 if (phy_mode)
1235 macb->phy_interface = phy_get_interface_by_name(phy_mode);
1236 if (macb->phy_interface == -1) {
1237 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1238 return -EINVAL;
1239 }
Wenyou Yanga212b662016-05-17 13:11:35 +08001240
Simon Glassf1dcc192016-05-05 07:28:11 -06001241 macb->regs = (void *)pdata->iobase;
1242
Anup Pateleff0e0c2019-07-24 04:09:37 +00001243 macb->is_big_endian = (cpu_to_be32(0x12345678) == 0x12345678);
1244
Anup Pateld0a04db2019-07-24 04:09:32 +00001245 macb->config = (struct macb_config *)dev_get_driver_data(dev);
1246 if (!macb->config)
1247 macb->config = &default_gem_config;
Ramon Frieded3c64f2019-07-16 22:04:35 +03001248
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001249#ifdef CONFIG_CLK
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001250 ret = macb_enable_clk(dev);
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001251 if (ret)
1252 return ret;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001253#endif
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001254
Simon Glassf1dcc192016-05-05 07:28:11 -06001255 _macb_eth_initialize(macb);
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001256
Simon Glassf1dcc192016-05-05 07:28:11 -06001257#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001258 macb->bus = mdio_alloc();
1259 if (!macb->bus)
Joe Hershberger5a49f172016-08-08 11:28:38 -05001260 return -ENOMEM;
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001261 strncpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
1262 macb->bus->read = macb_miiphy_read;
1263 macb->bus->write = macb_miiphy_write;
Joe Hershberger5a49f172016-08-08 11:28:38 -05001264
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001265 ret = mdio_register(macb->bus);
1266 if (ret < 0)
1267 return ret;
Simon Glassf1dcc192016-05-05 07:28:11 -06001268 macb->bus = miiphy_get_dev_by_name(dev->name);
1269#endif
1270
1271 return 0;
1272}
1273
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001274static int macb_eth_remove(struct udevice *dev)
1275{
1276 struct macb_device *macb = dev_get_priv(dev);
1277
1278#ifdef CONFIG_PHYLIB
1279 free(macb->phydev);
1280#endif
1281 mdio_unregister(macb->bus);
1282 mdio_free(macb->bus);
1283
1284 return 0;
1285}
1286
Wilson Lee4bf56912017-08-22 20:25:07 -07001287/**
1288 * macb_late_eth_ofdata_to_platdata
1289 * @dev: udevice struct
1290 * Returns 0 when operation success and negative errno number
1291 * when operation failed.
1292 */
1293int __weak macb_late_eth_ofdata_to_platdata(struct udevice *dev)
1294{
1295 return 0;
1296}
1297
Simon Glassf1dcc192016-05-05 07:28:11 -06001298static int macb_eth_ofdata_to_platdata(struct udevice *dev)
1299{
1300 struct eth_pdata *pdata = dev_get_platdata(dev);
1301
Ramon Fried9043c4e2018-12-27 19:58:42 +02001302 pdata->iobase = (phys_addr_t)dev_remap_addr(dev);
1303 if (!pdata->iobase)
1304 return -EINVAL;
Wilson Lee4bf56912017-08-22 20:25:07 -07001305
1306 return macb_late_eth_ofdata_to_platdata(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -06001307}
1308
Ramon Frieded3c64f2019-07-16 22:04:35 +03001309static const struct macb_config sama5d4_config = {
1310 .dma_burst_length = 4,
Anup Pateld0a04db2019-07-24 04:09:32 +00001311 .clk_init = NULL,
1312};
1313
1314static const struct macb_config sifive_config = {
1315 .dma_burst_length = 16,
1316 .clk_init = macb_sifive_clk_init,
Ramon Frieded3c64f2019-07-16 22:04:35 +03001317};
1318
Simon Glassf1dcc192016-05-05 07:28:11 -06001319static const struct udevice_id macb_eth_ids[] = {
1320 { .compatible = "cdns,macb" },
Wenyou Yang75460252017-04-14 14:36:05 +08001321 { .compatible = "cdns,at91sam9260-macb" },
1322 { .compatible = "atmel,sama5d2-gem" },
1323 { .compatible = "atmel,sama5d3-gem" },
Ramon Frieded3c64f2019-07-16 22:04:35 +03001324 { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
Wilson Lee4bf56912017-08-22 20:25:07 -07001325 { .compatible = "cdns,zynq-gem" },
Anup Pateld0a04db2019-07-24 04:09:32 +00001326 { .compatible = "sifive,fu540-c000-gem",
1327 .data = (ulong)&sifive_config },
Simon Glassf1dcc192016-05-05 07:28:11 -06001328 { }
1329};
1330
1331U_BOOT_DRIVER(eth_macb) = {
1332 .name = "eth_macb",
1333 .id = UCLASS_ETH,
1334 .of_match = macb_eth_ids,
1335 .ofdata_to_platdata = macb_eth_ofdata_to_platdata,
1336 .probe = macb_eth_probe,
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001337 .remove = macb_eth_remove,
Simon Glassf1dcc192016-05-05 07:28:11 -06001338 .ops = &macb_eth_ops,
1339 .priv_auto_alloc_size = sizeof(struct macb_device),
1340 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1341};
1342#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001343
Jon Loeliger07d38a12007-07-09 17:30:01 -05001344#endif