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