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