1 // SPDX-License-Identifier: GPL-2.0+
6 * Ethernet driver for H3/A64/A83T based SoC's
8 * It is derived from the work done by
9 * LABBE Corentin & Chen-Yu Tsai for Linux, THANKS!
15 #include <asm/cache.h>
16 #include <asm/global_data.h>
18 #include <asm/arch/clock.h>
19 #include <asm/arch/gpio.h>
23 #include <fdt_support.h>
24 #include <dm/device_compat.h>
25 #include <linux/bitops.h>
26 #include <linux/delay.h>
27 #include <linux/err.h>
32 #include <dt-bindings/pinctrl/sun4i-a10.h>
34 #if CONFIG_IS_ENABLED(DM_GPIO)
35 #include <asm-generic/gpio.h>
38 #define MDIO_CMD_MII_BUSY BIT(0)
39 #define MDIO_CMD_MII_WRITE BIT(1)
41 #define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
42 #define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
43 #define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
44 #define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
45 #define MDIO_CMD_MII_CLK_CSR_DIV_16 0x0
46 #define MDIO_CMD_MII_CLK_CSR_DIV_32 0x1
47 #define MDIO_CMD_MII_CLK_CSR_DIV_64 0x2
48 #define MDIO_CMD_MII_CLK_CSR_DIV_128 0x3
49 #define MDIO_CMD_MII_CLK_CSR_SHIFT 20
51 #define CONFIG_TX_DESCR_NUM 32
52 #define CONFIG_RX_DESCR_NUM 32
53 #define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
56 * The datasheet says that each descriptor can transfers up to 4096 bytes
57 * But later, the register documentation reduces that value to 2048,
58 * using 2048 cause strange behaviours and even BSP driver use 2047
60 #define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
62 #define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
63 #define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
65 #define H3_EPHY_DEFAULT_VALUE 0x58000
66 #define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
67 #define H3_EPHY_ADDR_SHIFT 20
68 #define REG_PHY_ADDR_MASK GENMASK(4, 0)
69 #define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
70 #define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
71 #define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
73 #define SC_RMII_EN BIT(13)
74 #define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
75 #define SC_ETCS_MASK GENMASK(1, 0)
76 #define SC_ETCS_EXT_GMII 0x1
77 #define SC_ETCS_INT_GMII 0x2
78 #define SC_ETXDC_MASK GENMASK(12, 10)
79 #define SC_ETXDC_OFFSET 10
80 #define SC_ERXDC_MASK GENMASK(9, 5)
81 #define SC_ERXDC_OFFSET 5
83 #define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
85 #define AHB_GATE_OFFSET_EPHY 0
88 #define SUN8I_IOMUX_H3 2
89 #define SUN8I_IOMUX_R40 5
90 #define SUN8I_IOMUX_H6 5
91 #define SUN8I_IOMUX_H616 2
94 /* H3/A64 EMAC Register's offset */
95 #define EMAC_CTL0 0x00
96 #define EMAC_CTL0_FULL_DUPLEX BIT(0)
97 #define EMAC_CTL0_SPEED_MASK GENMASK(3, 2)
98 #define EMAC_CTL0_SPEED_10 (0x2 << 2)
99 #define EMAC_CTL0_SPEED_100 (0x3 << 2)
100 #define EMAC_CTL0_SPEED_1000 (0x0 << 2)
101 #define EMAC_CTL1 0x04
102 #define EMAC_CTL1_SOFT_RST BIT(0)
103 #define EMAC_CTL1_BURST_LEN_SHIFT 24
104 #define EMAC_INT_STA 0x08
105 #define EMAC_INT_EN 0x0c
106 #define EMAC_TX_CTL0 0x10
107 #define EMAC_TX_CTL0_TX_EN BIT(31)
108 #define EMAC_TX_CTL1 0x14
109 #define EMAC_TX_CTL1_TX_MD BIT(1)
110 #define EMAC_TX_CTL1_TX_DMA_EN BIT(30)
111 #define EMAC_TX_CTL1_TX_DMA_START BIT(31)
112 #define EMAC_TX_FLOW_CTL 0x1c
113 #define EMAC_TX_DMA_DESC 0x20
114 #define EMAC_RX_CTL0 0x24
115 #define EMAC_RX_CTL0_RX_EN BIT(31)
116 #define EMAC_RX_CTL1 0x28
117 #define EMAC_RX_CTL1_RX_MD BIT(1)
118 #define EMAC_RX_CTL1_RX_RUNT_FRM BIT(2)
119 #define EMAC_RX_CTL1_RX_ERR_FRM BIT(3)
120 #define EMAC_RX_CTL1_RX_DMA_EN BIT(30)
121 #define EMAC_RX_CTL1_RX_DMA_START BIT(31)
122 #define EMAC_RX_DMA_DESC 0x34
123 #define EMAC_MII_CMD 0x48
124 #define EMAC_MII_DATA 0x4c
125 #define EMAC_ADDR0_HIGH 0x50
126 #define EMAC_ADDR0_LOW 0x54
127 #define EMAC_TX_DMA_STA 0xb0
128 #define EMAC_TX_CUR_DESC 0xb4
129 #define EMAC_TX_CUR_BUF 0xb8
130 #define EMAC_RX_DMA_STA 0xc0
131 #define EMAC_RX_CUR_DESC 0xc4
133 #define EMAC_DESC_OWN_DMA BIT(31)
134 #define EMAC_DESC_LAST_DESC BIT(30)
135 #define EMAC_DESC_FIRST_DESC BIT(29)
136 #define EMAC_DESC_CHAIN_SECOND BIT(24)
138 #define EMAC_DESC_RX_ERROR_MASK 0x400068db
140 DECLARE_GLOBAL_DATA_PTR;
150 struct emac_dma_desc {
155 } __aligned(ARCH_DMA_MINALIGN);
157 struct emac_eth_dev {
158 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
159 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
160 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
161 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
173 bool use_internal_phy;
175 enum emac_variant variant;
177 phys_addr_t sysctl_reg;
178 struct phy_device *phydev;
182 struct reset_ctl tx_rst;
183 struct reset_ctl ephy_rst;
184 #if CONFIG_IS_ENABLED(DM_GPIO)
185 struct gpio_desc reset_gpio;
190 struct sun8i_eth_pdata {
191 struct eth_pdata eth_pdata;
198 static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
200 struct udevice *dev = bus->priv;
201 struct emac_eth_dev *priv = dev_get_priv(dev);
205 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
206 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
207 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
208 MDIO_CMD_MII_PHY_ADDR_MASK;
211 * The EMAC clock is either 200 or 300 MHz, so we need a divider
212 * of 128 to get the MDIO frequency below the required 2.5 MHz.
214 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 << MDIO_CMD_MII_CLK_CSR_SHIFT;
216 mii_cmd |= MDIO_CMD_MII_BUSY;
218 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
220 ret = wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
221 MDIO_CMD_MII_BUSY, false,
222 CONFIG_MDIO_TIMEOUT, true);
226 return readl(priv->mac_reg + EMAC_MII_DATA);
229 static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
232 struct udevice *dev = bus->priv;
233 struct emac_eth_dev *priv = dev_get_priv(dev);
236 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
237 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
238 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
239 MDIO_CMD_MII_PHY_ADDR_MASK;
242 * The EMAC clock is either 200 or 300 MHz, so we need a divider
243 * of 128 to get the MDIO frequency below the required 2.5 MHz.
245 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 << MDIO_CMD_MII_CLK_CSR_SHIFT;
247 mii_cmd |= MDIO_CMD_MII_WRITE;
248 mii_cmd |= MDIO_CMD_MII_BUSY;
250 writel(val, priv->mac_reg + EMAC_MII_DATA);
251 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
253 return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
254 MDIO_CMD_MII_BUSY, false,
255 CONFIG_MDIO_TIMEOUT, true);
258 static int sun8i_eth_write_hwaddr(struct udevice *dev)
260 struct emac_eth_dev *priv = dev_get_priv(dev);
261 struct eth_pdata *pdata = dev_get_plat(dev);
262 uchar *mac_id = pdata->enetaddr;
263 u32 macid_lo, macid_hi;
265 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
267 macid_hi = mac_id[4] + (mac_id[5] << 8);
269 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
270 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
275 static void sun8i_adjust_link(struct emac_eth_dev *priv,
276 struct phy_device *phydev)
280 v = readl(priv->mac_reg + EMAC_CTL0);
283 v |= EMAC_CTL0_FULL_DUPLEX;
285 v &= ~EMAC_CTL0_FULL_DUPLEX;
287 v &= ~EMAC_CTL0_SPEED_MASK;
289 switch (phydev->speed) {
291 v |= EMAC_CTL0_SPEED_1000;
294 v |= EMAC_CTL0_SPEED_100;
297 v |= EMAC_CTL0_SPEED_10;
300 writel(v, priv->mac_reg + EMAC_CTL0);
303 static u32 sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 reg)
305 if (priv->use_internal_phy) {
306 /* H3 based SoC's that has an Internal 100MBit PHY
307 * needs to be configured and powered up before use
309 reg &= ~H3_EPHY_DEFAULT_MASK;
310 reg |= H3_EPHY_DEFAULT_VALUE;
311 reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
312 reg &= ~H3_EPHY_SHUTDOWN;
313 return reg | H3_EPHY_SELECT;
316 /* This is to select External Gigabit PHY on those boards with
317 * an internal PHY. Does not hurt on other SoCs. Linux does
320 return reg & ~H3_EPHY_SELECT;
323 static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
324 struct emac_eth_dev *priv)
328 if (priv->variant == R40_GMAC) {
329 /* Select RGMII for R40 */
330 reg = readl(priv->sysctl_reg + 0x164);
331 reg |= SC_ETCS_INT_GMII |
333 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
335 writel(reg, priv->sysctl_reg + 0x164);
339 reg = readl(priv->sysctl_reg + 0x30);
341 reg = sun8i_emac_set_syscon_ephy(priv, reg);
343 reg &= ~(SC_ETCS_MASK | SC_EPIT);
344 if (priv->variant == H3_EMAC ||
345 priv->variant == A64_EMAC ||
346 priv->variant == H6_EMAC)
349 switch (priv->interface) {
350 case PHY_INTERFACE_MODE_MII:
353 case PHY_INTERFACE_MODE_RGMII:
354 case PHY_INTERFACE_MODE_RGMII_ID:
355 case PHY_INTERFACE_MODE_RGMII_RXID:
356 case PHY_INTERFACE_MODE_RGMII_TXID:
357 reg |= SC_EPIT | SC_ETCS_INT_GMII;
359 case PHY_INTERFACE_MODE_RMII:
360 if (priv->variant == H3_EMAC ||
361 priv->variant == A64_EMAC ||
362 priv->variant == H6_EMAC) {
363 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
366 /* RMII not supported on A83T */
368 debug("%s: Invalid PHY interface\n", __func__);
372 if (pdata->tx_delay_ps)
373 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
376 if (pdata->rx_delay_ps)
377 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
380 writel(reg, priv->sysctl_reg + 0x30);
385 static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
387 struct phy_device *phydev;
389 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
393 phy_connect_dev(phydev, dev);
395 priv->phydev = phydev;
396 phy_config(priv->phydev);
401 #define cache_clean_descriptor(desc) \
402 flush_dcache_range((uintptr_t)(desc), \
403 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
405 #define cache_inv_descriptor(desc) \
406 invalidate_dcache_range((uintptr_t)(desc), \
407 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
409 static void rx_descs_init(struct emac_eth_dev *priv)
411 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
412 char *rxbuffs = &priv->rxbuffer[0];
413 struct emac_dma_desc *desc_p;
417 * Make sure we don't have dirty cache lines around, which could
418 * be cleaned to DRAM *after* the MAC has already written data to it.
420 invalidate_dcache_range((uintptr_t)desc_table_p,
421 (uintptr_t)desc_table_p + sizeof(priv->rx_chain));
422 invalidate_dcache_range((uintptr_t)rxbuffs,
423 (uintptr_t)rxbuffs + sizeof(priv->rxbuffer));
425 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
426 desc_p = &desc_table_p[i];
427 desc_p->buf_addr = (uintptr_t)&rxbuffs[i * CONFIG_ETH_BUFSIZE];
428 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
429 desc_p->ctl_size = CONFIG_ETH_RXSIZE;
430 desc_p->status = EMAC_DESC_OWN_DMA;
433 /* Correcting the last pointer of the chain */
434 desc_p->next = (uintptr_t)&desc_table_p[0];
436 flush_dcache_range((uintptr_t)priv->rx_chain,
437 (uintptr_t)priv->rx_chain +
438 sizeof(priv->rx_chain));
440 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
441 priv->rx_currdescnum = 0;
444 static void tx_descs_init(struct emac_eth_dev *priv)
446 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
447 char *txbuffs = &priv->txbuffer[0];
448 struct emac_dma_desc *desc_p;
451 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
452 desc_p = &desc_table_p[i];
453 desc_p->buf_addr = (uintptr_t)&txbuffs[i * CONFIG_ETH_BUFSIZE];
454 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
455 desc_p->ctl_size = 0;
459 /* Correcting the last pointer of the chain */
460 desc_p->next = (uintptr_t)&desc_table_p[0];
462 /* Flush the first TX buffer descriptor we will tell the MAC about. */
463 cache_clean_descriptor(desc_table_p);
465 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
466 priv->tx_currdescnum = 0;
469 static int sun8i_emac_eth_start(struct udevice *dev)
471 struct emac_eth_dev *priv = dev_get_priv(dev);
475 writel(EMAC_CTL1_SOFT_RST, priv->mac_reg + EMAC_CTL1);
476 ret = wait_for_bit_le32(priv->mac_reg + EMAC_CTL1,
477 EMAC_CTL1_SOFT_RST, false, 10, true);
479 printf("%s: Timeout\n", __func__);
483 /* Rewrite mac address after reset */
484 sun8i_eth_write_hwaddr(dev);
486 /* transmission starts after the full frame arrived in TX DMA FIFO */
487 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
490 * RX DMA reads data from RX DMA FIFO to host memory after a
491 * complete frame has been written to RX DMA FIFO
493 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
495 /* DMA burst length */
496 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
498 /* Initialize rx/tx descriptors */
503 ret = phy_startup(priv->phydev);
507 sun8i_adjust_link(priv, priv->phydev);
509 /* Start RX/TX DMA */
510 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN |
511 EMAC_RX_CTL1_RX_ERR_FRM | EMAC_RX_CTL1_RX_RUNT_FRM);
512 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
515 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
516 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
521 static int parse_phy_pins(struct udevice *dev)
524 const char *pin_name;
525 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
528 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
531 printf("WARNING: emac: cannot find pinctrl-0 node\n");
535 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
536 "drive-strength", ~0);
539 drive = SUN4I_PINCTRL_10_MA;
540 else if (drive <= 20)
541 drive = SUN4I_PINCTRL_20_MA;
542 else if (drive <= 30)
543 drive = SUN4I_PINCTRL_30_MA;
545 drive = SUN4I_PINCTRL_40_MA;
548 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
549 pull = SUN4I_PINCTRL_PULL_UP;
550 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
551 pull = SUN4I_PINCTRL_PULL_DOWN;
554 * The GPIO pinmux value is an integration choice, so depends on the
555 * SoC, not the EMAC variant.
557 if (IS_ENABLED(CONFIG_MACH_SUN8I_H3))
558 iomux = SUN8I_IOMUX_H3;
559 else if (IS_ENABLED(CONFIG_MACH_SUN8I_R40))
560 iomux = SUN8I_IOMUX_R40;
561 else if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
562 iomux = SUN8I_IOMUX_H6;
563 else if (IS_ENABLED(CONFIG_MACH_SUN50I_H616))
564 iomux = SUN8I_IOMUX_H616;
571 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
576 pin = sunxi_name_to_gpio(pin_name);
580 sunxi_gpio_set_cfgpin(pin, iomux);
583 sunxi_gpio_set_drv(pin, drive);
585 sunxi_gpio_set_pull(pin, pull);
589 printf("WARNING: emac: cannot find pins property\n");
596 static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
598 struct emac_eth_dev *priv = dev_get_priv(dev);
599 u32 status, desc_num = priv->rx_currdescnum;
600 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
601 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
604 /* Invalidate entire buffer descriptor */
605 cache_inv_descriptor(desc_p);
607 status = desc_p->status;
609 /* Check for DMA own bit */
610 if (status & EMAC_DESC_OWN_DMA)
613 length = (status >> 16) & 0x3fff;
615 /* make sure we read from DRAM, not our cache */
616 invalidate_dcache_range(data_start,
617 data_start + roundup(length, ARCH_DMA_MINALIGN));
619 if (status & EMAC_DESC_RX_ERROR_MASK) {
620 debug("RX: packet error: 0x%x\n",
621 status & EMAC_DESC_RX_ERROR_MASK);
625 debug("RX: Bad Packet (runt)\n");
629 if (length > CONFIG_ETH_RXSIZE) {
630 debug("RX: Too large packet (%d bytes)\n", length);
634 *packetp = (uchar *)(ulong)desc_p->buf_addr;
639 static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
641 struct emac_eth_dev *priv = dev_get_priv(dev);
642 u32 desc_num = priv->tx_currdescnum;
643 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
644 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
645 uintptr_t data_end = data_start +
646 roundup(length, ARCH_DMA_MINALIGN);
648 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
650 memcpy((void *)data_start, packet, length);
652 /* Flush data to be sent */
653 flush_dcache_range(data_start, data_end);
655 /* frame begin and end */
656 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
657 desc_p->status = EMAC_DESC_OWN_DMA;
659 /* make sure the MAC reads the actual data from DRAM */
660 cache_clean_descriptor(desc_p);
662 /* Move to next Descriptor and wrap around */
663 if (++desc_num >= CONFIG_TX_DESCR_NUM)
665 priv->tx_currdescnum = desc_num;
668 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
671 * Since we copied the data above, we return here without waiting
672 * for the packet to be actually send out.
678 static int sun8i_emac_board_setup(struct udevice *dev,
679 struct emac_eth_dev *priv)
683 ret = clk_enable(&priv->tx_clk);
685 dev_err(dev, "failed to enable TX clock\n");
689 if (reset_valid(&priv->tx_rst)) {
690 ret = reset_deassert(&priv->tx_rst);
692 dev_err(dev, "failed to deassert TX reset\n");
697 /* Only H3/H5 have clock controls for internal EPHY */
698 if (clk_valid(&priv->ephy_clk)) {
699 ret = clk_enable(&priv->ephy_clk);
701 dev_err(dev, "failed to enable EPHY TX clock\n");
706 if (reset_valid(&priv->ephy_rst)) {
707 ret = reset_deassert(&priv->ephy_rst);
709 dev_err(dev, "failed to deassert EPHY TX clock\n");
717 clk_disable(&priv->tx_clk);
721 #if CONFIG_IS_ENABLED(DM_GPIO)
722 static int sun8i_mdio_reset(struct mii_dev *bus)
724 struct udevice *dev = bus->priv;
725 struct emac_eth_dev *priv = dev_get_priv(dev);
726 struct sun8i_eth_pdata *pdata = dev_get_plat(dev);
729 if (!dm_gpio_is_valid(&priv->reset_gpio))
733 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
737 udelay(pdata->reset_delays[0]);
739 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
743 udelay(pdata->reset_delays[1]);
745 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
749 udelay(pdata->reset_delays[2]);
755 static int sun8i_mdio_init(const char *name, struct udevice *priv)
757 struct mii_dev *bus = mdio_alloc();
760 debug("Failed to allocate MDIO bus\n");
764 bus->read = sun8i_mdio_read;
765 bus->write = sun8i_mdio_write;
766 snprintf(bus->name, sizeof(bus->name), name);
767 bus->priv = (void *)priv;
768 #if CONFIG_IS_ENABLED(DM_GPIO)
769 bus->reset = sun8i_mdio_reset;
772 return mdio_register(bus);
775 static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
778 struct emac_eth_dev *priv = dev_get_priv(dev);
779 u32 desc_num = priv->rx_currdescnum;
780 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
782 /* give the current descriptor back to the MAC */
783 desc_p->status |= EMAC_DESC_OWN_DMA;
785 /* Flush Status field of descriptor */
786 cache_clean_descriptor(desc_p);
788 /* Move to next desc and wrap-around condition. */
789 if (++desc_num >= CONFIG_RX_DESCR_NUM)
791 priv->rx_currdescnum = desc_num;
796 static void sun8i_emac_eth_stop(struct udevice *dev)
798 struct emac_eth_dev *priv = dev_get_priv(dev);
800 /* Stop Rx/Tx transmitter */
801 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
802 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
805 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
806 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
808 phy_shutdown(priv->phydev);
811 static int sun8i_emac_eth_probe(struct udevice *dev)
813 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
814 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
815 struct emac_eth_dev *priv = dev_get_priv(dev);
818 priv->mac_reg = (void *)pdata->iobase;
820 ret = sun8i_emac_board_setup(dev, priv);
824 sun8i_emac_set_syscon(sun8i_pdata, priv);
826 sun8i_mdio_init(dev->name, dev);
827 priv->bus = miiphy_get_dev_by_name(dev->name);
829 return sun8i_phy_init(priv, dev);
832 static const struct eth_ops sun8i_emac_eth_ops = {
833 .start = sun8i_emac_eth_start,
834 .write_hwaddr = sun8i_eth_write_hwaddr,
835 .send = sun8i_emac_eth_send,
836 .recv = sun8i_emac_eth_recv,
837 .free_pkt = sun8i_eth_free_pkt,
838 .stop = sun8i_emac_eth_stop,
841 static int sun8i_handle_internal_phy(struct udevice *dev, struct emac_eth_dev *priv)
843 struct ofnode_phandle_args phandle;
846 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
847 NULL, 0, 0, &phandle);
851 /* If the PHY node is not a child of the internal MDIO bus, we are
852 * using some external PHY.
854 if (!ofnode_device_is_compatible(ofnode_get_parent(phandle.node),
855 "allwinner,sun8i-h3-mdio-internal"))
858 ret = clk_get_by_index_nodev(phandle.node, 0, &priv->ephy_clk);
860 dev_err(dev, "failed to get EPHY TX clock\n");
864 ret = reset_get_by_index_nodev(phandle.node, 0, &priv->ephy_rst);
866 dev_err(dev, "failed to get EPHY TX reset\n");
870 priv->use_internal_phy = true;
875 static int sun8i_emac_eth_of_to_plat(struct udevice *dev)
877 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
878 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
879 struct emac_eth_dev *priv = dev_get_priv(dev);
880 const char *phy_mode;
882 int node = dev_of_offset(dev);
884 #if CONFIG_IS_ENABLED(DM_GPIO)
885 int reset_flags = GPIOD_IS_OUT;
889 pdata->iobase = dev_read_addr(dev);
890 if (pdata->iobase == FDT_ADDR_T_NONE) {
891 debug("%s: Cannot find MAC base address\n", __func__);
895 priv->variant = dev_get_driver_data(dev);
897 if (!priv->variant) {
898 printf("%s: Missing variant\n", __func__);
902 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
904 dev_err(dev, "failed to get TX clock\n");
908 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
909 if (ret && ret != -ENOENT) {
910 dev_err(dev, "failed to get TX reset\n");
914 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
916 debug("%s: cannot find syscon node\n", __func__);
920 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
922 debug("%s: cannot find reg property in syscon node\n",
926 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
928 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
929 debug("%s: Cannot find syscon base address\n", __func__);
933 pdata->phy_interface = -1;
935 priv->use_internal_phy = false;
937 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
939 debug("%s: Cannot find PHY address\n", __func__);
942 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
944 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
947 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
948 printf("phy interface%d\n", pdata->phy_interface);
950 if (pdata->phy_interface == -1) {
951 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
955 if (priv->variant == H3_EMAC) {
956 ret = sun8i_handle_internal_phy(dev, priv);
961 priv->interface = pdata->phy_interface;
963 if (!priv->use_internal_phy)
966 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
967 "allwinner,tx-delay-ps", 0);
968 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
969 printf("%s: Invalid TX delay value %d\n", __func__,
970 sun8i_pdata->tx_delay_ps);
972 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
973 "allwinner,rx-delay-ps", 0);
974 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
975 printf("%s: Invalid RX delay value %d\n", __func__,
976 sun8i_pdata->rx_delay_ps);
978 #if CONFIG_IS_ENABLED(DM_GPIO)
979 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
980 "snps,reset-active-low"))
981 reset_flags |= GPIOD_ACTIVE_LOW;
983 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
984 &priv->reset_gpio, reset_flags);
987 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
988 "snps,reset-delays-us",
989 sun8i_pdata->reset_delays, 3);
990 } else if (ret == -ENOENT) {
998 static const struct udevice_id sun8i_emac_eth_ids[] = {
999 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
1000 {.compatible = "allwinner,sun50i-a64-emac",
1001 .data = (uintptr_t)A64_EMAC },
1002 {.compatible = "allwinner,sun8i-a83t-emac",
1003 .data = (uintptr_t)A83T_EMAC },
1004 {.compatible = "allwinner,sun8i-r40-gmac",
1005 .data = (uintptr_t)R40_GMAC },
1006 {.compatible = "allwinner,sun50i-h6-emac",
1007 .data = (uintptr_t)H6_EMAC },
1011 U_BOOT_DRIVER(eth_sun8i_emac) = {
1012 .name = "eth_sun8i_emac",
1014 .of_match = sun8i_emac_eth_ids,
1015 .of_to_plat = sun8i_emac_eth_of_to_plat,
1016 .probe = sun8i_emac_eth_probe,
1017 .ops = &sun8i_emac_eth_ops,
1018 .priv_auto = sizeof(struct emac_eth_dev),
1019 .plat_auto = sizeof(struct sun8i_eth_pdata),
1020 .flags = DM_FLAG_ALLOC_PRIV_DMA,