1 // SPDX-License-Identifier: GPL-2.0+
4 * Author: Amit Singh Tomar, amittomer25@gmail.com
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!
14 #include <asm/arch/clock.h>
15 #include <asm/arch/gpio.h>
19 #include <fdt_support.h>
20 #include <linux/err.h>
25 #include <dt-bindings/pinctrl/sun4i-a10.h>
27 #include <asm-generic/gpio.h>
30 #define MDIO_CMD_MII_BUSY BIT(0)
31 #define MDIO_CMD_MII_WRITE BIT(1)
33 #define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
34 #define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
35 #define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
36 #define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
38 #define CONFIG_TX_DESCR_NUM 32
39 #define CONFIG_RX_DESCR_NUM 32
40 #define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
43 * The datasheet says that each descriptor can transfers up to 4096 bytes
44 * But later, the register documentation reduces that value to 2048,
45 * using 2048 cause strange behaviours and even BSP driver use 2047
47 #define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
49 #define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
50 #define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
52 #define H3_EPHY_DEFAULT_VALUE 0x58000
53 #define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
54 #define H3_EPHY_ADDR_SHIFT 20
55 #define REG_PHY_ADDR_MASK GENMASK(4, 0)
56 #define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
57 #define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
58 #define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
60 #define SC_RMII_EN BIT(13)
61 #define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
62 #define SC_ETCS_MASK GENMASK(1, 0)
63 #define SC_ETCS_EXT_GMII 0x1
64 #define SC_ETCS_INT_GMII 0x2
65 #define SC_ETXDC_MASK GENMASK(12, 10)
66 #define SC_ETXDC_OFFSET 10
67 #define SC_ERXDC_MASK GENMASK(9, 5)
68 #define SC_ERXDC_OFFSET 5
70 #define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
72 #define AHB_GATE_OFFSET_EPHY 0
75 #define SUN8I_IOMUX_H3 2
76 #define SUN8I_IOMUX_R40 5
79 /* H3/A64 EMAC Register's offset */
80 #define EMAC_CTL0 0x00
81 #define EMAC_CTL1 0x04
82 #define EMAC_INT_STA 0x08
83 #define EMAC_INT_EN 0x0c
84 #define EMAC_TX_CTL0 0x10
85 #define EMAC_TX_CTL1 0x14
86 #define EMAC_TX_FLOW_CTL 0x1c
87 #define EMAC_TX_DMA_DESC 0x20
88 #define EMAC_RX_CTL0 0x24
89 #define EMAC_RX_CTL1 0x28
90 #define EMAC_RX_DMA_DESC 0x34
91 #define EMAC_MII_CMD 0x48
92 #define EMAC_MII_DATA 0x4c
93 #define EMAC_ADDR0_HIGH 0x50
94 #define EMAC_ADDR0_LOW 0x54
95 #define EMAC_TX_DMA_STA 0xb0
96 #define EMAC_TX_CUR_DESC 0xb4
97 #define EMAC_TX_CUR_BUF 0xb8
98 #define EMAC_RX_DMA_STA 0xc0
99 #define EMAC_RX_CUR_DESC 0xc4
101 DECLARE_GLOBAL_DATA_PTR;
110 struct emac_dma_desc {
115 } __aligned(ARCH_DMA_MINALIGN);
117 struct emac_eth_dev {
118 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
119 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
120 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
121 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
133 bool use_internal_phy;
135 enum emac_variant variant;
137 phys_addr_t sysctl_reg;
138 struct phy_device *phydev;
141 struct reset_ctl tx_rst;
142 #ifdef CONFIG_DM_GPIO
143 struct gpio_desc reset_gpio;
148 struct sun8i_eth_pdata {
149 struct eth_pdata eth_pdata;
156 static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
158 struct udevice *dev = bus->priv;
159 struct emac_eth_dev *priv = dev_get_priv(dev);
162 int timeout = CONFIG_MDIO_TIMEOUT;
164 miiaddr &= ~MDIO_CMD_MII_WRITE;
165 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
166 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
167 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
169 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
171 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
172 MDIO_CMD_MII_PHY_ADDR_MASK;
174 miiaddr |= MDIO_CMD_MII_BUSY;
176 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
178 start = get_timer(0);
179 while (get_timer(start) < timeout) {
180 if (!(readl(priv->mac_reg + EMAC_MII_CMD) & MDIO_CMD_MII_BUSY))
181 return readl(priv->mac_reg + EMAC_MII_DATA);
188 static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
191 struct udevice *dev = bus->priv;
192 struct emac_eth_dev *priv = dev_get_priv(dev);
195 int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
197 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
198 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
199 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
201 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
202 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
203 MDIO_CMD_MII_PHY_ADDR_MASK;
205 miiaddr |= MDIO_CMD_MII_WRITE;
206 miiaddr |= MDIO_CMD_MII_BUSY;
208 writel(val, priv->mac_reg + EMAC_MII_DATA);
209 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
211 start = get_timer(0);
212 while (get_timer(start) < timeout) {
213 if (!(readl(priv->mac_reg + EMAC_MII_CMD) &
214 MDIO_CMD_MII_BUSY)) {
224 static int _sun8i_write_hwaddr(struct emac_eth_dev *priv, u8 *mac_id)
226 u32 macid_lo, macid_hi;
228 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
230 macid_hi = mac_id[4] + (mac_id[5] << 8);
232 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
233 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
238 static void sun8i_adjust_link(struct emac_eth_dev *priv,
239 struct phy_device *phydev)
243 v = readl(priv->mac_reg + EMAC_CTL0);
252 switch (phydev->speed) {
263 writel(v, priv->mac_reg + EMAC_CTL0);
266 static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
268 if (priv->use_internal_phy) {
269 /* H3 based SoC's that has an Internal 100MBit PHY
270 * needs to be configured and powered up before use
272 *reg &= ~H3_EPHY_DEFAULT_MASK;
273 *reg |= H3_EPHY_DEFAULT_VALUE;
274 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
275 *reg &= ~H3_EPHY_SHUTDOWN;
276 *reg |= H3_EPHY_SELECT;
278 /* This is to select External Gigabit PHY on
279 * the boards with H3 SoC.
281 *reg &= ~H3_EPHY_SELECT;
286 static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
287 struct emac_eth_dev *priv)
292 if (priv->variant == R40_GMAC) {
293 /* Select RGMII for R40 */
294 reg = readl(priv->sysctl_reg + 0x164);
295 reg |= CCM_GMAC_CTRL_TX_CLK_SRC_INT_RGMII |
296 CCM_GMAC_CTRL_GPIT_RGMII |
297 CCM_GMAC_CTRL_TX_CLK_DELAY(CONFIG_GMAC_TX_DELAY);
299 writel(reg, priv->sysctl_reg + 0x164);
303 reg = readl(priv->sysctl_reg + 0x30);
305 if (priv->variant == H3_EMAC) {
306 ret = sun8i_emac_set_syscon_ephy(priv, ®);
311 reg &= ~(SC_ETCS_MASK | SC_EPIT);
312 if (priv->variant == H3_EMAC || priv->variant == A64_EMAC)
315 switch (priv->interface) {
316 case PHY_INTERFACE_MODE_MII:
319 case PHY_INTERFACE_MODE_RGMII:
320 reg |= SC_EPIT | SC_ETCS_INT_GMII;
322 case PHY_INTERFACE_MODE_RMII:
323 if (priv->variant == H3_EMAC ||
324 priv->variant == A64_EMAC) {
325 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
328 /* RMII not supported on A83T */
330 debug("%s: Invalid PHY interface\n", __func__);
334 if (pdata->tx_delay_ps)
335 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
338 if (pdata->rx_delay_ps)
339 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
342 writel(reg, priv->sysctl_reg + 0x30);
347 static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
349 struct phy_device *phydev;
351 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
355 phy_connect_dev(phydev, dev);
357 priv->phydev = phydev;
358 phy_config(priv->phydev);
363 static void rx_descs_init(struct emac_eth_dev *priv)
365 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
366 char *rxbuffs = &priv->rxbuffer[0];
367 struct emac_dma_desc *desc_p;
370 /* flush Rx buffers */
371 flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs +
374 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
375 desc_p = &desc_table_p[idx];
376 desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
378 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
379 desc_p->st |= CONFIG_ETH_RXSIZE;
380 desc_p->status = BIT(31);
383 /* Correcting the last pointer of the chain */
384 desc_p->next = (uintptr_t)&desc_table_p[0];
386 flush_dcache_range((uintptr_t)priv->rx_chain,
387 (uintptr_t)priv->rx_chain +
388 sizeof(priv->rx_chain));
390 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
391 priv->rx_currdescnum = 0;
394 static void tx_descs_init(struct emac_eth_dev *priv)
396 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
397 char *txbuffs = &priv->txbuffer[0];
398 struct emac_dma_desc *desc_p;
401 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
402 desc_p = &desc_table_p[idx];
403 desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
405 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
406 desc_p->status = (1 << 31);
410 /* Correcting the last pointer of the chain */
411 desc_p->next = (uintptr_t)&desc_table_p[0];
413 /* Flush all Tx buffer descriptors */
414 flush_dcache_range((uintptr_t)priv->tx_chain,
415 (uintptr_t)priv->tx_chain +
416 sizeof(priv->tx_chain));
418 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
419 priv->tx_currdescnum = 0;
422 static int _sun8i_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
427 reg = readl((priv->mac_reg + EMAC_CTL1));
431 setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
433 reg = readl(priv->mac_reg + EMAC_CTL1);
434 } while ((reg & 0x01) != 0 && (--timeout));
436 printf("%s: Timeout\n", __func__);
441 /* Rewrite mac address after reset */
442 _sun8i_write_hwaddr(priv, enetaddr);
444 v = readl(priv->mac_reg + EMAC_TX_CTL1);
445 /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/
447 writel(v, priv->mac_reg + EMAC_TX_CTL1);
449 v = readl(priv->mac_reg + EMAC_RX_CTL1);
450 /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a
451 * complete frame has been written to RX DMA FIFO
454 writel(v, priv->mac_reg + EMAC_RX_CTL1);
457 writel(8 << 24, priv->mac_reg + EMAC_CTL1);
459 /* Initialize rx/tx descriptors */
464 phy_startup(priv->phydev);
466 sun8i_adjust_link(priv, priv->phydev);
469 v = readl(priv->mac_reg + EMAC_RX_CTL1);
471 writel(v, priv->mac_reg + EMAC_RX_CTL1);
473 v = readl(priv->mac_reg + EMAC_TX_CTL1);
475 writel(v, priv->mac_reg + EMAC_TX_CTL1);
478 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
479 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
484 static int parse_phy_pins(struct udevice *dev)
486 struct emac_eth_dev *priv = dev_get_priv(dev);
488 const char *pin_name;
489 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
491 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
494 printf("WARNING: emac: cannot find pinctrl-0 node\n");
498 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
499 "drive-strength", ~0);
502 drive = SUN4I_PINCTRL_10_MA;
503 else if (drive <= 20)
504 drive = SUN4I_PINCTRL_20_MA;
505 else if (drive <= 30)
506 drive = SUN4I_PINCTRL_30_MA;
508 drive = SUN4I_PINCTRL_40_MA;
511 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
512 pull = SUN4I_PINCTRL_PULL_UP;
513 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
514 pull = SUN4I_PINCTRL_PULL_DOWN;
519 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
524 pin = sunxi_name_to_gpio(pin_name);
528 if (priv->variant == H3_EMAC)
529 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
530 else if (priv->variant == R40_GMAC)
531 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
533 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
536 sunxi_gpio_set_drv(pin, drive);
538 sunxi_gpio_set_pull(pin, pull);
542 printf("WARNING: emac: cannot find pins property\n");
549 static int _sun8i_eth_recv(struct emac_eth_dev *priv, uchar **packetp)
551 u32 status, desc_num = priv->rx_currdescnum;
552 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
553 int length = -EAGAIN;
555 uintptr_t desc_start = (uintptr_t)desc_p;
556 uintptr_t desc_end = desc_start +
557 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
559 ulong data_start = (uintptr_t)desc_p->buf_addr;
562 /* Invalidate entire buffer descriptor */
563 invalidate_dcache_range(desc_start, desc_end);
565 status = desc_p->status;
567 /* Check for DMA own bit */
568 if (!(status & BIT(31))) {
569 length = (desc_p->status >> 16) & 0x3FFF;
573 debug("RX: Bad Packet (runt)\n");
576 data_end = data_start + length;
577 /* Invalidate received data */
578 invalidate_dcache_range(rounddown(data_start,
583 if (length > CONFIG_ETH_RXSIZE) {
584 printf("Received packet is too big (len=%d)\n",
588 *packetp = (uchar *)(ulong)desc_p->buf_addr;
596 static int _sun8i_emac_eth_send(struct emac_eth_dev *priv, void *packet,
599 u32 v, desc_num = priv->tx_currdescnum;
600 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
601 uintptr_t desc_start = (uintptr_t)desc_p;
602 uintptr_t desc_end = desc_start +
603 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
605 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
606 uintptr_t data_end = data_start +
607 roundup(len, ARCH_DMA_MINALIGN);
609 /* Invalidate entire buffer descriptor */
610 invalidate_dcache_range(desc_start, desc_end);
613 /* Mandatory undocumented bit */
614 desc_p->st |= BIT(24);
616 memcpy((void *)data_start, packet, len);
618 /* Flush data to be sent */
619 flush_dcache_range(data_start, data_end);
622 desc_p->st |= BIT(30);
623 desc_p->st |= BIT(31);
626 desc_p->st |= BIT(29);
627 desc_p->status = BIT(31);
629 /*Descriptors st and status field has changed, so FLUSH it */
630 flush_dcache_range(desc_start, desc_end);
632 /* Move to next Descriptor and wrap around */
633 if (++desc_num >= CONFIG_TX_DESCR_NUM)
635 priv->tx_currdescnum = desc_num;
638 v = readl(priv->mac_reg + EMAC_TX_CTL1);
639 v |= BIT(31);/* mandatory */
640 v |= BIT(30);/* mandatory */
641 writel(v, priv->mac_reg + EMAC_TX_CTL1);
646 static int sun8i_eth_write_hwaddr(struct udevice *dev)
648 struct eth_pdata *pdata = dev_get_platdata(dev);
649 struct emac_eth_dev *priv = dev_get_priv(dev);
651 return _sun8i_write_hwaddr(priv, pdata->enetaddr);
654 static int sun8i_emac_board_setup(struct emac_eth_dev *priv)
656 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
659 ret = clk_enable(&priv->tx_clk);
661 dev_err(dev, "failed to enable TX clock\n");
665 if (reset_valid(&priv->tx_rst)) {
666 ret = reset_deassert(&priv->tx_rst);
668 dev_err(dev, "failed to deassert TX reset\n");
673 if (priv->variant == H3_EMAC) {
674 /* Only H3/H5 have clock controls for internal EPHY */
675 if (priv->use_internal_phy) {
676 /* Set clock gating for ephy */
677 setbits_le32(&ccm->bus_gate4,
678 BIT(AHB_GATE_OFFSET_EPHY));
681 setbits_le32(&ccm->ahb_reset2_cfg,
682 BIT(AHB_RESET_OFFSET_EPHY));
689 clk_disable(&priv->tx_clk);
693 #if defined(CONFIG_DM_GPIO)
694 static int sun8i_mdio_reset(struct mii_dev *bus)
696 struct udevice *dev = bus->priv;
697 struct emac_eth_dev *priv = dev_get_priv(dev);
698 struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
701 if (!dm_gpio_is_valid(&priv->reset_gpio))
705 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
709 udelay(pdata->reset_delays[0]);
711 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
715 udelay(pdata->reset_delays[1]);
717 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
721 udelay(pdata->reset_delays[2]);
727 static int sun8i_mdio_init(const char *name, struct udevice *priv)
729 struct mii_dev *bus = mdio_alloc();
732 debug("Failed to allocate MDIO bus\n");
736 bus->read = sun8i_mdio_read;
737 bus->write = sun8i_mdio_write;
738 snprintf(bus->name, sizeof(bus->name), name);
739 bus->priv = (void *)priv;
740 #if defined(CONFIG_DM_GPIO)
741 bus->reset = sun8i_mdio_reset;
744 return mdio_register(bus);
747 static int sun8i_emac_eth_start(struct udevice *dev)
749 struct eth_pdata *pdata = dev_get_platdata(dev);
751 return _sun8i_emac_eth_init(dev->priv, pdata->enetaddr);
754 static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
756 struct emac_eth_dev *priv = dev_get_priv(dev);
758 return _sun8i_emac_eth_send(priv, packet, length);
761 static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
763 struct emac_eth_dev *priv = dev_get_priv(dev);
765 return _sun8i_eth_recv(priv, packetp);
768 static int _sun8i_free_pkt(struct emac_eth_dev *priv)
770 u32 desc_num = priv->rx_currdescnum;
771 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
772 uintptr_t desc_start = (uintptr_t)desc_p;
773 uintptr_t desc_end = desc_start +
774 roundup(sizeof(u32), ARCH_DMA_MINALIGN);
776 /* Make the current descriptor valid again */
777 desc_p->status |= BIT(31);
779 /* Flush Status field of descriptor */
780 flush_dcache_range(desc_start, desc_end);
782 /* Move to next desc and wrap-around condition. */
783 if (++desc_num >= CONFIG_RX_DESCR_NUM)
785 priv->rx_currdescnum = desc_num;
790 static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
793 struct emac_eth_dev *priv = dev_get_priv(dev);
795 return _sun8i_free_pkt(priv);
798 static void sun8i_emac_eth_stop(struct udevice *dev)
800 struct emac_eth_dev *priv = dev_get_priv(dev);
802 /* Stop Rx/Tx transmitter */
803 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
804 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
807 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30));
809 phy_shutdown(priv->phydev);
812 static int sun8i_emac_eth_probe(struct udevice *dev)
814 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
815 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
816 struct emac_eth_dev *priv = dev_get_priv(dev);
819 priv->mac_reg = (void *)pdata->iobase;
821 ret = sun8i_emac_board_setup(priv);
825 sun8i_emac_set_syscon(sun8i_pdata, priv);
827 sun8i_mdio_init(dev->name, dev);
828 priv->bus = miiphy_get_dev_by_name(dev->name);
830 return sun8i_phy_init(priv, dev);
833 static const struct eth_ops sun8i_emac_eth_ops = {
834 .start = sun8i_emac_eth_start,
835 .write_hwaddr = sun8i_eth_write_hwaddr,
836 .send = sun8i_emac_eth_send,
837 .recv = sun8i_emac_eth_recv,
838 .free_pkt = sun8i_eth_free_pkt,
839 .stop = sun8i_emac_eth_stop,
842 static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
844 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
845 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
846 struct emac_eth_dev *priv = dev_get_priv(dev);
847 const char *phy_mode;
849 int node = dev_of_offset(dev);
851 #ifdef CONFIG_DM_GPIO
852 int reset_flags = GPIOD_IS_OUT;
856 pdata->iobase = devfdt_get_addr(dev);
857 if (pdata->iobase == FDT_ADDR_T_NONE) {
858 debug("%s: Cannot find MAC base address\n", __func__);
862 priv->variant = dev_get_driver_data(dev);
864 if (!priv->variant) {
865 printf("%s: Missing variant\n", __func__);
869 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
871 dev_err(dev, "failed to get TX clock\n");
875 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
876 if (ret && ret != -ENOENT) {
877 dev_err(dev, "failed to get TX reset\n");
881 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
883 debug("%s: cannot find syscon node\n", __func__);
887 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
889 debug("%s: cannot find reg property in syscon node\n",
893 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
895 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
896 debug("%s: Cannot find syscon base address\n", __func__);
900 pdata->phy_interface = -1;
902 priv->use_internal_phy = false;
904 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
906 debug("%s: Cannot find PHY address\n", __func__);
909 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
911 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
914 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
915 printf("phy interface%d\n", pdata->phy_interface);
917 if (pdata->phy_interface == -1) {
918 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
922 if (priv->variant == H3_EMAC) {
923 int parent = fdt_parent_offset(gd->fdt_blob, offset);
926 !fdt_node_check_compatible(gd->fdt_blob, parent,
927 "allwinner,sun8i-h3-mdio-internal"))
928 priv->use_internal_phy = true;
931 priv->interface = pdata->phy_interface;
933 if (!priv->use_internal_phy)
936 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
937 "allwinner,tx-delay-ps", 0);
938 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
939 printf("%s: Invalid TX delay value %d\n", __func__,
940 sun8i_pdata->tx_delay_ps);
942 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
943 "allwinner,rx-delay-ps", 0);
944 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
945 printf("%s: Invalid RX delay value %d\n", __func__,
946 sun8i_pdata->rx_delay_ps);
948 #ifdef CONFIG_DM_GPIO
949 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
950 "snps,reset-active-low"))
951 reset_flags |= GPIOD_ACTIVE_LOW;
953 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
954 &priv->reset_gpio, reset_flags);
957 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
958 "snps,reset-delays-us",
959 sun8i_pdata->reset_delays, 3);
960 } else if (ret == -ENOENT) {
968 static const struct udevice_id sun8i_emac_eth_ids[] = {
969 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
970 {.compatible = "allwinner,sun50i-a64-emac",
971 .data = (uintptr_t)A64_EMAC },
972 {.compatible = "allwinner,sun8i-a83t-emac",
973 .data = (uintptr_t)A83T_EMAC },
974 {.compatible = "allwinner,sun8i-r40-gmac",
975 .data = (uintptr_t)R40_GMAC },
979 U_BOOT_DRIVER(eth_sun8i_emac) = {
980 .name = "eth_sun8i_emac",
982 .of_match = sun8i_emac_eth_ids,
983 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
984 .probe = sun8i_emac_eth_probe,
985 .ops = &sun8i_emac_eth_ops,
986 .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
987 .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
988 .flags = DM_FLAG_ALLOC_PRIV_DMA,