blob: 54674adae7254861482d51f0f5714559d2bfe4d5 [file] [log] [blame]
developerfd40db22021-04-29 10:08:25 +08001/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 *
4 * Copyright (C) 2009-2016 John Crispin <blogic@openwrt.org>
5 * Copyright (C) 2009-2016 Felix Fietkau <nbd@openwrt.org>
6 * Copyright (C) 2013-2016 Michael Lee <igvtee@gmail.com>
7 */
8
9#ifndef MTK_ETH_H
10#define MTK_ETH_H
11
12#include <linux/dma-mapping.h>
13#include <linux/netdevice.h>
14#include <linux/of_net.h>
15#include <linux/u64_stats_sync.h>
16#include <linux/refcount.h>
17#include <linux/phylink.h>
18
19#define MTK_QDMA_PAGE_SIZE 2048
20#define MTK_MAX_RX_LENGTH 1536
21#define MTK_DMA_SIZE 2048
22#define MTK_NAPI_WEIGHT 256
23#define MTK_MAC_COUNT 2
24#define MTK_RX_ETH_HLEN (VLAN_ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN)
25#define MTK_RX_HLEN (NET_SKB_PAD + MTK_RX_ETH_HLEN + NET_IP_ALIGN)
26#define MTK_DMA_DUMMY_DESC 0xffffffff
27#define MTK_DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | \
28 NETIF_MSG_PROBE | \
29 NETIF_MSG_LINK | \
30 NETIF_MSG_TIMER | \
31 NETIF_MSG_IFDOWN | \
32 NETIF_MSG_IFUP | \
33 NETIF_MSG_RX_ERR | \
34 NETIF_MSG_TX_ERR)
35#define MTK_HW_FEATURES (NETIF_F_IP_CSUM | \
36 NETIF_F_RXCSUM | \
37 NETIF_F_HW_VLAN_CTAG_TX | \
developerfd40db22021-04-29 10:08:25 +080038 NETIF_F_SG | NETIF_F_TSO | \
39 NETIF_F_TSO6 | \
40 NETIF_F_IPV6_CSUM)
41#define MTK_SET_FEATURES (NETIF_F_LRO | \
42 NETIF_F_HW_VLAN_CTAG_RX)
43#define MTK_HW_FEATURES_MT7628 (NETIF_F_SG | NETIF_F_RXCSUM)
44#define NEXT_DESP_IDX(X, Y) (((X) + 1) & ((Y) - 1))
45
developerfd40db22021-04-29 10:08:25 +080046#define MTK_HW_LRO_DMA_SIZE 8
47
48#define MTK_MAX_LRO_RX_LENGTH (4096 * 3)
49#define MTK_MAX_LRO_IP_CNT 2
50#define MTK_HW_LRO_TIMER_UNIT 1 /* 20 us */
51#define MTK_HW_LRO_REFRESH_TIME 50000 /* 1 sec. */
52#define MTK_HW_LRO_AGG_TIME 10 /* 200us */
53#define MTK_HW_LRO_AGE_TIME 50 /* 1ms */
54#define MTK_HW_LRO_MAX_AGG_CNT 64
55#define MTK_HW_LRO_BW_THRE 3000
56#define MTK_HW_LRO_REPLACE_DELTA 1000
57#define MTK_HW_LRO_SDL_REMAIN_ROOM 1522
58
developer8051e042022-04-08 13:26:36 +080059/* Frame Engine Global Configuration */
60#define MTK_FE_GLO_CFG 0x00
61#define MTK_FE_LINK_DOWN_P3 BIT(11)
62#define MTK_FE_LINK_DOWN_P4 BIT(12)
63
developerfd40db22021-04-29 10:08:25 +080064/* Frame Engine Global Reset Register */
65#define MTK_RST_GL 0x04
66#define RST_GL_PSE BIT(0)
67
68/* Frame Engine Interrupt Status Register */
developer8051e042022-04-08 13:26:36 +080069#define MTK_FE_INT_STATUS 0x08
70#define MTK_FE_INT_STATUS2 0x28
71#define MTK_FE_INT_ENABLE 0x0C
72#define MTK_FE_INT_FQ_EMPTY BIT(8)
73#define MTK_FE_INT_TSO_FAIL BIT(12)
74#define MTK_FE_INT_TSO_ILLEGAL BIT(13)
75#define MTK_FE_INT_TSO_ALIGN BIT(14)
76#define MTK_FE_INT_RFIFO_OV BIT(18)
77#define MTK_FE_INT_RFIFO_UF BIT(19)
developerfd40db22021-04-29 10:08:25 +080078#define MTK_GDM1_AF BIT(28)
79#define MTK_GDM2_AF BIT(29)
80
81/* PDMA HW LRO Alter Flow Timer Register */
82#define MTK_PDMA_LRO_ALT_REFRESH_TIMER 0x1c
83
84/* Frame Engine Interrupt Grouping Register */
85#define MTK_FE_INT_GRP 0x20
86
developer77d03a72021-06-06 00:06:00 +080087/* Frame Engine LRO auto-learn table info */
88#define MTK_FE_ALT_CF8 0x300
89#define MTK_FE_ALT_SGL_CFC 0x304
90#define MTK_FE_ALT_SEQ_CFC 0x308
91
developerfd40db22021-04-29 10:08:25 +080092/* CDMP Ingress Control Register */
93#define MTK_CDMQ_IG_CTRL 0x1400
94#define MTK_CDMQ_STAG_EN BIT(0)
95
96/* CDMP Ingress Control Register */
97#define MTK_CDMP_IG_CTRL 0x400
98#define MTK_CDMP_STAG_EN BIT(0)
99
100/* CDMP Exgress Control Register */
101#define MTK_CDMP_EG_CTRL 0x404
102
103/* GDM Exgress Control Register */
104#define MTK_GDMA_FWD_CFG(x) (0x500 + (x * 0x1000))
105#define MTK_GDMA_SPECIAL_TAG BIT(24)
106#define MTK_GDMA_ICS_EN BIT(22)
107#define MTK_GDMA_TCS_EN BIT(21)
108#define MTK_GDMA_UCS_EN BIT(20)
109#define MTK_GDMA_TO_PDMA 0x0
110#define MTK_GDMA_DROP_ALL 0x7777
111
112/* Unicast Filter MAC Address Register - Low */
113#define MTK_GDMA_MAC_ADRL(x) (0x508 + (x * 0x1000))
114
115/* Unicast Filter MAC Address Register - High */
116#define MTK_GDMA_MAC_ADRH(x) (0x50C + (x * 0x1000))
117
118/* Internal SRAM offset */
119#define MTK_ETH_SRAM_OFFSET 0x40000
120
121/* FE global misc reg*/
122#define MTK_FE_GLO_MISC 0x124
123
developerfef9efd2021-06-16 18:28:09 +0800124/* PSE Free Queue Flow Control */
125#define PSE_FQFC_CFG1 0x100
126#define PSE_FQFC_CFG2 0x104
developer459b78e2022-07-01 17:25:10 +0800127#define PSE_NO_DROP_CFG 0x108
128#define PSE_PPE0_DROP 0x110
developerfef9efd2021-06-16 18:28:09 +0800129
developerfd40db22021-04-29 10:08:25 +0800130/* PSE Input Queue Reservation Register*/
131#define PSE_IQ_REV(x) (0x140 + ((x - 1) * 0x4))
132
133/* PSE Output Queue Threshold Register*/
134#define PSE_OQ_TH(x) (0x160 + ((x - 1) * 0x4))
135
developerfef9efd2021-06-16 18:28:09 +0800136/* GDM and CDM Threshold */
137#define MTK_GDM2_THRES 0x1530
138#define MTK_CDMW0_THRES 0x164c
139#define MTK_CDMW1_THRES 0x1650
140#define MTK_CDME0_THRES 0x1654
141#define MTK_CDME1_THRES 0x1658
142#define MTK_CDMM_THRES 0x165c
143
developerfd40db22021-04-29 10:08:25 +0800144#define MTK_PDMA_V2 BIT(4)
developerfd40db22021-04-29 10:08:25 +0800145
developera2bdbd52021-05-31 19:10:17 +0800146#if defined(CONFIG_MEDIATEK_NETSYS_V2)
developerfd40db22021-04-29 10:08:25 +0800147#define PDMA_BASE 0x6000
developerfd40db22021-04-29 10:08:25 +0800148#define QDMA_BASE 0x4400
developer8051e042022-04-08 13:26:36 +0800149#define WDMA_BASE(x) (0x4800 + ((x) * 0x400))
150#define PPE_BASE(x) (0x2200 + ((x) * 0x400))
developerfd40db22021-04-29 10:08:25 +0800151#else
152#define PDMA_BASE 0x0800
153#define QDMA_BASE 0x1800
developer8051e042022-04-08 13:26:36 +0800154#define WDMA_BASE(x) (0x2800 + ((x) * 0x400))
155#define PPE_BASE(x) (0xE00 + ((x) * 0x400))
developerfd40db22021-04-29 10:08:25 +0800156#endif
157/* PDMA RX Base Pointer Register */
158#define MTK_PRX_BASE_PTR0 (PDMA_BASE + 0x100)
159#define MTK_PRX_BASE_PTR_CFG(x) (MTK_PRX_BASE_PTR0 + (x * 0x10))
160
161/* PDMA RX Maximum Count Register */
162#define MTK_PRX_MAX_CNT0 (MTK_PRX_BASE_PTR0 + 0x04)
163#define MTK_PRX_MAX_CNT_CFG(x) (MTK_PRX_MAX_CNT0 + (x * 0x10))
164
165/* PDMA RX CPU Pointer Register */
166#define MTK_PRX_CRX_IDX0 (MTK_PRX_BASE_PTR0 + 0x08)
167#define MTK_PRX_CRX_IDX_CFG(x) (MTK_PRX_CRX_IDX0 + (x * 0x10))
168
developer77f3fd42021-10-05 15:16:05 +0800169/* PDMA RX DMA Pointer Register */
170#define MTK_PRX_DRX_IDX0 (MTK_PRX_BASE_PTR0 + 0x0c)
171#define MTK_PRX_DRX_IDX_CFG(x) (MTK_PRX_DRX_IDX0 + (x * 0x10))
172
developerfd40db22021-04-29 10:08:25 +0800173/* PDMA HW LRO Control Registers */
developer77d03a72021-06-06 00:06:00 +0800174#define BITS(m, n) (~(BIT(m) - 1) & ((BIT(n) - 1) | BIT(n)))
175#if defined(CONFIG_MEDIATEK_NETSYS_V2)
176#define MTK_MAX_RX_RING_NUM (8)
177#define MTK_HW_LRO_RING_NUM (4)
178#define IS_HW_LRO_RING(ring_no) (((ring_no) > 3) && ((ring_no) < 8))
179#define MTK_PDMA_LRO_CTRL_DW0 (PDMA_BASE + 0x408)
180#define MTK_LRO_ALT_SCORE_DELTA (PDMA_BASE + 0x41c)
181#define MTK_LRO_RX_RING0_CTRL_DW1 (PDMA_BASE + 0x438)
182#define MTK_LRO_RX_RING0_CTRL_DW2 (PDMA_BASE + 0x43c)
183#define MTK_LRO_RX_RING0_CTRL_DW3 (PDMA_BASE + 0x440)
184#define MTK_L3_CKS_UPD_EN BIT(19)
185#define MTK_LRO_CRSN_BNW BIT(22)
186#define MTK_LRO_RING_RELINGUISH_REQ (0xf << 24)
187#define MTK_LRO_RING_RELINGUISH_DONE (0xf << 28)
188#else
189#define MTK_MAX_RX_RING_NUM (4)
190#define MTK_HW_LRO_RING_NUM (3)
191#define IS_HW_LRO_RING(ring_no) (((ring_no) > 0) && ((ring_no) < 4))
192#define MTK_PDMA_LRO_CTRL_DW0 (PDMA_BASE + 0x180)
193#define MTK_LRO_ALT_SCORE_DELTA (PDMA_BASE + 0x24c)
194#define MTK_LRO_RX_RING0_CTRL_DW1 (PDMA_BASE + 0x328)
195#define MTK_LRO_RX_RING0_CTRL_DW2 (PDMA_BASE + 0x32c)
196#define MTK_LRO_RX_RING0_CTRL_DW3 (PDMA_BASE + 0x330)
197#define MTK_LRO_CRSN_BNW BIT(6)
developerfd40db22021-04-29 10:08:25 +0800198#define MTK_L3_CKS_UPD_EN BIT(7)
developer77d03a72021-06-06 00:06:00 +0800199#define MTK_LRO_RING_RELINGUISH_REQ (0x7 << 26)
200#define MTK_LRO_RING_RELINGUISH_DONE (0x7 << 29)
201#endif
202
203#define IS_NORMAL_RING(ring_no) ((ring_no) == 0)
204#define MTK_LRO_EN BIT(0)
developer18f46a82021-07-20 21:08:21 +0800205#define MTK_NON_LRO_MULTI_EN BIT(2)
206#define MTK_LRO_DLY_INT_EN BIT(5)
developerfd40db22021-04-29 10:08:25 +0800207#define MTK_LRO_ALT_PKT_CNT_MODE BIT(21)
developer77d03a72021-06-06 00:06:00 +0800208#define MTK_LRO_L4_CTRL_PSH_EN BIT(23)
209#define MTK_CTRL_DW0_SDL_OFFSET (3)
210#define MTK_CTRL_DW0_SDL_MASK BITS(3, 18)
developerfd40db22021-04-29 10:08:25 +0800211
212#define MTK_PDMA_LRO_CTRL_DW1 (MTK_PDMA_LRO_CTRL_DW0 + 0x04)
213#define MTK_PDMA_LRO_CTRL_DW2 (MTK_PDMA_LRO_CTRL_DW0 + 0x08)
214#define MTK_PDMA_LRO_CTRL_DW3 (MTK_PDMA_LRO_CTRL_DW0 + 0x0c)
215#define MTK_ADMA_MODE BIT(15)
216#define MTK_LRO_MIN_RXD_SDL (MTK_HW_LRO_SDL_REMAIN_ROOM << 16)
217
developer18f46a82021-07-20 21:08:21 +0800218/* PDMA RSS Control Registers */
219#if defined(CONFIG_MEDIATEK_NETSYS_V2)
220#define MTK_PDMA_RSS_GLO_CFG (PDMA_BASE + 0x800)
221#define MTK_RX_NAPI_NUM (2)
222#define MTK_MAX_IRQ_NUM (4)
223#else
224#define MTK_PDMA_RSS_GLO_CFG 0x3000
225#define MTK_RX_NAPI_NUM (1)
226#define MTK_MAX_IRQ_NUM (3)
227#endif
228#define MTK_RSS_RING1 (1)
229#define MTK_RSS_EN BIT(0)
230#define MTK_RSS_CFG_REQ BIT(2)
231#define MTK_RSS_IPV6_STATIC_HASH (0x7 << 8)
232#define MTK_RSS_IPV4_STATIC_HASH (0x7 << 12)
233#define MTK_RSS_INDR_TABLE_DW0 (MTK_PDMA_RSS_GLO_CFG + 0x50)
234#define MTK_RSS_INDR_TABLE_DW1 (MTK_PDMA_RSS_GLO_CFG + 0x54)
235#define MTK_RSS_INDR_TABLE_DW2 (MTK_PDMA_RSS_GLO_CFG + 0x58)
236#define MTK_RSS_INDR_TABLE_DW3 (MTK_PDMA_RSS_GLO_CFG + 0x5C)
237#define MTK_RSS_INDR_TABLE_DW4 (MTK_PDMA_RSS_GLO_CFG + 0x60)
238#define MTK_RSS_INDR_TABLE_DW5 (MTK_PDMA_RSS_GLO_CFG + 0x64)
239#define MTK_RSS_INDR_TABLE_DW6 (MTK_PDMA_RSS_GLO_CFG + 0x68)
240#define MTK_RSS_INDR_TABLE_DW7 (MTK_PDMA_RSS_GLO_CFG + 0x6C)
241#define MTK_RSS_INDR_TABLE_SIZE4 0x44444444
242
developerfd40db22021-04-29 10:08:25 +0800243/* PDMA Global Configuration Register */
244#define MTK_PDMA_GLO_CFG (PDMA_BASE + 0x204)
developer77d03a72021-06-06 00:06:00 +0800245#define MTK_RX_DMA_LRO_EN BIT(8)
developerfd40db22021-04-29 10:08:25 +0800246#define MTK_MULTI_EN BIT(10)
247#define MTK_PDMA_SIZE_8DWORDS (1 << 4)
248
developer77d03a72021-06-06 00:06:00 +0800249/* PDMA Global Configuration Register */
250#define MTK_PDMA_RX_CFG (PDMA_BASE + 0x210)
251#define MTK_PDMA_LRO_SDL (0x3000)
252#define MTK_RX_CFG_SDL_OFFSET (16)
253
developerfd40db22021-04-29 10:08:25 +0800254/* PDMA Reset Index Register */
255#define MTK_PDMA_RST_IDX (PDMA_BASE + 0x208)
256#define MTK_PST_DRX_IDX0 BIT(16)
257#define MTK_PST_DRX_IDX_CFG(x) (MTK_PST_DRX_IDX0 << (x))
258
259/* PDMA Delay Interrupt Register */
260#define MTK_PDMA_DELAY_INT (PDMA_BASE + 0x20c)
261#define MTK_PDMA_DELAY_RX_EN BIT(15)
262#define MTK_PDMA_DELAY_RX_PINT 4
263#define MTK_PDMA_DELAY_RX_PINT_SHIFT 8
264#define MTK_PDMA_DELAY_RX_PTIME 4
265#define MTK_PDMA_DELAY_RX_DELAY \
266 (MTK_PDMA_DELAY_RX_EN | MTK_PDMA_DELAY_RX_PTIME | \
267 (MTK_PDMA_DELAY_RX_PINT << MTK_PDMA_DELAY_RX_PINT_SHIFT))
268
269/* PDMA Interrupt Status Register */
270#define MTK_PDMA_INT_STATUS (PDMA_BASE + 0x220)
271
272/* PDMA Interrupt Mask Register */
273#define MTK_PDMA_INT_MASK (PDMA_BASE + 0x228)
274
developerfd40db22021-04-29 10:08:25 +0800275/* PDMA Interrupt grouping registers */
276#define MTK_PDMA_INT_GRP1 (PDMA_BASE + 0x250)
277#define MTK_PDMA_INT_GRP2 (PDMA_BASE + 0x254)
developer18f46a82021-07-20 21:08:21 +0800278#if defined(CONFIG_MEDIATEK_NETSYS_V2)
279#define MTK_PDMA_INT_GRP3 (PDMA_BASE + 0x258)
280#else
281#define MTK_PDMA_INT_GRP3 (PDMA_BASE + 0x22c)
282#endif
283#define MTK_LRO_RX1_DLY_INT 0xa70
284#define MTK_MAX_DELAY_INT 0x8f0f8f0f
developerfd40db22021-04-29 10:08:25 +0800285
286/* PDMA HW LRO IP Setting Registers */
developer77d03a72021-06-06 00:06:00 +0800287#if defined(CONFIG_MEDIATEK_NETSYS_V2)
288#define MTK_LRO_RX_RING0_DIP_DW0 (PDMA_BASE + 0x414)
289#else
developerfd40db22021-04-29 10:08:25 +0800290#define MTK_LRO_RX_RING0_DIP_DW0 (PDMA_BASE + 0x304)
developer77d03a72021-06-06 00:06:00 +0800291#endif
developerfd40db22021-04-29 10:08:25 +0800292#define MTK_LRO_DIP_DW0_CFG(x) (MTK_LRO_RX_RING0_DIP_DW0 + (x * 0x40))
293#define MTK_RING_MYIP_VLD BIT(9)
294
developer77d03a72021-06-06 00:06:00 +0800295/* PDMA HW LRO ALT Debug Registers */
296#define MTK_LRO_ALT_DBG (PDMA_BASE + 0x440)
297#define MTK_LRO_ALT_INDEX_OFFSET (8)
298
299/* PDMA HW LRO ALT Data Registers */
300#define MTK_LRO_ALT_DBG_DATA (PDMA_BASE + 0x444)
301
developerfd40db22021-04-29 10:08:25 +0800302/* PDMA HW LRO Ring Control Registers */
developerfd40db22021-04-29 10:08:25 +0800303#define MTK_LRO_CTRL_DW1_CFG(x) (MTK_LRO_RX_RING0_CTRL_DW1 + (x * 0x40))
304#define MTK_LRO_CTRL_DW2_CFG(x) (MTK_LRO_RX_RING0_CTRL_DW2 + (x * 0x40))
305#define MTK_LRO_CTRL_DW3_CFG(x) (MTK_LRO_RX_RING0_CTRL_DW3 + (x * 0x40))
306#define MTK_RING_AGE_TIME_L ((MTK_HW_LRO_AGE_TIME & 0x3ff) << 22)
307#define MTK_RING_AGE_TIME_H ((MTK_HW_LRO_AGE_TIME >> 10) & 0x3f)
developer18f46a82021-07-20 21:08:21 +0800308#define MTK_RING_PSE_MODE (1 << 6)
developerfd40db22021-04-29 10:08:25 +0800309#define MTK_RING_AUTO_LERAN_MODE (3 << 6)
310#define MTK_RING_VLD BIT(8)
311#define MTK_RING_MAX_AGG_TIME ((MTK_HW_LRO_AGG_TIME & 0xffff) << 10)
312#define MTK_RING_MAX_AGG_CNT_L ((MTK_HW_LRO_MAX_AGG_CNT & 0x3f) << 26)
313#define MTK_RING_MAX_AGG_CNT_H ((MTK_HW_LRO_MAX_AGG_CNT >> 6) & 0x3)
314
developer77d03a72021-06-06 00:06:00 +0800315/* LRO_RX_RING_CTRL_DW masks */
316#define MTK_LRO_RING_AGG_TIME_MASK BITS(10, 25)
317#define MTK_LRO_RING_AGG_CNT_L_MASK BITS(26, 31)
318#define MTK_LRO_RING_AGG_CNT_H_MASK BITS(0, 1)
319#define MTK_LRO_RING_AGE_TIME_L_MASK BITS(22, 31)
320#define MTK_LRO_RING_AGE_TIME_H_MASK BITS(0, 5)
321
322/* LRO_RX_RING_CTRL_DW0 offsets */
323#define MTK_RX_IPV6_FORCE_OFFSET (0)
324#define MTK_RX_IPV4_FORCE_OFFSET (1)
325
326/* LRO_RX_RING_CTRL_DW1 offsets */
327#define MTK_LRO_RING_AGE_TIME_L_OFFSET (22)
328
329/* LRO_RX_RING_CTRL_DW2 offsets */
330#define MTK_LRO_RING_AGE_TIME_H_OFFSET (0)
331#define MTK_RX_MODE_OFFSET (6)
332#define MTK_RX_PORT_VALID_OFFSET (8)
333#define MTK_RX_MYIP_VALID_OFFSET (9)
334#define MTK_LRO_RING_AGG_TIME_OFFSET (10)
335#define MTK_LRO_RING_AGG_CNT_L_OFFSET (26)
336
337/* LRO_RX_RING_CTRL_DW3 offsets */
338#define MTK_LRO_RING_AGG_CNT_H_OFFSET (0)
339
340/* LRO_RX_RING_STP_DTP_DW offsets */
341#define MTK_RX_TCP_DEST_PORT_OFFSET (0)
342#define MTK_RX_TCP_SRC_PORT_OFFSET (16)
343
developerfd40db22021-04-29 10:08:25 +0800344/* QDMA TX Queue Configuration Registers */
345#define MTK_QTX_CFG(x) (QDMA_BASE + (x * 0x10))
346#define QDMA_RES_THRES 4
347
348/* QDMA TX Queue Scheduler Registers */
349#define MTK_QTX_SCH(x) (QDMA_BASE + 4 + (x * 0x10))
350
351/* QDMA RX Base Pointer Register */
352#define MTK_QRX_BASE_PTR0 (QDMA_BASE + 0x100)
353#define MTK_QRX_BASE_PTR_CFG(x) (MTK_QRX_BASE_PTR0 + ((x) * 0x10))
354
355/* QDMA RX Maximum Count Register */
356#define MTK_QRX_MAX_CNT0 (QDMA_BASE + 0x104)
357#define MTK_QRX_MAX_CNT_CFG(x) (MTK_QRX_MAX_CNT0 + ((x) * 0x10))
358
359/* QDMA RX CPU Pointer Register */
360#define MTK_QRX_CRX_IDX0 (QDMA_BASE + 0x108)
361#define MTK_QRX_CRX_IDX_CFG(x) (MTK_QRX_CRX_IDX0 + ((x) * 0x10))
362
363/* QDMA RX DMA Pointer Register */
364#define MTK_QRX_DRX_IDX0 (QDMA_BASE + 0x10c)
365
developer329d8ee2022-08-02 08:49:42 +0800366/* QDMA Page Configuration Register */
367#define MTK_QDMA_PAGE (QDMA_BASE + 0x1f0)
368
developerfd40db22021-04-29 10:08:25 +0800369/* QDMA Global Configuration Register */
370#define MTK_QDMA_GLO_CFG (QDMA_BASE + 0x204)
371#define MTK_RX_2B_OFFSET BIT(31)
developer58ab5842022-06-01 15:10:25 +0800372#define MTK_PKT_RX_WDONE BIT(27)
developerfd40db22021-04-29 10:08:25 +0800373#define MTK_RX_BT_32DWORDS (3 << 11)
374#define MTK_NDP_CO_PRO BIT(10)
375#define MTK_TX_WB_DDONE BIT(6)
376#define MTK_DMA_SIZE_16DWORDS (2 << 4)
377#define MTK_DMA_SIZE_32DWORDS (3 << 4)
378#define MTK_RX_DMA_BUSY BIT(3)
379#define MTK_TX_DMA_BUSY BIT(1)
380#define MTK_RX_DMA_EN BIT(2)
381#define MTK_TX_DMA_EN BIT(0)
382#define MTK_DMA_BUSY_TIMEOUT HZ
383
384/* QDMA V2 Global Configuration Register */
385#define MTK_CHK_DDONE_EN BIT(28)
386#define MTK_DMAD_WR_WDONE BIT(26)
387#define MTK_WCOMP_EN BIT(24)
developer2cdef092022-04-15 17:27:55 +0800388#define MTK_RESV_BUF (0x80 << 16)
developerfd40db22021-04-29 10:08:25 +0800389#define MTK_MUTLI_CNT (0x4 << 12)
developer19d84562022-04-21 17:01:06 +0800390#define MTK_RESV_BUF_MASK (0xff << 16)
developerfd40db22021-04-29 10:08:25 +0800391
392/* QDMA Reset Index Register */
393#define MTK_QDMA_RST_IDX (QDMA_BASE + 0x208)
394
395/* QDMA Delay Interrupt Register */
396#define MTK_QDMA_DELAY_INT (QDMA_BASE + 0x20c)
397
398/* QDMA Flow Control Register */
399#define MTK_QDMA_FC_THRES (QDMA_BASE + 0x210)
400#define FC_THRES_DROP_MODE BIT(20)
401#define FC_THRES_DROP_EN (7 << 16)
402#define FC_THRES_MIN 0x4444
403
404/* QDMA Interrupt Status Register */
405#define MTK_QDMA_INT_STATUS (QDMA_BASE + 0x218)
developera2bdbd52021-05-31 19:10:17 +0800406#if defined(CONFIG_MEDIATEK_NETSYS_V2)
developer18f46a82021-07-20 21:08:21 +0800407#define MTK_RX_DONE_INT(ring_no) \
408 ((ring_no)? BIT(16 + (ring_no)) : BIT(14))
developerfd40db22021-04-29 10:08:25 +0800409#else
developer18f46a82021-07-20 21:08:21 +0800410#define MTK_RX_DONE_INT(ring_no) \
411 ((ring_no)? BIT(24 + (ring_no)) : BIT(30))
developerfd40db22021-04-29 10:08:25 +0800412#endif
413#define MTK_RX_DONE_INT3 BIT(19)
414#define MTK_RX_DONE_INT2 BIT(18)
415#define MTK_RX_DONE_INT1 BIT(17)
416#define MTK_RX_DONE_INT0 BIT(16)
417#define MTK_TX_DONE_INT3 BIT(3)
418#define MTK_TX_DONE_INT2 BIT(2)
419#define MTK_TX_DONE_INT1 BIT(1)
420#define MTK_TX_DONE_INT0 BIT(0)
developerfd40db22021-04-29 10:08:25 +0800421#define MTK_TX_DONE_DLY BIT(28)
422#define MTK_TX_DONE_INT MTK_TX_DONE_DLY
423
424/* QDMA Interrupt grouping registers */
425#define MTK_QDMA_INT_GRP1 (QDMA_BASE + 0x220)
426#define MTK_QDMA_INT_GRP2 (QDMA_BASE + 0x224)
427#define MTK_RLS_DONE_INT BIT(0)
428
429/* QDMA Interrupt Status Register */
430#define MTK_QDMA_INT_MASK (QDMA_BASE + 0x21c)
431
developer8051e042022-04-08 13:26:36 +0800432/* QDMA DMA FSM */
433#define MTK_QDMA_FSM (QDMA_BASE + 0x234)
434
developerfd40db22021-04-29 10:08:25 +0800435/* QDMA Interrupt Mask Register */
436#define MTK_QDMA_HRED2 (QDMA_BASE + 0x244)
437
438/* QDMA TX Forward CPU Pointer Register */
439#define MTK_QTX_CTX_PTR (QDMA_BASE +0x300)
440
441/* QDMA TX Forward DMA Pointer Register */
442#define MTK_QTX_DTX_PTR (QDMA_BASE +0x304)
443
developer8051e042022-04-08 13:26:36 +0800444/* QDMA TX Forward DMA Counter */
445#define MTK_QDMA_FWD_CNT (QDMA_BASE + 0x308)
446
developerfd40db22021-04-29 10:08:25 +0800447/* QDMA TX Release CPU Pointer Register */
448#define MTK_QTX_CRX_PTR (QDMA_BASE +0x310)
449
450/* QDMA TX Release DMA Pointer Register */
451#define MTK_QTX_DRX_PTR (QDMA_BASE +0x314)
452
453/* QDMA FQ Head Pointer Register */
454#define MTK_QDMA_FQ_HEAD (QDMA_BASE +0x320)
455
456/* QDMA FQ Head Pointer Register */
457#define MTK_QDMA_FQ_TAIL (QDMA_BASE +0x324)
458
459/* QDMA FQ Free Page Counter Register */
460#define MTK_QDMA_FQ_CNT (QDMA_BASE +0x328)
461
462/* QDMA FQ Free Page Buffer Length Register */
463#define MTK_QDMA_FQ_BLEN (QDMA_BASE +0x32c)
464
developer8051e042022-04-08 13:26:36 +0800465/* WDMA Registers */
466#define MTK_WDMA_DTX_PTR(x) (WDMA_BASE(x) + 0xC)
467#define MTK_WDMA_GLO_CFG(x) (WDMA_BASE(x) + 0x204)
468#define MTK_WDMA_TX_DBG_MON0(x) (WDMA_BASE(x) + 0x230)
469#define MTK_CDM_TXFIFO_RDY BIT(7)
470
developerfd40db22021-04-29 10:08:25 +0800471/* GMA1 Received Good Byte Count Register */
472#if defined(CONFIG_MEDIATEK_NETSYS_V2)
473#define MTK_GDM1_TX_GBCNT 0x1C00
474#else
475#define MTK_GDM1_TX_GBCNT 0x2400
476#endif
477#define MTK_STAT_OFFSET 0x40
478
479/* QDMA TX NUM */
480#define MTK_QDMA_TX_NUM 16
developer797e46c2022-07-29 12:05:32 +0800481#define MTK_QDMA_PAGE_NUM 8
developerfd40db22021-04-29 10:08:25 +0800482#define MTK_QDMA_TX_MASK ((MTK_QDMA_TX_NUM) - 1)
483#define QID_LOW_BITS(x) ((x) & 0xf)
484#define QID_HIGH_BITS(x) ((((x) >> 4) & 0x3) << 20)
485#define QID_BITS_V2(x) (((x) & 0x3f) << 16)
486
developerdc0d45f2021-12-27 13:01:22 +0800487#define MTK_QDMA_GMAC2_QID 8
488
developerfd40db22021-04-29 10:08:25 +0800489/* QDMA V2 descriptor txd6 */
490#define TX_DMA_INS_VLAN_V2 BIT(16)
491
492/* QDMA V2 descriptor txd5 */
493#define TX_DMA_CHKSUM_V2 (0x7 << 28)
494#define TX_DMA_TSO_V2 BIT(31)
495
496/* QDMA V2 descriptor txd4 */
497#define TX_DMA_FPORT_SHIFT_V2 8
498#define TX_DMA_FPORT_MASK_V2 0xf
499#define TX_DMA_SWC_V2 BIT(30)
500
developerfd40db22021-04-29 10:08:25 +0800501#define MTK_TX_DMA_BUF_LEN 0x3fff
developere9356982022-07-04 09:03:20 +0800502#define MTK_TX_DMA_BUF_LEN_V2 0xffff
developerfd40db22021-04-29 10:08:25 +0800503#define MTK_TX_DMA_BUF_SHIFT 16
developere9356982022-07-04 09:03:20 +0800504#define MTK_TX_DMA_BUF_SHIFT_V2 8
developerfd40db22021-04-29 10:08:25 +0800505
developerfd40db22021-04-29 10:08:25 +0800506#define RX_DMA_SPORT_SHIFT 19
developere9356982022-07-04 09:03:20 +0800507#define RX_DMA_SPORT_SHIFT_V2 26
developerfd40db22021-04-29 10:08:25 +0800508#define RX_DMA_SPORT_MASK 0x7
developere9356982022-07-04 09:03:20 +0800509#define RX_DMA_SPORT_MASK_V2 0xf
developerfd40db22021-04-29 10:08:25 +0800510
511/* QDMA descriptor txd4 */
512#define TX_DMA_CHKSUM (0x7 << 29)
513#define TX_DMA_TSO BIT(28)
514#define TX_DMA_FPORT_SHIFT 25
515#define TX_DMA_FPORT_MASK 0x7
516#define TX_DMA_INS_VLAN BIT(16)
517
518/* QDMA descriptor txd3 */
519#define TX_DMA_OWNER_CPU BIT(31)
520#define TX_DMA_LS0 BIT(30)
developere9356982022-07-04 09:03:20 +0800521#define TX_DMA_PLEN0(_x) (((_x) & eth->soc->txrx.dma_max_len) << eth->soc->txrx.dma_len_offset)
522#define TX_DMA_PLEN1(_x) ((_x) & eth->soc->txrx.dma_max_len)
developerfd40db22021-04-29 10:08:25 +0800523#define TX_DMA_SWC BIT(14)
developerfd40db22021-04-29 10:08:25 +0800524
525/* PDMA on MT7628 */
526#define TX_DMA_DONE BIT(31)
527#define TX_DMA_LS1 BIT(14)
528#define TX_DMA_DESP2_DEF (TX_DMA_LS0 | TX_DMA_DONE)
529
530/* QDMA descriptor rxd2 */
531#define RX_DMA_DONE BIT(31)
532#define RX_DMA_LSO BIT(30)
developere9356982022-07-04 09:03:20 +0800533#define RX_DMA_PLEN0(_x) (((_x) & eth->soc->txrx.dma_max_len) << eth->soc->txrx.dma_len_offset)
534#define RX_DMA_GET_PLEN0(_x) (((_x) >> eth->soc->txrx.dma_len_offset) & eth->soc->txrx.dma_max_len)
developer77d03a72021-06-06 00:06:00 +0800535#define RX_DMA_GET_AGG_CNT(_x) (((_x) >> 2) & 0xff)
536#define RX_DMA_GET_REV(_x) (((_x) >> 10) & 0x1f)
developerfd40db22021-04-29 10:08:25 +0800537#define RX_DMA_VTAG BIT(15)
538
539/* QDMA descriptor rxd3 */
540#define RX_DMA_VID(_x) ((_x) & VLAN_VID_MASK)
541#define RX_DMA_TCI(_x) ((_x) & (VLAN_PRIO_MASK | VLAN_VID_MASK))
542#define RX_DMA_VPID(_x) (((_x) >> 16) & 0xffff)
543
544/* QDMA descriptor rxd4 */
545#define RX_DMA_L4_VALID BIT(24)
546#define RX_DMA_L4_VALID_PDMA BIT(30) /* when PDMA is used */
547#define RX_DMA_SPECIAL_TAG BIT(22) /* switch header in packet */
548
549#define RX_DMA_GET_SPORT(_x) (((_x) >> RX_DMA_SPORT_SHIFT) & RX_DMA_SPORT_MASK)
developere9356982022-07-04 09:03:20 +0800550#define RX_DMA_GET_SPORT_V2(_x) (((_x) >> RX_DMA_SPORT_SHIFT_V2) & RX_DMA_SPORT_MASK_V2)
developerfd40db22021-04-29 10:08:25 +0800551
552/* PDMA V2 descriptor rxd3 */
553#define RX_DMA_VTAG_V2 BIT(0)
554#define RX_DMA_L4_VALID_V2 BIT(2)
555
556/* PDMA V2 descriptor rxd4 */
557#define RX_DMA_VID_V2(_x) RX_DMA_VID(_x)
developer255bba22021-07-27 15:16:33 +0800558#define RX_DMA_TCI_V2(_x) RX_DMA_TCI(_x)
559#define RX_DMA_VPID_V2(_x) RX_DMA_VPID(_x)
developerfd40db22021-04-29 10:08:25 +0800560
developer77d03a72021-06-06 00:06:00 +0800561/* PDMA V2 descriptor rxd6 */
562#define RX_DMA_GET_FLUSH_RSN_V2(_x) ((_x) & 0x7)
563#define RX_DMA_GET_AGG_CNT_V2(_x) (((_x) >> 16) & 0xff)
564
developerfd40db22021-04-29 10:08:25 +0800565/* PHY Indirect Access Control registers */
566#define MTK_PHY_IAC 0x10004
567#define PHY_IAC_ACCESS BIT(31)
568#define PHY_IAC_READ BIT(19)
developer599cda42022-05-24 15:13:31 +0800569#define PHY_IAC_READ_C45 (3 << 18)
570#define PHY_IAC_ADDR_C45 (0 << 18)
developerfd40db22021-04-29 10:08:25 +0800571#define PHY_IAC_WRITE BIT(18)
572#define PHY_IAC_START BIT(16)
developer599cda42022-05-24 15:13:31 +0800573#define PHY_IAC_START_C45 (0 << 16)
developerfd40db22021-04-29 10:08:25 +0800574#define PHY_IAC_ADDR_SHIFT 20
575#define PHY_IAC_REG_SHIFT 25
576#define PHY_IAC_TIMEOUT HZ
577
578#define MTK_MAC_MISC 0x1000c
579#define MTK_MUX_TO_ESW BIT(0)
580
581/* Mac control registers */
582#define MTK_MAC_MCR(x) (0x10100 + (x * 0x100))
583#define MAC_MCR_MAX_RX_1536 BIT(24)
developerd8a29752022-08-19 13:32:03 +0800584#define MAC_MCR_IPG_CFG (BIT(18) | BIT(16) | BIT(12))
developerfd40db22021-04-29 10:08:25 +0800585#define MAC_MCR_FORCE_MODE BIT(15)
586#define MAC_MCR_TX_EN BIT(14)
587#define MAC_MCR_RX_EN BIT(13)
588#define MAC_MCR_BACKOFF_EN BIT(9)
589#define MAC_MCR_BACKPR_EN BIT(8)
590#define MAC_MCR_FORCE_RX_FC BIT(5)
591#define MAC_MCR_FORCE_TX_FC BIT(4)
592#define MAC_MCR_SPEED_1000 BIT(3)
593#define MAC_MCR_SPEED_100 BIT(2)
594#define MAC_MCR_FORCE_DPX BIT(1)
595#define MAC_MCR_FORCE_LINK BIT(0)
596#define MAC_MCR_FORCE_LINK_DOWN (MAC_MCR_FORCE_MODE)
597
598/* Mac status registers */
599#define MTK_MAC_MSR(x) (0x10108 + (x * 0x100))
600#define MAC_MSR_EEE1G BIT(7)
601#define MAC_MSR_EEE100M BIT(6)
602#define MAC_MSR_RX_FC BIT(5)
603#define MAC_MSR_TX_FC BIT(4)
604#define MAC_MSR_SPEED_1000 BIT(3)
605#define MAC_MSR_SPEED_100 BIT(2)
606#define MAC_MSR_SPEED_MASK (MAC_MSR_SPEED_1000 | MAC_MSR_SPEED_100)
607#define MAC_MSR_DPX BIT(1)
608#define MAC_MSR_LINK BIT(0)
609
610/* TRGMII RXC control register */
611#define TRGMII_RCK_CTRL 0x10300
612#define DQSI0(x) ((x << 0) & GENMASK(6, 0))
613#define DQSI1(x) ((x << 8) & GENMASK(14, 8))
614#define RXCTL_DMWTLAT(x) ((x << 16) & GENMASK(18, 16))
615#define RXC_RST BIT(31)
616#define RXC_DQSISEL BIT(30)
617#define RCK_CTRL_RGMII_1000 (RXC_DQSISEL | RXCTL_DMWTLAT(2) | DQSI1(16))
618#define RCK_CTRL_RGMII_10_100 RXCTL_DMWTLAT(2)
619
620#define NUM_TRGMII_CTRL 5
621
622/* TRGMII RXC control register */
623#define TRGMII_TCK_CTRL 0x10340
624#define TXCTL_DMWTLAT(x) ((x << 16) & GENMASK(18, 16))
625#define TXC_INV BIT(30)
626#define TCK_CTRL_RGMII_1000 TXCTL_DMWTLAT(2)
627#define TCK_CTRL_RGMII_10_100 (TXC_INV | TXCTL_DMWTLAT(2))
628
629/* TRGMII TX Drive Strength */
630#define TRGMII_TD_ODT(i) (0x10354 + 8 * (i))
631#define TD_DM_DRVP(x) ((x) & 0xf)
632#define TD_DM_DRVN(x) (((x) & 0xf) << 4)
633
634/* TRGMII Interface mode register */
635#define INTF_MODE 0x10390
636#define TRGMII_INTF_DIS BIT(0)
637#define TRGMII_MODE BIT(1)
638#define TRGMII_CENTRAL_ALIGNED BIT(2)
639#define INTF_MODE_RGMII_1000 (TRGMII_MODE | TRGMII_CENTRAL_ALIGNED)
640#define INTF_MODE_RGMII_10_100 0
641
642/* GPIO port control registers for GMAC 2*/
643#define GPIO_OD33_CTRL8 0x4c0
644#define GPIO_BIAS_CTRL 0xed0
645#define GPIO_DRV_SEL10 0xf00
646
647/* ethernet subsystem chip id register */
648#define ETHSYS_CHIPID0_3 0x0
649#define ETHSYS_CHIPID4_7 0x4
650#define MT7623_ETH 7623
651#define MT7622_ETH 7622
652#define MT7621_ETH 7621
653
654/* ethernet system control register */
655#define ETHSYS_SYSCFG 0x10
656#define SYSCFG_DRAM_TYPE_DDR2 BIT(4)
657
658/* ethernet subsystem config register */
659#define ETHSYS_SYSCFG0 0x14
660#define SYSCFG0_GE_MASK 0x3
661#define SYSCFG0_GE_MODE(x, y) (x << (12 + (y * 2)))
662#define SYSCFG0_SGMII_MASK GENMASK(9, 8)
663#define SYSCFG0_SGMII_GMAC1 ((2 << 8) & SYSCFG0_SGMII_MASK)
664#define SYSCFG0_SGMII_GMAC2 ((3 << 8) & SYSCFG0_SGMII_MASK)
665#define SYSCFG0_SGMII_GMAC1_V2 BIT(9)
666#define SYSCFG0_SGMII_GMAC2_V2 BIT(8)
667
668
669/* ethernet subsystem clock register */
670#define ETHSYS_CLKCFG0 0x2c
671#define ETHSYS_TRGMII_CLK_SEL362_5 BIT(11)
672#define ETHSYS_TRGMII_MT7621_MASK (BIT(5) | BIT(6))
673#define ETHSYS_TRGMII_MT7621_APLL BIT(6)
674#define ETHSYS_TRGMII_MT7621_DDR_PLL BIT(5)
675
676/* ethernet reset control register */
developer545abf02021-07-15 17:47:01 +0800677#define ETHSYS_RSTCTRL 0x34
678#define RSTCTRL_FE BIT(6)
developer545abf02021-07-15 17:47:01 +0800679#define RSTCTRL_ETH BIT(23)
developer8051e042022-04-08 13:26:36 +0800680#if defined(CONFIG_MEDIATEK_NETSYS_V2)
681#define RSTCTRL_PPE0 BIT(30)
682#define RSTCTRL_PPE1 BIT(31)
developera5eb8d62022-04-22 15:42:20 +0800683#else
developer8051e042022-04-08 13:26:36 +0800684#define RSTCTRL_PPE0 BIT(31)
developera5eb8d62022-04-22 15:42:20 +0800685#define RSTCTRL_PPE1 0
developer8051e042022-04-08 13:26:36 +0800686#endif
developer545abf02021-07-15 17:47:01 +0800687
688/* ethernet reset check idle register */
689#define ETHSYS_FE_RST_CHK_IDLE_EN 0x28
690
developerfd40db22021-04-29 10:08:25 +0800691
692/* SGMII subsystem config registers */
693/* Register to auto-negotiation restart */
694#define SGMSYS_PCS_CONTROL_1 0x0
695#define SGMII_AN_RESTART BIT(9)
696#define SGMII_ISOLATE BIT(10)
697#define SGMII_AN_ENABLE BIT(12)
698#define SGMII_LINK_STATYS BIT(18)
699#define SGMII_AN_ABILITY BIT(19)
700#define SGMII_AN_COMPLETE BIT(21)
701#define SGMII_PCS_FAULT BIT(23)
702#define SGMII_AN_EXPANSION_CLR BIT(30)
703
704/* Register to programmable link timer, the unit in 2 * 8ns */
705#define SGMSYS_PCS_LINK_TIMER 0x18
706#define SGMII_LINK_TIMER_DEFAULT (0x186a0 & GENMASK(19, 0))
707
708/* Register to control remote fault */
709#define SGMSYS_SGMII_MODE 0x20
710#define SGMII_IF_MODE_BIT0 BIT(0)
711#define SGMII_SPEED_DUPLEX_AN BIT(1)
712#define SGMII_SPEED_10 0x0
713#define SGMII_SPEED_100 BIT(2)
714#define SGMII_SPEED_1000 BIT(3)
715#define SGMII_DUPLEX_FULL BIT(4)
716#define SGMII_IF_MODE_BIT5 BIT(5)
717#define SGMII_REMOTE_FAULT_DIS BIT(8)
718#define SGMII_CODE_SYNC_SET_VAL BIT(9)
719#define SGMII_CODE_SYNC_SET_EN BIT(10)
720#define SGMII_SEND_AN_ERROR_EN BIT(11)
721#define SGMII_IF_MODE_MASK GENMASK(5, 1)
722
723/* Register to set SGMII speed, ANA RG_ Control Signals III*/
724#define SGMSYS_ANA_RG_CS3 0x2028
725#define RG_PHY_SPEED_MASK (BIT(2) | BIT(3))
726#define RG_PHY_SPEED_1_25G 0x0
727#define RG_PHY_SPEED_3_125G BIT(2)
728
729/* Register to power up QPHY */
730#define SGMSYS_QPHY_PWR_STATE_CTRL 0xe8
731#define SGMII_PHYA_PWD BIT(4)
732
developerf8ac94a2021-07-29 16:40:01 +0800733/* Register to QPHY wrapper control */
734#define SGMSYS_QPHY_WRAP_CTRL 0xec
735#define SGMII_PN_SWAP_MASK GENMASK(1, 0)
736#define SGMII_PN_SWAP_TX_RX (BIT(0) | BIT(1))
737
developerfd40db22021-04-29 10:08:25 +0800738/* Infrasys subsystem config registers */
739#define INFRA_MISC2 0x70c
740#define CO_QPHY_SEL BIT(0)
741#define GEPHY_MAC_SEL BIT(1)
742
developer255bba22021-07-27 15:16:33 +0800743/* Top misc registers */
744#define USB_PHY_SWITCH_REG 0x218
745#define QPHY_SEL_MASK GENMASK(1, 0)
developerf1816a92021-11-15 12:18:02 +0800746#define SGMII_QPHY_SEL 0x2
developer255bba22021-07-27 15:16:33 +0800747
developerfd40db22021-04-29 10:08:25 +0800748/*MDIO control*/
749#define MII_MMD_ACC_CTL_REG 0x0d
750#define MII_MMD_ADDR_DATA_REG 0x0e
751#define MMD_OP_MODE_DATA BIT(14)
752
753/* MT7628/88 specific stuff */
754#define MT7628_PDMA_OFFSET 0x0800
755#define MT7628_SDM_OFFSET 0x0c00
756
757#define MT7628_TX_BASE_PTR0 (MT7628_PDMA_OFFSET + 0x00)
758#define MT7628_TX_MAX_CNT0 (MT7628_PDMA_OFFSET + 0x04)
759#define MT7628_TX_CTX_IDX0 (MT7628_PDMA_OFFSET + 0x08)
760#define MT7628_TX_DTX_IDX0 (MT7628_PDMA_OFFSET + 0x0c)
761#define MT7628_PST_DTX_IDX0 BIT(0)
762
763#define MT7628_SDM_MAC_ADRL (MT7628_SDM_OFFSET + 0x0c)
764#define MT7628_SDM_MAC_ADRH (MT7628_SDM_OFFSET + 0x10)
765
766struct mtk_rx_dma {
767 unsigned int rxd1;
768 unsigned int rxd2;
769 unsigned int rxd3;
770 unsigned int rxd4;
developere9356982022-07-04 09:03:20 +0800771} __packed __aligned(4);
772
773struct mtk_rx_dma_v2 {
774 unsigned int rxd1;
775 unsigned int rxd2;
776 unsigned int rxd3;
777 unsigned int rxd4;
developerfd40db22021-04-29 10:08:25 +0800778 unsigned int rxd5;
779 unsigned int rxd6;
780 unsigned int rxd7;
781 unsigned int rxd8;
developerfd40db22021-04-29 10:08:25 +0800782} __packed __aligned(4);
783
784struct mtk_tx_dma {
785 unsigned int txd1;
786 unsigned int txd2;
787 unsigned int txd3;
788 unsigned int txd4;
developere9356982022-07-04 09:03:20 +0800789} __packed __aligned(4);
790
791struct mtk_tx_dma_v2 {
792 unsigned int txd1;
793 unsigned int txd2;
794 unsigned int txd3;
795 unsigned int txd4;
developerfd40db22021-04-29 10:08:25 +0800796 unsigned int txd5;
797 unsigned int txd6;
798 unsigned int txd7;
799 unsigned int txd8;
developerfd40db22021-04-29 10:08:25 +0800800} __packed __aligned(4);
801
802struct mtk_eth;
803struct mtk_mac;
804
805/* struct mtk_hw_stats - the structure that holds the traffic statistics.
806 * @stats_lock: make sure that stats operations are atomic
807 * @reg_offset: the status register offset of the SoC
808 * @syncp: the refcount
809 *
810 * All of the supported SoCs have hardware counters for traffic statistics.
811 * Whenever the status IRQ triggers we can read the latest stats from these
812 * counters and store them in this struct.
813 */
814struct mtk_hw_stats {
815 u64 tx_bytes;
816 u64 tx_packets;
817 u64 tx_skip;
818 u64 tx_collisions;
819 u64 rx_bytes;
820 u64 rx_packets;
821 u64 rx_overflow;
822 u64 rx_fcs_errors;
823 u64 rx_short_errors;
824 u64 rx_long_errors;
825 u64 rx_checksum_errors;
826 u64 rx_flow_control_packets;
827
828 spinlock_t stats_lock;
829 u32 reg_offset;
830 struct u64_stats_sync syncp;
831};
832
833enum mtk_tx_flags {
834 /* PDMA descriptor can point at 1-2 segments. This enum allows us to
835 * track how memory was allocated so that it can be freed properly.
836 */
837 MTK_TX_FLAGS_SINGLE0 = 0x01,
838 MTK_TX_FLAGS_PAGE0 = 0x02,
839
840 /* MTK_TX_FLAGS_FPORTx allows tracking which port the transmitted
841 * SKB out instead of looking up through hardware TX descriptor.
842 */
843 MTK_TX_FLAGS_FPORT0 = 0x04,
844 MTK_TX_FLAGS_FPORT1 = 0x08,
845};
846
847/* This enum allows us to identify how the clock is defined on the array of the
848 * clock in the order
849 */
850enum mtk_clks_map {
851 MTK_CLK_ETHIF,
852 MTK_CLK_SGMIITOP,
853 MTK_CLK_ESW,
854 MTK_CLK_GP0,
855 MTK_CLK_GP1,
856 MTK_CLK_GP2,
857 MTK_CLK_FE,
858 MTK_CLK_TRGPLL,
859 MTK_CLK_SGMII_TX_250M,
860 MTK_CLK_SGMII_RX_250M,
861 MTK_CLK_SGMII_CDR_REF,
862 MTK_CLK_SGMII_CDR_FB,
863 MTK_CLK_SGMII2_TX_250M,
864 MTK_CLK_SGMII2_RX_250M,
865 MTK_CLK_SGMII2_CDR_REF,
866 MTK_CLK_SGMII2_CDR_FB,
867 MTK_CLK_SGMII_CK,
868 MTK_CLK_ETH2PLL,
869 MTK_CLK_WOCPU0,
870 MTK_CLK_WOCPU1,
871 MTK_CLK_MAX
872};
873
874#define MT7623_CLKS_BITMAP (BIT(MTK_CLK_ETHIF) | BIT(MTK_CLK_ESW) | \
875 BIT(MTK_CLK_GP1) | BIT(MTK_CLK_GP2) | \
876 BIT(MTK_CLK_TRGPLL))
877#define MT7622_CLKS_BITMAP (BIT(MTK_CLK_ETHIF) | BIT(MTK_CLK_ESW) | \
878 BIT(MTK_CLK_GP0) | BIT(MTK_CLK_GP1) | \
879 BIT(MTK_CLK_GP2) | \
880 BIT(MTK_CLK_SGMII_TX_250M) | \
881 BIT(MTK_CLK_SGMII_RX_250M) | \
882 BIT(MTK_CLK_SGMII_CDR_REF) | \
883 BIT(MTK_CLK_SGMII_CDR_FB) | \
884 BIT(MTK_CLK_SGMII_CK) | \
885 BIT(MTK_CLK_ETH2PLL))
886#define MT7621_CLKS_BITMAP (0)
887#define MT7628_CLKS_BITMAP (0)
888#define MT7629_CLKS_BITMAP (BIT(MTK_CLK_ETHIF) | BIT(MTK_CLK_ESW) | \
889 BIT(MTK_CLK_GP0) | BIT(MTK_CLK_GP1) | \
890 BIT(MTK_CLK_GP2) | BIT(MTK_CLK_FE) | \
891 BIT(MTK_CLK_SGMII_TX_250M) | \
892 BIT(MTK_CLK_SGMII_RX_250M) | \
893 BIT(MTK_CLK_SGMII_CDR_REF) | \
894 BIT(MTK_CLK_SGMII_CDR_FB) | \
895 BIT(MTK_CLK_SGMII2_TX_250M) | \
896 BIT(MTK_CLK_SGMII2_RX_250M) | \
897 BIT(MTK_CLK_SGMII2_CDR_REF) | \
898 BIT(MTK_CLK_SGMII2_CDR_FB) | \
899 BIT(MTK_CLK_SGMII_CK) | \
900 BIT(MTK_CLK_ETH2PLL) | BIT(MTK_CLK_SGMIITOP))
901
902#define MT7986_CLKS_BITMAP (BIT(MTK_CLK_FE) | BIT(MTK_CLK_GP2) | BIT(MTK_CLK_GP1) | \
903 BIT(MTK_CLK_WOCPU1) | BIT(MTK_CLK_WOCPU0) | \
904 BIT(MTK_CLK_SGMII_TX_250M) | \
905 BIT(MTK_CLK_SGMII_RX_250M) | \
906 BIT(MTK_CLK_SGMII_CDR_REF) | \
907 BIT(MTK_CLK_SGMII_CDR_FB) | \
908 BIT(MTK_CLK_SGMII2_TX_250M) | \
909 BIT(MTK_CLK_SGMII2_RX_250M) | \
910 BIT(MTK_CLK_SGMII2_CDR_REF) | \
911 BIT(MTK_CLK_SGMII2_CDR_FB))
912
developer255bba22021-07-27 15:16:33 +0800913
developer9e9fb4c2021-11-30 17:33:04 +0800914#define MT7981_CLKS_BITMAP (BIT(MTK_CLK_FE) | BIT(MTK_CLK_GP2) | BIT(MTK_CLK_GP1) | \
915 BIT(MTK_CLK_WOCPU0) | \
916 BIT(MTK_CLK_SGMII_TX_250M) | \
917 BIT(MTK_CLK_SGMII_RX_250M) | \
918 BIT(MTK_CLK_SGMII_CDR_REF) | \
919 BIT(MTK_CLK_SGMII_CDR_FB) | \
920 BIT(MTK_CLK_SGMII2_TX_250M) | \
921 BIT(MTK_CLK_SGMII2_RX_250M) | \
922 BIT(MTK_CLK_SGMII2_CDR_REF) | \
923 BIT(MTK_CLK_SGMII2_CDR_FB))
developerfd40db22021-04-29 10:08:25 +0800924enum mtk_dev_state {
925 MTK_HW_INIT,
926 MTK_RESETTING
927};
928
929/* struct mtk_tx_buf - This struct holds the pointers to the memory pointed at
930 * by the TX descriptor s
931 * @skb: The SKB pointer of the packet being sent
932 * @dma_addr0: The base addr of the first segment
933 * @dma_len0: The length of the first segment
934 * @dma_addr1: The base addr of the second segment
935 * @dma_len1: The length of the second segment
936 */
937struct mtk_tx_buf {
938 struct sk_buff *skb;
939 u32 flags;
940 DEFINE_DMA_UNMAP_ADDR(dma_addr0);
941 DEFINE_DMA_UNMAP_LEN(dma_len0);
942 DEFINE_DMA_UNMAP_ADDR(dma_addr1);
943 DEFINE_DMA_UNMAP_LEN(dma_len1);
944};
945
946/* struct mtk_tx_ring - This struct holds info describing a TX ring
947 * @dma: The descriptor ring
948 * @buf: The memory pointed at by the ring
949 * @phys: The physical addr of tx_buf
950 * @next_free: Pointer to the next free descriptor
951 * @last_free: Pointer to the last free descriptor
developerc4671b22021-05-28 13:16:42 +0800952 * @last_free_ptr: Hardware pointer value of the last free descriptor
developerfd40db22021-04-29 10:08:25 +0800953 * @thresh: The threshold of minimum amount of free descriptors
954 * @free_count: QDMA uses a linked list. Track how many free descriptors
955 * are present
956 */
957struct mtk_tx_ring {
developere9356982022-07-04 09:03:20 +0800958 void *dma;
developerfd40db22021-04-29 10:08:25 +0800959 struct mtk_tx_buf *buf;
960 dma_addr_t phys;
developere9356982022-07-04 09:03:20 +0800961 void *next_free;
962 void *last_free;
developerc4671b22021-05-28 13:16:42 +0800963 u32 last_free_ptr;
developerfd40db22021-04-29 10:08:25 +0800964 u16 thresh;
965 atomic_t free_count;
966 int dma_size;
developere9356982022-07-04 09:03:20 +0800967 void *dma_pdma; /* For MT7628/88 PDMA handling */
developerfd40db22021-04-29 10:08:25 +0800968 dma_addr_t phys_pdma;
969 int cpu_idx;
970};
971
972/* PDMA rx ring mode */
973enum mtk_rx_flags {
974 MTK_RX_FLAGS_NORMAL = 0,
975 MTK_RX_FLAGS_HWLRO,
976 MTK_RX_FLAGS_QDMA,
977};
978
979/* struct mtk_rx_ring - This struct holds info describing a RX ring
980 * @dma: The descriptor ring
981 * @data: The memory pointed at by the ring
982 * @phys: The physical addr of rx_buf
983 * @frag_size: How big can each fragment be
984 * @buf_size: The size of each packet buffer
985 * @calc_idx: The current head of ring
developer77d03a72021-06-06 00:06:00 +0800986 * @ring_no: The index of ring
developerfd40db22021-04-29 10:08:25 +0800987 */
988struct mtk_rx_ring {
developere9356982022-07-04 09:03:20 +0800989 void *dma;
developerfd40db22021-04-29 10:08:25 +0800990 u8 **data;
991 dma_addr_t phys;
992 u16 frag_size;
993 u16 buf_size;
994 u16 dma_size;
995 bool calc_idx_update;
996 u16 calc_idx;
997 u32 crx_idx_reg;
developer77d03a72021-06-06 00:06:00 +0800998 u32 ring_no;
developerfd40db22021-04-29 10:08:25 +0800999};
1000
developer18f46a82021-07-20 21:08:21 +08001001/* struct mtk_napi - This is the structure holding NAPI-related information,
1002 * and a mtk_napi struct is binding to one interrupt group
1003 * @napi: The NAPI struct
1004 * @rx_ring: Pointer to the memory holding info about the RX ring
1005 * @irq_grp_idx: The index indicates which interrupt group that this
1006 * mtk_napi is binding to
1007 */
1008struct mtk_napi {
1009 struct napi_struct napi;
1010 struct mtk_eth *eth;
1011 struct mtk_rx_ring *rx_ring;
1012 u32 irq_grp_no;
1013};
1014
developerfd40db22021-04-29 10:08:25 +08001015enum mkt_eth_capabilities {
1016 MTK_RGMII_BIT = 0,
1017 MTK_TRGMII_BIT,
1018 MTK_SGMII_BIT,
1019 MTK_ESW_BIT,
1020 MTK_GEPHY_BIT,
1021 MTK_MUX_BIT,
1022 MTK_INFRA_BIT,
1023 MTK_SHARED_SGMII_BIT,
1024 MTK_HWLRO_BIT,
developer18f46a82021-07-20 21:08:21 +08001025 MTK_RSS_BIT,
developerfd40db22021-04-29 10:08:25 +08001026 MTK_SHARED_INT_BIT,
1027 MTK_TRGMII_MT7621_CLK_BIT,
1028 MTK_QDMA_BIT,
developera2bdbd52021-05-31 19:10:17 +08001029 MTK_NETSYS_V2_BIT,
developerfd40db22021-04-29 10:08:25 +08001030 MTK_SOC_MT7628_BIT,
developer545abf02021-07-15 17:47:01 +08001031 MTK_RSTCTRL_PPE1_BIT,
developer255bba22021-07-27 15:16:33 +08001032 MTK_U3_COPHY_V2_BIT,
developerfd40db22021-04-29 10:08:25 +08001033
1034 /* MUX BITS*/
1035 MTK_ETH_MUX_GDM1_TO_GMAC1_ESW_BIT,
1036 MTK_ETH_MUX_GMAC2_GMAC0_TO_GEPHY_BIT,
1037 MTK_ETH_MUX_U3_GMAC2_TO_QPHY_BIT,
1038 MTK_ETH_MUX_GMAC1_GMAC2_TO_SGMII_RGMII_BIT,
1039 MTK_ETH_MUX_GMAC12_TO_GEPHY_SGMII_BIT,
1040
1041 /* PATH BITS */
1042 MTK_ETH_PATH_GMAC1_RGMII_BIT,
1043 MTK_ETH_PATH_GMAC1_TRGMII_BIT,
1044 MTK_ETH_PATH_GMAC1_SGMII_BIT,
1045 MTK_ETH_PATH_GMAC2_RGMII_BIT,
1046 MTK_ETH_PATH_GMAC2_SGMII_BIT,
1047 MTK_ETH_PATH_GMAC2_GEPHY_BIT,
1048 MTK_ETH_PATH_GDM1_ESW_BIT,
1049};
1050
1051/* Supported hardware group on SoCs */
1052#define MTK_RGMII BIT(MTK_RGMII_BIT)
1053#define MTK_TRGMII BIT(MTK_TRGMII_BIT)
1054#define MTK_SGMII BIT(MTK_SGMII_BIT)
1055#define MTK_ESW BIT(MTK_ESW_BIT)
1056#define MTK_GEPHY BIT(MTK_GEPHY_BIT)
1057#define MTK_MUX BIT(MTK_MUX_BIT)
1058#define MTK_INFRA BIT(MTK_INFRA_BIT)
1059#define MTK_SHARED_SGMII BIT(MTK_SHARED_SGMII_BIT)
1060#define MTK_HWLRO BIT(MTK_HWLRO_BIT)
developer18f46a82021-07-20 21:08:21 +08001061#define MTK_RSS BIT(MTK_RSS_BIT)
developerfd40db22021-04-29 10:08:25 +08001062#define MTK_SHARED_INT BIT(MTK_SHARED_INT_BIT)
1063#define MTK_TRGMII_MT7621_CLK BIT(MTK_TRGMII_MT7621_CLK_BIT)
1064#define MTK_QDMA BIT(MTK_QDMA_BIT)
developera2bdbd52021-05-31 19:10:17 +08001065#define MTK_NETSYS_V2 BIT(MTK_NETSYS_V2_BIT)
developerfd40db22021-04-29 10:08:25 +08001066#define MTK_SOC_MT7628 BIT(MTK_SOC_MT7628_BIT)
developer545abf02021-07-15 17:47:01 +08001067#define MTK_RSTCTRL_PPE1 BIT(MTK_RSTCTRL_PPE1_BIT)
developer255bba22021-07-27 15:16:33 +08001068#define MTK_U3_COPHY_V2 BIT(MTK_U3_COPHY_V2_BIT)
developerfd40db22021-04-29 10:08:25 +08001069
1070#define MTK_ETH_MUX_GDM1_TO_GMAC1_ESW \
1071 BIT(MTK_ETH_MUX_GDM1_TO_GMAC1_ESW_BIT)
1072#define MTK_ETH_MUX_GMAC2_GMAC0_TO_GEPHY \
1073 BIT(MTK_ETH_MUX_GMAC2_GMAC0_TO_GEPHY_BIT)
1074#define MTK_ETH_MUX_U3_GMAC2_TO_QPHY \
1075 BIT(MTK_ETH_MUX_U3_GMAC2_TO_QPHY_BIT)
1076#define MTK_ETH_MUX_GMAC1_GMAC2_TO_SGMII_RGMII \
1077 BIT(MTK_ETH_MUX_GMAC1_GMAC2_TO_SGMII_RGMII_BIT)
1078#define MTK_ETH_MUX_GMAC12_TO_GEPHY_SGMII \
1079 BIT(MTK_ETH_MUX_GMAC12_TO_GEPHY_SGMII_BIT)
1080
1081/* Supported path present on SoCs */
1082#define MTK_ETH_PATH_GMAC1_RGMII BIT(MTK_ETH_PATH_GMAC1_RGMII_BIT)
1083#define MTK_ETH_PATH_GMAC1_TRGMII BIT(MTK_ETH_PATH_GMAC1_TRGMII_BIT)
1084#define MTK_ETH_PATH_GMAC1_SGMII BIT(MTK_ETH_PATH_GMAC1_SGMII_BIT)
1085#define MTK_ETH_PATH_GMAC2_RGMII BIT(MTK_ETH_PATH_GMAC2_RGMII_BIT)
1086#define MTK_ETH_PATH_GMAC2_SGMII BIT(MTK_ETH_PATH_GMAC2_SGMII_BIT)
1087#define MTK_ETH_PATH_GMAC2_GEPHY BIT(MTK_ETH_PATH_GMAC2_GEPHY_BIT)
1088#define MTK_ETH_PATH_GDM1_ESW BIT(MTK_ETH_PATH_GDM1_ESW_BIT)
1089
1090#define MTK_GMAC1_RGMII (MTK_ETH_PATH_GMAC1_RGMII | MTK_RGMII)
1091#define MTK_GMAC1_TRGMII (MTK_ETH_PATH_GMAC1_TRGMII | MTK_TRGMII)
1092#define MTK_GMAC1_SGMII (MTK_ETH_PATH_GMAC1_SGMII | MTK_SGMII)
1093#define MTK_GMAC2_RGMII (MTK_ETH_PATH_GMAC2_RGMII | MTK_RGMII)
1094#define MTK_GMAC2_SGMII (MTK_ETH_PATH_GMAC2_SGMII | MTK_SGMII)
1095#define MTK_GMAC2_GEPHY (MTK_ETH_PATH_GMAC2_GEPHY | MTK_GEPHY)
1096#define MTK_GDM1_ESW (MTK_ETH_PATH_GDM1_ESW | MTK_ESW)
1097
1098/* MUXes present on SoCs */
1099/* 0: GDM1 -> GMAC1, 1: GDM1 -> ESW */
1100#define MTK_MUX_GDM1_TO_GMAC1_ESW (MTK_ETH_MUX_GDM1_TO_GMAC1_ESW | MTK_MUX)
1101
1102/* 0: GMAC2 -> GEPHY, 1: GMAC0 -> GePHY */
1103#define MTK_MUX_GMAC2_GMAC0_TO_GEPHY \
1104 (MTK_ETH_MUX_GMAC2_GMAC0_TO_GEPHY | MTK_MUX | MTK_INFRA)
1105
1106/* 0: U3 -> QPHY, 1: GMAC2 -> QPHY */
1107#define MTK_MUX_U3_GMAC2_TO_QPHY \
1108 (MTK_ETH_MUX_U3_GMAC2_TO_QPHY | MTK_MUX | MTK_INFRA)
1109
1110/* 2: GMAC1 -> SGMII, 3: GMAC2 -> SGMII */
1111#define MTK_MUX_GMAC1_GMAC2_TO_SGMII_RGMII \
1112 (MTK_ETH_MUX_GMAC1_GMAC2_TO_SGMII_RGMII | MTK_MUX | \
1113 MTK_SHARED_SGMII)
1114
1115/* 0: GMACx -> GEPHY, 1: GMACx -> SGMII where x is 1 or 2 */
1116#define MTK_MUX_GMAC12_TO_GEPHY_SGMII \
1117 (MTK_ETH_MUX_GMAC12_TO_GEPHY_SGMII | MTK_MUX)
1118
1119#define MTK_HAS_CAPS(caps, _x) (((caps) & (_x)) == (_x))
1120
1121#define MT7621_CAPS (MTK_GMAC1_RGMII | MTK_GMAC1_TRGMII | \
1122 MTK_GMAC2_RGMII | MTK_SHARED_INT | \
1123 MTK_TRGMII_MT7621_CLK | MTK_QDMA)
1124
1125#define MT7622_CAPS (MTK_GMAC1_RGMII | MTK_GMAC1_SGMII | MTK_GMAC2_RGMII | \
1126 MTK_GMAC2_SGMII | MTK_GDM1_ESW | \
1127 MTK_MUX_GDM1_TO_GMAC1_ESW | \
1128 MTK_MUX_GMAC1_GMAC2_TO_SGMII_RGMII | MTK_QDMA)
1129
1130#define MT7623_CAPS (MTK_GMAC1_RGMII | MTK_GMAC1_TRGMII | MTK_GMAC2_RGMII | \
1131 MTK_QDMA)
1132
1133#define MT7628_CAPS (MTK_SHARED_INT | MTK_SOC_MT7628)
1134
1135#define MT7629_CAPS (MTK_GMAC1_SGMII | MTK_GMAC2_SGMII | MTK_GMAC2_GEPHY | \
1136 MTK_GDM1_ESW | MTK_MUX_GDM1_TO_GMAC1_ESW | \
1137 MTK_MUX_GMAC2_GMAC0_TO_GEPHY | \
1138 MTK_MUX_U3_GMAC2_TO_QPHY | \
1139 MTK_MUX_GMAC12_TO_GEPHY_SGMII | MTK_QDMA)
1140
developerfd40db22021-04-29 10:08:25 +08001141#define MT7986_CAPS (MTK_GMAC1_SGMII | MTK_GMAC2_SGMII | \
1142 MTK_MUX_GMAC12_TO_GEPHY_SGMII | MTK_QDMA | \
developer8051e042022-04-08 13:26:36 +08001143 MTK_NETSYS_V2)
developerfd40db22021-04-29 10:08:25 +08001144
developer255bba22021-07-27 15:16:33 +08001145#define MT7981_CAPS (MTK_GMAC1_SGMII | MTK_GMAC2_SGMII | MTK_GMAC2_GEPHY | \
1146 MTK_MUX_GMAC12_TO_GEPHY_SGMII | MTK_QDMA | \
1147 MTK_MUX_U3_GMAC2_TO_QPHY | MTK_U3_COPHY_V2 | \
1148 MTK_NETSYS_V2)
1149
developere9356982022-07-04 09:03:20 +08001150struct mtk_tx_dma_desc_info {
1151 dma_addr_t addr;
1152 u32 size;
1153 u16 vlan_tci;
1154 u16 qid;
1155 u8 gso:1;
1156 u8 csum:1;
1157 u8 vlan:1;
1158 u8 first:1;
1159 u8 last:1;
1160};
1161
developerfd40db22021-04-29 10:08:25 +08001162/* struct mtk_eth_data - This is the structure holding all differences
1163 * among various plaforms
1164 * @ana_rgc3: The offset for register ANA_RGC3 related to
1165 * sgmiisys syscon
1166 * @caps Flags shown the extra capability for the SoC
1167 * @hw_features Flags shown HW features
1168 * @required_clks Flags shown the bitmap for required clocks on
1169 * the target SoC
1170 * @required_pctl A bool value to show whether the SoC requires
1171 * the extra setup for those pins used by GMAC.
developere9356982022-07-04 09:03:20 +08001172 * @txd_size Tx DMA descriptor size.
1173 * @rxd_size Rx DMA descriptor size.
1174 * @dma_max_len Max DMA tx/rx buffer length.
1175 * @dma_len_offset Tx/Rx DMA length field offset.
developerfd40db22021-04-29 10:08:25 +08001176 */
1177struct mtk_soc_data {
1178 u32 ana_rgc3;
1179 u32 caps;
1180 u32 required_clks;
1181 bool required_pctl;
1182 netdev_features_t hw_features;
1183 bool has_sram;
developere9356982022-07-04 09:03:20 +08001184 struct {
1185 u32 txd_size;
1186 u32 rxd_size;
1187 u32 dma_max_len;
1188 u32 dma_len_offset;
1189 } txrx;
developerfd40db22021-04-29 10:08:25 +08001190};
1191
1192/* currently no SoC has more than 2 macs */
1193#define MTK_MAX_DEVS 2
1194
1195#define MTK_SGMII_PHYSPEED_AN BIT(31)
1196#define MTK_SGMII_PHYSPEED_MASK GENMASK(2, 0)
1197#define MTK_SGMII_PHYSPEED_1000 BIT(0)
1198#define MTK_SGMII_PHYSPEED_2500 BIT(1)
developerf8ac94a2021-07-29 16:40:01 +08001199#define MTK_SGMII_PN_SWAP BIT(16)
developerfd40db22021-04-29 10:08:25 +08001200#define MTK_HAS_FLAGS(flags, _x) (((flags) & (_x)) == (_x))
1201
1202/* struct mtk_sgmii - This is the structure holding sgmii regmap and its
1203 * characteristics
1204 * @regmap: The register map pointing at the range used to setup
1205 * SGMII modes
1206 * @flags: The enum refers to which mode the sgmii wants to run on
1207 * @ana_rgc3: The offset refers to register ANA_RGC3 related to regmap
1208 */
1209
1210struct mtk_sgmii {
1211 struct regmap *regmap[MTK_MAX_DEVS];
1212 u32 flags[MTK_MAX_DEVS];
1213 u32 ana_rgc3;
1214};
1215
developer8051e042022-04-08 13:26:36 +08001216
1217/* struct mtk_reset_event - This is the structure holding statistics counters
1218 * for reset events
1219 * @count: The counter is used to record the number of events
1220 */
1221struct mtk_reset_event {
1222 u32 count[32];
1223};
1224
developera2613e62022-07-01 18:29:37 +08001225/* struct mtk_phylink_priv - This is the structure holding private data for phylink
1226 * @desc: Pointer to the memory holding info about the phylink gpio
1227 * @id: The element is used to record the phy index of phylink
1228 * @phyaddr: The element is used to record the phy address of phylink
1229 * @link: The element is used to record the phy link status of phylink
1230 */
1231struct mtk_phylink_priv {
1232 struct net_device *dev;
1233 struct gpio_desc *desc;
1234 char label[16];
1235 int id;
1236 int phyaddr;
1237 int link;
1238};
1239
developerfd40db22021-04-29 10:08:25 +08001240/* struct mtk_eth - This is the main datasructure for holding the state
1241 * of the driver
1242 * @dev: The device pointer
1243 * @base: The mapped register i/o base
1244 * @page_lock: Make sure that register operations are atomic
1245 * @tx_irq__lock: Make sure that IRQ register operations are atomic
1246 * @rx_irq__lock: Make sure that IRQ register operations are atomic
1247 * @dummy_dev: we run 2 netdevs on 1 physical DMA ring and need a
1248 * dummy for NAPI to work
1249 * @netdev: The netdev instances
1250 * @mac: Each netdev is linked to a physical MAC
1251 * @irq: The IRQ that we are using
1252 * @msg_enable: Ethtool msg level
1253 * @ethsys: The register map pointing at the range used to setup
1254 * MII modes
1255 * @infra: The register map pointing at the range used to setup
1256 * SGMII and GePHY path
1257 * @pctl: The register map pointing at the range used to setup
1258 * GMAC port drive/slew values
1259 * @dma_refcnt: track how many netdevs are using the DMA engine
1260 * @tx_ring: Pointer to the memory holding info about the TX ring
1261 * @rx_ring: Pointer to the memory holding info about the RX ring
1262 * @rx_ring_qdma: Pointer to the memory holding info about the QDMA RX ring
1263 * @tx_napi: The TX NAPI struct
1264 * @rx_napi: The RX NAPI struct
1265 * @scratch_ring: Newer SoCs need memory for a second HW managed TX ring
1266 * @phy_scratch_ring: physical address of scratch_ring
1267 * @scratch_head: The scratch memory that scratch_ring points to.
1268 * @clks: clock array for all clocks required
1269 * @mii_bus: If there is a bus we need to create an instance for it
1270 * @pending_work: The workqueue used to reset the dma ring
1271 * @state: Initialization and runtime state of the device
1272 * @soc: Holding specific data among vaious SoCs
1273 */
1274
1275struct mtk_eth {
1276 struct device *dev;
1277 void __iomem *base;
1278 spinlock_t page_lock;
1279 spinlock_t tx_irq_lock;
1280 spinlock_t rx_irq_lock;
1281 struct net_device dummy_dev;
1282 struct net_device *netdev[MTK_MAX_DEVS];
1283 struct mtk_mac *mac[MTK_MAX_DEVS];
developer18f46a82021-07-20 21:08:21 +08001284 int irq[MTK_MAX_IRQ_NUM];
developerfd40db22021-04-29 10:08:25 +08001285 u32 msg_enable;
1286 unsigned long sysclk;
1287 struct regmap *ethsys;
1288 struct regmap *infra;
1289 struct mtk_sgmii *sgmii;
1290 struct regmap *pctl;
1291 bool hwlro;
1292 refcount_t dma_refcnt;
1293 struct mtk_tx_ring tx_ring;
1294 struct mtk_rx_ring rx_ring[MTK_MAX_RX_RING_NUM];
1295 struct mtk_rx_ring rx_ring_qdma;
1296 struct napi_struct tx_napi;
developer18f46a82021-07-20 21:08:21 +08001297 struct mtk_napi rx_napi[MTK_RX_NAPI_NUM];
developere9356982022-07-04 09:03:20 +08001298 void *scratch_ring;
developer8051e042022-04-08 13:26:36 +08001299 struct mtk_reset_event reset_event;
developerfd40db22021-04-29 10:08:25 +08001300 dma_addr_t phy_scratch_ring;
1301 void *scratch_head;
1302 struct clk *clks[MTK_CLK_MAX];
1303
1304 struct mii_bus *mii_bus;
1305 struct work_struct pending_work;
1306 unsigned long state;
1307
1308 const struct mtk_soc_data *soc;
1309
1310 u32 tx_int_mask_reg;
1311 u32 tx_int_status_reg;
1312 u32 rx_dma_l4_valid;
1313 int ip_align;
developerd82e8372022-02-09 15:00:09 +08001314 spinlock_t syscfg0_lock;
developer8051e042022-04-08 13:26:36 +08001315 struct timer_list mtk_dma_monitor_timer;
developerfd40db22021-04-29 10:08:25 +08001316};
1317
1318/* struct mtk_mac - the structure that holds the info about the MACs of the
1319 * SoC
1320 * @id: The number of the MAC
1321 * @interface: Interface mode kept for detecting change in hw settings
1322 * @of_node: Our devicetree node
1323 * @hw: Backpointer to our main datastruture
1324 * @hw_stats: Packet statistics counter
1325 */
1326struct mtk_mac {
developerfb556ca2021-10-13 10:52:09 +08001327 unsigned int id;
developerfd40db22021-04-29 10:08:25 +08001328 phy_interface_t interface;
1329 unsigned int mode;
1330 int speed;
1331 struct device_node *of_node;
1332 struct phylink *phylink;
1333 struct phylink_config phylink_config;
developera2613e62022-07-01 18:29:37 +08001334 struct mtk_phylink_priv phylink_priv;
developerfd40db22021-04-29 10:08:25 +08001335 struct mtk_eth *hw;
1336 struct mtk_hw_stats *hw_stats;
1337 __be32 hwlro_ip[MTK_MAX_LRO_IP_CNT];
1338 int hwlro_ip_cnt;
1339};
1340
1341/* the struct describing the SoC. these are declared in the soc_xyz.c files */
1342extern const struct of_device_id of_mtk_match[];
developer77d03a72021-06-06 00:06:00 +08001343extern u32 mtk_hwlro_stats_ebl;
developerfd40db22021-04-29 10:08:25 +08001344
1345/* read the hardware status register */
1346void mtk_stats_update_mac(struct mtk_mac *mac);
1347
1348void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg);
1349u32 mtk_r32(struct mtk_eth *eth, unsigned reg);
developer8051e042022-04-08 13:26:36 +08001350u32 mtk_m32(struct mtk_eth *eth, u32 mask, u32 set, unsigned reg);
developerfd40db22021-04-29 10:08:25 +08001351
1352int mtk_sgmii_init(struct mtk_sgmii *ss, struct device_node *np,
1353 u32 ana_rgc3);
developerd8b55b62021-10-13 17:09:12 +08001354int mtk_sgmii_setup_mode_an(struct mtk_sgmii *ss, unsigned int id);
1355int mtk_sgmii_setup_mode_force(struct mtk_sgmii *ss, unsigned int id,
developerfd40db22021-04-29 10:08:25 +08001356 const struct phylink_link_state *state);
1357void mtk_sgmii_restart_an(struct mtk_eth *eth, int mac_id);
1358
1359int mtk_gmac_sgmii_path_setup(struct mtk_eth *eth, int mac_id);
1360int mtk_gmac_gephy_path_setup(struct mtk_eth *eth, int mac_id);
1361int mtk_gmac_rgmii_path_setup(struct mtk_eth *eth, int mac_id);
developer8051e042022-04-08 13:26:36 +08001362void mtk_gdm_config(struct mtk_eth *eth, u32 config);
1363void ethsys_reset(struct mtk_eth *eth, u32 reset_bits);
developerfd40db22021-04-29 10:08:25 +08001364
1365#endif /* MTK_ETH_H */