1 // SPDX-License-Identifier: GPL-2.0 2 /* Renesas Ethernet AVB device driver 3 * 4 * Copyright (C) 2014-2019 Renesas Electronics Corporation 5 * Copyright (C) 2015 Renesas Solutions Corp. 6 * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com> 7 * 8 * Based on the SuperH Ethernet driver 9 */ 10 11 #include <linux/cache.h> 12 #include <linux/clk.h> 13 #include <linux/delay.h> 14 #include <linux/dma-mapping.h> 15 #include <linux/err.h> 16 #include <linux/etherdevice.h> 17 #include <linux/ethtool.h> 18 #include <linux/if_vlan.h> 19 #include <linux/kernel.h> 20 #include <linux/list.h> 21 #include <linux/module.h> 22 #include <linux/net_tstamp.h> 23 #include <linux/of.h> 24 #include <linux/of_mdio.h> 25 #include <linux/of_net.h> 26 #include <linux/platform_device.h> 27 #include <linux/pm_runtime.h> 28 #include <linux/slab.h> 29 #include <linux/spinlock.h> 30 #include <linux/reset.h> 31 #include <linux/math64.h> 32 #include <net/ip.h> 33 34 #include "ravb.h" 35 36 #define RAVB_DEF_MSG_ENABLE \ 37 (NETIF_MSG_LINK | \ 38 NETIF_MSG_TIMER | \ 39 NETIF_MSG_RX_ERR | \ 40 NETIF_MSG_TX_ERR) 41 42 void ravb_modify(struct net_device *ndev, enum ravb_reg reg, u32 clear, 43 u32 set) 44 { 45 ravb_write(ndev, (ravb_read(ndev, reg) & ~clear) | set, reg); 46 } 47 48 int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value) 49 { 50 int i; 51 52 for (i = 0; i < 10000; i++) { 53 if ((ravb_read(ndev, reg) & mask) == value) 54 return 0; 55 udelay(10); 56 } 57 return -ETIMEDOUT; 58 } 59 60 static int ravb_set_opmode(struct net_device *ndev, u32 opmode) 61 { 62 u32 csr_ops = 1U << (opmode & CCC_OPC); 63 u32 ccc_mask = CCC_OPC; 64 int error; 65 66 /* If gPTP active in config mode is supported it needs to be configured 67 * along with CSEL and operating mode in the same access. This is a 68 * hardware limitation. 69 */ 70 if (opmode & CCC_GAC) 71 ccc_mask |= CCC_GAC | CCC_CSEL; 72 73 /* Set operating mode */ 74 ravb_modify(ndev, CCC, ccc_mask, opmode); 75 /* Check if the operating mode is changed to the requested one */ 76 error = ravb_wait(ndev, CSR, CSR_OPS, csr_ops); 77 if (error) { 78 netdev_err(ndev, "failed to switch device to requested mode (%u)\n", 79 opmode & CCC_OPC); 80 } 81 82 return error; 83 } 84 85 static void ravb_set_rate_gbeth(struct net_device *ndev) 86 { 87 struct ravb_private *priv = netdev_priv(ndev); 88 89 switch (priv->speed) { 90 case 10: /* 10BASE */ 91 ravb_write(ndev, GBETH_GECMR_SPEED_10, GECMR); 92 break; 93 case 100: /* 100BASE */ 94 ravb_write(ndev, GBETH_GECMR_SPEED_100, GECMR); 95 break; 96 case 1000: /* 1000BASE */ 97 ravb_write(ndev, GBETH_GECMR_SPEED_1000, GECMR); 98 break; 99 } 100 } 101 102 static void ravb_set_rate_rcar(struct net_device *ndev) 103 { 104 struct ravb_private *priv = netdev_priv(ndev); 105 106 switch (priv->speed) { 107 case 100: /* 100BASE */ 108 ravb_write(ndev, GECMR_SPEED_100, GECMR); 109 break; 110 case 1000: /* 1000BASE */ 111 ravb_write(ndev, GECMR_SPEED_1000, GECMR); 112 break; 113 } 114 } 115 116 static void ravb_set_buffer_align(struct sk_buff *skb) 117 { 118 u32 reserve = (unsigned long)skb->data & (RAVB_ALIGN - 1); 119 120 if (reserve) 121 skb_reserve(skb, RAVB_ALIGN - reserve); 122 } 123 124 /* Get MAC address from the MAC address registers 125 * 126 * Ethernet AVB device doesn't have ROM for MAC address. 127 * This function gets the MAC address that was used by a bootloader. 128 */ 129 static void ravb_read_mac_address(struct device_node *np, 130 struct net_device *ndev) 131 { 132 int ret; 133 134 ret = of_get_ethdev_address(np, ndev); 135 if (ret) { 136 u32 mahr = ravb_read(ndev, MAHR); 137 u32 malr = ravb_read(ndev, MALR); 138 u8 addr[ETH_ALEN]; 139 140 addr[0] = (mahr >> 24) & 0xFF; 141 addr[1] = (mahr >> 16) & 0xFF; 142 addr[2] = (mahr >> 8) & 0xFF; 143 addr[3] = (mahr >> 0) & 0xFF; 144 addr[4] = (malr >> 8) & 0xFF; 145 addr[5] = (malr >> 0) & 0xFF; 146 eth_hw_addr_set(ndev, addr); 147 } 148 } 149 150 static void ravb_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set) 151 { 152 struct ravb_private *priv = container_of(ctrl, struct ravb_private, 153 mdiobb); 154 155 ravb_modify(priv->ndev, PIR, mask, set ? mask : 0); 156 } 157 158 /* MDC pin control */ 159 static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level) 160 { 161 ravb_mdio_ctrl(ctrl, PIR_MDC, level); 162 } 163 164 /* Data I/O pin control */ 165 static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output) 166 { 167 ravb_mdio_ctrl(ctrl, PIR_MMD, output); 168 } 169 170 /* Set data bit */ 171 static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value) 172 { 173 ravb_mdio_ctrl(ctrl, PIR_MDO, value); 174 } 175 176 /* Get data bit */ 177 static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl) 178 { 179 struct ravb_private *priv = container_of(ctrl, struct ravb_private, 180 mdiobb); 181 182 return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0; 183 } 184 185 /* MDIO bus control struct */ 186 static const struct mdiobb_ops bb_ops = { 187 .owner = THIS_MODULE, 188 .set_mdc = ravb_set_mdc, 189 .set_mdio_dir = ravb_set_mdio_dir, 190 .set_mdio_data = ravb_set_mdio_data, 191 .get_mdio_data = ravb_get_mdio_data, 192 }; 193 194 /* Free TX skb function for AVB-IP */ 195 static int ravb_tx_free(struct net_device *ndev, int q, bool free_txed_only) 196 { 197 struct ravb_private *priv = netdev_priv(ndev); 198 struct net_device_stats *stats = &priv->stats[q]; 199 unsigned int num_tx_desc = priv->num_tx_desc; 200 struct ravb_tx_desc *desc; 201 unsigned int entry; 202 int free_num = 0; 203 u32 size; 204 205 for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) { 206 bool txed; 207 208 entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] * 209 num_tx_desc); 210 desc = &priv->tx_ring[q][entry]; 211 txed = desc->die_dt == DT_FEMPTY; 212 if (free_txed_only && !txed) 213 break; 214 /* Descriptor type must be checked before all other reads */ 215 dma_rmb(); 216 size = le16_to_cpu(desc->ds_tagl) & TX_DS; 217 /* Free the original skb. */ 218 if (priv->tx_skb[q][entry / num_tx_desc]) { 219 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr), 220 size, DMA_TO_DEVICE); 221 /* Last packet descriptor? */ 222 if (entry % num_tx_desc == num_tx_desc - 1) { 223 entry /= num_tx_desc; 224 dev_kfree_skb_any(priv->tx_skb[q][entry]); 225 priv->tx_skb[q][entry] = NULL; 226 if (txed) 227 stats->tx_packets++; 228 } 229 free_num++; 230 } 231 if (txed) 232 stats->tx_bytes += size; 233 desc->die_dt = DT_EEMPTY; 234 } 235 return free_num; 236 } 237 238 static void ravb_rx_ring_free_gbeth(struct net_device *ndev, int q) 239 { 240 struct ravb_private *priv = netdev_priv(ndev); 241 unsigned int ring_size; 242 unsigned int i; 243 244 if (!priv->gbeth_rx_ring) 245 return; 246 247 for (i = 0; i < priv->num_rx_ring[q]; i++) { 248 struct ravb_rx_desc *desc = &priv->gbeth_rx_ring[i]; 249 250 if (!dma_mapping_error(ndev->dev.parent, 251 le32_to_cpu(desc->dptr))) 252 dma_unmap_single(ndev->dev.parent, 253 le32_to_cpu(desc->dptr), 254 GBETH_RX_BUFF_MAX, 255 DMA_FROM_DEVICE); 256 } 257 ring_size = sizeof(struct ravb_rx_desc) * (priv->num_rx_ring[q] + 1); 258 dma_free_coherent(ndev->dev.parent, ring_size, priv->gbeth_rx_ring, 259 priv->rx_desc_dma[q]); 260 priv->gbeth_rx_ring = NULL; 261 } 262 263 static void ravb_rx_ring_free_rcar(struct net_device *ndev, int q) 264 { 265 struct ravb_private *priv = netdev_priv(ndev); 266 unsigned int ring_size; 267 unsigned int i; 268 269 if (!priv->rx_ring[q]) 270 return; 271 272 for (i = 0; i < priv->num_rx_ring[q]; i++) { 273 struct ravb_ex_rx_desc *desc = &priv->rx_ring[q][i]; 274 275 if (!dma_mapping_error(ndev->dev.parent, 276 le32_to_cpu(desc->dptr))) 277 dma_unmap_single(ndev->dev.parent, 278 le32_to_cpu(desc->dptr), 279 RX_BUF_SZ, 280 DMA_FROM_DEVICE); 281 } 282 ring_size = sizeof(struct ravb_ex_rx_desc) * 283 (priv->num_rx_ring[q] + 1); 284 dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q], 285 priv->rx_desc_dma[q]); 286 priv->rx_ring[q] = NULL; 287 } 288 289 /* Free skb's and DMA buffers for Ethernet AVB */ 290 static void ravb_ring_free(struct net_device *ndev, int q) 291 { 292 struct ravb_private *priv = netdev_priv(ndev); 293 const struct ravb_hw_info *info = priv->info; 294 unsigned int num_tx_desc = priv->num_tx_desc; 295 unsigned int ring_size; 296 unsigned int i; 297 298 info->rx_ring_free(ndev, q); 299 300 if (priv->tx_ring[q]) { 301 ravb_tx_free(ndev, q, false); 302 303 ring_size = sizeof(struct ravb_tx_desc) * 304 (priv->num_tx_ring[q] * num_tx_desc + 1); 305 dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q], 306 priv->tx_desc_dma[q]); 307 priv->tx_ring[q] = NULL; 308 } 309 310 /* Free RX skb ringbuffer */ 311 if (priv->rx_skb[q]) { 312 for (i = 0; i < priv->num_rx_ring[q]; i++) 313 dev_kfree_skb(priv->rx_skb[q][i]); 314 } 315 kfree(priv->rx_skb[q]); 316 priv->rx_skb[q] = NULL; 317 318 /* Free aligned TX buffers */ 319 kfree(priv->tx_align[q]); 320 priv->tx_align[q] = NULL; 321 322 /* Free TX skb ringbuffer. 323 * SKBs are freed by ravb_tx_free() call above. 324 */ 325 kfree(priv->tx_skb[q]); 326 priv->tx_skb[q] = NULL; 327 } 328 329 static void ravb_rx_ring_format_gbeth(struct net_device *ndev, int q) 330 { 331 struct ravb_private *priv = netdev_priv(ndev); 332 struct ravb_rx_desc *rx_desc; 333 unsigned int rx_ring_size; 334 dma_addr_t dma_addr; 335 unsigned int i; 336 337 rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q]; 338 memset(priv->gbeth_rx_ring, 0, rx_ring_size); 339 /* Build RX ring buffer */ 340 for (i = 0; i < priv->num_rx_ring[q]; i++) { 341 /* RX descriptor */ 342 rx_desc = &priv->gbeth_rx_ring[i]; 343 rx_desc->ds_cc = cpu_to_le16(GBETH_RX_DESC_DATA_SIZE); 344 dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data, 345 GBETH_RX_BUFF_MAX, 346 DMA_FROM_DEVICE); 347 /* We just set the data size to 0 for a failed mapping which 348 * should prevent DMA from happening... 349 */ 350 if (dma_mapping_error(ndev->dev.parent, dma_addr)) 351 rx_desc->ds_cc = cpu_to_le16(0); 352 rx_desc->dptr = cpu_to_le32(dma_addr); 353 rx_desc->die_dt = DT_FEMPTY; 354 } 355 rx_desc = &priv->gbeth_rx_ring[i]; 356 rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]); 357 rx_desc->die_dt = DT_LINKFIX; /* type */ 358 } 359 360 static void ravb_rx_ring_format_rcar(struct net_device *ndev, int q) 361 { 362 struct ravb_private *priv = netdev_priv(ndev); 363 struct ravb_ex_rx_desc *rx_desc; 364 unsigned int rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q]; 365 dma_addr_t dma_addr; 366 unsigned int i; 367 368 memset(priv->rx_ring[q], 0, rx_ring_size); 369 /* Build RX ring buffer */ 370 for (i = 0; i < priv->num_rx_ring[q]; i++) { 371 /* RX descriptor */ 372 rx_desc = &priv->rx_ring[q][i]; 373 rx_desc->ds_cc = cpu_to_le16(RX_BUF_SZ); 374 dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data, 375 RX_BUF_SZ, 376 DMA_FROM_DEVICE); 377 /* We just set the data size to 0 for a failed mapping which 378 * should prevent DMA from happening... 379 */ 380 if (dma_mapping_error(ndev->dev.parent, dma_addr)) 381 rx_desc->ds_cc = cpu_to_le16(0); 382 rx_desc->dptr = cpu_to_le32(dma_addr); 383 rx_desc->die_dt = DT_FEMPTY; 384 } 385 rx_desc = &priv->rx_ring[q][i]; 386 rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]); 387 rx_desc->die_dt = DT_LINKFIX; /* type */ 388 } 389 390 /* Format skb and descriptor buffer for Ethernet AVB */ 391 static void ravb_ring_format(struct net_device *ndev, int q) 392 { 393 struct ravb_private *priv = netdev_priv(ndev); 394 const struct ravb_hw_info *info = priv->info; 395 unsigned int num_tx_desc = priv->num_tx_desc; 396 struct ravb_tx_desc *tx_desc; 397 struct ravb_desc *desc; 398 unsigned int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] * 399 num_tx_desc; 400 unsigned int i; 401 402 priv->cur_rx[q] = 0; 403 priv->cur_tx[q] = 0; 404 priv->dirty_rx[q] = 0; 405 priv->dirty_tx[q] = 0; 406 407 info->rx_ring_format(ndev, q); 408 409 memset(priv->tx_ring[q], 0, tx_ring_size); 410 /* Build TX ring buffer */ 411 for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q]; 412 i++, tx_desc++) { 413 tx_desc->die_dt = DT_EEMPTY; 414 if (num_tx_desc > 1) { 415 tx_desc++; 416 tx_desc->die_dt = DT_EEMPTY; 417 } 418 } 419 tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]); 420 tx_desc->die_dt = DT_LINKFIX; /* type */ 421 422 /* RX descriptor base address for best effort */ 423 desc = &priv->desc_bat[RX_QUEUE_OFFSET + q]; 424 desc->die_dt = DT_LINKFIX; /* type */ 425 desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]); 426 427 /* TX descriptor base address for best effort */ 428 desc = &priv->desc_bat[q]; 429 desc->die_dt = DT_LINKFIX; /* type */ 430 desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]); 431 } 432 433 static void *ravb_alloc_rx_desc_gbeth(struct net_device *ndev, int q) 434 { 435 struct ravb_private *priv = netdev_priv(ndev); 436 unsigned int ring_size; 437 438 ring_size = sizeof(struct ravb_rx_desc) * (priv->num_rx_ring[q] + 1); 439 440 priv->gbeth_rx_ring = dma_alloc_coherent(ndev->dev.parent, ring_size, 441 &priv->rx_desc_dma[q], 442 GFP_KERNEL); 443 return priv->gbeth_rx_ring; 444 } 445 446 static void *ravb_alloc_rx_desc_rcar(struct net_device *ndev, int q) 447 { 448 struct ravb_private *priv = netdev_priv(ndev); 449 unsigned int ring_size; 450 451 ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1); 452 453 priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size, 454 &priv->rx_desc_dma[q], 455 GFP_KERNEL); 456 return priv->rx_ring[q]; 457 } 458 459 /* Init skb and descriptor buffer for Ethernet AVB */ 460 static int ravb_ring_init(struct net_device *ndev, int q) 461 { 462 struct ravb_private *priv = netdev_priv(ndev); 463 const struct ravb_hw_info *info = priv->info; 464 unsigned int num_tx_desc = priv->num_tx_desc; 465 unsigned int ring_size; 466 struct sk_buff *skb; 467 unsigned int i; 468 469 /* Allocate RX and TX skb rings */ 470 priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q], 471 sizeof(*priv->rx_skb[q]), GFP_KERNEL); 472 priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q], 473 sizeof(*priv->tx_skb[q]), GFP_KERNEL); 474 if (!priv->rx_skb[q] || !priv->tx_skb[q]) 475 goto error; 476 477 for (i = 0; i < priv->num_rx_ring[q]; i++) { 478 skb = __netdev_alloc_skb(ndev, info->max_rx_len, GFP_KERNEL); 479 if (!skb) 480 goto error; 481 ravb_set_buffer_align(skb); 482 priv->rx_skb[q][i] = skb; 483 } 484 485 if (num_tx_desc > 1) { 486 /* Allocate rings for the aligned buffers */ 487 priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] + 488 DPTR_ALIGN - 1, GFP_KERNEL); 489 if (!priv->tx_align[q]) 490 goto error; 491 } 492 493 /* Allocate all RX descriptors. */ 494 if (!info->alloc_rx_desc(ndev, q)) 495 goto error; 496 497 priv->dirty_rx[q] = 0; 498 499 /* Allocate all TX descriptors. */ 500 ring_size = sizeof(struct ravb_tx_desc) * 501 (priv->num_tx_ring[q] * num_tx_desc + 1); 502 priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size, 503 &priv->tx_desc_dma[q], 504 GFP_KERNEL); 505 if (!priv->tx_ring[q]) 506 goto error; 507 508 return 0; 509 510 error: 511 ravb_ring_free(ndev, q); 512 513 return -ENOMEM; 514 } 515 516 static void ravb_csum_init_gbeth(struct net_device *ndev) 517 { 518 bool tx_enable = ndev->features & NETIF_F_HW_CSUM; 519 bool rx_enable = ndev->features & NETIF_F_RXCSUM; 520 521 if (!(tx_enable || rx_enable)) 522 goto done; 523 524 ravb_write(ndev, 0, CSR0); 525 if (ravb_wait(ndev, CSR0, CSR0_TPE | CSR0_RPE, 0)) { 526 netdev_err(ndev, "Timeout enabling hardware checksum\n"); 527 528 if (tx_enable) 529 ndev->features &= ~NETIF_F_HW_CSUM; 530 531 if (rx_enable) 532 ndev->features &= ~NETIF_F_RXCSUM; 533 } else { 534 if (tx_enable) 535 ravb_write(ndev, CSR1_TIP4 | CSR1_TTCP4 | CSR1_TUDP4, CSR1); 536 537 if (rx_enable) 538 ravb_write(ndev, CSR2_RIP4 | CSR2_RTCP4 | CSR2_RUDP4 | CSR2_RICMP4, 539 CSR2); 540 } 541 542 done: 543 ravb_write(ndev, CSR0_TPE | CSR0_RPE, CSR0); 544 } 545 546 static void ravb_emac_init_gbeth(struct net_device *ndev) 547 { 548 struct ravb_private *priv = netdev_priv(ndev); 549 550 if (priv->phy_interface == PHY_INTERFACE_MODE_MII) { 551 ravb_write(ndev, (1000 << 16) | CXR35_SEL_XMII_MII, CXR35); 552 ravb_modify(ndev, CXR31, CXR31_SEL_LINK0 | CXR31_SEL_LINK1, 0); 553 } else { 554 ravb_write(ndev, (1000 << 16) | CXR35_SEL_XMII_RGMII, CXR35); 555 ravb_modify(ndev, CXR31, CXR31_SEL_LINK0 | CXR31_SEL_LINK1, 556 CXR31_SEL_LINK0); 557 } 558 559 /* Receive frame limit set register */ 560 ravb_write(ndev, GBETH_RX_BUFF_MAX + ETH_FCS_LEN, RFLR); 561 562 /* EMAC Mode: PAUSE prohibition; Duplex; TX; RX; CRC Pass Through */ 563 ravb_write(ndev, ECMR_ZPF | ((priv->duplex > 0) ? ECMR_DM : 0) | 564 ECMR_TE | ECMR_RE | ECMR_RCPT | 565 ECMR_TXF | ECMR_RXF, ECMR); 566 567 ravb_set_rate_gbeth(ndev); 568 569 /* Set MAC address */ 570 ravb_write(ndev, 571 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) | 572 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR); 573 ravb_write(ndev, (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR); 574 575 /* E-MAC status register clear */ 576 ravb_write(ndev, ECSR_ICD | ECSR_LCHNG | ECSR_PFRI, ECSR); 577 578 ravb_csum_init_gbeth(ndev); 579 580 /* E-MAC interrupt enable register */ 581 ravb_write(ndev, ECSIPR_ICDIP, ECSIPR); 582 } 583 584 static void ravb_emac_init_rcar(struct net_device *ndev) 585 { 586 /* Receive frame limit set register */ 587 ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR); 588 589 /* EMAC Mode: PAUSE prohibition; Duplex; RX Checksum; TX; RX */ 590 ravb_write(ndev, ECMR_ZPF | ECMR_DM | 591 (ndev->features & NETIF_F_RXCSUM ? ECMR_RCSC : 0) | 592 ECMR_TE | ECMR_RE, ECMR); 593 594 ravb_set_rate_rcar(ndev); 595 596 /* Set MAC address */ 597 ravb_write(ndev, 598 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) | 599 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR); 600 ravb_write(ndev, 601 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR); 602 603 /* E-MAC status register clear */ 604 ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR); 605 606 /* E-MAC interrupt enable register */ 607 ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR); 608 } 609 610 /* E-MAC init function */ 611 static void ravb_emac_init(struct net_device *ndev) 612 { 613 struct ravb_private *priv = netdev_priv(ndev); 614 const struct ravb_hw_info *info = priv->info; 615 616 info->emac_init(ndev); 617 } 618 619 static int ravb_dmac_init_gbeth(struct net_device *ndev) 620 { 621 int error; 622 623 error = ravb_ring_init(ndev, RAVB_BE); 624 if (error) 625 return error; 626 627 /* Descriptor format */ 628 ravb_ring_format(ndev, RAVB_BE); 629 630 /* Set DMAC RX */ 631 ravb_write(ndev, 0x60000000, RCR); 632 633 /* Set Max Frame Length (RTC) */ 634 ravb_write(ndev, 0x7ffc0000 | GBETH_RX_BUFF_MAX, RTC); 635 636 /* Set FIFO size */ 637 ravb_write(ndev, 0x00222200, TGC); 638 639 ravb_write(ndev, 0, TCCR); 640 641 /* Frame receive */ 642 ravb_write(ndev, RIC0_FRE0, RIC0); 643 /* Disable FIFO full warning */ 644 ravb_write(ndev, 0x0, RIC1); 645 /* Receive FIFO full error, descriptor empty */ 646 ravb_write(ndev, RIC2_QFE0 | RIC2_RFFE, RIC2); 647 648 ravb_write(ndev, TIC_FTE0, TIC); 649 650 return 0; 651 } 652 653 static int ravb_dmac_init_rcar(struct net_device *ndev) 654 { 655 struct ravb_private *priv = netdev_priv(ndev); 656 const struct ravb_hw_info *info = priv->info; 657 int error; 658 659 error = ravb_ring_init(ndev, RAVB_BE); 660 if (error) 661 return error; 662 error = ravb_ring_init(ndev, RAVB_NC); 663 if (error) { 664 ravb_ring_free(ndev, RAVB_BE); 665 return error; 666 } 667 668 /* Descriptor format */ 669 ravb_ring_format(ndev, RAVB_BE); 670 ravb_ring_format(ndev, RAVB_NC); 671 672 /* Set AVB RX */ 673 ravb_write(ndev, 674 RCR_EFFS | RCR_ENCF | RCR_ETS0 | RCR_ESF | 0x18000000, RCR); 675 676 /* Set FIFO size */ 677 ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00112200, TGC); 678 679 /* Timestamp enable */ 680 ravb_write(ndev, TCCR_TFEN, TCCR); 681 682 /* Interrupt init: */ 683 if (info->multi_irqs) { 684 /* Clear DIL.DPLx */ 685 ravb_write(ndev, 0, DIL); 686 /* Set queue specific interrupt */ 687 ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE); 688 } 689 /* Frame receive */ 690 ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0); 691 /* Disable FIFO full warning */ 692 ravb_write(ndev, 0, RIC1); 693 /* Receive FIFO full error, descriptor empty */ 694 ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2); 695 /* Frame transmitted, timestamp FIFO updated */ 696 ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC); 697 698 return 0; 699 } 700 701 /* Device init function for Ethernet AVB */ 702 static int ravb_dmac_init(struct net_device *ndev) 703 { 704 struct ravb_private *priv = netdev_priv(ndev); 705 const struct ravb_hw_info *info = priv->info; 706 int error; 707 708 /* Set CONFIG mode */ 709 error = ravb_set_opmode(ndev, CCC_OPC_CONFIG); 710 if (error) 711 return error; 712 713 error = info->dmac_init(ndev); 714 if (error) 715 return error; 716 717 /* Setting the control will start the AVB-DMAC process. */ 718 return ravb_set_opmode(ndev, CCC_OPC_OPERATION); 719 } 720 721 static void ravb_get_tx_tstamp(struct net_device *ndev) 722 { 723 struct ravb_private *priv = netdev_priv(ndev); 724 struct ravb_tstamp_skb *ts_skb, *ts_skb2; 725 struct skb_shared_hwtstamps shhwtstamps; 726 struct sk_buff *skb; 727 struct timespec64 ts; 728 u16 tag, tfa_tag; 729 int count; 730 u32 tfa2; 731 732 count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8; 733 while (count--) { 734 tfa2 = ravb_read(ndev, TFA2); 735 tfa_tag = (tfa2 & TFA2_TST) >> 16; 736 ts.tv_nsec = (u64)ravb_read(ndev, TFA0); 737 ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) | 738 ravb_read(ndev, TFA1); 739 memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 740 shhwtstamps.hwtstamp = timespec64_to_ktime(ts); 741 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, 742 list) { 743 skb = ts_skb->skb; 744 tag = ts_skb->tag; 745 list_del(&ts_skb->list); 746 kfree(ts_skb); 747 if (tag == tfa_tag) { 748 skb_tstamp_tx(skb, &shhwtstamps); 749 dev_consume_skb_any(skb); 750 break; 751 } else { 752 dev_kfree_skb_any(skb); 753 } 754 } 755 ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR); 756 } 757 } 758 759 static void ravb_rx_csum_gbeth(struct sk_buff *skb) 760 { 761 __wsum csum_ip_hdr, csum_proto; 762 u8 *hw_csum; 763 764 /* The hardware checksum status is contained in sizeof(__sum16) * 2 = 4 765 * bytes appended to packet data. First 2 bytes is ip header checksum 766 * and last 2 bytes is protocol checksum. 767 */ 768 if (unlikely(skb->len < sizeof(__sum16) * 2)) 769 return; 770 771 hw_csum = skb_tail_pointer(skb) - sizeof(__sum16); 772 csum_proto = csum_unfold((__force __sum16)get_unaligned_le16(hw_csum)); 773 774 hw_csum -= sizeof(__sum16); 775 csum_ip_hdr = csum_unfold((__force __sum16)get_unaligned_le16(hw_csum)); 776 skb_trim(skb, skb->len - 2 * sizeof(__sum16)); 777 778 /* TODO: IPV6 Rx checksum */ 779 if (skb->protocol == htons(ETH_P_IP) && !csum_ip_hdr && !csum_proto) 780 skb->ip_summed = CHECKSUM_UNNECESSARY; 781 } 782 783 static void ravb_rx_csum(struct sk_buff *skb) 784 { 785 u8 *hw_csum; 786 787 /* The hardware checksum is contained in sizeof(__sum16) (2) bytes 788 * appended to packet data 789 */ 790 if (unlikely(skb->len < sizeof(__sum16))) 791 return; 792 hw_csum = skb_tail_pointer(skb) - sizeof(__sum16); 793 skb->csum = csum_unfold((__force __sum16)get_unaligned_le16(hw_csum)); 794 skb->ip_summed = CHECKSUM_COMPLETE; 795 skb_trim(skb, skb->len - sizeof(__sum16)); 796 } 797 798 static struct sk_buff *ravb_get_skb_gbeth(struct net_device *ndev, int entry, 799 struct ravb_rx_desc *desc) 800 { 801 struct ravb_private *priv = netdev_priv(ndev); 802 struct sk_buff *skb; 803 804 skb = priv->rx_skb[RAVB_BE][entry]; 805 priv->rx_skb[RAVB_BE][entry] = NULL; 806 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr), 807 ALIGN(GBETH_RX_BUFF_MAX, 16), DMA_FROM_DEVICE); 808 809 return skb; 810 } 811 812 /* Packet receive function for Gigabit Ethernet */ 813 static bool ravb_rx_gbeth(struct net_device *ndev, int *quota, int q) 814 { 815 struct ravb_private *priv = netdev_priv(ndev); 816 const struct ravb_hw_info *info = priv->info; 817 struct net_device_stats *stats; 818 struct ravb_rx_desc *desc; 819 struct sk_buff *skb; 820 dma_addr_t dma_addr; 821 u8 desc_status; 822 int boguscnt; 823 u16 pkt_len; 824 u8 die_dt; 825 int entry; 826 int limit; 827 828 entry = priv->cur_rx[q] % priv->num_rx_ring[q]; 829 boguscnt = priv->dirty_rx[q] + priv->num_rx_ring[q] - priv->cur_rx[q]; 830 stats = &priv->stats[q]; 831 832 boguscnt = min(boguscnt, *quota); 833 limit = boguscnt; 834 desc = &priv->gbeth_rx_ring[entry]; 835 while (desc->die_dt != DT_FEMPTY) { 836 /* Descriptor type must be checked before all other reads */ 837 dma_rmb(); 838 desc_status = desc->msc; 839 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS; 840 841 if (--boguscnt < 0) 842 break; 843 844 /* We use 0-byte descriptors to mark the DMA mapping errors */ 845 if (!pkt_len) 846 continue; 847 848 if (desc_status & MSC_MC) 849 stats->multicast++; 850 851 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF | MSC_CEEF)) { 852 stats->rx_errors++; 853 if (desc_status & MSC_CRC) 854 stats->rx_crc_errors++; 855 if (desc_status & MSC_RFE) 856 stats->rx_frame_errors++; 857 if (desc_status & (MSC_RTLF | MSC_RTSF)) 858 stats->rx_length_errors++; 859 if (desc_status & MSC_CEEF) 860 stats->rx_missed_errors++; 861 } else { 862 die_dt = desc->die_dt & 0xF0; 863 switch (die_dt) { 864 case DT_FSINGLE: 865 skb = ravb_get_skb_gbeth(ndev, entry, desc); 866 skb_put(skb, pkt_len); 867 skb->protocol = eth_type_trans(skb, ndev); 868 if (ndev->features & NETIF_F_RXCSUM) 869 ravb_rx_csum_gbeth(skb); 870 napi_gro_receive(&priv->napi[q], skb); 871 stats->rx_packets++; 872 stats->rx_bytes += pkt_len; 873 break; 874 case DT_FSTART: 875 priv->rx_1st_skb = ravb_get_skb_gbeth(ndev, entry, desc); 876 skb_put(priv->rx_1st_skb, pkt_len); 877 break; 878 case DT_FMID: 879 skb = ravb_get_skb_gbeth(ndev, entry, desc); 880 skb_copy_to_linear_data_offset(priv->rx_1st_skb, 881 priv->rx_1st_skb->len, 882 skb->data, 883 pkt_len); 884 skb_put(priv->rx_1st_skb, pkt_len); 885 dev_kfree_skb(skb); 886 break; 887 case DT_FEND: 888 skb = ravb_get_skb_gbeth(ndev, entry, desc); 889 skb_copy_to_linear_data_offset(priv->rx_1st_skb, 890 priv->rx_1st_skb->len, 891 skb->data, 892 pkt_len); 893 skb_put(priv->rx_1st_skb, pkt_len); 894 dev_kfree_skb(skb); 895 priv->rx_1st_skb->protocol = 896 eth_type_trans(priv->rx_1st_skb, ndev); 897 if (ndev->features & NETIF_F_RXCSUM) 898 ravb_rx_csum_gbeth(skb); 899 napi_gro_receive(&priv->napi[q], 900 priv->rx_1st_skb); 901 stats->rx_packets++; 902 stats->rx_bytes += pkt_len; 903 break; 904 } 905 } 906 907 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q]; 908 desc = &priv->gbeth_rx_ring[entry]; 909 } 910 911 /* Refill the RX ring buffers. */ 912 for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) { 913 entry = priv->dirty_rx[q] % priv->num_rx_ring[q]; 914 desc = &priv->gbeth_rx_ring[entry]; 915 desc->ds_cc = cpu_to_le16(GBETH_RX_DESC_DATA_SIZE); 916 917 if (!priv->rx_skb[q][entry]) { 918 skb = netdev_alloc_skb(ndev, info->max_rx_len); 919 if (!skb) 920 break; 921 ravb_set_buffer_align(skb); 922 dma_addr = dma_map_single(ndev->dev.parent, 923 skb->data, 924 GBETH_RX_BUFF_MAX, 925 DMA_FROM_DEVICE); 926 skb_checksum_none_assert(skb); 927 /* We just set the data size to 0 for a failed mapping 928 * which should prevent DMA from happening... 929 */ 930 if (dma_mapping_error(ndev->dev.parent, dma_addr)) 931 desc->ds_cc = cpu_to_le16(0); 932 desc->dptr = cpu_to_le32(dma_addr); 933 priv->rx_skb[q][entry] = skb; 934 } 935 /* Descriptor type must be set after all the above writes */ 936 dma_wmb(); 937 desc->die_dt = DT_FEMPTY; 938 } 939 940 *quota -= limit - (++boguscnt); 941 942 return boguscnt <= 0; 943 } 944 945 /* Packet receive function for Ethernet AVB */ 946 static bool ravb_rx_rcar(struct net_device *ndev, int *quota, int q) 947 { 948 struct ravb_private *priv = netdev_priv(ndev); 949 const struct ravb_hw_info *info = priv->info; 950 int entry = priv->cur_rx[q] % priv->num_rx_ring[q]; 951 int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) - 952 priv->cur_rx[q]; 953 struct net_device_stats *stats = &priv->stats[q]; 954 struct ravb_ex_rx_desc *desc; 955 struct sk_buff *skb; 956 dma_addr_t dma_addr; 957 struct timespec64 ts; 958 u8 desc_status; 959 u16 pkt_len; 960 int limit; 961 962 boguscnt = min(boguscnt, *quota); 963 limit = boguscnt; 964 desc = &priv->rx_ring[q][entry]; 965 while (desc->die_dt != DT_FEMPTY) { 966 /* Descriptor type must be checked before all other reads */ 967 dma_rmb(); 968 desc_status = desc->msc; 969 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS; 970 971 if (--boguscnt < 0) 972 break; 973 974 /* We use 0-byte descriptors to mark the DMA mapping errors */ 975 if (!pkt_len) 976 continue; 977 978 if (desc_status & MSC_MC) 979 stats->multicast++; 980 981 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF | 982 MSC_CEEF)) { 983 stats->rx_errors++; 984 if (desc_status & MSC_CRC) 985 stats->rx_crc_errors++; 986 if (desc_status & MSC_RFE) 987 stats->rx_frame_errors++; 988 if (desc_status & (MSC_RTLF | MSC_RTSF)) 989 stats->rx_length_errors++; 990 if (desc_status & MSC_CEEF) 991 stats->rx_missed_errors++; 992 } else { 993 u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE; 994 995 skb = priv->rx_skb[q][entry]; 996 priv->rx_skb[q][entry] = NULL; 997 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr), 998 RX_BUF_SZ, 999 DMA_FROM_DEVICE); 1000 get_ts &= (q == RAVB_NC) ? 1001 RAVB_RXTSTAMP_TYPE_V2_L2_EVENT : 1002 ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT; 1003 if (get_ts) { 1004 struct skb_shared_hwtstamps *shhwtstamps; 1005 1006 shhwtstamps = skb_hwtstamps(skb); 1007 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 1008 ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) << 1009 32) | le32_to_cpu(desc->ts_sl); 1010 ts.tv_nsec = le32_to_cpu(desc->ts_n); 1011 shhwtstamps->hwtstamp = timespec64_to_ktime(ts); 1012 } 1013 1014 skb_put(skb, pkt_len); 1015 skb->protocol = eth_type_trans(skb, ndev); 1016 if (ndev->features & NETIF_F_RXCSUM) 1017 ravb_rx_csum(skb); 1018 napi_gro_receive(&priv->napi[q], skb); 1019 stats->rx_packets++; 1020 stats->rx_bytes += pkt_len; 1021 } 1022 1023 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q]; 1024 desc = &priv->rx_ring[q][entry]; 1025 } 1026 1027 /* Refill the RX ring buffers. */ 1028 for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) { 1029 entry = priv->dirty_rx[q] % priv->num_rx_ring[q]; 1030 desc = &priv->rx_ring[q][entry]; 1031 desc->ds_cc = cpu_to_le16(RX_BUF_SZ); 1032 1033 if (!priv->rx_skb[q][entry]) { 1034 skb = netdev_alloc_skb(ndev, info->max_rx_len); 1035 if (!skb) 1036 break; /* Better luck next round. */ 1037 ravb_set_buffer_align(skb); 1038 dma_addr = dma_map_single(ndev->dev.parent, skb->data, 1039 le16_to_cpu(desc->ds_cc), 1040 DMA_FROM_DEVICE); 1041 skb_checksum_none_assert(skb); 1042 /* We just set the data size to 0 for a failed mapping 1043 * which should prevent DMA from happening... 1044 */ 1045 if (dma_mapping_error(ndev->dev.parent, dma_addr)) 1046 desc->ds_cc = cpu_to_le16(0); 1047 desc->dptr = cpu_to_le32(dma_addr); 1048 priv->rx_skb[q][entry] = skb; 1049 } 1050 /* Descriptor type must be set after all the above writes */ 1051 dma_wmb(); 1052 desc->die_dt = DT_FEMPTY; 1053 } 1054 1055 *quota -= limit - (++boguscnt); 1056 1057 return boguscnt <= 0; 1058 } 1059 1060 /* Packet receive function for Ethernet AVB */ 1061 static bool ravb_rx(struct net_device *ndev, int *quota, int q) 1062 { 1063 struct ravb_private *priv = netdev_priv(ndev); 1064 const struct ravb_hw_info *info = priv->info; 1065 1066 return info->receive(ndev, quota, q); 1067 } 1068 1069 static void ravb_rcv_snd_disable(struct net_device *ndev) 1070 { 1071 /* Disable TX and RX */ 1072 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0); 1073 } 1074 1075 static void ravb_rcv_snd_enable(struct net_device *ndev) 1076 { 1077 /* Enable TX and RX */ 1078 ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE); 1079 } 1080 1081 /* function for waiting dma process finished */ 1082 static int ravb_stop_dma(struct net_device *ndev) 1083 { 1084 struct ravb_private *priv = netdev_priv(ndev); 1085 const struct ravb_hw_info *info = priv->info; 1086 int error; 1087 1088 /* Wait for stopping the hardware TX process */ 1089 error = ravb_wait(ndev, TCCR, info->tccr_mask, 0); 1090 1091 if (error) 1092 return error; 1093 1094 error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3, 1095 0); 1096 if (error) 1097 return error; 1098 1099 /* Stop the E-MAC's RX/TX processes. */ 1100 ravb_rcv_snd_disable(ndev); 1101 1102 /* Wait for stopping the RX DMA process */ 1103 error = ravb_wait(ndev, CSR, CSR_RPO, 0); 1104 if (error) 1105 return error; 1106 1107 /* Stop AVB-DMAC process */ 1108 return ravb_set_opmode(ndev, CCC_OPC_CONFIG); 1109 } 1110 1111 /* E-MAC interrupt handler */ 1112 static void ravb_emac_interrupt_unlocked(struct net_device *ndev) 1113 { 1114 struct ravb_private *priv = netdev_priv(ndev); 1115 u32 ecsr, psr; 1116 1117 ecsr = ravb_read(ndev, ECSR); 1118 ravb_write(ndev, ecsr, ECSR); /* clear interrupt */ 1119 1120 if (ecsr & ECSR_MPD) 1121 pm_wakeup_event(&priv->pdev->dev, 0); 1122 if (ecsr & ECSR_ICD) 1123 ndev->stats.tx_carrier_errors++; 1124 if (ecsr & ECSR_LCHNG) { 1125 /* Link changed */ 1126 if (priv->no_avb_link) 1127 return; 1128 psr = ravb_read(ndev, PSR); 1129 if (priv->avb_link_active_low) 1130 psr ^= PSR_LMON; 1131 if (!(psr & PSR_LMON)) { 1132 /* DIsable RX and TX */ 1133 ravb_rcv_snd_disable(ndev); 1134 } else { 1135 /* Enable RX and TX */ 1136 ravb_rcv_snd_enable(ndev); 1137 } 1138 } 1139 } 1140 1141 static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id) 1142 { 1143 struct net_device *ndev = dev_id; 1144 struct ravb_private *priv = netdev_priv(ndev); 1145 struct device *dev = &priv->pdev->dev; 1146 irqreturn_t result = IRQ_HANDLED; 1147 1148 pm_runtime_get_noresume(dev); 1149 1150 if (unlikely(!pm_runtime_active(dev))) { 1151 result = IRQ_NONE; 1152 goto out_rpm_put; 1153 } 1154 1155 spin_lock(&priv->lock); 1156 ravb_emac_interrupt_unlocked(ndev); 1157 spin_unlock(&priv->lock); 1158 1159 out_rpm_put: 1160 pm_runtime_put_noidle(dev); 1161 return result; 1162 } 1163 1164 /* Error interrupt handler */ 1165 static void ravb_error_interrupt(struct net_device *ndev) 1166 { 1167 struct ravb_private *priv = netdev_priv(ndev); 1168 u32 eis, ris2; 1169 1170 eis = ravb_read(ndev, EIS); 1171 ravb_write(ndev, ~(EIS_QFS | EIS_RESERVED), EIS); 1172 if (eis & EIS_QFS) { 1173 ris2 = ravb_read(ndev, RIS2); 1174 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_QFF1 | RIS2_RFFF | RIS2_RESERVED), 1175 RIS2); 1176 1177 /* Receive Descriptor Empty int */ 1178 if (ris2 & RIS2_QFF0) 1179 priv->stats[RAVB_BE].rx_over_errors++; 1180 1181 /* Receive Descriptor Empty int */ 1182 if (ris2 & RIS2_QFF1) 1183 priv->stats[RAVB_NC].rx_over_errors++; 1184 1185 /* Receive FIFO Overflow int */ 1186 if (ris2 & RIS2_RFFF) 1187 priv->rx_fifo_errors++; 1188 } 1189 } 1190 1191 static bool ravb_queue_interrupt(struct net_device *ndev, int q) 1192 { 1193 struct ravb_private *priv = netdev_priv(ndev); 1194 const struct ravb_hw_info *info = priv->info; 1195 u32 ris0 = ravb_read(ndev, RIS0); 1196 u32 ric0 = ravb_read(ndev, RIC0); 1197 u32 tis = ravb_read(ndev, TIS); 1198 u32 tic = ravb_read(ndev, TIC); 1199 1200 if (((ris0 & ric0) & BIT(q)) || ((tis & tic) & BIT(q))) { 1201 if (napi_schedule_prep(&priv->napi[q])) { 1202 /* Mask RX and TX interrupts */ 1203 if (!info->irq_en_dis) { 1204 ravb_write(ndev, ric0 & ~BIT(q), RIC0); 1205 ravb_write(ndev, tic & ~BIT(q), TIC); 1206 } else { 1207 ravb_write(ndev, BIT(q), RID0); 1208 ravb_write(ndev, BIT(q), TID); 1209 } 1210 __napi_schedule(&priv->napi[q]); 1211 } else { 1212 netdev_warn(ndev, 1213 "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n", 1214 ris0, ric0); 1215 netdev_warn(ndev, 1216 " tx status 0x%08x, tx mask 0x%08x.\n", 1217 tis, tic); 1218 } 1219 return true; 1220 } 1221 return false; 1222 } 1223 1224 static bool ravb_timestamp_interrupt(struct net_device *ndev) 1225 { 1226 u32 tis = ravb_read(ndev, TIS); 1227 1228 if (tis & TIS_TFUF) { 1229 ravb_write(ndev, ~(TIS_TFUF | TIS_RESERVED), TIS); 1230 ravb_get_tx_tstamp(ndev); 1231 return true; 1232 } 1233 return false; 1234 } 1235 1236 static irqreturn_t ravb_interrupt(int irq, void *dev_id) 1237 { 1238 struct net_device *ndev = dev_id; 1239 struct ravb_private *priv = netdev_priv(ndev); 1240 const struct ravb_hw_info *info = priv->info; 1241 struct device *dev = &priv->pdev->dev; 1242 irqreturn_t result = IRQ_NONE; 1243 u32 iss; 1244 1245 pm_runtime_get_noresume(dev); 1246 1247 if (unlikely(!pm_runtime_active(dev))) 1248 goto out_rpm_put; 1249 1250 spin_lock(&priv->lock); 1251 /* Get interrupt status */ 1252 iss = ravb_read(ndev, ISS); 1253 1254 /* Received and transmitted interrupts */ 1255 if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) { 1256 int q; 1257 1258 /* Timestamp updated */ 1259 if (ravb_timestamp_interrupt(ndev)) 1260 result = IRQ_HANDLED; 1261 1262 /* Network control and best effort queue RX/TX */ 1263 if (info->nc_queues) { 1264 for (q = RAVB_NC; q >= RAVB_BE; q--) { 1265 if (ravb_queue_interrupt(ndev, q)) 1266 result = IRQ_HANDLED; 1267 } 1268 } else { 1269 if (ravb_queue_interrupt(ndev, RAVB_BE)) 1270 result = IRQ_HANDLED; 1271 } 1272 } 1273 1274 /* E-MAC status summary */ 1275 if (iss & ISS_MS) { 1276 ravb_emac_interrupt_unlocked(ndev); 1277 result = IRQ_HANDLED; 1278 } 1279 1280 /* Error status summary */ 1281 if (iss & ISS_ES) { 1282 ravb_error_interrupt(ndev); 1283 result = IRQ_HANDLED; 1284 } 1285 1286 /* gPTP interrupt status summary */ 1287 if (iss & ISS_CGIS) { 1288 ravb_ptp_interrupt(ndev); 1289 result = IRQ_HANDLED; 1290 } 1291 1292 spin_unlock(&priv->lock); 1293 1294 out_rpm_put: 1295 pm_runtime_put_noidle(dev); 1296 return result; 1297 } 1298 1299 /* Timestamp/Error/gPTP interrupt handler */ 1300 static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id) 1301 { 1302 struct net_device *ndev = dev_id; 1303 struct ravb_private *priv = netdev_priv(ndev); 1304 struct device *dev = &priv->pdev->dev; 1305 irqreturn_t result = IRQ_NONE; 1306 u32 iss; 1307 1308 pm_runtime_get_noresume(dev); 1309 1310 if (unlikely(!pm_runtime_active(dev))) 1311 goto out_rpm_put; 1312 1313 spin_lock(&priv->lock); 1314 /* Get interrupt status */ 1315 iss = ravb_read(ndev, ISS); 1316 1317 /* Timestamp updated */ 1318 if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev)) 1319 result = IRQ_HANDLED; 1320 1321 /* Error status summary */ 1322 if (iss & ISS_ES) { 1323 ravb_error_interrupt(ndev); 1324 result = IRQ_HANDLED; 1325 } 1326 1327 /* gPTP interrupt status summary */ 1328 if (iss & ISS_CGIS) { 1329 ravb_ptp_interrupt(ndev); 1330 result = IRQ_HANDLED; 1331 } 1332 1333 spin_unlock(&priv->lock); 1334 1335 out_rpm_put: 1336 pm_runtime_put_noidle(dev); 1337 return result; 1338 } 1339 1340 static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q) 1341 { 1342 struct net_device *ndev = dev_id; 1343 struct ravb_private *priv = netdev_priv(ndev); 1344 struct device *dev = &priv->pdev->dev; 1345 irqreturn_t result = IRQ_NONE; 1346 1347 pm_runtime_get_noresume(dev); 1348 1349 if (unlikely(!pm_runtime_active(dev))) 1350 goto out_rpm_put; 1351 1352 spin_lock(&priv->lock); 1353 1354 /* Network control/Best effort queue RX/TX */ 1355 if (ravb_queue_interrupt(ndev, q)) 1356 result = IRQ_HANDLED; 1357 1358 spin_unlock(&priv->lock); 1359 1360 out_rpm_put: 1361 pm_runtime_put_noidle(dev); 1362 return result; 1363 } 1364 1365 static irqreturn_t ravb_be_interrupt(int irq, void *dev_id) 1366 { 1367 return ravb_dma_interrupt(irq, dev_id, RAVB_BE); 1368 } 1369 1370 static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id) 1371 { 1372 return ravb_dma_interrupt(irq, dev_id, RAVB_NC); 1373 } 1374 1375 static int ravb_poll(struct napi_struct *napi, int budget) 1376 { 1377 struct net_device *ndev = napi->dev; 1378 struct ravb_private *priv = netdev_priv(ndev); 1379 const struct ravb_hw_info *info = priv->info; 1380 unsigned long flags; 1381 int q = napi - priv->napi; 1382 int mask = BIT(q); 1383 int quota = budget; 1384 1385 /* Processing RX Descriptor Ring */ 1386 /* Clear RX interrupt */ 1387 ravb_write(ndev, ~(mask | RIS0_RESERVED), RIS0); 1388 if (ravb_rx(ndev, "a, q)) 1389 goto out; 1390 1391 /* Processing TX Descriptor Ring */ 1392 spin_lock_irqsave(&priv->lock, flags); 1393 /* Clear TX interrupt */ 1394 ravb_write(ndev, ~(mask | TIS_RESERVED), TIS); 1395 ravb_tx_free(ndev, q, true); 1396 netif_wake_subqueue(ndev, q); 1397 spin_unlock_irqrestore(&priv->lock, flags); 1398 1399 napi_complete(napi); 1400 1401 /* Re-enable RX/TX interrupts */ 1402 spin_lock_irqsave(&priv->lock, flags); 1403 if (!info->irq_en_dis) { 1404 ravb_modify(ndev, RIC0, mask, mask); 1405 ravb_modify(ndev, TIC, mask, mask); 1406 } else { 1407 ravb_write(ndev, mask, RIE0); 1408 ravb_write(ndev, mask, TIE); 1409 } 1410 spin_unlock_irqrestore(&priv->lock, flags); 1411 1412 /* Receive error message handling */ 1413 priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors; 1414 if (info->nc_queues) 1415 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors; 1416 if (priv->rx_over_errors != ndev->stats.rx_over_errors) 1417 ndev->stats.rx_over_errors = priv->rx_over_errors; 1418 if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors) 1419 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors; 1420 out: 1421 return budget - quota; 1422 } 1423 1424 static void ravb_set_duplex_gbeth(struct net_device *ndev) 1425 { 1426 struct ravb_private *priv = netdev_priv(ndev); 1427 1428 ravb_modify(ndev, ECMR, ECMR_DM, priv->duplex > 0 ? ECMR_DM : 0); 1429 } 1430 1431 /* PHY state control function */ 1432 static void ravb_adjust_link(struct net_device *ndev) 1433 { 1434 struct ravb_private *priv = netdev_priv(ndev); 1435 const struct ravb_hw_info *info = priv->info; 1436 struct phy_device *phydev = ndev->phydev; 1437 bool new_state = false; 1438 unsigned long flags; 1439 1440 spin_lock_irqsave(&priv->lock, flags); 1441 1442 /* Disable TX and RX right over here, if E-MAC change is ignored */ 1443 if (priv->no_avb_link) 1444 ravb_rcv_snd_disable(ndev); 1445 1446 if (phydev->link) { 1447 if (info->half_duplex && phydev->duplex != priv->duplex) { 1448 new_state = true; 1449 priv->duplex = phydev->duplex; 1450 ravb_set_duplex_gbeth(ndev); 1451 } 1452 1453 if (phydev->speed != priv->speed) { 1454 new_state = true; 1455 priv->speed = phydev->speed; 1456 info->set_rate(ndev); 1457 } 1458 if (!priv->link) { 1459 ravb_modify(ndev, ECMR, ECMR_TXF, 0); 1460 new_state = true; 1461 priv->link = phydev->link; 1462 } 1463 } else if (priv->link) { 1464 new_state = true; 1465 priv->link = 0; 1466 priv->speed = 0; 1467 if (info->half_duplex) 1468 priv->duplex = -1; 1469 } 1470 1471 /* Enable TX and RX right over here, if E-MAC change is ignored */ 1472 if (priv->no_avb_link && phydev->link) 1473 ravb_rcv_snd_enable(ndev); 1474 1475 spin_unlock_irqrestore(&priv->lock, flags); 1476 1477 if (new_state && netif_msg_link(priv)) 1478 phy_print_status(phydev); 1479 } 1480 1481 /* PHY init function */ 1482 static int ravb_phy_init(struct net_device *ndev) 1483 { 1484 struct device_node *np = ndev->dev.parent->of_node; 1485 struct ravb_private *priv = netdev_priv(ndev); 1486 const struct ravb_hw_info *info = priv->info; 1487 struct phy_device *phydev; 1488 struct device_node *pn; 1489 phy_interface_t iface; 1490 int err; 1491 1492 priv->link = 0; 1493 priv->speed = 0; 1494 priv->duplex = -1; 1495 1496 /* Try connecting to PHY */ 1497 pn = of_parse_phandle(np, "phy-handle", 0); 1498 if (!pn) { 1499 /* In the case of a fixed PHY, the DT node associated 1500 * to the PHY is the Ethernet MAC DT node. 1501 */ 1502 if (of_phy_is_fixed_link(np)) { 1503 err = of_phy_register_fixed_link(np); 1504 if (err) 1505 return err; 1506 } 1507 pn = of_node_get(np); 1508 } 1509 1510 iface = priv->rgmii_override ? PHY_INTERFACE_MODE_RGMII 1511 : priv->phy_interface; 1512 phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0, iface); 1513 of_node_put(pn); 1514 if (!phydev) { 1515 netdev_err(ndev, "failed to connect PHY\n"); 1516 err = -ENOENT; 1517 goto err_deregister_fixed_link; 1518 } 1519 1520 if (!info->half_duplex) { 1521 /* 10BASE, Pause and Asym Pause is not supported */ 1522 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Half_BIT); 1523 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Full_BIT); 1524 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_Pause_BIT); 1525 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_Asym_Pause_BIT); 1526 1527 /* Half Duplex is not supported */ 1528 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT); 1529 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Half_BIT); 1530 } 1531 1532 phy_attached_info(phydev); 1533 1534 return 0; 1535 1536 err_deregister_fixed_link: 1537 if (of_phy_is_fixed_link(np)) 1538 of_phy_deregister_fixed_link(np); 1539 1540 return err; 1541 } 1542 1543 /* PHY control start function */ 1544 static int ravb_phy_start(struct net_device *ndev) 1545 { 1546 int error; 1547 1548 error = ravb_phy_init(ndev); 1549 if (error) 1550 return error; 1551 1552 phy_start(ndev->phydev); 1553 1554 return 0; 1555 } 1556 1557 static u32 ravb_get_msglevel(struct net_device *ndev) 1558 { 1559 struct ravb_private *priv = netdev_priv(ndev); 1560 1561 return priv->msg_enable; 1562 } 1563 1564 static void ravb_set_msglevel(struct net_device *ndev, u32 value) 1565 { 1566 struct ravb_private *priv = netdev_priv(ndev); 1567 1568 priv->msg_enable = value; 1569 } 1570 1571 static const char ravb_gstrings_stats_gbeth[][ETH_GSTRING_LEN] = { 1572 "rx_queue_0_current", 1573 "tx_queue_0_current", 1574 "rx_queue_0_dirty", 1575 "tx_queue_0_dirty", 1576 "rx_queue_0_packets", 1577 "tx_queue_0_packets", 1578 "rx_queue_0_bytes", 1579 "tx_queue_0_bytes", 1580 "rx_queue_0_mcast_packets", 1581 "rx_queue_0_errors", 1582 "rx_queue_0_crc_errors", 1583 "rx_queue_0_frame_errors", 1584 "rx_queue_0_length_errors", 1585 "rx_queue_0_csum_offload_errors", 1586 "rx_queue_0_over_errors", 1587 }; 1588 1589 static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = { 1590 "rx_queue_0_current", 1591 "tx_queue_0_current", 1592 "rx_queue_0_dirty", 1593 "tx_queue_0_dirty", 1594 "rx_queue_0_packets", 1595 "tx_queue_0_packets", 1596 "rx_queue_0_bytes", 1597 "tx_queue_0_bytes", 1598 "rx_queue_0_mcast_packets", 1599 "rx_queue_0_errors", 1600 "rx_queue_0_crc_errors", 1601 "rx_queue_0_frame_errors", 1602 "rx_queue_0_length_errors", 1603 "rx_queue_0_missed_errors", 1604 "rx_queue_0_over_errors", 1605 1606 "rx_queue_1_current", 1607 "tx_queue_1_current", 1608 "rx_queue_1_dirty", 1609 "tx_queue_1_dirty", 1610 "rx_queue_1_packets", 1611 "tx_queue_1_packets", 1612 "rx_queue_1_bytes", 1613 "tx_queue_1_bytes", 1614 "rx_queue_1_mcast_packets", 1615 "rx_queue_1_errors", 1616 "rx_queue_1_crc_errors", 1617 "rx_queue_1_frame_errors", 1618 "rx_queue_1_length_errors", 1619 "rx_queue_1_missed_errors", 1620 "rx_queue_1_over_errors", 1621 }; 1622 1623 static int ravb_get_sset_count(struct net_device *netdev, int sset) 1624 { 1625 struct ravb_private *priv = netdev_priv(netdev); 1626 const struct ravb_hw_info *info = priv->info; 1627 1628 switch (sset) { 1629 case ETH_SS_STATS: 1630 return info->stats_len; 1631 default: 1632 return -EOPNOTSUPP; 1633 } 1634 } 1635 1636 static void ravb_get_ethtool_stats(struct net_device *ndev, 1637 struct ethtool_stats *estats, u64 *data) 1638 { 1639 struct ravb_private *priv = netdev_priv(ndev); 1640 const struct ravb_hw_info *info = priv->info; 1641 int num_rx_q; 1642 int i = 0; 1643 int q; 1644 1645 num_rx_q = info->nc_queues ? NUM_RX_QUEUE : 1; 1646 /* Device-specific stats */ 1647 for (q = RAVB_BE; q < num_rx_q; q++) { 1648 struct net_device_stats *stats = &priv->stats[q]; 1649 1650 data[i++] = priv->cur_rx[q]; 1651 data[i++] = priv->cur_tx[q]; 1652 data[i++] = priv->dirty_rx[q]; 1653 data[i++] = priv->dirty_tx[q]; 1654 data[i++] = stats->rx_packets; 1655 data[i++] = stats->tx_packets; 1656 data[i++] = stats->rx_bytes; 1657 data[i++] = stats->tx_bytes; 1658 data[i++] = stats->multicast; 1659 data[i++] = stats->rx_errors; 1660 data[i++] = stats->rx_crc_errors; 1661 data[i++] = stats->rx_frame_errors; 1662 data[i++] = stats->rx_length_errors; 1663 data[i++] = stats->rx_missed_errors; 1664 data[i++] = stats->rx_over_errors; 1665 } 1666 } 1667 1668 static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data) 1669 { 1670 struct ravb_private *priv = netdev_priv(ndev); 1671 const struct ravb_hw_info *info = priv->info; 1672 1673 switch (stringset) { 1674 case ETH_SS_STATS: 1675 memcpy(data, info->gstrings_stats, info->gstrings_size); 1676 break; 1677 } 1678 } 1679 1680 static void ravb_get_ringparam(struct net_device *ndev, 1681 struct ethtool_ringparam *ring, 1682 struct kernel_ethtool_ringparam *kernel_ring, 1683 struct netlink_ext_ack *extack) 1684 { 1685 struct ravb_private *priv = netdev_priv(ndev); 1686 1687 ring->rx_max_pending = BE_RX_RING_MAX; 1688 ring->tx_max_pending = BE_TX_RING_MAX; 1689 ring->rx_pending = priv->num_rx_ring[RAVB_BE]; 1690 ring->tx_pending = priv->num_tx_ring[RAVB_BE]; 1691 } 1692 1693 static int ravb_set_ringparam(struct net_device *ndev, 1694 struct ethtool_ringparam *ring, 1695 struct kernel_ethtool_ringparam *kernel_ring, 1696 struct netlink_ext_ack *extack) 1697 { 1698 struct ravb_private *priv = netdev_priv(ndev); 1699 const struct ravb_hw_info *info = priv->info; 1700 int error; 1701 1702 if (ring->tx_pending > BE_TX_RING_MAX || 1703 ring->rx_pending > BE_RX_RING_MAX || 1704 ring->tx_pending < BE_TX_RING_MIN || 1705 ring->rx_pending < BE_RX_RING_MIN) 1706 return -EINVAL; 1707 if (ring->rx_mini_pending || ring->rx_jumbo_pending) 1708 return -EINVAL; 1709 1710 if (netif_running(ndev)) { 1711 netif_device_detach(ndev); 1712 /* Stop PTP Clock driver */ 1713 if (info->gptp) 1714 ravb_ptp_stop(ndev); 1715 /* Wait for DMA stopping */ 1716 error = ravb_stop_dma(ndev); 1717 if (error) { 1718 netdev_err(ndev, 1719 "cannot set ringparam! Any AVB processes are still running?\n"); 1720 return error; 1721 } 1722 synchronize_irq(ndev->irq); 1723 1724 /* Free all the skb's in the RX queue and the DMA buffers. */ 1725 ravb_ring_free(ndev, RAVB_BE); 1726 if (info->nc_queues) 1727 ravb_ring_free(ndev, RAVB_NC); 1728 } 1729 1730 /* Set new parameters */ 1731 priv->num_rx_ring[RAVB_BE] = ring->rx_pending; 1732 priv->num_tx_ring[RAVB_BE] = ring->tx_pending; 1733 1734 if (netif_running(ndev)) { 1735 error = ravb_dmac_init(ndev); 1736 if (error) { 1737 netdev_err(ndev, 1738 "%s: ravb_dmac_init() failed, error %d\n", 1739 __func__, error); 1740 return error; 1741 } 1742 1743 ravb_emac_init(ndev); 1744 1745 /* Initialise PTP Clock driver */ 1746 if (info->gptp) 1747 ravb_ptp_init(ndev, priv->pdev); 1748 1749 netif_device_attach(ndev); 1750 } 1751 1752 return 0; 1753 } 1754 1755 static int ravb_get_ts_info(struct net_device *ndev, 1756 struct ethtool_ts_info *info) 1757 { 1758 struct ravb_private *priv = netdev_priv(ndev); 1759 const struct ravb_hw_info *hw_info = priv->info; 1760 1761 info->so_timestamping = 1762 SOF_TIMESTAMPING_TX_SOFTWARE | 1763 SOF_TIMESTAMPING_RX_SOFTWARE | 1764 SOF_TIMESTAMPING_SOFTWARE | 1765 SOF_TIMESTAMPING_TX_HARDWARE | 1766 SOF_TIMESTAMPING_RX_HARDWARE | 1767 SOF_TIMESTAMPING_RAW_HARDWARE; 1768 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON); 1769 info->rx_filters = 1770 (1 << HWTSTAMP_FILTER_NONE) | 1771 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | 1772 (1 << HWTSTAMP_FILTER_ALL); 1773 if (hw_info->gptp || hw_info->ccc_gac) 1774 info->phc_index = ptp_clock_index(priv->ptp.clock); 1775 1776 return 0; 1777 } 1778 1779 static void ravb_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 1780 { 1781 struct ravb_private *priv = netdev_priv(ndev); 1782 1783 wol->supported = WAKE_MAGIC; 1784 wol->wolopts = priv->wol_enabled ? WAKE_MAGIC : 0; 1785 } 1786 1787 static int ravb_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 1788 { 1789 struct ravb_private *priv = netdev_priv(ndev); 1790 const struct ravb_hw_info *info = priv->info; 1791 1792 if (!info->magic_pkt || (wol->wolopts & ~WAKE_MAGIC)) 1793 return -EOPNOTSUPP; 1794 1795 priv->wol_enabled = !!(wol->wolopts & WAKE_MAGIC); 1796 1797 device_set_wakeup_enable(&priv->pdev->dev, priv->wol_enabled); 1798 1799 return 0; 1800 } 1801 1802 static const struct ethtool_ops ravb_ethtool_ops = { 1803 .nway_reset = phy_ethtool_nway_reset, 1804 .get_msglevel = ravb_get_msglevel, 1805 .set_msglevel = ravb_set_msglevel, 1806 .get_link = ethtool_op_get_link, 1807 .get_strings = ravb_get_strings, 1808 .get_ethtool_stats = ravb_get_ethtool_stats, 1809 .get_sset_count = ravb_get_sset_count, 1810 .get_ringparam = ravb_get_ringparam, 1811 .set_ringparam = ravb_set_ringparam, 1812 .get_ts_info = ravb_get_ts_info, 1813 .get_link_ksettings = phy_ethtool_get_link_ksettings, 1814 .set_link_ksettings = phy_ethtool_set_link_ksettings, 1815 .get_wol = ravb_get_wol, 1816 .set_wol = ravb_set_wol, 1817 }; 1818 1819 static int ravb_set_config_mode(struct net_device *ndev) 1820 { 1821 struct ravb_private *priv = netdev_priv(ndev); 1822 const struct ravb_hw_info *info = priv->info; 1823 int error; 1824 1825 if (info->gptp) { 1826 error = ravb_set_opmode(ndev, CCC_OPC_CONFIG); 1827 if (error) 1828 return error; 1829 /* Set CSEL value */ 1830 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB); 1831 } else if (info->ccc_gac) { 1832 error = ravb_set_opmode(ndev, CCC_OPC_CONFIG | CCC_GAC | CCC_CSEL_HPB); 1833 } else { 1834 error = ravb_set_opmode(ndev, CCC_OPC_CONFIG); 1835 } 1836 1837 return error; 1838 } 1839 1840 static void ravb_set_gti(struct net_device *ndev) 1841 { 1842 struct ravb_private *priv = netdev_priv(ndev); 1843 const struct ravb_hw_info *info = priv->info; 1844 1845 if (!(info->gptp || info->ccc_gac)) 1846 return; 1847 1848 ravb_write(ndev, priv->gti_tiv, GTI); 1849 1850 /* Request GTI loading */ 1851 ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI); 1852 } 1853 1854 static int ravb_compute_gti(struct net_device *ndev) 1855 { 1856 struct ravb_private *priv = netdev_priv(ndev); 1857 const struct ravb_hw_info *info = priv->info; 1858 struct device *dev = ndev->dev.parent; 1859 unsigned long rate; 1860 u64 inc; 1861 1862 if (!(info->gptp || info->ccc_gac)) 1863 return 0; 1864 1865 if (info->gptp_ref_clk) 1866 rate = clk_get_rate(priv->gptp_clk); 1867 else 1868 rate = clk_get_rate(priv->clk); 1869 if (!rate) 1870 return -EINVAL; 1871 1872 inc = div64_ul(1000000000ULL << 20, rate); 1873 1874 if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) { 1875 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n", 1876 inc, GTI_TIV_MIN, GTI_TIV_MAX); 1877 return -EINVAL; 1878 } 1879 priv->gti_tiv = inc; 1880 1881 return 0; 1882 } 1883 1884 /* Set tx and rx clock internal delay modes */ 1885 static void ravb_parse_delay_mode(struct device_node *np, struct net_device *ndev) 1886 { 1887 struct ravb_private *priv = netdev_priv(ndev); 1888 bool explicit_delay = false; 1889 u32 delay; 1890 1891 if (!priv->info->internal_delay) 1892 return; 1893 1894 if (!of_property_read_u32(np, "rx-internal-delay-ps", &delay)) { 1895 /* Valid values are 0 and 1800, according to DT bindings */ 1896 priv->rxcidm = !!delay; 1897 explicit_delay = true; 1898 } 1899 if (!of_property_read_u32(np, "tx-internal-delay-ps", &delay)) { 1900 /* Valid values are 0 and 2000, according to DT bindings */ 1901 priv->txcidm = !!delay; 1902 explicit_delay = true; 1903 } 1904 1905 if (explicit_delay) 1906 return; 1907 1908 /* Fall back to legacy rgmii-*id behavior */ 1909 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID || 1910 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID) { 1911 priv->rxcidm = 1; 1912 priv->rgmii_override = 1; 1913 } 1914 1915 if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID || 1916 priv->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) { 1917 priv->txcidm = 1; 1918 priv->rgmii_override = 1; 1919 } 1920 } 1921 1922 static void ravb_set_delay_mode(struct net_device *ndev) 1923 { 1924 struct ravb_private *priv = netdev_priv(ndev); 1925 u32 set = 0; 1926 1927 if (!priv->info->internal_delay) 1928 return; 1929 1930 if (priv->rxcidm) 1931 set |= APSR_RDM; 1932 if (priv->txcidm) 1933 set |= APSR_TDM; 1934 ravb_modify(ndev, APSR, APSR_RDM | APSR_TDM, set); 1935 } 1936 1937 /* Network device open function for Ethernet AVB */ 1938 static int ravb_open(struct net_device *ndev) 1939 { 1940 struct ravb_private *priv = netdev_priv(ndev); 1941 const struct ravb_hw_info *info = priv->info; 1942 int error; 1943 1944 napi_enable(&priv->napi[RAVB_BE]); 1945 if (info->nc_queues) 1946 napi_enable(&priv->napi[RAVB_NC]); 1947 1948 /* Set AVB config mode */ 1949 error = ravb_set_config_mode(ndev); 1950 if (error) 1951 goto out_napi_off; 1952 1953 ravb_set_delay_mode(ndev); 1954 ravb_write(ndev, priv->desc_bat_dma, DBAT); 1955 1956 /* Device init */ 1957 error = ravb_dmac_init(ndev); 1958 if (error) 1959 goto out_set_reset; 1960 1961 ravb_emac_init(ndev); 1962 1963 ravb_set_gti(ndev); 1964 1965 /* Initialise PTP Clock driver */ 1966 if (info->gptp || info->ccc_gac) 1967 ravb_ptp_init(ndev, priv->pdev); 1968 1969 /* PHY control start */ 1970 error = ravb_phy_start(ndev); 1971 if (error) 1972 goto out_ptp_stop; 1973 1974 netif_tx_start_all_queues(ndev); 1975 1976 return 0; 1977 1978 out_ptp_stop: 1979 /* Stop PTP Clock driver */ 1980 if (info->gptp || info->ccc_gac) 1981 ravb_ptp_stop(ndev); 1982 ravb_stop_dma(ndev); 1983 out_set_reset: 1984 ravb_set_opmode(ndev, CCC_OPC_RESET); 1985 out_napi_off: 1986 if (info->nc_queues) 1987 napi_disable(&priv->napi[RAVB_NC]); 1988 napi_disable(&priv->napi[RAVB_BE]); 1989 return error; 1990 } 1991 1992 /* Timeout function for Ethernet AVB */ 1993 static void ravb_tx_timeout(struct net_device *ndev, unsigned int txqueue) 1994 { 1995 struct ravb_private *priv = netdev_priv(ndev); 1996 1997 netif_err(priv, tx_err, ndev, 1998 "transmit timed out, status %08x, resetting...\n", 1999 ravb_read(ndev, ISS)); 2000 2001 /* tx_errors count up */ 2002 ndev->stats.tx_errors++; 2003 2004 schedule_work(&priv->work); 2005 } 2006 2007 static void ravb_tx_timeout_work(struct work_struct *work) 2008 { 2009 struct ravb_private *priv = container_of(work, struct ravb_private, 2010 work); 2011 const struct ravb_hw_info *info = priv->info; 2012 struct net_device *ndev = priv->ndev; 2013 int error; 2014 2015 if (!rtnl_trylock()) { 2016 usleep_range(1000, 2000); 2017 schedule_work(&priv->work); 2018 return; 2019 } 2020 2021 netif_tx_stop_all_queues(ndev); 2022 2023 /* Stop PTP Clock driver */ 2024 if (info->gptp) 2025 ravb_ptp_stop(ndev); 2026 2027 /* Wait for DMA stopping */ 2028 if (ravb_stop_dma(ndev)) { 2029 /* If ravb_stop_dma() fails, the hardware is still operating 2030 * for TX and/or RX. So, this should not call the following 2031 * functions because ravb_dmac_init() is possible to fail too. 2032 * Also, this should not retry ravb_stop_dma() again and again 2033 * here because it's possible to wait forever. So, this just 2034 * re-enables the TX and RX and skip the following 2035 * re-initialization procedure. 2036 */ 2037 ravb_rcv_snd_enable(ndev); 2038 goto out; 2039 } 2040 2041 ravb_ring_free(ndev, RAVB_BE); 2042 if (info->nc_queues) 2043 ravb_ring_free(ndev, RAVB_NC); 2044 2045 /* Device init */ 2046 error = ravb_dmac_init(ndev); 2047 if (error) { 2048 /* If ravb_dmac_init() fails, descriptors are freed. So, this 2049 * should return here to avoid re-enabling the TX and RX in 2050 * ravb_emac_init(). 2051 */ 2052 netdev_err(ndev, "%s: ravb_dmac_init() failed, error %d\n", 2053 __func__, error); 2054 goto out_unlock; 2055 } 2056 ravb_emac_init(ndev); 2057 2058 out: 2059 /* Initialise PTP Clock driver */ 2060 if (info->gptp) 2061 ravb_ptp_init(ndev, priv->pdev); 2062 2063 netif_tx_start_all_queues(ndev); 2064 2065 out_unlock: 2066 rtnl_unlock(); 2067 } 2068 2069 static bool ravb_can_tx_csum_gbeth(struct sk_buff *skb) 2070 { 2071 struct iphdr *ip = ip_hdr(skb); 2072 2073 /* TODO: Need to add support for VLAN tag 802.1Q */ 2074 if (skb_vlan_tag_present(skb)) 2075 return false; 2076 2077 /* TODO: Need to add hardware checksum for IPv6 */ 2078 if (skb->protocol != htons(ETH_P_IP)) 2079 return false; 2080 2081 switch (ip->protocol) { 2082 case IPPROTO_TCP: 2083 break; 2084 case IPPROTO_UDP: 2085 /* If the checksum value in the UDP header field is 0, TOE does 2086 * not calculate checksum for UDP part of this frame as it is 2087 * optional function as per standards. 2088 */ 2089 if (udp_hdr(skb)->check == 0) 2090 return false; 2091 break; 2092 default: 2093 return false; 2094 } 2095 2096 return true; 2097 } 2098 2099 /* Packet transmit function for Ethernet AVB */ 2100 static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev) 2101 { 2102 struct ravb_private *priv = netdev_priv(ndev); 2103 const struct ravb_hw_info *info = priv->info; 2104 unsigned int num_tx_desc = priv->num_tx_desc; 2105 u16 q = skb_get_queue_mapping(skb); 2106 struct ravb_tstamp_skb *ts_skb; 2107 struct ravb_tx_desc *desc; 2108 unsigned long flags; 2109 dma_addr_t dma_addr; 2110 void *buffer; 2111 u32 entry; 2112 u32 len; 2113 2114 if (skb->ip_summed == CHECKSUM_PARTIAL && !ravb_can_tx_csum_gbeth(skb)) 2115 skb_checksum_help(skb); 2116 2117 spin_lock_irqsave(&priv->lock, flags); 2118 if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) * 2119 num_tx_desc) { 2120 netif_err(priv, tx_queued, ndev, 2121 "still transmitting with the full ring!\n"); 2122 netif_stop_subqueue(ndev, q); 2123 spin_unlock_irqrestore(&priv->lock, flags); 2124 return NETDEV_TX_BUSY; 2125 } 2126 2127 if (skb_put_padto(skb, ETH_ZLEN)) 2128 goto exit; 2129 2130 entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * num_tx_desc); 2131 priv->tx_skb[q][entry / num_tx_desc] = skb; 2132 2133 if (num_tx_desc > 1) { 2134 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) + 2135 entry / num_tx_desc * DPTR_ALIGN; 2136 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data; 2137 2138 /* Zero length DMA descriptors are problematic as they seem 2139 * to terminate DMA transfers. Avoid them by simply using a 2140 * length of DPTR_ALIGN (4) when skb data is aligned to 2141 * DPTR_ALIGN. 2142 * 2143 * As skb is guaranteed to have at least ETH_ZLEN (60) 2144 * bytes of data by the call to skb_put_padto() above this 2145 * is safe with respect to both the length of the first DMA 2146 * descriptor (len) overflowing the available data and the 2147 * length of the second DMA descriptor (skb->len - len) 2148 * being negative. 2149 */ 2150 if (len == 0) 2151 len = DPTR_ALIGN; 2152 2153 memcpy(buffer, skb->data, len); 2154 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, 2155 DMA_TO_DEVICE); 2156 if (dma_mapping_error(ndev->dev.parent, dma_addr)) 2157 goto drop; 2158 2159 desc = &priv->tx_ring[q][entry]; 2160 desc->ds_tagl = cpu_to_le16(len); 2161 desc->dptr = cpu_to_le32(dma_addr); 2162 2163 buffer = skb->data + len; 2164 len = skb->len - len; 2165 dma_addr = dma_map_single(ndev->dev.parent, buffer, len, 2166 DMA_TO_DEVICE); 2167 if (dma_mapping_error(ndev->dev.parent, dma_addr)) 2168 goto unmap; 2169 2170 desc++; 2171 } else { 2172 desc = &priv->tx_ring[q][entry]; 2173 len = skb->len; 2174 dma_addr = dma_map_single(ndev->dev.parent, skb->data, skb->len, 2175 DMA_TO_DEVICE); 2176 if (dma_mapping_error(ndev->dev.parent, dma_addr)) 2177 goto drop; 2178 } 2179 desc->ds_tagl = cpu_to_le16(len); 2180 desc->dptr = cpu_to_le32(dma_addr); 2181 2182 /* TX timestamp required */ 2183 if (info->gptp || info->ccc_gac) { 2184 if (q == RAVB_NC) { 2185 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC); 2186 if (!ts_skb) { 2187 if (num_tx_desc > 1) { 2188 desc--; 2189 dma_unmap_single(ndev->dev.parent, dma_addr, 2190 len, DMA_TO_DEVICE); 2191 } 2192 goto unmap; 2193 } 2194 ts_skb->skb = skb_get(skb); 2195 ts_skb->tag = priv->ts_skb_tag++; 2196 priv->ts_skb_tag &= 0x3ff; 2197 list_add_tail(&ts_skb->list, &priv->ts_skb_list); 2198 2199 /* TAG and timestamp required flag */ 2200 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 2201 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR; 2202 desc->ds_tagl |= cpu_to_le16(ts_skb->tag << 12); 2203 } 2204 2205 skb_tx_timestamp(skb); 2206 } 2207 /* Descriptor type must be set after all the above writes */ 2208 dma_wmb(); 2209 if (num_tx_desc > 1) { 2210 desc->die_dt = DT_FEND; 2211 desc--; 2212 desc->die_dt = DT_FSTART; 2213 } else { 2214 desc->die_dt = DT_FSINGLE; 2215 } 2216 ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q); 2217 2218 priv->cur_tx[q] += num_tx_desc; 2219 if (priv->cur_tx[q] - priv->dirty_tx[q] > 2220 (priv->num_tx_ring[q] - 1) * num_tx_desc && 2221 !ravb_tx_free(ndev, q, true)) 2222 netif_stop_subqueue(ndev, q); 2223 2224 exit: 2225 spin_unlock_irqrestore(&priv->lock, flags); 2226 return NETDEV_TX_OK; 2227 2228 unmap: 2229 dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr), 2230 le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE); 2231 drop: 2232 dev_kfree_skb_any(skb); 2233 priv->tx_skb[q][entry / num_tx_desc] = NULL; 2234 goto exit; 2235 } 2236 2237 static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb, 2238 struct net_device *sb_dev) 2239 { 2240 /* If skb needs TX timestamp, it is handled in network control queue */ 2241 return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC : 2242 RAVB_BE; 2243 2244 } 2245 2246 static struct net_device_stats *ravb_get_stats(struct net_device *ndev) 2247 { 2248 struct ravb_private *priv = netdev_priv(ndev); 2249 const struct ravb_hw_info *info = priv->info; 2250 struct net_device_stats *nstats, *stats0, *stats1; 2251 2252 nstats = &ndev->stats; 2253 stats0 = &priv->stats[RAVB_BE]; 2254 2255 if (info->tx_counters) { 2256 nstats->tx_dropped += ravb_read(ndev, TROCR); 2257 ravb_write(ndev, 0, TROCR); /* (write clear) */ 2258 } 2259 2260 if (info->carrier_counters) { 2261 nstats->collisions += ravb_read(ndev, CXR41); 2262 ravb_write(ndev, 0, CXR41); /* (write clear) */ 2263 nstats->tx_carrier_errors += ravb_read(ndev, CXR42); 2264 ravb_write(ndev, 0, CXR42); /* (write clear) */ 2265 } 2266 2267 nstats->rx_packets = stats0->rx_packets; 2268 nstats->tx_packets = stats0->tx_packets; 2269 nstats->rx_bytes = stats0->rx_bytes; 2270 nstats->tx_bytes = stats0->tx_bytes; 2271 nstats->multicast = stats0->multicast; 2272 nstats->rx_errors = stats0->rx_errors; 2273 nstats->rx_crc_errors = stats0->rx_crc_errors; 2274 nstats->rx_frame_errors = stats0->rx_frame_errors; 2275 nstats->rx_length_errors = stats0->rx_length_errors; 2276 nstats->rx_missed_errors = stats0->rx_missed_errors; 2277 nstats->rx_over_errors = stats0->rx_over_errors; 2278 if (info->nc_queues) { 2279 stats1 = &priv->stats[RAVB_NC]; 2280 2281 nstats->rx_packets += stats1->rx_packets; 2282 nstats->tx_packets += stats1->tx_packets; 2283 nstats->rx_bytes += stats1->rx_bytes; 2284 nstats->tx_bytes += stats1->tx_bytes; 2285 nstats->multicast += stats1->multicast; 2286 nstats->rx_errors += stats1->rx_errors; 2287 nstats->rx_crc_errors += stats1->rx_crc_errors; 2288 nstats->rx_frame_errors += stats1->rx_frame_errors; 2289 nstats->rx_length_errors += stats1->rx_length_errors; 2290 nstats->rx_missed_errors += stats1->rx_missed_errors; 2291 nstats->rx_over_errors += stats1->rx_over_errors; 2292 } 2293 2294 return nstats; 2295 } 2296 2297 /* Update promiscuous bit */ 2298 static void ravb_set_rx_mode(struct net_device *ndev) 2299 { 2300 struct ravb_private *priv = netdev_priv(ndev); 2301 unsigned long flags; 2302 2303 spin_lock_irqsave(&priv->lock, flags); 2304 ravb_modify(ndev, ECMR, ECMR_PRM, 2305 ndev->flags & IFF_PROMISC ? ECMR_PRM : 0); 2306 spin_unlock_irqrestore(&priv->lock, flags); 2307 } 2308 2309 /* Device close function for Ethernet AVB */ 2310 static int ravb_close(struct net_device *ndev) 2311 { 2312 struct device_node *np = ndev->dev.parent->of_node; 2313 struct ravb_private *priv = netdev_priv(ndev); 2314 const struct ravb_hw_info *info = priv->info; 2315 struct ravb_tstamp_skb *ts_skb, *ts_skb2; 2316 2317 netif_tx_stop_all_queues(ndev); 2318 2319 /* Disable interrupts by clearing the interrupt masks. */ 2320 ravb_write(ndev, 0, RIC0); 2321 ravb_write(ndev, 0, RIC2); 2322 ravb_write(ndev, 0, TIC); 2323 2324 /* Stop PTP Clock driver */ 2325 if (info->gptp || info->ccc_gac) 2326 ravb_ptp_stop(ndev); 2327 2328 /* Set the config mode to stop the AVB-DMAC's processes */ 2329 if (ravb_stop_dma(ndev) < 0) 2330 netdev_err(ndev, 2331 "device will be stopped after h/w processes are done.\n"); 2332 2333 /* Clear the timestamp list */ 2334 if (info->gptp || info->ccc_gac) { 2335 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) { 2336 list_del(&ts_skb->list); 2337 kfree_skb(ts_skb->skb); 2338 kfree(ts_skb); 2339 } 2340 } 2341 2342 /* PHY disconnect */ 2343 if (ndev->phydev) { 2344 phy_stop(ndev->phydev); 2345 phy_disconnect(ndev->phydev); 2346 if (of_phy_is_fixed_link(np)) 2347 of_phy_deregister_fixed_link(np); 2348 } 2349 2350 cancel_work_sync(&priv->work); 2351 2352 if (info->nc_queues) 2353 napi_disable(&priv->napi[RAVB_NC]); 2354 napi_disable(&priv->napi[RAVB_BE]); 2355 2356 /* Free all the skb's in the RX queue and the DMA buffers. */ 2357 ravb_ring_free(ndev, RAVB_BE); 2358 if (info->nc_queues) 2359 ravb_ring_free(ndev, RAVB_NC); 2360 2361 /* Set reset mode. */ 2362 return ravb_set_opmode(ndev, CCC_OPC_RESET); 2363 } 2364 2365 static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req) 2366 { 2367 struct ravb_private *priv = netdev_priv(ndev); 2368 struct hwtstamp_config config; 2369 2370 config.flags = 0; 2371 config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON : 2372 HWTSTAMP_TX_OFF; 2373 switch (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE) { 2374 case RAVB_RXTSTAMP_TYPE_V2_L2_EVENT: 2375 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT; 2376 break; 2377 case RAVB_RXTSTAMP_TYPE_ALL: 2378 config.rx_filter = HWTSTAMP_FILTER_ALL; 2379 break; 2380 default: 2381 config.rx_filter = HWTSTAMP_FILTER_NONE; 2382 } 2383 2384 return copy_to_user(req->ifr_data, &config, sizeof(config)) ? 2385 -EFAULT : 0; 2386 } 2387 2388 /* Control hardware time stamping */ 2389 static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req) 2390 { 2391 struct ravb_private *priv = netdev_priv(ndev); 2392 struct hwtstamp_config config; 2393 u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED; 2394 u32 tstamp_tx_ctrl; 2395 2396 if (copy_from_user(&config, req->ifr_data, sizeof(config))) 2397 return -EFAULT; 2398 2399 switch (config.tx_type) { 2400 case HWTSTAMP_TX_OFF: 2401 tstamp_tx_ctrl = 0; 2402 break; 2403 case HWTSTAMP_TX_ON: 2404 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED; 2405 break; 2406 default: 2407 return -ERANGE; 2408 } 2409 2410 switch (config.rx_filter) { 2411 case HWTSTAMP_FILTER_NONE: 2412 tstamp_rx_ctrl = 0; 2413 break; 2414 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 2415 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT; 2416 break; 2417 default: 2418 config.rx_filter = HWTSTAMP_FILTER_ALL; 2419 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL; 2420 } 2421 2422 priv->tstamp_tx_ctrl = tstamp_tx_ctrl; 2423 priv->tstamp_rx_ctrl = tstamp_rx_ctrl; 2424 2425 return copy_to_user(req->ifr_data, &config, sizeof(config)) ? 2426 -EFAULT : 0; 2427 } 2428 2429 /* ioctl to device function */ 2430 static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd) 2431 { 2432 struct phy_device *phydev = ndev->phydev; 2433 2434 if (!netif_running(ndev)) 2435 return -EINVAL; 2436 2437 if (!phydev) 2438 return -ENODEV; 2439 2440 switch (cmd) { 2441 case SIOCGHWTSTAMP: 2442 return ravb_hwtstamp_get(ndev, req); 2443 case SIOCSHWTSTAMP: 2444 return ravb_hwtstamp_set(ndev, req); 2445 } 2446 2447 return phy_mii_ioctl(phydev, req, cmd); 2448 } 2449 2450 static int ravb_change_mtu(struct net_device *ndev, int new_mtu) 2451 { 2452 struct ravb_private *priv = netdev_priv(ndev); 2453 2454 ndev->mtu = new_mtu; 2455 2456 if (netif_running(ndev)) { 2457 synchronize_irq(priv->emac_irq); 2458 ravb_emac_init(ndev); 2459 } 2460 2461 netdev_update_features(ndev); 2462 2463 return 0; 2464 } 2465 2466 static void ravb_set_rx_csum(struct net_device *ndev, bool enable) 2467 { 2468 struct ravb_private *priv = netdev_priv(ndev); 2469 unsigned long flags; 2470 2471 spin_lock_irqsave(&priv->lock, flags); 2472 2473 /* Disable TX and RX */ 2474 ravb_rcv_snd_disable(ndev); 2475 2476 /* Modify RX Checksum setting */ 2477 ravb_modify(ndev, ECMR, ECMR_RCSC, enable ? ECMR_RCSC : 0); 2478 2479 /* Enable TX and RX */ 2480 ravb_rcv_snd_enable(ndev); 2481 2482 spin_unlock_irqrestore(&priv->lock, flags); 2483 } 2484 2485 static int ravb_endisable_csum_gbeth(struct net_device *ndev, enum ravb_reg reg, 2486 u32 val, u32 mask) 2487 { 2488 u32 csr0 = CSR0_TPE | CSR0_RPE; 2489 int ret; 2490 2491 ravb_write(ndev, csr0 & ~mask, CSR0); 2492 ret = ravb_wait(ndev, CSR0, mask, 0); 2493 if (!ret) 2494 ravb_write(ndev, val, reg); 2495 2496 ravb_write(ndev, csr0, CSR0); 2497 2498 return ret; 2499 } 2500 2501 static int ravb_set_features_gbeth(struct net_device *ndev, 2502 netdev_features_t features) 2503 { 2504 netdev_features_t changed = ndev->features ^ features; 2505 struct ravb_private *priv = netdev_priv(ndev); 2506 unsigned long flags; 2507 int ret = 0; 2508 u32 val; 2509 2510 spin_lock_irqsave(&priv->lock, flags); 2511 if (changed & NETIF_F_RXCSUM) { 2512 if (features & NETIF_F_RXCSUM) 2513 val = CSR2_RIP4 | CSR2_RTCP4 | CSR2_RUDP4 | CSR2_RICMP4; 2514 else 2515 val = 0; 2516 2517 ret = ravb_endisable_csum_gbeth(ndev, CSR2, val, CSR0_RPE); 2518 if (ret) 2519 goto done; 2520 } 2521 2522 if (changed & NETIF_F_HW_CSUM) { 2523 if (features & NETIF_F_HW_CSUM) 2524 val = CSR1_TIP4 | CSR1_TTCP4 | CSR1_TUDP4; 2525 else 2526 val = 0; 2527 2528 ret = ravb_endisable_csum_gbeth(ndev, CSR1, val, CSR0_TPE); 2529 if (ret) 2530 goto done; 2531 } 2532 2533 ndev->features = features; 2534 done: 2535 spin_unlock_irqrestore(&priv->lock, flags); 2536 2537 return ret; 2538 } 2539 2540 static int ravb_set_features_rcar(struct net_device *ndev, 2541 netdev_features_t features) 2542 { 2543 netdev_features_t changed = ndev->features ^ features; 2544 2545 if (changed & NETIF_F_RXCSUM) 2546 ravb_set_rx_csum(ndev, features & NETIF_F_RXCSUM); 2547 2548 ndev->features = features; 2549 2550 return 0; 2551 } 2552 2553 static int ravb_set_features(struct net_device *ndev, 2554 netdev_features_t features) 2555 { 2556 struct ravb_private *priv = netdev_priv(ndev); 2557 const struct ravb_hw_info *info = priv->info; 2558 2559 return info->set_feature(ndev, features); 2560 } 2561 2562 static const struct net_device_ops ravb_netdev_ops = { 2563 .ndo_open = ravb_open, 2564 .ndo_stop = ravb_close, 2565 .ndo_start_xmit = ravb_start_xmit, 2566 .ndo_select_queue = ravb_select_queue, 2567 .ndo_get_stats = ravb_get_stats, 2568 .ndo_set_rx_mode = ravb_set_rx_mode, 2569 .ndo_tx_timeout = ravb_tx_timeout, 2570 .ndo_eth_ioctl = ravb_do_ioctl, 2571 .ndo_change_mtu = ravb_change_mtu, 2572 .ndo_validate_addr = eth_validate_addr, 2573 .ndo_set_mac_address = eth_mac_addr, 2574 .ndo_set_features = ravb_set_features, 2575 }; 2576 2577 /* MDIO bus init function */ 2578 static int ravb_mdio_init(struct ravb_private *priv) 2579 { 2580 struct platform_device *pdev = priv->pdev; 2581 struct device *dev = &pdev->dev; 2582 struct phy_device *phydev; 2583 struct device_node *pn; 2584 int error; 2585 2586 /* Bitbang init */ 2587 priv->mdiobb.ops = &bb_ops; 2588 2589 /* MII controller setting */ 2590 priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb); 2591 if (!priv->mii_bus) 2592 return -ENOMEM; 2593 2594 /* Hook up MII support for ethtool */ 2595 priv->mii_bus->name = "ravb_mii"; 2596 priv->mii_bus->parent = dev; 2597 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", 2598 pdev->name, pdev->id); 2599 2600 /* Register MDIO bus */ 2601 error = of_mdiobus_register(priv->mii_bus, dev->of_node); 2602 if (error) 2603 goto out_free_bus; 2604 2605 pn = of_parse_phandle(dev->of_node, "phy-handle", 0); 2606 phydev = of_phy_find_device(pn); 2607 if (phydev) { 2608 phydev->mac_managed_pm = true; 2609 put_device(&phydev->mdio.dev); 2610 } 2611 of_node_put(pn); 2612 2613 return 0; 2614 2615 out_free_bus: 2616 free_mdio_bitbang(priv->mii_bus); 2617 return error; 2618 } 2619 2620 /* MDIO bus release function */ 2621 static int ravb_mdio_release(struct ravb_private *priv) 2622 { 2623 /* Unregister mdio bus */ 2624 mdiobus_unregister(priv->mii_bus); 2625 2626 /* Free bitbang info */ 2627 free_mdio_bitbang(priv->mii_bus); 2628 2629 return 0; 2630 } 2631 2632 static const struct ravb_hw_info ravb_gen3_hw_info = { 2633 .rx_ring_free = ravb_rx_ring_free_rcar, 2634 .rx_ring_format = ravb_rx_ring_format_rcar, 2635 .alloc_rx_desc = ravb_alloc_rx_desc_rcar, 2636 .receive = ravb_rx_rcar, 2637 .set_rate = ravb_set_rate_rcar, 2638 .set_feature = ravb_set_features_rcar, 2639 .dmac_init = ravb_dmac_init_rcar, 2640 .emac_init = ravb_emac_init_rcar, 2641 .gstrings_stats = ravb_gstrings_stats, 2642 .gstrings_size = sizeof(ravb_gstrings_stats), 2643 .net_hw_features = NETIF_F_RXCSUM, 2644 .net_features = NETIF_F_RXCSUM, 2645 .stats_len = ARRAY_SIZE(ravb_gstrings_stats), 2646 .max_rx_len = RX_BUF_SZ + RAVB_ALIGN - 1, 2647 .tccr_mask = TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 2648 .rx_max_buf_size = SZ_2K, 2649 .internal_delay = 1, 2650 .tx_counters = 1, 2651 .multi_irqs = 1, 2652 .irq_en_dis = 1, 2653 .ccc_gac = 1, 2654 .nc_queues = 1, 2655 .magic_pkt = 1, 2656 }; 2657 2658 static const struct ravb_hw_info ravb_gen2_hw_info = { 2659 .rx_ring_free = ravb_rx_ring_free_rcar, 2660 .rx_ring_format = ravb_rx_ring_format_rcar, 2661 .alloc_rx_desc = ravb_alloc_rx_desc_rcar, 2662 .receive = ravb_rx_rcar, 2663 .set_rate = ravb_set_rate_rcar, 2664 .set_feature = ravb_set_features_rcar, 2665 .dmac_init = ravb_dmac_init_rcar, 2666 .emac_init = ravb_emac_init_rcar, 2667 .gstrings_stats = ravb_gstrings_stats, 2668 .gstrings_size = sizeof(ravb_gstrings_stats), 2669 .net_hw_features = NETIF_F_RXCSUM, 2670 .net_features = NETIF_F_RXCSUM, 2671 .stats_len = ARRAY_SIZE(ravb_gstrings_stats), 2672 .max_rx_len = RX_BUF_SZ + RAVB_ALIGN - 1, 2673 .tccr_mask = TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 2674 .rx_max_buf_size = SZ_2K, 2675 .aligned_tx = 1, 2676 .gptp = 1, 2677 .nc_queues = 1, 2678 .magic_pkt = 1, 2679 }; 2680 2681 static const struct ravb_hw_info ravb_rzv2m_hw_info = { 2682 .rx_ring_free = ravb_rx_ring_free_rcar, 2683 .rx_ring_format = ravb_rx_ring_format_rcar, 2684 .alloc_rx_desc = ravb_alloc_rx_desc_rcar, 2685 .receive = ravb_rx_rcar, 2686 .set_rate = ravb_set_rate_rcar, 2687 .set_feature = ravb_set_features_rcar, 2688 .dmac_init = ravb_dmac_init_rcar, 2689 .emac_init = ravb_emac_init_rcar, 2690 .gstrings_stats = ravb_gstrings_stats, 2691 .gstrings_size = sizeof(ravb_gstrings_stats), 2692 .net_hw_features = NETIF_F_RXCSUM, 2693 .net_features = NETIF_F_RXCSUM, 2694 .stats_len = ARRAY_SIZE(ravb_gstrings_stats), 2695 .max_rx_len = RX_BUF_SZ + RAVB_ALIGN - 1, 2696 .tccr_mask = TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 2697 .rx_max_buf_size = SZ_2K, 2698 .multi_irqs = 1, 2699 .err_mgmt_irqs = 1, 2700 .gptp = 1, 2701 .gptp_ref_clk = 1, 2702 .nc_queues = 1, 2703 .magic_pkt = 1, 2704 }; 2705 2706 static const struct ravb_hw_info gbeth_hw_info = { 2707 .rx_ring_free = ravb_rx_ring_free_gbeth, 2708 .rx_ring_format = ravb_rx_ring_format_gbeth, 2709 .alloc_rx_desc = ravb_alloc_rx_desc_gbeth, 2710 .receive = ravb_rx_gbeth, 2711 .set_rate = ravb_set_rate_gbeth, 2712 .set_feature = ravb_set_features_gbeth, 2713 .dmac_init = ravb_dmac_init_gbeth, 2714 .emac_init = ravb_emac_init_gbeth, 2715 .gstrings_stats = ravb_gstrings_stats_gbeth, 2716 .gstrings_size = sizeof(ravb_gstrings_stats_gbeth), 2717 .net_hw_features = NETIF_F_RXCSUM | NETIF_F_HW_CSUM, 2718 .net_features = NETIF_F_RXCSUM | NETIF_F_HW_CSUM, 2719 .stats_len = ARRAY_SIZE(ravb_gstrings_stats_gbeth), 2720 .max_rx_len = ALIGN(GBETH_RX_BUFF_MAX, RAVB_ALIGN), 2721 .tccr_mask = TCCR_TSRQ0, 2722 .rx_max_buf_size = SZ_8K, 2723 .aligned_tx = 1, 2724 .tx_counters = 1, 2725 .carrier_counters = 1, 2726 .half_duplex = 1, 2727 }; 2728 2729 static const struct of_device_id ravb_match_table[] = { 2730 { .compatible = "renesas,etheravb-r8a7790", .data = &ravb_gen2_hw_info }, 2731 { .compatible = "renesas,etheravb-r8a7794", .data = &ravb_gen2_hw_info }, 2732 { .compatible = "renesas,etheravb-rcar-gen2", .data = &ravb_gen2_hw_info }, 2733 { .compatible = "renesas,etheravb-r8a7795", .data = &ravb_gen3_hw_info }, 2734 { .compatible = "renesas,etheravb-rcar-gen3", .data = &ravb_gen3_hw_info }, 2735 { .compatible = "renesas,etheravb-rcar-gen4", .data = &ravb_gen3_hw_info }, 2736 { .compatible = "renesas,etheravb-rzv2m", .data = &ravb_rzv2m_hw_info }, 2737 { .compatible = "renesas,rzg2l-gbeth", .data = &gbeth_hw_info }, 2738 { } 2739 }; 2740 MODULE_DEVICE_TABLE(of, ravb_match_table); 2741 2742 static int ravb_setup_irq(struct ravb_private *priv, const char *irq_name, 2743 const char *ch, int *irq, irq_handler_t handler) 2744 { 2745 struct platform_device *pdev = priv->pdev; 2746 struct net_device *ndev = priv->ndev; 2747 struct device *dev = &pdev->dev; 2748 const char *dev_name; 2749 unsigned long flags; 2750 int error; 2751 2752 if (irq_name) { 2753 dev_name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch); 2754 if (!dev_name) 2755 return -ENOMEM; 2756 2757 *irq = platform_get_irq_byname(pdev, irq_name); 2758 flags = 0; 2759 } else { 2760 dev_name = ndev->name; 2761 *irq = platform_get_irq(pdev, 0); 2762 flags = IRQF_SHARED; 2763 } 2764 if (*irq < 0) 2765 return *irq; 2766 2767 error = devm_request_irq(dev, *irq, handler, flags, dev_name, ndev); 2768 if (error) 2769 netdev_err(ndev, "cannot request IRQ %s\n", dev_name); 2770 2771 return error; 2772 } 2773 2774 static int ravb_setup_irqs(struct ravb_private *priv) 2775 { 2776 const struct ravb_hw_info *info = priv->info; 2777 struct net_device *ndev = priv->ndev; 2778 const char *irq_name, *emac_irq_name; 2779 int error, irq; 2780 2781 if (!info->multi_irqs) 2782 return ravb_setup_irq(priv, NULL, NULL, &ndev->irq, ravb_interrupt); 2783 2784 if (info->err_mgmt_irqs) { 2785 irq_name = "dia"; 2786 emac_irq_name = "line3"; 2787 } else { 2788 irq_name = "ch22"; 2789 emac_irq_name = "ch24"; 2790 } 2791 2792 error = ravb_setup_irq(priv, irq_name, "ch22:multi", &ndev->irq, ravb_multi_interrupt); 2793 if (error) 2794 return error; 2795 2796 error = ravb_setup_irq(priv, emac_irq_name, "ch24:emac", &priv->emac_irq, 2797 ravb_emac_interrupt); 2798 if (error) 2799 return error; 2800 2801 if (info->err_mgmt_irqs) { 2802 error = ravb_setup_irq(priv, "err_a", "err_a", &irq, ravb_multi_interrupt); 2803 if (error) 2804 return error; 2805 2806 error = ravb_setup_irq(priv, "mgmt_a", "mgmt_a", &irq, ravb_multi_interrupt); 2807 if (error) 2808 return error; 2809 } 2810 2811 error = ravb_setup_irq(priv, "ch0", "ch0:rx_be", &irq, ravb_be_interrupt); 2812 if (error) 2813 return error; 2814 2815 error = ravb_setup_irq(priv, "ch1", "ch1:rx_nc", &irq, ravb_nc_interrupt); 2816 if (error) 2817 return error; 2818 2819 error = ravb_setup_irq(priv, "ch18", "ch18:tx_be", &irq, ravb_be_interrupt); 2820 if (error) 2821 return error; 2822 2823 return ravb_setup_irq(priv, "ch19", "ch19:tx_nc", &irq, ravb_nc_interrupt); 2824 } 2825 2826 static int ravb_probe(struct platform_device *pdev) 2827 { 2828 struct device_node *np = pdev->dev.of_node; 2829 const struct ravb_hw_info *info; 2830 struct reset_control *rstc; 2831 struct ravb_private *priv; 2832 struct net_device *ndev; 2833 struct resource *res; 2834 int error, q; 2835 2836 if (!np) { 2837 dev_err(&pdev->dev, 2838 "this driver is required to be instantiated from device tree\n"); 2839 return -EINVAL; 2840 } 2841 2842 rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL); 2843 if (IS_ERR(rstc)) 2844 return dev_err_probe(&pdev->dev, PTR_ERR(rstc), 2845 "failed to get cpg reset\n"); 2846 2847 ndev = alloc_etherdev_mqs(sizeof(struct ravb_private), 2848 NUM_TX_QUEUE, NUM_RX_QUEUE); 2849 if (!ndev) 2850 return -ENOMEM; 2851 2852 info = of_device_get_match_data(&pdev->dev); 2853 2854 ndev->features = info->net_features; 2855 ndev->hw_features = info->net_hw_features; 2856 2857 error = reset_control_deassert(rstc); 2858 if (error) 2859 goto out_free_netdev; 2860 2861 SET_NETDEV_DEV(ndev, &pdev->dev); 2862 2863 priv = netdev_priv(ndev); 2864 priv->info = info; 2865 priv->rstc = rstc; 2866 priv->ndev = ndev; 2867 priv->pdev = pdev; 2868 priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE; 2869 priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE; 2870 if (info->nc_queues) { 2871 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE; 2872 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE; 2873 } 2874 2875 error = ravb_setup_irqs(priv); 2876 if (error) 2877 goto out_reset_assert; 2878 2879 priv->clk = devm_clk_get(&pdev->dev, NULL); 2880 if (IS_ERR(priv->clk)) { 2881 error = PTR_ERR(priv->clk); 2882 goto out_reset_assert; 2883 } 2884 2885 if (info->gptp_ref_clk) { 2886 priv->gptp_clk = devm_clk_get(&pdev->dev, "gptp"); 2887 if (IS_ERR(priv->gptp_clk)) { 2888 error = PTR_ERR(priv->gptp_clk); 2889 goto out_reset_assert; 2890 } 2891 } 2892 2893 priv->refclk = devm_clk_get_optional(&pdev->dev, "refclk"); 2894 if (IS_ERR(priv->refclk)) { 2895 error = PTR_ERR(priv->refclk); 2896 goto out_reset_assert; 2897 } 2898 clk_prepare(priv->refclk); 2899 2900 platform_set_drvdata(pdev, ndev); 2901 pm_runtime_enable(&pdev->dev); 2902 error = pm_runtime_resume_and_get(&pdev->dev); 2903 if (error < 0) 2904 goto out_rpm_disable; 2905 2906 priv->addr = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 2907 if (IS_ERR(priv->addr)) { 2908 error = PTR_ERR(priv->addr); 2909 goto out_rpm_put; 2910 } 2911 2912 /* The Ether-specific entries in the device structure. */ 2913 ndev->base_addr = res->start; 2914 2915 spin_lock_init(&priv->lock); 2916 INIT_WORK(&priv->work, ravb_tx_timeout_work); 2917 2918 error = of_get_phy_mode(np, &priv->phy_interface); 2919 if (error && error != -ENODEV) 2920 goto out_rpm_put; 2921 2922 priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link"); 2923 priv->avb_link_active_low = 2924 of_property_read_bool(np, "renesas,ether-link-active-low"); 2925 2926 ndev->max_mtu = info->rx_max_buf_size - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN); 2927 ndev->min_mtu = ETH_MIN_MTU; 2928 2929 /* FIXME: R-Car Gen2 has 4byte alignment restriction for tx buffer 2930 * Use two descriptor to handle such situation. First descriptor to 2931 * handle aligned data buffer and second descriptor to handle the 2932 * overflow data because of alignment. 2933 */ 2934 priv->num_tx_desc = info->aligned_tx ? 2 : 1; 2935 2936 /* Set function */ 2937 ndev->netdev_ops = &ravb_netdev_ops; 2938 ndev->ethtool_ops = &ravb_ethtool_ops; 2939 2940 error = ravb_compute_gti(ndev); 2941 if (error) 2942 goto out_rpm_put; 2943 2944 ravb_parse_delay_mode(np, ndev); 2945 2946 /* Allocate descriptor base address table */ 2947 priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM; 2948 priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size, 2949 &priv->desc_bat_dma, GFP_KERNEL); 2950 if (!priv->desc_bat) { 2951 dev_err(&pdev->dev, 2952 "Cannot allocate desc base address table (size %d bytes)\n", 2953 priv->desc_bat_size); 2954 error = -ENOMEM; 2955 goto out_rpm_put; 2956 } 2957 for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++) 2958 priv->desc_bat[q].die_dt = DT_EOS; 2959 2960 /* Initialise HW timestamp list */ 2961 INIT_LIST_HEAD(&priv->ts_skb_list); 2962 2963 /* Debug message level */ 2964 priv->msg_enable = RAVB_DEF_MSG_ENABLE; 2965 2966 /* Set config mode as this is needed for PHY initialization. */ 2967 error = ravb_set_opmode(ndev, CCC_OPC_CONFIG); 2968 if (error) 2969 goto out_rpm_put; 2970 2971 /* Read and set MAC address */ 2972 ravb_read_mac_address(np, ndev); 2973 if (!is_valid_ether_addr(ndev->dev_addr)) { 2974 dev_warn(&pdev->dev, 2975 "no valid MAC address supplied, using a random one\n"); 2976 eth_hw_addr_random(ndev); 2977 } 2978 2979 /* MDIO bus init */ 2980 error = ravb_mdio_init(priv); 2981 if (error) { 2982 dev_err(&pdev->dev, "failed to initialize MDIO\n"); 2983 goto out_reset_mode; 2984 } 2985 2986 /* Undo previous switch to config opmode. */ 2987 error = ravb_set_opmode(ndev, CCC_OPC_RESET); 2988 if (error) 2989 goto out_mdio_release; 2990 2991 netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll); 2992 if (info->nc_queues) 2993 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll); 2994 2995 /* Network device register */ 2996 error = register_netdev(ndev); 2997 if (error) 2998 goto out_napi_del; 2999 3000 device_set_wakeup_capable(&pdev->dev, 1); 3001 3002 /* Print device information */ 3003 netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n", 3004 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq); 3005 3006 return 0; 3007 3008 out_napi_del: 3009 if (info->nc_queues) 3010 netif_napi_del(&priv->napi[RAVB_NC]); 3011 3012 netif_napi_del(&priv->napi[RAVB_BE]); 3013 out_mdio_release: 3014 ravb_mdio_release(priv); 3015 out_reset_mode: 3016 ravb_set_opmode(ndev, CCC_OPC_RESET); 3017 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat, 3018 priv->desc_bat_dma); 3019 out_rpm_put: 3020 pm_runtime_put(&pdev->dev); 3021 out_rpm_disable: 3022 pm_runtime_disable(&pdev->dev); 3023 clk_unprepare(priv->refclk); 3024 out_reset_assert: 3025 reset_control_assert(rstc); 3026 out_free_netdev: 3027 free_netdev(ndev); 3028 return error; 3029 } 3030 3031 static void ravb_remove(struct platform_device *pdev) 3032 { 3033 struct net_device *ndev = platform_get_drvdata(pdev); 3034 struct ravb_private *priv = netdev_priv(ndev); 3035 const struct ravb_hw_info *info = priv->info; 3036 3037 unregister_netdev(ndev); 3038 if (info->nc_queues) 3039 netif_napi_del(&priv->napi[RAVB_NC]); 3040 netif_napi_del(&priv->napi[RAVB_BE]); 3041 3042 ravb_mdio_release(priv); 3043 3044 dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat, 3045 priv->desc_bat_dma); 3046 3047 pm_runtime_put_sync(&pdev->dev); 3048 pm_runtime_disable(&pdev->dev); 3049 clk_unprepare(priv->refclk); 3050 reset_control_assert(priv->rstc); 3051 free_netdev(ndev); 3052 platform_set_drvdata(pdev, NULL); 3053 } 3054 3055 static int ravb_wol_setup(struct net_device *ndev) 3056 { 3057 struct ravb_private *priv = netdev_priv(ndev); 3058 const struct ravb_hw_info *info = priv->info; 3059 3060 /* Disable interrupts by clearing the interrupt masks. */ 3061 ravb_write(ndev, 0, RIC0); 3062 ravb_write(ndev, 0, RIC2); 3063 ravb_write(ndev, 0, TIC); 3064 3065 /* Only allow ECI interrupts */ 3066 synchronize_irq(priv->emac_irq); 3067 if (info->nc_queues) 3068 napi_disable(&priv->napi[RAVB_NC]); 3069 napi_disable(&priv->napi[RAVB_BE]); 3070 ravb_write(ndev, ECSIPR_MPDIP, ECSIPR); 3071 3072 /* Enable MagicPacket */ 3073 ravb_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE); 3074 3075 if (priv->info->ccc_gac) 3076 ravb_ptp_stop(ndev); 3077 3078 return enable_irq_wake(priv->emac_irq); 3079 } 3080 3081 static int ravb_wol_restore(struct net_device *ndev) 3082 { 3083 struct ravb_private *priv = netdev_priv(ndev); 3084 const struct ravb_hw_info *info = priv->info; 3085 int error; 3086 3087 /* Set reset mode to rearm the WoL logic. */ 3088 error = ravb_set_opmode(ndev, CCC_OPC_RESET); 3089 if (error) 3090 return error; 3091 3092 /* Set AVB config mode. */ 3093 error = ravb_set_config_mode(ndev); 3094 if (error) 3095 return error; 3096 3097 if (priv->info->ccc_gac) 3098 ravb_ptp_init(ndev, priv->pdev); 3099 3100 if (info->nc_queues) 3101 napi_enable(&priv->napi[RAVB_NC]); 3102 napi_enable(&priv->napi[RAVB_BE]); 3103 3104 /* Disable MagicPacket */ 3105 ravb_modify(ndev, ECMR, ECMR_MPDE, 0); 3106 3107 ravb_close(ndev); 3108 3109 return disable_irq_wake(priv->emac_irq); 3110 } 3111 3112 static int ravb_suspend(struct device *dev) 3113 { 3114 struct net_device *ndev = dev_get_drvdata(dev); 3115 struct ravb_private *priv = netdev_priv(ndev); 3116 int ret; 3117 3118 if (!netif_running(ndev)) 3119 goto reset_assert; 3120 3121 netif_device_detach(ndev); 3122 3123 if (priv->wol_enabled) 3124 return ravb_wol_setup(ndev); 3125 3126 ret = ravb_close(ndev); 3127 if (ret) 3128 return ret; 3129 3130 reset_assert: 3131 return reset_control_assert(priv->rstc); 3132 } 3133 3134 static int ravb_resume(struct device *dev) 3135 { 3136 struct net_device *ndev = dev_get_drvdata(dev); 3137 struct ravb_private *priv = netdev_priv(ndev); 3138 int ret; 3139 3140 ret = reset_control_deassert(priv->rstc); 3141 if (ret) 3142 return ret; 3143 3144 if (!netif_running(ndev)) 3145 return 0; 3146 3147 /* If WoL is enabled restore the interface. */ 3148 if (priv->wol_enabled) { 3149 ret = ravb_wol_restore(ndev); 3150 if (ret) 3151 return ret; 3152 } 3153 3154 /* Reopening the interface will restore the device to the working state. */ 3155 ret = ravb_open(ndev); 3156 if (ret < 0) 3157 return ret; 3158 3159 ravb_set_rx_mode(ndev); 3160 netif_device_attach(ndev); 3161 3162 return ret; 3163 } 3164 3165 static int ravb_runtime_suspend(struct device *dev) 3166 { 3167 struct net_device *ndev = dev_get_drvdata(dev); 3168 struct ravb_private *priv = netdev_priv(ndev); 3169 3170 clk_disable(priv->refclk); 3171 3172 return 0; 3173 } 3174 3175 static int ravb_runtime_resume(struct device *dev) 3176 { 3177 struct net_device *ndev = dev_get_drvdata(dev); 3178 struct ravb_private *priv = netdev_priv(ndev); 3179 3180 return clk_enable(priv->refclk); 3181 } 3182 3183 static const struct dev_pm_ops ravb_dev_pm_ops = { 3184 SYSTEM_SLEEP_PM_OPS(ravb_suspend, ravb_resume) 3185 RUNTIME_PM_OPS(ravb_runtime_suspend, ravb_runtime_resume, NULL) 3186 }; 3187 3188 static struct platform_driver ravb_driver = { 3189 .probe = ravb_probe, 3190 .remove_new = ravb_remove, 3191 .driver = { 3192 .name = "ravb", 3193 .pm = pm_ptr(&ravb_dev_pm_ops), 3194 .of_match_table = ravb_match_table, 3195 }, 3196 }; 3197 3198 module_platform_driver(ravb_driver); 3199 3200 MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai"); 3201 MODULE_DESCRIPTION("Renesas Ethernet AVB driver"); 3202 MODULE_LICENSE("GPL v2"); 3203