Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 | // SPDX-License-Identifier: GPL-2.0 /* * Xilinx Multirate Ethernet MAC(MRMAC) driver * * Author(s): Ashok Reddy Soma <ashok.reddy.soma@xilinx.com> * Michal Simek <michal.simek@amd.com> * * Copyright (C) 2021 Xilinx, Inc. All rights reserved. */ #include <config.h> #include <cpu_func.h> #include <dm.h> #include <log.h> #include <net.h> #include <malloc.h> #include <wait_bit.h> #include <asm/io.h> #include <linux/delay.h> #include <linux/ethtool.h> #include "xilinx_axi_mrmac.h" static void axi_mrmac_dma_write(struct mcdma_bd *bd, u32 *desc) { if (IS_ENABLED(CONFIG_PHYS_64BIT)) writeq((unsigned long)bd, desc); else writel((uintptr_t)bd, desc); } /** * axi_mrmac_ethernet_init - MRMAC init function * @priv: MRMAC private structure * * Return: 0 on success, negative value on errors * * This function is called to reset and initialize MRMAC core. This is * typically called during initialization. It does a reset of MRMAC Rx/Tx * channels and Rx/Tx SERDES. It configures MRMAC speed based on mrmac_rate * which is read from DT. This function waits for block lock bit to get set, * if it is not set within 100ms time returns a timeout error. */ static int axi_mrmac_ethernet_init(struct axi_mrmac_priv *priv) { struct mrmac_regs *regs = priv->iobase; u32 reg; u32 ret; /* Perform all the RESET's required */ setbits_le32(®s->reset, MRMAC_RX_SERDES_RST_MASK | MRMAC_RX_RST_MASK | MRMAC_TX_SERDES_RST_MASK | MRMAC_TX_RST_MASK); mdelay(MRMAC_RESET_DELAY); /* Configure Mode register */ reg = readl(®s->mode); log_debug("Configuring MRMAC speed to %d\n", priv->mrmac_rate); if (priv->mrmac_rate == SPEED_25000) { reg &= ~MRMAC_CTL_RATE_CFG_MASK; reg |= MRMAC_CTL_DATA_RATE_25G; reg |= (MRMAC_CTL_AXIS_CFG_25G_IND << MRMAC_CTL_AXIS_CFG_SHIFT); reg |= (MRMAC_CTL_SERDES_WIDTH_25G << MRMAC_CTL_SERDES_WIDTH_SHIFT); } else { reg &= ~MRMAC_CTL_RATE_CFG_MASK; reg |= MRMAC_CTL_DATA_RATE_10G; reg |= (MRMAC_CTL_AXIS_CFG_10G_IND << MRMAC_CTL_AXIS_CFG_SHIFT); reg |= (MRMAC_CTL_SERDES_WIDTH_10G << MRMAC_CTL_SERDES_WIDTH_SHIFT); } /* For tick reg */ reg |= MRMAC_CTL_PM_TICK_MASK; writel(reg, ®s->mode); clrbits_le32(®s->reset, MRMAC_RX_SERDES_RST_MASK | MRMAC_RX_RST_MASK | MRMAC_TX_SERDES_RST_MASK | MRMAC_TX_RST_MASK); mdelay(MRMAC_RESET_DELAY); /* Setup MRMAC hardware options */ setbits_le32(®s->rx_config, MRMAC_RX_DEL_FCS_MASK); setbits_le32(®s->tx_config, MRMAC_TX_INS_FCS_MASK); setbits_le32(®s->tx_config, MRMAC_TX_EN_MASK); setbits_le32(®s->rx_config, MRMAC_RX_EN_MASK); /* Check for block lock bit to be set. This ensures that * MRMAC ethernet IP is functioning normally. */ writel(MRMAC_STS_ALL_MASK, (phys_addr_t)priv->iobase + MRMAC_TX_STS_OFFSET); writel(MRMAC_STS_ALL_MASK, (phys_addr_t)priv->iobase + MRMAC_RX_STS_OFFSET); writel(MRMAC_STS_ALL_MASK, (phys_addr_t)priv->iobase + MRMAC_STATRX_BLKLCK_OFFSET); ret = wait_for_bit_le32((u32 *)((phys_addr_t)priv->iobase + MRMAC_STATRX_BLKLCK_OFFSET), MRMAC_RX_BLKLCK_MASK, true, MRMAC_BLKLCK_TIMEOUT, true); if (ret) { log_warning("Error: MRMAC block lock not complete!\n"); return -EIO; } writel(MRMAC_TICK_TRIGGER, ®s->tick_reg); return 0; } /** * axi_mcdma_init - Reset MCDMA engine * @priv: MRMAC private structure * * Return: 0 on success, negative value on timeouts * * This function is called to reset and initialize MCDMA engine */ static int axi_mcdma_init(struct axi_mrmac_priv *priv) { u32 ret; /* Reset the engine so the hardware starts from a known state */ writel(XMCDMA_CR_RESET, &priv->mm2s_cmn->control); writel(XMCDMA_CR_RESET, &priv->s2mm_cmn->control); /* Check Tx/Rx MCDMA.RST. Reset is done when the reset bit is low */ ret = wait_for_bit_le32(&priv->mm2s_cmn->control, XMCDMA_CR_RESET, false, MRMAC_DMARST_TIMEOUT, true); if (ret) { log_warning("Tx MCDMA reset Timeout\n"); return -ETIMEDOUT; } ret = wait_for_bit_le32(&priv->s2mm_cmn->control, XMCDMA_CR_RESET, false, MRMAC_DMARST_TIMEOUT, true); if (ret) { log_warning("Rx MCDMA reset Timeout\n"); return -ETIMEDOUT; } /* Enable channel 1 for Tx and Rx */ writel(XMCDMA_CHANNEL_1, &priv->mm2s_cmn->chen); writel(XMCDMA_CHANNEL_1, &priv->s2mm_cmn->chen); return 0; } /** * axi_mrmac_start - MRMAC start * @dev: udevice structure * * Return: 0 on success, negative value on errors * * This is a initialization function of MRMAC. Call MCDMA initialization * function and setup Rx buffer descriptors for starting reception of packets. * Enable Tx and Rx channels and trigger Rx channel fetch. */ static int axi_mrmac_start(struct udevice *dev) { struct axi_mrmac_priv *priv = dev_get_priv(dev); struct mrmac_regs *regs = priv->iobase; /* * Initialize MCDMA engine. MCDMA engine must be initialized before * MRMAC. During MCDMA engine initialization, MCDMA hardware is reset, * since MCDMA reset line is connected to MRMAC, this would ensure a * reset of MRMAC. */ axi_mcdma_init(priv); /* Initialize MRMAC hardware */ if (axi_mrmac_ethernet_init(priv)) return -EIO; /* Disable all Rx interrupts before RxBD space setup */ clrbits_le32(&priv->mcdma_rx->control, XMCDMA_IRQ_ALL_MASK); /* Update current descriptor */ axi_mrmac_dma_write(priv->rx_bd[0], &priv->mcdma_rx->current); /* Setup Rx BD. MRMAC needs atleast two descriptors */ memset(priv->rx_bd[0], 0, RX_BD_TOTAL_SIZE); priv->rx_bd[0]->next_desc = lower_32_bits((u64)priv->rx_bd[1]); priv->rx_bd[0]->buf_addr = lower_32_bits((u64)net_rx_packets[0]); priv->rx_bd[1]->next_desc = lower_32_bits((u64)priv->rx_bd[0]); priv->rx_bd[1]->buf_addr = lower_32_bits((u64)net_rx_packets[1]); if (IS_ENABLED(CONFIG_PHYS_64BIT)) { priv->rx_bd[0]->next_desc_msb = upper_32_bits((u64)priv->rx_bd[1]); priv->rx_bd[0]->buf_addr_msb = upper_32_bits((u64)net_rx_packets[0]); priv->rx_bd[1]->next_desc_msb = upper_32_bits((u64)priv->rx_bd[0]); priv->rx_bd[1]->buf_addr_msb = upper_32_bits((u64)net_rx_packets[1]); } priv->rx_bd[0]->cntrl = PKTSIZE_ALIGN; priv->rx_bd[1]->cntrl = PKTSIZE_ALIGN; /* Flush the last BD so DMA core could see the updates */ flush_cache((phys_addr_t)priv->rx_bd[0], RX_BD_TOTAL_SIZE); /* It is necessary to flush rx buffers because if you don't do it * then cache can contain uninitialized data */ flush_cache((phys_addr_t)priv->rx_bd[0]->buf_addr, RX_BUFF_TOTAL_SIZE); /* Start the hardware */ setbits_le32(&priv->s2mm_cmn->control, XMCDMA_CR_RUNSTOP_MASK); setbits_le32(&priv->mm2s_cmn->control, XMCDMA_CR_RUNSTOP_MASK); setbits_le32(&priv->mcdma_rx->control, XMCDMA_IRQ_ALL_MASK); /* Channel fetch */ setbits_le32(&priv->mcdma_rx->control, XMCDMA_CR_RUNSTOP_MASK); /* Update tail descriptor. Now it's ready to receive data */ axi_mrmac_dma_write(priv->rx_bd[1], &priv->mcdma_rx->tail); /* Enable Tx */ setbits_le32(®s->tx_config, MRMAC_TX_EN_MASK); /* Enable Rx */ setbits_le32(®s->rx_config, MRMAC_RX_EN_MASK); return 0; } /** * axi_mrmac_send - MRMAC Tx function * @dev: udevice structure * @ptr: pointer to Tx buffer * @len: transfer length * * Return: 0 on success, negative value on errors * * This is a Tx send function of MRMAC. Setup Tx buffer descriptors and trigger * transfer. Wait till the data is transferred. */ static int axi_mrmac_send(struct udevice *dev, void *ptr, int len) { struct axi_mrmac_priv *priv = dev_get_priv(dev); u32 ret; #ifdef DEBUG print_buffer(ptr, ptr, 1, len, 16); #endif if (len > PKTSIZE_ALIGN) len = PKTSIZE_ALIGN; /* If size is less than min packet size, pad to min size */ if (len < MIN_PKT_SIZE) { memset(priv->txminframe, 0, MIN_PKT_SIZE); memcpy(priv->txminframe, ptr, len); len = MIN_PKT_SIZE; ptr = priv->txminframe; } writel(XMCDMA_IRQ_ALL_MASK, &priv->mcdma_tx->status); clrbits_le32(&priv->mcdma_tx->control, XMCDMA_CR_RUNSTOP_MASK); /* Flush packet to main memory to be trasfered by DMA */ flush_cache((phys_addr_t)ptr, len); /* Setup Tx BD. MRMAC needs atleast two descriptors */ memset(priv->tx_bd[0], 0, TX_BD_TOTAL_SIZE); priv->tx_bd[0]->next_desc = lower_32_bits((u64)priv->tx_bd[1]); priv->tx_bd[0]->buf_addr = lower_32_bits((u64)ptr); /* At the end of the ring, link the last BD back to the top */ priv->tx_bd[1]->next_desc = lower_32_bits((u64)priv->tx_bd[0]); priv->tx_bd[1]->buf_addr = lower_32_bits((u64)ptr + len / 2); if (IS_ENABLED(CONFIG_PHYS_64BIT)) { priv->tx_bd[0]->next_desc_msb = upper_32_bits((u64)priv->tx_bd[1]); priv->tx_bd[0]->buf_addr_msb = upper_32_bits((u64)ptr); priv->tx_bd[1]->next_desc_msb = upper_32_bits((u64)priv->tx_bd[0]); priv->tx_bd[1]->buf_addr_msb = upper_32_bits((u64)ptr + len / 2); } /* Split Tx data in to half and send in two descriptors */ priv->tx_bd[0]->cntrl = (len / 2) | XMCDMA_BD_CTRL_TXSOF_MASK; priv->tx_bd[1]->cntrl = (len - len / 2) | XMCDMA_BD_CTRL_TXEOF_MASK; /* Flush the last BD so DMA core could see the updates */ flush_cache((phys_addr_t)priv->tx_bd[0], TX_BD_TOTAL_SIZE); if (readl(&priv->mcdma_tx->status) & XMCDMA_CH_IDLE) { axi_mrmac_dma_write(priv->tx_bd[0], &priv->mcdma_tx->current); /* Channel fetch */ setbits_le32(&priv->mcdma_tx->control, XMCDMA_CR_RUNSTOP_MASK); } else { log_warning("Error: current desc is not updated\n"); return -EIO; } setbits_le32(&priv->mcdma_tx->control, XMCDMA_IRQ_ALL_MASK); /* Start transfer */ axi_mrmac_dma_write(priv->tx_bd[1], &priv->mcdma_tx->tail); /* Wait for transmission to complete */ ret = wait_for_bit_le32(&priv->mcdma_tx->status, XMCDMA_IRQ_IOC_MASK, true, 1, true); if (ret) { log_warning("%s: Timeout\n", __func__); return -ETIMEDOUT; } /* Clear status */ priv->tx_bd[0]->sband_stats = 0; priv->tx_bd[1]->sband_stats = 0; log_debug("Sending complete\n"); return 0; } static bool isrxready(struct axi_mrmac_priv *priv) { u32 status; /* Read pending interrupts */ status = readl(&priv->mcdma_rx->status); /* Acknowledge pending interrupts */ writel(status & XMCDMA_IRQ_ALL_MASK, &priv->mcdma_rx->status); /* * If Reception done interrupt is asserted, call Rx call back function * to handle the processed BDs and then raise the according flag. */ if (status & (XMCDMA_IRQ_IOC_MASK | XMCDMA_IRQ_DELAY_MASK)) return 1; return 0; } /** * axi_mrmac_recv - MRMAC Rx function * @dev: udevice structure * @flags: flags from network stack * @packetp: pointer to received data * * Return: received data length on success, negative value on errors * * This is a Rx function of MRMAC. Check if any data is received on MCDMA. * Copy buffer pointer to packetp and return received data length. */ static int axi_mrmac_recv(struct udevice *dev, int flags, uchar **packetp) { struct axi_mrmac_priv *priv = dev_get_priv(dev); u32 rx_bd_end; u32 length; /* Wait for an incoming packet */ if (!isrxready(priv)) return -EAGAIN; /* Clear all interrupts */ writel(XMCDMA_IRQ_ALL_MASK, &priv->mcdma_rx->status); /* Disable IRQ for a moment till packet is handled */ clrbits_le32(&priv->mcdma_rx->control, XMCDMA_IRQ_ALL_MASK); /* Disable channel fetch */ clrbits_le32(&priv->mcdma_rx->control, XMCDMA_CR_RUNSTOP_MASK); rx_bd_end = (ulong)priv->rx_bd[0] + roundup(RX_BD_TOTAL_SIZE, ARCH_DMA_MINALIGN); /* Invalidate Rx descriptors to see proper Rx length */ invalidate_dcache_range((phys_addr_t)priv->rx_bd[0], rx_bd_end); length = priv->rx_bd[0]->status & XMCDMA_BD_STS_ACTUAL_LEN_MASK; *packetp = (uchar *)(ulong)priv->rx_bd[0]->buf_addr; if (!length) { length = priv->rx_bd[1]->status & XMCDMA_BD_STS_ACTUAL_LEN_MASK; *packetp = (uchar *)(ulong)priv->rx_bd[1]->buf_addr; } #ifdef DEBUG print_buffer(*packetp, *packetp, 1, length, 16); #endif /* Clear status */ priv->rx_bd[0]->status = 0; priv->rx_bd[1]->status = 0; return length; } /** * axi_mrmac_free_pkt - MRMAC free packet function * @dev: udevice structure * @packet: receive buffer pointer * @length: received data length * * Return: 0 on success, negative value on errors * * This is Rx free packet function of MRMAC. Prepare MRMAC for reception of * data again. Invalidate previous data from Rx buffers and set Rx buffer * descriptors. Trigger reception by updating tail descriptor. */ static int axi_mrmac_free_pkt(struct udevice *dev, uchar *packet, int length) { struct axi_mrmac_priv *priv = dev_get_priv(dev); #ifdef DEBUG /* It is useful to clear buffer to be sure that it is consistent */ memset(priv->rx_bd[0]->buf_addr, 0, RX_BUFF_TOTAL_SIZE); #endif /* Disable all Rx interrupts before RxBD space setup */ clrbits_le32(&priv->mcdma_rx->control, XMCDMA_IRQ_ALL_MASK); /* Disable channel fetch */ clrbits_le32(&priv->mcdma_rx->control, XMCDMA_CR_RUNSTOP_MASK); /* Update current descriptor */ axi_mrmac_dma_write(priv->rx_bd[0], &priv->mcdma_rx->current); /* Write bd to HW */ flush_cache((phys_addr_t)priv->rx_bd[0], RX_BD_TOTAL_SIZE); /* It is necessary to flush rx buffers because if you don't do it * then cache will contain previous packet */ flush_cache((phys_addr_t)priv->rx_bd[0]->buf_addr, RX_BUFF_TOTAL_SIZE); /* Enable all IRQ */ setbits_le32(&priv->mcdma_rx->control, XMCDMA_IRQ_ALL_MASK); /* Channel fetch */ setbits_le32(&priv->mcdma_rx->control, XMCDMA_CR_RUNSTOP_MASK); /* Update tail descriptor. Now it's ready to receive data */ axi_mrmac_dma_write(priv->rx_bd[1], &priv->mcdma_rx->tail); log_debug("Rx completed, framelength = %x\n", length); return 0; } /** * axi_mrmac_stop - Stop MCDMA transfers * @dev: udevice structure * * Return: 0 on success, negative value on errors * * Stop MCDMA engine for both Tx and Rx transfers. */ static void axi_mrmac_stop(struct udevice *dev) { struct axi_mrmac_priv *priv = dev_get_priv(dev); /* Stop the hardware */ clrbits_le32(&priv->mcdma_tx->control, XMCDMA_CR_RUNSTOP_MASK); clrbits_le32(&priv->mcdma_rx->control, XMCDMA_CR_RUNSTOP_MASK); log_debug("Halted\n"); } static int axi_mrmac_probe(struct udevice *dev) { struct axi_mrmac_plat *plat = dev_get_plat(dev); struct eth_pdata *pdata = &plat->eth_pdata; struct axi_mrmac_priv *priv = dev_get_priv(dev); priv->iobase = (struct mrmac_regs *)pdata->iobase; priv->mm2s_cmn = plat->mm2s_cmn; priv->mcdma_tx = (struct mcdma_chan_reg *)((phys_addr_t)priv->mm2s_cmn + XMCDMA_CHAN_OFFSET); priv->s2mm_cmn = (struct mcdma_common_regs *)((phys_addr_t)priv->mm2s_cmn + XMCDMA_RX_OFFSET); priv->mcdma_rx = (struct mcdma_chan_reg *)((phys_addr_t)priv->s2mm_cmn + XMCDMA_CHAN_OFFSET); priv->mrmac_rate = plat->mrmac_rate; /* Align buffers to ARCH_DMA_MINALIGN */ priv->tx_bd[0] = memalign(ARCH_DMA_MINALIGN, TX_BD_TOTAL_SIZE); priv->tx_bd[1] = (struct mcdma_bd *)((ulong)priv->tx_bd[0] + sizeof(struct mcdma_bd)); priv->rx_bd[0] = memalign(ARCH_DMA_MINALIGN, RX_BD_TOTAL_SIZE); priv->rx_bd[1] = (struct mcdma_bd *)((ulong)priv->rx_bd[0] + sizeof(struct mcdma_bd)); priv->txminframe = memalign(ARCH_DMA_MINALIGN, MIN_PKT_SIZE); return 0; } static int axi_mrmac_remove(struct udevice *dev) { struct axi_mrmac_priv *priv = dev_get_priv(dev); /* Free buffer descriptors */ free(priv->tx_bd[0]); free(priv->rx_bd[0]); free(priv->txminframe); return 0; } static int axi_mrmac_of_to_plat(struct udevice *dev) { struct axi_mrmac_plat *plat = dev_get_plat(dev); struct eth_pdata *pdata = &plat->eth_pdata; struct ofnode_phandle_args phandle_args; int ret = 0; pdata->iobase = dev_read_addr(dev); ret = dev_read_phandle_with_args(dev, "axistream-connected", NULL, 0, 0, &phandle_args); if (ret) { log_debug("axistream not found\n"); return -EINVAL; } plat->mm2s_cmn = (struct mcdma_common_regs *)ofnode_read_u64_default (phandle_args.node, "reg", -1); if (!plat->mm2s_cmn) { log_warning("MRMAC dma register space not found\n"); return -EINVAL; } /* Set default MRMAC rate to 10000 */ plat->mrmac_rate = dev_read_u32_default(dev, "xlnx,mrmac-rate", 10000); return 0; } static const struct eth_ops axi_mrmac_ops = { .start = axi_mrmac_start, .send = axi_mrmac_send, .recv = axi_mrmac_recv, .free_pkt = axi_mrmac_free_pkt, .stop = axi_mrmac_stop, }; static const struct udevice_id axi_mrmac_ids[] = { { .compatible = "xlnx,mrmac-ethernet-1.0" }, { } }; U_BOOT_DRIVER(axi_mrmac) = { .name = "axi_mrmac", .id = UCLASS_ETH, .of_match = axi_mrmac_ids, .of_to_plat = axi_mrmac_of_to_plat, .probe = axi_mrmac_probe, .remove = axi_mrmac_remove, .ops = &axi_mrmac_ops, .priv_auto = sizeof(struct axi_mrmac_priv), .plat_auto = sizeof(struct axi_mrmac_plat), }; |