• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /*
3  * Copyright (c) 2018 Synopsys, Inc. and/or its affiliates.
4  * stmmac XGMAC support.
5  */
6 
7 #include <linux/bitrev.h>
8 #include <linux/crc32.h>
9 #include <linux/iopoll.h>
10 #include "stmmac.h"
11 #include "stmmac_ptp.h"
12 #include "dwxlgmac2.h"
13 #include "dwxgmac2.h"
14 
dwxgmac2_core_init(struct mac_device_info * hw,struct net_device * dev)15 static void dwxgmac2_core_init(struct mac_device_info *hw,
16 			       struct net_device *dev)
17 {
18 	void __iomem *ioaddr = hw->pcsr;
19 	u32 tx, rx;
20 
21 	tx = readl(ioaddr + XGMAC_TX_CONFIG);
22 	rx = readl(ioaddr + XGMAC_RX_CONFIG);
23 
24 	tx |= XGMAC_CORE_INIT_TX;
25 	rx |= XGMAC_CORE_INIT_RX;
26 
27 	if (hw->ps) {
28 		tx |= XGMAC_CONFIG_TE;
29 		tx &= ~hw->link.speed_mask;
30 
31 		switch (hw->ps) {
32 		case SPEED_10000:
33 			tx |= hw->link.xgmii.speed10000;
34 			break;
35 		case SPEED_2500:
36 			tx |= hw->link.speed2500;
37 			break;
38 		case SPEED_1000:
39 		default:
40 			tx |= hw->link.speed1000;
41 			break;
42 		}
43 	}
44 
45 	writel(tx, ioaddr + XGMAC_TX_CONFIG);
46 	writel(rx, ioaddr + XGMAC_RX_CONFIG);
47 	writel(XGMAC_INT_DEFAULT_EN, ioaddr + XGMAC_INT_EN);
48 }
49 
dwxgmac2_set_mac(void __iomem * ioaddr,bool enable)50 static void dwxgmac2_set_mac(void __iomem *ioaddr, bool enable)
51 {
52 	u32 tx = readl(ioaddr + XGMAC_TX_CONFIG);
53 	u32 rx = readl(ioaddr + XGMAC_RX_CONFIG);
54 
55 	if (enable) {
56 		tx |= XGMAC_CONFIG_TE;
57 		rx |= XGMAC_CONFIG_RE;
58 	} else {
59 		tx &= ~XGMAC_CONFIG_TE;
60 		rx &= ~XGMAC_CONFIG_RE;
61 	}
62 
63 	writel(tx, ioaddr + XGMAC_TX_CONFIG);
64 	writel(rx, ioaddr + XGMAC_RX_CONFIG);
65 }
66 
dwxgmac2_rx_ipc(struct mac_device_info * hw)67 static int dwxgmac2_rx_ipc(struct mac_device_info *hw)
68 {
69 	void __iomem *ioaddr = hw->pcsr;
70 	u32 value;
71 
72 	value = readl(ioaddr + XGMAC_RX_CONFIG);
73 	if (hw->rx_csum)
74 		value |= XGMAC_CONFIG_IPC;
75 	else
76 		value &= ~XGMAC_CONFIG_IPC;
77 	writel(value, ioaddr + XGMAC_RX_CONFIG);
78 
79 	return !!(readl(ioaddr + XGMAC_RX_CONFIG) & XGMAC_CONFIG_IPC);
80 }
81 
dwxgmac2_rx_queue_enable(struct mac_device_info * hw,u8 mode,u32 queue)82 static void dwxgmac2_rx_queue_enable(struct mac_device_info *hw, u8 mode,
83 				     u32 queue)
84 {
85 	void __iomem *ioaddr = hw->pcsr;
86 	u32 value;
87 
88 	value = readl(ioaddr + XGMAC_RXQ_CTRL0) & ~XGMAC_RXQEN(queue);
89 	if (mode == MTL_QUEUE_AVB)
90 		value |= 0x1 << XGMAC_RXQEN_SHIFT(queue);
91 	else if (mode == MTL_QUEUE_DCB)
92 		value |= 0x2 << XGMAC_RXQEN_SHIFT(queue);
93 	writel(value, ioaddr + XGMAC_RXQ_CTRL0);
94 }
95 
dwxgmac2_rx_queue_prio(struct mac_device_info * hw,u32 prio,u32 queue)96 static void dwxgmac2_rx_queue_prio(struct mac_device_info *hw, u32 prio,
97 				   u32 queue)
98 {
99 	void __iomem *ioaddr = hw->pcsr;
100 	u32 value, reg;
101 
102 	reg = (queue < 4) ? XGMAC_RXQ_CTRL2 : XGMAC_RXQ_CTRL3;
103 	if (queue >= 4)
104 		queue -= 4;
105 
106 	value = readl(ioaddr + reg);
107 	value &= ~XGMAC_PSRQ(queue);
108 	value |= (prio << XGMAC_PSRQ_SHIFT(queue)) & XGMAC_PSRQ(queue);
109 
110 	writel(value, ioaddr + reg);
111 }
112 
dwxgmac2_tx_queue_prio(struct mac_device_info * hw,u32 prio,u32 queue)113 static void dwxgmac2_tx_queue_prio(struct mac_device_info *hw, u32 prio,
114 				   u32 queue)
115 {
116 	void __iomem *ioaddr = hw->pcsr;
117 	u32 value, reg;
118 
119 	reg = (queue < 4) ? XGMAC_TC_PRTY_MAP0 : XGMAC_TC_PRTY_MAP1;
120 	if (queue >= 4)
121 		queue -= 4;
122 
123 	value = readl(ioaddr + reg);
124 	value &= ~XGMAC_PSTC(queue);
125 	value |= (prio << XGMAC_PSTC_SHIFT(queue)) & XGMAC_PSTC(queue);
126 
127 	writel(value, ioaddr + reg);
128 }
129 
dwxgmac2_prog_mtl_rx_algorithms(struct mac_device_info * hw,u32 rx_alg)130 static void dwxgmac2_prog_mtl_rx_algorithms(struct mac_device_info *hw,
131 					    u32 rx_alg)
132 {
133 	void __iomem *ioaddr = hw->pcsr;
134 	u32 value;
135 
136 	value = readl(ioaddr + XGMAC_MTL_OPMODE);
137 	value &= ~XGMAC_RAA;
138 
139 	switch (rx_alg) {
140 	case MTL_RX_ALGORITHM_SP:
141 		break;
142 	case MTL_RX_ALGORITHM_WSP:
143 		value |= XGMAC_RAA;
144 		break;
145 	default:
146 		break;
147 	}
148 
149 	writel(value, ioaddr + XGMAC_MTL_OPMODE);
150 }
151 
dwxgmac2_prog_mtl_tx_algorithms(struct mac_device_info * hw,u32 tx_alg)152 static void dwxgmac2_prog_mtl_tx_algorithms(struct mac_device_info *hw,
153 					    u32 tx_alg)
154 {
155 	void __iomem *ioaddr = hw->pcsr;
156 	bool ets = true;
157 	u32 value;
158 	int i;
159 
160 	value = readl(ioaddr + XGMAC_MTL_OPMODE);
161 	value &= ~XGMAC_ETSALG;
162 
163 	switch (tx_alg) {
164 	case MTL_TX_ALGORITHM_WRR:
165 		value |= XGMAC_WRR;
166 		break;
167 	case MTL_TX_ALGORITHM_WFQ:
168 		value |= XGMAC_WFQ;
169 		break;
170 	case MTL_TX_ALGORITHM_DWRR:
171 		value |= XGMAC_DWRR;
172 		break;
173 	default:
174 		ets = false;
175 		break;
176 	}
177 
178 	writel(value, ioaddr + XGMAC_MTL_OPMODE);
179 
180 	/* Set ETS if desired */
181 	for (i = 0; i < MTL_MAX_TX_QUEUES; i++) {
182 		value = readl(ioaddr + XGMAC_MTL_TCx_ETS_CONTROL(i));
183 		value &= ~XGMAC_TSA;
184 		if (ets)
185 			value |= XGMAC_ETS;
186 		writel(value, ioaddr + XGMAC_MTL_TCx_ETS_CONTROL(i));
187 	}
188 }
189 
dwxgmac2_set_mtl_tx_queue_weight(struct mac_device_info * hw,u32 weight,u32 queue)190 static void dwxgmac2_set_mtl_tx_queue_weight(struct mac_device_info *hw,
191 					     u32 weight, u32 queue)
192 {
193 	void __iomem *ioaddr = hw->pcsr;
194 
195 	writel(weight, ioaddr + XGMAC_MTL_TCx_QUANTUM_WEIGHT(queue));
196 }
197 
dwxgmac2_map_mtl_to_dma(struct mac_device_info * hw,u32 queue,u32 chan)198 static void dwxgmac2_map_mtl_to_dma(struct mac_device_info *hw, u32 queue,
199 				    u32 chan)
200 {
201 	void __iomem *ioaddr = hw->pcsr;
202 	u32 value, reg;
203 
204 	reg = (queue < 4) ? XGMAC_MTL_RXQ_DMA_MAP0 : XGMAC_MTL_RXQ_DMA_MAP1;
205 	if (queue >= 4)
206 		queue -= 4;
207 
208 	value = readl(ioaddr + reg);
209 	value &= ~XGMAC_QxMDMACH(queue);
210 	value |= (chan << XGMAC_QxMDMACH_SHIFT(queue)) & XGMAC_QxMDMACH(queue);
211 
212 	writel(value, ioaddr + reg);
213 }
214 
dwxgmac2_config_cbs(struct mac_device_info * hw,u32 send_slope,u32 idle_slope,u32 high_credit,u32 low_credit,u32 queue)215 static void dwxgmac2_config_cbs(struct mac_device_info *hw,
216 				u32 send_slope, u32 idle_slope,
217 				u32 high_credit, u32 low_credit, u32 queue)
218 {
219 	void __iomem *ioaddr = hw->pcsr;
220 	u32 value;
221 
222 	writel(send_slope, ioaddr + XGMAC_MTL_TCx_SENDSLOPE(queue));
223 	writel(idle_slope, ioaddr + XGMAC_MTL_TCx_QUANTUM_WEIGHT(queue));
224 	writel(high_credit, ioaddr + XGMAC_MTL_TCx_HICREDIT(queue));
225 	writel(low_credit, ioaddr + XGMAC_MTL_TCx_LOCREDIT(queue));
226 
227 	value = readl(ioaddr + XGMAC_MTL_TCx_ETS_CONTROL(queue));
228 	value &= ~XGMAC_TSA;
229 	value |= XGMAC_CC | XGMAC_CBS;
230 	writel(value, ioaddr + XGMAC_MTL_TCx_ETS_CONTROL(queue));
231 }
232 
dwxgmac2_dump_regs(struct mac_device_info * hw,u32 * reg_space)233 static void dwxgmac2_dump_regs(struct mac_device_info *hw, u32 *reg_space)
234 {
235 	void __iomem *ioaddr = hw->pcsr;
236 	int i;
237 
238 	for (i = 0; i < XGMAC_MAC_REGSIZE; i++)
239 		reg_space[i] = readl(ioaddr + i * 4);
240 }
241 
dwxgmac2_host_irq_status(struct mac_device_info * hw,struct stmmac_extra_stats * x)242 static int dwxgmac2_host_irq_status(struct mac_device_info *hw,
243 				    struct stmmac_extra_stats *x)
244 {
245 	void __iomem *ioaddr = hw->pcsr;
246 	u32 stat, en;
247 	int ret = 0;
248 
249 	en = readl(ioaddr + XGMAC_INT_EN);
250 	stat = readl(ioaddr + XGMAC_INT_STATUS);
251 
252 	stat &= en;
253 
254 	if (stat & XGMAC_PMTIS) {
255 		x->irq_receive_pmt_irq_n++;
256 		readl(ioaddr + XGMAC_PMT);
257 	}
258 
259 	if (stat & XGMAC_LPIIS) {
260 		u32 lpi = readl(ioaddr + XGMAC_LPI_CTRL);
261 
262 		if (lpi & XGMAC_TLPIEN) {
263 			ret |= CORE_IRQ_TX_PATH_IN_LPI_MODE;
264 			x->irq_tx_path_in_lpi_mode_n++;
265 		}
266 		if (lpi & XGMAC_TLPIEX) {
267 			ret |= CORE_IRQ_TX_PATH_EXIT_LPI_MODE;
268 			x->irq_tx_path_exit_lpi_mode_n++;
269 		}
270 		if (lpi & XGMAC_RLPIEN)
271 			x->irq_rx_path_in_lpi_mode_n++;
272 		if (lpi & XGMAC_RLPIEX)
273 			x->irq_rx_path_exit_lpi_mode_n++;
274 	}
275 
276 	return ret;
277 }
278 
dwxgmac2_host_mtl_irq_status(struct mac_device_info * hw,u32 chan)279 static int dwxgmac2_host_mtl_irq_status(struct mac_device_info *hw, u32 chan)
280 {
281 	void __iomem *ioaddr = hw->pcsr;
282 	int ret = 0;
283 	u32 status;
284 
285 	status = readl(ioaddr + XGMAC_MTL_INT_STATUS);
286 	if (status & BIT(chan)) {
287 		u32 chan_status = readl(ioaddr + XGMAC_MTL_QINT_STATUS(chan));
288 
289 		if (chan_status & XGMAC_RXOVFIS)
290 			ret |= CORE_IRQ_MTL_RX_OVERFLOW;
291 
292 		writel(~0x0, ioaddr + XGMAC_MTL_QINT_STATUS(chan));
293 	}
294 
295 	return ret;
296 }
297 
dwxgmac2_flow_ctrl(struct mac_device_info * hw,unsigned int duplex,unsigned int fc,unsigned int pause_time,u32 tx_cnt)298 static void dwxgmac2_flow_ctrl(struct mac_device_info *hw, unsigned int duplex,
299 			       unsigned int fc, unsigned int pause_time,
300 			       u32 tx_cnt)
301 {
302 	void __iomem *ioaddr = hw->pcsr;
303 	u32 i;
304 
305 	if (fc & FLOW_RX)
306 		writel(XGMAC_RFE, ioaddr + XGMAC_RX_FLOW_CTRL);
307 	if (fc & FLOW_TX) {
308 		for (i = 0; i < tx_cnt; i++) {
309 			u32 value = XGMAC_TFE;
310 
311 			if (duplex)
312 				value |= pause_time << XGMAC_PT_SHIFT;
313 
314 			writel(value, ioaddr + XGMAC_Qx_TX_FLOW_CTRL(i));
315 		}
316 	}
317 }
318 
dwxgmac2_pmt(struct mac_device_info * hw,unsigned long mode)319 static void dwxgmac2_pmt(struct mac_device_info *hw, unsigned long mode)
320 {
321 	void __iomem *ioaddr = hw->pcsr;
322 	u32 val = 0x0;
323 
324 	if (mode & WAKE_MAGIC)
325 		val |= XGMAC_PWRDWN | XGMAC_MGKPKTEN;
326 	if (mode & WAKE_UCAST)
327 		val |= XGMAC_PWRDWN | XGMAC_GLBLUCAST | XGMAC_RWKPKTEN;
328 	if (val) {
329 		u32 cfg = readl(ioaddr + XGMAC_RX_CONFIG);
330 		cfg |= XGMAC_CONFIG_RE;
331 		writel(cfg, ioaddr + XGMAC_RX_CONFIG);
332 	}
333 
334 	writel(val, ioaddr + XGMAC_PMT);
335 }
336 
dwxgmac2_set_umac_addr(struct mac_device_info * hw,const unsigned char * addr,unsigned int reg_n)337 static void dwxgmac2_set_umac_addr(struct mac_device_info *hw,
338 				   const unsigned char *addr,
339 				   unsigned int reg_n)
340 {
341 	void __iomem *ioaddr = hw->pcsr;
342 	u32 value;
343 
344 	value = (addr[5] << 8) | addr[4];
345 	writel(value | XGMAC_AE, ioaddr + XGMAC_ADDRx_HIGH(reg_n));
346 
347 	value = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
348 	writel(value, ioaddr + XGMAC_ADDRx_LOW(reg_n));
349 }
350 
dwxgmac2_get_umac_addr(struct mac_device_info * hw,unsigned char * addr,unsigned int reg_n)351 static void dwxgmac2_get_umac_addr(struct mac_device_info *hw,
352 				   unsigned char *addr, unsigned int reg_n)
353 {
354 	void __iomem *ioaddr = hw->pcsr;
355 	u32 hi_addr, lo_addr;
356 
357 	/* Read the MAC address from the hardware */
358 	hi_addr = readl(ioaddr + XGMAC_ADDRx_HIGH(reg_n));
359 	lo_addr = readl(ioaddr + XGMAC_ADDRx_LOW(reg_n));
360 
361 	/* Extract the MAC address from the high and low words */
362 	addr[0] = lo_addr & 0xff;
363 	addr[1] = (lo_addr >> 8) & 0xff;
364 	addr[2] = (lo_addr >> 16) & 0xff;
365 	addr[3] = (lo_addr >> 24) & 0xff;
366 	addr[4] = hi_addr & 0xff;
367 	addr[5] = (hi_addr >> 8) & 0xff;
368 }
369 
dwxgmac2_set_eee_mode(struct mac_device_info * hw,bool en_tx_lpi_clockgating)370 static void dwxgmac2_set_eee_mode(struct mac_device_info *hw,
371 				  bool en_tx_lpi_clockgating)
372 {
373 	void __iomem *ioaddr = hw->pcsr;
374 	u32 value;
375 
376 	value = readl(ioaddr + XGMAC_LPI_CTRL);
377 
378 	value |= XGMAC_LPITXEN | XGMAC_LPITXA;
379 	if (en_tx_lpi_clockgating)
380 		value |= XGMAC_TXCGE;
381 
382 	writel(value, ioaddr + XGMAC_LPI_CTRL);
383 }
384 
dwxgmac2_reset_eee_mode(struct mac_device_info * hw)385 static void dwxgmac2_reset_eee_mode(struct mac_device_info *hw)
386 {
387 	void __iomem *ioaddr = hw->pcsr;
388 	u32 value;
389 
390 	value = readl(ioaddr + XGMAC_LPI_CTRL);
391 	value &= ~(XGMAC_LPITXEN | XGMAC_LPITXA | XGMAC_TXCGE);
392 	writel(value, ioaddr + XGMAC_LPI_CTRL);
393 }
394 
dwxgmac2_set_eee_pls(struct mac_device_info * hw,int link)395 static void dwxgmac2_set_eee_pls(struct mac_device_info *hw, int link)
396 {
397 	void __iomem *ioaddr = hw->pcsr;
398 	u32 value;
399 
400 	value = readl(ioaddr + XGMAC_LPI_CTRL);
401 	if (link)
402 		value |= XGMAC_PLS;
403 	else
404 		value &= ~XGMAC_PLS;
405 	writel(value, ioaddr + XGMAC_LPI_CTRL);
406 }
407 
dwxgmac2_set_eee_timer(struct mac_device_info * hw,int ls,int tw)408 static void dwxgmac2_set_eee_timer(struct mac_device_info *hw, int ls, int tw)
409 {
410 	void __iomem *ioaddr = hw->pcsr;
411 	u32 value;
412 
413 	value = (tw & 0xffff) | ((ls & 0x3ff) << 16);
414 	writel(value, ioaddr + XGMAC_LPI_TIMER_CTRL);
415 }
416 
dwxgmac2_set_mchash(void __iomem * ioaddr,u32 * mcfilterbits,int mcbitslog2)417 static void dwxgmac2_set_mchash(void __iomem *ioaddr, u32 *mcfilterbits,
418 				int mcbitslog2)
419 {
420 	int numhashregs, regs;
421 
422 	switch (mcbitslog2) {
423 	case 6:
424 		numhashregs = 2;
425 		break;
426 	case 7:
427 		numhashregs = 4;
428 		break;
429 	case 8:
430 		numhashregs = 8;
431 		break;
432 	default:
433 		return;
434 	}
435 
436 	for (regs = 0; regs < numhashregs; regs++)
437 		writel(mcfilterbits[regs], ioaddr + XGMAC_HASH_TABLE(regs));
438 }
439 
dwxgmac2_set_filter(struct mac_device_info * hw,struct net_device * dev)440 static void dwxgmac2_set_filter(struct mac_device_info *hw,
441 				struct net_device *dev)
442 {
443 	void __iomem *ioaddr = (void __iomem *)dev->base_addr;
444 	u32 value = readl(ioaddr + XGMAC_PACKET_FILTER);
445 	int mcbitslog2 = hw->mcast_bits_log2;
446 	u32 mc_filter[8];
447 	int i;
448 
449 	value &= ~(XGMAC_FILTER_PR | XGMAC_FILTER_HMC | XGMAC_FILTER_PM);
450 	value |= XGMAC_FILTER_HPF;
451 
452 	memset(mc_filter, 0, sizeof(mc_filter));
453 
454 	if (dev->flags & IFF_PROMISC) {
455 		value |= XGMAC_FILTER_PR;
456 		value |= XGMAC_FILTER_PCF;
457 	} else if ((dev->flags & IFF_ALLMULTI) ||
458 		   (netdev_mc_count(dev) > hw->multicast_filter_bins)) {
459 		value |= XGMAC_FILTER_PM;
460 
461 		for (i = 0; i < XGMAC_MAX_HASH_TABLE; i++)
462 			writel(~0x0, ioaddr + XGMAC_HASH_TABLE(i));
463 	} else if (!netdev_mc_empty(dev) && (dev->flags & IFF_MULTICAST)) {
464 		struct netdev_hw_addr *ha;
465 
466 		value |= XGMAC_FILTER_HMC;
467 
468 		netdev_for_each_mc_addr(ha, dev) {
469 			u32 nr = (bitrev32(~crc32_le(~0, ha->addr, 6)) >>
470 					(32 - mcbitslog2));
471 			mc_filter[nr >> 5] |= (1 << (nr & 0x1F));
472 		}
473 	}
474 
475 	dwxgmac2_set_mchash(ioaddr, mc_filter, mcbitslog2);
476 
477 	/* Handle multiple unicast addresses */
478 	if (netdev_uc_count(dev) > hw->unicast_filter_entries) {
479 		value |= XGMAC_FILTER_PR;
480 	} else {
481 		struct netdev_hw_addr *ha;
482 		int reg = 1;
483 
484 		netdev_for_each_uc_addr(ha, dev) {
485 			dwxgmac2_set_umac_addr(hw, ha->addr, reg);
486 			reg++;
487 		}
488 
489 		for ( ; reg < XGMAC_ADDR_MAX; reg++) {
490 			writel(0, ioaddr + XGMAC_ADDRx_HIGH(reg));
491 			writel(0, ioaddr + XGMAC_ADDRx_LOW(reg));
492 		}
493 	}
494 
495 	writel(value, ioaddr + XGMAC_PACKET_FILTER);
496 }
497 
dwxgmac2_set_mac_loopback(void __iomem * ioaddr,bool enable)498 static void dwxgmac2_set_mac_loopback(void __iomem *ioaddr, bool enable)
499 {
500 	u32 value = readl(ioaddr + XGMAC_RX_CONFIG);
501 
502 	if (enable)
503 		value |= XGMAC_CONFIG_LM;
504 	else
505 		value &= ~XGMAC_CONFIG_LM;
506 
507 	writel(value, ioaddr + XGMAC_RX_CONFIG);
508 }
509 
dwxgmac2_rss_write_reg(void __iomem * ioaddr,bool is_key,int idx,u32 val)510 static int dwxgmac2_rss_write_reg(void __iomem *ioaddr, bool is_key, int idx,
511 				  u32 val)
512 {
513 	u32 ctrl = 0;
514 
515 	writel(val, ioaddr + XGMAC_RSS_DATA);
516 	ctrl |= idx << XGMAC_RSSIA_SHIFT;
517 	ctrl |= is_key ? XGMAC_ADDRT : 0x0;
518 	ctrl |= XGMAC_OB;
519 	writel(ctrl, ioaddr + XGMAC_RSS_ADDR);
520 
521 	return readl_poll_timeout(ioaddr + XGMAC_RSS_ADDR, ctrl,
522 				  !(ctrl & XGMAC_OB), 100, 10000);
523 }
524 
dwxgmac2_rss_configure(struct mac_device_info * hw,struct stmmac_rss * cfg,u32 num_rxq)525 static int dwxgmac2_rss_configure(struct mac_device_info *hw,
526 				  struct stmmac_rss *cfg, u32 num_rxq)
527 {
528 	void __iomem *ioaddr = hw->pcsr;
529 	u32 value, *key;
530 	int i, ret;
531 
532 	value = readl(ioaddr + XGMAC_RSS_CTRL);
533 	if (!cfg || !cfg->enable) {
534 		value &= ~XGMAC_RSSE;
535 		writel(value, ioaddr + XGMAC_RSS_CTRL);
536 		return 0;
537 	}
538 
539 	key = (u32 *)cfg->key;
540 	for (i = 0; i < (ARRAY_SIZE(cfg->key) / sizeof(u32)); i++) {
541 		ret = dwxgmac2_rss_write_reg(ioaddr, true, i, key[i]);
542 		if (ret)
543 			return ret;
544 	}
545 
546 	for (i = 0; i < ARRAY_SIZE(cfg->table); i++) {
547 		ret = dwxgmac2_rss_write_reg(ioaddr, false, i, cfg->table[i]);
548 		if (ret)
549 			return ret;
550 	}
551 
552 	for (i = 0; i < num_rxq; i++)
553 		dwxgmac2_map_mtl_to_dma(hw, i, XGMAC_QDDMACH);
554 
555 	value |= XGMAC_UDP4TE | XGMAC_TCP4TE | XGMAC_IP2TE | XGMAC_RSSE;
556 	writel(value, ioaddr + XGMAC_RSS_CTRL);
557 	return 0;
558 }
559 
dwxgmac2_update_vlan_hash(struct mac_device_info * hw,u32 hash,__le16 perfect_match,bool is_double)560 static void dwxgmac2_update_vlan_hash(struct mac_device_info *hw, u32 hash,
561 				      __le16 perfect_match, bool is_double)
562 {
563 	void __iomem *ioaddr = hw->pcsr;
564 
565 	writel(hash, ioaddr + XGMAC_VLAN_HASH_TABLE);
566 
567 	if (hash) {
568 		u32 value = readl(ioaddr + XGMAC_PACKET_FILTER);
569 
570 		value |= XGMAC_FILTER_VTFE;
571 
572 		writel(value, ioaddr + XGMAC_PACKET_FILTER);
573 
574 		value = readl(ioaddr + XGMAC_VLAN_TAG);
575 
576 		value |= XGMAC_VLAN_VTHM | XGMAC_VLAN_ETV;
577 		if (is_double) {
578 			value |= XGMAC_VLAN_EDVLP;
579 			value |= XGMAC_VLAN_ESVL;
580 			value |= XGMAC_VLAN_DOVLTC;
581 		} else {
582 			value &= ~XGMAC_VLAN_EDVLP;
583 			value &= ~XGMAC_VLAN_ESVL;
584 			value &= ~XGMAC_VLAN_DOVLTC;
585 		}
586 
587 		value &= ~XGMAC_VLAN_VID;
588 		writel(value, ioaddr + XGMAC_VLAN_TAG);
589 	} else if (perfect_match) {
590 		u32 value = readl(ioaddr + XGMAC_PACKET_FILTER);
591 
592 		value |= XGMAC_FILTER_VTFE;
593 
594 		writel(value, ioaddr + XGMAC_PACKET_FILTER);
595 
596 		value = readl(ioaddr + XGMAC_VLAN_TAG);
597 
598 		value &= ~XGMAC_VLAN_VTHM;
599 		value |= XGMAC_VLAN_ETV;
600 		if (is_double) {
601 			value |= XGMAC_VLAN_EDVLP;
602 			value |= XGMAC_VLAN_ESVL;
603 			value |= XGMAC_VLAN_DOVLTC;
604 		} else {
605 			value &= ~XGMAC_VLAN_EDVLP;
606 			value &= ~XGMAC_VLAN_ESVL;
607 			value &= ~XGMAC_VLAN_DOVLTC;
608 		}
609 
610 		value &= ~XGMAC_VLAN_VID;
611 		writel(value | perfect_match, ioaddr + XGMAC_VLAN_TAG);
612 	} else {
613 		u32 value = readl(ioaddr + XGMAC_PACKET_FILTER);
614 
615 		value &= ~XGMAC_FILTER_VTFE;
616 
617 		writel(value, ioaddr + XGMAC_PACKET_FILTER);
618 
619 		value = readl(ioaddr + XGMAC_VLAN_TAG);
620 
621 		value &= ~(XGMAC_VLAN_VTHM | XGMAC_VLAN_ETV);
622 		value &= ~(XGMAC_VLAN_EDVLP | XGMAC_VLAN_ESVL);
623 		value &= ~XGMAC_VLAN_DOVLTC;
624 		value &= ~XGMAC_VLAN_VID;
625 
626 		writel(value, ioaddr + XGMAC_VLAN_TAG);
627 	}
628 }
629 
630 struct dwxgmac3_error_desc {
631 	bool valid;
632 	const char *desc;
633 	const char *detailed_desc;
634 };
635 
636 #define STAT_OFF(field)		offsetof(struct stmmac_safety_stats, field)
637 
dwxgmac3_log_error(struct net_device * ndev,u32 value,bool corr,const char * module_name,const struct dwxgmac3_error_desc * desc,unsigned long field_offset,struct stmmac_safety_stats * stats)638 static void dwxgmac3_log_error(struct net_device *ndev, u32 value, bool corr,
639 			       const char *module_name,
640 			       const struct dwxgmac3_error_desc *desc,
641 			       unsigned long field_offset,
642 			       struct stmmac_safety_stats *stats)
643 {
644 	unsigned long loc, mask;
645 	u8 *bptr = (u8 *)stats;
646 	unsigned long *ptr;
647 
648 	ptr = (unsigned long *)(bptr + field_offset);
649 
650 	mask = value;
651 	for_each_set_bit(loc, &mask, 32) {
652 		netdev_err(ndev, "Found %s error in %s: '%s: %s'\n", corr ?
653 				"correctable" : "uncorrectable", module_name,
654 				desc[loc].desc, desc[loc].detailed_desc);
655 
656 		/* Update counters */
657 		ptr[loc]++;
658 	}
659 }
660 
661 static const struct dwxgmac3_error_desc dwxgmac3_mac_errors[32]= {
662 	{ true, "ATPES", "Application Transmit Interface Parity Check Error" },
663 	{ true, "DPES", "Descriptor Cache Data Path Parity Check Error" },
664 	{ true, "TPES", "TSO Data Path Parity Check Error" },
665 	{ true, "TSOPES", "TSO Header Data Path Parity Check Error" },
666 	{ true, "MTPES", "MTL Data Path Parity Check Error" },
667 	{ true, "MTSPES", "MTL TX Status Data Path Parity Check Error" },
668 	{ true, "MTBUPES", "MAC TBU Data Path Parity Check Error" },
669 	{ true, "MTFCPES", "MAC TFC Data Path Parity Check Error" },
670 	{ true, "ARPES", "Application Receive Interface Data Path Parity Check Error" },
671 	{ true, "MRWCPES", "MTL RWC Data Path Parity Check Error" },
672 	{ true, "MRRCPES", "MTL RCC Data Path Parity Check Error" },
673 	{ true, "CWPES", "CSR Write Data Path Parity Check Error" },
674 	{ true, "ASRPES", "AXI Slave Read Data Path Parity Check Error" },
675 	{ true, "TTES", "TX FSM Timeout Error" },
676 	{ true, "RTES", "RX FSM Timeout Error" },
677 	{ true, "CTES", "CSR FSM Timeout Error" },
678 	{ true, "ATES", "APP FSM Timeout Error" },
679 	{ true, "PTES", "PTP FSM Timeout Error" },
680 	{ false, "UNKNOWN", "Unknown Error" }, /* 18 */
681 	{ false, "UNKNOWN", "Unknown Error" }, /* 19 */
682 	{ false, "UNKNOWN", "Unknown Error" }, /* 20 */
683 	{ true, "MSTTES", "Master Read/Write Timeout Error" },
684 	{ true, "SLVTES", "Slave Read/Write Timeout Error" },
685 	{ true, "ATITES", "Application Timeout on ATI Interface Error" },
686 	{ true, "ARITES", "Application Timeout on ARI Interface Error" },
687 	{ true, "FSMPES", "FSM State Parity Error" },
688 	{ false, "UNKNOWN", "Unknown Error" }, /* 26 */
689 	{ false, "UNKNOWN", "Unknown Error" }, /* 27 */
690 	{ false, "UNKNOWN", "Unknown Error" }, /* 28 */
691 	{ false, "UNKNOWN", "Unknown Error" }, /* 29 */
692 	{ false, "UNKNOWN", "Unknown Error" }, /* 30 */
693 	{ true, "CPI", "Control Register Parity Check Error" },
694 };
695 
dwxgmac3_handle_mac_err(struct net_device * ndev,void __iomem * ioaddr,bool correctable,struct stmmac_safety_stats * stats)696 static void dwxgmac3_handle_mac_err(struct net_device *ndev,
697 				    void __iomem *ioaddr, bool correctable,
698 				    struct stmmac_safety_stats *stats)
699 {
700 	u32 value;
701 
702 	value = readl(ioaddr + XGMAC_MAC_DPP_FSM_INT_STATUS);
703 	writel(value, ioaddr + XGMAC_MAC_DPP_FSM_INT_STATUS);
704 
705 	dwxgmac3_log_error(ndev, value, correctable, "MAC",
706 			   dwxgmac3_mac_errors, STAT_OFF(mac_errors), stats);
707 }
708 
709 static const struct dwxgmac3_error_desc dwxgmac3_mtl_errors[32]= {
710 	{ true, "TXCES", "MTL TX Memory Error" },
711 	{ true, "TXAMS", "MTL TX Memory Address Mismatch Error" },
712 	{ true, "TXUES", "MTL TX Memory Error" },
713 	{ false, "UNKNOWN", "Unknown Error" }, /* 3 */
714 	{ true, "RXCES", "MTL RX Memory Error" },
715 	{ true, "RXAMS", "MTL RX Memory Address Mismatch Error" },
716 	{ true, "RXUES", "MTL RX Memory Error" },
717 	{ false, "UNKNOWN", "Unknown Error" }, /* 7 */
718 	{ true, "ECES", "MTL EST Memory Error" },
719 	{ true, "EAMS", "MTL EST Memory Address Mismatch Error" },
720 	{ true, "EUES", "MTL EST Memory Error" },
721 	{ false, "UNKNOWN", "Unknown Error" }, /* 11 */
722 	{ true, "RPCES", "MTL RX Parser Memory Error" },
723 	{ true, "RPAMS", "MTL RX Parser Memory Address Mismatch Error" },
724 	{ true, "RPUES", "MTL RX Parser Memory Error" },
725 	{ false, "UNKNOWN", "Unknown Error" }, /* 15 */
726 	{ false, "UNKNOWN", "Unknown Error" }, /* 16 */
727 	{ false, "UNKNOWN", "Unknown Error" }, /* 17 */
728 	{ false, "UNKNOWN", "Unknown Error" }, /* 18 */
729 	{ false, "UNKNOWN", "Unknown Error" }, /* 19 */
730 	{ false, "UNKNOWN", "Unknown Error" }, /* 20 */
731 	{ false, "UNKNOWN", "Unknown Error" }, /* 21 */
732 	{ false, "UNKNOWN", "Unknown Error" }, /* 22 */
733 	{ false, "UNKNOWN", "Unknown Error" }, /* 23 */
734 	{ false, "UNKNOWN", "Unknown Error" }, /* 24 */
735 	{ false, "UNKNOWN", "Unknown Error" }, /* 25 */
736 	{ false, "UNKNOWN", "Unknown Error" }, /* 26 */
737 	{ false, "UNKNOWN", "Unknown Error" }, /* 27 */
738 	{ false, "UNKNOWN", "Unknown Error" }, /* 28 */
739 	{ false, "UNKNOWN", "Unknown Error" }, /* 29 */
740 	{ false, "UNKNOWN", "Unknown Error" }, /* 30 */
741 	{ false, "UNKNOWN", "Unknown Error" }, /* 31 */
742 };
743 
dwxgmac3_handle_mtl_err(struct net_device * ndev,void __iomem * ioaddr,bool correctable,struct stmmac_safety_stats * stats)744 static void dwxgmac3_handle_mtl_err(struct net_device *ndev,
745 				    void __iomem *ioaddr, bool correctable,
746 				    struct stmmac_safety_stats *stats)
747 {
748 	u32 value;
749 
750 	value = readl(ioaddr + XGMAC_MTL_ECC_INT_STATUS);
751 	writel(value, ioaddr + XGMAC_MTL_ECC_INT_STATUS);
752 
753 	dwxgmac3_log_error(ndev, value, correctable, "MTL",
754 			   dwxgmac3_mtl_errors, STAT_OFF(mtl_errors), stats);
755 }
756 
757 static const struct dwxgmac3_error_desc dwxgmac3_dma_errors[32]= {
758 	{ true, "TCES", "DMA TSO Memory Error" },
759 	{ true, "TAMS", "DMA TSO Memory Address Mismatch Error" },
760 	{ true, "TUES", "DMA TSO Memory Error" },
761 	{ false, "UNKNOWN", "Unknown Error" }, /* 3 */
762 	{ true, "DCES", "DMA DCACHE Memory Error" },
763 	{ true, "DAMS", "DMA DCACHE Address Mismatch Error" },
764 	{ true, "DUES", "DMA DCACHE Memory Error" },
765 	{ false, "UNKNOWN", "Unknown Error" }, /* 7 */
766 	{ false, "UNKNOWN", "Unknown Error" }, /* 8 */
767 	{ false, "UNKNOWN", "Unknown Error" }, /* 9 */
768 	{ false, "UNKNOWN", "Unknown Error" }, /* 10 */
769 	{ false, "UNKNOWN", "Unknown Error" }, /* 11 */
770 	{ false, "UNKNOWN", "Unknown Error" }, /* 12 */
771 	{ false, "UNKNOWN", "Unknown Error" }, /* 13 */
772 	{ false, "UNKNOWN", "Unknown Error" }, /* 14 */
773 	{ false, "UNKNOWN", "Unknown Error" }, /* 15 */
774 	{ false, "UNKNOWN", "Unknown Error" }, /* 16 */
775 	{ false, "UNKNOWN", "Unknown Error" }, /* 17 */
776 	{ false, "UNKNOWN", "Unknown Error" }, /* 18 */
777 	{ false, "UNKNOWN", "Unknown Error" }, /* 19 */
778 	{ false, "UNKNOWN", "Unknown Error" }, /* 20 */
779 	{ false, "UNKNOWN", "Unknown Error" }, /* 21 */
780 	{ false, "UNKNOWN", "Unknown Error" }, /* 22 */
781 	{ false, "UNKNOWN", "Unknown Error" }, /* 23 */
782 	{ false, "UNKNOWN", "Unknown Error" }, /* 24 */
783 	{ false, "UNKNOWN", "Unknown Error" }, /* 25 */
784 	{ false, "UNKNOWN", "Unknown Error" }, /* 26 */
785 	{ false, "UNKNOWN", "Unknown Error" }, /* 27 */
786 	{ false, "UNKNOWN", "Unknown Error" }, /* 28 */
787 	{ false, "UNKNOWN", "Unknown Error" }, /* 29 */
788 	{ false, "UNKNOWN", "Unknown Error" }, /* 30 */
789 	{ false, "UNKNOWN", "Unknown Error" }, /* 31 */
790 };
791 
792 #define DPP_RX_ERR "Read Rx Descriptor Parity checker Error"
793 #define DPP_TX_ERR "Read Tx Descriptor Parity checker Error"
794 
795 static const struct dwxgmac3_error_desc dwxgmac3_dma_dpp_errors[32] = {
796 	{ true, "TDPES0", DPP_TX_ERR },
797 	{ true, "TDPES1", DPP_TX_ERR },
798 	{ true, "TDPES2", DPP_TX_ERR },
799 	{ true, "TDPES3", DPP_TX_ERR },
800 	{ true, "TDPES4", DPP_TX_ERR },
801 	{ true, "TDPES5", DPP_TX_ERR },
802 	{ true, "TDPES6", DPP_TX_ERR },
803 	{ true, "TDPES7", DPP_TX_ERR },
804 	{ true, "TDPES8", DPP_TX_ERR },
805 	{ true, "TDPES9", DPP_TX_ERR },
806 	{ true, "TDPES10", DPP_TX_ERR },
807 	{ true, "TDPES11", DPP_TX_ERR },
808 	{ true, "TDPES12", DPP_TX_ERR },
809 	{ true, "TDPES13", DPP_TX_ERR },
810 	{ true, "TDPES14", DPP_TX_ERR },
811 	{ true, "TDPES15", DPP_TX_ERR },
812 	{ true, "RDPES0", DPP_RX_ERR },
813 	{ true, "RDPES1", DPP_RX_ERR },
814 	{ true, "RDPES2", DPP_RX_ERR },
815 	{ true, "RDPES3", DPP_RX_ERR },
816 	{ true, "RDPES4", DPP_RX_ERR },
817 	{ true, "RDPES5", DPP_RX_ERR },
818 	{ true, "RDPES6", DPP_RX_ERR },
819 	{ true, "RDPES7", DPP_RX_ERR },
820 	{ true, "RDPES8", DPP_RX_ERR },
821 	{ true, "RDPES9", DPP_RX_ERR },
822 	{ true, "RDPES10", DPP_RX_ERR },
823 	{ true, "RDPES11", DPP_RX_ERR },
824 	{ true, "RDPES12", DPP_RX_ERR },
825 	{ true, "RDPES13", DPP_RX_ERR },
826 	{ true, "RDPES14", DPP_RX_ERR },
827 	{ true, "RDPES15", DPP_RX_ERR },
828 };
829 
dwxgmac3_handle_dma_err(struct net_device * ndev,void __iomem * ioaddr,bool correctable,struct stmmac_safety_stats * stats)830 static void dwxgmac3_handle_dma_err(struct net_device *ndev,
831 				    void __iomem *ioaddr, bool correctable,
832 				    struct stmmac_safety_stats *stats)
833 {
834 	u32 value;
835 
836 	value = readl(ioaddr + XGMAC_DMA_ECC_INT_STATUS);
837 	writel(value, ioaddr + XGMAC_DMA_ECC_INT_STATUS);
838 
839 	dwxgmac3_log_error(ndev, value, correctable, "DMA",
840 			   dwxgmac3_dma_errors, STAT_OFF(dma_errors), stats);
841 
842 	value = readl(ioaddr + XGMAC_DMA_DPP_INT_STATUS);
843 	writel(value, ioaddr + XGMAC_DMA_DPP_INT_STATUS);
844 
845 	dwxgmac3_log_error(ndev, value, false, "DMA_DPP",
846 			   dwxgmac3_dma_dpp_errors,
847 			   STAT_OFF(dma_dpp_errors), stats);
848 }
849 
850 static int
dwxgmac3_safety_feat_config(void __iomem * ioaddr,unsigned int asp,struct stmmac_safety_feature_cfg * safety_cfg)851 dwxgmac3_safety_feat_config(void __iomem *ioaddr, unsigned int asp,
852 			    struct stmmac_safety_feature_cfg *safety_cfg)
853 {
854 	u32 value;
855 
856 	if (!asp)
857 		return -EINVAL;
858 
859 	/* 1. Enable Safety Features */
860 	writel(0x0, ioaddr + XGMAC_MTL_ECC_CONTROL);
861 
862 	/* 2. Enable MTL Safety Interrupts */
863 	value = readl(ioaddr + XGMAC_MTL_ECC_INT_ENABLE);
864 	value |= XGMAC_RPCEIE; /* RX Parser Memory Correctable Error */
865 	value |= XGMAC_ECEIE; /* EST Memory Correctable Error */
866 	value |= XGMAC_RXCEIE; /* RX Memory Correctable Error */
867 	value |= XGMAC_TXCEIE; /* TX Memory Correctable Error */
868 	writel(value, ioaddr + XGMAC_MTL_ECC_INT_ENABLE);
869 
870 	/* 3. Enable DMA Safety Interrupts */
871 	value = readl(ioaddr + XGMAC_DMA_ECC_INT_ENABLE);
872 	value |= XGMAC_DCEIE; /* Descriptor Cache Memory Correctable Error */
873 	value |= XGMAC_TCEIE; /* TSO Memory Correctable Error */
874 	writel(value, ioaddr + XGMAC_DMA_ECC_INT_ENABLE);
875 
876 	/* Only ECC Protection for External Memory feature is selected */
877 	if (asp <= 0x1)
878 		return 0;
879 
880 	/* 4. Enable Parity and Timeout for FSM */
881 	value = readl(ioaddr + XGMAC_MAC_FSM_CONTROL);
882 	value |= XGMAC_PRTYEN; /* FSM Parity Feature */
883 	value |= XGMAC_TMOUTEN; /* FSM Timeout Feature */
884 	writel(value, ioaddr + XGMAC_MAC_FSM_CONTROL);
885 
886 	/* 5. Enable Data Path Parity Protection */
887 	value = readl(ioaddr + XGMAC_MTL_DPP_CONTROL);
888 	/* already enabled by default, explicit enable it again */
889 	value &= ~XGMAC_DPP_DISABLE;
890 	writel(value, ioaddr + XGMAC_MTL_DPP_CONTROL);
891 
892 	return 0;
893 }
894 
dwxgmac3_safety_feat_irq_status(struct net_device * ndev,void __iomem * ioaddr,unsigned int asp,struct stmmac_safety_stats * stats)895 static int dwxgmac3_safety_feat_irq_status(struct net_device *ndev,
896 					   void __iomem *ioaddr,
897 					   unsigned int asp,
898 					   struct stmmac_safety_stats *stats)
899 {
900 	bool err, corr;
901 	u32 mtl, dma;
902 	int ret = 0;
903 
904 	if (!asp)
905 		return -EINVAL;
906 
907 	mtl = readl(ioaddr + XGMAC_MTL_SAFETY_INT_STATUS);
908 	dma = readl(ioaddr + XGMAC_DMA_SAFETY_INT_STATUS);
909 
910 	err = (mtl & XGMAC_MCSIS) || (dma & XGMAC_MCSIS);
911 	corr = false;
912 	if (err) {
913 		dwxgmac3_handle_mac_err(ndev, ioaddr, corr, stats);
914 		ret |= !corr;
915 	}
916 
917 	err = (mtl & (XGMAC_MEUIS | XGMAC_MECIS)) ||
918 	      (dma & (XGMAC_MSUIS | XGMAC_MSCIS));
919 	corr = (mtl & XGMAC_MECIS) || (dma & XGMAC_MSCIS);
920 	if (err) {
921 		dwxgmac3_handle_mtl_err(ndev, ioaddr, corr, stats);
922 		ret |= !corr;
923 	}
924 
925 	/* DMA_DPP_Interrupt_Status is indicated by MCSIS bit in
926 	 * DMA_Safety_Interrupt_Status, so we handle DMA Data Path
927 	 * Parity Errors here
928 	 */
929 	err = dma & (XGMAC_DEUIS | XGMAC_DECIS | XGMAC_MCSIS);
930 	corr = dma & XGMAC_DECIS;
931 	if (err) {
932 		dwxgmac3_handle_dma_err(ndev, ioaddr, corr, stats);
933 		ret |= !corr;
934 	}
935 
936 	return ret;
937 }
938 
939 static const struct dwxgmac3_error {
940 	const struct dwxgmac3_error_desc *desc;
941 } dwxgmac3_all_errors[] = {
942 	{ dwxgmac3_mac_errors },
943 	{ dwxgmac3_mtl_errors },
944 	{ dwxgmac3_dma_errors },
945 	{ dwxgmac3_dma_dpp_errors },
946 };
947 
dwxgmac3_safety_feat_dump(struct stmmac_safety_stats * stats,int index,unsigned long * count,const char ** desc)948 static int dwxgmac3_safety_feat_dump(struct stmmac_safety_stats *stats,
949 				     int index, unsigned long *count,
950 				     const char **desc)
951 {
952 	int module = index / 32, offset = index % 32;
953 	unsigned long *ptr = (unsigned long *)stats;
954 
955 	if (module >= ARRAY_SIZE(dwxgmac3_all_errors))
956 		return -EINVAL;
957 	if (!dwxgmac3_all_errors[module].desc[offset].valid)
958 		return -EINVAL;
959 	if (count)
960 		*count = *(ptr + index);
961 	if (desc)
962 		*desc = dwxgmac3_all_errors[module].desc[offset].desc;
963 	return 0;
964 }
965 
dwxgmac3_rxp_disable(void __iomem * ioaddr)966 static int dwxgmac3_rxp_disable(void __iomem *ioaddr)
967 {
968 	u32 val = readl(ioaddr + XGMAC_MTL_OPMODE);
969 
970 	val &= ~XGMAC_FRPE;
971 	writel(val, ioaddr + XGMAC_MTL_OPMODE);
972 
973 	return 0;
974 }
975 
dwxgmac3_rxp_enable(void __iomem * ioaddr)976 static void dwxgmac3_rxp_enable(void __iomem *ioaddr)
977 {
978 	u32 val;
979 
980 	val = readl(ioaddr + XGMAC_MTL_OPMODE);
981 	val |= XGMAC_FRPE;
982 	writel(val, ioaddr + XGMAC_MTL_OPMODE);
983 }
984 
dwxgmac3_rxp_update_single_entry(void __iomem * ioaddr,struct stmmac_tc_entry * entry,int pos)985 static int dwxgmac3_rxp_update_single_entry(void __iomem *ioaddr,
986 					    struct stmmac_tc_entry *entry,
987 					    int pos)
988 {
989 	int ret, i;
990 
991 	for (i = 0; i < (sizeof(entry->val) / sizeof(u32)); i++) {
992 		int real_pos = pos * (sizeof(entry->val) / sizeof(u32)) + i;
993 		u32 val;
994 
995 		/* Wait for ready */
996 		ret = readl_poll_timeout(ioaddr + XGMAC_MTL_RXP_IACC_CTRL_ST,
997 					 val, !(val & XGMAC_STARTBUSY), 1, 10000);
998 		if (ret)
999 			return ret;
1000 
1001 		/* Write data */
1002 		val = *((u32 *)&entry->val + i);
1003 		writel(val, ioaddr + XGMAC_MTL_RXP_IACC_DATA);
1004 
1005 		/* Write pos */
1006 		val = real_pos & XGMAC_ADDR;
1007 		writel(val, ioaddr + XGMAC_MTL_RXP_IACC_CTRL_ST);
1008 
1009 		/* Write OP */
1010 		val |= XGMAC_WRRDN;
1011 		writel(val, ioaddr + XGMAC_MTL_RXP_IACC_CTRL_ST);
1012 
1013 		/* Start Write */
1014 		val |= XGMAC_STARTBUSY;
1015 		writel(val, ioaddr + XGMAC_MTL_RXP_IACC_CTRL_ST);
1016 
1017 		/* Wait for done */
1018 		ret = readl_poll_timeout(ioaddr + XGMAC_MTL_RXP_IACC_CTRL_ST,
1019 					 val, !(val & XGMAC_STARTBUSY), 1, 10000);
1020 		if (ret)
1021 			return ret;
1022 	}
1023 
1024 	return 0;
1025 }
1026 
1027 static struct stmmac_tc_entry *
dwxgmac3_rxp_get_next_entry(struct stmmac_tc_entry * entries,unsigned int count,u32 curr_prio)1028 dwxgmac3_rxp_get_next_entry(struct stmmac_tc_entry *entries,
1029 			    unsigned int count, u32 curr_prio)
1030 {
1031 	struct stmmac_tc_entry *entry;
1032 	u32 min_prio = ~0x0;
1033 	int i, min_prio_idx;
1034 	bool found = false;
1035 
1036 	for (i = count - 1; i >= 0; i--) {
1037 		entry = &entries[i];
1038 
1039 		/* Do not update unused entries */
1040 		if (!entry->in_use)
1041 			continue;
1042 		/* Do not update already updated entries (i.e. fragments) */
1043 		if (entry->in_hw)
1044 			continue;
1045 		/* Let last entry be updated last */
1046 		if (entry->is_last)
1047 			continue;
1048 		/* Do not return fragments */
1049 		if (entry->is_frag)
1050 			continue;
1051 		/* Check if we already checked this prio */
1052 		if (entry->prio < curr_prio)
1053 			continue;
1054 		/* Check if this is the minimum prio */
1055 		if (entry->prio < min_prio) {
1056 			min_prio = entry->prio;
1057 			min_prio_idx = i;
1058 			found = true;
1059 		}
1060 	}
1061 
1062 	if (found)
1063 		return &entries[min_prio_idx];
1064 	return NULL;
1065 }
1066 
dwxgmac3_rxp_config(void __iomem * ioaddr,struct stmmac_tc_entry * entries,unsigned int count)1067 static int dwxgmac3_rxp_config(void __iomem *ioaddr,
1068 			       struct stmmac_tc_entry *entries,
1069 			       unsigned int count)
1070 {
1071 	struct stmmac_tc_entry *entry, *frag;
1072 	int i, ret, nve = 0;
1073 	u32 curr_prio = 0;
1074 	u32 old_val, val;
1075 
1076 	/* Force disable RX */
1077 	old_val = readl(ioaddr + XGMAC_RX_CONFIG);
1078 	val = old_val & ~XGMAC_CONFIG_RE;
1079 	writel(val, ioaddr + XGMAC_RX_CONFIG);
1080 
1081 	/* Disable RX Parser */
1082 	ret = dwxgmac3_rxp_disable(ioaddr);
1083 	if (ret)
1084 		goto re_enable;
1085 
1086 	/* Set all entries as NOT in HW */
1087 	for (i = 0; i < count; i++) {
1088 		entry = &entries[i];
1089 		entry->in_hw = false;
1090 	}
1091 
1092 	/* Update entries by reverse order */
1093 	while (1) {
1094 		entry = dwxgmac3_rxp_get_next_entry(entries, count, curr_prio);
1095 		if (!entry)
1096 			break;
1097 
1098 		curr_prio = entry->prio;
1099 		frag = entry->frag_ptr;
1100 
1101 		/* Set special fragment requirements */
1102 		if (frag) {
1103 			entry->val.af = 0;
1104 			entry->val.rf = 0;
1105 			entry->val.nc = 1;
1106 			entry->val.ok_index = nve + 2;
1107 		}
1108 
1109 		ret = dwxgmac3_rxp_update_single_entry(ioaddr, entry, nve);
1110 		if (ret)
1111 			goto re_enable;
1112 
1113 		entry->table_pos = nve++;
1114 		entry->in_hw = true;
1115 
1116 		if (frag && !frag->in_hw) {
1117 			ret = dwxgmac3_rxp_update_single_entry(ioaddr, frag, nve);
1118 			if (ret)
1119 				goto re_enable;
1120 			frag->table_pos = nve++;
1121 			frag->in_hw = true;
1122 		}
1123 	}
1124 
1125 	if (!nve)
1126 		goto re_enable;
1127 
1128 	/* Update all pass entry */
1129 	for (i = 0; i < count; i++) {
1130 		entry = &entries[i];
1131 		if (!entry->is_last)
1132 			continue;
1133 
1134 		ret = dwxgmac3_rxp_update_single_entry(ioaddr, entry, nve);
1135 		if (ret)
1136 			goto re_enable;
1137 
1138 		entry->table_pos = nve++;
1139 	}
1140 
1141 	/* Assume n. of parsable entries == n. of valid entries */
1142 	val = (nve << 16) & XGMAC_NPE;
1143 	val |= nve & XGMAC_NVE;
1144 	writel(val, ioaddr + XGMAC_MTL_RXP_CONTROL_STATUS);
1145 
1146 	/* Enable RX Parser */
1147 	dwxgmac3_rxp_enable(ioaddr);
1148 
1149 re_enable:
1150 	/* Re-enable RX */
1151 	writel(old_val, ioaddr + XGMAC_RX_CONFIG);
1152 	return ret;
1153 }
1154 
dwxgmac2_get_mac_tx_timestamp(struct mac_device_info * hw,u64 * ts)1155 static int dwxgmac2_get_mac_tx_timestamp(struct mac_device_info *hw, u64 *ts)
1156 {
1157 	void __iomem *ioaddr = hw->pcsr;
1158 	u32 value;
1159 
1160 	if (readl_poll_timeout_atomic(ioaddr + XGMAC_TIMESTAMP_STATUS,
1161 				      value, value & XGMAC_TXTSC, 100, 10000))
1162 		return -EBUSY;
1163 
1164 	*ts = readl(ioaddr + XGMAC_TXTIMESTAMP_NSEC) & XGMAC_TXTSSTSLO;
1165 	*ts += readl(ioaddr + XGMAC_TXTIMESTAMP_SEC) * 1000000000ULL;
1166 	return 0;
1167 }
1168 
dwxgmac2_flex_pps_config(void __iomem * ioaddr,int index,struct stmmac_pps_cfg * cfg,bool enable,u32 sub_second_inc,u32 systime_flags)1169 static int dwxgmac2_flex_pps_config(void __iomem *ioaddr, int index,
1170 				    struct stmmac_pps_cfg *cfg, bool enable,
1171 				    u32 sub_second_inc, u32 systime_flags)
1172 {
1173 	u32 tnsec = readl(ioaddr + XGMAC_PPSx_TARGET_TIME_NSEC(index));
1174 	u32 val = readl(ioaddr + XGMAC_PPS_CONTROL);
1175 	u64 period;
1176 
1177 	if (!cfg->available)
1178 		return -EINVAL;
1179 	if (tnsec & XGMAC_TRGTBUSY0)
1180 		return -EBUSY;
1181 	if (!sub_second_inc || !systime_flags)
1182 		return -EINVAL;
1183 
1184 	val &= ~XGMAC_PPSx_MASK(index);
1185 
1186 	if (!enable) {
1187 		val |= XGMAC_PPSCMDx(index, XGMAC_PPSCMD_STOP);
1188 		writel(val, ioaddr + XGMAC_PPS_CONTROL);
1189 		return 0;
1190 	}
1191 
1192 	val |= XGMAC_PPSCMDx(index, XGMAC_PPSCMD_START);
1193 	val |= XGMAC_TRGTMODSELx(index, XGMAC_PPSCMD_START);
1194 
1195 	/* XGMAC Core has 4 PPS outputs at most.
1196 	 *
1197 	 * Prior XGMAC Core 3.20, Fixed mode or Flexible mode are selectable for
1198 	 * PPS0 only via PPSEN0. PPS{1,2,3} are in Flexible mode by default,
1199 	 * and can not be switched to Fixed mode, since PPSEN{1,2,3} are
1200 	 * read-only reserved to 0.
1201 	 * But we always set PPSEN{1,2,3} do not make things worse ;-)
1202 	 *
1203 	 * From XGMAC Core 3.20 and later, PPSEN{0,1,2,3} are writable and must
1204 	 * be set, or the PPS outputs stay in Fixed PPS mode by default.
1205 	 */
1206 	val |= XGMAC_PPSENx(index);
1207 
1208 	writel(cfg->start.tv_sec, ioaddr + XGMAC_PPSx_TARGET_TIME_SEC(index));
1209 
1210 	if (!(systime_flags & PTP_TCR_TSCTRLSSR))
1211 		cfg->start.tv_nsec = (cfg->start.tv_nsec * 1000) / 465;
1212 	writel(cfg->start.tv_nsec, ioaddr + XGMAC_PPSx_TARGET_TIME_NSEC(index));
1213 
1214 	period = cfg->period.tv_sec * 1000000000;
1215 	period += cfg->period.tv_nsec;
1216 
1217 	do_div(period, sub_second_inc);
1218 
1219 	if (period <= 1)
1220 		return -EINVAL;
1221 
1222 	writel(period - 1, ioaddr + XGMAC_PPSx_INTERVAL(index));
1223 
1224 	period >>= 1;
1225 	if (period <= 1)
1226 		return -EINVAL;
1227 
1228 	writel(period - 1, ioaddr + XGMAC_PPSx_WIDTH(index));
1229 
1230 	/* Finally, activate it */
1231 	writel(val, ioaddr + XGMAC_PPS_CONTROL);
1232 	return 0;
1233 }
1234 
dwxgmac2_sarc_configure(void __iomem * ioaddr,int val)1235 static void dwxgmac2_sarc_configure(void __iomem *ioaddr, int val)
1236 {
1237 	u32 value = readl(ioaddr + XGMAC_TX_CONFIG);
1238 
1239 	value &= ~XGMAC_CONFIG_SARC;
1240 	value |= val << XGMAC_CONFIG_SARC_SHIFT;
1241 
1242 	writel(value, ioaddr + XGMAC_TX_CONFIG);
1243 }
1244 
dwxgmac2_enable_vlan(struct mac_device_info * hw,u32 type)1245 static void dwxgmac2_enable_vlan(struct mac_device_info *hw, u32 type)
1246 {
1247 	void __iomem *ioaddr = hw->pcsr;
1248 	u32 value;
1249 
1250 	value = readl(ioaddr + XGMAC_VLAN_INCL);
1251 	value |= XGMAC_VLAN_VLTI;
1252 	value |= XGMAC_VLAN_CSVL; /* Only use SVLAN */
1253 	value &= ~XGMAC_VLAN_VLC;
1254 	value |= (type << XGMAC_VLAN_VLC_SHIFT) & XGMAC_VLAN_VLC;
1255 	writel(value, ioaddr + XGMAC_VLAN_INCL);
1256 }
1257 
dwxgmac2_filter_wait(struct mac_device_info * hw)1258 static int dwxgmac2_filter_wait(struct mac_device_info *hw)
1259 {
1260 	void __iomem *ioaddr = hw->pcsr;
1261 	u32 value;
1262 
1263 	if (readl_poll_timeout(ioaddr + XGMAC_L3L4_ADDR_CTRL, value,
1264 			       !(value & XGMAC_XB), 100, 10000))
1265 		return -EBUSY;
1266 	return 0;
1267 }
1268 
dwxgmac2_filter_read(struct mac_device_info * hw,u32 filter_no,u8 reg,u32 * data)1269 static int dwxgmac2_filter_read(struct mac_device_info *hw, u32 filter_no,
1270 				u8 reg, u32 *data)
1271 {
1272 	void __iomem *ioaddr = hw->pcsr;
1273 	u32 value;
1274 	int ret;
1275 
1276 	ret = dwxgmac2_filter_wait(hw);
1277 	if (ret)
1278 		return ret;
1279 
1280 	value = ((filter_no << XGMAC_IDDR_FNUM) | reg) << XGMAC_IDDR_SHIFT;
1281 	value |= XGMAC_TT | XGMAC_XB;
1282 	writel(value, ioaddr + XGMAC_L3L4_ADDR_CTRL);
1283 
1284 	ret = dwxgmac2_filter_wait(hw);
1285 	if (ret)
1286 		return ret;
1287 
1288 	*data = readl(ioaddr + XGMAC_L3L4_DATA);
1289 	return 0;
1290 }
1291 
dwxgmac2_filter_write(struct mac_device_info * hw,u32 filter_no,u8 reg,u32 data)1292 static int dwxgmac2_filter_write(struct mac_device_info *hw, u32 filter_no,
1293 				 u8 reg, u32 data)
1294 {
1295 	void __iomem *ioaddr = hw->pcsr;
1296 	u32 value;
1297 	int ret;
1298 
1299 	ret = dwxgmac2_filter_wait(hw);
1300 	if (ret)
1301 		return ret;
1302 
1303 	writel(data, ioaddr + XGMAC_L3L4_DATA);
1304 
1305 	value = ((filter_no << XGMAC_IDDR_FNUM) | reg) << XGMAC_IDDR_SHIFT;
1306 	value |= XGMAC_XB;
1307 	writel(value, ioaddr + XGMAC_L3L4_ADDR_CTRL);
1308 
1309 	return dwxgmac2_filter_wait(hw);
1310 }
1311 
dwxgmac2_config_l3_filter(struct mac_device_info * hw,u32 filter_no,bool en,bool ipv6,bool sa,bool inv,u32 match)1312 static int dwxgmac2_config_l3_filter(struct mac_device_info *hw, u32 filter_no,
1313 				     bool en, bool ipv6, bool sa, bool inv,
1314 				     u32 match)
1315 {
1316 	void __iomem *ioaddr = hw->pcsr;
1317 	u32 value;
1318 	int ret;
1319 
1320 	value = readl(ioaddr + XGMAC_PACKET_FILTER);
1321 	value |= XGMAC_FILTER_IPFE;
1322 	writel(value, ioaddr + XGMAC_PACKET_FILTER);
1323 
1324 	ret = dwxgmac2_filter_read(hw, filter_no, XGMAC_L3L4_CTRL, &value);
1325 	if (ret)
1326 		return ret;
1327 
1328 	/* For IPv6 not both SA/DA filters can be active */
1329 	if (ipv6) {
1330 		value |= XGMAC_L3PEN0;
1331 		value &= ~(XGMAC_L3SAM0 | XGMAC_L3SAIM0);
1332 		value &= ~(XGMAC_L3DAM0 | XGMAC_L3DAIM0);
1333 		if (sa) {
1334 			value |= XGMAC_L3SAM0;
1335 			if (inv)
1336 				value |= XGMAC_L3SAIM0;
1337 		} else {
1338 			value |= XGMAC_L3DAM0;
1339 			if (inv)
1340 				value |= XGMAC_L3DAIM0;
1341 		}
1342 	} else {
1343 		value &= ~XGMAC_L3PEN0;
1344 		if (sa) {
1345 			value |= XGMAC_L3SAM0;
1346 			if (inv)
1347 				value |= XGMAC_L3SAIM0;
1348 		} else {
1349 			value |= XGMAC_L3DAM0;
1350 			if (inv)
1351 				value |= XGMAC_L3DAIM0;
1352 		}
1353 	}
1354 
1355 	ret = dwxgmac2_filter_write(hw, filter_no, XGMAC_L3L4_CTRL, value);
1356 	if (ret)
1357 		return ret;
1358 
1359 	if (sa) {
1360 		ret = dwxgmac2_filter_write(hw, filter_no, XGMAC_L3_ADDR0, match);
1361 		if (ret)
1362 			return ret;
1363 	} else {
1364 		ret = dwxgmac2_filter_write(hw, filter_no, XGMAC_L3_ADDR1, match);
1365 		if (ret)
1366 			return ret;
1367 	}
1368 
1369 	if (!en)
1370 		return dwxgmac2_filter_write(hw, filter_no, XGMAC_L3L4_CTRL, 0);
1371 
1372 	return 0;
1373 }
1374 
dwxgmac2_config_l4_filter(struct mac_device_info * hw,u32 filter_no,bool en,bool udp,bool sa,bool inv,u32 match)1375 static int dwxgmac2_config_l4_filter(struct mac_device_info *hw, u32 filter_no,
1376 				     bool en, bool udp, bool sa, bool inv,
1377 				     u32 match)
1378 {
1379 	void __iomem *ioaddr = hw->pcsr;
1380 	u32 value;
1381 	int ret;
1382 
1383 	value = readl(ioaddr + XGMAC_PACKET_FILTER);
1384 	value |= XGMAC_FILTER_IPFE;
1385 	writel(value, ioaddr + XGMAC_PACKET_FILTER);
1386 
1387 	ret = dwxgmac2_filter_read(hw, filter_no, XGMAC_L3L4_CTRL, &value);
1388 	if (ret)
1389 		return ret;
1390 
1391 	if (udp) {
1392 		value |= XGMAC_L4PEN0;
1393 	} else {
1394 		value &= ~XGMAC_L4PEN0;
1395 	}
1396 
1397 	value &= ~(XGMAC_L4SPM0 | XGMAC_L4SPIM0);
1398 	value &= ~(XGMAC_L4DPM0 | XGMAC_L4DPIM0);
1399 	if (sa) {
1400 		value |= XGMAC_L4SPM0;
1401 		if (inv)
1402 			value |= XGMAC_L4SPIM0;
1403 	} else {
1404 		value |= XGMAC_L4DPM0;
1405 		if (inv)
1406 			value |= XGMAC_L4DPIM0;
1407 	}
1408 
1409 	ret = dwxgmac2_filter_write(hw, filter_no, XGMAC_L3L4_CTRL, value);
1410 	if (ret)
1411 		return ret;
1412 
1413 	if (sa) {
1414 		value = match & XGMAC_L4SP0;
1415 
1416 		ret = dwxgmac2_filter_write(hw, filter_no, XGMAC_L4_ADDR, value);
1417 		if (ret)
1418 			return ret;
1419 	} else {
1420 		value = (match << XGMAC_L4DP0_SHIFT) & XGMAC_L4DP0;
1421 
1422 		ret = dwxgmac2_filter_write(hw, filter_no, XGMAC_L4_ADDR, value);
1423 		if (ret)
1424 			return ret;
1425 	}
1426 
1427 	if (!en)
1428 		return dwxgmac2_filter_write(hw, filter_no, XGMAC_L3L4_CTRL, 0);
1429 
1430 	return 0;
1431 }
1432 
dwxgmac2_set_arp_offload(struct mac_device_info * hw,bool en,u32 addr)1433 static void dwxgmac2_set_arp_offload(struct mac_device_info *hw, bool en,
1434 				     u32 addr)
1435 {
1436 	void __iomem *ioaddr = hw->pcsr;
1437 	u32 value;
1438 
1439 	writel(addr, ioaddr + XGMAC_ARP_ADDR);
1440 
1441 	value = readl(ioaddr + XGMAC_RX_CONFIG);
1442 	if (en)
1443 		value |= XGMAC_CONFIG_ARPEN;
1444 	else
1445 		value &= ~XGMAC_CONFIG_ARPEN;
1446 	writel(value, ioaddr + XGMAC_RX_CONFIG);
1447 }
1448 
dwxgmac3_est_write(void __iomem * ioaddr,u32 reg,u32 val,bool gcl)1449 static int dwxgmac3_est_write(void __iomem *ioaddr, u32 reg, u32 val, bool gcl)
1450 {
1451 	u32 ctrl;
1452 
1453 	writel(val, ioaddr + XGMAC_MTL_EST_GCL_DATA);
1454 
1455 	ctrl = (reg << XGMAC_ADDR_SHIFT);
1456 	ctrl |= gcl ? 0 : XGMAC_GCRR;
1457 
1458 	writel(ctrl, ioaddr + XGMAC_MTL_EST_GCL_CONTROL);
1459 
1460 	ctrl |= XGMAC_SRWO;
1461 	writel(ctrl, ioaddr + XGMAC_MTL_EST_GCL_CONTROL);
1462 
1463 	return readl_poll_timeout_atomic(ioaddr + XGMAC_MTL_EST_GCL_CONTROL,
1464 					 ctrl, !(ctrl & XGMAC_SRWO), 100, 5000);
1465 }
1466 
dwxgmac3_est_configure(void __iomem * ioaddr,struct stmmac_est * cfg,unsigned int ptp_rate)1467 static int dwxgmac3_est_configure(void __iomem *ioaddr, struct stmmac_est *cfg,
1468 				  unsigned int ptp_rate)
1469 {
1470 	int i, ret = 0x0;
1471 	u32 ctrl;
1472 
1473 	ret |= dwxgmac3_est_write(ioaddr, XGMAC_BTR_LOW, cfg->btr[0], false);
1474 	ret |= dwxgmac3_est_write(ioaddr, XGMAC_BTR_HIGH, cfg->btr[1], false);
1475 	ret |= dwxgmac3_est_write(ioaddr, XGMAC_TER, cfg->ter, false);
1476 	ret |= dwxgmac3_est_write(ioaddr, XGMAC_LLR, cfg->gcl_size, false);
1477 	ret |= dwxgmac3_est_write(ioaddr, XGMAC_CTR_LOW, cfg->ctr[0], false);
1478 	ret |= dwxgmac3_est_write(ioaddr, XGMAC_CTR_HIGH, cfg->ctr[1], false);
1479 	if (ret)
1480 		return ret;
1481 
1482 	for (i = 0; i < cfg->gcl_size; i++) {
1483 		ret = dwxgmac3_est_write(ioaddr, i, cfg->gcl[i], true);
1484 		if (ret)
1485 			return ret;
1486 	}
1487 
1488 	ctrl = readl(ioaddr + XGMAC_MTL_EST_CONTROL);
1489 	ctrl &= ~XGMAC_PTOV;
1490 	ctrl |= ((1000000000 / ptp_rate) * 9) << XGMAC_PTOV_SHIFT;
1491 	if (cfg->enable)
1492 		ctrl |= XGMAC_EEST | XGMAC_SSWL;
1493 	else
1494 		ctrl &= ~XGMAC_EEST;
1495 
1496 	writel(ctrl, ioaddr + XGMAC_MTL_EST_CONTROL);
1497 	return 0;
1498 }
1499 
dwxgmac3_fpe_configure(void __iomem * ioaddr,struct stmmac_fpe_cfg * cfg,u32 num_txq,u32 num_rxq,bool enable)1500 static void dwxgmac3_fpe_configure(void __iomem *ioaddr, struct stmmac_fpe_cfg *cfg,
1501 				   u32 num_txq,
1502 				   u32 num_rxq, bool enable)
1503 {
1504 	u32 value;
1505 
1506 	if (!enable) {
1507 		value = readl(ioaddr + XGMAC_FPE_CTRL_STS);
1508 
1509 		value &= ~XGMAC_EFPE;
1510 
1511 		writel(value, ioaddr + XGMAC_FPE_CTRL_STS);
1512 		return;
1513 	}
1514 
1515 	value = readl(ioaddr + XGMAC_RXQ_CTRL1);
1516 	value &= ~XGMAC_RQ;
1517 	value |= (num_rxq - 1) << XGMAC_RQ_SHIFT;
1518 	writel(value, ioaddr + XGMAC_RXQ_CTRL1);
1519 
1520 	value = readl(ioaddr + XGMAC_FPE_CTRL_STS);
1521 	value |= XGMAC_EFPE;
1522 	writel(value, ioaddr + XGMAC_FPE_CTRL_STS);
1523 }
1524 
1525 const struct stmmac_ops dwxgmac210_ops = {
1526 	.core_init = dwxgmac2_core_init,
1527 	.set_mac = dwxgmac2_set_mac,
1528 	.rx_ipc = dwxgmac2_rx_ipc,
1529 	.rx_queue_enable = dwxgmac2_rx_queue_enable,
1530 	.rx_queue_prio = dwxgmac2_rx_queue_prio,
1531 	.tx_queue_prio = dwxgmac2_tx_queue_prio,
1532 	.rx_queue_routing = NULL,
1533 	.prog_mtl_rx_algorithms = dwxgmac2_prog_mtl_rx_algorithms,
1534 	.prog_mtl_tx_algorithms = dwxgmac2_prog_mtl_tx_algorithms,
1535 	.set_mtl_tx_queue_weight = dwxgmac2_set_mtl_tx_queue_weight,
1536 	.map_mtl_to_dma = dwxgmac2_map_mtl_to_dma,
1537 	.config_cbs = dwxgmac2_config_cbs,
1538 	.dump_regs = dwxgmac2_dump_regs,
1539 	.host_irq_status = dwxgmac2_host_irq_status,
1540 	.host_mtl_irq_status = dwxgmac2_host_mtl_irq_status,
1541 	.flow_ctrl = dwxgmac2_flow_ctrl,
1542 	.pmt = dwxgmac2_pmt,
1543 	.set_umac_addr = dwxgmac2_set_umac_addr,
1544 	.get_umac_addr = dwxgmac2_get_umac_addr,
1545 	.set_eee_mode = dwxgmac2_set_eee_mode,
1546 	.reset_eee_mode = dwxgmac2_reset_eee_mode,
1547 	.set_eee_timer = dwxgmac2_set_eee_timer,
1548 	.set_eee_pls = dwxgmac2_set_eee_pls,
1549 	.pcs_ctrl_ane = NULL,
1550 	.pcs_rane = NULL,
1551 	.pcs_get_adv_lp = NULL,
1552 	.debug = NULL,
1553 	.set_filter = dwxgmac2_set_filter,
1554 	.safety_feat_config = dwxgmac3_safety_feat_config,
1555 	.safety_feat_irq_status = dwxgmac3_safety_feat_irq_status,
1556 	.safety_feat_dump = dwxgmac3_safety_feat_dump,
1557 	.set_mac_loopback = dwxgmac2_set_mac_loopback,
1558 	.rss_configure = dwxgmac2_rss_configure,
1559 	.update_vlan_hash = dwxgmac2_update_vlan_hash,
1560 	.rxp_config = dwxgmac3_rxp_config,
1561 	.get_mac_tx_timestamp = dwxgmac2_get_mac_tx_timestamp,
1562 	.flex_pps_config = dwxgmac2_flex_pps_config,
1563 	.sarc_configure = dwxgmac2_sarc_configure,
1564 	.enable_vlan = dwxgmac2_enable_vlan,
1565 	.config_l3_filter = dwxgmac2_config_l3_filter,
1566 	.config_l4_filter = dwxgmac2_config_l4_filter,
1567 	.set_arp_offload = dwxgmac2_set_arp_offload,
1568 	.est_configure = dwxgmac3_est_configure,
1569 	.fpe_configure = dwxgmac3_fpe_configure,
1570 };
1571 
dwxlgmac2_rx_queue_enable(struct mac_device_info * hw,u8 mode,u32 queue)1572 static void dwxlgmac2_rx_queue_enable(struct mac_device_info *hw, u8 mode,
1573 				      u32 queue)
1574 {
1575 	void __iomem *ioaddr = hw->pcsr;
1576 	u32 value;
1577 
1578 	value = readl(ioaddr + XLGMAC_RXQ_ENABLE_CTRL0) & ~XGMAC_RXQEN(queue);
1579 	if (mode == MTL_QUEUE_AVB)
1580 		value |= 0x1 << XGMAC_RXQEN_SHIFT(queue);
1581 	else if (mode == MTL_QUEUE_DCB)
1582 		value |= 0x2 << XGMAC_RXQEN_SHIFT(queue);
1583 	writel(value, ioaddr + XLGMAC_RXQ_ENABLE_CTRL0);
1584 }
1585 
1586 const struct stmmac_ops dwxlgmac2_ops = {
1587 	.core_init = dwxgmac2_core_init,
1588 	.set_mac = dwxgmac2_set_mac,
1589 	.rx_ipc = dwxgmac2_rx_ipc,
1590 	.rx_queue_enable = dwxlgmac2_rx_queue_enable,
1591 	.rx_queue_prio = dwxgmac2_rx_queue_prio,
1592 	.tx_queue_prio = dwxgmac2_tx_queue_prio,
1593 	.rx_queue_routing = NULL,
1594 	.prog_mtl_rx_algorithms = dwxgmac2_prog_mtl_rx_algorithms,
1595 	.prog_mtl_tx_algorithms = dwxgmac2_prog_mtl_tx_algorithms,
1596 	.set_mtl_tx_queue_weight = dwxgmac2_set_mtl_tx_queue_weight,
1597 	.map_mtl_to_dma = dwxgmac2_map_mtl_to_dma,
1598 	.config_cbs = dwxgmac2_config_cbs,
1599 	.dump_regs = dwxgmac2_dump_regs,
1600 	.host_irq_status = dwxgmac2_host_irq_status,
1601 	.host_mtl_irq_status = dwxgmac2_host_mtl_irq_status,
1602 	.flow_ctrl = dwxgmac2_flow_ctrl,
1603 	.pmt = dwxgmac2_pmt,
1604 	.set_umac_addr = dwxgmac2_set_umac_addr,
1605 	.get_umac_addr = dwxgmac2_get_umac_addr,
1606 	.set_eee_mode = dwxgmac2_set_eee_mode,
1607 	.reset_eee_mode = dwxgmac2_reset_eee_mode,
1608 	.set_eee_timer = dwxgmac2_set_eee_timer,
1609 	.set_eee_pls = dwxgmac2_set_eee_pls,
1610 	.pcs_ctrl_ane = NULL,
1611 	.pcs_rane = NULL,
1612 	.pcs_get_adv_lp = NULL,
1613 	.debug = NULL,
1614 	.set_filter = dwxgmac2_set_filter,
1615 	.safety_feat_config = dwxgmac3_safety_feat_config,
1616 	.safety_feat_irq_status = dwxgmac3_safety_feat_irq_status,
1617 	.safety_feat_dump = dwxgmac3_safety_feat_dump,
1618 	.set_mac_loopback = dwxgmac2_set_mac_loopback,
1619 	.rss_configure = dwxgmac2_rss_configure,
1620 	.update_vlan_hash = dwxgmac2_update_vlan_hash,
1621 	.rxp_config = dwxgmac3_rxp_config,
1622 	.get_mac_tx_timestamp = dwxgmac2_get_mac_tx_timestamp,
1623 	.flex_pps_config = dwxgmac2_flex_pps_config,
1624 	.sarc_configure = dwxgmac2_sarc_configure,
1625 	.enable_vlan = dwxgmac2_enable_vlan,
1626 	.config_l3_filter = dwxgmac2_config_l3_filter,
1627 	.config_l4_filter = dwxgmac2_config_l4_filter,
1628 	.set_arp_offload = dwxgmac2_set_arp_offload,
1629 	.est_configure = dwxgmac3_est_configure,
1630 	.fpe_configure = dwxgmac3_fpe_configure,
1631 };
1632 
dwxgmac2_setup(struct stmmac_priv * priv)1633 int dwxgmac2_setup(struct stmmac_priv *priv)
1634 {
1635 	struct mac_device_info *mac = priv->hw;
1636 
1637 	dev_info(priv->device, "\tXGMAC2\n");
1638 
1639 	priv->dev->priv_flags |= IFF_UNICAST_FLT;
1640 	mac->pcsr = priv->ioaddr;
1641 	mac->multicast_filter_bins = priv->plat->multicast_filter_bins;
1642 	mac->unicast_filter_entries = priv->plat->unicast_filter_entries;
1643 	mac->mcast_bits_log2 = 0;
1644 
1645 	if (mac->multicast_filter_bins)
1646 		mac->mcast_bits_log2 = ilog2(mac->multicast_filter_bins);
1647 
1648 	mac->link.duplex = 0;
1649 	mac->link.speed10 = XGMAC_CONFIG_SS_10_MII;
1650 	mac->link.speed100 = XGMAC_CONFIG_SS_100_MII;
1651 	mac->link.speed1000 = XGMAC_CONFIG_SS_1000_GMII;
1652 	mac->link.speed2500 = XGMAC_CONFIG_SS_2500_GMII;
1653 	mac->link.xgmii.speed2500 = XGMAC_CONFIG_SS_2500;
1654 	mac->link.xgmii.speed5000 = XGMAC_CONFIG_SS_5000;
1655 	mac->link.xgmii.speed10000 = XGMAC_CONFIG_SS_10000;
1656 	mac->link.speed_mask = XGMAC_CONFIG_SS_MASK;
1657 
1658 	mac->mii.addr = XGMAC_MDIO_ADDR;
1659 	mac->mii.data = XGMAC_MDIO_DATA;
1660 	mac->mii.addr_shift = 16;
1661 	mac->mii.addr_mask = GENMASK(20, 16);
1662 	mac->mii.reg_shift = 0;
1663 	mac->mii.reg_mask = GENMASK(15, 0);
1664 	mac->mii.clk_csr_shift = 19;
1665 	mac->mii.clk_csr_mask = GENMASK(21, 19);
1666 
1667 	return 0;
1668 }
1669 
dwxlgmac2_setup(struct stmmac_priv * priv)1670 int dwxlgmac2_setup(struct stmmac_priv *priv)
1671 {
1672 	struct mac_device_info *mac = priv->hw;
1673 
1674 	dev_info(priv->device, "\tXLGMAC\n");
1675 
1676 	priv->dev->priv_flags |= IFF_UNICAST_FLT;
1677 	mac->pcsr = priv->ioaddr;
1678 	mac->multicast_filter_bins = priv->plat->multicast_filter_bins;
1679 	mac->unicast_filter_entries = priv->plat->unicast_filter_entries;
1680 	mac->mcast_bits_log2 = 0;
1681 
1682 	if (mac->multicast_filter_bins)
1683 		mac->mcast_bits_log2 = ilog2(mac->multicast_filter_bins);
1684 
1685 	mac->link.duplex = 0;
1686 	mac->link.speed1000 = XLGMAC_CONFIG_SS_1000;
1687 	mac->link.speed2500 = XLGMAC_CONFIG_SS_2500;
1688 	mac->link.xgmii.speed10000 = XLGMAC_CONFIG_SS_10G;
1689 	mac->link.xlgmii.speed25000 = XLGMAC_CONFIG_SS_25G;
1690 	mac->link.xlgmii.speed40000 = XLGMAC_CONFIG_SS_40G;
1691 	mac->link.xlgmii.speed50000 = XLGMAC_CONFIG_SS_50G;
1692 	mac->link.xlgmii.speed100000 = XLGMAC_CONFIG_SS_100G;
1693 	mac->link.speed_mask = XLGMAC_CONFIG_SS;
1694 
1695 	mac->mii.addr = XGMAC_MDIO_ADDR;
1696 	mac->mii.data = XGMAC_MDIO_DATA;
1697 	mac->mii.addr_shift = 16;
1698 	mac->mii.addr_mask = GENMASK(20, 16);
1699 	mac->mii.reg_shift = 0;
1700 	mac->mii.reg_mask = GENMASK(15, 0);
1701 	mac->mii.clk_csr_shift = 19;
1702 	mac->mii.clk_csr_mask = GENMASK(21, 19);
1703 
1704 	return 0;
1705 }
1706