1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 MediaTek Inc.
5 * Author: Weijie Gao <weijie.gao@mediatek.com>
6 * Author: Mark Lee <mark-mc.lee@mediatek.com>
19 #include <linux/err.h>
20 #include <linux/ioport.h>
21 #include <linux/mdio.h>
22 #include <linux/mii.h>
26 #define NUM_TX_DESC 24
27 #define NUM_RX_DESC 24
28 #define TX_TOTAL_BUF_SIZE (NUM_TX_DESC * PKTSIZE_ALIGN)
29 #define RX_TOTAL_BUF_SIZE (NUM_RX_DESC * PKTSIZE_ALIGN)
30 #define TOTAL_PKT_BUF_SIZE (TX_TOTAL_BUF_SIZE + RX_TOTAL_BUF_SIZE)
32 #define MT7530_NUM_PHYS 5
33 #define MT7530_DFL_SMI_ADDR 31
35 #define MT7530_PHY_ADDR(base, addr) \
36 (((base) + (addr)) & 0x1f)
38 #define GDMA_FWD_TO_CPU \
44 (DP_PDMA << MYMAC_DP_S) | \
45 (DP_PDMA << BC_DP_S) | \
46 (DP_PDMA << MC_DP_S) | \
49 #define GDMA_FWD_DISCARD \
55 (DP_DISCARD << MYMAC_DP_S) | \
56 (DP_DISCARD << BC_DP_S) | \
57 (DP_DISCARD << MC_DP_S) | \
58 (DP_DISCARD << UN_DP_S))
60 struct pdma_rxd_info1 {
64 struct pdma_rxd_info2 {
73 struct pdma_rxd_info3 {
77 struct pdma_rxd_info4 {
91 struct pdma_rxd_info1 rxd_info1;
92 struct pdma_rxd_info2 rxd_info2;
93 struct pdma_rxd_info3 rxd_info3;
94 struct pdma_rxd_info4 rxd_info4;
97 struct pdma_txd_info1 {
101 struct pdma_txd_info2 {
110 struct pdma_txd_info3 {
114 struct pdma_txd_info4 {
125 struct pdma_txd_info1 txd_info1;
126 struct pdma_txd_info2 txd_info2;
127 struct pdma_txd_info3 txd_info3;
128 struct pdma_txd_info4 txd_info4;
141 struct mtk_eth_priv {
142 char pkt_pool[TOTAL_PKT_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
144 struct pdma_txdesc *tx_ring_noc;
145 struct pdma_rxdesc *rx_ring_noc;
147 int rx_dma_owner_idx0;
148 int tx_cpu_owner_idx0;
150 void __iomem *fe_base;
151 void __iomem *gmac_base;
152 void __iomem *ethsys_base;
154 struct mii_dev *mdio_bus;
155 int (*mii_read)(struct mtk_eth_priv *priv, u8 phy, u8 reg);
156 int (*mii_write)(struct mtk_eth_priv *priv, u8 phy, u8 reg, u16 val);
157 int (*mmd_read)(struct mtk_eth_priv *priv, u8 addr, u8 devad, u16 reg);
158 int (*mmd_write)(struct mtk_eth_priv *priv, u8 addr, u8 devad, u16 reg,
167 struct phy_device *phydev;
172 int (*switch_init)(struct mtk_eth_priv *priv);
176 struct gpio_desc rst_gpio;
179 struct reset_ctl rst_fe;
180 struct reset_ctl rst_mcm;
183 static void mtk_pdma_write(struct mtk_eth_priv *priv, u32 reg, u32 val)
185 writel(val, priv->fe_base + PDMA_BASE + reg);
188 static void mtk_pdma_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr,
191 clrsetbits_le32(priv->fe_base + PDMA_BASE + reg, clr, set);
194 static void mtk_gdma_write(struct mtk_eth_priv *priv, int no, u32 reg,
200 gdma_base = GDMA2_BASE;
202 gdma_base = GDMA1_BASE;
204 writel(val, priv->fe_base + gdma_base + reg);
207 static u32 mtk_gmac_read(struct mtk_eth_priv *priv, u32 reg)
209 return readl(priv->gmac_base + reg);
212 static void mtk_gmac_write(struct mtk_eth_priv *priv, u32 reg, u32 val)
214 writel(val, priv->gmac_base + reg);
217 static void mtk_gmac_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr, u32 set)
219 clrsetbits_le32(priv->gmac_base + reg, clr, set);
222 static void mtk_ethsys_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr,
225 clrsetbits_le32(priv->ethsys_base + reg, clr, set);
228 /* Direct MDIO clause 22/45 access via SoC */
229 static int mtk_mii_rw(struct mtk_eth_priv *priv, u8 phy, u8 reg, u16 data,
235 val = (st << MDIO_ST_S) |
236 ((cmd << MDIO_CMD_S) & MDIO_CMD_M) |
237 (((u32)phy << MDIO_PHY_ADDR_S) & MDIO_PHY_ADDR_M) |
238 (((u32)reg << MDIO_REG_ADDR_S) & MDIO_REG_ADDR_M);
240 if (cmd == MDIO_CMD_WRITE)
241 val |= data & MDIO_RW_DATA_M;
243 mtk_gmac_write(priv, GMAC_PIAC_REG, val | PHY_ACS_ST);
245 ret = wait_for_bit_le32(priv->gmac_base + GMAC_PIAC_REG,
246 PHY_ACS_ST, 0, 5000, 0);
248 pr_warn("MDIO access timeout\n");
252 if (cmd == MDIO_CMD_READ) {
253 val = mtk_gmac_read(priv, GMAC_PIAC_REG);
254 return val & MDIO_RW_DATA_M;
260 /* Direct MDIO clause 22 read via SoC */
261 static int mtk_mii_read(struct mtk_eth_priv *priv, u8 phy, u8 reg)
263 return mtk_mii_rw(priv, phy, reg, 0, MDIO_CMD_READ, MDIO_ST_C22);
266 /* Direct MDIO clause 22 write via SoC */
267 static int mtk_mii_write(struct mtk_eth_priv *priv, u8 phy, u8 reg, u16 data)
269 return mtk_mii_rw(priv, phy, reg, data, MDIO_CMD_WRITE, MDIO_ST_C22);
272 /* Direct MDIO clause 45 read via SoC */
273 static int mtk_mmd_read(struct mtk_eth_priv *priv, u8 addr, u8 devad, u16 reg)
277 ret = mtk_mii_rw(priv, addr, devad, reg, MDIO_CMD_ADDR, MDIO_ST_C45);
281 return mtk_mii_rw(priv, addr, devad, 0, MDIO_CMD_READ_C45,
285 /* Direct MDIO clause 45 write via SoC */
286 static int mtk_mmd_write(struct mtk_eth_priv *priv, u8 addr, u8 devad,
291 ret = mtk_mii_rw(priv, addr, devad, reg, MDIO_CMD_ADDR, MDIO_ST_C45);
295 return mtk_mii_rw(priv, addr, devad, val, MDIO_CMD_WRITE,
299 /* Indirect MDIO clause 45 read via MII registers */
300 static int mtk_mmd_ind_read(struct mtk_eth_priv *priv, u8 addr, u8 devad,
305 ret = priv->mii_write(priv, addr, MII_MMD_ACC_CTL_REG,
306 (MMD_ADDR << MMD_CMD_S) |
307 ((devad << MMD_DEVAD_S) & MMD_DEVAD_M));
311 ret = priv->mii_write(priv, addr, MII_MMD_ADDR_DATA_REG, reg);
315 ret = priv->mii_write(priv, addr, MII_MMD_ACC_CTL_REG,
316 (MMD_DATA << MMD_CMD_S) |
317 ((devad << MMD_DEVAD_S) & MMD_DEVAD_M));
321 return priv->mii_read(priv, addr, MII_MMD_ADDR_DATA_REG);
324 /* Indirect MDIO clause 45 write via MII registers */
325 static int mtk_mmd_ind_write(struct mtk_eth_priv *priv, u8 addr, u8 devad,
330 ret = priv->mii_write(priv, addr, MII_MMD_ACC_CTL_REG,
331 (MMD_ADDR << MMD_CMD_S) |
332 ((devad << MMD_DEVAD_S) & MMD_DEVAD_M));
336 ret = priv->mii_write(priv, addr, MII_MMD_ADDR_DATA_REG, reg);
340 ret = priv->mii_write(priv, addr, MII_MMD_ACC_CTL_REG,
341 (MMD_DATA << MMD_CMD_S) |
342 ((devad << MMD_DEVAD_S) & MMD_DEVAD_M));
346 return priv->mii_write(priv, addr, MII_MMD_ADDR_DATA_REG, val);
349 static int mtk_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
351 struct mtk_eth_priv *priv = bus->priv;
354 return priv->mii_read(priv, addr, reg);
356 return priv->mmd_read(priv, addr, devad, reg);
359 static int mtk_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
362 struct mtk_eth_priv *priv = bus->priv;
365 return priv->mii_write(priv, addr, reg, val);
367 return priv->mmd_write(priv, addr, devad, reg, val);
370 static int mtk_mdio_register(struct udevice *dev)
372 struct mtk_eth_priv *priv = dev_get_priv(dev);
373 struct mii_dev *mdio_bus = mdio_alloc();
379 /* Assign MDIO access APIs according to the switch/phy */
382 priv->mii_read = mtk_mii_read;
383 priv->mii_write = mtk_mii_write;
384 priv->mmd_read = mtk_mmd_ind_read;
385 priv->mmd_write = mtk_mmd_ind_write;
388 priv->mii_read = mtk_mii_read;
389 priv->mii_write = mtk_mii_write;
390 priv->mmd_read = mtk_mmd_read;
391 priv->mmd_write = mtk_mmd_write;
394 mdio_bus->read = mtk_mdio_read;
395 mdio_bus->write = mtk_mdio_write;
396 snprintf(mdio_bus->name, sizeof(mdio_bus->name), dev->name);
398 mdio_bus->priv = (void *)priv;
400 ret = mdio_register(mdio_bus);
405 priv->mdio_bus = mdio_bus;
411 * MT7530 Internal Register Address Bits
412 * -------------------------------------------------------------------
413 * | 15 14 13 12 11 10 9 8 7 6 | 5 4 3 2 | 1 0 |
414 * |----------------------------------------|---------------|--------|
415 * | Page Address | Reg Address | Unused |
416 * -------------------------------------------------------------------
419 static int mt7530_reg_read(struct mtk_eth_priv *priv, u32 reg, u32 *data)
421 int ret, low_word, high_word;
423 /* Write page address */
424 ret = mtk_mii_write(priv, priv->mt7530_smi_addr, 0x1f, reg >> 6);
429 low_word = mtk_mii_read(priv, priv->mt7530_smi_addr, (reg >> 2) & 0xf);
434 high_word = mtk_mii_read(priv, priv->mt7530_smi_addr, 0x10);
439 *data = ((u32)high_word << 16) | (low_word & 0xffff);
444 static int mt7530_reg_write(struct mtk_eth_priv *priv, u32 reg, u32 data)
448 /* Write page address */
449 ret = mtk_mii_write(priv, priv->mt7530_smi_addr, 0x1f, reg >> 6);
454 ret = mtk_mii_write(priv, priv->mt7530_smi_addr, (reg >> 2) & 0xf,
459 /* Write high word */
460 return mtk_mii_write(priv, priv->mt7530_smi_addr, 0x10, data >> 16);
463 static void mt7530_reg_rmw(struct mtk_eth_priv *priv, u32 reg, u32 clr,
468 mt7530_reg_read(priv, reg, &val);
471 mt7530_reg_write(priv, reg, val);
474 static void mt7530_core_reg_write(struct mtk_eth_priv *priv, u32 reg, u32 val)
476 u8 phy_addr = MT7530_PHY_ADDR(priv->mt7530_phy_base, 0);
478 mtk_mmd_ind_write(priv, phy_addr, 0x1f, reg, val);
481 static int mt7530_pad_clk_setup(struct mtk_eth_priv *priv, int mode)
483 u32 ncpo1, ssc_delta;
486 case PHY_INTERFACE_MODE_RGMII:
491 printf("error: xMII mode %d not supported\n", mode);
495 /* Disable MT7530 core clock */
496 mt7530_core_reg_write(priv, CORE_TRGMII_GSW_CLK_CG, 0);
498 /* Disable MT7530 PLL */
499 mt7530_core_reg_write(priv, CORE_GSWPLL_GRP1,
500 (2 << RG_GSWPLL_POSDIV_200M_S) |
501 (32 << RG_GSWPLL_FBKDIV_200M_S));
503 /* For MT7530 core clock = 500Mhz */
504 mt7530_core_reg_write(priv, CORE_GSWPLL_GRP2,
505 (1 << RG_GSWPLL_POSDIV_500M_S) |
506 (25 << RG_GSWPLL_FBKDIV_500M_S));
508 /* Enable MT7530 PLL */
509 mt7530_core_reg_write(priv, CORE_GSWPLL_GRP1,
510 (2 << RG_GSWPLL_POSDIV_200M_S) |
511 (32 << RG_GSWPLL_FBKDIV_200M_S) |
516 mt7530_core_reg_write(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN);
518 /* Setup the MT7530 TRGMII Tx Clock */
519 mt7530_core_reg_write(priv, CORE_PLL_GROUP5, ncpo1);
520 mt7530_core_reg_write(priv, CORE_PLL_GROUP6, 0);
521 mt7530_core_reg_write(priv, CORE_PLL_GROUP10, ssc_delta);
522 mt7530_core_reg_write(priv, CORE_PLL_GROUP11, ssc_delta);
523 mt7530_core_reg_write(priv, CORE_PLL_GROUP4, RG_SYSPLL_DDSFBK_EN |
524 RG_SYSPLL_BIAS_EN | RG_SYSPLL_BIAS_LPF_EN);
526 mt7530_core_reg_write(priv, CORE_PLL_GROUP2,
527 RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN |
528 (1 << RG_SYSPLL_POSDIV_S));
530 mt7530_core_reg_write(priv, CORE_PLL_GROUP7,
531 RG_LCDDS_PCW_NCPO_CHG | (3 << RG_LCCDS_C_S) |
532 RG_LCDDS_PWDB | RG_LCDDS_ISO_EN);
534 /* Enable MT7530 core clock */
535 mt7530_core_reg_write(priv, CORE_TRGMII_GSW_CLK_CG,
536 REG_GSWCK_EN | REG_TRGMIICK_EN);
541 static int mt7530_setup(struct mtk_eth_priv *priv)
543 u16 phy_addr, phy_val;
547 /* Select 250MHz clk for RGMII mode */
548 mtk_ethsys_rmw(priv, ETHSYS_CLKCFG0_REG,
549 ETHSYS_TRGMII_CLK_SEL362_5, 0);
551 /* Global reset switch */
553 reset_assert(&priv->rst_mcm);
555 reset_deassert(&priv->rst_mcm);
557 } else if (dm_gpio_is_valid(&priv->rst_gpio)) {
558 dm_gpio_set_value(&priv->rst_gpio, 0);
560 dm_gpio_set_value(&priv->rst_gpio, 1);
564 /* Modify HWTRAP first to allow direct access to internal PHYs */
565 mt7530_reg_read(priv, HWTRAP_REG, &val);
568 mt7530_reg_write(priv, MHWTRAP_REG, val);
570 /* Calculate the phy base address */
571 val = ((val & SMI_ADDR_M) >> SMI_ADDR_S) << 3;
572 priv->mt7530_phy_base = (val | 0x7) + 1;
575 for (i = 0; i < MT7530_NUM_PHYS; i++) {
576 phy_addr = MT7530_PHY_ADDR(priv->mt7530_phy_base, i);
577 phy_val = priv->mii_read(priv, phy_addr, MII_BMCR);
578 phy_val |= BMCR_PDOWN;
579 priv->mii_write(priv, phy_addr, MII_BMCR, phy_val);
582 /* Force MAC link down before reset */
583 mt7530_reg_write(priv, PCMR_REG(5), FORCE_MODE);
584 mt7530_reg_write(priv, PCMR_REG(6), FORCE_MODE);
587 mt7530_reg_write(priv, SYS_CTRL_REG, SW_SYS_RST | SW_REG_RST);
590 val = (1 << IPG_CFG_S) |
591 MAC_MODE | FORCE_MODE |
592 MAC_TX_EN | MAC_RX_EN |
593 BKOFF_EN | BACKPR_EN |
594 (SPEED_1000M << FORCE_SPD_S) |
595 FORCE_DPX | FORCE_LINK;
597 /* MT7530 Port6: Forced 1000M/FD, FC disabled */
598 mt7530_reg_write(priv, PCMR_REG(6), val);
600 /* MT7530 Port5: Forced link down */
601 mt7530_reg_write(priv, PCMR_REG(5), FORCE_MODE);
603 /* MT7530 Port6: Set to RGMII */
604 mt7530_reg_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_M, P6_INTF_MODE_RGMII);
606 /* Hardware Trap: Enable Port6, Disable Port5 */
607 mt7530_reg_read(priv, HWTRAP_REG, &val);
608 val |= CHG_TRAP | LOOPDET_DIS | P5_INTF_DIS |
609 (P5_INTF_SEL_GMAC5 << P5_INTF_SEL_S) |
610 (P5_INTF_MODE_RGMII << P5_INTF_MODE_S);
611 val &= ~(C_MDIO_BPS | P6_INTF_DIS);
612 mt7530_reg_write(priv, MHWTRAP_REG, val);
614 /* Setup switch core pll */
615 mt7530_pad_clk_setup(priv, priv->phy_interface);
617 /* Lower Tx Driving for TRGMII path */
618 for (i = 0 ; i < NUM_TRGMII_CTRL ; i++)
619 mt7530_reg_write(priv, MT7530_TRGMII_TD_ODT(i),
620 (8 << TD_DM_DRVP_S) | (8 << TD_DM_DRVN_S));
622 for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
623 mt7530_reg_rmw(priv, MT7530_TRGMII_RD(i), RD_TAP_M, 16);
626 for (i = 0; i < MT7530_NUM_PHYS; i++) {
627 phy_addr = MT7530_PHY_ADDR(priv->mt7530_phy_base, i);
628 phy_val = priv->mii_read(priv, phy_addr, MII_BMCR);
629 phy_val &= ~BMCR_PDOWN;
630 priv->mii_write(priv, phy_addr, MII_BMCR, phy_val);
633 /* Set port isolation */
634 for (i = 0; i < 8; i++) {
635 /* Set port matrix mode */
637 mt7530_reg_write(priv, PCR_REG(i),
638 (0x40 << PORT_MATRIX_S));
640 mt7530_reg_write(priv, PCR_REG(i),
641 (0x3f << PORT_MATRIX_S));
643 /* Set port mode to user port */
644 mt7530_reg_write(priv, PVC_REG(i),
645 (0x8100 << STAG_VPID_S) |
646 (VLAN_ATTR_USER << VLAN_ATTR_S));
652 static void mtk_phy_link_adjust(struct mtk_eth_priv *priv)
654 u16 lcl_adv = 0, rmt_adv = 0;
658 mcr = (1 << IPG_CFG_S) |
659 (MAC_RX_PKT_LEN_1536 << MAC_RX_PKT_LEN_S) |
660 MAC_MODE | FORCE_MODE |
661 MAC_TX_EN | MAC_RX_EN |
662 BKOFF_EN | BACKPR_EN;
664 switch (priv->phydev->speed) {
666 mcr |= (SPEED_10M << FORCE_SPD_S);
669 mcr |= (SPEED_100M << FORCE_SPD_S);
672 mcr |= (SPEED_1000M << FORCE_SPD_S);
676 if (priv->phydev->link)
679 if (priv->phydev->duplex) {
682 if (priv->phydev->pause)
683 rmt_adv = LPA_PAUSE_CAP;
684 if (priv->phydev->asym_pause)
685 rmt_adv |= LPA_PAUSE_ASYM;
687 if (priv->phydev->advertising & ADVERTISED_Pause)
688 lcl_adv |= ADVERTISE_PAUSE_CAP;
689 if (priv->phydev->advertising & ADVERTISED_Asym_Pause)
690 lcl_adv |= ADVERTISE_PAUSE_ASYM;
692 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
694 if (flowctrl & FLOW_CTRL_TX)
696 if (flowctrl & FLOW_CTRL_RX)
699 debug("rx pause %s, tx pause %s\n",
700 flowctrl & FLOW_CTRL_RX ? "enabled" : "disabled",
701 flowctrl & FLOW_CTRL_TX ? "enabled" : "disabled");
704 mtk_gmac_write(priv, GMAC_PORT_MCR(priv->gmac_id), mcr);
707 static int mtk_phy_start(struct mtk_eth_priv *priv)
709 struct phy_device *phydev = priv->phydev;
712 ret = phy_startup(phydev);
715 debug("Could not initialize PHY %s\n", phydev->dev->name);
720 debug("%s: link down.\n", phydev->dev->name);
724 mtk_phy_link_adjust(priv);
726 debug("Speed: %d, %s duplex%s\n", phydev->speed,
727 (phydev->duplex) ? "full" : "half",
728 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
733 static int mtk_phy_probe(struct udevice *dev)
735 struct mtk_eth_priv *priv = dev_get_priv(dev);
736 struct phy_device *phydev;
738 phydev = phy_connect(priv->mdio_bus, priv->phy_addr, dev,
739 priv->phy_interface);
743 phydev->supported &= PHY_GBIT_FEATURES;
744 phydev->advertising = phydev->supported;
746 priv->phydev = phydev;
752 static void mtk_mac_init(struct mtk_eth_priv *priv)
757 switch (priv->phy_interface) {
758 case PHY_INTERFACE_MODE_RGMII_RXID:
759 case PHY_INTERFACE_MODE_RGMII:
760 case PHY_INTERFACE_MODE_SGMII:
761 ge_mode = GE_MODE_RGMII;
763 case PHY_INTERFACE_MODE_MII:
764 case PHY_INTERFACE_MODE_GMII:
765 ge_mode = GE_MODE_MII;
767 case PHY_INTERFACE_MODE_RMII:
768 ge_mode = GE_MODE_RMII;
774 /* set the gmac to the right mode */
775 mtk_ethsys_rmw(priv, ETHSYS_SYSCFG0_REG,
776 SYSCFG0_GE_MODE_M << SYSCFG0_GE_MODE_S(priv->gmac_id),
777 ge_mode << SYSCFG0_GE_MODE_S(priv->gmac_id));
779 if (priv->force_mode) {
780 mcr = (1 << IPG_CFG_S) |
781 (MAC_RX_PKT_LEN_1536 << MAC_RX_PKT_LEN_S) |
782 MAC_MODE | FORCE_MODE |
783 MAC_TX_EN | MAC_RX_EN |
784 BKOFF_EN | BACKPR_EN |
787 switch (priv->speed) {
789 mcr |= SPEED_10M << FORCE_SPD_S;
792 mcr |= SPEED_100M << FORCE_SPD_S;
795 mcr |= SPEED_1000M << FORCE_SPD_S;
802 mtk_gmac_write(priv, GMAC_PORT_MCR(priv->gmac_id), mcr);
805 if (priv->soc == SOC_MT7623) {
806 /* Lower Tx Driving for TRGMII path */
807 for (i = 0 ; i < NUM_TRGMII_CTRL; i++)
808 mtk_gmac_write(priv, GMAC_TRGMII_TD_ODT(i),
809 (8 << TD_DM_DRVP_S) |
810 (8 << TD_DM_DRVN_S));
812 mtk_gmac_rmw(priv, GMAC_TRGMII_RCK_CTRL, 0,
813 RX_RST | RXC_DQSISEL);
814 mtk_gmac_rmw(priv, GMAC_TRGMII_RCK_CTRL, RX_RST, 0);
818 static void mtk_eth_fifo_init(struct mtk_eth_priv *priv)
820 char *pkt_base = priv->pkt_pool;
823 mtk_pdma_rmw(priv, PDMA_GLO_CFG_REG, 0xffff0000, 0);
826 memset(priv->tx_ring_noc, 0, NUM_TX_DESC * sizeof(struct pdma_txdesc));
827 memset(priv->rx_ring_noc, 0, NUM_RX_DESC * sizeof(struct pdma_rxdesc));
828 memset(priv->pkt_pool, 0, TOTAL_PKT_BUF_SIZE);
830 flush_dcache_range((u32)pkt_base, (u32)(pkt_base + TOTAL_PKT_BUF_SIZE));
832 priv->rx_dma_owner_idx0 = 0;
833 priv->tx_cpu_owner_idx0 = 0;
835 for (i = 0; i < NUM_TX_DESC; i++) {
836 priv->tx_ring_noc[i].txd_info2.LS0 = 1;
837 priv->tx_ring_noc[i].txd_info2.DDONE = 1;
838 priv->tx_ring_noc[i].txd_info4.FPORT = priv->gmac_id + 1;
840 priv->tx_ring_noc[i].txd_info1.SDP0 = virt_to_phys(pkt_base);
841 pkt_base += PKTSIZE_ALIGN;
844 for (i = 0; i < NUM_RX_DESC; i++) {
845 priv->rx_ring_noc[i].rxd_info2.PLEN0 = PKTSIZE_ALIGN;
846 priv->rx_ring_noc[i].rxd_info1.PDP0 = virt_to_phys(pkt_base);
847 pkt_base += PKTSIZE_ALIGN;
850 mtk_pdma_write(priv, TX_BASE_PTR_REG(0),
851 virt_to_phys(priv->tx_ring_noc));
852 mtk_pdma_write(priv, TX_MAX_CNT_REG(0), NUM_TX_DESC);
853 mtk_pdma_write(priv, TX_CTX_IDX_REG(0), priv->tx_cpu_owner_idx0);
855 mtk_pdma_write(priv, RX_BASE_PTR_REG(0),
856 virt_to_phys(priv->rx_ring_noc));
857 mtk_pdma_write(priv, RX_MAX_CNT_REG(0), NUM_RX_DESC);
858 mtk_pdma_write(priv, RX_CRX_IDX_REG(0), NUM_RX_DESC - 1);
860 mtk_pdma_write(priv, PDMA_RST_IDX_REG, RST_DTX_IDX0 | RST_DRX_IDX0);
863 static int mtk_eth_start(struct udevice *dev)
865 struct mtk_eth_priv *priv = dev_get_priv(dev);
869 reset_assert(&priv->rst_fe);
871 reset_deassert(&priv->rst_fe);
874 /* Packets forward to PDMA */
875 mtk_gdma_write(priv, priv->gmac_id, GDMA_IG_CTRL_REG, GDMA_FWD_TO_CPU);
877 if (priv->gmac_id == 0)
878 mtk_gdma_write(priv, 1, GDMA_IG_CTRL_REG, GDMA_FWD_DISCARD);
880 mtk_gdma_write(priv, 0, GDMA_IG_CTRL_REG, GDMA_FWD_DISCARD);
884 mtk_eth_fifo_init(priv);
887 if (priv->sw == SW_NONE) {
888 ret = mtk_phy_start(priv);
893 mtk_pdma_rmw(priv, PDMA_GLO_CFG_REG, 0,
894 TX_WB_DDONE | RX_DMA_EN | TX_DMA_EN);
900 static void mtk_eth_stop(struct udevice *dev)
902 struct mtk_eth_priv *priv = dev_get_priv(dev);
904 mtk_pdma_rmw(priv, PDMA_GLO_CFG_REG,
905 TX_WB_DDONE | RX_DMA_EN | TX_DMA_EN, 0);
908 wait_for_bit_le32(priv->fe_base + PDMA_BASE + PDMA_GLO_CFG_REG,
909 RX_DMA_BUSY | TX_DMA_BUSY, 0, 5000, 0);
912 static int mtk_eth_write_hwaddr(struct udevice *dev)
914 struct eth_pdata *pdata = dev_get_platdata(dev);
915 struct mtk_eth_priv *priv = dev_get_priv(dev);
916 unsigned char *mac = pdata->enetaddr;
917 u32 macaddr_lsb, macaddr_msb;
919 macaddr_msb = ((u32)mac[0] << 8) | (u32)mac[1];
920 macaddr_lsb = ((u32)mac[2] << 24) | ((u32)mac[3] << 16) |
921 ((u32)mac[4] << 8) | (u32)mac[5];
923 mtk_gdma_write(priv, priv->gmac_id, GDMA_MAC_MSB_REG, macaddr_msb);
924 mtk_gdma_write(priv, priv->gmac_id, GDMA_MAC_LSB_REG, macaddr_lsb);
929 static int mtk_eth_send(struct udevice *dev, void *packet, int length)
931 struct mtk_eth_priv *priv = dev_get_priv(dev);
932 u32 idx = priv->tx_cpu_owner_idx0;
935 if (!priv->tx_ring_noc[idx].txd_info2.DDONE) {
936 debug("mtk-eth: TX DMA descriptor ring is full\n");
940 pkt_base = (void *)phys_to_virt(priv->tx_ring_noc[idx].txd_info1.SDP0);
941 memcpy(pkt_base, packet, length);
942 flush_dcache_range((u32)pkt_base, (u32)pkt_base +
943 roundup(length, ARCH_DMA_MINALIGN));
945 priv->tx_ring_noc[idx].txd_info2.SDL0 = length;
946 priv->tx_ring_noc[idx].txd_info2.DDONE = 0;
948 priv->tx_cpu_owner_idx0 = (priv->tx_cpu_owner_idx0 + 1) % NUM_TX_DESC;
949 mtk_pdma_write(priv, TX_CTX_IDX_REG(0), priv->tx_cpu_owner_idx0);
954 static int mtk_eth_recv(struct udevice *dev, int flags, uchar **packetp)
956 struct mtk_eth_priv *priv = dev_get_priv(dev);
957 u32 idx = priv->rx_dma_owner_idx0;
961 if (!priv->rx_ring_noc[idx].rxd_info2.DDONE) {
962 debug("mtk-eth: RX DMA descriptor ring is empty\n");
966 length = priv->rx_ring_noc[idx].rxd_info2.PLEN0;
967 pkt_base = (void *)phys_to_virt(priv->rx_ring_noc[idx].rxd_info1.PDP0);
968 invalidate_dcache_range((u32)pkt_base, (u32)pkt_base +
969 roundup(length, ARCH_DMA_MINALIGN));
977 static int mtk_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
979 struct mtk_eth_priv *priv = dev_get_priv(dev);
980 u32 idx = priv->rx_dma_owner_idx0;
982 priv->rx_ring_noc[idx].rxd_info2.DDONE = 0;
983 priv->rx_ring_noc[idx].rxd_info2.LS0 = 0;
984 priv->rx_ring_noc[idx].rxd_info2.PLEN0 = PKTSIZE_ALIGN;
986 mtk_pdma_write(priv, RX_CRX_IDX_REG(0), idx);
987 priv->rx_dma_owner_idx0 = (priv->rx_dma_owner_idx0 + 1) % NUM_RX_DESC;
992 static int mtk_eth_probe(struct udevice *dev)
994 struct eth_pdata *pdata = dev_get_platdata(dev);
995 struct mtk_eth_priv *priv = dev_get_priv(dev);
996 u32 iobase = pdata->iobase;
999 /* Frame Engine Register Base */
1000 priv->fe_base = (void *)iobase;
1002 /* GMAC Register Base */
1003 priv->gmac_base = (void *)(iobase + GMAC_BASE);
1006 ret = mtk_mdio_register(dev);
1010 /* Prepare for tx/rx rings */
1011 priv->tx_ring_noc = (struct pdma_txdesc *)
1012 noncached_alloc(sizeof(struct pdma_txdesc) * NUM_TX_DESC,
1014 priv->rx_ring_noc = (struct pdma_rxdesc *)
1015 noncached_alloc(sizeof(struct pdma_rxdesc) * NUM_RX_DESC,
1021 /* Probe phy if switch is not specified */
1022 if (priv->sw == SW_NONE)
1023 return mtk_phy_probe(dev);
1025 /* Initialize switch */
1026 return priv->switch_init(priv);
1029 static int mtk_eth_remove(struct udevice *dev)
1031 struct mtk_eth_priv *priv = dev_get_priv(dev);
1033 /* MDIO unregister */
1034 mdio_unregister(priv->mdio_bus);
1035 mdio_free(priv->mdio_bus);
1037 /* Stop possibly started DMA */
1043 static int mtk_eth_ofdata_to_platdata(struct udevice *dev)
1045 struct eth_pdata *pdata = dev_get_platdata(dev);
1046 struct mtk_eth_priv *priv = dev_get_priv(dev);
1047 struct ofnode_phandle_args args;
1048 struct regmap *regmap;
1053 priv->soc = dev_get_driver_data(dev);
1055 pdata->iobase = devfdt_get_addr(dev);
1057 /* get corresponding ethsys phandle */
1058 ret = dev_read_phandle_with_args(dev, "mediatek,ethsys", NULL, 0, 0,
1063 regmap = syscon_node_to_regmap(args.node);
1065 return PTR_ERR(regmap);
1067 priv->ethsys_base = regmap_get_range(regmap, 0);
1068 if (!priv->ethsys_base) {
1069 dev_err(dev, "Unable to find ethsys\n");
1073 /* Reset controllers */
1074 ret = reset_get_by_name(dev, "fe", &priv->rst_fe);
1076 printf("error: Unable to get reset ctrl for frame engine\n");
1080 priv->gmac_id = dev_read_u32_default(dev, "mediatek,gmac-id", 0);
1082 /* Interface mode is required */
1083 str = dev_read_string(dev, "phy-mode");
1085 pdata->phy_interface = phy_get_interface_by_name(str);
1086 priv->phy_interface = pdata->phy_interface;
1088 printf("error: phy-mode is not set\n");
1092 /* Force mode or autoneg */
1093 subnode = ofnode_find_subnode(dev_ofnode(dev), "fixed-link");
1094 if (ofnode_valid(subnode)) {
1095 priv->force_mode = 1;
1096 priv->speed = ofnode_read_u32_default(subnode, "speed", 0);
1097 priv->duplex = ofnode_read_bool(subnode, "full-duplex");
1099 if (priv->speed != SPEED_10 && priv->speed != SPEED_100 &&
1100 priv->speed != SPEED_1000) {
1101 printf("error: no valid speed set in fixed-link\n");
1106 /* check for switch first, otherwise phy will be used */
1108 priv->switch_init = NULL;
1109 str = dev_read_string(dev, "mediatek,switch");
1112 if (!strcmp(str, "mt7530")) {
1113 priv->sw = SW_MT7530;
1114 priv->switch_init = mt7530_setup;
1115 priv->mt7530_smi_addr = MT7530_DFL_SMI_ADDR;
1117 printf("error: unsupported switch\n");
1121 priv->mcm = dev_read_bool(dev, "mediatek,mcm");
1123 ret = reset_get_by_name(dev, "mcm", &priv->rst_mcm);
1125 printf("error: no reset ctrl for mcm\n");
1129 gpio_request_by_name(dev, "reset-gpios", 0,
1130 &priv->rst_gpio, GPIOD_IS_OUT);
1133 subnode = ofnode_find_subnode(dev_ofnode(dev), "phy-handle");
1134 if (!ofnode_valid(subnode)) {
1135 printf("error: phy-handle is not specified\n");
1139 priv->phy_addr = ofnode_read_s32_default(subnode, "reg", -1);
1140 if (priv->phy_addr < 0) {
1141 printf("error: phy address is not specified\n");
1149 static const struct udevice_id mtk_eth_ids[] = {
1150 { .compatible = "mediatek,mt7629-eth", .data = SOC_MT7629 },
1151 { .compatible = "mediatek,mt7623-eth", .data = SOC_MT7623 },
1155 static const struct eth_ops mtk_eth_ops = {
1156 .start = mtk_eth_start,
1157 .stop = mtk_eth_stop,
1158 .send = mtk_eth_send,
1159 .recv = mtk_eth_recv,
1160 .free_pkt = mtk_eth_free_pkt,
1161 .write_hwaddr = mtk_eth_write_hwaddr,
1164 U_BOOT_DRIVER(mtk_eth) = {
1167 .of_match = mtk_eth_ids,
1168 .ofdata_to_platdata = mtk_eth_ofdata_to_platdata,
1169 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1170 .probe = mtk_eth_probe,
1171 .remove = mtk_eth_remove,
1172 .ops = &mtk_eth_ops,
1173 .priv_auto_alloc_size = sizeof(struct mtk_eth_priv),
1174 .flags = DM_FLAG_ALLOC_PRIV_DMA,