1 // SPDX-License-Identifier: GPL-2.0
3 * Xilinx Multirate Ethernet MAC(MRMAC) driver
5 * Author(s): Ashok Reddy Soma <ashok.reddy.soma@xilinx.com>
6 * Michal Simek <michal.simek@xilinx.com>
8 * Copyright (C) 2021 Xilinx, Inc. All rights reserved.
20 #include <linux/delay.h>
21 #include <linux/ethtool.h>
22 #include "xilinx_axi_mrmac.h"
24 static void axi_mrmac_dma_write(struct mcdma_bd *bd, u32 *desc)
26 if (IS_ENABLED(CONFIG_PHYS_64BIT))
27 writeq((unsigned long)bd, desc);
29 writel((uintptr_t)bd, desc);
33 * axi_mrmac_ethernet_init - MRMAC init function
34 * @priv: MRMAC private structure
36 * Return: 0 on success, negative value on errors
38 * This function is called to reset and initialize MRMAC core. This is
39 * typically called during initialization. It does a reset of MRMAC Rx/Tx
40 * channels and Rx/Tx SERDES. It configures MRMAC speed based on mrmac_rate
41 * which is read from DT. This function waits for block lock bit to get set,
42 * if it is not set within 100ms time returns a timeout error.
44 static int axi_mrmac_ethernet_init(struct axi_mrmac_priv *priv)
46 struct mrmac_regs *regs = priv->iobase;
50 /* Perform all the RESET's required */
51 setbits_le32(®s->reset, MRMAC_RX_SERDES_RST_MASK | MRMAC_RX_RST_MASK
52 | MRMAC_TX_SERDES_RST_MASK | MRMAC_TX_RST_MASK);
54 mdelay(MRMAC_RESET_DELAY);
56 /* Configure Mode register */
57 reg = readl(®s->mode);
59 log_debug("Configuring MRMAC speed to %d\n", priv->mrmac_rate);
61 if (priv->mrmac_rate == SPEED_25000) {
62 reg &= ~MRMAC_CTL_RATE_CFG_MASK;
63 reg |= MRMAC_CTL_DATA_RATE_25G;
64 reg |= (MRMAC_CTL_AXIS_CFG_25G_IND << MRMAC_CTL_AXIS_CFG_SHIFT);
65 reg |= (MRMAC_CTL_SERDES_WIDTH_25G <<
66 MRMAC_CTL_SERDES_WIDTH_SHIFT);
68 reg &= ~MRMAC_CTL_RATE_CFG_MASK;
69 reg |= MRMAC_CTL_DATA_RATE_10G;
70 reg |= (MRMAC_CTL_AXIS_CFG_10G_IND << MRMAC_CTL_AXIS_CFG_SHIFT);
71 reg |= (MRMAC_CTL_SERDES_WIDTH_10G <<
72 MRMAC_CTL_SERDES_WIDTH_SHIFT);
76 reg |= MRMAC_CTL_PM_TICK_MASK;
77 writel(reg, ®s->mode);
79 clrbits_le32(®s->reset, MRMAC_RX_SERDES_RST_MASK | MRMAC_RX_RST_MASK
80 | MRMAC_TX_SERDES_RST_MASK | MRMAC_TX_RST_MASK);
82 mdelay(MRMAC_RESET_DELAY);
84 /* Setup MRMAC hardware options */
85 setbits_le32(®s->rx_config, MRMAC_RX_DEL_FCS_MASK);
86 setbits_le32(®s->tx_config, MRMAC_TX_INS_FCS_MASK);
87 setbits_le32(®s->tx_config, MRMAC_TX_EN_MASK);
88 setbits_le32(®s->rx_config, MRMAC_RX_EN_MASK);
90 /* Check for block lock bit to be set. This ensures that
91 * MRMAC ethernet IP is functioning normally.
93 writel(MRMAC_STS_ALL_MASK, (phys_addr_t)priv->iobase +
95 writel(MRMAC_STS_ALL_MASK, (phys_addr_t)priv->iobase +
97 writel(MRMAC_STS_ALL_MASK, (phys_addr_t)priv->iobase +
98 MRMAC_STATRX_BLKLCK_OFFSET);
100 ret = wait_for_bit_le32((u32 *)((phys_addr_t)priv->iobase +
101 MRMAC_STATRX_BLKLCK_OFFSET),
102 MRMAC_RX_BLKLCK_MASK, true,
103 MRMAC_BLKLCK_TIMEOUT, true);
105 log_warning("Error: MRMAC block lock not complete!\n");
109 writel(MRMAC_TICK_TRIGGER, ®s->tick_reg);
115 * axi_mcdma_init - Reset MCDMA engine
116 * @priv: MRMAC private structure
118 * Return: 0 on success, negative value on timeouts
120 * This function is called to reset and initialize MCDMA engine
122 static int axi_mcdma_init(struct axi_mrmac_priv *priv)
126 /* Reset the engine so the hardware starts from a known state */
127 writel(XMCDMA_CR_RESET, &priv->mm2s_cmn->control);
128 writel(XMCDMA_CR_RESET, &priv->s2mm_cmn->control);
130 /* Check Tx/Rx MCDMA.RST. Reset is done when the reset bit is low */
131 ret = wait_for_bit_le32(&priv->mm2s_cmn->control, XMCDMA_CR_RESET,
132 false, MRMAC_DMARST_TIMEOUT, true);
134 log_warning("Tx MCDMA reset Timeout\n");
138 ret = wait_for_bit_le32(&priv->s2mm_cmn->control, XMCDMA_CR_RESET,
139 false, MRMAC_DMARST_TIMEOUT, true);
141 log_warning("Rx MCDMA reset Timeout\n");
145 /* Enable channel 1 for Tx and Rx */
146 writel(XMCDMA_CHANNEL_1, &priv->mm2s_cmn->chen);
147 writel(XMCDMA_CHANNEL_1, &priv->s2mm_cmn->chen);
153 * axi_mrmac_start - MRMAC start
154 * @dev: udevice structure
156 * Return: 0 on success, negative value on errors
158 * This is a initialization function of MRMAC. Call MCDMA initialization
159 * function and setup Rx buffer descriptors for starting reception of packets.
160 * Enable Tx and Rx channels and trigger Rx channel fetch.
162 static int axi_mrmac_start(struct udevice *dev)
164 struct axi_mrmac_priv *priv = dev_get_priv(dev);
165 struct mrmac_regs *regs = priv->iobase;
168 * Initialize MCDMA engine. MCDMA engine must be initialized before
169 * MRMAC. During MCDMA engine initialization, MCDMA hardware is reset,
170 * since MCDMA reset line is connected to MRMAC, this would ensure a
173 axi_mcdma_init(priv);
175 /* Initialize MRMAC hardware */
176 if (axi_mrmac_ethernet_init(priv))
179 /* Disable all Rx interrupts before RxBD space setup */
180 clrbits_le32(&priv->mcdma_rx->control, XMCDMA_IRQ_ALL_MASK);
182 /* Update current descriptor */
183 axi_mrmac_dma_write(priv->rx_bd[0], &priv->mcdma_rx->current);
185 /* Setup Rx BD. MRMAC needs atleast two descriptors */
186 memset(priv->rx_bd[0], 0, RX_BD_TOTAL_SIZE);
188 priv->rx_bd[0]->next_desc = lower_32_bits((u64)priv->rx_bd[1]);
189 priv->rx_bd[0]->buf_addr = lower_32_bits((u64)net_rx_packets[0]);
191 priv->rx_bd[1]->next_desc = lower_32_bits((u64)priv->rx_bd[0]);
192 priv->rx_bd[1]->buf_addr = lower_32_bits((u64)net_rx_packets[1]);
194 if (IS_ENABLED(CONFIG_PHYS_64BIT)) {
195 priv->rx_bd[0]->next_desc_msb = upper_32_bits((u64)priv->rx_bd[1]);
196 priv->rx_bd[0]->buf_addr_msb = upper_32_bits((u64)net_rx_packets[0]);
198 priv->rx_bd[1]->next_desc_msb = upper_32_bits((u64)priv->rx_bd[0]);
199 priv->rx_bd[1]->buf_addr_msb = upper_32_bits((u64)net_rx_packets[1]);
202 priv->rx_bd[0]->cntrl = PKTSIZE_ALIGN;
203 priv->rx_bd[1]->cntrl = PKTSIZE_ALIGN;
205 /* Flush the last BD so DMA core could see the updates */
206 flush_cache((phys_addr_t)priv->rx_bd[0], RX_BD_TOTAL_SIZE);
208 /* It is necessary to flush rx buffers because if you don't do it
209 * then cache can contain uninitialized data
211 flush_cache((phys_addr_t)priv->rx_bd[0]->buf_addr, RX_BUFF_TOTAL_SIZE);
213 /* Start the hardware */
214 setbits_le32(&priv->s2mm_cmn->control, XMCDMA_CR_RUNSTOP_MASK);
215 setbits_le32(&priv->mm2s_cmn->control, XMCDMA_CR_RUNSTOP_MASK);
216 setbits_le32(&priv->mcdma_rx->control, XMCDMA_IRQ_ALL_MASK);
219 setbits_le32(&priv->mcdma_rx->control, XMCDMA_CR_RUNSTOP_MASK);
221 /* Update tail descriptor. Now it's ready to receive data */
222 axi_mrmac_dma_write(priv->rx_bd[1], &priv->mcdma_rx->tail);
225 setbits_le32(®s->tx_config, MRMAC_TX_EN_MASK);
228 setbits_le32(®s->rx_config, MRMAC_RX_EN_MASK);
234 * axi_mrmac_send - MRMAC Tx function
235 * @dev: udevice structure
236 * @ptr: pointer to Tx buffer
237 * @len: transfer length
239 * Return: 0 on success, negative value on errors
241 * This is a Tx send function of MRMAC. Setup Tx buffer descriptors and trigger
242 * transfer. Wait till the data is transferred.
244 static int axi_mrmac_send(struct udevice *dev, void *ptr, int len)
246 struct axi_mrmac_priv *priv = dev_get_priv(dev);
250 print_buffer(ptr, ptr, 1, len, 16);
252 if (len > PKTSIZE_ALIGN)
255 /* If size is less than min packet size, pad to min size */
256 if (len < MIN_PKT_SIZE) {
257 memset(priv->txminframe, 0, MIN_PKT_SIZE);
258 memcpy(priv->txminframe, ptr, len);
260 ptr = priv->txminframe;
263 writel(XMCDMA_IRQ_ALL_MASK, &priv->mcdma_tx->status);
265 clrbits_le32(&priv->mcdma_tx->control, XMCDMA_CR_RUNSTOP_MASK);
267 /* Flush packet to main memory to be trasfered by DMA */
268 flush_cache((phys_addr_t)ptr, len);
270 /* Setup Tx BD. MRMAC needs atleast two descriptors */
271 memset(priv->tx_bd[0], 0, TX_BD_TOTAL_SIZE);
273 priv->tx_bd[0]->next_desc = lower_32_bits((u64)priv->tx_bd[1]);
274 priv->tx_bd[0]->buf_addr = lower_32_bits((u64)ptr);
276 /* At the end of the ring, link the last BD back to the top */
277 priv->tx_bd[1]->next_desc = lower_32_bits((u64)priv->tx_bd[0]);
278 priv->tx_bd[1]->buf_addr = lower_32_bits((u64)ptr + len / 2);
280 if (IS_ENABLED(CONFIG_PHYS_64BIT)) {
281 priv->tx_bd[0]->next_desc_msb = upper_32_bits((u64)priv->tx_bd[1]);
282 priv->tx_bd[0]->buf_addr_msb = upper_32_bits((u64)ptr);
284 priv->tx_bd[1]->next_desc_msb = upper_32_bits((u64)priv->tx_bd[0]);
285 priv->tx_bd[1]->buf_addr_msb = upper_32_bits((u64)ptr + len / 2);
288 /* Split Tx data in to half and send in two descriptors */
289 priv->tx_bd[0]->cntrl = (len / 2) | XMCDMA_BD_CTRL_TXSOF_MASK;
290 priv->tx_bd[1]->cntrl = (len - len / 2) | XMCDMA_BD_CTRL_TXEOF_MASK;
292 /* Flush the last BD so DMA core could see the updates */
293 flush_cache((phys_addr_t)priv->tx_bd[0], TX_BD_TOTAL_SIZE);
295 if (readl(&priv->mcdma_tx->status) & XMCDMA_CH_IDLE) {
296 axi_mrmac_dma_write(priv->tx_bd[0], &priv->mcdma_tx->current);
298 setbits_le32(&priv->mcdma_tx->control, XMCDMA_CR_RUNSTOP_MASK);
300 log_warning("Error: current desc is not updated\n");
304 setbits_le32(&priv->mcdma_tx->control, XMCDMA_IRQ_ALL_MASK);
307 axi_mrmac_dma_write(priv->tx_bd[1], &priv->mcdma_tx->tail);
309 /* Wait for transmission to complete */
310 ret = wait_for_bit_le32(&priv->mcdma_tx->status, XMCDMA_IRQ_IOC_MASK,
313 log_warning("%s: Timeout\n", __func__);
318 priv->tx_bd[0]->sband_stats = 0;
319 priv->tx_bd[1]->sband_stats = 0;
321 log_debug("Sending complete\n");
326 static bool isrxready(struct axi_mrmac_priv *priv)
330 /* Read pending interrupts */
331 status = readl(&priv->mcdma_rx->status);
333 /* Acknowledge pending interrupts */
334 writel(status & XMCDMA_IRQ_ALL_MASK, &priv->mcdma_rx->status);
337 * If Reception done interrupt is asserted, call Rx call back function
338 * to handle the processed BDs and then raise the according flag.
340 if (status & (XMCDMA_IRQ_IOC_MASK | XMCDMA_IRQ_DELAY_MASK))
347 * axi_mrmac_recv - MRMAC Rx function
348 * @dev: udevice structure
349 * @flags: flags from network stack
350 * @packetp pointer to received data
352 * Return: received data length on success, negative value on errors
354 * This is a Rx function of MRMAC. Check if any data is received on MCDMA.
355 * Copy buffer pointer to packetp and return received data length.
357 static int axi_mrmac_recv(struct udevice *dev, int flags, uchar **packetp)
359 struct axi_mrmac_priv *priv = dev_get_priv(dev);
363 /* Wait for an incoming packet */
364 if (!isrxready(priv))
367 /* Clear all interrupts */
368 writel(XMCDMA_IRQ_ALL_MASK, &priv->mcdma_rx->status);
370 /* Disable IRQ for a moment till packet is handled */
371 clrbits_le32(&priv->mcdma_rx->control, XMCDMA_IRQ_ALL_MASK);
373 /* Disable channel fetch */
374 clrbits_le32(&priv->mcdma_rx->control, XMCDMA_CR_RUNSTOP_MASK);
376 rx_bd_end = (ulong)priv->rx_bd[0] + roundup(RX_BD_TOTAL_SIZE,
378 /* Invalidate Rx descriptors to see proper Rx length */
379 invalidate_dcache_range((phys_addr_t)priv->rx_bd[0], rx_bd_end);
381 length = priv->rx_bd[0]->status & XMCDMA_BD_STS_ACTUAL_LEN_MASK;
382 *packetp = (uchar *)(ulong)priv->rx_bd[0]->buf_addr;
385 length = priv->rx_bd[1]->status & XMCDMA_BD_STS_ACTUAL_LEN_MASK;
386 *packetp = (uchar *)(ulong)priv->rx_bd[1]->buf_addr;
390 print_buffer(*packetp, *packetp, 1, length, 16);
393 priv->rx_bd[0]->status = 0;
394 priv->rx_bd[1]->status = 0;
400 * axi_mrmac_free_pkt - MRMAC free packet function
401 * @dev: udevice structure
402 * @packet: receive buffer pointer
403 * @length received data length
405 * Return: 0 on success, negative value on errors
407 * This is Rx free packet function of MRMAC. Prepare MRMAC for reception of
408 * data again. Invalidate previous data from Rx buffers and set Rx buffer
409 * descriptors. Trigger reception by updating tail descriptor.
411 static int axi_mrmac_free_pkt(struct udevice *dev, uchar *packet, int length)
413 struct axi_mrmac_priv *priv = dev_get_priv(dev);
416 /* It is useful to clear buffer to be sure that it is consistent */
417 memset(priv->rx_bd[0]->buf_addr, 0, RX_BUFF_TOTAL_SIZE);
419 /* Disable all Rx interrupts before RxBD space setup */
420 clrbits_le32(&priv->mcdma_rx->control, XMCDMA_IRQ_ALL_MASK);
422 /* Disable channel fetch */
423 clrbits_le32(&priv->mcdma_rx->control, XMCDMA_CR_RUNSTOP_MASK);
425 /* Update current descriptor */
426 axi_mrmac_dma_write(priv->rx_bd[0], &priv->mcdma_rx->current);
429 flush_cache((phys_addr_t)priv->rx_bd[0], RX_BD_TOTAL_SIZE);
431 /* It is necessary to flush rx buffers because if you don't do it
432 * then cache will contain previous packet
434 flush_cache((phys_addr_t)priv->rx_bd[0]->buf_addr, RX_BUFF_TOTAL_SIZE);
437 setbits_le32(&priv->mcdma_rx->control, XMCDMA_IRQ_ALL_MASK);
440 setbits_le32(&priv->mcdma_rx->control, XMCDMA_CR_RUNSTOP_MASK);
442 /* Update tail descriptor. Now it's ready to receive data */
443 axi_mrmac_dma_write(priv->rx_bd[1], &priv->mcdma_rx->tail);
445 log_debug("Rx completed, framelength = %x\n", length);
451 * axi_mrmac_stop - Stop MCDMA transfers
452 * @dev: udevice structure
454 * Return: 0 on success, negative value on errors
456 * Stop MCDMA engine for both Tx and Rx transfers.
458 static void axi_mrmac_stop(struct udevice *dev)
460 struct axi_mrmac_priv *priv = dev_get_priv(dev);
462 /* Stop the hardware */
463 clrbits_le32(&priv->mcdma_tx->control, XMCDMA_CR_RUNSTOP_MASK);
464 clrbits_le32(&priv->mcdma_rx->control, XMCDMA_CR_RUNSTOP_MASK);
466 log_debug("Halted\n");
469 static int axi_mrmac_probe(struct udevice *dev)
471 struct axi_mrmac_plat *plat = dev_get_plat(dev);
472 struct eth_pdata *pdata = &plat->eth_pdata;
473 struct axi_mrmac_priv *priv = dev_get_priv(dev);
475 priv->iobase = (struct mrmac_regs *)pdata->iobase;
477 priv->mm2s_cmn = plat->mm2s_cmn;
478 priv->mcdma_tx = (struct mcdma_chan_reg *)((phys_addr_t)priv->mm2s_cmn
479 + XMCDMA_CHAN_OFFSET);
480 priv->s2mm_cmn = (struct mcdma_common_regs *)((phys_addr_t)priv->mm2s_cmn
482 priv->mcdma_rx = (struct mcdma_chan_reg *)((phys_addr_t)priv->s2mm_cmn
483 + XMCDMA_CHAN_OFFSET);
484 priv->mrmac_rate = plat->mrmac_rate;
486 /* Align buffers to ARCH_DMA_MINALIGN */
487 priv->tx_bd[0] = memalign(ARCH_DMA_MINALIGN, TX_BD_TOTAL_SIZE);
488 priv->tx_bd[1] = (struct mcdma_bd *)((ulong)priv->tx_bd[0] +
489 sizeof(struct mcdma_bd));
491 priv->rx_bd[0] = memalign(ARCH_DMA_MINALIGN, RX_BD_TOTAL_SIZE);
492 priv->rx_bd[1] = (struct mcdma_bd *)((ulong)priv->rx_bd[0] +
493 sizeof(struct mcdma_bd));
495 priv->txminframe = memalign(ARCH_DMA_MINALIGN, MIN_PKT_SIZE);
500 static int axi_mrmac_remove(struct udevice *dev)
502 struct axi_mrmac_priv *priv = dev_get_priv(dev);
504 /* Free buffer descriptors */
505 free(priv->tx_bd[0]);
506 free(priv->rx_bd[0]);
507 free(priv->txminframe);
512 static int axi_mrmac_of_to_plat(struct udevice *dev)
514 struct axi_mrmac_plat *plat = dev_get_plat(dev);
515 struct eth_pdata *pdata = &plat->eth_pdata;
516 struct ofnode_phandle_args phandle_args;
519 pdata->iobase = dev_read_addr(dev);
521 ret = dev_read_phandle_with_args(dev, "axistream-connected", NULL, 0, 0,
524 log_debug("axistream not found\n");
528 plat->mm2s_cmn = (struct mcdma_common_regs *)ofnode_read_u64_default
529 (phandle_args.node, "reg", -1);
530 if (!plat->mm2s_cmn) {
531 log_warning("MRMAC dma register space not found\n");
535 /* Set default MRMAC rate to 10000 */
536 plat->mrmac_rate = dev_read_u32_default(dev, "xlnx,mrmac-rate", 10000);
541 static const struct eth_ops axi_mrmac_ops = {
542 .start = axi_mrmac_start,
543 .send = axi_mrmac_send,
544 .recv = axi_mrmac_recv,
545 .free_pkt = axi_mrmac_free_pkt,
546 .stop = axi_mrmac_stop,
549 static const struct udevice_id axi_mrmac_ids[] = {
550 { .compatible = "xlnx,mrmac-ethernet-1.0" },
554 U_BOOT_DRIVER(axi_mrmac) = {
557 .of_match = axi_mrmac_ids,
558 .of_to_plat = axi_mrmac_of_to_plat,
559 .probe = axi_mrmac_probe,
560 .remove = axi_mrmac_remove,
561 .ops = &axi_mrmac_ops,
562 .priv_auto = sizeof(struct axi_mrmac_priv),
563 .plat_auto = sizeof(struct axi_mrmac_plat),