1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Texas Instruments ICSSG Ethernet Driver 4 * 5 * Copyright (C) 2018-2022 Texas Instruments Incorporated - https://www.ti.com/ 6 * 7 */ 8 9 #include <linux/bitops.h> 10 #include <linux/clk.h> 11 #include <linux/delay.h> 12 #include <linux/dma-mapping.h> 13 #include <linux/dma/ti-cppi5.h> 14 #include <linux/etherdevice.h> 15 #include <linux/genalloc.h> 16 #include <linux/if_vlan.h> 17 #include <linux/interrupt.h> 18 #include <linux/kernel.h> 19 #include <linux/mfd/syscon.h> 20 #include <linux/module.h> 21 #include <linux/of.h> 22 #include <linux/of_mdio.h> 23 #include <linux/of_net.h> 24 #include <linux/platform_device.h> 25 #include <linux/phy.h> 26 #include <linux/property.h> 27 #include <linux/remoteproc/pruss.h> 28 #include <linux/regmap.h> 29 #include <linux/remoteproc.h> 30 #include <net/switchdev.h> 31 32 #include "icssg_prueth.h" 33 #include "icssg_mii_rt.h" 34 #include "icssg_switchdev.h" 35 #include "../k3-cppi-desc-pool.h" 36 37 #define PRUETH_MODULE_DESCRIPTION "PRUSS ICSSG Ethernet driver" 38 39 #define DEFAULT_VID 1 40 #define DEFAULT_PORT_MASK 1 41 #define DEFAULT_UNTAG_MASK 1 42 43 /* CTRLMMR_ICSSG_RGMII_CTRL register bits */ 44 #define ICSSG_CTRL_RGMII_ID_MODE BIT(24) 45 46 static int emac_get_tx_ts(struct prueth_emac *emac, 47 struct emac_tx_ts_response *rsp) 48 { 49 struct prueth *prueth = emac->prueth; 50 int slice = prueth_emac_slice(emac); 51 int addr; 52 53 addr = icssg_queue_pop(prueth, slice == 0 ? 54 ICSSG_TS_POP_SLICE0 : ICSSG_TS_POP_SLICE1); 55 if (addr < 0) 56 return addr; 57 58 memcpy_fromio(rsp, prueth->shram.va + addr, sizeof(*rsp)); 59 /* return buffer back for to pool */ 60 icssg_queue_push(prueth, slice == 0 ? 61 ICSSG_TS_PUSH_SLICE0 : ICSSG_TS_PUSH_SLICE1, addr); 62 63 return 0; 64 } 65 66 static void tx_ts_work(struct prueth_emac *emac) 67 { 68 struct skb_shared_hwtstamps ssh; 69 struct emac_tx_ts_response tsr; 70 struct sk_buff *skb; 71 int ret = 0; 72 u32 hi_sw; 73 u64 ns; 74 75 /* There may be more than one pending requests */ 76 while (1) { 77 ret = emac_get_tx_ts(emac, &tsr); 78 if (ret) /* nothing more */ 79 break; 80 81 if (tsr.cookie >= PRUETH_MAX_TX_TS_REQUESTS || 82 !emac->tx_ts_skb[tsr.cookie]) { 83 netdev_err(emac->ndev, "Invalid TX TS cookie 0x%x\n", 84 tsr.cookie); 85 break; 86 } 87 88 skb = emac->tx_ts_skb[tsr.cookie]; 89 emac->tx_ts_skb[tsr.cookie] = NULL; /* free slot */ 90 if (!skb) { 91 netdev_err(emac->ndev, "Driver Bug! got NULL skb\n"); 92 break; 93 } 94 95 hi_sw = readl(emac->prueth->shram.va + 96 TIMESYNC_FW_WC_COUNT_HI_SW_OFFSET_OFFSET); 97 ns = icssg_ts_to_ns(hi_sw, tsr.hi_ts, tsr.lo_ts, 98 IEP_DEFAULT_CYCLE_TIME_NS); 99 100 memset(&ssh, 0, sizeof(ssh)); 101 ssh.hwtstamp = ns_to_ktime(ns); 102 103 skb_tstamp_tx(skb, &ssh); 104 dev_consume_skb_any(skb); 105 106 if (atomic_dec_and_test(&emac->tx_ts_pending)) /* no more? */ 107 break; 108 } 109 } 110 111 static irqreturn_t prueth_tx_ts_irq(int irq, void *dev_id) 112 { 113 struct prueth_emac *emac = dev_id; 114 115 /* currently only TX timestamp is being returned */ 116 tx_ts_work(emac); 117 118 return IRQ_HANDLED; 119 } 120 121 static struct icssg_firmwares icssg_switch_firmwares[] = { 122 { 123 .pru = "ti-pruss/am65x-sr2-pru0-prusw-fw.elf", 124 .rtu = "ti-pruss/am65x-sr2-rtu0-prusw-fw.elf", 125 .txpru = "ti-pruss/am65x-sr2-txpru0-prusw-fw.elf", 126 }, 127 { 128 .pru = "ti-pruss/am65x-sr2-pru1-prusw-fw.elf", 129 .rtu = "ti-pruss/am65x-sr2-rtu1-prusw-fw.elf", 130 .txpru = "ti-pruss/am65x-sr2-txpru1-prusw-fw.elf", 131 } 132 }; 133 134 static struct icssg_firmwares icssg_emac_firmwares[] = { 135 { 136 .pru = "ti-pruss/am65x-sr2-pru0-prueth-fw.elf", 137 .rtu = "ti-pruss/am65x-sr2-rtu0-prueth-fw.elf", 138 .txpru = "ti-pruss/am65x-sr2-txpru0-prueth-fw.elf", 139 }, 140 { 141 .pru = "ti-pruss/am65x-sr2-pru1-prueth-fw.elf", 142 .rtu = "ti-pruss/am65x-sr2-rtu1-prueth-fw.elf", 143 .txpru = "ti-pruss/am65x-sr2-txpru1-prueth-fw.elf", 144 } 145 }; 146 147 static int prueth_emac_start(struct prueth *prueth, struct prueth_emac *emac) 148 { 149 struct icssg_firmwares *firmwares; 150 struct device *dev = prueth->dev; 151 int slice, ret; 152 153 if (prueth->is_switch_mode) 154 firmwares = icssg_switch_firmwares; 155 else 156 firmwares = icssg_emac_firmwares; 157 158 slice = prueth_emac_slice(emac); 159 if (slice < 0) { 160 netdev_err(emac->ndev, "invalid port\n"); 161 return -EINVAL; 162 } 163 164 ret = icssg_config(prueth, emac, slice); 165 if (ret) 166 return ret; 167 168 ret = rproc_set_firmware(prueth->pru[slice], firmwares[slice].pru); 169 ret = rproc_boot(prueth->pru[slice]); 170 if (ret) { 171 dev_err(dev, "failed to boot PRU%d: %d\n", slice, ret); 172 return -EINVAL; 173 } 174 175 ret = rproc_set_firmware(prueth->rtu[slice], firmwares[slice].rtu); 176 ret = rproc_boot(prueth->rtu[slice]); 177 if (ret) { 178 dev_err(dev, "failed to boot RTU%d: %d\n", slice, ret); 179 goto halt_pru; 180 } 181 182 ret = rproc_set_firmware(prueth->txpru[slice], firmwares[slice].txpru); 183 ret = rproc_boot(prueth->txpru[slice]); 184 if (ret) { 185 dev_err(dev, "failed to boot TX_PRU%d: %d\n", slice, ret); 186 goto halt_rtu; 187 } 188 189 emac->fw_running = 1; 190 return 0; 191 192 halt_rtu: 193 rproc_shutdown(prueth->rtu[slice]); 194 195 halt_pru: 196 rproc_shutdown(prueth->pru[slice]); 197 198 return ret; 199 } 200 201 /* called back by PHY layer if there is change in link state of hw port*/ 202 static void emac_adjust_link(struct net_device *ndev) 203 { 204 struct prueth_emac *emac = netdev_priv(ndev); 205 struct phy_device *phydev = ndev->phydev; 206 struct prueth *prueth = emac->prueth; 207 bool new_state = false; 208 unsigned long flags; 209 210 if (phydev->link) { 211 /* check the mode of operation - full/half duplex */ 212 if (phydev->duplex != emac->duplex) { 213 new_state = true; 214 emac->duplex = phydev->duplex; 215 } 216 if (phydev->speed != emac->speed) { 217 new_state = true; 218 emac->speed = phydev->speed; 219 } 220 if (!emac->link) { 221 new_state = true; 222 emac->link = 1; 223 } 224 } else if (emac->link) { 225 new_state = true; 226 emac->link = 0; 227 228 /* f/w should support 100 & 1000 */ 229 emac->speed = SPEED_1000; 230 231 /* half duplex may not be supported by f/w */ 232 emac->duplex = DUPLEX_FULL; 233 } 234 235 if (new_state) { 236 phy_print_status(phydev); 237 238 /* update RGMII and MII configuration based on PHY negotiated 239 * values 240 */ 241 if (emac->link) { 242 if (emac->duplex == DUPLEX_HALF) 243 icssg_config_half_duplex(emac); 244 /* Set the RGMII cfg for gig en and full duplex */ 245 icssg_update_rgmii_cfg(prueth->miig_rt, emac); 246 247 /* update the Tx IPG based on 100M/1G speed */ 248 spin_lock_irqsave(&emac->lock, flags); 249 icssg_config_ipg(emac); 250 spin_unlock_irqrestore(&emac->lock, flags); 251 icssg_config_set_speed(emac); 252 icssg_set_port_state(emac, ICSSG_EMAC_PORT_FORWARD); 253 254 } else { 255 icssg_set_port_state(emac, ICSSG_EMAC_PORT_DISABLE); 256 } 257 } 258 259 if (emac->link) { 260 /* reactivate the transmit queue */ 261 netif_tx_wake_all_queues(ndev); 262 } else { 263 netif_tx_stop_all_queues(ndev); 264 prueth_cleanup_tx_ts(emac); 265 } 266 } 267 268 static enum hrtimer_restart emac_rx_timer_callback(struct hrtimer *timer) 269 { 270 struct prueth_emac *emac = 271 container_of(timer, struct prueth_emac, rx_hrtimer); 272 int rx_flow = PRUETH_RX_FLOW_DATA; 273 274 enable_irq(emac->rx_chns.irq[rx_flow]); 275 return HRTIMER_NORESTART; 276 } 277 278 static int emac_phy_connect(struct prueth_emac *emac) 279 { 280 struct prueth *prueth = emac->prueth; 281 struct net_device *ndev = emac->ndev; 282 /* connect PHY */ 283 ndev->phydev = of_phy_connect(emac->ndev, emac->phy_node, 284 &emac_adjust_link, 0, 285 emac->phy_if); 286 if (!ndev->phydev) { 287 dev_err(prueth->dev, "couldn't connect to phy %s\n", 288 emac->phy_node->full_name); 289 return -ENODEV; 290 } 291 292 if (!emac->half_duplex) { 293 dev_dbg(prueth->dev, "half duplex mode is not supported\n"); 294 phy_remove_link_mode(ndev->phydev, ETHTOOL_LINK_MODE_10baseT_Half_BIT); 295 phy_remove_link_mode(ndev->phydev, ETHTOOL_LINK_MODE_100baseT_Half_BIT); 296 } 297 298 /* remove unsupported modes */ 299 phy_remove_link_mode(ndev->phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT); 300 phy_remove_link_mode(ndev->phydev, ETHTOOL_LINK_MODE_Pause_BIT); 301 phy_remove_link_mode(ndev->phydev, ETHTOOL_LINK_MODE_Asym_Pause_BIT); 302 303 if (emac->phy_if == PHY_INTERFACE_MODE_MII) 304 phy_set_max_speed(ndev->phydev, SPEED_100); 305 306 return 0; 307 } 308 309 static u64 prueth_iep_gettime(void *clockops_data, struct ptp_system_timestamp *sts) 310 { 311 u32 hi_rollover_count, hi_rollover_count_r; 312 struct prueth_emac *emac = clockops_data; 313 struct prueth *prueth = emac->prueth; 314 void __iomem *fw_hi_r_count_addr; 315 void __iomem *fw_count_hi_addr; 316 u32 iepcount_hi, iepcount_hi_r; 317 unsigned long flags; 318 u32 iepcount_lo; 319 u64 ts = 0; 320 321 fw_count_hi_addr = prueth->shram.va + TIMESYNC_FW_WC_COUNT_HI_SW_OFFSET_OFFSET; 322 fw_hi_r_count_addr = prueth->shram.va + TIMESYNC_FW_WC_HI_ROLLOVER_COUNT_OFFSET; 323 324 local_irq_save(flags); 325 do { 326 iepcount_hi = icss_iep_get_count_hi(emac->iep); 327 iepcount_hi += readl(fw_count_hi_addr); 328 hi_rollover_count = readl(fw_hi_r_count_addr); 329 ptp_read_system_prets(sts); 330 iepcount_lo = icss_iep_get_count_low(emac->iep); 331 ptp_read_system_postts(sts); 332 333 iepcount_hi_r = icss_iep_get_count_hi(emac->iep); 334 iepcount_hi_r += readl(fw_count_hi_addr); 335 hi_rollover_count_r = readl(fw_hi_r_count_addr); 336 } while ((iepcount_hi_r != iepcount_hi) || 337 (hi_rollover_count != hi_rollover_count_r)); 338 local_irq_restore(flags); 339 340 ts = ((u64)hi_rollover_count) << 23 | iepcount_hi; 341 ts = ts * (u64)IEP_DEFAULT_CYCLE_TIME_NS + iepcount_lo; 342 343 return ts; 344 } 345 346 static void prueth_iep_settime(void *clockops_data, u64 ns) 347 { 348 struct icssg_setclock_desc __iomem *sc_descp; 349 struct prueth_emac *emac = clockops_data; 350 struct icssg_setclock_desc sc_desc; 351 u64 cyclecount; 352 u32 cycletime; 353 int timeout; 354 355 if (!emac->fw_running) 356 return; 357 358 sc_descp = emac->prueth->shram.va + TIMESYNC_FW_WC_SETCLOCK_DESC_OFFSET; 359 360 cycletime = IEP_DEFAULT_CYCLE_TIME_NS; 361 cyclecount = ns / cycletime; 362 363 memset(&sc_desc, 0, sizeof(sc_desc)); 364 sc_desc.margin = cycletime - 1000; 365 sc_desc.cyclecounter0_set = cyclecount & GENMASK(31, 0); 366 sc_desc.cyclecounter1_set = (cyclecount & GENMASK(63, 32)) >> 32; 367 sc_desc.iepcount_set = ns % cycletime; 368 sc_desc.CMP0_current = cycletime - 4; //Count from 0 to (cycle time)-4 369 370 memcpy_toio(sc_descp, &sc_desc, sizeof(sc_desc)); 371 372 writeb(1, &sc_descp->request); 373 374 timeout = 5; /* fw should take 2-3 ms */ 375 while (timeout--) { 376 if (readb(&sc_descp->acknowledgment)) 377 return; 378 379 usleep_range(500, 1000); 380 } 381 382 dev_err(emac->prueth->dev, "settime timeout\n"); 383 } 384 385 static int prueth_perout_enable(void *clockops_data, 386 struct ptp_perout_request *req, int on, 387 u64 *cmp) 388 { 389 struct prueth_emac *emac = clockops_data; 390 u32 reduction_factor = 0, offset = 0; 391 struct timespec64 ts; 392 u64 ns_period; 393 394 if (!on) 395 return 0; 396 397 /* Any firmware specific stuff for PPS/PEROUT handling */ 398 ts.tv_sec = req->period.sec; 399 ts.tv_nsec = req->period.nsec; 400 ns_period = timespec64_to_ns(&ts); 401 402 /* f/w doesn't support period less than cycle time */ 403 if (ns_period < IEP_DEFAULT_CYCLE_TIME_NS) 404 return -ENXIO; 405 406 reduction_factor = ns_period / IEP_DEFAULT_CYCLE_TIME_NS; 407 offset = ns_period % IEP_DEFAULT_CYCLE_TIME_NS; 408 409 /* f/w requires at least 1uS within a cycle so CMP 410 * can trigger after SYNC is enabled 411 */ 412 if (offset < 5 * NSEC_PER_USEC) 413 offset = 5 * NSEC_PER_USEC; 414 415 /* if offset is close to cycle time then we will miss 416 * the CMP event for last tick when IEP rolls over. 417 * In normal mode, IEP tick is 4ns. 418 * In slow compensation it could be 0ns or 8ns at 419 * every slow compensation cycle. 420 */ 421 if (offset > IEP_DEFAULT_CYCLE_TIME_NS - 8) 422 offset = IEP_DEFAULT_CYCLE_TIME_NS - 8; 423 424 /* we're in shadow mode so need to set upper 32-bits */ 425 *cmp = (u64)offset << 32; 426 427 writel(reduction_factor, emac->prueth->shram.va + 428 TIMESYNC_FW_WC_SYNCOUT_REDUCTION_FACTOR_OFFSET); 429 430 writel(0, emac->prueth->shram.va + 431 TIMESYNC_FW_WC_SYNCOUT_START_TIME_CYCLECOUNT_OFFSET); 432 433 return 0; 434 } 435 436 const struct icss_iep_clockops prueth_iep_clockops = { 437 .settime = prueth_iep_settime, 438 .gettime = prueth_iep_gettime, 439 .perout_enable = prueth_perout_enable, 440 }; 441 442 static int icssg_prueth_add_mcast(struct net_device *ndev, const u8 *addr) 443 { 444 struct prueth_emac *emac = netdev_priv(ndev); 445 int port_mask = BIT(emac->port_id); 446 447 port_mask |= icssg_fdb_lookup(emac, addr, 0); 448 icssg_fdb_add_del(emac, addr, 0, port_mask, true); 449 icssg_vtbl_modify(emac, 0, port_mask, port_mask, true); 450 451 return 0; 452 } 453 454 static int icssg_prueth_del_mcast(struct net_device *ndev, const u8 *addr) 455 { 456 struct prueth_emac *emac = netdev_priv(ndev); 457 int port_mask = BIT(emac->port_id); 458 int other_port_mask; 459 460 other_port_mask = port_mask ^ icssg_fdb_lookup(emac, addr, 0); 461 462 icssg_fdb_add_del(emac, addr, 0, port_mask, false); 463 icssg_vtbl_modify(emac, 0, port_mask, port_mask, false); 464 465 if (other_port_mask) { 466 icssg_fdb_add_del(emac, addr, 0, other_port_mask, true); 467 icssg_vtbl_modify(emac, 0, other_port_mask, other_port_mask, true); 468 } 469 470 return 0; 471 } 472 473 /** 474 * emac_ndo_open - EMAC device open 475 * @ndev: network adapter device 476 * 477 * Called when system wants to start the interface. 478 * 479 * Return: 0 for a successful open, or appropriate error code 480 */ 481 static int emac_ndo_open(struct net_device *ndev) 482 { 483 struct prueth_emac *emac = netdev_priv(ndev); 484 int ret, i, num_data_chn = emac->tx_ch_num; 485 struct prueth *prueth = emac->prueth; 486 int slice = prueth_emac_slice(emac); 487 struct device *dev = prueth->dev; 488 int max_rx_flows; 489 int rx_flow; 490 491 /* clear SMEM and MSMC settings for all slices */ 492 if (!prueth->emacs_initialized) { 493 memset_io(prueth->msmcram.va, 0, prueth->msmcram.size); 494 memset_io(prueth->shram.va, 0, ICSSG_CONFIG_OFFSET_SLICE1 * PRUETH_NUM_MACS); 495 } 496 497 /* set h/w MAC as user might have re-configured */ 498 ether_addr_copy(emac->mac_addr, ndev->dev_addr); 499 500 icssg_class_set_mac_addr(prueth->miig_rt, slice, emac->mac_addr); 501 icssg_class_default(prueth->miig_rt, slice, 0, false); 502 icssg_ft1_set_mac_addr(prueth->miig_rt, slice, emac->mac_addr); 503 504 /* Notify the stack of the actual queue counts. */ 505 ret = netif_set_real_num_tx_queues(ndev, num_data_chn); 506 if (ret) { 507 dev_err(dev, "cannot set real number of tx queues\n"); 508 return ret; 509 } 510 511 init_completion(&emac->cmd_complete); 512 ret = prueth_init_tx_chns(emac); 513 if (ret) { 514 dev_err(dev, "failed to init tx channel: %d\n", ret); 515 return ret; 516 } 517 518 max_rx_flows = PRUETH_MAX_RX_FLOWS; 519 ret = prueth_init_rx_chns(emac, &emac->rx_chns, "rx", 520 max_rx_flows, PRUETH_MAX_RX_DESC); 521 if (ret) { 522 dev_err(dev, "failed to init rx channel: %d\n", ret); 523 goto cleanup_tx; 524 } 525 526 ret = prueth_ndev_add_tx_napi(emac); 527 if (ret) 528 goto cleanup_rx; 529 530 /* we use only the highest priority flow for now i.e. @irq[3] */ 531 rx_flow = PRUETH_RX_FLOW_DATA; 532 ret = request_irq(emac->rx_chns.irq[rx_flow], prueth_rx_irq, 533 IRQF_TRIGGER_HIGH, dev_name(dev), emac); 534 if (ret) { 535 dev_err(dev, "unable to request RX IRQ\n"); 536 goto cleanup_napi; 537 } 538 539 /* reset and start PRU firmware */ 540 ret = prueth_emac_start(prueth, emac); 541 if (ret) 542 goto free_rx_irq; 543 544 icssg_mii_update_mtu(prueth->mii_rt, slice, ndev->max_mtu); 545 546 if (!prueth->emacs_initialized) { 547 ret = icss_iep_init(emac->iep, &prueth_iep_clockops, 548 emac, IEP_DEFAULT_CYCLE_TIME_NS); 549 } 550 551 ret = request_threaded_irq(emac->tx_ts_irq, NULL, prueth_tx_ts_irq, 552 IRQF_ONESHOT, dev_name(dev), emac); 553 if (ret) 554 goto stop; 555 556 /* Prepare RX */ 557 ret = prueth_prepare_rx_chan(emac, &emac->rx_chns, PRUETH_MAX_PKT_SIZE); 558 if (ret) 559 goto free_tx_ts_irq; 560 561 ret = k3_udma_glue_enable_rx_chn(emac->rx_chns.rx_chn); 562 if (ret) 563 goto reset_rx_chn; 564 565 for (i = 0; i < emac->tx_ch_num; i++) { 566 ret = k3_udma_glue_enable_tx_chn(emac->tx_chns[i].tx_chn); 567 if (ret) 568 goto reset_tx_chan; 569 } 570 571 /* Enable NAPI in Tx and Rx direction */ 572 for (i = 0; i < emac->tx_ch_num; i++) 573 napi_enable(&emac->tx_chns[i].napi_tx); 574 napi_enable(&emac->napi_rx); 575 576 /* start PHY */ 577 phy_start(ndev->phydev); 578 579 prueth->emacs_initialized++; 580 581 queue_work(system_long_wq, &emac->stats_work.work); 582 583 return 0; 584 585 reset_tx_chan: 586 /* Since interface is not yet up, there is wouldn't be 587 * any SKB for completion. So set false to free_skb 588 */ 589 prueth_reset_tx_chan(emac, i, false); 590 reset_rx_chn: 591 prueth_reset_rx_chan(&emac->rx_chns, max_rx_flows, false); 592 free_tx_ts_irq: 593 free_irq(emac->tx_ts_irq, emac); 594 stop: 595 prueth_emac_stop(emac); 596 free_rx_irq: 597 free_irq(emac->rx_chns.irq[rx_flow], emac); 598 cleanup_napi: 599 prueth_ndev_del_tx_napi(emac, emac->tx_ch_num); 600 cleanup_rx: 601 prueth_cleanup_rx_chns(emac, &emac->rx_chns, max_rx_flows); 602 cleanup_tx: 603 prueth_cleanup_tx_chns(emac); 604 605 return ret; 606 } 607 608 /** 609 * emac_ndo_stop - EMAC device stop 610 * @ndev: network adapter device 611 * 612 * Called when system wants to stop or down the interface. 613 * 614 * Return: Always 0 (Success) 615 */ 616 static int emac_ndo_stop(struct net_device *ndev) 617 { 618 struct prueth_emac *emac = netdev_priv(ndev); 619 struct prueth *prueth = emac->prueth; 620 int rx_flow = PRUETH_RX_FLOW_DATA; 621 int max_rx_flows; 622 int ret, i; 623 624 /* inform the upper layers. */ 625 netif_tx_stop_all_queues(ndev); 626 627 /* block packets from wire */ 628 if (ndev->phydev) 629 phy_stop(ndev->phydev); 630 631 icssg_class_disable(prueth->miig_rt, prueth_emac_slice(emac)); 632 633 __dev_mc_unsync(ndev, icssg_prueth_del_mcast); 634 635 atomic_set(&emac->tdown_cnt, emac->tx_ch_num); 636 /* ensure new tdown_cnt value is visible */ 637 smp_mb__after_atomic(); 638 /* tear down and disable UDMA channels */ 639 reinit_completion(&emac->tdown_complete); 640 for (i = 0; i < emac->tx_ch_num; i++) 641 k3_udma_glue_tdown_tx_chn(emac->tx_chns[i].tx_chn, false); 642 643 ret = wait_for_completion_timeout(&emac->tdown_complete, 644 msecs_to_jiffies(1000)); 645 if (!ret) 646 netdev_err(ndev, "tx teardown timeout\n"); 647 648 prueth_reset_tx_chan(emac, emac->tx_ch_num, true); 649 for (i = 0; i < emac->tx_ch_num; i++) { 650 napi_disable(&emac->tx_chns[i].napi_tx); 651 hrtimer_cancel(&emac->tx_chns[i].tx_hrtimer); 652 } 653 654 max_rx_flows = PRUETH_MAX_RX_FLOWS; 655 k3_udma_glue_tdown_rx_chn(emac->rx_chns.rx_chn, true); 656 657 prueth_reset_rx_chan(&emac->rx_chns, max_rx_flows, true); 658 659 napi_disable(&emac->napi_rx); 660 hrtimer_cancel(&emac->rx_hrtimer); 661 662 cancel_work_sync(&emac->rx_mode_work); 663 664 /* Destroying the queued work in ndo_stop() */ 665 cancel_delayed_work_sync(&emac->stats_work); 666 667 if (prueth->emacs_initialized == 1) 668 icss_iep_exit(emac->iep); 669 670 /* stop PRUs */ 671 prueth_emac_stop(emac); 672 673 free_irq(emac->tx_ts_irq, emac); 674 675 free_irq(emac->rx_chns.irq[rx_flow], emac); 676 prueth_ndev_del_tx_napi(emac, emac->tx_ch_num); 677 678 prueth_cleanup_rx_chns(emac, &emac->rx_chns, max_rx_flows); 679 prueth_cleanup_tx_chns(emac); 680 681 prueth->emacs_initialized--; 682 683 return 0; 684 } 685 686 static void emac_ndo_set_rx_mode_work(struct work_struct *work) 687 { 688 struct prueth_emac *emac = container_of(work, struct prueth_emac, rx_mode_work); 689 struct net_device *ndev = emac->ndev; 690 bool promisc, allmulti; 691 692 if (!netif_running(ndev)) 693 return; 694 695 promisc = ndev->flags & IFF_PROMISC; 696 allmulti = ndev->flags & IFF_ALLMULTI; 697 icssg_set_port_state(emac, ICSSG_EMAC_PORT_UC_FLOODING_DISABLE); 698 icssg_set_port_state(emac, ICSSG_EMAC_PORT_MC_FLOODING_DISABLE); 699 700 if (promisc) { 701 icssg_set_port_state(emac, ICSSG_EMAC_PORT_UC_FLOODING_ENABLE); 702 icssg_set_port_state(emac, ICSSG_EMAC_PORT_MC_FLOODING_ENABLE); 703 return; 704 } 705 706 if (allmulti) { 707 icssg_set_port_state(emac, ICSSG_EMAC_PORT_MC_FLOODING_ENABLE); 708 return; 709 } 710 711 __dev_mc_sync(ndev, icssg_prueth_add_mcast, icssg_prueth_del_mcast); 712 } 713 714 /** 715 * emac_ndo_set_rx_mode - EMAC set receive mode function 716 * @ndev: The EMAC network adapter 717 * 718 * Called when system wants to set the receive mode of the device. 719 * 720 */ 721 static void emac_ndo_set_rx_mode(struct net_device *ndev) 722 { 723 struct prueth_emac *emac = netdev_priv(ndev); 724 725 queue_work(emac->cmd_wq, &emac->rx_mode_work); 726 } 727 728 static const struct net_device_ops emac_netdev_ops = { 729 .ndo_open = emac_ndo_open, 730 .ndo_stop = emac_ndo_stop, 731 .ndo_start_xmit = icssg_ndo_start_xmit, 732 .ndo_set_mac_address = eth_mac_addr, 733 .ndo_validate_addr = eth_validate_addr, 734 .ndo_tx_timeout = icssg_ndo_tx_timeout, 735 .ndo_set_rx_mode = emac_ndo_set_rx_mode, 736 .ndo_eth_ioctl = icssg_ndo_ioctl, 737 .ndo_get_stats64 = icssg_ndo_get_stats64, 738 .ndo_get_phys_port_name = icssg_ndo_get_phys_port_name, 739 }; 740 741 static int prueth_netdev_init(struct prueth *prueth, 742 struct device_node *eth_node) 743 { 744 int ret, num_tx_chn = PRUETH_MAX_TX_QUEUES; 745 struct prueth_emac *emac; 746 struct net_device *ndev; 747 enum prueth_port port; 748 const char *irq_name; 749 enum prueth_mac mac; 750 751 port = prueth_node_port(eth_node); 752 if (port == PRUETH_PORT_INVALID) 753 return -EINVAL; 754 755 mac = prueth_node_mac(eth_node); 756 if (mac == PRUETH_MAC_INVALID) 757 return -EINVAL; 758 759 ndev = alloc_etherdev_mq(sizeof(*emac), num_tx_chn); 760 if (!ndev) 761 return -ENOMEM; 762 763 emac = netdev_priv(ndev); 764 emac->prueth = prueth; 765 emac->ndev = ndev; 766 emac->port_id = port; 767 emac->cmd_wq = create_singlethread_workqueue("icssg_cmd_wq"); 768 if (!emac->cmd_wq) { 769 ret = -ENOMEM; 770 goto free_ndev; 771 } 772 INIT_WORK(&emac->rx_mode_work, emac_ndo_set_rx_mode_work); 773 774 INIT_DELAYED_WORK(&emac->stats_work, icssg_stats_work_handler); 775 776 ret = pruss_request_mem_region(prueth->pruss, 777 port == PRUETH_PORT_MII0 ? 778 PRUSS_MEM_DRAM0 : PRUSS_MEM_DRAM1, 779 &emac->dram); 780 if (ret) { 781 dev_err(prueth->dev, "unable to get DRAM: %d\n", ret); 782 ret = -ENOMEM; 783 goto free_wq; 784 } 785 786 emac->tx_ch_num = 1; 787 788 irq_name = "tx_ts0"; 789 if (emac->port_id == PRUETH_PORT_MII1) 790 irq_name = "tx_ts1"; 791 emac->tx_ts_irq = platform_get_irq_byname_optional(prueth->pdev, irq_name); 792 if (emac->tx_ts_irq < 0) { 793 ret = dev_err_probe(prueth->dev, emac->tx_ts_irq, "could not get tx_ts_irq\n"); 794 goto free; 795 } 796 797 SET_NETDEV_DEV(ndev, prueth->dev); 798 spin_lock_init(&emac->lock); 799 mutex_init(&emac->cmd_lock); 800 801 emac->phy_node = of_parse_phandle(eth_node, "phy-handle", 0); 802 if (!emac->phy_node && !of_phy_is_fixed_link(eth_node)) { 803 dev_err(prueth->dev, "couldn't find phy-handle\n"); 804 ret = -ENODEV; 805 goto free; 806 } else if (of_phy_is_fixed_link(eth_node)) { 807 ret = of_phy_register_fixed_link(eth_node); 808 if (ret) { 809 ret = dev_err_probe(prueth->dev, ret, 810 "failed to register fixed-link phy\n"); 811 goto free; 812 } 813 814 emac->phy_node = eth_node; 815 } 816 817 ret = of_get_phy_mode(eth_node, &emac->phy_if); 818 if (ret) { 819 dev_err(prueth->dev, "could not get phy-mode property\n"); 820 goto free; 821 } 822 823 if (emac->phy_if != PHY_INTERFACE_MODE_MII && 824 !phy_interface_mode_is_rgmii(emac->phy_if)) { 825 dev_err(prueth->dev, "PHY mode unsupported %s\n", phy_modes(emac->phy_if)); 826 ret = -EINVAL; 827 goto free; 828 } 829 830 /* AM65 SR2.0 has TX Internal delay always enabled by hardware 831 * and it is not possible to disable TX Internal delay. The below 832 * switch case block describes how we handle different phy modes 833 * based on hardware restriction. 834 */ 835 switch (emac->phy_if) { 836 case PHY_INTERFACE_MODE_RGMII_ID: 837 emac->phy_if = PHY_INTERFACE_MODE_RGMII_RXID; 838 break; 839 case PHY_INTERFACE_MODE_RGMII_TXID: 840 emac->phy_if = PHY_INTERFACE_MODE_RGMII; 841 break; 842 case PHY_INTERFACE_MODE_RGMII: 843 case PHY_INTERFACE_MODE_RGMII_RXID: 844 dev_err(prueth->dev, "RGMII mode without TX delay is not supported"); 845 ret = -EINVAL; 846 goto free; 847 default: 848 break; 849 } 850 851 /* get mac address from DT and set private and netdev addr */ 852 ret = of_get_ethdev_address(eth_node, ndev); 853 if (!is_valid_ether_addr(ndev->dev_addr)) { 854 eth_hw_addr_random(ndev); 855 dev_warn(prueth->dev, "port %d: using random MAC addr: %pM\n", 856 port, ndev->dev_addr); 857 } 858 ether_addr_copy(emac->mac_addr, ndev->dev_addr); 859 860 ndev->dev.of_node = eth_node; 861 ndev->min_mtu = PRUETH_MIN_PKT_SIZE; 862 ndev->max_mtu = PRUETH_MAX_MTU; 863 ndev->netdev_ops = &emac_netdev_ops; 864 ndev->ethtool_ops = &icssg_ethtool_ops; 865 ndev->hw_features = NETIF_F_SG; 866 ndev->features = ndev->hw_features; 867 868 netif_napi_add(ndev, &emac->napi_rx, icssg_napi_rx_poll); 869 hrtimer_init(&emac->rx_hrtimer, CLOCK_MONOTONIC, 870 HRTIMER_MODE_REL_PINNED); 871 emac->rx_hrtimer.function = &emac_rx_timer_callback; 872 prueth->emac[mac] = emac; 873 874 return 0; 875 876 free: 877 pruss_release_mem_region(prueth->pruss, &emac->dram); 878 free_wq: 879 destroy_workqueue(emac->cmd_wq); 880 free_ndev: 881 emac->ndev = NULL; 882 prueth->emac[mac] = NULL; 883 free_netdev(ndev); 884 885 return ret; 886 } 887 888 bool prueth_dev_check(const struct net_device *ndev) 889 { 890 if (ndev->netdev_ops == &emac_netdev_ops && netif_running(ndev)) { 891 struct prueth_emac *emac = netdev_priv(ndev); 892 893 return emac->prueth->is_switch_mode; 894 } 895 896 return false; 897 } 898 899 static void prueth_offload_fwd_mark_update(struct prueth *prueth) 900 { 901 int set_val = 0; 902 int i; 903 904 if (prueth->br_members == (BIT(PRUETH_PORT_MII0) | BIT(PRUETH_PORT_MII1))) 905 set_val = 1; 906 907 dev_dbg(prueth->dev, "set offload_fwd_mark %d\n", set_val); 908 909 for (i = PRUETH_MAC0; i < PRUETH_NUM_MACS; i++) { 910 struct prueth_emac *emac = prueth->emac[i]; 911 912 if (!emac || !emac->ndev) 913 continue; 914 915 emac->offload_fwd_mark = set_val; 916 } 917 } 918 919 static void prueth_emac_restart(struct prueth *prueth) 920 { 921 struct prueth_emac *emac0 = prueth->emac[PRUETH_MAC0]; 922 struct prueth_emac *emac1 = prueth->emac[PRUETH_MAC1]; 923 924 /* Detach the net_device for both PRUeth ports*/ 925 if (netif_running(emac0->ndev)) 926 netif_device_detach(emac0->ndev); 927 if (netif_running(emac1->ndev)) 928 netif_device_detach(emac1->ndev); 929 930 /* Disable both PRUeth ports */ 931 icssg_set_port_state(emac0, ICSSG_EMAC_PORT_DISABLE); 932 icssg_set_port_state(emac1, ICSSG_EMAC_PORT_DISABLE); 933 934 /* Stop both pru cores for both PRUeth ports*/ 935 prueth_emac_stop(emac0); 936 prueth->emacs_initialized--; 937 prueth_emac_stop(emac1); 938 prueth->emacs_initialized--; 939 940 /* Start both pru cores for both PRUeth ports */ 941 prueth_emac_start(prueth, emac0); 942 prueth->emacs_initialized++; 943 prueth_emac_start(prueth, emac1); 944 prueth->emacs_initialized++; 945 946 /* Enable forwarding for both PRUeth ports */ 947 icssg_set_port_state(emac0, ICSSG_EMAC_PORT_FORWARD); 948 icssg_set_port_state(emac1, ICSSG_EMAC_PORT_FORWARD); 949 950 /* Attache net_device for both PRUeth ports */ 951 netif_device_attach(emac0->ndev); 952 netif_device_attach(emac1->ndev); 953 } 954 955 static void icssg_enable_switch_mode(struct prueth *prueth) 956 { 957 struct prueth_emac *emac; 958 int mac; 959 960 prueth_emac_restart(prueth); 961 962 for (mac = PRUETH_MAC0; mac < PRUETH_NUM_MACS; mac++) { 963 emac = prueth->emac[mac]; 964 if (netif_running(emac->ndev)) { 965 icssg_fdb_add_del(emac, eth_stp_addr, prueth->default_vlan, 966 ICSSG_FDB_ENTRY_P0_MEMBERSHIP | 967 ICSSG_FDB_ENTRY_P1_MEMBERSHIP | 968 ICSSG_FDB_ENTRY_P2_MEMBERSHIP | 969 ICSSG_FDB_ENTRY_BLOCK, 970 true); 971 icssg_vtbl_modify(emac, emac->port_vlan | DEFAULT_VID, 972 BIT(emac->port_id) | DEFAULT_PORT_MASK, 973 BIT(emac->port_id) | DEFAULT_UNTAG_MASK, 974 true); 975 icssg_set_pvid(prueth, emac->port_vlan, emac->port_id); 976 icssg_set_port_state(emac, ICSSG_EMAC_PORT_VLAN_AWARE_ENABLE); 977 } 978 } 979 } 980 981 static int prueth_netdevice_port_link(struct net_device *ndev, 982 struct net_device *br_ndev, 983 struct netlink_ext_ack *extack) 984 { 985 struct prueth_emac *emac = netdev_priv(ndev); 986 struct prueth *prueth = emac->prueth; 987 int err; 988 989 if (!prueth->br_members) { 990 prueth->hw_bridge_dev = br_ndev; 991 } else { 992 /* This is adding the port to a second bridge, this is 993 * unsupported 994 */ 995 if (prueth->hw_bridge_dev != br_ndev) 996 return -EOPNOTSUPP; 997 } 998 999 err = switchdev_bridge_port_offload(br_ndev, ndev, emac, 1000 &prueth->prueth_switchdev_nb, 1001 &prueth->prueth_switchdev_bl_nb, 1002 false, extack); 1003 if (err) 1004 return err; 1005 1006 prueth->br_members |= BIT(emac->port_id); 1007 1008 if (!prueth->is_switch_mode) { 1009 if (prueth->br_members & BIT(PRUETH_PORT_MII0) && 1010 prueth->br_members & BIT(PRUETH_PORT_MII1)) { 1011 prueth->is_switch_mode = true; 1012 prueth->default_vlan = 1; 1013 emac->port_vlan = prueth->default_vlan; 1014 icssg_enable_switch_mode(prueth); 1015 } 1016 } 1017 1018 prueth_offload_fwd_mark_update(prueth); 1019 1020 return NOTIFY_DONE; 1021 } 1022 1023 static void prueth_netdevice_port_unlink(struct net_device *ndev) 1024 { 1025 struct prueth_emac *emac = netdev_priv(ndev); 1026 struct prueth *prueth = emac->prueth; 1027 1028 prueth->br_members &= ~BIT(emac->port_id); 1029 1030 if (prueth->is_switch_mode) { 1031 prueth->is_switch_mode = false; 1032 emac->port_vlan = 0; 1033 prueth_emac_restart(prueth); 1034 } 1035 1036 prueth_offload_fwd_mark_update(prueth); 1037 1038 if (!prueth->br_members) 1039 prueth->hw_bridge_dev = NULL; 1040 } 1041 1042 /* netdev notifier */ 1043 static int prueth_netdevice_event(struct notifier_block *unused, 1044 unsigned long event, void *ptr) 1045 { 1046 struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr); 1047 struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 1048 struct netdev_notifier_changeupper_info *info; 1049 int ret = NOTIFY_DONE; 1050 1051 if (ndev->netdev_ops != &emac_netdev_ops) 1052 return NOTIFY_DONE; 1053 1054 switch (event) { 1055 case NETDEV_CHANGEUPPER: 1056 info = ptr; 1057 1058 if (netif_is_bridge_master(info->upper_dev)) { 1059 if (info->linking) 1060 ret = prueth_netdevice_port_link(ndev, info->upper_dev, extack); 1061 else 1062 prueth_netdevice_port_unlink(ndev); 1063 } 1064 break; 1065 default: 1066 return NOTIFY_DONE; 1067 } 1068 1069 return notifier_from_errno(ret); 1070 } 1071 1072 static int prueth_register_notifiers(struct prueth *prueth) 1073 { 1074 int ret = 0; 1075 1076 prueth->prueth_netdevice_nb.notifier_call = &prueth_netdevice_event; 1077 ret = register_netdevice_notifier(&prueth->prueth_netdevice_nb); 1078 if (ret) { 1079 dev_err(prueth->dev, "can't register netdevice notifier\n"); 1080 return ret; 1081 } 1082 1083 ret = prueth_switchdev_register_notifiers(prueth); 1084 if (ret) 1085 unregister_netdevice_notifier(&prueth->prueth_netdevice_nb); 1086 1087 return ret; 1088 } 1089 1090 static void prueth_unregister_notifiers(struct prueth *prueth) 1091 { 1092 prueth_switchdev_unregister_notifiers(prueth); 1093 unregister_netdevice_notifier(&prueth->prueth_netdevice_nb); 1094 } 1095 1096 static int prueth_probe(struct platform_device *pdev) 1097 { 1098 struct device_node *eth_node, *eth_ports_node; 1099 struct device_node *eth0_node = NULL; 1100 struct device_node *eth1_node = NULL; 1101 struct genpool_data_align gp_data = { 1102 .align = SZ_64K, 1103 }; 1104 struct device *dev = &pdev->dev; 1105 struct device_node *np; 1106 struct prueth *prueth; 1107 struct pruss *pruss; 1108 u32 msmc_ram_size; 1109 int i, ret; 1110 1111 np = dev->of_node; 1112 1113 prueth = devm_kzalloc(dev, sizeof(*prueth), GFP_KERNEL); 1114 if (!prueth) 1115 return -ENOMEM; 1116 1117 dev_set_drvdata(dev, prueth); 1118 prueth->pdev = pdev; 1119 prueth->pdata = *(const struct prueth_pdata *)device_get_match_data(dev); 1120 1121 prueth->dev = dev; 1122 eth_ports_node = of_get_child_by_name(np, "ethernet-ports"); 1123 if (!eth_ports_node) 1124 return -ENOENT; 1125 1126 for_each_child_of_node(eth_ports_node, eth_node) { 1127 u32 reg; 1128 1129 if (strcmp(eth_node->name, "port")) 1130 continue; 1131 ret = of_property_read_u32(eth_node, "reg", ®); 1132 if (ret < 0) { 1133 dev_err(dev, "%pOF error reading port_id %d\n", 1134 eth_node, ret); 1135 } 1136 1137 of_node_get(eth_node); 1138 1139 if (reg == 0) { 1140 eth0_node = eth_node; 1141 if (!of_device_is_available(eth0_node)) { 1142 of_node_put(eth0_node); 1143 eth0_node = NULL; 1144 } 1145 } else if (reg == 1) { 1146 eth1_node = eth_node; 1147 if (!of_device_is_available(eth1_node)) { 1148 of_node_put(eth1_node); 1149 eth1_node = NULL; 1150 } 1151 } else { 1152 dev_err(dev, "port reg should be 0 or 1\n"); 1153 } 1154 } 1155 1156 of_node_put(eth_ports_node); 1157 1158 /* At least one node must be present and available else we fail */ 1159 if (!eth0_node && !eth1_node) { 1160 dev_err(dev, "neither port0 nor port1 node available\n"); 1161 return -ENODEV; 1162 } 1163 1164 if (eth0_node == eth1_node) { 1165 dev_err(dev, "port0 and port1 can't have same reg\n"); 1166 of_node_put(eth0_node); 1167 return -ENODEV; 1168 } 1169 1170 prueth->eth_node[PRUETH_MAC0] = eth0_node; 1171 prueth->eth_node[PRUETH_MAC1] = eth1_node; 1172 1173 prueth->miig_rt = syscon_regmap_lookup_by_phandle(np, "ti,mii-g-rt"); 1174 if (IS_ERR(prueth->miig_rt)) { 1175 dev_err(dev, "couldn't get ti,mii-g-rt syscon regmap\n"); 1176 return -ENODEV; 1177 } 1178 1179 prueth->mii_rt = syscon_regmap_lookup_by_phandle(np, "ti,mii-rt"); 1180 if (IS_ERR(prueth->mii_rt)) { 1181 dev_err(dev, "couldn't get ti,mii-rt syscon regmap\n"); 1182 return -ENODEV; 1183 } 1184 1185 prueth->pa_stats = syscon_regmap_lookup_by_phandle(np, "ti,pa-stats"); 1186 if (IS_ERR(prueth->pa_stats)) { 1187 dev_err(dev, "couldn't get ti,pa-stats syscon regmap\n"); 1188 prueth->pa_stats = NULL; 1189 } 1190 1191 if (eth0_node) { 1192 ret = prueth_get_cores(prueth, ICSS_SLICE0, false); 1193 if (ret) 1194 goto put_cores; 1195 } 1196 1197 if (eth1_node) { 1198 ret = prueth_get_cores(prueth, ICSS_SLICE1, false); 1199 if (ret) 1200 goto put_cores; 1201 } 1202 1203 pruss = pruss_get(eth0_node ? 1204 prueth->pru[ICSS_SLICE0] : prueth->pru[ICSS_SLICE1]); 1205 if (IS_ERR(pruss)) { 1206 ret = PTR_ERR(pruss); 1207 dev_err(dev, "unable to get pruss handle\n"); 1208 goto put_cores; 1209 } 1210 1211 prueth->pruss = pruss; 1212 1213 ret = pruss_request_mem_region(pruss, PRUSS_MEM_SHRD_RAM2, 1214 &prueth->shram); 1215 if (ret) { 1216 dev_err(dev, "unable to get PRUSS SHRD RAM2: %d\n", ret); 1217 goto put_pruss; 1218 } 1219 1220 prueth->sram_pool = of_gen_pool_get(np, "sram", 0); 1221 if (!prueth->sram_pool) { 1222 dev_err(dev, "unable to get SRAM pool\n"); 1223 ret = -ENODEV; 1224 1225 goto put_mem; 1226 } 1227 1228 msmc_ram_size = MSMC_RAM_SIZE; 1229 prueth->is_switchmode_supported = prueth->pdata.switch_mode; 1230 if (prueth->is_switchmode_supported) 1231 msmc_ram_size = MSMC_RAM_SIZE_SWITCH_MODE; 1232 1233 /* NOTE: FW bug needs buffer base to be 64KB aligned */ 1234 prueth->msmcram.va = 1235 (void __iomem *)gen_pool_alloc_algo(prueth->sram_pool, 1236 msmc_ram_size, 1237 gen_pool_first_fit_align, 1238 &gp_data); 1239 1240 if (!prueth->msmcram.va) { 1241 ret = -ENOMEM; 1242 dev_err(dev, "unable to allocate MSMC resource\n"); 1243 goto put_mem; 1244 } 1245 prueth->msmcram.pa = gen_pool_virt_to_phys(prueth->sram_pool, 1246 (unsigned long)prueth->msmcram.va); 1247 prueth->msmcram.size = msmc_ram_size; 1248 memset_io(prueth->msmcram.va, 0, msmc_ram_size); 1249 dev_dbg(dev, "sram: pa %llx va %p size %zx\n", prueth->msmcram.pa, 1250 prueth->msmcram.va, prueth->msmcram.size); 1251 1252 prueth->iep0 = icss_iep_get_idx(np, 0); 1253 if (IS_ERR(prueth->iep0)) { 1254 ret = dev_err_probe(dev, PTR_ERR(prueth->iep0), "iep0 get failed\n"); 1255 prueth->iep0 = NULL; 1256 goto free_pool; 1257 } 1258 1259 prueth->iep1 = icss_iep_get_idx(np, 1); 1260 if (IS_ERR(prueth->iep1)) { 1261 ret = dev_err_probe(dev, PTR_ERR(prueth->iep1), "iep1 get failed\n"); 1262 goto put_iep0; 1263 } 1264 1265 if (prueth->pdata.quirk_10m_link_issue) { 1266 /* Enable IEP1 for FW in 64bit mode as W/A for 10M FD link detect issue under TX 1267 * traffic. 1268 */ 1269 icss_iep_init_fw(prueth->iep1); 1270 } 1271 1272 /* setup netdev interfaces */ 1273 if (eth0_node) { 1274 ret = prueth_netdev_init(prueth, eth0_node); 1275 if (ret) { 1276 dev_err_probe(dev, ret, "netdev init %s failed\n", 1277 eth0_node->name); 1278 goto exit_iep; 1279 } 1280 1281 prueth->emac[PRUETH_MAC0]->half_duplex = 1282 of_property_read_bool(eth0_node, "ti,half-duplex-capable"); 1283 1284 prueth->emac[PRUETH_MAC0]->iep = prueth->iep0; 1285 } 1286 1287 if (eth1_node) { 1288 ret = prueth_netdev_init(prueth, eth1_node); 1289 if (ret) { 1290 dev_err_probe(dev, ret, "netdev init %s failed\n", 1291 eth1_node->name); 1292 goto netdev_exit; 1293 } 1294 1295 prueth->emac[PRUETH_MAC1]->half_duplex = 1296 of_property_read_bool(eth1_node, "ti,half-duplex-capable"); 1297 1298 prueth->emac[PRUETH_MAC1]->iep = prueth->iep0; 1299 } 1300 1301 /* register the network devices */ 1302 if (eth0_node) { 1303 ret = register_netdev(prueth->emac[PRUETH_MAC0]->ndev); 1304 if (ret) { 1305 dev_err(dev, "can't register netdev for port MII0"); 1306 goto netdev_exit; 1307 } 1308 1309 prueth->registered_netdevs[PRUETH_MAC0] = prueth->emac[PRUETH_MAC0]->ndev; 1310 1311 ret = emac_phy_connect(prueth->emac[PRUETH_MAC0]); 1312 if (ret) { 1313 dev_err(dev, 1314 "can't connect to MII0 PHY, error -%d", ret); 1315 goto netdev_unregister; 1316 } 1317 phy_attached_info(prueth->emac[PRUETH_MAC0]->ndev->phydev); 1318 } 1319 1320 if (eth1_node) { 1321 ret = register_netdev(prueth->emac[PRUETH_MAC1]->ndev); 1322 if (ret) { 1323 dev_err(dev, "can't register netdev for port MII1"); 1324 goto netdev_unregister; 1325 } 1326 1327 prueth->registered_netdevs[PRUETH_MAC1] = prueth->emac[PRUETH_MAC1]->ndev; 1328 ret = emac_phy_connect(prueth->emac[PRUETH_MAC1]); 1329 if (ret) { 1330 dev_err(dev, 1331 "can't connect to MII1 PHY, error %d", ret); 1332 goto netdev_unregister; 1333 } 1334 phy_attached_info(prueth->emac[PRUETH_MAC1]->ndev->phydev); 1335 } 1336 1337 if (prueth->is_switchmode_supported) { 1338 ret = prueth_register_notifiers(prueth); 1339 if (ret) 1340 goto netdev_unregister; 1341 1342 sprintf(prueth->switch_id, "%s", dev_name(dev)); 1343 } 1344 1345 dev_info(dev, "TI PRU ethernet driver initialized: %s EMAC mode\n", 1346 (!eth0_node || !eth1_node) ? "single" : "dual"); 1347 1348 if (eth1_node) 1349 of_node_put(eth1_node); 1350 if (eth0_node) 1351 of_node_put(eth0_node); 1352 return 0; 1353 1354 netdev_unregister: 1355 for (i = 0; i < PRUETH_NUM_MACS; i++) { 1356 if (!prueth->registered_netdevs[i]) 1357 continue; 1358 if (prueth->emac[i]->ndev->phydev) { 1359 phy_disconnect(prueth->emac[i]->ndev->phydev); 1360 prueth->emac[i]->ndev->phydev = NULL; 1361 } 1362 unregister_netdev(prueth->registered_netdevs[i]); 1363 } 1364 1365 netdev_exit: 1366 for (i = 0; i < PRUETH_NUM_MACS; i++) { 1367 eth_node = prueth->eth_node[i]; 1368 if (!eth_node) 1369 continue; 1370 1371 prueth_netdev_exit(prueth, eth_node); 1372 } 1373 1374 exit_iep: 1375 if (prueth->pdata.quirk_10m_link_issue) 1376 icss_iep_exit_fw(prueth->iep1); 1377 icss_iep_put(prueth->iep1); 1378 1379 put_iep0: 1380 icss_iep_put(prueth->iep0); 1381 prueth->iep0 = NULL; 1382 prueth->iep1 = NULL; 1383 1384 free_pool: 1385 gen_pool_free(prueth->sram_pool, 1386 (unsigned long)prueth->msmcram.va, msmc_ram_size); 1387 1388 put_mem: 1389 pruss_release_mem_region(prueth->pruss, &prueth->shram); 1390 1391 put_pruss: 1392 pruss_put(prueth->pruss); 1393 1394 put_cores: 1395 if (eth1_node) { 1396 prueth_put_cores(prueth, ICSS_SLICE1); 1397 of_node_put(eth1_node); 1398 } 1399 1400 if (eth0_node) { 1401 prueth_put_cores(prueth, ICSS_SLICE0); 1402 of_node_put(eth0_node); 1403 } 1404 1405 return ret; 1406 } 1407 1408 static void prueth_remove(struct platform_device *pdev) 1409 { 1410 struct prueth *prueth = platform_get_drvdata(pdev); 1411 struct device_node *eth_node; 1412 int i; 1413 1414 prueth_unregister_notifiers(prueth); 1415 1416 for (i = 0; i < PRUETH_NUM_MACS; i++) { 1417 if (!prueth->registered_netdevs[i]) 1418 continue; 1419 phy_stop(prueth->emac[i]->ndev->phydev); 1420 phy_disconnect(prueth->emac[i]->ndev->phydev); 1421 prueth->emac[i]->ndev->phydev = NULL; 1422 unregister_netdev(prueth->registered_netdevs[i]); 1423 } 1424 1425 for (i = 0; i < PRUETH_NUM_MACS; i++) { 1426 eth_node = prueth->eth_node[i]; 1427 if (!eth_node) 1428 continue; 1429 1430 prueth_netdev_exit(prueth, eth_node); 1431 } 1432 1433 if (prueth->pdata.quirk_10m_link_issue) 1434 icss_iep_exit_fw(prueth->iep1); 1435 1436 icss_iep_put(prueth->iep1); 1437 icss_iep_put(prueth->iep0); 1438 1439 gen_pool_free(prueth->sram_pool, 1440 (unsigned long)prueth->msmcram.va, 1441 MSMC_RAM_SIZE); 1442 1443 pruss_release_mem_region(prueth->pruss, &prueth->shram); 1444 1445 pruss_put(prueth->pruss); 1446 1447 if (prueth->eth_node[PRUETH_MAC1]) 1448 prueth_put_cores(prueth, ICSS_SLICE1); 1449 1450 if (prueth->eth_node[PRUETH_MAC0]) 1451 prueth_put_cores(prueth, ICSS_SLICE0); 1452 } 1453 1454 static const struct prueth_pdata am654_icssg_pdata = { 1455 .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 1456 .quirk_10m_link_issue = 1, 1457 .switch_mode = 1, 1458 }; 1459 1460 static const struct prueth_pdata am64x_icssg_pdata = { 1461 .fdqring_mode = K3_RINGACC_RING_MODE_RING, 1462 .quirk_10m_link_issue = 1, 1463 .switch_mode = 1, 1464 }; 1465 1466 static const struct of_device_id prueth_dt_match[] = { 1467 { .compatible = "ti,am654-icssg-prueth", .data = &am654_icssg_pdata }, 1468 { .compatible = "ti,am642-icssg-prueth", .data = &am64x_icssg_pdata }, 1469 { /* sentinel */ } 1470 }; 1471 MODULE_DEVICE_TABLE(of, prueth_dt_match); 1472 1473 static struct platform_driver prueth_driver = { 1474 .probe = prueth_probe, 1475 .remove_new = prueth_remove, 1476 .driver = { 1477 .name = "icssg-prueth", 1478 .of_match_table = prueth_dt_match, 1479 .pm = &prueth_dev_pm_ops, 1480 }, 1481 }; 1482 module_platform_driver(prueth_driver); 1483 1484 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>"); 1485 MODULE_AUTHOR("Md Danish Anwar <danishanwar@ti.com>"); 1486 MODULE_DESCRIPTION("PRUSS ICSSG Ethernet Driver"); 1487 MODULE_LICENSE("GPL"); 1488