blob: 65ec1f24ad22902e58de478fe99d883b62c2cdba [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01002/*
3 * Copyright (C) 2005-2006 Atmel Corporation
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01004 */
5#include <common.h>
Wenyou Yang3d8d3482016-11-02 10:06:56 +08006#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -07007#include <cpu_func.h>
Simon Glass75c5d182016-05-05 07:28:11 -06008#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060011#include <linux/delay.h>
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010012
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010013/*
14 * The u-boot networking stack is a little weird. It seems like the
15 * networking core allocates receive buffers up front without any
16 * regard to the hardware that's supposed to actually receive those
17 * packets.
18 *
19 * The MACB receives packets into 128-byte receive buffers, so the
20 * buffers allocated by the core isn't very practical to use. We'll
21 * allocate our own, but we need one such buffer in case a packet
22 * wraps around the DMA ring so that we have to copy it.
23 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020024 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010025 * configuration header. This way, the core allocates one RX buffer
26 * and one TX buffer, each of which can hold a ethernet packet of
27 * maximum size.
28 *
29 * For some reason, the networking core unconditionally specifies a
30 * 32-byte packet "alignment" (which really should be called
31 * "padding"). MACB shouldn't need that, but we'll refrain from any
32 * core modifications here...
33 */
34
35#include <net.h>
Simon Glass75c5d182016-05-05 07:28:11 -060036#ifndef CONFIG_DM_ETH
Ben Warren2f2b6b62008-08-31 22:22:04 -070037#include <netdev.h>
Simon Glass75c5d182016-05-05 07:28:11 -060038#endif
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010039#include <malloc.h>
Semih Hazar790088e2009-12-17 15:07:15 +020040#include <miiphy.h>
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010041
42#include <linux/mii.h>
43#include <asm/io.h>
Masahiro Yamada6373a172020-02-14 16:40:19 +090044#include <linux/dma-mapping.h>
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010045#include <asm/arch/clk.h>
Masahiro Yamada64e4f7f2016-09-21 11:28:57 +090046#include <linux/errno.h>
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010047
48#include "macb.h"
49
Wenyou Yang7b811852016-05-17 13:11:35 +080050DECLARE_GLOBAL_DATA_PTR;
51
Ramon Fried377d19d2019-07-14 18:25:14 +030052/*
53 * These buffer sizes must be power of 2 and divisible
54 * by RX_BUFFER_MULTIPLE
55 */
56#define MACB_RX_BUFFER_SIZE 128
57#define GEM_RX_BUFFER_SIZE 2048
Ramon Friedb40501f2019-07-16 22:04:36 +030058#define RX_BUFFER_MULTIPLE 64
Ramon Fried377d19d2019-07-14 18:25:14 +030059
60#define MACB_RX_RING_SIZE 32
Andreas Bießmann1e868122014-05-26 22:55:18 +020061#define MACB_TX_RING_SIZE 16
Ramon Fried377d19d2019-07-14 18:25:14 +030062
Andreas Bießmann1e868122014-05-26 22:55:18 +020063#define MACB_TX_TIMEOUT 1000
64#define MACB_AUTONEG_TIMEOUT 5000000
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010065
Wilson Lee41d6d1e2017-08-22 20:25:07 -070066#ifdef CONFIG_MACB_ZYNQ
67/* INCR4 AHB bursts */
68#define MACB_ZYNQ_GEM_DMACR_BLENGTH 0x00000004
69/* Use full configured addressable space (8 Kb) */
70#define MACB_ZYNQ_GEM_DMACR_RXSIZE 0x00000300
71/* Use full configured addressable space (4 Kb) */
72#define MACB_ZYNQ_GEM_DMACR_TXSIZE 0x00000400
73/* Set RXBUF with use of 128 byte */
74#define MACB_ZYNQ_GEM_DMACR_RXBUF 0x00020000
75#define MACB_ZYNQ_GEM_DMACR_INIT \
76 (MACB_ZYNQ_GEM_DMACR_BLENGTH | \
77 MACB_ZYNQ_GEM_DMACR_RXSIZE | \
78 MACB_ZYNQ_GEM_DMACR_TXSIZE | \
79 MACB_ZYNQ_GEM_DMACR_RXBUF)
80#endif
81
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010082struct macb_dma_desc {
83 u32 addr;
84 u32 ctrl;
85};
86
Padmarao Begari7a2c4962021-01-15 08:20:36 +053087struct macb_dma_desc_64 {
88 u32 addrh;
89 u32 unused;
90};
91
92#define HW_DMA_CAP_32B 0
93#define HW_DMA_CAP_64B 1
94
95#define DMA_DESC_SIZE 16
96#define DMA_DESC_BYTES(n) ((n) * DMA_DESC_SIZE)
Wu, Josh18052402014-05-27 16:31:05 +080097#define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
98#define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
Wu, Josh012d68d2015-06-03 16:45:44 +080099#define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
Wu, Josh18052402014-05-27 16:31:05 +0800100
Yaron Micher1ae088d2022-11-10 19:31:34 +0200101#define DESC_PER_CACHELINE_32 (ARCH_DMA_MINALIGN/sizeof(struct macb_dma_desc))
102#define DESC_PER_CACHELINE_64 (ARCH_DMA_MINALIGN/DMA_DESC_SIZE)
103
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100104#define RXBUF_FRMLEN_MASK 0x00000fff
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100105#define TXBUF_FRMLEN_MASK 0x000007ff
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100106
107struct macb_device {
108 void *regs;
Anup Patel88799a62019-07-24 04:09:32 +0000109
Anup Patela1818b12019-07-24 04:09:37 +0000110 bool is_big_endian;
111
Anup Patel88799a62019-07-24 04:09:32 +0000112 const struct macb_config *config;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100113
114 unsigned int rx_tail;
115 unsigned int tx_head;
116 unsigned int tx_tail;
Simon Glass5ad27512016-05-05 07:28:09 -0600117 unsigned int next_rx_tail;
118 bool wrapped;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100119
120 void *rx_buffer;
121 void *tx_buffer;
122 struct macb_dma_desc *rx_ring;
123 struct macb_dma_desc *tx_ring;
Ramon Fried377d19d2019-07-14 18:25:14 +0300124 size_t rx_buffer_size;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100125
126 unsigned long rx_buffer_dma;
127 unsigned long rx_ring_dma;
128 unsigned long tx_ring_dma;
129
Wu, Josh012d68d2015-06-03 16:45:44 +0800130 struct macb_dma_desc *dummy_desc;
131 unsigned long dummy_desc_dma;
132
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100133 const struct device *dev;
Simon Glass75c5d182016-05-05 07:28:11 -0600134#ifndef CONFIG_DM_ETH
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100135 struct eth_device netdev;
Simon Glass75c5d182016-05-05 07:28:11 -0600136#endif
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100137 unsigned short phy_addr;
Bo Shen7d91deb2013-04-24 15:59:27 +0800138 struct mii_dev *bus;
Wenyou Yang44835ea2017-04-14 14:36:04 +0800139#ifdef CONFIG_PHYLIB
140 struct phy_device *phydev;
141#endif
Wenyou Yang7b811852016-05-17 13:11:35 +0800142
143#ifdef CONFIG_DM_ETH
Wenyou Yang19449362017-02-14 16:24:40 +0800144#ifdef CONFIG_CLK
Wenyou Yang3d8d3482016-11-02 10:06:56 +0800145 unsigned long pclk_rate;
Wenyou Yang19449362017-02-14 16:24:40 +0800146#endif
Wenyou Yang7b811852016-05-17 13:11:35 +0800147 phy_interface_t phy_interface;
148#endif
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100149};
Ramon Fried834040c2019-07-16 22:04:35 +0300150
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200151struct macb_usrio_cfg {
152 unsigned int mii;
153 unsigned int rmii;
154 unsigned int rgmii;
155 unsigned int clken;
156};
157
Ramon Fried834040c2019-07-16 22:04:35 +0300158struct macb_config {
159 unsigned int dma_burst_length;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530160 unsigned int hw_dma_cap;
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200161 unsigned int caps;
Anup Patel88799a62019-07-24 04:09:32 +0000162
163 int (*clk_init)(struct udevice *dev, ulong rate);
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200164 const struct macb_usrio_cfg *usrio;
Ramon Fried834040c2019-07-16 22:04:35 +0300165};
166
Simon Glass75c5d182016-05-05 07:28:11 -0600167#ifndef CONFIG_DM_ETH
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100168#define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
Simon Glass75c5d182016-05-05 07:28:11 -0600169#endif
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100170
Bo Shen6f7d7d92013-04-24 15:59:28 +0800171static int macb_is_gem(struct macb_device *macb)
172{
Atish Patrae1a85182019-02-25 08:14:42 +0000173 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
Bo Shen6f7d7d92013-04-24 15:59:28 +0800174}
175
Gregory CLEMENTf1a1e582015-12-16 14:50:34 +0100176#ifndef cpu_is_sama5d2
177#define cpu_is_sama5d2() 0
178#endif
179
180#ifndef cpu_is_sama5d4
181#define cpu_is_sama5d4() 0
182#endif
183
184static int gem_is_gigabit_capable(struct macb_device *macb)
185{
186 /*
Robert P. J. Day8c60f922016-05-04 04:47:31 -0400187 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
Gregory CLEMENTf1a1e582015-12-16 14:50:34 +0100188 * configured to support only 10/100.
189 */
190 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
191}
192
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200193static void macb_mdio_write(struct macb_device *macb, u8 phy_adr, u8 reg,
194 u16 value)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100195{
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, 1)
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200206 | MACB_BF(PHYA, phy_adr)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100207 | MACB_BF(REGA, reg)
208 | MACB_BF(CODE, 2)
209 | MACB_BF(DATA, value));
210 macb_writel(macb, MAN, frame);
211
212 do {
213 netstat = macb_readl(macb, NSR);
214 } while (!(netstat & MACB_BIT(IDLE)));
215
216 netctl = macb_readl(macb, NCR);
217 netctl &= ~MACB_BIT(MPE);
218 macb_writel(macb, NCR, netctl);
219}
220
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200221static u16 macb_mdio_read(struct macb_device *macb, u8 phy_adr, u8 reg)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100222{
223 unsigned long netctl;
224 unsigned long netstat;
225 unsigned long frame;
226
227 netctl = macb_readl(macb, NCR);
228 netctl |= MACB_BIT(MPE);
229 macb_writel(macb, NCR, netctl);
230
231 frame = (MACB_BF(SOF, 1)
232 | MACB_BF(RW, 2)
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200233 | MACB_BF(PHYA, phy_adr)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100234 | MACB_BF(REGA, reg)
235 | MACB_BF(CODE, 2));
236 macb_writel(macb, MAN, frame);
237
238 do {
239 netstat = macb_readl(macb, NSR);
240 } while (!(netstat & MACB_BIT(IDLE)));
241
242 frame = macb_readl(macb, MAN);
243
244 netctl = macb_readl(macb, NCR);
245 netctl &= ~MACB_BIT(MPE);
246 macb_writel(macb, NCR, netctl);
247
248 return MACB_BFEXT(DATA, frame);
249}
250
Joe Hershberger9e5742b2013-06-24 19:06:38 -0500251void __weak arch_get_mdio_control(const char *name)
Shiraz Hashim77cdf0f2012-12-13 17:22:52 +0530252{
253 return;
254}
255
Bo Shen7d91deb2013-04-24 15:59:27 +0800256#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Semih Hazar790088e2009-12-17 15:07:15 +0200257
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500258int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
Semih Hazar790088e2009-12-17 15:07:15 +0200259{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500260 u16 value = 0;
Simon Glass75c5d182016-05-05 07:28:11 -0600261#ifdef CONFIG_DM_ETH
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500262 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glass75c5d182016-05-05 07:28:11 -0600263 struct macb_device *macb = dev_get_priv(dev);
264#else
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500265 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar790088e2009-12-17 15:07:15 +0200266 struct macb_device *macb = to_macb(dev);
Simon Glass75c5d182016-05-05 07:28:11 -0600267#endif
Semih Hazar790088e2009-12-17 15:07:15 +0200268
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500269 arch_get_mdio_control(bus->name);
Josef Holzmayr017feb52019-10-02 21:22:52 +0200270 value = macb_mdio_read(macb, phy_adr, reg);
Semih Hazar790088e2009-12-17 15:07:15 +0200271
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500272 return value;
Semih Hazar790088e2009-12-17 15:07:15 +0200273}
274
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500275int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
276 u16 value)
Semih Hazar790088e2009-12-17 15:07:15 +0200277{
Simon Glass75c5d182016-05-05 07:28:11 -0600278#ifdef CONFIG_DM_ETH
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500279 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glass75c5d182016-05-05 07:28:11 -0600280 struct macb_device *macb = dev_get_priv(dev);
281#else
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500282 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar790088e2009-12-17 15:07:15 +0200283 struct macb_device *macb = to_macb(dev);
Simon Glass75c5d182016-05-05 07:28:11 -0600284#endif
Semih Hazar790088e2009-12-17 15:07:15 +0200285
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500286 arch_get_mdio_control(bus->name);
Josef Holzmayr017feb52019-10-02 21:22:52 +0200287 macb_mdio_write(macb, phy_adr, reg, value);
Semih Hazar790088e2009-12-17 15:07:15 +0200288
289 return 0;
290}
291#endif
292
Wu, Josh18052402014-05-27 16:31:05 +0800293#define RX 1
294#define TX 0
295static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
296{
297 if (rx)
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200298 invalidate_dcache_range(macb->rx_ring_dma,
299 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
300 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800301 else
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200302 invalidate_dcache_range(macb->tx_ring_dma,
303 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
304 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800305}
306
307static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
308{
309 if (rx)
310 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200311 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800312 else
313 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200314 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800315}
316
317static inline void macb_flush_rx_buffer(struct macb_device *macb)
318{
319 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Stefan Roese7df65a52019-08-26 09:18:11 +0200320 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
321 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800322}
323
324static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
325{
326 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Stefan Roese7df65a52019-08-26 09:18:11 +0200327 ALIGN(macb->rx_buffer_size * MACB_RX_RING_SIZE,
328 PKTALIGN));
Wu, Josh18052402014-05-27 16:31:05 +0800329}
Semih Hazar790088e2009-12-17 15:07:15 +0200330
Jon Loeligerb1d408a2007-07-09 17:30:01 -0500331#if defined(CONFIG_CMD_NET)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100332
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530333static struct macb_dma_desc_64 *macb_64b_desc(struct macb_dma_desc *desc)
334{
335 return (struct macb_dma_desc_64 *)((void *)desc
336 + sizeof(struct macb_dma_desc));
337}
338
339static void macb_set_addr(struct macb_device *macb, struct macb_dma_desc *desc,
340 ulong addr)
341{
342 struct macb_dma_desc_64 *desc_64;
343
344 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
345 desc_64 = macb_64b_desc(desc);
346 desc_64->addrh = upper_32_bits(addr);
347 }
348 desc->addr = lower_32_bits(addr);
349}
350
Simon Glass5ad27512016-05-05 07:28:09 -0600351static int _macb_send(struct macb_device *macb, const char *name, void *packet,
352 int length)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100353{
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100354 unsigned long paddr, ctrl;
355 unsigned int tx_head = macb->tx_head;
356 int i;
357
358 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
359
360 ctrl = length & TXBUF_FRMLEN_MASK;
Ramon Fried6402fb192019-07-16 22:04:33 +0300361 ctrl |= MACB_BIT(TX_LAST);
Andreas Bießmann1e868122014-05-26 22:55:18 +0200362 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
Ramon Fried6402fb192019-07-16 22:04:33 +0300363 ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100364 macb->tx_head = 0;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200365 } else {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100366 macb->tx_head++;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200367 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100368
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530369 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
370 tx_head = tx_head * 2;
371
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100372 macb->tx_ring[tx_head].ctrl = ctrl;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530373 macb_set_addr(macb, &macb->tx_ring[tx_head], paddr);
374
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200375 barrier();
Wu, Josh18052402014-05-27 16:31:05 +0800376 macb_flush_ring_desc(macb, TX);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100377 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
378
379 /*
380 * I guess this is necessary because the networking core may
381 * re-use the transmit buffer as soon as we return...
382 */
Andreas Bießmann1e868122014-05-26 22:55:18 +0200383 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200384 barrier();
Wu, Josh18052402014-05-27 16:31:05 +0800385 macb_invalidate_ring_desc(macb, TX);
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200386 ctrl = macb->tx_ring[tx_head].ctrl;
Ramon Fried6402fb192019-07-16 22:04:33 +0300387 if (ctrl & MACB_BIT(TX_USED))
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100388 break;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100389 udelay(1);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100390 }
391
Masahiro Yamada05a5dba2020-02-14 16:40:18 +0900392 dma_unmap_single(paddr, length, DMA_TO_DEVICE);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100393
Andreas Bießmann1e868122014-05-26 22:55:18 +0200394 if (i <= MACB_TX_TIMEOUT) {
Ramon Fried6402fb192019-07-16 22:04:33 +0300395 if (ctrl & MACB_BIT(TX_UNDERRUN))
Simon Glass5ad27512016-05-05 07:28:09 -0600396 printf("%s: TX underrun\n", name);
Ramon Fried6402fb192019-07-16 22:04:33 +0300397 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
Simon Glass5ad27512016-05-05 07:28:09 -0600398 printf("%s: TX buffers exhausted in mid frame\n", name);
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200399 } else {
Simon Glass5ad27512016-05-05 07:28:09 -0600400 printf("%s: TX timeout\n", name);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100401 }
402
403 /* No one cares anyway */
404 return 0;
405}
406
Yaron Micher1ae088d2022-11-10 19:31:34 +0200407static void reclaim_rx_buffer(struct macb_device *macb,
408 unsigned int idx)
409{
410 unsigned int mask;
411 unsigned int shift;
412 unsigned int i;
413
414 /*
415 * There may be multiple descriptors per CPU cacheline,
416 * so a cache flush would flush the whole line, meaning the content of other descriptors
417 * in the cacheline would also flush. If one of the other descriptors had been
418 * written to by the controller, the flush would cause those changes to be lost.
419 *
420 * To circumvent this issue, we do the actual freeing only when we need to free
421 * the last descriptor in the current cacheline. When the current descriptor is the
422 * last in the cacheline, we free all the descriptors that belong to that cacheline.
423 */
424 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
425 mask = DESC_PER_CACHELINE_64 - 1;
426 shift = 1;
427 } else {
428 mask = DESC_PER_CACHELINE_32 - 1;
429 shift = 0;
430 }
431
432 /* we exit without freeing if idx is not the last descriptor in the cacheline */
433 if ((idx & mask) != mask)
434 return;
435
436 for (i = idx & (~mask); i <= idx; i++)
437 macb->rx_ring[i << shift].addr &= ~MACB_BIT(RX_USED);
438}
439
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100440static void reclaim_rx_buffers(struct macb_device *macb,
441 unsigned int new_tail)
442{
443 unsigned int i;
444
445 i = macb->rx_tail;
Wu, Josh18052402014-05-27 16:31:05 +0800446
447 macb_invalidate_ring_desc(macb, RX);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100448 while (i > new_tail) {
Yaron Micher1ae088d2022-11-10 19:31:34 +0200449 reclaim_rx_buffer(macb, i);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100450 i++;
Yaron Micher1ae088d2022-11-10 19:31:34 +0200451 if (i >= MACB_RX_RING_SIZE)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100452 i = 0;
453 }
454
455 while (i < new_tail) {
Yaron Micher1ae088d2022-11-10 19:31:34 +0200456 reclaim_rx_buffer(macb, i);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100457 i++;
458 }
459
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200460 barrier();
Wu, Josh18052402014-05-27 16:31:05 +0800461 macb_flush_ring_desc(macb, RX);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100462 macb->rx_tail = new_tail;
463}
464
Simon Glass5ad27512016-05-05 07:28:09 -0600465static int _macb_recv(struct macb_device *macb, uchar **packetp)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100466{
Simon Glass5ad27512016-05-05 07:28:09 -0600467 unsigned int next_rx_tail = macb->next_rx_tail;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100468 void *buffer;
469 int length;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100470 u32 status;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530471 u8 flag = false;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100472
Simon Glass5ad27512016-05-05 07:28:09 -0600473 macb->wrapped = false;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100474 for (;;) {
Wu, Josh18052402014-05-27 16:31:05 +0800475 macb_invalidate_ring_desc(macb, RX);
476
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530477 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
478 next_rx_tail = next_rx_tail * 2;
479
Ramon Fried6402fb192019-07-16 22:04:33 +0300480 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
Simon Glass5ad27512016-05-05 07:28:09 -0600481 return -EAGAIN;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100482
Simon Glass5ad27512016-05-05 07:28:09 -0600483 status = macb->rx_ring[next_rx_tail].ctrl;
Ramon Fried6402fb192019-07-16 22:04:33 +0300484 if (status & MACB_BIT(RX_SOF)) {
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530485 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
486 next_rx_tail = next_rx_tail / 2;
487 flag = true;
488 }
489
Simon Glass5ad27512016-05-05 07:28:09 -0600490 if (next_rx_tail != macb->rx_tail)
491 reclaim_rx_buffers(macb, next_rx_tail);
492 macb->wrapped = false;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100493 }
494
Ramon Fried6402fb192019-07-16 22:04:33 +0300495 if (status & MACB_BIT(RX_EOF)) {
Ramon Fried377d19d2019-07-14 18:25:14 +0300496 buffer = macb->rx_buffer +
497 macb->rx_buffer_size * macb->rx_tail;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100498 length = status & RXBUF_FRMLEN_MASK;
Wu, Josh18052402014-05-27 16:31:05 +0800499
500 macb_invalidate_rx_buffer(macb);
Simon Glass5ad27512016-05-05 07:28:09 -0600501 if (macb->wrapped) {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100502 unsigned int headlen, taillen;
503
Ramon Fried377d19d2019-07-14 18:25:14 +0300504 headlen = macb->rx_buffer_size *
505 (MACB_RX_RING_SIZE - macb->rx_tail);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100506 taillen = length - headlen;
Joe Hershberger9f09a362015-04-08 01:41:06 -0500507 memcpy((void *)net_rx_packets[0],
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100508 buffer, headlen);
Joe Hershberger9f09a362015-04-08 01:41:06 -0500509 memcpy((void *)net_rx_packets[0] + headlen,
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100510 macb->rx_buffer, taillen);
Simon Glass5ad27512016-05-05 07:28:09 -0600511 *packetp = (void *)net_rx_packets[0];
512 } else {
513 *packetp = buffer;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100514 }
515
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530516 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
517 if (!flag)
518 next_rx_tail = next_rx_tail / 2;
519 }
520
Simon Glass5ad27512016-05-05 07:28:09 -0600521 if (++next_rx_tail >= MACB_RX_RING_SIZE)
522 next_rx_tail = 0;
523 macb->next_rx_tail = next_rx_tail;
524 return length;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100525 } else {
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530526 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
527 if (!flag)
528 next_rx_tail = next_rx_tail / 2;
529 flag = false;
530 }
531
Simon Glass5ad27512016-05-05 07:28:09 -0600532 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
533 macb->wrapped = true;
534 next_rx_tail = 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100535 }
536 }
Haavard Skinnemoen996e1472007-05-02 13:22:38 +0200537 barrier();
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100538 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100539}
540
Simon Glass5ad27512016-05-05 07:28:09 -0600541static void macb_phy_reset(struct macb_device *macb, const char *name)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100542{
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100543 int i;
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200544 u16 status, adv;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100545
546 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200547 macb_mdio_write(macb, macb->phy_addr, MII_ADVERTISE, adv);
Simon Glass5ad27512016-05-05 07:28:09 -0600548 printf("%s: Starting autonegotiation...\n", name);
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200549 macb_mdio_write(macb, macb->phy_addr, MII_BMCR, (BMCR_ANENABLE
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100550 | BMCR_ANRESTART));
551
Andreas Bießmann1e868122014-05-26 22:55:18 +0200552 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200553 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100554 if (status & BMSR_ANEGCOMPLETE)
555 break;
556 udelay(100);
557 }
558
559 if (status & BMSR_ANEGCOMPLETE)
Simon Glass5ad27512016-05-05 07:28:09 -0600560 printf("%s: Autonegotiation complete\n", name);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100561 else
562 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600563 name, status);
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200564}
565
Wenyou Yang7b811852016-05-17 13:11:35 +0800566static int macb_phy_find(struct macb_device *macb, const char *name)
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100567{
568 int i;
569 u16 phy_id;
570
Padmarao Begari34394ba2021-01-15 08:20:37 +0530571 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
572 if (phy_id != 0xffff) {
573 printf("%s: PHY present at %d\n", name, macb->phy_addr);
574 return 0;
575 }
576
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100577 /* Search for PHY... */
578 for (i = 0; i < 32; i++) {
579 macb->phy_addr = i;
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200580 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100581 if (phy_id != 0xffff) {
Wenyou Yang7b811852016-05-17 13:11:35 +0800582 printf("%s: PHY present at %d\n", name, i);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700583 return 0;
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100584 }
585 }
586
587 /* PHY isn't up to snuff */
Wenyou Yang7b811852016-05-17 13:11:35 +0800588 printf("%s: PHY not found\n", name);
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100589
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700590 return -ENODEV;
591}
592
593/**
594 * macb_linkspd_cb - Linkspeed change callback function
Bin Mengcf821322019-05-22 00:09:45 -0700595 * @dev/@regs: MACB udevice (DM version) or
596 * Base Register of MACB devices (non-DM version)
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700597 * @speed: Linkspeed
598 * Returns 0 when operation success and negative errno number
599 * when operation failed.
600 */
Bin Mengcf821322019-05-22 00:09:45 -0700601#ifdef CONFIG_DM_ETH
Anup Patel88799a62019-07-24 04:09:32 +0000602static int macb_sifive_clk_init(struct udevice *dev, ulong rate)
603{
Anup Patel88799a62019-07-24 04:09:32 +0000604 void *gemgxl_regs;
605
Bin Meng81ce6372021-09-12 11:15:14 +0800606 gemgxl_regs = dev_read_addr_index_ptr(dev, 1);
Anup Patel88799a62019-07-24 04:09:32 +0000607 if (!gemgxl_regs)
608 return -ENODEV;
609
610 /*
611 * SiFive GEMGXL TX clock operation mode:
612 *
613 * 0 = GMII mode. Use 125 MHz gemgxlclk from PRCI in TX logic
614 * and output clock on GMII output signal GTX_CLK
615 * 1 = MII mode. Use MII input signal TX_CLK in TX logic
616 */
617 writel(rate != 125000000, gemgxl_regs);
618 return 0;
619}
620
Claudiu Beznea01ca4eb2021-01-19 13:26:46 +0200621static int macb_sama7g5_clk_init(struct udevice *dev, ulong rate)
622{
623 struct clk clk;
624 int ret;
625
626 ret = clk_get_by_name(dev, "tx_clk", &clk);
627 if (ret)
628 return ret;
629
630 /*
631 * This is for using GCK. Clock rate is addressed via assigned-clock
632 * property, so only clock enable is needed here. The switching to
633 * proper clock rate depending on link speed is managed by IP logic.
634 */
635 return clk_enable(&clk);
636}
637
Bin Mengcf821322019-05-22 00:09:45 -0700638int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
639{
Bin Meng12766ca2019-05-22 00:09:46 -0700640#ifdef CONFIG_CLK
Anup Patel88799a62019-07-24 04:09:32 +0000641 struct macb_device *macb = dev_get_priv(dev);
Bin Meng12766ca2019-05-22 00:09:46 -0700642 struct clk tx_clk;
643 ulong rate;
644 int ret;
645
Bin Meng12766ca2019-05-22 00:09:46 -0700646 switch (speed) {
647 case _10BASET:
648 rate = 2500000; /* 2.5 MHz */
649 break;
650 case _100BASET:
651 rate = 25000000; /* 25 MHz */
652 break;
653 case _1000BASET:
654 rate = 125000000; /* 125 MHz */
655 break;
656 default:
657 /* does not change anything */
658 return 0;
659 }
660
Anup Patel88799a62019-07-24 04:09:32 +0000661 if (macb->config->clk_init)
662 return macb->config->clk_init(dev, rate);
663
664 /*
665 * "tx_clk" is an optional clock source for MACB.
666 * Ignore if it does not exist in DT.
667 */
668 ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
669 if (ret)
670 return 0;
671
Bin Meng12766ca2019-05-22 00:09:46 -0700672 if (tx_clk.dev) {
673 ret = clk_set_rate(&tx_clk, rate);
Claudiu Bezneae2888dc2021-01-19 13:26:45 +0200674 if (ret < 0)
Bin Meng12766ca2019-05-22 00:09:46 -0700675 return ret;
676 }
677#endif
678
Bin Mengcf821322019-05-22 00:09:45 -0700679 return 0;
680}
681#else
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700682int __weak macb_linkspd_cb(void *regs, unsigned int speed)
683{
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100684 return 0;
685}
Bin Mengcf821322019-05-22 00:09:45 -0700686#endif
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100687
Wenyou Yang7b811852016-05-17 13:11:35 +0800688#ifdef CONFIG_DM_ETH
689static int macb_phy_init(struct udevice *dev, const char *name)
690#else
Simon Glass5ad27512016-05-05 07:28:09 -0600691static int macb_phy_init(struct macb_device *macb, const char *name)
Wenyou Yang7b811852016-05-17 13:11:35 +0800692#endif
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200693{
Wenyou Yang7b811852016-05-17 13:11:35 +0800694#ifdef CONFIG_DM_ETH
695 struct macb_device *macb = dev_get_priv(dev);
696#endif
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200697 u32 ncfgr;
698 u16 phy_id, status, adv, lpa;
699 int media, speed, duplex;
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700700 int ret;
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200701 int i;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100702
Simon Glass5ad27512016-05-05 07:28:09 -0600703 arch_get_mdio_control(name);
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100704 /* Auto-detect phy_addr */
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700705 ret = macb_phy_find(macb, name);
706 if (ret)
707 return ret;
Gunnar Rangoy6dd74f32009-01-23 12:56:31 +0100708
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200709 /* Check if the PHY is up to snuff... */
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200710 phy_id = macb_mdio_read(macb, macb->phy_addr, MII_PHYSID1);
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200711 if (phy_id == 0xffff) {
Simon Glass5ad27512016-05-05 07:28:09 -0600712 printf("%s: No PHY present\n", name);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700713 return -ENODEV;
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200714 }
715
Bo Shen7d91deb2013-04-24 15:59:27 +0800716#ifdef CONFIG_PHYLIB
Wenyou Yang7b811852016-05-17 13:11:35 +0800717#ifdef CONFIG_DM_ETH
Wenyou Yang44835ea2017-04-14 14:36:04 +0800718 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
Wenyou Yang7b811852016-05-17 13:11:35 +0800719 macb->phy_interface);
720#else
Bo Shene04fe552013-08-19 10:35:47 +0800721 /* need to consider other phy interface mode */
Wenyou Yang44835ea2017-04-14 14:36:04 +0800722 macb->phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
Bo Shene04fe552013-08-19 10:35:47 +0800723 PHY_INTERFACE_MODE_RGMII);
Wenyou Yang7b811852016-05-17 13:11:35 +0800724#endif
Wenyou Yang44835ea2017-04-14 14:36:04 +0800725 if (!macb->phydev) {
Bo Shene04fe552013-08-19 10:35:47 +0800726 printf("phy_connect failed\n");
727 return -ENODEV;
728 }
729
Wenyou Yang44835ea2017-04-14 14:36:04 +0800730 phy_config(macb->phydev);
Bo Shen7d91deb2013-04-24 15:59:27 +0800731#endif
732
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200733 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100734 if (!(status & BMSR_LSTATUS)) {
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200735 /* Try to re-negotiate if we don't have link already. */
Simon Glass5ad27512016-05-05 07:28:09 -0600736 macb_phy_reset(macb, name);
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200737
Andreas Bießmann1e868122014-05-26 22:55:18 +0200738 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200739 status = macb_mdio_read(macb, macb->phy_addr, MII_BMSR);
Stefan Roese40291802019-03-27 11:20:19 +0100740 if (status & BMSR_LSTATUS) {
741 /*
742 * Delay a bit after the link is established,
743 * so that the next xfer does not fail
744 */
745 mdelay(10);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100746 break;
Stefan Roese40291802019-03-27 11:20:19 +0100747 }
Haavard Skinnemoenb3ad7722007-05-02 13:31:53 +0200748 udelay(100);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100749 }
750 }
751
752 if (!(status & BMSR_LSTATUS)) {
753 printf("%s: link down (status: 0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600754 name, status);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700755 return -ENETDOWN;
Bo Shen6f7d7d92013-04-24 15:59:28 +0800756 }
757
Gregory CLEMENTf1a1e582015-12-16 14:50:34 +0100758 /* First check for GMAC and that it is GiB capable */
759 if (gem_is_gigabit_capable(macb)) {
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200760 lpa = macb_mdio_read(macb, macb->phy_addr, MII_STAT1000);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100761
Radu Pirea1676dfb2019-06-07 14:18:36 +0300762 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
763 LPA_1000XHALF)) {
764 duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
765 1 : 0);
Andreas Bießmannd43a89a2014-09-18 23:46:48 +0200766
767 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600768 name,
Bo Shen6f7d7d92013-04-24 15:59:28 +0800769 duplex ? "full" : "half",
770 lpa);
771
772 ncfgr = macb_readl(macb, NCFGR);
Andreas Bießmannd43a89a2014-09-18 23:46:48 +0200773 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
774 ncfgr |= GEM_BIT(GBE);
775
Bo Shen6f7d7d92013-04-24 15:59:28 +0800776 if (duplex)
777 ncfgr |= MACB_BIT(FD);
Andreas Bießmannd43a89a2014-09-18 23:46:48 +0200778
Bo Shen6f7d7d92013-04-24 15:59:28 +0800779 macb_writel(macb, NCFGR, ncfgr);
780
Bin Mengcf821322019-05-22 00:09:45 -0700781#ifdef CONFIG_DM_ETH
782 ret = macb_linkspd_cb(dev, _1000BASET);
783#else
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700784 ret = macb_linkspd_cb(macb->regs, _1000BASET);
Bin Mengcf821322019-05-22 00:09:45 -0700785#endif
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700786 if (ret)
787 return ret;
788
789 return 0;
Bo Shen6f7d7d92013-04-24 15:59:28 +0800790 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100791 }
Bo Shen6f7d7d92013-04-24 15:59:28 +0800792
793 /* fall back for EMAC checking */
Josef Holzmayr9fe18782019-10-02 21:22:51 +0200794 adv = macb_mdio_read(macb, macb->phy_addr, MII_ADVERTISE);
795 lpa = macb_mdio_read(macb, macb->phy_addr, MII_LPA);
Bo Shen6f7d7d92013-04-24 15:59:28 +0800796 media = mii_nway_result(lpa & adv);
797 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
798 ? 1 : 0);
799 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
800 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
Simon Glass5ad27512016-05-05 07:28:09 -0600801 name,
Bo Shen6f7d7d92013-04-24 15:59:28 +0800802 speed ? "100" : "10",
803 duplex ? "full" : "half",
804 lpa);
805
806 ncfgr = macb_readl(macb, NCFGR);
Bo Shenfe19ef32015-03-04 13:35:16 +0800807 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700808 if (speed) {
Bo Shen6f7d7d92013-04-24 15:59:28 +0800809 ncfgr |= MACB_BIT(SPD);
Bin Mengcf821322019-05-22 00:09:45 -0700810#ifdef CONFIG_DM_ETH
811 ret = macb_linkspd_cb(dev, _100BASET);
812#else
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700813 ret = macb_linkspd_cb(macb->regs, _100BASET);
Bin Mengcf821322019-05-22 00:09:45 -0700814#endif
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700815 } else {
Bin Mengcf821322019-05-22 00:09:45 -0700816#ifdef CONFIG_DM_ETH
817 ret = macb_linkspd_cb(dev, _10BASET);
818#else
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700819 ret = macb_linkspd_cb(macb->regs, _10BASET);
Bin Mengcf821322019-05-22 00:09:45 -0700820#endif
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700821 }
822
823 if (ret)
824 return ret;
825
Bo Shen6f7d7d92013-04-24 15:59:28 +0800826 if (duplex)
827 ncfgr |= MACB_BIT(FD);
828 macb_writel(macb, NCFGR, ncfgr);
829
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700830 return 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100831}
832
Wu, Josh012d68d2015-06-03 16:45:44 +0800833static int gmac_init_multi_queues(struct macb_device *macb)
834{
835 int i, num_queues = 1;
836 u32 queue_mask;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530837 unsigned long paddr;
Wu, Josh012d68d2015-06-03 16:45:44 +0800838
839 /* bit 0 is never set but queue 0 always exists */
840 queue_mask = gem_readl(macb, DCFG6) & 0xff;
841 queue_mask |= 0x1;
842
843 for (i = 1; i < MACB_MAX_QUEUES; i++)
844 if (queue_mask & (1 << i))
845 num_queues++;
846
Ramon Fried6402fb192019-07-16 22:04:33 +0300847 macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
Wu, Josh012d68d2015-06-03 16:45:44 +0800848 macb->dummy_desc->addr = 0;
849 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
Heiko Schocher8353f9d2016-08-29 07:46:11 +0200850 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530851 paddr = macb->dummy_desc_dma;
Wu, Josh012d68d2015-06-03 16:45:44 +0800852
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530853 for (i = 1; i < num_queues; i++) {
854 gem_writel_queue_TBQP(macb, lower_32_bits(paddr), i - 1);
855 gem_writel_queue_RBQP(macb, lower_32_bits(paddr), i - 1);
856 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
857 gem_writel_queue_TBQPH(macb, upper_32_bits(paddr),
858 i - 1);
859 gem_writel_queue_RBQPH(macb, upper_32_bits(paddr),
860 i - 1);
861 }
862 }
Wu, Josh012d68d2015-06-03 16:45:44 +0800863 return 0;
864}
865
Ramon Friedb40501f2019-07-16 22:04:36 +0300866static void gmac_configure_dma(struct macb_device *macb)
867{
868 u32 buffer_size;
869 u32 dmacfg;
870
Ramon Fried377d19d2019-07-14 18:25:14 +0300871 buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
Ramon Friedb40501f2019-07-16 22:04:36 +0300872 dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
873 dmacfg |= GEM_BF(RXBS, buffer_size);
874
Anup Patel88799a62019-07-24 04:09:32 +0000875 if (macb->config->dma_burst_length)
876 dmacfg = GEM_BFINS(FBLDO,
877 macb->config->dma_burst_length, dmacfg);
Ramon Friedb40501f2019-07-16 22:04:36 +0300878
879 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
880 dmacfg &= ~GEM_BIT(ENDIA_PKT);
881
Anup Patela1818b12019-07-24 04:09:37 +0000882 if (macb->is_big_endian)
Ramon Friedb40501f2019-07-16 22:04:36 +0300883 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
Anup Patela1818b12019-07-24 04:09:37 +0000884 else
885 dmacfg &= ~GEM_BIT(ENDIA_DESC);
Ramon Friedb40501f2019-07-16 22:04:36 +0300886
887 dmacfg &= ~GEM_BIT(ADDR64);
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530888 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
889 dmacfg |= GEM_BIT(ADDR64);
890
Ramon Friedb40501f2019-07-16 22:04:36 +0300891 gem_writel(macb, DMACFG, dmacfg);
892}
893
Wenyou Yang7b811852016-05-17 13:11:35 +0800894#ifdef CONFIG_DM_ETH
895static int _macb_init(struct udevice *dev, const char *name)
896#else
Simon Glass5ad27512016-05-05 07:28:09 -0600897static int _macb_init(struct macb_device *macb, const char *name)
Wenyou Yang7b811852016-05-17 13:11:35 +0800898#endif
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100899{
Wenyou Yang7b811852016-05-17 13:11:35 +0800900#ifdef CONFIG_DM_ETH
901 struct macb_device *macb = dev_get_priv(dev);
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200902 unsigned int val = 0;
Wenyou Yang7b811852016-05-17 13:11:35 +0800903#endif
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100904 unsigned long paddr;
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700905 int ret;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100906 int i;
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530907 int count;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100908
909 /*
910 * macb_halt should have been called at some point before now,
911 * so we'll assume the controller is idle.
912 */
913
914 /* initialize DMA descriptors */
915 paddr = macb->rx_buffer_dma;
Andreas Bießmann1e868122014-05-26 22:55:18 +0200916 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
917 if (i == (MACB_RX_RING_SIZE - 1))
Ramon Fried6402fb192019-07-16 22:04:33 +0300918 paddr |= MACB_BIT(RX_WRAP);
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530919 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
920 count = i * 2;
921 else
922 count = i;
923 macb->rx_ring[count].ctrl = 0;
924 macb_set_addr(macb, &macb->rx_ring[count], paddr);
Ramon Fried377d19d2019-07-14 18:25:14 +0300925 paddr += macb->rx_buffer_size;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100926 }
Wu, Josh18052402014-05-27 16:31:05 +0800927 macb_flush_ring_desc(macb, RX);
928 macb_flush_rx_buffer(macb);
929
Andreas Bießmann1e868122014-05-26 22:55:18 +0200930 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530931 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B)
932 count = i * 2;
933 else
934 count = i;
935 macb_set_addr(macb, &macb->tx_ring[count], 0);
Andreas Bießmann1e868122014-05-26 22:55:18 +0200936 if (i == (MACB_TX_RING_SIZE - 1))
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530937 macb->tx_ring[count].ctrl = MACB_BIT(TX_USED) |
Ramon Fried6402fb192019-07-16 22:04:33 +0300938 MACB_BIT(TX_WRAP);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100939 else
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530940 macb->tx_ring[count].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100941 }
Wu, Josh18052402014-05-27 16:31:05 +0800942 macb_flush_ring_desc(macb, TX);
943
Andreas Bießmann1e868122014-05-26 22:55:18 +0200944 macb->rx_tail = 0;
945 macb->tx_head = 0;
946 macb->tx_tail = 0;
Simon Glass5ad27512016-05-05 07:28:09 -0600947 macb->next_rx_tail = 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100948
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700949#ifdef CONFIG_MACB_ZYNQ
Michal Simekfc91bdb2020-03-26 15:01:29 +0100950 gem_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
Wilson Lee41d6d1e2017-08-22 20:25:07 -0700951#endif
952
Padmarao Begari7a2c4962021-01-15 08:20:36 +0530953 macb_writel(macb, RBQP, lower_32_bits(macb->rx_ring_dma));
954 macb_writel(macb, TBQP, lower_32_bits(macb->tx_ring_dma));
955 if (macb->config->hw_dma_cap & HW_DMA_CAP_64B) {
956 macb_writel(macb, RBQPH, upper_32_bits(macb->rx_ring_dma));
957 macb_writel(macb, TBQPH, upper_32_bits(macb->tx_ring_dma));
958 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100959
Bo Shen6f7d7d92013-04-24 15:59:28 +0800960 if (macb_is_gem(macb)) {
Ramon Friedb40501f2019-07-16 22:04:36 +0300961 /* Initialize DMA properties */
962 gmac_configure_dma(macb);
Wu, Josh012d68d2015-06-03 16:45:44 +0800963 /* Check the multi queue and initialize the queue for tx */
964 gmac_init_multi_queues(macb);
965
Bo Shen4660b332014-11-10 15:24:01 +0800966 /*
967 * When the GMAC IP with GE feature, this bit is used to
968 * select interface between RGMII and GMII.
969 * When the GMAC IP without GE feature, this bit is used
970 * to select interface between RMII and MII.
971 */
Wenyou Yang7b811852016-05-17 13:11:35 +0800972#ifdef CONFIG_DM_ETH
Claudiu Bezneaa81146b2021-01-19 13:26:48 +0200973 if (macb->phy_interface == PHY_INTERFACE_MODE_RGMII ||
974 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
975 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
976 macb->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200977 val = macb->config->usrio->rgmii;
978 else if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
979 val = macb->config->usrio->rmii;
980 else if (macb->phy_interface == PHY_INTERFACE_MODE_MII)
981 val = macb->config->usrio->mii;
982
983 if (macb->config->caps & MACB_CAPS_USRIO_HAS_CLKEN)
984 val |= macb->config->usrio->clken;
985
986 gem_writel(macb, USRIO, val);
Ramon Fried588a5b72019-07-16 22:04:34 +0300987
988 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
989 unsigned int ncfgr = macb_readl(macb, NCFGR);
990
991 ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
992 macb_writel(macb, NCFGR, ncfgr);
993 }
Wenyou Yang7b811852016-05-17 13:11:35 +0800994#else
Bo Shen4660b332014-11-10 15:24:01 +0800995#if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
Claudiu Bezneadc17aec2021-01-19 13:26:44 +0200996 gem_writel(macb, USRIO, macb->config->usrio->rgmii);
Bo Shen6f7d7d92013-04-24 15:59:28 +0800997#else
Ramon Fried94e6bd82019-07-16 22:03:00 +0300998 gem_writel(macb, USRIO, 0);
Bo Shen6f7d7d92013-04-24 15:59:28 +0800999#endif
Wenyou Yang7b811852016-05-17 13:11:35 +08001000#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +08001001 } else {
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001002 /* choose RMII or MII mode. This depends on the board */
Wenyou Yang7b811852016-05-17 13:11:35 +08001003#ifdef CONFIG_DM_ETH
1004#ifdef CONFIG_AT91FAMILY
1005 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
1006 macb_writel(macb, USRIO,
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001007 macb->config->usrio->rmii |
1008 macb->config->usrio->clken);
Wenyou Yang7b811852016-05-17 13:11:35 +08001009 } else {
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001010 macb_writel(macb, USRIO, macb->config->usrio->clken);
Wenyou Yang7b811852016-05-17 13:11:35 +08001011 }
1012#else
1013 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
1014 macb_writel(macb, USRIO, 0);
1015 else
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001016 macb_writel(macb, USRIO, macb->config->usrio->mii);
Wenyou Yang7b811852016-05-17 13:11:35 +08001017#endif
1018#else
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001019#ifdef CONFIG_RMII
Bo Shencc29ce52013-04-24 15:59:26 +08001020#ifdef CONFIG_AT91FAMILY
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001021 macb_writel(macb, USRIO, macb->config->usrio->rmii |
1022 macb->config->usrio->clken);
Stelian Pop87a82542008-01-03 21:15:56 +00001023#else
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001024 macb_writel(macb, USRIO, 0);
Stelian Pop87a82542008-01-03 21:15:56 +00001025#endif
1026#else
Bo Shencc29ce52013-04-24 15:59:26 +08001027#ifdef CONFIG_AT91FAMILY
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001028 macb_writel(macb, USRIO, macb->config->usrio->clken);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001029#else
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001030 macb_writel(macb, USRIO, macb->config->usrio->mii);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001031#endif
Stelian Pop87a82542008-01-03 21:15:56 +00001032#endif /* CONFIG_RMII */
Wenyou Yang7b811852016-05-17 13:11:35 +08001033#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +08001034 }
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001035
Wenyou Yang7b811852016-05-17 13:11:35 +08001036#ifdef CONFIG_DM_ETH
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001037 ret = macb_phy_init(dev, name);
Wenyou Yang7b811852016-05-17 13:11:35 +08001038#else
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001039 ret = macb_phy_init(macb, name);
Wenyou Yang7b811852016-05-17 13:11:35 +08001040#endif
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001041 if (ret)
1042 return ret;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001043
1044 /* Enable TX and RX */
1045 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
1046
Ben Warrende9fcb52008-01-09 18:15:53 -05001047 return 0;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001048}
1049
Simon Glass5ad27512016-05-05 07:28:09 -06001050static void _macb_halt(struct macb_device *macb)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001051{
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001052 u32 ncr, tsr;
1053
1054 /* Halt the controller and wait for any ongoing transmission to end. */
1055 ncr = macb_readl(macb, NCR);
1056 ncr |= MACB_BIT(THALT);
1057 macb_writel(macb, NCR, ncr);
1058
1059 do {
1060 tsr = macb_readl(macb, TSR);
1061 } while (tsr & MACB_BIT(TGO));
1062
1063 /* Disable TX and RX, and clear statistics */
1064 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
1065}
1066
Simon Glass5ad27512016-05-05 07:28:09 -06001067static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
Ben Warren33f84312010-06-01 11:55:42 -07001068{
Ben Warren33f84312010-06-01 11:55:42 -07001069 u32 hwaddr_bottom;
1070 u16 hwaddr_top;
1071
1072 /* set hardware address */
Simon Glass5ad27512016-05-05 07:28:09 -06001073 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
1074 enetaddr[2] << 16 | enetaddr[3] << 24;
Ben Warren33f84312010-06-01 11:55:42 -07001075 macb_writel(macb, SA1B, hwaddr_bottom);
Simon Glass5ad27512016-05-05 07:28:09 -06001076 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
Ben Warren33f84312010-06-01 11:55:42 -07001077 macb_writel(macb, SA1T, hwaddr_top);
1078 return 0;
1079}
1080
Bo Shen6f7d7d92013-04-24 15:59:28 +08001081static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
1082{
1083 u32 config;
Wenyou Yang19449362017-02-14 16:24:40 +08001084#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001085 unsigned long macb_hz = macb->pclk_rate;
1086#else
Bo Shen6f7d7d92013-04-24 15:59:28 +08001087 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001088#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +08001089
1090 if (macb_hz < 20000000)
1091 config = MACB_BF(CLK, MACB_CLK_DIV8);
1092 else if (macb_hz < 40000000)
1093 config = MACB_BF(CLK, MACB_CLK_DIV16);
1094 else if (macb_hz < 80000000)
1095 config = MACB_BF(CLK, MACB_CLK_DIV32);
1096 else
1097 config = MACB_BF(CLK, MACB_CLK_DIV64);
1098
1099 return config;
1100}
1101
1102static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
1103{
1104 u32 config;
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001105
Wenyou Yang19449362017-02-14 16:24:40 +08001106#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001107 unsigned long macb_hz = macb->pclk_rate;
1108#else
Bo Shen6f7d7d92013-04-24 15:59:28 +08001109 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001110#endif
Bo Shen6f7d7d92013-04-24 15:59:28 +08001111
1112 if (macb_hz < 20000000)
1113 config = GEM_BF(CLK, GEM_CLK_DIV8);
1114 else if (macb_hz < 40000000)
1115 config = GEM_BF(CLK, GEM_CLK_DIV16);
1116 else if (macb_hz < 80000000)
1117 config = GEM_BF(CLK, GEM_CLK_DIV32);
1118 else if (macb_hz < 120000000)
1119 config = GEM_BF(CLK, GEM_CLK_DIV48);
1120 else if (macb_hz < 160000000)
1121 config = GEM_BF(CLK, GEM_CLK_DIV64);
Ramon Friedb1b9b4f2019-07-16 22:04:32 +03001122 else if (macb_hz < 240000000)
Bo Shen6f7d7d92013-04-24 15:59:28 +08001123 config = GEM_BF(CLK, GEM_CLK_DIV96);
Ramon Friedb1b9b4f2019-07-16 22:04:32 +03001124 else if (macb_hz < 320000000)
1125 config = GEM_BF(CLK, GEM_CLK_DIV128);
1126 else
1127 config = GEM_BF(CLK, GEM_CLK_DIV224);
Bo Shen6f7d7d92013-04-24 15:59:28 +08001128
1129 return config;
1130}
1131
Bo Shen0e6624a2013-09-18 15:07:44 +08001132/*
1133 * Get the DMA bus width field of the network configuration register that we
1134 * should program. We find the width from decoding the design configuration
1135 * register to find the maximum supported data bus width.
1136 */
1137static u32 macb_dbw(struct macb_device *macb)
1138{
1139 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
1140 case 4:
1141 return GEM_BF(DBW, GEM_DBW128);
1142 case 2:
1143 return GEM_BF(DBW, GEM_DBW64);
1144 case 1:
1145 default:
1146 return GEM_BF(DBW, GEM_DBW32);
1147 }
Simon Glass5ad27512016-05-05 07:28:09 -06001148}
1149
1150static void _macb_eth_initialize(struct macb_device *macb)
1151{
1152 int id = 0; /* This is not used by functions we call */
1153 u32 ncfgr;
1154
Ramon Fried377d19d2019-07-14 18:25:14 +03001155 if (macb_is_gem(macb))
1156 macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
1157 else
1158 macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1159
Simon Glass5ad27512016-05-05 07:28:09 -06001160 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
Ramon Fried377d19d2019-07-14 18:25:14 +03001161 macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
1162 MACB_RX_RING_SIZE,
Simon Glass5ad27512016-05-05 07:28:09 -06001163 &macb->rx_buffer_dma);
1164 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
1165 &macb->rx_ring_dma);
1166 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
1167 &macb->tx_ring_dma);
1168 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
1169 &macb->dummy_desc_dma);
1170
1171 /*
1172 * Do some basic initialization so that we at least can talk
1173 * to the PHY
1174 */
1175 if (macb_is_gem(macb)) {
1176 ncfgr = gem_mdc_clk_div(id, macb);
1177 ncfgr |= macb_dbw(macb);
1178 } else {
1179 ncfgr = macb_mdc_clk_div(id, macb);
1180 }
1181
1182 macb_writel(macb, NCFGR, ncfgr);
1183}
1184
Simon Glass75c5d182016-05-05 07:28:11 -06001185#ifndef CONFIG_DM_ETH
Simon Glass5ad27512016-05-05 07:28:09 -06001186static int macb_send(struct eth_device *netdev, void *packet, int length)
1187{
1188 struct macb_device *macb = to_macb(netdev);
1189
1190 return _macb_send(macb, netdev->name, packet, length);
Bo Shen0e6624a2013-09-18 15:07:44 +08001191}
1192
Simon Glass5ad27512016-05-05 07:28:09 -06001193static int macb_recv(struct eth_device *netdev)
1194{
1195 struct macb_device *macb = to_macb(netdev);
1196 uchar *packet;
1197 int length;
1198
1199 macb->wrapped = false;
1200 for (;;) {
1201 macb->next_rx_tail = macb->rx_tail;
1202 length = _macb_recv(macb, &packet);
1203 if (length >= 0) {
1204 net_process_received_packet(packet, length);
1205 reclaim_rx_buffers(macb, macb->next_rx_tail);
Heinrich Schuchardt6c4aae92018-03-18 11:32:53 +01001206 } else {
Simon Glass5ad27512016-05-05 07:28:09 -06001207 return length;
1208 }
1209 }
1210}
1211
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001212static int macb_init(struct eth_device *netdev, struct bd_info *bd)
Simon Glass5ad27512016-05-05 07:28:09 -06001213{
1214 struct macb_device *macb = to_macb(netdev);
1215
1216 return _macb_init(macb, netdev->name);
1217}
1218
1219static void macb_halt(struct eth_device *netdev)
1220{
1221 struct macb_device *macb = to_macb(netdev);
1222
1223 return _macb_halt(macb);
1224}
1225
1226static int macb_write_hwaddr(struct eth_device *netdev)
1227{
1228 struct macb_device *macb = to_macb(netdev);
1229
1230 return _macb_write_hwaddr(macb, netdev->enetaddr);
1231}
1232
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001233int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
1234{
1235 struct macb_device *macb;
1236 struct eth_device *netdev;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001237
1238 macb = malloc(sizeof(struct macb_device));
1239 if (!macb) {
1240 printf("Error: Failed to allocate memory for MACB%d\n", id);
1241 return -1;
1242 }
1243 memset(macb, 0, sizeof(struct macb_device));
1244
1245 netdev = &macb->netdev;
1246
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001247 macb->regs = regs;
1248 macb->phy_addr = phy_addr;
1249
Bo Shen6f7d7d92013-04-24 15:59:28 +08001250 if (macb_is_gem(macb))
1251 sprintf(netdev->name, "gmac%d", id);
1252 else
1253 sprintf(netdev->name, "macb%d", id);
1254
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001255 netdev->init = macb_init;
1256 netdev->halt = macb_halt;
1257 netdev->send = macb_send;
1258 netdev->recv = macb_recv;
Ben Warren33f84312010-06-01 11:55:42 -07001259 netdev->write_hwaddr = macb_write_hwaddr;
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001260
Simon Glass5ad27512016-05-05 07:28:09 -06001261 _macb_eth_initialize(macb);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001262
1263 eth_register(netdev);
1264
Bo Shen7d91deb2013-04-24 15:59:27 +08001265#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -05001266 int retval;
1267 struct mii_dev *mdiodev = mdio_alloc();
1268 if (!mdiodev)
1269 return -ENOMEM;
Vladimir Oltean7947ab42021-09-27 14:21:52 +03001270 strlcpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
Joe Hershberger1fbcbed2016-08-08 11:28:38 -05001271 mdiodev->read = macb_miiphy_read;
1272 mdiodev->write = macb_miiphy_write;
1273
1274 retval = mdio_register(mdiodev);
1275 if (retval < 0)
1276 return retval;
Bo Shen7d91deb2013-04-24 15:59:27 +08001277 macb->bus = miiphy_get_dev_by_name(netdev->name);
Semih Hazar790088e2009-12-17 15:07:15 +02001278#endif
Simon Glass75c5d182016-05-05 07:28:11 -06001279 return 0;
1280}
1281#endif /* !CONFIG_DM_ETH */
1282
1283#ifdef CONFIG_DM_ETH
1284
1285static int macb_start(struct udevice *dev)
1286{
Wenyou Yang7b811852016-05-17 13:11:35 +08001287 return _macb_init(dev, dev->name);
Simon Glass75c5d182016-05-05 07:28:11 -06001288}
1289
1290static int macb_send(struct udevice *dev, void *packet, int length)
1291{
1292 struct macb_device *macb = dev_get_priv(dev);
1293
1294 return _macb_send(macb, dev->name, packet, length);
1295}
1296
1297static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1298{
1299 struct macb_device *macb = dev_get_priv(dev);
1300
1301 macb->next_rx_tail = macb->rx_tail;
1302 macb->wrapped = false;
1303
1304 return _macb_recv(macb, packetp);
1305}
1306
1307static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1308{
1309 struct macb_device *macb = dev_get_priv(dev);
1310
1311 reclaim_rx_buffers(macb, macb->next_rx_tail);
1312
1313 return 0;
1314}
1315
1316static void macb_stop(struct udevice *dev)
1317{
1318 struct macb_device *macb = dev_get_priv(dev);
1319
1320 _macb_halt(macb);
1321}
1322
1323static int macb_write_hwaddr(struct udevice *dev)
1324{
Simon Glassfa20e932020-12-03 16:55:20 -07001325 struct eth_pdata *plat = dev_get_plat(dev);
Simon Glass75c5d182016-05-05 07:28:11 -06001326 struct macb_device *macb = dev_get_priv(dev);
1327
1328 return _macb_write_hwaddr(macb, plat->enetaddr);
1329}
1330
1331static const struct eth_ops macb_eth_ops = {
1332 .start = macb_start,
1333 .send = macb_send,
1334 .recv = macb_recv,
1335 .stop = macb_stop,
1336 .free_pkt = macb_free_pkt,
1337 .write_hwaddr = macb_write_hwaddr,
1338};
1339
Wenyou Yang19449362017-02-14 16:24:40 +08001340#ifdef CONFIG_CLK
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001341static int macb_enable_clk(struct udevice *dev)
1342{
1343 struct macb_device *macb = dev_get_priv(dev);
1344 struct clk clk;
1345 ulong clk_rate;
1346 int ret;
1347
1348 ret = clk_get_by_index(dev, 0, &clk);
1349 if (ret)
1350 return -EINVAL;
1351
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001352 /*
Anup Patel51b51f82019-02-25 08:14:36 +00001353 * If clock driver didn't support enable or disable then
1354 * we get -ENOSYS from clk_enable(). To handle this, we
1355 * don't fail for ret == -ENOSYS.
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001356 */
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001357 ret = clk_enable(&clk);
Anup Patel51b51f82019-02-25 08:14:36 +00001358 if (ret && ret != -ENOSYS)
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001359 return ret;
1360
1361 clk_rate = clk_get_rate(&clk);
1362 if (!clk_rate)
1363 return -EINVAL;
1364
1365 macb->pclk_rate = clk_rate;
1366
1367 return 0;
1368}
Wenyou Yang19449362017-02-14 16:24:40 +08001369#endif
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001370
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001371static const struct macb_usrio_cfg macb_default_usrio = {
1372 .mii = MACB_BIT(MII),
1373 .rmii = MACB_BIT(RMII),
1374 .rgmii = GEM_BIT(RGMII),
1375 .clken = MACB_BIT(CLKEN),
1376};
1377
Padmarao Begari63459cd2021-11-17 18:21:15 +05301378static struct macb_config default_gem_config = {
Ramon Fried834040c2019-07-16 22:04:35 +03001379 .dma_burst_length = 16,
Padmarao Begari7a2c4962021-01-15 08:20:36 +05301380 .hw_dma_cap = HW_DMA_CAP_32B,
Anup Patel88799a62019-07-24 04:09:32 +00001381 .clk_init = NULL,
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001382 .usrio = &macb_default_usrio,
Ramon Fried834040c2019-07-16 22:04:35 +03001383};
1384
Simon Glass75c5d182016-05-05 07:28:11 -06001385static int macb_eth_probe(struct udevice *dev)
1386{
Simon Glassfa20e932020-12-03 16:55:20 -07001387 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass75c5d182016-05-05 07:28:11 -06001388 struct macb_device *macb = dev_get_priv(dev);
Padmarao Begari34394ba2021-01-15 08:20:37 +05301389 struct ofnode_phandle_args phandle_args;
Anup Patel88799a62019-07-24 04:09:32 +00001390 int ret;
Wenyou Yang7b811852016-05-17 13:11:35 +08001391
Marek Behúnbc194772022-04-07 00:33:01 +02001392 macb->phy_interface = dev_read_phy_mode(dev);
Marek Behún48631e42022-04-07 00:33:03 +02001393 if (macb->phy_interface == PHY_INTERFACE_MODE_NA)
Wenyou Yang7b811852016-05-17 13:11:35 +08001394 return -EINVAL;
Wenyou Yang7b811852016-05-17 13:11:35 +08001395
Padmarao Begari34394ba2021-01-15 08:20:37 +05301396 /* Read phyaddr from DT */
1397 if (!dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1398 &phandle_args))
1399 macb->phy_addr = ofnode_read_u32_default(phandle_args.node,
1400 "reg", -1);
1401
Bin Meng81ce6372021-09-12 11:15:14 +08001402 macb->regs = (void *)(uintptr_t)pdata->iobase;
Simon Glass75c5d182016-05-05 07:28:11 -06001403
Anup Patela1818b12019-07-24 04:09:37 +00001404 macb->is_big_endian = (cpu_to_be32(0x12345678) == 0x12345678);
1405
Anup Patel88799a62019-07-24 04:09:32 +00001406 macb->config = (struct macb_config *)dev_get_driver_data(dev);
Padmarao Begari63459cd2021-11-17 18:21:15 +05301407 if (!macb->config) {
1408 if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT)) {
1409 if (GEM_BFEXT(DAW64, gem_readl(macb, DCFG6)))
1410 default_gem_config.hw_dma_cap = HW_DMA_CAP_64B;
1411 }
Anup Patel88799a62019-07-24 04:09:32 +00001412 macb->config = &default_gem_config;
Padmarao Begari63459cd2021-11-17 18:21:15 +05301413 }
Ramon Fried834040c2019-07-16 22:04:35 +03001414
Wenyou Yang19449362017-02-14 16:24:40 +08001415#ifdef CONFIG_CLK
Wenyou Yang44835ea2017-04-14 14:36:04 +08001416 ret = macb_enable_clk(dev);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001417 if (ret)
1418 return ret;
Wenyou Yang19449362017-02-14 16:24:40 +08001419#endif
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001420
Simon Glass75c5d182016-05-05 07:28:11 -06001421 _macb_eth_initialize(macb);
Wenyou Yang3d8d3482016-11-02 10:06:56 +08001422
Simon Glass75c5d182016-05-05 07:28:11 -06001423#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Wenyou Yang44835ea2017-04-14 14:36:04 +08001424 macb->bus = mdio_alloc();
1425 if (!macb->bus)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -05001426 return -ENOMEM;
Vladimir Oltean7947ab42021-09-27 14:21:52 +03001427 strlcpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
Wenyou Yang44835ea2017-04-14 14:36:04 +08001428 macb->bus->read = macb_miiphy_read;
1429 macb->bus->write = macb_miiphy_write;
Joe Hershberger1fbcbed2016-08-08 11:28:38 -05001430
Wenyou Yang44835ea2017-04-14 14:36:04 +08001431 ret = mdio_register(macb->bus);
1432 if (ret < 0)
1433 return ret;
Simon Glass75c5d182016-05-05 07:28:11 -06001434 macb->bus = miiphy_get_dev_by_name(dev->name);
1435#endif
Wenyou Yang44835ea2017-04-14 14:36:04 +08001436
1437 return 0;
1438}
1439
1440static int macb_eth_remove(struct udevice *dev)
1441{
1442 struct macb_device *macb = dev_get_priv(dev);
1443
1444#ifdef CONFIG_PHYLIB
1445 free(macb->phydev);
1446#endif
1447 mdio_unregister(macb->bus);
1448 mdio_free(macb->bus);
Simon Glass75c5d182016-05-05 07:28:11 -06001449
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001450 return 0;
1451}
1452
1453/**
Simon Glassaad29ae2020-12-03 16:55:21 -07001454 * macb_late_eth_of_to_plat
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001455 * @dev: udevice struct
1456 * Returns 0 when operation success and negative errno number
1457 * when operation failed.
1458 */
Simon Glassaad29ae2020-12-03 16:55:21 -07001459int __weak macb_late_eth_of_to_plat(struct udevice *dev)
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001460{
Simon Glass75c5d182016-05-05 07:28:11 -06001461 return 0;
1462}
1463
Simon Glassaad29ae2020-12-03 16:55:21 -07001464static int macb_eth_of_to_plat(struct udevice *dev)
Simon Glass75c5d182016-05-05 07:28:11 -06001465{
Simon Glassfa20e932020-12-03 16:55:20 -07001466 struct eth_pdata *pdata = dev_get_plat(dev);
Simon Glass75c5d182016-05-05 07:28:11 -06001467
Bin Meng81ce6372021-09-12 11:15:14 +08001468 pdata->iobase = (uintptr_t)dev_remap_addr(dev);
Ramon Friedbf15d2f2018-12-27 19:58:42 +02001469 if (!pdata->iobase)
1470 return -EINVAL;
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001471
Simon Glassaad29ae2020-12-03 16:55:21 -07001472 return macb_late_eth_of_to_plat(dev);
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001473}
1474
Claudiu Beznea01ca4eb2021-01-19 13:26:46 +02001475static const struct macb_usrio_cfg sama7g5_usrio = {
1476 .mii = 0,
1477 .rmii = 1,
1478 .rgmii = 2,
1479 .clken = BIT(2),
1480};
1481
Ramon Fried834040c2019-07-16 22:04:35 +03001482static const struct macb_config sama5d4_config = {
1483 .dma_burst_length = 4,
Padmarao Begari7a2c4962021-01-15 08:20:36 +05301484 .hw_dma_cap = HW_DMA_CAP_32B,
Anup Patel88799a62019-07-24 04:09:32 +00001485 .clk_init = NULL,
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001486 .usrio = &macb_default_usrio,
Anup Patel88799a62019-07-24 04:09:32 +00001487};
1488
1489static const struct macb_config sifive_config = {
1490 .dma_burst_length = 16,
Padmarao Begari7a2c4962021-01-15 08:20:36 +05301491 .hw_dma_cap = HW_DMA_CAP_32B,
Anup Patel88799a62019-07-24 04:09:32 +00001492 .clk_init = macb_sifive_clk_init,
Claudiu Bezneadc17aec2021-01-19 13:26:44 +02001493 .usrio = &macb_default_usrio,
Ramon Fried834040c2019-07-16 22:04:35 +03001494};
1495
Claudiu Beznea01ca4eb2021-01-19 13:26:46 +02001496static const struct macb_config sama7g5_gmac_config = {
1497 .dma_burst_length = 16,
1498 .hw_dma_cap = HW_DMA_CAP_32B,
1499 .clk_init = macb_sama7g5_clk_init,
1500 .usrio = &sama7g5_usrio,
1501};
1502
Claudiu Beznea2acf5f82021-01-19 13:26:47 +02001503static const struct macb_config sama7g5_emac_config = {
1504 .caps = MACB_CAPS_USRIO_HAS_CLKEN,
1505 .dma_burst_length = 16,
1506 .hw_dma_cap = HW_DMA_CAP_32B,
1507 .usrio = &sama7g5_usrio,
1508};
1509
Simon Glass75c5d182016-05-05 07:28:11 -06001510static const struct udevice_id macb_eth_ids[] = {
1511 { .compatible = "cdns,macb" },
Wenyou Yang8f155402017-04-14 14:36:05 +08001512 { .compatible = "cdns,at91sam9260-macb" },
Nicolas Ferre9115f572019-09-27 13:08:32 +00001513 { .compatible = "cdns,sam9x60-macb" },
Claudiu Beznea01ca4eb2021-01-19 13:26:46 +02001514 { .compatible = "cdns,sama7g5-gem",
1515 .data = (ulong)&sama7g5_gmac_config },
Claudiu Beznea2acf5f82021-01-19 13:26:47 +02001516 { .compatible = "cdns,sama7g5-emac",
1517 .data = (ulong)&sama7g5_emac_config },
Wenyou Yang8f155402017-04-14 14:36:05 +08001518 { .compatible = "atmel,sama5d2-gem" },
1519 { .compatible = "atmel,sama5d3-gem" },
Ramon Fried834040c2019-07-16 22:04:35 +03001520 { .compatible = "atmel,sama5d4-gem", .data = (ulong)&sama5d4_config },
Wilson Lee41d6d1e2017-08-22 20:25:07 -07001521 { .compatible = "cdns,zynq-gem" },
Anup Patel88799a62019-07-24 04:09:32 +00001522 { .compatible = "sifive,fu540-c000-gem",
1523 .data = (ulong)&sifive_config },
Simon Glass75c5d182016-05-05 07:28:11 -06001524 { }
1525};
1526
1527U_BOOT_DRIVER(eth_macb) = {
1528 .name = "eth_macb",
1529 .id = UCLASS_ETH,
1530 .of_match = macb_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001531 .of_to_plat = macb_eth_of_to_plat,
Simon Glass75c5d182016-05-05 07:28:11 -06001532 .probe = macb_eth_probe,
Wenyou Yang44835ea2017-04-14 14:36:04 +08001533 .remove = macb_eth_remove,
Simon Glass75c5d182016-05-05 07:28:11 -06001534 .ops = &macb_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001535 .priv_auto = sizeof(struct macb_device),
Simon Glass71fa5b42020-12-03 16:55:18 -07001536 .plat_auto = sizeof(struct eth_pdata),
Simon Glass75c5d182016-05-05 07:28:11 -06001537};
1538#endif
1539
Jon Loeligerb1d408a2007-07-09 17:30:01 -05001540#endif