blob: 3cfce058451d4210b9d784ff4c40c052c135ae81 [file] [log] [blame]
Weijie Gao23f17162018-12-20 16:12:53 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2018 MediaTek Inc.
4 *
5 * Author: Weijie Gao <weijie.gao@mediatek.com>
6 * Author: Mark Lee <mark-mc.lee@mediatek.com>
7 */
8
9#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070010#include <cpu_func.h>
Weijie Gao23f17162018-12-20 16:12:53 +080011#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Weijie Gao23f17162018-12-20 16:12:53 +080013#include <malloc.h>
14#include <miiphy.h>
Simon Glass90526e92020-05-10 11:39:56 -060015#include <net.h>
Weijie Gao23f17162018-12-20 16:12:53 +080016#include <regmap.h>
17#include <reset.h>
18#include <syscon.h>
19#include <wait_bit.h>
Simon Glass90526e92020-05-10 11:39:56 -060020#include <asm/cache.h>
Weijie Gao23f17162018-12-20 16:12:53 +080021#include <asm/gpio.h>
22#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Simon Glassc05ed002020-05-10 11:40:11 -060024#include <linux/delay.h>
Weijie Gao23f17162018-12-20 16:12:53 +080025#include <linux/err.h>
26#include <linux/ioport.h>
27#include <linux/mdio.h>
28#include <linux/mii.h>
Simon Glass1e94b462023-09-14 18:21:46 -060029#include <linux/printk.h>
Weijie Gao23f17162018-12-20 16:12:53 +080030
31#include "mtk_eth.h"
32
33#define NUM_TX_DESC 24
34#define NUM_RX_DESC 24
35#define TX_TOTAL_BUF_SIZE (NUM_TX_DESC * PKTSIZE_ALIGN)
36#define RX_TOTAL_BUF_SIZE (NUM_RX_DESC * PKTSIZE_ALIGN)
37#define TOTAL_PKT_BUF_SIZE (TX_TOTAL_BUF_SIZE + RX_TOTAL_BUF_SIZE)
38
Landen Chao532de8d2020-02-18 16:49:37 +080039#define MT753X_NUM_PHYS 5
40#define MT753X_NUM_PORTS 7
41#define MT753X_DFL_SMI_ADDR 31
42#define MT753X_SMI_ADDR_MASK 0x1f
Weijie Gao23f17162018-12-20 16:12:53 +080043
Landen Chao532de8d2020-02-18 16:49:37 +080044#define MT753X_PHY_ADDR(base, addr) \
Weijie Gao23f17162018-12-20 16:12:53 +080045 (((base) + (addr)) & 0x1f)
46
47#define GDMA_FWD_TO_CPU \
48 (0x20000000 | \
49 GDM_ICS_EN | \
50 GDM_TCS_EN | \
51 GDM_UCS_EN | \
52 STRP_CRC | \
53 (DP_PDMA << MYMAC_DP_S) | \
54 (DP_PDMA << BC_DP_S) | \
55 (DP_PDMA << MC_DP_S) | \
56 (DP_PDMA << UN_DP_S))
57
Weijie Gao93eb7072023-07-19 17:17:41 +080058#define GDMA_BRIDGE_TO_CPU \
59 (0xC0000000 | \
60 GDM_ICS_EN | \
61 GDM_TCS_EN | \
62 GDM_UCS_EN | \
63 (DP_PDMA << MYMAC_DP_S) | \
64 (DP_PDMA << BC_DP_S) | \
65 (DP_PDMA << MC_DP_S) | \
66 (DP_PDMA << UN_DP_S))
67
Weijie Gao23f17162018-12-20 16:12:53 +080068#define GDMA_FWD_DISCARD \
69 (0x20000000 | \
70 GDM_ICS_EN | \
71 GDM_TCS_EN | \
72 GDM_UCS_EN | \
73 STRP_CRC | \
74 (DP_DISCARD << MYMAC_DP_S) | \
75 (DP_DISCARD << BC_DP_S) | \
76 (DP_DISCARD << MC_DP_S) | \
77 (DP_DISCARD << UN_DP_S))
78
Weijie Gao23f17162018-12-20 16:12:53 +080079enum mtk_switch {
80 SW_NONE,
Landen Chao532de8d2020-02-18 16:49:37 +080081 SW_MT7530,
Weijie Gao93eb7072023-07-19 17:17:41 +080082 SW_MT7531,
83 SW_MT7988,
Weijie Gao23f17162018-12-20 16:12:53 +080084};
85
Weijie Gao62596722022-09-09 19:59:21 +080086/* struct mtk_soc_data - This is the structure holding all differences
87 * among various plaforms
88 * @caps Flags shown the extra capability for the SoC
89 * @ana_rgc3: The offset for register ANA_RGC3 related to
90 * sgmiisys syscon
Weijie Gao76281942023-07-19 17:17:37 +080091 * @gdma_count: Number of GDMAs
Weijie Gaoe7ad0462022-09-09 19:59:26 +080092 * @pdma_base: Register base of PDMA block
93 * @txd_size: Tx DMA descriptor size.
94 * @rxd_size: Rx DMA descriptor size.
Weijie Gao62596722022-09-09 19:59:21 +080095 */
96struct mtk_soc_data {
97 u32 caps;
98 u32 ana_rgc3;
Weijie Gao76281942023-07-19 17:17:37 +080099 u32 gdma_count;
Weijie Gaoe7ad0462022-09-09 19:59:26 +0800100 u32 pdma_base;
Weijie Gao7d928c32022-09-09 19:59:24 +0800101 u32 txd_size;
102 u32 rxd_size;
Weijie Gao23f17162018-12-20 16:12:53 +0800103};
104
105struct mtk_eth_priv {
106 char pkt_pool[TOTAL_PKT_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
107
Weijie Gao7d928c32022-09-09 19:59:24 +0800108 void *tx_ring_noc;
109 void *rx_ring_noc;
Weijie Gao23f17162018-12-20 16:12:53 +0800110
111 int rx_dma_owner_idx0;
112 int tx_cpu_owner_idx0;
113
114 void __iomem *fe_base;
115 void __iomem *gmac_base;
MarkLeeb4ef49a2020-01-21 19:31:57 +0800116 void __iomem *sgmii_base;
Weijie Gao93eb7072023-07-19 17:17:41 +0800117 void __iomem *gsw_base;
Weijie Gao23f17162018-12-20 16:12:53 +0800118
Weijie Gao86062e72022-05-20 11:23:37 +0800119 struct regmap *ethsys_regmap;
120
Weijie Gao585a1a42023-07-19 17:17:22 +0800121 struct regmap *infra_regmap;
122
Weijie Gaoba026eb2023-07-19 17:17:31 +0800123 struct regmap *usxgmii_regmap;
124 struct regmap *xfi_pextp_regmap;
125 struct regmap *xfi_pll_regmap;
126 struct regmap *toprgu_regmap;
127
Weijie Gao23f17162018-12-20 16:12:53 +0800128 struct mii_dev *mdio_bus;
129 int (*mii_read)(struct mtk_eth_priv *priv, u8 phy, u8 reg);
130 int (*mii_write)(struct mtk_eth_priv *priv, u8 phy, u8 reg, u16 val);
131 int (*mmd_read)(struct mtk_eth_priv *priv, u8 addr, u8 devad, u16 reg);
132 int (*mmd_write)(struct mtk_eth_priv *priv, u8 addr, u8 devad, u16 reg,
133 u16 val);
134
Weijie Gao62596722022-09-09 19:59:21 +0800135 const struct mtk_soc_data *soc;
Weijie Gao23f17162018-12-20 16:12:53 +0800136 int gmac_id;
137 int force_mode;
138 int speed;
139 int duplex;
Weijie Gao29a48bf2022-09-09 19:59:28 +0800140 bool pn_swap;
Weijie Gao23f17162018-12-20 16:12:53 +0800141
142 struct phy_device *phydev;
143 int phy_interface;
144 int phy_addr;
145
146 enum mtk_switch sw;
147 int (*switch_init)(struct mtk_eth_priv *priv);
Weijie Gaoc73d3872023-07-19 17:16:54 +0800148 void (*switch_mac_control)(struct mtk_eth_priv *priv, bool enable);
Landen Chao532de8d2020-02-18 16:49:37 +0800149 u32 mt753x_smi_addr;
150 u32 mt753x_phy_base;
Weijie Gaoc73d3872023-07-19 17:16:54 +0800151 u32 mt753x_pmcr;
Weijie Gaoc41a0582023-07-19 17:16:59 +0800152 u32 mt753x_reset_wait_time;
Weijie Gao23f17162018-12-20 16:12:53 +0800153
154 struct gpio_desc rst_gpio;
155 int mcm;
156
157 struct reset_ctl rst_fe;
158 struct reset_ctl rst_mcm;
159};
160
161static void mtk_pdma_write(struct mtk_eth_priv *priv, u32 reg, u32 val)
162{
Weijie Gaoe7ad0462022-09-09 19:59:26 +0800163 writel(val, priv->fe_base + priv->soc->pdma_base + reg);
Weijie Gao23f17162018-12-20 16:12:53 +0800164}
165
166static void mtk_pdma_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr,
167 u32 set)
168{
Weijie Gaoe7ad0462022-09-09 19:59:26 +0800169 clrsetbits_le32(priv->fe_base + priv->soc->pdma_base + reg, clr, set);
Weijie Gao23f17162018-12-20 16:12:53 +0800170}
171
172static void mtk_gdma_write(struct mtk_eth_priv *priv, int no, u32 reg,
173 u32 val)
174{
175 u32 gdma_base;
176
Weijie Gao76281942023-07-19 17:17:37 +0800177 if (no == 2)
178 gdma_base = GDMA3_BASE;
179 else if (no == 1)
Weijie Gao23f17162018-12-20 16:12:53 +0800180 gdma_base = GDMA2_BASE;
181 else
182 gdma_base = GDMA1_BASE;
183
184 writel(val, priv->fe_base + gdma_base + reg);
185}
186
Weijie Gao93eb7072023-07-19 17:17:41 +0800187static void mtk_fe_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr, u32 set)
188{
189 clrsetbits_le32(priv->fe_base + reg, clr, set);
190}
191
Weijie Gao23f17162018-12-20 16:12:53 +0800192static u32 mtk_gmac_read(struct mtk_eth_priv *priv, u32 reg)
193{
194 return readl(priv->gmac_base + reg);
195}
196
197static void mtk_gmac_write(struct mtk_eth_priv *priv, u32 reg, u32 val)
198{
199 writel(val, priv->gmac_base + reg);
200}
201
202static void mtk_gmac_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr, u32 set)
203{
204 clrsetbits_le32(priv->gmac_base + reg, clr, set);
205}
206
207static void mtk_ethsys_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr,
208 u32 set)
209{
Weijie Gao86062e72022-05-20 11:23:37 +0800210 uint val;
211
212 regmap_read(priv->ethsys_regmap, reg, &val);
213 val &= ~clr;
214 val |= set;
215 regmap_write(priv->ethsys_regmap, reg, val);
Weijie Gao23f17162018-12-20 16:12:53 +0800216}
217
Weijie Gao585a1a42023-07-19 17:17:22 +0800218static void mtk_infra_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr,
219 u32 set)
220{
221 uint val;
222
223 regmap_read(priv->infra_regmap, reg, &val);
224 val &= ~clr;
225 val |= set;
226 regmap_write(priv->infra_regmap, reg, val);
227}
228
Weijie Gao93eb7072023-07-19 17:17:41 +0800229static u32 mtk_gsw_read(struct mtk_eth_priv *priv, u32 reg)
230{
231 return readl(priv->gsw_base + reg);
232}
233
234static void mtk_gsw_write(struct mtk_eth_priv *priv, u32 reg, u32 val)
235{
236 writel(val, priv->gsw_base + reg);
237}
238
Weijie Gao23f17162018-12-20 16:12:53 +0800239/* Direct MDIO clause 22/45 access via SoC */
240static int mtk_mii_rw(struct mtk_eth_priv *priv, u8 phy, u8 reg, u16 data,
241 u32 cmd, u32 st)
242{
243 int ret;
244 u32 val;
245
246 val = (st << MDIO_ST_S) |
247 ((cmd << MDIO_CMD_S) & MDIO_CMD_M) |
248 (((u32)phy << MDIO_PHY_ADDR_S) & MDIO_PHY_ADDR_M) |
249 (((u32)reg << MDIO_REG_ADDR_S) & MDIO_REG_ADDR_M);
250
Weijie Gaoc94ad002023-07-19 17:17:03 +0800251 if (cmd == MDIO_CMD_WRITE || cmd == MDIO_CMD_ADDR)
Weijie Gao23f17162018-12-20 16:12:53 +0800252 val |= data & MDIO_RW_DATA_M;
253
254 mtk_gmac_write(priv, GMAC_PIAC_REG, val | PHY_ACS_ST);
255
256 ret = wait_for_bit_le32(priv->gmac_base + GMAC_PIAC_REG,
257 PHY_ACS_ST, 0, 5000, 0);
258 if (ret) {
259 pr_warn("MDIO access timeout\n");
260 return ret;
261 }
262
Weijie Gaoc94ad002023-07-19 17:17:03 +0800263 if (cmd == MDIO_CMD_READ || cmd == MDIO_CMD_READ_C45) {
Weijie Gao23f17162018-12-20 16:12:53 +0800264 val = mtk_gmac_read(priv, GMAC_PIAC_REG);
265 return val & MDIO_RW_DATA_M;
266 }
267
268 return 0;
269}
270
271/* Direct MDIO clause 22 read via SoC */
272static int mtk_mii_read(struct mtk_eth_priv *priv, u8 phy, u8 reg)
273{
274 return mtk_mii_rw(priv, phy, reg, 0, MDIO_CMD_READ, MDIO_ST_C22);
275}
276
277/* Direct MDIO clause 22 write via SoC */
278static int mtk_mii_write(struct mtk_eth_priv *priv, u8 phy, u8 reg, u16 data)
279{
280 return mtk_mii_rw(priv, phy, reg, data, MDIO_CMD_WRITE, MDIO_ST_C22);
281}
282
283/* Direct MDIO clause 45 read via SoC */
284static int mtk_mmd_read(struct mtk_eth_priv *priv, u8 addr, u8 devad, u16 reg)
285{
286 int ret;
287
288 ret = mtk_mii_rw(priv, addr, devad, reg, MDIO_CMD_ADDR, MDIO_ST_C45);
289 if (ret)
290 return ret;
291
292 return mtk_mii_rw(priv, addr, devad, 0, MDIO_CMD_READ_C45,
293 MDIO_ST_C45);
294}
295
296/* Direct MDIO clause 45 write via SoC */
297static int mtk_mmd_write(struct mtk_eth_priv *priv, u8 addr, u8 devad,
298 u16 reg, u16 val)
299{
300 int ret;
301
302 ret = mtk_mii_rw(priv, addr, devad, reg, MDIO_CMD_ADDR, MDIO_ST_C45);
303 if (ret)
304 return ret;
305
306 return mtk_mii_rw(priv, addr, devad, val, MDIO_CMD_WRITE,
307 MDIO_ST_C45);
308}
309
310/* Indirect MDIO clause 45 read via MII registers */
311static int mtk_mmd_ind_read(struct mtk_eth_priv *priv, u8 addr, u8 devad,
312 u16 reg)
313{
314 int ret;
315
316 ret = priv->mii_write(priv, addr, MII_MMD_ACC_CTL_REG,
317 (MMD_ADDR << MMD_CMD_S) |
318 ((devad << MMD_DEVAD_S) & MMD_DEVAD_M));
319 if (ret)
320 return ret;
321
322 ret = priv->mii_write(priv, addr, MII_MMD_ADDR_DATA_REG, reg);
323 if (ret)
324 return ret;
325
326 ret = priv->mii_write(priv, addr, MII_MMD_ACC_CTL_REG,
327 (MMD_DATA << MMD_CMD_S) |
328 ((devad << MMD_DEVAD_S) & MMD_DEVAD_M));
329 if (ret)
330 return ret;
331
332 return priv->mii_read(priv, addr, MII_MMD_ADDR_DATA_REG);
333}
334
335/* Indirect MDIO clause 45 write via MII registers */
336static int mtk_mmd_ind_write(struct mtk_eth_priv *priv, u8 addr, u8 devad,
337 u16 reg, u16 val)
338{
339 int ret;
340
341 ret = priv->mii_write(priv, addr, MII_MMD_ACC_CTL_REG,
342 (MMD_ADDR << MMD_CMD_S) |
343 ((devad << MMD_DEVAD_S) & MMD_DEVAD_M));
344 if (ret)
345 return ret;
346
347 ret = priv->mii_write(priv, addr, MII_MMD_ADDR_DATA_REG, reg);
348 if (ret)
349 return ret;
350
351 ret = priv->mii_write(priv, addr, MII_MMD_ACC_CTL_REG,
352 (MMD_DATA << MMD_CMD_S) |
353 ((devad << MMD_DEVAD_S) & MMD_DEVAD_M));
354 if (ret)
355 return ret;
356
357 return priv->mii_write(priv, addr, MII_MMD_ADDR_DATA_REG, val);
358}
359
Landen Chao532de8d2020-02-18 16:49:37 +0800360/*
361 * MT7530 Internal Register Address Bits
362 * -------------------------------------------------------------------
363 * | 15 14 13 12 11 10 9 8 7 6 | 5 4 3 2 | 1 0 |
364 * |----------------------------------------|---------------|--------|
365 * | Page Address | Reg Address | Unused |
366 * -------------------------------------------------------------------
367 */
368
369static int mt753x_reg_read(struct mtk_eth_priv *priv, u32 reg, u32 *data)
370{
371 int ret, low_word, high_word;
372
Weijie Gao93eb7072023-07-19 17:17:41 +0800373 if (priv->sw == SW_MT7988) {
374 *data = mtk_gsw_read(priv, reg);
375 return 0;
376 }
377
Landen Chao532de8d2020-02-18 16:49:37 +0800378 /* Write page address */
379 ret = mtk_mii_write(priv, priv->mt753x_smi_addr, 0x1f, reg >> 6);
380 if (ret)
381 return ret;
382
383 /* Read low word */
384 low_word = mtk_mii_read(priv, priv->mt753x_smi_addr, (reg >> 2) & 0xf);
385 if (low_word < 0)
386 return low_word;
387
388 /* Read high word */
389 high_word = mtk_mii_read(priv, priv->mt753x_smi_addr, 0x10);
390 if (high_word < 0)
391 return high_word;
392
393 if (data)
394 *data = ((u32)high_word << 16) | (low_word & 0xffff);
395
396 return 0;
397}
398
399static int mt753x_reg_write(struct mtk_eth_priv *priv, u32 reg, u32 data)
400{
401 int ret;
402
Weijie Gao93eb7072023-07-19 17:17:41 +0800403 if (priv->sw == SW_MT7988) {
404 mtk_gsw_write(priv, reg, data);
405 return 0;
406 }
407
Landen Chao532de8d2020-02-18 16:49:37 +0800408 /* Write page address */
409 ret = mtk_mii_write(priv, priv->mt753x_smi_addr, 0x1f, reg >> 6);
410 if (ret)
411 return ret;
412
413 /* Write low word */
414 ret = mtk_mii_write(priv, priv->mt753x_smi_addr, (reg >> 2) & 0xf,
415 data & 0xffff);
416 if (ret)
417 return ret;
418
419 /* Write high word */
420 return mtk_mii_write(priv, priv->mt753x_smi_addr, 0x10, data >> 16);
421}
422
423static void mt753x_reg_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr,
424 u32 set)
425{
426 u32 val;
427
428 mt753x_reg_read(priv, reg, &val);
429 val &= ~clr;
430 val |= set;
431 mt753x_reg_write(priv, reg, val);
432}
433
434/* Indirect MDIO clause 22/45 access */
435static int mt7531_mii_rw(struct mtk_eth_priv *priv, int phy, int reg, u16 data,
436 u32 cmd, u32 st)
437{
438 ulong timeout;
439 u32 val, timeout_ms;
440 int ret = 0;
441
442 val = (st << MDIO_ST_S) |
443 ((cmd << MDIO_CMD_S) & MDIO_CMD_M) |
444 ((phy << MDIO_PHY_ADDR_S) & MDIO_PHY_ADDR_M) |
445 ((reg << MDIO_REG_ADDR_S) & MDIO_REG_ADDR_M);
446
447 if (cmd == MDIO_CMD_WRITE || cmd == MDIO_CMD_ADDR)
448 val |= data & MDIO_RW_DATA_M;
449
450 mt753x_reg_write(priv, MT7531_PHY_IAC, val | PHY_ACS_ST);
451
452 timeout_ms = 100;
453 timeout = get_timer(0);
454 while (1) {
455 mt753x_reg_read(priv, MT7531_PHY_IAC, &val);
456
457 if ((val & PHY_ACS_ST) == 0)
458 break;
459
460 if (get_timer(timeout) > timeout_ms)
461 return -ETIMEDOUT;
462 }
463
464 if (cmd == MDIO_CMD_READ || cmd == MDIO_CMD_READ_C45) {
465 mt753x_reg_read(priv, MT7531_PHY_IAC, &val);
466 ret = val & MDIO_RW_DATA_M;
467 }
468
469 return ret;
470}
471
472static int mt7531_mii_ind_read(struct mtk_eth_priv *priv, u8 phy, u8 reg)
473{
474 u8 phy_addr;
475
476 if (phy >= MT753X_NUM_PHYS)
477 return -EINVAL;
478
479 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, phy);
480
481 return mt7531_mii_rw(priv, phy_addr, reg, 0, MDIO_CMD_READ,
482 MDIO_ST_C22);
483}
484
485static int mt7531_mii_ind_write(struct mtk_eth_priv *priv, u8 phy, u8 reg,
486 u16 val)
487{
488 u8 phy_addr;
489
490 if (phy >= MT753X_NUM_PHYS)
491 return -EINVAL;
492
493 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, phy);
494
495 return mt7531_mii_rw(priv, phy_addr, reg, val, MDIO_CMD_WRITE,
496 MDIO_ST_C22);
497}
498
Weijie Gao159458d2023-07-19 17:17:07 +0800499static int mt7531_mmd_ind_read(struct mtk_eth_priv *priv, u8 addr, u8 devad,
500 u16 reg)
Landen Chao532de8d2020-02-18 16:49:37 +0800501{
502 u8 phy_addr;
503 int ret;
504
505 if (addr >= MT753X_NUM_PHYS)
506 return -EINVAL;
507
508 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, addr);
509
510 ret = mt7531_mii_rw(priv, phy_addr, devad, reg, MDIO_CMD_ADDR,
511 MDIO_ST_C45);
512 if (ret)
513 return ret;
514
515 return mt7531_mii_rw(priv, phy_addr, devad, 0, MDIO_CMD_READ_C45,
516 MDIO_ST_C45);
517}
518
519static int mt7531_mmd_ind_write(struct mtk_eth_priv *priv, u8 addr, u8 devad,
520 u16 reg, u16 val)
521{
522 u8 phy_addr;
523 int ret;
524
525 if (addr >= MT753X_NUM_PHYS)
526 return 0;
527
528 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, addr);
529
530 ret = mt7531_mii_rw(priv, phy_addr, devad, reg, MDIO_CMD_ADDR,
531 MDIO_ST_C45);
532 if (ret)
533 return ret;
534
535 return mt7531_mii_rw(priv, phy_addr, devad, val, MDIO_CMD_WRITE,
536 MDIO_ST_C45);
537}
538
Weijie Gao23f17162018-12-20 16:12:53 +0800539static int mtk_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
540{
541 struct mtk_eth_priv *priv = bus->priv;
542
543 if (devad < 0)
544 return priv->mii_read(priv, addr, reg);
545 else
546 return priv->mmd_read(priv, addr, devad, reg);
547}
548
549static int mtk_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
550 u16 val)
551{
552 struct mtk_eth_priv *priv = bus->priv;
553
554 if (devad < 0)
555 return priv->mii_write(priv, addr, reg, val);
556 else
557 return priv->mmd_write(priv, addr, devad, reg, val);
558}
559
560static int mtk_mdio_register(struct udevice *dev)
561{
562 struct mtk_eth_priv *priv = dev_get_priv(dev);
563 struct mii_dev *mdio_bus = mdio_alloc();
564 int ret;
565
566 if (!mdio_bus)
567 return -ENOMEM;
568
569 /* Assign MDIO access APIs according to the switch/phy */
570 switch (priv->sw) {
571 case SW_MT7530:
572 priv->mii_read = mtk_mii_read;
573 priv->mii_write = mtk_mii_write;
574 priv->mmd_read = mtk_mmd_ind_read;
575 priv->mmd_write = mtk_mmd_ind_write;
576 break;
Landen Chao532de8d2020-02-18 16:49:37 +0800577 case SW_MT7531:
Weijie Gao93eb7072023-07-19 17:17:41 +0800578 case SW_MT7988:
Landen Chao532de8d2020-02-18 16:49:37 +0800579 priv->mii_read = mt7531_mii_ind_read;
580 priv->mii_write = mt7531_mii_ind_write;
581 priv->mmd_read = mt7531_mmd_ind_read;
582 priv->mmd_write = mt7531_mmd_ind_write;
583 break;
Weijie Gao23f17162018-12-20 16:12:53 +0800584 default:
585 priv->mii_read = mtk_mii_read;
586 priv->mii_write = mtk_mii_write;
587 priv->mmd_read = mtk_mmd_read;
588 priv->mmd_write = mtk_mmd_write;
589 }
590
591 mdio_bus->read = mtk_mdio_read;
592 mdio_bus->write = mtk_mdio_write;
593 snprintf(mdio_bus->name, sizeof(mdio_bus->name), dev->name);
594
595 mdio_bus->priv = (void *)priv;
596
597 ret = mdio_register(mdio_bus);
598
599 if (ret)
600 return ret;
601
602 priv->mdio_bus = mdio_bus;
603
604 return 0;
605}
606
Landen Chao532de8d2020-02-18 16:49:37 +0800607static int mt753x_core_reg_read(struct mtk_eth_priv *priv, u32 reg)
Weijie Gao23f17162018-12-20 16:12:53 +0800608{
Landen Chao532de8d2020-02-18 16:49:37 +0800609 u8 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, 0);
Weijie Gao23f17162018-12-20 16:12:53 +0800610
Landen Chao532de8d2020-02-18 16:49:37 +0800611 return priv->mmd_read(priv, phy_addr, 0x1f, reg);
Weijie Gao23f17162018-12-20 16:12:53 +0800612}
613
Landen Chao532de8d2020-02-18 16:49:37 +0800614static void mt753x_core_reg_write(struct mtk_eth_priv *priv, u32 reg, u32 val)
Weijie Gao23f17162018-12-20 16:12:53 +0800615{
Landen Chao532de8d2020-02-18 16:49:37 +0800616 u8 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, 0);
Weijie Gao23f17162018-12-20 16:12:53 +0800617
Landen Chao532de8d2020-02-18 16:49:37 +0800618 priv->mmd_write(priv, phy_addr, 0x1f, reg, val);
Weijie Gao23f17162018-12-20 16:12:53 +0800619}
620
621static int mt7530_pad_clk_setup(struct mtk_eth_priv *priv, int mode)
622{
623 u32 ncpo1, ssc_delta;
624
625 switch (mode) {
626 case PHY_INTERFACE_MODE_RGMII:
627 ncpo1 = 0x0c80;
628 ssc_delta = 0x87;
629 break;
630 default:
631 printf("error: xMII mode %d not supported\n", mode);
632 return -EINVAL;
633 }
634
635 /* Disable MT7530 core clock */
Landen Chao532de8d2020-02-18 16:49:37 +0800636 mt753x_core_reg_write(priv, CORE_TRGMII_GSW_CLK_CG, 0);
Weijie Gao23f17162018-12-20 16:12:53 +0800637
638 /* Disable MT7530 PLL */
Landen Chao532de8d2020-02-18 16:49:37 +0800639 mt753x_core_reg_write(priv, CORE_GSWPLL_GRP1,
Weijie Gao23f17162018-12-20 16:12:53 +0800640 (2 << RG_GSWPLL_POSDIV_200M_S) |
641 (32 << RG_GSWPLL_FBKDIV_200M_S));
642
643 /* For MT7530 core clock = 500Mhz */
Landen Chao532de8d2020-02-18 16:49:37 +0800644 mt753x_core_reg_write(priv, CORE_GSWPLL_GRP2,
Weijie Gao23f17162018-12-20 16:12:53 +0800645 (1 << RG_GSWPLL_POSDIV_500M_S) |
646 (25 << RG_GSWPLL_FBKDIV_500M_S));
647
648 /* Enable MT7530 PLL */
Landen Chao532de8d2020-02-18 16:49:37 +0800649 mt753x_core_reg_write(priv, CORE_GSWPLL_GRP1,
Weijie Gao23f17162018-12-20 16:12:53 +0800650 (2 << RG_GSWPLL_POSDIV_200M_S) |
651 (32 << RG_GSWPLL_FBKDIV_200M_S) |
652 RG_GSWPLL_EN_PRE);
653
654 udelay(20);
655
Landen Chao532de8d2020-02-18 16:49:37 +0800656 mt753x_core_reg_write(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
Weijie Gao23f17162018-12-20 16:12:53 +0800657
658 /* Setup the MT7530 TRGMII Tx Clock */
Landen Chao532de8d2020-02-18 16:49:37 +0800659 mt753x_core_reg_write(priv, CORE_PLL_GROUP5, ncpo1);
660 mt753x_core_reg_write(priv, CORE_PLL_GROUP6, 0);
661 mt753x_core_reg_write(priv, CORE_PLL_GROUP10, ssc_delta);
662 mt753x_core_reg_write(priv, CORE_PLL_GROUP11, ssc_delta);
663 mt753x_core_reg_write(priv, CORE_PLL_GROUP4, RG_SYSPLL_DDSFBK_EN |
Weijie Gao23f17162018-12-20 16:12:53 +0800664 RG_SYSPLL_BIAS_EN | RG_SYSPLL_BIAS_LPF_EN);
665
Landen Chao532de8d2020-02-18 16:49:37 +0800666 mt753x_core_reg_write(priv, CORE_PLL_GROUP2,
Weijie Gao23f17162018-12-20 16:12:53 +0800667 RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
668 (1 << RG_SYSPLL_POSDIV_S));
669
Landen Chao532de8d2020-02-18 16:49:37 +0800670 mt753x_core_reg_write(priv, CORE_PLL_GROUP7,
Weijie Gao23f17162018-12-20 16:12:53 +0800671 RG_LCDDS_PCW_NCPO_CHG | (3 << RG_LCCDS_C_S) |
672 RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
673
674 /* Enable MT7530 core clock */
Landen Chao532de8d2020-02-18 16:49:37 +0800675 mt753x_core_reg_write(priv, CORE_TRGMII_GSW_CLK_CG,
Weijie Gao23f17162018-12-20 16:12:53 +0800676 REG_GSWCK_EN | REG_TRGMIICK_EN);
677
678 return 0;
679}
680
Weijie Gaoc73d3872023-07-19 17:16:54 +0800681static void mt7530_mac_control(struct mtk_eth_priv *priv, bool enable)
682{
683 u32 pmcr = FORCE_MODE;
684
685 if (enable)
686 pmcr = priv->mt753x_pmcr;
687
688 mt753x_reg_write(priv, PMCR_REG(6), pmcr);
689}
690
Weijie Gao23f17162018-12-20 16:12:53 +0800691static int mt7530_setup(struct mtk_eth_priv *priv)
692{
693 u16 phy_addr, phy_val;
Weijie Gaoad80d482022-05-20 11:23:42 +0800694 u32 val, txdrv;
Weijie Gao23f17162018-12-20 16:12:53 +0800695 int i;
696
Weijie Gao62596722022-09-09 19:59:21 +0800697 if (!MTK_HAS_CAPS(priv->soc->caps, MTK_TRGMII_MT7621_CLK)) {
Weijie Gaoad80d482022-05-20 11:23:42 +0800698 /* Select 250MHz clk for RGMII mode */
699 mtk_ethsys_rmw(priv, ETHSYS_CLKCFG0_REG,
700 ETHSYS_TRGMII_CLK_SEL362_5, 0);
701
702 txdrv = 8;
703 } else {
704 txdrv = 4;
705 }
Weijie Gao23f17162018-12-20 16:12:53 +0800706
Landen Chao532de8d2020-02-18 16:49:37 +0800707 /* Modify HWTRAP first to allow direct access to internal PHYs */
708 mt753x_reg_read(priv, HWTRAP_REG, &val);
709 val |= CHG_TRAP;
710 val &= ~C_MDIO_BPS;
711 mt753x_reg_write(priv, MHWTRAP_REG, val);
712
713 /* Calculate the phy base address */
714 val = ((val & SMI_ADDR_M) >> SMI_ADDR_S) << 3;
715 priv->mt753x_phy_base = (val | 0x7) + 1;
716
717 /* Turn off PHYs */
718 for (i = 0; i < MT753X_NUM_PHYS; i++) {
719 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, i);
720 phy_val = priv->mii_read(priv, phy_addr, MII_BMCR);
721 phy_val |= BMCR_PDOWN;
722 priv->mii_write(priv, phy_addr, MII_BMCR, phy_val);
723 }
724
725 /* Force MAC link down before reset */
726 mt753x_reg_write(priv, PMCR_REG(5), FORCE_MODE);
727 mt753x_reg_write(priv, PMCR_REG(6), FORCE_MODE);
728
729 /* MT7530 reset */
730 mt753x_reg_write(priv, SYS_CTRL_REG, SW_SYS_RST | SW_REG_RST);
731 udelay(100);
732
733 val = (IPG_96BIT_WITH_SHORT_IPG << IPG_CFG_S) |
734 MAC_MODE | FORCE_MODE |
735 MAC_TX_EN | MAC_RX_EN |
736 BKOFF_EN | BACKPR_EN |
737 (SPEED_1000M << FORCE_SPD_S) |
738 FORCE_DPX | FORCE_LINK;
739
740 /* MT7530 Port6: Forced 1000M/FD, FC disabled */
Weijie Gaoc73d3872023-07-19 17:16:54 +0800741 priv->mt753x_pmcr = val;
Landen Chao532de8d2020-02-18 16:49:37 +0800742
743 /* MT7530 Port5: Forced link down */
744 mt753x_reg_write(priv, PMCR_REG(5), FORCE_MODE);
745
Weijie Gaoc73d3872023-07-19 17:16:54 +0800746 /* Keep MAC link down before starting eth */
747 mt753x_reg_write(priv, PMCR_REG(6), FORCE_MODE);
748
Landen Chao532de8d2020-02-18 16:49:37 +0800749 /* MT7530 Port6: Set to RGMII */
750 mt753x_reg_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_M, P6_INTF_MODE_RGMII);
751
752 /* Hardware Trap: Enable Port6, Disable Port5 */
753 mt753x_reg_read(priv, HWTRAP_REG, &val);
754 val |= CHG_TRAP | LOOPDET_DIS | P5_INTF_DIS |
755 (P5_INTF_SEL_GMAC5 << P5_INTF_SEL_S) |
756 (P5_INTF_MODE_RGMII << P5_INTF_MODE_S);
757 val &= ~(C_MDIO_BPS | P6_INTF_DIS);
758 mt753x_reg_write(priv, MHWTRAP_REG, val);
759
760 /* Setup switch core pll */
761 mt7530_pad_clk_setup(priv, priv->phy_interface);
762
763 /* Lower Tx Driving for TRGMII path */
764 for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
765 mt753x_reg_write(priv, MT7530_TRGMII_TD_ODT(i),
Weijie Gaoad80d482022-05-20 11:23:42 +0800766 (txdrv << TD_DM_DRVP_S) |
767 (txdrv << TD_DM_DRVN_S));
Landen Chao532de8d2020-02-18 16:49:37 +0800768
769 for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
770 mt753x_reg_rmw(priv, MT7530_TRGMII_RD(i), RD_TAP_M, 16);
771
772 /* Turn on PHYs */
773 for (i = 0; i < MT753X_NUM_PHYS; i++) {
774 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, i);
775 phy_val = priv->mii_read(priv, phy_addr, MII_BMCR);
776 phy_val &= ~BMCR_PDOWN;
777 priv->mii_write(priv, phy_addr, MII_BMCR, phy_val);
778 }
779
780 return 0;
781}
782
783static void mt7531_core_pll_setup(struct mtk_eth_priv *priv, int mcm)
784{
785 /* Step 1 : Disable MT7531 COREPLL */
786 mt753x_reg_rmw(priv, MT7531_PLLGP_EN, EN_COREPLL, 0);
787
788 /* Step 2: switch to XTAL output */
789 mt753x_reg_rmw(priv, MT7531_PLLGP_EN, SW_CLKSW, SW_CLKSW);
790
791 mt753x_reg_rmw(priv, MT7531_PLLGP_CR0, RG_COREPLL_EN, 0);
792
793 /* Step 3: disable PLLGP and enable program PLLGP */
794 mt753x_reg_rmw(priv, MT7531_PLLGP_EN, SW_PLLGP, SW_PLLGP);
795
796 /* Step 4: program COREPLL output frequency to 500MHz */
797 mt753x_reg_rmw(priv, MT7531_PLLGP_CR0, RG_COREPLL_POSDIV_M,
798 2 << RG_COREPLL_POSDIV_S);
799 udelay(25);
800
801 /* Currently, support XTAL 25Mhz only */
802 mt753x_reg_rmw(priv, MT7531_PLLGP_CR0, RG_COREPLL_SDM_PCW_M,
803 0x140000 << RG_COREPLL_SDM_PCW_S);
804
805 /* Set feedback divide ratio update signal to high */
806 mt753x_reg_rmw(priv, MT7531_PLLGP_CR0, RG_COREPLL_SDM_PCW_CHG,
807 RG_COREPLL_SDM_PCW_CHG);
808
809 /* Wait for at least 16 XTAL clocks */
810 udelay(10);
811
812 /* Step 5: set feedback divide ratio update signal to low */
813 mt753x_reg_rmw(priv, MT7531_PLLGP_CR0, RG_COREPLL_SDM_PCW_CHG, 0);
814
815 /* add enable 325M clock for SGMII */
816 mt753x_reg_write(priv, MT7531_ANA_PLLGP_CR5, 0xad0000);
817
818 /* add enable 250SSC clock for RGMII */
819 mt753x_reg_write(priv, MT7531_ANA_PLLGP_CR2, 0x4f40000);
820
821 /*Step 6: Enable MT7531 PLL */
822 mt753x_reg_rmw(priv, MT7531_PLLGP_CR0, RG_COREPLL_EN, RG_COREPLL_EN);
823
824 mt753x_reg_rmw(priv, MT7531_PLLGP_EN, EN_COREPLL, EN_COREPLL);
825
826 udelay(25);
827}
828
829static int mt7531_port_sgmii_init(struct mtk_eth_priv *priv,
830 u32 port)
831{
832 if (port != 5 && port != 6) {
833 printf("mt7531: port %d is not a SGMII port\n", port);
834 return -EINVAL;
835 }
836
837 /* Set SGMII GEN2 speed(2.5G) */
838 mt753x_reg_rmw(priv, MT7531_PHYA_CTRL_SIGNAL3(port),
839 SGMSYS_SPEED_2500, SGMSYS_SPEED_2500);
840
841 /* Disable SGMII AN */
842 mt753x_reg_rmw(priv, MT7531_PCS_CONTROL_1(port),
843 SGMII_AN_ENABLE, 0);
844
845 /* SGMII force mode setting */
846 mt753x_reg_write(priv, MT7531_SGMII_MODE(port), SGMII_FORCE_MODE);
847
848 /* Release PHYA power down state */
849 mt753x_reg_rmw(priv, MT7531_QPHY_PWR_STATE_CTRL(port),
850 SGMII_PHYA_PWD, 0);
851
852 return 0;
853}
854
855static int mt7531_port_rgmii_init(struct mtk_eth_priv *priv, u32 port)
856{
857 u32 val;
858
859 if (port != 5) {
860 printf("error: RGMII mode is not available for port %d\n",
861 port);
862 return -EINVAL;
863 }
864
865 mt753x_reg_read(priv, MT7531_CLKGEN_CTRL, &val);
866 val |= GP_CLK_EN;
867 val &= ~GP_MODE_M;
868 val |= GP_MODE_RGMII << GP_MODE_S;
869 val |= TXCLK_NO_REVERSE;
870 val |= RXCLK_NO_DELAY;
871 val &= ~CLK_SKEW_IN_M;
872 val |= CLK_SKEW_IN_NO_CHANGE << CLK_SKEW_IN_S;
873 val &= ~CLK_SKEW_OUT_M;
874 val |= CLK_SKEW_OUT_NO_CHANGE << CLK_SKEW_OUT_S;
875 mt753x_reg_write(priv, MT7531_CLKGEN_CTRL, val);
876
877 return 0;
878}
879
880static void mt7531_phy_setting(struct mtk_eth_priv *priv)
881{
882 int i;
883 u32 val;
884
885 for (i = 0; i < MT753X_NUM_PHYS; i++) {
886 /* Enable HW auto downshift */
887 priv->mii_write(priv, i, 0x1f, 0x1);
888 val = priv->mii_read(priv, i, PHY_EXT_REG_14);
889 val |= PHY_EN_DOWN_SHFIT;
890 priv->mii_write(priv, i, PHY_EXT_REG_14, val);
891
892 /* PHY link down power saving enable */
893 val = priv->mii_read(priv, i, PHY_EXT_REG_17);
894 val |= PHY_LINKDOWN_POWER_SAVING_EN;
895 priv->mii_write(priv, i, PHY_EXT_REG_17, val);
896
897 val = priv->mmd_read(priv, i, 0x1e, PHY_DEV1E_REG_0C6);
898 val &= ~PHY_POWER_SAVING_M;
899 val |= PHY_POWER_SAVING_TX << PHY_POWER_SAVING_S;
900 priv->mmd_write(priv, i, 0x1e, PHY_DEV1E_REG_0C6, val);
901 }
902}
903
Weijie Gaoc73d3872023-07-19 17:16:54 +0800904static void mt7531_mac_control(struct mtk_eth_priv *priv, bool enable)
905{
906 u32 pmcr = FORCE_MODE_LNK;
907
908 if (enable)
909 pmcr = priv->mt753x_pmcr;
910
911 mt753x_reg_write(priv, PMCR_REG(5), pmcr);
912 mt753x_reg_write(priv, PMCR_REG(6), pmcr);
913}
914
Landen Chao532de8d2020-02-18 16:49:37 +0800915static int mt7531_setup(struct mtk_eth_priv *priv)
916{
917 u16 phy_addr, phy_val;
918 u32 val;
919 u32 pmcr;
920 u32 port5_sgmii;
921 int i;
922
923 priv->mt753x_phy_base = (priv->mt753x_smi_addr + 1) &
924 MT753X_SMI_ADDR_MASK;
925
926 /* Turn off PHYs */
927 for (i = 0; i < MT753X_NUM_PHYS; i++) {
928 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, i);
929 phy_val = priv->mii_read(priv, phy_addr, MII_BMCR);
930 phy_val |= BMCR_PDOWN;
931 priv->mii_write(priv, phy_addr, MII_BMCR, phy_val);
932 }
933
934 /* Force MAC link down before reset */
935 mt753x_reg_write(priv, PMCR_REG(5), FORCE_MODE_LNK);
936 mt753x_reg_write(priv, PMCR_REG(6), FORCE_MODE_LNK);
937
938 /* Switch soft reset */
939 mt753x_reg_write(priv, SYS_CTRL_REG, SW_SYS_RST | SW_REG_RST);
940 udelay(100);
941
942 /* Enable MDC input Schmitt Trigger */
943 mt753x_reg_rmw(priv, MT7531_SMT0_IOLB, SMT_IOLB_5_SMI_MDC_EN,
944 SMT_IOLB_5_SMI_MDC_EN);
945
946 mt7531_core_pll_setup(priv, priv->mcm);
947
948 mt753x_reg_read(priv, MT7531_TOP_SIG_SR, &val);
949 port5_sgmii = !!(val & PAD_DUAL_SGMII_EN);
950
951 /* port5 support either RGMII or SGMII, port6 only support SGMII. */
952 switch (priv->phy_interface) {
953 case PHY_INTERFACE_MODE_RGMII:
954 if (!port5_sgmii)
955 mt7531_port_rgmii_init(priv, 5);
956 break;
Weijie Gaobd70f3c2023-07-19 17:17:13 +0800957 case PHY_INTERFACE_MODE_2500BASEX:
Landen Chao532de8d2020-02-18 16:49:37 +0800958 mt7531_port_sgmii_init(priv, 6);
959 if (port5_sgmii)
960 mt7531_port_sgmii_init(priv, 5);
961 break;
962 default:
963 break;
964 }
965
966 pmcr = MT7531_FORCE_MODE |
967 (IPG_96BIT_WITH_SHORT_IPG << IPG_CFG_S) |
968 MAC_MODE | MAC_TX_EN | MAC_RX_EN |
969 BKOFF_EN | BACKPR_EN |
970 FORCE_RX_FC | FORCE_TX_FC |
971 (SPEED_1000M << FORCE_SPD_S) | FORCE_DPX |
972 FORCE_LINK;
973
Weijie Gaoc73d3872023-07-19 17:16:54 +0800974 priv->mt753x_pmcr = pmcr;
975
976 /* Keep MAC link down before starting eth */
977 mt753x_reg_write(priv, PMCR_REG(5), FORCE_MODE_LNK);
978 mt753x_reg_write(priv, PMCR_REG(6), FORCE_MODE_LNK);
Landen Chao532de8d2020-02-18 16:49:37 +0800979
980 /* Turn on PHYs */
981 for (i = 0; i < MT753X_NUM_PHYS; i++) {
982 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, i);
983 phy_val = priv->mii_read(priv, phy_addr, MII_BMCR);
984 phy_val &= ~BMCR_PDOWN;
985 priv->mii_write(priv, phy_addr, MII_BMCR, phy_val);
986 }
987
988 mt7531_phy_setting(priv);
989
990 /* Enable Internal PHYs */
991 val = mt753x_core_reg_read(priv, CORE_PLL_GROUP4);
992 val |= MT7531_BYPASS_MODE;
993 val &= ~MT7531_POWER_ON_OFF;
994 mt753x_core_reg_write(priv, CORE_PLL_GROUP4, val);
995
996 return 0;
997}
998
Weijie Gao93eb7072023-07-19 17:17:41 +0800999static void mt7988_phy_setting(struct mtk_eth_priv *priv)
1000{
1001 u16 val;
1002 u32 i;
1003
1004 for (i = 0; i < MT753X_NUM_PHYS; i++) {
1005 /* Enable HW auto downshift */
1006 priv->mii_write(priv, i, 0x1f, 0x1);
1007 val = priv->mii_read(priv, i, PHY_EXT_REG_14);
1008 val |= PHY_EN_DOWN_SHFIT;
1009 priv->mii_write(priv, i, PHY_EXT_REG_14, val);
1010
1011 /* PHY link down power saving enable */
1012 val = priv->mii_read(priv, i, PHY_EXT_REG_17);
1013 val |= PHY_LINKDOWN_POWER_SAVING_EN;
1014 priv->mii_write(priv, i, PHY_EXT_REG_17, val);
1015 }
1016}
1017
1018static void mt7988_mac_control(struct mtk_eth_priv *priv, bool enable)
1019{
1020 u32 pmcr = FORCE_MODE_LNK;
1021
1022 if (enable)
1023 pmcr = priv->mt753x_pmcr;
1024
1025 mt753x_reg_write(priv, PMCR_REG(6), pmcr);
1026}
1027
1028static int mt7988_setup(struct mtk_eth_priv *priv)
1029{
1030 u16 phy_addr, phy_val;
1031 u32 pmcr;
1032 int i;
1033
1034 priv->gsw_base = regmap_get_range(priv->ethsys_regmap, 0) + GSW_BASE;
1035
1036 priv->mt753x_phy_base = (priv->mt753x_smi_addr + 1) &
1037 MT753X_SMI_ADDR_MASK;
1038
1039 /* Turn off PHYs */
1040 for (i = 0; i < MT753X_NUM_PHYS; i++) {
1041 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, i);
1042 phy_val = priv->mii_read(priv, phy_addr, MII_BMCR);
1043 phy_val |= BMCR_PDOWN;
1044 priv->mii_write(priv, phy_addr, MII_BMCR, phy_val);
1045 }
1046
1047 switch (priv->phy_interface) {
1048 case PHY_INTERFACE_MODE_USXGMII:
1049 /* Use CPU bridge instead of actual USXGMII path */
1050
1051 /* Set GDM1 no drop */
1052 mtk_fe_rmw(priv, PSE_NO_DROP_CFG_REG, 0, PSE_NO_DROP_GDM1);
1053
1054 /* Enable GDM1 to GSW CPU bridge */
1055 mtk_gmac_rmw(priv, GMAC_MAC_MISC_REG, 0, BIT(0));
1056
1057 /* XGMAC force link up */
1058 mtk_gmac_rmw(priv, GMAC_XGMAC_STS_REG, 0, P1_XGMAC_FORCE_LINK);
1059
1060 /* Setup GSW CPU bridge IPG */
1061 mtk_gmac_rmw(priv, GMAC_GSW_CFG_REG, GSWTX_IPG_M | GSWRX_IPG_M,
1062 (0xB << GSWTX_IPG_S) | (0xB << GSWRX_IPG_S));
1063 break;
1064 default:
1065 printf("Error: MT7988 GSW does not support %s interface\n",
1066 phy_string_for_interface(priv->phy_interface));
1067 break;
1068 }
1069
1070 pmcr = MT7988_FORCE_MODE |
1071 (IPG_96BIT_WITH_SHORT_IPG << IPG_CFG_S) |
1072 MAC_MODE | MAC_TX_EN | MAC_RX_EN |
1073 BKOFF_EN | BACKPR_EN |
1074 FORCE_RX_FC | FORCE_TX_FC |
1075 (SPEED_1000M << FORCE_SPD_S) | FORCE_DPX |
1076 FORCE_LINK;
1077
1078 priv->mt753x_pmcr = pmcr;
1079
1080 /* Keep MAC link down before starting eth */
1081 mt753x_reg_write(priv, PMCR_REG(6), FORCE_MODE_LNK);
1082
1083 /* Turn on PHYs */
1084 for (i = 0; i < MT753X_NUM_PHYS; i++) {
1085 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, i);
1086 phy_val = priv->mii_read(priv, phy_addr, MII_BMCR);
1087 phy_val &= ~BMCR_PDOWN;
1088 priv->mii_write(priv, phy_addr, MII_BMCR, phy_val);
1089 }
1090
1091 mt7988_phy_setting(priv);
1092
1093 return 0;
1094}
1095
Weijie Gao159458d2023-07-19 17:17:07 +08001096static int mt753x_switch_init(struct mtk_eth_priv *priv)
Landen Chao532de8d2020-02-18 16:49:37 +08001097{
1098 int ret;
1099 int i;
1100
Weijie Gao23f17162018-12-20 16:12:53 +08001101 /* Global reset switch */
1102 if (priv->mcm) {
1103 reset_assert(&priv->rst_mcm);
1104 udelay(1000);
1105 reset_deassert(&priv->rst_mcm);
Weijie Gaoc41a0582023-07-19 17:16:59 +08001106 mdelay(priv->mt753x_reset_wait_time);
Weijie Gao23f17162018-12-20 16:12:53 +08001107 } else if (dm_gpio_is_valid(&priv->rst_gpio)) {
1108 dm_gpio_set_value(&priv->rst_gpio, 0);
1109 udelay(1000);
1110 dm_gpio_set_value(&priv->rst_gpio, 1);
Weijie Gaoc41a0582023-07-19 17:16:59 +08001111 mdelay(priv->mt753x_reset_wait_time);
Weijie Gao23f17162018-12-20 16:12:53 +08001112 }
1113
Landen Chao532de8d2020-02-18 16:49:37 +08001114 ret = priv->switch_init(priv);
1115 if (ret)
1116 return ret;
Weijie Gao23f17162018-12-20 16:12:53 +08001117
1118 /* Set port isolation */
Landen Chao532de8d2020-02-18 16:49:37 +08001119 for (i = 0; i < MT753X_NUM_PORTS; i++) {
Weijie Gao23f17162018-12-20 16:12:53 +08001120 /* Set port matrix mode */
1121 if (i != 6)
Landen Chao532de8d2020-02-18 16:49:37 +08001122 mt753x_reg_write(priv, PCR_REG(i),
Weijie Gao23f17162018-12-20 16:12:53 +08001123 (0x40 << PORT_MATRIX_S));
1124 else
Landen Chao532de8d2020-02-18 16:49:37 +08001125 mt753x_reg_write(priv, PCR_REG(i),
Weijie Gao23f17162018-12-20 16:12:53 +08001126 (0x3f << PORT_MATRIX_S));
1127
1128 /* Set port mode to user port */
Landen Chao532de8d2020-02-18 16:49:37 +08001129 mt753x_reg_write(priv, PVC_REG(i),
Weijie Gao23f17162018-12-20 16:12:53 +08001130 (0x8100 << STAG_VPID_S) |
1131 (VLAN_ATTR_USER << VLAN_ATTR_S));
1132 }
1133
1134 return 0;
1135}
1136
Weijie Gaoba026eb2023-07-19 17:17:31 +08001137static void mtk_xphy_link_adjust(struct mtk_eth_priv *priv)
1138{
1139 u16 lcl_adv = 0, rmt_adv = 0;
1140 u8 flowctrl;
1141 u32 mcr;
1142
1143 mcr = mtk_gmac_read(priv, XGMAC_PORT_MCR(priv->gmac_id));
1144 mcr &= ~(XGMAC_FORCE_TX_FC | XGMAC_FORCE_RX_FC);
1145
1146 if (priv->phydev->duplex) {
1147 if (priv->phydev->pause)
1148 rmt_adv = LPA_PAUSE_CAP;
1149 if (priv->phydev->asym_pause)
1150 rmt_adv |= LPA_PAUSE_ASYM;
1151
1152 if (priv->phydev->advertising & ADVERTISED_Pause)
1153 lcl_adv |= ADVERTISE_PAUSE_CAP;
1154 if (priv->phydev->advertising & ADVERTISED_Asym_Pause)
1155 lcl_adv |= ADVERTISE_PAUSE_ASYM;
1156
1157 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
1158
1159 if (flowctrl & FLOW_CTRL_TX)
1160 mcr |= XGMAC_FORCE_TX_FC;
1161 if (flowctrl & FLOW_CTRL_RX)
1162 mcr |= XGMAC_FORCE_RX_FC;
1163
1164 debug("rx pause %s, tx pause %s\n",
1165 flowctrl & FLOW_CTRL_RX ? "enabled" : "disabled",
1166 flowctrl & FLOW_CTRL_TX ? "enabled" : "disabled");
1167 }
1168
1169 mcr &= ~(XGMAC_TRX_DISABLE);
1170 mtk_gmac_write(priv, XGMAC_PORT_MCR(priv->gmac_id), mcr);
1171}
1172
Weijie Gao23f17162018-12-20 16:12:53 +08001173static void mtk_phy_link_adjust(struct mtk_eth_priv *priv)
1174{
1175 u16 lcl_adv = 0, rmt_adv = 0;
1176 u8 flowctrl;
1177 u32 mcr;
1178
Landen Chao532de8d2020-02-18 16:49:37 +08001179 mcr = (IPG_96BIT_WITH_SHORT_IPG << IPG_CFG_S) |
Weijie Gao23f17162018-12-20 16:12:53 +08001180 (MAC_RX_PKT_LEN_1536 << MAC_RX_PKT_LEN_S) |
1181 MAC_MODE | FORCE_MODE |
1182 MAC_TX_EN | MAC_RX_EN |
Weijie Gaobd70f3c2023-07-19 17:17:13 +08001183 DEL_RXFIFO_CLR |
Weijie Gao23f17162018-12-20 16:12:53 +08001184 BKOFF_EN | BACKPR_EN;
1185
1186 switch (priv->phydev->speed) {
1187 case SPEED_10:
1188 mcr |= (SPEED_10M << FORCE_SPD_S);
1189 break;
1190 case SPEED_100:
1191 mcr |= (SPEED_100M << FORCE_SPD_S);
1192 break;
1193 case SPEED_1000:
Weijie Gaobd70f3c2023-07-19 17:17:13 +08001194 case SPEED_2500:
Weijie Gao23f17162018-12-20 16:12:53 +08001195 mcr |= (SPEED_1000M << FORCE_SPD_S);
1196 break;
1197 };
1198
1199 if (priv->phydev->link)
1200 mcr |= FORCE_LINK;
1201
1202 if (priv->phydev->duplex) {
1203 mcr |= FORCE_DPX;
1204
1205 if (priv->phydev->pause)
1206 rmt_adv = LPA_PAUSE_CAP;
1207 if (priv->phydev->asym_pause)
1208 rmt_adv |= LPA_PAUSE_ASYM;
1209
1210 if (priv->phydev->advertising & ADVERTISED_Pause)
1211 lcl_adv |= ADVERTISE_PAUSE_CAP;
1212 if (priv->phydev->advertising & ADVERTISED_Asym_Pause)
1213 lcl_adv |= ADVERTISE_PAUSE_ASYM;
1214
1215 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
1216
1217 if (flowctrl & FLOW_CTRL_TX)
1218 mcr |= FORCE_TX_FC;
1219 if (flowctrl & FLOW_CTRL_RX)
1220 mcr |= FORCE_RX_FC;
1221
1222 debug("rx pause %s, tx pause %s\n",
1223 flowctrl & FLOW_CTRL_RX ? "enabled" : "disabled",
1224 flowctrl & FLOW_CTRL_TX ? "enabled" : "disabled");
1225 }
1226
1227 mtk_gmac_write(priv, GMAC_PORT_MCR(priv->gmac_id), mcr);
1228}
1229
1230static int mtk_phy_start(struct mtk_eth_priv *priv)
1231{
1232 struct phy_device *phydev = priv->phydev;
1233 int ret;
1234
1235 ret = phy_startup(phydev);
1236
1237 if (ret) {
1238 debug("Could not initialize PHY %s\n", phydev->dev->name);
1239 return ret;
1240 }
1241
1242 if (!phydev->link) {
1243 debug("%s: link down.\n", phydev->dev->name);
1244 return 0;
1245 }
1246
Weijie Gaoba026eb2023-07-19 17:17:31 +08001247 if (!priv->force_mode) {
1248 if (priv->phy_interface == PHY_INTERFACE_MODE_USXGMII)
1249 mtk_xphy_link_adjust(priv);
1250 else
1251 mtk_phy_link_adjust(priv);
1252 }
Weijie Gao23f17162018-12-20 16:12:53 +08001253
1254 debug("Speed: %d, %s duplex%s\n", phydev->speed,
1255 (phydev->duplex) ? "full" : "half",
1256 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
1257
1258 return 0;
1259}
1260
1261static int mtk_phy_probe(struct udevice *dev)
1262{
1263 struct mtk_eth_priv *priv = dev_get_priv(dev);
1264 struct phy_device *phydev;
1265
1266 phydev = phy_connect(priv->mdio_bus, priv->phy_addr, dev,
1267 priv->phy_interface);
1268 if (!phydev)
1269 return -ENODEV;
1270
1271 phydev->supported &= PHY_GBIT_FEATURES;
1272 phydev->advertising = phydev->supported;
1273
1274 priv->phydev = phydev;
1275 phy_config(phydev);
1276
1277 return 0;
1278}
1279
Weijie Gaobd70f3c2023-07-19 17:17:13 +08001280static void mtk_sgmii_an_init(struct mtk_eth_priv *priv)
1281{
1282 /* Set SGMII GEN1 speed(1G) */
1283 clrsetbits_le32(priv->sgmii_base + priv->soc->ana_rgc3,
1284 SGMSYS_SPEED_2500, 0);
1285
1286 /* Enable SGMII AN */
1287 setbits_le32(priv->sgmii_base + SGMSYS_PCS_CONTROL_1,
1288 SGMII_AN_ENABLE);
1289
1290 /* SGMII AN mode setting */
1291 writel(SGMII_AN_MODE, priv->sgmii_base + SGMSYS_SGMII_MODE);
1292
1293 /* SGMII PN SWAP setting */
1294 if (priv->pn_swap) {
1295 setbits_le32(priv->sgmii_base + SGMSYS_QPHY_WRAP_CTRL,
1296 SGMII_PN_SWAP_TX_RX);
1297 }
1298
1299 /* Release PHYA power down state */
1300 clrsetbits_le32(priv->sgmii_base + SGMSYS_QPHY_PWR_STATE_CTRL,
1301 SGMII_PHYA_PWD, 0);
1302}
1303
1304static void mtk_sgmii_force_init(struct mtk_eth_priv *priv)
MarkLeeb4ef49a2020-01-21 19:31:57 +08001305{
1306 /* Set SGMII GEN2 speed(2.5G) */
Weijie Gao62596722022-09-09 19:59:21 +08001307 setbits_le32(priv->sgmii_base + priv->soc->ana_rgc3,
1308 SGMSYS_SPEED_2500);
MarkLeeb4ef49a2020-01-21 19:31:57 +08001309
1310 /* Disable SGMII AN */
1311 clrsetbits_le32(priv->sgmii_base + SGMSYS_PCS_CONTROL_1,
1312 SGMII_AN_ENABLE, 0);
1313
1314 /* SGMII force mode setting */
1315 writel(SGMII_FORCE_MODE, priv->sgmii_base + SGMSYS_SGMII_MODE);
1316
Weijie Gao29a48bf2022-09-09 19:59:28 +08001317 /* SGMII PN SWAP setting */
1318 if (priv->pn_swap) {
1319 setbits_le32(priv->sgmii_base + SGMSYS_QPHY_WRAP_CTRL,
1320 SGMII_PN_SWAP_TX_RX);
1321 }
1322
MarkLeeb4ef49a2020-01-21 19:31:57 +08001323 /* Release PHYA power down state */
1324 clrsetbits_le32(priv->sgmii_base + SGMSYS_QPHY_PWR_STATE_CTRL,
1325 SGMII_PHYA_PWD, 0);
1326}
1327
Weijie Gaoba026eb2023-07-19 17:17:31 +08001328static void mtk_xfi_pll_enable(struct mtk_eth_priv *priv)
1329{
1330 u32 val = 0;
1331
1332 /* Add software workaround for USXGMII PLL TCL issue */
1333 regmap_write(priv->xfi_pll_regmap, XFI_PLL_ANA_GLB8,
1334 RG_XFI_PLL_ANA_SWWA);
1335
1336 regmap_read(priv->xfi_pll_regmap, XFI_PLL_DIG_GLB8, &val);
1337 val |= RG_XFI_PLL_EN;
1338 regmap_write(priv->xfi_pll_regmap, XFI_PLL_DIG_GLB8, val);
1339}
1340
1341static void mtk_usxgmii_reset(struct mtk_eth_priv *priv)
1342{
1343 switch (priv->gmac_id) {
1344 case 1:
1345 regmap_write(priv->toprgu_regmap, 0xFC, 0x0000A004);
1346 regmap_write(priv->toprgu_regmap, 0x18, 0x88F0A004);
1347 regmap_write(priv->toprgu_regmap, 0xFC, 0x00000000);
1348 regmap_write(priv->toprgu_regmap, 0x18, 0x88F00000);
1349 regmap_write(priv->toprgu_regmap, 0x18, 0x00F00000);
1350 break;
1351 case 2:
1352 regmap_write(priv->toprgu_regmap, 0xFC, 0x00005002);
1353 regmap_write(priv->toprgu_regmap, 0x18, 0x88F05002);
1354 regmap_write(priv->toprgu_regmap, 0xFC, 0x00000000);
1355 regmap_write(priv->toprgu_regmap, 0x18, 0x88F00000);
1356 regmap_write(priv->toprgu_regmap, 0x18, 0x00F00000);
1357 break;
1358 }
1359
1360 mdelay(10);
1361}
1362
1363static void mtk_usxgmii_setup_phya_an_10000(struct mtk_eth_priv *priv)
1364{
1365 regmap_write(priv->usxgmii_regmap, 0x810, 0x000FFE6D);
1366 regmap_write(priv->usxgmii_regmap, 0x818, 0x07B1EC7B);
1367 regmap_write(priv->usxgmii_regmap, 0x80C, 0x30000000);
1368 ndelay(1020);
1369 regmap_write(priv->usxgmii_regmap, 0x80C, 0x10000000);
1370 ndelay(1020);
1371 regmap_write(priv->usxgmii_regmap, 0x80C, 0x00000000);
1372
1373 regmap_write(priv->xfi_pextp_regmap, 0x9024, 0x00C9071C);
1374 regmap_write(priv->xfi_pextp_regmap, 0x2020, 0xAA8585AA);
1375 regmap_write(priv->xfi_pextp_regmap, 0x2030, 0x0C020707);
1376 regmap_write(priv->xfi_pextp_regmap, 0x2034, 0x0E050F0F);
1377 regmap_write(priv->xfi_pextp_regmap, 0x2040, 0x00140032);
1378 regmap_write(priv->xfi_pextp_regmap, 0x50F0, 0x00C014AA);
1379 regmap_write(priv->xfi_pextp_regmap, 0x50E0, 0x3777C12B);
1380 regmap_write(priv->xfi_pextp_regmap, 0x506C, 0x005F9CFF);
1381 regmap_write(priv->xfi_pextp_regmap, 0x5070, 0x9D9DFAFA);
1382 regmap_write(priv->xfi_pextp_regmap, 0x5074, 0x27273F3F);
1383 regmap_write(priv->xfi_pextp_regmap, 0x5078, 0xA7883C68);
1384 regmap_write(priv->xfi_pextp_regmap, 0x507C, 0x11661166);
1385 regmap_write(priv->xfi_pextp_regmap, 0x5080, 0x0E000AAF);
1386 regmap_write(priv->xfi_pextp_regmap, 0x5084, 0x08080D0D);
1387 regmap_write(priv->xfi_pextp_regmap, 0x5088, 0x02030909);
1388 regmap_write(priv->xfi_pextp_regmap, 0x50E4, 0x0C0C0000);
1389 regmap_write(priv->xfi_pextp_regmap, 0x50E8, 0x04040000);
1390 regmap_write(priv->xfi_pextp_regmap, 0x50EC, 0x0F0F0C06);
1391 regmap_write(priv->xfi_pextp_regmap, 0x50A8, 0x506E8C8C);
1392 regmap_write(priv->xfi_pextp_regmap, 0x6004, 0x18190000);
1393 regmap_write(priv->xfi_pextp_regmap, 0x00F8, 0x01423342);
1394 regmap_write(priv->xfi_pextp_regmap, 0x00F4, 0x80201F20);
1395 regmap_write(priv->xfi_pextp_regmap, 0x0030, 0x00050C00);
1396 regmap_write(priv->xfi_pextp_regmap, 0x0070, 0x02002800);
1397 ndelay(1020);
1398 regmap_write(priv->xfi_pextp_regmap, 0x30B0, 0x00000020);
1399 regmap_write(priv->xfi_pextp_regmap, 0x3028, 0x00008A01);
1400 regmap_write(priv->xfi_pextp_regmap, 0x302C, 0x0000A884);
1401 regmap_write(priv->xfi_pextp_regmap, 0x3024, 0x00083002);
1402 regmap_write(priv->xfi_pextp_regmap, 0x3010, 0x00022220);
1403 regmap_write(priv->xfi_pextp_regmap, 0x5064, 0x0F020A01);
1404 regmap_write(priv->xfi_pextp_regmap, 0x50B4, 0x06100600);
1405 regmap_write(priv->xfi_pextp_regmap, 0x3048, 0x40704000);
1406 regmap_write(priv->xfi_pextp_regmap, 0x3050, 0xA8000000);
1407 regmap_write(priv->xfi_pextp_regmap, 0x3054, 0x000000AA);
1408 regmap_write(priv->xfi_pextp_regmap, 0x306C, 0x00000F00);
1409 regmap_write(priv->xfi_pextp_regmap, 0xA060, 0x00040000);
1410 regmap_write(priv->xfi_pextp_regmap, 0x90D0, 0x00000001);
1411 regmap_write(priv->xfi_pextp_regmap, 0x0070, 0x0200E800);
1412 udelay(150);
1413 regmap_write(priv->xfi_pextp_regmap, 0x0070, 0x0200C111);
1414 ndelay(1020);
1415 regmap_write(priv->xfi_pextp_regmap, 0x0070, 0x0200C101);
1416 udelay(15);
1417 regmap_write(priv->xfi_pextp_regmap, 0x0070, 0x0202C111);
1418 ndelay(1020);
1419 regmap_write(priv->xfi_pextp_regmap, 0x0070, 0x0202C101);
1420 udelay(100);
1421 regmap_write(priv->xfi_pextp_regmap, 0x30B0, 0x00000030);
1422 regmap_write(priv->xfi_pextp_regmap, 0x00F4, 0x80201F00);
1423 regmap_write(priv->xfi_pextp_regmap, 0x3040, 0x30000000);
1424 udelay(400);
1425}
1426
1427static void mtk_usxgmii_an_init(struct mtk_eth_priv *priv)
1428{
1429 mtk_xfi_pll_enable(priv);
1430 mtk_usxgmii_reset(priv);
1431 mtk_usxgmii_setup_phya_an_10000(priv);
1432}
1433
Weijie Gao23f17162018-12-20 16:12:53 +08001434static void mtk_mac_init(struct mtk_eth_priv *priv)
1435{
1436 int i, ge_mode = 0;
1437 u32 mcr;
1438
1439 switch (priv->phy_interface) {
1440 case PHY_INTERFACE_MODE_RGMII_RXID:
1441 case PHY_INTERFACE_MODE_RGMII:
MarkLeeb4ef49a2020-01-21 19:31:57 +08001442 ge_mode = GE_MODE_RGMII;
1443 break;
Weijie Gao23f17162018-12-20 16:12:53 +08001444 case PHY_INTERFACE_MODE_SGMII:
Weijie Gaobd70f3c2023-07-19 17:17:13 +08001445 case PHY_INTERFACE_MODE_2500BASEX:
Weijie Gao585a1a42023-07-19 17:17:22 +08001446 if (MTK_HAS_CAPS(priv->soc->caps, MTK_GMAC2_U3_QPHY)) {
1447 mtk_infra_rmw(priv, USB_PHY_SWITCH_REG, QPHY_SEL_MASK,
1448 SGMII_QPHY_SEL);
1449 }
1450
Weijie Gao23f17162018-12-20 16:12:53 +08001451 ge_mode = GE_MODE_RGMII;
MarkLeeb4ef49a2020-01-21 19:31:57 +08001452 mtk_ethsys_rmw(priv, ETHSYS_SYSCFG0_REG, SYSCFG0_SGMII_SEL_M,
1453 SYSCFG0_SGMII_SEL(priv->gmac_id));
Weijie Gaobd70f3c2023-07-19 17:17:13 +08001454 if (priv->phy_interface == PHY_INTERFACE_MODE_SGMII)
1455 mtk_sgmii_an_init(priv);
1456 else
1457 mtk_sgmii_force_init(priv);
Weijie Gao23f17162018-12-20 16:12:53 +08001458 break;
1459 case PHY_INTERFACE_MODE_MII:
1460 case PHY_INTERFACE_MODE_GMII:
1461 ge_mode = GE_MODE_MII;
1462 break;
1463 case PHY_INTERFACE_MODE_RMII:
1464 ge_mode = GE_MODE_RMII;
1465 break;
1466 default:
1467 break;
1468 }
1469
1470 /* set the gmac to the right mode */
1471 mtk_ethsys_rmw(priv, ETHSYS_SYSCFG0_REG,
1472 SYSCFG0_GE_MODE_M << SYSCFG0_GE_MODE_S(priv->gmac_id),
1473 ge_mode << SYSCFG0_GE_MODE_S(priv->gmac_id));
1474
1475 if (priv->force_mode) {
Landen Chao532de8d2020-02-18 16:49:37 +08001476 mcr = (IPG_96BIT_WITH_SHORT_IPG << IPG_CFG_S) |
Weijie Gao23f17162018-12-20 16:12:53 +08001477 (MAC_RX_PKT_LEN_1536 << MAC_RX_PKT_LEN_S) |
1478 MAC_MODE | FORCE_MODE |
1479 MAC_TX_EN | MAC_RX_EN |
1480 BKOFF_EN | BACKPR_EN |
1481 FORCE_LINK;
1482
1483 switch (priv->speed) {
1484 case SPEED_10:
1485 mcr |= SPEED_10M << FORCE_SPD_S;
1486 break;
1487 case SPEED_100:
1488 mcr |= SPEED_100M << FORCE_SPD_S;
1489 break;
1490 case SPEED_1000:
Weijie Gaobd70f3c2023-07-19 17:17:13 +08001491 case SPEED_2500:
Weijie Gao23f17162018-12-20 16:12:53 +08001492 mcr |= SPEED_1000M << FORCE_SPD_S;
1493 break;
1494 }
1495
1496 if (priv->duplex)
1497 mcr |= FORCE_DPX;
1498
1499 mtk_gmac_write(priv, GMAC_PORT_MCR(priv->gmac_id), mcr);
1500 }
1501
Weijie Gao62596722022-09-09 19:59:21 +08001502 if (MTK_HAS_CAPS(priv->soc->caps, MTK_GMAC1_TRGMII) &&
1503 !MTK_HAS_CAPS(priv->soc->caps, MTK_TRGMII_MT7621_CLK)) {
Weijie Gao23f17162018-12-20 16:12:53 +08001504 /* Lower Tx Driving for TRGMII path */
1505 for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
1506 mtk_gmac_write(priv, GMAC_TRGMII_TD_ODT(i),
1507 (8 << TD_DM_DRVP_S) |
1508 (8 << TD_DM_DRVN_S));
1509
1510 mtk_gmac_rmw(priv, GMAC_TRGMII_RCK_CTRL, 0,
1511 RX_RST | RXC_DQSISEL);
1512 mtk_gmac_rmw(priv, GMAC_TRGMII_RCK_CTRL, RX_RST, 0);
1513 }
1514}
1515
Weijie Gaoba026eb2023-07-19 17:17:31 +08001516static void mtk_xmac_init(struct mtk_eth_priv *priv)
1517{
1518 u32 sts;
1519
1520 switch (priv->phy_interface) {
1521 case PHY_INTERFACE_MODE_USXGMII:
1522 mtk_usxgmii_an_init(priv);
1523 break;
1524 default:
1525 break;
1526 }
1527
1528 /* Set GMAC to the correct mode */
1529 mtk_ethsys_rmw(priv, ETHSYS_SYSCFG0_REG,
1530 SYSCFG0_GE_MODE_M << SYSCFG0_GE_MODE_S(priv->gmac_id),
1531 0);
1532
1533 if (priv->gmac_id == 1) {
1534 mtk_infra_rmw(priv, TOPMISC_NETSYS_PCS_MUX,
1535 NETSYS_PCS_MUX_MASK, MUX_G2_USXGMII_SEL);
1536 } else if (priv->gmac_id == 2) {
1537 sts = mtk_gmac_read(priv, XGMAC_STS(priv->gmac_id));
1538 sts |= XGMAC_FORCE_LINK;
1539 mtk_gmac_write(priv, XGMAC_STS(priv->gmac_id), sts);
1540 }
1541
1542 /* Force GMAC link down */
1543 mtk_gmac_write(priv, GMAC_PORT_MCR(priv->gmac_id), FORCE_MODE);
1544}
1545
Weijie Gao23f17162018-12-20 16:12:53 +08001546static void mtk_eth_fifo_init(struct mtk_eth_priv *priv)
1547{
1548 char *pkt_base = priv->pkt_pool;
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001549 struct mtk_tx_dma_v2 *txd;
1550 struct mtk_rx_dma_v2 *rxd;
Weijie Gao23f17162018-12-20 16:12:53 +08001551 int i;
1552
1553 mtk_pdma_rmw(priv, PDMA_GLO_CFG_REG, 0xffff0000, 0);
1554 udelay(500);
1555
Weijie Gao7d928c32022-09-09 19:59:24 +08001556 memset(priv->tx_ring_noc, 0, NUM_TX_DESC * priv->soc->txd_size);
1557 memset(priv->rx_ring_noc, 0, NUM_RX_DESC * priv->soc->rxd_size);
1558 memset(priv->pkt_pool, 0xff, TOTAL_PKT_BUF_SIZE);
Weijie Gao23f17162018-12-20 16:12:53 +08001559
Frank Wunderlich47b14312020-01-31 10:23:29 +01001560 flush_dcache_range((ulong)pkt_base,
1561 (ulong)(pkt_base + TOTAL_PKT_BUF_SIZE));
Weijie Gao23f17162018-12-20 16:12:53 +08001562
1563 priv->rx_dma_owner_idx0 = 0;
1564 priv->tx_cpu_owner_idx0 = 0;
1565
1566 for (i = 0; i < NUM_TX_DESC; i++) {
Weijie Gao7d928c32022-09-09 19:59:24 +08001567 txd = priv->tx_ring_noc + i * priv->soc->txd_size;
Weijie Gao23f17162018-12-20 16:12:53 +08001568
Weijie Gao7d928c32022-09-09 19:59:24 +08001569 txd->txd1 = virt_to_phys(pkt_base);
1570 txd->txd2 = PDMA_TXD2_DDONE | PDMA_TXD2_LS0;
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001571
Weijie Gao76281942023-07-19 17:17:37 +08001572 if (MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V3))
1573 txd->txd5 = PDMA_V2_TXD5_FPORT_SET(priv->gmac_id == 2 ?
1574 15 : priv->gmac_id + 1);
1575 else if (MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V2))
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001576 txd->txd5 = PDMA_V2_TXD5_FPORT_SET(priv->gmac_id + 1);
1577 else
1578 txd->txd4 = PDMA_V1_TXD4_FPORT_SET(priv->gmac_id + 1);
Weijie Gao7d928c32022-09-09 19:59:24 +08001579
Weijie Gao23f17162018-12-20 16:12:53 +08001580 pkt_base += PKTSIZE_ALIGN;
1581 }
1582
1583 for (i = 0; i < NUM_RX_DESC; i++) {
Weijie Gao7d928c32022-09-09 19:59:24 +08001584 rxd = priv->rx_ring_noc + i * priv->soc->rxd_size;
1585
1586 rxd->rxd1 = virt_to_phys(pkt_base);
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001587
Weijie Gao76281942023-07-19 17:17:37 +08001588 if (MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V2) ||
1589 MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V3))
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001590 rxd->rxd2 = PDMA_V2_RXD2_PLEN0_SET(PKTSIZE_ALIGN);
1591 else
1592 rxd->rxd2 = PDMA_V1_RXD2_PLEN0_SET(PKTSIZE_ALIGN);
Weijie Gao7d928c32022-09-09 19:59:24 +08001593
Weijie Gao23f17162018-12-20 16:12:53 +08001594 pkt_base += PKTSIZE_ALIGN;
1595 }
1596
1597 mtk_pdma_write(priv, TX_BASE_PTR_REG(0),
1598 virt_to_phys(priv->tx_ring_noc));
1599 mtk_pdma_write(priv, TX_MAX_CNT_REG(0), NUM_TX_DESC);
1600 mtk_pdma_write(priv, TX_CTX_IDX_REG(0), priv->tx_cpu_owner_idx0);
1601
1602 mtk_pdma_write(priv, RX_BASE_PTR_REG(0),
1603 virt_to_phys(priv->rx_ring_noc));
1604 mtk_pdma_write(priv, RX_MAX_CNT_REG(0), NUM_RX_DESC);
1605 mtk_pdma_write(priv, RX_CRX_IDX_REG(0), NUM_RX_DESC - 1);
1606
1607 mtk_pdma_write(priv, PDMA_RST_IDX_REG, RST_DTX_IDX0 | RST_DRX_IDX0);
1608}
1609
1610static int mtk_eth_start(struct udevice *dev)
1611{
1612 struct mtk_eth_priv *priv = dev_get_priv(dev);
Weijie Gao76281942023-07-19 17:17:37 +08001613 int i, ret;
Weijie Gao23f17162018-12-20 16:12:53 +08001614
1615 /* Reset FE */
1616 reset_assert(&priv->rst_fe);
1617 udelay(1000);
1618 reset_deassert(&priv->rst_fe);
1619 mdelay(10);
1620
Weijie Gao76281942023-07-19 17:17:37 +08001621 if (MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V2) ||
1622 MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V3))
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001623 setbits_le32(priv->fe_base + FE_GLO_MISC_REG, PDMA_VER_V2);
1624
Weijie Gao23f17162018-12-20 16:12:53 +08001625 /* Packets forward to PDMA */
1626 mtk_gdma_write(priv, priv->gmac_id, GDMA_IG_CTRL_REG, GDMA_FWD_TO_CPU);
1627
Weijie Gao76281942023-07-19 17:17:37 +08001628 for (i = 0; i < priv->soc->gdma_count; i++) {
1629 if (i == priv->gmac_id)
1630 continue;
1631
1632 mtk_gdma_write(priv, i, GDMA_IG_CTRL_REG, GDMA_FWD_DISCARD);
1633 }
1634
1635 if (MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V3)) {
Weijie Gao93eb7072023-07-19 17:17:41 +08001636 if (priv->sw == SW_MT7988 && priv->gmac_id == 0) {
1637 mtk_gdma_write(priv, priv->gmac_id, GDMA_IG_CTRL_REG,
1638 GDMA_BRIDGE_TO_CPU);
1639 }
1640
Weijie Gao76281942023-07-19 17:17:37 +08001641 mtk_gdma_write(priv, priv->gmac_id, GDMA_EG_CTRL_REG,
1642 GDMA_CPU_BRIDGE_EN);
1643 }
Weijie Gao23f17162018-12-20 16:12:53 +08001644
1645 udelay(500);
1646
1647 mtk_eth_fifo_init(priv);
1648
Weijie Gaoc73d3872023-07-19 17:16:54 +08001649 if (priv->switch_mac_control)
1650 priv->switch_mac_control(priv, true);
1651
Weijie Gao23f17162018-12-20 16:12:53 +08001652 /* Start PHY */
1653 if (priv->sw == SW_NONE) {
1654 ret = mtk_phy_start(priv);
1655 if (ret)
1656 return ret;
1657 }
1658
1659 mtk_pdma_rmw(priv, PDMA_GLO_CFG_REG, 0,
1660 TX_WB_DDONE | RX_DMA_EN | TX_DMA_EN);
1661 udelay(500);
1662
1663 return 0;
1664}
1665
1666static void mtk_eth_stop(struct udevice *dev)
1667{
1668 struct mtk_eth_priv *priv = dev_get_priv(dev);
1669
Weijie Gaoc73d3872023-07-19 17:16:54 +08001670 if (priv->switch_mac_control)
1671 priv->switch_mac_control(priv, false);
1672
Weijie Gao23f17162018-12-20 16:12:53 +08001673 mtk_pdma_rmw(priv, PDMA_GLO_CFG_REG,
1674 TX_WB_DDONE | RX_DMA_EN | TX_DMA_EN, 0);
1675 udelay(500);
1676
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001677 wait_for_bit_le32(priv->fe_base + priv->soc->pdma_base + PDMA_GLO_CFG_REG,
Weijie Gao23f17162018-12-20 16:12:53 +08001678 RX_DMA_BUSY | TX_DMA_BUSY, 0, 5000, 0);
1679}
1680
1681static int mtk_eth_write_hwaddr(struct udevice *dev)
1682{
Simon Glassc69cda22020-12-03 16:55:20 -07001683 struct eth_pdata *pdata = dev_get_plat(dev);
Weijie Gao23f17162018-12-20 16:12:53 +08001684 struct mtk_eth_priv *priv = dev_get_priv(dev);
1685 unsigned char *mac = pdata->enetaddr;
1686 u32 macaddr_lsb, macaddr_msb;
1687
1688 macaddr_msb = ((u32)mac[0] << 8) | (u32)mac[1];
1689 macaddr_lsb = ((u32)mac[2] << 24) | ((u32)mac[3] << 16) |
1690 ((u32)mac[4] << 8) | (u32)mac[5];
1691
1692 mtk_gdma_write(priv, priv->gmac_id, GDMA_MAC_MSB_REG, macaddr_msb);
1693 mtk_gdma_write(priv, priv->gmac_id, GDMA_MAC_LSB_REG, macaddr_lsb);
1694
1695 return 0;
1696}
1697
1698static int mtk_eth_send(struct udevice *dev, void *packet, int length)
1699{
1700 struct mtk_eth_priv *priv = dev_get_priv(dev);
1701 u32 idx = priv->tx_cpu_owner_idx0;
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001702 struct mtk_tx_dma_v2 *txd;
Weijie Gao23f17162018-12-20 16:12:53 +08001703 void *pkt_base;
1704
Weijie Gao7d928c32022-09-09 19:59:24 +08001705 txd = priv->tx_ring_noc + idx * priv->soc->txd_size;
1706
1707 if (!(txd->txd2 & PDMA_TXD2_DDONE)) {
Weijie Gao23f17162018-12-20 16:12:53 +08001708 debug("mtk-eth: TX DMA descriptor ring is full\n");
1709 return -EPERM;
1710 }
1711
Weijie Gao7d928c32022-09-09 19:59:24 +08001712 pkt_base = (void *)phys_to_virt(txd->txd1);
Weijie Gao23f17162018-12-20 16:12:53 +08001713 memcpy(pkt_base, packet, length);
Frank Wunderlich47b14312020-01-31 10:23:29 +01001714 flush_dcache_range((ulong)pkt_base, (ulong)pkt_base +
Weijie Gao23f17162018-12-20 16:12:53 +08001715 roundup(length, ARCH_DMA_MINALIGN));
1716
Weijie Gao76281942023-07-19 17:17:37 +08001717 if (MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V2) ||
1718 MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V3))
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001719 txd->txd2 = PDMA_TXD2_LS0 | PDMA_V2_TXD2_SDL0_SET(length);
1720 else
1721 txd->txd2 = PDMA_TXD2_LS0 | PDMA_V1_TXD2_SDL0_SET(length);
Weijie Gao23f17162018-12-20 16:12:53 +08001722
1723 priv->tx_cpu_owner_idx0 = (priv->tx_cpu_owner_idx0 + 1) % NUM_TX_DESC;
1724 mtk_pdma_write(priv, TX_CTX_IDX_REG(0), priv->tx_cpu_owner_idx0);
1725
1726 return 0;
1727}
1728
1729static int mtk_eth_recv(struct udevice *dev, int flags, uchar **packetp)
1730{
1731 struct mtk_eth_priv *priv = dev_get_priv(dev);
1732 u32 idx = priv->rx_dma_owner_idx0;
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001733 struct mtk_rx_dma_v2 *rxd;
Weijie Gao23f17162018-12-20 16:12:53 +08001734 uchar *pkt_base;
1735 u32 length;
1736
Weijie Gao7d928c32022-09-09 19:59:24 +08001737 rxd = priv->rx_ring_noc + idx * priv->soc->rxd_size;
1738
1739 if (!(rxd->rxd2 & PDMA_RXD2_DDONE)) {
Weijie Gao23f17162018-12-20 16:12:53 +08001740 debug("mtk-eth: RX DMA descriptor ring is empty\n");
1741 return -EAGAIN;
1742 }
1743
Weijie Gao76281942023-07-19 17:17:37 +08001744 if (MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V2) ||
1745 MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V3))
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001746 length = PDMA_V2_RXD2_PLEN0_GET(rxd->rxd2);
1747 else
1748 length = PDMA_V1_RXD2_PLEN0_GET(rxd->rxd2);
Weijie Gao7d928c32022-09-09 19:59:24 +08001749
1750 pkt_base = (void *)phys_to_virt(rxd->rxd1);
Frank Wunderlich47b14312020-01-31 10:23:29 +01001751 invalidate_dcache_range((ulong)pkt_base, (ulong)pkt_base +
Weijie Gao23f17162018-12-20 16:12:53 +08001752 roundup(length, ARCH_DMA_MINALIGN));
1753
1754 if (packetp)
1755 *packetp = pkt_base;
1756
1757 return length;
1758}
1759
1760static int mtk_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
1761{
1762 struct mtk_eth_priv *priv = dev_get_priv(dev);
1763 u32 idx = priv->rx_dma_owner_idx0;
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001764 struct mtk_rx_dma_v2 *rxd;
Weijie Gao23f17162018-12-20 16:12:53 +08001765
Weijie Gao7d928c32022-09-09 19:59:24 +08001766 rxd = priv->rx_ring_noc + idx * priv->soc->rxd_size;
1767
Weijie Gao76281942023-07-19 17:17:37 +08001768 if (MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V2) ||
1769 MTK_HAS_CAPS(priv->soc->caps, MTK_NETSYS_V3))
Weijie Gaoe7ad0462022-09-09 19:59:26 +08001770 rxd->rxd2 = PDMA_V2_RXD2_PLEN0_SET(PKTSIZE_ALIGN);
1771 else
1772 rxd->rxd2 = PDMA_V1_RXD2_PLEN0_SET(PKTSIZE_ALIGN);
Weijie Gao23f17162018-12-20 16:12:53 +08001773
1774 mtk_pdma_write(priv, RX_CRX_IDX_REG(0), idx);
1775 priv->rx_dma_owner_idx0 = (priv->rx_dma_owner_idx0 + 1) % NUM_RX_DESC;
1776
1777 return 0;
1778}
1779
1780static int mtk_eth_probe(struct udevice *dev)
1781{
Simon Glassc69cda22020-12-03 16:55:20 -07001782 struct eth_pdata *pdata = dev_get_plat(dev);
Weijie Gao23f17162018-12-20 16:12:53 +08001783 struct mtk_eth_priv *priv = dev_get_priv(dev);
Frank Wunderlich47b14312020-01-31 10:23:29 +01001784 ulong iobase = pdata->iobase;
Weijie Gao23f17162018-12-20 16:12:53 +08001785 int ret;
1786
1787 /* Frame Engine Register Base */
1788 priv->fe_base = (void *)iobase;
1789
1790 /* GMAC Register Base */
1791 priv->gmac_base = (void *)(iobase + GMAC_BASE);
1792
1793 /* MDIO register */
1794 ret = mtk_mdio_register(dev);
1795 if (ret)
1796 return ret;
1797
1798 /* Prepare for tx/rx rings */
Weijie Gao7d928c32022-09-09 19:59:24 +08001799 priv->tx_ring_noc = (void *)
1800 noncached_alloc(priv->soc->txd_size * NUM_TX_DESC,
Weijie Gao23f17162018-12-20 16:12:53 +08001801 ARCH_DMA_MINALIGN);
Weijie Gao7d928c32022-09-09 19:59:24 +08001802 priv->rx_ring_noc = (void *)
1803 noncached_alloc(priv->soc->rxd_size * NUM_RX_DESC,
Weijie Gao23f17162018-12-20 16:12:53 +08001804 ARCH_DMA_MINALIGN);
1805
1806 /* Set MAC mode */
Weijie Gaoba026eb2023-07-19 17:17:31 +08001807 if (priv->phy_interface == PHY_INTERFACE_MODE_USXGMII)
1808 mtk_xmac_init(priv);
1809 else
1810 mtk_mac_init(priv);
Weijie Gao23f17162018-12-20 16:12:53 +08001811
1812 /* Probe phy if switch is not specified */
1813 if (priv->sw == SW_NONE)
1814 return mtk_phy_probe(dev);
1815
1816 /* Initialize switch */
Landen Chao532de8d2020-02-18 16:49:37 +08001817 return mt753x_switch_init(priv);
Weijie Gao23f17162018-12-20 16:12:53 +08001818}
1819
1820static int mtk_eth_remove(struct udevice *dev)
1821{
1822 struct mtk_eth_priv *priv = dev_get_priv(dev);
1823
1824 /* MDIO unregister */
1825 mdio_unregister(priv->mdio_bus);
1826 mdio_free(priv->mdio_bus);
1827
1828 /* Stop possibly started DMA */
1829 mtk_eth_stop(dev);
1830
1831 return 0;
1832}
1833
Simon Glassd1998a92020-12-03 16:55:21 -07001834static int mtk_eth_of_to_plat(struct udevice *dev)
Weijie Gao23f17162018-12-20 16:12:53 +08001835{
Simon Glassc69cda22020-12-03 16:55:20 -07001836 struct eth_pdata *pdata = dev_get_plat(dev);
Weijie Gao23f17162018-12-20 16:12:53 +08001837 struct mtk_eth_priv *priv = dev_get_priv(dev);
1838 struct ofnode_phandle_args args;
1839 struct regmap *regmap;
1840 const char *str;
1841 ofnode subnode;
1842 int ret;
1843
Weijie Gao62596722022-09-09 19:59:21 +08001844 priv->soc = (const struct mtk_soc_data *)dev_get_driver_data(dev);
1845 if (!priv->soc) {
1846 dev_err(dev, "missing soc compatible data\n");
1847 return -EINVAL;
1848 }
Weijie Gao23f17162018-12-20 16:12:53 +08001849
Weijie Gao528e4832022-05-20 11:23:31 +08001850 pdata->iobase = (phys_addr_t)dev_remap_addr(dev);
Weijie Gao23f17162018-12-20 16:12:53 +08001851
1852 /* get corresponding ethsys phandle */
1853 ret = dev_read_phandle_with_args(dev, "mediatek,ethsys", NULL, 0, 0,
1854 &args);
1855 if (ret)
1856 return ret;
1857
Weijie Gao86062e72022-05-20 11:23:37 +08001858 priv->ethsys_regmap = syscon_node_to_regmap(args.node);
1859 if (IS_ERR(priv->ethsys_regmap))
1860 return PTR_ERR(priv->ethsys_regmap);
Weijie Gao23f17162018-12-20 16:12:53 +08001861
Weijie Gao585a1a42023-07-19 17:17:22 +08001862 if (MTK_HAS_CAPS(priv->soc->caps, MTK_INFRA)) {
1863 /* get corresponding infracfg phandle */
1864 ret = dev_read_phandle_with_args(dev, "mediatek,infracfg",
1865 NULL, 0, 0, &args);
1866
1867 if (ret)
1868 return ret;
1869
1870 priv->infra_regmap = syscon_node_to_regmap(args.node);
1871 if (IS_ERR(priv->infra_regmap))
1872 return PTR_ERR(priv->infra_regmap);
1873 }
1874
Weijie Gao23f17162018-12-20 16:12:53 +08001875 /* Reset controllers */
1876 ret = reset_get_by_name(dev, "fe", &priv->rst_fe);
1877 if (ret) {
1878 printf("error: Unable to get reset ctrl for frame engine\n");
1879 return ret;
1880 }
1881
1882 priv->gmac_id = dev_read_u32_default(dev, "mediatek,gmac-id", 0);
1883
1884 /* Interface mode is required */
Marek Behún123ca112022-04-07 00:33:01 +02001885 pdata->phy_interface = dev_read_phy_mode(dev);
1886 priv->phy_interface = pdata->phy_interface;
Marek Behúnffb0f6f2022-04-07 00:33:03 +02001887 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA) {
Weijie Gao23f17162018-12-20 16:12:53 +08001888 printf("error: phy-mode is not set\n");
1889 return -EINVAL;
1890 }
1891
1892 /* Force mode or autoneg */
1893 subnode = ofnode_find_subnode(dev_ofnode(dev), "fixed-link");
1894 if (ofnode_valid(subnode)) {
1895 priv->force_mode = 1;
1896 priv->speed = ofnode_read_u32_default(subnode, "speed", 0);
1897 priv->duplex = ofnode_read_bool(subnode, "full-duplex");
1898
1899 if (priv->speed != SPEED_10 && priv->speed != SPEED_100 &&
Weijie Gaobd70f3c2023-07-19 17:17:13 +08001900 priv->speed != SPEED_1000 && priv->speed != SPEED_2500 &&
1901 priv->speed != SPEED_10000) {
Weijie Gao23f17162018-12-20 16:12:53 +08001902 printf("error: no valid speed set in fixed-link\n");
1903 return -EINVAL;
1904 }
1905 }
1906
Weijie Gaobd70f3c2023-07-19 17:17:13 +08001907 if (priv->phy_interface == PHY_INTERFACE_MODE_SGMII ||
1908 priv->phy_interface == PHY_INTERFACE_MODE_2500BASEX) {
MarkLeeb4ef49a2020-01-21 19:31:57 +08001909 /* get corresponding sgmii phandle */
1910 ret = dev_read_phandle_with_args(dev, "mediatek,sgmiisys",
1911 NULL, 0, 0, &args);
1912 if (ret)
1913 return ret;
1914
1915 regmap = syscon_node_to_regmap(args.node);
1916
1917 if (IS_ERR(regmap))
1918 return PTR_ERR(regmap);
1919
1920 priv->sgmii_base = regmap_get_range(regmap, 0);
1921
1922 if (!priv->sgmii_base) {
1923 dev_err(dev, "Unable to find sgmii\n");
1924 return -ENODEV;
1925 }
Weijie Gao29a48bf2022-09-09 19:59:28 +08001926
1927 priv->pn_swap = ofnode_read_bool(args.node, "pn_swap");
Weijie Gaoba026eb2023-07-19 17:17:31 +08001928 } else if (priv->phy_interface == PHY_INTERFACE_MODE_USXGMII) {
1929 /* get corresponding usxgmii phandle */
1930 ret = dev_read_phandle_with_args(dev, "mediatek,usxgmiisys",
1931 NULL, 0, 0, &args);
1932 if (ret)
1933 return ret;
1934
1935 priv->usxgmii_regmap = syscon_node_to_regmap(args.node);
1936 if (IS_ERR(priv->usxgmii_regmap))
1937 return PTR_ERR(priv->usxgmii_regmap);
1938
1939 /* get corresponding xfi_pextp phandle */
1940 ret = dev_read_phandle_with_args(dev, "mediatek,xfi_pextp",
1941 NULL, 0, 0, &args);
1942 if (ret)
1943 return ret;
1944
1945 priv->xfi_pextp_regmap = syscon_node_to_regmap(args.node);
1946 if (IS_ERR(priv->xfi_pextp_regmap))
1947 return PTR_ERR(priv->xfi_pextp_regmap);
1948
1949 /* get corresponding xfi_pll phandle */
1950 ret = dev_read_phandle_with_args(dev, "mediatek,xfi_pll",
1951 NULL, 0, 0, &args);
1952 if (ret)
1953 return ret;
1954
1955 priv->xfi_pll_regmap = syscon_node_to_regmap(args.node);
1956 if (IS_ERR(priv->xfi_pll_regmap))
1957 return PTR_ERR(priv->xfi_pll_regmap);
1958
1959 /* get corresponding toprgu phandle */
1960 ret = dev_read_phandle_with_args(dev, "mediatek,toprgu",
1961 NULL, 0, 0, &args);
1962 if (ret)
1963 return ret;
1964
1965 priv->toprgu_regmap = syscon_node_to_regmap(args.node);
1966 if (IS_ERR(priv->toprgu_regmap))
1967 return PTR_ERR(priv->toprgu_regmap);
MarkLeeb4ef49a2020-01-21 19:31:57 +08001968 }
1969
Weijie Gao23f17162018-12-20 16:12:53 +08001970 /* check for switch first, otherwise phy will be used */
1971 priv->sw = SW_NONE;
1972 priv->switch_init = NULL;
Weijie Gaoc73d3872023-07-19 17:16:54 +08001973 priv->switch_mac_control = NULL;
Weijie Gao23f17162018-12-20 16:12:53 +08001974 str = dev_read_string(dev, "mediatek,switch");
1975
1976 if (str) {
1977 if (!strcmp(str, "mt7530")) {
1978 priv->sw = SW_MT7530;
1979 priv->switch_init = mt7530_setup;
Weijie Gaoc73d3872023-07-19 17:16:54 +08001980 priv->switch_mac_control = mt7530_mac_control;
Landen Chao532de8d2020-02-18 16:49:37 +08001981 priv->mt753x_smi_addr = MT753X_DFL_SMI_ADDR;
Weijie Gaoc41a0582023-07-19 17:16:59 +08001982 priv->mt753x_reset_wait_time = 1000;
Landen Chao532de8d2020-02-18 16:49:37 +08001983 } else if (!strcmp(str, "mt7531")) {
1984 priv->sw = SW_MT7531;
1985 priv->switch_init = mt7531_setup;
Weijie Gaoc73d3872023-07-19 17:16:54 +08001986 priv->switch_mac_control = mt7531_mac_control;
Landen Chao532de8d2020-02-18 16:49:37 +08001987 priv->mt753x_smi_addr = MT753X_DFL_SMI_ADDR;
Weijie Gaoc41a0582023-07-19 17:16:59 +08001988 priv->mt753x_reset_wait_time = 200;
Weijie Gao93eb7072023-07-19 17:17:41 +08001989 } else if (!strcmp(str, "mt7988")) {
1990 priv->sw = SW_MT7988;
1991 priv->switch_init = mt7988_setup;
1992 priv->switch_mac_control = mt7988_mac_control;
1993 priv->mt753x_smi_addr = MT753X_DFL_SMI_ADDR;
1994 priv->mt753x_reset_wait_time = 50;
Weijie Gao23f17162018-12-20 16:12:53 +08001995 } else {
1996 printf("error: unsupported switch\n");
1997 return -EINVAL;
1998 }
1999
2000 priv->mcm = dev_read_bool(dev, "mediatek,mcm");
2001 if (priv->mcm) {
2002 ret = reset_get_by_name(dev, "mcm", &priv->rst_mcm);
2003 if (ret) {
2004 printf("error: no reset ctrl for mcm\n");
2005 return ret;
2006 }
2007 } else {
2008 gpio_request_by_name(dev, "reset-gpios", 0,
2009 &priv->rst_gpio, GPIOD_IS_OUT);
2010 }
2011 } else {
Weijie Gaoebb97ea2019-04-28 15:08:57 +08002012 ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0,
2013 0, &args);
2014 if (ret) {
Weijie Gao23f17162018-12-20 16:12:53 +08002015 printf("error: phy-handle is not specified\n");
2016 return ret;
2017 }
2018
Weijie Gaoebb97ea2019-04-28 15:08:57 +08002019 priv->phy_addr = ofnode_read_s32_default(args.node, "reg", -1);
Weijie Gao23f17162018-12-20 16:12:53 +08002020 if (priv->phy_addr < 0) {
2021 printf("error: phy address is not specified\n");
2022 return ret;
2023 }
2024 }
2025
2026 return 0;
2027}
2028
Weijie Gao93eb7072023-07-19 17:17:41 +08002029static const struct mtk_soc_data mt7988_data = {
2030 .caps = MT7988_CAPS,
2031 .ana_rgc3 = 0x128,
2032 .gdma_count = 3,
2033 .pdma_base = PDMA_V3_BASE,
2034 .txd_size = sizeof(struct mtk_tx_dma_v2),
2035 .rxd_size = sizeof(struct mtk_rx_dma_v2),
2036};
2037
Weijie Gao29a48bf2022-09-09 19:59:28 +08002038static const struct mtk_soc_data mt7986_data = {
2039 .caps = MT7986_CAPS,
2040 .ana_rgc3 = 0x128,
Weijie Gao76281942023-07-19 17:17:37 +08002041 .gdma_count = 2,
Weijie Gao29a48bf2022-09-09 19:59:28 +08002042 .pdma_base = PDMA_V2_BASE,
2043 .txd_size = sizeof(struct mtk_tx_dma_v2),
2044 .rxd_size = sizeof(struct mtk_rx_dma_v2),
2045};
2046
2047static const struct mtk_soc_data mt7981_data = {
Weijie Gao585a1a42023-07-19 17:17:22 +08002048 .caps = MT7981_CAPS,
Weijie Gao29a48bf2022-09-09 19:59:28 +08002049 .ana_rgc3 = 0x128,
Weijie Gao76281942023-07-19 17:17:37 +08002050 .gdma_count = 2,
Weijie Gao29a48bf2022-09-09 19:59:28 +08002051 .pdma_base = PDMA_V2_BASE,
2052 .txd_size = sizeof(struct mtk_tx_dma_v2),
2053 .rxd_size = sizeof(struct mtk_rx_dma_v2),
2054};
2055
Weijie Gao62596722022-09-09 19:59:21 +08002056static const struct mtk_soc_data mt7629_data = {
2057 .ana_rgc3 = 0x128,
Weijie Gao76281942023-07-19 17:17:37 +08002058 .gdma_count = 2,
Weijie Gaoe7ad0462022-09-09 19:59:26 +08002059 .pdma_base = PDMA_V1_BASE,
Weijie Gao7d928c32022-09-09 19:59:24 +08002060 .txd_size = sizeof(struct mtk_tx_dma),
2061 .rxd_size = sizeof(struct mtk_rx_dma),
Weijie Gao62596722022-09-09 19:59:21 +08002062};
2063
2064static const struct mtk_soc_data mt7623_data = {
2065 .caps = MT7623_CAPS,
Weijie Gao76281942023-07-19 17:17:37 +08002066 .gdma_count = 2,
Weijie Gaoe7ad0462022-09-09 19:59:26 +08002067 .pdma_base = PDMA_V1_BASE,
Weijie Gao7d928c32022-09-09 19:59:24 +08002068 .txd_size = sizeof(struct mtk_tx_dma),
2069 .rxd_size = sizeof(struct mtk_rx_dma),
Weijie Gao62596722022-09-09 19:59:21 +08002070};
2071
2072static const struct mtk_soc_data mt7622_data = {
2073 .ana_rgc3 = 0x2028,
Weijie Gao76281942023-07-19 17:17:37 +08002074 .gdma_count = 2,
Weijie Gaoe7ad0462022-09-09 19:59:26 +08002075 .pdma_base = PDMA_V1_BASE,
Weijie Gao7d928c32022-09-09 19:59:24 +08002076 .txd_size = sizeof(struct mtk_tx_dma),
2077 .rxd_size = sizeof(struct mtk_rx_dma),
Weijie Gao62596722022-09-09 19:59:21 +08002078};
2079
2080static const struct mtk_soc_data mt7621_data = {
2081 .caps = MT7621_CAPS,
Weijie Gao76281942023-07-19 17:17:37 +08002082 .gdma_count = 2,
Weijie Gaoe7ad0462022-09-09 19:59:26 +08002083 .pdma_base = PDMA_V1_BASE,
Weijie Gao7d928c32022-09-09 19:59:24 +08002084 .txd_size = sizeof(struct mtk_tx_dma),
2085 .rxd_size = sizeof(struct mtk_rx_dma),
Weijie Gao62596722022-09-09 19:59:21 +08002086};
2087
Weijie Gao23f17162018-12-20 16:12:53 +08002088static const struct udevice_id mtk_eth_ids[] = {
Weijie Gao93eb7072023-07-19 17:17:41 +08002089 { .compatible = "mediatek,mt7988-eth", .data = (ulong)&mt7988_data },
Weijie Gao29a48bf2022-09-09 19:59:28 +08002090 { .compatible = "mediatek,mt7986-eth", .data = (ulong)&mt7986_data },
2091 { .compatible = "mediatek,mt7981-eth", .data = (ulong)&mt7981_data },
Weijie Gao62596722022-09-09 19:59:21 +08002092 { .compatible = "mediatek,mt7629-eth", .data = (ulong)&mt7629_data },
2093 { .compatible = "mediatek,mt7623-eth", .data = (ulong)&mt7623_data },
2094 { .compatible = "mediatek,mt7622-eth", .data = (ulong)&mt7622_data },
2095 { .compatible = "mediatek,mt7621-eth", .data = (ulong)&mt7621_data },
Weijie Gao23f17162018-12-20 16:12:53 +08002096 {}
2097};
2098
2099static const struct eth_ops mtk_eth_ops = {
2100 .start = mtk_eth_start,
2101 .stop = mtk_eth_stop,
2102 .send = mtk_eth_send,
2103 .recv = mtk_eth_recv,
2104 .free_pkt = mtk_eth_free_pkt,
2105 .write_hwaddr = mtk_eth_write_hwaddr,
2106};
2107
2108U_BOOT_DRIVER(mtk_eth) = {
2109 .name = "mtk-eth",
2110 .id = UCLASS_ETH,
2111 .of_match = mtk_eth_ids,
Simon Glassd1998a92020-12-03 16:55:21 -07002112 .of_to_plat = mtk_eth_of_to_plat,
Simon Glasscaa4daa2020-12-03 16:55:18 -07002113 .plat_auto = sizeof(struct eth_pdata),
Weijie Gao23f17162018-12-20 16:12:53 +08002114 .probe = mtk_eth_probe,
2115 .remove = mtk_eth_remove,
2116 .ops = &mtk_eth_ops,
Simon Glass41575d82020-12-03 16:55:17 -07002117 .priv_auto = sizeof(struct mtk_eth_priv),
Weijie Gao23f17162018-12-20 16:12:53 +08002118 .flags = DM_FLAG_ALLOC_PRIV_DMA,
2119};