1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2020 MediaTek Corporation
4  * Copyright (c) 2020 BayLibre SAS
5  *
6  * Author: Bartosz Golaszewski <bgolaszewski@baylibre.com>
7  */
8 
9 #include <linux/bits.h>
10 #include <linux/clk.h>
11 #include <linux/compiler.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/etherdevice.h>
14 #include <linux/kernel.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/mii.h>
17 #include <linux/module.h>
18 #include <linux/netdevice.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21 #include <linux/of_mdio.h>
22 #include <linux/of_net.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm.h>
25 #include <linux/regmap.h>
26 #include <linux/skbuff.h>
27 #include <linux/spinlock.h>
28 
29 #define MTK_STAR_DRVNAME			"mtk_star_emac"
30 
31 #define MTK_STAR_WAIT_TIMEOUT			300
32 #define MTK_STAR_MAX_FRAME_SIZE			1514
33 #define MTK_STAR_SKB_ALIGNMENT			16
34 #define MTK_STAR_HASHTABLE_MC_LIMIT		256
35 #define MTK_STAR_HASHTABLE_SIZE_MAX		512
36 #define MTK_STAR_DESC_NEEDED			(MAX_SKB_FRAGS + 4)
37 
38 /* Normally we'd use NET_IP_ALIGN but on arm64 its value is 0 and it doesn't
39  * work for this controller.
40  */
41 #define MTK_STAR_IP_ALIGN			2
42 
43 static const char *const mtk_star_clk_names[] = { "core", "reg", "trans" };
44 #define MTK_STAR_NCLKS ARRAY_SIZE(mtk_star_clk_names)
45 
46 /* PHY Control Register 0 */
47 #define MTK_STAR_REG_PHY_CTRL0			0x0000
48 #define MTK_STAR_BIT_PHY_CTRL0_WTCMD		BIT(13)
49 #define MTK_STAR_BIT_PHY_CTRL0_RDCMD		BIT(14)
50 #define MTK_STAR_BIT_PHY_CTRL0_RWOK		BIT(15)
51 #define MTK_STAR_MSK_PHY_CTRL0_PREG		GENMASK(12, 8)
52 #define MTK_STAR_OFF_PHY_CTRL0_PREG		8
53 #define MTK_STAR_MSK_PHY_CTRL0_RWDATA		GENMASK(31, 16)
54 #define MTK_STAR_OFF_PHY_CTRL0_RWDATA		16
55 
56 /* PHY Control Register 1 */
57 #define MTK_STAR_REG_PHY_CTRL1			0x0004
58 #define MTK_STAR_BIT_PHY_CTRL1_LINK_ST		BIT(0)
59 #define MTK_STAR_BIT_PHY_CTRL1_AN_EN		BIT(8)
60 #define MTK_STAR_OFF_PHY_CTRL1_FORCE_SPD	9
61 #define MTK_STAR_VAL_PHY_CTRL1_FORCE_SPD_10M	0x00
62 #define MTK_STAR_VAL_PHY_CTRL1_FORCE_SPD_100M	0x01
63 #define MTK_STAR_VAL_PHY_CTRL1_FORCE_SPD_1000M	0x02
64 #define MTK_STAR_BIT_PHY_CTRL1_FORCE_DPX	BIT(11)
65 #define MTK_STAR_BIT_PHY_CTRL1_FORCE_FC_RX	BIT(12)
66 #define MTK_STAR_BIT_PHY_CTRL1_FORCE_FC_TX	BIT(13)
67 
68 /* MAC Configuration Register */
69 #define MTK_STAR_REG_MAC_CFG			0x0008
70 #define MTK_STAR_OFF_MAC_CFG_IPG		10
71 #define MTK_STAR_VAL_MAC_CFG_IPG_96BIT		GENMASK(4, 0)
72 #define MTK_STAR_BIT_MAC_CFG_MAXLEN_1522	BIT(16)
73 #define MTK_STAR_BIT_MAC_CFG_AUTO_PAD		BIT(19)
74 #define MTK_STAR_BIT_MAC_CFG_CRC_STRIP		BIT(20)
75 #define MTK_STAR_BIT_MAC_CFG_VLAN_STRIP		BIT(22)
76 #define MTK_STAR_BIT_MAC_CFG_NIC_PD		BIT(31)
77 
78 /* Flow-Control Configuration Register */
79 #define MTK_STAR_REG_FC_CFG			0x000c
80 #define MTK_STAR_BIT_FC_CFG_BP_EN		BIT(7)
81 #define MTK_STAR_BIT_FC_CFG_UC_PAUSE_DIR	BIT(8)
82 #define MTK_STAR_OFF_FC_CFG_SEND_PAUSE_TH	16
83 #define MTK_STAR_MSK_FC_CFG_SEND_PAUSE_TH	GENMASK(27, 16)
84 #define MTK_STAR_VAL_FC_CFG_SEND_PAUSE_TH_2K	0x800
85 
86 /* ARL Configuration Register */
87 #define MTK_STAR_REG_ARL_CFG			0x0010
88 #define MTK_STAR_BIT_ARL_CFG_HASH_ALG		BIT(0)
89 #define MTK_STAR_BIT_ARL_CFG_MISC_MODE		BIT(4)
90 
91 /* MAC High and Low Bytes Registers */
92 #define MTK_STAR_REG_MY_MAC_H			0x0014
93 #define MTK_STAR_REG_MY_MAC_L			0x0018
94 
95 /* Hash Table Control Register */
96 #define MTK_STAR_REG_HASH_CTRL			0x001c
97 #define MTK_STAR_MSK_HASH_CTRL_HASH_BIT_ADDR	GENMASK(8, 0)
98 #define MTK_STAR_BIT_HASH_CTRL_HASH_BIT_DATA	BIT(12)
99 #define MTK_STAR_BIT_HASH_CTRL_ACC_CMD		BIT(13)
100 #define MTK_STAR_BIT_HASH_CTRL_CMD_START	BIT(14)
101 #define MTK_STAR_BIT_HASH_CTRL_BIST_OK		BIT(16)
102 #define MTK_STAR_BIT_HASH_CTRL_BIST_DONE	BIT(17)
103 #define MTK_STAR_BIT_HASH_CTRL_BIST_EN		BIT(31)
104 
105 /* TX DMA Control Register */
106 #define MTK_STAR_REG_TX_DMA_CTRL		0x0034
107 #define MTK_STAR_BIT_TX_DMA_CTRL_START		BIT(0)
108 #define MTK_STAR_BIT_TX_DMA_CTRL_STOP		BIT(1)
109 #define MTK_STAR_BIT_TX_DMA_CTRL_RESUME		BIT(2)
110 
111 /* RX DMA Control Register */
112 #define MTK_STAR_REG_RX_DMA_CTRL		0x0038
113 #define MTK_STAR_BIT_RX_DMA_CTRL_START		BIT(0)
114 #define MTK_STAR_BIT_RX_DMA_CTRL_STOP		BIT(1)
115 #define MTK_STAR_BIT_RX_DMA_CTRL_RESUME		BIT(2)
116 
117 /* DMA Address Registers */
118 #define MTK_STAR_REG_TX_DPTR			0x003c
119 #define MTK_STAR_REG_RX_DPTR			0x0040
120 #define MTK_STAR_REG_TX_BASE_ADDR		0x0044
121 #define MTK_STAR_REG_RX_BASE_ADDR		0x0048
122 
123 /* Interrupt Status Register */
124 #define MTK_STAR_REG_INT_STS			0x0050
125 #define MTK_STAR_REG_INT_STS_PORT_STS_CHG	BIT(2)
126 #define MTK_STAR_REG_INT_STS_MIB_CNT_TH		BIT(3)
127 #define MTK_STAR_BIT_INT_STS_FNRC		BIT(6)
128 #define MTK_STAR_BIT_INT_STS_TNTC		BIT(8)
129 
130 /* Interrupt Mask Register */
131 #define MTK_STAR_REG_INT_MASK			0x0054
132 #define MTK_STAR_BIT_INT_MASK_FNRC		BIT(6)
133 
134 /* Delay-Macro Register */
135 #define MTK_STAR_REG_TEST0			0x0058
136 #define MTK_STAR_BIT_INV_RX_CLK			BIT(30)
137 #define MTK_STAR_BIT_INV_TX_CLK			BIT(31)
138 
139 /* Misc. Config Register */
140 #define MTK_STAR_REG_TEST1			0x005c
141 #define MTK_STAR_BIT_TEST1_RST_HASH_MBIST	BIT(31)
142 
143 /* Extended Configuration Register */
144 #define MTK_STAR_REG_EXT_CFG			0x0060
145 #define MTK_STAR_OFF_EXT_CFG_SND_PAUSE_RLS	16
146 #define MTK_STAR_MSK_EXT_CFG_SND_PAUSE_RLS	GENMASK(26, 16)
147 #define MTK_STAR_VAL_EXT_CFG_SND_PAUSE_RLS_1K	0x400
148 
149 /* EthSys Configuration Register */
150 #define MTK_STAR_REG_SYS_CONF			0x0094
151 #define MTK_STAR_BIT_MII_PAD_OUT_ENABLE		BIT(0)
152 #define MTK_STAR_BIT_EXT_MDC_MODE		BIT(1)
153 #define MTK_STAR_BIT_SWC_MII_MODE		BIT(2)
154 
155 /* MAC Clock Configuration Register */
156 #define MTK_STAR_REG_MAC_CLK_CONF		0x00ac
157 #define MTK_STAR_MSK_MAC_CLK_CONF		GENMASK(7, 0)
158 #define MTK_STAR_BIT_CLK_DIV_10			0x0a
159 #define MTK_STAR_BIT_CLK_DIV_50			0x32
160 
161 /* Counter registers. */
162 #define MTK_STAR_REG_C_RXOKPKT			0x0100
163 #define MTK_STAR_REG_C_RXOKBYTE			0x0104
164 #define MTK_STAR_REG_C_RXRUNT			0x0108
165 #define MTK_STAR_REG_C_RXLONG			0x010c
166 #define MTK_STAR_REG_C_RXDROP			0x0110
167 #define MTK_STAR_REG_C_RXCRC			0x0114
168 #define MTK_STAR_REG_C_RXARLDROP		0x0118
169 #define MTK_STAR_REG_C_RXVLANDROP		0x011c
170 #define MTK_STAR_REG_C_RXCSERR			0x0120
171 #define MTK_STAR_REG_C_RXPAUSE			0x0124
172 #define MTK_STAR_REG_C_TXOKPKT			0x0128
173 #define MTK_STAR_REG_C_TXOKBYTE			0x012c
174 #define MTK_STAR_REG_C_TXPAUSECOL		0x0130
175 #define MTK_STAR_REG_C_TXRTY			0x0134
176 #define MTK_STAR_REG_C_TXSKIP			0x0138
177 #define MTK_STAR_REG_C_TX_ARP			0x013c
178 #define MTK_STAR_REG_C_RX_RERR			0x01d8
179 #define MTK_STAR_REG_C_RX_UNI			0x01dc
180 #define MTK_STAR_REG_C_RX_MULTI			0x01e0
181 #define MTK_STAR_REG_C_RX_BROAD			0x01e4
182 #define MTK_STAR_REG_C_RX_ALIGNERR		0x01e8
183 #define MTK_STAR_REG_C_TX_UNI			0x01ec
184 #define MTK_STAR_REG_C_TX_MULTI			0x01f0
185 #define MTK_STAR_REG_C_TX_BROAD			0x01f4
186 #define MTK_STAR_REG_C_TX_TIMEOUT		0x01f8
187 #define MTK_STAR_REG_C_TX_LATECOL		0x01fc
188 #define MTK_STAR_REG_C_RX_LENGTHERR		0x0214
189 #define MTK_STAR_REG_C_RX_TWIST			0x0218
190 
191 /* Ethernet CFG Control */
192 #define MTK_PERICFG_REG_NIC_CFG0_CON		0x03c4
193 #define MTK_PERICFG_REG_NIC_CFG1_CON		0x03c8
194 #define MTK_PERICFG_REG_NIC_CFG_CON_V2		0x0c10
195 #define MTK_PERICFG_REG_NIC_CFG_CON_CFG_INTF	GENMASK(3, 0)
196 #define MTK_PERICFG_BIT_NIC_CFG_CON_MII		0
197 #define MTK_PERICFG_BIT_NIC_CFG_CON_RMII	1
198 #define MTK_PERICFG_BIT_NIC_CFG_CON_CLK		BIT(0)
199 #define MTK_PERICFG_BIT_NIC_CFG_CON_CLK_V2	BIT(8)
200 
201 /* Represents the actual structure of descriptors used by the MAC. We can
202  * reuse the same structure for both TX and RX - the layout is the same, only
203  * the flags differ slightly.
204  */
205 struct mtk_star_ring_desc {
206 	/* Contains both the status flags as well as packet length. */
207 	u32 status;
208 	u32 data_ptr;
209 	u32 vtag;
210 	u32 reserved;
211 };
212 
213 #define MTK_STAR_DESC_MSK_LEN			GENMASK(15, 0)
214 #define MTK_STAR_DESC_BIT_RX_CRCE		BIT(24)
215 #define MTK_STAR_DESC_BIT_RX_OSIZE		BIT(25)
216 #define MTK_STAR_DESC_BIT_INT			BIT(27)
217 #define MTK_STAR_DESC_BIT_LS			BIT(28)
218 #define MTK_STAR_DESC_BIT_FS			BIT(29)
219 #define MTK_STAR_DESC_BIT_EOR			BIT(30)
220 #define MTK_STAR_DESC_BIT_COWN			BIT(31)
221 
222 /* Helper structure for storing data read from/written to descriptors in order
223  * to limit reads from/writes to DMA memory.
224  */
225 struct mtk_star_ring_desc_data {
226 	unsigned int len;
227 	unsigned int flags;
228 	dma_addr_t dma_addr;
229 	struct sk_buff *skb;
230 };
231 
232 #define MTK_STAR_RING_NUM_DESCS			512
233 #define MTK_STAR_TX_THRESH			(MTK_STAR_RING_NUM_DESCS / 4)
234 #define MTK_STAR_NUM_TX_DESCS			MTK_STAR_RING_NUM_DESCS
235 #define MTK_STAR_NUM_RX_DESCS			MTK_STAR_RING_NUM_DESCS
236 #define MTK_STAR_NUM_DESCS_TOTAL		(MTK_STAR_RING_NUM_DESCS * 2)
237 #define MTK_STAR_DMA_SIZE \
238 		(MTK_STAR_NUM_DESCS_TOTAL * sizeof(struct mtk_star_ring_desc))
239 
240 struct mtk_star_ring {
241 	struct mtk_star_ring_desc *descs;
242 	struct sk_buff *skbs[MTK_STAR_RING_NUM_DESCS];
243 	dma_addr_t dma_addrs[MTK_STAR_RING_NUM_DESCS];
244 	unsigned int head;
245 	unsigned int tail;
246 };
247 
248 struct mtk_star_compat {
249 	int (*set_interface_mode)(struct net_device *ndev);
250 	unsigned char bit_clk_div;
251 };
252 
253 struct mtk_star_priv {
254 	struct net_device *ndev;
255 
256 	struct regmap *regs;
257 	struct regmap *pericfg;
258 
259 	struct clk_bulk_data clks[MTK_STAR_NCLKS];
260 
261 	void *ring_base;
262 	struct mtk_star_ring_desc *descs_base;
263 	dma_addr_t dma_addr;
264 	struct mtk_star_ring tx_ring;
265 	struct mtk_star_ring rx_ring;
266 
267 	struct mii_bus *mii;
268 	struct napi_struct tx_napi;
269 	struct napi_struct rx_napi;
270 
271 	struct device_node *phy_node;
272 	phy_interface_t phy_intf;
273 	struct phy_device *phydev;
274 	unsigned int link;
275 	int speed;
276 	int duplex;
277 	int pause;
278 	bool rmii_rxc;
279 	bool rx_inv;
280 	bool tx_inv;
281 
282 	const struct mtk_star_compat *compat_data;
283 
284 	/* Protects against concurrent descriptor access. */
285 	spinlock_t lock;
286 
287 	struct rtnl_link_stats64 stats;
288 };
289 
290 static struct device *mtk_star_get_dev(struct mtk_star_priv *priv)
291 {
292 	return priv->ndev->dev.parent;
293 }
294 
295 static const struct regmap_config mtk_star_regmap_config = {
296 	.reg_bits		= 32,
297 	.val_bits		= 32,
298 	.reg_stride		= 4,
299 	.disable_locking	= true,
300 };
301 
302 static void mtk_star_ring_init(struct mtk_star_ring *ring,
303 			       struct mtk_star_ring_desc *descs)
304 {
305 	memset(ring, 0, sizeof(*ring));
306 	ring->descs = descs;
307 	ring->head = 0;
308 	ring->tail = 0;
309 }
310 
311 static int mtk_star_ring_pop_tail(struct mtk_star_ring *ring,
312 				  struct mtk_star_ring_desc_data *desc_data)
313 {
314 	struct mtk_star_ring_desc *desc = &ring->descs[ring->tail];
315 	unsigned int status;
316 
317 	status = READ_ONCE(desc->status);
318 	dma_rmb(); /* Make sure we read the status bits before checking it. */
319 
320 	if (!(status & MTK_STAR_DESC_BIT_COWN))
321 		return -1;
322 
323 	desc_data->len = status & MTK_STAR_DESC_MSK_LEN;
324 	desc_data->flags = status & ~MTK_STAR_DESC_MSK_LEN;
325 	desc_data->dma_addr = ring->dma_addrs[ring->tail];
326 	desc_data->skb = ring->skbs[ring->tail];
327 
328 	ring->dma_addrs[ring->tail] = 0;
329 	ring->skbs[ring->tail] = NULL;
330 
331 	status &= MTK_STAR_DESC_BIT_COWN | MTK_STAR_DESC_BIT_EOR;
332 
333 	WRITE_ONCE(desc->data_ptr, 0);
334 	WRITE_ONCE(desc->status, status);
335 
336 	ring->tail = (ring->tail + 1) % MTK_STAR_RING_NUM_DESCS;
337 
338 	return 0;
339 }
340 
341 static void mtk_star_ring_push_head(struct mtk_star_ring *ring,
342 				    struct mtk_star_ring_desc_data *desc_data,
343 				    unsigned int flags)
344 {
345 	struct mtk_star_ring_desc *desc = &ring->descs[ring->head];
346 	unsigned int status;
347 
348 	status = READ_ONCE(desc->status);
349 
350 	ring->skbs[ring->head] = desc_data->skb;
351 	ring->dma_addrs[ring->head] = desc_data->dma_addr;
352 
353 	status |= desc_data->len;
354 	if (flags)
355 		status |= flags;
356 
357 	WRITE_ONCE(desc->data_ptr, desc_data->dma_addr);
358 	WRITE_ONCE(desc->status, status);
359 	status &= ~MTK_STAR_DESC_BIT_COWN;
360 	/* Flush previous modifications before ownership change. */
361 	dma_wmb();
362 	WRITE_ONCE(desc->status, status);
363 
364 	ring->head = (ring->head + 1) % MTK_STAR_RING_NUM_DESCS;
365 }
366 
367 static void
368 mtk_star_ring_push_head_rx(struct mtk_star_ring *ring,
369 			   struct mtk_star_ring_desc_data *desc_data)
370 {
371 	mtk_star_ring_push_head(ring, desc_data, 0);
372 }
373 
374 static void
375 mtk_star_ring_push_head_tx(struct mtk_star_ring *ring,
376 			   struct mtk_star_ring_desc_data *desc_data)
377 {
378 	static const unsigned int flags = MTK_STAR_DESC_BIT_FS |
379 					  MTK_STAR_DESC_BIT_LS |
380 					  MTK_STAR_DESC_BIT_INT;
381 
382 	mtk_star_ring_push_head(ring, desc_data, flags);
383 }
384 
385 static unsigned int mtk_star_tx_ring_avail(struct mtk_star_ring *ring)
386 {
387 	u32 avail;
388 
389 	if (ring->tail > ring->head)
390 		avail = ring->tail - ring->head - 1;
391 	else
392 		avail = MTK_STAR_RING_NUM_DESCS - ring->head + ring->tail - 1;
393 
394 	return avail;
395 }
396 
397 static dma_addr_t mtk_star_dma_map_rx(struct mtk_star_priv *priv,
398 				      struct sk_buff *skb)
399 {
400 	struct device *dev = mtk_star_get_dev(priv);
401 
402 	/* Data pointer for the RX DMA descriptor must be aligned to 4N + 2. */
403 	return dma_map_single(dev, skb_tail_pointer(skb) - 2,
404 			      skb_tailroom(skb), DMA_FROM_DEVICE);
405 }
406 
407 static void mtk_star_dma_unmap_rx(struct mtk_star_priv *priv,
408 				  struct mtk_star_ring_desc_data *desc_data)
409 {
410 	struct device *dev = mtk_star_get_dev(priv);
411 
412 	dma_unmap_single(dev, desc_data->dma_addr,
413 			 skb_tailroom(desc_data->skb), DMA_FROM_DEVICE);
414 }
415 
416 static dma_addr_t mtk_star_dma_map_tx(struct mtk_star_priv *priv,
417 				      struct sk_buff *skb)
418 {
419 	struct device *dev = mtk_star_get_dev(priv);
420 
421 	return dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
422 }
423 
424 static void mtk_star_dma_unmap_tx(struct mtk_star_priv *priv,
425 				  struct mtk_star_ring_desc_data *desc_data)
426 {
427 	struct device *dev = mtk_star_get_dev(priv);
428 
429 	return dma_unmap_single(dev, desc_data->dma_addr,
430 				skb_headlen(desc_data->skb), DMA_TO_DEVICE);
431 }
432 
433 static void mtk_star_nic_disable_pd(struct mtk_star_priv *priv)
434 {
435 	regmap_clear_bits(priv->regs, MTK_STAR_REG_MAC_CFG,
436 			  MTK_STAR_BIT_MAC_CFG_NIC_PD);
437 }
438 
439 static void mtk_star_enable_dma_irq(struct mtk_star_priv *priv,
440 				    bool rx, bool tx)
441 {
442 	u32 value;
443 
444 	regmap_read(priv->regs, MTK_STAR_REG_INT_MASK, &value);
445 
446 	if (tx)
447 		value &= ~MTK_STAR_BIT_INT_STS_TNTC;
448 	if (rx)
449 		value &= ~MTK_STAR_BIT_INT_STS_FNRC;
450 
451 	regmap_write(priv->regs, MTK_STAR_REG_INT_MASK, value);
452 }
453 
454 static void mtk_star_disable_dma_irq(struct mtk_star_priv *priv,
455 				     bool rx, bool tx)
456 {
457 	u32 value;
458 
459 	regmap_read(priv->regs, MTK_STAR_REG_INT_MASK, &value);
460 
461 	if (tx)
462 		value |= MTK_STAR_BIT_INT_STS_TNTC;
463 	if (rx)
464 		value |= MTK_STAR_BIT_INT_STS_FNRC;
465 
466 	regmap_write(priv->regs, MTK_STAR_REG_INT_MASK, value);
467 }
468 
469 /* Unmask the three interrupts we care about, mask all others. */
470 static void mtk_star_intr_enable(struct mtk_star_priv *priv)
471 {
472 	unsigned int val = MTK_STAR_BIT_INT_STS_TNTC |
473 			   MTK_STAR_BIT_INT_STS_FNRC |
474 			   MTK_STAR_REG_INT_STS_MIB_CNT_TH;
475 
476 	regmap_write(priv->regs, MTK_STAR_REG_INT_MASK, ~val);
477 }
478 
479 static void mtk_star_intr_disable(struct mtk_star_priv *priv)
480 {
481 	regmap_write(priv->regs, MTK_STAR_REG_INT_MASK, ~0);
482 }
483 
484 static unsigned int mtk_star_intr_ack_all(struct mtk_star_priv *priv)
485 {
486 	unsigned int val;
487 
488 	regmap_read(priv->regs, MTK_STAR_REG_INT_STS, &val);
489 	regmap_write(priv->regs, MTK_STAR_REG_INT_STS, val);
490 
491 	return val;
492 }
493 
494 static void mtk_star_dma_init(struct mtk_star_priv *priv)
495 {
496 	struct mtk_star_ring_desc *desc;
497 	unsigned int val;
498 	int i;
499 
500 	priv->descs_base = (struct mtk_star_ring_desc *)priv->ring_base;
501 
502 	for (i = 0; i < MTK_STAR_NUM_DESCS_TOTAL; i++) {
503 		desc = &priv->descs_base[i];
504 
505 		memset(desc, 0, sizeof(*desc));
506 		desc->status = MTK_STAR_DESC_BIT_COWN;
507 		if ((i == MTK_STAR_NUM_TX_DESCS - 1) ||
508 		    (i == MTK_STAR_NUM_DESCS_TOTAL - 1))
509 			desc->status |= MTK_STAR_DESC_BIT_EOR;
510 	}
511 
512 	mtk_star_ring_init(&priv->tx_ring, priv->descs_base);
513 	mtk_star_ring_init(&priv->rx_ring,
514 			   priv->descs_base + MTK_STAR_NUM_TX_DESCS);
515 
516 	/* Set DMA pointers. */
517 	val = (unsigned int)priv->dma_addr;
518 	regmap_write(priv->regs, MTK_STAR_REG_TX_BASE_ADDR, val);
519 	regmap_write(priv->regs, MTK_STAR_REG_TX_DPTR, val);
520 
521 	val += sizeof(struct mtk_star_ring_desc) * MTK_STAR_NUM_TX_DESCS;
522 	regmap_write(priv->regs, MTK_STAR_REG_RX_BASE_ADDR, val);
523 	regmap_write(priv->regs, MTK_STAR_REG_RX_DPTR, val);
524 }
525 
526 static void mtk_star_dma_start(struct mtk_star_priv *priv)
527 {
528 	regmap_set_bits(priv->regs, MTK_STAR_REG_TX_DMA_CTRL,
529 			MTK_STAR_BIT_TX_DMA_CTRL_START);
530 	regmap_set_bits(priv->regs, MTK_STAR_REG_RX_DMA_CTRL,
531 			MTK_STAR_BIT_RX_DMA_CTRL_START);
532 }
533 
534 static void mtk_star_dma_stop(struct mtk_star_priv *priv)
535 {
536 	regmap_write(priv->regs, MTK_STAR_REG_TX_DMA_CTRL,
537 		     MTK_STAR_BIT_TX_DMA_CTRL_STOP);
538 	regmap_write(priv->regs, MTK_STAR_REG_RX_DMA_CTRL,
539 		     MTK_STAR_BIT_RX_DMA_CTRL_STOP);
540 }
541 
542 static void mtk_star_dma_disable(struct mtk_star_priv *priv)
543 {
544 	int i;
545 
546 	mtk_star_dma_stop(priv);
547 
548 	/* Take back all descriptors. */
549 	for (i = 0; i < MTK_STAR_NUM_DESCS_TOTAL; i++)
550 		priv->descs_base[i].status |= MTK_STAR_DESC_BIT_COWN;
551 }
552 
553 static void mtk_star_dma_resume_rx(struct mtk_star_priv *priv)
554 {
555 	regmap_set_bits(priv->regs, MTK_STAR_REG_RX_DMA_CTRL,
556 			MTK_STAR_BIT_RX_DMA_CTRL_RESUME);
557 }
558 
559 static void mtk_star_dma_resume_tx(struct mtk_star_priv *priv)
560 {
561 	regmap_set_bits(priv->regs, MTK_STAR_REG_TX_DMA_CTRL,
562 			MTK_STAR_BIT_TX_DMA_CTRL_RESUME);
563 }
564 
565 static void mtk_star_set_mac_addr(struct net_device *ndev)
566 {
567 	struct mtk_star_priv *priv = netdev_priv(ndev);
568 	const u8 *mac_addr = ndev->dev_addr;
569 	unsigned int high, low;
570 
571 	high = mac_addr[0] << 8 | mac_addr[1] << 0;
572 	low = mac_addr[2] << 24 | mac_addr[3] << 16 |
573 	      mac_addr[4] << 8 | mac_addr[5];
574 
575 	regmap_write(priv->regs, MTK_STAR_REG_MY_MAC_H, high);
576 	regmap_write(priv->regs, MTK_STAR_REG_MY_MAC_L, low);
577 }
578 
579 static void mtk_star_reset_counters(struct mtk_star_priv *priv)
580 {
581 	static const unsigned int counter_regs[] = {
582 		MTK_STAR_REG_C_RXOKPKT,
583 		MTK_STAR_REG_C_RXOKBYTE,
584 		MTK_STAR_REG_C_RXRUNT,
585 		MTK_STAR_REG_C_RXLONG,
586 		MTK_STAR_REG_C_RXDROP,
587 		MTK_STAR_REG_C_RXCRC,
588 		MTK_STAR_REG_C_RXARLDROP,
589 		MTK_STAR_REG_C_RXVLANDROP,
590 		MTK_STAR_REG_C_RXCSERR,
591 		MTK_STAR_REG_C_RXPAUSE,
592 		MTK_STAR_REG_C_TXOKPKT,
593 		MTK_STAR_REG_C_TXOKBYTE,
594 		MTK_STAR_REG_C_TXPAUSECOL,
595 		MTK_STAR_REG_C_TXRTY,
596 		MTK_STAR_REG_C_TXSKIP,
597 		MTK_STAR_REG_C_TX_ARP,
598 		MTK_STAR_REG_C_RX_RERR,
599 		MTK_STAR_REG_C_RX_UNI,
600 		MTK_STAR_REG_C_RX_MULTI,
601 		MTK_STAR_REG_C_RX_BROAD,
602 		MTK_STAR_REG_C_RX_ALIGNERR,
603 		MTK_STAR_REG_C_TX_UNI,
604 		MTK_STAR_REG_C_TX_MULTI,
605 		MTK_STAR_REG_C_TX_BROAD,
606 		MTK_STAR_REG_C_TX_TIMEOUT,
607 		MTK_STAR_REG_C_TX_LATECOL,
608 		MTK_STAR_REG_C_RX_LENGTHERR,
609 		MTK_STAR_REG_C_RX_TWIST,
610 	};
611 
612 	unsigned int i, val;
613 
614 	for (i = 0; i < ARRAY_SIZE(counter_regs); i++)
615 		regmap_read(priv->regs, counter_regs[i], &val);
616 }
617 
618 static void mtk_star_update_stat(struct mtk_star_priv *priv,
619 				 unsigned int reg, u64 *stat)
620 {
621 	unsigned int val;
622 
623 	regmap_read(priv->regs, reg, &val);
624 	*stat += val;
625 }
626 
627 /* Try to get as many stats as possible from the internal registers instead
628  * of tracking them ourselves.
629  */
630 static void mtk_star_update_stats(struct mtk_star_priv *priv)
631 {
632 	struct rtnl_link_stats64 *stats = &priv->stats;
633 
634 	/* OK packets and bytes. */
635 	mtk_star_update_stat(priv, MTK_STAR_REG_C_RXOKPKT, &stats->rx_packets);
636 	mtk_star_update_stat(priv, MTK_STAR_REG_C_TXOKPKT, &stats->tx_packets);
637 	mtk_star_update_stat(priv, MTK_STAR_REG_C_RXOKBYTE, &stats->rx_bytes);
638 	mtk_star_update_stat(priv, MTK_STAR_REG_C_TXOKBYTE, &stats->tx_bytes);
639 
640 	/* RX & TX multicast. */
641 	mtk_star_update_stat(priv, MTK_STAR_REG_C_RX_MULTI, &stats->multicast);
642 	mtk_star_update_stat(priv, MTK_STAR_REG_C_TX_MULTI, &stats->multicast);
643 
644 	/* Collisions. */
645 	mtk_star_update_stat(priv, MTK_STAR_REG_C_TXPAUSECOL,
646 			     &stats->collisions);
647 	mtk_star_update_stat(priv, MTK_STAR_REG_C_TX_LATECOL,
648 			     &stats->collisions);
649 	mtk_star_update_stat(priv, MTK_STAR_REG_C_RXRUNT, &stats->collisions);
650 
651 	/* RX Errors. */
652 	mtk_star_update_stat(priv, MTK_STAR_REG_C_RX_LENGTHERR,
653 			     &stats->rx_length_errors);
654 	mtk_star_update_stat(priv, MTK_STAR_REG_C_RXLONG,
655 			     &stats->rx_over_errors);
656 	mtk_star_update_stat(priv, MTK_STAR_REG_C_RXCRC, &stats->rx_crc_errors);
657 	mtk_star_update_stat(priv, MTK_STAR_REG_C_RX_ALIGNERR,
658 			     &stats->rx_frame_errors);
659 	mtk_star_update_stat(priv, MTK_STAR_REG_C_RXDROP,
660 			     &stats->rx_fifo_errors);
661 	/* Sum of the general RX error counter + all of the above. */
662 	mtk_star_update_stat(priv, MTK_STAR_REG_C_RX_RERR, &stats->rx_errors);
663 	stats->rx_errors += stats->rx_length_errors;
664 	stats->rx_errors += stats->rx_over_errors;
665 	stats->rx_errors += stats->rx_crc_errors;
666 	stats->rx_errors += stats->rx_frame_errors;
667 	stats->rx_errors += stats->rx_fifo_errors;
668 }
669 
670 static struct sk_buff *mtk_star_alloc_skb(struct net_device *ndev)
671 {
672 	uintptr_t tail, offset;
673 	struct sk_buff *skb;
674 
675 	skb = dev_alloc_skb(MTK_STAR_MAX_FRAME_SIZE);
676 	if (!skb)
677 		return NULL;
678 
679 	/* Align to 16 bytes. */
680 	tail = (uintptr_t)skb_tail_pointer(skb);
681 	if (tail & (MTK_STAR_SKB_ALIGNMENT - 1)) {
682 		offset = tail & (MTK_STAR_SKB_ALIGNMENT - 1);
683 		skb_reserve(skb, MTK_STAR_SKB_ALIGNMENT - offset);
684 	}
685 
686 	/* Ensure 16-byte alignment of the skb pointer: eth_type_trans() will
687 	 * extract the Ethernet header (14 bytes) so we need two more bytes.
688 	 */
689 	skb_reserve(skb, MTK_STAR_IP_ALIGN);
690 
691 	return skb;
692 }
693 
694 static int mtk_star_prepare_rx_skbs(struct net_device *ndev)
695 {
696 	struct mtk_star_priv *priv = netdev_priv(ndev);
697 	struct mtk_star_ring *ring = &priv->rx_ring;
698 	struct device *dev = mtk_star_get_dev(priv);
699 	struct mtk_star_ring_desc *desc;
700 	struct sk_buff *skb;
701 	dma_addr_t dma_addr;
702 	int i;
703 
704 	for (i = 0; i < MTK_STAR_NUM_RX_DESCS; i++) {
705 		skb = mtk_star_alloc_skb(ndev);
706 		if (!skb)
707 			return -ENOMEM;
708 
709 		dma_addr = mtk_star_dma_map_rx(priv, skb);
710 		if (dma_mapping_error(dev, dma_addr)) {
711 			dev_kfree_skb(skb);
712 			return -ENOMEM;
713 		}
714 
715 		desc = &ring->descs[i];
716 		desc->data_ptr = dma_addr;
717 		desc->status |= skb_tailroom(skb) & MTK_STAR_DESC_MSK_LEN;
718 		desc->status &= ~MTK_STAR_DESC_BIT_COWN;
719 		ring->skbs[i] = skb;
720 		ring->dma_addrs[i] = dma_addr;
721 	}
722 
723 	return 0;
724 }
725 
726 static void
727 mtk_star_ring_free_skbs(struct mtk_star_priv *priv, struct mtk_star_ring *ring,
728 			void (*unmap_func)(struct mtk_star_priv *,
729 					   struct mtk_star_ring_desc_data *))
730 {
731 	struct mtk_star_ring_desc_data desc_data;
732 	int i;
733 
734 	for (i = 0; i < MTK_STAR_RING_NUM_DESCS; i++) {
735 		if (!ring->dma_addrs[i])
736 			continue;
737 
738 		desc_data.dma_addr = ring->dma_addrs[i];
739 		desc_data.skb = ring->skbs[i];
740 
741 		unmap_func(priv, &desc_data);
742 		dev_kfree_skb(desc_data.skb);
743 	}
744 }
745 
746 static void mtk_star_free_rx_skbs(struct mtk_star_priv *priv)
747 {
748 	struct mtk_star_ring *ring = &priv->rx_ring;
749 
750 	mtk_star_ring_free_skbs(priv, ring, mtk_star_dma_unmap_rx);
751 }
752 
753 static void mtk_star_free_tx_skbs(struct mtk_star_priv *priv)
754 {
755 	struct mtk_star_ring *ring = &priv->tx_ring;
756 
757 	mtk_star_ring_free_skbs(priv, ring, mtk_star_dma_unmap_tx);
758 }
759 
760 /**
761  * mtk_star_handle_irq - Interrupt Handler.
762  * @irq: interrupt number.
763  * @data: pointer to a network interface device structure.
764  * Description : this is the driver interrupt service routine.
765  * it mainly handles:
766  *  1. tx complete interrupt for frame transmission.
767  *  2. rx complete interrupt for frame reception.
768  *  3. MAC Management Counter interrupt to avoid counter overflow.
769  **/
770 static irqreturn_t mtk_star_handle_irq(int irq, void *data)
771 {
772 	struct net_device *ndev = data;
773 	struct mtk_star_priv *priv = netdev_priv(ndev);
774 	unsigned int intr_status = mtk_star_intr_ack_all(priv);
775 	bool rx, tx;
776 
777 	rx = (intr_status & MTK_STAR_BIT_INT_STS_FNRC) &&
778 	     napi_schedule_prep(&priv->rx_napi);
779 	tx = (intr_status & MTK_STAR_BIT_INT_STS_TNTC) &&
780 	     napi_schedule_prep(&priv->tx_napi);
781 
782 	if (rx || tx) {
783 		spin_lock(&priv->lock);
784 		/* mask Rx and TX Complete interrupt */
785 		mtk_star_disable_dma_irq(priv, rx, tx);
786 		spin_unlock(&priv->lock);
787 
788 		if (rx)
789 			__napi_schedule(&priv->rx_napi);
790 		if (tx)
791 			__napi_schedule(&priv->tx_napi);
792 	}
793 
794 	/* interrupt is triggered once any counters reach 0x8000000 */
795 	if (intr_status & MTK_STAR_REG_INT_STS_MIB_CNT_TH) {
796 		mtk_star_update_stats(priv);
797 		mtk_star_reset_counters(priv);
798 	}
799 
800 	return IRQ_HANDLED;
801 }
802 
803 /* Wait for the completion of any previous command - CMD_START bit must be
804  * cleared by hardware.
805  */
806 static int mtk_star_hash_wait_cmd_start(struct mtk_star_priv *priv)
807 {
808 	unsigned int val;
809 
810 	return regmap_read_poll_timeout_atomic(priv->regs,
811 				MTK_STAR_REG_HASH_CTRL, val,
812 				!(val & MTK_STAR_BIT_HASH_CTRL_CMD_START),
813 				10, MTK_STAR_WAIT_TIMEOUT);
814 }
815 
816 static int mtk_star_hash_wait_ok(struct mtk_star_priv *priv)
817 {
818 	unsigned int val;
819 	int ret;
820 
821 	/* Wait for BIST_DONE bit. */
822 	ret = regmap_read_poll_timeout_atomic(priv->regs,
823 					MTK_STAR_REG_HASH_CTRL, val,
824 					val & MTK_STAR_BIT_HASH_CTRL_BIST_DONE,
825 					10, MTK_STAR_WAIT_TIMEOUT);
826 	if (ret)
827 		return ret;
828 
829 	/* Check the BIST_OK bit. */
830 	if (!regmap_test_bits(priv->regs, MTK_STAR_REG_HASH_CTRL,
831 			      MTK_STAR_BIT_HASH_CTRL_BIST_OK))
832 		return -EIO;
833 
834 	return 0;
835 }
836 
837 static int mtk_star_set_hashbit(struct mtk_star_priv *priv,
838 				unsigned int hash_addr)
839 {
840 	unsigned int val;
841 	int ret;
842 
843 	ret = mtk_star_hash_wait_cmd_start(priv);
844 	if (ret)
845 		return ret;
846 
847 	val = hash_addr & MTK_STAR_MSK_HASH_CTRL_HASH_BIT_ADDR;
848 	val |= MTK_STAR_BIT_HASH_CTRL_ACC_CMD;
849 	val |= MTK_STAR_BIT_HASH_CTRL_CMD_START;
850 	val |= MTK_STAR_BIT_HASH_CTRL_BIST_EN;
851 	val |= MTK_STAR_BIT_HASH_CTRL_HASH_BIT_DATA;
852 	regmap_write(priv->regs, MTK_STAR_REG_HASH_CTRL, val);
853 
854 	return mtk_star_hash_wait_ok(priv);
855 }
856 
857 static int mtk_star_reset_hash_table(struct mtk_star_priv *priv)
858 {
859 	int ret;
860 
861 	ret = mtk_star_hash_wait_cmd_start(priv);
862 	if (ret)
863 		return ret;
864 
865 	regmap_set_bits(priv->regs, MTK_STAR_REG_HASH_CTRL,
866 			MTK_STAR_BIT_HASH_CTRL_BIST_EN);
867 	regmap_set_bits(priv->regs, MTK_STAR_REG_TEST1,
868 			MTK_STAR_BIT_TEST1_RST_HASH_MBIST);
869 
870 	return mtk_star_hash_wait_ok(priv);
871 }
872 
873 static void mtk_star_phy_config(struct mtk_star_priv *priv)
874 {
875 	unsigned int val;
876 
877 	if (priv->speed == SPEED_1000)
878 		val = MTK_STAR_VAL_PHY_CTRL1_FORCE_SPD_1000M;
879 	else if (priv->speed == SPEED_100)
880 		val = MTK_STAR_VAL_PHY_CTRL1_FORCE_SPD_100M;
881 	else
882 		val = MTK_STAR_VAL_PHY_CTRL1_FORCE_SPD_10M;
883 	val <<= MTK_STAR_OFF_PHY_CTRL1_FORCE_SPD;
884 
885 	val |= MTK_STAR_BIT_PHY_CTRL1_AN_EN;
886 	if (priv->pause) {
887 		val |= MTK_STAR_BIT_PHY_CTRL1_FORCE_FC_RX;
888 		val |= MTK_STAR_BIT_PHY_CTRL1_FORCE_FC_TX;
889 		val |= MTK_STAR_BIT_PHY_CTRL1_FORCE_DPX;
890 	} else {
891 		val &= ~MTK_STAR_BIT_PHY_CTRL1_FORCE_FC_RX;
892 		val &= ~MTK_STAR_BIT_PHY_CTRL1_FORCE_FC_TX;
893 		val &= ~MTK_STAR_BIT_PHY_CTRL1_FORCE_DPX;
894 	}
895 	regmap_write(priv->regs, MTK_STAR_REG_PHY_CTRL1, val);
896 
897 	val = MTK_STAR_VAL_FC_CFG_SEND_PAUSE_TH_2K;
898 	val <<= MTK_STAR_OFF_FC_CFG_SEND_PAUSE_TH;
899 	val |= MTK_STAR_BIT_FC_CFG_UC_PAUSE_DIR;
900 	regmap_update_bits(priv->regs, MTK_STAR_REG_FC_CFG,
901 			   MTK_STAR_MSK_FC_CFG_SEND_PAUSE_TH |
902 			   MTK_STAR_BIT_FC_CFG_UC_PAUSE_DIR, val);
903 
904 	val = MTK_STAR_VAL_EXT_CFG_SND_PAUSE_RLS_1K;
905 	val <<= MTK_STAR_OFF_EXT_CFG_SND_PAUSE_RLS;
906 	regmap_update_bits(priv->regs, MTK_STAR_REG_EXT_CFG,
907 			   MTK_STAR_MSK_EXT_CFG_SND_PAUSE_RLS, val);
908 }
909 
910 static void mtk_star_adjust_link(struct net_device *ndev)
911 {
912 	struct mtk_star_priv *priv = netdev_priv(ndev);
913 	struct phy_device *phydev = priv->phydev;
914 	bool new_state = false;
915 
916 	if (phydev->link) {
917 		if (!priv->link) {
918 			priv->link = phydev->link;
919 			new_state = true;
920 		}
921 
922 		if (priv->speed != phydev->speed) {
923 			priv->speed = phydev->speed;
924 			new_state = true;
925 		}
926 
927 		if (priv->pause != phydev->pause) {
928 			priv->pause = phydev->pause;
929 			new_state = true;
930 		}
931 	} else {
932 		if (priv->link) {
933 			priv->link = phydev->link;
934 			new_state = true;
935 		}
936 	}
937 
938 	if (new_state) {
939 		if (phydev->link)
940 			mtk_star_phy_config(priv);
941 
942 		phy_print_status(ndev->phydev);
943 	}
944 }
945 
946 static void mtk_star_init_config(struct mtk_star_priv *priv)
947 {
948 	unsigned int val;
949 
950 	val = (MTK_STAR_BIT_MII_PAD_OUT_ENABLE |
951 	       MTK_STAR_BIT_EXT_MDC_MODE |
952 	       MTK_STAR_BIT_SWC_MII_MODE);
953 
954 	regmap_write(priv->regs, MTK_STAR_REG_SYS_CONF, val);
955 	regmap_update_bits(priv->regs, MTK_STAR_REG_MAC_CLK_CONF,
956 			   MTK_STAR_MSK_MAC_CLK_CONF,
957 			   priv->compat_data->bit_clk_div);
958 }
959 
960 static int mtk_star_enable(struct net_device *ndev)
961 {
962 	struct mtk_star_priv *priv = netdev_priv(ndev);
963 	unsigned int val;
964 	int ret;
965 
966 	mtk_star_nic_disable_pd(priv);
967 	mtk_star_intr_disable(priv);
968 	mtk_star_dma_stop(priv);
969 
970 	mtk_star_set_mac_addr(ndev);
971 
972 	/* Configure the MAC */
973 	val = MTK_STAR_VAL_MAC_CFG_IPG_96BIT;
974 	val <<= MTK_STAR_OFF_MAC_CFG_IPG;
975 	val |= MTK_STAR_BIT_MAC_CFG_MAXLEN_1522;
976 	val |= MTK_STAR_BIT_MAC_CFG_AUTO_PAD;
977 	val |= MTK_STAR_BIT_MAC_CFG_CRC_STRIP;
978 	regmap_write(priv->regs, MTK_STAR_REG_MAC_CFG, val);
979 
980 	/* Enable Hash Table BIST and reset it */
981 	ret = mtk_star_reset_hash_table(priv);
982 	if (ret)
983 		return ret;
984 
985 	/* Setup the hashing algorithm */
986 	regmap_clear_bits(priv->regs, MTK_STAR_REG_ARL_CFG,
987 			  MTK_STAR_BIT_ARL_CFG_HASH_ALG |
988 			  MTK_STAR_BIT_ARL_CFG_MISC_MODE);
989 
990 	/* Don't strip VLAN tags */
991 	regmap_clear_bits(priv->regs, MTK_STAR_REG_MAC_CFG,
992 			  MTK_STAR_BIT_MAC_CFG_VLAN_STRIP);
993 
994 	/* Setup DMA */
995 	mtk_star_dma_init(priv);
996 
997 	ret = mtk_star_prepare_rx_skbs(ndev);
998 	if (ret)
999 		goto err_out;
1000 
1001 	/* Request the interrupt */
1002 	ret = request_irq(ndev->irq, mtk_star_handle_irq,
1003 			  IRQF_TRIGGER_NONE, ndev->name, ndev);
1004 	if (ret)
1005 		goto err_free_skbs;
1006 
1007 	napi_enable(&priv->tx_napi);
1008 	napi_enable(&priv->rx_napi);
1009 
1010 	mtk_star_intr_ack_all(priv);
1011 	mtk_star_intr_enable(priv);
1012 
1013 	/* Connect to and start PHY */
1014 	priv->phydev = of_phy_connect(ndev, priv->phy_node,
1015 				      mtk_star_adjust_link, 0, priv->phy_intf);
1016 	if (!priv->phydev) {
1017 		netdev_err(ndev, "failed to connect to PHY\n");
1018 		ret = -ENODEV;
1019 		goto err_free_irq;
1020 	}
1021 
1022 	mtk_star_dma_start(priv);
1023 	phy_start(priv->phydev);
1024 	netif_start_queue(ndev);
1025 
1026 	return 0;
1027 
1028 err_free_irq:
1029 	napi_disable(&priv->rx_napi);
1030 	napi_disable(&priv->tx_napi);
1031 	free_irq(ndev->irq, ndev);
1032 err_free_skbs:
1033 	mtk_star_free_rx_skbs(priv);
1034 err_out:
1035 	return ret;
1036 }
1037 
1038 static void mtk_star_disable(struct net_device *ndev)
1039 {
1040 	struct mtk_star_priv *priv = netdev_priv(ndev);
1041 
1042 	netif_stop_queue(ndev);
1043 	napi_disable(&priv->tx_napi);
1044 	napi_disable(&priv->rx_napi);
1045 	mtk_star_intr_disable(priv);
1046 	mtk_star_dma_disable(priv);
1047 	mtk_star_intr_ack_all(priv);
1048 	phy_stop(priv->phydev);
1049 	phy_disconnect(priv->phydev);
1050 	free_irq(ndev->irq, ndev);
1051 	mtk_star_free_rx_skbs(priv);
1052 	mtk_star_free_tx_skbs(priv);
1053 }
1054 
1055 static int mtk_star_netdev_open(struct net_device *ndev)
1056 {
1057 	return mtk_star_enable(ndev);
1058 }
1059 
1060 static int mtk_star_netdev_stop(struct net_device *ndev)
1061 {
1062 	mtk_star_disable(ndev);
1063 
1064 	return 0;
1065 }
1066 
1067 static int mtk_star_netdev_ioctl(struct net_device *ndev,
1068 				 struct ifreq *req, int cmd)
1069 {
1070 	if (!netif_running(ndev))
1071 		return -EINVAL;
1072 
1073 	return phy_mii_ioctl(ndev->phydev, req, cmd);
1074 }
1075 
1076 static int __mtk_star_maybe_stop_tx(struct mtk_star_priv *priv, u16 size)
1077 {
1078 	netif_stop_queue(priv->ndev);
1079 
1080 	/* Might race with mtk_star_tx_poll, check again */
1081 	smp_mb();
1082 	if (likely(mtk_star_tx_ring_avail(&priv->tx_ring) < size))
1083 		return -EBUSY;
1084 
1085 	netif_start_queue(priv->ndev);
1086 
1087 	return 0;
1088 }
1089 
1090 static inline int mtk_star_maybe_stop_tx(struct mtk_star_priv *priv, u16 size)
1091 {
1092 	if (likely(mtk_star_tx_ring_avail(&priv->tx_ring) >= size))
1093 		return 0;
1094 
1095 	return __mtk_star_maybe_stop_tx(priv, size);
1096 }
1097 
1098 static netdev_tx_t mtk_star_netdev_start_xmit(struct sk_buff *skb,
1099 					      struct net_device *ndev)
1100 {
1101 	struct mtk_star_priv *priv = netdev_priv(ndev);
1102 	struct mtk_star_ring *ring = &priv->tx_ring;
1103 	struct device *dev = mtk_star_get_dev(priv);
1104 	struct mtk_star_ring_desc_data desc_data;
1105 	int nfrags = skb_shinfo(skb)->nr_frags;
1106 
1107 	if (unlikely(mtk_star_tx_ring_avail(ring) < nfrags + 1)) {
1108 		if (!netif_queue_stopped(ndev)) {
1109 			netif_stop_queue(ndev);
1110 			/* This is a hard error, log it. */
1111 			pr_err_ratelimited("Tx ring full when queue awake\n");
1112 		}
1113 		return NETDEV_TX_BUSY;
1114 	}
1115 
1116 	desc_data.dma_addr = mtk_star_dma_map_tx(priv, skb);
1117 	if (dma_mapping_error(dev, desc_data.dma_addr))
1118 		goto err_drop_packet;
1119 
1120 	desc_data.skb = skb;
1121 	desc_data.len = skb->len;
1122 	mtk_star_ring_push_head_tx(ring, &desc_data);
1123 
1124 	netdev_sent_queue(ndev, skb->len);
1125 
1126 	mtk_star_maybe_stop_tx(priv, MTK_STAR_DESC_NEEDED);
1127 
1128 	mtk_star_dma_resume_tx(priv);
1129 
1130 	return NETDEV_TX_OK;
1131 
1132 err_drop_packet:
1133 	dev_kfree_skb(skb);
1134 	ndev->stats.tx_dropped++;
1135 	return NETDEV_TX_OK;
1136 }
1137 
1138 /* Returns the number of bytes sent or a negative number on the first
1139  * descriptor owned by DMA.
1140  */
1141 static int mtk_star_tx_complete_one(struct mtk_star_priv *priv)
1142 {
1143 	struct mtk_star_ring *ring = &priv->tx_ring;
1144 	struct mtk_star_ring_desc_data desc_data;
1145 	int ret;
1146 
1147 	ret = mtk_star_ring_pop_tail(ring, &desc_data);
1148 	if (ret)
1149 		return ret;
1150 
1151 	mtk_star_dma_unmap_tx(priv, &desc_data);
1152 	ret = desc_data.skb->len;
1153 	dev_kfree_skb_irq(desc_data.skb);
1154 
1155 	return ret;
1156 }
1157 
1158 static int mtk_star_tx_poll(struct napi_struct *napi, int budget)
1159 {
1160 	struct mtk_star_priv *priv = container_of(napi, struct mtk_star_priv,
1161 						  tx_napi);
1162 	int ret = 0, pkts_compl = 0, bytes_compl = 0, count = 0;
1163 	struct mtk_star_ring *ring = &priv->tx_ring;
1164 	struct net_device *ndev = priv->ndev;
1165 	unsigned int head = ring->head;
1166 	unsigned int entry = ring->tail;
1167 
1168 	while (entry != head && count < (MTK_STAR_RING_NUM_DESCS - 1)) {
1169 		ret = mtk_star_tx_complete_one(priv);
1170 		if (ret < 0)
1171 			break;
1172 
1173 		count++;
1174 		pkts_compl++;
1175 		bytes_compl += ret;
1176 		entry = ring->tail;
1177 	}
1178 
1179 	netdev_completed_queue(ndev, pkts_compl, bytes_compl);
1180 
1181 	if (unlikely(netif_queue_stopped(ndev)) &&
1182 	    (mtk_star_tx_ring_avail(ring) > MTK_STAR_TX_THRESH))
1183 		netif_wake_queue(ndev);
1184 
1185 	if (napi_complete(napi)) {
1186 		spin_lock(&priv->lock);
1187 		mtk_star_enable_dma_irq(priv, false, true);
1188 		spin_unlock(&priv->lock);
1189 	}
1190 
1191 	return 0;
1192 }
1193 
1194 static void mtk_star_netdev_get_stats64(struct net_device *ndev,
1195 					struct rtnl_link_stats64 *stats)
1196 {
1197 	struct mtk_star_priv *priv = netdev_priv(ndev);
1198 
1199 	mtk_star_update_stats(priv);
1200 
1201 	memcpy(stats, &priv->stats, sizeof(*stats));
1202 }
1203 
1204 static void mtk_star_set_rx_mode(struct net_device *ndev)
1205 {
1206 	struct mtk_star_priv *priv = netdev_priv(ndev);
1207 	struct netdev_hw_addr *hw_addr;
1208 	unsigned int hash_addr, i;
1209 	int ret;
1210 
1211 	if (ndev->flags & IFF_PROMISC) {
1212 		regmap_set_bits(priv->regs, MTK_STAR_REG_ARL_CFG,
1213 				MTK_STAR_BIT_ARL_CFG_MISC_MODE);
1214 	} else if (netdev_mc_count(ndev) > MTK_STAR_HASHTABLE_MC_LIMIT ||
1215 		   ndev->flags & IFF_ALLMULTI) {
1216 		for (i = 0; i < MTK_STAR_HASHTABLE_SIZE_MAX; i++) {
1217 			ret = mtk_star_set_hashbit(priv, i);
1218 			if (ret)
1219 				goto hash_fail;
1220 		}
1221 	} else {
1222 		/* Clear previous settings. */
1223 		ret = mtk_star_reset_hash_table(priv);
1224 		if (ret)
1225 			goto hash_fail;
1226 
1227 		netdev_for_each_mc_addr(hw_addr, ndev) {
1228 			hash_addr = (hw_addr->addr[0] & 0x01) << 8;
1229 			hash_addr += hw_addr->addr[5];
1230 			ret = mtk_star_set_hashbit(priv, hash_addr);
1231 			if (ret)
1232 				goto hash_fail;
1233 		}
1234 	}
1235 
1236 	return;
1237 
1238 hash_fail:
1239 	if (ret == -ETIMEDOUT)
1240 		netdev_err(ndev, "setting hash bit timed out\n");
1241 	else
1242 		/* Should be -EIO */
1243 		netdev_err(ndev, "unable to set hash bit");
1244 }
1245 
1246 static const struct net_device_ops mtk_star_netdev_ops = {
1247 	.ndo_open		= mtk_star_netdev_open,
1248 	.ndo_stop		= mtk_star_netdev_stop,
1249 	.ndo_start_xmit		= mtk_star_netdev_start_xmit,
1250 	.ndo_get_stats64	= mtk_star_netdev_get_stats64,
1251 	.ndo_set_rx_mode	= mtk_star_set_rx_mode,
1252 	.ndo_eth_ioctl		= mtk_star_netdev_ioctl,
1253 	.ndo_set_mac_address	= eth_mac_addr,
1254 	.ndo_validate_addr	= eth_validate_addr,
1255 };
1256 
1257 static void mtk_star_get_drvinfo(struct net_device *dev,
1258 				 struct ethtool_drvinfo *info)
1259 {
1260 	strscpy(info->driver, MTK_STAR_DRVNAME, sizeof(info->driver));
1261 }
1262 
1263 /* TODO Add ethtool stats. */
1264 static const struct ethtool_ops mtk_star_ethtool_ops = {
1265 	.get_drvinfo		= mtk_star_get_drvinfo,
1266 	.get_link		= ethtool_op_get_link,
1267 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1268 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
1269 };
1270 
1271 static int mtk_star_rx(struct mtk_star_priv *priv, int budget)
1272 {
1273 	struct mtk_star_ring *ring = &priv->rx_ring;
1274 	struct device *dev = mtk_star_get_dev(priv);
1275 	struct mtk_star_ring_desc_data desc_data;
1276 	struct net_device *ndev = priv->ndev;
1277 	struct sk_buff *curr_skb, *new_skb;
1278 	dma_addr_t new_dma_addr;
1279 	int ret, count = 0;
1280 
1281 	while (count < budget) {
1282 		ret = mtk_star_ring_pop_tail(ring, &desc_data);
1283 		if (ret)
1284 			return -1;
1285 
1286 		curr_skb = desc_data.skb;
1287 
1288 		if ((desc_data.flags & MTK_STAR_DESC_BIT_RX_CRCE) ||
1289 		    (desc_data.flags & MTK_STAR_DESC_BIT_RX_OSIZE)) {
1290 			/* Error packet -> drop and reuse skb. */
1291 			new_skb = curr_skb;
1292 			goto push_new_skb;
1293 		}
1294 
1295 		/* Prepare new skb before receiving the current one.
1296 		 * Reuse the current skb if we fail at any point.
1297 		 */
1298 		new_skb = mtk_star_alloc_skb(ndev);
1299 		if (!new_skb) {
1300 			ndev->stats.rx_dropped++;
1301 			new_skb = curr_skb;
1302 			goto push_new_skb;
1303 		}
1304 
1305 		new_dma_addr = mtk_star_dma_map_rx(priv, new_skb);
1306 		if (dma_mapping_error(dev, new_dma_addr)) {
1307 			ndev->stats.rx_dropped++;
1308 			dev_kfree_skb(new_skb);
1309 			new_skb = curr_skb;
1310 			netdev_err(ndev, "DMA mapping error of RX descriptor\n");
1311 			goto push_new_skb;
1312 		}
1313 
1314 		/* We can't fail anymore at this point:
1315 		 * it's safe to unmap the skb.
1316 		 */
1317 		mtk_star_dma_unmap_rx(priv, &desc_data);
1318 
1319 		skb_put(desc_data.skb, desc_data.len);
1320 		desc_data.skb->ip_summed = CHECKSUM_NONE;
1321 		desc_data.skb->protocol = eth_type_trans(desc_data.skb, ndev);
1322 		desc_data.skb->dev = ndev;
1323 		netif_receive_skb(desc_data.skb);
1324 
1325 		/* update dma_addr for new skb */
1326 		desc_data.dma_addr = new_dma_addr;
1327 
1328 push_new_skb:
1329 
1330 		count++;
1331 
1332 		desc_data.len = skb_tailroom(new_skb);
1333 		desc_data.skb = new_skb;
1334 		mtk_star_ring_push_head_rx(ring, &desc_data);
1335 	}
1336 
1337 	mtk_star_dma_resume_rx(priv);
1338 
1339 	return count;
1340 }
1341 
1342 static int mtk_star_rx_poll(struct napi_struct *napi, int budget)
1343 {
1344 	struct mtk_star_priv *priv;
1345 	int work_done = 0;
1346 
1347 	priv = container_of(napi, struct mtk_star_priv, rx_napi);
1348 
1349 	work_done = mtk_star_rx(priv, budget);
1350 	if (work_done < budget) {
1351 		napi_complete_done(napi, work_done);
1352 		spin_lock(&priv->lock);
1353 		mtk_star_enable_dma_irq(priv, true, false);
1354 		spin_unlock(&priv->lock);
1355 	}
1356 
1357 	return work_done;
1358 }
1359 
1360 static void mtk_star_mdio_rwok_clear(struct mtk_star_priv *priv)
1361 {
1362 	regmap_write(priv->regs, MTK_STAR_REG_PHY_CTRL0,
1363 		     MTK_STAR_BIT_PHY_CTRL0_RWOK);
1364 }
1365 
1366 static int mtk_star_mdio_rwok_wait(struct mtk_star_priv *priv)
1367 {
1368 	unsigned int val;
1369 
1370 	return regmap_read_poll_timeout(priv->regs, MTK_STAR_REG_PHY_CTRL0,
1371 					val, val & MTK_STAR_BIT_PHY_CTRL0_RWOK,
1372 					10, MTK_STAR_WAIT_TIMEOUT);
1373 }
1374 
1375 static int mtk_star_mdio_read(struct mii_bus *mii, int phy_id, int regnum)
1376 {
1377 	struct mtk_star_priv *priv = mii->priv;
1378 	unsigned int val, data;
1379 	int ret;
1380 
1381 	if (regnum & MII_ADDR_C45)
1382 		return -EOPNOTSUPP;
1383 
1384 	mtk_star_mdio_rwok_clear(priv);
1385 
1386 	val = (regnum << MTK_STAR_OFF_PHY_CTRL0_PREG);
1387 	val &= MTK_STAR_MSK_PHY_CTRL0_PREG;
1388 	val |= MTK_STAR_BIT_PHY_CTRL0_RDCMD;
1389 
1390 	regmap_write(priv->regs, MTK_STAR_REG_PHY_CTRL0, val);
1391 
1392 	ret = mtk_star_mdio_rwok_wait(priv);
1393 	if (ret)
1394 		return ret;
1395 
1396 	regmap_read(priv->regs, MTK_STAR_REG_PHY_CTRL0, &data);
1397 
1398 	data &= MTK_STAR_MSK_PHY_CTRL0_RWDATA;
1399 	data >>= MTK_STAR_OFF_PHY_CTRL0_RWDATA;
1400 
1401 	return data;
1402 }
1403 
1404 static int mtk_star_mdio_write(struct mii_bus *mii, int phy_id,
1405 			       int regnum, u16 data)
1406 {
1407 	struct mtk_star_priv *priv = mii->priv;
1408 	unsigned int val;
1409 
1410 	if (regnum & MII_ADDR_C45)
1411 		return -EOPNOTSUPP;
1412 
1413 	mtk_star_mdio_rwok_clear(priv);
1414 
1415 	val = data;
1416 	val <<= MTK_STAR_OFF_PHY_CTRL0_RWDATA;
1417 	val &= MTK_STAR_MSK_PHY_CTRL0_RWDATA;
1418 	regnum <<= MTK_STAR_OFF_PHY_CTRL0_PREG;
1419 	regnum &= MTK_STAR_MSK_PHY_CTRL0_PREG;
1420 	val |= regnum;
1421 	val |= MTK_STAR_BIT_PHY_CTRL0_WTCMD;
1422 
1423 	regmap_write(priv->regs, MTK_STAR_REG_PHY_CTRL0, val);
1424 
1425 	return mtk_star_mdio_rwok_wait(priv);
1426 }
1427 
1428 static int mtk_star_mdio_init(struct net_device *ndev)
1429 {
1430 	struct mtk_star_priv *priv = netdev_priv(ndev);
1431 	struct device *dev = mtk_star_get_dev(priv);
1432 	struct device_node *of_node, *mdio_node;
1433 	int ret;
1434 
1435 	of_node = dev->of_node;
1436 
1437 	mdio_node = of_get_child_by_name(of_node, "mdio");
1438 	if (!mdio_node)
1439 		return -ENODEV;
1440 
1441 	if (!of_device_is_available(mdio_node)) {
1442 		ret = -ENODEV;
1443 		goto out_put_node;
1444 	}
1445 
1446 	priv->mii = devm_mdiobus_alloc(dev);
1447 	if (!priv->mii) {
1448 		ret = -ENOMEM;
1449 		goto out_put_node;
1450 	}
1451 
1452 	snprintf(priv->mii->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
1453 	priv->mii->name = "mtk-mac-mdio";
1454 	priv->mii->parent = dev;
1455 	priv->mii->read = mtk_star_mdio_read;
1456 	priv->mii->write = mtk_star_mdio_write;
1457 	priv->mii->priv = priv;
1458 
1459 	ret = devm_of_mdiobus_register(dev, priv->mii, mdio_node);
1460 
1461 out_put_node:
1462 	of_node_put(mdio_node);
1463 	return ret;
1464 }
1465 
1466 static __maybe_unused int mtk_star_suspend(struct device *dev)
1467 {
1468 	struct mtk_star_priv *priv;
1469 	struct net_device *ndev;
1470 
1471 	ndev = dev_get_drvdata(dev);
1472 	priv = netdev_priv(ndev);
1473 
1474 	if (netif_running(ndev))
1475 		mtk_star_disable(ndev);
1476 
1477 	clk_bulk_disable_unprepare(MTK_STAR_NCLKS, priv->clks);
1478 
1479 	return 0;
1480 }
1481 
1482 static __maybe_unused int mtk_star_resume(struct device *dev)
1483 {
1484 	struct mtk_star_priv *priv;
1485 	struct net_device *ndev;
1486 	int ret;
1487 
1488 	ndev = dev_get_drvdata(dev);
1489 	priv = netdev_priv(ndev);
1490 
1491 	ret = clk_bulk_prepare_enable(MTK_STAR_NCLKS, priv->clks);
1492 	if (ret)
1493 		return ret;
1494 
1495 	if (netif_running(ndev)) {
1496 		ret = mtk_star_enable(ndev);
1497 		if (ret)
1498 			clk_bulk_disable_unprepare(MTK_STAR_NCLKS, priv->clks);
1499 	}
1500 
1501 	return ret;
1502 }
1503 
1504 static void mtk_star_clk_disable_unprepare(void *data)
1505 {
1506 	struct mtk_star_priv *priv = data;
1507 
1508 	clk_bulk_disable_unprepare(MTK_STAR_NCLKS, priv->clks);
1509 }
1510 
1511 static int mtk_star_set_timing(struct mtk_star_priv *priv)
1512 {
1513 	struct device *dev = mtk_star_get_dev(priv);
1514 	unsigned int delay_val = 0;
1515 
1516 	switch (priv->phy_intf) {
1517 	case PHY_INTERFACE_MODE_MII:
1518 	case PHY_INTERFACE_MODE_RMII:
1519 		delay_val |= FIELD_PREP(MTK_STAR_BIT_INV_RX_CLK, priv->rx_inv);
1520 		delay_val |= FIELD_PREP(MTK_STAR_BIT_INV_TX_CLK, priv->tx_inv);
1521 		break;
1522 	default:
1523 		dev_err(dev, "This interface not supported\n");
1524 		return -EINVAL;
1525 	}
1526 
1527 	return regmap_write(priv->regs, MTK_STAR_REG_TEST0, delay_val);
1528 }
1529 
1530 static int mtk_star_probe(struct platform_device *pdev)
1531 {
1532 	struct device_node *of_node;
1533 	struct mtk_star_priv *priv;
1534 	struct net_device *ndev;
1535 	struct device *dev;
1536 	void __iomem *base;
1537 	int ret, i;
1538 
1539 	dev = &pdev->dev;
1540 	of_node = dev->of_node;
1541 
1542 	ndev = devm_alloc_etherdev(dev, sizeof(*priv));
1543 	if (!ndev)
1544 		return -ENOMEM;
1545 
1546 	priv = netdev_priv(ndev);
1547 	priv->ndev = ndev;
1548 	priv->compat_data = of_device_get_match_data(&pdev->dev);
1549 	SET_NETDEV_DEV(ndev, dev);
1550 	platform_set_drvdata(pdev, ndev);
1551 
1552 	ndev->min_mtu = ETH_ZLEN;
1553 	ndev->max_mtu = MTK_STAR_MAX_FRAME_SIZE;
1554 
1555 	spin_lock_init(&priv->lock);
1556 
1557 	base = devm_platform_ioremap_resource(pdev, 0);
1558 	if (IS_ERR(base))
1559 		return PTR_ERR(base);
1560 
1561 	/* We won't be checking the return values of regmap read & write
1562 	 * functions. They can only fail for mmio if there's a clock attached
1563 	 * to regmap which is not the case here.
1564 	 */
1565 	priv->regs = devm_regmap_init_mmio(dev, base,
1566 					   &mtk_star_regmap_config);
1567 	if (IS_ERR(priv->regs))
1568 		return PTR_ERR(priv->regs);
1569 
1570 	priv->pericfg = syscon_regmap_lookup_by_phandle(of_node,
1571 							"mediatek,pericfg");
1572 	if (IS_ERR(priv->pericfg)) {
1573 		dev_err(dev, "Failed to lookup the PERICFG syscon\n");
1574 		return PTR_ERR(priv->pericfg);
1575 	}
1576 
1577 	ndev->irq = platform_get_irq(pdev, 0);
1578 	if (ndev->irq < 0)
1579 		return ndev->irq;
1580 
1581 	for (i = 0; i < MTK_STAR_NCLKS; i++)
1582 		priv->clks[i].id = mtk_star_clk_names[i];
1583 	ret = devm_clk_bulk_get(dev, MTK_STAR_NCLKS, priv->clks);
1584 	if (ret)
1585 		return ret;
1586 
1587 	ret = clk_bulk_prepare_enable(MTK_STAR_NCLKS, priv->clks);
1588 	if (ret)
1589 		return ret;
1590 
1591 	ret = devm_add_action_or_reset(dev,
1592 				       mtk_star_clk_disable_unprepare, priv);
1593 	if (ret)
1594 		return ret;
1595 
1596 	ret = of_get_phy_mode(of_node, &priv->phy_intf);
1597 	if (ret) {
1598 		return ret;
1599 	} else if (priv->phy_intf != PHY_INTERFACE_MODE_RMII &&
1600 		   priv->phy_intf != PHY_INTERFACE_MODE_MII) {
1601 		dev_err(dev, "unsupported phy mode: %s\n",
1602 			phy_modes(priv->phy_intf));
1603 		return -EINVAL;
1604 	}
1605 
1606 	priv->phy_node = of_parse_phandle(of_node, "phy-handle", 0);
1607 	if (!priv->phy_node) {
1608 		dev_err(dev, "failed to retrieve the phy handle from device tree\n");
1609 		return -ENODEV;
1610 	}
1611 
1612 	priv->rmii_rxc = of_property_read_bool(of_node, "mediatek,rmii-rxc");
1613 	priv->rx_inv = of_property_read_bool(of_node, "mediatek,rxc-inverse");
1614 	priv->tx_inv = of_property_read_bool(of_node, "mediatek,txc-inverse");
1615 
1616 	if (priv->compat_data->set_interface_mode) {
1617 		ret = priv->compat_data->set_interface_mode(ndev);
1618 		if (ret) {
1619 			dev_err(dev, "Failed to set phy interface, err = %d\n", ret);
1620 			return -EINVAL;
1621 		}
1622 	}
1623 
1624 	ret = mtk_star_set_timing(priv);
1625 	if (ret) {
1626 		dev_err(dev, "Failed to set timing, err = %d\n", ret);
1627 		return -EINVAL;
1628 	}
1629 
1630 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
1631 	if (ret) {
1632 		dev_err(dev, "unsupported DMA mask\n");
1633 		return ret;
1634 	}
1635 
1636 	priv->ring_base = dmam_alloc_coherent(dev, MTK_STAR_DMA_SIZE,
1637 					      &priv->dma_addr,
1638 					      GFP_KERNEL | GFP_DMA);
1639 	if (!priv->ring_base)
1640 		return -ENOMEM;
1641 
1642 	mtk_star_nic_disable_pd(priv);
1643 	mtk_star_init_config(priv);
1644 
1645 	ret = mtk_star_mdio_init(ndev);
1646 	if (ret)
1647 		return ret;
1648 
1649 	ret = platform_get_ethdev_address(dev, ndev);
1650 	if (ret || !is_valid_ether_addr(ndev->dev_addr))
1651 		eth_hw_addr_random(ndev);
1652 
1653 	ndev->netdev_ops = &mtk_star_netdev_ops;
1654 	ndev->ethtool_ops = &mtk_star_ethtool_ops;
1655 
1656 	netif_napi_add(ndev, &priv->rx_napi, mtk_star_rx_poll);
1657 	netif_napi_add_tx(ndev, &priv->tx_napi, mtk_star_tx_poll);
1658 
1659 	return devm_register_netdev(dev, ndev);
1660 }
1661 
1662 #ifdef CONFIG_OF
1663 static int mt8516_set_interface_mode(struct net_device *ndev)
1664 {
1665 	struct mtk_star_priv *priv = netdev_priv(ndev);
1666 	struct device *dev = mtk_star_get_dev(priv);
1667 	unsigned int intf_val, ret, rmii_rxc;
1668 
1669 	switch (priv->phy_intf) {
1670 	case PHY_INTERFACE_MODE_MII:
1671 		intf_val = MTK_PERICFG_BIT_NIC_CFG_CON_MII;
1672 		rmii_rxc = 0;
1673 		break;
1674 	case PHY_INTERFACE_MODE_RMII:
1675 		intf_val = MTK_PERICFG_BIT_NIC_CFG_CON_RMII;
1676 		rmii_rxc = priv->rmii_rxc ? 0 : MTK_PERICFG_BIT_NIC_CFG_CON_CLK;
1677 		break;
1678 	default:
1679 		dev_err(dev, "This interface not supported\n");
1680 		return -EINVAL;
1681 	}
1682 
1683 	ret = regmap_update_bits(priv->pericfg,
1684 				 MTK_PERICFG_REG_NIC_CFG1_CON,
1685 				 MTK_PERICFG_BIT_NIC_CFG_CON_CLK,
1686 				 rmii_rxc);
1687 	if (ret)
1688 		return ret;
1689 
1690 	return regmap_update_bits(priv->pericfg,
1691 				  MTK_PERICFG_REG_NIC_CFG0_CON,
1692 				  MTK_PERICFG_REG_NIC_CFG_CON_CFG_INTF,
1693 				  intf_val);
1694 }
1695 
1696 static int mt8365_set_interface_mode(struct net_device *ndev)
1697 {
1698 	struct mtk_star_priv *priv = netdev_priv(ndev);
1699 	struct device *dev = mtk_star_get_dev(priv);
1700 	unsigned int intf_val;
1701 
1702 	switch (priv->phy_intf) {
1703 	case PHY_INTERFACE_MODE_MII:
1704 		intf_val = MTK_PERICFG_BIT_NIC_CFG_CON_MII;
1705 		break;
1706 	case PHY_INTERFACE_MODE_RMII:
1707 		intf_val = MTK_PERICFG_BIT_NIC_CFG_CON_RMII;
1708 		intf_val |= priv->rmii_rxc ? 0 : MTK_PERICFG_BIT_NIC_CFG_CON_CLK_V2;
1709 		break;
1710 	default:
1711 		dev_err(dev, "This interface not supported\n");
1712 		return -EINVAL;
1713 	}
1714 
1715 	return regmap_update_bits(priv->pericfg,
1716 				  MTK_PERICFG_REG_NIC_CFG_CON_V2,
1717 				  MTK_PERICFG_REG_NIC_CFG_CON_CFG_INTF |
1718 				  MTK_PERICFG_BIT_NIC_CFG_CON_CLK_V2,
1719 				  intf_val);
1720 }
1721 
1722 static const struct mtk_star_compat mtk_star_mt8516_compat = {
1723 	.set_interface_mode = mt8516_set_interface_mode,
1724 	.bit_clk_div = MTK_STAR_BIT_CLK_DIV_10,
1725 };
1726 
1727 static const struct mtk_star_compat mtk_star_mt8365_compat = {
1728 	.set_interface_mode = mt8365_set_interface_mode,
1729 	.bit_clk_div = MTK_STAR_BIT_CLK_DIV_50,
1730 };
1731 
1732 static const struct of_device_id mtk_star_of_match[] = {
1733 	{ .compatible = "mediatek,mt8516-eth",
1734 	  .data = &mtk_star_mt8516_compat },
1735 	{ .compatible = "mediatek,mt8518-eth",
1736 	  .data = &mtk_star_mt8516_compat },
1737 	{ .compatible = "mediatek,mt8175-eth",
1738 	  .data = &mtk_star_mt8516_compat },
1739 	{ .compatible = "mediatek,mt8365-eth",
1740 	  .data = &mtk_star_mt8365_compat },
1741 	{ }
1742 };
1743 MODULE_DEVICE_TABLE(of, mtk_star_of_match);
1744 #endif
1745 
1746 static SIMPLE_DEV_PM_OPS(mtk_star_pm_ops,
1747 			 mtk_star_suspend, mtk_star_resume);
1748 
1749 static struct platform_driver mtk_star_driver = {
1750 	.driver = {
1751 		.name = MTK_STAR_DRVNAME,
1752 		.pm = &mtk_star_pm_ops,
1753 		.of_match_table = of_match_ptr(mtk_star_of_match),
1754 	},
1755 	.probe = mtk_star_probe,
1756 };
1757 module_platform_driver(mtk_star_driver);
1758 
1759 MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
1760 MODULE_DESCRIPTION("Mediatek STAR Ethernet MAC Driver");
1761 MODULE_LICENSE("GPL");
1762