blob: bd588cab06b37b972238958ca7dc5ffba4230458 [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 Glass1eb69ae2019-11-14 12:57:39 -07007#include <cpu_func.h>
Simon Glassf1dcc192016-05-05 07:28:11 -06008#include <dm.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01009
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010010/*
11 * The u-boot networking stack is a little weird. It seems like the
12 * networking core allocates receive buffers up front without any
13 * regard to the hardware that's supposed to actually receive those
14 * packets.
15 *
16 * The MACB receives packets into 128-byte receive buffers, so the
17 * buffers allocated by the core isn't very practical to use. We'll
18 * allocate our own, but we need one such buffer in case a packet
19 * wraps around the DMA ring so that we have to copy it.
20 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020021 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010022 * configuration header. This way, the core allocates one RX buffer
23 * and one TX buffer, each of which can hold a ethernet packet of
24 * maximum size.
25 *
26 * For some reason, the networking core unconditionally specifies a
27 * 32-byte packet "alignment" (which really should be called
28 * "padding"). MACB shouldn't need that, but we'll refrain from any
29 * core modifications here...
30 */
31
32#include <net.h>
Simon Glassf1dcc192016-05-05 07:28:11 -060033#ifndef CONFIG_DM_ETH
Ben Warren89973f82008-08-31 22:22:04 -070034#include <netdev.h>
Simon Glassf1dcc192016-05-05 07:28:11 -060035#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010036#include <malloc.h>
Semih Hazar0f751d62009-12-17 15:07:15 +020037#include <miiphy.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010038
39#include <linux/mii.h>
40#include <asm/io.h>
Masahiro Yamada9d86b892020-02-14 16:40:19 +090041#include <linux/dma-mapping.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010042#include <asm/arch/clk.h>
Masahiro Yamada5d97dff2016-09-21 11:28:57 +090043#include <linux/errno.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010044
45#include "macb.h"
46
Wenyou Yanga212b662016-05-17 13:11:35 +080047DECLARE_GLOBAL_DATA_PTR;
48
Ramon Friedc6d07bf2019-07-14 18:25:14 +030049/*
50 * These buffer sizes must be power of 2 and divisible
51 * by RX_BUFFER_MULTIPLE
52 */
53#define MACB_RX_BUFFER_SIZE 128
54#define GEM_RX_BUFFER_SIZE 2048
Ramon Fried9c295802019-07-16 22:04:36 +030055#define RX_BUFFER_MULTIPLE 64
Ramon Friedc6d07bf2019-07-14 18:25:14 +030056
57#define MACB_RX_RING_SIZE 32
Andreas Bießmannceef9832014-05-26 22:55:18 +020058#define MACB_TX_RING_SIZE 16
Ramon Friedc6d07bf2019-07-14 18:25:14 +030059
Andreas Bießmannceef9832014-05-26 22:55:18 +020060#define MACB_TX_TIMEOUT 1000
61#define MACB_AUTONEG_TIMEOUT 5000000
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010062
Wilson Lee4bf56912017-08-22 20:25:07 -070063#ifdef CONFIG_MACB_ZYNQ
64/* INCR4 AHB bursts */
65#define MACB_ZYNQ_GEM_DMACR_BLENGTH 0x00000004
66/* Use full configured addressable space (8 Kb) */
67#define MACB_ZYNQ_GEM_DMACR_RXSIZE 0x00000300
68/* Use full configured addressable space (4 Kb) */
69#define MACB_ZYNQ_GEM_DMACR_TXSIZE 0x00000400
70/* Set RXBUF with use of 128 byte */
71#define MACB_ZYNQ_GEM_DMACR_RXBUF 0x00020000
72#define MACB_ZYNQ_GEM_DMACR_INIT \
73 (MACB_ZYNQ_GEM_DMACR_BLENGTH | \
74 MACB_ZYNQ_GEM_DMACR_RXSIZE | \
75 MACB_ZYNQ_GEM_DMACR_TXSIZE | \
76 MACB_ZYNQ_GEM_DMACR_RXBUF)
77#endif
78
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010079struct macb_dma_desc {
80 u32 addr;
81 u32 ctrl;
82};
83
Wu, Josh5ae0e382014-05-27 16:31:05 +080084#define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
85#define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
86#define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
Wu, Joshade4ea42015-06-03 16:45:44 +080087#define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
Wu, Josh5ae0e382014-05-27 16:31:05 +080088
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010089#define RXBUF_FRMLEN_MASK 0x00000fff
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010090#define TXBUF_FRMLEN_MASK 0x000007ff
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010091
92struct macb_device {
93 void *regs;
Anup Pateld0a04db2019-07-24 04:09:32 +000094
Anup Pateleff0e0c2019-07-24 04:09:37 +000095 bool is_big_endian;
96
Anup Pateld0a04db2019-07-24 04:09:32 +000097 const struct macb_config *config;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010098
99 unsigned int rx_tail;
100 unsigned int tx_head;
101 unsigned int tx_tail;
Simon Glassd5555b72016-05-05 07:28:09 -0600102 unsigned int next_rx_tail;
103 bool wrapped;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100104
105 void *rx_buffer;
106 void *tx_buffer;
107 struct macb_dma_desc *rx_ring;
108 struct macb_dma_desc *tx_ring;
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300109 size_t rx_buffer_size;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100110
111 unsigned long rx_buffer_dma;
112 unsigned long rx_ring_dma;
113 unsigned long tx_ring_dma;
114
Wu, Joshade4ea42015-06-03 16:45:44 +0800115 struct macb_dma_desc *dummy_desc;
116 unsigned long dummy_desc_dma;
117
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100118 const struct device *dev;
Simon Glassf1dcc192016-05-05 07:28:11 -0600119#ifndef CONFIG_DM_ETH
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100120 struct eth_device netdev;
Simon Glassf1dcc192016-05-05 07:28:11 -0600121#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100122 unsigned short phy_addr;
Bo Shenb1a00062013-04-24 15:59:27 +0800123 struct mii_dev *bus;
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800124#ifdef CONFIG_PHYLIB
125 struct phy_device *phydev;
126#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800127
128#ifdef CONFIG_DM_ETH
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800129#ifdef CONFIG_CLK
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800130 unsigned long pclk_rate;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800131#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800132 phy_interface_t phy_interface;
133#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100134};
Ramon Frieded3c64f2019-07-16 22:04:35 +0300135
136struct macb_config {
137 unsigned int dma_burst_length;
Anup Pateld0a04db2019-07-24 04:09:32 +0000138
139 int (*clk_init)(struct udevice *dev, ulong rate);
Ramon Frieded3c64f2019-07-16 22:04:35 +0300140};
141
Simon Glassf1dcc192016-05-05 07:28:11 -0600142#ifndef CONFIG_DM_ETH
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100143#define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
Simon Glassf1dcc192016-05-05 07:28:11 -0600144#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100145
Bo Shend256be22013-04-24 15:59:28 +0800146static int macb_is_gem(struct macb_device *macb)
147{
Atish Patrafbcaa262019-02-25 08:14:42 +0000148 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
Bo Shend256be22013-04-24 15:59:28 +0800149}
150
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100151#ifndef cpu_is_sama5d2
152#define cpu_is_sama5d2() 0
153#endif
154
155#ifndef cpu_is_sama5d4
156#define cpu_is_sama5d4() 0
157#endif
158
159static int gem_is_gigabit_capable(struct macb_device *macb)
160{
161 /*
Robert P. J. Day1cc0a9f2016-05-04 04:47:31 -0400162 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100163 * configured to support only 10/100.
164 */
165 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
166}
167
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200168static void macb_mdio_write(struct macb_device *macb, u8 phy_adr, u8 reg,
169 u16 value)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100170{
171 unsigned long netctl;
172 unsigned long netstat;
173 unsigned long frame;
174
175 netctl = macb_readl(macb, NCR);
176 netctl |= MACB_BIT(MPE);
177 macb_writel(macb, NCR, netctl);
178
179 frame = (MACB_BF(SOF, 1)
180 | MACB_BF(RW, 1)
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200181 | MACB_BF(PHYA, phy_adr)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100182 | MACB_BF(REGA, reg)
183 | MACB_BF(CODE, 2)
184 | MACB_BF(DATA, value));
185 macb_writel(macb, MAN, frame);
186
187 do {
188 netstat = macb_readl(macb, NSR);
189 } while (!(netstat & MACB_BIT(IDLE)));
190
191 netctl = macb_readl(macb, NCR);
192 netctl &= ~MACB_BIT(MPE);
193 macb_writel(macb, NCR, netctl);
194}
195
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200196static u16 macb_mdio_read(struct macb_device *macb, u8 phy_adr, u8 reg)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100197{
198 unsigned long netctl;
199 unsigned long netstat;
200 unsigned long frame;
201
202 netctl = macb_readl(macb, NCR);
203 netctl |= MACB_BIT(MPE);
204 macb_writel(macb, NCR, netctl);
205
206 frame = (MACB_BF(SOF, 1)
207 | MACB_BF(RW, 2)
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200208 | MACB_BF(PHYA, phy_adr)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100209 | MACB_BF(REGA, reg)
210 | MACB_BF(CODE, 2));
211 macb_writel(macb, MAN, frame);
212
213 do {
214 netstat = macb_readl(macb, NSR);
215 } while (!(netstat & MACB_BIT(IDLE)));
216
217 frame = macb_readl(macb, MAN);
218
219 netctl = macb_readl(macb, NCR);
220 netctl &= ~MACB_BIT(MPE);
221 macb_writel(macb, NCR, netctl);
222
223 return MACB_BFEXT(DATA, frame);
224}
225
Joe Hershberger1b8c18b2013-06-24 19:06:38 -0500226void __weak arch_get_mdio_control(const char *name)
Shiraz Hashim416ce622012-12-13 17:22:52 +0530227{
228 return;
229}
230
Bo Shenb1a00062013-04-24 15:59:27 +0800231#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Semih Hazar0f751d62009-12-17 15:07:15 +0200232
Joe Hershberger5a49f172016-08-08 11:28:38 -0500233int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
Semih Hazar0f751d62009-12-17 15:07:15 +0200234{
Joe Hershberger5a49f172016-08-08 11:28:38 -0500235 u16 value = 0;
Simon Glassf1dcc192016-05-05 07:28:11 -0600236#ifdef CONFIG_DM_ETH
Joe Hershberger5a49f172016-08-08 11:28:38 -0500237 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600238 struct macb_device *macb = dev_get_priv(dev);
239#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500240 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200241 struct macb_device *macb = to_macb(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -0600242#endif
Semih Hazar0f751d62009-12-17 15:07:15 +0200243
Joe Hershberger5a49f172016-08-08 11:28:38 -0500244 arch_get_mdio_control(bus->name);
Josef Holzmayr7c564082019-10-02 21:22:52 +0200245 value = macb_mdio_read(macb, phy_adr, reg);
Semih Hazar0f751d62009-12-17 15:07:15 +0200246
Joe Hershberger5a49f172016-08-08 11:28:38 -0500247 return value;
Semih Hazar0f751d62009-12-17 15:07:15 +0200248}
249
Joe Hershberger5a49f172016-08-08 11:28:38 -0500250int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
251 u16 value)
Semih Hazar0f751d62009-12-17 15:07:15 +0200252{
Simon Glassf1dcc192016-05-05 07:28:11 -0600253#ifdef CONFIG_DM_ETH
Joe Hershberger5a49f172016-08-08 11:28:38 -0500254 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600255 struct macb_device *macb = dev_get_priv(dev);
256#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500257 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200258 struct macb_device *macb = to_macb(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -0600259#endif
Semih Hazar0f751d62009-12-17 15:07:15 +0200260
Joe Hershberger5a49f172016-08-08 11:28:38 -0500261 arch_get_mdio_control(bus->name);
Josef Holzmayr7c564082019-10-02 21:22:52 +0200262 macb_mdio_write(macb, phy_adr, reg, value);
Semih Hazar0f751d62009-12-17 15:07:15 +0200263
264 return 0;
265}
266#endif
267
Wu, Josh5ae0e382014-05-27 16:31:05 +0800268#define RX 1
269#define TX 0
270static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
271{
272 if (rx)
Heiko Schocher592a7492016-08-29 07:46:11 +0200273 invalidate_dcache_range(macb->rx_ring_dma,
274 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
275 PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800276 else
Heiko Schocher592a7492016-08-29 07:46:11 +0200277 invalidate_dcache_range(macb->tx_ring_dma,
278 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
279 PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800280}
281
282static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
283{
284 if (rx)
285 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200286 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800287 else
288 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200289 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800290}
291
292static inline void macb_flush_rx_buffer(struct macb_device *macb)
293{
294 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Stefan Roese5ccd6572019-08-26 09:18:11 +0200295 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
296 PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800297}
298
299static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
300{
301 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Stefan Roese5ccd6572019-08-26 09:18:11 +0200302 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
303 PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800304}
Semih Hazar0f751d62009-12-17 15:07:15 +0200305
Jon Loeliger07d38a12007-07-09 17:30:01 -0500306#if defined(CONFIG_CMD_NET)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100307
Simon Glassd5555b72016-05-05 07:28:09 -0600308static int _macb_send(struct macb_device *macb, const char *name, void *packet,
309 int length)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100310{
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100311 unsigned long paddr, ctrl;
312 unsigned int tx_head = macb->tx_head;
313 int i;
314
315 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
316
317 ctrl = length & TXBUF_FRMLEN_MASK;
Ramon Fried0a2827e2019-07-16 22:04:33 +0300318 ctrl |= MACB_BIT(TX_LAST);
Andreas Bießmannceef9832014-05-26 22:55:18 +0200319 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300320 ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100321 macb->tx_head = 0;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200322 } else {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100323 macb->tx_head++;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200324 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100325
326 macb->tx_ring[tx_head].ctrl = ctrl;
327 macb->tx_ring[tx_head].addr = paddr;
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200328 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800329 macb_flush_ring_desc(macb, TX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100330 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
331
332 /*
333 * I guess this is necessary because the networking core may
334 * re-use the transmit buffer as soon as we return...
335 */
Andreas Bießmannceef9832014-05-26 22:55:18 +0200336 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200337 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800338 macb_invalidate_ring_desc(macb, TX);
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200339 ctrl = macb->tx_ring[tx_head].ctrl;
Ramon Fried0a2827e2019-07-16 22:04:33 +0300340 if (ctrl & MACB_BIT(TX_USED))
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100341 break;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100342 udelay(1);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100343 }
344
Masahiro Yamada950c5962020-02-14 16:40:18 +0900345 dma_unmap_single(paddr, length, DMA_TO_DEVICE);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100346
Andreas Bießmannceef9832014-05-26 22:55:18 +0200347 if (i <= MACB_TX_TIMEOUT) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300348 if (ctrl & MACB_BIT(TX_UNDERRUN))
Simon Glassd5555b72016-05-05 07:28:09 -0600349 printf("%s: TX underrun\n", name);
Ramon Fried0a2827e2019-07-16 22:04:33 +0300350 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
Simon Glassd5555b72016-05-05 07:28:09 -0600351 printf("%s: TX buffers exhausted in mid frame\n", name);
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200352 } else {
Simon Glassd5555b72016-05-05 07:28:09 -0600353 printf("%s: TX timeout\n", name);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100354 }
355
356 /* No one cares anyway */
357 return 0;
358}
359
360static void reclaim_rx_buffers(struct macb_device *macb,
361 unsigned int new_tail)
362{
363 unsigned int i;
364
365 i = macb->rx_tail;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800366
367 macb_invalidate_ring_desc(macb, RX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100368 while (i > new_tail) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300369 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100370 i++;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200371 if (i > MACB_RX_RING_SIZE)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100372 i = 0;
373 }
374
375 while (i < new_tail) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300376 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100377 i++;
378 }
379
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200380 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800381 macb_flush_ring_desc(macb, RX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100382 macb->rx_tail = new_tail;
383}
384
Simon Glassd5555b72016-05-05 07:28:09 -0600385static int _macb_recv(struct macb_device *macb, uchar **packetp)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100386{
Simon Glassd5555b72016-05-05 07:28:09 -0600387 unsigned int next_rx_tail = macb->next_rx_tail;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100388 void *buffer;
389 int length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100390 u32 status;
391
Simon Glassd5555b72016-05-05 07:28:09 -0600392 macb->wrapped = false;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100393 for (;;) {
Wu, Josh5ae0e382014-05-27 16:31:05 +0800394 macb_invalidate_ring_desc(macb, RX);
395
Ramon Fried0a2827e2019-07-16 22:04:33 +0300396 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
Simon Glassd5555b72016-05-05 07:28:09 -0600397 return -EAGAIN;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100398
Simon Glassd5555b72016-05-05 07:28:09 -0600399 status = macb->rx_ring[next_rx_tail].ctrl;
Ramon Fried0a2827e2019-07-16 22:04:33 +0300400 if (status & MACB_BIT(RX_SOF)) {
Simon Glassd5555b72016-05-05 07:28:09 -0600401 if (next_rx_tail != macb->rx_tail)
402 reclaim_rx_buffers(macb, next_rx_tail);
403 macb->wrapped = false;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100404 }
405
Ramon Fried0a2827e2019-07-16 22:04:33 +0300406 if (status & MACB_BIT(RX_EOF)) {
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300407 buffer = macb->rx_buffer +
408 macb->rx_buffer_size * macb->rx_tail;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100409 length = status & RXBUF_FRMLEN_MASK;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800410
411 macb_invalidate_rx_buffer(macb);
Simon Glassd5555b72016-05-05 07:28:09 -0600412 if (macb->wrapped) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100413 unsigned int headlen, taillen;
414
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300415 headlen = macb->rx_buffer_size *
416 (MACB_RX_RING_SIZE - macb->rx_tail);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100417 taillen = length - headlen;
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500418 memcpy((void *)net_rx_packets[0],
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100419 buffer, headlen);
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500420 memcpy((void *)net_rx_packets[0] + headlen,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100421 macb->rx_buffer, taillen);
Simon Glassd5555b72016-05-05 07:28:09 -0600422 *packetp = (void *)net_rx_packets[0];
423 } else {
424 *packetp = buffer;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100425 }
426
Simon Glassd5555b72016-05-05 07:28:09 -0600427 if (++next_rx_tail >= MACB_RX_RING_SIZE)
428 next_rx_tail = 0;
429 macb->next_rx_tail = next_rx_tail;
430 return length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100431 } else {
Simon Glassd5555b72016-05-05 07:28:09 -0600432 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
433 macb->wrapped = true;
434 next_rx_tail = 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100435 }
436 }
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200437 barrier();
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100438 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100439}
440
Simon Glassd5555b72016-05-05 07:28:09 -0600441static void macb_phy_reset(struct macb_device *macb, const char *name)
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200442{
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200443 int i;
444 u16 status, adv;
445
446 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200447 macb_mdio_write(macb, macb->phy_addr, MII_ADVERTISE, adv);
Simon Glassd5555b72016-05-05 07:28:09 -0600448 printf("%s: Starting autonegotiation...\n", name);
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200449 macb_mdio_write(macb, macb->phy_addr, MII_BMCR, (BMCR_ANENABLE
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200450 | BMCR_ANRESTART));
451
Andreas Bießmannceef9832014-05-26 22:55:18 +0200452 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200453 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200454 if (status & BMSR_ANEGCOMPLETE)
455 break;
456 udelay(100);
457 }
458
459 if (status & BMSR_ANEGCOMPLETE)
Simon Glassd5555b72016-05-05 07:28:09 -0600460 printf("%s: Autonegotiation complete\n", name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200461 else
462 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600463 name, status);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200464}
465
Wenyou Yanga212b662016-05-17 13:11:35 +0800466static int macb_phy_find(struct macb_device *macb, const char *name)
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100467{
468 int i;
469 u16 phy_id;
470
471 /* Search for PHY... */
472 for (i = 0; i < 32; i++) {
473 macb->phy_addr = i;
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200474 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100475 if (phy_id != 0xffff) {
Wenyou Yanga212b662016-05-17 13:11:35 +0800476 printf("%s: PHY present at %d\n", name, i);
Wilson Lee4bf56912017-08-22 20:25:07 -0700477 return 0;
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100478 }
479 }
480
481 /* PHY isn't up to snuff */
Wenyou Yanga212b662016-05-17 13:11:35 +0800482 printf("%s: PHY not found\n", name);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100483
Wilson Lee4bf56912017-08-22 20:25:07 -0700484 return -ENODEV;
485}
486
487/**
488 * macb_linkspd_cb - Linkspeed change callback function
Bin Menga5e3d232019-05-22 00:09:45 -0700489 * @dev/@regs: MACB udevice (DM version) or
490 * Base Register of MACB devices (non-DM version)
Wilson Lee4bf56912017-08-22 20:25:07 -0700491 * @speed: Linkspeed
492 * Returns 0 when operation success and negative errno number
493 * when operation failed.
494 */
Bin Menga5e3d232019-05-22 00:09:45 -0700495#ifdef CONFIG_DM_ETH
Anup Pateld0a04db2019-07-24 04:09:32 +0000496static int macb_sifive_clk_init(struct udevice *dev, ulong rate)
497{
498 fdt_addr_t addr;
499 void *gemgxl_regs;
500
501 addr = dev_read_addr_index(dev, 1);
502 if (addr == FDT_ADDR_T_NONE)
503 return -ENODEV;
504
505 gemgxl_regs = (void __iomem *)addr;
506 if (!gemgxl_regs)
507 return -ENODEV;
508
509 /*
510 * SiFive GEMGXL TX clock operation mode:
511 *
512 * 0 = GMII mode. Use 125 MHz gemgxlclk from PRCI in TX logic
513 * and output clock on GMII output signal GTX_CLK
514 * 1 = MII mode. Use MII input signal TX_CLK in TX logic
515 */
516 writel(rate != 125000000, gemgxl_regs);
517 return 0;
518}
519
Bin Menga5e3d232019-05-22 00:09:45 -0700520int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
521{
Bin Meng3ef64442019-05-22 00:09:46 -0700522#ifdef CONFIG_CLK
Anup Pateld0a04db2019-07-24 04:09:32 +0000523 struct macb_device *macb = dev_get_priv(dev);
Bin Meng3ef64442019-05-22 00:09:46 -0700524 struct clk tx_clk;
525 ulong rate;
526 int ret;
527
Bin Meng3ef64442019-05-22 00:09:46 -0700528 switch (speed) {
529 case _10BASET:
530 rate = 2500000; /* 2.5 MHz */
531 break;
532 case _100BASET:
533 rate = 25000000; /* 25 MHz */
534 break;
535 case _1000BASET:
536 rate = 125000000; /* 125 MHz */
537 break;
538 default:
539 /* does not change anything */
540 return 0;
541 }
542
Anup Pateld0a04db2019-07-24 04:09:32 +0000543 if (macb->config->clk_init)
544 return macb->config->clk_init(dev, rate);
545
546 /*
547 * "tx_clk" is an optional clock source for MACB.
548 * Ignore if it does not exist in DT.
549 */
550 ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
551 if (ret)
552 return 0;
553
Bin Meng3ef64442019-05-22 00:09:46 -0700554 if (tx_clk.dev) {
555 ret = clk_set_rate(&tx_clk, rate);
556 if (ret)
557 return ret;
558 }
559#endif
560
Bin Menga5e3d232019-05-22 00:09:45 -0700561 return 0;
562}
563#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700564int __weak macb_linkspd_cb(void *regs, unsigned int speed)
565{
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100566 return 0;
567}
Bin Menga5e3d232019-05-22 00:09:45 -0700568#endif
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100569
Wenyou Yanga212b662016-05-17 13:11:35 +0800570#ifdef CONFIG_DM_ETH
571static int macb_phy_init(struct udevice *dev, const char *name)
572#else
Simon Glassd5555b72016-05-05 07:28:09 -0600573static int macb_phy_init(struct macb_device *macb, const char *name)
Wenyou Yanga212b662016-05-17 13:11:35 +0800574#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100575{
Wenyou Yanga212b662016-05-17 13:11:35 +0800576#ifdef CONFIG_DM_ETH
577 struct macb_device *macb = dev_get_priv(dev);
578#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100579 u32 ncfgr;
580 u16 phy_id, status, adv, lpa;
581 int media, speed, duplex;
Wilson Lee4bf56912017-08-22 20:25:07 -0700582 int ret;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100583 int i;
584
Simon Glassd5555b72016-05-05 07:28:09 -0600585 arch_get_mdio_control(name);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100586 /* Auto-detect phy_addr */
Wilson Lee4bf56912017-08-22 20:25:07 -0700587 ret = macb_phy_find(macb, name);
588 if (ret)
589 return ret;
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100590
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100591 /* Check if the PHY is up to snuff... */
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200592 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100593 if (phy_id == 0xffff) {
Simon Glassd5555b72016-05-05 07:28:09 -0600594 printf("%s: No PHY present\n", name);
Wilson Lee4bf56912017-08-22 20:25:07 -0700595 return -ENODEV;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100596 }
597
Bo Shenb1a00062013-04-24 15:59:27 +0800598#ifdef CONFIG_PHYLIB
Wenyou Yanga212b662016-05-17 13:11:35 +0800599#ifdef CONFIG_DM_ETH
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800600 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
Wenyou Yanga212b662016-05-17 13:11:35 +0800601 macb->phy_interface);
602#else
Bo Shen8314ccd2013-08-19 10:35:47 +0800603 /* need to consider other phy interface mode */
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800604 macb->phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
Bo Shen8314ccd2013-08-19 10:35:47 +0800605 PHY_INTERFACE_MODE_RGMII);
Wenyou Yanga212b662016-05-17 13:11:35 +0800606#endif
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800607 if (!macb->phydev) {
Bo Shen8314ccd2013-08-19 10:35:47 +0800608 printf("phy_connect failed\n");
609 return -ENODEV;
610 }
611
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800612 phy_config(macb->phydev);
Bo Shenb1a00062013-04-24 15:59:27 +0800613#endif
614
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200615 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100616 if (!(status & BMSR_LSTATUS)) {
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200617 /* Try to re-negotiate if we don't have link already. */
Simon Glassd5555b72016-05-05 07:28:09 -0600618 macb_phy_reset(macb, name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200619
Andreas Bießmannceef9832014-05-26 22:55:18 +0200620 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200621 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
Stefan Roese7bf9bca2019-03-27 11:20:19 +0100622 if (status & BMSR_LSTATUS) {
623 /*
624 * Delay a bit after the link is established,
625 * so that the next xfer does not fail
626 */
627 mdelay(10);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100628 break;
Stefan Roese7bf9bca2019-03-27 11:20:19 +0100629 }
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200630 udelay(100);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100631 }
632 }
633
634 if (!(status & BMSR_LSTATUS)) {
635 printf("%s: link down (status: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600636 name, status);
Wilson Lee4bf56912017-08-22 20:25:07 -0700637 return -ENETDOWN;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100638 }
Bo Shend256be22013-04-24 15:59:28 +0800639
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100640 /* First check for GMAC and that it is GiB capable */
641 if (gem_is_gigabit_capable(macb)) {
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200642 lpa = macb_mdio_read(macb, macb->phy_addr, MII_STAT1000);
Bo Shend256be22013-04-24 15:59:28 +0800643
Radu Pirea0dc97fc2019-06-07 14:18:36 +0300644 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
645 LPA_1000XHALF)) {
646 duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
647 1 : 0);
Andreas Bießmann47609572014-09-18 23:46:48 +0200648
649 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600650 name,
Bo Shend256be22013-04-24 15:59:28 +0800651 duplex ? "full" : "half",
652 lpa);
653
654 ncfgr = macb_readl(macb, NCFGR);
Andreas Bießmann47609572014-09-18 23:46:48 +0200655 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
656 ncfgr |= GEM_BIT(GBE);
657
Bo Shend256be22013-04-24 15:59:28 +0800658 if (duplex)
659 ncfgr |= MACB_BIT(FD);
Andreas Bießmann47609572014-09-18 23:46:48 +0200660
Bo Shend256be22013-04-24 15:59:28 +0800661 macb_writel(macb, NCFGR, ncfgr);
662
Bin Menga5e3d232019-05-22 00:09:45 -0700663#ifdef CONFIG_DM_ETH
664 ret = macb_linkspd_cb(dev, _1000BASET);
665#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700666 ret = macb_linkspd_cb(macb->regs, _1000BASET);
Bin Menga5e3d232019-05-22 00:09:45 -0700667#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700668 if (ret)
669 return ret;
670
671 return 0;
Bo Shend256be22013-04-24 15:59:28 +0800672 }
673 }
674
675 /* fall back for EMAC checking */
Josef Holzmayr0d3044c2019-10-02 21:22:51 +0200676 adv = macb_mdio_read(macb, macb->phy_addr, MII_ADVERTISE);
677 lpa = macb_mdio_read(macb, macb->phy_addr, MII_LPA);
Bo Shend256be22013-04-24 15:59:28 +0800678 media = mii_nway_result(lpa & adv);
679 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
680 ? 1 : 0);
681 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
682 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600683 name,
Bo Shend256be22013-04-24 15:59:28 +0800684 speed ? "100" : "10",
685 duplex ? "full" : "half",
686 lpa);
687
688 ncfgr = macb_readl(macb, NCFGR);
Bo Shenc83cb5f2015-03-04 13:35:16 +0800689 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
Wilson Lee4bf56912017-08-22 20:25:07 -0700690 if (speed) {
Bo Shend256be22013-04-24 15:59:28 +0800691 ncfgr |= MACB_BIT(SPD);
Bin Menga5e3d232019-05-22 00:09:45 -0700692#ifdef CONFIG_DM_ETH
693 ret = macb_linkspd_cb(dev, _100BASET);
694#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700695 ret = macb_linkspd_cb(macb->regs, _100BASET);
Bin Menga5e3d232019-05-22 00:09:45 -0700696#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700697 } else {
Bin Menga5e3d232019-05-22 00:09:45 -0700698#ifdef CONFIG_DM_ETH
699 ret = macb_linkspd_cb(dev, _10BASET);
700#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700701 ret = macb_linkspd_cb(macb->regs, _10BASET);
Bin Menga5e3d232019-05-22 00:09:45 -0700702#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700703 }
704
705 if (ret)
706 return ret;
707
Bo Shend256be22013-04-24 15:59:28 +0800708 if (duplex)
709 ncfgr |= MACB_BIT(FD);
710 macb_writel(macb, NCFGR, ncfgr);
711
Wilson Lee4bf56912017-08-22 20:25:07 -0700712 return 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100713}
714
Wu, Joshade4ea42015-06-03 16:45:44 +0800715static int gmac_init_multi_queues(struct macb_device *macb)
716{
717 int i, num_queues = 1;
718 u32 queue_mask;
719
720 /* bit 0 is never set but queue 0 always exists */
721 queue_mask = gem_readl(macb, DCFG6) & 0xff;
722 queue_mask |= 0x1;
723
724 for (i = 1; i < MACB_MAX_QUEUES; i++)
725 if (queue_mask & (1 << i))
726 num_queues++;
727
Ramon Fried0a2827e2019-07-16 22:04:33 +0300728 macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
Wu, Joshade4ea42015-06-03 16:45:44 +0800729 macb->dummy_desc->addr = 0;
730 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200731 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
Wu, Joshade4ea42015-06-03 16:45:44 +0800732
733 for (i = 1; i < num_queues; i++)
734 gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
735
736 return 0;
737}
738
Ramon Fried9c295802019-07-16 22:04:36 +0300739static void gmac_configure_dma(struct macb_device *macb)
740{
741 u32 buffer_size;
742 u32 dmacfg;
743
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300744 buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
Ramon Fried9c295802019-07-16 22:04:36 +0300745 dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
746 dmacfg |= GEM_BF(RXBS, buffer_size);
747
Anup Pateld0a04db2019-07-24 04:09:32 +0000748 if (macb->config->dma_burst_length)
749 dmacfg = GEM_BFINS(FBLDO,
750 macb->config->dma_burst_length, dmacfg);
Ramon Fried9c295802019-07-16 22:04:36 +0300751
752 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
753 dmacfg &= ~GEM_BIT(ENDIA_PKT);
754
Anup Pateleff0e0c2019-07-24 04:09:37 +0000755 if (macb->is_big_endian)
Ramon Fried9c295802019-07-16 22:04:36 +0300756 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
Anup Pateleff0e0c2019-07-24 04:09:37 +0000757 else
758 dmacfg &= ~GEM_BIT(ENDIA_DESC);
Ramon Fried9c295802019-07-16 22:04:36 +0300759
760 dmacfg &= ~GEM_BIT(ADDR64);
761 gem_writel(macb, DMACFG, dmacfg);
762}
763
Wenyou Yanga212b662016-05-17 13:11:35 +0800764#ifdef CONFIG_DM_ETH
765static int _macb_init(struct udevice *dev, const char *name)
766#else
Simon Glassd5555b72016-05-05 07:28:09 -0600767static int _macb_init(struct macb_device *macb, const char *name)
Wenyou Yanga212b662016-05-17 13:11:35 +0800768#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100769{
Wenyou Yanga212b662016-05-17 13:11:35 +0800770#ifdef CONFIG_DM_ETH
771 struct macb_device *macb = dev_get_priv(dev);
772#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100773 unsigned long paddr;
Wilson Lee4bf56912017-08-22 20:25:07 -0700774 int ret;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100775 int i;
776
777 /*
778 * macb_halt should have been called at some point before now,
779 * so we'll assume the controller is idle.
780 */
781
782 /* initialize DMA descriptors */
783 paddr = macb->rx_buffer_dma;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200784 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
785 if (i == (MACB_RX_RING_SIZE - 1))
Ramon Fried0a2827e2019-07-16 22:04:33 +0300786 paddr |= MACB_BIT(RX_WRAP);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100787 macb->rx_ring[i].addr = paddr;
788 macb->rx_ring[i].ctrl = 0;
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300789 paddr += macb->rx_buffer_size;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100790 }
Wu, Josh5ae0e382014-05-27 16:31:05 +0800791 macb_flush_ring_desc(macb, RX);
792 macb_flush_rx_buffer(macb);
793
Andreas Bießmannceef9832014-05-26 22:55:18 +0200794 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100795 macb->tx_ring[i].addr = 0;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200796 if (i == (MACB_TX_RING_SIZE - 1))
Ramon Fried0a2827e2019-07-16 22:04:33 +0300797 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED) |
798 MACB_BIT(TX_WRAP);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100799 else
Ramon Fried0a2827e2019-07-16 22:04:33 +0300800 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100801 }
Wu, Josh5ae0e382014-05-27 16:31:05 +0800802 macb_flush_ring_desc(macb, TX);
803
Andreas Bießmannceef9832014-05-26 22:55:18 +0200804 macb->rx_tail = 0;
805 macb->tx_head = 0;
806 macb->tx_tail = 0;
Simon Glassd5555b72016-05-05 07:28:09 -0600807 macb->next_rx_tail = 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100808
Wilson Lee4bf56912017-08-22 20:25:07 -0700809#ifdef CONFIG_MACB_ZYNQ
Michal Simek7f6b0f32020-03-26 15:01:29 +0100810 gem_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
Wilson Lee4bf56912017-08-22 20:25:07 -0700811#endif
812
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100813 macb_writel(macb, RBQP, macb->rx_ring_dma);
814 macb_writel(macb, TBQP, macb->tx_ring_dma);
815
Bo Shend256be22013-04-24 15:59:28 +0800816 if (macb_is_gem(macb)) {
Ramon Fried9c295802019-07-16 22:04:36 +0300817 /* Initialize DMA properties */
818 gmac_configure_dma(macb);
Wu, Joshade4ea42015-06-03 16:45:44 +0800819 /* Check the multi queue and initialize the queue for tx */
820 gmac_init_multi_queues(macb);
821
Bo Shencabf61c2014-11-10 15:24:01 +0800822 /*
823 * When the GMAC IP with GE feature, this bit is used to
824 * select interface between RGMII and GMII.
825 * When the GMAC IP without GE feature, this bit is used
826 * to select interface between RMII and MII.
827 */
Wenyou Yanga212b662016-05-17 13:11:35 +0800828#ifdef CONFIG_DM_ETH
Wenyou Yang6de046e2017-04-20 11:13:13 +0800829 if ((macb->phy_interface == PHY_INTERFACE_MODE_RMII) ||
830 (macb->phy_interface == PHY_INTERFACE_MODE_RGMII))
Ramon Fried6c636512019-07-16 22:03:00 +0300831 gem_writel(macb, USRIO, GEM_BIT(RGMII));
Wenyou Yanga212b662016-05-17 13:11:35 +0800832 else
Ramon Fried6c636512019-07-16 22:03:00 +0300833 gem_writel(macb, USRIO, 0);
Ramon Fried5a1899f2019-07-16 22:04:34 +0300834
835 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
836 unsigned int ncfgr = macb_readl(macb, NCFGR);
837
838 ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
839 macb_writel(macb, NCFGR, ncfgr);
840 }
Wenyou Yanga212b662016-05-17 13:11:35 +0800841#else
Bo Shencabf61c2014-11-10 15:24:01 +0800842#if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
Ramon Fried6c636512019-07-16 22:03:00 +0300843 gem_writel(macb, USRIO, GEM_BIT(RGMII));
Bo Shend256be22013-04-24 15:59:28 +0800844#else
Ramon Fried6c636512019-07-16 22:03:00 +0300845 gem_writel(macb, USRIO, 0);
Bo Shend256be22013-04-24 15:59:28 +0800846#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800847#endif
Bo Shend256be22013-04-24 15:59:28 +0800848 } else {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100849 /* choose RMII or MII mode. This depends on the board */
Wenyou Yanga212b662016-05-17 13:11:35 +0800850#ifdef CONFIG_DM_ETH
851#ifdef CONFIG_AT91FAMILY
852 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
853 macb_writel(macb, USRIO,
854 MACB_BIT(RMII) | MACB_BIT(CLKEN));
855 } else {
856 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
857 }
858#else
859 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
860 macb_writel(macb, USRIO, 0);
861 else
862 macb_writel(macb, USRIO, MACB_BIT(MII));
863#endif
864#else
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100865#ifdef CONFIG_RMII
Bo Shend8f64b42013-04-24 15:59:26 +0800866#ifdef CONFIG_AT91FAMILY
Stelian Pop7263ef12008-01-03 21:15:56 +0000867 macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
868#else
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100869 macb_writel(macb, USRIO, 0);
Stelian Pop7263ef12008-01-03 21:15:56 +0000870#endif
871#else
Bo Shend8f64b42013-04-24 15:59:26 +0800872#ifdef CONFIG_AT91FAMILY
Stelian Pop7263ef12008-01-03 21:15:56 +0000873 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100874#else
875 macb_writel(macb, USRIO, MACB_BIT(MII));
876#endif
Stelian Pop7263ef12008-01-03 21:15:56 +0000877#endif /* CONFIG_RMII */
Wenyou Yanga212b662016-05-17 13:11:35 +0800878#endif
Bo Shend256be22013-04-24 15:59:28 +0800879 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100880
Wenyou Yanga212b662016-05-17 13:11:35 +0800881#ifdef CONFIG_DM_ETH
Wilson Lee4bf56912017-08-22 20:25:07 -0700882 ret = macb_phy_init(dev, name);
Wenyou Yanga212b662016-05-17 13:11:35 +0800883#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700884 ret = macb_phy_init(macb, name);
Wenyou Yanga212b662016-05-17 13:11:35 +0800885#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700886 if (ret)
887 return ret;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100888
889 /* Enable TX and RX */
890 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
891
Ben Warren422b1a02008-01-09 18:15:53 -0500892 return 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100893}
894
Simon Glassd5555b72016-05-05 07:28:09 -0600895static void _macb_halt(struct macb_device *macb)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100896{
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100897 u32 ncr, tsr;
898
899 /* Halt the controller and wait for any ongoing transmission to end. */
900 ncr = macb_readl(macb, NCR);
901 ncr |= MACB_BIT(THALT);
902 macb_writel(macb, NCR, ncr);
903
904 do {
905 tsr = macb_readl(macb, TSR);
906 } while (tsr & MACB_BIT(TGO));
907
908 /* Disable TX and RX, and clear statistics */
909 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
910}
911
Simon Glassd5555b72016-05-05 07:28:09 -0600912static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
Ben Warren6bb46792010-06-01 11:55:42 -0700913{
Ben Warren6bb46792010-06-01 11:55:42 -0700914 u32 hwaddr_bottom;
915 u16 hwaddr_top;
916
917 /* set hardware address */
Simon Glassd5555b72016-05-05 07:28:09 -0600918 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
919 enetaddr[2] << 16 | enetaddr[3] << 24;
Ben Warren6bb46792010-06-01 11:55:42 -0700920 macb_writel(macb, SA1B, hwaddr_bottom);
Simon Glassd5555b72016-05-05 07:28:09 -0600921 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
Ben Warren6bb46792010-06-01 11:55:42 -0700922 macb_writel(macb, SA1T, hwaddr_top);
923 return 0;
924}
925
Bo Shend256be22013-04-24 15:59:28 +0800926static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
927{
928 u32 config;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800929#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800930 unsigned long macb_hz = macb->pclk_rate;
931#else
Bo Shend256be22013-04-24 15:59:28 +0800932 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800933#endif
Bo Shend256be22013-04-24 15:59:28 +0800934
935 if (macb_hz < 20000000)
936 config = MACB_BF(CLK, MACB_CLK_DIV8);
937 else if (macb_hz < 40000000)
938 config = MACB_BF(CLK, MACB_CLK_DIV16);
939 else if (macb_hz < 80000000)
940 config = MACB_BF(CLK, MACB_CLK_DIV32);
941 else
942 config = MACB_BF(CLK, MACB_CLK_DIV64);
943
944 return config;
945}
946
947static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
948{
949 u32 config;
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800950
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800951#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800952 unsigned long macb_hz = macb->pclk_rate;
953#else
Bo Shend256be22013-04-24 15:59:28 +0800954 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800955#endif
Bo Shend256be22013-04-24 15:59:28 +0800956
957 if (macb_hz < 20000000)
958 config = GEM_BF(CLK, GEM_CLK_DIV8);
959 else if (macb_hz < 40000000)
960 config = GEM_BF(CLK, GEM_CLK_DIV16);
961 else if (macb_hz < 80000000)
962 config = GEM_BF(CLK, GEM_CLK_DIV32);
963 else if (macb_hz < 120000000)
964 config = GEM_BF(CLK, GEM_CLK_DIV48);
965 else if (macb_hz < 160000000)
966 config = GEM_BF(CLK, GEM_CLK_DIV64);
Ramon Fried9e65f802019-07-16 22:04:32 +0300967 else if (macb_hz < 240000000)
Bo Shend256be22013-04-24 15:59:28 +0800968 config = GEM_BF(CLK, GEM_CLK_DIV96);
Ramon Fried9e65f802019-07-16 22:04:32 +0300969 else if (macb_hz < 320000000)
970 config = GEM_BF(CLK, GEM_CLK_DIV128);
971 else
972 config = GEM_BF(CLK, GEM_CLK_DIV224);
Bo Shend256be22013-04-24 15:59:28 +0800973
974 return config;
975}
976
Bo Shen32e4f6b2013-09-18 15:07:44 +0800977/*
978 * Get the DMA bus width field of the network configuration register that we
979 * should program. We find the width from decoding the design configuration
980 * register to find the maximum supported data bus width.
981 */
982static u32 macb_dbw(struct macb_device *macb)
983{
984 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
985 case 4:
986 return GEM_BF(DBW, GEM_DBW128);
987 case 2:
988 return GEM_BF(DBW, GEM_DBW64);
989 case 1:
990 default:
991 return GEM_BF(DBW, GEM_DBW32);
992 }
993}
994
Simon Glassd5555b72016-05-05 07:28:09 -0600995static void _macb_eth_initialize(struct macb_device *macb)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100996{
Simon Glassd5555b72016-05-05 07:28:09 -0600997 int id = 0; /* This is not used by functions we call */
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100998 u32 ncfgr;
999
Ramon Friedc6d07bf2019-07-14 18:25:14 +03001000 if (macb_is_gem(macb))
1001 macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
1002 else
1003 macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1004
Simon Glassd5555b72016-05-05 07:28:09 -06001005 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
Ramon Friedc6d07bf2019-07-14 18:25:14 +03001006 macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
1007 MACB_RX_RING_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001008 &macb->rx_buffer_dma);
Wu, Josh5ae0e382014-05-27 16:31:05 +08001009 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001010 &macb->rx_ring_dma);
Wu, Josh5ae0e382014-05-27 16:31:05 +08001011 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001012 &macb->tx_ring_dma);
Wu, Joshade4ea42015-06-03 16:45:44 +08001013 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
1014 &macb->dummy_desc_dma);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001015
Simon Glassd5555b72016-05-05 07:28:09 -06001016 /*
1017 * Do some basic initialization so that we at least can talk
1018 * to the PHY
1019 */
1020 if (macb_is_gem(macb)) {
1021 ncfgr = gem_mdc_clk_div(id, macb);
1022 ncfgr |= macb_dbw(macb);
1023 } else {
1024 ncfgr = macb_mdc_clk_div(id, macb);
1025 }
1026
1027 macb_writel(macb, NCFGR, ncfgr);
1028}
1029
Simon Glassf1dcc192016-05-05 07:28:11 -06001030#ifndef CONFIG_DM_ETH
Simon Glassd5555b72016-05-05 07:28:09 -06001031static int macb_send(struct eth_device *netdev, void *packet, int length)
1032{
1033 struct macb_device *macb = to_macb(netdev);
1034
1035 return _macb_send(macb, netdev->name, packet, length);
1036}
1037
1038static int macb_recv(struct eth_device *netdev)
1039{
1040 struct macb_device *macb = to_macb(netdev);
1041 uchar *packet;
1042 int length;
1043
1044 macb->wrapped = false;
1045 for (;;) {
1046 macb->next_rx_tail = macb->rx_tail;
1047 length = _macb_recv(macb, &packet);
1048 if (length >= 0) {
1049 net_process_received_packet(packet, length);
1050 reclaim_rx_buffers(macb, macb->next_rx_tail);
Heinrich Schuchardt6cdf0722018-03-18 11:32:53 +01001051 } else {
Simon Glassd5555b72016-05-05 07:28:09 -06001052 return length;
1053 }
1054 }
1055}
1056
1057static int macb_init(struct eth_device *netdev, bd_t *bd)
1058{
1059 struct macb_device *macb = to_macb(netdev);
1060
1061 return _macb_init(macb, netdev->name);
1062}
1063
1064static void macb_halt(struct eth_device *netdev)
1065{
1066 struct macb_device *macb = to_macb(netdev);
1067
1068 return _macb_halt(macb);
1069}
1070
1071static int macb_write_hwaddr(struct eth_device *netdev)
1072{
1073 struct macb_device *macb = to_macb(netdev);
1074
1075 return _macb_write_hwaddr(macb, netdev->enetaddr);
1076}
1077
1078int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
1079{
1080 struct macb_device *macb;
1081 struct eth_device *netdev;
1082
1083 macb = malloc(sizeof(struct macb_device));
1084 if (!macb) {
1085 printf("Error: Failed to allocate memory for MACB%d\n", id);
1086 return -1;
1087 }
1088 memset(macb, 0, sizeof(struct macb_device));
1089
1090 netdev = &macb->netdev;
Wu, Josh5ae0e382014-05-27 16:31:05 +08001091
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001092 macb->regs = regs;
1093 macb->phy_addr = phy_addr;
1094
Bo Shend256be22013-04-24 15:59:28 +08001095 if (macb_is_gem(macb))
1096 sprintf(netdev->name, "gmac%d", id);
1097 else
1098 sprintf(netdev->name, "macb%d", id);
1099
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001100 netdev->init = macb_init;
1101 netdev->halt = macb_halt;
1102 netdev->send = macb_send;
1103 netdev->recv = macb_recv;
Ben Warren6bb46792010-06-01 11:55:42 -07001104 netdev->write_hwaddr = macb_write_hwaddr;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001105
Simon Glassd5555b72016-05-05 07:28:09 -06001106 _macb_eth_initialize(macb);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001107
1108 eth_register(netdev);
1109
Bo Shenb1a00062013-04-24 15:59:27 +08001110#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Joe Hershberger5a49f172016-08-08 11:28:38 -05001111 int retval;
1112 struct mii_dev *mdiodev = mdio_alloc();
1113 if (!mdiodev)
1114 return -ENOMEM;
1115 strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
1116 mdiodev->read = macb_miiphy_read;
1117 mdiodev->write = macb_miiphy_write;
1118
1119 retval = mdio_register(mdiodev);
1120 if (retval < 0)
1121 return retval;
Bo Shenb1a00062013-04-24 15:59:27 +08001122 macb->bus = miiphy_get_dev_by_name(netdev->name);
Semih Hazar0f751d62009-12-17 15:07:15 +02001123#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001124 return 0;
1125}
Simon Glassf1dcc192016-05-05 07:28:11 -06001126#endif /* !CONFIG_DM_ETH */
1127
1128#ifdef CONFIG_DM_ETH
1129
1130static int macb_start(struct udevice *dev)
1131{
Wenyou Yanga212b662016-05-17 13:11:35 +08001132 return _macb_init(dev, dev->name);
Simon Glassf1dcc192016-05-05 07:28:11 -06001133}
1134
1135static int macb_send(struct udevice *dev, void *packet, int length)
1136{
1137 struct macb_device *macb = dev_get_priv(dev);
1138
1139 return _macb_send(macb, dev->name, packet, length);
1140}
1141
1142static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1143{
1144 struct macb_device *macb = dev_get_priv(dev);
1145
1146 macb->next_rx_tail = macb->rx_tail;
1147 macb->wrapped = false;
1148
1149 return _macb_recv(macb, packetp);
1150}
1151
1152static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1153{
1154 struct macb_device *macb = dev_get_priv(dev);
1155
1156 reclaim_rx_buffers(macb, macb->next_rx_tail);
1157
1158 return 0;
1159}
1160
1161static void macb_stop(struct udevice *dev)
1162{
1163 struct macb_device *macb = dev_get_priv(dev);
1164
1165 _macb_halt(macb);
1166}
1167
1168static int macb_write_hwaddr(struct udevice *dev)
1169{
1170 struct eth_pdata *plat = dev_get_platdata(dev);
1171 struct macb_device *macb = dev_get_priv(dev);
1172
1173 return _macb_write_hwaddr(macb, plat->enetaddr);
1174}
1175
1176static const struct eth_ops macb_eth_ops = {
1177 .start = macb_start,
1178 .send = macb_send,
1179 .recv = macb_recv,
1180 .stop = macb_stop,
1181 .free_pkt = macb_free_pkt,
1182 .write_hwaddr = macb_write_hwaddr,
1183};
1184
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001185#ifdef CONFIG_CLK
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001186static int macb_enable_clk(struct udevice *dev)
1187{
1188 struct macb_device *macb = dev_get_priv(dev);
1189 struct clk clk;
1190 ulong clk_rate;
1191 int ret;
1192
1193 ret = clk_get_by_index(dev, 0, &clk);
1194 if (ret)
1195 return -EINVAL;
1196
Wilson Lee4bf56912017-08-22 20:25:07 -07001197 /*
Anup Patel2e242f52019-02-25 08:14:36 +00001198 * If clock driver didn't support enable or disable then
1199 * we get -ENOSYS from clk_enable(). To handle this, we
1200 * don't fail for ret == -ENOSYS.
Wilson Lee4bf56912017-08-22 20:25:07 -07001201 */
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001202 ret = clk_enable(&clk);
Anup Patel2e242f52019-02-25 08:14:36 +00001203 if (ret && ret != -ENOSYS)
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001204 return ret;
1205
1206 clk_rate = clk_get_rate(&clk);
1207 if (!clk_rate)
1208 return -EINVAL;
1209
1210 macb->pclk_rate = clk_rate;
1211
1212 return 0;
1213}
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001214#endif
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001215
Ramon Frieded3c64f2019-07-16 22:04:35 +03001216static const struct macb_config default_gem_config = {
1217 .dma_burst_length = 16,
Anup Pateld0a04db2019-07-24 04:09:32 +00001218 .clk_init = NULL,
Ramon Frieded3c64f2019-07-16 22:04:35 +03001219};
1220
Simon Glassf1dcc192016-05-05 07:28:11 -06001221static int macb_eth_probe(struct udevice *dev)
1222{
1223 struct eth_pdata *pdata = dev_get_platdata(dev);
1224 struct macb_device *macb = dev_get_priv(dev);
Wenyou Yanga212b662016-05-17 13:11:35 +08001225 const char *phy_mode;
Anup Pateld0a04db2019-07-24 04:09:32 +00001226 int ret;
Wenyou Yanga212b662016-05-17 13:11:35 +08001227
Simon Glasse160f7d2017-01-17 16:52:55 -07001228 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1229 NULL);
Wenyou Yanga212b662016-05-17 13:11:35 +08001230 if (phy_mode)
1231 macb->phy_interface = phy_get_interface_by_name(phy_mode);
1232 if (macb->phy_interface == -1) {
1233 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1234 return -EINVAL;
1235 }
Wenyou Yanga212b662016-05-17 13:11:35 +08001236
Simon Glassf1dcc192016-05-05 07:28:11 -06001237 macb->regs = (void *)pdata->iobase;
1238
Anup Pateleff0e0c2019-07-24 04:09:37 +00001239 macb->is_big_endian = (cpu_to_be32(0x12345678) == 0x12345678);
1240
Anup Pateld0a04db2019-07-24 04:09:32 +00001241 macb->config = (struct macb_config *)dev_get_driver_data(dev);
1242 if (!macb->config)
1243 macb->config = &default_gem_config;
Ramon Frieded3c64f2019-07-16 22:04:35 +03001244
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001245#ifdef CONFIG_CLK
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001246 ret = macb_enable_clk(dev);
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001247 if (ret)
1248 return ret;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001249#endif
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001250
Simon Glassf1dcc192016-05-05 07:28:11 -06001251 _macb_eth_initialize(macb);
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001252
Simon Glassf1dcc192016-05-05 07:28:11 -06001253#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001254 macb->bus = mdio_alloc();
1255 if (!macb->bus)
Joe Hershberger5a49f172016-08-08 11:28:38 -05001256 return -ENOMEM;
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001257 strncpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
1258 macb->bus->read = macb_miiphy_read;
1259 macb->bus->write = macb_miiphy_write;
Joe Hershberger5a49f172016-08-08 11:28:38 -05001260
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001261 ret = mdio_register(macb->bus);
1262 if (ret < 0)
1263 return ret;
Simon Glassf1dcc192016-05-05 07:28:11 -06001264 macb->bus = miiphy_get_dev_by_name(dev->name);
1265#endif
1266
1267 return 0;
1268}
1269
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001270static int macb_eth_remove(struct udevice *dev)
1271{
1272 struct macb_device *macb = dev_get_priv(dev);
1273
1274#ifdef CONFIG_PHYLIB
1275 free(macb->phydev);
1276#endif
1277 mdio_unregister(macb->bus);
1278 mdio_free(macb->bus);
1279
1280 return 0;
1281}
1282
Wilson Lee4bf56912017-08-22 20:25:07 -07001283/**
1284 * macb_late_eth_ofdata_to_platdata
1285 * @dev: udevice struct
1286 * Returns 0 when operation success and negative errno number
1287 * when operation failed.
1288 */
1289int __weak macb_late_eth_ofdata_to_platdata(struct udevice *dev)
1290{
1291 return 0;
1292}
1293
Simon Glassf1dcc192016-05-05 07:28:11 -06001294static int macb_eth_ofdata_to_platdata(struct udevice *dev)
1295{
1296 struct eth_pdata *pdata = dev_get_platdata(dev);
1297
Ramon Fried9043c4e2018-12-27 19:58:42 +02001298 pdata->iobase = (phys_addr_t)dev_remap_addr(dev);
1299 if (!pdata->iobase)
1300 return -EINVAL;
Wilson Lee4bf56912017-08-22 20:25:07 -07001301
1302 return macb_late_eth_ofdata_to_platdata(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -06001303}
1304
Ramon Frieded3c64f2019-07-16 22:04:35 +03001305static const struct macb_config sama5d4_config = {
1306 .dma_burst_length = 4,
Anup Pateld0a04db2019-07-24 04:09:32 +00001307 .clk_init = NULL,
1308};
1309
1310static const struct macb_config sifive_config = {
1311 .dma_burst_length = 16,
1312 .clk_init = macb_sifive_clk_init,
Ramon Frieded3c64f2019-07-16 22:04:35 +03001313};
1314
Simon Glassf1dcc192016-05-05 07:28:11 -06001315static const struct udevice_id macb_eth_ids[] = {
1316 { .compatible = "cdns,macb" },
Wenyou Yang75460252017-04-14 14:36:05 +08001317 { .compatible = "cdns,at91sam9260-macb" },
Nicolas Ferre39fa4162019-09-27 13:08:32 +00001318 { .compatible = "cdns,sam9x60-macb" },
Wenyou Yang75460252017-04-14 14:36:05 +08001319 { .compatible = "atmel,sama5d2-gem" },
1320 { .compatible = "atmel,sama5d3-gem" },
Ramon Frieded3c64f2019-07-16 22:04:35 +03001321 { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
Wilson Lee4bf56912017-08-22 20:25:07 -07001322 { .compatible = "cdns,zynq-gem" },
Anup Pateld0a04db2019-07-24 04:09:32 +00001323 { .compatible = "sifive,fu540-c000-gem",
1324 .data = (ulong)&sifive_config },
Simon Glassf1dcc192016-05-05 07:28:11 -06001325 { }
1326};
1327
1328U_BOOT_DRIVER(eth_macb) = {
1329 .name = "eth_macb",
1330 .id = UCLASS_ETH,
1331 .of_match = macb_eth_ids,
1332 .ofdata_to_platdata = macb_eth_ofdata_to_platdata,
1333 .probe = macb_eth_probe,
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001334 .remove = macb_eth_remove,
Simon Glassf1dcc192016-05-05 07:28:11 -06001335 .ops = &macb_eth_ops,
1336 .priv_auto_alloc_size = sizeof(struct macb_device),
1337 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1338};
1339#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001340
Jon Loeliger07d38a12007-07-09 17:30:01 -05001341#endif