1 // SPDX-License-Identifier: GPL-2.0
3 /* Texas Instruments ICSSG Ethernet Driver
5 * Copyright (C) 2018-2022 Texas Instruments Incorporated - https://www.ti.com/
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/io-64-nonatomic-hi-lo.h>
20 #include <linux/kernel.h>
21 #include <linux/mfd/syscon.h>
22 #include <linux/module.h>
24 #include <linux/of_mdio.h>
25 #include <linux/of_net.h>
26 #include <linux/platform_device.h>
27 #include <linux/phy.h>
28 #include <linux/property.h>
29 #include <linux/remoteproc/pruss.h>
30 #include <linux/regmap.h>
31 #include <linux/remoteproc.h>
32 #include <net/switchdev.h>
34 #include "icssg_prueth.h"
35 #include "icssg_mii_rt.h"
36 #include "icssg_switchdev.h"
37 #include "../k3-cppi-desc-pool.h"
39 #define PRUETH_MODULE_DESCRIPTION "PRUSS ICSSG Ethernet driver"
42 #define DEFAULT_PORT_MASK 1
43 #define DEFAULT_UNTAG_MASK 1
45 #define NETIF_PRUETH_HSR_OFFLOAD_FEATURES (NETIF_F_HW_HSR_FWD | \
46 NETIF_F_HW_HSR_DUP | \
47 NETIF_F_HW_HSR_TAG_INS | \
48 NETIF_F_HW_HSR_TAG_RM)
50 /* CTRLMMR_ICSSG_RGMII_CTRL register bits */
51 #define ICSSG_CTRL_RGMII_ID_MODE BIT(24)
53 static int emac_get_tx_ts(struct prueth_emac
*emac
,
54 struct emac_tx_ts_response
*rsp
)
56 struct prueth
*prueth
= emac
->prueth
;
57 int slice
= prueth_emac_slice(emac
);
60 addr
= icssg_queue_pop(prueth
, slice
== 0 ?
61 ICSSG_TS_POP_SLICE0
: ICSSG_TS_POP_SLICE1
);
65 memcpy_fromio(rsp
, prueth
->shram
.va
+ addr
, sizeof(*rsp
));
66 /* return buffer back for to pool */
67 icssg_queue_push(prueth
, slice
== 0 ?
68 ICSSG_TS_PUSH_SLICE0
: ICSSG_TS_PUSH_SLICE1
, addr
);
73 static void tx_ts_work(struct prueth_emac
*emac
)
75 struct skb_shared_hwtstamps ssh
;
76 struct emac_tx_ts_response tsr
;
82 /* There may be more than one pending requests */
84 ret
= emac_get_tx_ts(emac
, &tsr
);
85 if (ret
) /* nothing more */
88 if (tsr
.cookie
>= PRUETH_MAX_TX_TS_REQUESTS
||
89 !emac
->tx_ts_skb
[tsr
.cookie
]) {
90 netdev_err(emac
->ndev
, "Invalid TX TS cookie 0x%x\n",
95 skb
= emac
->tx_ts_skb
[tsr
.cookie
];
96 emac
->tx_ts_skb
[tsr
.cookie
] = NULL
; /* free slot */
98 netdev_err(emac
->ndev
, "Driver Bug! got NULL skb\n");
102 hi_sw
= readl(emac
->prueth
->shram
.va
+
103 TIMESYNC_FW_WC_COUNT_HI_SW_OFFSET_OFFSET
);
104 ns
= icssg_ts_to_ns(hi_sw
, tsr
.hi_ts
, tsr
.lo_ts
,
105 IEP_DEFAULT_CYCLE_TIME_NS
);
107 memset(&ssh
, 0, sizeof(ssh
));
108 ssh
.hwtstamp
= ns_to_ktime(ns
);
110 skb_tstamp_tx(skb
, &ssh
);
111 dev_consume_skb_any(skb
);
113 if (atomic_dec_and_test(&emac
->tx_ts_pending
)) /* no more? */
118 static irqreturn_t
prueth_tx_ts_irq(int irq
, void *dev_id
)
120 struct prueth_emac
*emac
= dev_id
;
122 /* currently only TX timestamp is being returned */
128 static struct icssg_firmwares icssg_hsr_firmwares
[] = {
130 .pru
= "ti-pruss/am65x-sr2-pru0-pruhsr-fw.elf",
131 .rtu
= "ti-pruss/am65x-sr2-rtu0-pruhsr-fw.elf",
132 .txpru
= "ti-pruss/am65x-sr2-txpru0-pruhsr-fw.elf",
135 .pru
= "ti-pruss/am65x-sr2-pru1-pruhsr-fw.elf",
136 .rtu
= "ti-pruss/am65x-sr2-rtu1-pruhsr-fw.elf",
137 .txpru
= "ti-pruss/am65x-sr2-txpru1-pruhsr-fw.elf",
141 static struct icssg_firmwares icssg_switch_firmwares
[] = {
143 .pru
= "ti-pruss/am65x-sr2-pru0-prusw-fw.elf",
144 .rtu
= "ti-pruss/am65x-sr2-rtu0-prusw-fw.elf",
145 .txpru
= "ti-pruss/am65x-sr2-txpru0-prusw-fw.elf",
148 .pru
= "ti-pruss/am65x-sr2-pru1-prusw-fw.elf",
149 .rtu
= "ti-pruss/am65x-sr2-rtu1-prusw-fw.elf",
150 .txpru
= "ti-pruss/am65x-sr2-txpru1-prusw-fw.elf",
154 static struct icssg_firmwares icssg_emac_firmwares
[] = {
156 .pru
= "ti-pruss/am65x-sr2-pru0-prueth-fw.elf",
157 .rtu
= "ti-pruss/am65x-sr2-rtu0-prueth-fw.elf",
158 .txpru
= "ti-pruss/am65x-sr2-txpru0-prueth-fw.elf",
161 .pru
= "ti-pruss/am65x-sr2-pru1-prueth-fw.elf",
162 .rtu
= "ti-pruss/am65x-sr2-rtu1-prueth-fw.elf",
163 .txpru
= "ti-pruss/am65x-sr2-txpru1-prueth-fw.elf",
167 static int prueth_emac_start(struct prueth
*prueth
, struct prueth_emac
*emac
)
169 struct icssg_firmwares
*firmwares
;
170 struct device
*dev
= prueth
->dev
;
173 if (prueth
->is_switch_mode
)
174 firmwares
= icssg_switch_firmwares
;
175 else if (prueth
->is_hsr_offload_mode
)
176 firmwares
= icssg_hsr_firmwares
;
178 firmwares
= icssg_emac_firmwares
;
180 slice
= prueth_emac_slice(emac
);
182 netdev_err(emac
->ndev
, "invalid port\n");
186 ret
= icssg_config(prueth
, emac
, slice
);
190 ret
= rproc_set_firmware(prueth
->pru
[slice
], firmwares
[slice
].pru
);
191 ret
= rproc_boot(prueth
->pru
[slice
]);
193 dev_err(dev
, "failed to boot PRU%d: %d\n", slice
, ret
);
197 ret
= rproc_set_firmware(prueth
->rtu
[slice
], firmwares
[slice
].rtu
);
198 ret
= rproc_boot(prueth
->rtu
[slice
]);
200 dev_err(dev
, "failed to boot RTU%d: %d\n", slice
, ret
);
204 ret
= rproc_set_firmware(prueth
->txpru
[slice
], firmwares
[slice
].txpru
);
205 ret
= rproc_boot(prueth
->txpru
[slice
]);
207 dev_err(dev
, "failed to boot TX_PRU%d: %d\n", slice
, ret
);
211 emac
->fw_running
= 1;
215 rproc_shutdown(prueth
->rtu
[slice
]);
218 rproc_shutdown(prueth
->pru
[slice
]);
223 /* called back by PHY layer if there is change in link state of hw port*/
224 static void emac_adjust_link(struct net_device
*ndev
)
226 struct prueth_emac
*emac
= netdev_priv(ndev
);
227 struct phy_device
*phydev
= ndev
->phydev
;
228 struct prueth
*prueth
= emac
->prueth
;
229 bool new_state
= false;
233 /* check the mode of operation - full/half duplex */
234 if (phydev
->duplex
!= emac
->duplex
) {
236 emac
->duplex
= phydev
->duplex
;
238 if (phydev
->speed
!= emac
->speed
) {
240 emac
->speed
= phydev
->speed
;
246 } else if (emac
->link
) {
250 /* f/w should support 100 & 1000 */
251 emac
->speed
= SPEED_1000
;
253 /* half duplex may not be supported by f/w */
254 emac
->duplex
= DUPLEX_FULL
;
258 phy_print_status(phydev
);
260 /* update RGMII and MII configuration based on PHY negotiated
264 if (emac
->duplex
== DUPLEX_HALF
)
265 icssg_config_half_duplex(emac
);
266 /* Set the RGMII cfg for gig en and full duplex */
267 icssg_update_rgmii_cfg(prueth
->miig_rt
, emac
);
269 /* update the Tx IPG based on 100M/1G speed */
270 spin_lock_irqsave(&emac
->lock
, flags
);
271 icssg_config_ipg(emac
);
272 spin_unlock_irqrestore(&emac
->lock
, flags
);
273 icssg_config_set_speed(emac
);
274 icssg_set_port_state(emac
, ICSSG_EMAC_PORT_FORWARD
);
277 icssg_set_port_state(emac
, ICSSG_EMAC_PORT_DISABLE
);
282 /* reactivate the transmit queue */
283 netif_tx_wake_all_queues(ndev
);
285 netif_tx_stop_all_queues(ndev
);
286 prueth_cleanup_tx_ts(emac
);
290 static enum hrtimer_restart
emac_rx_timer_callback(struct hrtimer
*timer
)
292 struct prueth_emac
*emac
=
293 container_of(timer
, struct prueth_emac
, rx_hrtimer
);
294 int rx_flow
= PRUETH_RX_FLOW_DATA
;
296 enable_irq(emac
->rx_chns
.irq
[rx_flow
]);
297 return HRTIMER_NORESTART
;
300 static int emac_phy_connect(struct prueth_emac
*emac
)
302 struct prueth
*prueth
= emac
->prueth
;
303 struct net_device
*ndev
= emac
->ndev
;
305 ndev
->phydev
= of_phy_connect(emac
->ndev
, emac
->phy_node
,
306 &emac_adjust_link
, 0,
309 dev_err(prueth
->dev
, "couldn't connect to phy %s\n",
310 emac
->phy_node
->full_name
);
314 if (!emac
->half_duplex
) {
315 dev_dbg(prueth
->dev
, "half duplex mode is not supported\n");
316 phy_remove_link_mode(ndev
->phydev
, ETHTOOL_LINK_MODE_10baseT_Half_BIT
);
317 phy_remove_link_mode(ndev
->phydev
, ETHTOOL_LINK_MODE_100baseT_Half_BIT
);
320 /* remove unsupported modes */
321 phy_remove_link_mode(ndev
->phydev
, ETHTOOL_LINK_MODE_1000baseT_Half_BIT
);
322 phy_remove_link_mode(ndev
->phydev
, ETHTOOL_LINK_MODE_Pause_BIT
);
323 phy_remove_link_mode(ndev
->phydev
, ETHTOOL_LINK_MODE_Asym_Pause_BIT
);
325 if (emac
->phy_if
== PHY_INTERFACE_MODE_MII
)
326 phy_set_max_speed(ndev
->phydev
, SPEED_100
);
331 static u64
prueth_iep_gettime(void *clockops_data
, struct ptp_system_timestamp
*sts
)
333 u32 hi_rollover_count
, hi_rollover_count_r
;
334 struct prueth_emac
*emac
= clockops_data
;
335 struct prueth
*prueth
= emac
->prueth
;
336 void __iomem
*fw_hi_r_count_addr
;
337 void __iomem
*fw_count_hi_addr
;
338 u32 iepcount_hi
, iepcount_hi_r
;
343 fw_count_hi_addr
= prueth
->shram
.va
+ TIMESYNC_FW_WC_COUNT_HI_SW_OFFSET_OFFSET
;
344 fw_hi_r_count_addr
= prueth
->shram
.va
+ TIMESYNC_FW_WC_HI_ROLLOVER_COUNT_OFFSET
;
346 local_irq_save(flags
);
348 iepcount_hi
= icss_iep_get_count_hi(emac
->iep
);
349 iepcount_hi
+= readl(fw_count_hi_addr
);
350 hi_rollover_count
= readl(fw_hi_r_count_addr
);
351 ptp_read_system_prets(sts
);
352 iepcount_lo
= icss_iep_get_count_low(emac
->iep
);
353 ptp_read_system_postts(sts
);
355 iepcount_hi_r
= icss_iep_get_count_hi(emac
->iep
);
356 iepcount_hi_r
+= readl(fw_count_hi_addr
);
357 hi_rollover_count_r
= readl(fw_hi_r_count_addr
);
358 } while ((iepcount_hi_r
!= iepcount_hi
) ||
359 (hi_rollover_count
!= hi_rollover_count_r
));
360 local_irq_restore(flags
);
362 ts
= ((u64
)hi_rollover_count
) << 23 | iepcount_hi
;
363 ts
= ts
* (u64
)IEP_DEFAULT_CYCLE_TIME_NS
+ iepcount_lo
;
368 static void prueth_iep_settime(void *clockops_data
, u64 ns
)
370 struct icssg_setclock_desc __iomem
*sc_descp
;
371 struct prueth_emac
*emac
= clockops_data
;
372 struct icssg_setclock_desc sc_desc
;
377 if (!emac
->fw_running
)
380 sc_descp
= emac
->prueth
->shram
.va
+ TIMESYNC_FW_WC_SETCLOCK_DESC_OFFSET
;
382 cycletime
= IEP_DEFAULT_CYCLE_TIME_NS
;
383 cyclecount
= ns
/ cycletime
;
385 memset(&sc_desc
, 0, sizeof(sc_desc
));
386 sc_desc
.margin
= cycletime
- 1000;
387 sc_desc
.cyclecounter0_set
= cyclecount
& GENMASK(31, 0);
388 sc_desc
.cyclecounter1_set
= (cyclecount
& GENMASK(63, 32)) >> 32;
389 sc_desc
.iepcount_set
= ns
% cycletime
;
390 /* Count from 0 to (cycle time) - emac->iep->def_inc */
391 sc_desc
.CMP0_current
= cycletime
- emac
->iep
->def_inc
;
393 memcpy_toio(sc_descp
, &sc_desc
, sizeof(sc_desc
));
395 writeb(1, &sc_descp
->request
);
397 timeout
= 5; /* fw should take 2-3 ms */
399 if (readb(&sc_descp
->acknowledgment
))
402 usleep_range(500, 1000);
405 dev_err(emac
->prueth
->dev
, "settime timeout\n");
408 static int prueth_perout_enable(void *clockops_data
,
409 struct ptp_perout_request
*req
, int on
,
412 struct prueth_emac
*emac
= clockops_data
;
413 u32 reduction_factor
= 0, offset
= 0;
414 struct timespec64 ts
;
422 /* Any firmware specific stuff for PPS/PEROUT handling */
423 ts
.tv_sec
= req
->period
.sec
;
424 ts
.tv_nsec
= req
->period
.nsec
;
425 ns_period
= timespec64_to_ns(&ts
);
427 /* f/w doesn't support period less than cycle time */
428 if (ns_period
< IEP_DEFAULT_CYCLE_TIME_NS
)
431 reduction_factor
= ns_period
/ IEP_DEFAULT_CYCLE_TIME_NS
;
432 offset
= ns_period
% IEP_DEFAULT_CYCLE_TIME_NS
;
434 /* f/w requires at least 1uS within a cycle so CMP
435 * can trigger after SYNC is enabled
437 if (offset
< 5 * NSEC_PER_USEC
)
438 offset
= 5 * NSEC_PER_USEC
;
440 /* if offset is close to cycle time then we will miss
441 * the CMP event for last tick when IEP rolls over.
442 * In normal mode, IEP tick is 4ns.
443 * In slow compensation it could be 0ns or 8ns at
444 * every slow compensation cycle.
446 if (offset
> IEP_DEFAULT_CYCLE_TIME_NS
- 8)
447 offset
= IEP_DEFAULT_CYCLE_TIME_NS
- 8;
449 /* we're in shadow mode so need to set upper 32-bits */
450 *cmp
= (u64
)offset
<< 32;
452 writel(reduction_factor
, emac
->prueth
->shram
.va
+
453 TIMESYNC_FW_WC_SYNCOUT_REDUCTION_FACTOR_OFFSET
);
455 current_cycle
= icssg_read_time(emac
->prueth
->shram
.va
+
456 TIMESYNC_FW_WC_CYCLECOUNT_OFFSET
);
458 /* Rounding of current_cycle count to next second */
459 start_offset
= roundup(current_cycle
, MSEC_PER_SEC
);
461 hi_lo_writeq(start_offset
, emac
->prueth
->shram
.va
+
462 TIMESYNC_FW_WC_SYNCOUT_START_TIME_CYCLECOUNT_OFFSET
);
467 const struct icss_iep_clockops prueth_iep_clockops
= {
468 .settime
= prueth_iep_settime
,
469 .gettime
= prueth_iep_gettime
,
470 .perout_enable
= prueth_perout_enable
,
473 static int icssg_prueth_add_mcast(struct net_device
*ndev
, const u8
*addr
)
475 struct prueth_emac
*emac
= netdev_priv(ndev
);
476 int port_mask
= BIT(emac
->port_id
);
478 port_mask
|= icssg_fdb_lookup(emac
, addr
, 0);
479 icssg_fdb_add_del(emac
, addr
, 0, port_mask
, true);
480 icssg_vtbl_modify(emac
, 0, port_mask
, port_mask
, true);
485 static int icssg_prueth_del_mcast(struct net_device
*ndev
, const u8
*addr
)
487 struct prueth_emac
*emac
= netdev_priv(ndev
);
488 int port_mask
= BIT(emac
->port_id
);
491 other_port_mask
= port_mask
^ icssg_fdb_lookup(emac
, addr
, 0);
493 icssg_fdb_add_del(emac
, addr
, 0, port_mask
, false);
494 icssg_vtbl_modify(emac
, 0, port_mask
, port_mask
, false);
496 if (other_port_mask
) {
497 icssg_fdb_add_del(emac
, addr
, 0, other_port_mask
, true);
498 icssg_vtbl_modify(emac
, 0, other_port_mask
, other_port_mask
, true);
504 static int icssg_prueth_hsr_add_mcast(struct net_device
*ndev
, const u8
*addr
)
506 struct prueth_emac
*emac
= netdev_priv(ndev
);
507 struct prueth
*prueth
= emac
->prueth
;
509 icssg_fdb_add_del(emac
, addr
, prueth
->default_vlan
,
510 ICSSG_FDB_ENTRY_P0_MEMBERSHIP
|
511 ICSSG_FDB_ENTRY_P1_MEMBERSHIP
|
512 ICSSG_FDB_ENTRY_P2_MEMBERSHIP
|
513 ICSSG_FDB_ENTRY_BLOCK
, true);
515 icssg_vtbl_modify(emac
, emac
->port_vlan
, BIT(emac
->port_id
),
516 BIT(emac
->port_id
), true);
520 static int icssg_prueth_hsr_del_mcast(struct net_device
*ndev
, const u8
*addr
)
522 struct prueth_emac
*emac
= netdev_priv(ndev
);
523 struct prueth
*prueth
= emac
->prueth
;
525 icssg_fdb_add_del(emac
, addr
, prueth
->default_vlan
,
526 ICSSG_FDB_ENTRY_P0_MEMBERSHIP
|
527 ICSSG_FDB_ENTRY_P1_MEMBERSHIP
|
528 ICSSG_FDB_ENTRY_P2_MEMBERSHIP
|
529 ICSSG_FDB_ENTRY_BLOCK
, false);
535 * emac_ndo_open - EMAC device open
536 * @ndev: network adapter device
538 * Called when system wants to start the interface.
540 * Return: 0 for a successful open, or appropriate error code
542 static int emac_ndo_open(struct net_device
*ndev
)
544 struct prueth_emac
*emac
= netdev_priv(ndev
);
545 int ret
, i
, num_data_chn
= emac
->tx_ch_num
;
546 struct prueth
*prueth
= emac
->prueth
;
547 int slice
= prueth_emac_slice(emac
);
548 struct device
*dev
= prueth
->dev
;
552 /* clear SMEM and MSMC settings for all slices */
553 if (!prueth
->emacs_initialized
) {
554 memset_io(prueth
->msmcram
.va
, 0, prueth
->msmcram
.size
);
555 memset_io(prueth
->shram
.va
, 0, ICSSG_CONFIG_OFFSET_SLICE1
* PRUETH_NUM_MACS
);
558 /* set h/w MAC as user might have re-configured */
559 ether_addr_copy(emac
->mac_addr
, ndev
->dev_addr
);
561 icssg_class_set_mac_addr(prueth
->miig_rt
, slice
, emac
->mac_addr
);
562 icssg_class_default(prueth
->miig_rt
, slice
, 0, false);
563 icssg_ft1_set_mac_addr(prueth
->miig_rt
, slice
, emac
->mac_addr
);
565 /* Notify the stack of the actual queue counts. */
566 ret
= netif_set_real_num_tx_queues(ndev
, num_data_chn
);
568 dev_err(dev
, "cannot set real number of tx queues\n");
572 init_completion(&emac
->cmd_complete
);
573 ret
= prueth_init_tx_chns(emac
);
575 dev_err(dev
, "failed to init tx channel: %d\n", ret
);
579 max_rx_flows
= PRUETH_MAX_RX_FLOWS
;
580 ret
= prueth_init_rx_chns(emac
, &emac
->rx_chns
, "rx",
581 max_rx_flows
, PRUETH_MAX_RX_DESC
);
583 dev_err(dev
, "failed to init rx channel: %d\n", ret
);
587 ret
= prueth_ndev_add_tx_napi(emac
);
591 /* we use only the highest priority flow for now i.e. @irq[3] */
592 rx_flow
= PRUETH_RX_FLOW_DATA
;
593 ret
= request_irq(emac
->rx_chns
.irq
[rx_flow
], prueth_rx_irq
,
594 IRQF_TRIGGER_HIGH
, dev_name(dev
), emac
);
596 dev_err(dev
, "unable to request RX IRQ\n");
600 /* reset and start PRU firmware */
601 ret
= prueth_emac_start(prueth
, emac
);
605 icssg_mii_update_mtu(prueth
->mii_rt
, slice
, ndev
->max_mtu
);
607 if (!prueth
->emacs_initialized
) {
608 ret
= icss_iep_init(emac
->iep
, &prueth_iep_clockops
,
609 emac
, IEP_DEFAULT_CYCLE_TIME_NS
);
612 ret
= request_threaded_irq(emac
->tx_ts_irq
, NULL
, prueth_tx_ts_irq
,
613 IRQF_ONESHOT
, dev_name(dev
), emac
);
618 ret
= prueth_prepare_rx_chan(emac
, &emac
->rx_chns
, PRUETH_MAX_PKT_SIZE
);
622 ret
= k3_udma_glue_enable_rx_chn(emac
->rx_chns
.rx_chn
);
626 for (i
= 0; i
< emac
->tx_ch_num
; i
++) {
627 ret
= k3_udma_glue_enable_tx_chn(emac
->tx_chns
[i
].tx_chn
);
632 /* Enable NAPI in Tx and Rx direction */
633 for (i
= 0; i
< emac
->tx_ch_num
; i
++)
634 napi_enable(&emac
->tx_chns
[i
].napi_tx
);
635 napi_enable(&emac
->napi_rx
);
638 phy_start(ndev
->phydev
);
640 prueth
->emacs_initialized
++;
642 queue_work(system_long_wq
, &emac
->stats_work
.work
);
647 /* Since interface is not yet up, there is wouldn't be
648 * any SKB for completion. So set false to free_skb
650 prueth_reset_tx_chan(emac
, i
, false);
652 prueth_reset_rx_chan(&emac
->rx_chns
, max_rx_flows
, false);
654 free_irq(emac
->tx_ts_irq
, emac
);
656 prueth_emac_stop(emac
);
658 free_irq(emac
->rx_chns
.irq
[rx_flow
], emac
);
660 prueth_ndev_del_tx_napi(emac
, emac
->tx_ch_num
);
662 prueth_cleanup_rx_chns(emac
, &emac
->rx_chns
, max_rx_flows
);
664 prueth_cleanup_tx_chns(emac
);
670 * emac_ndo_stop - EMAC device stop
671 * @ndev: network adapter device
673 * Called when system wants to stop or down the interface.
675 * Return: Always 0 (Success)
677 static int emac_ndo_stop(struct net_device
*ndev
)
679 struct prueth_emac
*emac
= netdev_priv(ndev
);
680 struct prueth
*prueth
= emac
->prueth
;
681 int rx_flow
= PRUETH_RX_FLOW_DATA
;
685 /* inform the upper layers. */
686 netif_tx_stop_all_queues(ndev
);
688 /* block packets from wire */
690 phy_stop(ndev
->phydev
);
692 icssg_class_disable(prueth
->miig_rt
, prueth_emac_slice(emac
));
694 if (emac
->prueth
->is_hsr_offload_mode
)
695 __dev_mc_unsync(ndev
, icssg_prueth_hsr_del_mcast
);
697 __dev_mc_unsync(ndev
, icssg_prueth_del_mcast
);
699 atomic_set(&emac
->tdown_cnt
, emac
->tx_ch_num
);
700 /* ensure new tdown_cnt value is visible */
701 smp_mb__after_atomic();
702 /* tear down and disable UDMA channels */
703 reinit_completion(&emac
->tdown_complete
);
704 for (i
= 0; i
< emac
->tx_ch_num
; i
++)
705 k3_udma_glue_tdown_tx_chn(emac
->tx_chns
[i
].tx_chn
, false);
707 ret
= wait_for_completion_timeout(&emac
->tdown_complete
,
708 msecs_to_jiffies(1000));
710 netdev_err(ndev
, "tx teardown timeout\n");
712 prueth_reset_tx_chan(emac
, emac
->tx_ch_num
, true);
713 for (i
= 0; i
< emac
->tx_ch_num
; i
++) {
714 napi_disable(&emac
->tx_chns
[i
].napi_tx
);
715 hrtimer_cancel(&emac
->tx_chns
[i
].tx_hrtimer
);
718 max_rx_flows
= PRUETH_MAX_RX_FLOWS
;
719 k3_udma_glue_tdown_rx_chn(emac
->rx_chns
.rx_chn
, true);
721 prueth_reset_rx_chan(&emac
->rx_chns
, max_rx_flows
, true);
723 napi_disable(&emac
->napi_rx
);
724 hrtimer_cancel(&emac
->rx_hrtimer
);
726 cancel_work_sync(&emac
->rx_mode_work
);
728 /* Destroying the queued work in ndo_stop() */
729 cancel_delayed_work_sync(&emac
->stats_work
);
731 if (prueth
->emacs_initialized
== 1)
732 icss_iep_exit(emac
->iep
);
735 prueth_emac_stop(emac
);
737 free_irq(emac
->tx_ts_irq
, emac
);
739 free_irq(emac
->rx_chns
.irq
[rx_flow
], emac
);
740 prueth_ndev_del_tx_napi(emac
, emac
->tx_ch_num
);
742 prueth_cleanup_rx_chns(emac
, &emac
->rx_chns
, max_rx_flows
);
743 prueth_cleanup_tx_chns(emac
);
745 prueth
->emacs_initialized
--;
750 static void emac_ndo_set_rx_mode_work(struct work_struct
*work
)
752 struct prueth_emac
*emac
= container_of(work
, struct prueth_emac
, rx_mode_work
);
753 struct net_device
*ndev
= emac
->ndev
;
754 bool promisc
, allmulti
;
756 if (!netif_running(ndev
))
759 promisc
= ndev
->flags
& IFF_PROMISC
;
760 allmulti
= ndev
->flags
& IFF_ALLMULTI
;
761 icssg_set_port_state(emac
, ICSSG_EMAC_PORT_UC_FLOODING_DISABLE
);
762 icssg_set_port_state(emac
, ICSSG_EMAC_PORT_MC_FLOODING_DISABLE
);
765 icssg_set_port_state(emac
, ICSSG_EMAC_PORT_UC_FLOODING_ENABLE
);
766 icssg_set_port_state(emac
, ICSSG_EMAC_PORT_MC_FLOODING_ENABLE
);
771 icssg_set_port_state(emac
, ICSSG_EMAC_PORT_MC_FLOODING_ENABLE
);
775 if (emac
->prueth
->is_hsr_offload_mode
)
776 __dev_mc_sync(ndev
, icssg_prueth_hsr_add_mcast
,
777 icssg_prueth_hsr_del_mcast
);
779 __dev_mc_sync(ndev
, icssg_prueth_add_mcast
,
780 icssg_prueth_del_mcast
);
784 * emac_ndo_set_rx_mode - EMAC set receive mode function
785 * @ndev: The EMAC network adapter
787 * Called when system wants to set the receive mode of the device.
790 static void emac_ndo_set_rx_mode(struct net_device
*ndev
)
792 struct prueth_emac
*emac
= netdev_priv(ndev
);
794 queue_work(emac
->cmd_wq
, &emac
->rx_mode_work
);
797 static netdev_features_t
emac_ndo_fix_features(struct net_device
*ndev
,
798 netdev_features_t features
)
800 /* hsr tag insertion offload and hsr dup offload are tightly coupled in
801 * firmware implementation. Both these features need to be enabled /
804 if (!(ndev
->features
& (NETIF_F_HW_HSR_DUP
| NETIF_F_HW_HSR_TAG_INS
)))
805 if ((features
& NETIF_F_HW_HSR_DUP
) ||
806 (features
& NETIF_F_HW_HSR_TAG_INS
))
807 features
|= NETIF_F_HW_HSR_DUP
|
808 NETIF_F_HW_HSR_TAG_INS
;
810 if ((ndev
->features
& NETIF_F_HW_HSR_DUP
) ||
811 (ndev
->features
& NETIF_F_HW_HSR_TAG_INS
))
812 if (!(features
& NETIF_F_HW_HSR_DUP
) ||
813 !(features
& NETIF_F_HW_HSR_TAG_INS
))
814 features
&= ~(NETIF_F_HW_HSR_DUP
|
815 NETIF_F_HW_HSR_TAG_INS
);
820 static int emac_ndo_vlan_rx_add_vid(struct net_device
*ndev
,
821 __be16 proto
, u16 vid
)
823 struct prueth_emac
*emac
= netdev_priv(ndev
);
824 struct prueth
*prueth
= emac
->prueth
;
828 if (prueth
->is_hsr_offload_mode
) {
829 port_mask
= BIT(PRUETH_PORT_HOST
) | BIT(emac
->port_id
);
832 netdev_dbg(emac
->ndev
, "VID add vid:%u port_mask:%X untag_mask %X\n",
833 vid
, port_mask
, untag_mask
);
835 icssg_vtbl_modify(emac
, vid
, port_mask
, untag_mask
, true);
836 icssg_set_pvid(emac
->prueth
, vid
, emac
->port_id
);
841 static int emac_ndo_vlan_rx_del_vid(struct net_device
*ndev
,
842 __be16 proto
, u16 vid
)
844 struct prueth_emac
*emac
= netdev_priv(ndev
);
845 struct prueth
*prueth
= emac
->prueth
;
849 if (prueth
->is_hsr_offload_mode
) {
850 port_mask
= BIT(PRUETH_PORT_HOST
);
853 netdev_dbg(emac
->ndev
, "VID del vid:%u port_mask:%X untag_mask %X\n",
854 vid
, port_mask
, untag_mask
);
856 icssg_vtbl_modify(emac
, vid
, port_mask
, untag_mask
, false);
861 static const struct net_device_ops emac_netdev_ops
= {
862 .ndo_open
= emac_ndo_open
,
863 .ndo_stop
= emac_ndo_stop
,
864 .ndo_start_xmit
= icssg_ndo_start_xmit
,
865 .ndo_set_mac_address
= eth_mac_addr
,
866 .ndo_validate_addr
= eth_validate_addr
,
867 .ndo_tx_timeout
= icssg_ndo_tx_timeout
,
868 .ndo_set_rx_mode
= emac_ndo_set_rx_mode
,
869 .ndo_eth_ioctl
= icssg_ndo_ioctl
,
870 .ndo_get_stats64
= icssg_ndo_get_stats64
,
871 .ndo_get_phys_port_name
= icssg_ndo_get_phys_port_name
,
872 .ndo_fix_features
= emac_ndo_fix_features
,
873 .ndo_vlan_rx_add_vid
= emac_ndo_vlan_rx_add_vid
,
874 .ndo_vlan_rx_kill_vid
= emac_ndo_vlan_rx_del_vid
,
877 static int prueth_netdev_init(struct prueth
*prueth
,
878 struct device_node
*eth_node
)
880 int ret
, num_tx_chn
= PRUETH_MAX_TX_QUEUES
;
881 struct prueth_emac
*emac
;
882 struct net_device
*ndev
;
883 enum prueth_port port
;
884 const char *irq_name
;
887 port
= prueth_node_port(eth_node
);
888 if (port
== PRUETH_PORT_INVALID
)
891 mac
= prueth_node_mac(eth_node
);
892 if (mac
== PRUETH_MAC_INVALID
)
895 ndev
= alloc_etherdev_mq(sizeof(*emac
), num_tx_chn
);
899 emac
= netdev_priv(ndev
);
900 emac
->prueth
= prueth
;
902 emac
->port_id
= port
;
903 emac
->cmd_wq
= create_singlethread_workqueue("icssg_cmd_wq");
908 INIT_WORK(&emac
->rx_mode_work
, emac_ndo_set_rx_mode_work
);
910 INIT_DELAYED_WORK(&emac
->stats_work
, icssg_stats_work_handler
);
912 ret
= pruss_request_mem_region(prueth
->pruss
,
913 port
== PRUETH_PORT_MII0
?
914 PRUSS_MEM_DRAM0
: PRUSS_MEM_DRAM1
,
917 dev_err(prueth
->dev
, "unable to get DRAM: %d\n", ret
);
925 if (emac
->port_id
== PRUETH_PORT_MII1
)
927 emac
->tx_ts_irq
= platform_get_irq_byname_optional(prueth
->pdev
, irq_name
);
928 if (emac
->tx_ts_irq
< 0) {
929 ret
= dev_err_probe(prueth
->dev
, emac
->tx_ts_irq
, "could not get tx_ts_irq\n");
933 SET_NETDEV_DEV(ndev
, prueth
->dev
);
934 spin_lock_init(&emac
->lock
);
935 mutex_init(&emac
->cmd_lock
);
937 emac
->phy_node
= of_parse_phandle(eth_node
, "phy-handle", 0);
938 if (!emac
->phy_node
&& !of_phy_is_fixed_link(eth_node
)) {
939 dev_err(prueth
->dev
, "couldn't find phy-handle\n");
942 } else if (of_phy_is_fixed_link(eth_node
)) {
943 ret
= of_phy_register_fixed_link(eth_node
);
945 ret
= dev_err_probe(prueth
->dev
, ret
,
946 "failed to register fixed-link phy\n");
950 emac
->phy_node
= eth_node
;
953 ret
= of_get_phy_mode(eth_node
, &emac
->phy_if
);
955 dev_err(prueth
->dev
, "could not get phy-mode property\n");
959 if (emac
->phy_if
!= PHY_INTERFACE_MODE_MII
&&
960 !phy_interface_mode_is_rgmii(emac
->phy_if
)) {
961 dev_err(prueth
->dev
, "PHY mode unsupported %s\n", phy_modes(emac
->phy_if
));
966 /* AM65 SR2.0 has TX Internal delay always enabled by hardware
967 * and it is not possible to disable TX Internal delay. The below
968 * switch case block describes how we handle different phy modes
969 * based on hardware restriction.
971 switch (emac
->phy_if
) {
972 case PHY_INTERFACE_MODE_RGMII_ID
:
973 emac
->phy_if
= PHY_INTERFACE_MODE_RGMII_RXID
;
975 case PHY_INTERFACE_MODE_RGMII_TXID
:
976 emac
->phy_if
= PHY_INTERFACE_MODE_RGMII
;
978 case PHY_INTERFACE_MODE_RGMII
:
979 case PHY_INTERFACE_MODE_RGMII_RXID
:
980 dev_err(prueth
->dev
, "RGMII mode without TX delay is not supported");
987 /* get mac address from DT and set private and netdev addr */
988 ret
= of_get_ethdev_address(eth_node
, ndev
);
989 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
990 eth_hw_addr_random(ndev
);
991 dev_warn(prueth
->dev
, "port %d: using random MAC addr: %pM\n",
992 port
, ndev
->dev_addr
);
994 ether_addr_copy(emac
->mac_addr
, ndev
->dev_addr
);
996 ndev
->dev
.of_node
= eth_node
;
997 ndev
->min_mtu
= PRUETH_MIN_PKT_SIZE
;
998 ndev
->max_mtu
= PRUETH_MAX_MTU
;
999 ndev
->netdev_ops
= &emac_netdev_ops
;
1000 ndev
->ethtool_ops
= &icssg_ethtool_ops
;
1001 ndev
->hw_features
= NETIF_F_SG
;
1002 ndev
->features
= ndev
->hw_features
| NETIF_F_HW_VLAN_CTAG_FILTER
;
1003 ndev
->hw_features
|= NETIF_PRUETH_HSR_OFFLOAD_FEATURES
;
1005 netif_napi_add(ndev
, &emac
->napi_rx
, icssg_napi_rx_poll
);
1006 hrtimer_init(&emac
->rx_hrtimer
, CLOCK_MONOTONIC
,
1007 HRTIMER_MODE_REL_PINNED
);
1008 emac
->rx_hrtimer
.function
= &emac_rx_timer_callback
;
1009 prueth
->emac
[mac
] = emac
;
1014 pruss_release_mem_region(prueth
->pruss
, &emac
->dram
);
1016 destroy_workqueue(emac
->cmd_wq
);
1019 prueth
->emac
[mac
] = NULL
;
1025 bool prueth_dev_check(const struct net_device
*ndev
)
1027 if (ndev
->netdev_ops
== &emac_netdev_ops
&& netif_running(ndev
)) {
1028 struct prueth_emac
*emac
= netdev_priv(ndev
);
1030 return emac
->prueth
->is_switch_mode
;
1036 static void prueth_offload_fwd_mark_update(struct prueth
*prueth
)
1041 if (prueth
->br_members
== (BIT(PRUETH_PORT_MII0
) | BIT(PRUETH_PORT_MII1
)))
1044 dev_dbg(prueth
->dev
, "set offload_fwd_mark %d\n", set_val
);
1046 for (i
= PRUETH_MAC0
; i
< PRUETH_NUM_MACS
; i
++) {
1047 struct prueth_emac
*emac
= prueth
->emac
[i
];
1049 if (!emac
|| !emac
->ndev
)
1052 emac
->offload_fwd_mark
= set_val
;
1056 static void prueth_emac_restart(struct prueth
*prueth
)
1058 struct prueth_emac
*emac0
= prueth
->emac
[PRUETH_MAC0
];
1059 struct prueth_emac
*emac1
= prueth
->emac
[PRUETH_MAC1
];
1061 /* Detach the net_device for both PRUeth ports*/
1062 if (netif_running(emac0
->ndev
))
1063 netif_device_detach(emac0
->ndev
);
1064 if (netif_running(emac1
->ndev
))
1065 netif_device_detach(emac1
->ndev
);
1067 /* Disable both PRUeth ports */
1068 icssg_set_port_state(emac0
, ICSSG_EMAC_PORT_DISABLE
);
1069 icssg_set_port_state(emac1
, ICSSG_EMAC_PORT_DISABLE
);
1071 /* Stop both pru cores for both PRUeth ports*/
1072 prueth_emac_stop(emac0
);
1073 prueth
->emacs_initialized
--;
1074 prueth_emac_stop(emac1
);
1075 prueth
->emacs_initialized
--;
1077 /* Start both pru cores for both PRUeth ports */
1078 prueth_emac_start(prueth
, emac0
);
1079 prueth
->emacs_initialized
++;
1080 prueth_emac_start(prueth
, emac1
);
1081 prueth
->emacs_initialized
++;
1083 /* Enable forwarding for both PRUeth ports */
1084 icssg_set_port_state(emac0
, ICSSG_EMAC_PORT_FORWARD
);
1085 icssg_set_port_state(emac1
, ICSSG_EMAC_PORT_FORWARD
);
1087 /* Attache net_device for both PRUeth ports */
1088 netif_device_attach(emac0
->ndev
);
1089 netif_device_attach(emac1
->ndev
);
1092 static void icssg_change_mode(struct prueth
*prueth
)
1094 struct prueth_emac
*emac
;
1097 prueth_emac_restart(prueth
);
1099 for (mac
= PRUETH_MAC0
; mac
< PRUETH_NUM_MACS
; mac
++) {
1100 emac
= prueth
->emac
[mac
];
1101 if (prueth
->is_hsr_offload_mode
) {
1102 if (emac
->ndev
->features
& NETIF_F_HW_HSR_TAG_RM
)
1103 icssg_set_port_state(emac
, ICSSG_EMAC_HSR_RX_OFFLOAD_ENABLE
);
1105 icssg_set_port_state(emac
, ICSSG_EMAC_HSR_RX_OFFLOAD_DISABLE
);
1108 if (netif_running(emac
->ndev
)) {
1109 icssg_fdb_add_del(emac
, eth_stp_addr
, prueth
->default_vlan
,
1110 ICSSG_FDB_ENTRY_P0_MEMBERSHIP
|
1111 ICSSG_FDB_ENTRY_P1_MEMBERSHIP
|
1112 ICSSG_FDB_ENTRY_P2_MEMBERSHIP
|
1113 ICSSG_FDB_ENTRY_BLOCK
,
1115 icssg_vtbl_modify(emac
, emac
->port_vlan
| DEFAULT_VID
,
1116 BIT(emac
->port_id
) | DEFAULT_PORT_MASK
,
1117 BIT(emac
->port_id
) | DEFAULT_UNTAG_MASK
,
1119 if (prueth
->is_hsr_offload_mode
)
1120 icssg_vtbl_modify(emac
, DEFAULT_VID
,
1122 DEFAULT_UNTAG_MASK
, true);
1123 icssg_set_pvid(prueth
, emac
->port_vlan
, emac
->port_id
);
1124 if (prueth
->is_switch_mode
)
1125 icssg_set_port_state(emac
, ICSSG_EMAC_PORT_VLAN_AWARE_ENABLE
);
1130 static int prueth_netdevice_port_link(struct net_device
*ndev
,
1131 struct net_device
*br_ndev
,
1132 struct netlink_ext_ack
*extack
)
1134 struct prueth_emac
*emac
= netdev_priv(ndev
);
1135 struct prueth
*prueth
= emac
->prueth
;
1138 if (!prueth
->br_members
) {
1139 prueth
->hw_bridge_dev
= br_ndev
;
1141 /* This is adding the port to a second bridge, this is
1144 if (prueth
->hw_bridge_dev
!= br_ndev
)
1148 err
= switchdev_bridge_port_offload(br_ndev
, ndev
, emac
,
1149 &prueth
->prueth_switchdev_nb
,
1150 &prueth
->prueth_switchdev_bl_nb
,
1155 prueth
->br_members
|= BIT(emac
->port_id
);
1157 if (!prueth
->is_switch_mode
) {
1158 if (prueth
->br_members
& BIT(PRUETH_PORT_MII0
) &&
1159 prueth
->br_members
& BIT(PRUETH_PORT_MII1
)) {
1160 prueth
->is_switch_mode
= true;
1161 prueth
->default_vlan
= 1;
1162 emac
->port_vlan
= prueth
->default_vlan
;
1163 icssg_change_mode(prueth
);
1167 prueth_offload_fwd_mark_update(prueth
);
1172 static void prueth_netdevice_port_unlink(struct net_device
*ndev
)
1174 struct prueth_emac
*emac
= netdev_priv(ndev
);
1175 struct prueth
*prueth
= emac
->prueth
;
1177 prueth
->br_members
&= ~BIT(emac
->port_id
);
1179 if (prueth
->is_switch_mode
) {
1180 prueth
->is_switch_mode
= false;
1181 emac
->port_vlan
= 0;
1182 prueth_emac_restart(prueth
);
1185 prueth_offload_fwd_mark_update(prueth
);
1187 if (!prueth
->br_members
)
1188 prueth
->hw_bridge_dev
= NULL
;
1191 static int prueth_hsr_port_link(struct net_device
*ndev
)
1193 struct prueth_emac
*emac
= netdev_priv(ndev
);
1194 struct prueth
*prueth
= emac
->prueth
;
1195 struct prueth_emac
*emac0
;
1196 struct prueth_emac
*emac1
;
1198 emac0
= prueth
->emac
[PRUETH_MAC0
];
1199 emac1
= prueth
->emac
[PRUETH_MAC1
];
1201 if (prueth
->is_switch_mode
)
1204 prueth
->hsr_members
|= BIT(emac
->port_id
);
1205 if (!prueth
->is_hsr_offload_mode
) {
1206 if (prueth
->hsr_members
& BIT(PRUETH_PORT_MII0
) &&
1207 prueth
->hsr_members
& BIT(PRUETH_PORT_MII1
)) {
1208 if (!(emac0
->ndev
->features
&
1209 NETIF_PRUETH_HSR_OFFLOAD_FEATURES
) &&
1210 !(emac1
->ndev
->features
&
1211 NETIF_PRUETH_HSR_OFFLOAD_FEATURES
))
1213 prueth
->is_hsr_offload_mode
= true;
1214 prueth
->default_vlan
= 1;
1215 emac0
->port_vlan
= prueth
->default_vlan
;
1216 emac1
->port_vlan
= prueth
->default_vlan
;
1217 icssg_change_mode(prueth
);
1218 netdev_dbg(ndev
, "Enabling HSR offload mode\n");
1225 static void prueth_hsr_port_unlink(struct net_device
*ndev
)
1227 struct prueth_emac
*emac
= netdev_priv(ndev
);
1228 struct prueth
*prueth
= emac
->prueth
;
1229 struct prueth_emac
*emac0
;
1230 struct prueth_emac
*emac1
;
1232 emac0
= prueth
->emac
[PRUETH_MAC0
];
1233 emac1
= prueth
->emac
[PRUETH_MAC1
];
1235 prueth
->hsr_members
&= ~BIT(emac
->port_id
);
1236 if (prueth
->is_hsr_offload_mode
) {
1237 prueth
->is_hsr_offload_mode
= false;
1238 emac0
->port_vlan
= 0;
1239 emac1
->port_vlan
= 0;
1240 prueth
->hsr_dev
= NULL
;
1241 prueth_emac_restart(prueth
);
1242 netdev_dbg(ndev
, "Disabling HSR Offload mode\n");
1246 /* netdev notifier */
1247 static int prueth_netdevice_event(struct notifier_block
*unused
,
1248 unsigned long event
, void *ptr
)
1250 struct netlink_ext_ack
*extack
= netdev_notifier_info_to_extack(ptr
);
1251 struct net_device
*ndev
= netdev_notifier_info_to_dev(ptr
);
1252 struct netdev_notifier_changeupper_info
*info
;
1253 struct prueth_emac
*emac
= netdev_priv(ndev
);
1254 struct prueth
*prueth
= emac
->prueth
;
1255 int ret
= NOTIFY_DONE
;
1257 if (ndev
->netdev_ops
!= &emac_netdev_ops
)
1261 case NETDEV_CHANGEUPPER
:
1264 if ((ndev
->features
& NETIF_PRUETH_HSR_OFFLOAD_FEATURES
) &&
1265 is_hsr_master(info
->upper_dev
)) {
1266 if (info
->linking
) {
1267 if (!prueth
->hsr_dev
) {
1268 prueth
->hsr_dev
= info
->upper_dev
;
1269 icssg_class_set_host_mac_addr(prueth
->miig_rt
,
1270 prueth
->hsr_dev
->dev_addr
);
1272 if (prueth
->hsr_dev
!= info
->upper_dev
) {
1273 netdev_dbg(ndev
, "Both interfaces must be linked to same upper device\n");
1277 prueth_hsr_port_link(ndev
);
1279 prueth_hsr_port_unlink(ndev
);
1283 if (netif_is_bridge_master(info
->upper_dev
)) {
1285 ret
= prueth_netdevice_port_link(ndev
, info
->upper_dev
, extack
);
1287 prueth_netdevice_port_unlink(ndev
);
1294 return notifier_from_errno(ret
);
1297 static int prueth_register_notifiers(struct prueth
*prueth
)
1301 prueth
->prueth_netdevice_nb
.notifier_call
= &prueth_netdevice_event
;
1302 ret
= register_netdevice_notifier(&prueth
->prueth_netdevice_nb
);
1304 dev_err(prueth
->dev
, "can't register netdevice notifier\n");
1308 ret
= prueth_switchdev_register_notifiers(prueth
);
1310 unregister_netdevice_notifier(&prueth
->prueth_netdevice_nb
);
1315 static void prueth_unregister_notifiers(struct prueth
*prueth
)
1317 prueth_switchdev_unregister_notifiers(prueth
);
1318 unregister_netdevice_notifier(&prueth
->prueth_netdevice_nb
);
1321 static int prueth_probe(struct platform_device
*pdev
)
1323 struct device_node
*eth_node
, *eth_ports_node
;
1324 struct device_node
*eth0_node
= NULL
;
1325 struct device_node
*eth1_node
= NULL
;
1326 struct genpool_data_align gp_data
= {
1329 struct device
*dev
= &pdev
->dev
;
1330 struct device_node
*np
;
1331 struct prueth
*prueth
;
1332 struct pruss
*pruss
;
1338 prueth
= devm_kzalloc(dev
, sizeof(*prueth
), GFP_KERNEL
);
1342 dev_set_drvdata(dev
, prueth
);
1343 prueth
->pdev
= pdev
;
1344 prueth
->pdata
= *(const struct prueth_pdata
*)device_get_match_data(dev
);
1347 eth_ports_node
= of_get_child_by_name(np
, "ethernet-ports");
1348 if (!eth_ports_node
)
1351 for_each_child_of_node(eth_ports_node
, eth_node
) {
1354 if (strcmp(eth_node
->name
, "port"))
1356 ret
= of_property_read_u32(eth_node
, "reg", ®
);
1358 dev_err(dev
, "%pOF error reading port_id %d\n",
1362 of_node_get(eth_node
);
1365 eth0_node
= eth_node
;
1366 if (!of_device_is_available(eth0_node
)) {
1367 of_node_put(eth0_node
);
1370 } else if (reg
== 1) {
1371 eth1_node
= eth_node
;
1372 if (!of_device_is_available(eth1_node
)) {
1373 of_node_put(eth1_node
);
1377 dev_err(dev
, "port reg should be 0 or 1\n");
1381 of_node_put(eth_ports_node
);
1383 /* At least one node must be present and available else we fail */
1384 if (!eth0_node
&& !eth1_node
) {
1385 dev_err(dev
, "neither port0 nor port1 node available\n");
1389 if (eth0_node
== eth1_node
) {
1390 dev_err(dev
, "port0 and port1 can't have same reg\n");
1391 of_node_put(eth0_node
);
1395 prueth
->eth_node
[PRUETH_MAC0
] = eth0_node
;
1396 prueth
->eth_node
[PRUETH_MAC1
] = eth1_node
;
1398 prueth
->miig_rt
= syscon_regmap_lookup_by_phandle(np
, "ti,mii-g-rt");
1399 if (IS_ERR(prueth
->miig_rt
)) {
1400 dev_err(dev
, "couldn't get ti,mii-g-rt syscon regmap\n");
1404 prueth
->mii_rt
= syscon_regmap_lookup_by_phandle(np
, "ti,mii-rt");
1405 if (IS_ERR(prueth
->mii_rt
)) {
1406 dev_err(dev
, "couldn't get ti,mii-rt syscon regmap\n");
1410 prueth
->pa_stats
= syscon_regmap_lookup_by_phandle(np
, "ti,pa-stats");
1411 if (IS_ERR(prueth
->pa_stats
)) {
1412 dev_err(dev
, "couldn't get ti,pa-stats syscon regmap\n");
1413 prueth
->pa_stats
= NULL
;
1417 ret
= prueth_get_cores(prueth
, ICSS_SLICE0
, false);
1423 ret
= prueth_get_cores(prueth
, ICSS_SLICE1
, false);
1428 pruss
= pruss_get(eth0_node
?
1429 prueth
->pru
[ICSS_SLICE0
] : prueth
->pru
[ICSS_SLICE1
]);
1430 if (IS_ERR(pruss
)) {
1431 ret
= PTR_ERR(pruss
);
1432 dev_err(dev
, "unable to get pruss handle\n");
1436 prueth
->pruss
= pruss
;
1438 ret
= pruss_request_mem_region(pruss
, PRUSS_MEM_SHRD_RAM2
,
1441 dev_err(dev
, "unable to get PRUSS SHRD RAM2: %d\n", ret
);
1445 prueth
->sram_pool
= of_gen_pool_get(np
, "sram", 0);
1446 if (!prueth
->sram_pool
) {
1447 dev_err(dev
, "unable to get SRAM pool\n");
1453 msmc_ram_size
= MSMC_RAM_SIZE
;
1454 prueth
->is_switchmode_supported
= prueth
->pdata
.switch_mode
;
1455 if (prueth
->is_switchmode_supported
)
1456 msmc_ram_size
= MSMC_RAM_SIZE_SWITCH_MODE
;
1458 /* NOTE: FW bug needs buffer base to be 64KB aligned */
1459 prueth
->msmcram
.va
=
1460 (void __iomem
*)gen_pool_alloc_algo(prueth
->sram_pool
,
1462 gen_pool_first_fit_align
,
1465 if (!prueth
->msmcram
.va
) {
1467 dev_err(dev
, "unable to allocate MSMC resource\n");
1470 prueth
->msmcram
.pa
= gen_pool_virt_to_phys(prueth
->sram_pool
,
1471 (unsigned long)prueth
->msmcram
.va
);
1472 prueth
->msmcram
.size
= msmc_ram_size
;
1473 memset_io(prueth
->msmcram
.va
, 0, msmc_ram_size
);
1474 dev_dbg(dev
, "sram: pa %llx va %p size %zx\n", prueth
->msmcram
.pa
,
1475 prueth
->msmcram
.va
, prueth
->msmcram
.size
);
1477 prueth
->iep0
= icss_iep_get_idx(np
, 0);
1478 if (IS_ERR(prueth
->iep0
)) {
1479 ret
= dev_err_probe(dev
, PTR_ERR(prueth
->iep0
), "iep0 get failed\n");
1480 prueth
->iep0
= NULL
;
1484 prueth
->iep1
= icss_iep_get_idx(np
, 1);
1485 if (IS_ERR(prueth
->iep1
)) {
1486 ret
= dev_err_probe(dev
, PTR_ERR(prueth
->iep1
), "iep1 get failed\n");
1490 if (prueth
->pdata
.quirk_10m_link_issue
) {
1491 /* Enable IEP1 for FW in 64bit mode as W/A for 10M FD link detect issue under TX
1494 icss_iep_init_fw(prueth
->iep1
);
1497 spin_lock_init(&prueth
->vtbl_lock
);
1498 /* setup netdev interfaces */
1500 ret
= prueth_netdev_init(prueth
, eth0_node
);
1502 dev_err_probe(dev
, ret
, "netdev init %s failed\n",
1507 prueth
->emac
[PRUETH_MAC0
]->half_duplex
=
1508 of_property_read_bool(eth0_node
, "ti,half-duplex-capable");
1510 prueth
->emac
[PRUETH_MAC0
]->iep
= prueth
->iep0
;
1514 ret
= prueth_netdev_init(prueth
, eth1_node
);
1516 dev_err_probe(dev
, ret
, "netdev init %s failed\n",
1521 prueth
->emac
[PRUETH_MAC1
]->half_duplex
=
1522 of_property_read_bool(eth1_node
, "ti,half-duplex-capable");
1524 prueth
->emac
[PRUETH_MAC1
]->iep
= prueth
->iep0
;
1527 /* register the network devices */
1529 ret
= register_netdev(prueth
->emac
[PRUETH_MAC0
]->ndev
);
1531 dev_err(dev
, "can't register netdev for port MII0");
1535 prueth
->registered_netdevs
[PRUETH_MAC0
] = prueth
->emac
[PRUETH_MAC0
]->ndev
;
1537 ret
= emac_phy_connect(prueth
->emac
[PRUETH_MAC0
]);
1540 "can't connect to MII0 PHY, error -%d", ret
);
1541 goto netdev_unregister
;
1543 phy_attached_info(prueth
->emac
[PRUETH_MAC0
]->ndev
->phydev
);
1547 ret
= register_netdev(prueth
->emac
[PRUETH_MAC1
]->ndev
);
1549 dev_err(dev
, "can't register netdev for port MII1");
1550 goto netdev_unregister
;
1553 prueth
->registered_netdevs
[PRUETH_MAC1
] = prueth
->emac
[PRUETH_MAC1
]->ndev
;
1554 ret
= emac_phy_connect(prueth
->emac
[PRUETH_MAC1
]);
1557 "can't connect to MII1 PHY, error %d", ret
);
1558 goto netdev_unregister
;
1560 phy_attached_info(prueth
->emac
[PRUETH_MAC1
]->ndev
->phydev
);
1563 if (prueth
->is_switchmode_supported
) {
1564 ret
= prueth_register_notifiers(prueth
);
1566 goto netdev_unregister
;
1568 sprintf(prueth
->switch_id
, "%s", dev_name(dev
));
1571 dev_info(dev
, "TI PRU ethernet driver initialized: %s EMAC mode\n",
1572 (!eth0_node
|| !eth1_node
) ? "single" : "dual");
1575 of_node_put(eth1_node
);
1577 of_node_put(eth0_node
);
1581 for (i
= 0; i
< PRUETH_NUM_MACS
; i
++) {
1582 if (!prueth
->registered_netdevs
[i
])
1584 if (prueth
->emac
[i
]->ndev
->phydev
) {
1585 phy_disconnect(prueth
->emac
[i
]->ndev
->phydev
);
1586 prueth
->emac
[i
]->ndev
->phydev
= NULL
;
1588 unregister_netdev(prueth
->registered_netdevs
[i
]);
1592 for (i
= 0; i
< PRUETH_NUM_MACS
; i
++) {
1593 eth_node
= prueth
->eth_node
[i
];
1597 prueth_netdev_exit(prueth
, eth_node
);
1601 if (prueth
->pdata
.quirk_10m_link_issue
)
1602 icss_iep_exit_fw(prueth
->iep1
);
1603 icss_iep_put(prueth
->iep1
);
1606 icss_iep_put(prueth
->iep0
);
1607 prueth
->iep0
= NULL
;
1608 prueth
->iep1
= NULL
;
1611 gen_pool_free(prueth
->sram_pool
,
1612 (unsigned long)prueth
->msmcram
.va
, msmc_ram_size
);
1615 pruss_release_mem_region(prueth
->pruss
, &prueth
->shram
);
1618 pruss_put(prueth
->pruss
);
1622 prueth_put_cores(prueth
, ICSS_SLICE1
);
1623 of_node_put(eth1_node
);
1627 prueth_put_cores(prueth
, ICSS_SLICE0
);
1628 of_node_put(eth0_node
);
1634 static void prueth_remove(struct platform_device
*pdev
)
1636 struct prueth
*prueth
= platform_get_drvdata(pdev
);
1637 struct device_node
*eth_node
;
1640 prueth_unregister_notifiers(prueth
);
1642 for (i
= 0; i
< PRUETH_NUM_MACS
; i
++) {
1643 if (!prueth
->registered_netdevs
[i
])
1645 phy_stop(prueth
->emac
[i
]->ndev
->phydev
);
1646 phy_disconnect(prueth
->emac
[i
]->ndev
->phydev
);
1647 prueth
->emac
[i
]->ndev
->phydev
= NULL
;
1648 unregister_netdev(prueth
->registered_netdevs
[i
]);
1651 for (i
= 0; i
< PRUETH_NUM_MACS
; i
++) {
1652 eth_node
= prueth
->eth_node
[i
];
1656 prueth_netdev_exit(prueth
, eth_node
);
1659 if (prueth
->pdata
.quirk_10m_link_issue
)
1660 icss_iep_exit_fw(prueth
->iep1
);
1662 icss_iep_put(prueth
->iep1
);
1663 icss_iep_put(prueth
->iep0
);
1665 gen_pool_free(prueth
->sram_pool
,
1666 (unsigned long)prueth
->msmcram
.va
,
1669 pruss_release_mem_region(prueth
->pruss
, &prueth
->shram
);
1671 pruss_put(prueth
->pruss
);
1673 if (prueth
->eth_node
[PRUETH_MAC1
])
1674 prueth_put_cores(prueth
, ICSS_SLICE1
);
1676 if (prueth
->eth_node
[PRUETH_MAC0
])
1677 prueth_put_cores(prueth
, ICSS_SLICE0
);
1680 static const struct prueth_pdata am654_icssg_pdata
= {
1681 .fdqring_mode
= K3_RINGACC_RING_MODE_MESSAGE
,
1682 .quirk_10m_link_issue
= 1,
1686 static const struct prueth_pdata am64x_icssg_pdata
= {
1687 .fdqring_mode
= K3_RINGACC_RING_MODE_RING
,
1688 .quirk_10m_link_issue
= 1,
1692 static const struct of_device_id prueth_dt_match
[] = {
1693 { .compatible
= "ti,am654-icssg-prueth", .data
= &am654_icssg_pdata
},
1694 { .compatible
= "ti,am642-icssg-prueth", .data
= &am64x_icssg_pdata
},
1697 MODULE_DEVICE_TABLE(of
, prueth_dt_match
);
1699 static struct platform_driver prueth_driver
= {
1700 .probe
= prueth_probe
,
1701 .remove
= prueth_remove
,
1703 .name
= "icssg-prueth",
1704 .of_match_table
= prueth_dt_match
,
1705 .pm
= &prueth_dev_pm_ops
,
1708 module_platform_driver(prueth_driver
);
1710 MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
1711 MODULE_AUTHOR("Md Danish Anwar <danishanwar@ti.com>");
1712 MODULE_DESCRIPTION("PRUSS ICSSG Ethernet Driver");
1713 MODULE_LICENSE("GPL");