1 // SPDX-License-Identifier: GPL-2.0 2 /* Renesas Ethernet Switch device driver 3 * 4 * Copyright (C) 2022 Renesas Electronics Corporation 5 */ 6 7 #include <linux/dma-mapping.h> 8 #include <linux/err.h> 9 #include <linux/etherdevice.h> 10 #include <linux/iopoll.h> 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/net_tstamp.h> 14 #include <linux/of.h> 15 #include <linux/of_device.h> 16 #include <linux/of_irq.h> 17 #include <linux/of_mdio.h> 18 #include <linux/of_net.h> 19 #include <linux/phylink.h> 20 #include <linux/phy/phy.h> 21 #include <linux/pm_runtime.h> 22 #include <linux/rtnetlink.h> 23 #include <linux/slab.h> 24 #include <linux/spinlock.h> 25 26 #include "rswitch.h" 27 28 static int rswitch_reg_wait(void __iomem *addr, u32 offs, u32 mask, u32 expected) 29 { 30 u32 val; 31 32 return readl_poll_timeout_atomic(addr + offs, val, (val & mask) == expected, 33 1, RSWITCH_TIMEOUT_US); 34 } 35 36 static void rswitch_modify(void __iomem *addr, enum rswitch_reg reg, u32 clear, u32 set) 37 { 38 iowrite32((ioread32(addr + reg) & ~clear) | set, addr + reg); 39 } 40 41 /* Common Agent block (COMA) */ 42 static void rswitch_reset(struct rswitch_private *priv) 43 { 44 iowrite32(RRC_RR, priv->addr + RRC); 45 iowrite32(RRC_RR_CLR, priv->addr + RRC); 46 } 47 48 static void rswitch_clock_enable(struct rswitch_private *priv) 49 { 50 iowrite32(RCEC_ACE_DEFAULT | RCEC_RCE, priv->addr + RCEC); 51 } 52 53 static void rswitch_clock_disable(struct rswitch_private *priv) 54 { 55 iowrite32(RCDC_RCD, priv->addr + RCDC); 56 } 57 58 static bool rswitch_agent_clock_is_enabled(void __iomem *coma_addr, int port) 59 { 60 u32 val = ioread32(coma_addr + RCEC); 61 62 if (val & RCEC_RCE) 63 return (val & BIT(port)) ? true : false; 64 else 65 return false; 66 } 67 68 static void rswitch_agent_clock_ctrl(void __iomem *coma_addr, int port, int enable) 69 { 70 u32 val; 71 72 if (enable) { 73 val = ioread32(coma_addr + RCEC); 74 iowrite32(val | RCEC_RCE | BIT(port), coma_addr + RCEC); 75 } else { 76 val = ioread32(coma_addr + RCDC); 77 iowrite32(val | BIT(port), coma_addr + RCDC); 78 } 79 } 80 81 static int rswitch_bpool_config(struct rswitch_private *priv) 82 { 83 u32 val; 84 85 val = ioread32(priv->addr + CABPIRM); 86 if (val & CABPIRM_BPR) 87 return 0; 88 89 iowrite32(CABPIRM_BPIOG, priv->addr + CABPIRM); 90 91 return rswitch_reg_wait(priv->addr, CABPIRM, CABPIRM_BPR, CABPIRM_BPR); 92 } 93 94 /* R-Switch-2 block (TOP) */ 95 static void rswitch_top_init(struct rswitch_private *priv) 96 { 97 int i; 98 99 for (i = 0; i < RSWITCH_MAX_NUM_QUEUES; i++) 100 iowrite32((i / 16) << (GWCA_INDEX * 8), priv->addr + TPEMIMC7(i)); 101 } 102 103 /* Forwarding engine block (MFWD) */ 104 static void rswitch_fwd_init(struct rswitch_private *priv) 105 { 106 int i; 107 108 /* For ETHA */ 109 for (i = 0; i < RSWITCH_NUM_PORTS; i++) { 110 iowrite32(FWPC0_DEFAULT, priv->addr + FWPC0(i)); 111 iowrite32(0, priv->addr + FWPBFC(i)); 112 } 113 114 for (i = 0; i < RSWITCH_NUM_PORTS; i++) { 115 iowrite32(priv->rdev[i]->rx_queue->index, 116 priv->addr + FWPBFCSDC(GWCA_INDEX, i)); 117 iowrite32(BIT(priv->gwca.index), priv->addr + FWPBFC(i)); 118 } 119 120 /* For GWCA */ 121 iowrite32(FWPC0_DEFAULT, priv->addr + FWPC0(priv->gwca.index)); 122 iowrite32(FWPC1_DDE, priv->addr + FWPC1(priv->gwca.index)); 123 iowrite32(0, priv->addr + FWPBFC(priv->gwca.index)); 124 iowrite32(GENMASK(RSWITCH_NUM_PORTS - 1, 0), priv->addr + FWPBFC(priv->gwca.index)); 125 } 126 127 /* gPTP timer (gPTP) */ 128 static void rswitch_get_timestamp(struct rswitch_private *priv, 129 struct timespec64 *ts) 130 { 131 priv->ptp_priv->info.gettime64(&priv->ptp_priv->info, ts); 132 } 133 134 /* Gateway CPU agent block (GWCA) */ 135 static int rswitch_gwca_change_mode(struct rswitch_private *priv, 136 enum rswitch_gwca_mode mode) 137 { 138 int ret; 139 140 if (!rswitch_agent_clock_is_enabled(priv->addr, priv->gwca.index)) 141 rswitch_agent_clock_ctrl(priv->addr, priv->gwca.index, 1); 142 143 iowrite32(mode, priv->addr + GWMC); 144 145 ret = rswitch_reg_wait(priv->addr, GWMS, GWMS_OPS_MASK, mode); 146 147 if (mode == GWMC_OPC_DISABLE) 148 rswitch_agent_clock_ctrl(priv->addr, priv->gwca.index, 0); 149 150 return ret; 151 } 152 153 static int rswitch_gwca_mcast_table_reset(struct rswitch_private *priv) 154 { 155 iowrite32(GWMTIRM_MTIOG, priv->addr + GWMTIRM); 156 157 return rswitch_reg_wait(priv->addr, GWMTIRM, GWMTIRM_MTR, GWMTIRM_MTR); 158 } 159 160 static int rswitch_gwca_axi_ram_reset(struct rswitch_private *priv) 161 { 162 iowrite32(GWARIRM_ARIOG, priv->addr + GWARIRM); 163 164 return rswitch_reg_wait(priv->addr, GWARIRM, GWARIRM_ARR, GWARIRM_ARR); 165 } 166 167 static void rswitch_gwca_set_rate_limit(struct rswitch_private *priv, int rate) 168 { 169 u32 gwgrlulc, gwgrlc; 170 171 switch (rate) { 172 case 1000: 173 gwgrlulc = 0x0000005f; 174 gwgrlc = 0x00010260; 175 break; 176 default: 177 dev_err(&priv->pdev->dev, "%s: This rate is not supported (%d)\n", __func__, rate); 178 return; 179 } 180 181 iowrite32(gwgrlulc, priv->addr + GWGRLULC); 182 iowrite32(gwgrlc, priv->addr + GWGRLC); 183 } 184 185 static bool rswitch_is_any_data_irq(struct rswitch_private *priv, u32 *dis, bool tx) 186 { 187 u32 *mask = tx ? priv->gwca.tx_irq_bits : priv->gwca.rx_irq_bits; 188 int i; 189 190 for (i = 0; i < RSWITCH_NUM_IRQ_REGS; i++) { 191 if (dis[i] & mask[i]) 192 return true; 193 } 194 195 return false; 196 } 197 198 static void rswitch_get_data_irq_status(struct rswitch_private *priv, u32 *dis) 199 { 200 int i; 201 202 for (i = 0; i < RSWITCH_NUM_IRQ_REGS; i++) { 203 dis[i] = ioread32(priv->addr + GWDIS(i)); 204 dis[i] &= ioread32(priv->addr + GWDIE(i)); 205 } 206 } 207 208 static void rswitch_enadis_data_irq(struct rswitch_private *priv, int index, bool enable) 209 { 210 u32 offs = enable ? GWDIE(index / 32) : GWDID(index / 32); 211 212 iowrite32(BIT(index % 32), priv->addr + offs); 213 } 214 215 static void rswitch_ack_data_irq(struct rswitch_private *priv, int index) 216 { 217 u32 offs = GWDIS(index / 32); 218 219 iowrite32(BIT(index % 32), priv->addr + offs); 220 } 221 222 static u32 rswitch_next_queue_index(struct rswitch_gwca_queue *gq, bool cur, u32 num) 223 { 224 u32 index = cur ? gq->cur : gq->dirty; 225 226 if (index + num >= gq->ring_size) 227 index = (index + num) % gq->ring_size; 228 else 229 index += num; 230 231 return index; 232 } 233 234 static u32 rswitch_get_num_cur_queues(struct rswitch_gwca_queue *gq) 235 { 236 if (gq->cur >= gq->dirty) 237 return gq->cur - gq->dirty; 238 else 239 return gq->ring_size - gq->dirty + gq->cur; 240 } 241 242 static bool rswitch_is_queue_rxed(struct rswitch_gwca_queue *gq) 243 { 244 struct rswitch_ext_ts_desc *desc = &gq->ts_ring[gq->dirty]; 245 246 if ((desc->desc.die_dt & DT_MASK) != DT_FEMPTY) 247 return true; 248 249 return false; 250 } 251 252 static int rswitch_gwca_queue_alloc_skb(struct rswitch_gwca_queue *gq, 253 u32 start_index, u32 num) 254 { 255 u32 i, index; 256 257 for (i = 0; i < num; i++) { 258 index = (i + start_index) % gq->ring_size; 259 if (gq->skbs[index]) 260 continue; 261 gq->skbs[index] = netdev_alloc_skb_ip_align(gq->ndev, 262 PKT_BUF_SZ + RSWITCH_ALIGN - 1); 263 if (!gq->skbs[index]) 264 goto err; 265 } 266 267 return 0; 268 269 err: 270 for (i--; i >= 0; i--) { 271 index = (i + start_index) % gq->ring_size; 272 dev_kfree_skb(gq->skbs[index]); 273 gq->skbs[index] = NULL; 274 } 275 276 return -ENOMEM; 277 } 278 279 static void rswitch_gwca_queue_free(struct net_device *ndev, 280 struct rswitch_gwca_queue *gq) 281 { 282 int i; 283 284 if (gq->gptp) { 285 dma_free_coherent(ndev->dev.parent, 286 sizeof(struct rswitch_ext_ts_desc) * 287 (gq->ring_size + 1), gq->ts_ring, gq->ring_dma); 288 gq->ts_ring = NULL; 289 } else { 290 dma_free_coherent(ndev->dev.parent, 291 sizeof(struct rswitch_ext_desc) * 292 (gq->ring_size + 1), gq->ring, gq->ring_dma); 293 gq->ring = NULL; 294 } 295 296 if (!gq->dir_tx) { 297 for (i = 0; i < gq->ring_size; i++) 298 dev_kfree_skb(gq->skbs[i]); 299 } 300 301 kfree(gq->skbs); 302 gq->skbs = NULL; 303 } 304 305 static int rswitch_gwca_queue_alloc(struct net_device *ndev, 306 struct rswitch_private *priv, 307 struct rswitch_gwca_queue *gq, 308 bool dir_tx, bool gptp, int ring_size) 309 { 310 int i, bit; 311 312 gq->dir_tx = dir_tx; 313 gq->gptp = gptp; 314 gq->ring_size = ring_size; 315 gq->ndev = ndev; 316 317 gq->skbs = kcalloc(gq->ring_size, sizeof(*gq->skbs), GFP_KERNEL); 318 if (!gq->skbs) 319 return -ENOMEM; 320 321 if (!dir_tx) 322 rswitch_gwca_queue_alloc_skb(gq, 0, gq->ring_size); 323 324 if (gptp) 325 gq->ts_ring = dma_alloc_coherent(ndev->dev.parent, 326 sizeof(struct rswitch_ext_ts_desc) * 327 (gq->ring_size + 1), &gq->ring_dma, GFP_KERNEL); 328 else 329 gq->ring = dma_alloc_coherent(ndev->dev.parent, 330 sizeof(struct rswitch_ext_desc) * 331 (gq->ring_size + 1), &gq->ring_dma, GFP_KERNEL); 332 if (!gq->ts_ring && !gq->ring) 333 goto out; 334 335 i = gq->index / 32; 336 bit = BIT(gq->index % 32); 337 if (dir_tx) 338 priv->gwca.tx_irq_bits[i] |= bit; 339 else 340 priv->gwca.rx_irq_bits[i] |= bit; 341 342 return 0; 343 344 out: 345 rswitch_gwca_queue_free(ndev, gq); 346 347 return -ENOMEM; 348 } 349 350 static void rswitch_desc_set_dptr(struct rswitch_desc *desc, dma_addr_t addr) 351 { 352 desc->dptrl = cpu_to_le32(lower_32_bits(addr)); 353 desc->dptrh = upper_32_bits(addr) & 0xff; 354 } 355 356 static dma_addr_t rswitch_desc_get_dptr(const struct rswitch_desc *desc) 357 { 358 return __le32_to_cpu(desc->dptrl) | (u64)(desc->dptrh) << 32; 359 } 360 361 static int rswitch_gwca_queue_format(struct net_device *ndev, 362 struct rswitch_private *priv, 363 struct rswitch_gwca_queue *gq) 364 { 365 int tx_ring_size = sizeof(struct rswitch_ext_desc) * gq->ring_size; 366 struct rswitch_ext_desc *desc; 367 struct rswitch_desc *linkfix; 368 dma_addr_t dma_addr; 369 int i; 370 371 memset(gq->ring, 0, tx_ring_size); 372 for (i = 0, desc = gq->ring; i < gq->ring_size; i++, desc++) { 373 if (!gq->dir_tx) { 374 dma_addr = dma_map_single(ndev->dev.parent, 375 gq->skbs[i]->data, PKT_BUF_SZ, 376 DMA_FROM_DEVICE); 377 if (dma_mapping_error(ndev->dev.parent, dma_addr)) 378 goto err; 379 380 desc->desc.info_ds = cpu_to_le16(PKT_BUF_SZ); 381 rswitch_desc_set_dptr(&desc->desc, dma_addr); 382 desc->desc.die_dt = DT_FEMPTY | DIE; 383 } else { 384 desc->desc.die_dt = DT_EEMPTY | DIE; 385 } 386 } 387 rswitch_desc_set_dptr(&desc->desc, gq->ring_dma); 388 desc->desc.die_dt = DT_LINKFIX; 389 390 linkfix = &priv->linkfix_table[gq->index]; 391 linkfix->die_dt = DT_LINKFIX; 392 rswitch_desc_set_dptr(linkfix, gq->ring_dma); 393 394 iowrite32(GWDCC_BALR | (gq->dir_tx ? GWDCC_DQT : 0) | GWDCC_EDE, 395 priv->addr + GWDCC_OFFS(gq->index)); 396 397 return 0; 398 399 err: 400 if (!gq->dir_tx) { 401 for (i--, desc = gq->ring; i >= 0; i--, desc++) { 402 dma_addr = rswitch_desc_get_dptr(&desc->desc); 403 dma_unmap_single(ndev->dev.parent, dma_addr, PKT_BUF_SZ, 404 DMA_FROM_DEVICE); 405 } 406 } 407 408 return -ENOMEM; 409 } 410 411 static int rswitch_gwca_queue_ts_fill(struct net_device *ndev, 412 struct rswitch_gwca_queue *gq, 413 u32 start_index, u32 num) 414 { 415 struct rswitch_device *rdev = netdev_priv(ndev); 416 struct rswitch_ext_ts_desc *desc; 417 dma_addr_t dma_addr; 418 u32 i, index; 419 420 for (i = 0; i < num; i++) { 421 index = (i + start_index) % gq->ring_size; 422 desc = &gq->ts_ring[index]; 423 if (!gq->dir_tx) { 424 dma_addr = dma_map_single(ndev->dev.parent, 425 gq->skbs[index]->data, PKT_BUF_SZ, 426 DMA_FROM_DEVICE); 427 if (dma_mapping_error(ndev->dev.parent, dma_addr)) 428 goto err; 429 430 desc->desc.info_ds = cpu_to_le16(PKT_BUF_SZ); 431 rswitch_desc_set_dptr(&desc->desc, dma_addr); 432 dma_wmb(); 433 desc->desc.die_dt = DT_FEMPTY | DIE; 434 desc->info1 = cpu_to_le64(INFO1_SPN(rdev->etha->index)); 435 } else { 436 desc->desc.die_dt = DT_EEMPTY | DIE; 437 } 438 } 439 440 return 0; 441 442 err: 443 if (!gq->dir_tx) { 444 for (i--; i >= 0; i--) { 445 index = (i + start_index) % gq->ring_size; 446 desc = &gq->ts_ring[index]; 447 dma_addr = rswitch_desc_get_dptr(&desc->desc); 448 dma_unmap_single(ndev->dev.parent, dma_addr, PKT_BUF_SZ, 449 DMA_FROM_DEVICE); 450 } 451 } 452 453 return -ENOMEM; 454 } 455 456 static int rswitch_gwca_queue_ts_format(struct net_device *ndev, 457 struct rswitch_private *priv, 458 struct rswitch_gwca_queue *gq) 459 { 460 int tx_ts_ring_size = sizeof(struct rswitch_ext_ts_desc) * gq->ring_size; 461 struct rswitch_ext_ts_desc *desc; 462 struct rswitch_desc *linkfix; 463 int err; 464 465 memset(gq->ts_ring, 0, tx_ts_ring_size); 466 err = rswitch_gwca_queue_ts_fill(ndev, gq, 0, gq->ring_size); 467 if (err < 0) 468 return err; 469 470 desc = &gq->ts_ring[gq->ring_size]; /* Last */ 471 rswitch_desc_set_dptr(&desc->desc, gq->ring_dma); 472 desc->desc.die_dt = DT_LINKFIX; 473 474 linkfix = &priv->linkfix_table[gq->index]; 475 linkfix->die_dt = DT_LINKFIX; 476 rswitch_desc_set_dptr(linkfix, gq->ring_dma); 477 478 iowrite32(GWDCC_BALR | (gq->dir_tx ? GWDCC_DQT : 0) | GWDCC_ETS | GWDCC_EDE, 479 priv->addr + GWDCC_OFFS(gq->index)); 480 481 return 0; 482 } 483 484 static int rswitch_gwca_desc_alloc(struct rswitch_private *priv) 485 { 486 int i, num_queues = priv->gwca.num_queues; 487 struct device *dev = &priv->pdev->dev; 488 489 priv->linkfix_table_size = sizeof(struct rswitch_desc) * num_queues; 490 priv->linkfix_table = dma_alloc_coherent(dev, priv->linkfix_table_size, 491 &priv->linkfix_table_dma, GFP_KERNEL); 492 if (!priv->linkfix_table) 493 return -ENOMEM; 494 for (i = 0; i < num_queues; i++) 495 priv->linkfix_table[i].die_dt = DT_EOS; 496 497 return 0; 498 } 499 500 static void rswitch_gwca_desc_free(struct rswitch_private *priv) 501 { 502 if (priv->linkfix_table) 503 dma_free_coherent(&priv->pdev->dev, priv->linkfix_table_size, 504 priv->linkfix_table, priv->linkfix_table_dma); 505 priv->linkfix_table = NULL; 506 } 507 508 static struct rswitch_gwca_queue *rswitch_gwca_get(struct rswitch_private *priv) 509 { 510 struct rswitch_gwca_queue *gq; 511 int index; 512 513 index = find_first_zero_bit(priv->gwca.used, priv->gwca.num_queues); 514 if (index >= priv->gwca.num_queues) 515 return NULL; 516 set_bit(index, priv->gwca.used); 517 gq = &priv->gwca.queues[index]; 518 memset(gq, 0, sizeof(*gq)); 519 gq->index = index; 520 521 return gq; 522 } 523 524 static void rswitch_gwca_put(struct rswitch_private *priv, 525 struct rswitch_gwca_queue *gq) 526 { 527 clear_bit(gq->index, priv->gwca.used); 528 } 529 530 static int rswitch_txdmac_alloc(struct net_device *ndev) 531 { 532 struct rswitch_device *rdev = netdev_priv(ndev); 533 struct rswitch_private *priv = rdev->priv; 534 int err; 535 536 rdev->tx_queue = rswitch_gwca_get(priv); 537 if (!rdev->tx_queue) 538 return -EBUSY; 539 540 err = rswitch_gwca_queue_alloc(ndev, priv, rdev->tx_queue, true, false, 541 TX_RING_SIZE); 542 if (err < 0) { 543 rswitch_gwca_put(priv, rdev->tx_queue); 544 return err; 545 } 546 547 return 0; 548 } 549 550 static void rswitch_txdmac_free(struct net_device *ndev) 551 { 552 struct rswitch_device *rdev = netdev_priv(ndev); 553 554 rswitch_gwca_queue_free(ndev, rdev->tx_queue); 555 rswitch_gwca_put(rdev->priv, rdev->tx_queue); 556 } 557 558 static int rswitch_txdmac_init(struct rswitch_private *priv, int index) 559 { 560 struct rswitch_device *rdev = priv->rdev[index]; 561 562 return rswitch_gwca_queue_format(rdev->ndev, priv, rdev->tx_queue); 563 } 564 565 static int rswitch_rxdmac_alloc(struct net_device *ndev) 566 { 567 struct rswitch_device *rdev = netdev_priv(ndev); 568 struct rswitch_private *priv = rdev->priv; 569 int err; 570 571 rdev->rx_queue = rswitch_gwca_get(priv); 572 if (!rdev->rx_queue) 573 return -EBUSY; 574 575 err = rswitch_gwca_queue_alloc(ndev, priv, rdev->rx_queue, false, true, 576 RX_RING_SIZE); 577 if (err < 0) { 578 rswitch_gwca_put(priv, rdev->rx_queue); 579 return err; 580 } 581 582 return 0; 583 } 584 585 static void rswitch_rxdmac_free(struct net_device *ndev) 586 { 587 struct rswitch_device *rdev = netdev_priv(ndev); 588 589 rswitch_gwca_queue_free(ndev, rdev->rx_queue); 590 rswitch_gwca_put(rdev->priv, rdev->rx_queue); 591 } 592 593 static int rswitch_rxdmac_init(struct rswitch_private *priv, int index) 594 { 595 struct rswitch_device *rdev = priv->rdev[index]; 596 struct net_device *ndev = rdev->ndev; 597 598 return rswitch_gwca_queue_ts_format(ndev, priv, rdev->rx_queue); 599 } 600 601 static int rswitch_gwca_hw_init(struct rswitch_private *priv) 602 { 603 int i, err; 604 605 err = rswitch_gwca_change_mode(priv, GWMC_OPC_DISABLE); 606 if (err < 0) 607 return err; 608 err = rswitch_gwca_change_mode(priv, GWMC_OPC_CONFIG); 609 if (err < 0) 610 return err; 611 612 err = rswitch_gwca_mcast_table_reset(priv); 613 if (err < 0) 614 return err; 615 err = rswitch_gwca_axi_ram_reset(priv); 616 if (err < 0) 617 return err; 618 619 iowrite32(GWVCC_VEM_SC_TAG, priv->addr + GWVCC); 620 iowrite32(0, priv->addr + GWTTFC); 621 iowrite32(lower_32_bits(priv->linkfix_table_dma), priv->addr + GWDCBAC1); 622 iowrite32(upper_32_bits(priv->linkfix_table_dma), priv->addr + GWDCBAC0); 623 rswitch_gwca_set_rate_limit(priv, priv->gwca.speed); 624 625 for (i = 0; i < RSWITCH_NUM_PORTS; i++) { 626 err = rswitch_rxdmac_init(priv, i); 627 if (err < 0) 628 return err; 629 err = rswitch_txdmac_init(priv, i); 630 if (err < 0) 631 return err; 632 } 633 634 err = rswitch_gwca_change_mode(priv, GWMC_OPC_DISABLE); 635 if (err < 0) 636 return err; 637 return rswitch_gwca_change_mode(priv, GWMC_OPC_OPERATION); 638 } 639 640 static int rswitch_gwca_hw_deinit(struct rswitch_private *priv) 641 { 642 int err; 643 644 err = rswitch_gwca_change_mode(priv, GWMC_OPC_DISABLE); 645 if (err < 0) 646 return err; 647 err = rswitch_gwca_change_mode(priv, GWMC_OPC_RESET); 648 if (err < 0) 649 return err; 650 651 return rswitch_gwca_change_mode(priv, GWMC_OPC_DISABLE); 652 } 653 654 static int rswitch_gwca_halt(struct rswitch_private *priv) 655 { 656 int err; 657 658 priv->gwca_halt = true; 659 err = rswitch_gwca_hw_deinit(priv); 660 dev_err(&priv->pdev->dev, "halted (%d)\n", err); 661 662 return err; 663 } 664 665 static bool rswitch_rx(struct net_device *ndev, int *quota) 666 { 667 struct rswitch_device *rdev = netdev_priv(ndev); 668 struct rswitch_gwca_queue *gq = rdev->rx_queue; 669 struct rswitch_ext_ts_desc *desc; 670 int limit, boguscnt, num, ret; 671 struct sk_buff *skb; 672 dma_addr_t dma_addr; 673 u16 pkt_len; 674 u32 get_ts; 675 676 boguscnt = min_t(int, gq->ring_size, *quota); 677 limit = boguscnt; 678 679 desc = &gq->ts_ring[gq->cur]; 680 while ((desc->desc.die_dt & DT_MASK) != DT_FEMPTY) { 681 if (--boguscnt < 0) 682 break; 683 dma_rmb(); 684 pkt_len = le16_to_cpu(desc->desc.info_ds) & RX_DS; 685 skb = gq->skbs[gq->cur]; 686 gq->skbs[gq->cur] = NULL; 687 dma_addr = rswitch_desc_get_dptr(&desc->desc); 688 dma_unmap_single(ndev->dev.parent, dma_addr, PKT_BUF_SZ, DMA_FROM_DEVICE); 689 get_ts = rdev->priv->ptp_priv->tstamp_rx_ctrl & RCAR_GEN4_RXTSTAMP_TYPE_V2_L2_EVENT; 690 if (get_ts) { 691 struct skb_shared_hwtstamps *shhwtstamps; 692 struct timespec64 ts; 693 694 shhwtstamps = skb_hwtstamps(skb); 695 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 696 ts.tv_sec = __le32_to_cpu(desc->ts_sec); 697 ts.tv_nsec = __le32_to_cpu(desc->ts_nsec & cpu_to_le32(0x3fffffff)); 698 shhwtstamps->hwtstamp = timespec64_to_ktime(ts); 699 } 700 skb_put(skb, pkt_len); 701 skb->protocol = eth_type_trans(skb, ndev); 702 netif_receive_skb(skb); 703 rdev->ndev->stats.rx_packets++; 704 rdev->ndev->stats.rx_bytes += pkt_len; 705 706 gq->cur = rswitch_next_queue_index(gq, true, 1); 707 desc = &gq->ts_ring[gq->cur]; 708 } 709 710 num = rswitch_get_num_cur_queues(gq); 711 ret = rswitch_gwca_queue_alloc_skb(gq, gq->dirty, num); 712 if (ret < 0) 713 goto err; 714 ret = rswitch_gwca_queue_ts_fill(ndev, gq, gq->dirty, num); 715 if (ret < 0) 716 goto err; 717 gq->dirty = rswitch_next_queue_index(gq, false, num); 718 719 *quota -= limit - (++boguscnt); 720 721 return boguscnt <= 0; 722 723 err: 724 rswitch_gwca_halt(rdev->priv); 725 726 return 0; 727 } 728 729 static int rswitch_tx_free(struct net_device *ndev, bool free_txed_only) 730 { 731 struct rswitch_device *rdev = netdev_priv(ndev); 732 struct rswitch_gwca_queue *gq = rdev->tx_queue; 733 struct rswitch_ext_desc *desc; 734 dma_addr_t dma_addr; 735 struct sk_buff *skb; 736 int free_num = 0; 737 int size; 738 739 for (; gq->cur - gq->dirty > 0; gq->dirty = rswitch_next_queue_index(gq, false, 1)) { 740 desc = &gq->ring[gq->dirty]; 741 if (free_txed_only && (desc->desc.die_dt & DT_MASK) != DT_FEMPTY) 742 break; 743 744 dma_rmb(); 745 size = le16_to_cpu(desc->desc.info_ds) & TX_DS; 746 skb = gq->skbs[gq->dirty]; 747 if (skb) { 748 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) { 749 struct skb_shared_hwtstamps shhwtstamps; 750 struct timespec64 ts; 751 752 rswitch_get_timestamp(rdev->priv, &ts); 753 memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 754 shhwtstamps.hwtstamp = timespec64_to_ktime(ts); 755 skb_tstamp_tx(skb, &shhwtstamps); 756 } 757 dma_addr = rswitch_desc_get_dptr(&desc->desc); 758 dma_unmap_single(ndev->dev.parent, dma_addr, 759 size, DMA_TO_DEVICE); 760 dev_kfree_skb_any(gq->skbs[gq->dirty]); 761 gq->skbs[gq->dirty] = NULL; 762 free_num++; 763 } 764 desc->desc.die_dt = DT_EEMPTY; 765 rdev->ndev->stats.tx_packets++; 766 rdev->ndev->stats.tx_bytes += size; 767 } 768 769 return free_num; 770 } 771 772 static int rswitch_poll(struct napi_struct *napi, int budget) 773 { 774 struct net_device *ndev = napi->dev; 775 struct rswitch_private *priv; 776 struct rswitch_device *rdev; 777 int quota = budget; 778 779 rdev = netdev_priv(ndev); 780 priv = rdev->priv; 781 782 retry: 783 rswitch_tx_free(ndev, true); 784 785 if (rswitch_rx(ndev, "a)) 786 goto out; 787 else if (rdev->priv->gwca_halt) 788 goto err; 789 else if (rswitch_is_queue_rxed(rdev->rx_queue)) 790 goto retry; 791 792 netif_wake_subqueue(ndev, 0); 793 794 napi_complete(napi); 795 796 rswitch_enadis_data_irq(priv, rdev->tx_queue->index, true); 797 rswitch_enadis_data_irq(priv, rdev->rx_queue->index, true); 798 799 out: 800 return budget - quota; 801 802 err: 803 napi_complete(napi); 804 805 return 0; 806 } 807 808 static void rswitch_queue_interrupt(struct net_device *ndev) 809 { 810 struct rswitch_device *rdev = netdev_priv(ndev); 811 812 if (napi_schedule_prep(&rdev->napi)) { 813 rswitch_enadis_data_irq(rdev->priv, rdev->tx_queue->index, false); 814 rswitch_enadis_data_irq(rdev->priv, rdev->rx_queue->index, false); 815 __napi_schedule(&rdev->napi); 816 } 817 } 818 819 static irqreturn_t rswitch_data_irq(struct rswitch_private *priv, u32 *dis) 820 { 821 struct rswitch_gwca_queue *gq; 822 int i, index, bit; 823 824 for (i = 0; i < priv->gwca.num_queues; i++) { 825 gq = &priv->gwca.queues[i]; 826 index = gq->index / 32; 827 bit = BIT(gq->index % 32); 828 if (!(dis[index] & bit)) 829 continue; 830 831 rswitch_ack_data_irq(priv, gq->index); 832 rswitch_queue_interrupt(gq->ndev); 833 } 834 835 return IRQ_HANDLED; 836 } 837 838 static irqreturn_t rswitch_gwca_irq(int irq, void *dev_id) 839 { 840 struct rswitch_private *priv = dev_id; 841 u32 dis[RSWITCH_NUM_IRQ_REGS]; 842 irqreturn_t ret = IRQ_NONE; 843 844 rswitch_get_data_irq_status(priv, dis); 845 846 if (rswitch_is_any_data_irq(priv, dis, true) || 847 rswitch_is_any_data_irq(priv, dis, false)) 848 ret = rswitch_data_irq(priv, dis); 849 850 return ret; 851 } 852 853 static int rswitch_gwca_request_irqs(struct rswitch_private *priv) 854 { 855 char *resource_name, *irq_name; 856 int i, ret, irq; 857 858 for (i = 0; i < GWCA_NUM_IRQS; i++) { 859 resource_name = kasprintf(GFP_KERNEL, GWCA_IRQ_RESOURCE_NAME, i); 860 if (!resource_name) 861 return -ENOMEM; 862 863 irq = platform_get_irq_byname(priv->pdev, resource_name); 864 kfree(resource_name); 865 if (irq < 0) 866 return irq; 867 868 irq_name = devm_kasprintf(&priv->pdev->dev, GFP_KERNEL, 869 GWCA_IRQ_NAME, i); 870 if (!irq_name) 871 return -ENOMEM; 872 873 ret = devm_request_irq(&priv->pdev->dev, irq, rswitch_gwca_irq, 874 0, irq_name, priv); 875 if (ret < 0) 876 return ret; 877 } 878 879 return 0; 880 } 881 882 /* Ethernet TSN Agent block (ETHA) and Ethernet MAC IP block (RMAC) */ 883 static int rswitch_etha_change_mode(struct rswitch_etha *etha, 884 enum rswitch_etha_mode mode) 885 { 886 int ret; 887 888 if (!rswitch_agent_clock_is_enabled(etha->coma_addr, etha->index)) 889 rswitch_agent_clock_ctrl(etha->coma_addr, etha->index, 1); 890 891 iowrite32(mode, etha->addr + EAMC); 892 893 ret = rswitch_reg_wait(etha->addr, EAMS, EAMS_OPS_MASK, mode); 894 895 if (mode == EAMC_OPC_DISABLE) 896 rswitch_agent_clock_ctrl(etha->coma_addr, etha->index, 0); 897 898 return ret; 899 } 900 901 static void rswitch_etha_read_mac_address(struct rswitch_etha *etha) 902 { 903 u32 mrmac0 = ioread32(etha->addr + MRMAC0); 904 u32 mrmac1 = ioread32(etha->addr + MRMAC1); 905 u8 *mac = ða->mac_addr[0]; 906 907 mac[0] = (mrmac0 >> 8) & 0xFF; 908 mac[1] = (mrmac0 >> 0) & 0xFF; 909 mac[2] = (mrmac1 >> 24) & 0xFF; 910 mac[3] = (mrmac1 >> 16) & 0xFF; 911 mac[4] = (mrmac1 >> 8) & 0xFF; 912 mac[5] = (mrmac1 >> 0) & 0xFF; 913 } 914 915 static void rswitch_etha_write_mac_address(struct rswitch_etha *etha, const u8 *mac) 916 { 917 iowrite32((mac[0] << 8) | mac[1], etha->addr + MRMAC0); 918 iowrite32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5], 919 etha->addr + MRMAC1); 920 } 921 922 static bool rswitch_etha_wait_link_verification(struct rswitch_etha *etha) 923 { 924 iowrite32(MLVC_PLV, etha->addr + MLVC); 925 926 return rswitch_reg_wait(etha->addr, MLVC, MLVC_PLV, 0); 927 } 928 929 static void rswitch_rmac_setting(struct rswitch_etha *etha, const u8 *mac) 930 { 931 u32 val; 932 933 rswitch_etha_write_mac_address(etha, mac); 934 935 switch (etha->speed) { 936 case 100: 937 val = MPIC_LSC_100M; 938 break; 939 case 1000: 940 val = MPIC_LSC_1G; 941 break; 942 case 2500: 943 val = MPIC_LSC_2_5G; 944 break; 945 default: 946 return; 947 } 948 949 iowrite32(MPIC_PIS_GMII | val, etha->addr + MPIC); 950 } 951 952 static void rswitch_etha_enable_mii(struct rswitch_etha *etha) 953 { 954 rswitch_modify(etha->addr, MPIC, MPIC_PSMCS_MASK | MPIC_PSMHT_MASK, 955 MPIC_PSMCS(0x05) | MPIC_PSMHT(0x06)); 956 rswitch_modify(etha->addr, MPSM, 0, MPSM_MFF_C45); 957 } 958 959 static int rswitch_etha_hw_init(struct rswitch_etha *etha, const u8 *mac) 960 { 961 int err; 962 963 err = rswitch_etha_change_mode(etha, EAMC_OPC_DISABLE); 964 if (err < 0) 965 return err; 966 err = rswitch_etha_change_mode(etha, EAMC_OPC_CONFIG); 967 if (err < 0) 968 return err; 969 970 iowrite32(EAVCC_VEM_SC_TAG, etha->addr + EAVCC); 971 rswitch_rmac_setting(etha, mac); 972 rswitch_etha_enable_mii(etha); 973 974 err = rswitch_etha_wait_link_verification(etha); 975 if (err < 0) 976 return err; 977 978 err = rswitch_etha_change_mode(etha, EAMC_OPC_DISABLE); 979 if (err < 0) 980 return err; 981 982 return rswitch_etha_change_mode(etha, EAMC_OPC_OPERATION); 983 } 984 985 static int rswitch_etha_set_access(struct rswitch_etha *etha, bool read, 986 int phyad, int devad, int regad, int data) 987 { 988 int pop = read ? MDIO_READ_C45 : MDIO_WRITE_C45; 989 u32 val; 990 int ret; 991 992 if (devad == 0xffffffff) 993 return -ENODEV; 994 995 writel(MMIS1_CLEAR_FLAGS, etha->addr + MMIS1); 996 997 val = MPSM_PSME | MPSM_MFF_C45; 998 iowrite32((regad << 16) | (devad << 8) | (phyad << 3) | val, etha->addr + MPSM); 999 1000 ret = rswitch_reg_wait(etha->addr, MMIS1, MMIS1_PAACS, MMIS1_PAACS); 1001 if (ret) 1002 return ret; 1003 1004 rswitch_modify(etha->addr, MMIS1, MMIS1_PAACS, MMIS1_PAACS); 1005 1006 if (read) { 1007 writel((pop << 13) | (devad << 8) | (phyad << 3) | val, etha->addr + MPSM); 1008 1009 ret = rswitch_reg_wait(etha->addr, MMIS1, MMIS1_PRACS, MMIS1_PRACS); 1010 if (ret) 1011 return ret; 1012 1013 ret = (ioread32(etha->addr + MPSM) & MPSM_PRD_MASK) >> 16; 1014 1015 rswitch_modify(etha->addr, MMIS1, MMIS1_PRACS, MMIS1_PRACS); 1016 } else { 1017 iowrite32((data << 16) | (pop << 13) | (devad << 8) | (phyad << 3) | val, 1018 etha->addr + MPSM); 1019 1020 ret = rswitch_reg_wait(etha->addr, MMIS1, MMIS1_PWACS, MMIS1_PWACS); 1021 } 1022 1023 return ret; 1024 } 1025 1026 static int rswitch_etha_mii_read(struct mii_bus *bus, int addr, int regnum) 1027 { 1028 struct rswitch_etha *etha = bus->priv; 1029 int mode, devad, regad; 1030 1031 mode = regnum & MII_ADDR_C45; 1032 devad = (regnum >> MII_DEVADDR_C45_SHIFT) & 0x1f; 1033 regad = regnum & MII_REGADDR_C45_MASK; 1034 1035 /* Not support Clause 22 access method */ 1036 if (!mode) 1037 return -EOPNOTSUPP; 1038 1039 return rswitch_etha_set_access(etha, true, addr, devad, regad, 0); 1040 } 1041 1042 static int rswitch_etha_mii_write(struct mii_bus *bus, int addr, int regnum, u16 val) 1043 { 1044 struct rswitch_etha *etha = bus->priv; 1045 int mode, devad, regad; 1046 1047 mode = regnum & MII_ADDR_C45; 1048 devad = (regnum >> MII_DEVADDR_C45_SHIFT) & 0x1f; 1049 regad = regnum & MII_REGADDR_C45_MASK; 1050 1051 /* Not support Clause 22 access method */ 1052 if (!mode) 1053 return -EOPNOTSUPP; 1054 1055 return rswitch_etha_set_access(etha, false, addr, devad, regad, val); 1056 } 1057 1058 /* Call of_node_put(port) after done */ 1059 static struct device_node *rswitch_get_port_node(struct rswitch_device *rdev) 1060 { 1061 struct device_node *ports, *port; 1062 int err = 0; 1063 u32 index; 1064 1065 ports = of_get_child_by_name(rdev->ndev->dev.parent->of_node, 1066 "ethernet-ports"); 1067 if (!ports) 1068 return NULL; 1069 1070 for_each_child_of_node(ports, port) { 1071 err = of_property_read_u32(port, "reg", &index); 1072 if (err < 0) { 1073 port = NULL; 1074 goto out; 1075 } 1076 if (index == rdev->etha->index) 1077 break; 1078 } 1079 1080 out: 1081 of_node_put(ports); 1082 1083 return port; 1084 } 1085 1086 /* Call of_node_put(mdio) after done */ 1087 static struct device_node *rswitch_get_mdio_node(struct rswitch_device *rdev) 1088 { 1089 struct device_node *port, *mdio; 1090 1091 port = rswitch_get_port_node(rdev); 1092 if (!port) 1093 return NULL; 1094 1095 mdio = of_get_child_by_name(port, "mdio"); 1096 of_node_put(port); 1097 1098 return mdio; 1099 } 1100 1101 static int rswitch_etha_get_params(struct rswitch_device *rdev) 1102 { 1103 struct device_node *port; 1104 int err; 1105 1106 port = rswitch_get_port_node(rdev); 1107 if (!port) 1108 return -ENODEV; 1109 1110 err = of_get_phy_mode(port, &rdev->etha->phy_interface); 1111 of_node_put(port); 1112 1113 switch (rdev->etha->phy_interface) { 1114 case PHY_INTERFACE_MODE_MII: 1115 rdev->etha->speed = SPEED_100; 1116 break; 1117 case PHY_INTERFACE_MODE_SGMII: 1118 rdev->etha->speed = SPEED_1000; 1119 break; 1120 case PHY_INTERFACE_MODE_USXGMII: 1121 rdev->etha->speed = SPEED_2500; 1122 break; 1123 default: 1124 err = -EINVAL; 1125 break; 1126 } 1127 1128 return err; 1129 } 1130 1131 static int rswitch_mii_register(struct rswitch_device *rdev) 1132 { 1133 struct device_node *mdio_np; 1134 struct mii_bus *mii_bus; 1135 int err; 1136 1137 mii_bus = mdiobus_alloc(); 1138 if (!mii_bus) 1139 return -ENOMEM; 1140 1141 mii_bus->name = "rswitch_mii"; 1142 sprintf(mii_bus->id, "etha%d", rdev->etha->index); 1143 mii_bus->priv = rdev->etha; 1144 mii_bus->read = rswitch_etha_mii_read; 1145 mii_bus->write = rswitch_etha_mii_write; 1146 mii_bus->parent = &rdev->priv->pdev->dev; 1147 1148 mdio_np = rswitch_get_mdio_node(rdev); 1149 err = of_mdiobus_register(mii_bus, mdio_np); 1150 if (err < 0) { 1151 mdiobus_free(mii_bus); 1152 goto out; 1153 } 1154 1155 rdev->etha->mii = mii_bus; 1156 1157 out: 1158 of_node_put(mdio_np); 1159 1160 return err; 1161 } 1162 1163 static void rswitch_mii_unregister(struct rswitch_device *rdev) 1164 { 1165 if (rdev->etha->mii) { 1166 mdiobus_unregister(rdev->etha->mii); 1167 mdiobus_free(rdev->etha->mii); 1168 rdev->etha->mii = NULL; 1169 } 1170 } 1171 1172 static void rswitch_mac_config(struct phylink_config *config, 1173 unsigned int mode, 1174 const struct phylink_link_state *state) 1175 { 1176 } 1177 1178 static void rswitch_mac_link_down(struct phylink_config *config, 1179 unsigned int mode, 1180 phy_interface_t interface) 1181 { 1182 } 1183 1184 static void rswitch_mac_link_up(struct phylink_config *config, 1185 struct phy_device *phydev, unsigned int mode, 1186 phy_interface_t interface, int speed, 1187 int duplex, bool tx_pause, bool rx_pause) 1188 { 1189 /* Current hardware cannot change speed at runtime */ 1190 } 1191 1192 static const struct phylink_mac_ops rswitch_phylink_ops = { 1193 .mac_config = rswitch_mac_config, 1194 .mac_link_down = rswitch_mac_link_down, 1195 .mac_link_up = rswitch_mac_link_up, 1196 }; 1197 1198 static int rswitch_phylink_init(struct rswitch_device *rdev) 1199 { 1200 struct device_node *port; 1201 struct phylink *phylink; 1202 int err; 1203 1204 port = rswitch_get_port_node(rdev); 1205 if (!port) 1206 return -ENODEV; 1207 1208 rdev->phylink_config.dev = &rdev->ndev->dev; 1209 rdev->phylink_config.type = PHYLINK_NETDEV; 1210 __set_bit(PHY_INTERFACE_MODE_SGMII, rdev->phylink_config.supported_interfaces); 1211 __set_bit(PHY_INTERFACE_MODE_USXGMII, rdev->phylink_config.supported_interfaces); 1212 rdev->phylink_config.mac_capabilities = MAC_100FD | MAC_1000FD | MAC_2500FD; 1213 1214 phylink = phylink_create(&rdev->phylink_config, &port->fwnode, 1215 rdev->etha->phy_interface, &rswitch_phylink_ops); 1216 if (IS_ERR(phylink)) { 1217 err = PTR_ERR(phylink); 1218 goto out; 1219 } 1220 1221 rdev->phylink = phylink; 1222 err = phylink_of_phy_connect(rdev->phylink, port, rdev->etha->phy_interface); 1223 out: 1224 of_node_put(port); 1225 1226 return err; 1227 } 1228 1229 static void rswitch_phylink_deinit(struct rswitch_device *rdev) 1230 { 1231 rtnl_lock(); 1232 phylink_disconnect_phy(rdev->phylink); 1233 rtnl_unlock(); 1234 phylink_destroy(rdev->phylink); 1235 } 1236 1237 static int rswitch_serdes_set_params(struct rswitch_device *rdev) 1238 { 1239 struct device_node *port = rswitch_get_port_node(rdev); 1240 struct phy *serdes; 1241 int err; 1242 1243 serdes = devm_of_phy_get(&rdev->priv->pdev->dev, port, NULL); 1244 of_node_put(port); 1245 if (IS_ERR(serdes)) 1246 return PTR_ERR(serdes); 1247 1248 err = phy_set_mode_ext(serdes, PHY_MODE_ETHERNET, 1249 rdev->etha->phy_interface); 1250 if (err < 0) 1251 return err; 1252 1253 return phy_set_speed(serdes, rdev->etha->speed); 1254 } 1255 1256 static int rswitch_serdes_init(struct rswitch_device *rdev) 1257 { 1258 struct device_node *port = rswitch_get_port_node(rdev); 1259 struct phy *serdes; 1260 1261 serdes = devm_of_phy_get(&rdev->priv->pdev->dev, port, NULL); 1262 of_node_put(port); 1263 if (IS_ERR(serdes)) 1264 return PTR_ERR(serdes); 1265 1266 return phy_init(serdes); 1267 } 1268 1269 static int rswitch_serdes_deinit(struct rswitch_device *rdev) 1270 { 1271 struct device_node *port = rswitch_get_port_node(rdev); 1272 struct phy *serdes; 1273 1274 serdes = devm_of_phy_get(&rdev->priv->pdev->dev, port, NULL); 1275 of_node_put(port); 1276 if (IS_ERR(serdes)) 1277 return PTR_ERR(serdes); 1278 1279 return phy_exit(serdes); 1280 } 1281 1282 static int rswitch_ether_port_init_one(struct rswitch_device *rdev) 1283 { 1284 int err; 1285 1286 if (!rdev->etha->operated) { 1287 err = rswitch_etha_hw_init(rdev->etha, rdev->ndev->dev_addr); 1288 if (err < 0) 1289 return err; 1290 rdev->etha->operated = true; 1291 } 1292 1293 err = rswitch_mii_register(rdev); 1294 if (err < 0) 1295 return err; 1296 1297 err = rswitch_phylink_init(rdev); 1298 if (err < 0) 1299 goto err_phylink_init; 1300 1301 err = rswitch_serdes_set_params(rdev); 1302 if (err < 0) 1303 goto err_serdes_set_params; 1304 1305 return 0; 1306 1307 err_serdes_set_params: 1308 rswitch_phylink_deinit(rdev); 1309 1310 err_phylink_init: 1311 rswitch_mii_unregister(rdev); 1312 1313 return err; 1314 } 1315 1316 static void rswitch_ether_port_deinit_one(struct rswitch_device *rdev) 1317 { 1318 rswitch_phylink_deinit(rdev); 1319 rswitch_mii_unregister(rdev); 1320 } 1321 1322 static int rswitch_ether_port_init_all(struct rswitch_private *priv) 1323 { 1324 int i, err; 1325 1326 for (i = 0; i < RSWITCH_NUM_PORTS; i++) { 1327 err = rswitch_ether_port_init_one(priv->rdev[i]); 1328 if (err) 1329 goto err_init_one; 1330 } 1331 1332 for (i = 0; i < RSWITCH_NUM_PORTS; i++) { 1333 err = rswitch_serdes_init(priv->rdev[i]); 1334 if (err) 1335 goto err_serdes; 1336 } 1337 1338 return 0; 1339 1340 err_serdes: 1341 for (i--; i >= 0; i--) 1342 rswitch_serdes_deinit(priv->rdev[i]); 1343 i = RSWITCH_NUM_PORTS; 1344 1345 err_init_one: 1346 for (i--; i >= 0; i--) 1347 rswitch_ether_port_deinit_one(priv->rdev[i]); 1348 1349 return err; 1350 } 1351 1352 static void rswitch_ether_port_deinit_all(struct rswitch_private *priv) 1353 { 1354 int i; 1355 1356 for (i = 0; i < RSWITCH_NUM_PORTS; i++) { 1357 rswitch_serdes_deinit(priv->rdev[i]); 1358 rswitch_ether_port_deinit_one(priv->rdev[i]); 1359 } 1360 } 1361 1362 static int rswitch_open(struct net_device *ndev) 1363 { 1364 struct rswitch_device *rdev = netdev_priv(ndev); 1365 1366 phylink_start(rdev->phylink); 1367 1368 napi_enable(&rdev->napi); 1369 netif_start_queue(ndev); 1370 1371 rswitch_enadis_data_irq(rdev->priv, rdev->tx_queue->index, true); 1372 rswitch_enadis_data_irq(rdev->priv, rdev->rx_queue->index, true); 1373 1374 return 0; 1375 }; 1376 1377 static int rswitch_stop(struct net_device *ndev) 1378 { 1379 struct rswitch_device *rdev = netdev_priv(ndev); 1380 1381 netif_tx_stop_all_queues(ndev); 1382 1383 rswitch_enadis_data_irq(rdev->priv, rdev->tx_queue->index, false); 1384 rswitch_enadis_data_irq(rdev->priv, rdev->rx_queue->index, false); 1385 1386 phylink_stop(rdev->phylink); 1387 napi_disable(&rdev->napi); 1388 1389 return 0; 1390 }; 1391 1392 static netdev_tx_t rswitch_start_xmit(struct sk_buff *skb, struct net_device *ndev) 1393 { 1394 struct rswitch_device *rdev = netdev_priv(ndev); 1395 struct rswitch_gwca_queue *gq = rdev->tx_queue; 1396 struct rswitch_ext_desc *desc; 1397 int ret = NETDEV_TX_OK; 1398 dma_addr_t dma_addr; 1399 1400 if (rswitch_get_num_cur_queues(gq) >= gq->ring_size - 1) { 1401 netif_stop_subqueue(ndev, 0); 1402 return ret; 1403 } 1404 1405 if (skb_put_padto(skb, ETH_ZLEN)) 1406 return ret; 1407 1408 dma_addr = dma_map_single(ndev->dev.parent, skb->data, skb->len, DMA_TO_DEVICE); 1409 if (dma_mapping_error(ndev->dev.parent, dma_addr)) { 1410 dev_kfree_skb_any(skb); 1411 return ret; 1412 } 1413 1414 gq->skbs[gq->cur] = skb; 1415 desc = &gq->ring[gq->cur]; 1416 rswitch_desc_set_dptr(&desc->desc, dma_addr); 1417 desc->desc.info_ds = cpu_to_le16(skb->len); 1418 1419 desc->info1 = cpu_to_le64(INFO1_DV(BIT(rdev->etha->index)) | INFO1_FMT); 1420 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) { 1421 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 1422 rdev->ts_tag++; 1423 desc->info1 |= cpu_to_le64(INFO1_TSUN(rdev->ts_tag) | INFO1_TXC); 1424 } 1425 skb_tx_timestamp(skb); 1426 1427 dma_wmb(); 1428 1429 desc->desc.die_dt = DT_FSINGLE | DIE; 1430 wmb(); /* gq->cur must be incremented after die_dt was set */ 1431 1432 gq->cur = rswitch_next_queue_index(gq, true, 1); 1433 rswitch_modify(rdev->addr, GWTRC(gq->index), 0, BIT(gq->index % 32)); 1434 1435 return ret; 1436 } 1437 1438 static struct net_device_stats *rswitch_get_stats(struct net_device *ndev) 1439 { 1440 return &ndev->stats; 1441 } 1442 1443 static int rswitch_hwstamp_get(struct net_device *ndev, struct ifreq *req) 1444 { 1445 struct rswitch_device *rdev = netdev_priv(ndev); 1446 struct rcar_gen4_ptp_private *ptp_priv; 1447 struct hwtstamp_config config; 1448 1449 ptp_priv = rdev->priv->ptp_priv; 1450 1451 config.flags = 0; 1452 config.tx_type = ptp_priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON : 1453 HWTSTAMP_TX_OFF; 1454 switch (ptp_priv->tstamp_rx_ctrl & RCAR_GEN4_RXTSTAMP_TYPE) { 1455 case RCAR_GEN4_RXTSTAMP_TYPE_V2_L2_EVENT: 1456 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT; 1457 break; 1458 case RCAR_GEN4_RXTSTAMP_TYPE_ALL: 1459 config.rx_filter = HWTSTAMP_FILTER_ALL; 1460 break; 1461 default: 1462 config.rx_filter = HWTSTAMP_FILTER_NONE; 1463 break; 1464 } 1465 1466 return copy_to_user(req->ifr_data, &config, sizeof(config)) ? -EFAULT : 0; 1467 } 1468 1469 static int rswitch_hwstamp_set(struct net_device *ndev, struct ifreq *req) 1470 { 1471 struct rswitch_device *rdev = netdev_priv(ndev); 1472 u32 tstamp_rx_ctrl = RCAR_GEN4_RXTSTAMP_ENABLED; 1473 struct hwtstamp_config config; 1474 u32 tstamp_tx_ctrl; 1475 1476 if (copy_from_user(&config, req->ifr_data, sizeof(config))) 1477 return -EFAULT; 1478 1479 if (config.flags) 1480 return -EINVAL; 1481 1482 switch (config.tx_type) { 1483 case HWTSTAMP_TX_OFF: 1484 tstamp_tx_ctrl = 0; 1485 break; 1486 case HWTSTAMP_TX_ON: 1487 tstamp_tx_ctrl = RCAR_GEN4_TXTSTAMP_ENABLED; 1488 break; 1489 default: 1490 return -ERANGE; 1491 } 1492 1493 switch (config.rx_filter) { 1494 case HWTSTAMP_FILTER_NONE: 1495 tstamp_rx_ctrl = 0; 1496 break; 1497 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 1498 tstamp_rx_ctrl |= RCAR_GEN4_RXTSTAMP_TYPE_V2_L2_EVENT; 1499 break; 1500 default: 1501 config.rx_filter = HWTSTAMP_FILTER_ALL; 1502 tstamp_rx_ctrl |= RCAR_GEN4_RXTSTAMP_TYPE_ALL; 1503 break; 1504 } 1505 1506 rdev->priv->ptp_priv->tstamp_tx_ctrl = tstamp_tx_ctrl; 1507 rdev->priv->ptp_priv->tstamp_rx_ctrl = tstamp_rx_ctrl; 1508 1509 return copy_to_user(req->ifr_data, &config, sizeof(config)) ? -EFAULT : 0; 1510 } 1511 1512 static int rswitch_eth_ioctl(struct net_device *ndev, struct ifreq *req, int cmd) 1513 { 1514 struct rswitch_device *rdev = netdev_priv(ndev); 1515 1516 if (!netif_running(ndev)) 1517 return -EINVAL; 1518 1519 switch (cmd) { 1520 case SIOCGHWTSTAMP: 1521 return rswitch_hwstamp_get(ndev, req); 1522 case SIOCSHWTSTAMP: 1523 return rswitch_hwstamp_set(ndev, req); 1524 default: 1525 return phylink_mii_ioctl(rdev->phylink, req, cmd); 1526 } 1527 } 1528 1529 static const struct net_device_ops rswitch_netdev_ops = { 1530 .ndo_open = rswitch_open, 1531 .ndo_stop = rswitch_stop, 1532 .ndo_start_xmit = rswitch_start_xmit, 1533 .ndo_get_stats = rswitch_get_stats, 1534 .ndo_eth_ioctl = rswitch_eth_ioctl, 1535 .ndo_validate_addr = eth_validate_addr, 1536 .ndo_set_mac_address = eth_mac_addr, 1537 }; 1538 1539 static int rswitch_get_ts_info(struct net_device *ndev, struct ethtool_ts_info *info) 1540 { 1541 struct rswitch_device *rdev = netdev_priv(ndev); 1542 1543 info->phc_index = ptp_clock_index(rdev->priv->ptp_priv->clock); 1544 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE | 1545 SOF_TIMESTAMPING_RX_SOFTWARE | 1546 SOF_TIMESTAMPING_SOFTWARE | 1547 SOF_TIMESTAMPING_TX_HARDWARE | 1548 SOF_TIMESTAMPING_RX_HARDWARE | 1549 SOF_TIMESTAMPING_RAW_HARDWARE; 1550 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON); 1551 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL); 1552 1553 return 0; 1554 } 1555 1556 static const struct ethtool_ops rswitch_ethtool_ops = { 1557 .get_ts_info = rswitch_get_ts_info, 1558 }; 1559 1560 static const struct of_device_id renesas_eth_sw_of_table[] = { 1561 { .compatible = "renesas,r8a779f0-ether-switch", }, 1562 { } 1563 }; 1564 MODULE_DEVICE_TABLE(of, renesas_eth_sw_of_table); 1565 1566 static void rswitch_etha_init(struct rswitch_private *priv, int index) 1567 { 1568 struct rswitch_etha *etha = &priv->etha[index]; 1569 1570 memset(etha, 0, sizeof(*etha)); 1571 etha->index = index; 1572 etha->addr = priv->addr + RSWITCH_ETHA_OFFSET + index * RSWITCH_ETHA_SIZE; 1573 etha->coma_addr = priv->addr; 1574 } 1575 1576 static int rswitch_device_alloc(struct rswitch_private *priv, int index) 1577 { 1578 struct platform_device *pdev = priv->pdev; 1579 struct rswitch_device *rdev; 1580 struct net_device *ndev; 1581 int err; 1582 1583 if (index >= RSWITCH_NUM_PORTS) 1584 return -EINVAL; 1585 1586 ndev = alloc_etherdev_mqs(sizeof(struct rswitch_device), 1, 1); 1587 if (!ndev) 1588 return -ENOMEM; 1589 1590 SET_NETDEV_DEV(ndev, &pdev->dev); 1591 ether_setup(ndev); 1592 1593 rdev = netdev_priv(ndev); 1594 rdev->ndev = ndev; 1595 rdev->priv = priv; 1596 priv->rdev[index] = rdev; 1597 rdev->port = index; 1598 rdev->etha = &priv->etha[index]; 1599 rdev->addr = priv->addr; 1600 1601 ndev->base_addr = (unsigned long)rdev->addr; 1602 snprintf(ndev->name, IFNAMSIZ, "tsn%d", index); 1603 ndev->netdev_ops = &rswitch_netdev_ops; 1604 ndev->ethtool_ops = &rswitch_ethtool_ops; 1605 1606 netif_napi_add(ndev, &rdev->napi, rswitch_poll); 1607 1608 err = of_get_ethdev_address(pdev->dev.of_node, ndev); 1609 if (err) { 1610 if (is_valid_ether_addr(rdev->etha->mac_addr)) 1611 eth_hw_addr_set(ndev, rdev->etha->mac_addr); 1612 else 1613 eth_hw_addr_random(ndev); 1614 } 1615 1616 err = rswitch_etha_get_params(rdev); 1617 if (err < 0) 1618 goto out_get_params; 1619 1620 if (rdev->priv->gwca.speed < rdev->etha->speed) 1621 rdev->priv->gwca.speed = rdev->etha->speed; 1622 1623 err = rswitch_rxdmac_alloc(ndev); 1624 if (err < 0) 1625 goto out_rxdmac; 1626 1627 err = rswitch_txdmac_alloc(ndev); 1628 if (err < 0) 1629 goto out_txdmac; 1630 1631 return 0; 1632 1633 out_txdmac: 1634 rswitch_rxdmac_free(ndev); 1635 1636 out_rxdmac: 1637 out_get_params: 1638 netif_napi_del(&rdev->napi); 1639 free_netdev(ndev); 1640 1641 return err; 1642 } 1643 1644 static void rswitch_device_free(struct rswitch_private *priv, int index) 1645 { 1646 struct rswitch_device *rdev = priv->rdev[index]; 1647 struct net_device *ndev = rdev->ndev; 1648 1649 rswitch_txdmac_free(ndev); 1650 rswitch_rxdmac_free(ndev); 1651 netif_napi_del(&rdev->napi); 1652 free_netdev(ndev); 1653 } 1654 1655 static int rswitch_init(struct rswitch_private *priv) 1656 { 1657 int i, err; 1658 1659 for (i = 0; i < RSWITCH_NUM_PORTS; i++) 1660 rswitch_etha_init(priv, i); 1661 1662 rswitch_clock_enable(priv); 1663 for (i = 0; i < RSWITCH_NUM_PORTS; i++) 1664 rswitch_etha_read_mac_address(&priv->etha[i]); 1665 1666 rswitch_reset(priv); 1667 1668 rswitch_clock_enable(priv); 1669 rswitch_top_init(priv); 1670 err = rswitch_bpool_config(priv); 1671 if (err < 0) 1672 return err; 1673 1674 err = rswitch_gwca_desc_alloc(priv); 1675 if (err < 0) 1676 return -ENOMEM; 1677 1678 for (i = 0; i < RSWITCH_NUM_PORTS; i++) { 1679 err = rswitch_device_alloc(priv, i); 1680 if (err < 0) { 1681 for (i--; i >= 0; i--) 1682 rswitch_device_free(priv, i); 1683 goto err_device_alloc; 1684 } 1685 } 1686 1687 rswitch_fwd_init(priv); 1688 1689 err = rcar_gen4_ptp_register(priv->ptp_priv, RCAR_GEN4_PTP_REG_LAYOUT_S4, 1690 RCAR_GEN4_PTP_CLOCK_S4); 1691 if (err < 0) 1692 goto err_ptp_register; 1693 1694 err = rswitch_gwca_request_irqs(priv); 1695 if (err < 0) 1696 goto err_gwca_request_irq; 1697 1698 err = rswitch_gwca_hw_init(priv); 1699 if (err < 0) 1700 goto err_gwca_hw_init; 1701 1702 err = rswitch_ether_port_init_all(priv); 1703 if (err) 1704 goto err_ether_port_init_all; 1705 1706 for (i = 0; i < RSWITCH_NUM_PORTS; i++) { 1707 err = register_netdev(priv->rdev[i]->ndev); 1708 if (err) { 1709 for (i--; i >= 0; i--) 1710 unregister_netdev(priv->rdev[i]->ndev); 1711 goto err_register_netdev; 1712 } 1713 } 1714 1715 for (i = 0; i < RSWITCH_NUM_PORTS; i++) 1716 netdev_info(priv->rdev[i]->ndev, "MAC address %pMn", 1717 priv->rdev[i]->ndev->dev_addr); 1718 1719 return 0; 1720 1721 err_register_netdev: 1722 rswitch_ether_port_deinit_all(priv); 1723 1724 err_ether_port_init_all: 1725 rswitch_gwca_hw_deinit(priv); 1726 1727 err_gwca_hw_init: 1728 err_gwca_request_irq: 1729 rcar_gen4_ptp_unregister(priv->ptp_priv); 1730 1731 err_ptp_register: 1732 for (i = 0; i < RSWITCH_NUM_PORTS; i++) 1733 rswitch_device_free(priv, i); 1734 1735 err_device_alloc: 1736 rswitch_gwca_desc_free(priv); 1737 1738 return err; 1739 } 1740 1741 static int renesas_eth_sw_probe(struct platform_device *pdev) 1742 { 1743 struct rswitch_private *priv; 1744 struct resource *res; 1745 int ret; 1746 1747 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "secure_base"); 1748 if (!res) { 1749 dev_err(&pdev->dev, "invalid resource\n"); 1750 return -EINVAL; 1751 } 1752 1753 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 1754 if (!priv) 1755 return -ENOMEM; 1756 1757 priv->ptp_priv = rcar_gen4_ptp_alloc(pdev); 1758 if (!priv->ptp_priv) 1759 return -ENOMEM; 1760 1761 platform_set_drvdata(pdev, priv); 1762 priv->pdev = pdev; 1763 priv->addr = devm_ioremap_resource(&pdev->dev, res); 1764 if (IS_ERR(priv->addr)) 1765 return PTR_ERR(priv->addr); 1766 1767 priv->ptp_priv->addr = priv->addr + RCAR_GEN4_GPTP_OFFSET_S4; 1768 1769 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40)); 1770 if (ret < 0) { 1771 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 1772 if (ret < 0) 1773 return ret; 1774 } 1775 1776 priv->gwca.index = AGENT_INDEX_GWCA; 1777 priv->gwca.num_queues = min(RSWITCH_NUM_PORTS * NUM_QUEUES_PER_NDEV, 1778 RSWITCH_MAX_NUM_QUEUES); 1779 priv->gwca.queues = devm_kcalloc(&pdev->dev, priv->gwca.num_queues, 1780 sizeof(*priv->gwca.queues), GFP_KERNEL); 1781 if (!priv->gwca.queues) 1782 return -ENOMEM; 1783 1784 pm_runtime_enable(&pdev->dev); 1785 pm_runtime_get_sync(&pdev->dev); 1786 1787 ret = rswitch_init(priv); 1788 1789 device_set_wakeup_capable(&pdev->dev, 1); 1790 1791 return ret; 1792 } 1793 1794 static void rswitch_deinit(struct rswitch_private *priv) 1795 { 1796 int i; 1797 1798 rswitch_gwca_hw_deinit(priv); 1799 rcar_gen4_ptp_unregister(priv->ptp_priv); 1800 1801 for (i = 0; i < RSWITCH_NUM_PORTS; i++) { 1802 struct rswitch_device *rdev = priv->rdev[i]; 1803 1804 rswitch_serdes_deinit(rdev); 1805 rswitch_ether_port_deinit_one(rdev); 1806 unregister_netdev(rdev->ndev); 1807 rswitch_device_free(priv, i); 1808 } 1809 1810 rswitch_gwca_desc_free(priv); 1811 1812 rswitch_clock_disable(priv); 1813 } 1814 1815 static int renesas_eth_sw_remove(struct platform_device *pdev) 1816 { 1817 struct rswitch_private *priv = platform_get_drvdata(pdev); 1818 1819 rswitch_deinit(priv); 1820 1821 pm_runtime_put(&pdev->dev); 1822 pm_runtime_disable(&pdev->dev); 1823 1824 platform_set_drvdata(pdev, NULL); 1825 1826 return 0; 1827 } 1828 1829 static struct platform_driver renesas_eth_sw_driver_platform = { 1830 .probe = renesas_eth_sw_probe, 1831 .remove = renesas_eth_sw_remove, 1832 .driver = { 1833 .name = "renesas_eth_sw", 1834 .of_match_table = renesas_eth_sw_of_table, 1835 } 1836 }; 1837 module_platform_driver(renesas_eth_sw_driver_platform); 1838 MODULE_AUTHOR("Yoshihiro Shimoda"); 1839 MODULE_DESCRIPTION("Renesas Ethernet Switch device driver"); 1840 MODULE_LICENSE("GPL"); 1841