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