blob: 1a11af66b7ad222bb0abc884d728b5c0ab0b39f4 [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
developer089e8852022-09-28 14:43:46 +080023
24#if defined(CONFIG_MEDIATEK_NETSYS_V3)
25#define MTK_MAC_COUNT 3
26#else
developerfd40db22021-04-29 10:08:25 +080027#define MTK_MAC_COUNT 2
developer089e8852022-09-28 14:43:46 +080028#endif
29
developerfd40db22021-04-29 10:08:25 +080030#define MTK_RX_ETH_HLEN (VLAN_ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN)
31#define MTK_RX_HLEN (NET_SKB_PAD + MTK_RX_ETH_HLEN + NET_IP_ALIGN)
32#define MTK_DMA_DUMMY_DESC 0xffffffff
33#define MTK_DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | \
34 NETIF_MSG_PROBE | \
35 NETIF_MSG_LINK | \
36 NETIF_MSG_TIMER | \
37 NETIF_MSG_IFDOWN | \
38 NETIF_MSG_IFUP | \
39 NETIF_MSG_RX_ERR | \
40 NETIF_MSG_TX_ERR)
41#define MTK_HW_FEATURES (NETIF_F_IP_CSUM | \
42 NETIF_F_RXCSUM | \
43 NETIF_F_HW_VLAN_CTAG_TX | \
developerfd40db22021-04-29 10:08:25 +080044 NETIF_F_SG | NETIF_F_TSO | \
45 NETIF_F_TSO6 | \
46 NETIF_F_IPV6_CSUM)
47#define MTK_SET_FEATURES (NETIF_F_LRO | \
48 NETIF_F_HW_VLAN_CTAG_RX)
49#define MTK_HW_FEATURES_MT7628 (NETIF_F_SG | NETIF_F_RXCSUM)
50#define NEXT_DESP_IDX(X, Y) (((X) + 1) & ((Y) - 1))
51
developerfd40db22021-04-29 10:08:25 +080052#define MTK_HW_LRO_DMA_SIZE 8
53
54#define MTK_MAX_LRO_RX_LENGTH (4096 * 3)
55#define MTK_MAX_LRO_IP_CNT 2
56#define MTK_HW_LRO_TIMER_UNIT 1 /* 20 us */
57#define MTK_HW_LRO_REFRESH_TIME 50000 /* 1 sec. */
58#define MTK_HW_LRO_AGG_TIME 10 /* 200us */
59#define MTK_HW_LRO_AGE_TIME 50 /* 1ms */
60#define MTK_HW_LRO_MAX_AGG_CNT 64
61#define MTK_HW_LRO_BW_THRE 3000
62#define MTK_HW_LRO_REPLACE_DELTA 1000
63#define MTK_HW_LRO_SDL_REMAIN_ROOM 1522
64
developer8051e042022-04-08 13:26:36 +080065/* Frame Engine Global Configuration */
66#define MTK_FE_GLO_CFG 0x00
67#define MTK_FE_LINK_DOWN_P3 BIT(11)
68#define MTK_FE_LINK_DOWN_P4 BIT(12)
69
developerfd40db22021-04-29 10:08:25 +080070/* Frame Engine Global Reset Register */
71#define MTK_RST_GL 0x04
72#define RST_GL_PSE BIT(0)
73
74/* Frame Engine Interrupt Status Register */
developer8051e042022-04-08 13:26:36 +080075#define MTK_FE_INT_STATUS 0x08
76#define MTK_FE_INT_STATUS2 0x28
77#define MTK_FE_INT_ENABLE 0x0C
78#define MTK_FE_INT_FQ_EMPTY BIT(8)
79#define MTK_FE_INT_TSO_FAIL BIT(12)
80#define MTK_FE_INT_TSO_ILLEGAL BIT(13)
81#define MTK_FE_INT_TSO_ALIGN BIT(14)
82#define MTK_FE_INT_RFIFO_OV BIT(18)
83#define MTK_FE_INT_RFIFO_UF BIT(19)
developerfd40db22021-04-29 10:08:25 +080084#define MTK_GDM1_AF BIT(28)
85#define MTK_GDM2_AF BIT(29)
86
87/* PDMA HW LRO Alter Flow Timer Register */
88#define MTK_PDMA_LRO_ALT_REFRESH_TIMER 0x1c
89
90/* Frame Engine Interrupt Grouping Register */
91#define MTK_FE_INT_GRP 0x20
92
developer77d03a72021-06-06 00:06:00 +080093/* Frame Engine LRO auto-learn table info */
94#define MTK_FE_ALT_CF8 0x300
95#define MTK_FE_ALT_SGL_CFC 0x304
96#define MTK_FE_ALT_SEQ_CFC 0x308
97
developerfd40db22021-04-29 10:08:25 +080098/* CDMP Ingress Control Register */
99#define MTK_CDMQ_IG_CTRL 0x1400
100#define MTK_CDMQ_STAG_EN BIT(0)
101
102/* CDMP Ingress Control Register */
103#define MTK_CDMP_IG_CTRL 0x400
104#define MTK_CDMP_STAG_EN BIT(0)
105
106/* CDMP Exgress Control Register */
107#define MTK_CDMP_EG_CTRL 0x404
108
developer089e8852022-09-28 14:43:46 +0800109/* GDM Ingress Control Register */
110#define MTK_GDMA_FWD_CFG(x) ((x == MTK_GMAC3_ID) ? \
111 0x540 : 0x500 + (x * 0x1000))
developerfd40db22021-04-29 10:08:25 +0800112#define MTK_GDMA_SPECIAL_TAG BIT(24)
113#define MTK_GDMA_ICS_EN BIT(22)
114#define MTK_GDMA_TCS_EN BIT(21)
115#define MTK_GDMA_UCS_EN BIT(20)
developer089e8852022-09-28 14:43:46 +0800116#define MTK_GDMA_STRP_CRC BIT(16)
developerfd40db22021-04-29 10:08:25 +0800117#define MTK_GDMA_TO_PDMA 0x0
118#define MTK_GDMA_DROP_ALL 0x7777
119
developer089e8852022-09-28 14:43:46 +0800120/* GDM Egress Control Register */
121#define MTK_GDMA_EG_CTRL(x) ((x == MTK_GMAC3_ID) ? \
122 0x544 : 0x504 + (x * 0x1000))
123#define MTK_GDMA_XGDM_SEL BIT(31)
124
developerfd40db22021-04-29 10:08:25 +0800125/* Unicast Filter MAC Address Register - Low */
developer089e8852022-09-28 14:43:46 +0800126#define MTK_GDMA_MAC_ADRL(x) ((x == MTK_GMAC3_ID) ? \
127 0x548 : 0x508 + (x * 0x1000))
developerfd40db22021-04-29 10:08:25 +0800128
129/* Unicast Filter MAC Address Register - High */
developer089e8852022-09-28 14:43:46 +0800130#define MTK_GDMA_MAC_ADRH(x) ((x == MTK_GMAC3_ID) ? \
131 0x54C : 0x50C + (x * 0x1000))
developerfd40db22021-04-29 10:08:25 +0800132
133/* Internal SRAM offset */
developer089e8852022-09-28 14:43:46 +0800134#if defined(CONFIG_MEDIATEK_NETSYS_V3)
135#define MTK_ETH_SRAM_OFFSET 0x300000
136#else
developerfd40db22021-04-29 10:08:25 +0800137#define MTK_ETH_SRAM_OFFSET 0x40000
developer089e8852022-09-28 14:43:46 +0800138#endif
developerfd40db22021-04-29 10:08:25 +0800139
140/* FE global misc reg*/
141#define MTK_FE_GLO_MISC 0x124
142
developerfef9efd2021-06-16 18:28:09 +0800143/* PSE Free Queue Flow Control */
144#define PSE_FQFC_CFG1 0x100
145#define PSE_FQFC_CFG2 0x104
developer459b78e2022-07-01 17:25:10 +0800146#define PSE_NO_DROP_CFG 0x108
147#define PSE_PPE0_DROP 0x110
developerfef9efd2021-06-16 18:28:09 +0800148
developer15f760a2022-10-12 15:57:21 +0800149/* PSE Last FreeQ Page Request Control */
150#define PSE_DUMY_REQ 0x10C
151#define PSE_DUMMY_WORK_GDM(x) BIT(16 + (x))
152#define DUMMY_PAGE_THR 0x151
153
developerfd40db22021-04-29 10:08:25 +0800154/* PSE Input Queue Reservation Register*/
155#define PSE_IQ_REV(x) (0x140 + ((x - 1) * 0x4))
156
157/* PSE Output Queue Threshold Register*/
158#define PSE_OQ_TH(x) (0x160 + ((x - 1) * 0x4))
159
developerfef9efd2021-06-16 18:28:09 +0800160/* GDM and CDM Threshold */
161#define MTK_GDM2_THRES 0x1530
162#define MTK_CDMW0_THRES 0x164c
163#define MTK_CDMW1_THRES 0x1650
164#define MTK_CDME0_THRES 0x1654
165#define MTK_CDME1_THRES 0x1658
166#define MTK_CDMM_THRES 0x165c
167
developerfd40db22021-04-29 10:08:25 +0800168#define MTK_PDMA_V2 BIT(4)
developerfd40db22021-04-29 10:08:25 +0800169
developer089e8852022-09-28 14:43:46 +0800170#if defined(CONFIG_MEDIATEK_NETSYS_V3)
171#define PDMA_BASE 0x6800
172#define QDMA_BASE 0x4400
173#define WDMA_BASE(x) (0x4800 + ((x) * 0x400))
174#define PPE_BASE(x) ((x == 2) ? 0x2C00 : 0x2200 + ((x) * 0x400))
175#elif defined(CONFIG_MEDIATEK_NETSYS_V2)
developerfd40db22021-04-29 10:08:25 +0800176#define PDMA_BASE 0x6000
developerfd40db22021-04-29 10:08:25 +0800177#define QDMA_BASE 0x4400
developer8051e042022-04-08 13:26:36 +0800178#define WDMA_BASE(x) (0x4800 + ((x) * 0x400))
179#define PPE_BASE(x) (0x2200 + ((x) * 0x400))
developerfd40db22021-04-29 10:08:25 +0800180#else
181#define PDMA_BASE 0x0800
182#define QDMA_BASE 0x1800
developer8051e042022-04-08 13:26:36 +0800183#define WDMA_BASE(x) (0x2800 + ((x) * 0x400))
184#define PPE_BASE(x) (0xE00 + ((x) * 0x400))
developerfd40db22021-04-29 10:08:25 +0800185#endif
186/* PDMA RX Base Pointer Register */
187#define MTK_PRX_BASE_PTR0 (PDMA_BASE + 0x100)
188#define MTK_PRX_BASE_PTR_CFG(x) (MTK_PRX_BASE_PTR0 + (x * 0x10))
189
190/* PDMA RX Maximum Count Register */
191#define MTK_PRX_MAX_CNT0 (MTK_PRX_BASE_PTR0 + 0x04)
192#define MTK_PRX_MAX_CNT_CFG(x) (MTK_PRX_MAX_CNT0 + (x * 0x10))
193
194/* PDMA RX CPU Pointer Register */
195#define MTK_PRX_CRX_IDX0 (MTK_PRX_BASE_PTR0 + 0x08)
196#define MTK_PRX_CRX_IDX_CFG(x) (MTK_PRX_CRX_IDX0 + (x * 0x10))
197
developer77f3fd42021-10-05 15:16:05 +0800198/* PDMA RX DMA Pointer Register */
199#define MTK_PRX_DRX_IDX0 (MTK_PRX_BASE_PTR0 + 0x0c)
200#define MTK_PRX_DRX_IDX_CFG(x) (MTK_PRX_DRX_IDX0 + (x * 0x10))
201
developerfd40db22021-04-29 10:08:25 +0800202/* PDMA HW LRO Control Registers */
developer77d03a72021-06-06 00:06:00 +0800203#define BITS(m, n) (~(BIT(m) - 1) & ((BIT(n) - 1) | BIT(n)))
developer089e8852022-09-28 14:43:46 +0800204#if defined(CONFIG_MEDIATEK_NETSYS_V2) || defined(CONFIG_MEDIATEK_NETSYS_V3)
developer77d03a72021-06-06 00:06:00 +0800205#define MTK_MAX_RX_RING_NUM (8)
206#define MTK_HW_LRO_RING_NUM (4)
207#define IS_HW_LRO_RING(ring_no) (((ring_no) > 3) && ((ring_no) < 8))
208#define MTK_PDMA_LRO_CTRL_DW0 (PDMA_BASE + 0x408)
209#define MTK_LRO_ALT_SCORE_DELTA (PDMA_BASE + 0x41c)
210#define MTK_LRO_RX_RING0_CTRL_DW1 (PDMA_BASE + 0x438)
211#define MTK_LRO_RX_RING0_CTRL_DW2 (PDMA_BASE + 0x43c)
212#define MTK_LRO_RX_RING0_CTRL_DW3 (PDMA_BASE + 0x440)
213#define MTK_L3_CKS_UPD_EN BIT(19)
214#define MTK_LRO_CRSN_BNW BIT(22)
215#define MTK_LRO_RING_RELINGUISH_REQ (0xf << 24)
216#define MTK_LRO_RING_RELINGUISH_DONE (0xf << 28)
217#else
218#define MTK_MAX_RX_RING_NUM (4)
219#define MTK_HW_LRO_RING_NUM (3)
220#define IS_HW_LRO_RING(ring_no) (((ring_no) > 0) && ((ring_no) < 4))
221#define MTK_PDMA_LRO_CTRL_DW0 (PDMA_BASE + 0x180)
222#define MTK_LRO_ALT_SCORE_DELTA (PDMA_BASE + 0x24c)
223#define MTK_LRO_RX_RING0_CTRL_DW1 (PDMA_BASE + 0x328)
224#define MTK_LRO_RX_RING0_CTRL_DW2 (PDMA_BASE + 0x32c)
225#define MTK_LRO_RX_RING0_CTRL_DW3 (PDMA_BASE + 0x330)
226#define MTK_LRO_CRSN_BNW BIT(6)
developerfd40db22021-04-29 10:08:25 +0800227#define MTK_L3_CKS_UPD_EN BIT(7)
developer77d03a72021-06-06 00:06:00 +0800228#define MTK_LRO_RING_RELINGUISH_REQ (0x7 << 26)
229#define MTK_LRO_RING_RELINGUISH_DONE (0x7 << 29)
230#endif
231
232#define IS_NORMAL_RING(ring_no) ((ring_no) == 0)
233#define MTK_LRO_EN BIT(0)
developer18f46a82021-07-20 21:08:21 +0800234#define MTK_NON_LRO_MULTI_EN BIT(2)
235#define MTK_LRO_DLY_INT_EN BIT(5)
developerfd40db22021-04-29 10:08:25 +0800236#define MTK_LRO_ALT_PKT_CNT_MODE BIT(21)
developer77d03a72021-06-06 00:06:00 +0800237#define MTK_LRO_L4_CTRL_PSH_EN BIT(23)
238#define MTK_CTRL_DW0_SDL_OFFSET (3)
239#define MTK_CTRL_DW0_SDL_MASK BITS(3, 18)
developerfd40db22021-04-29 10:08:25 +0800240
241#define MTK_PDMA_LRO_CTRL_DW1 (MTK_PDMA_LRO_CTRL_DW0 + 0x04)
242#define MTK_PDMA_LRO_CTRL_DW2 (MTK_PDMA_LRO_CTRL_DW0 + 0x08)
243#define MTK_PDMA_LRO_CTRL_DW3 (MTK_PDMA_LRO_CTRL_DW0 + 0x0c)
244#define MTK_ADMA_MODE BIT(15)
245#define MTK_LRO_MIN_RXD_SDL (MTK_HW_LRO_SDL_REMAIN_ROOM << 16)
246
developer18f46a82021-07-20 21:08:21 +0800247/* PDMA RSS Control Registers */
developer089e8852022-09-28 14:43:46 +0800248#if defined(CONFIG_MEDIATEK_NETSYS_V2) || defined(CONFIG_MEDIATEK_NETSYS_V3)
developer18f46a82021-07-20 21:08:21 +0800249#define MTK_PDMA_RSS_GLO_CFG (PDMA_BASE + 0x800)
250#define MTK_RX_NAPI_NUM (2)
251#define MTK_MAX_IRQ_NUM (4)
252#else
253#define MTK_PDMA_RSS_GLO_CFG 0x3000
254#define MTK_RX_NAPI_NUM (1)
255#define MTK_MAX_IRQ_NUM (3)
256#endif
257#define MTK_RSS_RING1 (1)
258#define MTK_RSS_EN BIT(0)
259#define MTK_RSS_CFG_REQ BIT(2)
260#define MTK_RSS_IPV6_STATIC_HASH (0x7 << 8)
261#define MTK_RSS_IPV4_STATIC_HASH (0x7 << 12)
262#define MTK_RSS_INDR_TABLE_DW0 (MTK_PDMA_RSS_GLO_CFG + 0x50)
263#define MTK_RSS_INDR_TABLE_DW1 (MTK_PDMA_RSS_GLO_CFG + 0x54)
264#define MTK_RSS_INDR_TABLE_DW2 (MTK_PDMA_RSS_GLO_CFG + 0x58)
265#define MTK_RSS_INDR_TABLE_DW3 (MTK_PDMA_RSS_GLO_CFG + 0x5C)
266#define MTK_RSS_INDR_TABLE_DW4 (MTK_PDMA_RSS_GLO_CFG + 0x60)
267#define MTK_RSS_INDR_TABLE_DW5 (MTK_PDMA_RSS_GLO_CFG + 0x64)
268#define MTK_RSS_INDR_TABLE_DW6 (MTK_PDMA_RSS_GLO_CFG + 0x68)
269#define MTK_RSS_INDR_TABLE_DW7 (MTK_PDMA_RSS_GLO_CFG + 0x6C)
270#define MTK_RSS_INDR_TABLE_SIZE4 0x44444444
271
developerfd40db22021-04-29 10:08:25 +0800272/* PDMA Global Configuration Register */
273#define MTK_PDMA_GLO_CFG (PDMA_BASE + 0x204)
developer77d03a72021-06-06 00:06:00 +0800274#define MTK_RX_DMA_LRO_EN BIT(8)
developerfd40db22021-04-29 10:08:25 +0800275#define MTK_MULTI_EN BIT(10)
276#define MTK_PDMA_SIZE_8DWORDS (1 << 4)
277
developer77d03a72021-06-06 00:06:00 +0800278/* PDMA Global Configuration Register */
279#define MTK_PDMA_RX_CFG (PDMA_BASE + 0x210)
280#define MTK_PDMA_LRO_SDL (0x3000)
281#define MTK_RX_CFG_SDL_OFFSET (16)
282
developerfd40db22021-04-29 10:08:25 +0800283/* PDMA Reset Index Register */
284#define MTK_PDMA_RST_IDX (PDMA_BASE + 0x208)
285#define MTK_PST_DRX_IDX0 BIT(16)
286#define MTK_PST_DRX_IDX_CFG(x) (MTK_PST_DRX_IDX0 << (x))
287
288/* PDMA Delay Interrupt Register */
289#define MTK_PDMA_DELAY_INT (PDMA_BASE + 0x20c)
developer089e8852022-09-28 14:43:46 +0800290#define MTK_PDMA_RSS_DELAY_INT (PDMA_BASE + 0x2c0)
developerfd40db22021-04-29 10:08:25 +0800291#define MTK_PDMA_DELAY_RX_EN BIT(15)
292#define MTK_PDMA_DELAY_RX_PINT 4
293#define MTK_PDMA_DELAY_RX_PINT_SHIFT 8
294#define MTK_PDMA_DELAY_RX_PTIME 4
295#define MTK_PDMA_DELAY_RX_DELAY \
296 (MTK_PDMA_DELAY_RX_EN | MTK_PDMA_DELAY_RX_PTIME | \
297 (MTK_PDMA_DELAY_RX_PINT << MTK_PDMA_DELAY_RX_PINT_SHIFT))
298
299/* PDMA Interrupt Status Register */
300#define MTK_PDMA_INT_STATUS (PDMA_BASE + 0x220)
301
302/* PDMA Interrupt Mask Register */
303#define MTK_PDMA_INT_MASK (PDMA_BASE + 0x228)
304
developerfd40db22021-04-29 10:08:25 +0800305/* PDMA Interrupt grouping registers */
306#define MTK_PDMA_INT_GRP1 (PDMA_BASE + 0x250)
307#define MTK_PDMA_INT_GRP2 (PDMA_BASE + 0x254)
developer089e8852022-09-28 14:43:46 +0800308#if defined(CONFIG_MEDIATEK_NETSYS_V2) || defined(CONFIG_MEDIATEK_NETSYS_V3)
developer18f46a82021-07-20 21:08:21 +0800309#define MTK_PDMA_INT_GRP3 (PDMA_BASE + 0x258)
310#else
311#define MTK_PDMA_INT_GRP3 (PDMA_BASE + 0x22c)
312#endif
313#define MTK_LRO_RX1_DLY_INT 0xa70
314#define MTK_MAX_DELAY_INT 0x8f0f8f0f
developerfd40db22021-04-29 10:08:25 +0800315
316/* PDMA HW LRO IP Setting Registers */
developer089e8852022-09-28 14:43:46 +0800317#if defined(CONFIG_MEDIATEK_NETSYS_V2) || defined(CONFIG_MEDIATEK_NETSYS_V3)
developer77d03a72021-06-06 00:06:00 +0800318#define MTK_LRO_RX_RING0_DIP_DW0 (PDMA_BASE + 0x414)
319#else
developerfd40db22021-04-29 10:08:25 +0800320#define MTK_LRO_RX_RING0_DIP_DW0 (PDMA_BASE + 0x304)
developer77d03a72021-06-06 00:06:00 +0800321#endif
developerfd40db22021-04-29 10:08:25 +0800322#define MTK_LRO_DIP_DW0_CFG(x) (MTK_LRO_RX_RING0_DIP_DW0 + (x * 0x40))
323#define MTK_RING_MYIP_VLD BIT(9)
324
developer77d03a72021-06-06 00:06:00 +0800325/* PDMA HW LRO ALT Debug Registers */
326#define MTK_LRO_ALT_DBG (PDMA_BASE + 0x440)
327#define MTK_LRO_ALT_INDEX_OFFSET (8)
328
329/* PDMA HW LRO ALT Data Registers */
330#define MTK_LRO_ALT_DBG_DATA (PDMA_BASE + 0x444)
331
developerfd40db22021-04-29 10:08:25 +0800332/* PDMA HW LRO Ring Control Registers */
developerfd40db22021-04-29 10:08:25 +0800333#define MTK_LRO_CTRL_DW1_CFG(x) (MTK_LRO_RX_RING0_CTRL_DW1 + (x * 0x40))
334#define MTK_LRO_CTRL_DW2_CFG(x) (MTK_LRO_RX_RING0_CTRL_DW2 + (x * 0x40))
335#define MTK_LRO_CTRL_DW3_CFG(x) (MTK_LRO_RX_RING0_CTRL_DW3 + (x * 0x40))
336#define MTK_RING_AGE_TIME_L ((MTK_HW_LRO_AGE_TIME & 0x3ff) << 22)
337#define MTK_RING_AGE_TIME_H ((MTK_HW_LRO_AGE_TIME >> 10) & 0x3f)
developer18f46a82021-07-20 21:08:21 +0800338#define MTK_RING_PSE_MODE (1 << 6)
developerfd40db22021-04-29 10:08:25 +0800339#define MTK_RING_AUTO_LERAN_MODE (3 << 6)
340#define MTK_RING_VLD BIT(8)
341#define MTK_RING_MAX_AGG_TIME ((MTK_HW_LRO_AGG_TIME & 0xffff) << 10)
342#define MTK_RING_MAX_AGG_CNT_L ((MTK_HW_LRO_MAX_AGG_CNT & 0x3f) << 26)
343#define MTK_RING_MAX_AGG_CNT_H ((MTK_HW_LRO_MAX_AGG_CNT >> 6) & 0x3)
344
developer77d03a72021-06-06 00:06:00 +0800345/* LRO_RX_RING_CTRL_DW masks */
346#define MTK_LRO_RING_AGG_TIME_MASK BITS(10, 25)
347#define MTK_LRO_RING_AGG_CNT_L_MASK BITS(26, 31)
348#define MTK_LRO_RING_AGG_CNT_H_MASK BITS(0, 1)
349#define MTK_LRO_RING_AGE_TIME_L_MASK BITS(22, 31)
350#define MTK_LRO_RING_AGE_TIME_H_MASK BITS(0, 5)
351
352/* LRO_RX_RING_CTRL_DW0 offsets */
353#define MTK_RX_IPV6_FORCE_OFFSET (0)
354#define MTK_RX_IPV4_FORCE_OFFSET (1)
355
356/* LRO_RX_RING_CTRL_DW1 offsets */
357#define MTK_LRO_RING_AGE_TIME_L_OFFSET (22)
358
359/* LRO_RX_RING_CTRL_DW2 offsets */
360#define MTK_LRO_RING_AGE_TIME_H_OFFSET (0)
361#define MTK_RX_MODE_OFFSET (6)
362#define MTK_RX_PORT_VALID_OFFSET (8)
363#define MTK_RX_MYIP_VALID_OFFSET (9)
364#define MTK_LRO_RING_AGG_TIME_OFFSET (10)
365#define MTK_LRO_RING_AGG_CNT_L_OFFSET (26)
366
367/* LRO_RX_RING_CTRL_DW3 offsets */
368#define MTK_LRO_RING_AGG_CNT_H_OFFSET (0)
369
370/* LRO_RX_RING_STP_DTP_DW offsets */
371#define MTK_RX_TCP_DEST_PORT_OFFSET (0)
372#define MTK_RX_TCP_SRC_PORT_OFFSET (16)
373
developerfd40db22021-04-29 10:08:25 +0800374/* QDMA TX Queue Configuration Registers */
375#define MTK_QTX_CFG(x) (QDMA_BASE + (x * 0x10))
376#define QDMA_RES_THRES 4
377
378/* QDMA TX Queue Scheduler Registers */
379#define MTK_QTX_SCH(x) (QDMA_BASE + 4 + (x * 0x10))
380
381/* QDMA RX Base Pointer Register */
382#define MTK_QRX_BASE_PTR0 (QDMA_BASE + 0x100)
383#define MTK_QRX_BASE_PTR_CFG(x) (MTK_QRX_BASE_PTR0 + ((x) * 0x10))
384
385/* QDMA RX Maximum Count Register */
386#define MTK_QRX_MAX_CNT0 (QDMA_BASE + 0x104)
387#define MTK_QRX_MAX_CNT_CFG(x) (MTK_QRX_MAX_CNT0 + ((x) * 0x10))
388
389/* QDMA RX CPU Pointer Register */
390#define MTK_QRX_CRX_IDX0 (QDMA_BASE + 0x108)
391#define MTK_QRX_CRX_IDX_CFG(x) (MTK_QRX_CRX_IDX0 + ((x) * 0x10))
392
393/* QDMA RX DMA Pointer Register */
394#define MTK_QRX_DRX_IDX0 (QDMA_BASE + 0x10c)
395
developer329d8ee2022-08-02 08:49:42 +0800396/* QDMA Page Configuration Register */
397#define MTK_QDMA_PAGE (QDMA_BASE + 0x1f0)
398
developerfd40db22021-04-29 10:08:25 +0800399/* QDMA Global Configuration Register */
400#define MTK_QDMA_GLO_CFG (QDMA_BASE + 0x204)
401#define MTK_RX_2B_OFFSET BIT(31)
developer58ab5842022-06-01 15:10:25 +0800402#define MTK_PKT_RX_WDONE BIT(27)
developerfd40db22021-04-29 10:08:25 +0800403#define MTK_RX_BT_32DWORDS (3 << 11)
404#define MTK_NDP_CO_PRO BIT(10)
405#define MTK_TX_WB_DDONE BIT(6)
406#define MTK_DMA_SIZE_16DWORDS (2 << 4)
407#define MTK_DMA_SIZE_32DWORDS (3 << 4)
408#define MTK_RX_DMA_BUSY BIT(3)
409#define MTK_TX_DMA_BUSY BIT(1)
410#define MTK_RX_DMA_EN BIT(2)
411#define MTK_TX_DMA_EN BIT(0)
412#define MTK_DMA_BUSY_TIMEOUT HZ
413
414/* QDMA V2 Global Configuration Register */
415#define MTK_CHK_DDONE_EN BIT(28)
416#define MTK_DMAD_WR_WDONE BIT(26)
417#define MTK_WCOMP_EN BIT(24)
developer2cdef092022-04-15 17:27:55 +0800418#define MTK_RESV_BUF (0x80 << 16)
developerfd40db22021-04-29 10:08:25 +0800419#define MTK_MUTLI_CNT (0x4 << 12)
developer19d84562022-04-21 17:01:06 +0800420#define MTK_RESV_BUF_MASK (0xff << 16)
developerfd40db22021-04-29 10:08:25 +0800421
422/* QDMA Reset Index Register */
423#define MTK_QDMA_RST_IDX (QDMA_BASE + 0x208)
424
425/* QDMA Delay Interrupt Register */
426#define MTK_QDMA_DELAY_INT (QDMA_BASE + 0x20c)
427
428/* QDMA Flow Control Register */
429#define MTK_QDMA_FC_THRES (QDMA_BASE + 0x210)
430#define FC_THRES_DROP_MODE BIT(20)
431#define FC_THRES_DROP_EN (7 << 16)
432#define FC_THRES_MIN 0x4444
433
434/* QDMA Interrupt Status Register */
435#define MTK_QDMA_INT_STATUS (QDMA_BASE + 0x218)
developer089e8852022-09-28 14:43:46 +0800436#if defined(CONFIG_MEDIATEK_NETSYS_V2) || defined(CONFIG_MEDIATEK_NETSYS_V3)
437#define MTK_RX_DONE_INT(ring_no) \
438 (MTK_HAS_CAPS(eth->soc->caps, MTK_RSS) ? (BIT(24 + (ring_no))) : \
439 ((ring_no) ? BIT(16 + (ring_no)) : BIT(14)))
developerfd40db22021-04-29 10:08:25 +0800440#else
developer18f46a82021-07-20 21:08:21 +0800441#define MTK_RX_DONE_INT(ring_no) \
442 ((ring_no)? BIT(24 + (ring_no)) : BIT(30))
developerfd40db22021-04-29 10:08:25 +0800443#endif
444#define MTK_RX_DONE_INT3 BIT(19)
445#define MTK_RX_DONE_INT2 BIT(18)
446#define MTK_RX_DONE_INT1 BIT(17)
447#define MTK_RX_DONE_INT0 BIT(16)
448#define MTK_TX_DONE_INT3 BIT(3)
449#define MTK_TX_DONE_INT2 BIT(2)
450#define MTK_TX_DONE_INT1 BIT(1)
451#define MTK_TX_DONE_INT0 BIT(0)
developerfd40db22021-04-29 10:08:25 +0800452#define MTK_TX_DONE_DLY BIT(28)
453#define MTK_TX_DONE_INT MTK_TX_DONE_DLY
454
455/* QDMA Interrupt grouping registers */
456#define MTK_QDMA_INT_GRP1 (QDMA_BASE + 0x220)
457#define MTK_QDMA_INT_GRP2 (QDMA_BASE + 0x224)
458#define MTK_RLS_DONE_INT BIT(0)
459
460/* QDMA Interrupt Status Register */
461#define MTK_QDMA_INT_MASK (QDMA_BASE + 0x21c)
462
developer8051e042022-04-08 13:26:36 +0800463/* QDMA DMA FSM */
464#define MTK_QDMA_FSM (QDMA_BASE + 0x234)
465
developerfd40db22021-04-29 10:08:25 +0800466/* QDMA Interrupt Mask Register */
467#define MTK_QDMA_HRED2 (QDMA_BASE + 0x244)
468
469/* QDMA TX Forward CPU Pointer Register */
470#define MTK_QTX_CTX_PTR (QDMA_BASE +0x300)
471
472/* QDMA TX Forward DMA Pointer Register */
473#define MTK_QTX_DTX_PTR (QDMA_BASE +0x304)
474
developer8051e042022-04-08 13:26:36 +0800475/* QDMA TX Forward DMA Counter */
476#define MTK_QDMA_FWD_CNT (QDMA_BASE + 0x308)
477
developerfd40db22021-04-29 10:08:25 +0800478/* QDMA TX Release CPU Pointer Register */
479#define MTK_QTX_CRX_PTR (QDMA_BASE +0x310)
480
481/* QDMA TX Release DMA Pointer Register */
482#define MTK_QTX_DRX_PTR (QDMA_BASE +0x314)
483
484/* QDMA FQ Head Pointer Register */
485#define MTK_QDMA_FQ_HEAD (QDMA_BASE +0x320)
486
487/* QDMA FQ Head Pointer Register */
488#define MTK_QDMA_FQ_TAIL (QDMA_BASE +0x324)
489
490/* QDMA FQ Free Page Counter Register */
491#define MTK_QDMA_FQ_CNT (QDMA_BASE +0x328)
492
493/* QDMA FQ Free Page Buffer Length Register */
494#define MTK_QDMA_FQ_BLEN (QDMA_BASE +0x32c)
495
developer8051e042022-04-08 13:26:36 +0800496/* WDMA Registers */
497#define MTK_WDMA_DTX_PTR(x) (WDMA_BASE(x) + 0xC)
498#define MTK_WDMA_GLO_CFG(x) (WDMA_BASE(x) + 0x204)
499#define MTK_WDMA_TX_DBG_MON0(x) (WDMA_BASE(x) + 0x230)
500#define MTK_CDM_TXFIFO_RDY BIT(7)
501
developerfd40db22021-04-29 10:08:25 +0800502/* GMA1 Received Good Byte Count Register */
developer089e8852022-09-28 14:43:46 +0800503#if defined(CONFIG_MEDIATEK_NETSYS_V2) || defined(CONFIG_MEDIATEK_NETSYS_V3)
developerfd40db22021-04-29 10:08:25 +0800504#define MTK_GDM1_TX_GBCNT 0x1C00
505#else
506#define MTK_GDM1_TX_GBCNT 0x2400
507#endif
developer089e8852022-09-28 14:43:46 +0800508
509#if defined(CONFIG_MEDIATEK_NETSYS_V3)
510#define MTK_STAT_OFFSET 0x80
511#else
developerfd40db22021-04-29 10:08:25 +0800512#define MTK_STAT_OFFSET 0x40
developer089e8852022-09-28 14:43:46 +0800513#endif
developerfd40db22021-04-29 10:08:25 +0800514
515/* QDMA TX NUM */
516#define MTK_QDMA_TX_NUM 16
developer797e46c2022-07-29 12:05:32 +0800517#define MTK_QDMA_PAGE_NUM 8
developerfd40db22021-04-29 10:08:25 +0800518#define MTK_QDMA_TX_MASK ((MTK_QDMA_TX_NUM) - 1)
519#define QID_LOW_BITS(x) ((x) & 0xf)
520#define QID_HIGH_BITS(x) ((((x) >> 4) & 0x3) << 20)
521#define QID_BITS_V2(x) (((x) & 0x3f) << 16)
522
developerdc0d45f2021-12-27 13:01:22 +0800523#define MTK_QDMA_GMAC2_QID 8
524
developerfd40db22021-04-29 10:08:25 +0800525/* QDMA V2 descriptor txd6 */
526#define TX_DMA_INS_VLAN_V2 BIT(16)
527
528/* QDMA V2 descriptor txd5 */
529#define TX_DMA_CHKSUM_V2 (0x7 << 28)
530#define TX_DMA_TSO_V2 BIT(31)
developer089e8852022-09-28 14:43:46 +0800531#define TX_DMA_SPTAG_V3 BIT(27)
developerfd40db22021-04-29 10:08:25 +0800532
533/* QDMA V2 descriptor txd4 */
534#define TX_DMA_FPORT_SHIFT_V2 8
535#define TX_DMA_FPORT_MASK_V2 0xf
536#define TX_DMA_SWC_V2 BIT(30)
537
developerfd40db22021-04-29 10:08:25 +0800538#define MTK_TX_DMA_BUF_LEN 0x3fff
developere9356982022-07-04 09:03:20 +0800539#define MTK_TX_DMA_BUF_LEN_V2 0xffff
developerfd40db22021-04-29 10:08:25 +0800540#define MTK_TX_DMA_BUF_SHIFT 16
developere9356982022-07-04 09:03:20 +0800541#define MTK_TX_DMA_BUF_SHIFT_V2 8
developerfd40db22021-04-29 10:08:25 +0800542
developerfd40db22021-04-29 10:08:25 +0800543#define RX_DMA_SPORT_SHIFT 19
developere9356982022-07-04 09:03:20 +0800544#define RX_DMA_SPORT_SHIFT_V2 26
developerfd40db22021-04-29 10:08:25 +0800545#define RX_DMA_SPORT_MASK 0x7
developere9356982022-07-04 09:03:20 +0800546#define RX_DMA_SPORT_MASK_V2 0xf
developerfd40db22021-04-29 10:08:25 +0800547
548/* QDMA descriptor txd4 */
549#define TX_DMA_CHKSUM (0x7 << 29)
550#define TX_DMA_TSO BIT(28)
551#define TX_DMA_FPORT_SHIFT 25
552#define TX_DMA_FPORT_MASK 0x7
553#define TX_DMA_INS_VLAN BIT(16)
554
555/* QDMA descriptor txd3 */
556#define TX_DMA_OWNER_CPU BIT(31)
557#define TX_DMA_LS0 BIT(30)
developere9356982022-07-04 09:03:20 +0800558#define TX_DMA_PLEN0(_x) (((_x) & eth->soc->txrx.dma_max_len) << eth->soc->txrx.dma_len_offset)
559#define TX_DMA_PLEN1(_x) ((_x) & eth->soc->txrx.dma_max_len)
developerfd40db22021-04-29 10:08:25 +0800560#define TX_DMA_SWC BIT(14)
developer089e8852022-09-28 14:43:46 +0800561#define TX_DMA_SDP1(_x) ((((u64)(_x)) >> 32) & 0xf)
developerfd40db22021-04-29 10:08:25 +0800562
563/* PDMA on MT7628 */
564#define TX_DMA_DONE BIT(31)
565#define TX_DMA_LS1 BIT(14)
566#define TX_DMA_DESP2_DEF (TX_DMA_LS0 | TX_DMA_DONE)
567
568/* QDMA descriptor rxd2 */
569#define RX_DMA_DONE BIT(31)
570#define RX_DMA_LSO BIT(30)
developere9356982022-07-04 09:03:20 +0800571#define RX_DMA_PLEN0(_x) (((_x) & eth->soc->txrx.dma_max_len) << eth->soc->txrx.dma_len_offset)
572#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 +0800573#define RX_DMA_GET_AGG_CNT(_x) (((_x) >> 2) & 0xff)
574#define RX_DMA_GET_REV(_x) (((_x) >> 10) & 0x1f)
developerfd40db22021-04-29 10:08:25 +0800575#define RX_DMA_VTAG BIT(15)
developer089e8852022-09-28 14:43:46 +0800576#define RX_DMA_SDP1(_x) ((((u64)(_x)) >> 32) & 0xf)
developerfd40db22021-04-29 10:08:25 +0800577
578/* QDMA descriptor rxd3 */
579#define RX_DMA_VID(_x) ((_x) & VLAN_VID_MASK)
580#define RX_DMA_TCI(_x) ((_x) & (VLAN_PRIO_MASK | VLAN_VID_MASK))
581#define RX_DMA_VPID(_x) (((_x) >> 16) & 0xffff)
582
583/* QDMA descriptor rxd4 */
584#define RX_DMA_L4_VALID BIT(24)
585#define RX_DMA_L4_VALID_PDMA BIT(30) /* when PDMA is used */
586#define RX_DMA_SPECIAL_TAG BIT(22) /* switch header in packet */
587
588#define RX_DMA_GET_SPORT(_x) (((_x) >> RX_DMA_SPORT_SHIFT) & RX_DMA_SPORT_MASK)
developere9356982022-07-04 09:03:20 +0800589#define RX_DMA_GET_SPORT_V2(_x) (((_x) >> RX_DMA_SPORT_SHIFT_V2) & RX_DMA_SPORT_MASK_V2)
developerfd40db22021-04-29 10:08:25 +0800590
591/* PDMA V2 descriptor rxd3 */
592#define RX_DMA_VTAG_V2 BIT(0)
593#define RX_DMA_L4_VALID_V2 BIT(2)
594
595/* PDMA V2 descriptor rxd4 */
596#define RX_DMA_VID_V2(_x) RX_DMA_VID(_x)
developer255bba22021-07-27 15:16:33 +0800597#define RX_DMA_TCI_V2(_x) RX_DMA_TCI(_x)
598#define RX_DMA_VPID_V2(_x) RX_DMA_VPID(_x)
developerfd40db22021-04-29 10:08:25 +0800599
developer77d03a72021-06-06 00:06:00 +0800600/* PDMA V2 descriptor rxd6 */
601#define RX_DMA_GET_FLUSH_RSN_V2(_x) ((_x) & 0x7)
602#define RX_DMA_GET_AGG_CNT_V2(_x) (((_x) >> 16) & 0xff)
developer006325c2022-10-06 16:39:50 +0800603#define RX_DMA_GET_TOPS_CRSN(_x) (((_x) >> 24) & 0xff)
developer77d03a72021-06-06 00:06:00 +0800604
developerc8acd8d2022-11-10 09:07:10 +0800605/* PHY Polling and SMI Master Control registers */
606#define MTK_PPSC 0x10000
607#define PPSC_MDC_CFG GENMASK(29, 24)
608#define PPSC_MDC_TURBO BIT(20)
609
developerfd40db22021-04-29 10:08:25 +0800610/* PHY Indirect Access Control registers */
611#define MTK_PHY_IAC 0x10004
612#define PHY_IAC_ACCESS BIT(31)
613#define PHY_IAC_READ BIT(19)
developer599cda42022-05-24 15:13:31 +0800614#define PHY_IAC_READ_C45 (3 << 18)
615#define PHY_IAC_ADDR_C45 (0 << 18)
developerfd40db22021-04-29 10:08:25 +0800616#define PHY_IAC_WRITE BIT(18)
617#define PHY_IAC_START BIT(16)
developer599cda42022-05-24 15:13:31 +0800618#define PHY_IAC_START_C45 (0 << 16)
developerfd40db22021-04-29 10:08:25 +0800619#define PHY_IAC_ADDR_SHIFT 20
620#define PHY_IAC_REG_SHIFT 25
621#define PHY_IAC_TIMEOUT HZ
622
developerc8acd8d2022-11-10 09:07:10 +0800623#if defined(CONFIG_MEDIATEK_NETSYS_V3)
624#define MTK_MAC_MISC 0x10010
625#else
developerfd40db22021-04-29 10:08:25 +0800626#define MTK_MAC_MISC 0x1000c
developerc8acd8d2022-11-10 09:07:10 +0800627#endif
628#define MISC_MDC_TURBO BIT(4)
developerfd40db22021-04-29 10:08:25 +0800629#define MTK_MUX_TO_ESW BIT(0)
630
developer089e8852022-09-28 14:43:46 +0800631/* XMAC status registers */
632#define MTK_XGMAC_STS(x) ((x == MTK_GMAC3_ID) ? 0x1001C : 0x1000C)
633#define MTK_XGMAC_FORCE_LINK BIT(15)
634#define MTK_USXGMII_PCS_LINK BIT(8)
635#define MTK_XGMAC_RX_FC BIT(5)
636#define MTK_XGMAC_TX_FC BIT(4)
637#define MTK_USXGMII_PCS_MODE GENMASK(3, 1)
638#define MTK_XGMAC_LINK_STS BIT(0)
639
640/* GSW bridge registers */
641#define MTK_GSW_CFG (0x10080)
642#define GSWTX_IPG_MASK GENMASK(19, 16)
643#define GSWTX_IPG_SHIFT 16
644#define GSWRX_IPG_MASK GENMASK(3, 0)
645#define GSWRX_IPG_SHIFT 0
646#define GSW_IPG_11 11
647
developerfd40db22021-04-29 10:08:25 +0800648/* Mac control registers */
649#define MTK_MAC_MCR(x) (0x10100 + (x * 0x100))
650#define MAC_MCR_MAX_RX_1536 BIT(24)
developerd8a29752022-08-19 13:32:03 +0800651#define MAC_MCR_IPG_CFG (BIT(18) | BIT(16) | BIT(12))
developerfd40db22021-04-29 10:08:25 +0800652#define MAC_MCR_FORCE_MODE BIT(15)
653#define MAC_MCR_TX_EN BIT(14)
654#define MAC_MCR_RX_EN BIT(13)
655#define MAC_MCR_BACKOFF_EN BIT(9)
656#define MAC_MCR_BACKPR_EN BIT(8)
657#define MAC_MCR_FORCE_RX_FC BIT(5)
658#define MAC_MCR_FORCE_TX_FC BIT(4)
659#define MAC_MCR_SPEED_1000 BIT(3)
660#define MAC_MCR_SPEED_100 BIT(2)
661#define MAC_MCR_FORCE_DPX BIT(1)
662#define MAC_MCR_FORCE_LINK BIT(0)
663#define MAC_MCR_FORCE_LINK_DOWN (MAC_MCR_FORCE_MODE)
664
developer089e8852022-09-28 14:43:46 +0800665/* XFI Mac control registers */
666#define MTK_XMAC_MCR(x) (0x12000 + ((x - 1) * 0x1000))
667#define XMAC_MCR_TRX_DISABLE 0xf
668#define XMAC_MCR_FORCE_TX_FC BIT(5)
669#define XMAC_MCR_FORCE_RX_FC BIT(4)
670
developerfd40db22021-04-29 10:08:25 +0800671/* Mac status registers */
672#define MTK_MAC_MSR(x) (0x10108 + (x * 0x100))
673#define MAC_MSR_EEE1G BIT(7)
674#define MAC_MSR_EEE100M BIT(6)
675#define MAC_MSR_RX_FC BIT(5)
676#define MAC_MSR_TX_FC BIT(4)
677#define MAC_MSR_SPEED_1000 BIT(3)
678#define MAC_MSR_SPEED_100 BIT(2)
679#define MAC_MSR_SPEED_MASK (MAC_MSR_SPEED_1000 | MAC_MSR_SPEED_100)
680#define MAC_MSR_DPX BIT(1)
681#define MAC_MSR_LINK BIT(0)
682
683/* TRGMII RXC control register */
684#define TRGMII_RCK_CTRL 0x10300
685#define DQSI0(x) ((x << 0) & GENMASK(6, 0))
686#define DQSI1(x) ((x << 8) & GENMASK(14, 8))
687#define RXCTL_DMWTLAT(x) ((x << 16) & GENMASK(18, 16))
688#define RXC_RST BIT(31)
689#define RXC_DQSISEL BIT(30)
690#define RCK_CTRL_RGMII_1000 (RXC_DQSISEL | RXCTL_DMWTLAT(2) | DQSI1(16))
691#define RCK_CTRL_RGMII_10_100 RXCTL_DMWTLAT(2)
692
693#define NUM_TRGMII_CTRL 5
694
695/* TRGMII RXC control register */
696#define TRGMII_TCK_CTRL 0x10340
697#define TXCTL_DMWTLAT(x) ((x << 16) & GENMASK(18, 16))
698#define TXC_INV BIT(30)
699#define TCK_CTRL_RGMII_1000 TXCTL_DMWTLAT(2)
700#define TCK_CTRL_RGMII_10_100 (TXC_INV | TXCTL_DMWTLAT(2))
701
702/* TRGMII TX Drive Strength */
703#define TRGMII_TD_ODT(i) (0x10354 + 8 * (i))
704#define TD_DM_DRVP(x) ((x) & 0xf)
705#define TD_DM_DRVN(x) (((x) & 0xf) << 4)
706
707/* TRGMII Interface mode register */
708#define INTF_MODE 0x10390
709#define TRGMII_INTF_DIS BIT(0)
710#define TRGMII_MODE BIT(1)
711#define TRGMII_CENTRAL_ALIGNED BIT(2)
712#define INTF_MODE_RGMII_1000 (TRGMII_MODE | TRGMII_CENTRAL_ALIGNED)
713#define INTF_MODE_RGMII_10_100 0
714
715/* GPIO port control registers for GMAC 2*/
716#define GPIO_OD33_CTRL8 0x4c0
717#define GPIO_BIAS_CTRL 0xed0
718#define GPIO_DRV_SEL10 0xf00
719
720/* ethernet subsystem chip id register */
721#define ETHSYS_CHIPID0_3 0x0
722#define ETHSYS_CHIPID4_7 0x4
723#define MT7623_ETH 7623
724#define MT7622_ETH 7622
725#define MT7621_ETH 7621
726
727/* ethernet system control register */
728#define ETHSYS_SYSCFG 0x10
729#define SYSCFG_DRAM_TYPE_DDR2 BIT(4)
730
731/* ethernet subsystem config register */
732#define ETHSYS_SYSCFG0 0x14
733#define SYSCFG0_GE_MASK 0x3
734#define SYSCFG0_GE_MODE(x, y) (x << (12 + (y * 2)))
developer089e8852022-09-28 14:43:46 +0800735#define SYSCFG0_SGMII_MASK GENMASK(9, 7)
developerfd40db22021-04-29 10:08:25 +0800736#define SYSCFG0_SGMII_GMAC1 ((2 << 8) & SYSCFG0_SGMII_MASK)
737#define SYSCFG0_SGMII_GMAC2 ((3 << 8) & SYSCFG0_SGMII_MASK)
738#define SYSCFG0_SGMII_GMAC1_V2 BIT(9)
739#define SYSCFG0_SGMII_GMAC2_V2 BIT(8)
developer089e8852022-09-28 14:43:46 +0800740#define SYSCFG0_SGMII_GMAC3_V2 BIT(7)
developerfd40db22021-04-29 10:08:25 +0800741
742
743/* ethernet subsystem clock register */
744#define ETHSYS_CLKCFG0 0x2c
745#define ETHSYS_TRGMII_CLK_SEL362_5 BIT(11)
746#define ETHSYS_TRGMII_MT7621_MASK (BIT(5) | BIT(6))
747#define ETHSYS_TRGMII_MT7621_APLL BIT(6)
748#define ETHSYS_TRGMII_MT7621_DDR_PLL BIT(5)
749
750/* ethernet reset control register */
developer545abf02021-07-15 17:47:01 +0800751#define ETHSYS_RSTCTRL 0x34
752#define RSTCTRL_FE BIT(6)
developer545abf02021-07-15 17:47:01 +0800753#define RSTCTRL_ETH BIT(23)
developer8051e042022-04-08 13:26:36 +0800754#if defined(CONFIG_MEDIATEK_NETSYS_V2)
755#define RSTCTRL_PPE0 BIT(30)
756#define RSTCTRL_PPE1 BIT(31)
developera5eb8d62022-04-22 15:42:20 +0800757#else
developer8051e042022-04-08 13:26:36 +0800758#define RSTCTRL_PPE0 BIT(31)
developera5eb8d62022-04-22 15:42:20 +0800759#define RSTCTRL_PPE1 0
developer8051e042022-04-08 13:26:36 +0800760#endif
developer545abf02021-07-15 17:47:01 +0800761
762/* ethernet reset check idle register */
763#define ETHSYS_FE_RST_CHK_IDLE_EN 0x28
764
developerfd40db22021-04-29 10:08:25 +0800765
766/* SGMII subsystem config registers */
767/* Register to auto-negotiation restart */
768#define SGMSYS_PCS_CONTROL_1 0x0
769#define SGMII_AN_RESTART BIT(9)
770#define SGMII_ISOLATE BIT(10)
771#define SGMII_AN_ENABLE BIT(12)
772#define SGMII_LINK_STATYS BIT(18)
773#define SGMII_AN_ABILITY BIT(19)
774#define SGMII_AN_COMPLETE BIT(21)
775#define SGMII_PCS_FAULT BIT(23)
776#define SGMII_AN_EXPANSION_CLR BIT(30)
777
developer089e8852022-09-28 14:43:46 +0800778/* Register to set SGMII speed */
779#define SGMII_PCS_SPEED_ABILITY 0x08
780#define SGMII_PCS_SPEED_MASK GENMASK(11, 10)
781#define SGMII_PCS_SPEED_10 0
782#define SGMII_PCS_SPEED_100 1
783#define SGMII_PCS_SPEED_1000 2
784#define SGMII_PCS_SPEED_DUPLEX BIT(12)
785#define SGMII_PCS_SPEED_LINK BIT(15)
786
developerfd40db22021-04-29 10:08:25 +0800787/* Register to programmable link timer, the unit in 2 * 8ns */
788#define SGMSYS_PCS_LINK_TIMER 0x18
789#define SGMII_LINK_TIMER_DEFAULT (0x186a0 & GENMASK(19, 0))
790
791/* Register to control remote fault */
792#define SGMSYS_SGMII_MODE 0x20
793#define SGMII_IF_MODE_BIT0 BIT(0)
794#define SGMII_SPEED_DUPLEX_AN BIT(1)
developer089e8852022-09-28 14:43:46 +0800795#define SGMII_SPEED_MASK GENMASK(3, 2)
developerfd40db22021-04-29 10:08:25 +0800796#define SGMII_SPEED_10 0x0
797#define SGMII_SPEED_100 BIT(2)
798#define SGMII_SPEED_1000 BIT(3)
799#define SGMII_DUPLEX_FULL BIT(4)
800#define SGMII_IF_MODE_BIT5 BIT(5)
801#define SGMII_REMOTE_FAULT_DIS BIT(8)
802#define SGMII_CODE_SYNC_SET_VAL BIT(9)
803#define SGMII_CODE_SYNC_SET_EN BIT(10)
804#define SGMII_SEND_AN_ERROR_EN BIT(11)
805#define SGMII_IF_MODE_MASK GENMASK(5, 1)
806
developer2b76a9d2022-09-20 14:59:45 +0800807/* Register to reset SGMII design */
808#define SGMII_RESERVED_0 0x34
809#define SGMII_SW_RESET BIT(0)
810
developerfd40db22021-04-29 10:08:25 +0800811/* Register to set SGMII speed, ANA RG_ Control Signals III*/
812#define SGMSYS_ANA_RG_CS3 0x2028
813#define RG_PHY_SPEED_MASK (BIT(2) | BIT(3))
814#define RG_PHY_SPEED_1_25G 0x0
815#define RG_PHY_SPEED_3_125G BIT(2)
816
817/* Register to power up QPHY */
818#define SGMSYS_QPHY_PWR_STATE_CTRL 0xe8
819#define SGMII_PHYA_PWD BIT(4)
820
developerf8ac94a2021-07-29 16:40:01 +0800821/* Register to QPHY wrapper control */
822#define SGMSYS_QPHY_WRAP_CTRL 0xec
823#define SGMII_PN_SWAP_MASK GENMASK(1, 0)
824#define SGMII_PN_SWAP_TX_RX (BIT(0) | BIT(1))
825
developer089e8852022-09-28 14:43:46 +0800826/* USXGMII subsystem config registers */
827/* Register to control speed */
828#define RG_PHY_TOP_SPEED_CTRL1 0x80C
829#define RG_USXGMII_RATE_UPDATE_MODE BIT(31)
830#define RG_MAC_CK_GATED BIT(29)
831#define RG_IF_FORCE_EN BIT(28)
832#define RG_RATE_ADAPT_MODE GENMASK(10, 8)
833#define RG_RATE_ADAPT_MODE_X1 0
834#define RG_RATE_ADAPT_MODE_X2 1
835#define RG_RATE_ADAPT_MODE_X4 2
836#define RG_RATE_ADAPT_MODE_X10 3
837#define RG_RATE_ADAPT_MODE_X100 4
838#define RG_RATE_ADAPT_MODE_X5 5
839#define RG_RATE_ADAPT_MODE_X50 6
840#define RG_XFI_RX_MODE GENMASK(6, 4)
841#define RG_XFI_RX_MODE_10G 0
842#define RG_XFI_RX_MODE_5G 1
843#define RG_XFI_TX_MODE GENMASK(2, 0)
844#define RG_XFI_TX_MODE_10G 0
845#define RG_XFI_TX_MODE_5G 1
846
847/* Register to control PCS AN */
848#define RG_PCS_AN_CTRL0 0x810
849#define RG_AN_ENABLE BIT(0)
850
851/* Register to control USXGMII XFI PLL digital */
852#define XFI_PLL_DIG_GLB8 0x08
853#define RG_XFI_PLL_EN BIT(31)
854
855/* Register to control USXGMII XFI PLL analog */
856#define XFI_PLL_ANA_GLB8 0x108
857#define RG_XFI_PLL_ANA_SWWA 0x02283248
858
developerfd40db22021-04-29 10:08:25 +0800859/* Infrasys subsystem config registers */
860#define INFRA_MISC2 0x70c
861#define CO_QPHY_SEL BIT(0)
862#define GEPHY_MAC_SEL BIT(1)
863
developer255bba22021-07-27 15:16:33 +0800864/* Top misc registers */
developer089e8852022-09-28 14:43:46 +0800865#define TOP_MISC_NETSYS_PCS_MUX 0x84
866#define NETSYS_PCS_MUX_MASK GENMASK(1, 0)
867#define MUX_G2_USXGMII_SEL BIT(1)
868#define MUX_HSGMII1_G1_SEL BIT(0)
developer255bba22021-07-27 15:16:33 +0800869#define USB_PHY_SWITCH_REG 0x218
870#define QPHY_SEL_MASK GENMASK(1, 0)
developerf1816a92021-11-15 12:18:02 +0800871#define SGMII_QPHY_SEL 0x2
developer255bba22021-07-27 15:16:33 +0800872
developerfd40db22021-04-29 10:08:25 +0800873/*MDIO control*/
874#define MII_MMD_ACC_CTL_REG 0x0d
875#define MII_MMD_ADDR_DATA_REG 0x0e
876#define MMD_OP_MODE_DATA BIT(14)
877
878/* MT7628/88 specific stuff */
879#define MT7628_PDMA_OFFSET 0x0800
880#define MT7628_SDM_OFFSET 0x0c00
881
882#define MT7628_TX_BASE_PTR0 (MT7628_PDMA_OFFSET + 0x00)
883#define MT7628_TX_MAX_CNT0 (MT7628_PDMA_OFFSET + 0x04)
884#define MT7628_TX_CTX_IDX0 (MT7628_PDMA_OFFSET + 0x08)
885#define MT7628_TX_DTX_IDX0 (MT7628_PDMA_OFFSET + 0x0c)
886#define MT7628_PST_DTX_IDX0 BIT(0)
887
888#define MT7628_SDM_MAC_ADRL (MT7628_SDM_OFFSET + 0x0c)
889#define MT7628_SDM_MAC_ADRH (MT7628_SDM_OFFSET + 0x10)
890
891struct mtk_rx_dma {
892 unsigned int rxd1;
893 unsigned int rxd2;
894 unsigned int rxd3;
895 unsigned int rxd4;
developere9356982022-07-04 09:03:20 +0800896} __packed __aligned(4);
897
898struct mtk_rx_dma_v2 {
899 unsigned int rxd1;
900 unsigned int rxd2;
901 unsigned int rxd3;
902 unsigned int rxd4;
developerfd40db22021-04-29 10:08:25 +0800903 unsigned int rxd5;
904 unsigned int rxd6;
905 unsigned int rxd7;
906 unsigned int rxd8;
developerfd40db22021-04-29 10:08:25 +0800907} __packed __aligned(4);
908
909struct mtk_tx_dma {
910 unsigned int txd1;
911 unsigned int txd2;
912 unsigned int txd3;
913 unsigned int txd4;
developere9356982022-07-04 09:03:20 +0800914} __packed __aligned(4);
915
916struct mtk_tx_dma_v2 {
917 unsigned int txd1;
918 unsigned int txd2;
919 unsigned int txd3;
920 unsigned int txd4;
developerfd40db22021-04-29 10:08:25 +0800921 unsigned int txd5;
922 unsigned int txd6;
923 unsigned int txd7;
924 unsigned int txd8;
developerfd40db22021-04-29 10:08:25 +0800925} __packed __aligned(4);
926
927struct mtk_eth;
928struct mtk_mac;
929
930/* struct mtk_hw_stats - the structure that holds the traffic statistics.
931 * @stats_lock: make sure that stats operations are atomic
932 * @reg_offset: the status register offset of the SoC
933 * @syncp: the refcount
934 *
935 * All of the supported SoCs have hardware counters for traffic statistics.
936 * Whenever the status IRQ triggers we can read the latest stats from these
937 * counters and store them in this struct.
938 */
939struct mtk_hw_stats {
940 u64 tx_bytes;
941 u64 tx_packets;
942 u64 tx_skip;
943 u64 tx_collisions;
944 u64 rx_bytes;
945 u64 rx_packets;
946 u64 rx_overflow;
947 u64 rx_fcs_errors;
948 u64 rx_short_errors;
949 u64 rx_long_errors;
950 u64 rx_checksum_errors;
951 u64 rx_flow_control_packets;
952
953 spinlock_t stats_lock;
954 u32 reg_offset;
955 struct u64_stats_sync syncp;
956};
957
958enum mtk_tx_flags {
959 /* PDMA descriptor can point at 1-2 segments. This enum allows us to
960 * track how memory was allocated so that it can be freed properly.
961 */
962 MTK_TX_FLAGS_SINGLE0 = 0x01,
963 MTK_TX_FLAGS_PAGE0 = 0x02,
964
965 /* MTK_TX_FLAGS_FPORTx allows tracking which port the transmitted
966 * SKB out instead of looking up through hardware TX descriptor.
967 */
968 MTK_TX_FLAGS_FPORT0 = 0x04,
969 MTK_TX_FLAGS_FPORT1 = 0x08,
developer089e8852022-09-28 14:43:46 +0800970 MTK_TX_FLAGS_FPORT2 = 0x10,
developerfd40db22021-04-29 10:08:25 +0800971};
972
973/* This enum allows us to identify how the clock is defined on the array of the
974 * clock in the order
975 */
976enum mtk_clks_map {
977 MTK_CLK_ETHIF,
978 MTK_CLK_SGMIITOP,
979 MTK_CLK_ESW,
980 MTK_CLK_GP0,
981 MTK_CLK_GP1,
982 MTK_CLK_GP2,
developer1bbcf512022-11-18 16:09:33 +0800983 MTK_CLK_GP3,
984 MTK_CLK_XGP1,
985 MTK_CLK_XGP2,
986 MTK_CLK_XGP3,
987 MTK_CLK_CRYPTO,
developerfd40db22021-04-29 10:08:25 +0800988 MTK_CLK_FE,
989 MTK_CLK_TRGPLL,
990 MTK_CLK_SGMII_TX_250M,
991 MTK_CLK_SGMII_RX_250M,
992 MTK_CLK_SGMII_CDR_REF,
993 MTK_CLK_SGMII_CDR_FB,
994 MTK_CLK_SGMII2_TX_250M,
995 MTK_CLK_SGMII2_RX_250M,
996 MTK_CLK_SGMII2_CDR_REF,
997 MTK_CLK_SGMII2_CDR_FB,
998 MTK_CLK_SGMII_CK,
999 MTK_CLK_ETH2PLL,
1000 MTK_CLK_WOCPU0,
1001 MTK_CLK_WOCPU1,
developer1bbcf512022-11-18 16:09:33 +08001002 MTK_CLK_USXGMII0_SEL,
1003 MTK_CLK_USXGMII1_SEL,
1004 MTK_CLK_SGM0_SEL,
1005 MTK_CLK_SGM1_SEL,
developerfd40db22021-04-29 10:08:25 +08001006 MTK_CLK_MAX
1007};
1008
1009#define MT7623_CLKS_BITMAP (BIT(MTK_CLK_ETHIF) | BIT(MTK_CLK_ESW) | \
1010 BIT(MTK_CLK_GP1) | BIT(MTK_CLK_GP2) | \
1011 BIT(MTK_CLK_TRGPLL))
1012#define MT7622_CLKS_BITMAP (BIT(MTK_CLK_ETHIF) | BIT(MTK_CLK_ESW) | \
1013 BIT(MTK_CLK_GP0) | BIT(MTK_CLK_GP1) | \
1014 BIT(MTK_CLK_GP2) | \
1015 BIT(MTK_CLK_SGMII_TX_250M) | \
1016 BIT(MTK_CLK_SGMII_RX_250M) | \
1017 BIT(MTK_CLK_SGMII_CDR_REF) | \
1018 BIT(MTK_CLK_SGMII_CDR_FB) | \
1019 BIT(MTK_CLK_SGMII_CK) | \
1020 BIT(MTK_CLK_ETH2PLL))
1021#define MT7621_CLKS_BITMAP (0)
1022#define MT7628_CLKS_BITMAP (0)
1023#define MT7629_CLKS_BITMAP (BIT(MTK_CLK_ETHIF) | BIT(MTK_CLK_ESW) | \
1024 BIT(MTK_CLK_GP0) | BIT(MTK_CLK_GP1) | \
1025 BIT(MTK_CLK_GP2) | BIT(MTK_CLK_FE) | \
1026 BIT(MTK_CLK_SGMII_TX_250M) | \
1027 BIT(MTK_CLK_SGMII_RX_250M) | \
1028 BIT(MTK_CLK_SGMII_CDR_REF) | \
1029 BIT(MTK_CLK_SGMII_CDR_FB) | \
1030 BIT(MTK_CLK_SGMII2_TX_250M) | \
1031 BIT(MTK_CLK_SGMII2_RX_250M) | \
1032 BIT(MTK_CLK_SGMII2_CDR_REF) | \
1033 BIT(MTK_CLK_SGMII2_CDR_FB) | \
1034 BIT(MTK_CLK_SGMII_CK) | \
1035 BIT(MTK_CLK_ETH2PLL) | BIT(MTK_CLK_SGMIITOP))
1036
1037#define MT7986_CLKS_BITMAP (BIT(MTK_CLK_FE) | BIT(MTK_CLK_GP2) | BIT(MTK_CLK_GP1) | \
1038 BIT(MTK_CLK_WOCPU1) | BIT(MTK_CLK_WOCPU0) | \
1039 BIT(MTK_CLK_SGMII_TX_250M) | \
1040 BIT(MTK_CLK_SGMII_RX_250M) | \
1041 BIT(MTK_CLK_SGMII_CDR_REF) | \
1042 BIT(MTK_CLK_SGMII_CDR_FB) | \
1043 BIT(MTK_CLK_SGMII2_TX_250M) | \
1044 BIT(MTK_CLK_SGMII2_RX_250M) | \
1045 BIT(MTK_CLK_SGMII2_CDR_REF) | \
1046 BIT(MTK_CLK_SGMII2_CDR_FB))
1047
developer255bba22021-07-27 15:16:33 +08001048
developer9e9fb4c2021-11-30 17:33:04 +08001049#define MT7981_CLKS_BITMAP (BIT(MTK_CLK_FE) | BIT(MTK_CLK_GP2) | BIT(MTK_CLK_GP1) | \
1050 BIT(MTK_CLK_WOCPU0) | \
1051 BIT(MTK_CLK_SGMII_TX_250M) | \
1052 BIT(MTK_CLK_SGMII_RX_250M) | \
1053 BIT(MTK_CLK_SGMII_CDR_REF) | \
1054 BIT(MTK_CLK_SGMII_CDR_FB) | \
1055 BIT(MTK_CLK_SGMII2_TX_250M) | \
1056 BIT(MTK_CLK_SGMII2_RX_250M) | \
1057 BIT(MTK_CLK_SGMII2_CDR_REF) | \
1058 BIT(MTK_CLK_SGMII2_CDR_FB))
developer089e8852022-09-28 14:43:46 +08001059
developer1bbcf512022-11-18 16:09:33 +08001060#define MT7988_CLKS_BITMAP (BIT(MTK_CLK_FE) | BIT(MTK_CLK_ESW) | \
1061 BIT(MTK_CLK_GP1) | BIT(MTK_CLK_GP2) | \
1062 BIT(MTK_CLK_GP3) | BIT(MTK_CLK_XGP1) | \
1063 BIT(MTK_CLK_XGP2) | BIT(MTK_CLK_XGP3) | \
1064 BIT(MTK_CLK_CRYPTO) | \
developer089e8852022-09-28 14:43:46 +08001065 BIT(MTK_CLK_SGMII_TX_250M) | \
1066 BIT(MTK_CLK_SGMII_RX_250M) | \
developer089e8852022-09-28 14:43:46 +08001067 BIT(MTK_CLK_SGMII2_TX_250M) | \
1068 BIT(MTK_CLK_SGMII2_RX_250M) | \
developer1bbcf512022-11-18 16:09:33 +08001069 BIT(MTK_CLK_USXGMII0_SEL) | \
1070 BIT(MTK_CLK_USXGMII1_SEL) | \
1071 BIT(MTK_CLK_SGM0_SEL) | \
1072 BIT(MTK_CLK_SGM1_SEL))
developer089e8852022-09-28 14:43:46 +08001073
developerfd40db22021-04-29 10:08:25 +08001074enum mtk_dev_state {
1075 MTK_HW_INIT,
1076 MTK_RESETTING
1077};
1078
developer089e8852022-09-28 14:43:46 +08001079/* PSE Port Definition */
1080enum mtk_pse_port {
1081 PSE_ADMA_PORT = 0,
1082 PSE_GDM1_PORT,
1083 PSE_GDM2_PORT,
1084 PSE_PPE0_PORT,
1085 PSE_PPE1_PORT,
1086 PSE_QDMA_TX_PORT,
1087 PSE_QDMA_RX_PORT,
1088 PSE_DROP_PORT,
1089 PSE_WDMA0_PORT,
1090 PSE_WDMA1_PORT,
1091 PSE_TDMA_PORT,
1092 PSE_NONE_PORT,
1093 PSE_PPE2_PORT,
1094 PSE_WDMA2_PORT,
1095 PSE_EIP197_PORT,
1096 PSE_GDM3_PORT,
1097 PSE_PORT_MAX
1098};
1099
1100/* GMAC Identifier */
1101enum mtk_gmac_id {
1102 MTK_GMAC1_ID = 0,
1103 MTK_GMAC2_ID,
1104 MTK_GMAC3_ID,
1105 MTK_GMAC_ID_MAX
1106};
1107
1108/* GDM Type */
1109enum mtk_gdm_type {
1110 MTK_GDM_TYPE = 0,
1111 MTK_XGDM_TYPE,
1112 MTK_GDM_TYPE_MAX
1113};
1114
developer30e13e72022-11-03 10:21:24 +08001115static inline const char *gdm_type(int type)
1116{
1117 switch (type) {
1118 case MTK_GDM_TYPE:
1119 return "gdm";
1120 case MTK_XGDM_TYPE:
1121 return "xgdm";
1122 default:
1123 return "unkown";
1124 }
1125}
1126
developerfd40db22021-04-29 10:08:25 +08001127/* struct mtk_tx_buf - This struct holds the pointers to the memory pointed at
1128 * by the TX descriptor s
1129 * @skb: The SKB pointer of the packet being sent
1130 * @dma_addr0: The base addr of the first segment
1131 * @dma_len0: The length of the first segment
1132 * @dma_addr1: The base addr of the second segment
1133 * @dma_len1: The length of the second segment
1134 */
1135struct mtk_tx_buf {
1136 struct sk_buff *skb;
1137 u32 flags;
1138 DEFINE_DMA_UNMAP_ADDR(dma_addr0);
1139 DEFINE_DMA_UNMAP_LEN(dma_len0);
1140 DEFINE_DMA_UNMAP_ADDR(dma_addr1);
1141 DEFINE_DMA_UNMAP_LEN(dma_len1);
1142};
1143
1144/* struct mtk_tx_ring - This struct holds info describing a TX ring
1145 * @dma: The descriptor ring
1146 * @buf: The memory pointed at by the ring
1147 * @phys: The physical addr of tx_buf
1148 * @next_free: Pointer to the next free descriptor
1149 * @last_free: Pointer to the last free descriptor
developerc4671b22021-05-28 13:16:42 +08001150 * @last_free_ptr: Hardware pointer value of the last free descriptor
developerfd40db22021-04-29 10:08:25 +08001151 * @thresh: The threshold of minimum amount of free descriptors
1152 * @free_count: QDMA uses a linked list. Track how many free descriptors
1153 * are present
1154 */
1155struct mtk_tx_ring {
developere9356982022-07-04 09:03:20 +08001156 void *dma;
developerfd40db22021-04-29 10:08:25 +08001157 struct mtk_tx_buf *buf;
1158 dma_addr_t phys;
developere9356982022-07-04 09:03:20 +08001159 void *next_free;
1160 void *last_free;
developerc4671b22021-05-28 13:16:42 +08001161 u32 last_free_ptr;
developerfd40db22021-04-29 10:08:25 +08001162 u16 thresh;
1163 atomic_t free_count;
1164 int dma_size;
developere9356982022-07-04 09:03:20 +08001165 void *dma_pdma; /* For MT7628/88 PDMA handling */
developerfd40db22021-04-29 10:08:25 +08001166 dma_addr_t phys_pdma;
1167 int cpu_idx;
1168};
1169
1170/* PDMA rx ring mode */
1171enum mtk_rx_flags {
1172 MTK_RX_FLAGS_NORMAL = 0,
1173 MTK_RX_FLAGS_HWLRO,
1174 MTK_RX_FLAGS_QDMA,
1175};
1176
1177/* struct mtk_rx_ring - This struct holds info describing a RX ring
1178 * @dma: The descriptor ring
1179 * @data: The memory pointed at by the ring
1180 * @phys: The physical addr of rx_buf
1181 * @frag_size: How big can each fragment be
1182 * @buf_size: The size of each packet buffer
1183 * @calc_idx: The current head of ring
developer77d03a72021-06-06 00:06:00 +08001184 * @ring_no: The index of ring
developerfd40db22021-04-29 10:08:25 +08001185 */
1186struct mtk_rx_ring {
developere9356982022-07-04 09:03:20 +08001187 void *dma;
developerfd40db22021-04-29 10:08:25 +08001188 u8 **data;
1189 dma_addr_t phys;
1190 u16 frag_size;
1191 u16 buf_size;
1192 u16 dma_size;
1193 bool calc_idx_update;
1194 u16 calc_idx;
1195 u32 crx_idx_reg;
developer77d03a72021-06-06 00:06:00 +08001196 u32 ring_no;
developerfd40db22021-04-29 10:08:25 +08001197};
1198
developer18f46a82021-07-20 21:08:21 +08001199/* struct mtk_napi - This is the structure holding NAPI-related information,
1200 * and a mtk_napi struct is binding to one interrupt group
1201 * @napi: The NAPI struct
1202 * @rx_ring: Pointer to the memory holding info about the RX ring
1203 * @irq_grp_idx: The index indicates which interrupt group that this
1204 * mtk_napi is binding to
1205 */
1206struct mtk_napi {
1207 struct napi_struct napi;
1208 struct mtk_eth *eth;
1209 struct mtk_rx_ring *rx_ring;
1210 u32 irq_grp_no;
1211};
1212
developerfd40db22021-04-29 10:08:25 +08001213enum mkt_eth_capabilities {
1214 MTK_RGMII_BIT = 0,
1215 MTK_TRGMII_BIT,
1216 MTK_SGMII_BIT,
developer30e13e72022-11-03 10:21:24 +08001217 MTK_XGMII_BIT,
developer089e8852022-09-28 14:43:46 +08001218 MTK_USXGMII_BIT,
developerfd40db22021-04-29 10:08:25 +08001219 MTK_ESW_BIT,
1220 MTK_GEPHY_BIT,
1221 MTK_MUX_BIT,
1222 MTK_INFRA_BIT,
1223 MTK_SHARED_SGMII_BIT,
1224 MTK_HWLRO_BIT,
developer18f46a82021-07-20 21:08:21 +08001225 MTK_RSS_BIT,
developerfd40db22021-04-29 10:08:25 +08001226 MTK_SHARED_INT_BIT,
1227 MTK_TRGMII_MT7621_CLK_BIT,
1228 MTK_QDMA_BIT,
developer089e8852022-09-28 14:43:46 +08001229 MTK_NETSYS_V1_BIT,
developera2bdbd52021-05-31 19:10:17 +08001230 MTK_NETSYS_V2_BIT,
developer089e8852022-09-28 14:43:46 +08001231 MTK_NETSYS_V3_BIT,
developerfd40db22021-04-29 10:08:25 +08001232 MTK_SOC_MT7628_BIT,
developer545abf02021-07-15 17:47:01 +08001233 MTK_RSTCTRL_PPE1_BIT,
developer255bba22021-07-27 15:16:33 +08001234 MTK_U3_COPHY_V2_BIT,
developer089e8852022-09-28 14:43:46 +08001235 MTK_8GB_ADDRESSING_BIT,
developerfd40db22021-04-29 10:08:25 +08001236
1237 /* MUX BITS*/
1238 MTK_ETH_MUX_GDM1_TO_GMAC1_ESW_BIT,
1239 MTK_ETH_MUX_GMAC2_GMAC0_TO_GEPHY_BIT,
1240 MTK_ETH_MUX_U3_GMAC2_TO_QPHY_BIT,
developer30e13e72022-11-03 10:21:24 +08001241 MTK_ETH_MUX_GMAC2_TO_XGMII_BIT,
developerfd40db22021-04-29 10:08:25 +08001242 MTK_ETH_MUX_GMAC1_GMAC2_TO_SGMII_RGMII_BIT,
1243 MTK_ETH_MUX_GMAC12_TO_GEPHY_SGMII_BIT,
developer089e8852022-09-28 14:43:46 +08001244 MTK_ETH_MUX_GMAC123_TO_GEPHY_SGMII_BIT,
1245 MTK_ETH_MUX_GMAC123_TO_USXGMII_BIT,
developerfd40db22021-04-29 10:08:25 +08001246
1247 /* PATH BITS */
1248 MTK_ETH_PATH_GMAC1_RGMII_BIT,
1249 MTK_ETH_PATH_GMAC1_TRGMII_BIT,
1250 MTK_ETH_PATH_GMAC1_SGMII_BIT,
1251 MTK_ETH_PATH_GMAC2_RGMII_BIT,
1252 MTK_ETH_PATH_GMAC2_SGMII_BIT,
developer30e13e72022-11-03 10:21:24 +08001253 MTK_ETH_PATH_GMAC2_XGMII_BIT,
developerfd40db22021-04-29 10:08:25 +08001254 MTK_ETH_PATH_GMAC2_GEPHY_BIT,
developer089e8852022-09-28 14:43:46 +08001255 MTK_ETH_PATH_GMAC3_SGMII_BIT,
developerfd40db22021-04-29 10:08:25 +08001256 MTK_ETH_PATH_GDM1_ESW_BIT,
developer089e8852022-09-28 14:43:46 +08001257 MTK_ETH_PATH_GMAC1_USXGMII_BIT,
1258 MTK_ETH_PATH_GMAC2_USXGMII_BIT,
1259 MTK_ETH_PATH_GMAC3_USXGMII_BIT,
developerfd40db22021-04-29 10:08:25 +08001260};
1261
1262/* Supported hardware group on SoCs */
developer425b23a2022-10-12 16:00:41 +08001263#define MTK_RGMII BIT_ULL(MTK_RGMII_BIT)
1264#define MTK_TRGMII BIT_ULL(MTK_TRGMII_BIT)
1265#define MTK_SGMII BIT_ULL(MTK_SGMII_BIT)
developer30e13e72022-11-03 10:21:24 +08001266#define MTK_XGMII BIT_ULL(MTK_XGMII_BIT)
developer425b23a2022-10-12 16:00:41 +08001267#define MTK_USXGMII BIT_ULL(MTK_USXGMII_BIT)
1268#define MTK_ESW BIT_ULL(MTK_ESW_BIT)
1269#define MTK_GEPHY BIT_ULL(MTK_GEPHY_BIT)
1270#define MTK_MUX BIT_ULL(MTK_MUX_BIT)
1271#define MTK_INFRA BIT_ULL(MTK_INFRA_BIT)
1272#define MTK_SHARED_SGMII BIT_ULL(MTK_SHARED_SGMII_BIT)
1273#define MTK_HWLRO BIT_ULL(MTK_HWLRO_BIT)
1274#define MTK_RSS BIT_ULL(MTK_RSS_BIT)
1275#define MTK_SHARED_INT BIT_ULL(MTK_SHARED_INT_BIT)
1276#define MTK_TRGMII_MT7621_CLK BIT_ULL(MTK_TRGMII_MT7621_CLK_BIT)
1277#define MTK_QDMA BIT_ULL(MTK_QDMA_BIT)
1278#define MTK_NETSYS_V1 BIT_ULL(MTK_NETSYS_V1_BIT)
1279#define MTK_NETSYS_V2 BIT_ULL(MTK_NETSYS_V2_BIT)
1280#define MTK_NETSYS_V3 BIT_ULL(MTK_NETSYS_V3_BIT)
1281#define MTK_SOC_MT7628 BIT_ULL(MTK_SOC_MT7628_BIT)
1282#define MTK_RSTCTRL_PPE1 BIT_ULL(MTK_RSTCTRL_PPE1_BIT)
1283#define MTK_U3_COPHY_V2 BIT_ULL(MTK_U3_COPHY_V2_BIT)
1284#define MTK_8GB_ADDRESSING BIT_ULL(MTK_8GB_ADDRESSING_BIT)
developerfd40db22021-04-29 10:08:25 +08001285
1286#define MTK_ETH_MUX_GDM1_TO_GMAC1_ESW \
developer425b23a2022-10-12 16:00:41 +08001287 BIT_ULL(MTK_ETH_MUX_GDM1_TO_GMAC1_ESW_BIT)
developerfd40db22021-04-29 10:08:25 +08001288#define MTK_ETH_MUX_GMAC2_GMAC0_TO_GEPHY \
developer425b23a2022-10-12 16:00:41 +08001289 BIT_ULL(MTK_ETH_MUX_GMAC2_GMAC0_TO_GEPHY_BIT)
developerfd40db22021-04-29 10:08:25 +08001290#define MTK_ETH_MUX_U3_GMAC2_TO_QPHY \
developer425b23a2022-10-12 16:00:41 +08001291 BIT_ULL(MTK_ETH_MUX_U3_GMAC2_TO_QPHY_BIT)
developer30e13e72022-11-03 10:21:24 +08001292#define MTK_ETH_MUX_GMAC2_TO_XGMII \
1293 BIT_ULL(MTK_ETH_MUX_GMAC2_TO_XGMII_BIT)
developerfd40db22021-04-29 10:08:25 +08001294#define MTK_ETH_MUX_GMAC1_GMAC2_TO_SGMII_RGMII \
developer425b23a2022-10-12 16:00:41 +08001295 BIT_ULL(MTK_ETH_MUX_GMAC1_GMAC2_TO_SGMII_RGMII_BIT)
developerfd40db22021-04-29 10:08:25 +08001296#define MTK_ETH_MUX_GMAC12_TO_GEPHY_SGMII \
developer425b23a2022-10-12 16:00:41 +08001297 BIT_ULL(MTK_ETH_MUX_GMAC12_TO_GEPHY_SGMII_BIT)
developer089e8852022-09-28 14:43:46 +08001298#define MTK_ETH_MUX_GMAC123_TO_GEPHY_SGMII \
developer425b23a2022-10-12 16:00:41 +08001299 BIT_ULL(MTK_ETH_MUX_GMAC123_TO_GEPHY_SGMII_BIT)
developer089e8852022-09-28 14:43:46 +08001300#define MTK_ETH_MUX_GMAC123_TO_USXGMII \
developer425b23a2022-10-12 16:00:41 +08001301 BIT_ULL(MTK_ETH_MUX_GMAC123_TO_USXGMII_BIT)
developerfd40db22021-04-29 10:08:25 +08001302
1303/* Supported path present on SoCs */
developer425b23a2022-10-12 16:00:41 +08001304#define MTK_ETH_PATH_GMAC1_RGMII BIT_ULL(MTK_ETH_PATH_GMAC1_RGMII_BIT)
1305#define MTK_ETH_PATH_GMAC1_TRGMII BIT_ULL(MTK_ETH_PATH_GMAC1_TRGMII_BIT)
1306#define MTK_ETH_PATH_GMAC1_SGMII BIT_ULL(MTK_ETH_PATH_GMAC1_SGMII_BIT)
1307#define MTK_ETH_PATH_GMAC2_RGMII BIT_ULL(MTK_ETH_PATH_GMAC2_RGMII_BIT)
1308#define MTK_ETH_PATH_GMAC2_SGMII BIT_ULL(MTK_ETH_PATH_GMAC2_SGMII_BIT)
developer30e13e72022-11-03 10:21:24 +08001309#define MTK_ETH_PATH_GMAC2_XGMII BIT_ULL(MTK_ETH_PATH_GMAC2_XGMII_BIT)
developer425b23a2022-10-12 16:00:41 +08001310#define MTK_ETH_PATH_GMAC2_GEPHY BIT_ULL(MTK_ETH_PATH_GMAC2_GEPHY_BIT)
1311#define MTK_ETH_PATH_GMAC3_SGMII BIT_ULL(MTK_ETH_PATH_GMAC3_SGMII_BIT)
1312#define MTK_ETH_PATH_GDM1_ESW BIT_ULL(MTK_ETH_PATH_GDM1_ESW_BIT)
1313#define MTK_ETH_PATH_GMAC1_USXGMII BIT_ULL(MTK_ETH_PATH_GMAC1_USXGMII_BIT)
1314#define MTK_ETH_PATH_GMAC2_USXGMII BIT_ULL(MTK_ETH_PATH_GMAC2_USXGMII_BIT)
1315#define MTK_ETH_PATH_GMAC3_USXGMII BIT_ULL(MTK_ETH_PATH_GMAC3_USXGMII_BIT)
developerfd40db22021-04-29 10:08:25 +08001316
1317#define MTK_GMAC1_RGMII (MTK_ETH_PATH_GMAC1_RGMII | MTK_RGMII)
1318#define MTK_GMAC1_TRGMII (MTK_ETH_PATH_GMAC1_TRGMII | MTK_TRGMII)
1319#define MTK_GMAC1_SGMII (MTK_ETH_PATH_GMAC1_SGMII | MTK_SGMII)
1320#define MTK_GMAC2_RGMII (MTK_ETH_PATH_GMAC2_RGMII | MTK_RGMII)
1321#define MTK_GMAC2_SGMII (MTK_ETH_PATH_GMAC2_SGMII | MTK_SGMII)
developer30e13e72022-11-03 10:21:24 +08001322#define MTK_GMAC2_XGMII (MTK_ETH_PATH_GMAC2_XGMII | MTK_XGMII)
developerfd40db22021-04-29 10:08:25 +08001323#define MTK_GMAC2_GEPHY (MTK_ETH_PATH_GMAC2_GEPHY | MTK_GEPHY)
developer089e8852022-09-28 14:43:46 +08001324#define MTK_GMAC3_SGMII (MTK_ETH_PATH_GMAC3_SGMII | MTK_SGMII)
developerfd40db22021-04-29 10:08:25 +08001325#define MTK_GDM1_ESW (MTK_ETH_PATH_GDM1_ESW | MTK_ESW)
developer089e8852022-09-28 14:43:46 +08001326#define MTK_GMAC1_USXGMII (MTK_ETH_PATH_GMAC1_USXGMII | MTK_USXGMII)
1327#define MTK_GMAC2_USXGMII (MTK_ETH_PATH_GMAC2_USXGMII | MTK_USXGMII)
1328#define MTK_GMAC3_USXGMII (MTK_ETH_PATH_GMAC3_USXGMII | MTK_USXGMII)
developerfd40db22021-04-29 10:08:25 +08001329
1330/* MUXes present on SoCs */
1331/* 0: GDM1 -> GMAC1, 1: GDM1 -> ESW */
1332#define MTK_MUX_GDM1_TO_GMAC1_ESW (MTK_ETH_MUX_GDM1_TO_GMAC1_ESW | MTK_MUX)
1333
1334/* 0: GMAC2 -> GEPHY, 1: GMAC0 -> GePHY */
1335#define MTK_MUX_GMAC2_GMAC0_TO_GEPHY \
1336 (MTK_ETH_MUX_GMAC2_GMAC0_TO_GEPHY | MTK_MUX | MTK_INFRA)
1337
1338/* 0: U3 -> QPHY, 1: GMAC2 -> QPHY */
1339#define MTK_MUX_U3_GMAC2_TO_QPHY \
1340 (MTK_ETH_MUX_U3_GMAC2_TO_QPHY | MTK_MUX | MTK_INFRA)
1341
1342/* 2: GMAC1 -> SGMII, 3: GMAC2 -> SGMII */
1343#define MTK_MUX_GMAC1_GMAC2_TO_SGMII_RGMII \
1344 (MTK_ETH_MUX_GMAC1_GMAC2_TO_SGMII_RGMII | MTK_MUX | \
1345 MTK_SHARED_SGMII)
1346
developer30e13e72022-11-03 10:21:24 +08001347/* 2: GMAC2 -> XGMII */
1348#define MTK_MUX_GMAC2_TO_XGMII \
1349 (MTK_ETH_MUX_GMAC2_TO_XGMII | MTK_MUX | MTK_INFRA)
1350
developerfd40db22021-04-29 10:08:25 +08001351/* 0: GMACx -> GEPHY, 1: GMACx -> SGMII where x is 1 or 2 */
1352#define MTK_MUX_GMAC12_TO_GEPHY_SGMII \
1353 (MTK_ETH_MUX_GMAC12_TO_GEPHY_SGMII | MTK_MUX)
1354
developer089e8852022-09-28 14:43:46 +08001355#define MTK_MUX_GMAC123_TO_GEPHY_SGMII \
1356 (MTK_ETH_MUX_GMAC123_TO_GEPHY_SGMII | MTK_MUX)
1357
1358#define MTK_MUX_GMAC123_TO_USXGMII \
1359 (MTK_ETH_MUX_GMAC123_TO_USXGMII | MTK_MUX | MTK_INFRA)
1360
developerfd40db22021-04-29 10:08:25 +08001361#define MTK_HAS_CAPS(caps, _x) (((caps) & (_x)) == (_x))
1362
1363#define MT7621_CAPS (MTK_GMAC1_RGMII | MTK_GMAC1_TRGMII | \
1364 MTK_GMAC2_RGMII | MTK_SHARED_INT | \
developer089e8852022-09-28 14:43:46 +08001365 MTK_TRGMII_MT7621_CLK | MTK_QDMA | MTK_NETSYS_V1)
developerfd40db22021-04-29 10:08:25 +08001366
1367#define MT7622_CAPS (MTK_GMAC1_RGMII | MTK_GMAC1_SGMII | MTK_GMAC2_RGMII | \
1368 MTK_GMAC2_SGMII | MTK_GDM1_ESW | \
developer089e8852022-09-28 14:43:46 +08001369 MTK_MUX_GDM1_TO_GMAC1_ESW | MTK_NETSYS_V1 | \
developerfd40db22021-04-29 10:08:25 +08001370 MTK_MUX_GMAC1_GMAC2_TO_SGMII_RGMII | MTK_QDMA)
1371
1372#define MT7623_CAPS (MTK_GMAC1_RGMII | MTK_GMAC1_TRGMII | MTK_GMAC2_RGMII | \
developer089e8852022-09-28 14:43:46 +08001373 MTK_QDMA | MTK_NETSYS_V1)
developerfd40db22021-04-29 10:08:25 +08001374
developer089e8852022-09-28 14:43:46 +08001375#define MT7628_CAPS (MTK_SHARED_INT | MTK_SOC_MT7628 | MTK_NETSYS_V1)
developerfd40db22021-04-29 10:08:25 +08001376
1377#define MT7629_CAPS (MTK_GMAC1_SGMII | MTK_GMAC2_SGMII | MTK_GMAC2_GEPHY | \
1378 MTK_GDM1_ESW | MTK_MUX_GDM1_TO_GMAC1_ESW | \
1379 MTK_MUX_GMAC2_GMAC0_TO_GEPHY | \
developer089e8852022-09-28 14:43:46 +08001380 MTK_MUX_U3_GMAC2_TO_QPHY | MTK_NETSYS_V1 | \
developerfd40db22021-04-29 10:08:25 +08001381 MTK_MUX_GMAC12_TO_GEPHY_SGMII | MTK_QDMA)
1382
developerfd40db22021-04-29 10:08:25 +08001383#define MT7986_CAPS (MTK_GMAC1_SGMII | MTK_GMAC2_SGMII | \
1384 MTK_MUX_GMAC12_TO_GEPHY_SGMII | MTK_QDMA | \
developer8051e042022-04-08 13:26:36 +08001385 MTK_NETSYS_V2)
developerfd40db22021-04-29 10:08:25 +08001386
developer255bba22021-07-27 15:16:33 +08001387#define MT7981_CAPS (MTK_GMAC1_SGMII | MTK_GMAC2_SGMII | MTK_GMAC2_GEPHY | \
1388 MTK_MUX_GMAC12_TO_GEPHY_SGMII | MTK_QDMA | \
1389 MTK_MUX_U3_GMAC2_TO_QPHY | MTK_U3_COPHY_V2 | \
1390 MTK_NETSYS_V2)
1391
developer089e8852022-09-28 14:43:46 +08001392#define MT7988_CAPS (MTK_GMAC1_SGMII | MTK_GMAC2_SGMII | MTK_GMAC3_SGMII | \
1393 MTK_MUX_GMAC123_TO_GEPHY_SGMII | MTK_QDMA | \
1394 MTK_NETSYS_V3 | MTK_RSTCTRL_PPE1 | \
1395 MTK_GMAC1_USXGMII | MTK_GMAC2_USXGMII | \
developer30e13e72022-11-03 10:21:24 +08001396 MTK_GMAC3_USXGMII | MTK_MUX_GMAC123_TO_USXGMII | \
1397 MTK_GMAC2_XGMII | MTK_MUX_GMAC2_TO_XGMII | MTK_RSS)
developer089e8852022-09-28 14:43:46 +08001398
developere9356982022-07-04 09:03:20 +08001399struct mtk_tx_dma_desc_info {
1400 dma_addr_t addr;
1401 u32 size;
1402 u16 vlan_tci;
1403 u16 qid;
1404 u8 gso:1;
1405 u8 csum:1;
1406 u8 vlan:1;
1407 u8 first:1;
1408 u8 last:1;
1409};
1410
developerfd40db22021-04-29 10:08:25 +08001411/* struct mtk_eth_data - This is the structure holding all differences
1412 * among various plaforms
1413 * @ana_rgc3: The offset for register ANA_RGC3 related to
1414 * sgmiisys syscon
1415 * @caps Flags shown the extra capability for the SoC
1416 * @hw_features Flags shown HW features
1417 * @required_clks Flags shown the bitmap for required clocks on
1418 * the target SoC
1419 * @required_pctl A bool value to show whether the SoC requires
1420 * the extra setup for those pins used by GMAC.
developere9356982022-07-04 09:03:20 +08001421 * @txd_size Tx DMA descriptor size.
1422 * @rxd_size Rx DMA descriptor size.
1423 * @dma_max_len Max DMA tx/rx buffer length.
1424 * @dma_len_offset Tx/Rx DMA length field offset.
developerfd40db22021-04-29 10:08:25 +08001425 */
1426struct mtk_soc_data {
1427 u32 ana_rgc3;
developer089e8852022-09-28 14:43:46 +08001428 u64 caps;
developerfd40db22021-04-29 10:08:25 +08001429 u32 required_clks;
1430 bool required_pctl;
1431 netdev_features_t hw_features;
1432 bool has_sram;
developere9356982022-07-04 09:03:20 +08001433 struct {
1434 u32 txd_size;
1435 u32 rxd_size;
1436 u32 dma_max_len;
1437 u32 dma_len_offset;
1438 } txrx;
developerfd40db22021-04-29 10:08:25 +08001439};
1440
developer089e8852022-09-28 14:43:46 +08001441/* currently no SoC has more than 3 macs */
1442#if defined(CONFIG_MEDIATEK_NETSYS_V3)
1443#define MTK_MAX_DEVS 3
1444#else
1445#define MTK_MAX_DEVS 2
1446#endif
developerfd40db22021-04-29 10:08:25 +08001447
1448#define MTK_SGMII_PHYSPEED_AN BIT(31)
1449#define MTK_SGMII_PHYSPEED_MASK GENMASK(2, 0)
1450#define MTK_SGMII_PHYSPEED_1000 BIT(0)
1451#define MTK_SGMII_PHYSPEED_2500 BIT(1)
developer089e8852022-09-28 14:43:46 +08001452#define MTK_SGMII_PHYSPEED_5000 BIT(2)
1453#define MTK_SGMII_PHYSPEED_10000 BIT(3)
developerf8ac94a2021-07-29 16:40:01 +08001454#define MTK_SGMII_PN_SWAP BIT(16)
developerfd40db22021-04-29 10:08:25 +08001455#define MTK_HAS_FLAGS(flags, _x) (((flags) & (_x)) == (_x))
1456
developer089e8852022-09-28 14:43:46 +08001457/* struct mtk_xgmii - This is the structure holding sgmii/usxgmii regmap and
1458 * its characteristics
developerfd40db22021-04-29 10:08:25 +08001459 * @regmap: The register map pointing at the range used to setup
developer089e8852022-09-28 14:43:46 +08001460 * SGMII/USXGMII modes
developerfd40db22021-04-29 10:08:25 +08001461 * @flags: The enum refers to which mode the sgmii wants to run on
1462 * @ana_rgc3: The offset refers to register ANA_RGC3 related to regmap
1463 */
1464
developer089e8852022-09-28 14:43:46 +08001465struct mtk_xgmii {
1466 struct mtk_eth *eth;
1467 struct regmap *regmap_sgmii[MTK_MAX_DEVS];
1468 struct regmap *regmap_usxgmii[MTK_MAX_DEVS];
1469 struct regmap *regmap_pextp[MTK_MAX_DEVS];
1470 struct regmap *regmap_pll;
developerfd40db22021-04-29 10:08:25 +08001471 u32 flags[MTK_MAX_DEVS];
1472 u32 ana_rgc3;
1473};
1474
developer8051e042022-04-08 13:26:36 +08001475
1476/* struct mtk_reset_event - This is the structure holding statistics counters
1477 * for reset events
1478 * @count: The counter is used to record the number of events
1479 */
1480struct mtk_reset_event {
1481 u32 count[32];
1482};
1483
developera2613e62022-07-01 18:29:37 +08001484/* struct mtk_phylink_priv - This is the structure holding private data for phylink
1485 * @desc: Pointer to the memory holding info about the phylink gpio
1486 * @id: The element is used to record the phy index of phylink
1487 * @phyaddr: The element is used to record the phy address of phylink
1488 * @link: The element is used to record the phy link status of phylink
1489 */
1490struct mtk_phylink_priv {
1491 struct net_device *dev;
1492 struct gpio_desc *desc;
1493 char label[16];
1494 int id;
1495 int phyaddr;
1496 int link;
1497};
1498
developerfd40db22021-04-29 10:08:25 +08001499/* struct mtk_eth - This is the main datasructure for holding the state
1500 * of the driver
1501 * @dev: The device pointer
1502 * @base: The mapped register i/o base
1503 * @page_lock: Make sure that register operations are atomic
1504 * @tx_irq__lock: Make sure that IRQ register operations are atomic
1505 * @rx_irq__lock: Make sure that IRQ register operations are atomic
1506 * @dummy_dev: we run 2 netdevs on 1 physical DMA ring and need a
1507 * dummy for NAPI to work
1508 * @netdev: The netdev instances
1509 * @mac: Each netdev is linked to a physical MAC
1510 * @irq: The IRQ that we are using
1511 * @msg_enable: Ethtool msg level
1512 * @ethsys: The register map pointing at the range used to setup
1513 * MII modes
1514 * @infra: The register map pointing at the range used to setup
1515 * SGMII and GePHY path
1516 * @pctl: The register map pointing at the range used to setup
1517 * GMAC port drive/slew values
1518 * @dma_refcnt: track how many netdevs are using the DMA engine
1519 * @tx_ring: Pointer to the memory holding info about the TX ring
1520 * @rx_ring: Pointer to the memory holding info about the RX ring
1521 * @rx_ring_qdma: Pointer to the memory holding info about the QDMA RX ring
1522 * @tx_napi: The TX NAPI struct
1523 * @rx_napi: The RX NAPI struct
1524 * @scratch_ring: Newer SoCs need memory for a second HW managed TX ring
1525 * @phy_scratch_ring: physical address of scratch_ring
1526 * @scratch_head: The scratch memory that scratch_ring points to.
1527 * @clks: clock array for all clocks required
1528 * @mii_bus: If there is a bus we need to create an instance for it
1529 * @pending_work: The workqueue used to reset the dma ring
1530 * @state: Initialization and runtime state of the device
1531 * @soc: Holding specific data among vaious SoCs
1532 */
1533
1534struct mtk_eth {
1535 struct device *dev;
1536 void __iomem *base;
developer089e8852022-09-28 14:43:46 +08001537 void __iomem *sram_base;
developerfd40db22021-04-29 10:08:25 +08001538 spinlock_t page_lock;
1539 spinlock_t tx_irq_lock;
1540 spinlock_t rx_irq_lock;
1541 struct net_device dummy_dev;
1542 struct net_device *netdev[MTK_MAX_DEVS];
1543 struct mtk_mac *mac[MTK_MAX_DEVS];
developer18f46a82021-07-20 21:08:21 +08001544 int irq[MTK_MAX_IRQ_NUM];
developerfd40db22021-04-29 10:08:25 +08001545 u32 msg_enable;
1546 unsigned long sysclk;
1547 struct regmap *ethsys;
1548 struct regmap *infra;
developer089e8852022-09-28 14:43:46 +08001549 struct regmap *toprgu;
1550 struct mtk_xgmii *xgmii;
developerfd40db22021-04-29 10:08:25 +08001551 struct regmap *pctl;
1552 bool hwlro;
1553 refcount_t dma_refcnt;
1554 struct mtk_tx_ring tx_ring;
1555 struct mtk_rx_ring rx_ring[MTK_MAX_RX_RING_NUM];
1556 struct mtk_rx_ring rx_ring_qdma;
1557 struct napi_struct tx_napi;
developer18f46a82021-07-20 21:08:21 +08001558 struct mtk_napi rx_napi[MTK_RX_NAPI_NUM];
developere9356982022-07-04 09:03:20 +08001559 void *scratch_ring;
developer8051e042022-04-08 13:26:36 +08001560 struct mtk_reset_event reset_event;
developerfd40db22021-04-29 10:08:25 +08001561 dma_addr_t phy_scratch_ring;
1562 void *scratch_head;
1563 struct clk *clks[MTK_CLK_MAX];
1564
1565 struct mii_bus *mii_bus;
1566 struct work_struct pending_work;
1567 unsigned long state;
1568
1569 const struct mtk_soc_data *soc;
1570
1571 u32 tx_int_mask_reg;
1572 u32 tx_int_status_reg;
1573 u32 rx_dma_l4_valid;
1574 int ip_align;
developerd82e8372022-02-09 15:00:09 +08001575 spinlock_t syscfg0_lock;
developer8051e042022-04-08 13:26:36 +08001576 struct timer_list mtk_dma_monitor_timer;
developerfd40db22021-04-29 10:08:25 +08001577};
1578
1579/* struct mtk_mac - the structure that holds the info about the MACs of the
1580 * SoC
1581 * @id: The number of the MAC
1582 * @interface: Interface mode kept for detecting change in hw settings
1583 * @of_node: Our devicetree node
1584 * @hw: Backpointer to our main datastruture
1585 * @hw_stats: Packet statistics counter
1586 */
1587struct mtk_mac {
developerfb556ca2021-10-13 10:52:09 +08001588 unsigned int id;
developerfd40db22021-04-29 10:08:25 +08001589 phy_interface_t interface;
1590 unsigned int mode;
developer089e8852022-09-28 14:43:46 +08001591 unsigned int type;
developerfd40db22021-04-29 10:08:25 +08001592 int speed;
1593 struct device_node *of_node;
1594 struct phylink *phylink;
1595 struct phylink_config phylink_config;
developera2613e62022-07-01 18:29:37 +08001596 struct mtk_phylink_priv phylink_priv;
developerfd40db22021-04-29 10:08:25 +08001597 struct mtk_eth *hw;
1598 struct mtk_hw_stats *hw_stats;
1599 __be32 hwlro_ip[MTK_MAX_LRO_IP_CNT];
1600 int hwlro_ip_cnt;
1601};
1602
1603/* the struct describing the SoC. these are declared in the soc_xyz.c files */
1604extern const struct of_device_id of_mtk_match[];
developer77d03a72021-06-06 00:06:00 +08001605extern u32 mtk_hwlro_stats_ebl;
developerfd40db22021-04-29 10:08:25 +08001606
1607/* read the hardware status register */
1608void mtk_stats_update_mac(struct mtk_mac *mac);
1609
1610void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg);
1611u32 mtk_r32(struct mtk_eth *eth, unsigned reg);
developer8051e042022-04-08 13:26:36 +08001612u32 mtk_m32(struct mtk_eth *eth, u32 mask, u32 set, unsigned reg);
developerfd40db22021-04-29 10:08:25 +08001613
developer089e8852022-09-28 14:43:46 +08001614int mtk_sgmii_init(struct mtk_xgmii *ss, struct device_node *np,
developerfd40db22021-04-29 10:08:25 +08001615 u32 ana_rgc3);
developer089e8852022-09-28 14:43:46 +08001616int mtk_sgmii_setup_mode_an(struct mtk_xgmii *ss, unsigned int mac_id);
1617int mtk_sgmii_setup_mode_force(struct mtk_xgmii *ss, unsigned int mac_id,
developerfd40db22021-04-29 10:08:25 +08001618 const struct phylink_link_state *state);
1619void mtk_sgmii_restart_an(struct mtk_eth *eth, int mac_id);
developer089e8852022-09-28 14:43:46 +08001620void mtk_sgmii_setup_phya_gen2(struct mtk_xgmii *ss, int mac_id);
developerfd40db22021-04-29 10:08:25 +08001621
1622int mtk_gmac_sgmii_path_setup(struct mtk_eth *eth, int mac_id);
developer30e13e72022-11-03 10:21:24 +08001623int mtk_gmac_xgmii_path_setup(struct mtk_eth *eth, int mac_id);
developerfd40db22021-04-29 10:08:25 +08001624int mtk_gmac_gephy_path_setup(struct mtk_eth *eth, int mac_id);
1625int mtk_gmac_rgmii_path_setup(struct mtk_eth *eth, int mac_id);
developer089e8852022-09-28 14:43:46 +08001626int mtk_gmac_usxgmii_path_setup(struct mtk_eth *eth, int mac_id);
developer8051e042022-04-08 13:26:36 +08001627void mtk_gdm_config(struct mtk_eth *eth, u32 config);
1628void ethsys_reset(struct mtk_eth *eth, u32 reset_bits);
developerfd40db22021-04-29 10:08:25 +08001629
developer089e8852022-09-28 14:43:46 +08001630int mtk_mac2xgmii_id(struct mtk_eth *eth, int mac_id);
1631int mtk_usxgmii_init(struct mtk_xgmii *ss, struct device_node *r);
1632int mtk_xfi_pextp_init(struct mtk_xgmii *ss, struct device_node *r);
1633int mtk_xfi_pll_init(struct mtk_xgmii *ss, struct device_node *r);
1634int mtk_toprgu_init(struct mtk_eth *eth, struct device_node *r);
1635int mtk_xfi_pll_enable(struct mtk_xgmii *ss);
1636int mtk_usxgmii_setup_mode_an(struct mtk_xgmii *ss, int mac_id,
1637 int max_speed);
1638int mtk_usxgmii_setup_mode_force(struct mtk_xgmii *ss, int mac_id,
1639 int max_speed);
1640void mtk_usxgmii_setup_phya_an_10000(struct mtk_xgmii *ss, int mac_id);
1641void mtk_usxgmii_reset(struct mtk_xgmii *ss, int mac_id);
developerfd40db22021-04-29 10:08:25 +08001642#endif /* MTK_ETH_H */