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