blob: 21a620c244c928ea954d04bf732dbfb44cf46309 [file] [log] [blame]
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001/*
2 * Copyright (C) 2005-2006 Atmel Corporation
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01005 */
6#include <common.h>
Wenyou Yang577aa3b2016-11-02 10:06:56 +08007#include <clk.h>
Simon Glassf1dcc192016-05-05 07:28:11 -06008#include <dm.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01009
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010010/*
11 * The u-boot networking stack is a little weird. It seems like the
12 * networking core allocates receive buffers up front without any
13 * regard to the hardware that's supposed to actually receive those
14 * packets.
15 *
16 * The MACB receives packets into 128-byte receive buffers, so the
17 * buffers allocated by the core isn't very practical to use. We'll
18 * allocate our own, but we need one such buffer in case a packet
19 * wraps around the DMA ring so that we have to copy it.
20 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020021 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010022 * configuration header. This way, the core allocates one RX buffer
23 * and one TX buffer, each of which can hold a ethernet packet of
24 * maximum size.
25 *
26 * For some reason, the networking core unconditionally specifies a
27 * 32-byte packet "alignment" (which really should be called
28 * "padding"). MACB shouldn't need that, but we'll refrain from any
29 * core modifications here...
30 */
31
32#include <net.h>
Simon Glassf1dcc192016-05-05 07:28:11 -060033#ifndef CONFIG_DM_ETH
Ben Warren89973f82008-08-31 22:22:04 -070034#include <netdev.h>
Simon Glassf1dcc192016-05-05 07:28:11 -060035#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010036#include <malloc.h>
Semih Hazar0f751d62009-12-17 15:07:15 +020037#include <miiphy.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010038
39#include <linux/mii.h>
40#include <asm/io.h>
41#include <asm/dma-mapping.h>
42#include <asm/arch/clk.h>
Masahiro Yamada5d97dff2016-09-21 11:28:57 +090043#include <linux/errno.h>
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010044
45#include "macb.h"
46
Wenyou Yanga212b662016-05-17 13:11:35 +080047DECLARE_GLOBAL_DATA_PTR;
48
Andreas Bießmannceef9832014-05-26 22:55:18 +020049#define MACB_RX_BUFFER_SIZE 4096
50#define MACB_RX_RING_SIZE (MACB_RX_BUFFER_SIZE / 128)
51#define MACB_TX_RING_SIZE 16
52#define MACB_TX_TIMEOUT 1000
53#define MACB_AUTONEG_TIMEOUT 5000000
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010054
55struct macb_dma_desc {
56 u32 addr;
57 u32 ctrl;
58};
59
Wu, Josh5ae0e382014-05-27 16:31:05 +080060#define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
61#define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
62#define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
Wu, Joshade4ea42015-06-03 16:45:44 +080063#define MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
Wu, Josh5ae0e382014-05-27 16:31:05 +080064
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010065#define RXADDR_USED 0x00000001
66#define RXADDR_WRAP 0x00000002
67
68#define RXBUF_FRMLEN_MASK 0x00000fff
69#define RXBUF_FRAME_START 0x00004000
70#define RXBUF_FRAME_END 0x00008000
71#define RXBUF_TYPEID_MATCH 0x00400000
72#define RXBUF_ADDR4_MATCH 0x00800000
73#define RXBUF_ADDR3_MATCH 0x01000000
74#define RXBUF_ADDR2_MATCH 0x02000000
75#define RXBUF_ADDR1_MATCH 0x04000000
76#define RXBUF_BROADCAST 0x80000000
77
78#define TXBUF_FRMLEN_MASK 0x000007ff
79#define TXBUF_FRAME_END 0x00008000
80#define TXBUF_NOCRC 0x00010000
81#define TXBUF_EXHAUSTED 0x08000000
82#define TXBUF_UNDERRUN 0x10000000
83#define TXBUF_MAXRETRY 0x20000000
84#define TXBUF_WRAP 0x40000000
85#define TXBUF_USED 0x80000000
86
87struct macb_device {
88 void *regs;
89
90 unsigned int rx_tail;
91 unsigned int tx_head;
92 unsigned int tx_tail;
Simon Glassd5555b72016-05-05 07:28:09 -060093 unsigned int next_rx_tail;
94 bool wrapped;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +010095
96 void *rx_buffer;
97 void *tx_buffer;
98 struct macb_dma_desc *rx_ring;
99 struct macb_dma_desc *tx_ring;
100
101 unsigned long rx_buffer_dma;
102 unsigned long rx_ring_dma;
103 unsigned long tx_ring_dma;
104
Wu, Joshade4ea42015-06-03 16:45:44 +0800105 struct macb_dma_desc *dummy_desc;
106 unsigned long dummy_desc_dma;
107
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100108 const struct device *dev;
Simon Glassf1dcc192016-05-05 07:28:11 -0600109#ifndef CONFIG_DM_ETH
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100110 struct eth_device netdev;
Simon Glassf1dcc192016-05-05 07:28:11 -0600111#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100112 unsigned short phy_addr;
Bo Shenb1a00062013-04-24 15:59:27 +0800113 struct mii_dev *bus;
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800114#ifdef CONFIG_PHYLIB
115 struct phy_device *phydev;
116#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800117
118#ifdef CONFIG_DM_ETH
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800119#ifdef CONFIG_CLK
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800120 unsigned long pclk_rate;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800121#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800122 phy_interface_t phy_interface;
123#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100124};
Simon Glassf1dcc192016-05-05 07:28:11 -0600125#ifndef CONFIG_DM_ETH
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100126#define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
Simon Glassf1dcc192016-05-05 07:28:11 -0600127#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100128
Bo Shend256be22013-04-24 15:59:28 +0800129static int macb_is_gem(struct macb_device *macb)
130{
131 return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) == 0x2;
132}
133
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100134#ifndef cpu_is_sama5d2
135#define cpu_is_sama5d2() 0
136#endif
137
138#ifndef cpu_is_sama5d4
139#define cpu_is_sama5d4() 0
140#endif
141
142static int gem_is_gigabit_capable(struct macb_device *macb)
143{
144 /*
Robert P. J. Day1cc0a9f2016-05-04 04:47:31 -0400145 * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100146 * configured to support only 10/100.
147 */
148 return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
149}
150
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100151static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
152{
153 unsigned long netctl;
154 unsigned long netstat;
155 unsigned long frame;
156
157 netctl = macb_readl(macb, NCR);
158 netctl |= MACB_BIT(MPE);
159 macb_writel(macb, NCR, netctl);
160
161 frame = (MACB_BF(SOF, 1)
162 | MACB_BF(RW, 1)
163 | MACB_BF(PHYA, macb->phy_addr)
164 | MACB_BF(REGA, reg)
165 | MACB_BF(CODE, 2)
166 | MACB_BF(DATA, value));
167 macb_writel(macb, MAN, frame);
168
169 do {
170 netstat = macb_readl(macb, NSR);
171 } while (!(netstat & MACB_BIT(IDLE)));
172
173 netctl = macb_readl(macb, NCR);
174 netctl &= ~MACB_BIT(MPE);
175 macb_writel(macb, NCR, netctl);
176}
177
178static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
179{
180 unsigned long netctl;
181 unsigned long netstat;
182 unsigned long frame;
183
184 netctl = macb_readl(macb, NCR);
185 netctl |= MACB_BIT(MPE);
186 macb_writel(macb, NCR, netctl);
187
188 frame = (MACB_BF(SOF, 1)
189 | MACB_BF(RW, 2)
190 | MACB_BF(PHYA, macb->phy_addr)
191 | MACB_BF(REGA, reg)
192 | MACB_BF(CODE, 2));
193 macb_writel(macb, MAN, frame);
194
195 do {
196 netstat = macb_readl(macb, NSR);
197 } while (!(netstat & MACB_BIT(IDLE)));
198
199 frame = macb_readl(macb, MAN);
200
201 netctl = macb_readl(macb, NCR);
202 netctl &= ~MACB_BIT(MPE);
203 macb_writel(macb, NCR, netctl);
204
205 return MACB_BFEXT(DATA, frame);
206}
207
Joe Hershberger1b8c18b2013-06-24 19:06:38 -0500208void __weak arch_get_mdio_control(const char *name)
Shiraz Hashim416ce622012-12-13 17:22:52 +0530209{
210 return;
211}
212
Bo Shenb1a00062013-04-24 15:59:27 +0800213#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Semih Hazar0f751d62009-12-17 15:07:15 +0200214
Joe Hershberger5a49f172016-08-08 11:28:38 -0500215int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
Semih Hazar0f751d62009-12-17 15:07:15 +0200216{
Joe Hershberger5a49f172016-08-08 11:28:38 -0500217 u16 value = 0;
Simon Glassf1dcc192016-05-05 07:28:11 -0600218#ifdef CONFIG_DM_ETH
Joe Hershberger5a49f172016-08-08 11:28:38 -0500219 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600220 struct macb_device *macb = dev_get_priv(dev);
221#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500222 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200223 struct macb_device *macb = to_macb(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -0600224#endif
Semih Hazar0f751d62009-12-17 15:07:15 +0200225
Andreas Bießmannceef9832014-05-26 22:55:18 +0200226 if (macb->phy_addr != phy_adr)
Semih Hazar0f751d62009-12-17 15:07:15 +0200227 return -1;
228
Joe Hershberger5a49f172016-08-08 11:28:38 -0500229 arch_get_mdio_control(bus->name);
230 value = macb_mdio_read(macb, reg);
Semih Hazar0f751d62009-12-17 15:07:15 +0200231
Joe Hershberger5a49f172016-08-08 11:28:38 -0500232 return value;
Semih Hazar0f751d62009-12-17 15:07:15 +0200233}
234
Joe Hershberger5a49f172016-08-08 11:28:38 -0500235int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
236 u16 value)
Semih Hazar0f751d62009-12-17 15:07:15 +0200237{
Simon Glassf1dcc192016-05-05 07:28:11 -0600238#ifdef CONFIG_DM_ETH
Joe Hershberger5a49f172016-08-08 11:28:38 -0500239 struct udevice *dev = eth_get_dev_by_name(bus->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600240 struct macb_device *macb = dev_get_priv(dev);
241#else
Joe Hershberger5a49f172016-08-08 11:28:38 -0500242 struct eth_device *dev = eth_get_dev_by_name(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200243 struct macb_device *macb = to_macb(dev);
Simon Glassf1dcc192016-05-05 07:28:11 -0600244#endif
Semih Hazar0f751d62009-12-17 15:07:15 +0200245
Andreas Bießmannceef9832014-05-26 22:55:18 +0200246 if (macb->phy_addr != phy_adr)
Semih Hazar0f751d62009-12-17 15:07:15 +0200247 return -1;
248
Joe Hershberger5a49f172016-08-08 11:28:38 -0500249 arch_get_mdio_control(bus->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200250 macb_mdio_write(macb, reg, value);
251
252 return 0;
253}
254#endif
255
Wu, Josh5ae0e382014-05-27 16:31:05 +0800256#define RX 1
257#define TX 0
258static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
259{
260 if (rx)
Heiko Schocher592a7492016-08-29 07:46:11 +0200261 invalidate_dcache_range(macb->rx_ring_dma,
262 ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
263 PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800264 else
Heiko Schocher592a7492016-08-29 07:46:11 +0200265 invalidate_dcache_range(macb->tx_ring_dma,
266 ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
267 PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800268}
269
270static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
271{
272 if (rx)
273 flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200274 ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800275 else
276 flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200277 ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800278}
279
280static inline void macb_flush_rx_buffer(struct macb_device *macb)
281{
282 flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200283 ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800284}
285
286static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
287{
288 invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200289 ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN));
Wu, Josh5ae0e382014-05-27 16:31:05 +0800290}
Semih Hazar0f751d62009-12-17 15:07:15 +0200291
Jon Loeliger07d38a12007-07-09 17:30:01 -0500292#if defined(CONFIG_CMD_NET)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100293
Simon Glassd5555b72016-05-05 07:28:09 -0600294static int _macb_send(struct macb_device *macb, const char *name, void *packet,
295 int length)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100296{
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100297 unsigned long paddr, ctrl;
298 unsigned int tx_head = macb->tx_head;
299 int i;
300
301 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
302
303 ctrl = length & TXBUF_FRMLEN_MASK;
304 ctrl |= TXBUF_FRAME_END;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200305 if (tx_head == (MACB_TX_RING_SIZE - 1)) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100306 ctrl |= TXBUF_WRAP;
307 macb->tx_head = 0;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200308 } else {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100309 macb->tx_head++;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200310 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100311
312 macb->tx_ring[tx_head].ctrl = ctrl;
313 macb->tx_ring[tx_head].addr = paddr;
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200314 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800315 macb_flush_ring_desc(macb, TX);
316 /* Do we need check paddr and length is dcache line aligned? */
Simon Glassf589f8c2016-05-05 07:28:10 -0600317 flush_dcache_range(paddr, paddr + ALIGN(length, ARCH_DMA_MINALIGN));
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100318 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
319
320 /*
321 * I guess this is necessary because the networking core may
322 * re-use the transmit buffer as soon as we return...
323 */
Andreas Bießmannceef9832014-05-26 22:55:18 +0200324 for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200325 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800326 macb_invalidate_ring_desc(macb, TX);
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200327 ctrl = macb->tx_ring[tx_head].ctrl;
328 if (ctrl & TXBUF_USED)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100329 break;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100330 udelay(1);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100331 }
332
333 dma_unmap_single(packet, length, paddr);
334
Andreas Bießmannceef9832014-05-26 22:55:18 +0200335 if (i <= MACB_TX_TIMEOUT) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100336 if (ctrl & TXBUF_UNDERRUN)
Simon Glassd5555b72016-05-05 07:28:09 -0600337 printf("%s: TX underrun\n", name);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100338 if (ctrl & TXBUF_EXHAUSTED)
Simon Glassd5555b72016-05-05 07:28:09 -0600339 printf("%s: TX buffers exhausted in mid frame\n", name);
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200340 } else {
Simon Glassd5555b72016-05-05 07:28:09 -0600341 printf("%s: TX timeout\n", name);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100342 }
343
344 /* No one cares anyway */
345 return 0;
346}
347
348static void reclaim_rx_buffers(struct macb_device *macb,
349 unsigned int new_tail)
350{
351 unsigned int i;
352
353 i = macb->rx_tail;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800354
355 macb_invalidate_ring_desc(macb, RX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100356 while (i > new_tail) {
357 macb->rx_ring[i].addr &= ~RXADDR_USED;
358 i++;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200359 if (i > MACB_RX_RING_SIZE)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100360 i = 0;
361 }
362
363 while (i < new_tail) {
364 macb->rx_ring[i].addr &= ~RXADDR_USED;
365 i++;
366 }
367
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200368 barrier();
Wu, Josh5ae0e382014-05-27 16:31:05 +0800369 macb_flush_ring_desc(macb, RX);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100370 macb->rx_tail = new_tail;
371}
372
Simon Glassd5555b72016-05-05 07:28:09 -0600373static int _macb_recv(struct macb_device *macb, uchar **packetp)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100374{
Simon Glassd5555b72016-05-05 07:28:09 -0600375 unsigned int next_rx_tail = macb->next_rx_tail;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100376 void *buffer;
377 int length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100378 u32 status;
379
Simon Glassd5555b72016-05-05 07:28:09 -0600380 macb->wrapped = false;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100381 for (;;) {
Wu, Josh5ae0e382014-05-27 16:31:05 +0800382 macb_invalidate_ring_desc(macb, RX);
383
Simon Glassd5555b72016-05-05 07:28:09 -0600384 if (!(macb->rx_ring[next_rx_tail].addr & RXADDR_USED))
385 return -EAGAIN;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100386
Simon Glassd5555b72016-05-05 07:28:09 -0600387 status = macb->rx_ring[next_rx_tail].ctrl;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100388 if (status & RXBUF_FRAME_START) {
Simon Glassd5555b72016-05-05 07:28:09 -0600389 if (next_rx_tail != macb->rx_tail)
390 reclaim_rx_buffers(macb, next_rx_tail);
391 macb->wrapped = false;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100392 }
393
394 if (status & RXBUF_FRAME_END) {
395 buffer = macb->rx_buffer + 128 * macb->rx_tail;
396 length = status & RXBUF_FRMLEN_MASK;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800397
398 macb_invalidate_rx_buffer(macb);
Simon Glassd5555b72016-05-05 07:28:09 -0600399 if (macb->wrapped) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100400 unsigned int headlen, taillen;
401
Andreas Bießmannceef9832014-05-26 22:55:18 +0200402 headlen = 128 * (MACB_RX_RING_SIZE
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100403 - macb->rx_tail);
404 taillen = length - headlen;
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500405 memcpy((void *)net_rx_packets[0],
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100406 buffer, headlen);
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500407 memcpy((void *)net_rx_packets[0] + headlen,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100408 macb->rx_buffer, taillen);
Simon Glassd5555b72016-05-05 07:28:09 -0600409 *packetp = (void *)net_rx_packets[0];
410 } else {
411 *packetp = buffer;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100412 }
413
Simon Glassd5555b72016-05-05 07:28:09 -0600414 if (++next_rx_tail >= MACB_RX_RING_SIZE)
415 next_rx_tail = 0;
416 macb->next_rx_tail = next_rx_tail;
417 return length;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100418 } else {
Simon Glassd5555b72016-05-05 07:28:09 -0600419 if (++next_rx_tail >= MACB_RX_RING_SIZE) {
420 macb->wrapped = true;
421 next_rx_tail = 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100422 }
423 }
Haavard Skinnemoen04fcb5d2007-05-02 13:22:38 +0200424 barrier();
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100425 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100426}
427
Simon Glassd5555b72016-05-05 07:28:09 -0600428static void macb_phy_reset(struct macb_device *macb, const char *name)
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200429{
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200430 int i;
431 u16 status, adv;
432
433 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
434 macb_mdio_write(macb, MII_ADVERTISE, adv);
Simon Glassd5555b72016-05-05 07:28:09 -0600435 printf("%s: Starting autonegotiation...\n", name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200436 macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
437 | BMCR_ANRESTART));
438
Andreas Bießmannceef9832014-05-26 22:55:18 +0200439 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200440 status = macb_mdio_read(macb, MII_BMSR);
441 if (status & BMSR_ANEGCOMPLETE)
442 break;
443 udelay(100);
444 }
445
446 if (status & BMSR_ANEGCOMPLETE)
Simon Glassd5555b72016-05-05 07:28:09 -0600447 printf("%s: Autonegotiation complete\n", name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200448 else
449 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600450 name, status);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200451}
452
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100453#ifdef CONFIG_MACB_SEARCH_PHY
Wenyou Yanga212b662016-05-17 13:11:35 +0800454static int macb_phy_find(struct macb_device *macb, const char *name)
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100455{
456 int i;
457 u16 phy_id;
458
459 /* Search for PHY... */
460 for (i = 0; i < 32; i++) {
461 macb->phy_addr = i;
462 phy_id = macb_mdio_read(macb, MII_PHYSID1);
463 if (phy_id != 0xffff) {
Wenyou Yanga212b662016-05-17 13:11:35 +0800464 printf("%s: PHY present at %d\n", name, i);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100465 return 1;
466 }
467 }
468
469 /* PHY isn't up to snuff */
Wenyou Yanga212b662016-05-17 13:11:35 +0800470 printf("%s: PHY not found\n", name);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100471
472 return 0;
473}
474#endif /* CONFIG_MACB_SEARCH_PHY */
475
Wenyou Yanga212b662016-05-17 13:11:35 +0800476#ifdef CONFIG_DM_ETH
477static int macb_phy_init(struct udevice *dev, const char *name)
478#else
Simon Glassd5555b72016-05-05 07:28:09 -0600479static int macb_phy_init(struct macb_device *macb, const char *name)
Wenyou Yanga212b662016-05-17 13:11:35 +0800480#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100481{
Wenyou Yanga212b662016-05-17 13:11:35 +0800482#ifdef CONFIG_DM_ETH
483 struct macb_device *macb = dev_get_priv(dev);
484#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100485 u32 ncfgr;
486 u16 phy_id, status, adv, lpa;
487 int media, speed, duplex;
488 int i;
489
Simon Glassd5555b72016-05-05 07:28:09 -0600490 arch_get_mdio_control(name);
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100491#ifdef CONFIG_MACB_SEARCH_PHY
492 /* Auto-detect phy_addr */
Wenyou Yanga212b662016-05-17 13:11:35 +0800493 if (!macb_phy_find(macb, name))
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100494 return 0;
Gunnar Rangoyfc01ea12009-01-23 12:56:31 +0100495#endif /* CONFIG_MACB_SEARCH_PHY */
496
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100497 /* Check if the PHY is up to snuff... */
498 phy_id = macb_mdio_read(macb, MII_PHYSID1);
499 if (phy_id == 0xffff) {
Simon Glassd5555b72016-05-05 07:28:09 -0600500 printf("%s: No PHY present\n", name);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100501 return 0;
502 }
503
Bo Shenb1a00062013-04-24 15:59:27 +0800504#ifdef CONFIG_PHYLIB
Wenyou Yanga212b662016-05-17 13:11:35 +0800505#ifdef CONFIG_DM_ETH
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800506 macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
Wenyou Yanga212b662016-05-17 13:11:35 +0800507 macb->phy_interface);
508#else
Bo Shen8314ccd2013-08-19 10:35:47 +0800509 /* need to consider other phy interface mode */
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800510 macb->phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
Bo Shen8314ccd2013-08-19 10:35:47 +0800511 PHY_INTERFACE_MODE_RGMII);
Wenyou Yanga212b662016-05-17 13:11:35 +0800512#endif
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800513 if (!macb->phydev) {
Bo Shen8314ccd2013-08-19 10:35:47 +0800514 printf("phy_connect failed\n");
515 return -ENODEV;
516 }
517
Wenyou Yang1870d4d2017-04-14 14:36:04 +0800518 phy_config(macb->phydev);
Bo Shenb1a00062013-04-24 15:59:27 +0800519#endif
520
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200521 status = macb_mdio_read(macb, MII_BMSR);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100522 if (!(status & BMSR_LSTATUS)) {
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200523 /* Try to re-negotiate if we don't have link already. */
Simon Glassd5555b72016-05-05 07:28:09 -0600524 macb_phy_reset(macb, name);
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200525
Andreas Bießmannceef9832014-05-26 22:55:18 +0200526 for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100527 status = macb_mdio_read(macb, MII_BMSR);
528 if (status & BMSR_LSTATUS)
529 break;
Haavard Skinnemoenf2134f82007-05-02 13:31:53 +0200530 udelay(100);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100531 }
532 }
533
534 if (!(status & BMSR_LSTATUS)) {
535 printf("%s: link down (status: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600536 name, status);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100537 return 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100538 }
Bo Shend256be22013-04-24 15:59:28 +0800539
Gregory CLEMENT75b03cf2015-12-16 14:50:34 +0100540 /* First check for GMAC and that it is GiB capable */
541 if (gem_is_gigabit_capable(macb)) {
Bo Shend256be22013-04-24 15:59:28 +0800542 lpa = macb_mdio_read(macb, MII_STAT1000);
Bo Shend256be22013-04-24 15:59:28 +0800543
Andreas Bießmann47609572014-09-18 23:46:48 +0200544 if (lpa & (LPA_1000FULL | LPA_1000HALF)) {
545 duplex = ((lpa & LPA_1000FULL) ? 1 : 0);
546
547 printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600548 name,
Bo Shend256be22013-04-24 15:59:28 +0800549 duplex ? "full" : "half",
550 lpa);
551
552 ncfgr = macb_readl(macb, NCFGR);
Andreas Bießmann47609572014-09-18 23:46:48 +0200553 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
554 ncfgr |= GEM_BIT(GBE);
555
Bo Shend256be22013-04-24 15:59:28 +0800556 if (duplex)
557 ncfgr |= MACB_BIT(FD);
Andreas Bießmann47609572014-09-18 23:46:48 +0200558
Bo Shend256be22013-04-24 15:59:28 +0800559 macb_writel(macb, NCFGR, ncfgr);
560
561 return 1;
562 }
563 }
564
565 /* fall back for EMAC checking */
566 adv = macb_mdio_read(macb, MII_ADVERTISE);
567 lpa = macb_mdio_read(macb, MII_LPA);
568 media = mii_nway_result(lpa & adv);
569 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
570 ? 1 : 0);
571 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
572 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
Simon Glassd5555b72016-05-05 07:28:09 -0600573 name,
Bo Shend256be22013-04-24 15:59:28 +0800574 speed ? "100" : "10",
575 duplex ? "full" : "half",
576 lpa);
577
578 ncfgr = macb_readl(macb, NCFGR);
Bo Shenc83cb5f2015-03-04 13:35:16 +0800579 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
Bo Shend256be22013-04-24 15:59:28 +0800580 if (speed)
581 ncfgr |= MACB_BIT(SPD);
582 if (duplex)
583 ncfgr |= MACB_BIT(FD);
584 macb_writel(macb, NCFGR, ncfgr);
585
586 return 1;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100587}
588
Wu, Joshade4ea42015-06-03 16:45:44 +0800589static int gmac_init_multi_queues(struct macb_device *macb)
590{
591 int i, num_queues = 1;
592 u32 queue_mask;
593
594 /* bit 0 is never set but queue 0 always exists */
595 queue_mask = gem_readl(macb, DCFG6) & 0xff;
596 queue_mask |= 0x1;
597
598 for (i = 1; i < MACB_MAX_QUEUES; i++)
599 if (queue_mask & (1 << i))
600 num_queues++;
601
602 macb->dummy_desc->ctrl = TXBUF_USED;
603 macb->dummy_desc->addr = 0;
604 flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
Heiko Schocher592a7492016-08-29 07:46:11 +0200605 ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
Wu, Joshade4ea42015-06-03 16:45:44 +0800606
607 for (i = 1; i < num_queues; i++)
608 gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
609
610 return 0;
611}
612
Wenyou Yanga212b662016-05-17 13:11:35 +0800613#ifdef CONFIG_DM_ETH
614static int _macb_init(struct udevice *dev, const char *name)
615#else
Simon Glassd5555b72016-05-05 07:28:09 -0600616static int _macb_init(struct macb_device *macb, const char *name)
Wenyou Yanga212b662016-05-17 13:11:35 +0800617#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100618{
Wenyou Yanga212b662016-05-17 13:11:35 +0800619#ifdef CONFIG_DM_ETH
620 struct macb_device *macb = dev_get_priv(dev);
621#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100622 unsigned long paddr;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100623 int i;
624
625 /*
626 * macb_halt should have been called at some point before now,
627 * so we'll assume the controller is idle.
628 */
629
630 /* initialize DMA descriptors */
631 paddr = macb->rx_buffer_dma;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200632 for (i = 0; i < MACB_RX_RING_SIZE; i++) {
633 if (i == (MACB_RX_RING_SIZE - 1))
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100634 paddr |= RXADDR_WRAP;
635 macb->rx_ring[i].addr = paddr;
636 macb->rx_ring[i].ctrl = 0;
637 paddr += 128;
638 }
Wu, Josh5ae0e382014-05-27 16:31:05 +0800639 macb_flush_ring_desc(macb, RX);
640 macb_flush_rx_buffer(macb);
641
Andreas Bießmannceef9832014-05-26 22:55:18 +0200642 for (i = 0; i < MACB_TX_RING_SIZE; i++) {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100643 macb->tx_ring[i].addr = 0;
Andreas Bießmannceef9832014-05-26 22:55:18 +0200644 if (i == (MACB_TX_RING_SIZE - 1))
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100645 macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
646 else
647 macb->tx_ring[i].ctrl = TXBUF_USED;
648 }
Wu, Josh5ae0e382014-05-27 16:31:05 +0800649 macb_flush_ring_desc(macb, TX);
650
Andreas Bießmannceef9832014-05-26 22:55:18 +0200651 macb->rx_tail = 0;
652 macb->tx_head = 0;
653 macb->tx_tail = 0;
Simon Glassd5555b72016-05-05 07:28:09 -0600654 macb->next_rx_tail = 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100655
656 macb_writel(macb, RBQP, macb->rx_ring_dma);
657 macb_writel(macb, TBQP, macb->tx_ring_dma);
658
Bo Shend256be22013-04-24 15:59:28 +0800659 if (macb_is_gem(macb)) {
Wu, Joshade4ea42015-06-03 16:45:44 +0800660 /* Check the multi queue and initialize the queue for tx */
661 gmac_init_multi_queues(macb);
662
Bo Shencabf61c2014-11-10 15:24:01 +0800663 /*
664 * When the GMAC IP with GE feature, this bit is used to
665 * select interface between RGMII and GMII.
666 * When the GMAC IP without GE feature, this bit is used
667 * to select interface between RMII and MII.
668 */
Wenyou Yanga212b662016-05-17 13:11:35 +0800669#ifdef CONFIG_DM_ETH
670 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
671 gem_writel(macb, UR, GEM_BIT(RGMII));
672 else
673 gem_writel(macb, UR, 0);
674#else
Bo Shencabf61c2014-11-10 15:24:01 +0800675#if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
Bo Shend256be22013-04-24 15:59:28 +0800676 gem_writel(macb, UR, GEM_BIT(RGMII));
677#else
678 gem_writel(macb, UR, 0);
679#endif
Wenyou Yanga212b662016-05-17 13:11:35 +0800680#endif
Bo Shend256be22013-04-24 15:59:28 +0800681 } else {
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100682 /* choose RMII or MII mode. This depends on the board */
Wenyou Yanga212b662016-05-17 13:11:35 +0800683#ifdef CONFIG_DM_ETH
684#ifdef CONFIG_AT91FAMILY
685 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
686 macb_writel(macb, USRIO,
687 MACB_BIT(RMII) | MACB_BIT(CLKEN));
688 } else {
689 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
690 }
691#else
692 if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
693 macb_writel(macb, USRIO, 0);
694 else
695 macb_writel(macb, USRIO, MACB_BIT(MII));
696#endif
697#else
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100698#ifdef CONFIG_RMII
Bo Shend8f64b42013-04-24 15:59:26 +0800699#ifdef CONFIG_AT91FAMILY
Stelian Pop7263ef12008-01-03 21:15:56 +0000700 macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
701#else
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100702 macb_writel(macb, USRIO, 0);
Stelian Pop7263ef12008-01-03 21:15:56 +0000703#endif
704#else
Bo Shend8f64b42013-04-24 15:59:26 +0800705#ifdef CONFIG_AT91FAMILY
Stelian Pop7263ef12008-01-03 21:15:56 +0000706 macb_writel(macb, USRIO, MACB_BIT(CLKEN));
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100707#else
708 macb_writel(macb, USRIO, MACB_BIT(MII));
709#endif
Stelian Pop7263ef12008-01-03 21:15:56 +0000710#endif /* CONFIG_RMII */
Wenyou Yanga212b662016-05-17 13:11:35 +0800711#endif
Bo Shend256be22013-04-24 15:59:28 +0800712 }
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100713
Wenyou Yanga212b662016-05-17 13:11:35 +0800714#ifdef CONFIG_DM_ETH
715 if (!macb_phy_init(dev, name))
716#else
Simon Glassd5555b72016-05-05 07:28:09 -0600717 if (!macb_phy_init(macb, name))
Wenyou Yanga212b662016-05-17 13:11:35 +0800718#endif
Ben Warren422b1a02008-01-09 18:15:53 -0500719 return -1;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100720
721 /* Enable TX and RX */
722 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
723
Ben Warren422b1a02008-01-09 18:15:53 -0500724 return 0;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100725}
726
Simon Glassd5555b72016-05-05 07:28:09 -0600727static void _macb_halt(struct macb_device *macb)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100728{
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100729 u32 ncr, tsr;
730
731 /* Halt the controller and wait for any ongoing transmission to end. */
732 ncr = macb_readl(macb, NCR);
733 ncr |= MACB_BIT(THALT);
734 macb_writel(macb, NCR, ncr);
735
736 do {
737 tsr = macb_readl(macb, TSR);
738 } while (tsr & MACB_BIT(TGO));
739
740 /* Disable TX and RX, and clear statistics */
741 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
742}
743
Simon Glassd5555b72016-05-05 07:28:09 -0600744static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
Ben Warren6bb46792010-06-01 11:55:42 -0700745{
Ben Warren6bb46792010-06-01 11:55:42 -0700746 u32 hwaddr_bottom;
747 u16 hwaddr_top;
748
749 /* set hardware address */
Simon Glassd5555b72016-05-05 07:28:09 -0600750 hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
751 enetaddr[2] << 16 | enetaddr[3] << 24;
Ben Warren6bb46792010-06-01 11:55:42 -0700752 macb_writel(macb, SA1B, hwaddr_bottom);
Simon Glassd5555b72016-05-05 07:28:09 -0600753 hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
Ben Warren6bb46792010-06-01 11:55:42 -0700754 macb_writel(macb, SA1T, hwaddr_top);
755 return 0;
756}
757
Bo Shend256be22013-04-24 15:59:28 +0800758static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
759{
760 u32 config;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800761#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800762 unsigned long macb_hz = macb->pclk_rate;
763#else
Bo Shend256be22013-04-24 15:59:28 +0800764 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800765#endif
Bo Shend256be22013-04-24 15:59:28 +0800766
767 if (macb_hz < 20000000)
768 config = MACB_BF(CLK, MACB_CLK_DIV8);
769 else if (macb_hz < 40000000)
770 config = MACB_BF(CLK, MACB_CLK_DIV16);
771 else if (macb_hz < 80000000)
772 config = MACB_BF(CLK, MACB_CLK_DIV32);
773 else
774 config = MACB_BF(CLK, MACB_CLK_DIV64);
775
776 return config;
777}
778
779static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
780{
781 u32 config;
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800782
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +0800783#if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800784 unsigned long macb_hz = macb->pclk_rate;
785#else
Bo Shend256be22013-04-24 15:59:28 +0800786 unsigned long macb_hz = get_macb_pclk_rate(id);
Wenyou Yang577aa3b2016-11-02 10:06:56 +0800787#endif
Bo Shend256be22013-04-24 15:59:28 +0800788
789 if (macb_hz < 20000000)
790 config = GEM_BF(CLK, GEM_CLK_DIV8);
791 else if (macb_hz < 40000000)
792 config = GEM_BF(CLK, GEM_CLK_DIV16);
793 else if (macb_hz < 80000000)
794 config = GEM_BF(CLK, GEM_CLK_DIV32);
795 else if (macb_hz < 120000000)
796 config = GEM_BF(CLK, GEM_CLK_DIV48);
797 else if (macb_hz < 160000000)
798 config = GEM_BF(CLK, GEM_CLK_DIV64);
799 else
800 config = GEM_BF(CLK, GEM_CLK_DIV96);
801
802 return config;
803}
804
Bo Shen32e4f6b2013-09-18 15:07:44 +0800805/*
806 * Get the DMA bus width field of the network configuration register that we
807 * should program. We find the width from decoding the design configuration
808 * register to find the maximum supported data bus width.
809 */
810static u32 macb_dbw(struct macb_device *macb)
811{
812 switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
813 case 4:
814 return GEM_BF(DBW, GEM_DBW128);
815 case 2:
816 return GEM_BF(DBW, GEM_DBW64);
817 case 1:
818 default:
819 return GEM_BF(DBW, GEM_DBW32);
820 }
821}
822
Simon Glassd5555b72016-05-05 07:28:09 -0600823static void _macb_eth_initialize(struct macb_device *macb)
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100824{
Simon Glassd5555b72016-05-05 07:28:09 -0600825 int id = 0; /* This is not used by functions we call */
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100826 u32 ncfgr;
827
Simon Glassd5555b72016-05-05 07:28:09 -0600828 /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
Andreas Bießmannceef9832014-05-26 22:55:18 +0200829 macb->rx_buffer = dma_alloc_coherent(MACB_RX_BUFFER_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100830 &macb->rx_buffer_dma);
Wu, Josh5ae0e382014-05-27 16:31:05 +0800831 macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100832 &macb->rx_ring_dma);
Wu, Josh5ae0e382014-05-27 16:31:05 +0800833 macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100834 &macb->tx_ring_dma);
Wu, Joshade4ea42015-06-03 16:45:44 +0800835 macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
836 &macb->dummy_desc_dma);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100837
Simon Glassd5555b72016-05-05 07:28:09 -0600838 /*
839 * Do some basic initialization so that we at least can talk
840 * to the PHY
841 */
842 if (macb_is_gem(macb)) {
843 ncfgr = gem_mdc_clk_div(id, macb);
844 ncfgr |= macb_dbw(macb);
845 } else {
846 ncfgr = macb_mdc_clk_div(id, macb);
847 }
848
849 macb_writel(macb, NCFGR, ncfgr);
850}
851
Simon Glassf1dcc192016-05-05 07:28:11 -0600852#ifndef CONFIG_DM_ETH
Simon Glassd5555b72016-05-05 07:28:09 -0600853static int macb_send(struct eth_device *netdev, void *packet, int length)
854{
855 struct macb_device *macb = to_macb(netdev);
856
857 return _macb_send(macb, netdev->name, packet, length);
858}
859
860static int macb_recv(struct eth_device *netdev)
861{
862 struct macb_device *macb = to_macb(netdev);
863 uchar *packet;
864 int length;
865
866 macb->wrapped = false;
867 for (;;) {
868 macb->next_rx_tail = macb->rx_tail;
869 length = _macb_recv(macb, &packet);
870 if (length >= 0) {
871 net_process_received_packet(packet, length);
872 reclaim_rx_buffers(macb, macb->next_rx_tail);
873 } else if (length < 0) {
874 return length;
875 }
876 }
877}
878
879static int macb_init(struct eth_device *netdev, bd_t *bd)
880{
881 struct macb_device *macb = to_macb(netdev);
882
883 return _macb_init(macb, netdev->name);
884}
885
886static void macb_halt(struct eth_device *netdev)
887{
888 struct macb_device *macb = to_macb(netdev);
889
890 return _macb_halt(macb);
891}
892
893static int macb_write_hwaddr(struct eth_device *netdev)
894{
895 struct macb_device *macb = to_macb(netdev);
896
897 return _macb_write_hwaddr(macb, netdev->enetaddr);
898}
899
900int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
901{
902 struct macb_device *macb;
903 struct eth_device *netdev;
904
905 macb = malloc(sizeof(struct macb_device));
906 if (!macb) {
907 printf("Error: Failed to allocate memory for MACB%d\n", id);
908 return -1;
909 }
910 memset(macb, 0, sizeof(struct macb_device));
911
912 netdev = &macb->netdev;
Wu, Josh5ae0e382014-05-27 16:31:05 +0800913
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100914 macb->regs = regs;
915 macb->phy_addr = phy_addr;
916
Bo Shend256be22013-04-24 15:59:28 +0800917 if (macb_is_gem(macb))
918 sprintf(netdev->name, "gmac%d", id);
919 else
920 sprintf(netdev->name, "macb%d", id);
921
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100922 netdev->init = macb_init;
923 netdev->halt = macb_halt;
924 netdev->send = macb_send;
925 netdev->recv = macb_recv;
Ben Warren6bb46792010-06-01 11:55:42 -0700926 netdev->write_hwaddr = macb_write_hwaddr;
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100927
Simon Glassd5555b72016-05-05 07:28:09 -0600928 _macb_eth_initialize(macb);
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100929
930 eth_register(netdev);
931
Bo Shenb1a00062013-04-24 15:59:27 +0800932#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Joe Hershberger5a49f172016-08-08 11:28:38 -0500933 int retval;
934 struct mii_dev *mdiodev = mdio_alloc();
935 if (!mdiodev)
936 return -ENOMEM;
937 strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
938 mdiodev->read = macb_miiphy_read;
939 mdiodev->write = macb_miiphy_write;
940
941 retval = mdio_register(mdiodev);
942 if (retval < 0)
943 return retval;
Bo Shenb1a00062013-04-24 15:59:27 +0800944 macb->bus = miiphy_get_dev_by_name(netdev->name);
Semih Hazar0f751d62009-12-17 15:07:15 +0200945#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +0100946 return 0;
947}
Simon Glassf1dcc192016-05-05 07:28:11 -0600948#endif /* !CONFIG_DM_ETH */
949
950#ifdef CONFIG_DM_ETH
951
952static int macb_start(struct udevice *dev)
953{
Wenyou Yanga212b662016-05-17 13:11:35 +0800954 return _macb_init(dev, dev->name);
Simon Glassf1dcc192016-05-05 07:28:11 -0600955}
956
957static int macb_send(struct udevice *dev, void *packet, int length)
958{
959 struct macb_device *macb = dev_get_priv(dev);
960
961 return _macb_send(macb, dev->name, packet, length);
962}
963
964static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
965{
966 struct macb_device *macb = dev_get_priv(dev);
967
968 macb->next_rx_tail = macb->rx_tail;
969 macb->wrapped = false;
970
971 return _macb_recv(macb, packetp);
972}
973
974static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
975{
976 struct macb_device *macb = dev_get_priv(dev);
977
978 reclaim_rx_buffers(macb, macb->next_rx_tail);
979
980 return 0;
981}
982
983static void macb_stop(struct udevice *dev)
984{
985 struct macb_device *macb = dev_get_priv(dev);
986
987 _macb_halt(macb);
988}
989
990static int macb_write_hwaddr(struct udevice *dev)
991{
992 struct eth_pdata *plat = dev_get_platdata(dev);
993 struct macb_device *macb = dev_get_priv(dev);
994
995 return _macb_write_hwaddr(macb, plat->enetaddr);
996}
997
998static const struct eth_ops macb_eth_ops = {
999 .start = macb_start,
1000 .send = macb_send,
1001 .recv = macb_recv,
1002 .stop = macb_stop,
1003 .free_pkt = macb_free_pkt,
1004 .write_hwaddr = macb_write_hwaddr,
1005};
1006
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001007#ifdef CONFIG_CLK
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001008static int macb_enable_clk(struct udevice *dev)
1009{
1010 struct macb_device *macb = dev_get_priv(dev);
1011 struct clk clk;
1012 ulong clk_rate;
1013 int ret;
1014
1015 ret = clk_get_by_index(dev, 0, &clk);
1016 if (ret)
1017 return -EINVAL;
1018
1019 ret = clk_enable(&clk);
1020 if (ret)
1021 return ret;
1022
1023 clk_rate = clk_get_rate(&clk);
1024 if (!clk_rate)
1025 return -EINVAL;
1026
1027 macb->pclk_rate = clk_rate;
1028
1029 return 0;
1030}
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001031#endif
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001032
Simon Glassf1dcc192016-05-05 07:28:11 -06001033static int macb_eth_probe(struct udevice *dev)
1034{
1035 struct eth_pdata *pdata = dev_get_platdata(dev);
1036 struct macb_device *macb = dev_get_priv(dev);
Wenyou Yanga212b662016-05-17 13:11:35 +08001037 const char *phy_mode;
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001038 __maybe_unused int ret;
Wenyou Yanga212b662016-05-17 13:11:35 +08001039
Simon Glasse160f7d2017-01-17 16:52:55 -07001040 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1041 NULL);
Wenyou Yanga212b662016-05-17 13:11:35 +08001042 if (phy_mode)
1043 macb->phy_interface = phy_get_interface_by_name(phy_mode);
1044 if (macb->phy_interface == -1) {
1045 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1046 return -EINVAL;
1047 }
Wenyou Yanga212b662016-05-17 13:11:35 +08001048
Simon Glassf1dcc192016-05-05 07:28:11 -06001049 macb->regs = (void *)pdata->iobase;
1050
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001051#ifdef CONFIG_CLK
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001052 ret = macb_enable_clk(dev);
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001053 if (ret)
1054 return ret;
Wenyou Yang3fd2b3a2017-02-14 16:24:40 +08001055#endif
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001056
Simon Glassf1dcc192016-05-05 07:28:11 -06001057 _macb_eth_initialize(macb);
Wenyou Yang577aa3b2016-11-02 10:06:56 +08001058
Simon Glassf1dcc192016-05-05 07:28:11 -06001059#if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001060 macb->bus = mdio_alloc();
1061 if (!macb->bus)
Joe Hershberger5a49f172016-08-08 11:28:38 -05001062 return -ENOMEM;
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001063 strncpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
1064 macb->bus->read = macb_miiphy_read;
1065 macb->bus->write = macb_miiphy_write;
Joe Hershberger5a49f172016-08-08 11:28:38 -05001066
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001067 ret = mdio_register(macb->bus);
1068 if (ret < 0)
1069 return ret;
Simon Glassf1dcc192016-05-05 07:28:11 -06001070 macb->bus = miiphy_get_dev_by_name(dev->name);
1071#endif
1072
1073 return 0;
1074}
1075
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001076static int macb_eth_remove(struct udevice *dev)
1077{
1078 struct macb_device *macb = dev_get_priv(dev);
1079
1080#ifdef CONFIG_PHYLIB
1081 free(macb->phydev);
1082#endif
1083 mdio_unregister(macb->bus);
1084 mdio_free(macb->bus);
1085
1086 return 0;
1087}
1088
Simon Glassf1dcc192016-05-05 07:28:11 -06001089static int macb_eth_ofdata_to_platdata(struct udevice *dev)
1090{
1091 struct eth_pdata *pdata = dev_get_platdata(dev);
1092
1093 pdata->iobase = dev_get_addr(dev);
1094 return 0;
1095}
1096
1097static const struct udevice_id macb_eth_ids[] = {
1098 { .compatible = "cdns,macb" },
Wenyou Yang75460252017-04-14 14:36:05 +08001099 { .compatible = "cdns,at91sam9260-macb" },
1100 { .compatible = "atmel,sama5d2-gem" },
1101 { .compatible = "atmel,sama5d3-gem" },
1102 { .compatible = "atmel,sama5d4-gem" },
Simon Glassf1dcc192016-05-05 07:28:11 -06001103 { }
1104};
1105
1106U_BOOT_DRIVER(eth_macb) = {
1107 .name = "eth_macb",
1108 .id = UCLASS_ETH,
1109 .of_match = macb_eth_ids,
1110 .ofdata_to_platdata = macb_eth_ofdata_to_platdata,
1111 .probe = macb_eth_probe,
Wenyou Yang1870d4d2017-04-14 14:36:04 +08001112 .remove = macb_eth_remove,
Simon Glassf1dcc192016-05-05 07:28:11 -06001113 .ops = &macb_eth_ops,
1114 .priv_auto_alloc_size = sizeof(struct macb_device),
1115 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1116};
1117#endif
Haavard Skinnemoen5c1fe1f2006-01-20 10:03:34 +01001118
Jon Loeliger07d38a12007-07-09 17:30:01 -05001119#endif