blob: f72d3ade5e0f54a99b57f04ae0a3dde44fb1f670 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01002/*
3 * Copyright (C) 2005-2006 Atmel Corporation
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01004 */
5#include <common.h>
Wenyou Yang577aa3b2016-11-02 10:06:56 +08006#include <clk.h>
Simon Glassf1dcc192016-05-05 07:28:11 -06007#include <dm.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01008
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01009/*
10 * The u-boot networking stack is a little weird. It seems like the
11 * networking core allocates receive buffers up front without any
12 * regard to the hardware that's supposed to actually receive those
13 * packets.
14 *
15 * The MACB receives packets into 128-byte receive buffers, so the
16 * buffers allocated by the core isn't very practical to use. We'll
17 * allocate our own, but we need one such buffer in case a packet
18 * wraps around the DMA ring so that we have to copy it.
19 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020020 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010021 * configuration header. This way, the core allocates one RX buffer
22 * and one TX buffer, each of which can hold a ethernet packet of
23 * maximum size.
24 *
25 * For some reason, the networking core unconditionally specifies a
26 * 32-byte packet "alignment" (which really should be called
27 * "padding"). MACB shouldn't need that, but we'll refrain from any
28 * core modifications here...
29 */
30
31#include <net.h>
Simon Glassf1dcc192016-05-05 07:28:11 -060032#ifndef CONFIG_DM_ETH
Ben Warren89973f82008-08-31 22:22:04 -070033#include <netdev.h>
Simon Glassf1dcc192016-05-05 07:28:11 -060034#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010035#include <malloc.h>
Semih Hazar0f751d62009-12-17 15:07:15 +020036#include <miiphy.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010037
38#include <linux/mii.h>
39#include <asm/io.h>
40#include <asm/dma-mapping.h>
41#include <asm/arch/clk.h>
Masahiro Yamada5d97dff2016-09-21 11:28:57 +090042#include <linux/errno.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010043
44#include "macb.h"
45
Wenyou Yanga212b662016-05-17 13:11:35 +080046DECLARE_GLOBAL_DATA_PTR;
47
Ramon Friedc6d07bf2019-07-14 18:25:14 +030048/*
49 * These buffer sizes must be power of 2 and divisible
50 * by RX_BUFFER_MULTIPLE
51 */
52#define MACB_RX_BUFFER_SIZE 128
53#define GEM_RX_BUFFER_SIZE 2048
Ramon Fried9c295802019-07-16 22:04:36 +030054#define RX_BUFFER_MULTIPLE 64
Ramon Friedc6d07bf2019-07-14 18:25:14 +030055
56#define MACB_RX_RING_SIZE 32
Andreas Bießmannceef9832014-05-26 22:55:18 +020057#define MACB_TX_RING_SIZE 16
Ramon Friedc6d07bf2019-07-14 18:25:14 +030058
Andreas Bießmannceef9832014-05-26 22:55:18 +020059#define MACB_TX_TIMEOUT 1000
60#define MACB_AUTONEG_TIMEOUT 5000000
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010061
Wilson Lee4bf56912017-08-22 20:25:07 -070062#ifdef CONFIG_MACB_ZYNQ
63/* INCR4 AHB bursts */
64#define MACB_ZYNQ_GEM_DMACR_BLENGTH 0x00000004
65/* Use full configured addressable space (8 Kb) */
66#define MACB_ZYNQ_GEM_DMACR_RXSIZE 0x00000300
67/* Use full configured addressable space (4 Kb) */
68#define MACB_ZYNQ_GEM_DMACR_TXSIZE 0x00000400
69/* Set RXBUF with use of 128 byte */
70#define MACB_ZYNQ_GEM_DMACR_RXBUF 0x00020000
71#define MACB_ZYNQ_GEM_DMACR_INIT \
72 (MACB_ZYNQ_GEM_DMACR_BLENGTH | \
73 MACB_ZYNQ_GEM_DMACR_RXSIZE | \
74 MACB_ZYNQ_GEM_DMACR_TXSIZE | \
75 MACB_ZYNQ_GEM_DMACR_RXBUF)
76#endif
77
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010078struct macb_dma_desc {
79 u32 addr;
80 u32 ctrl;
81};
82
Wu, Josh5ae0e382014-05-27 16:31:05 +080083#define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
84#define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
85#define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
Wu, Joshade4ea42015-06-03 16:45:44 +080086#define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
Wu, Josh5ae0e382014-05-27 16:31:05 +080087
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010088#define RXBUF_FRMLEN_MASK 0x00000fff
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010089#define TXBUF_FRMLEN_MASK 0x000007ff
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010090
91struct macb_device {
92 void *regs;
Anup Pateld0a04db2019-07-24 04:09:32 +000093
94 const struct macb_config *config;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010095
96 unsigned int rx_tail;
97 unsigned int tx_head;
98 unsigned int tx_tail;
Simon Glassd5555b72016-05-05 07:28:09 -060099 unsigned int next_rx_tail;
100 bool wrapped;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100101
102 void *rx_buffer;
103 void *tx_buffer;
104 struct macb_dma_desc *rx_ring;
105 struct macb_dma_desc *tx_ring;
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300106 size_t rx_buffer_size;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100107
108 unsigned long rx_buffer_dma;
109 unsigned long rx_ring_dma;
110 unsigned long tx_ring_dma;
111
Wu, Joshade4ea42015-06-03 16:45:44 +0800112 struct macb_dma_desc *dummy_desc;
113 unsigned long dummy_desc_dma;
114
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100115 const struct device *dev;
Simon Glassf1dcc192016-05-05 07:28:11 -0600116#ifndef CONFIG_DM_ETH
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100117 struct eth_device netdev;
Simon Glassf1dcc192016-05-05 07:28:11 -0600118#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100119 unsigned short phy_addr;
Bo Shenb1a00062013-04-24 15:59:27 +0800120 struct mii_dev *bus;
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800121#ifdef CONFIG_PHYLIB
122 struct phy_device *phydev;
123#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800124
125#ifdef CONFIG_DM_ETH
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800126#ifdef CONFIG_CLK
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800127 unsigned long pclk_rate;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800128#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800129 phy_interface_t phy_interface;
130#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100131};
Ramon Frieded3c64f2019-07-16 22:04:35 +0300132
133struct macb_config {
134 unsigned int dma_burst_length;
Anup Pateld0a04db2019-07-24 04:09:32 +0000135
136 int (*clk_init)(struct udevice *dev, ulong rate);
Ramon Frieded3c64f2019-07-16 22:04:35 +0300137};
138
Simon Glassf1dcc192016-05-05 07:28:11 -0600139#ifndef CONFIG_DM_ETH
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100140#define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
Simon Glassf1dcc192016-05-05 07:28:11 -0600141#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100142
Bo Shend256be22013-04-24 15:59:28 +0800143static int macb_is_gem(struct macb_device *macb)
144{
Atish Patrafbcaa262019-02-25 08:14:42 +0000145 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) >= 0x2;
Bo Shend256be22013-04-24 15:59:28 +0800146}
147
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100148#ifndef cpu_is_sama5d2
149#define cpu_is_sama5d2() 0
150#endif
151
152#ifndef cpu_is_sama5d4
153#define cpu_is_sama5d4() 0
154#endif
155
156static int gem_is_gigabit_capable(struct macb_device *macb)
157{
158 /*
Robert P. J. Day1cc0a9f2016-05-04 04:47:31 -0400159 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100160 * configured to support only 10/100.
161 */
162 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
163}
164
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100165static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
166{
167 unsigned long netctl;
168 unsigned long netstat;
169 unsigned long frame;
170
171 netctl = macb_readl(macb, NCR);
172 netctl |= MACB_BIT(MPE);
173 macb_writel(macb, NCR, netctl);
174
175 frame = (MACB_BF(SOF, 1)
176 | MACB_BF(RW, 1)
177 | MACB_BF(PHYA, macb->phy_addr)
178 | MACB_BF(REGA, reg)
179 | MACB_BF(CODE, 2)
180 | MACB_BF(DATA, value));
181 macb_writel(macb, MAN, frame);
182
183 do {
184 netstat = macb_readl(macb, NSR);
185 } while (!(netstat & MACB_BIT(IDLE)));
186
187 netctl = macb_readl(macb, NCR);
188 netctl &= ~MACB_BIT(MPE);
189 macb_writel(macb, NCR, netctl);
190}
191
192static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
193{
194 unsigned long netctl;
195 unsigned long netstat;
196 unsigned long frame;
197
198 netctl = macb_readl(macb, NCR);
199 netctl |= MACB_BIT(MPE);
200 macb_writel(macb, NCR, netctl);
201
202 frame = (MACB_BF(SOF, 1)
203 | MACB_BF(RW, 2)
204 | MACB_BF(PHYA, macb->phy_addr)
205 | MACB_BF(REGA, reg)
206 | MACB_BF(CODE, 2));
207 macb_writel(macb, MAN, frame);
208
209 do {
210 netstat = macb_readl(macb, NSR);
211 } while (!(netstat & MACB_BIT(IDLE)));
212
213 frame = macb_readl(macb, MAN);
214
215 netctl = macb_readl(macb, NCR);
216 netctl &= ~MACB_BIT(MPE);
217 macb_writel(macb, NCR, netctl);
218
219 return MACB_BFEXT(DATA, frame);
220}
221
Joe Hershberger1b8c18b2013-06-24 19:06:38 -0500222void __weak arch_get_mdio_control(const char *name)
Shiraz Hashim416ce622012-12-13 17:22:52 +0530223{
224 return;
225}
226
Bo Shenb1a00062013-04-24 15:59:27 +0800227#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Semih Hazar0f751d62009-12-17 15:07:15 +0200228
Joe Hershberger5a49f172016-08-08 11:28:38 -0500229int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
Semih Hazar0f751d62009-12-17 15:07:15 +0200230{
Joe Hershberger5a49f172016-08-08 11:28:38 -0500231 u16 value = 0;
Simon Glassf1dcc192016-05-05 07:28:11 -0600232#ifdef CONFIG_DM_ETH
Joe Hershberger5a49f172016-08-08 11:28:38 -0500233 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600234 struct macb_device *macb = dev_get_priv(dev);
235#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500236 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200237 struct macb_device *macb = to_macb(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -0600238#endif
Semih Hazar0f751d62009-12-17 15:07:15 +0200239
Andreas Bießmannceef9832014-05-26 22:55:18 +0200240 if (macb->phy_addr != phy_adr)
Semih Hazar0f751d62009-12-17 15:07:15 +0200241 return -1;
242
Joe Hershberger5a49f172016-08-08 11:28:38 -0500243 arch_get_mdio_control(bus->name);
244 value = macb_mdio_read(macb, reg);
Semih Hazar0f751d62009-12-17 15:07:15 +0200245
Joe Hershberger5a49f172016-08-08 11:28:38 -0500246 return value;
Semih Hazar0f751d62009-12-17 15:07:15 +0200247}
248
Joe Hershberger5a49f172016-08-08 11:28:38 -0500249int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
250 u16 value)
Semih Hazar0f751d62009-12-17 15:07:15 +0200251{
Simon Glassf1dcc192016-05-05 07:28:11 -0600252#ifdef CONFIG_DM_ETH
Joe Hershberger5a49f172016-08-08 11:28:38 -0500253 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600254 struct macb_device *macb = dev_get_priv(dev);
255#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500256 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200257 struct macb_device *macb = to_macb(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -0600258#endif
Semih Hazar0f751d62009-12-17 15:07:15 +0200259
Andreas Bießmannceef9832014-05-26 22:55:18 +0200260 if (macb->phy_addr != phy_adr)
Semih Hazar0f751d62009-12-17 15:07:15 +0200261 return -1;
262
Joe Hershberger5a49f172016-08-08 11:28:38 -0500263 arch_get_mdio_control(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200264 macb_mdio_write(macb, reg, value);
265
266 return 0;
267}
268#endif
269
Wu, Josh5ae0e382014-05-27 16:31:05 +0800270#define RX 1
271#define TX 0
272static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
273{
274 if (rx)
Heiko Schocher592a7492016-08-29 07:46:11 +0200275 invalidate_dcache_range(macb->rx_ring_dma,
276 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
277 PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800278 else
Heiko Schocher592a7492016-08-29 07:46:11 +0200279 invalidate_dcache_range(macb->tx_ring_dma,
280 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
281 PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800282}
283
284static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
285{
286 if (rx)
287 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200288 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800289 else
290 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200291 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800292}
293
294static inline void macb_flush_rx_buffer(struct macb_device *macb)
295{
296 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200297 ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800298}
299
300static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
301{
302 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200303 ALIGN(MACB_RX_BUFFER_SIZE, 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);
330 /* Do we need check paddr and length is dcache line aligned? */
Simon Glassf589f8c2016-05-05 07:28:10 -0600331 flush_dcache_range(paddr, paddr + ALIGN(length, ARCH_DMA_MINALIGN));
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100332 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
333
334 /*
335 * I guess this is necessary because the networking core may
336 * re-use the transmit buffer as soon as we return...
337 */
Andreas Bießmannceef9832014-05-26 22:55:18 +0200338 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200339 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800340 macb_invalidate_ring_desc(macb, TX);
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200341 ctrl = macb->tx_ring[tx_head].ctrl;
Ramon Fried0a2827e2019-07-16 22:04:33 +0300342 if (ctrl & MACB_BIT(TX_USED))
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100343 break;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100344 udelay(1);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100345 }
346
347 dma_unmap_single(packet, length, paddr);
348
Andreas Bießmannceef9832014-05-26 22:55:18 +0200349 if (i <= MACB_TX_TIMEOUT) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300350 if (ctrl & MACB_BIT(TX_UNDERRUN))
Simon Glassd5555b72016-05-05 07:28:09 -0600351 printf("%s: TX underrun\n", name);
Ramon Fried0a2827e2019-07-16 22:04:33 +0300352 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
Simon Glassd5555b72016-05-05 07:28:09 -0600353 printf("%s: TX buffers exhausted in mid frame\n", name);
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200354 } else {
Simon Glassd5555b72016-05-05 07:28:09 -0600355 printf("%s: TX timeout\n", name);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100356 }
357
358 /* No one cares anyway */
359 return 0;
360}
361
362static void reclaim_rx_buffers(struct macb_device *macb,
363 unsigned int new_tail)
364{
365 unsigned int i;
366
367 i = macb->rx_tail;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800368
369 macb_invalidate_ring_desc(macb, RX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100370 while (i > new_tail) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300371 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100372 i++;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200373 if (i > MACB_RX_RING_SIZE)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100374 i = 0;
375 }
376
377 while (i < new_tail) {
Ramon Fried0a2827e2019-07-16 22:04:33 +0300378 macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100379 i++;
380 }
381
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200382 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800383 macb_flush_ring_desc(macb, RX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100384 macb->rx_tail = new_tail;
385}
386
Simon Glassd5555b72016-05-05 07:28:09 -0600387static int _macb_recv(struct macb_device *macb, uchar **packetp)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100388{
Simon Glassd5555b72016-05-05 07:28:09 -0600389 unsigned int next_rx_tail = macb->next_rx_tail;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100390 void *buffer;
391 int length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100392 u32 status;
393
Simon Glassd5555b72016-05-05 07:28:09 -0600394 macb->wrapped = false;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100395 for (;;) {
Wu, Josh5ae0e382014-05-27 16:31:05 +0800396 macb_invalidate_ring_desc(macb, RX);
397
Ramon Fried0a2827e2019-07-16 22:04:33 +0300398 if (!(macb->rx_ring[next_rx_tail].addr & MACB_BIT(RX_USED)))
Simon Glassd5555b72016-05-05 07:28:09 -0600399 return -EAGAIN;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100400
Simon Glassd5555b72016-05-05 07:28:09 -0600401 status = macb->rx_ring[next_rx_tail].ctrl;
Ramon Fried0a2827e2019-07-16 22:04:33 +0300402 if (status & MACB_BIT(RX_SOF)) {
Simon Glassd5555b72016-05-05 07:28:09 -0600403 if (next_rx_tail != macb->rx_tail)
404 reclaim_rx_buffers(macb, next_rx_tail);
405 macb->wrapped = false;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100406 }
407
Ramon Fried0a2827e2019-07-16 22:04:33 +0300408 if (status & MACB_BIT(RX_EOF)) {
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300409 buffer = macb->rx_buffer +
410 macb->rx_buffer_size * macb->rx_tail;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100411 length = status & RXBUF_FRMLEN_MASK;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800412
413 macb_invalidate_rx_buffer(macb);
Simon Glassd5555b72016-05-05 07:28:09 -0600414 if (macb->wrapped) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100415 unsigned int headlen, taillen;
416
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300417 headlen = macb->rx_buffer_size *
418 (MACB_RX_RING_SIZE - macb->rx_tail);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100419 taillen = length - headlen;
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500420 memcpy((void *)net_rx_packets[0],
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100421 buffer, headlen);
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500422 memcpy((void *)net_rx_packets[0] + headlen,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100423 macb->rx_buffer, taillen);
Simon Glassd5555b72016-05-05 07:28:09 -0600424 *packetp = (void *)net_rx_packets[0];
425 } else {
426 *packetp = buffer;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100427 }
428
Simon Glassd5555b72016-05-05 07:28:09 -0600429 if (++next_rx_tail >= MACB_RX_RING_SIZE)
430 next_rx_tail = 0;
431 macb->next_rx_tail = next_rx_tail;
432 return length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100433 } else {
Simon Glassd5555b72016-05-05 07:28:09 -0600434 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
435 macb->wrapped = true;
436 next_rx_tail = 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100437 }
438 }
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200439 barrier();
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100440 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100441}
442
Simon Glassd5555b72016-05-05 07:28:09 -0600443static void macb_phy_reset(struct macb_device *macb, const char *name)
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200444{
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200445 int i;
446 u16 status, adv;
447
448 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
449 macb_mdio_write(macb, MII_ADVERTISE, adv);
Simon Glassd5555b72016-05-05 07:28:09 -0600450 printf("%s: Starting autonegotiation...\n", name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200451 macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
452 | BMCR_ANRESTART));
453
Andreas Bießmannceef9832014-05-26 22:55:18 +0200454 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200455 status = macb_mdio_read(macb, MII_BMSR);
456 if (status & BMSR_ANEGCOMPLETE)
457 break;
458 udelay(100);
459 }
460
461 if (status & BMSR_ANEGCOMPLETE)
Simon Glassd5555b72016-05-05 07:28:09 -0600462 printf("%s: Autonegotiation complete\n", name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200463 else
464 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600465 name, status);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200466}
467
Wenyou Yanga212b662016-05-17 13:11:35 +0800468static int macb_phy_find(struct macb_device *macb, const char *name)
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100469{
470 int i;
471 u16 phy_id;
472
473 /* Search for PHY... */
474 for (i = 0; i < 32; i++) {
475 macb->phy_addr = i;
476 phy_id = macb_mdio_read(macb, MII_PHYSID1);
477 if (phy_id != 0xffff) {
Wenyou Yanga212b662016-05-17 13:11:35 +0800478 printf("%s: PHY present at %d\n", name, i);
Wilson Lee4bf56912017-08-22 20:25:07 -0700479 return 0;
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100480 }
481 }
482
483 /* PHY isn't up to snuff */
Wenyou Yanga212b662016-05-17 13:11:35 +0800484 printf("%s: PHY not found\n", name);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100485
Wilson Lee4bf56912017-08-22 20:25:07 -0700486 return -ENODEV;
487}
488
489/**
490 * macb_linkspd_cb - Linkspeed change callback function
Bin Menga5e3d232019-05-22 00:09:45 -0700491 * @dev/@regs: MACB udevice (DM version) or
492 * Base Register of MACB devices (non-DM version)
Wilson Lee4bf56912017-08-22 20:25:07 -0700493 * @speed: Linkspeed
494 * Returns 0 when operation success and negative errno number
495 * when operation failed.
496 */
Bin Menga5e3d232019-05-22 00:09:45 -0700497#ifdef CONFIG_DM_ETH
Anup Pateld0a04db2019-07-24 04:09:32 +0000498static int macb_sifive_clk_init(struct udevice *dev, ulong rate)
499{
500 fdt_addr_t addr;
501 void *gemgxl_regs;
502
503 addr = dev_read_addr_index(dev, 1);
504 if (addr == FDT_ADDR_T_NONE)
505 return -ENODEV;
506
507 gemgxl_regs = (void __iomem *)addr;
508 if (!gemgxl_regs)
509 return -ENODEV;
510
511 /*
512 * SiFive GEMGXL TX clock operation mode:
513 *
514 * 0 = GMII mode. Use 125 MHz gemgxlclk from PRCI in TX logic
515 * and output clock on GMII output signal GTX_CLK
516 * 1 = MII mode. Use MII input signal TX_CLK in TX logic
517 */
518 writel(rate != 125000000, gemgxl_regs);
519 return 0;
520}
521
Bin Menga5e3d232019-05-22 00:09:45 -0700522int __weak macb_linkspd_cb(struct udevice *dev, unsigned int speed)
523{
Bin Meng3ef64442019-05-22 00:09:46 -0700524#ifdef CONFIG_CLK
Anup Pateld0a04db2019-07-24 04:09:32 +0000525 struct macb_device *macb = dev_get_priv(dev);
Bin Meng3ef64442019-05-22 00:09:46 -0700526 struct clk tx_clk;
527 ulong rate;
528 int ret;
529
Bin Meng3ef64442019-05-22 00:09:46 -0700530 switch (speed) {
531 case _10BASET:
532 rate = 2500000; /* 2.5 MHz */
533 break;
534 case _100BASET:
535 rate = 25000000; /* 25 MHz */
536 break;
537 case _1000BASET:
538 rate = 125000000; /* 125 MHz */
539 break;
540 default:
541 /* does not change anything */
542 return 0;
543 }
544
Anup Pateld0a04db2019-07-24 04:09:32 +0000545 if (macb->config->clk_init)
546 return macb->config->clk_init(dev, rate);
547
548 /*
549 * "tx_clk" is an optional clock source for MACB.
550 * Ignore if it does not exist in DT.
551 */
552 ret = clk_get_by_name(dev, "tx_clk", &tx_clk);
553 if (ret)
554 return 0;
555
Bin Meng3ef64442019-05-22 00:09:46 -0700556 if (tx_clk.dev) {
557 ret = clk_set_rate(&tx_clk, rate);
558 if (ret)
559 return ret;
560 }
561#endif
562
Bin Menga5e3d232019-05-22 00:09:45 -0700563 return 0;
564}
565#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700566int __weak macb_linkspd_cb(void *regs, unsigned int speed)
567{
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100568 return 0;
569}
Bin Menga5e3d232019-05-22 00:09:45 -0700570#endif
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100571
Wenyou Yanga212b662016-05-17 13:11:35 +0800572#ifdef CONFIG_DM_ETH
573static int macb_phy_init(struct udevice *dev, const char *name)
574#else
Simon Glassd5555b72016-05-05 07:28:09 -0600575static int macb_phy_init(struct macb_device *macb, const char *name)
Wenyou Yanga212b662016-05-17 13:11:35 +0800576#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100577{
Wenyou Yanga212b662016-05-17 13:11:35 +0800578#ifdef CONFIG_DM_ETH
579 struct macb_device *macb = dev_get_priv(dev);
580#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100581 u32 ncfgr;
582 u16 phy_id, status, adv, lpa;
583 int media, speed, duplex;
Wilson Lee4bf56912017-08-22 20:25:07 -0700584 int ret;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100585 int i;
586
Simon Glassd5555b72016-05-05 07:28:09 -0600587 arch_get_mdio_control(name);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100588 /* Auto-detect phy_addr */
Wilson Lee4bf56912017-08-22 20:25:07 -0700589 ret = macb_phy_find(macb, name);
590 if (ret)
591 return ret;
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100592
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100593 /* Check if the PHY is up to snuff... */
594 phy_id = macb_mdio_read(macb, MII_PHYSID1);
595 if (phy_id == 0xffff) {
Simon Glassd5555b72016-05-05 07:28:09 -0600596 printf("%s: No PHY present\n", name);
Wilson Lee4bf56912017-08-22 20:25:07 -0700597 return -ENODEV;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100598 }
599
Bo Shenb1a00062013-04-24 15:59:27 +0800600#ifdef CONFIG_PHYLIB
Wenyou Yanga212b662016-05-17 13:11:35 +0800601#ifdef CONFIG_DM_ETH
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800602 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
Wenyou Yanga212b662016-05-17 13:11:35 +0800603 macb->phy_interface);
604#else
Bo Shen8314ccd2013-08-19 10:35:47 +0800605 /* need to consider other phy interface mode */
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800606 macb->phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
Bo Shen8314ccd2013-08-19 10:35:47 +0800607 PHY_INTERFACE_MODE_RGMII);
Wenyou Yanga212b662016-05-17 13:11:35 +0800608#endif
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800609 if (!macb->phydev) {
Bo Shen8314ccd2013-08-19 10:35:47 +0800610 printf("phy_connect failed\n");
611 return -ENODEV;
612 }
613
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800614 phy_config(macb->phydev);
Bo Shenb1a00062013-04-24 15:59:27 +0800615#endif
616
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200617 status = macb_mdio_read(macb, MII_BMSR);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100618 if (!(status & BMSR_LSTATUS)) {
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200619 /* Try to re-negotiate if we don't have link already. */
Simon Glassd5555b72016-05-05 07:28:09 -0600620 macb_phy_reset(macb, name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200621
Andreas Bießmannceef9832014-05-26 22:55:18 +0200622 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100623 status = macb_mdio_read(macb, MII_BMSR);
Stefan Roese7bf9bca2019-03-27 11:20:19 +0100624 if (status & BMSR_LSTATUS) {
625 /*
626 * Delay a bit after the link is established,
627 * so that the next xfer does not fail
628 */
629 mdelay(10);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100630 break;
Stefan Roese7bf9bca2019-03-27 11:20:19 +0100631 }
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200632 udelay(100);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100633 }
634 }
635
636 if (!(status & BMSR_LSTATUS)) {
637 printf("%s: link down (status: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600638 name, status);
Wilson Lee4bf56912017-08-22 20:25:07 -0700639 return -ENETDOWN;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100640 }
Bo Shend256be22013-04-24 15:59:28 +0800641
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100642 /* First check for GMAC and that it is GiB capable */
643 if (gem_is_gigabit_capable(macb)) {
Radu Pirea1b0c9912019-06-07 14:18:35 +0300644 lpa = macb_mdio_read(macb, MII_LPA);
Bo Shend256be22013-04-24 15:59:28 +0800645
Radu Pirea0dc97fc2019-06-07 14:18:36 +0300646 if (lpa & (LPA_1000FULL | LPA_1000HALF | LPA_1000XFULL |
647 LPA_1000XHALF)) {
648 duplex = ((lpa & (LPA_1000FULL | LPA_1000XFULL)) ?
649 1 : 0);
Andreas Bießmann47609572014-09-18 23:46:48 +0200650
651 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600652 name,
Bo Shend256be22013-04-24 15:59:28 +0800653 duplex ? "full" : "half",
654 lpa);
655
656 ncfgr = macb_readl(macb, NCFGR);
Andreas Bießmann47609572014-09-18 23:46:48 +0200657 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
658 ncfgr |= GEM_BIT(GBE);
659
Bo Shend256be22013-04-24 15:59:28 +0800660 if (duplex)
661 ncfgr |= MACB_BIT(FD);
Andreas Bießmann47609572014-09-18 23:46:48 +0200662
Bo Shend256be22013-04-24 15:59:28 +0800663 macb_writel(macb, NCFGR, ncfgr);
664
Bin Menga5e3d232019-05-22 00:09:45 -0700665#ifdef CONFIG_DM_ETH
666 ret = macb_linkspd_cb(dev, _1000BASET);
667#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700668 ret = macb_linkspd_cb(macb->regs, _1000BASET);
Bin Menga5e3d232019-05-22 00:09:45 -0700669#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700670 if (ret)
671 return ret;
672
673 return 0;
Bo Shend256be22013-04-24 15:59:28 +0800674 }
675 }
676
677 /* fall back for EMAC checking */
678 adv = macb_mdio_read(macb, MII_ADVERTISE);
679 lpa = macb_mdio_read(macb, MII_LPA);
680 media = mii_nway_result(lpa & adv);
681 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
682 ? 1 : 0);
683 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
684 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600685 name,
Bo Shend256be22013-04-24 15:59:28 +0800686 speed ? "100" : "10",
687 duplex ? "full" : "half",
688 lpa);
689
690 ncfgr = macb_readl(macb, NCFGR);
Bo Shenc83cb5f2015-03-04 13:35:16 +0800691 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
Wilson Lee4bf56912017-08-22 20:25:07 -0700692 if (speed) {
Bo Shend256be22013-04-24 15:59:28 +0800693 ncfgr |= MACB_BIT(SPD);
Bin Menga5e3d232019-05-22 00:09:45 -0700694#ifdef CONFIG_DM_ETH
695 ret = macb_linkspd_cb(dev, _100BASET);
696#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700697 ret = macb_linkspd_cb(macb->regs, _100BASET);
Bin Menga5e3d232019-05-22 00:09:45 -0700698#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700699 } else {
Bin Menga5e3d232019-05-22 00:09:45 -0700700#ifdef CONFIG_DM_ETH
701 ret = macb_linkspd_cb(dev, _10BASET);
702#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700703 ret = macb_linkspd_cb(macb->regs, _10BASET);
Bin Menga5e3d232019-05-22 00:09:45 -0700704#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700705 }
706
707 if (ret)
708 return ret;
709
Bo Shend256be22013-04-24 15:59:28 +0800710 if (duplex)
711 ncfgr |= MACB_BIT(FD);
712 macb_writel(macb, NCFGR, ncfgr);
713
Wilson Lee4bf56912017-08-22 20:25:07 -0700714 return 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100715}
716
Wu, Joshade4ea42015-06-03 16:45:44 +0800717static int gmac_init_multi_queues(struct macb_device *macb)
718{
719 int i, num_queues = 1;
720 u32 queue_mask;
721
722 /* bit 0 is never set but queue 0 always exists */
723 queue_mask = gem_readl(macb, DCFG6) & 0xff;
724 queue_mask |= 0x1;
725
726 for (i = 1; i < MACB_MAX_QUEUES; i++)
727 if (queue_mask & (1 << i))
728 num_queues++;
729
Ramon Fried0a2827e2019-07-16 22:04:33 +0300730 macb->dummy_desc->ctrl = MACB_BIT(TX_USED);
Wu, Joshade4ea42015-06-03 16:45:44 +0800731 macb->dummy_desc->addr = 0;
732 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200733 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
Wu, Joshade4ea42015-06-03 16:45:44 +0800734
735 for (i = 1; i < num_queues; i++)
736 gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
737
738 return 0;
739}
740
Ramon Fried9c295802019-07-16 22:04:36 +0300741static void gmac_configure_dma(struct macb_device *macb)
742{
743 u32 buffer_size;
744 u32 dmacfg;
745
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300746 buffer_size = macb->rx_buffer_size / RX_BUFFER_MULTIPLE;
Ramon Fried9c295802019-07-16 22:04:36 +0300747 dmacfg = gem_readl(macb, DMACFG) & ~GEM_BF(RXBS, -1L);
748 dmacfg |= GEM_BF(RXBS, buffer_size);
749
Anup Pateld0a04db2019-07-24 04:09:32 +0000750 if (macb->config->dma_burst_length)
751 dmacfg = GEM_BFINS(FBLDO,
752 macb->config->dma_burst_length, dmacfg);
Ramon Fried9c295802019-07-16 22:04:36 +0300753
754 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
755 dmacfg &= ~GEM_BIT(ENDIA_PKT);
756
757#ifdef CONFIG_SYS_LITTLE_ENDIAN
758 dmacfg &= ~GEM_BIT(ENDIA_DESC);
759#else
760 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
761#endif
762
763 dmacfg &= ~GEM_BIT(ADDR64);
764 gem_writel(macb, DMACFG, dmacfg);
765}
766
Wenyou Yanga212b662016-05-17 13:11:35 +0800767#ifdef CONFIG_DM_ETH
768static int _macb_init(struct udevice *dev, const char *name)
769#else
Simon Glassd5555b72016-05-05 07:28:09 -0600770static int _macb_init(struct macb_device *macb, const char *name)
Wenyou Yanga212b662016-05-17 13:11:35 +0800771#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100772{
Wenyou Yanga212b662016-05-17 13:11:35 +0800773#ifdef CONFIG_DM_ETH
774 struct macb_device *macb = dev_get_priv(dev);
775#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100776 unsigned long paddr;
Wilson Lee4bf56912017-08-22 20:25:07 -0700777 int ret;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100778 int i;
779
780 /*
781 * macb_halt should have been called at some point before now,
782 * so we'll assume the controller is idle.
783 */
784
785 /* initialize DMA descriptors */
786 paddr = macb->rx_buffer_dma;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200787 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
788 if (i == (MACB_RX_RING_SIZE - 1))
Ramon Fried0a2827e2019-07-16 22:04:33 +0300789 paddr |= MACB_BIT(RX_WRAP);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100790 macb->rx_ring[i].addr = paddr;
791 macb->rx_ring[i].ctrl = 0;
Ramon Friedc6d07bf2019-07-14 18:25:14 +0300792 paddr += macb->rx_buffer_size;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100793 }
Wu, Josh5ae0e382014-05-27 16:31:05 +0800794 macb_flush_ring_desc(macb, RX);
795 macb_flush_rx_buffer(macb);
796
Andreas Bießmannceef9832014-05-26 22:55:18 +0200797 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100798 macb->tx_ring[i].addr = 0;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200799 if (i == (MACB_TX_RING_SIZE - 1))
Ramon Fried0a2827e2019-07-16 22:04:33 +0300800 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED) |
801 MACB_BIT(TX_WRAP);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100802 else
Ramon Fried0a2827e2019-07-16 22:04:33 +0300803 macb->tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100804 }
Wu, Josh5ae0e382014-05-27 16:31:05 +0800805 macb_flush_ring_desc(macb, TX);
806
Andreas Bießmannceef9832014-05-26 22:55:18 +0200807 macb->rx_tail = 0;
808 macb->tx_head = 0;
809 macb->tx_tail = 0;
Simon Glassd5555b72016-05-05 07:28:09 -0600810 macb->next_rx_tail = 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100811
Wilson Lee4bf56912017-08-22 20:25:07 -0700812#ifdef CONFIG_MACB_ZYNQ
813 macb_writel(macb, DMACFG, MACB_ZYNQ_GEM_DMACR_INIT);
814#endif
815
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100816 macb_writel(macb, RBQP, macb->rx_ring_dma);
817 macb_writel(macb, TBQP, macb->tx_ring_dma);
818
Bo Shend256be22013-04-24 15:59:28 +0800819 if (macb_is_gem(macb)) {
Ramon Fried9c295802019-07-16 22:04:36 +0300820 /* Initialize DMA properties */
821 gmac_configure_dma(macb);
Wu, Joshade4ea42015-06-03 16:45:44 +0800822 /* Check the multi queue and initialize the queue for tx */
823 gmac_init_multi_queues(macb);
824
Bo Shencabf61c2014-11-10 15:24:01 +0800825 /*
826 * When the GMAC IP with GE feature, this bit is used to
827 * select interface between RGMII and GMII.
828 * When the GMAC IP without GE feature, this bit is used
829 * to select interface between RMII and MII.
830 */
Wenyou Yanga212b662016-05-17 13:11:35 +0800831#ifdef CONFIG_DM_ETH
Wenyou Yang6de046e2017-04-20 11:13:13 +0800832 if ((macb->phy_interface == PHY_INTERFACE_MODE_RMII) ||
833 (macb->phy_interface == PHY_INTERFACE_MODE_RGMII))
Ramon Fried6c636512019-07-16 22:03:00 +0300834 gem_writel(macb, USRIO, GEM_BIT(RGMII));
Wenyou Yanga212b662016-05-17 13:11:35 +0800835 else
Ramon Fried6c636512019-07-16 22:03:00 +0300836 gem_writel(macb, USRIO, 0);
Ramon Fried5a1899f2019-07-16 22:04:34 +0300837
838 if (macb->phy_interface == PHY_INTERFACE_MODE_SGMII) {
839 unsigned int ncfgr = macb_readl(macb, NCFGR);
840
841 ncfgr |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
842 macb_writel(macb, NCFGR, ncfgr);
843 }
Wenyou Yanga212b662016-05-17 13:11:35 +0800844#else
Bo Shencabf61c2014-11-10 15:24:01 +0800845#if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
Ramon Fried6c636512019-07-16 22:03:00 +0300846 gem_writel(macb, USRIO, GEM_BIT(RGMII));
Bo Shend256be22013-04-24 15:59:28 +0800847#else
Ramon Fried6c636512019-07-16 22:03:00 +0300848 gem_writel(macb, USRIO, 0);
Bo Shend256be22013-04-24 15:59:28 +0800849#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800850#endif
Bo Shend256be22013-04-24 15:59:28 +0800851 } else {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100852 /* choose RMII or MII mode. This depends on the board */
Wenyou Yanga212b662016-05-17 13:11:35 +0800853#ifdef CONFIG_DM_ETH
854#ifdef CONFIG_AT91FAMILY
855 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
856 macb_writel(macb, USRIO,
857 MACB_BIT(RMII) | MACB_BIT(CLKEN));
858 } else {
859 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
860 }
861#else
862 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
863 macb_writel(macb, USRIO, 0);
864 else
865 macb_writel(macb, USRIO, MACB_BIT(MII));
866#endif
867#else
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100868#ifdef CONFIG_RMII
Bo Shend8f64b42013-04-24 15:59:26 +0800869#ifdef CONFIG_AT91FAMILY
Stelian Pop7263ef12008-01-03 21:15:56 +0000870 macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
871#else
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100872 macb_writel(macb, USRIO, 0);
Stelian Pop7263ef12008-01-03 21:15:56 +0000873#endif
874#else
Bo Shend8f64b42013-04-24 15:59:26 +0800875#ifdef CONFIG_AT91FAMILY
Stelian Pop7263ef12008-01-03 21:15:56 +0000876 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100877#else
878 macb_writel(macb, USRIO, MACB_BIT(MII));
879#endif
Stelian Pop7263ef12008-01-03 21:15:56 +0000880#endif /* CONFIG_RMII */
Wenyou Yanga212b662016-05-17 13:11:35 +0800881#endif
Bo Shend256be22013-04-24 15:59:28 +0800882 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100883
Wenyou Yanga212b662016-05-17 13:11:35 +0800884#ifdef CONFIG_DM_ETH
Wilson Lee4bf56912017-08-22 20:25:07 -0700885 ret = macb_phy_init(dev, name);
Wenyou Yanga212b662016-05-17 13:11:35 +0800886#else
Wilson Lee4bf56912017-08-22 20:25:07 -0700887 ret = macb_phy_init(macb, name);
Wenyou Yanga212b662016-05-17 13:11:35 +0800888#endif
Wilson Lee4bf56912017-08-22 20:25:07 -0700889 if (ret)
890 return ret;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100891
892 /* Enable TX and RX */
893 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
894
Ben Warren422b1a02008-01-09 18:15:53 -0500895 return 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100896}
897
Simon Glassd5555b72016-05-05 07:28:09 -0600898static void _macb_halt(struct macb_device *macb)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100899{
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100900 u32 ncr, tsr;
901
902 /* Halt the controller and wait for any ongoing transmission to end. */
903 ncr = macb_readl(macb, NCR);
904 ncr |= MACB_BIT(THALT);
905 macb_writel(macb, NCR, ncr);
906
907 do {
908 tsr = macb_readl(macb, TSR);
909 } while (tsr & MACB_BIT(TGO));
910
911 /* Disable TX and RX, and clear statistics */
912 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
913}
914
Simon Glassd5555b72016-05-05 07:28:09 -0600915static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
Ben Warren6bb46792010-06-01 11:55:42 -0700916{
Ben Warren6bb46792010-06-01 11:55:42 -0700917 u32 hwaddr_bottom;
918 u16 hwaddr_top;
919
920 /* set hardware address */
Simon Glassd5555b72016-05-05 07:28:09 -0600921 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
922 enetaddr[2] << 16 | enetaddr[3] << 24;
Ben Warren6bb46792010-06-01 11:55:42 -0700923 macb_writel(macb, SA1B, hwaddr_bottom);
Simon Glassd5555b72016-05-05 07:28:09 -0600924 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
Ben Warren6bb46792010-06-01 11:55:42 -0700925 macb_writel(macb, SA1T, hwaddr_top);
926 return 0;
927}
928
Bo Shend256be22013-04-24 15:59:28 +0800929static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
930{
931 u32 config;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800932#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800933 unsigned long macb_hz = macb->pclk_rate;
934#else
Bo Shend256be22013-04-24 15:59:28 +0800935 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800936#endif
Bo Shend256be22013-04-24 15:59:28 +0800937
938 if (macb_hz < 20000000)
939 config = MACB_BF(CLK, MACB_CLK_DIV8);
940 else if (macb_hz < 40000000)
941 config = MACB_BF(CLK, MACB_CLK_DIV16);
942 else if (macb_hz < 80000000)
943 config = MACB_BF(CLK, MACB_CLK_DIV32);
944 else
945 config = MACB_BF(CLK, MACB_CLK_DIV64);
946
947 return config;
948}
949
950static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
951{
952 u32 config;
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800953
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800954#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800955 unsigned long macb_hz = macb->pclk_rate;
956#else
Bo Shend256be22013-04-24 15:59:28 +0800957 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800958#endif
Bo Shend256be22013-04-24 15:59:28 +0800959
960 if (macb_hz < 20000000)
961 config = GEM_BF(CLK, GEM_CLK_DIV8);
962 else if (macb_hz < 40000000)
963 config = GEM_BF(CLK, GEM_CLK_DIV16);
964 else if (macb_hz < 80000000)
965 config = GEM_BF(CLK, GEM_CLK_DIV32);
966 else if (macb_hz < 120000000)
967 config = GEM_BF(CLK, GEM_CLK_DIV48);
968 else if (macb_hz < 160000000)
969 config = GEM_BF(CLK, GEM_CLK_DIV64);
Ramon Fried9e65f802019-07-16 22:04:32 +0300970 else if (macb_hz < 240000000)
Bo Shend256be22013-04-24 15:59:28 +0800971 config = GEM_BF(CLK, GEM_CLK_DIV96);
Ramon Fried9e65f802019-07-16 22:04:32 +0300972 else if (macb_hz < 320000000)
973 config = GEM_BF(CLK, GEM_CLK_DIV128);
974 else
975 config = GEM_BF(CLK, GEM_CLK_DIV224);
Bo Shend256be22013-04-24 15:59:28 +0800976
977 return config;
978}
979
Bo Shen32e4f6b2013-09-18 15:07:44 +0800980/*
981 * Get the DMA bus width field of the network configuration register that we
982 * should program. We find the width from decoding the design configuration
983 * register to find the maximum supported data bus width.
984 */
985static u32 macb_dbw(struct macb_device *macb)
986{
987 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
988 case 4:
989 return GEM_BF(DBW, GEM_DBW128);
990 case 2:
991 return GEM_BF(DBW, GEM_DBW64);
992 case 1:
993 default:
994 return GEM_BF(DBW, GEM_DBW32);
995 }
996}
997
Simon Glassd5555b72016-05-05 07:28:09 -0600998static void _macb_eth_initialize(struct macb_device *macb)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100999{
Simon Glassd5555b72016-05-05 07:28:09 -06001000 int id = 0; /* This is not used by functions we call */
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001001 u32 ncfgr;
1002
Ramon Friedc6d07bf2019-07-14 18:25:14 +03001003 if (macb_is_gem(macb))
1004 macb->rx_buffer_size = GEM_RX_BUFFER_SIZE;
1005 else
1006 macb->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1007
Simon Glassd5555b72016-05-05 07:28:09 -06001008 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
Ramon Friedc6d07bf2019-07-14 18:25:14 +03001009 macb->rx_buffer = dma_alloc_coherent(macb->rx_buffer_size *
1010 MACB_RX_RING_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001011 &macb->rx_buffer_dma);
Wu, Josh5ae0e382014-05-27 16:31:05 +08001012 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001013 &macb->rx_ring_dma);
Wu, Josh5ae0e382014-05-27 16:31:05 +08001014 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001015 &macb->tx_ring_dma);
Wu, Joshade4ea42015-06-03 16:45:44 +08001016 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
1017 &macb->dummy_desc_dma);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001018
Simon Glassd5555b72016-05-05 07:28:09 -06001019 /*
1020 * Do some basic initialization so that we at least can talk
1021 * to the PHY
1022 */
1023 if (macb_is_gem(macb)) {
1024 ncfgr = gem_mdc_clk_div(id, macb);
1025 ncfgr |= macb_dbw(macb);
1026 } else {
1027 ncfgr = macb_mdc_clk_div(id, macb);
1028 }
1029
1030 macb_writel(macb, NCFGR, ncfgr);
1031}
1032
Simon Glassf1dcc192016-05-05 07:28:11 -06001033#ifndef CONFIG_DM_ETH
Simon Glassd5555b72016-05-05 07:28:09 -06001034static int macb_send(struct eth_device *netdev, void *packet, int length)
1035{
1036 struct macb_device *macb = to_macb(netdev);
1037
1038 return _macb_send(macb, netdev->name, packet, length);
1039}
1040
1041static int macb_recv(struct eth_device *netdev)
1042{
1043 struct macb_device *macb = to_macb(netdev);
1044 uchar *packet;
1045 int length;
1046
1047 macb->wrapped = false;
1048 for (;;) {
1049 macb->next_rx_tail = macb->rx_tail;
1050 length = _macb_recv(macb, &packet);
1051 if (length >= 0) {
1052 net_process_received_packet(packet, length);
1053 reclaim_rx_buffers(macb, macb->next_rx_tail);
Heinrich Schuchardt6cdf0722018-03-18 11:32:53 +01001054 } else {
Simon Glassd5555b72016-05-05 07:28:09 -06001055 return length;
1056 }
1057 }
1058}
1059
1060static int macb_init(struct eth_device *netdev, bd_t *bd)
1061{
1062 struct macb_device *macb = to_macb(netdev);
1063
1064 return _macb_init(macb, netdev->name);
1065}
1066
1067static void macb_halt(struct eth_device *netdev)
1068{
1069 struct macb_device *macb = to_macb(netdev);
1070
1071 return _macb_halt(macb);
1072}
1073
1074static int macb_write_hwaddr(struct eth_device *netdev)
1075{
1076 struct macb_device *macb = to_macb(netdev);
1077
1078 return _macb_write_hwaddr(macb, netdev->enetaddr);
1079}
1080
1081int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
1082{
1083 struct macb_device *macb;
1084 struct eth_device *netdev;
1085
1086 macb = malloc(sizeof(struct macb_device));
1087 if (!macb) {
1088 printf("Error: Failed to allocate memory for MACB%d\n", id);
1089 return -1;
1090 }
1091 memset(macb, 0, sizeof(struct macb_device));
1092
1093 netdev = &macb->netdev;
Wu, Josh5ae0e382014-05-27 16:31:05 +08001094
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001095 macb->regs = regs;
1096 macb->phy_addr = phy_addr;
1097
Bo Shend256be22013-04-24 15:59:28 +08001098 if (macb_is_gem(macb))
1099 sprintf(netdev->name, "gmac%d", id);
1100 else
1101 sprintf(netdev->name, "macb%d", id);
1102
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001103 netdev->init = macb_init;
1104 netdev->halt = macb_halt;
1105 netdev->send = macb_send;
1106 netdev->recv = macb_recv;
Ben Warren6bb46792010-06-01 11:55:42 -07001107 netdev->write_hwaddr = macb_write_hwaddr;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001108
Simon Glassd5555b72016-05-05 07:28:09 -06001109 _macb_eth_initialize(macb);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001110
1111 eth_register(netdev);
1112
Bo Shenb1a00062013-04-24 15:59:27 +08001113#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Joe Hershberger5a49f172016-08-08 11:28:38 -05001114 int retval;
1115 struct mii_dev *mdiodev = mdio_alloc();
1116 if (!mdiodev)
1117 return -ENOMEM;
1118 strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
1119 mdiodev->read = macb_miiphy_read;
1120 mdiodev->write = macb_miiphy_write;
1121
1122 retval = mdio_register(mdiodev);
1123 if (retval < 0)
1124 return retval;
Bo Shenb1a00062013-04-24 15:59:27 +08001125 macb->bus = miiphy_get_dev_by_name(netdev->name);
Semih Hazar0f751d62009-12-17 15:07:15 +02001126#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001127 return 0;
1128}
Simon Glassf1dcc192016-05-05 07:28:11 -06001129#endif /* !CONFIG_DM_ETH */
1130
1131#ifdef CONFIG_DM_ETH
1132
1133static int macb_start(struct udevice *dev)
1134{
Wenyou Yanga212b662016-05-17 13:11:35 +08001135 return _macb_init(dev, dev->name);
Simon Glassf1dcc192016-05-05 07:28:11 -06001136}
1137
1138static int macb_send(struct udevice *dev, void *packet, int length)
1139{
1140 struct macb_device *macb = dev_get_priv(dev);
1141
1142 return _macb_send(macb, dev->name, packet, length);
1143}
1144
1145static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
1146{
1147 struct macb_device *macb = dev_get_priv(dev);
1148
1149 macb->next_rx_tail = macb->rx_tail;
1150 macb->wrapped = false;
1151
1152 return _macb_recv(macb, packetp);
1153}
1154
1155static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
1156{
1157 struct macb_device *macb = dev_get_priv(dev);
1158
1159 reclaim_rx_buffers(macb, macb->next_rx_tail);
1160
1161 return 0;
1162}
1163
1164static void macb_stop(struct udevice *dev)
1165{
1166 struct macb_device *macb = dev_get_priv(dev);
1167
1168 _macb_halt(macb);
1169}
1170
1171static int macb_write_hwaddr(struct udevice *dev)
1172{
1173 struct eth_pdata *plat = dev_get_platdata(dev);
1174 struct macb_device *macb = dev_get_priv(dev);
1175
1176 return _macb_write_hwaddr(macb, plat->enetaddr);
1177}
1178
1179static const struct eth_ops macb_eth_ops = {
1180 .start = macb_start,
1181 .send = macb_send,
1182 .recv = macb_recv,
1183 .stop = macb_stop,
1184 .free_pkt = macb_free_pkt,
1185 .write_hwaddr = macb_write_hwaddr,
1186};
1187
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001188#ifdef CONFIG_CLK
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001189static int macb_enable_clk(struct udevice *dev)
1190{
1191 struct macb_device *macb = dev_get_priv(dev);
1192 struct clk clk;
1193 ulong clk_rate;
1194 int ret;
1195
1196 ret = clk_get_by_index(dev, 0, &clk);
1197 if (ret)
1198 return -EINVAL;
1199
Wilson Lee4bf56912017-08-22 20:25:07 -07001200 /*
Anup Patel2e242f52019-02-25 08:14:36 +00001201 * If clock driver didn't support enable or disable then
1202 * we get -ENOSYS from clk_enable(). To handle this, we
1203 * don't fail for ret == -ENOSYS.
Wilson Lee4bf56912017-08-22 20:25:07 -07001204 */
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001205 ret = clk_enable(&clk);
Anup Patel2e242f52019-02-25 08:14:36 +00001206 if (ret && ret != -ENOSYS)
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001207 return ret;
1208
1209 clk_rate = clk_get_rate(&clk);
1210 if (!clk_rate)
1211 return -EINVAL;
1212
1213 macb->pclk_rate = clk_rate;
1214
1215 return 0;
1216}
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001217#endif
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001218
Ramon Frieded3c64f2019-07-16 22:04:35 +03001219static const struct macb_config default_gem_config = {
1220 .dma_burst_length = 16,
Anup Pateld0a04db2019-07-24 04:09:32 +00001221 .clk_init = NULL,
Ramon Frieded3c64f2019-07-16 22:04:35 +03001222};
1223
Simon Glassf1dcc192016-05-05 07:28:11 -06001224static int macb_eth_probe(struct udevice *dev)
1225{
1226 struct eth_pdata *pdata = dev_get_platdata(dev);
1227 struct macb_device *macb = dev_get_priv(dev);
Wenyou Yanga212b662016-05-17 13:11:35 +08001228 const char *phy_mode;
Anup Pateld0a04db2019-07-24 04:09:32 +00001229 int ret;
Wenyou Yanga212b662016-05-17 13:11:35 +08001230
Simon Glasse160f7d2017-01-17 16:52:55 -07001231 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1232 NULL);
Wenyou Yanga212b662016-05-17 13:11:35 +08001233 if (phy_mode)
1234 macb->phy_interface = phy_get_interface_by_name(phy_mode);
1235 if (macb->phy_interface == -1) {
1236 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1237 return -EINVAL;
1238 }
Wenyou Yanga212b662016-05-17 13:11:35 +08001239
Simon Glassf1dcc192016-05-05 07:28:11 -06001240 macb->regs = (void *)pdata->iobase;
1241
Anup Pateld0a04db2019-07-24 04:09:32 +00001242 macb->config = (struct macb_config *)dev_get_driver_data(dev);
1243 if (!macb->config)
1244 macb->config = &default_gem_config;
Ramon Frieded3c64f2019-07-16 22:04:35 +03001245
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001246#ifdef CONFIG_CLK
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001247 ret = macb_enable_clk(dev);
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001248 if (ret)
1249 return ret;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001250#endif
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001251
Simon Glassf1dcc192016-05-05 07:28:11 -06001252 _macb_eth_initialize(macb);
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001253
Simon Glassf1dcc192016-05-05 07:28:11 -06001254#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001255 macb->bus = mdio_alloc();
1256 if (!macb->bus)
Joe Hershberger5a49f172016-08-08 11:28:38 -05001257 return -ENOMEM;
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001258 strncpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
1259 macb->bus->read = macb_miiphy_read;
1260 macb->bus->write = macb_miiphy_write;
Joe Hershberger5a49f172016-08-08 11:28:38 -05001261
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001262 ret = mdio_register(macb->bus);
1263 if (ret < 0)
1264 return ret;
Simon Glassf1dcc192016-05-05 07:28:11 -06001265 macb->bus = miiphy_get_dev_by_name(dev->name);
1266#endif
1267
1268 return 0;
1269}
1270
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001271static int macb_eth_remove(struct udevice *dev)
1272{
1273 struct macb_device *macb = dev_get_priv(dev);
1274
1275#ifdef CONFIG_PHYLIB
1276 free(macb->phydev);
1277#endif
1278 mdio_unregister(macb->bus);
1279 mdio_free(macb->bus);
1280
1281 return 0;
1282}
1283
Wilson Lee4bf56912017-08-22 20:25:07 -07001284/**
1285 * macb_late_eth_ofdata_to_platdata
1286 * @dev: udevice struct
1287 * Returns 0 when operation success and negative errno number
1288 * when operation failed.
1289 */
1290int __weak macb_late_eth_ofdata_to_platdata(struct udevice *dev)
1291{
1292 return 0;
1293}
1294
Simon Glassf1dcc192016-05-05 07:28:11 -06001295static int macb_eth_ofdata_to_platdata(struct udevice *dev)
1296{
1297 struct eth_pdata *pdata = dev_get_platdata(dev);
1298
Ramon Fried9043c4e2018-12-27 19:58:42 +02001299 pdata->iobase = (phys_addr_t)dev_remap_addr(dev);
1300 if (!pdata->iobase)
1301 return -EINVAL;
Wilson Lee4bf56912017-08-22 20:25:07 -07001302
1303 return macb_late_eth_ofdata_to_platdata(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -06001304}
1305
Ramon Frieded3c64f2019-07-16 22:04:35 +03001306static const struct macb_config sama5d4_config = {
1307 .dma_burst_length = 4,
Anup Pateld0a04db2019-07-24 04:09:32 +00001308 .clk_init = NULL,
1309};
1310
1311static const struct macb_config sifive_config = {
1312 .dma_burst_length = 16,
1313 .clk_init = macb_sifive_clk_init,
Ramon Frieded3c64f2019-07-16 22:04:35 +03001314};
1315
Simon Glassf1dcc192016-05-05 07:28:11 -06001316static const struct udevice_id macb_eth_ids[] = {
1317 { .compatible = "cdns,macb" },
Wenyou Yang75460252017-04-14 14:36:05 +08001318 { .compatible = "cdns,at91sam9260-macb" },
1319 { .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