blob: ebb399457fb58041f402742478db54cfe87b6c40 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Herringc9830dc2011-12-15 11:15:49 +00002/*
3 * Copyright 2010-2011 Calxeda, Inc.
Rob Herringc9830dc2011-12-15 11:15:49 +00004 */
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005
Rob Herringc9830dc2011-12-15 11:15:49 +00006#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -06007#include <net.h>
Rob Herring3c76c542012-02-01 12:58:49 +00008#include <linux/compiler.h>
Simon Glassdbd79542020-05-10 11:40:11 -06009#include <linux/delay.h>
Rob Herringc9830dc2011-12-15 11:15:49 +000010#include <linux/err.h>
11#include <asm/io.h>
Andre Przywara7352fb92021-04-12 01:04:52 +010012#include <dm.h>
13#include <dm/device-internal.h> /* for dev_set_priv() */
Rob Herringc9830dc2011-12-15 11:15:49 +000014
15#define TX_NUM_DESC 1
16#define RX_NUM_DESC 32
17
18#define MAC_TIMEOUT (5*CONFIG_SYS_HZ)
19
20#define ETH_BUF_SZ 2048
21#define TX_BUF_SZ (ETH_BUF_SZ * TX_NUM_DESC)
22#define RX_BUF_SZ (ETH_BUF_SZ * RX_NUM_DESC)
23
24#define RXSTART 0x00000002
25#define TXSTART 0x00002000
26
27#define RXENABLE 0x00000004
28#define TXENABLE 0x00000008
29
30#define XGMAC_CONTROL_SPD 0x40000000
31#define XGMAC_CONTROL_SPD_MASK 0x60000000
32#define XGMAC_CONTROL_SARC 0x10000000
33#define XGMAC_CONTROL_SARK_MASK 0x18000000
34#define XGMAC_CONTROL_CAR 0x04000000
35#define XGMAC_CONTROL_CAR_MASK 0x06000000
36#define XGMAC_CONTROL_CAR_SHIFT 25
37#define XGMAC_CONTROL_DP 0x01000000
38#define XGMAC_CONTROL_WD 0x00800000
39#define XGMAC_CONTROL_JD 0x00400000
40#define XGMAC_CONTROL_JE 0x00100000
41#define XGMAC_CONTROL_LM 0x00001000
42#define XGMAC_CONTROL_IPC 0x00000400
43#define XGMAC_CONTROL_ACS 0x00000080
44#define XGMAC_CONTROL_DDIC 0x00000010
45#define XGMAC_CONTROL_TE 0x00000008
46#define XGMAC_CONTROL_RE 0x00000004
47
48#define XGMAC_DMA_BUSMODE_RESET 0x00000001
49#define XGMAC_DMA_BUSMODE_DSL 0x00000004
50#define XGMAC_DMA_BUSMODE_DSL_MASK 0x0000007c
51#define XGMAC_DMA_BUSMODE_DSL_SHIFT 2
52#define XGMAC_DMA_BUSMODE_ATDS 0x00000080
53#define XGMAC_DMA_BUSMODE_PBL_MASK 0x00003f00
54#define XGMAC_DMA_BUSMODE_PBL_SHIFT 8
55#define XGMAC_DMA_BUSMODE_FB 0x00010000
56#define XGMAC_DMA_BUSMODE_USP 0x00800000
57#define XGMAC_DMA_BUSMODE_8PBL 0x01000000
58#define XGMAC_DMA_BUSMODE_AAL 0x02000000
59
60#define XGMAC_DMA_AXIMODE_ENLPI 0x80000000
61#define XGMAC_DMA_AXIMODE_MGK 0x40000000
62#define XGMAC_DMA_AXIMODE_WROSR 0x00100000
63#define XGMAC_DMA_AXIMODE_WROSR_MASK 0x00F00000
64#define XGMAC_DMA_AXIMODE_WROSR_SHIFT 20
65#define XGMAC_DMA_AXIMODE_RDOSR 0x00010000
66#define XGMAC_DMA_AXIMODE_RDOSR_MASK 0x000F0000
67#define XGMAC_DMA_AXIMODE_RDOSR_SHIFT 16
68#define XGMAC_DMA_AXIMODE_AAL 0x00001000
69#define XGMAC_DMA_AXIMODE_BLEN256 0x00000080
70#define XGMAC_DMA_AXIMODE_BLEN128 0x00000040
71#define XGMAC_DMA_AXIMODE_BLEN64 0x00000020
72#define XGMAC_DMA_AXIMODE_BLEN32 0x00000010
73#define XGMAC_DMA_AXIMODE_BLEN16 0x00000008
74#define XGMAC_DMA_AXIMODE_BLEN8 0x00000004
75#define XGMAC_DMA_AXIMODE_BLEN4 0x00000002
76#define XGMAC_DMA_AXIMODE_UNDEF 0x00000001
77
78#define XGMAC_CORE_OMR_RTC_SHIFT 3
79#define XGMAC_CORE_OMR_RTC_MASK 0x00000018
80#define XGMAC_CORE_OMR_RTC 0x00000010
81#define XGMAC_CORE_OMR_RSF 0x00000020
82#define XGMAC_CORE_OMR_DT 0x00000040
83#define XGMAC_CORE_OMR_FEF 0x00000080
84#define XGMAC_CORE_OMR_EFC 0x00000100
85#define XGMAC_CORE_OMR_RFA_SHIFT 9
86#define XGMAC_CORE_OMR_RFA_MASK 0x00000E00
87#define XGMAC_CORE_OMR_RFD_SHIFT 12
88#define XGMAC_CORE_OMR_RFD_MASK 0x00007000
89#define XGMAC_CORE_OMR_TTC_SHIFT 16
90#define XGMAC_CORE_OMR_TTC_MASK 0x00030000
91#define XGMAC_CORE_OMR_TTC 0x00020000
92#define XGMAC_CORE_OMR_FTF 0x00100000
93#define XGMAC_CORE_OMR_TSF 0x00200000
94
95#define FIFO_MINUS_1K 0x0
96#define FIFO_MINUS_2K 0x1
97#define FIFO_MINUS_3K 0x2
98#define FIFO_MINUS_4K 0x3
99#define FIFO_MINUS_6K 0x4
100#define FIFO_MINUS_8K 0x5
101#define FIFO_MINUS_12K 0x6
102#define FIFO_MINUS_16K 0x7
103
104#define XGMAC_CORE_FLOW_PT_SHIFT 16
105#define XGMAC_CORE_FLOW_PT_MASK 0xFFFF0000
106#define XGMAC_CORE_FLOW_PT 0x00010000
107#define XGMAC_CORE_FLOW_DZQP 0x00000080
108#define XGMAC_CORE_FLOW_PLT_SHIFT 4
109#define XGMAC_CORE_FLOW_PLT_MASK 0x00000030
110#define XGMAC_CORE_FLOW_PLT 0x00000010
111#define XGMAC_CORE_FLOW_UP 0x00000008
112#define XGMAC_CORE_FLOW_RFE 0x00000004
113#define XGMAC_CORE_FLOW_TFE 0x00000002
114#define XGMAC_CORE_FLOW_FCB 0x00000001
115
116/* XGMAC Descriptor Defines */
117#define MAX_DESC_BUF_SZ (0x2000 - 8)
118
119#define RXDESC_EXT_STATUS 0x00000001
120#define RXDESC_CRC_ERR 0x00000002
121#define RXDESC_RX_ERR 0x00000008
122#define RXDESC_RX_WDOG 0x00000010
123#define RXDESC_FRAME_TYPE 0x00000020
124#define RXDESC_GIANT_FRAME 0x00000080
125#define RXDESC_LAST_SEG 0x00000100
126#define RXDESC_FIRST_SEG 0x00000200
127#define RXDESC_VLAN_FRAME 0x00000400
128#define RXDESC_OVERFLOW_ERR 0x00000800
129#define RXDESC_LENGTH_ERR 0x00001000
130#define RXDESC_SA_FILTER_FAIL 0x00002000
131#define RXDESC_DESCRIPTOR_ERR 0x00004000
132#define RXDESC_ERROR_SUMMARY 0x00008000
133#define RXDESC_FRAME_LEN_OFFSET 16
134#define RXDESC_FRAME_LEN_MASK 0x3fff0000
135#define RXDESC_DA_FILTER_FAIL 0x40000000
136
137#define RXDESC1_END_RING 0x00008000
138
139#define RXDESC_IP_PAYLOAD_MASK 0x00000003
140#define RXDESC_IP_PAYLOAD_UDP 0x00000001
141#define RXDESC_IP_PAYLOAD_TCP 0x00000002
142#define RXDESC_IP_PAYLOAD_ICMP 0x00000003
143#define RXDESC_IP_HEADER_ERR 0x00000008
144#define RXDESC_IP_PAYLOAD_ERR 0x00000010
145#define RXDESC_IPV4_PACKET 0x00000040
146#define RXDESC_IPV6_PACKET 0x00000080
147#define TXDESC_UNDERFLOW_ERR 0x00000001
148#define TXDESC_JABBER_TIMEOUT 0x00000002
149#define TXDESC_LOCAL_FAULT 0x00000004
150#define TXDESC_REMOTE_FAULT 0x00000008
151#define TXDESC_VLAN_FRAME 0x00000010
152#define TXDESC_FRAME_FLUSHED 0x00000020
153#define TXDESC_IP_HEADER_ERR 0x00000040
154#define TXDESC_PAYLOAD_CSUM_ERR 0x00000080
155#define TXDESC_ERROR_SUMMARY 0x00008000
156#define TXDESC_SA_CTRL_INSERT 0x00040000
157#define TXDESC_SA_CTRL_REPLACE 0x00080000
158#define TXDESC_2ND_ADDR_CHAINED 0x00100000
159#define TXDESC_END_RING 0x00200000
160#define TXDESC_CSUM_IP 0x00400000
161#define TXDESC_CSUM_IP_PAYLD 0x00800000
162#define TXDESC_CSUM_ALL 0x00C00000
163#define TXDESC_CRC_EN_REPLACE 0x01000000
164#define TXDESC_CRC_EN_APPEND 0x02000000
165#define TXDESC_DISABLE_PAD 0x04000000
166#define TXDESC_FIRST_SEG 0x10000000
167#define TXDESC_LAST_SEG 0x20000000
168#define TXDESC_INTERRUPT 0x40000000
169
170#define DESC_OWN 0x80000000
171#define DESC_BUFFER1_SZ_MASK 0x00001fff
172#define DESC_BUFFER2_SZ_MASK 0x1fff0000
173#define DESC_BUFFER2_SZ_OFFSET 16
174
175struct xgmac_regs {
176 u32 config;
177 u32 framefilter;
178 u32 resv_1[4];
179 u32 flow_control;
180 u32 vlantag;
181 u32 version;
182 u32 vlaninclude;
183 u32 resv_2[2];
184 u32 pacestretch;
185 u32 vlanhash;
186 u32 resv_3;
187 u32 intreg;
188 struct {
189 u32 hi; /* 0x40 */
190 u32 lo; /* 0x44 */
191 } macaddr[16];
192 u32 resv_4[0xd0];
193 u32 core_opmode; /* 0x400 */
194 u32 resv_5[0x2bf];
195 u32 busmode; /* 0xf00 */
196 u32 txpoll;
197 u32 rxpoll;
198 u32 rxdesclist;
199 u32 txdesclist;
200 u32 dma_status;
201 u32 dma_opmode;
202 u32 intenable;
203 u32 resv_6[2];
204 u32 axi_mode; /* 0xf28 */
205};
206
207struct xgmac_dma_desc {
208 __le32 flags;
209 __le32 buf_size;
210 __le32 buf1_addr; /* Buffer 1 Address Pointer */
211 __le32 buf2_addr; /* Buffer 2 Address Pointer */
212 __le32 ext_status;
213 __le32 res[3];
214};
215
Andre Przywara7352fb92021-04-12 01:04:52 +0100216static struct xgmac_regs *xgmac_get_regs(struct eth_pdata *pdata)
217{
218 /*
219 * We use PHYS_64BIT on Highbank, so phys_addr_t is bigger than
220 * a pointer. U-Boot doesn't use LPAE (not even the MMU on highbank),
221 * so we can't access anything above 4GB.
222 * We have a check in the probe function below the ensure this,
223 * so casting to a 32-bit pointer type is fine here.
224 */
225 return (struct xgmac_regs *)(uintptr_t)pdata->iobase;
226}
227
Rob Herringc9830dc2011-12-15 11:15:49 +0000228/* XGMAC Descriptor Access Helpers */
229static inline void desc_set_buf_len(struct xgmac_dma_desc *p, u32 buf_sz)
230{
231 if (buf_sz > MAX_DESC_BUF_SZ)
232 p->buf_size = cpu_to_le32(MAX_DESC_BUF_SZ |
233 (buf_sz - MAX_DESC_BUF_SZ) << DESC_BUFFER2_SZ_OFFSET);
234 else
235 p->buf_size = cpu_to_le32(buf_sz);
236}
237
238static inline int desc_get_buf_len(struct xgmac_dma_desc *p)
239{
240 u32 len = le32_to_cpu(p->buf_size);
241 return (len & DESC_BUFFER1_SZ_MASK) +
242 ((len & DESC_BUFFER2_SZ_MASK) >> DESC_BUFFER2_SZ_OFFSET);
243}
244
245static inline void desc_init_rx_desc(struct xgmac_dma_desc *p, int ring_size,
246 int buf_sz)
247{
248 struct xgmac_dma_desc *end = p + ring_size - 1;
249
250 memset(p, 0, sizeof(*p) * ring_size);
251
252 for (; p <= end; p++)
253 desc_set_buf_len(p, buf_sz);
254
255 end->buf_size |= cpu_to_le32(RXDESC1_END_RING);
256}
257
258static inline void desc_init_tx_desc(struct xgmac_dma_desc *p, u32 ring_size)
259{
260 memset(p, 0, sizeof(*p) * ring_size);
261 p[ring_size - 1].flags = cpu_to_le32(TXDESC_END_RING);
262}
263
264static inline int desc_get_owner(struct xgmac_dma_desc *p)
265{
266 return le32_to_cpu(p->flags) & DESC_OWN;
267}
268
269static inline void desc_set_rx_owner(struct xgmac_dma_desc *p)
270{
271 /* Clear all fields and set the owner */
272 p->flags = cpu_to_le32(DESC_OWN);
273}
274
275static inline void desc_set_tx_owner(struct xgmac_dma_desc *p, u32 flags)
276{
277 u32 tmpflags = le32_to_cpu(p->flags);
278 tmpflags &= TXDESC_END_RING;
279 tmpflags |= flags | DESC_OWN;
280 p->flags = cpu_to_le32(tmpflags);
281}
282
283static inline void *desc_get_buf_addr(struct xgmac_dma_desc *p)
284{
285 return (void *)le32_to_cpu(p->buf1_addr);
286}
287
288static inline void desc_set_buf_addr(struct xgmac_dma_desc *p,
289 void *paddr, int len)
290{
291 p->buf1_addr = cpu_to_le32(paddr);
292 if (len > MAX_DESC_BUF_SZ)
293 p->buf2_addr = cpu_to_le32(paddr + MAX_DESC_BUF_SZ);
294}
295
296static inline void desc_set_buf_addr_and_size(struct xgmac_dma_desc *p,
297 void *paddr, int len)
298{
299 desc_set_buf_len(p, len);
300 desc_set_buf_addr(p, paddr, len);
301}
302
303static inline int desc_get_rx_frame_len(struct xgmac_dma_desc *p)
304{
305 u32 data = le32_to_cpu(p->flags);
306 u32 len = (data & RXDESC_FRAME_LEN_MASK) >> RXDESC_FRAME_LEN_OFFSET;
307 if (data & RXDESC_FRAME_TYPE)
308 len -= 4;
309
310 return len;
311}
312
313struct calxeda_eth_dev {
314 struct xgmac_dma_desc rx_chain[RX_NUM_DESC];
315 struct xgmac_dma_desc tx_chain[TX_NUM_DESC];
316 char rxbuffer[RX_BUF_SZ];
317
318 u32 tx_currdesc;
319 u32 rx_currdesc;
Rob Herringc9830dc2011-12-15 11:15:49 +0000320} __aligned(32);
321
322/*
323 * Initialize a descriptor ring. Calxeda XGMAC is configured to use
324 * advanced descriptors.
325 */
326
Andre Przywara7352fb92021-04-12 01:04:52 +0100327static void init_rx_desc(struct eth_pdata *pdata, struct calxeda_eth_dev *priv)
Rob Herringc9830dc2011-12-15 11:15:49 +0000328{
329 struct xgmac_dma_desc *rxdesc = priv->rx_chain;
Andre Przywara7352fb92021-04-12 01:04:52 +0100330 struct xgmac_regs *regs = xgmac_get_regs(pdata);
Rob Herringc9830dc2011-12-15 11:15:49 +0000331 void *rxbuffer = priv->rxbuffer;
332 int i;
333
334 desc_init_rx_desc(rxdesc, RX_NUM_DESC, ETH_BUF_SZ);
335 writel((ulong)rxdesc, &regs->rxdesclist);
336
337 for (i = 0; i < RX_NUM_DESC; i++) {
338 desc_set_buf_addr(rxdesc + i, rxbuffer + (i * ETH_BUF_SZ),
339 ETH_BUF_SZ);
340 desc_set_rx_owner(rxdesc + i);
341 }
342}
343
Andre Przywara7352fb92021-04-12 01:04:52 +0100344static void init_tx_desc(struct eth_pdata *pdata, struct calxeda_eth_dev *priv)
Rob Herringc9830dc2011-12-15 11:15:49 +0000345{
Andre Przywara7352fb92021-04-12 01:04:52 +0100346 struct xgmac_regs *regs = xgmac_get_regs(pdata);
Rob Herringc9830dc2011-12-15 11:15:49 +0000347
348 desc_init_tx_desc(priv->tx_chain, TX_NUM_DESC);
349 writel((ulong)priv->tx_chain, &regs->txdesclist);
350}
351
Andre Przywara7352fb92021-04-12 01:04:52 +0100352static int xgmac_reset(struct xgmac_regs *regs)
Rob Herringc9830dc2011-12-15 11:15:49 +0000353{
Rob Herringc9830dc2011-12-15 11:15:49 +0000354 int timeout = MAC_TIMEOUT;
355 u32 value;
356
357 value = readl(&regs->config) & XGMAC_CONTROL_SPD_MASK;
358
359 writel(XGMAC_DMA_BUSMODE_RESET, &regs->busmode);
360 while ((timeout-- >= 0) &&
361 (readl(&regs->busmode) & XGMAC_DMA_BUSMODE_RESET))
362 udelay(1);
363
364 writel(value, &regs->config);
365
366 return timeout;
367}
368
Andre Przywara7352fb92021-04-12 01:04:52 +0100369static void xgmac_hwmacaddr(struct eth_pdata *pdata)
Rob Herringc9830dc2011-12-15 11:15:49 +0000370{
Andre Przywara7352fb92021-04-12 01:04:52 +0100371 struct xgmac_regs *regs = xgmac_get_regs(pdata);
Rob Herringc9830dc2011-12-15 11:15:49 +0000372 u32 macaddr[2];
373
Andre Przywara7352fb92021-04-12 01:04:52 +0100374 memcpy(macaddr, pdata->enetaddr, ARP_HLEN);
Rob Herringc9830dc2011-12-15 11:15:49 +0000375 writel(macaddr[1], &regs->macaddr[0].hi);
376 writel(macaddr[0], &regs->macaddr[0].lo);
377}
378
Andre Przywara7352fb92021-04-12 01:04:52 +0100379static int xgmac_eth_start(struct udevice *dev)
Rob Herringc9830dc2011-12-15 11:15:49 +0000380{
Andre Przywara7352fb92021-04-12 01:04:52 +0100381 struct eth_pdata *pdata = dev_get_plat(dev);
382 struct xgmac_regs *regs = xgmac_get_regs(pdata);
383 struct calxeda_eth_dev *priv = dev_get_priv(dev);
384 u32 value;
Rob Herringc9830dc2011-12-15 11:15:49 +0000385
Andre Przywara7352fb92021-04-12 01:04:52 +0100386 if (xgmac_reset(regs) < 0)
387 return -ETIMEDOUT;
Rob Herringc9830dc2011-12-15 11:15:49 +0000388
389 /* set the hardware MAC address */
Andre Przywara7352fb92021-04-12 01:04:52 +0100390 xgmac_hwmacaddr(pdata);
Rob Herringc9830dc2011-12-15 11:15:49 +0000391
392 /* set the AXI bus modes */
393 value = XGMAC_DMA_BUSMODE_ATDS |
394 (16 << XGMAC_DMA_BUSMODE_PBL_SHIFT) |
395 XGMAC_DMA_BUSMODE_FB | XGMAC_DMA_BUSMODE_AAL;
396 writel(value, &regs->busmode);
397
398 value = XGMAC_DMA_AXIMODE_AAL | XGMAC_DMA_AXIMODE_BLEN16 |
399 XGMAC_DMA_AXIMODE_BLEN8 | XGMAC_DMA_AXIMODE_BLEN4;
400 writel(value, &regs->axi_mode);
401
402 /* set flow control parameters and store and forward mode */
403 value = (FIFO_MINUS_12K << XGMAC_CORE_OMR_RFD_SHIFT) |
404 (FIFO_MINUS_4K << XGMAC_CORE_OMR_RFA_SHIFT) |
Rob Herringe48ed1a2013-06-12 22:24:46 -0500405 XGMAC_CORE_OMR_EFC | XGMAC_CORE_OMR_TSF;
Rob Herringc9830dc2011-12-15 11:15:49 +0000406 writel(value, &regs->core_opmode);
407
408 /* enable pause frames */
409 value = (1024 << XGMAC_CORE_FLOW_PT_SHIFT) |
410 (1 << XGMAC_CORE_FLOW_PLT_SHIFT) |
411 XGMAC_CORE_FLOW_UP | XGMAC_CORE_FLOW_RFE | XGMAC_CORE_FLOW_TFE;
412 writel(value, &regs->flow_control);
413
414 /* Initialize the descriptor chains */
Andre Przywara7352fb92021-04-12 01:04:52 +0100415 init_rx_desc(pdata, priv);
416 init_tx_desc(pdata, priv);
Rob Herringc9830dc2011-12-15 11:15:49 +0000417
418 /* must set to 0, or when started up will cause issues */
419 priv->tx_currdesc = 0;
420 priv->rx_currdesc = 0;
421
422 /* set default core values */
423 value = readl(&regs->config);
424 value &= XGMAC_CONTROL_SPD_MASK;
425 value |= XGMAC_CONTROL_DDIC | XGMAC_CONTROL_ACS |
426 XGMAC_CONTROL_IPC | XGMAC_CONTROL_CAR;
427
428 /* Everything is ready enable both mac and DMA */
429 value |= RXENABLE | TXENABLE;
430 writel(value, &regs->config);
431
432 value = readl(&regs->dma_opmode);
433 value |= RXSTART | TXSTART;
434 writel(value, &regs->dma_opmode);
435
436 return 0;
437}
438
Andre Przywara7352fb92021-04-12 01:04:52 +0100439static int xgmac_tx(struct udevice *dev, void *packet, int length)
Rob Herringc9830dc2011-12-15 11:15:49 +0000440{
Andre Przywara7352fb92021-04-12 01:04:52 +0100441 struct calxeda_eth_dev *priv = dev_get_priv(dev);
442 struct eth_pdata *pdata = dev_get_plat(dev);
443 struct xgmac_regs *regs = xgmac_get_regs(pdata);
Rob Herringc9830dc2011-12-15 11:15:49 +0000444 u32 currdesc = priv->tx_currdesc;
445 struct xgmac_dma_desc *txdesc = &priv->tx_chain[currdesc];
446 int timeout;
447
Joe Hershberger4aeb03b2012-05-21 14:45:20 +0000448 desc_set_buf_addr_and_size(txdesc, packet, length);
Rob Herringc9830dc2011-12-15 11:15:49 +0000449 desc_set_tx_owner(txdesc, TXDESC_FIRST_SEG |
450 TXDESC_LAST_SEG | TXDESC_CRC_EN_APPEND);
451
452 /* write poll demand */
453 writel(1, &regs->txpoll);
454
455 timeout = 1000000;
456 while (desc_get_owner(txdesc)) {
457 if (timeout-- < 0) {
458 printf("xgmac: TX timeout\n");
459 return -ETIMEDOUT;
460 }
461 udelay(1);
462 }
463
464 priv->tx_currdesc = (currdesc + 1) & (TX_NUM_DESC - 1);
465 return 0;
466}
467
Andre Przywara7352fb92021-04-12 01:04:52 +0100468static int xgmac_rx(struct udevice *dev, int flags, uchar **packetp)
Rob Herringc9830dc2011-12-15 11:15:49 +0000469{
Andre Przywara7352fb92021-04-12 01:04:52 +0100470 struct calxeda_eth_dev *priv = dev_get_priv(dev);
Rob Herringc9830dc2011-12-15 11:15:49 +0000471 u32 currdesc = priv->rx_currdesc;
472 struct xgmac_dma_desc *rxdesc = &priv->rx_chain[currdesc];
473 int length = 0;
474
475 /* check if the host has the desc */
476 if (desc_get_owner(rxdesc))
Andre Przywara7352fb92021-04-12 01:04:52 +0100477 return -EAGAIN; /* the MAC is still chewing on it */
Rob Herringc9830dc2011-12-15 11:15:49 +0000478
479 length = desc_get_rx_frame_len(rxdesc);
Andre Przywara7352fb92021-04-12 01:04:52 +0100480 *packetp = desc_get_buf_addr(rxdesc);
Rob Herringc9830dc2011-12-15 11:15:49 +0000481
Andre Przywara7352fb92021-04-12 01:04:52 +0100482 priv->rx_currdesc = (currdesc + 1) & (RX_NUM_DESC - 1);
483
484 return length;
485}
486
487static int xgmac_free_pkt(struct udevice *dev, uchar *packet, int length)
488{
489 struct eth_pdata *pdata = dev_get_plat(dev);
490 struct xgmac_regs *regs = xgmac_get_regs(pdata);
491 struct calxeda_eth_dev *priv = dev_get_priv(dev);
492 u32 rxdesc = ((char *)packet - priv->rxbuffer) / ETH_BUF_SZ;
493 struct xgmac_dma_desc *p = &priv->rx_chain[rxdesc];
Rob Herringc9830dc2011-12-15 11:15:49 +0000494
495 /* set descriptor back to owned by XGMAC */
Andre Przywara7352fb92021-04-12 01:04:52 +0100496 desc_set_rx_owner(p);
Rob Herringc9830dc2011-12-15 11:15:49 +0000497 writel(1, &regs->rxpoll);
498
Andre Przywara7352fb92021-04-12 01:04:52 +0100499 return 0;
Rob Herringc9830dc2011-12-15 11:15:49 +0000500}
501
Andre Przywara7352fb92021-04-12 01:04:52 +0100502static void xgmac_eth_stop(struct udevice *dev)
Rob Herringc9830dc2011-12-15 11:15:49 +0000503{
Andre Przywara7352fb92021-04-12 01:04:52 +0100504 struct calxeda_eth_dev *priv = dev_get_priv(dev);
505 struct eth_pdata *pdata = dev_get_plat(dev);
506 struct xgmac_regs *regs = xgmac_get_regs(pdata);
Rob Herringc9830dc2011-12-15 11:15:49 +0000507 int value;
508
509 /* Disable TX/RX */
510 value = readl(&regs->config);
511 value &= ~(RXENABLE | TXENABLE);
512 writel(value, &regs->config);
513
514 /* Disable DMA */
515 value = readl(&regs->dma_opmode);
516 value &= ~(RXSTART | TXSTART);
517 writel(value, &regs->dma_opmode);
518
519 /* must set to 0, or when started up will cause issues */
520 priv->tx_currdesc = 0;
521 priv->rx_currdesc = 0;
522}
523
Andre Przywara7352fb92021-04-12 01:04:52 +0100524/*
525 * Changing the MAC address is not a good idea, as the fabric would
526 * need to know about this as well (it does not learn MAC addresses).
527 */
528static int xgmac_eth_write_hwaddr(struct udevice *dev)
529{
530 return -ENOSYS;
531}
532
533static int xgmac_eth_read_rom_hwaddr(struct udevice *dev)
Rob Herringc9830dc2011-12-15 11:15:49 +0000534{
Andre Przywara7352fb92021-04-12 01:04:52 +0100535 struct eth_pdata *pdata = dev_get_plat(dev);
536 struct xgmac_regs *regs = xgmac_get_regs(pdata);
Rob Herringc9830dc2011-12-15 11:15:49 +0000537 u32 macaddr[2];
538
Andre Przywara7352fb92021-04-12 01:04:52 +0100539 /* The MAC address is already configured, so read it from registers. */
540 macaddr[1] = readl(&regs->macaddr[0].hi);
541 macaddr[0] = readl(&regs->macaddr[0].lo);
542 memcpy(pdata->enetaddr, macaddr, ARP_HLEN);
Rob Herringc9830dc2011-12-15 11:15:49 +0000543
Andre Przywara7352fb92021-04-12 01:04:52 +0100544 return 0;
545}
Rob Herringc9830dc2011-12-15 11:15:49 +0000546
Andre Przywara7352fb92021-04-12 01:04:52 +0100547static int xgmac_ofdata_to_platdata(struct udevice *dev)
548{
549 struct eth_pdata *pdata = dev_get_plat(dev);
550 struct calxeda_eth_dev *priv;
Rob Herringc9830dc2011-12-15 11:15:49 +0000551
552 /* Structure must be aligned, because it contains the descriptors */
553 priv = memalign(32, sizeof(*priv));
Andre Przywara7352fb92021-04-12 01:04:52 +0100554 if (!priv)
555 return -ENOMEM;
556 dev_set_priv(dev, priv);
557
558 pdata->iobase = devfdt_get_addr(dev);
559 if (pdata->iobase == FDT_ADDR_T_NONE) {
560 printf("%s: Cannot find XGMAC base address\n", __func__);
561 return -EINVAL;
562 }
563 if (pdata->iobase >= (1ULL << 32)) {
564 printf("%s: MMIO base address cannot be above 4GB\n", __func__);
565 return -EINVAL;
Rob Herringc9830dc2011-12-15 11:15:49 +0000566 }
567
Andre Przywara7352fb92021-04-12 01:04:52 +0100568 return 0;
569}
Rob Herringc9830dc2011-12-15 11:15:49 +0000570
Andre Przywara7352fb92021-04-12 01:04:52 +0100571static int xgmac_eth_probe(struct udevice *dev)
572{
573 struct eth_pdata *pdata = dev_get_plat(dev);
574 struct xgmac_regs *regs = xgmac_get_regs(pdata);
Rob Herringc9830dc2011-12-15 11:15:49 +0000575
Andre Przywara7352fb92021-04-12 01:04:52 +0100576 /* check hardware version */
577 if (readl(&regs->version) != 0x1012)
578 return -ENODEV;
Rob Herringc9830dc2011-12-15 11:15:49 +0000579
Andre Przywara7352fb92021-04-12 01:04:52 +0100580 xgmac_eth_read_rom_hwaddr(dev);
Rob Herringc9830dc2011-12-15 11:15:49 +0000581
Andre Przywara7352fb92021-04-12 01:04:52 +0100582 return 0;
Rob Herringc9830dc2011-12-15 11:15:49 +0000583}
Andre Przywara7352fb92021-04-12 01:04:52 +0100584
585static const struct eth_ops xgmac_eth_ops = {
586 .start = xgmac_eth_start,
587 .send = xgmac_tx,
588 .recv = xgmac_rx,
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200589 .free_pkt = xgmac_free_pkt,
Andre Przywara7352fb92021-04-12 01:04:52 +0100590 .stop = xgmac_eth_stop,
591 .write_hwaddr = xgmac_eth_write_hwaddr,
592 .read_rom_hwaddr = xgmac_eth_read_rom_hwaddr,
593};
594
595static const struct udevice_id xgmac_eth_ids[] = {
596 { .compatible = "calxeda,hb-xgmac" },
597 { }
598};
599
600U_BOOT_DRIVER(eth_xgmac) = {
601 .name = "eth_xgmac",
602 .id = UCLASS_ETH,
603 .of_match = xgmac_eth_ids,
604 .of_to_plat = xgmac_ofdata_to_platdata,
605 .probe = xgmac_eth_probe,
606 .ops = &xgmac_eth_ops,
607 .plat_auto = sizeof(struct eth_pdata),
608};