1 // SPDX-License-Identifier: GPL-2.0+
3 * MediaTek ethernet IP driver for U-Boot
5 * Copyright (C) 2018 Stefan Roese <sr@denx.de>
7 * This code is mostly based on the code extracted from this MediaTek
10 * https://github.com/MediaTek-Labs/linkit-smart-uboot.git
12 * I was not able to find a specific license or other developers
13 * copyrights here, so I can't add them here.
25 #include <linux/bitfield.h>
26 #include <linux/err.h>
28 /* System controller register */
29 #define MT7628_RSTCTRL_REG 0x34
30 #define RSTCTRL_EPHY_RST BIT(24)
32 #define MT7628_AGPIO_CFG_REG 0x3c
33 #define MT7628_EPHY_GPIO_AIO_EN GENMASK(20, 17)
34 #define MT7628_EPHY_P0_DIS BIT(16)
36 #define MT7628_GPIO2_MODE_REG 0x64
38 /* Ethernet frame engine register */
39 #define PDMA_RELATED 0x0800
41 #define TX_BASE_PTR0 (PDMA_RELATED + 0x000)
42 #define TX_MAX_CNT0 (PDMA_RELATED + 0x004)
43 #define TX_CTX_IDX0 (PDMA_RELATED + 0x008)
44 #define TX_DTX_IDX0 (PDMA_RELATED + 0x00c)
46 #define RX_BASE_PTR0 (PDMA_RELATED + 0x100)
47 #define RX_MAX_CNT0 (PDMA_RELATED + 0x104)
48 #define RX_CALC_IDX0 (PDMA_RELATED + 0x108)
50 #define PDMA_GLO_CFG (PDMA_RELATED + 0x204)
51 #define PDMA_RST_IDX (PDMA_RELATED + 0x208)
52 #define DLY_INT_CFG (PDMA_RELATED + 0x20c)
54 #define SDM_RELATED 0x0c00
56 #define SDM_MAC_ADRL (SDM_RELATED + 0x0c) /* MAC address LSB */
57 #define SDM_MAC_ADRH (SDM_RELATED + 0x10) /* MAC Address MSB */
59 #define RST_DTX_IDX0 BIT(0)
60 #define RST_DRX_IDX0 BIT(16)
62 #define TX_DMA_EN BIT(0)
63 #define TX_DMA_BUSY BIT(1)
64 #define RX_DMA_EN BIT(2)
65 #define RX_DMA_BUSY BIT(3)
66 #define TX_WB_DDONE BIT(6)
68 /* Ethernet switch register */
69 #define MT7628_SWITCH_FCT0 0x0008
70 #define MT7628_SWITCH_PFC1 0x0014
71 #define MT7628_SWITCH_FPA 0x0084
72 #define MT7628_SWITCH_SOCPC 0x008c
73 #define MT7628_SWITCH_POC0 0x0090
74 #define MT7628_SWITCH_POC2 0x0098
75 #define MT7628_SWITCH_SGC 0x009c
76 #define MT7628_SWITCH_PCR0 0x00c0
77 #define PCR0_PHY_ADDR GENMASK(4, 0)
78 #define PCR0_PHY_REG GENMASK(12, 8)
79 #define PCR0_WT_PHY_CMD BIT(13)
80 #define PCR0_RD_PHY_CMD BIT(14)
81 #define PCR0_WT_DATA GENMASK(31, 16)
83 #define MT7628_SWITCH_PCR1 0x00c4
84 #define PCR1_WT_DONE BIT(0)
85 #define PCR1_RD_RDY BIT(1)
86 #define PCR1_RD_DATA GENMASK(31, 16)
88 #define MT7628_SWITCH_FPA1 0x00c8
89 #define MT7628_SWITCH_FCT2 0x00cc
90 #define MT7628_SWITCH_SGC2 0x00e4
91 #define MT7628_SWITCH_BMU_CTRL 0x0110
94 #define RX_DMA_DONE BIT(31)
95 #define RX_DMA_LSO BIT(30)
96 #define RX_DMA_PLEN0 GENMASK(29, 16)
97 #define RX_DMA_TAG BIT(15)
104 } __packed __aligned(4);
106 #define TX_DMA_PLEN0 GENMASK(29, 16)
107 #define TX_DMA_LS1 BIT(14)
108 #define TX_DMA_LS0 BIT(30)
109 #define TX_DMA_DONE BIT(31)
111 #define TX_DMA_INS_VLAN_MT7621 BIT(16)
112 #define TX_DMA_INS_VLAN BIT(7)
113 #define TX_DMA_INS_PPPOE BIT(12)
114 #define TX_DMA_PN GENMASK(26, 24)
121 } __packed __aligned(4);
123 #define NUM_RX_DESC 256
124 #define NUM_TX_DESC 4
126 #define PADDING_LENGTH 60
128 #define MTK_QDMA_PAGE_SIZE 2048
130 #define CONFIG_MDIO_TIMEOUT 100
131 #define CONFIG_DMA_STOP_TIMEOUT 100
132 #define CONFIG_TX_DMA_TIMEOUT 100
134 #define LINK_DELAY_TIME 500 /* 500 ms */
135 #define LINK_TIMEOUT 10000 /* 10 seconds */
137 struct mt7628_eth_dev {
138 void __iomem *base; /* frame engine base address */
139 void __iomem *eth_sw_base; /* switch base address */
140 struct regmap *sysctrl_regmap; /* system-controller reg-map */
144 struct fe_tx_dma *tx_ring;
145 struct fe_rx_dma *rx_ring;
147 u8 *rx_buf[NUM_RX_DESC];
149 /* Point to the next RXD DMA wants to use in RXD Ring0 */
151 /* Point to the next TXD in TXD Ring0 CPU wants to use */
155 static int mdio_wait_read(struct mt7628_eth_dev *priv, u32 mask, bool mask_set)
157 void __iomem *base = priv->eth_sw_base;
160 ret = wait_for_bit_le32(base + MT7628_SWITCH_PCR1, mask, mask_set,
161 CONFIG_MDIO_TIMEOUT, false);
163 printf("MDIO operation timeout!\n");
170 static int mii_mgr_read(struct mt7628_eth_dev *priv,
171 u32 phy_addr, u32 phy_register, u32 *read_data)
173 void __iomem *base = priv->eth_sw_base;
178 /* Make sure previous read operation is complete */
179 ret = mdio_wait_read(priv, PCR1_RD_RDY, false);
183 writel(PCR0_RD_PHY_CMD |
184 FIELD_PREP(PCR0_PHY_REG, phy_register) |
185 FIELD_PREP(PCR0_PHY_ADDR, phy_addr),
186 base + MT7628_SWITCH_PCR0);
188 /* Make sure previous read operation is complete */
189 ret = mdio_wait_read(priv, PCR1_RD_RDY, true);
193 status = readl(base + MT7628_SWITCH_PCR1);
194 *read_data = FIELD_GET(PCR1_RD_DATA, status);
199 static int mii_mgr_write(struct mt7628_eth_dev *priv,
200 u32 phy_addr, u32 phy_register, u32 write_data)
202 void __iomem *base = priv->eth_sw_base;
206 /* Make sure previous write operation is complete */
207 ret = mdio_wait_read(priv, PCR1_WT_DONE, false);
211 data = FIELD_PREP(PCR0_WT_DATA, write_data) |
212 FIELD_PREP(PCR0_PHY_REG, phy_register) |
213 FIELD_PREP(PCR0_PHY_ADDR, phy_addr) |
215 writel(data, base + MT7628_SWITCH_PCR0);
217 return mdio_wait_read(priv, PCR1_WT_DONE, true);
220 static int mt7628_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
225 ret = mii_mgr_read(bus->priv, addr, reg, &val);
232 static int mt7628_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
235 return mii_mgr_write(bus->priv, addr, reg, value);
238 static void mt7628_ephy_init(struct mt7628_eth_dev *priv)
242 mii_mgr_write(priv, 0, 31, 0x2000); /* change G2 page */
243 mii_mgr_write(priv, 0, 26, 0x0000);
245 for (i = 0; i < 5; i++) {
246 mii_mgr_write(priv, i, 31, 0x8000); /* change L0 page */
247 mii_mgr_write(priv, i, 0, 0x3100);
250 mii_mgr_write(priv, i, 30, 0xa000);
251 mii_mgr_write(priv, i, 31, 0xa000); /* change L2 page */
252 mii_mgr_write(priv, i, 16, 0x0606);
253 mii_mgr_write(priv, i, 23, 0x0f0e);
254 mii_mgr_write(priv, i, 24, 0x1610);
255 mii_mgr_write(priv, i, 30, 0x1f15);
256 mii_mgr_write(priv, i, 28, 0x6111);
259 /* 100Base AOI setting */
260 mii_mgr_write(priv, 0, 31, 0x5000); /* change G5 page */
261 mii_mgr_write(priv, 0, 19, 0x004a);
262 mii_mgr_write(priv, 0, 20, 0x015a);
263 mii_mgr_write(priv, 0, 21, 0x00ee);
264 mii_mgr_write(priv, 0, 22, 0x0033);
265 mii_mgr_write(priv, 0, 23, 0x020a);
266 mii_mgr_write(priv, 0, 24, 0x0000);
267 mii_mgr_write(priv, 0, 25, 0x024a);
268 mii_mgr_write(priv, 0, 26, 0x035a);
269 mii_mgr_write(priv, 0, 27, 0x02ee);
270 mii_mgr_write(priv, 0, 28, 0x0233);
271 mii_mgr_write(priv, 0, 29, 0x000a);
272 mii_mgr_write(priv, 0, 30, 0x0000);
274 /* Fix EPHY idle state abnormal behavior */
275 mii_mgr_write(priv, 0, 31, 0x4000); /* change G4 page */
276 mii_mgr_write(priv, 0, 29, 0x000d);
277 mii_mgr_write(priv, 0, 30, 0x0500);
280 static void rt305x_esw_init(struct mt7628_eth_dev *priv)
282 void __iomem *base = priv->eth_sw_base;
285 * FC_RLS_TH=200, FC_SET_TH=160
286 * DROP_RLS=120, DROP_SET_TH=80
288 writel(0xc8a07850, base + MT7628_SWITCH_FCT0);
289 writel(0x00000000, base + MT7628_SWITCH_SGC2);
290 writel(0x00405555, base + MT7628_SWITCH_PFC1);
291 writel(0x00007f7f, base + MT7628_SWITCH_POC0);
292 writel(0x00007f7f, base + MT7628_SWITCH_POC2); /* disable VLAN */
293 writel(0x0002500c, base + MT7628_SWITCH_FCT2);
294 /* hashing algorithm=XOR48, aging interval=300sec */
295 writel(0x0008a301, base + MT7628_SWITCH_SGC);
296 writel(0x02404040, base + MT7628_SWITCH_SOCPC);
298 /* Ext PHY Addr=0x1f */
299 writel(0x3f502b28, base + MT7628_SWITCH_FPA1);
300 writel(0x00000000, base + MT7628_SWITCH_FPA);
301 /* 1us cycle number=125 (FE's clock=125Mhz) */
302 writel(0x7d000000, base + MT7628_SWITCH_BMU_CTRL);
304 /* Configure analog GPIO setup */
305 regmap_update_bits(priv->sysctrl_regmap, MT7628_AGPIO_CFG_REG,
306 MT7628_EPHY_P0_DIS, MT7628_EPHY_GPIO_AIO_EN);
309 regmap_update_bits(priv->sysctrl_regmap, MT7628_RSTCTRL_REG,
310 0, RSTCTRL_EPHY_RST);
311 regmap_update_bits(priv->sysctrl_regmap, MT7628_RSTCTRL_REG,
312 RSTCTRL_EPHY_RST, 0);
315 /* Set P0 EPHY LED mode */
316 regmap_update_bits(priv->sysctrl_regmap, MT7628_GPIO2_MODE_REG,
317 0x0ffc0ffc, 0x05540554);
320 mt7628_ephy_init(priv);
323 static void eth_dma_start(struct mt7628_eth_dev *priv)
325 void __iomem *base = priv->base;
327 setbits_le32(base + PDMA_GLO_CFG, TX_WB_DDONE | RX_DMA_EN | TX_DMA_EN);
330 static void eth_dma_stop(struct mt7628_eth_dev *priv)
332 void __iomem *base = priv->base;
335 clrbits_le32(base + PDMA_GLO_CFG, TX_WB_DDONE | RX_DMA_EN | TX_DMA_EN);
337 /* Wait for DMA to stop */
338 ret = wait_for_bit_le32(base + PDMA_GLO_CFG,
339 RX_DMA_BUSY | TX_DMA_BUSY, false,
340 CONFIG_DMA_STOP_TIMEOUT, false);
342 printf("DMA stop timeout error!\n");
345 static int mt7628_eth_write_hwaddr(struct udevice *dev)
347 struct mt7628_eth_dev *priv = dev_get_priv(dev);
348 void __iomem *base = priv->base;
349 u8 *addr = ((struct eth_pdata *)dev_get_platdata(dev))->enetaddr;
352 /* Set MAC address. */
354 val = (val << 8) | addr[1];
355 writel(val, base + SDM_MAC_ADRH);
358 val = (val << 8) | addr[3];
359 val = (val << 8) | addr[4];
360 val = (val << 8) | addr[5];
361 writel(val, base + SDM_MAC_ADRL);
366 static int mt7628_eth_send(struct udevice *dev, void *packet, int length)
368 struct mt7628_eth_dev *priv = dev_get_priv(dev);
369 void __iomem *base = priv->base;
374 idx = priv->tx_dma_idx;
376 /* Pad message to a minimum length */
377 if (length < PADDING_LENGTH) {
378 char *p = (char *)packet;
380 for (i = 0; i < PADDING_LENGTH - length; i++)
382 length = PADDING_LENGTH;
385 /* Check if buffer is ready for next TX DMA */
386 ret = wait_for_bit_le32(&priv->tx_ring[idx].txd2, TX_DMA_DONE, true,
387 CONFIG_TX_DMA_TIMEOUT, false);
389 printf("TX: DMA still busy on buffer %d\n", idx);
393 flush_dcache_range((u32)packet, (u32)packet + length);
395 priv->tx_ring[idx].txd1 = CPHYSADDR(packet);
396 priv->tx_ring[idx].txd2 &= ~TX_DMA_PLEN0;
397 priv->tx_ring[idx].txd2 |= FIELD_PREP(TX_DMA_PLEN0, length);
398 priv->tx_ring[idx].txd2 &= ~TX_DMA_DONE;
400 idx = (idx + 1) % NUM_TX_DESC;
402 /* Make sure the writes executed at this place */
404 writel(idx, base + TX_CTX_IDX0);
406 priv->tx_dma_idx = idx;
411 static int mt7628_eth_recv(struct udevice *dev, int flags, uchar **packetp)
413 struct mt7628_eth_dev *priv = dev_get_priv(dev);
418 idx = priv->rx_dma_idx;
420 rxd_info = priv->rx_ring[idx].rxd2;
421 if ((rxd_info & RX_DMA_DONE) == 0)
424 length = FIELD_GET(RX_DMA_PLEN0, priv->rx_ring[idx].rxd2);
425 if (length == 0 || length > MTK_QDMA_PAGE_SIZE) {
426 printf("%s: invalid length (%d bytes)\n", __func__, length);
430 *packetp = priv->rx_buf[idx];
431 invalidate_dcache_range((u32)*packetp, (u32)*packetp + length);
433 priv->rx_ring[idx].rxd4 = 0;
434 priv->rx_ring[idx].rxd2 = RX_DMA_LSO;
436 /* Make sure the writes executed at this place */
442 static int mt7628_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
444 struct mt7628_eth_dev *priv = dev_get_priv(dev);
445 void __iomem *base = priv->base;
448 idx = priv->rx_dma_idx;
450 /* Move point to next RXD which wants to alloc */
451 writel(idx, base + RX_CALC_IDX0);
453 /* Update to Next packet point that was received */
454 idx = (idx + 1) % NUM_RX_DESC;
456 priv->rx_dma_idx = idx;
461 static int phy_link_up(struct mt7628_eth_dev *priv)
465 mii_mgr_read(priv, 0x00, MII_BMSR, &val);
466 return !!(val & BMSR_LSTATUS);
469 static int mt7628_eth_start(struct udevice *dev)
471 struct mt7628_eth_dev *priv = dev_get_priv(dev);
472 void __iomem *base = priv->base;
473 uchar packet[MTK_QDMA_PAGE_SIZE];
477 for (i = 0; i < NUM_RX_DESC; i++) {
478 memset((void *)&priv->rx_ring[i], 0, sizeof(priv->rx_ring[0]));
479 priv->rx_ring[i].rxd2 |= RX_DMA_LSO;
480 priv->rx_ring[i].rxd1 = CPHYSADDR(priv->rx_buf[i]);
483 for (i = 0; i < NUM_TX_DESC; i++) {
484 memset((void *)&priv->tx_ring[i], 0, sizeof(priv->tx_ring[0]));
485 priv->tx_ring[i].txd2 = TX_DMA_LS0 | TX_DMA_DONE;
486 priv->tx_ring[i].txd4 = FIELD_PREP(TX_DMA_PN, 1);
489 priv->rx_dma_idx = 0;
490 priv->tx_dma_idx = 0;
492 /* Make sure the writes executed at this place */
495 /* disable delay interrupt */
496 writel(0, base + DLY_INT_CFG);
498 clrbits_le32(base + PDMA_GLO_CFG, 0xffff0000);
500 /* Tell the adapter where the TX/RX rings are located. */
501 writel(CPHYSADDR(&priv->rx_ring[0]), base + RX_BASE_PTR0);
502 writel(CPHYSADDR((u32)&priv->tx_ring[0]), base + TX_BASE_PTR0);
504 writel(NUM_RX_DESC, base + RX_MAX_CNT0);
505 writel(NUM_TX_DESC, base + TX_MAX_CNT0);
507 writel(priv->tx_dma_idx, base + TX_CTX_IDX0);
508 writel(RST_DTX_IDX0, base + PDMA_RST_IDX);
510 writel(NUM_RX_DESC - 1, base + RX_CALC_IDX0);
511 writel(RST_DRX_IDX0, base + PDMA_RST_IDX);
513 /* Make sure the writes executed at this place */
517 /* Check if link is not up yet */
518 if (!phy_link_up(priv)) {
519 /* Wait for link to come up */
521 printf("Waiting for link to come up .");
522 for (i = 0; i < (LINK_TIMEOUT / LINK_DELAY_TIME); i++) {
523 mdelay(LINK_DELAY_TIME);
524 if (phy_link_up(priv)) {
525 mdelay(100); /* Ensure all is ready */
532 if (phy_link_up(priv))
535 printf(" timeout! Trying anyways\n");
539 * The integrated switch seems to queue some received ethernet
540 * packets in some FIFO. Lets read the already queued packets
541 * out by using the receive routine, so that these old messages
542 * are dropped before the new xfer starts.
544 packetp = &packet[0];
545 while (mt7628_eth_recv(dev, 0, &packetp) != -EAGAIN)
546 mt7628_eth_free_pkt(dev, packetp, 0);
551 static void mt7628_eth_stop(struct udevice *dev)
553 struct mt7628_eth_dev *priv = dev_get_priv(dev);
558 static int mt7628_eth_probe(struct udevice *dev)
560 struct mt7628_eth_dev *priv = dev_get_priv(dev);
561 struct udevice *syscon;
566 /* Save frame-engine base address for later use */
567 priv->base = dev_remap_addr_index(dev, 0);
568 if (IS_ERR(priv->base))
569 return PTR_ERR(priv->base);
571 /* Save switch base address for later use */
572 priv->eth_sw_base = dev_remap_addr_index(dev, 1);
573 if (IS_ERR(priv->eth_sw_base))
574 return PTR_ERR(priv->eth_sw_base);
576 /* Get system controller regmap */
577 ret = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
580 pr_err("unable to find syscon device\n");
584 priv->sysctrl_regmap = syscon_get_regmap(syscon);
585 if (!priv->sysctrl_regmap) {
586 pr_err("unable to find regmap\n");
590 /* Put rx and tx rings into KSEG1 area (uncached) */
591 priv->tx_ring = (struct fe_tx_dma *)
592 KSEG1ADDR(memalign(ARCH_DMA_MINALIGN,
593 sizeof(*priv->tx_ring) * NUM_TX_DESC));
594 priv->rx_ring = (struct fe_rx_dma *)
595 KSEG1ADDR(memalign(ARCH_DMA_MINALIGN,
596 sizeof(*priv->rx_ring) * NUM_RX_DESC));
598 for (i = 0; i < NUM_RX_DESC; i++)
599 priv->rx_buf[i] = memalign(PKTALIGN, MTK_QDMA_PAGE_SIZE);
603 printf("Failed to allocate MDIO bus\n");
607 bus->read = mt7628_mdio_read;
608 bus->write = mt7628_mdio_write;
609 snprintf(bus->name, sizeof(bus->name), dev->name);
610 bus->priv = (void *)priv;
612 ret = mdio_register(bus);
616 /* Switch configuration */
617 rt305x_esw_init(priv);
622 static const struct eth_ops mt7628_eth_ops = {
623 .start = mt7628_eth_start,
624 .send = mt7628_eth_send,
625 .recv = mt7628_eth_recv,
626 .free_pkt = mt7628_eth_free_pkt,
627 .stop = mt7628_eth_stop,
628 .write_hwaddr = mt7628_eth_write_hwaddr,
631 static const struct udevice_id mt7628_eth_ids[] = {
632 { .compatible = "mediatek,mt7628-eth" },
636 U_BOOT_DRIVER(mt7628_eth) = {
637 .name = "mt7628_eth",
639 .of_match = mt7628_eth_ids,
640 .probe = mt7628_eth_probe,
641 .ops = &mt7628_eth_ops,
642 .priv_auto_alloc_size = sizeof(struct mt7628_eth_dev),
643 .platdata_auto_alloc_size = sizeof(struct eth_pdata),