blob: a06a15772c9bdd726d1ace81629f3cb98c6062cb [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>
29
30#include "mtk_eth.h"
31
32#define NUM_TX_DESC 24
33#define NUM_RX_DESC 24
34#define TX_TOTAL_BUF_SIZE (NUM_TX_DESC * PKTSIZE_ALIGN)
35#define RX_TOTAL_BUF_SIZE (NUM_RX_DESC * PKTSIZE_ALIGN)
36#define TOTAL_PKT_BUF_SIZE (TX_TOTAL_BUF_SIZE + RX_TOTAL_BUF_SIZE)
37
Landen Chao532de8d2020-02-18 16:49:37 +080038#define MT753X_NUM_PHYS 5
39#define MT753X_NUM_PORTS 7
40#define MT753X_DFL_SMI_ADDR 31
41#define MT753X_SMI_ADDR_MASK 0x1f
Weijie Gao23f17162018-12-20 16:12:53 +080042
Landen Chao532de8d2020-02-18 16:49:37 +080043#define MT753X_PHY_ADDR(base, addr) \
Weijie Gao23f17162018-12-20 16:12:53 +080044 (((base) + (addr)) & 0x1f)
45
46#define GDMA_FWD_TO_CPU \
47 (0x20000000 | \
48 GDM_ICS_EN | \
49 GDM_TCS_EN | \
50 GDM_UCS_EN | \
51 STRP_CRC | \
52 (DP_PDMA << MYMAC_DP_S) | \
53 (DP_PDMA << BC_DP_S) | \
54 (DP_PDMA << MC_DP_S) | \
55 (DP_PDMA << UN_DP_S))
56
57#define GDMA_FWD_DISCARD \
58 (0x20000000 | \
59 GDM_ICS_EN | \
60 GDM_TCS_EN | \
61 GDM_UCS_EN | \
62 STRP_CRC | \
63 (DP_DISCARD << MYMAC_DP_S) | \
64 (DP_DISCARD << BC_DP_S) | \
65 (DP_DISCARD << MC_DP_S) | \
66 (DP_DISCARD << UN_DP_S))
67
68struct pdma_rxd_info1 {
69 u32 PDP0;
70};
71
72struct pdma_rxd_info2 {
73 u32 PLEN1 : 14;
74 u32 LS1 : 1;
75 u32 UN_USED : 1;
76 u32 PLEN0 : 14;
77 u32 LS0 : 1;
78 u32 DDONE : 1;
79};
80
81struct pdma_rxd_info3 {
82 u32 PDP1;
83};
84
85struct pdma_rxd_info4 {
86 u32 FOE_ENTRY : 14;
87 u32 CRSN : 5;
88 u32 SP : 3;
89 u32 L4F : 1;
90 u32 L4VLD : 1;
91 u32 TACK : 1;
92 u32 IP4F : 1;
93 u32 IP4 : 1;
94 u32 IP6 : 1;
95 u32 UN_USED : 4;
96};
97
98struct pdma_rxdesc {
99 struct pdma_rxd_info1 rxd_info1;
100 struct pdma_rxd_info2 rxd_info2;
101 struct pdma_rxd_info3 rxd_info3;
102 struct pdma_rxd_info4 rxd_info4;
103};
104
105struct pdma_txd_info1 {
106 u32 SDP0;
107};
108
109struct pdma_txd_info2 {
110 u32 SDL1 : 14;
111 u32 LS1 : 1;
112 u32 BURST : 1;
113 u32 SDL0 : 14;
114 u32 LS0 : 1;
115 u32 DDONE : 1;
116};
117
118struct pdma_txd_info3 {
119 u32 SDP1;
120};
121
122struct pdma_txd_info4 {
123 u32 VLAN_TAG : 16;
124 u32 INS : 1;
125 u32 RESV : 2;
126 u32 UDF : 6;
127 u32 FPORT : 3;
128 u32 TSO : 1;
129 u32 TUI_CO : 3;
130};
131
132struct pdma_txdesc {
133 struct pdma_txd_info1 txd_info1;
134 struct pdma_txd_info2 txd_info2;
135 struct pdma_txd_info3 txd_info3;
136 struct pdma_txd_info4 txd_info4;
137};
138
139enum mtk_switch {
140 SW_NONE,
Landen Chao532de8d2020-02-18 16:49:37 +0800141 SW_MT7530,
142 SW_MT7531
Weijie Gao23f17162018-12-20 16:12:53 +0800143};
144
145enum mtk_soc {
146 SOC_MT7623,
MarkLeee3957172020-01-21 19:31:58 +0800147 SOC_MT7629,
148 SOC_MT7622
Weijie Gao23f17162018-12-20 16:12:53 +0800149};
150
151struct mtk_eth_priv {
152 char pkt_pool[TOTAL_PKT_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
153
154 struct pdma_txdesc *tx_ring_noc;
155 struct pdma_rxdesc *rx_ring_noc;
156
157 int rx_dma_owner_idx0;
158 int tx_cpu_owner_idx0;
159
160 void __iomem *fe_base;
161 void __iomem *gmac_base;
162 void __iomem *ethsys_base;
MarkLeeb4ef49a2020-01-21 19:31:57 +0800163 void __iomem *sgmii_base;
Weijie Gao23f17162018-12-20 16:12:53 +0800164
165 struct mii_dev *mdio_bus;
166 int (*mii_read)(struct mtk_eth_priv *priv, u8 phy, u8 reg);
167 int (*mii_write)(struct mtk_eth_priv *priv, u8 phy, u8 reg, u16 val);
168 int (*mmd_read)(struct mtk_eth_priv *priv, u8 addr, u8 devad, u16 reg);
169 int (*mmd_write)(struct mtk_eth_priv *priv, u8 addr, u8 devad, u16 reg,
170 u16 val);
171
172 enum mtk_soc soc;
173 int gmac_id;
174 int force_mode;
175 int speed;
176 int duplex;
177
178 struct phy_device *phydev;
179 int phy_interface;
180 int phy_addr;
181
182 enum mtk_switch sw;
183 int (*switch_init)(struct mtk_eth_priv *priv);
Landen Chao532de8d2020-02-18 16:49:37 +0800184 u32 mt753x_smi_addr;
185 u32 mt753x_phy_base;
Weijie Gao23f17162018-12-20 16:12:53 +0800186
187 struct gpio_desc rst_gpio;
188 int mcm;
189
190 struct reset_ctl rst_fe;
191 struct reset_ctl rst_mcm;
192};
193
194static void mtk_pdma_write(struct mtk_eth_priv *priv, u32 reg, u32 val)
195{
196 writel(val, priv->fe_base + PDMA_BASE + reg);
197}
198
199static void mtk_pdma_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr,
200 u32 set)
201{
202 clrsetbits_le32(priv->fe_base + PDMA_BASE + reg, clr, set);
203}
204
205static void mtk_gdma_write(struct mtk_eth_priv *priv, int no, u32 reg,
206 u32 val)
207{
208 u32 gdma_base;
209
210 if (no == 1)
211 gdma_base = GDMA2_BASE;
212 else
213 gdma_base = GDMA1_BASE;
214
215 writel(val, priv->fe_base + gdma_base + reg);
216}
217
218static u32 mtk_gmac_read(struct mtk_eth_priv *priv, u32 reg)
219{
220 return readl(priv->gmac_base + reg);
221}
222
223static void mtk_gmac_write(struct mtk_eth_priv *priv, u32 reg, u32 val)
224{
225 writel(val, priv->gmac_base + reg);
226}
227
228static void mtk_gmac_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr, u32 set)
229{
230 clrsetbits_le32(priv->gmac_base + reg, clr, set);
231}
232
233static void mtk_ethsys_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr,
234 u32 set)
235{
236 clrsetbits_le32(priv->ethsys_base + reg, clr, set);
237}
238
239/* 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
251 if (cmd == MDIO_CMD_WRITE)
252 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
263 if (cmd == MDIO_CMD_READ) {
264 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
373 /* Write page address */
374 ret = mtk_mii_write(priv, priv->mt753x_smi_addr, 0x1f, reg >> 6);
375 if (ret)
376 return ret;
377
378 /* Read low word */
379 low_word = mtk_mii_read(priv, priv->mt753x_smi_addr, (reg >> 2) & 0xf);
380 if (low_word < 0)
381 return low_word;
382
383 /* Read high word */
384 high_word = mtk_mii_read(priv, priv->mt753x_smi_addr, 0x10);
385 if (high_word < 0)
386 return high_word;
387
388 if (data)
389 *data = ((u32)high_word << 16) | (low_word & 0xffff);
390
391 return 0;
392}
393
394static int mt753x_reg_write(struct mtk_eth_priv *priv, u32 reg, u32 data)
395{
396 int ret;
397
398 /* Write page address */
399 ret = mtk_mii_write(priv, priv->mt753x_smi_addr, 0x1f, reg >> 6);
400 if (ret)
401 return ret;
402
403 /* Write low word */
404 ret = mtk_mii_write(priv, priv->mt753x_smi_addr, (reg >> 2) & 0xf,
405 data & 0xffff);
406 if (ret)
407 return ret;
408
409 /* Write high word */
410 return mtk_mii_write(priv, priv->mt753x_smi_addr, 0x10, data >> 16);
411}
412
413static void mt753x_reg_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr,
414 u32 set)
415{
416 u32 val;
417
418 mt753x_reg_read(priv, reg, &val);
419 val &= ~clr;
420 val |= set;
421 mt753x_reg_write(priv, reg, val);
422}
423
424/* Indirect MDIO clause 22/45 access */
425static int mt7531_mii_rw(struct mtk_eth_priv *priv, int phy, int reg, u16 data,
426 u32 cmd, u32 st)
427{
428 ulong timeout;
429 u32 val, timeout_ms;
430 int ret = 0;
431
432 val = (st << MDIO_ST_S) |
433 ((cmd << MDIO_CMD_S) & MDIO_CMD_M) |
434 ((phy << MDIO_PHY_ADDR_S) & MDIO_PHY_ADDR_M) |
435 ((reg << MDIO_REG_ADDR_S) & MDIO_REG_ADDR_M);
436
437 if (cmd == MDIO_CMD_WRITE || cmd == MDIO_CMD_ADDR)
438 val |= data & MDIO_RW_DATA_M;
439
440 mt753x_reg_write(priv, MT7531_PHY_IAC, val | PHY_ACS_ST);
441
442 timeout_ms = 100;
443 timeout = get_timer(0);
444 while (1) {
445 mt753x_reg_read(priv, MT7531_PHY_IAC, &val);
446
447 if ((val & PHY_ACS_ST) == 0)
448 break;
449
450 if (get_timer(timeout) > timeout_ms)
451 return -ETIMEDOUT;
452 }
453
454 if (cmd == MDIO_CMD_READ || cmd == MDIO_CMD_READ_C45) {
455 mt753x_reg_read(priv, MT7531_PHY_IAC, &val);
456 ret = val & MDIO_RW_DATA_M;
457 }
458
459 return ret;
460}
461
462static int mt7531_mii_ind_read(struct mtk_eth_priv *priv, u8 phy, u8 reg)
463{
464 u8 phy_addr;
465
466 if (phy >= MT753X_NUM_PHYS)
467 return -EINVAL;
468
469 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, phy);
470
471 return mt7531_mii_rw(priv, phy_addr, reg, 0, MDIO_CMD_READ,
472 MDIO_ST_C22);
473}
474
475static int mt7531_mii_ind_write(struct mtk_eth_priv *priv, u8 phy, u8 reg,
476 u16 val)
477{
478 u8 phy_addr;
479
480 if (phy >= MT753X_NUM_PHYS)
481 return -EINVAL;
482
483 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, phy);
484
485 return mt7531_mii_rw(priv, phy_addr, reg, val, MDIO_CMD_WRITE,
486 MDIO_ST_C22);
487}
488
489int mt7531_mmd_ind_read(struct mtk_eth_priv *priv, u8 addr, u8 devad, u16 reg)
490{
491 u8 phy_addr;
492 int ret;
493
494 if (addr >= MT753X_NUM_PHYS)
495 return -EINVAL;
496
497 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, addr);
498
499 ret = mt7531_mii_rw(priv, phy_addr, devad, reg, MDIO_CMD_ADDR,
500 MDIO_ST_C45);
501 if (ret)
502 return ret;
503
504 return mt7531_mii_rw(priv, phy_addr, devad, 0, MDIO_CMD_READ_C45,
505 MDIO_ST_C45);
506}
507
508static int mt7531_mmd_ind_write(struct mtk_eth_priv *priv, u8 addr, u8 devad,
509 u16 reg, u16 val)
510{
511 u8 phy_addr;
512 int ret;
513
514 if (addr >= MT753X_NUM_PHYS)
515 return 0;
516
517 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, addr);
518
519 ret = mt7531_mii_rw(priv, phy_addr, devad, reg, MDIO_CMD_ADDR,
520 MDIO_ST_C45);
521 if (ret)
522 return ret;
523
524 return mt7531_mii_rw(priv, phy_addr, devad, val, MDIO_CMD_WRITE,
525 MDIO_ST_C45);
526}
527
Weijie Gao23f17162018-12-20 16:12:53 +0800528static int mtk_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
529{
530 struct mtk_eth_priv *priv = bus->priv;
531
532 if (devad < 0)
533 return priv->mii_read(priv, addr, reg);
534 else
535 return priv->mmd_read(priv, addr, devad, reg);
536}
537
538static int mtk_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
539 u16 val)
540{
541 struct mtk_eth_priv *priv = bus->priv;
542
543 if (devad < 0)
544 return priv->mii_write(priv, addr, reg, val);
545 else
546 return priv->mmd_write(priv, addr, devad, reg, val);
547}
548
549static int mtk_mdio_register(struct udevice *dev)
550{
551 struct mtk_eth_priv *priv = dev_get_priv(dev);
552 struct mii_dev *mdio_bus = mdio_alloc();
553 int ret;
554
555 if (!mdio_bus)
556 return -ENOMEM;
557
558 /* Assign MDIO access APIs according to the switch/phy */
559 switch (priv->sw) {
560 case SW_MT7530:
561 priv->mii_read = mtk_mii_read;
562 priv->mii_write = mtk_mii_write;
563 priv->mmd_read = mtk_mmd_ind_read;
564 priv->mmd_write = mtk_mmd_ind_write;
565 break;
Landen Chao532de8d2020-02-18 16:49:37 +0800566 case SW_MT7531:
567 priv->mii_read = mt7531_mii_ind_read;
568 priv->mii_write = mt7531_mii_ind_write;
569 priv->mmd_read = mt7531_mmd_ind_read;
570 priv->mmd_write = mt7531_mmd_ind_write;
571 break;
Weijie Gao23f17162018-12-20 16:12:53 +0800572 default:
573 priv->mii_read = mtk_mii_read;
574 priv->mii_write = mtk_mii_write;
575 priv->mmd_read = mtk_mmd_read;
576 priv->mmd_write = mtk_mmd_write;
577 }
578
579 mdio_bus->read = mtk_mdio_read;
580 mdio_bus->write = mtk_mdio_write;
581 snprintf(mdio_bus->name, sizeof(mdio_bus->name), dev->name);
582
583 mdio_bus->priv = (void *)priv;
584
585 ret = mdio_register(mdio_bus);
586
587 if (ret)
588 return ret;
589
590 priv->mdio_bus = mdio_bus;
591
592 return 0;
593}
594
Landen Chao532de8d2020-02-18 16:49:37 +0800595static int mt753x_core_reg_read(struct mtk_eth_priv *priv, u32 reg)
Weijie Gao23f17162018-12-20 16:12:53 +0800596{
Landen Chao532de8d2020-02-18 16:49:37 +0800597 u8 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, 0);
Weijie Gao23f17162018-12-20 16:12:53 +0800598
Landen Chao532de8d2020-02-18 16:49:37 +0800599 return priv->mmd_read(priv, phy_addr, 0x1f, reg);
Weijie Gao23f17162018-12-20 16:12:53 +0800600}
601
Landen Chao532de8d2020-02-18 16:49:37 +0800602static void mt753x_core_reg_write(struct mtk_eth_priv *priv, u32 reg, u32 val)
Weijie Gao23f17162018-12-20 16:12:53 +0800603{
Landen Chao532de8d2020-02-18 16:49:37 +0800604 u8 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, 0);
Weijie Gao23f17162018-12-20 16:12:53 +0800605
Landen Chao532de8d2020-02-18 16:49:37 +0800606 priv->mmd_write(priv, phy_addr, 0x1f, reg, val);
Weijie Gao23f17162018-12-20 16:12:53 +0800607}
608
609static int mt7530_pad_clk_setup(struct mtk_eth_priv *priv, int mode)
610{
611 u32 ncpo1, ssc_delta;
612
613 switch (mode) {
614 case PHY_INTERFACE_MODE_RGMII:
615 ncpo1 = 0x0c80;
616 ssc_delta = 0x87;
617 break;
618 default:
619 printf("error: xMII mode %d not supported\n", mode);
620 return -EINVAL;
621 }
622
623 /* Disable MT7530 core clock */
Landen Chao532de8d2020-02-18 16:49:37 +0800624 mt753x_core_reg_write(priv, CORE_TRGMII_GSW_CLK_CG, 0);
Weijie Gao23f17162018-12-20 16:12:53 +0800625
626 /* Disable MT7530 PLL */
Landen Chao532de8d2020-02-18 16:49:37 +0800627 mt753x_core_reg_write(priv, CORE_GSWPLL_GRP1,
Weijie Gao23f17162018-12-20 16:12:53 +0800628 (2 << RG_GSWPLL_POSDIV_200M_S) |
629 (32 << RG_GSWPLL_FBKDIV_200M_S));
630
631 /* For MT7530 core clock = 500Mhz */
Landen Chao532de8d2020-02-18 16:49:37 +0800632 mt753x_core_reg_write(priv, CORE_GSWPLL_GRP2,
Weijie Gao23f17162018-12-20 16:12:53 +0800633 (1 << RG_GSWPLL_POSDIV_500M_S) |
634 (25 << RG_GSWPLL_FBKDIV_500M_S));
635
636 /* Enable MT7530 PLL */
Landen Chao532de8d2020-02-18 16:49:37 +0800637 mt753x_core_reg_write(priv, CORE_GSWPLL_GRP1,
Weijie Gao23f17162018-12-20 16:12:53 +0800638 (2 << RG_GSWPLL_POSDIV_200M_S) |
639 (32 << RG_GSWPLL_FBKDIV_200M_S) |
640 RG_GSWPLL_EN_PRE);
641
642 udelay(20);
643
Landen Chao532de8d2020-02-18 16:49:37 +0800644 mt753x_core_reg_write(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
Weijie Gao23f17162018-12-20 16:12:53 +0800645
646 /* Setup the MT7530 TRGMII Tx Clock */
Landen Chao532de8d2020-02-18 16:49:37 +0800647 mt753x_core_reg_write(priv, CORE_PLL_GROUP5, ncpo1);
648 mt753x_core_reg_write(priv, CORE_PLL_GROUP6, 0);
649 mt753x_core_reg_write(priv, CORE_PLL_GROUP10, ssc_delta);
650 mt753x_core_reg_write(priv, CORE_PLL_GROUP11, ssc_delta);
651 mt753x_core_reg_write(priv, CORE_PLL_GROUP4, RG_SYSPLL_DDSFBK_EN |
Weijie Gao23f17162018-12-20 16:12:53 +0800652 RG_SYSPLL_BIAS_EN | RG_SYSPLL_BIAS_LPF_EN);
653
Landen Chao532de8d2020-02-18 16:49:37 +0800654 mt753x_core_reg_write(priv, CORE_PLL_GROUP2,
Weijie Gao23f17162018-12-20 16:12:53 +0800655 RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
656 (1 << RG_SYSPLL_POSDIV_S));
657
Landen Chao532de8d2020-02-18 16:49:37 +0800658 mt753x_core_reg_write(priv, CORE_PLL_GROUP7,
Weijie Gao23f17162018-12-20 16:12:53 +0800659 RG_LCDDS_PCW_NCPO_CHG | (3 << RG_LCCDS_C_S) |
660 RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
661
662 /* Enable MT7530 core clock */
Landen Chao532de8d2020-02-18 16:49:37 +0800663 mt753x_core_reg_write(priv, CORE_TRGMII_GSW_CLK_CG,
Weijie Gao23f17162018-12-20 16:12:53 +0800664 REG_GSWCK_EN | REG_TRGMIICK_EN);
665
666 return 0;
667}
668
669static int mt7530_setup(struct mtk_eth_priv *priv)
670{
671 u16 phy_addr, phy_val;
672 u32 val;
673 int i;
674
675 /* Select 250MHz clk for RGMII mode */
676 mtk_ethsys_rmw(priv, ETHSYS_CLKCFG0_REG,
677 ETHSYS_TRGMII_CLK_SEL362_5, 0);
678
Landen Chao532de8d2020-02-18 16:49:37 +0800679 /* Modify HWTRAP first to allow direct access to internal PHYs */
680 mt753x_reg_read(priv, HWTRAP_REG, &val);
681 val |= CHG_TRAP;
682 val &= ~C_MDIO_BPS;
683 mt753x_reg_write(priv, MHWTRAP_REG, val);
684
685 /* Calculate the phy base address */
686 val = ((val & SMI_ADDR_M) >> SMI_ADDR_S) << 3;
687 priv->mt753x_phy_base = (val | 0x7) + 1;
688
689 /* Turn off PHYs */
690 for (i = 0; i < MT753X_NUM_PHYS; i++) {
691 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, i);
692 phy_val = priv->mii_read(priv, phy_addr, MII_BMCR);
693 phy_val |= BMCR_PDOWN;
694 priv->mii_write(priv, phy_addr, MII_BMCR, phy_val);
695 }
696
697 /* Force MAC link down before reset */
698 mt753x_reg_write(priv, PMCR_REG(5), FORCE_MODE);
699 mt753x_reg_write(priv, PMCR_REG(6), FORCE_MODE);
700
701 /* MT7530 reset */
702 mt753x_reg_write(priv, SYS_CTRL_REG, SW_SYS_RST | SW_REG_RST);
703 udelay(100);
704
705 val = (IPG_96BIT_WITH_SHORT_IPG << IPG_CFG_S) |
706 MAC_MODE | FORCE_MODE |
707 MAC_TX_EN | MAC_RX_EN |
708 BKOFF_EN | BACKPR_EN |
709 (SPEED_1000M << FORCE_SPD_S) |
710 FORCE_DPX | FORCE_LINK;
711
712 /* MT7530 Port6: Forced 1000M/FD, FC disabled */
713 mt753x_reg_write(priv, PMCR_REG(6), val);
714
715 /* MT7530 Port5: Forced link down */
716 mt753x_reg_write(priv, PMCR_REG(5), FORCE_MODE);
717
718 /* MT7530 Port6: Set to RGMII */
719 mt753x_reg_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_M, P6_INTF_MODE_RGMII);
720
721 /* Hardware Trap: Enable Port6, Disable Port5 */
722 mt753x_reg_read(priv, HWTRAP_REG, &val);
723 val |= CHG_TRAP | LOOPDET_DIS | P5_INTF_DIS |
724 (P5_INTF_SEL_GMAC5 << P5_INTF_SEL_S) |
725 (P5_INTF_MODE_RGMII << P5_INTF_MODE_S);
726 val &= ~(C_MDIO_BPS | P6_INTF_DIS);
727 mt753x_reg_write(priv, MHWTRAP_REG, val);
728
729 /* Setup switch core pll */
730 mt7530_pad_clk_setup(priv, priv->phy_interface);
731
732 /* Lower Tx Driving for TRGMII path */
733 for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
734 mt753x_reg_write(priv, MT7530_TRGMII_TD_ODT(i),
735 (8 << TD_DM_DRVP_S) | (8 << TD_DM_DRVN_S));
736
737 for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
738 mt753x_reg_rmw(priv, MT7530_TRGMII_RD(i), RD_TAP_M, 16);
739
740 /* Turn on PHYs */
741 for (i = 0; i < MT753X_NUM_PHYS; i++) {
742 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, i);
743 phy_val = priv->mii_read(priv, phy_addr, MII_BMCR);
744 phy_val &= ~BMCR_PDOWN;
745 priv->mii_write(priv, phy_addr, MII_BMCR, phy_val);
746 }
747
748 return 0;
749}
750
751static void mt7531_core_pll_setup(struct mtk_eth_priv *priv, int mcm)
752{
753 /* Step 1 : Disable MT7531 COREPLL */
754 mt753x_reg_rmw(priv, MT7531_PLLGP_EN, EN_COREPLL, 0);
755
756 /* Step 2: switch to XTAL output */
757 mt753x_reg_rmw(priv, MT7531_PLLGP_EN, SW_CLKSW, SW_CLKSW);
758
759 mt753x_reg_rmw(priv, MT7531_PLLGP_CR0, RG_COREPLL_EN, 0);
760
761 /* Step 3: disable PLLGP and enable program PLLGP */
762 mt753x_reg_rmw(priv, MT7531_PLLGP_EN, SW_PLLGP, SW_PLLGP);
763
764 /* Step 4: program COREPLL output frequency to 500MHz */
765 mt753x_reg_rmw(priv, MT7531_PLLGP_CR0, RG_COREPLL_POSDIV_M,
766 2 << RG_COREPLL_POSDIV_S);
767 udelay(25);
768
769 /* Currently, support XTAL 25Mhz only */
770 mt753x_reg_rmw(priv, MT7531_PLLGP_CR0, RG_COREPLL_SDM_PCW_M,
771 0x140000 << RG_COREPLL_SDM_PCW_S);
772
773 /* Set feedback divide ratio update signal to high */
774 mt753x_reg_rmw(priv, MT7531_PLLGP_CR0, RG_COREPLL_SDM_PCW_CHG,
775 RG_COREPLL_SDM_PCW_CHG);
776
777 /* Wait for at least 16 XTAL clocks */
778 udelay(10);
779
780 /* Step 5: set feedback divide ratio update signal to low */
781 mt753x_reg_rmw(priv, MT7531_PLLGP_CR0, RG_COREPLL_SDM_PCW_CHG, 0);
782
783 /* add enable 325M clock for SGMII */
784 mt753x_reg_write(priv, MT7531_ANA_PLLGP_CR5, 0xad0000);
785
786 /* add enable 250SSC clock for RGMII */
787 mt753x_reg_write(priv, MT7531_ANA_PLLGP_CR2, 0x4f40000);
788
789 /*Step 6: Enable MT7531 PLL */
790 mt753x_reg_rmw(priv, MT7531_PLLGP_CR0, RG_COREPLL_EN, RG_COREPLL_EN);
791
792 mt753x_reg_rmw(priv, MT7531_PLLGP_EN, EN_COREPLL, EN_COREPLL);
793
794 udelay(25);
795}
796
797static int mt7531_port_sgmii_init(struct mtk_eth_priv *priv,
798 u32 port)
799{
800 if (port != 5 && port != 6) {
801 printf("mt7531: port %d is not a SGMII port\n", port);
802 return -EINVAL;
803 }
804
805 /* Set SGMII GEN2 speed(2.5G) */
806 mt753x_reg_rmw(priv, MT7531_PHYA_CTRL_SIGNAL3(port),
807 SGMSYS_SPEED_2500, SGMSYS_SPEED_2500);
808
809 /* Disable SGMII AN */
810 mt753x_reg_rmw(priv, MT7531_PCS_CONTROL_1(port),
811 SGMII_AN_ENABLE, 0);
812
813 /* SGMII force mode setting */
814 mt753x_reg_write(priv, MT7531_SGMII_MODE(port), SGMII_FORCE_MODE);
815
816 /* Release PHYA power down state */
817 mt753x_reg_rmw(priv, MT7531_QPHY_PWR_STATE_CTRL(port),
818 SGMII_PHYA_PWD, 0);
819
820 return 0;
821}
822
823static int mt7531_port_rgmii_init(struct mtk_eth_priv *priv, u32 port)
824{
825 u32 val;
826
827 if (port != 5) {
828 printf("error: RGMII mode is not available for port %d\n",
829 port);
830 return -EINVAL;
831 }
832
833 mt753x_reg_read(priv, MT7531_CLKGEN_CTRL, &val);
834 val |= GP_CLK_EN;
835 val &= ~GP_MODE_M;
836 val |= GP_MODE_RGMII << GP_MODE_S;
837 val |= TXCLK_NO_REVERSE;
838 val |= RXCLK_NO_DELAY;
839 val &= ~CLK_SKEW_IN_M;
840 val |= CLK_SKEW_IN_NO_CHANGE << CLK_SKEW_IN_S;
841 val &= ~CLK_SKEW_OUT_M;
842 val |= CLK_SKEW_OUT_NO_CHANGE << CLK_SKEW_OUT_S;
843 mt753x_reg_write(priv, MT7531_CLKGEN_CTRL, val);
844
845 return 0;
846}
847
848static void mt7531_phy_setting(struct mtk_eth_priv *priv)
849{
850 int i;
851 u32 val;
852
853 for (i = 0; i < MT753X_NUM_PHYS; i++) {
854 /* Enable HW auto downshift */
855 priv->mii_write(priv, i, 0x1f, 0x1);
856 val = priv->mii_read(priv, i, PHY_EXT_REG_14);
857 val |= PHY_EN_DOWN_SHFIT;
858 priv->mii_write(priv, i, PHY_EXT_REG_14, val);
859
860 /* PHY link down power saving enable */
861 val = priv->mii_read(priv, i, PHY_EXT_REG_17);
862 val |= PHY_LINKDOWN_POWER_SAVING_EN;
863 priv->mii_write(priv, i, PHY_EXT_REG_17, val);
864
865 val = priv->mmd_read(priv, i, 0x1e, PHY_DEV1E_REG_0C6);
866 val &= ~PHY_POWER_SAVING_M;
867 val |= PHY_POWER_SAVING_TX << PHY_POWER_SAVING_S;
868 priv->mmd_write(priv, i, 0x1e, PHY_DEV1E_REG_0C6, val);
869 }
870}
871
872static int mt7531_setup(struct mtk_eth_priv *priv)
873{
874 u16 phy_addr, phy_val;
875 u32 val;
876 u32 pmcr;
877 u32 port5_sgmii;
878 int i;
879
880 priv->mt753x_phy_base = (priv->mt753x_smi_addr + 1) &
881 MT753X_SMI_ADDR_MASK;
882
883 /* Turn off PHYs */
884 for (i = 0; i < MT753X_NUM_PHYS; i++) {
885 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, i);
886 phy_val = priv->mii_read(priv, phy_addr, MII_BMCR);
887 phy_val |= BMCR_PDOWN;
888 priv->mii_write(priv, phy_addr, MII_BMCR, phy_val);
889 }
890
891 /* Force MAC link down before reset */
892 mt753x_reg_write(priv, PMCR_REG(5), FORCE_MODE_LNK);
893 mt753x_reg_write(priv, PMCR_REG(6), FORCE_MODE_LNK);
894
895 /* Switch soft reset */
896 mt753x_reg_write(priv, SYS_CTRL_REG, SW_SYS_RST | SW_REG_RST);
897 udelay(100);
898
899 /* Enable MDC input Schmitt Trigger */
900 mt753x_reg_rmw(priv, MT7531_SMT0_IOLB, SMT_IOLB_5_SMI_MDC_EN,
901 SMT_IOLB_5_SMI_MDC_EN);
902
903 mt7531_core_pll_setup(priv, priv->mcm);
904
905 mt753x_reg_read(priv, MT7531_TOP_SIG_SR, &val);
906 port5_sgmii = !!(val & PAD_DUAL_SGMII_EN);
907
908 /* port5 support either RGMII or SGMII, port6 only support SGMII. */
909 switch (priv->phy_interface) {
910 case PHY_INTERFACE_MODE_RGMII:
911 if (!port5_sgmii)
912 mt7531_port_rgmii_init(priv, 5);
913 break;
914 case PHY_INTERFACE_MODE_SGMII:
915 mt7531_port_sgmii_init(priv, 6);
916 if (port5_sgmii)
917 mt7531_port_sgmii_init(priv, 5);
918 break;
919 default:
920 break;
921 }
922
923 pmcr = MT7531_FORCE_MODE |
924 (IPG_96BIT_WITH_SHORT_IPG << IPG_CFG_S) |
925 MAC_MODE | MAC_TX_EN | MAC_RX_EN |
926 BKOFF_EN | BACKPR_EN |
927 FORCE_RX_FC | FORCE_TX_FC |
928 (SPEED_1000M << FORCE_SPD_S) | FORCE_DPX |
929 FORCE_LINK;
930
931 mt753x_reg_write(priv, PMCR_REG(5), pmcr);
932 mt753x_reg_write(priv, PMCR_REG(6), pmcr);
933
934 /* Turn on PHYs */
935 for (i = 0; i < MT753X_NUM_PHYS; i++) {
936 phy_addr = MT753X_PHY_ADDR(priv->mt753x_phy_base, i);
937 phy_val = priv->mii_read(priv, phy_addr, MII_BMCR);
938 phy_val &= ~BMCR_PDOWN;
939 priv->mii_write(priv, phy_addr, MII_BMCR, phy_val);
940 }
941
942 mt7531_phy_setting(priv);
943
944 /* Enable Internal PHYs */
945 val = mt753x_core_reg_read(priv, CORE_PLL_GROUP4);
946 val |= MT7531_BYPASS_MODE;
947 val &= ~MT7531_POWER_ON_OFF;
948 mt753x_core_reg_write(priv, CORE_PLL_GROUP4, val);
949
950 return 0;
951}
952
953int mt753x_switch_init(struct mtk_eth_priv *priv)
954{
955 int ret;
956 int i;
957
Weijie Gao23f17162018-12-20 16:12:53 +0800958 /* Global reset switch */
959 if (priv->mcm) {
960 reset_assert(&priv->rst_mcm);
961 udelay(1000);
962 reset_deassert(&priv->rst_mcm);
963 mdelay(1000);
964 } else if (dm_gpio_is_valid(&priv->rst_gpio)) {
965 dm_gpio_set_value(&priv->rst_gpio, 0);
966 udelay(1000);
967 dm_gpio_set_value(&priv->rst_gpio, 1);
968 mdelay(1000);
969 }
970
Landen Chao532de8d2020-02-18 16:49:37 +0800971 ret = priv->switch_init(priv);
972 if (ret)
973 return ret;
Weijie Gao23f17162018-12-20 16:12:53 +0800974
975 /* Set port isolation */
Landen Chao532de8d2020-02-18 16:49:37 +0800976 for (i = 0; i < MT753X_NUM_PORTS; i++) {
Weijie Gao23f17162018-12-20 16:12:53 +0800977 /* Set port matrix mode */
978 if (i != 6)
Landen Chao532de8d2020-02-18 16:49:37 +0800979 mt753x_reg_write(priv, PCR_REG(i),
Weijie Gao23f17162018-12-20 16:12:53 +0800980 (0x40 << PORT_MATRIX_S));
981 else
Landen Chao532de8d2020-02-18 16:49:37 +0800982 mt753x_reg_write(priv, PCR_REG(i),
Weijie Gao23f17162018-12-20 16:12:53 +0800983 (0x3f << PORT_MATRIX_S));
984
985 /* Set port mode to user port */
Landen Chao532de8d2020-02-18 16:49:37 +0800986 mt753x_reg_write(priv, PVC_REG(i),
Weijie Gao23f17162018-12-20 16:12:53 +0800987 (0x8100 << STAG_VPID_S) |
988 (VLAN_ATTR_USER << VLAN_ATTR_S));
989 }
990
991 return 0;
992}
993
994static void mtk_phy_link_adjust(struct mtk_eth_priv *priv)
995{
996 u16 lcl_adv = 0, rmt_adv = 0;
997 u8 flowctrl;
998 u32 mcr;
999
Landen Chao532de8d2020-02-18 16:49:37 +08001000 mcr = (IPG_96BIT_WITH_SHORT_IPG << IPG_CFG_S) |
Weijie Gao23f17162018-12-20 16:12:53 +08001001 (MAC_RX_PKT_LEN_1536 << MAC_RX_PKT_LEN_S) |
1002 MAC_MODE | FORCE_MODE |
1003 MAC_TX_EN | MAC_RX_EN |
1004 BKOFF_EN | BACKPR_EN;
1005
1006 switch (priv->phydev->speed) {
1007 case SPEED_10:
1008 mcr |= (SPEED_10M << FORCE_SPD_S);
1009 break;
1010 case SPEED_100:
1011 mcr |= (SPEED_100M << FORCE_SPD_S);
1012 break;
1013 case SPEED_1000:
1014 mcr |= (SPEED_1000M << FORCE_SPD_S);
1015 break;
1016 };
1017
1018 if (priv->phydev->link)
1019 mcr |= FORCE_LINK;
1020
1021 if (priv->phydev->duplex) {
1022 mcr |= FORCE_DPX;
1023
1024 if (priv->phydev->pause)
1025 rmt_adv = LPA_PAUSE_CAP;
1026 if (priv->phydev->asym_pause)
1027 rmt_adv |= LPA_PAUSE_ASYM;
1028
1029 if (priv->phydev->advertising & ADVERTISED_Pause)
1030 lcl_adv |= ADVERTISE_PAUSE_CAP;
1031 if (priv->phydev->advertising & ADVERTISED_Asym_Pause)
1032 lcl_adv |= ADVERTISE_PAUSE_ASYM;
1033
1034 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
1035
1036 if (flowctrl & FLOW_CTRL_TX)
1037 mcr |= FORCE_TX_FC;
1038 if (flowctrl & FLOW_CTRL_RX)
1039 mcr |= FORCE_RX_FC;
1040
1041 debug("rx pause %s, tx pause %s\n",
1042 flowctrl & FLOW_CTRL_RX ? "enabled" : "disabled",
1043 flowctrl & FLOW_CTRL_TX ? "enabled" : "disabled");
1044 }
1045
1046 mtk_gmac_write(priv, GMAC_PORT_MCR(priv->gmac_id), mcr);
1047}
1048
1049static int mtk_phy_start(struct mtk_eth_priv *priv)
1050{
1051 struct phy_device *phydev = priv->phydev;
1052 int ret;
1053
1054 ret = phy_startup(phydev);
1055
1056 if (ret) {
1057 debug("Could not initialize PHY %s\n", phydev->dev->name);
1058 return ret;
1059 }
1060
1061 if (!phydev->link) {
1062 debug("%s: link down.\n", phydev->dev->name);
1063 return 0;
1064 }
1065
1066 mtk_phy_link_adjust(priv);
1067
1068 debug("Speed: %d, %s duplex%s\n", phydev->speed,
1069 (phydev->duplex) ? "full" : "half",
1070 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
1071
1072 return 0;
1073}
1074
1075static int mtk_phy_probe(struct udevice *dev)
1076{
1077 struct mtk_eth_priv *priv = dev_get_priv(dev);
1078 struct phy_device *phydev;
1079
1080 phydev = phy_connect(priv->mdio_bus, priv->phy_addr, dev,
1081 priv->phy_interface);
1082 if (!phydev)
1083 return -ENODEV;
1084
1085 phydev->supported &= PHY_GBIT_FEATURES;
1086 phydev->advertising = phydev->supported;
1087
1088 priv->phydev = phydev;
1089 phy_config(phydev);
1090
1091 return 0;
1092}
1093
MarkLeeb4ef49a2020-01-21 19:31:57 +08001094static void mtk_sgmii_init(struct mtk_eth_priv *priv)
1095{
1096 /* Set SGMII GEN2 speed(2.5G) */
1097 clrsetbits_le32(priv->sgmii_base + SGMSYS_GEN2_SPEED,
1098 SGMSYS_SPEED_2500, SGMSYS_SPEED_2500);
1099
1100 /* Disable SGMII AN */
1101 clrsetbits_le32(priv->sgmii_base + SGMSYS_PCS_CONTROL_1,
1102 SGMII_AN_ENABLE, 0);
1103
1104 /* SGMII force mode setting */
1105 writel(SGMII_FORCE_MODE, priv->sgmii_base + SGMSYS_SGMII_MODE);
1106
1107 /* Release PHYA power down state */
1108 clrsetbits_le32(priv->sgmii_base + SGMSYS_QPHY_PWR_STATE_CTRL,
1109 SGMII_PHYA_PWD, 0);
1110}
1111
Weijie Gao23f17162018-12-20 16:12:53 +08001112static void mtk_mac_init(struct mtk_eth_priv *priv)
1113{
1114 int i, ge_mode = 0;
1115 u32 mcr;
1116
1117 switch (priv->phy_interface) {
1118 case PHY_INTERFACE_MODE_RGMII_RXID:
1119 case PHY_INTERFACE_MODE_RGMII:
MarkLeeb4ef49a2020-01-21 19:31:57 +08001120 ge_mode = GE_MODE_RGMII;
1121 break;
Weijie Gao23f17162018-12-20 16:12:53 +08001122 case PHY_INTERFACE_MODE_SGMII:
1123 ge_mode = GE_MODE_RGMII;
MarkLeeb4ef49a2020-01-21 19:31:57 +08001124 mtk_ethsys_rmw(priv, ETHSYS_SYSCFG0_REG, SYSCFG0_SGMII_SEL_M,
1125 SYSCFG0_SGMII_SEL(priv->gmac_id));
1126 mtk_sgmii_init(priv);
Weijie Gao23f17162018-12-20 16:12:53 +08001127 break;
1128 case PHY_INTERFACE_MODE_MII:
1129 case PHY_INTERFACE_MODE_GMII:
1130 ge_mode = GE_MODE_MII;
1131 break;
1132 case PHY_INTERFACE_MODE_RMII:
1133 ge_mode = GE_MODE_RMII;
1134 break;
1135 default:
1136 break;
1137 }
1138
1139 /* set the gmac to the right mode */
1140 mtk_ethsys_rmw(priv, ETHSYS_SYSCFG0_REG,
1141 SYSCFG0_GE_MODE_M << SYSCFG0_GE_MODE_S(priv->gmac_id),
1142 ge_mode << SYSCFG0_GE_MODE_S(priv->gmac_id));
1143
1144 if (priv->force_mode) {
Landen Chao532de8d2020-02-18 16:49:37 +08001145 mcr = (IPG_96BIT_WITH_SHORT_IPG << IPG_CFG_S) |
Weijie Gao23f17162018-12-20 16:12:53 +08001146 (MAC_RX_PKT_LEN_1536 << MAC_RX_PKT_LEN_S) |
1147 MAC_MODE | FORCE_MODE |
1148 MAC_TX_EN | MAC_RX_EN |
1149 BKOFF_EN | BACKPR_EN |
1150 FORCE_LINK;
1151
1152 switch (priv->speed) {
1153 case SPEED_10:
1154 mcr |= SPEED_10M << FORCE_SPD_S;
1155 break;
1156 case SPEED_100:
1157 mcr |= SPEED_100M << FORCE_SPD_S;
1158 break;
1159 case SPEED_1000:
1160 mcr |= SPEED_1000M << FORCE_SPD_S;
1161 break;
1162 }
1163
1164 if (priv->duplex)
1165 mcr |= FORCE_DPX;
1166
1167 mtk_gmac_write(priv, GMAC_PORT_MCR(priv->gmac_id), mcr);
1168 }
1169
1170 if (priv->soc == SOC_MT7623) {
1171 /* Lower Tx Driving for TRGMII path */
1172 for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
1173 mtk_gmac_write(priv, GMAC_TRGMII_TD_ODT(i),
1174 (8 << TD_DM_DRVP_S) |
1175 (8 << TD_DM_DRVN_S));
1176
1177 mtk_gmac_rmw(priv, GMAC_TRGMII_RCK_CTRL, 0,
1178 RX_RST | RXC_DQSISEL);
1179 mtk_gmac_rmw(priv, GMAC_TRGMII_RCK_CTRL, RX_RST, 0);
1180 }
1181}
1182
1183static void mtk_eth_fifo_init(struct mtk_eth_priv *priv)
1184{
1185 char *pkt_base = priv->pkt_pool;
1186 int i;
1187
1188 mtk_pdma_rmw(priv, PDMA_GLO_CFG_REG, 0xffff0000, 0);
1189 udelay(500);
1190
1191 memset(priv->tx_ring_noc, 0, NUM_TX_DESC * sizeof(struct pdma_txdesc));
1192 memset(priv->rx_ring_noc, 0, NUM_RX_DESC * sizeof(struct pdma_rxdesc));
1193 memset(priv->pkt_pool, 0, TOTAL_PKT_BUF_SIZE);
1194
Frank Wunderlich47b14312020-01-31 10:23:29 +01001195 flush_dcache_range((ulong)pkt_base,
1196 (ulong)(pkt_base + TOTAL_PKT_BUF_SIZE));
Weijie Gao23f17162018-12-20 16:12:53 +08001197
1198 priv->rx_dma_owner_idx0 = 0;
1199 priv->tx_cpu_owner_idx0 = 0;
1200
1201 for (i = 0; i < NUM_TX_DESC; i++) {
1202 priv->tx_ring_noc[i].txd_info2.LS0 = 1;
1203 priv->tx_ring_noc[i].txd_info2.DDONE = 1;
1204 priv->tx_ring_noc[i].txd_info4.FPORT = priv->gmac_id + 1;
1205
1206 priv->tx_ring_noc[i].txd_info1.SDP0 = virt_to_phys(pkt_base);
1207 pkt_base += PKTSIZE_ALIGN;
1208 }
1209
1210 for (i = 0; i < NUM_RX_DESC; i++) {
1211 priv->rx_ring_noc[i].rxd_info2.PLEN0 = PKTSIZE_ALIGN;
1212 priv->rx_ring_noc[i].rxd_info1.PDP0 = virt_to_phys(pkt_base);
1213 pkt_base += PKTSIZE_ALIGN;
1214 }
1215
1216 mtk_pdma_write(priv, TX_BASE_PTR_REG(0),
1217 virt_to_phys(priv->tx_ring_noc));
1218 mtk_pdma_write(priv, TX_MAX_CNT_REG(0), NUM_TX_DESC);
1219 mtk_pdma_write(priv, TX_CTX_IDX_REG(0), priv->tx_cpu_owner_idx0);
1220
1221 mtk_pdma_write(priv, RX_BASE_PTR_REG(0),
1222 virt_to_phys(priv->rx_ring_noc));
1223 mtk_pdma_write(priv, RX_MAX_CNT_REG(0), NUM_RX_DESC);
1224 mtk_pdma_write(priv, RX_CRX_IDX_REG(0), NUM_RX_DESC - 1);
1225
1226 mtk_pdma_write(priv, PDMA_RST_IDX_REG, RST_DTX_IDX0 | RST_DRX_IDX0);
1227}
1228
1229static int mtk_eth_start(struct udevice *dev)
1230{
1231 struct mtk_eth_priv *priv = dev_get_priv(dev);
1232 int ret;
1233
1234 /* Reset FE */
1235 reset_assert(&priv->rst_fe);
1236 udelay(1000);
1237 reset_deassert(&priv->rst_fe);
1238 mdelay(10);
1239
1240 /* Packets forward to PDMA */
1241 mtk_gdma_write(priv, priv->gmac_id, GDMA_IG_CTRL_REG, GDMA_FWD_TO_CPU);
1242
1243 if (priv->gmac_id == 0)
1244 mtk_gdma_write(priv, 1, GDMA_IG_CTRL_REG, GDMA_FWD_DISCARD);
1245 else
1246 mtk_gdma_write(priv, 0, GDMA_IG_CTRL_REG, GDMA_FWD_DISCARD);
1247
1248 udelay(500);
1249
1250 mtk_eth_fifo_init(priv);
1251
1252 /* Start PHY */
1253 if (priv->sw == SW_NONE) {
1254 ret = mtk_phy_start(priv);
1255 if (ret)
1256 return ret;
1257 }
1258
1259 mtk_pdma_rmw(priv, PDMA_GLO_CFG_REG, 0,
1260 TX_WB_DDONE | RX_DMA_EN | TX_DMA_EN);
1261 udelay(500);
1262
1263 return 0;
1264}
1265
1266static void mtk_eth_stop(struct udevice *dev)
1267{
1268 struct mtk_eth_priv *priv = dev_get_priv(dev);
1269
1270 mtk_pdma_rmw(priv, PDMA_GLO_CFG_REG,
1271 TX_WB_DDONE | RX_DMA_EN | TX_DMA_EN, 0);
1272 udelay(500);
1273
1274 wait_for_bit_le32(priv->fe_base + PDMA_BASE + PDMA_GLO_CFG_REG,
1275 RX_DMA_BUSY | TX_DMA_BUSY, 0, 5000, 0);
1276}
1277
1278static int mtk_eth_write_hwaddr(struct udevice *dev)
1279{
1280 struct eth_pdata *pdata = dev_get_platdata(dev);
1281 struct mtk_eth_priv *priv = dev_get_priv(dev);
1282 unsigned char *mac = pdata->enetaddr;
1283 u32 macaddr_lsb, macaddr_msb;
1284
1285 macaddr_msb = ((u32)mac[0] << 8) | (u32)mac[1];
1286 macaddr_lsb = ((u32)mac[2] << 24) | ((u32)mac[3] << 16) |
1287 ((u32)mac[4] << 8) | (u32)mac[5];
1288
1289 mtk_gdma_write(priv, priv->gmac_id, GDMA_MAC_MSB_REG, macaddr_msb);
1290 mtk_gdma_write(priv, priv->gmac_id, GDMA_MAC_LSB_REG, macaddr_lsb);
1291
1292 return 0;
1293}
1294
1295static int mtk_eth_send(struct udevice *dev, void *packet, int length)
1296{
1297 struct mtk_eth_priv *priv = dev_get_priv(dev);
1298 u32 idx = priv->tx_cpu_owner_idx0;
1299 void *pkt_base;
1300
1301 if (!priv->tx_ring_noc[idx].txd_info2.DDONE) {
1302 debug("mtk-eth: TX DMA descriptor ring is full\n");
1303 return -EPERM;
1304 }
1305
1306 pkt_base = (void *)phys_to_virt(priv->tx_ring_noc[idx].txd_info1.SDP0);
1307 memcpy(pkt_base, packet, length);
Frank Wunderlich47b14312020-01-31 10:23:29 +01001308 flush_dcache_range((ulong)pkt_base, (ulong)pkt_base +
Weijie Gao23f17162018-12-20 16:12:53 +08001309 roundup(length, ARCH_DMA_MINALIGN));
1310
1311 priv->tx_ring_noc[idx].txd_info2.SDL0 = length;
1312 priv->tx_ring_noc[idx].txd_info2.DDONE = 0;
1313
1314 priv->tx_cpu_owner_idx0 = (priv->tx_cpu_owner_idx0 + 1) % NUM_TX_DESC;
1315 mtk_pdma_write(priv, TX_CTX_IDX_REG(0), priv->tx_cpu_owner_idx0);
1316
1317 return 0;
1318}
1319
1320static int mtk_eth_recv(struct udevice *dev, int flags, uchar **packetp)
1321{
1322 struct mtk_eth_priv *priv = dev_get_priv(dev);
1323 u32 idx = priv->rx_dma_owner_idx0;
1324 uchar *pkt_base;
1325 u32 length;
1326
1327 if (!priv->rx_ring_noc[idx].rxd_info2.DDONE) {
1328 debug("mtk-eth: RX DMA descriptor ring is empty\n");
1329 return -EAGAIN;
1330 }
1331
1332 length = priv->rx_ring_noc[idx].rxd_info2.PLEN0;
1333 pkt_base = (void *)phys_to_virt(priv->rx_ring_noc[idx].rxd_info1.PDP0);
Frank Wunderlich47b14312020-01-31 10:23:29 +01001334 invalidate_dcache_range((ulong)pkt_base, (ulong)pkt_base +
Weijie Gao23f17162018-12-20 16:12:53 +08001335 roundup(length, ARCH_DMA_MINALIGN));
1336
1337 if (packetp)
1338 *packetp = pkt_base;
1339
1340 return length;
1341}
1342
1343static int mtk_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
1344{
1345 struct mtk_eth_priv *priv = dev_get_priv(dev);
1346 u32 idx = priv->rx_dma_owner_idx0;
1347
1348 priv->rx_ring_noc[idx].rxd_info2.DDONE = 0;
1349 priv->rx_ring_noc[idx].rxd_info2.LS0 = 0;
1350 priv->rx_ring_noc[idx].rxd_info2.PLEN0 = PKTSIZE_ALIGN;
1351
1352 mtk_pdma_write(priv, RX_CRX_IDX_REG(0), idx);
1353 priv->rx_dma_owner_idx0 = (priv->rx_dma_owner_idx0 + 1) % NUM_RX_DESC;
1354
1355 return 0;
1356}
1357
1358static int mtk_eth_probe(struct udevice *dev)
1359{
1360 struct eth_pdata *pdata = dev_get_platdata(dev);
1361 struct mtk_eth_priv *priv = dev_get_priv(dev);
Frank Wunderlich47b14312020-01-31 10:23:29 +01001362 ulong iobase = pdata->iobase;
Weijie Gao23f17162018-12-20 16:12:53 +08001363 int ret;
1364
1365 /* Frame Engine Register Base */
1366 priv->fe_base = (void *)iobase;
1367
1368 /* GMAC Register Base */
1369 priv->gmac_base = (void *)(iobase + GMAC_BASE);
1370
1371 /* MDIO register */
1372 ret = mtk_mdio_register(dev);
1373 if (ret)
1374 return ret;
1375
1376 /* Prepare for tx/rx rings */
1377 priv->tx_ring_noc = (struct pdma_txdesc *)
1378 noncached_alloc(sizeof(struct pdma_txdesc) * NUM_TX_DESC,
1379 ARCH_DMA_MINALIGN);
1380 priv->rx_ring_noc = (struct pdma_rxdesc *)
1381 noncached_alloc(sizeof(struct pdma_rxdesc) * NUM_RX_DESC,
1382 ARCH_DMA_MINALIGN);
1383
1384 /* Set MAC mode */
1385 mtk_mac_init(priv);
1386
1387 /* Probe phy if switch is not specified */
1388 if (priv->sw == SW_NONE)
1389 return mtk_phy_probe(dev);
1390
1391 /* Initialize switch */
Landen Chao532de8d2020-02-18 16:49:37 +08001392 return mt753x_switch_init(priv);
Weijie Gao23f17162018-12-20 16:12:53 +08001393}
1394
1395static int mtk_eth_remove(struct udevice *dev)
1396{
1397 struct mtk_eth_priv *priv = dev_get_priv(dev);
1398
1399 /* MDIO unregister */
1400 mdio_unregister(priv->mdio_bus);
1401 mdio_free(priv->mdio_bus);
1402
1403 /* Stop possibly started DMA */
1404 mtk_eth_stop(dev);
1405
1406 return 0;
1407}
1408
1409static int mtk_eth_ofdata_to_platdata(struct udevice *dev)
1410{
1411 struct eth_pdata *pdata = dev_get_platdata(dev);
1412 struct mtk_eth_priv *priv = dev_get_priv(dev);
1413 struct ofnode_phandle_args args;
1414 struct regmap *regmap;
1415 const char *str;
1416 ofnode subnode;
1417 int ret;
1418
1419 priv->soc = dev_get_driver_data(dev);
1420
Masahiro Yamada25484932020-07-17 14:36:48 +09001421 pdata->iobase = dev_read_addr(dev);
Weijie Gao23f17162018-12-20 16:12:53 +08001422
1423 /* get corresponding ethsys phandle */
1424 ret = dev_read_phandle_with_args(dev, "mediatek,ethsys", NULL, 0, 0,
1425 &args);
1426 if (ret)
1427 return ret;
1428
1429 regmap = syscon_node_to_regmap(args.node);
1430 if (IS_ERR(regmap))
1431 return PTR_ERR(regmap);
1432
1433 priv->ethsys_base = regmap_get_range(regmap, 0);
1434 if (!priv->ethsys_base) {
1435 dev_err(dev, "Unable to find ethsys\n");
1436 return -ENODEV;
1437 }
1438
1439 /* Reset controllers */
1440 ret = reset_get_by_name(dev, "fe", &priv->rst_fe);
1441 if (ret) {
1442 printf("error: Unable to get reset ctrl for frame engine\n");
1443 return ret;
1444 }
1445
1446 priv->gmac_id = dev_read_u32_default(dev, "mediatek,gmac-id", 0);
1447
1448 /* Interface mode is required */
1449 str = dev_read_string(dev, "phy-mode");
1450 if (str) {
1451 pdata->phy_interface = phy_get_interface_by_name(str);
1452 priv->phy_interface = pdata->phy_interface;
1453 } else {
1454 printf("error: phy-mode is not set\n");
1455 return -EINVAL;
1456 }
1457
1458 /* Force mode or autoneg */
1459 subnode = ofnode_find_subnode(dev_ofnode(dev), "fixed-link");
1460 if (ofnode_valid(subnode)) {
1461 priv->force_mode = 1;
1462 priv->speed = ofnode_read_u32_default(subnode, "speed", 0);
1463 priv->duplex = ofnode_read_bool(subnode, "full-duplex");
1464
1465 if (priv->speed != SPEED_10 && priv->speed != SPEED_100 &&
1466 priv->speed != SPEED_1000) {
1467 printf("error: no valid speed set in fixed-link\n");
1468 return -EINVAL;
1469 }
1470 }
1471
MarkLeeb4ef49a2020-01-21 19:31:57 +08001472 if (priv->phy_interface == PHY_INTERFACE_MODE_SGMII) {
1473 /* get corresponding sgmii phandle */
1474 ret = dev_read_phandle_with_args(dev, "mediatek,sgmiisys",
1475 NULL, 0, 0, &args);
1476 if (ret)
1477 return ret;
1478
1479 regmap = syscon_node_to_regmap(args.node);
1480
1481 if (IS_ERR(regmap))
1482 return PTR_ERR(regmap);
1483
1484 priv->sgmii_base = regmap_get_range(regmap, 0);
1485
1486 if (!priv->sgmii_base) {
1487 dev_err(dev, "Unable to find sgmii\n");
1488 return -ENODEV;
1489 }
1490 }
1491
Weijie Gao23f17162018-12-20 16:12:53 +08001492 /* check for switch first, otherwise phy will be used */
1493 priv->sw = SW_NONE;
1494 priv->switch_init = NULL;
1495 str = dev_read_string(dev, "mediatek,switch");
1496
1497 if (str) {
1498 if (!strcmp(str, "mt7530")) {
1499 priv->sw = SW_MT7530;
1500 priv->switch_init = mt7530_setup;
Landen Chao532de8d2020-02-18 16:49:37 +08001501 priv->mt753x_smi_addr = MT753X_DFL_SMI_ADDR;
1502 } else if (!strcmp(str, "mt7531")) {
1503 priv->sw = SW_MT7531;
1504 priv->switch_init = mt7531_setup;
1505 priv->mt753x_smi_addr = MT753X_DFL_SMI_ADDR;
Weijie Gao23f17162018-12-20 16:12:53 +08001506 } else {
1507 printf("error: unsupported switch\n");
1508 return -EINVAL;
1509 }
1510
1511 priv->mcm = dev_read_bool(dev, "mediatek,mcm");
1512 if (priv->mcm) {
1513 ret = reset_get_by_name(dev, "mcm", &priv->rst_mcm);
1514 if (ret) {
1515 printf("error: no reset ctrl for mcm\n");
1516 return ret;
1517 }
1518 } else {
1519 gpio_request_by_name(dev, "reset-gpios", 0,
1520 &priv->rst_gpio, GPIOD_IS_OUT);
1521 }
1522 } else {
Weijie Gaoebb97ea2019-04-28 15:08:57 +08001523 ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0,
1524 0, &args);
1525 if (ret) {
Weijie Gao23f17162018-12-20 16:12:53 +08001526 printf("error: phy-handle is not specified\n");
1527 return ret;
1528 }
1529
Weijie Gaoebb97ea2019-04-28 15:08:57 +08001530 priv->phy_addr = ofnode_read_s32_default(args.node, "reg", -1);
Weijie Gao23f17162018-12-20 16:12:53 +08001531 if (priv->phy_addr < 0) {
1532 printf("error: phy address is not specified\n");
1533 return ret;
1534 }
1535 }
1536
1537 return 0;
1538}
1539
1540static const struct udevice_id mtk_eth_ids[] = {
1541 { .compatible = "mediatek,mt7629-eth", .data = SOC_MT7629 },
1542 { .compatible = "mediatek,mt7623-eth", .data = SOC_MT7623 },
MarkLeee3957172020-01-21 19:31:58 +08001543 { .compatible = "mediatek,mt7622-eth", .data = SOC_MT7622 },
Weijie Gao23f17162018-12-20 16:12:53 +08001544 {}
1545};
1546
1547static const struct eth_ops mtk_eth_ops = {
1548 .start = mtk_eth_start,
1549 .stop = mtk_eth_stop,
1550 .send = mtk_eth_send,
1551 .recv = mtk_eth_recv,
1552 .free_pkt = mtk_eth_free_pkt,
1553 .write_hwaddr = mtk_eth_write_hwaddr,
1554};
1555
1556U_BOOT_DRIVER(mtk_eth) = {
1557 .name = "mtk-eth",
1558 .id = UCLASS_ETH,
1559 .of_match = mtk_eth_ids,
1560 .ofdata_to_platdata = mtk_eth_ofdata_to_platdata,
1561 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1562 .probe = mtk_eth_probe,
1563 .remove = mtk_eth_remove,
1564 .ops = &mtk_eth_ops,
1565 .priv_auto_alloc_size = sizeof(struct mtk_eth_priv),
1566 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1567};