xref: /openbmc/linux/drivers/net/dsa/mt7530.h (revision f7af616c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
4  */
5 
6 #ifndef __MT7530_H
7 #define __MT7530_H
8 
9 #define MT7530_NUM_PORTS		7
10 #define MT7530_NUM_PHYS			5
11 #define MT7530_CPU_PORT			6
12 #define MT7530_NUM_FDB_RECORDS		2048
13 #define MT7530_ALL_MEMBERS		0xff
14 
15 #define MTK_HDR_LEN	4
16 #define MT7530_MAX_MTU	(15 * 1024 - ETH_HLEN - ETH_FCS_LEN - MTK_HDR_LEN)
17 
18 enum mt753x_id {
19 	ID_MT7530 = 0,
20 	ID_MT7621 = 1,
21 	ID_MT7531 = 2,
22 };
23 
24 #define	NUM_TRGMII_CTRL			5
25 
26 #define TRGMII_BASE(x)			(0x10000 + (x))
27 
28 /* Registers to ethsys access */
29 #define ETHSYS_CLKCFG0			0x2c
30 #define  ETHSYS_TRGMII_CLK_SEL362_5	BIT(11)
31 
32 #define SYSC_REG_RSTCTRL		0x34
33 #define  RESET_MCM			BIT(2)
34 
35 /* Registers to mac forward control for unknown frames */
36 #define MT7530_MFC			0x10
37 #define  BC_FFP(x)			(((x) & 0xff) << 24)
38 #define  BC_FFP_MASK			BC_FFP(~0)
39 #define  UNM_FFP(x)			(((x) & 0xff) << 16)
40 #define  UNM_FFP_MASK			UNM_FFP(~0)
41 #define  UNU_FFP(x)			(((x) & 0xff) << 8)
42 #define  UNU_FFP_MASK			UNU_FFP(~0)
43 #define  CPU_EN				BIT(7)
44 #define  CPU_PORT(x)			((x) << 4)
45 #define  CPU_MASK			(0xf << 4)
46 #define  MIRROR_EN			BIT(3)
47 #define  MIRROR_PORT(x)			((x) & 0x7)
48 #define  MIRROR_MASK			0x7
49 
50 /* Registers for CPU forward control */
51 #define MT7531_CFC			0x4
52 #define  MT7531_MIRROR_EN		BIT(19)
53 #define  MT7531_MIRROR_MASK		(MIRROR_MASK << 16)
54 #define  MT7531_MIRROR_PORT_GET(x)	(((x) >> 16) & MIRROR_MASK)
55 #define  MT7531_MIRROR_PORT_SET(x)	(((x) & MIRROR_MASK) << 16)
56 #define  MT7531_CPU_PMAP_MASK		GENMASK(7, 0)
57 
58 #define MT753X_MIRROR_REG(id)		(((id) == ID_MT7531) ? \
59 					 MT7531_CFC : MT7530_MFC)
60 #define MT753X_MIRROR_EN(id)		(((id) == ID_MT7531) ? \
61 					 MT7531_MIRROR_EN : MIRROR_EN)
62 #define MT753X_MIRROR_MASK(id)		(((id) == ID_MT7531) ? \
63 					 MT7531_MIRROR_MASK : MIRROR_MASK)
64 
65 /* Registers for BPDU and PAE frame control*/
66 #define MT753X_BPC			0x24
67 #define  MT753X_BPDU_PORT_FW_MASK	GENMASK(2, 0)
68 
69 enum mt753x_bpdu_port_fw {
70 	MT753X_BPDU_FOLLOW_MFC,
71 	MT753X_BPDU_CPU_EXCLUDE = 4,
72 	MT753X_BPDU_CPU_INCLUDE = 5,
73 	MT753X_BPDU_CPU_ONLY = 6,
74 	MT753X_BPDU_DROP = 7,
75 };
76 
77 /* Registers for address table access */
78 #define MT7530_ATA1			0x74
79 #define  STATIC_EMP			0
80 #define  STATIC_ENT			3
81 #define MT7530_ATA2			0x78
82 
83 /* Register for address table write data */
84 #define MT7530_ATWD			0x7c
85 
86 /* Register for address table control */
87 #define MT7530_ATC			0x80
88 #define  ATC_HASH			(((x) & 0xfff) << 16)
89 #define  ATC_BUSY			BIT(15)
90 #define  ATC_SRCH_END			BIT(14)
91 #define  ATC_SRCH_HIT			BIT(13)
92 #define  ATC_INVALID			BIT(12)
93 #define  ATC_MAT(x)			(((x) & 0xf) << 8)
94 #define  ATC_MAT_MACTAB			ATC_MAT(0)
95 
96 enum mt7530_fdb_cmd {
97 	MT7530_FDB_READ	= 0,
98 	MT7530_FDB_WRITE = 1,
99 	MT7530_FDB_FLUSH = 2,
100 	MT7530_FDB_START = 4,
101 	MT7530_FDB_NEXT = 5,
102 };
103 
104 /* Registers for table search read address */
105 #define MT7530_TSRA1			0x84
106 #define  MAC_BYTE_0			24
107 #define  MAC_BYTE_1			16
108 #define  MAC_BYTE_2			8
109 #define  MAC_BYTE_3			0
110 #define  MAC_BYTE_MASK			0xff
111 
112 #define MT7530_TSRA2			0x88
113 #define  MAC_BYTE_4			24
114 #define  MAC_BYTE_5			16
115 #define  CVID				0
116 #define  CVID_MASK			0xfff
117 
118 #define MT7530_ATRD			0x8C
119 #define	 AGE_TIMER			24
120 #define  AGE_TIMER_MASK			0xff
121 #define  PORT_MAP			4
122 #define  PORT_MAP_MASK			0xff
123 #define  ENT_STATUS			2
124 #define  ENT_STATUS_MASK		0x3
125 
126 /* Register for vlan table control */
127 #define MT7530_VTCR			0x90
128 #define  VTCR_BUSY			BIT(31)
129 #define  VTCR_INVALID			BIT(16)
130 #define  VTCR_FUNC(x)			(((x) & 0xf) << 12)
131 #define  VTCR_VID			((x) & 0xfff)
132 
133 enum mt7530_vlan_cmd {
134 	/* Read/Write the specified VID entry from VAWD register based
135 	 * on VID.
136 	 */
137 	MT7530_VTCR_RD_VID = 0,
138 	MT7530_VTCR_WR_VID = 1,
139 };
140 
141 /* Register for setup vlan and acl write data */
142 #define MT7530_VAWD1			0x94
143 #define  PORT_STAG			BIT(31)
144 /* Independent VLAN Learning */
145 #define  IVL_MAC			BIT(30)
146 /* Per VLAN Egress Tag Control */
147 #define  VTAG_EN			BIT(28)
148 /* VLAN Member Control */
149 #define  PORT_MEM(x)			(((x) & 0xff) << 16)
150 /* VLAN Entry Valid */
151 #define  VLAN_VALID			BIT(0)
152 #define  PORT_MEM_SHFT			16
153 #define  PORT_MEM_MASK			0xff
154 
155 #define MT7530_VAWD2			0x98
156 /* Egress Tag Control */
157 #define  ETAG_CTRL_P(p, x)		(((x) & 0x3) << ((p) << 1))
158 #define  ETAG_CTRL_P_MASK(p)		ETAG_CTRL_P(p, 3)
159 
160 enum mt7530_vlan_egress_attr {
161 	MT7530_VLAN_EGRESS_UNTAG = 0,
162 	MT7530_VLAN_EGRESS_TAG = 2,
163 	MT7530_VLAN_EGRESS_STACK = 3,
164 };
165 
166 /* Register for address age control */
167 #define MT7530_AAC			0xa0
168 /* Disable ageing */
169 #define  AGE_DIS			BIT(20)
170 /* Age count */
171 #define  AGE_CNT_MASK			GENMASK(19, 12)
172 #define  AGE_CNT_MAX			0xff
173 #define  AGE_CNT(x)			(AGE_CNT_MASK & ((x) << 12))
174 /* Age unit */
175 #define  AGE_UNIT_MASK			GENMASK(11, 0)
176 #define  AGE_UNIT_MAX			0xfff
177 #define  AGE_UNIT(x)			(AGE_UNIT_MASK & (x))
178 
179 /* Register for port STP state control */
180 #define MT7530_SSP_P(x)			(0x2000 + ((x) * 0x100))
181 #define  FID_PST(x)			((x) & 0x3)
182 #define  FID_PST_MASK			FID_PST(0x3)
183 
184 enum mt7530_stp_state {
185 	MT7530_STP_DISABLED = 0,
186 	MT7530_STP_BLOCKING = 1,
187 	MT7530_STP_LISTENING = 1,
188 	MT7530_STP_LEARNING = 2,
189 	MT7530_STP_FORWARDING  = 3
190 };
191 
192 /* Register for port control */
193 #define MT7530_PCR_P(x)			(0x2004 + ((x) * 0x100))
194 #define  PORT_TX_MIR			BIT(9)
195 #define  PORT_RX_MIR			BIT(8)
196 #define  PORT_VLAN(x)			((x) & 0x3)
197 
198 enum mt7530_port_mode {
199 	/* Port Matrix Mode: Frames are forwarded by the PCR_MATRIX members. */
200 	MT7530_PORT_MATRIX_MODE = PORT_VLAN(0),
201 
202 	/* Fallback Mode: Forward received frames with ingress ports that do
203 	 * not belong to the VLAN member. Frames whose VID is not listed on
204 	 * the VLAN table are forwarded by the PCR_MATRIX members.
205 	 */
206 	MT7530_PORT_FALLBACK_MODE = PORT_VLAN(1),
207 
208 	/* Security Mode: Discard any frame due to ingress membership
209 	 * violation or VID missed on the VLAN table.
210 	 */
211 	MT7530_PORT_SECURITY_MODE = PORT_VLAN(3),
212 };
213 
214 #define  PCR_MATRIX(x)			(((x) & 0xff) << 16)
215 #define  PORT_PRI(x)			(((x) & 0x7) << 24)
216 #define  EG_TAG(x)			(((x) & 0x3) << 28)
217 #define  PCR_MATRIX_MASK		PCR_MATRIX(0xff)
218 #define  PCR_MATRIX_CLR			PCR_MATRIX(0)
219 #define  PCR_PORT_VLAN_MASK		PORT_VLAN(3)
220 
221 /* Register for port security control */
222 #define MT7530_PSC_P(x)			(0x200c + ((x) * 0x100))
223 #define  SA_DIS				BIT(4)
224 
225 /* Register for port vlan control */
226 #define MT7530_PVC_P(x)			(0x2010 + ((x) * 0x100))
227 #define  PORT_SPEC_TAG			BIT(5)
228 #define  PVC_EG_TAG(x)			(((x) & 0x7) << 8)
229 #define  PVC_EG_TAG_MASK		PVC_EG_TAG(7)
230 #define  VLAN_ATTR(x)			(((x) & 0x3) << 6)
231 #define  VLAN_ATTR_MASK			VLAN_ATTR(3)
232 
233 enum mt7530_vlan_port_eg_tag {
234 	MT7530_VLAN_EG_DISABLED = 0,
235 	MT7530_VLAN_EG_CONSISTENT = 1,
236 };
237 
238 enum mt7530_vlan_port_attr {
239 	MT7530_VLAN_USER = 0,
240 	MT7530_VLAN_TRANSPARENT = 3,
241 };
242 
243 #define  STAG_VPID			(((x) & 0xffff) << 16)
244 
245 /* Register for port port-and-protocol based vlan 1 control */
246 #define MT7530_PPBV1_P(x)		(0x2014 + ((x) * 0x100))
247 #define  G0_PORT_VID(x)			(((x) & 0xfff) << 0)
248 #define  G0_PORT_VID_MASK		G0_PORT_VID(0xfff)
249 #define  G0_PORT_VID_DEF		G0_PORT_VID(1)
250 
251 /* Register for port MAC control register */
252 #define MT7530_PMCR_P(x)		(0x3000 + ((x) * 0x100))
253 #define  PMCR_IFG_XMIT(x)		(((x) & 0x3) << 18)
254 #define  PMCR_EXT_PHY			BIT(17)
255 #define  PMCR_MAC_MODE			BIT(16)
256 #define  PMCR_FORCE_MODE		BIT(15)
257 #define  PMCR_TX_EN			BIT(14)
258 #define  PMCR_RX_EN			BIT(13)
259 #define  PMCR_BACKOFF_EN		BIT(9)
260 #define  PMCR_BACKPR_EN			BIT(8)
261 #define  PMCR_FORCE_EEE1G		BIT(7)
262 #define  PMCR_FORCE_EEE100		BIT(6)
263 #define  PMCR_TX_FC_EN			BIT(5)
264 #define  PMCR_RX_FC_EN			BIT(4)
265 #define  PMCR_FORCE_SPEED_1000		BIT(3)
266 #define  PMCR_FORCE_SPEED_100		BIT(2)
267 #define  PMCR_FORCE_FDX			BIT(1)
268 #define  PMCR_FORCE_LNK			BIT(0)
269 #define  PMCR_SPEED_MASK		(PMCR_FORCE_SPEED_100 | \
270 					 PMCR_FORCE_SPEED_1000)
271 #define  MT7531_FORCE_LNK		BIT(31)
272 #define  MT7531_FORCE_SPD		BIT(30)
273 #define  MT7531_FORCE_DPX		BIT(29)
274 #define  MT7531_FORCE_RX_FC		BIT(28)
275 #define  MT7531_FORCE_TX_FC		BIT(27)
276 #define  MT7531_FORCE_MODE		(MT7531_FORCE_LNK | \
277 					 MT7531_FORCE_SPD | \
278 					 MT7531_FORCE_DPX | \
279 					 MT7531_FORCE_RX_FC | \
280 					 MT7531_FORCE_TX_FC)
281 #define  PMCR_FORCE_MODE_ID(id)		(((id) == ID_MT7531) ? \
282 					 MT7531_FORCE_MODE : \
283 					 PMCR_FORCE_MODE)
284 #define  PMCR_LINK_SETTINGS_MASK	(PMCR_TX_EN | PMCR_FORCE_SPEED_1000 | \
285 					 PMCR_RX_EN | PMCR_FORCE_SPEED_100 | \
286 					 PMCR_TX_FC_EN | PMCR_RX_FC_EN | \
287 					 PMCR_FORCE_FDX | PMCR_FORCE_LNK | \
288 					 PMCR_FORCE_EEE1G | PMCR_FORCE_EEE100)
289 #define  PMCR_CPU_PORT_SETTING(id)	(PMCR_FORCE_MODE_ID((id)) | \
290 					 PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | \
291 					 PMCR_BACKOFF_EN | PMCR_BACKPR_EN | \
292 					 PMCR_TX_EN | PMCR_RX_EN | \
293 					 PMCR_TX_FC_EN | PMCR_RX_FC_EN | \
294 					 PMCR_FORCE_SPEED_1000 | \
295 					 PMCR_FORCE_FDX | PMCR_FORCE_LNK)
296 
297 #define MT7530_PMEEECR_P(x)		(0x3004 + (x) * 0x100)
298 #define  WAKEUP_TIME_1000(x)		(((x) & 0xFF) << 24)
299 #define  WAKEUP_TIME_100(x)		(((x) & 0xFF) << 16)
300 #define  LPI_THRESH_MASK		GENMASK(15, 4)
301 #define  LPI_THRESH_SHT			4
302 #define  SET_LPI_THRESH(x)		(((x) << LPI_THRESH_SHT) & LPI_THRESH_MASK)
303 #define  GET_LPI_THRESH(x)		(((x) & LPI_THRESH_MASK) >> LPI_THRESH_SHT)
304 #define  LPI_MODE_EN			BIT(0)
305 
306 #define MT7530_PMSR_P(x)		(0x3008 + (x) * 0x100)
307 #define  PMSR_EEE1G			BIT(7)
308 #define  PMSR_EEE100M			BIT(6)
309 #define  PMSR_RX_FC			BIT(5)
310 #define  PMSR_TX_FC			BIT(4)
311 #define  PMSR_SPEED_1000		BIT(3)
312 #define  PMSR_SPEED_100			BIT(2)
313 #define  PMSR_SPEED_10			0x00
314 #define  PMSR_SPEED_MASK		(PMSR_SPEED_100 | PMSR_SPEED_1000)
315 #define  PMSR_DPX			BIT(1)
316 #define  PMSR_LINK			BIT(0)
317 
318 /* Register for port debug count */
319 #define MT7531_DBG_CNT(x)		(0x3018 + (x) * 0x100)
320 #define  MT7531_DIS_CLR			BIT(31)
321 
322 #define MT7530_GMACCR			0x30e0
323 #define  MAX_RX_JUMBO(x)		((x) << 2)
324 #define  MAX_RX_JUMBO_MASK		GENMASK(5, 2)
325 #define  MAX_RX_PKT_LEN_MASK		GENMASK(1, 0)
326 #define  MAX_RX_PKT_LEN_1522		0x0
327 #define  MAX_RX_PKT_LEN_1536		0x1
328 #define  MAX_RX_PKT_LEN_1552		0x2
329 #define  MAX_RX_PKT_LEN_JUMBO		0x3
330 
331 /* Register for MIB */
332 #define MT7530_PORT_MIB_COUNTER(x)	(0x4000 + (x) * 0x100)
333 #define MT7530_MIB_CCR			0x4fe0
334 #define  CCR_MIB_ENABLE			BIT(31)
335 #define  CCR_RX_OCT_CNT_GOOD		BIT(7)
336 #define  CCR_RX_OCT_CNT_BAD		BIT(6)
337 #define  CCR_TX_OCT_CNT_GOOD		BIT(5)
338 #define  CCR_TX_OCT_CNT_BAD		BIT(4)
339 #define  CCR_MIB_FLUSH			(CCR_RX_OCT_CNT_GOOD | \
340 					 CCR_RX_OCT_CNT_BAD | \
341 					 CCR_TX_OCT_CNT_GOOD | \
342 					 CCR_TX_OCT_CNT_BAD)
343 #define  CCR_MIB_ACTIVATE		(CCR_MIB_ENABLE | \
344 					 CCR_RX_OCT_CNT_GOOD | \
345 					 CCR_RX_OCT_CNT_BAD | \
346 					 CCR_TX_OCT_CNT_GOOD | \
347 					 CCR_TX_OCT_CNT_BAD)
348 
349 /* MT7531 SGMII register group */
350 #define MT7531_SGMII_REG_BASE		0x5000
351 #define MT7531_SGMII_REG(p, r)		(MT7531_SGMII_REG_BASE + \
352 					((p) - 5) * 0x1000 + (r))
353 
354 /* Register forSGMII PCS_CONTROL_1 */
355 #define MT7531_PCS_CONTROL_1(p)		MT7531_SGMII_REG(p, 0x00)
356 #define  MT7531_SGMII_LINK_STATUS	BIT(18)
357 #define  MT7531_SGMII_AN_ENABLE		BIT(12)
358 #define  MT7531_SGMII_AN_RESTART	BIT(9)
359 
360 /* Register for SGMII PCS_SPPED_ABILITY */
361 #define MT7531_PCS_SPEED_ABILITY(p)	MT7531_SGMII_REG(p, 0x08)
362 #define  MT7531_SGMII_TX_CONFIG_MASK	GENMASK(15, 0)
363 #define  MT7531_SGMII_TX_CONFIG		BIT(0)
364 
365 /* Register for SGMII_MODE */
366 #define MT7531_SGMII_MODE(p)		MT7531_SGMII_REG(p, 0x20)
367 #define  MT7531_SGMII_REMOTE_FAULT_DIS	BIT(8)
368 #define  MT7531_SGMII_IF_MODE_MASK	GENMASK(5, 1)
369 #define  MT7531_SGMII_FORCE_DUPLEX	BIT(4)
370 #define  MT7531_SGMII_FORCE_SPEED_MASK	GENMASK(3, 2)
371 #define  MT7531_SGMII_FORCE_SPEED_1000	BIT(3)
372 #define  MT7531_SGMII_FORCE_SPEED_100	BIT(2)
373 #define  MT7531_SGMII_FORCE_SPEED_10	0
374 #define  MT7531_SGMII_SPEED_DUPLEX_AN	BIT(1)
375 
376 enum mt7531_sgmii_force_duplex {
377 	MT7531_SGMII_FORCE_FULL_DUPLEX = 0,
378 	MT7531_SGMII_FORCE_HALF_DUPLEX = 0x10,
379 };
380 
381 /* Fields of QPHY_PWR_STATE_CTRL */
382 #define MT7531_QPHY_PWR_STATE_CTRL(p)	MT7531_SGMII_REG(p, 0xe8)
383 #define  MT7531_SGMII_PHYA_PWD		BIT(4)
384 
385 /* Values of SGMII SPEED */
386 #define MT7531_PHYA_CTRL_SIGNAL3(p)	MT7531_SGMII_REG(p, 0x128)
387 #define  MT7531_RG_TPHY_SPEED_MASK	(BIT(2) | BIT(3))
388 #define  MT7531_RG_TPHY_SPEED_1_25G	0x0
389 #define  MT7531_RG_TPHY_SPEED_3_125G	BIT(2)
390 
391 /* Register for system reset */
392 #define MT7530_SYS_CTRL			0x7000
393 #define  SYS_CTRL_PHY_RST		BIT(2)
394 #define  SYS_CTRL_SW_RST		BIT(1)
395 #define  SYS_CTRL_REG_RST		BIT(0)
396 
397 /* Register for system interrupt */
398 #define MT7530_SYS_INT_EN		0x7008
399 
400 /* Register for system interrupt status */
401 #define MT7530_SYS_INT_STS		0x700c
402 
403 /* Register for PHY Indirect Access Control */
404 #define MT7531_PHY_IAC			0x701C
405 #define  MT7531_PHY_ACS_ST		BIT(31)
406 #define  MT7531_MDIO_REG_ADDR_MASK	(0x1f << 25)
407 #define  MT7531_MDIO_PHY_ADDR_MASK	(0x1f << 20)
408 #define  MT7531_MDIO_CMD_MASK		(0x3 << 18)
409 #define  MT7531_MDIO_ST_MASK		(0x3 << 16)
410 #define  MT7531_MDIO_RW_DATA_MASK	(0xffff)
411 #define  MT7531_MDIO_REG_ADDR(x)	(((x) & 0x1f) << 25)
412 #define  MT7531_MDIO_DEV_ADDR(x)	(((x) & 0x1f) << 25)
413 #define  MT7531_MDIO_PHY_ADDR(x)	(((x) & 0x1f) << 20)
414 #define  MT7531_MDIO_CMD(x)		(((x) & 0x3) << 18)
415 #define  MT7531_MDIO_ST(x)		(((x) & 0x3) << 16)
416 
417 enum mt7531_phy_iac_cmd {
418 	MT7531_MDIO_ADDR = 0,
419 	MT7531_MDIO_WRITE = 1,
420 	MT7531_MDIO_READ = 2,
421 	MT7531_MDIO_READ_CL45 = 3,
422 };
423 
424 /* MDIO_ST: MDIO start field */
425 enum mt7531_mdio_st {
426 	MT7531_MDIO_ST_CL45 = 0,
427 	MT7531_MDIO_ST_CL22 = 1,
428 };
429 
430 #define  MT7531_MDIO_CL22_READ		(MT7531_MDIO_ST(MT7531_MDIO_ST_CL22) | \
431 					 MT7531_MDIO_CMD(MT7531_MDIO_READ))
432 #define  MT7531_MDIO_CL22_WRITE		(MT7531_MDIO_ST(MT7531_MDIO_ST_CL22) | \
433 					 MT7531_MDIO_CMD(MT7531_MDIO_WRITE))
434 #define  MT7531_MDIO_CL45_ADDR		(MT7531_MDIO_ST(MT7531_MDIO_ST_CL45) | \
435 					 MT7531_MDIO_CMD(MT7531_MDIO_ADDR))
436 #define  MT7531_MDIO_CL45_READ		(MT7531_MDIO_ST(MT7531_MDIO_ST_CL45) | \
437 					 MT7531_MDIO_CMD(MT7531_MDIO_READ))
438 #define  MT7531_MDIO_CL45_WRITE		(MT7531_MDIO_ST(MT7531_MDIO_ST_CL45) | \
439 					 MT7531_MDIO_CMD(MT7531_MDIO_WRITE))
440 
441 /* Register for RGMII clock phase */
442 #define MT7531_CLKGEN_CTRL		0x7500
443 #define  CLK_SKEW_OUT(x)		(((x) & 0x3) << 8)
444 #define  CLK_SKEW_OUT_MASK		GENMASK(9, 8)
445 #define  CLK_SKEW_IN(x)			(((x) & 0x3) << 6)
446 #define  CLK_SKEW_IN_MASK		GENMASK(7, 6)
447 #define  RXCLK_NO_DELAY			BIT(5)
448 #define  TXCLK_NO_REVERSE		BIT(4)
449 #define  GP_MODE(x)			(((x) & 0x3) << 1)
450 #define  GP_MODE_MASK			GENMASK(2, 1)
451 #define  GP_CLK_EN			BIT(0)
452 
453 enum mt7531_gp_mode {
454 	MT7531_GP_MODE_RGMII = 0,
455 	MT7531_GP_MODE_MII = 1,
456 	MT7531_GP_MODE_REV_MII = 2
457 };
458 
459 enum mt7531_clk_skew {
460 	MT7531_CLK_SKEW_NO_CHG = 0,
461 	MT7531_CLK_SKEW_DLY_100PPS = 1,
462 	MT7531_CLK_SKEW_DLY_200PPS = 2,
463 	MT7531_CLK_SKEW_REVERSE = 3,
464 };
465 
466 /* Register for hw trap status */
467 #define MT7530_HWTRAP			0x7800
468 #define  HWTRAP_XTAL_MASK		(BIT(10) | BIT(9))
469 #define  HWTRAP_XTAL_25MHZ		(BIT(10) | BIT(9))
470 #define  HWTRAP_XTAL_40MHZ		(BIT(10))
471 #define  HWTRAP_XTAL_20MHZ		(BIT(9))
472 
473 #define MT7531_HWTRAP			0x7800
474 #define  HWTRAP_XTAL_FSEL_MASK		BIT(7)
475 #define  HWTRAP_XTAL_FSEL_25MHZ		BIT(7)
476 #define  HWTRAP_XTAL_FSEL_40MHZ		0
477 /* Unique fields of (M)HWSTRAP for MT7531 */
478 #define  XTAL_FSEL_S			7
479 #define  XTAL_FSEL_M			BIT(7)
480 #define  PHY_EN				BIT(6)
481 #define  CHG_STRAP			BIT(8)
482 
483 /* Register for hw trap modification */
484 #define MT7530_MHWTRAP			0x7804
485 #define  MHWTRAP_PHY0_SEL		BIT(20)
486 #define  MHWTRAP_MANUAL			BIT(16)
487 #define  MHWTRAP_P5_MAC_SEL		BIT(13)
488 #define  MHWTRAP_P6_DIS			BIT(8)
489 #define  MHWTRAP_P5_RGMII_MODE		BIT(7)
490 #define  MHWTRAP_P5_DIS			BIT(6)
491 #define  MHWTRAP_PHY_ACCESS		BIT(5)
492 
493 /* Register for TOP signal control */
494 #define MT7530_TOP_SIG_CTRL		0x7808
495 #define  TOP_SIG_CTRL_NORMAL		(BIT(17) | BIT(16))
496 
497 #define MT7531_TOP_SIG_SR		0x780c
498 #define  PAD_DUAL_SGMII_EN		BIT(1)
499 #define  PAD_MCM_SMI_EN			BIT(0)
500 
501 #define MT7530_IO_DRV_CR		0x7810
502 #define  P5_IO_CLK_DRV(x)		((x) & 0x3)
503 #define  P5_IO_DATA_DRV(x)		(((x) & 0x3) << 4)
504 
505 #define MT7531_CHIP_REV			0x781C
506 
507 #define MT7531_PLLGP_EN			0x7820
508 #define  EN_COREPLL			BIT(2)
509 #define  SW_CLKSW			BIT(1)
510 #define  SW_PLLGP			BIT(0)
511 
512 #define MT7530_P6ECR			0x7830
513 #define  P6_INTF_MODE_MASK		0x3
514 #define  P6_INTF_MODE(x)		((x) & 0x3)
515 
516 #define MT7531_PLLGP_CR0		0x78a8
517 #define  RG_COREPLL_EN			BIT(22)
518 #define  RG_COREPLL_POSDIV_S		23
519 #define  RG_COREPLL_POSDIV_M		0x3800000
520 #define  RG_COREPLL_SDM_PCW_S		1
521 #define  RG_COREPLL_SDM_PCW_M		0x3ffffe
522 #define  RG_COREPLL_SDM_PCW_CHG		BIT(0)
523 
524 /* Registers for RGMII and SGMII PLL clock */
525 #define MT7531_ANA_PLLGP_CR2		0x78b0
526 #define MT7531_ANA_PLLGP_CR5		0x78bc
527 
528 /* Registers for TRGMII on the both side */
529 #define MT7530_TRGMII_RCK_CTRL		0x7a00
530 #define  RX_RST				BIT(31)
531 #define  RXC_DQSISEL			BIT(30)
532 #define  DQSI1_TAP_MASK			(0x7f << 8)
533 #define  DQSI0_TAP_MASK			0x7f
534 #define  DQSI1_TAP(x)			(((x) & 0x7f) << 8)
535 #define  DQSI0_TAP(x)			((x) & 0x7f)
536 
537 #define MT7530_TRGMII_RCK_RTT		0x7a04
538 #define  DQS1_GATE			BIT(31)
539 #define  DQS0_GATE			BIT(30)
540 
541 #define MT7530_TRGMII_RD(x)		(0x7a10 + (x) * 8)
542 #define  BSLIP_EN			BIT(31)
543 #define  EDGE_CHK			BIT(30)
544 #define  RD_TAP_MASK			0x7f
545 #define  RD_TAP(x)			((x) & 0x7f)
546 
547 #define MT7530_TRGMII_TXCTRL		0x7a40
548 #define  TRAIN_TXEN			BIT(31)
549 #define  TXC_INV			BIT(30)
550 #define  TX_RST				BIT(28)
551 
552 #define MT7530_TRGMII_TD_ODT(i)		(0x7a54 + 8 * (i))
553 #define  TD_DM_DRVP(x)			((x) & 0xf)
554 #define  TD_DM_DRVN(x)			(((x) & 0xf) << 4)
555 
556 #define MT7530_TRGMII_TCK_CTRL		0x7a78
557 #define  TCK_TAP(x)			(((x) & 0xf) << 8)
558 
559 #define MT7530_P5RGMIIRXCR		0x7b00
560 #define  CSR_RGMII_EDGE_ALIGN		BIT(8)
561 #define  CSR_RGMII_RXC_0DEG_CFG(x)	((x) & 0xf)
562 
563 #define MT7530_P5RGMIITXCR		0x7b04
564 #define  CSR_RGMII_TXC_CFG(x)		((x) & 0x1f)
565 
566 /* Registers for GPIO mode */
567 #define MT7531_GPIO_MODE0		0x7c0c
568 #define  MT7531_GPIO0_MASK		GENMASK(3, 0)
569 #define  MT7531_GPIO0_INTERRUPT		1
570 
571 #define MT7531_GPIO_MODE1		0x7c10
572 #define  MT7531_GPIO11_RG_RXD2_MASK	GENMASK(15, 12)
573 #define  MT7531_EXT_P_MDC_11		(2 << 12)
574 #define  MT7531_GPIO12_RG_RXD3_MASK	GENMASK(19, 16)
575 #define  MT7531_EXT_P_MDIO_12		(2 << 16)
576 
577 /* Registers for LED GPIO control (MT7530 only)
578  * All registers follow this pattern:
579  * [ 2: 0]  port 0
580  * [ 6: 4]  port 1
581  * [10: 8]  port 2
582  * [14:12]  port 3
583  * [18:16]  port 4
584  */
585 
586 /* LED enable, 0: Disable, 1: Enable (Default) */
587 #define MT7530_LED_EN			0x7d00
588 /* LED mode, 0: GPIO mode, 1: PHY mode (Default) */
589 #define MT7530_LED_IO_MODE		0x7d04
590 /* GPIO direction, 0: Input, 1: Output */
591 #define MT7530_LED_GPIO_DIR		0x7d10
592 /* GPIO output enable, 0: Disable, 1: Enable */
593 #define MT7530_LED_GPIO_OE		0x7d14
594 /* GPIO value, 0: Low, 1: High */
595 #define MT7530_LED_GPIO_DATA		0x7d18
596 
597 #define MT7530_CREV			0x7ffc
598 #define  CHIP_NAME_SHIFT		16
599 #define  MT7530_ID			0x7530
600 
601 #define MT7531_CREV			0x781C
602 #define  CHIP_REV_M			0x0f
603 #define  MT7531_ID			0x7531
604 
605 /* Registers for core PLL access through mmd indirect */
606 #define CORE_PLL_GROUP2			0x401
607 #define  RG_SYSPLL_EN_NORMAL		BIT(15)
608 #define  RG_SYSPLL_VODEN		BIT(14)
609 #define  RG_SYSPLL_LF			BIT(13)
610 #define  RG_SYSPLL_RST_DLY(x)		(((x) & 0x3) << 12)
611 #define  RG_SYSPLL_LVROD_EN		BIT(10)
612 #define  RG_SYSPLL_PREDIV(x)		(((x) & 0x3) << 8)
613 #define  RG_SYSPLL_POSDIV(x)		(((x) & 0x3) << 5)
614 #define  RG_SYSPLL_FBKSEL		BIT(4)
615 #define  RT_SYSPLL_EN_AFE_OLT		BIT(0)
616 
617 #define CORE_PLL_GROUP4			0x403
618 #define  RG_SYSPLL_DDSFBK_EN		BIT(12)
619 #define  RG_SYSPLL_BIAS_EN		BIT(11)
620 #define  RG_SYSPLL_BIAS_LPF_EN		BIT(10)
621 #define  MT7531_PHY_PLL_OFF		BIT(5)
622 #define  MT7531_PHY_PLL_BYPASS_MODE	BIT(4)
623 
624 #define MT753X_CTRL_PHY_ADDR		0
625 
626 #define CORE_PLL_GROUP5			0x404
627 #define  RG_LCDDS_PCW_NCPO1(x)		((x) & 0xffff)
628 
629 #define CORE_PLL_GROUP6			0x405
630 #define  RG_LCDDS_PCW_NCPO0(x)		((x) & 0xffff)
631 
632 #define CORE_PLL_GROUP7			0x406
633 #define  RG_LCDDS_PWDB			BIT(15)
634 #define  RG_LCDDS_ISO_EN		BIT(13)
635 #define  RG_LCCDS_C(x)			(((x) & 0x7) << 4)
636 #define  RG_LCDDS_PCW_NCPO_CHG		BIT(3)
637 
638 #define CORE_PLL_GROUP10		0x409
639 #define  RG_LCDDS_SSC_DELTA(x)		((x) & 0xfff)
640 
641 #define CORE_PLL_GROUP11		0x40a
642 #define  RG_LCDDS_SSC_DELTA1(x)		((x) & 0xfff)
643 
644 #define CORE_GSWPLL_GRP1		0x40d
645 #define  RG_GSWPLL_PREDIV(x)		(((x) & 0x3) << 14)
646 #define  RG_GSWPLL_POSDIV_200M(x)	(((x) & 0x3) << 12)
647 #define  RG_GSWPLL_EN_PRE		BIT(11)
648 #define  RG_GSWPLL_FBKSEL		BIT(10)
649 #define  RG_GSWPLL_BP			BIT(9)
650 #define  RG_GSWPLL_BR			BIT(8)
651 #define  RG_GSWPLL_FBKDIV_200M(x)	((x) & 0xff)
652 
653 #define CORE_GSWPLL_GRP2		0x40e
654 #define  RG_GSWPLL_POSDIV_500M(x)	(((x) & 0x3) << 8)
655 #define  RG_GSWPLL_FBKDIV_500M(x)	((x) & 0xff)
656 
657 #define CORE_TRGMII_GSW_CLK_CG		0x410
658 #define  REG_GSWCK_EN			BIT(0)
659 #define  REG_TRGMIICK_EN		BIT(1)
660 
661 #define MIB_DESC(_s, _o, _n)	\
662 	{			\
663 		.size = (_s),	\
664 		.offset = (_o),	\
665 		.name = (_n),	\
666 	}
667 
668 struct mt7530_mib_desc {
669 	unsigned int size;
670 	unsigned int offset;
671 	const char *name;
672 };
673 
674 struct mt7530_fdb {
675 	u16 vid;
676 	u8 port_mask;
677 	u8 aging;
678 	u8 mac[6];
679 	bool noarp;
680 };
681 
682 /* struct mt7530_port -	This is the main data structure for holding the state
683  *			of the port.
684  * @enable:	The status used for show port is enabled or not.
685  * @pm:		The matrix used to show all connections with the port.
686  * @pvid:	The VLAN specified is to be considered a PVID at ingress.  Any
687  *		untagged frames will be assigned to the related VLAN.
688  * @vlan_filtering: The flags indicating whether the port that can recognize
689  *		    VLAN-tagged frames.
690  */
691 struct mt7530_port {
692 	bool enable;
693 	u32 pm;
694 	u16 pvid;
695 };
696 
697 /* Port 5 interface select definitions */
698 enum p5_interface_select {
699 	P5_DISABLED = 0,
700 	P5_INTF_SEL_PHY_P0,
701 	P5_INTF_SEL_PHY_P4,
702 	P5_INTF_SEL_GMAC5,
703 	P5_INTF_SEL_GMAC5_SGMII,
704 };
705 
706 static const char *p5_intf_modes(unsigned int p5_interface)
707 {
708 	switch (p5_interface) {
709 	case P5_DISABLED:
710 		return "DISABLED";
711 	case P5_INTF_SEL_PHY_P0:
712 		return "PHY P0";
713 	case P5_INTF_SEL_PHY_P4:
714 		return "PHY P4";
715 	case P5_INTF_SEL_GMAC5:
716 		return "GMAC5";
717 	case P5_INTF_SEL_GMAC5_SGMII:
718 		return "GMAC5_SGMII";
719 	default:
720 		return "unknown";
721 	}
722 }
723 
724 struct mt7530_priv;
725 
726 /* struct mt753x_info -	This is the main data structure for holding the specific
727  *			part for each supported device
728  * @sw_setup:		Holding the handler to a device initialization
729  * @phy_read:		Holding the way reading PHY port
730  * @phy_write:		Holding the way writing PHY port
731  * @pad_setup:		Holding the way setting up the bus pad for a certain
732  *			MAC port
733  * @phy_mode_supported:	Check if the PHY type is being supported on a certain
734  *			port
735  * @mac_port_validate:	Holding the way to set addition validate type for a
736  *			certan MAC port
737  * @mac_port_get_state: Holding the way getting the MAC/PCS state for a certain
738  *			MAC port
739  * @mac_port_config:	Holding the way setting up the PHY attribute to a
740  *			certain MAC port
741  * @mac_pcs_an_restart	Holding the way restarting PCS autonegotiation for a
742  *			certain MAC port
743  * @mac_pcs_link_up:	Holding the way setting up the PHY attribute to the pcs
744  *			of the certain MAC port
745  */
746 struct mt753x_info {
747 	enum mt753x_id id;
748 
749 	int (*sw_setup)(struct dsa_switch *ds);
750 	int (*phy_read)(struct mt7530_priv *priv, int port, int regnum);
751 	int (*phy_write)(struct mt7530_priv *priv, int port, int regnum, u16 val);
752 	int (*pad_setup)(struct dsa_switch *ds, phy_interface_t interface);
753 	int (*cpu_port_config)(struct dsa_switch *ds, int port);
754 	bool (*phy_mode_supported)(struct dsa_switch *ds, int port,
755 				   const struct phylink_link_state *state);
756 	void (*mac_port_validate)(struct dsa_switch *ds, int port,
757 				  unsigned long *supported);
758 	int (*mac_port_get_state)(struct dsa_switch *ds, int port,
759 				  struct phylink_link_state *state);
760 	int (*mac_port_config)(struct dsa_switch *ds, int port,
761 			       unsigned int mode,
762 			       phy_interface_t interface);
763 	void (*mac_pcs_an_restart)(struct dsa_switch *ds, int port);
764 	void (*mac_pcs_link_up)(struct dsa_switch *ds, int port,
765 				unsigned int mode, phy_interface_t interface,
766 				int speed, int duplex);
767 };
768 
769 /* struct mt7530_priv -	This is the main data structure for holding the state
770  *			of the driver
771  * @dev:		The device pointer
772  * @ds:			The pointer to the dsa core structure
773  * @bus:		The bus used for the device and built-in PHY
774  * @rstc:		The pointer to reset control used by MCM
775  * @core_pwr:		The power supplied into the core
776  * @io_pwr:		The power supplied into the I/O
777  * @reset:		The descriptor for GPIO line tied to its reset pin
778  * @mcm:		Flag for distinguishing if standalone IC or module
779  *			coupling
780  * @ports:		Holding the state among ports
781  * @reg_mutex:		The lock for protecting among process accessing
782  *			registers
783  * @p6_interface	Holding the current port 6 interface
784  * @p5_intf_sel:	Holding the current port 5 interface select
785  *
786  * @irq:		IRQ number of the switch
787  * @irq_domain:		IRQ domain of the switch irq_chip
788  * @irq_enable:		IRQ enable bits, synced to SYS_INT_EN
789  */
790 struct mt7530_priv {
791 	struct device		*dev;
792 	struct dsa_switch	*ds;
793 	struct mii_bus		*bus;
794 	struct reset_control	*rstc;
795 	struct regulator	*core_pwr;
796 	struct regulator	*io_pwr;
797 	struct gpio_desc	*reset;
798 	const struct mt753x_info *info;
799 	unsigned int		id;
800 	bool			mcm;
801 	phy_interface_t		p6_interface;
802 	phy_interface_t		p5_interface;
803 	unsigned int		p5_intf_sel;
804 	u8			mirror_rx;
805 	u8			mirror_tx;
806 
807 	struct mt7530_port	ports[MT7530_NUM_PORTS];
808 	/* protect among processes for registers access*/
809 	struct mutex reg_mutex;
810 	int irq;
811 	struct irq_domain *irq_domain;
812 	u32 irq_enable;
813 };
814 
815 struct mt7530_hw_vlan_entry {
816 	int port;
817 	u8  old_members;
818 	bool untagged;
819 };
820 
821 static inline void mt7530_hw_vlan_entry_init(struct mt7530_hw_vlan_entry *e,
822 					     int port, bool untagged)
823 {
824 	e->port = port;
825 	e->untagged = untagged;
826 }
827 
828 typedef void (*mt7530_vlan_op)(struct mt7530_priv *,
829 			       struct mt7530_hw_vlan_entry *);
830 
831 struct mt7530_hw_stats {
832 	const char	*string;
833 	u16		reg;
834 	u8		sizeof_stat;
835 };
836 
837 struct mt7530_dummy_poll {
838 	struct mt7530_priv *priv;
839 	u32 reg;
840 };
841 
842 static inline void INIT_MT7530_DUMMY_POLL(struct mt7530_dummy_poll *p,
843 					  struct mt7530_priv *priv, u32 reg)
844 {
845 	p->priv = priv;
846 	p->reg = reg;
847 }
848 
849 #endif /* __MT7530_H */
850